Skip to contentSkip to conversation

PCI: vgaarb: Include 0x0380 devices in default selection

PATCH Superseded Aaron Ma 3 participants 7 messages Jun 18 – Jun 22

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Aaron Ma

author
#0 lore raw

PATCH 1/1 PCI: vgaarb: Include 0x0380 devices in default selection

Some firmware boot GPUs report class 0x0380 (PCI_CLASS_DISPLAY_OTHER) instead of PCI_CLASS_DISPLAY_VGA. vgaarb only registers pci_is_vga() devices, so those GPUs are not considered by vga_is_firmware_default().

On these systems the AMD GPU matches the EFI framebuffer but is excluded from vgaarb because it is class 0x0380, while the NVIDIA GPU does not match the EFI framebuffer but becomes vga_default_device() through the vgaarb's enabled-device fallback.

Register legacy VGA and 0x0380 devices in vgaarb, and expose boot_vga for the same set of devices, so the firmware default device can be selected correctly.

---
 drivers/pci/pci-sysfs.c |  3 ++-
 drivers/pci/vgaarb.c    | 10 +++++-----
 include/linux/pci.h     | 14 ++++++++++++++
 3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +2 -1
index d37860841260c..843d83ec9550a 100644
@@ -1717,7 +1717,8 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
 	struct device *dev = kobj_to_dev(kobj);
 	struct pci_dev *pdev = to_pci_dev(dev);
 
-	if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
+	if (a == &dev_attr_boot_vga.attr &&
+	    pci_is_vga_or_other_display(pdev))
 		return a->mode;
 
 	if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
drivers/pci/vgaarb.c +5 -5
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
 	}
 
 	if (vga_is_boot_device(vgadev)) {
-		vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+		vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
 			    vga_default_device() ?
 			    " (overriding previous)" : "");
 		vga_set_default_device(pdev);
@@ -1483,8 +1483,8 @@ static int pci_notify(struct notifier_block *nb, unsigned long action,
 
 	vgaarb_dbg(dev, "%s\n", __func__);
 
-	/* Only deal with VGA class devices */
-	if (!pci_is_vga(pdev))
+	/* Only deal with legacy VGA and other display controller devices */
+	if (!pci_is_vga_or_other_display(pdev))
 		return 0;
 
 	/*
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
 	bus_register_notifier(&pci_bus_type, &pci_notifier);
 
-	/* Add all VGA class PCI devices by default */
+	/* Add legacy VGA and other display controller PCI devices by default */
 	pdev = NULL;
 	while ((pdev =
 		pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
 			       PCI_ANY_ID, pdev)) != NULL) {
-		if (pci_is_vga(pdev))
+		if (pci_is_vga_or_other_display(pdev))
 			vga_arbiter_add_pci_device(pdev);
 	}
 
include/linux/pci.h +14 -0
index 2c4454583c115..195ec1bdac863 100644
@@ -792,6 +792,20 @@ static inline bool pci_is_vga(struct pci_dev *pdev)
 	return false;
 }
 
