radeon_ttm.c 25.7 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
#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_device.h>
#include <drm/drm_file.h>
#include <drm/drm_prime.h>
#include <drm/radeon_drm.h>
46 47
#include <drm/ttm/ttm_bo_api.h>
#include <drm/ttm/ttm_bo_driver.h>
48 49
#include <drm/ttm/ttm_placement.h>

50 51
#include "radeon_reg.h"
#include "radeon.h"
52
#include "radeon_ttm.h"
53

54
static void radeon_ttm_debugfs_init(struct radeon_device *rdev);
55

56
static int radeon_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
57
			      struct ttm_resource *bo_mem);
58
static void radeon_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm);
59

60
struct radeon_device *radeon_get_rdev(struct ttm_device *bdev)
61 62 63 64 65 66 67 68 69
{
	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;
}

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

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

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

92 93 94 95 96 97 98 99 100 101
	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);
102
	switch (bo->mem.mem_type) {
103
	case TTM_PL_VRAM:
104
		if (rbo->rdev->ring[radeon_copy_ring_index(rbo->rdev)].ready == false)
105
			radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
106 107 108 109 110 111 112 113 114 115 116 117 118 119
		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++) {
120
				if (rbo->placements[i].mem_type == TTM_PL_VRAM) {
121 122
					if (rbo->placements[i].fpfn < fpfn)
						rbo->placements[i].fpfn = fpfn;
123 124 125 126 127 128 129
				} else {
					rbo->placement.busy_placement =
						&rbo->placements[i];
					rbo->placement.num_busy_placement = 1;
				}
			}
		} else
130
			radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
131 132
		break;
	case TTM_PL_TT:
133
	default:
134
		radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
135
	}
136
	*placement = rbo->placement;
137 138 139 140
}

static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp)
{
141
	struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
142
	struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
143

144
	if (radeon_ttm_tt_has_userptr(rdev, bo->ttm))
145
		return -EPERM;
146
	return drm_vma_node_verify_access(&rbo->tbo.base.vma_node,
D
David Herrmann 已提交
147
					  filp->private_data);
148 149 150
}

static int radeon_move_blit(struct ttm_buffer_object *bo,
151
			bool evict,
152 153
			struct ttm_resource *new_mem,
			struct ttm_resource *old_mem)
154 155 156
{
	struct radeon_device *rdev;
	uint64_t old_start, new_start;
157
	struct radeon_fence *fence;
158
	unsigned num_pages;
159
	int r, ridx;
160 161

	rdev = radeon_get_rdev(bo->bdev);
162
	ridx = radeon_copy_ring_index(rdev);
163 164
	old_start = (u64)old_mem->start << PAGE_SHIFT;
	new_start = (u64)new_mem->start << PAGE_SHIFT;
165 166 167

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

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

195
	num_pages = new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
196
	fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->base.resv);
197 198 199
	if (IS_ERR(fence))
		return PTR_ERR(fence);

200
	r = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, false, new_mem);
201 202 203 204
	radeon_fence_unref(&fence);
	return r;
}

205 206
static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
			  struct ttm_operation_ctx *ctx,
207 208
			  struct ttm_resource *new_mem,
			  struct ttm_place *hop)
209 210
{
	struct radeon_device *rdev;
211
	struct radeon_bo *rbo;
212
	struct ttm_resource *old_mem = &bo->mem;
213 214
	int r;

215 216 217 218 219
	if (new_mem->mem_type == TTM_PL_TT) {
		r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, new_mem);
		if (r)
			return r;
	}
220

221
	r = ttm_bo_wait_ctx(bo, ctx);
222
	if (r)
223
		return r;
224

225 226
	/* Can't move a pinned BO */
	rbo = container_of(bo, struct radeon_bo, tbo);
227
	if (WARN_ON_ONCE(rbo->tbo.pin_count > 0))
228 229
		return -EINVAL;

230 231
	rdev = radeon_get_rdev(bo->bdev);
	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
232
		ttm_bo_move_null(bo, new_mem);
233
		goto out;
234
	}
235 236
	if (old_mem->mem_type == TTM_PL_SYSTEM &&
	    new_mem->mem_type == TTM_PL_TT) {
237
		ttm_bo_move_null(bo, new_mem);
238
		goto out;
239
	}
