amdgpu_vram_mgr.c 19.0 KB
Newer Older
C
Christian König 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2016 Advanced Micro Devices, Inc.
 *
 * 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, sublicense,
 * 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 above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
 *
 * Authors: Christian König
 */

25
#include <linux/dma-mapping.h>
C
Christian König 已提交
26
#include "amdgpu.h"
27
#include "amdgpu_vm.h"
28 29
#include "amdgpu_atomfirmware.h"
#include "atom.h"
C
Christian König 已提交
30

31
static inline struct amdgpu_vram_mgr *to_vram_mgr(struct ttm_resource_manager *man)
32 33 34 35
{
	return container_of(man, struct amdgpu_vram_mgr, manager);
}

36 37 38 39
static inline struct amdgpu_device *to_amdgpu_device(struct amdgpu_vram_mgr *mgr)
{
	return container_of(mgr, struct amdgpu_device, mman.vram_mgr);
}
C
Christian König 已提交
40

41 42 43 44 45 46 47 48 49 50 51 52
/**
 * DOC: mem_info_vram_total
 *
 * The amdgpu driver provides a sysfs API for reporting current total VRAM
 * available on the device
 * The file mem_info_vram_total is used for this and returns the total
 * amount of VRAM in bytes
 */
static ssize_t amdgpu_mem_info_vram_total_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct drm_device *ddev = dev_get_drvdata(dev);
53
	struct amdgpu_device *adev = drm_to_adev(ddev);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

	return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.real_vram_size);
}

/**
 * DOC: mem_info_vis_vram_total
 *
 * The amdgpu driver provides a sysfs API for reporting current total
 * visible VRAM available on the device
 * The file mem_info_vis_vram_total is used for this and returns the total
 * amount of visible VRAM in bytes
 */
static ssize_t amdgpu_mem_info_vis_vram_total_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct drm_device *ddev = dev_get_drvdata(dev);
70
	struct amdgpu_device *adev = drm_to_adev(ddev);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.visible_vram_size);
}

/**
 * DOC: mem_info_vram_used
 *
 * The amdgpu driver provides a sysfs API for reporting current total VRAM
 * available on the device
 * The file mem_info_vram_used is used for this and returns the total
 * amount of currently used VRAM in bytes
 */
static ssize_t amdgpu_mem_info_vram_used_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct drm_device *ddev = dev_get_drvdata(dev);
87
	struct amdgpu_device *adev = drm_to_adev(ddev);
88
	struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
89 90

	return snprintf(buf, PAGE_SIZE, "%llu\n",
91
			amdgpu_vram_mgr_usage(man));
92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

/**
 * DOC: mem_info_vis_vram_used
 *
 * The amdgpu driver provides a sysfs API for reporting current total of
 * used visible VRAM
 * The file mem_info_vis_vram_used is used for this and returns the total
 * amount of currently used visible VRAM in bytes
 */
static ssize_t amdgpu_mem_info_vis_vram_used_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct drm_device *ddev = dev_get_drvdata(dev);
106
	struct amdgpu_device *adev = drm_to_adev(ddev);
107
	struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
108 109

	return snprintf(buf, PAGE_SIZE, "%llu\n",
110
			amdgpu_vram_mgr_vis_usage(man));
111 112
}

113 114 115 116 117
static ssize_t amdgpu_mem_info_vram_vendor(struct device *dev,
						 struct device_attribute *attr,
						 char *buf)
{
	struct drm_device *ddev = dev_get_drvdata(dev);
118
	struct amdgpu_device *adev = drm_to_adev(ddev);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

	switch (adev->gmc.vram_vendor) {
	case SAMSUNG:
		return snprintf(buf, PAGE_SIZE, "samsung\n");
	case INFINEON:
		return snprintf(buf, PAGE_SIZE, "infineon\n");
	case ELPIDA:
		return snprintf(buf, PAGE_SIZE, "elpida\n");
	case ETRON:
		return snprintf(buf, PAGE_SIZE, "etron\n");
	case NANYA:
		return snprintf(buf, PAGE_SIZE, "nanya\n");
	case HYNIX:
		return snprintf(buf, PAGE_SIZE, "hynix\n");
	case MOSEL:
		return snprintf(buf, PAGE_SIZE, "mosel\n");
	case WINBOND:
		return snprintf(buf, PAGE_SIZE, "winbond\n");
	case ESMT:
		return snprintf(buf, PAGE_SIZE, "esmt\n");
	case MICRON:
		return snprintf(buf, PAGE_SIZE, "micron\n");
	default:
		return snprintf(buf, PAGE_SIZE, "unknown\n");
	}
}