+/**
+ * pci_is_vga_or_other_display - check if the PCI device is VGA or 0x0380
+ * @pdev: PCI device
+ *
+ * Return true for legacy VGA-compatible devices and for "other display
+ * controller" devices.  Some firmware-selected boot display devices expose
+ * class 0x0380 instead of PCI_CLASS_DISPLAY_VGA.
+ */
+static inline bool pci_is_vga_or_other_display(struct pci_dev *pdev)
+{
+	return pci_is_vga(pdev) ||
+		(pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER;
+}
+
 /**
  * pci_is_display - check if the PCI device is a display controller
  * @pdev: PCI device
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Including non-VGA display devices (0x0380) in the VGA arbiter incorrectly subjects them to legacy VGA arbitration, breaking standard MMIO accesses.

Pre-existing issues:
- [High] Concurrent calls to vga_set_default_device and unprotected reads in vga_default_device cause double-puts and use-after-free vulnerabilities.
- [High] A race condition during early boot between bus_register_notifier and PCI device enumeration causes a kernel panic (BUG_ON). --

commit fe1f6cb8be8bef939febe2357fcf713ab76eb751
Author: Aaron Ma <[email protected]>

PCI: vgaarb: Include 0x0380 devices in default selection

This commit registers 0x0380 (PCI_CLASS_DISPLAY_OTHER) devices in vgaarb alongside legacy VGA devices. This allows firmware default devices on systems with class 0x0380 GPUs to be properly considered by the VGA arbiter for correct boot device selection.

drivers/pci/vgaarb.c static bool vga_arbiter_add_pci_device(struct pci_dev *pdev) · line 802

Quoted 13 lines
drivers/pci/vgaarb.c +1 -1
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
 	}
 
 	if (vga_is_boot_device(vgadev)) {
-		vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+		vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
 			    vga_default_device() ?
 			    " (overriding previous)" : "");
 		vga_set_default_device(pdev);

[Severity: High]
This isn't a bug introduced by this patch, but does the concurrent execution of vga_set_default_device() and vga_default_device() have protection against double-puts and use-after-free?

Looking at vga_set_default_device(), the global vga_default pointer is swapped and its reference count decremented without internal locking:

drivers/pci/vgaarb.c:vga_set_default_device() {
        ...
        pci_dev_put(vga_default);
        vga_default = pci_dev_get(pdev);
}

If two threads bypass the check concurrently, they could both execute pci_dev_put() on the same pointer. At the same time, a reader calling vga_default_device() can dereference the pointer just as it is freed.

[ ... ]

drivers/pci/vgaarb.c static int __init vga_arb_device_init(void) · line 1531

Quoted 3 lines
drivers/pci/vgaarb.c +0 -0
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
 	bus_register_notifier(&pci_bus_type, &pci_notifier);

[Severity: High]
This is a pre-existing issue, but does registering the bus notifier before the initialization loop finishes introduce a race condition?

In vga_arb_device_init(), the pci_notifier is registered right before iterating over the PCI devices. If a VGA device is hotplugged in this narrow window, pci_notify() will trigger vga_arbiter_add_pci_device(), adding it to the list.

When the enumeration loop subsequently finds the same device, it calls vga_arbiter_add_pci_device() again. The code explicitly panics if the device is already present:

drivers/pci/vgaarb.c:vga_arbiter_add_pci_device() {
        ...
        if (vgadev_find(pdev) != NULL) {
                BUG_ON(1);
                ...
}

drivers/pci/vgaarb.c static int __init vga_arb_device_init(void) · line 1538

Quoted 8 lines
drivers/pci/vgaarb.c +2 -2
-	/* Add all VGA class PCI devices by default */
+	/* Add legacy VGA and other display controller PCI devices by default */
 	pdev = NULL;
 	while ((pdev =
 		pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
 			       PCI_ANY_ID, pdev)) != NULL) {
-		if (pci_is_vga(pdev))
+		if (pci_is_vga_or_other_display(pdev))

[Severity: High]
Does registering 0x0380 non-VGA display devices here inadvertently subject them to legacy VGA arbitration, potentially breaking their MMIO access?

In vga_arbiter_add_pci_device(), the arbiter unconditionally assumes the device decodes legacy VGA IO and MEM:

drivers/pci/vgaarb.c:vga_arbiter_add_pci_device() {
        ...
        vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | ...
}

Non-VGA DRM drivers (like amdgpu for 0x0380 devices) typically do not register a VGA client to drop these legacy decodes. When another device requests VGA resources, __vga_tryget() believes there is a conflict and forcefully calls pci_set_vga_state(), which disables PCI_COMMAND_MEMORY:

drivers/pci/vgaarb.c:__vga_tryget() {
        ...
        if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
                pci_bits |= PCI_COMMAND_MEMORY;
        ...
        err = pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
}

Could this clear the memory enable bit on the 0x0380 device and lead to immediate driver crashes or system hangs?

Quoted 2 lines
drivers/pci/vgaarb.c +0 -0
 			vga_arbiter_add_pci_device(pdev);
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Aaron Ma
Quoted 86 lines · Aaron Ma

Some firmware boot GPUs report class 0x0380 (PCI_CLASS_DISPLAY_OTHER) instead of PCI_CLASS_DISPLAY_VGA. vgaarb only registers pci_is_vga() devices, so those GPUs are not considered by vga_is_firmware_default().

On these systems the AMD GPU matches the EFI framebuffer but is excluded from vgaarb because it is class 0x0380, while the NVIDIA GPU does not match the EFI framebuffer but becomes vga_default_device() through the vgaarb's enabled-device fallback.

Register legacy VGA and 0x0380 devices in vgaarb, and expose boot_vga for the same set of devices, so the firmware default device can be selected correctly.

---
  drivers/pci/pci-sysfs.c |  3 ++-
  drivers/pci/vgaarb.c    | 10 +++++-----
  include/linux/pci.h     | 14 ++++++++++++++
  3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +2 -1
index d37860841260c..843d83ec9550a 100644
@@ -1717,7 +1717,8 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
  	struct device *dev = kobj_to_dev(kobj);
  	struct pci_dev *pdev = to_pci_dev(dev);
 
-	if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
+	if (a == &dev_attr_boot_vga.attr &&
+	    pci_is_vga_or_other_display(pdev))
  		return a->mode;
  >   	if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
drivers/pci/vgaarb.c +5 -5
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
  	}
 
  	if (vga_is_boot_device(vgadev)) {
-		vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+		vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
  			    vga_default_device() ?
  			    " (overriding previous)" : "");
  		vga_set_default_device(pdev);
