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