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 ssize_t tcc_offset_degree_celsius_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
77 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
81 val = (val >> 24) & 0x3f;
82 return sprintf(buf, "%d\n", (int)val);
85 static int tcc_offset_update(unsigned int tcc)
93 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
100 val &= ~GENMASK_ULL(29, 24);
101 val |= (tcc & 0x3f) << 24;
103 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
110 static int tcc_offset_save = -1;
112 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
113 struct device_attribute *attr, const char *buf,
120 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
124 if (!(val & BIT(30)))
127 if (kstrtouint(buf, 0, &tcc))
130 err = tcc_offset_update(tcc);
134 tcc_offset_save = tcc;
139 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
141 static int stored_tjmax; /* since it is fixed, we can have local storage */
143 static int get_tjmax(void)
149 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
153 val = (eax >> 16) & 0xff;
160 static int read_temp_msr(int *temp)
165 unsigned long curr_temp_off = 0;
169 for_each_online_cpu(cpu) {
170 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
175 if (eax & 0x80000000) {
176 curr_temp_off = (eax >> 16) & 0x7f;
177 if (!*temp || curr_temp_off < *temp)
178 *temp = curr_temp_off;
191 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
196 ret = read_temp_msr(temp);
198 *temp = (stored_tjmax - *temp) * 1000;
203 static struct thermal_zone_device_ops proc_thermal_local_ops = {
204 .get_temp = proc_thermal_get_zone_temp,
207 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
211 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
212 union acpi_object *elements, *ppcc;
213 union acpi_object *p;
216 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
218 if (ACPI_FAILURE(status))
222 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
223 dev_err(proc_priv->dev, "Invalid PPCC data\n");
228 if (!p->package.count) {
229 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
234 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
235 elements = &(p->package.elements[i+1]);
236 if (elements->type != ACPI_TYPE_PACKAGE ||
237 elements->package.count != 6) {
241 ppcc = elements->package.elements;
242 proc_priv->power_limits[i].index = ppcc[0].integer.value;
243 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
244 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
245 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
246 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
247 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
256 #define PROC_POWER_CAPABILITY_CHANGED 0x83
257 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
259 struct proc_thermal_device *proc_priv = data;
265 case PROC_POWER_CAPABILITY_CHANGED:
266 proc_thermal_read_ppcc(proc_priv);
267 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
268 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
271 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
276 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
278 struct acpi_device *adev;
280 unsigned long long tmp;
281 struct thermal_zone_device_ops *ops = NULL;
284 adev = ACPI_COMPANION(dev);
288 proc_priv->dev = dev;
289 proc_priv->adev = adev;
291 ret = proc_thermal_read_ppcc(proc_priv);
295 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
296 if (ACPI_FAILURE(status)) {
297 /* there is no _TMP method, add local method */
298 stored_tjmax = get_tjmax();
299 if (stored_tjmax > 0)
300 ops = &proc_thermal_local_ops;
303 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
304 if (IS_ERR(proc_priv->int340x_zone)) {
305 return PTR_ERR(proc_priv->int340x_zone);
309 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
315 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
319 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
321 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
328 acpi_remove_notify_handler(adev->handle,
329 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
331 int340x_thermal_zone_remove(proc_priv->int340x_zone);
335 EXPORT_SYMBOL_GPL(proc_thermal_add);
337 void proc_thermal_remove(struct proc_thermal_device *proc_priv)
339 acpi_remove_notify_handler(proc_priv->adev->handle,
340 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
341 int340x_thermal_zone_remove(proc_priv->int340x_zone);
342 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
343 sysfs_remove_group(&proc_priv->dev->kobj,
344 &power_limit_attribute_group);
346 EXPORT_SYMBOL_GPL(proc_thermal_remove);
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 if (tcc_offset_save >= 0)
356 tcc_offset_update(tcc_offset_save);
360 EXPORT_SYMBOL_GPL(proc_thermal_resume);
364 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
368 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
370 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
374 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
379 int proc_thermal_mmio_add(struct pci_dev *pdev,
380 struct proc_thermal_device *proc_priv,
381 kernel_ulong_t feature_mask)
385 proc_priv->mmio_feature_mask = feature_mask;
388 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
393 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
394 ret = proc_thermal_rapl_add(pdev, proc_priv);
396 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
401 if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
402 feature_mask & PROC_THERMAL_FEATURE_DVFS) {
403 ret = proc_thermal_rfim_add(pdev, proc_priv);
405 dev_err(&pdev->dev, "failed to add RFIM interface\n");
410 if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
411 ret = proc_thermal_mbox_add(pdev, proc_priv);
413 dev_err(&pdev->dev, "failed to add MBOX interface\n");
421 proc_thermal_rfim_remove(pdev);
423 proc_thermal_rapl_remove();
427 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
429 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
431 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
432 proc_thermal_rapl_remove();
434 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
435 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS)
436 proc_thermal_rfim_remove(pdev);
438 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
439 proc_thermal_mbox_remove(pdev);
441 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
443 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
444 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
445 MODULE_LICENSE("GPL v2");