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