|
| 1 | +use std::sync::Mutex; |
| 2 | + |
| 3 | +use thiserror::Error; |
| 4 | +use vm_utils::range_allocator::RangeAllocator; |
| 5 | + |
| 6 | +#[derive(Error, Debug)] |
| 7 | +pub enum InterruptManagerError { |
| 8 | + #[error("Invalid argument")] |
| 9 | + InvalidArgument, |
| 10 | + |
| 11 | + #[error("Failed to reserve irq {0}")] |
| 12 | + ReserveIrq(u32), |
| 13 | + |
| 14 | + #[error("Failed to allocate irq")] |
| 15 | + AllocateIrq, |
| 16 | + |
| 17 | + #[error("Failed to allocate gsi")] |
| 18 | + AllocateGsi, |
| 19 | +} |
| 20 | + |
| 21 | +struct Allocator(Mutex<RangeAllocator<u32>>); |
| 22 | + |
| 23 | +impl Allocator { |
| 24 | + fn new(start: u32, len: usize) -> Result<Self, InterruptManagerError> { |
| 25 | + let mut irq_allocator = RangeAllocator::<u32>::default(); |
| 26 | + irq_allocator |
| 27 | + .insert(start, len) |
| 28 | + .map_err(|_| InterruptManagerError::InvalidArgument)?; |
| 29 | + Ok(Allocator(Mutex::new(irq_allocator))) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub struct InterruptManager { |
| 34 | + // We can use bitmap for better performance. |
| 35 | + irq_allocator: Allocator, |
| 36 | + #[cfg(target_arch = "x86_64")] |
| 37 | + gsi_allocator: Allocator, |
| 38 | +} |
| 39 | + |
| 40 | +impl InterruptManager { |
| 41 | + pub fn new( |
| 42 | + irq_start: u32, |
| 43 | + irq_len: usize, |
| 44 | + #[cfg(target_arch = "x86_64")] gsi_start: u32, |
| 45 | + #[cfg(target_arch = "x86_64")] gsi_len: usize, |
| 46 | + ) -> Result<Self, InterruptManagerError> { |
| 47 | + let irq_allocator = Allocator::new(irq_start, irq_len)?; |
| 48 | + |
| 49 | + #[cfg(target_arch = "x86_64")] |
| 50 | + let gsi_allocator = Allocator::new(gsi_start, gsi_len)?; |
| 51 | + |
| 52 | + Ok(InterruptManager { |
| 53 | + irq_allocator, |
| 54 | + #[cfg(target_arch = "x86_64")] |
| 55 | + gsi_allocator, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + pub fn reserve_irq(&self, irq: u32) -> Result<(), InterruptManagerError> { |
| 60 | + let mut irq_allocator = self.irq_allocator.0.lock().unwrap(); |
| 61 | + let _ = irq_allocator |
| 62 | + .reserve(irq, 1) |
| 63 | + .map_err(|_| InterruptManagerError::ReserveIrq(irq))?; |
| 64 | + |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn allocate_irq(&self) -> Result<u32, InterruptManagerError> { |
| 69 | + let mut irq_allocator = self.irq_allocator.0.lock().unwrap(); |
| 70 | + let range = irq_allocator |
| 71 | + .alloc(1) |
| 72 | + .map_err(|_| InterruptManagerError::AllocateIrq)?; |
| 73 | + |
| 74 | + Ok(range.start) |
| 75 | + } |
| 76 | + |
| 77 | + #[cfg(target_arch = "x86_64")] |
| 78 | + pub fn allocate_gsi(&self) -> Result<u32, InterruptManagerError> { |
| 79 | + let mut gsi_allocator = self.gsi_allocator.0.lock().unwrap(); |
| 80 | + let range = gsi_allocator |
| 81 | + .alloc(1) |
| 82 | + .map_err(|_| InterruptManagerError::AllocateGsi)?; |
| 83 | + |
| 84 | + Ok(range.start) |
| 85 | + } |
| 86 | + |
| 87 | + #[cfg(target_arch = "aarch64")] |
| 88 | + pub fn allocate_gsi(&self) -> Result<u32, InterruptManagerError> { |
| 89 | + self.allocate_irq() |
| 90 | + .map_err(|_| InterruptManagerError::AllocateGsi) |
| 91 | + } |
| 92 | +} |
0 commit comments