1 // SPDX-License-Identifier: GPL-2.0-only
3 * processor_thermal_device.c
4 * Copyright (c) 2014, Intel Corporation.
6 #include <linux/acpi.h>
7 #include <linux/intel_tcc.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/thermal.h>
12 #include "int340x_thermal_zone.h"
13 #include "processor_thermal_device.h"
14 #include "../intel_soc_dts_iosf.h"
16 #define DRV_NAME "proc_thermal"
18 #define POWER_LIMIT_SHOW(index, suffix) \
19 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
20 struct device_attribute *attr, \
23 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
25 return sprintf(buf, "%lu\n",\
26 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
29 static ssize_t
power_floor_status_show(struct device
*dev
,
30 struct device_attribute
*attr
,
33 struct proc_thermal_device
*proc_dev
= dev_get_drvdata(dev
);
36 ret
= proc_thermal_read_power_floor_status(proc_dev
);
38 return sysfs_emit(buf
, "%d\n", ret
);
41 static ssize_t
power_floor_enable_show(struct device
*dev
,
42 struct device_attribute
*attr
,
45 struct proc_thermal_device
*proc_dev
= dev_get_drvdata(dev
);
48 ret
= proc_thermal_power_floor_get_state(proc_dev
);
50 return sysfs_emit(buf
, "%d\n", ret
);
53 static ssize_t
power_floor_enable_store(struct device
*dev
,
54 struct device_attribute
*attr
,
55 const char *buf
, size_t count
)
57 struct proc_thermal_device
*proc_dev
= dev_get_drvdata(dev
);
61 if (kstrtou8(buf
, 0, &state
))
64 ret
= proc_thermal_power_floor_set_state(proc_dev
, !!state
);
71 POWER_LIMIT_SHOW(0, min_uw
)
72 POWER_LIMIT_SHOW(0, max_uw
)
73 POWER_LIMIT_SHOW(0, step_uw
)
74 POWER_LIMIT_SHOW(0, tmin_us
)
75 POWER_LIMIT_SHOW(0, tmax_us
)
77 POWER_LIMIT_SHOW(1, min_uw
)
78 POWER_LIMIT_SHOW(1, max_uw
)
79 POWER_LIMIT_SHOW(1, step_uw
)
80 POWER_LIMIT_SHOW(1, tmin_us
)
81 POWER_LIMIT_SHOW(1, tmax_us
)
83 static DEVICE_ATTR_RO(power_limit_0_min_uw
);
84 static DEVICE_ATTR_RO(power_limit_0_max_uw
);
85 static DEVICE_ATTR_RO(power_limit_0_step_uw
);
86 static DEVICE_ATTR_RO(power_limit_0_tmin_us
);
87 static DEVICE_ATTR_RO(power_limit_0_tmax_us
);
89 static DEVICE_ATTR_RO(power_limit_1_min_uw
);
90 static DEVICE_ATTR_RO(power_limit_1_max_uw
);
91 static DEVICE_ATTR_RO(power_limit_1_step_uw
);
92 static DEVICE_ATTR_RO(power_limit_1_tmin_us
);
93 static DEVICE_ATTR_RO(power_limit_1_tmax_us
);
95 static DEVICE_ATTR_RO(power_floor_status
);
96 static DEVICE_ATTR_RW(power_floor_enable
);
98 static struct attribute
*power_limit_attrs
[] = {
99 &dev_attr_power_limit_0_min_uw
.attr
,
100 &dev_attr_power_limit_1_min_uw
.attr
,
101 &dev_attr_power_limit_0_max_uw
.attr
,
102 &dev_attr_power_limit_1_max_uw
.attr
,
103 &dev_attr_power_limit_0_step_uw
.attr
,
104 &dev_attr_power_limit_1_step_uw
.attr
,
105 &dev_attr_power_limit_0_tmin_us
.attr
,
106 &dev_attr_power_limit_1_tmin_us
.attr
,
107 &dev_attr_power_limit_0_tmax_us
.attr
,
108 &dev_attr_power_limit_1_tmax_us
.attr
,
109 &dev_attr_power_floor_status
.attr
,
110 &dev_attr_power_floor_enable
.attr
,
114 static umode_t
power_limit_attr_visible(struct kobject
*kobj
, struct attribute
*attr
, int unused
)
116 struct device
*dev
= kobj_to_dev(kobj
);
117 struct proc_thermal_device
*proc_dev
;
119 if (attr
!= &dev_attr_power_floor_status
.attr
&& attr
!= &dev_attr_power_floor_enable
.attr
)
122 proc_dev
= dev_get_drvdata(dev
);
123 if (!proc_dev
|| !(proc_dev
->mmio_feature_mask
& PROC_THERMAL_FEATURE_POWER_FLOOR
))
129 static const struct attribute_group power_limit_attribute_group
= {
130 .attrs
= power_limit_attrs
,
131 .name
= "power_limits",
132 .is_visible
= power_limit_attr_visible
,
135 static ssize_t
tcc_offset_degree_celsius_show(struct device
*dev
,
136 struct device_attribute
*attr
,
141 offset
= intel_tcc_get_offset(-1);
145 return sprintf(buf
, "%d\n", offset
);
148 static ssize_t
tcc_offset_degree_celsius_store(struct device
*dev
,
149 struct device_attribute
*attr
, const char *buf
,
156 err
= rdmsrl_safe(MSR_PLATFORM_INFO
, &val
);
160 if (!(val
& BIT(30)))
163 if (kstrtouint(buf
, 0, &tcc
))
166 err
= intel_tcc_set_offset(-1, tcc
);
173 static DEVICE_ATTR_RW(tcc_offset_degree_celsius
);
175 static int proc_thermal_get_zone_temp(struct thermal_zone_device
*zone
,
183 for_each_online_cpu(cpu
) {
184 ret
= intel_tcc_get_temp(cpu
, &curr_temp
, false);
187 if (!*temp
|| curr_temp
> *temp
)
196 static int proc_thermal_read_ppcc(struct proc_thermal_device
*proc_priv
)
200 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
201 union acpi_object
*elements
, *ppcc
;
202 union acpi_object
*p
;
205 status
= acpi_evaluate_object(proc_priv
->adev
->handle
, "PPCC",
207 if (ACPI_FAILURE(status
))
211 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
212 dev_err(proc_priv
->dev
, "Invalid PPCC data\n");
217 if (!p
->package
.count
) {
218 dev_err(proc_priv
->dev
, "Invalid PPCC package size\n");
223 for (i
= 0; i
< min((int)p
->package
.count
- 1, 2); ++i
) {
224 elements
= &(p
->package
.elements
[i
+1]);
225 if (elements
->type
!= ACPI_TYPE_PACKAGE
||
226 elements
->package
.count
!= 6) {
230 ppcc
= elements
->package
.elements
;
231 proc_priv
->power_limits
[i
].index
= ppcc
[0].integer
.value
;
232 proc_priv
->power_limits
[i
].min_uw
= ppcc
[1].integer
.value
;
233 proc_priv
->power_limits
[i
].max_uw
= ppcc
[2].integer
.value
;
234 proc_priv
->power_limits
[i
].tmin_us
= ppcc
[3].integer
.value
;
235 proc_priv
->power_limits
[i
].tmax_us
= ppcc
[4].integer
.value
;
236 proc_priv
->power_limits
[i
].step_uw
= ppcc
[5].integer
.value
;
245 #define PROC_POWER_CAPABILITY_CHANGED 0x83
246 static void proc_thermal_notify(acpi_handle handle
, u32 event
, void *data
)
248 struct proc_thermal_device
*proc_priv
= data
;
254 case PROC_POWER_CAPABILITY_CHANGED
:
255 proc_thermal_read_ppcc(proc_priv
);
256 int340x_thermal_zone_device_update(proc_priv
->int340x_zone
,
257 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED
);
260 dev_dbg(proc_priv
->dev
, "Unsupported event [0x%x]\n", event
);
265 int proc_thermal_add(struct device
*dev
, struct proc_thermal_device
*proc_priv
)
267 struct acpi_device
*adev
;
269 unsigned long long tmp
;
270 int (*get_temp
) (struct thermal_zone_device
*, int *) = NULL
;
273 adev
= ACPI_COMPANION(dev
);
277 proc_priv
->dev
= dev
;
278 proc_priv
->adev
= adev
;
280 ret
= proc_thermal_read_ppcc(proc_priv
);
284 status
= acpi_evaluate_integer(adev
->handle
, "_TMP", NULL
, &tmp
);
285 if (ACPI_FAILURE(status
)) {
286 /* there is no _TMP method, add local method */
287 if (intel_tcc_get_tjmax(-1) > 0)
288 get_temp
= proc_thermal_get_zone_temp
;
291 proc_priv
->int340x_zone
= int340x_thermal_zone_add(adev
, get_temp
);
292 if (IS_ERR(proc_priv
->int340x_zone
)) {
293 return PTR_ERR(proc_priv
->int340x_zone
);
297 ret
= acpi_install_notify_handler(adev
->handle
, ACPI_DEVICE_NOTIFY
,
303 ret
= sysfs_create_file(&dev
->kobj
, &dev_attr_tcc_offset_degree_celsius
.attr
);
307 ret
= sysfs_create_group(&dev
->kobj
, &power_limit_attribute_group
);
309 sysfs_remove_file(&dev
->kobj
, &dev_attr_tcc_offset_degree_celsius
.attr
);
316 acpi_remove_notify_handler(adev
->handle
,
317 ACPI_DEVICE_NOTIFY
, proc_thermal_notify
);
319 int340x_thermal_zone_remove(proc_priv
->int340x_zone
);
323 EXPORT_SYMBOL_GPL(proc_thermal_add
);
325 void proc_thermal_remove(struct proc_thermal_device
*proc_priv
)
327 acpi_remove_notify_handler(proc_priv
->adev
->handle
,
328 ACPI_DEVICE_NOTIFY
, proc_thermal_notify
);
329 int340x_thermal_zone_remove(proc_priv
->int340x_zone
);
330 sysfs_remove_file(&proc_priv
->dev
->kobj
, &dev_attr_tcc_offset_degree_celsius
.attr
);
331 sysfs_remove_group(&proc_priv
->dev
->kobj
,
332 &power_limit_attribute_group
);
334 EXPORT_SYMBOL_GPL(proc_thermal_remove
);
336 static int tcc_offset_save
= -1;
338 int proc_thermal_suspend(struct device
*dev
)
340 tcc_offset_save
= intel_tcc_get_offset(-1);
341 if (tcc_offset_save
< 0)
342 dev_warn(dev
, "failed to save offset (%d)\n", tcc_offset_save
);
346 EXPORT_SYMBOL_GPL(proc_thermal_suspend
);
348 int proc_thermal_resume(struct device
*dev
)
350 struct proc_thermal_device
*proc_dev
;
352 proc_dev
= dev_get_drvdata(dev
);
353 proc_thermal_read_ppcc(proc_dev
);
355 /* Do not update if saving failed */
356 if (tcc_offset_save
>= 0)
357 intel_tcc_set_offset(-1, tcc_offset_save
);
361 EXPORT_SYMBOL_GPL(proc_thermal_resume
);
365 static int proc_thermal_set_mmio_base(struct pci_dev
*pdev
, struct proc_thermal_device
*proc_priv
)
369 ret
= pcim_iomap_regions(pdev
, 1 << MCHBAR
, DRV_NAME
);
371 dev_err(&pdev
->dev
, "cannot reserve PCI memory region\n");
375 proc_priv
->mmio_base
= pcim_iomap_table(pdev
)[MCHBAR
];
380 int proc_thermal_mmio_add(struct pci_dev
*pdev
,
381 struct proc_thermal_device
*proc_priv
,
382 kernel_ulong_t feature_mask
)
386 proc_priv
->mmio_feature_mask
= feature_mask
;
389 ret
= proc_thermal_set_mmio_base(pdev
, proc_priv
);
394 if (feature_mask
& PROC_THERMAL_FEATURE_RAPL
) {
395 ret
= proc_thermal_rapl_add(pdev
, proc_priv
);
397 dev_err(&pdev
->dev
, "failed to add RAPL MMIO interface\n");
402 if (feature_mask
& PROC_THERMAL_FEATURE_FIVR
||
403 feature_mask
& PROC_THERMAL_FEATURE_DVFS
||
404 feature_mask
& PROC_THERMAL_FEATURE_DLVR
) {
405 ret
= proc_thermal_rfim_add(pdev
, proc_priv
);
407 dev_err(&pdev
->dev
, "failed to add RFIM interface\n");
412 if (feature_mask
& PROC_THERMAL_FEATURE_WT_REQ
) {
413 ret
= proc_thermal_wt_req_add(pdev
, proc_priv
);
415 dev_err(&pdev
->dev
, "failed to add MBOX interface\n");
418 } else if (feature_mask
& PROC_THERMAL_FEATURE_WT_HINT
) {
419 ret
= proc_thermal_wt_hint_add(pdev
, proc_priv
);
421 dev_err(&pdev
->dev
, "failed to add WT Hint\n");
429 proc_thermal_rfim_remove(pdev
);
431 proc_thermal_rapl_remove();
435 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add
);
437 void proc_thermal_mmio_remove(struct pci_dev
*pdev
, struct proc_thermal_device
*proc_priv
)
439 if (proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_RAPL
)
440 proc_thermal_rapl_remove();
442 if (proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_FIVR
||
443 proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_DVFS
||
444 proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_DLVR
)
445 proc_thermal_rfim_remove(pdev
);
447 if (proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_POWER_FLOOR
)
448 proc_thermal_power_floor_set_state(proc_priv
, false);
450 if (proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_WT_REQ
)
451 proc_thermal_wt_req_remove(pdev
);
452 else if (proc_priv
->mmio_feature_mask
& PROC_THERMAL_FEATURE_WT_HINT
)
453 proc_thermal_wt_hint_remove(pdev
);
455 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove
);
457 MODULE_IMPORT_NS(INTEL_TCC
);
458 MODULE_IMPORT_NS(INT340X_THERMAL
);
459 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
460 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
461 MODULE_LICENSE("GPL v2");