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