Commit dea7a2b4 authored by Lucas De Marchi's avatar Lucas De Marchi
Browse files

drm/xe/hwmon: Simplify and fix 32b wrap



Like done in commit eaa28706 ("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@intel.com>
Cc: Raag Jadav <raag.jadav@intel.com>
Reviewed-by: default avatarRaag Jadav <raag.jadav@intel.com>
Link: https://lore.kernel.org/r/20250530-xe-hwmon-wrap-v2-1-ce653db7fe4a@intel.com


Signed-off-by: default avatarLucas De Marchi <lucas.demarchi@intel.com>
parent ccd3c682
Loading
Loading
Loading
Loading
+8 −10
Original line number Diff line number Diff line
@@ -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->reg_val_prev = reg_val;

	*energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,