added tbm_drm_helper_set/unset_fd
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
old mode 100755 (executable)
new mode 100644 (file)
index 7ce1b42..35535e3
@@ -56,12 +56,15 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <tbm_drm_helper.h>
 
 #define DEBUG
-#include "tbm_bufmgr_tgl.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
+
+#define TBM_COLOR_FORMAT_COUNT 4
 
 #ifdef DEBUG
 #define LOG_TAG    "TBM_BACKEND"
@@ -70,49 +73,46 @@ static int bDebug = 0;
 
 #define SPRD_DRM_NAME "sprd"
 
-char *
-target_name()
+static char *
+_target_name(void)
 {
-       FILE *f;
+       static char app_name[128] = {0, };
+       static int initialized = 0;
        char *slash;
-       static int     initialized = 0;
-       static char app_name[128];
+       FILE *f;
 
-       if ( initialized )
+       if (initialized)
                return app_name;
 
        /* get the application name */
        f = fopen("/proc/self/cmdline", "r");
+       if (!f)
+               return NULL;
 
-       if ( !f ) {
-               return 0;
-       }
-
-       memset(app_name, 0x00, sizeof(app_name));
-
-       if ( fgets(app_name, 100, f) == NULL ) {
+       if (fgets(app_name, 100, f) == NULL) {
                fclose(f);
-               return 0;
+               return NULL;
        }
 
        fclose(f);
 
-       if ( (slash = strrchr(app_name, '/')) != NULL ) {
+       slash = strrchr(app_name, '/');
+       if (slash != 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)
+
+#define TBM_SPRD_ERROR(fmt, args...)   LOGE("\033[31m"  "[%s] " fmt "\033[0m", _target_name(), ##args)
+#define TBM_SPRD_DEBUG(fmt, args...)   if (bDebug&01) LOGD("[%s] " fmt, _target_name(), ##args)
 #else
-#define TBM_SPRD_LOG(...)
-#define DBG(...)
+#define TBM_SPRD_ERROR(...)
+#define TBM_SPRD_DEBUG(...)
 #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)
@@ -121,16 +121,16 @@ target_name()
 
 /* check condition */
 #define SPRD_RETURN_IF_FAIL(cond) {\
-    if (!(cond)) {\
-        TBM_SPRD_LOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
-        return;\
-    }\
+       if (!(cond)) {\
+               TBM_SPRD_ERROR("[%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;\
-    }\
+       if (!(cond)) {\
+               TBM_SPRD_ERROR("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
+               return val;\
+       } \
 }
 
 struct dma_buf_info {
@@ -178,9 +178,9 @@ 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;
 };
 
@@ -230,7 +230,6 @@ struct _tbm_bufmgr_sprd {
        int tgl_fd;
 
        void *bind_display;
-
        char *device_name;
 };
 
@@ -250,29 +249,43 @@ 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;
+
+       err = ioctl(fd, TGL_IOCTL_GET_VERSION, &data);
+       if (err) {
+               TBM_SPRD_ERROR("error(%s) %s:%d\n",     strerror(errno));
+               return 0;
+       }
+
+       TBM_SPRD_DEBUG("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;
 
-       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_SPRD_ERROR("error(%s) key:%d\n", strerror(errno), key);
                return 0;
        }
 
@@ -282,13 +295,13 @@ _tgl_init(int fd, unsigned int key)
 static inline int
 _tgl_destroy(int fd, unsigned int key)
 {
+       struct tgl_reg_data data;
        int err;
 
-       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_SPRD_ERROR("error(%s) key:%d\n", strerror(errno), key);
                return 0;
        }
 
@@ -296,15 +309,31 @@ _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;
+       enum tgl_type_data tgl_type;
        int err;
 
-       err = ioctl(fd, TGL_IOC_LOCK_LOCK, key);
+       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_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_SPRD_ERROR("error(%s) key:%d opt:%d\n",
+                       strerror(errno), key, opt);
                return 0;
        }
 
@@ -314,13 +343,16 @@ _tgl_lock(int fd, unsigned int key)
 static inline int
 _tgl_unlock(int fd, unsigned int key)
 {
+       struct tgl_lock_data data;
        int err;
 
-       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_SPRD_ERROR("error(%s) key:%d\n",
+                       strerror(errno), key);
                return 0;
        }
 
@@ -330,16 +362,16 @@ _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;
 
-       arg.key = key;
-       arg.data1 = val;
-       err = ioctl(fd, TGL_IOC_SET_DATA, &arg);
+       data.key = key;
+       data.data1 = val;
+
+       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_SPRD_ERROR("error(%s) key:%d\n",
+                       strerror(errno), key);
                return 0;
        }
 
@@ -349,134 +381,120 @@ _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, };
 
