• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = AARCH64_AXI_BASE - 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 const AARCH64_AXI_BASE: u64 = 0x40000000;
68 
69 impl GeniezoneKernelIrqChip {
70     /// Construct a new GzvmKernelIrqchip.
new(vm: GeniezoneVm, num_vcpus: usize) -> Result<GeniezoneKernelIrqChip>71     pub fn new(vm: GeniezoneVm, num_vcpus: usize) -> Result<GeniezoneKernelIrqChip> {
72         let dist_if_addr: u64 = AARCH64_GIC_DIST_BASE;
73         let redist_addr: u64 = dist_if_addr - (AARCH64_GIC_REDIST_SIZE * num_vcpus as u64);
74         let device_kind = DeviceKind::ArmVgicV3;
75 
76         // prepare gzvm_create_device and call ioctl
77         let device_dis = gzvm_create_device {
78             dev_type: gzvm_device_type_GZVM_DEV_TYPE_ARM_VGIC_V3_DIST,
79             id: 0,
80             flags: 0,
81             dev_addr: dist_if_addr,
82             dev_reg_size: AARCH64_GIC_DIST_SIZE,
83             attr_addr: 0_u64,
84             attr_size: 0_u64,
85         };
86 
87         match vm.create_geniezone_device(device_dis) {
88             Ok(()) => {}
89             Err(e) => {
90                 error!("failed to create geniezone device with err: {}", e);
91                 return Err(e);
92             }
93         };
94 
95         let device_redis = gzvm_create_device {
96             dev_type: gzvm_device_type_GZVM_DEV_TYPE_ARM_VGIC_V3_REDIST,
97             id: 0,
98             flags: 0,
99             dev_addr: redist_addr,
100             dev_reg_size: AARCH64_GIC_REDIST_SIZE,
101             attr_addr: 0_u64,
102             attr_size: 0_u64,
103         };
104 
105         match vm.create_geniezone_device(device_redis) {
106             Ok(()) => {}
107             Err(e) => {
108                 error!("failed to create geniezone device with err: {}", e);
109                 return Err(e);
110             }
111         };
112 
113         Ok(GeniezoneKernelIrqChip {
114             vm,
115             vcpus: Arc::new(Mutex::new((0..num_vcpus).map(|_| None).collect())),
116             device_kind,
117             routes: Arc::new(Mutex::new(default_irq_routing_table())),
118         })
119     }
120     /// Attempt to create a shallow clone of this aarch64 GzvmKernelIrqChip instance.
arch_try_clone(&self) -> Result<Self>121     pub(super) fn arch_try_clone(&self) -> Result<Self> {
122         Ok(GeniezoneKernelIrqChip {
123             vm: self.vm.try_clone()?,
124             vcpus: self.vcpus.clone(),
125             device_kind: self.device_kind,
126             routes: self.routes.clone(),
127         })
128     }
129 }
130 
131 impl IrqChipAArch64 for GeniezoneKernelIrqChip {
try_box_clone(&self) -> Result<Box<dyn IrqChipAArch64>>132     fn try_box_clone(&self) -> Result<Box<dyn IrqChipAArch64>> {
133         Ok(Box::new(self.try_clone()?))
134     }
135 
as_irq_chip(&self) -> &dyn IrqChip136     fn as_irq_chip(&self) -> &dyn IrqChip {
137         self
138     }
139 
as_irq_chip_mut(&mut self) -> &mut dyn IrqChip140     fn as_irq_chip_mut(&mut self) -> &mut dyn IrqChip {
141         self
142     }
143 
get_vgic_version(&self) -> DeviceKind144     fn get_vgic_version(&self) -> DeviceKind {
145         self.device_kind
146     }
147 
finalize(&self) -> Result<()>148     fn finalize(&self) -> Result<()> {
149         Ok(())
150     }
151 }
152 
153 /// This IrqChip only works with Geniezone so we only implement it for GeniezoneVcpu.
154 impl IrqChip for GeniezoneKernelIrqChip {
155     /// Add a vcpu to the irq chip.
add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()>156     fn add_vcpu(&mut self, vcpu_id: usize, vcpu: &dyn Vcpu) -> Result<()> {
157         let vcpu: &GeniezoneVcpu = vcpu
158             .downcast_ref()
159             .expect("GeniezoneKernelIrqChip::add_vcpu called with non-GeniezoneVcpu");
160         self.vcpus.lock()[vcpu_id] = Some(vcpu.try_clone()?);
161         Ok(())
162     }
163 
164     /// Register an event with edge-trigger semantic that can trigger an interrupt
165     /// for a particular GSI.
register_edge_irq_event( &mut self, irq: u32, irq_event: &IrqEdgeEvent, _source: IrqEventSource, ) -> Result<Option<IrqEventIndex>>166     fn register_edge_irq_event(
167         &mut self,
168         irq: u32,
169         irq_event: &IrqEdgeEvent,
170         _source: IrqEventSource,
171     ) -> Result<Option<IrqEventIndex>> {
172         self.vm.register_irqfd(irq, irq_event.get_trigger(), None)?;
173         Ok(None)
174     }
175 
176     /// Unregister an event with edge-trigger semantic for a particular GSI.
unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()>177     fn unregister_edge_irq_event(&mut self, irq: u32, irq_event: &IrqEdgeEvent) -> Result<()> {
178         self.vm.unregister_irqfd(irq, irq_event.get_trigger())
179     }
180 
181     /// Register an event with level-trigger semantic that can trigger an interrupt
182     /// for a particular GSI.
register_level_irq_event( &mut self, irq: u32, irq_event: &IrqLevelEvent, _source: IrqEventSource, ) -> Result<Option<IrqEventIndex>>183     fn register_level_irq_event(
184         &mut self,
185         irq: u32,
186         irq_event: &IrqLevelEvent,
187         _source: IrqEventSource,
188     ) -> Result<Option<IrqEventIndex>> {
189         self.vm
190             .register_irqfd(irq, irq_event.get_trigger(), Some(irq_event.get_resample()))?;
191         Ok(None)
192     }
193 
194     /// Unregister an event with level-trigger semantic for a particular GSI.
unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()>195     fn unregister_level_irq_event(&mut self, irq: u32, irq_event: &IrqLevelEvent) -> Result<()> {
196         self.vm.unregister_irqfd(irq, irq_event.get_trigger())
197     }
198 
199     /// Route an IRQ line to an interrupt controller, or to a particular MSI vector.
route_irq(&mut self, route: IrqRoute) -> Result<()>200     fn route_irq(&mut self, route: IrqRoute) -> Result<()> {
201         let mut routes = self.routes.lock();
202         routes.retain(|r| r.gsi != route.gsi);
203 
204         routes.push(route);
205         Ok(())
206     }
207 
208     /// Replace all irq routes with the supplied routes
set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()>209     fn set_irq_routes(&mut self, routes: &[IrqRoute]) -> Result<()> {
210         let mut current_routes = self.routes.lock();
211         *current_routes = routes.to_vec();
212         Ok(())
213     }
214 
215     /// Return a vector of all registered irq numbers and their associated events and event
216     /// indices. These should be used by the main thread to wait for irq events.
217     /// For the GeniezoneKernelIrqChip, the kernel handles listening to irq events being triggered
218     /// by devices, so this function always returns an empty Vec.
irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, IrqEventSource, Event)>>219     fn irq_event_tokens(&self) -> Result<Vec<(IrqEventIndex, IrqEventSource, Event)>> {
220         Ok(Vec::new())
221     }
222 
223     /// Either assert or deassert an IRQ line.  Sends to either an interrupt controller, or does
224     /// a send_msi if the irq is associated with an MSI.
225     /// For the GeniezoneKernelIrqChip this simply calls the GZVM_SET_IRQ_LINE ioctl.
service_irq(&mut self, irq: u32, level: bool) -> Result<()>226     fn service_irq(&mut self, irq: u32, level: bool) -> Result<()> {
227         self.vm.set_irq_line(irq, level)
228     }
229 
230     /// Service an IRQ event by asserting then deasserting an IRQ line. The associated Event
231     /// that triggered the irq event will be read from. If the irq is associated with a resample
232     /// Event, then the deassert will only happen after an EOI is broadcast for a vector
233     /// associated with the irq line.
234     /// This function should never be called on GeniezoneKernelIrqChip.
service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()>235     fn service_irq_event(&mut self, _event_index: IrqEventIndex) -> Result<()> {
236         error!("service_irq_event should never be called for GeniezoneKernelIrqChip");
237         Ok(())
238     }
239 
240     /// Broadcast an end of interrupt.
241     /// This should never be called on a GeniezoneKernelIrqChip because a Geniezone vcpu should
242     /// never exit with the GZVM_EXIT_EOI_BROADCAST reason when an in-kernel irqchip exists.
broadcast_eoi(&self, _vector: u8) -> Result<()>243     fn broadcast_eoi(&self, _vector: u8) -> Result<()> {
244         error!("broadcast_eoi should never be called for GeniezoneKernelIrqChip");
245         Ok(())
246     }
247 
248     /// Injects any pending interrupts for `vcpu`.
249     /// For GeniezoneKernelIrqChip this is a no-op because Geniezone is responsible for injecting
250     /// all interrupts.
inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()>251     fn inject_interrupts(&self, _vcpu: &dyn Vcpu) -> Result<()> {
252         Ok(())
253     }
254 
255     /// Notifies the irq chip that the specified VCPU has executed a halt instruction.
256     /// For GeniezoneKernelIrqChip this is a no-op because Geniezone handles VCPU blocking.
halted(&self, _vcpu_id: usize)257     fn halted(&self, _vcpu_id: usize) {}
258 
259     /// Blocks until `vcpu` is in a runnable state or until interrupted by
260     /// `IrqChip::kick_halted_vcpus`.  Returns `VcpuRunState::Runnable if vcpu is runnable, or
261     /// `VcpuRunState::Interrupted` if the wait was interrupted.
262     /// For GeniezoneKernelIrqChip this is a no-op and always returns Runnable because Geniezone
263     /// handles VCPU blocking.
wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState>264     fn wait_until_runnable(&self, _vcpu: &dyn Vcpu) -> Result<VcpuRunState> {
265         Ok(VcpuRunState::Runnable)
266     }
267 
268     /// Makes unrunnable VCPUs return immediately from `wait_until_runnable`.
269     /// For GeniezoneKernelIrqChip this is a no-op because Geniezone handles VCPU blocking.
kick_halted_vcpus(&self)270     fn kick_halted_vcpus(&self) {}
271 
272     /// Get the current MP state of the specified VCPU.
get_mp_state(&self, _vcpu_id: usize) -> Result<MPState>273     fn get_mp_state(&self, _vcpu_id: usize) -> Result<MPState> {
274         Err(Error::new(libc::ENOENT))
275     }
276 
277     /// Set the current MP state of the specified VCPU.
set_mp_state(&mut self, _vcpu_id: usize, _state: &MPState) -> Result<()>278     fn set_mp_state(&mut self, _vcpu_id: usize, _state: &MPState) -> Result<()> {
279         Err(Error::new(libc::ENOENT))
280     }
281 
282     /// Attempt to clone this IrqChip instance.
try_clone(&self) -> Result<Self>283     fn try_clone(&self) -> Result<Self> {
284         // Because the GeniezoneKernelIrqChip struct contains arch-specific fields we leave the
285         // cloning to arch-specific implementations
286         self.arch_try_clone()
287     }
288 
289     /// Finalize irqchip setup. Should be called once all devices have registered irq events and
290     /// been added to the io_bus and mmio_bus.
291     /// GeniezoneKernelIrqChip does not need to do anything here.
finalize_devices( &mut self, _resources: &mut SystemAllocator, _io_bus: &Bus, _mmio_bus: &Bus, ) -> Result<()>292     fn finalize_devices(
293         &mut self,
294         _resources: &mut SystemAllocator,
295         _io_bus: &Bus,
296         _mmio_bus: &Bus,
297     ) -> Result<()> {
298         Ok(())
299     }
300 
301     /// The GeniezoneKernelIrqChip doesn't process irq events itself so this function does nothing.
process_delayed_irq_events(&mut self) -> Result<()>302     fn process_delayed_irq_events(&mut self) -> Result<()> {
303         Ok(())
304     }
305 
irq_delayed_event_token(&self) -> Result<Option<Event>>306     fn irq_delayed_event_token(&self) -> Result<Option<Event>> {
307         Ok(None)
308     }
309 
check_capability(&self, _c: IrqChipCap) -> bool310     fn check_capability(&self, _c: IrqChipCap) -> bool {
311         false
312     }
313 }
314