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