Initializer.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/IOAccess.h>
  8. #include <Kernel/Bus/PCI/Initializer.h>
  9. #include <Kernel/Bus/PCI/MMIOAccess.h>
  10. #include <Kernel/Bus/PCI/WindowedMMIOAccess.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").is_null())
  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::MappingPerDevice:
  33. WindowedMMIOAccess::initialize(ACPI::Parser::the()->find_table("MCFG"));
  34. break;
  35. case PCIAccessLevel::MappingPerBus:
  36. MMIOAccess::initialize(ACPI::Parser::the()->find_table("MCFG"));
  37. break;
  38. case PCIAccessLevel::IOAddressing:
  39. IOAccess::initialize();
  40. break;
  41. default:
  42. VERIFY_NOT_REACHED();
  43. }
  44. PCI::PCIBusSysFSDirectory::initialize();
  45. PCI::enumerate([&](const Address& address, ID id) {
  46. dmesgln("{} {}", address, id);
  47. });
  48. }
  49. UNMAP_AFTER_INIT bool test_pci_io()
  50. {
  51. dmesgln("Testing PCI via manual probing...");
  52. u32 tmp = 0x80000000;
  53. IO::out32(PCI_ADDRESS_PORT, tmp);
  54. tmp = IO::in32(PCI_ADDRESS_PORT);
  55. if (tmp == 0x80000000) {
  56. dmesgln("PCI IO supported");
  57. return true;
  58. }
  59. dmesgln("PCI IO not supported");
  60. return false;
  61. }
  62. }
  63. }