Clean up code 67/286867/1
authorChangyeon Lee <cyeon.lee@samsung.com>
Thu, 12 Jan 2023 08:26:56 +0000 (17:26 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Mon, 16 Jan 2023 04:42:39 +0000 (13:42 +0900)
Change-Id: I7b7c6e26e49b18fc15ab101058202f994d032044

src/tbm_backend_vc4.c

index bad3cdcf1b1c1d4c471a3ff569dd7e1ce06b66e7..f3b6afa930cb7a956dd5d192188b9961ceba75a5 100644 (file)
@@ -54,8 +54,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tbm_backend_log.h"
 #include "tbm_backend_list.h"
 
+#define TBM_DRM_NAME "vc4"
+
 #define TBM_COLOR_FORMAT_COUNT 4
-#define STRERR_BUFSIZE 128
 #define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1))
 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
@@ -75,7 +76,6 @@ typedef struct _tbm_vc4_bo      tbm_vc4_bo;
 struct _tbm_vc4_surface {
        struct list_head link;
        uint32_t refcnt;
-
        uint32_t width;
        uint32_t height;
        hal_tbm_format format;
@@ -87,26 +87,17 @@ struct _tbm_vc4_surface {
 /* tbm buffor object for vc4 */
 struct _tbm_vc4_bo {
        int fd;
-
-       unsigned int name;    /* FLINK ID */
-
-       unsigned int gem;     /* GEM Handle */
-
-       unsigned int dmabuf;  /* fd for dmabuf */
-
-       void *pBase;          /* virtual address */
-
+       unsigned int name; /* FLINK ID */
+       unsigned int gem; /* GEM Handle */
+       unsigned int dmabuf; /* fd for dmabuf */
+       void *pBase;  /* virtual address */
        unsigned int size;
-
        unsigned int flags_tbm; /*not used now*//*currently no values for the flags,but it may be used in future extension*/
-
        pthread_mutex_t mutex;
        int device;
        int opt;
-
        unsigned int map_cnt;
        int last_map_device;
-
        tbm_vc4_bufmgr *bufmgr_data;
 };
 
@@ -115,7 +106,6 @@ struct _tbm_vc4_bufmgr {
        int fd;
        int isLocal;
        void *hashBos;
-
        struct list_head surface_data_list;
 };
 
@@ -134,7 +124,6 @@ static char *STR_OPT[] = {
        "RDWR"
 };
 
-
 static uint32_t tbm_vc4_color_format_list[TBM_COLOR_FORMAT_COUNT] = {
                                                                                HAL_TBM_FORMAT_ARGB8888,
                                                                                HAL_TBM_FORMAT_XRGB8888,
@@ -149,32 +138,52 @@ static hal_tbm_bo *tbm_vc4_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd k
 static hal_tbm_fd tbm_vc4_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error);
 
 static int
-_tbm_vc4_open_drm()
+_tbm_vc4_open_fd(const char *filepath)
 {
-       struct udev *udev = NULL;
-       struct udev_enumerate *e = NULL;
-       struct udev_list_entry *entry = NULL;
-       struct udev_device *device = NULL, *drm_device = NULL, *pci = NULL;
-       const char *filepath, *id;
+       int fd, ret;
        struct stat s;
-       int fd = -1;
-       int ret;
+
+       /* Open DRM device file and check validity. */
+       fd = open(filepath, O_RDWR | O_CLOEXEC);
+       if (fd < 0) {
+               TBM_BACKEND_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.", filepath);
+               return -1;
+       }
+
+       ret = fstat(fd, &s);
+       if (ret) {
+               TBM_BACKEND_ERR("fstat() failed %s.");
+               close(fd);
+               return -1;
+       }
+
+       return fd;
+}
+
+static struct udev_device *
+_tbm_vc4_find_primary_gpu(void)
+{
+       struct udev *udev;
+       struct udev_enumerate *e;
+       struct udev_list_entry *entry;
+       const char *path, *id;
+       struct udev_device *device, *drm_device, *pci;
 
        udev = udev_new();
        if (!udev) {
-               TBM_BACKEND_ERR("udev_new() failed.");
-               return -1;
+               TBM_BACKEND_ERR("fail to initialize udev context\n");
+               return NULL;
        }
 
        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_enumerate_scan_devices(e);
        drm_device = NULL;
        udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-               filepath = udev_list_entry_get_name(entry);
-               device = udev_device_new_from_syspath(udev, filepath);
+               path = udev_list_entry_get_name(entry);
+               device = udev_device_new_from_syspath(udev, path);
                if (!device)
                        continue;
 
@@ -183,7 +192,7 @@ _tbm_vc4_open_drm()
                        id = udev_device_get_sysattr_value(pci, "boot_vga");
                        if (id && !strcmp(id, "1")) {
                                if (drm_device)
-                               udev_device_unref(drm_device);
+                                       udev_device_unref(drm_device);
                                drm_device = device;
                                break;
                        }
@@ -197,182 +206,178 @@ _tbm_vc4_open_drm()
 
        udev_enumerate_unref(e);
 
+       return drm_device;
+}
+
+static int
+_tbm_vc4_open_drm()
+{
+       struct udev_device *drm_device;
+       const char *filepath;
+       int fd = -1;
+
+       fd = drmOpen(TBM_DRM_NAME, NULL);
+       if (fd < 0)
+               TBM_BACKEND_WRN("Cannot open '%s' drm", TBM_DRM_NAME);
+       else
+               return fd;
+
+       drm_device = _tbm_vc4_find_primary_gpu();
+       if (drm_device == NULL) {
+               TBM_BACKEND_ERR("fail to find primary gpu");
+               return -1;
+       }
+
        /* Get device file path. */
        filepath = udev_device_get_devnode(drm_device);
        if (!filepath) {
                TBM_BACKEND_ERR("udev_device_get_devnode() failed.");
                udev_device_unref(drm_device);
-               udev_unref(udev);
                return -1;
        }
 
-       /* Open DRM device file and check validity. */
-       fd = open(filepath, O_RDWR | O_CLOEXEC);
+       fd = _tbm_vc4_open_fd(filepath);
        if (fd < 0) {
-               TBM_BACKEND_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               return -1;
-       }
-
-       ret = fstat(fd, &s);
-       if (ret) {
-               TBM_BACKEND_ERR("fstat() failed %s.");
-               close(fd);
+               TBM_BACKEND_ERR("fail to open %s", filepath);
                udev_device_unref(drm_device);
-               udev_unref(udev);
                return -1;
        }
 
        udev_device_unref(drm_device);
-       udev_unref(udev);
 
        return fd;
 }
 
-#if 0 // render node functions.
+static unsigned int
+_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_BACKEND_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d (%m)", gem);
+               return 0;
+       }
+
+       return (unsigned int)arg.name;
+}
+
 static int
-_check_render_node(void)
+_get_dmabuf(int fd, unsigned int gem)
 {
-#ifndef USE_RENDER_NODE
-       return 0;
-#else
-       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;
+       struct drm_prime_handle prime_arg = {0, };
 
-       udev = udev_new();
-       if (!udev) {
-               TBM_BACKEND_ERR("udev_new() failed.");
+       prime_arg.handle = gem;
+       if (drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem:%d (%m)", gem);
                return -1;
        }
 
-       e = udev_enumerate_new(udev);
-       udev_enumerate_add_match_subsystem(e, "drm");
-       udev_enumerate_add_match_sysname(e, "renderD[0-9]*");
-       udev_enumerate_scan_devices(e);
+       return prime_arg.fd;
+}
 
-       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), "vc4-drm") == 0) {
-                               drm_device = device;
-                               TBM_BACKEND_DBG("Found render device: '%s' (%s)",
-                                   udev_device_get_syspath(drm_device),
-                                   udev_device_get_sysname(device_parent));
-                               break;
-                       }
-               }
-               udev_device_unref(device);
+static unsigned int
+_get_gem(int fd, int dmabuf)
+{
+       struct drm_prime_handle prime_arg = {0, };
+
+       prime_arg.fd = dmabuf;
+       if (drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_PRIME_FD_TO_HANDLE dmabuf:%d (%m)", dmabuf);
+               return 0;
        }
 
-       udev_enumerate_unref(e);
-       udev_unref(udev);
+       return prime_arg.handle;
+}
 
