radeon_ttm.c 28.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
static int radeon_ttm_tt_bind(struct ttm_bo_device *bdev,
			      struct ttm_tt *ttm,
			      struct ttm_resource *bo_mem);
62 63
static void radeon_ttm_tt_unbind(struct ttm_bo_device *bdev,
				 struct ttm_tt *ttm);
64

65
struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev)
66 67 68 69 70 71 72 73 74
{
	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;
}

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

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

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

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

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

149
	if (radeon_ttm_tt_has_userptr(rdev, bo->ttm))
150
		return -EPERM;
151
	return drm_vma_node_verify_access(&rbo->tbo.base.vma_node,
D
David Herrmann 已提交
152
					  filp->private_data);
153 154 155
}

static int radeon_move_blit(struct ttm_buffer_object *bo,
156
			bool evict,
157 158
			struct ttm_resource *new_mem,
			struct ttm_resource *old_mem)
159 160 161
{
	struct radeon_device *rdev;
	uint64_t old_start, new_start;
162
	struct radeon_fence *fence;
163
	unsigned num_pages;
164
	int r, ridx;
165 166

	rdev = radeon_get_rdev(bo->bdev);
167
	ridx = radeon_copy_ring_index(rdev);
168 169
	old_start = (u64)old_mem->start << PAGE_SHIFT;
	new_start = (u64)new_mem->start << PAGE_SHIFT;
170 171 172

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

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

200
	num_pages = new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
201
	fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->base.resv);
202 203 204
	if (IS_ERR(fence))
		return PTR_ERR(fence);

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

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

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

236
	r = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
237 238 239 240
	if (unlikely(r)) {
		goto out_cleanup;
	}

241
	r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, &tmp_mem);
242 243 244
	if (unlikely(r)) {
		goto out_cleanup;
	}
245
	r = radeon_move_blit(bo, true, &tmp_mem, old_mem);
246 247 248
	if (unlikely(r)) {
		goto out_cleanup;
	}
249
	r = ttm_bo_wait_ctx(bo, ctx);
250 251 252
	if (unlikely(r))
		goto out_cleanup;

253 254
	radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
	ttm_resource_free(bo, &bo->mem);
255
	ttm_bo_assign_mem(bo, new_mem);
256
out_cleanup:
257
	ttm_resource_free(bo, &tmp_mem);
258 259 260 261
	return r;
}

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

	tmp_mem = *new_mem;
	tmp_mem.mm_node = NULL;
274 275 276 277
	placement.num_placement = 1;
	placement.placement = &placements;
	placement.num_busy_placement = 1;
	placement.busy_placement = &placements;
278 279
	placements.fpfn = 0;
	placements.lpfn = 0;
280
	placements.mem_type = TTM_PL_TT;
281
	placements.flags = 0;
282
	r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
283 284 285
	if (unlikely(r)) {
		return r;
	}
286 287 288

	r = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
	if (unlikely(r))
289
		goto out_cleanup;
290 291 292 293 294

	r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, &tmp_mem);
	if (unlikely(r))
		goto out_cleanup;

295
	ttm_bo_assign_mem(bo, &tmp_mem);
296
	r = radeon_move_blit(bo, true, new_mem, old_mem);
297 298 299 300
	if (unlikely(r)) {
		goto out_cleanup;
	}
out_cleanup:
301
	ttm_resource_free(bo, &tmp_mem);
302 303 304
	return r;
}

305 306
static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
			  struct ttm_operation_ctx *ctx,
307
			  struct ttm_resource *new_mem)
308 309
{
	struct radeon_device *rdev;
310
	struct radeon_bo *rbo;
311
	struct ttm_resource *old_mem = &bo->mem;
312 313
	int r;

314 315 316 317 318
	if (new_mem->mem_type == TTM_PL_TT) {
		r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, new_mem);
		if (r)
			return r;
	}
319 320
	radeon_bo_move_notify(bo, evict, new_mem);

321
	r = ttm_bo_wait_ctx(bo, ctx);
322
	if (r)
323
		goto fail;
324

325 326
	/* Can't move a pinned BO */
	rbo = container_of(bo, struct radeon_bo, tbo);
327
	if (WARN_ON_ONCE(rbo->tbo.pin_count > 0))
328 329
		return -EINVAL;

