From 7e845ecb2fbfa1bf800e703df29ee2e06592c2a0 Mon Sep 17 00:00:00 2001 From: Sui Jingfeng Date: Wed, 30 Aug 2023 19:15:28 +0800 Subject: PCI: Add pci_is_vga() helper The PCI Code and ID Assignment spec, r1.15, secs 1.4 and 1.1, define VGA Base Class and Sub-Classes: 03 00 PCI_CLASS_DISPLAY_VGA VGA-compatible or 8514-compatible 00 01 PCI_CLASS_NOT_DEFINED_VGA VGA-compatible (before Class Code) Add a pci_is_vga() helper to return true if a device is in either category. These VGA devices use the hardwired legacy VGA resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], [io 0x3c0-0x3df] and aliases), so they require special handling if more than one is present in the system. Link: https://lore.kernel.org/r/20230830111532.444535-2-sui.jingfeng@linux.dev Signed-off-by: Sui Jingfeng [bhelgaas: commit log, drop !pdev test] Signed-off-by: Bjorn Helgaas Cc: "Maciej W. Rozycki" --- include/linux/pci.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/pci.h b/include/linux/pci.h index 8c7c2c3c6c65..7bab234391cb 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -713,6 +713,30 @@ static inline bool pci_is_bridge(struct pci_dev *dev) dev->hdr_type == PCI_HEADER_TYPE_CARDBUS; } +/** + * pci_is_vga - check if the PCI device is a VGA device + * + * The PCI Code and ID Assignment spec, r1.15, secs 1.4 and 1.1, define + * VGA Base Class and Sub-Classes: + * + * 03 00 PCI_CLASS_DISPLAY_VGA VGA-compatible or 8514-compatible + * 00 01 PCI_CLASS_NOT_DEFINED_VGA VGA-compatible (before Class Code) + * + * Return true if the PCI device is a VGA device and uses the legacy VGA + * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], [io 0x3c0-0x3df] and + * aliases). + */ +static inline bool pci_is_vga(struct pci_dev *pdev) +{ + if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) + return true; + + if ((pdev->class >> 8) == PCI_CLASS_NOT_DEFINED_VGA) + return true; + + return false; +} + #define for_each_pci_bridge(dev, bus) \ list_for_each_entry(dev, &bus->devices, bus_list) \ if (!pci_is_bridge(dev)) {} else -- cgit v1.2.3-58-ga151 From 655e6fe178960c50bfbb0bfe3c4a12c82b1ad918 Mon Sep 17 00:00:00 2001 From: Sui Jingfeng Date: Wed, 30 Aug 2023 19:15:29 +0800 Subject: PCI/VGA: Use pci_is_vga() to identify VGA devices Use pci_is_vga() to identify VGA devices, so the arbiter will handle old PCI_CLASS_NOT_DEFINED_VGA (0x0001) devices as well as the PCI_CLASS_DISPLAY_VGA (0x0300) devices it previously handled. Link: https://lore.kernel.org/r/20230830111532.444535-3-sui.jingfeng@linux.dev Signed-off-by: Sui Jingfeng [bhelgaas: commit log, split functional change from optimization] Signed-off-by: Bjorn Helgaas Cc: "Maciej W. Rozycki" --- drivers/pci/vgaarb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c index 5e6b1eb54c64..2ef3c88e9c33 100644 --- a/drivers/pci/vgaarb.c +++ b/drivers/pci/vgaarb.c @@ -765,7 +765,7 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev) u16 cmd; /* Only deal with VGA class devices */ - if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) + if (!pci_is_vga(pdev)) return false; /* Allocate structure */ -- cgit v1.2.3-58-ga151 From 300bac9389e09274049fee5d367ae2b296bc8a10 Mon Sep 17 00:00:00 2001 From: Sui Jingfeng Date: Fri, 6 Oct 2023 16:48:38 -0500 Subject: PCI/VGA: Select VGA devices earlier Select VGA devices in vga_arb_device_init() and pci_notify() instead of in vga_arbiter_add_pci_device(). This is a trivial optimization for adding devices. It's a bigger optimization for the removal case because pci_notify() won't call vga_arbiter_del_pci_device() for non-VGA devices, so it won't have to search the vga_list for them. https://lore.kernel.org/r/20230830111532.444535-3-sui.jingfeng@linux.dev Signed-off-by: Sui Jingfeng [bhelgaas: commit log, split from functional change] Signed-off-by: Bjorn Helgaas --- drivers/pci/vgaarb.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c index 2ef3c88e9c33..feca96fc4255 100644 --- a/drivers/pci/vgaarb.c +++ b/drivers/pci/vgaarb.c @@ -764,10 +764,6 @@ static bool vga_arbiter_add_pci_device(struct pci_dev *pdev) struct pci_dev *bridge; u16 cmd; - /* Only deal with VGA class devices */ - if (!pci_is_vga(pdev)) - return false; - /* Allocate structure */ vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL); if (vgadev == NULL) { @@ -1503,6 +1499,10 @@ 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)) + return 0; + /* * For now, we're only interested in devices added and removed. * I didn't test this thing here, so someone needs to double check @@ -1550,8 +1550,10 @@ static int __init vga_arb_device_init(void) pdev = NULL; while ((pdev = pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, - PCI_ANY_ID, pdev)) != NULL) - vga_arbiter_add_pci_device(pdev); + PCI_ANY_ID, pdev)) != NULL) { + if (pci_is_vga(pdev)) + vga_arbiter_add_pci_device(pdev); + } pr_info("loaded\n"); return rc; -- cgit v1.2.3-58-ga151 From cdd3cecb521520ff316eea5ee36608b63a8df9f9 Mon Sep 17 00:00:00 2001 From: Sui Jingfeng Date: Wed, 30 Aug 2023 19:15:30 +0800 Subject: PCI/sysfs: Enable 'boot_vga' attribute via pci_is_vga() Enable the 'boot_vga' sysfs attribute via pci_is_vga(). This exposes 'boot_vga' for old PCI_CLASS_NOT_DEFINED_VGA (0x0001) devices as well as for the PCI_CLASS_DISPLAY_VGA (0x0300) devices where it was previously exposed. Link: https://lore.kernel.org/r/20230830111532.444535-4-sui.jingfeng@linux.dev Signed-off-by: Sui Jingfeng [bhelgaas: commit log] Signed-off-by: Bjorn Helgaas Cc: "Maciej W. Rozycki" --- drivers/pci/pci-sysfs.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index d9eede2dbc0e..252ee29fd697 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1551,11 +1551,10 @@ 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) - if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) - return 0; + if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev)) + return a->mode; - return a->mode; + return 0; } static struct attribute *pci_dev_hp_attrs[] = { -- cgit v1.2.3-58-ga151 From 76432cf63e2f56df23bf72c1ef9e1b3ef87a05d7 Mon Sep 17 00:00:00 2001 From: Sui Jingfeng Date: Wed, 30 Aug 2023 19:15:31 +0800 Subject: drm/virtio: Use pci_is_vga() to identify VGA devices Use pci_is_vga() to identify VGA devices instead of open-coding the class test. This means virtio_gpu_pci_quirk() will apply to old PCI_CLASS_NOT_DEFINED_VGA (0x0001) devices as well as the PCI_CLASS_DISPLAY_VGA (0x0300) devices it did previously. Link: https://lore.kernel.org/r/20230830111532.444535-5-sui.jingfeng@linux.dev Signed-off-by: Sui Jingfeng [bhelgaas: commit log] Signed-off-by: Bjorn Helgaas Cc: David Airlie Cc: Gerd Hoffmann Cc: Gurchetan Singh Cc: Chia-I Wu Cc: Daniel Vetter --- drivers/gpu/drm/virtio/virtgpu_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.c b/drivers/gpu/drm/virtio/virtgpu_drv.c index 644b8ee51009..4334c7608408 100644 --- a/drivers/gpu/drm/virtio/virtgpu_drv.c +++ b/drivers/gpu/drm/virtio/virtgpu_drv.c @@ -51,7 +51,7 @@ static int virtio_gpu_pci_quirk(struct drm_device *dev) { struct pci_dev *pdev = to_pci_dev(dev->dev); const char *pname = dev_name(&pdev->dev); - bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; + bool vga = pci_is_vga(pdev); int ret; DRM_INFO("pci: %s detected at %s\n", -- cgit v1.2.3-58-ga151 From 94cfada2a9cadec8e5302294fb1a144addfe6649 Mon Sep 17 00:00:00 2001 From: Sui Jingfeng Date: Wed, 30 Aug 2023 19:15:32 +0800 Subject: drm/qxl: Use pci_is_vga() to identify VGA devices Use pci_is_vga() to identify VGA devices instead of a private is_vga() function. This means qxl will use the VGA arbiter for old PCI_CLASS_NOT_DEFINED_VGA (0x0001) devices as well as the PCI_CLASS_DISPLAY_VGA (0x0300) devices it recognized previously. This probably doesn't make a difference because qxl_pci_driver doesn't claim PCI_CLASS_NOT_DEFINED_VGA devices by default, so it's mainly a code simplification. Link: https://lore.kernel.org/r/20230830111532.444535-6-sui.jingfeng@linux.dev Signed-off-by: Sui Jingfeng [bhelgaas: commit log] Signed-off-by: Bjorn Helgaas Cc: Dave Airlie Cc: Gerd Hoffmann Cc: David Airlie Cc: Daniel Vetter --- drivers/gpu/drm/qxl/qxl_drv.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/qxl/qxl_drv.c b/drivers/gpu/drm/qxl/qxl_drv.c index b30ede1cf62d..b94411ae17f2 100644 --- a/drivers/gpu/drm/qxl/qxl_drv.c +++ b/drivers/gpu/drm/qxl/qxl_drv.c @@ -68,11 +68,6 @@ module_param_named(num_heads, qxl_num_crtc, int, 0400); static struct drm_driver qxl_driver; static struct pci_driver qxl_pci_driver; -static bool is_vga(struct pci_dev *pdev) -{ - return pdev->class == PCI_CLASS_DISPLAY_VGA << 8; -} - static int qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) { @@ -100,7 +95,7 @@ qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) if (ret) goto disable_pci; - if (is_vga(pdev) && pdev->revision < 5) { + if (pci_is_vga(pdev) && pdev->revision < 5) { ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO); if (ret) { DRM_ERROR("can't get legacy vga ioports\n"); @@ -131,7 +126,7 @@ modeset_cleanup: unload: qxl_device_fini(qdev); put_vga: - if (is_vga(pdev) && pdev->revision < 5) + if (pci_is_vga(pdev) && pdev->revision < 5) vga_put(pdev, VGA_RSRC_LEGACY_IO); disable_pci: pci_disable_device(pdev); @@ -159,7 +154,7 @@ qxl_pci_remove(struct pci_dev *pdev) drm_dev_unregister(dev); drm_atomic_helper_shutdown(dev); - if (is_vga(pdev) && pdev->revision < 5) + if (pci_is_vga(pdev) && pdev->revision < 5) vga_put(pdev, VGA_RSRC_LEGACY_IO); } -- cgit v1.2.3-58-ga151