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>
33 #include <drm/ttm/ttm_tt.h>
35 #include <drm/drm_util.h>
37 /* Detach the cursor from the bulk move list*/
39 ttm_resource_cursor_clear_bulk(struct ttm_resource_cursor *cursor)
41 lockdep_assert_held(&cursor->man->bdev->lru_lock);
44 list_del_init(&cursor->bulk_link);
47 /* Move the cursor to the end of the bulk move list it's in */
48 static void ttm_resource_cursor_move_bulk_tail(struct ttm_lru_bulk_move *bulk,
49 struct ttm_resource_cursor *cursor)
51 struct ttm_lru_bulk_move_pos *pos;
53 lockdep_assert_held(&cursor->man->bdev->lru_lock);
55 if (WARN_ON_ONCE(bulk != cursor->bulk)) {
56 list_del_init(&cursor->bulk_link);
60 pos = &bulk->pos[cursor->mem_type][cursor->priority];
62 list_move(&cursor->hitch.link, &pos->last->lru.link);
63 ttm_resource_cursor_clear_bulk(cursor);
66 /* Move all cursors attached to a bulk move to its end */
67 static void ttm_bulk_move_adjust_cursors(struct ttm_lru_bulk_move *bulk)
69 struct ttm_resource_cursor *cursor, *next;
71 list_for_each_entry_safe(cursor, next, &bulk->cursor_list, bulk_link)
72 ttm_resource_cursor_move_bulk_tail(bulk, cursor);
75 /* Remove a cursor from an empty bulk move list */
76 static void ttm_bulk_move_drop_cursors(struct ttm_lru_bulk_move *bulk)
78 struct ttm_resource_cursor *cursor, *next;
80 list_for_each_entry_safe(cursor, next, &bulk->cursor_list, bulk_link)
81 ttm_resource_cursor_clear_bulk(cursor);
85 * ttm_resource_cursor_fini() - Finalize the LRU list cursor usage
86 * @cursor: The struct ttm_resource_cursor to finalize.
88 * The function pulls the LRU list cursor off any lists it was previusly
89 * attached to. Needs to be called with the LRU lock held. The function
90 * can be called multiple times after eachother.
92 void ttm_resource_cursor_fini(struct ttm_resource_cursor *cursor)
94 lockdep_assert_held(&cursor->man->bdev->lru_lock);
95 list_del_init(&cursor->hitch.link);
96 ttm_resource_cursor_clear_bulk(cursor);
100 * ttm_lru_bulk_move_init - initialize a bulk move structure
101 * @bulk: the structure to init
103 * For now just memset the structure to zero.
105 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
107 memset(bulk, 0, sizeof(*bulk));
108 INIT_LIST_HEAD(&bulk->cursor_list);
110 EXPORT_SYMBOL(ttm_lru_bulk_move_init);
113 * ttm_lru_bulk_move_fini - finalize a bulk move structure
114 * @bdev: The struct ttm_device
115 * @bulk: the structure to finalize
117 * Sanity checks that bulk moves don't have any
118 * resources left and hence no cursors attached.
120 void ttm_lru_bulk_move_fini(struct ttm_device *bdev,
121 struct ttm_lru_bulk_move *bulk)
123 spin_lock(&bdev->lru_lock);
124 ttm_bulk_move_drop_cursors(bulk);
125 spin_unlock(&bdev->lru_lock);
127 EXPORT_SYMBOL(ttm_lru_bulk_move_fini);
130 * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail.
132 * @bulk: bulk move structure
134 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
135 * resource order never changes. Should be called with &ttm_device.lru_lock held.
137 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
141 ttm_bulk_move_adjust_cursors(bulk);
142 for (i = 0; i < TTM_NUM_MEM_TYPES; ++i) {
143 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
144 struct ttm_lru_bulk_move_pos *pos = &bulk->pos[i][j];
145 struct ttm_resource_manager *man;
150 lockdep_assert_held(&pos->first->bo->bdev->lru_lock);
151 dma_resv_assert_held(pos->first->bo->base.resv);
152 dma_resv_assert_held(pos->last->bo->base.resv);
154 man = ttm_manager_type(pos->first->bo->bdev, i);
155 list_bulk_move_tail(&man->lru[j], &pos->first->lru.link,
156 &pos->last->lru.link);
160 EXPORT_SYMBOL(ttm_lru_bulk_move_tail);
162 /* Return the bulk move pos object for this resource */
163 static struct ttm_lru_bulk_move_pos *
164 ttm_lru_bulk_move_pos(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
166 return &bulk->pos[res->mem_type][res->bo->priority];
169 /* Return the previous resource on the list (skip over non-resource list items) */
170 static struct ttm_resource *ttm_lru_prev_res(struct ttm_resource *cur)
172 struct ttm_lru_item *lru = &cur->lru;
175 lru = list_prev_entry(lru, link);
176 } while (!ttm_lru_item_is_res(lru));
178 return ttm_lru_item_to_res(lru);
181 /* Return the next resource on the list (skip over non-resource list items) */
182 static struct ttm_resource *ttm_lru_next_res(struct ttm_resource *cur)
184 struct ttm_lru_item *lru = &cur->lru;
187 lru = list_next_entry(lru, link);
188 } while (!ttm_lru_item_is_res(lru));
190 return ttm_lru_item_to_res(lru);
193 /* Move the resource to the tail of the bulk move range */
194 static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
195 struct ttm_resource *res)
197 if (pos->last != res) {
198 if (pos->first == res)
199 pos->first = ttm_lru_next_res(res);
200 list_move(&res->lru.link, &pos->last->lru.link);
205 /* Add the resource to a bulk_move cursor */
206 static void ttm_lru_bulk_move_add(struct ttm_lru_bulk_move *bulk,
207 struct ttm_resource *res)
209 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
215 WARN_ON(pos->first->bo->base.resv != res->bo->base.resv);
216 ttm_lru_bulk_move_pos_tail(pos, res);
220 /* Remove the resource from a bulk_move range */
221 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
222 struct ttm_resource *res)
224 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
226 if (unlikely(WARN_ON(!pos->first || !pos->last) ||
227 (pos->first == res && pos->last == res))) {
230 } else if (pos->first == res) {
231 pos->first = ttm_lru_next_res(res);
232 } else if (pos->last == res) {
233 pos->last = ttm_lru_prev_res(res);
235 list_move(&res->lru.link, &pos->last->lru.link);
239 static bool ttm_resource_is_swapped(struct ttm_resource *res, struct ttm_buffer_object *bo)
242 * Take care when creating a new resource for a bo, that it is not considered
243 * swapped if it's not the current resource for the bo and is thus logically
244 * associated with the ttm_tt. Think a VRAM resource created to move a
245 * swapped-out bo to VRAM.
247 if (bo->resource != res || !bo->ttm)
250 dma_resv_assert_held(bo->base.resv);
251 return ttm_tt_is_swapped(bo->ttm);
254 /* Add the resource to a bulk move if the BO is configured for it */
255 void ttm_resource_add_bulk_move(struct ttm_resource *res,
256 struct ttm_buffer_object *bo)
258 if (bo->bulk_move && !bo->pin_count && !ttm_resource_is_swapped(res, bo))
259 ttm_lru_bulk_move_add(bo->bulk_move, res);
262 /* Remove the resource from a bulk move if the BO is configured for it */
263 void ttm_resource_del_bulk_move(struct ttm_resource *res,
264 struct ttm_buffer_object *bo)
266 if (bo->bulk_move && !bo->pin_count && !ttm_resource_is_swapped(res, bo))
267 ttm_lru_bulk_move_del(bo->bulk_move, res);
270 /* Move a resource to the LRU or bulk tail */
271 void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
273 struct ttm_buffer_object *bo = res->bo;
274 struct ttm_device *bdev = bo->bdev;
276 lockdep_assert_held(&bo->bdev->lru_lock);
278 if (bo->pin_count || ttm_resource_is_swapped(res, bo)) {
279 list_move_tail(&res->lru.link, &bdev->unevictable);
281 } else if (bo->bulk_move) {
282 struct ttm_lru_bulk_move_pos *pos =
283 ttm_lru_bulk_move_pos(bo->bulk_move, res);
285 ttm_lru_bulk_move_pos_tail(pos, res);
287 struct ttm_resource_manager *man;
289 man = ttm_manager_type(bdev, res->mem_type);
290 list_move_tail(&res->lru.link, &man->lru[bo->priority]);
295 * ttm_resource_init - resource object constructure
296 * @bo: buffer object this resources is allocated for
297 * @place: placement of the resource
298 * @res: the resource object to inistilize
300 * Initialize a new resource object. Counterpart of ttm_resource_fini().
302 void ttm_resource_init(struct ttm_buffer_object *bo,
303 const struct ttm_place *place,
304 struct ttm_resource *res)
306 struct ttm_resource_manager *man;
309 res->size = bo->base.size;
310 res->mem_type = place->mem_type;
311 res->placement = place->flags;
312 res->bus.addr = NULL;
314 res->bus.is_iomem = false;
315 res->bus.caching = ttm_cached;
318 man = ttm_manager_type(bo->bdev, place->mem_type);
319 spin_lock(&bo->bdev->lru_lock);
320 if (bo->pin_count || ttm_resource_is_swapped(res, bo))
321 list_add_tail(&res->lru.link, &bo->bdev->unevictable);
323 list_add_tail(&res->lru.link, &man->lru[bo->priority]);
324 man->usage += res->size;
325 spin_unlock(&bo->bdev->lru_lock);
327 EXPORT_SYMBOL(ttm_resource_init);
330 * ttm_resource_fini - resource destructor
331 * @man: the resource manager this resource belongs to
332 * @res: the resource to clean up
334 * Should be used by resource manager backends to clean up the TTM resource
335 * objects before freeing the underlying structure. Makes sure the resource is
336 * removed from the LRU before destruction.
337 * Counterpart of ttm_resource_init().
339 void ttm_resource_fini(struct ttm_resource_manager *man,
340 struct ttm_resource *res)
342 struct ttm_device *bdev = man->bdev;
344 spin_lock(&bdev->lru_lock);
345 list_del_init(&res->lru.link);
346 man->usage -= res->size;
347 spin_unlock(&bdev->lru_lock);
349 EXPORT_SYMBOL(ttm_resource_fini);
351 int ttm_resource_alloc(struct ttm_buffer_object *bo,
352 const struct ttm_place *place,
353 struct ttm_resource **res_ptr)
355 struct ttm_resource_manager *man =
356 ttm_manager_type(bo->bdev, place->mem_type);
359 ret = man->func->alloc(man, bo, place, res_ptr);
363 spin_lock(&bo->bdev->lru_lock);
364 ttm_resource_add_bulk_move(*res_ptr, bo);
365 spin_unlock(&bo->bdev->lru_lock);
368 EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
370 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
372 struct ttm_resource_manager *man;
377 spin_lock(&bo->bdev->lru_lock);
378 ttm_resource_del_bulk_move(*res, bo);
379 spin_unlock(&bo->bdev->lru_lock);
380 man = ttm_manager_type(bo->bdev, (*res)->mem_type);
381 man->func->free(man, *res);
384 EXPORT_SYMBOL(ttm_resource_free);
387 * ttm_resource_intersects - test for intersection
389 * @bdev: TTM device structure
390 * @res: The resource to test
391 * @place: The placement to test
392 * @size: How many bytes the new allocation needs.
394 * Test if @res intersects with @place and @size. Used for testing if evictions
395 * are valueable or not.
397 * Returns true if the res placement intersects with @place and @size.
399 bool ttm_resource_intersects(struct ttm_device *bdev,
400 struct ttm_resource *res,
401 const struct ttm_place *place,
404 struct ttm_resource_manager *man;
409 man = ttm_manager_type(bdev, res->mem_type);
410 if (!place || !man->func->intersects)
413 return man->func->intersects(man, res, place, size);
417 * ttm_resource_compatible - check if resource is compatible with placement
419 * @res: the resource to check
420 * @placement: the placement to check against
421 * @evicting: true if the caller is doing evictions
423 * Returns true if the placement is compatible.
425 bool ttm_resource_compatible(struct ttm_resource *res,
426 struct ttm_placement *placement,
429 struct ttm_buffer_object *bo = res->bo;
430 struct ttm_device *bdev = bo->bdev;
433 if (res->placement & TTM_PL_FLAG_TEMPORARY)
436 for (i = 0; i < placement->num_placement; i++) {
437 const struct ttm_place *place = &placement->placement[i];
438 struct ttm_resource_manager *man;
440 if (res->mem_type != place->mem_type)
443 if (place->flags & (evicting ? TTM_PL_FLAG_DESIRED :
444 TTM_PL_FLAG_FALLBACK))
447 if (place->flags & TTM_PL_FLAG_CONTIGUOUS &&
448 !(res->placement & TTM_PL_FLAG_CONTIGUOUS))
451 man = ttm_manager_type(bdev, res->mem_type);
452 if (man->func->compatible &&
453 !man->func->compatible(man, res, place, bo->base.size))
461 void ttm_resource_set_bo(struct ttm_resource *res,
462 struct ttm_buffer_object *bo)
464 spin_lock(&bo->bdev->lru_lock);
466 spin_unlock(&bo->bdev->lru_lock);
470 * ttm_resource_manager_init
472 * @man: memory manager object to init
473 * @bdev: ttm device this manager belongs to
474 * @size: size of managed resources in arbitrary units
476 * Initialise core parts of a manager object.
478 void ttm_resource_manager_init(struct ttm_resource_manager *man,
479 struct ttm_device *bdev,
484 spin_lock_init(&man->move_lock);
489 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
490 INIT_LIST_HEAD(&man->lru[i]);
493 EXPORT_SYMBOL(ttm_resource_manager_init);
496 * ttm_resource_manager_evict_all
498 * @bdev - device to use
499 * @man - manager to use
501 * Evict all the objects out of a memory manager until it is empty.
502 * Part of memory manager cleanup sequence.
504 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
505 struct ttm_resource_manager *man)
507 struct ttm_operation_ctx ctx = {
508 .interruptible = false,
509 .no_wait_gpu = false,
512 struct dma_fence *fence;
516 ret = ttm_bo_evict_first(bdev, man, &ctx);
520 spin_lock(&man->move_lock);
521 fence = dma_fence_get(man->move);
522 spin_unlock(&man->move_lock);
525 ret = dma_fence_wait(fence, false);
526 dma_fence_put(fence);
533 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
536 * ttm_resource_manager_usage
538 * @man: A memory manager object.
540 * Return how many resources are currently used.
542 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
546 spin_lock(&man->bdev->lru_lock);
548 spin_unlock(&man->bdev->lru_lock);
551 EXPORT_SYMBOL(ttm_resource_manager_usage);
554 * ttm_resource_manager_debug
556 * @man: manager type to dump.
557 * @p: printer to use for debug.
559 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
560 struct drm_printer *p)
562 drm_printf(p, " use_type: %d\n", man->use_type);
563 drm_printf(p, " use_tt: %d\n", man->use_tt);
564 drm_printf(p, " size: %llu\n", man->size);
565 drm_printf(p, " usage: %llu\n", ttm_resource_manager_usage(man));
566 if (man->func->debug)
567 man->func->debug(man, p);
569 EXPORT_SYMBOL(ttm_resource_manager_debug);
572 ttm_resource_cursor_check_bulk(struct ttm_resource_cursor *cursor,
573 struct ttm_lru_item *next_lru)
575 struct ttm_resource *next = ttm_lru_item_to_res(next_lru);
576 struct ttm_lru_bulk_move *bulk = NULL;
577 struct ttm_buffer_object *bo = next->bo;
579 lockdep_assert_held(&cursor->man->bdev->lru_lock);
580 bulk = bo->bulk_move;
582 if (cursor->bulk != bulk) {
584 list_move_tail(&cursor->bulk_link, &bulk->cursor_list);
585 cursor->mem_type = next->mem_type;
587 list_del_init(&cursor->bulk_link);
594 * ttm_resource_manager_first() - Start iterating over the resources
595 * of a resource manager
596 * @man: resource manager to iterate over
597 * @cursor: cursor to record the position
599 * Initializes the cursor and starts iterating. When done iterating,
600 * the caller must explicitly call ttm_resource_cursor_fini().
602 * Return: The first resource from the resource manager.
604 struct ttm_resource *
605 ttm_resource_manager_first(struct ttm_resource_manager *man,
606 struct ttm_resource_cursor *cursor)
608 lockdep_assert_held(&man->bdev->lru_lock);
610 cursor->priority = 0;
612 ttm_lru_item_init(&cursor->hitch, TTM_LRU_HITCH);
613 INIT_LIST_HEAD(&cursor->bulk_link);
614 list_add(&cursor->hitch.link, &man->lru[cursor->priority]);
616 return ttm_resource_manager_next(cursor);
620 * ttm_resource_manager_next() - Continue iterating over the resource manager
622 * @cursor: cursor to record the position
624 * Return: the next resource from the resource manager.
626 struct ttm_resource *
627 ttm_resource_manager_next(struct ttm_resource_cursor *cursor)
629 struct ttm_resource_manager *man = cursor->man;
630 struct ttm_lru_item *lru;
632 lockdep_assert_held(&man->bdev->lru_lock);
635 lru = &cursor->hitch;
636 list_for_each_entry_continue(lru, &man->lru[cursor->priority], link) {
637 if (ttm_lru_item_is_res(lru)) {
638 ttm_resource_cursor_check_bulk(cursor, lru);
639 list_move(&cursor->hitch.link, &lru->link);
640 return ttm_lru_item_to_res(lru);
644 if (++cursor->priority >= TTM_MAX_BO_PRIORITY)
647 list_move(&cursor->hitch.link, &man->lru[cursor->priority]);
648 ttm_resource_cursor_clear_bulk(cursor);
651 ttm_resource_cursor_fini(cursor);
657 * ttm_lru_first_res_or_null() - Return the first resource on an lru list
658 * @head: The list head of the lru list.
660 * Return: Pointer to the first resource on the lru list or NULL if
663 struct ttm_resource *ttm_lru_first_res_or_null(struct list_head *head)
665 struct ttm_lru_item *lru;
667 list_for_each_entry(lru, head, link) {
668 if (ttm_lru_item_is_res(lru))
669 return ttm_lru_item_to_res(lru);
675 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
676 struct iosys_map *dmap,
679 struct ttm_kmap_iter_iomap *iter_io =
680 container_of(iter, typeof(*iter_io), base);
684 while (i >= iter_io->cache.end) {
685 iter_io->cache.sg = iter_io->cache.sg ?
686 sg_next(iter_io->cache.sg) : iter_io->st->sgl;
687 iter_io->cache.i = iter_io->cache.end;
688 iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >>
690 iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) -
694 if (i < iter_io->cache.i) {
695 iter_io->cache.end = 0;
696 iter_io->cache.sg = NULL;
700 addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs +
701 (((resource_size_t)i - iter_io->cache.i)
703 iosys_map_set_vaddr_iomem(dmap, addr);
706 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter,
707 struct iosys_map *map)
709 io_mapping_unmap_local(map->vaddr_iomem);
712 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = {
713 .map_local = ttm_kmap_iter_iomap_map_local,
714 .unmap_local = ttm_kmap_iter_iomap_unmap_local,
719 * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap
720 * @iter_io: The struct ttm_kmap_iter_iomap to initialize.
721 * @iomap: The struct io_mapping representing the underlying linear io_memory.
722 * @st: sg_table into @iomap, representing the memory of the struct
724 * @start: Offset that needs to be subtracted from @st to make
725 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
727 * Return: Pointer to the embedded struct ttm_kmap_iter.
729 struct ttm_kmap_iter *
730 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
731 struct io_mapping *iomap,
733 resource_size_t start)
735 iter_io->base.ops = &ttm_kmap_iter_io_ops;
736 iter_io->iomap = iomap;
738 iter_io->start = start;
739 memset(&iter_io->cache, 0, sizeof(iter_io->cache));
741 return &iter_io->base;
743 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init);
746 * DOC: Linear io iterator
748 * This code should die in the not too near future. Best would be if we could
749 * make io-mapping use memremap for all io memory, and have memremap
750 * implement a kmap_local functionality. We could then strip a huge amount of
751 * code. These linear io iterators are implemented to mimic old functionality,
752 * and they don't use kmap_local semantics at all internally. Rather ioremap or
753 * friends, and at least on 32-bit they add global TLB flushes and points
757 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter,
758 struct iosys_map *dmap,
761 struct ttm_kmap_iter_linear_io *iter_io =
762 container_of(iter, typeof(*iter_io), base);
764 *dmap = iter_io->dmap;
765 iosys_map_incr(dmap, i * PAGE_SIZE);
768 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = {
769 .map_local = ttm_kmap_iter_linear_io_map_local,
774 * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory
775 * @iter_io: The iterator to initialize
776 * @bdev: The TTM device
777 * @mem: The ttm resource representing the iomap.
779 * This function is for internal TTM use only. It sets up a memcpy kmap iterator
780 * pointing at a linear chunk of io memory.
782 * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on
785 struct ttm_kmap_iter *
786 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
787 struct ttm_device *bdev,
788 struct ttm_resource *mem)
792 ret = ttm_mem_io_reserve(bdev, mem);
795 if (!mem->bus.is_iomem) {
801 iosys_map_set_vaddr(&iter_io->dmap, mem->bus.addr);
802 iter_io->needs_unmap = false;
804 iter_io->needs_unmap = true;
805 memset(&iter_io->dmap, 0, sizeof(iter_io->dmap));
806 if (mem->bus.caching == ttm_write_combined)
807 iosys_map_set_vaddr_iomem(&iter_io->dmap,
808 ioremap_wc(mem->bus.offset,
810 else if (mem->bus.caching == ttm_cached)
811 iosys_map_set_vaddr(&iter_io->dmap,
812 memremap(mem->bus.offset, mem->size,
817 /* If uncached requested or if mapping cached or wc failed */
818 if (iosys_map_is_null(&iter_io->dmap))
819 iosys_map_set_vaddr_iomem(&iter_io->dmap,
820 ioremap(mem->bus.offset,
823 if (iosys_map_is_null(&iter_io->dmap)) {
829 iter_io->base.ops = &ttm_kmap_iter_linear_io_ops;
830 return &iter_io->base;
833 ttm_mem_io_free(bdev, mem);
839 * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory
840 * @iter_io: The iterator to initialize
841 * @bdev: The TTM device
842 * @mem: The ttm resource representing the iomap.
844 * This function is for internal TTM use only. It cleans up a memcpy kmap
845 * iterator initialized by ttm_kmap_iter_linear_io_init.
848 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
849 struct ttm_device *bdev,
850 struct ttm_resource *mem)
852 if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) {
853 if (iter_io->dmap.is_iomem)
854 iounmap(iter_io->dmap.vaddr_iomem);
856 memunmap(iter_io->dmap.vaddr);
859 ttm_mem_io_free(bdev, mem);
862 #if defined(CONFIG_DEBUG_FS)
864 static int ttm_resource_manager_show(struct seq_file *m, void *unused)
866 struct ttm_resource_manager *man =
867 (struct ttm_resource_manager *)m->private;
868 struct drm_printer p = drm_seq_file_printer(m);
869 ttm_resource_manager_debug(man, &p);
872 DEFINE_SHOW_ATTRIBUTE(ttm_resource_manager);
877 * ttm_resource_manager_create_debugfs - Create debugfs entry for specified
879 * @man: The TTM resource manager for which the debugfs stats file be creates
880 * @parent: debugfs directory in which the file will reside
881 * @name: The filename to create.
883 * This function setups up a debugfs file that can be used to look
884 * at debug statistics of the specified ttm_resource_manager.
886 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
887 struct dentry * parent,
890 #if defined(CONFIG_DEBUG_FS)
891 debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops);
894 EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);