Skip to contentSkip to conversation

drm/amdgpu/UAPI: add new PROFILE IOCTL

RFC PATCH In review Alex Deucher 6 participants 19 messages Dec 2, 2021 – Dec 14, 2021

Subsystems (3)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

RFC PATCH 1/2 drm/amdgpu/UAPI: add new PROFILE IOCTL

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

---
 drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
 include/uapi/drm/amdgpu_drm.h               |  28 +++++
 5 files changed, 173 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
 	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
 	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
 	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-	amdgpu_eeprom.o amdgpu_mca.o
+	amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
 amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
 #include "amdgpu_sched.h"
 #include "amdgpu_fdinfo.h"
 #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
 #include "amdgpu_ras.h"
 #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0

112 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
 #define DRM_AMDGPU_VM			0x13
 #define DRM_AMDGPU_FENCE_TO_HANDLE	0x14
 #define DRM_AMDGPU_SCHED		0x15
+#define DRM_AMDGPU_PROFILE		0x16
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
 #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
 #define DRM_IOCTL_AMDGPU_SCHED		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE	DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
 /**
  * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
 	struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
 };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE	1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE	2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK	0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE	0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD	1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK	2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK	3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK	4
+
+struct drm_amdgpu_profile_in {
+	/** AMDGPU_PROFILE_OP_* */
+	__u32	op;
+	__u32	flags;
+};
+
+struct drm_amdgpu_profile_out {
+	__u64	flags;
+};
+
+union drm_amdgpu_profile {
+	struct drm_amdgpu_profile_in in;
+	struct drm_amdgpu_profile_out out;
+};
+
 /*
  * Supported GPU families
  */
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

RFC PATCH 2/2 drm/amdgpu: bump driver version for PROFILE IOCTL

So mesa and tools know when this is available.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
index 0e27f9673f8f..b50cf36417cf 100644
@@ -100,9 +100,10 @@
  * - 3.42.0 - Add 16bpc fixed point display support
  * - 3.43.0 - Add device hot plug/unplug support
  * - 3.44.0 - DCN3 supports DCC independent block settings: !64B && 128B, 64B && 128B
+ * - 3.45.0 - Add PROFILE IOCTL
  */
 #define KMS_DRIVER_MAJOR	3
-#define KMS_DRIVER_MINOR	44
+#define KMS_DRIVER_MINOR	45
 #define KMS_DRIVER_PATCHLEVEL	0
 
 int amdgpu_vram_limit;
Signature · 1 line
2.31.1
#2 lore raw
In reply to Alex Deucher
Quoted 153 lines · Alex Deucher

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

---
  drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
  include/uapi/drm/amdgpu_drm.h               |  28 +++++
  5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
  	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
  	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
  	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-	amdgpu_eeprom.o amdgpu_mca.o
+	amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
  amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
  #include "amdgpu_sched.h"
  #include "amdgpu_fdinfo.h"
  #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
  #include "amdgpu_ras.h"
  #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  };
 
  static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +85 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp)
+{
+	union drm_amdgpu_profile *args = data;
+	struct amdgpu_device *adev = drm_to_adev(dev);
+	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+	enum amd_dpm_forced_level current_level, requested_level;
+	int r;
+
+	if (pp_funcs->get_performance_level)
+		current_level = amdgpu_dpm_get_performance_level(adev);
+	else
+		current_level = adev->pm.dpm.forced_level;
+
+	switch (args->in.op) {
+	case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+		if (args->in.flags)
+			return -EINVAL;
+		switch (current_level) {
+		case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+			break;
+		default:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+			break;
+		}
+		break;
+	case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+		if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+			return -EINVAL;
+		switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+			break;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c line 95

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +10 -0
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+			requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+			break;
+		default:
+			return -EINVAL;
+		}
+

Not sure if the below code covers everything, but in general it's better for the sysfs handling in amdgpu_pm (after parsing the options) and this one to call a common API to handle this.

Thanks,
Lijo

Quoted 107 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +17 -0
+		if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+			mutex_lock(&adev->pm.mutex);
+			r = amdgpu_dpm_force_performance_level(adev, requested_level);
+			if (!r)
+				adev->pm.dpm.forced_level = requested_level;
+			mutex_unlock(&adev->pm.mutex);
+			if (r)
+				return r;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
  #define DRM_AMDGPU_VM			0x13
  #define DRM_AMDGPU_FENCE_TO_HANDLE	0x14
  #define DRM_AMDGPU_SCHED		0x15
+#define DRM_AMDGPU_PROFILE		0x16
 
  #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
  #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
  #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
  #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
  #define DRM_IOCTL_AMDGPU_SCHED		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE	DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
  /**
   * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
  	struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
  };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE	1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE	2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK	0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE	0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD	1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK	2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK	3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK	4
+
+struct drm_amdgpu_profile_in {
+	/** AMDGPU_PROFILE_OP_* */
+	__u32	op;
+	__u32	flags;
+};
+
+struct drm_amdgpu_profile_out {
+	__u64	flags;
+};
+
+union drm_amdgpu_profile {
+	struct drm_amdgpu_profile_in in;
+	struct drm_amdgpu_profile_out out;
+};
+
  /*
   * Supported GPU families
   */
#3 lore raw
Quoted 163 lines · Lijo
Quoted 159 lines · Alex Deucher

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

---
  drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
  include/uapi/drm/amdgpu_drm.h               |  28 +++++
  5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
      amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
      amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
      amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
  amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
  #include "amdgpu_sched.h"
  #include "amdgpu_fdinfo.h"
  #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
  #include "amdgpu_ras.h"
  #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
      DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
      DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
      DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  };
 
  static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +91 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c line 95

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+             default:
+                     return -EINVAL;
+             }
+

Not sure if the below code covers everything, but in general it's better for the sysfs handling in amdgpu_pm (after parsing the options) and this one to call a common API to handle this.

Yeah, I considered cleaning that up first, but then Evan's patches already took care of that, so I figured that would get cleaned up once those land.

