diff options
| author | Ian McIntyre <me@mciantyre.dev> | 2026-02-11 16:29:27 -0500 |
|---|---|---|
| committer | Ian McIntyre <me@mciantyre.dev> | 2026-03-08 14:14:31 -0400 |
| commit | a9bb416c822d20c1b94fb7b2bfab282d25e11125 (patch) | |
| tree | 5dcf3e22c65a4422e71a103ca1afbe8b5f5e5e0c | |
| parent | 57978b04a7e141e0f34bc24088771a6fedea634e (diff) | |
Preload RAM for ECC ciphers
Testing shows that we must blow fuses in order to activate either ECC
engine. Once the fuse is blown, NXP's boot ROM will enable the ECC
engine. So by the time the reset handler is running, ECC may already
be enabled.
The 32-bit copy loops won't work for initializing ITCM and OCRAM.
Since these bus widths are 64-bit and the ECC is computed over 64
bits, these 32 bit accesses result in a read-modify-write (RMW) on the
underlying interface. If these RAM regions aren't already initialized,
we'll fail an ECC check; the read in RMW compares bogus RAM contents
against a bogus ECC. This seems to dramatically slow down
initialization, or stall it (I've seen the CPU spinning in the boot
ROM).
If we listen to NXP and use 64-bit writes to preload ECC-protected RAM
regions, the 32-bit copy loops will work as expected. Note that we can
still issue 64-bit accesses towards DTCM; they're scattered across the
two 32-bit interfaces. There's an NXP whitepaper [1] describing that.
Once TCM ECC is enabled, we need to tell the TCM interfaces to RMW
smaller accesses. Otherwise, we won't generate correct ciphers. We
also might need to retry operations if there was a correction by the
RAM controller. We can perform these with predicated instructions.
Tested on an 1010EVK, where I used the debugger to show the control
registers weren't touched; and tested on the 1170EVK, with the same
approach showing the expected set bits.
[1]: https://www.nxp.com/docs/en/white-paper/CORTEXM7WP.pdf
[2]: https://www.nxp.com/docs/en/application-note/AN13204.pdf
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | board/build.rs | 2 | ||||
| -rw-r--r-- | src/host.rs | 56 | ||||
| -rw-r--r-- | src/target.rs | 52 | ||||
| -rw-r--r-- | tests/inspect_elf.rs | 32 |
5 files changed, 143 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d97758f..49b1683 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ You may optionally enable ECC RAM on 11xx MCUs. The implementation supports - MECC64 for dedicated OCRAM. See the ECC RAM documentation on `RuntimeBuilder` for more information. +When enabled, pre-init will preload ECC ciphers by writing zero to RAM regions. ## [0.1.7] 2025-06-14 diff --git a/board/build.rs b/board/build.rs index af17a95..0e79920 100644 --- a/board/build.rs +++ b/board/build.rs @@ -61,6 +61,8 @@ fn main() { .rodata(imxrt_rt::Memory::Dtcm) .stack_size_env_override("BOARD_STACK") .heap_size_env_override("BOARD_HEAP") + .flexram_ecc(imxrt_rt::FlexRamEcc::Enable) + .mecc64(imxrt_rt::Mecc64::Enable) .build() .unwrap(), "imxrt1010evk_ram" => { diff --git a/src/host.rs b/src/host.rs index 5f1dc33..e809895 100644 --- a/src/host.rs +++ b/src/host.rs @@ -816,6 +816,7 @@ impl RuntimeBuilder { "__flexram_config = {:#010X};", flexram_config(self.family, &self.flexram_layout) )?; + // The target runtime looks at this value to predicate some pre-init instructions. // Could be helpful for binary identification, but it's an undocumented feature. writeln!(writer, "__imxrt_rt_v0.2 = {:#010X};", self.family.id(),)?; @@ -925,6 +926,59 @@ fn write_flexram_memories( Ok(()) } +/// Generate spans for zeroing ECC RAM. +fn write_ecc_zero_spans( + output: &mut dyn Write, + family: Family, + flexram_layout: &[FlexRamKind], + flexram_ecc: FlexRamEcc, + mecc64: Mecc64, +) -> io::Result<()> { + let itcm_count = layout_count_of(FlexRamKind::Itcm, flexram_layout); + let dtcm_count = layout_count_of(FlexRamKind::Dtcm, flexram_layout); + let ocram_count = layout_count_of(FlexRamKind::Ocram, flexram_layout); + + // If memory isn't available or doesn't need + // use ECC, then we don't need to zero it. + // Generate dummy values that skip the loops. + + if itcm_count > 0 && flexram_ecc.is_enable() { + // Make sure to zero the reserved regions, too, + // just in case they're accidentally / intentionally accessed. + let itcm = family.itcm_start_size(itcm_count); + let start = itcm.start - 32; + let end = start + itcm.size + 32; + writeln!(output, "__sitcm = {:#010X};", start)?; + writeln!(output, "__eitcm = {:#010X};", end)?; + } else { + writeln!(output, "__sitcm = 0;")?; + writeln!(output, "__eitcm = 0;")?; + }; + + if dtcm_count > 0 && flexram_ecc.is_enable() { + writeln!(output, "__sdtcm = ORIGIN(DTCM);")?; + writeln!(output, "__edtcm = ORIGIN(DTCM) + LENGTH(DTCM);")?; + } else { + writeln!(output, "__sdtcm = 0;")?; + writeln!(output, "__edtcm = 0;")?; + }; + + let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64); + + // The zeroization should specifically target one of the two regions. + // But we're not prepared to do that, so incur the loop for the whole + // region no matter which is enabled. + if ocram.size > 0 && (mecc64.is_enable() || flexram_ecc.is_enable()) { + writeln!(output, "__socram = {:#010X};", ocram.start)?; + writeln!(output, "__eocram = {:#010X};", ocram.start + ocram.size)?; + } else { + writeln!(output, "__socram = 0;")?; + writeln!(output, "__eocram = 0;")?; + } + + Ok(()) +} + /// Generate a linker script MEMORY command that includes a FLASH block. fn write_flash_memory_map( output: &mut dyn Write, @@ -949,6 +1003,7 @@ fn write_flash_memory_map( write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; writeln!(output, "}}")?; writeln!(output, "__fcb_offset = {:#X};", family.fcb_offset())?; + write_ecc_zero_spans(output, family, flexram_layout, flexram_ecc, mecc64)?; Ok(()) } @@ -970,6 +1025,7 @@ fn write_ram_memory_map( writeln!(output, "MEMORY {{")?; write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; writeln!(output, "}}")?; + write_ecc_zero_spans(output, family, flexram_layout, flexram_ecc, mecc64)?; Ok(()) } diff --git a/src/target.rs b/src/target.rs index 96ae9f7..4515934 100644 --- a/src/target.rs +++ b/src/target.rs @@ -55,6 +55,22 @@ global_asm! {r#" 999: .endm +.macro zero_section64 beg, end + ldr r0, =\beg + ldr r1, =\end + movw r2, #0 + + 888: + cmp r0, r1 + bge 999f + + strd r2, r2, [r0], #8 + b 888b + + 999: + +.endm + __pre_init: ldr r0, =__imxrt_rt_v0.2 @ Need to know which chip family we're initializing. ldr r1, =0x1180 @@ -85,6 +101,42 @@ __pre_init: ldr r1, [r0, #64] @ r1 = *(IMXRT_IOMUXC_GPR + 16) orr r1, r1, #1<<2 @ r1 |= 1 << 2 str r1, [r0, #64] @ *(IMXRT_IOMUXC_GPR + 16) = r1 + + # If there's an ITCM ECC region, make sure + # + # - the CPU issues RMW operations for smaller accesses. + # - the CPU retries operations when signaled. + # + # If there's no ECC region, the host sets the ends + # to zero, and we skip the instructions. + movw r3, #0 + ldr r0, =__eitcm + cmp r0, r3 + itttt ne + ldrne r0, =0xE000EF90 @ ITCMCR + ldrne r1, [r0] @ r1 = *ITCMCR + orrne r1, r1, #3<<1 @ r1 |= 3 << 1, setting RMW and RETEN + strne r1, [r0] @ *ITCMCR = r1 + + # Same as above, but for DTCM. + ldr r0, =__edtcm + cmp r0, r3 + itttt ne + ldrne r0, =0xE000EF94 @ DTCMCR + ldrne r1, [r0] @ r1 = *DTCMCR + orrne r1, r1, #3<<1 @ r1 |= 3 << 1, setting RMW and RETEN + strne r1, [r0] @ *DTCMCR = r1 + + dsb + isb + + # Zero memory regions for any ECC support. + # If ECC isn't enabled, then the host generates + # values that short circuit the loops. + zero_section64 __sitcm , __eitcm + zero_section64 __sdtcm , __edtcm + zero_section64 __socram , __eocram + b 1000f 1180: diff --git a/tests/inspect_elf.rs b/tests/inspect_elf.rs index 654c632..d3c5ae1 100644 --- a/tests/inspect_elf.rs +++ b/tests/inspect_elf.rs @@ -266,6 +266,12 @@ fn imxrt1010evk() { binary.fcb().unwrap() ); assert_eq!(binary.flexram_config().unwrap(), 0b11_10_0101); + assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram").unwrap(), 0); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -380,6 +386,13 @@ fn imxrt1010evk_ram() { let binary = ImxrtBinary::new(&elf, &contents); + assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram").unwrap(), 0); + let stack = binary.section(".stack").unwrap(); assert_eq!( Section { @@ -495,6 +508,12 @@ fn baseline_teensy4( binary.flexram_config().unwrap(), 0b11111111_101010101010101010101010 ); + assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram").unwrap(), 0); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -735,6 +754,12 @@ fn imxrt1170evk_cm7() { binary.fcb().unwrap() ); assert_eq!(binary.flexram_config().unwrap(), 0xFFAAFFAA); + assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0x4_0000); + assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0x2000_0000); + assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0x2004_0000); + assert_eq!(binary.symbol_value("__socram").unwrap(), 0x2024_0000); + assert_eq!(binary.symbol_value("__eocram").unwrap(), 0x2034_0000); let ivt = binary.ivt().unwrap(); assert_eq!(ivt.magic_header, 0x402000D1); @@ -856,6 +881,13 @@ fn imxrt1170evk_cm7_nonboot() { assert!(binary.fcb().is_err()); assert_eq!(binary.flexram_config().unwrap(), 0xFFAAFFAA); + assert_eq!(binary.symbol_value("__sitcm").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm").unwrap(), 0x4_0000); + assert_eq!(binary.symbol_value("__sdtcm").unwrap(), 0x2000_0000); + assert_eq!(binary.symbol_value("__edtcm").unwrap(), 0x2004_0000); + assert_eq!(binary.symbol_value("__socram").unwrap(), 0x2024_0000); + assert_eq!(binary.symbol_value("__eocram").unwrap(), 0x2034_0000); + assert!( binary.ivt().is_err(), "Non boot image still contains boot IVT" |
