memory_layout.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef BOOTLOADER_BUILD
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include "esp_attr.h"
  10. #include "sdkconfig.h"
  11. #include "soc/soc.h"
  12. #include "soc/soc_memory_layout.h"
  13. #include "esp_heap_caps.h"
  14. /**
  15. * @brief Memory type descriptors. These describe the capabilities of a type of memory in the SoC.
  16. * Each type of memory map consists of one or more regions in the address space.
  17. * Each type contains an array of prioritized capabilities.
  18. * Types with later entries are only taken if earlier ones can't fulfill the memory request.
  19. *
  20. * - 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.
  21. * - For a malloc where 32-bit-aligned-only access is okay, first allocate IRAM, then DRAM, finally application IRAM.
  22. * - Application mallocs (PIDx) will allocate IRAM first, if possible, then DRAM.
  23. * - Most other malloc caps only fit in one region anyway.
  24. *
  25. */
  26. /* Index of memory in `soc_memory_types[]` */
  27. enum {
  28. SOC_MEMORY_TYPE_RAM = 0,
  29. SOC_MEMORY_TYPE_NUM,
  30. };
  31. const soc_memory_type_desc_t soc_memory_types[SOC_MEMORY_TYPE_NUM] = {
  32. // Type 0: DRAM which has an alias on the I-port
  33. [SOC_MEMORY_TYPE_RAM] = { "RAM", { MALLOC_CAP_DEFAULT | MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL | MALLOC_CAP_32BIT | MALLOC_CAP_EXEC, 0, 0 }},
  34. };
  35. const size_t soc_memory_type_count = sizeof(soc_memory_types) / sizeof(soc_memory_type_desc_t);
  36. /**
  37. * @brief Region descriptors. These describe all regions of memory available, and map them to a type in the above type.
  38. *
  39. * @note Because of requirements in the coalescing code which merges adjacent regions,
  40. * this list should always be sorted from low to high by start address.
  41. *
  42. */
  43. /**
  44. * Register the shared buffer area of the last memory block into the heap during heap initialization
  45. */
  46. #define APP_USABLE_DRAM_END (SOC_ROM_STACK_START - SOC_ROM_STACK_SIZE)
  47. const soc_memory_region_t soc_memory_regions[] = {
  48. { 0x3FCA0000, 0x10000, SOC_MEMORY_TYPE_RAM, 0x40380000, false}, //D/IRAM level1
  49. { 0x3FCB0000, 0x10000, SOC_MEMORY_TYPE_RAM, 0x40390000, false}, //D/IRAM level2
  50. { 0x3FCC0000, (APP_USABLE_DRAM_END-0x3FCC0000), SOC_MEMORY_TYPE_RAM, 0x403A0000, false}, //D/IRAM level3
  51. { APP_USABLE_DRAM_END, (SOC_DIRAM_DRAM_HIGH-APP_USABLE_DRAM_END), SOC_MEMORY_TYPE_RAM, MAP_DRAM_TO_IRAM(APP_USABLE_DRAM_END), true} //D/IRAM level3 (ROM reserved area)
  52. };
  53. const size_t soc_memory_region_count = sizeof(soc_memory_regions) / sizeof(soc_memory_region_t);
  54. extern int _data_start, _heap_start, _iram_start, _iram_end;
  55. /**
  56. * Reserved memory regions.
  57. * These are removed from the soc_memory_regions array when heaps are created.
  58. *
  59. */
  60. // Static data region. DRAM used by data+bss and possibly rodata
  61. SOC_RESERVE_MEMORY_REGION((intptr_t)&_data_start, (intptr_t)&_heap_start, dram_data);
  62. // Target has a big D/IRAM region, the part used by code is reserved
  63. // The address of the D/I bus are in the same order, directly shift IRAM address to get reserved DRAM address
  64. #define I_D_OFFSET (SOC_DIRAM_IRAM_LOW - SOC_DIRAM_DRAM_LOW)
  65. SOC_RESERVE_MEMORY_REGION((intptr_t)&_iram_start - I_D_OFFSET, (intptr_t)&_iram_end - I_D_OFFSET, iram_code);
  66. #endif // BOOTLOADER_BUILD