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