// Copyright © 2019 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause // use devices::interrupt_controller::InterruptController; use hypervisor::IrqRoutingEntry; use std::collections::HashMap; use std::io; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use vm_allocator::SystemAllocator; use vm_device::interrupt::{ InterruptIndex, InterruptManager, InterruptSourceConfig, InterruptSourceGroup, LegacyIrqGroupConfig, MsiIrqGroupConfig, }; use vmm_sys_util::eventfd::EventFd; /// Reuse std::io::Result to simplify interoperability among crates. pub type Result = std::io::Result; struct InterruptRoute { gsi: u32, irq_fd: EventFd, registered: AtomicBool, } impl InterruptRoute { pub fn new(allocator: &mut SystemAllocator) -> Result { let irq_fd = EventFd::new(libc::EFD_NONBLOCK)?; let gsi = allocator .allocate_gsi() .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed allocating new GSI"))?; Ok(InterruptRoute { gsi, irq_fd, registered: AtomicBool::new(false), }) } pub fn enable(&self, vm: &Arc) -> Result<()> { if !self.registered.load(Ordering::Acquire) { vm.register_irqfd(&self.irq_fd, self.gsi).map_err(|e| { io::Error::new( io::ErrorKind::Other, format!("Failed registering irq_fd: {}", e), ) })?; // Update internals to track the irq_fd as "registered". self.registered.store(true, Ordering::Release); } Ok(()) } pub fn disable(&self, vm: &Arc) -> Result<()> { if self.registered.load(Ordering::Acquire) { vm.unregister_irqfd(&self.irq_fd, self.gsi).map_err(|e| { io::Error::new( io::ErrorKind::Other, format!("Failed unregistering irq_fd: {}", e), ) })?; // Update internals to track the irq_fd as "unregistered". self.registered.store(false, Ordering::Release); } Ok(()) } pub fn trigger(&self) -> Result<()> { self.irq_fd.write(1) } pub fn notifier(&self) -> Option { Some( self.irq_fd .try_clone() .expect("Failed cloning interrupt's EventFd"), ) } } pub struct RoutingEntry { route: IrqRoutingEntry, masked: bool, } pub struct MsiInterruptGroup { vm: Arc, gsi_msi_routes: Arc>>, irq_routes: HashMap, } impl MsiInterruptGroup { fn set_gsi_routes(&self, routes: &HashMap) -> Result<()> { let mut entry_vec: Vec = Vec::new(); for (_, entry) in routes.iter() { if entry.masked { continue; } entry_vec.push(entry.route); } self.vm.set_gsi_routing(&entry_vec).map_err(|e| { io::Error::new( io::ErrorKind::Other, format!("Failed setting GSI routing: {}", e), ) }) } } impl MsiInterruptGroup { fn new( vm: Arc, gsi_msi_routes: Arc>>, irq_routes: HashMap, ) -> Self { MsiInterruptGroup { vm, gsi_msi_routes, irq_routes, } } } impl InterruptSourceGroup for MsiInterruptGroup { fn enable(&self) -> Result<()> { for (_, route) in self.irq_routes.iter() { route.enable(&self.vm)?; } Ok(()) } fn disable(&self) -> Result<()> { for (_, route) in self.irq_routes.iter() { route.disable(&self.vm)?; } Ok(()) } fn trigger(&self, index: InterruptIndex) -> Result<()> { if let Some(route) = self.irq_routes.get(&index) { return route.trigger(); } Err(io::Error::new( io::ErrorKind::Other, format!("trigger: Invalid interrupt index {}", index), )) } fn notifier(&self, index: InterruptIndex) -> Option { if let Some(route) = self.irq_routes.get(&index) { return route.notifier(); } None } fn update( &self, index: InterruptIndex, config: InterruptSourceConfig, masked: bool, ) -> Result<()> { if let Some(route) = self.irq_routes.get(&index) { let entry = RoutingEntry { route: self.vm.make_routing_entry(route.gsi, &config), masked, }; if masked { route.disable(&self.vm)?; } else { route.enable(&self.vm)?; } let mut routes = self.gsi_msi_routes.lock().unwrap(); routes.insert(route.gsi, entry); return self.set_gsi_routes(&routes); } Err(io::Error::new( io::ErrorKind::Other, format!("update: Invalid interrupt index {}", index), )) } } pub struct LegacyUserspaceInterruptGroup { ioapic: Arc>, irq: u32, } impl LegacyUserspaceInterruptGroup { fn new(ioapic: Arc>, irq: u32) -> Self { LegacyUserspaceInterruptGroup { ioapic, irq } } } impl InterruptSourceGroup for LegacyUserspaceInterruptGroup { fn trigger(&self, _index: InterruptIndex) -> Result<()> { self.ioapic .lock() .unwrap() .service_irq(self.irq as usize) .map_err(|e| { io::Error::new( io::ErrorKind::Other, format!("failed to inject IRQ #{}: {:?}", self.irq, e), ) }) } fn update( &self, _index: InterruptIndex, _config: InterruptSourceConfig, _masked: bool, ) -> Result<()> { Ok(()) } fn notifier(&self, _index: InterruptIndex) -> Option { self.ioapic.lock().unwrap().notifier(self.irq as usize) } } pub struct LegacyUserspaceInterruptManager { ioapic: Arc>, } pub struct MsiInterruptManager { allocator: Arc>, vm: Arc, gsi_msi_routes: Arc>>, } impl LegacyUserspaceInterruptManager { pub fn new(ioapic: Arc>) -> Self { LegacyUserspaceInterruptManager { ioapic } } } impl MsiInterruptManager { pub fn new(allocator: Arc>, vm: Arc) -> Self { // Create a shared list of GSI that can be shared through all PCI // devices. This way, we can maintain the full list of used GSI, // preventing one device from overriding interrupts setting from // another one. let gsi_msi_routes = Arc::new(Mutex::new(HashMap::new())); MsiInterruptManager { allocator, vm, gsi_msi_routes, } } } impl InterruptManager for LegacyUserspaceInterruptManager { type GroupConfig = LegacyIrqGroupConfig; fn create_group(&self, config: Self::GroupConfig) -> Result> { Ok(Arc::new(LegacyUserspaceInterruptGroup::new( self.ioapic.clone(), config.irq as u32, ))) } fn destroy_group(&self, _group: Arc) -> Result<()> { Ok(()) } } impl InterruptManager for MsiInterruptManager { type GroupConfig = MsiIrqGroupConfig; fn create_group(&self, config: Self::GroupConfig) -> Result> { let mut allocator = self.allocator.lock().unwrap(); let mut irq_routes: HashMap = HashMap::with_capacity(config.count as usize); for i in config.base..config.base + config.count { irq_routes.insert(i, InterruptRoute::new(&mut allocator)?); } Ok(Arc::new(MsiInterruptGroup::new( self.vm.clone(), self.gsi_msi_routes.clone(), irq_routes, ))) } fn destroy_group(&self, _group: Arc) -> Result<()> { Ok(()) } }