1d722e3fbSopenharmony_ci/*
2d722e3fbSopenharmony_ci * Copyright 2017 Advanced Micro Devices, Inc.
3d722e3fbSopenharmony_ci *
4d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation
7d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10d722e3fbSopenharmony_ci *
11d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in
12d722e3fbSopenharmony_ci * all copies or substantial portions of the Software.
13d722e3fbSopenharmony_ci *
14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20d722e3fbSopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
21d722e3fbSopenharmony_ci *
22d722e3fbSopenharmony_ci*/
23d722e3fbSopenharmony_ci
24d722e3fbSopenharmony_ci#include "CUnit/Basic.h"
25d722e3fbSopenharmony_ci
26d722e3fbSopenharmony_ci#include "amdgpu_test.h"
27d722e3fbSopenharmony_ci#include "amdgpu_drm.h"
28d722e3fbSopenharmony_ci#include "amdgpu_internal.h"
29d722e3fbSopenharmony_ci
30d722e3fbSopenharmony_cistatic  amdgpu_device_handle device_handle;
31d722e3fbSopenharmony_cistatic  uint32_t  major_version;
32d722e3fbSopenharmony_cistatic  uint32_t  minor_version;
33d722e3fbSopenharmony_cistatic  uint32_t  family_id;
34d722e3fbSopenharmony_cistatic  uint32_t  chip_id;
35d722e3fbSopenharmony_cistatic  uint32_t  chip_rev;
36d722e3fbSopenharmony_ci
37d722e3fbSopenharmony_cistatic void amdgpu_vmid_reserve_test(void);
38d722e3fbSopenharmony_cistatic void amdgpu_vm_unaligned_map(void);
39d722e3fbSopenharmony_cistatic void amdgpu_vm_mapping_test(void);
40d722e3fbSopenharmony_ci
41d722e3fbSopenharmony_ciCU_BOOL suite_vm_tests_enable(void)
42d722e3fbSopenharmony_ci{
43d722e3fbSopenharmony_ci    CU_BOOL enable = CU_TRUE;
44d722e3fbSopenharmony_ci
45d722e3fbSopenharmony_ci	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
46d722e3fbSopenharmony_ci				     &minor_version, &device_handle))
47d722e3fbSopenharmony_ci		return CU_FALSE;
48d722e3fbSopenharmony_ci
49d722e3fbSopenharmony_ci	if (device_handle->info.family_id == AMDGPU_FAMILY_SI) {
50d722e3fbSopenharmony_ci		printf("\n\nCurrently hangs the CP on this ASIC, VM suite disabled\n");
51d722e3fbSopenharmony_ci		enable = CU_FALSE;
52d722e3fbSopenharmony_ci	}
53d722e3fbSopenharmony_ci
54d722e3fbSopenharmony_ci	if (amdgpu_device_deinitialize(device_handle))
55d722e3fbSopenharmony_ci		return CU_FALSE;
56d722e3fbSopenharmony_ci
57d722e3fbSopenharmony_ci	return enable;
58d722e3fbSopenharmony_ci}
59d722e3fbSopenharmony_ci
60d722e3fbSopenharmony_ciint suite_vm_tests_init(void)
61d722e3fbSopenharmony_ci{
62d722e3fbSopenharmony_ci	int r;
63d722e3fbSopenharmony_ci
64d722e3fbSopenharmony_ci	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
65d722e3fbSopenharmony_ci				   &minor_version, &device_handle);
66d722e3fbSopenharmony_ci
67d722e3fbSopenharmony_ci	if (r) {
68d722e3fbSopenharmony_ci		if ((r == -EACCES) && (errno == EACCES))
69d722e3fbSopenharmony_ci			printf("\n\nError:%s. "
70d722e3fbSopenharmony_ci				"Hint:Try to run this test program as root.",
71d722e3fbSopenharmony_ci				strerror(errno));
72d722e3fbSopenharmony_ci		return CUE_SINIT_FAILED;
73d722e3fbSopenharmony_ci	}
74d722e3fbSopenharmony_ci
75d722e3fbSopenharmony_ci	return CUE_SUCCESS;
76d722e3fbSopenharmony_ci}
77d722e3fbSopenharmony_ci
78d722e3fbSopenharmony_ciint suite_vm_tests_clean(void)
79d722e3fbSopenharmony_ci{
80d722e3fbSopenharmony_ci	int r = amdgpu_device_deinitialize(device_handle);
81d722e3fbSopenharmony_ci
82d722e3fbSopenharmony_ci	if (r == 0)
83d722e3fbSopenharmony_ci		return CUE_SUCCESS;
84d722e3fbSopenharmony_ci	else
85d722e3fbSopenharmony_ci		return CUE_SCLEAN_FAILED;
86d722e3fbSopenharmony_ci}
87d722e3fbSopenharmony_ci
88d722e3fbSopenharmony_ci
89d722e3fbSopenharmony_ciCU_TestInfo vm_tests[] = {
90d722e3fbSopenharmony_ci	{ "resere vmid test",  amdgpu_vmid_reserve_test },
91d722e3fbSopenharmony_ci	{ "unaligned map",  amdgpu_vm_unaligned_map },
92d722e3fbSopenharmony_ci	{ "vm mapping test",  amdgpu_vm_mapping_test },
93d722e3fbSopenharmony_ci	CU_TEST_INFO_NULL,
94d722e3fbSopenharmony_ci};
95d722e3fbSopenharmony_ci
96d722e3fbSopenharmony_cistatic void amdgpu_vmid_reserve_test(void)
97d722e3fbSopenharmony_ci{
98d722e3fbSopenharmony_ci	amdgpu_context_handle context_handle;
99d722e3fbSopenharmony_ci	amdgpu_bo_handle ib_result_handle;
100d722e3fbSopenharmony_ci	void *ib_result_cpu;
101d722e3fbSopenharmony_ci	uint64_t ib_result_mc_address;
102d722e3fbSopenharmony_ci	struct amdgpu_cs_request ibs_request;
103d722e3fbSopenharmony_ci	struct amdgpu_cs_ib_info ib_info;
104d722e3fbSopenharmony_ci	struct amdgpu_cs_fence fence_status;
105d722e3fbSopenharmony_ci	uint32_t expired, flags;
106d722e3fbSopenharmony_ci	int i, r;
107d722e3fbSopenharmony_ci	amdgpu_bo_list_handle bo_list;
108d722e3fbSopenharmony_ci	amdgpu_va_handle va_handle;
109d722e3fbSopenharmony_ci	static uint32_t *ptr;
110d722e3fbSopenharmony_ci	struct amdgpu_gpu_info gpu_info = {0};
111d722e3fbSopenharmony_ci	unsigned gc_ip_type;
112d722e3fbSopenharmony_ci
113d722e3fbSopenharmony_ci	r = amdgpu_query_gpu_info(device_handle, &gpu_info);
114d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
115d722e3fbSopenharmony_ci
116d722e3fbSopenharmony_ci	family_id = device_handle->info.family_id;
117d722e3fbSopenharmony_ci	chip_id = device_handle->info.chip_external_rev;
118d722e3fbSopenharmony_ci	chip_rev = device_handle->info.chip_rev;
119d722e3fbSopenharmony_ci
120d722e3fbSopenharmony_ci	gc_ip_type = (asic_is_gfx_pipe_removed(family_id, chip_id, chip_rev)) ?
121d722e3fbSopenharmony_ci			AMDGPU_HW_IP_COMPUTE : AMDGPU_HW_IP_GFX;
122d722e3fbSopenharmony_ci
123d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
124d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
125d722e3fbSopenharmony_ci
126d722e3fbSopenharmony_ci	flags = 0;
127d722e3fbSopenharmony_ci	r = amdgpu_vm_reserve_vmid(device_handle, flags);
128d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
129d722e3fbSopenharmony_ci
130d722e3fbSopenharmony_ci
131d722e3fbSopenharmony_ci	r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
132d722e3fbSopenharmony_ci			AMDGPU_GEM_DOMAIN_GTT, 0,
133d722e3fbSopenharmony_ci						    &ib_result_handle, &ib_result_cpu,
134d722e3fbSopenharmony_ci						    &ib_result_mc_address, &va_handle);
135d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
136d722e3fbSopenharmony_ci
137d722e3fbSopenharmony_ci	r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
138d722e3fbSopenharmony_ci			       &bo_list);
139d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
140d722e3fbSopenharmony_ci
141d722e3fbSopenharmony_ci	ptr = ib_result_cpu;
142d722e3fbSopenharmony_ci
143d722e3fbSopenharmony_ci	for (i = 0; i < 16; ++i)
144d722e3fbSopenharmony_ci		ptr[i] = 0xffff1000;
145d722e3fbSopenharmony_ci
146d722e3fbSopenharmony_ci	memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
147d722e3fbSopenharmony_ci	ib_info.ib_mc_address = ib_result_mc_address;
148d722e3fbSopenharmony_ci	ib_info.size = 16;
149d722e3fbSopenharmony_ci
150d722e3fbSopenharmony_ci	memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
151d722e3fbSopenharmony_ci	ibs_request.ip_type = gc_ip_type;
152d722e3fbSopenharmony_ci	ibs_request.ring = 0;
153d722e3fbSopenharmony_ci	ibs_request.number_of_ibs = 1;
154d722e3fbSopenharmony_ci	ibs_request.ibs = &ib_info;
155d722e3fbSopenharmony_ci	ibs_request.resources = bo_list;
156d722e3fbSopenharmony_ci	ibs_request.fence_info.handle = NULL;
157d722e3fbSopenharmony_ci
158d722e3fbSopenharmony_ci	r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
159d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
160d722e3fbSopenharmony_ci
161d722e3fbSopenharmony_ci
162d722e3fbSopenharmony_ci	memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
163d722e3fbSopenharmony_ci	fence_status.context = context_handle;
164d722e3fbSopenharmony_ci	fence_status.ip_type = gc_ip_type;
165d722e3fbSopenharmony_ci	fence_status.ip_instance = 0;
166d722e3fbSopenharmony_ci	fence_status.ring = 0;
167d722e3fbSopenharmony_ci	fence_status.fence = ibs_request.seq_no;
168d722e3fbSopenharmony_ci
169d722e3fbSopenharmony_ci	r = amdgpu_cs_query_fence_status(&fence_status,
170d722e3fbSopenharmony_ci			AMDGPU_TIMEOUT_INFINITE,0, &expired);
171d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
172d722e3fbSopenharmony_ci
173d722e3fbSopenharmony_ci	r = amdgpu_bo_list_destroy(bo_list);
174d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
175d722e3fbSopenharmony_ci
176d722e3fbSopenharmony_ci	r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
177d722e3fbSopenharmony_ci				     ib_result_mc_address, 4096);
178d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
179d722e3fbSopenharmony_ci
180d722e3fbSopenharmony_ci	flags = 0;
181d722e3fbSopenharmony_ci	r = amdgpu_vm_unreserve_vmid(device_handle, flags);
182d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
183d722e3fbSopenharmony_ci
184d722e3fbSopenharmony_ci
185d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_free(context_handle);
186d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
187d722e3fbSopenharmony_ci}
188d722e3fbSopenharmony_ci
189d722e3fbSopenharmony_cistatic void amdgpu_vm_unaligned_map(void)
190d722e3fbSopenharmony_ci{
191d722e3fbSopenharmony_ci	const uint64_t map_size = (4ULL << 30) - (2 << 12);
192d722e3fbSopenharmony_ci	struct amdgpu_bo_alloc_request request = {};
193d722e3fbSopenharmony_ci	amdgpu_bo_handle buf_handle;
194d722e3fbSopenharmony_ci	amdgpu_va_handle handle;
195d722e3fbSopenharmony_ci	uint64_t vmc_addr;
196d722e3fbSopenharmony_ci	int r;
197d722e3fbSopenharmony_ci
198d722e3fbSopenharmony_ci	request.alloc_size = 4ULL << 30;
199d722e3fbSopenharmony_ci	request.phys_alignment = 4096;
200d722e3fbSopenharmony_ci	request.preferred_heap = AMDGPU_GEM_DOMAIN_VRAM;
201d722e3fbSopenharmony_ci	request.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
202d722e3fbSopenharmony_ci
203d722e3fbSopenharmony_ci	r = amdgpu_bo_alloc(device_handle, &request, &buf_handle);
204d722e3fbSopenharmony_ci	/* Don't let the test fail if the device doesn't have enough VRAM */
205d722e3fbSopenharmony_ci	if (r)
206d722e3fbSopenharmony_ci		return;
207d722e3fbSopenharmony_ci
208d722e3fbSopenharmony_ci	r = amdgpu_va_range_alloc(device_handle, amdgpu_gpu_va_range_general,
209d722e3fbSopenharmony_ci				  4ULL << 30, 1ULL << 30, 0, &vmc_addr,
210d722e3fbSopenharmony_ci				  &handle, 0);
211d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
212d722e3fbSopenharmony_ci	if (r)
213d722e3fbSopenharmony_ci		goto error_va_alloc;
214d722e3fbSopenharmony_ci
215d722e3fbSopenharmony_ci	vmc_addr += 1 << 12;
216d722e3fbSopenharmony_ci
217d722e3fbSopenharmony_ci	r = amdgpu_bo_va_op(buf_handle, 0, map_size, vmc_addr, 0,
218d722e3fbSopenharmony_ci			    AMDGPU_VA_OP_MAP);
219d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
220d722e3fbSopenharmony_ci	if (r)
221d722e3fbSopenharmony_ci		goto error_va_alloc;
222d722e3fbSopenharmony_ci
223d722e3fbSopenharmony_ci	amdgpu_bo_va_op(buf_handle, 0, map_size, vmc_addr, 0,
224d722e3fbSopenharmony_ci			AMDGPU_VA_OP_UNMAP);
225d722e3fbSopenharmony_ci
226d722e3fbSopenharmony_cierror_va_alloc:
227d722e3fbSopenharmony_ci	amdgpu_bo_free(buf_handle);
228d722e3fbSopenharmony_ci}
229d722e3fbSopenharmony_ci
230d722e3fbSopenharmony_cistatic void amdgpu_vm_mapping_test(void)
231d722e3fbSopenharmony_ci{
232d722e3fbSopenharmony_ci	struct amdgpu_bo_alloc_request req = {0};
233d722e3fbSopenharmony_ci	struct drm_amdgpu_info_device dev_info;
234d722e3fbSopenharmony_ci	const uint64_t size = 4096;
235d722e3fbSopenharmony_ci	amdgpu_bo_handle buf;
236d722e3fbSopenharmony_ci	uint64_t addr;
237d722e3fbSopenharmony_ci	int r;
238d722e3fbSopenharmony_ci
239d722e3fbSopenharmony_ci	req.alloc_size = size;
240d722e3fbSopenharmony_ci	req.phys_alignment = 0;
241d722e3fbSopenharmony_ci	req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
242d722e3fbSopenharmony_ci	req.flags = 0;
243d722e3fbSopenharmony_ci
244d722e3fbSopenharmony_ci	r = amdgpu_bo_alloc(device_handle, &req, &buf);
245d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
246d722e3fbSopenharmony_ci
247d722e3fbSopenharmony_ci	r = amdgpu_query_info(device_handle, AMDGPU_INFO_DEV_INFO,
248d722e3fbSopenharmony_ci			      sizeof(dev_info), &dev_info);
249d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
250d722e3fbSopenharmony_ci
251d722e3fbSopenharmony_ci	addr = dev_info.virtual_address_offset;
252d722e3fbSopenharmony_ci	r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
253d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
254d722e3fbSopenharmony_ci
255d722e3fbSopenharmony_ci	addr = dev_info.virtual_address_max - size;
256d722e3fbSopenharmony_ci	r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
257d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
258d722e3fbSopenharmony_ci
259d722e3fbSopenharmony_ci	if (dev_info.high_va_offset) {
260d722e3fbSopenharmony_ci		addr = dev_info.high_va_offset;
261d722e3fbSopenharmony_ci		r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
262d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
263d722e3fbSopenharmony_ci
264d722e3fbSopenharmony_ci		addr = dev_info.high_va_max - size;
265d722e3fbSopenharmony_ci		r = amdgpu_bo_va_op(buf, 0, size, addr, 0, AMDGPU_VA_OP_MAP);
266d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
267d722e3fbSopenharmony_ci	}
268d722e3fbSopenharmony_ci
269d722e3fbSopenharmony_ci	amdgpu_bo_free(buf);
270d722e3fbSopenharmony_ci}
271