|
| 1 | +//! mvien register |
| 2 | +
|
| 3 | +use crate::bits::{bf_extract, bf_insert}; |
| 4 | +use riscv_pac::result::{Error, Result}; |
| 5 | +use riscv_pac::InterruptNumber; |
| 6 | + |
| 7 | +#[cfg(target_arch = "riscv32")] |
| 8 | +const MASK: usize = 0xffff_e222; |
| 9 | +#[cfg(not(target_arch = "riscv32"))] |
| 10 | +const MASK: usize = 0xffff_ffff_ffff_e222; |
| 11 | + |
| 12 | +read_write_csr! { |
| 13 | + /// `mvien` register |
| 14 | + Mvien: 0x308, |
| 15 | + mask: MASK, |
| 16 | +} |
| 17 | + |
| 18 | +read_write_csr_field! { |
| 19 | + Mvien, |
| 20 | + /// Alias of `mie.SSIE` |
| 21 | + ssoft: 1, |
| 22 | +} |
| 23 | + |
| 24 | +read_write_csr_field! { |
| 25 | + Mvien, |
| 26 | + /// Alias of `mie.STIE` |
| 27 | + stimer: 5, |
| 28 | +} |
| 29 | + |
| 30 | +read_write_csr_field! { |
| 31 | + Mvien, |
| 32 | + /// Alias of `mie.SEIE` |
| 33 | + sext: 9, |
| 34 | +} |
| 35 | + |
| 36 | +impl Mvien { |
| 37 | + /// Represents the minimum interrupt of the unlabelled virtual interrupt range. |
| 38 | + pub const MIN_INTERRUPT: usize = 13; |
| 39 | + /// Represents the maximum interrupt of the unlabelled virtual interrupt range. |
| 40 | + #[cfg(target_arch = "riscv32")] |
| 41 | + pub const MAX_INTERRUPT: usize = 31; |
| 42 | + /// Represents the maximum interrupt of the unlabelled virtual interrupt range. |
| 43 | + #[cfg(not(target_arch = "riscv32"))] |
| 44 | + pub const MAX_INTERRUPT: usize = 63; |
| 45 | + |
| 46 | + /// Gets whether the interrupt number is a valid virtual interrupt. |
| 47 | + #[inline] |
| 48 | + pub const fn is_valid_interrupt(int: usize) -> bool { |
| 49 | + matches!(int, 1 | 5 | 9 | Self::MIN_INTERRUPT..=Self::MAX_INTERRUPT) |
| 50 | + } |
| 51 | + |
| 52 | + /// Check if a specific core interrupt source is enabled. |
| 53 | + /// |
| 54 | + /// Returns `Error` if the interrupt number is invalid. |
| 55 | + #[inline] |
| 56 | + pub fn is_enabled<I: InterruptNumber>(&self, interrupt: I) -> bool { |
| 57 | + let n = interrupt.number(); |
| 58 | + |
| 59 | + Self::is_valid_interrupt(n) && bf_extract(self.bits, n, 1) != 0 |
| 60 | + } |
| 61 | + |
| 62 | + /// Enable a specific core interrupt source. |
| 63 | + /// |
| 64 | + /// Returns `Error` if the interrupt number is invalid. |
| 65 | + #[inline] |
| 66 | + pub fn enable<I: InterruptNumber>(&mut self, interrupt: I) -> Result<()> { |
| 67 | + let n = interrupt.number(); |
| 68 | + |
| 69 | + if Self::is_valid_interrupt(n) { |
| 70 | + self.bits = bf_insert(self.bits, n, 1, 1); |
| 71 | + Ok(()) |
| 72 | + } else { |
| 73 | + Err(Error::InvalidVariant(n)) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Disable a specific core interrupt source. |
| 78 | + /// |
| 79 | + /// Returns `Error` if the interrupt number is invalid. |
| 80 | + #[inline] |
| 81 | + pub fn disable<I: InterruptNumber>(&mut self, interrupt: I) -> Result<()> { |
| 82 | + let n = interrupt.number(); |
| 83 | + |
| 84 | + if Self::is_valid_interrupt(n) { |
| 85 | + self.bits = bf_insert(self.bits, n, 1, 0); |
| 86 | + Ok(()) |
| 87 | + } else { |
| 88 | + Err(Error::InvalidVariant(n)) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +set!(0x308); |
| 94 | +clear!(0x308); |
| 95 | + |
| 96 | +set_clear_csr!( |
| 97 | + /// Supervisor Software Interrupt Enable |
| 98 | + , set_ssoft, clear_ssoft, 1 << 1); |
| 99 | +set_clear_csr!( |
| 100 | + /// Supervisor Timer Interrupt Enable |
| 101 | + , set_stimer, clear_stimer, 1 << 5); |
| 102 | +set_clear_csr!( |
| 103 | + /// Supervisor External Interrupt Enable |
| 104 | + , set_sext, clear_sext, 1 << 9); |
| 105 | + |
| 106 | +read_composite_csr!(super::mvienh::read().bits(), read().bits()); |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + |
| 112 | + /// Represents a custom set of virtual interrupts. |
| 113 | + /// |
| 114 | + /// NOTE: a real implementation may want to enumerate the valid virtual interrupt variants. |
| 115 | + #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 116 | + pub struct VirtualInterrupt(usize); |
| 117 | + |
| 118 | + /// SAFETY: `VirtualInterrupt` represents the virtual RISC-V interrupts |
| 119 | + unsafe impl InterruptNumber for VirtualInterrupt { |
| 120 | + const MAX_INTERRUPT_NUMBER: usize = Mvien::MAX_INTERRUPT; |
| 121 | + |
| 122 | + #[inline] |
| 123 | + fn number(self) -> usize { |
| 124 | + self.0 |
| 125 | + } |
| 126 | + |
| 127 | + #[inline] |
| 128 | + fn from_number(value: usize) -> Result<Self> { |
| 129 | + if Mvien::is_valid_interrupt(value) { |
| 130 | + Ok(Self(value)) |
| 131 | + } else { |
| 132 | + Err(Error::InvalidVariant(value)) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn test_mvien() { |
| 139 | + let mut m = Mvien::from_bits(0); |
| 140 | + |
| 141 | + test_csr_field!(m, ssoft); |
| 142 | + test_csr_field!(m, stimer); |
| 143 | + test_csr_field!(m, sext); |
| 144 | + |
| 145 | + (0..=VirtualInterrupt::MAX_INTERRUPT_NUMBER) |
| 146 | + .filter_map(|n| VirtualInterrupt::from_number(n).ok()) |
| 147 | + .for_each(|int| { |
| 148 | + assert!(!m.is_enabled(int)); |
| 149 | + |
| 150 | + assert!(m.enable(int).is_ok()); |
| 151 | + assert!(m.is_enabled(int)); |
| 152 | + |
| 153 | + assert!(m.disable(int).is_ok()); |
| 154 | + assert!(!m.is_enabled(int)); |
| 155 | + }); |
| 156 | + } |
| 157 | +} |
0 commit comments