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