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