1 /* 2 * Copyright (C) 2015 Red Hat, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 #include <drm/drm_file.h> 27 #include <drm/drm_fourcc.h> 28 29 #include "virtgpu_drv.h" 30 31 static int virtio_gpu_gem_create(struct drm_file *file, 32 struct drm_device *dev, 33 struct virtio_gpu_object_params *params, 34 struct drm_gem_object **obj_p, 35 uint32_t *handle_p) 36 { 37 struct virtio_gpu_device *vgdev = dev->dev_private; 38 struct virtio_gpu_object *obj; 39 int ret; 40 u32 handle; 41 42 ret = virtio_gpu_object_create(vgdev, params, &obj, NULL); 43 if (ret < 0) 44 return ret; 45 46 ret = drm_gem_handle_create(file, &obj->base.base, &handle); 47 if (ret) { 48 drm_gem_object_release(&obj->base.base); 49 return ret; 50 } 51 52 *obj_p = &obj->base.base; 53 54 /* drop reference from allocate - handle holds it now */ 55 drm_gem_object_put(&obj->base.base); 56 57 *handle_p = handle; 58 return 0; 59 } 60 61 int virtio_gpu_mode_dumb_create(struct drm_file *file_priv, 62 struct drm_device *dev, 63 struct drm_mode_create_dumb *args) 64 { 65 struct drm_gem_object *gobj; 66 struct virtio_gpu_object_params params = { 0 }; 67 struct virtio_gpu_device *vgdev = dev->dev_private; 68 int ret; 69 uint32_t pitch; 70 71 if (args->bpp != 32) 72 return -EINVAL; 73 74 pitch = args->width * 4; 75 args->size = pitch * args->height; 76 args->size = ALIGN(args->size, PAGE_SIZE); 77 78 params.format = virtio_gpu_translate_format(DRM_FORMAT_HOST_XRGB8888); 79 params.width = args->width; 80 params.height = args->height; 81 params.size = args->size; 82 params.dumb = true; 83 84 if (vgdev->has_resource_blob && !vgdev->has_virgl_3d) { 85 params.blob_mem = VIRTGPU_BLOB_MEM_GUEST; 86 params.blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE; 87 params.blob = true; 88 } 89 90 ret = virtio_gpu_gem_create(file_priv, dev, ¶ms, &gobj, 91 &args->handle); 92 if (ret) 93 goto fail; 94 95 args->pitch = pitch; 96 return ret; 97 98 fail: 99 return ret; 100 } 101 102 int virtio_gpu_gem_object_open(struct drm_gem_object *obj, 103 struct drm_file *file) 104 { 105 struct virtio_gpu_device *vgdev = obj->dev->dev_private; 106 struct virtio_gpu_fpriv *vfpriv = file->driver_priv; 107 struct virtio_gpu_object_array *objs; 108 109 if (!vgdev->has_virgl_3d) 110 goto out_notify; 111 112 /* the context might still be missing when the first ioctl is 113 * DRM_IOCTL_MODE_CREATE_DUMB or DRM_IOCTL_PRIME_FD_TO_HANDLE 114 */ 115 if (!vgdev->has_context_init) 116 virtio_gpu_create_context(obj->dev, file); 117 118 objs = virtio_gpu_array_alloc(1); 119 if (!objs) 120 return -ENOMEM; 121 virtio_gpu_array_add_obj(objs, obj); 122 123 if (vfpriv->ctx_id) 124 virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id, objs); 125 126 out_notify: 127 virtio_gpu_notify(vgdev); 128 return 0; 129 } 130 131 void virtio_gpu_gem_object_close(struct drm_gem_object *obj, 132 struct drm_file *file) 133 { 134 struct virtio_gpu_device *vgdev = obj->dev->dev_private; 135 struct virtio_gpu_fpriv *vfpriv = file->driver_priv; 136 struct virtio_gpu_object_array *objs; 137 138 if (!vgdev->has_virgl_3d) 139 return; 140 141 objs = virtio_gpu_array_alloc(1); 142 if (!objs) 143 return; 144 virtio_gpu_array_add_obj(objs, obj); 145 146 virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id, 147 objs); 148 virtio_gpu_notify(vgdev); 149 } 150 151 /* For drm panic */ 152 struct virtio_gpu_object_array *virtio_gpu_panic_array_alloc(void) 153 { 154 struct virtio_gpu_object_array *objs; 155 156 objs = kmalloc(sizeof(struct virtio_gpu_object_array), GFP_ATOMIC); 157 if (!objs) 158 return NULL; 159 160 objs->nents = 0; 161 objs->total = 1; 162 return objs; 163 } 164 165 struct virtio_gpu_object_array *virtio_gpu_array_alloc(u32 nents) 166 { 167 struct virtio_gpu_object_array *objs; 168 169 objs = kmalloc(struct_size(objs, objs, nents), GFP_KERNEL); 170 if (!objs) 171 return NULL; 172 173 objs->nents = 0; 174 objs->total = nents; 175 return objs; 176 } 177 178 static void virtio_gpu_array_free(struct virtio_gpu_object_array *objs) 179 { 180 kfree(objs); 181 } 182 183 struct virtio_gpu_object_array* 184 virtio_gpu_array_from_handles(struct drm_file *drm_file, u32 *handles, u32 nents) 185 { 186 struct virtio_gpu_object_array *objs; 187 u32 i; 188 189 objs = virtio_gpu_array_alloc(nents); 190 if (!objs) 191 return NULL; 192 193 for (i = 0; i < nents; i++) { 194 objs->objs[i] = drm_gem_object_lookup(drm_file, handles[i]); 195 if (!objs->objs[i]) { 196 objs->nents = i; 197 virtio_gpu_array_put_free(objs); 198 return NULL; 199 } 200 } 201 objs->nents = i; 202 return objs; 203 } 204 205 void virtio_gpu_array_add_obj(struct virtio_gpu_object_array *objs, 206 struct drm_gem_object *obj) 207 { 208 if (WARN_ON_ONCE(objs->nents == objs->total)) 209 return; 210 211 drm_gem_object_get(obj); 212 objs->objs[objs->nents] = obj; 213 objs->nents++; 214 } 215 216 int virtio_gpu_array_lock_resv(struct virtio_gpu_object_array *objs) 217 { 218 unsigned int i; 219 int ret; 220 221 if (objs->nents == 1) { 222 ret = dma_resv_lock_interruptible(objs->objs[0]->resv, NULL); 223 } else { 224 ret = drm_gem_lock_reservations(objs->objs, objs->nents, 225 &objs->ticket); 226 } 227 if (ret) 228 return ret; 229 230 for (i = 0; i < objs->nents; ++i) { 231 ret = dma_resv_reserve_fences(objs->objs[i]->resv, 1); 232 if (ret) { 233 virtio_gpu_array_unlock_resv(objs); 234 return ret; 235 } 236 } 237 return ret; 238 } 239 240 void virtio_gpu_array_unlock_resv(struct virtio_gpu_object_array *objs) 241 { 242 if (objs->nents == 1) { 243 dma_resv_unlock(objs->objs[0]->resv); 244 } else { 245 drm_gem_unlock_reservations(objs->objs, objs->nents, 246 &objs->ticket); 247 } 248 } 249 250 void virtio_gpu_array_add_fence(struct virtio_gpu_object_array *objs, 251 struct dma_fence *fence) 252 { 253 int i; 254 255 for (i = 0; i < objs->nents; i++) 256 dma_resv_add_fence(objs->objs[i]->resv, fence, 257 DMA_RESV_USAGE_WRITE); 258 } 259 260 void virtio_gpu_array_put_free(struct virtio_gpu_object_array *objs) 261 { 262 u32 i; 263 264 if (!objs) 265 return; 266 267 for (i = 0; i < objs->nents; i++) 268 drm_gem_object_put(objs->objs[i]); 269 virtio_gpu_array_free(objs); 270 } 271 272 void virtio_gpu_array_put_free_delayed(struct virtio_gpu_device *vgdev, 273 struct virtio_gpu_object_array *objs) 274 { 275 spin_lock(&vgdev->obj_free_lock); 276 list_add_tail(&objs->next, &vgdev->obj_free_list); 277 spin_unlock(&vgdev->obj_free_lock); 278 schedule_work(&vgdev->obj_free_work); 279 } 280 281 void virtio_gpu_array_put_free_work(struct work_struct *work) 282 { 283 struct virtio_gpu_device *vgdev = 284 container_of(work, struct virtio_gpu_device, obj_free_work); 285 struct virtio_gpu_object_array *objs; 286 287 spin_lock(&vgdev->obj_free_lock); 288 while (!list_empty(&vgdev->obj_free_list)) { 289 objs = list_first_entry(&vgdev->obj_free_list, 290 struct virtio_gpu_object_array, next); 291 list_del(&objs->next); 292 spin_unlock(&vgdev->obj_free_lock); 293 virtio_gpu_array_put_free(objs); 294 spin_lock(&vgdev->obj_free_lock); 295 } 296 spin_unlock(&vgdev->obj_free_lock); 297 } 298