set same pitch to all plane of NV format
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
old mode 100755 (executable)
new mode 100644 (file)
index 846f74d..2e2dd63
@@ -48,115 +48,56 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <fcntl.h>
 #include <errno.h>
 #include <xf86drm.h>
-#include <tbm_bufmgr.h>
-#include <tbm_bufmgr_backend.h>
 #include <drm/sprd_drm.h>
 #include <pthread.h>
-#include <tbm_surface.h>
+#include <tbm_backend.h>
 #include <tbm_drm_helper.h>
-
-#define DEBUG
-#include "tbm_bufmgr_tgl.h"
+#include <tbm_log.h>
 
 //#define USE_CONTIG_ONLY
+//#define USE_CACHE
 #define USE_DMAIMPORT
 
-#define TBM_COLOR_FORMAT_COUNT 8
+#ifdef USE_CACHE
+#include "tbm_bufmgr_tgl.h"
+#endif
 
-#ifdef DEBUG
-#define LOG_TAG    "TBM_BACKEND"
-#include <dlog.h>
-static int bDebug = 0;
+#define TBM_COLOR_FORMAT_COUNT 4
 
 #define SPRD_DRM_NAME "sprd"
 
-char *
-target_name()
-{
-       FILE *f;
-       char *slash;
-       static int     initialized = 0;
-       static char app_name[128];
-
-       if ( initialized )
-               return app_name;
-
-       /* get the application name */
-       f = fopen("/proc/self/cmdline", "r");
-
-       if ( !f ) {
-               return 0;
-       }
-
-       memset(app_name, 0x00, sizeof(app_name));
-
-       if ( fgets(app_name, 100, f) == NULL ) {
-               fclose(f);
-               return 0;
-       }
-
-       fclose(f);
+#define STRERR_BUFSIZE 128
 
-       if ( (slash = strrchr(app_name, '/')) != NULL ) {
-               memmove(app_name, slash + 1, strlen(slash));
-       }
-
-       initialized = 1;
-
-       return app_name;
-}
-#define TBM_SPRD_LOG(fmt, args...)        LOGE("\033[31m"  "[%s]" fmt "\033[0m", target_name(), ##args)
-#define DBG(fmt, args...)        if(bDebug&01) LOGE("[%s]" fmt, target_name(), ##args)
-#else
-#define TBM_SPRD_LOG(...)
-#define DBG(...)
-#endif
-
-#define SIZE_ALIGN( value, base ) (((value) + ((base) - 1)) & ~((base) - 1))
+#define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1))
 
 #define TBM_SURFACE_ALIGNMENT_PLANE (64)
 #define TBM_SURFACE_ALIGNMENT_PITCH_RGB (128)
 #define TBM_SURFACE_ALIGNMENT_PITCH_YUV (16)
 
-
-/* check condition */
-#define SPRD_RETURN_IF_FAIL(cond) {\
-    if (!(cond)) {\
-        TBM_SPRD_LOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
-        return;\
-    }\
-}
-#define SPRD_RETURN_VAL_IF_FAIL(cond, val) {\
-    if (!(cond)) {\
-        TBM_SPRD_LOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
-        return val;\
-    }\
-}
-
 struct dma_buf_info {
-       unsigned long    size;
-       unsigned int    fence_supported;
-       unsigned int    padding;
+       unsigned long   size;
+       unsigned int    fence_supported;
+       unsigned int    padding;
 };
 
-#define DMA_BUF_ACCESS_READ        0x1
-#define DMA_BUF_ACCESS_WRITE        0x2
-#define DMA_BUF_ACCESS_DMA        0x4
-#define DMA_BUF_ACCESS_MAX        0x8
+#define DMA_BUF_ACCESS_READ            0x1
+#define DMA_BUF_ACCESS_WRITE           0x2
+#define DMA_BUF_ACCESS_DMA             0x4
+#define DMA_BUF_ACCESS_MAX             0x8
 
-#define DMA_FENCE_LIST_MAX         5
+#define DMA_FENCE_LIST_MAX             5
 
 struct dma_buf_fence {
-       unsigned long        ctx;
-       unsigned int        type;
+       unsigned long           ctx;
+       unsigned int            type;
 };
 
-#define DMABUF_IOCTL_BASE    'F'
-#define DMABUF_IOWR(nr, type)    _IOWR(DMABUF_IOCTL_BASE, nr, type)
+#define DMABUF_IOCTL_BASE      'F'
+#define DMABUF_IOWR(nr, type)  _IOWR(DMABUF_IOCTL_BASE, nr, type)
 
-#define DMABUF_IOCTL_GET_INFO    DMABUF_IOWR(0x00, struct dma_buf_info)
-#define DMABUF_IOCTL_GET_FENCE    DMABUF_IOWR(0x01, struct dma_buf_fence)
-#define DMABUF_IOCTL_PUT_FENCE    DMABUF_IOWR(0x02, struct dma_buf_fence)
+#define DMABUF_IOCTL_GET_INFO  DMABUF_IOWR(0x00, struct dma_buf_info)
+#define DMABUF_IOCTL_GET_FENCE DMABUF_IOWR(0x01, struct dma_buf_fence)
+#define DMABUF_IOCTL_PUT_FENCE DMABUF_IOWR(0x02, struct dma_buf_fence)
 
 /* tgl key values */
 #define GLOBAL_KEY   ((unsigned int)(-1))
@@ -178,20 +119,15 @@ 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;
+               unsigned int cntFlush:16;       /*Flush all index for sync */
+               unsigned int isCached:1;
+               unsigned int isDirtied:2;
        } data;
 };
 
 typedef struct _tbm_bufmgr_sprd *tbm_bufmgr_sprd;
 typedef struct _tbm_bo_sprd *tbm_bo_sprd;
 
