1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use r3_core::kernel::{
traits, Cfg, EnableInterruptLineError, InterruptNum, InterruptPriority,
QueryInterruptLineError, SetInterruptLinePriorityError, StaticInterruptHandler,
};
use r3_kernel::{KernelTraits, System};
use tock_registers::interfaces::{Readable, Writeable};
use crate::{InterruptControllerToPort, Plic, INTERRUPT_EXTERNAL, INTERRUPT_PLATFORM_START};
pub const fn configure<C, Traits: Plic + KernelTraits + InterruptControllerToPort>(b: &mut Cfg<C>)
where
C: ~const traits::CfgInterruptLine<System = System<Traits>>,
{
StaticInterruptHandler::define()
.line(INTERRUPT_EXTERNAL)
.start(interrupt_handler::<Traits>)
.finish(b);
}
pub fn init<Traits: Plic>() {
let plic_regs = Traits::plic_regs();
let ctx = Traits::CONTEXT;
let num_ints = Traits::MAX_NUM + 1;
for i in 0..(num_ints + 31) / 32 {
plic_regs.interrupt_enable[ctx][i].set(0);
}
plic_regs.ctxs[Traits::CONTEXT].priority_threshold.set(0);
}
#[inline]
fn interrupt_handler<Traits: Plic + KernelTraits + InterruptControllerToPort>() {
if let Some((token, num)) = claim_interrupt::<Traits>() {
if let Some(handler) = Traits::INTERRUPT_HANDLERS.get(num) {
if Traits::USE_NESTING {
unsafe { Traits::enable_external_interrupts() };
}
unsafe { handler() };
if Traits::USE_NESTING {
unsafe { Traits::disable_external_interrupts() };
}
}
end_interrupt::<Traits>(token);
}
}
type Token = u32;
#[inline]
fn claim_interrupt<Traits: Plic>() -> Option<(Token, InterruptNum)> {
let plic_regs = Traits::plic_regs();
let num = plic_regs.ctxs[Traits::CONTEXT].claim_complete.get();
if num == 0 {
return None;
}
if Traits::USE_NESTING {
let old_threshold = plic_regs.ctxs[Traits::CONTEXT].priority_threshold.get();
let priority = plic_regs.interrupt_priority[num as usize].get();
plic_regs.ctxs[Traits::CONTEXT]
.priority_threshold
.set(priority);
plic_regs.ctxs[Traits::CONTEXT].claim_complete.set(num);
Some((
old_threshold,
num as InterruptNum + INTERRUPT_PLATFORM_START,
))
} else {
Some((num, num as InterruptNum + INTERRUPT_PLATFORM_START))
}
}
#[inline]
fn end_interrupt<Traits: Plic>(token: Token) {
let plic_regs = Traits::plic_regs();
if Traits::USE_NESTING {
plic_regs.ctxs[Traits::CONTEXT]
.priority_threshold
.set(token);
} else {
plic_regs.ctxs[Traits::CONTEXT].claim_complete.set(token);
}
}
pub fn set_interrupt_line_priority<Traits: Plic>(
line: InterruptNum,
priority: InterruptPriority,
) -> Result<(), SetInterruptLinePriorityError> {
let plic_regs = Traits::plic_regs();
let line = line - INTERRUPT_PLATFORM_START;
if line > Traits::MAX_NUM || priority < 0 || priority > Traits::MAX_PRIORITY {
return Err(SetInterruptLinePriorityError::BadParam);
}
plic_regs.interrupt_priority[line].set(priority as u32);
Ok(())
}
pub fn enable_interrupt_line<Traits: Plic>(
line: InterruptNum,
) -> Result<(), EnableInterruptLineError> {
let plic_regs = Traits::plic_regs();
let line = line - INTERRUPT_PLATFORM_START;
if line > Traits::MAX_NUM {
return Err(EnableInterruptLineError::BadParam);
}
let reg = &plic_regs.interrupt_enable[Traits::CONTEXT][line / 32];
reg.set(reg.get() | (1u32 << (line % 32)));
Ok(())
}
pub fn disable_interrupt_line<Traits: Plic>(
line: InterruptNum,
) -> Result<(), EnableInterruptLineError> {
let plic_regs = Traits::plic_regs();
let line = line - INTERRUPT_PLATFORM_START;
if line > Traits::MAX_NUM {
return Err(EnableInterruptLineError::BadParam);
}
let reg = &plic_regs.interrupt_enable[Traits::CONTEXT][line / 32];
reg.set(reg.get() & !(1u32 << (line % 32)));
Ok(())
}
pub fn is_interrupt_line_pending<Traits: Plic>(
line: InterruptNum,
) -> Result<bool, QueryInterruptLineError> {
let plic_regs = Traits::plic_regs();
let line = line - INTERRUPT_PLATFORM_START;
if line > Traits::MAX_NUM {
return Err(QueryInterruptLineError::BadParam);
}
Ok((plic_regs.interrupt_pending[line / 32].get() & (1u32 << (line % 32))) != 0)
}