added tbm_drm_helper_set/unset_fd
[platform/adaptation/spreadtrum/libtbm-sprd.git] / src / tbm_bufmgr_sprd.c
index 6866db9..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
@@ -254,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)
 {
@@ -271,7 +274,6 @@ _tgl_get_version(int fd)
        return 1;
 }
 
-
 static inline int
 _tgl_init(int fd, unsigned int key)
 {
@@ -328,7 +330,7 @@ _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_ERROR("error(%s) key:%d opt:%d\n",
                        strerror(errno), key, opt);
@@ -347,7 +349,7 @@ _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_ERROR("error(%s) key:%d\n",
                        strerror(errno), key);
@@ -357,7 +359,6 @@ _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)
 {
@@ -476,8 +477,7 @@ _tbm_sprd_open_drm(void)
                return -1;
        }
 #else
-       TBM_SPRD_ERROR("warning fail to open drm\n",
-                     );
+       TBM_SPRD_ERROR("warning fail to open drm\n");
 #endif
 
        return fd;
@@ -534,6 +534,7 @@ _sprd_bo_cache_flush(tbm_bufmgr_sprd bufmgr_sprd, tbm_bo_sprd bo_sprd, int flags
                return 0;
        }
 #endif
+
        return 1;
 }
 #endif
@@ -541,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);
 
@@ -549,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) {
@@ -656,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);
 
@@ -663,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)
@@ -675,9 +679,14 @@ _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_ERROR("fail to open global_lock:%s\n", 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)) {
@@ -686,7 +695,6 @@ _bufmgr_init_cache_state(tbm_bufmgr_sprd bufmgr_sprd)
                return 0;
        }
 
-#ifdef USE_CACHE
        if (!_tgl_init(bufmgr_sprd->tgl_fd, GLOBAL_KEY)) {
                TBM_SPRD_ERROR("fail to initialize the tgl\n");
                close(bufmgr_sprd->tgl_fd);
@@ -700,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)
@@ -707,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
@@ -1371,149 +1381,6 @@ tbm_sprd_bo_unmap(tbm_bo bo)
        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 {
-                               TBM_SPRD_DEBUG("GET_FENCE is ignored(device type is not 3D/CPU),\n");
-                               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 {
-                               TBM_SPRD_DEBUG("GET_FENCE is ignored(device type is not 3D/CPU),\n");
-                               return 0;
-                       }
-               } else {
-                       TBM_SPRD_ERROR("error Invalid argument\n");
-                       return 0;
-               }
-
-               ret = ioctl(bo_sprd->dmabuf, DMABUF_IOCTL_GET_FENCE, &fence);
-               if (ret < 0) {
-                       TBM_SPRD_ERROR("error  Can not set GET FENCE(%s)\n", 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_ERROR("error  fence list is full\n");
-               }
-               pthread_mutex_unlock(&bo_sprd->mutex);
-
-               TBM_SPRD_DEBUG("DMABUF_IOCTL_GET_FENCE! flink_id=%d dmabuf=%d\n",
-                       bo_sprd->name, bo_sprd->dmabuf);
-       } else {
-               ret = _tgl_lock(bufmgr_sprd->tgl_fd, bo_sprd->name, opt);
-
-               TBM_SPRD_DEBUG("lock tgl flink_id:%d\n", 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) {
-                       TBM_SPRD_DEBUG("FENCE not support or ignored,\n";
-                       return 0;
-               }
-
-               if (!bo_sprd->dma_fence[0].type) {
-                       TBM_SPRD_DEBUG("device type is not 3D/CPU,\n");
-                       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_ERROR("error  Can not set PUT FENCE(%s)\n", strerror(errno));
-                       return 0;
-               }
-
-               TBM_SPRD_DEBUG("DMABUF_IOCTL_PUT_FENCE! flink_id=%d dmabuf=%d\n",
-                       bo_sprd->name, bo_sprd->dmabuf);
-       } else {
-               ret = _tgl_unlock(bufmgr_sprd->tgl_fd, bo_sprd->name);
-
-               TBM_SPRD_DEBUG("unlock tgl flink_id:%d\n", bo_sprd->name);
-
-               return ret;
-       }
-#endif
-
-       return 1;
-}
-
 static void
 tbm_sprd_bufmgr_deinit(void *priv)
 {
@@ -1541,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);
@@ -1906,6 +1775,8 @@ init_tbm_bufmgr_priv(tbm_bufmgr bufmgr, int fd)
                        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
@@ -1949,8 +1820,8 @@ 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)) {
@@ -1986,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: