1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drm gem DMA helper functions 4 * 5 * Copyright (C) 2012 Sascha Hauer, Pengutronix 6 * 7 * Based on Samsung Exynos code 8 * 9 * Copyright (c) 2011 Samsung Electronics Co., Ltd. 10 */ 11 12 #include <linux/dma-buf.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/export.h> 15 #include <linux/mm.h> 16 #include <linux/module.h> 17 #include <linux/mutex.h> 18 #include <linux/slab.h> 19 20 #include <drm/drm.h> 21 #include <drm/drm_device.h> 22 #include <drm/drm_drv.h> 23 #include <drm/drm_dumb_buffers.h> 24 #include <drm/drm_gem_dma_helper.h> 25 #include <drm/drm_vma_manager.h> 26 27 /** 28 * DOC: dma helpers 29 * 30 * The DRM GEM/DMA helpers are a means to provide buffer objects that are 31 * presented to the device as a contiguous chunk of memory. This is useful 32 * for devices that do not support scatter-gather DMA (either directly or 33 * by using an intimately attached IOMMU). 34 * 35 * For devices that access the memory bus through an (external) IOMMU then 36 * the buffer objects are allocated using a traditional page-based 37 * allocator and may be scattered through physical memory. However they 38 * are contiguous in the IOVA space so appear contiguous to devices using 39 * them. 40 * 41 * For other devices then the helpers rely on CMA to provide buffer 42 * objects that are physically contiguous in memory. 43 * 44 * For GEM callback helpers in struct &drm_gem_object functions, see likewise 45 * named functions with an _object_ infix (e.g., drm_gem_dma_object_vmap() wraps 46 * drm_gem_dma_vmap()). These helpers perform the necessary type conversion. 47 */ 48 49 static const struct drm_gem_object_funcs drm_gem_dma_default_funcs = { 50 .free = drm_gem_dma_object_free, 51 .print_info = drm_gem_dma_object_print_info, 52 .get_sg_table = drm_gem_dma_object_get_sg_table, 53 .vmap = drm_gem_dma_object_vmap, 54 .mmap = drm_gem_dma_object_mmap, 55 .vm_ops = &drm_gem_dma_vm_ops, 56 }; 57 58 /** 59 * __drm_gem_dma_create - Create a GEM DMA object without allocating memory 60 * @drm: DRM device 61 * @size: size of the object to allocate 62 * @private: true if used for internal purposes 63 * 64 * This function creates and initializes a GEM DMA object of the given size, 65 * but doesn't allocate any memory to back the object. 66 * 67 * Returns: 68 * A struct drm_gem_dma_object * on success or an ERR_PTR()-encoded negative 69 * error code on failure. 70 */ 71 static struct drm_gem_dma_object * 72 __drm_gem_dma_create(struct drm_device *drm, size_t size, bool private) 73 { 74 struct drm_gem_dma_object *dma_obj; 75 struct drm_gem_object *gem_obj; 76 int ret = 0; 77 78 if (drm->driver->gem_create_object) { 79 gem_obj = drm->driver->gem_create_object(drm, size); 80 if (IS_ERR(gem_obj)) 81 return ERR_CAST(gem_obj); 82 dma_obj = to_drm_gem_dma_obj(gem_obj); 83 } else { 84 dma_obj = kzalloc(sizeof(*dma_obj), GFP_KERNEL); 85 if (!dma_obj) 86 return ERR_PTR(-ENOMEM); 87 gem_obj = &dma_obj->base; 88 } 89 90 if (!gem_obj->funcs) 91 gem_obj->funcs = &drm_gem_dma_default_funcs; 92 93 if (private) { 94 drm_gem_private_object_init(drm, gem_obj, size); 95 96 /* Always use writecombine for dma-buf mappings */ 97 dma_obj->map_noncoherent = false; 98 } else { 99 ret = drm_gem_object_init(drm, gem_obj, size); 100 } 101 if (ret) 102 goto error; 103 104 ret = drm_gem_create_mmap_offset(gem_obj); 105 if (ret) { 106 drm_gem_object_release(gem_obj); 107 goto error; 108 } 109 110 return dma_obj; 111 112 error: 113 kfree(dma_obj); 114 return ERR_PTR(ret); 115 } 116 117 /** 118 * drm_gem_dma_create - allocate an object with the given size 119 * @drm: DRM device 120 * @size: size of the object to allocate 121 * 122 * This function creates a DMA GEM object and allocates memory as backing store. 123 * The allocated memory will occupy a contiguous chunk of bus address space. 124 * 125 * For devices that are directly connected to the memory bus then the allocated 126 * memory will be physically contiguous. For devices that access through an 127 * IOMMU, then the allocated memory is not expected to be physically contiguous 128 * because having contiguous IOVAs is sufficient to meet a devices DMA 129 * requirements. 130 * 131 * Returns: 132 * A struct drm_gem_dma_object * on success or an ERR_PTR()-encoded negative 133 * error code on failure. 134 */ 135 struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm, 136 size_t size) 137 { 138 struct drm_gem_dma_object *dma_obj; 139 int ret; 140 141 size = round_up(size, PAGE_SIZE); 142 143 dma_obj = __drm_gem_dma_create(drm, size, false); 144 if (IS_ERR(dma_obj)) 145 return dma_obj; 146 147 if (dma_obj->map_noncoherent) { 148 dma_obj->vaddr = dma_alloc_noncoherent(drm->dev, size, 149 &dma_obj->dma_addr, 150 DMA_TO_DEVICE, 151 GFP_KERNEL | __GFP_NOWARN); 152 } else { 153 dma_obj->vaddr = dma_alloc_wc(drm->dev, size, 154 &dma_obj->dma_addr, 155 GFP_KERNEL | __GFP_NOWARN); 156 } 157 if (!dma_obj->vaddr) { 158 drm_dbg(drm, "failed to allocate buffer with size %zu\n", 159 size); 160 ret = -ENOMEM; 161 goto error; 162 } 163 164 return dma_obj; 165 166 error: 167 drm_gem_object_put(&dma_obj->base); 168 return ERR_PTR(ret); 169 } 170 EXPORT_SYMBOL_GPL(drm_gem_dma_create); 171 172 /** 173 * drm_gem_dma_create_with_handle - allocate an object with the given size and 174 * return a GEM handle to it 175 * @file_priv: DRM file-private structure to register the handle for 176 * @drm: DRM device 177 * @size: size of the object to allocate 178 * @handle: return location for the GEM handle 179 * 180 * This function creates a DMA GEM object, allocating a chunk of memory as 181 * backing store. The GEM object is then added to the list of object associated 182 * with the given file and a handle to it is returned. 183 * 184 * The allocated memory will occupy a contiguous chunk of bus address space. 185 * See drm_gem_dma_create() for more details. 186 * 187 * Returns: 188 * A struct drm_gem_dma_object * on success or an ERR_PTR()-encoded negative 189 * error code on failure. 190 */ 191 static struct drm_gem_dma_object * 192 drm_gem_dma_create_with_handle(struct drm_file *file_priv, 193 struct drm_device *drm, size_t size, 194 uint32_t *handle) 195 { 196 struct drm_gem_dma_object *dma_obj; 197 struct drm_gem_object *gem_obj; 198 int ret; 199 200 dma_obj = drm_gem_dma_create(drm, size); 201 if (IS_ERR(dma_obj)) 202 return dma_obj; 203 204 gem_obj = &dma_obj->base; 205 206 /* 207 * allocate a id of idr table where the obj is registered 208 * and handle has the id what user can see. 209 */ 210 ret = drm_gem_handle_create(file_priv, gem_obj, handle); 211 /* drop reference from allocate - handle holds it now. */ 212 drm_gem_object_put(gem_obj); 213 if (ret) 214 return ERR_PTR(ret); 215 216 return dma_obj; 217 } 218 219 /** 220 * drm_gem_dma_free - free resources associated with a DMA GEM object 221 * @dma_obj: DMA GEM object to free 222 * 223 * This function frees the backing memory of the DMA GEM object, cleans up the 224 * GEM object state and frees the memory used to store the object itself. 225 * If the buffer is imported and the virtual address is set, it is released. 226 */ 227 void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj) 228 { 229 struct drm_gem_object *gem_obj = &dma_obj->base; 230 struct iosys_map map = IOSYS_MAP_INIT_VADDR(dma_obj->vaddr); 231 232 if (drm_gem_is_imported(gem_obj)) { 233 if (dma_obj->vaddr) 234 dma_buf_vunmap_unlocked(gem_obj->import_attach->dmabuf, &map); 235 drm_prime_gem_destroy(gem_obj, dma_obj->sgt); 236 } else if (dma_obj->vaddr) { 237 if (dma_obj->map_noncoherent) 238 dma_free_noncoherent(gem_obj->dev->dev, dma_obj->base.size, 239 dma_obj->vaddr, dma_obj->dma_addr, 240 DMA_TO_DEVICE); 241 else 242 dma_free_wc(gem_obj->dev->dev, dma_obj->base.size, 243 dma_obj->vaddr, dma_obj->dma_addr); 244 } 245 246 drm_gem_object_release(gem_obj); 247 248 kfree(dma_obj); 249 } 250 EXPORT_SYMBOL_GPL(drm_gem_dma_free); 251 252 /** 253 * drm_gem_dma_dumb_create_internal - create a dumb buffer object 254 * @file_priv: DRM file-private structure to create the dumb buffer for 255 * @drm: DRM device 256 * @args: IOCTL data 257 * 258 * This aligns the pitch and size arguments to the minimum required. This is 259 * an internal helper that can be wrapped by a driver to account for hardware 260 * with more specific alignment requirements. It should not be used directly 261 * as their &drm_driver.dumb_create callback. 262 * 263 * Returns: 264 * 0 on success or a negative error code on failure. 265 */ 266 int drm_gem_dma_dumb_create_internal(struct drm_file *file_priv, 267 struct drm_device *drm, 268 struct drm_mode_create_dumb *args) 269 { 270 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); 271 struct drm_gem_dma_object *dma_obj; 272 273 if (args->pitch < min_pitch) 274 args->pitch = min_pitch; 275 276 if (args->size < args->pitch * args->height) 277 args->size = args->pitch * args->height; 278 279 dma_obj = drm_gem_dma_create_with_handle(file_priv, drm, args->size, 280 &args->handle); 281 return PTR_ERR_OR_ZERO(dma_obj); 282 } 283 EXPORT_SYMBOL_GPL(drm_gem_dma_dumb_create_internal); 284 285 /** 286 * drm_gem_dma_dumb_create - create a dumb buffer object 287 * @file_priv: DRM file-private structure to create the dumb buffer for 288 * @drm: DRM device 289 * @args: IOCTL data 290 * 291 * This function computes the pitch of the dumb buffer and rounds it up to an 292 * integer number of bytes per pixel. Drivers for hardware that doesn't have 293 * any additional restrictions on the pitch can directly use this function as 294 * their &drm_driver.dumb_create callback. 295 * 296 * For hardware with additional restrictions, drivers can adjust the fields 297 * set up by userspace and pass the IOCTL data along to the 298 * drm_gem_dma_dumb_create_internal() function. 299 * 300 * Returns: 301 * 0 on success or a negative error code on failure. 302 */ 303 int drm_gem_dma_dumb_create(struct drm_file *file_priv, 304 struct drm_device *drm, 305 struct drm_mode_create_dumb *args) 306 { 307 struct drm_gem_dma_object *dma_obj; 308 int ret; 309 310 ret = drm_mode_size_dumb(drm, args, SZ_8, 0); 311 if (ret) 312 return ret; 313 314 dma_obj = drm_gem_dma_create_with_handle(file_priv, drm, args->size, 315 &args->handle); 316 return PTR_ERR_OR_ZERO(dma_obj); 317 } 318 EXPORT_SYMBOL_GPL(drm_gem_dma_dumb_create); 319 320 const struct vm_operations_struct drm_gem_dma_vm_ops = { 321 .open = drm_gem_vm_open, 322 .close = drm_gem_vm_close, 323 }; 324 EXPORT_SYMBOL_GPL(drm_gem_dma_vm_ops); 325 326 #ifndef CONFIG_MMU 327 /** 328 * drm_gem_dma_get_unmapped_area - propose address for mapping in noMMU cases 329 * @filp: file object 330 * @addr: memory address 331 * @len: buffer size 332 * @pgoff: page offset 333 * @flags: memory flags 334 * 335 * This function is used in noMMU platforms to propose address mapping 336 * for a given buffer. 337 * It's intended to be used as a direct handler for the struct 338 * &file_operations.get_unmapped_area operation. 339 * 340 * Returns: 341 * mapping address on success or a negative error code on failure. 342 */ 343 unsigned long drm_gem_dma_get_unmapped_area(struct file *filp, 344 unsigned long addr, 345 unsigned long len, 346 unsigned long pgoff, 347 unsigned long flags) 348 { 349 struct drm_gem_dma_object *dma_obj; 350 struct drm_gem_object *obj = NULL; 351 struct drm_file *priv = filp->private_data; 352 struct drm_device *dev = priv->minor->dev; 353 struct drm_vma_offset_node *node; 354 355 if (drm_dev_is_unplugged(dev)) 356 return -ENODEV; 357 358 drm_vma_offset_lock_lookup(dev->vma_offset_manager); 359 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, 360 pgoff, 361 len >> PAGE_SHIFT); 362 if (likely(node)) { 363 obj = container_of(node, struct drm_gem_object, vma_node); 364 /* 365 * When the object is being freed, after it hits 0-refcnt it 366 * proceeds to tear down the object. In the process it will 367 * attempt to remove the VMA offset and so acquire this 368 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt 369 * that matches our range, we know it is in the process of being 370 * destroyed and will be freed as soon as we release the lock - 371 * so we have to check for the 0-refcnted object and treat it as 372 * invalid. 373 */ 374 if (!kref_get_unless_zero(&obj->refcount)) 375 obj = NULL; 376 } 377 378 drm_vma_offset_unlock_lookup(dev->vma_offset_manager); 379 380 if (!obj) 381 return -EINVAL; 382 383 if (!drm_vma_node_is_allowed(node, priv)) { 384 drm_gem_object_put(obj); 385 return -EACCES; 386 } 387 388 dma_obj = to_drm_gem_dma_obj(obj); 389 390 drm_gem_object_put(obj); 391 392 return dma_obj->vaddr ? (unsigned long)dma_obj->vaddr : -EINVAL; 393 } 394 EXPORT_SYMBOL_GPL(drm_gem_dma_get_unmapped_area); 395 #endif 396 397 /** 398 * drm_gem_dma_print_info() - Print &drm_gem_dma_object info for debugfs 399 * @dma_obj: DMA GEM object 400 * @p: DRM printer 401 * @indent: Tab indentation level 402 * 403 * This function prints dma_addr and vaddr for use in e.g. debugfs output. 404 */ 405 void drm_gem_dma_print_info(const struct drm_gem_dma_object *dma_obj, 406 struct drm_printer *p, unsigned int indent) 407 { 408 drm_printf_indent(p, indent, "dma_addr=%pad\n", &dma_obj->dma_addr); 409 drm_printf_indent(p, indent, "vaddr=%p\n", dma_obj->vaddr); 410 } 411 EXPORT_SYMBOL(drm_gem_dma_print_info); 412 413 /** 414 * drm_gem_dma_get_sg_table - provide a scatter/gather table of pinned 415 * pages for a DMA GEM object 416 * @dma_obj: DMA GEM object 417 * 418 * This function exports a scatter/gather table by calling the standard 419 * DMA mapping API. 420 * 421 * Returns: 422 * A pointer to the scatter/gather table of pinned pages or NULL on failure. 423 */ 424 struct sg_table *drm_gem_dma_get_sg_table(struct drm_gem_dma_object *dma_obj) 425 { 426 struct drm_gem_object *obj = &dma_obj->base; 427 struct sg_table *sgt; 428 int ret; 429 430 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); 431 if (!sgt) 432 return ERR_PTR(-ENOMEM); 433 434 ret = dma_get_sgtable(obj->dev->dev, sgt, dma_obj->vaddr, 435 dma_obj->dma_addr, obj->size); 436 if (ret < 0) 437 goto out; 438 439 return sgt; 440 441 out: 442 kfree(sgt); 443 return ERR_PTR(ret); 444 } 445 EXPORT_SYMBOL_GPL(drm_gem_dma_get_sg_table); 446 447 /** 448 * drm_gem_dma_prime_import_sg_table - produce a DMA GEM object from another 449 * driver's scatter/gather table of pinned pages 450 * @dev: device to import into 451 * @attach: DMA-BUF attachment 452 * @sgt: scatter/gather table of pinned pages 453 * 454 * This function imports a scatter/gather table exported via DMA-BUF by 455 * another driver. Imported buffers must be physically contiguous in memory 456 * (i.e. the scatter/gather table must contain a single entry). Drivers that 457 * use the DMA helpers should set this as their 458 * &drm_driver.gem_prime_import_sg_table callback. 459 * 460 * Returns: 461 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative 462 * error code on failure. 463 */ 464 struct drm_gem_object * 465 drm_gem_dma_prime_import_sg_table(struct drm_device *dev, 466 struct dma_buf_attachment *attach, 467 struct sg_table *sgt) 468 { 469 struct drm_gem_dma_object *dma_obj; 470 471 /* check if the entries in the sg_table are contiguous */ 472 if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size) 473 return ERR_PTR(-EINVAL); 474 475 /* Create a DMA GEM buffer. */ 476 dma_obj = __drm_gem_dma_create(dev, attach->dmabuf->size, true); 477 if (IS_ERR(dma_obj)) 478 return ERR_CAST(dma_obj); 479 480 dma_obj->dma_addr = sg_dma_address(sgt->sgl); 481 dma_obj->sgt = sgt; 482 483 drm_dbg_prime(dev, "dma_addr = %pad, size = %zu\n", &dma_obj->dma_addr, 484 attach->dmabuf->size); 485 486 return &dma_obj->base; 487 } 488 EXPORT_SYMBOL_GPL(drm_gem_dma_prime_import_sg_table); 489 490 /** 491 * drm_gem_dma_vmap - map a DMA GEM object into the kernel's virtual 492 * address space 493 * @dma_obj: DMA GEM object 494 * @map: Returns the kernel virtual address of the DMA GEM object's backing 495 * store. 496 * 497 * This function maps a buffer into the kernel's virtual address space. 498 * Since the DMA buffers are already mapped into the kernel virtual address 499 * space this simply returns the cached virtual address. 500 * 501 * Returns: 502 * 0 on success, or a negative error code otherwise. 503 */ 504 int drm_gem_dma_vmap(struct drm_gem_dma_object *dma_obj, 505 struct iosys_map *map) 506 { 507 iosys_map_set_vaddr(map, dma_obj->vaddr); 508 509 return 0; 510 } 511 EXPORT_SYMBOL_GPL(drm_gem_dma_vmap); 512 513 /** 514 * drm_gem_dma_mmap - memory-map an exported DMA GEM object 515 * @dma_obj: DMA GEM object 516 * @vma: VMA for the area to be mapped 517 * 518 * This function maps a buffer into a userspace process's address space. 519 * In addition to the usual GEM VMA setup it immediately faults in the entire 520 * object instead of using on-demand faulting. 521 * 522 * Returns: 523 * 0 on success or a negative error code on failure. 524 */ 525 int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *vma) 526 { 527 struct drm_gem_object *obj = &dma_obj->base; 528 int ret; 529 530 /* 531 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the 532 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map 533 * the whole buffer. 534 */ 535 vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node); 536 vm_flags_mod(vma, VM_DONTEXPAND, VM_PFNMAP); 537 538 if (dma_obj->map_noncoherent) { 539 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 540 541 ret = dma_mmap_pages(dma_obj->base.dev->dev, 542 vma, vma->vm_end - vma->vm_start, 543 virt_to_page(dma_obj->vaddr)); 544 } else { 545 ret = dma_mmap_wc(dma_obj->base.dev->dev, vma, dma_obj->vaddr, 546 dma_obj->dma_addr, 547 vma->vm_end - vma->vm_start); 548 } 549 if (ret) 550 drm_gem_vm_close(vma); 551 552 return ret; 553 } 554 EXPORT_SYMBOL_GPL(drm_gem_dma_mmap); 555 556 /** 557 * drm_gem_dma_prime_import_sg_table_vmap - PRIME import another driver's 558 * scatter/gather table and get the virtual address of the buffer 559 * @dev: DRM device 560 * @attach: DMA-BUF attachment 561 * @sgt: Scatter/gather table of pinned pages 562 * 563 * This function imports a scatter/gather table using 564 * drm_gem_dma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel 565 * virtual address. This ensures that a DMA GEM object always has its virtual 566 * address set. This address is released when the object is freed. 567 * 568 * This function can be used as the &drm_driver.gem_prime_import_sg_table 569 * callback. The &DRM_GEM_DMA_DRIVER_OPS_VMAP macro provides a shortcut to set 570 * the necessary DRM driver operations. 571 * 572 * Returns: 573 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative 574 * error code on failure. 575 */ 576 struct drm_gem_object * 577 drm_gem_dma_prime_import_sg_table_vmap(struct drm_device *dev, 578 struct dma_buf_attachment *attach, 579 struct sg_table *sgt) 580 { 581 struct drm_gem_dma_object *dma_obj; 582 struct drm_gem_object *obj; 583 struct iosys_map map; 584 int ret; 585 586 ret = dma_buf_vmap_unlocked(attach->dmabuf, &map); 587 if (ret) { 588 drm_err(dev, "Failed to vmap PRIME buffer\n"); 589 return ERR_PTR(ret); 590 } 591 592 obj = drm_gem_dma_prime_import_sg_table(dev, attach, sgt); 593 if (IS_ERR(obj)) { 594 dma_buf_vunmap_unlocked(attach->dmabuf, &map); 595 return obj; 596 } 597 598 dma_obj = to_drm_gem_dma_obj(obj); 599 dma_obj->vaddr = map.vaddr; 600 601 return obj; 602 } 603 EXPORT_SYMBOL(drm_gem_dma_prime_import_sg_table_vmap); 604 605 MODULE_DESCRIPTION("DRM DMA memory-management helpers"); 606 MODULE_IMPORT_NS("DMA_BUF"); 607 MODULE_LICENSE("GPL"); 608