drm/ttm: add move old to system to drivers.
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / radeon / radeon_ttm.c
1 /*
2  * Copyright 2009 Jerome Glisse.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * The above copyright notice and this permission notice (including the
22  * next paragraph) shall be included in all copies or substantial portions
23  * of the Software.
24  *
25  */
26 /*
27  * Authors:
28  *    Jerome Glisse <glisse@freedesktop.org>
29  *    Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30  *    Dave Airlie
31  */
32
33 #include <linux/dma-mapping.h>
34 #include <linux/pagemap.h>
35 #include <linux/pci.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/swap.h>
39 #include <linux/swiotlb.h>
40
41 #include <drm/drm_agpsupport.h>
42 #include <drm/drm_debugfs.h>
43 #include <drm/drm_device.h>
44 #include <drm/drm_file.h>
45 #include <drm/drm_prime.h>
46 #include <drm/radeon_drm.h>
47 #include <drm/ttm/ttm_bo_api.h>
48 #include <drm/ttm/ttm_bo_driver.h>
49 #include <drm/ttm/ttm_module.h>
50 #include <drm/ttm/ttm_page_alloc.h>
51 #include <drm/ttm/ttm_placement.h>
52
53 #include "radeon_reg.h"
54 #include "radeon.h"
55
56 static int radeon_ttm_debugfs_init(struct radeon_device *rdev);
57 static void radeon_ttm_debugfs_fini(struct radeon_device *rdev);
58
59 static int radeon_ttm_tt_bind(struct ttm_bo_device *bdev,
60                               struct ttm_tt *ttm,
61                               struct ttm_resource *bo_mem);
62
63 struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev)
64 {
65         struct radeon_mman *mman;
66         struct radeon_device *rdev;
67
68         mman = container_of(bdev, struct radeon_mman, bdev);
69         rdev = container_of(mman, struct radeon_device, mman);
70         return rdev;
71 }
72
73 static int radeon_ttm_init_vram(struct radeon_device *rdev)
74 {
75         return ttm_range_man_init(&rdev->mman.bdev, TTM_PL_VRAM,
76                                   false, rdev->mc.real_vram_size >> PAGE_SHIFT);
77 }
78
79 static int radeon_ttm_init_gtt(struct radeon_device *rdev)
80 {
81         return ttm_range_man_init(&rdev->mman.bdev, TTM_PL_TT,
82                                   true, rdev->mc.gtt_size >> PAGE_SHIFT);
83 }
84
85 static void radeon_evict_flags(struct ttm_buffer_object *bo,
86                                 struct ttm_placement *placement)
87 {
88         static const struct ttm_place placements = {
89                 .fpfn = 0,
90                 .lpfn = 0,
91                 .mem_type = TTM_PL_SYSTEM,
92                 .flags = 0
93         };
94
95         struct radeon_bo *rbo;
96
97         if (!radeon_ttm_bo_is_radeon_bo(bo)) {
98                 placement->placement = &placements;
99                 placement->busy_placement = &placements;
100                 placement->num_placement = 1;
101                 placement->num_busy_placement = 1;
102                 return;
103         }
104         rbo = container_of(bo, struct radeon_bo, tbo);
105         switch (bo->mem.mem_type) {
106         case TTM_PL_VRAM:
107                 if (rbo->rdev->ring[radeon_copy_ring_index(rbo->rdev)].ready == false)
108                         radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
109                 else if (rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size &&
110                          bo->mem.start < (rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT)) {
111                         unsigned fpfn = rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
112                         int i;
113
114                         /* Try evicting to the CPU inaccessible part of VRAM
115                          * first, but only set GTT as busy placement, so this
116                          * BO will be evicted to GTT rather than causing other
117                          * BOs to be evicted from VRAM
118                          */
119                         radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM |
120                                                          RADEON_GEM_DOMAIN_GTT);
121                         rbo->placement.num_busy_placement = 0;
122                         for (i = 0; i < rbo->placement.num_placement; i++) {
123                                 if (rbo->placements[i].mem_type == TTM_PL_VRAM) {
124                                         if (rbo->placements[i].fpfn < fpfn)
125                                                 rbo->placements[i].fpfn = fpfn;
126                                 } else {
127                                         rbo->placement.busy_placement =
128                                                 &rbo->placements[i];
129                                         rbo->placement.num_busy_placement = 1;
130                                 }
131                         }
132                 } else
133                         radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
134                 break;
135         case TTM_PL_TT:
136         default:
137                 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
138         }
139         *placement = rbo->placement;
140 }
141
142 static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp)
143 {
144         struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
145         struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
146
147         if (radeon_ttm_tt_has_userptr(rdev, bo->ttm))
148                 return -EPERM;
149         return drm_vma_node_verify_access(&rbo->tbo.base.vma_node,
150                                           filp->private_data);
151 }
152
153 static int radeon_move_blit(struct ttm_buffer_object *bo,
154                         bool evict,
155                         struct ttm_resource *new_mem,
156                         struct ttm_resource *old_mem)
157 {
158         struct radeon_device *rdev;
159         uint64_t old_start, new_start;
160         struct radeon_fence *fence;
161         unsigned num_pages;
162         int r, ridx;
163
164         rdev = radeon_get_rdev(bo->bdev);
165         ridx = radeon_copy_ring_index(rdev);
166         old_start = (u64)old_mem->start << PAGE_SHIFT;
167         new_start = (u64)new_mem->start << PAGE_SHIFT;
168
169         switch (old_mem->mem_type) {
170         case TTM_PL_VRAM:
171                 old_start += rdev->mc.vram_start;
172                 break;
173         case TTM_PL_TT:
174                 old_start += rdev->mc.gtt_start;
175                 break;
176         default:
177                 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
178                 return -EINVAL;
179         }
180         switch (new_mem->mem_type) {
181         case TTM_PL_VRAM:
182                 new_start += rdev->mc.vram_start;
183                 break;
184         case TTM_PL_TT:
185                 new_start += rdev->mc.gtt_start;
186                 break;
187         default:
188                 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
189                 return -EINVAL;
190         }
191         if (!rdev->ring[ridx].ready) {
192                 DRM_ERROR("Trying to move memory with ring turned off.\n");
193                 return -EINVAL;
194         }
195
196         BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0);
197
198         num_pages = new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
199         fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->base.resv);
200         if (IS_ERR(fence))
201                 return PTR_ERR(fence);
202
203         r = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, false, new_mem);
204         radeon_fence_unref(&fence);
205         return r;
206 }
207
208 static int radeon_move_vram_ram(struct ttm_buffer_object *bo,
209                                 bool evict,
210                                 struct ttm_operation_ctx *ctx,
211                                 struct ttm_resource *new_mem)
212 {
213         struct ttm_resource *old_mem = &bo->mem;
214         struct ttm_resource tmp_mem;
215         struct ttm_place placements;
216         struct ttm_placement placement;
217         int r;
218
219         tmp_mem = *new_mem;
220         tmp_mem.mm_node = NULL;
221         placement.num_placement = 1;
222         placement.placement = &placements;
223         placement.num_busy_placement = 1;
224         placement.busy_placement = &placements;
225         placements.fpfn = 0;
226         placements.lpfn = 0;
227         placements.mem_type = TTM_PL_TT;
228         placements.flags = 0;
229         r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
230         if (unlikely(r)) {
231                 return r;
232         }
233
234         r = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
235         if (unlikely(r)) {
236                 goto out_cleanup;
237         }
238
239         r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, &tmp_mem);
240         if (unlikely(r)) {
241                 goto out_cleanup;
242         }
243         r = radeon_move_blit(bo, true, &tmp_mem, old_mem);
244         if (unlikely(r)) {
245                 goto out_cleanup;
246         }
247         r = ttm_bo_move_to_system(bo, ctx);
248         if (unlikely(r))
249                 goto out_cleanup;
250
251         ttm_bo_assign_mem(bo, new_mem);
252 out_cleanup:
253         ttm_resource_free(bo, &tmp_mem);
254         return r;
255 }
256
257 static int radeon_move_ram_vram(struct ttm_buffer_object *bo,
258                                 bool evict,
259                                 struct ttm_operation_ctx *ctx,
260                                 struct ttm_resource *new_mem)
261 {
262         struct ttm_resource *old_mem = &bo->mem;
263         struct ttm_resource tmp_mem;
264         struct ttm_placement placement;
265         struct ttm_place placements;
266         int r;
267
268         tmp_mem = *new_mem;
269         tmp_mem.mm_node = NULL;
270         placement.num_placement = 1;
271         placement.placement = &placements;
272         placement.num_busy_placement = 1;
273         placement.busy_placement = &placements;
274         placements.fpfn = 0;
275         placements.lpfn = 0;
276         placements.mem_type = TTM_PL_TT;
277         placements.flags = 0;
278         r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
279         if (unlikely(r)) {
280                 return r;
281         }
282         r = ttm_bo_move_to_new_tt_mem(bo, ctx, &tmp_mem);
283         if (unlikely(r)) {
284                 goto out_cleanup;
285         }
286         ttm_bo_assign_mem(bo, &tmp_mem);
287         r = radeon_move_blit(bo, true, new_mem, old_mem);
288         if (unlikely(r)) {
289                 goto out_cleanup;
290         }
291 out_cleanup:
292         ttm_resource_free(bo, &tmp_mem);
293         return r;
294 }
295
296 static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
297                           struct ttm_operation_ctx *ctx,
298                           struct ttm_resource *new_mem)
299 {
300         struct radeon_device *rdev;
301         struct radeon_bo *rbo;
302         struct ttm_resource *old_mem = &bo->mem;
303         int r;
304
305         r = ttm_bo_wait_ctx(bo, ctx);
306         if (r)
307                 return r;
308
309         /* Can't move a pinned BO */
310         rbo = container_of(bo, struct radeon_bo, tbo);
311         if (WARN_ON_ONCE(rbo->tbo.pin_count > 0))
312                 return -EINVAL;
313
314         rdev = radeon_get_rdev(bo->bdev);
315         if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
316                 ttm_bo_move_null(bo, new_mem);
317                 return 0;
318         }
319         if (old_mem->mem_type == TTM_PL_SYSTEM &&
320             new_mem->mem_type == TTM_PL_TT) {
321                 ttm_bo_move_null(bo, new_mem);
322                 return 0;
323         }
324
325         if (old_mem->mem_type == TTM_PL_TT &&
326             new_mem->mem_type == TTM_PL_SYSTEM) {
327                 r = ttm_bo_move_to_system(bo, ctx);
328                 if (r)
329                         return r;
330
331                 ttm_bo_assign_mem(bo, new_mem);
332                 return 0;
333         }
334         if (!rdev->ring[radeon_copy_ring_index(rdev)].ready ||
335             rdev->asic->copy.copy == NULL) {
336                 /* use memcpy */
337                 goto memcpy;
338         }
339
340         if (old_mem->mem_type == TTM_PL_VRAM &&
341             new_mem->mem_type == TTM_PL_SYSTEM) {
342                 r = radeon_move_vram_ram(bo, evict, ctx, new_mem);
343         } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
344                    new_mem->mem_type == TTM_PL_VRAM) {
345                 r = radeon_move_ram_vram(bo, evict, ctx, new_mem);
346         } else {
347                 r = radeon_move_blit(bo, evict,
348                                      new_mem, old_mem);
349         }
350
351         if (r) {
352 memcpy:
353                 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
354                 if (r) {
355                         return r;
356                 }
357         }
358
359         /* update statistics */
360         atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &rdev->num_bytes_moved);
361         return 0;
362 }
363
364 static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
365 {
366         struct radeon_device *rdev = radeon_get_rdev(bdev);
367         size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
368
369         switch (mem->mem_type) {
370         case TTM_PL_SYSTEM:
371                 /* system memory */
372                 return 0;
373         case TTM_PL_TT:
374 #if IS_ENABLED(CONFIG_AGP)
375                 if (rdev->flags & RADEON_IS_AGP) {
376                         /* RADEON_IS_AGP is set only if AGP is active */
377                         mem->bus.offset = (mem->start << PAGE_SHIFT) +
378                                 rdev->mc.agp_base;
379                         mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture;
380                         mem->bus.caching = ttm_write_combined;
381                 }
382 #endif
383                 break;
384         case TTM_PL_VRAM:
385                 mem->bus.offset = mem->start << PAGE_SHIFT;
386                 /* check if it's visible */
387                 if ((mem->bus.offset + bus_size) > rdev->mc.visible_vram_size)
388                         return -EINVAL;
389                 mem->bus.offset += rdev->mc.aper_base;
390                 mem->bus.is_iomem = true;
391                 mem->bus.caching = ttm_write_combined;
392 #ifdef __alpha__
393                 /*
394                  * Alpha: use bus.addr to hold the ioremap() return,
395                  * so we can modify bus.base below.
396                  */
397                 mem->bus.addr = ioremap_wc(mem->bus.offset, bus_size);
398                 if (!mem->bus.addr)
399                         return -ENOMEM;
400
401                 /*
402                  * Alpha: Use just the bus offset plus
403                  * the hose/domain memory base for bus.base.
404                  * It then can be used to build PTEs for VRAM
405                  * access, as done in ttm_bo_vm_fault().
406                  */
407                 mem->bus.offset = (mem->bus.offset & 0x0ffffffffUL) +
408                         rdev->ddev->hose->dense_mem_base;
409 #endif
410                 break;
411         default:
412                 return -EINVAL;
413         }
414         return 0;
415 }
416
417 /*
418  * TTM backend functions.
419  */
420 struct radeon_ttm_tt {
421         struct ttm_dma_tt               ttm;
422         u64                             offset;
423
424         uint64_t                        userptr;
425         struct mm_struct                *usermm;
426         uint32_t                        userflags;
427         bool bound;
428 };
429
430 /* prepare the sg table with the user pages */
431 static int radeon_ttm_tt_pin_userptr(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
432 {
433         struct radeon_device *rdev = radeon_get_rdev(bdev);
434         struct radeon_ttm_tt *gtt = (void *)ttm;
435         unsigned pinned = 0;
436         int r;
437
438         int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
439         enum dma_data_direction direction = write ?
440                 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
441
442         if (current->mm != gtt->usermm)
443                 return -EPERM;
444
445         if (gtt->userflags & RADEON_GEM_USERPTR_ANONONLY) {
446                 /* check that we only pin down anonymous memory
447                    to prevent problems with writeback */
448                 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
449                 struct vm_area_struct *vma;
450                 vma = find_vma(gtt->usermm, gtt->userptr);
451                 if (!vma || vma->vm_file || vma->vm_end < end)
452                         return -EPERM;
453         }
454
455         do {
456                 unsigned num_pages = ttm->num_pages - pinned;
457                 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
458                 struct page **pages = ttm->pages + pinned;
459
460                 r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
461                                    pages, NULL);
462                 if (r < 0)
463                         goto release_pages;
464
465                 pinned += r;
466
467         } while (pinned < ttm->num_pages);
468
469         r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
470                                       ttm->num_pages << PAGE_SHIFT,
471                                       GFP_KERNEL);
472         if (r)
473                 goto release_sg;
474
475         r = dma_map_sgtable(rdev->dev, ttm->sg, direction, 0);
476         if (r)
477                 goto release_sg;
478
479         drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
480                                          gtt->ttm.dma_address, ttm->num_pages);
481
482         return 0;
483
484 release_sg:
485         kfree(ttm->sg);
486
487 release_pages:
488         release_pages(ttm->pages, pinned);
489         return r;
490 }
491
492 static void radeon_ttm_tt_unpin_userptr(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
493 {
494         struct radeon_device *rdev = radeon_get_rdev(bdev);
495         struct radeon_ttm_tt *gtt = (void *)ttm;
496         struct sg_page_iter sg_iter;
497
498         int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
499         enum dma_data_direction direction = write ?
500                 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
501
502         /* double check that we don't free the table twice */
503         if (!ttm->sg->sgl)
504                 return;
505
506         /* free the sg table and pages again */
507         dma_unmap_sgtable(rdev->dev, ttm->sg, direction, 0);
508
509         for_each_sgtable_page(ttm->sg, &sg_iter, 0) {
510                 struct page *page = sg_page_iter_page(&sg_iter);
511                 if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY))
512                         set_page_dirty(page);
513
514                 mark_page_accessed(page);
515                 put_page(page);
516         }
517
518         sg_free_table(ttm->sg);
519 }
520
521 static bool radeon_ttm_backend_is_bound(struct ttm_tt *ttm)
522 {
523         struct radeon_ttm_tt *gtt = (void*)ttm;
524
525         return (gtt->bound);
526 }
527
528 static int radeon_ttm_backend_bind(struct ttm_bo_device *bdev,
529                                    struct ttm_tt *ttm,
530                                    struct ttm_resource *bo_mem)
531 {
532         struct radeon_ttm_tt *gtt = (void*)ttm;
533         struct radeon_device *rdev = radeon_get_rdev(bdev);
534         uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ |
535                 RADEON_GART_PAGE_WRITE;
536         int r;
537
538         if (gtt->bound)
539                 return 0;
540
541         if (gtt->userptr) {
542                 radeon_ttm_tt_pin_userptr(bdev, ttm);
543                 flags &= ~RADEON_GART_PAGE_WRITE;
544         }
545
546         gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
547         if (!ttm->num_pages) {
548                 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
549                      ttm->num_pages, bo_mem, ttm);
550         }
551         if (ttm->caching == ttm_cached)
552                 flags |= RADEON_GART_PAGE_SNOOP;
553         r = radeon_gart_bind(rdev, gtt->offset, ttm->num_pages,
554                              ttm->pages, gtt->ttm.dma_address, flags);
555         if (r) {
556                 DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
557                           ttm->num_pages, (unsigned)gtt->offset);
558                 return r;
559         }
560         gtt->bound = true;
561         return 0;
562 }
563
564 static void radeon_ttm_backend_unbind(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
565 {
566         struct radeon_ttm_tt *gtt = (void *)ttm;
567         struct radeon_device *rdev = radeon_get_rdev(bdev);
568
569         if (!gtt->bound)
570                 return;
571
572         radeon_gart_unbind(rdev, gtt->offset, ttm->num_pages);
573
574         if (gtt->userptr)
575                 radeon_ttm_tt_unpin_userptr(bdev, ttm);
576         gtt->bound = false;
577 }
578
579 static void radeon_ttm_backend_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
580 {
581         struct radeon_ttm_tt *gtt = (void *)ttm;
582
583         radeon_ttm_backend_unbind(bdev, ttm);
584         ttm_tt_destroy_common(bdev, ttm);
585
586         ttm_dma_tt_fini(&gtt->ttm);
587         kfree(gtt);
588 }
589
590 static struct ttm_tt *radeon_ttm_tt_create(struct ttm_buffer_object *bo,
591                                            uint32_t page_flags)
592 {
593         struct radeon_device *rdev;
594         struct radeon_ttm_tt *gtt;
595         enum ttm_caching caching;
596         struct radeon_bo *rbo;
597
598         rbo = container_of(bo, struct radeon_bo, tbo);
599
600         rdev = radeon_get_rdev(bo->bdev);
601 #if IS_ENABLED(CONFIG_AGP)
602         if (rdev->flags & RADEON_IS_AGP) {
603                 return ttm_agp_tt_create(bo, rdev->ddev->agp->bridge,
604                                          page_flags);
605         }
606 #endif
607
608         gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
609         if (gtt == NULL) {
610                 return NULL;
611         }
612
613         if (rbo->flags & RADEON_GEM_GTT_UC)
614                 caching = ttm_uncached;
615         else if (rbo->flags & RADEON_GEM_GTT_WC)
616                 caching = ttm_write_combined;
617         else
618                 caching = ttm_cached;
619
620         if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags, caching)) {
621                 kfree(gtt);
622                 return NULL;
623         }
624         return &gtt->ttm.ttm;
625 }
626
627 static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct radeon_device *rdev,
628                                                   struct ttm_tt *ttm)
629 {
630 #if IS_ENABLED(CONFIG_AGP)
631         if (rdev->flags & RADEON_IS_AGP)
632                 return NULL;
633 #endif
634
635         if (!ttm)
636                 return NULL;
637         return container_of(ttm, struct radeon_ttm_tt, ttm.ttm);
638 }
639
640 static int radeon_ttm_tt_populate(struct ttm_bo_device *bdev,
641                                   struct ttm_tt *ttm,
642                                   struct ttm_operation_ctx *ctx)
643 {
644         struct radeon_device *rdev = radeon_get_rdev(bdev);
645         struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
646         bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
647
648         if (gtt && gtt->userptr) {
649                 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
650                 if (!ttm->sg)
651                         return -ENOMEM;
652
653                 ttm->page_flags |= TTM_PAGE_FLAG_SG;
654                 ttm_tt_set_populated(ttm);
655                 return 0;
656         }
657
658         if (slave && ttm->sg) {
659                 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
660                                                  gtt->ttm.dma_address, ttm->num_pages);
661                 ttm_tt_set_populated(ttm);
662                 return 0;
663         }
664
665 #if IS_ENABLED(CONFIG_AGP)
666         if (rdev->flags & RADEON_IS_AGP) {
667                 return ttm_pool_populate(ttm, ctx);
668         }
669 #endif
670
671 #ifdef CONFIG_SWIOTLB
672         if (rdev->need_swiotlb && swiotlb_nr_tbl()) {
673                 return ttm_dma_populate(&gtt->ttm, rdev->dev, ctx);
674         }
675 #endif
676
677         return ttm_populate_and_map_pages(rdev->dev, &gtt->ttm, ctx);
678 }
679
680 static void radeon_ttm_tt_unpopulate(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
681 {
682         struct radeon_device *rdev = radeon_get_rdev(bdev);
683         struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
684         bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
685
686         if (gtt && gtt->userptr) {
687                 kfree(ttm->sg);
688                 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
689                 return;
690         }
691
692         if (slave)
693                 return;
694
695 #if IS_ENABLED(CONFIG_AGP)
696         if (rdev->flags & RADEON_IS_AGP) {
697                 ttm_pool_unpopulate(ttm);
698                 return;
699         }
700 #endif
701
702 #ifdef CONFIG_SWIOTLB
703         if (rdev->need_swiotlb && swiotlb_nr_tbl()) {
704                 ttm_dma_unpopulate(&gtt->ttm, rdev->dev);
705                 return;
706         }
707 #endif
708
709         ttm_unmap_and_unpopulate_pages(rdev->dev, &gtt->ttm);
710 }
711
712 int radeon_ttm_tt_set_userptr(struct radeon_device *rdev,
713                               struct ttm_tt *ttm, uint64_t addr,
714                               uint32_t flags)
715 {
716         struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
717
718         if (gtt == NULL)
719                 return -EINVAL;
720
721         gtt->userptr = addr;
722         gtt->usermm = current->mm;
723         gtt->userflags = flags;
724         return 0;
725 }
726
727 bool radeon_ttm_tt_is_bound(struct ttm_bo_device *bdev,
728                             struct ttm_tt *ttm)
729 {
730 #if IS_ENABLED(CONFIG_AGP)
731         struct radeon_device *rdev = radeon_get_rdev(bdev);
732         if (rdev->flags & RADEON_IS_AGP)
733                 return ttm_agp_is_bound(ttm);
734 #endif
735         return radeon_ttm_backend_is_bound(ttm);
736 }
737
738 static int radeon_ttm_tt_bind(struct ttm_bo_device *bdev,
739                               struct ttm_tt *ttm,
740                               struct ttm_resource *bo_mem)
741 {
742 #if IS_ENABLED(CONFIG_AGP)
743         struct radeon_device *rdev = radeon_get_rdev(bdev);
744 #endif
745
746         if (!bo_mem)
747                 return -EINVAL;
748 #if IS_ENABLED(CONFIG_AGP)
749         if (rdev->flags & RADEON_IS_AGP)
750                 return ttm_agp_bind(ttm, bo_mem);
751 #endif
752
753         return radeon_ttm_backend_bind(bdev, ttm, bo_mem);
754 }
755
756 static void radeon_ttm_tt_unbind(struct ttm_bo_device *bdev,
757                                  struct ttm_tt *ttm)
758 {
759 #if IS_ENABLED(CONFIG_AGP)
760         struct radeon_device *rdev = radeon_get_rdev(bdev);
761
762         if (rdev->flags & RADEON_IS_AGP) {
763                 ttm_agp_unbind(ttm);
764                 return;
765         }
766 #endif
767         radeon_ttm_backend_unbind(bdev, ttm);
768 }
769
770 static void radeon_ttm_tt_destroy(struct ttm_bo_device *bdev,
771                                   struct ttm_tt *ttm)
772 {
773 #if IS_ENABLED(CONFIG_AGP)
774         struct radeon_device *rdev = radeon_get_rdev(bdev);
775
776         if (rdev->flags & RADEON_IS_AGP) {
777                 ttm_agp_unbind(ttm);
778                 ttm_tt_destroy_common(bdev, ttm);
779                 ttm_agp_destroy(ttm);
780                 return;
781         }
782 #endif
783         radeon_ttm_backend_destroy(bdev, ttm);
784 }
785
786 bool radeon_ttm_tt_has_userptr(struct radeon_device *rdev,
787                                struct ttm_tt *ttm)
788 {
789         struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
790
791         if (gtt == NULL)
792                 return false;
793
794         return !!gtt->userptr;
795 }
796
797 bool radeon_ttm_tt_is_readonly(struct radeon_device *rdev,
798                                struct ttm_tt *ttm)
799 {
800         struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
801
802         if (gtt == NULL)
803                 return false;
804
805         return !!(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
806 }
807
808 static struct ttm_bo_driver radeon_bo_driver = {
809         .ttm_tt_create = &radeon_ttm_tt_create,
810         .ttm_tt_populate = &radeon_ttm_tt_populate,
811         .ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
812         .ttm_tt_bind = &radeon_ttm_tt_bind,
813         .ttm_tt_unbind = &radeon_ttm_tt_unbind,
814         .ttm_tt_destroy = &radeon_ttm_tt_destroy,
815         .eviction_valuable = ttm_bo_eviction_valuable,
816         .evict_flags = &radeon_evict_flags,
817         .move = &radeon_bo_move,
818         .verify_access = &radeon_verify_access,
819         .move_notify = &radeon_bo_move_notify,
820         .io_mem_reserve = &radeon_ttm_io_mem_reserve,
821 };
822
823 int radeon_ttm_init(struct radeon_device *rdev)
824 {
825         int r;
826
827         /* No others user of address space so set it to 0 */
828         r = ttm_bo_device_init(&rdev->mman.bdev,
829                                &radeon_bo_driver,
830                                rdev->ddev->anon_inode->i_mapping,
831                                rdev->ddev->vma_offset_manager,
832                                dma_addressing_limited(&rdev->pdev->dev));
833         if (r) {
834                 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
835                 return r;
836         }
837         rdev->mman.initialized = true;
838
839         r = radeon_ttm_init_vram(rdev);
840         if (r) {
841                 DRM_ERROR("Failed initializing VRAM heap.\n");
842                 return r;
843         }
844         /* Change the size here instead of the init above so only lpfn is affected */
845         radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
846
847         r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
848                              RADEON_GEM_DOMAIN_VRAM, 0, NULL,
849                              NULL, &rdev->stolen_vga_memory);
850         if (r) {
851                 return r;
852         }
853         r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
854         if (r)
855                 return r;
856         r = radeon_bo_pin(rdev->stolen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
857         radeon_bo_unreserve(rdev->stolen_vga_memory);
858         if (r) {
859                 radeon_bo_unref(&rdev->stolen_vga_memory);
860                 return r;
861         }
862         DRM_INFO("radeon: %uM of VRAM memory ready\n",
863                  (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
864
865         r = radeon_ttm_init_gtt(rdev);
866         if (r) {
867                 DRM_ERROR("Failed initializing GTT heap.\n");
868                 return r;
869         }
870         DRM_INFO("radeon: %uM of GTT memory ready.\n",
871                  (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
872
873         r = radeon_ttm_debugfs_init(rdev);
874         if (r) {
875                 DRM_ERROR("Failed to init debugfs\n");
876                 return r;
877         }
878         return 0;
879 }
880
881 void radeon_ttm_fini(struct radeon_device *rdev)
882 {
883         int r;
884
885         if (!rdev->mman.initialized)
886                 return;
887         radeon_ttm_debugfs_fini(rdev);
888         if (rdev->stolen_vga_memory) {
889                 r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
890                 if (r == 0) {
891                         radeon_bo_unpin(rdev->stolen_vga_memory);
892                         radeon_bo_unreserve(rdev->stolen_vga_memory);
893                 }
894                 radeon_bo_unref(&rdev->stolen_vga_memory);
895         }
896         ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_VRAM);
897         ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_TT);
898         ttm_bo_device_release(&rdev->mman.bdev);
899         radeon_gart_fini(rdev);
900         rdev->mman.initialized = false;
901         DRM_INFO("radeon: ttm finalized\n");
902 }
903
904 /* this should only be called at bootup or when userspace
905  * isn't running */
906 void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size)
907 {
908         struct ttm_resource_manager *man;
909
910         if (!rdev->mman.initialized)
911                 return;
912
913         man = ttm_manager_type(&rdev->mman.bdev, TTM_PL_VRAM);
914         /* this just adjusts TTM size idea, which sets lpfn to the correct value */
915         man->size = size >> PAGE_SHIFT;
916 }
917
918 static vm_fault_t radeon_ttm_fault(struct vm_fault *vmf)
919 {
920         struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
921         struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
922         vm_fault_t ret;
923
924         down_read(&rdev->pm.mclk_lock);
925
926         ret = ttm_bo_vm_reserve(bo, vmf);
927         if (ret)
928                 goto unlock_mclk;
929
930         ret = radeon_bo_fault_reserve_notify(bo);
931         if (ret)
932                 goto unlock_resv;
933
934         ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
935                                        TTM_BO_VM_NUM_PREFAULT, 1);
936         if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
937                 goto unlock_mclk;
938
939 unlock_resv:
940         dma_resv_unlock(bo->base.resv);
941
942 unlock_mclk:
943         up_read(&rdev->pm.mclk_lock);
944         return ret;
945 }
946
947 static struct vm_operations_struct radeon_ttm_vm_ops = {
948         .fault = radeon_ttm_fault,
949         .open = ttm_bo_vm_open,
950         .close = ttm_bo_vm_close,
951         .access = ttm_bo_vm_access
952 };
953
954 int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
955 {
956         int r;
957         struct drm_file *file_priv = filp->private_data;
958         struct radeon_device *rdev = file_priv->minor->dev->dev_private;
959
960         if (rdev == NULL)
961                 return -EINVAL;
962
963         r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
964         if (unlikely(r != 0))
965                 return r;
966
967         vma->vm_ops = &radeon_ttm_vm_ops;
968         return 0;
969 }
970
971 #if defined(CONFIG_DEBUG_FS)
972
973 static int radeon_mm_dump_table(struct seq_file *m, void *data)
974 {
975         struct drm_info_node *node = (struct drm_info_node *)m->private;
976         unsigned ttm_pl = *(int*)node->info_ent->data;
977         struct drm_device *dev = node->minor->dev;
978         struct radeon_device *rdev = dev->dev_private;
979         struct ttm_resource_manager *man = ttm_manager_type(&rdev->mman.bdev, ttm_pl);
980         struct drm_printer p = drm_seq_file_printer(m);
981
982         man->func->debug(man, &p);
983         return 0;
984 }
985
986
987 static int ttm_pl_vram = TTM_PL_VRAM;
988 static int ttm_pl_tt = TTM_PL_TT;
989
990 static struct drm_info_list radeon_ttm_debugfs_list[] = {
991         {"radeon_vram_mm", radeon_mm_dump_table, 0, &ttm_pl_vram},
992         {"radeon_gtt_mm", radeon_mm_dump_table, 0, &ttm_pl_tt},
993         {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
994 #ifdef CONFIG_SWIOTLB
995         {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
996 #endif
997 };
998
999 static int radeon_ttm_vram_open(struct inode *inode, struct file *filep)
1000 {
1001         struct radeon_device *rdev = inode->i_private;
1002         i_size_write(inode, rdev->mc.mc_vram_size);
1003         filep->private_data = inode->i_private;
1004         return 0;
1005 }
1006
1007 static ssize_t radeon_ttm_vram_read(struct file *f, char __user *buf,
1008                                     size_t size, loff_t *pos)
1009 {
1010         struct radeon_device *rdev = f->private_data;
1011         ssize_t result = 0;
1012         int r;
1013
1014         if (size & 0x3 || *pos & 0x3)
1015                 return -EINVAL;
1016
1017         while (size) {
1018                 unsigned long flags;
1019                 uint32_t value;
1020
1021                 if (*pos >= rdev->mc.mc_vram_size)
1022                         return result;
1023
1024                 spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
1025                 WREG32(RADEON_MM_INDEX, ((uint32_t)*pos) | 0x80000000);
1026                 if (rdev->family >= CHIP_CEDAR)
1027                         WREG32(EVERGREEN_MM_INDEX_HI, *pos >> 31);
1028                 value = RREG32(RADEON_MM_DATA);
1029                 spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
1030
1031                 r = put_user(value, (uint32_t *)buf);
1032                 if (r)
1033                         return r;
1034
1035                 result += 4;
1036                 buf += 4;
1037                 *pos += 4;
1038                 size -= 4;
1039         }
1040
1041         return result;
1042 }
1043
1044 static const struct file_operations radeon_ttm_vram_fops = {
1045         .owner = THIS_MODULE,
1046         .open = radeon_ttm_vram_open,
1047         .read = radeon_ttm_vram_read,
1048         .llseek = default_llseek
1049 };
1050
1051 static int radeon_ttm_gtt_open(struct inode *inode, struct file *filep)
1052 {
1053         struct radeon_device *rdev = inode->i_private;
1054         i_size_write(inode, rdev->mc.gtt_size);
1055         filep->private_data = inode->i_private;
1056         return 0;
1057 }
1058
1059 static ssize_t radeon_ttm_gtt_read(struct file *f, char __user *buf,
1060                                    size_t size, loff_t *pos)
1061 {
1062         struct radeon_device *rdev = f->private_data;
1063         ssize_t result = 0;
1064         int r;
1065
1066         while (size) {
1067                 loff_t p = *pos / PAGE_SIZE;
1068                 unsigned off = *pos & ~PAGE_MASK;
1069                 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1070                 struct page *page;
1071                 void *ptr;
1072
1073                 if (p >= rdev->gart.num_cpu_pages)
1074                         return result;
1075
1076                 page = rdev->gart.pages[p];
1077                 if (page) {
1078                         ptr = kmap(page);
1079                         ptr += off;
1080
1081                         r = copy_to_user(buf, ptr, cur_size);
1082                         kunmap(rdev->gart.pages[p]);
1083                 } else
1084                         r = clear_user(buf, cur_size);
1085
1086                 if (r)
1087                         return -EFAULT;
1088
1089                 result += cur_size;
1090                 buf += cur_size;
1091                 *pos += cur_size;
1092                 size -= cur_size;
1093         }
1094
1095         return result;
1096 }
1097
1098 static const struct file_operations radeon_ttm_gtt_fops = {
1099         .owner = THIS_MODULE,
1100         .open = radeon_ttm_gtt_open,
1101         .read = radeon_ttm_gtt_read,
1102         .llseek = default_llseek
1103 };
1104
1105 #endif
1106
1107 static int radeon_ttm_debugfs_init(struct radeon_device *rdev)
1108 {
1109 #if defined(CONFIG_DEBUG_FS)
1110         unsigned count;
1111
1112         struct drm_minor *minor = rdev->ddev->primary;
1113         struct dentry *root = minor->debugfs_root;
1114
1115         rdev->mman.vram = debugfs_create_file("radeon_vram", S_IFREG | S_IRUGO,
1116                                               root, rdev,
1117                                               &radeon_ttm_vram_fops);
1118
1119         rdev->mman.gtt = debugfs_create_file("radeon_gtt", S_IFREG | S_IRUGO,
1120                                              root, rdev, &radeon_ttm_gtt_fops);
1121
1122         count = ARRAY_SIZE(radeon_ttm_debugfs_list);
1123
1124 #ifdef CONFIG_SWIOTLB
1125         if (!(rdev->need_swiotlb && swiotlb_nr_tbl()))
1126                 --count;
1127 #endif
1128
1129         return radeon_debugfs_add_files(rdev, radeon_ttm_debugfs_list, count);
1130 #else
1131
1132         return 0;
1133 #endif
1134 }
1135
1136 static void radeon_ttm_debugfs_fini(struct radeon_device *rdev)
1137 {
1138 #if defined(CONFIG_DEBUG_FS)
1139
1140         debugfs_remove(rdev->mman.vram);
1141         rdev->mman.vram = NULL;
1142
1143         debugfs_remove(rdev->mman.gtt);
1144         rdev->mman.gtt = NULL;
1145 #endif
1146 }