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;
334babd585Smrgstatic  uint32_t  family_id;
344babd585Smrgstatic  uint32_t  chip_id;
354babd585Smrgstatic  uint32_t  chip_rev;
3600a23bdaSmrg
3700a23bdaSmrgstatic void amdgpu_vmid_reserve_test(void);
387cdc0497Smrgstatic void amdgpu_vm_unaligned_map(void);
396532f28eSmrgstatic void amdgpu_vm_mapping_test(void);
4000a23bdaSmrg
4100a23bdaSmrgCU_BOOL suite_vm_tests_enable(void)
4200a23bdaSmrg{
4300a23bdaSmrg    CU_BOOL enable = CU_TRUE;
4400a23bdaSmrg
4500a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
4600a23bdaSmrg				     &minor_version, &device_handle))
4700a23bdaSmrg		return CU_FALSE;
4800a23bdaSmrg
4900a23bdaSmrg	if (device_handle->info.family_id == AMDGPU_FAMILY_SI) {
5000a23bdaSmrg		printf("\n\nCurrently hangs the CP on this ASIC, VM suite disabled\n");
5100a23bdaSmrg		enable = CU_FALSE;
5200a23bdaSmrg	}
5300a23bdaSmrg
5400a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
5500a23bdaSmrg		return CU_FALSE;
5600a23bdaSmrg
5700a23bdaSmrg	return enable;
5800a23bdaSmrg}
5900a23bdaSmrg
6000a23bdaSmrgint suite_vm_tests_init(void)
6100a23bdaSmrg{
6200a23bdaSmrg	int r;
6300a23bdaSmrg
6400a23bdaSmrg	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
6500a23bdaSmrg				   &minor_version, &device_handle);
6600a23bdaSmrg
6700a23bdaSmrg	if (r) {
6800a23bdaSmrg		if ((r == -EACCES) && (errno == EACCES))
6900a23bdaSmrg			printf("\n\nError:%s. "
7000a23bdaSmrg				"Hint:Try to run this test program as root.",
7100a23bdaSmrg				strerror(errno));
7200a23bdaSmrg		return CUE_SINIT_FAILED;
7300a23bdaSmrg	}
7400a23bdaSmrg
7500a23bdaSmrg	return CUE_SUCCESS;
7600a23bdaSmrg}
7700a23bdaSmrg
7800a23bdaSmrgint suite_vm_tests_clean(void)
7900a23bdaSmrg{
8000a23bdaSmrg	int r = amdgpu_device_deinitialize(device_handle);
8100a23bdaSmrg
8200a23bdaSmrg	if (r == 0)
8300a23bdaSmrg		return CUE_SUCCESS;
8400a23bdaSmrg	else
8500a23bdaSmrg		return CUE_SCLEAN_FAILED;
8600a23bdaSmrg}
8700a23bdaSmrg
8800a23bdaSmrg
8900a23bdaSmrgCU_TestInfo vm_tests[] = {
9000a23bdaSmrg	{ "resere vmid test",  amdgpu_vmid_reserve_test },
917cdc0497Smrg	{ "unaligned map",  amdgpu_vm_unaligned_map },
926532f28eSmrg	{ "vm mapping test",  amdgpu_vm_mapping_test },
9300a23bdaSmrg	CU_TEST_INFO_NULL,
9400a23bdaSmrg};
9500a23bdaSmrg
9600a23bdaSmrgstatic void amdgpu_vmid_reserve_test(void)
9700a23bdaSmrg{
9800a23bdaSmrg	amdgpu_context_handle context_handle;
9900a23bdaSmrg	amdgpu_bo_handle ib_result_handle;
10000a23bdaSmrg	void *ib_result_cpu;
10100a23bdaSmrg	uint64_t ib_result_mc_address;
10200a23bdaSmrg	struct amdgpu_cs_request ibs_request;
10300a23bdaSmrg	struct amdgpu_cs_ib_info ib_info;
10400a23bdaSmrg	struct amdgpu_cs_fence fence_status;
10500a23bdaSmrg	uint32_t expired, flags;
10600a23bdaSmrg	int i, r;
10700a23bdaSmrg	amdgpu_bo_list_handle bo_list;
10800a23bdaSmrg	amdgpu_va_handle va_handle;
10900a23bdaSmrg	static uint32_t *ptr;
11041687f09Smrg	struct amdgpu_gpu_info gpu_info = {0};
11141687f09Smrg	unsigned gc_ip_type;
11241687f09Smrg
11341687f09Smrg	r = amdgpu_query_gpu_info(device_handle, &gpu_info);
11441687f09Smrg	CU_ASSERT_EQUAL(r, 0);
11541687f09Smrg
1164babd585Smrg	family_id = device_handle->info.family_id;
1174babd585Smrg	chip_id = device_handle->info.chip_external_rev;
1184babd585Smrg	chip_rev = device_handle->info.chip_rev;
1194babd585Smrg
1204babd585Smrg	gc_ip_type = (asic_is_gfx_pipe_removed(family_id, chip_id, chip_rev)) ?
12141687f09Smrg			AMDGPU_HW_IP_COMPUTE : AMDGPU_HW_IP_GFX;
12200a23bdaSmrg
12300a23bdaSmrg	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
12400a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
12500a23bdaSmrg
12600a23bdaSmrg	flags = 0;
12700a23bdaSmrg	r = amdgpu_vm_reserve_vmid(device_handle, flags);
12800a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
12900a23bdaSmrg
13000a23bdaSmrg
13100a23bdaSmrg	r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
13200a23bdaSmrg			AMDGPU_GEM_DOMAIN_GTT, 0,
13300a23bdaSmrg						    &ib_result_handle, &ib_result_cpu,
13400a23bdaSmrg						    &ib_result_mc_address, &va_handle);
13500a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
13600a23bdaSmrg
13700a23bdaSmrg	r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
13800a23bdaSmrg			       &bo_list);
13900a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
14000a23bdaSmrg
14100a23bdaSmrg	ptr = ib_result_cpu;
14200a23bdaSmrg
14300a23bdaSmrg	for (i = 0; i < 16; ++i)
14400a23bdaSmrg		ptr[i] = 0xffff1000;
14500a23bdaSmrg
14600a23bdaSmrg	memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
14700a23bdaSmrg	ib_info.ib_mc_address = ib_result_mc_address;
14800a23bdaSmrg	ib_info.size = 16;
14900a23bdaSmrg
15000a23bdaSmrg	memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
15141687f09Smrg	ibs_request.ip_type = gc_ip_type;
15200a23bdaSmrg	ibs_request.ring = 0;
15300a23bdaSmrg	ibs_request.number_of_ibs = 1;
15400a23bdaSmrg	ibs_request.ibs = &ib_info;
15500a23bdaSmrg	ibs_request.resources = bo_list;
15600a23bdaSmrg	ibs_request.fence_info.handle = NULL;
15700a23bdaSmrg
15800a23bdaSmrg	r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
15900a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
16000a23bdaSmrg
16100a23bdaSmrg
16200a23bdaSmrg	memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
16300a23bdaSmrg	fence_status.context = context_handle;
16441687f09Smrg	fence_status.ip_type = gc_ip_type;
16500a23bdaSmrg	fence_status.ip_instance = 0;
16600a23bdaSmrg	fence_status.ring = 0;
16700a23bdaSmrg	fence_status.fence = ibs_request.seq_no;
16800a23bdaSmrg
16900a23bdaSmrg	r = amdgpu_cs_query_fence_status(&fence_status,
17000a23bdaSmrg			AMDGPU_TIMEOUT_INFINITE,0, &expired);
17100a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
17200a23bdaSmrg
17300a23bdaSmrg	r = amdgpu_bo_list_destroy(bo_list);
17400a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
17500a23bdaSmrg
17600a23bdaSmrg	r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
17700a23bdaSmrg				     ib_result_mc_address, 4096);
17800a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
17900a23bdaSmrg
18000a23bdaSmrg	flags = 0;
18100a23bdaSmrg	r = amdgpu_vm_unreserve_vmid(device_handle, flags);
18200a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
18300a23bdaSmrg
18400a23bdaSmrg
18500a23bdaSmrg	r = amdgpu_cs_ctx_free(context_handle);
18600a23bdaSmrg	CU_ASSERT_EQUAL(r, 0);
18700a23bdaSmrg}
1887cdc0497Smrg
1897cdc0497Smrgstatic void amdgpu_vm_unaligned_map(void)
1907cdc0497Smrg{
1917cdc0497Smrg	const uint64_t map_size = (4ULL << 30) - (2 << 12);
1927cdc0497Smrg	struct amdgpu_bo_alloc_request request = {};
1937cdc0497Smrg	amdgpu_bo_handle buf_handle;
1947cdc0497Smrg	amdgpu_va_handle handle;
1957cdc0497Smrg	uint64_t vmc_addr;
1967cdc0497Smrg	int r;
1977cdc0497Smrg
1987cdc0497Smrg	request.alloc_size = 4ULL << 30;
1997cdc0497Smrg	request.phys_alignment = 4096;
2007cdc0497Smrg	request.preferred_heap = AMDGPU_GEM_DOMAIN_VRAM;
2017cdc0497Smrg	request.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
2027cdc0497Smrg
2037cdc0497Smrg	r = amdgpu_bo_alloc(device_handle, &request, &buf_handle);
2047cdc0497Smrg	/* Don't let the test fail if the device doesn't have enough VRAM */
2057cdc0497Smrg	if (r)
2067cdc0497Smrg		return;
2077cdc0497Smrg
2087cdc0497Smrg	r = amdgpu_va_range_alloc(device_handle, amdgpu_gpu_va_range_general,
2097cdc0497Smrg				  4ULL << 30, 1ULL << 30, 0, &vmc_addr,
2107cdc0497Smrg				  &handle, 0);
2117cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
2127cdc0497Smrg	if (r)
2137cdc0497Smrg		goto error_va_alloc;
2147cdc0497Smrg
2157cdc0497Smrg	vmc_addr += 1 << 12;
2167cdc0497Smrg
2177cdc0497Smrg	r = amdgpu_bo_va_op(buf_handle, 0, map_size, vmc_addr, 0,
2187cdc0497Smrg			    AMDGPU_VA_OP_MAP);
2197cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
2207cdc0497Smrg	if (r)
2217cdc0497Smrg		goto error_va_alloc;
2227cdc0497Smrg
2237cdc0497Smrg	amdgpu_bo_va_op(buf_handle, 0, map_size, vmc_addr, 0,
2247cdc0497Smrg			AMDGPU_VA_OP_UNMAP);
2257cdc0497Smrg
2267cdc0497Smrgerror_va_alloc:
2277cdc0497Smrg	amdgpu_bo_free(buf_handle);
2286532f28eSmrg}
2296532f28eSmrg
2306532f28eSmrgstatic void amdgpu_vm_mapping_test(void)
2316532f28eSmrg{
2326532f28eSmrg	struct amdgpu_bo_alloc_request req = {0};
2336532f28eSmrg	struct drm_amdgpu_info_device dev_info;
2346532f28eSmrg	const uint64_t size = 4096;
2356532f28eSmrg	amdgpu_bo_handle buf;
2366532f28eSmrg	uint64_t addr;
2376532f28eSmrg	int r;
2386532f28eSmrg
2396532f28eSmrg	req.alloc_size = size;
2406532f28eSmrg	req.phys_alignment = 0;
2416532f28eSmrg	req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
2426532f28eSmrg	req.flags = 0;
2436532f28eSmrg
2446532f28eSmrg	r = amdgpu_bo_alloc(device_handle, &req, &buf);
2456532f28eSmrg	CU_ASSERT_EQUAL(r, 0);
2466532f28eSmrg
2476532f28eSmrg	r = amdgpu_query_info(device_handle, AMDGPU_INFO_DEV_INFO,
2486532f28eSmrg			      sizeof(dev_info), &dev_info);
2496532f28eSmrg	CU_ASSERT_EQUAL(r, 0);
2506532f28eSmrg
2516532f28eSmrg	addr = dev_info.virtual_address_offset;
2526532f28eSmrg	r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
2536532f28eSmrg	CU_ASSERT_EQUAL(r, 0);
2546532f28eSmrg
2556532f28eSmrg	addr = dev_info.virtual_address_max - size;
2566532f28eSmrg	r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
2576532f28eSmrg	CU_ASSERT_EQUAL(r, 0);
2586532f28eSmrg
2596532f28eSmrg	if (dev_info.high_va_offset) {
2606532f28eSmrg		addr = dev_info.high_va_offset;
2616532f28eSmrg		r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
2626532f28eSmrg		CU_ASSERT_EQUAL(r, 0);
2636532f28eSmrg
2646532f28eSmrg		addr = dev_info.high_va_max - size;
2656532f28eSmrg		r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
2666532f28eSmrg		CU_ASSERT_EQUAL(r, 0);
2676532f28eSmrg	}
2687cdc0497Smrg
2696532f28eSmrg	amdgpu_bo_free(buf);
2707cdc0497Smrg}
271