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