| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
 Re: [PATCH v5 05/10] drm/ttm: Add vmap/vunmap to TTM and TTM GEM helpers
 
To: Thomas Zimmermann <tzimmermann@xxxxxxx>, maarten.lankhorst@xxxxxxxxxxxxxxx, mripard@xxxxxxxxxx, airlied@xxxxxxxx, daniel@xxxxxxxx, sam@xxxxxxxxxxxx, alexander.deucher@xxxxxxx, kraxel@xxxxxxxxxx, l.stach@xxxxxxxxxxxxxx, linux+etnaviv@xxxxxxxxxxxxxxx, christian.gmeiner@xxxxxxxxx, inki.dae@xxxxxxxxxxx, jy0922.shim@xxxxxxxxxxx, sw0312.kim@xxxxxxxxxxx, kyungmin.park@xxxxxxxxxxx, kgene@xxxxxxxxxx, krzk@xxxxxxxxxx, yuq825@xxxxxxxxx, bskeggs@xxxxxxxxxx, robh@xxxxxxxxxx, tomeu.vizoso@xxxxxxxxxxxxx, steven.price@xxxxxxx, alyssa.rosenzweig@xxxxxxxxxxxxx, hjc@xxxxxxxxxxxxxx, heiko@xxxxxxxxx, hdegoede@xxxxxxxxxx, sean@xxxxxxxxxx, eric@xxxxxxxxxx, oleksandr_andrushchenko@xxxxxxxx, ray.huang@xxxxxxx, sumit.semwal@xxxxxxxxxx, emil.velikov@xxxxxxxxxxxxx, luben.tuikov@xxxxxxx, apaneers@xxxxxxx, linus.walleij@xxxxxxxxxx, melissa.srw@xxxxxxxxx, chris@xxxxxxxxxxxxxxxxxx, miaoqinglang@xxxxxxxxxxFrom: Christian König <christian.koenig@xxxxxxx>Date: Tue, 20 Oct 2020 15:39:35 +0200Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=noneArc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3cV8pVoP5z/fZz9XlAM66A4AVF1LwjqpED/6cqT1bI0=; b=CF240AmTPyDLWPLu3UPZ6ztQkl+vBNIRlafeSeiWU99XWWG8GF5w2+fn/m4TTecGNkyMLU34gotif7/5R8w4R1njtniZMmi6FeBNphabS+mrI3Uolm4ZrK/v9CglqjI/Gd9VYAacbdtWsyagqRdAwHmtlAnhdsd8TrqaJgfJTZmKRM2LqfaXmJphJ4F5AcSJr1EF+cuIpUux7r4TkPiV8/Ah0CoCkZPr9LeSktuSSkvIKoWhV2HzTRoBx0+hX8Hv/qd1T7wecnXYeIs65OJXv8B8rj409hoZ9oE/Khx2hT1q+1pF8wvVQwHqaJagXhEqGkgDmsJpNxaPz9scK07yiQ==Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eNLmLQEq2ogaJb89Y7a0DoyHU1dpu0akxG+g6Lj3M1spJtdg4xUXT5u3DWnog8xuhb4mHcDe4B5eFCGXwk7BJwkmsteVPmtz8Xy14cT6hiyaef0+Ih8M+CI7gxcp/vigxscIeESdvtqC/NiItwG6DJMLpl8iEU/1XU9puM/RCi+lRd0+TESk+HHMTxv3IcLZgaABA7GUj6CnDSJ5br8UhIz/+61FlhwrTRGmp0qD85QP/DCwd27VGkvji07Lf4kuzGv6aYDSDXX7cX7DjTct/gVg7/gwYUPM4KLrnUOH+4PNEcqO6EeHf0HylBajJD3a2uWq3SQGlQWX/usw5Kgmww==Authentication-results: ffwll.ch; dkim=none (message not signed) header.d=none;ffwll.ch; dmarc=none action=none header.from=amd.com;Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx, amd-gfx@xxxxxxxxxxxxxxxxxxxxx, virtualization@xxxxxxxxxxxxxxxxxxxxxxxxxx, etnaviv@xxxxxxxxxxxxxxxxxxxxx, linux-arm-kernel@xxxxxxxxxxxxxxxxxxx, linux-samsung-soc@xxxxxxxxxxxxxxx, lima@xxxxxxxxxxxxxxxxxxxxx, nouveau@xxxxxxxxxxxxxxxxxxxxx, spice-devel@xxxxxxxxxxxxxxxxxxxxx, linux-rockchip@xxxxxxxxxxxxxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx, linux-media@xxxxxxxxxxxxxxx, linaro-mm-sig@xxxxxxxxxxxxxxxx, Daniel Vetter <daniel.vetter@xxxxxxxx>Delivery-date: Tue, 20 Oct 2020 13:40:01 +0000List-id: Xen developer discussion <xen-devel.lists.xenproject.org> 
 
Am 20.10.20 um 14:20 schrieb Thomas Zimmermann:
 