-typedef struct _sprd_private {
-       int ref_count;
-       struct _tbm_bo_sprd *bo_priv;
-} PrivGem;
-
 /* tbm buffor object for sprd */
 struct _tbm_bo_sprd {
        int fd;
@@ -209,8 +145,6 @@ struct _tbm_bo_sprd {
        unsigned int flags_sprd;
        unsigned int flags_tbm;
 
-       PrivGem *private;
-
        pthread_mutex_t mutex;
        struct dma_buf_fence dma_fence[DMA_FENCE_LIST_MAX];
        int device;
@@ -218,6 +152,8 @@ struct _tbm_bo_sprd {
 
        tbm_bo_cache_state cache_state;
        unsigned int map_cnt;
+
+       tbm_bufmgr_sprd bufmgr_sprd;
 };
 
 /* tbm bufmgr private for sprd */
@@ -229,9 +165,13 @@ struct _tbm_bufmgr_sprd {
 
        int tgl_fd;
 
+       char *device_name;
        void *bind_display;
 
-       char *device_name;
+       tbm_backend_bufmgr_func *bufmgr_func;
+       tbm_backend_bo_func *bo_func;
+
+       tbm_bufmgr bufmgr;
 };
 
 char *STR_DEVICE[] = {
@@ -250,29 +190,47 @@ char *STR_OPT[] = {
 };
 
 
-uint32_t tbm_sprd_color_format_list[TBM_COLOR_FORMAT_COUNT] = { TBM_FORMAT_RGBA8888,
-                                                               TBM_FORMAT_BGRA8888,
-                                                               TBM_FORMAT_RGBX8888,
-                                                               TBM_FORMAT_RGB888,
-                                                               TBM_FORMAT_NV12,
-                                                               TBM_FORMAT_NV21,
-                                                               TBM_FORMAT_YUV420,
-                                                               TBM_FORMAT_YVU420
-                                                             };
+uint32_t tbm_sprd_color_format_list[TBM_COLOR_FORMAT_COUNT] = {
+                                                                               TBM_FORMAT_ARGB8888,
+                                                                               TBM_FORMAT_XRGB8888,
+                                                                               TBM_FORMAT_NV12,
+                                                                               TBM_FORMAT_YUV420
+                                                                       };
+
+#ifdef USE_CACHE
+static inline int
+_tgl_get_version(int fd)
+{
+       struct tgl_ver_data data;
+       int err;
+       char buf[STRERR_BUFSIZE];
+
+       err = ioctl(fd, TGL_IOCTL_GET_VERSION, &data);
+       if (err) {
+               TBM_ERR("error(%s) %s:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE));
+               return 0;
+       }
+
+       TBM_DBG("tgl version is (%u, %u).\n", data.major, data.minor);
+
+       return 1;
+}
 
 static inline int
 _tgl_init(int fd, unsigned int key)
 {
-       struct tgl_attribute attr;
+       struct tgl_reg_data data;
        int err;
+       char buf[STRERR_BUFSIZE];
 
-       attr.key = key;
-       attr.timeout_ms = 1000;
+       data.key = key;
+       data.timeout_ms = 1000;
 
-       err = ioctl(fd, TGL_IOC_INIT_LOCK, &attr);
+       err = ioctl(fd, TGL_IOCTL_REGISTER, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d\n",
-                            getpid(), strerror(errno), __func__, __LINE__, key);
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
                return 0;
        }
 
@@ -282,13 +240,15 @@ _tgl_init(int fd, unsigned int key)
 static inline int
 _tgl_destroy(int fd, unsigned int key)
 {
+       struct tgl_reg_data data;
        int err;
+       char buf[STRERR_BUFSIZE];
 
-       err = ioctl(fd, TGL_IOC_DESTROY_LOCK, key);
+       data.key = key;
+       err = ioctl(fd, TGL_IOCTL_UNREGISTER, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                            "error(%s) %s:%d key:%d\n",
-                            getpid(), strerror(errno), __func__, __LINE__, key);
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
                return 0;
        }
 
@@ -296,15 +256,32 @@ _tgl_destroy(int fd, unsigned int key)
 }
 
 static inline int
-_tgl_lock(int fd, unsigned int key)
+_tgl_lock(int fd, unsigned int key, int opt)
 {
+       struct tgl_lock_data data;
        int err;
+       char buf[STRERR_BUFSIZE];
+       enum tgl_type_data tgl_type;
+
+       switch (opt) {
+       case TBM_OPTION_READ:
+               tgl_type = TGL_TYPE_READ;
+               break;
+       case TBM_OPTION_WRITE:
+               tgl_type = TGL_TYPE_WRITE;
+               break;
+       default:
+               tgl_type = TGL_TYPE_NONE;
+               break;
+       }
+
+       data.key = key;
+       data.type = tgl_type;
 
-       err = ioctl(fd, TGL_IOC_LOCK_LOCK, key);
+       err = ioctl(fd, TGL_IOCTL_LOCK, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                            "error(%s) %s:%d key:%d\n",
-                            getpid(), strerror(errno), __func__, __LINE__, key);
+               TBM_ERR("error(%s) key:%d opt:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key, opt);
                return 0;
        }
 
@@ -314,13 +291,17 @@ _tgl_lock(int fd, unsigned int key)
 static inline int
 _tgl_unlock(int fd, unsigned int key)
 {
+       struct tgl_lock_data data;
        int err;
+       char buf[STRERR_BUFSIZE];
 
-       err = ioctl(fd, TGL_IOC_UNLOCK_LOCK, key);
+       data.key = key;
+       data.type = TGL_TYPE_NONE;
+
+       err = ioctl(fd, TGL_IOCTL_UNLOCK, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                            "error(%s) %s:%d key:%d\n",
-                            getpid(), strerror(errno), __func__, __LINE__, key);
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
                return 0;
        }
 
@@ -330,16 +311,17 @@ _tgl_unlock(int fd, unsigned int key)
 static inline int
 _tgl_set_data(int fd, unsigned int key, unsigned int val)
 {
+       struct tgl_usr_data data;
        int err;
-       struct tgl_user_data arg;
+       char buf[STRERR_BUFSIZE];
+
+       data.key = key;
+       data.data1 = val;
 
-       arg.key = key;
-       arg.data1 = val;
-       err = ioctl(fd, TGL_IOC_SET_DATA, &arg);
+       err = ioctl(fd, TGL_IOCTL_SET_DATA, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                            "error(%s) %s:%d key:%d\n",
-                            getpid(), strerror(errno), __func__, __LINE__, key);
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
                return 0;
        }
 
@@ -349,134 +331,119 @@ _tgl_set_data(int fd, unsigned int key, unsigned int val)
 static inline unsigned int
 _tgl_get_data(int fd, unsigned int key, unsigned int *locked)
 {
+       struct tgl_usr_data data = { 0, };
        int err;
-       struct tgl_user_data arg = { 0, };
+       char buf[STRERR_BUFSIZE];
 
-       arg.key = key;
-       err = ioctl(fd, TGL_IOC_GET_DATA, &arg);
+       data.key = key;
+
+       err = ioctl(fd, TGL_IOCTL_GET_DATA, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                            "error(%s) %s:%d key:%d\n",
-                            getpid(), strerror(errno), __func__, __LINE__, key);
+               TBM_ERR("error(%s) key:%d\n",
+                       strerror_r(errno, buf, STRERR_BUFSIZE), key);
                return 0;
        }
 
        if (locked)
-               *locked = arg.locked;
+               *locked = (unsigned int)data.status;
 
-       return arg.data1;
+       return data.data1;
 }
+#endif
 
 static int
-_tbm_sprd_open_drm()
+_tbm_sprd_open_drm(void)
 {
-       int fd = -1;
+#ifdef HAVE_UDEV
+       struct udev_device *drm_device = NULL;
+       struct udev_list_entry *entry = NULL;
+       struct udev_enumerate *e;
+       const char *filepath;
+       struct udev *udev;
+       struct stat s;
+#endif
+       int fd;
 
        fd = drmOpen(SPRD_DRM_NAME, NULL);
-       if (fd < 0) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "warning %s:%d fail to open drm\n",
-                             getpid(), __FUNCTION__, __LINE__);
-       }
+       if (fd != -1)
+               return fd;
 
 #ifdef HAVE_UDEV
-       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 fd = -1;
-               int ret;
-
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "%s:%d search drm-device by udev\n",
-                             getpid(), __FUNCTION__, __LINE__);
-
-               udev = udev_new();
-               if (!udev) {
-                       TBM_SPRD_LOG("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), "sprd-drm") == 0) {
-                                       drm_device = device;
-                                       DBG("[%s] Found render device: '%s' (%s)\n",
-                                           target_name(),
-                                           udev_device_get_syspath(drm_device),
-                                           udev_device_get_sysname(device_parent));
-                                       break;
-                               }
+       TBM_DBG("warning fail to open drm. 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)) {
+               struct udev_device *device, *device_parent;
+
+               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), "sprd-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_device_unref(device);
+       }
 
-               udev_enumerate_unref(e);
+       udev_enumerate_unref(e);
 
-               /* Get device file path. */
-               filepath = udev_device_get_devnode(drm_device);
-               if (!filepath) {
-                       TBM_SPRD_LOG("udev_device_get_devnode() failed.\n");
-                       udev_device_unref(drm_device);
-                       udev_unref(udev);
-                       return -1;
-               }
+       /* 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_SPRD_LOG("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
-                       udev_device_unref(drm_device);
-                       udev_unref(udev);
-                       return -1;
-               }
+       udev_device_unref(drm_device);
+       udev_unref(udev);
 
-               ret = fstat(fd, &s);
-               if (ret) {
-                       TBM_SPRD_LOG("fstat() failed %s.\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");
+               return -1;
+       }
 
-               udev_device_unref(drm_device);
-               udev_unref(udev);
+       if (fstat(fd, &s)) {
+               TBM_ERR("fstat() failed %s.\n");
+               close(fd);
+               return -1;
        }
 #endif
 
        return fd;
 }
 
+#ifdef USE_CACHE
 static int
-_sprd_bo_cache_flush (tbm_bo bo, int flags)
+_sprd_bo_cache_flush(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int flags)
 {
-       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
 
        /* cache flush is managed by kernel side when using dma-fence. */
        if (bufmgr_sprd->use_dma_fence)
                return 1;
-
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-       tbm_bo_sprd bo_sprd;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-
-#ifdef USE_CACHE
+// TODO: The tm1 kernel does not support ioctl for cache flush right now.
+//       The drm in tm1 kernel has to support cache_flush to turn on this feature(TBM_SRPD_CACHE_FLUSH).
+#if TBM_SRPD_CACHE_FLUSH
        struct drm_sprd_gem_cache_op cache_op = {0, };
        int ret;
 
@@ -509,56 +476,61 @@ _sprd_bo_cache_flush (tbm_bo bo, int flags)
        if (flags & TBM_SPRD_CACHE_ALL)
                cache_op.flags |= SPRD_DRM_ALL_CACHES_CORES;
 
-       ret = drmCommandWriteRead (bufmgr_sprd->fd, DRM_SPRD_GEM_CACHE_OP, &cache_op,
-                                  sizeof(cache_op));
+       ret = drmCommandWriteRead(bufmgr_sprd->fd, DRM_SPRD_GEM_CACHE_OP, &cache_op,
+                       sizeof(cache_op));
        if (ret) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d fail to flush the cache.\n",
-                             getpid(), __FUNCTION__, __LINE__);
+               TBM_ERR("error fail to flush the cache.\n");
                return 0;
        }
 #endif
 
        return 1;
 }
+#endif
 
 static int
-_bo_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
+_bo_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int import)
 {
-       tbm_bo_cache_state cache_state;
+#ifdef USE_CACHE
+       TBM_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
 
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       if (bufmgr_sprd->use_dma_fence)
+               return 1;
 
        _tgl_init(bufmgr_sprd->tgl_fd, bo_sprd->name);
 
-       cache_state.data.isDirtied = DEVICE_NONE;
-       cache_state.data.isCached = 0;
-       cache_state.data.cntFlush = 0;
+       tbm_bo_cache_state cache_state;
+
+       if (import == 0) {
+               cache_state.data.isDirtied = DEVICE_NONE;
+               cache_state.data.isCached = 0;
+               cache_state.data.cntFlush = 0;
 
-       _tgl_set_data(bufmgr_sprd->tgl_fd, bo_sprd->name, cache_state.val);
+               _tgl_set_data(bufmgr_sprd->tgl_fd, bo_sprd->name, cache_state.val);
+       }
+#endif
 
        return 1;
 }
 
 static int
-_bo_set_cache_state(tbm_bo bo, int device, int opt)
+_bo_set_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int device, int opt)
 {
-       tbm_bo_sprd bo_sprd;
-       tbm_bufmgr_sprd bufmgr_sprd;
+#ifdef USE_CACHE
+       TBM_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
+
        char need_flush = 0;
        unsigned short cntFlush = 0;
 
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       if (bufmgr_sprd->use_dma_fence)
+               return 1;
 
        if (bo_sprd->flags_sprd & SPRD_BO_NONCACHABLE)
                return 1;
 
-       /* get cache state of a bo */
+       /* get cache state of a bo_sprd */
        bo_sprd->cache_state.val = _tgl_get_data(bufmgr_sprd->tgl_fd, bo_sprd->name, NULL);
 
        /* get global cache flush count */
@@ -595,31 +567,30 @@ _bo_set_cache_state(tbm_bo bo, int device, int opt)
                        _tgl_set_data(bufmgr_sprd->tgl_fd, GLOBAL_KEY, (unsigned int)(++cntFlush));
 
                /* call cache flush */
-               _sprd_bo_cache_flush (bo, need_flush);
+               _sprd_bo_cache_flush(bufmgr_sprd, bo_sprd, need_flush);
 
-               DBG("[libtbm:%d] \tcache(%d,%d)....flush:0x%x, cntFlush(%d)\n",
-                   getpid(),
+               TBM_DBG("\tcache(%d,%d)....flush:0x%x, cntFlush(%d)\n",
                    bo_sprd->cache_state.data.isCached,
                    bo_sprd->cache_state.data.isDirtied,
                    need_flush,
                    cntFlush);
        }
+#endif
 
        return 1;
 }
 
 static int
-_bo_save_cache_state(tbm_bo bo)
+_bo_save_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
 {
-       unsigned short cntFlush = 0;
-       tbm_bo_sprd bo_sprd;
-       tbm_bufmgr_sprd bufmgr_sprd;
+#ifdef USE_CACHE
+       TBM_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
 
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
+       if (bufmgr_sprd->use_dma_fence)
+               return 1;
 
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       unsigned short cntFlush = 0;
 
        /* get global cache flush count */
        cntFlush = (unsigned short)_tgl_get_data(bufmgr_sprd->tgl_fd, GLOBAL_KEY, NULL);
@@ -627,28 +598,79 @@ _bo_save_cache_state(tbm_bo bo)
        /* save global cache flush count */
        bo_sprd->cache_state.data.cntFlush = cntFlush;
        _tgl_set_data(bufmgr_sprd->tgl_fd, bo_sprd->name, bo_sprd->cache_state.val);
+#endif
 
        return 1;
 }
 
 static void
