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