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