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