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