Alex

Quoted 111 lines

Thanks,
Lijo

Quoted 107 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +17 -0
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
  #define DRM_AMDGPU_VM                       0x13
  #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
  #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
  #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
  #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
  #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
  #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
  #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
  /**
   * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
      struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
  };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
  /*
   * Supported GPU families
   */
#4 lore raw
In reply to Alex Deucher

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

Christian.

Quoted 252 lines
---
  drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
  include/uapi/drm/amdgpu_drm.h               |  28 +++++
  5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
  	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
  	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
  	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-	amdgpu_eeprom.o amdgpu_mca.o
+	amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
  amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
  #include "amdgpu_sched.h"
  #include "amdgpu_fdinfo.h"
  #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
  #include "amdgpu_ras.h"
  #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  };
 
  static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp)
+{
+	union drm_amdgpu_profile *args = data;
+	struct amdgpu_device *adev = drm_to_adev(dev);
+	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+	enum amd_dpm_forced_level current_level, requested_level;
+	int r;
+
+	if (pp_funcs->get_performance_level)
+		current_level = amdgpu_dpm_get_performance_level(adev);
+	else
+		current_level = adev->pm.dpm.forced_level;
+
+	switch (args->in.op) {
+	case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+		if (args->in.flags)
+			return -EINVAL;
+		switch (current_level) {
+		case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+			break;
+		default:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+			break;
+		}
+		break;
+	case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+		if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+			return -EINVAL;
+		switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+			requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+			mutex_lock(&adev->pm.mutex);
+			r = amdgpu_dpm_force_performance_level(adev, requested_level);
+			if (!r)
+				adev->pm.dpm.forced_level = requested_level;
+			mutex_unlock(&adev->pm.mutex);
+			if (r)
+				return r;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
  #define DRM_AMDGPU_VM			0x13
  #define DRM_AMDGPU_FENCE_TO_HANDLE	0x14
  #define DRM_AMDGPU_SCHED		0x15
+#define DRM_AMDGPU_PROFILE		0x16
 
  #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
  #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
  #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
  #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
  #define DRM_IOCTL_AMDGPU_SCHED		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE	DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
  /**
   * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
  	struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
  };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE	1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE	2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK	0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE	0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD	1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK	2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK	3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK	4
+
+struct drm_amdgpu_profile_in {
+	/** AMDGPU_PROFILE_OP_* */
+	__u32	op;
+	__u32	flags;
+};
+
+struct drm_amdgpu_profile_out {
+	__u64	flags;
+};
+
+union drm_amdgpu_profile {
+	struct drm_amdgpu_profile_in in;
+	struct drm_amdgpu_profile_out out;
+};
+
  /*
   * Supported GPU families
   */
#5 lore raw
Quoted 17 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 6 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow. Do you mean to integrate this with the rendering context? That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

Alex

Quoted 247 lines

Christian.

Quoted 244 lines
---
  drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
  include/uapi/drm/amdgpu_drm.h               |  28 +++++
  5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
      amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
      amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
      amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
  amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
  #include "amdgpu_sched.h"
  #include "amdgpu_fdinfo.h"
  #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
  #include "amdgpu_ras.h"
  #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
      DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
      DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
      DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  };
 
  static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +23 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
  #define DRM_AMDGPU_VM                       0x13
  #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
  #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
  #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
  #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
  #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
  #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
  #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
  /**
   * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
      struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
  };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
Quoted 9 lines

include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_info_video_caps { · line 1153

Quoted 8 lines
include/uapi/drm/amdgpu_drm.h +5 -0
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
  /*
   * Supported GPU families
   */

commit 03ce38b4f5fc9d43820c669cba7bd8c03e3bdc02

Author: Alex Deucher <[email protected]>
Date:   Wed Dec 1 16:17:13 2021 -0500

drm/amdgpu/UAPI: add new CTX OP for setting profile modes

Add a new CTX ioctl operation to set profile modes. When creating traces for tools like RGP or using SPM or doing performance profiling, it's required to enable a special stable profiling power state on the GPU. These profiling states set fixed clocks and disable certain other power features like powergating which may impact the results.

Historically, these profiles were enabled via sysfs, but this adds an interface to enable it via the CTX ioctl from the application. Since the power state is global only one application can set it at a time, so if multiple applications try and use it only the first will get it, the ioctl will return -EBUSY for others.

drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -0
index c5cfe2926ca1..95b8908bd2e5 100644
@@ -1097,6 +1097,9 @@ struct amdgpu_device {
 
 	struct amdgpu_reset_control     *reset_cntl;
 	uint32_t                        ip_versions[MAX_HWIP][HWIP_MAX_INSTANCE];
+
+	struct mutex                    pstate_profile_ctx_lock;
+	struct amdgpu_ctx               *pstate_profile_ctx;
 };
 
 static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +99 -2

135 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index a44b8b8ed39c..7007ec9450dd 100644
@@ -53,6 +53,7 @@ struct amdgpu_ctx {
 	atomic_t			guilty;
 	unsigned long			ras_counter_ce;
 	unsigned long			ras_counter_ue;
+	uint32_t			pstate_profile;
 };
 
 struct amdgpu_ctx_mgr {
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index a1c14466f23d..c72e6153239a 100644
@@ -3452,6 +3452,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 	init_rwsem(&adev->reset_sem);
 	mutex_init(&adev->psp.mutex);
 	mutex_init(&adev->notifier_lock);
+	mutex_init(&adev->pstate_profile_ctx_lock);
 
 	r = amdgpu_device_init_apu_flags(adev);
 	if (r)
include/uapi/drm/amdgpu_drm.h +9 -0
index b9d68339103d..a4c2f7dc2f77 100644
@@ -206,6 +206,7 @@ union drm_amdgpu_bo_list {
 #define AMDGPU_CTX_OP_FREE_CTX	2
 #define AMDGPU_CTX_OP_QUERY_STATE	3
 #define AMDGPU_CTX_OP_QUERY_STATE2	4
+#define AMDGPU_CTX_OP_SET_PSTATE_PROFILE	5
 
 /* GPU reset status */
 #define AMDGPU_CTX_NO_RESET		0
@@ -238,6 +239,14 @@ union drm_amdgpu_bo_list {
 #define AMDGPU_CTX_PRIORITY_HIGH        512
 #define AMDGPU_CTX_PRIORITY_VERY_HIGH   1023
 
+/* select a profiling pstate for perfmon tools */
+#define AMDGPU_CTX_PSTATE_PROFILE_FLAGS_MASK  0xf
+#define AMDGPU_CTX_PSTATE_PROFILE_NONE  0
+#define AMDGPU_CTX_PSTATE_PROFILE_STANDARD  1
+#define AMDGPU_CTX_PSTATE_PROFILE_MIN_SCLK  2
+#define AMDGPU_CTX_PSTATE_PROFILE_MIN_MCLK  3
+#define AMDGPU_CTX_PSTATE_PROFILE_PEAK  4
+
 struct drm_amdgpu_ctx_in {
 	/** AMDGPU_CTX_OP_* */
 	__u32	op;
#6 lore raw

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow. Do you mean to integrate this with the rendering context? That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

Christian.

Quoted 257 lines

Alex

Quoted 253 lines

Christian.

Quoted 251 lines
---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
   include/uapi/drm/amdgpu_drm.h               |  28 +++++
   5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
       amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
       amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
       amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
   #include "amdgpu_sched.h"
   #include "amdgpu_fdinfo.h"
   #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
   #include "amdgpu_ras.h"
   #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
       DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   };
 
   static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
   #define DRM_AMDGPU_VM                       0x13
   #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
   #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
   #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
   #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
   #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
   #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
   #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
   /**
    * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
       struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
   };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
   /*
    * Supported GPU families
    */
#7 lore raw
Quoted 34 lines · Christian König

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling.  The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize.  Note that the power state is global so setting it will affect all applications.  There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow.  Do you mean to integrate this with the rendering context?  That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

Quoted 7 lines

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

That doesn't make sense to me. This is not optimizing scheduling for power. These settings are applied to the whole hardware. Even if we assume a hypothetical case of 'no-scheduler' (only one app running), the settings can be used to figure out whether an app is sensitive to GFXCLK vs MCLK or which phases of the app are sensitive.

Thanks,
Lijo

Quoted 305 lines

Christian.

Quoted 301 lines

Alex

Quoted 297 lines

Christian.

Quoted 295 lines

---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 + drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++ include/uapi/drm/amdgpu_drm.h               |  28 +++++

   5 files changed, 173 insertions(+), 1 deletion(-)

   create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 

   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc 
 amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, 
 DRM_AUTH|DRM_RENDER_ALLOW),
    };

   static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +35 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = 

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO; +                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +4 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +2 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
    #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + 
 DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**

    * DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#8 lore raw

Am 06.12.21 um 11:56 schrieb Lazar, Lijo:

Quoted 37 lines · Christian König
Quoted 36 lines

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling.  The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize.  Note that the power state is global so setting it will affect all applications.  There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow.  Do you mean to integrate this with the rendering context?  That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

Quoted 7 lines
Quoted 5 lines

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

That doesn't make sense to me. This is not optimizing scheduling for power. These settings are applied to the whole hardware. Even if we assume a hypothetical case of 'no-scheduler' (only one app running), the settings can be used to figure out whether an app is sensitive to GFXCLK vs MCLK or which phases of the app are sensitive.

What clocks are eventually in use is pretty much irrelevant. The functionality is that the application is defining a power level to use when it is active.

That we currently do that globally and only allow the first application to modify this is an implementation detail.

Regards,
Christian.

Quoted 310 lines

Thanks,
Lijo

Quoted 305 lines

Christian.

Quoted 301 lines

Alex

Quoted 297 lines

Christian.

Quoted 295 lines

---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 + drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++ include/uapi/drm/amdgpu_drm.h               |  28 +++++

   5 files changed, 173 insertions(+), 1 deletion(-)

   create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 

   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc 
 amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, 
 DRM_AUTH|DRM_RENDER_ALLOW),
    };

   static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +35 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = 

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO; +                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+ mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +4 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +2 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
    #define DRM_IOCTL_AMDGPU_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE DRM_IOW(DRM_COMMAND_BASE + 
 DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**

    * DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#9 lore raw
Quoted 40 lines · Christian König

Am 06.12.21 um 11:56 schrieb Lazar, Lijo:

Quoted 39 lines · Christian König
Quoted 38 lines

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling.  The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize.  Note that the power state is global so setting it will affect all applications.  There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow.  Do you mean to integrate this with the rendering context?  That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

That doesn't make sense to me. This is not optimizing scheduling for power. These settings are applied to the whole hardware. Even if we assume a hypothetical case of 'no-scheduler' (only one app running), the settings can be used to figure out whether an app is sensitive to GFXCLK vs MCLK or which phases of the app are sensitive.

What clocks are eventually in use is pretty much irrelevant. The functionality is that the application is defining a power level to use when it is active.

Exactly and it's irrelevant how it is made active. It just wants to run something under certain conditions for specific cases.

For ex: it could just collect some data for a time period less than scheduled quantum and it could make use of in the next phase of its logic. It is very much internal to the app and I don't see any scheduler related logic inside that (just because an app is made active by a scheduler).

Thanks,
Lijo

Quoted 316 lines

That we currently do that globally and only allow the first application to modify this is an implementation detail.

Regards,
Christian.

Quoted 310 lines

Thanks,
Lijo

Quoted 305 lines

Christian.

Quoted 301 lines

Alex

Quoted 297 lines

Christian.

Quoted 295 lines