330 331
	rdev = radeon_get_rdev(bo->bdev);
	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
332
		ttm_bo_move_null(bo, new_mem);
333 334
		return 0;
	}
335 336
	if (old_mem->mem_type == TTM_PL_SYSTEM &&
	    new_mem->mem_type == TTM_PL_TT) {
337
		ttm_bo_move_null(bo, new_mem);
338 339
		return 0;
	}
340 341

	if (old_mem->mem_type == TTM_PL_TT &&
342
	    new_mem->mem_type == TTM_PL_SYSTEM) {
343 344
		radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
		ttm_resource_free(bo, &bo->mem);
345 346 347
		ttm_bo_assign_mem(bo, new_mem);
		return 0;
	}
348 349
	if (!rdev->ring[radeon_copy_ring_index(rdev)].ready ||
	    rdev->asic->copy.copy == NULL) {
350
		/* use memcpy */
351
		goto memcpy;
352 353 354 355
	}

	if (old_mem->mem_type == TTM_PL_VRAM &&
	    new_mem->mem_type == TTM_PL_SYSTEM) {
356
		r = radeon_move_vram_ram(bo, evict, ctx, new_mem);
357 358
	} else if (old_mem->mem_type == TTM_PL_SYSTEM &&
		   new_mem->mem_type == TTM_PL_VRAM) {
359
		r = radeon_move_ram_vram(bo, evict, ctx, new_mem);
360
	} else {
361
		r = radeon_move_blit(bo, evict,
362
				     new_mem, old_mem);
363
	}
364 365 366

	if (r) {
memcpy:
367
		r = ttm_bo_move_memcpy(bo, ctx, new_mem);
368
		if (r) {
369
			goto fail;
370
		}
371
	}
372 373 374 375

	/* update statistics */
	atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &rdev->num_bytes_moved);
	return 0;
376 377 378 379 380
fail:
	swap(*new_mem, bo->mem);
	radeon_bo_move_notify(bo, false, new_mem);
	swap(*new_mem, bo->mem);
	return r;
381 382
}