-_bo_destroy_cache_state(tbm_bo bo)
+_bo_destroy_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
 {
-       tbm_bo_sprd bo_sprd;
-       tbm_bufmgr_sprd bufmgr_sprd;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_IF_FAIL (bo_sprd != NULL);
+#ifdef USE_CACHE
+       TBM_RETURN_IF_FAIL(bo_sprd != NULL);
+       TBM_RETURN_IF_FAIL(bufmgr_sprd != NULL);
 
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_IF_FAIL (bufmgr_sprd != NULL);
+       if (bufmgr_sprd->use_dma_fence)
+               return;
 
        _tgl_destroy(bufmgr_sprd->tgl_fd, bo_sprd->name);
+#endif
+}
+
+static int
+_bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
+{
+#ifdef USE_CACHE
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
+
+       if (bufmgr_sprd->use_dma_fence)
+               return 1;
+
+       /* open tgl fd for saving cache flush data */
+       bufmgr_sprd->tgl_fd = open(tgl_devfile, O_RDWR);
+
+       if (bufmgr_sprd->tgl_fd < 0) {
+           bufmgr_sprd->tgl_fd = open(tgl_devfile1, O_RDWR);
+           if (bufmgr_sprd->tgl_fd < 0) {
+                   TBM_ERR("fail to open global_lock:%s\n",
+                                       tgl_devfile1);
+                   return 0;
+           }
+       }
+
+       if (!_tgl_get_version(bufmgr_sprd->tgl_fd)) {
+               TBM_ERR("fail to get tgl_version. tgl init failed.\n");
+               close(bufmgr_sprd->tgl_fd);
+               return 0;
+       }
+
+       if (!_tgl_init(bufmgr_sprd->tgl_fd, GLOBAL_KEY)) {
+               TBM_ERR("fail to initialize the tgl\n");
+               close(bufmgr_sprd->tgl_fd);
+               return 0;
+       }
+#endif
+
+       return 1;
+}
+
+static void
+_bufmgr_deinit_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
+{
+#ifdef USE_CACHE
+       TBM_RETURN_IF_FAIL(bufmgr_sprd != NULL);
+
+       if (bufmgr_sprd->use_dma_fence)
+               return;
+
+       if (bufmgr_sprd->tgl_fd >= 0)
+               close(bufmgr_sprd->tgl_fd);
+#endif
 }
 
 #ifndef USE_CONTIG_ONLY
 static unsigned int
-_get_sprd_flag_from_tbm (unsigned int ftbm)
+_get_sprd_flag_from_tbm(unsigned int ftbm)
 {
        unsigned int flags = 0;
 
@@ -659,11 +681,10 @@ _get_sprd_flag_from_tbm (unsigned int ftbm)
         * To be updated appropriately once DRM-GEM supports different heap id masks.
         * */
 
-       if (ftbm & TBM_BO_SCANOUT) {
+       if (ftbm & TBM_BO_SCANOUT)
                flags = SPRD_BO_CONTIG;
-       } else {
+       else
                flags = SPRD_BO_NONCONTIG | SPRD_BO_DEV_SYSTEM;
-       }
 
        if (ftbm & TBM_BO_WC)
                flags |= SPRD_BO_WC;
@@ -674,7 +695,7 @@ _get_sprd_flag_from_tbm (unsigned int ftbm)
 }
 
 static unsigned int
-_get_tbm_flag_from_sprd (unsigned int fsprd)
+_get_tbm_flag_from_sprd(unsigned int fsprd)
 {
        unsigned int flags = 0;
 
@@ -695,15 +716,13 @@ _get_tbm_flag_from_sprd (unsigned int fsprd)
 #endif
 
 static unsigned int
-_get_name (int fd, unsigned int gem)
+_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_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d fail to get flink gem=%d\n",
-                             getpid(), __FUNCTION__, __LINE__, gem);
+       if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) {
+               TBM_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d", gem);
                return 0;
        }
 
@@ -711,10 +730,11 @@ _get_name (int fd, unsigned int gem)
 }
 
 static tbm_bo_handle
-_sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
+_sprd_bo_handle(tbm_bo_sprd bo_sprd, int device)
 {
        tbm_bo_handle bo_handle;
-       memset (&bo_handle, 0x0, sizeof (uint64_t));
+
+       memset(&bo_handle, 0x0, sizeof(uint64_t));
 
        switch (device) {
        case TBM_DEVICE_DEFAULT:
@@ -727,10 +747,8 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
 
                        arg.handle = bo_sprd->gem;
                        arg.size = bo_sprd->size;
-                       if (drmCommandWriteRead (bo_sprd->fd, DRM_SPRD_GEM_MMAP, &arg, sizeof(arg))) {
-                               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                             "error %s:%d Cannot usrptr gem=%d\n",
-                                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
+                       if (drmCommandWriteRead(bo_sprd->fd, DRM_SPRD_GEM_MMAP, &arg, sizeof(arg))) {
+                               TBM_ERR("error Cannot usrptr gem=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->pBase = (void *)((uint32_t)arg.mapped);
@@ -743,10 +761,8 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
                if (!bo_sprd->dmabuf) {
                        struct drm_prime_handle arg = {0, };
                        arg.handle = bo_sprd->gem;
-                       if (drmIoctl (bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                             "error %s:%d Cannot dmabuf=%d\n",
-                                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
+                       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                               TBM_ERR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->dmabuf = arg.fd;
@@ -760,19 +776,14 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
        case TBM_DEVICE_MM:
 #ifdef USE_HEAP_ID
                //TODO : Add ioctl for GSP MAP once available.
-               DBG ("[libtbm-sprd:%d] %s In case TBM_DEVICE_MM:  \n", getpid(),
-                    __FUNCTION_);
-               _
-
+               TBM_DBG("%s In case TBM_DEVICE_MM:  \n", __FUNCTION_);
 #else
                if (!bo_sprd->dmabuf) {
                        struct drm_prime_handle arg = {0, };
 
                        arg.handle = bo_sprd->gem;
-                       if (drmIoctl (bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                             "error %s:%d Cannot dmabuf=%d\n",
-                                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
+                       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                               TBM_ERR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->dmabuf = arg.fd;
@@ -782,6 +793,7 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
 #endif
                break;
        default:
+               TBM_ERR("Not supported device:%d\n", device);
                bo_handle.ptr = (void *) NULL;
                break;
        }
@@ -789,792 +801,74 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
        return bo_handle;
 }
 
-static int
-tbm_sprd_bo_size (tbm_bo bo)
+static tbm_bufmgr_capability
+tbm_sprd_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE;
 
-       tbm_bo_sprd bo_sprd;
+       capabilities = TBM_BUFMGR_CAPABILITY_SHARE_KEY|TBM_BUFMGR_CAPABILITY_SHARE_FD;
 
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       return bo_sprd->size;
+       return capabilities;
 }
 
-static void *
-tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
+static tbm_error_e
+tbm_sprd_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-       tbm_bo_sprd bo_sprd;
-       tbm_bufmgr_sprd bufmgr_sprd;
-       unsigned int sprd_flags;
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
-
-       bo_sprd = calloc (1, sizeof(struct _tbm_bo_sprd));
-       if (!bo_sprd) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d fail to allocate the bo private\n",
-                             getpid(), __FUNCTION__, __LINE__);
-               return 0;
-       }
-
-#ifdef USE_CONTIG_ONLY
-       flags = TBM_BO_SCANOUT;
-       sprd_flags = SPRD_BO_CONTIG;
-#else
-       sprd_flags = _get_sprd_flag_from_tbm (flags);
-       if ((flags & TBM_BO_SCANOUT) &&
-           size <= 4 * 1024) {
-               sprd_flags |= SPRD_BO_NONCONTIG;
-       }
-#endif // USE_CONTIG_ONLY
-       struct drm_sprd_gem_create arg = {0, };
-       arg.size = (uint64_t)size;
-       arg.flags = sprd_flags;
-       if (drmCommandWriteRead(bufmgr_sprd->fd, DRM_SPRD_GEM_CREATE, &arg,
-                               sizeof(arg))) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot create bo(flag:%x, size:%d)\n",
-                             getpid(), __FUNCTION__, __LINE__, arg.flags, (unsigned int)arg.size);
-               free (bo_sprd);
-               return 0;
-       }
-
-       bo_sprd->fd = bufmgr_sprd->fd;
-       bo_sprd->gem = arg.handle;
-       bo_sprd->size = size;
-       bo_sprd->flags_tbm = flags;
-       bo_sprd->flags_sprd = sprd_flags;
-       bo_sprd->name = _get_name (bo_sprd->fd, bo_sprd->gem);
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, TBM_ERROR_INVALID_PARAMETER);
 
-       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd)) {
-               TBM_SPRD_LOG ("error fail init cache state(%d)\n", bo_sprd->name);
-               free (bo_sprd);
-               return 0;
-       }
-
-       pthread_mutex_init(&bo_sprd->mutex, NULL);
-
-       if (bufmgr_sprd->use_dma_fence
-           && !bo_sprd->dmabuf) {
-               struct drm_prime_handle arg = {0, };
-
-               arg.handle = bo_sprd->gem;
-               if (drmIoctl (bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d Cannot dmabuf=%d\n",
-                                     getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
-                       free (bo_sprd);
-                       return 0;
-               }
-               bo_sprd->dmabuf = arg.fd;
-       }
-
-       /* add bo to hash */
-       PrivGem *privGem = calloc (1, sizeof(PrivGem));
-       privGem->ref_count = 1;
-       privGem->bo_priv = bo_sprd;
-       if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot insert bo to Hash(%d)\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->name);
+       if (!tbm_drm_helper_wl_auth_server_init(native_display, bufmgr_sprd->fd,
+                                          bufmgr_sprd->device_name, 0)) {
+               TBM_ERR("fail to tbm_drm_helper_wl_server_init\n");
+               return TBM_ERROR_INVALID_OPERATION;
        }
 
-       DBG ("[libtbm-sprd:%d] %s size:%d, gem:%d(%d), flags:%d(%d)\n", getpid(),
-            __FUNCTION__, bo_sprd->size,
-            bo_sprd->gem, bo_sprd->name,
-            flags, sprd_flags);
+       bufmgr_sprd->bind_display = native_display;
 
-       return (void *)bo_sprd;
+       return TBM_ERROR_NONE;
 }
 
-static void
-tbm_sprd_bo_free(tbm_bo bo)
+static tbm_error_e
+tbm_sprd_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data,
+                                                       uint32_t **formats, uint32_t *num)
 {
-       tbm_bo_sprd bo_sprd;
-       tbm_bufmgr_sprd bufmgr_sprd;
-
-       if (!bo)
-               return;
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_IF_FAIL (bufmgr_sprd != NULL);
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_IF_FAIL (bo_sprd != NULL);
-
-       DBG ("[libtbm-sprd:%d] %s size:%d, gem:%d(%d)\n",
-            getpid(), __FUNCTION__, bo_sprd->size, bo_sprd->gem, bo_sprd->name);
-
-       if (bo_sprd->pBase) {
-               if (munmap(bo_sprd->pBase, bo_sprd->size) == -1) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d\n",
-                                     getpid(), __FUNCTION__, __LINE__);
-               }
-       }
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       uint32_t *color_formats;
 
-       /* close dmabuf */
-       if (bo_sprd->dmabuf) {
-               close (bo_sprd->dmabuf);
-               bo_sprd->dmabuf = 0;
-       }
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, TBM_ERROR_INVALID_PARAMETER);
 
