drm/ttm: flip the switch, and convert to dma_fence
[deliverable/linux.git] / drivers / gpu / drm / ttm / ttm_bo.c
CommitLineData
ba4e7d97
TH
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
25d0479a
JP
31#define pr_fmt(fmt) "[TTM] " fmt
32
760285e7
DH
33#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
ba4e7d97
TH
36#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
60063497 42#include <linux/atomic.h>
f2c24b83 43#include <linux/reservation.h>
ba4e7d97
TH
44
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
ba4e7d97 49static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
a987fcaa
TH
50static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
f1217ed0
CK
57static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
fb53f862
JG
59{
60 int i;
61
62 for (i = 0; i <= TTM_PL_PRIV5; i++)
f1217ed0 63 if (place->flags & (1 << i)) {
fb53f862
JG
64 *mem_type = i;
65 return 0;
66 }
67 return -EINVAL;
68}
69
5012f506 70static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
fb53f862 71{
5012f506
JG
72 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
25d0479a
JP
74 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
77 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
78 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
d961db75
BS
81 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
fb53f862
JG
83}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
fb53f862
JG
88 int i, ret, mem_type;
89
25d0479a
JP
90 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
fb53f862 93 for (i = 0; i < placement->num_placement; i++) {
f1217ed0 94 ret = ttm_mem_type_from_place(&placement->placement[i],
fb53f862
JG
95 &mem_type);
96 if (ret)
97 return;
25d0479a 98 pr_err(" placement[%d]=0x%08X (%d)\n",
f1217ed0 99 i, placement->placement[i].flags, mem_type);
5012f506 100 ttm_mem_type_debug(bo->bdev, mem_type);
fb53f862
JG
101 }
102}
103
a987fcaa
TH
104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
52cf25d0 120static const struct sysfs_ops ttm_bo_global_ops = {
a987fcaa
TH
121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
ba4e7d97
TH
130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
57de4ba9 141 size_t acc_size = bo->acc_size;
ba4e7d97
TH
142
143 BUG_ON(atomic_read(&bo->list_kref.refcount));
144 BUG_ON(atomic_read(&bo->kref.refcount));
145 BUG_ON(atomic_read(&bo->cpu_writers));
ba4e7d97
TH
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
a987fcaa 152 atomic_dec(&bo->glob->bo_count);
5e338405
ML
153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
c58f009e 155 mutex_destroy(&bo->wu_mutex);
ba4e7d97
TH
156 if (bo->destroy)
157 bo->destroy(bo);
158 else {
ba4e7d97
TH
159 kfree(bo);
160 }
57de4ba9 161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
ba4e7d97
TH
162}
163
d6ea8886 164void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
ba4e7d97
TH
165{
166 struct ttm_bo_device *bdev = bo->bdev;
167 struct ttm_mem_type_manager *man;
168
009a9dad 169 lockdep_assert_held(&bo->resv->lock.base);
ba4e7d97
TH
170
171 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
172
173 BUG_ON(!list_empty(&bo->lru));
174
175 man = &bdev->man[bo->mem.mem_type];
176 list_add_tail(&bo->lru, &man->lru);
177 kref_get(&bo->list_kref);
178
179 if (bo->ttm != NULL) {
a987fcaa 180 list_add_tail(&bo->swap, &bo->glob->swap_lru);
ba4e7d97
TH
181 kref_get(&bo->list_kref);
182 }
183 }
184}
34820324 185EXPORT_SYMBOL(ttm_bo_add_to_lru);
ba4e7d97 186
d6ea8886 187int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
ba4e7d97
TH
188{
189 int put_count = 0;
190
191 if (!list_empty(&bo->swap)) {
192 list_del_init(&bo->swap);
193 ++put_count;
194 }
195 if (!list_empty(&bo->lru)) {
196 list_del_init(&bo->lru);
197 ++put_count;
198 }
199
200 /*
201 * TODO: Add a driver hook to delete from
202 * driver-specific LRU's here.
203 */
204
205 return put_count;
206}
207
ba4e7d97
TH
208static void ttm_bo_ref_bug(struct kref *list_kref)
209{
210 BUG();
211}
212
d6ea8886
DA
213void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
214 bool never_free)
215{
2357cbe5
TH
216 kref_sub(&bo->list_kref, count,
217 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
d6ea8886
DA
218}
219
34820324 220void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
5e45d7df 221{
34820324 222 int put_count;
ecff665f 223
34820324
ML
224 spin_lock(&bo->glob->lru_lock);
225 put_count = ttm_bo_del_from_lru(bo);
226 spin_unlock(&bo->glob->lru_lock);
227 ttm_bo_list_ref_sub(bo, put_count, true);
ecff665f 228}
34820324 229EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
ecff665f 230
ba4e7d97
TH
231/*
232 * Call bo->mutex locked.
233 */
ba4e7d97
TH
234static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
235{
236 struct ttm_bo_device *bdev = bo->bdev;
a987fcaa 237 struct ttm_bo_global *glob = bo->glob;
ba4e7d97
TH
238 int ret = 0;
239 uint32_t page_flags = 0;
240
241 TTM_ASSERT_LOCKED(&bo->mutex);
242 bo->ttm = NULL;
243
ad49f501
DA
244 if (bdev->need_dma32)
245 page_flags |= TTM_PAGE_FLAG_DMA32;
246
ba4e7d97
TH
247 switch (bo->type) {
248 case ttm_bo_type_device:
249 if (zero_alloc)
250 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
251 case ttm_bo_type_kernel:
649bf3ca
JG
252 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
253 page_flags, glob->dummy_read_page);
ba4e7d97
TH
254 if (unlikely(bo->ttm == NULL))
255 ret = -ENOMEM;
256 break;
129b78bf
DA
257 case ttm_bo_type_sg:
258 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
259 page_flags | TTM_PAGE_FLAG_SG,
260 glob->dummy_read_page);
261 if (unlikely(bo->ttm == NULL)) {
262 ret = -ENOMEM;
263 break;
264 }
265 bo->ttm->sg = bo->sg;
266 break;
ba4e7d97 267 default:
25d0479a 268 pr_err("Illegal buffer object type\n");
ba4e7d97
TH
269 ret = -EINVAL;
270 break;
271 }
272
273 return ret;
274}
275
276static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
277 struct ttm_mem_reg *mem,
9d87fa21 278 bool evict, bool interruptible,
97a875cb 279 bool no_wait_gpu)
ba4e7d97
TH
280{
281 struct ttm_bo_device *bdev = bo->bdev;
282 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
283 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
284 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
285 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
286 int ret = 0;
287
288 if (old_is_pci || new_is_pci ||
eba67093
TH
289 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
290 ret = ttm_mem_io_lock(old_man, true);
291 if (unlikely(ret != 0))
292 goto out_err;
293 ttm_bo_unmap_virtual_locked(bo);
294 ttm_mem_io_unlock(old_man);
295 }
ba4e7d97
TH
296
297 /*
298 * Create and bind a ttm if required.
299 */
300
8d3bb236
BS
301 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
302 if (bo->ttm == NULL) {
ff02b13f
BS
303 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
304 ret = ttm_bo_add_ttm(bo, zero);
8d3bb236
BS
305 if (ret)
306 goto out_err;
307 }
ba4e7d97
TH
308
309 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
310 if (ret)
87ef9209 311 goto out_err;
ba4e7d97
TH
312
313 if (mem->mem_type != TTM_PL_SYSTEM) {
314 ret = ttm_tt_bind(bo->ttm, mem);
315 if (ret)
316 goto out_err;
317 }
318
319 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
82ef594e
BS
320 if (bdev->driver->move_notify)
321 bdev->driver->move_notify(bo, mem);
ca262a99 322 bo->mem = *mem;
ba4e7d97 323 mem->mm_node = NULL;
ba4e7d97
TH
324 goto moved;
325 }
ba4e7d97
TH
326 }
327
9f1feed2
BS
328 if (bdev->driver->move_notify)
329 bdev->driver->move_notify(bo, mem);
330
ba4e7d97
TH
331 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
332 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
97a875cb 333 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
ba4e7d97
TH
334 else if (bdev->driver->move)
335 ret = bdev->driver->move(bo, evict, interruptible,
97a875cb 336 no_wait_gpu, mem);
ba4e7d97 337 else
97a875cb 338 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
ba4e7d97 339
9f1feed2
BS
340 if (ret) {
341 if (bdev->driver->move_notify) {
342 struct ttm_mem_reg tmp_mem = *mem;
343 *mem = bo->mem;
344 bo->mem = tmp_mem;
345 bdev->driver->move_notify(bo, mem);
346 bo->mem = *mem;
014b3440 347 *mem = tmp_mem;
9f1feed2 348 }
ba4e7d97 349
9f1feed2
BS
350 goto out_err;
351 }
dc97b340 352
ba4e7d97
TH
353moved:
354 if (bo->evicted) {
9ef7506f
RC
355 if (bdev->driver->invalidate_caches) {
356 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
357 if (ret)
358 pr_err("Can not flush read caches\n");
359 }
ba4e7d97
TH
360 bo->evicted = false;
361 }
362
363 if (bo->mem.mm_node) {
d961db75 364 bo->offset = (bo->mem.start << PAGE_SHIFT) +
ba4e7d97
TH
365 bdev->man[bo->mem.mem_type].gpu_offset;
366 bo->cur_placement = bo->mem.placement;
354fb52c
TH
367 } else
368 bo->offset = 0;
ba4e7d97
TH
369
370 return 0;
371
372out_err:
373 new_man = &bdev->man[bo->mem.mem_type];
374 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
375 ttm_tt_unbind(bo->ttm);
376 ttm_tt_destroy(bo->ttm);
377 bo->ttm = NULL;
378 }
379
380 return ret;
381}
382
1df6a2eb 383/**
40d857bb 384 * Call bo::reserved.
1df6a2eb 385 * Will release GPU memory type usage on destruction.
40d857bb
TH
386 * This is the place to put in driver specific hooks to release
387 * driver private resources.
388 * Will release the bo::reserved lock.
1df6a2eb
TH
389 */
390
391static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
392{
dc97b340
JG
393 if (bo->bdev->driver->move_notify)
394 bo->bdev->driver->move_notify(bo, NULL);
395
1df6a2eb 396 if (bo->ttm) {
1df6a2eb
TH
397 ttm_tt_unbind(bo->ttm);
398 ttm_tt_destroy(bo->ttm);
399 bo->ttm = NULL;
1df6a2eb 400 }
40d857bb 401 ttm_bo_mem_put(bo, &bo->mem);
1df6a2eb 402
5e338405 403 ww_mutex_unlock (&bo->resv->lock);
1df6a2eb
TH
404}
405
f2c24b83
ML
406static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
407{
408 struct reservation_object_list *fobj;
409 struct fence *fence;
410 int i;
411
412 fobj = reservation_object_get_list(bo->resv);
413 fence = reservation_object_get_excl(bo->resv);
414 if (fence && !fence->ops->signaled)
415 fence_enable_sw_signaling(fence);
416
417 for (i = 0; fobj && i < fobj->shared_count; ++i) {
418 fence = rcu_dereference_protected(fobj->shared[i],
419 reservation_object_held(bo->resv));
420
421 if (!fence->ops->signaled)
422 fence_enable_sw_signaling(fence);
423 }
424}
425
e1efc9b6 426static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
ba4e7d97
TH
427{
428 struct ttm_bo_device *bdev = bo->bdev;
a987fcaa 429 struct ttm_bo_global *glob = bo->glob;
e1efc9b6 430 int put_count;
ba4e7d97
TH
431 int ret;
432
4154f051 433 spin_lock(&glob->lru_lock);
0eff2a24 434 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
4154f051 435
dd7cfd64 436 if (!ret) {
f2c24b83 437 if (!ttm_bo_wait(bo, false, false, true)) {
dd7cfd64 438 put_count = ttm_bo_del_from_lru(bo);
ba4e7d97 439
dd7cfd64
ML
440 spin_unlock(&glob->lru_lock);
441 ttm_bo_cleanup_memtype_use(bo);
ba4e7d97 442
dd7cfd64 443 ttm_bo_list_ref_sub(bo, put_count, true);
4154f051 444
dd7cfd64 445 return;
f2c24b83
ML
446 } else
447 ttm_bo_flush_all_fences(bo);
15205fbc
TH
448
449 /*
450 * Make NO_EVICT bos immediately available to
451 * shrinkers, now that they are queued for
452 * destruction.
453 */
454 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
455 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
456 ttm_bo_add_to_lru(bo);
457 }
458
c7523083 459 __ttm_bo_unreserve(bo);
15205fbc 460 }
e1efc9b6
TH
461
462 kref_get(&bo->list_kref);
463 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
464 spin_unlock(&glob->lru_lock);
e1efc9b6 465
e1efc9b6
TH
466 schedule_delayed_work(&bdev->wq,
467 ((HZ / 100) < 1) ? 1 : HZ / 100);
468}
469
f2c24b83
ML
470static int ttm_bo_unreserve_and_wait(struct ttm_buffer_object *bo,
471 bool interruptible)
472{
473 struct ttm_bo_global *glob = bo->glob;
474 struct reservation_object_list *fobj;
475 struct fence *excl = NULL;
476 struct fence **shared = NULL;
477 u32 shared_count = 0, i;
478 int ret = 0;
479
480 fobj = reservation_object_get_list(bo->resv);
481 if (fobj && fobj->shared_count) {
482 shared = kmalloc(sizeof(*shared) * fobj->shared_count,
483 GFP_KERNEL);
484
485 if (!shared) {
486 ret = -ENOMEM;
487 __ttm_bo_unreserve(bo);
488 spin_unlock(&glob->lru_lock);
489 return ret;
490 }
491
492 for (i = 0; i < fobj->shared_count; ++i) {
493 if (!fence_is_signaled(fobj->shared[i])) {
494 fence_get(fobj->shared[i]);
495 shared[shared_count++] = fobj->shared[i];
496 }
497 }
498 if (!shared_count) {
499 kfree(shared);
500 shared = NULL;
501 }
502 }
503
504 excl = reservation_object_get_excl(bo->resv);
505 if (excl && !fence_is_signaled(excl))
506 fence_get(excl);
507 else
508 excl = NULL;
509
510 __ttm_bo_unreserve(bo);
511 spin_unlock(&glob->lru_lock);
512
513 if (excl) {
514 ret = fence_wait(excl, interruptible);
515 fence_put(excl);
516 }
517
518 if (shared_count > 0) {
519 for (i = 0; i < shared_count; ++i) {
520 if (!ret)
521 ret = fence_wait(shared[i], interruptible);
522 fence_put(shared[i]);
523 }
524 kfree(shared);
525 }
526
527 return ret;
528}
529
e1efc9b6 530/**
85b144f8 531 * function ttm_bo_cleanup_refs_and_unlock
e1efc9b6
TH
532 * If bo idle, remove from delayed- and lru lists, and unref.
533 * If not idle, do nothing.
534 *
85b144f8
ML
535 * Must be called with lru_lock and reservation held, this function
536 * will drop both before returning.
537 *
e1efc9b6 538 * @interruptible Any sleeps should occur interruptibly.
e1efc9b6
TH
539 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
540 */
541
85b144f8
ML
542static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
543 bool interruptible,
544 bool no_wait_gpu)
e1efc9b6
TH
545{
546 struct ttm_bo_global *glob = bo->glob;
547 int put_count;
85b144f8 548 int ret;
e1efc9b6 549
85b144f8 550 ret = ttm_bo_wait(bo, false, false, true);
e1efc9b6 551
85b144f8 552 if (ret && !no_wait_gpu) {
f2c24b83 553 ret = ttm_bo_unreserve_and_wait(bo, interruptible);
85b144f8 554 if (ret)
b8e902f2
TH
555 return ret;
556
85b144f8 557 spin_lock(&glob->lru_lock);
0eff2a24 558 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
b8e902f2 559
85b144f8
ML
560 /*
561 * We raced, and lost, someone else holds the reservation now,
562 * and is probably busy in ttm_bo_cleanup_memtype_use.
563 *
564 * Even if it's not the case, because we finished waiting any
565 * delayed destruction would succeed, so just return success
566 * here.
567 */
568 if (ret) {
569 spin_unlock(&glob->lru_lock);
570 return 0;
571 }
7040138f
ML
572
573 /*
574 * remove sync_obj with ttm_bo_wait, the wait should be
575 * finished, and no new wait object should have been added.
576 */
7040138f
ML
577 ret = ttm_bo_wait(bo, false, false, true);
578 WARN_ON(ret);
579 }
ba4e7d97 580
85b144f8 581 if (ret || unlikely(list_empty(&bo->ddestroy))) {
c7523083 582 __ttm_bo_unreserve(bo);
a987fcaa 583 spin_unlock(&glob->lru_lock);
85b144f8 584 return ret;
ba4e7d97
TH
585 }
586
e1efc9b6
TH
587 put_count = ttm_bo_del_from_lru(bo);
588 list_del_init(&bo->ddestroy);
589 ++put_count;
590
591 spin_unlock(&glob->lru_lock);
592 ttm_bo_cleanup_memtype_use(bo);
593
d6ea8886 594 ttm_bo_list_ref_sub(bo, put_count, true);
e1efc9b6
TH
595
596 return 0;
ba4e7d97
TH
597}
598
599/**
600 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
601 * encountered buffers.
602 */
603
604static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
605{
a987fcaa 606 struct ttm_bo_global *glob = bdev->glob;
1a961ce0
LB
607 struct ttm_buffer_object *entry = NULL;
608 int ret = 0;
ba4e7d97 609
a987fcaa 610 spin_lock(&glob->lru_lock);
1a961ce0
LB
611 if (list_empty(&bdev->ddestroy))
612 goto out_unlock;
613
614 entry = list_first_entry(&bdev->ddestroy,
615 struct ttm_buffer_object, ddestroy);
616 kref_get(&entry->list_kref);
617
618 for (;;) {
619 struct ttm_buffer_object *nentry = NULL;
620
621 if (entry->ddestroy.next != &bdev->ddestroy) {
622 nentry = list_first_entry(&entry->ddestroy,
623 struct ttm_buffer_object, ddestroy);
ba4e7d97
TH
624 kref_get(&nentry->list_kref);
625 }
ba4e7d97 626
0eff2a24 627 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
63d0a419
ML
628 if (remove_all && ret) {
629 spin_unlock(&glob->lru_lock);
c7523083 630 ret = __ttm_bo_reserve(entry, false, false,
0eff2a24 631 false, NULL);
63d0a419
ML
632 spin_lock(&glob->lru_lock);
633 }
634
85b144f8
ML
635 if (!ret)
636 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
637 !remove_all);
638 else
639 spin_unlock(&glob->lru_lock);
640
ba4e7d97 641 kref_put(&entry->list_kref, ttm_bo_release_list);
1a961ce0
LB
642 entry = nentry;
643
644 if (ret || !entry)
645 goto out;
ba4e7d97 646
a987fcaa 647 spin_lock(&glob->lru_lock);
1a961ce0 648 if (list_empty(&entry->ddestroy))
ba4e7d97
TH
649 break;
650 }
ba4e7d97 651
1a961ce0
LB
652out_unlock:
653 spin_unlock(&glob->lru_lock);
654out:
655 if (entry)
656 kref_put(&entry->list_kref, ttm_bo_release_list);
ba4e7d97
TH
657 return ret;
658}
659
660static void ttm_bo_delayed_workqueue(struct work_struct *work)
661{
662 struct ttm_bo_device *bdev =
663 container_of(work, struct ttm_bo_device, wq.work);
664
665 if (ttm_bo_delayed_delete(bdev, false)) {
666 schedule_delayed_work(&bdev->wq,
667 ((HZ / 100) < 1) ? 1 : HZ / 100);
668 }
669}
670
671static void ttm_bo_release(struct kref *kref)
672{
673 struct ttm_buffer_object *bo =
674 container_of(kref, struct ttm_buffer_object, kref);
675 struct ttm_bo_device *bdev = bo->bdev;
eba67093 676 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
ba4e7d97 677
72525b3f 678 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
eba67093
TH
679 ttm_mem_io_lock(man, false);
680 ttm_mem_io_free_vm(bo);
681 ttm_mem_io_unlock(man);
e1efc9b6 682 ttm_bo_cleanup_refs_or_queue(bo);
ba4e7d97 683 kref_put(&bo->list_kref, ttm_bo_release_list);
ba4e7d97
TH
684}
685
686void ttm_bo_unref(struct ttm_buffer_object **p_bo)
687{
688 struct ttm_buffer_object *bo = *p_bo;
ba4e7d97
TH
689
690 *p_bo = NULL;
ba4e7d97 691 kref_put(&bo->kref, ttm_bo_release);
ba4e7d97
TH
692}
693EXPORT_SYMBOL(ttm_bo_unref);
694
7c5ee536
MG
695int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
696{
697 return cancel_delayed_work_sync(&bdev->wq);
698}
699EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
700
701void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
702{
703 if (resched)
704 schedule_delayed_work(&bdev->wq,
705 ((HZ / 100) < 1) ? 1 : HZ / 100);
706}
707EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
708
ca262a99 709static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
97a875cb 710 bool no_wait_gpu)
ba4e7d97 711{
ba4e7d97
TH
712 struct ttm_bo_device *bdev = bo->bdev;
713 struct ttm_mem_reg evict_mem;
ca262a99
JG
714 struct ttm_placement placement;
715 int ret = 0;
ba4e7d97 716
1717c0e2 717 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
ba4e7d97 718
78ecf091 719 if (unlikely(ret != 0)) {
98ffc415 720 if (ret != -ERESTARTSYS) {
25d0479a 721 pr_err("Failed to expire sync object before buffer eviction\n");
78ecf091 722 }
ba4e7d97
TH
723 goto out;
724 }
725
009a9dad 726 lockdep_assert_held(&bo->resv->lock.base);
ba4e7d97
TH
727
728 evict_mem = bo->mem;
729 evict_mem.mm_node = NULL;
eba67093
TH
730 evict_mem.bus.io_reserved_vm = false;
731 evict_mem.bus.io_reserved_count = 0;
ba4e7d97 732
7cb7d1d7
JG
733 placement.num_placement = 0;
734 placement.num_busy_placement = 0;
ca262a99
JG
735 bdev->driver->evict_flags(bo, &placement);
736 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
97a875cb 737 no_wait_gpu);
ba4e7d97 738 if (ret) {
fb53f862 739 if (ret != -ERESTARTSYS) {
25d0479a
JP
740 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
741 bo);
fb53f862
JG
742 ttm_bo_mem_space_debug(bo, &placement);
743 }
ba4e7d97
TH
744 goto out;
745 }
746
747 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
97a875cb 748 no_wait_gpu);
ba4e7d97 749 if (ret) {
98ffc415 750 if (ret != -ERESTARTSYS)
25d0479a 751 pr_err("Buffer eviction failed\n");
42311ff9 752 ttm_bo_mem_put(bo, &evict_mem);
ba4e7d97
TH
753 goto out;
754 }
ca262a99
JG
755 bo->evicted = true;
756out:
757 return ret;
758}
759
760static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
761 uint32_t mem_type,
97a875cb 762 bool interruptible,
9d87fa21 763 bool no_wait_gpu)
ca262a99
JG
764{
765 struct ttm_bo_global *glob = bdev->glob;
766 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
767 struct ttm_buffer_object *bo;
e7ab2019 768 int ret = -EBUSY, put_count;
ba4e7d97 769
a987fcaa 770 spin_lock(&glob->lru_lock);
e7ab2019 771 list_for_each_entry(bo, &man->lru, lru) {
0eff2a24 772 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
e7ab2019
ML
773 if (!ret)
774 break;
775 }
776
777 if (ret) {
9c51ba1d 778 spin_unlock(&glob->lru_lock);
e7ab2019 779 return ret;
9c51ba1d
TH
780 }
781
ca262a99 782 kref_get(&bo->list_kref);
9c51ba1d 783
e1efc9b6 784 if (!list_empty(&bo->ddestroy)) {
e7ab2019
ML
785 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
786 no_wait_gpu);
e1efc9b6 787 kref_put(&bo->list_kref, ttm_bo_release_list);
b8e902f2 788 return ret;
e1efc9b6
TH
789 }
790
9c51ba1d 791 put_count = ttm_bo_del_from_lru(bo);
a987fcaa 792 spin_unlock(&glob->lru_lock);
9c51ba1d
TH
793
794 BUG_ON(ret != 0);
795
d6ea8886 796 ttm_bo_list_ref_sub(bo, put_count, true);
9c51ba1d 797
97a875cb 798 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
ca262a99 799 ttm_bo_unreserve(bo);
9c51ba1d 800
ca262a99 801 kref_put(&bo->list_kref, ttm_bo_release_list);
ba4e7d97
TH
802 return ret;
803}
804
42311ff9
BS
805void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
806{
d961db75 807 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
42311ff9 808
d961db75
BS
809 if (mem->mm_node)
810 (*man->func->put_node)(man, mem);
42311ff9
BS
811}
812EXPORT_SYMBOL(ttm_bo_mem_put);
813
ba4e7d97
TH
814/**
815 * Repeatedly evict memory from the LRU for @mem_type until we create enough
816 * space, or we've evicted everything and there isn't enough space.
817 */
ca262a99
JG
818static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
819 uint32_t mem_type,
f1217ed0 820 const struct ttm_place *place,
ca262a99 821 struct ttm_mem_reg *mem,
9d87fa21 822 bool interruptible,
9d87fa21 823 bool no_wait_gpu)
ba4e7d97 824{
ca262a99 825 struct ttm_bo_device *bdev = bo->bdev;
ba4e7d97 826 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
ba4e7d97
TH
827 int ret;
828
ba4e7d97 829 do {
f1217ed0 830 ret = (*man->func->get_node)(man, bo, place, mem);
ca262a99
JG
831 if (unlikely(ret != 0))
832 return ret;
d961db75 833 if (mem->mm_node)
ba4e7d97 834 break;
97a875cb
ML
835 ret = ttm_mem_evict_first(bdev, mem_type,
836 interruptible, no_wait_gpu);
ba4e7d97
TH
837 if (unlikely(ret != 0))
838 return ret;
ba4e7d97 839 } while (1);
d961db75 840 if (mem->mm_node == NULL)
ba4e7d97 841 return -ENOMEM;
ba4e7d97
TH
842 mem->mem_type = mem_type;
843 return 0;
844}
845
ae3e8122
TH
846static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
847 uint32_t cur_placement,
848 uint32_t proposed_placement)
849{
850 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
851 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
852
853 /**
854 * Keep current caching if possible.
855 */
856
857 if ((cur_placement & caching) != 0)
858 result |= (cur_placement & caching);
859 else if ((man->default_caching & caching) != 0)
860 result |= man->default_caching;
861 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
862 result |= TTM_PL_FLAG_CACHED;
863 else if ((TTM_PL_FLAG_WC & caching) != 0)
864 result |= TTM_PL_FLAG_WC;
865 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
866 result |= TTM_PL_FLAG_UNCACHED;
867
868 return result;
869}
870
ba4e7d97 871static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
ba4e7d97 872 uint32_t mem_type,
f1217ed0 873 const struct ttm_place *place,
ae3e8122 874 uint32_t *masked_placement)
ba4e7d97
TH
875{
876 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
877
f1217ed0 878 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
ba4e7d97
TH
879 return false;
880
f1217ed0 881 if ((place->flags & man->available_caching) == 0)
ba4e7d97 882 return false;
ba4e7d97 883
f1217ed0 884 cur_flags |= (place->flags & man->available_caching);
ae3e8122
TH
885
886 *masked_placement = cur_flags;
ba4e7d97
TH
887 return true;
888}
889
890/**
891 * Creates space for memory region @mem according to its type.
892 *
893 * This function first searches for free space in compatible memory types in
894 * the priority order defined by the driver. If free space isn't found, then
895 * ttm_bo_mem_force_space is attempted in priority order to evict and find
896 * space.
897 */
898int ttm_bo_mem_space(struct ttm_buffer_object *bo,
ca262a99
JG
899 struct ttm_placement *placement,
900 struct ttm_mem_reg *mem,
97a875cb 901 bool interruptible,
9d87fa21 902 bool no_wait_gpu)
ba4e7d97
TH
903{
904 struct ttm_bo_device *bdev = bo->bdev;
905 struct ttm_mem_type_manager *man;
ba4e7d97
TH
906 uint32_t mem_type = TTM_PL_SYSTEM;
907 uint32_t cur_flags = 0;
908 bool type_found = false;
909 bool type_ok = false;
98ffc415 910 bool has_erestartsys = false;
ca262a99 911 int i, ret;
ba4e7d97
TH
912
913 mem->mm_node = NULL;
b6637526 914 for (i = 0; i < placement->num_placement; ++i) {
f1217ed0
CK
915 const struct ttm_place *place = &placement->placement[i];
916
917 ret = ttm_mem_type_from_place(place, &mem_type);
ca262a99
JG
918 if (ret)
919 return ret;
ba4e7d97
TH
920 man = &bdev->man[mem_type];
921
f1217ed0 922 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
ca262a99 923 &cur_flags);
ba4e7d97
TH
924
925 if (!type_ok)
926 continue;
927
ae3e8122
TH
928 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
929 cur_flags);
ca262a99
JG
930 /*
931 * Use the access and other non-mapping-related flag bits from
932 * the memory placement flags to the current flags
933 */
f1217ed0 934 ttm_flag_masked(&cur_flags, place->flags,
ca262a99 935 ~TTM_PL_MASK_MEMTYPE);
ae3e8122 936
ba4e7d97
TH
937 if (mem_type == TTM_PL_SYSTEM)
938 break;
939
940 if (man->has_type && man->use_type) {
941 type_found = true;
f1217ed0 942 ret = (*man->func->get_node)(man, bo, place, mem);
ca262a99
JG
943 if (unlikely(ret))
944 return ret;
ba4e7d97 945 }
d961db75 946 if (mem->mm_node)
ba4e7d97
TH
947 break;
948 }
949
d961db75 950 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
ba4e7d97
TH
951 mem->mem_type = mem_type;
952 mem->placement = cur_flags;
953 return 0;
954 }
955
956 if (!type_found)
957 return -EINVAL;
958
b6637526 959 for (i = 0; i < placement->num_busy_placement; ++i) {
f1217ed0
CK
960 const struct ttm_place *place = &placement->busy_placement[i];
961
962 ret = ttm_mem_type_from_place(place, &mem_type);
ca262a99
JG
963 if (ret)
964 return ret;
ba4e7d97 965 man = &bdev->man[mem_type];
ba4e7d97
TH
966 if (!man->has_type)
967 continue;
f1217ed0 968 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
ba4e7d97
TH
969 continue;
970
ae3e8122
TH
971 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
972 cur_flags);
ca262a99
JG
973 /*
974 * Use the access and other non-mapping-related flag bits from
975 * the memory placement flags to the current flags
976 */
f1217ed0 977 ttm_flag_masked(&cur_flags, place->flags,
ca262a99 978 ~TTM_PL_MASK_MEMTYPE);
ae3e8122 979
0eaddb28
TH
980 if (mem_type == TTM_PL_SYSTEM) {
981 mem->mem_type = mem_type;
982 mem->placement = cur_flags;
983 mem->mm_node = NULL;
984 return 0;
985 }
986
f1217ed0 987 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
97a875cb 988 interruptible, no_wait_gpu);
ba4e7d97
TH
989 if (ret == 0 && mem->mm_node) {
990 mem->placement = cur_flags;
991 return 0;
992 }
98ffc415
TH
993 if (ret == -ERESTARTSYS)
994 has_erestartsys = true;
ba4e7d97 995 }
98ffc415 996 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
ba4e7d97
TH
997 return ret;
998}
999EXPORT_SYMBOL(ttm_bo_mem_space);
1000
6e87fa48 1001static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
ca262a99 1002 struct ttm_placement *placement,
97a875cb 1003 bool interruptible,
9d87fa21 1004 bool no_wait_gpu)
ba4e7d97 1005{
ba4e7d97
TH
1006 int ret = 0;
1007 struct ttm_mem_reg mem;
1008
009a9dad 1009 lockdep_assert_held(&bo->resv->lock.base);
ba4e7d97
TH
1010
1011 /*
1012 * FIXME: It's possible to pipeline buffer moves.
1013 * Have the driver move function wait for idle when necessary,
1014 * instead of doing it here.
1015 */
1717c0e2 1016 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
ba4e7d97
TH
1017 if (ret)
1018 return ret;
ba4e7d97
TH
1019 mem.num_pages = bo->num_pages;
1020 mem.size = mem.num_pages << PAGE_SHIFT;
1021 mem.page_alignment = bo->mem.page_alignment;
eba67093
TH
1022 mem.bus.io_reserved_vm = false;
1023 mem.bus.io_reserved_count = 0;
ba4e7d97
TH
1024 /*
1025 * Determine where to move the buffer.
1026 */
97a875cb
ML
1027 ret = ttm_bo_mem_space(bo, placement, &mem,
1028 interruptible, no_wait_gpu);
ba4e7d97
TH
1029 if (ret)
1030 goto out_unlock;
97a875cb
ML
1031 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1032 interruptible, no_wait_gpu);
ba4e7d97 1033out_unlock:
d961db75
BS
1034 if (ret && mem.mm_node)
1035 ttm_bo_mem_put(bo, &mem);
ba4e7d97
TH
1036 return ret;
1037}
1038
59c8e663
TH
1039static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1040 struct ttm_mem_reg *mem,
1041 uint32_t *new_flags)
ba4e7d97 1042{
ca262a99 1043 int i;
e22238ea 1044
ca262a99 1045 for (i = 0; i < placement->num_placement; i++) {
f1217ed0
CK
1046 const struct ttm_place *heap = &placement->placement[i];
1047 if (mem->mm_node && heap->lpfn != 0 &&
1048 (mem->start < heap->fpfn ||
1049 mem->start + mem->num_pages > heap->lpfn))
1050 continue;
1051
1052 *new_flags = heap->flags;
59c8e663
TH
1053 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1054 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1055 return true;
1056 }
1057
1058 for (i = 0; i < placement->num_busy_placement; i++) {
f1217ed0
CK
1059 const struct ttm_place *heap = &placement->busy_placement[i];
1060 if (mem->mm_node && heap->lpfn != 0 &&
1061 (mem->start < heap->fpfn ||
1062 mem->start + mem->num_pages > heap->lpfn))
1063 continue;
1064
1065 *new_flags = heap->flags;
59c8e663
TH
1066 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1067 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1068 return true;
ca262a99 1069 }
59c8e663
TH
1070
1071 return false;
ba4e7d97
TH
1072}
1073
09855acb
JG
1074int ttm_bo_validate(struct ttm_buffer_object *bo,
1075 struct ttm_placement *placement,
97a875cb 1076 bool interruptible,
9d87fa21 1077 bool no_wait_gpu)
ba4e7d97
TH
1078{
1079 int ret;
59c8e663 1080 uint32_t new_flags;
ba4e7d97 1081
009a9dad 1082 lockdep_assert_held(&bo->resv->lock.base);
ba4e7d97
TH
1083 /*
1084 * Check whether we need to move buffer.
1085 */
59c8e663 1086 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
97a875cb
ML
1087 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1088 no_wait_gpu);
ca262a99 1089 if (ret)
ba4e7d97 1090 return ret;
ca262a99
JG
1091 } else {
1092 /*
1093 * Use the access and other non-mapping-related flag bits from
1094 * the compatible memory placement flags to the active flags
1095 */
59c8e663 1096 ttm_flag_masked(&bo->mem.placement, new_flags,
ca262a99 1097 ~TTM_PL_MASK_MEMTYPE);
ba4e7d97 1098 }
ba4e7d97
TH
1099 /*
1100 * We might need to add a TTM.
1101 */
ba4e7d97
TH
1102 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1103 ret = ttm_bo_add_ttm(bo, true);
1104 if (ret)
1105 return ret;
1106 }
ba4e7d97
TH
1107 return 0;
1108}
09855acb 1109EXPORT_SYMBOL(ttm_bo_validate);
ba4e7d97 1110
09855acb
JG
1111int ttm_bo_init(struct ttm_bo_device *bdev,
1112 struct ttm_buffer_object *bo,
1113 unsigned long size,
1114 enum ttm_bo_type type,
1115 struct ttm_placement *placement,
1116 uint32_t page_alignment,
09855acb 1117 bool interruptible,
5df23979 1118 struct file *persistent_swap_storage,
09855acb 1119 size_t acc_size,
129b78bf 1120 struct sg_table *sg,
09855acb 1121 void (*destroy) (struct ttm_buffer_object *))
ba4e7d97 1122{
09855acb 1123 int ret = 0;
ba4e7d97 1124 unsigned long num_pages;
57de4ba9 1125 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
5e338405 1126 bool locked;
57de4ba9
JG
1127
1128 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1129 if (ret) {
25d0479a 1130 pr_err("Out of kernel memory\n");
57de4ba9
JG
1131 if (destroy)
1132 (*destroy)(bo);
1133 else
1134 kfree(bo);
1135 return -ENOMEM;
1136 }
ba4e7d97 1137
ba4e7d97
TH
1138 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1139 if (num_pages == 0) {
25d0479a 1140 pr_err("Illegal buffer object size\n");
7dfbbdcf
TH
1141 if (destroy)
1142 (*destroy)(bo);
1143 else
1144 kfree(bo);
a393c730 1145 ttm_mem_global_free(mem_glob, acc_size);
ba4e7d97
TH
1146 return -EINVAL;
1147 }
1148 bo->destroy = destroy;
1149
ba4e7d97
TH
1150 kref_init(&bo->kref);
1151 kref_init(&bo->list_kref);
1152 atomic_set(&bo->cpu_writers, 0);
ba4e7d97
TH
1153 INIT_LIST_HEAD(&bo->lru);
1154 INIT_LIST_HEAD(&bo->ddestroy);
1155 INIT_LIST_HEAD(&bo->swap);
eba67093 1156 INIT_LIST_HEAD(&bo->io_reserve_lru);
c58f009e 1157 mutex_init(&bo->wu_mutex);
ba4e7d97 1158 bo->bdev = bdev;
a987fcaa 1159 bo->glob = bdev->glob;
ba4e7d97
TH
1160 bo->type = type;
1161 bo->num_pages = num_pages;
eb6d2c39 1162 bo->mem.size = num_pages << PAGE_SHIFT;
ba4e7d97
TH
1163 bo->mem.mem_type = TTM_PL_SYSTEM;
1164 bo->mem.num_pages = bo->num_pages;
1165 bo->mem.mm_node = NULL;
1166 bo->mem.page_alignment = page_alignment;
eba67093
TH
1167 bo->mem.bus.io_reserved_vm = false;
1168 bo->mem.bus.io_reserved_count = 0;
ba4e7d97
TH
1169 bo->priv_flags = 0;
1170 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
5df23979 1171 bo->persistent_swap_storage = persistent_swap_storage;
ba4e7d97 1172 bo->acc_size = acc_size;
129b78bf 1173 bo->sg = sg;
5e338405
ML
1174 bo->resv = &bo->ttm_resv;
1175 reservation_object_init(bo->resv);
a987fcaa 1176 atomic_inc(&bo->glob->bo_count);
72525b3f 1177 drm_vma_node_reset(&bo->vma_node);
ba4e7d97 1178
ba4e7d97
TH
1179 /*
1180 * For ttm_bo_type_device buffers, allocate
1181 * address space from the device.
1182 */
f1217ed0
CK
1183 if (bo->type == ttm_bo_type_device ||
1184 bo->type == ttm_bo_type_sg)
abf19035
DH
1185 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1186 bo->mem.num_pages);
ba4e7d97 1187
5e338405
ML
1188 locked = ww_mutex_trylock(&bo->resv->lock);
1189 WARN_ON(!locked);
ba4e7d97 1190
5e338405
ML
1191 if (likely(!ret))
1192 ret = ttm_bo_validate(bo, placement, interruptible, false);
ba4e7d97 1193
ba4e7d97 1194 ttm_bo_unreserve(bo);
5e338405
ML
1195
1196 if (unlikely(ret))
1197 ttm_bo_unref(&bo);
ba4e7d97
TH
1198
1199 return ret;
1200}
09855acb 1201EXPORT_SYMBOL(ttm_bo_init);
ba4e7d97 1202
57de4ba9
JG
1203size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1204 unsigned long bo_size,
1205 unsigned struct_size)
ba4e7d97 1206{
57de4ba9
JG
1207 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1208 size_t size = 0;
ba4e7d97 1209
57de4ba9
JG
1210 size += ttm_round_pot(struct_size);
1211 size += PAGE_ALIGN(npages * sizeof(void *));
1212 size += ttm_round_pot(sizeof(struct ttm_tt));
1213 return size;
ba4e7d97 1214}
57de4ba9
JG
1215EXPORT_SYMBOL(ttm_bo_acc_size);
1216
1217size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1218 unsigned long bo_size,
1219 unsigned struct_size)
1220{
1221 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1222 size_t size = 0;
1223
1224 size += ttm_round_pot(struct_size);
1225 size += PAGE_ALIGN(npages * sizeof(void *));
1226 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1227 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1228 return size;
1229}
1230EXPORT_SYMBOL(ttm_bo_dma_acc_size);
ba4e7d97 1231
09855acb
JG
1232int ttm_bo_create(struct ttm_bo_device *bdev,
1233 unsigned long size,
1234 enum ttm_bo_type type,
1235 struct ttm_placement *placement,
1236 uint32_t page_alignment,
09855acb 1237 bool interruptible,
5df23979 1238 struct file *persistent_swap_storage,
09855acb 1239 struct ttm_buffer_object **p_bo)
ba4e7d97
TH
1240{
1241 struct ttm_buffer_object *bo;
57de4ba9 1242 size_t acc_size;
ca262a99 1243 int ret;
ba4e7d97 1244
ba4e7d97 1245 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
a393c730 1246 if (unlikely(bo == NULL))
ba4e7d97 1247 return -ENOMEM;
ba4e7d97 1248
a393c730 1249 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
09855acb 1250 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
0b91c4a1
MS
1251 interruptible, persistent_swap_storage, acc_size,
1252 NULL, NULL);
ba4e7d97
TH
1253 if (likely(ret == 0))
1254 *p_bo = bo;
1255
1256 return ret;
1257}
4d798937 1258EXPORT_SYMBOL(ttm_bo_create);
ba4e7d97 1259
ba4e7d97 1260static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
ca262a99 1261 unsigned mem_type, bool allow_errors)
ba4e7d97 1262{
ca262a99 1263 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
a987fcaa 1264 struct ttm_bo_global *glob = bdev->glob;
ba4e7d97 1265 int ret;
ba4e7d97
TH
1266
1267 /*
1268 * Can't use standard list traversal since we're unlocking.
1269 */
1270
a987fcaa 1271 spin_lock(&glob->lru_lock);
ca262a99 1272 while (!list_empty(&man->lru)) {
a987fcaa 1273 spin_unlock(&glob->lru_lock);
97a875cb 1274 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
ca262a99
JG
1275 if (ret) {
1276 if (allow_errors) {
1277 return ret;
1278 } else {
25d0479a 1279 pr_err("Cleanup eviction failed\n");
ca262a99
JG
1280 }
1281 }
a987fcaa 1282 spin_lock(&glob->lru_lock);
ba4e7d97 1283 }
a987fcaa 1284 spin_unlock(&glob->lru_lock);
ba4e7d97
TH
1285 return 0;
1286}
1287
1288int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1289{
c96e7c7a 1290 struct ttm_mem_type_manager *man;
ba4e7d97
TH
1291 int ret = -EINVAL;
1292
1293 if (mem_type >= TTM_NUM_MEM_TYPES) {
25d0479a 1294 pr_err("Illegal memory type %d\n", mem_type);
ba4e7d97
TH
1295 return ret;
1296 }
c96e7c7a 1297 man = &bdev->man[mem_type];
ba4e7d97
TH
1298
1299 if (!man->has_type) {
25d0479a
JP
1300 pr_err("Trying to take down uninitialized memory manager type %u\n",
1301 mem_type);
ba4e7d97
TH
1302 return ret;
1303 }
1304
1305 man->use_type = false;
1306 man->has_type = false;
1307
1308 ret = 0;
1309 if (mem_type > 0) {
ca262a99 1310 ttm_bo_force_list_clean(bdev, mem_type, false);
ba4e7d97 1311
d961db75 1312 ret = (*man->func->takedown)(man);
ba4e7d97
TH
1313 }
1314
1315 return ret;
1316}
1317EXPORT_SYMBOL(ttm_bo_clean_mm);
1318
1319int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1320{
1321 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1322
1323 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
25d0479a 1324 pr_err("Illegal memory manager memory type %u\n", mem_type);
ba4e7d97
TH
1325 return -EINVAL;
1326 }
1327
1328 if (!man->has_type) {
25d0479a 1329 pr_err("Memory type %u has not been initialized\n", mem_type);
ba4e7d97
TH
1330 return 0;
1331 }
1332
ca262a99 1333 return ttm_bo_force_list_clean(bdev, mem_type, true);
ba4e7d97
TH
1334}
1335EXPORT_SYMBOL(ttm_bo_evict_mm);
1336
1337int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
ca262a99 1338 unsigned long p_size)
ba4e7d97
TH
1339{
1340 int ret = -EINVAL;
1341 struct ttm_mem_type_manager *man;
1342
dbc4a5b8 1343 BUG_ON(type >= TTM_NUM_MEM_TYPES);
ba4e7d97 1344 man = &bdev->man[type];
dbc4a5b8 1345 BUG_ON(man->has_type);
eba67093
TH
1346 man->io_reserve_fastpath = true;
1347 man->use_io_reserve_lru = false;
1348 mutex_init(&man->io_reserve_mutex);
1349 INIT_LIST_HEAD(&man->io_reserve_lru);
ba4e7d97
TH
1350
1351 ret = bdev->driver->init_mem_type(bdev, type, man);
1352 if (ret)
1353 return ret;
d961db75 1354 man->bdev = bdev;
ba4e7d97
TH
1355
1356 ret = 0;
1357 if (type != TTM_PL_SYSTEM) {
d961db75 1358 ret = (*man->func->init)(man, p_size);
ba4e7d97
TH
1359 if (ret)
1360 return ret;
1361 }
1362 man->has_type = true;
1363 man->use_type = true;
1364 man->size = p_size;
1365
1366 INIT_LIST_HEAD(&man->lru);
1367
1368 return 0;
1369}
1370EXPORT_SYMBOL(ttm_bo_init_mm);
1371
a987fcaa
TH
1372static void ttm_bo_global_kobj_release(struct kobject *kobj)
1373{
1374 struct ttm_bo_global *glob =
1375 container_of(kobj, struct ttm_bo_global, kobj);
1376
a987fcaa
TH
1377 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1378 __free_page(glob->dummy_read_page);
1379 kfree(glob);
1380}
1381
ba4420c2 1382void ttm_bo_global_release(struct drm_global_reference *ref)
a987fcaa
TH
1383{
1384 struct ttm_bo_global *glob = ref->object;
1385
1386 kobject_del(&glob->kobj);
1387 kobject_put(&glob->kobj);
1388}
1389EXPORT_SYMBOL(ttm_bo_global_release);
1390
ba4420c2 1391int ttm_bo_global_init(struct drm_global_reference *ref)
a987fcaa
TH
1392{
1393 struct ttm_bo_global_ref *bo_ref =
1394 container_of(ref, struct ttm_bo_global_ref, ref);
1395 struct ttm_bo_global *glob = ref->object;
1396 int ret;
1397
1398 mutex_init(&glob->device_list_mutex);
1399 spin_lock_init(&glob->lru_lock);
1400 glob->mem_glob = bo_ref->mem_glob;
1401 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1402
1403 if (unlikely(glob->dummy_read_page == NULL)) {
1404 ret = -ENOMEM;
1405 goto out_no_drp;
1406 }
1407
1408 INIT_LIST_HEAD(&glob->swap_lru);
1409 INIT_LIST_HEAD(&glob->device_list);
1410
1411 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1412 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1413 if (unlikely(ret != 0)) {
25d0479a 1414 pr_err("Could not register buffer object swapout\n");
a987fcaa
TH
1415 goto out_no_shrink;
1416 }
1417
a987fcaa
TH
1418 atomic_set(&glob->bo_count, 0);
1419
b642ed06
RD
1420 ret = kobject_init_and_add(
1421 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
a987fcaa
TH
1422 if (unlikely(ret != 0))
1423 kobject_put(&glob->kobj);
1424 return ret;
1425out_no_shrink:
1426 __free_page(glob->dummy_read_page);
1427out_no_drp:
1428 kfree(glob);
1429 return ret;
1430}
1431EXPORT_SYMBOL(ttm_bo_global_init);
1432
1433
ba4e7d97
TH
1434int ttm_bo_device_release(struct ttm_bo_device *bdev)
1435{
1436 int ret = 0;
1437 unsigned i = TTM_NUM_MEM_TYPES;
1438 struct ttm_mem_type_manager *man;
a987fcaa 1439 struct ttm_bo_global *glob = bdev->glob;
ba4e7d97
TH
1440
1441 while (i--) {
1442 man = &bdev->man[i];
1443 if (man->has_type) {
1444 man->use_type = false;
1445 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1446 ret = -EBUSY;
25d0479a
JP
1447 pr_err("DRM memory manager type %d is not clean\n",
1448 i);
ba4e7d97
TH
1449 }
1450 man->has_type = false;
1451 }
1452 }
1453
a987fcaa
TH
1454 mutex_lock(&glob->device_list_mutex);
1455 list_del(&bdev->device_list);
1456 mutex_unlock(&glob->device_list_mutex);
1457
f094cfc6 1458 cancel_delayed_work_sync(&bdev->wq);
ba4e7d97
TH
1459
1460 while (ttm_bo_delayed_delete(bdev, true))
1461 ;
1462
a987fcaa 1463 spin_lock(&glob->lru_lock);
ba4e7d97
TH
1464 if (list_empty(&bdev->ddestroy))
1465 TTM_DEBUG("Delayed destroy list was clean\n");
1466
1467 if (list_empty(&bdev->man[0].lru))
1468 TTM_DEBUG("Swap list was clean\n");
a987fcaa 1469 spin_unlock(&glob->lru_lock);
ba4e7d97 1470
72525b3f 1471 drm_vma_offset_manager_destroy(&bdev->vma_manager);
ba4e7d97 1472
ba4e7d97
TH
1473 return ret;
1474}
1475EXPORT_SYMBOL(ttm_bo_device_release);
1476
ba4e7d97 1477int ttm_bo_device_init(struct ttm_bo_device *bdev,
a987fcaa
TH
1478 struct ttm_bo_global *glob,
1479 struct ttm_bo_driver *driver,
44d847b7 1480 struct address_space *mapping,
51c8b407 1481 uint64_t file_page_offset,
ad49f501 1482 bool need_dma32)
ba4e7d97
TH
1483{
1484 int ret = -EINVAL;
1485
ba4e7d97 1486 bdev->driver = driver;
ba4e7d97
TH
1487
1488 memset(bdev->man, 0, sizeof(bdev->man));
1489
ba4e7d97
TH
1490 /*
1491 * Initialize the system memory buffer type.
1492 * Other types need to be driver / IOCTL initialized.
1493 */
ca262a99 1494 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
ba4e7d97 1495 if (unlikely(ret != 0))
a987fcaa 1496 goto out_no_sys;
ba4e7d97 1497
72525b3f
DH
1498 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1499 0x10000000);
ba4e7d97 1500 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
ba4e7d97 1501 INIT_LIST_HEAD(&bdev->ddestroy);
44d847b7 1502 bdev->dev_mapping = mapping;
a987fcaa 1503 bdev->glob = glob;
ad49f501 1504 bdev->need_dma32 = need_dma32;
65705962 1505 bdev->val_seq = 0;
a987fcaa
TH
1506 mutex_lock(&glob->device_list_mutex);
1507 list_add_tail(&bdev->device_list, &glob->device_list);
1508 mutex_unlock(&glob->device_list_mutex);
ba4e7d97
TH
1509
1510 return 0;
a987fcaa 1511out_no_sys:
ba4e7d97
TH
1512 return ret;
1513}
1514EXPORT_SYMBOL(ttm_bo_device_init);
1515
1516/*
1517 * buffer object vm functions.
1518 */
1519
1520bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1521{
1522 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1523
1524 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1525 if (mem->mem_type == TTM_PL_SYSTEM)
1526 return false;
1527
1528 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1529 return false;
1530
1531 if (mem->placement & TTM_PL_FLAG_CACHED)
1532 return false;
1533 }
1534 return true;
1535}
1536
eba67093 1537void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
ba4e7d97
TH
1538{
1539 struct ttm_bo_device *bdev = bo->bdev;
ba4e7d97 1540
51335df9 1541 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
eba67093 1542 ttm_mem_io_free_vm(bo);
ba4e7d97 1543}
eba67093
TH
1544
1545void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1546{
1547 struct ttm_bo_device *bdev = bo->bdev;
1548 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1549
1550 ttm_mem_io_lock(man, false);
1551 ttm_bo_unmap_virtual_locked(bo);
1552 ttm_mem_io_unlock(man);
ba4e7d97 1553}
eba67093
TH
1554
1555
e024e110 1556EXPORT_SYMBOL(ttm_bo_unmap_virtual);
ba4e7d97 1557
ba4e7d97 1558int ttm_bo_wait(struct ttm_buffer_object *bo,
1717c0e2 1559 bool lazy, bool interruptible, bool no_wait)
ba4e7d97 1560{
f2c24b83
ML
1561 struct reservation_object_list *fobj;
1562 struct reservation_object *resv;
1563 struct fence *excl;
1564 long timeout = 15 * HZ;
1565 int i;
7040138f 1566
f2c24b83
ML
1567 resv = bo->resv;
1568 fobj = reservation_object_get_list(resv);
1569 excl = reservation_object_get_excl(resv);
1570 if (excl) {
1571 if (!fence_is_signaled(excl)) {
1572 if (no_wait)
1573 return -EBUSY;
ba4e7d97 1574
f2c24b83
ML
1575 timeout = fence_wait_timeout(excl,
1576 interruptible, timeout);
ba4e7d97 1577 }
f2c24b83 1578 }
ba4e7d97 1579
f2c24b83
ML
1580 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1581 struct fence *fence;
1582 fence = rcu_dereference_protected(fobj->shared[i],
1583 reservation_object_held(resv));
ba4e7d97 1584
f2c24b83
ML
1585 if (!fence_is_signaled(fence)) {
1586 if (no_wait)
1587 return -EBUSY;
dd7cfd64 1588
f2c24b83
ML
1589 timeout = fence_wait_timeout(fence,
1590 interruptible, timeout);
ba4e7d97
TH
1591 }
1592 }
f2c24b83
ML
1593
1594 if (timeout < 0)
1595 return timeout;
1596
1597 if (timeout == 0)
1598 return -EBUSY;
1599
1600 reservation_object_add_excl_fence(resv, NULL);
1601 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1602 return 0;
ba4e7d97
TH
1603}
1604EXPORT_SYMBOL(ttm_bo_wait);
1605
ba4e7d97
TH
1606int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1607{
1608 int ret = 0;
1609
1610 /*
8cfe92d6 1611 * Using ttm_bo_reserve makes sure the lru lists are updated.
ba4e7d97
TH
1612 */
1613
0eff2a24 1614 ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
ba4e7d97
TH
1615 if (unlikely(ret != 0))
1616 return ret;
1717c0e2 1617 ret = ttm_bo_wait(bo, false, true, no_wait);
ba4e7d97
TH
1618 if (likely(ret == 0))
1619 atomic_inc(&bo->cpu_writers);
1620 ttm_bo_unreserve(bo);
1621 return ret;
1622}
d1ede145 1623EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
ba4e7d97
TH
1624
1625void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1626{
654aa792 1627 atomic_dec(&bo->cpu_writers);
ba4e7d97 1628}
d1ede145 1629EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
ba4e7d97
TH
1630
1631/**
1632 * A buffer object shrink method that tries to swap out the first
1633 * buffer object on the bo_global::swap_lru list.
1634 */
1635
1636static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1637{
a987fcaa
TH
1638 struct ttm_bo_global *glob =
1639 container_of(shrink, struct ttm_bo_global, shrink);
ba4e7d97
TH
1640 struct ttm_buffer_object *bo;
1641 int ret = -EBUSY;
1642 int put_count;
1643 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1644
a987fcaa 1645 spin_lock(&glob->lru_lock);
2b7b3ad2 1646 list_for_each_entry(bo, &glob->swap_lru, swap) {
0eff2a24 1647 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
2b7b3ad2
ML
1648 if (!ret)
1649 break;
1650 }
85b144f8 1651
2b7b3ad2
ML
1652 if (ret) {
1653 spin_unlock(&glob->lru_lock);
1654 return ret;
1655 }
e1efc9b6 1656
2b7b3ad2 1657 kref_get(&bo->list_kref);
ba4e7d97 1658
2b7b3ad2
ML
1659 if (!list_empty(&bo->ddestroy)) {
1660 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1661 kref_put(&bo->list_kref, ttm_bo_release_list);
1662 return ret;
ba4e7d97
TH
1663 }
1664
ba4e7d97 1665 put_count = ttm_bo_del_from_lru(bo);
a987fcaa 1666 spin_unlock(&glob->lru_lock);
ba4e7d97 1667
d6ea8886 1668 ttm_bo_list_ref_sub(bo, put_count, true);
ba4e7d97
TH
1669
1670 /**
1671 * Wait for GPU, then move to system cached.
1672 */
1673
1717c0e2 1674 ret = ttm_bo_wait(bo, false, false, false);
ba4e7d97
TH
1675
1676 if (unlikely(ret != 0))
1677 goto out;
1678
1679 if ((bo->mem.placement & swap_placement) != swap_placement) {
1680 struct ttm_mem_reg evict_mem;
1681
1682 evict_mem = bo->mem;
1683 evict_mem.mm_node = NULL;
1684 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1685 evict_mem.mem_type = TTM_PL_SYSTEM;
1686
1687 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
97a875cb 1688 false, false);
ba4e7d97
TH
1689 if (unlikely(ret != 0))
1690 goto out;
1691 }
1692
1693 ttm_bo_unmap_virtual(bo);
1694
1695 /**
1696 * Swap out. Buffer will be swapped in again as soon as
1697 * anyone tries to access a ttm page.
1698 */
1699
3f09ea4e
TH
1700 if (bo->bdev->driver->swap_notify)
1701 bo->bdev->driver->swap_notify(bo);
1702
5df23979 1703 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
ba4e7d97
TH
1704out:
1705
1706 /**
1707 *
1708 * Unreserve without putting on LRU to avoid swapping out an
1709 * already swapped buffer.
1710 */
1711
c7523083 1712 __ttm_bo_unreserve(bo);
ba4e7d97
TH
1713 kref_put(&bo->list_kref, ttm_bo_release_list);
1714 return ret;
1715}
1716
1717void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1718{
a987fcaa 1719 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
ba4e7d97
TH
1720 ;
1721}
e99e1e78 1722EXPORT_SYMBOL(ttm_bo_swapout_all);
c58f009e
TH
1723
1724/**
1725 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1726 * unreserved
1727 *
1728 * @bo: Pointer to buffer
1729 */
1730int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1731{
1732 int ret;
1733
1734 /*
1735 * In the absense of a wait_unlocked API,
1736 * Use the bo::wu_mutex to avoid triggering livelocks due to
1737 * concurrent use of this function. Note that this use of
1738 * bo::wu_mutex can go away if we change locking order to
1739 * mmap_sem -> bo::reserve.
1740 */
1741 ret = mutex_lock_interruptible(&bo->wu_mutex);
1742 if (unlikely(ret != 0))
1743 return -ERESTARTSYS;
1744 if (!ww_mutex_is_locked(&bo->resv->lock))
1745 goto out_unlock;
c7523083 1746 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
c58f009e
TH
1747 if (unlikely(ret != 0))
1748 goto out_unlock;
c7523083 1749 __ttm_bo_unreserve(bo);
c58f009e
TH
1750
1751out_unlock:
1752 mutex_unlock(&bo->wu_mutex);
1753 return ret;
1754}
This page took 0.467484 seconds and 5 git commands to generate.