Revert "Revert "change the slp_global_lock to the tgl""
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
old mode 100755 (executable)
new mode 100644 (file)
index 63b0eb6..1db8ac5
@@ -59,9 +59,10 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tbm_bufmgr_tgl.h"
 
 //#define USE_CONTIG_ONLY
+//#define USE_CACHE
 #define USE_DMAIMPORT
 
-#define TBM_COLOR_FORMAT_COUNT 8
+#define TBM_COLOR_FORMAT_COUNT 4
 
 #ifdef DEBUG
 #define LOG_TAG    "TBM_BACKEND"
@@ -70,49 +71,45 @@ 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_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)
@@ -121,16 +118,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_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;\
-    }\
+       if (!(cond)) {\
+               TBM_SPRD_LOG("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
+               return val;\
+       } \
 }
 
 struct dma_buf_info {
@@ -178,9 +175,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 +227,6 @@ struct _tbm_bufmgr_sprd {
        int tgl_fd;
 
        void *bind_display;
-
        char *device_name;
 };
 
@@ -250,29 +246,46 @@ 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
+                                                                       };
+
+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_LOG("[libtbm-sprd:%d] error(%s) %s:%d\n",
+                               getpid(), strerror(errno), __func__, __LINE__);
+               return 0;
+       }
+
+       TBM_SPRD_LOG("[libtbm-sprd:%d] %s:%d tgl version is (%u, %u).\n",
+                               getpid(), __func__, __LINE__, 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);
+                               getpid(), strerror(errno), __func__, __LINE__, key);
                return 0;
        }
 
@@ -282,13 +295,14 @@ _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_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d\n",
+                               getpid(), strerror(errno), __func__, __LINE__, key);
                return 0;
        }
 
@@ -296,15 +310,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_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d opt:%d\n",
+                       getpid(), strerror(errno), __func__, __LINE__, key, opt);
                return 0;
        }
 
@@ -314,32 +344,36 @@ _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_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d\n",
+                       getpid(), strerror(errno), __func__, __LINE__, key);
                return 0;
        }
 
        return 1;
 }
 
+#ifdef USE_CACHE
 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_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d\n",
+                       getpid(), strerror(errno), __func__, __LINE__, key);
                return 0;
        }
 
@@ -349,134 +383,124 @@ _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_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d\n",
+                       getpid(), strerror(errno), __func__, __LINE__, 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_LOG("[libtbm-sprd:%d] warning %s:%d fail to open drm, "
+                       "so 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)) {
+               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;
+                               DBG("[%s] Found render device: '%s' (%s)\n",
+                                   _target_name(),
+                                   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_LOG("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_LOG("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
+               return -1;
+       }
 
-               udev_device_unref(drm_device);
-               udev_unref(udev);
+       if (fstat(fd, &s)) {
+               TBM_SPRD_LOG("fstat() failed %s.\n");
+               close(fd);
+               return -1;
        }
+#else
+       TBM_SPRD_LOG("[libtbm-sprd:%d] warning %s:%d fail to open drm\n",
+                     getpid(), __FUNCTION__, __LINE__);
 #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 +533,57 @@ _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,
+       ret = drmCommandWriteRead(bufmgr_sprd->fd, DRM_SPRD_GEM_CACHE_OP, &cache_op,
                                   sizeof(cache_op));
        if (ret) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
                              "error %s:%d fail to flush the cache.\n",
                              getpid(), __FUNCTION__, __LINE__);
                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;
+       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;
+#ifdef USE_CACHE
+       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
+       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,7 +625,7 @@ _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(),
@@ -604,22 +634,22 @@ _bo_set_cache_state(tbm_bo bo, int device, int opt)
                    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 +657,77 @@ _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);
+       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);
 }
 
+static int
+_bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
+{
+       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) {
+               TBM_SPRD_LOG("[libtbm-sprd:%d] error: Fail to open global_lock:%s\n",
+                               getpid(), tgl_devfile);
+               return 0;
+       }
+
+       if (!_tgl_get_version(bufmgr_sprd->tgl_fd)) {
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
+                               "error: fail to get tgl_version. tgl init failed.\n",
+                               getpid());
+
+               close(bufmgr_sprd->tgl_fd);
+               return 0;
+       }
+
+#ifdef USE_CACHE
+       if (!_tgl_init(bufmgr_sprd->tgl_fd, GLOBAL_KEY)) {
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
+                               "error: Fail to initialize the tgl\n",
+                               getpid());
+
+               close(bufmgr_sprd->tgl_fd);
+               return 0;
+       }
+#endif
+
+       return 1;
+}
+
+static void
+_bufmgr_deinit_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
+{
+       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);
+}
+
 #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 +738,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 +752,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,13 +773,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] "
+       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);
                return 0;