146 147 148 149 150 151 152 153
static DEVICE_ATTR(mem_info_vram_total, S_IRUGO,
		   amdgpu_mem_info_vram_total_show, NULL);
static DEVICE_ATTR(mem_info_vis_vram_total, S_IRUGO,
		   amdgpu_mem_info_vis_vram_total_show,NULL);
static DEVICE_ATTR(mem_info_vram_used, S_IRUGO,
		   amdgpu_mem_info_vram_used_show, NULL);
static DEVICE_ATTR(mem_info_vis_vram_used, S_IRUGO,
		   amdgpu_mem_info_vis_vram_used_show, NULL);
154 155
static DEVICE_ATTR(mem_info_vram_vendor, S_IRUGO,
		   amdgpu_mem_info_vram_vendor, NULL);
156

157 158 159 160 161 162 163 164 165
static const struct attribute *amdgpu_vram_mgr_attributes[] = {
	&dev_attr_mem_info_vram_total.attr,
	&dev_attr_mem_info_vis_vram_total.attr,
	&dev_attr_mem_info_vram_used.attr,
	&dev_attr_mem_info_vis_vram_used.attr,
	&dev_attr_mem_info_vram_vendor.attr,
	NULL
};

166
static const struct ttm_resource_manager_func amdgpu_vram_mgr_func;
167

C
Christian König 已提交
168 169 170
/**
 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
 *
171
 * @adev: amdgpu_device pointer
C
Christian König 已提交
172 173 174
 *
 * Allocate and initialize the VRAM manager.
 */
175
int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
C
Christian König 已提交
176
{
177 178
	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
	struct ttm_resource_manager *man = &mgr->manager;
179
	int ret;
C
Christian König 已提交
180

181
	ttm_resource_manager_init(man, adev->gmc.real_vram_size >> PAGE_SHIFT);
182 183

	man->func = &amdgpu_vram_mgr_func;
C
Christian König 已提交
184

185
	drm_mm_init(&mgr->mm, 0, man->size);
C
Christian König 已提交
186
	spin_lock_init(&mgr->lock);
187 188
	INIT_LIST_HEAD(&mgr->reservations_pending);
	INIT_LIST_HEAD(&mgr->reserved_pages);
189 190

	/* Add the two VRAM-related sysfs files */
191 192 193
	ret = sysfs_create_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
	if (ret)
		DRM_ERROR("Failed to register sysfs\n");
194

195
	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
196
	ttm_resource_manager_set_used(man, true);
C
Christian König 已提交
197 198 199 200 201 202
	return 0;
}

/**
 * amdgpu_vram_mgr_fini - free and destroy VRAM manager
 *
203
 * @adev: amdgpu_device pointer
C
Christian König 已提交
204 205 206 207
 *
 * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
 * allocated inside it.
 */
208
void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
C
Christian König 已提交
209
{
210 211
	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
	struct ttm_resource_manager *man = &mgr->manager;
212
	int ret;
213
	struct amdgpu_vram_reservation *rsv, *temp;
214

215
	ttm_resource_manager_set_used(man, false);
216

217
	ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
218 219
	if (ret)
		return;
C
Christian König 已提交
220 221

	spin_lock(&mgr->lock);
222 223 224 225 226 227 228
	list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node)
		kfree(rsv);

	list_for_each_entry_safe(rsv, temp, &mgr->reserved_pages, node) {
		drm_mm_remove_node(&rsv->mm_node);
		kfree(rsv);
	}
C
Christian König 已提交
229 230
	drm_mm_takedown(&mgr->mm);
	spin_unlock(&mgr->lock);
231

232
	sysfs_remove_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
