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