240 241

	if (old_mem->mem_type == TTM_PL_TT &&
242
	    new_mem->mem_type == TTM_PL_SYSTEM) {
243 244
		radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
		ttm_resource_free(bo, &bo->mem);
245
		ttm_bo_assign_mem(bo, new_mem);
246
		goto out;
247
	}
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	if (rdev->ring[radeon_copy_ring_index(rdev)].ready &&
	    rdev->asic->copy.copy != NULL) {
		if ((old_mem->mem_type == TTM_PL_SYSTEM &&
		     new_mem->mem_type == TTM_PL_VRAM) ||
		    (old_mem->mem_type == TTM_PL_VRAM &&
		     new_mem->mem_type == TTM_PL_SYSTEM)) {
			hop->fpfn = 0;
			hop->lpfn = 0;
			hop->mem_type = TTM_PL_TT;
			hop->flags = 0;
			return -EMULTIHOP;
		}

		r = radeon_move_blit(bo, evict, new_mem, old_mem);
	} else {
		r = -ENODEV;
264 265
	}

266
	if (r) {
267
		r = ttm_bo_move_memcpy(bo, ctx, new_mem);
268 269
		if (r)
			return r;
270
	}
271

272
out:
273
	/* update statistics */
274
	atomic64_add(bo->base.size, &rdev->num_bytes_moved);
275
	radeon_bo_move_notify(bo, evict, new_mem);
276
	return 0;
277 278
}

279
static int radeon_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
280 281
{
	struct radeon_device *rdev = radeon_get_rdev(bdev);
282
	size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
283 284 285 286 287 288

	switch (mem->mem_type) {
	case TTM_PL_SYSTEM:
		/* system memory */
		return 0;
	case TTM_PL_TT:
D
Daniel Vetter 已提交
289
#if IS_ENABLED(CONFIG_AGP)
290 291
		if (rdev->flags & RADEON_IS_AGP) {
			/* RADEON_IS_AGP is set only if AGP is active */
292 293
			mem->bus.offset = (mem->start << PAGE_SHIFT) +
				rdev->mc.agp_base;
294
			mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture;
295
			mem->bus.caching = ttm_write_combined;
296 297 298 299
		}
#endif
		break;
	case TTM_PL_VRAM:
300
		mem->bus.offset = mem->start << PAGE_SHIFT;
301
		/* check if it's visible */
302
		if ((mem->bus.offset + bus_size) > rdev->mc.visible_vram_size)
303
			return -EINVAL;
304
		mem->bus.offset += rdev->mc.aper_base;
305
		mem->bus.is_iomem = true;
306
		mem->bus.caching = ttm_write_combined;
307 308 309 310 311
#ifdef __alpha__
		/*
		 * Alpha: use bus.addr to hold the ioremap() return,
		 * so we can modify bus.base below.
		 */
312
		mem->bus.addr = ioremap_wc(mem->bus.offset, bus_size);
313 314
		if (!mem->bus.addr)
			return -ENOMEM;
315 316 317 318 319 320 321

		/*
		 * 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().
		 */
322
		mem->bus.offset = (mem->bus.offset & 0x0ffffffffUL) +
323
			rdev->hose->dense_mem_base;
324
#endif
325 326 327 328 329 330 331
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

332 333 334 335
/*
 * TTM backend functions.
 */
struct radeon_ttm_tt {
336
	struct ttm_tt		ttm;
337
	u64				offset;
338 339 340 341

	uint64_t			userptr;
	struct mm_struct		*usermm;
	uint32_t			userflags;
342
	bool bound;
343 344
};

345
/* prepare the sg table with the user pages */
346
static int radeon_ttm_tt_pin_userptr(struct ttm_device *bdev, struct ttm_tt *ttm)
347
{
D
Dave Airlie 已提交
348
	struct radeon_device *rdev = radeon_get_rdev(bdev);
349
	struct radeon_ttm_tt *gtt = (void *)ttm;
350
	unsigned pinned = 0;
351 352 353 354 355 356 357 358 359
	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;

360 361 362 363 364 365 366 367 368 369
	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;
	}

370 371 372 373 374
	do {
		unsigned num_pages = ttm->num_pages - pinned;
		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
		struct page **pages = ttm->pages + pinned;

375 376
		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
				   pages, NULL);
377 378 379 380 381 382 383 384 385 386 387 388 389
		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;

390 391
	r = dma_map_sgtable(rdev->dev, ttm->sg, direction, 0);
	if (r)
392 393
		goto release_sg;

394 395
	drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
				       ttm->num_pages);
396 397 398 399 400 401 402

	return 0;

release_sg:
	kfree(ttm->sg);

release_pages:
403
	release_pages(ttm->pages, pinned);
404 405 406
	return r;
}

