2 * Copyright 2021 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.
26 #include <sys/types.h>
33 #include "CUnit/Basic.h"
35 #include "amdgpu_test.h"
36 #include "amdgpu_drm.h"
37 #include "amdgpu_internal.h"
41 #define GFX_COMPUTE_NOP 0xffff1000
43 static amdgpu_device_handle device_handle;
44 static uint32_t major_version;
45 static uint32_t minor_version;
46 static char *sysfs_remove = NULL;
49 CU_BOOL suite_hotunplug_tests_enable(void)
51 CU_BOOL enable = CU_TRUE;
54 if (drmGetDevice2(drm_amdgpu[0], DRM_DEVICE_GET_PCI_REVISION, &device)) {
55 printf("\n\nGPU Failed to get DRM device PCI info!\n");
59 if (device->bustype != DRM_BUS_PCI) {
60 printf("\n\nGPU device is not on PCI bus!\n");
61 amdgpu_device_deinitialize(device_handle);
65 if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
66 &minor_version, &device_handle))
69 /* Latest tested amdgpu version to work with all the tests */
70 if (minor_version < 46)
73 /* skip hotplug test on APUs */
74 if(device_handle->dev_info.ids_flags & AMDGPU_IDS_FLAGS_FUSION)
77 if (amdgpu_device_deinitialize(device_handle))
83 int suite_hotunplug_tests_init(void)
85 /* We need to open/close device at each test manually */
86 amdgpu_close_devices();
91 int suite_hotunplug_tests_clean(void)
98 static int amdgpu_hotunplug_trigger(const char *pathname)
102 fd = open(pathname, O_WRONLY);
106 len = write(fd, "1", 1);
112 static int amdgpu_hotunplug_setup_test()
117 if (amdgpu_open_device_on_test_index(open_render_node) < 0) {
118 printf("\n\n Failed to reopen device file!\n");
119 return CUE_SINIT_FAILED;
125 r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
126 &minor_version, &device_handle);
129 if ((r == -EACCES) && (errno == EACCES))
130 printf("\n\nError:%s. "
131 "Hint:Try to run this test program as root.",
133 return CUE_SINIT_FAILED;
136 tmp_str = amdgpu_get_device_from_fd(drm_amdgpu[0]);
138 printf("\n\n Device path not found!\n");
139 return CUE_SINIT_FAILED;
142 sysfs_remove = realloc(tmp_str, strlen(tmp_str) * 2);
143 strcat(sysfs_remove, "/remove");
148 static int amdgpu_hotunplug_teardown_test()
150 if (amdgpu_device_deinitialize(device_handle))
151 return CUE_SCLEAN_FAILED;
153 amdgpu_close_devices();
161 static inline int amdgpu_hotunplug_remove()
163 return amdgpu_hotunplug_trigger(sysfs_remove);
166 static inline int amdgpu_hotunplug_rescan()
168 return amdgpu_hotunplug_trigger("/sys/bus/pci/rescan");
171 static int amdgpu_cs_sync(amdgpu_context_handle context,
172 unsigned int ip_type,
176 struct amdgpu_cs_fence fence = {
184 return amdgpu_cs_query_fence_status(&fence,
185 AMDGPU_TIMEOUT_INFINITE,
189 static void *amdgpu_nop_cs()
191 amdgpu_bo_handle ib_result_handle;
193 uint64_t ib_result_mc_address;
196 amdgpu_bo_list_handle bo_list;
197 amdgpu_va_handle va_handle;
198 amdgpu_context_handle context;
199 struct amdgpu_cs_request ibs_request;
200 struct amdgpu_cs_ib_info ib_info;
202 r = amdgpu_cs_ctx_create(device_handle, &context);
203 CU_ASSERT_EQUAL(r, 0);
205 r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
206 AMDGPU_GEM_DOMAIN_GTT, 0,
207 &ib_result_handle, &ib_result_cpu,
208 &ib_result_mc_address, &va_handle);
209 CU_ASSERT_EQUAL(r, 0);
212 for (i = 0; i < 16; ++i)
213 ptr[i] = GFX_COMPUTE_NOP;
215 r = amdgpu_bo_list_create(device_handle, 1, &ib_result_handle, NULL, &bo_list);
216 CU_ASSERT_EQUAL(r, 0);
218 memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
219 ib_info.ib_mc_address = ib_result_mc_address;
222 memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
223 ibs_request.ip_type = AMDGPU_HW_IP_GFX;
224 ibs_request.ring = 0;
225 ibs_request.number_of_ibs = 1;
226 ibs_request.ibs = &ib_info;
227 ibs_request.resources = bo_list;
230 amdgpu_cs_submit(context, 0, &ibs_request, 1);
232 amdgpu_cs_sync(context, AMDGPU_HW_IP_GFX, 0, ibs_request.seq_no);
233 amdgpu_bo_list_destroy(bo_list);
234 amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
235 ib_result_mc_address, 4096);
237 amdgpu_cs_ctx_free(context);
242 static pthread_t* amdgpu_create_cs_thread()
245 pthread_t *thread = malloc(sizeof(*thread));
251 r = pthread_create(thread, NULL, amdgpu_nop_cs, NULL);
252 CU_ASSERT_EQUAL(r, 0);
254 /* Give thread enough time to start*/
259 static void amdgpu_destroy_cs_thread(pthread_t *thread)
265 pthread_join(*thread, &status);
266 CU_ASSERT_EQUAL(status, 0);
272 static void amdgpu_hotunplug_test(bool with_cs)
275 pthread_t *thread = NULL;
277 r = amdgpu_hotunplug_setup_test();
278 CU_ASSERT_EQUAL(r , 0);
281 thread = amdgpu_create_cs_thread();
282 CU_ASSERT_NOT_EQUAL(thread, NULL);
285 r = amdgpu_hotunplug_remove();
286 CU_ASSERT_EQUAL(r > 0, 1);
289 amdgpu_destroy_cs_thread(thread);
291 r = amdgpu_hotunplug_teardown_test();
292 CU_ASSERT_EQUAL(r , 0);
294 r = amdgpu_hotunplug_rescan();
295 CU_ASSERT_EQUAL(r > 0, 1);
298 static void amdgpu_hotunplug_simple(void)
300 amdgpu_hotunplug_test(false);
303 static void amdgpu_hotunplug_with_cs(void)
305 amdgpu_hotunplug_test(true);
308 static void amdgpu_hotunplug_with_exported_bo(void)
313 amdgpu_bo_handle bo_handle;
315 struct amdgpu_bo_alloc_request request = {
317 .phys_alignment = 4096,
318 .preferred_heap = AMDGPU_GEM_DOMAIN_GTT,
322 r = amdgpu_hotunplug_setup_test();
323 CU_ASSERT_EQUAL(r , 0);
325 amdgpu_bo_alloc(device_handle, &request, &bo_handle);
326 CU_ASSERT_EQUAL(r, 0);
328 r = amdgpu_bo_export(bo_handle, amdgpu_bo_handle_type_dma_buf_fd, &dma_buf_fd);
329 CU_ASSERT_EQUAL(r, 0);
331 ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf_fd, 0);
332 CU_ASSERT_NOT_EQUAL(ptr, MAP_FAILED);
334 r = amdgpu_hotunplug_remove();
335 CU_ASSERT_EQUAL(r > 0, 1);
337 amdgpu_bo_free(bo_handle);
339 r = amdgpu_hotunplug_teardown_test();
340 CU_ASSERT_EQUAL(r , 0);
347 r = amdgpu_hotunplug_rescan();
348 CU_ASSERT_EQUAL(r > 0, 1);
351 static void amdgpu_hotunplug_with_exported_fence(void)
353 amdgpu_bo_handle ib_result_handle;
355 uint64_t ib_result_mc_address;
356 uint32_t *ptr, sync_obj_handle, sync_obj_handle2;
358 amdgpu_bo_list_handle bo_list;
359 amdgpu_va_handle va_handle;
360 uint32_t major2, minor2;
361 amdgpu_device_handle device2;
362 amdgpu_context_handle context;
363 struct amdgpu_cs_request ibs_request;
364 struct amdgpu_cs_ib_info ib_info;
365 struct amdgpu_cs_fence fence_status = {0};
368 r = amdgpu_hotunplug_setup_test();
369 CU_ASSERT_EQUAL(r , 0);
371 r = amdgpu_device_initialize(drm_amdgpu[1], &major2, &minor2, &device2);
372 CU_ASSERT_EQUAL(r, 0);
374 r = amdgpu_cs_ctx_create(device_handle, &context);
375 CU_ASSERT_EQUAL(r, 0);
377 r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096,
378 AMDGPU_GEM_DOMAIN_GTT, 0,
379 &ib_result_handle, &ib_result_cpu,
380 &ib_result_mc_address, &va_handle);
381 CU_ASSERT_EQUAL(r, 0);
384 for (i = 0; i < 16; ++i)
385 ptr[i] = GFX_COMPUTE_NOP;
387 r = amdgpu_bo_list_create(device_handle, 1, &ib_result_handle, NULL, &bo_list);
388 CU_ASSERT_EQUAL(r, 0);
390 memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info));
391 ib_info.ib_mc_address = ib_result_mc_address;
394 memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request));
395 ibs_request.ip_type = AMDGPU_HW_IP_GFX;
396 ibs_request.ring = 0;
397 ibs_request.number_of_ibs = 1;
398 ibs_request.ibs = &ib_info;
399 ibs_request.resources = bo_list;
401 CU_ASSERT_EQUAL(amdgpu_cs_submit(context, 0, &ibs_request, 1), 0);
403 fence_status.context = context;
404 fence_status.ip_type = AMDGPU_HW_IP_GFX;
405 fence_status.ip_instance = 0;
406 fence_status.fence = ibs_request.seq_no;
408 CU_ASSERT_EQUAL(amdgpu_cs_fence_to_handle(device_handle, &fence_status,
409 AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ,
413 CU_ASSERT_EQUAL(amdgpu_cs_export_syncobj(device_handle, sync_obj_handle, &shared_fd), 0);
415 CU_ASSERT_EQUAL(amdgpu_cs_import_syncobj(device2, shared_fd, &sync_obj_handle2), 0);
417 CU_ASSERT_EQUAL(amdgpu_cs_destroy_syncobj(device_handle, sync_obj_handle), 0);
419 CU_ASSERT_EQUAL(amdgpu_bo_list_destroy(bo_list), 0);
420 CU_ASSERT_EQUAL(amdgpu_bo_unmap_and_free(ib_result_handle, va_handle,
421 ib_result_mc_address, 4096), 0);
422 CU_ASSERT_EQUAL(amdgpu_cs_ctx_free(context), 0);
424 r = amdgpu_hotunplug_remove();
425 CU_ASSERT_EQUAL(r > 0, 1);
427 CU_ASSERT_EQUAL(amdgpu_cs_syncobj_wait(device2, &sync_obj_handle2, 1, 100000000, 0, NULL), 0);
429 CU_ASSERT_EQUAL(amdgpu_cs_destroy_syncobj(device2, sync_obj_handle2), 0);
431 amdgpu_device_deinitialize(device2);
433 r = amdgpu_hotunplug_teardown_test();
434 CU_ASSERT_EQUAL(r , 0);
436 r = amdgpu_hotunplug_rescan();
437 CU_ASSERT_EQUAL(r > 0, 1);
441 CU_TestInfo hotunplug_tests[] = {
442 { "Unplug card and rescan the bus to plug it back", amdgpu_hotunplug_simple },
443 { "Same as first test but with command submission", amdgpu_hotunplug_with_cs },
444 { "Unplug with exported bo", amdgpu_hotunplug_with_exported_bo },
445 { "Unplug with exported fence", amdgpu_hotunplug_with_exported_fence },