Skip to contentSkip to conversation

Improve vfio-pci primary GPU assignment behavior

PATCH In review Alex Williamson 4 participants 8 messages Jun 16, 2022 – Jun 21, 2022

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v2 cover Improve vfio-pci primary GPU assignment behavior

When assigning a primary graphics device to VM through vfio-pci device assignment, users often prevent binding of the native PCI graphics driver to avoid device initialization conflicts, however firmware console drivers may still be attached to the device which can often be cumbersome to manually unbind or exclude via cmdline options.

This series proposes to move the DRM aperture helpers out to drivers/video/ to make it more accessible to drivers like vfio-pci, which have neither dependencies on DRM code nor a struct drm_driver to present to existing interfaces. vfio-pci can then trivially call into the aperture helpers to remove conflicting drivers, rather than open coding it ourselves as was proposed with a new symbol export in v1 of this series[1].

Thanks to Thomas for splitting out the aperture code with new documentation.

Thomas had proposed this going through the vfio tree with appropriate stakeholder acks, that's fine with me, but I'm also open to it going through the DRM tree given that the vfio-pci-core change is even more trivial now and the bulk of the changes are DRM/video paths. Thanks,

Alex

[1]https://lore.kernel.org/all/165453797543.3592816.6381793341352595461.stgit@omen/

---

Alex Williamson (1):
vfio/pci: Remove console drivers

Thomas Zimmermann (1):
drm: Implement DRM aperture helpers under video/

 Documentation/driver-api/aperture.rst |  13 +
 Documentation/driver-api/index.rst    |   1 +
 drivers/gpu/drm/drm_aperture.c        | 174 +------------
 drivers/gpu/drm/tiny/Kconfig          |   1 +
 drivers/vfio/pci/vfio_pci_core.c      |   5 +
 drivers/video/Kconfig                 |   6 +
 drivers/video/Makefile                |   2 +
 drivers/video/aperture.c              | 340 ++++++++++++++++++++++++++
 drivers/video/console/Kconfig         |   1 +
 drivers/video/fbdev/Kconfig           |   7 +-
 include/linux/aperture.h              |  56 +++++
 11 files changed, 440 insertions(+), 166 deletions(-)
 create mode 100644 Documentation/driver-api/aperture.rst
 create mode 100644 drivers/video/aperture.c
 create mode 100644 include/linux/aperture.h
#1 lore raw

PATCH v2 1/2 drm: Implement DRM aperture helpers under video/

From: Thomas Zimmermann <[email protected]>

Implement DRM's aperture helpers under video/ for sharing with other sub-systems. Remove DRM-isms from the interface. The helpers track the ownership of framebuffer apertures and provide hand-over from firmware, such as EFI and VESA, to native graphics drivers.

Other subsystems, such as fbdev and vfio, also have to maintain ownership of framebuffer apertures. Moving DRM's aperture helpers to a more public location allows all subsystems to interact with each other and share a common implementation.

The aperture helpers are selected by the various firmware drivers within DRM and fbdev, and the VGA text-console driver.

The original DRM interface is kept in place for use by DRM drivers.

---
 Documentation/driver-api/aperture.rst |   13 +
 Documentation/driver-api/index.rst    |    1 
 drivers/gpu/drm/drm_aperture.c        |  174 +----------------
 drivers/gpu/drm/tiny/Kconfig          |    1 
 drivers/video/Kconfig                 |    6 +
 drivers/video/Makefile                |    2 
 drivers/video/aperture.c              |  340 +++++++++++++++++++++++++++++++++
 drivers/video/console/Kconfig         |    1 
 drivers/video/fbdev/Kconfig           |    7 +
 include/linux/aperture.h              |   56 +++++
 10 files changed, 435 insertions(+), 166 deletions(-)
 create mode 100644 Documentation/driver-api/aperture.rst
 create mode 100644 drivers/video/aperture.c
 create mode 100644 include/linux/aperture.h
