pci.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use crate::arch::TraitPciArch;
  2. use crate::driver::acpi::acpi::mcfg_find_segment;
  3. use crate::driver::pci::pci::{
  4. BusDeviceFunction, PciAddr, PciError, PciRoot, SegmentGroupNumber, PORT_PCI_CONFIG_ADDRESS,
  5. PORT_PCI_CONFIG_DATA,
  6. };
  7. use crate::include::bindings::bindings::{
  8. acpi_get_MCFG, acpi_iter_SDT, acpi_system_description_table_header_t, io_in32, io_out32,
  9. };
  10. use core::ffi::c_void;
  11. use core::ptr::NonNull;
  12. pub struct X86_64PciArch {}
  13. impl TraitPciArch for X86_64PciArch {
  14. fn read_config(bus_device_function: &BusDeviceFunction, offset: u8) -> u32 {
  15. // 构造pci配置空间地址
  16. let address = ((bus_device_function.bus as u32) << 16)
  17. | ((bus_device_function.device as u32) << 11)
  18. | ((bus_device_function.function as u32 & 7) << 8)
  19. | (offset & 0xfc) as u32
  20. | (0x80000000);
  21. let ret = unsafe {
  22. io_out32(PORT_PCI_CONFIG_ADDRESS, address);
  23. let temp = io_in32(PORT_PCI_CONFIG_DATA);
  24. temp
  25. };
  26. return ret;
  27. }
  28. fn write_config(bus_device_function: &BusDeviceFunction, offset: u8, data: u32) {
  29. let address = ((bus_device_function.bus as u32) << 16)
  30. | ((bus_device_function.device as u32) << 11)
  31. | ((bus_device_function.function as u32 & 7) << 8)
  32. | (offset & 0xfc) as u32
  33. | (0x80000000);
  34. unsafe {
  35. io_out32(PORT_PCI_CONFIG_ADDRESS, address);
  36. // 写入数据
  37. io_out32(PORT_PCI_CONFIG_DATA, data);
  38. }
  39. }
  40. fn address_pci_to_physical(pci_address: PciAddr) -> usize {
  41. return pci_address.data();
  42. }
  43. fn ecam_root(segement: SegmentGroupNumber) -> Result<PciRoot, PciError> {
  44. let mut data: usize = 0;
  45. let data_point = &mut data;
  46. unsafe {
  47. acpi_iter_SDT(Some(acpi_get_MCFG), data_point as *mut usize as *mut c_void);
  48. };
  49. // 防止无PCIE的机器找不到MCFG Table导致的错误
  50. if data == 0 {
  51. return Err(PciError::McfgTableNotFound);
  52. }
  53. //kdebug!("{}",data);
  54. //loop{}
  55. let head = NonNull::new(data as *mut acpi_system_description_table_header_t).unwrap();
  56. let outcome = unsafe { mcfg_find_segment(head).as_ref() };
  57. for segmentgroupconfiguration in outcome {
  58. if segmentgroupconfiguration.segement_group_number == segement {
  59. return Ok(PciRoot {
  60. physical_address_base: segmentgroupconfiguration.base_address,
  61. mmio_base: None,
  62. segement_group_number: segement,
  63. bus_begin: segmentgroupconfiguration.bus_begin,
  64. bus_end: segmentgroupconfiguration.bus_end,
  65. });
  66. }
  67. }
  68. return Err(PciError::SegmentNotFound);
  69. }
  70. }