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