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/sysfs.h>
12 #include <linux/thermal.h>
13 #include <asm/msr.h>
14 #include "int340x_thermal_zone.h"
15 #include "processor_thermal_device.h"
16 #include "../intel_soc_dts_iosf.h"
17
18 #define DRV_NAME "proc_thermal"
19
20 #define POWER_LIMIT_SHOW(index, suffix) \
21 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
22 struct device_attribute *attr, \
23 char *buf) \
24 { \
25 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
26 \
27 return sysfs_emit(buf, "%lu\n",\
28 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
29 }
30
power_floor_status_show(struct device * dev,struct device_attribute * attr,char * buf)31 static ssize_t power_floor_status_show(struct device *dev,
32 struct device_attribute *attr,
33 char *buf)
34 {
35 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
36 int ret;
37
38 ret = proc_thermal_read_power_floor_status(proc_dev);
39
40 return sysfs_emit(buf, "%d\n", ret);
41 }
42
power_floor_enable_show(struct device * dev,struct device_attribute * attr,char * buf)43 static ssize_t power_floor_enable_show(struct device *dev,
44 struct device_attribute *attr,
45 char *buf)
46 {
47 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
48 bool ret;
49
50 ret = proc_thermal_power_floor_get_state(proc_dev);
51
52 return sysfs_emit(buf, "%d\n", ret);
53 }
54
power_floor_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)55 static ssize_t power_floor_enable_store(struct device *dev,
56 struct device_attribute *attr,
57 const char *buf, size_t count)
58 {
59 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev);
60 u8 state;
61 int ret;
62
63 if (kstrtou8(buf, 0, &state))
64 return -EINVAL;
65
66 ret = proc_thermal_power_floor_set_state(proc_dev, !!state);
67 if (ret)
68 return ret;
69
70 return count;
71 }
72
73 POWER_LIMIT_SHOW(0, min_uw)
74 POWER_LIMIT_SHOW(0, max_uw)
75 POWER_LIMIT_SHOW(0, step_uw)
76 POWER_LIMIT_SHOW(0, tmin_us)
77 POWER_LIMIT_SHOW(0, tmax_us)
78
79 POWER_LIMIT_SHOW(1, min_uw)
80 POWER_LIMIT_SHOW(1, max_uw)
81 POWER_LIMIT_SHOW(1, step_uw)
82 POWER_LIMIT_SHOW(1, tmin_us)
83 POWER_LIMIT_SHOW(1, tmax_us)
84
85 static DEVICE_ATTR_RO(power_limit_0_min_uw);
86 static DEVICE_ATTR_RO(power_limit_0_max_uw);
87 static DEVICE_ATTR_RO(power_limit_0_step_uw);
88 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
89 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
90
91 static DEVICE_ATTR_RO(power_limit_1_min_uw);
92 static DEVICE_ATTR_RO(power_limit_1_max_uw);
93 static DEVICE_ATTR_RO(power_limit_1_step_uw);
94 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
95 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
96
97 static DEVICE_ATTR_RO(power_floor_status);
98 static DEVICE_ATTR_RW(power_floor_enable);
99
100 static struct attribute *power_limit_attrs[] = {
101 &dev_attr_power_limit_0_min_uw.attr,
102 &dev_attr_power_limit_1_min_uw.attr,
103 &dev_attr_power_limit_0_max_uw.attr,
104 &dev_attr_power_limit_1_max_uw.attr,
105 &dev_attr_power_limit_0_step_uw.attr,
106 &dev_attr_power_limit_1_step_uw.attr,
107 &dev_attr_power_limit_0_tmin_us.attr,
108 &dev_attr_power_limit_1_tmin_us.attr,
109 &dev_attr_power_limit_0_tmax_us.attr,
110 &dev_attr_power_limit_1_tmax_us.attr,
111 &dev_attr_power_floor_status.attr,
112 &dev_attr_power_floor_enable.attr,
113 NULL
114 };
115
power_limit_attr_visible(struct kobject * kobj,struct attribute * attr,int unused)116 static umode_t power_limit_attr_visible(struct kobject *kobj, struct attribute *attr, int unused)
117 {
118 struct device *dev = kobj_to_dev(kobj);
119 struct proc_thermal_device *proc_dev;
120
121 if (attr != &dev_attr_power_floor_status.attr && attr != &dev_attr_power_floor_enable.attr)
122 return attr->mode;
123
124 proc_dev = dev_get_drvdata(dev);
125 if (!proc_dev || !(proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR))
126 return 0;
127
128 return attr->mode;
129 }
130
131 static const struct attribute_group power_limit_attribute_group = {
132 .attrs = power_limit_attrs,
133 .name = "power_limits",
134 .is_visible = power_limit_attr_visible,
135 };
136
tcc_offset_degree_celsius_show(struct device * dev,struct device_attribute * attr,char * buf)137 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
138 struct device_attribute *attr,
139 char *buf)
140 {
141 int offset;
142
143 offset = intel_tcc_get_offset(-1);
144 if (offset < 0)
145 return offset;
146
147 return sysfs_emit(buf, "%d\n", offset);
148 }
149
tcc_offset_degree_celsius_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)150 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
151 struct device_attribute *attr, const char *buf,
152 size_t count)
153 {
154 unsigned int tcc;
155 u64 val;
156 int err;
157
158 err = rdmsrq_safe(MSR_PLATFORM_INFO, &val);
159 if (err)
160 return err;
161
162 if (!(val & BIT(30)))
163 return -EACCES;
164
165 if (kstrtouint(buf, 0, &tcc))
166 return -EINVAL;
167
168 err = intel_tcc_set_offset(-1, tcc);
169 if (err)
170 return err;
171
172 return count;
173 }
174
175 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
176
proc_thermal_get_zone_temp(struct thermal_zone_device * zone,int * temp)177 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
178 int *temp)
179 {
180 int cpu;
181 int curr_temp, ret;
182
183 *temp = 0;
184
185 for_each_online_cpu(cpu) {
186 ret = intel_tcc_get_temp(cpu, &curr_temp, false);
187 if (ret < 0)
188 return ret;
189 if (!*temp || curr_temp > *temp)
190 *temp = curr_temp;
191 }
192
193 *temp *= 1000;
194
195 return 0;
196 }
197
proc_thermal_read_ppcc(struct proc_thermal_device * proc_priv)198 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
199 {
200 int i;
201 acpi_status status;
202 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
203 union acpi_object *elements, *ppcc;
204 union acpi_object *p;
205 int ret = 0;
206
207 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
208 NULL, &buf);
209 if (ACPI_FAILURE(status))
210 return -ENODEV;
211
212 p = buf.pointer;
213 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
214 dev_err(proc_priv->dev, "Invalid PPCC data\n");
215 ret = -EFAULT;
216 goto free_buffer;
217 }
218
219 if (!p->package.count) {
220 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
221 ret = -EFAULT;
222 goto free_buffer;
223 }
224
225 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
226 elements = &(p->package.elements[i+1]);
227 if (elements->type != ACPI_TYPE_PACKAGE ||
228 elements->package.count != 6) {
229 ret = -EFAULT;
230 goto free_buffer;
231 }
232 ppcc = elements->package.elements;
233 proc_priv->power_limits[i].index = ppcc[0].integer.value;
234 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
235 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
236 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
237 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
238 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
239 }
240
241 free_buffer:
242 kfree(buf.pointer);
243
244 return ret;
245 }
246
247 #define PROC_POWER_CAPABILITY_CHANGED 0x83
proc_thermal_notify(acpi_handle handle,u32 event,void * data)248 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
249 {
250 struct proc_thermal_device *proc_priv = data;
251
252 if (!proc_priv)
253 return;
254
255 switch (event) {
256 case PROC_POWER_CAPABILITY_CHANGED:
257 proc_thermal_read_ppcc(proc_priv);
258 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
259 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
260 break;
261 default:
262 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
263 break;
264 }
265 }
266
proc_thermal_add(struct device * dev,struct proc_thermal_device * proc_priv)267 int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
268 {
269 struct acpi_device *adev;
270 acpi_status status;
271 unsigned long long tmp;
272 int (*get_temp) (struct thermal_zone_device *, int *) = NULL;
273 int ret;
274
275 adev = ACPI_COMPANION(dev);
276 if (!adev)
277 return -ENODEV;
278
279 proc_priv->dev = dev;
280 proc_priv->adev = adev;
281
282 ret = proc_thermal_read_ppcc(proc_priv);
283 if (ret)
284 return ret;
285
286 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
287 if (ACPI_FAILURE(status)) {
288 /* there is no _TMP method, add local method */
289 if (intel_tcc_get_tjmax(-1) > 0)
290 get_temp = proc_thermal_get_zone_temp;
291 }
292
293 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, get_temp);
294 if (IS_ERR(proc_priv->int340x_zone)) {
295 return PTR_ERR(proc_priv->int340x_zone);
296 } else
297 ret = 0;
298
299 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
300 proc_thermal_notify,
301 (void *)proc_priv);
302 if (ret)
303 goto remove_zone;
304
305 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
306 if (ret)
307 goto remove_notify;
308
309 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
310 if (ret) {
311 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
312 goto remove_notify;
313 }
314
315 return 0;
316
317 remove_notify:
318 acpi_remove_notify_handler(adev->handle,
319 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
320 remove_zone:
321 int340x_thermal_zone_remove(proc_priv->int340x_zone);
322
323 return ret;
324 }
325 EXPORT_SYMBOL_GPL(proc_thermal_add);
326
proc_thermal_remove(struct proc_thermal_device * proc_priv)327 void proc_thermal_remove(struct proc_thermal_device *proc_priv)
328 {
329 acpi_remove_notify_handler(proc_priv->adev->handle,
330 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
331 int340x_thermal_zone_remove(proc_priv->int340x_zone);
332 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
333 sysfs_remove_group(&proc_priv->dev->kobj,
334 &power_limit_attribute_group);
335 }
336 EXPORT_SYMBOL_GPL(proc_thermal_remove);
337
338 static int tcc_offset_save = -1;
339
proc_thermal_suspend(struct device * dev)340 int proc_thermal_suspend(struct device *dev)
341 {
342 struct proc_thermal_device *proc_dev;
343
344 tcc_offset_save = intel_tcc_get_offset(-1);
345 if (tcc_offset_save < 0)
346 dev_warn(dev, "failed to save offset (%d)\n", tcc_offset_save);
347
348 proc_dev = dev_get_drvdata(dev);
349
350 if (proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_SOC_POWER_SLIDER)
351 proc_thermal_soc_power_slider_suspend(proc_dev);
352
353 return 0;
354 }
355 EXPORT_SYMBOL_GPL(proc_thermal_suspend);
356
proc_thermal_resume(struct device * dev)357 int proc_thermal_resume(struct device *dev)
358 {
359 struct proc_thermal_device *proc_dev;
360
361 proc_dev = dev_get_drvdata(dev);
362 proc_thermal_read_ppcc(proc_dev);
363
364 /* Do not update if saving failed */
365 if (tcc_offset_save >= 0)
366 intel_tcc_set_offset(-1, tcc_offset_save);
367
368 if (proc_dev->mmio_feature_mask & PROC_THERMAL_FEATURE_SOC_POWER_SLIDER)
369 proc_thermal_soc_power_slider_resume(proc_dev);
370
371 return 0;
372 }
373 EXPORT_SYMBOL_GPL(proc_thermal_resume);
374
375 #define MCHBAR 0
376
proc_thermal_set_mmio_base(struct pci_dev * pdev,struct proc_thermal_device * proc_priv)377 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
378 {
379 int ret;
380
381 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
382 if (ret) {
383 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
384 return -ENOMEM;
385 }
386
387 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
388
389 return 0;
390 }
391
proc_thermal_mmio_add(struct pci_dev * pdev,struct proc_thermal_device * proc_priv,kernel_ulong_t feature_mask)392 int proc_thermal_mmio_add(struct pci_dev *pdev,
393 struct proc_thermal_device *proc_priv,
394 kernel_ulong_t feature_mask)
395 {
396 int ret;
397
398 proc_priv->mmio_feature_mask = feature_mask;
399
400 if (feature_mask) {
401 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
402 if (ret)
403 return ret;
404 }
405
406 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
407 ret = proc_thermal_rapl_add(pdev, proc_priv);
408 if (ret) {
409 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
410 return ret;
411 }
412 }
413
414 if (feature_mask & PROC_THERMAL_FEATURE_PTC) {
415 ret = proc_thermal_ptc_add(pdev, proc_priv);
416 if (ret) {
417 dev_err(&pdev->dev, "failed to add PTC MMIO interface\n");
418 goto err_rem_rapl;
419 }
420 }
421
422 if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
423 feature_mask & PROC_THERMAL_FEATURE_DVFS ||
424 feature_mask & PROC_THERMAL_FEATURE_DLVR) {
425 ret = proc_thermal_rfim_add(pdev, proc_priv);
426 if (ret) {
427 dev_err(&pdev->dev, "failed to add RFIM interface\n");
428 goto err_rem_ptc;
429 }
430 }
431
432 if (feature_mask & PROC_THERMAL_FEATURE_WT_REQ) {
433 ret = proc_thermal_wt_req_add(pdev, proc_priv);
434 if (ret) {
435 dev_err(&pdev->dev, "failed to add MBOX interface\n");
436 goto err_rem_rfim;
437 }
438 } else if (feature_mask & PROC_THERMAL_FEATURE_WT_HINT) {
439 ret = proc_thermal_wt_hint_add(pdev, proc_priv);
440 if (ret) {
441 dev_err(&pdev->dev, "failed to add WT Hint\n");
442 goto err_rem_rfim;
443 }
444 }
445
446 if (feature_mask & PROC_THERMAL_FEATURE_SOC_POWER_SLIDER) {
447 ret = proc_thermal_soc_power_slider_add(pdev, proc_priv);
448 if (ret) {
449 dev_info(&pdev->dev, "failed to add soc power efficiency slider\n");
450 goto err_rem_wlt;
451 }
452 }
453
454 return 0;
455
456 err_rem_wlt:
457 proc_thermal_wt_hint_remove(pdev);
458 err_rem_rfim:
459 proc_thermal_rfim_remove(pdev);
460 err_rem_ptc:
461 proc_thermal_ptc_remove(pdev);
462 err_rem_rapl:
463 proc_thermal_rapl_remove();
464
465 return ret;
466 }
467 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
468
proc_thermal_mmio_remove(struct pci_dev * pdev,struct proc_thermal_device * proc_priv)469 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
470 {
471 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
472 proc_thermal_rapl_remove();
473
474 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_PTC)
475 proc_thermal_ptc_remove(pdev);
476
477 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
478 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS ||
479 proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DLVR)
480 proc_thermal_rfim_remove(pdev);
481
482 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_POWER_FLOOR)
483 proc_thermal_power_floor_set_state(proc_priv, false);
484
485 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_WT_REQ)
486 proc_thermal_wt_req_remove(pdev);
487 else if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_WT_HINT)
488 proc_thermal_wt_hint_remove(pdev);
489 }
490 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
491
492 MODULE_IMPORT_NS("INTEL_TCC");
493 MODULE_IMPORT_NS("INT340X_THERMAL");
494 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
495 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
496 MODULE_LICENSE("GPL v2");
497