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