---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 + drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++ include/uapi/drm/amdgpu_drm.h               |  28 +++++

   5 files changed, 173 insertions(+), 1 deletion(-)

   create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 

   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc 
 amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, 
 DRM_AUTH|DRM_RENDER_ALLOW),
    };

   static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +35 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = 

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO; +                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+ mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +4 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +2 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
    #define DRM_IOCTL_AMDGPU_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE DRM_IOW(DRM_COMMAND_BASE + 
 DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**

    * DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#10 lore raw

Am 06.12.21 um 12:36 schrieb Lazar, Lijo:

Quoted 45 lines · Christian König
Quoted 44 lines

Am 06.12.21 um 11:56 schrieb Lazar, Lijo:

Quoted 43 lines · Christian König
Quoted 41 lines

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize.  Note that the power state is global so setting it will affect all applications.  There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow.  Do you mean to integrate this with the rendering context?  That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That doesn't make sense to me. This is not optimizing scheduling for power. These settings are applied to the whole hardware. Even if we assume a hypothetical case of 'no-scheduler' (only one app running), the settings can be used to figure out whether an app is sensitive to GFXCLK vs MCLK or which phases of the app are sensitive.

What clocks are eventually in use is pretty much irrelevant. The functionality is that the application is defining a power level to use when it is active.

Exactly and it's irrelevant how it is made active. It just wants to run something under certain conditions for specific cases.

For ex: it could just collect some data for a time period less than scheduled quantum and it could make use of in the next phase of its logic. It is very much internal to the app and I don't see any scheduler related logic inside that (just because an app is made active by a scheduler).

This is about the GPU scheduler who decides which application runs on the GPU and not related to the CPU scheduler in any way.

Our current plan is to expose the performance level globally, because it is to much overhead to change the performance level on each application switch.

But ideally you won't do that. Instead each application should note the performance level it wants to the kernel and the kernel then makes an adequate decision based on that.

Since this is related to how jobs are scheduled and switching between applications I suggested to put it into amdgpu_scheduler.c.

Regards,
Christian.

Quoted 322 lines

Thanks,
Lijo

Quoted 318 lines

That we currently do that globally and only allow the first application to modify this is an implementation detail.

Regards,
Christian.

Quoted 312 lines

Thanks,
Lijo

Quoted 307 lines

Christian.

Quoted 303 lines

Alex

Quoted 299 lines

Christian.

Quoted 297 lines

---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 + drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++ include/uapi/drm/amdgpu_drm.h               |  28 +++++

   5 files changed, 173 insertions(+), 1 deletion(-)

   create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 

   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc 
 amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, 
 DRM_AUTH|DRM_RENDER_ALLOW),
    };

   static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +20 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = 

adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level; +     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level; +
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO; +                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+ mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level); +                     if (!r)
+ adev->pm.dpm.forced_level = requested_level;
+ mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +4 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +2 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE 
+ DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)

   #define DRM_IOCTL_AMDGPU_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)

include/uapi/drm/amdgpu_drm.h +1 -0
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE DRM_IOW(DRM_COMMAND_BASE + 
 DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**

    * DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK 0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE 0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD 1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK 2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK 3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK 4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#11 lore raw
Quoted 53 lines · Christian König

Am 06.12.21 um 12:36 schrieb Lazar, Lijo:

Quoted 52 lines · Christian König
Quoted 40 lines

Am 06.12.21 um 11:56 schrieb Lazar, Lijo:

Quoted 39 lines · Christian König
Quoted 38 lines

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize.  Note that the power state is global so setting it will affect all applications.  There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow.  Do you mean to integrate this with the rendering context?  That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

That doesn't make sense to me. This is not optimizing scheduling for power. These settings are applied to the whole hardware. Even if we assume a hypothetical case of 'no-scheduler' (only one app running), the settings can be used to figure out whether an app is sensitive to GFXCLK vs MCLK or which phases of the app are sensitive.

What clocks are eventually in use is pretty much irrelevant. The functionality is that the application is defining a power level to use when it is active.

Exactly and it's irrelevant how it is made active. It just wants to run something under certain conditions for specific cases.

Quoted 8 lines
Quoted 1 line

For ex: it could just collect some data for a time period less than scheduled quantum and it could make use of in the next phase of its logic. It is very much internal to the app and I don't see any scheduler related logic inside that (just because an app is made active by a scheduler).

This is about the GPU scheduler who decides which application runs on the GPU and not related to the CPU scheduler in any way.

Our current plan is to expose the performance level globally, because it is to much overhead to change the performance level on each application switch.

But ideally you won't do that. Instead each application should note the performance level it wants to the kernel and the kernel then makes an adequate decision based on that.

If I understand correctly, in the ideal case, the current implementation of immediately switching to requested perf level shouldn't be done. It should only be associated with that context and then used later. The current one is way too far from ideal.

Thanks for explaining.

Thanks,
Lijo

Quoted 328 lines

Since this is related to how jobs are scheduled and switching between applications I suggested to put it into amdgpu_scheduler.c.

Regards,
Christian.

Quoted 322 lines

Thanks,
Lijo

Quoted 318 lines

That we currently do that globally and only allow the first application to modify this is an implementation detail.

Regards,
Christian.

Quoted 312 lines

Thanks,
Lijo

Quoted 307 lines

Christian.

Quoted 303 lines

Alex

Quoted 299 lines

Christian.

Quoted 297 lines

---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 + drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++ include/uapi/drm/amdgpu_drm.h               |  28 +++++

   5 files changed, 173 insertions(+), 1 deletion(-)

   create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 

   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc 
 amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, 
 DRM_AUTH|DRM_RENDER_ALLOW),
    };

   static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +20 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = 

adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level; +     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level; +
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO; +                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+ mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level); +                     if (!r)
+ adev->pm.dpm.forced_level = requested_level;
+ mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +4 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person 

obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +2 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE 
+ DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)

   #define DRM_IOCTL_AMDGPU_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)