383
static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
384 385
{
	struct radeon_device *rdev = radeon_get_rdev(bdev);
386
	size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
387 388 389 390 391 392

	switch (mem->mem_type) {
	case TTM_PL_SYSTEM:
		/* system memory */
		return 0;
	case TTM_PL_TT:
D
Daniel Vetter 已提交
393
#if IS_ENABLED(CONFIG_AGP)
394 395
		if (rdev->flags & RADEON_IS_AGP) {
			/* RADEON_IS_AGP is set only if AGP is active */
396 397
			mem->bus.offset = (mem->start << PAGE_SHIFT) +
				rdev->mc.agp_base;
398
			mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture;
399
			mem->bus.caching = ttm_write_combined;
400 401 402 403
		}
#endif
		break;
	case TTM_PL_VRAM:
404
		mem->bus.offset = mem->start << PAGE_SHIFT;
405
		/* check if it's visible */
406
		if ((mem->bus.offset + bus_size) > rdev->mc.visible_vram_size)
407
			return -EINVAL;
408
		mem->bus.offset += rdev->mc.aper_base;
409
		mem->bus.is_iomem = true;
410
		mem->bus.caching = ttm_write_combined;
411 412 413 414 415
#ifdef __alpha__
		/*
		 * Alpha: use bus.addr to hold the ioremap() return,
		 * so we can modify bus.base below.
		 */
416
		mem->bus.addr = ioremap_wc(mem->bus.offset, bus_size);
417 418
		if (!mem->bus.addr)
			return -ENOMEM;
419 420 421 422 423 424 425

		/*
		 * 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().
		 */
426
		mem->bus.offset = (mem->bus.offset & 0x0ffffffffUL) +
427 428
			rdev->ddev->hose->dense_mem_base;
#endif
429 430 431 432 433 434 435
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

436 437 438 439
/*
 * TTM backend functions.
 */
struct radeon_ttm_tt {
440
	struct ttm_tt		ttm;
441
	u64				offset;
442 443 444 445

	uint64_t			userptr;
	struct mm_struct		*usermm;
	uint32_t			userflags;
446
	bool bound;
447 448
};

449
/* prepare the sg table with the user pages */
D
Dave Airlie 已提交
450
static int radeon_ttm_tt_pin_userptr(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
451
{
D
Dave Airlie 已提交
452
	struct radeon_device *rdev = radeon_get_rdev(bdev);
453
	struct radeon_ttm_tt *gtt = (void *)ttm;
454
	unsigned pinned = 0;
455 456 457 458 459 460 461 462 463
	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;

464 465 466 467 468 469 470 471 472 473
	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;
	}

474 475 476 477 478
	do {
		unsigned num_pages = ttm->num_pages - pinned;
		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
		struct page **pages = ttm->pages + pinned;

479 480
		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
				   pages, NULL);
481 482 483 484 485 486 487 488 489 490 491 492 493
		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;

494 495
	r = dma_map_sgtable(rdev->dev, ttm->sg, direction, 0);
	if (r)
496 497 498 499 500 501 502 503 504 505 506
		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:
507
	release_pages(ttm->pages, pinned);
508 509 510
	return r;
}

D
Dave Airlie 已提交
511
static void radeon_ttm_tt_unpin_userptr(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
512
{
D
Dave Airlie 已提交
513
	struct radeon_device *rdev = radeon_get_rdev(bdev);
514
	struct radeon_ttm_tt *gtt = (void *)ttm;
515
	struct sg_page_iter sg_iter;
516 517 518 519 520

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

521 522 523 524
	/* double check that we don't free the table twice */
	if (!ttm->sg->sgl)
		return;

525
	/* free the sg table and pages again */
526
	dma_unmap_sgtable(rdev->dev, ttm->sg, direction, 0);
527

528
	for_each_sgtable_page(ttm->sg, &sg_iter, 0) {
529
		struct page *page = sg_page_iter_page(&sg_iter);
530 531 532 533
		if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY))
			set_page_dirty(page);

		mark_page_accessed(page);
534
		put_page(page);
535 536 537 538 539
	}

	sg_free_table(ttm->sg);
}

540 541 542 543 544 545 546
static bool radeon_ttm_backend_is_bound(struct ttm_tt *ttm)
{
	struct radeon_ttm_tt *gtt = (void*)ttm;

	return (gtt->bound);
}

D
Dave Airlie 已提交
547 548
static int radeon_ttm_backend_bind(struct ttm_bo_device *bdev,
				   struct ttm_tt *ttm,
549
				   struct ttm_resource *bo_mem)
550
{
551
	struct radeon_ttm_tt *gtt = (void*)ttm;
D
Dave Airlie 已提交
552
	struct radeon_device *rdev = radeon_get_rdev(bdev);
553 554
	uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ |
		RADEON_GART_PAGE_WRITE;
555 556
	int r;

557 558 559
	if (gtt->bound)
		return 0;

560
	if (gtt->userptr) {
D
Dave Airlie 已提交
561
		radeon_ttm_tt_pin_userptr(bdev, ttm);
562 563 564
		flags &= ~RADEON_GART_PAGE_WRITE;
	}

565 566
	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
	if (!ttm->num_pages) {
567
		WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
568 569
		     ttm->num_pages, bo_mem, ttm);
	}
570
	if (ttm->caching == ttm_cached)
571
		flags |= RADEON_GART_PAGE_SNOOP;
572
	r = radeon_gart_bind(rdev, gtt->offset, ttm->num_pages,
573
			     ttm->pages, gtt->ttm.dma_address, flags);
574
	if (r) {
575
		DRM_ERROR("failed to bind %u pages at 0x%08X\n",
576 577 578
			  ttm->num_pages, (unsigned)gtt->offset);
		return r;
	}
579
	gtt->bound = true;
580 581 582
	return 0;
}

