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 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 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 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 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 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 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 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 */ 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 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 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 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 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 static int 363 hwm_power_read(struct hwm_drvdata *ddat, u32 attr, int chan, long *val) 364 { 365 struct i915_hwmon *hwmon = ddat->hwmon; 366 int ret; 367 u32 uval; 368 369 switch (attr) { 370 case hwmon_power_max: 371 *val = hwm_field_read_and_scale(ddat, 372 hwmon->rg.pkg_rapl_limit, 373 PKG_PWR_LIM_1, 374 hwmon->scl_shift_power, 375 SF_POWER); 376 return 0; 377 case hwmon_power_rated_max: 378 *val = hwm_field_read_and_scale(ddat, 379 hwmon->rg.pkg_power_sku, 380 PKG_PKG_TDP, 381 hwmon->scl_shift_power, 382 SF_POWER); 383 return 0; 384 case hwmon_power_crit: 385 ret = hwm_pcode_read_i1(ddat->uncore->i915, &uval); 386 if (ret) 387 return ret; 388 if (!(uval & POWER_SETUP_I1_WATTS)) 389 return -ENODEV; 390 *val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval), 391 SF_POWER, POWER_SETUP_I1_SHIFT); 392 return 0; 393 default: 394 return -EOPNOTSUPP; 395 } 396 } 397 398 static int 399 hwm_power_write(struct hwm_drvdata *ddat, u32 attr, int chan, long val) 400 { 401 struct i915_hwmon *hwmon = ddat->hwmon; 402 u32 uval; 403 404 switch (attr) { 405 case hwmon_power_max: 406 hwm_field_scale_and_write(ddat, 407 hwmon->rg.pkg_rapl_limit, 408 hwmon->scl_shift_power, 409 SF_POWER, val); 410 return 0; 411 case hwmon_power_crit: 412 uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_POWER); 413 return hwm_pcode_write_i1(ddat->uncore->i915, uval); 414 default: 415 return -EOPNOTSUPP; 416 } 417 } 418 419 static umode_t 420 hwm_energy_is_visible(const struct hwm_drvdata *ddat, u32 attr) 421 { 422 struct i915_hwmon *hwmon = ddat->hwmon; 423 i915_reg_t rgaddr; 424 425 switch (attr) { 426 case hwmon_energy_input: 427 if (ddat->gt_n >= 0) 428 rgaddr = hwmon->rg.energy_status_tile; 429 else 430 rgaddr = hwmon->rg.energy_status_all; 431 return i915_mmio_reg_valid(rgaddr) ? 0444 : 0; 432 default: 433 return 0; 434 } 435 } 436 437 static int 438 hwm_energy_read(struct hwm_drvdata *ddat, u32 attr, long *val) 439 { 440 switch (attr) { 441 case hwmon_energy_input: 442 hwm_energy(ddat, val); 443 return 0; 444 default: 445 return -EOPNOTSUPP; 446 } 447 } 448 449 static umode_t 450 hwm_curr_is_visible(const struct hwm_drvdata *ddat, u32 attr) 451 { 452 struct drm_i915_private *i915 = ddat->uncore->i915; 453 u32 uval; 454 455 switch (attr) { 456 case hwmon_curr_crit: 457 return (hwm_pcode_read_i1(i915, &uval) || 458 (uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644; 459 default: 460 return 0; 461 } 462 } 463 464 static int 465 hwm_curr_read(struct hwm_drvdata *ddat, u32 attr, long *val) 466 { 467 int ret; 468 u32 uval; 469 470 switch (attr) { 471 case hwmon_curr_crit: 472 ret = hwm_pcode_read_i1(ddat->uncore->i915, &uval); 473 if (ret) 474 return ret; 475 if (uval & POWER_SETUP_I1_WATTS) 476 return -ENODEV; 477 *val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval), 478 SF_CURR, POWER_SETUP_I1_SHIFT); 479 return 0; 480 default: 481 return -EOPNOTSUPP; 482 } 483 } 484 485 static int 486 hwm_curr_write(struct hwm_drvdata *ddat, u32 attr, long val) 487 { 488 u32 uval; 489 490 switch (attr) { 491 case hwmon_curr_crit: 492 uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_CURR); 493 return hwm_pcode_write_i1(ddat->uncore->i915, uval); 494 default: 495 return -EOPNOTSUPP; 496 } 497 } 498 499 static umode_t 500 hwm_is_visible(const void *drvdata, enum hwmon_sensor_types type, 501 u32 attr, int channel) 502 { 503 struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata; 504 505 switch (type) { 506 case hwmon_in: 507 return hwm_in_is_visible(ddat, attr); 508 case hwmon_power: 509 return hwm_power_is_visible(ddat, attr, channel); 510 case hwmon_energy: 511 return hwm_energy_is_visible(ddat, attr); 512 case hwmon_curr: 513 return hwm_curr_is_visible(ddat, attr); 514 default: 515 return 0; 516 } 517 } 518 519 static int 520 hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, 521 int channel, long *val) 522 { 523 struct hwm_drvdata *ddat = dev_get_drvdata(dev); 524 525 switch (type) { 526 case hwmon_in: 527 return hwm_in_read(ddat, attr, val); 528 case hwmon_power: 529 return hwm_power_read(ddat, attr, channel, val); 530 case hwmon_energy: 531 return hwm_energy_read(ddat, attr, val); 532 case hwmon_curr: 533 return hwm_curr_read(ddat, attr, val); 534 default: 535 return -EOPNOTSUPP; 536 } 537 } 538 539 static int 540 hwm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, 541 int channel, long val) 542 { 543 struct hwm_drvdata *ddat = dev_get_drvdata(dev); 544 545 switch (type) { 546 case hwmon_power: 547 return hwm_power_write(ddat, attr, channel, val); 548 case hwmon_curr: 549 return hwm_curr_write(ddat, attr, val); 550 default: 551 return -EOPNOTSUPP; 552 } 553 } 554 555 static const struct hwmon_ops hwm_ops = { 556 .is_visible = hwm_is_visible, 557 .read = hwm_read, 558 .write = hwm_write, 559 }; 560 561 static const struct hwmon_chip_info hwm_chip_info = { 562 .ops = &hwm_ops, 563 .info = hwm_info, 564 }; 565 566 static umode_t 567 hwm_gt_is_visible(const void *drvdata, enum hwmon_sensor_types type, 568 u32 attr, int channel) 569 { 570 struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata; 571 572 switch (type) { 573 case hwmon_energy: 574 return hwm_energy_is_visible(ddat, attr); 575 default: 576 return 0; 577 } 578 } 579 580 static int 581 hwm_gt_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, 582 int channel, long *val) 583 { 584 struct hwm_drvdata *ddat = dev_get_drvdata(dev); 585 586 switch (type) { 587 case hwmon_energy: 588 return hwm_energy_read(ddat, attr, val); 589 default: 590 return -EOPNOTSUPP; 591 } 592 } 593 594 static const struct hwmon_ops hwm_gt_ops = { 595 .is_visible = hwm_gt_is_visible, 596 .read = hwm_gt_read, 597 }; 598 599 static const struct hwmon_chip_info hwm_gt_chip_info = { 600 .ops = &hwm_gt_ops, 601 .info = hwm_gt_info, 602 }; 603 604 static void 605 hwm_get_preregistration_info(struct drm_i915_private *i915) 606 { 607 struct i915_hwmon *hwmon = i915->hwmon; 608 struct intel_uncore *uncore = &i915->uncore; 609 struct hwm_drvdata *ddat = &hwmon->ddat; 610 intel_wakeref_t wakeref; 611 u32 val_sku_unit = 0; 612 struct intel_gt *gt; 613 long energy; 614 int i; 615 616 /* Available for all Gen12+/dGfx */ 617 hwmon->rg.gt_perf_status = GEN12_RPSTAT1; 618 619 if (IS_DG1(i915) || IS_DG2(i915)) { 620 hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT; 621 hwmon->rg.pkg_power_sku = PCU_PACKAGE_POWER_SKU; 622 hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT; 623 hwmon->rg.energy_status_all = PCU_PACKAGE_ENERGY_STATUS; 624 hwmon->rg.energy_status_tile = INVALID_MMIO_REG; 625 } else if (IS_XEHPSDV(i915)) { 626 hwmon->rg.pkg_power_sku_unit = GT0_PACKAGE_POWER_SKU_UNIT; 627 hwmon->rg.pkg_power_sku = INVALID_MMIO_REG; 628 hwmon->rg.pkg_rapl_limit = GT0_PACKAGE_RAPL_LIMIT; 629 hwmon->rg.energy_status_all = GT0_PLATFORM_ENERGY_STATUS; 630 hwmon->rg.energy_status_tile = GT0_PACKAGE_ENERGY_STATUS; 631 } else { 632 hwmon->rg.pkg_power_sku_unit = INVALID_MMIO_REG; 633 hwmon->rg.pkg_power_sku = INVALID_MMIO_REG; 634 hwmon->rg.pkg_rapl_limit = INVALID_MMIO_REG; 635 hwmon->rg.energy_status_all = INVALID_MMIO_REG; 636 hwmon->rg.energy_status_tile = INVALID_MMIO_REG; 637 } 638 639 with_intel_runtime_pm(uncore->rpm, wakeref) { 640 /* 641 * The contents of register hwmon->rg.pkg_power_sku_unit do not change, 642 * so read it once and store the shift values. 643 */ 644 if (i915_mmio_reg_valid(hwmon->rg.pkg_power_sku_unit)) 645 val_sku_unit = intel_uncore_read(uncore, 646 hwmon->rg.pkg_power_sku_unit); 647 } 648 649 hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit); 650 hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit); 651 hwmon->scl_shift_time = REG_FIELD_GET(PKG_TIME_UNIT, val_sku_unit); 652 653 /* 654 * Initialize 'struct hwm_energy_info', i.e. set fields to the 655 * first value of the energy register read 656 */ 657 if (i915_mmio_reg_valid(hwmon->rg.energy_status_all)) 658 hwm_energy(ddat, &energy); 659 if (i915_mmio_reg_valid(hwmon->rg.energy_status_tile)) { 660 for_each_gt(gt, i915, i) 661 hwm_energy(&hwmon->ddat_gt[i], &energy); 662 } 663 } 664 665 void i915_hwmon_register(struct drm_i915_private *i915) 666 { 667 struct device *dev = i915->drm.dev; 668 struct i915_hwmon *hwmon; 669 struct device *hwmon_dev; 670 struct hwm_drvdata *ddat; 671 struct hwm_drvdata *ddat_gt; 672 struct intel_gt *gt; 673 int i; 674 675 /* hwmon is available only for dGfx */ 676 if (!IS_DGFX(i915)) 677 return; 678 679 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL); 680 if (!hwmon) 681 return; 682 683 i915->hwmon = hwmon; 684 mutex_init(&hwmon->hwmon_lock); 685 ddat = &hwmon->ddat; 686 687 ddat->hwmon = hwmon; 688 ddat->uncore = &i915->uncore; 689 snprintf(ddat->name, sizeof(ddat->name), "i915"); 690 ddat->gt_n = -1; 691 692 for_each_gt(gt, i915, i) { 693 ddat_gt = hwmon->ddat_gt + i; 694 695 ddat_gt->hwmon = hwmon; 696 ddat_gt->uncore = gt->uncore; 697 snprintf(ddat_gt->name, sizeof(ddat_gt->name), "i915_gt%u", i); 698 ddat_gt->gt_n = i; 699 } 700 701 hwm_get_preregistration_info(i915); 702 703 /* hwmon_dev points to device hwmon<i> */ 704 hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat->name, 705 ddat, 706 &hwm_chip_info, 707 hwm_groups); 708 if (IS_ERR(hwmon_dev)) { 709 i915->hwmon = NULL; 710 return; 711 } 712 713 ddat->hwmon_dev = hwmon_dev; 714 715 for_each_gt(gt, i915, i) { 716 ddat_gt = hwmon->ddat_gt + i; 717 /* 718 * Create per-gt directories only if a per-gt attribute is 719 * visible. Currently this is only energy 720 */ 721 if (!hwm_gt_is_visible(ddat_gt, hwmon_energy, hwmon_energy_input, 0)) 722 continue; 723 724 hwmon_dev = devm_hwmon_device_register_with_info(dev, ddat_gt->name, 725 ddat_gt, 726 &hwm_gt_chip_info, 727 NULL); 728 if (!IS_ERR(hwmon_dev)) 729 ddat_gt->hwmon_dev = hwmon_dev; 730 } 731 } 732 733 void i915_hwmon_unregister(struct drm_i915_private *i915) 734 { 735 fetch_and_zero(&i915->hwmon); 736 } 737