1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2022 Intel Corporation
4 */
5
6 #include <linux/hwmon.h>
7 #include <linux/hwmon-sysfs.h>
8 #include <linux/types.h>
9
10 #include "i915_drv.h"
11 #include "i915_hwmon.h"
12 #include "i915_reg.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"
17
18 /*
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
25 */
26 #define SF_VOLTAGE 1000
27 #define SF_POWER 1000000
28 #define SF_CURR 1000
29 #define SF_ENERGY 1000000
30 #define SF_TIME 1000
31
32 struct hwm_reg {
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;
39 };
40
41 struct hwm_energy_info {
42 u32 reg_val_prev;
43 long accum_energy; /* Accumulated energy for energy1_input */
44 };
45
46 struct hwm_drvdata {
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 */
51 char name[12];
52 int gt_n;
53 };
54
55 struct i915_hwmon {
56 struct hwm_drvdata ddat;
57 struct hwm_drvdata ddat_gt[I915_MAX_GT];
58 struct mutex hwmon_lock; /* counter overflow logic and rmw */
59 struct hwm_reg rg;
60 int scl_shift_power;
61 int scl_shift_energy;
62 int scl_shift_time;
63 };
64
65 static void
hwm_locked_with_pm_intel_uncore_rmw(struct hwm_drvdata * ddat,i915_reg_t reg,u32 clear,u32 set)66 hwm_locked_with_pm_intel_uncore_rmw(struct hwm_drvdata *ddat,
67 i915_reg_t reg, u32 clear, u32 set)
68 {
69 struct i915_hwmon *hwmon = ddat->hwmon;
70 struct intel_uncore *uncore = ddat->uncore;
71 intel_wakeref_t wakeref;
72
73 mutex_lock(&hwmon->hwmon_lock);
74
75 with_intel_runtime_pm(uncore->rpm, wakeref)
76 intel_uncore_rmw(uncore, reg, clear, set);
77
78 mutex_unlock(&hwmon->hwmon_lock);
79 }
80
81 /*
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
84 * exceed 32 bits.
85 */
86 static u64
hwm_field_read_and_scale(struct hwm_drvdata * ddat,i915_reg_t rgadr,u32 field_msk,int nshift,u32 scale_factor)87 hwm_field_read_and_scale(struct hwm_drvdata *ddat, i915_reg_t rgadr,
88 u32 field_msk, int nshift, u32 scale_factor)
89 {
90 struct intel_uncore *uncore = ddat->uncore;
91 intel_wakeref_t wakeref;
92 u32 reg_value;
93
94 with_intel_runtime_pm(uncore->rpm, wakeref)
95 reg_value = intel_uncore_read(uncore, rgadr);
96
97 reg_value = REG_FIELD_GET(field_msk, reg_value);
98
99 return mul_u64_u32_shr(reg_value, scale_factor, nshift);
100 }
101
102 static void
hwm_field_scale_and_write(struct hwm_drvdata * ddat,i915_reg_t rgadr,int nshift,unsigned int scale_factor,long lval)103 hwm_field_scale_and_write(struct hwm_drvdata *ddat, i915_reg_t rgadr,
104 int nshift, unsigned int scale_factor, long lval)
105 {
106 u32 nval;
107
108 /* Computation in 64-bits to avoid overflow. Round to nearest. */
109 nval = DIV_ROUND_CLOSEST_ULL((u64)lval << nshift, scale_factor);
110
111 hwm_locked_with_pm_intel_uncore_rmw(ddat, rgadr,
112 PKG_PWR_LIM_1,
113 REG_FIELD_PREP(PKG_PWR_LIM_1, nval));
114 }
115
116 /*
117 * hwm_energy - Obtain energy value
118 *
119 * The underlying energy hardware register is 32-bits and is subject to
120 * overflow. How long before overflow? For example, with an example
121 * scaling bit shift of 14 bits (see register *PACKAGE_POWER_SKU_UNIT) and
122 * a power draw of 1000 watts, the 32-bit counter will overflow in
123 * approximately 4.36 minutes.
124 *
125 * Examples:
126 * 1 watt: (2^32 >> 14) / 1 W / (60 * 60 * 24) secs/day -> 3 days
127 * 1000 watts: (2^32 >> 14) / 1000 W / 60 secs/min -> 4.36 minutes
128 *
129 * The function significantly increases overflow duration (from 4.36
130 * minutes) by accumulating the energy register into a 'long' as allowed by
131 * the hwmon API. Using x86_64 128 bit arithmetic (see mul_u64_u32_shr()),
132 * a 'long' of 63 bits, SF_ENERGY of 1e6 (~20 bits) and
133 * hwmon->scl_shift_energy of 14 bits we have 57 (63 - 20 + 14) bits before
134 * energy1_input overflows. This at 1000 W is an overflow duration of 278 years.
135 */
136 static void
hwm_energy(struct hwm_drvdata * ddat,long * energy)137 hwm_energy(struct hwm_drvdata *ddat, long *energy)
138 {
139 struct intel_uncore *uncore = ddat->uncore;
140 struct i915_hwmon *hwmon = ddat->hwmon;
141 struct hwm_energy_info *ei = &ddat->ei;
142 intel_wakeref_t wakeref;
143 i915_reg_t rgaddr;
144 u32 reg_val;
145
146 if (ddat->gt_n >= 0)
147 rgaddr = hwmon->rg.energy_status_tile;
148 else
149 rgaddr = hwmon->rg.energy_status_all;
150
151 mutex_lock(&hwmon->hwmon_lock);
152
153 with_intel_runtime_pm(uncore->rpm, wakeref)
154 reg_val = intel_uncore_read(uncore, rgaddr);
155
156 if (reg_val >= ei->reg_val_prev)
157 ei->accum_energy += reg_val - ei->reg_val_prev;
158 else
159 ei->accum_energy += UINT_MAX - ei->reg_val_prev + reg_val;
160 ei->reg_val_prev = reg_val;
161
162 *energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,
163 hwmon->scl_shift_energy);
164 mutex_unlock(&hwmon->hwmon_lock);
165 }
166
167 static ssize_t
hwm_power1_max_interval_show(struct device * dev,struct device_attribute * attr,char * buf)168 hwm_power1_max_interval_show(struct device *dev, struct device_attribute *attr,
169 char *buf)
170 {
171 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
172 struct i915_hwmon *hwmon = ddat->hwmon;
173 intel_wakeref_t wakeref;
174 u32 r, x, y, x_w = 2; /* 2 bits */
175 u64 tau4, out;
176
177 with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
178 r = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_rapl_limit);
179
180 x = REG_FIELD_GET(PKG_PWR_LIM_1_TIME_X, r);
181 y = REG_FIELD_GET(PKG_PWR_LIM_1_TIME_Y, r);
182 /*
183 * tau = 1.x * power(2,y), x = bits(23:22), y = bits(21:17)
184 * = (4 | x) << (y - 2)
185 * where (y - 2) ensures a 1.x fixed point representation of 1.x
186 * However because y can be < 2, we compute
187 * tau4 = (4 | x) << y
188 * but add 2 when doing the final right shift to account for units
189 */
190 tau4 = ((1 << x_w) | x) << y;
191 /* val in hwmon interface units (millisec) */
192 out = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
193
194 return sysfs_emit(buf, "%llu\n", out);
195 }
196
197 static ssize_t
hwm_power1_max_interval_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)198 hwm_power1_max_interval_store(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
201 {
202 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
203 struct i915_hwmon *hwmon = ddat->hwmon;
204 u32 x, y, rxy, x_w = 2; /* 2 bits */
205 u64 tau4, r, max_win;
206 unsigned long val;
207 int ret;
208
209 ret = kstrtoul(buf, 0, &val);
210 if (ret)
211 return ret;
212
213 /*
214 * Max HW supported tau in '1.x * power(2,y)' format, x = 0, y = 0x12
215 * The hwmon->scl_shift_time default of 0xa results in a max tau of 256 seconds
216 */
217 #define PKG_MAX_WIN_DEFAULT 0x12ull
218
219 /*
220 * val must be < max in hwmon interface units. The steps below are
221 * explained in i915_power1_max_interval_show()
222 */
223 r = FIELD_PREP(PKG_MAX_WIN, PKG_MAX_WIN_DEFAULT);
224 x = REG_FIELD_GET(PKG_MAX_WIN_X, r);
225 y = REG_FIELD_GET(PKG_MAX_WIN_Y, r);
226 tau4 = ((1 << x_w) | x) << y;
227 max_win = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
228
229 if (val > max_win)
230 return -EINVAL;
231
232 /* val in hw units */
233 val = DIV_ROUND_CLOSEST_ULL((u64)val << hwmon->scl_shift_time, SF_TIME);
234 /* Convert to 1.x * power(2,y) */
235 if (!val)
236 return -EINVAL;
237 y = ilog2(val);
238 /* x = (val - (1 << y)) >> (y - 2); */
239 x = (val - (1ul << y)) << x_w >> y;
240
241 rxy = REG_FIELD_PREP(PKG_PWR_LIM_1_TIME_X, x) | REG_FIELD_PREP(PKG_PWR_LIM_1_TIME_Y, y);
242
243 hwm_locked_with_pm_intel_uncore_rmw(ddat, hwmon->rg.pkg_rapl_limit,
244 PKG_PWR_LIM_1_TIME, rxy);
245 return count;
246 }
247
248 static SENSOR_DEVICE_ATTR(power1_max_interval, 0664,
249 hwm_power1_max_interval_show,
250 hwm_power1_max_interval_store, 0);
251
252 static struct attribute *hwm_attributes[] = {
253 &sensor_dev_attr_power1_max_interval.dev_attr.attr,
254 NULL
255 };
256
hwm_attributes_visible(struct kobject * kobj,struct attribute * attr,int index)257 static umode_t hwm_attributes_visible(struct kobject *kobj,
258 struct attribute *attr, int index)
259 {
260 struct device *dev = kobj_to_dev(kobj);
261 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
262 struct i915_hwmon *hwmon = ddat->hwmon;
263
264 if (attr == &sensor_dev_attr_power1_max_interval.dev_attr.attr)
265 return i915_mmio_reg_valid(hwmon->rg.pkg_rapl_limit) ? attr->mode : 0;
266
267 return 0;
268 }
269
270 static const struct attribute_group hwm_attrgroup = {
271 .attrs = hwm_attributes,
272 .is_visible = hwm_attributes_visible,
273 };
274
275 static const struct attribute_group *hwm_groups[] = {
276 &hwm_attrgroup,
277 NULL
278 };
279
280 static const struct hwmon_channel_info *hwm_info[] = {
281 HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
282 HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
283 HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
284 HWMON_CHANNEL_INFO(curr, HWMON_C_CRIT),
285 NULL
286 };
287
288 static const struct hwmon_channel_info *hwm_gt_info[] = {
289 HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
290 NULL
291 };
292
293 /* I1 is exposed as power_crit or as curr_crit depending on bit 31 */
hwm_pcode_read_i1(struct drm_i915_private * i915,u32 * uval)294 static int hwm_pcode_read_i1(struct drm_i915_private *i915, u32 *uval)
295 {
296 /* Avoid ILLEGAL_SUBCOMMAND "mailbox access failed" warning in snb_pcode_read */
297 if (IS_DG1(i915) || IS_DG2(i915))
298 return -ENXIO;
299
300 return snb_pcode_read_p(&i915->uncore, PCODE_POWER_SETUP,
301 POWER_SETUP_SUBCOMMAND_READ_I1, 0, uval);
302 }
303
hwm_pcode_write_i1(struct drm_i915_private * i915,u32 uval)304 static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
305 {
306 return snb_pcode_write_p(&i915->uncore, PCODE_POWER_SETUP,
307 POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
308 }
309
310 static umode_t
hwm_in_is_visible(const struct hwm_drvdata * ddat,u32 attr)311 hwm_in_is_visible(const struct hwm_drvdata *ddat, u32 attr)
312 {
313 struct drm_i915_private *i915 = ddat->uncore->i915;
314
315 switch (attr) {
316 case hwmon_in_input:
317 return IS_DG1(i915) || IS_DG2(i915) ? 0444 : 0;
318 default:
319 return 0;
320 }
321 }
322
323 static int
hwm_in_read(struct hwm_drvdata * ddat,u32 attr,long * val)324 hwm_in_read(struct hwm_drvdata *ddat, u32 attr, long *val)
325 {
326 struct i915_hwmon *hwmon = ddat->hwmon;
327 intel_wakeref_t wakeref;
328 u32 reg_value;
329
330 switch (attr) {
331 case hwmon_in_input:
332 with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
333 reg_value = intel_uncore_read(ddat->uncore, hwmon->rg.gt_perf_status);
334 /* HW register value in units of 2.5 millivolt */
335 *val = DIV_ROUND_CLOSEST(REG_FIELD_GET(GEN12_VOLTAGE_MASK, reg_value) * 25, 10);
336 return 0;
337 default:
338 return -EOPNOTSUPP;
339 }
340 }
341
342 static umode_t
hwm_power_is_visible(const struct hwm_drvdata * ddat,u32 attr,int chan)343 hwm_power_is_visible(const struct hwm_drvdata *ddat, u32 attr, int chan)
344 {
345 struct drm_i915_private *i915 = ddat->uncore->i915;
346 struct i915_hwmon *hwmon = ddat->hwmon;
347 u32 uval;
348
349 switch (attr) {
350 case hwmon_power_max:
351 return i915_mmio_reg_valid(hwmon->rg.pkg_rapl_limit) ? 0664 : 0;
352 case hwmon_power_rated_max:
353 return i915_mmio_reg_valid(hwmon->rg.pkg_power_sku) ? 0444 : 0;
354 case hwmon_power_crit:
355 return (hwm_pcode_read_i1(i915, &uval) ||
356 !(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
357 default:
358 return 0;
359 }
360 }
361
362 /*
363 * HW allows arbitrary PL1 limits to be set but silently clamps these values to
364 * "typical but not guaranteed" min/max values in rg.pkg_power_sku. Follow the
365 * same pattern for sysfs, allow arbitrary PL1 limits to be set but display
366 * clamped values when read. Write/read I1 also follows the same pattern.
367 */
368 static int
hwm_power_max_read(struct hwm_drvdata * ddat,long * val)369 hwm_power_max_read(struct hwm_drvdata *ddat, long *val)
370 {
371 struct i915_hwmon *hwmon = ddat->hwmon;
372 intel_wakeref_t wakeref;
373 u64 r, min, max;
374
375 *val = hwm_field_read_and_scale(ddat,
376 hwmon->rg.pkg_rapl_limit,
377 PKG_PWR_LIM_1,
378 hwmon->scl_shift_power,
379 SF_POWER);
380
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);
387
388 if (min && max)
389 *val = clamp_t(u64, *val, min, max);
390
391 return 0;
392 }
393
394 static int
hwm_power_read(struct hwm_drvdata * ddat,u32 attr,int chan,long * val)395 hwm_power_read(struct hwm_drvdata *ddat, u32 attr, int chan, long *val)
396 {
397 struct i915_hwmon *hwmon = ddat->hwmon;
398 int ret;
399 u32 uval;
400
401 switch (attr) {
402 case hwmon_power_max:
403 return hwm_power_max_read(ddat, val);
404 case hwmon_power_rated_max:
405 *val = hwm_field_read_and_scale(ddat,
406 hwmon->rg.pkg_power_sku,
407 PKG_PKG_TDP,
408 hwmon->scl_shift_power,
409 SF_POWER);
410 return 0;
411 case hwmon_power_crit:
412 ret = hwm_pcode_read_i1(ddat->uncore->i915, &uval);
413 if (ret)
414 return ret;
415 if (!(uval & POWER_SETUP_I1_WATTS))
416 return -ENODEV;
417 *val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
418 SF_POWER, POWER_SETUP_I1_SHIFT);
419 return 0;
420 default:
421 return -EOPNOTSUPP;
422 }
423 }
424
425 static int
hwm_power_write(struct hwm_drvdata * ddat,u32 attr,int chan,long val)426 hwm_power_write(struct hwm_drvdata *ddat, u32 attr, int chan, long val)
427 {
428 struct i915_hwmon *hwmon = ddat->hwmon;
429 u32 uval;
430
431 switch (attr) {
432 case hwmon_power_max:
433 hwm_field_scale_and_write(ddat,
434 hwmon->rg.pkg_rapl_limit,
435 hwmon->scl_shift_power,
436 SF_POWER, val);
437 return 0;
438 case hwmon_power_crit:
439 uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_POWER);
440 return hwm_pcode_write_i1(ddat->uncore->i915, uval);
441 default:
442 return -EOPNOTSUPP;
443 }
444 }
445
446 static umode_t
hwm_energy_is_visible(const struct hwm_drvdata * ddat,u32 attr)447 hwm_energy_is_visible(const struct hwm_drvdata *ddat, u32 attr)
448 {
449 struct i915_hwmon *hwmon = ddat->hwmon;
450 i915_reg_t rgaddr;
451
452 switch (attr) {
453 case hwmon_energy_input:
454 if (ddat->gt_n >= 0)
455 rgaddr = hwmon->rg.energy_status_tile;
456 else
457 rgaddr = hwmon->rg.energy_status_all;
458 return i915_mmio_reg_valid(rgaddr) ? 0444 : 0;
459 default:
460 return 0;
461 }
462 }
463
464 static int
hwm_energy_read(struct hwm_drvdata * ddat,u32 attr,long * val)465 hwm_energy_read(struct hwm_drvdata *ddat, u32 attr, long *val)
466 {
467 switch (attr) {
468 case hwmon_energy_input:
469 hwm_energy(ddat, val);
470 return 0;
471 default:
472 return -EOPNOTSUPP;
473 }
474 }
475
476 static umode_t
hwm_curr_is_visible(const struct hwm_drvdata * ddat,u32 attr)477 hwm_curr_is_visible(const struct hwm_drvdata *ddat, u32 attr)
478 {
479 struct drm_i915_private *i915 = ddat->uncore->i915;
480 u32 uval;
481
482 switch (attr) {
483 case hwmon_curr_crit:
484 return (hwm_pcode_read_i1(i915, &uval) ||
485 (uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
486 default:
487 return 0;
488 }
489 }
490
491 static int
hwm_curr_read(struct hwm_drvdata * ddat,u32 attr,long * val)492 hwm_curr_read(struct hwm_drvdata *ddat, u32 attr, long *val)
493 {
494 int ret;
495 u32 uval;
496
497 switch (attr) {
498 case hwmon_curr_crit:
499 ret = hwm_pcode_read_i1(ddat->uncore->i915, &uval);
500 if (ret)
501 return ret;
502 if (uval & POWER_SETUP_I1_WATTS)
503 return -ENODEV;
504 *val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
505 SF_CURR, POWER_SETUP_I1_SHIFT);
506 return 0;
507 default:
508 return -EOPNOTSUPP;
509 }
510 }
511
512 static int
hwm_curr_write(struct hwm_drvdata * ddat,u32 attr,long val)513 hwm_curr_write(struct hwm_drvdata *ddat, u32 attr, long val)
514 {
515 u32 uval;
516
517 switch (attr) {
518 case hwmon_curr_crit:
519 uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_CURR);
520 return hwm_pcode_write_i1(ddat->uncore->i915, uval);
521 default:
522 return -EOPNOTSUPP;
523 }
524 }
525
526 static umode_t
hwm_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)527 hwm_is_visible(const void *drvdata, enum hwmon_sensor_types type,
528 u32 attr, int channel)
529 {
530 struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata;
531
532 switch (type) {
533 case hwmon_in:
534 return hwm_in_is_visible(ddat, attr);
535 case hwmon_power:
536 return hwm_power_is_visible(ddat, attr, channel);
537 case hwmon_energy:
538 return hwm_energy_is_visible(ddat, attr);
539 case hwmon_curr:
540 return hwm_curr_is_visible(ddat, attr);
541 default:
542 return 0;
543 }
544 }
545
546 static int
hwm_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)547 hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
548 int channel, long *val)
549 {
550 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
551
552 switch (type) {
553 case hwmon_in:
554 return hwm_in_read(ddat, attr, val);
555 case hwmon_power:
556 return hwm_power_read(ddat, attr, channel, val);
557 case hwmon_energy:
558 return hwm_energy_read(ddat, attr, val);
559 case hwmon_curr:
560 return hwm_curr_read(ddat, attr, val);
561 default:
562 return -EOPNOTSUPP;
563 }
564 }
565
566 static int
hwm_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)567 hwm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
568 int channel, long val)
569 {
570 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
571
572 switch (type) {
573 case hwmon_power:
574 return hwm_power_write(ddat, attr, channel, val);
575 case hwmon_curr:
576 return hwm_curr_write(ddat, attr, val);
577 default:
578 return -EOPNOTSUPP;
579 }
580 }
581
582 static const struct hwmon_ops hwm_ops = {
583 .is_visible = hwm_is_visible,
584 .read = hwm_read,
585 .write = hwm_write,
586 };
587
588 static const struct hwmon_chip_info hwm_chip_info = {
589 .ops = &hwm_ops,
590 .info = hwm_info,
591 };
592
593 static umode_t
hwm_gt_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)594 hwm_gt_is_visible(const void *drvdata, enum hwmon_sensor_types type,
595 u32 attr, int channel)
596 {
597 struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata;
598
599 switch (type) {
600 case hwmon_energy:
601 return hwm_energy_is_visible(ddat, attr);
602 default:
603 return 0;
604 }
605 }
606
607 static int
hwm_gt_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)608 hwm_gt_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
609 int channel, long *val)
610 {
611 struct hwm_drvdata *ddat = dev_get_drvdata(dev);
612
613 switch (type) {
614 case hwmon_energy:
615 return hwm_energy_read(ddat, attr, val);
616 default:
617 return -EOPNOTSUPP;
618 }
619 }
620
621 static const struct hwmon_ops hwm_gt_ops = {
622 .is_visible = hwm_gt_is_visible,
623 .read = hwm_gt_read,
624 };
625
626 static const struct hwmon_chip_info hwm_gt_chip_info = {
627 .ops = &hwm_gt_ops,
628 .info = hwm_gt_info,
629 };
630
631 static void
hwm_get_preregistration_info(struct drm_i915_private * i915)632 hwm_get_preregistration_info(struct drm_i915_private *i915)
633 {
634 struct i915_hwmon *hwmon = i915->hwmon;
635 struct intel_uncore *uncore = &i915->uncore;
636 struct hwm_drvdata *ddat = &hwmon->ddat;
637 intel_wakeref_t wakeref;
638 u32 val_sku_unit = 0;
639 struct intel_gt *gt;
640 long energy;
641 int i;
642
643 /* Available for all Gen12+/dGfx */
644 hwmon->rg.gt_perf_status = GEN12_RPSTAT1;
645
646 if (IS_DG1(i915) || IS_DG2(i915)) {
647 hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT;
648 hwmon->rg.pkg_power_sku = PCU_PACKAGE_POWER_SKU;
649 hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT;
650 hwmon->rg.energy_status_all = PCU_PACKAGE_ENERGY_STATUS;
651 hwmon->rg.energy_status_tile = INVALID_MMIO_REG;
652 } else if (IS_XEHPSDV(i915)) {
653 hwmon->rg.pkg_power_sku_unit = GT0_PACKAGE_POWER_SKU_UNIT;
654 hwmon->rg.pkg_power_sku = INVALID_MMIO_REG;
655 hwmon->rg.pkg_rapl_limit = GT0_PACKAGE_RAPL_LIMIT;
656 hwmon->rg.energy_status_all = GT0_PLATFORM_ENERGY_STATUS;
657 hwmon->rg.energy_status_tile = GT0_PACKAGE_ENERGY_STATUS;
658 } else {
659 hwmon->rg.pkg_power_sku_unit = INVALID_MMIO_REG;
660 hwmon->rg.pkg_power_sku = INVALID_MMIO_REG;
661 hwmon->rg.pkg_rapl_limit = INVALID_MMIO_REG;
662 hwmon->rg.energy_status_all = INVALID_MMIO_REG;
663 hwmon->rg.energy_status_tile = INVALID_MMIO_REG;
664 }
665
666 with_intel_runtime_pm(uncore->rpm, wakeref) {
667 /*
668 * The contents of register hwmon->rg.pkg_power_sku_unit do not change,
669 * so read it once and store the shift values.
670 */
671 if (i915_mmio_reg_valid(hwmon->rg.pkg_power_sku_unit))
672 val_sku_unit = intel_uncore_read(uncore,
673 hwmon->rg.pkg_power_sku_unit);
674 }
675
676 hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit);
677 hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit);
678 hwmon->scl_shift_time = REG_FIELD_GET(PKG_TIME_UNIT, val_sku_unit);
679
680 /*
681 * Initialize 'struct hwm_energy_info', i.e. set fields to the
682 * first value of the energy register read
683 */
684 if (i915_mmio_reg_valid(hwmon->rg.energy_status_all))
685 hwm_energy(ddat, &energy);
686 if (i915_mmio_reg_valid(hwmon->rg.energy_status_tile)) {
687 for_each_gt(gt, i915, i)
688 hwm_energy(&hwmon->ddat_gt[i], &energy);
689 }
690 }
691
i915_hwmon_register(struct drm_i915_private * i915)692 void i915_hwmon_register(struct drm_i915_private *i915)
693 {
694 struct device *dev = i915->drm.dev;
695 struct i915_hwmon *hwmon;
696 struct device *hwmon_dev;
697 struct hwm_drvdata *ddat;
698 struct hwm_drvdata *ddat_gt;
699 struct intel_gt *gt;
700 int i;
701
702 /* hwmon is available only for dGfx */
703 if (!IS_DGFX(i915))
704 return;
705
706 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
707 if (!hwmon)
708 return;
709
710 i915->hwmon = hwmon;
711 mutex_init(&hwmon->hwmon_lock);
712 ddat = &hwmon->ddat;
713
714 ddat->hwmon = hwmon;
715 ddat->uncore = &i915->uncore;
716 snprintf(ddat->name, sizeof(ddat->name), "i915");
717 ddat->gt_n = -1;
718
719 for_each_gt(gt, i915, i) {
720 ddat_gt = hwmon->ddat_gt + i;
721
722 ddat_gt->hwmon = hwmon;
723 ddat_gt->uncore = gt->uncore;
724 snprintf(ddat_gt->name, sizeof(ddat_gt->name), "i915_gt%u", i);
725 ddat_gt->gt_n = i;
726 }
727
728 hwm_get_preregistration_info(i915);
729
730 /* hwmon_dev points to device hwmon<i> */
731 hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name,
732 ddat,
733 &hwm_chip_info,
734 hwm_groups);
735 if (IS_ERR(hwmon_dev)) {
736 i915->hwmon = NULL;
737 return;
738 }
739
740 ddat->hwmon_dev = hwmon_dev;
741
742 for_each_gt(gt, i915, i) {
743 ddat_gt = hwmon->ddat_gt + i;
744 /*
745 * Create per-gt directories only if a per-gt attribute is
746 * visible. Currently this is only energy
747 */
748 if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0))
749 continue;
750
751 hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name,
752 ddat_gt,
753 &hwm_gt_chip_info,
754 NULL);
755 if (!IS_ERR(hwmon_dev))
756 ddat_gt->hwmon_dev = hwmon_dev;
757 }
758 }
759
i915_hwmon_unregister(struct drm_i915_private * i915)760 void i915_hwmon_unregister(struct drm_i915_private *i915)
761 {
762 fetch_and_zero(&i915->hwmon);
763 }
764