@@ -1483,8 +1483,8 @@ static int pci_notify(struct notifier_block *nb, unsigned long action,
 
  	vgaarb_dbg(dev, "%s\n", __func__);
 
-	/* Only deal with VGA class devices */
-	if (!pci_is_vga(pdev))
+	/* Only deal with legacy VGA and other display controller devices */
+	if (!pci_is_vga_or_other_display(pdev)) >   		return 0;
 
  	/*
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
  	bus_register_notifier(&pci_bus_type, &pci_notifier);
 
-	/* Add all VGA class PCI devices by default */
+	/* Add legacy VGA and other display controller PCI devices by default */
  	pdev = NULL;
  	while ((pdev =
  		pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  			       PCI_ANY_ID, pdev)) != NULL) {
-		if (pci_is_vga(pdev))
+		if (pci_is_vga_or_other_display(pdev))
  			vga_arbiter_add_pci_device(pdev);
  	}
 
include/linux/pci.h +7 -0
index 2c4454583c115..195ec1bdac863 100644
@@ -792,6 +792,20 @@ static inline bool pci_is_vga(struct pci_dev *pdev)
  	return false;
  }
 
+/**
+ * pci_is_vga_or_other_display - check if the PCI device is VGA or 0x0380
+ * @pdev: PCI device
+ *
+ * Return true for legacy VGA-compatible devices and for "other display
+ * controller" devices.  Some firmware-selected boot display devices expose
+ * class 0x0380 instead of PCI_CLASS_DISPLAY_VGA.

include/linux/pci.h static inline bool pci_is_vga(struct pci_dev *pdev) · line 811

Quoted 10 lines
include/linux/pci.h +7 -0
+ */
+static inline bool pci_is_vga_or_other_display(struct pci_dev *pdev)
+{
+	return pci_is_vga(pdev) ||
+		(pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER;
+}
+
  /**
   * pci_is_display - check if the PCI device is a display controller
   * @pdev: PCI device

This patch doesn't seem correct to me. It's overloading the purpose of boot_vga to cover non VGA devices. We should be using boot_display.

Trying to hypothesize what's going on here - I guess you're seeing boot_display set for more than one device due to enumeration order.

Can you see if this helps instead?

╰─❯ git diff

arch/x86/video/video-common.c +0 -3
index 152789f00fcda..9440eb3a7b15d 100644
@@ -43,9 +43,6 @@ bool video_is_primary_device(struct device *dev)
        if (!pci_is_display(pdev))
                return false;
 
-       if (pdev == vga_default_device())
-               return true;
-
 #ifdef CONFIG_SCREEN_INFO
        numres = screen_info_resources(si, res, ARRAY_SIZE(res));
        for (i = 0; i < numres; ++i) {
author
#3 lore raw
Quoted 116 lines · Mario Limonciello
Quoted 86 lines · Aaron Ma

Some firmware boot GPUs report class 0x0380 (PCI_CLASS_DISPLAY_OTHER) instead of PCI_CLASS_DISPLAY_VGA. vgaarb only registers pci_is_vga() devices, so those GPUs are not considered by vga_is_firmware_default().

On these systems the AMD GPU matches the EFI framebuffer but is excluded from vgaarb because it is class 0x0380, while the NVIDIA GPU does not match the EFI framebuffer but becomes vga_default_device() through the vgaarb's enabled-device fallback.

Register legacy VGA and 0x0380 devices in vgaarb, and expose boot_vga for the same set of devices, so the firmware default device can be selected correctly.

---
  drivers/pci/pci-sysfs.c |  3 ++-
  drivers/pci/vgaarb.c    | 10 +++++-----
  include/linux/pci.h     | 14 ++++++++++++++
  3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +2 -1
index d37860841260c..843d83ec9550a 100644
@@ -1717,7 +1717,8 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
      struct device *dev = kobj_to_dev(kobj);
      struct pci_dev *pdev = to_pci_dev(dev);
 
-     if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
+     if (a == &dev_attr_boot_vga.attr &&
+         pci_is_vga_or_other_display(pdev))
              return a->mode;
  >          if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
drivers/pci/vgaarb.c +5 -5
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
      }
 
      if (vga_is_boot_device(vgadev)) {
-             vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+             vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
                          vga_default_device() ?
                          " (overriding previous)" : "");
              vga_set_default_device(pdev);
@@ -1483,8 +1483,8 @@ static int pci_notify(struct notifier_block *nb, unsigned long action,
 
      vgaarb_dbg(dev, "%s\n", __func__);
 
-     /* Only deal with VGA class devices */
-     if (!pci_is_vga(pdev))
+     /* Only deal with legacy VGA and other display controller devices */
+     if (!pci_is_vga_or_other_display(pdev)) >               return 0;
 
      /*
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
      bus_register_notifier(&pci_bus_type, &pci_notifier);
 
-     /* Add all VGA class PCI devices by default */
+     /* Add legacy VGA and other display controller PCI devices by default */
      pdev = NULL;
      while ((pdev =
              pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
                             PCI_ANY_ID, pdev)) != NULL) {
-             if (pci_is_vga(pdev))
+             if (pci_is_vga_or_other_display(pdev))
                      vga_arbiter_add_pci_device(pdev);
      }
 
