1use alloc::vec::Vec;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct IrqSourceInfo {
5 pub id: usize,
6 pub queues: IdList,
7}
8
9impl IrqSourceInfo {
10 pub const fn new(id: usize, queues: IdList) -> Self {
11 Self { id, queues }
12 }
13
14 pub const fn legacy(queues: IdList) -> Self {
15 Self { id: 0, queues }
16 }
17}
18
19pub type IrqSourceList = Vec<IrqSourceInfo>;
20
21pub trait IrqHandler: Send + Sync + 'static {
22 fn handle_irq(&self) -> Event;
23}
24
25#[repr(transparent)]
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct IdList(u64);
28
29impl IdList {
30 pub const fn none() -> Self {
31 Self(0)
32 }
33
34 pub const fn from_bits(bits: u64) -> Self {
35 Self(bits)
36 }
37
38 pub const fn bits(self) -> u64 {
39 self.0
40 }
41
42 pub fn contains(&self, id: usize) -> bool {
43 id < 64 && (self.0 & (1 << id)) != 0
44 }
45
46 pub fn insert(&mut self, id: usize) {
47 if id < 64 {
48 self.0 |= 1 << id;
49 }
50 }
51
52 pub fn remove(&mut self, id: usize) {
53 if id < 64 {
54 self.0 &= !(1 << id);
55 }
56 }
57
58 pub fn iter(&self) -> impl Iterator<Item = usize> {
59 (0..64).filter(move |i| self.contains(*i))
60 }
61}
62
63#[derive(Debug, Clone, Copy)]
64pub struct Event {
65 pub queues: IdList,
66}
67
68impl Event {
69 pub const fn none() -> Self {
70 Self {
71 queues: IdList::none(),
72 }
73 }
74
75 pub const fn from_queue_bits(bits: u64) -> Self {
76 Self {
77 queues: IdList::from_bits(bits),
78 }
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn irq_source_lists_queue_masks() {
88 let mut queues = IdList::none();
89 queues.insert(2);
90 let source = IrqSourceInfo::legacy(queues);
91
92 assert_eq!(source.id, 0);
93 assert!(source.queues.contains(2));
94 }
95}