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