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