radeon_ttm.c 27.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright 2009 Jerome Glisse.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 */
/*
 * Authors:
 *    Jerome Glisse <glisse@freedesktop.org>
 *    Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 *    Dave Airlie
 */
32 33 34

#include <linux/dma-mapping.h>
#include <linux/pagemap.h>
35
#include <linux/pci.h>
36 37 38 39 40 41 42 43 44 45 46
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/swap.h>
#include <linux/swiotlb.h>

#include <drm/drm_agpsupport.h>
#include <drm/drm_debugfs.h>
#include <drm/drm_device.h>
#include <drm/drm_file.h>
#include <drm/drm_prime.h>
#include <drm/radeon_drm.h>
47 48 49 50
#include <drm/ttm/ttm_bo_api.h>
#include <drm/ttm/ttm_bo_driver.h>
#include <drm/ttm/ttm_module.h>
#include <drm/ttm/ttm_page_alloc.h>
51 52
#include <drm/ttm/ttm_placement.h>

53 54 55
#include "radeon_reg.h"
#include "radeon.h"

56
static int radeon_ttm_debugfs_init(struct radeon_device *rdev);
57
static void radeon_ttm_debugfs_fini(struct radeon_device *rdev);
58

59 60 61 62
static int radeon_ttm_tt_bind(struct ttm_bo_device *bdev,
			      struct ttm_tt *ttm,
			      struct ttm_resource *bo_mem);

63
struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev)
64 65 66 67 68 69 70 71 72
{
	struct radeon_mman *mman;
	struct radeon_device *rdev;

	mman = container_of(bdev, struct radeon_mman, bdev);
	rdev = container_of(mman, struct radeon_device, mman);
	return rdev;
}

73 74
static int radeon_ttm_init_vram(struct radeon_device *rdev)
{
75
	return ttm_range_man_init(&rdev->mman.bdev, TTM_PL_VRAM,
76
				  false, rdev->mc.real_vram_size >> PAGE_SHIFT);
77 78 79 80
}

static int radeon_ttm_init_gtt(struct radeon_device *rdev)
{
81
	return ttm_range_man_init(&rdev->mman.bdev, TTM_PL_TT,
82
				  true, rdev->mc.gtt_size >> PAGE_SHIFT);
83 84
}

85 86
static void radeon_evict_flags(struct ttm_buffer_object *bo,
				struct ttm_placement *placement)
87
{
88
	static const struct ttm_place placements = {
89 90
		.fpfn = 0,
		.lpfn = 0,
91 92
		.mem_type = TTM_PL_SYSTEM,
		.flags = TTM_PL_MASK_CACHING
93 94
	};

95 96 97 98 99 100 101 102 103 104
	struct radeon_bo *rbo;

	if (!radeon_ttm_bo_is_radeon_bo(bo)) {
		placement->placement = &placements;
		placement->busy_placement = &placements;
		placement->num_placement = 1;
		placement->num_busy_placement = 1;
		return;
	}
	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 110 111 112 113 114 115 116 117 118 119 120 121 122
		else if (rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size &&
			 bo->mem.start < (rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT)) {
			unsigned fpfn = rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
			int i;

			/* Try evicting to the CPU inaccessible part of VRAM
			 * first, but only set GTT as busy placement, so this
			 * BO will be evicted to GTT rather than causing other
			 * BOs to be evicted from VRAM
			 */
			radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM |
							 RADEON_GEM_DOMAIN_GTT);
			rbo->placement.num_busy_placement = 0;
			for (i = 0; i < rbo->placement.num_placement; i++) {
123
				if (rbo->placements[i].mem_type == TTM_PL_VRAM) {
124 125
					if (rbo->placements[i].fpfn < fpfn)
						rbo->placements[i].fpfn = fpfn;
126 127 128 129 130 131 132
				} else {
					rbo->placement.busy_placement =
						&rbo->placements[i];
					rbo->placement.num_busy_placement = 1;
				}
			}
		} else
133
			radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
134 135
		break;
	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 143
}

static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp)
{
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,
D
David Herrmann 已提交
150
					  filp->private_data);
151 152 153
}

static int radeon_move_blit(struct ttm_buffer_object *bo,
154
			bool evict,
155 156
			struct ttm_resource *new_mem,
			struct ttm_resource *old_mem)
