Skip to content

Commit f7af9df

Browse files
committed
refactor(virtio): make devices generic over transport
Instead of relying on importing one of the PCI and MMIO transport modules that implement identically named structs to select the transport used by VIRTIO device drivers, use generics. This is in preparation for removing the mutual exclusivity of PCI and MMIO-based devices.
1 parent ce6a236 commit f7af9df

22 files changed

Lines changed: 504 additions & 362 deletions

File tree

src/arch/aarch64/kernel/mmio.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::drivers::fs::VirtioFsDriver;
2626
#[cfg(feature = "virtio-net")]
2727
use crate::drivers::net::virtio::VirtioNetDriver;
2828
use crate::drivers::virtio::transport::mmio as mmio_virtio;
29+
use crate::drivers::virtio::transport::mmio::Transport;
2930
#[cfg(any(
3031
feature = "virtio-console",
3132
feature = "virtio-fs",
@@ -45,16 +46,16 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
4546
#[allow(clippy::enum_variant_names)]
4647
pub(crate) enum MmioDriver {
4748
#[cfg(feature = "virtio-console")]
48-
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
49+
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver<Transport>>),
4950
#[cfg(feature = "virtio-fs")]
50-
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
51+
VirtioFs(InterruptTicketMutex<VirtioFsDriver<Transport>>),
5152
#[cfg(feature = "virtio-vsock")]
52-
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
53+
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver<Transport>>),
5354
}
5455

5556
impl MmioDriver {
5657
#[cfg(feature = "virtio-console")]
57-
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
58+
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
5859
#[allow(unreachable_patterns)]
5960
match self {
6061
Self::VirtioConsole(drv) => Some(drv),
@@ -63,7 +64,7 @@ impl MmioDriver {
6364
}
6465

6566
#[cfg(feature = "virtio-fs")]
66-
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
67+
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<Transport>>> {
6768
#[allow(unreachable_patterns)]
6869
match self {
6970
Self::VirtioFs(drv) => Some(drv),
@@ -72,7 +73,7 @@ impl MmioDriver {
7273
}
7374

7475
#[cfg(feature = "virtio-vsock")]
75-
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
76+
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
7677
#[allow(unreachable_patterns)]
7778
match self {
7879
Self::VirtioVsock(drv) => Some(drv),
@@ -81,32 +82,39 @@ impl MmioDriver {
8182
}
8283
}
8384

84-
#[cfg(any(feature = "virtio-console", feature = "virtio-fs", feature = "virtio-vsock"))]
85+
#[cfg(any(
86+
feature = "virtio-console",
87+
feature = "virtio-fs",
88+
feature = "virtio-vsock"
89+
))]
8590
pub(crate) fn register_driver(drv: MmioDriver) {
8691
MMIO_DRIVERS.with(|mmio_drivers| mmio_drivers.unwrap().push(drv));
8792
}
8893

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

9297
#[cfg(feature = "virtio-console")]
93-
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
98+
pub(crate) fn get_console_driver()
99+
-> Option<&'static InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
94100
MMIO_DRIVERS
95101
.get()?
96102
.iter()
97103
.find_map(|drv| drv.get_console_driver())
98104
}
99105

100106
#[cfg(feature = "virtio-fs")]
101-
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver>> {
107+
pub(crate) fn get_filesystem_driver()
108+
-> Option<&'static InterruptTicketMutex<VirtioFsDriver<Transport>>> {
102109
MMIO_DRIVERS
103110
.get()?
104111
.iter()
105112
.find_map(|drv| drv.get_filesystem_driver())
106113
}
107114

108115
#[cfg(feature = "virtio-vsock")]
109-
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptTicketMutex<VirtioVsockDriver>> {
116+
pub(crate) fn get_vsock_driver()
117+
-> Option<&'static InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
110118
MMIO_DRIVERS
111119
.get()?
112120
.iter()
@@ -209,25 +217,29 @@ pub fn init_drivers() {
209217
} else {
210218
panic!("Invalid interrupt type");
211219
};
212-
gic.set_interrupt_priority(virtio_irqid, Some(cpu_id), 0x00).unwrap();
220+
gic.set_interrupt_priority(virtio_irqid, Some(cpu_id), 0x00)
221+
.unwrap();
213222
if (irqflags & 0xf) == 4 || (irqflags & 0xf) == 8 {
214-
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Level).unwrap();
223+
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Level)
224+
.unwrap();
215225
} else if (irqflags & 0xf) == 2 || (irqflags & 0xf) == 1 {
216-
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Edge).unwrap();
226+
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Edge)
227+
.unwrap();
217228
} else {
218229
panic!("Invalid interrupt level!");
219230
}
220-
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true).unwrap();
231+
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true)
232+
.unwrap();
221233

