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