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 base: kernel::io::Region<END>; 27 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<'bound> = pci::Bar<'bound, { 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 struct SampleDriverData<'bound> { 70 pdev: &'bound pci::Device, 71 bar: Bar0<'bound>, 72 index: TestIndex, 73 } 74 75 struct SampleDriver; 76 77 kernel::pci_device_table!( 78 PCI_TABLE, 79 <SampleDriver as pci::Driver>::IdInfo, 80 [( 81 pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), 82 TestIndex::NO_EVENTFD 83 )] 84 ); 85 86 impl SampleDriverData<'_> { 87 fn testdev(index: &TestIndex, bar: &Bar0<'_>) -> Result<u32> { 88 // Select the test. 89 bar.write_reg(regs::TEST::zeroed().with_index(*index)); 90 91 let offset = bar.read(regs::OFFSET).into_raw() as usize; 92 let data = bar.read(regs::DATA).into(); 93 94 // Write `data` to `offset` to increase `count` by one. 95 // 96 // Note that we need `try_write8`, since `offset` can't be checked at compile-time. 97 bar.try_write8(data, offset)?; 98 99 Ok(bar.read(regs::COUNT).into()) 100 } 101 102 fn config_space(pdev: &pci::Device<Bound>) { 103 let config = pdev.config_space(); 104 105 // Some PCI configuration space registers. 106 register! { 107 base: pci::Normal; 108 109 VENDOR_ID(u16) @ 0x0 { 110 15:0 vendor_id; 111 } 112 113 REVISION_ID(u8) @ 0x8 { 114 7:0 revision_id; 115 } 116 117 BAR(u32)[6] @ 0x10 { 118 31:0 value; 119 } 120 } 121 122 dev_info!( 123 pdev, 124 "pci-testdev config space read8 rev ID: {:x}\n", 125 config.read(REVISION_ID).revision_id() 126 ); 127 128 dev_info!( 129 pdev, 130 "pci-testdev config space read16 vendor ID: {:x}\n", 131 config.read(VENDOR_ID).vendor_id() 132 ); 133 134 dev_info!( 135 pdev, 136 "pci-testdev config space read32 BAR 0: {:x}\n", 137 config.read(BAR::at(0)).value() 138 ); 139 } 140 } 141 142 impl pci::Driver for SampleDriver { 143 type IdInfo = TestIndex; 144 type Data<'bound> = SampleDriverData<'bound>; 145 146 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE; 147 148 fn probe<'bound>( 149 pdev: &'bound pci::Device<Core<'_>>, 150 info: Option<&'bound Self::IdInfo>, 151 ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound { 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 let info = info.ok_or(ENODEV)?; 160 161 pdev.enable_device_mem()?; 162 pdev.set_master(); 163 164 let bar = pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci")?; 165 166 dev_info!( 167 pdev, 168 "pci-testdev data-match count: {}\n", 169 SampleDriverData::testdev(info, &bar)? 170 ); 171 SampleDriverData::config_space(pdev); 172 173 Ok(SampleDriverData { 174 pdev, 175 bar, 176 index: *info, 177 }) 178 } 179 180 fn unbind<'bound>(_pdev: &'bound pci::Device<Core<'_>>, this: Pin<&Self::Data<'bound>>) { 181 this.bar 182 .write_reg(regs::TEST::zeroed().with_index(this.index)); 183 } 184 } 185 186 impl Drop for SampleDriverData<'_> { 187 fn drop(&mut self) { 188 dev_dbg!(self.pdev, "Remove Rust PCI driver sample.\n"); 189 } 190 } 191 192 kernel::module_pci_driver! { 193 type: SampleDriver, 194 name: "rust_driver_pci", 195 authors: ["Danilo Krummrich"], 196 description: "Rust PCI driver", 197 license: "GPL v2", 198 } 199