Skip to content

Commit

Permalink
enumerate PCI bus
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyukai0403 committed Jan 17, 2025
1 parent 8242c2c commit a4ac06a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
18 changes: 6 additions & 12 deletions kernel/src/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ pub struct MADT {
#[derive(Debug, Copy, Clone)]
#[repr(packed)]
pub struct PCIE_CFG_ALLOC {
base_addr: u64,
pci_segment_group_number: u16,
start_pci_bus_number: u8,
end_pci_bus_number: u8,
reserved: u32,
pub base_addr: u64,
pub pci_segment_group_number: u16,
pub start_pci_bus_number: u8,
pub end_pci_bus_number: u8,
pub reserved: u32,
}

#[derive(Debug)]
#[repr(packed)]
pub struct MCFG {
header: ACPI_table_header,
reserved: u64,
alloc: PCIE_CFG_ALLOC,
pub alloc: PCIE_CFG_ALLOC,
}

pub static mut rsdp: * const RSDP = 0 as * const RSDP;
Expand Down Expand Up @@ -114,9 +114,3 @@ pub fn parse_madt() -> u64 {
}
res
}

pub fn parse_mcfg() {
unsafe {
println!("{:#?}", (*mcfg).alloc);
}
}
5 changes: 3 additions & 2 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use DoglinkOS_2nd::console::{init as init_console, clear as clear_console, puts
use DoglinkOS_2nd::int::{init as init_interrupt, register as register_interrupt_handler};
use DoglinkOS_2nd::mm::init as init_mm;
use DoglinkOS_2nd::apic::{local::init as init_lapic, io::init as init_ioapic};
use DoglinkOS_2nd::acpi::{init as init_acpi, parse_madt, parse_mcfg};
use DoglinkOS_2nd::acpi::{init as init_acpi, parse_madt};
use DoglinkOS_2nd::cpu::show_cpu_info;
use DoglinkOS_2nd::pcie::enumrate::doit;
use DoglinkOS_2nd::println;
use limine::request::{FramebufferRequest, HhdmRequest, RsdpRequest, RequestsEndMarker, RequestsStartMarker};
use limine::BaseRevision;
Expand Down Expand Up @@ -58,7 +59,7 @@ extern "C" fn kmain() -> ! {
let ioapic_phys_addr = parse_madt();
init_ioapic(ioapic_phys_addr);
show_cpu_info();
parse_mcfg();
doit();
hang();
}

Expand Down
39 changes: 39 additions & 0 deletions kernel/src/pcie/enumrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::acpi::mcfg;
use crate::mm::phys_to_virt;
use crate::println;

pub static mut pcie_mmio_base: u64 = 0;
pub static mut bus_range: core::ops::RangeInclusive<u8> = 0..=0;
#[derive(Debug)]
#[repr(packed)]
pub struct PCIConfigSpace {
vendor_id: u16,
device_id: u16,
command: u16,
status: u16,
prog_if: u16,
subclass: u8,
class_code: u8,
// TODO
}

pub fn get_config_space(bus: u8, device: u8) -> &'static PCIConfigSpace {
unsafe {
&*((pcie_mmio_base + ((bus as u64) << 20) + ((device as u64) << 15)) as * const PCIConfigSpace)
}
}

pub fn doit() {
unsafe {
pcie_mmio_base = phys_to_virt((*mcfg).alloc.base_addr);
bus_range = ((*mcfg).alloc.start_pci_bus_number)..=((*mcfg).alloc.end_pci_bus_number);
for bus in bus_range.clone() {
for device in 0..32 {
let config = get_config_space(bus, device);
if config.vendor_id != 65535 {
println!("PCI bus {} device {}: {:?}", bus, device, *config);
}
}
}
}
}

0 comments on commit a4ac06a

Please sign in to comment.