spi_flash_mmap.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * This file contains `spi_flash_mmap_xx` APIs, mainly for doing memory mapping
  8. * to an SPI0-connected external Flash, as well as some helper functions to
  9. * convert between virtual and physical address
  10. **/
  11. #pragma once
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include <stddef.h>
  15. #include "esp_err.h"
  16. #include "sdkconfig.h"
  17. #include "esp_spi_flash_counters.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define ESP_ERR_FLASH_OP_FAIL (ESP_ERR_FLASH_BASE + 1)
  22. #define ESP_ERR_FLASH_OP_TIMEOUT (ESP_ERR_FLASH_BASE + 2)
  23. #define SPI_FLASH_SEC_SIZE 4096 /**< SPI Flash sector size */
  24. #define SPI_FLASH_MMU_PAGE_SIZE CONFIG_MMU_PAGE_SIZE /**< Flash cache MMU mapping page size */
  25. /**
  26. * @brief Enumeration which specifies memory space requested in an mmap call
  27. */
  28. typedef enum {
  29. SPI_FLASH_MMAP_DATA, /**< map to data memory, allows byte-aligned access*/
  30. SPI_FLASH_MMAP_INST, /**< map to instruction memory, allows only 4-byte-aligned access*/
  31. } spi_flash_mmap_memory_t;
  32. /**
  33. * @brief Opaque handle for memory region obtained from spi_flash_mmap.
  34. */
  35. typedef uint32_t spi_flash_mmap_handle_t;
  36. /**
  37. * @brief Map region of flash memory into data or instruction address space
  38. *
  39. * This function allocates sufficient number of 64kB MMU pages and configures
  40. * them to map the requested region of flash memory into the address space.
  41. * It may reuse MMU pages which already provide the required mapping.
  42. *
  43. * As with any allocator, if mmap/munmap are heavily used then the address space
  44. * may become fragmented. To troubleshoot issues with page allocation, use
  45. * spi_flash_mmap_dump() function.
  46. *
  47. * @param src_addr Physical address in flash where requested region starts.
  48. * This address *must* be aligned to 64kB boundary
  49. * (SPI_FLASH_MMU_PAGE_SIZE)
  50. * @param size Size of region to be mapped. This size will be rounded
  51. * up to a 64kB boundary
  52. * @param memory Address space where the region should be mapped (data or instruction)
  53. * @param[out] out_ptr Output, pointer to the mapped memory region
  54. * @param[out] out_handle Output, handle which should be used for spi_flash_munmap call
  55. *
  56. * @return ESP_OK on success, ESP_ERR_NO_MEM if pages can not be allocated
  57. */
  58. esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
  59. const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
  60. /**
  61. * @brief Map sequences of pages of flash memory into data or instruction address space
  62. *
  63. * This function allocates sufficient number of 64kB MMU pages and configures
  64. * them to map the indicated pages of flash memory contiguously into address space.
  65. * In this respect, it works in a similar way as spi_flash_mmap() but it allows mapping
  66. * a (maybe non-contiguous) set of pages into a contiguous region of memory.
  67. *
  68. * @param pages An array of numbers indicating the 64kB pages in flash to be mapped
  69. * contiguously into memory. These indicate the indexes of the 64kB pages,
  70. * not the byte-size addresses as used in other functions.
  71. * Array must be located in internal memory.
  72. * @param page_count Number of entries in the pages array
  73. * @param memory Address space where the region should be mapped (instruction or data)
  74. * @param[out] out_ptr Output, pointer to the mapped memory region
  75. * @param[out] out_handle Output, handle which should be used for spi_flash_munmap call
  76. *
  77. * @return
  78. * - ESP_OK on success
  79. * - ESP_ERR_NO_MEM if pages can not be allocated
  80. * - ESP_ERR_INVALID_ARG if pagecount is zero or pages array is not in
  81. * internal memory
  82. */
  83. esp_err_t spi_flash_mmap_pages(const int *pages, size_t page_count, spi_flash_mmap_memory_t memory,
  84. const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
  85. /**
  86. * @brief Release region previously obtained using spi_flash_mmap
  87. *
  88. * @note Calling this function will not necessarily unmap memory region.
  89. * Region will only be unmapped when there are no other handles which
  90. * reference this region. In case of partially overlapping regions
  91. * it is possible that memory will be unmapped partially.
  92. *
  93. * @param handle Handle obtained from spi_flash_mmap
  94. */
  95. void spi_flash_munmap(spi_flash_mmap_handle_t handle);
  96. /**
  97. * @brief Display information about mapped regions
  98. *
  99. * This function lists handles obtained using spi_flash_mmap, along with range
  100. * of pages allocated to each handle. It also lists all non-zero entries of
  101. * MMU table and corresponding reference counts.
  102. */
  103. void spi_flash_mmap_dump(void);
  104. /**
  105. * @brief get free pages number which can be mmap
  106. *
  107. * This function will return number of free pages available in mmu table. This could be useful
  108. * before calling actual spi_flash_mmap (maps flash range to DCache or ICache memory) to check
  109. * if there is sufficient space available for mapping.
  110. *
  111. * @param memory memory type of MMU table free page
  112. *
  113. * @return number of free pages which can be mmaped
  114. */
  115. uint32_t spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory);
  116. #define SPI_FLASH_CACHE2PHYS_FAIL UINT32_MAX /*<! Result from spi_flash_cache2phys() if flash cache address is invalid */
  117. /**
  118. * @brief Given a memory address where flash is mapped, return the corresponding physical flash offset.
  119. *
  120. * Cache address does not have have been assigned via spi_flash_mmap(), any address in memory mapped flash space can be looked up.
  121. *
  122. * @param cached Pointer to flashed cached memory.
  123. *
  124. * @return
  125. * - SPI_FLASH_CACHE2PHYS_FAIL If cache address is outside flash cache region, or the address is not mapped.
  126. * - Otherwise, returns physical offset in flash
  127. */
  128. size_t spi_flash_cache2phys(const void *cached);
  129. /** @brief Given a physical offset in flash, return the address where it is mapped in the memory space.
  130. *
  131. * Physical address does not have to have been assigned via spi_flash_mmap(), any address in flash can be looked up.
  132. *
  133. * @note Only the first matching cache address is returned. If MMU flash cache table is configured so multiple entries
  134. * point to the same physical address, there may be more than one cache address corresponding to that physical
  135. * address. It is also possible for a single physical address to be mapped to both the IROM and DROM regions.
  136. *
  137. * @note This function doesn't impose any alignment constraints, but if memory argument is SPI_FLASH_MMAP_INST and
  138. * phys_offs is not 4-byte aligned, then reading from the returned pointer will result in a crash.
  139. *
  140. * @param phys_offs Physical offset in flash memory to look up.
  141. * @param memory Address space type to look up a flash cache address mapping for (instruction or data)
  142. *
  143. * @return
  144. * - NULL if the physical address is invalid or not mapped to flash cache of the specified memory type.
  145. * - Cached memory address (in IROM or DROM space) corresponding to phys_offs.
  146. */
  147. const void *spi_flash_phys2cache(size_t phys_offs, spi_flash_mmap_memory_t memory);
  148. #ifdef __cplusplus
  149. }
  150. #endif