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