157 158 159
{
	struct radeon_device *rdev;
	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 167
	old_start = (u64)old_mem->start << PAGE_SHIFT;
	new_start = (u64)new_mem->start << PAGE_SHIFT;
168 169 170

	switch (old_mem->mem_type) {
	case TTM_PL_VRAM:
171
		old_start += rdev->mc.vram_start;
172 173
		break;
	case TTM_PL_TT:
174
		old_start += rdev->mc.gtt_start;
175 176 177 178 179 180 181
		break;
	default:
		DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
		return -EINVAL;
	}
	switch (new_mem->mem_type) {
	case TTM_PL_VRAM:
182
		new_start += rdev->mc.vram_start;
183 184
		break;
	case TTM_PL_TT:
185
		new_start += rdev->mc.gtt_start;
186 187 188 189 190
		break;
	default:
		DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
		return -EINVAL;
	}
191
	if (!rdev->ring[ridx].ready) {
192
		DRM_ERROR("Trying to move memory with ring turned off.\n");
193 194
		return -EINVAL;
	}
195 196 197

	BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0);

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 201 202
	if (IS_ERR(fence))
		return PTR_ERR(fence);

203
	r = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, false, new_mem);
204 205 206 207 208
	radeon_fence_unref(&fence);
	return r;
}

static int radeon_move_vram_ram(struct ttm_buffer_object *bo,
209 210
				bool evict,
				struct ttm_operation_ctx *ctx,
211
				struct ttm_resource *new_mem)
212
{
213 214
	struct ttm_resource *old_mem = &bo->mem;
	struct ttm_resource tmp_mem;
215
	struct ttm_place placements;
216
	struct ttm_placement placement;
217 218 219 220
	int r;

	tmp_mem = *new_mem;
	tmp_mem.mm_node = NULL;
221 222 223 224
	placement.num_placement = 1;
	placement.placement = &placements;
	placement.num_busy_placement = 1;
	placement.busy_placement = &placements;
225 226
	placements.fpfn = 0;
	placements.lpfn = 0;
227 228
	placements.mem_type = TTM_PL_TT;
	placements.flags = TTM_PL_MASK_CACHING;
229
	r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
230 231 232
	if (unlikely(r)) {
		return r;
	}
233 234 235 236 237 238

	r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
	if (unlikely(r)) {
		goto out_cleanup;
	}

239
	r = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
240 241 242 243
	if (unlikely(r)) {
		goto out_cleanup;
	}

244
	r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, &tmp_mem);
245 246 247
	if (unlikely(r)) {
		goto out_cleanup;
	}
248
	r = radeon_move_blit(bo, true, &tmp_mem, old_mem);
249 250 251
	if (unlikely(r)) {
		goto out_cleanup;
	}
252
	r = ttm_bo_move_ttm(bo, ctx, new_mem);
253
out_cleanup:
254
	ttm_resource_free(bo, &tmp_mem);
255 256 257 258
	return r;
}

static int radeon_move_ram_vram(struct ttm_buffer_object *bo,
259 260
				bool evict,
				struct ttm_operation_ctx *ctx,
261
				struct ttm_resource *new_mem)
262
{
263 264
	struct ttm_resource *old_mem = &bo->mem;
	struct ttm_resource tmp_mem;
265
	struct ttm_placement placement;
266
	struct ttm_place placements;
267 268 269 270
	int r;

	tmp_mem = *new_mem;
	tmp_mem.mm_node = NULL;
271 272 273 274
	placement.num_placement = 1;
	placement.placement = &placements;
	placement.num_busy_placement = 1;
	placement.busy_placement = &placements;
275 276
	placements.fpfn = 0;
	placements.lpfn = 0;
277 278
	placements.mem_type = TTM_PL_TT;
	placements.flags = TTM_PL_MASK_CACHING;
279
	r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
280 281 282
	if (unlikely(r)) {
		return r;
	}
283
	r = ttm_bo_move_ttm(bo, ctx, &tmp_mem);
284 285 286
	if (unlikely(r)) {
		goto out_cleanup;
	}
287
	r = radeon_move_blit(bo, true, new_mem, old_mem);
288 289 290 291
	if (unlikely(r)) {
		goto out_cleanup;
	}
out_cleanup:
292
	ttm_resource_free(bo, &tmp_mem);
293 294 295
	return r;
}

296 297
static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
			  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 304
	int r;

305
	r = ttm_bo_wait_ctx(bo, ctx);
306 307 308
	if (r)
		return r;

309 310 311 312 313
	/* Can't move a pinned BO */
	rbo = container_of(bo, struct radeon_bo, tbo);
	if (WARN_ON_ONCE(rbo->pin_count > 0))
		return -EINVAL;

314 315
	rdev = radeon_get_rdev(bo->bdev);
	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
316
		ttm_bo_move_null(bo, new_mem);
317 318 319 320 321 322
		return 0;
	}
	if ((old_mem->mem_type == TTM_PL_TT &&
	     new_mem->mem_type == TTM_PL_SYSTEM) ||
	    (old_mem->mem_type == TTM_PL_SYSTEM &&
	     new_mem->mem_type == TTM_PL_TT)) {
323
		/* bind is enough */
324
		ttm_bo_move_null(bo, new_mem);
325 326
		return 0;
	}
