Initializer.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/ACPI/Parser.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/IO.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(PCIAccessLevel boot_determined)
  19. {
  20. if (!ACPI::is_enabled() || !ACPI::Parser::the()->find_table("MCFG").has_value())
  21. return PCIAccessLevel::IOAddressing;
  22. if (boot_determined != PCIAccessLevel::IOAddressing)
  23. return boot_determined;
  24. if (test_pci_io())
  25. return PCIAccessLevel::IOAddressing;
  26. PANIC("No PCI bus access method detected!");
  27. }
  28. UNMAP_AFTER_INIT void initialize()
  29. {
  30. auto boot_determined = kernel_command_line().pci_access_level();
  31. switch (detect_optimal_access_type(boot_determined)) {
  32. case PCIAccessLevel::MemoryAddressing: {
  33. auto mcfg = ACPI::Parser::the()->find_table("MCFG");
  34. VERIFY(mcfg.has_value());
  35. auto success = Access::initialize_for_memory_access(mcfg.value());
  36. VERIFY(success);
  37. break;
  38. }
  39. case PCIAccessLevel::IOAddressing: {
  40. auto success = Access::initialize_for_io_access();
  41. VERIFY(success);
  42. break;
  43. }
  44. default:
  45. VERIFY_NOT_REACHED();
  46. }
  47. PCI::PCIBusSysFSDirectory::initialize();
  48. PCI::enumerate([&](const Address& address, ID id) {
  49. dmesgln("{} {}", address, 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. }