233

234
	ttm_resource_manager_cleanup(man);
235
	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, NULL);
C
Christian König 已提交
236 237
}

238 239 240
/**
 * amdgpu_vram_mgr_vis_size - Calculate visible node size
 *
241
 * @adev: amdgpu_device pointer
242 243 244 245 246 247 248 249 250 251
 * @node: MM node structure
 *
 * Calculate how many bytes of the MM node are inside visible VRAM
 */
static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
				    struct drm_mm_node *node)
{
	uint64_t start = node->start << PAGE_SHIFT;
	uint64_t end = (node->size + node->start) << PAGE_SHIFT;

252
	if (start >= adev->gmc.visible_vram_size)
253 254
		return 0;

255 256
	return (end > adev->gmc.visible_vram_size ?
		adev->gmc.visible_vram_size : end) - start;
257 258
}

259
/**
260
 * amdgpu_vram_mgr_bo_visible_size - CPU visible BO size
261 262 263 264
 *
 * @bo: &amdgpu_bo buffer object (must be in VRAM)
 *
 * Returns:
265
 * How much of the given &amdgpu_bo buffer object lies in CPU visible VRAM.
266
 */
267
u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo)
268
{
269
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
270
	struct ttm_resource *mem = &bo->tbo.mem;
271 272
	struct drm_mm_node *nodes = mem->mm_node;
	unsigned pages = mem->num_pages;
273
	u64 usage;
274

275
	if (amdgpu_gmc_vram_full_visible(&adev->gmc))
276
		return amdgpu_bo_size(bo);
277 278

	if (mem->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
279
		return 0;
280

281 282
	for (usage = 0; nodes && pages; pages -= nodes->size, nodes++)
		usage += amdgpu_vram_mgr_vis_size(adev, nodes);
283 284

	return usage;
285 286
}

287 288 289 290 291 292 293 294 295 296 297 298
static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man)
{
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
	struct amdgpu_device *adev = to_amdgpu_device(mgr);
	struct drm_mm *mm = &mgr->mm;
	struct amdgpu_vram_reservation *rsv, *temp;
	uint64_t vis_usage;

	list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node) {
		if (drm_mm_reserve_node(mm, &rsv->mm_node))
			continue;

299
		dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
300
			rsv->mm_node.start, rsv->mm_node.size);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

		vis_usage = amdgpu_vram_mgr_vis_size(adev, &rsv->mm_node);
		atomic64_add(vis_usage, &mgr->vis_usage);
		atomic64_add(rsv->mm_node.size << PAGE_SHIFT, &mgr->usage);
		list_move(&rsv->node, &mgr->reserved_pages);
	}
}

/**
 * amdgpu_vram_mgr_reserve_range - Reserve a range from VRAM
 *
 * @man: TTM memory type manager
 * @start: start address of the range in VRAM
 * @size: size of the range
 *
 * Reserve memory from start addess with the specified size in VRAM
 */
int amdgpu_vram_mgr_reserve_range(struct ttm_resource_manager *man,
				  uint64_t start, uint64_t size)
{
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
	struct amdgpu_vram_reservation *rsv;

	rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
	if (!rsv)
		return -ENOMEM;

	INIT_LIST_HEAD(&rsv->node);
	rsv->mm_node.start = start >> PAGE_SHIFT;
	rsv->mm_node.size = size >> PAGE_SHIFT;

	spin_lock(&mgr->lock);
333
	list_add_tail(&mgr->reservations_pending, &rsv->node);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	amdgpu_vram_mgr_do_reserve(man);
	spin_unlock(&mgr->lock);

	return 0;
}

/**
 * amdgpu_vram_mgr_query_page_status - query the reservation status
 *
 * @man: TTM memory type manager
 * @start: start address of a page in VRAM
 *
 * Returns:
 *	-EBUSY: the page is still hold and in pending list
 *	0: the page has been reserved
 *	-ENOENT: the input page is not a reservation
 */
