devices/irqchip/kvm/
mod.rs

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