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