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