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