-       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_SPRD_ERROR("error(%s) key:%d\n",
+                       strerror(errno), 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_SPRD_DEBUG("warning fail to open drm. search drm-device by udev\n");
+
+       udev = udev_new();
+       if (!udev) {
+               TBM_SPRD_ERROR("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_SPRD_DEBUG("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_SPRD_ERROR("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_SPRD_ERROR("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
+               return -1;
+       }
 
-               udev_device_unref(drm_device);
-               udev_unref(udev);
+       if (fstat(fd, &s)) {
+               TBM_SPRD_ERROR("fstat() failed %s.\n");
+               close(fd);
+               return -1;
        }
+#else
+       TBM_SPRD_ERROR("warning fail to open drm\n");
 #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);
+       SPRD_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,51 +527,56 @@ _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_SPRD_ERROR("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
+       SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
+       SPRD_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;
 
-       _tgl_set_data(bufmgr_sprd->tgl_fd, bo_sprd->name, cache_state.val);
+       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);
+       }
+#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
+       SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
+       SPRD_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;
@@ -595,31 +618,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_SPRD_DEBUG("\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
+       SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
+       SPRD_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 +649,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
+       SPRD_RETURN_IF_FAIL(bo_sprd != NULL);
+       SPRD_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
+       SPRD_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_SPRD_ERROR("fail to open global_lock:%s\n",
+                                       tgl_devfile1);
+                   return 0;
+           }
+       }
+
+       if (!_tgl_get_version(bufmgr_sprd->tgl_fd)) {
+               TBM_SPRD_ERROR("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_SPRD_ERROR("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
+       SPRD_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 +732,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 +746,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 +767,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_SPRD_ERROR("error fail to get flink gem=%d\n", gem);
                return 0;
        }
 
@@ -711,10 +781,10 @@ _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 +797,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_SPRD_ERROR("error Cannot usrptr gem=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->pBase = (void *)((uint32_t)arg.mapped);
@@ -743,10 +811,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_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->dmabuf = arg.fd;
@@ -760,19 +826,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_SPRD_DEBUG("%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_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->dmabuf = arg.fd;
@@ -790,34 +851,33 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
 }
 
 static int
-tbm_sprd_bo_size (tbm_bo bo)
+tbm_sprd_bo_size(tbm_bo bo)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       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);
 
        return bo_sprd->size;
 }
 
 static void *
-tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
+tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0);
 
-       tbm_bo_sprd bo_sprd;
        tbm_bufmgr_sprd bufmgr_sprd;
        unsigned int sprd_flags;
+       tbm_bo_sprd bo_sprd;
 
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
 
-       bo_sprd = calloc (1, sizeof(struct _tbm_bo_sprd));
+       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__);
+               TBM_SPRD_ERROR("error fail to allocate the bo private\n");
                return 0;
        }
 
@@ -825,21 +885,19 @@ tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
        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 = _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);
+               TBM_SPRD_ERROR("error Cannot create bo(flag:%x, size:%d)\n",
+                       arg.flags, (unsigned int)arg.size);
+               free(bo_sprd);
                return 0;
        }
 
