1 // SPDX-License-Identifier: GPL-2.0 2 // SPDX-FileCopyrightText: Copyright 2025 Collabora ltd. 3 4 //! This module provides types like [`Registration`] and 5 //! [`ThreadedRegistration`], which allow users to register handlers for a given 6 //! IRQ line. 7 8 use core::marker::PhantomPinned; 9 10 use crate::alloc::Allocator; 11 use crate::device::{Bound, Device}; 12 use crate::devres::Devres; 13 use crate::error::to_result; 14 use crate::irq::flags::Flags; 15 use crate::prelude::*; 16 use crate::str::CStr; 17 use crate::sync::Arc; 18 19 /// The value that can be returned from a [`Handler`] or a [`ThreadedHandler`]. 20 #[repr(u32)] 21 pub enum IrqReturn { 22 /// The interrupt was not from this device or was not handled. 23 None = bindings::irqreturn_IRQ_NONE, 24 25 /// The interrupt was handled by this device. 26 Handled = bindings::irqreturn_IRQ_HANDLED, 27 } 28 29 /// Callbacks for an IRQ handler. 30 pub trait Handler: Sync { 31 /// The hard IRQ handler. 32 /// 33 /// This is executed in interrupt context, hence all corresponding 34 /// limitations do apply. 35 /// 36 /// All work that does not necessarily need to be executed from 37 /// interrupt context, should be deferred to a threaded handler. 38 /// See also [`ThreadedRegistration`]. 39 fn handle(&self, device: &Device<Bound>) -> IrqReturn; 40 } 41 42 impl<T: ?Sized + Handler + Send> Handler for Arc<T> { 43 fn handle(&self, device: &Device<Bound>) -> IrqReturn { 44 T::handle(self, device) 45 } 46 } 47 48 impl<T: ?Sized + Handler, A: Allocator> Handler for Box<T, A> { 49 fn handle(&self, device: &Device<Bound>) -> IrqReturn { 50 T::handle(self, device) 51 } 52 } 53 54 /// # Invariants 55 /// 56 /// - `self.irq` is the same as the one passed to `request_{threaded}_irq`. 57 /// - `cookie` was passed to `request_{threaded}_irq` as the cookie. It is guaranteed to be unique 58 /// by the type system, since each call to `new` will return a different instance of 59 /// `Registration`. 60 #[pin_data(PinnedDrop)] 61 struct RegistrationInner { 62 irq: u32, 63 cookie: *mut c_void, 64 } 65 66 impl RegistrationInner { 67 fn synchronize(&self) { 68 // SAFETY: safe as per the invariants of `RegistrationInner` 69 unsafe { bindings::synchronize_irq(self.irq) }; 70 } 71 } 72 73 #[pinned_drop] 74 impl PinnedDrop for RegistrationInner { 75 fn drop(self: Pin<&mut Self>) { 76 // SAFETY: 77 // 78 // Safe as per the invariants of `RegistrationInner` and: 79 // 80 // - The containing struct is `!Unpin` and was initialized using 81 // pin-init, so it occupied the same memory location for the entirety of 82 // its lifetime. 83 // 84 // Notice that this will block until all handlers finish executing, 85 // i.e.: at no point will &self be invalid while the handler is running. 86 unsafe { bindings::free_irq(self.irq, self.cookie) }; 87 } 88 } 89 90 // SAFETY: We only use `inner` on drop, which called at most once with no 91 // concurrent access. 92 unsafe impl Sync for RegistrationInner {} 93 94 // SAFETY: It is safe to send `RegistrationInner` across threads. 95 unsafe impl Send for RegistrationInner {} 96 97 /// A request for an IRQ line for a given device. 98 /// 99 /// # Invariants 100 /// 101 /// - `ìrq` is the number of an interrupt source of `dev`. 102 /// - `irq` has not been registered yet. 103 pub struct IrqRequest<'a> { 104 dev: &'a Device<Bound>, 105 irq: u32, 106 } 107 108 impl<'a> IrqRequest<'a> { 109 /// Creates a new IRQ request for the given device and IRQ number. 110 /// 111 /// # Safety 112 /// 113 /// - `irq` should be a valid IRQ number for `dev`. 114 pub(crate) unsafe fn new(dev: &'a Device<Bound>, irq: u32) -> Self { 115 // INVARIANT: `irq` is a valid IRQ number for `dev`. 116 IrqRequest { dev, irq } 117 } 118 119 /// Returns the IRQ number of an [`IrqRequest`]. 120 pub fn irq(&self) -> u32 { 121 self.irq 122 } 123 } 124 125 /// A registration of an IRQ handler for a given IRQ line. 126 /// 127 /// # Examples 128 /// 129 /// The following is an example of using `Registration`. It uses a 130 /// [`Completion`] to coordinate between the IRQ 131 /// handler and process context. [`Completion`] uses interior mutability, so the 132 /// handler can signal with [`Completion::complete_all()`] and the process 133 /// context can wait with [`Completion::wait_for_completion()`] even though 134 /// there is no way to get a mutable reference to the any of the fields in 135 /// `Data`. 136 /// 137 /// [`Completion`]: kernel::sync::Completion 138 /// [`Completion::complete_all()`]: kernel::sync::Completion::complete_all 139 /// [`Completion::wait_for_completion()`]: kernel::sync::Completion::wait_for_completion 140 /// 141 /// ``` 142 /// use kernel::c_str; 143 /// use kernel::device::{Bound, Device}; 144 /// use kernel::irq::{self, Flags, IrqRequest, IrqReturn, Registration}; 145 /// use kernel::prelude::*; 146 /// use kernel::sync::{Arc, Completion}; 147 /// 148 /// // Data shared between process and IRQ context. 149 /// #[pin_data] 150 /// struct Data { 151 /// #[pin] 152 /// completion: Completion, 153 /// } 154 /// 155 /// impl irq::Handler for Data { 156 /// // Executed in IRQ context. 157 /// fn handle(&self, _dev: &Device<Bound>) -> IrqReturn { 158 /// self.completion.complete_all(); 159 /// IrqReturn::Handled 160 /// } 161 /// } 162 /// 163 /// // Registers an IRQ handler for the given IrqRequest. 164 /// // 165 /// // This runs in process context and assumes `request` was previously acquired from a device. 166 /// fn register_irq( 167 /// handler: impl PinInit<Data, Error>, 168 /// request: IrqRequest<'_>, 169 /// ) -> Result<Arc<Registration<Data>>> { 170 /// let registration = Registration::new(request, Flags::SHARED, c_str!("my_device"), handler); 171 /// 172 /// let registration = Arc::pin_init(registration, GFP_KERNEL)?; 173 /// 174 /// registration.handler().completion.wait_for_completion(); 175 /// 176 /// Ok(registration) 177 /// } 178 /// # Ok::<(), Error>(()) 179 /// ``` 180 /// 181 /// # Invariants 182 /// 183 /// * We own an irq handler whose cookie is a pointer to `Self`. 184 #[pin_data] 185 pub struct Registration<T: Handler + 'static> { 186 #[pin] 187 inner: Devres<RegistrationInner>, 188 189 #[pin] 190 handler: T, 191 192 /// Pinned because we need address stability so that we can pass a pointer 193 /// to the callback. 194 #[pin] 195 _pin: PhantomPinned, 196 } 197 198 impl<T: Handler + 'static> Registration<T> { 199 /// Registers the IRQ handler with the system for the given IRQ number. 200 pub fn new<'a>( 201 request: IrqRequest<'a>, 202 flags: Flags, 203 name: &'static CStr, 204 handler: impl PinInit<T, Error> + 'a, 205 ) -> impl PinInit<Self, Error> + 'a { 206 try_pin_init!(&this in Self { 207 handler <- handler, 208 inner <- Devres::new( 209 request.dev, 210 try_pin_init!(RegistrationInner { 211 // INVARIANT: `this` is a valid pointer to the `Registration` instance 212 cookie: this.as_ptr().cast::<c_void>(), 213 irq: { 214 // SAFETY: 215 // - The callbacks are valid for use with request_irq. 216 // - If this succeeds, the slot is guaranteed to be valid until the 217 // destructor of Self runs, which will deregister the callbacks 218 // before the memory location becomes invalid. 219 // - When request_irq is called, everything that handle_irq_callback will 220 // touch has already been initialized, so it's safe for the callback to 221 // be called immediately. 222 to_result(unsafe { 223 bindings::request_irq( 224 request.irq, 225 Some(handle_irq_callback::<T>), 226 flags.into_inner(), 227 name.as_char_ptr(), 228 this.as_ptr().cast::<c_void>(), 229 ) 230 })?; 231 request.irq 232 } 233 }) 234 ), 235 _pin: PhantomPinned, 236 }) 237 } 238 239 /// Returns a reference to the handler that was registered with the system. 240 pub fn handler(&self) -> &T { 241 &self.handler 242 } 243 244 /// Wait for pending IRQ handlers on other CPUs. 245 /// 246 /// This will attempt to access the inner [`Devres`] container. 247 pub fn try_synchronize(&self) -> Result { 248 let inner = self.inner.try_access().ok_or(ENODEV)?; 249 inner.synchronize(); 250 Ok(()) 251 } 252 253 /// Wait for pending IRQ handlers on other CPUs. 254 pub fn synchronize(&self, dev: &Device<Bound>) -> Result { 255 let inner = self.inner.access(dev)?; 256 inner.synchronize(); 257 Ok(()) 258 } 259 } 260 261 /// # Safety 262 /// 263 /// This function should be only used as the callback in `request_irq`. 264 unsafe extern "C" fn handle_irq_callback<T: Handler>(_irq: i32, ptr: *mut c_void) -> c_uint { 265 // SAFETY: `ptr` is a pointer to `Registration<T>` set in `Registration::new` 266 let registration = unsafe { &*(ptr as *const Registration<T>) }; 267 // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq 268 // callback is running implies that the device has not yet been unbound. 269 let device = unsafe { registration.inner.device().as_bound() }; 270 271 T::handle(®istration.handler, device) as c_uint 272 } 273 274 /// The value that can be returned from [`ThreadedHandler::handle`]. 275 #[repr(u32)] 276 pub enum ThreadedIrqReturn { 277 /// The interrupt was not from this device or was not handled. 278 None = bindings::irqreturn_IRQ_NONE, 279 280 /// The interrupt was handled by this device. 281 Handled = bindings::irqreturn_IRQ_HANDLED, 282 283 /// The handler wants the handler thread to wake up. 284 WakeThread = bindings::irqreturn_IRQ_WAKE_THREAD, 285 } 286 287 /// Callbacks for a threaded IRQ handler. 288 pub trait ThreadedHandler: Sync { 289 /// The hard IRQ handler. 290 /// 291 /// This is executed in interrupt context, hence all corresponding 292 /// limitations do apply. All work that does not necessarily need to be 293 /// executed from interrupt context, should be deferred to the threaded 294 /// handler, i.e. [`ThreadedHandler::handle_threaded`]. 295 /// 296 /// The default implementation returns [`ThreadedIrqReturn::WakeThread`]. 297 #[expect(unused_variables)] 298 fn handle(&self, device: &Device<Bound>) -> ThreadedIrqReturn { 299 ThreadedIrqReturn::WakeThread 300 } 301 302 /// The threaded IRQ handler. 303 /// 304 /// This is executed in process context. The kernel creates a dedicated 305 /// `kthread` for this purpose. 306 fn handle_threaded(&self, device: &Device<Bound>) -> IrqReturn; 307 } 308 309 impl<T: ?Sized + ThreadedHandler + Send> ThreadedHandler for Arc<T> { 310 fn handle(&self, device: &Device<Bound>) -> ThreadedIrqReturn { 311 T::handle(self, device) 312 } 313 314 fn handle_threaded(&self, device: &Device<Bound>) -> IrqReturn { 315 T::handle_threaded(self, device) 316 } 317 } 318 319 impl<T: ?Sized + ThreadedHandler, A: Allocator> ThreadedHandler for Box<T, A> { 320 fn handle(&self, device: &Device<Bound>) -> ThreadedIrqReturn { 321 T::handle(self, device) 322 } 323 324 fn handle_threaded(&self, device: &Device<Bound>) -> IrqReturn { 325 T::handle_threaded(self, device) 326 } 327 } 328 329 /// A registration of a threaded IRQ handler for a given IRQ line. 330 /// 331 /// Two callbacks are required: one to handle the IRQ, and one to handle any 332 /// other work in a separate thread. 333 /// 334 /// The thread handler is only called if the IRQ handler returns 335 /// [`ThreadedIrqReturn::WakeThread`]. 336 /// 337 /// # Examples 338 /// 339 /// The following is an example of using [`ThreadedRegistration`]. It uses a 340 /// [`Mutex`](kernel::sync::Mutex) to provide interior mutability. 341 /// 342 /// ``` 343 /// use kernel::c_str; 344 /// use kernel::device::{Bound, Device}; 345 /// use kernel::irq::{ 346 /// self, Flags, IrqRequest, IrqReturn, ThreadedHandler, ThreadedIrqReturn, 347 /// ThreadedRegistration, 348 /// }; 349 /// use kernel::prelude::*; 350 /// use kernel::sync::{Arc, Mutex}; 351 /// 352 /// // Declare a struct that will be passed in when the interrupt fires. The u32 353 /// // merely serves as an example of some internal data. 354 /// // 355 /// // [`irq::ThreadedHandler::handle`] takes `&self`. This example 356 /// // illustrates how interior mutability can be used when sharing the data 357 /// // between process context and IRQ context. 358 /// #[pin_data] 359 /// struct Data { 360 /// #[pin] 361 /// value: Mutex<u32>, 362 /// } 363 /// 364 /// impl ThreadedHandler for Data { 365 /// // This will run (in a separate kthread) if and only if 366 /// // [`ThreadedHandler::handle`] returns [`WakeThread`], which it does by 367 /// // default. 368 /// fn handle_threaded(&self, _dev: &Device<Bound>) -> IrqReturn { 369 /// let mut data = self.value.lock(); 370 /// *data += 1; 371 /// IrqReturn::Handled 372 /// } 373 /// } 374 /// 375 /// // Registers a threaded IRQ handler for the given [`IrqRequest`]. 376 /// // 377 /// // This is executing in process context and assumes that `request` was 378 /// // previously acquired from a device. 379 /// fn register_threaded_irq( 380 /// handler: impl PinInit<Data, Error>, 381 /// request: IrqRequest<'_>, 382 /// ) -> Result<Arc<ThreadedRegistration<Data>>> { 383 /// let registration = 384 /// ThreadedRegistration::new(request, Flags::SHARED, c_str!("my_device"), handler); 385 /// 386 /// let registration = Arc::pin_init(registration, GFP_KERNEL)?; 387 /// 388 /// { 389 /// // The data can be accessed from process context too. 390 /// let mut data = registration.handler().value.lock(); 391 /// *data += 1; 392 /// } 393 /// 394 /// Ok(registration) 395 /// } 396 /// # Ok::<(), Error>(()) 397 /// ``` 398 /// 399 /// # Invariants 400 /// 401 /// * We own an irq handler whose cookie is a pointer to `Self`. 402 #[pin_data] 403 pub struct ThreadedRegistration<T: ThreadedHandler + 'static> { 404 #[pin] 405 inner: Devres<RegistrationInner>, 406 407 #[pin] 408 handler: T, 409 410 /// Pinned because we need address stability so that we can pass a pointer 411 /// to the callback. 412 #[pin] 413 _pin: PhantomPinned, 414 } 415 416 impl<T: ThreadedHandler + 'static> ThreadedRegistration<T> { 417 /// Registers the IRQ handler with the system for the given IRQ number. 418 pub fn new<'a>( 419 request: IrqRequest<'a>, 420 flags: Flags, 421 name: &'static CStr, 422 handler: impl PinInit<T, Error> + 'a, 423 ) -> impl PinInit<Self, Error> + 'a { 424 try_pin_init!(&this in Self { 425 handler <- handler, 426 inner <- Devres::new( 427 request.dev, 428 try_pin_init!(RegistrationInner { 429 // INVARIANT: `this` is a valid pointer to the `ThreadedRegistration` instance. 430 cookie: this.as_ptr().cast::<c_void>(), 431 irq: { 432 // SAFETY: 433 // - The callbacks are valid for use with request_threaded_irq. 434 // - If this succeeds, the slot is guaranteed to be valid until the 435 // destructor of Self runs, which will deregister the callbacks 436 // before the memory location becomes invalid. 437 // - When request_threaded_irq is called, everything that the two callbacks 438 // will touch has already been initialized, so it's safe for the 439 // callbacks to be called immediately. 440 to_result(unsafe { 441 bindings::request_threaded_irq( 442 request.irq, 443 Some(handle_threaded_irq_callback::<T>), 444 Some(thread_fn_callback::<T>), 445 flags.into_inner(), 446 name.as_char_ptr(), 447 this.as_ptr().cast::<c_void>(), 448 ) 449 })?; 450 request.irq 451 } 452 }) 453 ), 454 _pin: PhantomPinned, 455 }) 456 } 457 458 /// Returns a reference to the handler that was registered with the system. 459 pub fn handler(&self) -> &T { 460 &self.handler 461 } 462 463 /// Wait for pending IRQ handlers on other CPUs. 464 /// 465 /// This will attempt to access the inner [`Devres`] container. 466 pub fn try_synchronize(&self) -> Result { 467 let inner = self.inner.try_access().ok_or(ENODEV)?; 468 inner.synchronize(); 469 Ok(()) 470 } 471 472 /// Wait for pending IRQ handlers on other CPUs. 473 pub fn synchronize(&self, dev: &Device<Bound>) -> Result { 474 let inner = self.inner.access(dev)?; 475 inner.synchronize(); 476 Ok(()) 477 } 478 } 479 480 /// # Safety 481 /// 482 /// This function should be only used as the callback in `request_threaded_irq`. 483 unsafe extern "C" fn handle_threaded_irq_callback<T: ThreadedHandler>( 484 _irq: i32, 485 ptr: *mut c_void, 486 ) -> c_uint { 487 // SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new` 488 let registration = unsafe { &*(ptr as *const ThreadedRegistration<T>) }; 489 // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq 490 // callback is running implies that the device has not yet been unbound. 491 let device = unsafe { registration.inner.device().as_bound() }; 492 493 T::handle(®istration.handler, device) as c_uint 494 } 495 496 /// # Safety 497 /// 498 /// This function should be only used as the callback in `request_threaded_irq`. 499 unsafe extern "C" fn thread_fn_callback<T: ThreadedHandler>(_irq: i32, ptr: *mut c_void) -> c_uint { 500 // SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new` 501 let registration = unsafe { &*(ptr as *const ThreadedRegistration<T>) }; 502 // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq 503 // callback is running implies that the device has not yet been unbound. 504 let device = unsafe { registration.inner.device().as_bound() }; 505 506 T::handle_threaded(®istration.handler, device) as c_uint 507 } 508