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