blob: 6027584406af636f8cf05207febfdb7a331c1119 [file] [log] [blame]
Thomas Zimmermann85438a82019-05-08 10:26:11 +02001// SPDX-License-Identifier: GPL-2.0-or-later
2
Lucas De Marchi7938f422022-02-04 09:05:41 -08003#include <linux/iosys-map.h>
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +02004#include <linux/module.h>
5
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02006#include <drm/drm_debugfs.h>
7#include <drm/drm_device.h>
Thomas Zimmermann0fb5f692020-01-06 13:57:45 +01008#include <drm/drm_drv.h>
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02009#include <drm/drm_file.h>
Thomas Zimmermann6542ad82019-10-24 10:14:01 +020010#include <drm/drm_framebuffer.h>
Thomas Zimmermann820c1702021-02-22 15:17:56 +010011#include <drm/drm_gem_atomic_helper.h>
Thomas Zimmermann1c89b4b2022-05-17 13:33:25 +020012#include <drm/drm_gem_framebuffer_helper.h>
Gerd Hoffmann527f6d92019-09-04 07:47:36 +020013#include <drm/drm_gem_ttm_helper.h>
Thomas Zimmermann85438a82019-05-08 10:26:11 +020014#include <drm/drm_gem_vram_helper.h>
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +020015#include <drm/drm_managed.h>
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +020016#include <drm/drm_mode.h>
Thomas Zimmermann6542ad82019-10-24 10:14:01 +020017#include <drm/drm_plane.h>
Thomas Zimmermann1f460b42019-05-08 10:26:15 +020018#include <drm/drm_prime.h>
Thomas Zimmermann6542ad82019-10-24 10:14:01 +020019#include <drm/drm_simple_kms_helper.h>
Thomas Zimmermann85438a82019-05-08 10:26:11 +020020
Christian König3eb7d962021-04-17 18:48:36 +020021#include <drm/ttm/ttm_range_manager.h>
Christian Königa3185f92022-05-09 21:13:35 +020022#include <drm/ttm/ttm_tt.h>
Christian König3eb7d962021-04-17 18:48:36 +020023
Thomas Zimmermann31070a82019-07-02 13:50:08 +020024static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
25
Thomas Zimmermann85438a82019-05-08 10:26:11 +020026/**
27 * DOC: overview
28 *
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +020029 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
30 * buffer object that is backed by video RAM (VRAM). It can be used for
31 * framebuffer devices with dedicated memory.
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +020032 *
33 * The data structure &struct drm_vram_mm and its helpers implement a memory
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +020034 * manager for simple framebuffer devices with dedicated video memory. GEM
35 * VRAM buffer objects are either placed in the video memory or remain evicted
36 * to system memory.
37 *
38 * With the GEM interface userspace applications create, manage and destroy
39 * graphics buffers, such as an on-screen framebuffer. GEM does not provide
40 * an implementation of these interfaces. It's up to the DRM driver to
41 * provide an implementation that suits the hardware. If the hardware device
42 * contains dedicated video memory, the DRM driver can use the VRAM helper
43 * library. Each active buffer object is stored in video RAM. Active
44 * buffer are used for drawing the current frame, typically something like
45 * the frame's scanout buffer or the cursor image. If there's no more space
46 * left in VRAM, inactive GEM objects can be moved to system memory.
47 *
Luc Mab8e39222023-05-08 08:09:16 +080048 * To initialize the VRAM helper library call drmm_vram_helper_init().
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +020049 * The function allocates and initializes an instance of &struct drm_vram_mm
50 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
51 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize
52 * &struct file_operations; as illustrated below.
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +020053 *
54 * .. code-block:: c
55 *
56 * struct file_operations fops ={
57 * .owner = THIS_MODULE,
58 * DRM_VRAM_MM_FILE_OPERATION
59 * };
60 * struct drm_driver drv = {
61 * .driver_feature = DRM_ ... ,
62 * .fops = &fops,
63 * DRM_GEM_VRAM_DRIVER
64 * };
65 *
66 * int init_drm_driver()
67 * {
68 * struct drm_device *dev;
69 * uint64_t vram_base;
70 * unsigned long vram_size;
71 * int ret;
72 *
73 * // setup device, vram base and size
74 * // ...
75 *
Luc Mab8e39222023-05-08 08:09:16 +080076 * ret = drmm_vram_helper_init(dev, vram_base, vram_size);
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +020077 * if (ret)
78 * return ret;
79 * return 0;
80 * }
81 *
82 * This creates an instance of &struct drm_vram_mm, exports DRM userspace
83 * interfaces for GEM buffer management and initializes file operations to
84 * allow for accessing created GEM buffers. With this setup, the DRM driver
85 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
86 * to userspace.
87 *
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +020088 * You don't have to clean up the instance of VRAM MM.
Luc Mab8e39222023-05-08 08:09:16 +080089 * drmm_vram_helper_init() is a managed interface that installs a
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +020090 * clean-up handler to run during the DRM device's release.
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +020091 *
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +020092 * For drawing or scanout operations, rsp. buffer objects have to be pinned
93 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +020094 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
95 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
96 *
97 * A buffer object that is pinned in video RAM has a fixed address within that
98 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
99 * it's used to program the hardware's scanout engine for framebuffers, set
100 * the cursor overlay's image for a mouse cursor, or use it as input to the
Cai Huoqing0ae865e2021-07-30 21:27:29 +0800101 * hardware's drawing engine.
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +0200102 *
103 * To access a buffer object's memory from the DRM driver, call
Thomas Zimmermannd88656f2020-09-11 09:59:22 +0200104 * drm_gem_vram_vmap(). It maps the buffer into kernel address
105 * space and returns the memory address. Use drm_gem_vram_vunmap() to
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +0200106 * release the mapping.
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200107 */
108
109/*
110 * Buffer-objects helpers
111 */
112
113static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
114{
115 /* We got here via ttm_bo_put(), which means that the
116 * TTM buffer object in 'bo' has already been cleaned
117 * up; only release the GEM object.
118 */
Thomas Zimmermann37a48ad2019-09-06 14:20:53 +0200119
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100120 WARN_ON(gbo->vmap_use_count);
Lucas De Marchi7938f422022-02-04 09:05:41 -0800121 WARN_ON(iosys_map_is_set(&gbo->map));
Thomas Zimmermann37a48ad2019-09-06 14:20:53 +0200122
Gerd Hoffmann0e580c62019-08-05 16:01:04 +0200123 drm_gem_object_release(&gbo->bo.base);
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200124}
125
126static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
127{
128 drm_gem_vram_cleanup(gbo);
129 kfree(gbo);
130}
131
132static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
133{
134 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
135
136 drm_gem_vram_destroy(gbo);
137}
138
139static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
140 unsigned long pl_flag)
141{
Christian König7053e0e2020-09-07 16:14:52 +0200142 u32 invariant_flags = 0;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200143 unsigned int i;
144 unsigned int c = 0;
Christian König7053e0e2020-09-07 16:14:52 +0200145
146 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
Thomas Zimmermannb8f8dbf2020-09-21 16:25:36 +0200147 invariant_flags = TTM_PL_FLAG_TOPDOWN;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200148
149 gbo->placement.placement = gbo->placements;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200150
Christian König48e07c22020-09-10 13:39:41 +0200151 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
152 gbo->placements[c].mem_type = TTM_PL_VRAM;
Christian Königce65b872020-09-30 16:44:16 +0200153 gbo->placements[c++].flags = invariant_flags;
Christian König48e07c22020-09-10 13:39:41 +0200154 }
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200155
Christian König48e07c22020-09-10 13:39:41 +0200156 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
157 gbo->placements[c].mem_type = TTM_PL_SYSTEM;
Christian Königce65b872020-09-30 16:44:16 +0200158 gbo->placements[c++].flags = invariant_flags;
Christian König48e07c22020-09-10 13:39:41 +0200159 }
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200160
161 gbo->placement.num_placement = c;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200162
163 for (i = 0; i < c; ++i) {
164 gbo->placements[i].fpfn = 0;
165 gbo->placements[i].lpfn = 0;
166 }
167}
168
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200169/**
170 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
171 * @dev: the DRM device
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200172 * @size: the buffer size in bytes
173 * @pg_align: the buffer's alignment in multiples of the page size
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200174 *
Thomas Zimmermann4d92d7d2020-09-22 16:56:59 +0200175 * GEM objects are allocated by calling struct drm_driver.gem_create_object,
176 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
177 * object functions in struct drm_driver.gem_create_object. If no functions
178 * are set, the new GEM object will use the default functions from GEM VRAM
179 * helpers.
180 *
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200181 * Returns:
182 * A new instance of &struct drm_gem_vram_object on success, or
183 * an ERR_PTR()-encoded error code otherwise.
184 */
185struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200186 size_t size,
Thomas Zimmermannebe94282020-01-06 13:57:43 +0100187 unsigned long pg_align)
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200188{
189 struct drm_gem_vram_object *gbo;
Thomas Zimmermann4d92d7d2020-09-22 16:56:59 +0200190 struct drm_gem_object *gem;
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200191 struct drm_vram_mm *vmm = dev->vram_mm;
Christian König8af8a102020-10-01 14:51:40 +0200192 struct ttm_device *bdev;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200193 int ret;
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200194
195 if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
196 return ERR_PTR(-EINVAL);
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200197
Thomas Zimmermann0fb5f692020-01-06 13:57:45 +0100198 if (dev->driver->gem_create_object) {
Thomas Zimmermann4d92d7d2020-09-22 16:56:59 +0200199 gem = dev->driver->gem_create_object(dev, size);
Thomas Zimmermann4ff22f42021-11-30 10:52:55 +0100200 if (IS_ERR(gem))
201 return ERR_CAST(gem);
Thomas Zimmermann0fb5f692020-01-06 13:57:45 +0100202 gbo = drm_gem_vram_of_gem(gem);
203 } else {
204 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
205 if (!gbo)
206 return ERR_PTR(-ENOMEM);
Thomas Zimmermann4d92d7d2020-09-22 16:56:59 +0200207 gem = &gbo->bo.base;
Thomas Zimmermann0fb5f692020-01-06 13:57:45 +0100208 }
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200209
Thomas Zimmermann4d92d7d2020-09-22 16:56:59 +0200210 if (!gem->funcs)
211 gem->funcs = &drm_gem_vram_object_funcs;
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200212
Thomas Zimmermann4d92d7d2020-09-22 16:56:59 +0200213 ret = drm_gem_object_init(dev, gem, size);
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200214 if (ret) {
215 kfree(gbo);
216 return ERR_PTR(ret);
217 }
218
219 bdev = &vmm->bdev;
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200220
221 gbo->bo.bdev = bdev;
Thomas Zimmermann8bde6c0d2020-09-22 16:57:00 +0200222 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200223
224 /*
225 * A failing ttm_bo_init will call ttm_buffer_object_destroy
226 * to release gbo->bo.base and kfree gbo.
227 */
Christian König347987a2022-02-18 14:32:53 +0100228 ret = ttm_bo_init_validate(bdev, &gbo->bo, ttm_bo_type_device,
229 &gbo->placement, pg_align, false, NULL, NULL,
230 ttm_buffer_object_destroy);
Thomas Zimmermann7faa92d2020-09-22 16:56:58 +0200231 if (ret)
Jia Yangda62cb72020-07-14 10:32:36 +0200232 return ERR_PTR(ret);
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200233
234 return gbo;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200235}
236EXPORT_SYMBOL(drm_gem_vram_create);
237
238/**
239 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
240 * @gbo: the GEM VRAM object
241 *
242 * See ttm_bo_put() for more information.
243 */
244void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
245{
246 ttm_bo_put(&gbo->bo);
247}
248EXPORT_SYMBOL(drm_gem_vram_put);
249
Nirmoy Das46642a72020-06-24 20:26:46 +0200250static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
251{
252 /* Keep TTM behavior for now, remove when drivers are audited */
Christian Königcb1c8142021-04-30 09:48:27 +0200253 if (WARN_ON_ONCE(!gbo->bo.resource ||
254 gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
Nirmoy Das46642a72020-06-24 20:26:46 +0200255 return 0;
256
Christian Königd3116752021-04-12 15:11:47 +0200257 return gbo->bo.resource->start;
Nirmoy Das46642a72020-06-24 20:26:46 +0200258}
259
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200260/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100261 * drm_gem_vram_offset() - Returns a GEM VRAM object's offset in video memory
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200262 * @gbo: the GEM VRAM object
263 *
264 * This function returns the buffer object's offset in the device's video
265 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
266 *
267 * Returns:
268 * The buffer object's offset in video memory on success, or
269 * a negative errno code otherwise.
270 */
271s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
272{
Christian Königd5827232020-09-21 14:43:59 +0200273 if (WARN_ON_ONCE(!gbo->bo.pin_count))
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200274 return (s64)-ENODEV;
Nirmoy Das46642a72020-06-24 20:26:46 +0200275 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200276}
277EXPORT_SYMBOL(drm_gem_vram_offset);
278
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200279static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
280 unsigned long pl_flag)
281{
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200282 struct ttm_operation_ctx ctx = { false, false };
Christian Königd5827232020-09-21 14:43:59 +0200283 int ret;
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200284
Thomas Zimmermann94dee3b2024-02-27 11:14:49 +0100285 dma_resv_assert_held(gbo->bo.base.resv);
286
Christian Königd5827232020-09-21 14:43:59 +0200287 if (gbo->bo.pin_count)
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200288 goto out;
289
290 if (pl_flag)
291 drm_gem_vram_placement(gbo, pl_flag);
292
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200293 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
294 if (ret < 0)
295 return ret;
296
297out:
Christian Königd5827232020-09-21 14:43:59 +0200298 ttm_bo_pin(&gbo->bo);
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200299
300 return 0;
301}
302
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200303/**
304 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
305 * @gbo: the GEM VRAM object
306 * @pl_flag: a bitmask of possible memory regions
307 *
308 * Pinning a buffer object ensures that it is not evicted from
309 * a memory region. A pinned buffer object has to be unpinned before
Thomas Zimmermanna6c34642019-06-13 09:30:33 +0200310 * it can be pinned to another region. If the pl_flag argument is 0,
311 * the buffer is pinned at its current location (video RAM or system
312 * memory).
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200313 *
Thomas Zimmermann245f44e2019-09-23 19:27:42 +0200314 * Small buffer objects, such as cursor images, can lead to memory
315 * fragmentation if they are pinned in the middle of video RAM. This
316 * is especially a problem on devices with only a small amount of
317 * video RAM. Fragmentation can prevent the primary framebuffer from
318 * fitting in, even though there's enough memory overall. The modifier
319 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
320 * at the high end of the memory region to avoid fragmentation.
321 *
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200322 * Returns:
323 * 0 on success, or
324 * a negative error code otherwise.
325 */
326int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
327{
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200328 int ret;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200329
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200330 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200331 if (ret)
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200332 return ret;
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200333 ret = drm_gem_vram_pin_locked(gbo, pl_flag);
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200334 ttm_bo_unreserve(&gbo->bo);
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200335
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200336 return ret;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200337}
338EXPORT_SYMBOL(drm_gem_vram_pin);
339
Christian Königd5827232020-09-21 14:43:59 +0200340static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200341{
Thomas Zimmermann94dee3b2024-02-27 11:14:49 +0100342 dma_resv_assert_held(gbo->bo.base.resv);
343
Christian Königd5827232020-09-21 14:43:59 +0200344 ttm_bo_unpin(&gbo->bo);
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200345}
346
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200347/**
348 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
349 * @gbo: the GEM VRAM object
350 *
351 * Returns:
352 * 0 on success, or
353 * a negative error code otherwise.
354 */
355int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
356{
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200357 int ret;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200358
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200359 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
Thomas Zimmermannbc25bb92019-09-06 14:20:54 +0200360 if (ret)
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200361 return ret;
Christian Königd5827232020-09-21 14:43:59 +0200362
363 drm_gem_vram_unpin_locked(gbo);
Thomas Zimmermann5b24f712019-05-16 18:27:46 +0200364 ttm_bo_unreserve(&gbo->bo);
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200365
Christian Königd5827232020-09-21 14:43:59 +0200366 return 0;
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200367}
368EXPORT_SYMBOL(drm_gem_vram_unpin);
369
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200370/**
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200371 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
372 * space
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100373 * @gbo: The GEM VRAM object to map
374 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
375 * store.
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200376 *
377 * The vmap function pins a GEM VRAM object to its current location, either
378 * system or video memory, and maps its buffer into kernel address space.
379 * As pinned object cannot be relocated, you should avoid pinning objects
380 * permanently. Call drm_gem_vram_vunmap() with the returned address to
381 * unmap and unpin the GEM VRAM object.
382 *
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200383 * Returns:
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100384 * 0 on success, or a negative error code otherwise.
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200385 */
Lucas De Marchi7938f422022-02-04 09:05:41 -0800386int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200387{
388 int ret;
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200389
Dmitry Osipenko6d0bfef2022-11-14 02:38:50 +0300390 dma_resv_assert_held(gbo->bo.base.resv);
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200391
Thomas Zimmermannfe36f152024-02-27 11:14:59 +0100392 if (gbo->vmap_use_count > 0)
393 goto out;
394
395 /*
396 * VRAM helpers unmap the BO only on demand. So the previous
397 * page mapping might still be around. Only vmap if the there's
398 * no mapping present.
399 */
400 if (iosys_map_is_null(&gbo->map)) {
401 ret = ttm_bo_vmap(&gbo->bo, &gbo->map);
402 if (ret)
403 return ret;
404 }
405
406out:
407 ++gbo->vmap_use_count;
408 *map = gbo->map;
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200409
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100410 return 0;
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200411}
412EXPORT_SYMBOL(drm_gem_vram_vmap);
413
414/**
415 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100416 * @gbo: The GEM VRAM object to unmap
417 * @map: Kernel virtual address where the VRAM GEM object was mapped
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200418 *
419 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
420 * the documentation for drm_gem_vram_vmap() for more information.
421 */
Lucas De Marchi7938f422022-02-04 09:05:41 -0800422void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,
423 struct iosys_map *map)
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200424{
Thomas Zimmermannfe36f152024-02-27 11:14:59 +0100425 struct drm_device *dev = gbo->bo.base.dev;
426
Dmitry Osipenko6d0bfef2022-11-14 02:38:50 +0300427 dma_resv_assert_held(gbo->bo.base.resv);
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200428
Thomas Zimmermannfe36f152024-02-27 11:14:59 +0100429 if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count))
430 return;
431
432 if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map)))
433 return; /* BUG: map not mapped from this BO */
434
435 if (--gbo->vmap_use_count > 0)
436 return;
437
438 /*
439 * Permanently mapping and unmapping buffers adds overhead from
440 * updating the page tables and creates debugging output. Therefore,
441 * we delay the actual unmap operation until the BO gets evicted
442 * from memory. See drm_gem_vram_bo_driver_move_notify().
443 */
Thomas Zimmermannc8908bd2019-09-11 14:03:50 +0200444}
445EXPORT_SYMBOL(drm_gem_vram_vunmap);
446
447/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100448 * drm_gem_vram_fill_create_dumb() - Helper for implementing
449 * &struct drm_driver.dumb_create
450 *
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200451 * @file: the DRM file
452 * @dev: the DRM device
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200453 * @pg_align: the buffer's alignment in multiples of the page size
Thomas Zimmermann98707322019-12-03 09:38:17 +0100454 * @pitch_align: the scanline's alignment in powers of 2
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100455 * @args: the arguments as provided to
456 * &struct drm_driver.dumb_create
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200457 *
458 * This helper function fills &struct drm_mode_create_dumb, which is used
459 * by &struct drm_driver.dumb_create. Implementations of this interface
460 * should forwards their arguments to this helper, plus the driver-specific
461 * parameters.
462 *
463 * Returns:
464 * 0 on success, or
465 * a negative error code otherwise.
466 */
467int drm_gem_vram_fill_create_dumb(struct drm_file *file,
468 struct drm_device *dev,
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200469 unsigned long pg_align,
Thomas Zimmermann98707322019-12-03 09:38:17 +0100470 unsigned long pitch_align,
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200471 struct drm_mode_create_dumb *args)
472{
473 size_t pitch, size;
474 struct drm_gem_vram_object *gbo;
475 int ret;
476 u32 handle;
477
Thomas Zimmermann98707322019-12-03 09:38:17 +0100478 pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
479 if (pitch_align) {
480 if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
481 return -EINVAL;
482 pitch = ALIGN(pitch, pitch_align);
483 }
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200484 size = pitch * args->height;
485
486 size = roundup(size, PAGE_SIZE);
487 if (!size)
488 return -EINVAL;
489
Thomas Zimmermanna4d46a82020-01-06 13:57:44 +0100490 gbo = drm_gem_vram_create(dev, size, pg_align);
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200491 if (IS_ERR(gbo))
492 return PTR_ERR(gbo);
493
Gerd Hoffmann0e580c62019-08-05 16:01:04 +0200494 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200495 if (ret)
Emil Velikovbe6ee102020-05-15 10:50:53 +0100496 goto err_drm_gem_object_put;
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200497
Emil Velikovbe6ee102020-05-15 10:50:53 +0100498 drm_gem_object_put(&gbo->bo.base);
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200499
500 args->pitch = pitch;
501 args->size = size;
502 args->handle = handle;
503
504 return 0;
505
Emil Velikovbe6ee102020-05-15 10:50:53 +0100506err_drm_gem_object_put:
507 drm_gem_object_put(&gbo->bo.base);
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200508 return ret;
509}
510EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
511
Thomas Zimmermann6c812bc2019-05-08 10:26:12 +0200512/*
Christian König8af8a102020-10-01 14:51:40 +0200513 * Helpers for struct ttm_device_funcs
Thomas Zimmermann6c812bc2019-05-08 10:26:12 +0200514 */
515
516static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
517{
518 return (bo->destroy == ttm_buffer_object_destroy);
519}
520
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200521static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
522 struct ttm_placement *pl)
Thomas Zimmermann6c812bc2019-05-08 10:26:12 +0200523{
Christian König7053e0e2020-09-07 16:14:52 +0200524 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
Thomas Zimmermann6c812bc2019-05-08 10:26:12 +0200525 *pl = gbo->placement;
526}
Thomas Zimmermann6c812bc2019-05-08 10:26:12 +0200527
Christian König10073772021-02-11 11:04:23 +0100528static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
Thomas Zimmermann22364392019-09-06 14:20:56 +0200529{
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100530 struct ttm_buffer_object *bo = &gbo->bo;
531 struct drm_device *dev = bo->base.dev;
Thomas Zimmermann22364392019-09-06 14:20:56 +0200532
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100533 if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
Thomas Zimmermann22364392019-09-06 14:20:56 +0200534 return;
535
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100536 ttm_bo_vunmap(bo, &gbo->map);
Lucas De Marchi7938f422022-02-04 09:05:41 -0800537 iosys_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
Thomas Zimmermann22364392019-09-06 14:20:56 +0200538}
Thomas Zimmermann5c9dcac2019-05-08 10:26:17 +0200539
Dave Airlie2b8283f2020-10-06 10:06:43 +1000540static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
541 bool evict,
542 struct ttm_operation_ctx *ctx,
543 struct ttm_resource *new_mem)
544{
Christian König10073772021-02-11 11:04:23 +0100545 drm_gem_vram_bo_driver_move_notify(gbo);
546 return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
Dave Airlie2b8283f2020-10-06 10:06:43 +1000547}
548
Thomas Zimmermann737000f2019-05-08 10:26:13 +0200549/*
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200550 * Helpers for struct drm_gem_object_funcs
Thomas Zimmermann737000f2019-05-08 10:26:13 +0200551 */
552
553/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100554 * drm_gem_vram_object_free() - Implements &struct drm_gem_object_funcs.free
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200555 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
Thomas Zimmermann737000f2019-05-08 10:26:13 +0200556 */
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200557static void drm_gem_vram_object_free(struct drm_gem_object *gem)
Thomas Zimmermann737000f2019-05-08 10:26:13 +0200558{
559 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
560
561 drm_gem_vram_put(gbo);
562}
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200563
564/*
565 * Helpers for dump buffers
566 */
Thomas Zimmermann737000f2019-05-08 10:26:13 +0200567
568/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100569 * drm_gem_vram_driver_dumb_create() - Implements &struct drm_driver.dumb_create
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200570 * @file: the DRM file
571 * @dev: the DRM device
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100572 * @args: the arguments as provided to
573 * &struct drm_driver.dumb_create
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200574 *
575 * This function requires the driver to use @drm_device.vram_mm for its
576 * instance of VRAM MM.
577 *
578 * Returns:
579 * 0 on success, or
580 * a negative error code otherwise.
581 */
582int drm_gem_vram_driver_dumb_create(struct drm_file *file,
583 struct drm_device *dev,
584 struct drm_mode_create_dumb *args)
585{
586 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
587 return -EINVAL;
588
Thomas Zimmermanna4d46a82020-01-06 13:57:44 +0100589 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200590}
591EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
592
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200593/*
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200594 * Helpers for struct drm_plane_helper_funcs
595 */
596
Thomas Zimmermann1c89b4b2022-05-17 13:33:25 +0200597static void __drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
598 struct drm_plane_state *state,
599 unsigned int num_planes)
600{
601 struct drm_gem_object *obj;
602 struct drm_gem_vram_object *gbo;
603 struct drm_framebuffer *fb = state->fb;
604
605 while (num_planes) {
606 --num_planes;
607 obj = drm_gem_fb_get_obj(fb, num_planes);
608 if (!obj)
609 continue;
610 gbo = drm_gem_vram_of_gem(obj);
611 drm_gem_vram_unpin(gbo);
612 }
613}
614
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200615/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100616 * drm_gem_vram_plane_helper_prepare_fb() - Implements &struct
617 * drm_plane_helper_funcs.prepare_fb
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200618 * @plane: a DRM plane
619 * @new_state: the plane's new state
620 *
Thomas Zimmermannd7b001d2020-03-31 11:27:40 +0200621 * During plane updates, this function sets the plane's fence and
622 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
623 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200624 *
625 * Returns:
626 * 0 on success, or
627 * a negative errno code otherwise.
628 */
629int
630drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
631 struct drm_plane_state *new_state)
632{
Thomas Zimmermanna8d6e1272022-05-17 13:33:26 +0200633 struct drm_framebuffer *fb = new_state->fb;
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200634 struct drm_gem_vram_object *gbo;
Thomas Zimmermanna8d6e1272022-05-17 13:33:26 +0200635 struct drm_gem_object *obj;
636 unsigned int i;
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200637 int ret;
638
Thomas Zimmermanna8d6e1272022-05-17 13:33:26 +0200639 if (!fb)
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200640 return 0;
641
Thomas Zimmermanna8d6e1272022-05-17 13:33:26 +0200642 for (i = 0; i < fb->format->num_planes; ++i) {
643 obj = drm_gem_fb_get_obj(fb, i);
644 if (!obj) {
645 ret = -EINVAL;
646 goto err_drm_gem_vram_unpin;
647 }
648 gbo = drm_gem_vram_of_gem(obj);
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200649 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
650 if (ret)
651 goto err_drm_gem_vram_unpin;
652 }
653
Thomas Zimmermann820c1702021-02-22 15:17:56 +0100654 ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
Thomas Zimmermannd7b001d2020-03-31 11:27:40 +0200655 if (ret)
656 goto err_drm_gem_vram_unpin;
657
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200658 return 0;
659
660err_drm_gem_vram_unpin:
Thomas Zimmermann1c89b4b2022-05-17 13:33:25 +0200661 __drm_gem_vram_plane_helper_cleanup_fb(plane, new_state, i);
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200662 return ret;
663}
664EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
665
666/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100667 * drm_gem_vram_plane_helper_cleanup_fb() - Implements &struct
668 * drm_plane_helper_funcs.cleanup_fb
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200669 * @plane: a DRM plane
670 * @old_state: the plane's old state
671 *
672 * During plane updates, this function unpins the GEM VRAM
673 * objects of the plane's old framebuffer from VRAM. Complements
674 * drm_gem_vram_plane_helper_prepare_fb().
675 */
676void
677drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
678 struct drm_plane_state *old_state)
679{
Thomas Zimmermann1c89b4b2022-05-17 13:33:25 +0200680 struct drm_framebuffer *fb = old_state->fb;
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200681
Thomas Zimmermann1c89b4b2022-05-17 13:33:25 +0200682 if (!fb)
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200683 return;
684
Thomas Zimmermanna8d6e1272022-05-17 13:33:26 +0200685 __drm_gem_vram_plane_helper_cleanup_fb(plane, old_state, fb->format->num_planes);
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200686}
687EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
688
689/*
690 * Helpers for struct drm_simple_display_pipe_funcs
691 */
692
693/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100694 * drm_gem_vram_simple_display_pipe_prepare_fb() - Implements &struct
695 * drm_simple_display_pipe_funcs.prepare_fb
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200696 * @pipe: a simple display pipe
697 * @new_state: the plane's new state
698 *
699 * During plane updates, this function pins the GEM VRAM
700 * objects of the plane's new framebuffer to VRAM. Call
701 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
702 *
703 * Returns:
704 * 0 on success, or
705 * a negative errno code otherwise.
706 */
707int drm_gem_vram_simple_display_pipe_prepare_fb(
708 struct drm_simple_display_pipe *pipe,
709 struct drm_plane_state *new_state)
710{
711 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
712}
713EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
714
715/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100716 * drm_gem_vram_simple_display_pipe_cleanup_fb() - Implements &struct
717 * drm_simple_display_pipe_funcs.cleanup_fb
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200718 * @pipe: a simple display pipe
719 * @old_state: the plane's old state
720 *
721 * During plane updates, this function unpins the GEM VRAM
722 * objects of the plane's old framebuffer from VRAM. Complements
723 * drm_gem_vram_simple_display_pipe_prepare_fb().
724 */
725void drm_gem_vram_simple_display_pipe_cleanup_fb(
726 struct drm_simple_display_pipe *pipe,
727 struct drm_plane_state *old_state)
728{
729 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
730}
731EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
732
733/*
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200734 * PRIME helpers
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200735 */
736
737/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100738 * drm_gem_vram_object_pin() - Implements &struct drm_gem_object_funcs.pin
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200739 * @gem: The GEM object to pin
740 *
741 * Returns:
742 * 0 on success, or
743 * a negative errno code otherwise.
744 */
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200745static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200746{
747 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
748
Thomas Zimmermann94dee3b2024-02-27 11:14:49 +0100749 /*
750 * Fbdev console emulation is the use case of these PRIME
Thomas Zimmermanna6c34642019-06-13 09:30:33 +0200751 * helpers. This may involve updating a hardware buffer from
752 * a shadow FB. We pin the buffer to it's current location
753 * (either video RAM or system memory) to prevent it from
754 * being relocated during the update operation. If you require
755 * the buffer to be pinned to VRAM, implement a callback that
756 * sets the flags accordingly.
757 */
Thomas Zimmermanna7802782024-02-27 11:14:56 +0100758 return drm_gem_vram_pin_locked(gbo, 0);
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200759}
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200760
761/**
Anna-Maria Behnsena0abb822024-01-22 10:31:51 +0100762 * drm_gem_vram_object_unpin() - Implements &struct drm_gem_object_funcs.unpin
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200763 * @gem: The GEM object to unpin
764 */
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200765static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200766{
767 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
768
Thomas Zimmermann94dee3b2024-02-27 11:14:49 +0100769 drm_gem_vram_unpin_locked(gbo);
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200770}
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200771
772/**
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100773 * drm_gem_vram_object_vmap() -
774 * Implements &struct drm_gem_object_funcs.vmap
775 * @gem: The GEM object to map
776 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
777 * store.
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200778 *
779 * Returns:
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100780 * 0 on success, or a negative error code otherwise.
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200781 */
Lucas De Marchi7938f422022-02-04 09:05:41 -0800782static int drm_gem_vram_object_vmap(struct drm_gem_object *gem,
783 struct iosys_map *map)
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200784{
785 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200786
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100787 return drm_gem_vram_vmap(gbo, map);
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200788}
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200789
790/**
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100791 * drm_gem_vram_object_vunmap() -
792 * Implements &struct drm_gem_object_funcs.vunmap
793 * @gem: The GEM object to unmap
794 * @map: Kernel virtual address where the VRAM GEM object was mapped
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200795 */
Lucas De Marchi7938f422022-02-04 09:05:41 -0800796static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
797 struct iosys_map *map)
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200798{
799 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
800
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100801 drm_gem_vram_vunmap(gbo, map);
Thomas Zimmermann1f460b42019-05-08 10:26:15 +0200802}
Thomas Zimmermann31070a82019-07-02 13:50:08 +0200803
804/*
805 * GEM object funcs
806 */
807
808static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
Thomas Zimmermann0ccf52b2019-07-02 13:50:12 +0200809 .free = drm_gem_vram_object_free,
810 .pin = drm_gem_vram_object_pin,
811 .unpin = drm_gem_vram_object_unpin,
812 .vmap = drm_gem_vram_object_vmap,
Gerd Hoffmann527f6d92019-09-04 07:47:36 +0200813 .vunmap = drm_gem_vram_object_vunmap,
Gerd Hoffmann5a8b7cf2019-10-16 13:52:01 +0200814 .mmap = drm_gem_ttm_mmap,
Gerd Hoffmann527f6d92019-09-04 07:47:36 +0200815 .print_info = drm_gem_ttm_print_info,
Thomas Zimmermann31070a82019-07-02 13:50:08 +0200816};
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200817
818/*
819 * VRAM memory manager
820 */
821
822/*
823 * TTM TT
824 */
825
Christian König8af8a102020-10-01 14:51:40 +0200826static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200827{
828 ttm_tt_fini(tt);
829 kfree(tt);
830}
831
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200832/*
833 * TTM BO device
834 */
835
836static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
837 uint32_t page_flags)
838{
839 struct ttm_tt *tt;
840 int ret;
841
842 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
843 if (!tt)
844 return NULL;
845
Ramalingam Ce36764e2022-04-01 18:07:49 +0530846 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached, 0);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200847 if (ret < 0)
848 goto err_ttm_tt_init;
849
850 return tt;
851
852err_ttm_tt_init:
853 kfree(tt);
854 return NULL;
855}
856
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200857static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
858 struct ttm_placement *placement)
859{
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200860 struct drm_gem_vram_object *gbo;
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200861
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200862 /* TTM may pass BOs that are not GEM VRAM BOs. */
863 if (!drm_is_gem_vram(bo))
864 return;
865
866 gbo = drm_gem_vram_of_bo(bo);
867
868 drm_gem_vram_bo_driver_evict_flags(gbo, placement);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200869}
870
Dave Airlie6a6e5982020-10-21 14:40:29 +1000871static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200872{
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200873 struct drm_gem_vram_object *gbo;
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200874
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200875 /* TTM may pass BOs that are not GEM VRAM BOs. */
876 if (!drm_is_gem_vram(bo))
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200877 return;
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200878
879 gbo = drm_gem_vram_of_bo(bo);
880
Christian König10073772021-02-11 11:04:23 +0100881 drm_gem_vram_bo_driver_move_notify(gbo);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200882}
883
Dave Airlie2b8283f2020-10-06 10:06:43 +1000884static int bo_driver_move(struct ttm_buffer_object *bo,
885 bool evict,
886 struct ttm_operation_ctx *ctx,
Dave Airlieebdf5652020-10-29 13:58:52 +1000887 struct ttm_resource *new_mem,
888 struct ttm_place *hop)
Dave Airlie2b8283f2020-10-06 10:06:43 +1000889{
890 struct drm_gem_vram_object *gbo;
891
Matthew Auldc604d312023-02-08 14:53:16 +0000892 if (!bo->resource) {
893 if (new_mem->mem_type != TTM_PL_SYSTEM) {
894 hop->mem_type = TTM_PL_SYSTEM;
895 hop->flags = TTM_PL_FLAG_TEMPORARY;
896 return -EMULTIHOP;
897 }
898
899 ttm_bo_move_null(bo, new_mem);
900 return 0;
901 }
902
Dave Airlie2b8283f2020-10-06 10:06:43 +1000903 gbo = drm_gem_vram_of_bo(bo);
904
905 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
906}
907
Christian König8af8a102020-10-01 14:51:40 +0200908static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
Dave Airlie29661412020-08-04 12:56:32 +1000909 struct ttm_resource *mem)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200910{
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200911 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
912
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200913 switch (mem->mem_type) {
914 case TTM_PL_SYSTEM: /* nothing to do */
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200915 break;
916 case TTM_PL_VRAM:
Christian König54d04ea2020-09-07 13:44:36 +0200917 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200918 mem->bus.is_iomem = true;
Christian König1cf65c42020-09-30 11:17:44 +0200919 mem->bus.caching = ttm_write_combined;
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200920 break;
921 default:
922 return -EINVAL;
923 }
924
925 return 0;
926}
927
Christian König8af8a102020-10-01 14:51:40 +0200928static struct ttm_device_funcs bo_driver = {
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200929 .ttm_tt_create = bo_driver_ttm_tt_create,
Dave Airlie84693832020-09-08 06:46:26 +1000930 .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200931 .eviction_valuable = ttm_bo_eviction_valuable,
932 .evict_flags = bo_driver_evict_flags,
Dave Airlie2b8283f2020-10-06 10:06:43 +1000933 .move = bo_driver_move,
Dave Airlie6a6e5982020-10-21 14:40:29 +1000934 .delete_mem_notify = bo_driver_delete_mem_notify,
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200935 .io_mem_reserve = bo_driver_io_mem_reserve,
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200936};
937
938/*
939 * struct drm_vram_mm
940 */
941
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200942static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
943{
Maíra Canal6fd80722022-12-19 09:06:16 -0300944 struct drm_debugfs_entry *entry = m->private;
945 struct drm_vram_mm *vmm = entry->dev->vram_mm;
Dave Airlie9de59bc2020-08-04 12:56:31 +1000946 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200947 struct drm_printer p = drm_seq_file_printer(m);
948
Dave Airlie9de59bc2020-08-04 12:56:31 +1000949 ttm_resource_manager_debug(man, &p);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200950 return 0;
951}
952
Maíra Canal6fd80722022-12-19 09:06:16 -0300953static const struct drm_debugfs_info drm_vram_mm_debugfs_list[] = {
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200954 { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
955};
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200956
957/**
958 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
959 *
960 * @minor: drm minor device.
961 *
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200962 */
Wambui Karuga7ce844712020-03-10 16:31:21 +0300963void drm_vram_mm_debugfs_init(struct drm_minor *minor)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200964{
Maíra Canal6fd80722022-12-19 09:06:16 -0300965 drm_debugfs_add_files(minor->dev, drm_vram_mm_debugfs_list,
966 ARRAY_SIZE(drm_vram_mm_debugfs_list));
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200967}
968EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
969
Thomas Zimmermannc30b2252019-09-11 13:09:09 +0200970static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
971 uint64_t vram_base, size_t vram_size)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200972{
973 int ret;
974
975 vmm->vram_base = vram_base;
976 vmm->vram_size = vram_size;
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200977
Christian König8af8a102020-10-01 14:51:40 +0200978 ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200979 dev->anon_inode->i_mapping,
980 dev->vma_offset_manager,
Christian Königee5d2a82020-10-24 13:10:28 +0200981 false, true);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200982 if (ret)
983 return ret;
984
Dave Airlie37205892020-08-04 12:56:19 +1000985 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
Christian König0fe438c2020-09-11 15:06:53 +0200986 false, vram_size >> PAGE_SHIFT);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200987 if (ret)
988 return ret;
989
990 return 0;
991}
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200992
Thomas Zimmermannc30b2252019-09-11 13:09:09 +0200993static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200994{
Dave Airlie37205892020-08-04 12:56:19 +1000995 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
Christian König8af8a102020-10-01 14:51:40 +0200996 ttm_device_fini(&vmm->bdev);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200997}
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200998
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200999/*
1000 * Helpers for integration with struct drm_device
1001 */
1002
Thomas Zimmermann9aa02672021-07-02 09:54:34 +02001003static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base,
1004 size_t vram_size)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02001005{
1006 int ret;
1007
1008 if (WARN_ON(dev->vram_mm))
1009 return dev->vram_mm;
1010
1011 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1012 if (!dev->vram_mm)
1013 return ERR_PTR(-ENOMEM);
1014
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +02001015 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02001016 if (ret)
1017 goto err_kfree;
1018
1019 return dev->vram_mm;
1020
1021err_kfree:
1022 kfree(dev->vram_mm);
1023 dev->vram_mm = NULL;
1024 return ERR_PTR(ret);
1025}
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02001026
Thomas Zimmermann9aa02672021-07-02 09:54:34 +02001027static void drm_vram_helper_release_mm(struct drm_device *dev)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02001028{
1029 if (!dev->vram_mm)
1030 return;
1031
1032 drm_vram_mm_cleanup(dev->vram_mm);
1033 kfree(dev->vram_mm);
1034 dev->vram_mm = NULL;
1035}
Thomas Zimmermann80f7c3f2020-02-03 16:52:55 +01001036
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +02001037static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1038{
1039 drm_vram_helper_release_mm(dev);
1040}
1041
1042/**
1043 * drmm_vram_helper_init - Initializes a device's instance of
1044 * &struct drm_vram_mm
1045 * @dev: the DRM device
1046 * @vram_base: the base address of the video memory
1047 * @vram_size: the size of the video memory in bytes
1048 *
1049 * Creates a new instance of &struct drm_vram_mm and stores it in
1050 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1051 * up as part of device cleanup. Calling this function multiple times
1052 * will generate an error message.
1053 *
1054 * Returns:
1055 * 0 on success, or a negative errno code otherwise.
1056 */
1057int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1058 size_t vram_size)
1059{
1060 struct drm_vram_mm *vram_mm;
1061
1062 if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1063 return 0;
1064
1065 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1066 if (IS_ERR(vram_mm))
1067 return PTR_ERR(vram_mm);
1068 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1069}
1070EXPORT_SYMBOL(drmm_vram_helper_init);
1071
Thomas Zimmermann80f7c3f2020-02-03 16:52:55 +01001072/*
1073 * Mode-config helpers
1074 */
1075
1076static enum drm_mode_status
1077drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1078 const struct drm_display_mode *mode,
1079 unsigned long max_bpp)
1080{
1081 struct drm_vram_mm *vmm = dev->vram_mm;
1082 unsigned long fbsize, fbpages, max_fbpages;
1083
1084 if (WARN_ON(!dev->vram_mm))
1085 return MODE_BAD;
1086
1087 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1088
1089 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1090 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1091
1092 if (fbpages > max_fbpages)
1093 return MODE_MEM;
1094
1095 return MODE_OK;
1096}
1097
1098/**
1099 * drm_vram_helper_mode_valid - Tests if a display mode's
1100 * framebuffer fits into the available video memory.
1101 * @dev: the DRM device
1102 * @mode: the mode to test
1103 *
1104 * This function tests if enough video memory is available for using the
1105 * specified display mode. Atomic modesetting requires importing the
1106 * designated framebuffer into video memory before evicting the active
1107 * one. Hence, any framebuffer may consume at most half of the available
1108 * VRAM. Display modes that require a larger framebuffer can not be used,
1109 * even if the CRTC does support them. Each framebuffer is assumed to
1110 * have 32-bit color depth.
1111 *
1112 * Note:
1113 * The function can only test if the display mode is supported in
1114 * general. If there are too many framebuffers pinned to video memory,
1115 * a display mode may still not be usable in practice. The color depth of
1116 * 32-bit fits all current use case. A more flexible test can be added
1117 * when necessary.
1118 *
1119 * Returns:
1120 * MODE_OK if the display mode is supported, or an error code of type
1121 * enum drm_mode_status otherwise.
1122 */
1123enum drm_mode_status
1124drm_vram_helper_mode_valid(struct drm_device *dev,
1125 const struct drm_display_mode *mode)
1126{
1127 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1128
1129 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1130}
1131EXPORT_SYMBOL(drm_vram_helper_mode_valid);
Thomas Zimmermannb22b51a2020-03-31 10:12:38 +02001132
1133MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1134MODULE_LICENSE("GPL");