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