@@ -848,11 +906,11 @@ tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
        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);
+       bo_sprd->name = _get_name(bo_sprd->fd, bo_sprd->gem);
 
-       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);
+       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 0)) {
+               TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name);
+               free(bo_sprd);
                return 0;
        }
 
@@ -863,30 +921,31 @@ tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
                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);
+               if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
+                       free(bo_sprd);
                        return 0;
                }
                bo_sprd->dmabuf = arg.fd;
        }
 
        /* add bo to hash */
-       PrivGem *privGem = calloc (1, sizeof(PrivGem));
+       PrivGem *privGem = calloc(1, sizeof(PrivGem));
+       if (!privGem) {
+               TBM_SPRD_ERROR("error Fail to calloc PrivGem\n");
+               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 ("[libtbm-sprd:%d] "
-                             "error %s:%d Cannot insert bo to Hash(%d)\n",
-                             getpid(), __FUNCTION__, __LINE__, bo_sprd->name);
-       }
+       if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0)
+               TBM_SPRD_ERROR("error Cannot insert bo to Hash(%d)\n", 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,
-            flags, sprd_flags);
+       TBM_SPRD_DEBUG("%s size:%d, gem:%d(%d), flags:%d(%d)\n",
+               __FUNCTION__, bo_sprd->size,
+               bo_sprd->gem, bo_sprd->name,
+               flags, sprd_flags);
 
        return (void *)bo_sprd;
 }
@@ -901,25 +960,22 @@ tbm_sprd_bo_free(tbm_bo bo)
                return;
 
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_IF_FAIL (bufmgr_sprd != NULL);
+       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);
+       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);
+       TBM_SPRD_DEBUG("size:%d, gem:%d(%d)\n",
+               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__);
-               }
+               if (munmap(bo_sprd->pBase, bo_sprd->size) == -1)
+                       TBM_SPRD_ERROR("error fail to munmap.\n");
        }
 
-       /* close dmabuf */
+       /* closedmabuf */
        if (bo_sprd->dmabuf) {
-               close (bo_sprd->dmabuf);
+               close(bo_sprd->dmabuf);
                bo_sprd->dmabuf = 0;
        }
 
@@ -927,40 +983,35 @@ tbm_sprd_bo_free(tbm_bo bo)
        PrivGem *privGem = NULL;
        int ret;
 
-       ret = drmHashLookup (bufmgr_sprd->hashBos, bo_sprd->name, (void **)&privGem);
+       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);
+                       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);
+               TBM_SPRD_DEBUG("warning Cannot find bo to Hash(%d), ret=%d\n", bo_sprd->name, ret);
        }
 
-       _bo_destroy_cache_state(bo);
+       _bo_destroy_cache_state(bufmgr_sprd, bo_sprd);
 
        /* Free gem handle */
        struct drm_gem_close arg = {0, };
-       memset (&arg, 0, sizeof(arg));
+       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__);
-       }
+       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg))
+               TBM_SPRD_ERROR("error fail to DRM_IOCTL_GEM_CLOSE\n");
 
-       free (bo_sprd);
+       free(bo_sprd);
 }
 
 
 static void *
-tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
+tbm_sprd_bo_import(tbm_bo bo, unsigned int key)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bo != NULL, NULL);
 
        tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
@@ -968,23 +1019,19 @@ tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
        int ret;
 
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, NULL);
 
-       ret = drmHashLookup (bufmgr_sprd->hashBos, key, (void **)&privGem);
-       if (ret == 0) {
-               privGem->ref_count++;
+       ret = drmHashLookup(bufmgr_sprd->hashBos, key, (void **)&privGem);
+       if (ret == 0)
                return privGem->bo_priv;
-       }
 
-       struct drm_gem_open arg = {0, };
        struct drm_sprd_gem_info info = {0, };
