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/iosys-map.h>
26 #include <linux/io-mapping.h>
27 #include <linux/scatterlist.h>
29 #include <drm/ttm/ttm_bo.h>
30 #include <drm/ttm/ttm_placement.h>
31 #include <drm/ttm/ttm_resource.h>
33 #include <drm/drm_util.h>
36 * ttm_lru_bulk_move_init - initialize a bulk move structure
37 * @bulk: the structure to init
39 * For now just memset the structure to zero.
41 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
43 memset(bulk, 0, sizeof(*bulk));
45 EXPORT_SYMBOL(ttm_lru_bulk_move_init);
48 * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail.
50 * @bulk: bulk move structure
52 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
53 * resource order never changes. Should be called with &ttm_device.lru_lock held.
55 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
59 for (i = 0; i < TTM_NUM_MEM_TYPES; ++i) {
60 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
61 struct ttm_lru_bulk_move_pos *pos = &bulk->pos[i][j];
62 struct ttm_resource_manager *man;
67 lockdep_assert_held(&pos->first->bo->bdev->lru_lock);
68 dma_resv_assert_held(pos->first->bo->base.resv);
69 dma_resv_assert_held(pos->last->bo->base.resv);
71 man = ttm_manager_type(pos->first->bo->bdev, i);
72 list_bulk_move_tail(&man->lru[j], &pos->first->lru,
77 EXPORT_SYMBOL(ttm_lru_bulk_move_tail);
79 /* Return the bulk move pos object for this resource */
80 static struct ttm_lru_bulk_move_pos *
81 ttm_lru_bulk_move_pos(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
83 return &bulk->pos[res->mem_type][res->bo->priority];
86 /* Move the resource to the tail of the bulk move range */
87 static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
88 struct ttm_resource *res)
90 if (pos->last != res) {
91 if (pos->first == res)
92 pos->first = list_next_entry(res, lru);
93 list_move(&res->lru, &pos->last->lru);
98 /* Add the resource to a bulk_move cursor */
99 static void ttm_lru_bulk_move_add(struct ttm_lru_bulk_move *bulk,
100 struct ttm_resource *res)
102 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
108 ttm_lru_bulk_move_pos_tail(pos, res);
112 /* Remove the resource from a bulk_move range */
113 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
114 struct ttm_resource *res)
116 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
118 if (unlikely(WARN_ON(!pos->first || !pos->last) ||
119 (pos->first == res && pos->last == res))) {
122 } else if (pos->first == res) {
123 pos->first = list_next_entry(res, lru);
124 } else if (pos->last == res) {
125 pos->last = list_prev_entry(res, lru);
127 list_move(&res->lru, &pos->last->lru);
131 /* Add the resource to a bulk move if the BO is configured for it */
132 void ttm_resource_add_bulk_move(struct ttm_resource *res,
133 struct ttm_buffer_object *bo)
135 if (bo->bulk_move && !bo->pin_count)
136 ttm_lru_bulk_move_add(bo->bulk_move, res);
139 /* Remove the resource from a bulk move if the BO is configured for it */
140 void ttm_resource_del_bulk_move(struct ttm_resource *res,
141 struct ttm_buffer_object *bo)
143 if (bo->bulk_move && !bo->pin_count)
144 ttm_lru_bulk_move_del(bo->bulk_move, res);
147 /* Move a resource to the LRU or bulk tail */
148 void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
150 struct ttm_buffer_object *bo = res->bo;
151 struct ttm_device *bdev = bo->bdev;
153 lockdep_assert_held(&bo->bdev->lru_lock);
156 list_move_tail(&res->lru, &bdev->pinned);
158 } else if (bo->bulk_move) {
159 struct ttm_lru_bulk_move_pos *pos =
160 ttm_lru_bulk_move_pos(bo->bulk_move, res);
162 ttm_lru_bulk_move_pos_tail(pos, res);
164 struct ttm_resource_manager *man;
166 man = ttm_manager_type(bdev, res->mem_type);
167 list_move_tail(&res->lru, &man->lru[bo->priority]);
172 * ttm_resource_init - resource object constructure
173 * @bo: buffer object this resources is allocated for
174 * @place: placement of the resource
175 * @res: the resource object to inistilize
177 * Initialize a new resource object. Counterpart of ttm_resource_fini().
179 void ttm_resource_init(struct ttm_buffer_object *bo,
180 const struct ttm_place *place,
181 struct ttm_resource *res)
183 struct ttm_resource_manager *man;
186 res->size = bo->base.size;
187 res->mem_type = place->mem_type;
188 res->placement = place->flags;
189 res->bus.addr = NULL;
191 res->bus.is_iomem = false;
192 res->bus.caching = ttm_cached;
195 man = ttm_manager_type(bo->bdev, place->mem_type);
196 spin_lock(&bo->bdev->lru_lock);
198 list_add_tail(&res->lru, &bo->bdev->pinned);
200 list_add_tail(&res->lru, &man->lru[bo->priority]);
201 man->usage += res->size;
202 spin_unlock(&bo->bdev->lru_lock);
204 EXPORT_SYMBOL(ttm_resource_init);
207 * ttm_resource_fini - resource destructor
208 * @man: the resource manager this resource belongs to
209 * @res: the resource to clean up
211 * Should be used by resource manager backends to clean up the TTM resource
212 * objects before freeing the underlying structure. Makes sure the resource is
213 * removed from the LRU before destruction.
214 * Counterpart of ttm_resource_init().
216 void ttm_resource_fini(struct ttm_resource_manager *man,
217 struct ttm_resource *res)
219 struct ttm_device *bdev = man->bdev;
221 spin_lock(&bdev->lru_lock);
222 list_del_init(&res->lru);
223 man->usage -= res->size;
224 spin_unlock(&bdev->lru_lock);
226 EXPORT_SYMBOL(ttm_resource_fini);
228 int ttm_resource_alloc(struct ttm_buffer_object *bo,
229 const struct ttm_place *place,
230 struct ttm_resource **res_ptr)
232 struct ttm_resource_manager *man =
233 ttm_manager_type(bo->bdev, place->mem_type);
236 ret = man->func->alloc(man, bo, place, res_ptr);
240 spin_lock(&bo->bdev->lru_lock);
241 ttm_resource_add_bulk_move(*res_ptr, bo);
242 spin_unlock(&bo->bdev->lru_lock);
245 EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
247 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
249 struct ttm_resource_manager *man;
254 spin_lock(&bo->bdev->lru_lock);
255 ttm_resource_del_bulk_move(*res, bo);
256 spin_unlock(&bo->bdev->lru_lock);
257 man = ttm_manager_type(bo->bdev, (*res)->mem_type);
258 man->func->free(man, *res);
261 EXPORT_SYMBOL(ttm_resource_free);
264 * ttm_resource_intersects - test for intersection
266 * @bdev: TTM device structure
267 * @res: The resource to test
268 * @place: The placement to test
269 * @size: How many bytes the new allocation needs.
271 * Test if @res intersects with @place and @size. Used for testing if evictions
272 * are valueable or not.
274 * Returns true if the res placement intersects with @place and @size.
276 bool ttm_resource_intersects(struct ttm_device *bdev,
277 struct ttm_resource *res,
278 const struct ttm_place *place,
281 struct ttm_resource_manager *man;
286 man = ttm_manager_type(bdev, res->mem_type);
287 if (!place || !man->func->intersects)
290 return man->func->intersects(man, res, place, size);
294 * ttm_resource_compatible - check if resource is compatible with placement
296 * @res: the resource to check
297 * @placement: the placement to check against
299 * Returns true if the placement is compatible.
301 bool ttm_resource_compatible(struct ttm_resource *res,
302 struct ttm_placement *placement)
304 struct ttm_buffer_object *bo = res->bo;
305 struct ttm_device *bdev = bo->bdev;
308 if (res->placement & TTM_PL_FLAG_TEMPORARY)
311 for (i = 0; i < placement->num_placement; i++) {
312 const struct ttm_place *place = &placement->placement[i];
313 struct ttm_resource_manager *man;
315 if (res->mem_type != place->mem_type)
318 man = ttm_manager_type(bdev, res->mem_type);
319 if (man->func->compatible &&
320 !man->func->compatible(man, res, place, bo->base.size))
323 if ((!(place->flags & TTM_PL_FLAG_CONTIGUOUS) ||
324 (res->placement & TTM_PL_FLAG_CONTIGUOUS)))
330 void ttm_resource_set_bo(struct ttm_resource *res,
331 struct ttm_buffer_object *bo)
333 spin_lock(&bo->bdev->lru_lock);
335 spin_unlock(&bo->bdev->lru_lock);
339 * ttm_resource_manager_init
341 * @man: memory manager object to init
342 * @bdev: ttm device this manager belongs to
343 * @size: size of managed resources in arbitrary units
345 * Initialise core parts of a manager object.
347 void ttm_resource_manager_init(struct ttm_resource_manager *man,
348 struct ttm_device *bdev,
353 spin_lock_init(&man->move_lock);
358 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
359 INIT_LIST_HEAD(&man->lru[i]);
362 EXPORT_SYMBOL(ttm_resource_manager_init);
365 * ttm_resource_manager_evict_all
367 * @bdev - device to use
368 * @man - manager to use
370 * Evict all the objects out of a memory manager until it is empty.
371 * Part of memory manager cleanup sequence.
373 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
374 struct ttm_resource_manager *man)
376 struct ttm_operation_ctx ctx = {
377 .interruptible = false,
378 .no_wait_gpu = false,
381 struct dma_fence *fence;
386 * Can't use standard list traversal since we're unlocking.
389 spin_lock(&bdev->lru_lock);
390 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
391 while (!list_empty(&man->lru[i])) {
392 spin_unlock(&bdev->lru_lock);
393 ret = ttm_mem_evict_first(bdev, man, NULL, &ctx,
397 spin_lock(&bdev->lru_lock);
400 spin_unlock(&bdev->lru_lock);
402 spin_lock(&man->move_lock);
403 fence = dma_fence_get(man->move);
404 spin_unlock(&man->move_lock);
407 ret = dma_fence_wait(fence, false);
408 dma_fence_put(fence);
415 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
418 * ttm_resource_manager_usage
420 * @man: A memory manager object.
422 * Return how many resources are currently used.
424 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
428 spin_lock(&man->bdev->lru_lock);
430 spin_unlock(&man->bdev->lru_lock);
433 EXPORT_SYMBOL(ttm_resource_manager_usage);
436 * ttm_resource_manager_debug
438 * @man: manager type to dump.
439 * @p: printer to use for debug.
441 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
442 struct drm_printer *p)
444 drm_printf(p, " use_type: %d\n", man->use_type);
445 drm_printf(p, " use_tt: %d\n", man->use_tt);
446 drm_printf(p, " size: %llu\n", man->size);
447 drm_printf(p, " usage: %llu\n", ttm_resource_manager_usage(man));
448 if (man->func->debug)
449 man->func->debug(man, p);
451 EXPORT_SYMBOL(ttm_resource_manager_debug);
454 * ttm_resource_manager_first
456 * @man: resource manager to iterate over
457 * @cursor: cursor to record the position
459 * Returns the first resource from the resource manager.
461 struct ttm_resource *
462 ttm_resource_manager_first(struct ttm_resource_manager *man,
463 struct ttm_resource_cursor *cursor)
465 struct ttm_resource *res;
467 lockdep_assert_held(&man->bdev->lru_lock);
469 for (cursor->priority = 0; cursor->priority < TTM_MAX_BO_PRIORITY;
471 list_for_each_entry(res, &man->lru[cursor->priority], lru)
478 * ttm_resource_manager_next
480 * @man: resource manager to iterate over
481 * @cursor: cursor to record the position
482 * @res: the current resource pointer
484 * Returns the next resource from the resource manager.
486 struct ttm_resource *
487 ttm_resource_manager_next(struct ttm_resource_manager *man,
488 struct ttm_resource_cursor *cursor,
489 struct ttm_resource *res)
491 lockdep_assert_held(&man->bdev->lru_lock);
493 list_for_each_entry_continue(res, &man->lru[cursor->priority], lru)
496 for (++cursor->priority; cursor->priority < TTM_MAX_BO_PRIORITY;
498 list_for_each_entry(res, &man->lru[cursor->priority], lru)
504 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
505 struct iosys_map *dmap,
508 struct ttm_kmap_iter_iomap *iter_io =
509 container_of(iter, typeof(*iter_io), base);
513 while (i >= iter_io->cache.end) {
514 iter_io->cache.sg = iter_io->cache.sg ?
515 sg_next(iter_io->cache.sg) : iter_io->st->sgl;
516 iter_io->cache.i = iter_io->cache.end;
517 iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >>
519 iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) -
523 if (i < iter_io->cache.i) {
524 iter_io->cache.end = 0;
525 iter_io->cache.sg = NULL;
529 addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs +
530 (((resource_size_t)i - iter_io->cache.i)
532 iosys_map_set_vaddr_iomem(dmap, addr);
535 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter,
536 struct iosys_map *map)
538 io_mapping_unmap_local(map->vaddr_iomem);
541 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = {
542 .map_local = ttm_kmap_iter_iomap_map_local,
543 .unmap_local = ttm_kmap_iter_iomap_unmap_local,
548 * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap
549 * @iter_io: The struct ttm_kmap_iter_iomap to initialize.
550 * @iomap: The struct io_mapping representing the underlying linear io_memory.
551 * @st: sg_table into @iomap, representing the memory of the struct
553 * @start: Offset that needs to be subtracted from @st to make
554 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
556 * Return: Pointer to the embedded struct ttm_kmap_iter.
558 struct ttm_kmap_iter *
559 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
560 struct io_mapping *iomap,
562 resource_size_t start)
564 iter_io->base.ops = &ttm_kmap_iter_io_ops;
565 iter_io->iomap = iomap;
567 iter_io->start = start;
568 memset(&iter_io->cache, 0, sizeof(iter_io->cache));
570 return &iter_io->base;
572 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init);
575 * DOC: Linear io iterator
577 * This code should die in the not too near future. Best would be if we could
578 * make io-mapping use memremap for all io memory, and have memremap
579 * implement a kmap_local functionality. We could then strip a huge amount of
580 * code. These linear io iterators are implemented to mimic old functionality,
581 * and they don't use kmap_local semantics at all internally. Rather ioremap or
582 * friends, and at least on 32-bit they add global TLB flushes and points
586 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter,
587 struct iosys_map *dmap,
590 struct ttm_kmap_iter_linear_io *iter_io =
591 container_of(iter, typeof(*iter_io), base);
593 *dmap = iter_io->dmap;
594 iosys_map_incr(dmap, i * PAGE_SIZE);
597 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = {
598 .map_local = ttm_kmap_iter_linear_io_map_local,
603 * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory
604 * @iter_io: The iterator to initialize
605 * @bdev: The TTM device
606 * @mem: The ttm resource representing the iomap.
608 * This function is for internal TTM use only. It sets up a memcpy kmap iterator
609 * pointing at a linear chunk of io memory.
611 * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on
614 struct ttm_kmap_iter *
615 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
616 struct ttm_device *bdev,
617 struct ttm_resource *mem)
621 ret = ttm_mem_io_reserve(bdev, mem);
624 if (!mem->bus.is_iomem) {
630 iosys_map_set_vaddr(&iter_io->dmap, mem->bus.addr);
631 iter_io->needs_unmap = false;
633 iter_io->needs_unmap = true;
634 memset(&iter_io->dmap, 0, sizeof(iter_io->dmap));
635 if (mem->bus.caching == ttm_write_combined)
636 iosys_map_set_vaddr_iomem(&iter_io->dmap,
637 ioremap_wc(mem->bus.offset,
639 else if (mem->bus.caching == ttm_cached)
640 iosys_map_set_vaddr(&iter_io->dmap,
641 memremap(mem->bus.offset, mem->size,
646 /* If uncached requested or if mapping cached or wc failed */
647 if (iosys_map_is_null(&iter_io->dmap))
648 iosys_map_set_vaddr_iomem(&iter_io->dmap,
649 ioremap(mem->bus.offset,
652 if (iosys_map_is_null(&iter_io->dmap)) {
658 iter_io->base.ops = &ttm_kmap_iter_linear_io_ops;
659 return &iter_io->base;
662 ttm_mem_io_free(bdev, mem);
668 * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory
669 * @iter_io: The iterator to initialize
670 * @bdev: The TTM device
671 * @mem: The ttm resource representing the iomap.
673 * This function is for internal TTM use only. It cleans up a memcpy kmap
674 * iterator initialized by ttm_kmap_iter_linear_io_init.
677 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
678 struct ttm_device *bdev,
679 struct ttm_resource *mem)
681 if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) {
682 if (iter_io->dmap.is_iomem)
683 iounmap(iter_io->dmap.vaddr_iomem);
685 memunmap(iter_io->dmap.vaddr);
688 ttm_mem_io_free(bdev, mem);
691 #if defined(CONFIG_DEBUG_FS)
693 static int ttm_resource_manager_show(struct seq_file *m, void *unused)
695 struct ttm_resource_manager *man =
696 (struct ttm_resource_manager *)m->private;
697 struct drm_printer p = drm_seq_file_printer(m);
698 ttm_resource_manager_debug(man, &p);
701 DEFINE_SHOW_ATTRIBUTE(ttm_resource_manager);
706 * ttm_resource_manager_create_debugfs - Create debugfs entry for specified
708 * @man: The TTM resource manager for which the debugfs stats file be creates
709 * @parent: debugfs directory in which the file will reside
710 * @name: The filename to create.
712 * This function setups up a debugfs file that can be used to look
713 * at debug statistics of the specified ttm_resource_manager.
715 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
716 struct dentry * parent,
719 #if defined(CONFIG_DEBUG_FS)
720 debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops);
723 EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);