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