7ea0482c1aeeb37f42e8c57a6ed5235dae1ec665
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / ttm / ttm_bo_util.c
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  */
31
32 #include <drm/ttm/ttm_bo_driver.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/drm_vma_manager.h>
35 #include <linux/io.h>
36 #include <linux/highmem.h>
37 #include <linux/wait.h>
38 #include <linux/slab.h>
39 #include <linux/vmalloc.h>
40 #include <linux/module.h>
41 #include <linux/dma-resv.h>
42
43 struct ttm_transfer_obj {
44         struct ttm_buffer_object base;
45         struct ttm_buffer_object *bo;
46 };
47
48 int ttm_bo_move_to_new_tt_mem(struct ttm_buffer_object *bo,
49                               struct ttm_operation_ctx *ctx,
50                               struct ttm_resource *new_mem)
51 {
52         int ret;
53
54         if (new_mem->mem_type == TTM_PL_SYSTEM)
55                 return 0;
56
57         ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
58         if (unlikely(ret != 0))
59                 return ret;
60
61         ret = ttm_bo_tt_bind(bo, new_mem);
62         if (unlikely(ret != 0))
63                 return ret;
64
65         return 0;
66 }
67 EXPORT_SYMBOL(ttm_bo_move_to_new_tt_mem);
68
69 static int ttm_bo_move_to_system(struct ttm_buffer_object *bo,
70                                  struct ttm_operation_ctx *ctx)
71 {
72         struct ttm_resource *old_mem = &bo->mem;
73         int ret;
74
75         if (old_mem->mem_type == TTM_PL_SYSTEM)
76                 return 0;
77
78         ret = ttm_bo_wait_ctx(bo, ctx);
79         if (unlikely(ret != 0)) {
80                 if (ret != -ERESTARTSYS)
81                         pr_err("Failed to expire sync object before unbinding TTM\n");
82                 return ret;
83         }
84
85         ttm_bo_tt_unbind(bo);
86         ttm_resource_free(bo, &bo->mem);
87         old_mem->mem_type = TTM_PL_SYSTEM;
88         return 0;
89 }
90
91 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
92                    struct ttm_operation_ctx *ctx,
93                     struct ttm_resource *new_mem)
94 {
95         int ret;
96
97         ret = ttm_bo_move_to_system(bo, ctx);
98         if (unlikely(ret != 0))
99                 return ret;
100
101         ret = ttm_bo_move_to_new_tt_mem(bo, ctx, new_mem);
102         if (unlikely(ret != 0))
103                 return ret;
104
105         ttm_bo_assign_mem(bo, new_mem);
106         return 0;
107 }
108 EXPORT_SYMBOL(ttm_bo_move_ttm);
109
110 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
111                        struct ttm_resource *mem)
112 {
113         if (mem->bus.offset || mem->bus.addr)
114                 return 0;
115
116         mem->bus.is_iomem = false;
117         if (!bdev->driver->io_mem_reserve)
118                 return 0;
119
120         return bdev->driver->io_mem_reserve(bdev, mem);
121 }
122
123 void ttm_mem_io_free(struct ttm_bo_device *bdev,
124                      struct ttm_resource *mem)
125 {
126         if (!mem->bus.offset && !mem->bus.addr)
127                 return;
128
129         if (bdev->driver->io_mem_free)
130                 bdev->driver->io_mem_free(bdev, mem);
131
132         mem->bus.offset = 0;
133         mem->bus.addr = NULL;
134 }
135
136 static int ttm_resource_ioremap(struct ttm_bo_device *bdev,
137                                struct ttm_resource *mem,
138                                void **virtual)
139 {
140         int ret;
141         void *addr;
142
143         *virtual = NULL;
144         ret = ttm_mem_io_reserve(bdev, mem);
145         if (ret || !mem->bus.is_iomem)
146                 return ret;
147
148         if (mem->bus.addr) {
149                 addr = mem->bus.addr;
150         } else {
151                 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
152
153                 if (mem->bus.caching == ttm_write_combined)
154                         addr = ioremap_wc(mem->bus.offset, bus_size);
155                 else
156                         addr = ioremap(mem->bus.offset, bus_size);
157                 if (!addr) {
158                         ttm_mem_io_free(bdev, mem);
159                         return -ENOMEM;
160                 }
161         }
162         *virtual = addr;
163         return 0;
164 }
165
166 static void ttm_resource_iounmap(struct ttm_bo_device *bdev,
167                                 struct ttm_resource *mem,
168                                 void *virtual)
169 {
170         if (virtual && mem->bus.addr == NULL)
171                 iounmap(virtual);
172         ttm_mem_io_free(bdev, mem);
173 }
174
175 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
176 {
177         uint32_t *dstP =
178             (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
179         uint32_t *srcP =
180             (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
181
182         int i;
183         for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
184                 iowrite32(ioread32(srcP++), dstP++);
185         return 0;
186 }
187
188 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
189                                 unsigned long page,
190                                 pgprot_t prot)
191 {
192         struct page *d = ttm->pages[page];
193         void *dst;
194
195         if (!d)
196                 return -ENOMEM;
197
198         src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
199         dst = kmap_atomic_prot(d, prot);
200         if (!dst)
201                 return -ENOMEM;
202
203         memcpy_fromio(dst, src, PAGE_SIZE);
204
205         kunmap_atomic(dst);
206
207         return 0;
208 }
209
210 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
211                                 unsigned long page,
212                                 pgprot_t prot)
213 {
214         struct page *s = ttm->pages[page];
215         void *src;
216
217         if (!s)
218                 return -ENOMEM;
219
220         dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
221         src = kmap_atomic_prot(s, prot);
222         if (!src)
223                 return -ENOMEM;
224
225         memcpy_toio(dst, src, PAGE_SIZE);
226
227         kunmap_atomic(src);
228
229         return 0;
230 }
231
232 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
233                        struct ttm_operation_ctx *ctx,
234                        struct ttm_resource *new_mem)
235 {
236         struct ttm_bo_device *bdev = bo->bdev;
237         struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
238         struct ttm_tt *ttm = bo->ttm;
239         struct ttm_resource *old_mem = &bo->mem;
240         struct ttm_resource old_copy = *old_mem;
241         void *old_iomap;
242         void *new_iomap;
243         int ret;
244         unsigned long i;
245         unsigned long page;
246         unsigned long add = 0;
247         int dir;
248
249         ret = ttm_bo_wait_ctx(bo, ctx);
250         if (ret)
251                 return ret;
252
253         ret = ttm_resource_ioremap(bdev, old_mem, &old_iomap);
254         if (ret)
255                 return ret;
256         ret = ttm_resource_ioremap(bdev, new_mem, &new_iomap);
257         if (ret)
258                 goto out;
259
260         /*
261          * Single TTM move. NOP.
262          */
263         if (old_iomap == NULL && new_iomap == NULL)
264                 goto out2;
265
266         /*
267          * Don't move nonexistent data. Clear destination instead.
268          */
269         if (old_iomap == NULL &&
270             (ttm == NULL || (!ttm_tt_is_populated(ttm) &&
271                              !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
272                 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
273                 goto out2;
274         }
275
276         /*
277          * TTM might be null for moves within the same region.
278          */
279         if (ttm) {
280                 ret = ttm_tt_populate(bdev, ttm, ctx);
281                 if (ret)
282                         goto out1;
283         }
284
285         add = 0;
286         dir = 1;
287
288         if ((old_mem->mem_type == new_mem->mem_type) &&
289             (new_mem->start < old_mem->start + old_mem->size)) {
290                 dir = -1;
291                 add = new_mem->num_pages - 1;
292         }
293
294         for (i = 0; i < new_mem->num_pages; ++i) {
295                 page = i * dir + add;
296                 if (old_iomap == NULL) {
297                         pgprot_t prot = ttm_io_prot(bo, old_mem, PAGE_KERNEL);
298                         ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
299                                                    prot);
300                 } else if (new_iomap == NULL) {
301                         pgprot_t prot = ttm_io_prot(bo, new_mem, PAGE_KERNEL);
302                         ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
303                                                    prot);
304                 } else {
305                         ret = ttm_copy_io_page(new_iomap, old_iomap, page);
306                 }
307                 if (ret)
308                         goto out1;
309         }
310         mb();
311 out2:
312         old_copy = *old_mem;
313
314         ttm_bo_assign_mem(bo, new_mem);
315
316         if (!man->use_tt)
317                 ttm_bo_tt_destroy(bo);
318
319 out1:
320         ttm_resource_iounmap(bdev, old_mem, new_iomap);
321 out:
322         ttm_resource_iounmap(bdev, &old_copy, old_iomap);
323
324         /*
325          * On error, keep the mm node!
326          */
327         if (!ret)
328                 ttm_resource_free(bo, &old_copy);
329         return ret;
330 }
331 EXPORT_SYMBOL(ttm_bo_move_memcpy);
332
333 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
334 {
335         struct ttm_transfer_obj *fbo;
336
337         fbo = container_of(bo, struct ttm_transfer_obj, base);
338         ttm_bo_put(fbo->bo);
339         kfree(fbo);
340 }
341
342 /**
343  * ttm_buffer_object_transfer
344  *
345  * @bo: A pointer to a struct ttm_buffer_object.
346  * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
347  * holding the data of @bo with the old placement.
348  *
349  * This is a utility function that may be called after an accelerated move
350  * has been scheduled. A new buffer object is created as a placeholder for
351  * the old data while it's being copied. When that buffer object is idle,
352  * it can be destroyed, releasing the space of the old placement.
353  * Returns:
354  * !0: Failure.
355  */
356
357 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
358                                       struct ttm_buffer_object **new_obj)
359 {
360         struct ttm_transfer_obj *fbo;
361         int ret;
362
363         fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
364         if (!fbo)
365                 return -ENOMEM;
366
367         fbo->base = *bo;
368
369         ttm_bo_get(bo);
370         fbo->bo = bo;
371
372         /**
373          * Fix up members that we shouldn't copy directly:
374          * TODO: Explicit member copy would probably be better here.
375          */
376
377         atomic_inc(&ttm_bo_glob.bo_count);
378         INIT_LIST_HEAD(&fbo->base.ddestroy);
379         INIT_LIST_HEAD(&fbo->base.lru);
380         INIT_LIST_HEAD(&fbo->base.swap);
381         fbo->base.moving = NULL;
382         drm_vma_node_reset(&fbo->base.base.vma_node);
383
384         kref_init(&fbo->base.kref);
385         fbo->base.destroy = &ttm_transfered_destroy;
386         fbo->base.acc_size = 0;
387         fbo->base.pin_count = 1;
388         if (bo->type != ttm_bo_type_sg)
389                 fbo->base.base.resv = &fbo->base.base._resv;
390
391         dma_resv_init(&fbo->base.base._resv);
392         fbo->base.base.dev = NULL;
393         ret = dma_resv_trylock(&fbo->base.base._resv);
394         WARN_ON(!ret);
395
396         *new_obj = &fbo->base;
397         return 0;
398 }
399
400 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
401                      pgprot_t tmp)
402 {
403         struct ttm_resource_manager *man;
404         enum ttm_caching caching;
405
406         man = ttm_manager_type(bo->bdev, res->mem_type);
407         caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
408
409         /* Cached mappings need no adjustment */
410         if (caching == ttm_cached)
411                 return tmp;
412
413 #if defined(__i386__) || defined(__x86_64__)
414         if (caching == ttm_write_combined)
415                 tmp = pgprot_writecombine(tmp);
416         else if (boot_cpu_data.x86 > 3)
417                 tmp = pgprot_noncached(tmp);
418 #endif
419 #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
420     defined(__powerpc__) || defined(__mips__)
421         if (caching == ttm_write_combined)
422                 tmp = pgprot_writecombine(tmp);
423         else
424                 tmp = pgprot_noncached(tmp);
425 #endif
426 #if defined(__sparc__)
427         tmp = pgprot_noncached(tmp);
428 #endif
429         return tmp;
430 }
431 EXPORT_SYMBOL(ttm_io_prot);
432
433 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
434                           unsigned long offset,
435                           unsigned long size,
436                           struct ttm_bo_kmap_obj *map)
437 {
438         struct ttm_resource *mem = &bo->mem;
439
440         if (bo->mem.bus.addr) {
441                 map->bo_kmap_type = ttm_bo_map_premapped;
442                 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
443         } else {
444                 map->bo_kmap_type = ttm_bo_map_iomap;
445                 if (mem->bus.caching == ttm_write_combined)
446                         map->virtual = ioremap_wc(bo->mem.bus.offset + offset,
447                                                   size);
448                 else
449                         map->virtual = ioremap(bo->mem.bus.offset + offset,
450                                                size);
451         }
452         return (!map->virtual) ? -ENOMEM : 0;
453 }
454
455 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
456                            unsigned long start_page,
457                            unsigned long num_pages,
458                            struct ttm_bo_kmap_obj *map)
459 {
460         struct ttm_resource *mem = &bo->mem;
461         struct ttm_operation_ctx ctx = {
462                 .interruptible = false,
463                 .no_wait_gpu = false
464         };
465         struct ttm_tt *ttm = bo->ttm;
466         pgprot_t prot;
467         int ret;
468
469         BUG_ON(!ttm);
470
471         ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
472         if (ret)
473                 return ret;
474
475         if (num_pages == 1 && ttm->caching == ttm_cached) {
476                 /*
477                  * We're mapping a single page, and the desired
478                  * page protection is consistent with the bo.
479                  */
480
481                 map->bo_kmap_type = ttm_bo_map_kmap;
482                 map->page = ttm->pages[start_page];
483                 map->virtual = kmap(map->page);
484         } else {
485                 /*
486                  * We need to use vmap to get the desired page protection
487                  * or to make the buffer object look contiguous.
488                  */
489                 prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
490                 map->bo_kmap_type = ttm_bo_map_vmap;
491                 map->virtual = vmap(ttm->pages + start_page, num_pages,
492                                     0, prot);
493         }
494         return (!map->virtual) ? -ENOMEM : 0;
495 }
496
497 int ttm_bo_kmap(struct ttm_buffer_object *bo,
498                 unsigned long start_page, unsigned long num_pages,
499                 struct ttm_bo_kmap_obj *map)
500 {
501         unsigned long offset, size;
502         int ret;
503
504         map->virtual = NULL;
505         map->bo = bo;
506         if (num_pages > bo->num_pages)
507                 return -EINVAL;
508         if (start_page > bo->num_pages)
509                 return -EINVAL;
510
511         ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
512         if (ret)
513                 return ret;
514         if (!bo->mem.bus.is_iomem) {
515                 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
516         } else {
517                 offset = start_page << PAGE_SHIFT;
518                 size = num_pages << PAGE_SHIFT;
519                 return ttm_bo_ioremap(bo, offset, size, map);
520         }
521 }
522 EXPORT_SYMBOL(ttm_bo_kmap);
523
524 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
525 {
526         if (!map->virtual)
527                 return;
528         switch (map->bo_kmap_type) {
529         case ttm_bo_map_iomap:
530                 iounmap(map->virtual);
531                 break;
532         case ttm_bo_map_vmap:
533                 vunmap(map->virtual);
534                 break;
535         case ttm_bo_map_kmap:
536                 kunmap(map->page);
537                 break;
538         case ttm_bo_map_premapped:
539                 break;
540         default:
541                 BUG();
542         }
543         ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
544         map->virtual = NULL;
545         map->page = NULL;
546 }
547 EXPORT_SYMBOL(ttm_bo_kunmap);
548
549 static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
550                                  bool dst_use_tt)
551 {
552         int ret;
553         ret = ttm_bo_wait(bo, false, false);
554         if (ret)
555                 return ret;
556
557         if (!dst_use_tt)
558                 ttm_bo_tt_destroy(bo);
559         ttm_resource_free(bo, &bo->mem);
560         return 0;
561 }
562
563 static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
564                                 struct dma_fence *fence,
565                                 bool dst_use_tt)
566 {
567         struct ttm_buffer_object *ghost_obj;
568         int ret;
569
570         /**
571          * This should help pipeline ordinary buffer moves.
572          *
573          * Hang old buffer memory on a new buffer object,
574          * and leave it to be released when the GPU
575          * operation has completed.
576          */
577
578         dma_fence_put(bo->moving);
579         bo->moving = dma_fence_get(fence);
580
581         ret = ttm_buffer_object_transfer(bo, &ghost_obj);
582         if (ret)
583                 return ret;
584
585         dma_resv_add_excl_fence(&ghost_obj->base._resv, fence);
586
587         /**
588          * If we're not moving to fixed memory, the TTM object
589          * needs to stay alive. Otherwhise hang it on the ghost
590          * bo to be unbound and destroyed.
591          */
592
593         if (dst_use_tt)
594                 ghost_obj->ttm = NULL;
595         else
596                 bo->ttm = NULL;
597
598         dma_resv_unlock(&ghost_obj->base._resv);
599         ttm_bo_put(ghost_obj);
600         return 0;
601 }
602
603 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
604                                        struct dma_fence *fence)
605 {
606         struct ttm_bo_device *bdev = bo->bdev;
607         struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->mem.mem_type);
608
609         /**
610          * BO doesn't have a TTM we need to bind/unbind. Just remember
611          * this eviction and free up the allocation
612          */
613         spin_lock(&from->move_lock);
614         if (!from->move || dma_fence_is_later(fence, from->move)) {
615                 dma_fence_put(from->move);
616                 from->move = dma_fence_get(fence);
617         }
618         spin_unlock(&from->move_lock);
619
620         ttm_resource_free(bo, &bo->mem);
621
622         dma_fence_put(bo->moving);
623         bo->moving = dma_fence_get(fence);
624 }
625
626 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
627                               struct dma_fence *fence,
628                               bool evict,
629                               bool pipeline,
630                               struct ttm_resource *new_mem)
631 {
632         struct ttm_bo_device *bdev = bo->bdev;
633         struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->mem.mem_type);
634         struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
635         int ret = 0;
636
637         dma_resv_add_excl_fence(bo->base.resv, fence);
638         if (!evict)
639                 ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
640         else if (!from->use_tt && pipeline)
641                 ttm_bo_move_pipeline_evict(bo, fence);
642         else
643                 ret = ttm_bo_wait_free_node(bo, man->use_tt);
644
645         if (ret)
646                 return ret;
647
648         ttm_bo_assign_mem(bo, new_mem);
649
650         return 0;
651 }
652 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
653
654 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
655 {
656         struct ttm_buffer_object *ghost;
657         int ret;
658
659         ret = ttm_buffer_object_transfer(bo, &ghost);
660         if (ret)
661                 return ret;
662
663         ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
664         /* Last resort, wait for the BO to be idle when we are OOM */
665         if (ret)
666                 ttm_bo_wait(bo, false, false);
667
668         memset(&bo->mem, 0, sizeof(bo->mem));
669         bo->mem.mem_type = TTM_PL_SYSTEM;
670         bo->ttm = NULL;
671
672         dma_resv_unlock(&ghost->base._resv);
673         ttm_bo_put(ghost);
674
675         return 0;
676 }