int amdgpu_vram_mgr_query_page_status(struct ttm_resource_manager *man,
				      uint64_t start)
{
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
	struct amdgpu_vram_reservation *rsv;
	int ret;

	spin_lock(&mgr->lock);

	list_for_each_entry(rsv, &mgr->reservations_pending, node) {
		if ((rsv->mm_node.start <= start) &&
		    (start < (rsv->mm_node.start + rsv->mm_node.size))) {
			ret = -EBUSY;
			goto out;
		}
	}

	list_for_each_entry(rsv, &mgr->reserved_pages, node) {
		if ((rsv->mm_node.start <= start) &&
		    (start < (rsv->mm_node.start + rsv->mm_node.size))) {
			ret = 0;
			goto out;
		}
	}

	ret = -ENOENT;
out:
	spin_unlock(&mgr->lock);
	return ret;
}

382 383 384
/**
 * amdgpu_vram_mgr_virt_start - update virtual start address
 *
385
 * @mem: ttm_resource to update
386 387 388 389 390
 * @node: just allocated node
 *
 * Calculate a virtual BO start address to easily check if everything is CPU
 * accessible.
 */
391
static void amdgpu_vram_mgr_virt_start(struct ttm_resource *mem,
392 393 394 395 396 397 398 399 400 401 402 403
				       struct drm_mm_node *node)
{
	unsigned long start;

	start = node->start + node->size;
	if (start > mem->num_pages)
		start -= mem->num_pages;
	else
		start = 0;
	mem->start = max(mem->start, start);
}

C
Christian König 已提交
404 405 406 407 408 409 410 411 412 413
/**
 * amdgpu_vram_mgr_new - allocate new ranges
 *
 * @man: TTM memory type manager
 * @tbo: TTM BO we need this range for
 * @place: placement flags and restrictions
 * @mem: the resulting mem object
 *
 * Allocate VRAM for the given BO.
 */
414
static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
C
Christian König 已提交
415 416
			       struct ttm_buffer_object *tbo,
			       const struct ttm_place *place,
417
			       struct ttm_resource *mem)
C
Christian König 已提交
418
{
419
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
420
	struct amdgpu_device *adev = to_amdgpu_device(mgr);
C
Christian König 已提交
421 422
	struct drm_mm *mm = &mgr->mm;
	struct drm_mm_node *nodes;
423
	enum drm_mm_insert_mode mode;
C
Christian König 已提交
424
	unsigned long lpfn, num_nodes, pages_per_node, pages_left;
425
	uint64_t vis_usage = 0, mem_bytes, max_bytes;
C
Christian König 已提交
426 427 428 429 430 431 432
	unsigned i;
	int r;

	lpfn = place->lpfn;
	if (!lpfn)
		lpfn = man->size;

433 434 435 436
	max_bytes = adev->gmc.mc_vram_size;
	if (tbo->type != ttm_bo_type_kernel)
		max_bytes -= AMDGPU_VM_RESERVED_VRAM;

437
	/* bail out quickly if there's likely not enough VRAM for this BO */
438
	mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
439
	if (atomic64_add_return(mem_bytes, &mgr->usage) > max_bytes) {
440
		atomic64_sub(mem_bytes, &mgr->usage);
441
		return -ENOSPC;
442 443
	}

444
	if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
C
Christian König 已提交
445 446 447
		pages_per_node = ~0ul;
		num_nodes = 1;
	} else {
448 449 450 451 452 453 454
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
		pages_per_node = HPAGE_PMD_NR;
#else
		/* default to 2MB */
		pages_per_node = (2UL << (20UL - PAGE_SHIFT));
#endif
		pages_per_node = max((uint32_t)pages_per_node, mem->page_alignment);
C
Christian König 已提交
455 456 457
		num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
	}

458
	nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
459
			       GFP_KERNEL | __GFP_ZERO);
460
	if (!nodes) {
461
		atomic64_sub(mem_bytes, &mgr->usage);
C
Christian König 已提交
462
		return -ENOMEM;
463
	}
C
Christian König 已提交
464

465 466 467
	mode = DRM_MM_INSERT_BEST;
	if (place->flags & TTM_PL_FLAG_TOPDOWN)
		mode = DRM_MM_INSERT_HIGH;
C
Christian König 已提交
468

469
	mem->start = 0;
