Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.5  riastrad /*	$NetBSD: amdgpu_test.c,v 1.5 2021/12/18 23:44:58 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.5  riastrad // SPDX-License-Identifier: GPL-2.0 OR MIT
      4  1.1  riastrad /*
      5  1.1  riastrad  * Copyright 2009 VMware, Inc.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      9  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     10  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     12  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     13  1.1  riastrad  *
     14  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     15  1.1  riastrad  * all copies or substantial portions of the Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     21  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     22  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     23  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors: Michel Dnzer
     26  1.1  riastrad  */
     27  1.5  riastrad 
     28  1.1  riastrad #include <sys/cdefs.h>
     29  1.5  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_test.c,v 1.5 2021/12/18 23:44:58 riastradh Exp $");
     30  1.1  riastrad 
     31  1.1  riastrad #include <drm/amdgpu_drm.h>
     32  1.1  riastrad #include "amdgpu.h"
     33  1.1  riastrad #include "amdgpu_uvd.h"
     34  1.1  riastrad #include "amdgpu_vce.h"
     35  1.1  riastrad 
     36  1.1  riastrad /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
     37  1.1  riastrad static void amdgpu_do_test_moves(struct amdgpu_device *adev)
     38  1.1  riastrad {
     39  1.1  riastrad 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
     40  1.1  riastrad 	struct amdgpu_bo *vram_obj = NULL;
     41  1.1  riastrad 	struct amdgpu_bo **gtt_obj = NULL;
     42  1.5  riastrad 	struct amdgpu_bo_param bp;
     43  1.5  riastrad 	uint64_t gart_addr, vram_addr;
     44  1.1  riastrad 	unsigned n, size;
     45  1.1  riastrad 	int i, r;
     46  1.1  riastrad 
     47  1.1  riastrad 	size = 1024 * 1024;
     48  1.1  riastrad 
     49  1.1  riastrad 	/* Number of tests =
     50  1.1  riastrad 	 * (Total GTT - IB pool - writeback page - ring buffers) / test size
     51  1.1  riastrad 	 */
     52  1.5  riastrad 	n = adev->gmc.gart_size - AMDGPU_IB_POOL_SIZE*64*1024;
     53  1.1  riastrad 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
     54  1.1  riastrad 		if (adev->rings[i])
     55  1.1  riastrad 			n -= adev->rings[i]->ring_size;
     56  1.1  riastrad 	if (adev->wb.wb_obj)
     57  1.1  riastrad 		n -= AMDGPU_GPU_PAGE_SIZE;
     58  1.1  riastrad 	if (adev->irq.ih.ring_obj)
     59  1.1  riastrad 		n -= adev->irq.ih.ring_size;
     60  1.1  riastrad 	n /= size;
     61  1.1  riastrad 
     62  1.5  riastrad 	gtt_obj = kcalloc(n, sizeof(*gtt_obj), GFP_KERNEL);
     63  1.1  riastrad 	if (!gtt_obj) {
     64  1.1  riastrad 		DRM_ERROR("Failed to allocate %d pointers\n", n);
     65  1.1  riastrad 		r = 1;
     66  1.1  riastrad 		goto out_cleanup;
     67  1.1  riastrad 	}
     68  1.5  riastrad 	memset(&bp, 0, sizeof(bp));
     69  1.5  riastrad 	bp.size = size;
     70  1.5  riastrad 	bp.byte_align = PAGE_SIZE;
     71  1.5  riastrad 	bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
     72  1.5  riastrad 	bp.flags = 0;
     73  1.5  riastrad 	bp.type = ttm_bo_type_kernel;
     74  1.5  riastrad 	bp.resv = NULL;
     75  1.1  riastrad 
     76  1.5  riastrad 	r = amdgpu_bo_create(adev, &bp, &vram_obj);
     77  1.1  riastrad 	if (r) {
     78  1.1  riastrad 		DRM_ERROR("Failed to create VRAM object\n");
     79  1.1  riastrad 		goto out_cleanup;
     80  1.1  riastrad 	}
     81  1.1  riastrad 	r = amdgpu_bo_reserve(vram_obj, false);
     82  1.1  riastrad 	if (unlikely(r != 0))
     83  1.1  riastrad 		goto out_unref;
     84  1.5  riastrad 	r = amdgpu_bo_pin(vram_obj, AMDGPU_GEM_DOMAIN_VRAM);
     85  1.1  riastrad 	if (r) {
     86  1.1  riastrad 		DRM_ERROR("Failed to pin VRAM object\n");
     87  1.1  riastrad 		goto out_unres;
     88  1.1  riastrad 	}
     89  1.5  riastrad 	vram_addr = amdgpu_bo_gpu_offset(vram_obj);
     90  1.1  riastrad 	for (i = 0; i < n; i++) {
     91  1.1  riastrad 		void *gtt_map, *vram_map;
     92  1.5  riastrad 		void **gart_start, **gart_end;
     93  1.1  riastrad 		void **vram_start, **vram_end;
     94  1.5  riastrad 		struct dma_fence *fence = NULL;
     95  1.1  riastrad 
     96  1.5  riastrad 		bp.domain = AMDGPU_GEM_DOMAIN_GTT;
     97  1.5  riastrad 		r = amdgpu_bo_create(adev, &bp, gtt_obj + i);
     98  1.1  riastrad 		if (r) {
     99  1.1  riastrad 			DRM_ERROR("Failed to create GTT object %d\n", i);
    100  1.1  riastrad 			goto out_lclean;
    101  1.1  riastrad 		}
    102  1.1  riastrad 
    103  1.1  riastrad 		r = amdgpu_bo_reserve(gtt_obj[i], false);
    104  1.1  riastrad 		if (unlikely(r != 0))
    105  1.1  riastrad 			goto out_lclean_unref;
    106  1.5  riastrad 		r = amdgpu_bo_pin(gtt_obj[i], AMDGPU_GEM_DOMAIN_GTT);
    107  1.1  riastrad 		if (r) {
    108  1.1  riastrad 			DRM_ERROR("Failed to pin GTT object %d\n", i);
    109  1.1  riastrad 			goto out_lclean_unres;
    110  1.1  riastrad 		}
    111  1.5  riastrad 		r = amdgpu_ttm_alloc_gart(&gtt_obj[i]->tbo);
    112  1.5  riastrad 		if (r) {
    113  1.5  riastrad 			DRM_ERROR("%p bind failed\n", gtt_obj[i]);
    114  1.5  riastrad 			goto out_lclean_unpin;
    115  1.5  riastrad 		}
    116  1.5  riastrad 		gart_addr = amdgpu_bo_gpu_offset(gtt_obj[i]);
    117  1.1  riastrad 
    118  1.1  riastrad 		r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
    119  1.1  riastrad 		if (r) {
    120  1.1  riastrad 			DRM_ERROR("Failed to map GTT object %d\n", i);
    121  1.1  riastrad 			goto out_lclean_unpin;
    122  1.1  riastrad 		}
    123  1.1  riastrad 
    124  1.5  riastrad 		for (gart_start = gtt_map, gart_end = gtt_map + size;
    125  1.5  riastrad 		     gart_start < gart_end;
    126  1.5  riastrad 		     gart_start++)
    127  1.5  riastrad 			*gart_start = gart_start;
    128  1.1  riastrad 
    129  1.1  riastrad 		amdgpu_bo_kunmap(gtt_obj[i]);
    130  1.1  riastrad 
    131  1.5  riastrad 		r = amdgpu_copy_buffer(ring, gart_addr, vram_addr,
    132  1.5  riastrad 				       size, NULL, &fence, false, false);
    133  1.1  riastrad 
    134  1.1  riastrad 		if (r) {
    135  1.1  riastrad 			DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
    136  1.1  riastrad 			goto out_lclean_unpin;
    137  1.1  riastrad 		}
    138  1.1  riastrad 
    139  1.5  riastrad 		r = dma_fence_wait(fence, false);
    140  1.1  riastrad 		if (r) {
    141  1.1  riastrad 			DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
    142  1.1  riastrad 			goto out_lclean_unpin;
    143  1.1  riastrad 		}
    144  1.1  riastrad 
    145  1.5  riastrad 		dma_fence_put(fence);
    146  1.5  riastrad 		fence = NULL;
    147  1.1  riastrad 
    148  1.1  riastrad 		r = amdgpu_bo_kmap(vram_obj, &vram_map);
    149  1.1  riastrad 		if (r) {
    150  1.1  riastrad 			DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
    151  1.1  riastrad 			goto out_lclean_unpin;
    152  1.1  riastrad 		}
    153  1.1  riastrad 
    154  1.5  riastrad 		for (gart_start = gtt_map, gart_end = gtt_map + size,
    155  1.4  riastrad 		     vram_start = vram_map, vram_end = vram_map + size;
    156  1.1  riastrad 		     vram_start < vram_end;
    157  1.5  riastrad 		     gart_start++, vram_start++) {
    158  1.5  riastrad 			if (*vram_start != gart_start) {
    159  1.1  riastrad 				DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
    160  1.1  riastrad 					  "expected 0x%p (GTT/VRAM offset "
    161  1.1  riastrad 					  "0x%16llx/0x%16llx)\n",
    162  1.5  riastrad 					  i, *vram_start, gart_start,
    163  1.1  riastrad 					  (unsigned long long)
    164  1.5  riastrad 					  (gart_addr - adev->gmc.gart_start +
    165  1.5  riastrad 					   (void*)gart_start - gtt_map),
    166  1.1  riastrad 					  (unsigned long long)
    167  1.5  riastrad 					  (vram_addr - adev->gmc.vram_start +
    168  1.5  riastrad 					   (void*)gart_start - gtt_map));
    169  1.1  riastrad 				amdgpu_bo_kunmap(vram_obj);
    170  1.1  riastrad 				goto out_lclean_unpin;
    171  1.1  riastrad 			}
    172  1.1  riastrad 			*vram_start = vram_start;
    173  1.1  riastrad 		}
    174  1.1  riastrad 
    175  1.1  riastrad 		amdgpu_bo_kunmap(vram_obj);
    176  1.1  riastrad 
    177  1.5  riastrad 		r = amdgpu_copy_buffer(ring, vram_addr, gart_addr,
    178  1.5  riastrad 				       size, NULL, &fence, false, false);
    179  1.1  riastrad 
    180  1.1  riastrad 		if (r) {
    181  1.1  riastrad 			DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
    182  1.1  riastrad 			goto out_lclean_unpin;
    183  1.1  riastrad 		}
    184  1.1  riastrad 
    185  1.5  riastrad 		r = dma_fence_wait(fence, false);
    186  1.1  riastrad 		if (r) {
    187  1.1  riastrad 			DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
    188  1.1  riastrad 			goto out_lclean_unpin;
    189  1.1  riastrad 		}
    190  1.1  riastrad 
    191  1.5  riastrad 		dma_fence_put(fence);
    192  1.5  riastrad 		fence = NULL;
    193  1.1  riastrad 
    194  1.1  riastrad 		r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
    195  1.1  riastrad 		if (r) {
    196  1.1  riastrad 			DRM_ERROR("Failed to map GTT object after copy %d\n", i);
    197  1.1  riastrad 			goto out_lclean_unpin;
    198  1.1  riastrad 		}
    199  1.1  riastrad 
    200  1.5  riastrad 		for (gart_start = gtt_map, gart_end = gtt_map + size,
    201  1.4  riastrad 		     vram_start = vram_map, vram_end = vram_map + size;
    202  1.5  riastrad 		     gart_start < gart_end;
    203  1.5  riastrad 		     gart_start++, vram_start++) {
    204  1.5  riastrad 			if (*gart_start != vram_start) {
    205  1.1  riastrad 				DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
    206  1.1  riastrad 					  "expected 0x%p (VRAM/GTT offset "
    207  1.1  riastrad 					  "0x%16llx/0x%16llx)\n",
    208  1.5  riastrad 					  i, *gart_start, vram_start,
    209  1.1  riastrad 					  (unsigned long long)
    210  1.5  riastrad 					  (vram_addr - adev->gmc.vram_start +
    211  1.4  riastrad 					   (void*)vram_start - vram_map),
    212  1.1  riastrad 					  (unsigned long long)
    213  1.5  riastrad 					  (gart_addr - adev->gmc.gart_start +
    214  1.4  riastrad 					   (void*)vram_start - vram_map));
    215  1.1  riastrad 				amdgpu_bo_kunmap(gtt_obj[i]);
    216  1.1  riastrad 				goto out_lclean_unpin;
    217  1.1  riastrad 			}
    218  1.1  riastrad 		}
    219  1.1  riastrad 
    220  1.1  riastrad 		amdgpu_bo_kunmap(gtt_obj[i]);
    221  1.1  riastrad 
    222  1.3  riastrad 		DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%"PRIx64"\n",
    223  1.5  riastrad 			 gart_addr - adev->gmc.gart_start);
    224  1.1  riastrad 		continue;
    225  1.1  riastrad 
    226  1.1  riastrad out_lclean_unpin:
    227  1.1  riastrad 		amdgpu_bo_unpin(gtt_obj[i]);
    228  1.1  riastrad out_lclean_unres:
    229  1.1  riastrad 		amdgpu_bo_unreserve(gtt_obj[i]);
    230  1.1  riastrad out_lclean_unref:
    231  1.1  riastrad 		amdgpu_bo_unref(&gtt_obj[i]);
    232  1.1  riastrad out_lclean:
    233  1.1  riastrad 		for (--i; i >= 0; --i) {
    234  1.1  riastrad 			amdgpu_bo_unpin(gtt_obj[i]);
    235  1.1  riastrad 			amdgpu_bo_unreserve(gtt_obj[i]);
    236  1.1  riastrad 			amdgpu_bo_unref(&gtt_obj[i]);
    237  1.1  riastrad 		}
    238  1.1  riastrad 		if (fence)
    239  1.5  riastrad 			dma_fence_put(fence);
    240  1.1  riastrad 		break;
    241  1.1  riastrad 	}
    242  1.1  riastrad 
    243  1.1  riastrad 	amdgpu_bo_unpin(vram_obj);
    244  1.1  riastrad out_unres:
    245  1.1  riastrad 	amdgpu_bo_unreserve(vram_obj);
    246  1.1  riastrad out_unref:
    247  1.1  riastrad 	amdgpu_bo_unref(&vram_obj);
    248  1.1  riastrad out_cleanup:
    249  1.1  riastrad 	kfree(gtt_obj);
    250  1.1  riastrad 	if (r) {
    251  1.5  riastrad 		pr_warn("Error while testing BO move\n");
    252  1.1  riastrad 	}
    253  1.1  riastrad }
    254  1.1  riastrad 
    255  1.1  riastrad void amdgpu_test_moves(struct amdgpu_device *adev)
    256  1.1  riastrad {
    257  1.1  riastrad 	if (adev->mman.buffer_funcs)
    258  1.1  riastrad 		amdgpu_do_test_moves(adev);
    259  1.1  riastrad }
    260