Change license from GPLv3 to LGPLv3.
[seabios.git] / src / pci.c
index f5868865765bf7fab340fd228c3af7e939229947..a59af0742f0d6336a7885a4004f07f0dd2dc9075 100644 (file)
--- a/src/pci.c
+++ b/src/pci.c
@@ -3,9 +3,9 @@
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 
-#include "pci.h" // MaxBDF
+#include "pci.h" // pci_config_writel
 #include "ioport.h" // outl
 #include "util.h" // dprintf
 #include "config.h" // CONFIG_*
@@ -48,47 +48,51 @@ u8 pci_config_readb(u16 bdf, u32 addr)
     return inb(PORT_PCI_DATA + (addr & 3));
 }
 
-#if MODE16
-int MaxBDF VISIBLE16;
-#endif
-
-// Find the maximum bus number.
-void
-pci_bus_setup()
+int
+pci_next(int bdf, int *pmax)
 {
-    dprintf(3, "Scan for max PCI bus\n");
+    if (pci_bdf_to_fn(bdf) == 1
+        && (pci_config_readb(bdf-1, PCI_HEADER_TYPE) & 0x80) == 0)
+        // Last found device wasn't a multi-function device - skip to
+        // the next device.
+        bdf += 7;
 
-    int max = 0x0100;
-    int bdf;
-    for (bdf=0; bdf < max; bdf++) {
-        u32 v = pci_config_readl(bdf, PCI_VENDOR_ID);
-        if (v == 0xffffffff || v == 0x00000000
-            || v == 0x0000ffff || v == 0xffff0000)
-            // No device present.
-            continue;
-        v = pci_config_readb(bdf, PCI_HEADER_TYPE);
-        v &= 0x7f;
-        if (v != PCI_HEADER_TYPE_BRIDGE && v != PCI_HEADER_TYPE_CARDBUS)
-            // Not a bridge
-            continue;
+    int max = *pmax;
+    for (;;) {
+        if (bdf >= max)
+            return -1;
+
+        u16 v = pci_config_readw(bdf, PCI_VENDOR_ID);
+        if (v != 0x0000 && v != 0xffff)
+            // Device is present.
+            break;
+
+        if (pci_bdf_to_fn(bdf) == 0)
+            bdf += 8;
+        else
+            bdf += 1;
+    }
+
+    // Check if found device is a bridge.
+    u32 v = pci_config_readb(bdf, PCI_HEADER_TYPE);
+    v &= 0x7f;
+    if (v == PCI_HEADER_TYPE_BRIDGE || v == PCI_HEADER_TYPE_CARDBUS) {
         v = pci_config_readl(bdf, PCI_PRIMARY_BUS);
         int newmax = (v & 0xff00) + 0x0100;
         if (newmax > max)
-            max = newmax;
+            *pmax = newmax;
     }
-    SET_VAR(CS, MaxBDF, max);
 
-    dprintf(1, "Found %d PCI buses\n", pci_bdf_to_bus(max));
+    return bdf;
 }
 
 // Search for a device with the specified vendor and device ids.
 int
-pci_find_device(u16 vendid, u16 devid, int start_bdf)
+pci_find_device(u16 vendid, u16 devid)
 {
     u32 id = (devid << 16) | vendid;
-    int max = GET_VAR(CS, MaxBDF);
-    int bdf;
-    for (bdf=start_bdf; bdf < max; bdf++) {
+    int bdf, max;
+    foreachpci(bdf, max) {
         u32 v = pci_config_readl(bdf, PCI_VENDOR_ID);
         if (v != id)
             continue;
@@ -98,29 +102,12 @@ pci_find_device(u16 vendid, u16 devid, int start_bdf)
     return -1;
 }
 
-// Search for a device with the specified class id and prog-if.
-int
-pci_find_classprog(u32 classprog, int start_bdf)
-{
-    int max = GET_VAR(CS, MaxBDF);
-    int bdf;
-    for (bdf=start_bdf; bdf < max; bdf++) {
-        u32 v = pci_config_readl(bdf, PCI_CLASS_REVISION);
-        if ((v>>8) != classprog)
-            continue;
-        // Found it.
-        return bdf;
-    }
-    return -1;
-}
-
 // Search for a device with the specified class id.
 int
-pci_find_class(u16 classid, int start_bdf)
+pci_find_class(u16 classid)
 {
-    int max = GET_VAR(CS, MaxBDF);
-    int bdf;
-    for (bdf=start_bdf; bdf < max; bdf++) {
+    int bdf, max;
+    foreachpci(bdf, max) {
         u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
         if (v != classid)
             continue;