Initializer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/CommandLine.h>
  11. #include <Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h>
  12. #include <Kernel/Firmware/ACPI/Parser.h>
  13. #include <Kernel/Panic.h>
  14. #include <Kernel/Sections.h>
  15. namespace Kernel::PCI {
  16. READONLY_AFTER_INIT bool g_pci_access_io_probe_failed;
  17. READONLY_AFTER_INIT bool g_pci_access_is_disabled_from_commandline;
  18. static bool test_pci_io();
  19. UNMAP_AFTER_INIT static PCIAccessLevel detect_optimal_access_type()
  20. {
  21. #if ARCH(I386) || ARCH(X86_64)
  22. auto boot_determined = kernel_command_line().pci_access_level();
  23. if (!ACPI::is_enabled() || !ACPI::Parser::the()->find_table("MCFG"sv).has_value())
  24. return PCIAccessLevel::IOAddressing;
  25. if (boot_determined != PCIAccessLevel::IOAddressing)
  26. return boot_determined;
  27. if (!g_pci_access_io_probe_failed)
  28. return PCIAccessLevel::IOAddressing;
  29. #endif
  30. PANIC("No PCI bus access method detected!");
  31. }
  32. UNMAP_AFTER_INIT void initialize()
  33. {
  34. g_pci_access_io_probe_failed = !test_pci_io();
  35. g_pci_access_is_disabled_from_commandline = kernel_command_line().is_pci_disabled();
  36. if (g_pci_access_is_disabled_from_commandline || g_pci_access_io_probe_failed)
  37. return;
  38. switch (detect_optimal_access_type()) {
  39. case PCIAccessLevel::MemoryAddressing: {
  40. // FIXME: There are other arch-specific methods to find the memory range
  41. // for accessing the PCI configuration space.
  42. auto mcfg = ACPI::Parser::the()->find_table("MCFG"sv);
  43. VERIFY(mcfg.has_value());
  44. auto success = Access::initialize_for_multiple_pci_domains(mcfg.value());
  45. VERIFY(success);
  46. break;
  47. }
  48. #if ARCH(I386) || ARCH(X86_64)
  49. case PCIAccessLevel::IOAddressing: {
  50. auto success = Access::initialize_for_one_pci_domain();
  51. VERIFY(success);
  52. break;
  53. }
  54. #endif
  55. default:
  56. VERIFY_NOT_REACHED();
  57. }
  58. PCIBusSysFSDirectory::initialize();
  59. MUST(PCI::enumerate([&](DeviceIdentifier const& device_identifier) {
  60. dmesgln("{} {}", device_identifier.address(), device_identifier.hardware_id());
  61. }));
  62. }
  63. UNMAP_AFTER_INIT bool test_pci_io()
  64. {
  65. dmesgln("Testing PCI via manual probing...");
  66. u32 tmp = 0x80000000;
  67. IO::out32(PCI::address_port, tmp);
  68. tmp = IO::in32(PCI::address_port);
  69. if (tmp == 0x80000000) {
  70. dmesgln("PCI IO supported");
  71. return true;
  72. }
  73. dmesgln("PCI IO not supported");
  74. return false;
  75. }
  76. }