mmap with the dmabuf fd 68/286868/1
authorChangyeon Lee <cyeon.lee@samsung.com>
Fri, 13 Jan 2023 06:44:58 +0000 (15:44 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Mon, 16 Jan 2023 04:42:41 +0000 (13:42 +0900)
DRM_IOCTL_MODE_MAP_DUMB support only handle which is created
with DRM_IOCTL_MODE_CREATE_DUMB
(not support imported handle(DRM_IOCTL_PRIME_FD_TO_HANDLE)
 in other process)

vc4 backend mmap with the dma buf fd and O_RDWR flag is added
in DRM_IOCTL_PRIME_HANDLE_TO_FD

Change-Id: Ieafb1bfbbbdcd6e3911859a4ee8d8d99c61e7caf

src/tbm_backend_vc4.c

index f3b6afa..0b09253 100644 (file)
@@ -268,6 +268,7 @@ _get_dmabuf(int fd, unsigned int gem)
        struct drm_prime_handle prime_arg = {0, };
 
        prime_arg.handle = gem;
+       prime_arg.flags = O_RDWR;
        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;
@@ -305,21 +306,14 @@ _close_gem(int fd, unsigned int gem)
 }
 
 static void *
-_mmap_gem(int fd, unsigned int gem, unsigned int size)
+_mmap_dmabuf(int dmabuf, unsigned int size)
 {
-       struct drm_mode_map_dumb map_arg = {0,};
        void *map;
 
-       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;
-       }
-
        map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
-                       fd, map_arg.offset);
+                       dmabuf, 0);
        if (map == MAP_FAILED) {
-               TBM_BACKEND_ERR("Cannot usrptr gem:%d size:%d", gem, size);
+               TBM_BACKEND_ERR("fail to mmap dmabuf:%d size:%d (%m)", dmabuf, size);
                return NULL;
        }
 
@@ -391,10 +385,18 @@ _vc4_bo_handle(tbm_vc4_bo *bo_data, int device)
                bo_handle.u32 = (uint32_t)bo_data->gem;
                break;
        case HAL_TBM_DEVICE_CPU:
+               if (!bo_data->dmabuf) {
+                       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;
+                       }
+               }
+
                if (!bo_data->pBase) {
-                       bo_data->pBase = _mmap_gem(bo_data->fd, bo_data->gem, bo_data->size);
+                       bo_data->pBase = _mmap_dmabuf(bo_data->dmabuf, bo_data->size);
                        if (!bo_data->pBase) {
-                               TBM_BACKEND_ERR("fail to mmap gem:%d", bo_data->gem);
+                               TBM_BACKEND_ERR("fail to mmap dmabuf:%d", bo_data->dmabuf);
                                return (hal_tbm_bo_handle) NULL;
                        }
                }