+       struct drm_gem_open arg = {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;
+               TBM_SPRD_ERROR("error Cannot open gem name=%d\n", key);
+               return NULL;
        }
 
        info.handle = arg.handle;
@@ -992,18 +1039,14 @@ tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
                                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;
+               TBM_SPRD_ERROR("error Cannot get gem info=%d\n", key);
+               goto fail_get_gem;
        }
 
-       bo_sprd = calloc (1, sizeof(struct _tbm_bo_sprd));
+       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;
+               TBM_SPRD_ERROR("error fail to allocate the bo private\n");
+               goto fail_alloc_bo;
        }
 
        bo_sprd->fd = bufmgr_sprd->fd;
@@ -1015,85 +1058,97 @@ tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
        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);
+       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_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
+               TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name);
+               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_SPRD_LOG ("[libtbm-sprd:%d] "
-                                     "error %s:%d Cannot dmabuf=%d\n",
-                                     getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
-                       free (bo_sprd);
-                       return 0;
+               if (drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
+                       TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
+                       goto fail_prime_handle_to_fd;
                }
                bo_sprd->dmabuf = arg.fd;
        }
 
        /* add bo to hash */
-       privGem = calloc (1, sizeof(PrivGem));
+       privGem = calloc(1, sizeof(PrivGem));
+       if (!privGem) {
+               TBM_SPRD_ERROR("error Fail to alloc\n");
+               goto fail_alloc_gem_priv;
+       }
+
        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 (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0)
+               TBM_SPRD_ERROR("error Cannot insert bo to Hash(%d)\n", 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);
+       TBM_SPRD_DEBUG("size:%d, gem:%d(%d), flags:%d(%d)\n",
+               bo_sprd->size, bo_sprd->gem, bo_sprd->name,
+               bo_sprd->flags_tbm, bo_sprd->flags_sprd);
 
        return (void *)bo_sprd;
+
+fail_alloc_gem_priv:
+       if (bo_sprd->dmabuf)
+               close(bo_sprd->dmabuf);
+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;
 }
 
 static void *
-tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
+tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bo != NULL, NULL);
 
        tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
-       PrivGem *privGem = NULL;
-       int ret;
-       int name;
+       unsigned int name;
+       PrivGem *privGem;
 
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, NULL);
 
        //getting handle from fd
-       unsigned int gem = 0;
        struct drm_prime_handle arg = {0, };
+       unsigned int gem;
 
        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));
