From af658ef6b7e5b044d2566104137ee1cb34e52c59 Mon Sep 17 00:00:00 2001 From: Andrey Grodzovsky Date: Mon, 8 Feb 2021 00:41:28 -0500 Subject: DAFT: Add amdgpu command submission while unplug. --- tests/core_hotunplug.c | 218 ++++++++++++++++++++++++++++++++++++++++++++++++- tests/meson.build | 2 +- 2 files changed, 216 insertions(+), 4 deletions(-) diff --git a/tests/core_hotunplug.c b/tests/core_hotunplug.c index e7d2a44..5a4dcb4 100644 --- a/tests/core_hotunplug.c +++ b/tests/core_hotunplug.c @@ -52,6 +52,190 @@ struct hotunplug { bool need_healthcheck; }; +/* amdgpu specific code */ + +#include +#include +#include + +#define GFX_COMPUTE_NOP 0xffff1000 + + +static bool do_cs; + +static int +amdgpu_bo_alloc_and_map(amdgpu_device_handle dev, unsigned size, + unsigned alignment, unsigned heap, uint64_t flags, + amdgpu_bo_handle *bo, void **cpu, uint64_t *mc_address, + amdgpu_va_handle *va_handle) +{ + struct amdgpu_bo_alloc_request request = { + .alloc_size = size, + .phys_alignment = alignment, + .preferred_heap = heap, + .flags = flags, + }; + amdgpu_bo_handle buf_handle; + amdgpu_va_handle handle; + uint64_t vmc_addr; + int r; + + r = amdgpu_bo_alloc(dev, &request, &buf_handle); + if (r) + return r; + + r = amdgpu_va_range_alloc(dev, + amdgpu_gpu_va_range_general, + size, alignment, 0, &vmc_addr, + &handle, 0); + if (r) + goto error_va_alloc; + + r = amdgpu_bo_va_op(buf_handle, 0, size, vmc_addr, 0, AMDGPU_VA_OP_MAP); + if (r) + goto error_va_map; + + r = amdgpu_bo_cpu_map(buf_handle, cpu); + if (r) + goto error_cpu_map; + + *bo = buf_handle; + *mc_address = vmc_addr; + *va_handle = handle; + + return 0; + +error_cpu_map: + amdgpu_bo_cpu_unmap(buf_handle); + +error_va_map: + amdgpu_bo_va_op(buf_handle, 0, size, vmc_addr, 0, AMDGPU_VA_OP_UNMAP); + +error_va_alloc: + amdgpu_bo_free(buf_handle); + return r; +} + +static void +amdgpu_bo_unmap_and_free(amdgpu_bo_handle bo, amdgpu_va_handle va_handle, + uint64_t mc_addr, uint64_t size) +{ + amdgpu_bo_cpu_unmap(bo); + amdgpu_bo_va_op(bo, 0, size, mc_addr, 0, AMDGPU_VA_OP_UNMAP); + amdgpu_va_range_free(va_handle); + amdgpu_bo_free(bo); +} + +static void amdgpu_cs_sync(amdgpu_context_handle context, + unsigned int ip_type, + int ring, + unsigned int seqno) +{ + struct amdgpu_cs_fence fence = { + .context = context, + .ip_type = ip_type, + .ring = ring, + .fence = seqno, + }; + uint32_t expired; + int err; + + err = amdgpu_cs_query_fence_status(&fence, + AMDGPU_TIMEOUT_INFINITE, + 0, &expired); +} + +static void *amdgpu_nop_cs(void *p) +{ + int fd = *(int *)p; + amdgpu_bo_handle ib_result_handle; + void *ib_result_cpu; + uint64_t ib_result_mc_address; + uint32_t *ptr; + int i, r; + amdgpu_bo_list_handle bo_list; + amdgpu_va_handle va_handle; + uint32_t major, minor; + amdgpu_device_handle device; + amdgpu_context_handle context; + struct amdgpu_cs_request ibs_request; + struct amdgpu_cs_ib_info ib_info; + + + r = amdgpu_device_initialize(fd, &major, &minor, &device); + igt_require(r == 0); + + r = amdgpu_cs_ctx_create(device, &context); + igt_assert_eq(r, 0); + + r = amdgpu_bo_alloc_and_map(device, 4096, 4096, + AMDGPU_GEM_DOMAIN_GTT, 0, + &ib_result_handle, &ib_result_cpu, + &ib_result_mc_address, &va_handle); + igt_assert_eq(r, 0); + + ptr = ib_result_cpu; + for (i = 0; i < 16; ++i) + ptr[i] = GFX_COMPUTE_NOP; + + r = amdgpu_bo_list_create(device, 1, &ib_result_handle, NULL, &bo_list); + igt_assert_eq(r, 0); + + memset(&ib_info, 0, sizeof(struct amdgpu_cs_ib_info)); + ib_info.ib_mc_address = ib_result_mc_address; + ib_info.size = 16; + + memset(&ibs_request, 0, sizeof(struct amdgpu_cs_request)); + ibs_request.ip_type = AMDGPU_HW_IP_GFX; + ibs_request.ring = 0; + ibs_request.number_of_ibs = 1; + ibs_request.ibs = &ib_info; + ibs_request.resources = bo_list; + + while (do_cs) + amdgpu_cs_submit(context, 0, &ibs_request, 1); + + amdgpu_cs_sync(context, AMDGPU_HW_IP_GFX, 0, ibs_request.seq_no); + + amdgpu_bo_list_destroy(bo_list); + + amdgpu_bo_unmap_and_free(ib_result_handle, va_handle, + ib_result_mc_address, 4096); + + amdgpu_cs_ctx_free(context); + amdgpu_device_deinitialize(device); + + return (void *)0; +} + +static pthread_t* amdgpu_create_cs_thread(int *fd) +{ + int r; + pthread_t *thread = malloc(sizeof(*thread)); + + do_cs = true; + + r = pthread_create(thread, NULL, amdgpu_nop_cs, (void *)fd); + igt_assert_eq(r, 0); + + /* Give thread enough time to start*/ + usleep(100000); + return thread; +} + +static void amdgpu_destroy_cs_thread(pthread_t *thread) +{ + void *status; + + do_cs = false; + + pthread_join(*thread, &status); + igt_assert(status == 0); + + free(thread); +} + + @@ -455,15 +645,26 @@ static void unplug_rescan(struct hotunplug *priv) static void hotunbind_rebind(struct hotunplug *priv) { + pthread_t *thread = NULL; + pre_check(priv); priv->fd.drm = local_drm_open_driver(false, "", " for hot unbind"); + if (is_amdgpu_device(priv->fd.drm)) + thread = amdgpu_create_cs_thread(&priv->fd.drm); + driver_unbind(priv, "hot ", 0); + if (thread) + amdgpu_destroy_cs_thread(thread); + + priv->fd.drm = close_device(priv->fd.drm, "late ", "unbound "); igt_assert_eq(priv->fd.drm, -1); driver_bind(priv, 0); igt_assert_f(healthcheck(priv, false), "%s\n", priv->failure); @@ -471,15 +672,25 @@ static void hotunbind_rebind(struct hotunplug *priv) static void hotunplug_rescan(struct hotunplug *priv) { + pthread_t *thread = NULL; + pre_check(priv); priv->fd.drm = local_drm_open_driver(false, "", " for hot unplug"); + if (is_amdgpu_device(priv->fd.drm)) + thread = amdgpu_create_cs_thread(&priv->fd.drm); + device_unplug(priv, "hot ", 0); + if (thread) + amdgpu_destroy_cs_thread(thread); + priv->fd.drm = close_device(priv->fd.drm, "late ", "removed "); igt_assert_eq(priv->fd.drm, -1); bus_rescan(priv, 0); igt_assert_f(healthcheck(priv, false), "%s\n", priv->failure); @@ -543,6 +754,7 @@ static void hotreplug_lateclose(struct hotunplug *priv) igt_assert_f(healthcheck(priv, false), "%s\n", priv->failure); } diff --git a/tests/meson.build b/tests/meson.build index 825e018..1de6cc5 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -243,7 +243,7 @@ i915_progs = [ 'sysfs_timeslice_duration', ] -test_deps = [ igt_deps ] +test_deps = [ igt_deps + [ libdrm_amdgpu ] ] if libdrm_nouveau.found() test_progs += [ -- 2.7.4