drm: Use USB controller's DMA mask when importing dmabufs
Subsystems (7)
Thomas Zimmermann
maintainerPATCH v8 1/1 drm: Use USB controller's DMA mask when importing dmabufs
USB devices cannot perform DMA and hence have no dma_mask set in their device structure. Therefore importing dmabuf into a USB-based driver fails, which breaks joining and mirroring of display in X11.
For USB devices, pick the associated USB controller as attachment device. This allows the DRM import helpers to perform the DMA setup. If the DMA controller does not support DMA transfers, we're out of luck and cannot import. Our current USB-based DRM drivers don't use DMA, so the actual DMA device is not important.
Tested by joining/mirroring displays of udl and radeon under Gnome/X11.
v8:
* release dmadev if device initialization fails (Noralf)
* fix commit description (Noralf)
v7:
* fix use-before-init bug in gm12u320 (Dan)
v6:
* implement workaround in DRM drivers and hold reference to DMA device while USB device is in use
* remove dev_is_usb() (Greg)
* collapse USB helper into usb_intf_get_dma_device() (Alan)
* integrate Daniel's TODO statement (Daniel)
* fix typos (Greg)
v5:
* provide a helper for USB interfaces (Alan)
* add FIXME item to documentation and TODO list (Daniel)
v4:
* implement workaround with USB helper functions (Greg)
* use struct usb_device->bus->sysdev as DMA device (Takashi)
v3:
* drop gem_create_object
* use DMA mask of USB controller, if any (Daniel, Christian, Noralf)
v2:
* move fix to importer side (Christian, Daniel)
* update SHMEM and CMA helpers for new PRIME callbacks
--- Documentation/gpu/todo.rst | 21 ++++++++++++++++ drivers/gpu/drm/tiny/gm12u320.c | 44 +++++++++++++++++++++++++++------ drivers/gpu/drm/udl/udl_drv.c | 17 +++++++++++++ drivers/gpu/drm/udl/udl_drv.h | 1 + drivers/gpu/drm/udl/udl_main.c | 10 ++++++++ drivers/usb/core/usb.c | 32 ++++++++++++++++++++++++ include/linux/usb.h | 2 ++ 7 files changed, 119 insertions(+), 8 deletions(-)
Documentation/gpu/todo.rst +21 -0
@@ -571,6 +571,27 @@ Contact: Daniel Vetter Level: Intermediate +Remove automatic page mapping from dma-buf importing +---------------------------------------------------- + +When importing dma-bufs, the dma-buf and PRIME frameworks automatically map +imported pages into the importer's DMA area. drm_gem_prime_fd_to_handle() and +drm_gem_prime_handle_to_fd() require that importers call dma_buf_attach() +even if they never do actual device DMA, but only CPU access through +dma_buf_vmap(). This is a problem for USB devices, which do not support DMA +operations. + +To fix the issue, automatic page mappings should be removed from the +buffer-sharing code. Fixing this is a bit more involved, since the import/export +cache is also tied to &drm_gem_object.import_attach. Meanwhile we paper over +this problem for USB devices by fishing out the USB host controller device, as +long as that supports DMA. Otherwise importing can still needlessly fail. + +Contact: Thomas Zimmermann <[email protected]>, Daniel Vetter + +Level: Advanced + + Better Testing ==============
drivers/gpu/drm/tiny/gm12u320.c +36 -8
108 lines not on this page · raw message
drivers/gpu/drm/udl/udl_drv.c +17 -0
@@ -32,6 +32,22 @@ static int udl_usb_resume(struct usb_interface *interface)return drm_mode_config_helper_resume (dev); } +/* +* FIXME: Dma-buf sharing requires DMA support by the importing device. +* This function is a workaround to make USB devices work as well. +* See todo.rst for how to fix the issue in the dma-buf framework. +*/ +static struct drm_gem_object *udl_driver_gem_prime_import (struct drm_device *dev, +struct dma_buf *dma_buf) +{ +struct udl_device *udl =to_udl (dev); + +if (!udl->dmadev) +return ERR_PTR (-ENODEV ); + +return drm_gem_prime_import_dev (dev, dma_buf, udl->dmadev); +} +DEFINE_DRM_GEM_FOPS (udl_driver_fops);static const struct drm_driver driver = {
@@ -40,6 +56,7 @@ static const struct drm_driver driver = {/* GEM hooks */ .fops = &udl_driver_fops,DRM_GEM_SHMEM_DRIVER_OPS , + .gem_prime_import = udl_driver_gem_prime_import, .name = DRIVER_NAME, .desc = DRIVER_DESC,
drivers/gpu/drm/udl/udl_drv.h +1 -0
@@ -50,6 +50,7 @@ struct urb_list {struct udl_device {struct drm_device drm;struct device *dev; +struct device *dmadev;struct drm_simple_display_pipe display_pipe;
drivers/gpu/drm/udl/udl_main.c +10 -0
@@ -315,6 +315,10 @@ int udl_init(struct udl_device *udl)DRM_DEBUG ("\n" ); + udl->dmadev =usb_intf_get_dma_device (to_usb_interface (dev->dev)); +if (!udl->dmadev) +drm_warn (dev,"buffer sharing not supported" );/* not an error */ +mutex_init (&udl->gem_lock);if (!udl_parse_vendor_descriptor (udl)) {
@@ -343,12 +347,18 @@ int udl_init(struct udl_device *udl) err:if (udl->urbs.count)udl_free_urb_list (dev); +put_device (udl->dmadev);DRM_ERROR ("%d\n" , ret);return ret; }int udl_drop_usb (struct drm_device *dev) { +struct udl_device *udl =to_udl (dev); +udl_free_urb_list (dev); +put_device (udl->dmadev); + udl->dmadev =NULL ; +return 0 ; }
drivers/usb/core/usb.c +32 -0
@@ -748,6 +748,38 @@ void usb_put_intf(struct usb_interface *intf) }EXPORT_SYMBOL_GPL (usb_put_intf); +/** +* usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint +* @intf: the usb interface +* +* While a USB device cannot perform DMA operations by itself, many USB +* controllers can. A call to usb_intf_get_dma_device() returns the DMA endpoint +* for the given USB interface, if any. The returned device structure must be +* released with put_device(). +* +* See also usb_get_dma_device(). +* +* Returns: A reference to the usb interface's DMA endpoint; or NULL if none +* exists. +*/ +struct device *usb_intf_get_dma_device (struct usb_interface *intf) +{ +struct usb_device *udev =interface_to_usbdev (intf); +struct device *dmadev; + +if (!udev->bus) +return NULL ; + + dmadev =get_device (udev->bus->sysdev); +if (!dmadev || !dmadev->dma_mask) { +put_device (dmadev); +return NULL ; + } + +return dmadev; +} +EXPORT_SYMBOL_GPL (usb_intf_get_dma_device); +/* USB device locking * * USB devices and interfaces are locked using the semaphore in their
include/linux/usb.h +2 -0
@@ -746,6 +746,8 @@ extern int usb_lock_device_for_reset(struct usb_device *udev,extern int usb_reset_device (struct usb_device *dev);extern void usb_queue_reset_device (struct usb_interface *dev); +extern struct device *usb_intf_get_dma_device (struct usb_interface *intf); +#ifdef CONFIG_ACPI extern int usb_acpi_set_power_state (struct usb_device *hdev,int index,bool enable);
base-commit: b80182b9bffb51460e4a2ad7d96737ecd49126dc
prerequisite-patch-id: c2b2f08f0eccc9f5df0c0da49fa1d36267deb11d
Signature · 6 lines
2.30.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Den 03.03.2021 14.32, skrev Thomas Zimmermann:
Quoted 47 lines
USB devices cannot perform DMA and hence have no
dma_maskset in their device structure. Therefore importing dmabuf into a USB-based driver fails, which breaks joining and mirroring of display in X11.For USB devices, pick the associated USB controller as attachment device. This allows the DRM import helpers to perform the DMA setup. If the DMA controller does not support DMA transfers, we're out of luck and cannot import. Our current USB-based DRM drivers don't use DMA, so the actual DMA device is not important.
Tested by joining/mirroring displays of udl and radeon under Gnome/X11.
v8:
* release dmadev if device initialization fails (Noralf)
* fix commit description (Noralf)
v7:
* fix use-before-init bug in gm12u320 (Dan)
v6:
* implement workaround in DRM drivers and hold reference to DMA device while USB device is in use
* removedev_is_usb()(Greg)
* collapse USB helper intousb_intf_get_dma_device()(Alan)
* integrate Daniel's TODO statement (Daniel)
* fix typos (Greg)
v5:
* provide a helper for USB interfaces (Alan)
* add FIXME item to documentation and TODO list (Daniel)
v4:
* implement workaround with USB helper functions (Greg)
* use structusb_device->bus->sysdevas DMA device (Takashi)
v3:
* dropgem_create_object
* use DMA mask of USB controller, if any (Daniel, Christian, Noralf)
v2:
* move fix to importer side (Christian, Daniel)
* update SHMEM and CMA helpers for new PRIME callbacksSigned-off-by Thomas Zimmermann <[email protected]>Fixes 6eb0233ec2d0 ("usb: don't inherity DMA properties for USB devices")Tested-by Pavel Machek <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>Acked-by Christian König <[email protected]>Acked-by Daniel Vetter <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc <[email protected]> # v5.10+Signed-off-by Thomas Zimmermann <[email protected]>
Quoted 1 line
---
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel