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