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 <stdio.h>
25d722e3fbSopenharmony_ci#include <stdlib.h>
26d722e3fbSopenharmony_ci#include <unistd.h>
27d722e3fbSopenharmony_ci#if HAVE_ALLOCA_H
28d722e3fbSopenharmony_ci# include <alloca.h>
29d722e3fbSopenharmony_ci#endif
30d722e3fbSopenharmony_ci
31d722e3fbSopenharmony_ci#include "CUnit/Basic.h"
32d722e3fbSopenharmony_ci
33d722e3fbSopenharmony_ci#include "amdgpu_test.h"
34d722e3fbSopenharmony_ci#include "amdgpu_drm.h"
35d722e3fbSopenharmony_ci#include "amdgpu_internal.h"
36d722e3fbSopenharmony_ci
37d722e3fbSopenharmony_ci#include <pthread.h>
38d722e3fbSopenharmony_ci
39d722e3fbSopenharmony_ci
40d722e3fbSopenharmony_ci/*
41d722e3fbSopenharmony_ci * This defines the delay in MS after which memory location designated for
42d722e3fbSopenharmony_ci * compression against reference value is written to, unblocking command
43d722e3fbSopenharmony_ci * processor
44d722e3fbSopenharmony_ci */
45d722e3fbSopenharmony_ci#define WRITE_MEM_ADDRESS_DELAY_MS 100
46d722e3fbSopenharmony_ci
47d722e3fbSopenharmony_ci#define	PACKET_TYPE3	3
48d722e3fbSopenharmony_ci
49d722e3fbSopenharmony_ci#define PACKET3(op, n)	((PACKET_TYPE3 << 30) |				\
50d722e3fbSopenharmony_ci			 (((op) & 0xFF) << 8) |				\
51d722e3fbSopenharmony_ci			 ((n) & 0x3FFF) << 16)
52d722e3fbSopenharmony_ci
53d722e3fbSopenharmony_ci#define	PACKET3_WAIT_REG_MEM				0x3C
54d722e3fbSopenharmony_ci#define		WAIT_REG_MEM_FUNCTION(x)                ((x) << 0)
55d722e3fbSopenharmony_ci		/* 0 - always
56d722e3fbSopenharmony_ci		 * 1 - <
57d722e3fbSopenharmony_ci		 * 2 - <=
58d722e3fbSopenharmony_ci		 * 3 - ==
59d722e3fbSopenharmony_ci		 * 4 - !=
60d722e3fbSopenharmony_ci		 * 5 - >=
61d722e3fbSopenharmony_ci		 * 6 - >
62d722e3fbSopenharmony_ci		 */
63d722e3fbSopenharmony_ci#define		WAIT_REG_MEM_MEM_SPACE(x)               ((x) << 4)
64d722e3fbSopenharmony_ci		/* 0 - reg
65d722e3fbSopenharmony_ci		 * 1 - mem
66d722e3fbSopenharmony_ci		 */
67d722e3fbSopenharmony_ci#define		WAIT_REG_MEM_OPERATION(x)               ((x) << 6)
68d722e3fbSopenharmony_ci		/* 0 - wait_reg_mem
69d722e3fbSopenharmony_ci		 * 1 - wr_wait_wr_reg
70d722e3fbSopenharmony_ci		 */
71d722e3fbSopenharmony_ci#define		WAIT_REG_MEM_ENGINE(x)                  ((x) << 8)
72d722e3fbSopenharmony_ci		/* 0 - me
73d722e3fbSopenharmony_ci		 * 1 - pfp
74d722e3fbSopenharmony_ci		 */
75d722e3fbSopenharmony_ci
76d722e3fbSopenharmony_ci#define	PACKET3_WRITE_DATA				0x37
77d722e3fbSopenharmony_ci#define		WRITE_DATA_DST_SEL(x)                   ((x) << 8)
78d722e3fbSopenharmony_ci		/* 0 - register
79d722e3fbSopenharmony_ci		 * 1 - memory (sync - via GRBM)
80d722e3fbSopenharmony_ci		 * 2 - gl2
81d722e3fbSopenharmony_ci		 * 3 - gds
82d722e3fbSopenharmony_ci		 * 4 - reserved
83d722e3fbSopenharmony_ci		 * 5 - memory (async - direct)
84d722e3fbSopenharmony_ci		 */
85d722e3fbSopenharmony_ci#define		WR_ONE_ADDR                             (1 << 16)
86d722e3fbSopenharmony_ci#define		WR_CONFIRM                              (1 << 20)
87d722e3fbSopenharmony_ci#define		WRITE_DATA_CACHE_POLICY(x)              ((x) << 25)
88d722e3fbSopenharmony_ci		/* 0 - LRU
89d722e3fbSopenharmony_ci		 * 1 - Stream
90d722e3fbSopenharmony_ci		 */
91d722e3fbSopenharmony_ci#define		WRITE_DATA_ENGINE_SEL(x)                ((x) << 30)
92d722e3fbSopenharmony_ci		/* 0 - me
93d722e3fbSopenharmony_ci		 * 1 - pfp
94d722e3fbSopenharmony_ci		 * 2 - ce
95d722e3fbSopenharmony_ci		 */
96d722e3fbSopenharmony_ci
97d722e3fbSopenharmony_ci#define mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR                                      0x54f
98d722e3fbSopenharmony_ci
99d722e3fbSopenharmony_ci#define SDMA_PKT_HEADER_OP(x)	(x & 0xff)
100d722e3fbSopenharmony_ci#define SDMA_OP_POLL_REGMEM  8
101d722e3fbSopenharmony_ci
102d722e3fbSopenharmony_cistatic  amdgpu_device_handle device_handle;
103d722e3fbSopenharmony_cistatic  uint32_t  major_version;
104d722e3fbSopenharmony_cistatic  uint32_t  minor_version;
105d722e3fbSopenharmony_ci
106d722e3fbSopenharmony_cistatic pthread_t stress_thread;
107d722e3fbSopenharmony_cistatic uint32_t *ptr;
108d722e3fbSopenharmony_ci
109d722e3fbSopenharmony_cistatic uint32_t family_id;
110d722e3fbSopenharmony_cistatic uint32_t chip_rev;
111d722e3fbSopenharmony_cistatic uint32_t chip_id;
112d722e3fbSopenharmony_ci
113d722e3fbSopenharmony_ciint use_uc_mtype = 0;
114d722e3fbSopenharmony_ci
115d722e3fbSopenharmony_cistatic void amdgpu_deadlock_helper(unsigned ip_type);
116d722e3fbSopenharmony_cistatic void amdgpu_deadlock_gfx(void);
117d722e3fbSopenharmony_cistatic void amdgpu_deadlock_compute(void);
118d722e3fbSopenharmony_cistatic void amdgpu_illegal_reg_access();
119d722e3fbSopenharmony_cistatic void amdgpu_illegal_mem_access();
120d722e3fbSopenharmony_cistatic void amdgpu_deadlock_sdma(void);
121d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_gfx(void);
122d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_compute(void);
123d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_slow_gfx(void);
124d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_slow_compute(void);
125d722e3fbSopenharmony_cistatic void amdgpu_draw_hang_gfx(void);
126d722e3fbSopenharmony_cistatic void amdgpu_draw_hang_slow_gfx(void);
127d722e3fbSopenharmony_ci
128d722e3fbSopenharmony_ciCU_BOOL suite_deadlock_tests_enable(void)
129d722e3fbSopenharmony_ci{
130d722e3fbSopenharmony_ci	CU_BOOL enable = CU_TRUE;
131d722e3fbSopenharmony_ci
132d722e3fbSopenharmony_ci	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
133d722e3fbSopenharmony_ci					     &minor_version, &device_handle))
134d722e3fbSopenharmony_ci		return CU_FALSE;
135d722e3fbSopenharmony_ci
136d722e3fbSopenharmony_ci	family_id = device_handle->info.family_id;
137d722e3fbSopenharmony_ci	chip_id = device_handle->info.chip_external_rev;
138d722e3fbSopenharmony_ci	chip_rev = device_handle->info.chip_rev;
139d722e3fbSopenharmony_ci
140d722e3fbSopenharmony_ci	/*
141d722e3fbSopenharmony_ci	 * Only enable for ASICs supporting GPU reset and for which it's enabled
142d722e3fbSopenharmony_ci	 * by default (currently GFX8+ dGPUS and gfx9+ APUs).  Note that Raven1
143d722e3fbSopenharmony_ci	 * did not support GPU reset, but newer variants do.
144d722e3fbSopenharmony_ci	 */
145d722e3fbSopenharmony_ci	if (family_id == AMDGPU_FAMILY_SI ||
146d722e3fbSopenharmony_ci	    family_id == AMDGPU_FAMILY_KV ||
147d722e3fbSopenharmony_ci	    family_id == AMDGPU_FAMILY_CZ ||
148d722e3fbSopenharmony_ci	    family_id == AMDGPU_FAMILY_RV) {
149d722e3fbSopenharmony_ci		printf("\n\nGPU reset is not enabled for the ASIC, deadlock suite disabled\n");
150d722e3fbSopenharmony_ci		enable = CU_FALSE;
151d722e3fbSopenharmony_ci	}
152d722e3fbSopenharmony_ci
153d722e3fbSopenharmony_ci	if (asic_is_gfx_pipe_removed(family_id, chip_id, chip_rev)) {
154d722e3fbSopenharmony_ci		if (amdgpu_set_test_active("Deadlock Tests",
155d722e3fbSopenharmony_ci					"gfx ring block test (set amdgpu.lockup_timeout=50)",
156d722e3fbSopenharmony_ci					CU_FALSE))
157d722e3fbSopenharmony_ci			fprintf(stderr, "test deactivation failed - %s\n",
158d722e3fbSopenharmony_ci				CU_get_error_msg());
159d722e3fbSopenharmony_ci	}
160d722e3fbSopenharmony_ci
161d722e3fbSopenharmony_ci	if (device_handle->info.family_id >= AMDGPU_FAMILY_AI)
162d722e3fbSopenharmony_ci		use_uc_mtype = 1;
163d722e3fbSopenharmony_ci
164d722e3fbSopenharmony_ci	if (amdgpu_device_deinitialize(device_handle))
165d722e3fbSopenharmony_ci		return CU_FALSE;
166d722e3fbSopenharmony_ci
167d722e3fbSopenharmony_ci	return enable;
168d722e3fbSopenharmony_ci}
169d722e3fbSopenharmony_ci
170d722e3fbSopenharmony_ciint suite_deadlock_tests_init(void)
171d722e3fbSopenharmony_ci{
172d722e3fbSopenharmony_ci	int r;
173d722e3fbSopenharmony_ci
174d722e3fbSopenharmony_ci	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
175d722e3fbSopenharmony_ci				   &minor_version, &device_handle);
176d722e3fbSopenharmony_ci
177d722e3fbSopenharmony_ci	if (r) {
178d722e3fbSopenharmony_ci		if ((r == -EACCES) && (errno == EACCES))
179d722e3fbSopenharmony_ci			printf("\n\nError:%s. "
180d722e3fbSopenharmony_ci				"Hint:Try to run this test program as root.",
181d722e3fbSopenharmony_ci				strerror(errno));
182d722e3fbSopenharmony_ci		return CUE_SINIT_FAILED;
183d722e3fbSopenharmony_ci	}
184d722e3fbSopenharmony_ci
185d722e3fbSopenharmony_ci	return CUE_SUCCESS;
186d722e3fbSopenharmony_ci}
187d722e3fbSopenharmony_ci
188d722e3fbSopenharmony_ciint suite_deadlock_tests_clean(void)
189d722e3fbSopenharmony_ci{
190d722e3fbSopenharmony_ci	int r = amdgpu_device_deinitialize(device_handle);
191d722e3fbSopenharmony_ci
192d722e3fbSopenharmony_ci	if (r == 0)
193d722e3fbSopenharmony_ci		return CUE_SUCCESS;
194d722e3fbSopenharmony_ci	else
195d722e3fbSopenharmony_ci		return CUE_SCLEAN_FAILED;
196d722e3fbSopenharmony_ci}
197d722e3fbSopenharmony_ci
198d722e3fbSopenharmony_ci
199d722e3fbSopenharmony_ciCU_TestInfo deadlock_tests[] = {
200d722e3fbSopenharmony_ci	{ "gfx ring block test (set amdgpu.lockup_timeout=50)", amdgpu_deadlock_gfx },
201d722e3fbSopenharmony_ci	{ "compute ring block test (set amdgpu.lockup_timeout=50)", amdgpu_deadlock_compute },
202d722e3fbSopenharmony_ci	{ "sdma ring block test (set amdgpu.lockup_timeout=50)", amdgpu_deadlock_sdma },
203d722e3fbSopenharmony_ci	{ "illegal reg access test", amdgpu_illegal_reg_access },
204d722e3fbSopenharmony_ci	{ "illegal mem access test (set amdgpu.vm_fault_stop=2)", amdgpu_illegal_mem_access },
205d722e3fbSopenharmony_ci	{ "gfx ring bad dispatch test (set amdgpu.lockup_timeout=50)", amdgpu_dispatch_hang_gfx },
206d722e3fbSopenharmony_ci	{ "compute ring bad dispatch test (set amdgpu.lockup_timeout=50,50)", amdgpu_dispatch_hang_compute },
207d722e3fbSopenharmony_ci	{ "gfx ring bad slow dispatch test (set amdgpu.lockup_timeout=50)", amdgpu_dispatch_hang_slow_gfx },
208d722e3fbSopenharmony_ci	{ "compute ring bad slow dispatch test (set amdgpu.lockup_timeout=50,50)", amdgpu_dispatch_hang_slow_compute },
209d722e3fbSopenharmony_ci	{ "gfx ring bad draw test (set amdgpu.lockup_timeout=50)", amdgpu_draw_hang_gfx },
210d722e3fbSopenharmony_ci	{ "gfx ring slow bad draw test (set amdgpu.lockup_timeout=50)", amdgpu_draw_hang_slow_gfx },
211d722e3fbSopenharmony_ci	CU_TEST_INFO_NULL,
212d722e3fbSopenharmony_ci};
213d722e3fbSopenharmony_ci
214d722e3fbSopenharmony_cistatic void *write_mem_address(void *data)
215d722e3fbSopenharmony_ci{
216d722e3fbSopenharmony_ci	int i;
217d722e3fbSopenharmony_ci
218d722e3fbSopenharmony_ci	/* useconds_t range is [0, 1,000,000] so use loop for waits > 1s */
219d722e3fbSopenharmony_ci	for (i = 0; i < WRITE_MEM_ADDRESS_DELAY_MS; i++)
220d722e3fbSopenharmony_ci		usleep(1000);
221d722e3fbSopenharmony_ci
222d722e3fbSopenharmony_ci	ptr[256] = 0x1;
223d722e3fbSopenharmony_ci
224d722e3fbSopenharmony_ci	return 0;
225d722e3fbSopenharmony_ci}
226d722e3fbSopenharmony_ci
227d722e3fbSopenharmony_cistatic void amdgpu_deadlock_gfx(void)
228d722e3fbSopenharmony_ci{
229d722e3fbSopenharmony_ci	amdgpu_deadlock_helper(AMDGPU_HW_IP_GFX);
230d722e3fbSopenharmony_ci}
231d722e3fbSopenharmony_ci
232d722e3fbSopenharmony_cistatic void amdgpu_deadlock_compute(void)
233d722e3fbSopenharmony_ci{
234d722e3fbSopenharmony_ci	amdgpu_deadlock_helper(AMDGPU_HW_IP_COMPUTE);
235d722e3fbSopenharmony_ci}
236d722e3fbSopenharmony_ci
237d722e3fbSopenharmony_cistatic void amdgpu_deadlock_helper(unsigned ip_type)
238d722e3fbSopenharmony_ci{
239d722e3fbSopenharmony_ci	amdgpu_context_handle context_handle;
240d722e3fbSopenharmony_ci	amdgpu_bo_handle ib_result_handle;
241d722e3fbSopenharmony_ci	void *ib_result_cpu;
242d722e3fbSopenharmony_ci	uint64_t ib_result_mc_address;
243d722e3fbSopenharmony_ci	struct amdgpu_cs_request ibs_request;
244d722e3fbSopenharmony_ci	struct amdgpu_cs_ib_info ib_info;
245d722e3fbSopenharmony_ci	struct amdgpu_cs_fence fence_status;
246d722e3fbSopenharmony_ci	uint32_t expired;
247d722e3fbSopenharmony_ci	int i, r;
248d722e3fbSopenharmony_ci	amdgpu_bo_list_handle bo_list;
249d722e3fbSopenharmony_ci	amdgpu_va_handle va_handle;
250d722e3fbSopenharmony_ci
251d722e3fbSopenharmony_ci	r = pthread_create(&stress_thread, NULL, write_mem_address, NULL);
252d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
253d722e3fbSopenharmony_ci
254d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
255d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
256d722e3fbSopenharmony_ci
257d722e3fbSopenharmony_ci	r = amdgpu_bo_alloc_and_map_raw(device_handle, 4096, 4096,
258d722e3fbSopenharmony_ci			AMDGPU_GEM_DOMAIN_GTT, 0, use_uc_mtype ? AMDGPU_VM_MTYPE_UC : 0,
259d722e3fbSopenharmony_ci						    &ib_result_handle, &ib_result_cpu,
260d722e3fbSopenharmony_ci						    &ib_result_mc_address, &va_handle);
261d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
262d722e3fbSopenharmony_ci
263d722e3fbSopenharmony_ci	r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
264d722e3fbSopenharmony_ci			       &bo_list);
265d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
266d722e3fbSopenharmony_ci
267d722e3fbSopenharmony_ci	ptr = ib_result_cpu;
268d722e3fbSopenharmony_ci
269d722e3fbSopenharmony_ci	ptr[0] = PACKET3(PACKET3_WAIT_REG_MEM, 5);
270d722e3fbSopenharmony_ci	ptr[1] = (WAIT_REG_MEM_MEM_SPACE(1) | /* memory */
271d722e3fbSopenharmony_ci			 WAIT_REG_MEM_FUNCTION(4) | /* != */
272d722e3fbSopenharmony_ci			 WAIT_REG_MEM_ENGINE(0));  /* me */
273d722e3fbSopenharmony_ci	ptr[2] = (ib_result_mc_address + 256*4) & 0xfffffffc;
274d722e3fbSopenharmony_ci	ptr[3] = ((ib_result_mc_address + 256*4) >> 32) & 0xffffffff;
275d722e3fbSopenharmony_ci	ptr[4] = 0x00000000; /* reference value */
276d722e3fbSopenharmony_ci	ptr[5] = 0xffffffff; /* and mask */
277d722e3fbSopenharmony_ci	ptr[6] = 0x00000004; /* poll interval */
278d722e3fbSopenharmony_ci
279d722e3fbSopenharmony_ci	for (i = 7; i < 16; ++i)
280d722e3fbSopenharmony_ci		ptr[i] = 0xffff1000;
281d722e3fbSopenharmony_ci
282d722e3fbSopenharmony_ci
283d722e3fbSopenharmony_ci	ptr[256] = 0x0; /* the memory we wait on to change */
284d722e3fbSopenharmony_ci
285d722e3fbSopenharmony_ci
286d722e3fbSopenharmony_ci
287d722e3fbSopenharmony_ci	memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
288d722e3fbSopenharmony_ci	ib_info.ib_mc_address = ib_result_mc_address;
289d722e3fbSopenharmony_ci	ib_info.size = 16;
290d722e3fbSopenharmony_ci
291d722e3fbSopenharmony_ci	memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
292d722e3fbSopenharmony_ci	ibs_request.ip_type = ip_type;
293d722e3fbSopenharmony_ci	ibs_request.ring = 0;
294d722e3fbSopenharmony_ci	ibs_request.number_of_ibs = 1;
295d722e3fbSopenharmony_ci	ibs_request.ibs = &ib_info;
296d722e3fbSopenharmony_ci	ibs_request.resources = bo_list;
297d722e3fbSopenharmony_ci	ibs_request.fence_info.handle = NULL;
298d722e3fbSopenharmony_ci	for (i = 0; i < 200; i++) {
299d722e3fbSopenharmony_ci		r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
300d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
301d722e3fbSopenharmony_ci
302d722e3fbSopenharmony_ci	}
303d722e3fbSopenharmony_ci
304d722e3fbSopenharmony_ci	memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
305d722e3fbSopenharmony_ci	fence_status.context = context_handle;
306d722e3fbSopenharmony_ci	fence_status.ip_type = ip_type;
307d722e3fbSopenharmony_ci	fence_status.ip_instance = 0;
308d722e3fbSopenharmony_ci	fence_status.ring = 0;
309d722e3fbSopenharmony_ci	fence_status.fence = ibs_request.seq_no;
310d722e3fbSopenharmony_ci
311d722e3fbSopenharmony_ci	r = amdgpu_cs_query_fence_status(&fence_status,
312d722e3fbSopenharmony_ci			AMDGPU_TIMEOUT_INFINITE,0, &expired);
313d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
314d722e3fbSopenharmony_ci
315d722e3fbSopenharmony_ci	pthread_join(stress_thread, NULL);
316d722e3fbSopenharmony_ci
317d722e3fbSopenharmony_ci	r = amdgpu_bo_list_destroy(bo_list);
318d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
319d722e3fbSopenharmony_ci
320d722e3fbSopenharmony_ci	r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
321d722e3fbSopenharmony_ci				     ib_result_mc_address, 4096);
322d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
323d722e3fbSopenharmony_ci
324d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_free(context_handle);
325d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
326d722e3fbSopenharmony_ci}
327d722e3fbSopenharmony_ci
328d722e3fbSopenharmony_cistatic void amdgpu_deadlock_sdma(void)
329d722e3fbSopenharmony_ci{
330d722e3fbSopenharmony_ci	amdgpu_context_handle context_handle;
331d722e3fbSopenharmony_ci	amdgpu_bo_handle ib_result_handle;
332d722e3fbSopenharmony_ci	void *ib_result_cpu;
333d722e3fbSopenharmony_ci	uint64_t ib_result_mc_address;
334d722e3fbSopenharmony_ci	struct amdgpu_cs_request ibs_request;
335d722e3fbSopenharmony_ci	struct amdgpu_cs_ib_info ib_info;
336d722e3fbSopenharmony_ci	struct amdgpu_cs_fence fence_status;
337d722e3fbSopenharmony_ci	uint32_t expired;
338d722e3fbSopenharmony_ci	int i, r;
339d722e3fbSopenharmony_ci	amdgpu_bo_list_handle bo_list;
340d722e3fbSopenharmony_ci	amdgpu_va_handle va_handle;
341d722e3fbSopenharmony_ci	struct drm_amdgpu_info_hw_ip info;
342d722e3fbSopenharmony_ci	uint32_t ring_id;
343d722e3fbSopenharmony_ci
344d722e3fbSopenharmony_ci	r = amdgpu_query_hw_ip_info(device_handle, AMDGPU_HW_IP_DMA, 0, &info);
345d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
346d722e3fbSopenharmony_ci
347d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
348d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
349d722e3fbSopenharmony_ci
350d722e3fbSopenharmony_ci	for (ring_id = 0; (1 << ring_id) & info.available_rings; ring_id++) {
351d722e3fbSopenharmony_ci		r = pthread_create(&stress_thread, NULL, write_mem_address, NULL);
352d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
353d722e3fbSopenharmony_ci
354d722e3fbSopenharmony_ci		r = amdgpu_bo_alloc_and_map_raw(device_handle, 4096, 4096,
355d722e3fbSopenharmony_ci				AMDGPU_GEM_DOMAIN_GTT, 0, use_uc_mtype ? AMDGPU_VM_MTYPE_UC : 0,
356d722e3fbSopenharmony_ci							    &ib_result_handle, &ib_result_cpu,
357d722e3fbSopenharmony_ci							    &ib_result_mc_address, &va_handle);
358d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
359d722e3fbSopenharmony_ci
360d722e3fbSopenharmony_ci		r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
361d722e3fbSopenharmony_ci				       &bo_list);
362d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
363d722e3fbSopenharmony_ci
364d722e3fbSopenharmony_ci		ptr = ib_result_cpu;
365d722e3fbSopenharmony_ci		i = 0;
366d722e3fbSopenharmony_ci
367d722e3fbSopenharmony_ci		ptr[i++] = SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
368d722e3fbSopenharmony_ci				(0 << 26) | /* WAIT_REG_MEM */
369d722e3fbSopenharmony_ci				(4 << 28) | /* != */
370d722e3fbSopenharmony_ci				(1 << 31); /* memory */
371d722e3fbSopenharmony_ci		ptr[i++] = (ib_result_mc_address + 256*4) & 0xfffffffc;
372d722e3fbSopenharmony_ci		ptr[i++] = ((ib_result_mc_address + 256*4) >> 32) & 0xffffffff;
373d722e3fbSopenharmony_ci		ptr[i++] = 0x00000000; /* reference value */
374d722e3fbSopenharmony_ci		ptr[i++] = 0xffffffff; /* and mask */
375d722e3fbSopenharmony_ci		ptr[i++] =  4 | /* poll interval */
376d722e3fbSopenharmony_ci				(0xfff << 16); /* retry count */
377d722e3fbSopenharmony_ci
378d722e3fbSopenharmony_ci		for (; i < 16; i++)
379d722e3fbSopenharmony_ci			ptr[i] = 0;
380d722e3fbSopenharmony_ci
381d722e3fbSopenharmony_ci		ptr[256] = 0x0; /* the memory we wait on to change */
382d722e3fbSopenharmony_ci
383d722e3fbSopenharmony_ci		memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
384d722e3fbSopenharmony_ci		ib_info.ib_mc_address = ib_result_mc_address;
385d722e3fbSopenharmony_ci		ib_info.size = 16;
386d722e3fbSopenharmony_ci
387d722e3fbSopenharmony_ci		memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
388d722e3fbSopenharmony_ci		ibs_request.ip_type = AMDGPU_HW_IP_DMA;
389d722e3fbSopenharmony_ci		ibs_request.ring = ring_id;
390d722e3fbSopenharmony_ci		ibs_request.number_of_ibs = 1;
391d722e3fbSopenharmony_ci		ibs_request.ibs = &ib_info;
392d722e3fbSopenharmony_ci		ibs_request.resources = bo_list;
393d722e3fbSopenharmony_ci		ibs_request.fence_info.handle = NULL;
394d722e3fbSopenharmony_ci
395d722e3fbSopenharmony_ci		for (i = 0; i < 200; i++) {
396d722e3fbSopenharmony_ci			r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
397d722e3fbSopenharmony_ci			CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
398d722e3fbSopenharmony_ci
399d722e3fbSopenharmony_ci		}
400d722e3fbSopenharmony_ci
401d722e3fbSopenharmony_ci		memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
402d722e3fbSopenharmony_ci		fence_status.context = context_handle;
403d722e3fbSopenharmony_ci		fence_status.ip_type = AMDGPU_HW_IP_DMA;
404d722e3fbSopenharmony_ci		fence_status.ip_instance = 0;
405d722e3fbSopenharmony_ci		fence_status.ring = ring_id;
406d722e3fbSopenharmony_ci		fence_status.fence = ibs_request.seq_no;
407d722e3fbSopenharmony_ci
408d722e3fbSopenharmony_ci		r = amdgpu_cs_query_fence_status(&fence_status,
409d722e3fbSopenharmony_ci				AMDGPU_TIMEOUT_INFINITE,0, &expired);
410d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
411d722e3fbSopenharmony_ci
412d722e3fbSopenharmony_ci		pthread_join(stress_thread, NULL);
413d722e3fbSopenharmony_ci
414d722e3fbSopenharmony_ci		r = amdgpu_bo_list_destroy(bo_list);
415d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
416d722e3fbSopenharmony_ci
417d722e3fbSopenharmony_ci		r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
418d722e3fbSopenharmony_ci					     ib_result_mc_address, 4096);
419d722e3fbSopenharmony_ci		CU_ASSERT_EQUAL(r, 0);
420d722e3fbSopenharmony_ci	}
421d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_free(context_handle);
422d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
423d722e3fbSopenharmony_ci}
424d722e3fbSopenharmony_ci
425d722e3fbSopenharmony_cistatic void bad_access_helper(int reg_access)
426d722e3fbSopenharmony_ci{
427d722e3fbSopenharmony_ci	amdgpu_context_handle context_handle;
428d722e3fbSopenharmony_ci	amdgpu_bo_handle ib_result_handle;
429d722e3fbSopenharmony_ci	void *ib_result_cpu;
430d722e3fbSopenharmony_ci	uint64_t ib_result_mc_address;
431d722e3fbSopenharmony_ci	struct amdgpu_cs_request ibs_request;
432d722e3fbSopenharmony_ci	struct amdgpu_cs_ib_info ib_info;
433d722e3fbSopenharmony_ci	struct amdgpu_cs_fence fence_status;
434d722e3fbSopenharmony_ci	uint32_t expired;
435d722e3fbSopenharmony_ci	int i, r;
436d722e3fbSopenharmony_ci	amdgpu_bo_list_handle bo_list;
437d722e3fbSopenharmony_ci	amdgpu_va_handle va_handle;
438d722e3fbSopenharmony_ci
439d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
440d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
441d722e3fbSopenharmony_ci
442d722e3fbSopenharmony_ci	r = amdgpu_bo_alloc_and_map_raw(device_handle, 4096, 4096,
443d722e3fbSopenharmony_ci			AMDGPU_GEM_DOMAIN_GTT, 0, 0,
444d722e3fbSopenharmony_ci							&ib_result_handle, &ib_result_cpu,
445d722e3fbSopenharmony_ci							&ib_result_mc_address, &va_handle);
446d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
447d722e3fbSopenharmony_ci
448d722e3fbSopenharmony_ci	r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
449d722e3fbSopenharmony_ci				   &bo_list);
450d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
451d722e3fbSopenharmony_ci
452d722e3fbSopenharmony_ci	ptr = ib_result_cpu;
453d722e3fbSopenharmony_ci	i = 0;
454d722e3fbSopenharmony_ci
455d722e3fbSopenharmony_ci	ptr[i++] = PACKET3(PACKET3_WRITE_DATA, 3);
456d722e3fbSopenharmony_ci	ptr[i++] = (reg_access ? WRITE_DATA_DST_SEL(0) : WRITE_DATA_DST_SEL(5))| WR_CONFIRM;
457d722e3fbSopenharmony_ci	ptr[i++] = reg_access ? mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR : 0xdeadbee0;
458d722e3fbSopenharmony_ci	ptr[i++] = 0;
459d722e3fbSopenharmony_ci	ptr[i++] = 0xdeadbeef;
460d722e3fbSopenharmony_ci
461d722e3fbSopenharmony_ci	for (; i < 16; ++i)
462d722e3fbSopenharmony_ci		ptr[i] = 0xffff1000;
463d722e3fbSopenharmony_ci
464d722e3fbSopenharmony_ci	memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
465d722e3fbSopenharmony_ci	ib_info.ib_mc_address = ib_result_mc_address;
466d722e3fbSopenharmony_ci	ib_info.size = 16;
467d722e3fbSopenharmony_ci
468d722e3fbSopenharmony_ci	memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
469d722e3fbSopenharmony_ci	ibs_request.ip_type = AMDGPU_HW_IP_GFX;
470d722e3fbSopenharmony_ci	ibs_request.ring = 0;
471d722e3fbSopenharmony_ci	ibs_request.number_of_ibs = 1;
472d722e3fbSopenharmony_ci	ibs_request.ibs = &ib_info;
473d722e3fbSopenharmony_ci	ibs_request.resources = bo_list;
474d722e3fbSopenharmony_ci	ibs_request.fence_info.handle = NULL;
475d722e3fbSopenharmony_ci
476d722e3fbSopenharmony_ci	r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
477d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
478d722e3fbSopenharmony_ci
479d722e3fbSopenharmony_ci
480d722e3fbSopenharmony_ci	memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
481d722e3fbSopenharmony_ci	fence_status.context = context_handle;
482d722e3fbSopenharmony_ci	fence_status.ip_type = AMDGPU_HW_IP_GFX;
483d722e3fbSopenharmony_ci	fence_status.ip_instance = 0;
484d722e3fbSopenharmony_ci	fence_status.ring = 0;
485d722e3fbSopenharmony_ci	fence_status.fence = ibs_request.seq_no;
486d722e3fbSopenharmony_ci
487d722e3fbSopenharmony_ci	r = amdgpu_cs_query_fence_status(&fence_status,
488d722e3fbSopenharmony_ci			AMDGPU_TIMEOUT_INFINITE,0, &expired);
489d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
490d722e3fbSopenharmony_ci
491d722e3fbSopenharmony_ci	r = amdgpu_bo_list_destroy(bo_list);
492d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
493d722e3fbSopenharmony_ci
494d722e3fbSopenharmony_ci	r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
495d722e3fbSopenharmony_ci					 ib_result_mc_address, 4096);
496d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
497d722e3fbSopenharmony_ci
498d722e3fbSopenharmony_ci	r = amdgpu_cs_ctx_free(context_handle);
499d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
500d722e3fbSopenharmony_ci}
501d722e3fbSopenharmony_ci
502d722e3fbSopenharmony_cistatic void amdgpu_illegal_reg_access()
503d722e3fbSopenharmony_ci{
504d722e3fbSopenharmony_ci	bad_access_helper(1);
505d722e3fbSopenharmony_ci}
506d722e3fbSopenharmony_ci
507d722e3fbSopenharmony_cistatic void amdgpu_illegal_mem_access()
508d722e3fbSopenharmony_ci{
509d722e3fbSopenharmony_ci	bad_access_helper(0);
510d722e3fbSopenharmony_ci}
511d722e3fbSopenharmony_ci
512d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_gfx(void)
513d722e3fbSopenharmony_ci{
514d722e3fbSopenharmony_ci	amdgpu_dispatch_hang_helper(device_handle, AMDGPU_HW_IP_GFX);
515d722e3fbSopenharmony_ci}
516d722e3fbSopenharmony_ci
517d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_compute(void)
518d722e3fbSopenharmony_ci{
519d722e3fbSopenharmony_ci	amdgpu_dispatch_hang_helper(device_handle, AMDGPU_HW_IP_COMPUTE);
520d722e3fbSopenharmony_ci}
521d722e3fbSopenharmony_ci
522d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_slow_gfx(void)
523d722e3fbSopenharmony_ci{
524d722e3fbSopenharmony_ci	amdgpu_dispatch_hang_slow_helper(device_handle, AMDGPU_HW_IP_GFX);
525d722e3fbSopenharmony_ci}
526d722e3fbSopenharmony_ci
527d722e3fbSopenharmony_cistatic void amdgpu_dispatch_hang_slow_compute(void)
528d722e3fbSopenharmony_ci{
529d722e3fbSopenharmony_ci	amdgpu_dispatch_hang_slow_helper(device_handle, AMDGPU_HW_IP_COMPUTE);
530d722e3fbSopenharmony_ci}
531d722e3fbSopenharmony_ci
532d722e3fbSopenharmony_cistatic void amdgpu_draw_hang_gfx(void)
533d722e3fbSopenharmony_ci{
534d722e3fbSopenharmony_ci	int r;
535d722e3fbSopenharmony_ci	struct drm_amdgpu_info_hw_ip info;
536d722e3fbSopenharmony_ci	uint32_t ring_id, version;
537d722e3fbSopenharmony_ci
538d722e3fbSopenharmony_ci	r = amdgpu_query_hw_ip_info(device_handle, AMDGPU_HW_IP_GFX, 0, &info);
539d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
540d722e3fbSopenharmony_ci	if (!info.available_rings)
541d722e3fbSopenharmony_ci		printf("SKIP ... as there's no graphic ring\n");
542d722e3fbSopenharmony_ci
543d722e3fbSopenharmony_ci	version = info.hw_ip_version_major;
544d722e3fbSopenharmony_ci	if (version != 9 && version != 10) {
545d722e3fbSopenharmony_ci		printf("SKIP ... unsupported gfx version %d\n", version);
546d722e3fbSopenharmony_ci		return;
547d722e3fbSopenharmony_ci	}
548d722e3fbSopenharmony_ci
549d722e3fbSopenharmony_ci	for (ring_id = 0; (1 << ring_id) & info.available_rings; ring_id++) {
550d722e3fbSopenharmony_ci		amdgpu_memcpy_draw_test(device_handle, ring_id, version, 0);
551d722e3fbSopenharmony_ci		amdgpu_memcpy_draw_test(device_handle, ring_id, version, 1);
552d722e3fbSopenharmony_ci		amdgpu_memcpy_draw_test(device_handle, ring_id, version, 0);
553d722e3fbSopenharmony_ci	}
554d722e3fbSopenharmony_ci}
555d722e3fbSopenharmony_ci
556d722e3fbSopenharmony_cistatic void amdgpu_draw_hang_slow_gfx(void)
557d722e3fbSopenharmony_ci{
558d722e3fbSopenharmony_ci	struct drm_amdgpu_info_hw_ip info;
559d722e3fbSopenharmony_ci	uint32_t ring_id, version;
560d722e3fbSopenharmony_ci	int r;
561d722e3fbSopenharmony_ci
562d722e3fbSopenharmony_ci	r = amdgpu_query_hw_ip_info(device_handle, AMDGPU_HW_IP_GFX, 0, &info);
563d722e3fbSopenharmony_ci	CU_ASSERT_EQUAL(r, 0);
564d722e3fbSopenharmony_ci
565d722e3fbSopenharmony_ci	version = info.hw_ip_version_major;
566d722e3fbSopenharmony_ci	if (version != 9 && version != 10) {
567d722e3fbSopenharmony_ci		printf("SKIP ... unsupported gfx version %d\n", version);
568d722e3fbSopenharmony_ci		return;
569d722e3fbSopenharmony_ci	}
570d722e3fbSopenharmony_ci
571d722e3fbSopenharmony_ci	for (ring_id = 0; (1 << ring_id) & info.available_rings; ring_id++) {
572d722e3fbSopenharmony_ci		amdgpu_memcpy_draw_test(device_handle, ring_id, version, 0);
573d722e3fbSopenharmony_ci		amdgpu_memcpy_draw_hang_slow_test(device_handle, ring_id, version);
574d722e3fbSopenharmony_ci		amdgpu_memcpy_draw_test(device_handle, ring_id, version, 0);
575d722e3fbSopenharmony_ci	}
576d722e3fbSopenharmony_ci}
577