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