[PATCH] gpu: drm: Add helpers to allow export gem cma objects

2013-10-18 Thread benjamin.gaign...@linaro.org
From: Benjamin Gaignard 

DRM already offer helpers to use CMA for dumb buffers.
This patch add helpers to export/import gem_cam objects and allow them to be 
mmap from userland.
The goal is to make working this kind of sequence: create_dumb, get fd from
buffer handle and then use fd (maybe in another process which may ignore it
is comming from DRM) to mmap the buffer.

drm_gem_cma_prime_export() add O_RDWR to flags to be sure that memory
could be mmapped later with PROT_WRITE flag.

Signed-off-by: Benjamin Gaignard 
---
 drivers/gpu/drm/drm_gem_cma_helper.c |  192 ++
 include/drm/drm_gem_cma_helper.h |6 ++
 2 files changed, 198 insertions(+)

diff --git a/drivers/gpu/drm/drm_gem_cma_helper.c 
b/drivers/gpu/drm/drm_gem_cma_helper.c
index bad85bb..936c337 100644
--- a/drivers/gpu/drm/drm_gem_cma_helper.c
+++ b/drivers/gpu/drm/drm_gem_cma_helper.c
@@ -21,12 +21,204 @@
 #include 
 #include 
 #include 
+#include 
 #include 

 #include 
 #include 
 #include 

+struct drm_gem_cma_dmabuf_attachment {
+   struct sg_table sgt;
+   enum dma_data_direction dir;
+   bool is_mapped;
+};
+
+static int drm_gem_cma_attach_dma_buf(struct dma_buf *dmabuf,
+   struct device *dev,
+   struct dma_buf_attachment *attach)
+{
+   struct drm_gem_cma_dmabuf_attachment *drm_gem_cma_attach;
+
+   drm_gem_cma_attach = kzalloc(sizeof(*drm_gem_cma_attach), GFP_KERNEL);
+   if (!drm_gem_cma_attach)
+   return -ENOMEM;
+
+   drm_gem_cma_attach->dir = DMA_NONE;
+   attach->priv = drm_gem_cma_attach;
+
+   return 0;
+}
+
+static void drm_gem_cma_detach_dma_buf(struct dma_buf *dmabuf,
+   struct dma_buf_attachment *attach)
+{
+   struct drm_gem_cma_dmabuf_attachment *drm_gem_cma_attach = attach->priv;
+   struct sg_table *sgt;
+
+   if (!drm_gem_cma_attach)
+   return;
+
+   sgt = &drm_gem_cma_attach->sgt;
+
+   if (drm_gem_cma_attach->dir != DMA_NONE)
+   dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
+   drm_gem_cma_attach->dir);
+
+   sg_free_table(sgt);
+   kfree(drm_gem_cma_attach);
+   attach->priv = NULL;
+}
+
+static struct sg_table *
+drm_gem_cma_map_dma_buf(struct dma_buf_attachment *attach,
+   enum dma_data_direction dir)
+{
+   struct drm_gem_cma_dmabuf_attachment *drm_gem_cma_attach = attach->priv;
+   struct drm_gem_cma_object *cma_obj = attach->dmabuf->priv;
+   struct drm_device *dev = cma_obj->base.dev;
+   struct sg_table *sgt = NULL;
+   int nents, ret;
+
+   /* just return current sgt if already requested. */
+   if (drm_gem_cma_attach->dir == dir && drm_gem_cma_attach->is_mapped)
+   return &drm_gem_cma_attach->sgt;
+
+   sgt = &drm_gem_cma_attach->sgt;
+
+   ret = dma_common_get_sgtable(dev->dev, sgt,
+   cma_obj->vaddr, cma_obj->paddr, cma_obj->base.size);
+   if (ret) {
+   DRM_ERROR("failed to get sgt.\n");
+   return ERR_PTR(-ENOMEM);
+   }
+
+   mutex_lock(&dev->struct_mutex);
+
+   if (dir != DMA_NONE) {
+   nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
+   if (!nents) {
+   DRM_ERROR("failed to map sgl with iommu.\n");
+   sg_free_table(sgt);
+   sgt = ERR_PTR(-EIO);
+   goto err_unlock;
+   }
+   }
+
+   drm_gem_cma_attach->is_mapped = true;
+   drm_gem_cma_attach->dir = dir;
+   attach->priv = drm_gem_cma_attach;
+
+err_unlock:
+   mutex_unlock(&dev->struct_mutex);
+   return sgt;
+}
+
+static void drm_gem_cma_unmap_dma_buf(struct dma_buf_attachment *attach,
+   struct sg_table *sgt,
+   enum dma_data_direction dir)
+{
+   /* Nothing to do */
+}
+
+static void drm_gem_cma_dmabuf_release(struct dma_buf *dmabuf)
+{
+   struct drm_gem_cma_object *cma_obj = dmabuf->priv;
+
+   /*
+* drm_gem_cma_dmabuf_release() call means that file object's
+* f_count is 0 and it calls drm_gem_object_handle_unreference()
+* to drop the references that these values had been increased
+* at drm_prime_handle_to_fd()
+*/
+   if (cma_obj->base.export_dma_buf == dmabuf) {
+   cma_obj->base.export_dma_buf = NULL;
+
+   /*
+* drop this gem object refcount to release allocated buffer
+* and resources.
+*/
+   drm_gem_object_unreference_unlocked(&cma_obj->base);
+   }
+}
+
+static void *drm_gem_cma_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
+   unsigned long page_num)
+{
+   struct drm_gem_cma_object

[PATCH] [configure.ac] allow tests programs to be installed

2013-02-06 Thread benjamin.gaign...@linaro.org
From: Benjamin Gaignard 

Install test programs is useful in cross compilation case.
By default the behavior is the same and test programs aren't installed in 
$bindir.
If --enable-install-test-programs is set then test programs are installed in 
$bindir

Signed-off-by: Benjamin Gaignard 
---
 configure.ac|   10 ++
 tests/kmstest/Makefile.am   |5 +
 tests/modeprint/Makefile.am |5 +
 tests/modetest/Makefile.am  |5 +
 tests/vbltest/Makefile.am   |6 +-
 5 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 9ee7940..f65c85c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -93,6 +93,11 @@ AC_ARG_ENABLE(exynos-experimental-api,
  [Enable support for EXYNOS's experimental API (default: 
disabled)]),
  [EXYNOS=$enableval], [EXYNOS=no])

