Initializer.cpp 4.5 KB

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