-       /* delete bo from hash */
-       PrivGem *privGem = NULL;
-       int ret;
+       color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
+       if (color_formats == NULL)
+               return TBM_ERROR_OUT_OF_MEMORY;
 
-       ret = drmHashLookup (bufmgr_sprd->hashBos, bo_sprd->name, (void **)&privGem);
-       if (ret == 0) {
-               privGem->ref_count--;
-               if (privGem->ref_count == 0) {
-                       drmHashDelete (bufmgr_sprd->hashBos, bo_sprd->name);
-                       free (privGem);
-                       privGem = NULL;
-               }
-       } else {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "warning %s:%d Cannot find bo to Hash(%d), ret=%d\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->name, ret);
-       }
+       memcpy(color_formats, tbm_sprd_color_format_list, sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT);
 
-       _bo_destroy_cache_state(bo);
+       *formats = color_formats;
+       *num = TBM_COLOR_FORMAT_COUNT;
 
-       /* Free gem handle */
-       struct drm_gem_close arg = {0, };
-       memset (&arg, 0, sizeof(arg));
-       arg.handle = bo_sprd->gem;
-       if (drmIoctl (bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg)) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d\n",
-                             getpid(), __FUNCTION__, __LINE__);
-       }
+       TBM_DBG("supported format count = %d\n", *num);
 
-       free (bo_sprd);
+       return TBM_ERROR_NONE;
 }
 
-
-static void *
-tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
+static tbm_error_e
+tbm_sprd_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)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-       tbm_bufmgr_sprd bufmgr_sprd;
-       tbm_bo_sprd bo_sprd;
-       PrivGem *privGem = NULL;
-       int ret;
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
-
-       ret = drmHashLookup (bufmgr_sprd->hashBos, key, (void **)&privGem);
-       if (ret == 0) {
-               privGem->ref_count++;
-               return privGem->bo_priv;
-       }
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       int bpp;
+       int _offset = 0;
+       int _pitch = 0;
+       int _size = 0;
+       int _bo_idx = 0;
+       int _align_height = 0;
 
