1 // Copyright 2024 Red Hat, Inc. 2 // Author(s): Paolo Bonzini <pbonzini@redhat.com> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 //! Bindings for interrupt sources 6 7 use core::ptr; 8 use std::{marker::PhantomData, os::raw::c_int}; 9 10 use crate::{ 11 bindings::{qemu_set_irq, IRQState}, 12 prelude::*, 13 }; 14 15 /// Interrupt sources are used by devices to pass changes to a value (typically 16 /// a boolean). The interrupt sink is usually an interrupt controller or 17 /// GPIO controller. 18 /// 19 /// As far as devices are concerned, interrupt sources are always active-high: 20 /// for example, `InterruptSource<bool>`'s [`raise`](InterruptSource::raise) 21 /// method sends a `true` value to the sink. If the guest has to see a 22 /// different polarity, that change is performed by the board between the 23 /// device and the interrupt controller. 24 /// 25 /// Interrupts are implemented as a pointer to the interrupt "sink", which has 26 /// type [`IRQState`]. A device exposes its source as a QOM link property using 27 /// a function such as 28 /// [`SysBusDevice::init_irq`](crate::sysbus::SysBusDevice::init_irq), and 29 /// initially leaves the pointer to a NULL value, representing an unconnected 30 /// interrupt. To connect it, whoever creates the device fills the pointer with 31 /// the sink's `IRQState *`, for example using `sysbus_connect_irq`. Because 32 /// devices are generally shared objects, interrupt sources are an example of 33 /// the interior mutability pattern. 34 /// 35 /// Interrupt sources can only be triggered under the Big QEMU Lock; `BqlCell` 36 /// allows access from whatever thread has it. 37 #[derive(Debug)] 38 #[repr(transparent)] 39 pub struct InterruptSource<T = bool> 40 where 41 c_int: From<T>, 42 { 43 cell: BqlCell<*mut IRQState>, 44 _marker: PhantomData<T>, 45 } 46 47 impl InterruptSource<bool> { 48 /// Send a low (`false`) value to the interrupt sink. 49 pub fn lower(&self) { 50 self.set(false); 51 } 52 53 /// Send a high-low pulse to the interrupt sink. 54 pub fn pulse(&self) { 55 self.set(true); 56 self.set(false); 57 } 58 59 /// Send a high (`true`) value to the interrupt sink. 60 pub fn raise(&self) { 61 self.set(true); 62 } 63 } 64 65 impl<T> InterruptSource<T> 66 where 67 c_int: From<T>, 68 { 69 /// Send `level` to the interrupt sink. 70 pub fn set(&self, level: T) { 71 let ptr = self.cell.get(); 72 // SAFETY: the pointer is retrieved under the BQL and remains valid 73 // until the BQL is released, which is after qemu_set_irq() is entered. 74 unsafe { 75 qemu_set_irq(ptr, level.into()); 76 } 77 } 78 79 pub(crate) const fn as_ptr(&self) -> *mut *mut IRQState { 80 self.cell.as_ptr() 81 } 82 } 83 84 impl Default for InterruptSource { 85 fn default() -> Self { 86 InterruptSource { 87 cell: BqlCell::new(ptr::null_mut()), 88 _marker: PhantomData, 89 } 90 } 91 } 92