More cleanups and enhancements. This series simplifies pci_next() so that it no longer needs to check for pci bridges.
The vgahook changes in the first patch were compile tested only.
-Kevin
Kevin O'Connor (4): Calculate vgahook responses during setup instead of in 16bit code. Convert pci_find_device/class to use 'struct pci_device'. Convert remaining callers of foreachbdf to foreachbdf_in_bus. Rename foreachbdf_in_bus to foreachbdf and simplify it.
src/acpi.c | 2 +- src/coreboot.c | 9 +- src/floppy.c | 4 +- src/optionroms.c | 3 +- src/pci.c | 173 ++++++++++++++++------------------ src/pci.h | 21 ++--- src/pcibios.c | 55 ++++++----- src/pciinit.c | 6 +- src/shadow.c | 4 +- src/smm.c | 10 +- src/usb-ehci.c | 3 +- src/util.h | 5 +- src/vgahooks.c | 274 ++++++++++++++++++++++-------------------------------- 13 files changed, 255 insertions(+), 314 deletions(-)
Do vga type and parameter detection during setup and store the necessary info in global variables for the 16bit code. This simplifies the "vgahook" 16bit code. --- src/coreboot.c | 9 +- src/optionroms.c | 3 +- src/util.h | 5 +- src/vgahooks.c | 250 ++++++++++++++++++++++-------------------------------- 4 files changed, 109 insertions(+), 158 deletions(-)
diff --git a/src/coreboot.c b/src/coreboot.c index 6e22919..5926939 100644 --- a/src/coreboot.c +++ b/src/coreboot.c @@ -117,6 +117,7 @@ find_cb_subtable(struct cb_header *cbh, u32 tag) }
static struct cb_memory *CBMemTable; +const char *CBvendor, *CBpart;
// Populate max ram and e820 map info by scanning for a coreboot table. static void @@ -168,11 +169,9 @@ coreboot_fill_map(void)
struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD); if (cbmb) { - const char *vendor = &cbmb->strings[cbmb->vendor_idx]; - const char *part = &cbmb->strings[cbmb->part_idx]; - dprintf(1, "Found mainboard %s %s\n", vendor, part); - - vgahook_setup(vendor, part); + CBvendor = &cbmb->strings[cbmb->vendor_idx]; + CBpart = &cbmb->strings[cbmb->part_idx]; + dprintf(1, "Found mainboard %s %s\n", CBvendor, CBpart); }
return; diff --git a/src/optionroms.c b/src/optionroms.c index 27c172f..b5a4297 100644 --- a/src/optionroms.c +++ b/src/optionroms.c @@ -232,6 +232,7 @@ getRomPriority(u64 *sources, struct rom_header *rom, int instance) return bootprio_find_named_rom(romfile_name(source), instance); }
+ /**************************************************************** * Roms in CBFS ****************************************************************/ @@ -479,7 +480,7 @@ vga_setup(void) foreachpci(pci) { if (!is_pci_vga(pci)) continue; - VGAbdf = pci->bdf; + vgahook_setup(pci); init_pcirom(pci, 1, NULL); break; } diff --git a/src/util.h b/src/util.h index 7034d43..eecedac 100644 --- a/src/util.h +++ b/src/util.h @@ -387,6 +387,7 @@ void wrmsr_smp(u32 index, u64 val); void smp_probe(void);
// coreboot.c +extern const char *CBvendor, *CBpart; struct cbfs_file; struct cbfs_file *cbfs_finddatafile(const char *fname); struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last); @@ -405,9 +406,9 @@ void copy_acpi_rsdp(void *pos); void copy_smbios(void *pos);
// vgahooks.c -extern int VGAbdf; void handle_155f(struct bregs *regs); -void vgahook_setup(const char *vendor, const char *part); +struct pci_device; +void vgahook_setup(struct pci_device *pci);
// optionroms.c void call_bcv(u16 seg, u16 ip); diff --git a/src/vgahooks.c b/src/vgahooks.c index eb4dfa8..16f6b8a 100644 --- a/src/vgahooks.c +++ b/src/vgahooks.c @@ -12,27 +12,10 @@ #include "util.h" // handle_155f #include "config.h" // CONFIG_*
-// The Bus/Dev/Fn of the primary VGA device. -int VGAbdf VAR16VISIBLE = -1; -// Coreboot board detected. -int CBmainboard VAR16VISIBLE; - -#define MAINBOARD_DEFAULT 0 -#define KONTRON_986LCD_M 1 -#define GETAC_P470 2 -#define RODA_RK886EX 3 - -struct mainboards { - char *vendor; - char *device; - int type; -}; - -struct mainboards mainboard_list[] = { - { "KONTRON", "986LCD-M", KONTRON_986LCD_M }, - { "GETAC", "P470", GETAC_P470 }, - { "RODA", "RK886EX", RODA_RK886EX }, -}; +#define VH_VIA 1 +#define VH_INTEL 2 + +int VGAHookHandlerType VAR16VISIBLE;
static void handle_155fXX(struct bregs *regs) @@ -45,6 +28,8 @@ handle_155fXX(struct bregs *regs) * Via hooks ****************************************************************/
+int ViaFBsize VAR16VISIBLE, ViaRamSpeed VAR16VISIBLE; + static void via_155f01(struct bregs *regs) { @@ -65,6 +50,38 @@ via_155f02(struct bregs *regs) dprintf(1, "Warning: VGA TV/CRT output type is hardcoded\n"); }
+static void +via_155f18(struct bregs *regs) +{ + int fbsize = GET_GLOBAL(ViaFBsize), ramspeed = GET_GLOBAL(ViaRamSpeed); + if (fbsize < 0 || ramspeed < 0) { + set_code_invalid(regs, RET_EUNSUPPORTED); + return; + } + regs->eax = 0x5f; + regs->ebx = 0x500 | (ramspeed << 4) | fbsize; + regs->ecx = 0x060; + set_success(regs); +} + +static void +via_155f19(struct bregs *regs) +{ + set_invalid_silent(regs); +} + +static void +via_155f(struct bregs *regs) +{ + switch (regs->al) { + case 0x01: via_155f01(regs); break; + case 0x02: via_155f02(regs); break; + case 0x18: via_155f18(regs); break; + case 0x19: via_155f19(regs); break; + default: handle_155fXX(regs); break; + } +} + static int getFBSize(u16 bdf) { @@ -75,8 +92,8 @@ getFBSize(u16 bdf) if (!(reg & 0x80)) return -1;
- static u8 mem_power[] VAR16 = {0, 3, 4, 5, 6, 7, 8, 9}; - return GET_GLOBAL(mem_power[(reg >> 4) & 0x7]); + static u8 mem_power[] = {0, 3, 4, 5, 6, 7, 8, 9}; + return mem_power[(reg >> 4) & 0x7]; }
static int @@ -121,140 +138,95 @@ getAMDRamSpeed(void) #define PCI_DEVICE_ID_VIA_VX855_MEMCTRL 0x3409
static void -via_155f18(struct bregs *regs) +via_setup(struct pci_device *pci) { - int ramspeed, fbsize; + VGAHookHandlerType = VH_VIA;
int bdf = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_K8M890CE_3); if (bdf >= 0) { - fbsize = getFBSize(bdf); - ramspeed = getAMDRamSpeed(); - goto done; + ViaFBsize = getFBSize(bdf); + ViaRamSpeed = getAMDRamSpeed(); + return; } bdf = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_MEMCTRL); if (bdf >= 0) { - fbsize = getFBSize(bdf); - ramspeed = getViaRamSpeed(bdf); - goto done; - } - - dprintf(1, "Warning: VGA memory size and speed is hardcoded\n"); - fbsize = 5; // 32M frame buffer - ramspeed = 4; // MCLK = DDR266 - -done: - if (fbsize < 0 || ramspeed < 0) { - set_code_invalid(regs, RET_EUNSUPPORTED); + ViaFBsize = getFBSize(bdf); + ViaRamSpeed = getViaRamSpeed(bdf); return; } - regs->eax = 0x5f; - regs->ebx = 0x500 | (ramspeed << 4) | fbsize; - regs->ecx = 0x060; - set_success(regs); -}
-static void -via_155f19(struct bregs *regs) -{ - set_invalid_silent(regs); + dprintf(1, "Warning: VGA memory size and speed is hardcoded\n"); + ViaFBsize = 5; // 32M frame buffer + ViaRamSpeed = 4; // MCLK = DDR266 }
-static void -via_155f(struct bregs *regs) -{ - switch (regs->al) { - case 0x01: via_155f01(regs); break; - case 0x02: via_155f02(regs); break; - case 0x18: via_155f18(regs); break; - case 0x19: via_155f19(regs); break; - default: handle_155fXX(regs); break; - } -}
/**************************************************************** * Intel VGA hooks ****************************************************************/ -#define BOOT_DISPLAY_DEFAULT (0) -#define BOOT_DISPLAY_CRT (1 << 0) -#define BOOT_DISPLAY_TV (1 << 1) -#define BOOT_DISPLAY_EFP (1 << 2) -#define BOOT_DISPLAY_LCD (1 << 3) -#define BOOT_DISPLAY_CRT2 (1 << 4) -#define BOOT_DISPLAY_TV2 (1 << 5) -#define BOOT_DISPLAY_EFP2 (1 << 6) -#define BOOT_DISPLAY_LCD2 (1 << 7) - + +u8 IntelDisplayType VAR16VISIBLE, IntelDisplayId VAR16VISIBLE; + static void -roda_155f35(struct bregs *regs) +intel_155f35(struct bregs *regs) { regs->ax = 0x005f; - // regs->cl = BOOT_DISPLAY_DEFAULT; - regs->cl = BOOT_DISPLAY_LCD; + regs->cl = GET_GLOBAL(IntelDisplayType); set_success(regs); }
static void -roda_155f40(struct bregs *regs) +intel_155f40(struct bregs *regs) { - u8 display_id; - //display_id = inb(0x60f) & 0x0f; // Correct according to Crete - display_id = 3; // Correct according to empirical studies - regs->ax = 0x005f; - regs->cl = display_id; + regs->cl = GET_GLOBAL(IntelDisplayId); set_success(regs); }
static void -roda_155f(struct bregs *regs) +intel_155f(struct bregs *regs) { - dprintf(1, "Executing RODA specific interrupt %02x.\n", regs->al); switch (regs->al) { - case 0x35: roda_155f35(regs); break; - case 0x40: roda_155f40(regs); break; + case 0x35: intel_155f35(regs); break; + case 0x40: intel_155f40(regs); break; default: handle_155fXX(regs); break; } }
-static void -kontron_155f35(struct bregs *regs) -{ - regs->ax = 0x005f; - regs->cl = BOOT_DISPLAY_CRT; - set_success(regs); -} +#define BOOT_DISPLAY_DEFAULT (0) +#define BOOT_DISPLAY_CRT (1 << 0) +#define BOOT_DISPLAY_TV (1 << 1) +#define BOOT_DISPLAY_EFP (1 << 2) +#define BOOT_DISPLAY_LCD (1 << 3) +#define BOOT_DISPLAY_CRT2 (1 << 4) +#define BOOT_DISPLAY_TV2 (1 << 5) +#define BOOT_DISPLAY_EFP2 (1 << 6) +#define BOOT_DISPLAY_LCD2 (1 << 7)
static void -kontron_155f40(struct bregs *regs) +roda_setup(struct pci_device *pci) { - u8 display_id; - display_id = 3; - - regs->ax = 0x005f; - regs->cl = display_id; - set_success(regs); + VGAHookHandlerType = VH_INTEL; + // IntelDisplayType = BOOT_DISPLAY_DEFAULT; + IntelDisplayType = BOOT_DISPLAY_LCD; + // IntelDisplayId = inb(0x60f) & 0x0f; // Correct according to Crete + IntelDisplayId = 3; // Correct according to empirical studies }
static void -kontron_155f(struct bregs *regs) +kontron_setup(struct pci_device *pci) { - dprintf(1, "Executing Kontron specific interrupt %02x.\n", regs->al); - switch (regs->al) { - case 0x35: kontron_155f35(regs); break; - case 0x40: kontron_155f40(regs); break; - default: handle_155fXX(regs); break; - } + VGAHookHandlerType = VH_INTEL; + IntelDisplayType = BOOT_DISPLAY_CRT; + IntelDisplayId = 3; }
static void -getac_155f(struct bregs *regs) +getac_setup(struct pci_device *pci) { - dprintf(1, "Executing Getac specific interrupt %02x.\n", regs->al); - switch (regs->al) { - default: handle_155fXX(regs); break; - } }
+ /**************************************************************** * Entry and setup ****************************************************************/ @@ -263,54 +235,32 @@ getac_155f(struct bregs *regs) void handle_155f(struct bregs *regs) { - int bdf, cbmb; - - if (! CONFIG_VGAHOOKS) - goto fail; - - cbmb = GET_GLOBAL(CBmainboard); - - switch (cbmb) { - case KONTRON_986LCD_M: - kontron_155f(regs); - return; - case RODA_RK886EX: - roda_155f(regs); - return; - case GETAC_P470: - getac_155f(regs); - return; - case MAINBOARD_DEFAULT: - bdf = GET_GLOBAL(VGAbdf); - if (bdf < 0) - goto fail; - - u16 vendor = pci_config_readw(bdf, PCI_VENDOR_ID); - if (vendor == PCI_VENDOR_ID_VIA) { - via_155f(regs); - return; - } + if (!CONFIG_VGAHOOKS) { + handle_155fXX(regs); + return; }
-fail: - handle_155fXX(regs); + int htype = GET_GLOBAL(VGAHookHandlerType); + switch (htype) { + case VH_VIA: via_155f(regs); break; + case VH_INTEL: intel_155f(regs); break; + default: handle_155fXX(regs); break; + } }
// Setup void -vgahook_setup(const char *vendor, const char *part) +vgahook_setup(struct pci_device *pci) { - int i; - - if (! CONFIG_VGAHOOKS) + if (!CONFIG_VGAHOOKS || !CBvendor || !CBpart) return;
- for (i=0; i<(sizeof(mainboard_list) / sizeof(mainboard_list[0])); i++) { - if (!strcmp(vendor, mainboard_list[i].vendor) && - !strcmp(part, mainboard_list[i].device)) { - printf("Found mainboard %s %s\n", vendor, part); - CBmainboard = mainboard_list[i].type; - break; - } - } + if (strcmp(CBvendor, "KONTRON") == 0 && strcmp(CBpart, "986LCD-M") == 0) + kontron_setup(pci); + else if (strcmp(CBvendor, "GETAC") == 0 && strcmp(CBpart, "P470") == 0) + getac_setup(pci); + else if (strcmp(CBvendor, "RODA") == 0 && strcmp(CBpart, "RK886EX") == 0) + roda_setup(pci); + else if (pci->vendor == PCI_VENDOR_ID_VIA) + via_setup(pci); }
--- src/acpi.c | 2 +- src/floppy.c | 4 ++-- src/pci.c | 27 ++++++++++++--------------- src/pci.h | 4 ++-- src/smm.c | 10 +++++----- src/vgahooks.c | 30 ++++++++++++++++-------------- 6 files changed, 38 insertions(+), 39 deletions(-)
diff --git a/src/acpi.c b/src/acpi.c index fc7867a..ea7b171 100644 --- a/src/acpi.c +++ b/src/acpi.c @@ -7,7 +7,7 @@
#include "acpi.h" // struct rsdp_descriptor #include "util.h" // memcpy -#include "pci.h" // pci_find_device +#include "pci.h" // pci_find_init_device #include "biosvar.h" // GET_EBDA #include "pci_ids.h" // PCI_VENDOR_ID_INTEL #include "pci_regs.h" // PCI_INTERRUPT_LINE diff --git a/src/floppy.c b/src/floppy.c index edc675d..8009af0 100644 --- a/src/floppy.c +++ b/src/floppy.c @@ -123,8 +123,8 @@ addFloppy(int floppyid, int ftype) if (!drive_g) return; char *desc = znprintf(MAXDESCSIZE, "Floppy [drive %c]", 'A' + floppyid); - int bdf = pci_find_class(PCI_CLASS_BRIDGE_ISA); /* isa-to-pci bridge */ - int prio = bootprio_find_fdc_device(bdf, PORT_FD_BASE, floppyid); + struct pci_device *pci = pci_find_class(PCI_CLASS_BRIDGE_ISA); /* isa-to-pci bridge */ + int prio = bootprio_find_fdc_device(pci->bdf, PORT_FD_BASE, floppyid); boot_add_floppy(drive_g, desc, prio); }
diff --git a/src/pci.c b/src/pci.c index 78bbac2..bbb58cf 100644 --- a/src/pci.c +++ b/src/pci.c @@ -164,30 +164,27 @@ pci_probe(void) }
// Search for a device with the specified vendor and device ids. -int +struct pci_device * pci_find_device(u16 vendid, u16 devid) { - u32 id = (devid << 16) | vendid; - int bdf, max; - foreachbdf(bdf, max) { - u32 v = pci_config_readl(bdf, PCI_VENDOR_ID); - if (v == id) - return bdf; + struct pci_device *pci; + foreachpci(pci) { + if (pci->vendor == vendid && pci->device == devid) + return pci; } - return -1; + return NULL; }
// Search for a device with the specified class id. -int +struct pci_device * pci_find_class(u16 classid) { - int bdf, max; - foreachbdf(bdf, max) { - u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE); - if (v == classid) - return bdf; + struct pci_device *pci; + foreachpci(pci) { + if (pci->class == classid) + return pci; } - return -1; + return NULL; }
int pci_init_device(const struct pci_device_id *ids diff --git a/src/pci.h b/src/pci.h index a21a1fd..cde72dc 100644 --- a/src/pci.h +++ b/src/pci.h @@ -33,8 +33,8 @@ u16 pci_config_readw(u16 bdf, u32 addr); u8 pci_config_readb(u16 bdf, u32 addr); void pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on);
-int pci_find_device(u16 vendid, u16 devid); -int pci_find_class(u16 classid); +struct pci_device *pci_find_device(u16 vendid, u16 devid); +struct pci_device *pci_find_class(u16 classid);
struct pci_device { u16 bdf; diff --git a/src/smm.c b/src/smm.c index 9f14cec..72e5e88 100644 --- a/src/smm.c +++ b/src/smm.c @@ -112,9 +112,9 @@ smm_relocate_and_restore(void) // This code is hardcoded for PIIX4 Power Management device. static void piix4_apmc_smm_init(struct pci_device *pci, void *arg) { - int i440_bdf = pci_find_device(PCI_VENDOR_ID_INTEL - , PCI_DEVICE_ID_INTEL_82441); - if (i440_bdf < 0) + struct pci_device *i440_pci = pci_find_device(PCI_VENDOR_ID_INTEL + , PCI_DEVICE_ID_INTEL_82441); + if (!i440_pci) return;
/* check if SMM init is already done */ @@ -123,7 +123,7 @@ static void piix4_apmc_smm_init(struct pci_device *pci, void *arg) return;
/* enable the SMM memory window */ - pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x48); + pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x48);
smm_save_and_copy();
@@ -133,7 +133,7 @@ static void piix4_apmc_smm_init(struct pci_device *pci, void *arg) smm_relocate_and_restore();
/* close the SMM memory window and enable normal SMM */ - pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x08); + pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x08); }
static const struct pci_device_id smm_init_tbl[] = { diff --git a/src/vgahooks.c b/src/vgahooks.c index 16f6b8a..a8f667c 100644 --- a/src/vgahooks.c +++ b/src/vgahooks.c @@ -83,10 +83,10 @@ via_155f(struct bregs *regs) }
static int -getFBSize(u16 bdf) +getFBSize(struct pci_device *pci) { /* FB config */ - u8 reg = pci_config_readb(bdf, 0xa1); + u8 reg = pci_config_readb(pci->bdf, 0xa1);
/* GFX disabled ? */ if (!(reg & 0x80)) @@ -97,20 +97,21 @@ getFBSize(u16 bdf) }
static int -getViaRamSpeed(u16 bdf) +getViaRamSpeed(struct pci_device *pci) { - return (pci_config_readb(bdf, 0x90) & 0x07) + 3; + return (pci_config_readb(pci->bdf, 0x90) & 0x07) + 3; }
static int getAMDRamSpeed(void) { - int bdf = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MEMCTL); - if (bdf < 0) + struct pci_device *pci = pci_find_device(PCI_VENDOR_ID_AMD + , PCI_DEVICE_ID_AMD_K8_NB_MEMCTL); + if (!pci) return -1;
/* mem clk 0 = DDR2 400 */ - return (pci_config_readb(bdf, 0x94) & 0x7) + 6; + return (pci_config_readb(pci->bdf, 0x94) & 0x7) + 6; }
/* int 0x15 - 5f18 @@ -142,16 +143,17 @@ via_setup(struct pci_device *pci) { VGAHookHandlerType = VH_VIA;
- int bdf = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_K8M890CE_3); - if (bdf >= 0) { - ViaFBsize = getFBSize(bdf); + struct pci_device *d = pci_find_device(PCI_VENDOR_ID_VIA + , PCI_DEVICE_ID_VIA_K8M890CE_3); + if (d) { + ViaFBsize = getFBSize(d); ViaRamSpeed = getAMDRamSpeed(); return; } - bdf = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_MEMCTRL); - if (bdf >= 0) { - ViaFBsize = getFBSize(bdf); - ViaRamSpeed = getViaRamSpeed(bdf); + d = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_MEMCTRL); + if (d) { + ViaFBsize = getFBSize(d); + ViaRamSpeed = getViaRamSpeed(d); return; }
Convert the last few callers of foreachbdf to foreachbdf_in_bus. This is in preparation for simplification of foreachbdf_in_bus.
Also add in addition debugging messages to pci_probe. --- src/pci.c | 110 ++++++++++++++++++++++++++++++++------------------------- src/pcibios.c | 55 +++++++++++++++------------- 2 files changed, 91 insertions(+), 74 deletions(-)
diff --git a/src/pci.c b/src/pci.c index bbb58cf..ebc6f91 100644 --- a/src/pci.c +++ b/src/pci.c @@ -104,63 +104,77 @@ pci_next(int bdf, int *pmax) }
struct pci_device *PCIDevices; -int MaxPCIBus; +int MaxPCIBus VAR16VISIBLE;
+// Find all PCI devices and populate PCIDevices linked list. void pci_probe(void) { - int rootbuses = 0; + dprintf(3, "PCI probe\n"); + if (CONFIG_PCI_ROOT1 && CONFIG_PCI_ROOT1 > MaxPCIBus) + MaxPCIBus = CONFIG_PCI_ROOT1; + if (CONFIG_PCI_ROOT2 && CONFIG_PCI_ROOT2 > MaxPCIBus) + MaxPCIBus = CONFIG_PCI_ROOT2; + struct pci_device *busdevs[256]; memset(busdevs, 0, sizeof(busdevs)); - struct pci_device **pprev = &PCIDevices; - u8 lastbus = 0; - int bdf, max; - foreachbdf(bdf, max) { - // Create new pci_device struct and add to list. - struct pci_device *dev = malloc_tmp(sizeof(*dev)); - if (!dev) { - warn_noalloc(); - return; - } - memset(dev, 0, sizeof(*dev)); - *pprev = dev; - pprev = &dev->next; + int bus = -1, lastbus = 0, rootbuses = 0, count=0; + while (bus < MaxPCIBus) { + bus++; + int bdf, max; + foreachbdf_in_bus(bdf, max, bus) { + // Create new pci_device struct and add to list. + struct pci_device *dev = malloc_tmp(sizeof(*dev)); + if (!dev) { + warn_noalloc(); + return; + } + memset(dev, 0, sizeof(*dev)); + *pprev = dev; + pprev = &dev->next; + count++;
- // Find parent device. - u8 bus = pci_bdf_to_bus(bdf), rootbus; - struct pci_device *parent = busdevs[bus]; - if (!parent) { - if (bus != lastbus) - rootbuses++; - lastbus = bus; - rootbus = rootbuses; - } else { - rootbus = parent->rootbus; - } - if (bus > MaxPCIBus) - MaxPCIBus = bus; + // Find parent device. + int rootbus; + struct pci_device *parent = busdevs[bus]; + if (!parent) { + if (bus != lastbus) + rootbuses++; + lastbus = bus; + rootbus = rootbuses; + } else { + rootbus = parent->rootbus; + }
- // Populate pci_device info. - dev->bdf = bdf; - dev->parent = parent; - dev->rootbus = rootbus; - u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID); - dev->vendor = vendev & 0xffff; - dev->device = vendev >> 16; - u32 classrev = pci_config_readl(bdf, PCI_CLASS_REVISION); - dev->class = classrev >> 16; - dev->prog_if = classrev >> 8; - dev->revision = classrev & 0xff; - dev->header_type = pci_config_readb(bdf, PCI_HEADER_TYPE); - u8 v = dev->header_type & 0x7f; - if (v == PCI_HEADER_TYPE_BRIDGE || v == PCI_HEADER_TYPE_CARDBUS) { - u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS); - dev->secondary_bus = secbus; - if (secbus > bus && !busdevs[secbus]) - busdevs[secbus] = dev; + // Populate pci_device info. + dev->bdf = bdf; + dev->parent = parent; + dev->rootbus = rootbus; + u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID); + dev->vendor = vendev & 0xffff; + dev->device = vendev >> 16; + u32 classrev = pci_config_readl(bdf, PCI_CLASS_REVISION); + dev->class = classrev >> 16; + dev->prog_if = classrev >> 8; + dev->revision = classrev & 0xff; + dev->header_type = pci_config_readb(bdf, PCI_HEADER_TYPE); + u8 v = dev->header_type & 0x7f; + if (v == PCI_HEADER_TYPE_BRIDGE || v == PCI_HEADER_TYPE_CARDBUS) { + u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS); + dev->secondary_bus = secbus; + if (secbus > bus && !busdevs[secbus]) + busdevs[secbus] = dev; + if (secbus > MaxPCIBus) + MaxPCIBus = secbus; + } + dprintf(4, "PCI device %02x:%02x.%x (vd=%04x:%04x c=%04x)\n" + , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf) + , pci_bdf_to_fn(bdf) + , dev->vendor, dev->device, dev->class); } } + dprintf(1, "Found %d PCI devices (max PCI bus is %02x)\n", count, MaxPCIBus); }
// Search for a device with the specified vendor and device ids. diff --git a/src/pcibios.c b/src/pcibios.c index 4fdfd5e..ca91c15 100644 --- a/src/pcibios.c +++ b/src/pcibios.c @@ -25,14 +25,9 @@ extern void pcibios32_entry(void); static void handle_1ab101(struct bregs *regs) { - // Find max bus. - int bdf, max; - foreachbdf(bdf, max) { - } - regs->al = 0x01; // Flags - "Config Mechanism #1" supported. regs->bx = 0x0210; // PCI version 2.10 - regs->cl = pci_bdf_to_bus(max - 1); + regs->cl = GET_GLOBAL(MaxPCIBus); regs->edx = 0x20494350; // "PCI " regs->edi = (u32)pcibios32_entry + BUILD_BIOS_ADDR; set_code_success(regs); @@ -44,16 +39,20 @@ handle_1ab102(struct bregs *regs) { u32 id = (regs->cx << 16) | regs->dx; int count = regs->si; - int bdf, max; - foreachbdf(bdf, max) { - u32 v = pci_config_readl(bdf, PCI_VENDOR_ID); - if (v != id) - continue; - if (count--) - continue; - regs->bx = bdf; - set_code_success(regs); - return; + int bus = -1; + while (bus < GET_GLOBAL(MaxPCIBus)) { + bus++; + int bdf, max; + foreachbdf_in_bus(bdf, max, bus) { + u32 v = pci_config_readl(bdf, PCI_VENDOR_ID); + if (v != id) + continue; + if (count--) + continue; + regs->bx = bdf; + set_code_success(regs); + return; + } } set_code_invalid(regs, RET_DEVICE_NOT_FOUND); } @@ -64,16 +63,20 @@ handle_1ab103(struct bregs *regs) { int count = regs->si; u32 classprog = regs->ecx; - int bdf, max; - foreachbdf(bdf, max) { - u32 v = pci_config_readl(bdf, PCI_CLASS_REVISION); - if ((v>>8) != classprog) - continue; - if (count--) - continue; - regs->bx = bdf; - set_code_success(regs); - return; + int bus = -1; + while (bus < GET_GLOBAL(MaxPCIBus)) { + bus++; + int bdf, max; + foreachbdf_in_bus(bdf, max, bus) { + u32 v = pci_config_readl(bdf, PCI_CLASS_REVISION); + if ((v>>8) != classprog) + continue; + if (count--) + continue; + regs->bx = bdf; + set_code_success(regs); + return; + } } set_code_invalid(regs, RET_DEVICE_NOT_FOUND); }
Now that all callers of foreachbdf have been converted to foreachbdf_in_bus, simplify the pci_next() code - it no longer needs to track PCI bridges.
Also, rename the remaining users of foreachbdf_in_bus to foreachbdf. --- src/pci.c | 40 +++++++++++----------------------------- src/pci.h | 17 +++++------------ src/pcibios.c | 8 ++++---- src/pciinit.c | 6 +++--- src/shadow.c | 4 ++-- src/usb-ehci.c | 3 +-- 6 files changed, 26 insertions(+), 52 deletions(-)
diff --git a/src/pci.c b/src/pci.c index ebc6f91..0de8ec5 100644 --- a/src/pci.c +++ b/src/pci.c @@ -59,48 +59,30 @@ pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on)
// Helper function for foreachbdf() macro - return next device int -pci_next(int bdf, int *pmax) +pci_next(int bdf, int bus) { - if (pci_bdf_to_fn(bdf) == 1 - && (pci_config_readb(bdf-1, PCI_HEADER_TYPE) & 0x80) == 0) + if (pci_bdf_to_fn(bdf) == 0 + && (pci_config_readb(bdf, PCI_HEADER_TYPE) & 0x80) == 0) // Last found device wasn't a multi-function device - skip to // the next device. - bdf += 7; + bdf += 8; + else + bdf += 1;
- int max = *pmax; for (;;) { - if (bdf >= max) { - if (CONFIG_PCI_ROOT1 && bdf <= (CONFIG_PCI_ROOT1 << 8)) - bdf = CONFIG_PCI_ROOT1 << 8; - else if (CONFIG_PCI_ROOT2 && bdf <= (CONFIG_PCI_ROOT2 << 8)) - bdf = CONFIG_PCI_ROOT2 << 8; - else - return -1; - *pmax = max = bdf + 0x0100; - } + if (pci_bdf_to_bus(bdf) != bus) + return -1;
u16 v = pci_config_readw(bdf, PCI_VENDOR_ID); if (v != 0x0000 && v != 0xffff) // Device is present. - break; + return bdf;
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) - *pmax = newmax; - } - - return bdf; }
struct pci_device *PCIDevices; @@ -122,8 +104,8 @@ pci_probe(void) int bus = -1, lastbus = 0, rootbuses = 0, count=0; while (bus < MaxPCIBus) { bus++; - int bdf, max; - foreachbdf_in_bus(bdf, max, bus) { + int bdf; + foreachbdf(bdf, bus) { // Create new pci_device struct and add to list. struct pci_device *dev = malloc_tmp(sizeof(*dev)); if (!dev) { diff --git a/src/pci.h b/src/pci.h index cde72dc..c34e348 100644 --- a/src/pci.h +++ b/src/pci.h @@ -62,18 +62,11 @@ static inline u32 pci_classprog(struct pci_device *pci) { #define foreachpci(PCI) \ for (PCI=PCIDevices; PCI; PCI=PCI->next)
-int pci_next(int bdf, int *pmax); -#define foreachbdf(BDF, MAX) \ - for (MAX=0x0100, BDF=pci_next(0, &MAX) \ - ; BDF >= 0 \ - ; BDF=pci_next(BDF+1, &MAX)) - -#define foreachbdf_in_bus(BDF, MAX, BUS) \ - for (MAX = pci_bus_devfn_to_bdf(BUS, 0) + 0x0100, \ - BDF = pci_next(pci_bus_devfn_to_bdf(BUS, 0), &MAX) \ - ; BDF >= 0 && BDF < pci_bus_devfn_to_bdf(BUS, 0) + 0x0100 \ - ; MAX = pci_bus_devfn_to_bdf(BUS, 0) + 0x0100, \ - BDF = pci_next(BDF + 1, &MAX)) +int pci_next(int bdf, int bus); +#define foreachbdf(BDF, BUS) \ + for (BDF=pci_next(pci_bus_devfn_to_bdf((BUS), 0)-1, (BUS)) \ + ; BDF >= 0 \ + ; BDF=pci_next(BDF, (BUS)))
#define PCI_ANY_ID (~0) struct pci_device_id { diff --git a/src/pcibios.c b/src/pcibios.c index ca91c15..31ca37e 100644 --- a/src/pcibios.c +++ b/src/pcibios.c @@ -42,8 +42,8 @@ handle_1ab102(struct bregs *regs) int bus = -1; while (bus < GET_GLOBAL(MaxPCIBus)) { bus++; - int bdf, max; - foreachbdf_in_bus(bdf, max, bus) { + int bdf; + foreachbdf(bdf, bus) { u32 v = pci_config_readl(bdf, PCI_VENDOR_ID); if (v != id) continue; @@ -66,8 +66,8 @@ handle_1ab103(struct bregs *regs) int bus = -1; while (bus < GET_GLOBAL(MaxPCIBus)) { bus++; - int bdf, max; - foreachbdf_in_bus(bdf, max, bus) { + int bdf; + foreachbdf(bdf, bus) { u32 v = pci_config_readl(bdf, PCI_CLASS_REVISION); if ((v>>8) != classprog) continue; diff --git a/src/pciinit.c b/src/pciinit.c index bfff3db..57747c0 100644 --- a/src/pciinit.c +++ b/src/pciinit.c @@ -377,13 +377,13 @@ static void pci_bios_init_device_in_bus(int bus) static void pci_bios_init_bus_rec(int bus, u8 *pci_bus) { - int bdf, max; + int bdf; u16 class;
dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
/* prevent accidental access to unintended devices */ - foreachbdf_in_bus(bdf, max, bus) { + foreachbdf(bdf, bus) { class = pci_config_readw(bdf, PCI_CLASS_DEVICE); if (class == PCI_CLASS_BRIDGE_PCI) { pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255); @@ -391,7 +391,7 @@ pci_bios_init_bus_rec(int bus, u8 *pci_bus) } }
- foreachbdf_in_bus(bdf, max, bus) { + foreachbdf(bdf, bus) { class = pci_config_readw(bdf, PCI_CLASS_DEVICE); if (class != PCI_CLASS_BRIDGE_PCI) { continue; diff --git a/src/shadow.c b/src/shadow.c index ece7d97..c0c8cc2 100644 --- a/src/shadow.c +++ b/src/shadow.c @@ -117,8 +117,8 @@ make_bios_writable(void)
// At this point, statically allocated variables can't be written, // so do this search manually. - int bdf, max; - foreachbdf_in_bus(bdf, max, 0) { + int bdf; + foreachbdf(bdf, 0) { u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID); u16 vendor = vendev & 0xffff, device = vendev >> 16; if (vendor == PCI_VENDOR_ID_INTEL diff --git a/src/usb-ehci.c b/src/usb-ehci.c index f11924a..5a0eb3e 100644 --- a/src/usb-ehci.c +++ b/src/usb-ehci.c @@ -280,7 +280,6 @@ ehci_init(u16 bdf, int busid, int compbdf)
// Find companion controllers. int count = 0; - int max = pci_to_bdf(pci_bdf_to_bus(bdf) + 1, 0, 0); for (;;) { if (compbdf < 0 || compbdf >= bdf) break; @@ -294,7 +293,7 @@ ehci_init(u16 bdf, int busid, int compbdf) cntl->companion[count].type = USB_TYPE_OHCI; count++; } - compbdf = pci_next(compbdf+1, &max); + compbdf = pci_next(compbdf+1, pci_bdf_to_bus(compbdf)); }
run_thread(configure_ehci, cntl);