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