1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3 *
4 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28 /*
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 */
31
32 #include <linux/vmalloc.h>
33
34 #include <drm/ttm/ttm_bo.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <drm/ttm/ttm_tt.h>
37
38 #include <drm/drm_cache.h>
39
40 #include <linux/android_kabi.h>
41 ANDROID_KABI_DECLONLY(dma_buf);
42 ANDROID_KABI_DECLONLY(dma_buf_attachment);
43
44 struct ttm_transfer_obj {
45 struct ttm_buffer_object base;
46 struct ttm_buffer_object *bo;
47 };
48
ttm_mem_io_reserve(struct ttm_device * bdev,struct ttm_resource * mem)49 int ttm_mem_io_reserve(struct ttm_device *bdev,
50 struct ttm_resource *mem)
51 {
52 if (mem->bus.offset || mem->bus.addr)
53 return 0;
54
55 mem->bus.is_iomem = false;
56 if (!bdev->funcs->io_mem_reserve)
57 return 0;
58
59 return bdev->funcs->io_mem_reserve(bdev, mem);
60 }
61
ttm_mem_io_free(struct ttm_device * bdev,struct ttm_resource * mem)62 void ttm_mem_io_free(struct ttm_device *bdev,
63 struct ttm_resource *mem)
64 {
65 if (!mem)
66 return;
67
68 if (!mem->bus.offset && !mem->bus.addr)
69 return;
70
71 if (bdev->funcs->io_mem_free)
72 bdev->funcs->io_mem_free(bdev, mem);
73
74 mem->bus.offset = 0;
75 mem->bus.addr = NULL;
76 }
77
78 /**
79 * ttm_move_memcpy - Helper to perform a memcpy ttm move operation.
80 * @clear: Whether to clear rather than copy.
81 * @num_pages: Number of pages of the operation.
82 * @dst_iter: A struct ttm_kmap_iter representing the destination resource.
83 * @src_iter: A struct ttm_kmap_iter representing the source resource.
84 *
85 * This function is intended to be able to move out async under a
86 * dma-fence if desired.
87 */
ttm_move_memcpy(bool clear,u32 num_pages,struct ttm_kmap_iter * dst_iter,struct ttm_kmap_iter * src_iter)88 void ttm_move_memcpy(bool clear,
89 u32 num_pages,
90 struct ttm_kmap_iter *dst_iter,
91 struct ttm_kmap_iter *src_iter)
92 {
93 const struct ttm_kmap_iter_ops *dst_ops = dst_iter->ops;
94 const struct ttm_kmap_iter_ops *src_ops = src_iter->ops;
95 struct iosys_map src_map, dst_map;
96 pgoff_t i;
97
98 /* Single TTM move. NOP */
99 if (dst_ops->maps_tt && src_ops->maps_tt)
100 return;
101
102 /* Don't move nonexistent data. Clear destination instead. */
103 if (clear) {
104 for (i = 0; i < num_pages; ++i) {
105 dst_ops->map_local(dst_iter, &dst_map, i);
106 if (dst_map.is_iomem)
107 memset_io(dst_map.vaddr_iomem, 0, PAGE_SIZE);
108 else
109 memset(dst_map.vaddr, 0, PAGE_SIZE);
110 if (dst_ops->unmap_local)
111 dst_ops->unmap_local(dst_iter, &dst_map);
112 }
113 return;
114 }
115
116 for (i = 0; i < num_pages; ++i) {
117 dst_ops->map_local(dst_iter, &dst_map, i);
118 src_ops->map_local(src_iter, &src_map, i);
119
120 drm_memcpy_from_wc(&dst_map, &src_map, PAGE_SIZE);
121
122 if (src_ops->unmap_local)
123 src_ops->unmap_local(src_iter, &src_map);
124 if (dst_ops->unmap_local)
125 dst_ops->unmap_local(dst_iter, &dst_map);
126 }
127 }
128 EXPORT_SYMBOL(ttm_move_memcpy);
129
130 /**
131 * ttm_bo_move_memcpy
132 *
133 * @bo: A pointer to a struct ttm_buffer_object.
134 * @ctx: operation context
135 * @dst_mem: struct ttm_resource indicating where to move.
136 *
137 * Fallback move function for a mappable buffer object in mappable memory.
138 * The function will, if successful,
139 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
140 * and update the (@bo)->mem placement flags. If unsuccessful, the old
141 * data remains untouched, and it's up to the caller to free the
142 * memory space indicated by @new_mem.
143 * Returns:
144 * !0: Failure.
145 */
ttm_bo_move_memcpy(struct ttm_buffer_object * bo,struct ttm_operation_ctx * ctx,struct ttm_resource * dst_mem)146 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
147 struct ttm_operation_ctx *ctx,
148 struct ttm_resource *dst_mem)
149 {
150 struct ttm_device *bdev = bo->bdev;
151 struct ttm_resource_manager *dst_man =
152 ttm_manager_type(bo->bdev, dst_mem->mem_type);
153 struct ttm_tt *ttm = bo->ttm;
154 struct ttm_resource *src_mem = bo->resource;
155 struct ttm_resource_manager *src_man;
156 union {
157 struct ttm_kmap_iter_tt tt;
158 struct ttm_kmap_iter_linear_io io;
159 } _dst_iter, _src_iter;
160 struct ttm_kmap_iter *dst_iter, *src_iter;
161 bool clear;
162 int ret = 0;
163
164 if (WARN_ON(!src_mem))
165 return -EINVAL;
166
167 src_man = ttm_manager_type(bdev, src_mem->mem_type);
168 if (ttm && ((ttm->page_flags & TTM_TT_FLAG_SWAPPED) ||
169 dst_man->use_tt)) {
170 ret = ttm_tt_populate(bdev, ttm, ctx);
171 if (ret)
172 return ret;
173 }
174
175 dst_iter = ttm_kmap_iter_linear_io_init(&_dst_iter.io, bdev, dst_mem);
176 if (PTR_ERR(dst_iter) == -EINVAL && dst_man->use_tt)
177 dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, bo->ttm);
178 if (IS_ERR(dst_iter))
179 return PTR_ERR(dst_iter);
180
181 src_iter = ttm_kmap_iter_linear_io_init(&_src_iter.io, bdev, src_mem);
182 if (PTR_ERR(src_iter) == -EINVAL && src_man->use_tt)
183 src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm);
184 if (IS_ERR(src_iter)) {
185 ret = PTR_ERR(src_iter);
186 goto out_src_iter;
187 }
188
189 clear = src_iter->ops->maps_tt && (!ttm || !ttm_tt_is_populated(ttm));
190 if (!(clear && ttm && !(ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC)))
191 ttm_move_memcpy(clear, PFN_UP(dst_mem->size), dst_iter, src_iter);
192
193 if (!src_iter->ops->maps_tt)
194 ttm_kmap_iter_linear_io_fini(&_src_iter.io, bdev, src_mem);
195 ttm_bo_move_sync_cleanup(bo, dst_mem);
196
197 out_src_iter:
198 if (!dst_iter->ops->maps_tt)
199 ttm_kmap_iter_linear_io_fini(&_dst_iter.io, bdev, dst_mem);
200
201 return ret;
202 }
203 EXPORT_SYMBOL(ttm_bo_move_memcpy);
204
ttm_transfered_destroy(struct ttm_buffer_object * bo)205 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
206 {
207 struct ttm_transfer_obj *fbo;
208
209 fbo = container_of(bo, struct ttm_transfer_obj, base);
210 dma_resv_fini(&fbo->base.base._resv);
211 ttm_bo_put(fbo->bo);
212 kfree(fbo);
213 }
214
215 /**
216 * ttm_buffer_object_transfer
217 *
218 * @bo: A pointer to a struct ttm_buffer_object.
219 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
220 * holding the data of @bo with the old placement.
221 *
222 * This is a utility function that may be called after an accelerated move
223 * has been scheduled. A new buffer object is created as a placeholder for
224 * the old data while it's being copied. When that buffer object is idle,
225 * it can be destroyed, releasing the space of the old placement.
226 * Returns:
227 * !0: Failure.
228 */
229
ttm_buffer_object_transfer(struct ttm_buffer_object * bo,struct ttm_buffer_object ** new_obj)230 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
231 struct ttm_buffer_object **new_obj)
232 {
233 struct ttm_transfer_obj *fbo;
234 int ret;
235
236 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
237 if (!fbo)
238 return -ENOMEM;
239
240 fbo->base = *bo;
241
242 /**
243 * Fix up members that we shouldn't copy directly:
244 * TODO: Explicit member copy would probably be better here.
245 */
246
247 atomic_inc(&ttm_glob.bo_count);
248 drm_vma_node_reset(&fbo->base.base.vma_node);
249
250 kref_init(&fbo->base.kref);
251 fbo->base.destroy = &ttm_transfered_destroy;
252 fbo->base.pin_count = 0;
253 if (bo->type != ttm_bo_type_sg)
254 fbo->base.base.resv = &fbo->base.base._resv;
255
256 dma_resv_init(&fbo->base.base._resv);
257 fbo->base.base.dev = NULL;
258 ret = dma_resv_trylock(&fbo->base.base._resv);
259 WARN_ON(!ret);
260
261 ret = dma_resv_reserve_fences(&fbo->base.base._resv, 1);
262 if (ret) {
263 dma_resv_unlock(&fbo->base.base._resv);
264 kfree(fbo);
265 return ret;
266 }
267
268 if (fbo->base.resource) {
269 ttm_resource_set_bo(fbo->base.resource, &fbo->base);
270 bo->resource = NULL;
271 ttm_bo_set_bulk_move(&fbo->base, NULL);
272 } else {
273 fbo->base.bulk_move = NULL;
274 }
275
276 ttm_bo_get(bo);
277 fbo->bo = bo;
278
279 ttm_bo_move_to_lru_tail_unlocked(&fbo->base);
280
281 *new_obj = &fbo->base;
282 return 0;
283 }
284
285 /**
286 * ttm_io_prot
287 *
288 * @bo: ttm buffer object
289 * @res: ttm resource object
290 * @tmp: Page protection flag for a normal, cached mapping.
291 *
292 * Utility function that returns the pgprot_t that should be used for
293 * setting up a PTE with the caching model indicated by @c_state.
294 */
ttm_io_prot(struct ttm_buffer_object * bo,struct ttm_resource * res,pgprot_t tmp)295 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
296 pgprot_t tmp)
297 {
298 struct ttm_resource_manager *man;
299 enum ttm_caching caching;
300
301 man = ttm_manager_type(bo->bdev, res->mem_type);
302 if (man->use_tt) {
303 caching = bo->ttm->caching;
304 if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
305 tmp = pgprot_decrypted(tmp);
306 } else {
307 caching = res->bus.caching;
308 }
309
310 return ttm_prot_from_caching(caching, tmp);
311 }
312 EXPORT_SYMBOL(ttm_io_prot);
313
ttm_bo_ioremap(struct ttm_buffer_object * bo,unsigned long offset,unsigned long size,struct ttm_bo_kmap_obj * map)314 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
315 unsigned long offset,
316 unsigned long size,
317 struct ttm_bo_kmap_obj *map)
318 {
319 struct ttm_resource *mem = bo->resource;
320
321 if (bo->resource->bus.addr) {
322 map->bo_kmap_type = ttm_bo_map_premapped;
323 map->virtual = ((u8 *)bo->resource->bus.addr) + offset;
324 } else {
325 resource_size_t res = bo->resource->bus.offset + offset;
326
327 map->bo_kmap_type = ttm_bo_map_iomap;
328 if (mem->bus.caching == ttm_write_combined)
329 map->virtual = ioremap_wc(res, size);
330 #ifdef CONFIG_X86
331 else if (mem->bus.caching == ttm_cached)
332 map->virtual = ioremap_cache(res, size);
333 #endif
334 else
335 map->virtual = ioremap(res, size);
336 }
337 return (!map->virtual) ? -ENOMEM : 0;
338 }
339
ttm_bo_kmap_ttm(struct ttm_buffer_object * bo,unsigned long start_page,unsigned long num_pages,struct ttm_bo_kmap_obj * map)340 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
341 unsigned long start_page,
342 unsigned long num_pages,
343 struct ttm_bo_kmap_obj *map)
344 {
345 struct ttm_resource *mem = bo->resource;
346 struct ttm_operation_ctx ctx = {
347 .interruptible = false,
348 .no_wait_gpu = false
349 };
350 struct ttm_tt *ttm = bo->ttm;
351 struct ttm_resource_manager *man =
352 ttm_manager_type(bo->bdev, bo->resource->mem_type);
353 pgprot_t prot;
354 int ret;
355
356 BUG_ON(!ttm);
357
358 ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
359 if (ret)
360 return ret;
361
362 if (num_pages == 1 && ttm->caching == ttm_cached &&
363 !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
364 /*
365 * We're mapping a single page, and the desired
366 * page protection is consistent with the bo.
367 */
368
369 map->bo_kmap_type = ttm_bo_map_kmap;
370 map->page = ttm->pages[start_page];
371 map->virtual = kmap(map->page);
372 } else {
373 /*
374 * We need to use vmap to get the desired page protection
375 * or to make the buffer object look contiguous.
376 */
377 prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
378 map->bo_kmap_type = ttm_bo_map_vmap;
379 map->virtual = vmap(ttm->pages + start_page, num_pages,
380 0, prot);
381 }
382 return (!map->virtual) ? -ENOMEM : 0;
383 }
384
385 /**
386 * ttm_bo_kmap
387 *
388 * @bo: The buffer object.
389 * @start_page: The first page to map.
390 * @num_pages: Number of pages to map.
391 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
392 *
393 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
394 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
395 * used to obtain a virtual address to the data.
396 *
397 * Returns
398 * -ENOMEM: Out of memory.
399 * -EINVAL: Invalid range.
400 */
ttm_bo_kmap(struct ttm_buffer_object * bo,unsigned long start_page,unsigned long num_pages,struct ttm_bo_kmap_obj * map)401 int ttm_bo_kmap(struct ttm_buffer_object *bo,
402 unsigned long start_page, unsigned long num_pages,
403 struct ttm_bo_kmap_obj *map)
404 {
405 unsigned long offset, size;
406 int ret;
407
408 map->virtual = NULL;
409 map->bo = bo;
410 if (num_pages > PFN_UP(bo->resource->size))
411 return -EINVAL;
412 if ((start_page + num_pages) > PFN_UP(bo->resource->size))
413 return -EINVAL;
414
415 ret = ttm_mem_io_reserve(bo->bdev, bo->resource);
416 if (ret)
417 return ret;
418 if (!bo->resource->bus.is_iomem) {
419 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
420 } else {
421 offset = start_page << PAGE_SHIFT;
422 size = num_pages << PAGE_SHIFT;
423 return ttm_bo_ioremap(bo, offset, size, map);
424 }
425 }
426 EXPORT_SYMBOL(ttm_bo_kmap);
427
428 /**
429 * ttm_bo_kunmap
430 *
431 * @map: Object describing the map to unmap.
432 *
433 * Unmaps a kernel map set up by ttm_bo_kmap.
434 */
ttm_bo_kunmap(struct ttm_bo_kmap_obj * map)435 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
436 {
437 if (!map->virtual)
438 return;
439 switch (map->bo_kmap_type) {
440 case ttm_bo_map_iomap:
441 iounmap(map->virtual);
442 break;
443 case ttm_bo_map_vmap:
444 vunmap(map->virtual);
445 break;
446 case ttm_bo_map_kmap:
447 kunmap(map->page);
448 break;
449 case ttm_bo_map_premapped:
450 break;
451 default:
452 BUG();
453 }
454 ttm_mem_io_free(map->bo->bdev, map->bo->resource);
455 map->virtual = NULL;
456 map->page = NULL;
457 }
458 EXPORT_SYMBOL(ttm_bo_kunmap);
459
460 /**
461 * ttm_bo_vmap
462 *
463 * @bo: The buffer object.
464 * @map: pointer to a struct iosys_map representing the map.
465 *
466 * Sets up a kernel virtual mapping, using ioremap or vmap to the
467 * data in the buffer object. The parameter @map returns the virtual
468 * address as struct iosys_map. Unmap the buffer with ttm_bo_vunmap().
469 *
470 * Returns
471 * -ENOMEM: Out of memory.
472 * -EINVAL: Invalid range.
473 */
ttm_bo_vmap(struct ttm_buffer_object * bo,struct iosys_map * map)474 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map)
475 {
476 struct ttm_resource *mem = bo->resource;
477 int ret;
478
479 dma_resv_assert_held(bo->base.resv);
480
481 ret = ttm_mem_io_reserve(bo->bdev, mem);
482 if (ret)
483 return ret;
484
485 if (mem->bus.is_iomem) {
486 void __iomem *vaddr_iomem;
487
488 if (mem->bus.addr)
489 vaddr_iomem = (void __iomem *)mem->bus.addr;
490 else if (mem->bus.caching == ttm_write_combined)
491 vaddr_iomem = ioremap_wc(mem->bus.offset,
492 bo->base.size);
493 #ifdef CONFIG_X86
494 else if (mem->bus.caching == ttm_cached)
495 vaddr_iomem = ioremap_cache(mem->bus.offset,
496 bo->base.size);
497 #endif
498 else
499 vaddr_iomem = ioremap(mem->bus.offset, bo->base.size);
500
501 if (!vaddr_iomem)
502 return -ENOMEM;
503
504 iosys_map_set_vaddr_iomem(map, vaddr_iomem);
505
506 } else {
507 struct ttm_operation_ctx ctx = {
508 .interruptible = false,
509 .no_wait_gpu = false
510 };
511 struct ttm_tt *ttm = bo->ttm;
512 pgprot_t prot;
513 void *vaddr;
514
515 ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
516 if (ret)
517 return ret;
518
519 /*
520 * We need to use vmap to get the desired page protection
521 * or to make the buffer object look contiguous.
522 */
523 prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
524 vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot);
525 if (!vaddr)
526 return -ENOMEM;
527
528 iosys_map_set_vaddr(map, vaddr);
529 }
530
531 return 0;
532 }
533 EXPORT_SYMBOL(ttm_bo_vmap);
534
535 /**
536 * ttm_bo_vunmap
537 *
538 * @bo: The buffer object.
539 * @map: Object describing the map to unmap.
540 *
541 * Unmaps a kernel map set up by ttm_bo_vmap().
542 */
ttm_bo_vunmap(struct ttm_buffer_object * bo,struct iosys_map * map)543 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map)
544 {
545 struct ttm_resource *mem = bo->resource;
546
547 dma_resv_assert_held(bo->base.resv);
548
549 if (iosys_map_is_null(map))
550 return;
551
552 if (!map->is_iomem)
553 vunmap(map->vaddr);
554 else if (!mem->bus.addr)
555 iounmap(map->vaddr_iomem);
556 iosys_map_clear(map);
557
558 ttm_mem_io_free(bo->bdev, bo->resource);
559 }
560 EXPORT_SYMBOL(ttm_bo_vunmap);
561
ttm_bo_wait_free_node(struct ttm_buffer_object * bo,bool dst_use_tt)562 static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
563 bool dst_use_tt)
564 {
565 long ret;
566
567 ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
568 false, 15 * HZ);
569 if (ret == 0)
570 return -EBUSY;
571 if (ret < 0)
572 return ret;
573
574 if (!dst_use_tt)
575 ttm_bo_tt_destroy(bo);
576 ttm_resource_free(bo, &bo->resource);
577 return 0;
578 }
579
ttm_bo_move_to_ghost(struct ttm_buffer_object * bo,struct dma_fence * fence,bool dst_use_tt)580 static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
581 struct dma_fence *fence,
582 bool dst_use_tt)
583 {
584 struct ttm_buffer_object *ghost_obj;
585 int ret;
586
587 /**
588 * This should help pipeline ordinary buffer moves.
589 *
590 * Hang old buffer memory on a new buffer object,
591 * and leave it to be released when the GPU
592 * operation has completed.
593 */
594
595 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
596 if (ret)
597 return ret;
598
599 dma_resv_add_fence(&ghost_obj->base._resv, fence,
600 DMA_RESV_USAGE_KERNEL);
601
602 /**
603 * If we're not moving to fixed memory, the TTM object
604 * needs to stay alive. Otherwhise hang it on the ghost
605 * bo to be unbound and destroyed.
606 */
607
608 if (dst_use_tt)
609 ghost_obj->ttm = NULL;
610 else
611 bo->ttm = NULL;
612
613 dma_resv_unlock(&ghost_obj->base._resv);
614 ttm_bo_put(ghost_obj);
615 return 0;
616 }
617
ttm_bo_move_pipeline_evict(struct ttm_buffer_object * bo,struct dma_fence * fence)618 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
619 struct dma_fence *fence)
620 {
621 struct ttm_device *bdev = bo->bdev;
622 struct ttm_resource_manager *from;
623
624 from = ttm_manager_type(bdev, bo->resource->mem_type);
625
626 /**
627 * BO doesn't have a TTM we need to bind/unbind. Just remember
628 * this eviction and free up the allocation
629 */
630 spin_lock(&from->move_lock);
631 if (!from->move || dma_fence_is_later(fence, from->move)) {
632 dma_fence_put(from->move);
633 from->move = dma_fence_get(fence);
634 }
635 spin_unlock(&from->move_lock);
636
637 ttm_resource_free(bo, &bo->resource);
638 }
639
640 /**
641 * ttm_bo_move_accel_cleanup - cleanup helper for hw copies
642 *
643 * @bo: A pointer to a struct ttm_buffer_object.
644 * @fence: A fence object that signals when moving is complete.
645 * @evict: This is an evict move. Don't return until the buffer is idle.
646 * @pipeline: evictions are to be pipelined.
647 * @new_mem: struct ttm_resource indicating where to move.
648 *
649 * Accelerated move function to be called when an accelerated move
650 * has been scheduled. The function will create a new temporary buffer object
651 * representing the old placement, and put the sync object on both buffer
652 * objects. After that the newly created buffer object is unref'd to be
653 * destroyed when the move is complete. This will help pipeline
654 * buffer moves.
655 */
ttm_bo_move_accel_cleanup(struct ttm_buffer_object * bo,struct dma_fence * fence,bool evict,bool pipeline,struct ttm_resource * new_mem)656 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
657 struct dma_fence *fence,
658 bool evict,
659 bool pipeline,
660 struct ttm_resource *new_mem)
661 {
662 struct ttm_device *bdev = bo->bdev;
663 struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->resource->mem_type);
664 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
665 int ret = 0;
666
667 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
668 if (!evict)
669 ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
670 else if (!from->use_tt && pipeline)
671 ttm_bo_move_pipeline_evict(bo, fence);
672 else
673 ret = ttm_bo_wait_free_node(bo, man->use_tt);
674
675 if (ret)
676 return ret;
677
678 ttm_bo_assign_mem(bo, new_mem);
679
680 return 0;
681 }
682 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
683
684 /**
685 * ttm_bo_move_sync_cleanup - cleanup by waiting for the move to finish
686 *
687 * @bo: A pointer to a struct ttm_buffer_object.
688 * @new_mem: struct ttm_resource indicating where to move.
689 *
690 * Special case of ttm_bo_move_accel_cleanup where the bo is guaranteed
691 * by the caller to be idle. Typically used after memcpy buffer moves.
692 */
ttm_bo_move_sync_cleanup(struct ttm_buffer_object * bo,struct ttm_resource * new_mem)693 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
694 struct ttm_resource *new_mem)
695 {
696 struct ttm_device *bdev = bo->bdev;
697 struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
698 int ret;
699
700 ret = ttm_bo_wait_free_node(bo, man->use_tt);
701 if (WARN_ON(ret))
702 return;
703
704 ttm_bo_assign_mem(bo, new_mem);
705 }
706 EXPORT_SYMBOL(ttm_bo_move_sync_cleanup);
707
708 /**
709 * ttm_bo_pipeline_gutting - purge the contents of a bo
710 * @bo: The buffer object
711 *
712 * Purge the contents of a bo, async if the bo is not idle.
713 * After a successful call, the bo is left unpopulated in
714 * system placement. The function may wait uninterruptible
715 * for idle on OOM.
716 *
717 * Return: 0 if successful, negative error code on failure.
718 */
ttm_bo_pipeline_gutting(struct ttm_buffer_object * bo)719 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
720 {
721 struct ttm_buffer_object *ghost;
722 struct ttm_tt *ttm;
723 int ret;
724
725 /* If already idle, no need for ghost object dance. */
726 if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP)) {
727 if (!bo->ttm) {
728 /* See comment below about clearing. */
729 ret = ttm_tt_create(bo, true);
730 if (ret)
731 return ret;
732 } else {
733 ttm_tt_unpopulate(bo->bdev, bo->ttm);
734 if (bo->type == ttm_bo_type_device)
735 ttm_tt_mark_for_clear(bo->ttm);
736 }
737 ttm_resource_free(bo, &bo->resource);
738 return 0;
739 }
740
741 /*
742 * We need an unpopulated ttm_tt after giving our current one,
743 * if any, to the ghost object. And we can't afford to fail
744 * creating one *after* the operation. If the bo subsequently gets
745 * resurrected, make sure it's cleared (if ttm_bo_type_device)
746 * to avoid leaking sensitive information to user-space.
747 */
748
749 ttm = bo->ttm;
750 bo->ttm = NULL;
751 ret = ttm_tt_create(bo, true);
752 swap(bo->ttm, ttm);
753 if (ret)
754 return ret;
755
756 ret = ttm_buffer_object_transfer(bo, &ghost);
757 if (ret)
758 goto error_destroy_tt;
759
760 ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
761 /* Last resort, wait for the BO to be idle when we are OOM */
762 if (ret) {
763 dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
764 false, MAX_SCHEDULE_TIMEOUT);
765 }
766
767 dma_resv_unlock(&ghost->base._resv);
768 ttm_bo_put(ghost);
769 bo->ttm = ttm;
770 return 0;
771
772 error_destroy_tt:
773 ttm_tt_destroy(bo->bdev, ttm);
774 return ret;
775 }
776
ttm_lru_walk_trylock(struct ttm_lru_walk * walk,struct ttm_buffer_object * bo,bool * needs_unlock)777 static bool ttm_lru_walk_trylock(struct ttm_lru_walk *walk,
778 struct ttm_buffer_object *bo,
779 bool *needs_unlock)
780 {
781 struct ttm_operation_ctx *ctx = walk->ctx;
782
783 *needs_unlock = false;
784
785 if (dma_resv_trylock(bo->base.resv)) {
786 *needs_unlock = true;
787 return true;
788 }
789
790 if (bo->base.resv == ctx->resv && ctx->allow_res_evict) {
791 dma_resv_assert_held(bo->base.resv);
792 return true;
793 }
794
795 return false;
796 }
797
ttm_lru_walk_ticketlock(struct ttm_lru_walk * walk,struct ttm_buffer_object * bo,bool * needs_unlock)798 static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk,
799 struct ttm_buffer_object *bo,
800 bool *needs_unlock)
801 {
802 struct dma_resv *resv = bo->base.resv;
803 int ret;
804
805 if (walk->ctx->interruptible)
806 ret = dma_resv_lock_interruptible(resv, walk->ticket);
807 else
808 ret = dma_resv_lock(resv, walk->ticket);
809
810 if (!ret) {
811 *needs_unlock = true;
812 /*
813 * Only a single ticketlock per loop. Ticketlocks are prone
814 * to return -EDEADLK causing the eviction to fail, so
815 * after waiting for the ticketlock, revert back to
816 * trylocking for this walk.
817 */
818 walk->ticket = NULL;
819 } else if (ret == -EDEADLK) {
820 /* Caller needs to exit the ww transaction. */
821 ret = -ENOSPC;
822 }
823
824 return ret;
825 }
826
ttm_lru_walk_unlock(struct ttm_buffer_object * bo,bool locked)827 static void ttm_lru_walk_unlock(struct ttm_buffer_object *bo, bool locked)
828 {
829 if (locked)
830 dma_resv_unlock(bo->base.resv);
831 }
832
833 /**
834 * ttm_lru_walk_for_evict() - Perform a LRU list walk, with actions taken on
835 * valid items.
836 * @walk: describe the walks and actions taken
837 * @bdev: The TTM device.
838 * @man: The struct ttm_resource manager whose LRU lists we're walking.
839 * @target: The end condition for the walk.
840 *
841 * The LRU lists of @man are walk, and for each struct ttm_resource encountered,
842 * the corresponding ttm_buffer_object is locked and taken a reference on, and
843 * the LRU lock is dropped. the LRU lock may be dropped before locking and, in
844 * that case, it's verified that the item actually remains on the LRU list after
845 * the lock, and that the buffer object didn't switch resource in between.
846 *
847 * With a locked object, the actions indicated by @walk->process_bo are
848 * performed, and after that, the bo is unlocked, the refcount dropped and the
849 * next struct ttm_resource is processed. Here, the walker relies on
850 * TTM's restartable LRU list implementation.
851 *
852 * Typically @walk->process_bo() would return the number of pages evicted,
853 * swapped or shrunken, so that when the total exceeds @target, or when the
854 * LRU list has been walked in full, iteration is terminated. It's also terminated
855 * on error. Note that the definition of @target is done by the caller, it
856 * could have a different meaning than the number of pages.
857 *
858 * Note that the way dma_resv individualization is done, locking needs to be done
859 * either with the LRU lock held (trylocking only) or with a reference on the
860 * object.
861 *
862 * Return: The progress made towards target or negative error code on error.
863 */
ttm_lru_walk_for_evict(struct ttm_lru_walk * walk,struct ttm_device * bdev,struct ttm_resource_manager * man,s64 target)864 s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
865 struct ttm_resource_manager *man, s64 target)
866 {
867 struct ttm_resource_cursor cursor;
868 struct ttm_resource *res;
869 s64 progress = 0;
870 s64 lret;
871
872 spin_lock(&bdev->lru_lock);
873 ttm_resource_manager_for_each_res(man, &cursor, res) {
874 struct ttm_buffer_object *bo = res->bo;
875 bool bo_needs_unlock = false;
876 bool bo_locked = false;
877 int mem_type;
878
879 /*
880 * Attempt a trylock before taking a reference on the bo,
881 * since if we do it the other way around, and the trylock fails,
882 * we need to drop the lru lock to put the bo.
883 */
884 if (ttm_lru_walk_trylock(walk, bo, &bo_needs_unlock))
885 bo_locked = true;
886 else if (!walk->ticket || walk->ctx->no_wait_gpu ||
887 walk->trylock_only)
888 continue;
889
890 if (!ttm_bo_get_unless_zero(bo)) {
891 ttm_lru_walk_unlock(bo, bo_needs_unlock);
892 continue;
893 }
894
895 mem_type = res->mem_type;
896 spin_unlock(&bdev->lru_lock);
897
898 lret = 0;
899 if (!bo_locked)
900 lret = ttm_lru_walk_ticketlock(walk, bo, &bo_needs_unlock);
901
902 /*
903 * Note that in between the release of the lru lock and the
904 * ticketlock, the bo may have switched resource,
905 * and also memory type, since the resource may have been
906 * freed and allocated again with a different memory type.
907 * In that case, just skip it.
908 */
909 if (!lret && bo->resource && bo->resource->mem_type == mem_type)
910 lret = walk->ops->process_bo(walk, bo);
911
912 ttm_lru_walk_unlock(bo, bo_needs_unlock);
913 ttm_bo_put(bo);
914 if (lret == -EBUSY || lret == -EALREADY)
915 lret = 0;
916 progress = (lret < 0) ? lret : progress + lret;
917
918 spin_lock(&bdev->lru_lock);
919 if (progress < 0 || progress >= target)
920 break;
921 }
922 ttm_resource_cursor_fini(&cursor);
923 spin_unlock(&bdev->lru_lock);
924
925 return progress;
926 }
927