Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thermal / intel / int340x_thermal / processor_thermal_device.c
blob7c46dd6bee731eaec4659a179a59681cfd7a91d3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * processor_thermal_device.c
4 * Copyright (c) 2014, Intel Corporation.
5 */
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, \
21 char *buf) \
22 { \
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,
31 char *buf)
33 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
34 int ret;
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,
43 char *buf)
45 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
46 bool ret;
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);
58 u8 state;
59 int ret;
61 if (kstrtou8(buf, 0, &state))
62 return -EINVAL;
64 ret = proc_thermal_power_floor_set_state(proc_dev, !!state);
65 if (ret)
66 return ret;
68 return count;
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,
111 NULL
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)
120 return attr->mode;
122 proc_dev = dev_get_drvdata(dev);
123 if (!proc_dev || !(proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR))
124 return 0;
126 return attr->mode;
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,
137 char *buf)
139 int offset;
141 offset = intel_tcc_get_offset(-1);
142 if (offset < 0)
143 return offset;
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,
150 size_t count)
152 unsigned int tcc;
153 u64 val;
154 int err;
156 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
157 if (err)
158 return err;
160 if (!(val & BIT(30)))
161 return -EACCES;
163 if (kstrtouint(buf, 0, &tcc))
164 return -EINVAL;
166 err = intel_tcc_set_offset(-1, tcc);
167 if (err)
168 return err;
170 return count;
173 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
175 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
176 int *temp)
178 int cpu;
179 int curr_temp, ret;
181 *temp = 0;
183 for_each_online_cpu(cpu) {
184 ret = intel_tcc_get_temp(cpu, &curr_temp, false);
185 if (ret < 0)
186 return ret;
187 if (!*temp || curr_temp > *temp)
188 *temp = curr_temp;
191 *temp *= 1000;
193 return 0;
196 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
198 int i;
199 acpi_status status;
200 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
201 union acpi_object *elements, *ppcc;
202 union acpi_object *p;
203 int ret = 0;
205 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
206 NULL, &buf);
207 if (ACPI_FAILURE(status))
208 return -ENODEV;
210 p = buf.pointer;
211 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
212 dev_err(proc_priv->dev, "Invalid PPCC data\n");
213 ret = -EFAULT;
214 goto free_buffer;
217 if (!p->package.count) {
218 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
219 ret = -EFAULT;
220 goto free_buffer;
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) {
227 ret = -EFAULT;
228 goto free_buffer;
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;
239 free_buffer:
240 kfree(buf.pointer);
242 return ret;
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;
250 if (!proc_priv)
251 return;
253 switch (event) {
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);
258 break;
259 default:
260 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
261 break;
265 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
267 struct acpi_device *adev;
268 acpi_status status;
269 unsigned long long tmp;
270 int (*get_temp) (struct thermal_zone_device *, int *) = NULL;
271 int ret;
273 adev = ACPI_COMPANION(dev);
274 if (!adev)
275 return -ENODEV;
277 proc_priv->dev = dev;
278 proc_priv->adev = adev;
280 ret = proc_thermal_read_ppcc(proc_priv);
281 if (ret)
282 return ret;
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);
294 } else
295 ret = 0;
297 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
298 proc_thermal_notify,
299 (void *)proc_priv);
300 if (ret)
301 goto remove_zone;
303 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
304 if (ret)
305 goto remove_notify;
307 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
308 if (ret) {
309 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
310 goto remove_notify;
313 return 0;
315 remove_notify:
316 acpi_remove_notify_handler(adev->handle,
317 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
318 remove_zone:
319 int340x_thermal_zone_remove(proc_priv->int340x_zone);
321 return ret;
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);
344 return 0;
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);
359 return 0;
361 EXPORT_SYMBOL_GPL(proc_thermal_resume);
363 #define MCHBAR 0
365 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
367 int ret;
369 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
370 if (ret) {
371 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
372 return -ENOMEM;
375 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
377 return 0;
380 int proc_thermal_mmio_add(struct pci_dev *pdev,
381 struct proc_thermal_device *proc_priv,
382 kernel_ulong_t feature_mask)
384 int ret;
386 proc_priv->mmio_feature_mask = feature_mask;
388 if (feature_mask) {
389 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
390 if (ret)
391 return ret;
394 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
395 ret = proc_thermal_rapl_add(pdev, proc_priv);
396 if (ret) {
397 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
398 return ret;
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);
406 if (ret) {
407 dev_err(&pdev->dev, "failed to add RFIM interface\n");
408 goto err_rem_rapl;
412 if (feature_mask & PROC_THERMAL_FEATURE_WT_REQ) {
413 ret = proc_thermal_wt_req_add(pdev, proc_priv);
414 if (ret) {
415 dev_err(&pdev->dev, "failed to add MBOX interface\n");
416 goto err_rem_rfim;
418 } else if (feature_mask & PROC_THERMAL_FEATURE_WT_HINT) {
419 ret = proc_thermal_wt_hint_add(pdev, proc_priv);
420 if (ret) {
421 dev_err(&pdev->dev, "failed to add WT Hint\n");
422 goto err_rem_rfim;
426 return 0;
428 err_rem_rfim:
429 proc_thermal_rfim_remove(pdev);
430 err_rem_rapl:
431 proc_thermal_rapl_remove();
433 return ret;
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");