Skip to content

Commit e96902e

Browse files
chenhuacaibjorn-helgaas
authored andcommitted
PCI/VGA: Move non-legacy VGA detection to ADD_DEVICE path
a37c0f4 ("vgaarb: Select a default VGA device even if there's no legacy VGA") extended the vga_arb_device_init() subsys_initcall so it could select a non-legacy VGA device as the default. That failed to consider that PCI devices may be enumerated after vga_arb_device_init(), e.g., hot-added devices or non-ACPI systems that do PCI enumeration in pcibios_init(). Devices found then could never be selected as the default. One system where this is a problem is the MIPS-based Loongson where an ASpeed AST2500 VGA device is behind a bridge that doesn't implement the VGA Enable bit, so legacy resources are not routed to the VGA device. [1] Fix this by moving the non-legacy VGA device selection from vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is called after every PCI device is enumerated, either by the vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier. [1] https://lore.kernel.org/r/20210514080025.1828197-6-chenhuacai@loongson.cn [bhelgaas: commit log, restructure] Link: https://lore.kernel.org/r/20211015061512.2941859-5-chenhuacai@loongson.cn Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn Link: https://lore.kernel.org/r/20220224224753.297579-7-helgaas@kernel.org Signed-off-by: Huacai Chen <chenhuacai@loongson.cn> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Cc: Daniel Axtens <dja@axtens.net> Cc: Zhou Wang <wangzhou1@hisilicon.com>
1 parent f8d81df commit e96902e

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

drivers/pci/vgaarb.c

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ static bool vga_is_boot_device(struct vga_device *vgadev)
624624
{
625625
struct vga_device *boot_vga = vgadev_find(vga_default_device());
626626
struct pci_dev *pdev = vgadev->pdev;
627+
u16 cmd, boot_cmd;
627628

628629
/*
629630
* We select the default VGA device in this order:
@@ -661,6 +662,37 @@ static bool vga_is_boot_device(struct vga_device *vgadev)
661662
if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
662663
return true;
663664

665+
/*
666+
* If we haven't found a legacy VGA device, accept a non-legacy
667+
* device. It may have either IO or MEM enabled, and bridges may
668+
* not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
669+
* use legacy VGA resources. Prefer an integrated GPU over others.
670+
*/
671+
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
672+
if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
673+
674+
/*
675+
* An integrated GPU overrides a previous non-legacy
676+
* device. We expect only a single integrated GPU, but if
677+
* there are more, we use the *last* because that was the
678+
* previous behavior.
679+
*/
680+
if (vga_arb_integrated_gpu(&pdev->dev))
681+
return true;
682+
683+
/*
684+
* We prefer the first non-legacy discrete device we find.
685+
* If we already found one, vgadev is no better.
686+
*/
687+
if (boot_vga) {
688+
pci_read_config_word(boot_vga->pdev, PCI_COMMAND,
689+
&boot_cmd);
690+
if (boot_cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
691+
return false;
692+
}
693+
return true;
694+
}
695+
664696
return false;
665697
}
666698

@@ -1529,30 +1561,8 @@ static struct miscdevice vga_arb_device = {
15291561

15301562
static void __init vga_arb_select_default_device(void)
15311563
{
1532-
struct pci_dev *pdev, *found = NULL;
15331564
struct vga_device *vgadev;
15341565

1535-
if (!vga_default_device()) {
1536-
list_for_each_entry_reverse(vgadev, &vga_list, list) {
1537-
struct device *dev = &vgadev->pdev->dev;
1538-
u16 cmd;
1539-
1540-
pdev = vgadev->pdev;
1541-
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1542-
if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
1543-
found = pdev;
1544-
if (vga_arb_integrated_gpu(dev))
1545-
break;
1546-
}
1547-
}
1548-
}
1549-
1550-
if (found) {
1551-
vgaarb_info(&found->dev, "setting as boot device (VGA legacy resources not available)\n");
1552-
vga_set_default_device(found);
1553-
return;
1554-
}
1555-
15561566
if (!vga_default_device()) {
15571567
vgadev = list_first_entry_or_null(&vga_list,
15581568
struct vga_device, list);

0 commit comments

Comments
 (0)