1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
6 #include <linux/hwmon.h>
7 #include <linux/hwmon-sysfs.h>
8 #include <linux/types.h>
11 #include "i915_hwmon.h"
13 #include "intel_mchbar_regs.h"
14 #include "intel_pcode.h"
15 #include "gt/intel_gt.h"
16 #include "gt/intel_gt_regs.h"
19 * SF_* - scale factors for particular quantities according to hwmon spec.
20 * - voltage - millivolts
21 * - power - microwatts
22 * - curr - milliamperes
23 * - energy - microjoules
24 * - time - milliseconds
26 #define SF_VOLTAGE 1000
27 #define SF_POWER 1000000
29 #define SF_ENERGY 1000000
33 i915_reg_t gt_perf_status;
34 i915_reg_t pkg_power_sku_unit;
35 i915_reg_t pkg_power_sku;
36 i915_reg_t pkg_rapl_limit;
37 i915_reg_t energy_status_all;
38 i915_reg_t energy_status_tile;
41 struct hwm_energy_info {
43 long accum_energy; /* Accumulated energy for energy1_input */
47 struct i915_hwmon *hwmon;
48 struct intel_uncore *uncore;
49 struct device *hwmon_dev;
50 struct hwm_energy_info ei; /* Energy info for energy1_input */
56 struct hwm_drvdata ddat;
57 struct hwm_drvdata ddat_gt[I915_MAX_GT];
58 struct mutex hwmon_lock; /* counter overflow logic and rmw */
66 hwm_locked_with_pm_intel_uncore_rmw(struct hwm_drvdata *ddat,
67 i915_reg_t reg, u32 clear, u32 set)
69 struct i915_hwmon *hwmon = ddat->hwmon;
70 struct intel_uncore *uncore = ddat->uncore;
71 intel_wakeref_t wakeref;
73 mutex_lock(&hwmon->hwmon_lock);
75 with_intel_runtime_pm(uncore->rpm, wakeref)
76 intel_uncore_rmw(uncore, reg, clear, set);
78 mutex_unlock(&hwmon->hwmon_lock);
82 * This function's return type of u64 allows for the case where the scaling
83 * of the field taken from the 32-bit register value might cause a result to
87 hwm_field_read_and_scale(struct hwm_drvdata *ddat, i915_reg_t rgadr,
88 u32 field_msk, int nshift, u32 scale_factor)
90 struct intel_uncore *uncore = ddat->uncore;
91 intel_wakeref_t wakeref;
94 with_intel_runtime_pm(uncore->rpm, wakeref)
95 reg_value = intel_uncore_read(uncore, rgadr);
97 reg_value = REG_FIELD_GET(field_msk, reg_value);
99 return mul_u64_u32_shr(reg_value, scale_factor, nshift);
103 * hwm_energy - Obtain energy value
105 * The underlying energy hardware register is 32-bits and is subject to
106 * overflow. How long before overflow? For example, with an example
107 * scaling bit shift of 14 bits (see register *PACKAGE_POWER_SKU_UNIT) and
108 * a power draw of 1000 watts, the 32-bit counter will overflow in
109 * approximately 4.36 minutes.
112 * 1 watt: (2^32 >> 14) / 1 W / (60 * 60 * 24) secs/day -> 3 days
113 * 1000 watts: (2^32 >> 14) / 1000 W / 60 secs/min -> 4.36 minutes
115 * The function significantly increases overflow duration (from 4.36
116 * minutes) by accumulating the energy register into a 'long' as allowed by
117 * the hwmon API. Using x86_64 128 bit arithmetic (see mul_u64_u32_shr()),
118 * a 'long' of 63 bits, SF_ENERGY of 1e6 (~20 bits) and
119 * hwmon->scl_shift_energy of 14 bits we have 57 (63 - 20 + 14) bits before
120 * energy1_input overflows. This at 1000 W is an overflow duration of 278 years.
123 hwm_energy(struct hwm_drvdata *ddat, long *energy)
125 struct intel_uncore *uncore = ddat->uncore;
126 struct i915_hwmon *hwmon = ddat->hwmon;
127 struct hwm_energy_info *ei = &ddat->ei;
128 intel_wakeref_t wakeref;
133 rgaddr = hwmon->rg.energy_status_tile;
135 rgaddr = hwmon->rg.energy_status_all;
137 mutex_lock(&hwmon->hwmon_lock);
139 with_intel_runtime_pm(uncore->rpm, wakeref)
140 reg_val = intel_uncore_read(uncore, rgaddr);
142 if (reg_val >= ei->reg_val_prev)
143 ei->accum_energy += reg_val - ei->reg_val_prev;
145 ei->accum_energy += UINT_MAX - ei->reg_val_prev + reg_val;
146 ei->reg_val_prev = reg_val;
148 *energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,
149 hwmon->scl_shift_energy);
150 mutex_unlock(&hwmon->hwmon_lock);
154 hwm_power1_max_interval_show(struct device *dev, struct device_attribute *attr,
157 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
158 struct i915_hwmon *hwmon = ddat->hwmon;
159 intel_wakeref_t wakeref;
160 u32 r, x, y, x_w = 2; /* 2 bits */
163 with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
164 r = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_rapl_limit);
166 x = REG_FIELD_GET(PKG_PWR_LIM_1_TIME_X, r);
167 y = REG_FIELD_GET(PKG_PWR_LIM_1_TIME_Y, r);
169 * tau = 1.x * power(2,y), x = bits(23:22), y = bits(21:17)
170 * = (4 | x) << (y - 2)
171 * where (y - 2) ensures a 1.x fixed point representation of 1.x
172 * However because y can be < 2, we compute
173 * tau4 = (4 | x) << y
174 * but add 2 when doing the final right shift to account for units
176 tau4 = ((1 << x_w) | x) << y;
177 /* val in hwmon interface units (millisec) */
178 out = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
180 return sysfs_emit(buf, "%llu\n", out);
184 hwm_power1_max_interval_store(struct device *dev,
185 struct device_attribute *attr,
186 const char *buf, size_t count)
188 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
189 struct i915_hwmon *hwmon = ddat->hwmon;
190 u32 x, y, rxy, x_w = 2; /* 2 bits */
191 u64 tau4, r, max_win;
195 ret = kstrtoul(buf, 0, &val);
200 * Max HW supported tau in '1.x * power(2,y)' format, x = 0, y = 0x12
201 * The hwmon->scl_shift_time default of 0xa results in a max tau of 256 seconds
203 #define PKG_MAX_WIN_DEFAULT 0x12ull
206 * val must be < max in hwmon interface units. The steps below are
207 * explained in i915_power1_max_interval_show()
209 r = FIELD_PREP(PKG_MAX_WIN, PKG_MAX_WIN_DEFAULT);
210 x = REG_FIELD_GET(PKG_MAX_WIN_X, r);
211 y = REG_FIELD_GET(PKG_MAX_WIN_Y, r);
212 tau4 = ((1 << x_w) | x) << y;
213 max_win = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
218 /* val in hw units */
219 val = DIV_ROUND_CLOSEST_ULL((u64)val << hwmon->scl_shift_time, SF_TIME);
220 /* Convert to 1.x * power(2,y) */
227 /* x = (val - (1 << y)) >> (y - 2); */
228 x = (val - (1ul << y)) << x_w >> y;
231 rxy = REG_FIELD_PREP(PKG_PWR_LIM_1_TIME_X, x) | REG_FIELD_PREP(PKG_PWR_LIM_1_TIME_Y, y);
233 hwm_locked_with_pm_intel_uncore_rmw(ddat, hwmon->rg.pkg_rapl_limit,
234 PKG_PWR_LIM_1_TIME, rxy);
238 static SENSOR_DEVICE_ATTR(power1_max_interval, 0664,
239 hwm_power1_max_interval_show,
240 hwm_power1_max_interval_store, 0);
242 static struct attribute *hwm_attributes[] = {
243 &sensor_dev_attr_power1_max_interval.dev_attr.attr,
247 static umode_t hwm_attributes_visible(struct kobject *kobj,
248 struct attribute *attr, int index)
250 struct device *dev = kobj_to_dev(kobj);
251 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
252 struct i915_hwmon *hwmon = ddat->hwmon;
254 if (attr == &sensor_dev_attr_power1_max_interval.dev_attr.attr)
255 return i915_mmio_reg_valid(hwmon->rg.pkg_rapl_limit) ? attr->mode : 0;
260 static const struct attribute_group hwm_attrgroup = {
261 .attrs = hwm_attributes,
262 .is_visible = hwm_attributes_visible,
265 static const struct attribute_group *hwm_groups[] = {
270 static const struct hwmon_channel_info *hwm_info[] = {
271 HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
272 HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
273 HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
274 HWMON_CHANNEL_INFO(curr, HWMON_C_CRIT),
278 static const struct hwmon_channel_info *hwm_gt_info[] = {
279 HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
283 /* I1 is exposed as power_crit or as curr_crit depending on bit 31 */
284 static int hwm_pcode_read_i1(struct drm_i915_private *i915, u32 *uval)
286 /* Avoid ILLEGAL_SUBCOMMAND "mailbox access failed" warning in snb_pcode_read */
287 if (IS_DG1(i915) || IS_DG2(i915))
290 return snb_pcode_read_p(&i915->uncore, PCODE_POWER_SETUP,
291 POWER_SETUP_SUBCOMMAND_READ_I1, 0, uval);
294 static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
296 return snb_pcode_write_p(&i915->uncore, PCODE_POWER_SETUP,
297 POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
301 hwm_in_is_visible(const struct hwm_drvdata *ddat, u32 attr)
303 struct drm_i915_private *i915 = ddat->uncore->i915;
307 return IS_DG1(i915) || IS_DG2(i915) ? 0444 : 0;
314 hwm_in_read(struct hwm_drvdata *ddat, u32 attr, long *val)
316 struct i915_hwmon *hwmon = ddat->hwmon;
317 intel_wakeref_t wakeref;
322 with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
323 reg_value = intel_uncore_read(ddat->uncore, hwmon->rg.gt_perf_status);
324 /* HW register value in units of 2.5 millivolt */
325 *val = DIV_ROUND_CLOSEST(REG_FIELD_GET(GEN12_VOLTAGE_MASK, reg_value) * 25, 10);
333 hwm_power_is_visible(const struct hwm_drvdata *ddat, u32 attr, int chan)
335 struct drm_i915_private *i915 = ddat->uncore->i915;
336 struct i915_hwmon *hwmon = ddat->hwmon;
340 case hwmon_power_max:
341 return i915_mmio_reg_valid(hwmon->rg.pkg_rapl_limit) ? 0664 : 0;
342 case hwmon_power_rated_max:
343 return i915_mmio_reg_valid(hwmon->rg.pkg_power_sku) ? 0444 : 0;
344 case hwmon_power_crit:
345 return (hwm_pcode_read_i1(i915, &uval) ||
346 !(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
352 #define PL1_DISABLE 0
355 * HW allows arbitrary PL1 limits to be set but silently clamps these values to
356 * "typical but not guaranteed" min/max values in rg.pkg_power_sku. Follow the
357 * same pattern for sysfs, allow arbitrary PL1 limits to be set but display
358 * clamped values when read. Write/read I1 also follows the same pattern.
361 hwm_power_max_read(struct hwm_drvdata *ddat, long *val)
363 struct i915_hwmon *hwmon = ddat->hwmon;
364 intel_wakeref_t wakeref;
367 /* Check if PL1 limit is disabled */
368 with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
369 r = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_rapl_limit);
370 if (!(r & PKG_PWR_LIM_1_EN)) {
375 *val = hwm_field_read_and_scale(ddat,
376 hwmon->rg.pkg_rapl_limit,
378 hwmon->scl_shift_power,
381 with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
382 r = intel_uncore_read64(ddat->uncore, hwmon->rg.pkg_power_sku);
383 min = REG_FIELD_GET(PKG_MIN_PWR, r);
384 min = mul_u64_u32_shr(min, SF_POWER, hwmon->scl_shift_power);
385 max = REG_FIELD_GET(PKG_MAX_PWR, r);
386 max = mul_u64_u32_shr(max, SF_POWER, hwmon->scl_shift_power);
389 *val = clamp_t(u64, *val, min, max);
395 hwm_power_max_write(struct hwm_drvdata *ddat, long val)
397 struct i915_hwmon *hwmon = ddat->hwmon;
398 intel_wakeref_t wakeref;
402 mutex_lock(&hwmon->hwmon_lock);
403 wakeref = intel_runtime_pm_get(ddat->uncore->rpm);
405 /* Disable PL1 limit and verify, because the limit cannot be disabled on all platforms */
406 if (val == PL1_DISABLE) {
407 intel_uncore_rmw(ddat->uncore, hwmon->rg.pkg_rapl_limit,
408 PKG_PWR_LIM_1_EN, 0);
409 nval = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_rapl_limit);
411 if (nval & PKG_PWR_LIM_1_EN)
416 /* Computation in 64-bits to avoid overflow. Round to nearest. */
417 nval = DIV_ROUND_CLOSEST_ULL((u64)val << hwmon->scl_shift_power, SF_POWER);
418 nval = PKG_PWR_LIM_1_EN | REG_FIELD_PREP(PKG_PWR_LIM_1, nval);
420 intel_uncore_rmw(ddat->uncore, hwmon->rg.pkg_rapl_limit,
421 PKG_PWR_LIM_1_EN | PKG_PWR_LIM_1, nval);
423 intel_runtime_pm_put(ddat->uncore->rpm, wakeref);
424 mutex_unlock(&hwmon->hwmon_lock);
429 hwm_power_read(struct hwm_drvdata *ddat, u32 attr, int chan, long *val)
431 struct i915_hwmon *hwmon = ddat->hwmon;
436 case hwmon_power_max:
437 return hwm_power_max_read(ddat, val);
438 case hwmon_power_rated_max:
439 *val = hwm_field_read_and_scale(ddat,
440 hwmon->rg.pkg_power_sku,
442 hwmon->scl_shift_power,
445 case hwmon_power_crit:
446 ret = hwm_pcode_read_i1(ddat->uncore->i915, &uval);
449 if (!(uval & POWER_SETUP_I1_WATTS))
451 *val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
452 SF_POWER, POWER_SETUP_I1_SHIFT);
460 hwm_power_write(struct hwm_drvdata *ddat, u32 attr, int chan, long val)
465 case hwmon_power_max:
466 return hwm_power_max_write(ddat, val);
467 case hwmon_power_crit:
468 uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_POWER);
469 return hwm_pcode_write_i1(ddat->uncore->i915, uval);
476 hwm_energy_is_visible(const struct hwm_drvdata *ddat, u32 attr)
478 struct i915_hwmon *hwmon = ddat->hwmon;
482 case hwmon_energy_input:
484 rgaddr = hwmon->rg.energy_status_tile;
486 rgaddr = hwmon->rg.energy_status_all;
487 return i915_mmio_reg_valid(rgaddr) ? 0444 : 0;
494 hwm_energy_read(struct hwm_drvdata *ddat, u32 attr, long *val)
497 case hwmon_energy_input:
498 hwm_energy(ddat, val);
506 hwm_curr_is_visible(const struct hwm_drvdata *ddat, u32 attr)
508 struct drm_i915_private *i915 = ddat->uncore->i915;
512 case hwmon_curr_crit:
513 return (hwm_pcode_read_i1(i915, &uval) ||
514 (uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
521 hwm_curr_read(struct hwm_drvdata *ddat, u32 attr, long *val)
527 case hwmon_curr_crit:
528 ret = hwm_pcode_read_i1(ddat->uncore->i915, &uval);
531 if (uval & POWER_SETUP_I1_WATTS)
533 *val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
534 SF_CURR, POWER_SETUP_I1_SHIFT);
542 hwm_curr_write(struct hwm_drvdata *ddat, u32 attr, long val)
547 case hwmon_curr_crit:
548 uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_CURR);
549 return hwm_pcode_write_i1(ddat->uncore->i915, uval);
556 hwm_is_visible(const void *drvdata, enum hwmon_sensor_types type,
557 u32 attr, int channel)
559 struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata;
563 return hwm_in_is_visible(ddat, attr);
565 return hwm_power_is_visible(ddat, attr, channel);
567 return hwm_energy_is_visible(ddat, attr);
569 return hwm_curr_is_visible(ddat, attr);
576 hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
577 int channel, long *val)
579 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
583 return hwm_in_read(ddat, attr, val);
585 return hwm_power_read(ddat, attr, channel, val);
587 return hwm_energy_read(ddat, attr, val);
589 return hwm_curr_read(ddat, attr, val);
596 hwm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
597 int channel, long val)
599 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
603 return hwm_power_write(ddat, attr, channel, val);
605 return hwm_curr_write(ddat, attr, val);
611 static const struct hwmon_ops hwm_ops = {
612 .is_visible = hwm_is_visible,
617 static const struct hwmon_chip_info hwm_chip_info = {
623 hwm_gt_is_visible(const void *drvdata, enum hwmon_sensor_types type,
624 u32 attr, int channel)
626 struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata;
630 return hwm_energy_is_visible(ddat, attr);
637 hwm_gt_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
638 int channel, long *val)
640 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
644 return hwm_energy_read(ddat, attr, val);
650 static const struct hwmon_ops hwm_gt_ops = {
651 .is_visible = hwm_gt_is_visible,
655 static const struct hwmon_chip_info hwm_gt_chip_info = {
661 hwm_get_preregistration_info(struct drm_i915_private *i915)
663 struct i915_hwmon *hwmon = i915->hwmon;
664 struct intel_uncore *uncore = &i915->uncore;
665 struct hwm_drvdata *ddat = &hwmon->ddat;
666 intel_wakeref_t wakeref;
667 u32 val_sku_unit = 0;
672 /* Available for all Gen12+/dGfx */
673 hwmon->rg.gt_perf_status = GEN12_RPSTAT1;
675 if (IS_DG1(i915) || IS_DG2(i915)) {
676 hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT;
677 hwmon->rg.pkg_power_sku = PCU_PACKAGE_POWER_SKU;
678 hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT;
679 hwmon->rg.energy_status_all = PCU_PACKAGE_ENERGY_STATUS;
680 hwmon->rg.energy_status_tile = INVALID_MMIO_REG;
681 } else if (IS_XEHPSDV(i915)) {
682 hwmon->rg.pkg_power_sku_unit = GT0_PACKAGE_POWER_SKU_UNIT;
683 hwmon->rg.pkg_power_sku = INVALID_MMIO_REG;
684 hwmon->rg.pkg_rapl_limit = GT0_PACKAGE_RAPL_LIMIT;
685 hwmon->rg.energy_status_all = GT0_PLATFORM_ENERGY_STATUS;
686 hwmon->rg.energy_status_tile = GT0_PACKAGE_ENERGY_STATUS;
688 hwmon->rg.pkg_power_sku_unit = INVALID_MMIO_REG;
689 hwmon->rg.pkg_power_sku = INVALID_MMIO_REG;
690 hwmon->rg.pkg_rapl_limit = INVALID_MMIO_REG;
691 hwmon->rg.energy_status_all = INVALID_MMIO_REG;
692 hwmon->rg.energy_status_tile = INVALID_MMIO_REG;
695 with_intel_runtime_pm(uncore->rpm, wakeref) {
697 * The contents of register hwmon->rg.pkg_power_sku_unit do not change,
698 * so read it once and store the shift values.
700 if (i915_mmio_reg_valid(hwmon->rg.pkg_power_sku_unit))
701 val_sku_unit = intel_uncore_read(uncore,
702 hwmon->rg.pkg_power_sku_unit);
705 hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit);
706 hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit);
707 hwmon->scl_shift_time = REG_FIELD_GET(PKG_TIME_UNIT, val_sku_unit);
710 * Initialize 'struct hwm_energy_info', i.e. set fields to the
711 * first value of the energy register read
713 if (i915_mmio_reg_valid(hwmon->rg.energy_status_all))
714 hwm_energy(ddat, &energy);
715 if (i915_mmio_reg_valid(hwmon->rg.energy_status_tile)) {
716 for_each_gt(gt, i915, i)
717 hwm_energy(&hwmon->ddat_gt[i], &energy);
721 void i915_hwmon_register(struct drm_i915_private *i915)
723 struct device *dev = i915->drm.dev;
724 struct i915_hwmon *hwmon;
725 struct device *hwmon_dev;
726 struct hwm_drvdata *ddat;
727 struct hwm_drvdata *ddat_gt;
731 /* hwmon is available only for dGfx */
735 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
740 mutex_init(&hwmon->hwmon_lock);
744 ddat->uncore = &i915->uncore;
745 snprintf(ddat->name, sizeof(ddat->name), "i915");
748 for_each_gt(gt, i915, i) {
749 ddat_gt = hwmon->ddat_gt + i;
751 ddat_gt->hwmon = hwmon;
752 ddat_gt->uncore = gt->uncore;
753 snprintf(ddat_gt->name, sizeof(ddat_gt->name), "i915_gt%u", i);
757 hwm_get_preregistration_info(i915);
759 /* hwmon_dev points to device hwmon<i> */
760 hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name,
764 if (IS_ERR(hwmon_dev)) {
769 ddat->hwmon_dev = hwmon_dev;
771 for_each_gt(gt, i915, i) {
772 ddat_gt = hwmon->ddat_gt + i;
774 * Create per-gt directories only if a per-gt attribute is
775 * visible. Currently this is only energy
777 if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0))
780 hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name,
784 if (!IS_ERR(hwmon_dev))
785 ddat_gt->hwmon_dev = hwmon_dev;
789 void i915_hwmon_unregister(struct drm_i915_private *i915)
791 fetch_and_zero(&i915->hwmon);