-       if (!drm_device) {
-               udev_device_unref(drm_device);
+static int
+_close_gem(int fd, unsigned int gem)
+{
+       struct drm_gem_close close_arg = {0, };
+
+       close_arg.handle = gem;
+       if (drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_GEM_CLOSE gem:%d (%m)", gem);
                return 0;
        }
 
-       udev_device_unref(drm_device);
        return 1;
-#endif
 }
 
-static int
-_get_render_node(void)
+static void *
+_mmap_gem(int fd, unsigned int gem, unsigned int size)
 {
-       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;
+       struct drm_mode_map_dumb map_arg = {0,};
+       void *map;
 
-       udev = udev_new();
-       if (!udev) {
-               TBM_BACKEND_ERR("udev_new() failed.");
-               return -1;
+       map_arg.handle = gem;
+       if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_MAP_DUMB gem:%d (%m)", gem);
+               return NULL;
        }
 
-       e = udev_enumerate_new(udev);
-       udev_enumerate_add_match_subsystem(e, "drm");
-       udev_enumerate_add_match_sysname(e, "renderD[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), "vc4-drm") == 0) {
-                               drm_device = device;
-                               TBM_BACKEND_DBG("Found render device: '%s' (%s)",
-                                   udev_device_get_syspath(drm_device),
-                                   udev_device_get_sysname(device_parent));
-                               break;
-                       }
-               }
-               udev_device_unref(device);
+       map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
+                       fd, map_arg.offset);
+       if (map == MAP_FAILED) {
+               TBM_BACKEND_ERR("Cannot usrptr gem:%d size:%d", gem, size);
+               return NULL;
        }
 
