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