+       if (drmIoctl(bufmgr_sprd->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) {
+               TBM_SPRD_ERROR("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);
+       name = _get_name(bufmgr_sprd->fd, gem);
+       if (!name) {
+               TBM_SPRD_ERROR("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n",
+                       bo, gem, key, strerror(errno));
+               return NULL;
+       }
 
-       ret = drmHashLookup (bufmgr_sprd->hashBos, name, (void **)&privGem);
-       if (ret == 0) {
-               if (gem == privGem->bo_priv->gem) {
-                       privGem->ref_count++;
+       if (!drmHashLookup(bufmgr_sprd->hashBos, name, (void **)&privGem)) {
+               if (gem == privGem->bo_priv->gem)
                        return privGem->bo_priv;
-               }
        }
 
-       unsigned int real_size = -1;
+       unsigned int real_size;
        struct drm_sprd_gem_info info = {0, };
 
        /* Determine size of bo.  The fd-to-handle ioctl really should
@@ -1108,61 +1163,53 @@ tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
                                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;
+               TBM_SPRD_ERROR("error bo:%p Cannot get gem info from gem:%d, fd:%d (%s)\n",
+                       bo, gem, key, strerror(errno));
+               return NULL;
        }
 
        if (real_size == -1)
                real_size = info.size;
 
-       bo_sprd = calloc (1, sizeof(struct _tbm_bo_sprd));
+       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;
+               TBM_SPRD_ERROR("error bo:%p fail to allocate the bo private\n", bo);
+               return NULL;
        }
 
        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->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;
+               TBM_SPRD_ERROR("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n",
+                       bo, gem, key, strerror(errno));
+               goto fail_check_name;
        }
 
-       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_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
+               TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name);
+               goto fail_init_cache;
        }
 
        /* add bo to hash */
-       privGem = NULL;
-
-       privGem = calloc (1, sizeof(PrivGem));
+       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;
+               TBM_SPRD_ERROR("error Fail to callocprivGem\n");
+               goto fail_alloc_gem_priv;
        }
 
        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);
+       if (drmHashInsert(bufmgr_sprd->hashBos, bo_sprd->name, (void *)privGem) < 0) {
+               TBM_SPRD_ERROR("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(),
+       TBM_SPRD_DEBUG("bo:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
             bo,
             bo_sprd->gem, bo_sprd->name,
             bo_sprd->dmabuf,
@@ -1171,97 +1218,97 @@ tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
             bo_sprd->size);
 
        return (void *)bo_sprd;
+
+fail_alloc_gem_priv:
+       _bo_destroy_cache_state(bufmgr_sprd, bo_sprd);
+fail_init_cache:
+fail_check_name:
+       free(bo_sprd);
+       return NULL;
 }
 
 static unsigned int
-tbm_sprd_bo_export (tbm_bo bo)
+tbm_sprd_bo_export(tbm_bo bo)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       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);
+       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__);
+                       TBM_SPRD_ERROR("error Cannot get name\n");
                        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);
+       TBM_SPRD_DEBUG("size:%d, gem:%d(%d), flags:%d(%d)\n",
+               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)
+static tbm_fd
+tbm_sprd_bo_export_fd(tbm_bo bo)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, -1);
+       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);
+       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);
+       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));
+               TBM_SPRD_ERROR("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);
+       TBM_SPRD_DEBUG("bo:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d(%d), size:%d\n",
+               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)
+tbm_sprd_bo_get_handle(tbm_bo bo, int device)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, (tbm_bo_handle) NULL);
+       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);
+       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);
+               TBM_SPRD_ERROR("error Cannot map gem=%d\n", 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]);
+       TBM_SPRD_DEBUG("gem:%d(%d), %s\n",
+               bo_sprd->gem, bo_sprd->name, STR_DEVICE[device]);
 
        /*Get mapped bo_handle*/
-       bo_handle = _sprd_bo_handle (bo_sprd, device);
+       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);
+               TBM_SPRD_ERROR("error Cannot get handle: gem:%d, device:%d\n",
+                       bo_sprd->gem, device);
                return (tbm_bo_handle) NULL;
        }
 
@@ -1269,37 +1316,38 @@ tbm_sprd_bo_get_handle (tbm_bo bo, int device)
 }
 
 static tbm_bo_handle
-tbm_sprd_bo_map (tbm_bo bo, int device, int opt)
+tbm_sprd_bo_map(tbm_bo bo, int device, int opt)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, (tbm_bo_handle) NULL);
+       SPRD_RETURN_VAL_IF_FAIL(bo != NULL, (tbm_bo_handle) NULL);
 
        tbm_bo_handle bo_handle;
        tbm_bo_sprd bo_sprd;
+       tbm_bufmgr_sprd bufmgr_sprd;
+
+       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
+       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, (tbm_bo_handle) NULL);
 
        bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
-       SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, (tbm_bo_handle) NULL);
+       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);
+               TBM_SPRD_ERROR("error Cannot map gem=%d\n", 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]);
+       TBM_SPRD_DEBUG("%s gem:%d(%d), %s, %s\n",
+               __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);
+       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);
+               TBM_SPRD_ERROR("error Cannot get handle: gem:%d, device:%d, opt:%d\n",
+                       bo_sprd->gem, device, opt);
                return (tbm_bo_handle) NULL;
        }
 
        if (bo_sprd->map_cnt == 0)
