1 // SPDX-License-Identifier: GPL-2.0-only
3 * processor_thermal_device.c
4 * Copyright (c) 2014, Intel Corporation.
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/platform_device.h>
12 #include <linux/acpi.h>
13 #include <linux/thermal.h>
14 #include <linux/cpuhotplug.h>
15 #include <linux/intel_rapl.h>
16 #include "int340x_thermal_zone.h"
17 #include "../intel_soc_dts_iosf.h"
19 /* Broadwell-U/HSB thermal reporting device */
20 #define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603
21 #define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03
23 /* Skylake thermal reporting device */
24 #define PCI_DEVICE_ID_PROC_SKL_THERMAL 0x1903
26 /* CannonLake thermal reporting device */
27 #define PCI_DEVICE_ID_PROC_CNL_THERMAL 0x5a03
28 #define PCI_DEVICE_ID_PROC_CFL_THERMAL 0x3E83
30 /* Braswell thermal reporting device */
31 #define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC
33 /* Broxton thermal reporting device */
34 #define PCI_DEVICE_ID_PROC_BXT0_THERMAL 0x0A8C
35 #define PCI_DEVICE_ID_PROC_BXT1_THERMAL 0x1A8C
36 #define PCI_DEVICE_ID_PROC_BXTX_THERMAL 0x4A8C
37 #define PCI_DEVICE_ID_PROC_BXTP_THERMAL 0x5A8C
39 /* GeminiLake thermal reporting device */
40 #define PCI_DEVICE_ID_PROC_GLK_THERMAL 0x318C
42 /* IceLake thermal reporting device */
43 #define PCI_DEVICE_ID_PROC_ICL_THERMAL 0x8a03
45 /* JasperLake thermal reporting device */
46 #define PCI_DEVICE_ID_PROC_JSL_THERMAL 0x4503
48 #define DRV_NAME "proc_thermal"
59 struct proc_thermal_device {
61 struct acpi_device *adev;
62 struct power_config power_limits[2];
63 struct int34x_thermal_zone *int340x_zone;
64 struct intel_soc_dts_sensors *soc_dts;
65 void __iomem *mmio_base;
68 enum proc_thermal_emum_mode_type {
71 PROC_THERMAL_PLATFORM_DEV
74 struct rapl_mmio_regs {
76 u64 regs[RAPL_DOMAIN_MAX][RAPL_DOMAIN_REG_MAX];
77 int limits[RAPL_DOMAIN_MAX];
81 * We can have only one type of enumeration, PCI or Platform,
82 * not both. So we don't need instance specific data.
84 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
87 #define POWER_LIMIT_SHOW(index, suffix) \
88 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
89 struct device_attribute *attr, \
92 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
94 if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \
95 dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \
99 return sprintf(buf, "%lu\n",\
100 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
103 POWER_LIMIT_SHOW(0, min_uw)
104 POWER_LIMIT_SHOW(0, max_uw)
105 POWER_LIMIT_SHOW(0, step_uw)
106 POWER_LIMIT_SHOW(0, tmin_us)
107 POWER_LIMIT_SHOW(0, tmax_us)
109 POWER_LIMIT_SHOW(1, min_uw)
110 POWER_LIMIT_SHOW(1, max_uw)
111 POWER_LIMIT_SHOW(1, step_uw)
112 POWER_LIMIT_SHOW(1, tmin_us)
113 POWER_LIMIT_SHOW(1, tmax_us)
115 static DEVICE_ATTR_RO(power_limit_0_min_uw);
116 static DEVICE_ATTR_RO(power_limit_0_max_uw);
117 static DEVICE_ATTR_RO(power_limit_0_step_uw);
118 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
119 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
121 static DEVICE_ATTR_RO(power_limit_1_min_uw);
122 static DEVICE_ATTR_RO(power_limit_1_max_uw);
123 static DEVICE_ATTR_RO(power_limit_1_step_uw);
124 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
125 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
127 static struct attribute *power_limit_attrs[] = {
128 &dev_attr_power_limit_0_min_uw.attr,
129 &dev_attr_power_limit_1_min_uw.attr,
130 &dev_attr_power_limit_0_max_uw.attr,
131 &dev_attr_power_limit_1_max_uw.attr,
132 &dev_attr_power_limit_0_step_uw.attr,
133 &dev_attr_power_limit_1_step_uw.attr,
134 &dev_attr_power_limit_0_tmin_us.attr,
135 &dev_attr_power_limit_1_tmin_us.attr,
136 &dev_attr_power_limit_0_tmax_us.attr,
137 &dev_attr_power_limit_1_tmax_us.attr,
141 static const struct attribute_group power_limit_attribute_group = {
142 .attrs = power_limit_attrs,
143 .name = "power_limits"
146 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
152 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
156 val = (val >> 24) & 0xff;
157 return sprintf(buf, "%d\n", (int)val);
160 static int tcc_offset_update(int tcc)
168 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
172 val &= ~GENMASK_ULL(31, 24);
173 val |= (tcc & 0xff) << 24;
175 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
182 static int tcc_offset_save;
184 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
185 struct device_attribute *attr, const char *buf,
191 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
195 if (!(val & BIT(30)))
198 if (kstrtoint(buf, 0, &tcc))
201 err = tcc_offset_update(tcc);
205 tcc_offset_save = tcc;
210 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
212 static int stored_tjmax; /* since it is fixed, we can have local storage */
214 static int get_tjmax(void)
220 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
224 val = (eax >> 16) & 0xff;
231 static int read_temp_msr(int *temp)
236 unsigned long curr_temp_off = 0;
240 for_each_online_cpu(cpu) {
241 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
246 if (eax & 0x80000000) {
247 curr_temp_off = (eax >> 16) & 0x7f;
248 if (!*temp || curr_temp_off < *temp)
249 *temp = curr_temp_off;
262 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
267 ret = read_temp_msr(temp);
269 *temp = (stored_tjmax - *temp) * 1000;
274 static struct thermal_zone_device_ops proc_thermal_local_ops = {
275 .get_temp = proc_thermal_get_zone_temp,
278 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
282 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
283 union acpi_object *elements, *ppcc;
284 union acpi_object *p;
287 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
289 if (ACPI_FAILURE(status))
293 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
294 dev_err(proc_priv->dev, "Invalid PPCC data\n");
299 if (!p->package.count) {
300 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
305 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
306 elements = &(p->package.elements[i+1]);
307 if (elements->type != ACPI_TYPE_PACKAGE ||
308 elements->package.count != 6) {
312 ppcc = elements->package.elements;
313 proc_priv->power_limits[i].index = ppcc[0].integer.value;
314 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
315 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
316 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
317 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
318 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
327 #define PROC_POWER_CAPABILITY_CHANGED 0x83
328 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
330 struct proc_thermal_device *proc_priv = data;
336 case PROC_POWER_CAPABILITY_CHANGED:
337 proc_thermal_read_ppcc(proc_priv);
338 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
339 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
342 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
348 static int proc_thermal_add(struct device *dev,
349 struct proc_thermal_device **priv)
351 struct proc_thermal_device *proc_priv;
352 struct acpi_device *adev;
354 unsigned long long tmp;
355 struct thermal_zone_device_ops *ops = NULL;
358 adev = ACPI_COMPANION(dev);
362 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
366 proc_priv->dev = dev;
367 proc_priv->adev = adev;
370 ret = proc_thermal_read_ppcc(proc_priv);
374 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
375 if (ACPI_FAILURE(status)) {
376 /* there is no _TMP method, add local method */
377 stored_tjmax = get_tjmax();
378 if (stored_tjmax > 0)
379 ops = &proc_thermal_local_ops;
382 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
383 if (IS_ERR(proc_priv->int340x_zone)) {
384 return PTR_ERR(proc_priv->int340x_zone);
388 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
397 int340x_thermal_zone_remove(proc_priv->int340x_zone);
402 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
404 acpi_remove_notify_handler(proc_priv->adev->handle,
405 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
406 int340x_thermal_zone_remove(proc_priv->int340x_zone);
407 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
408 sysfs_remove_group(&proc_priv->dev->kobj,
409 &power_limit_attribute_group);
412 static int int3401_add(struct platform_device *pdev)
414 struct proc_thermal_device *proc_priv;
417 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
418 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
422 ret = proc_thermal_add(&pdev->dev, &proc_priv);
426 platform_set_drvdata(pdev, proc_priv);
427 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
429 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
431 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
435 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
437 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
442 static int int3401_remove(struct platform_device *pdev)
444 proc_thermal_remove(platform_get_drvdata(pdev));
449 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
451 struct proc_thermal_device *proc_priv;
452 struct pci_dev *pdev = devid;
454 proc_priv = pci_get_drvdata(pdev);
456 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
461 #ifdef CONFIG_PROC_THERMAL_MMIO_RAPL
465 /* RAPL Support via MMIO interface */
466 static struct rapl_if_priv rapl_mmio_priv;
468 static int rapl_mmio_cpu_online(unsigned int cpu)
470 struct rapl_package *rp;
472 /* mmio rapl supports package 0 only for now */
473 if (topology_physical_package_id(cpu))
476 rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
478 rp = rapl_add_package(cpu, &rapl_mmio_priv);
482 cpumask_set_cpu(cpu, &rp->cpumask);
486 static int rapl_mmio_cpu_down_prep(unsigned int cpu)
488 struct rapl_package *rp;
491 rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
495 cpumask_clear_cpu(cpu, &rp->cpumask);
496 lead_cpu = cpumask_first(&rp->cpumask);
497 if (lead_cpu >= nr_cpu_ids)
498 rapl_remove_package(rp);
499 else if (rp->lead_cpu == cpu)
500 rp->lead_cpu = lead_cpu;
504 static int rapl_mmio_read_raw(int cpu, struct reg_action *ra)
509 ra->value = readq((void __iomem *)ra->reg);
510 ra->value &= ra->mask;
514 static int rapl_mmio_write_raw(int cpu, struct reg_action *ra)
521 val = readq((void __iomem *)ra->reg);
524 writeq(val, (void __iomem *)ra->reg);
528 static int proc_thermal_rapl_add(struct pci_dev *pdev,
529 struct proc_thermal_device *proc_priv,
530 struct rapl_mmio_regs *rapl_regs)
532 enum rapl_domain_reg_id reg;
533 enum rapl_domain_type domain;
539 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
541 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
545 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
547 for (domain = RAPL_DOMAIN_PACKAGE; domain < RAPL_DOMAIN_MAX; domain++) {
548 for (reg = RAPL_DOMAIN_REG_LIMIT; reg < RAPL_DOMAIN_REG_MAX; reg++)
549 if (rapl_regs->regs[domain][reg])
550 rapl_mmio_priv.regs[domain][reg] =
551 (u64)proc_priv->mmio_base +
552 rapl_regs->regs[domain][reg];
553 rapl_mmio_priv.limits[domain] = rapl_regs->limits[domain];
555 rapl_mmio_priv.reg_unit = (u64)proc_priv->mmio_base + rapl_regs->reg_unit;
557 rapl_mmio_priv.read_raw = rapl_mmio_read_raw;
558 rapl_mmio_priv.write_raw = rapl_mmio_write_raw;
560 rapl_mmio_priv.control_type = powercap_register_control_type(NULL, "intel-rapl-mmio", NULL);
561 if (IS_ERR(rapl_mmio_priv.control_type)) {
562 pr_debug("failed to register powercap control_type.\n");
563 return PTR_ERR(rapl_mmio_priv.control_type);
566 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
567 rapl_mmio_cpu_online, rapl_mmio_cpu_down_prep);
569 powercap_unregister_control_type(rapl_mmio_priv.control_type);
570 rapl_mmio_priv.control_type = NULL;
573 rapl_mmio_priv.pcap_rapl_online = ret;
578 static void proc_thermal_rapl_remove(void)
580 if (IS_ERR_OR_NULL(rapl_mmio_priv.control_type))
583 cpuhp_remove_state(rapl_mmio_priv.pcap_rapl_online);
584 powercap_unregister_control_type(rapl_mmio_priv.control_type);
587 static const struct rapl_mmio_regs rapl_mmio_hsw = {
589 .regs[RAPL_DOMAIN_PACKAGE] = { 0x59a0, 0x593c, 0x58f0, 0, 0x5930},
590 .regs[RAPL_DOMAIN_DRAM] = { 0x58e0, 0x58e8, 0x58ec, 0, 0},
591 .limits[RAPL_DOMAIN_PACKAGE] = 2,
592 .limits[RAPL_DOMAIN_DRAM] = 2,
597 static int proc_thermal_rapl_add(struct pci_dev *pdev,
598 struct proc_thermal_device *proc_priv,
599 struct rapl_mmio_regs *rapl_regs)
603 static void proc_thermal_rapl_remove(void) {}
604 static const struct rapl_mmio_regs rapl_mmio_hsw;
606 #endif /* CONFIG_MMIO_RAPL */
608 static int proc_thermal_pci_probe(struct pci_dev *pdev,
609 const struct pci_device_id *id)
611 struct proc_thermal_device *proc_priv;
614 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
615 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
619 ret = pcim_enable_device(pdev);
621 dev_err(&pdev->dev, "error: could not enable device\n");
625 ret = proc_thermal_add(&pdev->dev, &proc_priv);
629 ret = proc_thermal_rapl_add(pdev, proc_priv,
630 (struct rapl_mmio_regs *)id->driver_data);
632 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
633 proc_thermal_remove(proc_priv);
637 pci_set_drvdata(pdev, proc_priv);
638 proc_thermal_emum_mode = PROC_THERMAL_PCI;
640 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
642 * Enumerate additional DTS sensors available via IOSF.
643 * But we are not treating as a failure condition, if
644 * there are no aux DTSs enabled or fails. This driver
645 * already exposes sensors, which can be accessed via
646 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
648 proc_priv->soc_dts = intel_soc_dts_iosf_init(
649 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
651 if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
652 ret = pci_enable_msi(pdev);
654 ret = request_threaded_irq(pdev->irq, NULL,
655 proc_thermal_pci_msi_irq,
656 IRQF_ONESHOT, "proc_thermal",
659 intel_soc_dts_iosf_exit(
661 pci_disable_msi(pdev);
662 proc_priv->soc_dts = NULL;
666 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
669 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
671 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
675 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
677 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
682 static void proc_thermal_pci_remove(struct pci_dev *pdev)
684 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
686 if (proc_priv->soc_dts) {
687 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
689 free_irq(pdev->irq, pdev);
690 pci_disable_msi(pdev);
693 proc_thermal_rapl_remove();
694 proc_thermal_remove(proc_priv);
697 #ifdef CONFIG_PM_SLEEP
698 static int proc_thermal_resume(struct device *dev)
700 struct proc_thermal_device *proc_dev;
702 proc_dev = dev_get_drvdata(dev);
703 proc_thermal_read_ppcc(proc_dev);
705 tcc_offset_update(tcc_offset_save);
710 #define proc_thermal_resume NULL
713 static SIMPLE_DEV_PM_OPS(proc_thermal_pm, NULL, proc_thermal_resume);
715 static const struct pci_device_id proc_thermal_pci_ids[] = {
716 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
717 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
718 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL),
719 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
720 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
721 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
722 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
723 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
724 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
725 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)},
726 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)},
727 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_GLK_THERMAL)},
728 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_ICL_THERMAL),
729 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
730 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_JSL_THERMAL)},
734 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
736 static struct pci_driver proc_thermal_pci_driver = {
738 .probe = proc_thermal_pci_probe,
739 .remove = proc_thermal_pci_remove,
740 .id_table = proc_thermal_pci_ids,
741 .driver.pm = &proc_thermal_pm,
744 static const struct acpi_device_id int3401_device_ids[] = {
748 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
750 static struct platform_driver int3401_driver = {
751 .probe = int3401_add,
752 .remove = int3401_remove,
754 .name = "int3401 thermal",
755 .acpi_match_table = int3401_device_ids,
756 .pm = &proc_thermal_pm,
760 static int __init proc_thermal_init(void)
764 ret = platform_driver_register(&int3401_driver);
768 ret = pci_register_driver(&proc_thermal_pci_driver);
773 static void __exit proc_thermal_exit(void)
775 platform_driver_unregister(&int3401_driver);
776 pci_unregister_driver(&proc_thermal_pci_driver);
779 module_init(proc_thermal_init);
780 module_exit(proc_thermal_exit);
782 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
783 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
784 MODULE_LICENSE("GPL v2");