From be49df628f41344eed3a6acfbea228b64fcfead0 Mon Sep 17 00:00:00 2001 From: Aurelien Jarno Date: Thu, 13 May 2021 15:34:00 +0200 Subject: [PATCH] arch: arm: cortex_m: z_arm_mpu_init: fix D-Cache invalidation In case CONFIG_NOCACHE_MEMORY=y, the D-Cache need to be clean and invalidated before enabling the MPU to make sure no data from a __nocache__ region is present in the D-Cache. If the D-Cache is disabled, SCB_CleanInvalidateDCache() shall not be used as it might contains random data for random addresses, and this might just create a bus fault. Signed-off-by: Aurelien Jarno --- arch/arm/core/aarch32/mpu/arm_mpu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/arm/core/aarch32/mpu/arm_mpu.c b/arch/arm/core/aarch32/mpu/arm_mpu.c index dbbda551190..bce17b47322 100644 --- a/arch/arm/core/aarch32/mpu/arm_mpu.c +++ b/arch/arm/core/aarch32/mpu/arm_mpu.c @@ -326,11 +326,13 @@ int z_arm_mpu_init(void) arm_core_mpu_disable(); #if defined(CONFIG_NOCACHE_MEMORY) - /* Clean and invalidate data cache if + /* Clean and invalidate data cache if it is enabled and * that was not already done at boot */ #if !defined(CONFIG_INIT_ARCH_HW_AT_BOOT) - SCB_CleanInvalidateDCache(); + if (SCB->CCR & SCB_CCR_DC_Msk) { + SCB_CleanInvalidateDCache(); + } #endif #endif /* CONFIG_NOCACHE_MEMORY */