2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
31 #include "CUnit/Basic.h"
33 #include "amdgpu_test.h"
34 #include "amdgpu_drm.h"
35 #include "amdgpu_internal.h"
41 * This defines the delay in MS after which memory location designated for
42 * compression against reference value is written to, unblocking command
45 #define WRITE_MEM_ADDRESS_DELAY_MS 100
47 #define PACKET_TYPE3 3
49 #define PACKET3(op, n) ((PACKET_TYPE3 << 30) | \
50 (((op) & 0xFF) << 8) | \
53 #define PACKET3_WAIT_REG_MEM 0x3C
54 #define WAIT_REG_MEM_FUNCTION(x) ((x) << 0)
63 #define WAIT_REG_MEM_MEM_SPACE(x) ((x) << 4)
67 #define WAIT_REG_MEM_OPERATION(x) ((x) << 6)
71 #define WAIT_REG_MEM_ENGINE(x) ((x) << 8)
76 static amdgpu_device_handle device_handle;
77 static uint32_t major_version;
78 static uint32_t minor_version;
80 static pthread_t stress_thread;
83 static void amdgpu_deadlock_helper(unsigned ip_type);
84 static void amdgpu_deadlock_gfx(void);
85 static void amdgpu_deadlock_compute(void);
87 CU_BOOL suite_deadlock_tests_enable(void)
89 CU_BOOL enable = CU_TRUE;
91 if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
92 &minor_version, &device_handle))
95 if (device_handle->info.family_id == AMDGPU_FAMILY_AI ||
96 device_handle->info.family_id == AMDGPU_FAMILY_SI ||
97 device_handle->info.family_id == AMDGPU_FAMILY_RV) {
98 printf("\n\nCurrently hangs the CP on this ASIC, deadlock suite disabled\n");
102 if (amdgpu_device_deinitialize(device_handle))
108 int suite_deadlock_tests_init(void)
112 r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
113 &minor_version, &device_handle);
116 if ((r == -EACCES) && (errno == EACCES))
117 printf("\n\nError:%s. "
118 "Hint:Try to run this test program as root.",
120 return CUE_SINIT_FAILED;
126 int suite_deadlock_tests_clean(void)
128 int r = amdgpu_device_deinitialize(device_handle);
133 return CUE_SCLEAN_FAILED;
137 CU_TestInfo deadlock_tests[] = {
138 { "gfx ring block test", amdgpu_deadlock_gfx },
139 { "compute ring block test", amdgpu_deadlock_compute },
143 static void *write_mem_address(void *data)
147 /* useconds_t range is [0, 1,000,000] so use loop for waits > 1s */
148 for (i = 0; i < WRITE_MEM_ADDRESS_DELAY_MS; i++)
156 static void amdgpu_deadlock_gfx(void)
158 amdgpu_deadlock_helper(AMDGPU_HW_IP_GFX);
161 static void amdgpu_deadlock_compute(void)
163 amdgpu_deadlock_helper(AMDGPU_HW_IP_COMPUTE);
166 static void amdgpu_deadlock_helper(unsigned ip_type)
168 amdgpu_context_handle context_handle;
169 amdgpu_bo_handle ib_result_handle;
171 uint64_t ib_result_mc_address;
172 struct amdgpu_cs_request ibs_request;
173 struct amdgpu_cs_ib_info ib_info;
174 struct amdgpu_cs_fence fence_status;
177 amdgpu_bo_list_handle bo_list;
178 amdgpu_va_handle va_handle;
180 r = pthread_create(&stress_thread, NULL, write_mem_address, NULL);
181 CU_ASSERT_EQUAL(r, 0);
183 r = amdgpu_cs_ctx_create(device_handle, &context_handle);
184 CU_ASSERT_EQUAL(r, 0);
186 r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
187 AMDGPU_GEM_DOMAIN_GTT, 0,
188 &ib_result_handle, &ib_result_cpu,
189 &ib_result_mc_address, &va_handle);
190 CU_ASSERT_EQUAL(r, 0);
192 r = amdgpu_get_bo_list(device_handle, ib_result_handle, NULL,
194 CU_ASSERT_EQUAL(r, 0);
198 ptr[0] = PACKET3(PACKET3_WAIT_REG_MEM, 5);
199 ptr[1] = (WAIT_REG_MEM_MEM_SPACE(1) | /* memory */
200 WAIT_REG_MEM_FUNCTION(4) | /* != */
201 WAIT_REG_MEM_ENGINE(0)); /* me */
202 ptr[2] = (ib_result_mc_address + 256*4) & 0xfffffffc;
203 ptr[3] = ((ib_result_mc_address + 256*4) >> 32) & 0xffffffff;
204 ptr[4] = 0x00000000; /* reference value */
205 ptr[5] = 0xffffffff; /* and mask */
206 ptr[6] = 0x00000004; /* poll interval */
208 for (i = 7; i < 16; ++i)
212 ptr[256] = 0x0; /* the memory we wait on to change */
216 memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
217 ib_info.ib_mc_address = ib_result_mc_address;
220 memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
221 ibs_request.ip_type = ip_type;
222 ibs_request.ring = 0;
223 ibs_request.number_of_ibs = 1;
224 ibs_request.ibs = &ib_info;
225 ibs_request.resources = bo_list;
226 ibs_request.fence_info.handle = NULL;
228 for (i = 0; i < 200; i++) {
229 r = amdgpu_cs_submit(context_handle, 0,&ibs_request, 1);
230 CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
234 memset(&fence_status, 0, sizeof(struct amdgpu_cs_fence));
235 fence_status.context = context_handle;
236 fence_status.ip_type = ip_type;
237 fence_status.ip_instance = 0;
238 fence_status.ring = 0;
239 fence_status.fence = ibs_request.seq_no;
241 r = amdgpu_cs_query_fence_status(&fence_status,
242 AMDGPU_TIMEOUT_INFINITE,0, &expired);
243 CU_ASSERT_EQUAL((r == 0 || r == -ECANCELED), 1);
245 pthread_join(stress_thread, NULL);
247 r = amdgpu_bo_list_destroy(bo_list);
248 CU_ASSERT_EQUAL(r, 0);
250 r = amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
251 ib_result_mc_address, 4096);
252 CU_ASSERT_EQUAL(r, 0);
254 r = amdgpu_cs_ctx_free(context_handle);
255 CU_ASSERT_EQUAL(r, 0);