Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/devices/src/irqchip/halla/mod.rs
5394 views
1
// Copyright 2025 The ChromiumOS Authors
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
use std::sync::Arc;
6
7
use base::error;
8
use base::Error;
9
use base::Event;
10
use base::Result;
11
use hypervisor::halla::halla_sys::*;
12
use hypervisor::halla::HallaVcpu;
13
use hypervisor::halla::HallaVm;
14
use hypervisor::DeviceKind;
15
use hypervisor::IrqRoute;
16
use hypervisor::MPState;
17
use hypervisor::Vcpu;
18
use hypervisor::Vm;
19
use resources::SystemAllocator;
20
use sync::Mutex;
21
22
use crate::Bus;
23
use crate::IrqChip;
24
use crate::IrqChipAArch64;
25
use crate::IrqChipCap;
26
use crate::IrqEdgeEvent;
27
use crate::IrqEventIndex;
28
use crate::IrqEventSource;
29
use crate::IrqLevelEvent;
30
use crate::VcpuRunState;
31
32
/// Default ARM routing table. AARCH64_GIC_NR_SPIS pins go to VGIC.
33
fn default_irq_routing_table() -> Vec<IrqRoute> {
34
let mut routes: Vec<IrqRoute> = Vec::new();
35
36
for i in 0..AARCH64_GIC_NR_SPIS {
37
routes.push(IrqRoute::gic_irq_route(i));
38
}
39
40
routes
41
}
42
43
/// IrqChip implementation where the entire IrqChip is emulated by HVM.
44
///
45
/// This implementation will use the HVM API to create and configure the in-kernel irqchip.
46
pub struct HallaKernelIrqChip {
47
pub(super) vm: HallaVm,
48
pub(super) vcpus: Arc<Mutex<Vec<Option<HallaVcpu>>>>,
49
device_kind: DeviceKind,
50
pub(super) routes: Arc<Mutex<Vec<IrqRoute>>>,
51
}
52
53
// These constants indicate the address space used by the ARM vGIC.
54
const AARCH64_GIC_DIST_SIZE: u64 = 0x10000;
55
56
// These constants indicate the placement of the GIC registers in the physical
57
// address space.
58
const AARCH64_GIC_DIST_BASE: u64 = 0x40000000 - AARCH64_GIC_DIST_SIZE;
59
const AARCH64_GIC_REDIST_SIZE: u64 = 0x20000;
60
61
// This is the minimum number of SPI interrupts aligned to 32 + 32 for the
62
// PPI (16) and GSI (16).
63
// pub const AARCH64_GIC_NR_IRQS: u32 = 64;
64
// Number of SPIs (32), which is the NR_IRQS (64) minus the number of PPIs (16) and GSIs (16)
65
pub const AARCH64_GIC_NR_SPIS: u32 = 32;
66
67
impl HallaKernelIrqChip {
68
/// Construct a new HallaKernelIrqchip.
69
pub fn new(vm: HallaVm, num_vcpus: usize) -> Result<HallaKernelIrqChip> {
70
let dist_if_addr: u64 = AARCH64_GIC_DIST_BASE;
71
let redist_addr: u64 = dist_if_addr - (AARCH64_GIC_REDIST_SIZE * num_vcpus as u64);
72
let device_kind = DeviceKind::ArmVgicV3;
73
74
// prepare hvm_create_device and call ioctl
75
let device_dis = hvm_create_device {
76
dev_type: hvm_device_type_HVM_DEV_TYPE_ARM_VGIC_V3_DIST,
77
id: 0,
78
flags: 0,
79
dev_addr: dist_if_addr,
80
dev_reg_size: AARCH64_GIC_DIST_SIZE,
81
attr_addr: 0_u64,
82
attr_size: 0_u64,
83
};
84
85
match vm.create_halla_device(device_dis) {
86
Ok(()) => {}
87
Err(e) => {
88
error!("failed to create halla device with err: {}", e);
89
return Err(e);
90
}
91
};
92
93
let device_redis = hvm_create_device {
94
dev_type: hvm_device_type_HVM_DEV_TYPE_ARM_VGIC_V3_REDIST,
95
id: 0,
96
flags: 0,
97
dev_addr: redist_addr,
98
dev_reg_size: AARCH64_GIC_REDIST_SIZE,
99
attr_addr: 0_u64,
100
attr_size: 0_u64,
101
};
102
103
match vm.create_halla_device(device_redis) {
104
Ok(()) => {}
105
Err(e) => {
106
error!("failed to create halla device with err: {}", e);
107
return Err(e);
108
}
109
};
110
111
Ok(HallaKernelIrqChip {
112
vm,
113
vcpus: Arc::new(Mutex::new((0..num_vcpus).map(|_| None).collect())),
114
device_kind,
115
routes: Arc::new(Mutex::new(default_irq_routing_table())),
116
})
117
}
118
/// Attempt to create a shallow clone of this aarch64 HallaKernelIrqChip instance.
119
pub(super) fn arch_try_clone(&self) -> Result<Self> {
120
Ok(HallaKernelIrqChip {
121
vm: self.vm.try_clone()?,
122
vcpus: self.vcpus.clone(),
123
device_kind: self.device_kind,
124
routes: self.routes.clone(),
125
})
126
}
127
}
128
129
impl IrqChipAArch64 for HallaKernelIrqChip {
130
fn try_box_clone(&self) -> Result<Box<dyn IrqChipAArch64>> {
131
Ok(Box::new(self.try_clone()?))
132
}
133
134
fn as_irq_chip(&self) -> &dyn IrqChip {
135
self
136
}
137
138
fn as_irq_chip_mut(&mut self) -> &mut dyn IrqChip {
139
self
140
}
141
142
fn get_vgic_version(&self) -> DeviceKind {
143
self.device_kind
144
}
145
146
fn has_vgic_its(&self) -> bool {
147
false
148
}
149
150
fn finalize(&self) -> Result<()> {
151
Ok(())
152
}
153
}
154
155
/// This IrqChip only works with Halla so we only implement it for HallaVcpu.
156
impl IrqChip for HallaKernelIrqChip {
157
/// Add a vcpu to the irq chip.
158
fn add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()> {
159
let vcpu: &HallaVcpu = vcpu
160
.downcast_ref()
161
.expect("HallaKernelIrqChip::add_vcpu called with non-HallaVcpu");
162
self.vcpus.lock()[vcpu_id] = Some(vcpu.try_clone()?);
163
Ok(())
164
}
165
166
/// Register an event with edge-trigger semantic that can trigger an interrupt
167
/// for a particular GSI.
168
fn register_edge_irq_event(
169
&mut self,
170
irq: u32,
171
irq_event: &IrqEdgeEvent,
172
_source: IrqEventSource,
173
) -> Result<Option<IrqEventIndex>> {
174
self.vm.register_irqfd(irq, irq_event.get_trigger(), None)?;
175
Ok(None)
176
}
177
178
/// Unregister an event with edge-trigger semantic for a particular GSI.
179
fn unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()> {
180
self.vm.unregister_irqfd(irq, irq_event.get_trigger())
181
}
182
183
/// Register an event with level-trigger semantic that can trigger an interrupt
184
/// for a particular GSI.
185
fn register_level_irq_event(
186
&mut self,
187
irq: u32,
188
irq_event: &IrqLevelEvent,
189
_source: IrqEventSource,
190
) -> Result<Option<IrqEventIndex>> {
191
self.vm
192
.register_irqfd(irq, irq_event.get_trigger(), Some(irq_event.get_resample()))?;
193
Ok(None)
194
}
195
196
/// Unregister an event with level-trigger semantic for a particular GSI.
197
fn unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()> {
198
self.vm.unregister_irqfd(irq, irq_event.get_trigger())
199
}
200
201
/// Route an IRQ line to an interrupt controller, or to a particular MSI vector.
202
fn route_irq(&mut self, route: IrqRoute) -> Result<()> {
203
let mut routes = self.routes.lock();
204
routes.retain(|r| r.gsi != route.gsi);
205
206
routes.push(route);
207
Ok(())
208
}
209
210
/// Replace all irq routes with the supplied routes
211
fn set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()> {
212
let mut current_routes = self.routes.lock();
213
*current_routes = routes.to_vec();
214
Ok(())
215
}
216
217
/// Return a vector of all registered irq numbers and their associated events and event
218
/// indices. These should be used by the main thread to wait for irq events.
219
/// For the HallaKernelIrqChip, the kernel handles listening to irq events being triggered
220
/// by devices, so this function always returns an empty Vec.
221
fn irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, IrqEventSource, Event)>> {
222
Ok(Vec::new())
223
}
224
225
/// Either assert or deassert an IRQ line. Sends to either an interrupt controller, or does
226
/// a send_msi if the irq is associated with an MSI.
227
/// For the HallaKernelIrqChip this simply calls the HVM_SET_IRQ_LINE ioctl.
228
fn service_irq(&mut self, irq: u32, level: bool) -> Result<()> {
229
self.vm.set_irq_line(irq, level)
230
}
231
232
/// Service an IRQ event by asserting then deasserting an IRQ line. The associated Event
233
/// that triggered the irq event will be read from. If the irq is associated with a resample
234
/// Event, then the deassert will only happen after an EOI is broadcast for a vector
235
/// associated with the irq line.
236
/// This function should never be called on HallaKernelIrqChip.
237
fn service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()> {
238
error!("service_irq_event should never be called for HallaKernelIrqChip");
239
Ok(())
240
}
241
242
/// Broadcast an end of interrupt.
243
/// This should never be called on a HallaKernelIrqChip because a Halla vcpu should
244
/// never exit with the HVM_EXIT_EOI_BROADCAST reason when an in-kernel irqchip exists.
245
fn broadcast_eoi(&self, _vector: u8) -> Result<()> {
246
error!("broadcast_eoi should never be called for HallaKernelIrqChip");
247
Ok(())
248
}
249
250
/// Injects any pending interrupts for `vcpu`.
251
/// For HallaKernelIrqChip this is a no-op because Halla is responsible for injecting
252
/// all interrupts.
253
fn inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()> {
254
Ok(())
255
}
256
257
/// Notifies the irq chip that the specified VCPU has executed a halt instruction.
258
/// For HallaKernelIrqChip this is a no-op because Halla handles VCPU blocking.
259
fn halted(&self, _vcpu_id: usize) {}
260
261
/// Blocks until `vcpu` is in a runnable state or until interrupted by
262
/// `IrqChip::kick_halted_vcpus`. Returns `VcpuRunState::Runnable if vcpu is runnable, or
263
/// `VcpuRunState::Interrupted` if the wait was interrupted.
264
/// For HallaKernelIrqChip this is a no-op and always returns Runnable because Halla
265
/// handles VCPU blocking.
266
fn wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState> {
267
Ok(VcpuRunState::Runnable)
268
}
269
270
/// Makes unrunnable VCPUs return immediately from `wait_until_runnable`.
271
/// For HallaKernelIrqChip this is a no-op because Halla handles VCPU blocking.
272
fn kick_halted_vcpus(&self) {}
273
274
/// Get the current MP state of the specified VCPU.
275
fn get_mp_state(&self, _vcpu_id: usize) -> Result<MPState> {
276
Err(Error::new(libc::ENOENT))
277
}
278
279
/// Set the current MP state of the specified VCPU.
280
fn set_mp_state(&mut self, _vcpu_id: usize, _state: &MPState) -> Result<()> {
281
Err(Error::new(libc::ENOENT))
282
}
283
284
/// Attempt to clone this IrqChip instance.
285
fn try_clone(&self) -> Result<Self> {
286
// Because the HallaKernelIrqChip struct contains arch-specific fields we leave the
287
// cloning to arch-specific implementations
288
self.arch_try_clone()
289
}
290
291
/// Finalize irqchip setup. Should be called once all devices have registered irq events and
292
/// been added to the io_bus and mmio_bus.
293
/// HallaKernelIrqChip does not need to do anything here.
294
fn finalize_devices(
295
&mut self,
296
_resources: &mut SystemAllocator,
297
_io_bus: &Bus,
298
_mmio_bus: &Bus,
299
) -> Result<()> {
300
Ok(())
301
}
302
303
/// The HallaKernelIrqChip doesn't process irq events itself so this function does nothing.
304
fn process_delayed_irq_events(&mut self) -> Result<()> {
305
Ok(())
306
}
307
308
fn irq_delayed_event_token(&self) -> Result<Option<Event>> {
309
Ok(None)
310
}
311
312
fn check_capability(&self, _c: IrqChipCap) -> bool {
313
false
314
}
315
}
316
317