327 328
	if (!rdev->ring[radeon_copy_ring_index(rdev)].ready ||
	    rdev->asic->copy.copy == NULL) {
329
		/* use memcpy */
330
		goto memcpy;
331 332 333 334
	}

	if (old_mem->mem_type == TTM_PL_VRAM &&
	    new_mem->mem_type == TTM_PL_SYSTEM) {
335
		r = radeon_move_vram_ram(bo, evict, ctx, new_mem);
336 337
	} else if (old_mem->mem_type == TTM_PL_SYSTEM &&
		   new_mem->mem_type == TTM_PL_VRAM) {
338
		r = radeon_move_ram_vram(bo, evict, ctx, new_mem);
339
	} else {
340
		r = radeon_move_blit(bo, evict,
341
				     new_mem, old_mem);
342
	}
343 344 345

	if (r) {
memcpy:
346
		r = ttm_bo_move_memcpy(bo, ctx, new_mem);
347 348 349
		if (r) {
			return r;
		}
350
	}
351 352 353 354

	/* update statistics */
	atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &rdev->num_bytes_moved);
	return 0;
355 356
}

357
static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
358 359
{
	struct radeon_device *rdev = radeon_get_rdev(bdev);
360
	size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
361 362 363 364 365 366

	switch (mem->mem_type) {
	case TTM_PL_SYSTEM:
		/* system memory */
		return 0;
	case TTM_PL_TT:
D
Daniel Vetter 已提交
367
#if IS_ENABLED(CONFIG_AGP)
368 369
		if (rdev->flags & RADEON_IS_AGP) {
			/* RADEON_IS_AGP is set only if AGP is active */
370 371
			mem->bus.offset = (mem->start << PAGE_SHIFT) +
				rdev->mc.agp_base;
372
			mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture;
373 374 375 376
		}
#endif
		break;
	case TTM_PL_VRAM:
377
		mem->bus.offset = mem->start << PAGE_SHIFT;
378
		/* check if it's visible */
379
		if ((mem->bus.offset + bus_size) > rdev->mc.visible_vram_size)
380
			return -EINVAL;
381
		mem->bus.offset += rdev->mc.aper_base;
382
		mem->bus.is_iomem = true;
383 384 385 386 387 388 389
#ifdef __alpha__
		/*
		 * Alpha: use bus.addr to hold the ioremap() return,
		 * so we can modify bus.base below.
		 */
		if (mem->placement & TTM_PL_FLAG_WC)
			mem->bus.addr =
390
				ioremap_wc(mem->bus.offset, bus_size);
391 392
		else
			mem->bus.addr =
393
				ioremap(mem->bus.offset, bus_size);
394 395
		if (!mem->bus.addr)
			return -ENOMEM;
396 397 398 399 400 401 402

		/*
		 * Alpha: Use just the bus offset plus
		 * the hose/domain memory base for bus.base.
		 * It then can be used to build PTEs for VRAM
		 * access, as done in ttm_bo_vm_fault().
		 */
403
		mem->bus.offset = (mem->bus.offset & 0x0ffffffffUL) +
404 405
			rdev->ddev->hose->dense_mem_base;
#endif
406 407 408 409 410 411 412
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

413 414 415 416
/*
 * TTM backend functions.
 */
struct radeon_ttm_tt {
417
	struct ttm_dma_tt		ttm;
418
	u64				offset;
419 420 421 422

	uint64_t			userptr;
	struct mm_struct		*usermm;
	uint32_t			userflags;
423
	bool bound;
424 425
};

426
/* prepare the sg table with the user pages */
D
Dave Airlie 已提交
427
static int radeon_ttm_tt_pin_userptr(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
428
{
D
Dave Airlie 已提交
429
	struct radeon_device *rdev = radeon_get_rdev(bdev);
430
	struct radeon_ttm_tt *gtt = (void *)ttm;
431
	unsigned pinned = 0;
432 433 434 435 436 437 438 439 440
	int r;

	int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
	enum dma_data_direction direction = write ?
		DMA_BIDIRECTIONAL : DMA_TO_DEVICE;

	if (current->mm != gtt->usermm)
		return -EPERM;

441 442 443 444 445 446 447 448 449 450
	if (gtt->userflags & RADEON_GEM_USERPTR_ANONONLY) {
		/* check that we only pin down anonymous memory
		   to prevent problems with writeback */
		unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
		struct vm_area_struct *vma;
		vma = find_vma(gtt->usermm, gtt->userptr);
		if (!vma || vma->vm_file || vma->vm_end < end)
			return -EPERM;
	}

451 452 453 454 455
	do {
		unsigned num_pages = ttm->num_pages - pinned;
		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
		struct page **pages = ttm->pages + pinned;

456 457
		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
				   pages, NULL);
458 459 460 461 462 463 464 465 466 467 468 469 470
		if (r < 0)
			goto release_pages;

		pinned += r;

	} while (pinned < ttm->num_pages);

	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
				      ttm->num_pages << PAGE_SHIFT,
				      GFP_KERNEL);
	if (r)
		goto release_sg;

471 472
	r = dma_map_sgtable(rdev->dev, ttm->sg, direction, 0);
	if (r)
473 474 475 476 477 478 479 480 481 482 483
		goto release_sg;

	drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
					 gtt->ttm.dma_address, ttm->num_pages);

	return 0;

release_sg:
	kfree(ttm->sg);

release_pages:
484
	release_pages(ttm->pages, pinned);
485 486 487
	return r;
}

