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 serde::de::Error as SerdeError; 9 use serde::{Deserialize, Serialize}; 10 use serde_json; 11 use thiserror::Error; 12 13 use crate::{CpuState, HypervisorDeviceError, HypervisorVmError}; 14 15 /// Errors thrown while setting up the VGIC. 16 #[derive(Debug, Error)] 17 pub enum Error { 18 /// Error while calling KVM ioctl for setting up the global interrupt controller. 19 #[error("Failed creating GIC device: {0}")] 20 CreateGic(HypervisorVmError), 21 /// Error while setting device attributes for the GIC. 22 #[error("Failed setting device attributes for the GIC: {0}")] 23 SetDeviceAttribute(HypervisorDeviceError), 24 /// Error while getting device attributes for the GIC. 25 #[error("Failed getting device attributes for the GIC: {0}")] 26 GetDeviceAttribute(HypervisorDeviceError), 27 } 28 pub type Result<T> = result::Result<T, Error>; 29 30 #[derive(Debug)] 31 pub struct VgicConfig { 32 pub vcpu_count: u64, 33 pub dist_addr: u64, 34 pub dist_size: u64, 35 pub redists_addr: u64, 36 pub redists_size: u64, 37 pub msi_addr: u64, 38 pub msi_size: u64, 39 pub nr_irqs: u32, 40 } 41 42 #[derive(Clone, Serialize)] 43 pub enum GicState { 44 #[cfg(feature = "kvm")] 45 Kvm(crate::kvm::aarch64::gic::Gicv3ItsState), 46 } 47 48 impl<'de> Deserialize<'de> for GicState { 49 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> 50 where 51 D: serde::Deserializer<'de>, 52 { 53 // GicStateDefaultDeserialize is a helper enum that mirrors GicState but also derives the Deserialize trait. 54 // This enables backward-compatible deserialization of GicState, facilitating live-upgrade scenarios. 55 #[derive(Deserialize)] 56 pub enum GicStateDefaultDeserialize { 57 #[cfg(feature = "kvm")] 58 Kvm(crate::kvm::aarch64::gic::Gicv3ItsState), 59 } 60 61 const { 62 assert!( 63 std::mem::size_of::<GicStateDefaultDeserialize>() 64 == std::mem::size_of::<GicState>() 65 ) 66 }; 67 68 let value: serde_json::Value = Deserialize::deserialize(deserializer)?; 69 70 #[cfg(feature = "kvm")] 71 if let Ok(gicv3_its_state) = 72 crate::kvm::aarch64::gic::Gicv3ItsState::deserialize(value.clone()) 73 { 74 return Ok(GicState::Kvm(gicv3_its_state)); 75 } 76 77 if let Ok(gic_state_de) = GicStateDefaultDeserialize::deserialize(value.clone()) { 78 return match gic_state_de { 79 #[cfg(feature = "kvm")] 80 GicStateDefaultDeserialize::Kvm(state) => Ok(GicState::Kvm(state)), 81 }; 82 } 83 Err(SerdeError::custom("Failed to deserialize GicState")) 84 } 85 } 86 /// Hypervisor agnostic interface for a virtualized GIC 87 pub trait Vgic: Send + Sync { 88 /// Returns the fdt compatibility property of the device 89 fn fdt_compatibility(&self) -> &str; 90 91 /// Returns the maint_irq fdt property of the device 92 fn fdt_maint_irq(&self) -> u32; 93 94 /// Returns an array with GIC device properties 95 fn device_properties(&self) -> [u64; 4]; 96 97 /// Returns the number of vCPUs this GIC handles 98 fn vcpu_count(&self) -> u64; 99 100 /// Returns whether the GIC device is MSI compatible or not 101 fn msi_compatible(&self) -> bool; 102 103 /// Returns the MSI compatibility property of the device 104 fn msi_compatibility(&self) -> &str; 105 106 /// Returns the MSI reg property of the device 107 fn msi_properties(&self) -> [u64; 2]; 108 109 /// Get the values of GICR_TYPER for each vCPU. 110 fn set_gicr_typers(&mut self, vcpu_states: &[CpuState]); 111 112 /// Downcast the trait object to its concrete type. 113 fn as_any_concrete_mut(&mut self) -> &mut dyn Any; 114 115 /// Save the state of GICv3ITS. 116 fn state(&self) -> Result<GicState>; 117 118 /// Restore the state of GICv3ITS. 119 fn set_state(&mut self, state: &GicState) -> Result<()>; 120 121 /// Saves GIC internal data tables into RAM. 122 fn save_data_tables(&self) -> Result<()>; 123 } 124