Procházet zdrojové kódy

Kernel: Simplify PCI::initialize()

Choosing between I/O and MMIO is not as difficult as we were making it.
Andreas Kling před 5 roky
rodič
revize
66f7c8e0e8
2 změnil soubory, kde provedl 23 přidání a 41 odebrání
  1. 5 0
      Kernel/PCI/Access.h
  2. 18 41
      Kernel/PCI/Initializer.cpp

+ 5 - 0
Kernel/PCI/Access.h

@@ -33,6 +33,11 @@ namespace Kernel {
 
 class PCI::Access {
 public:
+    enum class Type {
+        IO,
+        MMIO,
+    };
+
     virtual void enumerate_all(Function<void(Address, ID)>&) = 0;
 
     void enumerate_bus(int type, u8 bus, Function<void(Address, ID)>&);

+ 18 - 41
Kernel/PCI/Initializer.cpp

@@ -38,17 +38,31 @@ namespace PCI {
 
 static void initialize_pci_mmio_access(PhysicalAddress mcfg);
 static void initialize_pci_io_access();
-static void test_and_initialize(bool disable_pci_mmio);
 static void detect_devices();
 static bool test_acpi();
 static bool test_pci_io();
 static bool test_pci_mmio();
-static void initialize_pci_mmio_access_after_test();
+
+static Access::Type detect_optimal_access_type(bool mmio_allowed)
+{
+    if (mmio_allowed && test_acpi() && test_pci_mmio())
+        return Access::Type::MMIO;
+
+    if (test_pci_io())
+        return Access::Type::IO;
+
+    klog() << "No PCI bus access method detected!";
+    hang();
+}
 
 void initialize()
 {
-    bool pci_mmio = kernel_command_line().lookup("pci_mmio").value_or("off") == "on";
-    test_and_initialize(!pci_mmio);
+    bool mmio_allowed = kernel_command_line().lookup("pci_mmio").value_or("off") == "on";
+
+    if (detect_optimal_access_type(mmio_allowed) == Access::Type::MMIO)
+        initialize_pci_mmio_access(ACPI::Parser::the().find_table("MCFG"));
+    else
+        initialize_pci_io_access();
 }
 
 void initialize_pci_mmio_access(PhysicalAddress mcfg)
@@ -72,38 +86,6 @@ void detect_devices()
     });
 }
 
-void test_and_initialize(bool disable_pci_mmio)
-{
-    if (disable_pci_mmio) {
-        if (test_pci_io()) {
-            initialize_pci_io_access();
-        } else {
-            klog() << "No PCI Bus Access Method Detected, Halt!";
-            ASSERT_NOT_REACHED(); // NO PCI Access ?!
-        }
-        return;
-    }
-    if (test_acpi()) {
-        if (test_pci_mmio()) {
-            initialize_pci_mmio_access_after_test();
-        } else {
-            if (test_pci_io()) {
-                initialize_pci_io_access();
-            } else {
-                klog() << "No PCI Bus Access Method Detected, Halt!";
-                ASSERT_NOT_REACHED(); // NO PCI Access ?!
-            }
-        }
-    } else {
-        if (test_pci_io()) {
-            initialize_pci_io_access();
-        } else {
-            klog() << "No PCI Bus Access Method Detected, Halt!";
-            ASSERT_NOT_REACHED(); // NO PCI Access ?!
-        }
-    }
-}
-
 bool test_acpi()
 {
     if ((kernel_command_line().contains("noacpi")) || !ACPI::Parser::the().is_operable())
@@ -131,10 +113,5 @@ bool test_pci_mmio()
     return !ACPI::Parser::the().find_table("MCFG").is_null();
 }
 
-void initialize_pci_mmio_access_after_test()
-{
-    initialize_pci_mmio_access(ACPI::Parser::the().find_table("MCFG"));
-}
-
 }
 }