intel_pch_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* intel_pch_thermal.c - Intel PCH Thermal driver
  3. *
  4. * Copyright (c) 2015, Intel Corporation.
  5. *
  6. * Authors:
  7. * Tushar Dave <[email protected]>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/pci.h>
  14. #include <linux/pm.h>
  15. #include <linux/suspend.h>
  16. #include <linux/thermal.h>
  17. #include <linux/types.h>
  18. #include <linux/units.h>
  19. /* Intel PCH thermal Device IDs */
  20. #define PCH_THERMAL_DID_HSW_1 0x9C24 /* Haswell PCH */
  21. #define PCH_THERMAL_DID_HSW_2 0x8C24 /* Haswell PCH */
  22. #define PCH_THERMAL_DID_WPT 0x9CA4 /* Wildcat Point */
  23. #define PCH_THERMAL_DID_SKL 0x9D31 /* Skylake PCH */
  24. #define PCH_THERMAL_DID_SKL_H 0xA131 /* Skylake PCH 100 series */
  25. #define PCH_THERMAL_DID_CNL 0x9Df9 /* CNL PCH */
  26. #define PCH_THERMAL_DID_CNL_H 0xA379 /* CNL-H PCH */
  27. #define PCH_THERMAL_DID_CNL_LP 0x02F9 /* CNL-LP PCH */
  28. #define PCH_THERMAL_DID_CML_H 0X06F9 /* CML-H PCH */
  29. #define PCH_THERMAL_DID_LWB 0xA1B1 /* Lewisburg PCH */
  30. #define PCH_THERMAL_DID_WBG 0x8D24 /* Wellsburg PCH */
  31. /* Wildcat Point-LP PCH Thermal registers */
  32. #define WPT_TEMP 0x0000 /* Temperature */
  33. #define WPT_TSC 0x04 /* Thermal Sensor Control */
  34. #define WPT_TSS 0x06 /* Thermal Sensor Status */
  35. #define WPT_TSEL 0x08 /* Thermal Sensor Enable and Lock */
  36. #define WPT_TSREL 0x0A /* Thermal Sensor Report Enable and Lock */
  37. #define WPT_TSMIC 0x0C /* Thermal Sensor SMI Control */
  38. #define WPT_CTT 0x0010 /* Catastrophic Trip Point */
  39. #define WPT_TSPM 0x001C /* Thermal Sensor Power Management */
  40. #define WPT_TAHV 0x0014 /* Thermal Alert High Value */
  41. #define WPT_TALV 0x0018 /* Thermal Alert Low Value */
  42. #define WPT_TL 0x00000040 /* Throttle Value */
  43. #define WPT_PHL 0x0060 /* PCH Hot Level */
  44. #define WPT_PHLC 0x62 /* PHL Control */
  45. #define WPT_TAS 0x80 /* Thermal Alert Status */
  46. #define WPT_TSPIEN 0x82 /* PCI Interrupt Event Enables */
  47. #define WPT_TSGPEN 0x84 /* General Purpose Event Enables */
  48. /* Wildcat Point-LP PCH Thermal Register bit definitions */
  49. #define WPT_TEMP_TSR 0x01ff /* Temp TS Reading */
  50. #define WPT_TSC_CPDE 0x01 /* Catastrophic Power-Down Enable */
  51. #define WPT_TSS_TSDSS 0x10 /* Thermal Sensor Dynamic Shutdown Status */
  52. #define WPT_TSS_GPES 0x08 /* GPE status */
  53. #define WPT_TSEL_ETS 0x01 /* Enable TS */
  54. #define WPT_TSEL_PLDB 0x80 /* TSEL Policy Lock-Down Bit */
  55. #define WPT_TL_TOL 0x000001FF /* T0 Level */
  56. #define WPT_TL_T1L 0x1ff00000 /* T1 Level */
  57. #define WPT_TL_TTEN 0x20000000 /* TT Enable */
  58. /* Resolution of 1/2 degree C and an offset of -50C */
  59. #define PCH_TEMP_OFFSET (-50)
  60. #define GET_WPT_TEMP(x) ((x) * MILLIDEGREE_PER_DEGREE / 2 + WPT_TEMP_OFFSET)
  61. #define WPT_TEMP_OFFSET (PCH_TEMP_OFFSET * MILLIDEGREE_PER_DEGREE)
  62. #define GET_PCH_TEMP(x) (((x) / 2) + PCH_TEMP_OFFSET)
  63. /* Amount of time for each cooling delay, 100ms by default for now */
  64. static unsigned int delay_timeout = 100;
  65. module_param(delay_timeout, int, 0644);
  66. MODULE_PARM_DESC(delay_timeout, "amount of time delay for each iteration.");
  67. /* Number of iterations for cooling delay, 600 counts by default for now */
  68. static unsigned int delay_cnt = 600;
  69. module_param(delay_cnt, int, 0644);
  70. MODULE_PARM_DESC(delay_cnt, "total number of iterations for time delay.");
  71. static char driver_name[] = "Intel PCH thermal driver";
  72. struct pch_thermal_device {
  73. void __iomem *hw_base;
  74. const struct pch_dev_ops *ops;
  75. struct pci_dev *pdev;
  76. struct thermal_zone_device *tzd;
  77. int crt_trip_id;
  78. unsigned long crt_temp;
  79. int hot_trip_id;
  80. unsigned long hot_temp;
  81. int psv_trip_id;
  82. unsigned long psv_temp;
  83. bool bios_enabled;
  84. };
  85. #ifdef CONFIG_ACPI
  86. /*
  87. * On some platforms, there is a companion ACPI device, which adds
  88. * passive trip temperature using _PSV method. There is no specific
  89. * passive temperature setting in MMIO interface of this PCI device.
  90. */
  91. static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
  92. int *nr_trips)
  93. {
  94. struct acpi_device *adev;
  95. ptd->psv_trip_id = -1;
  96. adev = ACPI_COMPANION(&ptd->pdev->dev);
  97. if (adev) {
  98. unsigned long long r;
  99. acpi_status status;
  100. status = acpi_evaluate_integer(adev->handle, "_PSV", NULL,
  101. &r);
  102. if (ACPI_SUCCESS(status)) {
  103. unsigned long trip_temp;
  104. trip_temp = deci_kelvin_to_millicelsius(r);
  105. if (trip_temp) {
  106. ptd->psv_temp = trip_temp;
  107. ptd->psv_trip_id = *nr_trips;
  108. ++(*nr_trips);
  109. }
  110. }
  111. }
  112. }
  113. #else
  114. static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
  115. int *nr_trips)
  116. {
  117. ptd->psv_trip_id = -1;
  118. }
  119. #endif
  120. static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
  121. {
  122. u8 tsel;
  123. u16 trip_temp;
  124. *nr_trips = 0;
  125. /* Check if BIOS has already enabled thermal sensor */
  126. if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
  127. ptd->bios_enabled = true;
  128. goto read_trips;
  129. }
  130. tsel = readb(ptd->hw_base + WPT_TSEL);
  131. /*
  132. * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
  133. * If so, thermal sensor cannot enable. Bail out.
  134. */
  135. if (tsel & WPT_TSEL_PLDB) {
  136. dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
  137. return -ENODEV;
  138. }
  139. writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
  140. if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
  141. dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
  142. return -ENODEV;
  143. }
  144. read_trips:
  145. ptd->crt_trip_id = -1;
  146. trip_temp = readw(ptd->hw_base + WPT_CTT);
  147. trip_temp &= 0x1FF;
  148. if (trip_temp) {
  149. ptd->crt_temp = GET_WPT_TEMP(trip_temp);
  150. ptd->crt_trip_id = 0;
  151. ++(*nr_trips);
  152. }
  153. ptd->hot_trip_id = -1;
  154. trip_temp = readw(ptd->hw_base + WPT_PHL);
  155. trip_temp &= 0x1FF;
  156. if (trip_temp) {
  157. ptd->hot_temp = GET_WPT_TEMP(trip_temp);
  158. ptd->hot_trip_id = *nr_trips;
  159. ++(*nr_trips);
  160. }
  161. pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
  162. return 0;
  163. }
  164. static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
  165. {
  166. *temp = GET_WPT_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
  167. return 0;
  168. }
  169. /* Cool the PCH when it's overheat in .suspend_noirq phase */
  170. static int pch_wpt_suspend(struct pch_thermal_device *ptd)
  171. {
  172. u8 tsel;
  173. int pch_delay_cnt = 0;
  174. u16 pch_thr_temp, pch_cur_temp;
  175. /* Shutdown the thermal sensor if it is not enabled by BIOS */
  176. if (!ptd->bios_enabled) {
  177. tsel = readb(ptd->hw_base + WPT_TSEL);
  178. writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
  179. return 0;
  180. }
  181. /* Do not check temperature if it is not s2idle */
  182. if (pm_suspend_via_firmware())
  183. return 0;
  184. /* Get the PCH temperature threshold value */
  185. pch_thr_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TSPM));
  186. /* Get the PCH current temperature value */
  187. pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
  188. /*
  189. * If current PCH temperature is higher than configured PCH threshold
  190. * value, run some delay loop with sleep to let the current temperature
  191. * go down below the threshold value which helps to allow system enter
  192. * lower power S0ix suspend state. Even after delay loop if PCH current
  193. * temperature stays above threshold, notify the warning message
  194. * which helps to indentify the reason why S0ix entry was rejected.
  195. */
  196. while (pch_delay_cnt < delay_cnt) {
  197. if (pch_cur_temp < pch_thr_temp)
  198. break;
  199. if (pm_wakeup_pending()) {
  200. dev_warn(&ptd->pdev->dev, "Wakeup event detected, abort cooling\n");
  201. return 0;
  202. }
  203. pch_delay_cnt++;
  204. dev_dbg(&ptd->pdev->dev,
  205. "CPU-PCH current temp [%dC] higher than the threshold temp [%dC], sleep %d times for %d ms duration\n",
  206. pch_cur_temp, pch_thr_temp, pch_delay_cnt, delay_timeout);
  207. msleep(delay_timeout);
  208. /* Read the PCH current temperature for next cycle. */
  209. pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
  210. }
  211. if (pch_cur_temp >= pch_thr_temp)
  212. dev_warn(&ptd->pdev->dev,
  213. "CPU-PCH is hot [%dC] after %d ms delay. S0ix might fail\n",
  214. pch_cur_temp, pch_delay_cnt * delay_timeout);
  215. else {
  216. if (pch_delay_cnt)
  217. dev_info(&ptd->pdev->dev,
  218. "CPU-PCH is cool [%dC] after %d ms delay\n",
  219. pch_cur_temp, pch_delay_cnt * delay_timeout);
  220. else
  221. dev_info(&ptd->pdev->dev,
  222. "CPU-PCH is cool [%dC]\n",
  223. pch_cur_temp);
  224. }
  225. return 0;
  226. }
  227. static int pch_wpt_resume(struct pch_thermal_device *ptd)
  228. {
  229. u8 tsel;
  230. if (ptd->bios_enabled)
  231. return 0;
  232. tsel = readb(ptd->hw_base + WPT_TSEL);
  233. writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
  234. return 0;
  235. }
  236. struct pch_dev_ops {
  237. int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
  238. int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
  239. int (*suspend)(struct pch_thermal_device *ptd);
  240. int (*resume)(struct pch_thermal_device *ptd);
  241. };
  242. /* dev ops for Wildcat Point */
  243. static const struct pch_dev_ops pch_dev_ops_wpt = {
  244. .hw_init = pch_wpt_init,
  245. .get_temp = pch_wpt_get_temp,
  246. .suspend = pch_wpt_suspend,
  247. .resume = pch_wpt_resume,
  248. };
  249. static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
  250. {
  251. struct pch_thermal_device *ptd = tzd->devdata;
  252. return ptd->ops->get_temp(ptd, temp);
  253. }
  254. static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
  255. enum thermal_trip_type *type)
  256. {
  257. struct pch_thermal_device *ptd = tzd->devdata;
  258. if (ptd->crt_trip_id == trip)
  259. *type = THERMAL_TRIP_CRITICAL;
  260. else if (ptd->hot_trip_id == trip)
  261. *type = THERMAL_TRIP_HOT;
  262. else if (ptd->psv_trip_id == trip)
  263. *type = THERMAL_TRIP_PASSIVE;
  264. else
  265. return -EINVAL;
  266. return 0;
  267. }
  268. static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
  269. {
  270. struct pch_thermal_device *ptd = tzd->devdata;
  271. if (ptd->crt_trip_id == trip)
  272. *temp = ptd->crt_temp;
  273. else if (ptd->hot_trip_id == trip)
  274. *temp = ptd->hot_temp;
  275. else if (ptd->psv_trip_id == trip)
  276. *temp = ptd->psv_temp;
  277. else
  278. return -EINVAL;
  279. return 0;
  280. }
  281. static void pch_critical(struct thermal_zone_device *tzd)
  282. {
  283. dev_dbg(&tzd->device, "%s: critical temperature reached\n", tzd->type);
  284. }
  285. static struct thermal_zone_device_ops tzd_ops = {
  286. .get_temp = pch_thermal_get_temp,
  287. .get_trip_type = pch_get_trip_type,
  288. .get_trip_temp = pch_get_trip_temp,
  289. .critical = pch_critical,
  290. };
  291. enum board_ids {
  292. board_hsw,
  293. board_wpt,
  294. board_skl,
  295. board_cnl,
  296. board_cml,
  297. board_lwb,
  298. board_wbg,
  299. };
  300. static const struct board_info {
  301. const char *name;
  302. const struct pch_dev_ops *ops;
  303. } board_info[] = {
  304. [board_hsw] = {
  305. .name = "pch_haswell",
  306. .ops = &pch_dev_ops_wpt,
  307. },
  308. [board_wpt] = {
  309. .name = "pch_wildcat_point",
  310. .ops = &pch_dev_ops_wpt,
  311. },
  312. [board_skl] = {
  313. .name = "pch_skylake",
  314. .ops = &pch_dev_ops_wpt,
  315. },
  316. [board_cnl] = {
  317. .name = "pch_cannonlake",
  318. .ops = &pch_dev_ops_wpt,
  319. },
  320. [board_cml] = {
  321. .name = "pch_cometlake",
  322. .ops = &pch_dev_ops_wpt,
  323. },
  324. [board_lwb] = {
  325. .name = "pch_lewisburg",
  326. .ops = &pch_dev_ops_wpt,
  327. },
  328. [board_wbg] = {
  329. .name = "pch_wellsburg",
  330. .ops = &pch_dev_ops_wpt,
  331. },
  332. };
  333. static int intel_pch_thermal_probe(struct pci_dev *pdev,
  334. const struct pci_device_id *id)
  335. {
  336. enum board_ids board_id = id->driver_data;
  337. const struct board_info *bi = &board_info[board_id];
  338. struct pch_thermal_device *ptd;
  339. int err;
  340. int nr_trips;
  341. ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
  342. if (!ptd)
  343. return -ENOMEM;
  344. ptd->ops = bi->ops;
  345. pci_set_drvdata(pdev, ptd);
  346. ptd->pdev = pdev;
  347. err = pci_enable_device(pdev);
  348. if (err) {
  349. dev_err(&pdev->dev, "failed to enable pci device\n");
  350. return err;
  351. }
  352. err = pci_request_regions(pdev, driver_name);
  353. if (err) {
  354. dev_err(&pdev->dev, "failed to request pci region\n");
  355. goto error_disable;
  356. }
  357. ptd->hw_base = pci_ioremap_bar(pdev, 0);
  358. if (!ptd->hw_base) {
  359. err = -ENOMEM;
  360. dev_err(&pdev->dev, "failed to map mem base\n");
  361. goto error_release;
  362. }
  363. err = ptd->ops->hw_init(ptd, &nr_trips);
  364. if (err)
  365. goto error_cleanup;
  366. ptd->tzd = thermal_zone_device_register(bi->name, nr_trips, 0, ptd,
  367. &tzd_ops, NULL, 0, 0);
  368. if (IS_ERR(ptd->tzd)) {
  369. dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
  370. bi->name);
  371. err = PTR_ERR(ptd->tzd);
  372. goto error_cleanup;
  373. }
  374. err = thermal_zone_device_enable(ptd->tzd);
  375. if (err)
  376. goto err_unregister;
  377. return 0;
  378. err_unregister:
  379. thermal_zone_device_unregister(ptd->tzd);
  380. error_cleanup:
  381. iounmap(ptd->hw_base);
  382. error_release:
  383. pci_release_regions(pdev);
  384. error_disable:
  385. pci_disable_device(pdev);
  386. dev_err(&pdev->dev, "pci device failed to probe\n");
  387. return err;
  388. }
  389. static void intel_pch_thermal_remove(struct pci_dev *pdev)
  390. {
  391. struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
  392. thermal_zone_device_unregister(ptd->tzd);
  393. iounmap(ptd->hw_base);
  394. pci_set_drvdata(pdev, NULL);
  395. pci_release_regions(pdev);
  396. pci_disable_device(pdev);
  397. }
  398. static int intel_pch_thermal_suspend_noirq(struct device *device)
  399. {
  400. struct pch_thermal_device *ptd = dev_get_drvdata(device);
  401. return ptd->ops->suspend(ptd);
  402. }
  403. static int intel_pch_thermal_resume(struct device *device)
  404. {
  405. struct pch_thermal_device *ptd = dev_get_drvdata(device);
  406. return ptd->ops->resume(ptd);
  407. }
  408. static const struct pci_device_id intel_pch_thermal_id[] = {
  409. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
  410. .driver_data = board_hsw, },
  411. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
  412. .driver_data = board_hsw, },
  413. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
  414. .driver_data = board_wpt, },
  415. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
  416. .driver_data = board_skl, },
  417. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
  418. .driver_data = board_skl, },
  419. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL),
  420. .driver_data = board_cnl, },
  421. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_H),
  422. .driver_data = board_cnl, },
  423. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_LP),
  424. .driver_data = board_cnl, },
  425. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CML_H),
  426. .driver_data = board_cml, },
  427. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_LWB),
  428. .driver_data = board_lwb, },
  429. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WBG),
  430. .driver_data = board_wbg, },
  431. { 0, },
  432. };
  433. MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
  434. static const struct dev_pm_ops intel_pch_pm_ops = {
  435. .suspend_noirq = intel_pch_thermal_suspend_noirq,
  436. .resume = intel_pch_thermal_resume,
  437. };
  438. static struct pci_driver intel_pch_thermal_driver = {
  439. .name = "intel_pch_thermal",
  440. .id_table = intel_pch_thermal_id,
  441. .probe = intel_pch_thermal_probe,
  442. .remove = intel_pch_thermal_remove,
  443. .driver.pm = &intel_pch_pm_ops,
  444. };
  445. module_pci_driver(intel_pch_thermal_driver);
  446. MODULE_LICENSE("GPL v2");
  447. MODULE_DESCRIPTION("Intel PCH Thermal driver");