Skip to content

Commit 0233033

Browse files
committed
Fix target.mpu-rom-end setting, for ARMv8-M too
targets.json was not specifying the same macro name as the code was checking for, so setting was ineffective. Making this work tripped up not-supported checks in ARMv8-M - rather than deal with making this work, support it instead. Both ARMv7-M and ARMv8-M slightly reduce code size and runtime impact if mpu-rom-end is 0x1fffffff, using one fewer region. This means default setup for ARMv8-M now requires 5 regions, with mpu-rom-end set to default 0x0fffffff, but this can be put back to 4 by changing the setting.
1 parent 170b734 commit 0233033

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

hal/mpu/mbed_mpu_v7m.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
#error "Device has v7m MPU but it is not enabled. Add 'MPU' to device_has in targets.json"
2626
#endif
2727

28-
#if !defined(MBED_MPU_ROM_END)
28+
#ifdef MBED_CONF_TARGET_MPU_ROM_END
29+
#define MBED_MPU_ROM_END MBED_CONF_TARGET_MPU_ROM_END
30+
#else
2931
#define MBED_MPU_ROM_END (0x10000000 - 1)
3032
#endif
3133
#define MBED_MPU_RAM_START (MBED_MPU_ROM_END + 1)
@@ -48,9 +50,13 @@ void mbed_mpu_init()
4850

4951
const uint32_t regions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
5052

51-
// Our MPU setup requires 4 regions - if this assert is hit, remove
52-
// DEVICE_MPU from device_has
53+
// Our MPU setup requires 3 or 4 regions - if this assert is hit, remove
54+
// a region by setting MPU_ROM_END to 0x1fffffff, or remove MPU from device_has
55+
#if MBED_MPU_RAM_START == 0x20000000
56+
MBED_ASSERT(regions >= 3);
57+
#else
5358
MBED_ASSERT(regions >= 4);
59+
#endif
5460

5561
// Disable the MCU
5662
MPU->CTRL = 0;
@@ -99,11 +105,12 @@ void mbed_mpu_init()
99105
ARM_MPU_REGION_SIZE_512MB) // Size
100106
);
101107

102-
// Select region 1 and use it for a WT ram region in the Code area
108+
#if MBED_MPU_RAM_START < 0x20000000
109+
// Select region 3 and use it for a WT ram region in the Code area
103110
// - Code MBED_MPU_ROM_END + 1 to 0x1FFFFFFF
104111
ARM_MPU_SetRegion(
105112
ARM_MPU_RBAR(
106-
1, // Region
113+
3, // Region
107114
0x00000000), // Base
108115
ARM_MPU_RASR(
109116
1, // DisableExec
@@ -123,13 +130,17 @@ void mbed_mpu_init()
123130
((MBED_MPU_RAM_START <= 0x20000000) ? 0 : (1 << 7)),
124131
ARM_MPU_REGION_SIZE_512MB) // Size
125132
);
133+
#define LAST_RAM_REGION 3
134+
#else
135+
#define LAST_RAM_REGION 2
136+
#endif
126137

127-
// Select region 2 and use it for WBWA ram regions
138+
// Select region 1 and use it for WBWA ram regions
128139
// - SRAM 0x20000000 to 0x3FFFFFFF
129140
// - RAM 0x60000000 to 0x7FFFFFFF
130141
ARM_MPU_SetRegion(
131142
ARM_MPU_RBAR(
132-
2, // Region
143+
1, // Region
133144
0x00000000), // Base
134145
ARM_MPU_RASR(
135146
1, // DisableExec
@@ -150,11 +161,11 @@ void mbed_mpu_init()
150161
ARM_MPU_REGION_SIZE_4GB) // Size
151162
);
152163

