xref: /aosp_15_r20/external/crosvm/devices/src/irqchip/geniezone/mod.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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::GeniezoneVcpu;
13 use hypervisor::geniezone::GeniezoneVm;
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.
default_irq_routing_table() -> Vec<IrqRoute>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 GZVM.
44 ///
45 /// This implementation will use the GZVM API to create and configure the in-kernel irqchip.
46 pub struct GeniezoneKernelIrqChip {
47     pub(super) vm: GeniezoneVm,
48     pub(super) vcpus: Arc<Mutex<Vec<Option<GeniezoneVcpu>>>>,
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 GeniezoneKernelIrqChip {
68     /// Construct a new GzvmKernelIrqchip.
new(vm: GeniezoneVm, num_vcpus: usize) -> Result<GeniezoneKernelIrqChip>69     pub fn new(vm: GeniezoneVm, num_vcpus: usize) -> Result<GeniezoneKernelIrqChip> {
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 gzvm_create_device and call ioctl
75         let device_dis = gzvm_create_device {
76             dev_type: gzvm_device_type_GZVM_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_geniezone_device(device_dis) {
86             Ok(()) => {}
87             Err(e) => {
88                 error!("failed to create geniezone device with err: {}", e);
89                 return Err(e);
90             }
91         };
92 
93         let device_redis = gzvm_create_device {
94             dev_type: gzvm_device_type_GZVM_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_geniezone_device(device_redis) {
104             Ok(()) => {}
105             Err(e) => {
106                 error!("failed to create geniezone device with err: {}", e);
107                 return Err(e);
108             }
109         };
110 
111         Ok(GeniezoneKernelIrqChip {
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 GzvmKernelIrqChip instance.
arch_try_clone(&self) -> Result<Self>119     pub(super) fn arch_try_clone(&self) -> Result<Self> {
120         Ok(GeniezoneKernelIrqChip {
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 GeniezoneKernelIrqChip {
try_box_clone(&self) -> Result<Box<dyn IrqChipAArch64>>130     fn try_box_clone(&self) -> Result<Box<dyn IrqChipAArch64>> {
131         Ok(Box::new(self.try_clone()?))
132     }
133 
as_irq_chip(&self) -> &dyn IrqChip134     fn as_irq_chip(&self) -> &dyn IrqChip {
135         self
136     }
137 
as_irq_chip_mut(&mut self) -> &mut dyn IrqChip138     fn as_irq_chip_mut(&mut self) -> &mut dyn IrqChip {
139         self
140     }
141 
get_vgic_version(&self) -> DeviceKind142     fn get_vgic_version(&self) -> DeviceKind {
143         self.device_kind
144     }
145 
finalize(&self) -> Result<()>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.
add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()>154     fn add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()> {
155         let vcpu: &GeniezoneVcpu = vcpu
156             .downcast_ref()
157             .expect("GeniezoneKernelIrqChip::add_vcpu called with non-GeniezoneVcpu");
158         self.vcpus.lock()[vcpu_id] = Some(vcpu.try_clone()?);
159         Ok(())
160     }
161 
162     /// Register an event with edge-trigger semantic that can trigger an interrupt
163     /// for a particular GSI.
register_edge_irq_event( &mut self, irq: u32, irq_event: &IrqEdgeEvent, _source: IrqEventSource, ) -> Result<Option<IrqEventIndex>>164     fn register_edge_irq_event(
165         &mut self,
166         irq: u32,
167         irq_event: &IrqEdgeEvent,
168         _source: IrqEventSource,
169     ) -> Result<Option<IrqEventIndex>> {
170         self.vm.register_irqfd(irq, irq_event.get_trigger(), None)?;
171         Ok(None)
172     }
173 
174     /// Unregister an event with edge-trigger semantic for a particular GSI.
unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()>175     fn unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()> {
176         self.vm.unregister_irqfd(irq, irq_event.get_trigger())
177     }
178 
179     /// Register an event with level-trigger semantic that can trigger an interrupt
180     /// for a particular GSI.
register_level_irq_event( &mut self, irq: u32, irq_event: &IrqLevelEvent, _source: IrqEventSource, ) -> Result<Option<IrqEventIndex>>181     fn register_level_irq_event(
182         &mut self,
183         irq: u32,
184         irq_event: &IrqLevelEvent,
185         _source: IrqEventSource,
186     ) -> Result<Option<IrqEventIndex>> {
187         self.vm
188             .register_irqfd(irq, irq_event.get_trigger(), Some(irq_event.get_resample()))?;
189         Ok(None)
190     }
191 
192     /// Unregister an event with level-trigger semantic for a particular GSI.
unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()>193     fn unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()> {
194         self.vm.unregister_irqfd(irq, irq_event.get_trigger())
195     }
196 
197     /// Route an IRQ line to an interrupt controller, or to a particular MSI vector.
route_irq(&mut self, route: IrqRoute) -> Result<()>198     fn route_irq(&mut self, route: IrqRoute) -> Result<()> {
199         let mut routes = self.routes.lock();
200         routes.retain(|r| r.gsi != route.gsi);
201 
202         routes.push(route);
203         Ok(())
204     }
205 
206     /// Replace all irq routes with the supplied routes
set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()>207     fn set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()> {
208         let mut current_routes = self.routes.lock();
209         *current_routes = routes.to_vec();
210         Ok(())
211     }
212 
213     /// Return a vector of all registered irq numbers and their associated events and event
214     /// indices. These should be used by the main thread to wait for irq events.
215     /// For the GeniezoneKernelIrqChip, the kernel handles listening to irq events being triggered
216     /// by devices, so this function always returns an empty Vec.
irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, IrqEventSource, Event)>>217     fn irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, IrqEventSource, Event)>> {
218         Ok(Vec::new())
219     }
220 
221     /// Either assert or deassert an IRQ line.  Sends to either an interrupt controller, or does
222     /// a send_msi if the irq is associated with an MSI.
223     /// For the GeniezoneKernelIrqChip this simply calls the GZVM_SET_IRQ_LINE ioctl.
service_irq(&mut self, irq: u32, level: bool) -> Result<()>224     fn service_irq(&mut self, irq: u32, level: bool) -> Result<()> {
225         self.vm.set_irq_line(irq, level)
226     }
227 
228     /// Service an IRQ event by asserting then deasserting an IRQ line. The associated Event
229     /// that triggered the irq event will be read from. If the irq is associated with a resample
230     /// Event, then the deassert will only happen after an EOI is broadcast for a vector
231     /// associated with the irq line.
232     /// This function should never be called on GeniezoneKernelIrqChip.
service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()>233     fn service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()> {
234         error!("service_irq_event should never be called for GeniezoneKernelIrqChip");
235         Ok(())
236     }
237 
238     /// Broadcast an end of interrupt.
239     /// This should never be called on a GeniezoneKernelIrqChip because a Geniezone vcpu should
240     /// never exit with the GZVM_EXIT_EOI_BROADCAST reason when an in-kernel irqchip exists.
broadcast_eoi(&self, _vector: u8) -> Result<()>241     fn broadcast_eoi(&self, _vector: u8) -> Result<()> {
242         error!("broadcast_eoi should never be called for GeniezoneKernelIrqChip");
243         Ok(())
244     }
245 
246     /// Injects any pending interrupts for `vcpu`.
247     /// For GeniezoneKernelIrqChip this is a no-op because Geniezone is responsible for injecting
248     /// all interrupts.
inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()>249     fn inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()> {
250         Ok(())
251     }
252 
253     /// Notifies the irq chip that the specified VCPU has executed a halt instruction.
254     /// For GeniezoneKernelIrqChip this is a no-op because Geniezone handles VCPU blocking.
halted(&self, _vcpu_id: usize)255     fn halted(&self, _vcpu_id: usize) {}
256 
257     /// Blocks until `vcpu` is in a runnable state or until interrupted by
258     /// `IrqChip::kick_halted_vcpus`.  Returns `VcpuRunState::Runnable if vcpu is runnable, or
259     /// `VcpuRunState::Interrupted` if the wait was interrupted.
260     /// For GeniezoneKernelIrqChip this is a no-op and always returns Runnable because Geniezone
261     /// handles VCPU blocking.
wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState>262     fn wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState> {
263         Ok(VcpuRunState::Runnable)
264     }
265 
266     /// Makes unrunnable VCPUs return immediately from `wait_until_runnable`.
267     /// For GeniezoneKernelIrqChip this is a no-op because Geniezone handles VCPU blocking.
kick_halted_vcpus(&self)268     fn kick_halted_vcpus(&self) {}
269 
270     /// Get the current MP state of the specified VCPU.
get_mp_state(&self, _vcpu_id: usize) -> Result<MPState>271     fn get_mp_state(&self, _vcpu_id: usize) -> Result<MPState> {
272         Err(Error::new(libc::ENOENT))
273     }
274 
275     /// Set the current MP state of the specified VCPU.
set_mp_state(&mut self, _vcpu_id: usize, _state: &MPState) -> Result<()>276     fn set_mp_state(&mut self, _vcpu_id: usize, _state: &MPState) -> Result<()> {
277         Err(Error::new(libc::ENOENT))
278     }
279 
280     /// Attempt to clone this IrqChip instance.
try_clone(&self) -> Result<Self>281     fn try_clone(&self) -> Result<Self> {
282         // Because the GeniezoneKernelIrqChip struct contains arch-specific fields we leave the
283         // cloning to arch-specific implementations
284         self.arch_try_clone()
285     }
286 
287     /// Finalize irqchip setup. Should be called once all devices have registered irq events and
288     /// been added to the io_bus and mmio_bus.
289     /// GeniezoneKernelIrqChip does not need to do anything here.
finalize_devices( &mut self, _resources: &mut SystemAllocator, _io_bus: &Bus, _mmio_bus: &Bus, ) -> Result<()>290     fn finalize_devices(
291         &mut self,
292         _resources: &mut SystemAllocator,
293         _io_bus: &Bus,
294         _mmio_bus: &Bus,
295     ) -> Result<()> {
296         Ok(())
297     }
298 
299     /// The GeniezoneKernelIrqChip doesn't process irq events itself so this function does nothing.
process_delayed_irq_events(&mut self) -> Result<()>300     fn process_delayed_irq_events(&mut self) -> Result<()> {
301         Ok(())
302     }
303 
irq_delayed_event_token(&self) -> Result<Option<Event>>304     fn irq_delayed_event_token(&self) -> Result<Option<Event>> {
305         Ok(None)
306     }
307 
check_capability(&self, _c: IrqChipCap) -> bool308     fn check_capability(&self, _c: IrqChipCap) -> bool {
309         false
310     }
311 }
312