From 3299a070f23b5a4c2102b502382eafd2d1f1c5a9 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:21:37 +0200 Subject: [PATCH] pcid: Also return bar size from physmap_mem and use it in two more drivers --- audio/ihdad/src/main.rs | 2 +- net/e1000d/src/main.rs | 2 +- net/ixgbed/src/main.rs | 16 +++------------ net/rtl8139d/src/main.rs | 2 +- net/rtl8168d/src/main.rs | 2 +- pcid/src/pci/bar.rs | 7 ++++--- storage/ahcid/src/main.rs | 2 +- storage/nvmed/src/main.rs | 37 ++++++++++++++-------------------- vboxd/src/main.rs | 2 +- virtio-core/src/arch/x86_64.rs | 2 +- xhcid/src/main.rs | 2 +- 11 files changed, 30 insertions(+), 46 deletions(-) diff --git a/audio/ihdad/src/main.rs b/audio/ihdad/src/main.rs index e86cc832a4..a8e6aa4ef3 100755 --- a/audio/ihdad/src/main.rs +++ b/audio/ihdad/src/main.rs @@ -146,7 +146,7 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { log::info!(" + IHDA {}", pci_config.func.display()); - let address = unsafe { bar.physmap_mem("ihdad") } as usize; + let address = unsafe { bar.physmap_mem("ihdad") }.0 as usize; //TODO: MSI-X let mut irq_file = get_int_method(&mut pcid_handle); diff --git a/net/e1000d/src/main.rs b/net/e1000d/src/main.rs index b5480587bc..db893106e7 100644 --- a/net/e1000d/src/main.rs +++ b/net/e1000d/src/main.rs @@ -33,7 +33,7 @@ fn main() { redox_daemon::Daemon::new(move |daemon| { let mut irq_file = irq.irq_handle("e1000d"); - let address = unsafe { bar.physmap_mem("e1000d") } as usize; + let address = unsafe { bar.physmap_mem("e1000d") }.0 as usize; let device = unsafe { device::Intel8254x::new(address).expect("e1000d: failed to allocate device") }; diff --git a/net/ixgbed/src/main.rs b/net/ixgbed/src/main.rs index 716253324a..2c54c73ee2 100644 --- a/net/ixgbed/src/main.rs +++ b/net/ixgbed/src/main.rs @@ -13,8 +13,6 @@ pub mod device; #[rustfmt::skip] mod ixgbe; -const IXGBE_MMIO_SIZE: usize = 512 * 1024; - fn main() { let mut pcid_handle = PcidServerHandle::connect_default().expect("ixgbed: failed to setup channel to pcid"); @@ -25,8 +23,6 @@ fn main() { let mut name = pci_config.func.name(); name.push_str("_ixgbe"); - let (bar, _) = pci_config.func.bars[0].expect_mem(); - let irq = pci_config .func .legacy_interrupt_line @@ -37,17 +33,11 @@ fn main() { redox_daemon::Daemon::new(move |daemon| { let mut irq_file = irq.irq_handle("ixgbed"); - let address = unsafe { - common::physmap( - bar, - IXGBE_MMIO_SIZE, - common::Prot::RW, - common::MemoryType::Uncacheable, - ) - .expect("ixgbed: failed to map address") as usize + let (address, size) = unsafe { + pci_config.func.bars[0].physmap_mem("ixgbed") }; - let device = device::Intel8259x::new(address, IXGBE_MMIO_SIZE) + let device = device::Intel8259x::new(address as usize, size) .expect("ixgbed: failed to allocate device"); let mut scheme = NetworkScheme::new(device, format!("network.{name}")); diff --git a/net/rtl8139d/src/main.rs b/net/rtl8139d/src/main.rs index a949d46e18..04cba8fc48 100644 --- a/net/rtl8139d/src/main.rs +++ b/net/rtl8139d/src/main.rs @@ -136,7 +136,7 @@ fn get_int_method(pcid_handle: &mut PcidServerHandle) -> File { msix_info.validate(pci_config.func.bars); let bar = &pci_config.func.bars[msix_info.table_bar as usize]; - let bar_address = unsafe { bar.physmap_mem("rtl8139d") } as usize; + let bar_address = unsafe { bar.physmap_mem("rtl8139d") }.0 as usize; let virt_table_base = (bar_address + msix_info.table_offset as usize) as *mut MsixTableEntry; diff --git a/net/rtl8168d/src/main.rs b/net/rtl8168d/src/main.rs index c797c2e059..f8177b98d8 100644 --- a/net/rtl8168d/src/main.rs +++ b/net/rtl8168d/src/main.rs @@ -131,7 +131,7 @@ fn get_int_method(pcid_handle: &mut PcidServerHandle) -> File { msix_info.validate(pci_config.func.bars); let bar = &pci_config.func.bars[msix_info.table_bar as usize]; - let bar_address = unsafe { bar.physmap_mem("rtl8168d") } as usize; + let bar_address = unsafe { bar.physmap_mem("rtl8168d") }.0 as usize; let virt_table_base = (bar_address + msix_info.table_offset as usize) as *mut MsixTableEntry; diff --git a/pcid/src/pci/bar.rs b/pcid/src/pci/bar.rs index b279c44577..706479008b 100644 --- a/pcid/src/pci/bar.rs +++ b/pcid/src/pci/bar.rs @@ -53,9 +53,9 @@ impl PciBar { } } - pub unsafe fn physmap_mem(&self, driver: &str) -> *mut () { + pub unsafe fn physmap_mem(&self, driver: &str) -> (*mut (), usize) { let (bar, bar_size) = self.expect_mem(); - unsafe { + let addr = unsafe { common::physmap( bar, bar_size, @@ -64,6 +64,7 @@ impl PciBar { common::MemoryType::Uncacheable, ) } - .unwrap_or_else(|err| panic!("{driver}: failed to map BAR at {bar:016X}: {err}")) + .unwrap_or_else(|err| panic!("{driver}: failed to map BAR at {bar:016X}: {err}")); + (addr, bar_size) } } diff --git a/storage/ahcid/src/main.rs b/storage/ahcid/src/main.rs index 7aae947b78..a5bb6f79db 100644 --- a/storage/ahcid/src/main.rs +++ b/storage/ahcid/src/main.rs @@ -88,7 +88,7 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { info!(" + AHCI {}", pci_config.func.display()); - let address = unsafe { bar.physmap_mem("ahcid") }; + let (address, _) = unsafe { bar.physmap_mem("ahcid") }; { let scheme_name = format!("disk.{}", name); let socket = Socket::::nonblock(&scheme_name).expect("ahcid: failed to create disk scheme"); diff --git a/storage/nvmed/src/main.rs b/storage/nvmed/src/main.rs index 39167633d2..6910e0a65b 100644 --- a/storage/nvmed/src/main.rs +++ b/storage/nvmed/src/main.rs @@ -27,22 +27,12 @@ mod scheme; /// A wrapper for a BAR allocation. pub struct Bar { ptr: NonNull, - physical: usize, bar_size: usize, } impl Bar { - pub fn allocate(bar: usize, bar_size: usize) -> Result { + pub fn new(ptr: *mut (), bar_size: usize) -> Result { Ok(Self { - ptr: NonNull::new( - unsafe { common::physmap( - bar, - bar_size, - common::Prot { read: true, write: true }, - common::MemoryType::Uncacheable, - )? as *mut u8 }, - ) - .expect("Mapping a BAR resulted in a nullptr"), - physical: bar, + ptr: NonNull::new(ptr.cast::()).expect("Mapping a BAR resulted in a nullptr"), bar_size, }) } @@ -101,9 +91,9 @@ fn get_int_method( match &mut *bar_guard { &mut Some(ref bar) => Ok(bar.ptr), bar_to_set @ &mut None => { - let (bar, bar_size) = function.bars[bir].expect_mem(); + let (ptr, bar_size) = unsafe { function.bars[bir].physmap_mem("nvmed") }; - let bar = Bar::allocate(bar, bar_size)?; + let bar = Bar::new(ptr, bar_size)?; *bar_to_set = Some(bar); Ok(bar_to_set.as_ref().unwrap().ptr) } @@ -265,18 +255,16 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { let _logger_ref = setup_logging(&scheme_name); - let bar = &pci_config.func.bars[0]; - let (bar_ptr, bar_size) = bar.expect_mem(); - log::debug!("NVME PCI CONFIG: {:?}", pci_config); let allocated_bars = AllocatedBars::default(); - let address = unsafe { bar.physmap_mem("nvmed") } as usize; + let bar = &pci_config.func.bars[0]; + let (address, bar_size) = unsafe { bar.physmap_mem("nvmed") }; + *allocated_bars.0[0].lock().unwrap() = Some(Bar { - physical: bar_ptr, bar_size, - ptr: NonNull::new(address as *mut u8).expect("Physmapping BAR gave nullptr"), + ptr: NonNull::new(address.cast::()).expect("Physmapping BAR gave nullptr"), }); let socket = Socket::::create(&scheme_name).expect("nvmed: failed to create disk scheme"); @@ -287,8 +275,13 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { let (interrupt_method, interrupt_sources) = get_int_method(&mut pcid_handle, &pci_config.func, &allocated_bars) .expect("nvmed: failed to find a suitable interrupt method"); - let mut nvme = Nvme::new(address, interrupt_method, pcid_handle, reactor_sender) - .expect("nvmed: failed to allocate driver data"); + let mut nvme = Nvme::new( + address as usize, + interrupt_method, + pcid_handle, + reactor_sender, + ) + .expect("nvmed: failed to allocate driver data"); unsafe { nvme.init() } log::debug!("Finished base initialization"); let nvme = Arc::new(nvme); diff --git a/vboxd/src/main.rs b/vboxd/src/main.rs index b24ddb0dcf..be65b6a11d 100644 --- a/vboxd/src/main.rs +++ b/vboxd/src/main.rs @@ -217,7 +217,7 @@ fn main() { let mut irq_file = irq.irq_handle("vboxd"); let mut port = Pio::::new(bar0 as u16); - let address = unsafe { bar1.physmap_mem("vboxd") }; + let (address, _) = unsafe { bar1.physmap_mem("vboxd") }; { let vmmdev = unsafe { &mut *(address as *mut VboxVmmDev) }; diff --git a/virtio-core/src/arch/x86_64.rs b/virtio-core/src/arch/x86_64.rs index e1b25fbbb5..95072fd0ac 100644 --- a/virtio-core/src/arch/x86_64.rs +++ b/virtio-core/src/arch/x86_64.rs @@ -19,7 +19,7 @@ pub fn enable_msix(pcid_handle: &mut PcidServerHandle) -> Result { msix_info.validate(pci_config.func.bars); let bar = &pci_config.func.bars[msix_info.table_bar as usize]; - let bar_address = unsafe { bar.physmap_mem("virtio-core") } as usize; + let bar_address = unsafe { bar.physmap_mem("virtio-core") }.0 as usize; let virt_table_base = (bar_address + msix_info.table_offset as usize) as *mut MsixTableEntry; let mut info = MappedMsixRegs { diff --git a/xhcid/src/main.rs b/xhcid/src/main.rs index ef5b122beb..3c3aa0dbce 100644 --- a/xhcid/src/main.rs +++ b/xhcid/src/main.rs @@ -195,7 +195,7 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { log::debug!("XHCI PCI CONFIG: {:?}", pci_config); let bar = &pci_config.func.bars[0]; - let address = unsafe { bar.physmap_mem("xhcid") } as usize; + let address = unsafe { bar.physmap_mem("xhcid") }.0 as usize; let (irq_file, interrupt_method) = (None, InterruptMethod::Polling); //TODO: get_int_method(&mut pcid_handle, address);