153-
// Select region 3 and use it for the WT ram region
164+
// Select region 2 and use it for the WT ram region
154165
// - RAM 0x80000000 to 0x9FFFFFFF
155166
ARM_MPU_SetRegion(
156167
ARM_MPU_RBAR(
157-
3, // Region
168+
2, // Region
158169
0x80000000), // Base
159170
ARM_MPU_RASR(
160171
1, // DisableExec
@@ -214,7 +225,7 @@ void mbed_mpu_enable_ram_xn(bool enable)
214225
// Flush memory writes before configuring the MPU.
215226
__DMB();
216227

217-
for (uint32_t region = 1; region <= 3; region++) {
228+
for (uint32_t region = 1; region <= LAST_RAM_REGION; region++) {
218229
enable_region(enable, region);
219230
}
220231

hal/mpu/mbed_mpu_v8m.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
#error "Device has v8m MPU but it is not enabled. Add 'MPU' to device_has in targets.json"
2626
#endif
2727

28-
#if !defined(MBED_MPU_ROM_END)
29-
#define MBED_MPU_ROM_END (0x20000000 - 1)
28+
#ifdef MBED_CONF_TARGET_MPU_ROM_END
29+
#define MBED_MPU_ROM_END MBED_CONF_TARGET_MPU_ROM_END
30+
#else
31+
#define MBED_MPU_ROM_END (0x10000000 - 1)
3032
#endif
33+
#define MBED_MPU_RAM_START (MBED_MPU_ROM_END + 1)
3134

32-
MBED_STATIC_ASSERT(MBED_MPU_ROM_END == 0x1fffffff, "Changing MBED_MPU_ROM_END for ARMv8-M is not supported.");
35+
MBED_STATIC_ASSERT(MBED_MPU_ROM_END <= 0x20000000 - 1,
36+
"Unsupported value for MBED_MPU_ROM_END");
3337

3438
void mbed_mpu_init()
3539
{
@@ -38,9 +42,13 @@ void mbed_mpu_init()
3842

3943
const uint32_t regions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
4044

41-
// Our MPU setup requires 4 regions - if this assert is hit, remove
42-
// DEVICE_MPU from device_has
45+
// Our MPU setup requires 4 or 5 regions - if this assert is hit, remove
46+
// a region by setting MPU_ROM_END to 0x1fffffff, or remove MPU from device_has
47+
#if MBED_MPU_RAM_START == 0x20000000
4348
MBED_ASSERT(regions >= 4);
49+
#else
50+
MBED_ASSERT(regions >= 5);
51+
#endif
4452

4553
// Disable the MCU
4654
MPU->CTRL = 0;
@@ -54,7 +62,7 @@ void mbed_mpu_init()
5462
* ARMv8-M memory map:
5563
*
5664
* Start End Name Executable by default Default cache Mbed MPU protection
57-
* 0x00000000 - 0x1FFFFFFF Code Yes WT, WA Write disabled
65+
* 0x00000000 - 0x1FFFFFFF Code Yes WT, WA Write disabled for first portion and execute disabled for the rest
5866
* 0x20000000 - 0x3FFFFFFF SRAM Yes WB, WA, RA Execute disabled
5967
* 0x40000000 - 0x5FFFFFFF Peripheral No
6068
* 0x60000000 - 0x7FFFFFFF RAM Yes WB, WA, RA Execute disabled
@@ -82,10 +90,28 @@ void mbed_mpu_init()
8290
1, // Read-Only
8391
1, // Non-Privileged
8492
0), // Execute Never disabled
93+
ARM_MPU_RLAR(
94+
MBED_MPU_ROM_END, // Limit
95+
AttrIndex_WTRA) // Attribute index - Write-Through, Read-allocate
96+
);
97+
98+
#if MBED_MPU_RAM_START != 0x20000000
99+
ARM_MPU_SetRegion(
100+
4, // Region
101+
ARM_MPU_RBAR(
102+
MBED_MPU_RAM_START, // Base
103+
ARM_MPU_SH_NON, // Non-shareable
104+
0, // Read-Write
105+
1, // Non-Privileged
106+
1), // Execute Never enabled
85107
ARM_MPU_RLAR(
86108
0x1FFFFFFF, // Limit
87109
AttrIndex_WTRA) // Attribute index - Write-Through, Read-allocate
88110
);
111+
#define LAST_RAM_REGION 4
112+
#else
113+
#define LAST_RAM_REGION 3
114+
#endif
89115

90116
ARM_MPU_SetRegion(
91117
1, // Region
@@ -173,7 +199,7 @@ void mbed_mpu_enable_ram_xn(bool enable)
173199
// Flush memory writes before configuring the MPU.
174200
__DMB();
175201

176-
for (uint32_t region = 1; region <= 3; region++) {
202+
for (uint32_t region = 1; region <= LAST_RAM_REGION; region++) {
177203
enable_region(enable, region);
178204
}
179205

targets/targets.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
},
3333
"mpu-rom-end": {
3434
"help": "Last address of ROM protected by the MPU",
35-
"value": "0x0fffffff",
36-
"macro_name": "MPU_ROM_END"
35+
"value": "0x0fffffff"
3736
}
3837
}
3938
},

0 commit comments

Comments
 (0)