D
Dave Airlie 已提交
488
static void radeon_ttm_tt_unpin_userptr(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
489
{
D
Dave Airlie 已提交
490
	struct radeon_device *rdev = radeon_get_rdev(bdev);
491
	struct radeon_ttm_tt *gtt = (void *)ttm;
492
	struct sg_page_iter sg_iter;
493 494 495 496 497

	int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
	enum dma_data_direction direction = write ?
		DMA_BIDIRECTIONAL : DMA_TO_DEVICE;

498 499 500 501
	/* double check that we don't free the table twice */
	if (!ttm->sg->sgl)
		return;

502
	/* free the sg table and pages again */
503
	dma_unmap_sgtable(rdev->dev, ttm->sg, direction, 0);
504

505
	for_each_sgtable_page(ttm->sg, &sg_iter, 0) {
506
		struct page *page = sg_page_iter_page(&sg_iter);
507 508 509 510
		if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY))
			set_page_dirty(page);

		mark_page_accessed(page);
511
		put_page(page);
512 513 514 515 516
	}

	sg_free_table(ttm->sg);
}

517 518 519 520 521 522 523
static bool radeon_ttm_backend_is_bound(struct ttm_tt *ttm)
{
	struct radeon_ttm_tt *gtt = (void*)ttm;

	return (gtt->bound);
}

D
Dave Airlie 已提交
524 525
static int radeon_ttm_backend_bind(struct ttm_bo_device *bdev,
				   struct ttm_tt *ttm,
526
				   struct ttm_resource *bo_mem)
527
{
528
	struct radeon_ttm_tt *gtt = (void*)ttm;
D
Dave Airlie 已提交
529
	struct radeon_device *rdev = radeon_get_rdev(bdev);
530 531
	uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ |
		RADEON_GART_PAGE_WRITE;
532 533
	int r;

534 535 536
	if (gtt->bound)
		return 0;

537
	if (gtt->userptr) {
D
Dave Airlie 已提交
538
		radeon_ttm_tt_pin_userptr(bdev, ttm);
539 540 541
		flags &= ~RADEON_GART_PAGE_WRITE;
	}

542 543 544 545 546
	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
	if (!ttm->num_pages) {
		WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
		     ttm->num_pages, bo_mem, ttm);
	}
547 548
	if (ttm->caching_state == tt_cached)
		flags |= RADEON_GART_PAGE_SNOOP;
549
	r = radeon_gart_bind(rdev, gtt->offset, ttm->num_pages,
550
			     ttm->pages, gtt->ttm.dma_address, flags);
551 552 553 554 555
	if (r) {
		DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
			  ttm->num_pages, (unsigned)gtt->offset);
		return r;
	}
556
	gtt->bound = true;
557 558 559
	return 0;
}

