20fbfd9222665872f43edc2eb1cecb56da33c1d2
[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_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/dma-resv.h>
44
45 #include "ttm_module.h"
46
47 /* default destructor */
48 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
49 {
50         kfree(bo);
51 }
52
53 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
54                                         struct ttm_placement *placement)
55 {
56         struct drm_printer p = drm_debug_printer(TTM_PFX);
57         struct ttm_resource_manager *man;
58         int i, mem_type;
59
60         drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
61                    bo, bo->resource->num_pages, bo->base.size >> 10,
62                    bo->base.size >> 20);
63         for (i = 0; i < placement->num_placement; i++) {
64                 mem_type = placement->placement[i].mem_type;
65                 drm_printf(&p, "  placement[%d]=0x%08X (%d)\n",
66                            i, placement->placement[i].flags, mem_type);
67                 man = ttm_manager_type(bo->bdev, mem_type);
68                 ttm_resource_manager_debug(man, &p);
69         }
70 }
71
72 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
73 {
74         struct ttm_device *bdev = bo->bdev;
75
76         list_del_init(&bo->lru);
77
78         if (bdev->funcs->del_from_lru_notify)
79                 bdev->funcs->del_from_lru_notify(bo);
80 }
81
82 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
83                                      struct ttm_buffer_object *bo)
84 {
85         if (!pos->first)
86                 pos->first = bo;
87         pos->last = bo;
88 }
89
90 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
91                              struct ttm_resource *mem,
92                              struct ttm_lru_bulk_move *bulk)
93 {
94         struct ttm_device *bdev = bo->bdev;
95         struct ttm_resource_manager *man;
96
97         if (!bo->deleted)
98                 dma_resv_assert_held(bo->base.resv);
99
100         if (bo->pin_count) {
101                 ttm_bo_del_from_lru(bo);
102                 return;
103         }
104
105         man = ttm_manager_type(bdev, mem->mem_type);
106         list_move_tail(&bo->lru, &man->lru[bo->priority]);
107
108         if (bdev->funcs->del_from_lru_notify)
109                 bdev->funcs->del_from_lru_notify(bo);
110
111         if (bulk && !bo->pin_count) {
112                 switch (bo->resource->mem_type) {
113                 case TTM_PL_TT:
114                         ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
115                         break;
116
117                 case TTM_PL_VRAM:
118                         ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
119                         break;
120                 }
121         }
122 }
123 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
124
125 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
126 {
127         unsigned i;
128
129         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
130                 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
131                 struct ttm_resource_manager *man;
132
133                 if (!pos->first)
134                         continue;
135
136                 dma_resv_assert_held(pos->first->base.resv);
137                 dma_resv_assert_held(pos->last->base.resv);
138
139                 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
140                 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
141                                     &pos->last->lru);
142         }
143
144         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
145                 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
146                 struct ttm_resource_manager *man;
147
148                 if (!pos->first)
149                         continue;
150
151                 dma_resv_assert_held(pos->first->base.resv);
152                 dma_resv_assert_held(pos->last->base.resv);
153
154                 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
155                 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
156                                     &pos->last->lru);
157         }
158 }
159 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
160
161 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
162                                   struct ttm_resource *mem, bool evict,
163                                   struct ttm_operation_ctx *ctx,
164                                   struct ttm_place *hop)
165 {
166         struct ttm_resource_manager *old_man, *new_man;
167         struct ttm_device *bdev = bo->bdev;
168         int ret;
169
170         old_man = ttm_manager_type(bdev, bo->resource->mem_type);
171         new_man = ttm_manager_type(bdev, mem->mem_type);
172
173         ttm_bo_unmap_virtual(bo);
174
175         /*
176          * Create and bind a ttm if required.
177          */
178
179         if (new_man->use_tt) {
180                 /* Zero init the new TTM structure if the old location should
181                  * have used one as well.
182                  */
183                 ret = ttm_tt_create(bo, old_man->use_tt);
184                 if (ret)
185                         goto out_err;
186
187                 if (mem->mem_type != TTM_PL_SYSTEM) {
188                         ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
189                         if (ret)
190                                 goto out_err;
191                 }
192         }
193
194         ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
195         if (ret) {
196                 if (ret == -EMULTIHOP)
197                         return ret;
198                 goto out_err;
199         }
200
201         ctx->bytes_moved += bo->base.size;
202         return 0;
203
204 out_err:
205         new_man = ttm_manager_type(bdev, bo->resource->mem_type);
206         if (!new_man->use_tt)
207                 ttm_bo_tt_destroy(bo);
208
209         return ret;
210 }
211
212 /*
213  * Call bo::reserved.
214  * Will release GPU memory type usage on destruction.
215  * This is the place to put in driver specific hooks to release
216  * driver private resources.
217  * Will release the bo::reserved lock.
218  */
219
220 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
221 {
222         if (bo->bdev->funcs->delete_mem_notify)
223                 bo->bdev->funcs->delete_mem_notify(bo);
224
225         ttm_bo_tt_destroy(bo);
226         ttm_resource_free(bo, &bo->resource);
227 }
228
229 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
230 {
231         int r;
232
233         if (bo->base.resv == &bo->base._resv)
234                 return 0;
235
236         BUG_ON(!dma_resv_trylock(&bo->base._resv));
237
238         r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
239         dma_resv_unlock(&bo->base._resv);
240         if (r)
241                 return r;
242
243         if (bo->type != ttm_bo_type_sg) {
244                 /* This works because the BO is about to be destroyed and nobody
245                  * reference it any more. The only tricky case is the trylock on
246                  * the resv object while holding the lru_lock.
247                  */
248                 spin_lock(&bo->bdev->lru_lock);
249                 bo->base.resv = &bo->base._resv;
250                 spin_unlock(&bo->bdev->lru_lock);
251         }
252
253         return r;
254 }
255
256 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
257 {
258         struct dma_resv *resv = &bo->base._resv;
259         struct dma_resv_list *fobj;
260         struct dma_fence *fence;
261         int i;
262
263         rcu_read_lock();
264         fobj = dma_resv_shared_list(resv);
265         fence = dma_resv_excl_fence(resv);
266         if (fence && !fence->ops->signaled)
267                 dma_fence_enable_sw_signaling(fence);
268
269         for (i = 0; fobj && i < fobj->shared_count; ++i) {
270                 fence = rcu_dereference(fobj->shared[i]);
271
272                 if (!fence->ops->signaled)
273                         dma_fence_enable_sw_signaling(fence);
274         }
275         rcu_read_unlock();
276 }
277
278 /**
279  * ttm_bo_cleanup_refs
280  * If bo idle, remove from lru lists, and unref.
281  * If not idle, block if possible.
282  *
283  * Must be called with lru_lock and reservation held, this function
284  * will drop the lru lock and optionally the reservation lock before returning.
285  *
286  * @bo:                    The buffer object to clean-up
287  * @interruptible:         Any sleeps should occur interruptibly.
288  * @no_wait_gpu:           Never wait for gpu. Return -EBUSY instead.
289  * @unlock_resv:           Unlock the reservation lock as well.
290  */
291
292 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
293                                bool interruptible, bool no_wait_gpu,
294                                bool unlock_resv)
295 {
296         struct dma_resv *resv = &bo->base._resv;
297         int ret;
298
299         if (dma_resv_test_signaled(resv, true))
300                 ret = 0;
301         else
302                 ret = -EBUSY;
303
304         if (ret && !no_wait_gpu) {
305                 long lret;
306
307                 if (unlock_resv)
308                         dma_resv_unlock(bo->base.resv);
309                 spin_unlock(&bo->bdev->lru_lock);
310
311                 lret = dma_resv_wait_timeout(resv, true, interruptible,
312                                              30 * HZ);
313
314                 if (lret < 0)
315                         return lret;
316                 else if (lret == 0)
317                         return -EBUSY;
318
319                 spin_lock(&bo->bdev->lru_lock);
320                 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
321                         /*
322                          * We raced, and lost, someone else holds the reservation now,
323                          * and is probably busy in ttm_bo_cleanup_memtype_use.
324                          *
325                          * Even if it's not the case, because we finished waiting any
326                          * delayed destruction would succeed, so just return success
327                          * here.
328                          */
329                         spin_unlock(&bo->bdev->lru_lock);
330                         return 0;
331                 }
332                 ret = 0;
333         }
334
335         if (ret || unlikely(list_empty(&bo->ddestroy))) {
336                 if (unlock_resv)
337                         dma_resv_unlock(bo->base.resv);
338                 spin_unlock(&bo->bdev->lru_lock);
339                 return ret;
340         }
341
342         ttm_bo_del_from_lru(bo);
343         list_del_init(&bo->ddestroy);
344         spin_unlock(&bo->bdev->lru_lock);
345         ttm_bo_cleanup_memtype_use(bo);
346
347         if (unlock_resv)
348                 dma_resv_unlock(bo->base.resv);
349
350         ttm_bo_put(bo);
351
352         return 0;
353 }
354
355 /*
356  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
357  * encountered buffers.
358  */
359 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
360 {
361         struct list_head removed;
362         bool empty;
363
364         INIT_LIST_HEAD(&removed);
365
366         spin_lock(&bdev->lru_lock);
367         while (!list_empty(&bdev->ddestroy)) {
368                 struct ttm_buffer_object *bo;
369
370                 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
371                                       ddestroy);
372                 list_move_tail(&bo->ddestroy, &removed);
373                 if (!ttm_bo_get_unless_zero(bo))
374                         continue;
375
376                 if (remove_all || bo->base.resv != &bo->base._resv) {
377                         spin_unlock(&bdev->lru_lock);
378                         dma_resv_lock(bo->base.resv, NULL);
379
380                         spin_lock(&bdev->lru_lock);
381                         ttm_bo_cleanup_refs(bo, false, !remove_all, true);
382
383                 } else if (dma_resv_trylock(bo->base.resv)) {
384                         ttm_bo_cleanup_refs(bo, false, !remove_all, true);
385                 } else {
386                         spin_unlock(&bdev->lru_lock);
387                 }
388
389                 ttm_bo_put(bo);
390                 spin_lock(&bdev->lru_lock);
391         }
392         list_splice_tail(&removed, &bdev->ddestroy);
393         empty = list_empty(&bdev->ddestroy);
394         spin_unlock(&bdev->lru_lock);
395
396         return empty;
397 }
398
399 static void ttm_bo_release(struct kref *kref)
400 {
401         struct ttm_buffer_object *bo =
402             container_of(kref, struct ttm_buffer_object, kref);
403         struct ttm_device *bdev = bo->bdev;
404         int ret;
405
406         WARN_ON_ONCE(bo->pin_count);
407
408         if (!bo->deleted) {
409                 ret = ttm_bo_individualize_resv(bo);
410                 if (ret) {
411                         /* Last resort, if we fail to allocate memory for the
412                          * fences block for the BO to become idle
413                          */
414                         dma_resv_wait_timeout(bo->base.resv, true, false,
415                                               30 * HZ);
416                 }
417
418                 if (bo->bdev->funcs->release_notify)
419                         bo->bdev->funcs->release_notify(bo);
420
421                 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
422                 ttm_mem_io_free(bdev, bo->resource);
423         }
424
425         if (!dma_resv_test_signaled(bo->base.resv, true) ||
426             !dma_resv_trylock(bo->base.resv)) {
427                 /* The BO is not idle, resurrect it for delayed destroy */
428                 ttm_bo_flush_all_fences(bo);
429                 bo->deleted = true;
430
431                 spin_lock(&bo->bdev->lru_lock);
432
433                 /*
434                  * Make pinned bos immediately available to
435                  * shrinkers, now that they are queued for
436                  * destruction.
437                  *
438                  * FIXME: QXL is triggering this. Can be removed when the
439                  * driver is fixed.
440                  */
441                 if (bo->pin_count) {
442                         bo->pin_count = 0;
443                         ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
444                 }
445
446                 kref_init(&bo->kref);
447                 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
448                 spin_unlock(&bo->bdev->lru_lock);
449
450                 schedule_delayed_work(&bdev->wq,
451                                       ((HZ / 100) < 1) ? 1 : HZ / 100);
452                 return;
453         }
454
455         spin_lock(&bo->bdev->lru_lock);
456         ttm_bo_del_from_lru(bo);
457         list_del(&bo->ddestroy);
458         spin_unlock(&bo->bdev->lru_lock);
459
460         ttm_bo_cleanup_memtype_use(bo);
461         dma_resv_unlock(bo->base.resv);
462
463         atomic_dec(&ttm_glob.bo_count);
464         dma_fence_put(bo->moving);
465         bo->destroy(bo);
466 }
467
468 void ttm_bo_put(struct ttm_buffer_object *bo)
469 {
470         kref_put(&bo->kref, ttm_bo_release);
471 }
472 EXPORT_SYMBOL(ttm_bo_put);
473
474 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
475 {
476         return cancel_delayed_work_sync(&bdev->wq);
477 }
478 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
479
480 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
481 {
482         if (resched)
483                 schedule_delayed_work(&bdev->wq,
484                                       ((HZ / 100) < 1) ? 1 : HZ / 100);
485 }
486 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
487
488 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
489                                      struct ttm_resource **mem,
490                                      struct ttm_operation_ctx *ctx,
491                                      struct ttm_place *hop)
492 {
493         struct ttm_placement hop_placement;
494         struct ttm_resource *hop_mem;
495         int ret;
496
497         hop_placement.num_placement = hop_placement.num_busy_placement = 1;
498         hop_placement.placement = hop_placement.busy_placement = hop;
499
500         /* find space in the bounce domain */
501         ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
502         if (ret)
503                 return ret;
504         /* move to the bounce domain */
505         ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
506         if (ret) {
507                 ttm_resource_free(bo, &hop_mem);
508                 return ret;
509         }
510         return 0;
511 }
512
513 static int ttm_bo_evict(struct ttm_buffer_object *bo,
514                         struct ttm_operation_ctx *ctx)
515 {
516         struct ttm_device *bdev = bo->bdev;
517         struct ttm_resource *evict_mem;
518         struct ttm_placement placement;
519         struct ttm_place hop;
520         int ret = 0;
521
522         memset(&hop, 0, sizeof(hop));
523
524         dma_resv_assert_held(bo->base.resv);
525
526         placement.num_placement = 0;
527         placement.num_busy_placement = 0;
528         bdev->funcs->evict_flags(bo, &placement);
529
530         if (!placement.num_placement && !placement.num_busy_placement) {
531                 ret = ttm_bo_wait(bo, true, false);
532                 if (ret)
533                         return ret;
534
535                 /*
536                  * Since we've already synced, this frees backing store
537                  * immediately.
538                  */
539                 return ttm_bo_pipeline_gutting(bo);
540         }
541
542         ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
543         if (ret) {
544                 if (ret != -ERESTARTSYS) {
545                         pr_err("Failed to find memory space for buffer 0x%p eviction\n",
546                                bo);
547                         ttm_bo_mem_space_debug(bo, &placement);
548                 }
549                 goto out;
550         }
551
552 bounce:
553         ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
554         if (ret == -EMULTIHOP) {
555                 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
556                 if (ret) {
557                         pr_err("Buffer eviction failed\n");
558                         ttm_resource_free(bo, &evict_mem);
559                         goto out;
560                 }
561                 /* try and move to final place now. */
562                 goto bounce;
563         }
564 out:
565         return ret;
566 }
567
568 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
569                               const struct ttm_place *place)
570 {
571         dma_resv_assert_held(bo->base.resv);
572         if (bo->resource->mem_type == TTM_PL_SYSTEM)
573                 return true;
574
575         /* Don't evict this BO if it's outside of the
576          * requested placement range
577          */
578         if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
579             (place->lpfn && place->lpfn <= bo->resource->start))
580                 return false;
581
582         return true;
583 }
584 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
585
586 /*
587  * Check the target bo is allowable to be evicted or swapout, including cases:
588  *
589  * a. if share same reservation object with ctx->resv, have assumption
590  * reservation objects should already be locked, so not lock again and
591  * return true directly when either the opreation allow_reserved_eviction
592  * or the target bo already is in delayed free list;
593  *
594  * b. Otherwise, trylock it.
595  */
596 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
597                                            struct ttm_operation_ctx *ctx,
598                                            const struct ttm_place *place,
599                                            bool *locked, bool *busy)
600 {
601         bool ret = false;
602
603         if (bo->base.resv == ctx->resv) {
604                 dma_resv_assert_held(bo->base.resv);
605                 if (ctx->allow_res_evict)
606                         ret = true;
607                 *locked = false;
608                 if (busy)
609                         *busy = false;
610         } else {
611                 ret = dma_resv_trylock(bo->base.resv);
612                 *locked = ret;
613                 if (busy)
614                         *busy = !ret;
615         }
616
617         if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
618                 ret = false;
619                 if (*locked) {
620                         dma_resv_unlock(bo->base.resv);
621                         *locked = false;
622                 }
623         }
624
625         return ret;
626 }
627
628 /**
629  * ttm_mem_evict_wait_busy - wait for a busy BO to become available
630  *
631  * @busy_bo: BO which couldn't be locked with trylock
632  * @ctx: operation context
633  * @ticket: acquire ticket
634  *
635  * Try to lock a busy buffer object to avoid failing eviction.
636  */
637 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
638                                    struct ttm_operation_ctx *ctx,
639                                    struct ww_acquire_ctx *ticket)
640 {
641         int r;
642
643         if (!busy_bo || !ticket)
644                 return -EBUSY;
645
646         if (ctx->interruptible)
647                 r = dma_resv_lock_interruptible(busy_bo->base.resv,
648                                                           ticket);
649         else
650                 r = dma_resv_lock(busy_bo->base.resv, ticket);
651
652         /*
653          * TODO: It would be better to keep the BO locked until allocation is at
654          * least tried one more time, but that would mean a much larger rework
655          * of TTM.
656          */
657         if (!r)
658                 dma_resv_unlock(busy_bo->base.resv);
659
660         return r == -EDEADLK ? -EBUSY : r;
661 }
662
663 int ttm_mem_evict_first(struct ttm_device *bdev,
664                         struct ttm_resource_manager *man,
665                         const struct ttm_place *place,
666                         struct ttm_operation_ctx *ctx,
667                         struct ww_acquire_ctx *ticket)
668 {
669         struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
670         bool locked = false;
671         unsigned i;
672         int ret;
673
674         spin_lock(&bdev->lru_lock);
675         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
676                 list_for_each_entry(bo, &man->lru[i], lru) {
677                         bool busy;
678
679                         if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
680                                                             &locked, &busy)) {
681                                 if (busy && !busy_bo && ticket !=
682                                     dma_resv_locking_ctx(bo->base.resv))
683                                         busy_bo = bo;
684                                 continue;
685                         }
686
687                         if (!ttm_bo_get_unless_zero(bo)) {
688                                 if (locked)
689                                         dma_resv_unlock(bo->base.resv);
690                                 continue;
691                         }
692                         break;
693                 }
694
695                 /* If the inner loop terminated early, we have our candidate */
696                 if (&bo->lru != &man->lru[i])
697                         break;
698
699                 bo = NULL;
700         }
701
702         if (!bo) {
703                 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
704                         busy_bo = NULL;
705                 spin_unlock(&bdev->lru_lock);
706                 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
707                 if (busy_bo)
708                         ttm_bo_put(busy_bo);
709                 return ret;
710         }
711
712         if (bo->deleted) {
713                 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
714                                           ctx->no_wait_gpu, locked);
715                 ttm_bo_put(bo);
716                 return ret;
717         }
718
719         spin_unlock(&bdev->lru_lock);
720
721         ret = ttm_bo_evict(bo, ctx);
722         if (locked)
723                 ttm_bo_unreserve(bo);
724
725         ttm_bo_put(bo);
726         return ret;
727 }
728
729 /*
730  * Add the last move fence to the BO and reserve a new shared slot. We only use
731  * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
732  * either stall or use an exclusive fence respectively set bo->moving.
733  */
734 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
735                                  struct ttm_resource_manager *man,
736                                  struct ttm_resource *mem,
737                                  bool no_wait_gpu)
738 {
739         struct dma_fence *fence;
740         int ret;
741
742         spin_lock(&man->move_lock);
743         fence = dma_fence_get(man->move);
744         spin_unlock(&man->move_lock);
745
746         if (!fence)
747                 return 0;
748
749         if (no_wait_gpu) {
750                 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
751                 dma_fence_put(fence);
752                 return ret;
753         }
754
755         dma_resv_add_shared_fence(bo->base.resv, fence);
756
757         ret = dma_resv_reserve_shared(bo->base.resv, 1);
758         if (unlikely(ret)) {
759                 dma_fence_put(fence);
760                 return ret;
761         }
762
763         dma_fence_put(bo->moving);
764         bo->moving = fence;
765         return 0;
766 }
767
768 /*
769  * Repeatedly evict memory from the LRU for @mem_type until we create enough
770  * space, or we've evicted everything and there isn't enough space.
771  */
772 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
773                                   const struct ttm_place *place,
774                                   struct ttm_resource **mem,
775                                   struct ttm_operation_ctx *ctx)
776 {
777         struct ttm_device *bdev = bo->bdev;
778         struct ttm_resource_manager *man;
779         struct ww_acquire_ctx *ticket;
780         int ret;
781
782         man = ttm_manager_type(bdev, place->mem_type);
783         ticket = dma_resv_locking_ctx(bo->base.resv);
784         do {
785                 ret = ttm_resource_alloc(bo, place, mem);
786                 if (likely(!ret))
787                         break;
788                 if (unlikely(ret != -ENOSPC))
789                         return ret;
790                 ret = ttm_mem_evict_first(bdev, man, place, ctx,
791                                           ticket);
792                 if (unlikely(ret != 0))
793                         return ret;
794         } while (1);
795
796         return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
797 }
798
799 /*
800  * Creates space for memory region @mem according to its type.
801  *
802  * This function first searches for free space in compatible memory types in
803  * the priority order defined by the driver.  If free space isn't found, then
804  * ttm_bo_mem_force_space is attempted in priority order to evict and find
805  * space.
806  */
807 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
808                         struct ttm_placement *placement,
809                         struct ttm_resource **mem,
810                         struct ttm_operation_ctx *ctx)
811 {
812         struct ttm_device *bdev = bo->bdev;
813         bool type_found = false;
814         int i, ret;
815
816         ret = dma_resv_reserve_shared(bo->base.resv, 1);
817         if (unlikely(ret))
818                 return ret;
819
820         for (i = 0; i < placement->num_placement; ++i) {
821                 const struct ttm_place *place = &placement->placement[i];
822                 struct ttm_resource_manager *man;
823
824                 man = ttm_manager_type(bdev, place->mem_type);
825                 if (!man || !ttm_resource_manager_used(man))
826                         continue;
827
828                 type_found = true;
829                 ret = ttm_resource_alloc(bo, place, mem);
830                 if (ret == -ENOSPC)
831                         continue;
832                 if (unlikely(ret))
833                         goto error;
834
835                 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
836                 if (unlikely(ret)) {
837                         ttm_resource_free(bo, mem);
838                         if (ret == -EBUSY)
839                                 continue;
840
841                         goto error;
842                 }
843                 return 0;
844         }
845
846         for (i = 0; i < placement->num_busy_placement; ++i) {
847                 const struct ttm_place *place = &placement->busy_placement[i];
848                 struct ttm_resource_manager *man;
849
850                 man = ttm_manager_type(bdev, place->mem_type);
851                 if (!man || !ttm_resource_manager_used(man))
852                         continue;
853
854                 type_found = true;
855                 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
856                 if (likely(!ret))
857                         return 0;
858
859                 if (ret && ret != -EBUSY)
860                         goto error;
861         }
862
863         ret = -ENOMEM;
864         if (!type_found) {
865                 pr_err(TTM_PFX "No compatible memory type found\n");
866                 ret = -EINVAL;
867         }
868
869 error:
870         if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
871                 ttm_bo_move_to_lru_tail_unlocked(bo);
872
873         return ret;
874 }
875 EXPORT_SYMBOL(ttm_bo_mem_space);
876
877 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
878                               struct ttm_placement *placement,
879                               struct ttm_operation_ctx *ctx)
880 {
881         struct ttm_resource *mem;
882         struct ttm_place hop;
883         int ret;
884
885         dma_resv_assert_held(bo->base.resv);
886
887         /*
888          * Determine where to move the buffer.
889          *
890          * If driver determines move is going to need
891          * an extra step then it will return -EMULTIHOP
892          * and the buffer will be moved to the temporary
893          * stop and the driver will be called to make
894          * the second hop.
895          */
896         ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
897         if (ret)
898                 return ret;
899 bounce:
900         ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
901         if (ret == -EMULTIHOP) {
902                 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
903                 if (ret)
904                         goto out;
905                 /* try and move to final place now. */
906                 goto bounce;
907         }
908 out:
909         if (ret)
910                 ttm_resource_free(bo, &mem);
911         return ret;
912 }
913
914 static bool ttm_bo_places_compat(const struct ttm_place *places,
915                                  unsigned num_placement,
916                                  struct ttm_resource *mem,
917                                  uint32_t *new_flags)
918 {
919         unsigned i;
920
921         if (mem->placement & TTM_PL_FLAG_TEMPORARY)
922                 return false;
923
924         for (i = 0; i < num_placement; i++) {
925                 const struct ttm_place *heap = &places[i];
926
927                 if ((mem->start < heap->fpfn ||
928                      (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
929                         continue;
930
931                 *new_flags = heap->flags;
932                 if ((mem->mem_type == heap->mem_type) &&
933                     (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
934                      (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
935                         return true;
936         }
937         return false;
938 }
939
940 bool ttm_bo_mem_compat(struct ttm_placement *placement,
941                        struct ttm_resource *mem,
942                        uint32_t *new_flags)
943 {
944         if (ttm_bo_places_compat(placement->placement, placement->num_placement,
945                                  mem, new_flags))
946                 return true;
947
948         if ((placement->busy_placement != placement->placement ||
949              placement->num_busy_placement > placement->num_placement) &&
950             ttm_bo_places_compat(placement->busy_placement,
951                                  placement->num_busy_placement,
952                                  mem, new_flags))
953                 return true;
954
955         return false;
956 }
957 EXPORT_SYMBOL(ttm_bo_mem_compat);
958
959 int ttm_bo_validate(struct ttm_buffer_object *bo,
960                     struct ttm_placement *placement,
961                     struct ttm_operation_ctx *ctx)
962 {
963         int ret;
964         uint32_t new_flags;
965
966         dma_resv_assert_held(bo->base.resv);
967
968         /*
969          * Remove the backing store if no placement is given.
970          */
971         if (!placement->num_placement && !placement->num_busy_placement)
972                 return ttm_bo_pipeline_gutting(bo);
973
974         /*
975          * Check whether we need to move buffer.
976          */
977         if (!ttm_bo_mem_compat(placement, bo->resource, &new_flags)) {
978                 ret = ttm_bo_move_buffer(bo, placement, ctx);
979                 if (ret)
980                         return ret;
981         }
982         /*
983          * We might need to add a TTM.
984          */
985         if (bo->resource->mem_type == TTM_PL_SYSTEM) {
986                 ret = ttm_tt_create(bo, true);
987                 if (ret)
988                         return ret;
989         }
990         return 0;
991 }
992 EXPORT_SYMBOL(ttm_bo_validate);
993
994 int ttm_bo_init_reserved(struct ttm_device *bdev,
995                          struct ttm_buffer_object *bo,
996                          size_t size,
997                          enum ttm_bo_type type,
998                          struct ttm_placement *placement,
999                          uint32_t page_alignment,
1000                          struct ttm_operation_ctx *ctx,
1001                          struct sg_table *sg,
1002                          struct dma_resv *resv,
1003                          void (*destroy) (struct ttm_buffer_object *))
1004 {
1005         static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
1006         bool locked;
1007         int ret;
1008
1009         bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1010
1011         kref_init(&bo->kref);
1012         INIT_LIST_HEAD(&bo->lru);
1013         INIT_LIST_HEAD(&bo->ddestroy);
1014         bo->bdev = bdev;
1015         bo->type = type;
1016         bo->page_alignment = page_alignment;
1017         bo->moving = NULL;
1018         bo->pin_count = 0;
1019         bo->sg = sg;
1020         if (resv) {
1021                 bo->base.resv = resv;
1022                 dma_resv_assert_held(bo->base.resv);
1023         } else {
1024                 bo->base.resv = &bo->base._resv;
1025         }
1026         atomic_inc(&ttm_glob.bo_count);
1027
1028         ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1029         if (unlikely(ret)) {
1030                 ttm_bo_put(bo);
1031                 return ret;
1032         }
1033
1034         /*
1035          * For ttm_bo_type_device buffers, allocate
1036          * address space from the device.
1037          */
1038         if (bo->type == ttm_bo_type_device ||
1039             bo->type == ttm_bo_type_sg)
1040                 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1041                                          bo->resource->num_pages);
1042
1043         /* passed reservation objects should already be locked,
1044          * since otherwise lockdep will be angered in radeon.
1045          */
1046         if (!resv) {
1047                 locked = dma_resv_trylock(bo->base.resv);
1048                 WARN_ON(!locked);
1049         }
1050
1051         if (likely(!ret))
1052                 ret = ttm_bo_validate(bo, placement, ctx);
1053
1054         if (unlikely(ret)) {
1055                 if (!resv)
1056                         ttm_bo_unreserve(bo);
1057
1058                 ttm_bo_put(bo);
1059                 return ret;
1060         }
1061
1062         ttm_bo_move_to_lru_tail_unlocked(bo);
1063
1064         return ret;
1065 }
1066 EXPORT_SYMBOL(ttm_bo_init_reserved);
1067
1068 int ttm_bo_init(struct ttm_device *bdev,
1069                 struct ttm_buffer_object *bo,
1070                 size_t size,
1071                 enum ttm_bo_type type,
1072                 struct ttm_placement *placement,
1073                 uint32_t page_alignment,
1074                 bool interruptible,
1075                 struct sg_table *sg,
1076                 struct dma_resv *resv,
1077                 void (*destroy) (struct ttm_buffer_object *))
1078 {
1079         struct ttm_operation_ctx ctx = { interruptible, false };
1080         int ret;
1081
1082         ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1083                                    page_alignment, &ctx, sg, resv, destroy);
1084         if (ret)
1085                 return ret;
1086
1087         if (!resv)
1088                 ttm_bo_unreserve(bo);
1089
1090         return 0;
1091 }
1092 EXPORT_SYMBOL(ttm_bo_init);
1093
1094 /*
1095  * buffer object vm functions.
1096  */
1097
1098 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1099 {
1100         struct ttm_device *bdev = bo->bdev;
1101
1102         drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1103         ttm_mem_io_free(bdev, bo->resource);
1104 }
1105 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1106
1107 int ttm_bo_wait(struct ttm_buffer_object *bo,
1108                 bool interruptible, bool no_wait)
1109 {
1110         long timeout = 15 * HZ;
1111
1112         if (no_wait) {
1113                 if (dma_resv_test_signaled(bo->base.resv, true))
1114                         return 0;
1115                 else
1116                         return -EBUSY;
1117         }
1118
1119         timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1120                                         timeout);
1121         if (timeout < 0)
1122                 return timeout;
1123
1124         if (timeout == 0)
1125                 return -EBUSY;
1126
1127         dma_resv_add_excl_fence(bo->base.resv, NULL);
1128         return 0;
1129 }
1130 EXPORT_SYMBOL(ttm_bo_wait);
1131
1132 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1133                    gfp_t gfp_flags)
1134 {
1135         struct ttm_place place;
1136         bool locked;
1137         int ret;
1138
1139         /*
1140          * While the bo may already reside in SYSTEM placement, set
1141          * SYSTEM as new placement to cover also the move further below.
1142          * The driver may use the fact that we're moving from SYSTEM
1143          * as an indication that we're about to swap out.
1144          */
1145         memset(&place, 0, sizeof(place));
1146         place.mem_type = TTM_PL_SYSTEM;
1147         if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1148                 return -EBUSY;
1149
1150         if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1151             bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1152             bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1153             !ttm_bo_get_unless_zero(bo)) {
1154                 if (locked)
1155                         dma_resv_unlock(bo->base.resv);
1156                 return -EBUSY;
1157         }
1158
1159         if (bo->deleted) {
1160                 ttm_bo_cleanup_refs(bo, false, false, locked);
1161                 ttm_bo_put(bo);
1162                 return 0;
1163         }
1164
1165         ttm_bo_del_from_lru(bo);
1166         /* TODO: Cleanup the locking */
1167         spin_unlock(&bo->bdev->lru_lock);
1168
1169         /*
1170          * Move to system cached
1171          */
1172         if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1173                 struct ttm_operation_ctx ctx = { false, false };
1174                 struct ttm_resource *evict_mem;
1175                 struct ttm_place hop;
1176
1177                 memset(&hop, 0, sizeof(hop));
1178                 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1179                 if (unlikely(ret))
1180                         goto out;
1181
1182                 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1183                 if (unlikely(ret != 0)) {
1184                         WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1185                         goto out;
1186                 }
1187         }
1188
1189         /*
1190          * Make sure BO is idle.
1191          */
1192         ret = ttm_bo_wait(bo, false, false);
1193         if (unlikely(ret != 0))
1194                 goto out;
1195
1196         ttm_bo_unmap_virtual(bo);
1197
1198         /*
1199          * Swap out. Buffer will be swapped in again as soon as
1200          * anyone tries to access a ttm page.
1201          */
1202         if (bo->bdev->funcs->swap_notify)
1203                 bo->bdev->funcs->swap_notify(bo);
1204
1205         if (ttm_tt_is_populated(bo->ttm))
1206                 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1207 out:
1208
1209         /*
1210          * Unreserve without putting on LRU to avoid swapping out an
1211          * already swapped buffer.
1212          */
1213         if (locked)
1214                 dma_resv_unlock(bo->base.resv);
1215         ttm_bo_put(bo);
1216         return ret;
1217 }
1218
1219 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1220 {
1221         if (bo->ttm == NULL)
1222                 return;
1223
1224         ttm_tt_destroy(bo->bdev, bo->ttm);
1225         bo->ttm = NULL;
1226 }