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