Teensy 4.1: exact 2^30-cycle CPU stall (1491 ms @720 MHz), depends on the build

If the second chip is absent or PSRAM: yes.
If the second chip is flash and the combined sum of both chips is smaller than 32MB: no, because LittleFS will overwrite FLEXSPI2_FLSHA2CR0.

Thanks, that makes sense. On my board both chips are PSRAM, so leaving A2CR0 at 64 MB works there (results above). For a flash on chip 2 where PSRAM + flash is below 32 MB, LittleFS rewriting A2CR0 would bring the hole back, so resizing the MPU region to the detected total (A1 + A2) is the only variant I tested that does not depend on what else touches the FlexSPI2 registers. I cannot test the LittleFS case myself, as I have no flash on that bus.
 
Ok, just to make sure I understand this, it seems the fundamental problem is Cortex M7 speculatively fetches beyond actually accessed memory.

I have seen mention about the cache controller speculatively fetching the next row after what the program actually used. I'm a bit skeptical about the explanation of merely having an address in a register with no LD or ST instruction actually using it. Whatever the reason, what matters is understanding how far beyond the end of actually accessed memory we need to anticipate?
I agree the "value in a register" explanation is not proven, and I could not reproduce speculation with a small program (mispredicted branch trained 200 times, worst case 14 cycles). What I can offer is what my logger data say about the "how far" question.

1. It is not a next-line prefetch
The two stalls I captured in the logger (build with the 32 MB region, DIAG.LOG, 5 s resolution) happened when micros() was 0x71E4DAC2 (about 14.3 MB behind the end of the 16 MB PSRAM) and about 7 s earlier in the same window (about 7 MB behind the end). A cache-row prefetch of the line after the last used one cannot be 7-14 MB away from anything the program accessed. Stalls only occurred while micros() was inside 0x71000000..0x71FFFFFF, and never in the other 4294 s of the 32-bit period. After the MPU resize 16 such windows (about 21 h in two runs) were clean.

So in my case the distance is "anywhere in the mapped range of the region", not "a few lines". The only way I see to cover it is to make the whole region that the MPU marks as cacheable Normal memory actually have a device behind it, or to shrink the MPU region to what is fitted.

