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