407
static void radeon_ttm_tt_unpin_userptr(struct ttm_device *bdev, struct ttm_tt *ttm)
408
{
D
Dave Airlie 已提交
409
	struct radeon_device *rdev = radeon_get_rdev(bdev);
410
	struct radeon_ttm_tt *gtt = (void *)ttm;
411
	struct sg_page_iter sg_iter;
412 413 414 415 416

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

417 418 419 420
	/* double check that we don't free the table twice */
	if (!ttm->sg->sgl)
		return;

421
	/* free the sg table and pages again */
422
	dma_unmap_sgtable(rdev->dev, ttm->sg, direction, 0);
423

424
	for_each_sgtable_page(ttm->sg, &sg_iter, 0) {
425
		struct page *page = sg_page_iter_page(&sg_iter);
426 427 428 429
		if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY))
			set_page_dirty(page);

		mark_page_accessed(page);
430
		put_page(page);
431 432 433 434 435
	}

	sg_free_table(ttm->sg);
}

436 437 438 439 440 441 442
static bool radeon_ttm_backend_is_bound(struct ttm_tt *ttm)
{
	struct radeon_ttm_tt *gtt = (void*)ttm;

	return (gtt->bound);
}

443
static int radeon_ttm_backend_bind(struct ttm_device *bdev,
D
Dave Airlie 已提交
444
				   struct ttm_tt *ttm,
445
				   struct ttm_resource *bo_mem)
446
{
447
	struct radeon_ttm_tt *gtt = (void*)ttm;
D
Dave Airlie 已提交
448
	struct radeon_device *rdev = radeon_get_rdev(bdev);
449 450
	uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ |
		RADEON_GART_PAGE_WRITE;
451 452
	int r;

453 454 455
	if (gtt->bound)
		return 0;

456
	if (gtt->userptr) {
D
Dave Airlie 已提交
457
		radeon_ttm_tt_pin_userptr(bdev, ttm);
458 459 460
		flags &= ~RADEON_GART_PAGE_WRITE;
	}

461 462
	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
	if (!ttm->num_pages) {
463
		WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
464 465
		     ttm->num_pages, bo_mem, ttm);
	}
466
	if (ttm->caching == ttm_cached)
467
		flags |= RADEON_GART_PAGE_SNOOP;
468
	r = radeon_gart_bind(rdev, gtt->offset, ttm->num_pages,
469
			     ttm->pages, gtt->ttm.dma_address, flags);
470
	if (r) {
471
		DRM_ERROR("failed to bind %u pages at 0x%08X\n",
472 473 474
			  ttm->num_pages, (unsigned)gtt->offset);
		return r;
	}
475
	gtt->bound = true;
476 477 478
	return 0;
}

479
static void radeon_ttm_backend_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
480
{
481
	struct radeon_ttm_tt *gtt = (void *)ttm;
D
Dave Airlie 已提交
482
	struct radeon_device *rdev = radeon_get_rdev(bdev);
483

484 485 486
	if (gtt->userptr)
		radeon_ttm_tt_unpin_userptr(bdev, ttm);

487 488 489
	if (!gtt->bound)
		return;

490
	radeon_gart_unbind(rdev, gtt->offset, ttm->num_pages);
491

492
	gtt->bound = false;
493 494
}

495
static void radeon_ttm_backend_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
496
{
497
	struct radeon_ttm_tt *gtt = (void *)ttm;
498

499
	radeon_ttm_backend_unbind(bdev, ttm);
D
Dave Airlie 已提交
500 501
	ttm_tt_destroy_common(bdev, ttm);

502
	ttm_tt_fini(&gtt->ttm);
503 504 505
	kfree(gtt);
}

506 507
static struct ttm_tt *radeon_ttm_tt_create(struct ttm_buffer_object *bo,
					   uint32_t page_flags)
