Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions src/arch/aarch64/kernel/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::drivers::virtio::transport::mmio as mmio_virtio;
feature = "virtio-net",
feature = "virtio-vsock",
))]
use crate::drivers::virtio::transport::mmio::VirtioDriver;
use crate::drivers::virtio::transport::mmio::{Transport, VirtioDriver};
#[cfg(feature = "virtio-vsock")]
use crate::drivers::vsock::VirtioVsockDriver;
#[cfg(feature = "virtio-net")]
Expand All @@ -45,16 +45,16 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
#[allow(clippy::enum_variant_names)]
pub(crate) enum MmioDriver {
#[cfg(feature = "virtio-console")]
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver<Transport>>),
#[cfg(feature = "virtio-fs")]
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
VirtioFs(InterruptTicketMutex<VirtioFsDriver<Transport>>),
#[cfg(feature = "virtio-vsock")]
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver<Transport>>),
}

impl MmioDriver {
#[cfg(feature = "virtio-console")]
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioConsole(drv) => Some(drv),
Expand All @@ -63,7 +63,7 @@ impl MmioDriver {
}

#[cfg(feature = "virtio-fs")]
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioFs(drv) => Some(drv),
Expand All @@ -72,7 +72,7 @@ impl MmioDriver {
}

#[cfg(feature = "virtio-vsock")]
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioVsock(drv) => Some(drv),
Expand All @@ -81,32 +81,39 @@ impl MmioDriver {
}
}

#[cfg(any(feature = "virtio-console", feature = "virtio-fs", feature = "virtio-vsock"))]
#[cfg(any(
feature = "virtio-console",
feature = "virtio-fs",
feature = "virtio-vsock"
))]
pub(crate) fn register_driver(drv: MmioDriver) {
MMIO_DRIVERS.with(|mmio_drivers| mmio_drivers.unwrap().push(drv));
}

#[cfg(feature = "virtio-net")]
pub(crate) type NetworkDevice = VirtioNetDriver;
pub(crate) type NetworkDevice = VirtioNetDriver<Transport>;

#[cfg(feature = "virtio-console")]
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
pub(crate) fn get_console_driver()
-> Option<&'static InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_console_driver())
}

#[cfg(feature = "virtio-fs")]
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver>> {
pub(crate) fn get_filesystem_driver()
-> Option<&'static InterruptTicketMutex<VirtioFsDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_filesystem_driver())
}

#[cfg(feature = "virtio-vsock")]
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptTicketMutex<VirtioVsockDriver>> {
pub(crate) fn get_vsock_driver()
-> Option<&'static InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
Expand Down Expand Up @@ -209,25 +216,29 @@ pub fn init_drivers() {
} else {
panic!("Invalid interrupt type");
};
gic.set_interrupt_priority(virtio_irqid, Some(cpu_id), 0x00).unwrap();
gic.set_interrupt_priority(virtio_irqid, Some(cpu_id), 0x00)
.unwrap();
if (irqflags & 0xf) == 4 || (irqflags & 0xf) == 8 {
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Level).unwrap();
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Level)
.unwrap();
} else if (irqflags & 0xf) == 2 || (irqflags & 0xf) == 1 {
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Edge).unwrap();
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Edge)
.unwrap();
} else {
panic!("Invalid interrupt level!");
}
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true).unwrap();
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true)
.unwrap();