include/linux/pci.h +7 -0
index 2c4454583c115..195ec1bdac863 100644
@@ -792,6 +792,20 @@ static inline bool pci_is_vga(struct pci_dev *pdev)
      return false;
  }
 
+/**
+ * pci_is_vga_or_other_display - check if the PCI device is VGA or 0x0380
+ * @pdev: PCI device
+ *
+ * Return true for legacy VGA-compatible devices and for "other display
+ * controller" devices.  Some firmware-selected boot display devices expose
+ * class 0x0380 instead of PCI_CLASS_DISPLAY_VGA.

include/linux/pci.h static inline bool pci_is_vga(struct pci_dev *pdev) · line 811

Quoted 10 lines
include/linux/pci.h +7 -0
+ */
+static inline bool pci_is_vga_or_other_display(struct pci_dev *pdev)
+{
+     return pci_is_vga(pdev) ||
+             (pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER;
+}
+
  /**
   * pci_is_display - check if the PCI device is a display controller
   * @pdev: PCI device

This patch doesn't seem correct to me. It's overloading the purpose of boot_vga to cover non VGA devices. We should be using boot_display.

Trying to hypothesize what's going on here - I guess you're seeing boot_display set for more than one device due to enumeration order.

Can you see if this helps instead?

╰─❯ git diff

arch/x86/video/video-common.c +0 -0
index 152789f00fcda..9440eb3a7b15d 100644

arch/x86/video/video-common.c bool video_is_primary_device(struct device *dev) · line 48

Quoted 9 lines
arch/x86/video/video-common.c +0 -3
@@ -43,9 +43,6 @@ bool video_is_primary_device(struct device *dev)
         if (!pci_is_display(pdev))
                 return false;
 
-       if (pdev == vga_default_device())
-               return true;
-
  #ifdef CONFIG_SCREEN_INFO
         numres = screen_info_resources(si, res, ARRAY_SIZE(res));
         for (i = 0; i < numres; ++i) {

Hi Mario,

I tested your change. boot_display becomes correct, but the issue is not fixed.

The kernel state becomes:

AMD: boot_display=1, boot_vga absent
NVIDIA: boot_display absent, boot_vga=1

Current Xorg checks boot_display || boot_vga, so both GPUs still match. The selected primary GPU still depends on Xorg's DRM device enumeration order.

So boot_vga is still wrong, and that still matters. The kernel should not expose boot_vga=1 for NVIDIA when the firmware framebuffer is on AMD.

My patch makes boot_vga and boot_display agree on the AMD boot display.

Thanks,
Aaron

#4 lore raw
Quoted 140 lines · Aaron Ma
Quoted 116 lines · Mario Limonciello
Quoted 86 lines · Aaron Ma

Some firmware boot GPUs report class 0x0380 (PCI_CLASS_DISPLAY_OTHER) instead of PCI_CLASS_DISPLAY_VGA. vgaarb only registers pci_is_vga() devices, so those GPUs are not considered by vga_is_firmware_default().

On these systems the AMD GPU matches the EFI framebuffer but is excluded from vgaarb because it is class 0x0380, while the NVIDIA GPU does not match the EFI framebuffer but becomes vga_default_device() through the vgaarb's enabled-device fallback.

Register legacy VGA and 0x0380 devices in vgaarb, and expose boot_vga for the same set of devices, so the firmware default device can be selected correctly.

---
   drivers/pci/pci-sysfs.c |  3 ++-
   drivers/pci/vgaarb.c    | 10 +++++-----
   include/linux/pci.h     | 14 ++++++++++++++
   3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +2 -1
index d37860841260c..843d83ec9550a 100644
@@ -1717,7 +1717,8 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
       struct device *dev = kobj_to_dev(kobj);
       struct pci_dev *pdev = to_pci_dev(dev);
 
-     if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
+     if (a == &dev_attr_boot_vga.attr &&
+         pci_is_vga_or_other_display(pdev))
               return a->mode;
  >          if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
drivers/pci/vgaarb.c +5 -5
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
       }
 
       if (vga_is_boot_device(vgadev)) {
-             vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+             vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
                           vga_default_device() ?
                           " (overriding previous)" : "");
               vga_set_default_device(pdev);
@@ -1483,8 +1483,8 @@ static int pci_notify(struct notifier_block *nb, unsigned long action,
 
       vgaarb_dbg(dev, "%s\n", __func__);
 
-     /* Only deal with VGA class devices */
-     if (!pci_is_vga(pdev))
+     /* Only deal with legacy VGA and other display controller devices */
+     if (!pci_is_vga_or_other_display(pdev)) >               return 0;
 
       /*
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
       bus_register_notifier(&pci_bus_type, &pci_notifier);
 
-     /* Add all VGA class PCI devices by default */
+     /* Add legacy VGA and other display controller PCI devices by default */
       pdev = NULL;
       while ((pdev =
               pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
                              PCI_ANY_ID, pdev)) != NULL) {
-             if (pci_is_vga(pdev))
+             if (pci_is_vga_or_other_display(pdev))
                       vga_arbiter_add_pci_device(pdev);
       }
 