Documentation/driver-api/aperture.rst new file +13 -0
new file mode 100644
index 000000000000..d173f4e7a7d9
@@ -0,0 +1,13 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Managing Ownership of the Framebuffer Aperture
+==============================================
+
+.. kernel-doc:: drivers/video/aperture.c
+   :doc: overview
+
+.. kernel-doc:: include/linux/aperture.h
+   :internal:
+
+.. kernel-doc:: drivers/video/aperture.c
+   :export:
Documentation/driver-api/index.rst +1 -0
index a6d525cd9fc4..d3a58f77328e 100644
@@ -27,6 +27,7 @@ available subsections can be seen below.
    component
    message-based
    infiniband
+   aperture
    frame-buffer
    regulator
    reset
drivers/gpu/drm/drm_aperture.c +9 -165

207 lines not on this page · raw message

drivers/gpu/drm/tiny/Kconfig +1 -0
index 627d637a1e7e..027cd87c3d0d 100644
@@ -69,6 +69,7 @@ config DRM_PANEL_MIPI_DBI
 config DRM_SIMPLEDRM
 	tristate "Simple framebuffer driver"
 	depends on DRM && MMU
+	select APERTURE_HELPERS
 	select DRM_GEM_SHMEM_HELPER
 	select DRM_KMS_HELPER
 	help
drivers/video/Kconfig +6 -0
index 427a993c7f57..c69b45f8c427 100644
@@ -5,6 +5,12 @@
 
 menu "Graphics support"
 
+config APERTURE_HELPERS
+	bool
+	help
+	  Support tracking and hand-over of aperture ownership. Required
+	  for firmware graphics drivers.
+
 if HAS_IOMEM
 
 config HAVE_FB_ATMEL
drivers/video/Makefile +2 -0
index df7650adede9..5bb6b452cc83 100644
@@ -1,4 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_APERTURE_HELPERS)    += aperture.o
 obj-$(CONFIG_VGASTATE)            += vgastate.o
 obj-$(CONFIG_HDMI)                += hdmi.o
 
drivers/video/aperture.c new file +340 -0

340 lines not on this page · raw message

drivers/video/console/Kconfig +1 -0
index 40c50fa2dd70..22cea5082ac4 100644
@@ -10,6 +10,7 @@ config VGA_CONSOLE
 	depends on !4xx && !PPC_8xx && !SPARC && !M68K && !PARISC &&  !SUPERH && \
 		(!ARM || ARCH_FOOTBRIDGE || ARCH_INTEGRATOR || ARCH_NETWINDER) && \
 		!ARM64 && !ARC && !MICROBLAZE && !OPENRISC && !S390 && !UML
+	select APERTURE_HELPERS if (DRM || FB || VFIO_PCI_CORE)
 	default y
 	help
 	  Saying Y here will allow you to use Linux in text mode through a
drivers/video/fbdev/Kconfig +6 -1
index f2a6b81e45c4..cfc55273dc5d 100644
@@ -455,6 +455,7 @@ config FB_ATARI
 config FB_OF
 	bool "Open Firmware frame buffer device support"
 	depends on (FB = y) && PPC && (!PPC_PSERIES || PCI)
+	select APERTURE_HELPERS
 	select FB_CFB_FILLRECT
 	select FB_CFB_COPYAREA
 	select FB_CFB_IMAGEBLIT
@@ -527,6 +528,7 @@ config FB_IMSTT
 config FB_VGA16
 	tristate "VGA 16-color graphics support"
 	depends on FB && (X86 || PPC)
+	select APERTURE_HELPERS
 	select FB_CFB_FILLRECT
 	select FB_CFB_COPYAREA
 	select FB_CFB_IMAGEBLIT
@@ -551,7 +553,7 @@ config FB_STI
 	  BIOS routines contained in a ROM chip in HP PA-RISC based machines.
 	  Enabling this option will implement the linux framebuffer device
 	  using calls to the STI BIOS routines for initialisation.
-	
+
 	  If you enable this option, you will get a planar framebuffer device
 	  /dev/fb which will work on the most common HP graphic cards of the
 	  NGLE family, including the artist chips (in the 7xx and Bxxx series),