D
Dave Airlie 已提交
560
static void radeon_ttm_backend_unbind(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
561
{
562
	struct radeon_ttm_tt *gtt = (void *)ttm;
D
Dave Airlie 已提交
563
	struct radeon_device *rdev = radeon_get_rdev(bdev);
564

565 566 567
	if (!gtt->bound)
		return;

568
	radeon_gart_unbind(rdev, gtt->offset, ttm->num_pages);
569 570

	if (gtt->userptr)
D
Dave Airlie 已提交
571
		radeon_ttm_tt_unpin_userptr(bdev, ttm);
572
	gtt->bound = false;
573 574
}

D
Dave Airlie 已提交
575
static void radeon_ttm_backend_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
576
{
577
	struct radeon_ttm_tt *gtt = (void *)ttm;
578

579
	radeon_ttm_backend_unbind(bdev, ttm);
D
Dave Airlie 已提交
580 581
	ttm_tt_destroy_common(bdev, ttm);

582
	ttm_dma_tt_fini(&gtt->ttm);
583 584 585
	kfree(gtt);
}

586 587
static struct ttm_tt *radeon_ttm_tt_create(struct ttm_buffer_object *bo,
					   uint32_t page_flags)
588 589 590 591
{
	struct radeon_device *rdev;
	struct radeon_ttm_tt *gtt;

592
	rdev = radeon_get_rdev(bo->bdev);
D
Daniel Vetter 已提交
593
#if IS_ENABLED(CONFIG_AGP)
594
	if (rdev->flags & RADEON_IS_AGP) {
595 596
		return ttm_agp_tt_create(bo, rdev->ddev->agp->bridge,
					 page_flags);
597 598 599 600 601 602 603
	}
#endif

	gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
	if (gtt == NULL) {
		return NULL;
	}
604
	if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags)) {
605
		kfree(gtt);
606 607
		return NULL;
	}
608
	return &gtt->ttm.ttm;
609 610
}

611 612
static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct radeon_device *rdev,
						  struct ttm_tt *ttm)
613
{
614 615 616 617 618 619
#if IS_ENABLED(CONFIG_AGP)
	if (rdev->flags & RADEON_IS_AGP)
		return NULL;
#endif

	if (!ttm)
620
		return NULL;
621
	return container_of(ttm, struct radeon_ttm_tt, ttm.ttm);
622 623
}

D
Dave Airlie 已提交
624 625 626
static int radeon_ttm_tt_populate(struct ttm_bo_device *bdev,
				  struct ttm_tt *ttm,
				  struct ttm_operation_ctx *ctx)
627
{
628 629
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
630
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
631

632
	if (gtt && gtt->userptr) {
633
		ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
634 635 636 637
		if (!ttm->sg)
			return -ENOMEM;

		ttm->page_flags |= TTM_PAGE_FLAG_SG;
638
		ttm_tt_set_populated(ttm);
639 640 641
		return 0;
	}

642 643 644
	if (slave && ttm->sg) {
		drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
						 gtt->ttm.dma_address, ttm->num_pages);
645
		ttm_tt_set_populated(ttm);
646 647 648
		return 0;
	}

D
Daniel Vetter 已提交
649
#if IS_ENABLED(CONFIG_AGP)
J
Jerome Glisse 已提交
650
	if (rdev->flags & RADEON_IS_AGP) {
651
		return ttm_pool_populate(ttm, ctx);
J
Jerome Glisse 已提交
652 653
	}
#endif
654 655

#ifdef CONFIG_SWIOTLB
656
	if (rdev->need_swiotlb && swiotlb_nr_tbl()) {
657
		return ttm_dma_populate(&gtt->ttm, rdev->dev, ctx);
658 659 660
	}
#endif

661
	return ttm_populate_and_map_pages(rdev->dev, &gtt->ttm, ctx);
662 663
}

D
Dave Airlie 已提交
664
static void radeon_ttm_tt_unpopulate(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
665
{
666 667
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
668 669
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);

670
	if (gtt && gtt->userptr) {
671 672 673 674 675
		kfree(ttm->sg);
		ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
		return;
	}

676 677
	if (slave)
		return;
678

D
Daniel Vetter 已提交
679
#if IS_ENABLED(CONFIG_AGP)
J
Jerome Glisse 已提交
680
	if (rdev->flags & RADEON_IS_AGP) {
681
		ttm_pool_unpopulate(ttm);
J
Jerome Glisse 已提交
682 683 684
		return;
	}
#endif
685 686

#ifdef CONFIG_SWIOTLB
687
	if (rdev->need_swiotlb && swiotlb_nr_tbl()) {
688
		ttm_dma_unpopulate(&gtt->ttm, rdev->dev);
689 690 691 692
		return;
	}
#endif

693
	ttm_unmap_and_unpopulate_pages(rdev->dev, &gtt->ttm);
694
}
695

696 697
int radeon_ttm_tt_set_userptr(struct radeon_device *rdev,
			      struct ttm_tt *ttm, uint64_t addr,
698 699
			      uint32_t flags)
{
700
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
701 702 703 704 705 706 707 708 709 710

	if (gtt == NULL)
		return -EINVAL;

	gtt->userptr = addr;
	gtt->usermm = current->mm;
	gtt->userflags = flags;
	return 0;
}

711 712 713 714 715 716 717 718 719 720 721
bool radeon_ttm_tt_is_bound(struct ttm_bo_device *bdev,
			    struct ttm_tt *ttm)
{
#if IS_ENABLED(CONFIG_AGP)
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	if (rdev->flags & RADEON_IS_AGP)
		return ttm_agp_is_bound(ttm);
#endif
	return radeon_ttm_backend_is_bound(ttm);
}

