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