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