222234
match drv {
223235
#[cfg(feature = "virtio-console")]
224236
VirtioDriver::Console(drv) => register_driver(MmioDriver::VirtioConsole(
225237
InterruptTicketMutex::new(*drv),
226238
)),
227239
#[cfg(feature = "virtio-fs")]
228-
VirtioDriver::Fs(drv) => register_driver(MmioDriver::VirtioFs(
229-
InterruptTicketMutex::new(*drv),
230-
)),
240+
VirtioDriver::Fs(drv) => {
241+
register_driver(MmioDriver::VirtioFs(InterruptTicketMutex::new(*drv)))
242+
}
231243
#[cfg(feature = "virtio-net")]
232244
VirtioDriver::Net(drv) => *NETWORK_DEVICE.lock() = Some(*drv),
233245
#[cfg(feature = "virtio-vsock")]

src/arch/riscv64/kernel/mmio.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::drivers::fs::VirtioFsDriver;
1515
use crate::drivers::net::gem::GEMDriver;
1616
#[cfg(all(not(feature = "gem-net"), feature = "virtio-net"))]
1717
use crate::drivers::net::virtio::VirtioNetDriver;
18+
use crate::drivers::virtio::transport::mmio::Transport;
1819
#[cfg(feature = "virtio-vsock")]
1920
use crate::drivers::vsock::VirtioVsockDriver;
2021
use crate::init_cell::InitCell;
@@ -24,16 +25,16 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
2425
#[allow(clippy::enum_variant_names)]
2526
pub(crate) enum MmioDriver {
2627
#[cfg(feature = "virtio-console")]
27-
VirtioConsole(InterruptSpinMutex<VirtioConsoleDriver>),
28+
VirtioConsole(InterruptSpinMutex<VirtioConsoleDriver<Transport>>),
2829
#[cfg(feature = "virtio-fs")]
29-
VirtioFs(InterruptSpinMutex<VirtioFsDriver>),
30+
VirtioFs(InterruptSpinMutex<VirtioFsDriver<Transport>>),
3031
#[cfg(feature = "virtio-vsock")]
31-
VirtioVsock(InterruptSpinMutex<VirtioVsockDriver>),
32+
VirtioVsock(InterruptSpinMutex<VirtioVsockDriver<Transport>>),
3233
}
3334

3435
impl MmioDriver {
3536
#[cfg(feature = "virtio-console")]
36-
fn get_console_driver(&self) -> Option<&InterruptSpinMutex<VirtioConsoleDriver>> {
37+
fn get_console_driver(&self) -> Option<&InterruptSpinMutex<VirtioConsoleDriver<Transport>>> {
3738
#[allow(unreachable_patterns)]
3839
match self {
3940
Self::VirtioConsole(drv) => Some(drv),
@@ -42,7 +43,7 @@ impl MmioDriver {
4243
}
4344

4445
#[cfg(feature = "virtio-fs")]
45-
fn get_filesystem_driver(&self) -> Option<&InterruptSpinMutex<VirtioFsDriver>> {
46+
fn get_filesystem_driver(&self) -> Option<&InterruptSpinMutex<VirtioFsDriver<Transport>>> {
4647
#[allow(unreachable_patterns)]
4748
match self {
4849
Self::VirtioFs(drv) => Some(drv),
@@ -51,7 +52,7 @@ impl MmioDriver {
5152
}
5253

5354
#[cfg(feature = "virtio-vsock")]
54-
fn get_vsock_driver(&self) -> Option<&InterruptSpinMutex<VirtioVsockDriver>> {
55+
fn get_vsock_driver(&self) -> Option<&InterruptSpinMutex<VirtioVsockDriver<Transport>>> {
5556
#[allow(unreachable_patterns)]
5657
match self {
5758
Self::VirtioVsock(drv) => Some(drv),
@@ -73,26 +74,29 @@ pub(crate) fn register_driver(drv: MmioDriver) {
7374
pub(crate) type NetworkDevice = GEMDriver;
7475

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

7879
#[cfg(feature = "virtio-console")]
79-
pub(crate) fn get_console_driver() -> Option<&'static InterruptSpinMutex<VirtioConsoleDriver>> {
80+
pub(crate) fn get_console_driver()
81+
-> Option<&'static InterruptSpinMutex<VirtioConsoleDriver<Transport>>> {
8082
MMIO_DRIVERS
8183
.get()?
8284
.iter()
8385
.find_map(|drv| drv.get_console_driver())
8486
}
8587

8688
#[cfg(feature = "virtio-fs")]
87-
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptSpinMutex<VirtioFsDriver>> {
89+
pub(crate) fn get_filesystem_driver()
90+
-> Option<&'static InterruptSpinMutex<VirtioFsDriver<Transport>>> {
8891
MMIO_DRIVERS
8992
.get()?
9093
.iter()
9194
.find_map(|drv| drv.get_filesystem_driver())
9295
}
9396

9497
#[cfg(feature = "virtio-vsock")]
95-
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptSpinMutex<VirtioVsockDriver>> {
98+
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptSpinMutex<VirtioVsockDriver<Transport>>>
99+
{
96100
MMIO_DRIVERS
97101
.get()?
98102
.iter()

src/arch/x86_64/kernel/mmio.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::drivers::fs::VirtioFsDriver;
2727
#[cfg(feature = "virtio-net")]
2828
use crate::drivers::net::virtio::VirtioNetDriver;
2929
use crate::drivers::virtio::transport::mmio as mmio_virtio;
30+
use crate::drivers::virtio::transport::mmio::Transport;
3031
#[cfg(any(
3132
feature = "virtio-console",
3233
feature = "virtio-fs",
@@ -49,16 +50,16 @@ static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::new());
4950
#[allow(clippy::enum_variant_names)]
5051
pub(crate) enum MmioDriver {
5152
#[cfg(feature = "virtio-console")]
52-
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
53+
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver<Transport>>),
5354
#[cfg(feature = "virtio-fs")]
54-
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
55+
VirtioFs(InterruptTicketMutex<VirtioFsDriver<Transport>>),
5556
#[cfg(feature = "virtio-vsock")]
56-
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
57+
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver<Transport>>),
5758
}
5859

5960
impl MmioDriver {
6061
#[cfg(feature = "virtio-console")]
61-
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
62+
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
6263
#[allow(unreachable_patterns)]
6364
match self {
6465
Self::VirtioConsole(drv) => Some(drv),
@@ -67,7 +68,7 @@ impl MmioDriver {
6768
}
6869

6970
#[cfg(feature = "virtio-fs")]
70-
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
71+
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<Transport>>> {
7172
#[allow(unreachable_patterns)]
7273
match self {
7374
Self::VirtioFs(drv) => Some(drv),
@@ -76,7 +77,7 @@ impl MmioDriver {
7677
}
7778

7879
#[cfg(feature = "virtio-vsock")]
79-
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
80+
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
8081
#[allow(unreachable_patterns)]
8182
match self {
8283
Self::VirtioVsock(drv) => Some(drv),
@@ -191,26 +192,29 @@ pub(crate) fn register_driver(drv: MmioDriver) {
191192
}
192193

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

196197
#[cfg(feature = "virtio-console")]
197-
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
198+
pub(crate) fn get_console_driver()
199+
-> Option<&'static InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
198200
MMIO_DRIVERS
199201
.get()?
200202
.iter()
201203
.find_map(|drv| drv.get_console_driver())
202204
}
203205

204206
#[cfg(feature = "virtio-fs")]
205-
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver>> {
207+
pub(crate) fn get_filesystem_driver()
208+
-> Option<&'static InterruptTicketMutex<VirtioFsDriver<Transport>>> {
206209
MMIO_DRIVERS
207210
.get()?
208211
.iter()
209212
.find_map(|drv| drv.get_filesystem_driver())
210213
}
211214

212215
#[cfg(feature = "virtio-vsock")]
213-
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptTicketMutex<VirtioVsockDriver>> {
216+
pub(crate) fn get_vsock_driver()
217+
-> Option<&'static InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
214218
MMIO_DRIVERS
215219
.get()?
216220
.iter()

src/drivers/console/mmio.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use volatile::VolatileRef;
55
use crate::drivers::InterruptLine;
66
use crate::drivers::console::{ConsoleDevCfg, RxQueue, TxQueue, VirtioConsoleDriver};
77
use crate::drivers::virtio::error::VirtioError;
8-
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
8+
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg, Transport};
99

1010
// Backend-dependent interface for Virtio console driver
11-
impl VirtioConsoleDriver {
11+
impl VirtioConsoleDriver<Transport> {
1212
pub fn new(
1313
dev_id: u16,
1414
mut registers: VolatileRef<'static, DeviceRegisters>,
1515
irq: InterruptLine,
16-
) -> Result<VirtioConsoleDriver, VirtioError> {
16+
) -> Result<Self, VirtioError> {
1717
let dev_cfg_raw: &'static Config = unsafe {
1818
&*registers
1919
.borrow_mut()
@@ -32,7 +32,7 @@ impl VirtioConsoleDriver {
3232
let isr_stat = IsrStatus::new(registers.borrow_mut());
3333
let notif_cfg = NotifCfg::new(registers.borrow_mut());
3434

35-
Ok(VirtioConsoleDriver {
35+
Ok(Self {
3636
dev_cfg,
3737
com_cfg: ComCfg::new(registers),
3838
isr_stat,
@@ -52,7 +52,7 @@ impl VirtioConsoleDriver {
5252
dev_id: u16,
5353
registers: VolatileRef<'static, DeviceRegisters>,
5454
irq: InterruptLine,
55-
) -> Result<VirtioConsoleDriver, VirtioError> {
55+
) -> Result<Self, VirtioError> {
5656
let mut drv = VirtioConsoleDriver::new(dev_id, registers, irq)?;
5757
drv.init_dev().map_err(VirtioError::ConsoleDriver)?;
5858
drv.com_cfg.print_information();

0 commit comments

Comments
 (0)