added tbm_drm_helper_set/unset_fd
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
index 1db8ac5..35535e3 100644 (file)
@@ -56,12 +56,14 @@ 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
 
+#ifdef USE_CACHE
+#include "tbm_bufmgr_tgl.h"
+#endif
+
 #define TBM_COLOR_FORMAT_COUNT 4
 
 #ifdef DEBUG
@@ -102,11 +104,12 @@ _target_name(void)
 
        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))
@@ -119,13 +122,13 @@ _target_name(void)
 /* check condition */
 #define SPRD_RETURN_IF_FAIL(cond) {\
        if (!(cond)) {\
-               TBM_SPRD_LOG("[%s] : '%s' failed.\n", __FUNCTION__, #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);\
+               TBM_SPRD_ERROR("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
                return val;\
        } \
 }
@@ -253,6 +256,7 @@ uint32_t tbm_sprd_color_format_list[TBM_COLOR_FORMAT_COUNT] = {
                                                                                TBM_FORMAT_YUV420
                                                                        };
 
+#ifdef USE_CACHE
 static inline int
 _tgl_get_version(int fd)
 {
@@ -261,18 +265,15 @@ _tgl_get_version(int fd)
 
        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__);
+               TBM_SPRD_ERROR("error(%s) %s:%d\n",     strerror(errno));
                return 0;
        }
 
-       TBM_SPRD_LOG("[libtbm-sprd:%d] %s:%d tgl version is (%u, %u).\n",
-                               getpid(), __func__, __LINE__, data.major, data.minor);
+       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)
 {
@@ -284,8 +285,7 @@ _tgl_init(int fd, unsigned int key)
 
        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;
        }
 
@@ -301,8 +301,7 @@ _tgl_destroy(int fd, unsigned int 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;
        }
 
@@ -331,10 +330,10 @@ _tgl_lock(int fd, unsigned int key, int opt)
        data.key = key;
        data.type = tgl_type;
 
-       err = ioctl(fd, TGL_IOCTL_LOCK, data);
+       err = ioctl(fd, TGL_IOCTL_LOCK, &data);
        if (err) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] error(%s) %s:%d key:%d opt:%d\n",
-                       getpid(), strerror(errno), __func__, __LINE__, key, opt);
+               TBM_SPRD_ERROR("error(%s) key:%d opt:%d\n",
+                       strerror(errno), key, opt);
                return 0;
        }
 
@@ -350,17 +349,16 @@ _tgl_unlock(int fd, unsigned int key)
        data.key = key;
        data.type = TGL_TYPE_NONE;
 
-       err = ioctl(fd, TGL_IOCTL_UNLOCK, data);
+       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;
        }
 
        return 1;
 }
 
-#ifdef USE_CACHE
 static inline int
 _tgl_set_data(int fd, unsigned int key, unsigned int val)
 {
@@ -372,8 +370,8 @@ _tgl_set_data(int fd, unsigned int key, unsigned int 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;
        }
 
@@ -390,8 +388,8 @@ _tgl_get_data(int fd, unsigned int key, unsigned int *locked)
 
        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;
        }
 
@@ -420,13 +418,11 @@ _tbm_sprd_open_drm(void)
                return fd;
 
 #ifdef HAVE_UDEV
-       TBM_SPRD_LOG("[libtbm-sprd:%d] warning %s:%d fail to open drm, "
-                       "so search drm-device by udev\n",
-                       getpid(), __FUNCTION__, __LINE__);
+       TBM_SPRD_DEBUG("warning fail to open drm. search drm-device by udev\n");
 
        udev = udev_new();
        if (!udev) {
-               TBM_SPRD_LOG("udev_new() failed.\n");
+               TBM_SPRD_ERROR("udev_new() failed.\n");
                return -1;
        }
 
@@ -445,8 +441,7 @@ _tbm_sprd_open_drm(void)
                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(),
+                               TBM_SPRD_DEBUG("Found render device: '%s' (%s)\n",
                                    udev_device_get_syspath(drm_device),
                                    udev_device_get_sysname(device_parent));
                                break;
