Skip to content

Commit c7e8b68

Browse files
committed
Merge branch 'fix/esp_mmu_vaddr_to_paddr_cannot_figure_psram_p4_v5.4' into 'release/v5.4'
mmu: vaddr to paddr cannot figure psram vaddr on esp32p4 (v5.4) See merge request espressif/esp-idf!37049
2 parents 315f0fc + 03e344d commit c7e8b68

File tree

12 files changed

+134
-18
lines changed

12 files changed

+134
-18
lines changed

components/esp_mm/esp_mmu_map.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -186,13 +186,6 @@ static void s_reserve_drom_region(mem_region_t *hw_mem_regions, int region_nums)
186186
}
187187
#endif //#if CONFIG_APP_BUILD_USE_FLASH_SECTIONS
188188

189-
#if SOC_MMU_PER_EXT_MEM_TARGET
190-
FORCE_INLINE_ATTR uint32_t s_get_mmu_id_from_target(mmu_target_t target)
191-
{
192-
return (target == MMU_TARGET_FLASH0) ? MMU_LL_FLASH_MMU_ID : MMU_LL_PSRAM_MMU_ID;
193-
}
194-
#endif
195-
196189
void esp_mmu_map_init(void)
197190
{
198191
mem_region_t hw_mem_regions[SOC_MMU_LINEAR_ADDRESS_REGION_NUM] = {};
@@ -393,7 +386,7 @@ static void IRAM_ATTR NOINLINE_ATTR s_do_cache_invalidate(uint32_t vaddr_start,
393386
FORCE_INLINE_ATTR uint32_t s_mapping_operation(mmu_target_t target, uint32_t vaddr_start, esp_paddr_t paddr_start, uint32_t size)
394387
{
395388
uint32_t actual_mapped_len = 0;
396-
uint32_t mmu_id = s_get_mmu_id_from_target(target);
389+
uint32_t mmu_id = mmu_hal_get_id_from_target(target);
397390
mmu_hal_map_region(mmu_id, target, vaddr_start, paddr_start, size, &actual_mapped_len);
398391

399392
return actual_mapped_len;
@@ -599,7 +592,7 @@ esp_err_t esp_mmu_map(esp_paddr_t paddr_start, size_t size, mmu_target_t target,
599592
FORCE_INLINE_ATTR void s_unmapping_operation(uint32_t vaddr_start, uint32_t size)
600593
{
601594
mmu_target_t target = mmu_ll_vaddr_to_target(vaddr_start);
602-
uint32_t mmu_id = s_get_mmu_id_from_target(target);
595+
uint32_t mmu_id = mmu_hal_get_id_from_target(target);
603596
mmu_hal_unmap_region(mmu_id, vaddr_start, size);
604597
}
605598
#else
@@ -753,10 +746,14 @@ esp_err_t IRAM_ATTR esp_mmu_map_dump_mapped_blocks_private(void)
753746
---------------------------------------------------------------*/
754747
static bool NOINLINE_ATTR IRAM_ATTR s_vaddr_to_paddr(uint32_t vaddr, esp_paddr_t *out_paddr, mmu_target_t *out_target)
755748
{
749+
uint32_t mmu_id = 0;
756750
//we call this for now, but this will be refactored to move out of `spi_flash`
757751
spi_flash_disable_interrupts_caches_and_other_cpu();
758-
bool is_mapped = mmu_hal_vaddr_to_paddr(0, vaddr, out_paddr, out_target);
759752
#if SOC_MMU_PER_EXT_MEM_TARGET
753+
mmu_id = mmu_hal_get_id_from_vaddr(vaddr);
754+
#endif
755+
bool is_mapped = mmu_hal_vaddr_to_paddr(mmu_id, vaddr, out_paddr, out_target);
756+
#if SPIRAM_FLASH_LOAD_TO_PSRAM
760757
if (!is_mapped) {
761758
is_mapped = mmu_hal_vaddr_to_paddr(1, vaddr, out_paddr, out_target);
762759
}
@@ -789,7 +786,7 @@ static bool NOINLINE_ATTR IRAM_ATTR s_paddr_to_vaddr(esp_paddr_t paddr, mmu_targ
789786
spi_flash_disable_interrupts_caches_and_other_cpu();
790787
uint32_t mmu_id = 0;
791788
#if SOC_MMU_PER_EXT_MEM_TARGET
792-
mmu_id = s_get_mmu_id_from_target(target);
789+
mmu_id = mmu_hal_get_id_from_target(target);
793790
#endif
794791
bool found = mmu_hal_paddr_to_vaddr(mmu_id, paddr, target, type, out_vaddr);
795792
spi_flash_enable_interrupts_caches_and_other_cpu();

components/esp_mm/test_apps/mm/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ set(COMPONENTS main esp_psram)
1010
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
1111
project(mm_test)
1212

13+
string(JOIN "," ignore_refs)
14+
15+
if(CONFIG_SOC_MMU_PER_EXT_MEM_TARGET AND CONFIG_SPIRAM_FLASH_LOAD_TO_PSRAM)
16+
# On SOC_MMU_PER_EXT_MEM_TARGET chips, when xip_psram, we need
17+
# - _instruction_reserved_start, _instruction_reserved_end
18+
# - _rodata_reserved_start, _rodata_reserved_end
19+
# to do some calculation. As we don't access the addresses, so we disable this check
20+
list(APPEND ignore_refs esp_mmu_map_init/*)
21+
endif()
22+
1323
if(CONFIG_COMPILER_DUMP_RTL_FILES)
1424
add_custom_target(check_test_app_sections ALL
1525
COMMAND ${PYTHON} $ENV{IDF_PATH}/tools/ci/check_callgraph.py
@@ -18,6 +28,7 @@ if(CONFIG_COMPILER_DUMP_RTL_FILES)
1828
find-refs
1929
--from-sections=.iram0.text
2030
--to-sections=.flash.text,.flash.rodata
31+
--ignore-refs=${ignore_refs}
2132
--exit-code
2233
DEPENDS ${elf}
2334
)

components/esp_mm/test_apps/mm/main/test_mmap.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -70,3 +70,26 @@ TEST_CASE("Can find paddr caps by any paddr offset", "[mmu]")
7070

7171
TEST_ESP_OK(esp_mmu_unmap(ptr0));
7272
}
73+
74+
#if CONFIG_SPIRAM
75+
#if !CONFIG_IDF_TARGET_ESP32 //ESP32 doesn't support using `esp_mmu_map` to map to PSRAM
76+
TEST_CASE("Can find paddr when mapping to psram", "[mmu]")
77+
{
78+
esp_paddr_t paddr = 0;
79+
mmu_target_t target = MMU_TARGET_FLASH0;
80+
81+
void *vaddr = NULL;
82+
esp_err_t err = ESP_FAIL;
83+
84+
vaddr = heap_caps_malloc(10, MALLOC_CAP_SPIRAM);
85+
err = esp_mmu_vaddr_to_paddr(vaddr, &paddr, &target);
86+
if (err == ESP_OK) {
87+
ESP_LOGI("MMU", "Virtual Address: %p, Physical Address: 0x%lx, Target: %d", vaddr, paddr, target);
88+
} else {
89+
ESP_LOGE("MMU", "Failed to convert virtual address to physical address: %s", esp_err_to_name(err));
90+
}
91+
92+
TEST_ASSERT(target == MMU_TARGET_PSRAM0);
93+
}
94+
#endif //#if !CONFIG_IDF_TARGET_ESP32
95+
#endif //#if CONFIG_SPIRAM

components/esp_mm/test_apps/mm/pytest_mmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: CC0-1.0
3-
43
import pytest
54
from pytest_embedded import Dut
65

@@ -24,6 +23,7 @@ def test_mmap(dut: Dut) -> None:
2423
pytest.param('psram_release_esp32', marks=[pytest.mark.esp32]),
2524
pytest.param('psram_release_esp32s2', marks=[pytest.mark.esp32s2]),
2625
pytest.param('psram_release_esp32s3', marks=[pytest.mark.esp32s3]),
26+
pytest.param('psram_release_esp32p4', marks=[pytest.mark.esp32p4]),
2727
]
2828

2929

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CONFIG_IDF_TARGET="esp32p4"
2+
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
3+
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
4+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
5+
6+
CONFIG_SPIRAM=y
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CONFIG_IDF_TARGET="esp32p4"
2+
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
3+
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
4+
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
5+
6+
CONFIG_SPIRAM=y
7+
CONFIG_SPIRAM_XIP_FROM_PSRAM=y

components/hal/esp32p4/include/hal/mmu_ll.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -84,6 +84,28 @@ static inline mmu_target_t mmu_ll_vaddr_to_target(uint32_t vaddr)
8484
return target;
8585
}
8686

87+
/**
88+
* Convert MMU virtual address to MMU ID
89+
*
90+
* @param vaddr virtual address
91+
*
92+
* @return MMU ID
93+
*/
94+
__attribute__((always_inline))
95+
static inline uint32_t mmu_ll_vaddr_to_id(uint32_t vaddr)
96+
{
97+
uint32_t id = 0;
98+
if (vaddr >= SOC_DRAM_FLASH_ADDRESS_LOW && vaddr < SOC_DRAM_FLASH_ADDRESS_HIGH) {
99+
id = MMU_LL_FLASH_MMU_ID;
100+
} else if (vaddr >= SOC_DRAM_PSRAM_ADDRESS_LOW && vaddr < SOC_DRAM_PSRAM_ADDRESS_HIGH) {
101+
id = MMU_LL_PSRAM_MMU_ID;
102+
} else {
103+
HAL_ASSERT(0);
104+
}
105+
106+
return id;
107+
}
108+
87109
__attribute__((always_inline)) static inline bool mmu_ll_cache_encryption_enabled(void)
88110
{
89111
unsigned cnt = efuse_ll_get_flash_crypt_cnt();

components/hal/include/hal/mmu_hal.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2010-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#pragma once
88

99
#include <esp_types.h>
10+
#include "soc/soc_caps.h"
1011
#include "hal/mmu_types.h"
1112

1213
#ifdef __cplusplus
@@ -118,6 +119,28 @@ bool mmu_hal_paddr_to_vaddr(uint32_t mmu_id, uint32_t paddr, mmu_target_t target
118119
*/
119120
bool mmu_hal_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start, uint32_t len, mmu_vaddr_t type);
120121

122+
#if SOC_MMU_PER_EXT_MEM_TARGET
123+
/**
124+
* Get MMU ID from MMU target
125+
*
126+
* @param target MMU target
127+
*
128+
* @return
129+
* MMU ID
130+
*/
131+
uint32_t mmu_hal_get_id_from_target(mmu_target_t target);
132+
133+
/**
134+
* Get MMU ID from vaddr
135+
*
136+
* @param vaddr Virtual address
137+
*
138+
* @return
139+
* MMU ID
140+
*/
141+
uint32_t mmu_hal_get_id_from_vaddr(uint32_t vaddr);
142+
#endif
143+
121144
#ifdef __cplusplus
122145
}
123146
#endif

components/hal/mmu_hal.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -161,3 +161,15 @@ bool mmu_hal_check_valid_ext_vaddr_region(uint32_t mmu_id, uint32_t vaddr_start,
161161
{
162162
return mmu_ll_check_valid_ext_vaddr_region(mmu_id, vaddr_start, len, type);
163163
}
164+
165+
#if SOC_MMU_PER_EXT_MEM_TARGET
166+
uint32_t mmu_hal_get_id_from_target(mmu_target_t target)
167+
{
168+
return (target == MMU_TARGET_FLASH0) ? MMU_LL_FLASH_MMU_ID : MMU_LL_PSRAM_MMU_ID;
169+
}
170+
171+
uint32_t mmu_hal_get_id_from_vaddr(uint32_t vaddr)
172+
{
173+
return mmu_ll_vaddr_to_id(vaddr);
174+
}
175+
#endif

components/spi_flash/test_apps/.build-test-rules.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ components/spi_flash/test_apps/flash_mmap:
3232
- spi_flash
3333
enable:
3434
- if: CONFIG_NAME in ["release", "rom_impl"] and IDF_TARGET != "linux"
35+
- if: CONFIG_NAME == "psram" and SOC_MMU_PER_EXT_MEM_TARGET == 1 # MMU per target needs test. On unified MMU chips, the entry ID is unique
3536
- if: CONFIG_NAME == "xip_psram" and IDF_TARGET in ["esp32s2", "esp32s3", "esp32p4"]
3637
# S2 doesn't have ROM for flash
3738
- if: CONFIG_NAME == "xip_psram_with_rom_impl" and IDF_TARGET in ["esp32s3", "esp32p4"]

components/spi_flash/test_apps/flash_mmap/pytest_flash_mmap.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: Apache-2.0
33
import pytest
44
from pytest_embedded import Dut
@@ -47,6 +47,19 @@ def test_flash_mmap_xip_psram(dut: Dut) -> None:
4747
dut.run_all_single_board_cases(timeout=30)
4848

4949

50+
@pytest.mark.supported_targets
51+
@pytest.mark.generic
52+
@pytest.mark.parametrize(
53+
'config',
54+
[
55+
'psram',
56+
],
57+
indirect=True,
58+
)
59+
def test_flash_mmap_psram(dut: Dut) -> None:
60+
dut.run_all_single_board_cases(timeout=30)
61+
62+
5063
@pytest.mark.supported_targets
5164
@pytest.mark.generic
5265
@pytest.mark.parametrize(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_SPIRAM=y

0 commit comments

Comments
 (0)