use tgl lock if don't support dma fence
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
index 63b0eb6..428658a 100755 (executable)
@@ -230,7 +230,6 @@ struct _tbm_bufmgr_sprd {
        int tgl_fd;
 
        void *bind_display;
-
        char *device_name;
 };
 
@@ -327,6 +326,7 @@ _tgl_unlock(int fd, unsigned int key)
        return 1;
 }
 
+#ifdef USE_CACHE
 static inline int
 _tgl_set_data(int fd, unsigned int key, unsigned int val)
 {
@@ -366,6 +366,7 @@ _tgl_get_data(int fd, unsigned int key, unsigned int *locked)
 
        return arg.data1;
 }
+#endif
 
 static int
 _tbm_sprd_open_drm()
@@ -459,24 +460,17 @@ _tbm_sprd_open_drm()
        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 (bo_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
        struct drm_sprd_gem_cache_op cache_op = {0, };
        int ret;
 
@@ -517,43 +511,49 @@ _sprd_bo_cache_flush (tbm_bo bo, int flags)
                              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);
 
+       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 +595,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,48 +604,95 @@ _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;
-
-       bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
+#ifdef USE_CACHE
        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;
+
+       unsigned short cntFlush = 0;
+
        /* get global cache flush count */
        cntFlush = (unsigned short)_tgl_get_data(bufmgr_sprd->tgl_fd, GLOBAL_KEY, NULL);
 
        /* 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);
-
-       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) {
+               bufmgr_sprd->tgl_fd = open(tgl_devfile1, 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);
+
+                       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)
@@ -832,7 +879,7 @@ tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
        }
 #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))) {
@@ -850,7 +897,7 @@ tbm_sprd_bo_alloc (tbm_bo bo, int size, int flags)
        bo_sprd->flags_sprd = sprd_flags;
        bo_sprd->name = _get_name (bo_sprd->fd, bo_sprd->gem);
 
-       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd)) {
+       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;
@@ -941,7 +988,7 @@ tbm_sprd_bo_free(tbm_bo bo)
                              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, };
@@ -972,7 +1019,6 @@ tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
 
        ret = drmHashLookup (bufmgr_sprd->hashBos, key, (void **)&privGem);
        if (ret == 0) {
-               privGem->ref_count++;
                return privGem->bo_priv;
        }
 
@@ -1018,8 +1064,8 @@ tbm_sprd_bo_import (tbm_bo bo, unsigned int key)
        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);
+       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
+               TBM_SPRD_LOG ("error fail init cache state(%d)\n", bo_sprd->name);
                free (bo_sprd);
                return 0;
        }
@@ -1065,7 +1111,7 @@ tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
        tbm_bo_sprd bo_sprd;
        PrivGem *privGem = NULL;
        int ret;
-       int name;
+       unsigned int name;
 
        bufmgr_sprd = (tbm_bufmgr_sprd)tbm_backend_get_bufmgr_priv(bo);
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
@@ -1088,7 +1134,6 @@ tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
        ret = drmHashLookup (bufmgr_sprd->hashBos, name, (void **)&privGem);
        if (ret == 0) {
                if (gem == privGem->bo_priv->gem) {
-                       privGem->ref_count++;
                        return privGem->bo_priv;
                }
        }
@@ -1136,8 +1181,8 @@ tbm_sprd_bo_import_fd (tbm_bo bo, tbm_fd key)
                return 0;
        }
 
-       if (!_tgl_init(bufmgr_sprd->tgl_fd, bo_sprd->name)) {
-               TBM_SPRD_LOG ("error fail tgl init(%d)\n", bo_sprd->name);
+       if (!_bo_init_cache_state(bufmgr_sprd, bo_sprd, 1)) {
+               TBM_SPRD_LOG ("error fail init cache state(%d)\n", bo_sprd->name);
                free (bo_sprd);
                return 0;
        }
@@ -1275,6 +1320,10 @@ tbm_sprd_bo_map (tbm_bo bo, int device, int opt)
 
        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);
@@ -1299,7 +1348,7 @@ tbm_sprd_bo_map (tbm_bo bo, int device, int opt)
        }
 
        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++;
 
@@ -1312,6 +1361,10 @@ tbm_sprd_bo_unmap (tbm_bo bo)
        SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
 
        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, 0);
 
        bo_sprd = (tbm_bo_sprd)tbm_backend_get_bo_priv(bo);
        SPRD_RETURN_VAL_IF_FAIL (bo_sprd != NULL, 0);
@@ -1322,7 +1375,7 @@ tbm_sprd_bo_unmap (tbm_bo bo)
        bo_sprd->map_cnt--;
 
        if (bo_sprd->map_cnt == 0)
-               _bo_save_cache_state (bo);
+               _bo_save_cache_state (bufmgr_sprd, bo_sprd);
 
        DBG ("[libtbm-sprd:%d] %s gem:%d(%d) \n", getpid(),
             __FUNCTION__, bo_sprd->gem, bo_sprd->name);
@@ -1335,7 +1388,6 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
 {
        SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
 
-#if USE_BACKEND_LOCK
        tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
        int ret = 0;
@@ -1347,6 +1399,7 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
 
        if (bufmgr_sprd->use_dma_fence) {
+#if USE_BACKEND_LOCK
                struct dma_buf_fence fence;
 
                memset(&fence, 0, sizeof(struct dma_buf_fence));
@@ -1416,6 +1469,7 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
                     getpid(),
                     __FUNCTION__, bo_sprd->name, bo_sprd->dmabuf);
        } else {
+
                ret = _tgl_lock(bufmgr_sprd->tgl_fd, bo_sprd->name);
 
                DBG ("[libtbm-sprd:%d] lock tgl flink_id:%d\n",
@@ -1423,8 +1477,8 @@ tbm_sprd_bo_lock(tbm_bo bo, int device, int opt)
 
                return ret;
        }
-
 #endif
+
        return 1;
 }
 
@@ -1433,7 +1487,6 @@ tbm_sprd_bo_unlock(tbm_bo bo)
 {
        SPRD_RETURN_VAL_IF_FAIL (bo != NULL, 0);
 
-#if USE_BACKEND_LOCK
        tbm_bufmgr_sprd bufmgr_sprd;
        tbm_bo_sprd bo_sprd;
        int ret = 0;
@@ -1445,6 +1498,7 @@ tbm_sprd_bo_unlock(tbm_bo bo)
        SPRD_RETURN_VAL_IF_FAIL (bufmgr_sprd != NULL, 0);
 
        if (bufmgr_sprd->use_dma_fence) {
+#if USE_BACKEND_LOCK
                struct dma_buf_fence fence;
 
                if (!bo_sprd->dma_fence[0].ctx) {
@@ -1491,6 +1545,7 @@ tbm_sprd_bo_unlock(tbm_bo bo)
                return ret;
        }
 #endif
+
        return 1;
 }
 
@@ -1519,10 +1574,14 @@ tbm_sprd_bufmgr_deinit (void *priv)
        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);
+       _bufmgr_deinit_cache_state(bufmgr_sprd);
+
        close (bufmgr_sprd->fd);
 
        free (bufmgr_sprd);
@@ -1553,7 +1612,6 @@ tbm_sprd_surface_supported_format(uint32_t **formats, uint32_t *num)
 
 /**
  * @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
@@ -1565,7 +1623,7 @@ tbm_sprd_surface_supported_format(uint32_t **formats, uint32_t *num)
  * @return 1 if this function succeeds, otherwise 0.
  */
 int
