From 9c01fdd7c02d8b9b5003e659ebca0b3643bd47c4 Mon Sep 17 00:00:00 2001 From: Damien Zammit Date: Wed, 7 Apr 2021 18:29:13 +1000 Subject: x86: Sort devices by B/D/F due to recursive scan --- src/x86_pci.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src') diff --git a/src/x86_pci.c b/src/x86_pci.c index 03864e6..565dbc8 100644 --- a/src/x86_pci.c +++ b/src/x86_pci.c @@ -211,6 +211,28 @@ outl(uint32_t value, uint16_t port) #endif +static int cmp_devices(const void *dev1, const void *dev2) +{ + const struct pci_device *d1 = dev1; + const struct pci_device *d2 = dev2; + + if (d1->bus != d2->bus) { + return (d1->bus > d2->bus) ? 1 : -1; + } + + if (d1->dev != d2->dev) { + return (d1->dev > d2->dev) ? 1 : -1; + } + + return (d1->func > d2->func) ? 1 : -1; +} + +static void +sort_devices(void) +{ + qsort(pci_sys->devices, pci_sys->num_devices, + sizeof (pci_sys->devices[0]), &cmp_devices); +} #if defined(__GNU__) #include @@ -1202,10 +1224,15 @@ pci_system_x86_create(void) if (err) { x86_disable_io (); + if (pci_sys->num_devices) + { + free (pci_sys->devices); + } free (pci_sys); pci_sys = NULL; return err; } + sort_devices (); return 0; } -- cgit v1.2.3