virgl ioctls
authorMateusz Majewski <m.majewski2@samsung.com>
Wed, 20 Mar 2024 09:09:21 +0000 (10:09 +0100)
committerMateusz Majewski <m.majewski2@samsung.com>
Wed, 20 Mar 2024 09:09:21 +0000 (10:09 +0100)
src/tbm_backend_dumb.c

index 0f7eea643b211e08489ff1aa8e283735f758bf06..34498c48524b8ccad7a12f322ff45944df440306 100644 (file)
@@ -51,6 +51,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <hal-common.h>
 #include <hal-tbm-types.h>
 #include <hal-tbm-interface.h>
+#include <drm/virtgpu_drm.h>
 #include "tbm_backend_log.h"
 
 #ifdef HAVE_DMA_BUF
@@ -262,6 +263,23 @@ static unsigned int
 _get_dumb_flag_from_tbm(unsigned int ftbm)
 {
        unsigned int flags = 0;
+
+       flags |= (1 << 0); // depth stencil
+       flags |= (1 << 1); // render target
+       flags |= (1 << 3); // sampler view
+       flags |= (1 << 4); // vertex buffer
+       flags |= (1 << 5); // index buffer
+       flags |= (1 << 6); // constant buffer
+       flags |= (1 << 7); // display target
+       flags |= (1 << 8); // command args
+       flags |= (1 << 11); // stream output
+       flags |= (1 << 14); // shader buffer
+       flags |= (1 << 15); // query buffer
+       flags |= (1 << 22); // linear
+
+       if (ftbm & HAL_TBM_BO_SCANOUT)
+               flags |= (1 << 18); // scanout
+
        return flags;
 }
 
@@ -270,7 +288,8 @@ _get_tbm_flag_from_dumb(unsigned int fdumb)
 {
        unsigned int flags = 0;
 
-       flags |= HAL_TBM_BO_SCANOUT;
+       if (fdumb & (1 << 18)) // scanout
+               flags |= HAL_TBM_BO_SCANOUT;
        flags |= HAL_TBM_BO_NONCACHABLE;
 
        return flags;
@@ -665,7 +684,7 @@ tbm_dumb_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
        tbm_dumb_bufmgr *bufmgr_data = (tbm_dumb_bufmgr *)bufmgr;
        tbm_dumb_bo *bo_data;
        unsigned int dumb_flags;
-       struct drm_mode_create_dumb create_dumb_arg = {0, };
+       struct drm_virtgpu_resource_create create_dumb_arg = {0, };
        struct drm_gem_close close_arg = {0, };
        int dmabuf;
 
@@ -717,7 +736,7 @@ tbm_dumb_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
 
        //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;
-       create_dumb_arg.bpp = 32; // virtio-gpu refuses to accept bpp=8
+       create_dumb_arg.format = 3; // ARGB; XRGB would be 4
        if (size <= 16384) {
                create_dumb_arg.height = 1;
                create_dumb_arg.width = (size + 4 - 1) / 4;
@@ -735,21 +754,29 @@ tbm_dumb_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
                        break;
                }
        }
-
-       create_dumb_arg.flags = dumb_flags;
-       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb_arg)) {
-               TBM_BACKEND_ERR("fail to DRM_IOCTL_MODE_CREATE_DUMB flag:%x size:%d",
-                               create_dumb_arg.flags, (unsigned int)size);
+       create_dumb_arg.depth = 1;
+       create_dumb_arg.size = create_dumb_arg.width * create_dumb_arg.height;
+       create_dumb_arg.stride = create_dumb_arg.width * 4;
+       create_dumb_arg.target = 2; // PIPE_TEXTURE_2D... hopefully :)
+       create_dumb_arg.array_size = 1;
+       create_dumb_arg.last_level = 0;
+       create_dumb_arg.nr_samples = 0; // ????????
+       create_dumb_arg.flags = 0; // I don't think they are used at all in this ioctl. PERSISTENT and COHERENT both look useful, but probably need the other ioctl.
+
+       create_dumb_arg.bind = dumb_flags;
+       if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &create_dumb_arg)) {
+               TBM_BACKEND_ERR("fail to DRM_IOCTL_VIRTGPU_RESOURCE_CREATE flag:%x size:%d",
+                               create_dumb_arg.bind, (unsigned int)size);
                if (error)
                        *error = HAL_TBM_ERROR_INVALID_OPERATION;
                return NULL;
        }
 
-       dmabuf = _get_dmabuf(bufmgr_data->fd, create_dumb_arg.handle);
+       dmabuf = _get_dmabuf(bufmgr_data->fd, create_dumb_arg.bo_handle);
        if (dmabuf < 0) {
                TBM_BACKEND_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD gem:%d",
-                               create_dumb_arg.handle);
-               close_arg.handle = create_dumb_arg.handle;
+                               create_dumb_arg.bo_handle);
+               close_arg.handle = create_dumb_arg.bo_handle;
                drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
 
                if (error)
@@ -767,7 +794,7 @@ tbm_dumb_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
 
        bo_data->bufmgr_data = bufmgr_data;
        bo_data->fd = bufmgr_data->fd;
-       bo_data->gem = create_dumb_arg.handle;
+       bo_data->gem = create_dumb_arg.bo_handle;
        bo_data->size = create_dumb_arg.size;
        bo_data->dmabuf = dmabuf;
        bo_data->flags_tbm = flags;