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