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