ttm: Make parts of a struct ttm_bo_device global.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / ttm / ttm_bo.c
blob0d0b1b7afbcff7cd841e3f13533481bad30dbd80
1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include "ttm/ttm_module.h"
32 #include "ttm/ttm_bo_driver.h"
33 #include "ttm/ttm_placement.h"
34 #include <linux/jiffies.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
37 #include <linux/mm.h>
38 #include <linux/file.h>
39 #include <linux/module.h>
41 #define TTM_ASSERT_LOCKED(param)
42 #define TTM_DEBUG(fmt, arg...)
43 #define TTM_BO_HASH_ORDER 13
45 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
46 static void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
47 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
48 static void ttm_bo_global_kobj_release(struct kobject *kobj);
50 static struct attribute ttm_bo_count = {
51 .name = "bo_count",
52 .mode = S_IRUGO
55 static ssize_t ttm_bo_global_show(struct kobject *kobj,
56 struct attribute *attr,
57 char *buffer)
59 struct ttm_bo_global *glob =
60 container_of(kobj, struct ttm_bo_global, kobj);
62 return snprintf(buffer, PAGE_SIZE, "%lu\n",
63 (unsigned long) atomic_read(&glob->bo_count));
66 static struct attribute *ttm_bo_global_attrs[] = {
67 &ttm_bo_count,
68 NULL
71 static struct sysfs_ops ttm_bo_global_ops = {
72 .show = &ttm_bo_global_show
75 static struct kobj_type ttm_bo_glob_kobj_type = {
76 .release = &ttm_bo_global_kobj_release,
77 .sysfs_ops = &ttm_bo_global_ops,
78 .default_attrs = ttm_bo_global_attrs
82 static inline uint32_t ttm_bo_type_flags(unsigned type)
84 return 1 << (type);
87 static void ttm_bo_release_list(struct kref *list_kref)
89 struct ttm_buffer_object *bo =
90 container_of(list_kref, struct ttm_buffer_object, list_kref);
91 struct ttm_bo_device *bdev = bo->bdev;
93 BUG_ON(atomic_read(&bo->list_kref.refcount));
94 BUG_ON(atomic_read(&bo->kref.refcount));
95 BUG_ON(atomic_read(&bo->cpu_writers));
96 BUG_ON(bo->sync_obj != NULL);
97 BUG_ON(bo->mem.mm_node != NULL);
98 BUG_ON(!list_empty(&bo->lru));
99 BUG_ON(!list_empty(&bo->ddestroy));
101 if (bo->ttm)
102 ttm_tt_destroy(bo->ttm);
103 atomic_dec(&bo->glob->bo_count);
104 if (bo->destroy)
105 bo->destroy(bo);
106 else {
107 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
108 kfree(bo);
112 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
115 if (interruptible) {
116 int ret = 0;
118 ret = wait_event_interruptible(bo->event_queue,
119 atomic_read(&bo->reserved) == 0);
120 if (unlikely(ret != 0))
121 return -ERESTART;
122 } else {
123 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
125 return 0;
128 static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
130 struct ttm_bo_device *bdev = bo->bdev;
131 struct ttm_mem_type_manager *man;
133 BUG_ON(!atomic_read(&bo->reserved));
135 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
137 BUG_ON(!list_empty(&bo->lru));
139 man = &bdev->man[bo->mem.mem_type];
140 list_add_tail(&bo->lru, &man->lru);
141 kref_get(&bo->list_kref);
143 if (bo->ttm != NULL) {
144 list_add_tail(&bo->swap, &bo->glob->swap_lru);
145 kref_get(&bo->list_kref);
151 * Call with the lru_lock held.
154 static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
156 int put_count = 0;
158 if (!list_empty(&bo->swap)) {
159 list_del_init(&bo->swap);
160 ++put_count;
162 if (!list_empty(&bo->lru)) {
163 list_del_init(&bo->lru);
164 ++put_count;
168 * TODO: Add a driver hook to delete from
169 * driver-specific LRU's here.
172 return put_count;
175 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
176 bool interruptible,
177 bool no_wait, bool use_sequence, uint32_t sequence)
179 struct ttm_bo_global *glob = bo->glob;
180 int ret;
182 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
183 if (use_sequence && bo->seq_valid &&
184 (sequence - bo->val_seq < (1 << 31))) {
185 return -EAGAIN;
188 if (no_wait)
189 return -EBUSY;
191 spin_unlock(&glob->lru_lock);
192 ret = ttm_bo_wait_unreserved(bo, interruptible);
193 spin_lock(&glob->lru_lock);
195 if (unlikely(ret))
196 return ret;
199 if (use_sequence) {
200 bo->val_seq = sequence;
201 bo->seq_valid = true;
202 } else {
203 bo->seq_valid = false;
206 return 0;
208 EXPORT_SYMBOL(ttm_bo_reserve);
210 static void ttm_bo_ref_bug(struct kref *list_kref)
212 BUG();
215 int ttm_bo_reserve(struct ttm_buffer_object *bo,
216 bool interruptible,
217 bool no_wait, bool use_sequence, uint32_t sequence)
219 struct ttm_bo_global *glob = bo->glob;
220 int put_count = 0;
221 int ret;
223 spin_lock(&glob->lru_lock);
224 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
225 sequence);
226 if (likely(ret == 0))
227 put_count = ttm_bo_del_from_lru(bo);
228 spin_unlock(&glob->lru_lock);
230 while (put_count--)
231 kref_put(&bo->list_kref, ttm_bo_ref_bug);
233 return ret;
236 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
238 struct ttm_bo_global *glob = bo->glob;
240 spin_lock(&glob->lru_lock);
241 ttm_bo_add_to_lru(bo);
242 atomic_set(&bo->reserved, 0);
243 wake_up_all(&bo->event_queue);
244 spin_unlock(&glob->lru_lock);
246 EXPORT_SYMBOL(ttm_bo_unreserve);
249 * Call bo->mutex locked.
252 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
254 struct ttm_bo_device *bdev = bo->bdev;
255 struct ttm_bo_global *glob = bo->glob;
256 int ret = 0;
257 uint32_t page_flags = 0;
259 TTM_ASSERT_LOCKED(&bo->mutex);
260 bo->ttm = NULL;
262 switch (bo->type) {
263 case ttm_bo_type_device:
264 if (zero_alloc)
265 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
266 case ttm_bo_type_kernel:
267 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
268 page_flags, glob->dummy_read_page);
269 if (unlikely(bo->ttm == NULL))
270 ret = -ENOMEM;
271 break;
272 case ttm_bo_type_user:
273 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
274 page_flags | TTM_PAGE_FLAG_USER,
275 glob->dummy_read_page);
276 if (unlikely(bo->ttm == NULL))
277 ret = -ENOMEM;
278 break;
280 ret = ttm_tt_set_user(bo->ttm, current,
281 bo->buffer_start, bo->num_pages);
282 if (unlikely(ret != 0))
283 ttm_tt_destroy(bo->ttm);
284 break;
285 default:
286 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
287 ret = -EINVAL;
288 break;
291 return ret;
294 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
295 struct ttm_mem_reg *mem,
296 bool evict, bool interruptible, bool no_wait)
298 struct ttm_bo_device *bdev = bo->bdev;
299 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
300 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
301 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
302 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
303 int ret = 0;
305 if (old_is_pci || new_is_pci ||
306 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
307 ttm_bo_unmap_virtual(bo);
310 * Create and bind a ttm if required.
313 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
314 ret = ttm_bo_add_ttm(bo, false);
315 if (ret)
316 goto out_err;
318 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
319 if (ret)
320 goto out_err;
322 if (mem->mem_type != TTM_PL_SYSTEM) {
323 ret = ttm_tt_bind(bo->ttm, mem);
324 if (ret)
325 goto out_err;
328 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
330 struct ttm_mem_reg *old_mem = &bo->mem;
331 uint32_t save_flags = old_mem->placement;
333 *old_mem = *mem;
334 mem->mm_node = NULL;
335 ttm_flag_masked(&save_flags, mem->placement,
336 TTM_PL_MASK_MEMTYPE);
337 goto moved;
342 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
343 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
344 ret = ttm_bo_move_ttm(bo, evict, no_wait, mem);
345 else if (bdev->driver->move)
346 ret = bdev->driver->move(bo, evict, interruptible,
347 no_wait, mem);
348 else
349 ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem);
351 if (ret)
352 goto out_err;
354 moved:
355 if (bo->evicted) {
356 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
357 if (ret)
358 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
359 bo->evicted = false;
362 if (bo->mem.mm_node) {
363 spin_lock(&bo->lock);
364 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
365 bdev->man[bo->mem.mem_type].gpu_offset;
366 bo->cur_placement = bo->mem.placement;
367 spin_unlock(&bo->lock);
370 return 0;
372 out_err:
373 new_man = &bdev->man[bo->mem.mem_type];
374 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
375 ttm_tt_unbind(bo->ttm);
376 ttm_tt_destroy(bo->ttm);
377 bo->ttm = NULL;
380 return ret;
384 * If bo idle, remove from delayed- and lru lists, and unref.
385 * If not idle, and already on delayed list, do nothing.
386 * If not idle, and not on delayed list, put on delayed list,
387 * up the list_kref and schedule a delayed list check.
390 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
392 struct ttm_bo_device *bdev = bo->bdev;
393 struct ttm_bo_global *glob = bo->glob;
394 struct ttm_bo_driver *driver = bdev->driver;
395 int ret;
397 spin_lock(&bo->lock);
398 (void) ttm_bo_wait(bo, false, false, !remove_all);
400 if (!bo->sync_obj) {
401 int put_count;
403 spin_unlock(&bo->lock);
405 spin_lock(&glob->lru_lock);
406 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
407 BUG_ON(ret);
408 if (bo->ttm)
409 ttm_tt_unbind(bo->ttm);
411 if (!list_empty(&bo->ddestroy)) {
412 list_del_init(&bo->ddestroy);
413 kref_put(&bo->list_kref, ttm_bo_ref_bug);
415 if (bo->mem.mm_node) {
416 drm_mm_put_block(bo->mem.mm_node);
417 bo->mem.mm_node = NULL;
419 put_count = ttm_bo_del_from_lru(bo);
420 spin_unlock(&glob->lru_lock);
422 atomic_set(&bo->reserved, 0);
424 while (put_count--)
425 kref_put(&bo->list_kref, ttm_bo_release_list);
427 return 0;
430 spin_lock(&glob->lru_lock);
431 if (list_empty(&bo->ddestroy)) {
432 void *sync_obj = bo->sync_obj;
433 void *sync_obj_arg = bo->sync_obj_arg;
435 kref_get(&bo->list_kref);
436 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
437 spin_unlock(&glob->lru_lock);
438 spin_unlock(&bo->lock);
440 if (sync_obj)
441 driver->sync_obj_flush(sync_obj, sync_obj_arg);
442 schedule_delayed_work(&bdev->wq,
443 ((HZ / 100) < 1) ? 1 : HZ / 100);
444 ret = 0;
446 } else {
447 spin_unlock(&glob->lru_lock);
448 spin_unlock(&bo->lock);
449 ret = -EBUSY;
452 return ret;
456 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
457 * encountered buffers.
460 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
462 struct ttm_bo_global *glob = bdev->glob;
463 struct ttm_buffer_object *entry, *nentry;
464 struct list_head *list, *next;
465 int ret;
467 spin_lock(&glob->lru_lock);
468 list_for_each_safe(list, next, &bdev->ddestroy) {
469 entry = list_entry(list, struct ttm_buffer_object, ddestroy);
470 nentry = NULL;
473 * Protect the next list entry from destruction while we
474 * unlock the lru_lock.
477 if (next != &bdev->ddestroy) {
478 nentry = list_entry(next, struct ttm_buffer_object,
479 ddestroy);
480 kref_get(&nentry->list_kref);
482 kref_get(&entry->list_kref);
484 spin_unlock(&glob->lru_lock);
485 ret = ttm_bo_cleanup_refs(entry, remove_all);
486 kref_put(&entry->list_kref, ttm_bo_release_list);
488 spin_lock(&glob->lru_lock);
489 if (nentry) {
490 bool next_onlist = !list_empty(next);
491 spin_unlock(&glob->lru_lock);
492 kref_put(&nentry->list_kref, ttm_bo_release_list);
493 spin_lock(&glob->lru_lock);
495 * Someone might have raced us and removed the
496 * next entry from the list. We don't bother restarting
497 * list traversal.
500 if (!next_onlist)
501 break;
503 if (ret)
504 break;
506 ret = !list_empty(&bdev->ddestroy);
507 spin_unlock(&glob->lru_lock);
509 return ret;
512 static void ttm_bo_delayed_workqueue(struct work_struct *work)
514 struct ttm_bo_device *bdev =
515 container_of(work, struct ttm_bo_device, wq.work);
517 if (ttm_bo_delayed_delete(bdev, false)) {
518 schedule_delayed_work(&bdev->wq,
519 ((HZ / 100) < 1) ? 1 : HZ / 100);
523 static void ttm_bo_release(struct kref *kref)
525 struct ttm_buffer_object *bo =
526 container_of(kref, struct ttm_buffer_object, kref);
527 struct ttm_bo_device *bdev = bo->bdev;
529 if (likely(bo->vm_node != NULL)) {
530 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
531 drm_mm_put_block(bo->vm_node);
532 bo->vm_node = NULL;
534 write_unlock(&bdev->vm_lock);
535 ttm_bo_cleanup_refs(bo, false);
536 kref_put(&bo->list_kref, ttm_bo_release_list);
537 write_lock(&bdev->vm_lock);
540 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
542 struct ttm_buffer_object *bo = *p_bo;
543 struct ttm_bo_device *bdev = bo->bdev;
545 *p_bo = NULL;
546 write_lock(&bdev->vm_lock);
547 kref_put(&bo->kref, ttm_bo_release);
548 write_unlock(&bdev->vm_lock);
550 EXPORT_SYMBOL(ttm_bo_unref);
552 static int ttm_bo_evict(struct ttm_buffer_object *bo, unsigned mem_type,
553 bool interruptible, bool no_wait)
555 int ret = 0;
556 struct ttm_bo_device *bdev = bo->bdev;
557 struct ttm_bo_global *glob = bo->glob;
558 struct ttm_mem_reg evict_mem;
559 uint32_t proposed_placement;
561 if (bo->mem.mem_type != mem_type)
562 goto out;
564 spin_lock(&bo->lock);
565 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
566 spin_unlock(&bo->lock);
568 if (unlikely(ret != 0)) {
569 if (ret != -ERESTART) {
570 printk(KERN_ERR TTM_PFX
571 "Failed to expire sync object before "
572 "buffer eviction.\n");
574 goto out;
577 BUG_ON(!atomic_read(&bo->reserved));
579 evict_mem = bo->mem;
580 evict_mem.mm_node = NULL;
582 proposed_placement = bdev->driver->evict_flags(bo);
584 ret = ttm_bo_mem_space(bo, proposed_placement,
585 &evict_mem, interruptible, no_wait);
586 if (unlikely(ret != 0 && ret != -ERESTART))
587 ret = ttm_bo_mem_space(bo, TTM_PL_FLAG_SYSTEM,
588 &evict_mem, interruptible, no_wait);
590 if (ret) {
591 if (ret != -ERESTART)
592 printk(KERN_ERR TTM_PFX
593 "Failed to find memory space for "
594 "buffer 0x%p eviction.\n", bo);
595 goto out;
598 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
599 no_wait);
600 if (ret) {
601 if (ret != -ERESTART)
602 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
603 goto out;
606 spin_lock(&glob->lru_lock);
607 if (evict_mem.mm_node) {
608 drm_mm_put_block(evict_mem.mm_node);
609 evict_mem.mm_node = NULL;
611 spin_unlock(&glob->lru_lock);
612 bo->evicted = true;
613 out:
614 return ret;
618 * Repeatedly evict memory from the LRU for @mem_type until we create enough
619 * space, or we've evicted everything and there isn't enough space.
621 static int ttm_bo_mem_force_space(struct ttm_bo_device *bdev,
622 struct ttm_mem_reg *mem,
623 uint32_t mem_type,
624 bool interruptible, bool no_wait)
626 struct ttm_bo_global *glob = bdev->glob;
627 struct drm_mm_node *node;
628 struct ttm_buffer_object *entry;
629 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
630 struct list_head *lru;
631 unsigned long num_pages = mem->num_pages;
632 int put_count = 0;
633 int ret;
635 retry_pre_get:
636 ret = drm_mm_pre_get(&man->manager);
637 if (unlikely(ret != 0))
638 return ret;
640 spin_lock(&glob->lru_lock);
641 do {
642 node = drm_mm_search_free(&man->manager, num_pages,
643 mem->page_alignment, 1);
644 if (node)
645 break;
647 lru = &man->lru;
648 if (list_empty(lru))
649 break;
651 entry = list_first_entry(lru, struct ttm_buffer_object, lru);
652 kref_get(&entry->list_kref);
654 ret =
655 ttm_bo_reserve_locked(entry, interruptible, no_wait,
656 false, 0);
658 if (likely(ret == 0))
659 put_count = ttm_bo_del_from_lru(entry);
661 spin_unlock(&glob->lru_lock);
663 if (unlikely(ret != 0))
664 return ret;
666 while (put_count--)
667 kref_put(&entry->list_kref, ttm_bo_ref_bug);
669 ret = ttm_bo_evict(entry, mem_type, interruptible, no_wait);
671 ttm_bo_unreserve(entry);
673 kref_put(&entry->list_kref, ttm_bo_release_list);
674 if (ret)
675 return ret;
677 spin_lock(&glob->lru_lock);
678 } while (1);
680 if (!node) {
681 spin_unlock(&glob->lru_lock);
682 return -ENOMEM;
685 node = drm_mm_get_block_atomic(node, num_pages, mem->page_alignment);
686 if (unlikely(!node)) {
687 spin_unlock(&glob->lru_lock);
688 goto retry_pre_get;
691 spin_unlock(&glob->lru_lock);
692 mem->mm_node = node;
693 mem->mem_type = mem_type;
694 return 0;
697 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
698 bool disallow_fixed,
699 uint32_t mem_type,
700 uint32_t mask, uint32_t *res_mask)
702 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
704 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
705 return false;
707 if ((cur_flags & mask & TTM_PL_MASK_MEM) == 0)
708 return false;
710 if ((mask & man->available_caching) == 0)
711 return false;
712 if (mask & man->default_caching)
713 cur_flags |= man->default_caching;
714 else if (mask & TTM_PL_FLAG_CACHED)
715 cur_flags |= TTM_PL_FLAG_CACHED;
716 else if (mask & TTM_PL_FLAG_WC)
717 cur_flags |= TTM_PL_FLAG_WC;
718 else
719 cur_flags |= TTM_PL_FLAG_UNCACHED;
721 *res_mask = cur_flags;
722 return true;
726 * Creates space for memory region @mem according to its type.
728 * This function first searches for free space in compatible memory types in
729 * the priority order defined by the driver. If free space isn't found, then
730 * ttm_bo_mem_force_space is attempted in priority order to evict and find
731 * space.
733 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
734 uint32_t proposed_placement,
735 struct ttm_mem_reg *mem,
736 bool interruptible, bool no_wait)
738 struct ttm_bo_device *bdev = bo->bdev;
739 struct ttm_bo_global *glob = bo->glob;
740 struct ttm_mem_type_manager *man;
742 uint32_t num_prios = bdev->driver->num_mem_type_prio;
743 const uint32_t *prios = bdev->driver->mem_type_prio;
744 uint32_t i;
745 uint32_t mem_type = TTM_PL_SYSTEM;
746 uint32_t cur_flags = 0;
747 bool type_found = false;
748 bool type_ok = false;
749 bool has_eagain = false;
750 struct drm_mm_node *node = NULL;
751 int ret;
753 mem->mm_node = NULL;
754 for (i = 0; i < num_prios; ++i) {
755 mem_type = prios[i];
756 man = &bdev->man[mem_type];
758 type_ok = ttm_bo_mt_compatible(man,
759 bo->type == ttm_bo_type_user,
760 mem_type, proposed_placement,
761 &cur_flags);
763 if (!type_ok)
764 continue;
766 if (mem_type == TTM_PL_SYSTEM)
767 break;
769 if (man->has_type && man->use_type) {
770 type_found = true;
771 do {
772 ret = drm_mm_pre_get(&man->manager);
773 if (unlikely(ret))
774 return ret;
776 spin_lock(&glob->lru_lock);
777 node = drm_mm_search_free(&man->manager,
778 mem->num_pages,
779 mem->page_alignment,
781 if (unlikely(!node)) {
782 spin_unlock(&glob->lru_lock);
783 break;
785 node = drm_mm_get_block_atomic(node,
786 mem->num_pages,
787 mem->
788 page_alignment);
789 spin_unlock(&glob->lru_lock);
790 } while (!node);
792 if (node)
793 break;
796 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
797 mem->mm_node = node;
798 mem->mem_type = mem_type;
799 mem->placement = cur_flags;
800 return 0;
803 if (!type_found)
804 return -EINVAL;
806 num_prios = bdev->driver->num_mem_busy_prio;
807 prios = bdev->driver->mem_busy_prio;
809 for (i = 0; i < num_prios; ++i) {
810 mem_type = prios[i];
811 man = &bdev->man[mem_type];
813 if (!man->has_type)
814 continue;
816 if (!ttm_bo_mt_compatible(man,
817 bo->type == ttm_bo_type_user,
818 mem_type,
819 proposed_placement, &cur_flags))
820 continue;
822 ret = ttm_bo_mem_force_space(bdev, mem, mem_type,
823 interruptible, no_wait);
825 if (ret == 0 && mem->mm_node) {
826 mem->placement = cur_flags;
827 return 0;
830 if (ret == -ERESTART)
831 has_eagain = true;
834 ret = (has_eagain) ? -ERESTART : -ENOMEM;
835 return ret;
837 EXPORT_SYMBOL(ttm_bo_mem_space);
839 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
841 int ret = 0;
843 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
844 return -EBUSY;
846 ret = wait_event_interruptible(bo->event_queue,
847 atomic_read(&bo->cpu_writers) == 0);
849 if (ret == -ERESTARTSYS)
850 ret = -ERESTART;
852 return ret;
855 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
856 uint32_t proposed_placement,
857 bool interruptible, bool no_wait)
859 struct ttm_bo_global *glob = bo->glob;
860 int ret = 0;
861 struct ttm_mem_reg mem;
863 BUG_ON(!atomic_read(&bo->reserved));
866 * FIXME: It's possible to pipeline buffer moves.
867 * Have the driver move function wait for idle when necessary,
868 * instead of doing it here.
871 spin_lock(&bo->lock);
872 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
873 spin_unlock(&bo->lock);
875 if (ret)
876 return ret;
878 mem.num_pages = bo->num_pages;
879 mem.size = mem.num_pages << PAGE_SHIFT;
880 mem.page_alignment = bo->mem.page_alignment;
883 * Determine where to move the buffer.
886 ret = ttm_bo_mem_space(bo, proposed_placement, &mem,
887 interruptible, no_wait);
888 if (ret)
889 goto out_unlock;
891 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait);
893 out_unlock:
894 if (ret && mem.mm_node) {
895 spin_lock(&glob->lru_lock);
896 drm_mm_put_block(mem.mm_node);
897 spin_unlock(&glob->lru_lock);
899 return ret;
902 static int ttm_bo_mem_compat(uint32_t proposed_placement,
903 struct ttm_mem_reg *mem)
905 if ((proposed_placement & mem->placement & TTM_PL_MASK_MEM) == 0)
906 return 0;
907 if ((proposed_placement & mem->placement & TTM_PL_MASK_CACHING) == 0)
908 return 0;
910 return 1;
913 int ttm_buffer_object_validate(struct ttm_buffer_object *bo,
914 uint32_t proposed_placement,
915 bool interruptible, bool no_wait)
917 int ret;
919 BUG_ON(!atomic_read(&bo->reserved));
920 bo->proposed_placement = proposed_placement;
922 TTM_DEBUG("Proposed placement 0x%08lx, Old flags 0x%08lx\n",
923 (unsigned long)proposed_placement,
924 (unsigned long)bo->mem.placement);
927 * Check whether we need to move buffer.
930 if (!ttm_bo_mem_compat(bo->proposed_placement, &bo->mem)) {
931 ret = ttm_bo_move_buffer(bo, bo->proposed_placement,
932 interruptible, no_wait);
933 if (ret) {
934 if (ret != -ERESTART)
935 printk(KERN_ERR TTM_PFX
936 "Failed moving buffer. "
937 "Proposed placement 0x%08x\n",
938 bo->proposed_placement);
939 if (ret == -ENOMEM)
940 printk(KERN_ERR TTM_PFX
941 "Out of aperture space or "
942 "DRM memory quota.\n");
943 return ret;
948 * We might need to add a TTM.
951 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
952 ret = ttm_bo_add_ttm(bo, true);
953 if (ret)
954 return ret;
957 * Validation has succeeded, move the access and other
958 * non-mapping-related flag bits from the proposed flags to
959 * the active flags
962 ttm_flag_masked(&bo->mem.placement, bo->proposed_placement,
963 ~TTM_PL_MASK_MEMTYPE);
965 return 0;
967 EXPORT_SYMBOL(ttm_buffer_object_validate);
970 ttm_bo_check_placement(struct ttm_buffer_object *bo,
971 uint32_t set_flags, uint32_t clr_flags)
973 uint32_t new_mask = set_flags | clr_flags;
975 if ((bo->type == ttm_bo_type_user) &&
976 (clr_flags & TTM_PL_FLAG_CACHED)) {
977 printk(KERN_ERR TTM_PFX
978 "User buffers require cache-coherent memory.\n");
979 return -EINVAL;
982 if (!capable(CAP_SYS_ADMIN)) {
983 if (new_mask & TTM_PL_FLAG_NO_EVICT) {
984 printk(KERN_ERR TTM_PFX "Need to be root to modify"
985 " NO_EVICT status.\n");
986 return -EINVAL;
989 if ((clr_flags & bo->mem.placement & TTM_PL_MASK_MEMTYPE) &&
990 (bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
991 printk(KERN_ERR TTM_PFX
992 "Incompatible memory specification"
993 " for NO_EVICT buffer.\n");
994 return -EINVAL;
997 return 0;
1000 int ttm_buffer_object_init(struct ttm_bo_device *bdev,
1001 struct ttm_buffer_object *bo,
1002 unsigned long size,
1003 enum ttm_bo_type type,
1004 uint32_t flags,
1005 uint32_t page_alignment,
1006 unsigned long buffer_start,
1007 bool interruptible,
1008 struct file *persistant_swap_storage,
1009 size_t acc_size,
1010 void (*destroy) (struct ttm_buffer_object *))
1012 int ret = 0;
1013 unsigned long num_pages;
1015 size += buffer_start & ~PAGE_MASK;
1016 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1017 if (num_pages == 0) {
1018 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1019 return -EINVAL;
1021 bo->destroy = destroy;
1023 spin_lock_init(&bo->lock);
1024 kref_init(&bo->kref);
1025 kref_init(&bo->list_kref);
1026 atomic_set(&bo->cpu_writers, 0);
1027 atomic_set(&bo->reserved, 1);
1028 init_waitqueue_head(&bo->event_queue);
1029 INIT_LIST_HEAD(&bo->lru);
1030 INIT_LIST_HEAD(&bo->ddestroy);
1031 INIT_LIST_HEAD(&bo->swap);
1032 bo->bdev = bdev;
1033 bo->glob = bdev->glob;
1034 bo->type = type;
1035 bo->num_pages = num_pages;
1036 bo->mem.mem_type = TTM_PL_SYSTEM;
1037 bo->mem.num_pages = bo->num_pages;
1038 bo->mem.mm_node = NULL;
1039 bo->mem.page_alignment = page_alignment;
1040 bo->buffer_start = buffer_start & PAGE_MASK;
1041 bo->priv_flags = 0;
1042 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1043 bo->seq_valid = false;
1044 bo->persistant_swap_storage = persistant_swap_storage;
1045 bo->acc_size = acc_size;
1046 atomic_inc(&bo->glob->bo_count);
1048 ret = ttm_bo_check_placement(bo, flags, 0ULL);
1049 if (unlikely(ret != 0))
1050 goto out_err;
1053 * If no caching attributes are set, accept any form of caching.
1056 if ((flags & TTM_PL_MASK_CACHING) == 0)
1057 flags |= TTM_PL_MASK_CACHING;
1060 * For ttm_bo_type_device buffers, allocate
1061 * address space from the device.
1064 if (bo->type == ttm_bo_type_device) {
1065 ret = ttm_bo_setup_vm(bo);
1066 if (ret)
1067 goto out_err;
1070 ret = ttm_buffer_object_validate(bo, flags, interruptible, false);
1071 if (ret)
1072 goto out_err;
1074 ttm_bo_unreserve(bo);
1075 return 0;
1077 out_err:
1078 ttm_bo_unreserve(bo);
1079 ttm_bo_unref(&bo);
1081 return ret;
1083 EXPORT_SYMBOL(ttm_buffer_object_init);
1085 static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
1086 unsigned long num_pages)
1088 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1089 PAGE_MASK;
1091 return glob->ttm_bo_size + 2 * page_array_size;
1094 int ttm_buffer_object_create(struct ttm_bo_device *bdev,
1095 unsigned long size,
1096 enum ttm_bo_type type,
1097 uint32_t flags,
1098 uint32_t page_alignment,
1099 unsigned long buffer_start,
1100 bool interruptible,
1101 struct file *persistant_swap_storage,
1102 struct ttm_buffer_object **p_bo)
1104 struct ttm_buffer_object *bo;
1105 int ret;
1106 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1108 size_t acc_size =
1109 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
1110 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1111 if (unlikely(ret != 0))
1112 return ret;
1114 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1116 if (unlikely(bo == NULL)) {
1117 ttm_mem_global_free(mem_glob, acc_size);
1118 return -ENOMEM;
1121 ret = ttm_buffer_object_init(bdev, bo, size, type, flags,
1122 page_alignment, buffer_start,
1123 interruptible,
1124 persistant_swap_storage, acc_size, NULL);
1125 if (likely(ret == 0))
1126 *p_bo = bo;
1128 return ret;
1131 static int ttm_bo_leave_list(struct ttm_buffer_object *bo,
1132 uint32_t mem_type, bool allow_errors)
1134 int ret;
1136 spin_lock(&bo->lock);
1137 ret = ttm_bo_wait(bo, false, false, false);
1138 spin_unlock(&bo->lock);
1140 if (ret && allow_errors)
1141 goto out;
1143 if (bo->mem.mem_type == mem_type)
1144 ret = ttm_bo_evict(bo, mem_type, false, false);
1146 if (ret) {
1147 if (allow_errors) {
1148 goto out;
1149 } else {
1150 ret = 0;
1151 printk(KERN_ERR TTM_PFX "Cleanup eviction failed\n");
1155 out:
1156 return ret;
1159 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1160 struct list_head *head,
1161 unsigned mem_type, bool allow_errors)
1163 struct ttm_bo_global *glob = bdev->glob;
1164 struct ttm_buffer_object *entry;
1165 int ret;
1166 int put_count;
1169 * Can't use standard list traversal since we're unlocking.
1172 spin_lock(&glob->lru_lock);
1174 while (!list_empty(head)) {
1175 entry = list_first_entry(head, struct ttm_buffer_object, lru);
1176 kref_get(&entry->list_kref);
1177 ret = ttm_bo_reserve_locked(entry, false, false, false, 0);
1178 put_count = ttm_bo_del_from_lru(entry);
1179 spin_unlock(&glob->lru_lock);
1180 while (put_count--)
1181 kref_put(&entry->list_kref, ttm_bo_ref_bug);
1182 BUG_ON(ret);
1183 ret = ttm_bo_leave_list(entry, mem_type, allow_errors);
1184 ttm_bo_unreserve(entry);
1185 kref_put(&entry->list_kref, ttm_bo_release_list);
1186 spin_lock(&glob->lru_lock);
1189 spin_unlock(&glob->lru_lock);
1191 return 0;
1194 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1196 struct ttm_bo_global *glob = bdev->glob;
1197 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1198 int ret = -EINVAL;
1200 if (mem_type >= TTM_NUM_MEM_TYPES) {
1201 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1202 return ret;
1205 if (!man->has_type) {
1206 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1207 "memory manager type %u\n", mem_type);
1208 return ret;
1211 man->use_type = false;
1212 man->has_type = false;
1214 ret = 0;
1215 if (mem_type > 0) {
1216 ttm_bo_force_list_clean(bdev, &man->lru, mem_type, false);
1218 spin_lock(&glob->lru_lock);
1219 if (drm_mm_clean(&man->manager))
1220 drm_mm_takedown(&man->manager);
1221 else
1222 ret = -EBUSY;
1224 spin_unlock(&glob->lru_lock);
1227 return ret;
1229 EXPORT_SYMBOL(ttm_bo_clean_mm);
1231 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1233 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1235 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1236 printk(KERN_ERR TTM_PFX
1237 "Illegal memory manager memory type %u.\n",
1238 mem_type);
1239 return -EINVAL;
1242 if (!man->has_type) {
1243 printk(KERN_ERR TTM_PFX
1244 "Memory type %u has not been initialized.\n",
1245 mem_type);
1246 return 0;
1249 return ttm_bo_force_list_clean(bdev, &man->lru, mem_type, true);
1251 EXPORT_SYMBOL(ttm_bo_evict_mm);
1253 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1254 unsigned long p_offset, unsigned long p_size)
1256 int ret = -EINVAL;
1257 struct ttm_mem_type_manager *man;
1259 if (type >= TTM_NUM_MEM_TYPES) {
1260 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1261 return ret;
1264 man = &bdev->man[type];
1265 if (man->has_type) {
1266 printk(KERN_ERR TTM_PFX
1267 "Memory manager already initialized for type %d\n",
1268 type);
1269 return ret;
1272 ret = bdev->driver->init_mem_type(bdev, type, man);
1273 if (ret)
1274 return ret;
1276 ret = 0;
1277 if (type != TTM_PL_SYSTEM) {
1278 if (!p_size) {
1279 printk(KERN_ERR TTM_PFX
1280 "Zero size memory manager type %d\n",
1281 type);
1282 return ret;
1284 ret = drm_mm_init(&man->manager, p_offset, p_size);
1285 if (ret)
1286 return ret;
1288 man->has_type = true;
1289 man->use_type = true;
1290 man->size = p_size;
1292 INIT_LIST_HEAD(&man->lru);
1294 return 0;
1296 EXPORT_SYMBOL(ttm_bo_init_mm);
1298 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1300 struct ttm_bo_global *glob =
1301 container_of(kobj, struct ttm_bo_global, kobj);
1303 printk(KERN_INFO TTM_PFX "Freeing bo global.\n");
1304 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1305 __free_page(glob->dummy_read_page);
1306 kfree(glob);
1309 void ttm_bo_global_release(struct ttm_global_reference *ref)
1311 struct ttm_bo_global *glob = ref->object;
1313 kobject_del(&glob->kobj);
1314 kobject_put(&glob->kobj);
1316 EXPORT_SYMBOL(ttm_bo_global_release);
1318 int ttm_bo_global_init(struct ttm_global_reference *ref)
1320 struct ttm_bo_global_ref *bo_ref =
1321 container_of(ref, struct ttm_bo_global_ref, ref);
1322 struct ttm_bo_global *glob = ref->object;
1323 int ret;
1325 mutex_init(&glob->device_list_mutex);
1326 spin_lock_init(&glob->lru_lock);
1327 glob->mem_glob = bo_ref->mem_glob;
1328 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1330 if (unlikely(glob->dummy_read_page == NULL)) {
1331 ret = -ENOMEM;
1332 goto out_no_drp;
1335 INIT_LIST_HEAD(&glob->swap_lru);
1336 INIT_LIST_HEAD(&glob->device_list);
1338 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1339 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1340 if (unlikely(ret != 0)) {
1341 printk(KERN_ERR TTM_PFX
1342 "Could not register buffer object swapout.\n");
1343 goto out_no_shrink;
1346 glob->ttm_bo_extra_size =
1347 ttm_round_pot(sizeof(struct ttm_tt)) +
1348 ttm_round_pot(sizeof(struct ttm_backend));
1350 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1351 ttm_round_pot(sizeof(struct ttm_buffer_object));
1353 atomic_set(&glob->bo_count, 0);
1355 kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type);
1356 ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects");
1357 if (unlikely(ret != 0))
1358 kobject_put(&glob->kobj);
1359 return ret;
1360 out_no_shrink:
1361 __free_page(glob->dummy_read_page);
1362 out_no_drp:
1363 kfree(glob);
1364 return ret;
1366 EXPORT_SYMBOL(ttm_bo_global_init);
1369 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1371 int ret = 0;
1372 unsigned i = TTM_NUM_MEM_TYPES;
1373 struct ttm_mem_type_manager *man;
1374 struct ttm_bo_global *glob = bdev->glob;
1376 while (i--) {
1377 man = &bdev->man[i];
1378 if (man->has_type) {
1379 man->use_type = false;
1380 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1381 ret = -EBUSY;
1382 printk(KERN_ERR TTM_PFX
1383 "DRM memory manager type %d "
1384 "is not clean.\n", i);
1386 man->has_type = false;
1390 mutex_lock(&glob->device_list_mutex);
1391 list_del(&bdev->device_list);
1392 mutex_unlock(&glob->device_list_mutex);
1394 if (!cancel_delayed_work(&bdev->wq))
1395 flush_scheduled_work();
1397 while (ttm_bo_delayed_delete(bdev, true))
1400 spin_lock(&glob->lru_lock);
1401 if (list_empty(&bdev->ddestroy))
1402 TTM_DEBUG("Delayed destroy list was clean\n");
1404 if (list_empty(&bdev->man[0].lru))
1405 TTM_DEBUG("Swap list was clean\n");
1406 spin_unlock(&glob->lru_lock);
1408 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1409 write_lock(&bdev->vm_lock);
1410 drm_mm_takedown(&bdev->addr_space_mm);
1411 write_unlock(&bdev->vm_lock);
1413 return ret;
1415 EXPORT_SYMBOL(ttm_bo_device_release);
1417 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1418 struct ttm_bo_global *glob,
1419 struct ttm_bo_driver *driver,
1420 uint64_t file_page_offset)
1422 int ret = -EINVAL;
1424 rwlock_init(&bdev->vm_lock);
1425 spin_lock_init(&glob->lru_lock);
1427 bdev->driver = driver;
1429 memset(bdev->man, 0, sizeof(bdev->man));
1432 * Initialize the system memory buffer type.
1433 * Other types need to be driver / IOCTL initialized.
1435 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0, 0);
1436 if (unlikely(ret != 0))
1437 goto out_no_sys;
1439 bdev->addr_space_rb = RB_ROOT;
1440 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1441 if (unlikely(ret != 0))
1442 goto out_no_addr_mm;
1444 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1445 bdev->nice_mode = true;
1446 INIT_LIST_HEAD(&bdev->ddestroy);
1447 bdev->dev_mapping = NULL;
1448 bdev->glob = glob;
1450 mutex_lock(&glob->device_list_mutex);
1451 list_add_tail(&bdev->device_list, &glob->device_list);
1452 mutex_unlock(&glob->device_list_mutex);
1454 return 0;
1455 out_no_addr_mm:
1456 ttm_bo_clean_mm(bdev, 0);
1457 out_no_sys:
1458 return ret;
1460 EXPORT_SYMBOL(ttm_bo_device_init);
1463 * buffer object vm functions.
1466 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1468 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1470 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1471 if (mem->mem_type == TTM_PL_SYSTEM)
1472 return false;
1474 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1475 return false;
1477 if (mem->placement & TTM_PL_FLAG_CACHED)
1478 return false;
1480 return true;
1483 int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1484 struct ttm_mem_reg *mem,
1485 unsigned long *bus_base,
1486 unsigned long *bus_offset, unsigned long *bus_size)
1488 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1490 *bus_size = 0;
1491 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1492 return -EINVAL;
1494 if (ttm_mem_reg_is_pci(bdev, mem)) {
1495 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1496 *bus_size = mem->num_pages << PAGE_SHIFT;
1497 *bus_base = man->io_offset;
1500 return 0;
1503 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1505 struct ttm_bo_device *bdev = bo->bdev;
1506 loff_t offset = (loff_t) bo->addr_space_offset;
1507 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1509 if (!bdev->dev_mapping)
1510 return;
1512 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1515 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1517 struct ttm_bo_device *bdev = bo->bdev;
1518 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1519 struct rb_node *parent = NULL;
1520 struct ttm_buffer_object *cur_bo;
1521 unsigned long offset = bo->vm_node->start;
1522 unsigned long cur_offset;
1524 while (*cur) {
1525 parent = *cur;
1526 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1527 cur_offset = cur_bo->vm_node->start;
1528 if (offset < cur_offset)
1529 cur = &parent->rb_left;
1530 else if (offset > cur_offset)
1531 cur = &parent->rb_right;
1532 else
1533 BUG();
1536 rb_link_node(&bo->vm_rb, parent, cur);
1537 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1541 * ttm_bo_setup_vm:
1543 * @bo: the buffer to allocate address space for
1545 * Allocate address space in the drm device so that applications
1546 * can mmap the buffer and access the contents. This only
1547 * applies to ttm_bo_type_device objects as others are not
1548 * placed in the drm device address space.
1551 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1553 struct ttm_bo_device *bdev = bo->bdev;
1554 int ret;
1556 retry_pre_get:
1557 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1558 if (unlikely(ret != 0))
1559 return ret;
1561 write_lock(&bdev->vm_lock);
1562 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1563 bo->mem.num_pages, 0, 0);
1565 if (unlikely(bo->vm_node == NULL)) {
1566 ret = -ENOMEM;
1567 goto out_unlock;
1570 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1571 bo->mem.num_pages, 0);
1573 if (unlikely(bo->vm_node == NULL)) {
1574 write_unlock(&bdev->vm_lock);
1575 goto retry_pre_get;
1578 ttm_bo_vm_insert_rb(bo);
1579 write_unlock(&bdev->vm_lock);
1580 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1582 return 0;
1583 out_unlock:
1584 write_unlock(&bdev->vm_lock);
1585 return ret;
1588 int ttm_bo_wait(struct ttm_buffer_object *bo,
1589 bool lazy, bool interruptible, bool no_wait)
1591 struct ttm_bo_driver *driver = bo->bdev->driver;
1592 void *sync_obj;
1593 void *sync_obj_arg;
1594 int ret = 0;
1596 if (likely(bo->sync_obj == NULL))
1597 return 0;
1599 while (bo->sync_obj) {
1601 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1602 void *tmp_obj = bo->sync_obj;
1603 bo->sync_obj = NULL;
1604 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1605 spin_unlock(&bo->lock);
1606 driver->sync_obj_unref(&tmp_obj);
1607 spin_lock(&bo->lock);
1608 continue;
1611 if (no_wait)
1612 return -EBUSY;
1614 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1615 sync_obj_arg = bo->sync_obj_arg;
1616 spin_unlock(&bo->lock);
1617 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1618 lazy, interruptible);
1619 if (unlikely(ret != 0)) {
1620 driver->sync_obj_unref(&sync_obj);
1621 spin_lock(&bo->lock);
1622 return ret;
1624 spin_lock(&bo->lock);
1625 if (likely(bo->sync_obj == sync_obj &&
1626 bo->sync_obj_arg == sync_obj_arg)) {
1627 void *tmp_obj = bo->sync_obj;
1628 bo->sync_obj = NULL;
1629 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1630 &bo->priv_flags);
1631 spin_unlock(&bo->lock);
1632 driver->sync_obj_unref(&sync_obj);
1633 driver->sync_obj_unref(&tmp_obj);
1634 spin_lock(&bo->lock);
1637 return 0;
1639 EXPORT_SYMBOL(ttm_bo_wait);
1641 void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1643 atomic_set(&bo->reserved, 0);
1644 wake_up_all(&bo->event_queue);
1647 int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1648 bool no_wait)
1650 int ret;
1652 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1653 if (no_wait)
1654 return -EBUSY;
1655 else if (interruptible) {
1656 ret = wait_event_interruptible
1657 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1658 if (unlikely(ret != 0))
1659 return -ERESTART;
1660 } else {
1661 wait_event(bo->event_queue,
1662 atomic_read(&bo->reserved) == 0);
1665 return 0;
1668 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1670 int ret = 0;
1673 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1674 * makes sure the lru lists are updated.
1677 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1678 if (unlikely(ret != 0))
1679 return ret;
1680 spin_lock(&bo->lock);
1681 ret = ttm_bo_wait(bo, false, true, no_wait);
1682 spin_unlock(&bo->lock);
1683 if (likely(ret == 0))
1684 atomic_inc(&bo->cpu_writers);
1685 ttm_bo_unreserve(bo);
1686 return ret;
1689 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1691 if (atomic_dec_and_test(&bo->cpu_writers))
1692 wake_up_all(&bo->event_queue);
1696 * A buffer object shrink method that tries to swap out the first
1697 * buffer object on the bo_global::swap_lru list.
1700 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1702 struct ttm_bo_global *glob =
1703 container_of(shrink, struct ttm_bo_global, shrink);
1704 struct ttm_buffer_object *bo;
1705 int ret = -EBUSY;
1706 int put_count;
1707 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1709 spin_lock(&glob->lru_lock);
1710 while (ret == -EBUSY) {
1711 if (unlikely(list_empty(&glob->swap_lru))) {
1712 spin_unlock(&glob->lru_lock);
1713 return -EBUSY;
1716 bo = list_first_entry(&glob->swap_lru,
1717 struct ttm_buffer_object, swap);
1718 kref_get(&bo->list_kref);
1721 * Reserve buffer. Since we unlock while sleeping, we need
1722 * to re-check that nobody removed us from the swap-list while
1723 * we slept.
1726 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1727 if (unlikely(ret == -EBUSY)) {
1728 spin_unlock(&glob->lru_lock);
1729 ttm_bo_wait_unreserved(bo, false);
1730 kref_put(&bo->list_kref, ttm_bo_release_list);
1731 spin_lock(&glob->lru_lock);
1735 BUG_ON(ret != 0);
1736 put_count = ttm_bo_del_from_lru(bo);
1737 spin_unlock(&glob->lru_lock);
1739 while (put_count--)
1740 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1743 * Wait for GPU, then move to system cached.
1746 spin_lock(&bo->lock);
1747 ret = ttm_bo_wait(bo, false, false, false);
1748 spin_unlock(&bo->lock);
1750 if (unlikely(ret != 0))
1751 goto out;
1753 if ((bo->mem.placement & swap_placement) != swap_placement) {
1754 struct ttm_mem_reg evict_mem;
1756 evict_mem = bo->mem;
1757 evict_mem.mm_node = NULL;
1758 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1759 evict_mem.mem_type = TTM_PL_SYSTEM;
1761 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1762 false, false);
1763 if (unlikely(ret != 0))
1764 goto out;
1767 ttm_bo_unmap_virtual(bo);
1770 * Swap out. Buffer will be swapped in again as soon as
1771 * anyone tries to access a ttm page.
1774 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1775 out:
1779 * Unreserve without putting on LRU to avoid swapping out an
1780 * already swapped buffer.
1783 atomic_set(&bo->reserved, 0);
1784 wake_up_all(&bo->event_queue);
1785 kref_put(&bo->list_kref, ttm_bo_release_list);
1786 return ret;
1789 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1791 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)