diff --git a/baremetal/src/platform/artyvexii/artyvexii.rs b/baremetal/src/platform/artyvexii/artyvexii.rs index 735f462ca..b51c83962 100644 --- a/baremetal/src/platform/artyvexii/artyvexii.rs +++ b/baremetal/src/platform/artyvexii/artyvexii.rs @@ -9,7 +9,7 @@ pub const RAM_BASE: usize = utralib::generated::HW_MAIN_RAM_MEM; // scratch page for exceptions located at top of RAM // NOTE: there is an additional page above this for exception stack -pub const SCRATCH_PAGE: usize = RAM_BASE + RAM_SIZE; +// pub const SCRATCH_PAGE: usize = RAM_BASE + RAM_SIZE; // Arbitrarily placing heap at 16k into RAM, with a 32k length // Total memory is 400k in size @@ -24,7 +24,11 @@ pub const HEAP_LEN: usize = 1024 * 32; pub const SYSTEM_CLOCK_FREQUENCY: u32 = 40_000_000; pub const SYSTEM_TICK_INTERVAL_MS: u32 = 1; +pub const CACHE_LINE_STRIDE_BYTES: usize = 512 / 8; + pub fn early_init() { + // crate::ramtests::ramtests(); + // setup interrupts & enable IRQ handler for characters crate::irq::irq_setup(); crate::debug::Uart::enable_rx(true); @@ -76,3 +80,356 @@ pub fn delay(ms: usize) { timer.wfo(utra::timer0::EV_PENDING_ZERO, 1); } } + +pub const PAGE_SIZE: usize = 4096; +const WORD_SIZE: usize = core::mem::size_of::(); + +const FLG_VALID: usize = 0x1; +const FLG_X: usize = 0x8; +const FLG_W: usize = 0x4; +const FLG_R: usize = 0x2; +#[allow(dead_code)] +const FLG_U: usize = 0x10; +#[allow(dead_code)] +const FLG_G: usize = 0x20; +#[allow(dead_code)] +const FLG_A: usize = 0x40; +#[allow(dead_code)] +const FLG_D: usize = 0x80; + +#[repr(C)] +pub struct PageTable { + entries: [usize; PAGE_SIZE / WORD_SIZE], +} + +// locate the page table entries +pub const ROOT_PT_PA: usize = 0x4000_0000; // 1st level at base of sram +// 2nd level PTs +const SRAM_PT_PA: usize = 0x4000_1000; +const CODE_PT_PA: usize = 0x4000_2000; +const CSR_PT_PA: usize = 0x4000_3000; +const LCD_PT_PA: usize = 0x4000_4000; +const FLASH_PT_PA: usize = 0x4000_5000; +const TEST_PT_PA: usize = 0x4000_6000; + +pub const ROOT_S_PT_PA: usize = 0x4000_7000; +const SRAM_S_PT_PA: usize = 0x4000_8000; +const CODE_S_PT_PA: usize = 0x4000_9000; +const CSR_S_PT_PA: usize = 0x4000_A000; +const TEST_S_PT_PA: usize = 0x4000_B000; + +// exception handler pages. Mapped 1:1 PA:VA, so no explicit remapping needed as RAM area is already mapped. +pub const SCRATCH_PAGE: usize = 0x4000_C000; // update this in irq.rs _start_trap() asm! as the scratch are +pub const _EXCEPTION_STACK_LIMIT: usize = 0x4000_D000; // update this in irq.rs as default stack pointer. The start of stack is this + 0x1000 & grows down +const BSS_PAGE: usize = 0x4000_E000; // this is manually read out of the link file. Equal to "base of RAM" +pub const PT_LIMIT: usize = 0x4000_F000; // this is carved out in link.x by setting RAM base at BSS_PAGE start + +// VAs +const CODE_VA: usize = 0x8000_0000; +const CSR_VA: usize = 0xe000_0000; +const SRAM_VA: usize = 0x4000_0000; +const LCD_VA: usize = 0xB000_0000; +const FLASH_VA: usize = 0x2000_0000; +pub const TEST_VA: usize = 0xF000_0000; + +// PAs (when different from VAs) +const CODE_PA: usize = 0x8000_0000; +const TEST_PA: usize = 0x5000_0000; + +// ASID's for various 'processes' +pub const SUP_ASID: u32 = 1; +pub const USR_ASID: u32 = 2; + +fn set_l1_pte(from_va: usize, to_pa: usize, root_pt: &mut PageTable) { + let index = from_va >> 22; + root_pt.entries[index] = ((to_pa & 0xFFFF_FC00) >> 2) // top 2 bits of PA are not used, we don't do 34-bit PA featured by Sv32 + | FLG_VALID; +} + +fn set_l2_pte(from_va: usize, to_pa: usize, l2_pt: &mut PageTable, flags: usize) { + let index = (from_va >> 12) & 0x3_FF; + l2_pt.entries[index] = ((to_pa & 0xFFFF_FC00) >> 2) // top 2 bits of PA are not used, we don't do 34-bit PA featured by Sv32 + | flags + | FLG_VALID; +} + +#[inline(never)] // correct behavior depends on RA being set. +pub fn test_pivot() { + // crate::println!("test_pivot"); + /* + unsafe { + let pt_clr = ROOT_PT_PA as *mut u32; + for i in 0..(PT_LIMIT - ROOT_PT_PA) / core::mem::size_of::() { + pt_clr.add(i).write_volatile(0x0000_0000); + } + } + */ + // root page table is at p0x6100_0000 == v0x6100_0000 + let mut root_pt = unsafe { &mut *(ROOT_PT_PA as *mut PageTable) }; + let mut sram_pt = unsafe { &mut *(SRAM_PT_PA as *mut PageTable) }; + let mut code_pt = unsafe { &mut *(CODE_PT_PA as *mut PageTable) }; + let mut csr_pt = unsafe { &mut *(CSR_PT_PA as *mut PageTable) }; + let mut lcd_pt = unsafe { &mut *(LCD_PT_PA as *mut PageTable) }; + let mut flash_pt = unsafe { &mut *(FLASH_PT_PA as *mut PageTable) }; + let mut test_pt = unsafe { &mut *(TEST_PT_PA as *mut PageTable) }; + + let mut root_s_pt = unsafe { &mut *(ROOT_S_PT_PA as *mut PageTable) }; + let mut sram_s_pt = unsafe { &mut *(SRAM_S_PT_PA as *mut PageTable) }; + let mut code_s_pt = unsafe { &mut *(CODE_S_PT_PA as *mut PageTable) }; + let mut csr_s_pt = unsafe { &mut *(CSR_S_PT_PA as *mut PageTable) }; + let mut test_s_pt = unsafe { &mut *(TEST_S_PT_PA as *mut PageTable) }; + + set_l1_pte(CODE_VA, CODE_PT_PA, &mut root_pt); + set_l1_pte(CSR_VA, CSR_PT_PA, &mut root_pt); + set_l1_pte(SRAM_VA, SRAM_PT_PA, &mut root_pt); // L1 covers 16MiB, so SP_VA will cover all of SRAM + set_l1_pte(LCD_VA, LCD_PT_PA, &mut root_pt); + set_l1_pte(FLASH_VA, FLASH_PT_PA, &mut root_pt); + set_l1_pte(TEST_VA, TEST_PT_PA, &mut root_pt); + + set_l1_pte(CODE_VA, CODE_S_PT_PA, &mut root_s_pt); + set_l1_pte(CSR_VA, CSR_S_PT_PA, &mut root_s_pt); + set_l1_pte(SRAM_VA, SRAM_S_PT_PA, &mut root_s_pt); + set_l1_pte(TEST_VA, TEST_S_PT_PA, &mut root_s_pt); + + // map code space. This is the only one that has a difference on VA->PA + const CODE_LEN: usize = 0x2_0000; // 128k + for offset in (0..CODE_LEN).step_by(PAGE_SIZE) { + set_l2_pte(CODE_VA + offset, CODE_PA + offset, &mut code_pt, FLG_X | FLG_R | FLG_A | FLG_D | FLG_U); + } + for offset in (0..CODE_LEN).step_by(PAGE_SIZE) { + set_l2_pte(CODE_VA + offset, CODE_PA + offset, &mut code_s_pt, FLG_X | FLG_R | FLG_A | FLG_D); + } + + // crate::println!("mem pte"); + // map sram. Mapping is 1:1, so we use _VA and _PA targets for both args + const SRAM_LEN: usize = 0x2_0000; // 128k + // make the page tables not writeable + for offset in (0..SCRATCH_PAGE - RAM_BASE).step_by(PAGE_SIZE) { + set_l2_pte(SRAM_VA + offset, SRAM_VA + offset, &mut sram_pt, FLG_R | FLG_U); + } + for offset in (0..SCRATCH_PAGE - RAM_BASE).step_by(PAGE_SIZE) { + set_l2_pte(SRAM_VA + offset, SRAM_VA + offset, &mut sram_s_pt, FLG_R); + } + // rest of RAM is r/w/x + for offset in ((SCRATCH_PAGE - RAM_BASE)..SRAM_LEN).step_by(PAGE_SIZE) { + set_l2_pte( + SRAM_VA + offset, + SRAM_VA + offset, + &mut sram_pt, + FLG_W | FLG_R | FLG_X | FLG_A | FLG_D | FLG_U, + ); + } + for offset in ((SCRATCH_PAGE - RAM_BASE)..SRAM_LEN).step_by(PAGE_SIZE) { + set_l2_pte(SRAM_VA + offset, SRAM_VA + offset, &mut sram_s_pt, FLG_W | FLG_R | FLG_X | FLG_A | FLG_D); + } + if true { + // map SoC-local peripherals (ticktimer, etc.) + const CSR_LEN: usize = 0x2_0000; // 128k + for offset in (0..CSR_LEN).step_by(PAGE_SIZE) { + set_l2_pte(CSR_VA + offset, CSR_VA + offset, &mut csr_pt, FLG_W | FLG_R | FLG_A | FLG_D | FLG_U); + } + for offset in (0..CSR_LEN).step_by(PAGE_SIZE) { + set_l2_pte(CSR_VA + offset, CSR_VA + offset, &mut csr_s_pt, FLG_W | FLG_R | FLG_A | FLG_D); + } + const LCD_LEN: usize = 0x0_8000; // 32k + for offset in (0..LCD_LEN).step_by(PAGE_SIZE) { + set_l2_pte(LCD_VA + offset, LCD_VA + offset, &mut lcd_pt, FLG_W | FLG_R | FLG_U | FLG_A | FLG_U); + } + const FLASH_LEN: usize = 0x10_0000; // 1M + for offset in (0..FLASH_LEN).step_by(PAGE_SIZE) { + set_l2_pte( + FLASH_VA + offset, + FLASH_VA + offset, + &mut flash_pt, + FLG_R | FLG_U | FLG_X | FLG_A | FLG_U, + ); + } + const TEST_LEN: usize = 0x1_0000; // 64k + for offset in (0..TEST_LEN).step_by(PAGE_SIZE) { + set_l2_pte( + TEST_VA + offset, + TEST_PA + offset, + &mut test_pt, + FLG_R | FLG_W | FLG_X | FLG_A | FLG_D | FLG_U, + ); + } + for offset in (0..TEST_LEN).step_by(PAGE_SIZE) { + set_l2_pte( + TEST_VA + offset, + TEST_PA + offset, + &mut test_s_pt, + FLG_R | FLG_W | FLG_X | FLG_A | FLG_D, + ); + } + } + if false { + // clear BSS + unsafe { + let bss_region = core::slice::from_raw_parts_mut(BSS_PAGE as *mut u32, PAGE_SIZE / WORD_SIZE); + for d in bss_region.iter_mut() { + *d = 0; + core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); + } + } + } + + let asid: u32 = SUP_ASID; + let satp: u32 = 0x8000_0000 | asid << 22 | (ROOT_S_PT_PA as u32 >> 12); + + /* + // flush pts + for i in (ROOT_PT_PA..SCRATCH_PAGE).step_by(512 / 8) { + unsafe { flush_block(i) }; + } + for i in (0x8000_0000..0x8000_2000).step_by(512 / 8) { + unsafe { flush_block(i) }; + } + */ + // flush stack + for i in (0x4001_d000..0x4001_f000).step_by(512 / 8) { + // unsafe { flush_block(i) }; + unsafe { + core::arch::asm!( + ".word 0x0025200f", + in("a0") i, + options(nostack), + ); + } + } + + // crate::println!("vmem pivot"); + unsafe { + core::arch::asm!( + "fence", + "fence.i", + + // Install a machine mode trap handler + "la t0, abort_vii", + "csrw mtvec, t0", + // "csrw utvec, t0", + "csrw stvec, t0", + + // Delegate as much as we can supervisor mode + "li t0, 0xffffffff", + "csrw mideleg, t0", + "csrw medeleg, t0", + + // Return to Supervisor mode (1 << 11) when we call `reti`. + // Disable interrupts (0 << 5), allow supervisor mode to run user mode code (1 << 18) + "li t0, (0 << 5) | (1 << 11)", // | (1 << 18) + "csrw mstatus, t0", + + // Enable the MMU (once we issue `mret`) and flush the cache + "csrw satp, {satp_val}", + "sfence.vma", + /* + ".word 0x500F", + "nop", + "nop", + "nop", + "nop", + "fence", + "nop", + "nop", + "nop", + "nop", + "sfence.vma", + ".word 0x500F", + "nop", + "nop", + "nop", + "nop", + "fence", + "nop", + "nop", + "nop", + "nop", + */ + satp_val = in(reg) satp, + ); + core::arch::asm!( + // When loading with GDB we don't use a VM offset so GDB is less confused + // "li t0, 0x20000000", + "li t0, 0x00000000", + ); + core::arch::asm!( + "add a4, ra, t0", + "csrw mepc, a4", + // sp "shouldn't move" because the mapping will take RAM mapping as 1:1 for VA:PA + + // Issue the return, which will jump to $mepc in the specified mode in mstatus + "mret", + ); + } +} + +#[inline(never)] // correct behavior depends on RA being set. +pub fn pivot_user() { + unsafe { + core::arch::asm!( + // Disable interrupts (0 << 5) + "li t0, (0 << 5)", + "csrw sstatus, t0", + // Enable the MMU (once we issue `mret`) and flush the cache + "csrw sepc, ra", + // Issue the return, which will jump to $mepc in the specified mode in mstatus + "sret", + ); + } +} +/* +#[target_feature(enable = "zicbom")] +pub unsafe fn flush_block(addr: usize) { + core::arch::asm!( + "cbo.flush 0({addr})", + addr = in(reg) addr, + ) +} +*/ +pub unsafe fn flush_block(addr: usize) { + core::arch::asm!( + "mv a0, {addr}", + ".word 0x0025200f", /* cbo.flush 0(a0) */ + addr = in(reg) addr, + out("a0") _, // clobber a0 + ); +} + +/* +#define XENVCFG_CBIE_OK 0x30 +#define XENVCFG_CBCFE_OK 0x40 + li x1, XENVCFG_CBCFE_OK | XENVCFG_CBIE_OK + csrw 0x30a, x1 // Allow supervisor + csrw 0x10a, x1 // Allow user +*/ + +pub const XENVCFG_CBIE_OK: usize = 0x30; +pub const XENVCFG_CBCFE_OK: usize = 0x40; + +pub unsafe fn config_flush() { + core::arch::asm!( + "li t0, 0x30 | 0x40", + "csrw 0x30a, t0", // allow supervisor + "csrw 0x10a, t0", // allow user + out("t0") _, // clobber t0 + ) +} + +#[link_section = ".text.init"] +#[export_name = "abort_vii"] +/// This is only used in debug mode +pub extern "C" fn abort_vii() -> ! { + unsafe { + #[rustfmt::skip] + core::arch::asm!( + "li t0, 0xe0000800", + "li t1, 0x00f", + "li t2, 0xf00", + "300:", // abort + "sw t1, 0(t0)", + "sw t2, 0(t0)", + "j 300b", + options(noreturn) + ); + } +} diff --git a/baremetal/src/platform/artyvexii/irq.rs b/baremetal/src/platform/artyvexii/irq.rs index 3ba89a869..e729f65a8 100644 --- a/baremetal/src/platform/artyvexii/irq.rs +++ b/baremetal/src/platform/artyvexii/irq.rs @@ -44,7 +44,7 @@ pub unsafe extern "C" fn _start_trap() -> ! { #[rustfmt::skip] core::arch::asm!( "csrw mscratch, sp", - "li sp, 0x4001E000", // crate::platform::SCRATCH_PAGE - has to be hard-coded + "li sp, 0x4000C000", // crate::platform::SCRATCH_PAGE - has to be hard-coded "sw x1, 0*4(sp)", // Skip SP for now "sw x3, 2*4(sp)", @@ -90,7 +90,7 @@ pub unsafe extern "C" fn _start_trap() -> ! { "csrr t0, mscratch", "sw t0, 1*4(sp)", // Restore a default stack pointer - "li sp, 0x40020000", // last page of RAM, grows down + "li sp, 0x4000E000", // last page of RAM, grows down // Note that registers $a0-$a7 still contain the arguments "j _start_trap_rust", diff --git a/baremetal/src/platform/artyvexii/link.x b/baremetal/src/platform/artyvexii/link.x index abbe8008e..bd9eb9db5 100644 --- a/baremetal/src/platform/artyvexii/link.x +++ b/baremetal/src/platform/artyvexii/link.x @@ -1,7 +1,7 @@ MEMORY { FLASH : ORIGIN = 0x80000000, LENGTH = 64k - RAM : ORIGIN = 0x40000000, LENGTH = 128k + RAM : ORIGIN = 0x4000E000, LENGTH = 128k } REGION_ALIAS("REGION_TEXT", FLASH); diff --git a/baremetal/src/platform/artyvexii/ramtests.rs b/baremetal/src/platform/artyvexii/ramtests.rs new file mode 100644 index 000000000..f80f0840e --- /dev/null +++ b/baremetal/src/platform/artyvexii/ramtests.rs @@ -0,0 +1,304 @@ +#![allow(dead_code)] + +use core::convert::TryFrom; +use core::convert::TryInto; +use core::mem::size_of; + +use utralib::generated::*; + +use super::artyvexii::PT_LIMIT; + +pub fn ramtests() { + unsafe { caching_tests() }; + + const BASE_ADDR: u32 = PT_LIMIT as u32; + + unsafe { + crate::println!("bytestrobes"); + check_byte_strobes(); + + // 'random' access test + crate::println!("ra"); + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u32, 512); + ramtest_lfsr(&mut test_slice, 3); + + // now some basic memory read/write tests + // entirely within cache access test + // 256-entry by 32-bit slice at start of RAM + crate::println!("basic r/w"); + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u32, 256); + ramtest_all(&mut test_slice, 4); + // byte access test + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u8, 256); + ramtest_fast(&mut test_slice, 5); + // word access test + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u16, 512); + ramtest_fast(&mut test_slice, 6); // 1ff00 + + // outside cache test + // 6144-entry by 32-bit slice at start of RAM - should cross outside cache boundary + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u32, 0x1800); + ramtest_fast(&mut test_slice, 7); // c7f600 + + // this passed, now that the AXI state machine is fixed. + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u32, 0x1800); + ramtest_fast_specialcase1(&mut test_slice, 8); // c7f600 + + // u64 access test + let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u64, 0xC00); + ramtest_fast(&mut test_slice, 9); + + // random size/access test + // let mut test_slice = core::slice::from_raw_parts_mut(BASE_ADDR as *mut u8, 0x6000); + } +} +pub unsafe fn check_byte_strobes() { + let u8_test = utra::rgb::HW_RGB_BASE as *mut u8; + let u16_test = utra::rgb::HW_RGB_BASE as *mut u16; + + // quick test to check byte and word write strobes on the + unsafe { + u8_test.write_volatile(0x31); + u8_test.add(1).write_volatile(32); + u8_test.add(2).write_volatile(33); + u8_test.add(3).write_volatile(34); + + u16_test.write_volatile(0x44); + u16_test.add(1).write_volatile(0x55); + } +} +pub unsafe fn caching_tests() -> usize { + // test of the 0x500F cache flush instruction - this requires manual inspection of the report values + const CACHE_WAYS: usize = 4; + const CACHE_SET_SIZE: usize = 4096 / size_of::(); + let test_slice = core::slice::from_raw_parts_mut(PT_LIMIT as *mut u32, CACHE_SET_SIZE * CACHE_WAYS); + crate::println!("at {:x}", test_slice.as_ptr() as usize); + // bottom of cache + for set in 0..4 { + (&mut test_slice[set * CACHE_SET_SIZE] as *mut u32).write_volatile(0x0011_1111 * (1 + set as u32)); + flush_phys_one(unsafe { test_slice.as_ptr().add(set * CACHE_SET_SIZE) } as usize); + } + // top of cache + for set in 0..4 { + (&mut test_slice[set * CACHE_SET_SIZE + CACHE_SET_SIZE - 1] as *mut u32) + .write_volatile(0x1100_2222 * (1 + set as u32)); + flush_phys_one(unsafe { test_slice.as_ptr().add(set * CACHE_SET_SIZE + CACHE_SET_SIZE - 1) } as usize); + } + // read cached values - first iteration populates the cache; second iteration should be cached + for _iter in 0..2 { + for set in 0..4 { + let a = (&mut test_slice[set * CACHE_SET_SIZE] as *mut u32).read_volatile(); + crate::println!("a: {:x}", a); + let b = (&mut test_slice[set * CACHE_SET_SIZE + CACHE_SET_SIZE - 1] as *mut u32).read_volatile(); + crate::println!("b: {:x}", b); + } + } + // flush cache + crate::println!("flush"); + // bottom of cache + for set in 0..4 { + flush_phys_one(unsafe { test_slice.as_ptr().add(set * CACHE_SET_SIZE) as usize }); + } + // top of cache + for set in 0..4 { + flush_phys_one(unsafe { + test_slice.as_ptr().add(set * CACHE_SET_SIZE + CACHE_SET_SIZE - 1) as usize + }); + } + + // read cached values - first iteration populates the cache; second iteration should be cached + for _iter in 0..2 { + for set in 0..4 { + let a = (&mut test_slice[set * CACHE_SET_SIZE] as *mut u32).read_volatile(); + crate::println!("a: {:x}", a); + let b = (&mut test_slice[set * CACHE_SET_SIZE + CACHE_SET_SIZE - 1] as *mut u32).read_volatile(); + crate::println!("b: {:x}", b); + } + } + + // check that caching is disabled for I/O regions + let mut csrtest = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32); + let mut passing = 1; + for i in 0..4 { + csrtest.wfo(utra::csrtest::WTEST_WTEST, i); + let val = csrtest.rf(utra::csrtest::RTEST_RTEST); + if val != i + 0x1000_0000 { + passing = 0; + } + } + crate::println!("caching tests: {}", passing); + passing +} + +/// chunks through the entire bank of data +pub unsafe fn ramtest_all(test_slice: &mut [T], test_index: u32) -> usize +where + T: TryFrom + TryInto + Default + Copy, +{ + let mut sum: u32 = 0; + for (index, d) in test_slice.iter_mut().enumerate() { + // Convert the element into a `u32`, failing + (d as *mut T).write_volatile(index.try_into().unwrap_or_default()); + sum += TryInto::::try_into(index).unwrap(); + } + let mut checksum: u32 = 0; + for d in test_slice.iter() { + let a = (d as *const T).read_volatile().try_into().unwrap_or_default(); + checksum += a; + // report_api(a); + } + + if sum == checksum { + crate::println!("ramtest {} passing: {:x}", test_index, checksum); + 1 + } else { + crate::println!("ramtest failing ({}): {:x} vs {:x}", test_index, checksum, sum); + 0 + } +} + +/// only touches two words on each cache line +/// this one tries to write the same word twice to two consecutive addresses +/// this causes the valid strobe to hit twice in a row. seems to pass. +pub unsafe fn ramtest_fast_specialcase1(test_slice: &mut [T], test_index: u32) -> usize +where + T: TryFrom + TryInto + Default + Copy, +{ + const CACHE_LINE_SIZE: usize = 32; + let mut sum: u32 = 0; + for (index, d) in test_slice.chunks_mut(CACHE_LINE_SIZE / size_of::()).enumerate() { + let idxp1 = index + 0; + // unroll the loop to force b2b writes + sum += TryInto::::try_into(index).unwrap(); + sum += TryInto::::try_into(idxp1).unwrap(); + // Convert the element into a `u32`, failing + (d.as_mut_ptr() as *mut T).write_volatile(index.try_into().unwrap_or_default()); + // Convert the element into a `u32`, failing + (d.as_mut_ptr().add(1) as *mut T).write_volatile(idxp1.try_into().unwrap_or_default()); + } + let mut checksum: u32 = 0; + for d in test_slice.chunks(CACHE_LINE_SIZE / size_of::()) { + checksum += (d.as_ptr() as *const T).read_volatile().try_into().unwrap_or_default(); + checksum += (d.as_ptr().add(1) as *const T).read_volatile().try_into().unwrap_or_default(); + } + + if sum == checksum { + crate::println!("specialcase passing ({}): {:x}", test_index, checksum); + 1 + } else { + crate::println!("specialcase failing ({}): {:x} vs {:x}", test_index, checksum, sum); + 0 + } +} + +/// only touches two words on each cache line +pub unsafe fn ramtest_fast(test_slice: &mut [T], test_index: u32) -> usize +where + T: TryFrom + TryInto + Default + Copy, +{ + const CACHE_LINE_SIZE: usize = 32; + let mut sum: u32 = 0; + for (index, d) in test_slice.chunks_mut(CACHE_LINE_SIZE / size_of::()).enumerate() { + let idxp1 = index + 1; + // unroll the loop to force b2b writes + sum += TryInto::::try_into(index).unwrap(); + sum += TryInto::::try_into(idxp1).unwrap(); + // Convert the element into a `u32`, failing + (d.as_mut_ptr() as *mut T).write_volatile(index.try_into().unwrap_or_default()); + // Convert the element into a `u32`, failing + (d.as_mut_ptr().add(1) as *mut T).write_volatile(idxp1.try_into().unwrap_or_default()); + } + let mut checksum: u32 = 0; + for d in test_slice.chunks(CACHE_LINE_SIZE / size_of::()) { + let a = (d.as_ptr() as *const T).read_volatile().try_into().unwrap_or_default(); + let b = (d.as_ptr().add(1) as *const T).read_volatile().try_into().unwrap_or_default(); + checksum = checksum + a + b; + // report_api(a); + // report_api(b); + } + + if sum == checksum { + crate::println!("fast passing ({}): {:x}", test_index, checksum); + 1 + } else { + crate::println!("fast failing ({}): {:x} vs {:x}", test_index, checksum, sum); + 0 + } +} + +/// uses an LFSR to cycle through "random" locations. The slice length +/// should equal the (LFSR period+1), so that we guarantee that each entry +/// is visited once. +pub unsafe fn ramtest_lfsr(test_slice: &mut [T], test_index: u32) -> usize +where + T: TryFrom + TryInto + Default + Copy, +{ + if test_slice.len() != 512 { + return 0; + } + let mut state: u16 = 1; + let mut sum: u32 = 0; + const MAX_STATES: usize = 511; + (&mut test_slice[0] as *mut T).write_volatile(0.try_into().unwrap_or_default()); // the 0 index is never written to by this, initialize it to 0 + for i in 0..MAX_STATES { + let wr_val = i * 3; + (&mut test_slice[state as usize] as *mut T).write_volatile(wr_val.try_into().unwrap_or_default()); + sum += wr_val as u32; + state = lfsr_next(state); + } + + // flush cache + crate::println!("cache flush"); + flush_phys_range( + test_slice.as_ptr() as usize + ..test_slice.as_ptr() as usize + test_slice.len() * core::mem::size_of::(), + ); + + // we should be able to just iterate in-order and sum all the values, and get the same thing back as above + let mut checksum: u32 = 0; + for d in test_slice.iter() { + let a = (d as *const T).read_volatile().try_into().unwrap_or_default(); + checksum += a; + // report_api(a); + } + + if sum == checksum { + crate::println!("lfsr passing ({}): {:x}", test_index, checksum); + 1 + } else { + crate::println!("lfsr failing ({}): {:x} vs {:x}", test_index, checksum, sum); + 0 + } +} + +#[inline(always)] +pub fn flush_phys_range(region: core::ops::Range) { + for i in region.step_by(512 / 8) { + // unsafe { flush_block(i) }; + unsafe { + core::arch::asm!( + ".word 0x0025200f", + in("a0") i, + options(nostack) + ); + } + } +} + +#[inline(always)] +pub fn flush_phys_one(phys_addr: usize) { + unsafe { + core::arch::asm!( + ".word 0x0025200f", + in("a0") phys_addr, + options(nostack) + ); + } +} + +/// our desired test length is 512 entries, so pick an LFSR with a period of 2^9-1... +pub fn lfsr_next(state: u16) -> u16 { + let bit = ((state >> 8) ^ (state >> 4)) & 1; + + ((state << 1) + bit) & 0x1_FF +}