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