-       struct drm_gem_open arg = {0, };
-       struct drm_sprd_gem_info info = {0, };
-
-       arg.name = key;
-       if (drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot open gem name=%d\n",
-                             getpid(), __FUNCTION__, __LINE__, key);
-               return 0;
-       }
-
-       info.handle = arg.handle;
-       if (drmCommandWriteRead(bufmgr_sprd->fd,
-                               DRM_SPRD_GEM_GET,
-                               &info,
-                               sizeof(struct drm_sprd_gem_info))) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot get gem info=%d\n",
-                             getpid(), __FUNCTION__, __LINE__, key);
-               return 0;
-       }
-
-       bo_sprd = calloc (1, sizeof(struct _tbm_bo_sprd));
-       if (!bo_sprd) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d fail to allocate the bo private\n",
-                             getpid(), __FUNCTION__, __LINE__);
-               return 0;
-       }
-
-       bo_sprd->fd = bufmgr_sprd->fd;
-       bo_sprd->gem = arg.handle;
-       bo_sprd->size = arg.size;
-       bo_sprd->flags_sprd = info.flags;
-       bo_sprd->name = key;
-#ifdef USE_CONTIG_ONLY
-       bo_sprd->flags_sprd = SPRD_BO_CONTIG;
-       bo_sprd->flags_tbm |= TBM_BO_SCANOUT;
-#else
-       bo_sprd->flags_tbm = _get_tbm_flag_from_sprd (bo_sprd->flags_sprd);
-#endif
-
-       if (!_tgl_init(bufmgr_sprd->tgl_fd, bo_sprd->name)) {
-               TBM_SPRD_LOG ("error fail tgl init(%d)\n", bo_sprd->name);
-               free (bo_sprd);
-               return 0;
-       }
-
-       if (!bo_sprd->dmabuf) {
-               struct drm_prime_handle arg = {0, };
-
-               arg.handle = bo_sprd->gem;
-               if (drmIoctl (bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d Cannot dmabuf=%d\n",
-                                     getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
-                       free (bo_sprd);
-                       return 0;
-               }
-               bo_sprd->dmabuf = arg.fd;
-       }
-
-       /* add bo to hash */
-       privGem = calloc (1, sizeof(PrivGem));
-       privGem->ref_count = 1;
-       privGem->bo_priv = bo_sprd;
-       if (drmHashInsert (bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot insert bo to Hash(%d)\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->name);
-       }
-
-       DBG ("[libtbm-sprd:%d] %s size:%d, gem:%d(%d), flags:%d(%d)\n", getpid(),
-            __FUNCTION__, bo_sprd->size,
-            bo_sprd->gem, bo_sprd->name,
-            bo_sprd->flags_tbm, bo_sprd->flags_sprd);
-
-       return (void *)bo_sprd;
-}
-
-static void *
-tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-       tbm_bufmgr_sprd bufmgr_sprd;
-       tbm_bo_sprd bo_sprd;
-       PrivGem *privGem = NULL;
-       int ret;
-       unsigned int name;
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
-
-       //getting handle from fd
-       unsigned int gem = 0;
-       struct drm_prime_handle arg = {0, };
-
-       arg.fd = key;
-       arg.flags = 0;
-       if (drmIoctl (bufmgr_sprd->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) {
-               TBM_SPRD_LOG ("error bo:%p Cannot get gem handle from fd:%d (%s)\n",
-                             bo, arg.fd, strerror(errno));
-               return NULL;
-       }
-       gem = arg.handle;
-
-       name = _get_name (bufmgr_sprd->fd, gem);
-
-       ret = drmHashLookup (bufmgr_sprd->hashBos, name, (void **)&privGem);
-       if (ret == 0) {
-               if (gem == privGem->bo_priv->gem) {
-                       privGem->ref_count++;
-                       return privGem->bo_priv;
-               }
-       }
-
-       unsigned int real_size = -1;
-       struct drm_sprd_gem_info info = {0, };
-
-       /* 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). */
-       real_size = lseek(key, 0, SEEK_END);
-
-       info.handle = gem;
-       if (drmCommandWriteRead(bufmgr_sprd->fd,
-                               DRM_SPRD_GEM_GET,
-                               &info,
-                               sizeof(struct drm_sprd_gem_info))) {
-               TBM_SPRD_LOG ("error bo:%p Cannot get gem info from gem:%d, fd:%d (%s)\n",
-                             bo, gem, key, strerror(errno));
-               return 0;
-       }
-
-       if (real_size == -1)
-               real_size = info.size;
-
-       bo_sprd = calloc (1, sizeof(struct _tbm_bo_sprd));
-       if (!bo_sprd) {
-               TBM_SPRD_LOG ("error bo:%p fail to allocate the bo private\n", bo);
-               return 0;
-       }
-
-       bo_sprd->fd = bufmgr_sprd->fd;
-       bo_sprd->gem = gem;
-       bo_sprd->size = real_size;
-       bo_sprd->flags_sprd = info.flags;
-       bo_sprd->flags_tbm = _get_tbm_flag_from_sprd (bo_sprd->flags_sprd);
-
-       bo_sprd->name = name;
-       if (!bo_sprd->name) {
-               TBM_SPRD_LOG ("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n",
-                             bo, gem, key, strerror(errno));
-               free (bo_sprd);
-               return 0;
-       }
-
-       if (!_tgl_init(bufmgr_sprd->tgl_fd, bo_sprd->name)) {
-               TBM_SPRD_LOG ("error fail tgl init(%d)\n", bo_sprd->name);
-               free (bo_sprd);
-               return 0;
-       }
-
-       /* add bo to hash */
-       privGem = NULL;
-
-       privGem = calloc (1, sizeof(PrivGem));
-       if (!privGem) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Fail to calloc privGem\n",
-                             getpid(), __FUNCTION__, __LINE__);
-               free (bo_sprd);
-               return 0;
-       }
-
-       privGem->ref_count = 1;
-       privGem->bo_priv = bo_sprd;
-       if (drmHashInsert (bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) {
-               TBM_SPRD_LOG ("error bo:%p Cannot insert bo to Hash(%d) from gem:%d, fd:%d\n",
-                             bo, bo_sprd->name, gem, key);
-       }
-
-       DBG (" [%s] bo:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
-            target_name(),
-            bo,
-            bo_sprd->gem, bo_sprd->name,
-            bo_sprd->dmabuf,
-            key,
-            bo_sprd->flags_tbm, bo_sprd->flags_sprd,
-            bo_sprd->size);
-
-       return (void *)bo_sprd;
-}
-
-static unsigned int
-tbm_sprd_bo_export (tbm_bo bo)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-       tbm_bo_sprd bo_sprd;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-
-       if (!bo_sprd->name) {
-               bo_sprd->name = _get_name(bo_sprd->fd, bo_sprd->gem);
-               if (!bo_sprd->name) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d Cannot get name\n",
-                                     getpid(), __FUNCTION__, __LINE__);
-                       return 0;
-               }
-       }
-
-       DBG ("[libtbm-sprd:%d] %s size:%d, gem:%d(%d), flags:%d(%d)\n", getpid(),
-            __FUNCTION__, bo_sprd->size,
-            bo_sprd->gem, bo_sprd->name,
-            bo_sprd->flags_tbm, bo_sprd->flags_sprd);
-
-       return (unsigned int)bo_sprd->name;
-}
-
-tbm_fd
-tbm_sprd_bo_export_fd (tbm_bo bo)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, -1);
-
-       tbm_bo_sprd bo_sprd;
-       int ret;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, -1);
-
-       struct drm_prime_handle arg = {0, };
-
-       arg.handle = bo_sprd->gem;
-       ret = drmIoctl (bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
-       if (ret) {
-               TBM_SPRD_LOG ("error bo:%p Cannot dmabuf=%d (%s)\n",
-                             bo, bo_sprd->gem, strerror(errno));
-               return (tbm_fd) ret;
-       }
-
-       DBG (" [%s] bo:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
-            target_name(),
-            bo,
-            bo_sprd->gem, bo_sprd->name,
-            bo_sprd->dmabuf,
-            arg.fd,
-            bo_sprd->flags_tbm, bo_sprd->flags_sprd,
-            bo_sprd->size);
-
-       return (tbm_fd)arg.fd;
-}
-
-
-static tbm_bo_handle
-tbm_sprd_bo_get_handle (tbm_bo bo, int device)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, (tbm_bo_handle) NULL);
-
-       tbm_bo_handle bo_handle;
-       tbm_bo_sprd bo_sprd;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, (tbm_bo_handle) NULL);
-
-       if (!bo_sprd->gem) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot map gem=%d\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
-               return (tbm_bo_handle) NULL;
-       }
-
-       DBG ("[libtbm-sprd:%d] %s gem:%d(%d), %s\n", getpid(),
-            __FUNCTION__, bo_sprd->gem, bo_sprd->name, STR_DEVICE[device]);
-
-       /*Get mapped bo_handle*/
-       bo_handle = _sprd_bo_handle (bo_sprd, device);
-       if (bo_handle.ptr == NULL) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot get handle: gem:%d, device:%d\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem, device);
-               return (tbm_bo_handle) NULL;
-       }
-
-       return bo_handle;
-}
-
-static tbm_bo_handle
-tbm_sprd_bo_map (tbm_bo bo, int device, int opt)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, (tbm_bo_handle) NULL);
-
-       tbm_bo_handle bo_handle;
-       tbm_bo_sprd bo_sprd;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, (tbm_bo_handle) NULL);
-
-       if (!bo_sprd->gem) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot map gem=%d\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
-               return (tbm_bo_handle) NULL;
-       }
-
-       DBG ("[libtbm-sprd:%d] %s gem:%d(%d), %s, %s\n", getpid(),
-            __FUNCTION__, bo_sprd->gem, bo_sprd->name, STR_DEVICE[device], STR_OPT[opt]);
-
-       /*Get mapped bo_handle*/
-       bo_handle = _sprd_bo_handle (bo_sprd, device);
-       if (bo_handle.ptr == NULL) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot get handle: gem:%d, device:%d, opt:%d\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem, device, opt);
-               return (tbm_bo_handle) NULL;
-       }
-
-       if (bo_sprd->map_cnt == 0)
-               _bo_set_cache_state (bo, device, opt);
-
-       bo_sprd->map_cnt++;
-
-       return bo_handle;
-}
-
-static int
-tbm_sprd_bo_unmap (tbm_bo bo)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-       tbm_bo_sprd bo_sprd;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-
-       if (!bo_sprd->gem)
-               return 0;
-
-       bo_sprd->map_cnt--;
-
-       if (bo_sprd->map_cnt == 0)
-               _bo_save_cache_state (bo);
-
-       DBG ("[libtbm-sprd:%d] %s gem:%d(%d) \n", getpid(),
-            __FUNCTION__, bo_sprd->gem, bo_sprd->name);
-
-       return 1;
-}
-
-static int
-tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-#if USE_BACKEND_LOCK
-       tbm_bufmgr_sprd bufmgr_sprd;
-       tbm_bo_sprd bo_sprd;
-       int ret = 0;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
-
-       if (bufmgr_sprd->use_dma_fence) {
-               struct dma_buf_fence fence;
-
-               memset(&fence, 0, sizeof(struct dma_buf_fence));
-
-               /* Check if the given type is valid or not. */
-               if (opt & TBM_OPTION_WRITE) {
-                       if (device == TBM_DEVICE_CPU)
-                               fence.type = DMA_BUF_ACCESS_WRITE;
-                       else if (device == TBM_DEVICE_3D)
-                               fence.type = DMA_BUF_ACCESS_WRITE | DMA_BUF_ACCESS_DMA;
-                       else {
-                               DBG ("[libtbm-sprd:%d] %s GET_FENCE is ignored(device type is not 3D/CPU),\n",
-                                    getpid(), __FUNCTION__);
-                               return 0;
-                       }
-               } else if (opt & TBM_OPTION_READ) {
-                       if (device == TBM_DEVICE_CPU)
-                               fence.type = DMA_BUF_ACCESS_READ;
-                       else if (device == TBM_DEVICE_3D)
-                               fence.type = DMA_BUF_ACCESS_READ | DMA_BUF_ACCESS_DMA;
-                       else {
-                               DBG ("[libtbm-sprd:%d] %s GET_FENCE is ignored(device type is not 3D/CPU),\n",
-                                    getpid(), __FUNCTION__);
-                               return 0;
-                       }
-               } else {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] error %s:%d Invalid argument\n", getpid(),
-                                     __FUNCTION__, __LINE__);
-                       return 0;
-               }
-
-               /* Check if the tbm manager supports dma fence or not. */
-               if (!bufmgr_sprd->use_dma_fence) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d  Not support DMA FENCE(%s)\n",
-                                     getpid(), __FUNCTION__, __LINE__, strerror(errno) );
-                       return 0;
-
-               }
-
-               ret = ioctl(bo_sprd->dmabuf, DMABUF_IOCTL_GET_FENCE, &fence);
-               if (ret < 0) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d  Can not set GET FENCE(%s)\n",
-                                     getpid(), __FUNCTION__, __LINE__, strerror(errno) );
-                       return 0;
-               }
-
-               pthread_mutex_lock(&bo_sprd->mutex);
-               int i;
-               for (i = 0; i < DMA_FENCE_LIST_MAX; i++) {
-                       if (bo_sprd->dma_fence[i].ctx == 0) {
-                               bo_sprd->dma_fence[i].type = fence.type;
-                               bo_sprd->dma_fence[i].ctx = fence.ctx;
-                               break;
-                       }
-               }
-               if (i == DMA_FENCE_LIST_MAX) {
-                       //TODO: if dma_fence list is full, it needs realloc. I will fix this. by minseok3.kim
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d  fence list is full\n",
-                                     getpid(), __FUNCTION__, __LINE__);
-               }
-               pthread_mutex_unlock(&bo_sprd->mutex);
-
-               DBG ("[libtbm-sprd:%d] %s DMABUF_IOCTL_GET_FENCE! flink_id=%d dmabuf=%d\n",
-                    getpid(),
-                    __FUNCTION__, bo_sprd->name, bo_sprd->dmabuf);
-       } else {
-               ret = _tgl_lock(bufmgr_sprd->tgl_fd, bo_sprd->name);
-
-               DBG ("[libtbm-sprd:%d] lock tgl flink_id:%d\n",
-                    getpid(), __FUNCTION__, bo_sprd->name);
-
-               return ret;
-       }
-
-#endif
-       return 1;
-}
-
-static int
-tbm_sprd_bo_unlock(tbm_bo bo)
-{
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
-
-#if USE_BACKEND_LOCK
-       tbm_bufmgr_sprd bufmgr_sprd;
-       tbm_bo_sprd bo_sprd;
-       int ret = 0;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
-
-       if (bufmgr_sprd->use_dma_fence) {
-               struct dma_buf_fence fence;
-
-               if (!bo_sprd->dma_fence[0].ctx) {
-                       DBG ("[libtbm-sprd:%d] %s FENCE not support or ignored,\n", getpid(),
-                            __FUNCTION__);
-                       return 0;
-               }
-
-               if (!bo_sprd->dma_fence[0].type) {
-                       DBG ("[libtbm-sprd:%d] %s device type is not 3D/CPU,\n", getpid(),
-                            __FUNCTION__);
-                       return 0;
-               }
-
-               pthread_mutex_lock(&bo_sprd->mutex);
-               fence.type = bo_sprd->dma_fence[0].type;
-               fence.ctx = bo_sprd->dma_fence[0].ctx;
-               int i;
-               for (i = 1; i < DMA_FENCE_LIST_MAX; i++) {
-                       bo_sprd->dma_fence[i - 1].type = bo_sprd->dma_fence[i].type;
-                       bo_sprd->dma_fence[i - 1].ctx = bo_sprd->dma_fence[i].ctx;
-               }
-               bo_sprd->dma_fence[DMA_FENCE_LIST_MAX - 1].type = 0;
-               bo_sprd->dma_fence[DMA_FENCE_LIST_MAX - 1].ctx = 0;
-               pthread_mutex_unlock(&bo_sprd->mutex);
-
-               ret = ioctl(bo_sprd->dmabuf, DMABUF_IOCTL_PUT_FENCE, &fence);
-               if (ret < 0) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d  Can not set PUT FENCE(%s)\n",
-                                     getpid(), __FUNCTION__, __LINE__, strerror(errno) );
-                       return 0;
-               }
-
-               DBG ("[libtbm-sprd:%d] %s DMABUF_IOCTL_PUT_FENCE! flink_id=%d dmabuf=%d\n",
-                    getpid(),
-                    __FUNCTION__, bo_sprd->name, bo_sprd->dmabuf);
-       } else {
-               ret = _tgl_unlock(bufmgr_sprd->tgl_fd, bo_sprd->name);
-
-               DBG ("[libtbm-sprd:%d] unlock tgl flink_id:%d\n",
-                    getpid(), __FUNCTION__, bo_sprd->name);
-
-               return ret;
-       }
-#endif
-       return 1;
-}
-
-static void
-tbm_sprd_bufmgr_deinit (void *priv)
-{
-       SPRD_RETURN_IF_FAIL (priv != NULL);
-
-       tbm_bufmgr_sprd bufmgr_sprd;
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)priv;
-
-       if (bufmgr_sprd->hashBos) {
-               unsigned long key;
-               void *value;
-
-               while (drmHashFirst(bufmgr_sprd->hashBos, &key, &value) > 0) {
-                       free (value);
-                       drmHashDelete (bufmgr_sprd->hashBos, key);
-               }
-
-               drmHashDestroy (bufmgr_sprd->hashBos);
-               bufmgr_sprd->hashBos = NULL;
-       }
-
-       if (bufmgr_sprd->bind_display)
-               tbm_drm_helper_wl_auth_server_deinit();
-
-       if (bufmgr_sprd->device_name)
-               free(bufmgr_sprd->device_name);
-
-       close (bufmgr_sprd->tgl_fd);
-       close (bufmgr_sprd->fd);
-
-       free (bufmgr_sprd);
-}
-
-int
-tbm_sprd_surface_supported_format(uint32_t **formats, uint32_t *num)
-{
-       uint32_t *color_formats = NULL;
-
-       color_formats = (uint32_t *)calloc (1,
-                                           sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
-
-       if ( color_formats == NULL ) {
-               return 0;
-       }
-       memcpy( color_formats, tbm_sprd_color_format_list ,
-               sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT );
-
-
-       *formats = color_formats;
-       *num = TBM_COLOR_FORMAT_COUNT;
-
-
-       return 1;
-}
-
-
-/**
- * @brief get the plane data of the surface.
- * @param[in] surface : the surface
- * @param[in] width : the width of the surface
- * @param[in] height : the height of the surface
- * @param[in] format : the format of the surface
- * @param[in] plane_idx : the format of the surface
- * @param[out] size : the size of the plane
- * @param[out] offset : the offset of the plane
- * @param[out] pitch : the pitch of the plane
- * @param[out] padding : the padding of the plane
- * @return 1 if this function succeeds, otherwise 0.
- */
-int
-tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
-                               tbm_format format, int plane_idx, uint32_t *size, uint32_t *offset,
-                               uint32_t *pitch, int *bo_idx)
-{
-       int ret = 1;
-       int bpp;
-       int _offset = 0;
-       int _pitch = 0;
-       int _size = 0;
-       int _bo_idx = 0;
+       TBM_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, TBM_ERROR_INVALID_PARAMETER);
 
        switch (format) {
                /* 16 bpp RGB */
@@ -1649,15 +943,22 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
        case TBM_FORMAT_NV12:
        case TBM_FORMAT_NV21:
                bpp = 12;
-               if (plane_idx == 0) {
+//             if (plane_idx == 0)
+               {
                        _offset = 0;
-                       _pitch = SIZE_ALIGN( width , TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _pitch = SIZE_ALIGN(width , TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
-               } else if ( plane_idx == 1 ) {
-                       _offset = width * height;
-                       _pitch = SIZE_ALIGN( width , TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
+                       if (plane_idx == 0)
+                               break;
+               }
+//             else if (plane_idx == 1)
+               {
+                       _offset = _size;
+                       _pitch = SIZE_ALIGN(width , TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                }
                break;
@@ -1677,7 +978,7 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
                //else if( plane_idx ==1 )
                {
                        _offset += _size;
-                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
                        _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                }
@@ -1709,7 +1010,8 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
                {
                        _offset = 0;
                        _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
-                       _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+                       _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                        if (plane_idx == 0)
                                break;
@@ -1718,7 +1020,8 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
                {
                        _offset += _size;
                        _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                        if (plane_idx == 1)
                                break;
@@ -1727,7 +1030,8 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
                {
                        _offset += _size;
                        _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
-                       _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE);
+                       _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+                       _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                }
                break;
@@ -1788,221 +1092,857 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
                        _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
                        _bo_idx = 0;
                }
-               break;
-       default:
-               bpp = 0;
-               break;
+               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_sprd_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size,
+                                       tbm_bo_memory_type flags, tbm_error_e *error)
+{
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       tbm_bo_sprd bo_sprd;
+       unsigned int sprd_flags;
+
+       if (bufmgr_sprd == NULL) {
+               TBM_ERR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       bo_sprd = calloc(1, sizeof(struct _tbm_bo_sprd));
+       if (!bo_sprd) {
+               TBM_ERR("error fail to allocate the bo_sprd\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_sprd->bufmgr_sprd = bufmgr_sprd;
+
+#ifdef USE_CONTIG_ONLY
+       flags = TBM_BO_SCANOUT;
+       sprd_flags = SPRD_BO_CONTIG;
+#else
+       sprd_flags = _get_sprd_flag_from_tbm(flags);
+       if ((flags & TBM_BO_SCANOUT) && (size <= 4 * 1024))
+               sprd_flags |= SPRD_BO_NONCONTIG;
+#endif // USE_CONTIG_ONLY
+
+       struct drm_sprd_gem_create arg = {0, };
+
+       arg.size = (uint64_t)size;
+       arg.flags = sprd_flags;
+       if (drmCommandWriteRead(bufmgr_sprd->fd, DRM_SPRD_GEM_CREATE, &arg,
+                               sizeof(arg))) {
+               TBM_ERR("error Cannot create bo_sprd(flag:%x, size:%d)\n",
+                       arg.flags, (unsigned int)arg.size);
+               free(bo_sprd);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       bo_sprd->fd = bufmgr_sprd->fd;
+       bo_sprd->gem = arg.handle;
+       bo_sprd->size = size;
+       bo_sprd->flags_tbm = flags;
+       bo_sprd->flags_sprd = sprd_flags;
+       bo_sprd->name = _get_name(bo_sprd->fd, bo_sprd->gem);
+
+       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 0)) {
+               TBM_ERR("error fail init cache state(%d)\n", bo_sprd->name);
+               free(bo_sprd);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       pthread_mutex_init(&bo_sprd->mutex, NULL);
+
+       if (bufmgr_sprd->use_dma_fence && !bo_sprd->dmabuf) {
+               struct drm_prime_handle arg = {0, };
+
+               arg.handle = bo_sprd->gem;
+               if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_ERR("error Cannot dmabuf=%d\n", bo_sprd->gem);
+                       free(bo_sprd);
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       return NULL;
+               }
+               bo_sprd->dmabuf = arg.fd;
+       }
+
+       /* add bo_sprd to hash */
+       if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)bo_sprd) < 0)
+               TBM_ERR("Cannot insert bo_sprd to Hash(%d)\n", bo_sprd->name);
+
+       TBM_DBG("%s size:%d, gem:%d(%d), flags:%d(%d)\n",
+               __FUNCTION__, bo_sprd->size,
+               bo_sprd->gem, bo_sprd->name,
+               flags, sprd_flags);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_sprd;
+}
+
+static tbm_backend_bo_data *
+tbm_sprd_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_error_e *error)
+{
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       tbm_bo_sprd bo_sprd;
+       unsigned int gem;
+       unsigned int name;
+       int ret;
+       char buf[STRERR_BUFSIZE];
+
+       if (bufmgr_sprd == 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;
+       if (drmIoctl(bufmgr_sprd->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_sprd->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_sprd->hashBos, name, (void **)&bo_sprd);
+       if (ret == 0) {
+               if (gem == bo_sprd->gem) {
+                       if (error)
+                               *error = TBM_ERROR_NONE;
+                       return bo_sprd;
+               }
+       }
+
+       /* Determine size of bo_sprd.  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;
+       struct drm_sprd_gem_info info = {0, };
+
+       real_size = lseek(key, 0, SEEK_END);
+
+       info.handle = gem;
+       if (drmCommandWriteRead(bufmgr_sprd->fd,
+                               DRM_SPRD_GEM_GET,
+                               &info,
+                               sizeof(struct drm_sprd_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_sprd = calloc(1, sizeof(struct _tbm_bo_sprd));
+       if (!bo_sprd) {
+               TBM_ERR("error bo_sprd:%p fail to allocate the bo_sprd\n", bo_sprd);
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+       bo_sprd->bufmgr_sprd = bufmgr_sprd;
+
+       bo_sprd->fd = bufmgr_sprd->fd;
+       bo_sprd->gem = gem;
+       bo_sprd->size = real_size;
+       bo_sprd->flags_sprd = info.flags;
+       bo_sprd->flags_tbm = _get_tbm_flag_from_sprd(bo_sprd->flags_sprd);
+       bo_sprd->name = name;
+
+       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
+               TBM_ERR("error fail init cache state(%d)\n", bo_sprd->name);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               goto fail_init_cache;
+       }
+
+       /* add bo_sprd to hash */
+       if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)bo_sprd) < 0)
+               TBM_ERR("bo_sprd:%p Cannot insert bo_sprd to Hash(%d) from gem:%d, fd:%d\n",
+                              bo_sprd, bo_sprd->name, gem, key);
+
+       TBM_DBG("bo_sprd:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
+            bo_sprd,
+            bo_sprd->gem, bo_sprd->name,
+            bo_sprd->dmabuf,
+            key,
+            bo_sprd->flags_tbm, bo_sprd->flags_sprd,
+            bo_sprd->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_sprd;
+
+fail_init_cache:
+       free(bo_sprd);
+       return NULL;
+}
+
+static tbm_backend_bo_data *
+tbm_sprd_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error)
+{
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       tbm_bo_sprd bo_sprd;
+       int ret;
+
+       if (bufmgr_sprd == NULL) {
+               TBM_ERR("bufmgr_data is null\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
+       }
+
+       ret = drmHashLookup(bufmgr_sprd->hashBos, key, (void **)&bo_sprd);
+       if (ret == 0) {
+               if (error)
+                       *error = TBM_ERROR_NONE;
+               return (tbm_backend_bo_data *)bo_sprd;
+       }
+
+       struct drm_gem_open arg = {0, };
+       struct drm_sprd_gem_info info = {0, };
+
+       arg.name = key;
+       if (drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
+               TBM_ERR("error Cannot open gem name=%d\n", key);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
+
+       info.handle = arg.handle;
+       if (drmCommandWriteRead(bufmgr_sprd->fd,
+                               DRM_SPRD_GEM_GET,
+                               &info,
+                               sizeof(struct drm_sprd_gem_info))) {
+               TBM_ERR("error Cannot get gem info=%d\n", key);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               goto fail_get_gem;
+       }
+
+       bo_sprd = calloc(1, sizeof(struct _tbm_bo_sprd));
+       if (!bo_sprd) {
+               TBM_ERR("error fail to allocate the bo_sprd\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               goto fail_alloc_bo;
+       }
+       bo_sprd->bufmgr_sprd = bufmgr_sprd;
+
+       bo_sprd->fd = bufmgr_sprd->fd;
+       bo_sprd->gem = arg.handle;
+       bo_sprd->size = arg.size;
+       bo_sprd->flags_sprd = info.flags;
+       bo_sprd->name = key;
+#ifdef USE_CONTIG_ONLY
+       bo_sprd->flags_sprd = SPRD_BO_CONTIG;
+       bo_sprd->flags_tbm |= TBM_BO_SCANOUT;
+#else
+       bo_sprd->flags_tbm = _get_tbm_flag_from_sprd(bo_sprd->flags_sprd);
+#endif
+
+       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
+               TBM_ERR("error fail init cache state(%d)\n", bo_sprd->name);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               goto fail_init_cache;
+       }
+
+       if (!bo_sprd->dmabuf) {
+               struct drm_prime_handle arg = {0, };
+
+               arg.handle = bo_sprd->gem;
+               if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_ERR("error Cannot dmabuf=%d\n", bo_sprd->gem);
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       goto fail_prime_handle_to_fd;
+               }
+               bo_sprd->dmabuf = arg.fd;
        }
 
-       *size = _size;
-       *offset = _offset;
-       *pitch = _pitch;
-       *bo_idx = _bo_idx;
+       /* add bo_sprd to hash */
+       if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)bo_sprd) < 0)
+               TBM_ERR("Cannot insert bo_sprd to Hash(%d)\n", bo_sprd->name);
+
+       TBM_DBG("    bo_sprd:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
+           bo_sprd,
+           bo_sprd->gem, bo_sprd->name,
+           bo_sprd->dmabuf,
+           bo_sprd->flags_tbm, bo_sprd->flags_sprd,
+           bo_sprd->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_backend_bo_data *)bo_sprd;
 
-       return ret;
+fail_prime_handle_to_fd:
+       _bo_destroy_cache_state(bufmgr_sprd, bo_sprd);
+fail_init_cache:
+       free(bo_sprd);
+fail_alloc_bo:
+fail_get_gem:
+       {
+               struct drm_gem_close gem_close_arg = {arg.handle, 0};
+               drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_GEM_CLOSE, &gem_close_arg);
+       }
+       return NULL;
 }
 
