drm: Add simple PRIME helpers for GEM VRAM
authorThomas Zimmermann <tzimmermann@suse.de>
Wed, 8 May 2019 08:26:15 +0000 (10:26 +0200)
committerGerd Hoffmann <kraxel@redhat.com>
Wed, 15 May 2019 14:17:06 +0000 (16:17 +0200)
These basic helper functions for GEM VRAM allow for pinning and mapping
GEM VRAM objects via the PRIME interfaces. It's not a full implementation,
but complete enough for generic fbcon.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Link: http://patchwork.freedesktop.org/patch/msgid/20190508082630.15116-6-tzimmermann@suse.de
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
drivers/gpu/drm/drm_gem_vram_helper.c
include/drm/drm_gem_vram_helper.h

index aba073279ca23bd8ba908742ddbbe774e96bafcf..15c6193e4985d934613e668dcb771b5d3d466629 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <drm/drm_gem_vram_helper.h>
 #include <drm/drm_mode.h>
+#include <drm/drm_prime.h>
 #include <drm/ttm/ttm_page_alloc.h>
 
 /**
@@ -571,3 +572,100 @@ int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
        return 0;
 }
 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_mmap_offset);
+
+/*
+ * PRIME helpers for struct drm_driver
+ */
+
+/**
+ * drm_gem_vram_driver_gem_prime_pin() - \
+       Implements &struct drm_driver.gem_prime_pin
+ * @gem:       The GEM object to pin
+ *
+ * Returns:
+ * 0 on success, or
+ * a negative errno code otherwise.
+ */
+int drm_gem_vram_driver_gem_prime_pin(struct drm_gem_object *gem)
+{
+       struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
+
+       return drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
+}
+EXPORT_SYMBOL(drm_gem_vram_driver_gem_prime_pin);
+
+/**
+ * drm_gem_vram_driver_gem_prime_unpin() - \
+       Implements &struct drm_driver.gem_prime_unpin
+ * @gem:       The GEM object to unpin
+ */
+void drm_gem_vram_driver_gem_prime_unpin(struct drm_gem_object *gem)
+{
+       struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
+
+       drm_gem_vram_unpin(gbo);
+}
+EXPORT_SYMBOL(drm_gem_vram_driver_gem_prime_unpin);
+
+/**
+ * drm_gem_vram_driver_gem_prime_vmap() - \
+       Implements &struct drm_driver.gem_prime_vmap
+ * @gem:       The GEM object to map
+ *
+ * Returns:
+ * The buffers virtual address on success, or
+ * NULL otherwise.
+ */
+void *drm_gem_vram_driver_gem_prime_vmap(struct drm_gem_object *gem)
+{
+       struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
+       int ret;
+       void *base;
+
+       ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
+       if (ret)
+               return NULL;
+       base = drm_gem_vram_kmap(gbo, true, NULL);
+       if (IS_ERR(base)) {
+               drm_gem_vram_unpin(gbo);
+               return NULL;
+       }
+       return base;
+}
+EXPORT_SYMBOL(drm_gem_vram_driver_gem_prime_vmap);
+
+/**
+ * drm_gem_vram_driver_gem_prime_vunmap() - \
+       Implements &struct drm_driver.gem_prime_vunmap
+ * @gem:       The GEM object to unmap
+ * @vaddr:     The mapping's base address
+ */
+void drm_gem_vram_driver_gem_prime_vunmap(struct drm_gem_object *gem,
+                                         void *vaddr)
+{
+       struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
+
+       drm_gem_vram_kunmap(gbo);
+       drm_gem_vram_unpin(gbo);
+}
+EXPORT_SYMBOL(drm_gem_vram_driver_gem_prime_vunmap);
+
+/**
+ * drm_gem_vram_driver_gem_prime_mmap() - \
+       Implements &struct drm_driver.gem_prime_mmap
+ * @gem:       The GEM object to map
+ * @vma:       The VMA describing the mapping
+ *
+ * Returns:
+ * 0 on success, or
+ * a negative errno code otherwise.
+ */
+int drm_gem_vram_driver_gem_prime_mmap(struct drm_gem_object *gem,
+                                      struct vm_area_struct *vma)
+{
+       struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
+
+       gbo->gem.vma_node.vm_node.start = gbo->bo.vma_node.vm_node.start;
+       return drm_gem_prime_mmap(gem, vma);
+}
+EXPORT_SYMBOL(drm_gem_vram_driver_gem_prime_mmap);
index 8e54f721c9f8d27f84dcb6d81a3b3999285dead5..9a8bd98b623f9664ba7770d1fdecfd5eb64fb519 100644 (file)
@@ -116,5 +116,25 @@ void drm_gem_vram_driver_gem_free_object_unlocked(struct drm_gem_object *gem);
 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
                                         struct drm_device *dev,
                                         uint32_t handle, uint64_t *offset);
+/*
+ * PRIME helpers for struct drm_driver
+ */
+
+int drm_gem_vram_driver_gem_prime_pin(struct drm_gem_object *obj);
+void drm_gem_vram_driver_gem_prime_unpin(struct drm_gem_object *obj);
+void *drm_gem_vram_driver_gem_prime_vmap(struct drm_gem_object *obj);
+void drm_gem_vram_driver_gem_prime_vunmap(struct drm_gem_object *obj,
+                                         void *vaddr);
+int drm_gem_vram_driver_gem_prime_mmap(struct drm_gem_object *obj,
+                                      struct vm_area_struct *vma);
+
+#define DRM_GEM_VRAM_DRIVER_PRIME \
+       .gem_prime_export = drm_gem_prime_export, \
+       .gem_prime_import = drm_gem_prime_import, \
+       .gem_prime_pin    = drm_gem_vram_driver_gem_prime_pin, \
+       .gem_prime_unpin  = drm_gem_vram_driver_gem_prime_unpin, \
+       .gem_prime_vmap   = drm_gem_vram_driver_gem_prime_vmap, \
+       .gem_prime_vunmap = drm_gem_vram_driver_gem_prime_vunmap, \
+       .gem_prime_mmap   = drm_gem_vram_driver_gem_prime_mmap
 
 #endif