1 /* 2 * Copyright © 2012 Red Hat 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 (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 * Authors: 24 * Dave Airlie <airlied@redhat.com> 25 * Rob Clark <rob.clark@linaro.org> 26 * 27 */ 28 29 #include <linux/export.h> 30 #include <linux/dma-buf.h> 31 #include <linux/rbtree.h> 32 #include <linux/module.h> 33 34 #include <drm/drm.h> 35 #include <drm/drm_drv.h> 36 #include <drm/drm_file.h> 37 #include <drm/drm_framebuffer.h> 38 #include <drm/drm_gem.h> 39 #include <drm/drm_prime.h> 40 41 #include "drm_internal.h" 42 43 MODULE_IMPORT_NS(DMA_BUF); 44 45 /** 46 * DOC: overview and lifetime rules 47 * 48 * Similar to GEM global names, PRIME file descriptors are also used to share 49 * buffer objects across processes. They offer additional security: as file 50 * descriptors must be explicitly sent over UNIX domain sockets to be shared 51 * between applications, they can't be guessed like the globally unique GEM 52 * names. 53 * 54 * Drivers that support the PRIME API implement the drm_gem_object_funcs.export 55 * and &drm_driver.gem_prime_import hooks. &dma_buf_ops implementations for 56 * drivers are all individually exported for drivers which need to overwrite 57 * or reimplement some of them. 58 * 59 * Reference Counting for GEM Drivers 60 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 61 * 62 * On the export the &dma_buf holds a reference to the exported buffer object, 63 * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD 64 * IOCTL, when it first calls &drm_gem_object_funcs.export 65 * and stores the exporting GEM object in the &dma_buf.priv field. This 66 * reference needs to be released when the final reference to the &dma_buf 67 * itself is dropped and its &dma_buf_ops.release function is called. For 68 * GEM-based drivers, the &dma_buf should be exported using 69 * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release(). 70 * 71 * Thus the chain of references always flows in one direction, avoiding loops: 72 * importing GEM object -> dma-buf -> exported GEM bo. A further complication 73 * are the lookup caches for import and export. These are required to guarantee 74 * that any given object will always have only one unique userspace handle. This 75 * is required to allow userspace to detect duplicated imports, since some GEM 76 * drivers do fail command submissions if a given buffer object is listed more 77 * than once. These import and export caches in &drm_prime_file_private only 78 * retain a weak reference, which is cleaned up when the corresponding object is 79 * released. 80 * 81 * Self-importing: If userspace is using PRIME as a replacement for flink then 82 * it will get a fd->handle request for a GEM object that it created. Drivers 83 * should detect this situation and return back the underlying object from the 84 * dma-buf private. For GEM based drivers this is handled in 85 * drm_gem_prime_import() already. 86 */ 87 88 struct drm_prime_member { 89 struct dma_buf *dma_buf; 90 uint32_t handle; 91 92 struct rb_node dmabuf_rb; 93 struct rb_node handle_rb; 94 }; 95 96 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, 97 struct dma_buf *dma_buf, uint32_t handle) 98 { 99 struct drm_prime_member *member; 100 struct rb_node **p, *rb; 101 102 member = kmalloc(sizeof(*member), GFP_KERNEL); 103 if (!member) 104 return -ENOMEM; 105 106 get_dma_buf(dma_buf); 107 member->dma_buf = dma_buf; 108 member->handle = handle; 109 110 rb = NULL; 111 p = &prime_fpriv->dmabufs.rb_node; 112 while (*p) { 113 struct drm_prime_member *pos; 114 115 rb = *p; 116 pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb); 117 if (dma_buf > pos->dma_buf) 118 p = &rb->rb_right; 119 else 120 p = &rb->rb_left; 121 } 122 rb_link_node(&member->dmabuf_rb, rb, p); 123 rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs); 124 125 rb = NULL; 126 p = &prime_fpriv->handles.rb_node; 127 while (*p) { 128 struct drm_prime_member *pos; 129 130 rb = *p; 131 pos = rb_entry(rb, struct drm_prime_member, handle_rb); 132 if (handle > pos->handle) 133 p = &rb->rb_right; 134 else 135 p = &rb->rb_left; 136 } 137 rb_link_node(&member->handle_rb, rb, p); 138 rb_insert_color(&member->handle_rb, &prime_fpriv->handles); 139 140 return 0; 141 } 142 143 static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv, 144 uint32_t handle) 145 { 146 struct rb_node *rb; 147 148 rb = prime_fpriv->handles.rb_node; 149 while (rb) { 150 struct drm_prime_member *member; 151 152 member = rb_entry(rb, struct drm_prime_member, handle_rb); 153 if (member->handle == handle) 154 return member->dma_buf; 155 else if (member->handle < handle) 156 rb = rb->rb_right; 157 else 158 rb = rb->rb_left; 159 } 160 161 return NULL; 162 } 163 164 static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, 165 struct dma_buf *dma_buf, 166 uint32_t *handle) 167 { 168 struct rb_node *rb; 169 170 rb = prime_fpriv->dmabufs.rb_node; 171 while (rb) { 172 struct drm_prime_member *member; 173 174 member = rb_entry(rb, struct drm_prime_member, dmabuf_rb); 175 if (member->dma_buf == dma_buf) { 176 *handle = member->handle; 177 return 0; 178 } else if (member->dma_buf < dma_buf) { 179 rb = rb->rb_right; 180 } else { 181 rb = rb->rb_left; 182 } 183 } 184 185 return -ENOENT; 186 } 187 188 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, 189 uint32_t handle) 190 { 191 struct rb_node *rb; 192 193 mutex_lock(&prime_fpriv->lock); 194 195 rb = prime_fpriv->handles.rb_node; 196 while (rb) { 197 struct drm_prime_member *member; 198 199 member = rb_entry(rb, struct drm_prime_member, handle_rb); 200 if (member->handle == handle) { 201 rb_erase(&member->handle_rb, &prime_fpriv->handles); 202 rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs); 203 204 dma_buf_put(member->dma_buf); 205 kfree(member); 206 break; 207 } else if (member->handle < handle) { 208 rb = rb->rb_right; 209 } else { 210 rb = rb->rb_left; 211 } 212 } 213 214 mutex_unlock(&prime_fpriv->lock); 215 } 216 217 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv) 218 { 219 rw_init(&prime_fpriv->lock, "primlk"); 220 prime_fpriv->dmabufs = RB_ROOT; 221 prime_fpriv->handles = RB_ROOT; 222 } 223 224 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv) 225 { 226 /* by now drm_gem_release should've made sure the list is empty */ 227 WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs)); 228 } 229 230 /** 231 * drm_gem_dmabuf_export - &dma_buf export implementation for GEM 232 * @dev: parent device for the exported dmabuf 233 * @exp_info: the export information used by dma_buf_export() 234 * 235 * This wraps dma_buf_export() for use by generic GEM drivers that are using 236 * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take 237 * a reference to the &drm_device and the exported &drm_gem_object (stored in 238 * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release(). 239 * 240 * Returns the new dmabuf. 241 */ 242 struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev, 243 struct dma_buf_export_info *exp_info) 244 { 245 struct drm_gem_object *obj = exp_info->priv; 246 struct dma_buf *dma_buf; 247 248 dma_buf = dma_buf_export(exp_info); 249 if (IS_ERR(dma_buf)) 250 return dma_buf; 251 252 drm_dev_get(dev); 253 drm_gem_object_get(obj); 254 #ifdef __linux__ 255 dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping; 256 #endif 257 258 return dma_buf; 259 } 260 EXPORT_SYMBOL(drm_gem_dmabuf_export); 261 262 /** 263 * drm_gem_dmabuf_release - &dma_buf release implementation for GEM 264 * @dma_buf: buffer to be released 265 * 266 * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers 267 * must use this in their &dma_buf_ops structure as the release callback. 268 * drm_gem_dmabuf_release() should be used in conjunction with 269 * drm_gem_dmabuf_export(). 270 */ 271 void drm_gem_dmabuf_release(struct dma_buf *dma_buf) 272 { 273 struct drm_gem_object *obj = dma_buf->priv; 274 struct drm_device *dev = obj->dev; 275 276 /* drop the reference on the export fd holds */ 277 drm_gem_object_put(obj); 278 279 drm_dev_put(dev); 280 } 281 EXPORT_SYMBOL(drm_gem_dmabuf_release); 282 283 /** 284 * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers 285 * @dev: drm_device to import into 286 * @file_priv: drm file-private structure 287 * @prime_fd: fd id of the dma-buf which should be imported 288 * @handle: pointer to storage for the handle of the imported buffer object 289 * 290 * This is the PRIME import function which must be used mandatorily by GEM 291 * drivers to ensure correct lifetime management of the underlying GEM object. 292 * The actual importing of GEM object from the dma-buf is done through the 293 * &drm_driver.gem_prime_import driver callback. 294 * 295 * Returns 0 on success or a negative error code on failure. 296 */ 297 int drm_gem_prime_fd_to_handle(struct drm_device *dev, 298 struct drm_file *file_priv, int prime_fd, 299 uint32_t *handle) 300 { 301 struct dma_buf *dma_buf; 302 struct drm_gem_object *obj; 303 int ret; 304 305 dma_buf = dma_buf_get(prime_fd); 306 if (IS_ERR(dma_buf)) 307 return PTR_ERR(dma_buf); 308 309 mutex_lock(&file_priv->prime.lock); 310 311 ret = drm_prime_lookup_buf_handle(&file_priv->prime, 312 dma_buf, handle); 313 if (ret == 0) 314 goto out_put; 315 316 /* never seen this one, need to import */ 317 mutex_lock(&dev->object_name_lock); 318 if (dev->driver->gem_prime_import) 319 obj = dev->driver->gem_prime_import(dev, dma_buf); 320 else 321 obj = drm_gem_prime_import(dev, dma_buf); 322 if (IS_ERR(obj)) { 323 ret = PTR_ERR(obj); 324 goto out_unlock; 325 } 326 327 if (obj->dma_buf) { 328 WARN_ON(obj->dma_buf != dma_buf); 329 } else { 330 obj->dma_buf = dma_buf; 331 get_dma_buf(dma_buf); 332 } 333 334 /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */ 335 ret = drm_gem_handle_create_tail(file_priv, obj, handle); 336 drm_gem_object_put(obj); 337 if (ret) 338 goto out_put; 339 340 ret = drm_prime_add_buf_handle(&file_priv->prime, 341 dma_buf, *handle); 342 mutex_unlock(&file_priv->prime.lock); 343 if (ret) 344 goto fail; 345 346 dma_buf_put(dma_buf); 347 348 return 0; 349 350 fail: 351 /* hmm, if driver attached, we are relying on the free-object path 352 * to detach.. which seems ok.. 353 */ 354 drm_gem_handle_delete(file_priv, *handle); 355 dma_buf_put(dma_buf); 356 return ret; 357 358 out_unlock: 359 mutex_unlock(&dev->object_name_lock); 360 out_put: 361 mutex_unlock(&file_priv->prime.lock); 362 dma_buf_put(dma_buf); 363 return ret; 364 } 365 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle); 366 367 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data, 368 struct drm_file *file_priv) 369 { 370 struct drm_prime_handle *args = data; 371 372 if (dev->driver->prime_fd_to_handle) { 373 return dev->driver->prime_fd_to_handle(dev, file_priv, args->fd, 374 &args->handle); 375 } 376 377 return drm_gem_prime_fd_to_handle(dev, file_priv, args->fd, &args->handle); 378 } 379 380 static struct dma_buf *export_and_register_object(struct drm_device *dev, 381 struct drm_gem_object *obj, 382 uint32_t flags) 383 { 384 struct dma_buf *dmabuf; 385 386 /* prevent races with concurrent gem_close. */ 387 if (obj->handle_count == 0) { 388 dmabuf = ERR_PTR(-ENOENT); 389 return dmabuf; 390 } 391 392 if (obj->funcs && obj->funcs->export) 393 dmabuf = obj->funcs->export(obj, flags); 394 else 395 dmabuf = drm_gem_prime_export(obj, flags); 396 if (IS_ERR(dmabuf)) { 397 /* normally the created dma-buf takes ownership of the ref, 398 * but if that fails then drop the ref 399 */ 400 return dmabuf; 401 } 402 403 /* 404 * Note that callers do not need to clean up the export cache 405 * since the check for obj->handle_count guarantees that someone 406 * will clean it up. 407 */ 408 obj->dma_buf = dmabuf; 409 get_dma_buf(obj->dma_buf); 410 411 return dmabuf; 412 } 413 414 /** 415 * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers 416 * @dev: dev to export the buffer from 417 * @file_priv: drm file-private structure 418 * @handle: buffer handle to export 419 * @flags: flags like DRM_CLOEXEC 420 * @prime_fd: pointer to storage for the fd id of the create dma-buf 421 * 422 * This is the PRIME export function which must be used mandatorily by GEM 423 * drivers to ensure correct lifetime management of the underlying GEM object. 424 * The actual exporting from GEM object to a dma-buf is done through the 425 * &drm_gem_object_funcs.export callback. 426 */ 427 int drm_gem_prime_handle_to_fd(struct drm_device *dev, 428 struct drm_file *file_priv, uint32_t handle, 429 uint32_t flags, 430 int *prime_fd) 431 { 432 struct drm_gem_object *obj; 433 int ret = 0; 434 struct dma_buf *dmabuf; 435 436 mutex_lock(&file_priv->prime.lock); 437 obj = drm_gem_object_lookup(file_priv, handle); 438 if (!obj) { 439 ret = -ENOENT; 440 goto out_unlock; 441 } 442 443 dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle); 444 if (dmabuf) { 445 get_dma_buf(dmabuf); 446 goto out_have_handle; 447 } 448 449 mutex_lock(&dev->object_name_lock); 450 #ifdef notyet 451 /* re-export the original imported object */ 452 if (obj->import_attach) { 453 dmabuf = obj->import_attach->dmabuf; 454 get_dma_buf(dmabuf); 455 goto out_have_obj; 456 } 457 #endif 458 459 if (obj->dma_buf) { 460 get_dma_buf(obj->dma_buf); 461 dmabuf = obj->dma_buf; 462 goto out_have_obj; 463 } 464 465 dmabuf = export_and_register_object(dev, obj, flags); 466 if (IS_ERR(dmabuf)) { 467 /* normally the created dma-buf takes ownership of the ref, 468 * but if that fails then drop the ref 469 */ 470 ret = PTR_ERR(dmabuf); 471 mutex_unlock(&dev->object_name_lock); 472 goto out; 473 } 474 475 out_have_obj: 476 /* 477 * If we've exported this buffer then cheat and add it to the import list 478 * so we get the correct handle back. We must do this under the 479 * protection of dev->object_name_lock to ensure that a racing gem close 480 * ioctl doesn't miss to remove this buffer handle from the cache. 481 */ 482 ret = drm_prime_add_buf_handle(&file_priv->prime, 483 dmabuf, handle); 484 mutex_unlock(&dev->object_name_lock); 485 if (ret) 486 goto fail_put_dmabuf; 487 488 out_have_handle: 489 ret = dma_buf_fd(dmabuf, flags); 490 /* 491 * We must _not_ remove the buffer from the handle cache since the newly 492 * created dma buf is already linked in the global obj->dma_buf pointer, 493 * and that is invariant as long as a userspace gem handle exists. 494 * Closing the handle will clean out the cache anyway, so we don't leak. 495 */ 496 if (ret < 0) { 497 goto fail_put_dmabuf; 498 } else { 499 *prime_fd = ret; 500 ret = 0; 501 } 502 503 goto out; 504 505 fail_put_dmabuf: 506 dma_buf_put(dmabuf); 507 out: 508 drm_gem_object_put(obj); 509 out_unlock: 510 mutex_unlock(&file_priv->prime.lock); 511 512 return ret; 513 } 514 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd); 515 516 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data, 517 struct drm_file *file_priv) 518 { 519 struct drm_prime_handle *args = data; 520 521 /* check flags are valid */ 522 if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR)) 523 return -EINVAL; 524 525 if (dev->driver->prime_handle_to_fd) { 526 return dev->driver->prime_handle_to_fd(dev, file_priv, 527 args->handle, args->flags, 528 &args->fd); 529 } 530 return drm_gem_prime_handle_to_fd(dev, file_priv, args->handle, 531 args->flags, &args->fd); 532 } 533 534 /** 535 * DOC: PRIME Helpers 536 * 537 * Drivers can implement &drm_gem_object_funcs.export and 538 * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper 539 * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions 540 * implement dma-buf support in terms of some lower-level helpers, which are 541 * again exported for drivers to use individually: 542 * 543 * Exporting buffers 544 * ~~~~~~~~~~~~~~~~~ 545 * 546 * Optional pinning of buffers is handled at dma-buf attach and detach time in 547 * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is 548 * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on 549 * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is 550 * unimplemented, exports into another device are rejected. 551 * 552 * For kernel-internal access there's drm_gem_dmabuf_vmap() and 553 * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by 554 * drm_gem_dmabuf_mmap(). 555 * 556 * Note that these export helpers can only be used if the underlying backing 557 * storage is fully coherent and either permanently pinned, or it is safe to pin 558 * it indefinitely. 559 * 560 * FIXME: The underlying helper functions are named rather inconsistently. 561 * 562 * Importing buffers 563 * ~~~~~~~~~~~~~~~~~ 564 * 565 * Importing dma-bufs using drm_gem_prime_import() relies on 566 * &drm_driver.gem_prime_import_sg_table. 567 * 568 * Note that similarly to the export helpers this permanently pins the 569 * underlying backing storage. Which is ok for scanout, but is not the best 570 * option for sharing lots of buffers for rendering. 571 */ 572 573 /** 574 * drm_gem_map_attach - dma_buf attach implementation for GEM 575 * @dma_buf: buffer to attach device to 576 * @attach: buffer attachment data 577 * 578 * Calls &drm_gem_object_funcs.pin for device specific handling. This can be 579 * used as the &dma_buf_ops.attach callback. Must be used together with 580 * drm_gem_map_detach(). 581 * 582 * Returns 0 on success, negative error code on failure. 583 */ 584 int drm_gem_map_attach(struct dma_buf *dma_buf, 585 struct dma_buf_attachment *attach) 586 { 587 struct drm_gem_object *obj = dma_buf->priv; 588 589 if (!obj->funcs->get_sg_table) 590 return -ENOSYS; 591 592 return drm_gem_pin(obj); 593 } 594 EXPORT_SYMBOL(drm_gem_map_attach); 595 596 /** 597 * drm_gem_map_detach - dma_buf detach implementation for GEM 598 * @dma_buf: buffer to detach from 599 * @attach: attachment to be detached 600 * 601 * Calls &drm_gem_object_funcs.pin for device specific handling. Cleans up 602 * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the 603 * &dma_buf_ops.detach callback. 604 */ 605 void drm_gem_map_detach(struct dma_buf *dma_buf, 606 struct dma_buf_attachment *attach) 607 { 608 struct drm_gem_object *obj = dma_buf->priv; 609 610 drm_gem_unpin(obj); 611 } 612 EXPORT_SYMBOL(drm_gem_map_detach); 613 614 #ifdef notyet 615 616 /** 617 * drm_gem_map_dma_buf - map_dma_buf implementation for GEM 618 * @attach: attachment whose scatterlist is to be returned 619 * @dir: direction of DMA transfer 620 * 621 * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This 622 * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together 623 * with drm_gem_unmap_dma_buf(). 624 * 625 * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR 626 * on error. May return -EINTR if it is interrupted by a signal. 627 */ 628 struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach, 629 enum dma_data_direction dir) 630 { 631 struct drm_gem_object *obj = attach->dmabuf->priv; 632 struct sg_table *sgt; 633 int ret; 634 635 if (WARN_ON(dir == DMA_NONE)) 636 return ERR_PTR(-EINVAL); 637 638 if (WARN_ON(!obj->funcs->get_sg_table)) 639 return ERR_PTR(-ENOSYS); 640 641 sgt = obj->funcs->get_sg_table(obj); 642 if (IS_ERR(sgt)) 643 return sgt; 644 645 ret = dma_map_sgtable(attach->dev, sgt, dir, 646 DMA_ATTR_SKIP_CPU_SYNC); 647 if (ret) { 648 sg_free_table(sgt); 649 kfree(sgt); 650 sgt = ERR_PTR(ret); 651 } 652 653 return sgt; 654 } 655 EXPORT_SYMBOL(drm_gem_map_dma_buf); 656 657 /** 658 * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM 659 * @attach: attachment to unmap buffer from 660 * @sgt: scatterlist info of the buffer to unmap 661 * @dir: direction of DMA transfer 662 * 663 * This can be used as the &dma_buf_ops.unmap_dma_buf callback. 664 */ 665 void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach, 666 struct sg_table *sgt, 667 enum dma_data_direction dir) 668 { 669 if (!sgt) 670 return; 671 672 dma_unmap_sgtable(attach->dev, sgt, dir, DMA_ATTR_SKIP_CPU_SYNC); 673 sg_free_table(sgt); 674 kfree(sgt); 675 } 676 EXPORT_SYMBOL(drm_gem_unmap_dma_buf); 677 678 #endif /* notyet */ 679 680 /** 681 * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM 682 * @dma_buf: buffer to be mapped 683 * @map: the virtual address of the buffer 684 * 685 * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap 686 * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling. 687 * The kernel virtual address is returned in map. 688 * 689 * Returns 0 on success or a negative errno code otherwise. 690 */ 691 int drm_gem_dmabuf_vmap(struct dma_buf *dma_buf, struct iosys_map *map) 692 { 693 struct drm_gem_object *obj = dma_buf->priv; 694 695 return drm_gem_vmap(obj, map); 696 } 697 EXPORT_SYMBOL(drm_gem_dmabuf_vmap); 698 699 /** 700 * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM 701 * @dma_buf: buffer to be unmapped 702 * @map: the virtual address of the buffer 703 * 704 * Releases a kernel virtual mapping. This can be used as the 705 * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling. 706 */ 707 void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, struct iosys_map *map) 708 { 709 struct drm_gem_object *obj = dma_buf->priv; 710 711 drm_gem_vunmap(obj, map); 712 } 713 EXPORT_SYMBOL(drm_gem_dmabuf_vunmap); 714 715 #ifdef __linux__ 716 /** 717 * drm_gem_prime_mmap - PRIME mmap function for GEM drivers 718 * @obj: GEM object 719 * @vma: Virtual address range 720 * 721 * This function sets up a userspace mapping for PRIME exported buffers using 722 * the same codepath that is used for regular GEM buffer mapping on the DRM fd. 723 * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is 724 * called to set up the mapping. 725 */ 726 int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 727 { 728 struct drm_file *priv; 729 struct file *fil; 730 int ret; 731 732 /* Add the fake offset */ 733 vma->vm_pgoff += drm_vma_node_start(&obj->vma_node); 734 735 if (obj->funcs && obj->funcs->mmap) { 736 vma->vm_ops = obj->funcs->vm_ops; 737 738 drm_gem_object_get(obj); 739 ret = obj->funcs->mmap(obj, vma); 740 if (ret) { 741 drm_gem_object_put(obj); 742 return ret; 743 } 744 vma->vm_private_data = obj; 745 return 0; 746 } 747 748 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 749 fil = kzalloc(sizeof(*fil), GFP_KERNEL); 750 if (!priv || !fil) { 751 ret = -ENOMEM; 752 goto out; 753 } 754 755 /* Used by drm_gem_mmap() to lookup the GEM object */ 756 priv->minor = obj->dev->primary; 757 fil->private_data = priv; 758 759 ret = drm_vma_node_allow(&obj->vma_node, priv); 760 if (ret) 761 goto out; 762 763 ret = obj->dev->driver->fops->mmap(fil, vma); 764 765 drm_vma_node_revoke(&obj->vma_node, priv); 766 out: 767 kfree(priv); 768 kfree(fil); 769 770 return ret; 771 } 772 EXPORT_SYMBOL(drm_gem_prime_mmap); 773 #else 774 struct uvm_object * 775 drm_gem_prime_mmap(struct file *filp, vm_prot_t accessprot, voff_t off, 776 vsize_t size) 777 { 778 STUB(); 779 return NULL; 780 } 781 #endif 782 783 #ifdef notyet 784 785 /** 786 * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM 787 * @dma_buf: buffer to be mapped 788 * @vma: virtual address range 789 * 790 * Provides memory mapping for the buffer. This can be used as the 791 * &dma_buf_ops.mmap callback. It just forwards to drm_gem_prime_mmap(). 792 * 793 * Returns 0 on success or a negative error code on failure. 794 */ 795 int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) 796 { 797 struct drm_gem_object *obj = dma_buf->priv; 798 799 return drm_gem_prime_mmap(obj, vma); 800 } 801 EXPORT_SYMBOL(drm_gem_dmabuf_mmap); 802 803 #endif /* notyet */ 804 805 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = { 806 #ifdef notyet 807 .cache_sgt_mapping = true, 808 .attach = drm_gem_map_attach, 809 .detach = drm_gem_map_detach, 810 .map_dma_buf = drm_gem_map_dma_buf, 811 .unmap_dma_buf = drm_gem_unmap_dma_buf, 812 #endif 813 .release = drm_gem_dmabuf_release, 814 #ifdef notyet 815 .mmap = drm_gem_dmabuf_mmap, 816 .vmap = drm_gem_dmabuf_vmap, 817 .vunmap = drm_gem_dmabuf_vunmap, 818 #endif 819 }; 820 821 /** 822 * drm_prime_pages_to_sg - converts a page array into an sg list 823 * @dev: DRM device 824 * @pages: pointer to the array of page pointers to convert 825 * @nr_pages: length of the page vector 826 * 827 * This helper creates an sg table object from a set of pages 828 * the driver is responsible for mapping the pages into the 829 * importers address space for use with dma_buf itself. 830 * 831 * This is useful for implementing &drm_gem_object_funcs.get_sg_table. 832 */ 833 struct sg_table *drm_prime_pages_to_sg(struct drm_device *dev, 834 struct vm_page **pages, unsigned int nr_pages) 835 { 836 STUB(); 837 return NULL; 838 #ifdef notyet 839 struct sg_table *sg; 840 size_t max_segment = 0; 841 int err; 842 843 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL); 844 if (!sg) 845 return ERR_PTR(-ENOMEM); 846 847 if (dev) 848 max_segment = dma_max_mapping_size(dev->dev); 849 if (max_segment == 0) 850 max_segment = UINT_MAX; 851 err = sg_alloc_table_from_pages_segment(sg, pages, nr_pages, 0, 852 nr_pages << PAGE_SHIFT, 853 max_segment, GFP_KERNEL); 854 if (err) { 855 kfree(sg); 856 sg = ERR_PTR(err); 857 } 858 return sg; 859 #endif 860 } 861 EXPORT_SYMBOL(drm_prime_pages_to_sg); 862 863 /** 864 * drm_prime_get_contiguous_size - returns the contiguous size of the buffer 865 * @sgt: sg_table describing the buffer to check 866 * 867 * This helper calculates the contiguous size in the DMA address space 868 * of the buffer described by the provided sg_table. 869 * 870 * This is useful for implementing 871 * &drm_gem_object_funcs.gem_prime_import_sg_table. 872 */ 873 unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt) 874 { 875 STUB(); 876 return 0; 877 #ifdef notyet 878 dma_addr_t expected = sg_dma_address(sgt->sgl); 879 struct scatterlist *sg; 880 unsigned long size = 0; 881 int i; 882 883 for_each_sgtable_dma_sg(sgt, sg, i) { 884 unsigned int len = sg_dma_len(sg); 885 886 if (!len) 887 break; 888 if (sg_dma_address(sg) != expected) 889 break; 890 expected += len; 891 size += len; 892 } 893 return size; 894 #endif 895 } 896 EXPORT_SYMBOL(drm_prime_get_contiguous_size); 897 898 /** 899 * drm_gem_prime_export - helper library implementation of the export callback 900 * @obj: GEM object to export 901 * @flags: flags like DRM_CLOEXEC and DRM_RDWR 902 * 903 * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers 904 * using the PRIME helpers. It is used as the default in 905 * drm_gem_prime_handle_to_fd(). 906 */ 907 struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj, 908 int flags) 909 { 910 struct drm_device *dev = obj->dev; 911 struct dma_buf_export_info exp_info = { 912 #ifdef __linux__ 913 .exp_name = KBUILD_MODNAME, /* white lie for debug */ 914 .owner = dev->driver->fops->owner, 915 #endif 916 .ops = &drm_gem_prime_dmabuf_ops, 917 .size = obj->size, 918 .flags = flags, 919 .priv = obj, 920 .resv = obj->resv, 921 }; 922 923 return drm_gem_dmabuf_export(dev, &exp_info); 924 } 925 EXPORT_SYMBOL(drm_gem_prime_export); 926 927 /** 928 * drm_gem_prime_import_dev - core implementation of the import callback 929 * @dev: drm_device to import into 930 * @dma_buf: dma-buf object to import 931 * @attach_dev: struct device to dma_buf attach 932 * 933 * This is the core of drm_gem_prime_import(). It's designed to be called by 934 * drivers who want to use a different device structure than &drm_device.dev for 935 * attaching via dma_buf. This function calls 936 * &drm_driver.gem_prime_import_sg_table internally. 937 * 938 * Drivers must arrange to call drm_prime_gem_destroy() from their 939 * &drm_gem_object_funcs.free hook when using this function. 940 */ 941 struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev, 942 struct dma_buf *dma_buf, 943 struct device *attach_dev) 944 { 945 struct dma_buf_attachment *attach; 946 #ifdef notyet 947 struct sg_table *sgt; 948 #endif 949 struct drm_gem_object *obj; 950 int ret; 951 952 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) { 953 obj = dma_buf->priv; 954 if (obj->dev == dev) { 955 /* 956 * Importing dmabuf exported from our own gem increases 957 * refcount on gem itself instead of f_count of dmabuf. 958 */ 959 drm_gem_object_get(obj); 960 return obj; 961 } 962 } 963 964 #ifdef notyet 965 if (!dev->driver->gem_prime_import_sg_table) 966 return ERR_PTR(-EINVAL); 967 #endif 968 969 attach = dma_buf_attach(dma_buf, attach_dev); 970 if (IS_ERR(attach)) 971 return ERR_CAST(attach); 972 973 #ifdef notyet 974 get_dma_buf(dma_buf); 975 976 sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL); 977 if (IS_ERR(sgt)) { 978 ret = PTR_ERR(sgt); 979 goto fail_detach; 980 } 981 982 obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt); 983 if (IS_ERR(obj)) { 984 ret = PTR_ERR(obj); 985 goto fail_unmap; 986 } 987 988 obj->import_attach = attach; 989 obj->resv = dma_buf->resv; 990 991 return obj; 992 993 fail_unmap: 994 dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL); 995 fail_detach: 996 dma_buf_detach(dma_buf, attach); 997 dma_buf_put(dma_buf); 998 999 return ERR_PTR(ret); 1000 #else 1001 ret = 0; 1002 panic(__func__); 1003 #endif 1004 } 1005 EXPORT_SYMBOL(drm_gem_prime_import_dev); 1006 1007 /** 1008 * drm_gem_prime_import - helper library implementation of the import callback 1009 * @dev: drm_device to import into 1010 * @dma_buf: dma-buf object to import 1011 * 1012 * This is the implementation of the gem_prime_import functions for GEM drivers 1013 * using the PRIME helpers. Drivers can use this as their 1014 * &drm_driver.gem_prime_import implementation. It is used as the default 1015 * implementation in drm_gem_prime_fd_to_handle(). 1016 * 1017 * Drivers must arrange to call drm_prime_gem_destroy() from their 1018 * &drm_gem_object_funcs.free hook when using this function. 1019 */ 1020 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev, 1021 struct dma_buf *dma_buf) 1022 { 1023 return drm_gem_prime_import_dev(dev, dma_buf, dev->dev); 1024 } 1025 EXPORT_SYMBOL(drm_gem_prime_import); 1026 1027 /** 1028 * drm_prime_sg_to_page_array - convert an sg table into a page array 1029 * @sgt: scatter-gather table to convert 1030 * @pages: array of page pointers to store the pages in 1031 * @max_entries: size of the passed-in array 1032 * 1033 * Exports an sg table into an array of pages. 1034 * 1035 * This function is deprecated and strongly discouraged to be used. 1036 * The page array is only useful for page faults and those can corrupt fields 1037 * in the struct page if they are not handled by the exporting driver. 1038 */ 1039 int __deprecated drm_prime_sg_to_page_array(struct sg_table *sgt, 1040 struct vm_page **pages, 1041 int max_entries) 1042 { 1043 STUB(); 1044 return -ENOSYS; 1045 #ifdef notyet 1046 struct sg_page_iter page_iter; 1047 struct vm_page **p = pages; 1048 1049 for_each_sgtable_page(sgt, &page_iter, 0) { 1050 if (WARN_ON(p - pages >= max_entries)) 1051 return -1; 1052 *p++ = sg_page_iter_page(&page_iter); 1053 } 1054 return 0; 1055 #endif 1056 } 1057 EXPORT_SYMBOL(drm_prime_sg_to_page_array); 1058 1059 /** 1060 * drm_prime_sg_to_dma_addr_array - convert an sg table into a dma addr array 1061 * @sgt: scatter-gather table to convert 1062 * @addrs: array to store the dma bus address of each page 1063 * @max_entries: size of both the passed-in arrays 1064 * 1065 * Exports an sg table into an array of addresses. 1066 * 1067 * Drivers should use this in their &drm_driver.gem_prime_import_sg_table 1068 * implementation. 1069 */ 1070 int drm_prime_sg_to_dma_addr_array(struct sg_table *sgt, dma_addr_t *addrs, 1071 int max_entries) 1072 { 1073 STUB(); 1074 return -ENOSYS; 1075 #ifdef notyet 1076 struct sg_dma_page_iter dma_iter; 1077 dma_addr_t *a = addrs; 1078 1079 for_each_sgtable_dma_page(sgt, &dma_iter, 0) { 1080 if (WARN_ON(a - addrs >= max_entries)) 1081 return -1; 1082 *a++ = sg_page_iter_dma_address(&dma_iter); 1083 } 1084 return 0; 1085 #endif 1086 } 1087 EXPORT_SYMBOL(drm_prime_sg_to_dma_addr_array); 1088 1089 /** 1090 * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object 1091 * @obj: GEM object which was created from a dma-buf 1092 * @sg: the sg-table which was pinned at import time 1093 * 1094 * This is the cleanup functions which GEM drivers need to call when they use 1095 * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs. 1096 */ 1097 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg) 1098 { 1099 STUB(); 1100 #ifdef notyet 1101 struct dma_buf_attachment *attach; 1102 struct dma_buf *dma_buf; 1103 1104 attach = obj->import_attach; 1105 if (sg) 1106 dma_buf_unmap_attachment_unlocked(attach, sg, DMA_BIDIRECTIONAL); 1107 dma_buf = attach->dmabuf; 1108 dma_buf_detach(attach->dmabuf, attach); 1109 /* remove the reference */ 1110 dma_buf_put(dma_buf); 1111 #endif 1112 } 1113 EXPORT_SYMBOL(drm_prime_gem_destroy); 1114