-               _bo_set_cache_state (bo, device, opt);
+               _bo_set_cache_state(bufmgr_sprd, bo_sprd, device, opt);
 
        bo_sprd->map_cnt++;
 
@@ -1307,14 +1355,18 @@ tbm_sprd_bo_map (tbm_bo bo, int device, int opt)
 }
 
 static int
-tbm_sprd_bo_unmap (tbm_bo bo)
+tbm_sprd_bo_unmap(tbm_bo bo)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0);
 
+       tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
 
+       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
+       SPRD_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);
+       SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
 
        if (!bo_sprd->gem)
                return 0;
@@ -1322,182 +1374,17 @@ tbm_sprd_bo_unmap (tbm_bo bo)
        bo_sprd->map_cnt--;
 
        if (bo_sprd->map_cnt == 0)
-               _bo_save_cache_state (bo);
+               _bo_save_cache_state(bufmgr_sprd, bo_sprd);
 
-       DBG ("[libtbm-sprd:%d] %s gem:%d(%d) \n", getpid(),
-            __FUNCTION__, bo_sprd->gem, bo_sprd->name);
+       TBM_SPRD_DEBUG("gem:%d(%d) \n", 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)
+tbm_sprd_bufmgr_deinit(void *priv)
 {
-       SPRD_RETURN_IF_FAIL (priv != NULL);
+       SPRD_RETURN_IF_FAIL(priv != NULL);
 
        tbm_bufmgr_sprd bufmgr_sprd;
 
@@ -1508,52 +1395,54 @@ tbm_sprd_bufmgr_deinit (void *priv)
                void *value;
 
                while (drmHashFirst(bufmgr_sprd->hashBos, &key, &value) > 0) {
-                       free (value);
-                       drmHashDelete (bufmgr_sprd->hashBos, key);
+                       free(value);
+                       drmHashDelete(bufmgr_sprd->hashBos, key);
                }
 
-               drmHashDestroy (bufmgr_sprd->hashBos);
+               drmHashDestroy(bufmgr_sprd->hashBos);
                bufmgr_sprd->hashBos = NULL;
        }
 
        if (bufmgr_sprd->bind_display)
                tbm_drm_helper_wl_auth_server_deinit();
 
+       if (tbm_backend_is_display_server())
+               tbm_drm_helper_unset_tbm_master_fd();
+       else
+               tbm_drm_helper_unset_fd();
+
        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);
+       close(bufmgr_sprd->fd);
+
+       free(bufmgr_sprd);
 }
 
-int
+static int
 tbm_sprd_surface_supported_format(uint32_t **formats, uint32_t *num)
 {
-       uint32_t *color_formats = NULL;
+       uint32_t *color_formats;
 
-       color_formats = (uint32_t *)calloc (1,
+       color_formats = (uint32_t *)calloc(1,
                                            sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT);
-
-       if ( color_formats == NULL ) {
+       if (color_formats == NULL)
                return 0;
-       }
-       memcpy( color_formats, tbm_sprd_color_format_list ,
-               sizeof(uint32_t)*TBM_COLOR_FORMAT_COUNT );
 
+       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
@@ -1564,8 +1453,8 @@ tbm_sprd_surface_supported_format(uint32_t **formats, uint32_t *num)
  * @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,
+static int
+tbm_sprd_surface_get_plane_data(int width, int height,
                                tbm_format format, int plane_idx, uint32_t *size, uint32_t *offset,
                                uint32_t *pitch, int *bo_idx)
 {
@@ -1575,6 +1464,7 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
        int _pitch = 0;
        int _size = 0;
        int _bo_idx = 0;
+       int _align_height = 0;
 
        switch (format) {
                /* 16 bpp RGB */
@@ -1651,13 +1541,15 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
                bpp = 12;
                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 ) {
+               } 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);
+                       _pitch = SIZE_ALIGN(width , TBM_SURFACE_ALIGNMENT_PITCH_YUV / 2);
+                       _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;
@@ -1802,21 +1694,21 @@ tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
        return ret;
 }
 
-int
-tbm_sprd_bo_get_flags (tbm_bo bo)
+static int
+tbm_sprd_bo_get_flags(tbm_bo bo)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       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);
+       SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
 
        return bo_sprd->flags_tbm;
 }
 
