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