drm/ttm: Remove mm init error printouts and checks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / ttm / ttm_bo.c
blobf561eead057d40bb1bea4fa1e0b4277e000aa0e7
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 int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
47 static void ttm_bo_global_kobj_release(struct kobject *kobj);
49 static struct attribute ttm_bo_count = {
50 .name = "bo_count",
51 .mode = S_IRUGO
54 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
56 int i;
58 for (i = 0; i <= TTM_PL_PRIV5; i++)
59 if (flags & (1 << i)) {
60 *mem_type = i;
61 return 0;
63 return -EINVAL;
66 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
68 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
70 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
71 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
72 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
73 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
74 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
75 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
76 man->available_caching);
77 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
78 man->default_caching);
79 if (mem_type != TTM_PL_SYSTEM)
80 (*man->func->debug)(man, TTM_PFX);
83 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
84 struct ttm_placement *placement)
86 int i, ret, mem_type;
88 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
89 bo, bo->mem.num_pages, bo->mem.size >> 10,
90 bo->mem.size >> 20);
91 for (i = 0; i < placement->num_placement; i++) {
92 ret = ttm_mem_type_from_flags(placement->placement[i],
93 &mem_type);
94 if (ret)
95 return;
96 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
97 i, placement->placement[i], mem_type);
98 ttm_mem_type_debug(bo->bdev, mem_type);
102 static ssize_t ttm_bo_global_show(struct kobject *kobj,
103 struct attribute *attr,
104 char *buffer)
106 struct ttm_bo_global *glob =
107 container_of(kobj, struct ttm_bo_global, kobj);
109 return snprintf(buffer, PAGE_SIZE, "%lu\n",
110 (unsigned long) atomic_read(&glob->bo_count));
113 static struct attribute *ttm_bo_global_attrs[] = {
114 &ttm_bo_count,
115 NULL
118 static const struct sysfs_ops ttm_bo_global_ops = {
119 .show = &ttm_bo_global_show
122 static struct kobj_type ttm_bo_glob_kobj_type = {
123 .release = &ttm_bo_global_kobj_release,
124 .sysfs_ops = &ttm_bo_global_ops,
125 .default_attrs = ttm_bo_global_attrs
129 static inline uint32_t ttm_bo_type_flags(unsigned type)
131 return 1 << (type);
134 static void ttm_bo_release_list(struct kref *list_kref)
136 struct ttm_buffer_object *bo =
137 container_of(list_kref, struct ttm_buffer_object, list_kref);
138 struct ttm_bo_device *bdev = bo->bdev;
140 BUG_ON(atomic_read(&bo->list_kref.refcount));
141 BUG_ON(atomic_read(&bo->kref.refcount));
142 BUG_ON(atomic_read(&bo->cpu_writers));
143 BUG_ON(bo->sync_obj != NULL);
144 BUG_ON(bo->mem.mm_node != NULL);
145 BUG_ON(!list_empty(&bo->lru));
146 BUG_ON(!list_empty(&bo->ddestroy));
148 if (bo->ttm)
149 ttm_tt_destroy(bo->ttm);
150 atomic_dec(&bo->glob->bo_count);
151 if (bo->destroy)
152 bo->destroy(bo);
153 else {
154 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
155 kfree(bo);
159 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
161 if (interruptible) {
162 return wait_event_interruptible(bo->event_queue,
163 atomic_read(&bo->reserved) == 0);
164 } else {
165 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
166 return 0;
169 EXPORT_SYMBOL(ttm_bo_wait_unreserved);
171 static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
173 struct ttm_bo_device *bdev = bo->bdev;
174 struct ttm_mem_type_manager *man;
176 BUG_ON(!atomic_read(&bo->reserved));
178 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
180 BUG_ON(!list_empty(&bo->lru));
182 man = &bdev->man[bo->mem.mem_type];
183 list_add_tail(&bo->lru, &man->lru);
184 kref_get(&bo->list_kref);
186 if (bo->ttm != NULL) {
187 list_add_tail(&bo->swap, &bo->glob->swap_lru);
188 kref_get(&bo->list_kref);
194 * Call with the lru_lock held.
197 static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
199 int put_count = 0;
201 if (!list_empty(&bo->swap)) {
202 list_del_init(&bo->swap);
203 ++put_count;
205 if (!list_empty(&bo->lru)) {
206 list_del_init(&bo->lru);
207 ++put_count;
211 * TODO: Add a driver hook to delete from
212 * driver-specific LRU's here.
215 return put_count;
218 int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
219 bool interruptible,
220 bool no_wait, bool use_sequence, uint32_t sequence)
222 struct ttm_bo_global *glob = bo->glob;
223 int ret;
225 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
226 if (use_sequence && bo->seq_valid &&
227 (sequence - bo->val_seq < (1 << 31))) {
228 return -EAGAIN;
231 if (no_wait)
232 return -EBUSY;
234 spin_unlock(&glob->lru_lock);
235 ret = ttm_bo_wait_unreserved(bo, interruptible);
236 spin_lock(&glob->lru_lock);
238 if (unlikely(ret))
239 return ret;
242 if (use_sequence) {
243 bo->val_seq = sequence;
244 bo->seq_valid = true;
245 } else {
246 bo->seq_valid = false;
249 return 0;
251 EXPORT_SYMBOL(ttm_bo_reserve);
253 static void ttm_bo_ref_bug(struct kref *list_kref)
255 BUG();
258 int ttm_bo_reserve(struct ttm_buffer_object *bo,
259 bool interruptible,
260 bool no_wait, bool use_sequence, uint32_t sequence)
262 struct ttm_bo_global *glob = bo->glob;
263 int put_count = 0;
264 int ret;
266 spin_lock(&glob->lru_lock);
267 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
268 sequence);
269 if (likely(ret == 0))
270 put_count = ttm_bo_del_from_lru(bo);
271 spin_unlock(&glob->lru_lock);
273 while (put_count--)
274 kref_put(&bo->list_kref, ttm_bo_ref_bug);
276 return ret;
279 void ttm_bo_unreserve(struct ttm_buffer_object *bo)
281 struct ttm_bo_global *glob = bo->glob;
283 spin_lock(&glob->lru_lock);
284 ttm_bo_add_to_lru(bo);
285 atomic_set(&bo->reserved, 0);
286 wake_up_all(&bo->event_queue);
287 spin_unlock(&glob->lru_lock);
289 EXPORT_SYMBOL(ttm_bo_unreserve);
292 * Call bo->mutex locked.
294 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
296 struct ttm_bo_device *bdev = bo->bdev;
297 struct ttm_bo_global *glob = bo->glob;
298 int ret = 0;
299 uint32_t page_flags = 0;
301 TTM_ASSERT_LOCKED(&bo->mutex);
302 bo->ttm = NULL;
304 if (bdev->need_dma32)
305 page_flags |= TTM_PAGE_FLAG_DMA32;
307 switch (bo->type) {
308 case ttm_bo_type_device:
309 if (zero_alloc)
310 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
311 case ttm_bo_type_kernel:
312 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
313 page_flags, glob->dummy_read_page);
314 if (unlikely(bo->ttm == NULL))
315 ret = -ENOMEM;
316 break;
317 case ttm_bo_type_user:
318 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
319 page_flags | TTM_PAGE_FLAG_USER,
320 glob->dummy_read_page);
321 if (unlikely(bo->ttm == NULL)) {
322 ret = -ENOMEM;
323 break;
326 ret = ttm_tt_set_user(bo->ttm, current,
327 bo->buffer_start, bo->num_pages);
328 if (unlikely(ret != 0))
329 ttm_tt_destroy(bo->ttm);
330 break;
331 default:
332 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
333 ret = -EINVAL;
334 break;
337 return ret;
340 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
341 struct ttm_mem_reg *mem,
342 bool evict, bool interruptible,
343 bool no_wait_reserve, bool no_wait_gpu)
345 struct ttm_bo_device *bdev = bo->bdev;
346 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
347 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
348 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
349 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
350 int ret = 0;
352 if (old_is_pci || new_is_pci ||
353 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
354 ttm_bo_unmap_virtual(bo);
357 * Create and bind a ttm if required.
360 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
361 ret = ttm_bo_add_ttm(bo, false);
362 if (ret)
363 goto out_err;
365 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
366 if (ret)
367 goto out_err;
369 if (mem->mem_type != TTM_PL_SYSTEM) {
370 ret = ttm_tt_bind(bo->ttm, mem);
371 if (ret)
372 goto out_err;
375 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
376 bo->mem = *mem;
377 mem->mm_node = NULL;
378 goto moved;
383 if (bdev->driver->move_notify)
384 bdev->driver->move_notify(bo, mem);
386 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
387 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
388 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
389 else if (bdev->driver->move)
390 ret = bdev->driver->move(bo, evict, interruptible,
391 no_wait_reserve, no_wait_gpu, mem);
392 else
393 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
395 if (ret)
396 goto out_err;
398 moved:
399 if (bo->evicted) {
400 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
401 if (ret)
402 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
403 bo->evicted = false;
406 if (bo->mem.mm_node) {
407 spin_lock(&bo->lock);
408 bo->offset = (bo->mem.start << PAGE_SHIFT) +
409 bdev->man[bo->mem.mem_type].gpu_offset;
410 bo->cur_placement = bo->mem.placement;
411 spin_unlock(&bo->lock);
412 } else
413 bo->offset = 0;
415 return 0;
417 out_err:
418 new_man = &bdev->man[bo->mem.mem_type];
419 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
420 ttm_tt_unbind(bo->ttm);
421 ttm_tt_destroy(bo->ttm);
422 bo->ttm = NULL;
425 return ret;
429 * Call bo::reserved.
430 * Will release GPU memory type usage on destruction.
431 * This is the place to put in driver specific hooks to release
432 * driver private resources.
433 * Will release the bo::reserved lock.
436 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
438 if (bo->ttm) {
439 ttm_tt_unbind(bo->ttm);
440 ttm_tt_destroy(bo->ttm);
441 bo->ttm = NULL;
444 ttm_bo_mem_put(bo, &bo->mem);
446 atomic_set(&bo->reserved, 0);
447 wake_up_all(&bo->event_queue);
450 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
452 struct ttm_bo_device *bdev = bo->bdev;
453 struct ttm_bo_global *glob = bo->glob;
454 struct ttm_bo_driver *driver;
455 void *sync_obj;
456 void *sync_obj_arg;
457 int put_count;
458 int ret;
460 spin_lock(&bo->lock);
461 (void) ttm_bo_wait(bo, false, false, true);
462 if (!bo->sync_obj) {
464 spin_lock(&glob->lru_lock);
467 * Lock inversion between bo::reserve and bo::lock here,
468 * but that's OK, since we're only trylocking.
471 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
473 if (unlikely(ret == -EBUSY))
474 goto queue;
476 spin_unlock(&bo->lock);
477 put_count = ttm_bo_del_from_lru(bo);
479 spin_unlock(&glob->lru_lock);
480 ttm_bo_cleanup_memtype_use(bo);
482 while (put_count--)
483 kref_put(&bo->list_kref, ttm_bo_ref_bug);
485 return;
486 } else {
487 spin_lock(&glob->lru_lock);
489 queue:
490 sync_obj = bo->sync_obj;
491 sync_obj_arg = bo->sync_obj_arg;
492 driver = bdev->driver;
494 kref_get(&bo->list_kref);
495 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
496 spin_unlock(&glob->lru_lock);
497 spin_unlock(&bo->lock);
499 if (sync_obj)
500 driver->sync_obj_flush(sync_obj, sync_obj_arg);
501 schedule_delayed_work(&bdev->wq,
502 ((HZ / 100) < 1) ? 1 : HZ / 100);
506 * function ttm_bo_cleanup_refs
507 * If bo idle, remove from delayed- and lru lists, and unref.
508 * If not idle, do nothing.
510 * @interruptible Any sleeps should occur interruptibly.
511 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
512 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
515 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
516 bool interruptible,
517 bool no_wait_reserve,
518 bool no_wait_gpu)
520 struct ttm_bo_global *glob = bo->glob;
521 int put_count;
522 int ret = 0;
524 retry:
525 spin_lock(&bo->lock);
526 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
527 spin_unlock(&bo->lock);
529 if (unlikely(ret != 0))
530 return ret;
532 spin_lock(&glob->lru_lock);
533 ret = ttm_bo_reserve_locked(bo, interruptible,
534 no_wait_reserve, false, 0);
536 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
537 spin_unlock(&glob->lru_lock);
538 return ret;
542 * We can re-check for sync object without taking
543 * the bo::lock since setting the sync object requires
544 * also bo::reserved. A busy object at this point may
545 * be caused by another thread recently starting an accelerated
546 * eviction.
549 if (unlikely(bo->sync_obj)) {
550 atomic_set(&bo->reserved, 0);
551 wake_up_all(&bo->event_queue);
552 spin_unlock(&glob->lru_lock);
553 goto retry;
556 put_count = ttm_bo_del_from_lru(bo);
557 list_del_init(&bo->ddestroy);
558 ++put_count;
560 spin_unlock(&glob->lru_lock);
561 ttm_bo_cleanup_memtype_use(bo);
563 while (put_count--)
564 kref_put(&bo->list_kref, ttm_bo_ref_bug);
566 return 0;
570 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
571 * encountered buffers.
574 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
576 struct ttm_bo_global *glob = bdev->glob;
577 struct ttm_buffer_object *entry = NULL;
578 int ret = 0;
580 spin_lock(&glob->lru_lock);
581 if (list_empty(&bdev->ddestroy))
582 goto out_unlock;
584 entry = list_first_entry(&bdev->ddestroy,
585 struct ttm_buffer_object, ddestroy);
586 kref_get(&entry->list_kref);
588 for (;;) {
589 struct ttm_buffer_object *nentry = NULL;
591 if (entry->ddestroy.next != &bdev->ddestroy) {
592 nentry = list_first_entry(&entry->ddestroy,
593 struct ttm_buffer_object, ddestroy);
594 kref_get(&nentry->list_kref);
597 spin_unlock(&glob->lru_lock);
598 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
599 !remove_all);
600 kref_put(&entry->list_kref, ttm_bo_release_list);
601 entry = nentry;
603 if (ret || !entry)
604 goto out;
606 spin_lock(&glob->lru_lock);
607 if (list_empty(&entry->ddestroy))
608 break;
611 out_unlock:
612 spin_unlock(&glob->lru_lock);
613 out:
614 if (entry)
615 kref_put(&entry->list_kref, ttm_bo_release_list);
616 return ret;
619 static void ttm_bo_delayed_workqueue(struct work_struct *work)
621 struct ttm_bo_device *bdev =
622 container_of(work, struct ttm_bo_device, wq.work);
624 if (ttm_bo_delayed_delete(bdev, false)) {
625 schedule_delayed_work(&bdev->wq,
626 ((HZ / 100) < 1) ? 1 : HZ / 100);
630 static void ttm_bo_release(struct kref *kref)
632 struct ttm_buffer_object *bo =
633 container_of(kref, struct ttm_buffer_object, kref);
634 struct ttm_bo_device *bdev = bo->bdev;
636 if (likely(bo->vm_node != NULL)) {
637 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
638 drm_mm_put_block(bo->vm_node);
639 bo->vm_node = NULL;
641 write_unlock(&bdev->vm_lock);
642 ttm_bo_cleanup_refs_or_queue(bo);
643 kref_put(&bo->list_kref, ttm_bo_release_list);
644 write_lock(&bdev->vm_lock);
647 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
649 struct ttm_buffer_object *bo = *p_bo;
650 struct ttm_bo_device *bdev = bo->bdev;
652 *p_bo = NULL;
653 write_lock(&bdev->vm_lock);
654 kref_put(&bo->kref, ttm_bo_release);
655 write_unlock(&bdev->vm_lock);
657 EXPORT_SYMBOL(ttm_bo_unref);
659 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
661 return cancel_delayed_work_sync(&bdev->wq);
663 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
665 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
667 if (resched)
668 schedule_delayed_work(&bdev->wq,
669 ((HZ / 100) < 1) ? 1 : HZ / 100);
671 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
673 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
674 bool no_wait_reserve, bool no_wait_gpu)
676 struct ttm_bo_device *bdev = bo->bdev;
677 struct ttm_mem_reg evict_mem;
678 struct ttm_placement placement;
679 int ret = 0;
681 spin_lock(&bo->lock);
682 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
683 spin_unlock(&bo->lock);
685 if (unlikely(ret != 0)) {
686 if (ret != -ERESTARTSYS) {
687 printk(KERN_ERR TTM_PFX
688 "Failed to expire sync object before "
689 "buffer eviction.\n");
691 goto out;
694 BUG_ON(!atomic_read(&bo->reserved));
696 evict_mem = bo->mem;
697 evict_mem.mm_node = NULL;
698 evict_mem.bus.io_reserved = false;
700 placement.fpfn = 0;
701 placement.lpfn = 0;
702 placement.num_placement = 0;
703 placement.num_busy_placement = 0;
704 bdev->driver->evict_flags(bo, &placement);
705 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
706 no_wait_reserve, no_wait_gpu);
707 if (ret) {
708 if (ret != -ERESTARTSYS) {
709 printk(KERN_ERR TTM_PFX
710 "Failed to find memory space for "
711 "buffer 0x%p eviction.\n", bo);
712 ttm_bo_mem_space_debug(bo, &placement);
714 goto out;
717 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
718 no_wait_reserve, no_wait_gpu);
719 if (ret) {
720 if (ret != -ERESTARTSYS)
721 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
722 ttm_bo_mem_put(bo, &evict_mem);
723 goto out;
725 bo->evicted = true;
726 out:
727 return ret;
730 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
731 uint32_t mem_type,
732 bool interruptible, bool no_wait_reserve,
733 bool no_wait_gpu)
735 struct ttm_bo_global *glob = bdev->glob;
736 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
737 struct ttm_buffer_object *bo;
738 int ret, put_count = 0;
740 retry:
741 spin_lock(&glob->lru_lock);
742 if (list_empty(&man->lru)) {
743 spin_unlock(&glob->lru_lock);
744 return -EBUSY;
747 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
748 kref_get(&bo->list_kref);
750 if (!list_empty(&bo->ddestroy)) {
751 spin_unlock(&glob->lru_lock);
752 ret = ttm_bo_cleanup_refs(bo, interruptible,
753 no_wait_reserve, no_wait_gpu);
754 kref_put(&bo->list_kref, ttm_bo_release_list);
756 if (likely(ret == 0 || ret == -ERESTARTSYS))
757 return ret;
759 goto retry;
762 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
764 if (unlikely(ret == -EBUSY)) {
765 spin_unlock(&glob->lru_lock);
766 if (likely(!no_wait_gpu))
767 ret = ttm_bo_wait_unreserved(bo, interruptible);
769 kref_put(&bo->list_kref, ttm_bo_release_list);
772 * We *need* to retry after releasing the lru lock.
775 if (unlikely(ret != 0))
776 return ret;
777 goto retry;
780 put_count = ttm_bo_del_from_lru(bo);
781 spin_unlock(&glob->lru_lock);
783 BUG_ON(ret != 0);
785 while (put_count--)
786 kref_put(&bo->list_kref, ttm_bo_ref_bug);
788 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
789 ttm_bo_unreserve(bo);
791 kref_put(&bo->list_kref, ttm_bo_release_list);
792 return ret;
795 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
797 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
799 if (mem->mm_node)
800 (*man->func->put_node)(man, mem);
802 EXPORT_SYMBOL(ttm_bo_mem_put);
805 * Repeatedly evict memory from the LRU for @mem_type until we create enough
806 * space, or we've evicted everything and there isn't enough space.
808 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
809 uint32_t mem_type,
810 struct ttm_placement *placement,
811 struct ttm_mem_reg *mem,
812 bool interruptible,
813 bool no_wait_reserve,
814 bool no_wait_gpu)
816 struct ttm_bo_device *bdev = bo->bdev;
817 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
818 int ret;
820 do {
821 ret = (*man->func->get_node)(man, bo, placement, mem);
822 if (unlikely(ret != 0))
823 return ret;
824 if (mem->mm_node)
825 break;
826 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
827 no_wait_reserve, no_wait_gpu);
828 if (unlikely(ret != 0))
829 return ret;
830 } while (1);
831 if (mem->mm_node == NULL)
832 return -ENOMEM;
833 mem->mem_type = mem_type;
834 return 0;
837 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
838 uint32_t cur_placement,
839 uint32_t proposed_placement)
841 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
842 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
845 * Keep current caching if possible.
848 if ((cur_placement & caching) != 0)
849 result |= (cur_placement & caching);
850 else if ((man->default_caching & caching) != 0)
851 result |= man->default_caching;
852 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
853 result |= TTM_PL_FLAG_CACHED;
854 else if ((TTM_PL_FLAG_WC & caching) != 0)
855 result |= TTM_PL_FLAG_WC;
856 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
857 result |= TTM_PL_FLAG_UNCACHED;
859 return result;
862 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
863 bool disallow_fixed,
864 uint32_t mem_type,
865 uint32_t proposed_placement,
866 uint32_t *masked_placement)
868 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
870 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
871 return false;
873 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
874 return false;
876 if ((proposed_placement & man->available_caching) == 0)
877 return false;
879 cur_flags |= (proposed_placement & man->available_caching);
881 *masked_placement = cur_flags;
882 return true;
886 * Creates space for memory region @mem according to its type.
888 * This function first searches for free space in compatible memory types in
889 * the priority order defined by the driver. If free space isn't found, then
890 * ttm_bo_mem_force_space is attempted in priority order to evict and find
891 * space.
893 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
894 struct ttm_placement *placement,
895 struct ttm_mem_reg *mem,
896 bool interruptible, bool no_wait_reserve,
897 bool no_wait_gpu)
899 struct ttm_bo_device *bdev = bo->bdev;
900 struct ttm_mem_type_manager *man;
901 uint32_t mem_type = TTM_PL_SYSTEM;
902 uint32_t cur_flags = 0;
903 bool type_found = false;
904 bool type_ok = false;
905 bool has_erestartsys = false;
906 int i, ret;
908 mem->mm_node = NULL;
909 for (i = 0; i < placement->num_placement; ++i) {
910 ret = ttm_mem_type_from_flags(placement->placement[i],
911 &mem_type);
912 if (ret)
913 return ret;
914 man = &bdev->man[mem_type];
916 type_ok = ttm_bo_mt_compatible(man,
917 bo->type == ttm_bo_type_user,
918 mem_type,
919 placement->placement[i],
920 &cur_flags);
922 if (!type_ok)
923 continue;
925 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
926 cur_flags);
928 * Use the access and other non-mapping-related flag bits from
929 * the memory placement flags to the current flags
931 ttm_flag_masked(&cur_flags, placement->placement[i],
932 ~TTM_PL_MASK_MEMTYPE);
934 if (mem_type == TTM_PL_SYSTEM)
935 break;
937 if (man->has_type && man->use_type) {
938 type_found = true;
939 ret = (*man->func->get_node)(man, bo, placement, mem);
940 if (unlikely(ret))
941 return ret;
943 if (mem->mm_node)
944 break;
947 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
948 mem->mem_type = mem_type;
949 mem->placement = cur_flags;
950 return 0;
953 if (!type_found)
954 return -EINVAL;
956 for (i = 0; i < placement->num_busy_placement; ++i) {
957 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
958 &mem_type);
959 if (ret)
960 return ret;
961 man = &bdev->man[mem_type];
962 if (!man->has_type)
963 continue;
964 if (!ttm_bo_mt_compatible(man,
965 bo->type == ttm_bo_type_user,
966 mem_type,
967 placement->busy_placement[i],
968 &cur_flags))
969 continue;
971 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
972 cur_flags);
974 * Use the access and other non-mapping-related flag bits from
975 * the memory placement flags to the current flags
977 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
978 ~TTM_PL_MASK_MEMTYPE);
981 if (mem_type == TTM_PL_SYSTEM) {
982 mem->mem_type = mem_type;
983 mem->placement = cur_flags;
984 mem->mm_node = NULL;
985 return 0;
988 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
989 interruptible, no_wait_reserve, no_wait_gpu);
990 if (ret == 0 && mem->mm_node) {
991 mem->placement = cur_flags;
992 return 0;
994 if (ret == -ERESTARTSYS)
995 has_erestartsys = true;
997 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
998 return ret;
1000 EXPORT_SYMBOL(ttm_bo_mem_space);
1002 int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1004 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1005 return -EBUSY;
1007 return wait_event_interruptible(bo->event_queue,
1008 atomic_read(&bo->cpu_writers) == 0);
1010 EXPORT_SYMBOL(ttm_bo_wait_cpu);
1012 int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1013 struct ttm_placement *placement,
1014 bool interruptible, bool no_wait_reserve,
1015 bool no_wait_gpu)
1017 int ret = 0;
1018 struct ttm_mem_reg mem;
1020 BUG_ON(!atomic_read(&bo->reserved));
1023 * FIXME: It's possible to pipeline buffer moves.
1024 * Have the driver move function wait for idle when necessary,
1025 * instead of doing it here.
1027 spin_lock(&bo->lock);
1028 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1029 spin_unlock(&bo->lock);
1030 if (ret)
1031 return ret;
1032 mem.num_pages = bo->num_pages;
1033 mem.size = mem.num_pages << PAGE_SHIFT;
1034 mem.page_alignment = bo->mem.page_alignment;
1035 mem.bus.io_reserved = false;
1037 * Determine where to move the buffer.
1039 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
1040 if (ret)
1041 goto out_unlock;
1042 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
1043 out_unlock:
1044 if (ret && mem.mm_node)
1045 ttm_bo_mem_put(bo, &mem);
1046 return ret;
1049 static int ttm_bo_mem_compat(struct ttm_placement *placement,
1050 struct ttm_mem_reg *mem)
1052 int i;
1054 if (mem->mm_node && placement->lpfn != 0 &&
1055 (mem->start < placement->fpfn ||
1056 mem->start + mem->num_pages > placement->lpfn))
1057 return -1;
1059 for (i = 0; i < placement->num_placement; i++) {
1060 if ((placement->placement[i] & mem->placement &
1061 TTM_PL_MASK_CACHING) &&
1062 (placement->placement[i] & mem->placement &
1063 TTM_PL_MASK_MEM))
1064 return i;
1066 return -1;
1069 int ttm_bo_validate(struct ttm_buffer_object *bo,
1070 struct ttm_placement *placement,
1071 bool interruptible, bool no_wait_reserve,
1072 bool no_wait_gpu)
1074 int ret;
1076 BUG_ON(!atomic_read(&bo->reserved));
1077 /* Check that range is valid */
1078 if (placement->lpfn || placement->fpfn)
1079 if (placement->fpfn > placement->lpfn ||
1080 (placement->lpfn - placement->fpfn) < bo->num_pages)
1081 return -EINVAL;
1083 * Check whether we need to move buffer.
1085 ret = ttm_bo_mem_compat(placement, &bo->mem);
1086 if (ret < 0) {
1087 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
1088 if (ret)
1089 return ret;
1090 } else {
1092 * Use the access and other non-mapping-related flag bits from
1093 * the compatible memory placement flags to the active flags
1095 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1096 ~TTM_PL_MASK_MEMTYPE);
1099 * We might need to add a TTM.
1101 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1102 ret = ttm_bo_add_ttm(bo, true);
1103 if (ret)
1104 return ret;
1106 return 0;
1108 EXPORT_SYMBOL(ttm_bo_validate);
1110 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1111 struct ttm_placement *placement)
1113 int i;
1115 if (placement->fpfn || placement->lpfn) {
1116 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1117 printk(KERN_ERR TTM_PFX "Page number range to small "
1118 "Need %lu pages, range is [%u, %u]\n",
1119 bo->mem.num_pages, placement->fpfn,
1120 placement->lpfn);
1121 return -EINVAL;
1124 for (i = 0; i < placement->num_placement; i++) {
1125 if (!capable(CAP_SYS_ADMIN)) {
1126 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1127 printk(KERN_ERR TTM_PFX "Need to be root to "
1128 "modify NO_EVICT status.\n");
1129 return -EINVAL;
1133 for (i = 0; i < placement->num_busy_placement; i++) {
1134 if (!capable(CAP_SYS_ADMIN)) {
1135 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1136 printk(KERN_ERR TTM_PFX "Need to be root to "
1137 "modify NO_EVICT status.\n");
1138 return -EINVAL;
1142 return 0;
1145 int ttm_bo_init(struct ttm_bo_device *bdev,
1146 struct ttm_buffer_object *bo,
1147 unsigned long size,
1148 enum ttm_bo_type type,
1149 struct ttm_placement *placement,
1150 uint32_t page_alignment,
1151 unsigned long buffer_start,
1152 bool interruptible,
1153 struct file *persistant_swap_storage,
1154 size_t acc_size,
1155 void (*destroy) (struct ttm_buffer_object *))
1157 int ret = 0;
1158 unsigned long num_pages;
1160 size += buffer_start & ~PAGE_MASK;
1161 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1162 if (num_pages == 0) {
1163 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1164 return -EINVAL;
1166 bo->destroy = destroy;
1168 spin_lock_init(&bo->lock);
1169 kref_init(&bo->kref);
1170 kref_init(&bo->list_kref);
1171 atomic_set(&bo->cpu_writers, 0);
1172 atomic_set(&bo->reserved, 1);
1173 init_waitqueue_head(&bo->event_queue);
1174 INIT_LIST_HEAD(&bo->lru);
1175 INIT_LIST_HEAD(&bo->ddestroy);
1176 INIT_LIST_HEAD(&bo->swap);
1177 bo->bdev = bdev;
1178 bo->glob = bdev->glob;
1179 bo->type = type;
1180 bo->num_pages = num_pages;
1181 bo->mem.size = num_pages << PAGE_SHIFT;
1182 bo->mem.mem_type = TTM_PL_SYSTEM;
1183 bo->mem.num_pages = bo->num_pages;
1184 bo->mem.mm_node = NULL;
1185 bo->mem.page_alignment = page_alignment;
1186 bo->mem.bus.io_reserved = false;
1187 bo->buffer_start = buffer_start & PAGE_MASK;
1188 bo->priv_flags = 0;
1189 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1190 bo->seq_valid = false;
1191 bo->persistant_swap_storage = persistant_swap_storage;
1192 bo->acc_size = acc_size;
1193 atomic_inc(&bo->glob->bo_count);
1195 ret = ttm_bo_check_placement(bo, placement);
1196 if (unlikely(ret != 0))
1197 goto out_err;
1200 * For ttm_bo_type_device buffers, allocate
1201 * address space from the device.
1203 if (bo->type == ttm_bo_type_device) {
1204 ret = ttm_bo_setup_vm(bo);
1205 if (ret)
1206 goto out_err;
1209 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
1210 if (ret)
1211 goto out_err;
1213 ttm_bo_unreserve(bo);
1214 return 0;
1216 out_err:
1217 ttm_bo_unreserve(bo);
1218 ttm_bo_unref(&bo);
1220 return ret;
1222 EXPORT_SYMBOL(ttm_bo_init);
1224 static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
1225 unsigned long num_pages)
1227 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1228 PAGE_MASK;
1230 return glob->ttm_bo_size + 2 * page_array_size;
1233 int ttm_bo_create(struct ttm_bo_device *bdev,
1234 unsigned long size,
1235 enum ttm_bo_type type,
1236 struct ttm_placement *placement,
1237 uint32_t page_alignment,
1238 unsigned long buffer_start,
1239 bool interruptible,
1240 struct file *persistant_swap_storage,
1241 struct ttm_buffer_object **p_bo)
1243 struct ttm_buffer_object *bo;
1244 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1245 int ret;
1247 size_t acc_size =
1248 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
1249 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1250 if (unlikely(ret != 0))
1251 return ret;
1253 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1255 if (unlikely(bo == NULL)) {
1256 ttm_mem_global_free(mem_glob, acc_size);
1257 return -ENOMEM;
1260 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1261 buffer_start, interruptible,
1262 persistant_swap_storage, acc_size, NULL);
1263 if (likely(ret == 0))
1264 *p_bo = bo;
1266 return ret;
1269 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1270 unsigned mem_type, bool allow_errors)
1272 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1273 struct ttm_bo_global *glob = bdev->glob;
1274 int ret;
1277 * Can't use standard list traversal since we're unlocking.
1280 spin_lock(&glob->lru_lock);
1281 while (!list_empty(&man->lru)) {
1282 spin_unlock(&glob->lru_lock);
1283 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
1284 if (ret) {
1285 if (allow_errors) {
1286 return ret;
1287 } else {
1288 printk(KERN_ERR TTM_PFX
1289 "Cleanup eviction failed\n");
1292 spin_lock(&glob->lru_lock);
1294 spin_unlock(&glob->lru_lock);
1295 return 0;
1298 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1300 struct ttm_mem_type_manager *man;
1301 int ret = -EINVAL;
1303 if (mem_type >= TTM_NUM_MEM_TYPES) {
1304 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1305 return ret;
1307 man = &bdev->man[mem_type];
1309 if (!man->has_type) {
1310 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1311 "memory manager type %u\n", mem_type);
1312 return ret;
1315 man->use_type = false;
1316 man->has_type = false;
1318 ret = 0;
1319 if (mem_type > 0) {
1320 ttm_bo_force_list_clean(bdev, mem_type, false);
1322 ret = (*man->func->takedown)(man);
1325 return ret;
1327 EXPORT_SYMBOL(ttm_bo_clean_mm);
1329 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1331 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1333 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1334 printk(KERN_ERR TTM_PFX
1335 "Illegal memory manager memory type %u.\n",
1336 mem_type);
1337 return -EINVAL;
1340 if (!man->has_type) {
1341 printk(KERN_ERR TTM_PFX
1342 "Memory type %u has not been initialized.\n",
1343 mem_type);
1344 return 0;
1347 return ttm_bo_force_list_clean(bdev, mem_type, true);
1349 EXPORT_SYMBOL(ttm_bo_evict_mm);
1351 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1352 unsigned long p_size)
1354 int ret = -EINVAL;
1355 struct ttm_mem_type_manager *man;
1357 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1358 man = &bdev->man[type];
1359 BUG_ON(man->has_type);
1361 ret = bdev->driver->init_mem_type(bdev, type, man);
1362 if (ret)
1363 return ret;
1364 man->bdev = bdev;
1366 ret = 0;
1367 if (type != TTM_PL_SYSTEM) {
1368 ret = (*man->func->init)(man, p_size);
1369 if (ret)
1370 return ret;
1372 man->has_type = true;
1373 man->use_type = true;
1374 man->size = p_size;
1376 INIT_LIST_HEAD(&man->lru);
1378 return 0;
1380 EXPORT_SYMBOL(ttm_bo_init_mm);
1382 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1384 struct ttm_bo_global *glob =
1385 container_of(kobj, struct ttm_bo_global, kobj);
1387 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1388 __free_page(glob->dummy_read_page);
1389 kfree(glob);
1392 void ttm_bo_global_release(struct drm_global_reference *ref)
1394 struct ttm_bo_global *glob = ref->object;
1396 kobject_del(&glob->kobj);
1397 kobject_put(&glob->kobj);
1399 EXPORT_SYMBOL(ttm_bo_global_release);
1401 int ttm_bo_global_init(struct drm_global_reference *ref)
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;
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);
1413 if (unlikely(glob->dummy_read_page == NULL)) {
1414 ret = -ENOMEM;
1415 goto out_no_drp;
1418 INIT_LIST_HEAD(&glob->swap_lru);
1419 INIT_LIST_HEAD(&glob->device_list);
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;
1429 glob->ttm_bo_extra_size =
1430 ttm_round_pot(sizeof(struct ttm_tt)) +
1431 ttm_round_pot(sizeof(struct ttm_backend));
1433 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1434 ttm_round_pot(sizeof(struct ttm_buffer_object));
1436 atomic_set(&glob->bo_count, 0);
1438 ret = kobject_init_and_add(
1439 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1440 if (unlikely(ret != 0))
1441 kobject_put(&glob->kobj);
1442 return ret;
1443 out_no_shrink:
1444 __free_page(glob->dummy_read_page);
1445 out_no_drp:
1446 kfree(glob);
1447 return ret;
1449 EXPORT_SYMBOL(ttm_bo_global_init);
1452 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1454 int ret = 0;
1455 unsigned i = TTM_NUM_MEM_TYPES;
1456 struct ttm_mem_type_manager *man;
1457 struct ttm_bo_global *glob = bdev->glob;
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);
1469 man->has_type = false;
1473 mutex_lock(&glob->device_list_mutex);
1474 list_del(&bdev->device_list);
1475 mutex_unlock(&glob->device_list_mutex);
1477 if (!cancel_delayed_work(&bdev->wq))
1478 flush_scheduled_work();
1480 while (ttm_bo_delayed_delete(bdev, true))
1483 spin_lock(&glob->lru_lock);
1484 if (list_empty(&bdev->ddestroy))
1485 TTM_DEBUG("Delayed destroy list was clean\n");
1487 if (list_empty(&bdev->man[0].lru))
1488 TTM_DEBUG("Swap list was clean\n");
1489 spin_unlock(&glob->lru_lock);
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);
1496 return ret;
1498 EXPORT_SYMBOL(ttm_bo_device_release);
1500 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1501 struct ttm_bo_global *glob,
1502 struct ttm_bo_driver *driver,
1503 uint64_t file_page_offset,
1504 bool need_dma32)
1506 int ret = -EINVAL;
1508 rwlock_init(&bdev->vm_lock);
1509 bdev->driver = driver;
1511 memset(bdev->man, 0, sizeof(bdev->man));
1514 * Initialize the system memory buffer type.
1515 * Other types need to be driver / IOCTL initialized.
1517 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1518 if (unlikely(ret != 0))
1519 goto out_no_sys;
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))
1524 goto out_no_addr_mm;
1526 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1527 bdev->nice_mode = true;
1528 INIT_LIST_HEAD(&bdev->ddestroy);
1529 bdev->dev_mapping = NULL;
1530 bdev->glob = glob;
1531 bdev->need_dma32 = need_dma32;
1533 mutex_lock(&glob->device_list_mutex);
1534 list_add_tail(&bdev->device_list, &glob->device_list);
1535 mutex_unlock(&glob->device_list_mutex);
1537 return 0;
1538 out_no_addr_mm:
1539 ttm_bo_clean_mm(bdev, 0);
1540 out_no_sys:
1541 return ret;
1543 EXPORT_SYMBOL(ttm_bo_device_init);
1546 * buffer object vm functions.
1549 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1551 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1553 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1554 if (mem->mem_type == TTM_PL_SYSTEM)
1555 return false;
1557 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1558 return false;
1560 if (mem->placement & TTM_PL_FLAG_CACHED)
1561 return false;
1563 return true;
1566 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1568 struct ttm_bo_device *bdev = bo->bdev;
1569 loff_t offset = (loff_t) bo->addr_space_offset;
1570 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1572 if (!bdev->dev_mapping)
1573 return;
1574 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1575 ttm_mem_io_free(bdev, &bo->mem);
1577 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1579 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1581 struct ttm_bo_device *bdev = bo->bdev;
1582 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1583 struct rb_node *parent = NULL;
1584 struct ttm_buffer_object *cur_bo;
1585 unsigned long offset = bo->vm_node->start;
1586 unsigned long cur_offset;
1588 while (*cur) {
1589 parent = *cur;
1590 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1591 cur_offset = cur_bo->vm_node->start;
1592 if (offset < cur_offset)
1593 cur = &parent->rb_left;
1594 else if (offset > cur_offset)
1595 cur = &parent->rb_right;
1596 else
1597 BUG();
1600 rb_link_node(&bo->vm_rb, parent, cur);
1601 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1605 * ttm_bo_setup_vm:
1607 * @bo: the buffer to allocate address space for
1609 * Allocate address space in the drm device so that applications
1610 * can mmap the buffer and access the contents. This only
1611 * applies to ttm_bo_type_device objects as others are not
1612 * placed in the drm device address space.
1615 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1617 struct ttm_bo_device *bdev = bo->bdev;
1618 int ret;
1620 retry_pre_get:
1621 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1622 if (unlikely(ret != 0))
1623 return ret;
1625 write_lock(&bdev->vm_lock);
1626 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1627 bo->mem.num_pages, 0, 0);
1629 if (unlikely(bo->vm_node == NULL)) {
1630 ret = -ENOMEM;
1631 goto out_unlock;
1634 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1635 bo->mem.num_pages, 0);
1637 if (unlikely(bo->vm_node == NULL)) {
1638 write_unlock(&bdev->vm_lock);
1639 goto retry_pre_get;
1642 ttm_bo_vm_insert_rb(bo);
1643 write_unlock(&bdev->vm_lock);
1644 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1646 return 0;
1647 out_unlock:
1648 write_unlock(&bdev->vm_lock);
1649 return ret;
1652 int ttm_bo_wait(struct ttm_buffer_object *bo,
1653 bool lazy, bool interruptible, bool no_wait)
1655 struct ttm_bo_driver *driver = bo->bdev->driver;
1656 void *sync_obj;
1657 void *sync_obj_arg;
1658 int ret = 0;
1660 if (likely(bo->sync_obj == NULL))
1661 return 0;
1663 while (bo->sync_obj) {
1665 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1666 void *tmp_obj = bo->sync_obj;
1667 bo->sync_obj = NULL;
1668 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1669 spin_unlock(&bo->lock);
1670 driver->sync_obj_unref(&tmp_obj);
1671 spin_lock(&bo->lock);
1672 continue;
1675 if (no_wait)
1676 return -EBUSY;
1678 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1679 sync_obj_arg = bo->sync_obj_arg;
1680 spin_unlock(&bo->lock);
1681 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1682 lazy, interruptible);
1683 if (unlikely(ret != 0)) {
1684 driver->sync_obj_unref(&sync_obj);
1685 spin_lock(&bo->lock);
1686 return ret;
1688 spin_lock(&bo->lock);
1689 if (likely(bo->sync_obj == sync_obj &&
1690 bo->sync_obj_arg == sync_obj_arg)) {
1691 void *tmp_obj = bo->sync_obj;
1692 bo->sync_obj = NULL;
1693 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1694 &bo->priv_flags);
1695 spin_unlock(&bo->lock);
1696 driver->sync_obj_unref(&sync_obj);
1697 driver->sync_obj_unref(&tmp_obj);
1698 spin_lock(&bo->lock);
1699 } else {
1700 spin_unlock(&bo->lock);
1701 driver->sync_obj_unref(&sync_obj);
1702 spin_lock(&bo->lock);
1705 return 0;
1707 EXPORT_SYMBOL(ttm_bo_wait);
1709 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1711 int ret = 0;
1714 * Using ttm_bo_reserve makes sure the lru lists are updated.
1717 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1718 if (unlikely(ret != 0))
1719 return ret;
1720 spin_lock(&bo->lock);
1721 ret = ttm_bo_wait(bo, false, true, no_wait);
1722 spin_unlock(&bo->lock);
1723 if (likely(ret == 0))
1724 atomic_inc(&bo->cpu_writers);
1725 ttm_bo_unreserve(bo);
1726 return ret;
1728 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1730 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1732 if (atomic_dec_and_test(&bo->cpu_writers))
1733 wake_up_all(&bo->event_queue);
1735 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1738 * A buffer object shrink method that tries to swap out the first
1739 * buffer object on the bo_global::swap_lru list.
1742 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1744 struct ttm_bo_global *glob =
1745 container_of(shrink, struct ttm_bo_global, shrink);
1746 struct ttm_buffer_object *bo;
1747 int ret = -EBUSY;
1748 int put_count;
1749 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1751 spin_lock(&glob->lru_lock);
1752 while (ret == -EBUSY) {
1753 if (unlikely(list_empty(&glob->swap_lru))) {
1754 spin_unlock(&glob->lru_lock);
1755 return -EBUSY;
1758 bo = list_first_entry(&glob->swap_lru,
1759 struct ttm_buffer_object, swap);
1760 kref_get(&bo->list_kref);
1762 if (!list_empty(&bo->ddestroy)) {
1763 spin_unlock(&glob->lru_lock);
1764 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1765 kref_put(&bo->list_kref, ttm_bo_release_list);
1766 continue;
1770 * Reserve buffer. Since we unlock while sleeping, we need
1771 * to re-check that nobody removed us from the swap-list while
1772 * we slept.
1775 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1776 if (unlikely(ret == -EBUSY)) {
1777 spin_unlock(&glob->lru_lock);
1778 ttm_bo_wait_unreserved(bo, false);
1779 kref_put(&bo->list_kref, ttm_bo_release_list);
1780 spin_lock(&glob->lru_lock);
1784 BUG_ON(ret != 0);
1785 put_count = ttm_bo_del_from_lru(bo);
1786 spin_unlock(&glob->lru_lock);
1788 while (put_count--)
1789 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1792 * Wait for GPU, then move to system cached.
1795 spin_lock(&bo->lock);
1796 ret = ttm_bo_wait(bo, false, false, false);
1797 spin_unlock(&bo->lock);
1799 if (unlikely(ret != 0))
1800 goto out;
1802 if ((bo->mem.placement & swap_placement) != swap_placement) {
1803 struct ttm_mem_reg evict_mem;
1805 evict_mem = bo->mem;
1806 evict_mem.mm_node = NULL;
1807 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1808 evict_mem.mem_type = TTM_PL_SYSTEM;
1810 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1811 false, false, false);
1812 if (unlikely(ret != 0))
1813 goto out;
1816 ttm_bo_unmap_virtual(bo);
1819 * Swap out. Buffer will be swapped in again as soon as
1820 * anyone tries to access a ttm page.
1823 if (bo->bdev->driver->swap_notify)
1824 bo->bdev->driver->swap_notify(bo);
1826 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1827 out:
1831 * Unreserve without putting on LRU to avoid swapping out an
1832 * already swapped buffer.
1835 atomic_set(&bo->reserved, 0);
1836 wake_up_all(&bo->event_queue);
1837 kref_put(&bo->list_kref, ttm_bo_release_list);
1838 return ret;
1841 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1843 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1846 EXPORT_SYMBOL(ttm_bo_swapout_all);