From c925b0ecd547afaed51e7fd4b032da32a67bc066 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Fri, 10 Jan 2025 15:24:37 -0800 Subject: [PATCH] xtensa: mmu: fix incorrect caching attrs on double mapping Inside map_memory() with double mapping enabled, we should not be mapping the memory with the incoming attributes as-is since the incoming address may be on un-cached region but with caching attribute. So we need to sanitize the attributes according to the incoming address. Signed-off-by: Daniel Leung --- arch/xtensa/core/ptables.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/xtensa/core/ptables.c b/arch/xtensa/core/ptables.c index c02ecc64b0d..6f2a732d3cc 100644 --- a/arch/xtensa/core/ptables.c +++ b/arch/xtensa/core/ptables.c @@ -250,18 +250,25 @@ static void map_memory_range(const uint32_t start, const uint32_t end, static void map_memory(const uint32_t start, const uint32_t end, const uint32_t attrs) { - map_memory_range(start, end, attrs); - #ifdef CONFIG_XTENSA_MMU_DOUBLE_MAP + uint32_t uc_attrs = attrs & ~XTENSA_MMU_PTE_ATTR_CACHED_MASK; + uint32_t c_attrs = attrs | XTENSA_MMU_CACHED_WB; + if (sys_cache_is_ptr_uncached((void *)start)) { + map_memory_range(start, end, uc_attrs); + map_memory_range(POINTER_TO_UINT(sys_cache_cached_ptr_get((void *)start)), - POINTER_TO_UINT(sys_cache_cached_ptr_get((void *)end)), - attrs | XTENSA_MMU_CACHED_WB); + POINTER_TO_UINT(sys_cache_cached_ptr_get((void *)end)), c_attrs); } else if (sys_cache_is_ptr_cached((void *)start)) { + map_memory_range(start, end, c_attrs); + map_memory_range(POINTER_TO_UINT(sys_cache_uncached_ptr_get((void *)start)), - POINTER_TO_UINT(sys_cache_uncached_ptr_get((void *)end)), attrs); - } + POINTER_TO_UINT(sys_cache_uncached_ptr_get((void *)end)), uc_attrs); + } else #endif + { + map_memory_range(start, end, attrs); + } } static void xtensa_init_page_tables(void)