include/uapi/drm/amdgpu_drm.h +1 -0
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE DRM_IOW(DRM_COMMAND_BASE + 
 DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**

    * DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK 0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE 0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD 1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK 2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK 3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK 4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#12 lore raw

Am 06.12.21 um 14:23 schrieb Lazar, Lijo:

Quoted 56 lines · Christian König
Quoted 55 lines

Am 06.12.21 um 12:36 schrieb Lazar, Lijo:

Quoted 53 lines · Christian König
Quoted 40 lines

Am 06.12.21 um 11:56 schrieb Lazar, Lijo:

Quoted 39 lines · Christian König
Quoted 38 lines

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize.  Note that the power state is global so setting it will affect all applications.  There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow.  Do you mean to integrate this with the rendering context?  That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

That doesn't make sense to me. This is not optimizing scheduling for power. These settings are applied to the whole hardware. Even if we assume a hypothetical case of 'no-scheduler' (only one app running), the settings can be used to figure out whether an app is sensitive to GFXCLK vs MCLK or which phases of the app are sensitive.

What clocks are eventually in use is pretty much irrelevant. The functionality is that the application is defining a power level to use when it is active.

Exactly and it's irrelevant how it is made active. It just wants to run something under certain conditions for specific cases.

For ex: it could just collect some data for a time period less than scheduled quantum and it could make use of in the next phase of its logic. It is very much internal to the app and I don't see any scheduler related logic inside that (just because an app is made active by a scheduler).

Quoted 8 lines
Quoted 6 lines

This is about the GPU scheduler who decides which application runs on the GPU and not related to the CPU scheduler in any way.

Our current plan is to expose the performance level globally, because it is to much overhead to change the performance level on each application switch.

But ideally you won't do that. Instead each application should note the performance level it wants to the kernel and the kernel then makes an adequate decision based on that.

If I understand correctly, in the ideal case, the current implementation of immediately switching to requested perf level shouldn't be done. It should only be associated with that context and then used later. The current one is way too far from ideal.

Yes, exactly that.

Alex initial patch even tied this to the context, but then we said: Ok an application should at least be capable to figure that out for itself.

One core problem is also that you don't have performance levels like low, medium or high. Instead that's use case driven like desktop, compute, encoding or gaming.

Regards,
Christian.

Quoted 335 lines

Thanks for explaining.

Thanks,
Lijo

Quoted 329 lines

Since this is related to how jobs are scheduled and switching between applications I suggested to put it into amdgpu_scheduler.c.

Regards,
Christian.

Quoted 323 lines

Thanks,
Lijo

Quoted 319 lines

That we currently do that globally and only allow the first application to modify this is an implementation detail.

Regards,
Christian.

Quoted 313 lines

Thanks,
Lijo

Quoted 308 lines

Christian.

Quoted 304 lines

Alex

Quoted 300 lines

Christian.

Quoted 298 lines

---
   drivers/gpu/drm/amd/amdgpu/Makefile         | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h | 30 ++++++ include/uapi/drm/amdgpu_drm.h               | 28 +++++

   5 files changed, 173 insertions(+), 1 deletion(-)

   create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 

   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc 
 amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, 
 DRM_AUTH|DRM_RENDER_ALLOW),
    };

   static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any 

person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +20 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = 

adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level; +     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level; +
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+ mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level); +                     if (!r)
+ adev->pm.dpm.forced_level = requested_level;
+ mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +4 -0
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +4 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any 

person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +3 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM 0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
    #define DRM_IOCTL_AMDGPU_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE 
+ DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE DRM_IOW(DRM_COMMAND_BASE + 
 DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**

    * DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK 0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE 0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD 1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK 2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK 3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK 4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#13 lore raw
In reply to Christian König
Quoted 37 lines · Christian König

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow. Do you mean to integrate this with the rendering context? That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

Quoted 7 lines

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

The problem is that the sched ioctl is currently DRM_MASTER only. That kind of defeats the purpose of putting this in a user ioctl in the first place.

Alex

Quoted 261 lines

Christian.

Quoted 257 lines

Alex

Quoted 253 lines

Christian.

Quoted 251 lines
---
   drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
   include/uapi/drm/amdgpu_drm.h               |  28 +++++
   5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
       amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
       amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
       amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
   amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
   #include "amdgpu_sched.h"
   #include "amdgpu_fdinfo.h"
   #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
   #include "amdgpu_ras.h"
   #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
       DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   };
 
   static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
   #define DRM_AMDGPU_VM                       0x13
   #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
   #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
   #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
   #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
   #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
   #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
   #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
   /**
    * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
       struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
   };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
   /*
    * Supported GPU families
    */
#14 lore raw

Am 14.12.21 um 15:53 schrieb Alex Deucher:

Quoted 40 lines
Quoted 38 lines · Christian König

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow. Do you mean to integrate this with the rendering context? That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Quoted 7 lines
Quoted 4 lines

Putting the hardware power management control alongside that kind of makes sense I think.

Something like AMDGPU_SCHED_OP_POWER.

The problem is that the sched ioctl is currently DRM_MASTER only. That kind of defeats the purpose of putting this in a user ioctl in the first place.

My recollection was that we wanted to double check if DRM_MASTER wouldn't be sufficient. Mhm, so we really need to open that up for everybody?

If the scheduler IOCTL really doesn't fit we can still add another IOCTL. I would just try to avoid adding another file just for another feature, otherwise we will quickly end up with quite a lot of them.

Christian.

Quoted 262 lines

Alex

Quoted 258 lines

Christian.

Quoted 256 lines

Alex

Quoted 253 lines

Christian.

Quoted 251 lines
---
    drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
    drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
    drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
    include/uapi/drm/amdgpu_drm.h               |  28 +++++
    5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
    amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
    };
 
    static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
    #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**
     * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#15 lore raw
Quoted 45 lines · Christian König

Am 14.12.21 um 15:53 schrieb Alex Deucher:

Quoted 43 lines
Quoted 41 lines · Christian König

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow. Do you mean to integrate this with the rendering context? That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Something like AMDGPU_SCHED_OP_POWER.

