mm.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. Memory Management for MMU Supported Memory
  2. ******************************************
  3. .. toctree::
  4. :maxdepth: 1
  5. Introduction
  6. ============
  7. {IDF_TARGET_NAME} Memory Management Unit (MMU) is relatively simple. It can do memory address translation between physical memory addresses and virtual memory addresses. So CPU can access physical memories via virtual addresses. There are multiple types of virtual memory addresses, which have different capabilities.
  8. ESP-IDF provides a memory mapping driver that manages the relation between these physical memory addresses and virtual memory addresses, so as to achieve some features such as reading from SPI Flash via a pointer.
  9. Memory mapping driver is actually a capabilities-based virtual memory address allocator that allows apps to make virtual memory address allocations for different purposes. In the following chapters, we call this driver `esp_mmap` driver.
  10. ESP-IDF also provides a memory synchronisation driver which can be used for potential memory desychronisation scenarios.
  11. Physical Memory Types
  12. =====================
  13. Memory mapping driver currently supports mapping to following physical memory types:
  14. .. list::
  15. - SPI Flash
  16. :SOC_SPIRAM_SUPPORTED and not esp32: - PSRAM
  17. Virtual Memory Capabilities
  18. ===========================
  19. .. list::
  20. - :cpp:enumerator:`MMU_MEM_CAP_EXEC`. This capability indicates that the virtual memory address has the execute permission. Note this permission scope is within the MMU hardware.
  21. - :cpp:enumerator:`MMU_MEM_CAP_READ`. This capability indicates that the virtual memory address has the read permission. Note this permission scope is within the MMU hardware.
  22. - :cpp:enumerator:`MMU_MEM_CAP_WRITE`. This capability indicates that the virtual memory address has the write permission. Note this permission scope is within the MMU hardware.
  23. - :cpp:enumerator:`MMU_MEM_CAP_32BIT`. This capability indicates that the virtual memory address allows for 32 bits or multiples of 32 bits access.
  24. - :cpp:enumerator:`MMU_MEM_CAP_8BIT`. This capability indicates that the virtual memory address allows for 8 bits or multiples of 8 bits access.
  25. .. only:: esp32
  26. 8 MB external memory addresses (from 0x40400000 to 0x40C00000) which have the :cpp:enumerator:`MMU_MEM_CAP_EXEC` and :cpp:enumerator:`MMU_MEM_CAP_READ` capabilities are not avaiable for users to allocate, due to hardware limitations.
  27. .. only:: esp32s2
  28. 4 MB external memory addresses (from 0x40400000 to 0x40800000) which have the :cpp:enumerator:`MMU_MEM_CAP_EXEC` and :cpp:enumerator:`MMU_MEM_CAP_READ` capabilities are not avaiable for users to allocate, due to hardware limitations.
  29. You can call :cpp:func:`esp_mmu_map_get_max_consecutive_free_block_size` to know the largest consecutive mappable block size with certain capabilities.
  30. Memory Management Drivers
  31. =========================
  32. Driver Concept
  33. --------------
  34. Terminology
  35. ^^^^^^^^^^^
  36. The virtual memory pool is made up with one or multiple virtual memory regions, see below figure:
  37. .. image:: /../_static/diagrams/mmu/mem_pool.png
  38. :scale: 100 %
  39. :align: center
  40. - A virtual memory pool stands for the whole virtual address range that can be mapped to physical memory
  41. - A virtual memory region is a range of virtual address with same attributes
  42. - A virtual memory block is a piece of virtual address range that is dynamically mapped.
  43. - A slot is the virtual address range between two virtual memory blocks.
  44. - A physical memory block is a piece of physical address range that is to-be-mapped or already mapped to a virtual memory block.
  45. - Dynamical mapping is done by calling `esp_mmap` driver API :cpp:func:`esp_mmu_map`, this API will map the given physical memory block to a virtual memory block which is allocated by the `esp_mmap` driver.
  46. Relation between Memory Blocks
  47. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  48. When mapping a physical memory block A, block A can have one of the following relations with another previously mapped physical memory block B:
  49. - Enclosed: block A is completely enclosed within block B, see figure below:
  50. .. image:: /../_static/diagrams/mmu/enclosed.png
  51. - Identical: block A is completely the same as block B, see figure below:
  52. .. image:: /../_static/diagrams/mmu/identical.png
  53. Note `esp_mmap` driver will consider the identical scenario **the same as the enclosed scenario**.
  54. - Overlapped: block A is overlapped with block B, see figure below:
  55. .. image:: /../_static/diagrams/mmu/overlapped.png
  56. There is a special condition, when block A entirely encloses block B, see figure below:
  57. .. image:: /../_static/diagrams/mmu/inversed_enclosed.png
  58. `esp_mmap` driver will consider this scenario **the same as the overlapped scenario**.
  59. Driver Behaviour
  60. ----------------
  61. Memory Map
  62. ^^^^^^^^^^
  63. You can call :cpp:func:`esp_mmu_map` to do a dynamical mapping. This API will allocate a certain size of virtual memory block according to the virtual memory capabilities you selected, then map this virtual memory block to the physical memory block as you requested. The `esp_mmap` driver supports mapping to one or more types of physical memory, so you should specify the physical memory target when mapping.
  64. By default, physical memory blocks and virtual memory blocks are one-to-one mapped. This means, when calling :cpp:func:`esp_mmu_map`:
  65. * If it's the enclosed scenario, this API will return an :c:macro:`ESP_ERR_INVALID_STATE`. The `out_ptr` will be assigned to the start virtual memory address of the previously mapped one which encloses the to-be-mapped one.
  66. * If it's the identical scenario, this API will behaves exactly the same as the enclosed scenario.
  67. * If it's the overlapped scenario, this API will by default return an :c:macro:`ESP_ERR_INVALID_ARG`. This means, `esp_mmap` driver by default doesn't allow mapping a physical memory address to multiple virtual memory addresses.
  68. Specially, you can use :c:macro:`ESP_MMU_MMAP_FLAG_PADDR_SHARED`. This flags stands for one-to-multiple mapping between a physical address and multiple virtual addresses:
  69. * If it's the overlapped scenario, this API will allocate a new virtual memory block as requested, then map to the given physical memory block.
  70. Memory Unmap
  71. ^^^^^^^^^^^^
  72. You can call :cpp:func:`esp_mmu_unmap` to unmap a previously mapped memory block. This API will return an :c:macro:`ESP_ERR_NOT_FOUND` if you are trying to unmap a virtual memory block that isn't mapped to any physical memory block yet.
  73. Memory Address Conversion
  74. ^^^^^^^^^^^^^^^^^^^^^^^^^
  75. The `esp_mmap` driver provides two helper APIs to do the conversion between virtual memory address and physical memory address.
  76. * :cpp:func:`esp_mmu_vaddr_to_paddr`, convert virtual address to physical address.
  77. * :cpp:func:`esp_mmu_paddr_to_vaddr`, convert physical address to virtual address.
  78. Memory Synchronisation
  79. ^^^^^^^^^^^^^^^^^^^^^^
  80. MMU supported physical memories can be accessed by one or multiple methods.
  81. SPI Flash can be accessed by SPI1 (ESP-IDF `esp_flash` driver APIs), or by pointers. ESP-IDF `esp_flash` driver APIs have already considered the memory synchronisation, so users don't need to worry about this.
  82. .. only:: SOC_SPIRAM_SUPPORTED
  83. PSRAM can be accessed by pointers, hardware guarantees the data consistency when PSRAM is only accessed via pointers.
  84. .. only:: esp32s3
  85. PSRAM can also be accessed by EDMA. Data desynchronisation may happen because hardware does not guarantee the data consistency under such condition. You should call :cpp:func:`esp_cache_msync` to synchronise the Cache and the PSRAM.
  86. Thread Safety
  87. =============
  88. APIs in `esp_mmu_map.h` are not guaranteed to be thread-safe.
  89. APIs in `esp_cache.h` are guaranteed to be thread-safe.
  90. API Reference
  91. =============
  92. API Reference - ESP MMAP Driver
  93. -------------------------------
  94. .. include-build-file:: inc/esp_mmu_map.inc
  95. API Reference - ESP MSYNC Driver
  96. --------------------------------
  97. .. include-build-file:: inc/esp_cache.inc