@@ -460,7 +455,7 @@ _tbm_sprd_open_drm(void)
        /* Get device file path. */
        filepath = udev_device_get_devnode(drm_device);
        if (!filepath) {
-               TBM_SPRD_LOG("udev_device_get_devnode() failed.\n");
+               TBM_SPRD_ERROR("udev_device_get_devnode() failed.\n");
                udev_device_unref(drm_device);
                udev_unref(udev);
                return -1;
@@ -472,18 +467,17 @@ _tbm_sprd_open_drm(void)
        /* 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");
+               TBM_SPRD_ERROR("open(%s, O_RDWR | O_CLOEXEC) failed.\n");
                return -1;
        }
 
        if (fstat(fd, &s)) {
-               TBM_SPRD_LOG("fstat() failed %s.\n");
+               TBM_SPRD_ERROR("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__);
+       TBM_SPRD_ERROR("warning fail to open drm\n");
 #endif
 
        return fd;
@@ -534,14 +528,13 @@ _sprd_bo_cache_flush(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int flags
                cache_op.flags |= SPRD_DRM_ALL_CACHES_CORES;
 
        ret = drmCommandWriteRead(bufmgr_sprd->fd, DRM_SPRD_GEM_CACHE_OP, &cache_op,
-                                  sizeof(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
@@ -549,6 +542,7 @@ _sprd_bo_cache_flush(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int flags
 static int
 _bo_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int import)
 {
+#ifdef USE_CACHE
        SPRD_RETURN_VAL_IF_FAIL(bo_sprd != NULL, 0);
        SPRD_RETURN_VAL_IF_FAIL(bufmgr_sprd != NULL, 0);
 
@@ -557,7 +551,6 @@ _bo_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int impor
 
        _tgl_init(bufmgr_sprd->tgl_fd, bo_sprd->name);
 
-#ifdef USE_CACHE
        tbm_bo_cache_state cache_state;
 
        if (import == 0) {
@@ -627,8 +620,7 @@ _bo_set_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int device
                /* call cache 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,
@@ -665,6 +657,7 @@ _bo_save_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
 static void
 _bo_destroy_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
 {
+#ifdef USE_CACHE
        SPRD_RETURN_IF_FAIL(bo_sprd != NULL);
        SPRD_RETURN_IF_FAIL(bufmgr_sprd != NULL);
 
@@ -672,11 +665,13 @@ _bo_destroy_cache_state(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd)
                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)
@@ -684,27 +679,24 @@ _bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 
        /* 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;
+           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_LOG("[libtbm-sprd:%d] "
-                               "error: fail to get tgl_version. tgl init failed.\n",
-                               getpid());
-
+               TBM_SPRD_ERROR("fail to get tgl_version. tgl init failed.\n");
                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());
-
+               TBM_SPRD_ERROR("fail to initialize the tgl\n");
                close(bufmgr_sprd->tgl_fd);
                return 0;
        }
@@ -716,6 +708,7 @@ _bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 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)
@@ -723,6 +716,7 @@ _bufmgr_deinit_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
 
        if (bufmgr_sprd->tgl_fd >= 0)
                close(bufmgr_sprd->tgl_fd);
+#endif
 }
 
 #ifndef USE_CONTIG_ONLY
@@ -779,9 +773,7 @@ _get_name(int fd, unsigned int gem)
 
        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);
+               TBM_SPRD_ERROR("error fail to get flink gem=%d\n", gem);
                return 0;
        }
 
@@ -806,9 +798,7 @@ _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);
+                               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);
@@ -822,9 +812,7 @@ _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] "
-                                             "error %s:%d Cannot dmabuf=%d\n",
-                                             getpid(), __FUNCTION__, __LINE__, bo_sprd->gem);
+                               TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->dmabuf = arg.fd;
@@ -838,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);
+                               TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                                return (tbm_bo_handle) NULL;
                        }
                        bo_sprd->dmabuf = arg.fd;
@@ -894,9 +877,7 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags)
 
        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;
        }
 
@@ -914,9 +895,8 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags)
        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);
+               TBM_SPRD_ERROR("error Cannot create bo(flag:%x, size:%d)\n",
+                       arg.flags, (unsigned int)arg.size);
                free(bo_sprd);
                return 0;
        }
@@ -929,7 +909,7 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags)
        bo_sprd->name = _get_name(bo_sprd->fd, bo_sprd->gem);
 
        if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 0)) {
-               TBM_SPRD_LOG("error fail init cache state(%d)\n", bo_sprd->name);
+               TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name);
                free(bo_sprd);
                return 0;
        }
@@ -942,9 +922,7 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags)
 
                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);
+                       TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                        free(bo_sprd);
                        return 0;
                }
@@ -954,25 +932,20 @@ tbm_sprd_bo_alloc(tbm_bo bo, int size, int flags)
        /* add bo to hash */
        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__);
