1 /* 2 * Copyright 2014 Canonical 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Andreas Pokorny 23 */ 24 25 #include <drm/drm_prime.h> 26 #include <linux/virtio_dma_buf.h> 27 28 #include "virtgpu_drv.h" 29 30 MODULE_IMPORT_NS("DMA_BUF"); 31 32 static int virtgpu_virtio_get_uuid(struct dma_buf *buf, 33 uuid_t *uuid) 34 { 35 struct drm_gem_object *obj = buf->priv; 36 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 37 struct virtio_gpu_device *vgdev = obj->dev->dev_private; 38 39 wait_event(vgdev->resp_wq, bo->uuid_state != STATE_INITIALIZING); 40 if (bo->uuid_state != STATE_OK) 41 return -ENODEV; 42 43 uuid_copy(uuid, &bo->uuid); 44 45 return 0; 46 } 47 48 static struct sg_table * 49 virtgpu_gem_map_dma_buf(struct dma_buf_attachment *attach, 50 enum dma_data_direction dir) 51 { 52 struct drm_gem_object *obj = attach->dmabuf->priv; 53 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 54 55 if (virtio_gpu_is_vram(bo)) 56 return virtio_gpu_vram_map_dma_buf(bo, attach->dev, dir); 57 58 return drm_gem_map_dma_buf(attach, dir); 59 } 60 61 static void virtgpu_gem_unmap_dma_buf(struct dma_buf_attachment *attach, 62 struct sg_table *sgt, 63 enum dma_data_direction dir) 64 { 65 struct drm_gem_object *obj = attach->dmabuf->priv; 66 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 67 68 if (virtio_gpu_is_vram(bo)) { 69 virtio_gpu_vram_unmap_dma_buf(attach->dev, sgt, dir); 70 return; 71 } 72 73 drm_gem_unmap_dma_buf(attach, sgt, dir); 74 } 75 76 static const struct virtio_dma_buf_ops virtgpu_dmabuf_ops = { 77 .ops = { 78 .attach = virtio_dma_buf_attach, 79 .detach = drm_gem_map_detach, 80 .map_dma_buf = virtgpu_gem_map_dma_buf, 81 .unmap_dma_buf = virtgpu_gem_unmap_dma_buf, 82 .release = drm_gem_dmabuf_release, 83 .mmap = drm_gem_dmabuf_mmap, 84 .vmap = drm_gem_dmabuf_vmap, 85 .vunmap = drm_gem_dmabuf_vunmap, 86 }, 87 .device_attach = drm_gem_map_attach, 88 .get_uuid = virtgpu_virtio_get_uuid, 89 }; 90 91 int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev, 92 struct virtio_gpu_object *bo) 93 { 94 struct virtio_gpu_object_array *objs; 95 96 objs = virtio_gpu_array_alloc(1); 97 if (!objs) 98 return -ENOMEM; 99 100 virtio_gpu_array_add_obj(objs, &bo->base.base); 101 102 return virtio_gpu_cmd_resource_assign_uuid(vgdev, objs); 103 } 104 105 struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, 106 int flags) 107 { 108 struct dma_buf *buf; 109 struct drm_device *dev = obj->dev; 110 struct virtio_gpu_device *vgdev = dev->dev_private; 111 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 112 int ret = 0; 113 bool blob = bo->host3d_blob || bo->guest_blob; 114 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 115 116 if (!blob) { 117 if (vgdev->has_resource_assign_uuid) { 118 ret = virtio_gpu_resource_assign_uuid(vgdev, bo); 119 if (ret) 120 return ERR_PTR(ret); 121 122 virtio_gpu_notify(vgdev); 123 } else { 124 bo->uuid_state = STATE_ERR; 125 } 126 } else if (!(bo->blob_flags & VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE)) { 127 bo->uuid_state = STATE_ERR; 128 } 129 130 exp_info.ops = &virtgpu_dmabuf_ops.ops; 131 exp_info.size = obj->size; 132 exp_info.flags = flags; 133 exp_info.priv = obj; 134 exp_info.resv = obj->resv; 135 136 buf = virtio_dma_buf_export(&exp_info); 137 if (IS_ERR(buf)) 138 return buf; 139 140 drm_dev_get(dev); 141 drm_gem_object_get(obj); 142 143 return buf; 144 } 145 146 int virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents, 147 unsigned int *nents, 148 struct virtio_gpu_object *bo, 149 struct dma_buf_attachment *attach) 150 { 151 struct scatterlist *sl; 152 struct sg_table *sgt; 153 long i, ret; 154 155 dma_resv_assert_held(attach->dmabuf->resv); 156 157 ret = dma_resv_wait_timeout(attach->dmabuf->resv, 158 DMA_RESV_USAGE_KERNEL, 159 false, MAX_SCHEDULE_TIMEOUT); 160 if (ret <= 0) 161 return ret < 0 ? ret : -ETIMEDOUT; 162 163 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); 164 if (IS_ERR(sgt)) 165 return PTR_ERR(sgt); 166 167 *ents = kvmalloc_objs(struct virtio_gpu_mem_entry, sgt->nents); 168 if (!(*ents)) { 169 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); 170 return -ENOMEM; 171 } 172 173 *nents = sgt->nents; 174 for_each_sgtable_dma_sg(sgt, sl, i) { 175 (*ents)[i].addr = cpu_to_le64(sg_dma_address(sl)); 176 (*ents)[i].length = cpu_to_le32(sg_dma_len(sl)); 177 (*ents)[i].padding = 0; 178 } 179 180 bo->sgt = sgt; 181 return 0; 182 } 183 184 static void virtgpu_dma_buf_unmap(struct virtio_gpu_object *bo) 185 { 186 struct dma_buf_attachment *attach = bo->base.base.import_attach; 187 188 dma_resv_assert_held(attach->dmabuf->resv); 189 190 if (bo->created) { 191 virtio_gpu_detach_object_fenced(bo); 192 193 if (bo->sgt) 194 dma_buf_unmap_attachment(attach, bo->sgt, 195 DMA_BIDIRECTIONAL); 196 197 bo->sgt = NULL; 198 } 199 } 200 201 static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj) 202 { 203 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 204 struct virtio_gpu_device *vgdev = obj->dev->dev_private; 205 struct dma_buf_attachment *attach = obj->import_attach; 206 207 if (drm_gem_is_imported(obj)) { 208 struct dma_buf *dmabuf = attach->dmabuf; 209 210 dma_resv_lock(dmabuf->resv, NULL); 211 virtgpu_dma_buf_unmap(bo); 212 dma_resv_unlock(dmabuf->resv); 213 214 dma_buf_detach(dmabuf, attach); 215 dma_buf_put(dmabuf); 216 } 217 218 if (bo->created) { 219 virtio_gpu_cmd_unref_resource(vgdev, bo); 220 virtio_gpu_notify(vgdev); 221 return; 222 } 223 virtio_gpu_cleanup_object(bo); 224 } 225 226 static int virtgpu_dma_buf_init_obj(struct drm_device *dev, 227 struct virtio_gpu_object *bo, 228 struct dma_buf_attachment *attach) 229 { 230 struct virtio_gpu_device *vgdev = dev->dev_private; 231 struct virtio_gpu_object_params params = { 0 }; 232 struct dma_resv *resv = attach->dmabuf->resv; 233 struct virtio_gpu_mem_entry *ents = NULL; 234 unsigned int nents; 235 int ret; 236 237 ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle); 238 if (ret) { 239 virtgpu_dma_buf_free_obj(&bo->base.base); 240 return ret; 241 } 242 243 dma_resv_lock(resv, NULL); 244 245 ret = dma_buf_pin(attach); 246 if (ret) 247 goto err_pin; 248 249 ret = virtgpu_dma_buf_import_sgt(&ents, &nents, bo, attach); 250 if (ret) 251 goto err_import; 252 253 params.blob = true; 254 params.blob_mem = VIRTGPU_BLOB_MEM_GUEST; 255 params.blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE; 256 params.size = attach->dmabuf->size; 257 258 virtio_gpu_cmd_resource_create_blob(vgdev, bo, ¶ms, 259 ents, nents); 260 bo->guest_blob = true; 261 262 dma_buf_unpin(attach); 263 dma_resv_unlock(resv); 264 265 return 0; 266 267 err_import: 268 dma_buf_unpin(attach); 269 err_pin: 270 dma_resv_unlock(resv); 271 virtgpu_dma_buf_free_obj(&bo->base.base); 272 return ret; 273 } 274 275 static const struct drm_gem_object_funcs virtgpu_gem_dma_buf_funcs = { 276 .free = virtgpu_dma_buf_free_obj, 277 }; 278 279 static void virtgpu_dma_buf_move_notify(struct dma_buf_attachment *attach) 280 { 281 struct drm_gem_object *obj = attach->importer_priv; 282 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 283 284 virtgpu_dma_buf_unmap(bo); 285 } 286 287 static const struct dma_buf_attach_ops virtgpu_dma_buf_attach_ops = { 288 .allow_peer2peer = true, 289 .move_notify = virtgpu_dma_buf_move_notify 290 }; 291 292 struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, 293 struct dma_buf *buf) 294 { 295 struct virtio_gpu_device *vgdev = dev->dev_private; 296 struct dma_buf_attachment *attach; 297 struct virtio_gpu_object *bo; 298 struct drm_gem_object *obj; 299 int ret; 300 301 if (buf->ops == &virtgpu_dmabuf_ops.ops) { 302 obj = buf->priv; 303 if (obj->dev == dev) { 304 /* 305 * Importing dmabuf exported from our own gem increases 306 * refcount on gem itself instead of f_count of dmabuf. 307 */ 308 drm_gem_object_get(obj); 309 return obj; 310 } 311 } 312 313 if (!vgdev->has_resource_blob || vgdev->has_virgl_3d) 314 return drm_gem_prime_import(dev, buf); 315 316 bo = kzalloc_obj(*bo); 317 if (!bo) 318 return ERR_PTR(-ENOMEM); 319 320 obj = &bo->base.base; 321 obj->resv = buf->resv; 322 obj->funcs = &virtgpu_gem_dma_buf_funcs; 323 drm_gem_private_object_init(dev, obj, buf->size); 324 325 attach = dma_buf_dynamic_attach(buf, dev->dev, 326 &virtgpu_dma_buf_attach_ops, obj); 327 if (IS_ERR(attach)) { 328 kfree(bo); 329 return ERR_CAST(attach); 330 } 331 332 obj->import_attach = attach; 333 get_dma_buf(buf); 334 335 ret = virtgpu_dma_buf_init_obj(dev, bo, attach); 336 if (ret < 0) 337 return ERR_PTR(ret); 338 339 return obj; 340 } 341 342 struct drm_gem_object *virtgpu_gem_prime_import_sg_table( 343 struct drm_device *dev, struct dma_buf_attachment *attach, 344 struct sg_table *table) 345 { 346 return ERR_PTR(-ENODEV); 347 } 348