1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 #include <linux/iosys-map.h> 4 #include <linux/module.h> 5 6 #include <drm/drm_debugfs.h> 7 #include <drm/drm_device.h> 8 #include <drm/drm_drv.h> 9 #include <drm/drm_file.h> 10 #include <drm/drm_framebuffer.h> 11 #include <drm/drm_gem_atomic_helper.h> 12 #include <drm/drm_gem_framebuffer_helper.h> 13 #include <drm/drm_gem_ttm_helper.h> 14 #include <drm/drm_gem_vram_helper.h> 15 #include <drm/drm_managed.h> 16 #include <drm/drm_mode.h> 17 #include <drm/drm_plane.h> 18 #include <drm/drm_prime.h> 19 #include <drm/drm_simple_kms_helper.h> 20 21 #include <drm/ttm/ttm_range_manager.h> 22 #include <drm/ttm/ttm_tt.h> 23 24 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; 25 26 /** 27 * DOC: overview 28 * 29 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM 30 * buffer object that is backed by video RAM (VRAM). It can be used for 31 * framebuffer devices with dedicated memory. 32 * 33 * The data structure &struct drm_vram_mm and its helpers implement a memory 34 * manager for simple framebuffer devices with dedicated video memory. GEM 35 * VRAM buffer objects are either placed in the video memory or remain evicted 36 * to system memory. 37 * 38 * With the GEM interface userspace applications create, manage and destroy 39 * graphics buffers, such as an on-screen framebuffer. GEM does not provide 40 * an implementation of these interfaces. It's up to the DRM driver to 41 * provide an implementation that suits the hardware. If the hardware device 42 * contains dedicated video memory, the DRM driver can use the VRAM helper 43 * library. Each active buffer object is stored in video RAM. Active 44 * buffer are used for drawing the current frame, typically something like 45 * the frame's scanout buffer or the cursor image. If there's no more space 46 * left in VRAM, inactive GEM objects can be moved to system memory. 47 * 48 * To initialize the VRAM helper library call drmm_vram_helper_init(). 49 * The function allocates and initializes an instance of &struct drm_vram_mm 50 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize 51 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize 52 * &struct file_operations; as illustrated below. 53 * 54 * .. code-block:: c 55 * 56 * struct file_operations fops ={ 57 * .owner = THIS_MODULE, 58 * DRM_VRAM_MM_FILE_OPERATION 59 * }; 60 * struct drm_driver drv = { 61 * .driver_feature = DRM_ ... , 62 * .fops = &fops, 63 * DRM_GEM_VRAM_DRIVER 64 * }; 65 * 66 * int init_drm_driver() 67 * { 68 * struct drm_device *dev; 69 * uint64_t vram_base; 70 * unsigned long vram_size; 71 * int ret; 72 * 73 * // setup device, vram base and size 74 * // ... 75 * 76 * ret = drmm_vram_helper_init(dev, vram_base, vram_size); 77 * if (ret) 78 * return ret; 79 * return 0; 80 * } 81 * 82 * This creates an instance of &struct drm_vram_mm, exports DRM userspace 83 * interfaces for GEM buffer management and initializes file operations to 84 * allow for accessing created GEM buffers. With this setup, the DRM driver 85 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects 86 * to userspace. 87 * 88 * You don't have to clean up the instance of VRAM MM. 89 * drmm_vram_helper_init() is a managed interface that installs a 90 * clean-up handler to run during the DRM device's release. 91 * 92 * For drawing or scanout operations, rsp. buffer objects have to be pinned 93 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or 94 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system 95 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards. 96 * 97 * A buffer object that is pinned in video RAM has a fixed address within that 98 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically 99 * it's used to program the hardware's scanout engine for framebuffers, set 100 * the cursor overlay's image for a mouse cursor, or use it as input to the 101 * hardware's drawing engine. 102 * 103 * To access a buffer object's memory from the DRM driver, call 104 * drm_gem_vram_vmap(). It maps the buffer into kernel address 105 * space and returns the memory address. Use drm_gem_vram_vunmap() to 106 * release the mapping. 107 */ 108 109 /* 110 * Buffer-objects helpers 111 */ 112 113 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo) 114 { 115 /* We got here via ttm_bo_put(), which means that the 116 * TTM buffer object in 'bo' has already been cleaned 117 * up; only release the GEM object. 118 */ 119 120 WARN_ON(gbo->vmap_use_count); 121 WARN_ON(iosys_map_is_set(&gbo->map)); 122 123 drm_gem_object_release(&gbo->bo.base); 124 } 125 126 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo) 127 { 128 drm_gem_vram_cleanup(gbo); 129 kfree(gbo); 130 } 131 132 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo) 133 { 134 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo); 135 136 drm_gem_vram_destroy(gbo); 137 } 138 139 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo, 140 unsigned long pl_flag) 141 { 142 u32 invariant_flags = 0; 143 unsigned int i; 144 unsigned int c = 0; 145 146 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN) 147 invariant_flags = TTM_PL_FLAG_TOPDOWN; 148 149 gbo->placement.placement = gbo->placements; 150 151 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) { 152 gbo->placements[c].mem_type = TTM_PL_VRAM; 153 gbo->placements[c++].flags = invariant_flags; 154 } 155 156 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) { 157 gbo->placements[c].mem_type = TTM_PL_SYSTEM; 158 gbo->placements[c++].flags = invariant_flags; 159 } 160 161 gbo->placement.num_placement = c; 162 163 for (i = 0; i < c; ++i) { 164 gbo->placements[i].fpfn = 0; 165 gbo->placements[i].lpfn = 0; 166 } 167 } 168 169 /** 170 * drm_gem_vram_create() - Creates a VRAM-backed GEM object 171 * @dev: the DRM device 172 * @size: the buffer size in bytes 173 * @pg_align: the buffer's alignment in multiples of the page size 174 * 175 * GEM objects are allocated by calling struct drm_driver.gem_create_object, 176 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM 177 * object functions in struct drm_driver.gem_create_object. If no functions 178 * are set, the new GEM object will use the default functions from GEM VRAM 179 * helpers. 180 * 181 * Returns: 182 * A new instance of &struct drm_gem_vram_object on success, or 183 * an ERR_PTR()-encoded error code otherwise. 184 */ 185 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev, 186 size_t size, 187 unsigned long pg_align) 188 { 189 struct drm_gem_vram_object *gbo; 190 struct drm_gem_object *gem; 191 struct drm_vram_mm *vmm = dev->vram_mm; 192 struct ttm_device *bdev; 193 int ret; 194 195 if (WARN_ONCE(!vmm, "VRAM MM not initialized")) 196 return ERR_PTR(-EINVAL); 197 198 if (dev->driver->gem_create_object) { 199 gem = dev->driver->gem_create_object(dev, size); 200 if (IS_ERR(gem)) 201 return ERR_CAST(gem); 202 gbo = drm_gem_vram_of_gem(gem); 203 } else { 204 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL); 205 if (!gbo) 206 return ERR_PTR(-ENOMEM); 207 gem = &gbo->bo.base; 208 } 209 210 if (!gem->funcs) 211 gem->funcs = &drm_gem_vram_object_funcs; 212 213 ret = drm_gem_object_init(dev, gem, size); 214 if (ret) { 215 kfree(gbo); 216 return ERR_PTR(ret); 217 } 218 219 bdev = &vmm->bdev; 220 221 gbo->bo.bdev = bdev; 222 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM); 223 224 /* 225 * A failing ttm_bo_init will call ttm_buffer_object_destroy 226 * to release gbo->bo.base and kfree gbo. 227 */ 228 ret = ttm_bo_init_validate(bdev, &gbo->bo, ttm_bo_type_device, 229 &gbo->placement, pg_align, false, NULL, NULL, 230 ttm_buffer_object_destroy); 231 if (ret) 232 return ERR_PTR(ret); 233 234 return gbo; 235 } 236 EXPORT_SYMBOL(drm_gem_vram_create); 237 238 /** 239 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object 240 * @gbo: the GEM VRAM object 241 * 242 * See ttm_bo_put() for more information. 243 */ 244 void drm_gem_vram_put(struct drm_gem_vram_object *gbo) 245 { 246 ttm_bo_put(&gbo->bo); 247 } 248 EXPORT_SYMBOL(drm_gem_vram_put); 249 250 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo) 251 { 252 /* Keep TTM behavior for now, remove when drivers are audited */ 253 if (WARN_ON_ONCE(!gbo->bo.resource || 254 gbo->bo.resource->mem_type == TTM_PL_SYSTEM)) 255 return 0; 256 257 return gbo->bo.resource->start; 258 } 259 260 /** 261 * drm_gem_vram_offset() - \ 262 Returns a GEM VRAM object's offset in video memory 263 * @gbo: the GEM VRAM object 264 * 265 * This function returns the buffer object's offset in the device's video 266 * memory. The buffer object has to be pinned to %TTM_PL_VRAM. 267 * 268 * Returns: 269 * The buffer object's offset in video memory on success, or 270 * a negative errno code otherwise. 271 */ 272 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo) 273 { 274 if (WARN_ON_ONCE(!gbo->bo.pin_count)) 275 return (s64)-ENODEV; 276 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT; 277 } 278 EXPORT_SYMBOL(drm_gem_vram_offset); 279 280 static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo, 281 unsigned long pl_flag) 282 { 283 struct ttm_operation_ctx ctx = { false, false }; 284 int ret; 285 286 if (gbo->bo.pin_count) 287 goto out; 288 289 if (pl_flag) 290 drm_gem_vram_placement(gbo, pl_flag); 291 292 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx); 293 if (ret < 0) 294 return ret; 295 296 out: 297 ttm_bo_pin(&gbo->bo); 298 299 return 0; 300 } 301 302 /** 303 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region. 304 * @gbo: the GEM VRAM object 305 * @pl_flag: a bitmask of possible memory regions 306 * 307 * Pinning a buffer object ensures that it is not evicted from 308 * a memory region. A pinned buffer object has to be unpinned before 309 * it can be pinned to another region. If the pl_flag argument is 0, 310 * the buffer is pinned at its current location (video RAM or system 311 * memory). 312 * 313 * Small buffer objects, such as cursor images, can lead to memory 314 * fragmentation if they are pinned in the middle of video RAM. This 315 * is especially a problem on devices with only a small amount of 316 * video RAM. Fragmentation can prevent the primary framebuffer from 317 * fitting in, even though there's enough memory overall. The modifier 318 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned 319 * at the high end of the memory region to avoid fragmentation. 320 * 321 * Returns: 322 * 0 on success, or 323 * a negative error code otherwise. 324 */ 325 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag) 326 { 327 int ret; 328 329 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL); 330 if (ret) 331 return ret; 332 ret = drm_gem_vram_pin_locked(gbo, pl_flag); 333 ttm_bo_unreserve(&gbo->bo); 334 335 return ret; 336 } 337 EXPORT_SYMBOL(drm_gem_vram_pin); 338 339 static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo) 340 { 341 ttm_bo_unpin(&gbo->bo); 342 } 343 344 /** 345 * drm_gem_vram_unpin() - Unpins a GEM VRAM object 346 * @gbo: the GEM VRAM object 347 * 348 * Returns: 349 * 0 on success, or 350 * a negative error code otherwise. 351 */ 352 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo) 353 { 354 int ret; 355 356 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL); 357 if (ret) 358 return ret; 359 360 drm_gem_vram_unpin_locked(gbo); 361 ttm_bo_unreserve(&gbo->bo); 362 363 return 0; 364 } 365 EXPORT_SYMBOL(drm_gem_vram_unpin); 366 367 static int drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo, 368 struct iosys_map *map) 369 { 370 int ret; 371 372 if (gbo->vmap_use_count > 0) 373 goto out; 374 375 /* 376 * VRAM helpers unmap the BO only on demand. So the previous 377 * page mapping might still be around. Only vmap if the there's 378 * no mapping present. 379 */ 380 if (iosys_map_is_null(&gbo->map)) { 381 ret = ttm_bo_vmap(&gbo->bo, &gbo->map); 382 if (ret) 383 return ret; 384 } 385 386 out: 387 ++gbo->vmap_use_count; 388 *map = gbo->map; 389 390 return 0; 391 } 392 393 static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo, 394 struct iosys_map *map) 395 { 396 struct drm_device *dev = gbo->bo.base.dev; 397 398 if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count)) 399 return; 400 401 if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map))) 402 return; /* BUG: map not mapped from this BO */ 403 404 if (--gbo->vmap_use_count > 0) 405 return; 406 407 /* 408 * Permanently mapping and unmapping buffers adds overhead from 409 * updating the page tables and creates debugging output. Therefore, 410 * we delay the actual unmap operation until the BO gets evicted 411 * from memory. See drm_gem_vram_bo_driver_move_notify(). 412 */ 413 } 414 415 /** 416 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address 417 * space 418 * @gbo: The GEM VRAM object to map 419 * @map: Returns the kernel virtual address of the VRAM GEM object's backing 420 * store. 421 * 422 * The vmap function pins a GEM VRAM object to its current location, either 423 * system or video memory, and maps its buffer into kernel address space. 424 * As pinned object cannot be relocated, you should avoid pinning objects 425 * permanently. Call drm_gem_vram_vunmap() with the returned address to 426 * unmap and unpin the GEM VRAM object. 427 * 428 * Returns: 429 * 0 on success, or a negative error code otherwise. 430 */ 431 int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map) 432 { 433 int ret; 434 435 dma_resv_assert_held(gbo->bo.base.resv); 436 437 ret = drm_gem_vram_pin_locked(gbo, 0); 438 if (ret) 439 return ret; 440 ret = drm_gem_vram_kmap_locked(gbo, map); 441 if (ret) 442 goto err_drm_gem_vram_unpin_locked; 443 444 return 0; 445 446 err_drm_gem_vram_unpin_locked: 447 drm_gem_vram_unpin_locked(gbo); 448 return ret; 449 } 450 EXPORT_SYMBOL(drm_gem_vram_vmap); 451 452 /** 453 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object 454 * @gbo: The GEM VRAM object to unmap 455 * @map: Kernel virtual address where the VRAM GEM object was mapped 456 * 457 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See 458 * the documentation for drm_gem_vram_vmap() for more information. 459 */ 460 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, 461 struct iosys_map *map) 462 { 463 dma_resv_assert_held(gbo->bo.base.resv); 464 465 drm_gem_vram_kunmap_locked(gbo, map); 466 drm_gem_vram_unpin_locked(gbo); 467 } 468 EXPORT_SYMBOL(drm_gem_vram_vunmap); 469 470 /** 471 * drm_gem_vram_fill_create_dumb() - \ 472 Helper for implementing &struct drm_driver.dumb_create 473 * @file: the DRM file 474 * @dev: the DRM device 475 * @pg_align: the buffer's alignment in multiples of the page size 476 * @pitch_align: the scanline's alignment in powers of 2 477 * @args: the arguments as provided to \ 478 &struct drm_driver.dumb_create 479 * 480 * This helper function fills &struct drm_mode_create_dumb, which is used 481 * by &struct drm_driver.dumb_create. Implementations of this interface 482 * should forwards their arguments to this helper, plus the driver-specific 483 * parameters. 484 * 485 * Returns: 486 * 0 on success, or 487 * a negative error code otherwise. 488 */ 489 int drm_gem_vram_fill_create_dumb(struct drm_file *file, 490 struct drm_device *dev, 491 unsigned long pg_align, 492 unsigned long pitch_align, 493 struct drm_mode_create_dumb *args) 494 { 495 size_t pitch, size; 496 struct drm_gem_vram_object *gbo; 497 int ret; 498 u32 handle; 499 500 pitch = args->width * DIV_ROUND_UP(args->bpp, 8); 501 if (pitch_align) { 502 if (WARN_ON_ONCE(!is_power_of_2(pitch_align))) 503 return -EINVAL; 504 pitch = ALIGN(pitch, pitch_align); 505 } 506 size = pitch * args->height; 507 508 size = roundup(size, PAGE_SIZE); 509 if (!size) 510 return -EINVAL; 511 512 gbo = drm_gem_vram_create(dev, size, pg_align); 513 if (IS_ERR(gbo)) 514 return PTR_ERR(gbo); 515 516 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle); 517 if (ret) 518 goto err_drm_gem_object_put; 519 520 drm_gem_object_put(&gbo->bo.base); 521 522 args->pitch = pitch; 523 args->size = size; 524 args->handle = handle; 525 526 return 0; 527 528 err_drm_gem_object_put: 529 drm_gem_object_put(&gbo->bo.base); 530 return ret; 531 } 532 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb); 533 534 /* 535 * Helpers for struct ttm_device_funcs 536 */ 537 538 static bool drm_is_gem_vram(struct ttm_buffer_object *bo) 539 { 540 return (bo->destroy == ttm_buffer_object_destroy); 541 } 542 543 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo, 544 struct ttm_placement *pl) 545 { 546 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM); 547 *pl = gbo->placement; 548 } 549 550 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo) 551 { 552 struct ttm_buffer_object *bo = &gbo->bo; 553 struct drm_device *dev = bo->base.dev; 554 555 if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count)) 556 return; 557 558 ttm_bo_vunmap(bo, &gbo->map); 559 iosys_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */ 560 } 561 562 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo, 563 bool evict, 564 struct ttm_operation_ctx *ctx, 565 struct ttm_resource *new_mem) 566 { 567 drm_gem_vram_bo_driver_move_notify(gbo); 568 return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem); 569 } 570 571 /* 572 * Helpers for struct drm_gem_object_funcs 573 */ 574 575 /** 576 * drm_gem_vram_object_free() - \ 577 Implements &struct drm_gem_object_funcs.free 578 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem 579 */ 580 static void drm_gem_vram_object_free(struct drm_gem_object *gem) 581 { 582 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem); 583 584 drm_gem_vram_put(gbo); 585 } 586 587 /* 588 * Helpers for dump buffers 589 */ 590 591 /** 592 * drm_gem_vram_driver_dumb_create() - \ 593 Implements &struct drm_driver.dumb_create 594 * @file: the DRM file 595 * @dev: the DRM device 596 * @args: the arguments as provided to \ 597 &struct drm_driver.dumb_create 598 * 599 * This function requires the driver to use @drm_device.vram_mm for its 600 * instance of VRAM MM. 601 * 602 * Returns: 603 * 0 on success, or 604 * a negative error code otherwise. 605 */ 606 int drm_gem_vram_driver_dumb_create(struct drm_file *file, 607 struct drm_device *dev, 608 struct drm_mode_create_dumb *args) 609 { 610 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized")) 611 return -EINVAL; 612 613 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args); 614 } 615 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create); 616 617 /* 618 * Helpers for struct drm_plane_helper_funcs 619 */ 620 621 static void __drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane, 622 struct drm_plane_state *state, 623 unsigned int num_planes) 624 { 625 struct drm_gem_object *obj; 626 struct drm_gem_vram_object *gbo; 627 struct drm_framebuffer *fb = state->fb; 628 629 while (num_planes) { 630 --num_planes; 631 obj = drm_gem_fb_get_obj(fb, num_planes); 632 if (!obj) 633 continue; 634 gbo = drm_gem_vram_of_gem(obj); 635 drm_gem_vram_unpin(gbo); 636 } 637 } 638 639 /** 640 * drm_gem_vram_plane_helper_prepare_fb() - \ 641 * Implements &struct drm_plane_helper_funcs.prepare_fb 642 * @plane: a DRM plane 643 * @new_state: the plane's new state 644 * 645 * During plane updates, this function sets the plane's fence and 646 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM. 647 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them. 648 * 649 * Returns: 650 * 0 on success, or 651 * a negative errno code otherwise. 652 */ 653 int 654 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane, 655 struct drm_plane_state *new_state) 656 { 657 struct drm_framebuffer *fb = new_state->fb; 658 struct drm_gem_vram_object *gbo; 659 struct drm_gem_object *obj; 660 unsigned int i; 661 int ret; 662 663 if (!fb) 664 return 0; 665 666 for (i = 0; i < fb->format->num_planes; ++i) { 667 obj = drm_gem_fb_get_obj(fb, i); 668 if (!obj) { 669 ret = -EINVAL; 670 goto err_drm_gem_vram_unpin; 671 } 672 gbo = drm_gem_vram_of_gem(obj); 673 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM); 674 if (ret) 675 goto err_drm_gem_vram_unpin; 676 } 677 678 ret = drm_gem_plane_helper_prepare_fb(plane, new_state); 679 if (ret) 680 goto err_drm_gem_vram_unpin; 681 682 return 0; 683 684 err_drm_gem_vram_unpin: 685 __drm_gem_vram_plane_helper_cleanup_fb(plane, new_state, i); 686 return ret; 687 } 688 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb); 689 690 /** 691 * drm_gem_vram_plane_helper_cleanup_fb() - \ 692 * Implements &struct drm_plane_helper_funcs.cleanup_fb 693 * @plane: a DRM plane 694 * @old_state: the plane's old state 695 * 696 * During plane updates, this function unpins the GEM VRAM 697 * objects of the plane's old framebuffer from VRAM. Complements 698 * drm_gem_vram_plane_helper_prepare_fb(). 699 */ 700 void 701 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane, 702 struct drm_plane_state *old_state) 703 { 704 struct drm_framebuffer *fb = old_state->fb; 705 706 if (!fb) 707 return; 708 709 __drm_gem_vram_plane_helper_cleanup_fb(plane, old_state, fb->format->num_planes); 710 } 711 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb); 712 713 /* 714 * Helpers for struct drm_simple_display_pipe_funcs 715 */ 716 717 /** 718 * drm_gem_vram_simple_display_pipe_prepare_fb() - \ 719 * Implements &struct drm_simple_display_pipe_funcs.prepare_fb 720 * @pipe: a simple display pipe 721 * @new_state: the plane's new state 722 * 723 * During plane updates, this function pins the GEM VRAM 724 * objects of the plane's new framebuffer to VRAM. Call 725 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them. 726 * 727 * Returns: 728 * 0 on success, or 729 * a negative errno code otherwise. 730 */ 731 int drm_gem_vram_simple_display_pipe_prepare_fb( 732 struct drm_simple_display_pipe *pipe, 733 struct drm_plane_state *new_state) 734 { 735 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state); 736 } 737 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb); 738 739 /** 740 * drm_gem_vram_simple_display_pipe_cleanup_fb() - \ 741 * Implements &struct drm_simple_display_pipe_funcs.cleanup_fb 742 * @pipe: a simple display pipe 743 * @old_state: the plane's old state 744 * 745 * During plane updates, this function unpins the GEM VRAM 746 * objects of the plane's old framebuffer from VRAM. Complements 747 * drm_gem_vram_simple_display_pipe_prepare_fb(). 748 */ 749 void drm_gem_vram_simple_display_pipe_cleanup_fb( 750 struct drm_simple_display_pipe *pipe, 751 struct drm_plane_state *old_state) 752 { 753 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state); 754 } 755 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb); 756 757 /* 758 * PRIME helpers 759 */ 760 761 /** 762 * drm_gem_vram_object_pin() - \ 763 Implements &struct drm_gem_object_funcs.pin 764 * @gem: The GEM object to pin 765 * 766 * Returns: 767 * 0 on success, or 768 * a negative errno code otherwise. 769 */ 770 static int drm_gem_vram_object_pin(struct drm_gem_object *gem) 771 { 772 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem); 773 774 /* Fbdev console emulation is the use case of these PRIME 775 * helpers. This may involve updating a hardware buffer from 776 * a shadow FB. We pin the buffer to it's current location 777 * (either video RAM or system memory) to prevent it from 778 * being relocated during the update operation. If you require 779 * the buffer to be pinned to VRAM, implement a callback that 780 * sets the flags accordingly. 781 */ 782 return drm_gem_vram_pin(gbo, 0); 783 } 784 785 /** 786 * drm_gem_vram_object_unpin() - \ 787 Implements &struct drm_gem_object_funcs.unpin 788 * @gem: The GEM object to unpin 789 */ 790 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem) 791 { 792 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem); 793 794 drm_gem_vram_unpin(gbo); 795 } 796 797 /** 798 * drm_gem_vram_object_vmap() - 799 * Implements &struct drm_gem_object_funcs.vmap 800 * @gem: The GEM object to map 801 * @map: Returns the kernel virtual address of the VRAM GEM object's backing 802 * store. 803 * 804 * Returns: 805 * 0 on success, or a negative error code otherwise. 806 */ 807 static int drm_gem_vram_object_vmap(struct drm_gem_object *gem, 808 struct iosys_map *map) 809 { 810 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem); 811 812 return drm_gem_vram_vmap(gbo, map); 813 } 814 815 /** 816 * drm_gem_vram_object_vunmap() - 817 * Implements &struct drm_gem_object_funcs.vunmap 818 * @gem: The GEM object to unmap 819 * @map: Kernel virtual address where the VRAM GEM object was mapped 820 */ 821 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem, 822 struct iosys_map *map) 823 { 824 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem); 825 826 drm_gem_vram_vunmap(gbo, map); 827 } 828 829 /* 830 * GEM object funcs 831 */ 832 833 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = { 834 .free = drm_gem_vram_object_free, 835 .pin = drm_gem_vram_object_pin, 836 .unpin = drm_gem_vram_object_unpin, 837 .vmap = drm_gem_vram_object_vmap, 838 .vunmap = drm_gem_vram_object_vunmap, 839 .mmap = drm_gem_ttm_mmap, 840 .print_info = drm_gem_ttm_print_info, 841 }; 842 843 /* 844 * VRAM memory manager 845 */ 846 847 /* 848 * TTM TT 849 */ 850 851 static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt) 852 { 853 ttm_tt_fini(tt); 854 kfree(tt); 855 } 856 857 /* 858 * TTM BO device 859 */ 860 861 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo, 862 uint32_t page_flags) 863 { 864 struct ttm_tt *tt; 865 int ret; 866 867 tt = kzalloc(sizeof(*tt), GFP_KERNEL); 868 if (!tt) 869 return NULL; 870 871 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached, 0); 872 if (ret < 0) 873 goto err_ttm_tt_init; 874 875 return tt; 876 877 err_ttm_tt_init: 878 kfree(tt); 879 return NULL; 880 } 881 882 static void bo_driver_evict_flags(struct ttm_buffer_object *bo, 883 struct ttm_placement *placement) 884 { 885 struct drm_gem_vram_object *gbo; 886 887 /* TTM may pass BOs that are not GEM VRAM BOs. */ 888 if (!drm_is_gem_vram(bo)) 889 return; 890 891 gbo = drm_gem_vram_of_bo(bo); 892 893 drm_gem_vram_bo_driver_evict_flags(gbo, placement); 894 } 895 896 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo) 897 { 898 struct drm_gem_vram_object *gbo; 899 900 /* TTM may pass BOs that are not GEM VRAM BOs. */ 901 if (!drm_is_gem_vram(bo)) 902 return; 903 904 gbo = drm_gem_vram_of_bo(bo); 905 906 drm_gem_vram_bo_driver_move_notify(gbo); 907 } 908 909 static int bo_driver_move(struct ttm_buffer_object *bo, 910 bool evict, 911 struct ttm_operation_ctx *ctx, 912 struct ttm_resource *new_mem, 913 struct ttm_place *hop) 914 { 915 struct drm_gem_vram_object *gbo; 916 917 if (!bo->resource) { 918 if (new_mem->mem_type != TTM_PL_SYSTEM) { 919 hop->mem_type = TTM_PL_SYSTEM; 920 hop->flags = TTM_PL_FLAG_TEMPORARY; 921 return -EMULTIHOP; 922 } 923 924 ttm_bo_move_null(bo, new_mem); 925 return 0; 926 } 927 928 gbo = drm_gem_vram_of_bo(bo); 929 930 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem); 931 } 932 933 static int bo_driver_io_mem_reserve(struct ttm_device *bdev, 934 struct ttm_resource *mem) 935 { 936 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev); 937 938 switch (mem->mem_type) { 939 case TTM_PL_SYSTEM: /* nothing to do */ 940 break; 941 case TTM_PL_VRAM: 942 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base; 943 mem->bus.is_iomem = true; 944 mem->bus.caching = ttm_write_combined; 945 break; 946 default: 947 return -EINVAL; 948 } 949 950 return 0; 951 } 952 953 static struct ttm_device_funcs bo_driver = { 954 .ttm_tt_create = bo_driver_ttm_tt_create, 955 .ttm_tt_destroy = bo_driver_ttm_tt_destroy, 956 .eviction_valuable = ttm_bo_eviction_valuable, 957 .evict_flags = bo_driver_evict_flags, 958 .move = bo_driver_move, 959 .delete_mem_notify = bo_driver_delete_mem_notify, 960 .io_mem_reserve = bo_driver_io_mem_reserve, 961 }; 962 963 /* 964 * struct drm_vram_mm 965 */ 966 967 static int drm_vram_mm_debugfs(struct seq_file *m, void *data) 968 { 969 struct drm_debugfs_entry *entry = m->private; 970 struct drm_vram_mm *vmm = entry->dev->vram_mm; 971 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM); 972 struct drm_printer p = drm_seq_file_printer(m); 973 974 ttm_resource_manager_debug(man, &p); 975 return 0; 976 } 977 978 static const struct drm_debugfs_info drm_vram_mm_debugfs_list[] = { 979 { "vram-mm", drm_vram_mm_debugfs, 0, NULL }, 980 }; 981 982 /** 983 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file. 984 * 985 * @minor: drm minor device. 986 * 987 */ 988 void drm_vram_mm_debugfs_init(struct drm_minor *minor) 989 { 990 drm_debugfs_add_files(minor->dev, drm_vram_mm_debugfs_list, 991 ARRAY_SIZE(drm_vram_mm_debugfs_list)); 992 } 993 EXPORT_SYMBOL(drm_vram_mm_debugfs_init); 994 995 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev, 996 uint64_t vram_base, size_t vram_size) 997 { 998 int ret; 999 1000 vmm->vram_base = vram_base; 1001 vmm->vram_size = vram_size; 1002 1003 ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev, 1004 dev->anon_inode->i_mapping, 1005 dev->vma_offset_manager, 1006 false, true); 1007 if (ret) 1008 return ret; 1009 1010 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM, 1011 false, vram_size >> PAGE_SHIFT); 1012 if (ret) 1013 return ret; 1014 1015 return 0; 1016 } 1017 1018 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm) 1019 { 1020 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM); 1021 ttm_device_fini(&vmm->bdev); 1022 } 1023 1024 /* 1025 * Helpers for integration with struct drm_device 1026 */ 1027 1028 static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base, 1029 size_t vram_size) 1030 { 1031 int ret; 1032 1033 if (WARN_ON(dev->vram_mm)) 1034 return dev->vram_mm; 1035 1036 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL); 1037 if (!dev->vram_mm) 1038 return ERR_PTR(-ENOMEM); 1039 1040 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size); 1041 if (ret) 1042 goto err_kfree; 1043 1044 return dev->vram_mm; 1045 1046 err_kfree: 1047 kfree(dev->vram_mm); 1048 dev->vram_mm = NULL; 1049 return ERR_PTR(ret); 1050 } 1051 1052 static void drm_vram_helper_release_mm(struct drm_device *dev) 1053 { 1054 if (!dev->vram_mm) 1055 return; 1056 1057 drm_vram_mm_cleanup(dev->vram_mm); 1058 kfree(dev->vram_mm); 1059 dev->vram_mm = NULL; 1060 } 1061 1062 static void drm_vram_mm_release(struct drm_device *dev, void *ptr) 1063 { 1064 drm_vram_helper_release_mm(dev); 1065 } 1066 1067 /** 1068 * drmm_vram_helper_init - Initializes a device's instance of 1069 * &struct drm_vram_mm 1070 * @dev: the DRM device 1071 * @vram_base: the base address of the video memory 1072 * @vram_size: the size of the video memory in bytes 1073 * 1074 * Creates a new instance of &struct drm_vram_mm and stores it in 1075 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned 1076 * up as part of device cleanup. Calling this function multiple times 1077 * will generate an error message. 1078 * 1079 * Returns: 1080 * 0 on success, or a negative errno code otherwise. 1081 */ 1082 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base, 1083 size_t vram_size) 1084 { 1085 struct drm_vram_mm *vram_mm; 1086 1087 if (drm_WARN_ON_ONCE(dev, dev->vram_mm)) 1088 return 0; 1089 1090 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size); 1091 if (IS_ERR(vram_mm)) 1092 return PTR_ERR(vram_mm); 1093 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL); 1094 } 1095 EXPORT_SYMBOL(drmm_vram_helper_init); 1096 1097 /* 1098 * Mode-config helpers 1099 */ 1100 1101 static enum drm_mode_status 1102 drm_vram_helper_mode_valid_internal(struct drm_device *dev, 1103 const struct drm_display_mode *mode, 1104 unsigned long max_bpp) 1105 { 1106 struct drm_vram_mm *vmm = dev->vram_mm; 1107 unsigned long fbsize, fbpages, max_fbpages; 1108 1109 if (WARN_ON(!dev->vram_mm)) 1110 return MODE_BAD; 1111 1112 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT; 1113 1114 fbsize = mode->hdisplay * mode->vdisplay * max_bpp; 1115 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE); 1116 1117 if (fbpages > max_fbpages) 1118 return MODE_MEM; 1119 1120 return MODE_OK; 1121 } 1122 1123 /** 1124 * drm_vram_helper_mode_valid - Tests if a display mode's 1125 * framebuffer fits into the available video memory. 1126 * @dev: the DRM device 1127 * @mode: the mode to test 1128 * 1129 * This function tests if enough video memory is available for using the 1130 * specified display mode. Atomic modesetting requires importing the 1131 * designated framebuffer into video memory before evicting the active 1132 * one. Hence, any framebuffer may consume at most half of the available 1133 * VRAM. Display modes that require a larger framebuffer can not be used, 1134 * even if the CRTC does support them. Each framebuffer is assumed to 1135 * have 32-bit color depth. 1136 * 1137 * Note: 1138 * The function can only test if the display mode is supported in 1139 * general. If there are too many framebuffers pinned to video memory, 1140 * a display mode may still not be usable in practice. The color depth of 1141 * 32-bit fits all current use case. A more flexible test can be added 1142 * when necessary. 1143 * 1144 * Returns: 1145 * MODE_OK if the display mode is supported, or an error code of type 1146 * enum drm_mode_status otherwise. 1147 */ 1148 enum drm_mode_status 1149 drm_vram_helper_mode_valid(struct drm_device *dev, 1150 const struct drm_display_mode *mode) 1151 { 1152 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */ 1153 1154 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp); 1155 } 1156 EXPORT_SYMBOL(drm_vram_helper_mode_valid); 1157 1158 MODULE_DESCRIPTION("DRM VRAM memory-management helpers"); 1159 MODULE_LICENSE("GPL"); 1160