xref: /qemu/rust/qemu-api/src/irq.rs (revision c0fb8e88cbbfc140800e614586b446193a3fd93a)
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 [`SysBusDeviceMethods::init_irq`], and
28 /// initially leaves the pointer to a NULL value, representing an unconnected
29 /// interrupt. To connect it, whoever creates the device fills the pointer with
30 /// the sink's `IRQState *`, for example using `sysbus_connect_irq`.  Because
31 /// devices are generally shared objects, interrupt sources are an example of
32 /// the interior mutability pattern.
33 ///
34 /// Interrupt sources can only be triggered under the Big QEMU Lock; `BqlCell`
35 /// allows access from whatever thread has it.
36 #[derive(Debug)]
37 #[repr(transparent)]
38 pub struct InterruptSource<T = bool>
39 where
40     c_int: From<T>,
41 {
42     cell: BqlCell<*mut IRQState>,
43     _marker: PhantomData<T>,
44 }
45 
46 impl InterruptSource<bool> {
47     /// Send a low (`false`) value to the interrupt sink.
48     pub fn lower(&self) {
49         self.set(false);
50     }
51 
52     /// Send a high-low pulse to the interrupt sink.
53     pub fn pulse(&self) {
54         self.set(true);
55         self.set(false);
56     }
57 
58     /// Send a high (`true`) value to the interrupt sink.
59     pub fn raise(&self) {
60         self.set(true);
61     }
62 }
63 
64 impl<T> InterruptSource<T>
65 where
66     c_int: From<T>,
67 {
68     /// Send `level` to the interrupt sink.
69     pub fn set(&self, level: T) {
70         let ptr = self.cell.get();
71         // SAFETY: the pointer is retrieved under the BQL and remains valid
72         // until the BQL is released, which is after qemu_set_irq() is entered.
73         unsafe {
74             qemu_set_irq(ptr, level.into());
75         }
76     }
77 
78     pub(crate) const fn as_ptr(&self) -> *mut *mut IRQState {
79         self.cell.as_ptr()
80     }
81 }
82 
83 impl Default for InterruptSource {
84     fn default() -> Self {
85         InterruptSource {
86             cell: BqlCell::new(ptr::null_mut()),
87             _marker: PhantomData,
88         }
89     }
90 }
91