xref: /linux/rust/kernel/pci.rs (revision 16c2b8fdab7c0808ff36430b2f49569029a8f484)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Abstractions for the PCI bus.
4 //!
5 //! C header: [`include/linux/pci.h`](srctree/include/linux/pci.h)
6 
7 use crate::{
8     bindings,
9     container_of,
10     device,
11     device_id::{
12         RawDeviceId,
13         RawDeviceIdIndex, //
14     },
15     driver,
16     error::{
17         from_result,
18         to_result, //
19     },
20     prelude::*,
21     str::CStr,
22     types::Opaque,
23     ThisModule, //
24 };
25 use core::{
26     marker::PhantomData,
27     mem::offset_of,
28     ptr::{
29         addr_of_mut,
30         NonNull, //
31     },
32 };
33 
34 mod id;
35 mod io;
36 mod irq;
37 
38 pub use self::id::{
39     Class,
40     ClassMask,
41     Vendor, //
42 };
43 pub use self::io::{
44     Bar,
45     ConfigSpace,
46     ConfigSpaceKind,
47     ConfigSpaceSize,
48     Extended,
49     Normal, //
50 };
51 pub use self::irq::{
52     IrqType,
53     IrqTypes,
54     IrqVector, //
55 };
56 
57 /// An adapter for the registration of PCI drivers.
58 pub struct Adapter<T: Driver>(T);
59 
60 // SAFETY:
61 // - `bindings::pci_driver` is a C type declared as `repr(C)`.
62 // - `T::Data` is the type of the driver's device private data.
63 // - `struct pci_driver` embeds a `struct device_driver`.
64 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
65 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
66     type DriverType = bindings::pci_driver;
67     type DriverData<'bound> = T::Data<'bound>;
68     const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
69 }
70 
71 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
72 // a preceding call to `register` has been successful.
73 unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
74     unsafe fn register(
75         pdrv: &Opaque<Self::DriverType>,
76         name: &'static CStr,
77         module: &'static ThisModule,
78     ) -> Result {
79         // SAFETY: It's safe to set the fields of `struct pci_driver` on initialization.
80         unsafe {
81             (*pdrv.get()).name = name.as_char_ptr();
82             (*pdrv.get()).probe = Some(Self::probe_callback);
83             (*pdrv.get()).remove = Some(Self::remove_callback);
84             (*pdrv.get()).id_table = T::ID_TABLE.as_ptr();
85         }
86 
87         // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
88         to_result(unsafe {
89             bindings::__pci_register_driver(pdrv.get(), module.0, name.as_char_ptr())
90         })
91     }
92 
93     unsafe fn unregister(pdrv: &Opaque<Self::DriverType>) {
94         // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
95         unsafe { bindings::pci_unregister_driver(pdrv.get()) }
96     }
97 }
98 
99 impl<T: Driver> Adapter<T> {
100     extern "C" fn probe_callback(
101         pdev: *mut bindings::pci_dev,
102         id: *const bindings::pci_device_id,
103     ) -> c_int {
104         // SAFETY: The PCI bus only ever calls the probe callback with a valid pointer to a
105         // `struct pci_dev`.
106         //
107         // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
108         let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
109 
110         // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct pci_device_id` and
111         // does not add additional invariants, so it's safe to transmute.
112         let id = unsafe { &*id.cast::<DeviceId>() };
113         let info = T::ID_TABLE.info(id.index());
114 
115         from_result(|| {
116             let data = T::probe(pdev, info);
117 
118             pdev.as_ref().set_drvdata(data)?;
119             Ok(0)
120         })
121     }
122 
123     extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) {
124         // SAFETY: The PCI bus only ever calls the remove callback with a valid pointer to a
125         // `struct pci_dev`.
126         //
127         // INVARIANT: `pdev` is valid for the duration of `remove_callback()`.
128         let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
129 
130         // SAFETY: `remove_callback` is only ever called after a successful call to
131         // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
132         // and stored a `Pin<KBox<T::Data<'_>>>`.
133         let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data<'_>>() };
134 
135         T::unbind(pdev, data);
136     }
137 }
138 
139 /// Declares a kernel module that exposes a single PCI driver.
140 ///
141 /// # Examples
142 ///
143 ///```ignore
144 /// kernel::module_pci_driver! {
145 ///     type: MyDriver,
146 ///     name: "Module name",
147 ///     authors: ["Author name"],
148 ///     description: "Description",
149 ///     license: "GPL v2",
150 /// }
151 ///```
152 #[macro_export]
153 macro_rules! module_pci_driver {
154 ($($f:tt)*) => {
155     $crate::module_driver!(<T>, $crate::pci::Adapter<T>, { $($f)* });
156 };
157 }
158 
159 /// Abstraction for the PCI device ID structure ([`struct pci_device_id`]).
160 ///
161 /// [`struct pci_device_id`]: https://docs.kernel.org/PCI/pci.html#c.pci_device_id
162 #[repr(transparent)]
163 #[derive(Clone, Copy)]
164 pub struct DeviceId(bindings::pci_device_id);
165 
166 impl DeviceId {
167     const PCI_ANY_ID: u32 = !0;
168 
169     /// Equivalent to C's `PCI_DEVICE` macro.
170     ///
171     /// Create a new `pci::DeviceId` from a vendor and device ID.
172     #[inline]
173     pub const fn from_id(vendor: Vendor, device: u32) -> Self {
174         Self(bindings::pci_device_id {
175             vendor: vendor.as_raw() as u32,
176             device,
177             subvendor: DeviceId::PCI_ANY_ID,
178             subdevice: DeviceId::PCI_ANY_ID,
179             class: 0,
180             class_mask: 0,
181             driver_data: 0,
182             override_only: 0,
183         })
184     }
185 
186     /// Equivalent to C's `PCI_DEVICE_CLASS` macro.
187     ///
188     /// Create a new `pci::DeviceId` from a class number and mask.
189     #[inline]
190     pub const fn from_class(class: u32, class_mask: u32) -> Self {
191         Self(bindings::pci_device_id {
192             vendor: DeviceId::PCI_ANY_ID,
193             device: DeviceId::PCI_ANY_ID,
194             subvendor: DeviceId::PCI_ANY_ID,
195             subdevice: DeviceId::PCI_ANY_ID,
196             class,
197             class_mask,
198             driver_data: 0,
199             override_only: 0,
200         })
201     }
202 
203     /// Create a new [`DeviceId`] from a class number, mask, and specific vendor.
204     ///
205     /// This is more targeted than [`DeviceId::from_class`]: in addition to matching by [`Vendor`],
206     /// it also matches the PCI [`Class`] (up to the entire 24 bits, depending on the
207     /// [`ClassMask`]).
208     #[inline]
209     pub const fn from_class_and_vendor(
210         class: Class,
211         class_mask: ClassMask,
212         vendor: Vendor,
213     ) -> Self {
214         Self(bindings::pci_device_id {
215             vendor: vendor.as_raw() as u32,
216             device: DeviceId::PCI_ANY_ID,
217             subvendor: DeviceId::PCI_ANY_ID,
218             subdevice: DeviceId::PCI_ANY_ID,
219             class: class.as_raw(),
220             class_mask: class_mask.as_raw(),
221             driver_data: 0,
222             override_only: 0,
223         })
224     }
225 }
226 
227 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and does not add
228 // additional invariants, so it's safe to transmute to `RawType`.
229 unsafe impl RawDeviceId for DeviceId {
230     type RawType = bindings::pci_device_id;
231 }
232 
233 // SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field.
234 unsafe impl RawDeviceIdIndex for DeviceId {
235     const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::pci_device_id, driver_data);
236 
237     fn index(&self) -> usize {
238         self.0.driver_data
239     }
240 }
241 
242 /// `IdTable` type for PCI.
243 pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
244 
245 /// Create a PCI `IdTable` with its alias for modpost.
246 #[macro_export]
247 macro_rules! pci_device_table {
248     ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
249         const $table_name: $crate::device_id::IdArray<
250             $crate::pci::DeviceId,
251             $id_info_type,
252             { $table_data.len() },
253         > = $crate::device_id::IdArray::new($table_data);
254 
255         $crate::module_device_table!("pci", $module_table_name, $table_name);
256     };
257 }
258 
259 /// The PCI driver trait.
260 ///
261 /// # Examples
262 ///
263 ///```
264 /// # use kernel::{bindings, device::Core, pci};
265 ///
266 /// struct MyDriver;
267 ///
268 /// kernel::pci_device_table!(
269 ///     PCI_TABLE,
270 ///     MODULE_PCI_TABLE,
271 ///     <MyDriver as pci::Driver>::IdInfo,
272 ///     [
273 ///         (
274 ///             pci::DeviceId::from_id(pci::Vendor::REDHAT, bindings::PCI_ANY_ID as u32),
275 ///             (),
276 ///         )
277 ///     ]
278 /// );
279 ///
280 /// impl pci::Driver for MyDriver {
281 ///     type IdInfo = ();
282 ///     type Data<'bound> = Self;
283 ///     const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
284 ///
285 ///     fn probe<'bound>(
286 ///         _pdev: &'bound pci::Device<Core<'_>>,
287 ///         _id_info: &'bound Self::IdInfo,
288 ///     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
289 ///         Err(ENODEV)
290 ///     }
291 /// }
292 ///```
293 /// Drivers must implement this trait in order to get a PCI driver registered. Please refer to the
294 /// `Adapter` documentation for an example.
295 pub trait Driver {
296     /// The type holding information about each device id supported by the driver.
297     // TODO: Use `associated_type_defaults` once stabilized:
298     //
299     // ```
300     // type IdInfo: 'static = ();
301     // ```
302     type IdInfo: 'static;
303 
304     /// The type of the driver's bus device private data.
305     type Data<'bound>: Send + 'bound;
306 
307     /// The table of device ids supported by the driver.
308     const ID_TABLE: IdTable<Self::IdInfo>;
309 
310     /// PCI driver probe.
311     ///
312     /// Called when a new pci device is added or discovered. Implementers should
313     /// attempt to initialize the device here.
314     fn probe<'bound>(
315         dev: &'bound Device<device::Core<'_>>,
316         id_info: &'bound Self::IdInfo,
317     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
318 
319     /// PCI driver unbind.
320     ///
321     /// Called when a [`Device`] is unbound from its bound [`Driver`]. Implementing this callback
322     /// is optional.
323     ///
324     /// This callback serves as a place for drivers to perform teardown operations that require a
325     /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
326     /// operations to gracefully tear down the device.
327     ///
328     /// Otherwise, release operations for driver resources should be performed in `Drop`.
329     fn unbind<'bound>(dev: &'bound Device<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
330         let _ = (dev, this);
331     }
332 }
333 
334 /// The PCI device representation.
335 ///
336 /// This structure represents the Rust abstraction for a C `struct pci_dev`. The implementation
337 /// abstracts the usage of an already existing C `struct pci_dev` within Rust code that we get
338 /// passed from the C side.
339 ///
340 /// # Invariants
341 ///
342 /// A [`Device`] instance represents a valid `struct pci_dev` created by the C portion of the
343 /// kernel.
344 #[repr(transparent)]
345 pub struct Device<Ctx: device::DeviceContext = device::Normal>(
346     Opaque<bindings::pci_dev>,
347     PhantomData<Ctx>,
348 );
349 
350 impl<Ctx: device::DeviceContext> Device<Ctx> {
351     #[inline]
352     fn as_raw(&self) -> *mut bindings::pci_dev {
353         self.0.get()
354     }
355 }
356 
357 impl Device {
358     /// Returns the PCI vendor ID as [`Vendor`].
359     ///
360     /// # Examples
361     ///
362     /// ```
363     /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*};
364     /// fn log_device_info(pdev: &pci::Device<Core<'_>>) -> Result {
365     ///     // Get an instance of `Vendor`.
366     ///     let vendor = pdev.vendor_id();
367     ///     dev_info!(
368     ///         pdev,
369     ///         "Device: Vendor={}, Device=0x{:x}\n",
370     ///         vendor,
371     ///         pdev.device_id()
372     ///     );
373     ///     Ok(())
374     /// }
375     /// ```
376     #[inline]
377     pub fn vendor_id(&self) -> Vendor {
378         // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
379         let vendor_id = unsafe { (*self.as_raw()).vendor };
380         Vendor::from_raw(vendor_id)
381     }
382 
383     /// Returns the PCI device ID.
384     #[inline]
385     pub fn device_id(&self) -> u16 {
386         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
387         // `struct pci_dev`.
388         unsafe { (*self.as_raw()).device }
389     }
390 
391     /// Returns the PCI revision ID.
392     #[inline]
393     pub fn revision_id(&self) -> u8 {
394         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
395         // `struct pci_dev`.
396         unsafe { (*self.as_raw()).revision }
397     }
398 
399     /// Returns the PCI bus device/function.
400     #[inline]
401     pub fn dev_id(&self) -> u16 {
402         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
403         // `struct pci_dev`.
404         unsafe { bindings::pci_dev_id(self.as_raw()) }
405     }
406 
407     /// Returns the PCI subsystem vendor ID.
408     #[inline]
409     pub fn subsystem_vendor_id(&self) -> u16 {
410         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
411         // `struct pci_dev`.
412         unsafe { (*self.as_raw()).subsystem_vendor }
413     }
414 
415     /// Returns the PCI subsystem device ID.
416     #[inline]
417     pub fn subsystem_device_id(&self) -> u16 {
418         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
419         // `struct pci_dev`.
420         unsafe { (*self.as_raw()).subsystem_device }
421     }
422 
423     /// Returns the start of the given PCI BAR resource.
424     pub fn resource_start(&self, bar: u32) -> Result<bindings::resource_size_t> {
425         if !Bar::index_is_valid(bar) {
426             return Err(EINVAL);
427         }
428 
429         // SAFETY:
430         // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`,
431         // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`.
432         Ok(unsafe { bindings::pci_resource_start(self.as_raw(), bar.try_into()?) })
433     }
434 
435     /// Returns the size of the given PCI BAR resource.
436     pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> {
437         if !Bar::index_is_valid(bar) {
438             return Err(EINVAL);
439         }
440 
441         // SAFETY:
442         // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`,
443         // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`.
444         Ok(unsafe { bindings::pci_resource_len(self.as_raw(), bar.try_into()?) })
445     }
446 
447     /// Returns the PCI class as a `Class` struct.
448     #[inline]
449     pub fn pci_class(&self) -> Class {
450         // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
451         Class::from_raw(unsafe { (*self.as_raw()).class })
452     }
453 }
454 
455 impl<'a> Device<device::Core<'a>> {
456     /// Enable memory resources for this device.
457     pub fn enable_device_mem(&self) -> Result {
458         // SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`.
459         to_result(unsafe { bindings::pci_enable_device_mem(self.as_raw()) })
460     }
461 
462     /// Enable bus-mastering for this device.
463     #[inline]
464     pub fn set_master(&self) {
465         // SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`.
466         unsafe { bindings::pci_set_master(self.as_raw()) };
467     }
468 }
469 
470 // SAFETY: `pci::Device` is a transparent wrapper of `struct pci_dev`.
471 // The offset is guaranteed to point to a valid device field inside `pci::Device`.
472 unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> {
473     const OFFSET: usize = offset_of!(bindings::pci_dev, dev);
474 }
475 
476 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
477 // argument.
478 kernel::impl_device_context_deref!(unsafe { Device });
479 kernel::impl_device_context_into_aref!(Device);
480 
481 impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}
482 
483 // SAFETY: Instances of `Device` are always reference-counted.
484 unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
485     fn inc_ref(&self) {
486         // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
487         unsafe { bindings::pci_dev_get(self.as_raw()) };
488     }
489 
490     unsafe fn dec_ref(obj: NonNull<Self>) {
491         // SAFETY: The safety requirements guarantee that the refcount is non-zero.
492         unsafe { bindings::pci_dev_put(obj.cast().as_ptr()) }
493     }
494 }
495 
496 impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
497     fn as_ref(&self) -> &device::Device<Ctx> {
498         // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
499         // `struct pci_dev`.
500         let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
501 
502         // SAFETY: `dev` points to a valid `struct device`.
503         unsafe { device::Device::from_raw(dev) }
504     }
505 }
506 
507 impl<Ctx: device::DeviceContext> TryFrom<&device::Device<Ctx>> for &Device<Ctx> {
508     type Error = kernel::error::Error;
509 
510     fn try_from(dev: &device::Device<Ctx>) -> Result<Self, Self::Error> {
511         // SAFETY: By the type invariant of `Device`, `dev.as_raw()` is a valid pointer to a
512         // `struct device`.
513         if !unsafe { bindings::dev_is_pci(dev.as_raw()) } {
514             return Err(EINVAL);
515         }
516 
517         // SAFETY: We've just verified that the bus type of `dev` equals `bindings::pci_bus_type`,
518         // hence `dev` must be embedded in a valid `struct pci_dev` as guaranteed by the
519         // corresponding C code.
520         let pdev = unsafe { container_of!(dev.as_raw(), bindings::pci_dev, dev) };
521 
522         // SAFETY: `pdev` is a valid pointer to a `struct pci_dev`.
523         Ok(unsafe { &*pdev.cast() })
524     }
525 }
526 
527 // SAFETY: A `Device` is always reference-counted and can be released from any thread.
528 unsafe impl Send for Device {}
529 
530 // SAFETY: `Device` can be shared among threads because all methods of `Device`
531 // (i.e. `Device<Normal>) are thread safe.
532 unsafe impl Sync for Device {}
533 
534 // SAFETY: Same as `Device<Normal>` -- the underlying `struct pci_dev` is the same;
535 // `Bound` is a zero-sized type-state marker that does not affect thread safety.
536 unsafe impl Sync for Device<device::Bound> {}
537