heap_alloc_caps.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <rom/ets_sys.h>
  14. #include <freertos/heap_regions.h>
  15. #include "esp_heap_alloc_caps.h"
  16. #include "spiram.h"
  17. #include "esp_log.h"
  18. #include <stdbool.h>
  19. static const char* TAG = "heap_alloc_caps";
  20. /*
  21. This file, combined with a region allocator that supports tags, solves the problem that the ESP32 has RAM that's
  22. slightly heterogeneous. Some RAM can be byte-accessed, some allows only 32-bit accesses, some can execute memory,
  23. some can be remapped by the MMU to only be accessed by a certain PID etc. In order to allow the most flexible
  24. memory allocation possible, this code makes it possible to request memory that has certain capabilities. The
  25. code will then use its knowledge of how the memory is configured along with a priority scheme to allocate that
  26. memory in the most sane way possible. This should optimize the amount of RAM accessible to the code without
  27. hardwiring addresses.
  28. */
  29. //Amount of priority slots for the tag descriptors.
  30. #define NO_PRIOS 3
  31. typedef struct {
  32. const char *name;
  33. uint32_t prio[NO_PRIOS];
  34. bool aliasedIram;
  35. } tag_desc_t;
  36. /*
  37. Tag descriptors. These describe the capabilities of a bit of memory that's tagged with the index into this table.
  38. Each tag contains NO_PRIOS entries; later entries are only taken if earlier ones can't fulfill the memory request.
  39. Make sure there are never more than HEAPREGIONS_MAX_TAGCOUNT (in heap_regions.h) tags (ex the last empty marker)
  40. */
  41. static const tag_desc_t tag_desc[]={
  42. { "DRAM", { MALLOC_CAP_DMA|MALLOC_CAP_8BIT, MALLOC_CAP_32BIT, 0 }, false}, //Tag 0: Plain ole D-port RAM
  43. { "D/IRAM", { 0, MALLOC_CAP_DMA|MALLOC_CAP_8BIT, MALLOC_CAP_32BIT|MALLOC_CAP_EXEC }, true}, //Tag 1: Plain ole D-port RAM which has an alias on the I-port
  44. { "IRAM", { MALLOC_CAP_EXEC|MALLOC_CAP_32BIT, 0, 0 }, false}, //Tag 2: IRAM
  45. { "PID2IRAM", { MALLOC_CAP_PID2, 0, MALLOC_CAP_EXEC|MALLOC_CAP_32BIT }, false}, //Tag 3-8: PID 2-7 IRAM
  46. { "PID3IRAM", { MALLOC_CAP_PID3, 0, MALLOC_CAP_EXEC|MALLOC_CAP_32BIT }, false}, //
  47. { "PID4IRAM", { MALLOC_CAP_PID4, 0, MALLOC_CAP_EXEC|MALLOC_CAP_32BIT }, false}, //
  48. { "PID5IRAM", { MALLOC_CAP_PID5, 0, MALLOC_CAP_EXEC|MALLOC_CAP_32BIT }, false}, //
  49. { "PID6IRAM", { MALLOC_CAP_PID6, 0, MALLOC_CAP_EXEC|MALLOC_CAP_32BIT }, false}, //
  50. { "PID7IRAM", { MALLOC_CAP_PID7, 0, MALLOC_CAP_EXEC|MALLOC_CAP_32BIT }, false}, //
  51. { "PID2DRAM", { MALLOC_CAP_PID2, MALLOC_CAP_8BIT, MALLOC_CAP_32BIT }, false}, //Tag 9-14: PID 2-7 DRAM
  52. { "PID3DRAM", { MALLOC_CAP_PID3, MALLOC_CAP_8BIT, MALLOC_CAP_32BIT }, false}, //
  53. { "PID4DRAM", { MALLOC_CAP_PID4, MALLOC_CAP_8BIT, MALLOC_CAP_32BIT }, false}, //
  54. { "PID5DRAM", { MALLOC_CAP_PID5, MALLOC_CAP_8BIT, MALLOC_CAP_32BIT }, false}, //
  55. { "PID6DRAM", { MALLOC_CAP_PID6, MALLOC_CAP_8BIT, MALLOC_CAP_32BIT }, false}, //
  56. { "PID7DRAM", { MALLOC_CAP_PID7, MALLOC_CAP_8BIT, MALLOC_CAP_32BIT }, false}, //
  57. { "SPISRAM", { MALLOC_CAP_SPISRAM, 0, MALLOC_CAP_DMA|MALLOC_CAP_8BIT|MALLOC_CAP_32BIT}, false}, //Tag 15: SPI SRAM data
  58. { "", { MALLOC_CAP_INVALID, MALLOC_CAP_INVALID, MALLOC_CAP_INVALID }, false} //End
  59. };
  60. /*
  61. Region descriptors. These describe all regions of memory available, and tag them according to the
  62. capabilities the hardware has. This array is not marked constant; the initialization code may want to
  63. change the tags of some regions because eg BT is detected, applications are loaded etc.
  64. The priorities here roughly work like this:
  65. - For a normal malloc (MALLOC_CAP_8BIT), give away the DRAM-only memory first, then pass off any dual-use IRAM regions,
  66. finally eat into the application memory.
  67. - For a malloc where 32-bit-aligned-only access is okay, first allocate IRAM, then DRAM, finally application IRAM.
  68. - Application mallocs (PIDx) will allocate IRAM first, if possible, then DRAM.
  69. - Most other malloc caps only fit in one region anyway.
  70. These region descriptors are very ESP32 specific, because they describe the memory pools available there.
  71. Because of requirements in the coalescing code as well as the heap allocator itself, this list should always
  72. be sorted from low to high start address.
  73. This array is *NOT* const because it gets modified depending on what pools are/aren't available.
  74. */
  75. static HeapRegionTagged_t regions[]={
  76. { (uint8_t *)0x3F800000, 0x20000, 15, 0}, //SPI SRAM, if available
  77. { (uint8_t *)0x3FFAE000, 0x2000, 0, 0}, //pool 16 <- used for rom code
  78. { (uint8_t *)0x3FFB0000, 0x8000, 0, 0}, //pool 15 <- can be used for BT
  79. { (uint8_t *)0x3FFB8000, 0x8000, 0, 0}, //pool 14 <- can be used for BT
  80. { (uint8_t *)0x3FFC0000, 0x2000, 0, 0}, //pool 10-13, mmu page 0
  81. { (uint8_t *)0x3FFC2000, 0x2000, 0, 0}, //pool 10-13, mmu page 1
  82. { (uint8_t *)0x3FFC4000, 0x2000, 0, 0}, //pool 10-13, mmu page 2
  83. { (uint8_t *)0x3FFC6000, 0x2000, 0, 0}, //pool 10-13, mmu page 3
  84. { (uint8_t *)0x3FFC8000, 0x2000, 0, 0}, //pool 10-13, mmu page 4
  85. { (uint8_t *)0x3FFCA000, 0x2000, 0, 0}, //pool 10-13, mmu page 5
  86. { (uint8_t *)0x3FFCC000, 0x2000, 0, 0}, //pool 10-13, mmu page 6
  87. { (uint8_t *)0x3FFCE000, 0x2000, 0, 0}, //pool 10-13, mmu page 7
  88. { (uint8_t *)0x3FFD0000, 0x2000, 0, 0}, //pool 10-13, mmu page 8
  89. { (uint8_t *)0x3FFD2000, 0x2000, 0, 0}, //pool 10-13, mmu page 9
  90. { (uint8_t *)0x3FFD4000, 0x2000, 0, 0}, //pool 10-13, mmu page 10
  91. { (uint8_t *)0x3FFD6000, 0x2000, 0, 0}, //pool 10-13, mmu page 11
  92. { (uint8_t *)0x3FFD8000, 0x2000, 0, 0}, //pool 10-13, mmu page 12
  93. { (uint8_t *)0x3FFDA000, 0x2000, 0, 0}, //pool 10-13, mmu page 13
  94. { (uint8_t *)0x3FFDC000, 0x2000, 0, 0}, //pool 10-13, mmu page 14
  95. { (uint8_t *)0x3FFDE000, 0x2000, 0, 0}, //pool 10-13, mmu page 15
  96. { (uint8_t *)0x3FFE0000, 0x4000, 1, 0x400BC000}, //pool 9 blk 1
  97. { (uint8_t *)0x3FFE4000, 0x4000, 1, 0x400B8000}, //pool 9 blk 0
  98. { (uint8_t *)0x3FFE8000, 0x8000, 1, 0x400B0000}, //pool 8 <- can be remapped to ROM, used for MAC dump
  99. { (uint8_t *)0x3FFF0000, 0x8000, 1, 0x400A8000}, //pool 7 <- can be used for MAC dump
  100. { (uint8_t *)0x3FFF8000, 0x4000, 1, 0x400A4000}, //pool 6 blk 1 <- can be used as trace memory
  101. { (uint8_t *)0x3FFFC000, 0x4000, 1, 0x400A0000}, //pool 6 blk 0 <- can be used as trace memory
  102. { (uint8_t *)0x40070000, 0x8000, 2, 0}, //pool 0
  103. { (uint8_t *)0x40078000, 0x8000, 2, 0}, //pool 1
  104. { (uint8_t *)0x40080000, 0x2000, 2, 0}, //pool 2-5, mmu page 0
  105. { (uint8_t *)0x40082000, 0x2000, 2, 0}, //pool 2-5, mmu page 1
  106. { (uint8_t *)0x40084000, 0x2000, 2, 0}, //pool 2-5, mmu page 2
  107. { (uint8_t *)0x40086000, 0x2000, 2, 0}, //pool 2-5, mmu page 3
  108. { (uint8_t *)0x40088000, 0x2000, 2, 0}, //pool 2-5, mmu page 4
  109. { (uint8_t *)0x4008A000, 0x2000, 2, 0}, //pool 2-5, mmu page 5
  110. { (uint8_t *)0x4008C000, 0x2000, 2, 0}, //pool 2-5, mmu page 6
  111. { (uint8_t *)0x4008E000, 0x2000, 2, 0}, //pool 2-5, mmu page 7
  112. { (uint8_t *)0x40090000, 0x2000, 2, 0}, //pool 2-5, mmu page 8
  113. { (uint8_t *)0x40092000, 0x2000, 2, 0}, //pool 2-5, mmu page 9
  114. { (uint8_t *)0x40094000, 0x2000, 2, 0}, //pool 2-5, mmu page 10
  115. { (uint8_t *)0x40096000, 0x2000, 2, 0}, //pool 2-5, mmu page 11
  116. { (uint8_t *)0x40098000, 0x2000, 2, 0}, //pool 2-5, mmu page 12
  117. { (uint8_t *)0x4009A000, 0x2000, 2, 0}, //pool 2-5, mmu page 13
  118. { (uint8_t *)0x4009C000, 0x2000, 2, 0}, //pool 2-5, mmu page 14
  119. { (uint8_t *)0x4009E000, 0x2000, 2, 0}, //pool 2-5, mmu page 15
  120. { NULL, 0, 0, 0} //end
  121. };
  122. //Modify regions array to disable the given range of memory.
  123. static void disable_mem_region(void *from, void *to) {
  124. int i;
  125. //Align from and to on word boundaries
  126. from=(void*)((uint32_t)from&~3);
  127. to=(void*)(((uint32_t)to+3)&~3);
  128. for (i=0; regions[i].xSizeInBytes!=0; i++) {
  129. void *regStart=regions[i].pucStartAddress;
  130. void *regEnd=regions[i].pucStartAddress+regions[i].xSizeInBytes;
  131. if (regStart>=from && regEnd<=to) {
  132. //Entire region falls in the range. Disable entirely.
  133. regions[i].xTag=-1;
  134. } else if (regStart>=from && regEnd>to && regStart<to) {
  135. //Start of the region falls in the range. Modify address/len.
  136. int overlap=(uint8_t *)to-(uint8_t *)regStart;
  137. regions[i].pucStartAddress+=overlap;
  138. regions[i].xSizeInBytes-=overlap;
  139. if (regions[i].xExecAddr) regions[i].xExecAddr+=overlap;
  140. } else if (regStart<from && regEnd>from && regEnd<=to) {
  141. //End of the region falls in the range. Modify length.
  142. regions[i].xSizeInBytes-=(uint8_t *)regEnd-(uint8_t *)from;
  143. } else if (regStart<from && regEnd>to) {
  144. //Range punches a hole in the region! We do not support this.
  145. ESP_EARLY_LOGE(TAG, "region %d: hole punching is not supported!", i);
  146. regions[i].xTag=-1; //Just disable memory region. That'll teach them!
  147. }
  148. }
  149. }
  150. /*
  151. Warning: These variables are assumed to have the start and end of the data and iram
  152. area used statically by the program, respectively. These variables are defined in the ld
  153. file.
  154. */
  155. extern int _bss_start, _heap_start, _init_start, _iram_text_end;
  156. /*
  157. Initialize the heap allocator. We pass it a bunch of region descriptors, but we need to modify those first to accommodate for
  158. the data as loaded by the bootloader.
  159. ToDo: The regions are different when stuff like trace memory, BT, ... is used. Modify the regions struct on the fly for this.
  160. Same with loading of apps. Same with using SPI RAM.
  161. */
  162. void heap_alloc_caps_init() {
  163. int i;
  164. //Compile-time assert to see if we don't have more tags than is set in heap_regions.h
  165. _Static_assert((sizeof(tag_desc)/sizeof(tag_desc[0]))-1 <= HEAPREGIONS_MAX_TAGCOUNT, "More than HEAPREGIONS_MAX_TAGCOUNT tags defined!");
  166. //Disable the bits of memory where this code is loaded.
  167. disable_mem_region(&_bss_start, &_heap_start); //DRAM used by bss/data static variables
  168. disable_mem_region(&_init_start, &_iram_text_end); //IRAM used by code
  169. disable_mem_region((void*)0x3ffae000, (void*)0x3ffb0000); //knock out ROM data region
  170. disable_mem_region((void*)0x40070000, (void*)0x40078000); //CPU0 cache region
  171. disable_mem_region((void*)0x40078000, (void*)0x40080000); //CPU1 cache region
  172. // TODO: this region should be checked, since we don't need to knock out all region finally
  173. disable_mem_region((void*)0x3ffe0000, (void*)0x3ffe8000); //knock out ROM data region
  174. #if CONFIG_MEMMAP_BT
  175. disable_mem_region((void*)0x3ffb0000, (void*)0x3ffc0000); //knock out BT data region
  176. #endif
  177. #if CONFIG_MEMMAP_TRACEMEM
  178. #if CONFIG_MEMMAP_TRACEMEM_TWOBANKS
  179. disable_mem_region((void*)0x3fff8000, (void*)0x40000000); //knock out trace mem region
  180. #else
  181. disable_mem_region((void*)0x3fff8000, (void*)0x3fffc000); //knock out trace mem region
  182. #endif
  183. #endif
  184. #if 0
  185. enable_spi_sram();
  186. #else
  187. disable_mem_region((void*)0x3f800000, (void*)0x3f820000); //SPI SRAM not installed
  188. #endif
  189. //The heap allocator will treat every region given to it as separate. In order to get bigger ranges of contiguous memory,
  190. //it's useful to coalesce adjacent regions that have the same tag.
  191. for (i=1; regions[i].xSizeInBytes!=0; i++) {
  192. if (regions[i].pucStartAddress == (regions[i-1].pucStartAddress + regions[i-1].xSizeInBytes) &&
  193. regions[i].xTag == regions[i-1].xTag ) {
  194. regions[i-1].xTag=-1;
  195. regions[i].pucStartAddress=regions[i-1].pucStartAddress;
  196. regions[i].xSizeInBytes+=regions[i-1].xSizeInBytes;
  197. }
  198. }
  199. ESP_EARLY_LOGI(TAG, "Initializing. RAM available for dynamic allocation:");
  200. for (i=0; regions[i].xSizeInBytes!=0; i++) {
  201. if (regions[i].xTag != -1) {
  202. ESP_EARLY_LOGI(TAG, "At %08X len %08X (%d KiB): %s",
  203. (int)regions[i].pucStartAddress, regions[i].xSizeInBytes, regions[i].xSizeInBytes/1024, tag_desc[regions[i].xTag].name);
  204. }
  205. }
  206. //Initialize the malloc implementation.
  207. vPortDefineHeapRegionsTagged( regions );
  208. }
  209. //First and last words of the D/IRAM region, for both the DRAM address as well as the IRAM alias.
  210. #define DIRAM_IRAM_START 0x400A0000
  211. #define DIRAM_IRAM_END 0x400BFFFC
  212. #define DIRAM_DRAM_START 0x3FFE0000
  213. #define DIRAM_DRAM_END 0x3FFFFFFC
  214. /*
  215. This takes a memory chunk in a region that can be addressed as both DRAM as well as IRAM. It will convert it to
  216. IRAM in such a way that it can be later freed. It assumes both the address as wel as the length to be word-aligned.
  217. It returns a region that's 1 word smaller than the region given because it stores the original Dram address there.
  218. In theory, we can also make this work by prepending a struct that looks similar to the block link struct used by the
  219. heap allocator itself, which will allow inspection tools relying on any block returned from any sort of malloc to
  220. have such a block in front of it, work. We may do this later, if/when there is demand for it. For now, a simple
  221. pointer is used.
  222. */
  223. static void *dram_alloc_to_iram_addr(void *addr, size_t len)
  224. {
  225. uint32_t dstart=(int)addr; //First word
  226. uint32_t dend=((int)addr)+len-4; //Last word
  227. configASSERT(dstart>=DIRAM_DRAM_START);
  228. configASSERT(dend<=DIRAM_DRAM_END);
  229. configASSERT((dstart&3)==0);
  230. configASSERT((dend&3)==0);
  231. uint32_t istart=DIRAM_IRAM_START+(DIRAM_DRAM_END-dend);
  232. uint32_t *iptr=(uint32_t*)istart;
  233. *iptr=dstart;
  234. return (void*)(iptr+1);
  235. }
  236. /*
  237. Standard malloc() implementation. Will return standard no-frills byte-accessible data memory.
  238. */
  239. void *pvPortMalloc( size_t xWantedSize )
  240. {
  241. return pvPortMallocCaps( xWantedSize, MALLOC_CAP_8BIT );
  242. }
  243. /*
  244. Standard free() implementation. Will pass memory on to the allocator unless it's an IRAM address where the
  245. actual meory is allocated in DRAM, it will convert to the DRAM address then.
  246. */
  247. void vPortFree( void *pv )
  248. {
  249. if (((int)pv>=DIRAM_IRAM_START) && ((int)pv<=DIRAM_IRAM_END)) {
  250. //Memory allocated here is actually allocated in the DRAM alias region and
  251. //cannot be de-allocated as usual. dram_alloc_to_iram_addr stores a pointer to
  252. //the equivalent DRAM address, though; free that.
  253. uint32_t* dramAddrPtr=(uint32_t*)pv;
  254. return vPortFreeTagged((void*)dramAddrPtr[-1]);
  255. }
  256. return vPortFreeTagged(pv);
  257. }
  258. /*
  259. Routine to allocate a bit of memory with certain capabilities. caps is a bitfield of MALLOC_CAP_* bits.
  260. */
  261. void *pvPortMallocCaps( size_t xWantedSize, uint32_t caps )
  262. {
  263. int prio;
  264. int tag, j;
  265. void *ret=NULL;
  266. uint32_t remCaps;
  267. if (caps & MALLOC_CAP_EXEC) {
  268. //MALLOC_CAP_EXEC forces an alloc from IRAM. There is a region which has both this
  269. //as well as the following caps, but the following caps are not possible for IRAM.
  270. //Thus, the combination is impossible and we return NULL directly, even although our tag_desc
  271. //table would indicate there is a tag for this.
  272. if ((caps & MALLOC_CAP_8BIT) || (caps & MALLOC_CAP_DMA)) {
  273. return NULL;
  274. }
  275. //If any, EXEC memory should be 32-bit aligned, so round up to the next multiple of 4.
  276. xWantedSize=(xWantedSize+3)&(~3);
  277. }
  278. for (prio=0; prio<NO_PRIOS; prio++) {
  279. //Iterate over tag descriptors for this priority
  280. for (tag=0; tag_desc[tag].prio[prio]!=MALLOC_CAP_INVALID; tag++) {
  281. if ((tag_desc[tag].prio[prio]&caps)!=0) {
  282. //Tag has at least one of the caps requested. If caps has other bits set that this prio
  283. //doesn't cover, see if they're available in other prios.
  284. remCaps=caps&(~tag_desc[tag].prio[prio]); //Remaining caps to be fulfilled
  285. j=prio+1;
  286. while (remCaps!=0 && j<NO_PRIOS) {
  287. remCaps=remCaps&(~tag_desc[tag].prio[j]);
  288. j++;
  289. }
  290. if (remCaps==0) {
  291. //This tag can satisfy all the requested capabilities. See if we can grab some memory using it.
  292. if ((caps & MALLOC_CAP_EXEC) && tag_desc[tag].aliasedIram) {
  293. //This is special, insofar that what we're going to get back is probably a DRAM address. If so,
  294. //we need to 'invert' it (lowest address in DRAM == highest address in IRAM and vice-versa) and
  295. //add a pointer to the DRAM equivalent before the address we're going to return.
  296. ret=pvPortMallocTagged(xWantedSize+4, tag);
  297. if (ret!=NULL) return dram_alloc_to_iram_addr(ret, xWantedSize+4);
  298. } else {
  299. //Just try to alloc, nothing special.
  300. ret=pvPortMallocTagged(xWantedSize, tag);
  301. if (ret!=NULL) return ret;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. //Nothing usable found.
  308. return NULL;
  309. }
  310. size_t xPortGetFreeHeapSizeCaps( uint32_t caps )
  311. {
  312. int prio;
  313. int tag;
  314. size_t ret=0;
  315. for (prio=0; prio<NO_PRIOS; prio++) {
  316. //Iterate over tag descriptors for this priority
  317. for (tag=0; tag_desc[tag].prio[prio]!=MALLOC_CAP_INVALID; tag++) {
  318. if ((tag_desc[tag].prio[prio]&caps)!=0) {
  319. ret+=xPortGetFreeHeapSizeTagged(tag);
  320. }
  321. }
  322. }
  323. return ret;
  324. }
  325. size_t xPortGetMinimumEverFreeHeapSizeCaps( uint32_t caps )
  326. {
  327. int prio;
  328. int tag;
  329. size_t ret=0;
  330. for (prio=0; prio<NO_PRIOS; prio++) {
  331. //Iterate over tag descriptors for this priority
  332. for (tag=0; tag_desc[tag].prio[prio]!=MALLOC_CAP_INVALID; tag++) {
  333. if ((tag_desc[tag].prio[prio]&caps)!=0) {
  334. ret+=xPortGetMinimumEverFreeHeapSizeTagged(tag);
  335. }
  336. }
  337. }
  338. return ret;
  339. }
  340. size_t xPortGetFreeHeapSize( void )
  341. {
  342. return xPortGetFreeHeapSizeCaps( MALLOC_CAP_8BIT );
  343. }
  344. size_t xPortGetMinimumEverFreeHeapSize( void )
  345. {
  346. return xPortGetMinimumEverFreeHeapSizeCaps( MALLOC_CAP_8BIT );
  347. }