File Index
Files and directories
Sign in
FILE

drm_gem_vram_helper.c

File preview with syntax highlighting.

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