xref: /linux/drivers/gpu/drm/ttm/ttm_resource.c (revision e6a901a00822659181c93c86d8bbc2a17779fddc)
1 /*
2  * Copyright 2020 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Christian König
23  */
24 
25 #include <linux/iosys-map.h>
26 #include <linux/io-mapping.h>
27 #include <linux/scatterlist.h>
28 
29 #include <drm/ttm/ttm_bo.h>
30 #include <drm/ttm/ttm_placement.h>
31 #include <drm/ttm/ttm_resource.h>
32 
33 #include <drm/drm_util.h>
34 
35 /**
36  * ttm_lru_bulk_move_init - initialize a bulk move structure
37  * @bulk: the structure to init
38  *
39  * For now just memset the structure to zero.
40  */
41 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
42 {
43 	memset(bulk, 0, sizeof(*bulk));
44 }
45 EXPORT_SYMBOL(ttm_lru_bulk_move_init);
46 
47 /**
48  * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail.
49  *
50  * @bulk: bulk move structure
51  *
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.
54  */
55 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
56 {
57 	unsigned i, j;
58 
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;
63 
64 			if (!pos->first)
65 				continue;
66 
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);
70 
71 			man = ttm_manager_type(pos->first->bo->bdev, i);
72 			list_bulk_move_tail(&man->lru[j], &pos->first->lru,
73 					    &pos->last->lru);
74 		}
75 	}
76 }
77 EXPORT_SYMBOL(ttm_lru_bulk_move_tail);
78 
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)
82 {
83 	return &bulk->pos[res->mem_type][res->bo->priority];
84 }
85 
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)
89 {
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);
94 		pos->last = res;
95 	}
96 }
97 
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)
101 {
102 	struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
103 
104 	if (!pos->first) {
105 		pos->first = res;
106 		pos->last = res;
107 	} else {
108 		WARN_ON(pos->first->bo->base.resv != res->bo->base.resv);
109 		ttm_lru_bulk_move_pos_tail(pos, res);
110 	}
111 }
112 
113 /* Remove the resource from a bulk_move range */
114 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
115 				  struct ttm_resource *res)
116 {
117 	struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
118 
119 	if (unlikely(WARN_ON(!pos->first || !pos->last) ||
120 		     (pos->first == res && pos->last == res))) {
121 		pos->first = NULL;
122 		pos->last = NULL;
123 	} else if (pos->first == res) {
124 		pos->first = list_next_entry(res, lru);
125 	} else if (pos->last == res) {
126 		pos->last = list_prev_entry(res, lru);
127 	} else {
128 		list_move(&res->lru, &pos->last->lru);
129 	}
130 }
131 
132 /* Add the resource to a bulk move if the BO is configured for it */
133 void ttm_resource_add_bulk_move(struct ttm_resource *res,
134 				struct ttm_buffer_object *bo)
135 {
136 	if (bo->bulk_move && !bo->pin_count)
137 		ttm_lru_bulk_move_add(bo->bulk_move, res);
138 }
139 
140 /* Remove the resource from a bulk move if the BO is configured for it */
141 void ttm_resource_del_bulk_move(struct ttm_resource *res,
142 				struct ttm_buffer_object *bo)
143 {
144 	if (bo->bulk_move && !bo->pin_count)
145 		ttm_lru_bulk_move_del(bo->bulk_move, res);
146 }
147 
148 /* Move a resource to the LRU or bulk tail */
149 void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
150 {
151 	struct ttm_buffer_object *bo = res->bo;
152 	struct ttm_device *bdev = bo->bdev;
153 
154 	lockdep_assert_held(&bo->bdev->lru_lock);
155 
156 	if (bo->pin_count) {
157 		list_move_tail(&res->lru, &bdev->pinned);
158 
159 	} else	if (bo->bulk_move) {
160 		struct ttm_lru_bulk_move_pos *pos =
161 			ttm_lru_bulk_move_pos(bo->bulk_move, res);
162 
163 		ttm_lru_bulk_move_pos_tail(pos, res);
164 	} else {
165 		struct ttm_resource_manager *man;
166 
167 		man = ttm_manager_type(bdev, res->mem_type);
168 		list_move_tail(&res->lru, &man->lru[bo->priority]);
169 	}
170 }
171 
172 /**
173  * ttm_resource_init - resource object constructure
174  * @bo: buffer object this resources is allocated for
175  * @place: placement of the resource
176  * @res: the resource object to inistilize
177  *
178  * Initialize a new resource object. Counterpart of ttm_resource_fini().
179  */
180 void ttm_resource_init(struct ttm_buffer_object *bo,
181                        const struct ttm_place *place,
182                        struct ttm_resource *res)
183 {
184 	struct ttm_resource_manager *man;
185 
186 	res->start = 0;
187 	res->size = bo->base.size;
188 	res->mem_type = place->mem_type;
189 	res->placement = place->flags;
190 	res->bus.addr = NULL;
191 	res->bus.offset = 0;
192 	res->bus.is_iomem = false;
193 	res->bus.caching = ttm_cached;
194 	res->bo = bo;
195 
196 	man = ttm_manager_type(bo->bdev, place->mem_type);
197 	spin_lock(&bo->bdev->lru_lock);
198 	if (bo->pin_count)
199 		list_add_tail(&res->lru, &bo->bdev->pinned);
200 	else
201 		list_add_tail(&res->lru, &man->lru[bo->priority]);
202 	man->usage += res->size;
203 	spin_unlock(&bo->bdev->lru_lock);
204 }
205 EXPORT_SYMBOL(ttm_resource_init);
206 
207 /**
208  * ttm_resource_fini - resource destructor
209  * @man: the resource manager this resource belongs to
210  * @res: the resource to clean up
211  *
212  * Should be used by resource manager backends to clean up the TTM resource
213  * objects before freeing the underlying structure. Makes sure the resource is
214  * removed from the LRU before destruction.
215  * Counterpart of ttm_resource_init().
216  */
217 void ttm_resource_fini(struct ttm_resource_manager *man,
218 		       struct ttm_resource *res)
219 {
220 	struct ttm_device *bdev = man->bdev;
221 
222 	spin_lock(&bdev->lru_lock);
223 	list_del_init(&res->lru);
224 	man->usage -= res->size;
225 	spin_unlock(&bdev->lru_lock);
226 }
227 EXPORT_SYMBOL(ttm_resource_fini);
228 
229 int ttm_resource_alloc(struct ttm_buffer_object *bo,
230 		       const struct ttm_place *place,
231 		       struct ttm_resource **res_ptr)
232 {
233 	struct ttm_resource_manager *man =
234 		ttm_manager_type(bo->bdev, place->mem_type);
235 	int ret;
236 
237 	ret = man->func->alloc(man, bo, place, res_ptr);
238 	if (ret)
239 		return ret;
240 
241 	spin_lock(&bo->bdev->lru_lock);
242 	ttm_resource_add_bulk_move(*res_ptr, bo);
243 	spin_unlock(&bo->bdev->lru_lock);
244 	return 0;
245 }
246 EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
247 
248 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
249 {
250 	struct ttm_resource_manager *man;
251 
252 	if (!*res)
253 		return;
254 
255 	spin_lock(&bo->bdev->lru_lock);
256 	ttm_resource_del_bulk_move(*res, bo);
257 	spin_unlock(&bo->bdev->lru_lock);
258 	man = ttm_manager_type(bo->bdev, (*res)->mem_type);
259 	man->func->free(man, *res);
260 	*res = NULL;
261 }
262 EXPORT_SYMBOL(ttm_resource_free);
263 
264 /**
265  * ttm_resource_intersects - test for intersection
266  *
267  * @bdev: TTM device structure
268  * @res: The resource to test
269  * @place: The placement to test
270  * @size: How many bytes the new allocation needs.
271  *
272  * Test if @res intersects with @place and @size. Used for testing if evictions
273  * are valueable or not.
274  *
275  * Returns true if the res placement intersects with @place and @size.
276  */
277 bool ttm_resource_intersects(struct ttm_device *bdev,
278 			     struct ttm_resource *res,
279 			     const struct ttm_place *place,
280 			     size_t size)
281 {
282 	struct ttm_resource_manager *man;
283 
284 	if (!res)
285 		return false;
286 
287 	man = ttm_manager_type(bdev, res->mem_type);
288 	if (!place || !man->func->intersects)
289 		return true;
290 
291 	return man->func->intersects(man, res, place, size);
292 }
293 
294 /**
295  * ttm_resource_compatible - check if resource is compatible with placement
296  *
297  * @res: the resource to check
298  * @placement: the placement to check against
299  * @evicting: true if the caller is doing evictions
300  *
301  * Returns true if the placement is compatible.
302  */
303 bool ttm_resource_compatible(struct ttm_resource *res,
304 			     struct ttm_placement *placement,
305 			     bool evicting)
306 {
307 	struct ttm_buffer_object *bo = res->bo;
308 	struct ttm_device *bdev = bo->bdev;
309 	unsigned i;
310 
311 	if (res->placement & TTM_PL_FLAG_TEMPORARY)
312 		return false;
313 
314 	for (i = 0; i < placement->num_placement; i++) {
315 		const struct ttm_place *place = &placement->placement[i];
316 		struct ttm_resource_manager *man;
317 
318 		if (res->mem_type != place->mem_type)
319 			continue;
320 
321 		if (place->flags & (evicting ? TTM_PL_FLAG_DESIRED :
322 				    TTM_PL_FLAG_FALLBACK))
323 			continue;
324 
325 		if (place->flags & TTM_PL_FLAG_CONTIGUOUS &&
326 		    !(res->placement & TTM_PL_FLAG_CONTIGUOUS))
327 			continue;
328 
329 		man = ttm_manager_type(bdev, res->mem_type);
330 		if (man->func->compatible &&
331 		    !man->func->compatible(man, res, place, bo->base.size))
332 			continue;
333 
334 		return true;
335 	}
336 	return false;
337 }
338 
339 void ttm_resource_set_bo(struct ttm_resource *res,
340 			 struct ttm_buffer_object *bo)
341 {
342 	spin_lock(&bo->bdev->lru_lock);
343 	res->bo = bo;
344 	spin_unlock(&bo->bdev->lru_lock);
345 }
346 
347 /**
348  * ttm_resource_manager_init
349  *
350  * @man: memory manager object to init
351  * @bdev: ttm device this manager belongs to
352  * @size: size of managed resources in arbitrary units
353  *
354  * Initialise core parts of a manager object.
355  */
356 void ttm_resource_manager_init(struct ttm_resource_manager *man,
357 			       struct ttm_device *bdev,
358 			       uint64_t size)
359 {
360 	unsigned i;
361 
362 	spin_lock_init(&man->move_lock);
363 	man->bdev = bdev;
364 	man->size = size;
365 	man->usage = 0;
366 
367 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
368 		INIT_LIST_HEAD(&man->lru[i]);
369 	man->move = NULL;
370 }
371 EXPORT_SYMBOL(ttm_resource_manager_init);
372 
373 /*
374  * ttm_resource_manager_evict_all
375  *
376  * @bdev - device to use
377  * @man - manager to use
378  *
379  * Evict all the objects out of a memory manager until it is empty.
380  * Part of memory manager cleanup sequence.
381  */
382 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
383 				   struct ttm_resource_manager *man)
384 {
385 	struct ttm_operation_ctx ctx = {
386 		.interruptible = false,
387 		.no_wait_gpu = false,
388 		.force_alloc = true
389 	};
390 	struct dma_fence *fence;
391 	int ret;
392 	unsigned i;
393 
394 	/*
395 	 * Can't use standard list traversal since we're unlocking.
396 	 */
397 
398 	spin_lock(&bdev->lru_lock);
399 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
400 		while (!list_empty(&man->lru[i])) {
401 			spin_unlock(&bdev->lru_lock);
402 			ret = ttm_mem_evict_first(bdev, man, NULL, &ctx,
403 						  NULL);
404 			if (ret)
405 				return ret;
406 			spin_lock(&bdev->lru_lock);
407 		}
408 	}
409 	spin_unlock(&bdev->lru_lock);
410 
411 	spin_lock(&man->move_lock);
412 	fence = dma_fence_get(man->move);
413 	spin_unlock(&man->move_lock);
414 
415 	if (fence) {
416 		ret = dma_fence_wait(fence, false);
417 		dma_fence_put(fence);
418 		if (ret)
419 			return ret;
420 	}
421 
422 	return 0;
423 }
424 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
425 
426 /**
427  * ttm_resource_manager_usage
428  *
429  * @man: A memory manager object.
430  *
431  * Return how many resources are currently used.
432  */
433 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
434 {
435 	uint64_t usage;
436 
437 	spin_lock(&man->bdev->lru_lock);
438 	usage = man->usage;
439 	spin_unlock(&man->bdev->lru_lock);
440 	return usage;
441 }
442 EXPORT_SYMBOL(ttm_resource_manager_usage);
443 
444 /**
445  * ttm_resource_manager_debug
446  *
447  * @man: manager type to dump.
448  * @p: printer to use for debug.
449  */
450 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
451 				struct drm_printer *p)
452 {
453 	drm_printf(p, "  use_type: %d\n", man->use_type);
454 	drm_printf(p, "  use_tt: %d\n", man->use_tt);
455 	drm_printf(p, "  size: %llu\n", man->size);
456 	drm_printf(p, "  usage: %llu\n", ttm_resource_manager_usage(man));
457 	if (man->func->debug)
458 		man->func->debug(man, p);
459 }
460 EXPORT_SYMBOL(ttm_resource_manager_debug);
461 
462 /**
463  * ttm_resource_manager_first
464  *
465  * @man: resource manager to iterate over
466  * @cursor: cursor to record the position
467  *
468  * Returns the first resource from the resource manager.
469  */
470 struct ttm_resource *
471 ttm_resource_manager_first(struct ttm_resource_manager *man,
472 			   struct ttm_resource_cursor *cursor)
473 {
474 	struct ttm_resource *res;
475 
476 	lockdep_assert_held(&man->bdev->lru_lock);
477 
478 	for (cursor->priority = 0; cursor->priority < TTM_MAX_BO_PRIORITY;
479 	     ++cursor->priority)
480 		list_for_each_entry(res, &man->lru[cursor->priority], lru)
481 			return res;
482 
483 	return NULL;
484 }
485 
486 /**
487  * ttm_resource_manager_next
488  *
489  * @man: resource manager to iterate over
490  * @cursor: cursor to record the position
491  * @res: the current resource pointer
492  *
493  * Returns the next resource from the resource manager.
494  */
495 struct ttm_resource *
496 ttm_resource_manager_next(struct ttm_resource_manager *man,
497 			  struct ttm_resource_cursor *cursor,
498 			  struct ttm_resource *res)
499 {
500 	lockdep_assert_held(&man->bdev->lru_lock);
501 
502 	list_for_each_entry_continue(res, &man->lru[cursor->priority], lru)
503 		return res;
504 
505 	for (++cursor->priority; cursor->priority < TTM_MAX_BO_PRIORITY;
506 	     ++cursor->priority)
507 		list_for_each_entry(res, &man->lru[cursor->priority], lru)
508 			return res;
509 
510 	return NULL;
511 }
512 
513 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
514 					  struct iosys_map *dmap,
515 					  pgoff_t i)
516 {
517 	struct ttm_kmap_iter_iomap *iter_io =
518 		container_of(iter, typeof(*iter_io), base);
519 	void __iomem *addr;
520 
521 retry:
522 	while (i >= iter_io->cache.end) {
523 		iter_io->cache.sg = iter_io->cache.sg ?
524 			sg_next(iter_io->cache.sg) : iter_io->st->sgl;
525 		iter_io->cache.i = iter_io->cache.end;
526 		iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >>
527 			PAGE_SHIFT;
528 		iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) -
529 			iter_io->start;
530 	}
531 
532 	if (i < iter_io->cache.i) {
533 		iter_io->cache.end = 0;
534 		iter_io->cache.sg = NULL;
535 		goto retry;
536 	}
537 
538 	addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs +
539 				       (((resource_size_t)i - iter_io->cache.i)
540 					<< PAGE_SHIFT));
541 	iosys_map_set_vaddr_iomem(dmap, addr);
542 }
543 
544 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter,
545 					    struct iosys_map *map)
546 {
547 	io_mapping_unmap_local(map->vaddr_iomem);
548 }
549 
550 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = {
551 	.map_local =  ttm_kmap_iter_iomap_map_local,
552 	.unmap_local = ttm_kmap_iter_iomap_unmap_local,
553 	.maps_tt = false,
554 };
555 
556 /**
557  * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap
558  * @iter_io: The struct ttm_kmap_iter_iomap to initialize.
559  * @iomap: The struct io_mapping representing the underlying linear io_memory.
560  * @st: sg_table into @iomap, representing the memory of the struct
561  * ttm_resource.
562  * @start: Offset that needs to be subtracted from @st to make
563  * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
564  *
565  * Return: Pointer to the embedded struct ttm_kmap_iter.
566  */
567 struct ttm_kmap_iter *
568 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
569 			 struct io_mapping *iomap,
570 			 struct sg_table *st,
571 			 resource_size_t start)
572 {
573 	iter_io->base.ops = &ttm_kmap_iter_io_ops;
574 	iter_io->iomap = iomap;
575 	iter_io->st = st;
576 	iter_io->start = start;
577 	memset(&iter_io->cache, 0, sizeof(iter_io->cache));
578 
579 	return &iter_io->base;
580 }
581 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init);
582 
583 /**
584  * DOC: Linear io iterator
585  *
586  * This code should die in the not too near future. Best would be if we could
587  * make io-mapping use memremap for all io memory, and have memremap
588  * implement a kmap_local functionality. We could then strip a huge amount of
589  * code. These linear io iterators are implemented to mimic old functionality,
590  * and they don't use kmap_local semantics at all internally. Rather ioremap or
591  * friends, and at least on 32-bit they add global TLB flushes and points
592  * of failure.
593  */
594 
595 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter,
596 					      struct iosys_map *dmap,
597 					      pgoff_t i)
598 {
599 	struct ttm_kmap_iter_linear_io *iter_io =
600 		container_of(iter, typeof(*iter_io), base);
601 
602 	*dmap = iter_io->dmap;
603 	iosys_map_incr(dmap, i * PAGE_SIZE);
604 }
605 
606 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = {
607 	.map_local =  ttm_kmap_iter_linear_io_map_local,
608 	.maps_tt = false,
609 };
610 
611 /**
612  * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory
613  * @iter_io: The iterator to initialize
614  * @bdev: The TTM device
615  * @mem: The ttm resource representing the iomap.
616  *
617  * This function is for internal TTM use only. It sets up a memcpy kmap iterator
618  * pointing at a linear chunk of io memory.
619  *
620  * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on
621  * failure.
622  */
623 struct ttm_kmap_iter *
624 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
625 			     struct ttm_device *bdev,
626 			     struct ttm_resource *mem)
627 {
628 	int ret;
629 
630 	ret = ttm_mem_io_reserve(bdev, mem);
631 	if (ret)
632 		goto out_err;
633 	if (!mem->bus.is_iomem) {
634 		ret = -EINVAL;
635 		goto out_io_free;
636 	}
637 
638 	if (mem->bus.addr) {
639 		iosys_map_set_vaddr(&iter_io->dmap, mem->bus.addr);
640 		iter_io->needs_unmap = false;
641 	} else {
642 		iter_io->needs_unmap = true;
643 		memset(&iter_io->dmap, 0, sizeof(iter_io->dmap));
644 		if (mem->bus.caching == ttm_write_combined)
645 			iosys_map_set_vaddr_iomem(&iter_io->dmap,
646 						  ioremap_wc(mem->bus.offset,
647 							     mem->size));
648 		else if (mem->bus.caching == ttm_cached)
649 			iosys_map_set_vaddr(&iter_io->dmap,
650 					    memremap(mem->bus.offset, mem->size,
651 						     MEMREMAP_WB |
652 						     MEMREMAP_WT |
653 						     MEMREMAP_WC));
654 
655 		/* If uncached requested or if mapping cached or wc failed */
656 		if (iosys_map_is_null(&iter_io->dmap))
657 			iosys_map_set_vaddr_iomem(&iter_io->dmap,
658 						  ioremap(mem->bus.offset,
659 							  mem->size));
660 
661 		if (iosys_map_is_null(&iter_io->dmap)) {
662 			ret = -ENOMEM;
663 			goto out_io_free;
664 		}
665 	}
666 
667 	iter_io->base.ops = &ttm_kmap_iter_linear_io_ops;
668 	return &iter_io->base;
669 
670 out_io_free:
671 	ttm_mem_io_free(bdev, mem);
672 out_err:
673 	return ERR_PTR(ret);
674 }
675 
676 /**
677  * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory
678  * @iter_io: The iterator to initialize
679  * @bdev: The TTM device
680  * @mem: The ttm resource representing the iomap.
681  *
682  * This function is for internal TTM use only. It cleans up a memcpy kmap
683  * iterator initialized by ttm_kmap_iter_linear_io_init.
684  */
685 void
686 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
687 			     struct ttm_device *bdev,
688 			     struct ttm_resource *mem)
689 {
690 	if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) {
691 		if (iter_io->dmap.is_iomem)
692 			iounmap(iter_io->dmap.vaddr_iomem);
693 		else
694 			memunmap(iter_io->dmap.vaddr);
695 	}
696 
697 	ttm_mem_io_free(bdev, mem);
698 }
699 
700 #if defined(CONFIG_DEBUG_FS)
701 
702 static int ttm_resource_manager_show(struct seq_file *m, void *unused)
703 {
704 	struct ttm_resource_manager *man =
705 		(struct ttm_resource_manager *)m->private;
706 	struct drm_printer p = drm_seq_file_printer(m);
707 	ttm_resource_manager_debug(man, &p);
708 	return 0;
709 }
710 DEFINE_SHOW_ATTRIBUTE(ttm_resource_manager);
711 
712 #endif
713 
714 /**
715  * ttm_resource_manager_create_debugfs - Create debugfs entry for specified
716  * resource manager.
717  * @man: The TTM resource manager for which the debugfs stats file be creates
718  * @parent: debugfs directory in which the file will reside
719  * @name: The filename to create.
720  *
721  * This function setups up a debugfs file that can be used to look
722  * at debug statistics of the specified ttm_resource_manager.
723  */
724 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
725 					 struct dentry * parent,
726 					 const char *name)
727 {
728 #if defined(CONFIG_DEBUG_FS)
729 	debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops);
730 #endif
731 }
732 EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);
733