xref: /linux/samples/rust/rust_driver_pci.rs (revision 04b325fb3456df0ffa7d6a8ac5cb4f94b860b575)
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::{
8     device::{
9         Bound,
10         Core, //
11     },
12     io::{
13         register,
14         register::Array,
15         Io, //
16     },
17     num::Bounded,
18     pci,
19     prelude::*, //
20 };
21 
22 mod regs {
23     use super::*;
24 
25     register! {
26         pub(super) TEST(u8) @ 0x0 {
27             7:0 index => TestIndex;
28         }
29 
30         pub(super) OFFSET(u32) @ 0x4 {
31             31:0 offset;
32         }
33 
34         pub(super) DATA(u8) @ 0x8 {
35             7:0 data;
36         }
37 
38         pub(super) COUNT(u32) @ 0xC {
39             31:0 count;
40         }
41     }
42 
43     pub(super) const END: usize = 0x10;
44 }
45 
46 type Bar0<'bound> = pci::Bar<'bound, { regs::END }>;
47 
48 #[derive(Copy, Clone, Debug)]
49 struct TestIndex(u8);
50 
51 impl From<Bounded<u8, 8>> for TestIndex {
52     fn from(value: Bounded<u8, 8>) -> Self {
53         Self(value.into())
54     }
55 }
56 
57 impl From<TestIndex> for Bounded<u8, 8> {
58     fn from(value: TestIndex) -> Self {
59         value.0.into()
60     }
61 }
62 
63 impl TestIndex {
64     const NO_EVENTFD: Self = Self(0);
65 }
66 
67 struct SampleDriverData<'bound> {
68     pdev: &'bound pci::Device,
69     bar: Bar0<'bound>,
70     index: TestIndex,
71 }
72 
73 struct SampleDriver;
74 
75 kernel::pci_device_table!(
76     PCI_TABLE,
77     MODULE_PCI_TABLE,
78     <SampleDriver as pci::Driver>::IdInfo,
79     [(
80         pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5),
81         TestIndex::NO_EVENTFD
82     )]
83 );
84 
85 impl SampleDriverData<'_> {
86     fn testdev(index: &TestIndex, bar: &Bar0<'_>) -> Result<u32> {
87         // Select the test.
88         bar.write_reg(regs::TEST::zeroed().with_index(*index));
89 
90         let offset = bar.read(regs::OFFSET).into_raw() as usize;
91         let data = bar.read(regs::DATA).into();
92 
93         // Write `data` to `offset` to increase `count` by one.
94         //
95         // Note that we need `try_write8`, since `offset` can't be checked at compile-time.
96         bar.try_write8(data, offset)?;
97 
98         Ok(bar.read(regs::COUNT).into())
99     }
100 
101     fn config_space(pdev: &pci::Device<Bound>) {
102         let config = pdev.config_space();
103 
104         // Some PCI configuration space registers.
105         register! {
106             VENDOR_ID(u16) @ 0x0 {
107                 15:0 vendor_id;
108             }
109 
110             REVISION_ID(u8) @ 0x8 {
111                 7:0 revision_id;
112             }
113 
114             BAR(u32)[6] @ 0x10 {
115                 31:0 value;
116             }
117         }
118 
119         dev_info!(
120             pdev,
121             "pci-testdev config space read8 rev ID: {:x}\n",
122             config.read(REVISION_ID).revision_id()
123         );
124 
125         dev_info!(
126             pdev,
127             "pci-testdev config space read16 vendor ID: {:x}\n",
128             config.read(VENDOR_ID).vendor_id()
129         );
130 
131         dev_info!(
132             pdev,
133             "pci-testdev config space read32 BAR 0: {:x}\n",
134             config.read(BAR::at(0)).value()
135         );
136     }
137 }
138 
139 impl pci::Driver for SampleDriver {
140     type IdInfo = TestIndex;
141     type Data<'bound> = SampleDriverData<'bound>;
142 
143     const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
144 
145     fn probe<'bound>(
146         pdev: &'bound pci::Device<Core<'_>>,
147         info: &'bound Self::IdInfo,
148     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
149         let vendor = pdev.vendor_id();
150         dev_dbg!(
151             pdev,
152             "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
153             vendor,
154             pdev.device_id()
155         );
156 
157         pdev.enable_device_mem()?;
158         pdev.set_master();
159 
160         let bar = pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci")?;
161 
162         dev_info!(
163             pdev,
164             "pci-testdev data-match count: {}\n",
165             SampleDriverData::testdev(info, &bar)?
166         );
167         SampleDriverData::config_space(pdev);
168 
169         Ok(SampleDriverData {
170             pdev,
171             bar,
172             index: *info,
173         })
174     }
175 
176     fn unbind<'bound>(_pdev: &'bound pci::Device<Core<'_>>, this: Pin<&Self::Data<'bound>>) {
177         this.bar
178             .write_reg(regs::TEST::zeroed().with_index(this.index));
179     }
180 }
181 
182 impl Drop for SampleDriverData<'_> {
183     fn drop(&mut self) {
184         dev_dbg!(self.pdev, "Remove Rust PCI driver sample.\n");
185     }
186 }
187 
188 kernel::module_pci_driver! {
189     type: SampleDriver,
190     name: "rust_driver_pci",
191     authors: ["Danilo Krummrich"],
192     description: "Rust PCI driver",
193     license: "GPL v2",
194 }
195