-int
-tbm_sprd_bo_get_flags (tbm_bo bo)
+static void
+tbm_sprd_bo_free(tbm_backend_bo_data *bo_data)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
+       tbm_bo_sprd temp;
+       tbm_bufmgr_sprd bufmgr_sprd;
+       char buf[STRERR_BUFSIZE];
+       int ret;
 
-       tbm_bo_sprd bo_sprd;
+       if (!bo_data)
+               return;
+
+       bufmgr_sprd = bo_sprd->bufmgr_sprd;
+       if (!bufmgr_sprd)
+               return;
 
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
+       TBM_DBG("      bo_sprd:%p, gem:%d(%d), fd:%d, size:%d\n",
+           bo_sprd,
+           bo_sprd->gem, bo_sprd->name,
+           bo_sprd->dmabuf,
+           bo_sprd->size);
 
-       return bo_sprd->flags_tbm;
+       if (bo_sprd->pBase) {
+               if (munmap(bo_sprd->pBase, bo_sprd->size) == -1) {
+                       TBM_ERR("bo_sprd:%p fail to munmap(%s)\n",
+                                      bo_sprd, strerror_r(errno, buf, STRERR_BUFSIZE));
+               }
+       }
+
+       /* close dmabuf */
+       if (bo_sprd->dmabuf) {
+               close(bo_sprd->dmabuf);
+               bo_sprd->dmabuf = 0;
+       }
+
+       /* delete bo from hash */
+       ret = drmHashLookup(bufmgr_sprd->hashBos, bo_sprd->name,
+                            (void **)&temp);
+       if (ret == 0)
+               drmHashDelete(bufmgr_sprd->hashBos, bo_sprd->name);
+       else
+               TBM_ERR("Cannot find bo_sprd to Hash(%d), ret=%d\n", bo_sprd->name, ret);
+
+       if (temp != bo_sprd)
+               TBM_ERR("hashBos probably has several BOs with same name!!!\n");
+
+       _bo_destroy_cache_state(bufmgr_sprd, bo_sprd);
+
+       /* Free gem handle */
+       struct drm_gem_close arg = {0, };
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo_sprd->gem;
+       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg))
+               TBM_ERR("bo_sprd:%p fail to gem close.(%s)\n",
+                              bo_sprd, strerror_r(errno, buf, STRERR_BUFSIZE));
+       free(bo_sprd);
 }
 
