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