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