include/linux/pci.h +7 -0
index 2c4454583c115..195ec1bdac863 100644
@@ -792,6 +792,20 @@ static inline bool pci_is_vga(struct pci_dev *pdev)
       return false;
   }
 
+/**
+ * pci_is_vga_or_other_display - check if the PCI device is VGA or 0x0380
+ * @pdev: PCI device
+ *
+ * Return true for legacy VGA-compatible devices and for "other display
+ * controller" devices.  Some firmware-selected boot display devices expose
+ * class 0x0380 instead of PCI_CLASS_DISPLAY_VGA.

include/linux/pci.h static inline bool pci_is_vga(struct pci_dev *pdev) · line 811

Quoted 10 lines
include/linux/pci.h +7 -0
+ */
+static inline bool pci_is_vga_or_other_display(struct pci_dev *pdev)
+{
+     return pci_is_vga(pdev) ||
+             (pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER;
+}
+
   /**
    * pci_is_display - check if the PCI device is a display controller
    * @pdev: PCI device

This patch doesn't seem correct to me. It's overloading the purpose of boot_vga to cover non VGA devices. We should be using boot_display.

Trying to hypothesize what's going on here - I guess you're seeing boot_display set for more than one device due to enumeration order.

Can you see if this helps instead?

╰─❯ git diff

arch/x86/video/video-common.c +0 -0
index 152789f00fcda..9440eb3a7b15d 100644

arch/x86/video/video-common.c bool video_is_primary_device(struct device *dev) · line 48

Quoted 9 lines
arch/x86/video/video-common.c +0 -3
@@ -43,9 +43,6 @@ bool video_is_primary_device(struct device *dev)
          if (!pci_is_display(pdev))
                  return false;
 
-       if (pdev == vga_default_device())
-               return true;
-
   #ifdef CONFIG_SCREEN_INFO
          numres = screen_info_resources(si, res, ARRAY_SIZE(res));
          for (i = 0; i < numres; ++i) {

Hi Mario,

I tested your change. boot_display becomes correct, but the issue is not fixed.

The kernel state becomes:

AMD: boot_display=1, boot_vga absent
NVIDIA: boot_display absent, boot_vga=1

Current Xorg checks boot_display || boot_vga, so both GPUs still match. The selected primary GPU still depends on Xorg's DRM device enumeration order.

Quoted 7 lines

So boot_vga is still wrong, and that still matters. The kernel should not expose boot_vga=1 for NVIDIA when the firmware framebuffer is on AMD.

My patch makes boot_vga and boot_display agree on the AMD boot display.

Thanks,
Aaron

Got it. I still don't think it's correct to overload boot_vga. It would be better for us to not show boot_vga at all - because it's not correct.

Maybe we should drop the fallback code in VGA arbiter that is setting NVIDIA as boot_vga at all.

Could you look at doing that?

author
#5 lore raw
Quoted 152 lines · Mario Limonciello
Quoted 140 lines · Aaron Ma
Quoted 116 lines · Mario Limonciello
Quoted 86 lines · Aaron Ma

Some firmware boot GPUs report class 0x0380 (PCI_CLASS_DISPLAY_OTHER) instead of PCI_CLASS_DISPLAY_VGA. vgaarb only registers pci_is_vga() devices, so those GPUs are not considered by vga_is_firmware_default().

On these systems the AMD GPU matches the EFI framebuffer but is excluded from vgaarb because it is class 0x0380, while the NVIDIA GPU does not match the EFI framebuffer but becomes vga_default_device() through the vgaarb's enabled-device fallback.

Register legacy VGA and 0x0380 devices in vgaarb, and expose boot_vga for the same set of devices, so the firmware default device can be selected correctly.

---
   drivers/pci/pci-sysfs.c |  3 ++-
   drivers/pci/vgaarb.c    | 10 +++++-----
   include/linux/pci.h     | 14 ++++++++++++++
   3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +2 -1
index d37860841260c..843d83ec9550a 100644
@@ -1717,7 +1717,8 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
       struct device *dev = kobj_to_dev(kobj);
       struct pci_dev *pdev = to_pci_dev(dev);
 
-     if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
+     if (a == &dev_attr_boot_vga.attr &&
+         pci_is_vga_or_other_display(pdev))
               return a->mode;
  >          if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
drivers/pci/vgaarb.c +5 -5
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
       }
 
       if (vga_is_boot_device(vgadev)) {
-             vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+             vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
                           vga_default_device() ?
                           " (overriding previous)" : "");
               vga_set_default_device(pdev);
@@ -1483,8 +1483,8 @@ static int pci_notify(struct notifier_block *nb, unsigned long action,
 
       vgaarb_dbg(dev, "%s\n", __func__);
 
-     /* Only deal with VGA class devices */
-     if (!pci_is_vga(pdev))
+     /* Only deal with legacy VGA and other display controller devices */
+     if (!pci_is_vga_or_other_display(pdev)) >               return 0;
 
       /*
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
       bus_register_notifier(&pci_bus_type, &pci_notifier);
 
-     /* Add all VGA class PCI devices by default */
+     /* Add legacy VGA and other display controller PCI devices by default */
       pdev = NULL;
       while ((pdev =
               pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
                              PCI_ANY_ID, pdev)) != NULL) {
-             if (pci_is_vga(pdev))
+             if (pci_is_vga_or_other_display(pdev))
                       vga_arbiter_add_pci_device(pdev);
       }
 
