init.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. use super::{
  2. device::{PCIDevice, SegmentGroup, PCIE_DEVICES},
  3. error::PciError,
  4. };
  5. use crate::kernel::mem::PhysAccess as _;
  6. use acpi::{AcpiHandler, AcpiTables, PciConfigRegions, PhysicalMapping};
  7. use eonix_mm::address::PAddr;
  8. #[derive(Clone)]
  9. struct AcpiHandlerImpl;
  10. impl AcpiHandler for AcpiHandlerImpl {
  11. unsafe fn map_physical_region<T>(
  12. &self,
  13. physical_address: usize,
  14. size: usize,
  15. ) -> PhysicalMapping<Self, T> {
  16. let virtual_address = unsafe {
  17. // SAFETY: `physical_address` is guaranteed to be valid by the caller.
  18. PAddr::from_val(physical_address).as_ptr()
  19. };
  20. PhysicalMapping::new(physical_address, virtual_address, size, size, self.clone())
  21. }
  22. fn unmap_physical_region<T>(_: &PhysicalMapping<Self, T>) {}
  23. }
  24. pub fn init_pcie() -> Result<(), PciError> {
  25. let acpi_tables = unsafe {
  26. // SAFETY: Our impl should be correct.
  27. AcpiTables::search_for_rsdp_bios(AcpiHandlerImpl)?
  28. };
  29. let conf_regions = PciConfigRegions::new(&acpi_tables)?;
  30. for region in conf_regions.iter() {
  31. let segment_group = SegmentGroup::from_entry(&region);
  32. for config_space in segment_group.iter() {
  33. if let Some(header) = config_space.header() {
  34. let pci_device = PCIDevice::new(segment_group.clone(), config_space, header);
  35. PCIE_DEVICES.lock().insert(pci_device);
  36. }
  37. }
  38. }
  39. Ok(())
  40. }