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