Skip to content

Commit dfe3da8

Browse files
chenhuacaibjorn-helgaas
authored andcommitted
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after the vga_arb_device_init() subsys_initcall. vga_arbiter_add_pci_device() selects the first fully enabled device to which legacy VGA resources are routed as the default VGA device. This is an ADD_DEVICE notifier, so it runs after every PCI device is enumerated. vga_arb_select_default_device() may select framebuffer devices, partially enabled GPUs, or non-legacy devices that don't have legacy VGA resources routed to them as the default VGA device. But this only happens once, from the vga_arb_device_init() subsys_initcall, so it doesn't consider devices enumerated after that: acpi_init acpi_scan_init acpi_pci_root_init # PCI device enumeration (ACPI systems) vga_arb_device_init for_each_pci_device vga_arbiter_add_pci_device # ADD_DEVICE notifier if (VGA-owner) vga_set_default_device <-- set default VGA vga_arb_select_default_device # only called ONCE for_each_vga_device if (framebuffer) vga_set_default_device <-- set default VGA to framebuffer if (!vga_default_device()) if (non-legacy, integrated GPU, etc) vga_set_default_device <-- set default VGA if (!vga_default_device()) vga_set_default_device <-- set default VGA pcibios_init pcibios_scanbus # PCI device enumeration (non-ACPI systems) ... vga_arbiter_add_pci_device # ADD_DEVICE notification if (VGA-owner) vga_set_default_device <-- set default VGA Note that on non-ACPI systems, vga_arb_select_default_device() runs before pcibios_init(), so it sees no VGA devices and can never set a framebuffer device, a non-legacy integrated GPU, etc., as the default device. Factor out the default VGA device selection to vga_is_boot_device(), called from vga_arbiter_add_pci_device(). Then we can migrate the default device selection from vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path. [bhelgaas: commit log, split to separate patch] Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org Signed-off-by: Huacai Chen <chenhuacai@loongson.cn> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent 60a9bac commit dfe3da8

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

drivers/pci/vgaarb.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,41 @@ static bool vga_arb_integrated_gpu(struct device *dev)
628628
#endif
629629
}
630630

631+
/*
632+
* Return true if vgadev is a better default VGA device than the best one
633+
* we've seen so far.
634+
*/
635+
static bool vga_is_boot_device(struct vga_device *vgadev)
636+
{
637+
struct vga_device *boot_vga = vgadev_find(vga_default_device());
638+
639+
/*
640+
* We select the default VGA device in this order:
641+
* Firmware framebuffer (see vga_arb_select_default_device())
642+
* Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
643+
* Non-legacy integrated device (see vga_arb_select_default_device())
644+
* Non-legacy discrete device (see vga_arb_select_default_device())
645+
* Other device (see vga_arb_select_default_device())
646+
*/
647+
648+
/*
649+
* A legacy VGA device has MEM and IO enabled and any bridges
650+
* leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
651+
* resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
652+
* routed to it.
653+
*
654+
* We use the first one we find, so if we've already found one,
655+
* vgadev is no better.
656+
*/
657+
if (boot_vga)
658+
return false;
659+
660+
if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
661+
return true;
662+
663+
return false;
664+
}
665+
631666
/*
632667
* Rules for using a bridge to control a VGA descendant decoding: if a bridge
633668
* has only one VGA descendant then it can be used to control the VGA routing
@@ -755,12 +790,10 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
755790
bus = bus->parent;
756791
}
757792

758-
/* Deal with VGA default device. Use first enabled one
759-
* by default if arch doesn't have it's own hook
760-
*/
761-
if (vga_default == NULL &&
762-
((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) {
763-
vgaarb_info(&pdev->dev, "setting as boot VGA device\n");
793+
if (vga_is_boot_device(vgadev)) {
794+
vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
795+
vga_default_device() ?
796+
" (overriding previous)" : "");
764797
vga_set_default_device(pdev);
765798
}
766799

0 commit comments

Comments
 (0)