2 * Copyright 2020 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Christian König
25 #include <linux/debugfs.h>
26 #include <linux/io-mapping.h>
27 #include <linux/iosys-map.h>
28 #include <linux/scatterlist.h>
30 #include <drm/ttm/ttm_bo.h>
31 #include <drm/ttm/ttm_placement.h>
32 #include <drm/ttm/ttm_resource.h>
34 #include <drm/drm_util.h>
37 * ttm_lru_bulk_move_init - initialize a bulk move structure
38 * @bulk: the structure to init
40 * For now just memset the structure to zero.
42 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
44 memset(bulk, 0, sizeof(*bulk));
46 EXPORT_SYMBOL(ttm_lru_bulk_move_init);
49 * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail.
51 * @bulk: bulk move structure
53 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
54 * resource order never changes. Should be called with &ttm_device.lru_lock held.
56 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
60 for (i = 0; i < TTM_NUM_MEM_TYPES; ++i) {
61 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
62 struct ttm_lru_bulk_move_pos *pos = &bulk->pos[i][j];
63 struct ttm_resource_manager *man;
68 lockdep_assert_held(&pos->first->bo->bdev->lru_lock);
69 dma_resv_assert_held(pos->first->bo->base.resv);
70 dma_resv_assert_held(pos->last->bo->base.resv);
72 man = ttm_manager_type(pos->first->bo->bdev, i);
73 list_bulk_move_tail(&man->lru[j], &pos->first->lru,
78 EXPORT_SYMBOL(ttm_lru_bulk_move_tail);
80 /* Return the bulk move pos object for this resource */
81 static struct ttm_lru_bulk_move_pos *
82 ttm_lru_bulk_move_pos(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
84 return &bulk->pos[res->mem_type][res->bo->priority];
87 /* Move the resource to the tail of the bulk move range */
88 static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
89 struct ttm_resource *res)
91 if (pos->last != res) {
92 if (pos->first == res)
93 pos->first = list_next_entry(res, lru);
94 list_move(&res->lru, &pos->last->lru);
99 /* Add the resource to a bulk_move cursor */
100 static void ttm_lru_bulk_move_add(struct ttm_lru_bulk_move *bulk,
101 struct ttm_resource *res)
103 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
109 WARN_ON(pos->first->bo->base.resv != res->bo->base.resv);
110 ttm_lru_bulk_move_pos_tail(pos, res);
114 /* Remove the resource from a bulk_move range */
115 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
116 struct ttm_resource *res)
118 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
120 if (unlikely(WARN_ON(!pos->first || !pos->last) ||
121 (pos->first == res && pos->last == res))) {
124 } else if (pos->first == res) {
125 pos->first = list_next_entry(res, lru);
126 } else if (pos->last == res) {
127 pos->last = list_prev_entry(res, lru);
129 list_move(&res->lru, &pos->last->lru);
133 /* Add the resource to a bulk move if the BO is configured for it */
134 void ttm_resource_add_bulk_move(struct ttm_resource *res,
135 struct ttm_buffer_object *bo)
137 if (bo->bulk_move && !bo->pin_count)
138 ttm_lru_bulk_move_add(bo->bulk_move, res);
141 /* Remove the resource from a bulk move if the BO is configured for it */
142 void ttm_resource_del_bulk_move(struct ttm_resource *res,
143 struct ttm_buffer_object *bo)
145 if (bo->bulk_move && !bo->pin_count)
146 ttm_lru_bulk_move_del(bo->bulk_move, res);
149 /* Move a resource to the LRU or bulk tail */
150 void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
152 struct ttm_buffer_object *bo = res->bo;
153 struct ttm_device *bdev = bo->bdev;
155 lockdep_assert_held(&bo->bdev->lru_lock);
158 list_move_tail(&res->lru, &bdev->pinned);
160 } else if (bo->bulk_move) {
161 struct ttm_lru_bulk_move_pos *pos =
162 ttm_lru_bulk_move_pos(bo->bulk_move, res);
164 ttm_lru_bulk_move_pos_tail(pos, res);
166 struct ttm_resource_manager *man;
168 man = ttm_manager_type(bdev, res->mem_type);
169 list_move_tail(&res->lru, &man->lru[bo->priority]);
174 * ttm_resource_init - resource object constructure
175 * @bo: buffer object this resources is allocated for
176 * @place: placement of the resource
177 * @res: the resource object to inistilize
179 * Initialize a new resource object. Counterpart of ttm_resource_fini().
181 void ttm_resource_init(struct ttm_buffer_object *bo,
182 const struct ttm_place *place,
183 struct ttm_resource *res)
185 struct ttm_resource_manager *man;
188 res->size = bo->base.size;
189 res->mem_type = place->mem_type;
190 res->placement = place->flags;
191 res->bus.addr = NULL;
193 res->bus.is_iomem = false;
194 res->bus.caching = ttm_cached;
197 man = ttm_manager_type(bo->bdev, place->mem_type);
198 spin_lock(&bo->bdev->lru_lock);
200 list_add_tail(&res->lru, &bo->bdev->pinned);
202 list_add_tail(&res->lru, &man->lru[bo->priority]);
203 man->usage += res->size;
204 spin_unlock(&bo->bdev->lru_lock);
206 EXPORT_SYMBOL(ttm_resource_init);
209 * ttm_resource_fini - resource destructor
210 * @man: the resource manager this resource belongs to
211 * @res: the resource to clean up
213 * Should be used by resource manager backends to clean up the TTM resource
214 * objects before freeing the underlying structure. Makes sure the resource is
215 * removed from the LRU before destruction.
216 * Counterpart of ttm_resource_init().
218 void ttm_resource_fini(struct ttm_resource_manager *man,
219 struct ttm_resource *res)
221 struct ttm_device *bdev = man->bdev;
223 spin_lock(&bdev->lru_lock);
224 list_del_init(&res->lru);
225 man->usage -= res->size;
226 spin_unlock(&bdev->lru_lock);
228 EXPORT_SYMBOL(ttm_resource_fini);
230 int ttm_resource_alloc(struct ttm_buffer_object *bo,
231 const struct ttm_place *place,
232 struct ttm_resource **res_ptr)
234 struct ttm_resource_manager *man =
235 ttm_manager_type(bo->bdev, place->mem_type);
238 ret = man->func->alloc(man, bo, place, res_ptr);
242 spin_lock(&bo->bdev->lru_lock);
243 ttm_resource_add_bulk_move(*res_ptr, bo);
244 spin_unlock(&bo->bdev->lru_lock);
247 EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
249 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
251 struct ttm_resource_manager *man;
256 spin_lock(&bo->bdev->lru_lock);
257 ttm_resource_del_bulk_move(*res, bo);
258 spin_unlock(&bo->bdev->lru_lock);
259 man = ttm_manager_type(bo->bdev, (*res)->mem_type);
260 man->func->free(man, *res);
263 EXPORT_SYMBOL(ttm_resource_free);
266 * ttm_resource_intersects - test for intersection
268 * @bdev: TTM device structure
269 * @res: The resource to test
270 * @place: The placement to test
271 * @size: How many bytes the new allocation needs.
273 * Test if @res intersects with @place and @size. Used for testing if evictions
274 * are valueable or not.
276 * Returns true if the res placement intersects with @place and @size.
278 bool ttm_resource_intersects(struct ttm_device *bdev,
279 struct ttm_resource *res,
280 const struct ttm_place *place,
283 struct ttm_resource_manager *man;
288 man = ttm_manager_type(bdev, res->mem_type);
289 if (!place || !man->func->intersects)
292 return man->func->intersects(man, res, place, size);
296 * ttm_resource_compatible - check if resource is compatible with placement
298 * @res: the resource to check
299 * @placement: the placement to check against
300 * @evicting: true if the caller is doing evictions
302 * Returns true if the placement is compatible.
304 bool ttm_resource_compatible(struct ttm_resource *res,
305 struct ttm_placement *placement,
308 struct ttm_buffer_object *bo = res->bo;
309 struct ttm_device *bdev = bo->bdev;
312 if (res->placement & TTM_PL_FLAG_TEMPORARY)
315 for (i = 0; i < placement->num_placement; i++) {
316 const struct ttm_place *place = &placement->placement[i];
317 struct ttm_resource_manager *man;
319 if (res->mem_type != place->mem_type)
322 if (place->flags & (evicting ? TTM_PL_FLAG_DESIRED :
323 TTM_PL_FLAG_FALLBACK))
326 if (place->flags & TTM_PL_FLAG_CONTIGUOUS &&
327 !(res->placement & TTM_PL_FLAG_CONTIGUOUS))
330 man = ttm_manager_type(bdev, res->mem_type);
331 if (man->func->compatible &&
332 !man->func->compatible(man, res, place, bo->base.size))
340 void ttm_resource_set_bo(struct ttm_resource *res,
341 struct ttm_buffer_object *bo)
343 spin_lock(&bo->bdev->lru_lock);
345 spin_unlock(&bo->bdev->lru_lock);
349 * ttm_resource_manager_init
351 * @man: memory manager object to init
352 * @bdev: ttm device this manager belongs to
353 * @size: size of managed resources in arbitrary units
355 * Initialise core parts of a manager object.
357 void ttm_resource_manager_init(struct ttm_resource_manager *man,
358 struct ttm_device *bdev,
363 spin_lock_init(&man->move_lock);
368 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
369 INIT_LIST_HEAD(&man->lru[i]);
372 EXPORT_SYMBOL(ttm_resource_manager_init);
375 * ttm_resource_manager_evict_all
377 * @bdev - device to use
378 * @man - manager to use
380 * Evict all the objects out of a memory manager until it is empty.
381 * Part of memory manager cleanup sequence.
383 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
384 struct ttm_resource_manager *man)
386 struct ttm_operation_ctx ctx = {
387 .interruptible = false,
388 .no_wait_gpu = false,
391 struct dma_fence *fence;
396 * Can't use standard list traversal since we're unlocking.
399 spin_lock(&bdev->lru_lock);
400 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
401 while (!list_empty(&man->lru[i])) {
402 spin_unlock(&bdev->lru_lock);
403 ret = ttm_mem_evict_first(bdev, man, NULL, &ctx,
407 spin_lock(&bdev->lru_lock);
410 spin_unlock(&bdev->lru_lock);
412 spin_lock(&man->move_lock);
413 fence = dma_fence_get(man->move);
414 spin_unlock(&man->move_lock);
417 ret = dma_fence_wait(fence, false);
418 dma_fence_put(fence);
425 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
428 * ttm_resource_manager_usage
430 * @man: A memory manager object.
432 * Return how many resources are currently used.
434 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
438 spin_lock(&man->bdev->lru_lock);
440 spin_unlock(&man->bdev->lru_lock);
443 EXPORT_SYMBOL(ttm_resource_manager_usage);
446 * ttm_resource_manager_debug
448 * @man: manager type to dump.
449 * @p: printer to use for debug.
451 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
452 struct drm_printer *p)
454 drm_printf(p, " use_type: %d\n", man->use_type);
455 drm_printf(p, " use_tt: %d\n", man->use_tt);
456 drm_printf(p, " size: %llu\n", man->size);
457 drm_printf(p, " usage: %llu\n", ttm_resource_manager_usage(man));
458 if (man->func->debug)
459 man->func->debug(man, p);
461 EXPORT_SYMBOL(ttm_resource_manager_debug);
464 * ttm_resource_manager_first
466 * @man: resource manager to iterate over
467 * @cursor: cursor to record the position
469 * Returns the first resource from the resource manager.
471 struct ttm_resource *
472 ttm_resource_manager_first(struct ttm_resource_manager *man,
473 struct ttm_resource_cursor *cursor)
475 struct ttm_resource *res;
477 lockdep_assert_held(&man->bdev->lru_lock);
479 for (cursor->priority = 0; cursor->priority < TTM_MAX_BO_PRIORITY;
481 list_for_each_entry(res, &man->lru[cursor->priority], lru)
488 * ttm_resource_manager_next
490 * @man: resource manager to iterate over
491 * @cursor: cursor to record the position
492 * @res: the current resource pointer
494 * Returns the next resource from the resource manager.
496 struct ttm_resource *
497 ttm_resource_manager_next(struct ttm_resource_manager *man,
498 struct ttm_resource_cursor *cursor,
499 struct ttm_resource *res)
501 lockdep_assert_held(&man->bdev->lru_lock);
503 list_for_each_entry_continue(res, &man->lru[cursor->priority], lru)
506 for (++cursor->priority; cursor->priority < TTM_MAX_BO_PRIORITY;
508 list_for_each_entry(res, &man->lru[cursor->priority], lru)
514 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
515 struct iosys_map *dmap,
518 struct ttm_kmap_iter_iomap *iter_io =
519 container_of(iter, typeof(*iter_io), base);
523 while (i >= iter_io->cache.end) {
524 iter_io->cache.sg = iter_io->cache.sg ?
525 sg_next(iter_io->cache.sg) : iter_io->st->sgl;
526 iter_io->cache.i = iter_io->cache.end;
527 iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >>
529 iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) -
533 if (i < iter_io->cache.i) {
534 iter_io->cache.end = 0;
535 iter_io->cache.sg = NULL;
539 addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs +
540 (((resource_size_t)i - iter_io->cache.i)
542 iosys_map_set_vaddr_iomem(dmap, addr);
545 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter,
546 struct iosys_map *map)
548 io_mapping_unmap_local(map->vaddr_iomem);
551 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = {
552 .map_local = ttm_kmap_iter_iomap_map_local,
553 .unmap_local = ttm_kmap_iter_iomap_unmap_local,
558 * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap
559 * @iter_io: The struct ttm_kmap_iter_iomap to initialize.
560 * @iomap: The struct io_mapping representing the underlying linear io_memory.
561 * @st: sg_table into @iomap, representing the memory of the struct
563 * @start: Offset that needs to be subtracted from @st to make
564 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
566 * Return: Pointer to the embedded struct ttm_kmap_iter.
568 struct ttm_kmap_iter *
569 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
570 struct io_mapping *iomap,
572 resource_size_t start)
574 iter_io->base.ops = &ttm_kmap_iter_io_ops;
575 iter_io->iomap = iomap;
577 iter_io->start = start;
578 memset(&iter_io->cache, 0, sizeof(iter_io->cache));
580 return &iter_io->base;
582 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init);
585 * DOC: Linear io iterator
587 * This code should die in the not too near future. Best would be if we could
588 * make io-mapping use memremap for all io memory, and have memremap
589 * implement a kmap_local functionality. We could then strip a huge amount of
590 * code. These linear io iterators are implemented to mimic old functionality,
591 * and they don't use kmap_local semantics at all internally. Rather ioremap or
592 * friends, and at least on 32-bit they add global TLB flushes and points
596 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter,
597 struct iosys_map *dmap,
600 struct ttm_kmap_iter_linear_io *iter_io =
601 container_of(iter, typeof(*iter_io), base);
603 *dmap = iter_io->dmap;
604 iosys_map_incr(dmap, i * PAGE_SIZE);
607 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = {
608 .map_local = ttm_kmap_iter_linear_io_map_local,
613 * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory
614 * @iter_io: The iterator to initialize
615 * @bdev: The TTM device
616 * @mem: The ttm resource representing the iomap.
618 * This function is for internal TTM use only. It sets up a memcpy kmap iterator
619 * pointing at a linear chunk of io memory.
621 * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on
624 struct ttm_kmap_iter *
625 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
626 struct ttm_device *bdev,
627 struct ttm_resource *mem)
631 ret = ttm_mem_io_reserve(bdev, mem);
634 if (!mem->bus.is_iomem) {
640 iosys_map_set_vaddr(&iter_io->dmap, mem->bus.addr);
641 iter_io->needs_unmap = false;
643 iter_io->needs_unmap = true;
644 memset(&iter_io->dmap, 0, sizeof(iter_io->dmap));
645 if (mem->bus.caching == ttm_write_combined)
646 iosys_map_set_vaddr_iomem(&iter_io->dmap,
647 ioremap_wc(mem->bus.offset,
649 else if (mem->bus.caching == ttm_cached)
650 iosys_map_set_vaddr(&iter_io->dmap,
651 memremap(mem->bus.offset, mem->size,
656 /* If uncached requested or if mapping cached or wc failed */
657 if (iosys_map_is_null(&iter_io->dmap))
658 iosys_map_set_vaddr_iomem(&iter_io->dmap,
659 ioremap(mem->bus.offset,
662 if (iosys_map_is_null(&iter_io->dmap)) {
668 iter_io->base.ops = &ttm_kmap_iter_linear_io_ops;
669 return &iter_io->base;
672 ttm_mem_io_free(bdev, mem);
678 * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory
679 * @iter_io: The iterator to initialize
680 * @bdev: The TTM device
681 * @mem: The ttm resource representing the iomap.
683 * This function is for internal TTM use only. It cleans up a memcpy kmap
684 * iterator initialized by ttm_kmap_iter_linear_io_init.
687 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
688 struct ttm_device *bdev,
689 struct ttm_resource *mem)
691 if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) {
692 if (iter_io->dmap.is_iomem)
693 iounmap(iter_io->dmap.vaddr_iomem);
695 memunmap(iter_io->dmap.vaddr);
698 ttm_mem_io_free(bdev, mem);
701 #if defined(CONFIG_DEBUG_FS)
703 static int ttm_resource_manager_show(struct seq_file *m, void *unused)
705 struct ttm_resource_manager *man =
706 (struct ttm_resource_manager *)m->private;
707 struct drm_printer p = drm_seq_file_printer(m);
708 ttm_resource_manager_debug(man, &p);
711 DEFINE_SHOW_ATTRIBUTE(ttm_resource_manager);
716 * ttm_resource_manager_create_debugfs - Create debugfs entry for specified
718 * @man: The TTM resource manager for which the debugfs stats file be creates
719 * @parent: debugfs directory in which the file will reside
720 * @name: The filename to create.
722 * This function setups up a debugfs file that can be used to look
723 * at debug statistics of the specified ttm_resource_manager.
725 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
726 struct dentry * parent,
729 #if defined(CONFIG_DEBUG_FS)
730 debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops);
733 EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);