processor_thermal_device.c 13 KB

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