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