include/linux/pci.h +7 -0
index 2c4454583c115..195ec1bdac863 100644
@@ -792,6 +792,20 @@ static inline bool pci_is_vga(struct pci_dev *pdev)
       return false;
   }
 
+/**
+ * pci_is_vga_or_other_display - check if the PCI device is VGA or 0x0380
+ * @pdev: PCI device
+ *
+ * Return true for legacy VGA-compatible devices and for "other display
+ * controller" devices.  Some firmware-selected boot display devices expose
+ * class 0x0380 instead of PCI_CLASS_DISPLAY_VGA.

include/linux/pci.h static inline bool pci_is_vga(struct pci_dev *pdev) · line 811

Quoted 10 lines
include/linux/pci.h +7 -0
+ */
+static inline bool pci_is_vga_or_other_display(struct pci_dev *pdev)
+{
+     return pci_is_vga(pdev) ||
+             (pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER;
+}
+
   /**
    * pci_is_display - check if the PCI device is a display controller
    * @pdev: PCI device

This patch doesn't seem correct to me. It's overloading the purpose of boot_vga to cover non VGA devices. We should be using boot_display.

Trying to hypothesize what's going on here - I guess you're seeing boot_display set for more than one device due to enumeration order.

Can you see if this helps instead?

╰─❯ git diff

arch/x86/video/video-common.c +0 -0
index 152789f00fcda..9440eb3a7b15d 100644

arch/x86/video/video-common.c bool video_is_primary_device(struct device *dev) · line 48

Quoted 9 lines
arch/x86/video/video-common.c +0 -3
@@ -43,9 +43,6 @@ bool video_is_primary_device(struct device *dev)
          if (!pci_is_display(pdev))
                  return false;
 
-       if (pdev == vga_default_device())
-               return true;
-
   #ifdef CONFIG_SCREEN_INFO
          numres = screen_info_resources(si, res, ARRAY_SIZE(res));
          for (i = 0; i < numres; ++i) {

Hi Mario,

I tested your change. boot_display becomes correct, but the issue is not fixed.

The kernel state becomes:

AMD: boot_display=1, boot_vga absent
NVIDIA: boot_display absent, boot_vga=1

Current Xorg checks boot_display || boot_vga, so both GPUs still match. The selected primary GPU still depends on Xorg's DRM device enumeration order.

Quoted 7 lines

So boot_vga is still wrong, and that still matters. The kernel should not expose boot_vga=1 for NVIDIA when the firmware framebuffer is on AMD.

My patch makes boot_vga and boot_display agree on the AMD boot display.

Thanks,
Aaron

Quoted 9 lines

Got it. I still don't think it's correct to overload boot_vga. It would be better for us to not show boot_vga at all - because it's not correct.

Maybe we should drop the fallback code in VGA arbiter that is setting NVIDIA as boot_vga at all.

Could you look at doing that?

Hi Mario,

I looked at dropping the fallback. I don't think we should remove it globally; old systems may still need vga_default_device() when no firmware framebuffer owner can be identified.

For v2 I will keep the fallback, but avoid the wrong result on this system. The 0x0380 firmware display can become the default/boot_vga device, while legacy VGA decoding and ownership stay limited to pci_is_vga() devices.

So this keeps compatibility, but avoids reporting NVIDIA as boot_vga when the firmware framebuffer is on AMD.

Thanks,
Aaron

#6 lore raw
Quoted 161 lines · Aaron Ma
Quoted 159 lines · Mario Limonciello
Quoted 140 lines · Aaron Ma
Quoted 116 lines · Mario Limonciello
Quoted 86 lines · Aaron Ma

Some firmware boot GPUs report class 0x0380 (PCI_CLASS_DISPLAY_OTHER) instead of PCI_CLASS_DISPLAY_VGA. vgaarb only registers pci_is_vga() devices, so those GPUs are not considered by vga_is_firmware_default().

On these systems the AMD GPU matches the EFI framebuffer but is excluded from vgaarb because it is class 0x0380, while the NVIDIA GPU does not match the EFI framebuffer but becomes vga_default_device() through the vgaarb's enabled-device fallback.

Register legacy VGA and 0x0380 devices in vgaarb, and expose boot_vga for the same set of devices, so the firmware default device can be selected correctly.

---
    drivers/pci/pci-sysfs.c |  3 ++-
    drivers/pci/vgaarb.c    | 10 +++++-----
    include/linux/pci.h     | 14 ++++++++++++++
    3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +2 -1
index d37860841260c..843d83ec9550a 100644
@@ -1717,7 +1717,8 @@ static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
        struct device *dev = kobj_to_dev(kobj);
        struct pci_dev *pdev = to_pci_dev(dev);
 
-     if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
+     if (a == &dev_attr_boot_vga.attr &&
+         pci_is_vga_or_other_display(pdev))
                return a->mode;
  >          if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
drivers/pci/vgaarb.c +5 -5
index c360eee11dd9e..0e0878189e3d8 100644
@@ -796,7 +796,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
        }
 
        if (vga_is_boot_device(vgadev)) {
-             vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
+             vgaarb_info(&pdev->dev, "setting as boot display device%s\n",
                            vga_default_device() ?
                            " (overriding previous)" : "");
                vga_set_default_device(pdev);
@@ -1483,8 +1483,8 @@ static int pci_notify(struct notifier_block *nb, unsigned long action,
 
        vgaarb_dbg(dev, "%s\n", __func__);
 
-     /* Only deal with VGA class devices */
-     if (!pci_is_vga(pdev))
+     /* Only deal with legacy VGA and other display controller devices */
+     if (!pci_is_vga_or_other_display(pdev)) >               return 0;
 
        /*
@@ -1530,12 +1530,12 @@ static int __init vga_arb_device_init(void)
 
        bus_register_notifier(&pci_bus_type, &pci_notifier);
 
-     /* Add all VGA class PCI devices by default */
+     /* Add legacy VGA and other display controller PCI devices by default */
        pdev = NULL;
        while ((pdev =
                pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
                               PCI_ANY_ID, pdev)) != NULL) {
-             if (pci_is_vga(pdev))
+             if (pci_is_vga_or_other_display(pdev))
                        vga_arbiter_add_pci_device(pdev);
        }
 