@@ -617,6 +619,7 @@ config FB_UVESA
 config FB_VESA
 	bool "VESA VGA graphics support"
 	depends on (FB = y) && X86
+	select APERTURE_HELPERS
 	select FB_CFB_FILLRECT
 	select FB_CFB_COPYAREA
 	select FB_CFB_IMAGEBLIT
@@ -630,6 +633,7 @@ config FB_VESA
 config FB_EFI
 	bool "EFI-based Framebuffer Support"
 	depends on (FB = y) && !IA64 && EFI
+	select APERTURE_HELPERS
 	select DRM_PANEL_ORIENTATION_QUIRKS
 	select FB_CFB_FILLRECT
 	select FB_CFB_COPYAREA
@@ -2190,6 +2194,7 @@ config FB_SIMPLE
 	tristate "Simple framebuffer support"
 	depends on FB
 	depends on !DRM_SIMPLEDRM
+	select APERTURE_HELPERS
 	select FB_CFB_FILLRECT
 	select FB_CFB_COPYAREA
 	select FB_CFB_IMAGEBLIT
include/linux/aperture.h new file +56 -0

56 lines not on this page · raw message

#2 lore raw

Re: [PATCH v2 1/2] drm: Implement DRM aperture helpers under video/

Quoted 7 lines · Alex Williamson

From: Thomas Zimmermann <[email protected]>

Implement DRM's aperture helpers under video/ for sharing with other sub-systems. Remove DRM-isms from the interface. The helpers track the ownership of framebuffer apertures and provide hand-over from firmware, such as EFI and VESA, to native graphics drivers.

Quoted 8 lines

Other subsystems, such as fbdev and vfio, also have to maintain ownership of framebuffer apertures. Moving DRM's aperture helpers to a more public location allows all subsystems to interact with each other and share a common implementation.

The aperture helpers are selected by the various firmware drivers within DRM and fbdev, and the VGA text-console driver.

Thanks a lot for working on this.

Quoted 5 lines

The original DRM interface is kept in place for use by DRM drivers.

---

[...]

Quoted 5 lines
drivers/video/Kconfig +0 -0
index 427a993c7f57..c69b45f8c427 100644

drivers/video/Kconfig line 13

Quoted 9 lines
drivers/video/Kconfig +6 -0
@@ -5,6 +5,12 @@
 
 menu "Graphics support"
 
+config APERTURE_HELPERS
+	bool
+	help
+	  Support tracking and hand-over of aperture ownership. Required
+	  for firmware graphics drivers.
+

Maybe "graphics drivers using a firmware-provided framebuffer" is more clear?

[...]

