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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use r3_core::kernel::{
ClearInterruptLineError, EnableInterruptLineError, InterruptNum, InterruptPriority,
PendInterruptLineError, QueryInterruptLineError, SetInterruptLinePriorityError,
};
use tock_registers::interfaces::{ReadWriteable, Readable, Writeable};
use super::{
cfg::{Gic, GicOptions},
gic_regs,
};
#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct GicRegs {
pub(super) distributor: &'static gic_regs::GicDistributor,
pub(super) cpu_interface: &'static gic_regs::GicCpuInterface,
}
impl GicRegs {
#[inline(always)]
pub unsafe fn from_system_traits<Traits: GicOptions>() -> Self {
Self {
distributor: unsafe {
&*(Traits::GIC_DISTRIBUTOR_BASE as *const gic_regs::GicDistributor)
},
cpu_interface: unsafe { &*(Traits::GIC_CPU_BASE as *const gic_regs::GicCpuInterface) },
}
}
}
pub fn init<Traits: Gic>() {
let GicRegs {
distributor,
cpu_interface,
} = Traits::gic_regs();
distributor
.CTLR
.modify(gic_regs::GICD_CTLR::Enable::Disable);
let num_lines = Traits::num_interrupt_lines();
for r in &distributor.ICENABLE[0..(num_lines + 31) / 32] {
r.set(0xffffffff);
}
for r in &distributor.ICPEND[0..(num_lines + 31) / 32] {
r.set(0xffffffff);
}
for r in &distributor.ICFGR[0..(num_lines + 15) / 16] {
r.set(0);
}
for r in &distributor.ITARGETS[0..(num_lines + 3) / 4] {
r.set(0x01010101);
}
cpu_interface.PMR.set(0xff);
while let Some(x) = acknowledge_interrupt::<Traits>() {
end_interrupt::<Traits>(x);
}
cpu_interface.BPR.set(0);
distributor.CTLR.modify(gic_regs::GICD_CTLR::Enable::Enable);
cpu_interface
.CTLR
.modify(gic_regs::GICC_CTLR::Enable::Enable);
}
#[inline]
pub fn acknowledge_interrupt<Traits: Gic>() -> Option<InterruptNum> {
let cpu_interface = Traits::gic_regs().cpu_interface;
let raw = cpu_interface.IAR.get();
let interrupt_id = raw & 0x3ff;
if interrupt_id == 0x3ff {
None
} else {
Some(interrupt_id as _)
}
}
#[inline]
pub fn end_interrupt<Traits: Gic>(num: InterruptNum) {
let cpu_interface = Traits::gic_regs().cpu_interface;
cpu_interface.EOIR.set(num as _);
}
pub fn set_interrupt_line_priority<Traits: Gic>(
line: InterruptNum,
priority: InterruptPriority,
) -> Result<(), SetInterruptLinePriorityError> {
let distributor = Traits::gic_regs().distributor;
if line >= Traits::num_interrupt_lines() || !(0..=255).contains(&priority) {
return Err(SetInterruptLinePriorityError::BadParam);
}
distributor.IPRIORITY[line].set(priority as u8);
Ok(())
}
pub fn enable_interrupt_line<Traits: Gic>(
line: InterruptNum,
) -> Result<(), EnableInterruptLineError> {
let distributor = Traits::gic_regs().distributor;
if line < 16 || line >= Traits::num_interrupt_lines() {
return Err(EnableInterruptLineError::BadParam);
}
distributor.ISENABLE[line / 32].set(1 << (line % 32));
Ok(())
}
pub fn disable_interrupt_line<Traits: Gic>(
line: InterruptNum,
) -> Result<(), EnableInterruptLineError> {
let distributor = Traits::gic_regs().distributor;
if line < 16 || line >= Traits::num_interrupt_lines() {
return Err(EnableInterruptLineError::BadParam);
}
distributor.ICENABLE[line / 32].set(1 << (line % 32));
Ok(())
}
pub fn pend_interrupt_line<Traits: Gic>(line: InterruptNum) -> Result<(), PendInterruptLineError> {
let distributor = Traits::gic_regs().distributor;
if line >= Traits::num_interrupt_lines() {
return Err(PendInterruptLineError::BadParam);
} else if line < 16 {
distributor.SPENDSGIR[line].set(1);
} else {
distributor.ISPEND[line / 32].set(1 << (line % 32));
}
Ok(())
}
pub fn clear_interrupt_line<Traits: Gic>(
line: InterruptNum,
) -> Result<(), ClearInterruptLineError> {
let distributor = Traits::gic_regs().distributor;
if line >= Traits::num_interrupt_lines() {
return Err(ClearInterruptLineError::BadParam);
} else if line < 16 {
distributor.CPENDSGIR[line].set(1);
} else {
distributor.ICPEND[line / 32].set(1 << (line % 32));
}
Ok(())
}
pub fn is_interrupt_line_pending<Traits: Gic>(
line: InterruptNum,
) -> Result<bool, QueryInterruptLineError> {
let distributor = Traits::gic_regs().distributor;
if line >= Traits::num_interrupt_lines() {
return Err(QueryInterruptLineError::BadParam);
}
Ok((distributor.ISPEND[line / 32].get() & (1 << (line % 32))) != 0)
}