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