722 723 724 725
static int radeon_ttm_tt_bind(struct ttm_bo_device *bdev,
			      struct ttm_tt *ttm,
			      struct ttm_resource *bo_mem)
{
726
#if IS_ENABLED(CONFIG_AGP)
727
	struct radeon_device *rdev = radeon_get_rdev(bdev);
728
#endif
729

730 731
	if (!bo_mem)
		return -EINVAL;
732 733
#if IS_ENABLED(CONFIG_AGP)
	if (rdev->flags & RADEON_IS_AGP)
734
		return ttm_agp_bind(ttm, bo_mem);
735 736 737 738 739 740 741 742 743 744 745 746
#endif

	return radeon_ttm_backend_bind(bdev, ttm, bo_mem);
}

static void radeon_ttm_tt_unbind(struct ttm_bo_device *bdev,
				 struct ttm_tt *ttm)
{
#if IS_ENABLED(CONFIG_AGP)
	struct radeon_device *rdev = radeon_get_rdev(bdev);

	if (rdev->flags & RADEON_IS_AGP) {
747
		ttm_agp_unbind(ttm);
748 749 750 751 752 753 754 755 756 757 758 759 760
		return;
	}
#endif
	radeon_ttm_backend_unbind(bdev, ttm);
}

static void radeon_ttm_tt_destroy(struct ttm_bo_device *bdev,
				  struct ttm_tt *ttm)
{
#if IS_ENABLED(CONFIG_AGP)
	struct radeon_device *rdev = radeon_get_rdev(bdev);

	if (rdev->flags & RADEON_IS_AGP) {
761
		ttm_agp_unbind(ttm);
D
Dave Airlie 已提交
762
		ttm_tt_destroy_common(bdev, ttm);
763
		ttm_agp_destroy(ttm);
764 765 766 767 768 769 770 771
		return;
	}
#endif
	radeon_ttm_backend_destroy(bdev, ttm);
}

bool radeon_ttm_tt_has_userptr(struct radeon_device *rdev,
			       struct ttm_tt *ttm)
772
{
773
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
774 775 776 777 778 779 780

	if (gtt == NULL)
		return false;

	return !!gtt->userptr;
}

781 782
bool radeon_ttm_tt_is_readonly(struct radeon_device *rdev,
			       struct ttm_tt *ttm)
783
{
784
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
785 786 787 788 789 790 791

	if (gtt == NULL)
		return false;

	return !!(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
}

792
static struct ttm_bo_driver radeon_bo_driver = {
793
	.ttm_tt_create = &radeon_ttm_tt_create,
794 795
	.ttm_tt_populate = &radeon_ttm_tt_populate,
	.ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
796 797 798
	.ttm_tt_bind = &radeon_ttm_tt_bind,
	.ttm_tt_unbind = &radeon_ttm_tt_unbind,
	.ttm_tt_destroy = &radeon_ttm_tt_destroy,
799
	.eviction_valuable = ttm_bo_eviction_valuable,
800 801 802
	.evict_flags = &radeon_evict_flags,
	.move = &radeon_bo_move,
	.verify_access = &radeon_verify_access,
803 804
	.move_notify = &radeon_bo_move_notify,
	.fault_reserve_notify = &radeon_bo_fault_reserve_notify,
805
	.io_mem_reserve = &radeon_ttm_io_mem_reserve,
806 807 808 809 810 811 812 813
};

int radeon_ttm_init(struct radeon_device *rdev)
{
	int r;

	/* No others user of address space so set it to 0 */
	r = ttm_bo_device_init(&rdev->mman.bdev,
814 815
			       &radeon_bo_driver,
			       rdev->ddev->anon_inode->i_mapping,
816
			       rdev->ddev->vma_offset_manager,
817
			       dma_addressing_limited(&rdev->pdev->dev));
818 819 820 821
	if (r) {
		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
		return r;
	}
822
	rdev->mman.initialized = true;
823 824

	r = radeon_ttm_init_vram(rdev);
825 826 827 828
	if (r) {
		DRM_ERROR("Failed initializing VRAM heap.\n");
		return r;
	}
829 830 831
	/* Change the size here instead of the init above so only lpfn is affected */
	radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);

832
	r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
833
			     RADEON_GEM_DOMAIN_VRAM, 0, NULL,
K
Kent Russell 已提交
834
			     NULL, &rdev->stolen_vga_memory);
835 836 837
	if (r) {
		return r;
	}
K
Kent Russell 已提交
838
	r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
839 840
	if (r)
		return r;
