Skip to content
Open
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ktask = { path = "core/ktask" }
watchdog = { path = "io/watchdog" }
kcpu = { path = "arch/kcpu" }


# x-kernel Crates
alloc-engine = { path = "mm/alloc-engine" }
page_table = { path = "mm/page_table" }
Expand Down Expand Up @@ -128,7 +129,6 @@ net = { path = "drivers/net" }
pci = { path = "drivers/pci" }
vsock = { path = "drivers/vsock" }
virtio = { path = "drivers/virtio" }
virtio-drivers = { version = "0.12.0", default-features = false }
aarch64-pmuv3 = { path = "drivers/aarch64-pmuv3" }
fatfs = { path = "fs/fatfs", default-features = false }
rsext4 = { path = "fs/rsext4", default-features = false }
Expand All @@ -143,4 +143,4 @@ unittest = { path = "util/unittest" }
kbuild_config = { path = "util/kbuild_config" }

kconfig-gen = { path = "xtask/kconfig-gen" }
smoltcp = { version = "0.12.0", package = "x-smoltcp", default-features = false }
smoltcp = { version = "0.12.0",package = "x-smoltcp", default-features = false }
34 changes: 9 additions & 25 deletions drivers/kdriver/src/bus/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,22 @@
//! PCI bus probing and BAR configuration.
use khal::mem::p2v;
use pci::{
BarInfo, Cam, Command, ConfigurationAccess, DeviceFunction, HeaderType, MemoryBarType, MmioCam,
PciRangeAllocator, PciRoot,
BarInfo, Cam, Command, DeviceFunction, HeaderType, MemoryBarType, PciRangeAllocator, PciRoot,
};

use crate::{AllDevices, prelude::*};

const PCI_BAR_NUM: u8 = 6;