The problem is that the sched ioctl is currently DRM_MASTER only. That kind of defeats the purpose of putting this in a user ioctl in the first place.

My recollection was that we wanted to double check if DRM_MASTER wouldn't be sufficient. Mhm, so we really need to open that up for everybody?

Tools and UMD teams don't want to require that, otherwise, they could just use the existing sysfs interface.

Quoted 4 lines

If the scheduler IOCTL really doesn't fit we can still add another IOCTL. I would just try to avoid adding another file just for another feature, otherwise we will quickly end up with quite a lot of them.

That's why I originally thought it made sense as part of the context ioctl since it's sort of related to the context in that the user wants to apply it to their rendering context (even though it is global).

Alex

Quoted 266 lines

Christian.

Quoted 262 lines

Alex

Quoted 258 lines

Christian.

Quoted 256 lines

Alex

Quoted 253 lines

Christian.

Quoted 251 lines
---
    drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
    drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
    drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
    include/uapi/drm/amdgpu_drm.h               |  28 +++++
    5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
        amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
        amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
        amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
    amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
    #include "amdgpu_sched.h"
    #include "amdgpu_fdinfo.h"
    #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
    };
 
    static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
    #define DRM_AMDGPU_VM                       0x13
    #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
    #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
    #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
    #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
    #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
    #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
    #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
    /**
     * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
        struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
    };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
    /*
     * Supported GPU families
     */
#16 lore raw

Am 14.12.21 um 16:12 schrieb Alex Deucher:

Quoted 56 lines
Quoted 44 lines · Christian König

Am 14.12.21 um 15:53 schrieb Alex Deucher:

Quoted 43 lines
Quoted 41 lines · Christian König

Am 03.12.21 um 17:13 schrieb Alex Deucher:

Quoted 25 lines
Quoted 16 lines · Christian König

Am 02.12.21 um 20:19 schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 5 lines
Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

First of all I wouldn't call this profiling mode. Profiling is the use case, but performance and power management are what is controlled here.

Ok.

Quoted 7 lines
Quoted 3 lines

Then we already have functionality for process and context priority override for the SW scheduler in amdgpu_sched.c. I think we should add this functionality there.

I'm not sure I follow. Do you mean to integrate this with the rendering context? That was what my original patch did (attached). Or were you thinking it would be better to make this part of the sched ioctl?

The later,

The scheduler IOCTLs are all about adjusting the default priority of a process (and optionally a context as well).

Putting the hardware power management control alongside that kind of makes sense I think.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Something like AMDGPU_SCHED_OP_POWER.

The problem is that the sched ioctl is currently DRM_MASTER only. That kind of defeats the purpose of putting this in a user ioctl in the first place.

My recollection was that we wanted to double check if DRM_MASTER wouldn't be sufficient. Mhm, so we really need to open that up for everybody?

Tools and UMD teams don't want to require that, otherwise, they could just use the existing sysfs interface.

Quoted 6 lines
Quoted 3 lines

If the scheduler IOCTL really doesn't fit we can still add another IOCTL. I would just try to avoid adding another file just for another feature, otherwise we will quickly end up with quite a lot of them.

That's why I originally thought it made sense as part of the context ioctl since it's sort of related to the context in that the user wants to apply it to their rendering context (even though it is global).

Yeah, I know what you mean. Maybe that's the best option then.

I mean the kernel needs some policy what to do when with oposing requests from userspace anyway.

I just thought that doing it on a per fd basis would move at least a little bit of those decisions to userspace.

Christian.

Quoted 266 lines

Alex

Quoted 263 lines

Christian.

Quoted 261 lines

Alex

Quoted 258 lines

Christian.

Quoted 256 lines

Alex

Quoted 253 lines

Christian.

Quoted 251 lines
---
     drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
     drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
     drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
     drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
     include/uapi/drm/amdgpu_drm.h               |  28 +++++
     5 files changed, 173 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h

drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
         amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
         amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
         amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-     amdgpu_eeprom.o amdgpu_mca.o
+     amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
     amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
     #include "amdgpu_sched.h"
     #include "amdgpu_fdinfo.h"
     #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
     #include "amdgpu_ras.h"
     #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
         DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
         DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
         DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+     DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
     };
 
     static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp)
+{
+     union drm_amdgpu_profile *args = data;
+     struct amdgpu_device *adev = drm_to_adev(dev);
+     const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+     enum amd_dpm_forced_level current_level, requested_level;
+     int r;
+
+     if (pp_funcs->get_performance_level)
+             current_level = amdgpu_dpm_get_performance_level(adev);
+     else
+             current_level = adev->pm.dpm.forced_level;
+
+     switch (args->in.op) {
+     case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+             if (args->in.flags)
+                     return -EINVAL;
+             switch (current_level) {
+             case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+                     break;
+             case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+                     break;
+             default:
+                     args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+                     break;
+             }
+             break;
+     case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+             if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+                     return -EINVAL;
+             switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+                     requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+                     break;
+             case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+                     requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+                     break;
+             default:
+                     return -EINVAL;
+             }
+
+             if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+                     mutex_lock(&adev->pm.mutex);
+                     r = amdgpu_dpm_force_performance_level(adev, requested_level);
+                     if (!r)
+                             adev->pm.dpm.forced_level = requested_level;
+                     mutex_unlock(&adev->pm.mutex);
+                     if (r)
+                             return r;
+             }
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
     #define DRM_AMDGPU_VM                       0x13
     #define DRM_AMDGPU_FENCE_TO_HANDLE  0x14
     #define DRM_AMDGPU_SCHED            0x15
