Initializer.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Arch/x86/IO.h>
  7. #include <Kernel/Bus/PCI/API.h>
  8. #include <Kernel/Bus/PCI/Access.h>
  9. #include <Kernel/Bus/PCI/Initializer.h>
  10. #include <Kernel/Bus/PCI/SysFSPCI.h>
  11. #include <Kernel/CommandLine.h>
  12. #include <Kernel/Firmware/ACPI/Parser.h>
  13. #include <Kernel/Panic.h>
  14. #include <Kernel/Sections.h>
  15. namespace Kernel {
  16. namespace PCI {
  17. static bool test_pci_io();
  18. UNMAP_AFTER_INIT static PCIAccessLevel detect_optimal_access_type()
  19. {
  20. auto boot_determined = kernel_command_line().pci_access_level();
  21. if (!ACPI::is_enabled() || !ACPI::Parser::the()->find_table("MCFG").has_value())
  22. return PCIAccessLevel::IOAddressing;
  23. if (boot_determined != PCIAccessLevel::IOAddressing)
  24. return boot_determined;
  25. if (test_pci_io())
  26. return PCIAccessLevel::IOAddressing;
  27. PANIC("No PCI bus access method detected!");
  28. }
  29. UNMAP_AFTER_INIT void initialize()
  30. {
  31. switch (detect_optimal_access_type()) {
  32. case PCIAccessLevel::MemoryAddressing: {
  33. auto mcfg = ACPI::Parser::the()->find_table("MCFG");
  34. VERIFY(mcfg.has_value());
  35. auto success = Access::initialize_for_multiple_pci_domains(mcfg.value());
  36. VERIFY(success);
  37. break;
  38. }
  39. case PCIAccessLevel::IOAddressing: {
  40. auto success = Access::initialize_for_one_pci_domain();
  41. VERIFY(success);
  42. break;
  43. }
  44. default:
  45. VERIFY_NOT_REACHED();
  46. }
  47. PCI::PCIBusSysFSDirectory::initialize();
  48. PCI::enumerate([&](DeviceIdentifier const& device_identifier) {
  49. dmesgln("{} {}", device_identifier.address(), device_identifier.hardware_id());
  50. });
  51. }
  52. UNMAP_AFTER_INIT bool test_pci_io()
  53. {
  54. dmesgln("Testing PCI via manual probing...");
  55. u32 tmp = 0x80000000;
  56. IO::out32(PCI::address_port, tmp);
  57. tmp = IO::in32(PCI::address_port);
  58. if (tmp == 0x80000000) {
  59. dmesgln("PCI IO supported");
  60. return true;
  61. }
  62. dmesgln("PCI IO not supported");
  63. return false;
  64. }
  65. }
  66. }