D
Dave Airlie 已提交
583
static void radeon_ttm_backend_unbind(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
584
{
585
	struct radeon_ttm_tt *gtt = (void *)ttm;
D
Dave Airlie 已提交
586
	struct radeon_device *rdev = radeon_get_rdev(bdev);
587

588 589 590
	if (!gtt->bound)
		return;

591
	radeon_gart_unbind(rdev, gtt->offset, ttm->num_pages);
592 593

	if (gtt->userptr)
D
Dave Airlie 已提交
594
		radeon_ttm_tt_unpin_userptr(bdev, ttm);
595
	gtt->bound = false;
596 597
}

D
Dave Airlie 已提交
598
static void radeon_ttm_backend_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
599
{
600
	struct radeon_ttm_tt *gtt = (void *)ttm;
601

602
	radeon_ttm_backend_unbind(bdev, ttm);
D
Dave Airlie 已提交
603 604
	ttm_tt_destroy_common(bdev, ttm);

605
	ttm_tt_fini(&gtt->ttm);
606 607 608
	kfree(gtt);
}

609 610
static struct ttm_tt *radeon_ttm_tt_create(struct ttm_buffer_object *bo,
					   uint32_t page_flags)
611 612 613
{
	struct radeon_device *rdev;
	struct radeon_ttm_tt *gtt;
614 615 616 617
	enum ttm_caching caching;
	struct radeon_bo *rbo;

	rbo = container_of(bo, struct radeon_bo, tbo);
618

619
	rdev = radeon_get_rdev(bo->bdev);
D
Daniel Vetter 已提交
620
#if IS_ENABLED(CONFIG_AGP)
621
	if (rdev->flags & RADEON_IS_AGP) {
622 623
		return ttm_agp_tt_create(bo, rdev->ddev->agp->bridge,
					 page_flags);
624 625 626 627 628 629 630
	}
#endif

	gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
	if (gtt == NULL) {
		return NULL;
	}
631 632 633 634 635 636 637 638 639

	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;

	if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags, caching)) {
640
		kfree(gtt);
641 642
		return NULL;
	}
643
	return &gtt->ttm;
644 645
}

646 647
static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct radeon_device *rdev,
						  struct ttm_tt *ttm)
648
{
649 650 651 652 653 654
#if IS_ENABLED(CONFIG_AGP)
	if (rdev->flags & RADEON_IS_AGP)
		return NULL;
#endif

	if (!ttm)
655
		return NULL;
656
	return container_of(ttm, struct radeon_ttm_tt, ttm);
657 658
}

D
Dave Airlie 已提交
659 660 661
static int radeon_ttm_tt_populate(struct ttm_bo_device *bdev,
				  struct ttm_tt *ttm,
				  struct ttm_operation_ctx *ctx)
662
{
663 664
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
665
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
666

667
	if (gtt && gtt->userptr) {
668
		ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
669 670 671 672 673 674 675
		if (!ttm->sg)
			return -ENOMEM;

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

676 677 678 679 680 681
	if (slave && ttm->sg) {
		drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
						 gtt->ttm.dma_address, ttm->num_pages);
		return 0;
	}

D
Daniel Vetter 已提交
682
#if IS_ENABLED(CONFIG_AGP)
J
Jerome Glisse 已提交
683
	if (rdev->flags & RADEON_IS_AGP) {
684
		return ttm_pool_populate(ttm, ctx);
J
Jerome Glisse 已提交
685 686
	}
#endif
687 688

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

694
	return ttm_populate_and_map_pages(rdev->dev, &gtt->ttm, ctx);
695 696
}

D
Dave Airlie 已提交
697
static void radeon_ttm_tt_unpopulate(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
698
{
699 700
	struct radeon_device *rdev = radeon_get_rdev(bdev);
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
701 702
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);

703
	if (gtt && gtt->userptr) {
704 705 706 707 708
		kfree(ttm->sg);
		ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
		return;
	}

709 710
	if (slave)
		return;
711

D
Daniel Vetter 已提交
712
#if IS_ENABLED(CONFIG_AGP)
J
Jerome Glisse 已提交
713
	if (rdev->flags & RADEON_IS_AGP) {
714
		ttm_pool_unpopulate(ttm);
J
Jerome Glisse 已提交
715 716 717
		return;
	}
#endif
718 719

#ifdef CONFIG_SWIOTLB
720
	if (rdev->need_swiotlb && swiotlb_nr_tbl()) {
721
		ttm_dma_unpopulate(&gtt->ttm, rdev->dev);
722 723 724 725
		return;
	}
#endif

726
	ttm_unmap_and_unpopulate_pages(rdev->dev, &gtt->ttm);
727
}
728

729 730
int radeon_ttm_tt_set_userptr(struct radeon_device *rdev,
			      struct ttm_tt *ttm, uint64_t addr,
731 732
			      uint32_t flags)
{
733
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
734 735 736 737 738 739 740 741 742 743

	if (gtt == NULL)
		return -EINVAL;

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

744 745 746 747 748 749 750 751 752 753 754
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);
}

