ionmemory: dmabuf memory allocator based on ion driver.

Upstream Status: Pending

https://bugzilla.gnome.org/show_bug.cgi?id=768794
diff --git a/configure.ac b/configure.ac
index 1287583..3a72378 100644
--- a/configure.ac
+++ b/configure.ac
@@ -673,6 +673,14 @@
     AC_SUBST(CDPARANOIA_LIBS)
   ])
 ])
+
+
+dnl check for ion
+translit(dnm, m, l) AM_CONDITIONAL(USE_ION, true)
+AG_GST_CHECK_FEATURE(ION, [ion], ion, [
+  AC_CHECK_HEADER(linux/ion.h, HAVE_ION="yes", HAVE_ION="no")
+])
+
 dnl FIXME : add second check somehow if that is necessary
 dnl AC_CHECK_LIB(cdda_paranoia, paranoia_init, : , HAVE_CDPARANOIA=no, -lcdda_interface )
 dnl AC_CHECK_HEADER(cdda_paranoia.h, :, HAVE_CDPARANOIA=no)
diff --git a/gst-libs/gst/allocators/Makefile.am b/gst-libs/gst/allocators/Makefile.am
index 4f72b2e..f3d63e0 100644
--- a/gst-libs/gst/allocators/Makefile.am
+++ b/gst-libs/gst/allocators/Makefile.am
@@ -10,6 +10,11 @@
 	gstdmabuf.h \
 	gstdmabufmeta.h
 
+if USE_ION
+libgstallocators_@GST_API_VERSION@_include_HEADERS += \
+ gstionmemory.h
+endif
+
 noinst_HEADERS =
 
 libgstallocators_@GST_API_VERSION@_la_SOURCES = \
@@ -18,6 +23,11 @@
 	gstdmabuf.c \
 	gstdmabufmeta.c
 
+if USE_ION
+libgstallocators_@GST_API_VERSION@_la_SOURCES += \
+ gstionmemory.c
+endif
+
 libgstallocators_@GST_API_VERSION@_la_LIBADD = $(GST_LIBS) $(LIBM)
 libgstallocators_@GST_API_VERSION@_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS)
 libgstallocators_@GST_API_VERSION@_la_LDFLAGS = $(GST_LIB_LDFLAGS) $(GST_ALL_LDFLAGS) $(GST_LT_LDFLAGS)
diff --git a/gst-libs/gst/allocators/gstionmemory.c b/gst-libs/gst/allocators/gstionmemory.c
new file mode 100644
index 0000000..bfe13ad
--- /dev/null
+++ b/gst-libs/gst/allocators/gstionmemory.c
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2016, Freescale Semiconductor, Inc. All rights reserved.
+ * Copyright 2017 NXP
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <linux/ion.h>
+
+#include <gst/allocators/gstdmabuf.h>
+#include "gstionmemory.h"
+
+GST_DEBUG_CATEGORY_STATIC (ion_allocator_debug);
+#define GST_CAT_DEFAULT ion_allocator_debug
+
+#define gst_ion_allocator_parent_class parent_class
+
+G_DEFINE_TYPE (GstIONAllocator, gst_ion_allocator, GST_TYPE_DMABUF_ALLOCATOR)
+
+#define DEFAULT_HEAP_ID  0
+#define DEFAULT_FLAG     0
+
+enum
+{
+  PROP_0,
+  PROP_HEAP_ID,
+  PROP_FLAG,
+  PROP_LAST
+};
+
+static gint
+gst_ion_ioctl (gint fd, gint req, void *arg)
+{
+  gint ret = ioctl (fd, req, arg);
+  if (ret < 0) {
+    GST_ERROR ("ioctl %x failed with code %d: %s\n", req, ret,
+        strerror (errno));
+  }
+  return ret;
+}
+
+static void
+gst_ion_mem_init (void)
+{
+  GstAllocator *allocator = g_object_new (gst_ion_allocator_get_type (), NULL);
+  GstIONAllocator *self = GST_ION_ALLOCATOR (allocator);
+  gint fd;
+
+  fd = open ("/dev/ion", O_RDWR);
+  if (fd < 0) {
+    GST_WARNING ("Could not open ion driver");
+    g_object_unref (self);
+    return;
+  }
+
+  self->fd = fd;
+
+  gst_allocator_register (GST_ALLOCATOR_ION, allocator);
+}
+
+GstAllocator *
+gst_ion_allocator_obtain (void)
+{
+  static GOnce ion_allocator_once = G_ONCE_INIT;
+  GstAllocator *allocator;
+
+  g_once (&ion_allocator_once, (GThreadFunc) gst_ion_mem_init, NULL);
+
+  allocator = gst_allocator_find (GST_ALLOCATOR_ION);
+  if (allocator == NULL)
+    GST_WARNING ("No allocator named %s found", GST_ALLOCATOR_ION);
+
+  return allocator;
+}
+
+static GstMemory *
+gst_ion_alloc_alloc (GstAllocator * allocator, gsize size,
+    GstAllocationParams * params)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (allocator);
+  struct ion_allocation_data allocation_data = { 0 };
+  struct ion_fd_data fd_data = { 0 };
+  struct ion_handle_data handle_data = { 0 };
+  ion_user_handle_t ion_handle;
+  GstMemory *mem;
+  gsize ion_size;
+  gint dma_fd = -1;
+  gint ret;
+
+  if (self->fd < 0) {
+    GST_ERROR ("ion allocate param wrong");
+    return NULL;
+  }
+
+  ion_size = size + params->prefix + params->padding;
+  allocation_data.len = ion_size;
+  allocation_data.align = params->align;
+  allocation_data.heap_id_mask = 1 << self->heap_id;
+  allocation_data.flags = self->flags;
+  if (gst_ion_ioctl (self->fd, ION_IOC_ALLOC, &allocation_data) < 0) {
+    GST_ERROR ("ion allocate failed.");
+    return NULL;
+  }
+  ion_handle = allocation_data.handle;
+
+  fd_data.handle = ion_handle;
+  ret = gst_ion_ioctl (self->fd, ION_IOC_MAP, &fd_data);
+  if (ret < 0 || fd_data.fd < 0) {
+    GST_ERROR ("map ioctl failed or returned negative fd");
+    goto bail;
+  }
+  dma_fd = fd_data.fd;
+
+  handle_data.handle = ion_handle;
+  gst_ion_ioctl (self->fd, ION_IOC_FREE, &handle_data);
+
+  mem = gst_dmabuf_allocator_alloc (allocator, dma_fd, size);
+
+  GST_DEBUG ("ion allocated size: %" G_GSIZE_FORMAT "DMA FD: %d", ion_size,
+      dma_fd);
+
+  return mem;
+
+bail:
+  if (dma_fd >= 0) {
+    close (dma_fd);
+  }
+  handle_data.handle = ion_handle;
+  gst_ion_ioctl (self->fd, ION_IOC_FREE, &handle_data);
+
+  return NULL;
+}
+
+static void
+gst_ion_allocator_dispose (GObject * object)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (object);
+
+  if (self->fd > 0) {
+    close (self->fd);
+    self->fd = -1;
+  }
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gst_ion_allocator_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (object);
+
+  switch (prop_id) {
+    case PROP_HEAP_ID:
+      self->heap_id = g_value_get_uint (value);
+      break;
+    case PROP_FLAG:
+      self->flags = g_value_get_uint (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_ion_allocator_get_property (GObject * object, guint prop_id, GValue * value,
+    GParamSpec * pspec)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (object);
+
+  switch (prop_id) {
+    case PROP_HEAP_ID:
+      g_value_set_uint (value, self->heap_id);
+      break;
+    case PROP_FLAG:
+      g_value_set_uint (value, self->flags);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_ion_allocator_class_init (GstIONAllocatorClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
+
+  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ion_allocator_dispose);
+  gobject_class->set_property = gst_ion_allocator_set_property;
+  gobject_class->get_property = gst_ion_allocator_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_HEAP_ID,
+      g_param_spec_uint ("heap-id", "Heap ID",
+        "ION heap id", 0, G_MAXUINT32, DEFAULT_HEAP_ID,
+        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_FLAG,
+      g_param_spec_uint ("flags", "Flags",
+        "ION memory flags", 0, G_MAXUINT32, DEFAULT_FLAG,
+        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  allocator_class->alloc = GST_DEBUG_FUNCPTR (gst_ion_alloc_alloc);
+
+  GST_DEBUG_CATEGORY_INIT (ion_allocator_debug, "ionmemory", 0,
+      "DMA FD memory allocator based on ion");
+}
+
+static void
+gst_ion_allocator_init (GstIONAllocator * self)
+{
+  GstAllocator *allocator = GST_ALLOCATOR (self);
+
+  allocator->mem_type = GST_ALLOCATOR_ION;
+
+  self->heap_id = DEFAULT_HEAP_ID;
+  self->flags = DEFAULT_FLAG;
+}
diff --git a/gst-libs/gst/allocators/gstionmemory.h b/gst-libs/gst/allocators/gstionmemory.h
new file mode 100644
index 0000000..434cdec
--- /dev/null
+++ b/gst-libs/gst/allocators/gstionmemory.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2016, Freescale Semiconductor, Inc. All rights reserved.
+ * Copyright 2017 NXP
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_IONMEMORY_H__
+#define __GST_IONMEMORY_H__
+
+#include <gst/gst.h>
+#include <gst/allocators/gstdmabuf.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GstIONAllocator GstIONAllocator;
+typedef struct _GstIONAllocatorClass GstIONAllocatorClass;
+typedef struct _GstIONMemory GstIONMemory;
+
+#define GST_ALLOCATOR_ION "ionmem"
+
+#define GST_TYPE_ION_ALLOCATOR gst_ion_allocator_get_type ()
+#define GST_IS_ION_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+    GST_TYPE_ION_ALLOCATOR))
+#define GST_ION_ALLOCATOR(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_ION_ALLOCATOR, GstIONAllocator))
+#define GST_ION_ALLOCATOR_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_ION_ALLOCATOR, GstIONAllocatorClass))
+#define GST_ION_ALLOCATOR_CAST(obj) ((GstIONAllocator *)(obj))
+
+#define GST_ION_MEMORY_QUARK gst_ion_memory_quark ()
+
+struct _GstIONAllocator
+{
+  GstDmaBufAllocator parent;
+
+  gint fd;
+  guint heap_id;
+  guint flags;
+};
+
+struct _GstIONAllocatorClass
+{
+  GstDmaBufAllocatorClass parent;
+};
+
+GST_EXPORT
+GType gst_ion_allocator_get_type (void);
+
+GST_EXPORT
+GstAllocator* gst_ion_allocator_obtain (void);
+
+G_END_DECLS
+
+#endif /* __GST_IONMEMORY_H__ */