1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/dma-buf-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_ttm_helper.h>
13 #include <drm/drm_gem_vram_helper.h>
14 #include <drm/drm_managed.h>
15 #include <drm/drm_mode.h>
16 #include <drm/drm_plane.h>
17 #include <drm/drm_prime.h>
18 #include <drm/drm_simple_kms_helper.h>
19 
20 #include <drm/ttm/ttm_range_manager.h>
21 
22 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
23 
24 /**
25  * DOC: overview
26  *
27  * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
28  * buffer object that is backed by video RAM (VRAM). It can be used for
29  * framebuffer devices with dedicated memory.
30  *
31  * The data structure &struct drm_vram_mm and its helpers implement a memory
32  * manager for simple framebuffer devices with dedicated video memory. GEM
33  * VRAM buffer objects are either placed in the video memory or remain evicted
34  * to system memory.
35  *
36  * With the GEM interface userspace applications create, manage and destroy
37  * graphics buffers, such as an on-screen framebuffer. GEM does not provide
38  * an implementation of these interfaces. It's up to the DRM driver to
39  * provide an implementation that suits the hardware. If the hardware device
40  * contains dedicated video memory, the DRM driver can use the VRAM helper
41  * library. Each active buffer object is stored in video RAM. Active
42  * buffer are used for drawing the current frame, typically something like
43  * the frame's scanout buffer or the cursor image. If there's no more space
44  * left in VRAM, inactive GEM objects can be moved to system memory.
45  *
46  * To initialize the VRAM helper library call drmm_vram_helper_alloc_mm().
47  * The function allocates and initializes an instance of &struct drm_vram_mm
48  * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
49  * &struct drm_driver and  &DRM_VRAM_MM_FILE_OPERATIONS to initialize
50  * &struct file_operations; as illustrated below.
51  *
52  * .. code-block:: c
53  *
54  *	struct file_operations fops ={
55  *		.owner = THIS_MODULE,
56  *		DRM_VRAM_MM_FILE_OPERATION
57  *	};
58  *	struct drm_driver drv = {
59  *		.driver_feature = DRM_ ... ,
60  *		.fops = &fops,
61  *		DRM_GEM_VRAM_DRIVER
62  *	};
63  *
64  *	int init_drm_driver()
65  *	{
66  *		struct drm_device *dev;
67  *		uint64_t vram_base;
68  *		unsigned long vram_size;
69  *		int ret;
70  *
71  *		// setup device, vram base and size
72  *		// ...
73  *
74  *		ret = drmm_vram_helper_alloc_mm(dev, vram_base, vram_size);
75  *		if (ret)
76  *			return ret;
77  *		return 0;
78  *	}
79  *
80  * This creates an instance of &struct drm_vram_mm, exports DRM userspace
81  * interfaces for GEM buffer management and initializes file operations to
82  * allow for accessing created GEM buffers. With this setup, the DRM driver
83  * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
84  * to userspace.
85  *
86  * You don't have to clean up the instance of VRAM MM.
87  * drmm_vram_helper_alloc_mm() is a managed interface that installs a
88  * clean-up handler to run during the DRM device's release.
89  *
90  * For drawing or scanout operations, rsp. buffer objects have to be pinned
91  * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
92  * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
93  * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
94  *
95  * A buffer object that is pinned in video RAM has a fixed address within that
96  * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
97  * it's used to program the hardware's scanout engine for framebuffers, set
98  * the cursor overlay's image for a mouse cursor, or use it as input to the
99  * hardware's drawing engine.
100  *
101  * To access a buffer object's memory from the DRM driver, call
102  * drm_gem_vram_vmap(). It maps the buffer into kernel address
103  * space and returns the memory address. Use drm_gem_vram_vunmap() to
104  * release the mapping.
105  */
106 
107 /*
108  * Buffer-objects helpers
109  */
110 
111 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
112 {
113 	/* We got here via ttm_bo_put(), which means that the
114 	 * TTM buffer object in 'bo' has already been cleaned
115 	 * up; only release the GEM object.
116 	 */
117 
118 	WARN_ON(gbo->vmap_use_count);
119 	WARN_ON(dma_buf_map_is_set(&gbo->map));
120 
121 	drm_gem_object_release(&gbo->bo.base);
122 }
123 
124 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
125 {
126 	drm_gem_vram_cleanup(gbo);
127 	kfree(gbo);
128 }
129 
130 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
131 {
132 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
133 
134 	drm_gem_vram_destroy(gbo);
135 }
136 
137 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
138 				   unsigned long pl_flag)
139 {
140 	u32 invariant_flags = 0;
141 	unsigned int i;
142 	unsigned int c = 0;
143 
144 	if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
145 		invariant_flags = TTM_PL_FLAG_TOPDOWN;
146 
147 	gbo->placement.placement = gbo->placements;
148 	gbo->placement.busy_placement = gbo->placements;
149 
150 	if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
151 		gbo->placements[c].mem_type = TTM_PL_VRAM;
152 		gbo->placements[c++].flags = invariant_flags;
153 	}
154 
155 	if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
156 		gbo->placements[c].mem_type = TTM_PL_SYSTEM;
157 		gbo->placements[c++].flags = invariant_flags;
158 	}
159 
160 	gbo->placement.num_placement = c;
161 	gbo->placement.num_busy_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 (!gem)
201 			return ERR_PTR(-ENOMEM);
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(bdev, &gbo->bo, size, 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 dma_buf_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 (dma_buf_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 dma_buf_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, !dma_buf_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 dma_buf_map *map)
432 {
433 	int ret;
434 
435 	ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
436 	if (ret)
437 		return ret;
438 
439 	ret = drm_gem_vram_pin_locked(gbo, 0);
440 	if (ret)
441 		goto err_ttm_bo_unreserve;
442 	ret = drm_gem_vram_kmap_locked(gbo, map);
443 	if (ret)
444 		goto err_drm_gem_vram_unpin_locked;
445 
446 	ttm_bo_unreserve(&gbo->bo);
447 
448 	return 0;
449 
450 err_drm_gem_vram_unpin_locked:
451 	drm_gem_vram_unpin_locked(gbo);
452 err_ttm_bo_unreserve:
453 	ttm_bo_unreserve(&gbo->bo);
454 	return ret;
455 }
456 EXPORT_SYMBOL(drm_gem_vram_vmap);
457 
458 /**
459  * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
460  * @gbo: The GEM VRAM object to unmap
461  * @map: Kernel virtual address where the VRAM GEM object was mapped
462  *
463  * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
464  * the documentation for drm_gem_vram_vmap() for more information.
465  */
466 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map)
467 {
468 	int ret;
469 
470 	ret = ttm_bo_reserve(&gbo->bo, false, false, NULL);
471 	if (WARN_ONCE(ret, "ttm_bo_reserve_failed(): ret=%d\n", ret))
472 		return;
473 
474 	drm_gem_vram_kunmap_locked(gbo, map);
475 	drm_gem_vram_unpin_locked(gbo);
476 
477 	ttm_bo_unreserve(&gbo->bo);
478 }
479 EXPORT_SYMBOL(drm_gem_vram_vunmap);
480 
481 /**
482  * drm_gem_vram_fill_create_dumb() - \
483 	Helper for implementing &struct drm_driver.dumb_create
484  * @file:		the DRM file
485  * @dev:		the DRM device
486  * @pg_align:		the buffer's alignment in multiples of the page size
487  * @pitch_align:	the scanline's alignment in powers of 2
488  * @args:		the arguments as provided to \
489 				&struct drm_driver.dumb_create
490  *
491  * This helper function fills &struct drm_mode_create_dumb, which is used
492  * by &struct drm_driver.dumb_create. Implementations of this interface
493  * should forwards their arguments to this helper, plus the driver-specific
494  * parameters.
495  *
496  * Returns:
497  * 0 on success, or
498  * a negative error code otherwise.
499  */
500 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
501 				  struct drm_device *dev,
502 				  unsigned long pg_align,
503 				  unsigned long pitch_align,
504 				  struct drm_mode_create_dumb *args)
505 {
506 	size_t pitch, size;
507 	struct drm_gem_vram_object *gbo;
508 	int ret;
509 	u32 handle;
510 
511 	pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
512 	if (pitch_align) {
513 		if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
514 			return -EINVAL;
515 		pitch = ALIGN(pitch, pitch_align);
516 	}
517 	size = pitch * args->height;
518 
519 	size = roundup(size, PAGE_SIZE);
520 	if (!size)
521 		return -EINVAL;
522 
523 	gbo = drm_gem_vram_create(dev, size, pg_align);
524 	if (IS_ERR(gbo))
525 		return PTR_ERR(gbo);
526 
527 	ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
528 	if (ret)
529 		goto err_drm_gem_object_put;
530 
531 	drm_gem_object_put(&gbo->bo.base);
532 
533 	args->pitch = pitch;
534 	args->size = size;
535 	args->handle = handle;
536 
537 	return 0;
538 
539 err_drm_gem_object_put:
540 	drm_gem_object_put(&gbo->bo.base);
541 	return ret;
542 }
543 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
544 
545 /*
546  * Helpers for struct ttm_device_funcs
547  */
548 
549 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
550 {
551 	return (bo->destroy == ttm_buffer_object_destroy);
552 }
553 
554 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
555 					       struct ttm_placement *pl)
556 {
557 	drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
558 	*pl = gbo->placement;
559 }
560 
561 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
562 {
563 	struct ttm_buffer_object *bo = &gbo->bo;
564 	struct drm_device *dev = bo->base.dev;
565 
566 	if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
567 		return;
568 
569 	ttm_bo_vunmap(bo, &gbo->map);
570 	dma_buf_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
571 }
572 
573 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
574 				       bool evict,
575 				       struct ttm_operation_ctx *ctx,
576 				       struct ttm_resource *new_mem)
577 {
578 	drm_gem_vram_bo_driver_move_notify(gbo);
579 	return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
580 }
581 
582 /*
583  * Helpers for struct drm_gem_object_funcs
584  */
585 
586 /**
587  * drm_gem_vram_object_free() - \
588 	Implements &struct drm_gem_object_funcs.free
589  * @gem:       GEM object. Refers to &struct drm_gem_vram_object.gem
590  */
591 static void drm_gem_vram_object_free(struct drm_gem_object *gem)
592 {
593 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
594 
595 	drm_gem_vram_put(gbo);
596 }
597 
598 /*
599  * Helpers for dump buffers
600  */
601 
602 /**
603  * drm_gem_vram_driver_dumb_create() - \
604 	Implements &struct drm_driver.dumb_create
605  * @file:		the DRM file
606  * @dev:		the DRM device
607  * @args:		the arguments as provided to \
608 				&struct drm_driver.dumb_create
609  *
610  * This function requires the driver to use @drm_device.vram_mm for its
611  * instance of VRAM MM.
612  *
613  * Returns:
614  * 0 on success, or
615  * a negative error code otherwise.
616  */
617 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
618 				    struct drm_device *dev,
619 				    struct drm_mode_create_dumb *args)
620 {
621 	if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
622 		return -EINVAL;
623 
624 	return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
625 }
626 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
627 
628 /*
629  * Helpers for struct drm_plane_helper_funcs
630  */
631 
632 /**
633  * drm_gem_vram_plane_helper_prepare_fb() - \
634  *	Implements &struct drm_plane_helper_funcs.prepare_fb
635  * @plane:	a DRM plane
636  * @new_state:	the plane's new state
637  *
638  * During plane updates, this function sets the plane's fence and
639  * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
640  * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
641  *
642  * Returns:
643  *	0 on success, or
644  *	a negative errno code otherwise.
645  */
646 int
647 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
648 				     struct drm_plane_state *new_state)
649 {
650 	size_t i;
651 	struct drm_gem_vram_object *gbo;
652 	int ret;
653 
654 	if (!new_state->fb)
655 		return 0;
656 
657 	for (i = 0; i < ARRAY_SIZE(new_state->fb->obj); ++i) {
658 		if (!new_state->fb->obj[i])
659 			continue;
660 		gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
661 		ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
662 		if (ret)
663 			goto err_drm_gem_vram_unpin;
664 	}
665 
666 	ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
667 	if (ret)
668 		goto err_drm_gem_vram_unpin;
669 
670 	return 0;
671 
672 err_drm_gem_vram_unpin:
673 	while (i) {
674 		--i;
675 		gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
676 		drm_gem_vram_unpin(gbo);
677 	}
678 	return ret;
679 }
680 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
681 
682 /**
683  * drm_gem_vram_plane_helper_cleanup_fb() - \
684  *	Implements &struct drm_plane_helper_funcs.cleanup_fb
685  * @plane:	a DRM plane
686  * @old_state:	the plane's old state
687  *
688  * During plane updates, this function unpins the GEM VRAM
689  * objects of the plane's old framebuffer from VRAM. Complements
690  * drm_gem_vram_plane_helper_prepare_fb().
691  */
692 void
693 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
694 				     struct drm_plane_state *old_state)
695 {
696 	size_t i;
697 	struct drm_gem_vram_object *gbo;
698 
699 	if (!old_state->fb)
700 		return;
701 
702 	for (i = 0; i < ARRAY_SIZE(old_state->fb->obj); ++i) {
703 		if (!old_state->fb->obj[i])
704 			continue;
705 		gbo = drm_gem_vram_of_gem(old_state->fb->obj[i]);
706 		drm_gem_vram_unpin(gbo);
707 	}
708 }
709 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
710 
711 /*
712  * Helpers for struct drm_simple_display_pipe_funcs
713  */
714 
715 /**
716  * drm_gem_vram_simple_display_pipe_prepare_fb() - \
717  *	Implements &struct drm_simple_display_pipe_funcs.prepare_fb
718  * @pipe:	a simple display pipe
719  * @new_state:	the plane's new state
720  *
721  * During plane updates, this function pins the GEM VRAM
722  * objects of the plane's new framebuffer to VRAM. Call
723  * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
724  *
725  * Returns:
726  *	0 on success, or
727  *	a negative errno code otherwise.
728  */
729 int drm_gem_vram_simple_display_pipe_prepare_fb(
730 	struct drm_simple_display_pipe *pipe,
731 	struct drm_plane_state *new_state)
732 {
733 	return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
734 }
735 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
736 
737 /**
738  * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
739  *	Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
740  * @pipe:	a simple display pipe
741  * @old_state:	the plane's old state
742  *
743  * During plane updates, this function unpins the GEM VRAM
744  * objects of the plane's old framebuffer from VRAM. Complements
745  * drm_gem_vram_simple_display_pipe_prepare_fb().
746  */
747 void drm_gem_vram_simple_display_pipe_cleanup_fb(
748 	struct drm_simple_display_pipe *pipe,
749 	struct drm_plane_state *old_state)
750 {
751 	drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
752 }
753 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
754 
755 /*
756  * PRIME helpers
757  */
758 
759 /**
760  * drm_gem_vram_object_pin() - \
761 	Implements &struct drm_gem_object_funcs.pin
762  * @gem:	The GEM object to pin
763  *
764  * Returns:
765  * 0 on success, or
766  * a negative errno code otherwise.
767  */
768 static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
769 {
770 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
771 
772 	/* Fbdev console emulation is the use case of these PRIME
773 	 * helpers. This may involve updating a hardware buffer from
774 	 * a shadow FB. We pin the buffer to it's current location
775 	 * (either video RAM or system memory) to prevent it from
776 	 * being relocated during the update operation. If you require
777 	 * the buffer to be pinned to VRAM, implement a callback that
778 	 * sets the flags accordingly.
779 	 */
780 	return drm_gem_vram_pin(gbo, 0);
781 }
782 
783 /**
784  * drm_gem_vram_object_unpin() - \
785 	Implements &struct drm_gem_object_funcs.unpin
786  * @gem:	The GEM object to unpin
787  */
788 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
789 {
790 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
791 
792 	drm_gem_vram_unpin(gbo);
793 }
794 
795 /**
796  * drm_gem_vram_object_vmap() -
797  *	Implements &struct drm_gem_object_funcs.vmap
798  * @gem: The GEM object to map
799  * @map: Returns the kernel virtual address of the VRAM GEM object's backing
800  *       store.
801  *
802  * Returns:
803  * 0 on success, or a negative error code otherwise.
804  */
805 static int drm_gem_vram_object_vmap(struct drm_gem_object *gem, struct dma_buf_map *map)
806 {
807 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
808 
809 	return drm_gem_vram_vmap(gbo, map);
810 }
811 
812 /**
813  * drm_gem_vram_object_vunmap() -
814  *	Implements &struct drm_gem_object_funcs.vunmap
815  * @gem: The GEM object to unmap
816  * @map: Kernel virtual address where the VRAM GEM object was mapped
817  */
818 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem, struct dma_buf_map *map)
819 {
820 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
821 
822 	drm_gem_vram_vunmap(gbo, map);
823 }
824 
825 /*
826  * GEM object funcs
827  */
828 
829 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
830 	.free	= drm_gem_vram_object_free,
831 	.pin	= drm_gem_vram_object_pin,
832 	.unpin	= drm_gem_vram_object_unpin,
833 	.vmap	= drm_gem_vram_object_vmap,
834 	.vunmap	= drm_gem_vram_object_vunmap,
835 	.mmap   = drm_gem_ttm_mmap,
836 	.print_info = drm_gem_ttm_print_info,
837 };
838 
839 /*
840  * VRAM memory manager
841  */
842 
843 /*
844  * TTM TT
845  */
846 
847 static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
848 {
849 	ttm_tt_destroy_common(bdev, tt);
850 	ttm_tt_fini(tt);
851 	kfree(tt);
852 }
853 
854 /*
855  * TTM BO device
856  */
857 
858 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
859 					      uint32_t page_flags)
860 {
861 	struct ttm_tt *tt;
862 	int ret;
863 
864 	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
865 	if (!tt)
866 		return NULL;
867 
868 	ret = ttm_tt_init(tt, bo, page_flags, ttm_cached);
869 	if (ret < 0)
870 		goto err_ttm_tt_init;
871 
872 	return tt;
873 
874 err_ttm_tt_init:
875 	kfree(tt);
876 	return NULL;
877 }
878 
879 static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
880 				  struct ttm_placement *placement)
881 {
882 	struct drm_gem_vram_object *gbo;
883 
884 	/* TTM may pass BOs that are not GEM VRAM BOs. */
885 	if (!drm_is_gem_vram(bo))
886 		return;
887 
888 	gbo = drm_gem_vram_of_bo(bo);
889 
890 	drm_gem_vram_bo_driver_evict_flags(gbo, placement);
891 }
892 
893 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
894 {
895 	struct drm_gem_vram_object *gbo;
896 
897 	/* TTM may pass BOs that are not GEM VRAM BOs. */
898 	if (!drm_is_gem_vram(bo))
899 		return;
900 
901 	gbo = drm_gem_vram_of_bo(bo);
902 
903 	drm_gem_vram_bo_driver_move_notify(gbo);
904 }
905 
906 static int bo_driver_move(struct ttm_buffer_object *bo,
907 			  bool evict,
908 			  struct ttm_operation_ctx *ctx,
909 			  struct ttm_resource *new_mem,
910 			  struct ttm_place *hop)
911 {
912 	struct drm_gem_vram_object *gbo;
913 
914 	gbo = drm_gem_vram_of_bo(bo);
915 
916 	return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
917 }
918 
919 static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
920 				    struct ttm_resource *mem)
921 {
922 	struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
923 
924 	switch (mem->mem_type) {
925 	case TTM_PL_SYSTEM:	/* nothing to do */
926 		break;
927 	case TTM_PL_VRAM:
928 		mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
929 		mem->bus.is_iomem = true;
930 		mem->bus.caching = ttm_write_combined;
931 		break;
932 	default:
933 		return -EINVAL;
934 	}
935 
936 	return 0;
937 }
938 
939 static struct ttm_device_funcs bo_driver = {
940 	.ttm_tt_create = bo_driver_ttm_tt_create,
941 	.ttm_tt_destroy = bo_driver_ttm_tt_destroy,
942 	.eviction_valuable = ttm_bo_eviction_valuable,
943 	.evict_flags = bo_driver_evict_flags,
944 	.move = bo_driver_move,
945 	.delete_mem_notify = bo_driver_delete_mem_notify,
946 	.io_mem_reserve = bo_driver_io_mem_reserve,
947 };
948 
949 /*
950  * struct drm_vram_mm
951  */
952 
953 static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
954 {
955 	struct drm_info_node *node = (struct drm_info_node *) m->private;
956 	struct drm_vram_mm *vmm = node->minor->dev->vram_mm;
957 	struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
958 	struct drm_printer p = drm_seq_file_printer(m);
959 
960 	ttm_resource_manager_debug(man, &p);
961 	return 0;
962 }
963 
964 static const struct drm_info_list drm_vram_mm_debugfs_list[] = {
965 	{ "vram-mm", drm_vram_mm_debugfs, 0, NULL },
966 };
967 
968 /**
969  * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
970  *
971  * @minor: drm minor device.
972  *
973  */
974 void drm_vram_mm_debugfs_init(struct drm_minor *minor)
975 {
976 	drm_debugfs_create_files(drm_vram_mm_debugfs_list,
977 				 ARRAY_SIZE(drm_vram_mm_debugfs_list),
978 				 minor->debugfs_root, minor);
979 }
980 EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
981 
982 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
983 			    uint64_t vram_base, size_t vram_size)
984 {
985 	int ret;
986 
987 	vmm->vram_base = vram_base;
988 	vmm->vram_size = vram_size;
989 
990 	ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
991 				 dev->anon_inode->i_mapping,
992 				 dev->vma_offset_manager,
993 				 false, true);
994 	if (ret)
995 		return ret;
996 
997 	ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
998 				 false, vram_size >> PAGE_SHIFT);
999 	if (ret)
1000 		return ret;
1001 
1002 	return 0;
1003 }
1004 
1005 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
1006 {
1007 	ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
1008 	ttm_device_fini(&vmm->bdev);
1009 }
1010 
1011 /*
1012  * Helpers for integration with struct drm_device
1013  */
1014 
1015 static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base,
1016 						    size_t vram_size)
1017 {
1018 	int ret;
1019 
1020 	if (WARN_ON(dev->vram_mm))
1021 		return dev->vram_mm;
1022 
1023 	dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1024 	if (!dev->vram_mm)
1025 		return ERR_PTR(-ENOMEM);
1026 
1027 	ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
1028 	if (ret)
1029 		goto err_kfree;
1030 
1031 	return dev->vram_mm;
1032 
1033 err_kfree:
1034 	kfree(dev->vram_mm);
1035 	dev->vram_mm = NULL;
1036 	return ERR_PTR(ret);
1037 }
1038 
1039 static void drm_vram_helper_release_mm(struct drm_device *dev)
1040 {
1041 	if (!dev->vram_mm)
1042 		return;
1043 
1044 	drm_vram_mm_cleanup(dev->vram_mm);
1045 	kfree(dev->vram_mm);
1046 	dev->vram_mm = NULL;
1047 }
1048 
1049 static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1050 {
1051 	drm_vram_helper_release_mm(dev);
1052 }
1053 
1054 /**
1055  * drmm_vram_helper_init - Initializes a device's instance of
1056  *                         &struct drm_vram_mm
1057  * @dev:	the DRM device
1058  * @vram_base:	the base address of the video memory
1059  * @vram_size:	the size of the video memory in bytes
1060  *
1061  * Creates a new instance of &struct drm_vram_mm and stores it in
1062  * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1063  * up as part of device cleanup. Calling this function multiple times
1064  * will generate an error message.
1065  *
1066  * Returns:
1067  * 0 on success, or a negative errno code otherwise.
1068  */
1069 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1070 			  size_t vram_size)
1071 {
1072 	struct drm_vram_mm *vram_mm;
1073 
1074 	if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1075 		return 0;
1076 
1077 	vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1078 	if (IS_ERR(vram_mm))
1079 		return PTR_ERR(vram_mm);
1080 	return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1081 }
1082 EXPORT_SYMBOL(drmm_vram_helper_init);
1083 
1084 /*
1085  * Mode-config helpers
1086  */
1087 
1088 static enum drm_mode_status
1089 drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1090 				    const struct drm_display_mode *mode,
1091 				    unsigned long max_bpp)
1092 {
1093 	struct drm_vram_mm *vmm = dev->vram_mm;
1094 	unsigned long fbsize, fbpages, max_fbpages;
1095 
1096 	if (WARN_ON(!dev->vram_mm))
1097 		return MODE_BAD;
1098 
1099 	max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1100 
1101 	fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1102 	fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1103 
1104 	if (fbpages > max_fbpages)
1105 		return MODE_MEM;
1106 
1107 	return MODE_OK;
1108 }
1109 
1110 /**
1111  * drm_vram_helper_mode_valid - Tests if a display mode's
1112  *	framebuffer fits into the available video memory.
1113  * @dev:	the DRM device
1114  * @mode:	the mode to test
1115  *
1116  * This function tests if enough video memory is available for using the
1117  * specified display mode. Atomic modesetting requires importing the
1118  * designated framebuffer into video memory before evicting the active
1119  * one. Hence, any framebuffer may consume at most half of the available
1120  * VRAM. Display modes that require a larger framebuffer can not be used,
1121  * even if the CRTC does support them. Each framebuffer is assumed to
1122  * have 32-bit color depth.
1123  *
1124  * Note:
1125  * The function can only test if the display mode is supported in
1126  * general. If there are too many framebuffers pinned to video memory,
1127  * a display mode may still not be usable in practice. The color depth of
1128  * 32-bit fits all current use case. A more flexible test can be added
1129  * when necessary.
1130  *
1131  * Returns:
1132  * MODE_OK if the display mode is supported, or an error code of type
1133  * enum drm_mode_status otherwise.
1134  */
1135 enum drm_mode_status
1136 drm_vram_helper_mode_valid(struct drm_device *dev,
1137 			   const struct drm_display_mode *mode)
1138 {
1139 	static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1140 
1141 	return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1142 }
1143 EXPORT_SYMBOL(drm_vram_helper_mode_valid);
1144 
1145 MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1146 MODULE_LICENSE("GPL");
1147