drm: Add drm_prime.c
authorFrançois Tigeot <ftigeot@wolfpond.org>
Sat, 5 Jan 2019 22:28:31 +0000 (23:28 +0100)
committerFrançois Tigeot <ftigeot@wolfpond.org>
Sat, 5 Jan 2019 22:28:31 +0000 (23:28 +0100)
Increase dma-buf subsystem coverage.

sys/conf/files
sys/dev/drm/drm/Makefile
sys/dev/drm/drm_internal.h
sys/dev/drm/drm_prime.c [new file with mode: 0644]
sys/dev/drm/include/linux/dma-buf.h
sys/dev/drm/include/linux/dma-direction.h
sys/dev/drm/include/linux/fs.h
sys/dev/drm/include/linux/poll.h
sys/dev/drm/linux_dma-buf.c [copied from sys/dev/drm/include/linux/poll.h with 71% similarity]

index 387cd0f..02bb23a 100644 (file)
@@ -2268,6 +2268,7 @@ dev/drm/drm_modeset_lock.c                optional drm compile-with "${NORMAL_C} -include $S/d
 dev/drm/drm_panel.c                    optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/drm_pci.c                      optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/drm_plane_helper.c             optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
+dev/drm/drm_prime.c                    optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/drm_probe_helper.c             optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/drm_rect.c                     optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/drm_scatter.c                  optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
@@ -2278,6 +2279,7 @@ dev/drm/drm_vma_manager.c         optional drm compile-with "${NORMAL_C} -include $S/de
 dev/drm/linux_async.c                  optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/linux_compat.c                 optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/linux_dma.c                    optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
+dev/drm/linux_dma-buf.c                        optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/linux_hdmi.c                   optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/linux_hrtimer.c                        optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/linux_i2c.c                    optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
index d133240..769c488 100644 (file)
@@ -39,6 +39,7 @@ SRCS  = \
        drm_panel.c \
        drm_pci.c \
        drm_plane_helper.c \
+       drm_prime.c \
        drm_probe_helper.c \
        drm_rect.c \
        drm_scatter.c \
@@ -49,6 +50,7 @@ SRCS  = \
        linux_async.c \
        linux_compat.c \
        linux_dma.c \
+       linux_dma-buf.c \
        linux_hdmi.c \
        linux_i2c.c \
        linux_hrtimer.c \
index f58173b..daa272a 100644 (file)
@@ -41,7 +41,6 @@ int drm_getpciinfo(struct drm_device *dev, void *data,
 int drm_vma_info(struct seq_file *m, void *data);
 
 /* drm_prime.c */
-#if 0
 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
                                 struct drm_file *file_priv);
 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
@@ -51,7 +50,6 @@ void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
 void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
                                        struct dma_buf *dma_buf);
-#endif
 
 /* drm_info.c */
 int drm_name_info(struct seq_file *m, void *data);
