xref: /linux/drivers/gpu/drm/xe/xe_hwmon.c (revision 28472374291c380c22f40deec07a90d09bcbffb6)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <linux/hwmon-sysfs.h>
7 #include <linux/hwmon.h>
8 #include <linux/jiffies.h>
9 #include <linux/types.h>
10 #include <linux/units.h>
11 
12 #include <drm/drm_managed.h>
13 #include "regs/xe_gt_regs.h"
14 #include "regs/xe_mchbar_regs.h"
15 #include "regs/xe_pcode_regs.h"
16 #include "xe_device.h"
17 #include "xe_hwmon.h"
18 #include "xe_mmio.h"
19 #include "xe_pcode.h"
20 #include "xe_pcode_api.h"
21 #include "xe_sriov.h"
22 #include "xe_pm.h"
23 
24 enum xe_hwmon_reg {
25 	REG_TEMP,
26 	REG_PKG_RAPL_LIMIT,
27 	REG_PKG_POWER_SKU,
28 	REG_PKG_POWER_SKU_UNIT,
29 	REG_GT_PERF_STATUS,
30 	REG_PKG_ENERGY_STATUS,
31 	REG_FAN_SPEED,
32 };
33 
34 enum xe_hwmon_reg_operation {
35 	REG_READ32,
36 	REG_RMW32,
37 	REG_READ64,
38 };
39 
40 enum xe_hwmon_channel {
41 	CHANNEL_CARD,
42 	CHANNEL_PKG,
43 	CHANNEL_VRAM,
44 	CHANNEL_MAX,
45 };
46 
47 enum xe_fan_channel {
48 	FAN_1,
49 	FAN_2,
50 	FAN_3,
51 	FAN_MAX,
52 };
53 
54 /*
55  * SF_* - scale factors for particular quantities according to hwmon spec.
56  */
57 #define SF_POWER	1000000		/* microwatts */
58 #define SF_CURR		1000		/* milliamperes */
59 #define SF_VOLTAGE	1000		/* millivolts */
60 #define SF_ENERGY	1000000		/* microjoules */
61 #define SF_TIME		1000		/* milliseconds */
62 
63 /**
64  * struct xe_hwmon_energy_info - to accumulate energy
65  */
66 struct xe_hwmon_energy_info {
67 	/** @reg_val_prev: previous energy reg val */
68 	u32 reg_val_prev;
69 	/** @accum_energy: accumulated energy */
70 	long accum_energy;
71 };
72 
73 /**
74  * struct xe_hwmon_fan_info - to cache previous fan reading
75  */
76 struct xe_hwmon_fan_info {
77 	/** @reg_val_prev: previous fan reg val */
78 	u32 reg_val_prev;
79 	/** @time_prev: previous timestamp */
80 	u64 time_prev;
81 };
82 
83 /**
84  * struct xe_hwmon - xe hwmon data structure
85  */
86 struct xe_hwmon {
87 	/** @hwmon_dev: hwmon device for xe */
88 	struct device *hwmon_dev;
89 	/** @xe: Xe device */
90 	struct xe_device *xe;
91 	/** @hwmon_lock: lock for rw attributes*/
92 	struct mutex hwmon_lock;
93 	/** @scl_shift_power: pkg power unit */
94 	int scl_shift_power;
95 	/** @scl_shift_energy: pkg energy unit */
96 	int scl_shift_energy;
97 	/** @scl_shift_time: pkg time unit */
98 	int scl_shift_time;
99 	/** @ei: Energy info for energyN_input */
100 	struct xe_hwmon_energy_info ei[CHANNEL_MAX];
101 	/** @fi: Fan info for fanN_input */
102 	struct xe_hwmon_fan_info fi[FAN_MAX];
103 };
104 
105 static struct xe_reg xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg,
106 				      int channel)
107 {
108 	struct xe_device *xe = hwmon->xe;
109 
110 	switch (hwmon_reg) {
111 	case REG_TEMP:
112 		if (xe->info.platform == XE_BATTLEMAGE) {
113 			if (channel == CHANNEL_PKG)
114 				return BMG_PACKAGE_TEMPERATURE;
115 			else if (channel == CHANNEL_VRAM)
116 				return BMG_VRAM_TEMPERATURE;
117 		} else if (xe->info.platform == XE_DG2) {
118 			if (channel == CHANNEL_PKG)
119 				return PCU_CR_PACKAGE_TEMPERATURE;
120 			else if (channel == CHANNEL_VRAM)
121 				return BMG_VRAM_TEMPERATURE;
122 		}
123 		break;
124 	case REG_PKG_RAPL_LIMIT:
125 		if (xe->info.platform == XE_BATTLEMAGE) {
126 			if (channel == CHANNEL_PKG)
127 				return BMG_PACKAGE_RAPL_LIMIT;
128 			else
129 				return BMG_PLATFORM_POWER_LIMIT;
130 		} else if (xe->info.platform == XE_PVC && channel == CHANNEL_PKG) {
131 			return PVC_GT0_PACKAGE_RAPL_LIMIT;
132 		} else if ((xe->info.platform == XE_DG2) && (channel == CHANNEL_PKG)) {
133 			return PCU_CR_PACKAGE_RAPL_LIMIT;
134 		}
135 		break;
136 	case REG_PKG_POWER_SKU:
137 		if (xe->info.platform == XE_BATTLEMAGE)
138 			return BMG_PACKAGE_POWER_SKU;
139 		else if (xe->info.platform == XE_PVC && channel == CHANNEL_PKG)
140 			return PVC_GT0_PACKAGE_POWER_SKU;
141 		else if ((xe->info.platform == XE_DG2) && (channel == CHANNEL_PKG))
142 			return PCU_CR_PACKAGE_POWER_SKU;
143 		break;
144 	case REG_PKG_POWER_SKU_UNIT:
145 		if (xe->info.platform == XE_BATTLEMAGE)
146 			return BMG_PACKAGE_POWER_SKU_UNIT;
147 		else if (xe->info.platform == XE_PVC)
148 			return PVC_GT0_PACKAGE_POWER_SKU_UNIT;
149 		else if (xe->info.platform == XE_DG2)
150 			return PCU_CR_PACKAGE_POWER_SKU_UNIT;
151 		break;
152 	case REG_GT_PERF_STATUS:
153 		if (xe->info.platform == XE_DG2 && channel == CHANNEL_PKG)
154 			return GT_PERF_STATUS;
155 		break;
156 	case REG_PKG_ENERGY_STATUS:
157 		if (xe->info.platform == XE_BATTLEMAGE) {
158 			if (channel == CHANNEL_PKG)
159 				return BMG_PACKAGE_ENERGY_STATUS;
160 			else
161 				return BMG_PLATFORM_ENERGY_STATUS;
162 		} else if (xe->info.platform == XE_PVC && channel == CHANNEL_PKG) {
163 			return PVC_GT0_PLATFORM_ENERGY_STATUS;
164 		} else if ((xe->info.platform == XE_DG2) && (channel == CHANNEL_PKG)) {
165 			return PCU_CR_PACKAGE_ENERGY_STATUS;
166 		}
167 		break;
168 	case REG_FAN_SPEED:
169 		if (channel == FAN_1)
170 			return BMG_FAN_1_SPEED;
171 		else if (channel == FAN_2)
172 			return BMG_FAN_2_SPEED;
173 		else if (channel == FAN_3)
174 			return BMG_FAN_3_SPEED;
175 		break;
176 	default:
177 		drm_warn(&xe->drm, "Unknown xe hwmon reg id: %d\n", hwmon_reg);
178 		break;
179 	}
180 
181 	return XE_REG(0);
182 }
183 
184 #define PL1_DISABLE 0
185 
186 /*
187  * HW allows arbitrary PL1 limits to be set but silently clamps these values to
188  * "typical but not guaranteed" min/max values in REG_PKG_POWER_SKU. Follow the
189  * same pattern for sysfs, allow arbitrary PL1 limits to be set but display
190  * clamped values when read.
191  */
192 static void xe_hwmon_power_max_read(struct xe_hwmon *hwmon, int channel, long *value)
193 {
194 	u64 reg_val, min, max;
195 	struct xe_device *xe = hwmon->xe;
196 	struct xe_reg rapl_limit, pkg_power_sku;
197 	struct xe_mmio *mmio = xe_root_tile_mmio(xe);
198 
199 	rapl_limit = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
200 	pkg_power_sku = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU, channel);
201 
202 	/*
203 	 * Valid check of REG_PKG_RAPL_LIMIT is already done in xe_hwmon_power_is_visible.
204 	 * So not checking it again here.
205 	 */
206 	if (!xe_reg_is_valid(pkg_power_sku)) {
207 		drm_warn(&xe->drm, "pkg_power_sku invalid\n");
208 		*value = 0;
209 		return;
210 	}
211 
212 	mutex_lock(&hwmon->hwmon_lock);
213 
214 	reg_val = xe_mmio_read32(mmio, rapl_limit);
215 	/* Check if PL1 limit is disabled */
216 	if (!(reg_val & PKG_PWR_LIM_1_EN)) {
217 		*value = PL1_DISABLE;
218 		goto unlock;
219 	}
220 
221 	reg_val = REG_FIELD_GET(PKG_PWR_LIM_1, reg_val);
222 	*value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power);
223 
224 	reg_val = xe_mmio_read64_2x32(mmio, pkg_power_sku);
225 	min = REG_FIELD_GET(PKG_MIN_PWR, reg_val);
226 	min = mul_u64_u32_shr(min, SF_POWER, hwmon->scl_shift_power);
227 	max = REG_FIELD_GET(PKG_MAX_PWR, reg_val);
228 	max = mul_u64_u32_shr(max, SF_POWER, hwmon->scl_shift_power);
229 
230 	if (min && max)
231 		*value = clamp_t(u64, *value, min, max);
232 unlock:
233 	mutex_unlock(&hwmon->hwmon_lock);
234 }
235 
236 static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, int channel, long value)
237 {
238 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
239 	int ret = 0;
240 	u64 reg_val;
241 	struct xe_reg rapl_limit;
242 
243 	rapl_limit = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
244 
245 	mutex_lock(&hwmon->hwmon_lock);
246 
247 	/* Disable PL1 limit and verify, as limit cannot be disabled on all platforms */
248 	if (value == PL1_DISABLE) {
249 		reg_val = xe_mmio_rmw32(mmio, rapl_limit, PKG_PWR_LIM_1_EN, 0);
250 		reg_val = xe_mmio_read32(mmio, rapl_limit);
251 		if (reg_val & PKG_PWR_LIM_1_EN) {
252 			drm_warn(&hwmon->xe->drm, "PL1 disable is not supported!\n");
253 			ret = -EOPNOTSUPP;
254 		}
255 		goto unlock;
256 	}
257 
258 	/* Computation in 64-bits to avoid overflow. Round to nearest. */
259 	reg_val = DIV_ROUND_CLOSEST_ULL((u64)value << hwmon->scl_shift_power, SF_POWER);
260 	reg_val = PKG_PWR_LIM_1_EN | REG_FIELD_PREP(PKG_PWR_LIM_1, reg_val);
261 	reg_val = xe_mmio_rmw32(mmio, rapl_limit, PKG_PWR_LIM_1_EN | PKG_PWR_LIM_1, reg_val);
262 
263 unlock:
264 	mutex_unlock(&hwmon->hwmon_lock);
265 	return ret;
266 }
267 
268 static void xe_hwmon_power_rated_max_read(struct xe_hwmon *hwmon, int channel, long *value)
269 {
270 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
271 	struct xe_reg reg = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU, channel);
272 	u64 reg_val;
273 
274 	/*
275 	 * This sysfs file won't be visible if REG_PKG_POWER_SKU is invalid, so valid check
276 	 * for this register can be skipped.
277 	 * See xe_hwmon_power_is_visible.
278 	 */
279 	reg_val = xe_mmio_read32(mmio, reg);
280 	reg_val = REG_FIELD_GET(PKG_TDP, reg_val);
281 	*value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power);
282 }
283 
284 /*
285  * xe_hwmon_energy_get - Obtain energy value
286  *
287  * The underlying energy hardware register is 32-bits and is subject to
288  * overflow. How long before overflow? For example, with an example
289  * scaling bit shift of 14 bits (see register *PACKAGE_POWER_SKU_UNIT) and
290  * a power draw of 1000 watts, the 32-bit counter will overflow in
291  * approximately 4.36 minutes.
292  *
293  * Examples:
294  *    1 watt:  (2^32 >> 14) /    1 W / (60 * 60 * 24) secs/day -> 3 days
295  * 1000 watts: (2^32 >> 14) / 1000 W / 60             secs/min -> 4.36 minutes
296  *
297  * The function significantly increases overflow duration (from 4.36
298  * minutes) by accumulating the energy register into a 'long' as allowed by
299  * the hwmon API. Using x86_64 128 bit arithmetic (see mul_u64_u32_shr()),
300  * a 'long' of 63 bits, SF_ENERGY of 1e6 (~20 bits) and
301  * hwmon->scl_shift_energy of 14 bits we have 57 (63 - 20 + 14) bits before
302  * energyN_input overflows. This at 1000 W is an overflow duration of 278 years.
303  */
304 static void
305 xe_hwmon_energy_get(struct xe_hwmon *hwmon, int channel, long *energy)
306 {
307 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
308 	struct xe_hwmon_energy_info *ei = &hwmon->ei[channel];
309 	u64 reg_val;
310 
311 	reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_ENERGY_STATUS,
312 							channel));
313 
314 	if (reg_val >= ei->reg_val_prev)
315 		ei->accum_energy += reg_val - ei->reg_val_prev;
316 	else
317 		ei->accum_energy += UINT_MAX - ei->reg_val_prev + reg_val;
318 
319 	ei->reg_val_prev = reg_val;
320 
321 	*energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,
322 				  hwmon->scl_shift_energy);
323 }
324 
325 static ssize_t
326 xe_hwmon_power_max_interval_show(struct device *dev, struct device_attribute *attr,
327 				 char *buf)
328 {
329 	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
330 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
331 	u32 x, y, x_w = 2; /* 2 bits */
332 	u64 r, tau4, out;
333 	int sensor_index = to_sensor_dev_attr(attr)->index;
334 
335 	xe_pm_runtime_get(hwmon->xe);
336 
337 	mutex_lock(&hwmon->hwmon_lock);
338 
339 	r = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, sensor_index));
340 
341 	mutex_unlock(&hwmon->hwmon_lock);
342 
343 	xe_pm_runtime_put(hwmon->xe);
344 
345 	x = REG_FIELD_GET(PKG_PWR_LIM_1_TIME_X, r);
346 	y = REG_FIELD_GET(PKG_PWR_LIM_1_TIME_Y, r);
347 
348 	/*
349 	 * tau = 1.x * power(2,y), x = bits(23:22), y = bits(21:17)
350 	 *     = (4 | x) << (y - 2)
351 	 *
352 	 * Here (y - 2) ensures a 1.x fixed point representation of 1.x
353 	 * As x is 2 bits so 1.x can be 1.0, 1.25, 1.50, 1.75
354 	 *
355 	 * As y can be < 2, we compute tau4 = (4 | x) << y
356 	 * and then add 2 when doing the final right shift to account for units
357 	 */
358 	tau4 = (u64)((1 << x_w) | x) << y;
359 
360 	/* val in hwmon interface units (millisec) */
361 	out = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
362 
363 	return sysfs_emit(buf, "%llu\n", out);
364 }
365 
366 static ssize_t
367 xe_hwmon_power_max_interval_store(struct device *dev, struct device_attribute *attr,
368 				  const char *buf, size_t count)
369 {
370 	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
371 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
372 	u32 x, y, rxy, x_w = 2; /* 2 bits */
373 	u64 tau4, r, max_win;
374 	unsigned long val;
375 	int ret;
376 	int sensor_index = to_sensor_dev_attr(attr)->index;
377 
378 	ret = kstrtoul(buf, 0, &val);
379 	if (ret)
380 		return ret;
381 
382 	/*
383 	 * Max HW supported tau in '1.x * power(2,y)' format, x = 0, y = 0x12.
384 	 * The hwmon->scl_shift_time default of 0xa results in a max tau of 256 seconds.
385 	 *
386 	 * The ideal scenario is for PKG_MAX_WIN to be read from the PKG_PWR_SKU register.
387 	 * However, it is observed that existing discrete GPUs does not provide correct
388 	 * PKG_MAX_WIN value, therefore a using default constant value. For future discrete GPUs
389 	 * this may get resolved, in which case PKG_MAX_WIN should be obtained from PKG_PWR_SKU.
390 	 */
391 #define PKG_MAX_WIN_DEFAULT 0x12ull
392 
393 	/*
394 	 * val must be < max in hwmon interface units. The steps below are
395 	 * explained in xe_hwmon_power_max_interval_show()
396 	 */
397 	r = FIELD_PREP(PKG_MAX_WIN, PKG_MAX_WIN_DEFAULT);
398 	x = REG_FIELD_GET(PKG_MAX_WIN_X, r);
399 	y = REG_FIELD_GET(PKG_MAX_WIN_Y, r);
400 	tau4 = (u64)((1 << x_w) | x) << y;
401 	max_win = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
402 
403 	if (val > max_win)
404 		return -EINVAL;
405 
406 	/* val in hw units */
407 	val = DIV_ROUND_CLOSEST_ULL((u64)val << hwmon->scl_shift_time, SF_TIME);
408 
409 	/*
410 	 * Convert val to 1.x * power(2,y)
411 	 * y = ilog2(val)
412 	 * x = (val - (1 << y)) >> (y - 2)
413 	 */
414 	if (!val) {
415 		y = 0;
416 		x = 0;
417 	} else {
418 		y = ilog2(val);
419 		x = (val - (1ul << y)) << x_w >> y;
420 	}
421 
422 	rxy = REG_FIELD_PREP(PKG_PWR_LIM_1_TIME_X, x) | REG_FIELD_PREP(PKG_PWR_LIM_1_TIME_Y, y);
423 
424 	xe_pm_runtime_get(hwmon->xe);
425 
426 	mutex_lock(&hwmon->hwmon_lock);
427 
428 	r = xe_mmio_rmw32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, sensor_index),
429 			  PKG_PWR_LIM_1_TIME, rxy);
430 
431 	mutex_unlock(&hwmon->hwmon_lock);
432 
433 	xe_pm_runtime_put(hwmon->xe);
434 
435 	return count;
436 }
437 
438 static SENSOR_DEVICE_ATTR(power1_max_interval, 0664,
439 			  xe_hwmon_power_max_interval_show,
440 			  xe_hwmon_power_max_interval_store, CHANNEL_CARD);
441 
442 static SENSOR_DEVICE_ATTR(power2_max_interval, 0664,
443 			  xe_hwmon_power_max_interval_show,
444 			  xe_hwmon_power_max_interval_store, CHANNEL_PKG);
445 
446 static struct attribute *hwmon_attributes[] = {
447 	&sensor_dev_attr_power1_max_interval.dev_attr.attr,
448 	&sensor_dev_attr_power2_max_interval.dev_attr.attr,
449 	NULL
450 };
451 
452 static umode_t xe_hwmon_attributes_visible(struct kobject *kobj,
453 					   struct attribute *attr, int index)
454 {
455 	struct device *dev = kobj_to_dev(kobj);
456 	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
457 	int ret = 0;
458 
459 	xe_pm_runtime_get(hwmon->xe);
460 
461 	ret = xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, index)) ? attr->mode : 0;
462 
463 	xe_pm_runtime_put(hwmon->xe);
464 
465 	return ret;
466 }
467 
468 static const struct attribute_group hwmon_attrgroup = {
469 	.attrs = hwmon_attributes,
470 	.is_visible = xe_hwmon_attributes_visible,
471 };
472 
473 static const struct attribute_group *hwmon_groups[] = {
474 	&hwmon_attrgroup,
475 	NULL
476 };
477 
478 static const struct hwmon_channel_info * const hwmon_info[] = {
479 	HWMON_CHANNEL_INFO(temp, HWMON_T_LABEL, HWMON_T_INPUT | HWMON_T_LABEL,
480 			   HWMON_T_INPUT | HWMON_T_LABEL),
481 	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_LABEL,
482 			   HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT | HWMON_P_LABEL),
483 	HWMON_CHANNEL_INFO(curr, HWMON_C_LABEL, HWMON_C_CRIT | HWMON_C_LABEL),
484 	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL),
485 	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL),
486 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT),
487 	NULL
488 };
489 
490 /* I1 is exposed as power_crit or as curr_crit depending on bit 31 */
491 static int xe_hwmon_pcode_read_i1(const struct xe_hwmon *hwmon, u32 *uval)
492 {
493 	struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
494 
495 	/* Avoid Illegal Subcommand error */
496 	if (hwmon->xe->info.platform == XE_DG2)
497 		return -ENXIO;
498 
499 	return xe_pcode_read(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
500 			     POWER_SETUP_SUBCOMMAND_READ_I1, 0),
501 			     uval, NULL);
502 }
503 
504 static int xe_hwmon_pcode_write_i1(const struct xe_hwmon *hwmon, u32 uval)
505 {
506 	struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
507 
508 	return xe_pcode_write(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
509 			      POWER_SETUP_SUBCOMMAND_WRITE_I1, 0),
510 			      (uval & POWER_SETUP_I1_DATA_MASK));
511 }
512 
513 static int xe_hwmon_pcode_read_fan_control(const struct xe_hwmon *hwmon, u32 subcmd, u32 *uval)
514 {
515 	struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
516 
517 	/* Platforms that don't return correct value */
518 	if (hwmon->xe->info.platform == XE_DG2 && subcmd == FSC_READ_NUM_FANS) {
519 		*uval = 2;
520 		return 0;
521 	}
522 
523 	return xe_pcode_read(root_tile, PCODE_MBOX(FAN_SPEED_CONTROL, subcmd, 0), uval, NULL);
524 }
525 
526 static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, int channel,
527 					 long *value, u32 scale_factor)
528 {
529 	int ret;
530 	u32 uval;
531 
532 	mutex_lock(&hwmon->hwmon_lock);
533 
534 	ret = xe_hwmon_pcode_read_i1(hwmon, &uval);
535 	if (ret)
536 		goto unlock;
537 
538 	*value = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
539 				 scale_factor, POWER_SETUP_I1_SHIFT);
540 unlock:
541 	mutex_unlock(&hwmon->hwmon_lock);
542 	return ret;
543 }
544 
545 static int xe_hwmon_power_curr_crit_write(struct xe_hwmon *hwmon, int channel,
546 					  long value, u32 scale_factor)
547 {
548 	int ret;
549 	u32 uval;
550 
551 	mutex_lock(&hwmon->hwmon_lock);
552 
553 	uval = DIV_ROUND_CLOSEST_ULL(value << POWER_SETUP_I1_SHIFT, scale_factor);
554 	ret = xe_hwmon_pcode_write_i1(hwmon, uval);
555 
556 	mutex_unlock(&hwmon->hwmon_lock);
557 	return ret;
558 }
559 
560 static void xe_hwmon_get_voltage(struct xe_hwmon *hwmon, int channel, long *value)
561 {
562 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
563 	u64 reg_val;
564 
565 	reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_GT_PERF_STATUS, channel));
566 	/* HW register value in units of 2.5 millivolt */
567 	*value = DIV_ROUND_CLOSEST(REG_FIELD_GET(VOLTAGE_MASK, reg_val) * 2500, SF_VOLTAGE);
568 }
569 
570 static umode_t
571 xe_hwmon_temp_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
572 {
573 	switch (attr) {
574 	case hwmon_temp_input:
575 	case hwmon_temp_label:
576 		return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_TEMP, channel)) ? 0444 : 0;
577 	default:
578 		return 0;
579 	}
580 }
581 
582 static int
583 xe_hwmon_temp_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
584 {
585 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
586 	u64 reg_val;
587 
588 	switch (attr) {
589 	case hwmon_temp_input:
590 		reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_TEMP, channel));
591 
592 		/* HW register value is in degrees Celsius, convert to millidegrees. */
593 		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
594 		return 0;
595 	default:
596 		return -EOPNOTSUPP;
597 	}
598 }
599 
600 static umode_t
601 xe_hwmon_power_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
602 {
603 	u32 uval;
604 
605 	switch (attr) {
606 	case hwmon_power_max:
607 		return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT,
608 				       channel)) ? 0664 : 0;
609 	case hwmon_power_rated_max:
610 		return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU,
611 				       channel)) ? 0444 : 0;
612 	case hwmon_power_crit:
613 		if (channel == CHANNEL_PKG)
614 			return (xe_hwmon_pcode_read_i1(hwmon, &uval) ||
615 				!(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
616 		break;
617 	case hwmon_power_label:
618 		return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU_UNIT,
619 				       channel)) ? 0444 : 0;
620 	default:
621 		return 0;
622 	}
623 	return 0;
624 }
625 
626 static int
627 xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
628 {
629 	switch (attr) {
630 	case hwmon_power_max:
631 		xe_hwmon_power_max_read(hwmon, channel, val);
632 		return 0;
633 	case hwmon_power_rated_max:
634 		xe_hwmon_power_rated_max_read(hwmon, channel, val);
635 		return 0;
636 	case hwmon_power_crit:
637 		return xe_hwmon_power_curr_crit_read(hwmon, channel, val, SF_POWER);
638 	default:
639 		return -EOPNOTSUPP;
640 	}
641 }
642 
643 static int
644 xe_hwmon_power_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
645 {
646 	switch (attr) {
647 	case hwmon_power_max:
648 		return xe_hwmon_power_max_write(hwmon, channel, val);
649 	case hwmon_power_crit:
650 		return xe_hwmon_power_curr_crit_write(hwmon, channel, val, SF_POWER);
651 	default:
652 		return -EOPNOTSUPP;
653 	}
654 }
655 
656 static umode_t
657 xe_hwmon_curr_is_visible(const struct xe_hwmon *hwmon, u32 attr, int channel)
658 {
659 	u32 uval;
660 
661 	/* hwmon sysfs attribute of current available only for package */
662 	if (channel != CHANNEL_PKG)
663 		return 0;
664 
665 	switch (attr) {
666 	case hwmon_curr_crit:
667 			return (xe_hwmon_pcode_read_i1(hwmon, &uval) ||
668 				(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
669 	case hwmon_curr_label:
670 			return (xe_hwmon_pcode_read_i1(hwmon, &uval) ||
671 				(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0444;
672 		break;
673 	default:
674 		return 0;
675 	}
676 	return 0;
677 }
678 
679 static int
680 xe_hwmon_curr_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
681 {
682 	switch (attr) {
683 	case hwmon_curr_crit:
684 		return xe_hwmon_power_curr_crit_read(hwmon, channel, val, SF_CURR);
685 	default:
686 		return -EOPNOTSUPP;
687 	}
688 }
689 
690 static int
691 xe_hwmon_curr_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
692 {
693 	switch (attr) {
694 	case hwmon_curr_crit:
695 		return xe_hwmon_power_curr_crit_write(hwmon, channel, val, SF_CURR);
696 	default:
697 		return -EOPNOTSUPP;
698 	}
699 }
700 
701 static umode_t
702 xe_hwmon_in_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
703 {
704 	switch (attr) {
705 	case hwmon_in_input:
706 	case hwmon_in_label:
707 		return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_GT_PERF_STATUS,
708 				       channel)) ? 0444 : 0;
709 	default:
710 		return 0;
711 	}
712 }
713 
714 static int
715 xe_hwmon_in_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
716 {
717 	switch (attr) {
718 	case hwmon_in_input:
719 		xe_hwmon_get_voltage(hwmon, channel, val);
720 		return 0;
721 	default:
722 		return -EOPNOTSUPP;
723 	}
724 }
725 
726 static umode_t
727 xe_hwmon_energy_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
728 {
729 	switch (attr) {
730 	case hwmon_energy_input:
731 	case hwmon_energy_label:
732 		return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_PKG_ENERGY_STATUS,
733 				       channel)) ? 0444 : 0;
734 	default:
735 		return 0;
736 	}
737 }
738 
739 static int
740 xe_hwmon_energy_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
741 {
742 	switch (attr) {
743 	case hwmon_energy_input:
744 		xe_hwmon_energy_get(hwmon, channel, val);
745 		return 0;
746 	default:
747 		return -EOPNOTSUPP;
748 	}
749 }
750 
751 static umode_t
752 xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
753 {
754 	u32 uval;
755 
756 	if (!hwmon->xe->info.has_fan_control)
757 		return 0;
758 
759 	switch (attr) {
760 	case hwmon_fan_input:
761 		if (xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_NUM_FANS, &uval))
762 			return 0;
763 
764 		return channel < uval ? 0444 : 0;
765 	default:
766 		return 0;
767 	}
768 }
769 
770 static int
771 xe_hwmon_fan_input_read(struct xe_hwmon *hwmon, int channel, long *val)
772 {
773 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
774 	struct xe_hwmon_fan_info *fi = &hwmon->fi[channel];
775 	u64 rotations, time_now, time;
776 	u32 reg_val;
777 	int ret = 0;
778 
779 	mutex_lock(&hwmon->hwmon_lock);
780 
781 	reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_FAN_SPEED, channel));
782 	time_now = get_jiffies_64();
783 
784 	/*
785 	 * HW register value is accumulated count of pulses from PWM fan with the scale
786 	 * of 2 pulses per rotation.
787 	 */
788 	rotations = (reg_val - fi->reg_val_prev) / 2;
789 
790 	time = jiffies_delta_to_msecs(time_now - fi->time_prev);
791 	if (unlikely(!time)) {
792 		ret = -EAGAIN;
793 		goto unlock;
794 	}
795 
796 	/*
797 	 * Calculate fan speed in RPM by time averaging two subsequent readings in minutes.
798 	 * RPM = number of rotations * msecs per minute / time in msecs
799 	 */
800 	*val = DIV_ROUND_UP_ULL(rotations * (MSEC_PER_SEC * 60), time);
801 
802 	fi->reg_val_prev = reg_val;
803 	fi->time_prev = time_now;
804 unlock:
805 	mutex_unlock(&hwmon->hwmon_lock);
806 	return ret;
807 }
808 
809 static int
810 xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
811 {
812 	switch (attr) {
813 	case hwmon_fan_input:
814 		return xe_hwmon_fan_input_read(hwmon, channel, val);
815 	default:
816 		return -EOPNOTSUPP;
817 	}
818 }
819 
820 static umode_t
821 xe_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
822 		    u32 attr, int channel)
823 {
824 	struct xe_hwmon *hwmon = (struct xe_hwmon *)drvdata;
825 	int ret;
826 
827 	xe_pm_runtime_get(hwmon->xe);
828 
829 	switch (type) {
830 	case hwmon_temp:
831 		ret = xe_hwmon_temp_is_visible(hwmon, attr, channel);
832 		break;
833 	case hwmon_power:
834 		ret = xe_hwmon_power_is_visible(hwmon, attr, channel);
835 		break;
836 	case hwmon_curr:
837 		ret = xe_hwmon_curr_is_visible(hwmon, attr, channel);
838 		break;
839 	case hwmon_in:
840 		ret = xe_hwmon_in_is_visible(hwmon, attr, channel);
841 		break;
842 	case hwmon_energy:
843 		ret = xe_hwmon_energy_is_visible(hwmon, attr, channel);
844 		break;
845 	case hwmon_fan:
846 		ret = xe_hwmon_fan_is_visible(hwmon, attr, channel);
847 		break;
848 	default:
849 		ret = 0;
850 		break;
851 	}
852 
853 	xe_pm_runtime_put(hwmon->xe);
854 
855 	return ret;
856 }
857 
858 static int
859 xe_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
860 	      int channel, long *val)
861 {
862 	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
863 	int ret;
864 
865 	xe_pm_runtime_get(hwmon->xe);
866 
867 	switch (type) {
868 	case hwmon_temp:
869 		ret = xe_hwmon_temp_read(hwmon, attr, channel, val);
870 		break;
871 	case hwmon_power:
872 		ret = xe_hwmon_power_read(hwmon, attr, channel, val);
873 		break;
874 	case hwmon_curr:
875 		ret = xe_hwmon_curr_read(hwmon, attr, channel, val);
876 		break;
877 	case hwmon_in:
878 		ret = xe_hwmon_in_read(hwmon, attr, channel, val);
879 		break;
880 	case hwmon_energy:
881 		ret = xe_hwmon_energy_read(hwmon, attr, channel, val);
882 		break;
883 	case hwmon_fan:
884 		ret = xe_hwmon_fan_read(hwmon, attr, channel, val);
885 		break;
886 	default:
887 		ret = -EOPNOTSUPP;
888 		break;
889 	}
890 
891 	xe_pm_runtime_put(hwmon->xe);
892 
893 	return ret;
894 }
895 
896 static int
897 xe_hwmon_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
898 	       int channel, long val)
899 {
900 	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
901 	int ret;
902 
903 	xe_pm_runtime_get(hwmon->xe);
904 
905 	switch (type) {
906 	case hwmon_power:
907 		ret = xe_hwmon_power_write(hwmon, attr, channel, val);
908 		break;
909 	case hwmon_curr:
910 		ret = xe_hwmon_curr_write(hwmon, attr, channel, val);
911 		break;
912 	default:
913 		ret = -EOPNOTSUPP;
914 		break;
915 	}
916 
917 	xe_pm_runtime_put(hwmon->xe);
918 
919 	return ret;
920 }
921 
922 static int xe_hwmon_read_label(struct device *dev,
923 			       enum hwmon_sensor_types type,
924 			       u32 attr, int channel, const char **str)
925 {
926 	switch (type) {
927 	case hwmon_temp:
928 		if (channel == CHANNEL_PKG)
929 			*str = "pkg";
930 		else if (channel == CHANNEL_VRAM)
931 			*str = "vram";
932 		return 0;
933 	case hwmon_power:
934 	case hwmon_energy:
935 	case hwmon_curr:
936 	case hwmon_in:
937 		if (channel == CHANNEL_CARD)
938 			*str = "card";
939 		else if (channel == CHANNEL_PKG)
940 			*str = "pkg";
941 		return 0;
942 	default:
943 		return -EOPNOTSUPP;
944 	}
945 }
946 
947 static const struct hwmon_ops hwmon_ops = {
948 	.is_visible = xe_hwmon_is_visible,
949 	.read = xe_hwmon_read,
950 	.write = xe_hwmon_write,
951 	.read_string = xe_hwmon_read_label,
952 };
953 
954 static const struct hwmon_chip_info hwmon_chip_info = {
955 	.ops = &hwmon_ops,
956 	.info = hwmon_info,
957 };
958 
959 static void
960 xe_hwmon_get_preregistration_info(struct xe_hwmon *hwmon)
961 {
962 	struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
963 	long energy, fan_speed;
964 	u64 val_sku_unit = 0;
965 	int channel;
966 	struct xe_reg pkg_power_sku_unit;
967 
968 	/*
969 	 * The contents of register PKG_POWER_SKU_UNIT do not change,
970 	 * so read it once and store the shift values.
971 	 */
972 	pkg_power_sku_unit = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU_UNIT, 0);
973 	if (xe_reg_is_valid(pkg_power_sku_unit)) {
974 		val_sku_unit = xe_mmio_read32(mmio, pkg_power_sku_unit);
975 		hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit);
976 		hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit);
977 		hwmon->scl_shift_time = REG_FIELD_GET(PKG_TIME_UNIT, val_sku_unit);
978 	}
979 
980 	/*
981 	 * Initialize 'struct xe_hwmon_energy_info', i.e. set fields to the
982 	 * first value of the energy register read
983 	 */
984 	for (channel = 0; channel < CHANNEL_MAX; channel++)
985 		if (xe_hwmon_is_visible(hwmon, hwmon_energy, hwmon_energy_input, channel))
986 			xe_hwmon_energy_get(hwmon, channel, &energy);
987 
988 	/* Initialize 'struct xe_hwmon_fan_info' with initial fan register reading. */
989 	for (channel = 0; channel < FAN_MAX; channel++)
990 		if (xe_hwmon_is_visible(hwmon, hwmon_fan, hwmon_fan_input, channel))
991 			xe_hwmon_fan_input_read(hwmon, channel, &fan_speed);
992 }
993 
994 static void xe_hwmon_mutex_destroy(void *arg)
995 {
996 	struct xe_hwmon *hwmon = arg;
997 
998 	mutex_destroy(&hwmon->hwmon_lock);
999 }
1000 
1001 int xe_hwmon_register(struct xe_device *xe)
1002 {
1003 	struct device *dev = xe->drm.dev;
1004 	struct xe_hwmon *hwmon;
1005 	int ret;
1006 
1007 	/* hwmon is available only for dGfx */
1008 	if (!IS_DGFX(xe))
1009 		return 0;
1010 
1011 	/* hwmon is not available on VFs */
1012 	if (IS_SRIOV_VF(xe))
1013 		return 0;
1014 
1015 	hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
1016 	if (!hwmon)
1017 		return -ENOMEM;
1018 
1019 	mutex_init(&hwmon->hwmon_lock);
1020 	ret = devm_add_action_or_reset(dev, xe_hwmon_mutex_destroy, hwmon);
1021 	if (ret)
1022 		return ret;
1023 
1024 	/* There's only one instance of hwmon per device */
1025 	hwmon->xe = xe;
1026 	xe->hwmon = hwmon;
1027 
1028 	xe_hwmon_get_preregistration_info(hwmon);
1029 
1030 	drm_dbg(&xe->drm, "Register xe hwmon interface\n");
1031 
1032 	/*  hwmon_dev points to device hwmon<i> */
1033 	hwmon->hwmon_dev = devm_hwmon_device_register_with_info(dev, "xe", hwmon,
1034 								&hwmon_chip_info,
1035 								hwmon_groups);
1036 	if (IS_ERR(hwmon->hwmon_dev)) {
1037 		drm_err(&xe->drm, "Failed to register xe hwmon (%pe)\n", hwmon->hwmon_dev);
1038 		xe->hwmon = NULL;
1039 		return PTR_ERR(hwmon->hwmon_dev);
1040 	}
1041 
1042 	return 0;
1043 }
1044 
1045