755 756 757 758
static int radeon_ttm_tt_bind(struct ttm_bo_device *bdev,
			      struct ttm_tt *ttm,
			      struct ttm_resource *bo_mem)
{
759
#if IS_ENABLED(CONFIG_AGP)
760
	struct radeon_device *rdev = radeon_get_rdev(bdev);
761
#endif
762

763 764
	if (!bo_mem)
		return -EINVAL;
765 766
#if IS_ENABLED(CONFIG_AGP)
	if (rdev->flags & RADEON_IS_AGP)
767
		return ttm_agp_bind(ttm, bo_mem);
768 769 770 771 772 773 774 775 776 777 778 779
#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) {
780
		ttm_agp_unbind(ttm);
781 782 783 784 785 786 787 788 789 790 791 792 793
		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) {
794
		ttm_agp_unbind(ttm);
D
Dave Airlie 已提交
795
		ttm_tt_destroy_common(bdev, ttm);
796
		ttm_agp_destroy(ttm);
797 798 799 800 801 802 803 804
		return;
	}
#endif
	radeon_ttm_backend_destroy(bdev, ttm);
}

bool radeon_ttm_tt_has_userptr(struct radeon_device *rdev,
			       struct ttm_tt *ttm)
805
{
806
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
807 808 809 810 811 812 813

	if (gtt == NULL)
		return false;

	return !!gtt->userptr;
}

814 815
bool radeon_ttm_tt_is_readonly(struct radeon_device *rdev,
			       struct ttm_tt *ttm)
816
{
817
	struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
818 819 820 821 822 823 824

	if (gtt == NULL)
		return false;

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

825 826 827 828 829 830
static void
radeon_bo_delete_mem_notify(struct ttm_buffer_object *bo)
{
	radeon_bo_move_notify(bo, false, NULL);
}

831
static struct ttm_bo_driver radeon_bo_driver = {
832
	.ttm_tt_create = &radeon_ttm_tt_create,
833 834
	.ttm_tt_populate = &radeon_ttm_tt_populate,
	.ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
835
	.ttm_tt_destroy = &radeon_ttm_tt_destroy,
836
	.eviction_valuable = ttm_bo_eviction_valuable,
837 838 839
	.evict_flags = &radeon_evict_flags,
	.move = &radeon_bo_move,
	.verify_access = &radeon_verify_access,
840
	.delete_mem_notify = &radeon_bo_delete_mem_notify,
841
	.io_mem_reserve = &radeon_ttm_io_mem_reserve,
842 843 844 845 846 847 848 849
};

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,
850 851
			       &radeon_bo_driver,
			       rdev->ddev->anon_inode->i_mapping,
852
			       rdev->ddev->vma_offset_manager,
853
			       dma_addressing_limited(&rdev->pdev->dev));
854 855 856 857
	if (r) {
		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
		return r;
	}
858
	rdev->mman.initialized = true;
859 860

	r = radeon_ttm_init_vram(rdev);
861 862 863 864
	if (r) {
		DRM_ERROR("Failed initializing VRAM heap.\n");
		return r;
	}
