processor_thermal_device.c 11 KB

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