diff --git a/sys/dev/drm/drm_prime.c b/sys/dev/drm/drm_prime.c
new file mode 100644 (file)
index 0000000..a0d9754
--- /dev/null
@@ -0,0 +1,795 @@
+/*
+ * Copyright © 2012 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *      Dave Airlie <airlied@redhat.com>
+ *      Rob Clark <rob.clark@linaro.org>
+ *
+ */
+
+#include <linux/export.h>
+#include <linux/dma-buf.h>
+#include <drm/drmP.h>
+#include <drm/drm_gem.h>
+
+#include "drm_internal.h"
+
+/*
+ * DMA-BUF/GEM Object references and lifetime overview:
+ *
+ * On the export the dma_buf holds a reference to the exporting GEM
+ * object. It takes this reference in handle_to_fd_ioctl, when it
+ * first calls .prime_export and stores the exporting GEM object in
+ * the dma_buf priv. This reference is released when the dma_buf
+ * object goes away in the driver .release function.
+ *
+ * On the import the importing GEM object holds a reference to the
+ * dma_buf (which in turn holds a ref to the exporting GEM object).
+ * It takes that reference in the fd_to_handle ioctl.
+ * It calls dma_buf_get, creates an attachment to it and stores the
+ * attachment in the GEM object. When this attachment is destroyed
+ * when the imported object is destroyed, we remove the attachment
+ * and drop the reference to the dma_buf.
+ *
+ * Thus the chain of references always flows in one direction
+ * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
+ *
+ * Self-importing: if userspace is using PRIME as a replacement for flink
+ * then it will get a fd->handle request for a GEM object that it created.
+ * Drivers should detect this situation and return back the gem object
+ * from the dma-buf private.  Prime will do this automatically for drivers that
+ * use the drm_gem_prime_{import,export} helpers.
+ */
+
+struct drm_prime_member {
+       struct list_head entry;
+       struct dma_buf *dma_buf;
+       uint32_t handle;
+};
+
+struct drm_prime_attachment {
+       struct sg_table *sgt;
+       enum dma_data_direction dir;
+};
+
+#if 0
+static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
+                                   struct dma_buf *dma_buf, uint32_t handle)
+{
+       struct drm_prime_member *member;
+
+       member = kmalloc(sizeof(*member), GFP_KERNEL);
+       if (!member)
+               return -ENOMEM;
+
+       get_dma_buf(dma_buf);
+       member->dma_buf = dma_buf;
+       member->handle = handle;
+       list_add(&member->entry, &prime_fpriv->head);
+       return 0;
+}
+
+static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
+                                                     uint32_t handle)
+{
+       struct drm_prime_member *member;
+
+       list_for_each_entry(member, &prime_fpriv->head, entry) {
+               if (member->handle == handle)
+                       return member->dma_buf;
+       }
+
+       return NULL;
+}
+
+static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
+                                      struct dma_buf *dma_buf,
+                                      uint32_t *handle)
+{
+       struct drm_prime_member *member;
+
+       list_for_each_entry(member, &prime_fpriv->head, entry) {
+               if (member->dma_buf == dma_buf) {
+                       *handle = member->handle;
+                       return 0;
+               }
+       }
+       return -ENOENT;
+}
+#endif
+
+static int drm_gem_map_attach(struct dma_buf *dma_buf,
+                             struct device *target_dev,
+                             struct dma_buf_attachment *attach)
+{
+       struct drm_prime_attachment *prime_attach;
+       struct drm_gem_object *obj = dma_buf->priv;
+       struct drm_device *dev = obj->dev;
+
+       prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
+       if (!prime_attach)
+               return -ENOMEM;
+
+       prime_attach->dir = DMA_NONE;
+       attach->priv = prime_attach;
+
+       if (!dev->driver->gem_prime_pin)
+               return 0;
+
+       return dev->driver->gem_prime_pin(obj);
+}
+
+static void drm_gem_map_detach(struct dma_buf *dma_buf,
+                              struct dma_buf_attachment *attach)
+{
+       struct drm_prime_attachment *prime_attach = attach->priv;
+       struct drm_gem_object *obj = dma_buf->priv;
+       struct drm_device *dev = obj->dev;
+       struct sg_table *sgt;
+
+       if (dev->driver->gem_prime_unpin)
+               dev->driver->gem_prime_unpin(obj);
+
+       if (!prime_attach)
+               return;
+
+       sgt = prime_attach->sgt;
+       if (sgt) {
+               if (prime_attach->dir != DMA_NONE)
+                       dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
+                                       prime_attach->dir);
+               sg_free_table(sgt);
+       }
+
+       kfree(sgt);
+       kfree(prime_attach);
+       attach->priv = NULL;
+}
+
+#if 0
+void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
+                                       struct dma_buf *dma_buf)
+{
+       struct drm_prime_member *member, *safe;
+
+       list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
+               if (member->dma_buf == dma_buf) {
+                       dma_buf_put(dma_buf);
+                       list_del(&member->entry);
+                       kfree(member);
+               }
+       }
+}
+#endif
+
+static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
+                                           enum dma_data_direction dir)
+{
+       struct drm_prime_attachment *prime_attach = attach->priv;
+       struct drm_gem_object *obj = attach->dmabuf->priv;
+       struct sg_table *sgt;
+
+       if (WARN_ON(dir == DMA_NONE || !prime_attach))
+               return ERR_PTR(-EINVAL);
+
+       /* return the cached mapping when possible */
+       if (prime_attach->dir == dir)
+               return prime_attach->sgt;
+
+       /*
+        * two mappings with different directions for the same attachment are
+        * not allowed
+        */
+       if (WARN_ON(prime_attach->dir != DMA_NONE))
+               return ERR_PTR(-EBUSY);
+
+       sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
+
+       if (!IS_ERR(sgt)) {
+               if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
+                       sg_free_table(sgt);
+                       kfree(sgt);
+                       sgt = ERR_PTR(-ENOMEM);
+               } else {
+                       prime_attach->sgt = sgt;
+                       prime_attach->dir = dir;
+               }
+       }
+
+       return sgt;
+}
+
+static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
+                                 struct sg_table *sgt,
+                                 enum dma_data_direction dir)
+{
+       /* nothing to be done here */
+}
+
+/**
+ * drm_gem_dmabuf_release - dma_buf release implementation for GEM
+ * @dma_buf: buffer to be released
+ *
+ * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
+ * must use this in their dma_buf ops structure as the release callback.
+ */
+void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
+{
+       struct drm_gem_object *obj = dma_buf->priv;
+
+       /* drop the reference on the export fd holds */
+       drm_gem_object_unreference_unlocked(obj);
+}
+EXPORT_SYMBOL(drm_gem_dmabuf_release);
+
+static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
+{
+       struct drm_gem_object *obj = dma_buf->priv;
+       struct drm_device *dev = obj->dev;
+
+       return dev->driver->gem_prime_vmap(obj);
+}
+
+static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
+{
+       struct drm_gem_object *obj = dma_buf->priv;
+       struct drm_device *dev = obj->dev;
+
+       dev->driver->gem_prime_vunmap(obj, vaddr);
+}
+
+static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
+                                       unsigned long page_num)
+{
+       return NULL;
+}
+
+static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
+                                        unsigned long page_num, void *addr)
+{
+
+}
+static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
+                                unsigned long page_num)
+{
+       return NULL;
+}
+
+static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
+                                 unsigned long page_num, void *addr)
+{
+
+}
+
+static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
+                              struct vm_area_struct *vma)
+{
+       struct drm_gem_object *obj = dma_buf->priv;
+       struct drm_device *dev = obj->dev;
+
+       if (!dev->driver->gem_prime_mmap)
+               return -ENOSYS;
+
+       return dev->driver->gem_prime_mmap(obj, vma);
+}
+
+static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
+       .attach = drm_gem_map_attach,
+       .detach = drm_gem_map_detach,
+       .map_dma_buf = drm_gem_map_dma_buf,
+       .unmap_dma_buf = drm_gem_unmap_dma_buf,
+       .release = drm_gem_dmabuf_release,
+       .kmap = drm_gem_dmabuf_kmap,
+       .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
+       .kunmap = drm_gem_dmabuf_kunmap,
+       .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
+       .mmap = drm_gem_dmabuf_mmap,
+       .vmap = drm_gem_dmabuf_vmap,
+       .vunmap = drm_gem_dmabuf_vunmap,
+};
+
+/**
+ * DOC: PRIME Helpers
+ *
+ * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
+ * simpler APIs by using the helper functions @drm_gem_prime_export and
+ * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
+ * six lower-level driver callbacks:
+ *
+ * Export callbacks:
+ *
+ *  * @gem_prime_pin (optional): prepare a GEM object for exporting
+ *  * @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
+ *  * @gem_prime_vmap: vmap a buffer exported by your driver
+ *  * @gem_prime_vunmap: vunmap a buffer exported by your driver
+ *  * @gem_prime_mmap (optional): mmap a buffer exported by your driver
+ *
+ * Import callback:
+ *
+ *  * @gem_prime_import_sg_table (import): produce a GEM object from another
+ *    driver's scatter/gather table
+ */
+
+/**
+ * drm_gem_prime_export - helper library implementation of the export callback
+ * @dev: drm_device to export from
+ * @obj: GEM object to export
+ * @flags: flags like DRM_CLOEXEC and DRM_RDWR
+ *
+ * This is the implementation of the gem_prime_export functions for GEM drivers
+ * using the PRIME helpers.
+ */
+struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
+                                    struct drm_gem_object *obj, int flags)
+{
+       DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
+
+       exp_info.ops = &drm_gem_prime_dmabuf_ops;
+       exp_info.size = obj->size;
+       exp_info.flags = flags;
+       exp_info.priv = obj;
+
+       if (dev->driver->gem_prime_res_obj)
+               exp_info.resv = dev->driver->gem_prime_res_obj(obj);
+
+       return dma_buf_export(&exp_info);
+}
+EXPORT_SYMBOL(drm_gem_prime_export);
+
+#if 0
+static struct dma_buf *export_and_register_object(struct drm_device *dev,
+                                                 struct drm_gem_object *obj,
+                                                 uint32_t flags)
+{
+       struct dma_buf *dmabuf;
+
+       /* prevent races with concurrent gem_close. */
+       if (obj->handle_count == 0) {
+               dmabuf = ERR_PTR(-ENOENT);
+               return dmabuf;
+       }
+
+       dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
+       if (IS_ERR(dmabuf)) {
+               /* normally the created dma-buf takes ownership of the ref,
+                * but if that fails then drop the ref
+                */
+               return dmabuf;
+       }
+
+       /*
+        * Note that callers do not need to clean up the export cache
+        * since the check for obj->handle_count guarantees that someone
+        * will clean it up.
+        */
+       obj->dma_buf = dmabuf;
+       get_dma_buf(obj->dma_buf);
+       /* Grab a new ref since the callers is now used by the dma-buf */
+       drm_gem_object_reference(obj);
+
+       return dmabuf;
+}
+#endif
+
+/**
+ * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
+ * @dev: dev to export the buffer from
+ * @file_priv: drm file-private structure
+ * @handle: buffer handle to export
+ * @flags: flags like DRM_CLOEXEC
+ * @prime_fd: pointer to storage for the fd id of the create dma-buf
+ *
+ * This is the PRIME export function which must be used mandatorily by GEM
+ * drivers to ensure correct lifetime management of the underlying GEM object.
+ * The actual exporting from GEM object to a dma-buf is done through the
+ * gem_prime_export driver callback.
+ */
+int drm_gem_prime_handle_to_fd(struct drm_device *dev,
+                              struct drm_file *file_priv, uint32_t handle,
+                              uint32_t flags,
+                              int *prime_fd)
+{
+#if 0
+       struct drm_gem_object *obj;
+#endif
+       int ret = 0;
+#if 0
+       struct dma_buf *dmabuf;
+
+       mutex_lock(&file_priv->prime.lock);
+       obj = drm_gem_object_lookup(file_priv, handle);
+       if (!obj)  {
+#endif
+               ret = -ENOENT;
+#if 0
+               goto out_unlock;
+       }
+
+       dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
+       if (dmabuf) {
+               get_dma_buf(dmabuf);
+               goto out_have_handle;
+       }
+
+       mutex_lock(&dev->object_name_lock);
+       /* re-export the original imported object */
+       if (obj->import_attach) {
+               dmabuf = obj->import_attach->dmabuf;
+               get_dma_buf(dmabuf);
+               goto out_have_obj;
+       }
+
+       if (obj->dma_buf) {
+               get_dma_buf(obj->dma_buf);
+               dmabuf = obj->dma_buf;
+               goto out_have_obj;
+       }
+
+       dmabuf = export_and_register_object(dev, obj, flags);
+       if (IS_ERR(dmabuf)) {
+               /* normally the created dma-buf takes ownership of the ref,
+                * but if that fails then drop the ref
+                */
+               ret = PTR_ERR(dmabuf);
+               mutex_unlock(&dev->object_name_lock);
+               goto out;
+       }
+
+out_have_obj:
+       /*
+        * If we've exported this buffer then cheat and add it to the import list
+        * so we get the correct handle back. We must do this under the
+        * protection of dev->object_name_lock to ensure that a racing gem close
+        * ioctl doesn't miss to remove this buffer handle from the cache.
+        */
+       ret = drm_prime_add_buf_handle(&file_priv->prime,
+                                      dmabuf, handle);
+       mutex_unlock(&dev->object_name_lock);
+       if (ret)
+               goto fail_put_dmabuf;
+
+out_have_handle:
+       ret = dma_buf_fd(dmabuf, flags);
+       /*
+        * We must _not_ remove the buffer from the handle cache since the newly
+        * created dma buf is already linked in the global obj->dma_buf pointer,
+        * and that is invariant as long as a userspace gem handle exists.
+        * Closing the handle will clean out the cache anyway, so we don't leak.
+        */
+       if (ret < 0) {
+               goto fail_put_dmabuf;
+       } else {
+               *prime_fd = ret;
+               ret = 0;
+       }
+
+       goto out;
+
+fail_put_dmabuf:
+       dma_buf_put(dmabuf);
+out:
+       drm_gem_object_unreference_unlocked(obj);
+out_unlock:
+       mutex_unlock(&file_priv->prime.lock);
+#endif
+
+       return ret;
+}
+EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
+
+/**
+ * drm_gem_prime_import - helper library implementation of the import callback
+ * @dev: drm_device to import into
+ * @dma_buf: dma-buf object to import
+ *
+ * This is the implementation of the gem_prime_import functions for GEM drivers
+ * using the PRIME helpers.
+ */
+struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
+                                           struct dma_buf *dma_buf)
+{
+#if 0
+       struct dma_buf_attachment *attach;
+       struct sg_table *sgt;
+       struct drm_gem_object *obj;
+       int ret;
+
+       if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
+               obj = dma_buf->priv;
+               if (obj->dev == dev) {
+                       /*
+                        * Importing dmabuf exported from out own gem increases
+                        * refcount on gem itself instead of f_count of dmabuf.
+                        */
+                       drm_gem_object_reference(obj);
+                       return obj;
+               }
+       }
+
+       if (!dev->driver->gem_prime_import_sg_table)
+#endif
+               return ERR_PTR(-EINVAL);
+
+#if 0
+       attach = dma_buf_attach(dma_buf, dev->dev);
+       if (IS_ERR(attach))
+               return ERR_CAST(attach);
+
+       get_dma_buf(dma_buf);
+
+       sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+       if (IS_ERR(sgt)) {
+               ret = PTR_ERR(sgt);
+               goto fail_detach;
+       }
+
+       obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
+       if (IS_ERR(obj)) {
+               ret = PTR_ERR(obj);
+               goto fail_unmap;
+       }
+
+       obj->import_attach = attach;
+
+       return obj;
+
+fail_unmap:
+       dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
+fail_detach:
+       dma_buf_detach(dma_buf, attach);
+       dma_buf_put(dma_buf);
+
+       return ERR_PTR(ret);
+#endif
+}
+EXPORT_SYMBOL(drm_gem_prime_import);
+
+/**
+ * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
+ * @dev: dev to export the buffer from
+ * @file_priv: drm file-private structure
+ * @prime_fd: fd id of the dma-buf which should be imported
+ * @handle: pointer to storage for the handle of the imported buffer object
+ *
+ * This is the PRIME import function which must be used mandatorily by GEM
+ * drivers to ensure correct lifetime management of the underlying GEM object.
+ * The actual importing of GEM object from the dma-buf is done through the
+ * gem_import_export driver callback.
+ */
+int drm_gem_prime_fd_to_handle(struct drm_device *dev,
+                              struct drm_file *file_priv, int prime_fd,
+                              uint32_t *handle)
+{
+#if 0
+       struct dma_buf *dma_buf;
+       struct drm_gem_object *obj;
+       int ret;
+
+       dma_buf = dma_buf_get(prime_fd);
+       if (IS_ERR(dma_buf))
+               return PTR_ERR(dma_buf);
+
+       mutex_lock(&file_priv->prime.lock);
+
+       ret = drm_prime_lookup_buf_handle(&file_priv->prime,
+                       dma_buf, handle);
+       if (ret == 0)
+               goto out_put;
+
+       /* never seen this one, need to import */
+       mutex_lock(&dev->object_name_lock);
+       obj = dev->driver->gem_prime_import(dev, dma_buf);
+       if (IS_ERR(obj)) {
+               ret = PTR_ERR(obj);
+               goto out_unlock;
+       }
+
+       if (obj->dma_buf) {
+               WARN_ON(obj->dma_buf != dma_buf);
+       } else {
+               obj->dma_buf = dma_buf;
+               get_dma_buf(dma_buf);
+       }
+
+       /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
+       ret = drm_gem_handle_create_tail(file_priv, obj, handle);
+       drm_gem_object_unreference_unlocked(obj);
+       if (ret)
+               goto out_put;
+
+       ret = drm_prime_add_buf_handle(&file_priv->prime,
+                       dma_buf, *handle);
+       if (ret)
+               goto fail;
+
+       mutex_unlock(&file_priv->prime.lock);
+
+       dma_buf_put(dma_buf);
+
+       return 0;
+
+fail:
+       /* hmm, if driver attached, we are relying on the free-object path
+        * to detach.. which seems ok..
+        */
+       drm_gem_handle_delete(file_priv, *handle);
+out_unlock:
+       mutex_unlock(&dev->object_name_lock);
+out_put:
+       dma_buf_put(dma_buf);
+       mutex_unlock(&file_priv->prime.lock);
+       return ret;
+#endif
+       return -EINVAL;
+}
+EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
+
+int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
+                                struct drm_file *file_priv)
+{
+       struct drm_prime_handle *args = data;
+
+       if (!drm_core_check_feature(dev, DRIVER_PRIME))
+               return -EINVAL;
+
+       if (!dev->driver->prime_handle_to_fd)
+               return -ENOSYS;
+
+       /* check flags are valid */
+       if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
+               return -EINVAL;
+
+       return dev->driver->prime_handle_to_fd(dev, file_priv,
+                       args->handle, args->flags, &args->fd);
+}
+
+int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
+                                struct drm_file *file_priv)
+{
+       struct drm_prime_handle *args = data;
+
+       if (!drm_core_check_feature(dev, DRIVER_PRIME))
+               return -EINVAL;
+
+       if (!dev->driver->prime_fd_to_handle)
+               return -ENOSYS;
+
+       return dev->driver->prime_fd_to_handle(dev, file_priv,
+                       args->fd, &args->handle);
+}
+
+/**
+ * drm_prime_pages_to_sg - converts a page array into an sg list
+ * @pages: pointer to the array of page pointers to convert
+ * @nr_pages: length of the page vector
+ *
+ * This helper creates an sg table object from a set of pages
+ * the driver is responsible for mapping the pages into the
+ * importers address space for use with dma_buf itself.
+ */
+struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
+{
+#if 0
+       struct sg_table *sg = NULL;
+#endif
+       int ret;
+
+#if 0
+       sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
+       if (!sg) {
+#endif
+               ret = -ENOMEM;
+#if 0
+               goto out;
+       }
+
+       ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
+                               nr_pages << PAGE_SHIFT, GFP_KERNEL);
+       if (ret)
+               goto out;
+
+       return sg;
+out:
+       kfree(sg);
+#endif
+       return ERR_PTR(ret);
+}
+EXPORT_SYMBOL(drm_prime_pages_to_sg);
+
+/**
+ * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
+ * @sgt: scatter-gather table to convert
+ * @pages: array of page pointers to store the page array in
+ * @addrs: optional array to store the dma bus address of each page
+ * @max_pages: size of both the passed-in arrays
+ *
+ * Exports an sg table into an array of pages and addresses. This is currently
+ * required by the TTM driver in order to do correct fault handling.
+ */
+int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
+                                    dma_addr_t *addrs, int max_pages)
+{
+       unsigned count;
+       struct scatterlist *sg;
+       struct page *page;
+       u32 len;
+       int pg_index;
+       dma_addr_t addr;
+
+       pg_index = 0;
+       for_each_sg(sgt->sgl, sg, sgt->nents, count) {
+               len = sg->length;
+               page = sg_page(sg);
+               addr = sg_dma_address(sg);
+
+               while (len > 0) {
+                       if (WARN_ON(pg_index >= max_pages))
+                               return -1;
+                       pages[pg_index] = page;
+                       if (addrs)
+                               addrs[pg_index] = addr;
+
+                       page++;
+                       addr += PAGE_SIZE;
+                       len -= PAGE_SIZE;
+                       pg_index++;
+               }
+       }
+       return 0;
+}
+EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
+
+/**
+ * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
+ * @obj: GEM object which was created from a dma-buf
+ * @sg: the sg-table which was pinned at import time
+ *
+ * This is the cleanup functions which GEM drivers need to call when they use
+ * @drm_gem_prime_import to import dma-bufs.
+ */
+void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
+{
+       struct dma_buf_attachment *attach;
+       struct dma_buf *dma_buf;
+       attach = obj->import_attach;
+       if (sg)
+               dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
+       dma_buf = attach->dmabuf;
+#if 0
+       dma_buf_detach(attach->dmabuf, attach);
+       /* remove the reference */
+       dma_buf_put(dma_buf);
+#endif
+}
+EXPORT_SYMBOL(drm_prime_gem_destroy);
+
+void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
+{
+       INIT_LIST_HEAD(&prime_fpriv->head);
+       lockinit(&prime_fpriv->lock, "drmpfpl", 0, LK_CANRECURSE);
+}
+
+void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
+{
+       /* by now drm_gem_release should've made sure the list is empty */
+       WARN_ON(!list_empty(&prime_fpriv->head));
+}
index a56b52d..f082f88 100644 (file)
 #include <linux/fence.h>
 #include <linux/wait.h>
 