C
Christian König 已提交
470 471 472
	pages_left = mem->num_pages;

	spin_lock(&mgr->lock);
473 474 475
	for (i = 0; pages_left >= pages_per_node; ++i) {
		unsigned long pages = rounddown_pow_of_two(pages_left);

476 477 478
		/* Limit maximum size to 2GB due to SG table limitations */
		pages = min(pages, (2UL << (30 - PAGE_SHIFT)));

479 480 481 482 483 484 485 486 487 488 489 490 491
		r = drm_mm_insert_node_in_range(mm, &nodes[i], pages,
						pages_per_node, 0,
						place->fpfn, lpfn,
						mode);
		if (unlikely(r))
			break;

		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
		pages_left -= pages;
	}

	for (; pages_left; ++i) {
C
Christian König 已提交
492 493 494 495 496 497
		unsigned long pages = min(pages_left, pages_per_node);
		uint32_t alignment = mem->page_alignment;

		if (pages == pages_per_node)
			alignment = pages_per_node;

498 499 500 501
		r = drm_mm_insert_node_in_range(mm, &nodes[i],
						pages, alignment, 0,
						place->fpfn, lpfn,
						mode);
C
Christian König 已提交
502 503 504
		if (unlikely(r))
			goto error;

505
		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
506
		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
C
Christian König 已提交
507 508 509 510
		pages_left -= pages;
	}
	spin_unlock(&mgr->lock);

511 512
	atomic64_add(vis_usage, &mgr->vis_usage);

C
Christian König 已提交
513 514 515 516 517 518 519 520
	mem->mm_node = nodes;

	return 0;

error:
	while (i--)
		drm_mm_remove_node(&nodes[i]);
	spin_unlock(&mgr->lock);
521
	atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
C
Christian König 已提交
522

523
	kvfree(nodes);
524
	return r;
C
Christian König 已提交
525 526 527 528 529 530 531 532 533 534
}

/**
 * amdgpu_vram_mgr_del - free ranges
 *
 * @man: TTM memory type manager
 * @mem: TTM memory object
 *
 * Free the allocated VRAM again.
 */
535
static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
536
				struct ttm_resource *mem)
C
Christian König 已提交
537
{
538
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
539
	struct amdgpu_device *adev = to_amdgpu_device(mgr);
C
Christian König 已提交
540
	struct drm_mm_node *nodes = mem->mm_node;
541
	uint64_t usage = 0, vis_usage = 0;
C
Christian König 已提交
542 543 544 545 546 547 548 549 550
	unsigned pages = mem->num_pages;

	if (!mem->mm_node)
		return;

	spin_lock(&mgr->lock);
	while (pages) {
		pages -= nodes->size;
		drm_mm_remove_node(nodes);
551 552
		usage += nodes->size << PAGE_SHIFT;
		vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
C
Christian König 已提交
553 554
		++nodes;
	}
555
	amdgpu_vram_mgr_do_reserve(man);
C
Christian König 已提交
556 557
	spin_unlock(&mgr->lock);

558 559 560
	atomic64_sub(usage, &mgr->usage);
	atomic64_sub(vis_usage, &mgr->vis_usage);

561
	kvfree(mem->mm_node);
C
Christian König 已提交
562 563 564
	mem->mm_node = NULL;
}

565 566 567 568 569 570 571 572 573 574 575 576
/**
 * amdgpu_vram_mgr_alloc_sgt - allocate and fill a sg table
 *
 * @adev: amdgpu device pointer
 * @mem: TTM memory object
 * @dev: the other device
 * @dir: dma direction
 * @sgt: resulting sg table
 *
 * Allocate and fill a sg table from a VRAM allocation.
 */
int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
577
			      struct ttm_resource *mem,
578 579 580 581 582 583 584 585 586 587
			      struct device *dev,
			      enum dma_data_direction dir,
			      struct sg_table **sgt)
{
	struct drm_mm_node *node;
	struct scatterlist *sg;
	int num_entries = 0;
	unsigned int pages;
	int i, r;

588
	*sgt = kmalloc(sizeof(**sgt), GFP_KERNEL);
589 590 591 592 593 594 595 596 597 598 599
	if (!*sgt)
		return -ENOMEM;

