Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/devices/src/irqchip/kvm/mod.rs
5394 views
1
// Copyright 2020 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 base::error;
6
use base::Error;
7
use base::Event;
8
use base::Result;
9
#[cfg(target_arch = "x86_64")]
10
use hypervisor::kvm::KvmCap;
11
use hypervisor::kvm::KvmVcpu;
12
use hypervisor::IrqRoute;
13
use hypervisor::MPState;
14
use hypervisor::Vcpu;
15
use kvm_sys::kvm_mp_state;
16
use resources::SystemAllocator;
17
18
use crate::Bus;
19
use crate::IrqEdgeEvent;
20
use crate::IrqEventSource;
21
use crate::IrqLevelEvent;
22
23
#[cfg(target_arch = "x86_64")]
24
mod x86_64;
25
#[cfg(target_arch = "x86_64")]
26
pub use x86_64::*;
27
28
#[cfg(target_arch = "aarch64")]
29
mod aarch64;
30
#[cfg(target_arch = "aarch64")]
31
pub use aarch64::*;
32
33
#[cfg(target_arch = "riscv64")]
34
mod riscv64;
35
#[cfg(target_arch = "riscv64")]
36
pub use riscv64::*;
37
38
use crate::IrqChip;
39
use crate::IrqChipCap;
40
use crate::IrqEventIndex;
41
use crate::VcpuRunState;
42
43
/// This IrqChip only works with Kvm so we only implement it for KvmVcpu.
44
impl IrqChip for KvmKernelIrqChip {
45
/// Add a vcpu to the irq chip.
46
fn add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()> {
47
let vcpu: &KvmVcpu = vcpu
48
.downcast_ref()
49
.expect("KvmKernelIrqChip::add_vcpu called with non-KvmVcpu");
50
self.vcpus.lock()[vcpu_id] = Some(vcpu.try_clone()?);
51
Ok(())
52
}
53
54
/// Register an event with edge-trigger semantic that can trigger an interrupt
55
/// for a particular GSI.
56
fn register_edge_irq_event(
57
&mut self,
58
irq: u32,
59
irq_event: &IrqEdgeEvent,
60
_source: IrqEventSource,
61
) -> Result<Option<IrqEventIndex>> {
62
self.vm.register_irqfd(irq, irq_event.get_trigger(), None)?;
63
Ok(None)
64
}
65
66
/// Unregister an event with edge-trigger semantic for a particular GSI.
67
fn unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()> {
68
self.vm.unregister_irqfd(irq, irq_event.get_trigger())
69
}
70
71
/// Register an event with level-trigger semantic that can trigger an interrupt
72
/// for a particular GSI.
73
fn register_level_irq_event(
74
&mut self,
75
irq: u32,
76
irq_event: &IrqLevelEvent,
77
_source: IrqEventSource,
78
) -> Result<Option<IrqEventIndex>> {
79
self.vm
80
.register_irqfd(irq, irq_event.get_trigger(), Some(irq_event.get_resample()))?;
81
Ok(None)
82
}
83
84
/// Unregister an event with level-trigger semantic for a particular GSI.
85
fn unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()> {
86
self.vm.unregister_irqfd(irq, irq_event.get_trigger())
87
}
88
89
/// Route an IRQ line to an interrupt controller, or to a particular MSI vector.
90
fn route_irq(&mut self, route: IrqRoute) -> Result<()> {
91
let mut routes = self.routes.lock();
92
routes.retain(|r| r.gsi != route.gsi);
93
94
routes.push(route);
95
96
self.vm.set_gsi_routing(&routes)
97
}
98
99
/// Replace all irq routes with the supplied routes
100
fn set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()> {
101
let mut current_routes = self.routes.lock();
102
*current_routes = routes.to_vec();
103
104
self.vm.set_gsi_routing(&current_routes)
105
}
106
107
/// Return a vector of all registered irq numbers and their associated events and event
108
/// indices. These should be used by the main thread to wait for irq events.
109
/// For the KvmKernelIrqChip, the kernel handles listening to irq events being triggered by
110
/// devices, so this function always returns an empty Vec.
111
fn irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, IrqEventSource, Event)>> {
112
Ok(Vec::new())
113
}
114
115
/// Either assert or deassert an IRQ line. Sends to either an interrupt controller, or does
116
/// a send_msi if the irq is associated with an MSI.
117
/// For the KvmKernelIrqChip this simply calls the KVM_SET_IRQ_LINE ioctl.
118
fn service_irq(&mut self, irq: u32, level: bool) -> Result<()> {
119
self.vm.set_irq_line(irq, level)
120
}
121
122
/// Service an IRQ event by asserting then deasserting an IRQ line. The associated Event
123
/// that triggered the irq event will be read from. If the irq is associated with a resample
124
/// Event, then the deassert will only happen after an EOI is broadcast for a vector
125
/// associated with the irq line.
126
/// This function should never be called on KvmKernelIrqChip.
127
fn service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()> {
128
error!("service_irq_event should never be called for KvmKernelIrqChip");
129
Ok(())
130
}
131
132
/// Broadcast an end of interrupt.
133
/// This should never be called on a KvmKernelIrqChip because a KVM vcpu should never exit
134
/// with the KVM_EXIT_EOI_BROADCAST reason when an in-kernel irqchip exists.
135
fn broadcast_eoi(&self, _vector: u8) -> Result<()> {
136
error!("broadcast_eoi should never be called for KvmKernelIrqChip");
137
Ok(())
138
}
139
140
/// Injects any pending interrupts for `vcpu`.
141
/// For KvmKernelIrqChip this is a no-op because KVM is responsible for injecting all
142
/// interrupts.
143
fn inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()> {
144
Ok(())
145
}
146
147
/// Notifies the irq chip that the specified VCPU has executed a halt instruction.
148
/// For KvmKernelIrqChip this is a no-op because KVM handles VCPU blocking.
149
fn halted(&self, _vcpu_id: usize) {}
150
151
/// Blocks until `vcpu` is in a runnable state or until interrupted by
152
/// `IrqChip::kick_halted_vcpus`. Returns `VcpuRunState::Runnable if vcpu is runnable, or
153
/// `VcpuRunState::Interrupted` if the wait was interrupted.
154
/// For KvmKernelIrqChip this is a no-op and always returns Runnable because KVM handles VCPU
155
/// blocking.
156
fn wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState> {
157
Ok(VcpuRunState::Runnable)
158
}
159
160
/// Makes unrunnable VCPUs return immediately from `wait_until_runnable`.
161
/// For KvmKernelIrqChip this is a no-op because KVM handles VCPU blocking.
162
fn kick_halted_vcpus(&self) {}
163
164
/// Get the current MP state of the specified VCPU.
165
fn get_mp_state(&self, vcpu_id: usize) -> Result<MPState> {
166
match self.vcpus.lock().get(vcpu_id) {
167
Some(Some(vcpu)) => Ok(MPState::from(&vcpu.get_mp_state()?)),
168
_ => Err(Error::new(libc::ENOENT)),
169
}
170
}
171
172
/// Set the current MP state of the specified VCPU.
173
fn set_mp_state(&mut self, vcpu_id: usize, state: &MPState) -> Result<()> {
174
match self.vcpus.lock().get(vcpu_id) {
175
Some(Some(vcpu)) => vcpu.set_mp_state(&kvm_mp_state::from(state)),
176
_ => Err(Error::new(libc::ENOENT)),
177
}
178
}
179
180
/// Attempt to clone this IrqChip instance.
181
fn try_clone(&self) -> Result<Self> {
182
// Because the KvmKernelIrqchip struct contains arch-specific fields we leave the
183
// cloning to arch-specific implementations
184
self.arch_try_clone()
185
}
186
187
/// Finalize irqchip setup. Should be called once all devices have registered irq events and
188
/// been added to the io_bus and mmio_bus.
189
/// KvmKernelIrqChip does not need to do anything here.
190
fn finalize_devices(
191
&mut self,
192
_resources: &mut SystemAllocator,
193
_io_bus: &Bus,
194
_mmio_bus: &Bus,
195
) -> Result<()> {
196
Ok(())
197
}
198
199
/// The KvmKernelIrqChip doesn't process irq events itself so this function does nothing.
200
fn process_delayed_irq_events(&mut self) -> Result<()> {
201
Ok(())
202
}
203
204
fn irq_delayed_event_token(&self) -> Result<Option<Event>> {
205
Ok(None)
206
}
207
208
fn check_capability(&self, c: IrqChipCap) -> bool {
209
match c {
210
#[cfg(target_arch = "x86_64")]
211
IrqChipCap::TscDeadlineTimer => self.vm.check_raw_capability(KvmCap::TscDeadlineTimer),
212
#[cfg(target_arch = "x86_64")]
213
IrqChipCap::X2Apic => true,
214
IrqChipCap::MpStateGetSet => true,
215
}
216
}
217
}
218
219