[PATCH] drm/amdgpu/pm/powerplay: factor voltage index bounds check into a macro

yaolu@kylinos.cn posted 1 patch 1 day, 7 hours ago
.../drm/amd/pm/powerplay/hwmgr/smu7_hwmgr.c   | 28 +++++----------
.../drm/amd/pm/powerplay/hwmgr/vega10_hwmgr.c | 35 ++++++-------------
.../gpu/drm/amd/pm/powerplay/inc/pp_debug.h   | 12 +++++++
3 files changed, 30 insertions(+), 45 deletions(-)
[PATCH] drm/amdgpu/pm/powerplay: factor voltage index bounds check into a macro
Posted by yaolu@kylinos.cn 1 day, 7 hours ago
From: Lu Yao <yaolu@kylinos.cn>

commit 3a8a05477cda ("drm/amdgpu/pm/powerplay: bounds-check voltage index in SMU7 lookup") and
commit 6fa33f594e46 ("drm/amdgpu/pm/powerplay: bounds-check voltage index in Vega10 lookup")
added the same block.

Move the pattern into PP_CHECK_VOLTAGE_INDEX().

Signed-off-by: Lu Yao <yaolu@kylinos.cn>
---
 .../drm/amd/pm/powerplay/hwmgr/smu7_hwmgr.c   | 28 +++++----------
 .../drm/amd/pm/powerplay/hwmgr/vega10_hwmgr.c | 35 ++++++-------------
 .../gpu/drm/amd/pm/powerplay/inc/pp_debug.h   | 12 +++++++
 3 files changed, 30 insertions(+), 45 deletions(-)

diff --git a/drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu7_hwmgr.c b/drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu7_hwmgr.c
index bdf1e489369e..aafb52bf94ff 100644
--- a/drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu7_hwmgr.c
+++ b/drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu7_hwmgr.c
@@ -2216,24 +2216,18 @@ static int smu7_patch_voltage_dependency_tables_with_lookup_table(
 	if (data->vdd_gfx_control == SMU7_VOLTAGE_CONTROL_BY_SVID2) {
 		for (entry_id = 0; entry_id < sclk_table->count; ++entry_id) {
 			voltage_id = sclk_table->entries[entry_id].vddInd;
-			if (voltage_id >= table_info->vddgfx_lookup_table->count) {
-				pr_err("amdgpu: sclk[%u] vddgfx index %u out of bounds (%u)\n",
-				       entry_id, voltage_id,
-				       table_info->vddgfx_lookup_table->count);
+			if (PP_VOLTAGE_INDEX_OOB(table_info->vddgfx_lookup_table, voltage_id,
+						 "sclk[%u] vddgfx", entry_id))
 				return -EINVAL;
-			}
 			sclk_table->entries[entry_id].vddgfx =
 				table_info->vddgfx_lookup_table->entries[voltage_id].us_vdd;
 		}
 	} else {
 		for (entry_id = 0; entry_id < sclk_table->count; ++entry_id) {
 			voltage_id = sclk_table->entries[entry_id].vddInd;
-			if (voltage_id >= table_info->vddc_lookup_table->count) {
-				pr_err("amdgpu: sclk[%u] vddc index %u out of bounds (%u)\n",
-				       entry_id, voltage_id,
-				       table_info->vddc_lookup_table->count);
+			if (PP_VOLTAGE_INDEX_OOB(table_info->vddc_lookup_table, voltage_id,
+						 "sclk[%u] vddc", entry_id))
 				return -EINVAL;
-			}
 			sclk_table->entries[entry_id].vddc =
 				table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
 		}
@@ -2241,24 +2235,18 @@ static int smu7_patch_voltage_dependency_tables_with_lookup_table(
 
 	for (entry_id = 0; entry_id < mclk_table->count; ++entry_id) {
 		voltage_id = mclk_table->entries[entry_id].vddInd;
-		if (voltage_id >= table_info->vddc_lookup_table->count) {
-			pr_err("amdgpu: mclk[%u] vddc index %u out of bounds (%u)\n",
-			       entry_id, voltage_id,
-			       table_info->vddc_lookup_table->count);
+		if (PP_VOLTAGE_INDEX_OOB(table_info->vddc_lookup_table, voltage_id,
+					 "mclk[%u] vddc", entry_id))
 			return -EINVAL;
-		}
 		mclk_table->entries[entry_id].vddc =
 			table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
 	}
 
 	for (entry_id = 0; entry_id < mm_table->count; ++entry_id) {
 		voltage_id = mm_table->entries[entry_id].vddcInd;
-		if (voltage_id >= table_info->vddc_lookup_table->count) {
-			pr_err("amdgpu: mm[%u] vddc index %u out of bounds (%u)\n",
-			       entry_id, voltage_id,
-			       table_info->vddc_lookup_table->count);
+		if (PP_VOLTAGE_INDEX_OOB(table_info->vddc_lookup_table, voltage_id,
+					 "mm[%u] vddc", entry_id))
 			return -EINVAL;
-		}
 		mm_table->entries[entry_id].vddc =
 			table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
 	}
diff --git a/drivers/gpu/drm/amd/pm/powerplay/hwmgr/vega10_hwmgr.c b/drivers/gpu/drm/amd/pm/powerplay/hwmgr/vega10_hwmgr.c
index 05b1e69116fd..96783710afe3 100644
--- a/drivers/gpu/drm/amd/pm/powerplay/hwmgr/vega10_hwmgr.c
+++ b/drivers/gpu/drm/amd/pm/powerplay/hwmgr/vega10_hwmgr.c
@@ -691,12 +691,9 @@ static int vega10_patch_voltage_dependency_tables_with_lookup_table(
 
 		for (entry_id = 0; entry_id < vdt->count; entry_id++) {
 			voltage_id = vdt->entries[entry_id].vddInd;
-			if (voltage_id >= table_info->vddc_lookup_table->count) {
-				pr_err("amdgpu: clk_dep[%u][%u] vddc index %u out of bounds (%u)\n",
-				       i, entry_id, voltage_id,
-				       table_info->vddc_lookup_table->count);
+			if (PP_VOLTAGE_INDEX_OOB(table_info->vddc_lookup_table, voltage_id,
+						 "clk_dep[%u][%u] vddc", i, entry_id))
 				return -EINVAL;
-			}
 			vdt->entries[entry_id].vddc =
 					table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
 		}
@@ -704,44 +701,32 @@ static int vega10_patch_voltage_dependency_tables_with_lookup_table(
 
 	for (entry_id = 0; entry_id < mm_table->count; ++entry_id) {
 		voltage_id = mm_table->entries[entry_id].vddcInd;
-		if (voltage_id >= table_info->vddc_lookup_table->count) {
-			pr_err("amdgpu: mm[%u] vddc index %u out of bounds (%u)\n",
-			       entry_id, voltage_id,
-			       table_info->vddc_lookup_table->count);
+		if (PP_VOLTAGE_INDEX_OOB(table_info->vddc_lookup_table, voltage_id,
+					 "mm[%u] vddc", entry_id))
 			return -EINVAL;
-		}
 		mm_table->entries[entry_id].vddc =
 			table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
 	}
 
 	for (entry_id = 0; entry_id < mclk_table->count; ++entry_id) {
 		voltage_id = mclk_table->entries[entry_id].vddInd;
-		if (voltage_id >= table_info->vddc_lookup_table->count) {
-			pr_err("amdgpu: mclk[%u] vddc index %u out of bounds (%u)\n",
-			       entry_id, voltage_id,
-			       table_info->vddc_lookup_table->count);
+		if (PP_VOLTAGE_INDEX_OOB(table_info->vddc_lookup_table, voltage_id,
+					 "mclk[%u] vddc", entry_id))
 			return -EINVAL;
-		}
 		mclk_table->entries[entry_id].vddc =
 				table_info->vddc_lookup_table->entries[voltage_id].us_vdd;
 
 		voltage_id = mclk_table->entries[entry_id].vddciInd;
-		if (voltage_id >= table_info->vddci_lookup_table->count) {
-			pr_err("amdgpu: mclk[%u] vddci index %u out of bounds (%u)\n",
-			       entry_id, voltage_id,
-			       table_info->vddci_lookup_table->count);
+		if (PP_VOLTAGE_INDEX_OOB(table_info->vddci_lookup_table, voltage_id,
+					 "mclk[%u] vddci", entry_id))
 			return -EINVAL;
-		}
 		mclk_table->entries[entry_id].vddci =
 				table_info->vddci_lookup_table->entries[voltage_id].us_vdd;
 
 		voltage_id = mclk_table->entries[entry_id].mvddInd;
-		if (voltage_id >= table_info->vddmem_lookup_table->count) {
-			pr_err("amdgpu: mclk[%u] vddmem index %u out of bounds (%u)\n",
-			       entry_id, voltage_id,
-			       table_info->vddmem_lookup_table->count);
+		if (PP_VOLTAGE_INDEX_OOB(table_info->vddmem_lookup_table, voltage_id,
+					 "mclk[%u] vddmem", entry_id))
 			return -EINVAL;
-		}
 		mclk_table->entries[entry_id].mvdd =
 				table_info->vddmem_lookup_table->entries[voltage_id].us_vdd;
 	}
diff --git a/drivers/gpu/drm/amd/pm/powerplay/inc/pp_debug.h b/drivers/gpu/drm/amd/pm/powerplay/inc/pp_debug.h
index cea65093b6ad..efe5f6df8c9b 100644
--- a/drivers/gpu/drm/amd/pm/powerplay/inc/pp_debug.h
+++ b/drivers/gpu/drm/amd/pm/powerplay/inc/pp_debug.h
@@ -54,6 +54,18 @@
 		pr_debug(fmt, ##__VA_ARGS__); \
 	} while (0)
 
+/* Reports and returns true when index does not fit into the lookup table. */
+#define PP_VOLTAGE_INDEX_OOB(lookup_table, index, fmt, ...)		\
+({									\
+	typeof(lookup_table) __lookup = (lookup_table);			\
+	typeof(index) __idx = (index);					\
+	bool __oob = __idx >= __lookup->count;				\
+									\
+	if (__oob)							\
+		pr_err(fmt " index %u out of bounds (%u)\n",		\
+		       ##__VA_ARGS__, __idx, __lookup->count);		\
+	__oob;								\
+})
 
 #define GET_FLEXIBLE_ARRAY_MEMBER_ADDR(type, member, ptr, n)	\
 	(type *)((char *)&(ptr)->member + (sizeof(type) * (n)))
-- 
2.25.1