-int
-tbm_sprd_bufmgr_bind_native_display (tbm_bufmgr bufmgr, void *NativeDisplay)
+static int
+tbm_sprd_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay)
 {
        tbm_bufmgr_sprd bufmgr_sprd;
 
@@ -1825,7 +1717,7 @@ tbm_sprd_bufmgr_bind_native_display (tbm_bufmgr bufmgr, void *NativeDisplay)
 
        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");
+               TBM_SPRD_ERROR("fail to tbm_drm_helper_wl_server_init\n");
                return 0;
        }
 
@@ -1834,7 +1726,7 @@ tbm_sprd_bufmgr_bind_native_display (tbm_bufmgr bufmgr, void *NativeDisplay)
        return 1;
 }
 
-MODULEINITPPROTO (init_tbm_bufmgr_priv);
+MODULEINITPPROTO(init_tbm_bufmgr_priv);
 
 static TBMModuleVersionInfo SprdVersRec = {
        "sprd",
@@ -1845,93 +1737,56 @@ static TBMModuleVersionInfo SprdVersRec = {
 TBMModuleData tbmModuleData = { &SprdVersRec, init_tbm_bufmgr_priv};
 
 int
-init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
+init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd)
 {
-       tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bufmgr_backend bufmgr_backend;
+       tbm_bufmgr_sprd bufmgr_sprd;
+       int fp;
 
        if (!bufmgr)
                return 0;
 
-       bufmgr_sprd = calloc (1, sizeof(struct _tbm_bufmgr_sprd));
+       bufmgr_sprd = calloc(1, sizeof(struct _tbm_bufmgr_sprd));
        if (!bufmgr_sprd) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to alloc bufmgr_sprd!\n", getpid());
+               TBM_SPRD_ERROR("fail to alloc bufmgr_sprd!\n");
                return 0;
        }
 
        if (tbm_backend_is_display_server()) {
-               int master_fd = -1;
-
-               bufmgr_sprd->fd = -1;
-               master_fd = tbm_drm_helper_get_master_fd();
-               if (master_fd < 0) {
+               bufmgr_sprd->fd = tbm_drm_helper_get_master_fd();
+               if (bufmgr_sprd->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);
+                       if (bufmgr_sprd->fd < 0) {
+                               TBM_SPRD_ERROR("fail to open drm!\n");
+                               goto fail_open_drm;
+                       }
                }
 
-               if (bufmgr_sprd->fd < 0) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
-                       free (bufmgr_sprd);
-                       return 0;
-               }
+               tbm_drm_helper_set_tbm_master_fd(bufmgr_sprd->fd);
 
                bufmgr_sprd->device_name = drmGetDeviceNameFromFd(bufmgr_sprd->fd);
-
-               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 (!bufmgr_sprd->device_name) {
+                       TBM_SPRD_ERROR("fail to get device name!\n");
+                       tbm_drm_helper_unset_tbm_master_fd();
+                       goto fail_get_device_name;
                }
-
        } 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;
+                       TBM_SPRD_ERROR("fail to get auth drm info!\n");
+                       goto fail_get_auth_info;
                }
