processor_thermal_device.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/platform_device.h>
  20. #include <linux/acpi.h>
  21. /* Broadwell-U/HSB thermal reporting device */
  22. #define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603
  23. #define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03
  24. /* Braswell thermal reporting device */
  25. #define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC
  26. struct power_config {
  27. u32 index;
  28. u32 min_uw;
  29. u32 max_uw;
  30. u32 tmin_us;
  31. u32 tmax_us;
  32. u32 step_uw;
  33. };
  34. struct proc_thermal_device {
  35. struct device *dev;
  36. struct acpi_device *adev;
  37. struct power_config power_limits[2];
  38. };
  39. enum proc_thermal_emum_mode_type {
  40. PROC_THERMAL_NONE,
  41. PROC_THERMAL_PCI,
  42. PROC_THERMAL_PLATFORM_DEV
  43. };
  44. /*
  45. * We can have only one type of enumeration, PCI or Platform,
  46. * not both. So we don't need instance specific data.
  47. */
  48. static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
  49. PROC_THERMAL_NONE;
  50. #define POWER_LIMIT_SHOW(index, suffix) \
  51. static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
  52. struct device_attribute *attr, \
  53. char *buf) \
  54. { \
  55. struct pci_dev *pci_dev; \
  56. struct platform_device *pdev; \
  57. struct proc_thermal_device *proc_dev; \
  58. \
  59. if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
  60. pdev = to_platform_device(dev); \
  61. proc_dev = platform_get_drvdata(pdev); \
  62. } else { \
  63. pci_dev = to_pci_dev(dev); \
  64. proc_dev = pci_get_drvdata(pci_dev); \
  65. } \
  66. return sprintf(buf, "%lu\n",\
  67. (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
  68. }
  69. POWER_LIMIT_SHOW(0, min_uw)
  70. POWER_LIMIT_SHOW(0, max_uw)
  71. POWER_LIMIT_SHOW(0, step_uw)
  72. POWER_LIMIT_SHOW(0, tmin_us)
  73. POWER_LIMIT_SHOW(0, tmax_us)
  74. POWER_LIMIT_SHOW(1, min_uw)
  75. POWER_LIMIT_SHOW(1, max_uw)
  76. POWER_LIMIT_SHOW(1, step_uw)
  77. POWER_LIMIT_SHOW(1, tmin_us)
  78. POWER_LIMIT_SHOW(1, tmax_us)
  79. static DEVICE_ATTR_RO(power_limit_0_min_uw);
  80. static DEVICE_ATTR_RO(power_limit_0_max_uw);
  81. static DEVICE_ATTR_RO(power_limit_0_step_uw);
  82. static DEVICE_ATTR_RO(power_limit_0_tmin_us);
  83. static DEVICE_ATTR_RO(power_limit_0_tmax_us);
  84. static DEVICE_ATTR_RO(power_limit_1_min_uw);
  85. static DEVICE_ATTR_RO(power_limit_1_max_uw);
  86. static DEVICE_ATTR_RO(power_limit_1_step_uw);
  87. static DEVICE_ATTR_RO(power_limit_1_tmin_us);
  88. static DEVICE_ATTR_RO(power_limit_1_tmax_us);
  89. static struct attribute *power_limit_attrs[] = {
  90. &dev_attr_power_limit_0_min_uw.attr,
  91. &dev_attr_power_limit_1_min_uw.attr,
  92. &dev_attr_power_limit_0_max_uw.attr,
  93. &dev_attr_power_limit_1_max_uw.attr,
  94. &dev_attr_power_limit_0_step_uw.attr,
  95. &dev_attr_power_limit_1_step_uw.attr,
  96. &dev_attr_power_limit_0_tmin_us.attr,
  97. &dev_attr_power_limit_1_tmin_us.attr,
  98. &dev_attr_power_limit_0_tmax_us.attr,
  99. &dev_attr_power_limit_1_tmax_us.attr,
  100. NULL
  101. };
  102. static struct attribute_group power_limit_attribute_group = {
  103. .attrs = power_limit_attrs,
  104. .name = "power_limits"
  105. };
  106. static int proc_thermal_add(struct device *dev,
  107. struct proc_thermal_device **priv)
  108. {
  109. struct proc_thermal_device *proc_priv;
  110. struct acpi_device *adev;
  111. acpi_status status;
  112. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  113. union acpi_object *elements, *ppcc;
  114. union acpi_object *p;
  115. int i;
  116. int ret;
  117. adev = ACPI_COMPANION(dev);
  118. if (!adev)
  119. return -ENODEV;
  120. status = acpi_evaluate_object(adev->handle, "PPCC", NULL, &buf);
  121. if (ACPI_FAILURE(status))
  122. return -ENODEV;
  123. p = buf.pointer;
  124. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  125. dev_err(dev, "Invalid PPCC data\n");
  126. ret = -EFAULT;
  127. goto free_buffer;
  128. }
  129. if (!p->package.count) {
  130. dev_err(dev, "Invalid PPCC package size\n");
  131. ret = -EFAULT;
  132. goto free_buffer;
  133. }
  134. proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
  135. if (!proc_priv) {
  136. ret = -ENOMEM;
  137. goto free_buffer;
  138. }
  139. proc_priv->dev = dev;
  140. proc_priv->adev = adev;
  141. for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
  142. elements = &(p->package.elements[i+1]);
  143. if (elements->type != ACPI_TYPE_PACKAGE ||
  144. elements->package.count != 6) {
  145. ret = -EFAULT;
  146. goto free_buffer;
  147. }
  148. ppcc = elements->package.elements;
  149. proc_priv->power_limits[i].index = ppcc[0].integer.value;
  150. proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
  151. proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
  152. proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
  153. proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
  154. proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
  155. }
  156. *priv = proc_priv;
  157. ret = sysfs_create_group(&dev->kobj,
  158. &power_limit_attribute_group);
  159. free_buffer:
  160. kfree(buf.pointer);
  161. return ret;
  162. }
  163. void proc_thermal_remove(struct proc_thermal_device *proc_priv)
  164. {
  165. sysfs_remove_group(&proc_priv->dev->kobj,
  166. &power_limit_attribute_group);
  167. }
  168. static int int3401_add(struct platform_device *pdev)
  169. {
  170. struct proc_thermal_device *proc_priv;
  171. int ret;
  172. if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
  173. dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
  174. return -ENODEV;
  175. }
  176. ret = proc_thermal_add(&pdev->dev, &proc_priv);
  177. if (ret)
  178. return ret;
  179. platform_set_drvdata(pdev, proc_priv);
  180. proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
  181. return 0;
  182. }
  183. static int int3401_remove(struct platform_device *pdev)
  184. {
  185. proc_thermal_remove(platform_get_drvdata(pdev));
  186. return 0;
  187. }
  188. static int proc_thermal_pci_probe(struct pci_dev *pdev,
  189. const struct pci_device_id *unused)
  190. {
  191. struct proc_thermal_device *proc_priv;
  192. int ret;
  193. if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
  194. dev_err(&pdev->dev, "error: enumerated as platform dev\n");
  195. return -ENODEV;
  196. }
  197. ret = pci_enable_device(pdev);
  198. if (ret < 0) {
  199. dev_err(&pdev->dev, "error: could not enable device\n");
  200. return ret;
  201. }
  202. ret = proc_thermal_add(&pdev->dev, &proc_priv);
  203. if (ret) {
  204. pci_disable_device(pdev);
  205. return ret;
  206. }
  207. pci_set_drvdata(pdev, proc_priv);
  208. proc_thermal_emum_mode = PROC_THERMAL_PCI;
  209. return 0;
  210. }
  211. static void proc_thermal_pci_remove(struct pci_dev *pdev)
  212. {
  213. proc_thermal_remove(pci_get_drvdata(pdev));
  214. pci_disable_device(pdev);
  215. }
  216. static const struct pci_device_id proc_thermal_pci_ids[] = {
  217. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
  218. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
  219. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
  220. { 0, },
  221. };
  222. MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
  223. static struct pci_driver proc_thermal_pci_driver = {
  224. .name = "proc_thermal",
  225. .probe = proc_thermal_pci_probe,
  226. .remove = proc_thermal_pci_remove,
  227. .id_table = proc_thermal_pci_ids,
  228. };
  229. static const struct acpi_device_id int3401_device_ids[] = {
  230. {"INT3401", 0},
  231. {"", 0},
  232. };
  233. MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
  234. static struct platform_driver int3401_driver = {
  235. .probe = int3401_add,
  236. .remove = int3401_remove,
  237. .driver = {
  238. .name = "int3401 thermal",
  239. .acpi_match_table = int3401_device_ids,
  240. },
  241. };
  242. static int __init proc_thermal_init(void)
  243. {
  244. int ret;
  245. ret = platform_driver_register(&int3401_driver);
  246. if (ret)
  247. return ret;
  248. ret = pci_register_driver(&proc_thermal_pci_driver);
  249. return ret;
  250. }
  251. static void __exit proc_thermal_exit(void)
  252. {
  253. platform_driver_unregister(&int3401_driver);
  254. pci_unregister_driver(&proc_thermal_pci_driver);
  255. }
  256. module_init(proc_thermal_init);
  257. module_exit(proc_thermal_exit);
  258. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  259. MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
  260. MODULE_LICENSE("GPL v2");