Outlining PCI driver with threaded IRQ handling
I've detailed a Rust PCI driver skeleton that uses the kernel crate's IRQ module to implement a top half Handler acknowledging interrupts and waking a bottom half threaded IRQ for heavy processing, using dev_id pointers to manage device context and avoid static lifetime issues in handlers.Providing complete Rust PCI driver with threaded IRQ example
A full Rust kernel module example has been sketched showing PCI registration via bindings, a custom device struct holding irq registration, and IRQ handling splitting the top half that acknowledges the interrupt and returns WakeThread, and the bottom half threaded handler for heavy processing—using static handler instances and passing device context via dev_id pointers.
Here is a self–contained skeleton of a Rust‑for‑Linux PCI driver that requests a threaded interrupt. The primary handler (“top half”) simply acknowledges/clears the device’s interrupt status and returns WakeThread, which causes the kernel to run the secondary (“bottom half”) thread where you can do heavier processing:
// SPDX-License-Identifier: GPL-2.0
#![no_std]
#![feature(allocator_api)]
use core::{ffi::c_void, ptr};
use kernel::{
bindings,
cstr,
irq,
prelude::*,
};
const VENDOR_ID: u16 = 0x1234;
const DEVICE_ID: u16 = 0x5678;
const BAR: u32 = 0;
// Device register offsets/masks for your custom device.
const INT_STATUS_OFF: usize = 0x00;
const INT_ACK_MASK: u32 = 0xffff_ffff;
struct PciDev {
pdev: *mut bindings::pci_dev,
mmio: *mut u8,
irq_reg: Option<irq::Registration>,
}
impl Drop for PciDev {
fn drop(&mut self) {
// Drop irq registration first so that handlers are never called on freed state.
self.irq_reg.take();
unsafe {
if !self.mmio.is_null() {
bindings::pci_iounmap(self.pdev, self.mmio as *mut _);
}
bindings::pci_disable_device(self.pdev);
}
}
}
/// Zero‑sized handler, we use the `dev_id` argument to get back to our `PciDev`.
struct PciIrq;
unsafe impl Sync for PciIrq {}
unsafe impl Send for PciIrq {}
impl irq::Handler for PciIrq {
unsafe fn handle_irq(&self, _irq: u32, dev_id: *mut c_void) -> irq::Return {
let dev = &*(dev_id as *mut PciDev);
// Acknowledge/clear the device interrupt.
ptr::write_volatile(dev.mmio.add(INT_STATUS_OFF) as *mut u32, INT_ACK_MASK);
// Wake the threaded bottom half for heavy work.
irq::Return::WakeThread
}
}
impl irq::Thread for PciIrq {
fn run(&self, irq: u32, dev_id: *mut c_void) -> irq::Return {
let dev = unsafe { &*(dev_id as *mut PciDev) };
pr_info!("threaded IRQ {}: doing heavy processing\n", irq);
// TODO: perform heavy processing of device data here.
irq::Return::Handled
}
}
static PCI_IRQ: PciIrq = PciIrq;
// PCI ID table terminated with a zeroed entry.
static ID_TABLE: [bindings::pci_device_id; 2] = [
bindings::pci_device_id {
vendor: VENDOR_ID,
device: DEVICE_ID,
subvendor: bindings::PCI_ANY_ID,
subdevice: bindings::PCI_ANY_ID,
class: 0,
class_mask: 0,
driver_data: 0,
..bindings::pci_device_id::default()
},
bindings::pci_device_id {
vendor: 0,
device: 0,
subvendor: 0,
subdevice: 0,
class: 0,
class_mask: 0,
driver_data: 0,
..bindings::pci_device_id::default()
},
];
unsafe extern "C" fn probe(pdev: *mut bindings::pci_dev,
_id: *const bindings::pci_device_id) -> i32 {
// Enable device and BAR mapping.
if bindings::pci_enable_device(pdev) != 0 {
return -kernel::errno::ENODEV;
}
let mmio = bindings::pci_iomap(pdev, BAR, 0);
if mmio.is_null() {
bindings::pci_disable_device(pdev);
return -kernel::errno::ENODEV;
}
// Allocate our device state.
let mut dev = match Box::try_new(PciDev {
pdev,
mmio,
irq_reg: None,
}) {
Ok(b) => b,
Err(e) => {
bindings::pci_iounmap(pdev, mmio);
bindings::pci_disable_device(pdev);
return e.to_kernel_errno();
}
};
// Request a threaded IRQ.
let irq_num = (*pdev).irq as u32;
match irq::request_threaded(
irq_num,
Some(&PCI_IRQ), // top half
Some(&PCI_IRQ), // bottom half
0,
cstr!("rust_pci"),
dev.as_mut() as *mut _ as *mut c_void,
) {
Ok(reg) => dev.irq_reg = Some(reg),
Err(e) => {
// Clean up and fail probe.
drop(dev);
return e.to_kernel_errno();
}
}
// Stash our state in drvdata so remove() can find it.
bindings::pci_set_drvdata(pdev, dev.as_mut() as *mut _ as *mut c_void);
// Leak the box to the kernel; it will be reclaimed in remove().
Box::leak(dev);
0
}
unsafe extern "C" fn remove(pdev: *mut bindings::pci_dev) {
let ptr = bindings::pci_get_drvdata(pdev) as *mut PciDev;
if !ptr.is_null() {
// Reconstruct the Box to drop it and run Drop impl.
drop(Box::from_raw(ptr));
}
}
static mut PCI_DRIVER: bindings::pci_driver = bindings::pci_driver {
name: cstr!("rust_pci").as_char_ptr(),
id_table: ID_TABLE.as_ptr(),
probe: Some(probe),
remove: Some(remove),
..bindings::pci_driver::default()
};
module! {
type: RustPciModule,
name: b"rust_pci",
author: b"Example",
description: b"Rust PCI driver with threaded IRQ",
license: b"GPL",
}
struct RustPciModule;
impl kernel::module::KernelModule for RustPciModule {
fn init() -> Result<Self> {
// Register the PCI driver.
let ret = unsafe { bindings::pci_register_driver(&mut PCI_DRIVER) };
if ret != 0 {
return Err(Error::from_kernel_errno(ret));
}
pr_info!("rust_pci: registered\n");
Ok(RustPciModule)
}
}
impl Drop for RustPciModule {
fn drop(&mut self) {
unsafe { bindings::pci_unregister_driver(&mut PCI_DRIVER) };
pr_info!("rust_pci: unregistered\n");
}
}
This is not a complete production driver, but it shows the key pieces:
- The
probe routine sets up the device, maps BAR0, and requests a threaded IRQ using irq::request_threaded.
PciIrq implements both irq::Handler (top half) and irq::Thread (bottom half). The handler acknowledges the interrupt and returns WakeThread to schedule the thread.
- The bottom half does the heavy work and returns
Handled.
You would fill in the register offsets/masks and the actual data processing for your hardware.