memory_layout.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include "esp_attr.h"
  9. #include "sdkconfig.h"
  10. #include "soc/soc.h"
  11. #include "heap_memory_layout.h"
  12. #include "esp_heap_caps.h"
  13. /**
  14. * @brief Memory type descriptors. These describe the capabilities of a type of memory in the SoC.
  15. * Each type of memory map consists of one or more regions in the address space.
  16. * Each type contains an array of prioritized capabilities.
  17. * Types with later entries are only taken if earlier ones can't fulfill the memory request.
  18. *
  19. * - For a normal malloc (MALLOC_CAP_DEFAULT), give away the DRAM-only memory first, then pass off any dual-use IRAM regions, finally eat into the application memory.
  20. * - For a malloc where 32-bit-aligned-only access is okay, first allocate IRAM, then DRAM, finally application IRAM.
  21. * - Application mallocs (PIDx) will allocate IRAM first, if possible, then DRAM.
  22. * - Most other malloc caps only fit in one region anyway.
  23. *
  24. */
  25. /* Index of memory in `soc_memory_types[]` */
  26. enum {
  27. SOC_MEMORY_TYPE_RAM = 0,
  28. SOC_MEMORY_TYPE_RETENTION_RAM = 1,
  29. SOC_MEMORY_TYPE_RTCRAM = 2,
  30. SOC_MEMORY_TYPE_NUM,
  31. };
  32. /* COMMON_CAPS is the set of attributes common to all types of memory on this chip */
  33. #ifdef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  34. #define ESP32C3_MEM_COMMON_CAPS (MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL | MALLOC_CAP_32BIT | MALLOC_CAP_8BIT)
  35. #else
  36. #define ESP32C3_MEM_COMMON_CAPS (MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL | MALLOC_CAP_32BIT | MALLOC_CAP_8BIT | MALLOC_CAP_EXEC)
  37. #endif
  38. /**
  39. * Defined the attributes and allocation priority of each memory on the chip,
  40. * The heap allocator will traverse all types of memory types in column High Priority Matching and match the specified caps at first,
  41. * if no memory caps matched or the allocation is failed, it will go to columns Medium Priorty Matching and Low Priority Matching
  42. * in turn to continue matching.
  43. */
  44. const soc_memory_type_desc_t soc_memory_types[SOC_MEMORY_TYPE_NUM] = {
  45. /* Mem Type Name | High Priority Matching | Medium Priorty Matching | Low Priority Matching */
  46. [SOC_MEMORY_TYPE_RAM] = { "RAM", { ESP32C3_MEM_COMMON_CAPS | MALLOC_CAP_DMA, 0 , 0}},
  47. [SOC_MEMORY_TYPE_RETENTION_RAM] = { "Retention RAM", { MALLOC_CAP_RETENTION, ESP32C3_MEM_COMMON_CAPS | MALLOC_CAP_DMA, 0}},
  48. [SOC_MEMORY_TYPE_RTCRAM] = { "RTCRAM", { MALLOC_CAP_RTCRAM, 0, ESP32C3_MEM_COMMON_CAPS }},
  49. };
  50. const size_t soc_memory_type_count = sizeof(soc_memory_types) / sizeof(soc_memory_type_desc_t);
  51. /**
  52. * @brief Region descriptors. These describe all regions of memory available, and map them to a type in the above type.
  53. *
  54. * @note Because of requirements in the coalescing code which merges adjacent regions,
  55. * this list should always be sorted from low to high by start address.
  56. *
  57. */
  58. /**
  59. * Register the shared buffer area of the last memory block into the heap during heap initialization
  60. */
  61. #define APP_USABLE_DRAM_END (SOC_ROM_STACK_START - SOC_ROM_STACK_SIZE)
  62. const soc_memory_region_t soc_memory_regions[] = {
  63. { 0x3FC80000, 0x20000, SOC_MEMORY_TYPE_RAM, 0x40380000, false}, //D/IRAM level1, can be used as trace memory
  64. { 0x3FCA0000, 0x20000, SOC_MEMORY_TYPE_RAM, 0x403A0000, false}, //D/IRAM level2, can be used as trace memory
  65. { 0x3FCC0000, (APP_USABLE_DRAM_END-0x3FCC0000), SOC_MEMORY_TYPE_RETENTION_RAM, 0x403C0000, false}, //D/IRAM level3, backup dma accessible, can be used as trace memory
  66. { APP_USABLE_DRAM_END, (SOC_DIRAM_DRAM_HIGH-APP_USABLE_DRAM_END), SOC_MEMORY_TYPE_RETENTION_RAM, MAP_DRAM_TO_IRAM(APP_USABLE_DRAM_END), true}, //D/IRAM level3, backup dma accessible, can be used as trace memory (ROM reserved area)
  67. #ifdef CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  68. { 0x50000000, 0x2000, SOC_MEMORY_TYPE_RTCRAM, 0, false}, //Fast RTC memory
  69. #endif
  70. };
  71. const size_t soc_memory_region_count = sizeof(soc_memory_regions) / sizeof(soc_memory_region_t);
  72. extern int _data_start, _heap_start, _iram_start, _iram_end, _rtc_force_slow_end;
  73. extern int _rtc_reserved_start, _rtc_reserved_end;
  74. /**
  75. * Reserved memory regions.
  76. * These are removed from the soc_memory_regions array when heaps are created.
  77. *
  78. */
  79. // Static data region. DRAM used by data+bss and possibly rodata
  80. SOC_RESERVE_MEMORY_REGION((intptr_t)&_data_start, (intptr_t)&_heap_start, dram_data);
  81. // Target has a big D/IRAM region, the part used by code is reserved
  82. // The address of the D/I bus are in the same order, directly shift IRAM address to get reserved DRAM address
  83. #define I_D_OFFSET (SOC_DIRAM_IRAM_LOW - SOC_DIRAM_DRAM_LOW)
  84. SOC_RESERVE_MEMORY_REGION((intptr_t)&_iram_start - I_D_OFFSET, (intptr_t)&_iram_end - I_D_OFFSET, iram_code);
  85. #ifdef CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  86. /* We use _rtc_force_slow_end not _rtc_noinit_end here, as rtc "fast" memory ends up in RTC SLOW
  87. region on C3, no differentiation. And _rtc_force_slow_end is the end of all the static RTC sections.
  88. */
  89. SOC_RESERVE_MEMORY_REGION(SOC_RTC_DRAM_LOW, (intptr_t)&_rtc_force_slow_end, rtcram_data);
  90. #endif
  91. SOC_RESERVE_MEMORY_REGION((intptr_t)&_rtc_reserved_start, (intptr_t)&_rtc_reserved_end, rtc_reserved_data);