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
45 struct proc_thermal_device
{
47 struct acpi_device
*adev
;
48 struct power_config power_limits
[2];
49 struct int34x_thermal_zone
*int340x_zone
;
50 struct intel_soc_dts_sensors
*soc_dts
;
53 enum proc_thermal_emum_mode_type
{
56 PROC_THERMAL_PLATFORM_DEV
60 * We can have only one type of enumeration, PCI or Platform,
61 * not both. So we don't need instance specific data.
63 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode
=
66 #define POWER_LIMIT_SHOW(index, suffix) \
67 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
68 struct device_attribute *attr, \
71 struct pci_dev *pci_dev; \
72 struct platform_device *pdev; \
73 struct proc_thermal_device *proc_dev; \
75 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
76 pdev = to_platform_device(dev); \
77 proc_dev = platform_get_drvdata(pdev); \
79 pci_dev = to_pci_dev(dev); \
80 proc_dev = pci_get_drvdata(pci_dev); \
82 return sprintf(buf, "%lu\n",\
83 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
86 POWER_LIMIT_SHOW(0, min_uw
)
87 POWER_LIMIT_SHOW(0, max_uw
)
88 POWER_LIMIT_SHOW(0, step_uw
)
89 POWER_LIMIT_SHOW(0, tmin_us
)
90 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
)
98 static DEVICE_ATTR_RO(power_limit_0_min_uw
);
99 static DEVICE_ATTR_RO(power_limit_0_max_uw
);
100 static DEVICE_ATTR_RO(power_limit_0_step_uw
);
101 static DEVICE_ATTR_RO(power_limit_0_tmin_us
);
102 static DEVICE_ATTR_RO(power_limit_0_tmax_us
);
104 static DEVICE_ATTR_RO(power_limit_1_min_uw
);
105 static DEVICE_ATTR_RO(power_limit_1_max_uw
);
106 static DEVICE_ATTR_RO(power_limit_1_step_uw
);
107 static DEVICE_ATTR_RO(power_limit_1_tmin_us
);
108 static DEVICE_ATTR_RO(power_limit_1_tmax_us
);
110 static struct attribute
*power_limit_attrs
[] = {
111 &dev_attr_power_limit_0_min_uw
.attr
,
112 &dev_attr_power_limit_1_min_uw
.attr
,
113 &dev_attr_power_limit_0_max_uw
.attr
,
114 &dev_attr_power_limit_1_max_uw
.attr
,
115 &dev_attr_power_limit_0_step_uw
.attr
,
116 &dev_attr_power_limit_1_step_uw
.attr
,
117 &dev_attr_power_limit_0_tmin_us
.attr
,
118 &dev_attr_power_limit_1_tmin_us
.attr
,
119 &dev_attr_power_limit_0_tmax_us
.attr
,
120 &dev_attr_power_limit_1_tmax_us
.attr
,
124 static struct attribute_group power_limit_attribute_group
= {
125 .attrs
= power_limit_attrs
,
126 .name
= "power_limits"
129 static int stored_tjmax
; /* since it is fixed, we can have local storage */
131 static int get_tjmax(void)
137 err
= rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET
, &eax
, &edx
);
141 val
= (eax
>> 16) & 0xff;
148 static int read_temp_msr(unsigned long *temp
)
153 unsigned long curr_temp_off
= 0;
157 for_each_online_cpu(cpu
) {
158 err
= rdmsr_safe_on_cpu(cpu
, MSR_IA32_THERM_STATUS
, &eax
,
163 if (eax
& 0x80000000) {
164 curr_temp_off
= (eax
>> 16) & 0x7f;
165 if (!*temp
|| curr_temp_off
< *temp
)
166 *temp
= curr_temp_off
;
179 static int proc_thermal_get_zone_temp(struct thermal_zone_device
*zone
,
184 ret
= read_temp_msr(temp
);
186 *temp
= (stored_tjmax
- *temp
) * 1000;
191 static struct thermal_zone_device_ops proc_thermal_local_ops
= {
192 .get_temp
= proc_thermal_get_zone_temp
,
195 static int proc_thermal_add(struct device
*dev
,
196 struct proc_thermal_device
**priv
)
198 struct proc_thermal_device
*proc_priv
;
199 struct acpi_device
*adev
;
201 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
202 union acpi_object
*elements
, *ppcc
;
203 union acpi_object
*p
;
204 unsigned long long tmp
;
205 struct thermal_zone_device_ops
*ops
= NULL
;
209 adev
= ACPI_COMPANION(dev
);
213 status
= acpi_evaluate_object(adev
->handle
, "PPCC", NULL
, &buf
);
214 if (ACPI_FAILURE(status
))
218 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
219 dev_err(dev
, "Invalid PPCC data\n");
223 if (!p
->package
.count
) {
224 dev_err(dev
, "Invalid PPCC package size\n");
229 proc_priv
= devm_kzalloc(dev
, sizeof(*proc_priv
), GFP_KERNEL
);
235 proc_priv
->dev
= dev
;
236 proc_priv
->adev
= adev
;
238 for (i
= 0; i
< min((int)p
->package
.count
- 1, 2); ++i
) {
239 elements
= &(p
->package
.elements
[i
+1]);
240 if (elements
->type
!= ACPI_TYPE_PACKAGE
||
241 elements
->package
.count
!= 6) {
245 ppcc
= elements
->package
.elements
;
246 proc_priv
->power_limits
[i
].index
= ppcc
[0].integer
.value
;
247 proc_priv
->power_limits
[i
].min_uw
= ppcc
[1].integer
.value
;
248 proc_priv
->power_limits
[i
].max_uw
= ppcc
[2].integer
.value
;
249 proc_priv
->power_limits
[i
].tmin_us
= ppcc
[3].integer
.value
;
250 proc_priv
->power_limits
[i
].tmax_us
= ppcc
[4].integer
.value
;
251 proc_priv
->power_limits
[i
].step_uw
= ppcc
[5].integer
.value
;
256 ret
= sysfs_create_group(&dev
->kobj
,
257 &power_limit_attribute_group
);
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
;
269 proc_priv
->int340x_zone
= int340x_thermal_zone_add(adev
, ops
);
270 if (IS_ERR(proc_priv
->int340x_zone
)) {
271 sysfs_remove_group(&proc_priv
->dev
->kobj
,
272 &power_limit_attribute_group
);
273 ret
= PTR_ERR(proc_priv
->int340x_zone
);
283 static void proc_thermal_remove(struct proc_thermal_device
*proc_priv
)
285 int340x_thermal_zone_remove(proc_priv
->int340x_zone
);
286 sysfs_remove_group(&proc_priv
->dev
->kobj
,
287 &power_limit_attribute_group
);
290 static int int3401_add(struct platform_device
*pdev
)
292 struct proc_thermal_device
*proc_priv
;
295 if (proc_thermal_emum_mode
== PROC_THERMAL_PCI
) {
296 dev_err(&pdev
->dev
, "error: enumerated as PCI dev\n");
300 ret
= proc_thermal_add(&pdev
->dev
, &proc_priv
);
304 platform_set_drvdata(pdev
, proc_priv
);
305 proc_thermal_emum_mode
= PROC_THERMAL_PLATFORM_DEV
;
310 static int int3401_remove(struct platform_device
*pdev
)
312 proc_thermal_remove(platform_get_drvdata(pdev
));
317 static irqreturn_t
proc_thermal_pci_msi_irq(int irq
, void *devid
)
319 struct proc_thermal_device
*proc_priv
;
320 struct pci_dev
*pdev
= devid
;
322 proc_priv
= pci_get_drvdata(pdev
);
324 intel_soc_dts_iosf_interrupt_handler(proc_priv
->soc_dts
);
329 static int proc_thermal_pci_probe(struct pci_dev
*pdev
,
330 const struct pci_device_id
*unused
)
332 struct proc_thermal_device
*proc_priv
;
335 if (proc_thermal_emum_mode
== PROC_THERMAL_PLATFORM_DEV
) {
336 dev_err(&pdev
->dev
, "error: enumerated as platform dev\n");
340 ret
= pci_enable_device(pdev
);
342 dev_err(&pdev
->dev
, "error: could not enable device\n");
346 ret
= proc_thermal_add(&pdev
->dev
, &proc_priv
);
348 pci_disable_device(pdev
);
352 pci_set_drvdata(pdev
, proc_priv
);
353 proc_thermal_emum_mode
= PROC_THERMAL_PCI
;
355 if (pdev
->device
== PCI_DEVICE_ID_PROC_BSW_THERMAL
) {
357 * Enumerate additional DTS sensors available via IOSF.
358 * But we are not treating as a failure condition, if
359 * there are no aux DTSs enabled or fails. This driver
360 * already exposes sensors, which can be accessed via
361 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
363 proc_priv
->soc_dts
= intel_soc_dts_iosf_init(
364 INTEL_SOC_DTS_INTERRUPT_MSI
, 2, 0);
366 if (proc_priv
->soc_dts
&& pdev
->irq
) {
367 ret
= pci_enable_msi(pdev
);
369 ret
= request_threaded_irq(pdev
->irq
, NULL
,
370 proc_thermal_pci_msi_irq
,
371 IRQF_ONESHOT
, "proc_thermal",
374 intel_soc_dts_iosf_exit(
376 pci_disable_msi(pdev
);
377 proc_priv
->soc_dts
= NULL
;
381 dev_err(&pdev
->dev
, "No auxiliary DTSs enabled\n");
387 static void proc_thermal_pci_remove(struct pci_dev
*pdev
)
389 struct proc_thermal_device
*proc_priv
= pci_get_drvdata(pdev
);
391 if (proc_priv
->soc_dts
) {
392 intel_soc_dts_iosf_exit(proc_priv
->soc_dts
);
394 free_irq(pdev
->irq
, pdev
);
395 pci_disable_msi(pdev
);
398 proc_thermal_remove(proc_priv
);
399 pci_disable_device(pdev
);
402 static const struct pci_device_id proc_thermal_pci_ids
[] = {
403 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_PROC_BDW_THERMAL
)},
404 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_PROC_HSB_THERMAL
)},
405 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_PROC_SKL_THERMAL
)},
406 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_PROC_BSW_THERMAL
)},
410 MODULE_DEVICE_TABLE(pci
, proc_thermal_pci_ids
);
412 static struct pci_driver proc_thermal_pci_driver
= {
413 .name
= "proc_thermal",
414 .probe
= proc_thermal_pci_probe
,
415 .remove
= proc_thermal_pci_remove
,
416 .id_table
= proc_thermal_pci_ids
,
419 static const struct acpi_device_id int3401_device_ids
[] = {
423 MODULE_DEVICE_TABLE(acpi
, int3401_device_ids
);
425 static struct platform_driver int3401_driver
= {
426 .probe
= int3401_add
,
427 .remove
= int3401_remove
,
429 .name
= "int3401 thermal",
430 .acpi_match_table
= int3401_device_ids
,
434 static int __init
proc_thermal_init(void)
438 ret
= platform_driver_register(&int3401_driver
);
442 ret
= pci_register_driver(&proc_thermal_pci_driver
);
447 static void __exit
proc_thermal_exit(void)
449 platform_driver_unregister(&int3401_driver
);
450 pci_unregister_driver(&proc_thermal_pci_driver
);
453 module_init(proc_thermal_init
);
454 module_exit(proc_thermal_exit
);
456 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
457 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
458 MODULE_LICENSE("GPL v2");