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