[PATCH v2] drm/xe/hwmon: Simplify and fix 32b wrap

Lucas De Marchi lucas.demarchi at intel.com
Fri May 30 22:31:57 UTC 2025


Like done in commit eaa287069a70 ("drm/xe/guc_submit: Simplify and fix
diff calculation"), just use u32 for wrapping the value, which is
simpler and more correct: when wrapping on 32b, the accumulated value
was off by one.

Also, do not mix the u64 value from pmt with the u32 value used for the
calculation.

Cc: Badal Nilawar <badal.nilawar at intel.com>
Cc: Raag Jadav <raag.jadav at intel.com>
Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
Changes in v2:
- Rebase - now there's a u64 coming from pmt that was mixed as
  "reg_val", so use separate variables.
- Removed previous r-b due to the code changes. Raag, please let me know
  if you are still ok with this
- Link to v1: https://lore.kernel.org/r/20250529-xe-hwmon-wrap-v1-1-60d3f1eed17d@intel.com
---
 drivers/gpu/drm/xe/xe_hwmon.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 778354c91f598..0d32e977537ca 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -436,7 +436,7 @@ xe_hwmon_energy_get(struct xe_hwmon *hwmon, int channel, long *energy)
 {
 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
 	struct xe_hwmon_energy_info *ei = &hwmon->ei[channel];
-	u64 reg_val;
+	u32 reg_val;
 	int ret = 0;
 
 	/* Energy is supported only for card and pkg */
@@ -446,29 +446,27 @@ xe_hwmon_energy_get(struct xe_hwmon *hwmon, int channel, long *energy)
 	}
 
 	if (hwmon->xe->info.platform == XE_BATTLEMAGE) {
+		u64 pmt_val;
+
 		ret = xe_pmt_telem_read(to_pci_dev(hwmon->xe->drm.dev),
 					xe_mmio_read32(mmio, PUNIT_TELEMETRY_GUID),
-					&reg_val, BMG_ENERGY_STATUS_PMT_OFFSET,	sizeof(reg_val));
-		if (ret != sizeof(reg_val)) {
+					&pmt_val, BMG_ENERGY_STATUS_PMT_OFFSET,	sizeof(pmt_val));
+		if (ret != sizeof(pmt_val)) {
 			drm_warn(&hwmon->xe->drm, "energy read from pmt failed, ret %d\n", ret);
 			*energy = 0;
 			return;
 		}
 
 		if (channel == CHANNEL_PKG)
-			reg_val = REG_FIELD_GET64(ENERGY_PKG, reg_val);
+			reg_val = REG_FIELD_GET64(ENERGY_PKG, pmt_val);
 		else
-			reg_val = REG_FIELD_GET64(ENERGY_CARD, reg_val);
+			reg_val = REG_FIELD_GET64(ENERGY_CARD, pmt_val);
 	} else {
 		reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_ENERGY_STATUS,
 								channel));
 	}
 
-	if (reg_val >= ei->reg_val_prev)
-		ei->accum_energy += reg_val - ei->reg_val_prev;
-	else
-		ei->accum_energy += UINT_MAX - ei->reg_val_prev + reg_val;
-
+	ei->accum_energy += reg_val - ei->reg_val_prev;
 	ei->reg_val_prev = reg_val;
 
 	*energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,





More information about the Intel-xe mailing list