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
use crate::addressing::Pa;
use crate::x86_64::pio::Pio;
#[derive(Debug)]
pub struct IoSpace {
pub(crate) base: u32,
pub(crate) length: u32,
}
impl IoSpace {
#[cfg(target_arch = "x86_64")]
pub fn offset<const IDX: u32>(&self) -> Option<Pio> {
IDX.checked_add(4).and_then(|i| {
if i <= self.length + 1 {
Some(Pio::new((self.base + IDX) as u16))
} else {
None
}
})
}
}
#[derive(Debug)]
pub struct MemorySpace {
pub(crate) base: Pa,
pub(crate) length: usize,
pub(crate) _prefetchable: bool,
}
impl MemorySpace {
pub fn all(&self) -> crate::dev::mmio::MmioArea {
unsafe { crate::dev::mmio::MmioArea::new(self.base..(self.base + self.length)) }
}
pub fn try_split_mmio_range(
&self,
offset: usize,
length: usize,
) -> Option<crate::dev::mmio::MmioArea> {
if offset < self.length
&& offset
.checked_add(length)
.map(|limit| limit <= self.length)
.unwrap_or(false)
{
unsafe {
Some(crate::dev::mmio::MmioArea::new(
(self.base + offset)..(self.base + offset + length),
))
}
} else {
None
}
}
}
#[derive(Debug)]
pub enum Bar {
MemorySpace(MemorySpace),
IoSpace(IoSpace),
}
impl Bar {
pub fn try_get_memory_bar(self) -> Option<MemorySpace> {
if let Self::MemorySpace(memory_space) = self {
Some(memory_space)
} else {
None
}
}
pub fn try_get_io_bar(self) -> Option<IoSpace> {
if let Self::IoSpace(io_space) = self {
Some(io_space)
} else {
None
}
}
}