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