|
| 1 | +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::io; |
| 5 | +use std::sync::LazyLock; |
| 6 | + |
| 7 | +use crate::bus::BusDevice; |
| 8 | +use crate::legacy::gic::GICDevice; |
| 9 | +use crate::legacy::irqchip::IrqChipT; |
| 10 | +use crate::Error as DeviceError; |
| 11 | + |
| 12 | +use hvf::bindings::{hv_gic_config_t, hv_ipa_t, hv_return_t, HV_SUCCESS}; |
| 13 | +use hvf::Error; |
| 14 | +use utils::eventfd::EventFd; |
| 15 | + |
| 16 | +// Device trees specific constants |
| 17 | +const ARCH_GIC_V3_MAINT_IRQ: u32 = 9; |
| 18 | + |
| 19 | +pub struct HvfGicBindings { |
| 20 | + hv_gic_create: |
| 21 | + libloading::Symbol<'static, unsafe extern "C" fn(hv_gic_config_t) -> hv_return_t>, |
| 22 | + hv_gic_config_create: libloading::Symbol<'static, unsafe extern "C" fn() -> hv_gic_config_t>, |
| 23 | + hv_gic_config_set_distributor_base: |
| 24 | + libloading::Symbol<'static, unsafe extern "C" fn(hv_gic_config_t, hv_ipa_t) -> hv_return_t>, |
| 25 | + hv_gic_config_set_redistributor_base: |
| 26 | + libloading::Symbol<'static, unsafe extern "C" fn(hv_gic_config_t, hv_ipa_t) -> hv_return_t>, |
| 27 | + hv_gic_get_distributor_size: |
| 28 | + libloading::Symbol<'static, unsafe extern "C" fn(*mut usize) -> hv_return_t>, |
| 29 | + hv_gic_get_redistributor_size: |
| 30 | + libloading::Symbol<'static, unsafe extern "C" fn(*mut usize) -> hv_return_t>, |
| 31 | + hv_gic_set_spi: libloading::Symbol<'static, unsafe extern "C" fn(u32, bool) -> hv_return_t>, |
| 32 | +} |
| 33 | + |
| 34 | +pub struct HvfGicV3 { |
| 35 | + bindings: HvfGicBindings, |
| 36 | + |
| 37 | + /// GIC device properties, to be used for setting up the fdt entry |
| 38 | + properties: [u64; 4], |
| 39 | + |
| 40 | + /// Number of CPUs handled by the device |
| 41 | + vcpu_count: u64, |
| 42 | +} |
| 43 | + |
| 44 | +static HVF: LazyLock<libloading::Library> = LazyLock::new(|| unsafe { |
| 45 | + libloading::Library::new( |
| 46 | + "/System/Library/Frameworks/Hypervisor.framework/Versions/A/Hypervisor", |
| 47 | + ) |
| 48 | + .unwrap() |
| 49 | +}); |
| 50 | + |
| 51 | +impl HvfGicV3 { |
| 52 | + pub fn new(vcpu_count: u64) -> Result<Self, Error> { |
| 53 | + let bindings = unsafe { |
| 54 | + HvfGicBindings { |
| 55 | + hv_gic_create: HVF.get(b"hv_gic_create").map_err(Error::FindSymbol)?, |
| 56 | + hv_gic_config_create: HVF |
| 57 | + .get(b"hv_gic_config_create") |
| 58 | + .map_err(Error::FindSymbol)?, |
| 59 | + hv_gic_config_set_distributor_base: HVF |
| 60 | + .get(b"hv_gic_config_set_distributor_base") |
| 61 | + .map_err(Error::FindSymbol)?, |
| 62 | + hv_gic_config_set_redistributor_base: HVF |
| 63 | + .get(b"hv_gic_config_set_redistributor_base") |
| 64 | + .map_err(Error::FindSymbol)?, |
| 65 | + hv_gic_get_distributor_size: HVF |
| 66 | + .get(b"hv_gic_get_distributor_size") |
| 67 | + .map_err(Error::FindSymbol)?, |
| 68 | + hv_gic_get_redistributor_size: HVF |
| 69 | + .get(b"hv_gic_get_redistributor_size") |
| 70 | + .map_err(Error::FindSymbol)?, |
| 71 | + hv_gic_set_spi: HVF.get(b"hv_gic_set_spi").map_err(Error::FindSymbol)?, |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + let mut dist_size: usize = 0; |
| 76 | + let ret = unsafe { (bindings.hv_gic_get_distributor_size)(&mut dist_size) }; |
| 77 | + if ret != HV_SUCCESS { |
| 78 | + return Err(Error::VmCreate); |
| 79 | + } |
| 80 | + let dist_size = dist_size as u64; |
| 81 | + |
| 82 | + let mut redist_size: usize = 0; |
| 83 | + let ret = unsafe { (bindings.hv_gic_get_redistributor_size)(&mut redist_size) }; |
| 84 | + if ret != HV_SUCCESS { |
| 85 | + return Err(Error::VmCreate); |
| 86 | + } |
| 87 | + |
| 88 | + let redists_size = redist_size as u64 * vcpu_count; |
| 89 | + let dist_addr = arch::MMIO_MEM_START - dist_size - redists_size; |
| 90 | + let redists_addr = arch::MMIO_MEM_START - redists_size; |
| 91 | + |
| 92 | + let gic_config = unsafe { (bindings.hv_gic_config_create)() }; |
| 93 | + let ret = unsafe { (bindings.hv_gic_config_set_distributor_base)(gic_config, dist_addr) }; |
| 94 | + if ret != HV_SUCCESS { |
| 95 | + return Err(Error::VmCreate); |
| 96 | + } |
| 97 | + |
| 98 | + let ret = unsafe { |
| 99 | + (bindings.hv_gic_config_set_redistributor_base)( |
| 100 | + gic_config, |
| 101 | + arch::MMIO_MEM_START - redists_size, |
| 102 | + ) |
| 103 | + }; |
| 104 | + if ret != HV_SUCCESS { |
| 105 | + return Err(Error::VmCreate); |
| 106 | + } |
| 107 | + |
| 108 | + let ret = unsafe { (bindings.hv_gic_create)(gic_config) }; |
| 109 | + if ret != HV_SUCCESS { |
| 110 | + return Err(Error::VmCreate); |
| 111 | + } |
| 112 | + |
| 113 | + Ok(Self { |
| 114 | + bindings, |
| 115 | + properties: [dist_addr, dist_size, redists_addr, redists_size], |
| 116 | + vcpu_count, |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl IrqChipT for HvfGicV3 { |
| 122 | + fn get_mmio_addr(&self) -> u64 { |
| 123 | + 0 |
| 124 | + } |
| 125 | + |
| 126 | + fn get_mmio_size(&self) -> u64 { |
| 127 | + 0 |
| 128 | + } |
| 129 | + |
| 130 | + fn set_irq( |
| 131 | + &self, |
| 132 | + irq_line: Option<u32>, |
| 133 | + _interrupt_evt: Option<&EventFd>, |
| 134 | + ) -> Result<(), DeviceError> { |
| 135 | + if let Some(irq_line) = irq_line { |
| 136 | + let ret = unsafe { (self.bindings.hv_gic_set_spi)(irq_line, true) }; |
| 137 | + if ret != HV_SUCCESS { |
| 138 | + Err(DeviceError::FailedSignalingUsedQueue(io::Error::new( |
| 139 | + io::ErrorKind::Other, |
| 140 | + "HVF returned error when setting SPI", |
| 141 | + ))) |
| 142 | + } else { |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | + } else { |
| 146 | + Err(DeviceError::FailedSignalingUsedQueue(io::Error::new( |
| 147 | + io::ErrorKind::InvalidData, |
| 148 | + "IRQ not line configured", |
| 149 | + ))) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl BusDevice for HvfGicV3 { |
| 155 | + fn read(&mut self, _vcpuid: u64, _offset: u64, _data: &mut [u8]) { |
| 156 | + unreachable!("MMIO operations are managed in-kernel"); |
| 157 | + } |
| 158 | + |
| 159 | + fn write(&mut self, _vcpuid: u64, _offset: u64, _data: &[u8]) { |
| 160 | + unreachable!("MMIO operations are managed in-kernel"); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl GICDevice for HvfGicV3 { |
| 165 | + fn device_properties(&self) -> Vec<u64> { |
| 166 | + self.properties.to_vec() |
| 167 | + } |
| 168 | + |
| 169 | + fn vcpu_count(&self) -> u64 { |
| 170 | + self.vcpu_count |
| 171 | + } |
| 172 | + |
| 173 | + fn fdt_compatibility(&self) -> String { |
| 174 | + "arm,gic-v3".to_string() |
| 175 | + } |
| 176 | + |
| 177 | + fn fdt_maint_irq(&self) -> u32 { |
| 178 | + ARCH_GIC_V3_MAINT_IRQ |
| 179 | + } |
| 180 | + |
| 181 | + fn version(&self) -> u32 { |
| 182 | + 7 |
| 183 | + } |
| 184 | +} |
0 commit comments