-       }
-
-       /* 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_SPRD_LOG("[libtbm:%d] "
-                                    "error: Fail to open global_lock:%s\n",
-                                    getpid(), tgl_devfile);
-
-                       close(bufmgr_sprd->fd);
-                       free (bufmgr_sprd);
-                       return 0;
-               }
-       }
-
-       if (!_tgl_init(bufmgr_sprd->tgl_fd, GLOBAL_KEY)) {
-               TBM_SPRD_LOG("[libtbm:%d] "
-                            "error: Fail to initialize the tgl\n",
-                            getpid());
-
-               close(bufmgr_sprd->fd);
-               close(bufmgr_sprd->tgl_fd);
 
-               free (bufmgr_sprd);
-               return 0;
+               tbm_drm_helper_set_fd(bufmgr_sprd->fd);
        }
 
        //Create Hash Table
-       bufmgr_sprd->hashBos = drmHashCreate ();
+       bufmgr_sprd->hashBos = drmHashCreate();
 
        //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];
+       fp = open("/sys/module/dmabuf_sync/parameters/enabled", O_RDONLY);
        if (fp != -1) {
-               length = read(fp, buf, 1);
+               char buf[1];
+               int length = read(fp, buf, 1);
 
                if (length == 1 && buf[0] == '1')
                        bufmgr_sprd->use_dma_fence = 1;
@@ -1939,15 +1794,15 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
                close(fp);
        }
 
+       if (!_bufmgr_init_cache_state(bufmgr_sprd)) {
+               TBM_SPRD_ERROR("fail to init bufmgr cache state\n");
+               goto fail_init_cache_state;
+       }
+
        bufmgr_backend = tbm_backend_alloc();
        if (!bufmgr_backend) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
-
-               close(bufmgr_sprd->fd);
-               close(bufmgr_sprd->tgl_fd);
-
-               free (bufmgr_sprd);
-               return 0;
+               TBM_SPRD_ERROR("fail to alloc backend!\n");
+               goto fail_alloc_backend;
        }
 
        bufmgr_backend->priv = (void *)bufmgr_sprd;
@@ -1966,43 +1821,51 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
        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->bo_unlock = NULL;
        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);
-
-               close(bufmgr_sprd->tgl_fd);
-               close(bufmgr_sprd->fd);
-
-               free (bufmgr_sprd);
-               return 0;
+       if (!tbm_backend_init(bufmgr, bufmgr_backend)) {
+               TBM_SPRD_ERROR("fail to init backend!\n");
+               goto fail_init_backend;
        }
 
 #ifdef DEBUG
        {
                char *env;
-               env = getenv ("TBM_SPRD_DEBUG");
+
+               env = getenv("TBM_SPRD_DEBUG");
                if (env) {
-                       bDebug = atoi (env);
-                       TBM_SPRD_LOG ("TBM_SPRD_DEBUG=%s\n", env);
-               } else {
+                       bDebug = atoi(env);
+                       TBM_SPRD_ERROR("TBM_SPRD_DEBUG=%s\n", env);
+               } else
                        bDebug = 0;
-               }
        }
 #endif
 
-       DBG ("[libtbm-sprd:%d] %s DMABUF FENCE is %s\n", getpid(),
-            __FUNCTION__, bufmgr_sprd->use_dma_fence ? "supported!" : "NOT supported!");
-
-       DBG ("[libtbm-sprd:%d] %s fd:%d\n", getpid(),
-            __FUNCTION__, bufmgr_sprd->fd);
+       TBM_SPRD_DEBUG("DMABUF FENCE is %s\n",
+               bufmgr_sprd->use_dma_fence ? "supported!" : "NOT supported!");
+       TBM_SPRD_DEBUG("fd:%d\n", bufmgr_sprd->fd);
 
        return 1;
-}
 
+fail_init_backend:
+       tbm_backend_free(bufmgr_backend);
+fail_alloc_backend:
+       _bufmgr_deinit_cache_state(bufmgr_sprd);
+fail_init_cache_state:
+       if (bufmgr_sprd->hashBos)
+               drmHashDestroy(bufmgr_sprd->hashBos);
+       if (tbm_backend_is_display_server())
+               tbm_drm_helper_unset_tbm_master_fd();
+       else
+               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 0;
+}