projects/linux·blob master · refs

194 lines · 4.4 KiB

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
7use 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
22mod 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
46type Bar0<'bound> = pci::Bar<'bound, { regs::END }>;
47
48#[derive(Copy, Clone, Debug)]
49struct TestIndex(u8);
50
51impl From<Bounded<u8, 8>> for TestIndex {
52 fn from(value: Bounded<u8, 8>) -> Self {
53 Self(value.into())
54 }
55}
56
57impl From<TestIndex> for Bounded<u8, 8> {
58 fn from(value: TestIndex) -> Self {
59 value.0.into()
60 }
61}
62
63impl TestIndex {
64 const NO_EVENTFD: Self = Self(0);
65}
66
67struct SampleDriverData<'bound> {
68 pdev: &'bound pci::Device,
69 bar: Bar0<'bound>,
70 index: TestIndex,
71}
72
73struct SampleDriver;
74
75kernel::pci_device_table!(
76 PCI_TABLE,
77 <SampleDriver as pci::Driver>::IdInfo,
78 [(
79 pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5),
80 TestIndex::NO_EVENTFD
81 )]
82);
83
84impl SampleDriverData<'_> {
85 fn testdev(index: &TestIndex, bar: &Bar0<'_>) -> Result<u32> {
86 // Select the test.
87 bar.write_reg(regs::TEST::zeroed().with_index(*index));
88
89 let offset = bar.read(regs::OFFSET).into_raw() as usize;
90 let data = bar.read(regs::DATA).into();
91
92 // Write `data` to `offset` to increase `count` by one.
93 //
94 // Note that we need `try_write8`, since `offset` can't be checked at compile-time.
95 bar.try_write8(data, offset)?;
96
97 Ok(bar.read(regs::COUNT).into())
98 }
99
100 fn config_space(pdev: &pci::Device<Bound>) {
101 let config = pdev.config_space();
102
103 // Some PCI configuration space registers.
104 register! {
105 VENDOR_ID(u16) @ 0x0 {
106 15:0 vendor_id;
107 }
108
109 REVISION_ID(u8) @ 0x8 {
110 7:0 revision_id;
111 }
112
113 BAR(u32)[6] @ 0x10 {
114 31:0 value;
115 }
116 }
117
118 dev_info!(
119 pdev,
120 "pci-testdev config space read8 rev ID: {:x}\n",
121 config.read(REVISION_ID).revision_id()
122 );
123
124 dev_info!(
125 pdev,
126 "pci-testdev config space read16 vendor ID: {:x}\n",
127 config.read(VENDOR_ID).vendor_id()
128 );
129
130 dev_info!(
131 pdev,
132 "pci-testdev config space read32 BAR 0: {:x}\n",
133 config.read(BAR::at(0)).value()
134 );
135 }
136}
137
138impl pci::Driver for SampleDriver {
139 type IdInfo = TestIndex;
140 type Data<'bound> = SampleDriverData<'bound>;
141
142 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
143
144 fn probe<'bound>(
145 pdev: &'bound pci::Device<Core<'_>>,
146 info: Option<&'bound Self::IdInfo>,
147 ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
148 let vendor = pdev.vendor_id();
149 dev_dbg!(
150 pdev,
151 "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
152 vendor,
153 pdev.device_id()
154 );
155 let info = info.ok_or(ENODEV)?;
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
182impl Drop for SampleDriverData<'_> {
183 fn drop(&mut self) {
184 dev_dbg!(self.pdev, "Remove Rust PCI driver sample.\n");
185 }
186}
187
188kernel::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}