Quoted 20 lines
drivers/video/Kconfig +19 -0
+++ b/drivers/video/aperture.c
@@ -0,0 +1,340 @@
+// SPDX-License-Identifier: MIT
+
+#include <linux/aperture.h>
+#include <linux/device.h>
+#include <linux/fb.h> /* for old fbdev helpers */
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/pci.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <linux/vgaarb.h>
+
+/**
+ * DOC: overview
+ *
+ * A graphics device might be supported by different drivers, but only one
+ * driver can be active at any given time. Many systems load a generic

drivers/video/Kconfig line 28

Quoted 10 lines
drivers/video/Kconfig +10 -0
+ * graphics drivers, such as EFI-GOP or VESA, early during the boot process.
+ * During later boot stages, they replace the generic driver with a dedicated,
+ * hardware-specific driver. To take over the device the dedicated driver
+ * first has to remove the generic driver. Aperture functions manage
+ * ownership of framebuffer memory and hand-over between drivers.
+ *
+ * Graphics drivers should call remove_conflicting_devices()
+ * at the top of their probe function. The function removes any generic
+ * driver that is currently associated with the given framebuffer memory.
+ * If the framebuffer is located at PCI BAR 0, the rsp code looks as in the

s/rsp/respective

drivers/video/Kconfig line 30

Quoted 2 lines
drivers/video/Kconfig +2 -0
+ * example given below. The cod assumes a DRM driver.
+ *

s/cod/code

Quoted 8 lines
drivers/video/Kconfig +8 -0
+ * .. code-block:: c
+ *
+ *	static const struct drm_driver example_driver = {
+ *		.name = "exampledrm",
+ *		...
+ *	};
+ *
+ *	static int remove_conflicting_framebuffers(struct pci_dev *pdev)

drivers/video/Kconfig line 48

Quoted 10 lines
drivers/video/Kconfig +10 -0
+ *	{
+ *		bool primary = false;
+ *		resource_size_t base, size;
+ *		int ret;
+ *
+ *		base = pci_resource_start(pdev, 0);
+ *		size = pci_resource_len(pdev, 0);
+ *	#ifdef CONFIG_X86
+ *		primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
+ *	#endif

This example seems to be copied from drivers/gpu/drm/ast/ast_drv.c and I don't see any other driver that has its framebuffer located in PCI BAR 0 or at least having a similar code.

So I wonder if we really want to have this example for such a corner case ?

Also, remove_conflicting_pci_framebuffers() seems to already at least check for the IORESOURCE_ROM_SHADOW flag so it would be better to grow that and support this special case of PCI BAR 0 (maybe adding another param that is passed through remove_conflicting_pci_devices() ?

In any case, it seems to me that it is something that ast shouldn't really have to open code it and instead the helpers should be fixed to cover that case for drivers not to care. I would really not add the snippet in the doc.

Since we are talking about remove_conflicting_devices() here, a better code example could be for a platform device instead of a PCI device, like this:

* static const struct platform_driver example_driver = {
* .name = "example",
* ...
* };
*
* static int probe(struct platform_device *pdev)
* {
* struct resource *mem;
* resource_size_t base, size;
*
* mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
* if (!mem)
* return -EINVAL;
* base = mem->start;
* size = resource_size(mem);
*
* ret = remove_conflicting_devices(base, size, false, &example_driver->name);
* if (ret)
* return ret;
*
* // ... and initialize the hardware.
* ...
*
* return 0;
* }

drivers/video/Kconfig line 54

Quoted 6 lines
drivers/video/Kconfig +6 -0
+ *	static int probe(struct pci_dev *pdev)
+ *	{
+ *		int ret;
+ *
+ *		// Remove any generic drivers...
+ *		ret = remove_conflicting_framebuffers(pdev);

And here we can just use remove_conflicting_pci_devices(pdev) without the unnecessary level of indirection. It makes the example more clear IMO and it could be moved as an example for the remove_conflicting_pci_devices().

Another option is to have here an example for platform devices instead of a PCI device (and move this example when talking about remove

[...]

drivers/video/Kconfig line 59

Quoted 5 lines
drivers/video/Kconfig +5 -0
+ * PCI device drivers can also call remove_conflicting_pci_devices() and let the
+ * function detect the apertures automatically. Device drivers without knowledge of
+ * the framebuffer's location shall call remove_all_conflicting_devices(),
+ * which removes all known devices.
+ *

Can we get all the public aperture functions be in the aperture namespace? i.e:
aperture_remove_conflicting_devices(), aperture_remove_all_conflicting_devices() and so on. That makes easier to grep, ftrace and also read the drivers' code.

drivers/video/Kconfig line 63

Quoted 4 lines
drivers/video/Kconfig +4 -0
+ * Drivers that are susceptible to being removed by other drivers, such as
+ * generic EFI or VESA drivers, have to register themselves as owners of their
+ * framebuffer apertures. Ownership of the framebuffer memory is achieved
+ * by calling devm_acquire_aperture_of_platform_device(). On success, the driver

AFAICT the aperture infrastructure only allows to remove platform devices, even when it can check if the requested I/O resource overlaps with a PCI BAR range, so maybe all functions also should use _platform_device() as suffix instead of just _device() ? Or maybe the _platform is just verbose but I think that the functions should be named consistently and only use either _device or _platform.

[...]

drivers/video/Kconfig line 67

Quoted 4 lines
drivers/video/Kconfig +4 -0
+ *	static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
+ *	{
+ *		resource_size_t base, size;
+ *

This example is missing a struct resource *mem declaration.

drivers/video/Kconfig line 78

Quoted 11 lines
drivers/video/Kconfig +11 -0
+ * The generic driver is now subject to forced removal by other drivers. This
+ * only works for platform drivers that support hot unplugging.
+ * When a driver calls remove_conflicting_devices() et al
+ * for the registered framebuffer range, the aperture helpers call
+ * platform_device_unregister() and the generic driver unloads itself. It
+ * may not access the device's registers, framebuffer memory, ROM, etc
+ * afterwards.
+ */
+
+struct dev_aperture {
+	struct device *dev;

And here we could just use a struct platform_device *pdev since currently we only support platform devices. It seems to me that this is a DRM-ism that we are carrying since for DRM drivers made sense to use struct device.

Doing that would also allow get rid of indirections like the need of both a devm_acquire_aperture_of_platform_device() and devm_aperture_acquire() just to do a &pdev->dev.

And also some to_platform_device() in drm_aperture_detach_firmware() and detach_platform_device().

If we ever support non-platform devices then we can refactor it, but I don't think that is worth to complicate just in case we ever support struct device.

drivers/video/Kconfig line 82

Quoted 4 lines
drivers/video/Kconfig +4 -0
+	resource_size_t base;
+	resource_size_t size;
+	struct list_head lh;
+	void (*detach)(struct device *dev);

Same here, just void (*detach)(struct platform_device *pdev) if you agree with
that I mentioned above.

drivers/video/Kconfig line 92

Quoted 10 lines
drivers/video/Kconfig +10 -0
+};
+
+static LIST_HEAD(apertures);
+static DEFINE_MUTEX(apertures_lock);
+
+static bool overlap(resource_size_t base1, resource_size_t end1,
+		    resource_size_t base2, resource_size_t end2)
+{
+	return (base1 < end2) && (end1 > base2);
+}

