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>
29 #include <linux/cgroup_dmem.h>
31 #include <drm/ttm/ttm_bo.h>
32 #include <drm/ttm/ttm_placement.h>
33 #include <drm/ttm/ttm_resource.h>
34 #include <drm/ttm/ttm_tt.h>
36 #include <drm/drm_util.h>
38 /* Detach the cursor from the bulk move list*/
40 ttm_resource_cursor_clear_bulk(struct ttm_resource_cursor *cursor)
42 lockdep_assert_held(&cursor->man->bdev->lru_lock);
45 list_del_init(&cursor->bulk_link);
48 /* Move the cursor to the end of the bulk move list it's in */
49 static void ttm_resource_cursor_move_bulk_tail(struct ttm_lru_bulk_move *bulk,
50 struct ttm_resource_cursor *cursor)
52 struct ttm_lru_bulk_move_pos *pos;
54 lockdep_assert_held(&cursor->man->bdev->lru_lock);
56 if (WARN_ON_ONCE(bulk != cursor->bulk)) {
57 list_del_init(&cursor->bulk_link);
61 pos = &bulk->pos[cursor->mem_type][cursor->priority];
63 list_move(&cursor->hitch.link, &pos->last->lru.link);
64 ttm_resource_cursor_clear_bulk(cursor);
67 /* Move all cursors attached to a bulk move to its end */
68 static void ttm_bulk_move_adjust_cursors(struct ttm_lru_bulk_move *bulk)
70 struct ttm_resource_cursor *cursor, *next;
72 list_for_each_entry_safe(cursor, next, &bulk->cursor_list, bulk_link)
73 ttm_resource_cursor_move_bulk_tail(bulk, cursor);
76 /* Remove a cursor from an empty bulk move list */
77 static void ttm_bulk_move_drop_cursors(struct ttm_lru_bulk_move *bulk)
79 struct ttm_resource_cursor *cursor, *next;
81 list_for_each_entry_safe(cursor, next, &bulk->cursor_list, bulk_link)
82 ttm_resource_cursor_clear_bulk(cursor);
86 * ttm_resource_cursor_fini() - Finalize the LRU list cursor usage
87 * @cursor: The struct ttm_resource_cursor to finalize.
89 * The function pulls the LRU list cursor off any lists it was previusly
90 * attached to. Needs to be called with the LRU lock held. The function
91 * can be called multiple times after eachother.
93 void ttm_resource_cursor_fini(struct ttm_resource_cursor *cursor)
95 lockdep_assert_held(&cursor->man->bdev->lru_lock);
96 list_del_init(&cursor->hitch.link);
97 ttm_resource_cursor_clear_bulk(cursor);
101 * ttm_lru_bulk_move_init - initialize a bulk move structure
102 * @bulk: the structure to init
104 * For now just memset the structure to zero.
106 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
108 memset(bulk, 0, sizeof(*bulk));
109 INIT_LIST_HEAD(&bulk->cursor_list);
111 EXPORT_SYMBOL(ttm_lru_bulk_move_init);
114 * ttm_lru_bulk_move_fini - finalize a bulk move structure
115 * @bdev: The struct ttm_device
116 * @bulk: the structure to finalize
118 * Sanity checks that bulk moves don't have any
119 * resources left and hence no cursors attached.
121 void ttm_lru_bulk_move_fini(struct ttm_device *bdev,
122 struct ttm_lru_bulk_move *bulk)
124 spin_lock(&bdev->lru_lock);
125 ttm_bulk_move_drop_cursors(bulk);
126 spin_unlock(&bdev->lru_lock);
128 EXPORT_SYMBOL(ttm_lru_bulk_move_fini);
131 * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail.
133 * @bulk: bulk move structure
135 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
136 * resource order never changes. Should be called with &ttm_device.lru_lock held.
138 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
142 ttm_bulk_move_adjust_cursors(bulk);
143 for (i = 0; i < TTM_NUM_MEM_TYPES; ++i) {
144 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
145 struct ttm_lru_bulk_move_pos *pos = &bulk->pos[i][j];
146 struct ttm_resource_manager *man;
151 lockdep_assert_held(&pos->first->bo->bdev->lru_lock);
152 dma_resv_assert_held(pos->first->bo->base.resv);
153 dma_resv_assert_held(pos->last->bo->base.resv);
155 man = ttm_manager_type(pos->first->bo->bdev, i);
156 list_bulk_move_tail(&man->lru[j], &pos->first->lru.link,
157 &pos->last->lru.link);
161 EXPORT_SYMBOL(ttm_lru_bulk_move_tail);
163 /* Return the bulk move pos object for this resource */
164 static struct ttm_lru_bulk_move_pos *
165 ttm_lru_bulk_move_pos(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
167 return &bulk->pos[res->mem_type][res->bo->priority];
170 /* Return the previous resource on the list (skip over non-resource list items) */
171 static struct ttm_resource *ttm_lru_prev_res(struct ttm_resource *cur)
173 struct ttm_lru_item *lru = &cur->lru;
176 lru = list_prev_entry(lru, link);
177 } while (!ttm_lru_item_is_res(lru));
179 return ttm_lru_item_to_res(lru);
182 /* Return the next resource on the list (skip over non-resource list items) */
183 static struct ttm_resource *ttm_lru_next_res(struct ttm_resource *cur)
185 struct ttm_lru_item *lru = &cur->lru;
188 lru = list_next_entry(lru, link);
189 } while (!ttm_lru_item_is_res(lru));
191 return ttm_lru_item_to_res(lru);
194 /* Move the resource to the tail of the bulk move range */
195 static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
196 struct ttm_resource *res)
198 if (pos->last != res) {
199 if (pos->first == res)
200 pos->first = ttm_lru_next_res(res);
201 list_move(&res->lru.link, &pos->last->lru.link);
206 /* Add the resource to a bulk_move cursor */
207 static void ttm_lru_bulk_move_add(struct ttm_lru_bulk_move *bulk,
208 struct ttm_resource *res)
210 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
216 WARN_ON(pos->first->bo->base.resv != res->bo->base.resv);
217 ttm_lru_bulk_move_pos_tail(pos, res);
221 /* Remove the resource from a bulk_move range */
222 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
223 struct ttm_resource *res)
225 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
227 if (unlikely(WARN_ON(!pos->first || !pos->last) ||
228 (pos->first == res && pos->last == res))) {
231 } else if (pos->first == res) {
232 pos->first = ttm_lru_next_res(res);
233 } else if (pos->last == res) {
234 pos->last = ttm_lru_prev_res(res);
236 list_move(&res->lru.link, &pos->last->lru.link);
240 static bool ttm_resource_is_swapped(struct ttm_resource *res, struct ttm_buffer_object *bo)
243 * Take care when creating a new resource for a bo, that it is not considered
244 * swapped if it's not the current resource for the bo and is thus logically
245 * associated with the ttm_tt. Think a VRAM resource created to move a
246 * swapped-out bo to VRAM.
248 if (bo->resource != res || !bo->ttm)
251 dma_resv_assert_held(bo->base.resv);
252 return ttm_tt_is_swapped(bo->ttm);
255 /* Add the resource to a bulk move if the BO is configured for it */
256 void ttm_resource_add_bulk_move(struct ttm_resource *res,
257 struct ttm_buffer_object *bo)
259 if (bo->bulk_move && !bo->pin_count && !ttm_resource_is_swapped(res, bo))
260 ttm_lru_bulk_move_add(bo->bulk_move, res);
263 /* Remove the resource from a bulk move if the BO is configured for it */
264 void ttm_resource_del_bulk_move(struct ttm_resource *res,
265 struct ttm_buffer_object *bo)
267 if (bo->bulk_move && !bo->pin_count && !ttm_resource_is_swapped(res, bo))
268 ttm_lru_bulk_move_del(bo->bulk_move, res);
271 /* Move a resource to the LRU or bulk tail */
272 void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
274 struct ttm_buffer_object *bo = res->bo;
275 struct ttm_device *bdev = bo->bdev;
277 lockdep_assert_held(&bo->bdev->lru_lock);
279 if (bo->pin_count || ttm_resource_is_swapped(res, bo)) {
280 list_move_tail(&res->lru.link, &bdev->unevictable);
282 } else if (bo->bulk_move) {
283 struct ttm_lru_bulk_move_pos *pos =
284 ttm_lru_bulk_move_pos(bo->bulk_move, res);
286 ttm_lru_bulk_move_pos_tail(pos, res);
288 struct ttm_resource_manager *man;
290 man = ttm_manager_type(bdev, res->mem_type);
291 list_move_tail(&res->lru.link, &man->lru[bo->priority]);
296 * ttm_resource_init - resource object constructure
297 * @bo: buffer object this resources is allocated for
298 * @place: placement of the resource
299 * @res: the resource object to inistilize
301 * Initialize a new resource object. Counterpart of ttm_resource_fini().
303 void ttm_resource_init(struct ttm_buffer_object *bo,
304 const struct ttm_place *place,
305 struct ttm_resource *res)
307 struct ttm_resource_manager *man;
310 res->size = bo->base.size;
311 res->mem_type = place->mem_type;
312 res->placement = place->flags;
313 res->bus.addr = NULL;
315 res->bus.is_iomem = false;
316 res->bus.caching = ttm_cached;
319 man = ttm_manager_type(bo->bdev, place->mem_type);
320 spin_lock(&bo->bdev->lru_lock);
321 if (bo->pin_count || ttm_resource_is_swapped(res, bo))
322 list_add_tail(&res->lru.link, &bo->bdev->unevictable);
324 list_add_tail(&res->lru.link, &man->lru[bo->priority]);
325 man->usage += res->size;
326 spin_unlock(&bo->bdev->lru_lock);
328 EXPORT_SYMBOL(ttm_resource_init);
331 * ttm_resource_fini - resource destructor
332 * @man: the resource manager this resource belongs to
333 * @res: the resource to clean up
335 * Should be used by resource manager backends to clean up the TTM resource
336 * objects before freeing the underlying structure. Makes sure the resource is
337 * removed from the LRU before destruction.
338 * Counterpart of ttm_resource_init().
340 void ttm_resource_fini(struct ttm_resource_manager *man,
341 struct ttm_resource *res)
343 struct ttm_device *bdev = man->bdev;
345 spin_lock(&bdev->lru_lock);
346 list_del_init(&res->lru.link);
347 man->usage -= res->size;
348 spin_unlock(&bdev->lru_lock);
350 EXPORT_SYMBOL(ttm_resource_fini);
352 int ttm_resource_alloc(struct ttm_buffer_object *bo,
353 const struct ttm_place *place,
354 struct ttm_resource **res_ptr,
355 struct dmem_cgroup_pool_state **ret_limit_pool)
357 struct ttm_resource_manager *man =
358 ttm_manager_type(bo->bdev, place->mem_type);
359 struct dmem_cgroup_pool_state *pool = NULL;
363 ret = dmem_cgroup_try_charge(man->cg, bo->base.size, &pool, ret_limit_pool);
368 ret = man->func->alloc(man, bo, place, res_ptr);
371 dmem_cgroup_uncharge(pool, bo->base.size);
375 (*res_ptr)->css = pool;
377 spin_lock(&bo->bdev->lru_lock);
378 ttm_resource_add_bulk_move(*res_ptr, bo);
379 spin_unlock(&bo->bdev->lru_lock);
382 EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
384 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
386 struct ttm_resource_manager *man;
387 struct dmem_cgroup_pool_state *pool;
392 spin_lock(&bo->bdev->lru_lock);
393 ttm_resource_del_bulk_move(*res, bo);
394 spin_unlock(&bo->bdev->lru_lock);
397 man = ttm_manager_type(bo->bdev, (*res)->mem_type);
398 man->func->free(man, *res);
401 dmem_cgroup_uncharge(pool, bo->base.size);
403 EXPORT_SYMBOL(ttm_resource_free);
406 * ttm_resource_intersects - test for intersection
408 * @bdev: TTM device structure
409 * @res: The resource to test
410 * @place: The placement to test
411 * @size: How many bytes the new allocation needs.
413 * Test if @res intersects with @place and @size. Used for testing if evictions
414 * are valueable or not.
416 * Returns true if the res placement intersects with @place and @size.
418 bool ttm_resource_intersects(struct ttm_device *bdev,
419 struct ttm_resource *res,
420 const struct ttm_place *place,
423 struct ttm_resource_manager *man;
428 man = ttm_manager_type(bdev, res->mem_type);
429 if (!place || !man->func->intersects)
432 return man->func->intersects(man, res, place, size);
436 * ttm_resource_compatible - check if resource is compatible with placement
438 * @res: the resource to check
439 * @placement: the placement to check against
440 * @evicting: true if the caller is doing evictions
442 * Returns true if the placement is compatible.
444 bool ttm_resource_compatible(struct ttm_resource *res,
445 struct ttm_placement *placement,
448 struct ttm_buffer_object *bo = res->bo;
449 struct ttm_device *bdev = bo->bdev;
452 if (res->placement & TTM_PL_FLAG_TEMPORARY)
455 for (i = 0; i < placement->num_placement; i++) {
456 const struct ttm_place *place = &placement->placement[i];
457 struct ttm_resource_manager *man;
459 if (res->mem_type != place->mem_type)
462 if (place->flags & (evicting ? TTM_PL_FLAG_DESIRED :
463 TTM_PL_FLAG_FALLBACK))
466 if (place->flags & TTM_PL_FLAG_CONTIGUOUS &&
467 !(res->placement & TTM_PL_FLAG_CONTIGUOUS))
470 man = ttm_manager_type(bdev, res->mem_type);
471 if (man->func->compatible &&
472 !man->func->compatible(man, res, place, bo->base.size))
480 void ttm_resource_set_bo(struct ttm_resource *res,
481 struct ttm_buffer_object *bo)
483 spin_lock(&bo->bdev->lru_lock);
485 spin_unlock(&bo->bdev->lru_lock);
489 * ttm_resource_manager_init
491 * @man: memory manager object to init
492 * @bdev: ttm device this manager belongs to
493 * @size: size of managed resources in arbitrary units
495 * Initialise core parts of a manager object.
497 void ttm_resource_manager_init(struct ttm_resource_manager *man,
498 struct ttm_device *bdev,
503 spin_lock_init(&man->move_lock);
508 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
509 INIT_LIST_HEAD(&man->lru[i]);
512 EXPORT_SYMBOL(ttm_resource_manager_init);
515 * ttm_resource_manager_evict_all
517 * @bdev - device to use
518 * @man - manager to use
520 * Evict all the objects out of a memory manager until it is empty.
521 * Part of memory manager cleanup sequence.
523 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
524 struct ttm_resource_manager *man)
526 struct ttm_operation_ctx ctx = {
527 .interruptible = false,
528 .no_wait_gpu = false,
531 struct dma_fence *fence;
535 ret = ttm_bo_evict_first(bdev, man, &ctx);
539 spin_lock(&man->move_lock);
540 fence = dma_fence_get(man->move);
541 spin_unlock(&man->move_lock);
544 ret = dma_fence_wait(fence, false);
545 dma_fence_put(fence);
552 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
555 * ttm_resource_manager_usage
557 * @man: A memory manager object.
559 * Return how many resources are currently used.
561 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
565 spin_lock(&man->bdev->lru_lock);
567 spin_unlock(&man->bdev->lru_lock);
570 EXPORT_SYMBOL(ttm_resource_manager_usage);
573 * ttm_resource_manager_debug
575 * @man: manager type to dump.
576 * @p: printer to use for debug.
578 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
579 struct drm_printer *p)
581 drm_printf(p, " use_type: %d\n", man->use_type);
582 drm_printf(p, " use_tt: %d\n", man->use_tt);
583 drm_printf(p, " size: %llu\n", man->size);
584 drm_printf(p, " usage: %llu\n", ttm_resource_manager_usage(man));
585 if (man->func->debug)
586 man->func->debug(man, p);
588 EXPORT_SYMBOL(ttm_resource_manager_debug);
591 ttm_resource_cursor_check_bulk(struct ttm_resource_cursor *cursor,
592 struct ttm_lru_item *next_lru)
594 struct ttm_resource *next = ttm_lru_item_to_res(next_lru);
595 struct ttm_lru_bulk_move *bulk = NULL;
596 struct ttm_buffer_object *bo = next->bo;
598 lockdep_assert_held(&cursor->man->bdev->lru_lock);
599 bulk = bo->bulk_move;
601 if (cursor->bulk != bulk) {
603 list_move_tail(&cursor->bulk_link, &bulk->cursor_list);
604 cursor->mem_type = next->mem_type;
606 list_del_init(&cursor->bulk_link);
613 * ttm_resource_manager_first() - Start iterating over the resources
614 * of a resource manager
615 * @man: resource manager to iterate over
616 * @cursor: cursor to record the position
618 * Initializes the cursor and starts iterating. When done iterating,
619 * the caller must explicitly call ttm_resource_cursor_fini().
621 * Return: The first resource from the resource manager.
623 struct ttm_resource *
624 ttm_resource_manager_first(struct ttm_resource_manager *man,
625 struct ttm_resource_cursor *cursor)
627 lockdep_assert_held(&man->bdev->lru_lock);
629 cursor->priority = 0;
631 ttm_lru_item_init(&cursor->hitch, TTM_LRU_HITCH);
632 INIT_LIST_HEAD(&cursor->bulk_link);
633 list_add(&cursor->hitch.link, &man->lru[cursor->priority]);
635 return ttm_resource_manager_next(cursor);
639 * ttm_resource_manager_next() - Continue iterating over the resource manager
641 * @cursor: cursor to record the position
643 * Return: the next resource from the resource manager.
645 struct ttm_resource *
646 ttm_resource_manager_next(struct ttm_resource_cursor *cursor)
648 struct ttm_resource_manager *man = cursor->man;
649 struct ttm_lru_item *lru;
651 lockdep_assert_held(&man->bdev->lru_lock);
654 lru = &cursor->hitch;
655 list_for_each_entry_continue(lru, &man->lru[cursor->priority], link) {
656 if (ttm_lru_item_is_res(lru)) {
657 ttm_resource_cursor_check_bulk(cursor, lru);
658 list_move(&cursor->hitch.link, &lru->link);
659 return ttm_lru_item_to_res(lru);
663 if (++cursor->priority >= TTM_MAX_BO_PRIORITY)
666 list_move(&cursor->hitch.link, &man->lru[cursor->priority]);
667 ttm_resource_cursor_clear_bulk(cursor);
670 ttm_resource_cursor_fini(cursor);
676 * ttm_lru_first_res_or_null() - Return the first resource on an lru list
677 * @head: The list head of the lru list.
679 * Return: Pointer to the first resource on the lru list or NULL if
682 struct ttm_resource *ttm_lru_first_res_or_null(struct list_head *head)
684 struct ttm_lru_item *lru;
686 list_for_each_entry(lru, head, link) {
687 if (ttm_lru_item_is_res(lru))
688 return ttm_lru_item_to_res(lru);
694 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
695 struct iosys_map *dmap,
698 struct ttm_kmap_iter_iomap *iter_io =
699 container_of(iter, typeof(*iter_io), base);
703 while (i >= iter_io->cache.end) {
704 iter_io->cache.sg = iter_io->cache.sg ?
705 sg_next(iter_io->cache.sg) : iter_io->st->sgl;
706 iter_io->cache.i = iter_io->cache.end;
707 iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >>
709 iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) -
713 if (i < iter_io->cache.i) {
714 iter_io->cache.end = 0;
715 iter_io->cache.sg = NULL;
719 addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs +
720 (((resource_size_t)i - iter_io->cache.i)
722 iosys_map_set_vaddr_iomem(dmap, addr);
725 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter,
726 struct iosys_map *map)
728 io_mapping_unmap_local(map->vaddr_iomem);
731 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = {
732 .map_local = ttm_kmap_iter_iomap_map_local,
733 .unmap_local = ttm_kmap_iter_iomap_unmap_local,
738 * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap
739 * @iter_io: The struct ttm_kmap_iter_iomap to initialize.
740 * @iomap: The struct io_mapping representing the underlying linear io_memory.
741 * @st: sg_table into @iomap, representing the memory of the struct
743 * @start: Offset that needs to be subtracted from @st to make
744 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
746 * Return: Pointer to the embedded struct ttm_kmap_iter.
748 struct ttm_kmap_iter *
749 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
750 struct io_mapping *iomap,
752 resource_size_t start)
754 iter_io->base.ops = &ttm_kmap_iter_io_ops;
755 iter_io->iomap = iomap;
757 iter_io->start = start;
758 memset(&iter_io->cache, 0, sizeof(iter_io->cache));
760 return &iter_io->base;
762 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init);
765 * DOC: Linear io iterator
767 * This code should die in the not too near future. Best would be if we could
768 * make io-mapping use memremap for all io memory, and have memremap
769 * implement a kmap_local functionality. We could then strip a huge amount of
770 * code. These linear io iterators are implemented to mimic old functionality,
771 * and they don't use kmap_local semantics at all internally. Rather ioremap or
772 * friends, and at least on 32-bit they add global TLB flushes and points
776 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter,
777 struct iosys_map *dmap,
780 struct ttm_kmap_iter_linear_io *iter_io =
781 container_of(iter, typeof(*iter_io), base);
783 *dmap = iter_io->dmap;
784 iosys_map_incr(dmap, i * PAGE_SIZE);
787 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = {
788 .map_local = ttm_kmap_iter_linear_io_map_local,
793 * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory
794 * @iter_io: The iterator to initialize
795 * @bdev: The TTM device
796 * @mem: The ttm resource representing the iomap.
798 * This function is for internal TTM use only. It sets up a memcpy kmap iterator
799 * pointing at a linear chunk of io memory.
801 * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on
804 struct ttm_kmap_iter *
805 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
806 struct ttm_device *bdev,
807 struct ttm_resource *mem)
811 ret = ttm_mem_io_reserve(bdev, mem);
814 if (!mem->bus.is_iomem) {
820 iosys_map_set_vaddr(&iter_io->dmap, mem->bus.addr);
821 iter_io->needs_unmap = false;
823 iter_io->needs_unmap = true;
824 memset(&iter_io->dmap, 0, sizeof(iter_io->dmap));
825 if (mem->bus.caching == ttm_write_combined)
826 iosys_map_set_vaddr_iomem(&iter_io->dmap,
827 ioremap_wc(mem->bus.offset,
829 else if (mem->bus.caching == ttm_cached)
830 iosys_map_set_vaddr(&iter_io->dmap,
831 memremap(mem->bus.offset, mem->size,
836 /* If uncached requested or if mapping cached or wc failed */
837 if (iosys_map_is_null(&iter_io->dmap))
838 iosys_map_set_vaddr_iomem(&iter_io->dmap,
839 ioremap(mem->bus.offset,
842 if (iosys_map_is_null(&iter_io->dmap)) {
848 iter_io->base.ops = &ttm_kmap_iter_linear_io_ops;
849 return &iter_io->base;
852 ttm_mem_io_free(bdev, mem);
858 * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory
859 * @iter_io: The iterator to initialize
860 * @bdev: The TTM device
861 * @mem: The ttm resource representing the iomap.
863 * This function is for internal TTM use only. It cleans up a memcpy kmap
864 * iterator initialized by ttm_kmap_iter_linear_io_init.
867 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
868 struct ttm_device *bdev,
869 struct ttm_resource *mem)
871 if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) {
872 if (iter_io->dmap.is_iomem)
873 iounmap(iter_io->dmap.vaddr_iomem);
875 memunmap(iter_io->dmap.vaddr);
878 ttm_mem_io_free(bdev, mem);
881 #if defined(CONFIG_DEBUG_FS)
883 static int ttm_resource_manager_show(struct seq_file *m, void *unused)
885 struct ttm_resource_manager *man =
886 (struct ttm_resource_manager *)m->private;
887 struct drm_printer p = drm_seq_file_printer(m);
888 ttm_resource_manager_debug(man, &p);
891 DEFINE_SHOW_ATTRIBUTE(ttm_resource_manager);
896 * ttm_resource_manager_create_debugfs - Create debugfs entry for specified
898 * @man: The TTM resource manager for which the debugfs stats file be creates
899 * @parent: debugfs directory in which the file will reside
900 * @name: The filename to create.
902 * This function setups up a debugfs file that can be used to look
903 * at debug statistics of the specified ttm_resource_manager.
905 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
906 struct dentry * parent,
909 #if defined(CONFIG_DEBUG_FS)
910 debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops);
913 EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);