+#define DRM_AMDGPU_PROFILE           0x16
 
     #define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
     #define DRM_IOCTL_AMDGPU_GEM_MMAP   DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
     #define DRM_IOCTL_AMDGPU_VM         DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
     #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
     #define DRM_IOCTL_AMDGPU_SCHED              DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE     DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
     /**
      * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
         struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
     };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE  1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE  2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK      0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE      0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD  1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK  2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK  3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK      4
+
+struct drm_amdgpu_profile_in {
+     /** AMDGPU_PROFILE_OP_* */
+     __u32   op;
+     __u32   flags;
+};
+
+struct drm_amdgpu_profile_out {
+     __u64   flags;
+};
+
+union drm_amdgpu_profile {
+     struct drm_amdgpu_profile_in in;
+     struct drm_amdgpu_profile_out out;
+};
+
     /*
      * Supported GPU families
      */
#17 lore raw
In reply to Alex Deucher

[AMD Official Use Only]

Quoted 199 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Friday, December 3, 2021 3:19 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: [RFC PATCH 1/2] drm/amdgpu/UAPI: add new PROFILE IOCTL

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

---
 drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112
++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
 include/uapi/drm/amdgpu_drm.h               |  28 +++++
 5 files changed, 173 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
 	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o
 amdgpu_nbio.o \
 	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o
 amdgpu_rap.o \
 	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o
 \
-	amdgpu_eeprom.o amdgpu_mca.o
+	amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o

amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
 #include "amdgpu_sched.h"
 #include "amdgpu_fdinfo.h"
 #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
 #include "amdgpu_ras.h"
 #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] =
 {
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl,
 DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl,
 DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR,

amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
+	DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl,

DRM_AUTH|DRM_RENDER_ALLOW),
};

static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +5 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the

"Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +25 -0
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp)
+{
+	union drm_amdgpu_profile *args = data;
+	struct amdgpu_device *adev = drm_to_adev(dev);
+	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+	enum amd_dpm_forced_level current_level, requested_level;
+	int r;
+
+	if (pp_funcs->get_performance_level)
+		current_level =

amdgpu_dpm_get_performance_level(adev);

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +10 -0
+	else
+		current_level = adev->pm.dpm.forced_level;
+
+	switch (args->in.op) {
+	case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+		if (args->in.flags)
+			return -EINVAL;
+		switch (current_level) {
+		case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+			args->out.flags =

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+			args->out.flags =

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+			args->out.flags =

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+			args->out.flags =

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		default:
+			args->out.flags =

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +5 -0
+			break;
+		}
+		break;
+	case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+		if (args->in.flags &

~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +2 -0
+			return -EINVAL;
+		switch (args->in.flags &

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +1 -0
+		case

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +1 -0
+			requested_level =

AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+			requested_level =

AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +2 -0
+			break;
+		case

AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +1 -0
+			requested_level =

AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+			requested_level =

AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +3 -0
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+			requested_level =
Quoted 1 line

AMD_DPM_FORCED_LEVEL_AUTO;

[Quan, Evan] I think the idea(to expose those important power interfaces via ioctl) is good. Since current pm sysfs way seems less official(more like debug purpose). And most users do not even know there is(or how to use) such APIs. Another interface I can think of is the power profile mode setting related. As although there is more profile modes(like fullscreen3d, powersaving, vr). Only the "compute" and "video" modes get actually used in our driver.

For this specific patch, can you expand it to support like custom profiling mode(manual sclk/mclk setting)? I know we may need to update other power internal interfaces to get that(custom profling mode) supported. But can you update the data structure below to fully prepared for that?
Or do you think better to do this when the custom profling mode support is really available?

struct drm_amdgpu_profile_in {
	/** AMDGPU_PROFILE_OP_* */
	__u32	op;
	__u32	flags.
              __u32 sclk;    --> e..g when flag & AMDGPU_PROFILE_FLAGS_CUSTOM_PROFILE, these two members will be valid
             __u32 mclk;    -->
};

BR
Evan

Quoted 138 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +6 -0
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		if ((current_level != requested_level) && pp_funcs-
Quoted 1 line

force_performance_level) {

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +2 -0
+			mutex_lock(&adev->pm.mutex);
+			r = amdgpu_dpm_force_performance_level(adev,

requested_level);

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +2 -0
+			if (!r)
+				adev->pm.dpm.forced_level =

requested_level;

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +12 -0
+			mutex_unlock(&adev->pm.mutex);
+			if (r)
+				return r;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +5 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the

"Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +8 -0
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +1 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
 #define DRM_AMDGPU_VM			0x13
 #define DRM_AMDGPU_FENCE_TO_HANDLE	0x14
 #define DRM_AMDGPU_SCHED		0x15
+#define DRM_AMDGPU_PROFILE		0x16
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE
 	DRM_IOWR(DRM_COMMAND_BASE +

DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
#define DRM_IOCTL_AMDGPU_GEM_MMAP
DRM_IOWR(DRM_COMMAND_BASE +
DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)

include/uapi/drm/amdgpu_drm.h +0 -0
@@ -71,6 +72,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_VM
 	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union
 drm_amdgpu_vm)
 #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE
 DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE,
 union drm_amdgpu_fence_to_handle)
 #define DRM_IOCTL_AMDGPU_SCHED

DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union
drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE
DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE,
union drm_amdgpu_profile)

/**
* DOC: memory domains

include/uapi/drm/amdgpu_drm.h +26 -0
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
 	struct drm_amdgpu_info_video_codec_info
 codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
 };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE	1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE	2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK	0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE	0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD	1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK	2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK	3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK	4
+
+struct drm_amdgpu_profile_in {
+	/** AMDGPU_PROFILE_OP_* */
+	__u32	op;
+	__u32	flags;
+};
+
+struct drm_amdgpu_profile_out {
+	__u64	flags;
+};
+
+union drm_amdgpu_profile {
+	struct drm_amdgpu_profile_in in;
+	struct drm_amdgpu_profile_out out;
+};
+
 /*
  * Supported GPU families

*/
--
2.31.1

#18 lore raw
In reply to Alex Deucher

Am 2021-12-02 um 2:19 p.m. schrieb Alex Deucher:

Quoted 15 lines

This adds a new IOCTL currently used to implement querying and setting the stable power state for GPU profiling. The stable pstates use fixed clocks and disable certain power features in order to get accurate pipeline profiling.

Currently this is handled via sysfs, and that is still available, but this makes it easier for applications to utilize. Note that the power state is global so setting it will affect all applications. There are currently no checks in place to prevent multiple applications from using this interface, but it doesn't make sense to do profiling while you have multiple applications running in the first place, so it's up to the user to ensure this in order to get good results.

Quoted 2 lines

This patch add an interface to query what profiling mode is currently active and to set enable a profiling mode.

I was expecting this to be some exclusive mode that only one client could use at a time. If someone is already using it, other users should get -EBUSY. When the client terminates, the default mode should be restored automatically.

It would also be nice to coordinate with the existing sysfs interface in some defined way. E.g. the ioctl could override the mode chosen in sysfs and client termination would restore the mode chosen in sysfs. Or a non-default mode in sysfs could block the ioctl (return -EBUSY), sysfs writes fail if there is an ioctl client, and sysfs faithfully reports the current mode chosen by the ioctl.

Regards,
  Felix

Quoted 252 lines
---
 drivers/gpu/drm/amd/amdgpu/Makefile         |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c     |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c | 112 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h |  30 ++++++
 include/uapi/drm/amdgpu_drm.h               |  28 +++++
 5 files changed, 173 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 7fedbb725e17..4cf5bf637a9e 100644
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
 	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
 	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
 	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_hdp.o \
-	amdgpu_eeprom.o amdgpu_mca.o
+	amdgpu_eeprom.o amdgpu_mca.o amdgpu_profile.o
 
 amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index bc1355c6248d..0e27f9673f8f 100644
@@ -46,6 +46,7 @@
 #include "amdgpu_sched.h"
 #include "amdgpu_fdinfo.h"
 #include "amdgpu_amdkfd.h"
+#include "amdgpu_profile.h"
 
 #include "amdgpu_ras.h"
 #include "amdgpu_xgmi.h"
@@ -2467,6 +2468,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_PROFILE, amdgpu_profile_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.c new file +112 -0
new file mode 100644
index 000000000000..94fe408e810f
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+
+/**
+ * amdgpu_profile_ioctl - Manages settings for profiling.
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_vm
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp)
+{
+	union drm_amdgpu_profile *args = data;
+	struct amdgpu_device *adev = drm_to_adev(dev);
+	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
+	enum amd_dpm_forced_level current_level, requested_level;
+	int r;
+
+	if (pp_funcs->get_performance_level)
+		current_level = amdgpu_dpm_get_performance_level(adev);
+	else
+		current_level = adev->pm.dpm.forced_level;
+
+	switch (args->in.op) {
+	case AMDGPU_PROFILE_OP_GET_STABLE_PSTATE:
+		if (args->in.flags)
+			return -EINVAL;
+		switch (current_level) {
+		case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK;
+			break;
+		case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK;
+			break;
+		default:
+			args->out.flags = AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE;
+			break;
+		}
+		break;
+	case AMDGPU_PROFILE_OP_SET_STABLE_PSTATE:
+		if (args->in.flags & ~AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK)
+			return -EINVAL;
+		switch (args->in.flags & AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK) {
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK:
+			requested_level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
+			break;
+		case AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE:
+			requested_level = AMD_DPM_FORCED_LEVEL_AUTO;
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		if ((current_level != requested_level) && pp_funcs->force_performance_level) {
+			mutex_lock(&adev->pm.mutex);
+			r = amdgpu_dpm_force_performance_level(adev, requested_level);
+			if (!r)
+				adev->pm.dpm.forced_level = requested_level;
+			mutex_unlock(&adev->pm.mutex);
+			if (r)
+				return r;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_profile.h new file +30 -0
new file mode 100644
index 000000000000..cd1c597bae11
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILE_H__
+#define __AMDGPU_PROFILE_H__
+
+int amdgpu_profile_ioctl(struct drm_device *dev, void *data,
+			 struct drm_file *filp);
+
+#endif
include/uapi/drm/amdgpu_drm.h +28 -0
index 26e45fc5eb1a..b6edf4a826f9 100644
@@ -54,6 +54,7 @@ extern "C" {
 #define DRM_AMDGPU_VM			0x13
 #define DRM_AMDGPU_FENCE_TO_HANDLE	0x14
 #define DRM_AMDGPU_SCHED		0x15
+#define DRM_AMDGPU_PROFILE		0x16
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -71,6 +72,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_VM		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_VM, union drm_amdgpu_vm)
 #define DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_FENCE_TO_HANDLE, union drm_amdgpu_fence_to_handle)
 #define DRM_IOCTL_AMDGPU_SCHED		DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_SCHED, union drm_amdgpu_sched)
+#define DRM_IOCTL_AMDGPU_PROFILE	DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILE, union drm_amdgpu_profile)
 
 /**
  * DOC: memory domains
@@ -1120,6 +1122,32 @@ struct drm_amdgpu_info_video_caps {
 	struct drm_amdgpu_info_video_codec_info codec_info[AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_COUNT];
 };
 
+/* profile ioctl */
+#define AMDGPU_PROFILE_OP_GET_STABLE_PSTATE	1
+#define AMDGPU_PROFILE_OP_SET_STABLE_PSTATE	2
+
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MASK	0xf
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_NONE	0
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_STANDARD	1
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_SCLK	2
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_MIN_MCLK	3
+#define AMDGPU_PROFILE_FLAGS_STABLE_PSTATE_PEAK	4
+
+struct drm_amdgpu_profile_in {
+	/** AMDGPU_PROFILE_OP_* */
+	__u32	op;
+	__u32	flags;
+};
+
+struct drm_amdgpu_profile_out {
+	__u64	flags;
+};
+
+union drm_amdgpu_profile {
+	struct drm_amdgpu_profile_in in;
+	struct drm_amdgpu_profile_out out;
+};
+
 /*
  * Supported GPU families
  */