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