K
Kent Russell 已提交
841 842
	r = radeon_bo_pin(rdev->stolen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
	radeon_bo_unreserve(rdev->stolen_vga_memory);
843
	if (r) {
K
Kent Russell 已提交
844
		radeon_bo_unref(&rdev->stolen_vga_memory);
845 846 847
		return r;
	}
	DRM_INFO("radeon: %uM of VRAM memory ready\n",
848
		 (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
849 850

	r = radeon_ttm_init_gtt(rdev);
851 852 853 854 855
	if (r) {
		DRM_ERROR("Failed initializing GTT heap.\n");
		return r;
	}
	DRM_INFO("radeon: %uM of GTT memory ready.\n",
856
		 (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
857 858 859 860 861 862

	r = radeon_ttm_debugfs_init(rdev);
	if (r) {
		DRM_ERROR("Failed to init debugfs\n");
		return r;
	}
863 864 865 866 867
	return 0;
}

void radeon_ttm_fini(struct radeon_device *rdev)
{
868 869
	int r;

870 871
	if (!rdev->mman.initialized)
		return;
872
	radeon_ttm_debugfs_fini(rdev);
K
Kent Russell 已提交
873 874
	if (rdev->stolen_vga_memory) {
		r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
875
		if (r == 0) {
K
Kent Russell 已提交
876 877
			radeon_bo_unpin(rdev->stolen_vga_memory);
			radeon_bo_unreserve(rdev->stolen_vga_memory);
878
		}
K
Kent Russell 已提交
879
		radeon_bo_unref(&rdev->stolen_vga_memory);
880
	}
881 882
	ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_VRAM);
	ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_TT);
883 884
	ttm_bo_device_release(&rdev->mman.bdev);
	radeon_gart_fini(rdev);
885
	rdev->mman.initialized = false;
886 887 888
	DRM_INFO("radeon: ttm finalized\n");
}

889 890 891 892
/* this should only be called at bootup or when userspace
 * isn't running */
void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size)
{
893
	struct ttm_resource_manager *man;
894 895 896 897

	if (!rdev->mman.initialized)
		return;

898
	man = ttm_manager_type(&rdev->mman.bdev, TTM_PL_VRAM);
899 900 901 902
	/* this just adjusts TTM size idea, which sets lpfn to the correct value */
	man->size = size >> PAGE_SHIFT;
}

903
static vm_fault_t radeon_ttm_fault(struct vm_fault *vmf)
904 905
{
	struct ttm_buffer_object *bo;
906
	struct radeon_device *rdev;
907
	vm_fault_t ret;
908

909
	bo = (struct ttm_buffer_object *)vmf->vma->vm_private_data;
910
	if (bo == NULL)
911
		return VM_FAULT_NOPAGE;
912

913
	rdev = radeon_get_rdev(bo->bdev);
914
	down_read(&rdev->pm.mclk_lock);
915
	ret = ttm_bo_vm_fault(vmf);
916
	up_read(&rdev->pm.mclk_lock);
917
	return ret;
918 919
}

920 921 922 923 924 925 926
static struct vm_operations_struct radeon_ttm_vm_ops = {
	.fault = radeon_ttm_fault,
	.open = ttm_bo_vm_open,
	.close = ttm_bo_vm_close,
	.access = ttm_bo_vm_access
};

927 928 929
int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
{
	int r;
930 931
	struct drm_file *file_priv = filp->private_data;
	struct radeon_device *rdev = file_priv->minor->dev->dev_private;
932

933
	if (rdev == NULL)
934
		return -EINVAL;
935

936
	r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
937
	if (unlikely(r != 0))
938
		return r;
939

940 941 942 943
	vma->vm_ops = &radeon_ttm_vm_ops;
	return 0;
}

944
#if defined(CONFIG_DEBUG_FS)
945

946 947 948
static int radeon_mm_dump_table(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
949
	unsigned ttm_pl = *(int*)node->info_ent->data;
950 951
	struct drm_device *dev = node->minor->dev;
	struct radeon_device *rdev = dev->dev_private;
952
	struct ttm_resource_manager *man = ttm_manager_type(&rdev->mman.bdev, ttm_pl);
D
Daniel Vetter 已提交
953
	struct drm_printer p = drm_seq_file_printer(m);
954

955
	man->func->debug(man, &p);
D
Daniel Vetter 已提交
956
	return 0;
957
}
958

959

960 961 962 963 964 965 966 967 968 969 970 971
static int ttm_pl_vram = TTM_PL_VRAM;
static int ttm_pl_tt = TTM_PL_TT;

static struct drm_info_list radeon_ttm_debugfs_list[] = {
	{"radeon_vram_mm", radeon_mm_dump_table, 0, &ttm_pl_vram},
	{"radeon_gtt_mm", radeon_mm_dump_table, 0, &ttm_pl_tt},
	{"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
#ifdef CONFIG_SWIOTLB
	{"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
#endif
};

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
static int radeon_ttm_vram_open(struct inode *inode, struct file *filep)
{
	struct radeon_device *rdev = inode->i_private;
	i_size_write(inode, rdev->mc.mc_vram_size);
	filep->private_data = inode->i_private;
	return 0;
}

static ssize_t radeon_ttm_vram_read(struct file *f, char __user *buf,
				    size_t size, loff_t *pos)
{
	struct radeon_device *rdev = f->private_data;
	ssize_t result = 0;
	int r;

	if (size & 0x3 || *pos & 0x3)
		return -EINVAL;

	while (size) {
		unsigned long flags;
		uint32_t value;

		if (*pos >= rdev->mc.mc_vram_size)
			return result;

		spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
		WREG32(RADEON_MM_INDEX, ((uint32_t)*pos) | 0x80000000);
		if (rdev->family >= CHIP_CEDAR)
			WREG32(EVERGREEN_MM_INDEX_HI, *pos >> 31);
		value = RREG32(RADEON_MM_DATA);
		spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);

		r = put_user(value, (uint32_t *)buf);
		if (r)
			return r;

		result += 4;
		buf += 4;
		*pos += 4;
		size -= 4;
	}

	return result;
}

static const struct file_operations radeon_ttm_vram_fops = {
	.owner = THIS_MODULE,
	.open = radeon_ttm_vram_open,
	.read = radeon_ttm_vram_read,
	.llseek = default_llseek
};

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
static int radeon_ttm_gtt_open(struct inode *inode, struct file *filep)
{
	struct radeon_device *rdev = inode->i_private;
	i_size_write(inode, rdev->mc.gtt_size);
	filep->private_data = inode->i_private;
	return 0;
}

static ssize_t radeon_ttm_gtt_read(struct file *f, char __user *buf,
				   size_t size, loff_t *pos)
{
	struct radeon_device *rdev = f->private_data;
	ssize_t result = 0;
	int r;

	while (size) {
		loff_t p = *pos / PAGE_SIZE;
		unsigned off = *pos & ~PAGE_MASK;
1042
		size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		struct page *page;
		void *ptr;

		if (p >= rdev->gart.num_cpu_pages)
			return result;

		page = rdev->gart.pages[p];
		if (page) {
			ptr = kmap(page);
			ptr += off;

			r = copy_to_user(buf, ptr, cur_size);
			kunmap(rdev->gart.pages[p]);
		} else
			r = clear_user(buf, cur_size);

		if (r)
			return -EFAULT;

		result += cur_size;
		buf += cur_size;
		*pos += cur_size;
		size -= cur_size;
	}

	return result;
}

static const struct file_operations radeon_ttm_gtt_fops = {
	.owner = THIS_MODULE,
	.open = radeon_ttm_gtt_open,
	.read = radeon_ttm_gtt_read,
	.llseek = default_llseek
};

1078 1079 1080 1081
#endif

static int radeon_ttm_debugfs_init(struct radeon_device *rdev)
{
1082
#if defined(CONFIG_DEBUG_FS)
1083 1084 1085
	unsigned count;

	struct drm_minor *minor = rdev->ddev->primary;
1086 1087 1088 1089 1090 1091 1092 1093
	struct dentry *root = minor->debugfs_root;

	rdev->mman.vram = debugfs_create_file("radeon_vram", S_IFREG | S_IRUGO,
					      root, rdev,
					      &radeon_ttm_vram_fops);

	rdev->mman.gtt = debugfs_create_file("radeon_gtt", S_IFREG | S_IRUGO,
					     root, rdev, &radeon_ttm_gtt_fops);
1094

1095
	count = ARRAY_SIZE(radeon_ttm_debugfs_list);
1096

1097
#ifdef CONFIG_SWIOTLB
1098
	if (!(rdev->need_swiotlb && swiotlb_nr_tbl()))
1099
		--count;
1100
#endif
1101

1102 1103 1104
	return radeon_debugfs_add_files(rdev, radeon_ttm_debugfs_list, count);
#else

1105
	return 0;
1106
#endif
1107
}
1108 1109 1110 1111 1112 1113 1114

static void radeon_ttm_debugfs_fini(struct radeon_device *rdev)
{
#if defined(CONFIG_DEBUG_FS)

	debugfs_remove(rdev->mman.vram);
	rdev->mman.vram = NULL;
1115 1116 1117

	debugfs_remove(rdev->mman.gtt);
	rdev->mman.gtt = NULL;
1118 1119
#endif
}