xref: /cloud-hypervisor/hypervisor/src/arch/aarch64/gic.rs (revision 19d36c765fdf00be749d95b3e61028bc302d6d73)
1 // Copyright 2022 Arm Limited (or its affiliates). All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 
5 use std::any::Any;
6 use std::result;
7 
8 use thiserror::Error;
9 
10 use crate::{CpuState, GicState, HypervisorDeviceError, HypervisorVmError};
11 
12 /// Errors thrown while setting up the VGIC.
13 #[derive(Debug, Error)]
14 pub enum Error {
15     /// Error while calling KVM ioctl for setting up the global interrupt controller.
16     #[error("Failed creating GIC device: {0}")]
17     CreateGic(HypervisorVmError),
18     /// Error while setting device attributes for the GIC.
19     #[error("Failed setting device attributes for the GIC: {0}")]
20     SetDeviceAttribute(HypervisorDeviceError),
21     /// Error while getting device attributes for the GIC.
22     #[error("Failed getting device attributes for the GIC: {0}")]
23     GetDeviceAttribute(HypervisorDeviceError),
24 }
25 pub type Result<T> = result::Result<T, Error>;
26 
27 #[derive(Debug)]
28 pub struct VgicConfig {
29     pub vcpu_count: u64,
30     pub dist_addr: u64,
31     pub dist_size: u64,
32     pub redists_addr: u64,
33     pub redists_size: u64,
34     pub msi_addr: u64,
35     pub msi_size: u64,
36     pub nr_irqs: u32,
37 }
38 
39 /// Hypervisor agnostic interface for a virtualized GIC
40 pub trait Vgic: Send + Sync {
41     /// Returns the fdt compatibility property of the device
42     fn fdt_compatibility(&self) -> &str;
43 
44     /// Returns the maint_irq fdt property of the device
45     fn fdt_maint_irq(&self) -> u32;
46 
47     /// Returns an array with GIC device properties
48     fn device_properties(&self) -> [u64; 4];
49 
50     /// Returns the number of vCPUs this GIC handles
51     fn vcpu_count(&self) -> u64;
52 
53     /// Returns whether the GIC device is MSI compatible or not
54     fn msi_compatible(&self) -> bool;
55 
56     /// Returns the MSI compatibility property of the device
57     fn msi_compatibility(&self) -> &str;
58 
59     /// Returns the MSI reg property of the device
60     fn msi_properties(&self) -> [u64; 2];
61 
62     /// Get the values of GICR_TYPER for each vCPU.
63     fn set_gicr_typers(&mut self, vcpu_states: &[CpuState]);
64 
65     /// Downcast the trait object to its concrete type.
66     fn as_any_concrete_mut(&mut self) -> &mut dyn Any;
67 
68     /// Save the state of GICv3ITS.
69     fn state(&self) -> Result<GicState>;
70 
71     /// Restore the state of GICv3ITS.
72     fn set_state(&mut self, state: &GicState) -> Result<()>;
73 
74     /// Saves GIC internal data tables into RAM.
75     fn save_data_tables(&self) -> Result<()>;
76 }
77