-int
-tbm_sprd_bufmgr_bind_native_display (tbm_bufmgr bufmgr, void *NativeDisplay)
+static int
+tbm_sprd_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error)
 {
-       tbm_bufmgr_sprd bufmgr_sprd;
-
-       bufmgr_sprd = tbm_backend_get_priv_from_bufmgr(bufmgr);
-       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
 
-       if (!tbm_drm_helper_wl_auth_server_init(NativeDisplay, bufmgr_sprd->fd,
-                                          bufmgr_sprd->device_name, 0)) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] error:Fail to tbm_drm_helper_wl_server_init\n");
+       if (!bo_sprd) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
                return 0;
        }
 
-       bufmgr_sprd->bind_display = NativeDisplay;
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       return 1;
+       return bo_sprd->size;
 }
 
-MODULEINITPPROTO (init_tbm_bufmgr_priv);
+static tbm_bo_memory_type
+tbm_sprd_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
 
-static TBMModuleVersionInfo SprdVersRec = {
-       "sprd",
-       "Samsung",
-       TBM_ABI_VERSION,
-};
+       if (!bo_sprd) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return TBM_BO_DEFAULT;
+       }
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return bo_sprd->flags_tbm;
+}
+
+static tbm_bo_handle
+tbm_sprd_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error)
+{
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
+       tbm_bo_handle bo_handle;
+
+       if (!bo_sprd) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       if (!bo_sprd->gem) {
+               TBM_ERR("Cannot map gem=%d\n", bo_sprd->gem);
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       TBM_DBG("bo_sprd:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d, %s\n",
+           bo_sprd,
+           bo_sprd->gem, bo_sprd->name,
+           bo_sprd->dmabuf,
+           bo_sprd->flags_tbm, bo_sprd->flags_sprd,
+           bo_sprd->size,
+           STR_DEVICE[device]);
+
+       /*Get mapped bo_handle*/
+       bo_handle = _sprd_bo_handle(bo_sprd, device);
+       if (bo_handle.ptr == NULL) {
+               TBM_ERR("Cannot get handle: gem:%d, device:%d\n",
+                       bo_sprd->gem, device);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return (tbm_bo_handle) NULL;
+       }
 
-TBMModuleData tbmModuleData = { &SprdVersRec, init_tbm_bufmgr_priv};
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-int
-init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
+       return bo_handle;
+}
+
+static tbm_bo_handle
+tbm_sprd_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device,
+                               tbm_bo_access_option opt, tbm_error_e *error)
 {
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
+       tbm_bo_handle bo_handle;
        tbm_bufmgr_sprd bufmgr_sprd;
-       tbm_bufmgr_backend bufmgr_backend;
 
-       if (!bufmgr)
-               return 0;
+       if (!bo_sprd) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
 
-       bufmgr_sprd = calloc (1, sizeof(struct _tbm_bufmgr_sprd));
+       bufmgr_sprd = bo_sprd->bufmgr_sprd;
        if (!bufmgr_sprd) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to alloc bufmgr_sprd!\n", getpid());
-               return 0;
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
+       }
+
+       if (!bo_sprd->gem) {
+               TBM_ERR("Cannot map gem=%d\n", bo_sprd->gem);
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return (tbm_bo_handle) NULL;
        }
 
-       if (tbm_backend_is_display_server()) {
-               int master_fd = -1;
+       TBM_DBG("       bo_sprd:%p, gem:%d(%d), fd:%d, %s, %s\n",
+           bo_sprd,
+           bo_sprd->gem, bo_sprd->name,
+           bo_sprd->dmabuf,
+           STR_DEVICE[device],
+           STR_OPT[opt]);
 
-               bufmgr_sprd->fd = -1;
-               master_fd = tbm_drm_helper_get_master_fd();
-               if (master_fd < 0) {
-                       bufmgr_sprd->fd = _tbm_sprd_open_drm();
-                       tbm_drm_helper_set_master_fd(bufmgr_sprd->fd);
-               } else {
-                       bufmgr_sprd->fd = dup(master_fd);
-               }
+       /*Get mapped bo_handle*/
+       bo_handle = _sprd_bo_handle(bo_sprd, device);
+       if (bo_handle.ptr == NULL) {
+               TBM_ERR("Cannot get handle: gem:%d, device:%d, opt:%d\n",
+                       bo_sprd->gem, device, opt);
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return (tbm_bo_handle) NULL;
+       }
 
-               if (bufmgr_sprd->fd < 0) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
-                       free (bufmgr_sprd);
-                       return 0;
-               }
+       if (bo_sprd->map_cnt == 0)
+               _bo_set_cache_state(bufmgr_sprd, bo_sprd, device, opt);
 
-               bufmgr_sprd->device_name = drmGetDeviceNameFromFd(bufmgr_sprd->fd);
+       bo_sprd->map_cnt++;
 
-               if (!bufmgr_sprd->device_name)
-               {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to get device name!\n", getpid());
-                       close(bufmgr_sprd->fd);
-                       free (bufmgr_sprd);
-                       return 0;
-               }
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-       } else {
-               if (!tbm_drm_helper_get_auth_info(&(bufmgr_sprd->fd), &(bufmgr_sprd->device_name), NULL)) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to get auth drm info!\n", getpid());
-                       free (bufmgr_sprd);
-                       return 0;
-               }
+       return bo_handle;
+}
+
+static tbm_error_e
+tbm_sprd_bo_unmap(tbm_backend_bo_data *bo_data)
+{
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
+       tbm_bufmgr_sprd bufmgr_sprd;
+
+       if (!bo_sprd)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       bufmgr_sprd = bo_sprd->bufmgr_sprd;
+       if (!bufmgr_sprd)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       if (!bo_sprd->gem)
+               return TBM_ERROR_INVALID_PARAMETER;
+
+       bo_sprd->map_cnt--;
+
+       if (bo_sprd->map_cnt == 0)
+               _bo_save_cache_state(bufmgr_sprd, bo_sprd);
+
+       TBM_DBG("     bo_sprd:%p, gem:%d(%d), fd:%d\n",
+           bo_sprd,
+           bo_sprd->gem, bo_sprd->name,
+           bo_sprd->dmabuf);
+
+       return TBM_ERROR_NONE;
+}
+
+static tbm_fd
+tbm_sprd_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
+       struct drm_prime_handle arg = {0, };
+       int ret;
+       char buf[STRERR_BUFSIZE];
+
+       if (!bo_sprd) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return -1;
        }
 
-       /* open tgl fd for saving cache flush data */
-       bufmgr_sprd->tgl_fd = open(tgl_devfile, O_RDWR);
+       arg.handle = bo_sprd->gem;
+       ret = drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
+       if (ret) {
+               TBM_ERR("bo_sprd:%p Cannot dmabuf=%d (%s)\n",
+                              bo_sprd, bo_sprd->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
+               if (error)
+                       *error = TBM_ERROR_INVALID_OPERATION;
+               return (tbm_fd) ret;
+       }
 
-       if (bufmgr_sprd->tgl_fd < 0) {
-               bufmgr_sprd->tgl_fd = open(tgl_devfile1, O_RDWR);
-               if (bufmgr_sprd->tgl_fd < 0) {
-                       TBM_SPRD_LOG("[libtbm:%d] "
-                                    "error: Fail to open global_lock:%s\n",
-                                    getpid(), tgl_devfile);
-
-                       close(bufmgr_sprd->fd);
-                       free (bufmgr_sprd);
+       TBM_DBG("bo_sprd:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
+               bo_sprd,
+               bo_sprd->gem, bo_sprd->name,
+               bo_sprd->dmabuf,
+               arg.fd,
+               bo_sprd->flags_tbm, bo_sprd->flags_sprd,
+               bo_sprd->size);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       return (tbm_fd)arg.fd;
+}
+
+static tbm_key
+tbm_sprd_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error)
+{
+       tbm_bo_sprd bo_sprd = (tbm_bo_sprd)bo_data;
+
+       if (!bo_sprd) {
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return 0;
+       }
+
+       if (!bo_sprd->name) {
+               bo_sprd->name = _get_name(bo_sprd->fd, bo_sprd->gem);
+               if (!bo_sprd->name) {
+                       TBM_ERR("error Cannot get name\n");
+                       if (error)
+                               *error = TBM_ERROR_INVALID_PARAMETER;
                        return 0;
                }
        }
 
-       if (!_tgl_init(bufmgr_sprd->tgl_fd, GLOBAL_KEY)) {
-               TBM_SPRD_LOG("[libtbm:%d] "
-                            "error: Fail to initialize the tgl\n",
-                            getpid());
+       TBM_DBG("    bo_sprd:%p, gem:%d(%d), fd:%d, flags:%d(%d), size:%d\n",
+           bo_sprd,
+           bo_sprd->gem, bo_sprd->name,
+           bo_sprd->dmabuf,
+           bo_sprd->flags_tbm, bo_sprd->flags_sprd,
+           bo_sprd->size);
 
-               close(bufmgr_sprd->fd);
-               close(bufmgr_sprd->tgl_fd);
+       if (error)
+               *error = TBM_ERROR_NONE;
 
-               free (bufmgr_sprd);
-               return 0;
-       }
+       return (tbm_key)bo_sprd->name;
+}
+
+static void
+tbm_sprd_deinit(tbm_backend_bufmgr_data *bufmgr_data)
+{
+       tbm_bufmgr_sprd bufmgr_sprd = (tbm_bufmgr_sprd)bufmgr_data;
+       tbm_bufmgr bufmgr;
+       tbm_error_e error;
+       unsigned long key;
+       void *value;
 
-       //Create Hash Table
-       bufmgr_sprd->hashBos = drmHashCreate ();
+       TBM_RETURN_IF_FAIL(bufmgr_sprd != NULL);
 
-       //Check if the tbm manager supports dma fence or not.
-       int fp = open("/sys/module/dmabuf_sync/parameters/enabled", O_RDONLY);
-       int length;
-       char buf[1];
-       if (fp != -1) {
-               length = read(fp, buf, 1);
+       bufmgr = bufmgr_sprd->bufmgr;
 
-               if (length == 1 && buf[0] == '1')
-                       bufmgr_sprd->use_dma_fence = 1;
+       tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_sprd->bufmgr_func);
+       tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_sprd->bo_func);
 
-               close(fp);
+       if (bufmgr_sprd->hashBos) {
+               while (drmHashFirst(bufmgr_sprd->hashBos, &key, &value) > 0) {
+                       free(value);
+                       drmHashDelete(bufmgr_sprd->hashBos, key);
+               }
+
+               drmHashDestroy(bufmgr_sprd->hashBos);
+               bufmgr_sprd->hashBos = NULL;
        }
 
-       bufmgr_backend = tbm_backend_alloc();
-       if (!bufmgr_backend) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
+       if (bufmgr_sprd->bind_display)
+               tbm_drm_helper_wl_auth_server_deinit();
 
-               close(bufmgr_sprd->fd);
-               close(bufmgr_sprd->tgl_fd);
+       if (tbm_backend_bufmgr_query_display_server(bufmgr, &error))
+               tbm_drm_helper_unset_tbm_master_fd();
 
-               free (bufmgr_sprd);
-               return 0;
-       }
+       tbm_drm_helper_unset_fd();
 
