Initializer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/KParams.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. static PCI::Initializer* s_pci_initializer;
  36. PCI::Initializer& PCI::Initializer::the()
  37. {
  38. if (s_pci_initializer == nullptr) {
  39. s_pci_initializer = new PCI::Initializer();
  40. }
  41. return *s_pci_initializer;
  42. }
  43. void PCI::Initializer::initialize_pci_mmio_access(PhysicalAddress mcfg)
  44. {
  45. PCI::MMIOAccess::initialize(mcfg);
  46. detect_devices();
  47. }
  48. void PCI::Initializer::initialize_pci_io_access()
  49. {
  50. PCI::IOAccess::initialize();
  51. detect_devices();
  52. }
  53. void PCI::Initializer::detect_devices()
  54. {
  55. PCI::enumerate_all([&](const PCI::Address& address, PCI::ID id) {
  56. 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) << "]";
  57. E1000NetworkAdapter::detect(address);
  58. RTL8139NetworkAdapter::detect(address);
  59. });
  60. }
  61. void PCI::Initializer::test_and_initialize(bool disable_pci_mmio)
  62. {
  63. if (disable_pci_mmio) {
  64. if (test_pci_io()) {
  65. initialize_pci_io_access();
  66. } else {
  67. klog() << "No PCI Bus Access Method Detected, Halt!";
  68. ASSERT_NOT_REACHED(); // NO PCI Access ?!
  69. }
  70. return;
  71. }
  72. if (test_acpi()) {
  73. if (test_pci_mmio()) {
  74. initialize_pci_mmio_access_after_test();
  75. } else {
  76. if (test_pci_io()) {
  77. initialize_pci_io_access();
  78. } else {
  79. klog() << "No PCI Bus Access Method Detected, Halt!";
  80. ASSERT_NOT_REACHED(); // NO PCI Access ?!
  81. }
  82. }
  83. } else {
  84. if (test_pci_io()) {
  85. initialize_pci_io_access();
  86. } else {
  87. klog() << "No PCI Bus Access Method Detected, Halt!";
  88. ASSERT_NOT_REACHED(); // NO PCI Access ?!
  89. }
  90. }
  91. }
  92. PCI::Initializer::Initializer()
  93. {
  94. }
  95. bool PCI::Initializer::test_acpi()
  96. {
  97. if ((KParams::the().has("noacpi")) || !ACPI::Parser::the().is_operable())
  98. return false;
  99. else
  100. return true;
  101. }
  102. bool PCI::Initializer::test_pci_io()
  103. {
  104. klog() << "Testing PCI via manual probing... ";
  105. u32 tmp = 0x80000000;
  106. IO::out32(PCI_ADDRESS_PORT, tmp);
  107. tmp = IO::in32(PCI_ADDRESS_PORT);
  108. if (tmp == 0x80000000) {
  109. klog() << "PCI IO Supported!";
  110. return true;
  111. }
  112. klog() << "PCI IO Not Supported!";
  113. return false;
  114. }
  115. bool PCI::Initializer::test_pci_mmio()
  116. {
  117. return !ACPI::Parser::the().find_table("MCFG").is_null();
  118. }
  119. void PCI::Initializer::initialize_pci_mmio_access_after_test()
  120. {
  121. initialize_pci_mmio_access(ACPI::Parser::the().find_table("MCFG"));
  122. }
  123. void PCI::Initializer::dismiss()
  124. {
  125. if (s_pci_initializer == nullptr)
  126. return;
  127. klog() << "PCI Subsystem Initializer dismissed.";
  128. delete s_pci_initializer;
  129. s_pci_initializer = nullptr;
  130. }
  131. PCI::Initializer::~Initializer()
  132. {
  133. }
  134. }