himem.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "sdkconfig.h"
  8. #if !CONFIG_IDF_TARGET_ESP32
  9. #error esp_himem is only supported on ESP32
  10. #else
  11. #include <stddef.h>
  12. #include "esp_err.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. //Opaque pointers as handles for ram/range data
  17. typedef struct esp_himem_ramdata_t *esp_himem_handle_t;
  18. typedef struct esp_himem_rangedata_t *esp_himem_rangehandle_t;
  19. //ESP32 MMU block size
  20. #define ESP_HIMEM_BLKSZ (0x8000)
  21. #define ESP_HIMEM_MAPFLAG_RO 1 /*!< Indicates that a mapping will only be read from. Note that this is unused for now. */
  22. /**
  23. * @brief Allocate a block in high memory
  24. *
  25. * @param size Size of the to-be-allocated block, in bytes. Note that this needs to be
  26. * a multiple of the external RAM mmu block size (32K).
  27. * @param[out] handle_out Handle to be returned
  28. * @returns - ESP_OK if succesful
  29. * - ESP_ERR_NO_MEM if out of memory
  30. * - ESP_ERR_INVALID_SIZE if size is not a multiple of 32K
  31. */
  32. esp_err_t esp_himem_alloc(size_t size, esp_himem_handle_t *handle_out);
  33. /**
  34. * @brief Allocate a memory region to map blocks into
  35. *
  36. * This allocates a contiguous CPU memory region that can be used to map blocks
  37. * of physical memory into.
  38. *
  39. * @param size Size of the range to be allocated. Note this needs to be a multiple of
  40. * the external RAM mmu block size (32K).
  41. * @param[out] handle_out Handle to be returned
  42. * @returns - ESP_OK if succesful
  43. * - ESP_ERR_NO_MEM if out of memory or address space
  44. * - ESP_ERR_INVALID_SIZE if size is not a multiple of 32K
  45. */
  46. esp_err_t esp_himem_alloc_map_range(size_t size, esp_himem_rangehandle_t *handle_out);
  47. /**
  48. * @brief Map a block of high memory into the CPUs address space
  49. *
  50. * This effectively makes the block available for read/write operations.
  51. *
  52. * @note The region to be mapped needs to have offsets and sizes that are aligned to the
  53. * SPI RAM MMU block size (32K)
  54. *
  55. * @param handle Handle to the block of memory, as given by esp_himem_alloc
  56. * @param range Range handle to map the memory in
  57. * @param ram_offset Offset into the block of physical memory of the block to map
  58. * @param range_offset Offset into the address range where the block will be mapped
  59. * @param len Length of region to map
  60. * @param flags One of ESP_HIMEM_MAPFLAG_*
  61. * @param[out] out_ptr Pointer to variable to store resulting memory pointer in
  62. * @returns - ESP_OK if the memory could be mapped
  63. * - ESP_ERR_INVALID_ARG if offset, range or len aren't MMU-block-aligned (32K)
  64. * - ESP_ERR_INVALID_SIZE if the offsets/lengths don't fit in the allocated memory or range
  65. * - ESP_ERR_INVALID_STATE if a block in the selected ram offset/length is already mapped, or
  66. * if a block in the selected range offset/length already has a mapping.
  67. */
  68. esp_err_t esp_himem_map(esp_himem_handle_t handle, esp_himem_rangehandle_t range, size_t ram_offset, size_t range_offset, size_t len, int flags, void **out_ptr);
  69. /**
  70. * @brief Free a block of physical memory
  71. *
  72. * This clears out the associated handle making the memory available for re-allocation again.
  73. * This will only succeed if none of the memory blocks currently have a mapping.
  74. *
  75. * @param handle Handle to the block of memory, as given by esp_himem_alloc
  76. * @returns - ESP_OK if the memory is succesfully freed
  77. * - ESP_ERR_INVALID_ARG if the handle still is (partially) mapped
  78. */
  79. esp_err_t esp_himem_free(esp_himem_handle_t handle);
  80. /**
  81. * @brief Free a mapping range
  82. *
  83. * This clears out the associated handle making the range available for re-allocation again.
  84. * This will only succeed if none of the range blocks currently are used for a mapping.
  85. *
  86. * @param handle Handle to the range block, as given by esp_himem_alloc_map_range
  87. * @returns - ESP_OK if the memory is succesfully freed
  88. * - ESP_ERR_INVALID_ARG if the handle still is (partially) mapped to
  89. */
  90. esp_err_t esp_himem_free_map_range(esp_himem_rangehandle_t handle);
  91. /**
  92. * @brief Unmap a region
  93. *
  94. * @param range Range handle
  95. * @param ptr Pointer returned by esp_himem_map
  96. * @param len Length of the block to be unmapped. Must be aligned to the SPI RAM MMU blocksize (32K)
  97. * @returns - ESP_OK if the memory is succesfully unmapped,
  98. * - ESP_ERR_INVALID_ARG if ptr or len are invalid.
  99. */
  100. esp_err_t esp_himem_unmap(esp_himem_rangehandle_t range, void *ptr, size_t len);
  101. /**
  102. * @brief Get total amount of memory under control of himem API
  103. *
  104. * @returns Amount of memory, in bytes
  105. */
  106. size_t esp_himem_get_phys_size(void);
  107. /**
  108. * @brief Get free amount of memory under control of himem API
  109. *
  110. * @returns Amount of free memory, in bytes
  111. */
  112. size_t esp_himem_get_free_size(void);
  113. /**
  114. * @brief Get amount of SPI memory address space needed for bankswitching
  115. *
  116. * @note This is also weakly defined in esp32/spiram.c and returns 0 there, so
  117. * if no other function in this file is used, no memory is reserved.
  118. *
  119. * @returns Amount of reserved area, in bytes
  120. */
  121. size_t esp_himem_reserved_area_size(void);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif // !CONFIG_IDF_TARGET_ESP32