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/kernel.h>
8 #include <linux/module.h>
10 #include <linux/thermal.h>
11 #include "int340x_thermal_zone.h"
12 #include "processor_thermal_device.h"
13 #include "../intel_soc_dts_iosf.h"
15 #define DRV_NAME "proc_thermal"
17 #define POWER_LIMIT_SHOW(index, suffix) \
18 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
19 struct device_attribute *attr, \
22 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
24 return sprintf(buf, "%lu\n",\
25 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
28 POWER_LIMIT_SHOW(0, min_uw)
29 POWER_LIMIT_SHOW(0, max_uw)
30 POWER_LIMIT_SHOW(0, step_uw)
31 POWER_LIMIT_SHOW(0, tmin_us)
32 POWER_LIMIT_SHOW(0, tmax_us)
34 POWER_LIMIT_SHOW(1, min_uw)
35 POWER_LIMIT_SHOW(1, max_uw)
36 POWER_LIMIT_SHOW(1, step_uw)
37 POWER_LIMIT_SHOW(1, tmin_us)
38 POWER_LIMIT_SHOW(1, tmax_us)
40 static DEVICE_ATTR_RO(power_limit_0_min_uw);
41 static DEVICE_ATTR_RO(power_limit_0_max_uw);
42 static DEVICE_ATTR_RO(power_limit_0_step_uw);
43 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
44 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
46 static DEVICE_ATTR_RO(power_limit_1_min_uw);
47 static DEVICE_ATTR_RO(power_limit_1_max_uw);
48 static DEVICE_ATTR_RO(power_limit_1_step_uw);
49 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
50 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
52 static struct attribute *power_limit_attrs[] = {
53 &dev_attr_power_limit_0_min_uw.attr,
54 &dev_attr_power_limit_1_min_uw.attr,
55 &dev_attr_power_limit_0_max_uw.attr,
56 &dev_attr_power_limit_1_max_uw.attr,
57 &dev_attr_power_limit_0_step_uw.attr,
58 &dev_attr_power_limit_1_step_uw.attr,
59 &dev_attr_power_limit_0_tmin_us.attr,
60 &dev_attr_power_limit_1_tmin_us.attr,
61 &dev_attr_power_limit_0_tmax_us.attr,
62 &dev_attr_power_limit_1_tmax_us.attr,
66 static const struct attribute_group power_limit_attribute_group = {
67 .attrs = power_limit_attrs,
68 .name = "power_limits"
71 static int tcc_get_offset(void)
76 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
80 return (val >> 24) & 0x3f;
83 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
84 struct device_attribute *attr,
89 tcc = tcc_get_offset();
93 return sprintf(buf, "%d\n", tcc);
96 static int tcc_offset_update(unsigned int tcc)
104 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
111 val &= ~GENMASK_ULL(29, 24);
112 val |= (tcc & 0x3f) << 24;
114 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
121 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
122 struct device_attribute *attr, const char *buf,
129 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
133 if (!(val & BIT(30)))
136 if (kstrtouint(buf, 0, &tcc))
139 err = tcc_offset_update(tcc);
146 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
148 static int stored_tjmax; /* since it is fixed, we can have local storage */
150 static int get_tjmax(void)
156 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
160 val = (eax >> 16) & 0xff;
167 static int read_temp_msr(int *temp)
172 unsigned long curr_temp_off = 0;
176 for_each_online_cpu(cpu) {
177 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
182 if (eax & 0x80000000) {
183 curr_temp_off = (eax >> 16) & 0x7f;
184 if (!*temp || curr_temp_off < *temp)
185 *temp = curr_temp_off;
198 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
203 ret = read_temp_msr(temp);
205 *temp = (stored_tjmax - *temp) * 1000;
210 static struct thermal_zone_device_ops proc_thermal_local_ops = {
211 .get_temp = proc_thermal_get_zone_temp,
214 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
218 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
219 union acpi_object *elements, *ppcc;
220 union acpi_object *p;
223 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
225 if (ACPI_FAILURE(status))
229 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
230 dev_err(proc_priv->dev, "Invalid PPCC data\n");
235 if (!p->package.count) {
236 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
241 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
242 elements = &(p->package.elements[i+1]);
243 if (elements->type != ACPI_TYPE_PACKAGE ||
244 elements->package.count != 6) {
248 ppcc = elements->package.elements;
249 proc_priv->power_limits[i].index = ppcc[0].integer.value;
250 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
251 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
252 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
253 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
254 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
263 #define PROC_POWER_CAPABILITY_CHANGED 0x83
264 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
266 struct proc_thermal_device *proc_priv = data;
272 case PROC_POWER_CAPABILITY_CHANGED:
273 proc_thermal_read_ppcc(proc_priv);
274 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
275 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
278 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
283 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
285 struct acpi_device *adev;
287 unsigned long long tmp;
288 struct thermal_zone_device_ops *ops = NULL;
291 adev = ACPI_COMPANION(dev);
295 proc_priv->dev = dev;
296 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,
322 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
326 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
328 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
335 acpi_remove_notify_handler(adev->handle,
336 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
338 int340x_thermal_zone_remove(proc_priv->int340x_zone);
342 EXPORT_SYMBOL_GPL(proc_thermal_add);
344 void proc_thermal_remove(struct proc_thermal_device *proc_priv)
346 acpi_remove_notify_handler(proc_priv->adev->handle,
347 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
348 int340x_thermal_zone_remove(proc_priv->int340x_zone);
349 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
350 sysfs_remove_group(&proc_priv->dev->kobj,
351 &power_limit_attribute_group);
353 EXPORT_SYMBOL_GPL(proc_thermal_remove);
355 static int tcc_offset_save = -1;
357 int proc_thermal_suspend(struct device *dev)
359 tcc_offset_save = tcc_get_offset();
360 if (tcc_offset_save < 0)
361 dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save);
365 EXPORT_SYMBOL_GPL(proc_thermal_suspend);
367 int proc_thermal_resume(struct device *dev)
369 struct proc_thermal_device *proc_dev;
371 proc_dev = dev_get_drvdata(dev);
372 proc_thermal_read_ppcc(proc_dev);
374 /* Do not update if saving failed */
375 if (tcc_offset_save >= 0)
376 tcc_offset_update(tcc_offset_save);
380 EXPORT_SYMBOL_GPL(proc_thermal_resume);
384 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
388 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
390 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
394 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
399 int proc_thermal_mmio_add(struct pci_dev *pdev,
400 struct proc_thermal_device *proc_priv,
401 kernel_ulong_t feature_mask)
405 proc_priv->mmio_feature_mask = feature_mask;
408 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
413 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
414 ret = proc_thermal_rapl_add(pdev, proc_priv);
416 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
421 if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
422 feature_mask & PROC_THERMAL_FEATURE_DVFS) {
423 ret = proc_thermal_rfim_add(pdev, proc_priv);
425 dev_err(&pdev->dev, "failed to add RFIM interface\n");
430 if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
431 ret = proc_thermal_mbox_add(pdev, proc_priv);
433 dev_err(&pdev->dev, "failed to add MBOX interface\n");
441 proc_thermal_rfim_remove(pdev);
443 proc_thermal_rapl_remove();
447 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
449 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
451 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
452 proc_thermal_rapl_remove();
454 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
455 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS)
456 proc_thermal_rfim_remove(pdev);
458 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
459 proc_thermal_mbox_remove(pdev);
461 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
464 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
465 MODULE_LICENSE("GPL v2");