processor_thermal_device.c 13 KB

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