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