-       bufmgr_backend->priv = (void *)bufmgr_sprd;
-       bufmgr_backend->bufmgr_deinit = tbm_sprd_bufmgr_deinit;
-       bufmgr_backend->bo_size = tbm_sprd_bo_size;
-       bufmgr_backend->bo_alloc = tbm_sprd_bo_alloc;
-       bufmgr_backend->bo_free = tbm_sprd_bo_free;
-       bufmgr_backend->bo_import = tbm_sprd_bo_import;
-       bufmgr_backend->bo_import_fd = tbm_sprd_bo_import_fd;
-       bufmgr_backend->bo_export = tbm_sprd_bo_export;
-       bufmgr_backend->bo_export_fd = tbm_sprd_bo_export_fd;
-       bufmgr_backend->bo_get_handle = tbm_sprd_bo_get_handle;
-       bufmgr_backend->bo_map = tbm_sprd_bo_map;
-       bufmgr_backend->bo_unmap = tbm_sprd_bo_unmap;
-       bufmgr_backend->surface_get_plane_data = tbm_sprd_surface_get_plane_data;
-       bufmgr_backend->surface_supported_format = tbm_sprd_surface_supported_format;
-       bufmgr_backend->bo_get_flags = tbm_sprd_bo_get_flags;
-       bufmgr_backend->bo_lock = NULL;
-       bufmgr_backend->bo_lock2 = tbm_sprd_bo_lock;
-       bufmgr_backend->bo_unlock = tbm_sprd_bo_unlock;
-       bufmgr_backend->bufmgr_bind_native_display = tbm_sprd_bufmgr_bind_native_display;
-
-       bufmgr_backend->flags |= TBM_USE_2_0_BACKEND;
-
-       if (!tbm_backend_init (bufmgr, bufmgr_backend)) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to init backend!\n", getpid());
-               tbm_backend_free (bufmgr_backend);
+       if (bufmgr_sprd->device_name)
+               free(bufmgr_sprd->device_name);
 
-               close(bufmgr_sprd->tgl_fd);
-               close(bufmgr_sprd->fd);
+       _bufmgr_deinit_cache_state(bufmgr_sprd);
 
-               free (bufmgr_sprd);
-               return 0;
+       close(bufmgr_sprd->fd);
+
+       free(bufmgr_sprd);
+}
+
+static tbm_backend_bufmgr_data *
+tbm_sprd_init(tbm_bufmgr bufmgr, tbm_error_e *error)
+{
+       tbm_bufmgr_sprd bufmgr_sprd = NULL;
+       tbm_backend_bufmgr_func *bufmgr_func = NULL;
+       tbm_backend_bo_func *bo_func = NULL;
+       int fp;
+       tbm_error_e err;
+
+       if (!bufmgr) {
+               TBM_ERR("bufmgr is null.\n");
+               if (error)
+                       *error = TBM_ERROR_INVALID_PARAMETER;
+               return NULL;
        }
 
-#ifdef DEBUG
-       {
-               char *env;
-               env = getenv ("TBM_SPRD_DEBUG");
-               if (env) {
-                       bDebug = atoi (env);
-                       TBM_SPRD_LOG ("TBM_SPRD_DEBUG=%s\n", env);
-               } else {
-                       bDebug = 0;
+       bufmgr_sprd = calloc(1, sizeof(struct _tbm_bufmgr_sprd));
+       if (!bufmgr_sprd) {
+               TBM_ERR("fail to alloc bufmgr_sprd!\n");
+               if (error)
+                       *error = TBM_ERROR_OUT_OF_MEMORY;
+               return NULL;
+       }
+
+       if (tbm_backend_bufmgr_query_display_server(bufmgr, &err)) {
+               bufmgr_sprd->fd = tbm_drm_helper_get_master_fd();
+               if (bufmgr_sprd->fd < 0) {
+                       bufmgr_sprd->fd = _tbm_sprd_open_drm();
+                       if (bufmgr_sprd->fd < 0) {
+                               TBM_ERR("fail to open drm!\n");
+                               if (error)
+                                       *error = TBM_ERROR_INVALID_OPERATION;
+                               goto fail_open_drm;
+                       }
+               }
+
+               tbm_drm_helper_set_tbm_master_fd(bufmgr_sprd->fd);
+
+               bufmgr_sprd->device_name = drmGetDeviceNameFromFd(bufmgr_sprd->fd);
+               if (!bufmgr_sprd->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;
+               }
+       } else {
+               if (!tbm_drm_helper_get_auth_info(&(bufmgr_sprd->fd), &(bufmgr_sprd->device_name), NULL)) {
+                       TBM_ERR("fail to get auth drm info!\n");
+                       if (error)
+                               *error = TBM_ERROR_INVALID_OPERATION;
+                       goto fail_get_auth_info;
                }
+
        }
-#endif
+       tbm_drm_helper_set_fd(bufmgr_sprd->fd);
 
-       DBG ("[libtbm-sprd:%d] %s DMABUF FENCE is %s\n", getpid(),
-            __FUNCTION__, bufmgr_sprd->use_dma_fence ? "supported!" : "NOT supported!");
+       //Check if the tbm manager supports dma fence or not.
+       fp = open("/sys/module/dmabuf_sync/parameters/enabled", O_RDONLY);
+       if (fp != -1) {
+               char buf[1];
+               int length = read(fp, buf, 1);
 
-       DBG ("[libtbm-sprd:%d] %s fd:%d\n", getpid(),
-            __FUNCTION__, bufmgr_sprd->fd);
+               if (length == 1 && buf[0] == '1')
+                       bufmgr_sprd->use_dma_fence = 1;
 
-       return 1;
-}
+               close(fp);
+       }
 
+       if (!_bufmgr_init_cache_state(bufmgr_sprd)) {
+               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_sprd->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_sprd_bufmgr_get_capabilities;
+       bufmgr_func->bufmgr_bind_native_display = tbm_sprd_bufmgr_bind_native_display;
+       bufmgr_func->bufmgr_get_supported_formats = tbm_sprd_bufmgr_get_supported_formats;
+       bufmgr_func->bufmgr_get_plane_data = tbm_sprd_bufmgr_get_plane_data;
+       bufmgr_func->bufmgr_alloc_bo = tbm_sprd_bufmgr_alloc_bo;
+       bufmgr_func->bufmgr_alloc_bo_with_format = NULL;
+       bufmgr_func->bufmgr_import_fd = tbm_sprd_bufmgr_import_fd;
+       bufmgr_func->bufmgr_import_key = tbm_sprd_bufmgr_import_key;
+
+       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_sprd->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_sprd_bo_free;
+       bo_func->bo_get_size = tbm_sprd_bo_get_size;
+       bo_func->bo_get_memory_types = tbm_sprd_bo_get_memory_type;
+       bo_func->bo_get_handle = tbm_sprd_bo_get_handle;
+       bo_func->bo_map = tbm_sprd_bo_map;
+       bo_func->bo_unmap = tbm_sprd_bo_unmap;
+       bo_func->bo_lock = NULL;
+       bo_func->bo_unlock = NULL;
+       bo_func->bo_export_fd = tbm_sprd_bo_export_fd;
+       bo_func->bo_export_key = tbm_sprd_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_sprd->bo_func = bo_func;
+
+       TBM_DBG("DMABUF FENCE is %s\n",
+               bufmgr_sprd->use_dma_fence ? "supported!" : "NOT supported!");
+       TBM_DBG("fd:%d\n", bufmgr_sprd->fd);
+
+       if (error)
+               *error = TBM_ERROR_NONE;
+
+       bufmgr_sprd->bufmgr = bufmgr;
+
+       return (tbm_backend_bufmgr_data *)bufmgr_sprd;
+
+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_sprd);
+       if (bufmgr_sprd->hashBos)
+               drmHashDestroy(bufmgr_sprd->hashBos);
+fail_init_cache_state:
+       if (tbm_backend_bufmgr_query_display_server(bufmgr, &err))
+               tbm_drm_helper_unset_tbm_master_fd();
+       tbm_drm_helper_unset_fd();
+       if (bufmgr_sprd->device_name)
+               free(bufmgr_sprd->device_name);
+fail_get_device_name:
+       close(bufmgr_sprd->fd);
+fail_get_auth_info:
+fail_open_drm:
+       free(bufmgr_sprd);
+       return NULL;
+}
 
+tbm_backend_module tbm_backend_module_data = {
+       "sprd",
+       "Samsung",
+       TBM_BACKEND_ABI_VERSION_3_0,
+       tbm_sprd_init,
+       tbm_sprd_deinit
+};