processor_thermal_device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * processor_thermal_device.c
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/pci.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/acpi.h>
  22. #include <linux/thermal.h>
  23. #include "int340x_thermal_zone.h"
  24. #include "../intel_soc_dts_iosf.h"
  25. /* Broadwell-U/HSB thermal reporting device */
  26. #define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603
  27. #define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03
  28. /* Skylake thermal reporting device */
  29. #define PCI_DEVICE_ID_PROC_SKL_THERMAL 0x1903
  30. /* CannonLake thermal reporting device */
  31. #define PCI_DEVICE_ID_PROC_CNL_THERMAL 0x5a03
  32. #define PCI_DEVICE_ID_PROC_CFL_THERMAL 0x3E83
  33. /* Braswell thermal reporting device */
  34. #define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC
  35. /* Broxton thermal reporting device */
  36. #define PCI_DEVICE_ID_PROC_BXT0_THERMAL 0x0A8C
  37. #define PCI_DEVICE_ID_PROC_BXT1_THERMAL 0x1A8C
  38. #define PCI_DEVICE_ID_PROC_BXTX_THERMAL 0x4A8C
  39. #define PCI_DEVICE_ID_PROC_BXTP_THERMAL 0x5A8C
  40. struct power_config {
  41. u32 index;
  42. u32 min_uw;
  43. u32 max_uw;
  44. u32 tmin_us;
  45. u32 tmax_us;
  46. u32 step_uw;
  47. };
  48. struct proc_thermal_device {
  49. struct device *dev;
  50. struct acpi_device *adev;
  51. struct power_config power_limits[2];
  52. struct int34x_thermal_zone *int340x_zone;
  53. struct intel_soc_dts_sensors *soc_dts;
  54. };
  55. enum proc_thermal_emum_mode_type {
  56. PROC_THERMAL_NONE,
  57. PROC_THERMAL_PCI,
  58. PROC_THERMAL_PLATFORM_DEV
  59. };
  60. /*
  61. * We can have only one type of enumeration, PCI or Platform,
  62. * not both. So we don't need instance specific data.
  63. */
  64. static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
  65. PROC_THERMAL_NONE;
  66. #define POWER_LIMIT_SHOW(index, suffix) \
  67. static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
  68. struct device_attribute *attr, \
  69. char *buf) \
  70. { \
  71. struct pci_dev *pci_dev; \
  72. struct platform_device *pdev; \
  73. struct proc_thermal_device *proc_dev; \
  74. \
  75. if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
  76. pdev = to_platform_device(dev); \
  77. proc_dev = platform_get_drvdata(pdev); \
  78. } else { \
  79. pci_dev = to_pci_dev(dev); \
  80. proc_dev = pci_get_drvdata(pci_dev); \
  81. } \
  82. return sprintf(buf, "%lu\n",\
  83. (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
  84. }
  85. POWER_LIMIT_SHOW(0, min_uw)
  86. POWER_LIMIT_SHOW(0, max_uw)
  87. POWER_LIMIT_SHOW(0, step_uw)
  88. POWER_LIMIT_SHOW(0, tmin_us)
  89. POWER_LIMIT_SHOW(0, tmax_us)
  90. POWER_LIMIT_SHOW(1, min_uw)
  91. POWER_LIMIT_SHOW(1, max_uw)
  92. POWER_LIMIT_SHOW(1, step_uw)
  93. POWER_LIMIT_SHOW(1, tmin_us)
  94. POWER_LIMIT_SHOW(1, tmax_us)
  95. static DEVICE_ATTR_RO(power_limit_0_min_uw);
  96. static DEVICE_ATTR_RO(power_limit_0_max_uw);
  97. static DEVICE_ATTR_RO(power_limit_0_step_uw);
  98. static DEVICE_ATTR_RO(power_limit_0_tmin_us);
  99. static DEVICE_ATTR_RO(power_limit_0_tmax_us);
  100. static DEVICE_ATTR_RO(power_limit_1_min_uw);
  101. static DEVICE_ATTR_RO(power_limit_1_max_uw);
  102. static DEVICE_ATTR_RO(power_limit_1_step_uw);
  103. static DEVICE_ATTR_RO(power_limit_1_tmin_us);
  104. static DEVICE_ATTR_RO(power_limit_1_tmax_us);
  105. static struct attribute *power_limit_attrs[] = {
  106. &dev_attr_power_limit_0_min_uw.attr,
  107. &dev_attr_power_limit_1_min_uw.attr,
  108. &dev_attr_power_limit_0_max_uw.attr,
  109. &dev_attr_power_limit_1_max_uw.attr,
  110. &dev_attr_power_limit_0_step_uw.attr,
  111. &dev_attr_power_limit_1_step_uw.attr,
  112. &dev_attr_power_limit_0_tmin_us.attr,
  113. &dev_attr_power_limit_1_tmin_us.attr,
  114. &dev_attr_power_limit_0_tmax_us.attr,
  115. &dev_attr_power_limit_1_tmax_us.attr,
  116. NULL
  117. };
  118. static const struct attribute_group power_limit_attribute_group = {
  119. .attrs = power_limit_attrs,
  120. .name = "power_limits"
  121. };
  122. static int stored_tjmax; /* since it is fixed, we can have local storage */
  123. static int get_tjmax(void)
  124. {
  125. u32 eax, edx;
  126. u32 val;
  127. int err;
  128. err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  129. if (err)
  130. return err;
  131. val = (eax >> 16) & 0xff;
  132. if (val)
  133. return val;
  134. return -EINVAL;
  135. }
  136. static int read_temp_msr(int *temp)
  137. {
  138. int cpu;
  139. u32 eax, edx;
  140. int err;
  141. unsigned long curr_temp_off = 0;
  142. *temp = 0;
  143. for_each_online_cpu(cpu) {
  144. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
  145. &edx);
  146. if (err)
  147. goto err_ret;
  148. else {
  149. if (eax & 0x80000000) {
  150. curr_temp_off = (eax >> 16) & 0x7f;
  151. if (!*temp || curr_temp_off < *temp)
  152. *temp = curr_temp_off;
  153. } else {
  154. err = -EINVAL;
  155. goto err_ret;
  156. }
  157. }
  158. }
  159. return 0;
  160. err_ret:
  161. return err;
  162. }
  163. static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
  164. int *temp)
  165. {
  166. int ret;
  167. ret = read_temp_msr(temp);
  168. if (!ret)
  169. *temp = (stored_tjmax - *temp) * 1000;
  170. return ret;
  171. }
  172. static struct thermal_zone_device_ops proc_thermal_local_ops = {
  173. .get_temp = proc_thermal_get_zone_temp,
  174. };
  175. static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
  176. {
  177. int i;
  178. acpi_status status;
  179. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  180. union acpi_object *elements, *ppcc;
  181. union acpi_object *p;
  182. int ret = 0;
  183. status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
  184. NULL, &buf);
  185. if (ACPI_FAILURE(status))
  186. return -ENODEV;
  187. p = buf.pointer;
  188. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  189. dev_err(proc_priv->dev, "Invalid PPCC data\n");
  190. ret = -EFAULT;
  191. goto free_buffer;
  192. }
  193. if (!p->package.count) {
  194. dev_err(proc_priv->dev, "Invalid PPCC package size\n");
  195. ret = -EFAULT;
  196. goto free_buffer;
  197. }
  198. for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
  199. elements = &(p->package.elements[i+1]);
  200. if (elements->type != ACPI_TYPE_PACKAGE ||
  201. elements->package.count != 6) {
  202. ret = -EFAULT;
  203. goto free_buffer;
  204. }
  205. ppcc = elements->package.elements;
  206. proc_priv->power_limits[i].index = ppcc[0].integer.value;
  207. proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
  208. proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
  209. proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
  210. proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
  211. proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
  212. }
  213. free_buffer:
  214. kfree(buf.pointer);
  215. return ret;
  216. }
  217. #define PROC_POWER_CAPABILITY_CHANGED 0x83
  218. static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
  219. {
  220. struct proc_thermal_device *proc_priv = data;
  221. if (!proc_priv)
  222. return;
  223. switch (event) {
  224. case PROC_POWER_CAPABILITY_CHANGED:
  225. proc_thermal_read_ppcc(proc_priv);
  226. int340x_thermal_zone_device_update(proc_priv->int340x_zone,
  227. THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
  228. break;
  229. default:
  230. dev_err(proc_priv->dev, "Unsupported event [0x%x]\n", event);
  231. break;
  232. }
  233. }
  234. static int proc_thermal_add(struct device *dev,
  235. struct proc_thermal_device **priv)
  236. {
  237. struct proc_thermal_device *proc_priv;
  238. struct acpi_device *adev;
  239. acpi_status status;
  240. unsigned long long tmp;
  241. struct thermal_zone_device_ops *ops = NULL;
  242. int ret;
  243. adev = ACPI_COMPANION(dev);
  244. if (!adev)
  245. return -ENODEV;
  246. proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
  247. if (!proc_priv)
  248. return -ENOMEM;
  249. proc_priv->dev = dev;
  250. proc_priv->adev = adev;
  251. *priv = proc_priv;
  252. ret = proc_thermal_read_ppcc(proc_priv);
  253. if (!ret) {
  254. ret = sysfs_create_group(&dev->kobj,
  255. &power_limit_attribute_group);
  256. }
  257. if (ret)
  258. return ret;
  259. status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
  260. if (ACPI_FAILURE(status)) {
  261. /* there is no _TMP method, add local method */
  262. stored_tjmax = get_tjmax();
  263. if (stored_tjmax > 0)
  264. ops = &proc_thermal_local_ops;
  265. }
  266. proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
  267. if (IS_ERR(proc_priv->int340x_zone)) {
  268. ret = PTR_ERR(proc_priv->int340x_zone);
  269. goto remove_group;
  270. } else
  271. ret = 0;
  272. ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
  273. proc_thermal_notify,
  274. (void *)proc_priv);
  275. if (ret)
  276. goto remove_zone;
  277. return 0;
  278. remove_zone:
  279. int340x_thermal_zone_remove(proc_priv->int340x_zone);
  280. remove_group:
  281. sysfs_remove_group(&proc_priv->dev->kobj,
  282. &power_limit_attribute_group);
  283. return ret;
  284. }
  285. static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
  286. {
  287. acpi_remove_notify_handler(proc_priv->adev->handle,
  288. ACPI_DEVICE_NOTIFY, proc_thermal_notify);
  289. int340x_thermal_zone_remove(proc_priv->int340x_zone);
  290. sysfs_remove_group(&proc_priv->dev->kobj,
  291. &power_limit_attribute_group);
  292. }
  293. static int int3401_add(struct platform_device *pdev)
  294. {
  295. struct proc_thermal_device *proc_priv;
  296. int ret;
  297. if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
  298. dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
  299. return -ENODEV;
  300. }
  301. ret = proc_thermal_add(&pdev->dev, &proc_priv);
  302. if (ret)
  303. return ret;
  304. platform_set_drvdata(pdev, proc_priv);
  305. proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
  306. return 0;
  307. }
  308. static int int3401_remove(struct platform_device *pdev)
  309. {
  310. proc_thermal_remove(platform_get_drvdata(pdev));
  311. return 0;
  312. }
  313. static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
  314. {
  315. struct proc_thermal_device *proc_priv;
  316. struct pci_dev *pdev = devid;
  317. proc_priv = pci_get_drvdata(pdev);
  318. intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
  319. return IRQ_HANDLED;
  320. }
  321. static int proc_thermal_pci_probe(struct pci_dev *pdev,
  322. const struct pci_device_id *unused)
  323. {
  324. struct proc_thermal_device *proc_priv;
  325. int ret;
  326. if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
  327. dev_err(&pdev->dev, "error: enumerated as platform dev\n");
  328. return -ENODEV;
  329. }
  330. ret = pci_enable_device(pdev);
  331. if (ret < 0) {
  332. dev_err(&pdev->dev, "error: could not enable device\n");
  333. return ret;
  334. }
  335. ret = proc_thermal_add(&pdev->dev, &proc_priv);
  336. if (ret) {
  337. pci_disable_device(pdev);
  338. return ret;
  339. }
  340. pci_set_drvdata(pdev, proc_priv);
  341. proc_thermal_emum_mode = PROC_THERMAL_PCI;
  342. if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
  343. /*
  344. * Enumerate additional DTS sensors available via IOSF.
  345. * But we are not treating as a failure condition, if
  346. * there are no aux DTSs enabled or fails. This driver
  347. * already exposes sensors, which can be accessed via
  348. * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
  349. */
  350. proc_priv->soc_dts = intel_soc_dts_iosf_init(
  351. INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
  352. if (proc_priv->soc_dts && pdev->irq) {
  353. ret = pci_enable_msi(pdev);
  354. if (!ret) {
  355. ret = request_threaded_irq(pdev->irq, NULL,
  356. proc_thermal_pci_msi_irq,
  357. IRQF_ONESHOT, "proc_thermal",
  358. pdev);
  359. if (ret) {
  360. intel_soc_dts_iosf_exit(
  361. proc_priv->soc_dts);
  362. pci_disable_msi(pdev);
  363. proc_priv->soc_dts = NULL;
  364. }
  365. }
  366. } else
  367. dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
  368. }
  369. return 0;
  370. }
  371. static void proc_thermal_pci_remove(struct pci_dev *pdev)
  372. {
  373. struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
  374. if (proc_priv->soc_dts) {
  375. intel_soc_dts_iosf_exit(proc_priv->soc_dts);
  376. if (pdev->irq) {
  377. free_irq(pdev->irq, pdev);
  378. pci_disable_msi(pdev);
  379. }
  380. }
  381. proc_thermal_remove(proc_priv);
  382. pci_disable_device(pdev);
  383. }
  384. static const struct pci_device_id proc_thermal_pci_ids[] = {
  385. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
  386. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
  387. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)},
  388. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
  389. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
  390. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
  391. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
  392. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
  393. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)},
  394. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)},
  395. { 0, },
  396. };
  397. MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
  398. static struct pci_driver proc_thermal_pci_driver = {
  399. .name = "proc_thermal",
  400. .probe = proc_thermal_pci_probe,
  401. .remove = proc_thermal_pci_remove,
  402. .id_table = proc_thermal_pci_ids,
  403. };
  404. static const struct acpi_device_id int3401_device_ids[] = {
  405. {"INT3401", 0},
  406. {"", 0},
  407. };
  408. MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
  409. static struct platform_driver int3401_driver = {
  410. .probe = int3401_add,
  411. .remove = int3401_remove,
  412. .driver = {
  413. .name = "int3401 thermal",
  414. .acpi_match_table = int3401_device_ids,
  415. },
  416. };
  417. static int __init proc_thermal_init(void)
  418. {
  419. int ret;
  420. ret = platform_driver_register(&int3401_driver);
  421. if (ret)
  422. return ret;
  423. ret = pci_register_driver(&proc_thermal_pci_driver);
  424. return ret;
  425. }
  426. static void __exit proc_thermal_exit(void)
  427. {
  428. platform_driver_unregister(&int3401_driver);
  429. pci_unregister_driver(&proc_thermal_pci_driver);
  430. }
  431. module_init(proc_thermal_init);
  432. module_exit(proc_thermal_exit);
  433. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  434. MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
  435. MODULE_LICENSE("GPL v2");