8000 stm32/qspi: Use MPU to allow access to valid memory-mapped QSPI region. · micropython/micropython@8da39fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 8da39fd

Browse files
committed
stm32/qspi: Use MPU to allow access to valid memory-mapped QSPI region.
The Cortex-M7 CPU will do speculative loads from any memory location that is not explicitly forbidden. This includes the QSPI memory-mapped region starting at 0x90000000 and with size 256MiB. Speculative loads to this QSPI region may 1) interfere with the QSPI peripheral registers (eg the address register) if the QSPI is not in memory-mapped mode; 2) attempt to access data outside the configured size of the QSPI flash when it is in memory-mapped mode. Both of these scenarios will lead to issues with the QSPI peripheral (eg Cortex bus lock up in scenario 2). To prevent such speculative loads from interfering with the peripheral the MPU is configured in this commit to restrict access to the QSPI mapped region: when not memory mapped the entire region is forbidden; when memory mapped only accesses to the valid flash size are permitted.
1 parent eca4115 commit 8da39fd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

ports/stm32/mpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#if defined(STM32F7) || defined(STM32H7)
3030

3131
#define MPU_REGION_ETH (MPU_REGION_NUMBER0)
32+
#define MPU_REGION_QSPI1 (MPU_REGION_NUMBER1)
33+
#define MPU_REGION_QSPI2 (MPU_REGION_NUMBER2)
34+
#define MPU_REGION_QSPI3 (MPU_REGION_NUMBER3)
3235
#define MPU_REGION_SDRAM1 (MPU_REGION_NUMBER4)
3336
#define MPU_REGION_SDRAM2 (MPU_REGION_NUMBER5)
3437

ports/stm32/qspi.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828

2929
#include "py/mperrno.h"
3030
#include "py/mphal.h"
31+
#include "mpu.h"
3132
#include "qspi.h"
3233
#include "pin_static_af.h"
3334

3435
#if defined(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2)
3536

37+
#define QSPI_MAP_ADDR (0x90000000)
38+
3639
#ifndef MICROPY_HW_QSPI_PRESCALER
3740
#define MICROPY_HW_QSPI_PRESCALER 3 // F_CLK = F_AHB/3 (72MHz when CPU is 216MHz)
3841
#endif
@@ -49,7 +52,31 @@
4952
#define MICROPY_HW_QSPI_CS_HIGH_CYCLES 2 // nCS stays high for 2 cycles
5053
#endif
5154

55+
static inline void qspi_mpu_disable_all(void) {
56+
// Configure MPU to disable access to entire QSPI region, to prevent CPU
57+
// speculative execution from accessing this region and modifying QSPI registers.
58+
mpu_config_start();
59+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x00, MPU_REGION_SIZE_256MB));
60+
mpu_config_end();
61+
}
62+
63+
static inline void qspi_mpu_enable_mapped(void) {
64+
// Configure MPU to allow access to only the valid part of external SPI flash.
65+
// The memory accesses to the mapped QSPI are faster if the MPU is not used
66+
// for the memory-mapped region, so 3 MPU regions are used to disable access
67+
// to everything except the valid address space, using holes in the bottom
68+
// of the regions and nesting them.
69+
// At the moment this is hard-coded to 2MiB of QSPI address space.
70+
mpu_config_start();
71+
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_256MB));
72+
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x0f, MPU_REGION_SIZE_32MB));
73+
mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_DISABLE(0x01, MPU_REGION_SIZE_16MB));
74+
mpu_config_end();
75+
}
76+
5277
void qspi_init(void) {
78+
qspi_mpu_disable_all();
79+
5380
// Configure pins
5481
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_CS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_BK1_NCS);
5582
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_SCK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_CLK);
@@ -100,6 +127,8 @@ void qspi_memory_map(void) {
100127
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
101128
| 0xeb << QUADSPI_CCR_INSTRUCTION_Pos // quad read opcode
102129
;
130+
131+
qspi_mpu_enable_mapped();
103132
}
104133

105134
STATIC int qspi_ioctl(void *self_in, uint32_t cmd) {

0 commit comments

Comments
 (0)
0