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