vm_tests.c revision 7cdc0497
100a23bdaSmrg/*
200a23bdaSmrg * Copyright 2017 Advanced Micro Devices, Inc.
300a23bdaSmrg *
400a23bdaSmrg * Permission is hereby granted, free of charge, to any person obtaining a
500a23bdaSmrg * copy of this software and associated documentation files (the "Software"),
600a23bdaSmrg * to deal in the Software without restriction, including without limitation
700a23bdaSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
800a23bdaSmrg * and/or sell copies of the Software, and to permit persons to whom the
900a23bdaSmrg * Software is furnished to do so, subject to the following conditions:
1000a23bdaSmrg *
1100a23bdaSmrg * The above copyright notice and this permission notice shall be included in
1200a23bdaSmrg * all copies or substantial portions of the Software.
1300a23bdaSmrg *
1400a23bdaSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1500a23bdaSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1600a23bdaSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1700a23bdaSmrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1800a23bdaSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1900a23bdaSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2000a23bdaSmrg * OTHER DEALINGS IN THE SOFTWARE.
2100a23bdaSmrg *
2200a23bdaSmrg*/
2300a23bdaSmrg
2400a23bdaSmrg#include "CUnit/Basic.h"
2500a23bdaSmrg
2600a23bdaSmrg#include "amdgpu_test.h"
2700a23bdaSmrg#include "amdgpu_drm.h"
2800a23bdaSmrg#include "amdgpu_internal.h"
2900a23bdaSmrg
3000a23bdaSmrgstatic  amdgpu_device_handle device_handle;
3100a23bdaSmrgstatic  uint32_t  major_version;
3200a23bdaSmrgstatic  uint32_t  minor_version;
3300a23bdaSmrg
3400a23bdaSmrgstatic void amdgpu_vmid_reserve_test(void);
357cdc0497Smrgstatic void amdgpu_vm_unaligned_map(void);
3600a23bdaSmrg
3700a23bdaSmrgCU_BOOL suite_vm_tests_enable(void)
3800a23bdaSmrg{
3900a23bdaSmrg    CU_BOOL enable = CU_TRUE;
4000a23bdaSmrg
4100a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
4200a23bdaSmrg				     &minor_version, &device_handle))
4300a23bdaSmrg		return CU_FALSE;
4400a23bdaSmrg
4500a23bdaSmrg	if (device_handle->info.family_id == AMDGPU_FAMILY_SI) {
4600a23bdaSmrg		printf("\n\nCurrently hangs the CP on this ASIC, VM suite disabled\n");
4700a23bdaSmrg		enable = CU_FALSE;
4800a23bdaSmrg	}
4900a23bdaSmrg
5000a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
5100a23bdaSmrg		return CU_FALSE;
5200a23bdaSmrg
5300a23bdaSmrg	return enable;
5400a23bdaSmrg}
5500a23bdaSmrg
5600a23bdaSmrgint suite_vm_tests_init(void)
5700a23bdaSmrg{
5800a23bdaSmrg	int r;
5900a23bdaSmrg
6000a23bdaSmrg	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
6100a23bdaSmrg				   &minor_version, &device_handle);
6200a23bdaSmrg
6300a23bdaSmrg	if (r) {
6400a23bdaSmrg		if ((r == -EACCES) && (errno == EACCES))
6500a23bdaSmrg			printf("\n\nError:%s. "
6600a23bdaSmrg				"Hint:Try to run this test program as root.",
6700a23bdaSmrg				strerror(errno));
6800a23bdaSmrg		return CUE_SINIT_FAILED;
6900a23bdaSmrg	}
7000a23bdaSmrg
7100a23bdaSmrg	return CUE_SUCCESS;
7200a23bdaSmrg}
7300a23bdaSmrg
7400a23bdaSmrgint suite_vm_tests_clean(void)
7500a23bdaSmrg{
7600a23bdaSmrg	int r = amdgpu_device_deinitialize(device_handle);
7700a23bdaSmrg
7800a23bdaSmrg	if (r == 0)
7900a23bdaSmrg		return CUE_SUCCESS;
8000a23bdaSmrg	else
8100a23bdaSmrg		return CUE_SCLEAN_FAILED;
8200a23bdaSmrg}
8300a23bdaSmrg
8400a23bdaSmrg
8500a23bdaSmrgCU_TestInfo vm_tests[] = {
8600a23bdaSmrg	{ "resere vmid test",  amdgpu_vmid_reserve_test },
877cdc0497Smrg	{ "unaligned map",  amdgpu_vm_unaligned_map },
8800a23bdaSmrg	CU_TEST_INFO_NULL,
8900a23bdaSmrg};
9000a23bdaSmrg
9100a23bdaSmrgstatic void amdgpu_vmid_reserve_test(void)
9200a23bdaSmrg{
9300a23bdaSmrg	amdgpu_context_handle context_handle;
9400a23bdaSmrg	amdgpu_bo_handle ib_result_handle;
9500a23bdaSmrg	void *ib_result_cpu;
9600a23bdaSmrg	uint64_t ib_result_mc_address;
9700a23bdaSmrg	struct amdgpu_cs_request ibs_request;
9800a23bdaSmrg	struct amdgpu_cs_ib_info ib_info;
9900a23bdaSmrg	struct amdgpu_cs_fence fence_status;
10000a23bdaSmrg	uint32_t expired, flags;
10100a23bdaSmrg	int i, r;
10200a23bdaSmrg	amdgpu_bo_list_handle bo_list;
10300a23bdaSmrg	amdgpu_va_handle va_handle;
10400a23bdaSmrg	static uint32_t *ptr;
10500a23bdaSmrg
10600a23bdaSmrg	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
10700a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
10800a23bdaSmrg
10900a23bdaSmrg	flags = 0;
11000a23bdaSmrg	r = amdgpu_vm_reserve_vmid(device_handle, flags);
11100a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
11200a23bdaSmrg
11300a23bdaSmrg
11400a23bdaSmrg	r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
11500a23bdaSmrg			AMDGPU_GEM_DOMAIN_GTT, 0,
11600a23bdaSmrg						    &ib_result_handle, &ib_result_cpu,
11700a23bdaSmrg						    &ib_result_mc_address, &va_handle);
11800a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
11900a23bdaSmrg
12000a23bdaSmrg	r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
12100a23bdaSmrg			       &bo_list);
12200a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
12300a23bdaSmrg
12400a23bdaSmrg	ptr = ib_result_cpu;
12500a23bdaSmrg
12600a23bdaSmrg	for (i = 0; i < 16; ++i)
12700a23bdaSmrg		ptr[i] = 0xffff1000;
12800a23bdaSmrg
12900a23bdaSmrg	memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
13000a23bdaSmrg	ib_info.ib_mc_address = ib_result_mc_address;
13100a23bdaSmrg	ib_info.size = 16;
13200a23bdaSmrg
13300a23bdaSmrg	memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
13400a23bdaSmrg	ibs_request.ip_type = AMDGPU_HW_IP_GFX;
13500a23bdaSmrg	ibs_request.ring = 0;
13600a23bdaSmrg	ibs_request.number_of_ibs = 1;
13700a23bdaSmrg	ibs_request.ibs = &ib_info;
13800a23bdaSmrg	ibs_request.resources = bo_list;
13900a23bdaSmrg	ibs_request.fence_info.handle = NULL;
14000a23bdaSmrg
14100a23bdaSmrg	r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
14200a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
14300a23bdaSmrg
14400a23bdaSmrg
14500a23bdaSmrg	memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
14600a23bdaSmrg	fence_status.context = context_handle;
14700a23bdaSmrg	fence_status.ip_type = AMDGPU_HW_IP_GFX;
14800a23bdaSmrg	fence_status.ip_instance = 0;
14900a23bdaSmrg	fence_status.ring = 0;
15000a23bdaSmrg	fence_status.fence = ibs_request.seq_no;
15100a23bdaSmrg
15200a23bdaSmrg	r = amdgpu_cs_query_fence_status(&fence_status,
15300a23bdaSmrg			AMDGPU_TIMEOUT_INFINITE,0, &expired);
15400a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
15500a23bdaSmrg
15600a23bdaSmrg	r = amdgpu_bo_list_destroy(bo_list);
15700a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
15800a23bdaSmrg
15900a23bdaSmrg	r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
16000a23bdaSmrg				     ib_result_mc_address, 4096);
16100a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
16200a23bdaSmrg
16300a23bdaSmrg	flags = 0;
16400a23bdaSmrg	r = amdgpu_vm_unreserve_vmid(device_handle, flags);
16500a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
16600a23bdaSmrg
16700a23bdaSmrg
16800a23bdaSmrg	r = amdgpu_cs_ctx_free(context_handle);
16900a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
17000a23bdaSmrg}
1717cdc0497Smrg
1727cdc0497Smrgstatic void amdgpu_vm_unaligned_map(void)
1737cdc0497Smrg{
1747cdc0497Smrg	const uint64_t map_size = (4ULL << 30) - (2 << 12);
1757cdc0497Smrg	struct amdgpu_bo_alloc_request request = {};
1767cdc0497Smrg	amdgpu_bo_handle buf_handle;
1777cdc0497Smrg	amdgpu_va_handle handle;
1787cdc0497Smrg	uint64_t vmc_addr;
1797cdc0497Smrg	int r;
1807cdc0497Smrg
1817cdc0497Smrg	request.alloc_size = 4ULL << 30;
1827cdc0497Smrg	request.phys_alignment = 4096;
1837cdc0497Smrg	request.preferred_heap = AMDGPU_GEM_DOMAIN_VRAM;
1847cdc0497Smrg	request.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1857cdc0497Smrg
1867cdc0497Smrg	r = amdgpu_bo_alloc(device_handle, &request, &buf_handle);
1877cdc0497Smrg	/* Don't let the test fail if the device doesn't have enough VRAM */
1887cdc0497Smrg	if (r)
1897cdc0497Smrg		return;
1907cdc0497Smrg
1917cdc0497Smrg	r = amdgpu_va_range_alloc(device_handle, amdgpu_gpu_va_range_general,
1927cdc0497Smrg				  4ULL << 30, 1ULL << 30, 0, &vmc_addr,
1937cdc0497Smrg				  &handle, 0);
1947cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
1957cdc0497Smrg	if (r)
1967cdc0497Smrg		goto error_va_alloc;
1977cdc0497Smrg
1987cdc0497Smrg	vmc_addr += 1 << 12;
1997cdc0497Smrg
2007cdc0497Smrg	r = amdgpu_bo_va_op(buf_handle, 0, map_size, vmc_addr, 0,
2017cdc0497Smrg			    AMDGPU_VA_OP_MAP);
2027cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
2037cdc0497Smrg	if (r)
2047cdc0497Smrg		goto error_va_alloc;
2057cdc0497Smrg
2067cdc0497Smrg	amdgpu_bo_va_op(buf_handle, 0, map_size, vmc_addr, 0,
2077cdc0497Smrg			AMDGPU_VA_OP_UNMAP);
2087cdc0497Smrg
2097cdc0497Smrgerror_va_alloc:
2107cdc0497Smrg	amdgpu_bo_free(buf_handle);
2117cdc0497Smrg
2127cdc0497Smrg}
213