	for (pages = mem->num_pages, node = mem->mm_node;
	     pages; pages -= node->size, ++node)
		++num_entries;

	r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
	if (r)
		goto error_free;

600
	for_each_sgtable_sg((*sgt), sg, i)
601 602 603
		sg->length = 0;

	node = mem->mm_node;
604
	for_each_sgtable_sg((*sgt), sg, i) {
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
		phys_addr_t phys = (node->start << PAGE_SHIFT) +
			adev->gmc.aper_base;
		size_t size = node->size << PAGE_SHIFT;
		dma_addr_t addr;

		++node;
		addr = dma_map_resource(dev, phys, size, dir,
					DMA_ATTR_SKIP_CPU_SYNC);
		r = dma_mapping_error(dev, addr);
		if (r)
			goto error_unmap;

		sg_set_page(sg, NULL, size, 0);
		sg_dma_address(sg) = addr;
		sg_dma_len(sg) = size;
	}
	return 0;

error_unmap:
624
	for_each_sgtable_sg((*sgt), sg, i) {
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
		if (!sg->length)
			continue;

		dma_unmap_resource(dev, sg->dma_address,
				   sg->length, dir,
				   DMA_ATTR_SKIP_CPU_SYNC);
	}
	sg_free_table(*sgt);

error_free:
	kfree(*sgt);
	return r;
}

/**
640
 * amdgpu_vram_mgr_free_sgt - allocate and fill a sg table
641
 *
642 643
 * @dev: device pointer
 * @dir: data direction of resource to unmap
644 645 646 647
 * @sgt: sg table to free
 *
 * Free a previously allocate sg table.
 */
648
void amdgpu_vram_mgr_free_sgt(struct device *dev,
649 650 651 652 653 654
			      enum dma_data_direction dir,
			      struct sg_table *sgt)
{
	struct scatterlist *sg;
	int i;

655
	for_each_sgtable_sg(sgt, sg, i)
656 657 658 659 660 661 662
		dma_unmap_resource(dev, sg->dma_address,
				   sg->length, dir,
				   DMA_ATTR_SKIP_CPU_SYNC);
	sg_free_table(sgt);
	kfree(sgt);
}

663 664 665 666 667 668 669
/**
 * amdgpu_vram_mgr_usage - how many bytes are used in this domain
 *
 * @man: TTM memory type manager
 *
 * Returns how many bytes are used in this domain.
 */
670
uint64_t amdgpu_vram_mgr_usage(struct ttm_resource_manager *man)
671
{
672
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
673 674 675 676 677 678 679 680 681 682 683

	return atomic64_read(&mgr->usage);
}

/**
 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
 *
 * @man: TTM memory type manager
 *
 * Returns how many bytes are used in the visible part of VRAM
 */
684
uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_resource_manager *man)
685
{
686
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
687 688 689 690

	return atomic64_read(&mgr->vis_usage);
}

C
Christian König 已提交
691 692 693 694
/**
 * amdgpu_vram_mgr_debug - dump VRAM table
 *
 * @man: TTM memory type manager
695
 * @printer: DRM printer to use
C
Christian König 已提交
696 697 698
 *
 * Dump the table content using printk.
 */
699
static void amdgpu_vram_mgr_debug(struct ttm_resource_manager *man,
700
				  struct drm_printer *printer)
C
Christian König 已提交
701
{
702
	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
C
Christian König 已提交
703 704

	spin_lock(&mgr->lock);
705
	drm_mm_print(&mgr->mm, printer);
C
Christian König 已提交
706
	spin_unlock(&mgr->lock);
707 708

	drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
709 710
		   man->size, amdgpu_vram_mgr_usage(man) >> 20,
		   amdgpu_vram_mgr_vis_usage(man) >> 20);
C
Christian König 已提交
711 712
}

713
static const struct ttm_resource_manager_func amdgpu_vram_mgr_func = {
714 715 716
	.alloc	= amdgpu_vram_mgr_new,
	.free	= amdgpu_vram_mgr_del,
	.debug	= amdgpu_vram_mgr_debug
C
Christian König 已提交
717
};