-tbm_sprd_surface_get_plane_data(tbm_surface_h surface, int width, int height,
+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)
 {
@@ -1864,12 +1922,10 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
 
                bufmgr_sprd->fd = -1;
                master_fd = tbm_drm_helper_get_master_fd();
-               if (master_fd < 0) {
+               if (master_fd < 0)
                        bufmgr_sprd->fd = _tbm_sprd_open_drm();
-                       tbm_drm_helper_set_master_fd(bufmgr_sprd->fd);
-               } else {
-                       bufmgr_sprd->fd = dup(master_fd);
-               }
+               else
+                       bufmgr_sprd->fd = master_fd;
 
                if (bufmgr_sprd->fd < 0) {
                        TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
@@ -1877,11 +1933,14 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
                        return 0;
                }
 
+               tbm_drm_helper_set_tbm_master_fd(bufmgr_sprd->fd);
+
                bufmgr_sprd->device_name = drmGetDeviceNameFromFd(bufmgr_sprd->fd);
 
                if (!bufmgr_sprd->device_name)
                {
                        TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to get device name!\n", getpid());
+                       close(bufmgr_sprd->fd);
                        free (bufmgr_sprd);
                        return 0;
                }
@@ -1894,34 +1953,6 @@ init_tbm_bufmgr_priv (tbm_bufmgr bufmgr, int fd)
                }
        }
 
-       /* 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 ();
 
@@ -1938,12 +1969,24 @@ 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: init bufmgr cache state failed!\n", getpid());
+
+               tbm_drm_helper_unset_tbm_master_fd();
+               close(bufmgr_sprd->fd);
+
+               free(bufmgr_sprd);
+               return 0;
+       }
+
        bufmgr_backend = tbm_backend_alloc();
        if (!bufmgr_backend) {
                TBM_SPRD_LOG ("[libtbm-sprd:%d] error: Fail to create drm!\n", getpid());
 
+               _bufmgr_deinit_cache_state(bufmgr_sprd);
+
+               tbm_drm_helper_unset_tbm_master_fd();
                close(bufmgr_sprd->fd);
-               close(bufmgr_sprd->tgl_fd);
 
                free (bufmgr_sprd);
                return 0;
@@ -1964,19 +2007,17 @@ 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);
+               _bufmgr_deinit_cache_state(bufmgr_sprd);
+
+               tbm_drm_helper_unset_tbm_master_fd();
                close(bufmgr_sprd->fd);
 
                free (bufmgr_sprd);