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