+#include <linux/slab.h>
+
+struct dma_buf;
+struct dma_buf_attachment;
+
 struct dma_buf_ops {
        struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
                                                enum dma_data_direction);
@@ -51,6 +56,8 @@ struct dma_buf_ops {
        void (*vunmap)(struct dma_buf *, void *vaddr);
        int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
        int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
+       int (*attach)(struct dma_buf *, struct device *, struct dma_buf_attachment *);
+       void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 };
 
 struct dma_buf {
@@ -63,6 +70,7 @@ struct dma_buf {
 struct dma_buf_attachment {
        struct dma_buf *dmabuf;
        struct device *dev;
+       void *priv;
 };
 
 struct dma_buf_export_info {
@@ -70,6 +78,7 @@ struct dma_buf_export_info {
        size_t size;
        int flags;
        void *priv;
+       struct reservation_object *resv;
 };
 
 struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
@@ -94,6 +103,4 @@ dma_buf_attach(struct dma_buf *dmabuf, struct device *dev)
        return attach;
 }
 
-
-
 #endif /* LINUX_DMA_BUF_H */
index 3d7ba4f..ede790d 100644 (file)
@@ -30,6 +30,7 @@
 enum dma_data_direction {
        DMA_BIDIRECTIONAL = 0,
        DMA_TO_DEVICE = 1,
+       DMA_NONE = 3,
 };
 
 #endif /* _LINUX_DMA_DIRECTION_H_ */
index 24bfc52..a454f4d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2018 François Tigeot <ftigeot@wolfpond.org>
+ * Copyright (c) 2015-2019 François Tigeot <ftigeot@wolfpond.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 struct address_space;
 
+struct poll_table_struct;
+struct vm_area_struct;
+
+struct inode {
+};
+
+struct file_operations {
+       struct module *owner;
+       int (*open) (struct inode *, struct file *);
+       int (*release) (struct inode *, struct file *);
+       long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
+       int (*mmap) (struct file *, struct vm_area_struct *);
+       unsigned int (*poll) (struct file *, struct poll_table_struct *);
+       ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
+       loff_t (*llseek) (struct file *, loff_t, int);
+};
+
+extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
+
 #endif /* _LINUX_FS_H_ */
index ee68b6a..c1b0fd5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 François Tigeot <ftigeot@wolfpond.org>
+ * Copyright (c) 2017-2019 François Tigeot <ftigeot@wolfpond.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -34,4 +34,7 @@
 #include <linux/fs.h>
 #include <asm/uaccess.h>
 
+struct poll_table_struct {
+};
+
 #endif /* _LINUX_POLL_H_ */
similarity index 71%
copy from sys/dev/drm/include/linux/poll.h
copy to sys/dev/drm/linux_dma-buf.c
index ee68b6a..8dcd16d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 François Tigeot <ftigeot@wolfpond.org>
+ * Copyright (c) 2019 François Tigeot <ftigeot@wolfpond.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef _LINUX_POLL_H_
-#define _LINUX_POLL_H_
-
-#include <linux/compiler.h>
-#include <linux/ktime.h>
-#include <linux/wait.h>
-#include <linux/string.h>
 #include <linux/fs.h>
-#include <asm/uaccess.h>
+#include <linux/slab.h>
+#include <linux/dma-buf.h>
+#include <linux/fence.h>
+#include <linux/export.h>
+#include <linux/module.h>
+#include <linux/seq_file.h>
+#include <linux/poll.h>
+#include <linux/reservation.h>
+#include <linux/mm.h>
+
+struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
+{
+       return ERR_PTR(-EINVAL);
+}
+
+void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
+                               struct sg_table *sg_table,
+                               enum dma_data_direction direction)
+{
+}
 
-#endif /* _LINUX_POLL_H_ */