[PATCH 5/7] test/amdgpu/hotunplug: Add basic test

Andrey Grodzovsky andrey.grodzovsky at amd.com
Tue Jun 1 20:17:00 UTC 2021


Add plug/unplug device and open/close device file
infrastrucutre.
Add basic test - unplug device while device file still
open. Close device file afterwards and replug the device.

Signed-off-by: Andrey Grodzovsky <andrey.grodzovsky at amd.com>
---
 tests/amdgpu/hotunplug_tests.c | 135 +++++++++++++++++++++++++--------
 1 file changed, 105 insertions(+), 30 deletions(-)

diff --git a/tests/amdgpu/hotunplug_tests.c b/tests/amdgpu/hotunplug_tests.c
index 9d11dae4..c2bc1cf2 100644
--- a/tests/amdgpu/hotunplug_tests.c
+++ b/tests/amdgpu/hotunplug_tests.c
@@ -21,9 +21,11 @@
  *
 */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #if HAVE_ALLOCA_H
 # include <alloca.h>
 #endif
@@ -33,40 +35,40 @@
 #include "amdgpu_test.h"
 #include "amdgpu_drm.h"
 #include "amdgpu_internal.h"
-
+#include "xf86drm.h"
 #include <pthread.h>
 
 
 static  amdgpu_device_handle device_handle;
 static  uint32_t  major_version;
 static  uint32_t  minor_version;
-
-static uint32_t family_id;
-static uint32_t chip_rev;
-static uint32_t chip_id;
+static char *sysfs_remove = NULL;
 
 CU_BOOL suite_hotunplug_tests_enable(void)
 {
 	CU_BOOL enable = CU_TRUE;
+	drmDevicePtr device;
+
+	if (drmGetDevice2(drm_amdgpu[0], DRM_DEVICE_GET_PCI_REVISION, &device)) {
+		printf("\n\nGPU Failed to get DRM device PCI info!\n");
+		return CU_FALSE;
+	}
+
+	if (device->bustype != DRM_BUS_PCI) {
+		printf("\n\nGPU device is not on PCI bus!\n");
+		amdgpu_device_deinitialize(device_handle);
+		return CU_FALSE;
+	}
+
+	/* Disable until the hot-unplug support in kernel gets into drm-next */
+	if (major_version < 0xff)
+		enable = false;
 
 	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
 					     &minor_version, &device_handle))
 		return CU_FALSE;
 
-	family_id = device_handle->info.family_id;
-	chip_id = device_handle->info.chip_external_rev;
-	chip_rev = device_handle->info.chip_rev;
-
-	/*
-	 * Only enable for ASICs supporting GPU reset and for which it's enabled
-	 * by default (currently GFX8/9 dGPUS)
-	 */
-	if (family_id != AMDGPU_FAMILY_VI &&
-	    family_id != AMDGPU_FAMILY_AI &&
-	    family_id != AMDGPU_FAMILY_CI) {
-		printf("\n\nGPU reset is not enabled for the ASIC, hotunplug suite disabled\n");
-		enable = CU_FALSE;
-	}
+	/* TODO Once DRM version for unplug feature ready compare here agains it*/
 
 	if (amdgpu_device_deinitialize(device_handle))
 		return CU_FALSE;
@@ -75,8 +77,46 @@ CU_BOOL suite_hotunplug_tests_enable(void)
 }
 
 int suite_hotunplug_tests_init(void)
+{
+	/* We need to open/close device at each test manually */
+	amdgpu_close_devices();
+
+	return CUE_SUCCESS;
+}
+
+int suite_hotunplug_tests_clean(void)
+{
+
+
+	return CUE_SUCCESS;
+}
+
+static int amdgpu_hotunplug_trigger(const char *pathname)
+{
+	int fd, len;
+
+	fd = open(pathname, O_WRONLY);
+	if (fd < 0)
+		return -errno;
+
+	len = write(fd, "1", 1);
+	close(fd);
+
+	return len;
+}
+
+static int amdgpu_hotunplug_setup_test()
 {
 	int r;
+	char *tmp_str;
+
+	if (amdgpu_open_device_on_test_index(open_render_node) <= 0) {
+		printf("\n\n Failed to reopen device file!\n");
+		return CUE_SINIT_FAILED;
+
+
+
+	}
 
 	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
 				   &minor_version, &device_handle);
@@ -89,27 +129,62 @@ int suite_hotunplug_tests_init(void)
 		return CUE_SINIT_FAILED;
 	}
 
-	return CUE_SUCCESS;
+	tmp_str = drmGetCharDeviceFromFd(drm_amdgpu[0]);
+	if (!tmp_str){
+		printf("\n\n Device path not found!\n");
+		return  CUE_SINIT_FAILED;
+	}
+
+	sysfs_remove = realloc(tmp_str, strlen(tmp_str) * 2);
+	strcat(sysfs_remove, "/device/remove");
+
+	return 0;
+
 }
 
-int suite_hotunplug_tests_clean(void)
+static int amdgpu_hotunplug_teardown_test()
 {
-	int r = amdgpu_device_deinitialize(device_handle);
-
-	if (r == 0)
-		return CUE_SUCCESS;
-	else
+	if (amdgpu_device_deinitialize(device_handle))
 		return CUE_SCLEAN_FAILED;
+
+	amdgpu_close_devices();
+
+	if (sysfs_remove)
+		free(sysfs_remove);
+
+	return 0;
+}
+
+static inline int amdgpu_hotunplug_remove()
+{
+	return amdgpu_hotunplug_trigger(sysfs_remove);
+}
+
+static inline int amdgpu_hotunplug_rescan()
+{
+	return amdgpu_hotunplug_trigger("/sys/bus/pci/rescan");
 }
 
 
-static void amdgpu_hotunplug_gfx(void)
+static void amdgpu_hotunplug_simple(void)
 {
-	printf("Hello!\n");
+	int r;
+
+	r = amdgpu_hotunplug_setup_test();
+	CU_ASSERT_EQUAL(r , 0);
+
+	r = amdgpu_hotunplug_remove();
+	CU_ASSERT_EQUAL(r > 0, 1);
+
+	r = amdgpu_hotunplug_teardown_test();
+	CU_ASSERT_EQUAL(r , 0);
+
+	r = amdgpu_hotunplug_rescan();
+	CU_ASSERT_EQUAL(r > 0, 1);
 }
 
 CU_TestInfo hotunplug_tests[] = {
-	{ "gfx ring block test (set amdgpu.lockup_timeout=50)", amdgpu_hotunplug_gfx },
+	{ "Unplug card and rescan the bus to plug it back", amdgpu_hotunplug_simple },
 	CU_TEST_INFO_NULL,
 };
 
-- 
2.25.1



More information about the dri-devel mailing list