esp_mmu_map.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <sys/param.h>
  9. #include <sys/queue.h>
  10. #include <inttypes.h>
  11. #include "sdkconfig.h"
  12. #include "esp_attr.h"
  13. #include "esp_log.h"
  14. #include "esp_check.h"
  15. #include "esp_heap_caps.h"
  16. #include "soc/soc_caps.h"
  17. #include "hal/cache_types.h"
  18. #include "hal/cache_hal.h"
  19. #include "hal/cache_ll.h"
  20. #include "hal/mmu_types.h"
  21. #include "hal/mmu_hal.h"
  22. #include "hal/mmu_ll.h"
  23. #include "esp_private/cache_utils.h"
  24. #include "esp_private/esp_cache_esp32_private.h"
  25. #include "esp_private/esp_mmu_map_private.h"
  26. #include "ext_mem_layout.h"
  27. #include "esp_mmu_map.h"
  28. //This is for size align
  29. #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
  30. //This is for vaddr align
  31. #define ALIGN_DOWN_BY(num, align) ((num) & (~((align) - 1)))
  32. //This flag indicates the memory region is merged, we don't care about it anymore
  33. #define MEM_REGION_MERGED -1
  34. /**
  35. * We have some hw related tests for vaddr region capabilites
  36. * Use this macro to disable paddr check as we need to reuse certain paddr blocks
  37. */
  38. #define ENABLE_PADDR_CHECK !ESP_MMAP_TEST_ALLOW_MAP_TO_MAPPED_PADDR
  39. static DRAM_ATTR const char *TAG = "mmap";
  40. /**
  41. * @brief MMU Memory Mapping Driver
  42. *
  43. * Driver Backgrounds:
  44. *
  45. * --------------------------------------------------------------------------------------------------------
  46. * Memory Pool |
  47. * --------------------------------------------------------------------------------------------------------
  48. * | Memory Region 0 | Memory Region 1 | ... |
  49. * --------------------------------------------------------------------------------------------------------
  50. * | Block 0 | Slot 0 | Block 1 | Block 2 | ... | Slot 1 (final slot) | ... |
  51. * --------------------------------------------------------------------------------------------------------
  52. *
  53. * - A block is a piece of vaddr range that is dynamically mapped. Blocks are doubly linked:
  54. * Block 0 <-> Block 1 <-> Block 2
  55. * - A Slot is the vaddr range between 2 blocks.
  56. */
  57. /**
  58. * Struct for a block
  59. */
  60. typedef struct mem_block_ {
  61. uint32_t laddr_start; //linear address start of this block
  62. uint32_t laddr_end; //linear address end of this block
  63. intptr_t vaddr_start; //virtual address start of this block
  64. intptr_t vaddr_end; //virtual address end of this block
  65. size_t size; //size of this block, should be aligned to MMU page size
  66. int caps; //caps of this block, `mmu_mem_caps_t`
  67. uint32_t paddr_start; //physical address start of this block
  68. uint32_t paddr_end; //physical address end of this block
  69. mmu_target_t target; //physical target that this block is mapped to
  70. TAILQ_ENTRY(mem_block_) entries; //link entry
  71. } mem_block_t;
  72. /**
  73. * Struct for a memory region
  74. */
  75. typedef struct mem_region_ {
  76. cache_bus_mask_t bus_id; //cache bus mask of this region
  77. uint32_t start; //linear address start of this region
  78. uint32_t end; //linear address end of this region
  79. size_t region_size; //region size, in bytes
  80. uint32_t free_head; //linear address free head of this region
  81. size_t max_slot_size; //max slot size within this region
  82. int caps; //caps of this region, `mmu_mem_caps_t`
  83. mmu_target_t targets; //physical targets that this region is supported
  84. TAILQ_HEAD(mem_block_head_, mem_block_) mem_block_head; //link head of allocated blocks within this region
  85. } mem_region_t;
  86. typedef struct {
  87. /**
  88. * number of memory regions that are available, after coalescing, this number should be smaller than or equal to `SOC_MMU_LINEAR_ADDRESS_REGION_NUM`
  89. */
  90. uint32_t num_regions;
  91. /**
  92. * This saves the available MMU linear address regions,
  93. * after reserving flash .rodata and .text, and after coalescing.
  94. * Only the first `num_regions` items are valid
  95. */
  96. mem_region_t mem_regions[SOC_MMU_LINEAR_ADDRESS_REGION_NUM];
  97. } mmu_ctx_t;
  98. static mmu_ctx_t s_mmu_ctx;
  99. #if ENABLE_PADDR_CHECK
  100. static bool s_is_enclosed(uint32_t block_start, uint32_t block_end, uint32_t new_block_start, uint32_t new_block_size);
  101. static bool s_is_overlapped(uint32_t block_start, uint32_t block_end, uint32_t new_block_start, uint32_t new_block_size);
  102. #endif //#if ENABLE_PADDR_CHECK
  103. #if CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  104. static void s_reserve_irom_region(mem_region_t *hw_mem_regions, int region_nums)
  105. {
  106. /**
  107. * We follow the way how 1st bootloader load flash .text:
  108. *
  109. * - Now IBUS addresses (between `_instruction_reserved_start` and `_instruction_reserved_end`) are consecutive on all chips,
  110. * we strongly rely on this to calculate the .text length
  111. */
  112. extern int _instruction_reserved_start;
  113. extern int _instruction_reserved_end;
  114. size_t irom_len_to_reserve = (uint32_t)&_instruction_reserved_end - (uint32_t)&_instruction_reserved_start;
  115. assert((mmu_ll_vaddr_to_laddr((uint32_t)&_instruction_reserved_end) - mmu_ll_vaddr_to_laddr((uint32_t)&_instruction_reserved_start)) == irom_len_to_reserve);
  116. irom_len_to_reserve += (uint32_t)&_instruction_reserved_start - ALIGN_DOWN_BY((uint32_t)&_instruction_reserved_start, CONFIG_MMU_PAGE_SIZE);
  117. irom_len_to_reserve = ALIGN_UP_BY(irom_len_to_reserve, CONFIG_MMU_PAGE_SIZE);
  118. cache_bus_mask_t bus_mask = cache_ll_l1_get_bus(0, (uint32_t)&_instruction_reserved_start, irom_len_to_reserve);
  119. for (int i = 0; i < SOC_MMU_LINEAR_ADDRESS_REGION_NUM; i++) {
  120. if (bus_mask & hw_mem_regions[i].bus_id) {
  121. if (hw_mem_regions[i].region_size <= irom_len_to_reserve) {
  122. hw_mem_regions[i].free_head = hw_mem_regions[i].end;
  123. hw_mem_regions[i].max_slot_size = 0;
  124. irom_len_to_reserve -= hw_mem_regions[i].region_size;
  125. } else {
  126. hw_mem_regions[i].free_head = hw_mem_regions[i].free_head + irom_len_to_reserve;
  127. hw_mem_regions[i].max_slot_size -= irom_len_to_reserve;
  128. }
  129. }
  130. }
  131. }
  132. static void s_reserve_drom_region(mem_region_t *hw_mem_regions, int region_nums)
  133. {
  134. /**
  135. * Similarly, we follow the way how 1st bootloader load flash .rodata:
  136. */
  137. extern int _rodata_reserved_start;
  138. extern int _rodata_reserved_end;
  139. size_t drom_len_to_reserve = (uint32_t)&_rodata_reserved_end - (uint32_t)&_rodata_reserved_start;
  140. assert((mmu_ll_vaddr_to_laddr((uint32_t)&_rodata_reserved_end) - mmu_ll_vaddr_to_laddr((uint32_t)&_rodata_reserved_start)) == drom_len_to_reserve);
  141. drom_len_to_reserve += (uint32_t)&_rodata_reserved_start - ALIGN_DOWN_BY((uint32_t)&_rodata_reserved_start, CONFIG_MMU_PAGE_SIZE);
  142. drom_len_to_reserve = ALIGN_UP_BY(drom_len_to_reserve, CONFIG_MMU_PAGE_SIZE);
  143. cache_bus_mask_t bus_mask = cache_ll_l1_get_bus(0, (uint32_t)&_rodata_reserved_start, drom_len_to_reserve);
  144. for (int i = 0; i < SOC_MMU_LINEAR_ADDRESS_REGION_NUM; i++) {
  145. if (bus_mask & hw_mem_regions[i].bus_id) {
  146. if (hw_mem_regions[i].region_size <= drom_len_to_reserve) {
  147. hw_mem_regions[i].free_head = hw_mem_regions[i].end;
  148. hw_mem_regions[i].max_slot_size = 0;
  149. drom_len_to_reserve -= hw_mem_regions[i].region_size;
  150. } else {
  151. hw_mem_regions[i].free_head = hw_mem_regions[i].free_head + drom_len_to_reserve;
  152. hw_mem_regions[i].max_slot_size -= drom_len_to_reserve;
  153. }
  154. }
  155. }
  156. }
  157. #endif //#if CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  158. void esp_mmu_map_init(void)
  159. {
  160. mem_region_t hw_mem_regions[SOC_MMU_LINEAR_ADDRESS_REGION_NUM] = {};
  161. for (int i = 0; i < SOC_MMU_LINEAR_ADDRESS_REGION_NUM; i++) {
  162. hw_mem_regions[i].start = g_mmu_mem_regions[i].start;
  163. hw_mem_regions[i].end = g_mmu_mem_regions[i].end;
  164. hw_mem_regions[i].region_size = g_mmu_mem_regions[i].size;
  165. hw_mem_regions[i].max_slot_size = g_mmu_mem_regions[i].size;
  166. hw_mem_regions[i].free_head = g_mmu_mem_regions[i].start;
  167. hw_mem_regions[i].bus_id = g_mmu_mem_regions[i].bus_id;
  168. hw_mem_regions[i].caps = g_mmu_mem_regions[i].caps;
  169. hw_mem_regions[i].targets = g_mmu_mem_regions[i].targets;
  170. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  171. assert(__builtin_popcount(hw_mem_regions[i].bus_id) == 1);
  172. #endif
  173. assert(hw_mem_regions[i].region_size % CONFIG_MMU_PAGE_SIZE == 0);
  174. }
  175. #if CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  176. //First reserve memory regions used for irom and drom, as we must follow the way how 1st bootloader load them
  177. s_reserve_irom_region(hw_mem_regions, SOC_MMU_LINEAR_ADDRESS_REGION_NUM);
  178. s_reserve_drom_region(hw_mem_regions, SOC_MMU_LINEAR_ADDRESS_REGION_NUM);
  179. #endif //#if CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  180. if (SOC_MMU_LINEAR_ADDRESS_REGION_NUM > 1) {
  181. //Now we can coalesce adjacent regions
  182. for (int i = 1; i < SOC_MMU_LINEAR_ADDRESS_REGION_NUM; i++) {
  183. mem_region_t *a = &hw_mem_regions[i - 1];
  184. mem_region_t *b = &hw_mem_regions[i];
  185. if ((b->free_head == a->end) && (b->caps == a->caps) && (b->targets == a->targets)) {
  186. a->caps = MEM_REGION_MERGED;
  187. b->bus_id |= a->bus_id;
  188. b->start = a->start;
  189. b->region_size += a->region_size;
  190. b->free_head = a->free_head;
  191. b->max_slot_size += a->max_slot_size;
  192. }
  193. }
  194. }
  195. //Count the mem regions left after coalescing
  196. uint32_t region_num = 0;
  197. for (int i = 0; i < SOC_MMU_LINEAR_ADDRESS_REGION_NUM; i++) {
  198. if(hw_mem_regions[i].caps != MEM_REGION_MERGED) {
  199. region_num++;
  200. }
  201. }
  202. ESP_EARLY_LOGV(TAG, "after coalescing, %d regions are left", region_num);
  203. //Initialise `s_mmu_ctx.mem_regions[]`, as we've done all static allocation, to prepare available virtual memory regions
  204. uint32_t available_region_idx = 0;
  205. s_mmu_ctx.num_regions = region_num;
  206. for (int i = 0; i < SOC_MMU_LINEAR_ADDRESS_REGION_NUM; i++) {
  207. if (hw_mem_regions[i].caps == MEM_REGION_MERGED) {
  208. continue;
  209. }
  210. memcpy(&s_mmu_ctx.mem_regions[available_region_idx], &hw_mem_regions[i], sizeof(mem_region_t));
  211. available_region_idx++;
  212. }
  213. for (int i = 0; i < available_region_idx; i++) {
  214. TAILQ_INIT(&s_mmu_ctx.mem_regions[i].mem_block_head);
  215. }
  216. assert(available_region_idx == region_num);
  217. }
  218. static esp_err_t s_mem_caps_check(mmu_mem_caps_t caps)
  219. {
  220. if (caps & MMU_MEM_CAP_EXEC) {
  221. if ((caps & MMU_MEM_CAP_8BIT) || (caps & MMU_MEM_CAP_WRITE)) {
  222. //None of the executable memory are expected to be 8-bit accessible or writable.
  223. return ESP_ERR_INVALID_ARG;
  224. }
  225. caps |= MMU_MEM_CAP_32BIT;
  226. }
  227. return ESP_OK;
  228. }
  229. esp_err_t esp_mmu_map_get_max_consecutive_free_block_size(mmu_mem_caps_t caps, mmu_target_t target, size_t *out_len)
  230. {
  231. ESP_RETURN_ON_FALSE(out_len, ESP_ERR_INVALID_ARG, TAG, "null pointer");
  232. ESP_RETURN_ON_ERROR(s_mem_caps_check(caps), TAG, "invalid caps");
  233. *out_len = 0;
  234. size_t max = 0;
  235. for (int i = 0; i < s_mmu_ctx.num_regions; i++) {
  236. if (((s_mmu_ctx.mem_regions[i].caps & caps) == caps) && ((s_mmu_ctx.mem_regions[i].targets & target) == target)) {
  237. if (s_mmu_ctx.mem_regions[i].max_slot_size > max) {
  238. max = s_mmu_ctx.mem_regions[i].max_slot_size;
  239. }
  240. }
  241. }
  242. *out_len = max;
  243. return ESP_OK;
  244. }
  245. static int32_t s_find_available_region(mem_region_t *mem_regions, uint32_t region_nums, size_t size, mmu_mem_caps_t caps, mmu_target_t target)
  246. {
  247. int32_t found_region_id = -1;
  248. for (int i = 0; i < region_nums; i++) {
  249. if (((mem_regions[i].caps & caps) == caps) && ((mem_regions[i].targets & target) == target)) {
  250. if (mem_regions[i].max_slot_size >= size) {
  251. found_region_id = i;
  252. break;
  253. }
  254. }
  255. }
  256. return found_region_id;
  257. }
  258. esp_err_t esp_mmu_map_reserve_block_with_caps(size_t size, mmu_mem_caps_t caps, mmu_target_t target, const void **out_ptr)
  259. {
  260. ESP_RETURN_ON_FALSE(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
  261. ESP_RETURN_ON_ERROR(s_mem_caps_check(caps), TAG, "invalid caps");
  262. size_t aligned_size = ALIGN_UP_BY(size, CONFIG_MMU_PAGE_SIZE);
  263. uint32_t laddr = 0;
  264. int32_t found_region_id = s_find_available_region(s_mmu_ctx.mem_regions, s_mmu_ctx.num_regions, aligned_size, caps, target);
  265. if (found_region_id == -1) {
  266. ESP_EARLY_LOGE(TAG, "no such vaddr range");
  267. return ESP_ERR_NOT_FOUND;
  268. }
  269. laddr = (uint32_t)s_mmu_ctx.mem_regions[found_region_id].free_head;
  270. s_mmu_ctx.mem_regions[found_region_id].free_head += aligned_size;
  271. s_mmu_ctx.mem_regions[found_region_id].max_slot_size -= aligned_size;
  272. ESP_EARLY_LOGV(TAG, "found laddr is 0x%x", laddr);
  273. uint32_t vaddr = 0;
  274. if (caps & MMU_MEM_CAP_EXEC) {
  275. vaddr = mmu_ll_laddr_to_vaddr(laddr, MMU_VADDR_INSTRUCTION, target);
  276. } else {
  277. vaddr = mmu_ll_laddr_to_vaddr(laddr, MMU_VADDR_DATA, target);
  278. }
  279. *out_ptr = (void *)vaddr;
  280. return ESP_OK;
  281. }
  282. IRAM_ATTR esp_err_t esp_mmu_paddr_find_caps(const esp_paddr_t paddr, mmu_mem_caps_t *out_caps)
  283. {
  284. mem_region_t *region = NULL;
  285. mem_block_t *mem_block = NULL;
  286. bool found = false;
  287. mem_block_t *found_block = NULL;
  288. if (out_caps == NULL) {
  289. return ESP_ERR_INVALID_ARG;
  290. }
  291. for (int i = 0; i < s_mmu_ctx.num_regions; i++) {
  292. region = &s_mmu_ctx.mem_regions[i];
  293. TAILQ_FOREACH(mem_block, &region->mem_block_head, entries) {
  294. if (mem_block == TAILQ_FIRST(&region->mem_block_head) || mem_block == TAILQ_LAST(&region->mem_block_head, mem_block_head_)) {
  295. //we don't care the dummy_head and the dummy_tail
  296. continue;
  297. }
  298. //now we are only traversing the actual dynamically allocated blocks, dummy_head and dummy_tail are excluded already
  299. if (mem_block->paddr_start == paddr) {
  300. found = true;
  301. found_block = mem_block;
  302. break;
  303. }
  304. }
  305. }
  306. if (!found) {
  307. return ESP_ERR_NOT_FOUND;
  308. }
  309. *out_caps = found_block->caps;
  310. return ESP_OK;
  311. }
  312. static void IRAM_ATTR NOINLINE_ATTR s_do_cache_invalidate(uint32_t vaddr_start, uint32_t size)
  313. {
  314. #if CONFIG_IDF_TARGET_ESP32
  315. /**
  316. * On ESP32, due to hardware limitation, we don't have an
  317. * easy way to sync between cache and external memory wrt
  318. * certain range. So we do a full sync here
  319. */
  320. cache_sync();
  321. #else //Other chips
  322. cache_hal_invalidate_addr(vaddr_start, size);
  323. #endif // CONFIG_IDF_TARGET_ESP32
  324. }
  325. #if MMU_LL_MMU_PER_TARGET
  326. FORCE_INLINE_ATTR uint32_t s_mapping_operation(mmu_target_t target, uint32_t vaddr_start, esp_paddr_t paddr_start, uint32_t size)
  327. {
  328. uint32_t actual_mapped_len = 0;
  329. uint32_t mmu_id = 0;
  330. if (target == MMU_TARGET_FLASH0) {
  331. mmu_id = MMU_LL_FLASH_MMU_ID;
  332. } else {
  333. mmu_id = MMU_LL_PSRAM_MMU_ID;
  334. }
  335. mmu_hal_map_region(mmu_id, target, vaddr_start, paddr_start, size, &actual_mapped_len);
  336. return actual_mapped_len;
  337. }
  338. #else
  339. FORCE_INLINE_ATTR uint32_t s_mapping_operation(mmu_target_t target, uint32_t vaddr_start, esp_paddr_t paddr_start, uint32_t size)
  340. {
  341. uint32_t actual_mapped_len = 0;
  342. mmu_hal_map_region(0, target, vaddr_start, paddr_start, size, &actual_mapped_len);
  343. #if (SOC_MMU_PERIPH_NUM == 2)
  344. #if !CONFIG_FREERTOS_UNICORE
  345. mmu_hal_map_region(1, target, vaddr_start, paddr_start, size, &actual_mapped_len);
  346. #endif // #if !CONFIG_FREERTOS_UNICORE
  347. #endif // #if (SOC_MMU_PERIPH_NUM == 2)
  348. return actual_mapped_len;
  349. }
  350. #endif
  351. static void IRAM_ATTR NOINLINE_ATTR s_do_mapping(mmu_target_t target, uint32_t vaddr_start, esp_paddr_t paddr_start, uint32_t size)
  352. {
  353. /**
  354. * Disable Cache, after this function, involved code and data should be placed in internal RAM.
  355. *
  356. * @note we call this for now, but this will be refactored to move out of `spi_flash`
  357. */
  358. spi_flash_disable_interrupts_caches_and_other_cpu();
  359. uint32_t actual_mapped_len = s_mapping_operation(target, vaddr_start, paddr_start, size);
  360. cache_bus_mask_t bus_mask = cache_ll_l1_get_bus(0, vaddr_start, size);
  361. cache_ll_l1_enable_bus(0, bus_mask);
  362. #if !CONFIG_FREERTOS_UNICORE
  363. bus_mask = cache_ll_l1_get_bus(0, vaddr_start, size);
  364. cache_ll_l1_enable_bus(1, bus_mask);
  365. #endif
  366. s_do_cache_invalidate(vaddr_start, size);
  367. //enable Cache, after this function, internal RAM access is no longer mandatory
  368. spi_flash_enable_interrupts_caches_and_other_cpu();
  369. ESP_EARLY_LOGV(TAG, "actual_mapped_len is 0x%"PRIx32, actual_mapped_len);
  370. }
  371. esp_err_t esp_mmu_map(esp_paddr_t paddr_start, size_t size, mmu_target_t target, mmu_mem_caps_t caps, int flags, void **out_ptr)
  372. {
  373. esp_err_t ret = ESP_FAIL;
  374. ESP_RETURN_ON_FALSE(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
  375. #if !SOC_SPIRAM_SUPPORTED || CONFIG_IDF_TARGET_ESP32
  376. ESP_RETURN_ON_FALSE(!(target & MMU_TARGET_PSRAM0), ESP_ERR_NOT_SUPPORTED, TAG, "PSRAM is not supported");
  377. #endif
  378. ESP_RETURN_ON_FALSE((paddr_start % CONFIG_MMU_PAGE_SIZE == 0), ESP_ERR_INVALID_ARG, TAG, "paddr must be rounded up to the nearest multiple of CONFIG_MMU_PAGE_SIZE");
  379. ESP_RETURN_ON_ERROR(s_mem_caps_check(caps), TAG, "invalid caps");
  380. size_t aligned_size = ALIGN_UP_BY(size, CONFIG_MMU_PAGE_SIZE);
  381. int32_t found_region_id = s_find_available_region(s_mmu_ctx.mem_regions, s_mmu_ctx.num_regions, aligned_size, caps, target);
  382. if (found_region_id == -1) {
  383. ESP_EARLY_LOGE(TAG, "no such vaddr range");
  384. return ESP_ERR_NOT_FOUND;
  385. }
  386. //Now we're sure we can find an available block inside a certain region
  387. mem_region_t *found_region = &s_mmu_ctx.mem_regions[found_region_id];
  388. mem_block_t *dummy_head = NULL;
  389. mem_block_t *dummy_tail = NULL;
  390. mem_block_t *new_block = NULL;
  391. if (TAILQ_EMPTY(&found_region->mem_block_head)) {
  392. dummy_head = (mem_block_t *)heap_caps_calloc(1, sizeof(mem_block_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  393. ESP_GOTO_ON_FALSE(dummy_head, ESP_ERR_NO_MEM, err, TAG, "no mem");
  394. dummy_head->laddr_start = found_region->free_head;
  395. dummy_head->laddr_end = found_region->free_head;
  396. //We don't care vaddr or paddr address for dummy head
  397. dummy_head->size = 0;
  398. dummy_head->caps = caps;
  399. TAILQ_INSERT_HEAD(&found_region->mem_block_head, dummy_head, entries);
  400. dummy_tail = (mem_block_t *)heap_caps_calloc(1, sizeof(mem_block_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  401. ESP_GOTO_ON_FALSE(dummy_tail, ESP_ERR_NO_MEM, err, TAG, "no mem");
  402. dummy_tail->laddr_start = found_region->end;
  403. dummy_tail->laddr_end = found_region->end;
  404. //We don't care vaddr or paddr address for dummy tail
  405. dummy_tail->size = 0;
  406. dummy_tail->caps = caps;
  407. TAILQ_INSERT_TAIL(&found_region->mem_block_head, dummy_tail, entries);
  408. }
  409. //Check if paddr is overlapped
  410. mem_block_t *mem_block = NULL;
  411. #if ENABLE_PADDR_CHECK
  412. bool is_enclosed = false;
  413. bool is_overlapped = false;
  414. bool allow_overlap = flags & ESP_MMU_MMAP_FLAG_PADDR_SHARED;
  415. TAILQ_FOREACH(mem_block, &found_region->mem_block_head, entries) {
  416. if (target == mem_block->target) {
  417. if ((s_is_enclosed(mem_block->paddr_start, mem_block->paddr_end, paddr_start, aligned_size))) {
  418. //the to-be-mapped paddr block is mapped already
  419. is_enclosed = true;
  420. break;
  421. }
  422. if (!allow_overlap && (s_is_overlapped(mem_block->paddr_start, mem_block->paddr_end, paddr_start, aligned_size))) {
  423. is_overlapped = true;
  424. break;
  425. }
  426. }
  427. }
  428. if (is_enclosed) {
  429. ESP_LOGW(TAG, "paddr block is mapped already, vaddr_start: %p, size: 0x%x", (void *)mem_block->vaddr_start, mem_block->size);
  430. *out_ptr = (void *)mem_block->vaddr_start;
  431. return ESP_ERR_INVALID_STATE;
  432. }
  433. if (!allow_overlap && is_overlapped) {
  434. ESP_LOGE(TAG, "paddr block is overlapped with an already mapped paddr block");
  435. return ESP_ERR_INVALID_ARG;
  436. }
  437. #endif //#if ENABLE_PADDR_CHECK
  438. new_block = (mem_block_t *)heap_caps_calloc(1, sizeof(mem_block_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  439. ESP_GOTO_ON_FALSE(new_block, ESP_ERR_NO_MEM, err, TAG, "no mem");
  440. //Reserve this block as it'll be mapped
  441. bool found = false;
  442. // Get the end address of the dummy_head block, which is always first block on the list
  443. uint32_t last_end = TAILQ_FIRST(&found_region->mem_block_head)->laddr_end;
  444. size_t slot_len = 0;
  445. size_t max_slot_len = 0;
  446. mem_block_t *found_block = NULL; //This stands for the block we found, whose slot between its prior block is where we will insert the new block to
  447. TAILQ_FOREACH(mem_block, &found_region->mem_block_head, entries) {
  448. slot_len = mem_block->laddr_start - last_end;
  449. if (!found) {
  450. if (slot_len >= aligned_size) {
  451. //Found it
  452. found = true;
  453. found_block = mem_block;
  454. slot_len -= aligned_size;
  455. new_block->laddr_start = last_end;
  456. }
  457. }
  458. max_slot_len = (slot_len > max_slot_len) ? slot_len : max_slot_len;
  459. last_end = mem_block->laddr_end;
  460. }
  461. assert(found);
  462. //insert the to-be-mapped new block to the list
  463. TAILQ_INSERT_BEFORE(found_block, new_block, entries);
  464. //Finally, we update the max_slot_size
  465. found_region->max_slot_size = max_slot_len;
  466. //Now we fill others according to the found `new_block->laddr_start`
  467. new_block->laddr_end = new_block->laddr_start + aligned_size;
  468. new_block->size = aligned_size;
  469. new_block->caps = caps;
  470. new_block->paddr_start = paddr_start;
  471. new_block->paddr_end = paddr_start + aligned_size;
  472. new_block->target = target;
  473. if (caps & MMU_MEM_CAP_EXEC) {
  474. new_block->vaddr_start = mmu_ll_laddr_to_vaddr(new_block->laddr_start, MMU_VADDR_INSTRUCTION, target);
  475. new_block->vaddr_end = mmu_ll_laddr_to_vaddr(new_block->laddr_end, MMU_VADDR_INSTRUCTION, target);
  476. } else {
  477. new_block->vaddr_start = mmu_ll_laddr_to_vaddr(new_block->laddr_start, MMU_VADDR_DATA, target);
  478. new_block->vaddr_end = mmu_ll_laddr_to_vaddr(new_block->laddr_end, MMU_VADDR_DATA, target);
  479. }
  480. //do mapping
  481. s_do_mapping(target, new_block->vaddr_start, paddr_start, aligned_size);
  482. *out_ptr = (void *)new_block->vaddr_start;
  483. return ESP_OK;
  484. err:
  485. if (dummy_tail) {
  486. free(dummy_tail);
  487. }
  488. if (dummy_head) {
  489. free(dummy_head);
  490. }
  491. return ret;
  492. }
  493. #if MMU_LL_MMU_PER_TARGET
  494. FORCE_INLINE_ATTR void s_unmapping_operation(uint32_t vaddr_start, uint32_t size)
  495. {
  496. uint32_t mmu_id = 0;
  497. mmu_target_t target = mmu_ll_vaddr_to_target(vaddr_start);
  498. if (target == MMU_TARGET_FLASH0) {
  499. mmu_id = MMU_LL_FLASH_MMU_ID;
  500. } else {
  501. mmu_id = MMU_LL_PSRAM_MMU_ID;
  502. }
  503. mmu_hal_unmap_region(mmu_id, vaddr_start, size);
  504. }
  505. #else
  506. FORCE_INLINE_ATTR void s_unmapping_operation(uint32_t vaddr_start, uint32_t size)
  507. {
  508. mmu_hal_unmap_region(0, vaddr_start, size);
  509. #if (SOC_MMU_PERIPH_NUM == 2)
  510. #if !CONFIG_FREERTOS_UNICORE
  511. mmu_hal_unmap_region(1, vaddr_start, size);
  512. #endif // #if !CONFIG_FREERTOS_UNICORE
  513. #endif // #if (SOC_MMU_PERIPH_NUM == 2)
  514. }
  515. #endif
  516. static void IRAM_ATTR NOINLINE_ATTR s_do_unmapping(uint32_t vaddr_start, uint32_t size)
  517. {
  518. /**
  519. * Disable Cache, after this function, involved code and data should be placed in internal RAM.
  520. *
  521. * @note we call this for now, but this will be refactored to move out of `spi_flash`
  522. */
  523. spi_flash_disable_interrupts_caches_and_other_cpu();
  524. s_unmapping_operation(vaddr_start, size);
  525. //enable Cache, after this function, internal RAM access is no longer mandatory
  526. spi_flash_enable_interrupts_caches_and_other_cpu();
  527. }
  528. esp_err_t esp_mmu_unmap(void *ptr)
  529. {
  530. ESP_RETURN_ON_FALSE(ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
  531. mem_region_t *region = NULL;
  532. mem_block_t *mem_block = NULL;
  533. uint32_t ptr_laddr = mmu_ll_vaddr_to_laddr((uint32_t)ptr);
  534. size_t slot_len = 0;
  535. for (int i = 0; i < s_mmu_ctx.num_regions; i++) {
  536. if (ptr_laddr >= s_mmu_ctx.mem_regions[i].free_head && ptr_laddr < s_mmu_ctx.mem_regions[i].end) {
  537. region = &s_mmu_ctx.mem_regions[i];
  538. }
  539. }
  540. ESP_RETURN_ON_FALSE(region, ESP_ERR_NOT_FOUND, TAG, "munmap target pointer is outside external memory regions");
  541. bool found = false;
  542. mem_block_t *found_block = NULL;
  543. TAILQ_FOREACH(mem_block, &region->mem_block_head, entries) {
  544. if (mem_block == TAILQ_FIRST(&region->mem_block_head) || mem_block == TAILQ_LAST(&region->mem_block_head, mem_block_head_)) {
  545. //we don't care the dummy_head and the dummy_tail
  546. continue;
  547. }
  548. //now we are only traversing the actual dynamically allocated blocks, dummy_head and dummy_tail are excluded already
  549. if (mem_block->laddr_start == ptr_laddr) {
  550. slot_len = TAILQ_NEXT(mem_block, entries)->laddr_start - TAILQ_PREV(mem_block, mem_block_head_, entries)->laddr_end;
  551. region->max_slot_size = (slot_len > region->max_slot_size) ? slot_len : region->max_slot_size;
  552. found = true;
  553. found_block = mem_block;
  554. break;
  555. }
  556. }
  557. ESP_RETURN_ON_FALSE(found, ESP_ERR_NOT_FOUND, TAG, "munmap target pointer isn't mapped yet");
  558. //do unmap
  559. s_do_unmapping(mem_block->vaddr_start, mem_block->size);
  560. //remove the already unmapped block from the list
  561. TAILQ_REMOVE(&region->mem_block_head, found_block, entries);
  562. free(found_block);
  563. return ESP_OK;
  564. }
  565. esp_err_t esp_mmu_map_dump_mapped_blocks(FILE* stream)
  566. {
  567. char line[100];
  568. for (int i = 0; i < s_mmu_ctx.num_regions; i++) {
  569. fprintf(stream, "region %d:\n", i);
  570. fprintf(stream, "%-15s %-14s %-14s %-12s %-12s %-12s\n", "Bus ID", "Start", "Free Head", "End", "Caps", "Max Slot Size");
  571. char *buf = line;
  572. size_t len = sizeof(line);
  573. memset(line, 0x0, len);
  574. snprintf(buf, len, "0x%-13x 0x%-12"PRIx32" 0x%-11"PRIx32" 0x%-10"PRIx32" 0x%-10x 0x%-8x\n",
  575. s_mmu_ctx.mem_regions[i].bus_id,
  576. s_mmu_ctx.mem_regions[i].start,
  577. s_mmu_ctx.mem_regions[i].free_head,
  578. s_mmu_ctx.mem_regions[i].end,
  579. s_mmu_ctx.mem_regions[i].caps,
  580. s_mmu_ctx.mem_regions[i].max_slot_size);
  581. fputs(line, stream);
  582. fprintf(stream, "mapped blocks:\n");
  583. fprintf(stream, "%-4s %-13s %-12s %-12s %-6s %-13s %-11s\n", "ID", "Vaddr Start", "Vaddr End", "Block Size", "Caps", "Paddr Start", "Paddr End");
  584. mem_region_t *region = &s_mmu_ctx.mem_regions[i];
  585. mem_block_t *mem_block = NULL;
  586. int id = 0;
  587. TAILQ_FOREACH(mem_block, &region->mem_block_head, entries) {
  588. if (mem_block != TAILQ_FIRST(&region->mem_block_head) && mem_block != TAILQ_LAST(&region->mem_block_head, mem_block_head_)) {
  589. snprintf(buf, len, "%-4d 0x%-11x 0x%-10x 0x%-10x 0x%-4x 0x%-11"PRIx32" 0x%-8"PRIx32"\n",
  590. id,
  591. mem_block->vaddr_start,
  592. mem_block->vaddr_end,
  593. mem_block->size,
  594. mem_block->caps,
  595. mem_block->paddr_start,
  596. mem_block->paddr_end);
  597. fputs(line, stream);
  598. id++;
  599. }
  600. }
  601. fprintf(stream, "\n");
  602. }
  603. return ESP_OK;
  604. }
  605. /*---------------------------------------------------------------
  606. Private dump functions, IRAM Safe
  607. ---------------------------------------------------------------*/
  608. esp_err_t IRAM_ATTR esp_mmu_map_dump_mapped_blocks_private(void)
  609. {
  610. for (int i = 0; i < s_mmu_ctx.num_regions; i++) {
  611. mem_region_t *region = &s_mmu_ctx.mem_regions[i];
  612. mem_block_t *mem_block = NULL;
  613. TAILQ_FOREACH(mem_block, &region->mem_block_head, entries) {
  614. if (mem_block != TAILQ_FIRST(&region->mem_block_head) && mem_block != TAILQ_LAST(&region->mem_block_head, mem_block_head_)) {
  615. ESP_DRAM_LOGI(TAG, "block vaddr_start: 0x%x", mem_block->vaddr_start);
  616. ESP_DRAM_LOGI(TAG, "block vaddr_end: 0x%x", mem_block->vaddr_end);
  617. ESP_DRAM_LOGI(TAG, "block size: 0x%x", mem_block->size);
  618. ESP_DRAM_LOGI(TAG, "block caps: 0x%x\n", mem_block->caps);
  619. ESP_DRAM_LOGI(TAG, "block paddr_start: 0x%x\n", mem_block->paddr_start);
  620. ESP_DRAM_LOGI(TAG, "block paddr_end: 0x%x\n", mem_block->paddr_end);
  621. }
  622. }
  623. ESP_DRAM_LOGI(TAG, "region bus_id: 0x%x", s_mmu_ctx.mem_regions[i].bus_id);
  624. ESP_DRAM_LOGI(TAG, "region start: 0x%x", s_mmu_ctx.mem_regions[i].start);
  625. ESP_DRAM_LOGI(TAG, "region end: 0x%x", s_mmu_ctx.mem_regions[i].end);
  626. ESP_DRAM_LOGI(TAG, "region caps: 0x%x\n", s_mmu_ctx.mem_regions[i].caps);
  627. }
  628. return ESP_OK;
  629. }
  630. /*---------------------------------------------------------------
  631. Helper APIs for conversion between vaddr and paddr
  632. ---------------------------------------------------------------*/
  633. static bool NOINLINE_ATTR IRAM_ATTR s_vaddr_to_paddr(uint32_t vaddr, esp_paddr_t *out_paddr, mmu_target_t *out_target)
  634. {
  635. //we call this for now, but this will be refactored to move out of `spi_flash`
  636. spi_flash_disable_interrupts_caches_and_other_cpu();
  637. //On ESP32, core 1 settings should be the same as the core 0
  638. bool is_mapped = mmu_hal_vaddr_to_paddr(0, vaddr, out_paddr, out_target);
  639. spi_flash_enable_interrupts_caches_and_other_cpu();
  640. return is_mapped;
  641. }
  642. esp_err_t esp_mmu_vaddr_to_paddr(void *vaddr, esp_paddr_t *out_paddr, mmu_target_t *out_target)
  643. {
  644. ESP_RETURN_ON_FALSE(vaddr && out_paddr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
  645. ESP_RETURN_ON_FALSE(mmu_hal_check_valid_ext_vaddr_region(0, (uint32_t)vaddr, 1, MMU_VADDR_DATA | MMU_VADDR_INSTRUCTION), ESP_ERR_INVALID_ARG, TAG, "not a valid external virtual address");
  646. esp_paddr_t paddr = 0;
  647. mmu_target_t target = 0;
  648. bool is_mapped = s_vaddr_to_paddr((uint32_t)vaddr, &paddr, &target);
  649. ESP_RETURN_ON_FALSE(is_mapped, ESP_ERR_NOT_FOUND, TAG, "vaddr isn't mapped");
  650. *out_paddr = paddr;
  651. *out_target = target;
  652. return ESP_OK;
  653. }
  654. static bool NOINLINE_ATTR IRAM_ATTR s_paddr_to_vaddr(esp_paddr_t paddr, mmu_target_t target, mmu_vaddr_t type, uint32_t *out_vaddr)
  655. {
  656. //we call this for now, but this will be refactored to move out of `spi_flash`
  657. spi_flash_disable_interrupts_caches_and_other_cpu();
  658. //On ESP32, core 1 settings should be the same as the core 0
  659. bool found = mmu_hal_paddr_to_vaddr(0, paddr, target, type, out_vaddr);
  660. spi_flash_enable_interrupts_caches_and_other_cpu();
  661. return found;
  662. }
  663. esp_err_t esp_mmu_paddr_to_vaddr(esp_paddr_t paddr, mmu_target_t target, mmu_vaddr_t type, void **out_vaddr)
  664. {
  665. ESP_RETURN_ON_FALSE(out_vaddr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
  666. uint32_t vaddr = 0;
  667. bool found = false;
  668. found = s_paddr_to_vaddr(paddr, target, type, &vaddr);
  669. ESP_RETURN_ON_FALSE(found, ESP_ERR_NOT_FOUND, TAG, "paddr isn't mapped");
  670. *out_vaddr = (void *)vaddr;
  671. return ESP_OK;
  672. }
  673. #if ENABLE_PADDR_CHECK
  674. /*---------------------------------------------------------------
  675. Helper functions to check block
  676. ---------------------------------------------------------------*/
  677. /**
  678. * Check if a new block is enclosed by another, e.g.
  679. *
  680. * This is enclosed:
  681. *
  682. * new_block_start new_block_end
  683. * |-------- New Block --------|
  684. * |--------------- Block ---------------|
  685. * block_start block_end
  686. *
  687. * @note Note the difference between `s_is_overlapped()` below
  688. *
  689. * @param block_start An original block start
  690. * @param block_end An original block end
  691. * @param new_block_start New block start
  692. * @param new_block_size New block size
  693. *
  694. * @return True: new block is enclosed; False: new block is not enclosed
  695. */
  696. static bool s_is_enclosed(uint32_t block_start, uint32_t block_end, uint32_t new_block_start, uint32_t new_block_size)
  697. {
  698. bool is_enclosed = false;
  699. uint32_t new_block_end = new_block_start + new_block_size;
  700. if ((new_block_start >= block_start) && (new_block_end <= block_end)) {
  701. is_enclosed = true;
  702. } else {
  703. is_enclosed = false;
  704. }
  705. return is_enclosed;
  706. }
  707. /**
  708. * Check if a new block is overlapped by another, e.g.
  709. *
  710. * This is overlapped:
  711. *
  712. * new_block_start new_block_end
  713. * |---------- New Block ----------|
  714. * |--------------- Block ---------------|
  715. * block_start block_end
  716. *
  717. * @note Note the difference between `s_is_enclosed()` above
  718. *
  719. * @param block_start An original block start
  720. * @param block_end An original block end
  721. * @param new_block_start New block start
  722. * @param new_block_size New block size
  723. *
  724. * @return True: new block is overlapped; False: new block is not overlapped
  725. */
  726. static bool s_is_overlapped(uint32_t block_start, uint32_t block_end, uint32_t new_block_start, uint32_t new_block_size)
  727. {
  728. bool is_overlapped = false;
  729. uint32_t new_block_end = new_block_start + new_block_size;
  730. if (((new_block_start < block_start) && (new_block_end > block_start)) ||
  731. ((new_block_start < block_end) && (new_block_end > block_end))) {
  732. is_overlapped = true;
  733. } else {
  734. is_overlapped = false;
  735. }
  736. return is_overlapped;
  737. }
  738. #endif //#if ENABLE_PADDR_CHECK