1 1.1 riastrad /* $NetBSD: virtgpu_object.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright (C) 2015 Red Hat, Inc. 5 1.1 riastrad * All Rights Reserved. 6 1.1 riastrad * 7 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining 8 1.1 riastrad * a copy of this software and associated documentation files (the 9 1.1 riastrad * "Software"), to deal in the Software without restriction, including 10 1.1 riastrad * without limitation the rights to use, copy, modify, merge, publish, 11 1.1 riastrad * distribute, sublicense, and/or sell copies of the Software, and to 12 1.1 riastrad * permit persons to whom the Software is furnished to do so, subject to 13 1.1 riastrad * the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice (including the 16 1.1 riastrad * next paragraph) shall be included in all copies or substantial 17 1.1 riastrad * portions of the Software. 18 1.1 riastrad * 19 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 1.1 riastrad * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 1.1 riastrad * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 1.1 riastrad * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 23 1.1 riastrad * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 1.1 riastrad * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 1.1 riastrad * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 1.1 riastrad */ 27 1.1 riastrad 28 1.1 riastrad #include <sys/cdefs.h> 29 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: virtgpu_object.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $"); 30 1.1 riastrad 31 1.3 riastrad #include <linux/moduleparam.h> 32 1.3 riastrad 33 1.1 riastrad #include "virtgpu_drv.h" 34 1.1 riastrad 35 1.3 riastrad static int virtio_gpu_virglrenderer_workaround = 1; 36 1.3 riastrad module_param_named(virglhack, virtio_gpu_virglrenderer_workaround, int, 0400); 37 1.3 riastrad 38 1.3 riastrad static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev, 39 1.3 riastrad uint32_t *resid) 40 1.1 riastrad { 41 1.3 riastrad if (virtio_gpu_virglrenderer_workaround) { 42 1.3 riastrad /* 43 1.3 riastrad * Hack to avoid re-using resource IDs. 44 1.3 riastrad * 45 1.3 riastrad * virglrenderer versions up to (and including) 0.7.0 46 1.3 riastrad * can't deal with that. virglrenderer commit 47 1.3 riastrad * "f91a9dd35715 Fix unlinking resources from hash 48 1.3 riastrad * table." (Feb 2019) fixes the bug. 49 1.3 riastrad */ 50 1.3 riastrad static int handle; 51 1.3 riastrad handle++; 52 1.3 riastrad *resid = handle + 1; 53 1.3 riastrad } else { 54 1.3 riastrad int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL); 55 1.3 riastrad if (handle < 0) 56 1.3 riastrad return handle; 57 1.3 riastrad *resid = handle + 1; 58 1.3 riastrad } 59 1.3 riastrad return 0; 60 1.3 riastrad } 61 1.3 riastrad 62 1.3 riastrad static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id) 63 1.3 riastrad { 64 1.3 riastrad if (!virtio_gpu_virglrenderer_workaround) { 65 1.3 riastrad ida_free(&vgdev->resource_ida, id - 1); 66 1.3 riastrad } 67 1.3 riastrad } 68 1.1 riastrad 69 1.3 riastrad static void virtio_gpu_free_object(struct drm_gem_object *obj) 70 1.3 riastrad { 71 1.3 riastrad struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 72 1.3 riastrad struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private; 73 1.1 riastrad 74 1.3 riastrad if (bo->pages) 75 1.3 riastrad virtio_gpu_object_detach(vgdev, bo); 76 1.3 riastrad if (bo->created) 77 1.1 riastrad virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle); 78 1.3 riastrad virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle); 79 1.3 riastrad 80 1.3 riastrad drm_gem_shmem_free_object(obj); 81 1.1 riastrad } 82 1.1 riastrad 83 1.3 riastrad static const struct drm_gem_object_funcs virtio_gpu_gem_funcs = { 84 1.3 riastrad .free = virtio_gpu_free_object, 85 1.3 riastrad .open = virtio_gpu_gem_object_open, 86 1.3 riastrad .close = virtio_gpu_gem_object_close, 87 1.3 riastrad 88 1.3 riastrad .print_info = drm_gem_shmem_print_info, 89 1.3 riastrad .pin = drm_gem_shmem_pin, 90 1.3 riastrad .unpin = drm_gem_shmem_unpin, 91 1.3 riastrad .get_sg_table = drm_gem_shmem_get_sg_table, 92 1.3 riastrad .vmap = drm_gem_shmem_vmap, 93 1.3 riastrad .vunmap = drm_gem_shmem_vunmap, 94 1.3 riastrad .mmap = &drm_gem_shmem_mmap, 95 1.3 riastrad }; 96 1.3 riastrad 97 1.3 riastrad struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev, 98 1.3 riastrad size_t size) 99 1.1 riastrad { 100 1.3 riastrad struct virtio_gpu_object *bo; 101 1.1 riastrad 102 1.3 riastrad bo = kzalloc(sizeof(*bo), GFP_KERNEL); 103 1.3 riastrad if (!bo) 104 1.3 riastrad return NULL; 105 1.1 riastrad 106 1.3 riastrad bo->base.base.funcs = &virtio_gpu_gem_funcs; 107 1.3 riastrad return &bo->base.base; 108 1.1 riastrad } 109 1.1 riastrad 110 1.1 riastrad int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, 111 1.3 riastrad struct virtio_gpu_object_params *params, 112 1.3 riastrad struct virtio_gpu_object **bo_ptr, 113 1.3 riastrad struct virtio_gpu_fence *fence) 114 1.1 riastrad { 115 1.3 riastrad struct virtio_gpu_object_array *objs = NULL; 116 1.3 riastrad struct drm_gem_shmem_object *shmem_obj; 117 1.1 riastrad struct virtio_gpu_object *bo; 118 1.1 riastrad int ret; 119 1.1 riastrad 120 1.1 riastrad *bo_ptr = NULL; 121 1.1 riastrad 122 1.3 riastrad params->size = roundup(params->size, PAGE_SIZE); 123 1.3 riastrad shmem_obj = drm_gem_shmem_create(vgdev->ddev, params->size); 124 1.3 riastrad if (IS_ERR(shmem_obj)) 125 1.3 riastrad return PTR_ERR(shmem_obj); 126 1.3 riastrad bo = gem_to_virtio_gpu_obj(&shmem_obj->base); 127 1.3 riastrad 128 1.3 riastrad ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle); 129 1.3 riastrad if (ret < 0) 130 1.3 riastrad goto err_free_gem; 131 1.3 riastrad 132 1.3 riastrad bo->dumb = params->dumb; 133 1.3 riastrad 134 1.3 riastrad if (fence) { 135 1.3 riastrad ret = -ENOMEM; 136 1.3 riastrad objs = virtio_gpu_array_alloc(1); 137 1.3 riastrad if (!objs) 138 1.3 riastrad goto err_put_id; 139 1.3 riastrad virtio_gpu_array_add_obj(objs, &bo->base.base); 140 1.3 riastrad 141 1.3 riastrad ret = virtio_gpu_array_lock_resv(objs); 142 1.3 riastrad if (ret != 0) 143 1.3 riastrad goto err_put_objs; 144 1.3 riastrad } 145 1.3 riastrad 146 1.3 riastrad if (params->virgl) { 147 1.3 riastrad virtio_gpu_cmd_resource_create_3d(vgdev, bo, params, 148 1.3 riastrad objs, fence); 149 1.3 riastrad } else { 150 1.3 riastrad virtio_gpu_cmd_create_resource(vgdev, bo, params, 151 1.3 riastrad objs, fence); 152 1.3 riastrad } 153 1.1 riastrad 154 1.3 riastrad ret = virtio_gpu_object_attach(vgdev, bo, NULL); 155 1.1 riastrad if (ret != 0) { 156 1.3 riastrad virtio_gpu_free_object(&shmem_obj->base); 157 1.1 riastrad return ret; 158 1.1 riastrad } 159 1.1 riastrad 160 1.1 riastrad *bo_ptr = bo; 161 1.1 riastrad return 0; 162 1.1 riastrad 163 1.3 riastrad err_put_objs: 164 1.3 riastrad virtio_gpu_array_put_free(objs); 165 1.3 riastrad err_put_id: 166 1.3 riastrad virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle); 167 1.3 riastrad err_free_gem: 168 1.3 riastrad drm_gem_shmem_free_object(&shmem_obj->base); 169 1.3 riastrad return ret; 170 1.1 riastrad } 171