i915_hwmon.c 25 KB

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