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