-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbar.rs
More file actions
310 lines (287 loc) · 9.64 KB
/
Copy pathbar.rs
File metadata and controls
310 lines (287 loc) · 9.64 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::convert::TryFrom;
use super::bits;
use super::BarN;
pub const BAR_COUNT: usize = 6;
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub enum BarDefine {
Pio(u16),
Mmio(u32),
Mmio64(u64),
}
impl BarDefine {
/// Definition represent PIO-backed BAR
pub fn is_pio(&self) -> bool {
matches!(self, BarDefine::Pio(_))
}
/// Definition represent MMIO-backed (32-bit or 64-bit) BAR
pub fn is_mmio(&self) -> bool {
matches!(self, BarDefine::Mmio(_) | BarDefine::Mmio64(_))
}
/// Get the size of the BAR definition, regardless of type
pub fn size(&self) -> u64 {
match self {
BarDefine::Pio(sz) => *sz as u64,
BarDefine::Mmio(sz) => *sz as u64,
BarDefine::Mmio64(sz) => *sz as u64,
}
}
}
impl TryFrom<EntryKind> for BarDefine {
type Error = ();
fn try_from(value: EntryKind) -> Result<Self, Self::Error> {
match value {
EntryKind::Empty | EntryKind::Mmio64High => Err(()),
EntryKind::Pio(sz) => Ok(BarDefine::Pio(sz)),
EntryKind::Mmio(sz) => Ok(BarDefine::Mmio(sz)),
EntryKind::Mmio64(sz) => Ok(BarDefine::Mmio64(sz)),
}
}
}
#[derive(Copy, Clone)]
enum EntryKind {
Empty,
Pio(u16),
Mmio(u32),
Mmio64(u64),
Mmio64High,
}
#[derive(Copy, Clone)]
struct Entry {
kind: EntryKind,
value: u64,
}
impl Default for Entry {
fn default() -> Self {
Self { kind: EntryKind::Empty, value: 0 }
}
}
pub(super) struct Bars {
entries: [Entry; BAR_COUNT],
}
impl Bars {
pub(super) fn new(defs: &[Option<BarDefine>; BAR_COUNT]) -> Self {
let mut this = Self { entries: Default::default() };
for (idx, def) in defs.iter().enumerate() {
match def {
None => continue,
Some(def) => {
assert!(matches!(this.entries[idx].kind, EntryKind::Empty));
this.entries[idx].kind = match def {
BarDefine::Pio(sz) => EntryKind::Pio(*sz),
BarDefine::Mmio(sz) => EntryKind::Mmio(*sz),
BarDefine::Mmio64(sz) => {
// Make sure 64-bit BAR definitions are playing by
// the rules
assert!(idx < (BarN::BAR5 as usize));
this.entries[idx + 1].kind = EntryKind::Mmio64High;
EntryKind::Mmio64(*sz)
}
}
}
}
}
this
}
pub(super) fn reg_read(&self, bar: BarN) -> u32 {
let idx = bar as usize;
let ent = self.entries[idx];
match ent.kind {
EntryKind::Empty => 0,
EntryKind::Pio(_) => (ent.value as u16) as u32 | bits::BAR_TYPE_IO,
EntryKind::Mmio(_) => ent.value as u32 | bits::BAR_TYPE_MEM,
EntryKind::Mmio64(_) => ent.value as u32 | bits::BAR_TYPE_MEM64,
EntryKind::Mmio64High => {
assert_ne!(idx, 0);
let ent = self.entries[idx - 1];
assert!(matches!(ent.kind, EntryKind::Mmio64(_)));
(ent.value >> 32) as u32
}
}
}
pub(super) fn reg_write(
&mut self,
bar: BarN,
val: u32,
) -> Option<(BarDefine, u64, u64)> {
let idx = bar as usize;
let ent = &mut self.entries[idx];
let (def, old, new) = match ent.kind {
EntryKind::Empty => return None,
EntryKind::Pio(size) => {
let mask = !(size - 1) as u32;
let old = ent.value;
ent.value = (val & mask) as u64;
(BarDefine::Pio(size), old, ent.value)
}
EntryKind::Mmio(size) => {
let mask = !(size - 1);
let old = ent.value;
ent.value = (val & mask) as u64;
(BarDefine::Mmio(size), old, ent.value)
}
EntryKind::Mmio64(size) => {
let old = ent.value;
let mask = !(size - 1) as u32;
let low = val as u32 & mask;
ent.value = (old & (0xffffffff << 32)) | low as u64;
(BarDefine::Mmio64(size), old, ent.value)
}
EntryKind::Mmio64High => {
assert!(idx > 0);
let mut ent = &mut self.entries[idx - 1];
let size = match ent.kind {
EntryKind::Mmio64(sz) => sz,
_ => panic!(),
};
let mask = !(size - 1);
let old = ent.value;
let high = (((val as u64) << 32) & mask) & 0xffffffff00000000;
ent.value = high | (old & 0xffffffff);
(BarDefine::Mmio64(size), old, ent.value)
}
};
if old != new {
return Some((def, old, new));
}
None
}
/// Get BAR definition and current value
pub fn get(&self, n: BarN) -> Option<(BarDefine, u64)> {
let ent = &self.entries[n as usize];
let def = BarDefine::try_from(ent.kind).ok()?;
Some((def, ent.value))
}
/// Set BAR value directly
///
/// May only be called on BARs which are defined (not on empty BARs or the
/// high portions of 64-bit MMIO BARs). Furthermore, the value must be
/// valid for the BAR type (ie. not > u32::MAX for 32-bit MMIO BAR).
pub fn set(&mut self, n: BarN, value: u64) {
let ent = &mut self.entries[n as usize];
match ent.kind {
EntryKind::Empty => panic!("{:?} not defined", n),
EntryKind::Mmio64High => {
panic!("high BAR bits not to be set directly")
}
EntryKind::Pio(_) => {
assert!(value <= u16::MAX as u64);
ent.value = value;
}
EntryKind::Mmio(_) => {
assert!(value <= u32::MAX as u64);
}
EntryKind::Mmio64(_) => {}
}
ent.value = value;
}
pub(super) fn export(&self) -> migrate::BarStateV1 {
let mut entries = Vec::new();
for (idx, entry) in self.entries.iter().enumerate() {
match entry.kind {
EntryKind::Pio(sz) => entries.push(migrate::BarEntryV1 {
n: idx as u8,
kind: migrate::BarKindV1::Pio,
size: sz as u64,
value: entry.value,
}),
EntryKind::Mmio(sz) => entries.push(migrate::BarEntryV1 {
n: idx as u8,
kind: migrate::BarKindV1::Mmio,
size: sz as u64,
value: entry.value,
}),
EntryKind::Mmio64(sz) => entries.push(migrate::BarEntryV1 {
n: idx as u8,
kind: migrate::BarKindV1::Mmio64,
size: sz,
value: entry.value,
}),
EntryKind::Empty | EntryKind::Mmio64High => {}
}
}
migrate::BarStateV1 { entries }
}
}
pub mod migrate {
use serde::Serialize;
#[derive(Serialize)]
pub enum BarKindV1 {
Pio,
Mmio,
Mmio64,
}
#[derive(Serialize)]
pub struct BarEntryV1 {
pub n: u8,
pub kind: BarKindV1,
pub size: u64,
pub value: u64,
}
#[derive(Serialize)]
pub struct BarStateV1 {
pub entries: Vec<BarEntryV1>,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryFrom;
fn setup() -> Bars {
let bar_defs = [
Some(BarDefine::Pio(0x100)),
Some(BarDefine::Mmio(0x20000)),
Some(BarDefine::Mmio64(0x40000)),
None, // high bits
Some(BarDefine::Mmio64(0x200000000)),
None, // high bits
];
let bars = Bars::new(&bar_defs);
bars
}
#[test]
fn init() {
let _ = setup();
}
#[test]
fn read_type() {
let mut bars = setup();
bars.set(BarN::BAR0, 0x1000);
bars.set(BarN::BAR1, 0xc000000);
bars.set(BarN::BAR2, 0xd000000);
bars.set(BarN::BAR4, 0x800000000);
assert_eq!(bars.reg_read(BarN::BAR0), 0x1001);
assert_eq!(bars.reg_read(BarN::BAR1), 0xc000000);
assert_eq!(bars.reg_read(BarN::BAR2), 0xd000004);
assert_eq!(bars.reg_read(BarN::BAR3), 0);
assert_eq!(bars.reg_read(BarN::BAR4), 0x4);
assert_eq!(bars.reg_read(BarN::BAR5), 0x8);
}
#[test]
fn write_place() {
let mut bars = setup();
bars.reg_write(BarN::BAR0, 0x1000);
bars.reg_write(BarN::BAR1, 0xc000000);
bars.reg_write(BarN::BAR2, 0xd000000);
bars.reg_write(BarN::BAR5, 0x8);
bars.reg_write(BarN::BAR4, 0x0);
assert_eq!(bars.reg_read(BarN::BAR0), 0x1001);
assert_eq!(bars.reg_read(BarN::BAR1), 0xc000000);
assert_eq!(bars.reg_read(BarN::BAR2), 0xd000004);
assert_eq!(bars.reg_read(BarN::BAR3), 0);
assert_eq!(bars.reg_read(BarN::BAR4), 0x4);
assert_eq!(bars.reg_read(BarN::BAR5), 0x8);
}
#[test]
fn limits() {
let mut bars = setup();
for i in 0..=5u8 {
bars.reg_write(BarN::try_from(i).unwrap(), 0xffffffff);
}
assert_eq!(bars.reg_read(BarN::BAR0), 0x0000ff01);
assert_eq!(bars.reg_read(BarN::BAR1), 0xfffe0000);
assert_eq!(bars.reg_read(BarN::BAR2), 0xfffc0004);
assert_eq!(bars.reg_read(BarN::BAR3), 0xffffffff);
assert_eq!(bars.reg_read(BarN::BAR4), 0x00000004);
assert_eq!(bars.reg_read(BarN::BAR5), 0xfffffffe);
}
}