processor_thermal_device.c 13 KB

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