Loading...
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/kernel.h>
8#include <linux/module.h>
9#include <linux/pci.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"
14
15#define DRV_NAME "proc_thermal"
16
17#define POWER_LIMIT_SHOW(index, suffix) \
18static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
19 struct device_attribute *attr, \
20 char *buf) \
21{ \
22 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
23 \
24 return sprintf(buf, "%lu\n",\
25 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
26}
27
28POWER_LIMIT_SHOW(0, min_uw)
29POWER_LIMIT_SHOW(0, max_uw)
30POWER_LIMIT_SHOW(0, step_uw)
31POWER_LIMIT_SHOW(0, tmin_us)
32POWER_LIMIT_SHOW(0, tmax_us)
33
34POWER_LIMIT_SHOW(1, min_uw)
35POWER_LIMIT_SHOW(1, max_uw)
36POWER_LIMIT_SHOW(1, step_uw)
37POWER_LIMIT_SHOW(1, tmin_us)
38POWER_LIMIT_SHOW(1, tmax_us)
39
40static DEVICE_ATTR_RO(power_limit_0_min_uw);
41static DEVICE_ATTR_RO(power_limit_0_max_uw);
42static DEVICE_ATTR_RO(power_limit_0_step_uw);
43static DEVICE_ATTR_RO(power_limit_0_tmin_us);
44static DEVICE_ATTR_RO(power_limit_0_tmax_us);
45
46static DEVICE_ATTR_RO(power_limit_1_min_uw);
47static DEVICE_ATTR_RO(power_limit_1_max_uw);
48static DEVICE_ATTR_RO(power_limit_1_step_uw);
49static DEVICE_ATTR_RO(power_limit_1_tmin_us);
50static DEVICE_ATTR_RO(power_limit_1_tmax_us);
51
52static 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,
63 NULL
64};
65
66static const struct attribute_group power_limit_attribute_group = {
67 .attrs = power_limit_attrs,
68 .name = "power_limits"
69};
70
71static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
73{
74 u64 val;
75 int err;
76
77 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
78 if (err)
79 return err;
80
81 val = (val >> 24) & 0x3f;
82 return sprintf(buf, "%d\n", (int)val);
83}
84
85static int tcc_offset_update(unsigned int tcc)
86{
87 u64 val;
88 int err;
89
90 if (tcc > 63)
91 return -EINVAL;
92
93 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
94 if (err)
95 return err;
96
97 if (val & BIT(31))
98 return -EPERM;
99
100 val &= ~GENMASK_ULL(29, 24);
101 val |= (tcc & 0x3f) << 24;
102
103 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
104 if (err)
105 return err;
106
107 return 0;
108}
109
110static int tcc_offset_save = -1;
111
112static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
113 struct device_attribute *attr, const char *buf,
114 size_t count)
115{
116 unsigned int tcc;
117 u64 val;
118 int err;
119
120 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
121 if (err)
122 return err;
123
124 if (!(val & BIT(30)))
125 return -EACCES;
126
127 if (kstrtouint(buf, 0, &tcc))
128 return -EINVAL;
129
130 err = tcc_offset_update(tcc);
131 if (err)
132 return err;
133
134 tcc_offset_save = tcc;
135
136 return count;
137}
138
139static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
140
141static int stored_tjmax; /* since it is fixed, we can have local storage */
142
143static int get_tjmax(void)
144{
145 u32 eax, edx;
146 u32 val;
147 int err;
148
149 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
150 if (err)
151 return err;
152
153 val = (eax >> 16) & 0xff;
154 if (val)
155 return val;
156
157 return -EINVAL;
158}
159
160static int read_temp_msr(int *temp)
161{
162 int cpu;
163 u32 eax, edx;
164 int err;
165 unsigned long curr_temp_off = 0;
166
167 *temp = 0;
168
169 for_each_online_cpu(cpu) {
170 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
171 &edx);
172 if (err)
173 goto err_ret;
174 else {
175 if (eax & 0x80000000) {
176 curr_temp_off = (eax >> 16) & 0x7f;
177 if (!*temp || curr_temp_off < *temp)
178 *temp = curr_temp_off;
179 } else {
180 err = -EINVAL;
181 goto err_ret;
182 }
183 }
184 }
185
186 return 0;
187err_ret:
188 return err;
189}
190
191static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
192 int *temp)
193{
194 int ret;
195
196 ret = read_temp_msr(temp);
197 if (!ret)
198 *temp = (stored_tjmax - *temp) * 1000;
199
200 return ret;
201}
202
203static struct thermal_zone_device_ops proc_thermal_local_ops = {
204 .get_temp = proc_thermal_get_zone_temp,
205};
206
207static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
208{
209 int i;
210 acpi_status status;
211 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
212 union acpi_object *elements, *ppcc;
213 union acpi_object *p;
214 int ret = 0;
215
216 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
217 NULL, &buf);
218 if (ACPI_FAILURE(status))
219 return -ENODEV;
220
221 p = buf.pointer;
222 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
223 dev_err(proc_priv->dev, "Invalid PPCC data\n");
224 ret = -EFAULT;
225 goto free_buffer;
226 }
227
228 if (!p->package.count) {
229 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
230 ret = -EFAULT;
231 goto free_buffer;
232 }
233
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) {
238 ret = -EFAULT;
239 goto free_buffer;
240 }
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;
248 }
249
250free_buffer:
251 kfree(buf.pointer);
252
253 return ret;
254}
255
256#define PROC_POWER_CAPABILITY_CHANGED 0x83
257static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
258{
259 struct proc_thermal_device *proc_priv = data;
260
261 if (!proc_priv)
262 return;
263
264 switch (event) {
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);
269 break;
270 default:
271 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
272 break;
273 }
274}
275
276int proc_thermal_add(struct device *dev, struct proc_thermal_device *proc_priv)
277{
278 struct acpi_device *adev;
279 acpi_status status;
280 unsigned long long tmp;
281 struct thermal_zone_device_ops *ops = NULL;
282 int ret;
283
284 adev = ACPI_COMPANION(dev);
285 if (!adev)
286 return -ENODEV;
287
288 proc_priv->dev = dev;
289 proc_priv->adev = adev;
290
291 ret = proc_thermal_read_ppcc(proc_priv);
292 if (ret)
293 return ret;
294
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;
301 }
302
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);
306 } else
307 ret = 0;
308
309 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
310 proc_thermal_notify,
311 (void *)proc_priv);
312 if (ret)
313 goto remove_zone;
314
315 ret = sysfs_create_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
316 if (ret)
317 goto remove_notify;
318
319 ret = sysfs_create_group(&dev->kobj, &power_limit_attribute_group);
320 if (ret) {
321 sysfs_remove_file(&dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
322 goto remove_notify;
323 }
324
325 return 0;
326
327remove_notify:
328 acpi_remove_notify_handler(adev->handle,
329 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
330remove_zone:
331 int340x_thermal_zone_remove(proc_priv->int340x_zone);
332
333 return ret;
334}
335EXPORT_SYMBOL_GPL(proc_thermal_add);
336
337void proc_thermal_remove(struct proc_thermal_device *proc_priv)
338{
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);
345}
346EXPORT_SYMBOL_GPL(proc_thermal_remove);
347
348int proc_thermal_resume(struct device *dev)
349{
350 struct proc_thermal_device *proc_dev;
351
352 proc_dev = dev_get_drvdata(dev);
353 proc_thermal_read_ppcc(proc_dev);
354
355 if (tcc_offset_save >= 0)
356 tcc_offset_update(tcc_offset_save);
357
358 return 0;
359}
360EXPORT_SYMBOL_GPL(proc_thermal_resume);
361
362#define MCHBAR 0
363
364static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
365{
366 int ret;
367
368 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
369 if (ret) {
370 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
371 return -ENOMEM;
372 }
373
374 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
375
376 return 0;
377}
378
379int proc_thermal_mmio_add(struct pci_dev *pdev,
380 struct proc_thermal_device *proc_priv,
381 kernel_ulong_t feature_mask)
382{
383 int ret;
384
385 proc_priv->mmio_feature_mask = feature_mask;
386
387 if (feature_mask) {
388 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
389 if (ret)
390 return ret;
391 }
392
393 if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
394 ret = proc_thermal_rapl_add(pdev, proc_priv);
395 if (ret) {
396 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
397 return ret;
398 }
399 }
400
401 if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
402 feature_mask & PROC_THERMAL_FEATURE_DVFS) {
403 ret = proc_thermal_rfim_add(pdev, proc_priv);
404 if (ret) {
405 dev_err(&pdev->dev, "failed to add RFIM interface\n");
406 goto err_rem_rapl;
407 }
408 }
409
410 if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
411 ret = proc_thermal_mbox_add(pdev, proc_priv);
412 if (ret) {
413 dev_err(&pdev->dev, "failed to add MBOX interface\n");
414 goto err_rem_rfim;
415 }
416 }
417
418 return 0;
419
420err_rem_rfim:
421 proc_thermal_rfim_remove(pdev);
422err_rem_rapl:
423 proc_thermal_rapl_remove();
424
425 return ret;
426}
427EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
428
429void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
430{
431 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
432 proc_thermal_rapl_remove();
433
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);
437
438 if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
439 proc_thermal_mbox_remove(pdev);
440}
441EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
442
443MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
444MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
445MODULE_LICENSE("GPL v2");