include/linux/pci.h +7 -0
index 2c4454583c115..195ec1bdac863 100644
@@ -792,6 +792,20 @@ static inline bool pci_is_vga(struct pci_dev *pdev)
        return false;
    }
 
+/**
+ * pci_is_vga_or_other_display - check if the PCI device is VGA or 0x0380
+ * @pdev: PCI device
+ *
+ * Return true for legacy VGA-compatible devices and for "other display
+ * controller" devices.  Some firmware-selected boot display devices expose
+ * class 0x0380 instead of PCI_CLASS_DISPLAY_VGA.

include/linux/pci.h static inline bool pci_is_vga(struct pci_dev *pdev) · line 811

Quoted 10 lines
include/linux/pci.h +7 -0
+ */
+static inline bool pci_is_vga_or_other_display(struct pci_dev *pdev)
+{
+     return pci_is_vga(pdev) ||
+             (pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER;
+}
+
    /**
     * pci_is_display - check if the PCI device is a display controller
     * @pdev: PCI device

This patch doesn't seem correct to me. It's overloading the purpose of boot_vga to cover non VGA devices. We should be using boot_display.

Trying to hypothesize what's going on here - I guess you're seeing boot_display set for more than one device due to enumeration order.

Can you see if this helps instead?

╰─❯ git diff

arch/x86/video/video-common.c +0 -0
index 152789f00fcda..9440eb3a7b15d 100644

arch/x86/video/video-common.c bool video_is_primary_device(struct device *dev) · line 48

Quoted 9 lines
arch/x86/video/video-common.c +0 -3
@@ -43,9 +43,6 @@ bool video_is_primary_device(struct device *dev)
           if (!pci_is_display(pdev))
                   return false;
 
-       if (pdev == vga_default_device())
-               return true;
-
    #ifdef CONFIG_SCREEN_INFO
           numres = screen_info_resources(si, res, ARRAY_SIZE(res));
           for (i = 0; i < numres; ++i) {

Hi Mario,

I tested your change. boot_display becomes correct, but the issue is not fixed.

The kernel state becomes:

AMD: boot_display=1, boot_vga absent
NVIDIA: boot_display absent, boot_vga=1

Current Xorg checks boot_display || boot_vga, so both GPUs still match. The selected primary GPU still depends on Xorg's DRM device enumeration order.

Quoted 7 lines

So boot_vga is still wrong, and that still matters. The kernel should not expose boot_vga=1 for NVIDIA when the firmware framebuffer is on AMD.

My patch makes boot_vga and boot_display agree on the AMD boot display.

Thanks,
Aaron

Got it. I still don't think it's correct to overload boot_vga. It would be better for us to not show boot_vga at all - because it's not correct.

Maybe we should drop the fallback code in VGA arbiter that is setting NVIDIA as boot_vga at all.

Quoted 9 lines
Quoted 2 lines

Could you look at doing that?

Hi Mario,

I looked at dropping the fallback. I don't think we should remove it globally; old systems may still need vga_default_device() when no firmware framebuffer owner can be identified.

What exactly happens on such old systems? Any chance you have anything in your cert lab that's really old you could try?

I have to wonder if the boot_display code will work on them at least and we can just rely upon that.

I don't want to keep this fallback path around as tech debt if we don't need it and it's actively causing problems.

Quoted 7 lines

For v2 I will keep the fallback, but avoid the wrong result on this system. The 0x0380 firmware display can become the default/boot_vga device, while legacy VGA decoding and ownership stay limited to pci_is_vga() devices.

So this keeps compatibility, but avoids reporting NVIDIA as boot_vga when the firmware framebuffer is on AMD.

I'll look over the patch.