865 866 867
	/* 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);

868
	r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
869
			     RADEON_GEM_DOMAIN_VRAM, 0, NULL,
K
Kent Russell 已提交
870
			     NULL, &rdev->stolen_vga_memory);
871 872 873
	if (r) {
		return r;
	}
K
Kent Russell 已提交
874
	r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
875 876
	if (r)
		return r;
K
Kent Russell 已提交
877 878
	r = radeon_bo_pin(rdev->stolen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
	radeon_bo_unreserve(rdev->stolen_vga_memory);
879
	if (r) {
K
Kent Russell 已提交
880
		radeon_bo_unref(&rdev->stolen_vga_memory);
881 882 883
		return r;
	}
	DRM_INFO("radeon: %uM of VRAM memory ready\n",
884
		 (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
885 886

	r = radeon_ttm_init_gtt(rdev);
887 888 889 890 891
	if (r) {
		DRM_ERROR("Failed initializing GTT heap.\n");
		return r;
	}
	DRM_INFO("radeon: %uM of GTT memory ready.\n",
892
		 (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
893 894 895 896 897 898

	r = radeon_ttm_debugfs_init(rdev);
	if (r) {
		DRM_ERROR("Failed to init debugfs\n");
		return r;
	}
899 900 901 902 903
	return 0;
}

void radeon_ttm_fini(struct radeon_device *rdev)
{
904 905
	int r;

906 907
	if (!rdev->mman.initialized)
		return;
908
	radeon_ttm_debugfs_fini(rdev);
K
Kent Russell 已提交
909 910
	if (rdev->stolen_vga_memory) {
		r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
911
		if (r == 0) {
K
Kent Russell 已提交
912 913
			radeon_bo_unpin(rdev->stolen_vga_memory);
			radeon_bo_unreserve(rdev->stolen_vga_memory);
914
		}
K
Kent Russell 已提交
915
		radeon_bo_unref(&rdev->stolen_vga_memory);
916
	}
917 918
	ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_VRAM);
	ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_TT);
919 920
	ttm_bo_device_release(&rdev->mman.bdev);
	radeon_gart_fini(rdev);
921
	rdev->mman.initialized = false;
922 923 924
	DRM_INFO("radeon: ttm finalized\n");
}

925 926 927 928
/* 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)
{
929
	struct ttm_resource_manager *man;
930 931 932 933

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

934
	man = ttm_manager_type(&rdev->mman.bdev, TTM_PL_VRAM);
935 936 937 938
	/* this just adjusts TTM size idea, which sets lpfn to the correct value */
	man->size = size >> PAGE_SHIFT;
}

939
static vm_fault_t radeon_ttm_fault(struct vm_fault *vmf)
940
{
941 942
	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
	struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
943
	vm_fault_t ret;
944

945
	down_read(&rdev->pm.mclk_lock);
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

	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:
964
	up_read(&rdev->pm.mclk_lock);
965
	return ret;
966 967
}

968 969 970 971 972 973 974
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
};

975 976 977
int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
{
	int r;
978 979
	struct drm_file *file_priv = filp->private_data;
	struct radeon_device *rdev = file_priv->minor->dev->dev_private;
980

981
	if (rdev == NULL)
982
		return -EINVAL;
983

984
	r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
985
	if (unlikely(r != 0))
986
		return r;
987

988 989 990 991
	vma->vm_ops = &radeon_ttm_vm_ops;
	return 0;
}

992
#if defined(CONFIG_DEBUG_FS)
993

994 995 996
static int radeon_mm_dump_table(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
997
	unsigned ttm_pl = *(int*)node->info_ent->data;
998 999
	struct drm_device *dev = node->minor->dev;
	struct radeon_device *rdev = dev->dev_private;
1000
	struct ttm_resource_manager *man = ttm_manager_type(&rdev->mman.bdev, ttm_pl);
D
Daniel Vetter 已提交
1001
	struct drm_printer p = drm_seq_file_printer(m);
1002

1003
	man->func->debug(man, &p);
D
Daniel Vetter 已提交
1004
	return 0;
1005
}
1006

1007

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
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
};

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 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
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
};

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
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;
1090
		size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		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
};

1126 1127 1128 1129
#endif

static int radeon_ttm_debugfs_init(struct radeon_device *rdev)
{
1130
#if defined(CONFIG_DEBUG_FS)
1131 1132 1133
	unsigned count;

	struct drm_minor *minor = rdev->ddev->primary;
1134 1135 1136 1137 1138 1139 1140 1141
	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);
1142

1143
	count = ARRAY_SIZE(radeon_ttm_debugfs_list);
1144

1145
#ifdef CONFIG_SWIOTLB
1146
	if (!(rdev->need_swiotlb && swiotlb_nr_tbl()))
1147
		--count;
1148
#endif
1149

1150 1151 1152
	return radeon_debugfs_add_files(rdev, radeon_ttm_debugfs_list, count);
#else

1153
	return 0;
1154
#endif
1155
}
1156 1157 1158 1159 1160 1161 1162

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

	debugfs_remove(rdev->mman.vram);
	rdev->mman.vram = NULL;
1163 1164 1165

	debugfs_remove(rdev->mman.gtt);
	rdev->mman.gtt = NULL;
1166 1167
#endif
}