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