luwen 0.8.5

A high-level interface for Tenstorrent AI accelerators
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#![cfg(test)]

use serial_test::serial;

use luwen::api::{chip::ArcMsgOptions, ArcMsg, ArcMsgOk, ChipImpl, TypedArcMsg};
use luwen::kmd::PciDevice;

/// Test utilities for verifying PCI device register read/write operations
///
/// These tests verify:
/// - Aligned and unaligned 32-bit register reads/writes
/// - Block memory operations with different alignment offsets
/// - Data integrity through read-after-write verification
/// - Boundary conditions for register access
///
/// Note: These tests require physical hardware to run. By default, they are
/// annotated with #[ignore] to avoid false failures on systems without hardware.
/// To run all hardware tests:
///
///   cargo test --test read_write_test -- --ignored
///
/// The tests will automatically detect if compatible hardware is present;
/// if hardware is not found, the test will be skipped.
#[serial]
mod tests {
    use luwen::api::chip::HlComms;
    use luwen::pci::detect_chips_fallible;

    use super::*;

    // Common test fixture setup
    struct TestFixture {
        raw_device: PciDevice,
        aligned_addr: u32,
    }

    impl TestFixture {
        /// Sets up a test environment with a properly aligned memory address for testing
        /// Returns None if no suitable hardware is found, which will cause tests to be skipped
        fn setup() -> Option<Self> {
            for id in PciDevice::scan() {
                let raw_device = match PciDevice::open(id) {
                    Ok(device) => device,
                    Err(_) => continue,
                };

                let device = match luwen::pci::open(id) {
                    Ok(dev) => dev,
                    Err(_) => continue,
                };

                if let Some(wh) = device.as_wh() {
                    // Get SPI dump address from chip via ARC message
                    let dump_addr = if let Ok(result) = wh.arc_msg(ArcMsgOptions {
                        msg: ArcMsg::Typed(TypedArcMsg::GetSpiDumpAddr),
                        ..Default::default()
                    }) {
                        match result {
                            ArcMsgOk::Ok { rc: _, arg } => Some(arg),
                            ArcMsgOk::OkBuf(_) => unreachable!(),
                            ArcMsgOk::OkNoWait => None,
                        }
                    } else {
                        None
                    }
                    .unwrap();

                    // Translate to physical memory space
                    let csm_offset =
                        wh.arc_if.axi_translate("ARC_CSM.DATA[0]").unwrap().addr - 0x10000000_u64;

                    // Calculate test memory location
                    let addr = csm_offset + u64::from(dump_addr);

                    // Ensure 4-byte alignment for tests
                    let aligned_addr = (addr + 3) & !3;

                    return Some(TestFixture {
                        raw_device,
                        aligned_addr: u32::try_from(aligned_addr).unwrap_or(0),
                    });
                }
            }
            None
        }
    }

    /// Helper function to reset test area to a known pattern
    fn reset_test_area(fixture: &mut TestFixture, pattern: u32) {
        fixture
            .raw_device
            .write32(fixture.aligned_addr, pattern)
            .unwrap();

        fixture
            .raw_device
            .write32(fixture.aligned_addr + 4, pattern)
            .unwrap();
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    fn wormhole_test_aligned_register_access() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Test 1: Basic aligned write/read of 32-bit value
        fixture
            .raw_device
            .write32(fixture.aligned_addr, 0x0000_faca)
            .unwrap();
        let readback = fixture.raw_device.read32(fixture.aligned_addr).unwrap();
        assert_eq!(
            readback, 0x0000_faca,
            "Aligned read/write of 0xfaca failed, got 0x{readback:x}"
        );

        // Test 2: Aligned write/read with 32-bit pattern
        fixture
            .raw_device
            .write32(fixture.aligned_addr, 0xcdcd_cdcd)
            .unwrap();
        let readback = fixture.raw_device.read32(fixture.aligned_addr).unwrap();
        assert_eq!(
            readback, 0xcdcd_cdcd,
            "Aligned read/write of 0xcdcdcdcd failed, got 0x{readback:x}"
        );

        // Test 3: Aligned write/read at next word boundary
        fixture
            .raw_device
            .write32(fixture.aligned_addr + 4, 0xcdcd_cdcd)
            .unwrap();
        let readback = fixture.raw_device.read32(fixture.aligned_addr + 4).unwrap();
        assert_eq!(
            readback, 0xcdcd_cdcd,
            "Aligned read/write at next word boundary failed, got 0x{readback:x}"
        );
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    fn wormhole_test_unaligned_register_access() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Reset test area to known pattern
        reset_test_area(&mut fixture, 0xcdcd_cdcd);

        // Test 4: Unaligned write with byte offset +1
        fixture
            .raw_device
            .write32(fixture.aligned_addr + 1, 0xdead)
            .unwrap();

        // Verify cross-boundary effects on adjacent words
        let readback = fixture.raw_device.read32(fixture.aligned_addr).unwrap();
        assert_eq!(
            readback, 0xdeadcd,
            "Unaligned write +1 effect on current word failed, got 0x{readback:x}"
        );

        let readback = fixture.raw_device.read32(fixture.aligned_addr + 4).unwrap();
        assert_eq!(
            readback, 0xcdcdcd00,
            "Unaligned write +1 effect on next word failed, got 0x{readback:x}"
        );

        // Reset test area to known pattern
        reset_test_area(&mut fixture, 0xcdcd_cdcd);

        // Test 5: Unaligned write with byte offset +3 (word boundary -1)
        fixture
            .raw_device
            .write32(fixture.aligned_addr + 3, 0xc0ffe)
            .unwrap();

        // Verify cross-boundary effects
        let readback = fixture.raw_device.read32(fixture.aligned_addr).unwrap();
        assert_eq!(
            readback, 0xfecdcdcd,
            "Unaligned write +3 effect on current word failed, got 0x{readback:x}"
        );

        let readback = fixture.raw_device.read32(fixture.aligned_addr + 4).unwrap();
        assert_eq!(
            readback, 0xcd000c0f,
            "Unaligned write +3 effect on next word failed, got 0x{readback:x}"
        );
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    fn wormhole_test_sequential_pattern_reads() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Test 6: Write sequential pattern for readback tests
        fixture
            .raw_device
            .write32(fixture.aligned_addr, 0x01234567)
            .unwrap();
        let readback = fixture.raw_device.read32(fixture.aligned_addr).unwrap();
        assert_eq!(
            readback, 0x01234567,
            "Sequential pattern write/read failed, got 0x{readback:x}"
        );

        // Write to adjacent word
        fixture
            .raw_device
            .write32(fixture.aligned_addr + 4, 0xabcdef)
            .unwrap();
        let readback = fixture.raw_device.read32(fixture.aligned_addr + 4).unwrap();
        assert_eq!(
            readback, 0xabcdef,
            "Sequential pattern write/read at next word failed, got 0x{readback:x}"
        );

        // Test 7: Verify unaligned reads with sequential data
        // Read with +1 byte offset (crosses word boundary)
        let readback = fixture.raw_device.read32(fixture.aligned_addr + 1).unwrap();
        assert_eq!(
            readback, 0xef012345,
            "Unaligned read +1 with sequential pattern failed, got 0x{readback:x}"
        );

        // Read with +3 byte offset (crosses word boundary)
        let readback = fixture.raw_device.read32(fixture.aligned_addr + 3).unwrap();
        assert_eq!(
            readback, 0xabcdef01,
            "Unaligned read +3 with sequential pattern failed, got 0x{readback:x}"
        );
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    fn wormhole_test_aligned_block_operations() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Test 8: Block write/read with aligned address
        let mut write_buffer = Vec::new();
        write_buffer.extend(0xcdcd_cdcdu32.to_le_bytes());
        write_buffer.extend(0xcdcd_cdcdu32.to_le_bytes());

        fixture
            .raw_device
            .write_block(fixture.aligned_addr, &write_buffer)
            .unwrap();

        // Verify block read matches written data
        let mut readback_buffer = vec![0u8; write_buffer.len()];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            write_buffer, readback_buffer,
            "Aligned block write/read failed"
        );
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    fn wormhole_test_unaligned_block_operations() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Reset test area
        let mut reset_buffer = Vec::new();
        reset_buffer.extend(0xcdcd_cdcdu32.to_le_bytes());
        reset_buffer.extend(0xcdcd_cdcdu32.to_le_bytes());

        fixture
            .raw_device
            .write_block(fixture.aligned_addr, &reset_buffer)
            .unwrap();

        // Test 9: Unaligned block write with 2-byte data at offset +1
        let write_buffer = vec![0xad, 0xde];
        fixture
            .raw_device
            .write_block(fixture.aligned_addr + 1, &write_buffer)
            .unwrap();

        // Read back full word to verify partial write behavior
        let mut readback_buffer = vec![0u8; 4];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            [0xcd, 0xad, 0xde, 0xcd],
            readback_buffer.as_slice(),
            "Unaligned block write with offset +1 failed"
        );

        // Reset test area
        fixture
            .raw_device
            .write_block(fixture.aligned_addr, &reset_buffer)
            .unwrap();

        // Test 10: Unaligned block write at word boundary-1 (offset +3)
        let write_buffer = vec![0xad, 0xde];
        fixture
            .raw_device
            .write_block(fixture.aligned_addr + 3, &write_buffer)
            .unwrap();

        // Read extended range to verify cross-boundary behavior
        let mut readback_buffer = vec![0u8; 7];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            [0xcd, 0xcd, 0xcd, 0xad, 0xde, 0xcd, 0xcd],
            readback_buffer.as_slice(),
            "Unaligned block write with offset +3 failed"
        );
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    fn wormhole_test_sequential_pattern_block_operations() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Test 11: Block write with sequential pattern for boundary tests
        let mut write_buffer = Vec::new();
        write_buffer.extend(0x01234567u32.to_le_bytes());
        write_buffer.extend(0xabcdefu32.to_le_bytes());

        fixture
            .raw_device
            .write_block(fixture.aligned_addr, &write_buffer)
            .unwrap();

        // Verify block read with sequential pattern
        let mut readback_buffer = vec![0u8; write_buffer.len()];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            write_buffer, readback_buffer,
            "Sequential pattern block write/read failed"
        );

        // Test 12: Verify 32-bit register reads match block reads
        let reg_readback = fixture.raw_device.read32(fixture.aligned_addr + 1).unwrap();
        assert_eq!(
            reg_readback, 0xef012345,
            "Register read at +1 doesn't match expected pattern, got 0x{reg_readback:x}"
        );

        // Verify unaligned block read at +1 offset
        let mut readback_buffer = vec![0u8; 4];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr + 1, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            [0x45, 0x23, 0x01, 0xef],
            readback_buffer.as_slice(),
            "Unaligned block read at +1 failed"
        );

        // Verify unaligned block read at +3 offset
        let mut readback_buffer = vec![0u8; 4];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr + 3, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            [0x01, 0xef, 0xcd, 0xab],
            readback_buffer.as_slice(),
            "Unaligned block read at +3 failed"
        );
    }

    #[test]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_wormhole")),
        ignore = "Requires real wormhole hardware"
    )]
    #[allow(clippy::cast_possible_truncation)]
    fn wormhole_test_large_block_transfers() {
        let mut fixture = TestFixture::setup().expect("Hardware should be available");

        // Test 13: Larger block transfers (1KB) with sequential pattern
        let mut write_buffer = vec![0; 1024];
        for (index, r) in write_buffer.iter_mut().enumerate() {
            // The modulo ensures we never exceed u8 range
            let value = index % 256;
            // Safe cast: we've ensured the value is < 256, which fits in u8
            *r = value as u8;
        }

        fixture
            .raw_device
            .write_block(fixture.aligned_addr, &write_buffer)
            .unwrap();

        // Verify large block read with aligned address
        let mut readback_buffer = vec![0u8; write_buffer.len()];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            write_buffer, readback_buffer,
            "Large (1KB) aligned block transfer failed"
        );

        // Test 14: Large block read with unaligned address (+3)
        fixture
            .raw_device
            .write_block(fixture.aligned_addr, &write_buffer)
            .unwrap();

        // Verify partial data matching with offset consideration
        let mut readback_buffer = vec![0u8; write_buffer.len()];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr + 3, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            write_buffer[3..],
            readback_buffer[..readback_buffer.len() - 3],
            "Large block read with offset +3 failed"
        );

        // Test 15: Large block write with unaligned address
        let mut write_buffer = vec![0; 1024];
        for (index, r) in write_buffer.iter_mut().enumerate() {
            // The modulo ensures we never exceed u8 range
            let value = index % 256;
            // Safe cast: we've ensured the value is < 256, which fits in u8
            *r = value as u8;
        }

        fixture
            .raw_device
            .write_block(fixture.aligned_addr + 1, &write_buffer)
            .unwrap();

        // Verify large block read from same unaligned address
        let mut readback_buffer = vec![0u8; write_buffer.len()];
        fixture
            .raw_device
            .read_block(fixture.aligned_addr + 1, &mut readback_buffer)
            .unwrap();

        assert_eq!(
            write_buffer, readback_buffer,
            "Large block transfer with offset +1 failed"
        );
    }

    #[test]
    #[cfg_attr(
        any(feature = "test_hardware", feature = "test_blackhole"),
        ignore = "Test has an issue. Do not run."
    )]
    #[cfg_attr(
        not(all(feature = "test_hardware", feature = "test_blackhole")),
        ignore = "Requires real blackhole hardware"
    )]
    #[allow(clippy::cast_possible_truncation)]
    fn blackhole_test_large_block_transfers_broadcast() {
        let bh = detect_chips_fallible().expect("need to be able to talk to the chips");
        let bh = bh
            .iter()
            .filter_map(|chip| chip.try_upgrade())
            .filter_map(|chip| chip.as_bh())
            .next()
            .expect("one working BH chip");

        let mut write_buffer = vec![0; 1024];
        for (index, r) in write_buffer.iter_mut().enumerate() {
            // The modulo ensures we never exceed u8 range
            let value = index % 256;
            // Safe cast: we've ensured the value is < 256, which fits in u8
            *r = value as u8;
        }

        let telem = bh
            .get_telemetry()
            .expect("need to be able to fetch telemetry");

        let mut all_tensix = Vec::new();
        for y in 2..=11 {
            for x in 1..=7 {
                all_tensix.push((x, y));
            }

            for x in 10..=16 {
                all_tensix.push((x, y));
            }
        }

        let working_cores = if telem.noc_translation_enabled {
            bh.noc_multicast(0, (2, 3), (1, 2), 0, &write_buffer)
                .expect("multicast to succeed");

            let mut working_tensix = Vec::with_capacity(all_tensix.len());

            let working_cols = telem.tensix_enabled_col.count_ones();
            for core in all_tensix {
                let x = core.0 as u32;
                if (x <= 7 && x < working_cols) || (x >= 10 && (x - 2) < working_cols) {
                    working_tensix.push(core);
                }
            }

            working_tensix
        } else {
            bh.noc_broadcast(0, 0, &write_buffer)
                .expect("broadcast to succeed");

            let mut working_col_bitmask = telem.tensix_enabled_col;

            let mut working_cols = Vec::new();
            let mut col = 0;
            let tensix_cols = [1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16];
            while working_col_bitmask != 0 {
                if working_col_bitmask & 0x1 != 0 {
                    working_cols.push(tensix_cols[col]);
                }
                working_col_bitmask >>= 1;
                col += 1;
            }

            let mut working_tensix = Vec::with_capacity(all_tensix.len());
            for core in all_tensix {
                if working_cols.contains(&core.0) {
                    working_tensix.push(core);
                }
            }

            working_tensix
        };

        for core in working_cores {
            println!("Checking core {core:?}");

            let mut readback_buffer = vec![0u8; write_buffer.len()];
            bh.noc_read(0, core.0, core.1, 0, &mut readback_buffer)
                .expect("readback to succeed");

            assert_eq!(
                write_buffer, readback_buffer,
                "Write to core {core:?} failed"
            );
        }
    }
}