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