mmu_hal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "sdkconfig.h"
  10. #include "esp_err.h"
  11. #include "esp_attr.h"
  12. #include "hal/assert.h"
  13. #include "hal/mmu_hal.h"
  14. #include "hal/mmu_ll.h"
  15. #if CONFIG_IDF_TARGET_ESP32
  16. #include "esp32/rom/cache.h"
  17. #include "soc/dport_reg.h"
  18. #elif CONFIG_IDF_TARGET_ESP32S2
  19. #include "esp32s2/rom/cache.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S3
  21. #include "esp32s3/rom/cache.h"
  22. #elif CONFIG_IDF_TARGET_ESP32C3
  23. #include "esp32c3/rom/cache.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C2
  25. #include "esp32c2/rom/cache.h"
  26. #elif CONFIG_IDF_TARGET_ESP32H2
  27. #include "esp32h2/rom/cache.h"
  28. #endif
  29. void mmu_hal_init(void)
  30. {
  31. mmu_ll_unmap_all(0);
  32. #if !CONFIG_FREERTOS_UNICORE
  33. mmu_ll_unmap_all(1);
  34. #endif
  35. }
  36. #if !CONFIG_IDF_TARGET_ESP32
  37. //If decided, add a jira ticket for implementing these APIs on ESP32
  38. uint32_t mmu_hal_pages_to_bytes(uint32_t mmu_id, uint32_t page_num)
  39. {
  40. mmu_page_size_t page_size = mmu_ll_get_page_size(mmu_id);
  41. uint32_t shift_code = 0;
  42. switch (page_size) {
  43. case MMU_PAGE_64KB:
  44. shift_code = 16;
  45. break;
  46. case MMU_PAGE_32KB:
  47. shift_code = 15;
  48. break;
  49. case MMU_PAGE_16KB:
  50. shift_code = 14;
  51. break;
  52. default:
  53. HAL_ASSERT(shift_code);
  54. }
  55. return page_num << shift_code;
  56. }
  57. uint32_t mmu_hal_bytes_to_pages(uint32_t mmu_id, uint32_t bytes)
  58. {
  59. mmu_page_size_t page_size = mmu_ll_get_page_size(mmu_id);
  60. uint32_t shift_code = 0;
  61. switch (page_size) {
  62. case MMU_PAGE_64KB:
  63. shift_code = 16;
  64. break;
  65. case MMU_PAGE_32KB:
  66. shift_code = 15;
  67. break;
  68. case MMU_PAGE_16KB:
  69. shift_code = 14;
  70. break;
  71. default:
  72. HAL_ASSERT(shift_code);
  73. }
  74. return bytes >> shift_code;
  75. }
  76. void mmu_hal_map_region(uint32_t mmu_id, mmu_target_t mem_type, uint32_t vaddr, uint32_t paddr, uint32_t len, uint32_t *out_len)
  77. {
  78. uint32_t page_size_in_bytes = mmu_hal_pages_to_bytes(mmu_id, 1);
  79. HAL_ASSERT(vaddr % page_size_in_bytes == 0);
  80. HAL_ASSERT(paddr % page_size_in_bytes == 0);
  81. HAL_ASSERT((paddr + len - 1) < mmu_hal_pages_to_bytes(mmu_id, MMU_MAX_PADDR_PAGE_NUM));
  82. HAL_ASSERT(mmu_ll_check_valid_ext_vaddr_region(mmu_id, vaddr, len));
  83. uint32_t page_num = (len + page_size_in_bytes - 1) / page_size_in_bytes;
  84. uint32_t entry_id = 0;
  85. uint32_t mmu_val; //This is the physical address in the format that MMU supported
  86. *out_len = mmu_hal_pages_to_bytes(mmu_id, page_num);
  87. mmu_val = mmu_ll_format_paddr(mmu_id, paddr);
  88. while (page_num) {
  89. entry_id = mmu_ll_get_entry_id(mmu_id, vaddr);
  90. mmu_ll_write_entry(mmu_id, entry_id, mmu_val, mem_type);
  91. vaddr += page_size_in_bytes;
  92. mmu_val++;
  93. page_num--;
  94. }
  95. }
  96. #endif //#if !CONFIG_IDF_TARGET_ESP32