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 POWER_LIMIT_SHOW(0, min_uw)
30 POWER_LIMIT_SHOW(0, max_uw)
31 POWER_LIMIT_SHOW(0, step_uw)
32 POWER_LIMIT_SHOW(0, tmin_us)
33 POWER_LIMIT_SHOW(0, tmax_us)
35 POWER_LIMIT_SHOW(1, min_uw)
36 POWER_LIMIT_SHOW(1, max_uw)
37 POWER_LIMIT_SHOW(1, step_uw)
38 POWER_LIMIT_SHOW(1, tmin_us)
39 POWER_LIMIT_SHOW(1, tmax_us)
41 static DEVICE_ATTR_RO(power_limit_0_min_uw);
42 static DEVICE_ATTR_RO(power_limit_0_max_uw);
43 static DEVICE_ATTR_RO(power_limit_0_step_uw);
44 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
45 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
47 static DEVICE_ATTR_RO(power_limit_1_min_uw);
48 static DEVICE_ATTR_RO(power_limit_1_max_uw);
49 static DEVICE_ATTR_RO(power_limit_1_step_uw);
50 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
51 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
53 static struct attribute *power_limit_attrs[] = {
54 &dev_attr_power_limit_0_min_uw.attr,
55 &dev_attr_power_limit_1_min_uw.attr,
56 &dev_attr_power_limit_0_max_uw.attr,
57 &dev_attr_power_limit_1_max_uw.attr,
58 &dev_attr_power_limit_0_step_uw.attr,
59 &dev_attr_power_limit_1_step_uw.attr,
60 &dev_attr_power_limit_0_tmin_us.attr,
61 &dev_attr_power_limit_1_tmin_us.attr,
62 &dev_attr_power_limit_0_tmax_us.attr,
63 &dev_attr_power_limit_1_tmax_us.attr,
67 static const struct attribute_group power_limit_attribute_group = {
68 .attrs = power_limit_attrs,
69 .name = "power_limits"
72 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
73 struct device_attribute *attr,
78 offset = intel_tcc_get_offset(-1);
82 return sprintf(buf, "%d\n", offset);
85 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
86 struct device_attribute *attr, const char *buf,
93 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
100 if (kstrtouint(buf, 0, &tcc))
103 err = intel_tcc_set_offset(-1, tcc);
110 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
112 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
120 for_each_online_cpu(cpu) {
121 curr_temp = intel_tcc_get_temp(cpu, false);
124 if (!*temp || curr_temp > *temp)
133 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
137 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
138 union acpi_object *elements, *ppcc;
139 union acpi_object *p;
142 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
144 if (ACPI_FAILURE(status))
148 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
149 dev_err(proc_priv->dev, "Invalid PPCC data\n");
154 if (!p->package.count) {
155 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
160 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
161 elements = &(p->package.elements[i+1]);
162 if (elements->type != ACPI_TYPE_PACKAGE ||
163 elements->package.count != 6) {
167 ppcc = elements->package.elements;
168 proc_priv->power_limits[i].index = ppcc[0].integer.value;
169 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
170 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
171 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
172 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
173 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
182 #define PROC_POWER_CAPABILITY_CHANGED 0x83
183 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
185 struct proc_thermal_device *proc_priv = data;
191 case PROC_POWER_CAPABILITY_CHANGED:
192 proc_thermal_read_ppcc(proc_priv);
193 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
194 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
197 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
202 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
204 struct acpi_device *adev;
206 unsigned long long tmp;
207 int (*get_temp) (struct thermal_zone_device *, int *) = NULL;
210 adev = ACPI_COMPANION(dev);
214 proc_priv->dev = dev;
215 proc_priv->adev = adev;
217 ret = proc_thermal_read_ppcc(proc_priv);
221 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
222 if (ACPI_FAILURE(status)) {
223 /* there is no _TMP method, add local method */
224 if (intel_tcc_get_tjmax(-1) > 0)
225 get_temp = proc_thermal_get_zone_temp;
228 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, get_temp);
229 if (IS_ERR(proc_priv->int340x_zone)) {
230 return PTR_ERR(proc_priv->int340x_zone);
234 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
240 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
244 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
246 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
253 acpi_remove_notify_handler(adev->handle,
254 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
256 int340x_thermal_zone_remove(proc_priv->int340x_zone);
260 EXPORT_SYMBOL_GPL(proc_thermal_add);
262 void proc_thermal_remove(struct proc_thermal_device *proc_priv)
264 acpi_remove_notify_handler(proc_priv->adev->handle,
265 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
266 int340x_thermal_zone_remove(proc_priv->int340x_zone);
267 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
268 sysfs_remove_group(&proc_priv->dev->kobj,
269 &power_limit_attribute_group);
271 EXPORT_SYMBOL_GPL(proc_thermal_remove);
273 static int tcc_offset_save = -1;
275 int proc_thermal_suspend(struct device *dev)
277 tcc_offset_save = intel_tcc_get_offset(-1);
278 if (tcc_offset_save < 0)
279 dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save);
283 EXPORT_SYMBOL_GPL(proc_thermal_suspend);
285 int proc_thermal_resume(struct device *dev)
287 struct proc_thermal_device *proc_dev;
289 proc_dev = dev_get_drvdata(dev);
290 proc_thermal_read_ppcc(proc_dev);
292 /* Do not update if saving failed */
293 if (tcc_offset_save >= 0)
294 intel_tcc_set_offset(-1, tcc_offset_save);
298 EXPORT_SYMBOL_GPL(proc_thermal_resume);
302 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
306 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
308 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
312 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
317 int proc_thermal_mmio_add(struct pci_dev *pdev,
318 struct proc_thermal_device *proc_priv,
319 kernel_ulong_t feature_mask)
323 proc_priv->mmio_feature_mask = feature_mask;
326 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
331 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
332 ret = proc_thermal_rapl_add(pdev, proc_priv);
334 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
339 if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
340 feature_mask & PROC_THERMAL_FEATURE_DVFS ||
341 feature_mask & PROC_THERMAL_FEATURE_DLVR) {
342 ret = proc_thermal_rfim_add(pdev, proc_priv);
344 dev_err(&pdev->dev, "failed to add RFIM interface\n");
349 if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
350 ret = proc_thermal_mbox_add(pdev, proc_priv);
352 dev_err(&pdev->dev, "failed to add MBOX interface\n");
360 proc_thermal_rfim_remove(pdev);
362 proc_thermal_rapl_remove();
366 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
368 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
370 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
371 proc_thermal_rapl_remove();
373 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
374 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS)
375 proc_thermal_rfim_remove(pdev);
377 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
378 proc_thermal_mbox_remove(pdev);
380 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
382 MODULE_IMPORT_NS(INTEL_TCC);
384 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
385 MODULE_LICENSE("GPL v2");