Skip to content

Commit cedaf81

Browse files
authored
Merge pull request #985 from dgarske/h7s_gfxmmu_mpu
Guard the STM32H7S GFXMMU window with an MPU region while the D-cache is on
2 parents 6155973 + 9d54377 commit cedaf81

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎hal/stm32h7s.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,35 @@ void RAMFUNCTION hal_cache_invalidate(void)
276276
ISB();
277277
}
278278

279+
/* Make the GFXMMU virtual-buffer window inaccessible while the D-cache is
280+
* on. The window is Normal memory in the default map, the core may read
281+
* Normal memory speculatively once the D-cache is enabled, and with the
282+
* GFXMMU clock off such a read stalls the bus for good: no fault, and the
283+
* debugger loses the core. A no-access region stops the read being issued. */
284+
static void mpu_guard_enable(void)
285+
{
286+
MPU_RNR = GFXMMU_MPU_REGION;
287+
MPU_RBAR = GFXMMU_VBUF_BASE;
288+
MPU_RASR = MPU_RASR_XN | MPU_RASR_SIZE_16MB | MPU_RASR_ENABLE;
289+
#ifdef WOLFBOOT_NO_MPU
290+
/* Nothing else has set the MPU up: keep the default map for the rest */
291+
MPU_CTRL = MPU_CTRL_PRIVDEFENA | MPU_CTRL_ENABLE;
292+
#endif
293+
DSB();
294+
ISB();
295+
}
296+
297+
static void mpu_guard_disable(void)
298+
{
299+
#ifdef WOLFBOOT_NO_MPU
300+
MPU_CTRL = 0;
301+
#endif
302+
MPU_RNR = GFXMMU_MPU_REGION;
303+
MPU_RASR = 0;
304+
DSB();
305+
ISB();
306+
}
307+
279308
static void dcache_enable(void)
280309
{
281310
uint32_t sets, ways;
@@ -884,6 +913,7 @@ void hal_init(void)
884913
icache_enable();
885914
octospi_gpio_init();
886915
octospi_init();
916+
mpu_guard_enable();
887917
dcache_enable();
888918
#ifdef DEBUG_UART
889919
uart_init();
@@ -905,6 +935,7 @@ void hal_prepare_boot(void)
905935
octospi_enable_mmap();
906936
dcache_disable();
907937
icache_disable();
938+
mpu_guard_disable();
908939
}
909940

910941
#endif /* !WOLFBOOT_UNIT_TEST_FLASH_ERASE */

‎hal/stm32h7s.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,22 @@
454454
#define CCSIDR (*(volatile uint32_t *)(0xE000ED80UL))
455455
#define CSSELR (*(volatile uint32_t *)(0xE000ED84UL))
456456

457+
#define MPU_CTRL (*(volatile uint32_t *)(0xE000ED94UL))
458+
#define MPU_RNR (*(volatile uint32_t *)(0xE000ED98UL))
459+
#define MPU_RBAR (*(volatile uint32_t *)(0xE000ED9CUL))
460+
#define MPU_RASR (*(volatile uint32_t *)(0xE000EDA0UL))
461+
#define MPU_CTRL_ENABLE (1 << 0)
462+
#define MPU_CTRL_PRIVDEFENA (1 << 2)
463+
#define MPU_RASR_ENABLE (1 << 0)
464+
#define MPU_RASR_SIZE_16MB (23 << 1)
465+
#define MPU_RASR_XN (1 << 28)
466+
467+
/* GFXMMU virtual buffers 0..3. A read here never completes while the
468+
* GFXMMU clock is off. */
469+
#define GFXMMU_VBUF_BASE (0x25000000UL)
470+
/* MPU region for the guard: one mpu_init() in src/boot_arm.c leaves free. */
471+
#define GFXMMU_MPU_REGION 3
472+
457473
#define CACHE_LINE_SIZE 32
458474

459475
#endif /* _STM32H7S_DEF_INCLUDED */

0 commit comments

Comments
 (0)