The new functions ttm_bo_{vmap,vunmap}() map and unmap a TTM BO in kernel
address space. The mapping's address is returned as struct dma_buf_map.
Each function is a simplified version of TTM's existing kmap code. Both
functions respect the memory's location ani/or writecombine flags.
On top TTM's functions, GEM TTM helpers got drm_gem_ttm_{vmap,vunmap}(),
two helpers that convert a GEM object into the TTM BO and forward the call
to TTM's vmap/vunmap. These helpers can be dropped into the rsp GEM object
callbacks.
v5:
        * use size_t for storing mapping size (Christian)
        * ignore premapped memory areas correctly in ttm_bo_vunmap()
        * rebase onto latest TTM interfaces (Christian)
        * remove BUG() from ttm_bo_vmap() (Christian)
v4:
        * drop ttm_kmap_obj_to_dma_buf() in favor of vmap helpers (Daniel,
          Christian)
Signed-off-by: Thomas Zimmermann <tzimmermann@xxxxxxx>
Acked-by: Daniel Vetter <daniel.vetter@xxxxxxxx>
Tested-by: Sam Ravnborg <sam@xxxxxxxxxxxx>
 
Reviewed-by: Christian König <christian.koenig@xxxxxxx>
 
---
  drivers/gpu/drm/drm_gem_ttm_helper.c | 38 +++++++++++++++
  drivers/gpu/drm/ttm/ttm_bo_util.c    | 72 ++++++++++++++++++++++++++++
  include/drm/drm_gem_ttm_helper.h     |  6 +++
  include/drm/ttm/ttm_bo_api.h         | 28 +++++++++++
  include/linux/dma-buf-map.h          | 20 ++++++++
  5 files changed, 164 insertions(+)
diff --git a/drivers/gpu/drm/drm_gem_ttm_helper.c 
b/drivers/gpu/drm/drm_gem_ttm_helper.c
index 0e4fb9ba43ad..db4c14d78a30 100644
--- a/drivers/gpu/drm/drm_gem_ttm_helper.c
+++ b/drivers/gpu/drm/drm_gem_ttm_helper.c
@@ -49,6 +49,44 @@ void drm_gem_ttm_print_info(struct drm_printer *p, unsigned 
int indent,
  }
  EXPORT_SYMBOL(drm_gem_ttm_print_info);