match drv {
#[cfg(feature = "virtio-console")]
VirtioDriver::Console(drv) => register_driver(MmioDriver::VirtioConsole(
InterruptTicketMutex::new(*drv),
)),
#[cfg(feature = "virtio-fs")]
VirtioDriver::Fs(drv) => register_driver(MmioDriver::VirtioFs(
InterruptTicketMutex::new(*drv),
)),
VirtioDriver::Fs(drv) => {
register_driver(MmioDriver::VirtioFs(InterruptTicketMutex::new(*drv)))
}
#[cfg(feature = "virtio-net")]
VirtioDriver::Net(drv) => *NETWORK_DEVICE.lock() = Some(*drv),
#[cfg(feature = "virtio-vsock")]
Expand Down
25 changes: 15 additions & 10 deletions src/arch/riscv64/kernel/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::drivers::fs::VirtioFsDriver;
use crate::drivers::net::gem::GEMDriver;
#[cfg(all(not(feature = "gem-net"), feature = "virtio-net"))]
use crate::drivers::net::virtio::VirtioNetDriver;
#[cfg(feature = "virtio")]
use crate::drivers::virtio::transport::mmio::Transport;
#[cfg(feature = "virtio-vsock")]
use crate::drivers::vsock::VirtioVsockDriver;
use crate::init_cell::InitCell;
Expand All @@ -24,16 +26,16 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
#[allow(clippy::enum_variant_names)]
pub(crate) enum MmioDriver {
#[cfg(feature = "virtio-console")]
VirtioConsole(InterruptSpinMutex<VirtioConsoleDriver>),
VirtioConsole(InterruptSpinMutex<VirtioConsoleDriver<Transport>>),
#[cfg(feature = "virtio-fs")]
VirtioFs(InterruptSpinMutex<VirtioFsDriver>),
VirtioFs(InterruptSpinMutex<VirtioFsDriver<Transport>>),
#[cfg(feature = "virtio-vsock")]
VirtioVsock(InterruptSpinMutex<VirtioVsockDriver>),
VirtioVsock(InterruptSpinMutex<VirtioVsockDriver<Transport>>),
}

impl MmioDriver {
#[cfg(feature = "virtio-console")]
fn get_console_driver(&self) -> Option<&InterruptSpinMutex<VirtioConsoleDriver>> {
fn get_console_driver(&self) -> Option<&InterruptSpinMutex<VirtioConsoleDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioConsole(drv) => Some(drv),
Expand All @@ -42,7 +44,7 @@ impl MmioDriver {
}

#[cfg(feature = "virtio-fs")]
fn get_filesystem_driver(&self) -> Option<&InterruptSpinMutex<VirtioFsDriver>> {
fn get_filesystem_driver(&self) -> Option<&InterruptSpinMutex<VirtioFsDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioFs(drv) => Some(drv),
Expand All @@ -51,7 +53,7 @@ impl MmioDriver {
}

#[cfg(feature = "virtio-vsock")]
fn get_vsock_driver(&self) -> Option<&InterruptSpinMutex<VirtioVsockDriver>> {
fn get_vsock_driver(&self) -> Option<&InterruptSpinMutex<VirtioVsockDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioVsock(drv) => Some(drv),
Expand All @@ -73,26 +75,29 @@ pub(crate) fn register_driver(drv: MmioDriver) {
pub(crate) type NetworkDevice = GEMDriver;

#[cfg(all(not(feature = "gem-net"), feature = "virtio-net"))]
pub(crate) type NetworkDevice = VirtioNetDriver;
pub(crate) type NetworkDevice = VirtioNetDriver<Transport>;

#[cfg(feature = "virtio-console")]
pub(crate) fn get_console_driver() -> Option<&'static InterruptSpinMutex<VirtioConsoleDriver>> {
pub(crate) fn get_console_driver()
-> Option<&'static InterruptSpinMutex<VirtioConsoleDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_console_driver())
}

#[cfg(feature = "virtio-fs")]
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptSpinMutex<VirtioFsDriver>> {
pub(crate) fn get_filesystem_driver()
-> Option<&'static InterruptSpinMutex<VirtioFsDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_filesystem_driver())
}

#[cfg(feature = "virtio-vsock")]
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptSpinMutex<VirtioVsockDriver>> {
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptSpinMutex<VirtioVsockDriver<Transport>>>
{
MMIO_DRIVERS
.get()?
.iter()
Expand Down
25 changes: 14 additions & 11 deletions src/arch/x86_64/kernel/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::drivers::virtio::transport::mmio as mmio_virtio;
feature = "virtio-net",
feature = "virtio-vsock",
))]
use crate::drivers::virtio::transport::mmio::VirtioDriver;
use crate::drivers::virtio::transport::mmio::{Transport, VirtioDriver};
#[cfg(feature = "virtio-vsock")]
use crate::drivers::vsock::VirtioVsockDriver;
use crate::env;
Expand All @@ -49,16 +49,16 @@ static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::new());
#[allow(clippy::enum_variant_names)]
pub(crate) enum MmioDriver {
#[cfg(feature = "virtio-console")]
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver<Transport>>),
#[cfg(feature = "virtio-fs")]
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
VirtioFs(InterruptTicketMutex<VirtioFsDriver<Transport>>),
#[cfg(feature = "virtio-vsock")]
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver<Transport>>),
}

impl MmioDriver {
#[cfg(feature = "virtio-console")]
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioConsole(drv) => Some(drv),
Expand All @@ -67,7 +67,7 @@ impl MmioDriver {
}

#[cfg(feature = "virtio-fs")]
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioFs(drv) => Some(drv),
Expand All @@ -76,7 +76,7 @@ impl MmioDriver {
}

#[cfg(feature = "virtio-vsock")]
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
#[allow(unreachable_patterns)]
match self {
Self::VirtioVsock(drv) => Some(drv),
Expand Down Expand Up @@ -191,26 +191,29 @@ pub(crate) fn register_driver(drv: MmioDriver) {
}

#[cfg(feature = "virtio-net")]
pub(crate) type NetworkDevice = VirtioNetDriver;
pub(crate) type NetworkDevice = VirtioNetDriver<Transport>;

#[cfg(feature = "virtio-console")]
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
pub(crate) fn get_console_driver()
-> Option<&'static InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_console_driver())
}

#[cfg(feature = "virtio-fs")]
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver>> {
pub(crate) fn get_filesystem_driver()
-> Option<&'static InterruptTicketMutex<VirtioFsDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_filesystem_driver())
}

#[cfg(feature = "virtio-vsock")]
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptTicketMutex<VirtioVsockDriver>> {
pub(crate) fn get_vsock_driver()
-> Option<&'static InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
MMIO_DRIVERS
.get()?
.iter()
Expand Down
10 changes: 5 additions & 5 deletions src/drivers/console/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use volatile::VolatileRef;
use crate::drivers::InterruptLine;
use crate::drivers::console::{ConsoleDevCfg, RxQueue, TxQueue, VirtioConsoleDriver};
use crate::drivers::virtio::error::VirtioError;
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg, Transport};

// Backend-dependent interface for Virtio console driver
impl VirtioConsoleDriver {
impl VirtioConsoleDriver<Transport> {
pub fn new(
dev_id: u16,
mut registers: VolatileRef<'static, DeviceRegisters>,
irq: InterruptLine,
) -> Result<VirtioConsoleDriver, VirtioError> {
) -> Result<Self, VirtioError> {
let dev_cfg_raw: &'static Config = unsafe {
&*registers
.borrow_mut()
Expand All @@ -32,7 +32,7 @@ impl VirtioConsoleDriver {
let isr_stat = IsrStatus::new(registers.borrow_mut());
let notif_cfg = NotifCfg::new(registers.borrow_mut());

Ok(VirtioConsoleDriver {
Ok(Self {
dev_cfg,
com_cfg: ComCfg::new(registers),
isr_stat,
Expand All @@ -52,7 +52,7 @@ impl VirtioConsoleDriver {
dev_id: u16,
registers: VolatileRef<'static, DeviceRegisters>,
irq: InterruptLine,
) -> Result<VirtioConsoleDriver, VirtioError> {
) -> Result<Self, VirtioError> {
let mut drv = VirtioConsoleDriver::new(dev_id, registers, irq)?;
drv.init_dev().map_err(VirtioError::ConsoleDriver)?;
drv.com_cfg.print_information();
Expand Down
Loading
Loading