+               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;
 }
@@ -992,15 +965,12 @@ tbm_sprd_bo_free(tbm_bo bo)
        bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
        SPRD_RETURN_IF_FAIL(bo_sprd != NULL);
 
-       DBG("[libtbm-sprd:%d] %s size:%d, gem:%d(%d)\n",
-            getpid(), __FUNCTION__, bo_sprd->size, bo_sprd->gem, bo_sprd->name);
+       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");
        }
 
        /* closedmabuf */
@@ -1022,9 +992,7 @@ tbm_sprd_bo_free(tbm_bo bo)
                        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(bufmgr_sprd, bo_sprd);
@@ -1033,11 +1001,8 @@ tbm_sprd_bo_free(tbm_bo bo)
        struct drm_gem_close arg = {0, };
        memset(&arg, 0, sizeof(arg));
        arg.handle = bo_sprd->gem;
-       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg)) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                             "error %s:%d\n",
-                             getpid(), __FUNCTION__, __LINE__);
-       }
+       if (drmIoctl(bo_sprd->fd, DRM_IOCTL_GEM_CLOSE, &arg))
+               TBM_SPRD_ERROR("error fail to DRM_IOCTL_GEM_CLOSE\n");
 
        free(bo_sprd);
 }
@@ -1065,9 +1030,7 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key)
 
        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);
+               TBM_SPRD_ERROR("error Cannot open gem name=%d\n", key);
                return NULL;
        }
 
@@ -1076,17 +1039,13 @@ 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);
+               TBM_SPRD_ERROR("error Cannot get gem info=%d\n", key);
                goto fail_get_gem;
        }
 
        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");
                goto fail_alloc_bo;
        }
 
@@ -1103,7 +1062,7 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key)
 #endif
 
        if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
-               TBM_SPRD_LOG("error fail init cache state(%d)\n", bo_sprd->name);
+               TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name);
                goto fail_init_cache;
        }
 
@@ -1112,9 +1071,7 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key)
 
                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);
+                       TBM_SPRD_ERROR("error Cannot dmabuf=%d\n", bo_sprd->gem);
                        goto fail_prime_handle_to_fd;
                }
                bo_sprd->dmabuf = arg.fd;
@@ -1123,23 +1080,18 @@ tbm_sprd_bo_import(tbm_bo bo, unsigned int key)
        /* add bo to hash */
        privGem = calloc(1, sizeof(PrivGem));
        if (!privGem) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] error %s:%d Fail to alloc\n",
-                             getpid(), __FUNCTION__, __LINE__);
+               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;
 
@@ -1178,16 +1130,16 @@ tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key)
 
        arg.fd = key;
        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));
+               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);
        if (!name) {
-               TBM_SPRD_LOG("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n",
-                               bo, gem, key, strerror(errno));
+               TBM_SPRD_ERROR("error bo:%p Cannot get name from gem:%d, fd:%d (%s)\n",
+                       bo, gem, key, strerror(errno));
                return NULL;
        }
 
@@ -1211,8 +1163,8 @@ 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));
+               TBM_SPRD_ERROR("error bo:%p Cannot get gem info from gem:%d, fd:%d (%s)\n",
+                       bo, gem, key, strerror(errno));
                return NULL;
        }
 
@@ -1221,7 +1173,7 @@ tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key)
 
        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);
+               TBM_SPRD_ERROR("error bo:%p fail to allocate the bo private\n", bo);
                return NULL;
        }
 
@@ -1233,34 +1185,31 @@ tbm_sprd_bo_import_fd(tbm_bo bo, tbm_fd key)
 
        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));
+               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 (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
-               TBM_SPRD_LOG("error fail init cache state(%d)\n", bo_sprd->name);
+               TBM_SPRD_ERROR("error fail init cache state(%d)\n", bo_sprd->name);
                goto fail_init_cache;
        }
 
        /* add bo to hash */
        privGem = calloc(1, sizeof(PrivGem));
        if (!privGem) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] "
