Skip to content

Commit d6146e2

Browse files
author
Danilo Krummrich
committed
samples: rust: add Rust PCI sample driver
This commit adds a sample Rust PCI driver for QEMU's "pci-testdev" device. To enable this device QEMU has to be called with `-device pci-testdev`. The same driver shows how to use the PCI device / driver abstractions, as well as how to request and map PCI BARs, including a short sequence of MMIO operations. Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 0fdb57c commit d6146e2

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

samples/rust/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ config SAMPLE_RUST_PRINT
3030

3131
If unsure, say N.
3232

33+
config SAMPLE_RUST_DRIVER_PCI
34+
tristate "PCI Driver"
35+
help
36+
This option builds the Rust PCI driver sample.
37+
38+
To compile this as a module, choose M here:
39+
the module will be called driver_pci.
40+
41+
If unsure, say N.
42+
3343
config SAMPLE_RUST_HOSTPROGS
3444
bool "Host programs"
3545
help

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
44
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
5+
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
56

67
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/rust_driver_pci.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust PCI driver sample (based on QEMU's `pci-testdev`).
4+
//!
5+
//! To make this driver probe, QEMU must be run with `-device pci-testdev`.
6+
7+
use kernel::{bindings, c_str, device_id::IdArray, devres::Devres, pci, prelude::*};
8+
9+
struct Regs;
10+
11+
impl Regs {
12+
const TEST: usize = 0x0;
13+
const OFFSET: usize = 0x4;
14+
const DATA: usize = 0x8;
15+
const COUNT: usize = 0xC;
16+
const END: usize = 0x10;
17+
}
18+
19+
type Bar0 = pci::Bar<{ Regs::END }>;
20+
21+
#[derive(Debug)]
22+
struct TestIndex(u8);
23+
24+
impl TestIndex {
25+
const NO_EVENTFD: Self = Self(0);
26+
}
27+
28+
struct SampleDriver {
29+
pdev: pci::Device,
30+
bar: Devres<Bar0>,
31+
}
32+
33+
impl SampleDriver {
34+
fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
35+
// Select the test.
36+
bar.writeb(index.0, Regs::TEST);
37+
38+
let offset = u32::from_le(bar.readl(Regs::OFFSET)) as usize;
39+
let data = bar.readb(Regs::DATA);
40+
41+
// Write `data` to `offset` to increase `count` by one.
42+
//
43+
// Note that we need `try_writeb`, since `offset` can't be checked at compile-time.
44+
bar.try_writeb(data, offset)?;
45+
46+
Ok(u32::from_le(bar.readl(Regs::COUNT)))
47+
}
48+
}
49+
50+
impl pci::Driver for SampleDriver {
51+
type IdInfo = TestIndex;
52+
53+
const ID_TABLE: pci::IdTable<Self::IdInfo> = &IdArray::new([(
54+
pci::DeviceId::new(bindings::PCI_VENDOR_ID_REDHAT, 0x5),
55+
TestIndex::NO_EVENTFD,
56+
)]);
57+
58+
fn probe(
59+
pdev: &mut pci::Device,
60+
_id: &pci::DeviceId,
61+
info: &Self::IdInfo,
62+
) -> Result<Pin<KBox<Self>>> {
63+
dev_dbg!(pdev.as_ref(), "Probe Rust PCI driver sample.\n");
64+
65+
pdev.enable_device_mem()?;
66+
pdev.set_master();
67+
68+
let bar = pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci"))?;
69+
70+
let drvdata = KBox::new(
71+
Self {
72+
pdev: pdev.clone(),
73+
bar,
74+
},
75+
GFP_KERNEL,
76+
)?;
77+
78+
let bar = drvdata.bar.try_access().ok_or(ENXIO)?;
79+
80+
dev_info!(
81+
pdev.as_ref(),
82+
"pci-testdev data-match count: {}\n",
83+
Self::testdev(info, &bar)?
84+
);
85+
86+
Ok(drvdata.into())
87+
}
88+
}
89+
90+
impl Drop for SampleDriver {
91+
fn drop(&mut self) {
92+
dev_dbg!(self.pdev.as_ref(), "Remove Rust PCI driver sample.\n");
93+
}
94+
}
95+
96+
kernel::module_pci_driver! {
97+
type: SampleDriver,
98+
name: "rust_driver_pci",
99+
author: "Danilo Krummrich",
100+
description: "Rust PCI driver",
101+
license: "GPL v2",
102+
}

0 commit comments

Comments
 (0)