Merge branches 'thermal-core', 'thermal-tool', 'thermal-intel' and 'thermal-soc'...
[sfrench/cifs-2.6.git] / drivers / thermal / int340x_thermal / processor_thermal_device.c
1 /*
2  * processor_thermal_device.c
3  * Copyright (c) 2014, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/acpi.h>
22 #include <linux/thermal.h>
23 #include "int340x_thermal_zone.h"
24 #include "../intel_soc_dts_iosf.h"
25
26 /* Broadwell-U/HSB thermal reporting device */
27 #define PCI_DEVICE_ID_PROC_BDW_THERMAL  0x1603
28 #define PCI_DEVICE_ID_PROC_HSB_THERMAL  0x0A03
29
30 /* Skylake thermal reporting device */
31 #define PCI_DEVICE_ID_PROC_SKL_THERMAL  0x1903
32
33 /* CannonLake thermal reporting device */
34 #define PCI_DEVICE_ID_PROC_CNL_THERMAL  0x5a03
35 #define PCI_DEVICE_ID_PROC_CFL_THERMAL  0x3E83
36
37 /* Braswell thermal reporting device */
38 #define PCI_DEVICE_ID_PROC_BSW_THERMAL  0x22DC
39
40 /* Broxton thermal reporting device */
41 #define PCI_DEVICE_ID_PROC_BXT0_THERMAL  0x0A8C
42 #define PCI_DEVICE_ID_PROC_BXT1_THERMAL  0x1A8C
43 #define PCI_DEVICE_ID_PROC_BXTX_THERMAL  0x4A8C
44 #define PCI_DEVICE_ID_PROC_BXTP_THERMAL  0x5A8C
45
46 struct power_config {
47         u32     index;
48         u32     min_uw;
49         u32     max_uw;
50         u32     tmin_us;
51         u32     tmax_us;
52         u32     step_uw;
53 };
54
55 struct proc_thermal_device {
56         struct device *dev;
57         struct acpi_device *adev;
58         struct power_config power_limits[2];
59         struct int34x_thermal_zone *int340x_zone;
60         struct intel_soc_dts_sensors *soc_dts;
61 };
62
63 enum proc_thermal_emum_mode_type {
64         PROC_THERMAL_NONE,
65         PROC_THERMAL_PCI,
66         PROC_THERMAL_PLATFORM_DEV
67 };
68
69 /*
70  * We can have only one type of enumeration, PCI or Platform,
71  * not both. So we don't need instance specific data.
72  */
73 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
74                                                         PROC_THERMAL_NONE;
75
76 #define POWER_LIMIT_SHOW(index, suffix) \
77 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
78                                         struct device_attribute *attr, \
79                                         char *buf) \
80 { \
81         struct pci_dev *pci_dev; \
82         struct platform_device *pdev; \
83         struct proc_thermal_device *proc_dev; \
84 \
85         if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
86                 pdev = to_platform_device(dev); \
87                 proc_dev = platform_get_drvdata(pdev); \
88         } else { \
89                 pci_dev = to_pci_dev(dev); \
90                 proc_dev = pci_get_drvdata(pci_dev); \
91         } \
92         return sprintf(buf, "%lu\n",\
93         (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
94 }
95
96 POWER_LIMIT_SHOW(0, min_uw)
97 POWER_LIMIT_SHOW(0, max_uw)
98 POWER_LIMIT_SHOW(0, step_uw)
99 POWER_LIMIT_SHOW(0, tmin_us)
100 POWER_LIMIT_SHOW(0, tmax_us)
101
102 POWER_LIMIT_SHOW(1, min_uw)
103 POWER_LIMIT_SHOW(1, max_uw)
104 POWER_LIMIT_SHOW(1, step_uw)
105 POWER_LIMIT_SHOW(1, tmin_us)
106 POWER_LIMIT_SHOW(1, tmax_us)
107
108 static DEVICE_ATTR_RO(power_limit_0_min_uw);
109 static DEVICE_ATTR_RO(power_limit_0_max_uw);
110 static DEVICE_ATTR_RO(power_limit_0_step_uw);
111 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
112 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
113
114 static DEVICE_ATTR_RO(power_limit_1_min_uw);
115 static DEVICE_ATTR_RO(power_limit_1_max_uw);
116 static DEVICE_ATTR_RO(power_limit_1_step_uw);
117 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
118 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
119
120 static struct attribute *power_limit_attrs[] = {
121         &dev_attr_power_limit_0_min_uw.attr,
122         &dev_attr_power_limit_1_min_uw.attr,
123         &dev_attr_power_limit_0_max_uw.attr,
124         &dev_attr_power_limit_1_max_uw.attr,
125         &dev_attr_power_limit_0_step_uw.attr,
126         &dev_attr_power_limit_1_step_uw.attr,
127         &dev_attr_power_limit_0_tmin_us.attr,
128         &dev_attr_power_limit_1_tmin_us.attr,
129         &dev_attr_power_limit_0_tmax_us.attr,
130         &dev_attr_power_limit_1_tmax_us.attr,
131         NULL
132 };
133
134 static const struct attribute_group power_limit_attribute_group = {
135         .attrs = power_limit_attrs,
136         .name = "power_limits"
137 };
138
139 static int stored_tjmax; /* since it is fixed, we can have local storage */
140
141 static int get_tjmax(void)
142 {
143         u32 eax, edx;
144         u32 val;
145         int err;
146
147         err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
148         if (err)
149                 return err;
150
151         val = (eax >> 16) & 0xff;
152         if (val)
153                 return val;
154
155         return -EINVAL;
156 }
157
158 static int read_temp_msr(int *temp)
159 {
160         int cpu;
161         u32 eax, edx;
162         int err;
163         unsigned long curr_temp_off = 0;
164
165         *temp = 0;
166
167         for_each_online_cpu(cpu) {
168                 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
169                                         &edx);
170                 if (err)
171                         goto err_ret;
172                 else {
173                         if (eax & 0x80000000) {
174                                 curr_temp_off = (eax >> 16) & 0x7f;
175                                 if (!*temp || curr_temp_off < *temp)
176                                         *temp = curr_temp_off;
177                         } else {
178                                 err = -EINVAL;
179                                 goto err_ret;
180                         }
181                 }
182         }
183
184         return 0;
185 err_ret:
186         return err;
187 }
188
189 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
190                                          int *temp)
191 {
192         int ret;
193
194         ret = read_temp_msr(temp);
195         if (!ret)
196                 *temp = (stored_tjmax - *temp) * 1000;
197
198         return ret;
199 }
200
201 static struct thermal_zone_device_ops proc_thermal_local_ops = {
202         .get_temp       = proc_thermal_get_zone_temp,
203 };
204
205 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
206 {
207         int i;
208         acpi_status status;
209         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
210         union acpi_object *elements, *ppcc;
211         union acpi_object *p;
212         int ret = 0;
213
214         status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
215                                       NULL, &buf);
216         if (ACPI_FAILURE(status))
217                 return -ENODEV;
218
219         p = buf.pointer;
220         if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
221                 dev_err(proc_priv->dev, "Invalid PPCC data\n");
222                 ret = -EFAULT;
223                 goto free_buffer;
224         }
225
226         if (!p->package.count) {
227                 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
228                 ret = -EFAULT;
229                 goto free_buffer;
230         }
231
232         for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
233                 elements = &(p->package.elements[i+1]);
234                 if (elements->type != ACPI_TYPE_PACKAGE ||
235                     elements->package.count != 6) {
236                         ret = -EFAULT;
237                         goto free_buffer;
238                 }
239                 ppcc = elements->package.elements;
240                 proc_priv->power_limits[i].index = ppcc[0].integer.value;
241                 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
242                 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
243                 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
244                 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
245                 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
246         }
247
248 free_buffer:
249         kfree(buf.pointer);
250
251         return ret;
252 }
253
254 #define PROC_POWER_CAPABILITY_CHANGED   0x83
255 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
256 {
257         struct proc_thermal_device *proc_priv = data;
258
259         if (!proc_priv)
260                 return;
261
262         switch (event) {
263         case PROC_POWER_CAPABILITY_CHANGED:
264                 proc_thermal_read_ppcc(proc_priv);
265                 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
266                                 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
267                 break;
268         default:
269                 dev_err(proc_priv->dev, "Unsupported event [0x%x]\n", event);
270                 break;
271         }
272 }
273
274
275 static int proc_thermal_add(struct device *dev,
276                             struct proc_thermal_device **priv)
277 {
278         struct proc_thermal_device *proc_priv;
279         struct acpi_device *adev;
280         acpi_status status;
281         unsigned long long tmp;
282         struct thermal_zone_device_ops *ops = NULL;
283         int ret;
284
285         adev = ACPI_COMPANION(dev);
286         if (!adev)
287                 return -ENODEV;
288
289         proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
290         if (!proc_priv)
291                 return -ENOMEM;
292
293         proc_priv->dev = dev;
294         proc_priv->adev = adev;
295         *priv = proc_priv;
296
297         ret = proc_thermal_read_ppcc(proc_priv);
298         if (!ret) {
299                 ret = sysfs_create_group(&dev->kobj,
300                                          &power_limit_attribute_group);
301
302         }
303         if (ret)
304                 return ret;
305
306         status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
307         if (ACPI_FAILURE(status)) {
308                 /* there is no _TMP method, add local method */
309                 stored_tjmax = get_tjmax();
310                 if (stored_tjmax > 0)
311                         ops = &proc_thermal_local_ops;
312         }
313
314         proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
315         if (IS_ERR(proc_priv->int340x_zone)) {
316                 ret = PTR_ERR(proc_priv->int340x_zone);
317                 goto remove_group;
318         } else
319                 ret = 0;
320
321         ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
322                                           proc_thermal_notify,
323                                           (void *)proc_priv);
324         if (ret)
325                 goto remove_zone;
326
327         return 0;
328
329 remove_zone:
330         int340x_thermal_zone_remove(proc_priv->int340x_zone);
331 remove_group:
332         sysfs_remove_group(&proc_priv->dev->kobj,
333                            &power_limit_attribute_group);
334
335         return ret;
336 }
337
338 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
339 {
340         acpi_remove_notify_handler(proc_priv->adev->handle,
341                                    ACPI_DEVICE_NOTIFY, proc_thermal_notify);
342         int340x_thermal_zone_remove(proc_priv->int340x_zone);
343         sysfs_remove_group(&proc_priv->dev->kobj,
344                            &power_limit_attribute_group);
345 }
346
347 static int int3401_add(struct platform_device *pdev)
348 {
349         struct proc_thermal_device *proc_priv;
350         int ret;
351
352         if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
353                 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
354                 return -ENODEV;
355         }
356
357         ret = proc_thermal_add(&pdev->dev, &proc_priv);
358         if (ret)
359                 return ret;
360
361         platform_set_drvdata(pdev, proc_priv);
362         proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
363
364         return 0;
365 }
366
367 static int int3401_remove(struct platform_device *pdev)
368 {
369         proc_thermal_remove(platform_get_drvdata(pdev));
370
371         return 0;
372 }
373
374 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
375 {
376         struct proc_thermal_device *proc_priv;
377         struct pci_dev *pdev = devid;
378
379         proc_priv = pci_get_drvdata(pdev);
380
381         intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
382
383         return IRQ_HANDLED;
384 }
385
386 static int  proc_thermal_pci_probe(struct pci_dev *pdev,
387                                    const struct pci_device_id *unused)
388 {
389         struct proc_thermal_device *proc_priv;
390         int ret;
391
392         if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
393                 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
394                 return -ENODEV;
395         }
396
397         ret = pci_enable_device(pdev);
398         if (ret < 0) {
399                 dev_err(&pdev->dev, "error: could not enable device\n");
400                 return ret;
401         }
402
403         ret = proc_thermal_add(&pdev->dev, &proc_priv);
404         if (ret) {
405                 pci_disable_device(pdev);
406                 return ret;
407         }
408
409         pci_set_drvdata(pdev, proc_priv);
410         proc_thermal_emum_mode = PROC_THERMAL_PCI;
411
412         if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
413                 /*
414                  * Enumerate additional DTS sensors available via IOSF.
415                  * But we are not treating as a failure condition, if
416                  * there are no aux DTSs enabled or fails. This driver
417                  * already exposes sensors, which can be accessed via
418                  * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
419                  */
420                 proc_priv->soc_dts = intel_soc_dts_iosf_init(
421                                         INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
422
423                 if (proc_priv->soc_dts && pdev->irq) {
424                         ret = pci_enable_msi(pdev);
425                         if (!ret) {
426                                 ret = request_threaded_irq(pdev->irq, NULL,
427                                                 proc_thermal_pci_msi_irq,
428                                                 IRQF_ONESHOT, "proc_thermal",
429                                                 pdev);
430                                 if (ret) {
431                                         intel_soc_dts_iosf_exit(
432                                                         proc_priv->soc_dts);
433                                         pci_disable_msi(pdev);
434                                         proc_priv->soc_dts = NULL;
435                                 }
436                         }
437                 } else
438                         dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
439         }
440
441         return 0;
442 }
443
444 static void  proc_thermal_pci_remove(struct pci_dev *pdev)
445 {
446         struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
447
448         if (proc_priv->soc_dts) {
449                 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
450                 if (pdev->irq) {
451                         free_irq(pdev->irq, pdev);
452                         pci_disable_msi(pdev);
453                 }
454         }
455         proc_thermal_remove(proc_priv);
456         pci_disable_device(pdev);
457 }
458
459 static const struct pci_device_id proc_thermal_pci_ids[] = {
460         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
461         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
462         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)},
463         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
464         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
465         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
466         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
467         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
468         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)},
469         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)},
470         { 0, },
471 };
472
473 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
474
475 static struct pci_driver proc_thermal_pci_driver = {
476         .name           = "proc_thermal",
477         .probe          = proc_thermal_pci_probe,
478         .remove         = proc_thermal_pci_remove,
479         .id_table       = proc_thermal_pci_ids,
480 };
481
482 static const struct acpi_device_id int3401_device_ids[] = {
483         {"INT3401", 0},
484         {"", 0},
485 };
486 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
487
488 static struct platform_driver int3401_driver = {
489         .probe = int3401_add,
490         .remove = int3401_remove,
491         .driver = {
492                 .name = "int3401 thermal",
493                 .acpi_match_table = int3401_device_ids,
494         },
495 };
496
497 static int __init proc_thermal_init(void)
498 {
499         int ret;
500
501         ret = platform_driver_register(&int3401_driver);
502         if (ret)
503                 return ret;
504
505         ret = pci_register_driver(&proc_thermal_pci_driver);
506
507         return ret;
508 }
509
510 static void __exit proc_thermal_exit(void)
511 {
512         platform_driver_unregister(&int3401_driver);
513         pci_unregister_driver(&proc_thermal_pci_driver);
514 }
515
516 module_init(proc_thermal_init);
517 module_exit(proc_thermal_exit);
518
519 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
520 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
521 MODULE_LICENSE("GPL v2");