-                             "error %s:%d Fail to callocprivGem\n",
-                             getpid(), __FUNCTION__, __LINE__);
+               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);
+               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,
@@ -1291,17 +1240,14 @@ tbm_sprd_bo_export(tbm_bo bo)
        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;
 }
@@ -1322,19 +1268,18 @@ tbm_sprd_bo_export_fd(tbm_bo bo)
        arg.handle = bo_sprd->gem;
        ret = drmIoctl(bo_sprd->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
        if (ret) {
-               TBM_SPRD_LOG("error bo:%p Cannot dmabuf=%d (%s)\n",
-                             bo, bo_sprd->gem, strerror(errno));
+               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;
 }
@@ -1352,21 +1297,18 @@ tbm_sprd_bo_get_handle(tbm_bo bo, int device)
        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);
        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;
        }
 
@@ -1389,21 +1331,18 @@ tbm_sprd_bo_map(tbm_bo bo, int device, int opt)
        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);
        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;
        }
 
@@ -1437,166 +1376,7 @@ tbm_sprd_bo_unmap(tbm_bo bo)
        if (bo_sprd->map_cnt == 0)
                _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);
-
-       return 1;
-}
-
-static int
-tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
-{
-       SPRD_RETURN_VAL_IF_FAIL(bo != NULL, 0);
-
-       tbm_bufmgr_sprd bufmgr_sprd;
-       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);
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       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;
-
-               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;
-               }
-
-               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, opt);
-
-               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);
-
-       tbm_bufmgr_sprd bufmgr_sprd;
-       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);
-
-       bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
-       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(),
-                            __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
+       TBM_SPRD_DEBUG("gem:%d(%d) \n", bo_sprd->gem, bo_sprd->name);
 
        return 1;
 }
@@ -1628,6 +1408,8 @@ tbm_sprd_bufmgr_deinit(void *priv)
 
        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);
@@ -1935,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;
        }
 
@@ -1966,7 +1748,7 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd)
 
        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;
        }
 
@@ -1975,7 +1757,7 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd)
                if (bufmgr_sprd->fd < 0) {
                        bufmgr_sprd->fd = _tbm_sprd_open_drm();
                        if (bufmgr_sprd->fd < 0) {
-                               TBM_SPRD_LOG("[libtbm-sprd:%d] error: Fail to open drm!\n", getpid());
+                               TBM_SPRD_ERROR("fail to open drm!\n");
                                goto fail_open_drm;
                        }
                }
@@ -1984,15 +1766,17 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int 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());
+                       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());
+                       TBM_SPRD_ERROR("fail to get auth drm info!\n");
                        goto fail_get_auth_info;
                }
+
+               tbm_drm_helper_set_fd(bufmgr_sprd->fd);
        }
 
        //Create Hash Table
@@ -2011,13 +1795,13 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd)
        }
 
        if (!_bufmgr_init_cache_state(bufmgr_sprd)) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] error: Fail to init bufmgr cache state\n", getpid());
+               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 alloc backend!\n", getpid());
+               TBM_SPRD_ERROR("fail to alloc backend!\n");
                goto fail_alloc_backend;
        }
 
@@ -2036,12 +1820,12 @@ 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 = tbm_sprd_bo_lock;
-       bufmgr_backend->bo_unlock = tbm_sprd_bo_unlock;
+       bufmgr_backend->bo_lock = NULL;
+       bufmgr_backend->bo_unlock = NULL;
        bufmgr_backend->bufmgr_bind_native_display = tbm_sprd_bufmgr_bind_native_display;
 
        if (!tbm_backend_init(bufmgr, bufmgr_backend)) {
-               TBM_SPRD_LOG("[libtbm-sprd:%d] error: Fail to init backend!\n", getpid());
+               TBM_SPRD_ERROR("fail to init backend!\n");
                goto fail_init_backend;
        }
 
@@ -2052,16 +1836,15 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd)
                env = getenv("TBM_SPRD_DEBUG");
                if (env) {
                        bDebug = atoi(env);
-                       TBM_SPRD_LOG("TBM_SPRD_DEBUG=%s\n", 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;
 
@@ -2074,6 +1857,10 @@ fail_init_cache_state:
                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: