move source files to src/libtbm-nexell/ 41/252841/1
authorSooChan Lim <sc1.lim@samsung.com>
Wed, 3 Feb 2021 00:14:49 +0000 (09:14 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Wed, 3 Feb 2021 00:14:49 +0000 (09:14 +0900)
Change-Id: Ic9ed8695b1b71699d8ff8bbe803d1dd49ed7317d

configure.ac
src/Makefile.am
src/libtbm-nexell/Makefile.am [new file with mode: 0644]
src/libtbm-nexell/tbm_bufmgr_nexell.c [new file with mode: 0644]
src/libtbm-nexell/tbm_bufmgr_tgl.h [new file with mode: 0644]
src/tbm_bufmgr_nexell.c [deleted file]
src/tbm_bufmgr_tgl.h [deleted file]

index a5f590144ac05211e91af704ee680e9030a8699f..2908cd8eb4b4d95378ad0983c4e6c7e2cb6fe4ca 100644 (file)
@@ -82,6 +82,7 @@ AC_SUBST(bufmgr_dir)
 
 AC_OUTPUT([
        Makefile
+       src/libtbm-nexell/Makefile
        src/Makefile])
 
 echo ""
index 42dd21ae08ba93601c2dd0aee87e776221bd01df..d9a90ff0ac9395bdf4aa35ec78d69fe787f8c520 100644 (file)
@@ -1,11 +1 @@
-AM_CFLAGS = \
-       @LIBTBM_NEXELL_CFLAGS@ \
-       -I$(top_srcdir) \
-       -I$(top_srcdir)/src
-
-libtbm_nexell_la_LTLIBRARIES = libtbm_nexell.la
-libtbm_nexell_ladir = /${bufmgr_dir}
-libtbm_nexell_la_LIBADD = @LIBTBM_NEXELL_LIBS@
-
-libtbm_nexell_la_SOURCES = \
-       tbm_bufmgr_nexell.c
+SUBDIRS = libtbm-nexell
diff --git a/src/libtbm-nexell/Makefile.am b/src/libtbm-nexell/Makefile.am
new file mode 100644 (file)
index 0000000..42dd21a
--- /dev/null
@@ -0,0 +1,11 @@
+AM_CFLAGS = \
+       @LIBTBM_NEXELL_CFLAGS@ \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/src
+
+libtbm_nexell_la_LTLIBRARIES = libtbm_nexell.la
+libtbm_nexell_ladir = /${bufmgr_dir}
+libtbm_nexell_la_LIBADD = @LIBTBM_NEXELL_LIBS@
+
+libtbm_nexell_la_SOURCES = \
+       tbm_bufmgr_nexell.c
diff --git a/src/libtbm-nexell/tbm_bufmgr_nexell.c b/src/libtbm-nexell/tbm_bufmgr_nexell.c
new file mode 100644 (file)
index 0000000..424a290
--- /dev/null
@@ -0,0 +1,2023 @@
+/**************************************************************************
+
+libtbm_nexell
+
+Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+
+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, sub license, 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 NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+
+**************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <libudev.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <xf86drm.h>
+#include <nexell_drm.h>
+#include <pthread.h>
+#include <tbm_backend.h>
+#include <tbm_drm_helper.h>
+#include <tbm_log.h>
+#include "tbm_bufmgr_tgl.h"
+
+#define TBM_COLOR_FORMAT_COUNT 6
+
+#define NEXELL_DRM_NAME "nexell"
+
+#define STRERR_BUFSIZE 128
+
+#define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1))
+#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+#ifdef ALIGN_EIGHT
+#define TBM_SURFACE_ALIGNMENT_PLANE (8)
+#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (8)
+#else
+#define TBM_SURFACE_ALIGNMENT_PLANE (64)
+#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (64)
+#endif
+
+#define TBM_SURFACE_ALIGNMENT_PLANE_NV12 (4096)
+#define TBM_SURFACE_ALIGNMENT_PITCH_YUV (32)
+
+#define SZ_1M                                   0x00100000
+#define S5P_FIMV_MAX_FRAME_SIZE                 (2 * SZ_1M)
+#define S5P_FIMV_D_ALIGN_PLANE_SIZE             64
+#define S5P_FIMV_NUM_PIXELS_IN_MB_ROW           16
+#define S5P_FIMV_NUM_PIXELS_IN_MB_COL           16
+#define S5P_FIMV_DEC_BUF_ALIGN                  (8 * 1024)
+#define S5P_FIMV_NV12MT_HALIGN                  128
+#define S5P_FIMV_NV12MT_VALIGN                  64
+
+/* tgl key values */
+#define GLOBAL_KEY   ((unsigned int)(-1))
+/* TBM_CACHE */
+#define TBM_NEXELL_CACHE_INV       0x01 /**< cache invalidate  */
+#define TBM_NEXELL_CACHE_CLN       0x02 /**< cache clean */
+#define TBM_NEXELL_CACHE_ALL       0x10 /**< cache all */
+#define TBM_NEXELL_CACHE_FLUSH     (TBM_NEXELL_CACHE_INV|TBM_NEXELL_CACHE_CLN) /**< cache flush  */
+#define TBM_NEXELL_CACHE_FLUSH_ALL (TBM_NEXELL_CACHE_FLUSH|TBM_NEXELL_CACHE_ALL)       /**< cache flush all */
+
+enum {
+       DEVICE_NONE = 0,
+       DEVICE_CA,                                      /* cache aware device */
+       DEVICE_CO                                       /* cache oblivious device */
+};
+
+typedef union _tbm_bo_cache_state tbm_bo_cache_state;
+
+union _tbm_bo_cache_state {
+       unsigned int val;
+       struct {
+               unsigned int cntFlush:16;       /*Flush all index for sync */
+               unsigned int isCached:1;
+               unsigned int isDirtied:2;
+       } data;
+};
+
+typedef struct _tbm_bufmgr_nexell *tbm_bufmgr_nexell;
+typedef struct _tbm_bo_nexell *tbm_bo_nexell;
+
+/* tbm buffor object for nexell */
+struct _tbm_bo_nexell {
+       int fd;
+
+       unsigned int name;    /* FLINK ID */
+
+       unsigned int gem;     /* GEM Handle */
+
+       unsigned int dmabuf;  /* fd for dmabuf */
+
+       void *pBase;          /* virtual address */
+
+       unsigned int size;
+
+       unsigned int flags_nexell;
+       unsigned int flags_tbm;
+
+       pthread_mutex_t mutex;
+       int device;
+       int opt;
+
+       tbm_bo_cache_state cache_state;
+       unsigned int map_cnt;
+
+       tbm_bufmgr_nexell bufmgr_nexell;
+};
+
+/* tbm bufmgr private for nexell */
+struct _tbm_bufmgr_nexell {
+       int fd;
+       int isLocal;
+       void *hashBos;
+
+       int tgl_fd;
+
+       char *device_name;
+       void *bind_display;
+
+       tbm_backend_bufmgr_func *bufmgr_func;
+       tbm_backend_bo_func *bo_func;
+
+       tbm_bufmgr bufmgr;
+};
+
+static char *STR_DEVICE[] = {
+       "DEF",
+       "CPU",
+       "2D",
+       "3D",
+       "MM"
+};
+
+static char *STR_OPT[] = {
+       "NONE",
+       "RD",
+       "WR",
+       "RDWR"
+};
+
+
+static uint32_t tbm_nexell_color_format_list[TBM_COLOR_FORMAT_COUNT] = {
+                                                                   TBM_FORMAT_ARGB8888,
+                                                                   TBM_FORMAT_XRGB8888,
+                                                                   TBM_FORMAT_NV12,
+                                                                   TBM_FORMAT_NV21,
+                                                                   TBM_FORMAT_YUV420,
+                                                                   TBM_FORMAT_YVU420
+                                                               };
+
+#ifdef ENABLE_CACHECRTL
+static inline int
+_tgl_init(int fd, unsigned int key)
+{
+       struct tgl_attribute attr;
+       int err;
+       char buf[STRERR_BUFSIZE];
+
+       attr.key = key;
+       attr.timeout_ms = 1000;
+
+       err = ioctl(fd, TGL_IOC_INIT_LOCK, &attr);
+       if (err) {
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
+               return 0;
+       }
+
+       return 1;
+}
+
+static inline int
+_tgl_destroy(int fd, unsigned int key)
+{
+       int err;
+       char buf[STRERR_BUFSIZE];
+
+       err = ioctl(fd, TGL_IOC_DESTROY_LOCK, key);
+       if (err) {
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
+               return 0;
+       }
+
+       return 1;
+}
+static inline int
+_tgl_set_data(int fd, unsigned int key, unsigned int val)
+{
+       struct tgl_user_data arg;
+       int err;
+       char buf[STRERR_BUFSIZE];
+
+       arg.key = key;
+       arg.data1 = val;
+       err = ioctl(fd, TGL_IOC_SET_DATA, &arg);
+       if (err) {
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
+               return 0;
+       }
+
+       return 1;
+}
+
+static inline unsigned int
+_tgl_get_data(int fd, unsigned int key)
+{
+       struct tgl_user_data arg = { 0, };
+       int err;
+       char buf[STRERR_BUFSIZE];
+
+       arg.key = key;
+       err = ioctl(fd, TGL_IOC_GET_DATA, &arg);
+       if (err) {
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
+               return 0;
+       }
+
+       return arg.data1;
+}
+
+static int
+_nexell_cache_flush(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell, int flags)
+{
+       struct nx_drm_gem_create gem;
+       char buf[STRERR_BUFSIZE];
+
+       gem.handle = bo_nexell->gem;
+       gem.size = bo_nexell->size;
+
+       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_NX_GEM_SYNC, &gem)) {
+               TBM_ERR("error(%s) DRM_IOCTL_NX_GEM_SYNC.\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE));
+               return 0;
+       }
+
+       return 1;
+}
+#endif
+
+static int
+_bo_init_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell, int import)
+{
+#ifdef ENABLE_CACHECRTL
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bo_nexell != NULL, 0);
+
+       tbm_bo_cache_state cache_state;
+
+       _tgl_init(bufmgr_nexell->tgl_fd, bo_nexell->name);
+
+       if (import == 0) {
+               cache_state.data.isDirtied = DEVICE_NONE;
+               cache_state.data.isCached = 0;
+               cache_state.data.cntFlush = 0;
+
+               _tgl_set_data(bufmgr_nexell->tgl_fd, bo_nexell->name, cache_state.val);
+       }
+#endif
+
+       return 1;
+}
+
+static int
+_bo_set_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell, int device, int opt)
+{
+#ifdef ENABLE_CACHECRTL
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bo_nexell != NULL, 0);
+
+       char need_flush = 0;
+       unsigned short cntFlush = 0;
+
+       if (bo_nexell->flags_nexell == NEXELL_BO_DMA ||
+           bo_nexell->flags_nexell == NEXELL_BO_SYSTEM_NONCONTIG)
+               return 1;
+
+       /* get cache state of a bo */
+       bo_nexell->cache_state.val = _tgl_get_data(bufmgr_nexell->tgl_fd,
+                                    bo_nexell->name);
+
+       /* get global cache flush count */
+       cntFlush = (unsigned short)_tgl_get_data(bufmgr_nexell->tgl_fd, GLOBAL_KEY);
+
+       if (device == TBM_DEVICE_CPU) {
+               if (bo_nexell->cache_state.data.isDirtied == DEVICE_CO &&
+                   bo_nexell->cache_state.data.isCached)
+                       need_flush = TBM_NEXELL_CACHE_INV;
+
+               bo_nexell->cache_state.data.isCached = 1;
+               if (opt & TBM_OPTION_WRITE)
+                       bo_nexell->cache_state.data.isDirtied = DEVICE_CA;
+               else {
+                       if (bo_nexell->cache_state.data.isDirtied != DEVICE_CA)
+                               bo_nexell->cache_state.data.isDirtied = DEVICE_NONE;
+               }
+       } else {
+               if (bo_nexell->cache_state.data.isDirtied == DEVICE_CA &&
+                   bo_nexell->cache_state.data.isCached &&
+                   bo_nexell->cache_state.data.cntFlush == cntFlush)
+                       need_flush = TBM_NEXELL_CACHE_CLN | TBM_NEXELL_CACHE_ALL;
+
+               if (opt & TBM_OPTION_WRITE)
+                       bo_nexell->cache_state.data.isDirtied = DEVICE_CO;
+               else {
+                       if (bo_nexell->cache_state.data.isDirtied != DEVICE_CO)
+                               bo_nexell->cache_state.data.isDirtied = DEVICE_NONE;
+               }
+       }
+
+       if (need_flush) {
+               if (need_flush & TBM_NEXELL_CACHE_ALL)
+                       _tgl_set_data(bufmgr_nexell->tgl_fd, GLOBAL_KEY, (unsigned int)(++cntFlush));
+
+               /* call cache flush */
+               _nexell_cache_flush(bufmgr_nexell, bo_nexell, need_flush);
+
+               TBM_DBG(" \tcache(%d,%d)....flush:0x%x, cntFlush(%d)\n",
+                   bo_nexell->cache_state.data.isCached,
+                   bo_nexell->cache_state.data.isDirtied,
+                   need_flush,
+                   cntFlush);
+       }
+#endif
+
+       return 1;
+}
+
+static int
+_bo_save_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell)
+{
+#ifdef ENABLE_CACHECRTL
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bo_nexell != NULL, 0);
+
+       unsigned short cntFlush = 0;
+
+       /* get global cache flush count */
+       cntFlush = (unsigned short)_tgl_get_data(bufmgr_nexell->tgl_fd, GLOBAL_KEY);
+
+       /* save global cache flush count */
+       bo_nexell->cache_state.data.cntFlush = cntFlush;
+       _tgl_set_data(bufmgr_nexell->tgl_fd, bo_nexell->name,
+                     bo_nexell->cache_state.val);
+#endif
+
+       return 1;
+}
+
+static void
+_bo_destroy_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell)
+{
+#ifdef ENABLE_CACHECRTL
+       TBM_RETURN_IF_FAIL(bufmgr_nexell != NULL);
+       TBM_RETURN_IF_FAIL(bo_nexell != NULL);
+
+       _tgl_destroy(bufmgr_nexell->tgl_fd, bo_nexell->name);
+#endif
+}
+
+static int
+_bufmgr_init_cache_state(tbm_bufmgr_nexell bufmgr_nexell)
+{
+#ifdef ENABLE_CACHECRTL
+       bufmgr_nexell->tgl_fd = -1;
+
+       /* open tgl fd for saving cache flush data */
+       bufmgr_nexell->tgl_fd = open(tgl_devfile, O_RDWR);
+       if (bufmgr_nexell->tgl_fd < 0) {
+               bufmgr_nexell->tgl_fd = open(tgl_devfile1, O_RDWR);
+               if (bufmgr_nexell->tgl_fd < 0) {
+                   TBM_ERR("fail to open global_lock:%s\n",
+                                       tgl_devfile1);
+                       return 0;
+               }
+       }
+
+       if (!_tgl_init(bufmgr_nexell->tgl_fd, GLOBAL_KEY)) {
+               TBM_ERR("fail to initialize the tgl\n");
+               close(bufmgr_nexell->tgl_fd);
+               bufmgr_nexell->tgl_fd = -1;
+               return 0;
+       }
+#endif
+
+       return 1;
+}
+
+static void
+_bufmgr_deinit_cache_state(tbm_bufmgr_nexell bufmgr_nexell)
+{
+#ifdef ENABLE_CACHECRTL
+       if (bufmgr_nexell->tgl_fd >= 0) {
+               close(bufmgr_nexell->tgl_fd);
+               bufmgr_nexell->tgl_fd = -1;
+       }
+#endif
+}
+
+static int
+_tbm_nexell_open_drm()
+{
+       int fd = -1;
+
+       fd = drmOpen(NEXELL_DRM_NAME, NULL);
+       if (fd < 0) {
+               TBM_ERR("fail to open drm.(%s)\n", NEXELL_DRM_NAME);
+       }
+
+       if (fd < 0) {
+               struct udev *udev = NULL;
+               struct udev_enumerate *e = NULL;
+               struct udev_list_entry *entry = NULL;
+               struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL;
+               const char *filepath;
+               struct stat s;
+               int ret;
+
+               TBM_DBG("search drm-device by udev\n");
+
+               udev = udev_new();
+               if (!udev) {
+                       TBM_ERR("udev_new() failed.\n");
+                       return -1;
+               }
+
+               e = udev_enumerate_new(udev);
+               udev_enumerate_add_match_subsystem(e, "drm");
+               udev_enumerate_add_match_sysname(e, "card[0-9]*");
+               udev_enumerate_scan_devices(e);
+
+               udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+                       device = udev_device_new_from_syspath(udev_enumerate_get_udev(e),
+                                                             udev_list_entry_get_name(entry));
+                       device_parent = udev_device_get_parent(device);
+                       /* Not need unref device_parent. device_parent and device have same refcnt */
+                       if (device_parent) {
+                               if (strcmp(udev_device_get_sysname(device_parent), "nexell-drm") == 0) {
+                                       drm_device = device;
+                                       TBM_DBG("Found render device: '%s' (%s)\n",
+                                           udev_device_get_syspath(drm_device),
+                                           udev_device_get_sysname(device_parent));
+                                       break;
+                               }
+                       }
+                       udev_device_unref(device);
+               }
+
+               udev_enumerate_unref(e);
+
+               /* Get device file path. */
+               filepath = udev_device_get_devnode(drm_device);
+               if (!filepath) {
+                       TBM_ERR("udev_device_get_devnode() failed.\n");
+                       udev_device_unref(drm_device);
+                       udev_unref(udev);
+                       return -1;
+               }
+
+               /* Open DRM device file and check validity. */
+               fd = open(filepath, O_RDWR | O_CLOEXEC);
+               if (fd < 0) {
+                       TBM_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
+                       udev_device_unref(drm_device);
+                       udev_unref(udev);
+                       return -1;
+               }
+
+               ret = fstat(fd, &s);
+               if (ret) {
+                       TBM_ERR("fstat() failed %s.\n");
+                       close(fd);
+                       udev_device_unref(drm_device);
+                       udev_unref(udev);
+                       return -1;
+               }
+
+               udev_device_unref(drm_device);
+               udev_unref(udev);
+       }
+
+       return fd;
+}
+
+static int
+_check_render_node(void)
+{
+#ifndef USE_RENDER_NODE
+       return 0;
+#else
+       struct udev *udev = NULL;
+       struct udev_enumerate *e = NULL;
+       struct udev_list_entry *entry = NULL;
+       struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL;
+
+       udev = udev_new();
+       if (!udev) {
+               TBM_ERR("udev_new() failed.\n");
+               return -1;
+       }
+
+       e = udev_enumerate_new(udev);
+       udev_enumerate_add_match_subsystem(e, "drm");
+       udev_enumerate_add_match_sysname(e, "renderD[0-9]*");
+       udev_enumerate_scan_devices(e);
+
+       udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+               device = udev_device_new_from_syspath(udev_enumerate_get_udev(e),
+                                                     udev_list_entry_get_name(entry));
+               device_parent = udev_device_get_parent(device);
+               /* Not need unref device_parent. device_parent and device have same refcnt */
+               if (device_parent) {
+                       if (strcmp(udev_device_get_sysname(device_parent), "nexell-drm") == 0) {
+                               drm_device = device;
+                               TBM_DBG("Found render device: '%s' (%s)\n",
+                                   udev_device_get_syspath(drm_device),
+                                   udev_device_get_sysname(device_parent));
+                               break;
+                       }
+               }
+               udev_device_unref(device);
+       }
+
+       udev_enumerate_unref(e);
+       udev_unref(udev);
+
+       if (!drm_device) {
+               udev_device_unref(drm_device);
+               return 0;
+       }
+
+       udev_device_unref(drm_device);
+       return 1;
+#endif
+}
+
+static int
+_get_render_node(void)
+{
+       struct udev *udev = NULL;
+       struct udev_enumerate *e = NULL;
+       struct udev_list_entry *entry = NULL;
+       struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL;
+       const char *filepath;
+       struct stat s;
+       int fd = -1;
+       int ret;
+
+       udev = udev_new();
+       if (!udev) {
+               TBM_ERR("udev_new() failed.\n");
+               return -1;
+       }
+
+       e = udev_enumerate_new(udev);
+       udev_enumerate_add_match_subsystem(e, "drm");
+       udev_enumerate_add_match_sysname(e, "renderD[0-9]*");
+       udev_enumerate_scan_devices(e);
+
+       udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+               device = udev_device_new_from_syspath(udev_enumerate_get_udev(e),
+                                                     udev_list_entry_get_name(entry));
+               device_parent = udev_device_get_parent(device);
+               /* Not need unref device_parent. device_parent and device have same refcnt */
+               if (device_parent) {
+                       if (strcmp(udev_device_get_sysname(device_parent), "nexell-drm") == 0) {
+                               drm_device = device;
+                               TBM_DBG("Found render device: '%s' (%s)\n",
+                                   udev_device_get_syspath(drm_device),
+                                   udev_device_get_sysname(device_parent));
+                               break;
+                       }
+               }
+               udev_device_unref(device);
+       }
+
+       udev_enumerate_unref(e);
+
+       /* Get device file path. */
+       filepath = udev_device_get_devnode(drm_device);
+       if (!filepath) {
+               TBM_ERR("udev_device_get_devnode() failed.\n");
+               udev_device_unref(drm_device);
+               udev_unref(udev);
+               return -1;
+       }
+
+       /* Open DRM device file and check validity. */
+       fd = open(filepath, O_RDWR | O_CLOEXEC);
+       if (fd < 0) {
+               TBM_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
+               udev_device_unref(drm_device);
+               udev_unref(udev);
+               return -1;
+       }
+
+       ret = fstat(fd, &s);
+       if (ret) {
+               TBM_ERR("fstat() failed %s.\n");
+               udev_device_unref(drm_device);
+               udev_unref(udev);
+               close(fd);
+               return -1;
+       }
+
+       udev_device_unref(drm_device);
+       udev_unref(udev);
+
+       return fd;
+}
+
+static unsigned int
+_get_nexell_flag_from_tbm(unsigned int ftbm)
+{
+       unsigned int flags = 0;
+
+       switch (ftbm) {
+       /*
+        * As default, allocate DMA buffer with physically non-contiguous
+        * and cachable attributes for Wayland clients who consider CPU
+        * access to DMA buffer.
+        */
+       case TBM_BO_DEFAULT:
+               flags = NEXELL_BO_SYSTEM_NONCONTIG_CACHEABLE;
+               break;
+       /*
+        * Nexell Display controller has no IOMMU so alloate DMA buffer
+        * with phycially contiguous memory and non-cachable attributes
+        * for SCANOUT buffer.
+        */
+       case TBM_BO_SCANOUT:
+               flags = NEXELL_BO_DMA;
+               break;
+       /*
+        * Allocate DMA buffer with physically non-contiguous
+        * and non-cachable attributes for Wayland clients
+        * who never consider CPU access to DMA buffer.
+        */
+       case TBM_BO_WC:
+       case TBM_BO_NONCACHABLE:
+               flags = NEXELL_BO_SYSTEM_NONCONTIG;
+               break;
+       default:
+               flags = NEXELL_BO_DMA;
+               break;
+       }
+
+       return flags;
+}
+
+static unsigned int
+_get_tbm_flag_from_nexell(unsigned int fnexell)
+{
+       unsigned int flags = 0;
+
+       switch (fnexell) {
+       case NEXELL_BO_SYSTEM_NONCONTIG_CACHEABLE:
+               flags = TBM_BO_DEFAULT;
+               break;
+       case NEXELL_BO_DMA:
+               flags = TBM_BO_SCANOUT;
+               break;
+       case NEXELL_BO_SYSTEM_NONCONTIG:
+               flags = TBM_BO_WC|TBM_BO_NONCACHABLE;
+               break;
+       default:
+               flags = TBM_BO_SCANOUT;
+               break;
+       }
+
+       return flags;
+}
+
+static unsigned int
+_get_name(int fd, unsigned int gem)
+{
+       struct drm_gem_flink arg = {0,};
+
+       arg.handle = gem;
+       if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) {
+               TBM_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d", gem);
+               return 0;
+       }
+
+       return (unsigned int)arg.name;
+}
+
+static tbm_bo_handle
+_nexell_bo_handle(tbm_bo_nexell bo_nexell, int device)
+{
+       tbm_bo_handle bo_handle;
+
+       memset(&bo_handle, 0x0, sizeof(uint64_t));
+
+       switch (device) {
+       case TBM_DEVICE_DEFAULT:
+       case TBM_DEVICE_2D:
+               bo_handle.u32 = (uint32_t)bo_nexell->gem;
+               break;
+       case TBM_DEVICE_CPU:
+               if (!bo_nexell->pBase) {
+                       struct drm_mode_map_dumb arg = {0,};
+                       void *map = NULL;
+
+                       arg.handle = bo_nexell->gem;
+                       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg)) {
+                               TBM_ERR("Cannot map_dumb gem=%d\n", bo_nexell->gem);
+                               return (tbm_bo_handle) NULL;
+                       }
+
+                       map = mmap(NULL, bo_nexell->size, PROT_READ | PROT_WRITE, MAP_SHARED,
+                                  bo_nexell->fd, arg.offset);
+                       if (map == MAP_FAILED) {
+                               TBM_ERR("Cannot usrptr gem=%d\n", bo_nexell->gem);
+                               return (tbm_bo_handle) NULL;
+                       }
+                       bo_nexell->pBase = map;
+               }
+               bo_handle.ptr = (void *)bo_nexell->pBase;
+               break;
+       case TBM_DEVICE_3D:
+       case TBM_DEVICE_MM:
+               if (!bo_nexell->dmabuf) {
+                       struct drm_prime_handle arg = {0, };
+
+                       arg.handle = bo_nexell->gem;
+                       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                               TBM_ERR("Cannot dmabuf=%d\n", bo_nexell->gem);
+                               return (tbm_bo_handle) NULL;
+                       }
+                       bo_nexell->dmabuf = arg.fd;
+               }
+
+               bo_handle.u32 = (uint32_t)bo_nexell->dmabuf;
+               break;
+       default:
+               TBM_ERR("Not supported device:%d\n", device);
+               bo_handle.ptr = (void *) NULL;
+               break;
+       }
+
+       return bo_handle;
+}
+
+static int
+_calc_yplane_nv12(int width, int height)
+{
+       int mbX, mbY;
+
+       mbX = SIZE_ALIGN(width + 24, S5P_FIMV_NV12MT_HALIGN);
+       mbY = SIZE_ALIGN(height + 16, S5P_FIMV_NV12MT_VALIGN);
+
+       return SIZE_ALIGN(mbX * mbY, S5P_FIMV_DEC_BUF_ALIGN);
+}
+
+static int
+_calc_uvplane_nv12(int width, int height)
+{
+       int mbX, mbY;
+
+       mbX = SIZE_ALIGN(width + 16, S5P_FIMV_NV12MT_HALIGN);
+       mbY = SIZE_ALIGN(height + 4, S5P_FIMV_NV12MT_VALIGN);
+
+       return SIZE_ALIGN((mbX * mbY) >> 1, S5P_FIMV_DEC_BUF_ALIGN);
+}
+
+static int
+_new_calc_plane_nv12(int width, int height)
+{
+       int mbX, mbY;
+
+       mbX = DIV_ROUND_UP(width, S5P_FIMV_NUM_PIXELS_IN_MB_ROW);
+       mbY = DIV_ROUND_UP(height, S5P_FIMV_NUM_PIXELS_IN_MB_COL);
+
+       if (width * height < S5P_FIMV_MAX_FRAME_SIZE)
+               mbY = (mbY + 1) / 2 * 2;
+
+       return ((mbX * S5P_FIMV_NUM_PIXELS_IN_MB_COL) * (mbY *
+                       S5P_FIMV_NUM_PIXELS_IN_MB_ROW));
+}
+
+static int
+_new_calc_yplane_nv12(int width, int height)
+{
+       return SIZE_ALIGN(_new_calc_plane_nv12(width,
+                                               height) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
+                          TBM_SURFACE_ALIGNMENT_PLANE_NV12);
+}
+
+static int
+_new_calc_uvplane_nv12(int width, int height)
+{
+       return SIZE_ALIGN((_new_calc_plane_nv12(width,
+                                               height) >> 1) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
+                         TBM_SURFACE_ALIGNMENT_PLANE_NV12);
+}
+
+static tbm_bufmgr_capability
+tbm_nexell_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error)
+{
+       tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE;
+
+       capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD;
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return capabilities;
+}
+
+static tbm_error_e
+tbm_nexell_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, TBM_ERROR_INVALID_PARAMETER);
+
+       if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_nexell->fd,
+                                          bufmgr_nexell->device_name, 0)) {
+               TBM_ERR("fail to tbm_drm_helper_wl_server_init\n");
+               return TBM_ERROR_INVALID_OPERATION;
+       }
+
+       bufmgr_nexell->bind_display = native_display;
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_error_e
+tbm_nexell_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data,
+                                                       uint32_t **formats, uint32_t *num)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       uint32_t *color_formats;
+
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, TBM_ERROR_INVALID_PARAMETER);
+
+       color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
+       if (color_formats == NULL)
+               return TBM_ERROR_OUT_OF_MEMORY;
+
+       memcpy(color_formats, tbm_nexell_color_format_list, sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT);
+
+       *formats = color_formats;
+       *num = TBM_COLOR_FORMAT_COUNT;
+
+       TBM_DBG("supported format count = %d\n", *num);
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_error_e
+tbm_nexell_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
+                                                       tbm_format format, int plane_idx, int width,
+                                                       int height, uint32_t *size, uint32_t *offset,
+                                                       uint32_t *pitch, int *bo_idx)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       int bpp;
+       int _offset = 0;
+       int _pitch = 0;
+       int _size = 0;
+       int _bo_idx = 0;
+
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, TBM_ERROR_INVALID_PARAMETER);
+
+       switch (format) {
+               /* 16 bpp RGB */
+       case TBM_FORMAT_XRGB4444:
+       case TBM_FORMAT_XBGR4444:
+       case TBM_FORMAT_RGBX4444:
+       case TBM_FORMAT_BGRX4444:
+       case TBM_FORMAT_ARGB4444:
+       case TBM_FORMAT_ABGR4444:
+       case TBM_FORMAT_RGBA4444:
+       case TBM_FORMAT_BGRA4444:
+       case TBM_FORMAT_XRGB1555:
+       case TBM_FORMAT_XBGR1555:
+       case TBM_FORMAT_RGBX5551:
+       case TBM_FORMAT_BGRX5551:
+       case TBM_FORMAT_ARGB1555:
+       case TBM_FORMAT_ABGR1555:
+       case TBM_FORMAT_RGBA5551:
+       case TBM_FORMAT_BGRA5551:
+       case TBM_FORMAT_RGB565:
+               bpp = 16;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+               /* 24 bpp RGB */
+       case TBM_FORMAT_RGB888:
+       case TBM_FORMAT_BGR888:
+               bpp = 24;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+               /* 32 bpp RGB */
+       case TBM_FORMAT_XRGB8888:
+       case TBM_FORMAT_XBGR8888:
+       case TBM_FORMAT_RGBX8888:
+       case TBM_FORMAT_BGRX8888:
+       case TBM_FORMAT_ARGB8888:
+       case TBM_FORMAT_ABGR8888:
+       case TBM_FORMAT_RGBA8888:
+       case TBM_FORMAT_BGRA8888:
+               bpp = 32;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+
+               /* packed YCbCr */
+       case TBM_FORMAT_YUYV:
+       case TBM_FORMAT_YVYU:
+       case TBM_FORMAT_UYVY:
+       case TBM_FORMAT_VYUY:
+       case TBM_FORMAT_AYUV:
+               bpp = 32;
+               _offset = 0;
+               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+               _bo_idx = 0;
+               break;
+
+               /*
+               * 2 plane YCbCr
+               * index 0 = Y plane, [7:0] Y
+               * index 1 = Cr:Cb plane, [15:0] Cr:Cb little endian
+               * or
+               * index 1 = Cb:Cr plane, [15:0] Cb:Cr little endian
+               */
+       case TBM_FORMAT_NV12:
+       case TBM_FORMAT_NV21:
+               bpp = 12;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = MAX(_calc_yplane_nv12(width, height),
+                                               _new_calc_yplane_nv12(width, height));
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if (plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = MAX(_calc_uvplane_nv12(width, height),
+                                               _new_calc_uvplane_nv12(width, height));
+                       _bo_idx = 0;
+               }
+               break;
+       case TBM_FORMAT_NV16:
+       case TBM_FORMAT_NV61:
+               bpp = 16;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if( plane_idx ==1 )*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+
+               /*
+               * 3 plane YCbCr
+               * index 0: Y plane, [7:0] Y
+               * index 1: Cb plane, [7:0] Cb
+               * index 2: Cr plane, [7:0] Cr
+               * or
+               * index 1: Cr plane, [7:0] Cr
+               * index 2: Cb plane, [7:0] Cb
+               */
+               /*
+               * NATIVE_BUFFER_FORMAT_YV12
+               * NATIVE_BUFFER_FORMAT_I420
+               */
+       case TBM_FORMAT_YUV410:
+       case TBM_FORMAT_YVU410:
+               bpp = 9;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
+                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
+                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+       case TBM_FORMAT_YUV411:
+       case TBM_FORMAT_YVU411:
+       case TBM_FORMAT_YUV420:
+       case TBM_FORMAT_YVU420:
+               bpp = 12;
+               /* plane_idx == 0 */
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = _pitch * SIZE_ALIGN(height, 16);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /* plane_idx == 1 */
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(_pitch / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = _pitch * SIZE_ALIGN(height >> 1, 16);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /* plane_idx == 2 */
+               {
+                       _offset += _size;
+               }
+               break;
+       case TBM_FORMAT_YUV422:
+       case TBM_FORMAT_YVU422:
+               bpp = 16;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+       case TBM_FORMAT_YUV444:
+       case TBM_FORMAT_YVU444:
+               bpp = 24;
+               /*if(plane_idx == 0)*/
+               {
+                       _offset = 0;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 0)
+                               break;
+               }
+               /*else if(plane_idx == 1)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+                       if (plane_idx == 1)
+                               break;
+               }
+               /*else if (plane_idx == 2)*/
+               {
+                       _offset += _size;
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _bo_idx = 0;
+               }
+               break;
+       default:
+               bpp = 0;
+               break;
+       }
+
+       *size = _size;
+       *offset = _offset;
+       *pitch = _pitch;
+       *bo_idx = _bo_idx;
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_backend_bo_data *
+tbm_nexell_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size,
+                                       tbm_bo_memory_type flags, tbm_error_e *error)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       tbm_bo_nexell bo_nexell;
+       unsigned int nexell_flags;
+
+       if (bufmgr_nexell == NULL) {
+               TBM_ERR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       bo_nexell = calloc(1, sizeof(struct _tbm_bo_nexell));
+       if (!bo_nexell) {
+               TBM_ERR("fail to allocate the bo_nexell private\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_nexell->bufmgr_nexell = bufmgr_nexell;
+
+       nexell_flags = _get_nexell_flag_from_tbm(flags);
+
+       struct nx_drm_gem_create arg = {0, };
+
+       arg.size = (uint64_t)size;
+       arg.flags = nexell_flags;
+       if (drmCommandWriteRead(bufmgr_nexell->fd, DRM_NX_GEM_CREATE, &arg,
+                               sizeof(arg))) {
+               TBM_ERR("Cannot create bo_nexell(flag:%x, size:%d)\n", arg.flags,
+                              (unsigned int)arg.size);
+               free(bo_nexell);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       bo_nexell->fd = bufmgr_nexell->fd;
+       bo_nexell->gem = arg.handle;
+       bo_nexell->size = size;
+       bo_nexell->flags_tbm = flags;
+       bo_nexell->flags_nexell = nexell_flags;
+       bo_nexell->name = _get_name(bo_nexell->fd, bo_nexell->gem);
+
+       if (!_bo_init_cache_state(bufmgr_nexell, bo_nexell, 0)) {
+               TBM_ERR("fail init cache state(%d)\n", bo_nexell->name);
+               free(bo_nexell);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       pthread_mutex_init(&bo_nexell->mutex, NULL);
+
+       if (!bo_nexell->dmabuf) {
+               struct drm_prime_handle arg = {0, };
+
+               arg.handle = bo_nexell->gem;
+               if (drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_ERR("Cannot dmabuf=%d\n", bo_nexell->gem);
+                       free(bo_nexell);
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       return NULL;
+               }
+               bo_nexell->dmabuf = arg.fd;
+       }
+
+       /* add bo_nexell to hash */
+       if (drmHashInsert(bufmgr_nexell->hashBos, bo_nexell->name, (void *)bo_nexell) < 0)
+               TBM_ERR("Cannot insert bo_nexell to Hash(%d)\n", bo_nexell->name);
+
+       TBM_DBG("     bo_nexell:%p, gem:%d(%d), flags:%d(%d), size:%d\n",
+               bo_nexell,
+               bo_nexell->gem, bo_nexell->name,
+               flags, nexell_flags,
+               bo_nexell->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_nexell;
+}
+
+static tbm_backend_bo_data *
+tbm_nexell_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_error_e *error)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       tbm_bo_nexell bo_nexell;
+       unsigned int gem = 0;
+       unsigned int name;
+       int ret;
+       char buf[STRERR_BUFSIZE];
+
+       if (bufmgr_nexell == NULL) {
+               TBM_ERR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       /*getting handle from fd*/
+       struct drm_prime_handle arg = {0, };
+
+       arg.fd = key;
+       arg.flags = 0;
+       if (drmIoctl(bufmgr_nexell->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) {
+               TBM_ERR("Cannot get gem handle from fd:%d (%s)\n",
+                              arg.fd, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+       gem = arg.handle;
+
+       name = _get_name(bufmgr_nexell->fd, gem);
+       if (!name) {
+               TBM_ERR("Cannot get name from gem:%d, fd:%d (%s)\n",
+                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       ret = drmHashLookup(bufmgr_nexell->hashBos, name, (void **)&bo_nexell);
+       if (ret == 0) {
+               if (gem == bo_nexell->gem) {
+                       if (error)
+                               *error = TBM_ERROR_NONE;
+                       return bo_nexell;
+               }
+       }
+
+       /* Determine size of bo.  The fd-to-handle ioctl really should
+        * return the size, but it doesn't.  If we have kernel 3.12 or
+        * later, we can lseek on the prime fd to get the size.  Older
+        * kernels will just fail, in which case we fall back to the
+        * provided (estimated or guess size).
+        */
+       unsigned int real_size = -1;
+       struct nx_drm_gem_info info = {0, };
+
+       real_size = lseek(key, 0, SEEK_END);
+
+       info.handle = gem;
+       if (drmCommandWriteRead(bufmgr_nexell->fd,
+                               DRM_NX_GEM_GET,
+                               &info,
+                               sizeof(struct nx_drm_gem_info))) {
+               TBM_ERR("Cannot get gem info from gem:%d, fd:%d (%s)\n",
+                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       if (real_size == -1)
+               real_size = info.size;
+
+       bo_nexell = calloc(1, sizeof(struct _tbm_bo_nexell));
+       if (!bo_nexell) {
+               TBM_ERR("bo_nexell:%p fail to allocate the bo_nexell\n", bo_nexell);
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_nexell->bufmgr_nexell = bufmgr_nexell;
+
+       bo_nexell->fd = bufmgr_nexell->fd;
+       bo_nexell->gem = gem;
+       bo_nexell->size = real_size;
+       bo_nexell->flags_nexell = info.flags;
+       bo_nexell->flags_tbm = _get_tbm_flag_from_nexell(bo_nexell->flags_nexell);
+       bo_nexell->name = name;
+
+       if (!_bo_init_cache_state(bufmgr_nexell, bo_nexell, 1)) {
+               TBM_ERR("fail init cache state(%d)\n", bo_nexell->name);
+               free(bo_nexell);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       /* add bo_nexell to hash */
+       if (drmHashInsert(bufmgr_nexell->hashBos, bo_nexell->name, (void *)bo_nexell) < 0)
+               TBM_ERR("bo_nexell:%p Cannot insert bo_nexell to Hash(%d) from gem:%d, fd:%d\n",
+                              bo_nexell, bo_nexell->name, gem, key);
+
+       TBM_DBG(" bo_nexell:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           key,
+           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
+           bo_nexell->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_nexell;
+}
+
+static tbm_backend_bo_data *
+tbm_nexell_bufmgr_import(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       tbm_bo_nexell bo_nexell;
+       int ret;
+
+       if (bufmgr_nexell == NULL) {
+               TBM_ERR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       ret = drmHashLookup(bufmgr_nexell->hashBos, key, (void **)&bo_nexell);
+       if (ret == 0) {
+               if (error)
+                       *error = TBM_ERROR_NONE;
+               return (tbm_backend_bo_data *)bo_nexell;
+       }
+
+       struct drm_gem_open arg = {0, };
+       struct nx_drm_gem_info info = {0, };
+
+       arg.name = key;
+       if (drmIoctl(bufmgr_nexell->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
+               TBM_ERR("Cannot open gem name=%d\n", key);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       info.handle = arg.handle;
+       if (drmCommandWriteRead(bufmgr_nexell->fd,
+                               DRM_NX_GEM_GET,
+                               &info,
+                               sizeof(struct nx_drm_gem_info))) {
+               TBM_ERR("Cannot get gem info=%d\n", key);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       bo_nexell = calloc(1, sizeof(struct _tbm_bo_nexell));
+       if (!bo_nexell) {
+               TBM_ERR("fail to allocate the bo_nexell private\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_nexell->bufmgr_nexell = bufmgr_nexell;
+
+       bo_nexell->fd = bufmgr_nexell->fd;
+       bo_nexell->gem = arg.handle;
+       bo_nexell->size = arg.size;
+       bo_nexell->flags_nexell = info.flags;
+       bo_nexell->name = key;
+       bo_nexell->flags_tbm = _get_tbm_flag_from_nexell(bo_nexell->flags_nexell);
+
+       if (!_bo_init_cache_state(bufmgr_nexell, bo_nexell, 1)) {
+               TBM_ERR("fail init cache state(%d)\n", bo_nexell->name);
+               free(bo_nexell);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       if (!bo_nexell->dmabuf) {
+               struct drm_prime_handle arg = {0, };
+
+               arg.handle = bo_nexell->gem;
+               if (drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem=%d\n", bo_nexell->gem);
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       free(bo_nexell);
+                       return NULL;
+               }
+               bo_nexell->dmabuf = arg.fd;
+       }
+
+       /* add bo_nexell to hash */
+       if (drmHashInsert(bufmgr_nexell->hashBos, bo_nexell->name, (void *)bo_nexell) < 0)
+               TBM_ERR("Cannot insert bo_nexell to Hash(%d)\n", bo_nexell->name);
+
+
+       TBM_DBG("    bo_nexell:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
+           bo_nexell->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_nexell;
+}
+
+static void
+tbm_nexell_bo_free(tbm_backend_bo_data *bo_data)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+       tbm_bo_nexell temp;
+       tbm_bufmgr_nexell bufmgr_nexell;
+       char buf[STRERR_BUFSIZE];
+       int ret;
+
+       if (!bo_data)
+               return;
+
+       bufmgr_nexell = bo_nexell->bufmgr_nexell;
+       if (!bufmgr_nexell)
+               return;
+
+       TBM_DBG("      bo_nexell:%p, gem:%d(%d), fd:%d, size:%d\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           bo_nexell->size);
+
+       if (bo_nexell->pBase) {
+               if (munmap(bo_nexell->pBase, bo_nexell->size) == -1) {
+                       TBM_ERR("bo_nexell:%p fail to munmap(%s)\n",
+                                      bo_nexell, strerror_r(errno, buf, STRERR_BUFSIZE));
+               }
+       }
+
+       /* close dmabuf */
+       if (bo_nexell->dmabuf) {
+               close(bo_nexell->dmabuf);
+               bo_nexell->dmabuf = 0;
+       }
+
+       /* delete bo from hash */
+       ret = drmHashLookup(bufmgr_nexell->hashBos, bo_nexell->name,
+                            (void **)&temp);
+       if (ret == 0)
+               drmHashDelete(bufmgr_nexell->hashBos, bo_nexell->name);
+       else
+               TBM_ERR("Cannot find bo_nexell to Hash(%d), ret=%d\n", bo_nexell->name, ret);
+
+       if (temp != bo_nexell)
+               TBM_ERR("hashBos probably has several BOs with same name!!!\n");
+
+       _bo_destroy_cache_state(bufmgr_nexell, bo_nexell);
+
+       /* Free gem handle */
+       struct drm_gem_close arg = {0, };
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo_nexell->gem;
+       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_GEM_CLOSE, &arg))
+               TBM_ERR("bo_nexell:%p fail to gem close.(%s)\n",
+                              bo_nexell, strerror_r(errno, buf, STRERR_BUFSIZE));
+
+       free(bo_nexell);
+}
+
+static int
+tbm_nexell_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+
+       if (!bo_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return 0;
+       }
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return bo_nexell->size;
+}
+
+static tbm_bo_memory_type
+tbm_nexell_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+
+       if (!bo_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return TBM_BO_DEFAULT;
+       }
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return bo_nexell->flags_tbm;
+}
+
+static tbm_bo_handle
+tbm_nexell_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+       tbm_bo_handle bo_handle;
+
+       if (!bo_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       if (!bo_nexell->gem) {
+               TBM_ERR("Cannot map gem=%d\n", bo_nexell->gem);
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       TBM_DBG("bo_nexell:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d, %s\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
+           bo_nexell->size,
+           STR_DEVICE[device]);
+
+       /*Get mapped bo_handle*/
+       bo_handle = _nexell_bo_handle(bo_nexell, device);
+       if (bo_handle.ptr == NULL) {
+               TBM_ERR("Cannot get handle: gem:%d, device:%d\n",
+                       bo_nexell->gem, device);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return (tbm_bo_handle) NULL;
+       }
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return bo_handle;
+}
+
+static tbm_bo_handle
+tbm_nexell_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
+                               tbm_bo_access_option opt, tbm_error_e *error)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+       tbm_bo_handle bo_handle;
+       tbm_bufmgr_nexell bufmgr_nexell;
+
+       if (!bo_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       bufmgr_nexell = bo_nexell->bufmgr_nexell;
+       if (!bufmgr_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       if (!bo_nexell->gem) {
+               TBM_ERR("Cannot map gem=%d\n", bo_nexell->gem);
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       TBM_DBG("       bo_nexell:%p, gem:%d(%d), fd:%d, %s, %s\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           STR_DEVICE[device],
+           STR_OPT[opt]);
+
+       /*Get mapped bo_handle*/
+       bo_handle = _nexell_bo_handle(bo_nexell, device);
+       if (bo_handle.ptr == NULL) {
+               TBM_ERR("Cannot get handle: gem:%d, device:%d, opt:%d\n",
+                              bo_nexell->gem, device, opt);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return (tbm_bo_handle) NULL;
+       }
+
+       if (bo_nexell->map_cnt == 0)
+               _bo_set_cache_state(bufmgr_nexell, bo_nexell, device, opt);
+
+       bo_nexell->map_cnt++;
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return bo_handle;
+}
+
+static tbm_error_e
+tbm_nexell_bo_unmap(tbm_backend_bo_data *bo_data)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+       tbm_bufmgr_nexell bufmgr_nexell;
+
+       if (!bo_nexell)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       bufmgr_nexell = bo_nexell->bufmgr_nexell;
+       if (!bufmgr_nexell)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       if (!bo_nexell->gem)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       bo_nexell->map_cnt--;
+
+       if (bo_nexell->map_cnt == 0)
+               _bo_save_cache_state(bufmgr_nexell, bo_nexell);
+
+       TBM_DBG("     bo_nexell:%p, gem:%d(%d), fd:%d\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf);
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_error_e
+tbm_nexell_bo_lock(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
+                               tbm_bo_access_option opt)
+{
+#ifndef ALWAYS_BACKEND_CTRL
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+       tbm_bufmgr_nexell bufmgr_nexell;
+
+       if (!bo_nexell)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       bufmgr_nexell = bo_nexell->bufmgr_nexell;
+       if (!bufmgr_nexell)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       if (device != TBM_DEVICE_3D && device != TBM_DEVICE_CPU) {
+               TBM_DBG("Not support device type,\n");
+               return TBM_ERROR_INVALID_OPERATION;
+       }
+#endif /* ALWAYS_BACKEND_CTRL */
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_error_e
+tbm_nexell_bo_unlock(tbm_backend_bo_data *bo_data)
+{
+#ifndef ALWAYS_BACKEND_CTRL
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+
+       if (!bo_nexell)
+               return TBM_ERROR_INVALID_PARAMETER;
+#endif /* ALWAYS_BACKEND_CTRL */
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_fd
+tbm_nexell_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+       int ret;
+       char buf[STRERR_BUFSIZE];
+
+       if (!bo_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return -1;
+       }
+
+       struct drm_prime_handle arg = {0, };
+
+       arg.handle = bo_nexell->gem;
+       ret = drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
+       if (ret) {
+               TBM_ERR("bo_nexell:%p Cannot dmabuf=%d (%s)\n",
+                              bo_nexell, bo_nexell->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return (tbm_fd) ret;
+       }
+
+       TBM_DBG(" bo_nexell:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           arg.fd,
+           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
+           bo_nexell->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_fd)arg.fd;
+}
+
+static tbm_key
+tbm_nexell_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
+
+       if (!bo_nexell) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return 0;
+       }
+
+       if (!bo_nexell->name) {
+               bo_nexell->name = _get_name(bo_nexell->fd, bo_nexell->gem);
+               if (!bo_nexell->name) {
+                       TBM_ERR("error Cannot get name\n");
+                       if (error)
+                               *error = TBM_ERROR_INVALID_PARAMETER;
+                       return 0;
+               }
+       }
+
+       TBM_DBG("    bo_nexell:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
+           bo_nexell,
+           bo_nexell->gem, bo_nexell->name,
+           bo_nexell->dmabuf,
+           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
+           bo_nexell->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_key)bo_nexell->name;
+}
+
+static void
+tbm_nexell_deinit(tbm_backend_bufmgr_data *bufmgr_data)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
+       tbm_bufmgr bufmgr;
+       tbm_error_e error;
+       unsigned long key;
+       void *value;
+
+       TBM_RETURN_IF_FAIL(bufmgr_nexell != NULL);
+
+       bufmgr = bufmgr_nexell->bufmgr;
+
+       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_nexell->bufmgr_func);
+       tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_nexell->bo_func);
+
+       if (bufmgr_nexell->hashBos) {
+               while (drmHashFirst(bufmgr_nexell->hashBos, &key, &value) > 0) {
+                       free(value);
+                       drmHashDelete(bufmgr_nexell->hashBos, key);
+               }
+
+               drmHashDestroy(bufmgr_nexell->hashBos);
+               bufmgr_nexell->hashBos = NULL;
+       }
+
+       _bufmgr_deinit_cache_state(bufmgr_nexell);
+
+       if (bufmgr_nexell->bind_display)
+               tbm_drm_helper_wl_auth_server_deinit();
+
+       if (bufmgr_nexell->device_name)
+               free(bufmgr_nexell->device_name);
+
+       if (tbm_backend_bufmgr_query_display_server(bufmgr, &error))
+               tbm_drm_helper_unset_tbm_master_fd();
+       else
+               tbm_drm_helper_unset_fd();
+
+       close(bufmgr_nexell->fd);
+
+       free(bufmgr_nexell);
+}
+
+static tbm_backend_bufmgr_data *
+tbm_nexell_init(tbm_bufmgr bufmgr, tbm_error_e *error)
+{
+       tbm_bufmgr_nexell bufmgr_nexell = NULL;
+       tbm_backend_bufmgr_func *bufmgr_func = NULL;
+       tbm_backend_bo_func *bo_func = NULL;
+       tbm_error_e err;
+       int set_master = 0;
+
+       if (!bufmgr) {
+               TBM_ERR("bufmgr is null.\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       bufmgr_nexell = calloc(1, sizeof(struct _tbm_bufmgr_nexell));
+       if (!bufmgr_nexell) {
+               TBM_ERR("fail to alloc bufmgr_nexell!\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+
+       /* check the master_fd which already had opened */
+       bufmgr_nexell->fd = tbm_drm_helper_get_master_fd();
+       if (bufmgr_nexell->fd < 0) {
+               bufmgr_nexell->fd = _tbm_nexell_open_drm();
+               if (bufmgr_nexell->fd < 0) {
+                       TBM_ERR("fail to open drm!\n");
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       goto fail_open_drm;
+               }
+               if (drmIsMaster(bufmgr_nexell->fd)) {
+                       tbm_drm_helper_set_tbm_master_fd(bufmgr_nexell->fd);
+                       set_master = 1;
+
+                       bufmgr_nexell->device_name = drmGetDeviceNameFromFd(bufmgr_nexell->fd);
+                       if (!bufmgr_nexell->device_name) {
+                               TBM_ERR("fail to get device name!\n");
+                               tbm_drm_helper_unset_tbm_master_fd();
+                               if (error)
+                                       *error = TBM_ERROR_INVALID_OPERATION;
+                               goto fail_get_device_name;
+                       }
+                       TBM_INFO("This is Master FD(%d) from open_drm.", bufmgr_nexell->fd);
+               } else {
+                       /* close the fd and get the authenticated fd from the master fd */
+                       close(bufmgr_nexell->fd);
+                       bufmgr_nexell->fd = -1;
+
+                       if (_check_render_node()) {
+                               bufmgr_nexell->fd = _get_render_node();//TODO
+                               if (bufmgr_nexell->fd < 0) {
+                                       TBM_ERR("fail to get render node\n");
+                                       if (error)
+                                               *error = TBM_ERROR_INVALID_OPERATION;
+                                       goto fail_get_render_node;
+                               }
+                               TBM_INFO("Use render node:%d\n", bufmgr_nexell->fd);
+                       } else {
+                               /* get the authenticated drm fd from the master fd */
+                               if (!tbm_drm_helper_get_auth_info(&(bufmgr_nexell->fd), &(bufmgr_nexell->device_name), NULL)) {
+                                       TBM_ERR("fail to get auth drm info!\n");
+                                       if (error)
+                                               *error = TBM_ERROR_INVALID_OPERATION;
+                                       goto fail_get_auth_info;
+                               }
+                               TBM_INFO("This is Authenticated FD(%d)", bufmgr_nexell->fd);
+                       }
+               }
+       } else {
+               bufmgr_nexell->device_name = drmGetDeviceNameFromFd(bufmgr_nexell->fd);
+               if (!bufmgr_nexell->device_name) {
+                       TBM_ERR("fail to get device name!\n");
+                       tbm_drm_helper_unset_tbm_master_fd();
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       goto fail_get_device_name;
+               }
+               TBM_INFO("This is Master FD from tbm_drm_helper_get_master_fd(%d)", bufmgr_nexell->fd);
+       }
+       tbm_drm_helper_set_fd(bufmgr_nexell->fd);
+
+       if (!_bufmgr_init_cache_state(bufmgr_nexell)) {
+               TBM_ERR("fail to init bufmgr cache state\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               goto fail_init_cache_state;
+       }
+
+       /*Create Hash Table*/
+       bufmgr_nexell->hashBos = drmHashCreate();
+
+       /* alloc and register bufmgr_funcs */
+       bufmgr_func = tbm_backend_bufmgr_alloc_bufmgr_func(bufmgr, &err);
+       if (!bufmgr_func) {
+               TBM_ERR("fail to alloc bufmgr_func! err(%d)\n", err);
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto fail_alloc_bufmgr_func;
+       }
+
+       bufmgr_func->bufmgr_get_capabilities = tbm_nexell_bufmgr_get_capabilities;
+       bufmgr_func->bufmgr_bind_native_display = tbm_nexell_bufmgr_bind_native_display;
+       bufmgr_func->bufmgr_get_supported_formats = tbm_nexell_bufmgr_get_supported_formats;
+       bufmgr_func->bufmgr_get_plane_data = tbm_nexell_bufmgr_get_plane_data;
+       bufmgr_func->bufmgr_alloc_bo = tbm_nexell_bufmgr_alloc_bo;
+       bufmgr_func->bufmgr_alloc_bo_with_format = NULL;
+       bufmgr_func->bufmgr_import_fd = tbm_nexell_bufmgr_import_fd;
+       bufmgr_func->bufmgr_import_key = tbm_nexell_bufmgr_import;
+
+       err = tbm_backend_bufmgr_register_bufmgr_func(bufmgr, bufmgr_func);
+       if (err != TBM_ERROR_NONE) {
+               TBM_ERR("fail to register bufmgr_func! err(%d)\n", err);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               goto fail_register_bufmgr_func;
+       }
+       bufmgr_nexell->bufmgr_func = bufmgr_func;
+
+       /* alloc and register bo_funcs */
+       bo_func = tbm_backend_bufmgr_alloc_bo_func(bufmgr, &err);
+       if (!bo_func) {
+               TBM_ERR("fail to alloc bo_func! err(%d)\n", err);
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto fail_alloc_bo_func;
+       }
+
+       bo_func->bo_free = tbm_nexell_bo_free;
+       bo_func->bo_get_size = tbm_nexell_bo_get_size;
+       bo_func->bo_get_memory_types = tbm_nexell_bo_get_memory_type;
+       bo_func->bo_get_handle = tbm_nexell_bo_get_handle;
+       bo_func->bo_map = tbm_nexell_bo_map;
+       bo_func->bo_unmap = tbm_nexell_bo_unmap;
+       bo_func->bo_lock = tbm_nexell_bo_lock;
+       bo_func->bo_unlock = tbm_nexell_bo_unlock;
+       bo_func->bo_export_fd = tbm_nexell_bo_export_fd;
+       bo_func->bo_export_key = tbm_nexell_bo_export_key;
+
+       err = tbm_backend_bufmgr_register_bo_func(bufmgr, bo_func);
+       if (err != TBM_ERROR_NONE) {
+               TBM_ERR("fail to register bo_func! err(%d)\n", err);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               goto fail_register_bo_func;
+       }
+       bufmgr_nexell->bo_func = bo_func;
+
+       TBM_DBG("drm_fd:%d\n", bufmgr_nexell->fd);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       bufmgr_nexell->bufmgr = bufmgr;
+
+       return (tbm_backend_bufmgr_data *)bufmgr_nexell;
+
+fail_register_bo_func:
+       tbm_backend_bufmgr_free_bo_func(bufmgr, bo_func);
+fail_alloc_bo_func:
+fail_register_bufmgr_func:
+       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_func);
+fail_alloc_bufmgr_func:
+       _bufmgr_deinit_cache_state(bufmgr_nexell);
+       if (bufmgr_nexell->hashBos)
+               drmHashDestroy(bufmgr_nexell->hashBos);
+fail_init_cache_state:
+       if (set_master)
+               tbm_drm_helper_unset_tbm_master_fd();
+       tbm_drm_helper_unset_fd();
+fail_get_device_name:
+       close(bufmgr_nexell->fd);
+fail_get_auth_info:
+fail_get_render_node:
+fail_open_drm:
+       free(bufmgr_nexell);
+       return NULL;
+}
+
+tbm_backend_module tbm_backend_module_data = {
+       "nexell",
+       "Samsung",
+       TBM_BACKEND_ABI_VERSION_3_0,
+       tbm_nexell_init,
+       tbm_nexell_deinit
+};
diff --git a/src/libtbm-nexell/tbm_bufmgr_tgl.h b/src/libtbm-nexell/tbm_bufmgr_tgl.h
new file mode 100644 (file)
index 0000000..fee35d5
--- /dev/null
@@ -0,0 +1,72 @@
+/**************************************************************************
+ *
+ * libtbm
+ *
+ * Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+ * Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+ *
+ * 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, sub license, 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+ *
+ * **************************************************************************/
+
+#ifndef __TBM_BUFMGR_TGL_H__
+#define __TBM_BUFMGR_TGL_H__
+
+#include <linux/ioctl.h>
+
+#ifdef ENABLE_CACHECRTL
+static char tgl_devfile[] = "/dev/slp_global_lock";
+static char tgl_devfile1[] = "/dev/tgl";
+#endif
+
+#define TGL_IOC_BASE                           0x32
+
+struct tgl_attribute {
+       unsigned int key;
+       unsigned int timeout_ms;
+};
+
+struct tgl_user_data {
+       unsigned int key;
+       unsigned int data1;
+       unsigned int data2;
+       unsigned int locked;
+};
+
+typedef enum {
+       _TGL_INIT_LOCK = 1,
+       _TGL_DESTROY_LOCK,
+       _TGL_LOCK_LOCK,
+       _TGL_UNLOCK_LOCK,
+       _TGL_SET_DATA,
+       _TGL_GET_DATA,
+} _tgl_ioctls;
+
+#define TGL_IOC_INIT_LOCK                      _IOW(TGL_IOC_BASE, _TGL_INIT_LOCK, struct tgl_attribute *)
+#define TGL_IOC_DESTROY_LOCK           _IOW(TGL_IOC_BASE, _TGL_DESTROY_LOCK, unsigned int)
+#define TGL_IOC_LOCK_LOCK                      _IOW(TGL_IOC_BASE, _TGL_LOCK_LOCK, unsigned int)
+#define TGL_IOC_UNLOCK_LOCK                    _IOW(TGL_IOC_BASE, _TGL_UNLOCK_LOCK, unsigned int)
+#define TGL_IOC_SET_DATA                       _IOW(TGL_IOC_BASE, _TGL_SET_DATA, struct tgl_user_data *)
+#define TGL_IOC_GET_DATA                       _IOW(TGL_IOC_BASE, _TGL_GET_DATA, struct tgl_user_data *)
+
+#endif                                                 /* __TBM_BUFMGR_TGL_H__ */
diff --git a/src/tbm_bufmgr_nexell.c b/src/tbm_bufmgr_nexell.c
deleted file mode 100644 (file)
index 424a290..0000000
+++ /dev/null
@@ -1,2023 +0,0 @@
-/**************************************************************************
-
-libtbm_nexell
-
-Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
-
-Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
-
-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, sub license, 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 NON-INFRINGEMENT.
-IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
-
-**************************************************************************/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <libudev.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <xf86drm.h>
-#include <nexell_drm.h>
-#include <pthread.h>
-#include <tbm_backend.h>
-#include <tbm_drm_helper.h>
-#include <tbm_log.h>
-#include "tbm_bufmgr_tgl.h"
-
-#define TBM_COLOR_FORMAT_COUNT 6
-
-#define NEXELL_DRM_NAME "nexell"
-
-#define STRERR_BUFSIZE 128
-
-#define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1))
-#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-
-#ifdef ALIGN_EIGHT
-#define TBM_SURFACE_ALIGNMENT_PLANE (8)
-#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (8)
-#else
-#define TBM_SURFACE_ALIGNMENT_PLANE (64)
-#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (64)
-#endif
-
-#define TBM_SURFACE_ALIGNMENT_PLANE_NV12 (4096)
-#define TBM_SURFACE_ALIGNMENT_PITCH_YUV (32)
-
-#define SZ_1M                                   0x00100000
-#define S5P_FIMV_MAX_FRAME_SIZE                 (2 * SZ_1M)
-#define S5P_FIMV_D_ALIGN_PLANE_SIZE             64
-#define S5P_FIMV_NUM_PIXELS_IN_MB_ROW           16
-#define S5P_FIMV_NUM_PIXELS_IN_MB_COL           16
-#define S5P_FIMV_DEC_BUF_ALIGN                  (8 * 1024)
-#define S5P_FIMV_NV12MT_HALIGN                  128
-#define S5P_FIMV_NV12MT_VALIGN                  64
-
-/* tgl key values */
-#define GLOBAL_KEY   ((unsigned int)(-1))
-/* TBM_CACHE */
-#define TBM_NEXELL_CACHE_INV       0x01 /**< cache invalidate  */
-#define TBM_NEXELL_CACHE_CLN       0x02 /**< cache clean */
-#define TBM_NEXELL_CACHE_ALL       0x10 /**< cache all */
-#define TBM_NEXELL_CACHE_FLUSH     (TBM_NEXELL_CACHE_INV|TBM_NEXELL_CACHE_CLN) /**< cache flush  */
-#define TBM_NEXELL_CACHE_FLUSH_ALL (TBM_NEXELL_CACHE_FLUSH|TBM_NEXELL_CACHE_ALL)       /**< cache flush all */
-
-enum {
-       DEVICE_NONE = 0,
-       DEVICE_CA,                                      /* cache aware device */
-       DEVICE_CO                                       /* cache oblivious device */
-};
-
-typedef union _tbm_bo_cache_state tbm_bo_cache_state;
-
-union _tbm_bo_cache_state {
-       unsigned int val;
-       struct {
-               unsigned int cntFlush:16;       /*Flush all index for sync */
-               unsigned int isCached:1;
-               unsigned int isDirtied:2;
-       } data;
-};
-
-typedef struct _tbm_bufmgr_nexell *tbm_bufmgr_nexell;
-typedef struct _tbm_bo_nexell *tbm_bo_nexell;
-
-/* tbm buffor object for nexell */
-struct _tbm_bo_nexell {
-       int fd;
-
-       unsigned int name;    /* FLINK ID */
-
-       unsigned int gem;     /* GEM Handle */
-
-       unsigned int dmabuf;  /* fd for dmabuf */
-
-       void *pBase;          /* virtual address */
-
-       unsigned int size;
-
-       unsigned int flags_nexell;
-       unsigned int flags_tbm;
-
-       pthread_mutex_t mutex;
-       int device;
-       int opt;
-
-       tbm_bo_cache_state cache_state;
-       unsigned int map_cnt;
-
-       tbm_bufmgr_nexell bufmgr_nexell;
-};
-
-/* tbm bufmgr private for nexell */
-struct _tbm_bufmgr_nexell {
-       int fd;
-       int isLocal;
-       void *hashBos;
-
-       int tgl_fd;
-
-       char *device_name;
-       void *bind_display;
-
-       tbm_backend_bufmgr_func *bufmgr_func;
-       tbm_backend_bo_func *bo_func;
-
-       tbm_bufmgr bufmgr;
-};
-
-static char *STR_DEVICE[] = {
-       "DEF",
-       "CPU",
-       "2D",
-       "3D",
-       "MM"
-};
-
-static char *STR_OPT[] = {
-       "NONE",
-       "RD",
-       "WR",
-       "RDWR"
-};
-
-
-static uint32_t tbm_nexell_color_format_list[TBM_COLOR_FORMAT_COUNT] = {
-                                                                   TBM_FORMAT_ARGB8888,
-                                                                   TBM_FORMAT_XRGB8888,
-                                                                   TBM_FORMAT_NV12,
-                                                                   TBM_FORMAT_NV21,
-                                                                   TBM_FORMAT_YUV420,
-                                                                   TBM_FORMAT_YVU420
-                                                               };
-
-#ifdef ENABLE_CACHECRTL
-static inline int
-_tgl_init(int fd, unsigned int key)
-{
-       struct tgl_attribute attr;
-       int err;
-       char buf[STRERR_BUFSIZE];
-
-       attr.key = key;
-       attr.timeout_ms = 1000;
-
-       err = ioctl(fd, TGL_IOC_INIT_LOCK, &attr);
-       if (err) {
-               TBM_ERR("error(%s) key:%d\n",
-                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
-               return 0;
-       }
-
-       return 1;
-}
-
-static inline int
-_tgl_destroy(int fd, unsigned int key)
-{
-       int err;
-       char buf[STRERR_BUFSIZE];
-
-       err = ioctl(fd, TGL_IOC_DESTROY_LOCK, key);
-       if (err) {
-               TBM_ERR("error(%s) key:%d\n",
-                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
-               return 0;
-       }
-
-       return 1;
-}
-static inline int
-_tgl_set_data(int fd, unsigned int key, unsigned int val)
-{
-       struct tgl_user_data arg;
-       int err;
-       char buf[STRERR_BUFSIZE];
-
-       arg.key = key;
-       arg.data1 = val;
-       err = ioctl(fd, TGL_IOC_SET_DATA, &arg);
-       if (err) {
-               TBM_ERR("error(%s) key:%d\n",
-                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
-               return 0;
-       }
-
-       return 1;
-}
-
-static inline unsigned int
-_tgl_get_data(int fd, unsigned int key)
-{
-       struct tgl_user_data arg = { 0, };
-       int err;
-       char buf[STRERR_BUFSIZE];
-
-       arg.key = key;
-       err = ioctl(fd, TGL_IOC_GET_DATA, &arg);
-       if (err) {
-               TBM_ERR("error(%s) key:%d\n",
-                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
-               return 0;
-       }
-
-       return arg.data1;
-}
-
-static int
-_nexell_cache_flush(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell, int flags)
-{
-       struct nx_drm_gem_create gem;
-       char buf[STRERR_BUFSIZE];
-
-       gem.handle = bo_nexell->gem;
-       gem.size = bo_nexell->size;
-
-       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_NX_GEM_SYNC, &gem)) {
-               TBM_ERR("error(%s) DRM_IOCTL_NX_GEM_SYNC.\n",
-                       strerror_r(errno, buf, STRERR_BUFSIZE));
-               return 0;
-       }
-
-       return 1;
-}
-#endif
-
-static int
-_bo_init_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell, int import)
-{
-#ifdef ENABLE_CACHECRTL
-       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, 0);
-       TBM_RETURN_VAL_IF_FAIL(bo_nexell != NULL, 0);
-
-       tbm_bo_cache_state cache_state;
-
-       _tgl_init(bufmgr_nexell->tgl_fd, bo_nexell->name);
-
-       if (import == 0) {
-               cache_state.data.isDirtied = DEVICE_NONE;
-               cache_state.data.isCached = 0;
-               cache_state.data.cntFlush = 0;
-
-               _tgl_set_data(bufmgr_nexell->tgl_fd, bo_nexell->name, cache_state.val);
-       }
-#endif
-
-       return 1;
-}
-
-static int
-_bo_set_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell, int device, int opt)
-{
-#ifdef ENABLE_CACHECRTL
-       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, 0);
-       TBM_RETURN_VAL_IF_FAIL(bo_nexell != NULL, 0);
-
-       char need_flush = 0;
-       unsigned short cntFlush = 0;
-
-       if (bo_nexell->flags_nexell == NEXELL_BO_DMA ||
-           bo_nexell->flags_nexell == NEXELL_BO_SYSTEM_NONCONTIG)
-               return 1;
-
-       /* get cache state of a bo */
-       bo_nexell->cache_state.val = _tgl_get_data(bufmgr_nexell->tgl_fd,
-                                    bo_nexell->name);
-
-       /* get global cache flush count */
-       cntFlush = (unsigned short)_tgl_get_data(bufmgr_nexell->tgl_fd, GLOBAL_KEY);
-
-       if (device == TBM_DEVICE_CPU) {
-               if (bo_nexell->cache_state.data.isDirtied == DEVICE_CO &&
-                   bo_nexell->cache_state.data.isCached)
-                       need_flush = TBM_NEXELL_CACHE_INV;
-
-               bo_nexell->cache_state.data.isCached = 1;
-               if (opt & TBM_OPTION_WRITE)
-                       bo_nexell->cache_state.data.isDirtied = DEVICE_CA;
-               else {
-                       if (bo_nexell->cache_state.data.isDirtied != DEVICE_CA)
-                               bo_nexell->cache_state.data.isDirtied = DEVICE_NONE;
-               }
-       } else {
-               if (bo_nexell->cache_state.data.isDirtied == DEVICE_CA &&
-                   bo_nexell->cache_state.data.isCached &&
-                   bo_nexell->cache_state.data.cntFlush == cntFlush)
-                       need_flush = TBM_NEXELL_CACHE_CLN | TBM_NEXELL_CACHE_ALL;
-
-               if (opt & TBM_OPTION_WRITE)
-                       bo_nexell->cache_state.data.isDirtied = DEVICE_CO;
-               else {
-                       if (bo_nexell->cache_state.data.isDirtied != DEVICE_CO)
-                               bo_nexell->cache_state.data.isDirtied = DEVICE_NONE;
-               }
-       }
-
-       if (need_flush) {
-               if (need_flush & TBM_NEXELL_CACHE_ALL)
-                       _tgl_set_data(bufmgr_nexell->tgl_fd, GLOBAL_KEY, (unsigned int)(++cntFlush));
-
-               /* call cache flush */
-               _nexell_cache_flush(bufmgr_nexell, bo_nexell, need_flush);
-
-               TBM_DBG(" \tcache(%d,%d)....flush:0x%x, cntFlush(%d)\n",
-                   bo_nexell->cache_state.data.isCached,
-                   bo_nexell->cache_state.data.isDirtied,
-                   need_flush,
-                   cntFlush);
-       }
-#endif
-
-       return 1;
-}
-
-static int
-_bo_save_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell)
-{
-#ifdef ENABLE_CACHECRTL
-       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, 0);
-       TBM_RETURN_VAL_IF_FAIL(bo_nexell != NULL, 0);
-
-       unsigned short cntFlush = 0;
-
-       /* get global cache flush count */
-       cntFlush = (unsigned short)_tgl_get_data(bufmgr_nexell->tgl_fd, GLOBAL_KEY);
-
-       /* save global cache flush count */
-       bo_nexell->cache_state.data.cntFlush = cntFlush;
-       _tgl_set_data(bufmgr_nexell->tgl_fd, bo_nexell->name,
-                     bo_nexell->cache_state.val);
-#endif
-
-       return 1;
-}
-
-static void
-_bo_destroy_cache_state(tbm_bufmgr_nexell bufmgr_nexell, tbm_bo_nexell bo_nexell)
-{
-#ifdef ENABLE_CACHECRTL
-       TBM_RETURN_IF_FAIL(bufmgr_nexell != NULL);
-       TBM_RETURN_IF_FAIL(bo_nexell != NULL);
-
-       _tgl_destroy(bufmgr_nexell->tgl_fd, bo_nexell->name);
-#endif
-}
-
-static int
-_bufmgr_init_cache_state(tbm_bufmgr_nexell bufmgr_nexell)
-{
-#ifdef ENABLE_CACHECRTL
-       bufmgr_nexell->tgl_fd = -1;
-
-       /* open tgl fd for saving cache flush data */
-       bufmgr_nexell->tgl_fd = open(tgl_devfile, O_RDWR);
-       if (bufmgr_nexell->tgl_fd < 0) {
-               bufmgr_nexell->tgl_fd = open(tgl_devfile1, O_RDWR);
-               if (bufmgr_nexell->tgl_fd < 0) {
-                   TBM_ERR("fail to open global_lock:%s\n",
-                                       tgl_devfile1);
-                       return 0;
-               }
-       }
-
-       if (!_tgl_init(bufmgr_nexell->tgl_fd, GLOBAL_KEY)) {
-               TBM_ERR("fail to initialize the tgl\n");
-               close(bufmgr_nexell->tgl_fd);
-               bufmgr_nexell->tgl_fd = -1;
-               return 0;
-       }
-#endif
-
-       return 1;
-}
-
-static void
-_bufmgr_deinit_cache_state(tbm_bufmgr_nexell bufmgr_nexell)
-{
-#ifdef ENABLE_CACHECRTL
-       if (bufmgr_nexell->tgl_fd >= 0) {
-               close(bufmgr_nexell->tgl_fd);
-               bufmgr_nexell->tgl_fd = -1;
-       }
-#endif
-}
-
-static int
-_tbm_nexell_open_drm()
-{
-       int fd = -1;
-
-       fd = drmOpen(NEXELL_DRM_NAME, NULL);
-       if (fd < 0) {
-               TBM_ERR("fail to open drm.(%s)\n", NEXELL_DRM_NAME);
-       }
-
-       if (fd < 0) {
-               struct udev *udev = NULL;
-               struct udev_enumerate *e = NULL;
-               struct udev_list_entry *entry = NULL;
-               struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL;
-               const char *filepath;
-               struct stat s;
-               int ret;
-
-               TBM_DBG("search drm-device by udev\n");
-
-               udev = udev_new();
-               if (!udev) {
-                       TBM_ERR("udev_new() failed.\n");
-                       return -1;
-               }
-
-               e = udev_enumerate_new(udev);
-               udev_enumerate_add_match_subsystem(e, "drm");
-               udev_enumerate_add_match_sysname(e, "card[0-9]*");
-               udev_enumerate_scan_devices(e);
-
-               udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-                       device = udev_device_new_from_syspath(udev_enumerate_get_udev(e),
-                                                             udev_list_entry_get_name(entry));
-                       device_parent = udev_device_get_parent(device);
-                       /* Not need unref device_parent. device_parent and device have same refcnt */
-                       if (device_parent) {
-                               if (strcmp(udev_device_get_sysname(device_parent), "nexell-drm") == 0) {
-                                       drm_device = device;
-                                       TBM_DBG("Found render device: '%s' (%s)\n",
-                                           udev_device_get_syspath(drm_device),
-                                           udev_device_get_sysname(device_parent));
-                                       break;
-                               }
-                       }
-                       udev_device_unref(device);
-               }
-
-               udev_enumerate_unref(e);
-
-               /* Get device file path. */
-               filepath = udev_device_get_devnode(drm_device);
-               if (!filepath) {
-                       TBM_ERR("udev_device_get_devnode() failed.\n");
-                       udev_device_unref(drm_device);
-                       udev_unref(udev);
-                       return -1;
-               }
-
-               /* Open DRM device file and check validity. */
-               fd = open(filepath, O_RDWR | O_CLOEXEC);
-               if (fd < 0) {
-                       TBM_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
-                       udev_device_unref(drm_device);
-                       udev_unref(udev);
-                       return -1;
-               }
-
-               ret = fstat(fd, &s);
-               if (ret) {
-                       TBM_ERR("fstat() failed %s.\n");
-                       close(fd);
-                       udev_device_unref(drm_device);
-                       udev_unref(udev);
-                       return -1;
-               }
-
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-       }
-
-       return fd;
-}
-
-static int
-_check_render_node(void)
-{
-#ifndef USE_RENDER_NODE
-       return 0;
-#else
-       struct udev *udev = NULL;
-       struct udev_enumerate *e = NULL;
-       struct udev_list_entry *entry = NULL;
-       struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL;
-
-       udev = udev_new();
-       if (!udev) {
-               TBM_ERR("udev_new() failed.\n");
-               return -1;
-       }
-
-       e = udev_enumerate_new(udev);
-       udev_enumerate_add_match_subsystem(e, "drm");
-       udev_enumerate_add_match_sysname(e, "renderD[0-9]*");
-       udev_enumerate_scan_devices(e);
-
-       udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-               device = udev_device_new_from_syspath(udev_enumerate_get_udev(e),
-                                                     udev_list_entry_get_name(entry));
-               device_parent = udev_device_get_parent(device);
-               /* Not need unref device_parent. device_parent and device have same refcnt */
-               if (device_parent) {
-                       if (strcmp(udev_device_get_sysname(device_parent), "nexell-drm") == 0) {
-                               drm_device = device;
-                               TBM_DBG("Found render device: '%s' (%s)\n",
-                                   udev_device_get_syspath(drm_device),
-                                   udev_device_get_sysname(device_parent));
-                               break;
-                       }
-               }
-               udev_device_unref(device);
-       }
-
-       udev_enumerate_unref(e);
-       udev_unref(udev);
-
-       if (!drm_device) {
-               udev_device_unref(drm_device);
-               return 0;
-       }
-
-       udev_device_unref(drm_device);
-       return 1;
-#endif
-}
-
-static int
-_get_render_node(void)
-{
-       struct udev *udev = NULL;
-       struct udev_enumerate *e = NULL;
-       struct udev_list_entry *entry = NULL;
-       struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL;
-       const char *filepath;
-       struct stat s;
-       int fd = -1;
-       int ret;
-
-       udev = udev_new();
-       if (!udev) {
-               TBM_ERR("udev_new() failed.\n");
-               return -1;
-       }
-
-       e = udev_enumerate_new(udev);
-       udev_enumerate_add_match_subsystem(e, "drm");
-       udev_enumerate_add_match_sysname(e, "renderD[0-9]*");
-       udev_enumerate_scan_devices(e);
-
-       udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-               device = udev_device_new_from_syspath(udev_enumerate_get_udev(e),
-                                                     udev_list_entry_get_name(entry));
-               device_parent = udev_device_get_parent(device);
-               /* Not need unref device_parent. device_parent and device have same refcnt */
-               if (device_parent) {
-                       if (strcmp(udev_device_get_sysname(device_parent), "nexell-drm") == 0) {
-                               drm_device = device;
-                               TBM_DBG("Found render device: '%s' (%s)\n",
-                                   udev_device_get_syspath(drm_device),
-                                   udev_device_get_sysname(device_parent));
-                               break;
-                       }
-               }
-               udev_device_unref(device);
-       }
-
-       udev_enumerate_unref(e);
-
-       /* Get device file path. */
-       filepath = udev_device_get_devnode(drm_device);
-       if (!filepath) {
-               TBM_ERR("udev_device_get_devnode() failed.\n");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               return -1;
-       }
-
-       /* Open DRM device file and check validity. */
-       fd = open(filepath, O_RDWR | O_CLOEXEC);
-       if (fd < 0) {
-               TBM_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               return -1;
-       }
-
-       ret = fstat(fd, &s);
-       if (ret) {
-               TBM_ERR("fstat() failed %s.\n");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               close(fd);
-               return -1;
-       }
-
-       udev_device_unref(drm_device);
-       udev_unref(udev);
-
-       return fd;
-}
-
-static unsigned int
-_get_nexell_flag_from_tbm(unsigned int ftbm)
-{
-       unsigned int flags = 0;
-
-       switch (ftbm) {
-       /*
-        * As default, allocate DMA buffer with physically non-contiguous
-        * and cachable attributes for Wayland clients who consider CPU
-        * access to DMA buffer.
-        */
-       case TBM_BO_DEFAULT:
-               flags = NEXELL_BO_SYSTEM_NONCONTIG_CACHEABLE;
-               break;
-       /*
-        * Nexell Display controller has no IOMMU so alloate DMA buffer
-        * with phycially contiguous memory and non-cachable attributes
-        * for SCANOUT buffer.
-        */
-       case TBM_BO_SCANOUT:
-               flags = NEXELL_BO_DMA;
-               break;
-       /*
-        * Allocate DMA buffer with physically non-contiguous
-        * and non-cachable attributes for Wayland clients
-        * who never consider CPU access to DMA buffer.
-        */
-       case TBM_BO_WC:
-       case TBM_BO_NONCACHABLE:
-               flags = NEXELL_BO_SYSTEM_NONCONTIG;
-               break;
-       default:
-               flags = NEXELL_BO_DMA;
-               break;
-       }
-
-       return flags;
-}
-
-static unsigned int
-_get_tbm_flag_from_nexell(unsigned int fnexell)
-{
-       unsigned int flags = 0;
-
-       switch (fnexell) {
-       case NEXELL_BO_SYSTEM_NONCONTIG_CACHEABLE:
-               flags = TBM_BO_DEFAULT;
-               break;
-       case NEXELL_BO_DMA:
-               flags = TBM_BO_SCANOUT;
-               break;
-       case NEXELL_BO_SYSTEM_NONCONTIG:
-               flags = TBM_BO_WC|TBM_BO_NONCACHABLE;
-               break;
-       default:
-               flags = TBM_BO_SCANOUT;
-               break;
-       }
-
-       return flags;
-}
-
-static unsigned int
-_get_name(int fd, unsigned int gem)
-{
-       struct drm_gem_flink arg = {0,};
-
-       arg.handle = gem;
-       if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) {
-               TBM_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d", gem);
-               return 0;
-       }
-
-       return (unsigned int)arg.name;
-}
-
-static tbm_bo_handle
-_nexell_bo_handle(tbm_bo_nexell bo_nexell, int device)
-{
-       tbm_bo_handle bo_handle;
-
-       memset(&bo_handle, 0x0, sizeof(uint64_t));
-
-       switch (device) {
-       case TBM_DEVICE_DEFAULT:
-       case TBM_DEVICE_2D:
-               bo_handle.u32 = (uint32_t)bo_nexell->gem;
-               break;
-       case TBM_DEVICE_CPU:
-               if (!bo_nexell->pBase) {
-                       struct drm_mode_map_dumb arg = {0,};
-                       void *map = NULL;
-
-                       arg.handle = bo_nexell->gem;
-                       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg)) {
-                               TBM_ERR("Cannot map_dumb gem=%d\n", bo_nexell->gem);
-                               return (tbm_bo_handle) NULL;
-                       }
-
-                       map = mmap(NULL, bo_nexell->size, PROT_READ | PROT_WRITE, MAP_SHARED,
-                                  bo_nexell->fd, arg.offset);
-                       if (map == MAP_FAILED) {
-                               TBM_ERR("Cannot usrptr gem=%d\n", bo_nexell->gem);
-                               return (tbm_bo_handle) NULL;
-                       }
-                       bo_nexell->pBase = map;
-               }
-               bo_handle.ptr = (void *)bo_nexell->pBase;
-               break;
-       case TBM_DEVICE_3D:
-       case TBM_DEVICE_MM:
-               if (!bo_nexell->dmabuf) {
-                       struct drm_prime_handle arg = {0, };
-
-                       arg.handle = bo_nexell->gem;
-                       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                               TBM_ERR("Cannot dmabuf=%d\n", bo_nexell->gem);
-                               return (tbm_bo_handle) NULL;
-                       }
-                       bo_nexell->dmabuf = arg.fd;
-               }
-
-               bo_handle.u32 = (uint32_t)bo_nexell->dmabuf;
-               break;
-       default:
-               TBM_ERR("Not supported device:%d\n", device);
-               bo_handle.ptr = (void *) NULL;
-               break;
-       }
-
-       return bo_handle;
-}
-
-static int
-_calc_yplane_nv12(int width, int height)
-{
-       int mbX, mbY;
-
-       mbX = SIZE_ALIGN(width + 24, S5P_FIMV_NV12MT_HALIGN);
-       mbY = SIZE_ALIGN(height + 16, S5P_FIMV_NV12MT_VALIGN);
-
-       return SIZE_ALIGN(mbX * mbY, S5P_FIMV_DEC_BUF_ALIGN);
-}
-
-static int
-_calc_uvplane_nv12(int width, int height)
-{
-       int mbX, mbY;
-
-       mbX = SIZE_ALIGN(width + 16, S5P_FIMV_NV12MT_HALIGN);
-       mbY = SIZE_ALIGN(height + 4, S5P_FIMV_NV12MT_VALIGN);
-
-       return SIZE_ALIGN((mbX * mbY) >> 1, S5P_FIMV_DEC_BUF_ALIGN);
-}
-
-static int
-_new_calc_plane_nv12(int width, int height)
-{
-       int mbX, mbY;
-
-       mbX = DIV_ROUND_UP(width, S5P_FIMV_NUM_PIXELS_IN_MB_ROW);
-       mbY = DIV_ROUND_UP(height, S5P_FIMV_NUM_PIXELS_IN_MB_COL);
-
-       if (width * height < S5P_FIMV_MAX_FRAME_SIZE)
-               mbY = (mbY + 1) / 2 * 2;
-
-       return ((mbX * S5P_FIMV_NUM_PIXELS_IN_MB_COL) * (mbY *
-                       S5P_FIMV_NUM_PIXELS_IN_MB_ROW));
-}
-
-static int
-_new_calc_yplane_nv12(int width, int height)
-{
-       return SIZE_ALIGN(_new_calc_plane_nv12(width,
-                                               height) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
-                          TBM_SURFACE_ALIGNMENT_PLANE_NV12);
-}
-
-static int
-_new_calc_uvplane_nv12(int width, int height)
-{
-       return SIZE_ALIGN((_new_calc_plane_nv12(width,
-                                               height) >> 1) + S5P_FIMV_D_ALIGN_PLANE_SIZE,
-                         TBM_SURFACE_ALIGNMENT_PLANE_NV12);
-}
-
-static tbm_bufmgr_capability
-tbm_nexell_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error)
-{
-       tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE;
-
-       capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD;
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return capabilities;
-}
-
-static tbm_error_e
-tbm_nexell_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, TBM_ERROR_INVALID_PARAMETER);
-
-       if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_nexell->fd,
-                                          bufmgr_nexell->device_name, 0)) {
-               TBM_ERR("fail to tbm_drm_helper_wl_server_init\n");
-               return TBM_ERROR_INVALID_OPERATION;
-       }
-
-       bufmgr_nexell->bind_display = native_display;
-
-       return TBM_ERROR_NONE;
-}
-
-static tbm_error_e
-tbm_nexell_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data,
-                                                       uint32_t **formats, uint32_t *num)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       uint32_t *color_formats;
-
-       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, TBM_ERROR_INVALID_PARAMETER);
-
-       color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
-       if (color_formats == NULL)
-               return TBM_ERROR_OUT_OF_MEMORY;
-
-       memcpy(color_formats, tbm_nexell_color_format_list, sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT);
-
-       *formats = color_formats;
-       *num = TBM_COLOR_FORMAT_COUNT;
-
-       TBM_DBG("supported format count = %d\n", *num);
-
-       return TBM_ERROR_NONE;
-}
-
-static tbm_error_e
-tbm_nexell_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data,
-                                                       tbm_format format, int plane_idx, int width,
-                                                       int height, uint32_t *size, uint32_t *offset,
-                                                       uint32_t *pitch, int *bo_idx)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       int bpp;
-       int _offset = 0;
-       int _pitch = 0;
-       int _size = 0;
-       int _bo_idx = 0;
-
-       TBM_RETURN_VAL_IF_FAIL(bufmgr_nexell != NULL, TBM_ERROR_INVALID_PARAMETER);
-
-       switch (format) {
-               /* 16 bpp RGB */
-       case TBM_FORMAT_XRGB4444:
-       case TBM_FORMAT_XBGR4444:
-       case TBM_FORMAT_RGBX4444:
-       case TBM_FORMAT_BGRX4444:
-       case TBM_FORMAT_ARGB4444:
-       case TBM_FORMAT_ABGR4444:
-       case TBM_FORMAT_RGBA4444:
-       case TBM_FORMAT_BGRA4444:
-       case TBM_FORMAT_XRGB1555:
-       case TBM_FORMAT_XBGR1555:
-       case TBM_FORMAT_RGBX5551:
-       case TBM_FORMAT_BGRX5551:
-       case TBM_FORMAT_ARGB1555:
-       case TBM_FORMAT_ABGR1555:
-       case TBM_FORMAT_RGBA5551:
-       case TBM_FORMAT_BGRA5551:
-       case TBM_FORMAT_RGB565:
-               bpp = 16;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
-               /* 24 bpp RGB */
-       case TBM_FORMAT_RGB888:
-       case TBM_FORMAT_BGR888:
-               bpp = 24;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
-               /* 32 bpp RGB */
-       case TBM_FORMAT_XRGB8888:
-       case TBM_FORMAT_XBGR8888:
-       case TBM_FORMAT_RGBX8888:
-       case TBM_FORMAT_BGRX8888:
-       case TBM_FORMAT_ARGB8888:
-       case TBM_FORMAT_ABGR8888:
-       case TBM_FORMAT_RGBA8888:
-       case TBM_FORMAT_BGRA8888:
-               bpp = 32;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
-
-               /* packed YCbCr */
-       case TBM_FORMAT_YUYV:
-       case TBM_FORMAT_YVYU:
-       case TBM_FORMAT_UYVY:
-       case TBM_FORMAT_VYUY:
-       case TBM_FORMAT_AYUV:
-               bpp = 32;
-               _offset = 0;
-               _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-               _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-               _bo_idx = 0;
-               break;
-
-               /*
-               * 2 plane YCbCr
-               * index 0 = Y plane, [7:0] Y
-               * index 1 = Cr:Cb plane, [15:0] Cr:Cb little endian
-               * or
-               * index 1 = Cb:Cr plane, [15:0] Cb:Cr little endian
-               */
-       case TBM_FORMAT_NV12:
-       case TBM_FORMAT_NV21:
-               bpp = 12;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = MAX(_calc_yplane_nv12(width, height),
-                                               _new_calc_yplane_nv12(width, height));
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if (plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = MAX(_calc_uvplane_nv12(width, height),
-                                               _new_calc_uvplane_nv12(width, height));
-                       _bo_idx = 0;
-               }
-               break;
-       case TBM_FORMAT_NV16:
-       case TBM_FORMAT_NV61:
-               bpp = 16;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if( plane_idx ==1 )*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
-
-               /*
-               * 3 plane YCbCr
-               * index 0: Y plane, [7:0] Y
-               * index 1: Cb plane, [7:0] Cb
-               * index 2: Cr plane, [7:0] Cr
-               * or
-               * index 1: Cr plane, [7:0] Cr
-               * index 2: Cb plane, [7:0] Cb
-               */
-               /*
-               * NATIVE_BUFFER_FORMAT_YV12
-               * NATIVE_BUFFER_FORMAT_I420
-               */
-       case TBM_FORMAT_YUV410:
-       case TBM_FORMAT_YVU410:
-               bpp = 9;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
-                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4);
-                       _size = SIZE_ALIGN(_pitch * (height / 4), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
-       case TBM_FORMAT_YUV411:
-       case TBM_FORMAT_YVU411:
-       case TBM_FORMAT_YUV420:
-       case TBM_FORMAT_YVU420:
-               bpp = 12;
-               /* plane_idx == 0 */
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = _pitch * SIZE_ALIGN(height, 16);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /* plane_idx == 1 */
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(_pitch / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = _pitch * SIZE_ALIGN(height >> 1, 16);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /* plane_idx == 2 */
-               {
-                       _offset += _size;
-               }
-               break;
-       case TBM_FORMAT_YUV422:
-       case TBM_FORMAT_YVU422:
-               bpp = 16;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
-       case TBM_FORMAT_YUV444:
-       case TBM_FORMAT_YVU444:
-               bpp = 24;
-               /*if(plane_idx == 0)*/
-               {
-                       _offset = 0;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 0)
-                               break;
-               }
-               /*else if(plane_idx == 1)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-                       if (plane_idx == 1)
-                               break;
-               }
-               /*else if (plane_idx == 2)*/
-               {
-                       _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
-                       _bo_idx = 0;
-               }
-               break;
-       default:
-               bpp = 0;
-               break;
-       }
-
-       *size = _size;
-       *offset = _offset;
-       *pitch = _pitch;
-       *bo_idx = _bo_idx;
-
-       return TBM_ERROR_NONE;
-}
-
-static tbm_backend_bo_data *
-tbm_nexell_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size,
-                                       tbm_bo_memory_type flags, tbm_error_e *error)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       tbm_bo_nexell bo_nexell;
-       unsigned int nexell_flags;
-
-       if (bufmgr_nexell == NULL) {
-               TBM_ERR("bufmgr_data is null\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return NULL;
-       }
-
-       bo_nexell = calloc(1, sizeof(struct _tbm_bo_nexell));
-       if (!bo_nexell) {
-               TBM_ERR("fail to allocate the bo_nexell private\n");
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
-       bo_nexell->bufmgr_nexell = bufmgr_nexell;
-
-       nexell_flags = _get_nexell_flag_from_tbm(flags);
-
-       struct nx_drm_gem_create arg = {0, };
-
-       arg.size = (uint64_t)size;
-       arg.flags = nexell_flags;
-       if (drmCommandWriteRead(bufmgr_nexell->fd, DRM_NX_GEM_CREATE, &arg,
-                               sizeof(arg))) {
-               TBM_ERR("Cannot create bo_nexell(flag:%x, size:%d)\n", arg.flags,
-                              (unsigned int)arg.size);
-               free(bo_nexell);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       bo_nexell->fd = bufmgr_nexell->fd;
-       bo_nexell->gem = arg.handle;
-       bo_nexell->size = size;
-       bo_nexell->flags_tbm = flags;
-       bo_nexell->flags_nexell = nexell_flags;
-       bo_nexell->name = _get_name(bo_nexell->fd, bo_nexell->gem);
-
-       if (!_bo_init_cache_state(bufmgr_nexell, bo_nexell, 0)) {
-               TBM_ERR("fail init cache state(%d)\n", bo_nexell->name);
-               free(bo_nexell);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       pthread_mutex_init(&bo_nexell->mutex, NULL);
-
-       if (!bo_nexell->dmabuf) {
-               struct drm_prime_handle arg = {0, };
-
-               arg.handle = bo_nexell->gem;
-               if (drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_ERR("Cannot dmabuf=%d\n", bo_nexell->gem);
-                       free(bo_nexell);
-                       if (error)
-                               *error = TBM_ERROR_INVALID_OPERATION;
-                       return NULL;
-               }
-               bo_nexell->dmabuf = arg.fd;
-       }
-
-       /* add bo_nexell to hash */
-       if (drmHashInsert(bufmgr_nexell->hashBos, bo_nexell->name, (void *)bo_nexell) < 0)
-               TBM_ERR("Cannot insert bo_nexell to Hash(%d)\n", bo_nexell->name);
-
-       TBM_DBG("     bo_nexell:%p, gem:%d(%d), flags:%d(%d), size:%d\n",
-               bo_nexell,
-               bo_nexell->gem, bo_nexell->name,
-               flags, nexell_flags,
-               bo_nexell->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_backend_bo_data *)bo_nexell;
-}
-
-static tbm_backend_bo_data *
-tbm_nexell_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_error_e *error)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       tbm_bo_nexell bo_nexell;
-       unsigned int gem = 0;
-       unsigned int name;
-       int ret;
-       char buf[STRERR_BUFSIZE];
-
-       if (bufmgr_nexell == NULL) {
-               TBM_ERR("bufmgr_data is null\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return NULL;
-       }
-
-       /*getting handle from fd*/
-       struct drm_prime_handle arg = {0, };
-
-       arg.fd = key;
-       arg.flags = 0;
-       if (drmIoctl(bufmgr_nexell->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) {
-               TBM_ERR("Cannot get gem handle from fd:%d (%s)\n",
-                              arg.fd, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-       gem = arg.handle;
-
-       name = _get_name(bufmgr_nexell->fd, gem);
-       if (!name) {
-               TBM_ERR("Cannot get name from gem:%d, fd:%d (%s)\n",
-                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       ret = drmHashLookup(bufmgr_nexell->hashBos, name, (void **)&bo_nexell);
-       if (ret == 0) {
-               if (gem == bo_nexell->gem) {
-                       if (error)
-                               *error = TBM_ERROR_NONE;
-                       return bo_nexell;
-               }
-       }
-
-       /* Determine size of bo.  The fd-to-handle ioctl really should
-        * return the size, but it doesn't.  If we have kernel 3.12 or
-        * later, we can lseek on the prime fd to get the size.  Older
-        * kernels will just fail, in which case we fall back to the
-        * provided (estimated or guess size).
-        */
-       unsigned int real_size = -1;
-       struct nx_drm_gem_info info = {0, };
-
-       real_size = lseek(key, 0, SEEK_END);
-
-       info.handle = gem;
-       if (drmCommandWriteRead(bufmgr_nexell->fd,
-                               DRM_NX_GEM_GET,
-                               &info,
-                               sizeof(struct nx_drm_gem_info))) {
-               TBM_ERR("Cannot get gem info from gem:%d, fd:%d (%s)\n",
-                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       if (real_size == -1)
-               real_size = info.size;
-
-       bo_nexell = calloc(1, sizeof(struct _tbm_bo_nexell));
-       if (!bo_nexell) {
-               TBM_ERR("bo_nexell:%p fail to allocate the bo_nexell\n", bo_nexell);
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
-       bo_nexell->bufmgr_nexell = bufmgr_nexell;
-
-       bo_nexell->fd = bufmgr_nexell->fd;
-       bo_nexell->gem = gem;
-       bo_nexell->size = real_size;
-       bo_nexell->flags_nexell = info.flags;
-       bo_nexell->flags_tbm = _get_tbm_flag_from_nexell(bo_nexell->flags_nexell);
-       bo_nexell->name = name;
-
-       if (!_bo_init_cache_state(bufmgr_nexell, bo_nexell, 1)) {
-               TBM_ERR("fail init cache state(%d)\n", bo_nexell->name);
-               free(bo_nexell);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       /* add bo_nexell to hash */
-       if (drmHashInsert(bufmgr_nexell->hashBos, bo_nexell->name, (void *)bo_nexell) < 0)
-               TBM_ERR("bo_nexell:%p Cannot insert bo_nexell to Hash(%d) from gem:%d, fd:%d\n",
-                              bo_nexell, bo_nexell->name, gem, key);
-
-       TBM_DBG(" bo_nexell:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           key,
-           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
-           bo_nexell->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_backend_bo_data *)bo_nexell;
-}
-
-static tbm_backend_bo_data *
-tbm_nexell_bufmgr_import(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       tbm_bo_nexell bo_nexell;
-       int ret;
-
-       if (bufmgr_nexell == NULL) {
-               TBM_ERR("bufmgr_data is null\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return NULL;
-       }
-
-       ret = drmHashLookup(bufmgr_nexell->hashBos, key, (void **)&bo_nexell);
-       if (ret == 0) {
-               if (error)
-                       *error = TBM_ERROR_NONE;
-               return (tbm_backend_bo_data *)bo_nexell;
-       }
-
-       struct drm_gem_open arg = {0, };
-       struct nx_drm_gem_info info = {0, };
-
-       arg.name = key;
-       if (drmIoctl(bufmgr_nexell->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
-               TBM_ERR("Cannot open gem name=%d\n", key);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       info.handle = arg.handle;
-       if (drmCommandWriteRead(bufmgr_nexell->fd,
-                               DRM_NX_GEM_GET,
-                               &info,
-                               sizeof(struct nx_drm_gem_info))) {
-               TBM_ERR("Cannot get gem info=%d\n", key);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       bo_nexell = calloc(1, sizeof(struct _tbm_bo_nexell));
-       if (!bo_nexell) {
-               TBM_ERR("fail to allocate the bo_nexell private\n");
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
-       bo_nexell->bufmgr_nexell = bufmgr_nexell;
-
-       bo_nexell->fd = bufmgr_nexell->fd;
-       bo_nexell->gem = arg.handle;
-       bo_nexell->size = arg.size;
-       bo_nexell->flags_nexell = info.flags;
-       bo_nexell->name = key;
-       bo_nexell->flags_tbm = _get_tbm_flag_from_nexell(bo_nexell->flags_nexell);
-
-       if (!_bo_init_cache_state(bufmgr_nexell, bo_nexell, 1)) {
-               TBM_ERR("fail init cache state(%d)\n", bo_nexell->name);
-               free(bo_nexell);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       if (!bo_nexell->dmabuf) {
-               struct drm_prime_handle arg = {0, };
-
-               arg.handle = bo_nexell->gem;
-               if (drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem=%d\n", bo_nexell->gem);
-                       if (error)
-                               *error = TBM_ERROR_INVALID_OPERATION;
-                       free(bo_nexell);
-                       return NULL;
-               }
-               bo_nexell->dmabuf = arg.fd;
-       }
-
-       /* add bo_nexell to hash */
-       if (drmHashInsert(bufmgr_nexell->hashBos, bo_nexell->name, (void *)bo_nexell) < 0)
-               TBM_ERR("Cannot insert bo_nexell to Hash(%d)\n", bo_nexell->name);
-
-
-       TBM_DBG("    bo_nexell:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
-           bo_nexell->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_backend_bo_data *)bo_nexell;
-}
-
-static void
-tbm_nexell_bo_free(tbm_backend_bo_data *bo_data)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-       tbm_bo_nexell temp;
-       tbm_bufmgr_nexell bufmgr_nexell;
-       char buf[STRERR_BUFSIZE];
-       int ret;
-
-       if (!bo_data)
-               return;
-
-       bufmgr_nexell = bo_nexell->bufmgr_nexell;
-       if (!bufmgr_nexell)
-               return;
-
-       TBM_DBG("      bo_nexell:%p, gem:%d(%d), fd:%d, size:%d\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           bo_nexell->size);
-
-       if (bo_nexell->pBase) {
-               if (munmap(bo_nexell->pBase, bo_nexell->size) == -1) {
-                       TBM_ERR("bo_nexell:%p fail to munmap(%s)\n",
-                                      bo_nexell, strerror_r(errno, buf, STRERR_BUFSIZE));
-               }
-       }
-
-       /* close dmabuf */
-       if (bo_nexell->dmabuf) {
-               close(bo_nexell->dmabuf);
-               bo_nexell->dmabuf = 0;
-       }
-
-       /* delete bo from hash */
-       ret = drmHashLookup(bufmgr_nexell->hashBos, bo_nexell->name,
-                            (void **)&temp);
-       if (ret == 0)
-               drmHashDelete(bufmgr_nexell->hashBos, bo_nexell->name);
-       else
-               TBM_ERR("Cannot find bo_nexell to Hash(%d), ret=%d\n", bo_nexell->name, ret);
-
-       if (temp != bo_nexell)
-               TBM_ERR("hashBos probably has several BOs with same name!!!\n");
-
-       _bo_destroy_cache_state(bufmgr_nexell, bo_nexell);
-
-       /* Free gem handle */
-       struct drm_gem_close arg = {0, };
-
-       memset(&arg, 0, sizeof(arg));
-       arg.handle = bo_nexell->gem;
-       if (drmIoctl(bo_nexell->fd, DRM_IOCTL_GEM_CLOSE, &arg))
-               TBM_ERR("bo_nexell:%p fail to gem close.(%s)\n",
-                              bo_nexell, strerror_r(errno, buf, STRERR_BUFSIZE));
-
-       free(bo_nexell);
-}
-
-static int
-tbm_nexell_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-
-       if (!bo_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return 0;
-       }
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return bo_nexell->size;
-}
-
-static tbm_bo_memory_type
-tbm_nexell_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-
-       if (!bo_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return TBM_BO_DEFAULT;
-       }
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return bo_nexell->flags_tbm;
-}
-
-static tbm_bo_handle
-tbm_nexell_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-       tbm_bo_handle bo_handle;
-
-       if (!bo_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (!bo_nexell->gem) {
-               TBM_ERR("Cannot map gem=%d\n", bo_nexell->gem);
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       TBM_DBG("bo_nexell:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d, %s\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
-           bo_nexell->size,
-           STR_DEVICE[device]);
-
-       /*Get mapped bo_handle*/
-       bo_handle = _nexell_bo_handle(bo_nexell, device);
-       if (bo_handle.ptr == NULL) {
-               TBM_ERR("Cannot get handle: gem:%d, device:%d\n",
-                       bo_nexell->gem, device);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return bo_handle;
-}
-
-static tbm_bo_handle
-tbm_nexell_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
-                               tbm_bo_access_option opt, tbm_error_e *error)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-       tbm_bo_handle bo_handle;
-       tbm_bufmgr_nexell bufmgr_nexell;
-
-       if (!bo_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       bufmgr_nexell = bo_nexell->bufmgr_nexell;
-       if (!bufmgr_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (!bo_nexell->gem) {
-               TBM_ERR("Cannot map gem=%d\n", bo_nexell->gem);
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return (tbm_bo_handle) NULL;
-       }
-
-       TBM_DBG("       bo_nexell:%p, gem:%d(%d), fd:%d, %s, %s\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           STR_DEVICE[device],
-           STR_OPT[opt]);
-
-       /*Get mapped bo_handle*/
-       bo_handle = _nexell_bo_handle(bo_nexell, device);
-       if (bo_handle.ptr == NULL) {
-               TBM_ERR("Cannot get handle: gem:%d, device:%d, opt:%d\n",
-                              bo_nexell->gem, device, opt);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (bo_nexell->map_cnt == 0)
-               _bo_set_cache_state(bufmgr_nexell, bo_nexell, device, opt);
-
-       bo_nexell->map_cnt++;
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return bo_handle;
-}
-
-static tbm_error_e
-tbm_nexell_bo_unmap(tbm_backend_bo_data *bo_data)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-       tbm_bufmgr_nexell bufmgr_nexell;
-
-       if (!bo_nexell)
-               return TBM_ERROR_INVALID_PARAMETER;
-
-       bufmgr_nexell = bo_nexell->bufmgr_nexell;
-       if (!bufmgr_nexell)
-               return TBM_ERROR_INVALID_PARAMETER;
-
-       if (!bo_nexell->gem)
-               return TBM_ERROR_INVALID_PARAMETER;
-
-       bo_nexell->map_cnt--;
-
-       if (bo_nexell->map_cnt == 0)
-               _bo_save_cache_state(bufmgr_nexell, bo_nexell);
-
-       TBM_DBG("     bo_nexell:%p, gem:%d(%d), fd:%d\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf);
-
-       return TBM_ERROR_NONE;
-}
-
-static tbm_error_e
-tbm_nexell_bo_lock(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
-                               tbm_bo_access_option opt)
-{
-#ifndef ALWAYS_BACKEND_CTRL
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-       tbm_bufmgr_nexell bufmgr_nexell;
-
-       if (!bo_nexell)
-               return TBM_ERROR_INVALID_PARAMETER;
-
-       bufmgr_nexell = bo_nexell->bufmgr_nexell;
-       if (!bufmgr_nexell)
-               return TBM_ERROR_INVALID_PARAMETER;
-
-       if (device != TBM_DEVICE_3D && device != TBM_DEVICE_CPU) {
-               TBM_DBG("Not support device type,\n");
-               return TBM_ERROR_INVALID_OPERATION;
-       }
-#endif /* ALWAYS_BACKEND_CTRL */
-
-       return TBM_ERROR_NONE;
-}
-
-static tbm_error_e
-tbm_nexell_bo_unlock(tbm_backend_bo_data *bo_data)
-{
-#ifndef ALWAYS_BACKEND_CTRL
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-
-       if (!bo_nexell)
-               return TBM_ERROR_INVALID_PARAMETER;
-#endif /* ALWAYS_BACKEND_CTRL */
-
-       return TBM_ERROR_NONE;
-}
-
-static tbm_fd
-tbm_nexell_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-       int ret;
-       char buf[STRERR_BUFSIZE];
-
-       if (!bo_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return -1;
-       }
-
-       struct drm_prime_handle arg = {0, };
-
-       arg.handle = bo_nexell->gem;
-       ret = drmIoctl(bo_nexell->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
-       if (ret) {
-               TBM_ERR("bo_nexell:%p Cannot dmabuf=%d (%s)\n",
-                              bo_nexell, bo_nexell->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               return (tbm_fd) ret;
-       }
-
-       TBM_DBG(" bo_nexell:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           arg.fd,
-           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
-           bo_nexell->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_fd)arg.fd;
-}
-
-static tbm_key
-tbm_nexell_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error)
-{
-       tbm_bo_nexell bo_nexell = (tbm_bo_nexell)bo_data;
-
-       if (!bo_nexell) {
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return 0;
-       }
-
-       if (!bo_nexell->name) {
-               bo_nexell->name = _get_name(bo_nexell->fd, bo_nexell->gem);
-               if (!bo_nexell->name) {
-                       TBM_ERR("error Cannot get name\n");
-                       if (error)
-                               *error = TBM_ERROR_INVALID_PARAMETER;
-                       return 0;
-               }
-       }
-
-       TBM_DBG("    bo_nexell:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
-           bo_nexell,
-           bo_nexell->gem, bo_nexell->name,
-           bo_nexell->dmabuf,
-           bo_nexell->flags_tbm, bo_nexell->flags_nexell,
-           bo_nexell->size);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       return (tbm_key)bo_nexell->name;
-}
-
-static void
-tbm_nexell_deinit(tbm_backend_bufmgr_data *bufmgr_data)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = (tbm_bufmgr_nexell)bufmgr_data;
-       tbm_bufmgr bufmgr;
-       tbm_error_e error;
-       unsigned long key;
-       void *value;
-
-       TBM_RETURN_IF_FAIL(bufmgr_nexell != NULL);
-
-       bufmgr = bufmgr_nexell->bufmgr;
-
-       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_nexell->bufmgr_func);
-       tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_nexell->bo_func);
-
-       if (bufmgr_nexell->hashBos) {
-               while (drmHashFirst(bufmgr_nexell->hashBos, &key, &value) > 0) {
-                       free(value);
-                       drmHashDelete(bufmgr_nexell->hashBos, key);
-               }
-
-               drmHashDestroy(bufmgr_nexell->hashBos);
-               bufmgr_nexell->hashBos = NULL;
-       }
-
-       _bufmgr_deinit_cache_state(bufmgr_nexell);
-
-       if (bufmgr_nexell->bind_display)
-               tbm_drm_helper_wl_auth_server_deinit();
-
-       if (bufmgr_nexell->device_name)
-               free(bufmgr_nexell->device_name);
-
-       if (tbm_backend_bufmgr_query_display_server(bufmgr, &error))
-               tbm_drm_helper_unset_tbm_master_fd();
-       else
-               tbm_drm_helper_unset_fd();
-
-       close(bufmgr_nexell->fd);
-
-       free(bufmgr_nexell);
-}
-
-static tbm_backend_bufmgr_data *
-tbm_nexell_init(tbm_bufmgr bufmgr, tbm_error_e *error)
-{
-       tbm_bufmgr_nexell bufmgr_nexell = NULL;
-       tbm_backend_bufmgr_func *bufmgr_func = NULL;
-       tbm_backend_bo_func *bo_func = NULL;
-       tbm_error_e err;
-       int set_master = 0;
-
-       if (!bufmgr) {
-               TBM_ERR("bufmgr is null.\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_PARAMETER;
-               return NULL;
-       }
-
-       bufmgr_nexell = calloc(1, sizeof(struct _tbm_bufmgr_nexell));
-       if (!bufmgr_nexell) {
-               TBM_ERR("fail to alloc bufmgr_nexell!\n");
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               return NULL;
-       }
-
-       /* check the master_fd which already had opened */
-       bufmgr_nexell->fd = tbm_drm_helper_get_master_fd();
-       if (bufmgr_nexell->fd < 0) {
-               bufmgr_nexell->fd = _tbm_nexell_open_drm();
-               if (bufmgr_nexell->fd < 0) {
-                       TBM_ERR("fail to open drm!\n");
-                       if (error)
-                               *error = TBM_ERROR_INVALID_OPERATION;
-                       goto fail_open_drm;
-               }
-               if (drmIsMaster(bufmgr_nexell->fd)) {
-                       tbm_drm_helper_set_tbm_master_fd(bufmgr_nexell->fd);
-                       set_master = 1;
-
-                       bufmgr_nexell->device_name = drmGetDeviceNameFromFd(bufmgr_nexell->fd);
-                       if (!bufmgr_nexell->device_name) {
-                               TBM_ERR("fail to get device name!\n");
-                               tbm_drm_helper_unset_tbm_master_fd();
-                               if (error)
-                                       *error = TBM_ERROR_INVALID_OPERATION;
-                               goto fail_get_device_name;
-                       }
-                       TBM_INFO("This is Master FD(%d) from open_drm.", bufmgr_nexell->fd);
-               } else {
-                       /* close the fd and get the authenticated fd from the master fd */
-                       close(bufmgr_nexell->fd);
-                       bufmgr_nexell->fd = -1;
-
-                       if (_check_render_node()) {
-                               bufmgr_nexell->fd = _get_render_node();//TODO
-                               if (bufmgr_nexell->fd < 0) {
-                                       TBM_ERR("fail to get render node\n");
-                                       if (error)
-                                               *error = TBM_ERROR_INVALID_OPERATION;
-                                       goto fail_get_render_node;
-                               }
-                               TBM_INFO("Use render node:%d\n", bufmgr_nexell->fd);
-                       } else {
-                               /* get the authenticated drm fd from the master fd */
-                               if (!tbm_drm_helper_get_auth_info(&(bufmgr_nexell->fd), &(bufmgr_nexell->device_name), NULL)) {
-                                       TBM_ERR("fail to get auth drm info!\n");
-                                       if (error)
-                                               *error = TBM_ERROR_INVALID_OPERATION;
-                                       goto fail_get_auth_info;
-                               }
-                               TBM_INFO("This is Authenticated FD(%d)", bufmgr_nexell->fd);
-                       }
-               }
-       } else {
-               bufmgr_nexell->device_name = drmGetDeviceNameFromFd(bufmgr_nexell->fd);
-               if (!bufmgr_nexell->device_name) {
-                       TBM_ERR("fail to get device name!\n");
-                       tbm_drm_helper_unset_tbm_master_fd();
-                       if (error)
-                               *error = TBM_ERROR_INVALID_OPERATION;
-                       goto fail_get_device_name;
-               }
-               TBM_INFO("This is Master FD from tbm_drm_helper_get_master_fd(%d)", bufmgr_nexell->fd);
-       }
-       tbm_drm_helper_set_fd(bufmgr_nexell->fd);
-
-       if (!_bufmgr_init_cache_state(bufmgr_nexell)) {
-               TBM_ERR("fail to init bufmgr cache state\n");
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               goto fail_init_cache_state;
-       }
-
-       /*Create Hash Table*/
-       bufmgr_nexell->hashBos = drmHashCreate();
-
-       /* alloc and register bufmgr_funcs */
-       bufmgr_func = tbm_backend_bufmgr_alloc_bufmgr_func(bufmgr, &err);
-       if (!bufmgr_func) {
-               TBM_ERR("fail to alloc bufmgr_func! err(%d)\n", err);
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               goto fail_alloc_bufmgr_func;
-       }
-
-       bufmgr_func->bufmgr_get_capabilities = tbm_nexell_bufmgr_get_capabilities;
-       bufmgr_func->bufmgr_bind_native_display = tbm_nexell_bufmgr_bind_native_display;
-       bufmgr_func->bufmgr_get_supported_formats = tbm_nexell_bufmgr_get_supported_formats;
-       bufmgr_func->bufmgr_get_plane_data = tbm_nexell_bufmgr_get_plane_data;
-       bufmgr_func->bufmgr_alloc_bo = tbm_nexell_bufmgr_alloc_bo;
-       bufmgr_func->bufmgr_alloc_bo_with_format = NULL;
-       bufmgr_func->bufmgr_import_fd = tbm_nexell_bufmgr_import_fd;
-       bufmgr_func->bufmgr_import_key = tbm_nexell_bufmgr_import;
-
-       err = tbm_backend_bufmgr_register_bufmgr_func(bufmgr, bufmgr_func);
-       if (err != TBM_ERROR_NONE) {
-               TBM_ERR("fail to register bufmgr_func! err(%d)\n", err);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               goto fail_register_bufmgr_func;
-       }
-       bufmgr_nexell->bufmgr_func = bufmgr_func;
-
-       /* alloc and register bo_funcs */
-       bo_func = tbm_backend_bufmgr_alloc_bo_func(bufmgr, &err);
-       if (!bo_func) {
-               TBM_ERR("fail to alloc bo_func! err(%d)\n", err);
-               if (error)
-                       *error = TBM_ERROR_OUT_OF_MEMORY;
-               goto fail_alloc_bo_func;
-       }
-
-       bo_func->bo_free = tbm_nexell_bo_free;
-       bo_func->bo_get_size = tbm_nexell_bo_get_size;
-       bo_func->bo_get_memory_types = tbm_nexell_bo_get_memory_type;
-       bo_func->bo_get_handle = tbm_nexell_bo_get_handle;
-       bo_func->bo_map = tbm_nexell_bo_map;
-       bo_func->bo_unmap = tbm_nexell_bo_unmap;
-       bo_func->bo_lock = tbm_nexell_bo_lock;
-       bo_func->bo_unlock = tbm_nexell_bo_unlock;
-       bo_func->bo_export_fd = tbm_nexell_bo_export_fd;
-       bo_func->bo_export_key = tbm_nexell_bo_export_key;
-
-       err = tbm_backend_bufmgr_register_bo_func(bufmgr, bo_func);
-       if (err != TBM_ERROR_NONE) {
-               TBM_ERR("fail to register bo_func! err(%d)\n", err);
-               if (error)
-                       *error = TBM_ERROR_INVALID_OPERATION;
-               goto fail_register_bo_func;
-       }
-       bufmgr_nexell->bo_func = bo_func;
-
-       TBM_DBG("drm_fd:%d\n", bufmgr_nexell->fd);
-
-       if (error)
-               *error = TBM_ERROR_NONE;
-
-       bufmgr_nexell->bufmgr = bufmgr;
-
-       return (tbm_backend_bufmgr_data *)bufmgr_nexell;
-
-fail_register_bo_func:
-       tbm_backend_bufmgr_free_bo_func(bufmgr, bo_func);
-fail_alloc_bo_func:
-fail_register_bufmgr_func:
-       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_func);
-fail_alloc_bufmgr_func:
-       _bufmgr_deinit_cache_state(bufmgr_nexell);
-       if (bufmgr_nexell->hashBos)
-               drmHashDestroy(bufmgr_nexell->hashBos);
-fail_init_cache_state:
-       if (set_master)
-               tbm_drm_helper_unset_tbm_master_fd();
-       tbm_drm_helper_unset_fd();
-fail_get_device_name:
-       close(bufmgr_nexell->fd);
-fail_get_auth_info:
-fail_get_render_node:
-fail_open_drm:
-       free(bufmgr_nexell);
-       return NULL;
-}
-
-tbm_backend_module tbm_backend_module_data = {
-       "nexell",
-       "Samsung",
-       TBM_BACKEND_ABI_VERSION_3_0,
-       tbm_nexell_init,
-       tbm_nexell_deinit
-};
diff --git a/src/tbm_bufmgr_tgl.h b/src/tbm_bufmgr_tgl.h
deleted file mode 100644 (file)
index fee35d5..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/**************************************************************************
- *
- * libtbm
- *
- * Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
- *
- * Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
- * Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
- *
- * 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, sub license, 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 NON-INFRINGEMENT.
- * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
- *
- * **************************************************************************/
-
-#ifndef __TBM_BUFMGR_TGL_H__
-#define __TBM_BUFMGR_TGL_H__
-
-#include <linux/ioctl.h>
-
-#ifdef ENABLE_CACHECRTL
-static char tgl_devfile[] = "/dev/slp_global_lock";
-static char tgl_devfile1[] = "/dev/tgl";
-#endif
-
-#define TGL_IOC_BASE                           0x32
-
-struct tgl_attribute {
-       unsigned int key;
-       unsigned int timeout_ms;
-};
-
-struct tgl_user_data {
-       unsigned int key;
-       unsigned int data1;
-       unsigned int data2;
-       unsigned int locked;
-};
-
-typedef enum {
-       _TGL_INIT_LOCK = 1,
-       _TGL_DESTROY_LOCK,
-       _TGL_LOCK_LOCK,
-       _TGL_UNLOCK_LOCK,
-       _TGL_SET_DATA,
-       _TGL_GET_DATA,
-} _tgl_ioctls;
-
-#define TGL_IOC_INIT_LOCK                      _IOW(TGL_IOC_BASE, _TGL_INIT_LOCK, struct tgl_attribute *)
-#define TGL_IOC_DESTROY_LOCK           _IOW(TGL_IOC_BASE, _TGL_DESTROY_LOCK, unsigned int)
-#define TGL_IOC_LOCK_LOCK                      _IOW(TGL_IOC_BASE, _TGL_LOCK_LOCK, unsigned int)
-#define TGL_IOC_UNLOCK_LOCK                    _IOW(TGL_IOC_BASE, _TGL_UNLOCK_LOCK, unsigned int)
-#define TGL_IOC_SET_DATA                       _IOW(TGL_IOC_BASE, _TGL_SET_DATA, struct tgl_user_data *)
-#define TGL_IOC_GET_DATA                       _IOW(TGL_IOC_BASE, _TGL_GET_DATA, struct tgl_user_data *)
-
-#endif                                                 /* __TBM_BUFMGR_TGL_H__ */