From f325ad22117964cff3c9476f422b339214248cfe Mon Sep 17 00:00:00 2001 From: Armando Date: Wed, 27 Jul 2022 10:57:49 +0800 Subject: [PATCH 1/2] mmu: fix wrong mmu end check In mmu code, we follow the rule that the `end` address shouldn't be touched. This commit fix wrong end address check in mmu_ll.h --- components/hal/esp32/include/hal/mmu_ll.h | 2 +- components/hal/esp32c2/include/hal/mmu_ll.h | 2 +- components/hal/esp32c3/include/hal/mmu_ll.h | 2 +- components/hal/esp32h2/include/hal/mmu_ll.h | 2 +- components/hal/esp32s3/include/hal/mmu_ll.h | 2 +- components/hal/mmu_hal.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/components/hal/esp32/include/hal/mmu_ll.h b/components/hal/esp32/include/hal/mmu_ll.h index e636fa8190..adb5cc62f2 100644 --- a/components/hal/esp32/include/hal/mmu_ll.h +++ b/components/hal/esp32/include/hal/mmu_ll.h @@ -60,7 +60,7 @@ __attribute__((always_inline)) static inline bool mmu_ll_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start, uint32_t len) { (void)mmu_id; - uint32_t vaddr_end = vaddr_start + len; + uint32_t vaddr_end = vaddr_start + len - 1; return (ADDRESS_IN_IRAM0_CACHE(vaddr_start) && ADDRESS_IN_IRAM0_CACHE(vaddr_end)) || (ADDRESS_IN_IRAM1_CACHE(vaddr_start) && ADDRESS_IN_IRAM1_CACHE(vaddr_end)) || diff --git a/components/hal/esp32c2/include/hal/mmu_ll.h b/components/hal/esp32c2/include/hal/mmu_ll.h index 8e34fea673..305f1fe445 100644 --- a/components/hal/esp32c2/include/hal/mmu_ll.h +++ b/components/hal/esp32c2/include/hal/mmu_ll.h @@ -68,7 +68,7 @@ __attribute__((always_inline)) static inline bool mmu_ll_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start, uint32_t len) { (void)mmu_id; - uint32_t vaddr_end = vaddr_start + len; + uint32_t vaddr_end = vaddr_start + len - 1; return (ADDRESS_IN_IRAM0_CACHE(vaddr_start, MMU_LL_PAGE_SIZE) && ADDRESS_IN_IRAM0_CACHE(vaddr_end, MMU_LL_PAGE_SIZE)) || (ADDRESS_IN_DRAM0_CACHE(vaddr_start, MMU_LL_PAGE_SIZE) && ADDRESS_IN_DRAM0_CACHE(vaddr_end, MMU_LL_PAGE_SIZE)); } diff --git a/components/hal/esp32c3/include/hal/mmu_ll.h b/components/hal/esp32c3/include/hal/mmu_ll.h index f9591d006f..a1e28d6937 100644 --- a/components/hal/esp32c3/include/hal/mmu_ll.h +++ b/components/hal/esp32c3/include/hal/mmu_ll.h @@ -60,7 +60,7 @@ __attribute__((always_inline)) static inline bool mmu_ll_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start, uint32_t len) { (void)mmu_id; - uint32_t vaddr_end = vaddr_start + len; + uint32_t vaddr_end = vaddr_start + len - 1; return (ADDRESS_IN_IRAM0_CACHE(vaddr_start) && ADDRESS_IN_IRAM0_CACHE(vaddr_end)) || (ADDRESS_IN_DRAM0_CACHE(vaddr_start) && ADDRESS_IN_DRAM0_CACHE(vaddr_end)); } diff --git a/components/hal/esp32h2/include/hal/mmu_ll.h b/components/hal/esp32h2/include/hal/mmu_ll.h index 650b685a59..05a9ace74d 100644 --- a/components/hal/esp32h2/include/hal/mmu_ll.h +++ b/components/hal/esp32h2/include/hal/mmu_ll.h @@ -60,7 +60,7 @@ __attribute__((always_inline)) static inline bool mmu_ll_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start, uint32_t len) { (void)mmu_id; - uint32_t vaddr_end = vaddr_start + len; + uint32_t vaddr_end = vaddr_start + len - 1; return (ADDRESS_IN_IRAM0_CACHE(vaddr_start) && ADDRESS_IN_IRAM0_CACHE(vaddr_end)) || (ADDRESS_IN_DRAM0_CACHE(vaddr_start) && ADDRESS_IN_DRAM0_CACHE(vaddr_end)); } diff --git a/components/hal/esp32s3/include/hal/mmu_ll.h b/components/hal/esp32s3/include/hal/mmu_ll.h index 968d62aaa1..fe2d38cd38 100644 --- a/components/hal/esp32s3/include/hal/mmu_ll.h +++ b/components/hal/esp32s3/include/hal/mmu_ll.h @@ -60,7 +60,7 @@ __attribute__((always_inline)) static inline bool mmu_ll_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start, uint32_t len) { (void)mmu_id; - uint32_t vaddr_end = vaddr_start + len; + uint32_t vaddr_end = vaddr_start + len - 1; return (ADDRESS_IN_IRAM0_CACHE(vaddr_start) && ADDRESS_IN_IRAM0_CACHE(vaddr_end)) || (ADDRESS_IN_DRAM0_CACHE(vaddr_start) && ADDRESS_IN_DRAM0_CACHE(vaddr_end)); } diff --git a/components/hal/mmu_hal.c b/components/hal/mmu_hal.c index 44f095521c..ad0e829bce 100644 --- a/components/hal/mmu_hal.c +++ b/components/hal/mmu_hal.c @@ -83,7 +83,7 @@ void mmu_hal_map_region(uint32_t mmu_id, mmu_target_t mem_type, uint32_t vaddr, uint32_t page_size_in_bytes = mmu_hal_pages_to_bytes(mmu_id, 1); HAL_ASSERT(vaddr % page_size_in_bytes == 0); HAL_ASSERT(paddr % page_size_in_bytes == 0); - HAL_ASSERT((paddr + len) <= mmu_hal_pages_to_bytes(mmu_id, MMU_MAX_PADDR_PAGE_NUM)); + HAL_ASSERT((paddr + len - 1) < mmu_hal_pages_to_bytes(mmu_id, MMU_MAX_PADDR_PAGE_NUM)); HAL_ASSERT(mmu_ll_check_valid_ext_vaddr_region(mmu_id, vaddr, len)); uint32_t page_num = (len + page_size_in_bytes - 1) / page_size_in_bytes; From 42dfb5cadde8d94fa06b4ce4cc4d3d73f4937859 Mon Sep 17 00:00:00 2001 From: Armando Date: Wed, 27 Jul 2022 10:59:47 +0800 Subject: [PATCH 2/2] esp_psram: improve mapping log when physical range is larger After this commit, when physical address is larger than vaddr range, driver will still map as much as it can, but also give a verbose level log to show the actual mapped size --- components/esp_psram/esp_psram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_psram/esp_psram.c b/components/esp_psram/esp_psram.c index df942c804a..e068edf58d 100644 --- a/components/esp_psram/esp_psram.c +++ b/components/esp_psram/esp_psram.c @@ -159,8 +159,8 @@ esp_err_t esp_psram_init(void) //----------------------------------Map the PSRAM physical range to MMU-----------------------------// intptr_t vaddr_start = mmu_get_psram_vaddr_start(); if (vaddr_start + psram_available_size > mmu_get_psram_vaddr_end()) { - ESP_EARLY_LOGV(TAG, "Virtual address not enough for PSRAM!"); psram_available_size = mmu_get_psram_vaddr_end() - vaddr_start; + ESP_EARLY_LOGV(TAG, "Virtual address not enough for PSRAM, map as much as we can. %dMB is mapped", psram_available_size / 1024 / 1024); } #if CONFIG_IDF_TARGET_ESP32