/// Configure PCI BARs and enable the device.
fn config_pci_device<C: ConfigurationAccess>(
root: &mut PciRoot<C>,
fn config_pci_device(
root: &mut PciRoot,
bdf: DeviceFunction,
allocator: &mut Option<PciRangeAllocator>,
) -> DriverResult {
let mut bar = 0;
while bar < PCI_BAR_NUM {
let info = match root.bar_info(bdf, bar).unwrap() {
Some(info) => info,
None => {
bar += 1;
continue;
}
};
let info = root.bar_info(bdf, bar).unwrap();
if let BarInfo::Memory {
address_type,
address,
Expand All @@ -51,14 +44,7 @@ fn config_pci_device<C: ConfigurationAccess>(
}

// read the BAR info again after assignment.
let info = match root.bar_info(bdf, bar).unwrap() {
Some(info) => info,
None => {
bar += 1;
continue;
}
};
let takes_two = info.takes_two_entries();
let info = root.bar_info(bdf, bar).unwrap();
match info {
BarInfo::IO { address, size } => {
if address > 0 && size > 0 {
Expand All @@ -76,7 +62,7 @@ fn config_pci_device<C: ConfigurationAccess>(
" BAR {}: MEM [{:#x}, {:#x}){}{}",
bar,
address,
address + size,
address + size as u64,
if address_type == MemoryBarType::Width64 {
" 64bit"
} else {
Expand All @@ -89,7 +75,7 @@ fn config_pci_device<C: ConfigurationAccess>(
}

bar += 1;
if takes_two {
if info.takes_two_entries() {
bar += 1;
}
}
Expand All @@ -110,13 +96,11 @@ impl AllDevices {
let mut root = {
#[cfg(feature = "pci-mmio")]
{
let cam = unsafe { MmioCam::new(base_vaddr.as_mut_ptr(), Cam::MmioCam) };
PciRoot::new(cam)
unsafe { PciRoot::new(base_vaddr.as_mut_ptr(), Cam::MmioCam) }
}
#[cfg(not(feature = "pci-mmio"))]
{
let cam = unsafe { MmioCam::new(base_vaddr.as_mut_ptr(), Cam::Ecam) };
PciRoot::new(cam)
unsafe { PciRoot::new(base_vaddr.as_mut_ptr(), Cam::Ecam) }
}
};

Expand Down
10 changes: 5 additions & 5 deletions drivers/kdriver/src/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use driver_base::DeviceKind;
#[cfg(feature = "bus-pci")]
use pci::{ConfigurationAccess, DeviceFunction, DeviceFunctionInfo, PciRoot};
use pci::{DeviceFunction, DeviceFunctionInfo, PciRoot};

pub use super::dummy::*;
use crate::DeviceEnum;
Expand All @@ -30,8 +30,8 @@ pub trait DriverProbe {

#[cfg(bus = "pci")]
/// Probe a PCI device described by BDF and device info.
fn probe_pci<C: ConfigurationAccess>(
_root: &mut PciRoot<C>,
fn probe_pci(
_root: &mut PciRoot,
_bdf: DeviceFunction,
_dev_info: &DeviceFunctionInfo,
) -> Option<DeviceEnum> {
Expand Down Expand Up @@ -171,8 +171,8 @@ cfg_if::cfg_if! {
register_net_driver!(IxgbeDriver, net::ixgbe::IxgbeNic<IxgbeHalImpl, 1024, 1>);
impl DriverProbe for IxgbeDriver {
#[cfg(bus = "pci")]
fn probe_pci<C: pci::ConfigurationAccess>(
root: &mut pci::PciRoot<C>,
fn probe_pci(
root: &mut pci::PciRoot,
bdf: pci::DeviceFunction,
dev_info: &pci::DeviceFunctionInfo,
) -> Option<crate::DeviceEnum> {
Expand Down
26 changes: 10 additions & 16 deletions drivers/kdriver/src/virtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use crate::{DeviceEnum, drivers::DriverProbe};

cfg_if! {
if #[cfg(bus = "pci")] {
use pci::{ConfigurationAccess, DeviceFunction, DeviceFunctionInfo, PciRoot};
use pci::{PciRoot, DeviceFunction, DeviceFunctionInfo};
type VirtIoTransport = virtio::PciTransport;
} else if #[cfg(bus = "mmio")] {
type VirtIoTransport = virtio::MmioTransport<'static>;
type VirtIoTransport = virtio::MmioTransport;
}
}

Expand Down Expand Up @@ -139,8 +139,8 @@ impl<D: VirtIoDevMeta> DriverProbe for VirtIoDriver<D> {
}

#[cfg(bus = "pci")]
fn probe_pci<C: ConfigurationAccess>(
root: &mut PciRoot<C>,
fn probe_pci(
root: &mut PciRoot,
bdf: DeviceFunction,
dev_info: &DeviceFunctionInfo,
) -> Option<DeviceEnum> {
Expand All @@ -157,7 +157,7 @@ impl<D: VirtIoDevMeta> DriverProbe for VirtIoDriver<D> {
}

if let Some((ty, transport, irq)) =
virtio::probe_pci_device::<VirtIoHalImpl, C>(root, bdf, dev_info)
virtio::probe_pci_device::<VirtIoHalImpl>(root, bdf, dev_info)
&& ty == D::DEVICE_TYPE
{
match D::try_new(transport, Some(irq)) {
Expand Down Expand Up @@ -214,7 +214,7 @@ unsafe impl VirtIoHal for VirtIoHalImpl {
let layout = Layout::from_size_align(size, PAGE_SIZE).unwrap();
let dma_info = kdma::DMAInfo {
cpu_addr: vaddr,
bus_addr: kdma::DmaBusAddress::new(paddr),
bus_addr: kdma::DmaBusAddress::new(paddr as u64),
};
unsafe { kdma::deallocate_dma_memory(dma_info, layout) };
#[cfg(feature = "crosvm")]
Expand All @@ -226,8 +226,7 @@ unsafe impl VirtIoHal for VirtIoHalImpl {

#[inline]
unsafe fn mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8> {
let paddr_usize = paddr as usize;
NonNull::new(p2v(paddr_usize.into()).as_mut_ptr()).unwrap()
NonNull::new(p2v(paddr.into()).as_mut_ptr()).unwrap()
}

#[allow(unused_variables)]
Expand Down Expand Up @@ -277,8 +276,7 @@ unsafe impl VirtIoHal for VirtIoHalImpl {
#[cfg(not(any(feature = "crosvm", feature = "sev")))]
{
let vaddr = buffer.as_ptr() as *mut u8 as usize;
let paddr_usize: usize = khal::mem::v2p(vaddr.into()).into();
paddr_usize as PhysAddr
khal::mem::v2p(vaddr.into()).into()
}
}

Expand All @@ -298,8 +296,7 @@ unsafe impl VirtIoHal for VirtIoHalImpl {

// If data flows from device to driver, copy back from shared buffer
if direction != BufferDirection::DriverToDevice {
let paddr_usize = paddr as usize;
let shared_ptr = p2v(paddr_usize.into()).as_ptr();
let shared_ptr = p2v(paddr.into()).as_ptr();
unsafe {
core::ptr::copy_nonoverlapping(shared_ptr, buffer.as_ptr() as *mut u8, len);
}
Expand All @@ -317,10 +314,7 @@ unsafe impl VirtIoHal for VirtIoHalImpl {
// Free the bounce buffer via kdma
let layout = Layout::from_size_align(aligned_size, PAGE_SIZE).unwrap();
let dma_info = kdma::DMAInfo {
cpu_addr: {
let paddr_usize = paddr as usize;
NonNull::new(p2v(paddr_usize.into()).as_mut_ptr()).unwrap()
},
cpu_addr: NonNull::new(p2v(paddr.into()).as_mut_ptr()).unwrap(),
bus_addr: kdma::DmaBusAddress::new(paddr as u64),
};
unsafe { kdma::deallocate_dma_memory(dma_info, layout) };
Expand Down
3 changes: 1 addition & 2 deletions drivers/pci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ repository.workspace = true
categories.workspace = true

[dependencies]
virtio-drivers.workspace = true
kbuild_config.workspace = true
virtio-drivers = "0.7.4"
4 changes: 2 additions & 2 deletions drivers/pci/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#![no_std]

pub use virtio_drivers::transport::pci::bus::{
BarInfo, Cam, CapabilityInfo, Command, ConfigurationAccess, DeviceFunction, DeviceFunctionInfo,
HeaderType, MemoryBarType, MmioCam, PciError, PciRoot, Status,
BarInfo, Cam, CapabilityInfo, Command, DeviceFunction, DeviceFunctionInfo, HeaderType,
MemoryBarType, PciError, PciRoot, Status,
};

/// Used to allocate MMIO regions for PCI BARs.
Expand Down
3 changes: 1 addition & 2 deletions drivers/virtio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ input = { workspace = true, optional = true }
net = { workspace = true, optional = true }
vsock = { workspace = true, optional = true }
log = { workspace = true }
virtio-drivers.workspace = true
virtio-drivers = { version = "0.7.4", default-features = false }
unittest = { workspace = true }
zerocopy = "0.8"
3 changes: 1 addition & 2 deletions drivers/virtio/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ impl<H: Hal, T: Transport> InputDriverOps for VirtIoInputDev<H, T> {
fn get_event_bits(&mut self, ty: EventType, out: &mut [u8]) -> DriverResult<bool> {
let read = self
.inner
.query_config_select(InputConfigSelect::EvBits, ty as u8, out)
.map_err(as_driver_error)?;
.query_config_select(InputConfigSelect::EvBits, ty as u8, out);
Ok(read != 0)
}

Expand Down
18 changes: 10 additions & 8 deletions drivers/virtio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub use virtio_drivers::{
},
};

use self::pci::{ConfigurationAccess, DeviceFunction, DeviceFunctionInfo, PciRoot};
use self::pci::{DeviceFunction, DeviceFunctionInfo, PciRoot};
#[cfg(feature = "socket")]
pub use self::socket::VirtIoSocketDev;

Expand All @@ -66,14 +66,14 @@ pub use self::socket::VirtIoSocketDev;
/// for later operations. Otherwise, returns [`None`].
pub fn probe_mmio_device(
reg_base: *mut u8,
reg_size: usize,
) -> Option<(DeviceKind, MmioTransport<'static>)> {
_reg_size: usize,
) -> Option<(DeviceKind, MmioTransport)> {
use core::ptr::NonNull;

use virtio_drivers::transport::mmio::VirtIOHeader;

let header = NonNull::new(reg_base as *mut VirtIOHeader).unwrap();
let transport = unsafe { MmioTransport::new(header, reg_size) }.ok()?;
let transport = unsafe { MmioTransport::new(header) }.ok()?;
let dev_kind = as_device_kind(transport.device_type())?;
Some((dev_kind, transport))
}
Expand All @@ -82,8 +82,8 @@ pub fn probe_mmio_device(
///
/// If the device is recognized, returns the device type and a transport object
/// for later operations. Otherwise, returns [`None`].
pub fn probe_pci_device<H: VirtIoHal, C: ConfigurationAccess>(
root: &mut PciRoot<C>,
pub fn probe_pci_device<H: VirtIoHal>(
root: &mut PciRoot,
bdf: DeviceFunction,
dev_info: &DeviceFunctionInfo,
) -> Option<(DeviceKind, PciTransport, usize)> {
Expand All @@ -99,7 +99,7 @@ pub fn probe_pci_device<H: VirtIoHal, C: ConfigurationAccess>(
const PCI_IRQ_BASE: usize = 0x23;

let dev_kind = virtio_device_type(dev_info).and_then(as_device_kind)?;
let transport = PciTransport::new::<H, C>(root, bdf).ok()?;
let transport = PciTransport::new::<H>(root, bdf).ok()?;
let irq = PCI_IRQ_BASE + (bdf.device & 3) as usize;
Some((dev_kind, transport, irq))
}
Expand Down Expand Up @@ -137,7 +137,9 @@ pub(crate) const fn as_driver_error(e: virtio_drivers::Error) -> DriverError {
OutputBufferTooShort(_) | BufferTooShort | BufferTooLong(..) => {
DriverError::InvalidInput
}
UnexpectedDataInPacket | PeerSocketShutdown => DriverError::Io,
UnexpectedDataInPacket | PeerSocketShutdown | NoResponseReceived | ConnectionFailed => {
DriverError::Io
}
InsufficientBufferSpaceInPeer => DriverError::WouldBlock,
RecycledWrongBuffer => DriverError::BadState,
},
Expand Down
Loading