fdt_parser/
pci.rs

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
use core::{fmt::Debug, ops::Range};

use crate::{
    define::Phandle,
    error::FdtResult,
    node::Node,
    read::{FdtReader, U32Array},
    FdtError, FdtRangeIter, InterruptController,
};

pub struct Pci<'a> {
    pub node: Node<'a>,
}

impl<'a> Pci<'a> {
    pub fn bus_range(&self) -> Option<Range<usize>> {
        let prop = self.node.find_property("bus-range")?;
        let mut reader = FdtReader::new(prop.raw_value());
        let start = reader.take_u32()?;
        let end = reader.take_u32()?;

        Some(start as usize..end as usize)
    }

    pub fn ranges(&self) -> FdtResult<impl Iterator<Item = PciRange> + 'a> {
        let ranges = self
            .node
            .node_ranges()
            .ok_or(FdtError::NotFound("ranges"))?;

        let iter = ranges.iter();

        Ok(PciRangeIter { iter })
    }

    pub fn child_interrupts(
        &self,
        bus: u8,
        device: u8,
        func: u8,
        irq_pin: u32,
    ) -> FdtResult<PciChildIrq<'a>> {
        let mask = self.interrupt_map_mask()?;

        let want0 = (bus as u32) << 16 | (device as u32) << 11 | (func as u32) << 8;

        let mut want = mask;

        want[0] = want0 | mask[0];
        want[3] = irq_pin;

        let mut prop = self
            .node
            .find_property("interrupt-map")
            .ok_or(FdtError::NotFound("interrupt-map"))?;

        while let Some(hi) = prop.data.take_u32() {
            let _mid = prop.data.take_u32().ok_or(FdtError::Eof)?;
            let _lo = prop.data.take_u32().ok_or(FdtError::Eof)?;
            let irq_line = prop.data.take_u32().ok_or(FdtError::Eof)?;

            let parent = Phandle::from(prop.data.take_u32().ok_or(FdtError::Eof)?);

            let parent_node = self
                .node
                .fdt
                .get_node_by_phandle(parent)
                .ok_or(FdtError::NotFound("parent interrupt"))?;

            let address_cell = parent_node
                .find_property("#address-cells")
                .map(|p| p.u32())
                .unwrap_or_default();

            for _i in 0..address_cell {
                prop.data.take_u32().ok_or(FdtError::Eof)?;
            }

            let parent_node = InterruptController { node: parent_node };

            let cell_size = parent_node.interrupt_cells();

            let data = prop
                .data
                .take(cell_size * size_of::<u32>())
                .ok_or(FdtError::Eof)?;

            if hi & mask[0] != want[0] || irq_line != want[3] {
                continue;
            }

            return Ok(PciChildIrq {
                parent,
                irqs: U32Array::new(data),
            });
        }

        Err(FdtError::NotFound("pci child"))
    }

    fn interrupt_map_mask(&self) -> FdtResult<[u32; 4]> {
        let prop = self
            .node
            .find_property("interrupt-map-mask")
            .ok_or(FdtError::NotFound("interrupt-map-mask"))?;

        let mut mask = [0u32; 4];
        let mut data = prop.data.clone();

        for one in mask.iter_mut() {
            *one = data.take_u32().ok_or(FdtError::Eof)?;
        }

        Ok(mask)
    }
}

pub struct PciChildIrq<'a> {
    pub parent: Phandle,
    pub irqs: U32Array<'a>,
}

pub struct PciRangeIter<'a> {
    iter: FdtRangeIter<'a>,
}

impl Iterator for PciRangeIter<'_> {
    type Item = PciRange;

    fn next(&mut self) -> Option<Self::Item> {
        let one = self.iter.next()?;
        let mut child = one.child_bus_address();
        let cpu_address = one.parent_bus_address().as_u64();
        let size = one.size;

        let hi = child.next().unwrap();
        let mid = child.next().unwrap();
        let low = child.next().unwrap();

        let ss = (hi >> 24) & 0b11;
        let prefetchable = (hi & 1 << 30) > 0;

        let space = match ss {
            0b00 => PciSpace::Configuration,
            0b01 => PciSpace::IO,
            0b10 => PciSpace::Memory32,
            0b11 => PciSpace::Memory64,
            _ => panic!(),
        };

        let child_bus_address = (mid as u64) << 32 | low as u64;

        Some(PciRange {
            space,
            bus_address: child_bus_address,
            cpu_address,
            size,
            prefetchable,
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PciSpace {
    Configuration,
    IO,
    Memory32,
    Memory64,
}

#[derive(Clone, PartialEq, Eq)]
pub struct PciRange {
    pub space: PciSpace,
    pub bus_address: u64,
    pub cpu_address: u64,
    pub size: u64,
    pub prefetchable: bool,
}

// #[derive(Debug, Clone, Copy)]
// pub struct PciPhys {
//     pub bus_address: u64,
//     pub bus_num: u8,
//     pub device_num: u8,
//     pub function_num: u8,
//     pub register_num: u8,
//     pub space: PciSpace,
//     pub prefetchable: bool,
//     pub relocatable: bool,
// }

// impl From<[u32; 3]> for PciPhys {
//     fn from(value: [u32; 3]) -> Self {
//         // [hi, mid, low]

//         let hi = value[0];
//         let mid = value[1];
//         let low = value[2];

//         let ss = (hi >> 24) & 0b11;
//         let prefetchable = (hi & 1 << 30) > 0;
//         let relocatable = (hi & 1 << 31) > 0;

//         let bus_num = ((hi >> 16) & 0xFF) as u8;

//         let device_num = ((hi >> 11) & 0b11111) as u8;
//         let function_num = (hi >> 8 & 0b111) as u8;
//         let register_num = (hi & 0xFF) as u8;

//         let space = match ss {
//             0b00 => PciSpace::Configuration,
//             0b01 => PciSpace::IO,
//             0b10 => PciSpace::Memory32,
//             0b11 => PciSpace::Memory64,
//             _ => panic!(),
//         };

//         let child_bus_address = (mid as u64) << 32 | low as u64;

//         PciPhys {
//             space,
//             bus_address: child_bus_address,
//             prefetchable,
//             relocatable,
//             bus_num,
//             device_num,
//             function_num,
//             register_num,
//         }
//     }
// }

impl Debug for PciRange {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "PciRange {{ space: {:?}, child_bus_address: {:#x}, parent_bus_address: {:#x}, size: {:#x}, prefetchable: {}}}", 
        self.space, self.bus_address, self.cpu_address, self.size, self.prefetchable)
    }
}