|
1 | 1 | #![allow(dead_code)]
|
2 | 2 |
|
3 |
| -#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))] |
| 3 | +#[cfg(all( |
| 4 | + any(feature = "tcp", feature = "udp", feature = "console"), |
| 5 | + not(feature = "pci") |
| 6 | +))] |
4 | 7 | use core::ptr::NonNull;
|
5 | 8 |
|
6 | 9 | use fdt::Fdt;
|
7 | 10 | use memory_addresses::PhysAddr;
|
8 | 11 | #[cfg(all(
|
9 |
| - any(feature = "tcp", feature = "udp"), |
| 12 | + any(feature = "tcp", feature = "udp", feature = "console"), |
10 | 13 | feature = "gem-net",
|
11 | 14 | not(feature = "pci")
|
12 | 15 | ))]
|
13 | 16 | use memory_addresses::VirtAddr;
|
14 |
| -#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))] |
| 17 | +#[cfg(all( |
| 18 | + any(feature = "tcp", feature = "udp", feature = "console"), |
| 19 | + not(feature = "pci") |
| 20 | +))] |
15 | 21 | use virtio::mmio::{DeviceRegisters, DeviceRegistersVolatileFieldAccess};
|
16 |
| -#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))] |
| 22 | +#[cfg(all( |
| 23 | + any(feature = "tcp", feature = "udp", feature = "console"), |
| 24 | + not(feature = "pci") |
| 25 | +))] |
17 | 26 | use volatile::VolatileRef;
|
18 | 27 |
|
19 | 28 | use crate::arch::riscv64::kernel::get_dtb_ptr;
|
20 | 29 | use crate::arch::riscv64::kernel::interrupts::init_plic;
|
21 |
| -#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))] |
| 30 | +#[cfg(all( |
| 31 | + any(feature = "tcp", feature = "udp", feature = "console"), |
| 32 | + not(feature = "pci") |
| 33 | +))] |
22 | 34 | use crate::arch::riscv64::kernel::mmio::MmioDriver;
|
23 | 35 | use crate::arch::riscv64::mm::paging::{self, PageSize};
|
| 36 | +#[cfg(feature = "console")] |
| 37 | +use crate::console::IoDevice; |
| 38 | +#[cfg(feature = "console")] |
| 39 | +use crate::drivers::console::VirtioUART; |
| 40 | +#[cfg(all(feature = "console", not(feature = "pci")))] |
| 41 | +use crate::drivers::mmio::get_console_driver; |
24 | 42 | #[cfg(all(
|
25 | 43 | any(feature = "tcp", feature = "udp"),
|
26 | 44 | feature = "gem-net",
|
27 | 45 | not(feature = "pci")
|
28 | 46 | ))]
|
29 | 47 | use crate::drivers::net::gem;
|
| 48 | +#[cfg(all(feature = "console", feature = "pci"))] |
| 49 | +use crate::drivers::pci::get_console_driver; |
30 | 50 | #[cfg(all(
|
31 |
| - any(feature = "tcp", feature = "udp"), |
| 51 | + any(feature = "tcp", feature = "udp", feature = "console"), |
32 | 52 | not(feature = "pci"),
|
33 | 53 | not(feature = "gem-net")
|
34 | 54 | ))]
|
35 | 55 | use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver};
|
36 |
| -#[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))] |
| 56 | +#[cfg(all( |
| 57 | + any(feature = "tcp", feature = "udp", feature = "console"), |
| 58 | + not(feature = "pci") |
| 59 | +))] |
37 | 60 | use crate::kernel::mmio::register_driver;
|
38 | 61 |
|
39 | 62 | static mut PLATFORM_MODEL: Model = Model::Unknown;
|
@@ -172,7 +195,10 @@ pub fn init_drivers() {
|
172 | 195 | }
|
173 | 196 |
|
174 | 197 | // Init virtio-mmio
|
175 |
| - #[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "pci")))] |
| 198 | + #[cfg(all( |
| 199 | + any(feature = "tcp", feature = "udp", feature = "console"), |
| 200 | + not(feature = "pci") |
| 201 | + ))] |
176 | 202 | if let Some(virtio_node) = fdt.find_compatible(&["virtio,mmio"]) {
|
177 | 203 | debug!("Found virtio mmio device");
|
178 | 204 | let virtio_region = virtio_node
|
@@ -218,31 +244,54 @@ pub fn init_drivers() {
|
218 | 244 | // Verify the device-ID to find the network card
|
219 | 245 | let id = mmio.as_ptr().device_id().read();
|
220 | 246 |
|
221 |
| - #[cfg(any(feature = "tcp", feature = "udp"))] |
222 |
| - if id != virtio::Id::Net { |
223 |
| - debug!("It's not a network card at {mmio:p}"); |
224 |
| - return; |
225 |
| - } |
226 |
| - |
227 |
| - info!("Found network card at {mmio:p}"); |
228 |
| - |
229 | 247 | // crate::mm::physicalmem::reserve(
|
230 | 248 | // PhysAddr::from(current_address.align_down(BasePageSize::SIZE as usize)),
|
231 | 249 | // BasePageSize::SIZE as usize,
|
232 | 250 | // );
|
233 | 251 |
|
234 |
| - #[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "gem-net")))] |
235 |
| - if let Ok(VirtioDriver::Network(drv)) = |
236 |
| - mmio_virtio::init_device(mmio, irq.try_into().unwrap()) |
237 |
| - { |
238 |
| - register_driver(MmioDriver::VirtioNet(hermit_sync::InterruptSpinMutex::new( |
239 |
| - drv, |
240 |
| - ))); |
| 252 | + match id { |
| 253 | + #[cfg(all(any(feature = "tcp", feature = "udp"), not(feature = "gem-net")))] |
| 254 | + virtio::Id::Net => { |
| 255 | + debug!("Found virtio network card at {mmio:p}"); |
| 256 | + |
| 257 | + if let Ok(VirtioDriver::Network(drv)) = |
| 258 | + mmio_virtio::init_device(mmio, irq.try_into().unwrap()) |
| 259 | + { |
| 260 | + register_driver(MmioDriver::VirtioNet( |
| 261 | + hermit_sync::InterruptSpinMutex::new(drv), |
| 262 | + )); |
| 263 | + } |
| 264 | + } |
| 265 | + #[cfg(feature = "console")] |
| 266 | + virtio::Id::Console => { |
| 267 | + debug!("Found virtio console at {mmio:p}"); |
| 268 | + |
| 269 | + if let Ok(VirtioDriver::Console(drv)) = |
| 270 | + mmio_virtio::init_device(mmio, irq.try_into().unwrap()) |
| 271 | + { |
| 272 | + register_driver(MmioDriver::VirtioConsole( |
| 273 | + hermit_sync::InterruptSpinMutex::new(*drv), |
| 274 | + )); |
| 275 | + } |
| 276 | + } |
| 277 | + _ => { |
| 278 | + warn!("Found unknown virtio device with ID {id:?} at {mmio:p}"); |
| 279 | + } |
241 | 280 | }
|
242 | 281 | }
|
243 | 282 | }
|
244 | 283 | }
|
245 | 284 |
|
246 | 285 | #[cfg(all(feature = "tcp", not(feature = "pci")))]
|
247 | 286 | super::mmio::MMIO_DRIVERS.finalize();
|
| 287 | + |
| 288 | + #[cfg(feature = "console")] |
| 289 | + { |
| 290 | + if get_console_driver().is_some() { |
| 291 | + info!("Switch to virtio console"); |
| 292 | + crate::console::CONSOLE |
| 293 | + .lock() |
| 294 | + .replace_device(IoDevice::Virtio(VirtioUART::new())); |
| 295 | + } |
| 296 | + } |
248 | 297 | }
|
0 commit comments