Initializer.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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").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::MemoryAddressing: {
  33. auto success = Access::initialize_for_memory_access(ACPI::Parser::the()->find_table("MCFG"));
  34. VERIFY(success);
  35. break;
  36. }
  37. case PCIAccessLevel::IOAddressing: {
  38. auto success = Access::initialize_for_io_access();
  39. VERIFY(success);
  40. break;
  41. }
  42. default:
  43. VERIFY_NOT_REACHED();
  44. }
  45. PCI::PCIBusSysFSDirectory::initialize();
  46. PCI::enumerate([&](const Address& address, ID id) {
  47. dmesgln("{} {}", address, id);
  48. });
  49. }
  50. UNMAP_AFTER_INIT bool test_pci_io()
  51. {
  52. dmesgln("Testing PCI via manual probing...");
  53. u32 tmp = 0x80000000;
  54. IO::out32(PCI_ADDRESS_PORT, tmp);
  55. tmp = IO::in32(PCI_ADDRESS_PORT);
  56. if (tmp == 0x80000000) {
  57. dmesgln("PCI IO supported");
  58. return true;
  59. }
  60. dmesgln("PCI IO not supported");
  61. return false;
  62. }
  63. }
  64. }