508 509
{
	struct radeon_ttm_tt *gtt;
510 511
	enum ttm_caching caching;
	struct radeon_bo *rbo;
D
Daniel Vetter 已提交
512
#if IS_ENABLED(CONFIG_AGP)
513 514
	struct radeon_device *rdev = radeon_get_rdev(bo->bdev);

515
	if (rdev->flags & RADEON_IS_AGP) {
516 517
		return ttm_agp_tt_create(bo, rdev->ddev->agp->bridge,
					 page_flags);
518 519
	}
#endif
520
	rbo = container_of(bo, struct radeon_bo, tbo);
521 522 523 524 525

	gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
	if (gtt == NULL) {
		return NULL;
	}
526 527 528 529 530 531 532 533

	if (rbo->flags & RADEON_GEM_GTT_UC)
		caching = ttm_uncached;
	else if (rbo->flags & RADEON_GEM_GTT_WC)
		caching = ttm_write_combined;
	else
		caching = ttm_cached;

534
	if (ttm_sg_tt_init(&gtt->ttm, bo, page_flags, caching)) {
535
		kfree(gtt);
536 537
		return NULL;
	}
538
	return &gtt->ttm;
539 540
}

541 542
static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct radeon_device *rdev,
						  struct ttm_tt *ttm)
543
{
544 545 546 547 548 549
#if IS_ENABLED(CONFIG_AGP)
	if (rdev->flags & RADEON_IS_AGP)
		return NULL;
#endif

	if (!ttm)
550
		return NULL;
551
	return container_of(ttm, struct radeon_ttm_tt, ttm);
552 553
}

554
static int radeon_ttm_tt_populate(struct ttm_device *bdev,
D
Dave Airlie 已提交
555 556
				  struct ttm_tt *ttm,
				  struct ttm_operation_ctx *ctx)
557
{
558 559
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
560
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
561

562
	if (gtt && gtt->userptr) {
563
		ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
564 565 566 567 568 569 570
		if (!ttm->sg)
			return -ENOMEM;

		ttm->page_flags |= TTM_PAGE_FLAG_SG;
		return 0;
	}

571
	if (slave && ttm->sg) {
572 573
		drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
					       ttm->num_pages);
574 575 576
		return 0;
	}

577
	return ttm_pool_alloc(&rdev->mman.bdev.pool, ttm, ctx);
578 579
}

580
static void radeon_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
581
{
582 583
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
584 585
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);

586
	if (gtt && gtt->userptr) {
587 588 589 590 591
		kfree(ttm->sg);
		ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
		return;
	}

592 593
	if (slave)
		return;
594

595
	return ttm_pool_free(&rdev->mman.bdev.pool, ttm);
596
}
597

598 599
int radeon_ttm_tt_set_userptr(struct radeon_device *rdev,
			      struct ttm_tt *ttm, uint64_t addr,
600 601
			      uint32_t flags)
{
602
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
603 604 605 606 607 608 609 610 611 612

	if (gtt == NULL)
		return -EINVAL;

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

613
bool radeon_ttm_tt_is_bound(struct ttm_device *bdev,
614 615 616 617 618 619 620 621 622 623
			    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);
}

624
static int radeon_ttm_tt_bind(struct ttm_device *bdev,
625 626 627
			      struct ttm_tt *ttm,
			      struct ttm_resource *bo_mem)
{
628
#if IS_ENABLED(CONFIG_AGP)
629
	struct radeon_device *rdev = radeon_get_rdev(bdev);
630
#endif
631

632 633
	if (!bo_mem)
		return -EINVAL;
634 635
#if IS_ENABLED(CONFIG_AGP)
	if (rdev->flags & RADEON_IS_AGP)
636
		return ttm_agp_bind(ttm, bo_mem);
637 638 639 640 641
#endif

	return radeon_ttm_backend_bind(bdev, ttm, bo_mem);
}

642
static void radeon_ttm_tt_unbind(struct ttm_device *bdev,
643 644 645 646 647 648
				 struct ttm_tt *ttm)
{
#if IS_ENABLED(CONFIG_AGP)
	struct radeon_device *rdev = radeon_get_rdev(bdev);

	if (rdev->flags & RADEON_IS_AGP) {
649
		ttm_agp_unbind(ttm);
650 651 652 653 654 655
		return;
	}
#endif
	radeon_ttm_backend_unbind(bdev, ttm);
}

656
static void radeon_ttm_tt_destroy(struct ttm_device *bdev,
657 658 659 660 661 662
				  struct ttm_tt *ttm)
{
#if IS_ENABLED(CONFIG_AGP)
	struct radeon_device *rdev = radeon_get_rdev(bdev);

	if (rdev->flags & RADEON_IS_AGP) {
663
		ttm_agp_unbind(ttm);
D
Dave Airlie 已提交
664
		ttm_tt_destroy_common(bdev, ttm);
665
		ttm_agp_destroy(ttm);
666 667 668 669 670 671 672 673
		return;
	}
#endif
	radeon_ttm_backend_destroy(bdev, ttm);
}

