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