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