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