+AC_ARG_ENABLE(install-test-programs,
+ AS_HELP_STRING([--enable-install-test-programs],
+ [Install test programs (default: no)]),
+ [INSTALL_TESTS=$enableval], [INSTALL_TESTS=no])
+
 dnl ===
 dnl check compiler flags
 AC_DEFUN([LIBDRM_CC_TRY_FLAG], [
@@ -201,6 +206,11 @@ if test "x$EXYNOS" = xyes; then
AC_DEFINE(HAVE_EXYNOS, 1, [Have EXYNOS support])
 fi

+AM_CONDITIONAL(HAVE_INSTALL_TESTS, [test "x$INSTALL_TESTS" = xyes])
+if test "x$INSTALL_TESTS" = xyes; then
+   AC_DEFINE(HAVE_INSTALL_TESTS, 1, [Install test programs])
+fi
+
 AC_ARG_ENABLE([cairo-tests],
   [AS_HELP_STRING([--enable-cairo-tests],
   [Enable support for Cairo rendering in tests 
(default: auto)])],
diff --git a/tests/kmstest/Makefile.am b/tests/kmstest/Makefile.am
index ae562a1..7903a26 100644
--- a/tests/kmstest/Makefile.am
+++ b/tests/kmstest/Makefile.am
@@ -3,8 +3,13 @@ AM_CFLAGS = \
-I$(top_srcdir)/libkms/ \
-I$(top_srcdir)

+if HAVE_INSTALL_TESTS
+bin_PROGRAMS = \
+   kmstest
+else
 noinst_PROGRAMS = \
kmstest
+endif

 kmstest_SOURCES = \
main.c
diff --git a/tests/modeprint/Makefile.am b/tests/modeprint/Makefile.am
index c4862ac..6420ef3 100644
--- a/tests/modeprint/Makefile.am
+++ b/tests/modeprint/Makefile.am
@@ -2,8 +2,13 @@ AM_CFLAGS = \
-I$(top_srcdir)/include/drm \
-I$(top_srcdir)

+if HAVE_INSTALL_TESTS
+bin_PROGRAMS = \
+   modeprint
+else
 noinst_PROGRAMS = \
modeprint
+endif

 modeprint_SOURCES = \
modeprint.c
diff --git a/tests/modetest/Makefile.am b/tests/modetest/Makefile.am
index 065ae13..410c632 100644
--- a/tests/modetest/Makefile.am
+++ b/tests/modetest/Makefile.am
@@ -3,8 +3,13 @@ AM_CFLAGS = \
-I$(top_srcdir)/libkms/ \
-I$(top_srcdir)

+if HAVE_INSTALL_TESTS
+bin_PROGRAMS = \
+   modetest
+else
 noinst_PROGRAMS = \
modetest
+endif

 modetest_SOURCES = \
buffers.c modetest.c buffers.h
diff --git a/tests/vbltest/Makefile.am b/tests/vbltest/Makefile.am
index 77f9037..f99b6a2 100644
--- a/tests/vbltest/Makefile.am
+++ b/tests/vbltest/Makefile.am
@@ -1,9 +1,13 @@
 AM_CFLAGS = \
-I$(top_srcdir)/include/drm \
-I$(top_srcdir)
-
+if HAVE_INSTALL_TESTS
+noinst_PROGRAMS = \
+   vbltest
+else
 noinst_PROGRAMS = \
vbltest
+endif

 vbltest_SOURCES = \
vbltest.c
-- 
1.7.10