bool radeon_ttm_tt_has_userptr(struct radeon_device *rdev,
			       struct ttm_tt *ttm)
674
{
675
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
676 677 678 679 680 681 682

	if (gtt == NULL)
		return false;

	return !!gtt->userptr;
}

683 684
bool radeon_ttm_tt_is_readonly(struct radeon_device *rdev,
			       struct ttm_tt *ttm)
685
{
686
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
687 688 689 690 691 692 693

	if (gtt == NULL)
		return false;

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

694 695 696 697 698 699
static void
radeon_bo_delete_mem_notify(struct ttm_buffer_object *bo)
{
	radeon_bo_move_notify(bo, false, NULL);
}

700
static struct ttm_device_funcs radeon_bo_driver = {
701
	.ttm_tt_create = &radeon_ttm_tt_create,
702 703
	.ttm_tt_populate = &radeon_ttm_tt_populate,
	.ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
704
	.ttm_tt_destroy = &radeon_ttm_tt_destroy,
705
	.eviction_valuable = ttm_bo_eviction_valuable,
706 707 708
	.evict_flags = &radeon_evict_flags,
	.move = &radeon_bo_move,
	.verify_access = &radeon_verify_access,
709
	.delete_mem_notify = &radeon_bo_delete_mem_notify,
710
	.io_mem_reserve = &radeon_ttm_io_mem_reserve,
711 712 713 714 715 716 717
};

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

	/* No others user of address space so set it to 0 */
718
	r = ttm_device_init(&rdev->mman.bdev, &radeon_bo_driver, rdev->dev,
719
			       rdev->ddev->anon_inode->i_mapping,
720
			       rdev->ddev->vma_offset_manager,
721
			       rdev->need_swiotlb,
722
			       dma_addressing_limited(&rdev->pdev->dev));
723 724 725 726
	if (r) {
		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
		return r;
	}
727
	rdev->mman.initialized = true;
728 729

	r = radeon_ttm_init_vram(rdev);
730 731 732 733
	if (r) {
		DRM_ERROR("Failed initializing VRAM heap.\n");
		return r;
	}
734 735 736
	/* 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);

737
	r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
738
			     RADEON_GEM_DOMAIN_VRAM, 0, NULL,
K
Kent Russell 已提交
739
			     NULL, &rdev->stolen_vga_memory);
740 741 742
	if (r) {
		return r;
	}
K
Kent Russell 已提交
743
	r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
744 745
	if (r)
		return r;
K
Kent Russell 已提交
746 747
	r = radeon_bo_pin(rdev->stolen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
	radeon_bo_unreserve(rdev->stolen_vga_memory);
748
	if (r) {
K
Kent Russell 已提交
749
		radeon_bo_unref(&rdev->stolen_vga_memory);
750 751 752
		return r;
	}
	DRM_INFO("radeon: %uM of VRAM memory ready\n",
753
		 (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
754 755

	r = radeon_ttm_init_gtt(rdev);
756 757 758 759 760
	if (r) {
		DRM_ERROR("Failed initializing GTT heap.\n");
		return r;
	}
	DRM_INFO("radeon: %uM of GTT memory ready.\n",
761
		 (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
762

763 764
	radeon_ttm_debugfs_init(rdev);

765 766 767 768 769
	return 0;
}

void radeon_ttm_fini(struct radeon_device *rdev)
{
770 771
	int r;

772 773
	if (!rdev->mman.initialized)
		return;
N
Nirmoy Das 已提交
774

K
Kent Russell 已提交
775 776
	if (rdev->stolen_vga_memory) {
		r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
777
		if (r == 0) {
K
Kent Russell 已提交
778 779
			radeon_bo_unpin(rdev->stolen_vga_memory);
			radeon_bo_unreserve(rdev->stolen_vga_memory);
780
		}
K
Kent Russell 已提交
781
		radeon_bo_unref(&rdev->stolen_vga_memory);
782
	}
783 784
	ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_VRAM);
	ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_TT);
785
	ttm_device_fini(&rdev->mman.bdev);
786
	radeon_gart_fini(rdev);
787
	rdev->mman.initialized = false;
788 789 790
	DRM_INFO("radeon: ttm finalized\n");
}

791 792 793 794
/* 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)
{
795
	struct ttm_resource_manager *man;
796 797 798 799

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

800
	man = ttm_manager_type(&rdev->mman.bdev, TTM_PL_VRAM);
801 802 803 804
	/* this just adjusts TTM size idea, which sets lpfn to the correct value */
	man->size = size >> PAGE_SHIFT;
}