@@ -711,10 +789,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,8 +805,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] "
+                       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);
                                return (tbm_bo_handle) NULL;
@@ -743,8 +821,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] "
+                       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);
                                return (tbm_bo_handle) NULL;
@@ -760,7 +838,7 @@ _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(),
+               DBG("[libtbm-sprd:%d] %s In case TBM_DEVICE_MM:  \n", getpid(),
                     __FUNCTION_);
                _
 
@@ -769,8 +847,8 @@ _sprd_bo_handle (tbm_bo_sprd bo_sprd, int device)
                        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] "
+                       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);
                                return (tbm_bo_handle) NULL;
@@ -790,32 +868,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] "
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
                              "error %s:%d fail to allocate the bo private\n",
                              getpid(), __FUNCTION__, __LINE__);
                return 0;
@@ -825,21 +904,20 @@ 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 = size;
+       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] "
+               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);
+               free(bo_sprd);
                return 0;
        }
 
@@ -848,11 +926,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_LOG("error fail init cache state(%d)\n", bo_sprd->name);
+               free(bo_sprd);
                return 0;
        }
 
@@ -863,27 +941,35 @@ 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] "
+               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);
+                       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_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 ("[libtbm-sprd:%d] "
+               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(),
+       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);
@@ -901,25 +987,25 @@ 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",
+       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] "
+                       TBM_SPRD_LOG("[libtbm-sprd:%d] "
                                      "error %s:%d\n",
                                      getpid(), __FUNCTION__, __LINE__);
                }
        }
 
-       /* close dmabuf */
+       /* closedmabuf */
        if (bo_sprd->dmabuf) {
-               close (bo_sprd->dmabuf);
+               close(bo_sprd->dmabuf);
                bo_sprd->dmabuf = 0;
        }
 
@@ -927,40 +1013,40 @@ 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] "
+               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);
        }
 
-       _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] "
+       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg)) {
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
                              "error %s:%d\n",
                              getpid(), __FUNCTION__, __LINE__);
        }
 
-       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 +1054,21 @@ 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] "
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
                              "error %s:%d Cannot open gem name=%d\n",
                              getpid(), __FUNCTION__, __LINE__, key);
-               return 0;
+               return NULL;
        }
 
        info.handle = arg.handle;
@@ -992,18 +1076,18 @@ 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] "
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
                              "error %s:%d Cannot get gem info=%d\n",
                              getpid(), __FUNCTION__, __LINE__, key);
-               return 0;
+               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] "
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
                              "error %s:%d fail to allocate the bo private\n",
                              getpid(), __FUNCTION__, __LINE__);
-               return 0;
+               goto fail_alloc_bo;
        }
 
        bo_sprd->fd = bufmgr_sprd->fd;
@@ -1015,85 +1099,104 @@ 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_LOG("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] "
+               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;
+                       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_LOG("[libtbm-sprd:%d] error %s:%d Fail to alloc\n",
+                             getpid(), __FUNCTION__, __LINE__);
+               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] "
+       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(),
+       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;
+
+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",
+       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);
+       name = _get_name(bufmgr_sprd->fd, gem);
+       if (!name) {
+               TBM_SPRD_LOG("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 +1211,56 @@ 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",
+               TBM_SPRD_LOG("error bo:%p Cannot get gem info from gem:%d, fd:%d (%s)\n",
                              bo, gem, key, strerror(errno));
-               return 0;
+               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_LOG("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",
+               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;
+               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_LOG("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",
+               TBM_SPRD_LOG("[libtbm-sprd:%d] "
+                             "error %s:%d Fail to callocprivGem\n",
                              getpid(), __FUNCTION__, __LINE__);
-               free (bo_sprd);
-               return 0;
+               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",
+       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(),
+       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,
@@ -1171,29 +1269,36 @@ 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] "
+                       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(),
+       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);
@@ -1201,29 +1306,29 @@ tbm_sprd_bo_export (tbm_bo bo)
        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",
+               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(),
+       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,
@@ -1236,30 +1341,30 @@ tbm_sprd_bo_export_fd (tbm_bo bo)
 
 
 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] "
+               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(),
+       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);
+       bo_handle = _sprd_bo_handle(bo_sprd, device);
        if (bo_handle.ptr == NULL) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
