xref: /cloud-hypervisor/hypervisor/src/arch/riscv64/aia.rs (revision 30cf1eed5e63499f3101ed320fc384b59c60fc6b)
1 // Copyright © 2024 Institute of Software, CAS. 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::{AiaState, HypervisorDeviceError, HypervisorVmError};
11 
12 /// Errors thrown while setting up the VAIA.
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 AIA device: {0}")]
17     CreateAia(HypervisorVmError),
18     /// Error while setting device attributes for the AIA.
19     #[error("Failed setting device attributes for the AIA: {0}")]
20     SetDeviceAttribute(HypervisorDeviceError),
21     /// Error while getting device attributes for the AIA.
22     #[error("Failed getting device attributes for the AIA: {0}")]
23     GetDeviceAttribute(HypervisorDeviceError),
24 }
25 pub type Result<T> = result::Result<T, Error>;
26 
27 #[derive(Debug)]
28 pub struct VaiaConfig {
29     pub vcpu_count: u64,
30     pub aplic_addr: u64,
31     pub aplic_size: u64,
32     pub imsic_addr: u64,
33     pub imsic_size: u64,
34     pub nr_irqs: u32,
35 }
36 
37 /// Hypervisor agnostic interface for a virtualized AIA
38 pub trait Vaia: Send + Sync {
39     /// Returns the compatibility property of APLIC
40     fn aplic_compatibility(&self) -> &str;
41 
42     /// Returns an array with APLIC device properties
43     fn aplic_properties(&self) -> [u64; 4];
44 
45     /// Returns the compatibility property of IMSIC
46     fn imsic_compatibility(&self) -> &str;
47 
48     /// Returns an array with IMSIC device properties
49     fn imsic_properties(&self) -> [u64; 4];
50 
51     /// Returns the number of vCPUs this AIA handles
52     fn vcpu_count(&self) -> u64;
53 
54     /// Returns whether the AIA device is MSI compatible or not
55     fn msi_compatible(&self) -> bool;
56 
57     /// Downcast the trait object to its concrete type.
58     fn as_any_concrete_mut(&mut self) -> &mut dyn Any;
59 
60     /// Save the state of AiaImsics.
61     fn state(&self) -> Result<AiaState>;
62 
63     /// Restore the state of AiaImsics.
64     fn set_state(&mut self, state: &AiaState) -> Result<()>;
65 }
66