Skip to main content

reset_test/
reset_test.rs

1//! Hardware test for [`Device::reset_shires`] and [`Device::reset_device`].
2//!
3//! Runs three steps in order:
4//!
5//! 1. **CM reset**: calls `reset_shires` for all present shires and verifies the
6//!    firmware returns a success response. The device node stays open.
7//! 2. **Post-CM-reset launch**: launches the supplied kernel to confirm the device
8//!    is still usable after a compute-minion reset.
9//! 3. **Full ETSOC reset**: calls `reset_device`, which closes the device node and
10//!    polls until the firmware brings it back up.  Then launches the kernel once
11//!    more to confirm the freshly re-opened device is healthy.
12//!
13//! Usage:
14//! ```text
15//! cargo run --release --example reset_test -- \
16//!     et-k-rs/target/riscv64imac-unknown-none-elf/release/reduce-rs
17//! ```
18//!
19//! The `reduce-rs` kernel is a convenient, self-verifying payload: it sums a
20//! known array and the host confirms the total.  Any kernel that prints a
21//! RESULT PASS line will do.
22
23use std::process::ExitCode;
24
25use et_abi::{DeviceArgs, ReduceArgs};
26use et_soc1::{Device, LaunchOptions};
27
28fn main() -> ExitCode {
29    match run() {
30        Ok(()) => ExitCode::SUCCESS,
31        Err(e) => {
32            eprintln!("error: {e}");
33            ExitCode::FAILURE
34        }
35    }
36}
37
38fn run() -> et_soc1::Result<()> {
39    let kernel_path = std::env::args().nth(1).unwrap_or_else(|| {
40        eprintln!("usage: reset_test <path-to-reduce-rs>");
41        std::process::exit(1);
42    });
43    let elf = std::fs::read(&kernel_path).map_err(|e| et_soc1::Error::io("read kernel ELF", e))?;
44
45    // --- Step 1: open and query topology ---------------------------------
46    let device = Device::open(0)?;
47    let topo = device.topology()?;
48    // Use all shires so that n_harts equals the number of output slots the
49    // kernel writes. Launching on first_shire() with n_harts = num_harts()
50    // (total across all shires) would leave most slots uninitialized, giving
51    // a corrupted reduction sum.
52    let shire_mask = topo.shire_mask;
53    let n_harts = topo.num_harts();
54    println!(
55        "device: {} shire(s) present (mask {:#x}), {} harts/shire",
56        topo.num_shires(),
57        topo.shire_mask,
58        topo.harts_per_shire
59    );
60
61    // --- Step 2: CM reset (does not close the device node) ---------------
62    println!("\nStep 1: reset_shires({:#x}) ...", topo.shire_mask);
63    device.reset_shires(topo.shire_mask)?;
64    println!("  CM reset acknowledged by firmware -- OK");
65
66    // --- Step 3: launch to confirm the device is still usable ------------
67    println!("\nStep 2: kernel launch after CM reset ...");
68    let kernel = device.load_kernel(&elf)?;
69    launch_reduce(&device, &kernel, shire_mask, n_harts)?;
70    println!("  post-CM-reset launch -- PASS");
71
72    // --- Step 4: full ETSOC reset (consumes device, closes fd) -----------
73    println!("\nStep 3: reset_device() (full ETSOC reset) ...");
74    let device = device.reset_device()?;
75    println!("  device re-opened after ETSOC reset -- OK");
76
77    // --- Step 5: launch on the fresh device to confirm health ------------
78    println!("\nStep 4: kernel launch on re-opened device ...");
79    let topo2 = device.topology()?;
80    let shire_mask2 = topo2.shire_mask;
81    let n_harts2 = topo2.num_harts();
82    let kernel2 = device.load_kernel(&elf)?;
83    launch_reduce(&device, &kernel2, shire_mask2, n_harts2)?;
84    println!("  post-ETSOC-reset launch -- PASS");
85
86    println!("\nreset_test PASSED");
87    Ok(())
88}
89
90/// Launch one reduction pass on `shire_mask` / `n_harts` and verify the result.
91fn launch_reduce(
92    device: &Device<et_soc1::transport::IoctlTransport>,
93    kernel: &et_soc1::LoadedKernel,
94    shire_mask: u64,
95    n_harts: u32,
96) -> et_soc1::Result<()> {
97    const N: u32 = 4096;
98    // The reduce-rs kernel reads the input as &[u32], so upload u32 values.
99    // The expected total is sum(0..N) regardless of whether i64 or u32 is used,
100    // but the partial values written by the kernel (out[h] = sum of the h-th
101    // slice of u32 elements) must be consistent with what the kernel computes.
102    let input: Vec<u32> = (0..N).collect();
103    let expected: i64 = (0..N as i64).sum();
104
105    let buf = device.upload(&input)?;
106    let partials = device.alloc_padded::<i64>(n_harts as usize)?;
107
108    let args = ReduceArgs {
109        input: buf.addr(),
110        out: partials.addr(),
111        n: N,
112        n_harts,
113    };
114    let opts = LaunchOptions::new(shire_mask).with_args(args.as_bytes().to_vec());
115    device.launch(kernel, &opts)?;
116
117    let result: Vec<i64> = device.download_padded(&partials)?;
118    let total: i64 = result.iter().copied().take(n_harts as usize).sum();
119    if total != expected {
120        return Err(et_soc1::Error::Protocol(format!(
121            "reduction mismatch: got {total}, expected {expected}"
122        )));
123    }
124    println!("  reduction over {N} elements: total = {total} (correct)");
125    Ok(())
126}