805
static vm_fault_t radeon_ttm_fault(struct vm_fault *vmf)
806
{
807 808
	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
	struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
809
	vm_fault_t ret;
810

811
	down_read(&rdev->pm.mclk_lock);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

	ret = ttm_bo_vm_reserve(bo, vmf);
	if (ret)
		goto unlock_mclk;

	ret = radeon_bo_fault_reserve_notify(bo);
	if (ret)
		goto unlock_resv;

	ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
				       TTM_BO_VM_NUM_PREFAULT, 1);
	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
		goto unlock_mclk;

unlock_resv:
	dma_resv_unlock(bo->base.resv);

unlock_mclk:
830
	up_read(&rdev->pm.mclk_lock);
831
	return ret;
832 833
}

834
static const struct vm_operations_struct radeon_ttm_vm_ops = {
835 836 837 838 839 840
	.fault = radeon_ttm_fault,
	.open = ttm_bo_vm_open,
	.close = ttm_bo_vm_close,
	.access = ttm_bo_vm_access
};

841 842 843
int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
{
	int r;
844 845
	struct drm_file *file_priv = filp->private_data;
	struct radeon_device *rdev = file_priv->minor->dev->dev_private;
846

847
	if (rdev == NULL)
848
		return -EINVAL;
849

850
	r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
851
	if (unlikely(r != 0))
852
		return r;
853

854 855 856 857
	vma->vm_ops = &radeon_ttm_vm_ops;
	return 0;
}

858
#if defined(CONFIG_DEBUG_FS)
859

860
static int radeon_mm_vram_dump_table_show(struct seq_file *m, void *unused)
861
{
862 863 864
	struct radeon_device *rdev = (struct radeon_device *)m->private;
	struct ttm_resource_manager *man = ttm_manager_type(&rdev->mman.bdev,
							    TTM_PL_VRAM);
D
Daniel Vetter 已提交
865
	struct drm_printer p = drm_seq_file_printer(m);
866

867
	man->func->debug(man, &p);
D
Daniel Vetter 已提交
868
	return 0;
869
}
870

871
static int radeon_ttm_page_pool_show(struct seq_file *m, void *data)
872
{
873
	struct radeon_device *rdev = (struct radeon_device *)m->private;
874 875 876

	return ttm_pool_debugfs(&rdev->mman.bdev.pool, m);
}
877

878 879 880 881 882 883
static int radeon_mm_gtt_dump_table_show(struct seq_file *m, void *unused)
{
	struct radeon_device *rdev = (struct radeon_device *)m->private;
	struct ttm_resource_manager *man = ttm_manager_type(&rdev->mman.bdev,
							    TTM_PL_TT);
	struct drm_printer p = drm_seq_file_printer(m);
884

885 886 887
	man->func->debug(man, &p);
	return 0;
}
888

889 890 891
DEFINE_SHOW_ATTRIBUTE(radeon_mm_vram_dump_table);
DEFINE_SHOW_ATTRIBUTE(radeon_mm_gtt_dump_table);
DEFINE_SHOW_ATTRIBUTE(radeon_ttm_page_pool);
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
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);

925
		r = put_user(value, (uint32_t __user *)buf);
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
		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
};

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
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;
963
		size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
964 965 966 967 968 969 970 971 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
		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
};

999 1000
#endif

1001
static void radeon_ttm_debugfs_init(struct radeon_device *rdev)
1002
{
1003
#if defined(CONFIG_DEBUG_FS)
1004
	struct drm_minor *minor = rdev->ddev->primary;
1005 1006
	struct dentry *root = minor->debugfs_root;

1007
	debugfs_create_file("radeon_vram", 0444, root, rdev,
N
Nirmoy Das 已提交
1008
			    &radeon_ttm_vram_fops);
1009

1010
	debugfs_create_file("radeon_gtt", 0444, root, rdev,
N
Nirmoy Das 已提交
1011
			    &radeon_ttm_gtt_fops);
1012

1013 1014 1015 1016 1017 1018
	debugfs_create_file("radeon_vram_mm", 0444, root, rdev,
			    &radeon_mm_vram_dump_table_fops);
	debugfs_create_file("radeon_gtt_mm", 0444, root, rdev,
			    &radeon_mm_gtt_dump_table_fops);
	debugfs_create_file("ttm_page_pool", 0444, root, rdev,
			    &radeon_ttm_page_pool_fops);
1019 1020
#endif
}