forked from hermit-os/kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicetree.rs
More file actions
271 lines (239 loc) · 8.36 KB
/
Copy pathdevicetree.rs
File metadata and controls
271 lines (239 loc) · 8.36 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
#![allow(dead_code)]
#[cfg(all(any(feature = "virtio-net", feature = "console"), not(feature = "pci")))]
use core::ptr::NonNull;
use fdt::Fdt;
use memory_addresses::PhysAddr;
#[cfg(all(feature = "gem-net", not(feature = "pci")))]
use memory_addresses::VirtAddr;
#[cfg(all(any(feature = "virtio-net", feature = "console"), not(feature = "pci")))]
use virtio::mmio::{DeviceRegisters, DeviceRegistersVolatileFieldAccess};
#[cfg(all(any(feature = "virtio-net", feature = "console"), not(feature = "pci")))]
use volatile::VolatileRef;
use crate::arch::riscv64::kernel::get_dtb_ptr;
use crate::arch::riscv64::kernel::interrupts::init_plic;
#[cfg(all(feature = "console", not(feature = "pci")))]
use crate::arch::riscv64::kernel::mmio::MmioDriver;
use crate::arch::riscv64::mm::paging::{self, PageSize};
#[cfg(feature = "console")]
use crate::console::IoDevice;
#[cfg(feature = "console")]
use crate::drivers::console::VirtioUART;
#[cfg(all(feature = "console", not(feature = "pci")))]
use crate::drivers::mmio::get_console_driver;
#[cfg(all(feature = "gem-net", not(feature = "pci")))]
use crate::drivers::net::gem;
#[cfg(all(feature = "console", feature = "pci"))]
use crate::drivers::pci::get_console_driver;
#[cfg(all(any(feature = "virtio-net", feature = "console"), not(feature = "pci")))]
use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver};
#[cfg(all(any(feature = "gem-net", feature = "virtio-net"), not(feature = "pci")))]
use crate::executor::device::NETWORK_DEVICE;
#[cfg(all(feature = "console", not(feature = "pci")))]
use crate::kernel::mmio::register_driver;
static mut PLATFORM_MODEL: Model = Model::Unknown;
enum Model {
Fux40,
Virt,
Unknown,
}
/// Inits variables based on the device tree
/// This function should only be called once
pub fn init() {
debug!("Init devicetree");
if !get_dtb_ptr().is_null() {
unsafe {
let fdt = Fdt::from_ptr(get_dtb_ptr()).expect("FDT is invalid");
let model = fdt
.find_node("/")
.unwrap()
.property("compatible")
.expect("compatible not found in FDT")
.as_str()
.unwrap();
if model.contains("riscv-virtio") {
PLATFORM_MODEL = Model::Virt;
} else if model.contains("sifive,hifive-unmatched-a00")
|| model.contains("sifive,hifive-unleashed-a00")
|| model.contains("sifive,fu740")
|| model.contains("sifive,fu540")
{
PLATFORM_MODEL = Model::Fux40;
} else {
warn!("Unknown platform, guessing PLIC context 1");
PLATFORM_MODEL = Model::Unknown;
}
info!("Model: {model}");
}
}
}
/// Inits drivers based on the device tree
/// This function should only be called once
pub fn init_drivers() {
// TODO: Implement devicetree correctly
if !get_dtb_ptr().is_null() {
unsafe {
debug!("Init drivers using devicetree");
let fdt = Fdt::from_ptr(get_dtb_ptr()).expect("FDT is invalid");
// Init PLIC first
if let Some(plic_node) = fdt.find_compatible(&["sifive,plic-1.0.0"]) {
debug!("Found interrupt controller");
let plic_region = plic_node
.reg()
.expect("Reg property for PLIC not found in FDT")
.next()
.unwrap();
let plic_region_start = PhysAddr::new(plic_region.starting_address as u64);
debug!(
"Init PLIC at {:p}, size: {:x}",
plic_region_start,
plic_region.size.unwrap()
);
assert!(
plic_region.size.unwrap()
< usize::try_from(paging::HugePageSize::SIZE).unwrap()
);
paging::identity_map::<paging::HugePageSize>(plic_region_start);
// TODO: Determine correct context via devicetree and allow more than one context
match PLATFORM_MODEL {
Model::Virt | Model::Unknown => {
init_plic(plic_region.starting_address as usize, 1);
}
Model::Fux40 => init_plic(plic_region.starting_address as usize, 2),
}
}
// Init GEM
#[cfg(all(feature = "gem-net", not(feature = "pci")))]
if let Some(gem_node) = fdt.find_compatible(&["sifive,fu540-c000-gem"]) {
debug!("Found Ethernet controller");
let gem_region = gem_node
.reg()
.expect("reg property for GEM not found in FDT")
.next()
.unwrap();
let irq = gem_node
.interrupts()
.expect("interrupts property for GEM not found in FDT")
.next()
.unwrap();
let mac = gem_node
.property("local-mac-address")
.expect("local-mac-address property for GEM not found in FDT")
.value;
debug!("Local MAC address: {mac:x?}");
let mut phy_addr = u32::MAX;
let phy_node = gem_node
.children()
.next()
.expect("GEM node has no child node (i. e. ethernet-phy)");
if phy_node.name.contains("ethernet-phy") {
phy_addr = phy_node
.property("reg")
.expect("reg property for ethernet-phy not found in FDT")
.as_usize()
.unwrap() as u32;
} else {
warn!("Expected ethernet-phy node, found something else");
}
let gem_region_start = PhysAddr::new(gem_region.starting_address as u64);
debug!("Init GEM at {gem_region_start:p}, irq: {irq}, phy_addr: {phy_addr}");
assert!(
gem_region.size.unwrap() < usize::try_from(paging::HugePageSize::SIZE).unwrap()
);
paging::identity_map::<paging::HugePageSize>(gem_region_start);
match gem::init_device(
VirtAddr::new(gem_region_start.as_u64()),
irq.try_into().unwrap(),
phy_addr,
<[u8; 6]>::try_from(mac).expect("MAC with invalid length"),
) {
Ok(drv) => *NETWORK_DEVICE.lock() = Some(drv),
Err(err) => error!("Could not initialize GEM driver: {err}"),
}
}
// Init virtio-mmio
#[cfg(all(any(feature = "virtio-net", feature = "console"), not(feature = "pci")))]
if let Some(virtio_node) = fdt.find_compatible(&["virtio,mmio"]) {
debug!("Found virtio mmio device");
let virtio_region = virtio_node
.reg()
.expect("reg property for virtio mmio not found in FDT")
.next()
.unwrap();
let irq = virtio_node
.interrupts()
.expect("interrupts property for virtio mmio not found in FDT")
.next()
.unwrap();
let virtio_region_start = PhysAddr::new(virtio_region.starting_address as u64);
debug!("Init virtio_mmio at {virtio_region_start:p}, irq: {irq}");
assert!(
virtio_region.size.unwrap()
< usize::try_from(paging::HugePageSize::SIZE).unwrap()
);
paging::identity_map::<paging::HugePageSize>(virtio_region_start);
// Verify the first register value to find out if this is really an MMIO magic-value.
let ptr = virtio_region.starting_address as *mut DeviceRegisters;
let mmio = VolatileRef::new(NonNull::new(ptr).unwrap());
let magic = mmio.as_ptr().magic_value().read().to_ne();
let version = mmio.as_ptr().version().read().to_ne();
const MMIO_MAGIC_VALUE: u32 = 0x7472_6976;
if magic != MMIO_MAGIC_VALUE {
error!("It's not a MMIO-device at {mmio:p}");
}
if version != 2 {
warn!("Found a legacy device, which isn't supported");
return;
}
// We found a MMIO-device (whose 512-bit address in this structure).
trace!("Found a MMIO-device at {mmio:p}");
// Verify the device-ID to find the network card
let id = mmio.as_ptr().device_id().read();
if cfg!(debug_assertions) {
crate::mm::physicalmem::assert_physical_unavailable(
PhysAddr::new(virtio_region.starting_address.addr() as u64),
virtio_region.size.unwrap(),
);
}
match id {
#[cfg(all(feature = "virtio-net", not(feature = "gem-net")))]
virtio::Id::Net => {
debug!("Found virtio network card at {mmio:p}");
if let Ok(VirtioDriver::Network(drv)) =
mmio_virtio::init_device(mmio, irq.try_into().unwrap())
{
*NETWORK_DEVICE.lock() = Some(drv);
}
}
#[cfg(feature = "console")]
virtio::Id::Console => {
debug!("Found virtio console at {mmio:p}");
if let Ok(VirtioDriver::Console(drv)) =
mmio_virtio::init_device(mmio, irq.try_into().unwrap())
{
register_driver(MmioDriver::VirtioConsole(
hermit_sync::InterruptSpinMutex::new(*drv),
));
}
}
_ => {
warn!("Found unknown virtio device with ID {id:?} at {mmio:p}");
}
}
}
}
}
#[cfg(all(
any(feature = "virtio-net", feature = "console", feature = "gem-net"),
not(feature = "pci"),
))]
super::mmio::MMIO_DRIVERS.finalize();
#[cfg(feature = "console")]
{
if get_console_driver().is_some() {
info!("Switch to virtio console");
crate::console::CONSOLE
.lock()
.replace_device(IoDevice::Virtio(VirtioUART::new()));
}
}
}