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 0x4E03
48 /* TigerLake thermal reporting device */
49 #define PCI_DEVICE_ID_PROC_TGL_THERMAL 0x9A03
51 #define DRV_NAME "proc_thermal"
62 struct proc_thermal_device {
64 struct acpi_device *adev;
65 struct power_config power_limits[2];
66 struct int34x_thermal_zone *int340x_zone;
67 struct intel_soc_dts_sensors *soc_dts;
68 void __iomem *mmio_base;
71 enum proc_thermal_emum_mode_type {
74 PROC_THERMAL_PLATFORM_DEV
77 struct rapl_mmio_regs {
79 u64 regs[RAPL_DOMAIN_MAX][RAPL_DOMAIN_REG_MAX];
80 int limits[RAPL_DOMAIN_MAX];
84 * We can have only one type of enumeration, PCI or Platform,
85 * not both. So we don't need instance specific data.
87 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
90 #define POWER_LIMIT_SHOW(index, suffix) \
91 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
92 struct device_attribute *attr, \
95 struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
97 if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \
98 dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \
102 return sprintf(buf, "%lu\n",\
103 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
106 POWER_LIMIT_SHOW(0, min_uw)
107 POWER_LIMIT_SHOW(0, max_uw)
108 POWER_LIMIT_SHOW(0, step_uw)
109 POWER_LIMIT_SHOW(0, tmin_us)
110 POWER_LIMIT_SHOW(0, tmax_us)
112 POWER_LIMIT_SHOW(1, min_uw)
113 POWER_LIMIT_SHOW(1, max_uw)
114 POWER_LIMIT_SHOW(1, step_uw)
115 POWER_LIMIT_SHOW(1, tmin_us)
116 POWER_LIMIT_SHOW(1, tmax_us)
118 static DEVICE_ATTR_RO(power_limit_0_min_uw);
119 static DEVICE_ATTR_RO(power_limit_0_max_uw);
120 static DEVICE_ATTR_RO(power_limit_0_step_uw);
121 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
122 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
124 static DEVICE_ATTR_RO(power_limit_1_min_uw);
125 static DEVICE_ATTR_RO(power_limit_1_max_uw);
126 static DEVICE_ATTR_RO(power_limit_1_step_uw);
127 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
128 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
130 static struct attribute *power_limit_attrs[] = {
131 &dev_attr_power_limit_0_min_uw.attr,
132 &dev_attr_power_limit_1_min_uw.attr,
133 &dev_attr_power_limit_0_max_uw.attr,
134 &dev_attr_power_limit_1_max_uw.attr,
135 &dev_attr_power_limit_0_step_uw.attr,
136 &dev_attr_power_limit_1_step_uw.attr,
137 &dev_attr_power_limit_0_tmin_us.attr,
138 &dev_attr_power_limit_1_tmin_us.attr,
139 &dev_attr_power_limit_0_tmax_us.attr,
140 &dev_attr_power_limit_1_tmax_us.attr,
144 static const struct attribute_group power_limit_attribute_group = {
145 .attrs = power_limit_attrs,
146 .name = "power_limits"
149 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
150 struct device_attribute *attr, char *buf)
155 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
159 val = (val >> 24) & 0xff;
160 return sprintf(buf, "%d\n", (int)val);
163 static int tcc_offset_update(int tcc)
171 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
175 val &= ~GENMASK_ULL(31, 24);
176 val |= (tcc & 0xff) << 24;
178 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
185 static int tcc_offset_save;
187 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
188 struct device_attribute *attr, const char *buf,
194 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
198 if (!(val & BIT(30)))
201 if (kstrtoint(buf, 0, &tcc))
204 err = tcc_offset_update(tcc);
208 tcc_offset_save = tcc;
213 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
215 static int stored_tjmax; /* since it is fixed, we can have local storage */
217 static int get_tjmax(void)
223 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
227 val = (eax >> 16) & 0xff;
234 static int read_temp_msr(int *temp)
239 unsigned long curr_temp_off = 0;
243 for_each_online_cpu(cpu) {
244 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
249 if (eax & 0x80000000) {
250 curr_temp_off = (eax >> 16) & 0x7f;
251 if (!*temp || curr_temp_off < *temp)
252 *temp = curr_temp_off;
265 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
270 ret = read_temp_msr(temp);
272 *temp = (stored_tjmax - *temp) * 1000;
277 static struct thermal_zone_device_ops proc_thermal_local_ops = {
278 .get_temp = proc_thermal_get_zone_temp,
281 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
285 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
286 union acpi_object *elements, *ppcc;
287 union acpi_object *p;
290 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
292 if (ACPI_FAILURE(status))
296 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
297 dev_err(proc_priv->dev, "Invalid PPCC data\n");
302 if (!p->package.count) {
303 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
308 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
309 elements = &(p->package.elements[i+1]);
310 if (elements->type != ACPI_TYPE_PACKAGE ||
311 elements->package.count != 6) {
315 ppcc = elements->package.elements;
316 proc_priv->power_limits[i].index = ppcc[0].integer.value;
317 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
318 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
319 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
320 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
321 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
330 #define PROC_POWER_CAPABILITY_CHANGED 0x83
331 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
333 struct proc_thermal_device *proc_priv = data;
339 case PROC_POWER_CAPABILITY_CHANGED:
340 proc_thermal_read_ppcc(proc_priv);
341 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
342 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
345 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
351 static int proc_thermal_add(struct device *dev,
352 struct proc_thermal_device **priv)
354 struct proc_thermal_device *proc_priv;
355 struct acpi_device *adev;
357 unsigned long long tmp;
358 struct thermal_zone_device_ops *ops = NULL;
361 adev = ACPI_COMPANION(dev);
365 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
369 proc_priv->dev = dev;
370 proc_priv->adev = adev;
373 ret = proc_thermal_read_ppcc(proc_priv);
377 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
378 if (ACPI_FAILURE(status)) {
379 /* there is no _TMP method, add local method */
380 stored_tjmax = get_tjmax();
381 if (stored_tjmax > 0)
382 ops = &proc_thermal_local_ops;
385 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
386 if (IS_ERR(proc_priv->int340x_zone)) {
387 return PTR_ERR(proc_priv->int340x_zone);
391 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
400 int340x_thermal_zone_remove(proc_priv->int340x_zone);
405 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
407 acpi_remove_notify_handler(proc_priv->adev->handle,
408 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
409 int340x_thermal_zone_remove(proc_priv->int340x_zone);
410 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
411 sysfs_remove_group(&proc_priv->dev->kobj,
412 &power_limit_attribute_group);
415 static int int3401_add(struct platform_device *pdev)
417 struct proc_thermal_device *proc_priv;
420 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
421 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
425 ret = proc_thermal_add(&pdev->dev, &proc_priv);
429 platform_set_drvdata(pdev, proc_priv);
430 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
432 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
434 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
438 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
440 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
445 static int int3401_remove(struct platform_device *pdev)
447 proc_thermal_remove(platform_get_drvdata(pdev));
452 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
454 struct proc_thermal_device *proc_priv;
455 struct pci_dev *pdev = devid;
457 proc_priv = pci_get_drvdata(pdev);
459 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
464 #ifdef CONFIG_PROC_THERMAL_MMIO_RAPL
468 /* RAPL Support via MMIO interface */
469 static struct rapl_if_priv rapl_mmio_priv;
471 static int rapl_mmio_cpu_online(unsigned int cpu)
473 struct rapl_package *rp;
475 /* mmio rapl supports package 0 only for now */
476 if (topology_physical_package_id(cpu))
479 rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
481 rp = rapl_add_package(cpu, &rapl_mmio_priv);
485 cpumask_set_cpu(cpu, &rp->cpumask);
489 static int rapl_mmio_cpu_down_prep(unsigned int cpu)
491 struct rapl_package *rp;
494 rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
498 cpumask_clear_cpu(cpu, &rp->cpumask);
499 lead_cpu = cpumask_first(&rp->cpumask);
500 if (lead_cpu >= nr_cpu_ids)
501 rapl_remove_package(rp);
502 else if (rp->lead_cpu == cpu)
503 rp->lead_cpu = lead_cpu;
507 static int rapl_mmio_read_raw(int cpu, struct reg_action *ra)
512 ra->value = readq((void __iomem *)ra->reg);
513 ra->value &= ra->mask;
517 static int rapl_mmio_write_raw(int cpu, struct reg_action *ra)
524 val = readq((void __iomem *)ra->reg);
527 writeq(val, (void __iomem *)ra->reg);
531 static int proc_thermal_rapl_add(struct pci_dev *pdev,
532 struct proc_thermal_device *proc_priv,
533 struct rapl_mmio_regs *rapl_regs)
535 enum rapl_domain_reg_id reg;
536 enum rapl_domain_type domain;
542 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
544 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
548 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
550 for (domain = RAPL_DOMAIN_PACKAGE; domain < RAPL_DOMAIN_MAX; domain++) {
551 for (reg = RAPL_DOMAIN_REG_LIMIT; reg < RAPL_DOMAIN_REG_MAX; reg++)
552 if (rapl_regs->regs[domain][reg])
553 rapl_mmio_priv.regs[domain][reg] =
554 (u64)proc_priv->mmio_base +
555 rapl_regs->regs[domain][reg];
556 rapl_mmio_priv.limits[domain] = rapl_regs->limits[domain];
558 rapl_mmio_priv.reg_unit = (u64)proc_priv->mmio_base + rapl_regs->reg_unit;
560 rapl_mmio_priv.read_raw = rapl_mmio_read_raw;
561 rapl_mmio_priv.write_raw = rapl_mmio_write_raw;
563 rapl_mmio_priv.control_type = powercap_register_control_type(NULL, "intel-rapl-mmio", NULL);
564 if (IS_ERR(rapl_mmio_priv.control_type)) {
565 pr_debug("failed to register powercap control_type.\n");
566 return PTR_ERR(rapl_mmio_priv.control_type);
569 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
570 rapl_mmio_cpu_online, rapl_mmio_cpu_down_prep);
572 powercap_unregister_control_type(rapl_mmio_priv.control_type);
573 rapl_mmio_priv.control_type = NULL;
576 rapl_mmio_priv.pcap_rapl_online = ret;
581 static void proc_thermal_rapl_remove(void)
583 if (IS_ERR_OR_NULL(rapl_mmio_priv.control_type))
586 cpuhp_remove_state(rapl_mmio_priv.pcap_rapl_online);
587 powercap_unregister_control_type(rapl_mmio_priv.control_type);
590 static const struct rapl_mmio_regs rapl_mmio_hsw = {
592 .regs[RAPL_DOMAIN_PACKAGE] = { 0x59a0, 0x593c, 0x58f0, 0, 0x5930},
593 .regs[RAPL_DOMAIN_DRAM] = { 0x58e0, 0x58e8, 0x58ec, 0, 0},
594 .limits[RAPL_DOMAIN_PACKAGE] = 2,
595 .limits[RAPL_DOMAIN_DRAM] = 2,
600 static int proc_thermal_rapl_add(struct pci_dev *pdev,
601 struct proc_thermal_device *proc_priv,
602 struct rapl_mmio_regs *rapl_regs)
606 static void proc_thermal_rapl_remove(void) {}
607 static const struct rapl_mmio_regs rapl_mmio_hsw;
609 #endif /* CONFIG_MMIO_RAPL */
611 static int proc_thermal_pci_probe(struct pci_dev *pdev,
612 const struct pci_device_id *id)
614 struct proc_thermal_device *proc_priv;
617 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
618 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
622 ret = pcim_enable_device(pdev);
624 dev_err(&pdev->dev, "error: could not enable device\n");
628 ret = proc_thermal_add(&pdev->dev, &proc_priv);
632 ret = proc_thermal_rapl_add(pdev, proc_priv,
633 (struct rapl_mmio_regs *)id->driver_data);
635 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
636 proc_thermal_remove(proc_priv);
640 pci_set_drvdata(pdev, proc_priv);
641 proc_thermal_emum_mode = PROC_THERMAL_PCI;
643 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
645 * Enumerate additional DTS sensors available via IOSF.
646 * But we are not treating as a failure condition, if
647 * there are no aux DTSs enabled or fails. This driver
648 * already exposes sensors, which can be accessed via
649 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
651 proc_priv->soc_dts = intel_soc_dts_iosf_init(
652 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
654 if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
655 ret = pci_enable_msi(pdev);
657 ret = request_threaded_irq(pdev->irq, NULL,
658 proc_thermal_pci_msi_irq,
659 IRQF_ONESHOT, "proc_thermal",
662 intel_soc_dts_iosf_exit(
664 pci_disable_msi(pdev);
665 proc_priv->soc_dts = NULL;
669 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
672 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
674 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
678 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
680 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
685 static void proc_thermal_pci_remove(struct pci_dev *pdev)
687 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
689 if (proc_priv->soc_dts) {
690 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
692 free_irq(pdev->irq, pdev);
693 pci_disable_msi(pdev);
696 proc_thermal_rapl_remove();
697 proc_thermal_remove(proc_priv);
700 #ifdef CONFIG_PM_SLEEP
701 static int proc_thermal_resume(struct device *dev)
703 struct proc_thermal_device *proc_dev;
705 proc_dev = dev_get_drvdata(dev);
706 proc_thermal_read_ppcc(proc_dev);
708 tcc_offset_update(tcc_offset_save);
713 #define proc_thermal_resume NULL
716 static SIMPLE_DEV_PM_OPS(proc_thermal_pm, NULL, proc_thermal_resume);
718 static const struct pci_device_id proc_thermal_pci_ids[] = {
719 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
720 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
721 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL),
722 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
723 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
724 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
725 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
726 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
727 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
728 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)},
729 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)},
730 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_GLK_THERMAL)},
731 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_ICL_THERMAL),
732 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
733 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_JSL_THERMAL)},
734 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_TGL_THERMAL),
735 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
739 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
741 static struct pci_driver proc_thermal_pci_driver = {
743 .probe = proc_thermal_pci_probe,
744 .remove = proc_thermal_pci_remove,
745 .id_table = proc_thermal_pci_ids,
746 .driver.pm = &proc_thermal_pm,
749 static const struct acpi_device_id int3401_device_ids[] = {
753 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
755 static struct platform_driver int3401_driver = {
756 .probe = int3401_add,
757 .remove = int3401_remove,
759 .name = "int3401 thermal",
760 .acpi_match_table = int3401_device_ids,
761 .pm = &proc_thermal_pm,
765 static int __init proc_thermal_init(void)
769 ret = platform_driver_register(&int3401_driver);
773 ret = pci_register_driver(&proc_thermal_pci_driver);
778 static void __exit proc_thermal_exit(void)
780 platform_driver_unregister(&int3401_driver);
781 pci_unregister_driver(&proc_thermal_pci_driver);
784 module_init(proc_thermal_init);
785 module_exit(proc_thermal_exit);
788 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
789 MODULE_LICENSE("GPL v2");