amdgpu_test.c 6.6 KB
Newer Older
A
Alex Deucher 已提交
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 32 33 34 35
/*
 * Copyright 2009 VMware, 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: Michel Dänzer
 */
#include <drm/drmP.h>
#include <drm/amdgpu_drm.h>
#include "amdgpu.h"
#include "amdgpu_uvd.h"
#include "amdgpu_vce.h"

/* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
static void amdgpu_do_test_moves(struct amdgpu_device *adev)
{
	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
	struct amdgpu_bo *vram_obj = NULL;
	struct amdgpu_bo **gtt_obj = NULL;
36
	uint64_t gart_addr, vram_addr;
A
Alex Deucher 已提交
37 38 39 40 41 42 43 44
	unsigned n, size;
	int i, r;

	size = 1024 * 1024;

	/* Number of tests =
	 * (Total GTT - IB pool - writeback page - ring buffers) / test size
	 */
45
	n = adev->gmc.gart_size - AMDGPU_IB_POOL_SIZE*64*1024;
A
Alex Deucher 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
		if (adev->rings[i])
			n -= adev->rings[i]->ring_size;
	if (adev->wb.wb_obj)
		n -= AMDGPU_GPU_PAGE_SIZE;
	if (adev->irq.ih.ring_obj)
		n -= adev->irq.ih.ring_size;
	n /= size;

	gtt_obj = kzalloc(n * sizeof(*gtt_obj), GFP_KERNEL);
	if (!gtt_obj) {
		DRM_ERROR("Failed to allocate %d pointers\n", n);
		r = 1;
		goto out_cleanup;
	}

62 63
	r = amdgpu_bo_create(adev, size, PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM, 0,
			     ttm_bo_type_kernel, NULL, &vram_obj);
A
Alex Deucher 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77
	if (r) {
		DRM_ERROR("Failed to create VRAM object\n");
		goto out_cleanup;
	}
	r = amdgpu_bo_reserve(vram_obj, false);
	if (unlikely(r != 0))
		goto out_unref;
	r = amdgpu_bo_pin(vram_obj, AMDGPU_GEM_DOMAIN_VRAM, &vram_addr);
	if (r) {
		DRM_ERROR("Failed to pin VRAM object\n");
		goto out_unres;
	}
	for (i = 0; i < n; i++) {
		void *gtt_map, *vram_map;
78
		void **gart_start, **gart_end;
A
Alex Deucher 已提交
79
		void **vram_start, **vram_end;
80
		struct dma_fence *fence = NULL;
A
Alex Deucher 已提交
81

82 83 84
		r = amdgpu_bo_create(adev, size, PAGE_SIZE,
				     AMDGPU_GEM_DOMAIN_GTT, 0,
				     ttm_bo_type_kernel, NULL, gtt_obj + i);
A
Alex Deucher 已提交
85 86 87 88 89 90 91 92
		if (r) {
			DRM_ERROR("Failed to create GTT object %d\n", i);
			goto out_lclean;
		}

		r = amdgpu_bo_reserve(gtt_obj[i], false);
		if (unlikely(r != 0))
			goto out_lclean_unref;
93
		r = amdgpu_bo_pin(gtt_obj[i], AMDGPU_GEM_DOMAIN_GTT, &gart_addr);
A
Alex Deucher 已提交
94 95 96 97 98 99 100 101 102 103 104
		if (r) {
			DRM_ERROR("Failed to pin GTT object %d\n", i);
			goto out_lclean_unres;
		}

		r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
		if (r) {
			DRM_ERROR("Failed to map GTT object %d\n", i);
			goto out_lclean_unpin;
		}

105 106 107 108
		for (gart_start = gtt_map, gart_end = gtt_map + size;
		     gart_start < gart_end;
		     gart_start++)
			*gart_start = gart_start;
A
Alex Deucher 已提交
109 110 111

		amdgpu_bo_kunmap(gtt_obj[i]);

112
		r = amdgpu_copy_buffer(ring, gart_addr, vram_addr,
113
				       size, NULL, &fence, false, false);
A
Alex Deucher 已提交
114 115 116 117 118 119

		if (r) {
			DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
			goto out_lclean_unpin;
		}

120
		r = dma_fence_wait(fence, false);
A
Alex Deucher 已提交
121 122 123 124 125
		if (r) {
			DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
			goto out_lclean_unpin;
		}

126
		dma_fence_put(fence);
A
Alex Deucher 已提交
127 128 129 130 131 132 133

		r = amdgpu_bo_kmap(vram_obj, &vram_map);
		if (r) {
			DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
			goto out_lclean_unpin;
		}

134
		for (gart_start = gtt_map, gart_end = gtt_map + size,
A
Alex Deucher 已提交
135 136
		     vram_start = vram_map, vram_end = vram_map + size;
		     vram_start < vram_end;
137 138
		     gart_start++, vram_start++) {
			if (*vram_start != gart_start) {
A
Alex Deucher 已提交
139 140 141
				DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
					  "expected 0x%p (GTT/VRAM offset "
					  "0x%16llx/0x%16llx)\n",
142
					  i, *vram_start, gart_start,
A
Alex Deucher 已提交
143
					  (unsigned long long)
144
					  (gart_addr - adev->gmc.gart_start +
145
					   (void*)gart_start - gtt_map),
A
Alex Deucher 已提交
146
					  (unsigned long long)
147
					  (vram_addr - adev->gmc.vram_start +
148
					   (void*)gart_start - gtt_map));
A
Alex Deucher 已提交
149 150 151 152 153 154 155 156
				amdgpu_bo_kunmap(vram_obj);
				goto out_lclean_unpin;
			}
			*vram_start = vram_start;
		}

		amdgpu_bo_kunmap(vram_obj);