+               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;
@@ -1269,37 +1374,41 @@ 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] "
+               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(),
+       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);
+       bo_handle = _sprd_bo_handle(bo_sprd, device);
        if (bo_handle.ptr == NULL) {
-               TBM_SPRD_LOG ("[libtbm-sprd:%d] "
+               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_set_cache_state(bufmgr_sprd, bo_sprd, device, opt);
 
        bo_sprd->map_cnt++;
 
@@ -1307,14 +1416,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,9 +1435,9 @@ 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(),
+       DBG("[libtbm-sprd:%d] %s gem:%d(%d) \n", getpid(),
             __FUNCTION__, bo_sprd->gem, bo_sprd->name);
 
        return 1;
@@ -1333,18 +1446,19 @@ tbm_sprd_bo_unmap (tbm_bo bo)
 static int
 tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
 {
-       SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
+       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);
+       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);
+       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
+
+#if USE_BACKEND_LOCK
+       int ret = 0;
 
        if (bufmgr_sprd->use_dma_fence) {
                struct dma_buf_fence fence;
@@ -1358,7 +1472,7 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
                        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",
+                               DBG("[libtbm-sprd:%d] %s GET_FENCE is ignored(device type is not 3D/CPU),\n",
                                     getpid(), __FUNCTION__);
                                return 0;
                        }
@@ -1368,30 +1482,21 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
                        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",
+                               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(),
+                       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] "
+                       TBM_SPRD_LOG("[libtbm-sprd:%d] "
                                      "error %s:%d  Can not set GET FENCE(%s)\n",
-                                     getpid(), __FUNCTION__, __LINE__, strerror(errno) );
+                                     getpid(), __FUNCTION__, __LINE__, strerror(errno));
                        return 0;
                }
 
@@ -1406,55 +1511,56 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
                }
                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] "
+                       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",
+               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);
+               ret = _tgl_lock(bufmgr_sprd->tgl_fd, bo_sprd->name, opt);
 
-               DBG ("[libtbm-sprd:%d] lock tgl flink_id:%d\n",
+               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);
+       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);
+       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);
+       SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
+
+#if USE_BACKEND_LOCK
+       int ret = 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(),
+                       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(),
+                       DBG("[libtbm-sprd:%d] %s device type is not 3D/CPU,\n", getpid(),
                             __FUNCTION__);
                        return 0;
                }
@@ -1473,31 +1579,32 @@ tbm_sprd_bo_unlock(tbm_bo bo)
 
                ret = ioctl(bo_sprd->dmabuf, DMABUF_IOCTL_PUT_FENCE, &fence);
                if (ret < 0) {
-                       TBM_SPRD_LOG ("[libtbm-sprd:%d] "
+                       TBM_SPRD_LOG("[libtbm-sprd:%d] "
                                      "error %s:%d  Can not set PUT FENCE(%s)\n",
-                                     getpid(), __FUNCTION__, __LINE__, strerror(errno) );
+                                     getpid(), __FUNCTION__, __LINE__, strerror(errno));
                        return 0;
                }
 
-               DBG ("[libtbm-sprd:%d] %s DMABUF_IOCTL_PUT_FENCE! flink_id=%d dmabuf=%d\n",
+               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",
+               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 +1615,52 @@ 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();
+
        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);
+
+       close(bufmgr_sprd->fd);
 
-       free (bufmgr_sprd);
+       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 +1671,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 +1682,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 +1759,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 +1912,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;
 
@@ -1834,7 +1944,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,92 +1955,54 @@ 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_LOG("[libtbm-sprd:%d] error: Fail to alloc bufmgr_sprd!\n", getpid());
                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_LOG("[libtbm-sprd:%d] error: Fail to open drm!\n", getpid());
+                               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());
-                       free (bufmgr_sprd);
-                       return 0;
+               if (!bufmgr_sprd->device_name) {
+                       TBM_SPRD_LOG("[libtbm-sprd:%d] error: Fail to get device name!\n", getpid());
+                       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_LOG("[libtbm-sprd:%d] error: Fail to get auth drm info!\n", getpid());
+                       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;
-       }
-
        //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;
@@ -1938,15 +2010,15 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
                close(fp);
        }
 
+       if (!_bufmgr_init_cache_state(bufmgr_sprd)) {
+               TBM_SPRD_LOG("[libtbm-sprd:%d] error: Fail to init bufmgr cache state\n", getpid());
+               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_LOG("[libtbm-sprd:%d] error: Fail to alloc backend!\n", getpid());
+               goto fail_alloc_backend;
        }
 
        bufmgr_backend->priv = (void *)bufmgr_sprd;
@@ -1964,45 +2036,49 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
        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_lock = 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_LOCK_CTRL_BACKEND;
-       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_LOG("[libtbm-sprd:%d] error: Fail to init backend!\n", getpid());
+               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_LOG("TBM_SPRD_DEBUG=%s\n", env);
+               } else
                        bDebug = 0;
-               }
        }
 #endif
 
-       DBG ("[libtbm-sprd:%d] %s DMABUF FENCE is %s\n", getpid(),
+       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(),
+       DBG("[libtbm-sprd:%d] %s fd:%d\n", getpid(),
             __FUNCTION__, 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();
+fail_get_device_name:
+       close(bufmgr_sprd->fd);
+fail_get_auth_info:
+fail_open_drm:
+       free(bufmgr_sprd);
+       return 0;
+}