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