-       udev_enumerate_unref(e);
+       return map;
+}
 
-       /* Get device file path. */
-       filepath = udev_device_get_devnode(drm_device);
-       if (!filepath) {
-               TBM_BACKEND_ERR("udev_device_get_devnode() failed.");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               return -1;
+static unsigned int
+_create_gem(int fd, unsigned int size, unsigned int flags)
+{
+       struct drm_mode_create_dumb create_arg = {0, };
+
+       create_arg.height = 1;
+       create_arg.bpp = 8;
+       create_arg.width = size;
+       create_arg.flags = flags;
+       if (drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_MODE_CREATE_DUMB size:%d flags:%d (%m)",
+                               size, flags);
+               return 0;
        }
 
-       /* Open DRM device file and check validity. */
-       fd = open(filepath, O_RDWR | O_CLOEXEC);
-       if (fd < 0) {
-               TBM_BACKEND_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               return -1;
-       }
+       return create_arg.handle;
+}
 
-       ret = fstat(fd, &s);
-       if (ret) {
-               TBM_BACKEND_ERR("fstat() failed %s.");
-               udev_device_unref(drm_device);
-               udev_unref(udev);
-               close(fd);
-               return -1;
+static int
+_open_gem(int fd, unsigned name, unsigned int *gem, unsigned int *size)
+{
+       struct drm_gem_open open_arg = {0, };
+
+       open_arg.name = name;
+       if (drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &open_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_GEM_OPEN name:%d (%m)", name);
+               return 0;
        }
 
-       udev_device_unref(drm_device);
-       udev_unref(udev);
+       *gem = open_arg.handle;
+       *size = open_arg.size;
 
-       return fd;
+       return 1;
 }
-#endif
 
 static unsigned int
-_get_name(int fd, unsigned int gem)
+_get_size_with_open(int fd, unsigned int name)
 {
-       struct drm_gem_flink arg = {0,};
+       unsigned int gem, size;
 
-       arg.handle = gem;
-       if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) {
-               TBM_BACKEND_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d", gem);
+       if (!_open_gem(fd, name, &gem, &size)) {
+               TBM_BACKEND_ERR("fail to get gem info from gem:%d name:%d", gem, name);
                return 0;
        }
 
-       return (unsigned int)arg.name;
+       if (!_close_gem(fd, gem))
+               TBM_BACKEND_ERR("fail to close gem:%d", gem);
+
+       return size;
 }
 
+
 static hal_tbm_bo_handle
 _vc4_bo_handle(tbm_vc4_bo *bo_data, int device)
 {
@@ -387,38 +392,23 @@ _vc4_bo_handle(tbm_vc4_bo *bo_data, int device)
                break;
        case HAL_TBM_DEVICE_CPU:
                if (!bo_data->pBase) {
-                       struct drm_mode_map_dumb arg = {0,};
-                       void *map = NULL;
-
-                       arg.handle = bo_data->gem;
-                       if (drmIoctl(bo_data->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg)) {
-                               TBM_BACKEND_ERR("Cannot map_vc4 gem=%d", bo_data->gem);
+                       bo_data->pBase = _mmap_gem(bo_data->fd, bo_data->gem, bo_data->size);
+                       if (!bo_data->pBase) {
+                               TBM_BACKEND_ERR("fail to mmap gem:%d", bo_data->gem);
                                return (hal_tbm_bo_handle) NULL;
                        }
-
-                       map = mmap(NULL, bo_data->size, PROT_READ | PROT_WRITE, MAP_SHARED,
-                                  bo_data->fd, arg.offset);
-                       if (map == MAP_FAILED) {
-                               TBM_BACKEND_ERR("Cannot usrptr gem=%d", bo_data->gem);
-                               return (hal_tbm_bo_handle) NULL;
-                       }
-                       bo_data->pBase = map;
                }
                bo_handle.ptr = (void *)bo_data->pBase;
                break;
        case HAL_TBM_DEVICE_3D:
        case HAL_TBM_DEVICE_MM:
                if (!bo_data->dmabuf) {
-                       struct drm_prime_handle arg = {0, };
-
-                       arg.handle = bo_data->gem;
-                       if (drmIoctl(bo_data->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                               TBM_BACKEND_ERR("Cannot dmabuf=%d", bo_data->gem);
+                       bo_data->dmabuf = _get_dmabuf(bo_data->fd, bo_data->gem);
+                       if (!bo_data->dmabuf) {
+                               TBM_BACKEND_ERR("fail to get dmabuf from gem:%d", bo_data->gem);
                                return (hal_tbm_bo_handle) NULL;
                        }
-                       bo_data->dmabuf = arg.fd;
                }
-
                bo_handle.u32 = (uint32_t)bo_data->dmabuf;
                break;
        default:
@@ -1042,55 +1032,43 @@ tbm_vc4_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
                        *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
                return NULL;
        }
+
        bo_data->bufmgr_data = bufmgr_data;
+       bo_data->fd = bufmgr_data->fd;
+       bo_data->size = size;
+       bo_data->flags_tbm = flags;
 
-       struct drm_mode_create_dumb arg = {0, };
-       //as we know only size for new bo set height=1 and bpp=8 and in this case
-       //width will by equal to size in bytes;
-       arg.height = 1;
-       arg.bpp = 8;
-       arg.width = size;
-       arg.flags = flags;/*currently no values for the flags,but it may be used in future extension*/
-       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg)) {
-               TBM_BACKEND_ERR("Cannot create bo_data(flag:%x, size:%d)", arg.flags,
-                              (unsigned int)arg.size);
+       bo_data->gem = _create_gem(bo_data->fd, size, 0);
+       if (!bo_data->gem) {
+               TBM_BACKEND_ERR("fail to create gem size:%d flags:%d", size, flags);
                free(bo_data);
                if (error)
-                       *error = HAL_TBM_ERROR_INVALID_OPERATION;
+                       *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
                return NULL;
        }
 
-       bo_data->fd = bufmgr_data->fd;
-       bo_data->gem = (unsigned int)arg.handle;
-       bo_data->size = size;
-       bo_data->flags_tbm = flags;
        bo_data->name = _get_name(bo_data->fd, bo_data->gem);
+       if (!bo_data->name) {
+               TBM_BACKEND_ERR("fail to get name from gem:%d", bo_data->gem);
+               _close_gem(bo_data->fd, bo_data->gem);
+               free(bo_data);
+               if (error)
+                       *error = HAL_TBM_ERROR_INVALID_OPERATION;
+               return NULL;
+       }
 
        pthread_mutex_init(&bo_data->mutex, NULL);
 
-       if (!bo_data->dmabuf) {
-               struct drm_prime_handle arg = {0, };
-
-               arg.handle = bo_data->gem;
-               if (drmIoctl(bo_data->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_BACKEND_ERR("Cannot dmabuf=%d", bo_data->gem);
-                       free(bo_data);
-                       if (error)
-                               *error = HAL_TBM_ERROR_INVALID_OPERATION;
-                       return NULL;
-               }
-               bo_data->dmabuf = arg.fd;
-       }
-
        /* add bo_data to hash */
        if (drmHashInsert(bufmgr_data->hashBos, bo_data->name, (void *)bo_data) < 0)
-               TBM_BACKEND_ERR("Cannot insert bo_data to Hash(%d)", bo_data->name);
+               TBM_BACKEND_ERR("fail to insert bo_data(%p) to Hash(%d)", bo_data, bo_data->name);
 
-       TBM_BACKEND_DBG("     bo_data:%p, gem:%d(%d), flags:%d, size:%d",
-               bo_data,
-               bo_data->gem, bo_data->name,
-           bo_data->flags_tbm,
-               bo_data->size);
+       TBM_BACKEND_DBG("bo_data:%p, gem:%d name:%d flags:%d size:%d",
+                       bo_data,
+                       bo_data->gem,
+                       bo_data->name,
+                       bo_data->flags_tbm,
+                       bo_data->size);
 
        if (error)
                *error = HAL_TBM_ERROR_NONE;
@@ -1103,10 +1081,8 @@ tbm_vc4_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *
 {
        tbm_vc4_bufmgr *bufmgr_data = (tbm_vc4_bufmgr *)bufmgr;
        tbm_vc4_bo *bo_data;
-       unsigned int gem = 0;
-       unsigned int name;
+       unsigned int gem, name, size;
        int ret;
-       char buf[STRERR_BUFSIZE];
 
        if (bufmgr_data == NULL) {
                TBM_BACKEND_ERR("bufmgr is null");
@@ -1115,24 +1091,17 @@ tbm_vc4_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *
                return NULL;
        }
 
-       /*getting handle from fd*/
-       struct drm_prime_handle arg = {0, };
-
-       arg.fd = key;
-       arg.flags = 0;
-       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &arg)) {
-               TBM_BACKEND_ERR("Cannot get gem handle from fd:%d (%s)",
-                              arg.fd, strerror_r(errno, buf, STRERR_BUFSIZE));
+       gem = _get_gem(bufmgr_data->fd, key);
+       if (!gem) {
+               TBM_BACKEND_ERR("fail to get gem from fd:%d", key);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
-       gem = arg.handle;
 
        name = _get_name(bufmgr_data->fd, gem);
        if (!name) {
-               TBM_BACKEND_ERR("Cannot get name from gem:%d, fd:%d (%s)",
-                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
+               TBM_BACKEND_ERR("fail to get name from gem:%d", gem, key);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_OPERATION;
                return NULL;
@@ -1153,62 +1122,38 @@ tbm_vc4_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *
         * kernels will just fail, in which case we fall back to the
         * provided (estimated or guess size).
         */
-       unsigned int real_size = -1;
-       struct drm_gem_open open_arg = {0, };
-
-       real_size = lseek(key, 0, SEEK_END);
-
-       open_arg.name = name;
-       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) {
-               TBM_BACKEND_ERR("Cannot get gem info from gem:%d, fd:%d (%s)",
-                              gem, key, strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = HAL_TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       /* Free gem handle to avoid a memory leak*/
-       struct drm_gem_close close_arg = {0, };
-       memset(&close_arg, 0, sizeof(close_arg));
-       close_arg.handle = open_arg.handle;
-       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg)) {
-               TBM_BACKEND_ERR("Cannot close gem_handle.",
-                             strerror_r(errno, buf, STRERR_BUFSIZE));
-               if (error)
-                       *error = HAL_TBM_ERROR_INVALID_OPERATION;
-               return NULL;
-       }
-
-       if (real_size == -1)
-               real_size = open_arg.size;
+       size = lseek(key, 0, SEEK_END);
+       if (size == -1)
+               size = _get_size_with_open(bufmgr_data->fd, name);
 
        bo_data = calloc(1, sizeof(struct _tbm_vc4_bo));
        if (!bo_data) {
                TBM_BACKEND_ERR("bo_data:%p fail to allocate the bo_data", bo_data);
+               _close_gem(bufmgr_data->fd, gem);
                if (error)
                        *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
                return NULL;
        }
-       bo_data->bufmgr_data = bufmgr_data;
 
+       bo_data->bufmgr_data = bufmgr_data;
        bo_data->fd = bufmgr_data->fd;
+       bo_data->size = size;
+       bo_data->flags_tbm = 0;
        bo_data->gem = gem;
-       bo_data->size = real_size;
        bo_data->name = name;
-       bo_data->flags_tbm = 0;
 
        /* add bo_data to hash */
        if (drmHashInsert(bufmgr_data->hashBos, bo_data->name, (void *)bo_data) < 0)
-               TBM_BACKEND_ERR("bo_data:%p Cannot insert bo_data to Hash(%d) from gem:%d, fd:%d",
-                              bo_data, bo_data->name, gem, key);
+               TBM_BACKEND_ERR("fail to insert bo_data(%p) to Hash(%d)", bo_data, bo_data->name);
 
-       TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           key,
-           bo_data->flags_tbm,
-           bo_data->size);
+       TBM_BACKEND_DBG("bo_data:%p gem:%d name:%d fd:%d key_fd:%d flags:%d size:%d",
+                       bo_data,
+                       bo_data->gem,
+                       bo_data->name,
+                       bo_data->dmabuf,
+                       key,
+                       bo_data->flags_tbm,
+                       bo_data->size);
 
        if (error)
                *error = HAL_TBM_ERROR_NONE;
@@ -1222,6 +1167,7 @@ tbm_vc4_bufmgr_import_key(hal_tbm_bufmgr *bufmgr, hal_tbm_key key, hal_tbm_error
        tbm_vc4_bufmgr *bufmgr_data = (tbm_vc4_bufmgr *)bufmgr;
        tbm_vc4_bo *bo_data;
        int ret;
+       unsigned int gem, size;
 
        if (bufmgr_data == NULL) {
                TBM_BACKEND_ERR("bufmgr is null");
@@ -1237,11 +1183,8 @@ tbm_vc4_bufmgr_import_key(hal_tbm_bufmgr *bufmgr, hal_tbm_key key, hal_tbm_error
                return (hal_tbm_bo *)bo_data;
        }
 
-       struct drm_gem_open arg = {0, };
-
-       arg.name = key;
-       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_OPEN, &arg)) {
-               TBM_BACKEND_ERR("Cannot open gem name=%d", key);
+       if (!_open_gem(bufmgr_data->fd, key, &gem, &size)) {
+               TBM_BACKEND_ERR("fail to open gem from name:%d", key);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_OPERATION;
                return NULL;
@@ -1250,42 +1193,31 @@ tbm_vc4_bufmgr_import_key(hal_tbm_bufmgr *bufmgr, hal_tbm_key key, hal_tbm_error
        bo_data = calloc(1, sizeof(struct _tbm_vc4_bo));
        if (!bo_data) {
                TBM_BACKEND_ERR("fail to allocate the bo_data private");
+               _close_gem(bufmgr_data->fd, gem);
                if (error)
                        *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
                return NULL;
        }
-       bo_data->bufmgr_data = bufmgr_data;
 
+       bo_data->bufmgr_data = bufmgr_data;
        bo_data->fd = bufmgr_data->fd;
-       bo_data->gem = arg.handle;
-       bo_data->size = arg.size;
-       bo_data->name = key;
+       bo_data->size = size;
        bo_data->flags_tbm = 0;
-
-       if (!bo_data->dmabuf) {
-               struct drm_prime_handle arg = {0, };
-
-               arg.handle = bo_data->gem;
-               if (drmIoctl(bo_data->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg)) {
-                       TBM_BACKEND_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem=%d", bo_data->gem);
-                       if (error)
-                               *error = HAL_TBM_ERROR_INVALID_OPERATION;
-                       free(bo_data);
-                       return NULL;
-               }
-               bo_data->dmabuf = arg.fd;
-       }
+       bo_data->dmabuf = -1;
+       bo_data->gem = gem;
+       bo_data->name = key;
 
        /* add bo_data to hash */
        if (drmHashInsert(bufmgr_data->hashBos, bo_data->name, (void *)bo_data) < 0)
-               TBM_BACKEND_ERR("Cannot insert bo_data to Hash(%d)", bo_data->name);
+               TBM_BACKEND_ERR("fail to insert bo_data(%p) to Hash(%d)", bo_data, bo_data->name);
 
-       TBM_BACKEND_DBG("    bo_data:%p, gem:%d(%d), fd:%d, flags:%d, size:%d",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           bo_data->flags_tbm,
-           bo_data->size);
+       TBM_BACKEND_DBG("bo_data:%p gem:%d name:%d dmabuf:%d flags:%d, size:%d",
+                       bo_data,
+                       bo_data->gem,
+                       bo_data->name,
+                       bo_data->dmabuf,
+                       bo_data->flags_tbm,
+                       bo_data->size);
 
        if (error)
                *error = HAL_TBM_ERROR_NONE;
@@ -1423,7 +1355,6 @@ tbm_vc4_bo_free(hal_tbm_bo *bo)
        tbm_vc4_bo *bo_data = (tbm_vc4_bo *)bo;
        tbm_vc4_bo *temp;
        tbm_vc4_bufmgr *bufmgr_data;
-       char buf[STRERR_BUFSIZE];
        int ret;
 
        if (!bo_data)
@@ -1433,16 +1364,15 @@ tbm_vc4_bo_free(hal_tbm_bo *bo)
        if (!bufmgr_data)
                return;
 
-       TBM_BACKEND_DBG("      bo_data:%p, gem:%d(%d), fd:%d, size:%d",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           bo_data->size);
+       TBM_BACKEND_DBG("bo_data:%p gem:%d name:%d dmabuf:%d size:%d",
+                       bo_data,
+                       bo_data->gem, bo_data->name,
+                       bo_data->dmabuf,
+                       bo_data->size);
 
        if (bo_data->pBase) {
                if (munmap(bo_data->pBase, bo_data->size) == -1) {
-                       TBM_BACKEND_ERR("bo_data:%p fail to munmap(%s)",
-                                      bo_data, strerror_r(errno, buf, STRERR_BUFSIZE));
+                       TBM_BACKEND_ERR("bo_data:%p fail to munmap (%m)", bo_data);
                }
        }
 
@@ -1457,19 +1387,13 @@ tbm_vc4_bo_free(hal_tbm_bo *bo)
        if (ret == 0)
                drmHashDelete(bufmgr_data->hashBos, bo_data->name);
        else
-               TBM_BACKEND_ERR("Cannot find bo_data to Hash(%d), ret=%d", bo_data->name, ret);
+               TBM_BACKEND_ERR("Cannot find bo_data to Hash(%d), ret:%d", bo_data->name, ret);
 
        if (temp != bo_data)
                TBM_BACKEND_ERR("hashBos probably has several BOs with same name!!!");
 
-       /* Free gem handle */
-       struct drm_gem_close arg = {0, };
-
-       memset(&arg, 0, sizeof(arg));
-       arg.handle = bo_data->gem;
-       if (drmIoctl(bo_data->fd, DRM_IOCTL_GEM_CLOSE, &arg))
-               TBM_BACKEND_ERR("bo_data:%p fail to gem close.(%s)",
-                              bo_data, strerror_r(errno, buf, STRERR_BUFSIZE));
+       if (!_close_gem(bo_data->fd, bo_data->gem))
+               TBM_BACKEND_ERR("bo_data:%p fail to gem:%d close. (%m)", bo_data, bo_data->gem);
 
        free(bo_data);
 }
@@ -1521,20 +1445,12 @@ tbm_vc4_bo_get_handle(hal_tbm_bo *bo, hal_tbm_bo_device_type device, hal_tbm_err
        }
 
        if (!bo_data->gem) {
-               TBM_BACKEND_ERR("Cannot map gem=%d", bo_data->gem);
+               TBM_BACKEND_ERR("Cannot map gem:%d", bo_data->gem);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_PARAMETER;
                return (hal_tbm_bo_handle) NULL;
        }
 
-       TBM_BACKEND_DBG("bo_data:%p, gem:%d(%d), fd:%d, flags:%d, size:%d, %s",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           bo_data->flags_tbm,
-           bo_data->size,
-           STR_DEVICE[device]);
-
        /*Get mapped bo_handle*/
        bo_handle = _vc4_bo_handle(bo_data, device);
        if (bo_handle.ptr == NULL) {
@@ -1545,6 +1461,14 @@ tbm_vc4_bo_get_handle(hal_tbm_bo *bo, hal_tbm_bo_device_type device, hal_tbm_err
                return (hal_tbm_bo_handle) NULL;
        }
 
+       TBM_BACKEND_DBG("bo_data:%p gem:%d name:%d dmabuf:%d flags:%d size:%d %s",
+                       bo_data,
+                       bo_data->gem, bo_data->name,
+                       bo_data->dmabuf,
+                       bo_data->flags_tbm,
+                       bo_data->size,
+                       STR_DEVICE[device]);
+
        if (error)
                *error = HAL_TBM_ERROR_NONE;
 
@@ -1573,24 +1497,24 @@ tbm_vc4_bo_map(hal_tbm_bo *bo, hal_tbm_bo_device_type device,
        }
 
        if (!bo_data->gem) {
-               TBM_BACKEND_ERR("Cannot map gem=%d", bo_data->gem);
+               TBM_BACKEND_ERR("Cannot map gem:%d", bo_data->gem);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_PARAMETER;
                return (hal_tbm_bo_handle) NULL;
        }
 
-       TBM_BACKEND_DBG("       bo_data:%p, gem:%d(%d), fd:%d, %s, %s",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           STR_DEVICE[device],
-           STR_OPT[opt]);
+       TBM_BACKEND_DBG("bo_data:%p  gem:%d name:%d fd:%d %s %s",
+                       bo_data,
+                       bo_data->gem, bo_data->name,
+                       bo_data->dmabuf,
+                       STR_DEVICE[device],
+                       STR_OPT[opt]);
 
        /*Get mapped bo_handle*/
        bo_handle = _vc4_bo_handle(bo_data, device);
        if (bo_handle.ptr == NULL) {
                TBM_BACKEND_ERR("Cannot get handle: gem:%d, device:%d, opt:%d",
-                              bo_data->gem, device, opt);
+                               bo_data->gem, device, opt);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_OPERATION;
                return (hal_tbm_bo_handle) NULL;
@@ -1626,10 +1550,10 @@ tbm_vc4_bo_unmap(hal_tbm_bo *bo)
 
        bo_data->last_map_device = -1;
 
-       TBM_BACKEND_DBG("     bo_data:%p, gem:%d(%d), fd:%d",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf);
+       TBM_BACKEND_DBG("bo_data:%p gem:%d name:%d dmabuf:%d",
+                       bo_data,
+                       bo_data->gem, bo_data->name,
+                       bo_data->dmabuf);
 
        return HAL_TBM_ERROR_NONE;
 }
@@ -1651,8 +1575,7 @@ static hal_tbm_fd
 tbm_vc4_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error)
 {
        tbm_vc4_bo *bo_data = (tbm_vc4_bo *)bo;
-       int ret;
-       char buf[STRERR_BUFSIZE];
+       int fd;
 
        if (!bo_data) {
                if (error)
@@ -1660,30 +1583,26 @@ tbm_vc4_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error)
                return -1;
        }
 
-       struct drm_prime_handle arg = {0, };
-
-       arg.handle = bo_data->gem;
-       ret = drmIoctl(bo_data->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
-       if (ret) {
-               TBM_BACKEND_ERR("bo_data:%p Cannot dmabuf=%d (%s)",
-                              bo_data, bo_data->gem, strerror_r(errno, buf, STRERR_BUFSIZE));
+       fd = _get_dmabuf(bo_data->fd, bo_data->gem);
+       if (fd < 0) {
+               TBM_BACKEND_ERR("fail to get dmabuf");
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_OPERATION;
-               return (hal_tbm_fd) ret;
+               return (hal_tbm_fd)fd;
        }
 
-       TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           arg.fd,
-           bo_data->flags_tbm,
-           bo_data->size);
+       TBM_BACKEND_DBG("bo_data:%p, gem:%d name:%d dmabuf:%d, key_fd:%d, flags:%d, size:%d",
+                       bo_data,
+                       bo_data->gem, bo_data->name,
+                       bo_data->dmabuf,
+                       fd,
+                       bo_data->flags_tbm,
+                       bo_data->size);
 
        if (error)
                *error = HAL_TBM_ERROR_NONE;
 
-       return (hal_tbm_fd)arg.fd;
+       return (hal_tbm_fd)fd;
 }
 
 static hal_tbm_key
@@ -1707,12 +1626,13 @@ tbm_vc4_bo_export_key(hal_tbm_bo *bo, hal_tbm_error *error)
                }
        }
 
-       TBM_BACKEND_DBG("    bo_data:%p, gem:%d(%d), fd:%d, flags:%d, size:%d",
-           bo_data,
-           bo_data->gem, bo_data->name,
-           bo_data->dmabuf,
-           bo_data->flags_tbm,
-           bo_data->size);
+       TBM_BACKEND_DBG("bo_data:%p gem:%d name:%d fd:%d flags:%d size:%d",
+                       bo_data,
+                       bo_data->gem,
+                       bo_data->name,
+                       bo_data->dmabuf,
+                       bo_data->flags_tbm,
+                       bo_data->size);
 
        if (error)
                *error = HAL_TBM_ERROR_NONE;