2 * processor_thermal_device.c
3 * Copyright (c) 2014, Intel Corporation.
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.
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
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"
26 /* Broadwell-U/HSB thermal reporting device */
27 #define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603
28 #define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03
30 /* Skylake thermal reporting device */
31 #define PCI_DEVICE_ID_PROC_SKL_THERMAL 0x1903
33 /* Braswell thermal reporting device */
34 #define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC
36 /* Broxton thermal reporting device */
37 #define PCI_DEVICE_ID_PROC_BXT0_THERMAL 0x0A8C
38 #define PCI_DEVICE_ID_PROC_BXT1_THERMAL 0x1A8C
39 #define PCI_DEVICE_ID_PROC_BXTX_THERMAL 0x4A8C
40 #define PCI_DEVICE_ID_PROC_BXTP_THERMAL 0x5A8C
51 struct proc_thermal_device {
53 struct acpi_device *adev;
54 struct power_config power_limits[2];
55 struct int34x_thermal_zone *int340x_zone;
56 struct intel_soc_dts_sensors *soc_dts;
59 enum proc_thermal_emum_mode_type {
62 PROC_THERMAL_PLATFORM_DEV
66 * We can have only one type of enumeration, PCI or Platform,
67 * not both. So we don't need instance specific data.
69 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
72 #define POWER_LIMIT_SHOW(index, suffix) \
73 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
74 struct device_attribute *attr, \
77 struct pci_dev *pci_dev; \
78 struct platform_device *pdev; \
79 struct proc_thermal_device *proc_dev; \
81 if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \
82 dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \
86 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
87 pdev = to_platform_device(dev); \
88 proc_dev = platform_get_drvdata(pdev); \
90 pci_dev = to_pci_dev(dev); \
91 proc_dev = pci_get_drvdata(pci_dev); \
93 return sprintf(buf, "%lu\n",\
94 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
97 POWER_LIMIT_SHOW(0, min_uw)
98 POWER_LIMIT_SHOW(0, max_uw)
99 POWER_LIMIT_SHOW(0, step_uw)
100 POWER_LIMIT_SHOW(0, tmin_us)
101 POWER_LIMIT_SHOW(0, tmax_us)
103 POWER_LIMIT_SHOW(1, min_uw)
104 POWER_LIMIT_SHOW(1, max_uw)
105 POWER_LIMIT_SHOW(1, step_uw)
106 POWER_LIMIT_SHOW(1, tmin_us)
107 POWER_LIMIT_SHOW(1, tmax_us)
109 static DEVICE_ATTR_RO(power_limit_0_min_uw);
110 static DEVICE_ATTR_RO(power_limit_0_max_uw);
111 static DEVICE_ATTR_RO(power_limit_0_step_uw);
112 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
113 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
115 static DEVICE_ATTR_RO(power_limit_1_min_uw);
116 static DEVICE_ATTR_RO(power_limit_1_max_uw);
117 static DEVICE_ATTR_RO(power_limit_1_step_uw);
118 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
119 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
121 static struct attribute *power_limit_attrs[] = {
122 &dev_attr_power_limit_0_min_uw.attr,
123 &dev_attr_power_limit_1_min_uw.attr,
124 &dev_attr_power_limit_0_max_uw.attr,
125 &dev_attr_power_limit_1_max_uw.attr,
126 &dev_attr_power_limit_0_step_uw.attr,
127 &dev_attr_power_limit_1_step_uw.attr,
128 &dev_attr_power_limit_0_tmin_us.attr,
129 &dev_attr_power_limit_1_tmin_us.attr,
130 &dev_attr_power_limit_0_tmax_us.attr,
131 &dev_attr_power_limit_1_tmax_us.attr,
135 static const struct attribute_group power_limit_attribute_group = {
136 .attrs = power_limit_attrs,
137 .name = "power_limits"
140 static int stored_tjmax; /* since it is fixed, we can have local storage */
142 static int get_tjmax(void)
148 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
152 val = (eax >> 16) & 0xff;
159 static int read_temp_msr(int *temp)
164 unsigned long curr_temp_off = 0;
168 for_each_online_cpu(cpu) {
169 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
174 if (eax & 0x80000000) {
175 curr_temp_off = (eax >> 16) & 0x7f;
176 if (!*temp || curr_temp_off < *temp)
177 *temp = curr_temp_off;
190 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
195 ret = read_temp_msr(temp);
197 *temp = (stored_tjmax - *temp) * 1000;
202 static struct thermal_zone_device_ops proc_thermal_local_ops = {
203 .get_temp = proc_thermal_get_zone_temp,
206 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
210 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
211 union acpi_object *elements, *ppcc;
212 union acpi_object *p;
215 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
217 if (ACPI_FAILURE(status))
221 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
222 dev_err(proc_priv->dev, "Invalid PPCC data\n");
227 if (!p->package.count) {
228 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
233 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
234 elements = &(p->package.elements[i+1]);
235 if (elements->type != ACPI_TYPE_PACKAGE ||
236 elements->package.count != 6) {
240 ppcc = elements->package.elements;
241 proc_priv->power_limits[i].index = ppcc[0].integer.value;
242 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
243 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
244 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
245 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
246 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
255 #define PROC_POWER_CAPABILITY_CHANGED 0x83
256 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
258 struct proc_thermal_device *proc_priv = data;
264 case PROC_POWER_CAPABILITY_CHANGED:
265 proc_thermal_read_ppcc(proc_priv);
266 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
267 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
270 dev_err(proc_priv->dev, "Unsupported event [0x%x]\n", event);
276 static int proc_thermal_add(struct device *dev,
277 struct proc_thermal_device **priv)
279 struct proc_thermal_device *proc_priv;
280 struct acpi_device *adev;
282 unsigned long long tmp;
283 struct thermal_zone_device_ops *ops = NULL;
286 adev = ACPI_COMPANION(dev);
290 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
294 proc_priv->dev = dev;
295 proc_priv->adev = adev;
298 ret = proc_thermal_read_ppcc(proc_priv);
302 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
303 if (ACPI_FAILURE(status)) {
304 /* there is no _TMP method, add local method */
305 stored_tjmax = get_tjmax();
306 if (stored_tjmax > 0)
307 ops = &proc_thermal_local_ops;
310 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
311 if (IS_ERR(proc_priv->int340x_zone)) {
312 return PTR_ERR(proc_priv->int340x_zone);
316 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
325 int340x_thermal_zone_remove(proc_priv->int340x_zone);
330 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
332 acpi_remove_notify_handler(proc_priv->adev->handle,
333 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
334 int340x_thermal_zone_remove(proc_priv->int340x_zone);
335 sysfs_remove_group(&proc_priv->dev->kobj,
336 &power_limit_attribute_group);
339 static int int3401_add(struct platform_device *pdev)
341 struct proc_thermal_device *proc_priv;
344 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
345 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
349 ret = proc_thermal_add(&pdev->dev, &proc_priv);
353 platform_set_drvdata(pdev, proc_priv);
354 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
356 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
358 return sysfs_create_group(&pdev->dev.kobj,
359 &power_limit_attribute_group);
362 static int int3401_remove(struct platform_device *pdev)
364 proc_thermal_remove(platform_get_drvdata(pdev));
369 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
371 struct proc_thermal_device *proc_priv;
372 struct pci_dev *pdev = devid;
374 proc_priv = pci_get_drvdata(pdev);
376 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
381 static int proc_thermal_pci_probe(struct pci_dev *pdev,
382 const struct pci_device_id *unused)
384 struct proc_thermal_device *proc_priv;
387 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
388 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
392 ret = pci_enable_device(pdev);
394 dev_err(&pdev->dev, "error: could not enable device\n");
398 ret = proc_thermal_add(&pdev->dev, &proc_priv);
400 pci_disable_device(pdev);
404 pci_set_drvdata(pdev, proc_priv);
405 proc_thermal_emum_mode = PROC_THERMAL_PCI;
407 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
409 * Enumerate additional DTS sensors available via IOSF.
410 * But we are not treating as a failure condition, if
411 * there are no aux DTSs enabled or fails. This driver
412 * already exposes sensors, which can be accessed via
413 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
415 proc_priv->soc_dts = intel_soc_dts_iosf_init(
416 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
418 if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
419 ret = pci_enable_msi(pdev);
421 ret = request_threaded_irq(pdev->irq, NULL,
422 proc_thermal_pci_msi_irq,
423 IRQF_ONESHOT, "proc_thermal",
426 intel_soc_dts_iosf_exit(
428 pci_disable_msi(pdev);
429 proc_priv->soc_dts = NULL;
433 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
436 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
438 return sysfs_create_group(&pdev->dev.kobj,
439 &power_limit_attribute_group);
442 static void proc_thermal_pci_remove(struct pci_dev *pdev)
444 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
446 if (proc_priv->soc_dts) {
447 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
449 free_irq(pdev->irq, pdev);
450 pci_disable_msi(pdev);
453 proc_thermal_remove(proc_priv);
454 pci_disable_device(pdev);
457 static const struct pci_device_id proc_thermal_pci_ids[] = {
458 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
459 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
460 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)},
461 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
462 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
463 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
464 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
465 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
469 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
471 static struct pci_driver proc_thermal_pci_driver = {
472 .name = "proc_thermal",
473 .probe = proc_thermal_pci_probe,
474 .remove = proc_thermal_pci_remove,
475 .id_table = proc_thermal_pci_ids,
478 static const struct acpi_device_id int3401_device_ids[] = {
482 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
484 static struct platform_driver int3401_driver = {
485 .probe = int3401_add,
486 .remove = int3401_remove,
488 .name = "int3401 thermal",
489 .acpi_match_table = int3401_device_ids,
493 static int __init proc_thermal_init(void)
497 ret = platform_driver_register(&int3401_driver);
501 ret = pci_register_driver(&proc_thermal_pci_driver);
506 static void __exit proc_thermal_exit(void)
508 platform_driver_unregister(&int3401_driver);
509 pci_unregister_driver(&proc_thermal_pci_driver);
512 module_init(proc_thermal_init);
513 module_exit(proc_thermal_exit);
515 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
516 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
517 MODULE_LICENSE("GPL v2");