Initializer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/ACPI/ACPIParser.h>
  27. #include <Kernel/CommandLine.h>
  28. #include <Kernel/Net/E1000NetworkAdapter.h>
  29. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  30. #include <Kernel/PCI/IOAccess.h>
  31. #include <Kernel/PCI/Initializer.h>
  32. #include <Kernel/PCI/MMIOAccess.h>
  33. #include <LibBareMetal/IO.h>
  34. namespace Kernel {
  35. namespace PCI {
  36. static void initialize_pci_mmio_access(PhysicalAddress mcfg);
  37. static void initialize_pci_io_access();
  38. static void detect_devices();
  39. static bool test_acpi();
  40. static bool test_pci_io();
  41. static bool test_pci_mmio();
  42. static Access::Type detect_optimal_access_type(bool mmio_allowed)
  43. {
  44. if (mmio_allowed && test_acpi() && test_pci_mmio())
  45. return Access::Type::MMIO;
  46. if (test_pci_io())
  47. return Access::Type::IO;
  48. klog() << "No PCI bus access method detected!";
  49. hang();
  50. }
  51. void initialize()
  52. {
  53. bool mmio_allowed = kernel_command_line().lookup("pci_mmio").value_or("off") == "on";
  54. if (detect_optimal_access_type(mmio_allowed) == Access::Type::MMIO)
  55. initialize_pci_mmio_access(ACPI::Parser::the().find_table("MCFG"));
  56. else
  57. initialize_pci_io_access();
  58. }
  59. void initialize_pci_mmio_access(PhysicalAddress mcfg)
  60. {
  61. MMIOAccess::initialize(mcfg);
  62. detect_devices();
  63. }
  64. void initialize_pci_io_access()
  65. {
  66. IOAccess::initialize();
  67. detect_devices();
  68. }
  69. void detect_devices()
  70. {
  71. enumerate_all([&](const Address& address, ID id) {
  72. klog() << "PCI: Device @ " << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%d", address.function()) << " [" << String::format("%w", id.vendor_id) << ":" << String::format("%w", id.device_id) << "]";
  73. E1000NetworkAdapter::detect(address);
  74. RTL8139NetworkAdapter::detect(address);
  75. });
  76. }
  77. bool test_acpi()
  78. {
  79. if ((kernel_command_line().contains("noacpi")) || !ACPI::Parser::the().is_operable())
  80. return false;
  81. return true;
  82. }
  83. bool test_pci_io()
  84. {
  85. klog() << "Testing PCI via manual probing... ";
  86. u32 tmp = 0x80000000;
  87. IO::out32(PCI_ADDRESS_PORT, tmp);
  88. tmp = IO::in32(PCI_ADDRESS_PORT);
  89. if (tmp == 0x80000000) {
  90. klog() << "PCI IO Supported!";
  91. return true;
  92. }
  93. klog() << "PCI IO Not Supported!";
  94. return false;
  95. }
  96. bool test_pci_mmio()
  97. {
  98. return !ACPI::Parser::the().find_table("MCFG").is_null();
  99. }
  100. }
  101. }