feat(kvm/aarch64)!: assign a DevID to MsiSender

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu 2024-06-29 11:57:19 -07:00 committed by Lencerf
parent 3160823378
commit 6ca2e25b89
5 changed files with 35 additions and 6 deletions

View file

@ -266,7 +266,10 @@ pub trait Vm {
type IoeventFdRegistry: IoeventFdRegistry;
fn create_vcpu(&self, id: u32) -> Result<Self::Vcpu, Error>;
fn create_irq_sender(&self, pin: u8) -> Result<Self::IrqSender, Error>;
fn create_msi_sender(&self) -> Result<Self::MsiSender>;
fn create_msi_sender(
&self,
#[cfg(target_arch = "aarch64")] devid: u32,
) -> Result<Self::MsiSender>;
fn create_vm_memory(&mut self) -> Result<Self::Memory, Error>;
fn create_ioeventfd_registry(&self) -> Result<Self::IoeventFdRegistry>;
fn stop_vcpu<T>(id: u32, handle: &JoinHandle<T>) -> Result<(), Error>;

View file

@ -236,7 +236,7 @@ impl Vm for HvfVm {
fn create_ioeventfd_registry(&self) -> Result<Self::IoeventFdRegistry> {
unimplemented!()
}
fn create_msi_sender(&self) -> Result<Self::MsiSender> {
fn create_msi_sender(&self, _devid: u32) -> Result<Self::MsiSender> {
unimplemented!()
}
fn create_vcpu(&self, id: u32) -> Result<Self::Vcpu> {

View file

@ -428,13 +428,22 @@ impl<const N: usize> Debug for KvmIrqRouting<N> {
}
}
bitflags! {
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct KvmMsiFlag: u32 {
#[cfg(target_arch = "aarch64")]
const VALID_DEVID = 1 << 0;
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
pub struct KvmMsi {
pub address_lo: u32,
pub address_hi: u32,
pub data: u32,
pub flags: u32,
pub flags: KvmMsiFlag,
pub devid: u32,
pub pad: [u8; 12usize],
}

View file

@ -31,6 +31,8 @@ use snafu::ResultExt;
#[cfg(target_arch = "x86_64")]
use crate::arch::sev::{SnpPageType, SnpPolicy};
use crate::ffi;
#[cfg(target_arch = "aarch64")]
use crate::hv::kvm::bindings::KvmMsiFlag;
#[cfg(target_arch = "x86_64")]
use crate::hv::kvm::bindings::KVM_IRQCHIP_IOAPIC;
use crate::hv::kvm::bindings::{
@ -419,6 +421,8 @@ const MAX_GSI_ROUTES: usize = 256;
#[derive(Debug)]
pub struct KvmMsiSender {
vm: Arc<VmInner>,
#[cfg(target_arch = "aarch64")]
devid: u32,
}
impl MsiSender for KvmMsiSender {
@ -429,6 +433,10 @@ impl MsiSender for KvmMsiSender {
address_lo: addr as u32,
address_hi: (addr >> 32) as u32,
data,
#[cfg(target_arch = "aarch64")]
devid: self.devid,
#[cfg(target_arch = "aarch64")]
flags: KvmMsiFlag::VALID_DEVID,
..Default::default()
};
unsafe { kvm_signal_msi(&self.vm, &kvm_msi) }.context(error::SendInterrupt)?;
@ -599,7 +607,10 @@ impl Vm for KvmVm {
})
}
fn create_msi_sender(&self) -> Result<Self::MsiSender> {
fn create_msi_sender(
&self,
#[cfg(target_arch = "aarch64")] devid: u32,
) -> Result<Self::MsiSender> {
if self.vm.check_extension(KvmCap::SIGNAL_MSI)? == 0 {
return error::Capability {
cap: "KVM_CAP_SIGNAL_MSI",
@ -608,6 +619,8 @@ impl Vm for KvmVm {
}
Ok(KvmMsiSender {
vm: self.vm.clone(),
#[cfg(target_arch = "aarch64")]
devid,
})
}

View file

@ -211,6 +211,7 @@ where
D: Virtio,
{
let name = Arc::new(name);
let bdf = self.board.pci_bus.reserve(None, name.clone()).unwrap();
let dev = param.build(name.clone())?;
let registry = self.board.vm.create_ioeventfd_registry()?;
let virtio_dev = VirtioDevice::new(
@ -220,11 +221,14 @@ where
&registry,
self.board.config.coco.is_some(),
)?;
let msi_sender = self.board.vm.create_msi_sender()?;
let msi_sender = self.board.vm.create_msi_sender(
#[cfg(target_arch = "aarch64")]
u32::from(bdf.0),
)?;
let dev = VirtioPciDevice::new(virtio_dev, msi_sender, registry)?;
let dev = Arc::new(dev);
let pci_dev = PciDevice::new(name.clone(), dev.clone());
self.add_pci_dev(None, pci_dev)?;
self.add_pci_dev(Some(bdf), pci_dev)?;
Ok(dev)
}