There's a resource_overlaps() helper in include/linux/ioport.h, I wonder if it could just be used, maybe declaring and filling a struct resource just to call that helper. Later as an optimization a resource_range_overlap() or something could be proposed for include/linux/ioport.h.

Also, I noticed that resource_overlaps() uses <= and >= but this helper uses < and >. It seems there's an off-by-one error here but maybe I'm wrong on this.

[...]

drivers/video/Kconfig line 104

Quoted 12 lines
drivers/video/Kconfig +12 -0
+static void detach_platform_device(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+
+	/*
+	 * Remove the device from the device hierarchy. This is the right thing
+	 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
+	 * the new driver takes over the hardware, the firmware device's state
+	 * will be lost.
+	 *
+	 * For non-platform devices, a new callback would be required.
+	 *

I wonder if we ever are going to need this. AFAICT the problem only happens for platform devices. Or do you envision a case when some a bus could need this and the aperture unregister the device instead of the Linux kernel device model ?

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
#3 lore raw

Re: [PATCH v2 1/2] drm: Implement DRM aperture helpers under video/

Hi

Am 21.06.22 um 02:14 schrieb Javier Martinez Canillas:
[...]

Quoted 37 lines

Since we are talking about remove_conflicting_devices() here, a better code example could be for a platform device instead of a PCI device, like this:

* static const struct platform_driver example_driver = {
* .name = "example",
* ...
* };
*
* static int probe(struct platform_device *pdev)
* {
* struct resource *mem;
* resource_size_t base, size;
*
* mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
* if (!mem)
* return -EINVAL;
* base = mem->start;
* size = resource_size(mem);
*
* ret = remove_conflicting_devices(base, size, false, &example_driver->name);
* if (ret)
* return ret;
*
* // ... and initialize the hardware.
* ...
*
* return 0;
* }

Quoted 6 lines

+ * static int probe(struct pci_dev *pdev)
+ * {
+ * int ret;
+ *
+ * // Remove any generic drivers...
+ * ret = remove_conflicting_framebuffers(pdev);

Quoted 3 lines

And here we can just use remove_conflicting_pci_devices(pdev) without the unnecessary level of indirection. It makes the example more clear IMO and it could be moved as an example for the remove_conflicting_pci_devices().

I'll see if I can simplify the examples.

Quoted 12 lines

Another option is to have here an example for platform devices instead of a PCI device (and move this example when talking about remove

[...]

Quoted 5 lines

+ * PCI device drivers can also call remove_conflicting_pci_devices() and let the
+ * function detect the apertures automatically. Device drivers without knowledge of
+ * the framebuffer's location shall call remove_all_conflicting_devices(),
+ * which removes all known devices.
+ *

Quoted 3 lines

Can we get all the public aperture functions be in the aperture namespace? i.e:
aperture_remove_conflicting_devices(), aperture_remove_all_conflicting_devices() and so on. That makes easier to grep, ftrace and also read the drivers' code.

Ok.

Quoted 11 lines
Quoted 5 lines

+ * Drivers that are susceptible to being removed by other drivers, such as
+ * generic EFI or VESA drivers, have to register themselves as owners of their
+ * framebuffer apertures. Ownership of the framebuffer memory is achieved
+ * by calling devm_acquire_aperture_of_platform_device(). On success, the driver

AFAICT the aperture infrastructure only allows to remove platform devices, even when it can check if the requested I/O resource overlaps with a PCI BAR range, so maybe all functions also should use _platform_device() as suffix instead of just _device() ? Or maybe the _platform is just verbose but I think that the functions should be named consistently and only use either _device or _platform.

That nameing makes sense. Firmware drivers register a platform device. But native drivers unregister any device. If we ever have a generic driver that does not use platform devices, we'd need another variant of devm_acquire_aperture_of_*_device().

Quoted 36 lines

[...]

Quoted 4 lines

+ * static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
+ * {
+ * resource_size_t base, size;
+ *

This example is missing a struct resource *mem declaration.

drivers/video/aperture.c line 126

Quoted 11 lines

+ * The generic driver is now subject to forced removal by other drivers. This
+ * only works for platform drivers that support hot unplugging.
+ * When a driver calls remove_conflicting_devices() et al
+ * for the registered framebuffer range, the aperture helpers call
+ * platform_device_unregister() and the generic driver unloads itself. It
+ * may not access the device's registers, framebuffer memory, ROM, etc
+ * afterwards.
+ */

drivers/video/aperture.c new file +3 -0
+
+struct dev_aperture {
+	struct device *dev;

And here we could just use a struct platform_device *pdev since currently we only support platform devices. It seems to me that this is a DRM-ism that we are carrying since for DRM drivers made sense to use struct device.

Doing that would also allow get rid of indirections like the need of both a devm_acquire_aperture_of_platform_device() and devm_aperture_acquire() just to do a &pdev->dev.

And also some to_platform_device() in drm_aperture_detach_firmware() and detach_platform_device().

If we ever support non-platform devices then we can refactor it, but I don't think that is worth to complicate just in case we ever support struct device.

Quoted 7 lines

drivers/video/aperture.c line 130

Quoted 4 lines
drivers/video/aperture.c new file +4 -0
+	resource_size_t base;
+	resource_size_t size;
+	struct list_head lh;
+	void (*detach)(struct device *dev);

Same here, just void (*detach)(struct platform_device *pdev) if you agree with
that I mentioned above.

I took that code from DRM. No need to change it for less flexibility.

Quoted 16 lines

drivers/video/aperture.c line 140

Quoted 11 lines
drivers/video/aperture.c new file +10 -0
+};
+
+static LIST_HEAD(apertures);
+static DEFINE_MUTEX(apertures_lock);
+
+static bool overlap(resource_size_t base1, resource_size_t end1,
+		    resource_size_t base2, resource_size_t end2)
+{
+	return (base1 < end2) && (end1 > base2);
+}

There's a resource_overlaps() helper in include/linux/ioport.h, I wonder if it could just be used, maybe declaring and filling a struct resource just to call that helper. Later as an optimization a resource_range_overlap() or something could be proposed for include/linux/ioport.h.

Bu then we'd have to declare struct resource-es for using an interface. This helper is trivial. If anything, resource_overlaps() should be generalized.

Quoted 3 lines

Also, I noticed that resource_overlaps() uses <= and >= but this helper uses < and >. It seems there's an off-by-one error here but maybe I'm wrong on this.

struct resource stores the final byte of the resource. In our case 'end' is the byte after that. So the code is correct.

Do we ever have resources that end at the top-most byte of the address space?

Quoted 20 lines

[...]

drivers/video/aperture.c line 203

Quoted 12 lines
drivers/video/aperture.c new file +12 -0
+static void detach_platform_device(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+
+	/*
+	 * Remove the device from the device hierarchy. This is the right thing
+	 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
+	 * the new driver takes over the hardware, the firmware device's state
+	 * will be lost.
+	 *
+	 * For non-platform devices, a new callback would be required.
+	 *

I wonder if we ever are going to need this. AFAICT the problem only happens for platform devices. Or do you envision a case when some a bus could need this and the aperture unregister the device instead of the Linux kernel device model ?

In the current code, we clearly distinguish between the device and the platform device. The latter is only used in a few places where it's absolutely necessary, because there's no generic equivalent to platform_device_unregister(). (device_unregister() is something else AFAICT.) At some point, I'd like to see the aperture code being handled in a more prominent place within resource management. That would need it to use struct device.

Best regards
Thomas

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#4 lore raw

Re: [PATCH v2 1/2] drm: Implement DRM aperture helpers under video/

Hello Thomas,

On 6/21/22 13:29, Thomas Zimmermann wrote:

[...]

Quoted 12 lines
Quoted 11 lines

drivers/video/aperture.c line 140

Quoted 6 lines
drivers/video/aperture.c new file +6 -0
+
+static bool overlap(resource_size_t base1, resource_size_t end1,
+		    resource_size_t base2, resource_size_t end2)
+{
+	return (base1 < end2) && (end1 > base2);
+}

There's a resource_overlaps() helper in include/linux/ioport.h, I wonder if it could just be used, maybe declaring and filling a struct resource just to call that helper. Later as an optimization a resource_range_overlap() or something could be proposed for include/linux/ioport.h.

Quoted 4 lines

Bu then we'd have to declare struct resource-es for using an interface. This helper is trivial. If anything, resource_overlaps() should be generalized.

Yes, that works too. Probably then we should just keep as is and then as a follow up we can add another helper to include/linux/ioport.h to avoid having something that's only for the aperture helpers.

Quoted 10 lines
Quoted 3 lines

Also, I noticed that resource_overlaps() uses <= and >= but this helper uses < and >. It seems there's an off-by-one error here but maybe I'm wrong on this.

struct resource stores the final byte of the resource. In our case 'end' is the byte after that. So the code is correct.

Do we ever have resources that end at the top-most byte of the address space?

I don't know to be honest.

[...]

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
drivers/video/aperture.c new file +8 -0
+static void detach_platform_device(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+
+	/*
+	 * Remove the device from the device hierarchy. This is the right thing
+	 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
+	 * the new driver takes over the hardware, the firmware device's state
Quoted 9 lines

drivers/video/aperture.c line 203

Quoted 4 lines
drivers/video/aperture.c new file +4 -0
+	 * will be lost.
+	 *
+	 * For non-platform devices, a new callback would be required.
+	 *

I wonder if we ever are going to need this. AFAICT the problem only happens for platform devices. Or do you envision a case when some a bus could need this and the aperture unregister the device instead of the Linux kernel device model ?

Quoted 8 lines

In the current code, we clearly distinguish between the device and the platform device. The latter is only used in a few places where it's absolutely necessary, because there's no generic equivalent to platform_device_unregister(). (device_unregister() is something else AFAICT.) At some point, I'd like to see the aperture code being handled in a more prominent place within resource management. That would need it to use struct device.

Ok, I was wondering what was the value of the indirection level other than making the code more complex and supporting an hypothetical case of a FW driver that would not bind against a platform device.

But if the goal is to move this at some point to a more generic place (i.e:
the Linux device model itself) then I agree that we can just keep it as is.

When you re-spin this patch, feel free to add my R-b since looks good to me.

And thanks again for working on this!

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
#5 lore raw

PATCH v2 2/2 vfio/pci: Remove console drivers

In reply to Alex Williamson

Console drivers can create conflicts with PCI resources resulting in userspace getting mmap failures to memory BARs. This is especially evident when trying to re-use the system primary console for userspace drivers. Use the aperture helpers to remove these conflicts.

---
 drivers/vfio/pci/vfio_pci_core.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +5 -0
index a0d69ddaf90d..5b2a6e9f7cf7 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/aperture.h>
 #include <linux/device.h>
 #include <linux/eventfd.h>
 #include <linux/file.h>
@@ -1793,6 +1794,10 @@ static int vfio_pci_vga_init(struct vfio_pci_core_device *vdev)
 	if (!vfio_pci_is_vga(pdev))
 		return 0;
 
+	ret = remove_conflicting_pci_devices(pdev, vdev->vdev.ops->name);
+	if (ret)
+		return ret;
+
 	ret = vga_client_register(pdev, vfio_pci_set_decode);
 	if (ret)
 		return ret;
maintainer Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] vfio/pci: Remove console drivers

Hello Alex,

Quoted 9 lines · Alex Williamson

Console drivers can create conflicts with PCI resources resulting in userspace getting mmap failures to memory BARs. This is especially evident when trying to re-use the system primary console for userspace drivers. Use the aperture helpers to remove these conflicts.

---

Patch looks good to me.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Linux Engineering
Red Hat
Tested-by on the v2 series
#7 lore raw
In reply to Alex Williamson
Quoted 35 lines · Alex Williamson

When assigning a primary graphics device to VM through vfio-pci device assignment, users often prevent binding of the native PCI graphics driver to avoid device initialization conflicts, however firmware console drivers may still be attached to the device which can often be cumbersome to manually unbind or exclude via cmdline options.

This series proposes to move the DRM aperture helpers out to drivers/video/ to make it more accessible to drivers like vfio-pci, which have neither dependencies on DRM code nor a struct drm_driver to present to existing interfaces. vfio-pci can then trivially call into the aperture helpers to remove conflicting drivers, rather than open coding it ourselves as was proposed with a new symbol export in v1 of this series[1].

Thanks to Thomas for splitting out the aperture code with new documentation.

Thomas had proposed this going through the vfio tree with appropriate stakeholder acks, that's fine with me, but I'm also open to it going through the DRM tree given that the vfio-pci-core change is even more trivial now and the bulk of the changes are DRM/video paths. Thanks,

Alex

[1]https://lore.kernel.org/all/165453797543.3592816.6381793341352595461.stgit@omen/

---

Alex Williamson (1):
vfio/pci: Remove console drivers

Thomas Zimmermann (1):
drm: Implement DRM aperture helpers under video/

Quoted 16 lines
 Documentation/driver-api/aperture.rst |  13 +
 Documentation/driver-api/index.rst    |   1 +
 drivers/gpu/drm/drm_aperture.c        | 174 +------------
 drivers/gpu/drm/tiny/Kconfig          |   1 +
 drivers/vfio/pci/vfio_pci_core.c      |   5 +
 drivers/video/Kconfig                 |   6 +
 drivers/video/Makefile                |   2 +
 drivers/video/aperture.c              | 340 ++++++++++++++++++++++++++
 drivers/video/console/Kconfig         |   1 +
 drivers/video/fbdev/Kconfig           |   7 +-
 include/linux/aperture.h              |  56 +++++
 11 files changed, 440 insertions(+), 166 deletions(-)
 create mode 100644 Documentation/driver-api/aperture.rst
 create mode 100644 drivers/video/aperture.c
 create mode 100644 include/linux/aperture.h

series

(on top of Fedora's 5.18.5-100.fc35.x86_64)

Thanks,
Laszlo