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