157
		r = amdgpu_copy_buffer(ring, vram_addr, gart_addr,
158
				       size, NULL, &fence, false, false);
A
Alex Deucher 已提交
159 160 161 162 163 164

		if (r) {
			DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
			goto out_lclean_unpin;
		}

165
		r = dma_fence_wait(fence, false);
A
Alex Deucher 已提交
166 167 168 169 170
		if (r) {
			DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
			goto out_lclean_unpin;
		}

171
		dma_fence_put(fence);
A
Alex Deucher 已提交
172 173 174 175 176 177 178

		r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
		if (r) {
			DRM_ERROR("Failed to map GTT object after copy %d\n", i);
			goto out_lclean_unpin;
		}

179
		for (gart_start = gtt_map, gart_end = gtt_map + size,
A
Alex Deucher 已提交
180
		     vram_start = vram_map, vram_end = vram_map + size;
181 182 183
		     gart_start < gart_end;
		     gart_start++, vram_start++) {
			if (*gart_start != vram_start) {
A
Alex Deucher 已提交
184 185 186
				DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
					  "expected 0x%p (VRAM/GTT offset "
					  "0x%16llx/0x%16llx)\n",
187
					  i, *gart_start, vram_start,
A
Alex Deucher 已提交
188
					  (unsigned long long)
189
					  (vram_addr - adev->gmc.vram_start +
A
Alex Deucher 已提交
190 191
					   (void*)vram_start - vram_map),
					  (unsigned long long)
192
					  (gart_addr - adev->gmc.gart_start +
A
Alex Deucher 已提交
193 194 195 196 197 198 199 200 201
					   (void*)vram_start - vram_map));
				amdgpu_bo_kunmap(gtt_obj[i]);
				goto out_lclean_unpin;
			}
		}

		amdgpu_bo_kunmap(gtt_obj[i]);

		DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n",
202
			 gart_addr - adev->gmc.gart_start);
A
Alex Deucher 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
		continue;

out_lclean_unpin:
		amdgpu_bo_unpin(gtt_obj[i]);
out_lclean_unres:
		amdgpu_bo_unreserve(gtt_obj[i]);
out_lclean_unref:
		amdgpu_bo_unref(&gtt_obj[i]);
out_lclean:
		for (--i; i >= 0; --i) {
			amdgpu_bo_unpin(gtt_obj[i]);
			amdgpu_bo_unreserve(gtt_obj[i]);
			amdgpu_bo_unref(&gtt_obj[i]);
		}
		if (fence)
218
			dma_fence_put(fence);
A
Alex Deucher 已提交
219 220 221 222 223 224 225 226 227 228 229
		break;
	}

	amdgpu_bo_unpin(vram_obj);
out_unres:
	amdgpu_bo_unreserve(vram_obj);
out_unref:
	amdgpu_bo_unref(&vram_obj);
out_cleanup:
	kfree(gtt_obj);
	if (r) {
230
		pr_warn("Error while testing BO move\n");
A
Alex Deucher 已提交
231 232 233 234 235 236 237 238
	}
}

void amdgpu_test_moves(struct amdgpu_device *adev)
{
	if (adev->mman.buffer_funcs)
		amdgpu_do_test_moves(adev);
}