2. The stall signature points to a cache line fill that nobody waited for
In sts1_test a direct, architectural load from the hole stalls 2^28 cycles (0.37 s at 720 MHz) and then raises a BusFault (CFSR 0x8200, BFAR = the address, INTR bit 10 set). In the logger the stall is 2^30 cycles (1.49 s) and there is no fault at all, the program just continues. 2^30 = 4 x 2^28 fits a 32-byte line fill done as four 64-bit AHB bursts that each wait for the timeout (jmarsh's explanation), and a missing fault fits an access whose result was discarded. I cannot prove from this that it was speculative rather than a real load whose fault was suppressed, but a real load in my code should have produced the 0.37 s BusFault like the test does. I have not checked the generated code for a load that could use that value.

3. What I would conclude
Whatever the trigger, an access anywhere inside the 32 MB region above the fitted PSRAM can stall the CPU, so the safe fixes are those that make the whole region valid: MPU size = fitted size (tested), or FLSHA2CR0/B1/B2 so that every address in the region is claimed by a chip that answers (tested with A2CR0 = 32 MB and 64 MB; caveat: LittleFS flash on chip 2 can rewrite A2CR0).
 
As I understand this so far, these are the 3 bad outcomes?
  1. Access beyond A1 + A2 regions stalls because B1 and B2 aren't configured.
  2. Access meant for PSRAM into A2 region with PSRAM + Flash could trigger LittleFS QSPI sequences.
  3. Access outside MPU allowed range causes a fault.
Is #3 an issue? Asking because of suggesting "or to shrink the MPU region to what is fitted". If the problem is access beyond where the program actually accesses, wouldn't this suggestion result in false memory faults? How should we handle the case of two 16MB chips?
 
As I understand this so far, these are the 3 bad outcomes?
  1. Access beyond A1 + A2 regions stalls because B1 and B2 aren't configured.
  2. Access meant for PSRAM into A2 region with PSRAM + Flash could trigger LittleFS QSPI sequences.
  3. Access outside MPU allowed range causes a fault.
Is #3 an issue? Asking because of suggesting "or to shrink the MPU region to what is fitted". If the problem is access beyond where the program actually accesses, wouldn't this suggestion result in false memory faults? How should we handle the case of two 16MB chips?
Yes, those three are a fair summary, with one correction on #3: it is not an issue in practice, and I have data for that.

1. Correct. This is the case I hit: two PSRAM chips on A1/A2 (8 + 8 MB), the MPU region is 32 MB, so everything above A1 + A2 goes to B1/B2, which are unconfigured defaults (64 MB, LUT sequence 0, no data), and FlexSPI waits for AHB bus timeout.

2. I cannot test this one (no flash on the FlexSPI2 bus on my board), so I just repeat jmarsh's point: if chip 2 is flash and LittleFS rewrites A2CR0, the A2CR0-based fixes no longer cover the hole.

3. A MemManage fault from a region that is too small only happens if an instruction that is actually executed accesses an address inside the no-access range. A speculative access, a mispredicted-path load or a prefetch never retires, so it cannot raise a precise fault; it is just dropped by the MPU. That is also what I measured:
  • With the MPU region resized to the fitted size (mpu_fix.h: SIZE 23 for 16 MB, r9=70000000/23 in my log), my logger ran two overnight runs (12 h 9 min and 8 h 35 min, about 21 h in total) with micros() passing through 0x71000000..0x71FFFFFF 16 times, plus normal PSRAM use (1 MB ring buffer in PSRAM, extmem allocations). No MemManage fault, no reset, no bad frames, CPU gap stayed at 62 ms. The same build with the 32 MB region stalled 1.49 s in that window.
  • If the stall were a speculative access that gets reported as a fault, the logger would have crashed in each of those 16 windows instead of running through them.
  • A real read behind the PSRAM does fault immediately (sts1_test with mpu_fix.h: MemManage after 116 cycles, MMFAR = 0x71000000), which I would count as the desired behaviour, since that is a program bug (pointer into memory that does not exist) that otherwise stalls the CPU for 0.37 s and then BusFaults anyway.

A false fault could only occur for code that legitimately touches PSRAM beyond the size detected at boot. I do not see such a case, and the core's own PSRAM size detection happens before the MPU is configured, but someone who knows startup.c better than I do should check that. Also the region size has to be a power of two, so for 24 MB I used a 32 MB region with the upper 8 MB disabled via a sub-region (SRD) mask; that variant is untested on hardware.

So on the 16 MB board the MPU variant is the strictest one (fault instead of silent aliasing), the A2CR0 variants avoid the MPU change altogether but alias the hole silently.
 
Ok, just to make sure I understand this, it seems the fundamental problem is Cortex M7 speculatively fetches beyond actually accessed memory.
No. The fundamental problem is having a region in the MPU that is larger than the physical memory. Speculative execution will not attempt to access any memory that is marked inaccessible by the MPU.
Programming FlexSPI2 to route the memory access to any chip (instead of not trying to do anything and just timing out) is a workaround.
I have seen mention about the cache controller speculatively fetching the next row after what the program actually used. I'm a bit skeptical about the explanation of merely having an address in a register with no LD or ST instruction actually using it.
There will be a load somewhere that's causing the speculative access, but it may be on a branch that isn't taken. It could even be data in the ITCM being interpreted by the lookahead unit as code, e.g. jump tables or pool constants.
Whatever the reason, what matters is understanding how far beyond the end of actually accessed memory we need to anticipate?
The size of the MPU region is the hard cut-off. But a single region is also restricted to a power-of-2, which is no longer guaranteed to match the total PSRAM size.
3. Access outside MPU allowed range causes a fault.
Is #3 an issue? Asking because of suggesting "or to shrink the MPU region to what is fitted". If the problem is access beyond where the program actually accesses, wouldn't this suggestion result in false memory faults?
Speculative access can't cause a fault - if it would trigger one, it is discarded. If there's a faulting non-speculative access then that will trigger a fault (and should because it's an error).
 
No. The fundamental problem is having a region in the MPU that is larger than the physical memory. Speculative execution will not attempt to access any memory that is marked inaccessible by the MPU.
Programming FlexSPI2 to route the memory access to any chip (instead of not trying to do anything and just timing out) is a workaround.

There will be a load somewhere that's causing the speculative access, but it may be on a branch that isn't taken. It could even be data in the ITCM being interpreted by the lookahead unit as code, e.g. jump tables or pool constants.

The size of the MPU region is the hard cut-off. But a single region is also restricted to a power-of-2, which is no longer guaranteed to match the total PSRAM size.


Speculative access can't cause a fault - if it would trigger one, it is discarded. If there's a faulting non-speculative access then that will trigger a fault (and should because it's an error).
Thanks, that clarifies it. I agree the root cause is an MPU region larger than the installed memory. For sizes that are not a power of two (e.g. 24 MB) I use a 32 MB region with the upper sub-regions disabled (SRD); this is only tested for 16 MB so far.
 
Ok, look, I'm going to do something for 1.63. But I want you to understand how uncomfortable I am doing anything without a test case and so much guesswork and uncertainty as to what actually causes this problem.
 
Ok, look, I'm going to do something for 1.63. But I want you to understand how uncomfortable I am doing anything without a test case and so much guesswork and uncertainty as to what actually causes this problem.
Understood, and thanks for doing something for 1.63. To separate what is certain from what is not:


Certain and reproducible in two minutes on any board with less than 32 MB PSRAM: a direct read behind the PSRAM stalls 2^28 cycles and BusFaults on the stock core (sts1_test / psram_hole_test, source in my earlier posts). With the MPU region resized it faults immediately (116 cycles), with A2CR0 at 32 or 64 MB it returns after about 330 cycles without a fault.


Not known: which instruction in my application triggers it speculatively. I could not reproduce that part with a small program. But the fix does not depend on it: any change that makes every address in the MPU region land on a real chip (or removes the part with no chip from the MPU) removes the problem whatever the trigger is.


When 1.63 beta is available I will run psram_hole_test on it (it only reports PASS if no read behind the PSRAM takes more than 1,000,000 cycles and INTR bit 10 stays clear), then run my logger through the micros() = 0x71xxxxxx window again (first at 1895.8 s after boot, repeats every 4295 s).
 
This is the part which gives me pause and makes the decision for 1.63 so difficult

With the MPU region resized it faults immediately
....
Not known: which instruction in my application triggers it speculatively. I could not reproduce that part with a small program.

Yes, I hear you're only concerned about the bus timeout problem.

But for a public software release, false memory faults are not an acceptable outcome!

The info I'm hearing so far is you're certain your large program isn't actually accessing outside the 16MB range of your pair of 8MB PSRAM chips, and you do get memory faults when the MPU is configured to limit access to 16MB, and you're sure it's not just fetching 1 extra cache row, but there's no real understand of why this is happening...

Can you see how that's a problem for me to make a decision for 1.63 release?
 
The "Doom game" from the old thread did not access "forbidden" adresses, as far as I remember.
Edit:
As long as a memory region is marked as accessible, the MPU won't prevent the CPU from accessing it, regardless of which part of the core initiates the access.
If I understand NXP correctly, this is also how they intend the MPU to be used. In other words: If no memory is present at an address, that address range should be marked as no-access in the MPU.

While you're looking at the MPU configuration, it might be worth checking this as well.
SCB_MPU_RBAR = 0x80000000 | REGION(i++); // SEMC: SDRAM, NAND, SRAM, etc
SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1G;
I'm just mentioning it as another area worth looking into while you're at it.
 
Last edited:
We never execute code from PSRAM. The MPU setting has NOEXEC and the linker script doesn't generate any code in that memory region.
That's not what I'm saying. I'm talking about the case where a register holds a value that matches an address in the non-existent memory range and there's a load instruction that uses that register as the source address somewhere near the code currently being executed. The load gets speculatively executed because the branch predictor thinks it will happen, but the prediction is wrong and a different branch ends up being taken.

Ok, look, I'm going to do something for 1.63. But I want you to understand how uncomfortable I am doing anything without a test case and so much guesswork and uncertainty as to what actually causes this problem.
At this point it's pretty certain; there's been 3 different reports, all confirmed to be instances of FlexSPI2 stalling the AHB bus for a multiple of AHBBUSWAIT, all involving boards with 2x 8MB PSRAM chips fitted. That specific configuration is the only time both FLEXSPI2_FLSHA1CR0 and FLEXSPI2_FLSHA2CR0 are set to a total size that is less than the size of the MPU region.

Try this code on a board with 2x 8MB PSRAM chips fitted:
Code:
#include <Arduino.h>

elapsedMillis ticker;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWriteFast(LED_BUILTIN, LOW);
}

void loop() {
  static uint16_t offset;
  if (ticker > 500) {
    digitalToggle(LED_BUILTIN);
    ticker = 0;
  }
  asm volatile("pld [%0, %1]" :: "r"(0x71000000), "r"(offset));
  ++offset;
}
This is not speculative execution; it's a direct preload instruction, which (in theory) isn't allowed to trigger a fault on a bad memory address. This is the best I can do to simulate speculative execution since it's a wild and unpredictable beast.

I don't have a board with 2x 8MB to test with but it runs fine on a board with 1x 8MB, flashing the LED at 1Hz as expected. But I suspect it will flash much slower on a 2x 8MB board due to triggering constant AHB bus stalls.
 
I don't have a board with 2x 8MB to test with but it runs fine on a board with 1x 8MB, flashing the LED at 1Hz as expected. But I suspect it will flash much slower on a 2x 8MB board due to triggering constant AHB bus stalls.
I just happened to have a Teensy 4.1 with 8+8MB fitted loafing about doing nothing, so I tried the above. It is as @jmarsh says: the LED flashes slower than 1Hz, but at 1Hz when the memory address is set to 0x70100000 (a valid address).
 
Here's a variant on the code, which switches between valid and invalid preloads
Code:
#include <Arduino.h>

elapsedMillis ticker;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWriteFast(LED_BUILTIN, LOW);
}

void loop() {
  static int toggle;
  static uint16_t offset;
  if (ticker > 500) {
    digitalToggle(LED_BUILTIN);
    ticker = 0;
    ++toggle;
  }
  if (toggle & 8)
    asm volatile("pld [%0, %1]" :: "r"(0x7100'0000), "r"(offset)); 
  else   
    asm volatile("pld [%0, %1]" :: "r"(0x7010'0000), "r"(offset)); 
  ++offset;
}

Running is here on a board with two 8MB PSRAM. Looks like 3.58 second period.
I agree
 
This is the part which gives me pause and makes the decision for 1.63 so difficult



Yes, I hear you're only concerned about the bus timeout problem.

But for a public software release, false memory faults are not an acceptable outcome!

The info I'm hearing so far is you're certain your large program isn't actually accessing outside the 16MB range of your pair of 8MB PSRAM chips, and you do get memory faults when the MPU is configured to limit access to 16MB, and you're sure it's not just fetching 1 extra cache row, but there's no real understand of why this is happening...

Can you see how that's a problem for me to make a decision for 1.63 release?
I think there is a misunderstanding, and I may have caused it with the sentence about faulting. Let me separate the two cases:


  1. My large application, with the MPU region resized to 16 MB: NO faults at all. Two overnight runs (12 h 9 min and 8 h 35 min, about 21 h total), micros() went through 0x71000000..0x71FFFFFF 16 times, no MemManage fault, no reset, no bad data, CPU latency unchanged. The same build with the 32 MB region stalled 1.49 s in that window every time.
  2. The immediate fault (“116 cycles”) is only the synthetic test sts1_test, which deliberately does a normal load from 0x71000040. That is a real access outside the installed memory, so a fault is the correct result there.

So the application does not make any real access beyond 16 MB (otherwise the resized region would have faulted in it), and the stall comes from an access that never retires; as jmarsh said, the MPU drops such accesses without raising a fault. I do not know which instruction issues it, but both outcomes (no false faults in the app, stall gone) were measured, not assumed.


If a possible false fault is the concern for a public release, the A2CR0 variant (leave FLSHA2CR0 at the 64 MB reset value or set it to 32 MB) does not touch the MPU at all, and it passed the same test on my board (about 330 cycles, no fault, INTR bit 10 clear). The limit, as jmarsh pointed out, is the PSRAM + flash on chip 2 case where LittleFS rewrites A2CR0.
 
p#21 test code:
1793067932335.png

A couple of T_4.1's PASS 8MB and 1 16MB, and nothing to test 32 MB:

Code:
PSRAM 8 MB, FLSHA1CR0=0x2000 FLSHA2CR0=0x10000
  0x70800040 -> 0x00000000, 283 cycles
  0x70900040 -> 0x00000000, 279 cycles
  0x70A00040 -> 0x00000000, 279 cycles
  0x70B00040 -> 0x00000000, 279 cycles
  0x70C00040 -> 0x00000000, 702 cycles
  0x70D00040 -> 0x00000000, 283 cycles
  0x70E00040 -> 0x00000000, 283 cycles
  0x70F00040 -> 0x00000000, 279 cycles
FLEXSPI2_INTR = 0x40
PASS

Code:
SD card is Inserted
PSRAM 16 MB, FLSHA1CR0=0x4000 FLSHA2CR0=0x10000
  0x71000040 -> 0x00000000, 283 cycles
  0x71100040 -> 0x00000000, 279 cycles
  0x71200040 -> 0x00000000, 279 cycles
  0x71300040 -> 0x00000000, 279 cycles
  0x71400040 -> 0x00000000, 702 cycles
  0x71500040 -> 0x00000000, 283 cycles
  0x71600040 -> 0x00000000, 283 cycles
  0x71700040 -> 0x00000000, 279 cycles
FLEXSPI2_INTR = 0x40
PASS

Code:
PSRAM 32 MB, FLSHA1CR0=0x4000 FLSHA2CR0=0x4000
nothing to test

But when 32MB tested:
Code:
PSRAM 32 MB, FLSHA1CR0=0x4000 FLSHA2CR0=0x4000
CrashReport:
  A problem occurred at (system time) 19:27:43
  Code was executing from address 0x12C
  CFSR: 82
    (DACCVIOL) Data Access Violation
    (MMARVALID) Accessed Address: 0x72000040
  Temperature inside the chip was 54.70 °C
  Startup CPU clock speed is 600MHz
  Reboot was caused by auto reboot after fault or bad interrupt detected
PSRAM 32 MB, FLSHA1CR0=0x4000 FLSHA2CR0=0x4000
 
I ran jmarsh's alternating PLD test on my board (Teensy 4.1, 2 x 8 MB APS6404L = 16 MB, core 1.62.0, 720 MHz). It reproduces the 2^30-cycle stall with the stock core, and the three fixes remove it. The MPU variant does not fault.

Test
jmarsh's code unchanged (PLD [0x71000000 + offset] for 4 s, then PLD [0x70100000 + offset] for 4 s, alternating), plus DWT_CYCCNT around the PLD and a once-per-second print of: number of PLDs done, worst PLD in cycles and FLEXSPI2_INTR. FLEXSPI2_INTR is cleared in setup(). Only the setup() part differs between the four runs:
  • USE_FIX 0: stock, nothing changed.
  • USE_FIX 1: MPU region 0x70000000 resized to the installed PSRAM (16 MB, SIZE field 23) in startup_middle_hook().
  • USE_FIX 2: MCR0[MDIS] set, FLEXSPI2_FLSHA2CR0 = 32 * 1024, MDIS cleared.
  • USE_FIX 3: same with FLEXSPI2_FLSHA2CR0 = 64 * 1024 (the reset default, same final state as removing line 545).

Results (PLD to 0x71000000 + offset, the invalid phase)
Code:
USE_FIX 0 (stock):   first PLD of each phase 1,073,724,xxx cycles (2^30 = 1.491 s), then
                     2,147,442,xxx cycles (2^31 = 2.983 s) per cache line, 32 PLDs per row.
                     FLEXSPI2_INTR 0x40 -> 0x440 (bit 10, AHBBUSTIMEOUT) at the first invalid PLD.
                     No fault. LED period matches Paul's 3.58 s observation in kind (long stalls).
USE_FIX 1 (MPU 16M): 10.9 M PLDs/s, worst PLD 40 (rare outliers up to 1230 cycles, also in the valid phase),
                     INTR stays 0x40, no fault, no stall.
USE_FIX 2 (A2 32M):  11.08 M PLDs/s, worst PLD 38 (outliers up to ~1040), INTR stays 0x40, no stall.
USE_FIX 3 (A2 64M):  11.08 M PLDs/s, worst PLD 38 (outliers up to ~1240), INTR stays 0x40, no stall.
Valid phase in all four runs: about 10.9-11.1 M PLDs per second, worst 40 cycles.

What this shows
  • The stock-core stall from a non-faulting preload is 2^30 cycles with no BusFault, the same signature as the 1.49 s stall in my application (a direct load behind the PSRAM gave 2^28 cycles and a BusFault in my earlier test). So the hardware behaviour of my application is reproduced by a few lines of code.
  • With the MPU region resized, the preload to the now-inaccessible range neither stalls nor faults (the MPU drops it, as jmarsh said). That answers the false-fault question for this class of access: no MemManage fault, no stall, 55 s over several phase changes. A normal load there would of course fault, which is the intended behaviour.
  • The FlexSPI2 A2CR0 variants (32 MB and 64 MB) also remove the stall, again without any fault. Caveat from before: not applicable if chip 2 is flash and LittleFS rewrites A2CR0, and I could not test that.

My logger has been running the MPU variant for three overnight runs (12 h 9 min, 8 h 35 min and 12 h 6 min, 26 windows in which micros() is in 0x71xxxxxx, 0 bad frames, no faults).

I do not know why the later stalls in the stock run are 2^31 and not 2^30; it may be two timeouts back to back (two line fills), but that is a guess.
 
I've been working on a hopefully permanent fix for core library. Please give this startup.c code a try.

I tried to incorporate both fix #1 and fix #2. The MPU is configured for exactly the total detected memory size. The last required FLSH*CR0 region is also configured for twice the actual chip size, so hopefully we don't stall even if the MPU fix isn't effective, or the MPU gets disabled.

In the case of PSRAM+Flash, FLSHA1CR0 gets double the chip's size. Still needs testing with LittleFS, but just in case of speculative fetches, it should give some safety from any speculative access accidentally interfering with LittleFS flash.

I also made an attempt at the 24MB cases. Right now I have 5 test boards with 0, 8, 16, 8+8 and 16+16. These the 3 earliest 16MB chips @KenHahn sent me (thanks Ken, you're awesome). I've placed an order with Mouser for more 16MB chips, so next week I should be able to test the 8+16 and 16+8 cases.

So far my testing has mostly looked for the fault handler. Hoping for feedback on the timing and performance.
 

Attachments

  • startup.c
    29.6 KB · Views: 19
Right now I have 5 test boards with 0, 8, 16, 8+8 and 16+16. These the 3 earliest 16MB chips @KenHahn sent me (thank Ken, you're awesome). I've placed an order with Mouser for more 16MB chips, so next week I should be able to test the 8+16 and 16+8 cases.
@PaulStoffregen, do / will you also have the hardware to test 8MB+Flash and 16MB+Flash, with LittleFS or similar? I'm not fully following all the register-bashing here, but it seems these are the other hardware options that could affect results.

Indeed Ken is awesome! (Never bought anything off him, as it's awkward from the UK, but his product line looks great, and he's good at supporting people on here).
 
I've been working on a hopefully permanent fix for core library. Please give this startup.c code a try.

I tried to incorporate both fix #1 and fix #2. The MPU is configured for exactly the total detected memory size. The last required FLSH*CR0 region is also configured for twice the actual chip size, so hopefully we don't stall even if the MPU fix isn't effective, or the MPU gets disabled.

In the case of PSRAM+Flash, FLSHA1CR0 gets double the chip's size. Still needs testing with LittleFS, but just in case of speculative fetches, it should give some safety from any speculative access accidentally interfering with LittleFS flash.

I also made an attempt at the 24MB cases. Right now I have 5 test boards with 0, 8, 16, 8+8 and 16+16. These the 3 earliest 16MB chips @KenHahn sent me (thanks Ken, you're awesome). I've placed an order with Mouser for more 16MB chips, so next week I should be able to test the 8+16 and 16+8 cases.

So far my testing has mostly looked for the fault handler. Hoping for feedback on the timing and performance.
Tested your new startup.c on core 1.62.0 (Teensy 4.1, 2 x 8 MB APS6404L = 16 MB, 720 MHz). No mpu_fix.h or any other workaround in the sketches.

Register state after boot (printed by the test sketch)
Code:
PSRAM 16 MB, FLSHA1CR0=0x2000 (8 MB), FLSHA2CR0=0x4000 (16 MB), MPU 0x70000000 SIZE=23 (16 MB), SRD=0
As intended: A1 = chip size, A2 = twice the chip size, MPU region = installed memory.

1. PLD test (jmarsh's alternating valid / invalid preload, 0x71000000 + offset)
About 50 s, six phase changes: invalid phase runs at 10.9 M PLDs/s, worst PLD 39-40 cycles (same as the valid phase), FLEXSPI2_INTR stays 0x40 (bit 10 AHBBUSTIMEOUT never set), no fault, no stall. On the stock 1.62.0 core the same sketch gave 2^30 cycles (1.491 s) for the first PLD of every invalid phase and INTR 0x440.

2. Direct read behind the PSRAM (sts1_test, plain 32-bit load from 0x71000040)
Code:
vector 4 (MemManage), CFSR 0x82 (DACCVIOL + MMARVALID), MMFAR = BFAR = 0x71000040
fault after 29 cycles
FLEXSPI2_INTR 0x40 (IPTXWE only, bit 10 and 11 clear), STS1 0
Stock core: 268,431,504 cycles and then a BusFault. So a real access beyond the installed memory now faults immediately, which is what I would want, while the non-faulting speculative-style access (PLD) is silently dropped.

Timing / performance
My logger (which previously used a sketch-level MPU resize) will run an overnight test on this startup.c next; I will report CPU latency (GAP, loop time) and bad frames after that.

One comment on the backup plan
With A2CR0 = 2 x chip size, the backup only covers up to the end of the doubled last region (24 MB on my 8+8 board). In my application the stall happened with micros() up to about 14 MB behind the end of the PSRAM, i.e. well outside such a window. So if the MPU region were ever not effective, the backup would only help for the first part of the hole. I can test that by widening the MPU region back to 32 MB in a sketch and using PLD at 0x71800000 and above, if that is useful.

Note on psram_hole_test
The small regression test posted earlier expected "no fault, fast reads". With this startup.c the first read behind the PSRAM now correctly faults at once (MemManage, DACCVIOL), which the old version reported as a crash. I updated it to classify the CrashReport after the reboot: DACCVIOL = "PASS (immediate MemManage fault)", BusFault / stall = "FAIL", and all-fast-reads without a fault = "PASS (no stall)". On my board with your startup.c:
Code:
CrashReport: CFSR 0x82 (DACCVIOL), accessed address 0x71000040
PSRAM 16 MB, FLSHA1CR0=0x2000 FLSHA2CR0=0x4000
PASS (immediate MemManage fault: MPU region matches the installed memory)
 
With A2CR0 = 2 x chip size, the backup only covers up to the end of the doubled last region (24 MB on my 8+8 board). In my application the stall happened with micros() up to about 14 MB behind the end of the PSRAM, i.e. well outside such a window. So if the MPU region were ever not effective, the backup would only help for the first part of the hole.

After you've done plenty of testing with it as-is, please try deleting these last 6 lines from configure_external_ram(), so we can get some measurement of fix #2 without fix #1.

Code:
        asm("dsb");
        SCB_MPU_CTRL = 0;
        SCB_MPU_RBAR = 0x70000000 | REGION(9); // FlexSPI2
        SCB_MPU_RASR = rasr;
        SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
        asm("dsb");
 
After you've done plenty of testing with it as-is, please try deleting these last 6 lines from configure_external_ram(), so we can get some measurement of fix #2 without fix #1.

Code:
        asm("dsb");
        SCB_MPU_CTRL = 0;
        SCB_MPU_RBAR = 0x70000000 | REGION(9); // FlexSPI2
        SCB_MPU_RASR = rasr;
        SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
        asm("dsb");
Yes, for sure. Right now I'm running long test with Your startup.c in core, After 8 h I can report about this
 
One comment on the backup plan
With A2CR0 = 2 x chip size, the backup only covers up to the end of the doubled last region (24 MB on my 8+8 board). In my application the stall happened with micros() up to about 14 MB behind the end of the PSRAM, i.e. well outside such a window. So if the MPU region were ever not effective, the backup would only help for the first part of the hole. I can test that by widening the MPU region back to 32 MB in a sketch and using PLD at 0x71800000 and above, if that is useful.

Here's another version meant to cover the 14MB window you observed. It configures the last used FLSH*CR0 to 32MB rather than double the chip size, which is only 16MB for the 8MB chip.

I had hoped to use smaller to maximize the amount left available for LittleFS, but taking another 16MB or 24MB is not a big deal, especially if it fully solves this problem even when the MPU is disabled default 32MB config. It's still an improvement over the old way where these were left with their default 64MB settings.


EDIT: didn't really mean "disabled", just the code which reconfigures to exactly the PSRAM size is disabled, so the MPU is enabled but configured as we have today with 32MB fixed max size.
 

Attachments

  • startup.c
    29.6 KB · Views: 20
Last edited:
Back
Top