+/** 
+ * drm_gem_ttm_vmap() - vmap &ttm_buffer_object
+ * @gem: GEM object.
+ * @map: [out] returns the dma-buf mapping.
+ *
+ * Maps a GEM object with ttm_bo_vmap(). This function can be used as
+ * &drm_gem_object_funcs.vmap callback.
+ *
+ * Returns:
+ * 0 on success, or a negative errno code otherwise.
+ */
+int drm_gem_ttm_vmap(struct drm_gem_object *gem,
+                    struct dma_buf_map *map)
+{
+       struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
+
+       return ttm_bo_vmap(bo, map);
+
+}
+EXPORT_SYMBOL(drm_gem_ttm_vmap);
+
+/**
+ * drm_gem_ttm_vunmap() - vunmap &ttm_buffer_object
+ * @gem: GEM object.
+ * @map: dma-buf mapping.
+ *
+ * Unmaps a GEM object with ttm_bo_vunmap(). This function can be used as
+ * &drm_gem_object_funcs.vmap callback.
+ */
+void drm_gem_ttm_vunmap(struct drm_gem_object *gem,
+                       struct dma_buf_map *map)
+{
+       struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
+
+       ttm_bo_vunmap(bo, map);
+}
+EXPORT_SYMBOL(drm_gem_ttm_vunmap);
+
  /**
   * drm_gem_ttm_mmap() - mmap &ttm_buffer_object
   * @gem: GEM object.
diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c 
b/drivers/gpu/drm/ttm/ttm_bo_util.c
index ba7ab5ed85d0..5c79418405ea 100644
--- a/drivers/gpu/drm/ttm/ttm_bo_util.c
+++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
@@ -32,6 +32,7 @@
  #include <drm/ttm/ttm_bo_driver.h>
  #include <drm/ttm/ttm_placement.h>
  #include <drm/drm_vma_manager.h>
+#include <linux/dma-buf-map.h>
  #include <linux/io.h>
  #include <linux/highmem.h>
  #include <linux/wait.h>
@@ -527,6 +528,77 @@ void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  }
  EXPORT_SYMBOL(ttm_bo_kunmap);
+int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map)
+{
+       struct ttm_resource *mem = &bo->mem;
+       int ret;
+
+       ret = ttm_mem_io_reserve(bo->bdev, mem);
+       if (ret)
+               return ret;
+
+       if (mem->bus.is_iomem) {
+               void __iomem *vaddr_iomem;
+               size_t size = bo->num_pages << PAGE_SHIFT;
+
+               if (mem->bus.addr)
+                       vaddr_iomem = (void __iomem *)mem->bus.addr;
+               else if (mem->bus.caching == ttm_write_combined)
+                       vaddr_iomem = ioremap_wc(mem->bus.offset, size);
+               else
+                       vaddr_iomem = ioremap(mem->bus.offset, size);
+
+               if (!vaddr_iomem)
+                       return -ENOMEM;
+
+               dma_buf_map_set_vaddr_iomem(map, vaddr_iomem);
+
+       } else {
+               struct ttm_operation_ctx ctx = {
+                       .interruptible = false,
+                       .no_wait_gpu = false
+               };
+               struct ttm_tt *ttm = bo->ttm;
+               pgprot_t prot;
+               void *vaddr;
+
+               ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
+               if (ret)
+                       return ret;
+
+               /*
+                * We need to use vmap to get the desired page protection
+                * or to make the buffer object look contiguous.
+                */
+               prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
+               vaddr = vmap(ttm->pages, bo->num_pages, 0, prot);
+               if (!vaddr)
+                       return -ENOMEM;
+
+               dma_buf_map_set_vaddr(map, vaddr);
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(ttm_bo_vmap);
+
+void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map)
+{
+       struct ttm_resource *mem = &bo->mem;
+
+       if (dma_buf_map_is_null(map))
+               return;
+
+       if (!map->is_iomem)
+               vunmap(map->vaddr);
+       else if (!mem->bus.addr)
+               iounmap(map->vaddr_iomem);
+       dma_buf_map_clear(map);
+
+       ttm_mem_io_free(bo->bdev, &bo->mem);
+}
+EXPORT_SYMBOL(ttm_bo_vunmap);
+
  static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
                                 bool dst_use_tt)
  {
diff --git a/include/drm/drm_gem_ttm_helper.h b/include/drm/drm_gem_ttm_helper.h
index 118cef76f84f..7c6d874910b8 100644
--- a/include/drm/drm_gem_ttm_helper.h
+++ b/include/drm/drm_gem_ttm_helper.h
@@ -10,11 +10,17 @@
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
+struct dma_buf_map;
+
  #define drm_gem_ttm_of_gem(gem_obj) \
        container_of(gem_obj, struct ttm_buffer_object, base)
void drm_gem_ttm_print_info(struct drm_printer *p, unsigned int indent,
                            const struct drm_gem_object *gem);
+int drm_gem_ttm_vmap(struct drm_gem_object *gem,
+                    struct dma_buf_map *map);
+void drm_gem_ttm_vunmap(struct drm_gem_object *gem,
+                       struct dma_buf_map *map);
  int drm_gem_ttm_mmap(struct drm_gem_object *gem,
                     struct vm_area_struct *vma);
diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
index 37102e45e496..2c59a785374c 100644
--- a/include/drm/ttm/ttm_bo_api.h
+++ b/include/drm/ttm/ttm_bo_api.h
@@ -48,6 +48,8 @@ struct ttm_bo_global;
struct ttm_bo_device;
  
+struct dma_buf_map; 
+
  struct drm_mm_node;
struct ttm_placement; 
@@ -494,6 +496,32 @@ int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned 
long start_page,
   */
  void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
+/** 
+ * ttm_bo_vmap
+ *
+ * @bo: The buffer object.
+ * @map: pointer to a struct dma_buf_map representing the map.
+ *
+ * Sets up a kernel virtual mapping, using ioremap or vmap to the
+ * data in the buffer object. The parameter @map returns the virtual
+ * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
+ *
+ * Returns
+ * -ENOMEM: Out of memory.
+ * -EINVAL: Invalid range.
+ */
+int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
+
+/**
+ * ttm_bo_vunmap
+ *
+ * @bo: The buffer object.
+ * @map: Object describing the map to unmap.
+ *
+ * Unmaps a kernel map set up by ttm_bo_vmap().
+ */
+void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
+
  /**
   * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
   *
diff --git a/include/linux/dma-buf-map.h b/include/linux/dma-buf-map.h
index fd1aba545fdf..2e8bbecb5091 100644
--- a/include/linux/dma-buf-map.h
+++ b/include/linux/dma-buf-map.h
@@ -45,6 +45,12 @@
   *
   *    dma_buf_map_set_vaddr(&map. 0xdeadbeaf);
   *
+ * To set an address in I/O memory, use dma_buf_map_set_vaddr_iomem().
+ *
+ * .. code-block:: c
+ *
+ *     dma_buf_map_set_vaddr_iomem(&map. 0xdeadbeaf);
+ *
   * Test if a mapping is valid with either dma_buf_map_is_set() or
   * dma_buf_map_is_null().
   *
@@ -118,6 +124,20 @@ static inline void dma_buf_map_set_vaddr(struct 
dma_buf_map *map, void *vaddr)
        map->is_iomem = false;
  }
+/**
+ * dma_buf_map_set_vaddr_iomem - Sets a dma-buf mapping structure to an 
address in I/O memory
+ * @map:               The dma-buf mapping structure
+ * @vaddr_iomem:       An I/O-memory address
+ *
+ * Sets the address and the I/O-memory flag.
+ */
+static inline void dma_buf_map_set_vaddr_iomem(struct dma_buf_map *map,
+                                              void __iomem *vaddr_iomem)
+{
+       map->vaddr_iomem = vaddr_iomem;
+       map->is_iomem = true;
+}
+
  /**
   * dma_buf_map_is_equal - Compares two dma-buf mapping structures for equality
   * @lhs:      The dma-buf mapping structure
 
 
 |