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