ARM: 9148/1: handle CONFIG_CPU_ENDIAN_BE32 in arch/arm/kernel/head.S
[platform/kernel/linux-rpi.git] / drivers / thermal / intel / int340x_thermal / processor_thermal_device.c
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) \
18 static 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
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)
33
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)
39
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);
45
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);
51
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,
63         NULL
64 };
65
66 static const struct attribute_group power_limit_attribute_group = {
67         .attrs = power_limit_attrs,
68         .name = "power_limits"
69 };
70
71 static 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
85 static 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
110 static unsigned int tcc_offset_save;
111
112 static 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
139 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
140
141 static int stored_tjmax; /* since it is fixed, we can have local storage */
142
143 static 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
160 static 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;
187 err_ret:
188         return err;
189 }
190
191 static 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
203 static struct thermal_zone_device_ops proc_thermal_local_ops = {
204         .get_temp       = proc_thermal_get_zone_temp,
205 };
206
207 static 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
250 free_buffer:
251         kfree(buf.pointer);
252
253         return ret;
254 }
255
256 #define PROC_POWER_CAPABILITY_CHANGED   0x83
257 static 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
276 int 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
327 remove_notify:
328         acpi_remove_notify_handler(adev->handle,
329                                     ACPI_DEVICE_NOTIFY, proc_thermal_notify);
330 remove_zone:
331         int340x_thermal_zone_remove(proc_priv->int340x_zone);
332
333         return ret;
334 }
335 EXPORT_SYMBOL_GPL(proc_thermal_add);
336
337 void 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 }
346 EXPORT_SYMBOL_GPL(proc_thermal_remove);
347
348 int 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         tcc_offset_update(tcc_offset_save);
356
357         return 0;
358 }
359 EXPORT_SYMBOL_GPL(proc_thermal_resume);
360
361 #define MCHBAR 0
362
363 static int proc_thermal_set_mmio_base(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
364 {
365         int ret;
366
367         ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
368         if (ret) {
369                 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
370                 return -ENOMEM;
371         }
372
373         proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
374
375         return 0;
376 }
377
378 int proc_thermal_mmio_add(struct pci_dev *pdev,
379                           struct proc_thermal_device *proc_priv,
380                           kernel_ulong_t feature_mask)
381 {
382         int ret;
383
384         proc_priv->mmio_feature_mask = feature_mask;
385
386         if (feature_mask) {
387                 ret = proc_thermal_set_mmio_base(pdev, proc_priv);
388                 if (ret)
389                         return ret;
390         }
391
392         if (feature_mask & PROC_THERMAL_FEATURE_RAPL) {
393                 ret = proc_thermal_rapl_add(pdev, proc_priv);
394                 if (ret) {
395                         dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
396                         return ret;
397                 }
398         }
399
400         if (feature_mask & PROC_THERMAL_FEATURE_FIVR ||
401             feature_mask & PROC_THERMAL_FEATURE_DVFS) {
402                 ret = proc_thermal_rfim_add(pdev, proc_priv);
403                 if (ret) {
404                         dev_err(&pdev->dev, "failed to add RFIM interface\n");
405                         goto err_rem_rapl;
406                 }
407         }
408
409         if (feature_mask & PROC_THERMAL_FEATURE_MBOX) {
410                 ret = proc_thermal_mbox_add(pdev, proc_priv);
411                 if (ret) {
412                         dev_err(&pdev->dev, "failed to add MBOX interface\n");
413                         goto err_rem_rfim;
414                 }
415         }
416
417         return 0;
418
419 err_rem_rfim:
420         proc_thermal_rfim_remove(pdev);
421 err_rem_rapl:
422         proc_thermal_rapl_remove();
423
424         return ret;
425 }
426 EXPORT_SYMBOL_GPL(proc_thermal_mmio_add);
427
428 void proc_thermal_mmio_remove(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
429 {
430         if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_RAPL)
431                 proc_thermal_rapl_remove();
432
433         if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_FIVR ||
434             proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_DVFS)
435                 proc_thermal_rfim_remove(pdev);
436
437         if (proc_priv->mmio_feature_mask & PROC_THERMAL_FEATURE_MBOX)
438                 proc_thermal_mbox_remove(pdev);
439 }
440 EXPORT_SYMBOL_GPL(proc_thermal_mmio_remove);
441
442 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
443 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
444 MODULE_LICENSE("GPL v2");