xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_bo.c (revision 675aaf05)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  */
31 
32 #define pr_fmt(fmt) "[TTM] " fmt
33 
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_bo_driver.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/atomic.h>
44 #include <linux/reservation.h>
45 
46 static void ttm_bo_global_kobj_release(struct kobject *kobj);
47 
48 /**
49  * ttm_global_mutex - protecting the global BO state
50  */
51 DEFINE_MUTEX(ttm_global_mutex);
52 unsigned ttm_bo_glob_use_count;
53 struct ttm_bo_global ttm_bo_glob;
54 
55 static struct attribute ttm_bo_count = {
56 	.name = "bo_count",
57 	.mode = S_IRUGO
58 };
59 
60 /* default destructor */
61 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
62 {
63 	kfree(bo);
64 }
65 
66 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
67 					  uint32_t *mem_type)
68 {
69 	int pos;
70 
71 	pos = ffs(place->flags & TTM_PL_MASK_MEM);
72 	if (unlikely(!pos))
73 		return -EINVAL;
74 
75 	*mem_type = pos - 1;
76 	return 0;
77 }
78 
79 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, struct drm_printer *p,
80 			       int mem_type)
81 {
82 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
83 
84 	drm_printf(p, "    has_type: %d\n", man->has_type);
85 	drm_printf(p, "    use_type: %d\n", man->use_type);
86 	drm_printf(p, "    flags: 0x%08X\n", man->flags);
87 	drm_printf(p, "    gpu_offset: 0x%08llX\n", man->gpu_offset);
88 	drm_printf(p, "    size: %llu\n", man->size);
89 	drm_printf(p, "    available_caching: 0x%08X\n", man->available_caching);
90 	drm_printf(p, "    default_caching: 0x%08X\n", man->default_caching);
91 	if (mem_type != TTM_PL_SYSTEM)
92 		(*man->func->debug)(man, p);
93 }
94 
95 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
96 					struct ttm_placement *placement)
97 {
98 	struct drm_printer p = drm_debug_printer(TTM_PFX);
99 	int i, ret, mem_type;
100 
101 	drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
102 		   bo, bo->mem.num_pages, bo->mem.size >> 10,
103 		   bo->mem.size >> 20);
104 	for (i = 0; i < placement->num_placement; i++) {
105 		ret = ttm_mem_type_from_place(&placement->placement[i],
106 						&mem_type);
107 		if (ret)
108 			return;
109 		drm_printf(&p, "  placement[%d]=0x%08X (%d)\n",
110 			   i, placement->placement[i].flags, mem_type);
111 		ttm_mem_type_debug(bo->bdev, &p, mem_type);
112 	}
113 }
114 
115 static ssize_t ttm_bo_global_show(struct kobject *kobj,
116 				  struct attribute *attr,
117 				  char *buffer)
118 {
119 	struct ttm_bo_global *glob =
120 		container_of(kobj, struct ttm_bo_global, kobj);
121 
122 	return snprintf(buffer, PAGE_SIZE, "%d\n",
123 				atomic_read(&glob->bo_count));
124 }
125 
126 static struct attribute *ttm_bo_global_attrs[] = {
127 	&ttm_bo_count,
128 	NULL
129 };
130 
131 static const struct sysfs_ops ttm_bo_global_ops = {
132 	.show = &ttm_bo_global_show
133 };
134 
135 static struct kobj_type ttm_bo_glob_kobj_type  = {
136 	.release = &ttm_bo_global_kobj_release,
137 	.sysfs_ops = &ttm_bo_global_ops,
138 	.default_attrs = ttm_bo_global_attrs
139 };
140 
141 
142 static inline uint32_t ttm_bo_type_flags(unsigned type)
143 {
144 	return 1 << (type);
145 }
146 
147 static void ttm_bo_release_list(struct kref *list_kref)
148 {
149 	struct ttm_buffer_object *bo =
150 	    container_of(list_kref, struct ttm_buffer_object, list_kref);
151 	struct ttm_bo_device *bdev = bo->bdev;
152 	size_t acc_size = bo->acc_size;
153 
154 	BUG_ON(kref_read(&bo->list_kref));
155 	BUG_ON(kref_read(&bo->kref));
156 	BUG_ON(atomic_read(&bo->cpu_writers));
157 	BUG_ON(bo->mem.mm_node != NULL);
158 	BUG_ON(!list_empty(&bo->lru));
159 	BUG_ON(!list_empty(&bo->ddestroy));
160 	ttm_tt_destroy(bo->ttm);
161 	atomic_dec(&bo->bdev->glob->bo_count);
162 	dma_fence_put(bo->moving);
163 	reservation_object_fini(&bo->ttm_resv);
164 	mutex_destroy(&bo->wu_mutex);
165 	bo->destroy(bo);
166 	ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
167 }
168 
169 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
170 {
171 	struct ttm_bo_device *bdev = bo->bdev;
172 	struct ttm_mem_type_manager *man;
173 
174 	reservation_object_assert_held(bo->resv);
175 
176 	if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
177 		BUG_ON(!list_empty(&bo->lru));
178 
179 		man = &bdev->man[bo->mem.mem_type];
180 		list_add_tail(&bo->lru, &man->lru[bo->priority]);
181 		kref_get(&bo->list_kref);
182 
183 		if (bo->ttm && !(bo->ttm->page_flags &
184 				 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED))) {
185 			list_add_tail(&bo->swap,
186 				      &bdev->glob->swap_lru[bo->priority]);
187 			kref_get(&bo->list_kref);
188 		}
189 	}
190 }
191 EXPORT_SYMBOL(ttm_bo_add_to_lru);
192 
193 static void ttm_bo_ref_bug(struct kref *list_kref)
194 {
195 	BUG();
196 }
197 
198 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
199 {
200 	struct ttm_bo_device *bdev = bo->bdev;
201 	bool notify = false;
202 
203 	if (!list_empty(&bo->swap)) {
204 		list_del_init(&bo->swap);
205 		kref_put(&bo->list_kref, ttm_bo_ref_bug);
206 		notify = true;
207 	}
208 	if (!list_empty(&bo->lru)) {
209 		list_del_init(&bo->lru);
210 		kref_put(&bo->list_kref, ttm_bo_ref_bug);
211 		notify = true;
212 	}
213 
214 	if (notify && bdev->driver->del_from_lru_notify)
215 		bdev->driver->del_from_lru_notify(bo);
216 }
217 
218 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
219 {
220 	struct ttm_bo_global *glob = bo->bdev->glob;
221 
222 	spin_lock(&glob->lru_lock);
223 	ttm_bo_del_from_lru(bo);
224 	spin_unlock(&glob->lru_lock);
225 }
226 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
227 
228 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
229 				     struct ttm_buffer_object *bo)
230 {
231 	if (!pos->first)
232 		pos->first = bo;
233 	pos->last = bo;
234 }
235 
236 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
237 			     struct ttm_lru_bulk_move *bulk)
238 {
239 	reservation_object_assert_held(bo->resv);
240 
241 	ttm_bo_del_from_lru(bo);
242 	ttm_bo_add_to_lru(bo);
243 
244 	if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
245 		switch (bo->mem.mem_type) {
246 		case TTM_PL_TT:
247 			ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
248 			break;
249 
250 		case TTM_PL_VRAM:
251 			ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
252 			break;
253 		}
254 		if (bo->ttm && !(bo->ttm->page_flags &
255 				 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
256 			ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
257 	}
258 }
259 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
260 
261 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
262 {
263 	unsigned i;
264 
265 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
266 		struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
267 		struct ttm_mem_type_manager *man;
268 
269 		if (!pos->first)
270 			continue;
271 
272 		reservation_object_assert_held(pos->first->resv);
273 		reservation_object_assert_held(pos->last->resv);
274 
275 		man = &pos->first->bdev->man[TTM_PL_TT];
276 		list_bulk_move_tail(&man->lru[i], &pos->first->lru,
277 				    &pos->last->lru);
278 	}
279 
280 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
281 		struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
282 		struct ttm_mem_type_manager *man;
283 
284 		if (!pos->first)
285 			continue;
286 
287 		reservation_object_assert_held(pos->first->resv);
288 		reservation_object_assert_held(pos->last->resv);
289 
290 		man = &pos->first->bdev->man[TTM_PL_VRAM];
291 		list_bulk_move_tail(&man->lru[i], &pos->first->lru,
292 				    &pos->last->lru);
293 	}
294 
295 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
296 		struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
297 		struct list_head *lru;
298 
299 		if (!pos->first)
300 			continue;
301 
302 		reservation_object_assert_held(pos->first->resv);
303 		reservation_object_assert_held(pos->last->resv);
304 
305 		lru = &pos->first->bdev->glob->swap_lru[i];
306 		list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
307 	}
308 }
309 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
310 
311 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
312 				  struct ttm_mem_reg *mem, bool evict,
313 				  struct ttm_operation_ctx *ctx)
314 {
315 	struct ttm_bo_device *bdev = bo->bdev;
316 	bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
317 	bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
318 	struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
319 	struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
320 	int ret = 0;
321 
322 	if (old_is_pci || new_is_pci ||
323 	    ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
324 		ret = ttm_mem_io_lock(old_man, true);
325 		if (unlikely(ret != 0))
326 			goto out_err;
327 		ttm_bo_unmap_virtual_locked(bo);
328 		ttm_mem_io_unlock(old_man);
329 	}
330 
331 	/*
332 	 * Create and bind a ttm if required.
333 	 */
334 
335 	if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
336 		if (bo->ttm == NULL) {
337 			bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
338 			ret = ttm_tt_create(bo, zero);
339 			if (ret)
340 				goto out_err;
341 		}
342 
343 		ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
344 		if (ret)
345 			goto out_err;
346 
347 		if (mem->mem_type != TTM_PL_SYSTEM) {
348 			ret = ttm_tt_bind(bo->ttm, mem, ctx);
349 			if (ret)
350 				goto out_err;
351 		}
352 
353 		if (bo->mem.mem_type == TTM_PL_SYSTEM) {
354 			if (bdev->driver->move_notify)
355 				bdev->driver->move_notify(bo, evict, mem);
356 			bo->mem = *mem;
357 			mem->mm_node = NULL;
358 			goto moved;
359 		}
360 	}
361 
362 	if (bdev->driver->move_notify)
363 		bdev->driver->move_notify(bo, evict, mem);
364 
365 	if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
366 	    !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
367 		ret = ttm_bo_move_ttm(bo, ctx, mem);
368 	else if (bdev->driver->move)
369 		ret = bdev->driver->move(bo, evict, ctx, mem);
370 	else
371 		ret = ttm_bo_move_memcpy(bo, ctx, mem);
372 
373 	if (ret) {
374 		if (bdev->driver->move_notify) {
375 			swap(*mem, bo->mem);
376 			bdev->driver->move_notify(bo, false, mem);
377 			swap(*mem, bo->mem);
378 		}
379 
380 		goto out_err;
381 	}
382 
383 moved:
384 	if (bo->evicted) {
385 		if (bdev->driver->invalidate_caches) {
386 			ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
387 			if (ret)
388 				pr_err("Can not flush read caches\n");
389 		}
390 		bo->evicted = false;
391 	}
392 
393 	if (bo->mem.mm_node)
394 		bo->offset = (bo->mem.start << PAGE_SHIFT) +
395 		    bdev->man[bo->mem.mem_type].gpu_offset;
396 	else
397 		bo->offset = 0;
398 
399 	ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
400 	return 0;
401 
402 out_err:
403 	new_man = &bdev->man[bo->mem.mem_type];
404 	if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
405 		ttm_tt_destroy(bo->ttm);
406 		bo->ttm = NULL;
407 	}
408 
409 	return ret;
410 }
411 
412 /**
413  * Call bo::reserved.
414  * Will release GPU memory type usage on destruction.
415  * This is the place to put in driver specific hooks to release
416  * driver private resources.
417  * Will release the bo::reserved lock.
418  */
419 
420 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
421 {
422 	if (bo->bdev->driver->move_notify)
423 		bo->bdev->driver->move_notify(bo, false, NULL);
424 
425 	ttm_tt_destroy(bo->ttm);
426 	bo->ttm = NULL;
427 	ttm_bo_mem_put(bo, &bo->mem);
428 }
429 
430 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
431 {
432 	int r;
433 
434 	if (bo->resv == &bo->ttm_resv)
435 		return 0;
436 
437 	BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
438 
439 	r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
440 	if (r)
441 		reservation_object_unlock(&bo->ttm_resv);
442 
443 	return r;
444 }
445 
446 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
447 {
448 	struct reservation_object_list *fobj;
449 	struct dma_fence *fence;
450 	int i;
451 
452 	fobj = reservation_object_get_list(&bo->ttm_resv);
453 	fence = reservation_object_get_excl(&bo->ttm_resv);
454 	if (fence && !fence->ops->signaled)
455 		dma_fence_enable_sw_signaling(fence);
456 
457 	for (i = 0; fobj && i < fobj->shared_count; ++i) {
458 		fence = rcu_dereference_protected(fobj->shared[i],
459 					reservation_object_held(bo->resv));
460 
461 		if (!fence->ops->signaled)
462 			dma_fence_enable_sw_signaling(fence);
463 	}
464 }
465 
466 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
467 {
468 	struct ttm_bo_device *bdev = bo->bdev;
469 	struct ttm_bo_global *glob = bdev->glob;
470 	int ret;
471 
472 	ret = ttm_bo_individualize_resv(bo);
473 	if (ret) {
474 		/* Last resort, if we fail to allocate memory for the
475 		 * fences block for the BO to become idle
476 		 */
477 		reservation_object_wait_timeout_rcu(bo->resv, true, false,
478 						    30 * HZ);
479 		spin_lock(&glob->lru_lock);
480 		goto error;
481 	}
482 
483 	spin_lock(&glob->lru_lock);
484 	ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
485 	if (!ret) {
486 		if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
487 			ttm_bo_del_from_lru(bo);
488 			spin_unlock(&glob->lru_lock);
489 			if (bo->resv != &bo->ttm_resv)
490 				reservation_object_unlock(&bo->ttm_resv);
491 
492 			ttm_bo_cleanup_memtype_use(bo);
493 			reservation_object_unlock(bo->resv);
494 			return;
495 		}
496 
497 		ttm_bo_flush_all_fences(bo);
498 
499 		/*
500 		 * Make NO_EVICT bos immediately available to
501 		 * shrinkers, now that they are queued for
502 		 * destruction.
503 		 */
504 		if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
505 			bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
506 			ttm_bo_add_to_lru(bo);
507 		}
508 
509 		reservation_object_unlock(bo->resv);
510 	}
511 	if (bo->resv != &bo->ttm_resv)
512 		reservation_object_unlock(&bo->ttm_resv);
513 
514 error:
515 	kref_get(&bo->list_kref);
516 	list_add_tail(&bo->ddestroy, &bdev->ddestroy);
517 	spin_unlock(&glob->lru_lock);
518 
519 	schedule_delayed_work(&bdev->wq,
520 			      ((HZ / 100) < 1) ? 1 : HZ / 100);
521 }
522 
523 /**
524  * function ttm_bo_cleanup_refs
525  * If bo idle, remove from delayed- and lru lists, and unref.
526  * If not idle, do nothing.
527  *
528  * Must be called with lru_lock and reservation held, this function
529  * will drop the lru lock and optionally the reservation lock before returning.
530  *
531  * @interruptible         Any sleeps should occur interruptibly.
532  * @no_wait_gpu           Never wait for gpu. Return -EBUSY instead.
533  * @unlock_resv           Unlock the reservation lock as well.
534  */
535 
536 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
537 			       bool interruptible, bool no_wait_gpu,
538 			       bool unlock_resv)
539 {
540 	struct ttm_bo_global *glob = bo->bdev->glob;
541 	struct reservation_object *resv;
542 	int ret;
543 
544 	if (unlikely(list_empty(&bo->ddestroy)))
545 		resv = bo->resv;
546 	else
547 		resv = &bo->ttm_resv;
548 
549 	if (reservation_object_test_signaled_rcu(resv, true))
550 		ret = 0;
551 	else
552 		ret = -EBUSY;
553 
554 	if (ret && !no_wait_gpu) {
555 		long lret;
556 
557 		if (unlock_resv)
558 			reservation_object_unlock(bo->resv);
559 		spin_unlock(&glob->lru_lock);
560 
561 		lret = reservation_object_wait_timeout_rcu(resv, true,
562 							   interruptible,
563 							   30 * HZ);
564 
565 		if (lret < 0)
566 			return lret;
567 		else if (lret == 0)
568 			return -EBUSY;
569 
570 		spin_lock(&glob->lru_lock);
571 		if (unlock_resv && !reservation_object_trylock(bo->resv)) {
572 			/*
573 			 * We raced, and lost, someone else holds the reservation now,
574 			 * and is probably busy in ttm_bo_cleanup_memtype_use.
575 			 *
576 			 * Even if it's not the case, because we finished waiting any
577 			 * delayed destruction would succeed, so just return success
578 			 * here.
579 			 */
580 			spin_unlock(&glob->lru_lock);
581 			return 0;
582 		}
583 		ret = 0;
584 	}
585 
586 	if (ret || unlikely(list_empty(&bo->ddestroy))) {
587 		if (unlock_resv)
588 			reservation_object_unlock(bo->resv);
589 		spin_unlock(&glob->lru_lock);
590 		return ret;
591 	}
592 
593 	ttm_bo_del_from_lru(bo);
594 	list_del_init(&bo->ddestroy);
595 	kref_put(&bo->list_kref, ttm_bo_ref_bug);
596 
597 	spin_unlock(&glob->lru_lock);
598 	ttm_bo_cleanup_memtype_use(bo);
599 
600 	if (unlock_resv)
601 		reservation_object_unlock(bo->resv);
602 
603 	return 0;
604 }
605 
606 /**
607  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
608  * encountered buffers.
609  */
610 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
611 {
612 	struct ttm_bo_global *glob = bdev->glob;
613 	struct list_head removed;
614 	bool empty;
615 
616 	INIT_LIST_HEAD(&removed);
617 
618 	spin_lock(&glob->lru_lock);
619 	while (!list_empty(&bdev->ddestroy)) {
620 		struct ttm_buffer_object *bo;
621 
622 		bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
623 				      ddestroy);
624 		kref_get(&bo->list_kref);
625 		list_move_tail(&bo->ddestroy, &removed);
626 
627 		if (remove_all || bo->resv != &bo->ttm_resv) {
628 			spin_unlock(&glob->lru_lock);
629 			reservation_object_lock(bo->resv, NULL);
630 
631 			spin_lock(&glob->lru_lock);
632 			ttm_bo_cleanup_refs(bo, false, !remove_all, true);
633 
634 		} else if (reservation_object_trylock(bo->resv)) {
635 			ttm_bo_cleanup_refs(bo, false, !remove_all, true);
636 		} else {
637 			spin_unlock(&glob->lru_lock);
638 		}
639 
640 		kref_put(&bo->list_kref, ttm_bo_release_list);
641 		spin_lock(&glob->lru_lock);
642 	}
643 	list_splice_tail(&removed, &bdev->ddestroy);
644 	empty = list_empty(&bdev->ddestroy);
645 	spin_unlock(&glob->lru_lock);
646 
647 	return empty;
648 }
649 
650 static void ttm_bo_delayed_workqueue(struct work_struct *work)
651 {
652 	struct ttm_bo_device *bdev =
653 	    container_of(work, struct ttm_bo_device, wq.work);
654 
655 	if (!ttm_bo_delayed_delete(bdev, false))
656 		schedule_delayed_work(&bdev->wq,
657 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
658 }
659 
660 static void ttm_bo_release(struct kref *kref)
661 {
662 	struct ttm_buffer_object *bo =
663 	    container_of(kref, struct ttm_buffer_object, kref);
664 	struct ttm_bo_device *bdev = bo->bdev;
665 	struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
666 
667 	drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
668 	ttm_mem_io_lock(man, false);
669 	ttm_mem_io_free_vm(bo);
670 	ttm_mem_io_unlock(man);
671 	ttm_bo_cleanup_refs_or_queue(bo);
672 	kref_put(&bo->list_kref, ttm_bo_release_list);
673 }
674 
675 void ttm_bo_put(struct ttm_buffer_object *bo)
676 {
677 	kref_put(&bo->kref, ttm_bo_release);
678 }
679 EXPORT_SYMBOL(ttm_bo_put);
680 
681 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
682 {
683 	return cancel_delayed_work_sync(&bdev->wq);
684 }
685 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
686 
687 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
688 {
689 	if (resched)
690 		schedule_delayed_work(&bdev->wq,
691 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
692 }
693 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
694 
695 static int ttm_bo_evict(struct ttm_buffer_object *bo,
696 			struct ttm_operation_ctx *ctx)
697 {
698 	struct ttm_bo_device *bdev = bo->bdev;
699 	struct ttm_mem_reg evict_mem;
700 	struct ttm_placement placement;
701 	int ret = 0;
702 
703 	reservation_object_assert_held(bo->resv);
704 
705 	placement.num_placement = 0;
706 	placement.num_busy_placement = 0;
707 	bdev->driver->evict_flags(bo, &placement);
708 
709 	if (!placement.num_placement && !placement.num_busy_placement) {
710 		ret = ttm_bo_pipeline_gutting(bo);
711 		if (ret)
712 			return ret;
713 
714 		return ttm_tt_create(bo, false);
715 	}
716 
717 	evict_mem = bo->mem;
718 	evict_mem.mm_node = NULL;
719 	evict_mem.bus.io_reserved_vm = false;
720 	evict_mem.bus.io_reserved_count = 0;
721 
722 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
723 	if (ret) {
724 		if (ret != -ERESTARTSYS) {
725 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
726 			       bo);
727 			ttm_bo_mem_space_debug(bo, &placement);
728 		}
729 		goto out;
730 	}
731 
732 	ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
733 	if (unlikely(ret)) {
734 		if (ret != -ERESTARTSYS)
735 			pr_err("Buffer eviction failed\n");
736 		ttm_bo_mem_put(bo, &evict_mem);
737 		goto out;
738 	}
739 	bo->evicted = true;
740 out:
741 	return ret;
742 }
743 
744 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
745 			      const struct ttm_place *place)
746 {
747 	/* Don't evict this BO if it's outside of the
748 	 * requested placement range
749 	 */
750 	if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
751 	    (place->lpfn && place->lpfn <= bo->mem.start))
752 		return false;
753 
754 	return true;
755 }
756 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
757 
758 /**
759  * Check the target bo is allowable to be evicted or swapout, including cases:
760  *
761  * a. if share same reservation object with ctx->resv, have assumption
762  * reservation objects should already be locked, so not lock again and
763  * return true directly when either the opreation allow_reserved_eviction
764  * or the target bo already is in delayed free list;
765  *
766  * b. Otherwise, trylock it.
767  */
768 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
769 			struct ttm_operation_ctx *ctx, bool *locked)
770 {
771 	bool ret = false;
772 
773 	*locked = false;
774 	if (bo->resv == ctx->resv) {
775 		reservation_object_assert_held(bo->resv);
776 		if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
777 		    || !list_empty(&bo->ddestroy))
778 			ret = true;
779 	} else {
780 		*locked = reservation_object_trylock(bo->resv);
781 		ret = *locked;
782 	}
783 
784 	return ret;
785 }
786 
787 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
788 			       uint32_t mem_type,
789 			       const struct ttm_place *place,
790 			       struct ttm_operation_ctx *ctx)
791 {
792 	struct ttm_bo_global *glob = bdev->glob;
793 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
794 	struct ttm_buffer_object *bo = NULL;
795 	bool locked = false;
796 	unsigned i;
797 	int ret;
798 
799 	spin_lock(&glob->lru_lock);
800 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
801 		list_for_each_entry(bo, &man->lru[i], lru) {
802 			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
803 				continue;
804 
805 			if (place && !bdev->driver->eviction_valuable(bo,
806 								      place)) {
807 				if (locked)
808 					reservation_object_unlock(bo->resv);
809 				continue;
810 			}
811 			break;
812 		}
813 
814 		/* If the inner loop terminated early, we have our candidate */
815 		if (&bo->lru != &man->lru[i])
816 			break;
817 
818 		bo = NULL;
819 	}
820 
821 	if (!bo) {
822 		spin_unlock(&glob->lru_lock);
823 		return -EBUSY;
824 	}
825 
826 	kref_get(&bo->list_kref);
827 
828 	if (!list_empty(&bo->ddestroy)) {
829 		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
830 					  ctx->no_wait_gpu, locked);
831 		kref_put(&bo->list_kref, ttm_bo_release_list);
832 		return ret;
833 	}
834 
835 	ttm_bo_del_from_lru(bo);
836 	spin_unlock(&glob->lru_lock);
837 
838 	ret = ttm_bo_evict(bo, ctx);
839 	if (locked) {
840 		ttm_bo_unreserve(bo);
841 	} else {
842 		spin_lock(&glob->lru_lock);
843 		ttm_bo_add_to_lru(bo);
844 		spin_unlock(&glob->lru_lock);
845 	}
846 
847 	kref_put(&bo->list_kref, ttm_bo_release_list);
848 	return ret;
849 }
850 
851 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
852 {
853 	struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
854 
855 	if (mem->mm_node)
856 		(*man->func->put_node)(man, mem);
857 }
858 EXPORT_SYMBOL(ttm_bo_mem_put);
859 
860 /**
861  * Add the last move fence to the BO and reserve a new shared slot.
862  */
863 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
864 				 struct ttm_mem_type_manager *man,
865 				 struct ttm_mem_reg *mem)
866 {
867 	struct dma_fence *fence;
868 	int ret;
869 
870 	spin_lock(&man->move_lock);
871 	fence = dma_fence_get(man->move);
872 	spin_unlock(&man->move_lock);
873 
874 	if (fence) {
875 		reservation_object_add_shared_fence(bo->resv, fence);
876 
877 		ret = reservation_object_reserve_shared(bo->resv, 1);
878 		if (unlikely(ret)) {
879 			dma_fence_put(fence);
880 			return ret;
881 		}
882 
883 		dma_fence_put(bo->moving);
884 		bo->moving = fence;
885 	}
886 
887 	return 0;
888 }
889 
890 /**
891  * Repeatedly evict memory from the LRU for @mem_type until we create enough
892  * space, or we've evicted everything and there isn't enough space.
893  */
894 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
895 					uint32_t mem_type,
896 					const struct ttm_place *place,
897 					struct ttm_mem_reg *mem,
898 					struct ttm_operation_ctx *ctx)
899 {
900 	struct ttm_bo_device *bdev = bo->bdev;
901 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
902 	int ret;
903 
904 	do {
905 		ret = (*man->func->get_node)(man, bo, place, mem);
906 		if (unlikely(ret != 0))
907 			return ret;
908 		if (mem->mm_node)
909 			break;
910 		ret = ttm_mem_evict_first(bdev, mem_type, place, ctx);
911 		if (unlikely(ret != 0))
912 			return ret;
913 	} while (1);
914 	mem->mem_type = mem_type;
915 	return ttm_bo_add_move_fence(bo, man, mem);
916 }
917 
918 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
919 				      uint32_t cur_placement,
920 				      uint32_t proposed_placement)
921 {
922 	uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
923 	uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
924 
925 	/**
926 	 * Keep current caching if possible.
927 	 */
928 
929 	if ((cur_placement & caching) != 0)
930 		result |= (cur_placement & caching);
931 	else if ((man->default_caching & caching) != 0)
932 		result |= man->default_caching;
933 	else if ((TTM_PL_FLAG_CACHED & caching) != 0)
934 		result |= TTM_PL_FLAG_CACHED;
935 	else if ((TTM_PL_FLAG_WC & caching) != 0)
936 		result |= TTM_PL_FLAG_WC;
937 	else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
938 		result |= TTM_PL_FLAG_UNCACHED;
939 
940 	return result;
941 }
942 
943 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
944 				 uint32_t mem_type,
945 				 const struct ttm_place *place,
946 				 uint32_t *masked_placement)
947 {
948 	uint32_t cur_flags = ttm_bo_type_flags(mem_type);
949 
950 	if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
951 		return false;
952 
953 	if ((place->flags & man->available_caching) == 0)
954 		return false;
955 
956 	cur_flags |= (place->flags & man->available_caching);
957 
958 	*masked_placement = cur_flags;
959 	return true;
960 }
961 
962 /**
963  * Creates space for memory region @mem according to its type.
964  *
965  * This function first searches for free space in compatible memory types in
966  * the priority order defined by the driver.  If free space isn't found, then
967  * ttm_bo_mem_force_space is attempted in priority order to evict and find
968  * space.
969  */
970 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
971 			struct ttm_placement *placement,
972 			struct ttm_mem_reg *mem,
973 			struct ttm_operation_ctx *ctx)
974 {
975 	struct ttm_bo_device *bdev = bo->bdev;
976 	struct ttm_mem_type_manager *man;
977 	uint32_t mem_type = TTM_PL_SYSTEM;
978 	uint32_t cur_flags = 0;
979 	bool type_found = false;
980 	bool type_ok = false;
981 	bool has_erestartsys = false;
982 	int i, ret;
983 
984 	ret = reservation_object_reserve_shared(bo->resv, 1);
985 	if (unlikely(ret))
986 		return ret;
987 
988 	mem->mm_node = NULL;
989 	for (i = 0; i < placement->num_placement; ++i) {
990 		const struct ttm_place *place = &placement->placement[i];
991 
992 		ret = ttm_mem_type_from_place(place, &mem_type);
993 		if (ret)
994 			return ret;
995 		man = &bdev->man[mem_type];
996 		if (!man->has_type || !man->use_type)
997 			continue;
998 
999 		type_ok = ttm_bo_mt_compatible(man, mem_type, place,
1000 						&cur_flags);
1001 
1002 		if (!type_ok)
1003 			continue;
1004 
1005 		type_found = true;
1006 		cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1007 						  cur_flags);
1008 		/*
1009 		 * Use the access and other non-mapping-related flag bits from
1010 		 * the memory placement flags to the current flags
1011 		 */
1012 		ttm_flag_masked(&cur_flags, place->flags,
1013 				~TTM_PL_MASK_MEMTYPE);
1014 
1015 		if (mem_type == TTM_PL_SYSTEM)
1016 			break;
1017 
1018 		ret = (*man->func->get_node)(man, bo, place, mem);
1019 		if (unlikely(ret))
1020 			return ret;
1021 
1022 		if (mem->mm_node) {
1023 			ret = ttm_bo_add_move_fence(bo, man, mem);
1024 			if (unlikely(ret)) {
1025 				(*man->func->put_node)(man, mem);
1026 				return ret;
1027 			}
1028 			break;
1029 		}
1030 	}
1031 
1032 	if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1033 		mem->mem_type = mem_type;
1034 		mem->placement = cur_flags;
1035 		return 0;
1036 	}
1037 
1038 	for (i = 0; i < placement->num_busy_placement; ++i) {
1039 		const struct ttm_place *place = &placement->busy_placement[i];
1040 
1041 		ret = ttm_mem_type_from_place(place, &mem_type);
1042 		if (ret)
1043 			return ret;
1044 		man = &bdev->man[mem_type];
1045 		if (!man->has_type || !man->use_type)
1046 			continue;
1047 		if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1048 			continue;
1049 
1050 		type_found = true;
1051 		cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1052 						  cur_flags);
1053 		/*
1054 		 * Use the access and other non-mapping-related flag bits from
1055 		 * the memory placement flags to the current flags
1056 		 */
1057 		ttm_flag_masked(&cur_flags, place->flags,
1058 				~TTM_PL_MASK_MEMTYPE);
1059 
1060 		if (mem_type == TTM_PL_SYSTEM) {
1061 			mem->mem_type = mem_type;
1062 			mem->placement = cur_flags;
1063 			mem->mm_node = NULL;
1064 			return 0;
1065 		}
1066 
1067 		ret = ttm_bo_mem_force_space(bo, mem_type, place, mem, ctx);
1068 		if (ret == 0 && mem->mm_node) {
1069 			mem->placement = cur_flags;
1070 			return 0;
1071 		}
1072 		if (ret == -ERESTARTSYS)
1073 			has_erestartsys = true;
1074 	}
1075 
1076 	if (!type_found) {
1077 		pr_err(TTM_PFX "No compatible memory type found\n");
1078 		return -EINVAL;
1079 	}
1080 
1081 	return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1082 }
1083 EXPORT_SYMBOL(ttm_bo_mem_space);
1084 
1085 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1086 			      struct ttm_placement *placement,
1087 			      struct ttm_operation_ctx *ctx)
1088 {
1089 	int ret = 0;
1090 	struct ttm_mem_reg mem;
1091 
1092 	reservation_object_assert_held(bo->resv);
1093 
1094 	mem.num_pages = bo->num_pages;
1095 	mem.size = mem.num_pages << PAGE_SHIFT;
1096 	mem.page_alignment = bo->mem.page_alignment;
1097 	mem.bus.io_reserved_vm = false;
1098 	mem.bus.io_reserved_count = 0;
1099 	/*
1100 	 * Determine where to move the buffer.
1101 	 */
1102 	ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1103 	if (ret)
1104 		goto out_unlock;
1105 	ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1106 out_unlock:
1107 	if (ret && mem.mm_node)
1108 		ttm_bo_mem_put(bo, &mem);
1109 	return ret;
1110 }
1111 
1112 static bool ttm_bo_places_compat(const struct ttm_place *places,
1113 				 unsigned num_placement,
1114 				 struct ttm_mem_reg *mem,
1115 				 uint32_t *new_flags)
1116 {
1117 	unsigned i;
1118 
1119 	for (i = 0; i < num_placement; i++) {
1120 		const struct ttm_place *heap = &places[i];
1121 
1122 		if (mem->mm_node && (mem->start < heap->fpfn ||
1123 		     (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1124 			continue;
1125 
1126 		*new_flags = heap->flags;
1127 		if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1128 		    (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1129 		    (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1130 		     (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1131 			return true;
1132 	}
1133 	return false;
1134 }
1135 
1136 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1137 		       struct ttm_mem_reg *mem,
1138 		       uint32_t *new_flags)
1139 {
1140 	if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1141 				 mem, new_flags))
1142 		return true;
1143 
1144 	if ((placement->busy_placement != placement->placement ||
1145 	     placement->num_busy_placement > placement->num_placement) &&
1146 	    ttm_bo_places_compat(placement->busy_placement,
1147 				 placement->num_busy_placement,
1148 				 mem, new_flags))
1149 		return true;
1150 
1151 	return false;
1152 }
1153 EXPORT_SYMBOL(ttm_bo_mem_compat);
1154 
1155 int ttm_bo_validate(struct ttm_buffer_object *bo,
1156 		    struct ttm_placement *placement,
1157 		    struct ttm_operation_ctx *ctx)
1158 {
1159 	int ret;
1160 	uint32_t new_flags;
1161 
1162 	reservation_object_assert_held(bo->resv);
1163 	/*
1164 	 * Check whether we need to move buffer.
1165 	 */
1166 	if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1167 		ret = ttm_bo_move_buffer(bo, placement, ctx);
1168 		if (ret)
1169 			return ret;
1170 	} else {
1171 		/*
1172 		 * Use the access and other non-mapping-related flag bits from
1173 		 * the compatible memory placement flags to the active flags
1174 		 */
1175 		ttm_flag_masked(&bo->mem.placement, new_flags,
1176 				~TTM_PL_MASK_MEMTYPE);
1177 	}
1178 	/*
1179 	 * We might need to add a TTM.
1180 	 */
1181 	if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1182 		ret = ttm_tt_create(bo, true);
1183 		if (ret)
1184 			return ret;
1185 	}
1186 	return 0;
1187 }
1188 EXPORT_SYMBOL(ttm_bo_validate);
1189 
1190 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1191 			 struct ttm_buffer_object *bo,
1192 			 unsigned long size,
1193 			 enum ttm_bo_type type,
1194 			 struct ttm_placement *placement,
1195 			 uint32_t page_alignment,
1196 			 struct ttm_operation_ctx *ctx,
1197 			 size_t acc_size,
1198 			 struct sg_table *sg,
1199 			 struct reservation_object *resv,
1200 			 void (*destroy) (struct ttm_buffer_object *))
1201 {
1202 	int ret = 0;
1203 	unsigned long num_pages;
1204 	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1205 	bool locked;
1206 
1207 	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1208 	if (ret) {
1209 		pr_err("Out of kernel memory\n");
1210 		if (destroy)
1211 			(*destroy)(bo);
1212 		else
1213 			kfree(bo);
1214 		return -ENOMEM;
1215 	}
1216 
1217 	num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1218 	if (num_pages == 0) {
1219 		pr_err("Illegal buffer object size\n");
1220 		if (destroy)
1221 			(*destroy)(bo);
1222 		else
1223 			kfree(bo);
1224 		ttm_mem_global_free(mem_glob, acc_size);
1225 		return -EINVAL;
1226 	}
1227 	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1228 
1229 	kref_init(&bo->kref);
1230 	kref_init(&bo->list_kref);
1231 	atomic_set(&bo->cpu_writers, 0);
1232 	INIT_LIST_HEAD(&bo->lru);
1233 	INIT_LIST_HEAD(&bo->ddestroy);
1234 	INIT_LIST_HEAD(&bo->swap);
1235 	INIT_LIST_HEAD(&bo->io_reserve_lru);
1236 	mutex_init(&bo->wu_mutex);
1237 	bo->bdev = bdev;
1238 	bo->type = type;
1239 	bo->num_pages = num_pages;
1240 	bo->mem.size = num_pages << PAGE_SHIFT;
1241 	bo->mem.mem_type = TTM_PL_SYSTEM;
1242 	bo->mem.num_pages = bo->num_pages;
1243 	bo->mem.mm_node = NULL;
1244 	bo->mem.page_alignment = page_alignment;
1245 	bo->mem.bus.io_reserved_vm = false;
1246 	bo->mem.bus.io_reserved_count = 0;
1247 	bo->moving = NULL;
1248 	bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1249 	bo->acc_size = acc_size;
1250 	bo->sg = sg;
1251 	if (resv) {
1252 		bo->resv = resv;
1253 		reservation_object_assert_held(bo->resv);
1254 	} else {
1255 		bo->resv = &bo->ttm_resv;
1256 	}
1257 	reservation_object_init(&bo->ttm_resv);
1258 	atomic_inc(&bo->bdev->glob->bo_count);
1259 	drm_vma_node_reset(&bo->vma_node);
1260 
1261 	/*
1262 	 * For ttm_bo_type_device buffers, allocate
1263 	 * address space from the device.
1264 	 */
1265 	if (bo->type == ttm_bo_type_device ||
1266 	    bo->type == ttm_bo_type_sg)
1267 		ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1268 					 bo->mem.num_pages);
1269 
1270 	/* passed reservation objects should already be locked,
1271 	 * since otherwise lockdep will be angered in radeon.
1272 	 */
1273 	if (!resv) {
1274 		locked = reservation_object_trylock(bo->resv);
1275 		WARN_ON(!locked);
1276 	}
1277 
1278 	if (likely(!ret))
1279 		ret = ttm_bo_validate(bo, placement, ctx);
1280 
1281 	if (unlikely(ret)) {
1282 		if (!resv)
1283 			ttm_bo_unreserve(bo);
1284 
1285 		ttm_bo_put(bo);
1286 		return ret;
1287 	}
1288 
1289 	if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1290 		spin_lock(&bdev->glob->lru_lock);
1291 		ttm_bo_add_to_lru(bo);
1292 		spin_unlock(&bdev->glob->lru_lock);
1293 	}
1294 
1295 	return ret;
1296 }
1297 EXPORT_SYMBOL(ttm_bo_init_reserved);
1298 
1299 int ttm_bo_init(struct ttm_bo_device *bdev,
1300 		struct ttm_buffer_object *bo,
1301 		unsigned long size,
1302 		enum ttm_bo_type type,
1303 		struct ttm_placement *placement,
1304 		uint32_t page_alignment,
1305 		bool interruptible,
1306 		size_t acc_size,
1307 		struct sg_table *sg,
1308 		struct reservation_object *resv,
1309 		void (*destroy) (struct ttm_buffer_object *))
1310 {
1311 	struct ttm_operation_ctx ctx = { interruptible, false };
1312 	int ret;
1313 
1314 	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1315 				   page_alignment, &ctx, acc_size,
1316 				   sg, resv, destroy);
1317 	if (ret)
1318 		return ret;
1319 
1320 	if (!resv)
1321 		ttm_bo_unreserve(bo);
1322 
1323 	return 0;
1324 }
1325 EXPORT_SYMBOL(ttm_bo_init);
1326 
1327 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1328 		       unsigned long bo_size,
1329 		       unsigned struct_size)
1330 {
1331 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1332 	size_t size = 0;
1333 
1334 	size += ttm_round_pot(struct_size);
1335 	size += ttm_round_pot(npages * sizeof(void *));
1336 	size += ttm_round_pot(sizeof(struct ttm_tt));
1337 	return size;
1338 }
1339 EXPORT_SYMBOL(ttm_bo_acc_size);
1340 
1341 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1342 			   unsigned long bo_size,
1343 			   unsigned struct_size)
1344 {
1345 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1346 	size_t size = 0;
1347 
1348 	size += ttm_round_pot(struct_size);
1349 	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1350 	size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1351 	return size;
1352 }
1353 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1354 
1355 int ttm_bo_create(struct ttm_bo_device *bdev,
1356 			unsigned long size,
1357 			enum ttm_bo_type type,
1358 			struct ttm_placement *placement,
1359 			uint32_t page_alignment,
1360 			bool interruptible,
1361 			struct ttm_buffer_object **p_bo)
1362 {
1363 	struct ttm_buffer_object *bo;
1364 	size_t acc_size;
1365 	int ret;
1366 
1367 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1368 	if (unlikely(bo == NULL))
1369 		return -ENOMEM;
1370 
1371 	acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1372 	ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1373 			  interruptible, acc_size,
1374 			  NULL, NULL, NULL);
1375 	if (likely(ret == 0))
1376 		*p_bo = bo;
1377 
1378 	return ret;
1379 }
1380 EXPORT_SYMBOL(ttm_bo_create);
1381 
1382 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1383 				   unsigned mem_type)
1384 {
1385 	struct ttm_operation_ctx ctx = {
1386 		.interruptible = false,
1387 		.no_wait_gpu = false,
1388 		.flags = TTM_OPT_FLAG_FORCE_ALLOC
1389 	};
1390 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1391 	struct ttm_bo_global *glob = bdev->glob;
1392 	struct dma_fence *fence;
1393 	int ret;
1394 	unsigned i;
1395 
1396 	/*
1397 	 * Can't use standard list traversal since we're unlocking.
1398 	 */
1399 
1400 	spin_lock(&glob->lru_lock);
1401 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1402 		while (!list_empty(&man->lru[i])) {
1403 			spin_unlock(&glob->lru_lock);
1404 			ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx);
1405 			if (ret)
1406 				return ret;
1407 			spin_lock(&glob->lru_lock);
1408 		}
1409 	}
1410 	spin_unlock(&glob->lru_lock);
1411 
1412 	spin_lock(&man->move_lock);
1413 	fence = dma_fence_get(man->move);
1414 	spin_unlock(&man->move_lock);
1415 
1416 	if (fence) {
1417 		ret = dma_fence_wait(fence, false);
1418 		dma_fence_put(fence);
1419 		if (ret)
1420 			return ret;
1421 	}
1422 
1423 	return 0;
1424 }
1425 
1426 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1427 {
1428 	struct ttm_mem_type_manager *man;
1429 	int ret = -EINVAL;
1430 
1431 	if (mem_type >= TTM_NUM_MEM_TYPES) {
1432 		pr_err("Illegal memory type %d\n", mem_type);
1433 		return ret;
1434 	}
1435 	man = &bdev->man[mem_type];
1436 
1437 	if (!man->has_type) {
1438 		pr_err("Trying to take down uninitialized memory manager type %u\n",
1439 		       mem_type);
1440 		return ret;
1441 	}
1442 
1443 	man->use_type = false;
1444 	man->has_type = false;
1445 
1446 	ret = 0;
1447 	if (mem_type > 0) {
1448 		ret = ttm_bo_force_list_clean(bdev, mem_type);
1449 		if (ret) {
1450 			pr_err("Cleanup eviction failed\n");
1451 			return ret;
1452 		}
1453 
1454 		ret = (*man->func->takedown)(man);
1455 	}
1456 
1457 	dma_fence_put(man->move);
1458 	man->move = NULL;
1459 
1460 	return ret;
1461 }
1462 EXPORT_SYMBOL(ttm_bo_clean_mm);
1463 
1464 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1465 {
1466 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1467 
1468 	if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1469 		pr_err("Illegal memory manager memory type %u\n", mem_type);
1470 		return -EINVAL;
1471 	}
1472 
1473 	if (!man->has_type) {
1474 		pr_err("Memory type %u has not been initialized\n", mem_type);
1475 		return 0;
1476 	}
1477 
1478 	return ttm_bo_force_list_clean(bdev, mem_type);
1479 }
1480 EXPORT_SYMBOL(ttm_bo_evict_mm);
1481 
1482 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1483 			unsigned long p_size)
1484 {
1485 	int ret;
1486 	struct ttm_mem_type_manager *man;
1487 	unsigned i;
1488 
1489 	BUG_ON(type >= TTM_NUM_MEM_TYPES);
1490 	man = &bdev->man[type];
1491 	BUG_ON(man->has_type);
1492 	man->io_reserve_fastpath = true;
1493 	man->use_io_reserve_lru = false;
1494 	mutex_init(&man->io_reserve_mutex);
1495 	spin_lock_init(&man->move_lock);
1496 	INIT_LIST_HEAD(&man->io_reserve_lru);
1497 
1498 	ret = bdev->driver->init_mem_type(bdev, type, man);
1499 	if (ret)
1500 		return ret;
1501 	man->bdev = bdev;
1502 
1503 	if (type != TTM_PL_SYSTEM) {
1504 		ret = (*man->func->init)(man, p_size);
1505 		if (ret)
1506 			return ret;
1507 	}
1508 	man->has_type = true;
1509 	man->use_type = true;
1510 	man->size = p_size;
1511 
1512 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1513 		INIT_LIST_HEAD(&man->lru[i]);
1514 	man->move = NULL;
1515 
1516 	return 0;
1517 }
1518 EXPORT_SYMBOL(ttm_bo_init_mm);
1519 
1520 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1521 {
1522 	struct ttm_bo_global *glob =
1523 		container_of(kobj, struct ttm_bo_global, kobj);
1524 
1525 	__free_page(glob->dummy_read_page);
1526 }
1527 
1528 static void ttm_bo_global_release(void)
1529 {
1530 	struct ttm_bo_global *glob = &ttm_bo_glob;
1531 
1532 	mutex_lock(&ttm_global_mutex);
1533 	if (--ttm_bo_glob_use_count > 0)
1534 		goto out;
1535 
1536 	kobject_del(&glob->kobj);
1537 	kobject_put(&glob->kobj);
1538 	ttm_mem_global_release(&ttm_mem_glob);
1539 	memset(glob, 0, sizeof(*glob));
1540 out:
1541 	mutex_unlock(&ttm_global_mutex);
1542 }
1543 
1544 static int ttm_bo_global_init(void)
1545 {
1546 	struct ttm_bo_global *glob = &ttm_bo_glob;
1547 	int ret = 0;
1548 	unsigned i;
1549 
1550 	mutex_lock(&ttm_global_mutex);
1551 	if (++ttm_bo_glob_use_count > 1)
1552 		goto out;
1553 
1554 	ret = ttm_mem_global_init(&ttm_mem_glob);
1555 	if (ret)
1556 		goto out;
1557 
1558 	spin_lock_init(&glob->lru_lock);
1559 	glob->mem_glob = &ttm_mem_glob;
1560 	glob->mem_glob->bo_glob = glob;
1561 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1562 
1563 	if (unlikely(glob->dummy_read_page == NULL)) {
1564 		ret = -ENOMEM;
1565 		goto out;
1566 	}
1567 
1568 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1569 		INIT_LIST_HEAD(&glob->swap_lru[i]);
1570 	INIT_LIST_HEAD(&glob->device_list);
1571 	atomic_set(&glob->bo_count, 0);
1572 
1573 	ret = kobject_init_and_add(
1574 		&glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1575 	if (unlikely(ret != 0))
1576 		kobject_put(&glob->kobj);
1577 out:
1578 	mutex_unlock(&ttm_global_mutex);
1579 	return ret;
1580 }
1581 
1582 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1583 {
1584 	int ret = 0;
1585 	unsigned i = TTM_NUM_MEM_TYPES;
1586 	struct ttm_mem_type_manager *man;
1587 	struct ttm_bo_global *glob = bdev->glob;
1588 
1589 	while (i--) {
1590 		man = &bdev->man[i];
1591 		if (man->has_type) {
1592 			man->use_type = false;
1593 			if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1594 				ret = -EBUSY;
1595 				pr_err("DRM memory manager type %d is not clean\n",
1596 				       i);
1597 			}
1598 			man->has_type = false;
1599 		}
1600 	}
1601 
1602 	mutex_lock(&ttm_global_mutex);
1603 	list_del(&bdev->device_list);
1604 	mutex_unlock(&ttm_global_mutex);
1605 
1606 	cancel_delayed_work_sync(&bdev->wq);
1607 
1608 	if (ttm_bo_delayed_delete(bdev, true))
1609 		pr_debug("Delayed destroy list was clean\n");
1610 
1611 	spin_lock(&glob->lru_lock);
1612 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1613 		if (list_empty(&bdev->man[0].lru[0]))
1614 			pr_debug("Swap list %d was clean\n", i);
1615 	spin_unlock(&glob->lru_lock);
1616 
1617 	drm_vma_offset_manager_destroy(&bdev->vma_manager);
1618 
1619 	if (!ret)
1620 		ttm_bo_global_release();
1621 
1622 	return ret;
1623 }
1624 EXPORT_SYMBOL(ttm_bo_device_release);
1625 
1626 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1627 		       struct ttm_bo_driver *driver,
1628 		       struct address_space *mapping,
1629 		       bool need_dma32)
1630 {
1631 	struct ttm_bo_global *glob = &ttm_bo_glob;
1632 	int ret;
1633 
1634 	ret = ttm_bo_global_init();
1635 	if (ret)
1636 		return ret;
1637 
1638 	bdev->driver = driver;
1639 
1640 	memset(bdev->man, 0, sizeof(bdev->man));
1641 
1642 	/*
1643 	 * Initialize the system memory buffer type.
1644 	 * Other types need to be driver / IOCTL initialized.
1645 	 */
1646 	ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1647 	if (unlikely(ret != 0))
1648 		goto out_no_sys;
1649 
1650 	drm_vma_offset_manager_init(&bdev->vma_manager,
1651 				    DRM_FILE_PAGE_OFFSET_START,
1652 				    DRM_FILE_PAGE_OFFSET_SIZE);
1653 	INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1654 	INIT_LIST_HEAD(&bdev->ddestroy);
1655 	bdev->dev_mapping = mapping;
1656 	bdev->glob = glob;
1657 	bdev->need_dma32 = need_dma32;
1658 	mutex_lock(&ttm_global_mutex);
1659 	list_add_tail(&bdev->device_list, &glob->device_list);
1660 	mutex_unlock(&ttm_global_mutex);
1661 
1662 	return 0;
1663 out_no_sys:
1664 	ttm_bo_global_release();
1665 	return ret;
1666 }
1667 EXPORT_SYMBOL(ttm_bo_device_init);
1668 
1669 /*
1670  * buffer object vm functions.
1671  */
1672 
1673 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1674 {
1675 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1676 
1677 	if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1678 		if (mem->mem_type == TTM_PL_SYSTEM)
1679 			return false;
1680 
1681 		if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1682 			return false;
1683 
1684 		if (mem->placement & TTM_PL_FLAG_CACHED)
1685 			return false;
1686 	}
1687 	return true;
1688 }
1689 
1690 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1691 {
1692 	struct ttm_bo_device *bdev = bo->bdev;
1693 
1694 	drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
1695 	ttm_mem_io_free_vm(bo);
1696 }
1697 
1698 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1699 {
1700 	struct ttm_bo_device *bdev = bo->bdev;
1701 	struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1702 
1703 	ttm_mem_io_lock(man, false);
1704 	ttm_bo_unmap_virtual_locked(bo);
1705 	ttm_mem_io_unlock(man);
1706 }
1707 
1708 
1709 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1710 
1711 int ttm_bo_wait(struct ttm_buffer_object *bo,
1712 		bool interruptible, bool no_wait)
1713 {
1714 	long timeout = 15 * HZ;
1715 
1716 	if (no_wait) {
1717 		if (reservation_object_test_signaled_rcu(bo->resv, true))
1718 			return 0;
1719 		else
1720 			return -EBUSY;
1721 	}
1722 
1723 	timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1724 						      interruptible, timeout);
1725 	if (timeout < 0)
1726 		return timeout;
1727 
1728 	if (timeout == 0)
1729 		return -EBUSY;
1730 
1731 	reservation_object_add_excl_fence(bo->resv, NULL);
1732 	return 0;
1733 }
1734 EXPORT_SYMBOL(ttm_bo_wait);
1735 
1736 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1737 {
1738 	int ret = 0;
1739 
1740 	/*
1741 	 * Using ttm_bo_reserve makes sure the lru lists are updated.
1742 	 */
1743 
1744 	ret = ttm_bo_reserve(bo, true, no_wait, NULL);
1745 	if (unlikely(ret != 0))
1746 		return ret;
1747 	ret = ttm_bo_wait(bo, true, no_wait);
1748 	if (likely(ret == 0))
1749 		atomic_inc(&bo->cpu_writers);
1750 	ttm_bo_unreserve(bo);
1751 	return ret;
1752 }
1753 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1754 
1755 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1756 {
1757 	atomic_dec(&bo->cpu_writers);
1758 }
1759 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1760 
1761 /**
1762  * A buffer object shrink method that tries to swap out the first
1763  * buffer object on the bo_global::swap_lru list.
1764  */
1765 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1766 {
1767 	struct ttm_buffer_object *bo;
1768 	int ret = -EBUSY;
1769 	bool locked;
1770 	unsigned i;
1771 
1772 	spin_lock(&glob->lru_lock);
1773 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1774 		list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1775 			if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
1776 				ret = 0;
1777 				break;
1778 			}
1779 		}
1780 		if (!ret)
1781 			break;
1782 	}
1783 
1784 	if (ret) {
1785 		spin_unlock(&glob->lru_lock);
1786 		return ret;
1787 	}
1788 
1789 	kref_get(&bo->list_kref);
1790 
1791 	if (!list_empty(&bo->ddestroy)) {
1792 		ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1793 		kref_put(&bo->list_kref, ttm_bo_release_list);
1794 		return ret;
1795 	}
1796 
1797 	ttm_bo_del_from_lru(bo);
1798 	spin_unlock(&glob->lru_lock);
1799 
1800 	/**
1801 	 * Move to system cached
1802 	 */
1803 
1804 	if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1805 	    bo->ttm->caching_state != tt_cached) {
1806 		struct ttm_operation_ctx ctx = { false, false };
1807 		struct ttm_mem_reg evict_mem;
1808 
1809 		evict_mem = bo->mem;
1810 		evict_mem.mm_node = NULL;
1811 		evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1812 		evict_mem.mem_type = TTM_PL_SYSTEM;
1813 
1814 		ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1815 		if (unlikely(ret != 0))
1816 			goto out;
1817 	}
1818 
1819 	/**
1820 	 * Make sure BO is idle.
1821 	 */
1822 
1823 	ret = ttm_bo_wait(bo, false, false);
1824 	if (unlikely(ret != 0))
1825 		goto out;
1826 
1827 	ttm_bo_unmap_virtual(bo);
1828 
1829 	/**
1830 	 * Swap out. Buffer will be swapped in again as soon as
1831 	 * anyone tries to access a ttm page.
1832 	 */
1833 
1834 	if (bo->bdev->driver->swap_notify)
1835 		bo->bdev->driver->swap_notify(bo);
1836 
1837 	ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1838 out:
1839 
1840 	/**
1841 	 *
1842 	 * Unreserve without putting on LRU to avoid swapping out an
1843 	 * already swapped buffer.
1844 	 */
1845 	if (locked)
1846 		reservation_object_unlock(bo->resv);
1847 	kref_put(&bo->list_kref, ttm_bo_release_list);
1848 	return ret;
1849 }
1850 EXPORT_SYMBOL(ttm_bo_swapout);
1851 
1852 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1853 {
1854 	struct ttm_operation_ctx ctx = {
1855 		.interruptible = false,
1856 		.no_wait_gpu = false
1857 	};
1858 
1859 	while (ttm_bo_swapout(bdev->glob, &ctx) == 0)
1860 		;
1861 }
1862 EXPORT_SYMBOL(ttm_bo_swapout_all);
1863 
1864 /**
1865  * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1866  * unreserved
1867  *
1868  * @bo: Pointer to buffer
1869  */
1870 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1871 {
1872 	int ret;
1873 
1874 	/*
1875 	 * In the absense of a wait_unlocked API,
1876 	 * Use the bo::wu_mutex to avoid triggering livelocks due to
1877 	 * concurrent use of this function. Note that this use of
1878 	 * bo::wu_mutex can go away if we change locking order to
1879 	 * mmap_sem -> bo::reserve.
1880 	 */
1881 	ret = mutex_lock_interruptible(&bo->wu_mutex);
1882 	if (unlikely(ret != 0))
1883 		return -ERESTARTSYS;
1884 	if (!ww_mutex_is_locked(&bo->resv->lock))
1885 		goto out_unlock;
1886 	ret = reservation_object_lock_interruptible(bo->resv, NULL);
1887 	if (ret == -EINTR)
1888 		ret = -ERESTARTSYS;
1889 	if (unlikely(ret != 0))
1890 		goto out_unlock;
1891 	reservation_object_unlock(bo->resv);
1892 
1893 out_unlock:
1894 	mutex_unlock(&bo->wu_mutex);
1895 	return ret;
1896 }
1897