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) & 0x3f;
160 return sprintf(buf, "%d\n", (int)val);
163 static int tcc_offset_update(unsigned int tcc)
171 err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
178 val &= ~GENMASK_ULL(29, 24);
179 val |= (tcc & 0x3f) << 24;
181 err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
188 static int tcc_offset_save = -1;
190 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
191 struct device_attribute *attr, const char *buf,
198 err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
202 if (!(val & BIT(30)))
205 if (kstrtouint(buf, 0, &tcc))
208 err = tcc_offset_update(tcc);
212 tcc_offset_save = tcc;
217 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
219 static int stored_tjmax; /* since it is fixed, we can have local storage */
221 static int get_tjmax(void)
227 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
231 val = (eax >> 16) & 0xff;
238 static int read_temp_msr(int *temp)
243 unsigned long curr_temp_off = 0;
247 for_each_online_cpu(cpu) {
248 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
253 if (eax & 0x80000000) {
254 curr_temp_off = (eax >> 16) & 0x7f;
255 if (!*temp || curr_temp_off < *temp)
256 *temp = curr_temp_off;
269 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
274 ret = read_temp_msr(temp);
276 *temp = (stored_tjmax - *temp) * 1000;
281 static struct thermal_zone_device_ops proc_thermal_local_ops = {
282 .get_temp = proc_thermal_get_zone_temp,
285 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
289 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
290 union acpi_object *elements, *ppcc;
291 union acpi_object *p;
294 status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
296 if (ACPI_FAILURE(status))
300 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
301 dev_err(proc_priv->dev, "Invalid PPCC data\n");
306 if (!p->package.count) {
307 dev_err(proc_priv->dev, "Invalid PPCC package size\n");
312 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
313 elements = &(p->package.elements[i+1]);
314 if (elements->type != ACPI_TYPE_PACKAGE ||
315 elements->package.count != 6) {
319 ppcc = elements->package.elements;
320 proc_priv->power_limits[i].index = ppcc[0].integer.value;
321 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
322 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
323 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
324 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
325 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
334 #define PROC_POWER_CAPABILITY_CHANGED 0x83
335 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
337 struct proc_thermal_device *proc_priv = data;
343 case PROC_POWER_CAPABILITY_CHANGED:
344 proc_thermal_read_ppcc(proc_priv);
345 int340x_thermal_zone_device_update(proc_priv->int340x_zone,
346 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
349 dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
355 static int proc_thermal_add(struct device *dev,
356 struct proc_thermal_device **priv)
358 struct proc_thermal_device *proc_priv;
359 struct acpi_device *adev;
361 unsigned long long tmp;
362 struct thermal_zone_device_ops *ops = NULL;
365 adev = ACPI_COMPANION(dev);
369 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
373 proc_priv->dev = dev;
374 proc_priv->adev = adev;
377 ret = proc_thermal_read_ppcc(proc_priv);
381 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
382 if (ACPI_FAILURE(status)) {
383 /* there is no _TMP method, add local method */
384 stored_tjmax = get_tjmax();
385 if (stored_tjmax > 0)
386 ops = &proc_thermal_local_ops;
389 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
390 if (IS_ERR(proc_priv->int340x_zone)) {
391 return PTR_ERR(proc_priv->int340x_zone);
395 ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
404 int340x_thermal_zone_remove(proc_priv->int340x_zone);
409 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
411 acpi_remove_notify_handler(proc_priv->adev->handle,
412 ACPI_DEVICE_NOTIFY, proc_thermal_notify);
413 int340x_thermal_zone_remove(proc_priv->int340x_zone);
414 sysfs_remove_file(&proc_priv->dev->kobj, &dev_attr_tcc_offset_degree_celsius.attr);
415 sysfs_remove_group(&proc_priv->dev->kobj,
416 &power_limit_attribute_group);
419 static int int3401_add(struct platform_device *pdev)
421 struct proc_thermal_device *proc_priv;
424 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
425 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
429 ret = proc_thermal_add(&pdev->dev, &proc_priv);
433 platform_set_drvdata(pdev, proc_priv);
434 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
436 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
438 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
442 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
444 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
449 static int int3401_remove(struct platform_device *pdev)
451 proc_thermal_remove(platform_get_drvdata(pdev));
456 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
458 struct proc_thermal_device *proc_priv;
459 struct pci_dev *pdev = devid;
461 proc_priv = pci_get_drvdata(pdev);
463 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
468 #ifdef CONFIG_PROC_THERMAL_MMIO_RAPL
472 /* RAPL Support via MMIO interface */
473 static struct rapl_if_priv rapl_mmio_priv;
475 static int rapl_mmio_cpu_online(unsigned int cpu)
477 struct rapl_package *rp;
479 /* mmio rapl supports package 0 only for now */
480 if (topology_physical_package_id(cpu))
483 rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
485 rp = rapl_add_package(cpu, &rapl_mmio_priv);
489 cpumask_set_cpu(cpu, &rp->cpumask);
493 static int rapl_mmio_cpu_down_prep(unsigned int cpu)
495 struct rapl_package *rp;
498 rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
502 cpumask_clear_cpu(cpu, &rp->cpumask);
503 lead_cpu = cpumask_first(&rp->cpumask);
504 if (lead_cpu >= nr_cpu_ids)
505 rapl_remove_package(rp);
506 else if (rp->lead_cpu == cpu)
507 rp->lead_cpu = lead_cpu;
511 static int rapl_mmio_read_raw(int cpu, struct reg_action *ra)
516 ra->value = readq((void __iomem *)ra->reg);
517 ra->value &= ra->mask;
521 static int rapl_mmio_write_raw(int cpu, struct reg_action *ra)
528 val = readq((void __iomem *)ra->reg);
531 writeq(val, (void __iomem *)ra->reg);
535 static int proc_thermal_rapl_add(struct pci_dev *pdev,
536 struct proc_thermal_device *proc_priv,
537 struct rapl_mmio_regs *rapl_regs)
539 enum rapl_domain_reg_id reg;
540 enum rapl_domain_type domain;
546 ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
548 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
552 proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
554 for (domain = RAPL_DOMAIN_PACKAGE; domain < RAPL_DOMAIN_MAX; domain++) {
555 for (reg = RAPL_DOMAIN_REG_LIMIT; reg < RAPL_DOMAIN_REG_MAX; reg++)
556 if (rapl_regs->regs[domain][reg])
557 rapl_mmio_priv.regs[domain][reg] =
558 (u64)proc_priv->mmio_base +
559 rapl_regs->regs[domain][reg];
560 rapl_mmio_priv.limits[domain] = rapl_regs->limits[domain];
562 rapl_mmio_priv.reg_unit = (u64)proc_priv->mmio_base + rapl_regs->reg_unit;
564 rapl_mmio_priv.read_raw = rapl_mmio_read_raw;
565 rapl_mmio_priv.write_raw = rapl_mmio_write_raw;
567 rapl_mmio_priv.control_type = powercap_register_control_type(NULL, "intel-rapl-mmio", NULL);
568 if (IS_ERR(rapl_mmio_priv.control_type)) {
569 pr_debug("failed to register powercap control_type.\n");
570 return PTR_ERR(rapl_mmio_priv.control_type);
573 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
574 rapl_mmio_cpu_online, rapl_mmio_cpu_down_prep);
576 powercap_unregister_control_type(rapl_mmio_priv.control_type);
577 rapl_mmio_priv.control_type = NULL;
580 rapl_mmio_priv.pcap_rapl_online = ret;
585 static void proc_thermal_rapl_remove(void)
587 if (IS_ERR_OR_NULL(rapl_mmio_priv.control_type))
590 cpuhp_remove_state(rapl_mmio_priv.pcap_rapl_online);
591 powercap_unregister_control_type(rapl_mmio_priv.control_type);
594 static const struct rapl_mmio_regs rapl_mmio_hsw = {
596 .regs[RAPL_DOMAIN_PACKAGE] = { 0x59a0, 0x593c, 0x58f0, 0, 0x5930},
597 .regs[RAPL_DOMAIN_DRAM] = { 0x58e0, 0x58e8, 0x58ec, 0, 0},
598 .limits[RAPL_DOMAIN_PACKAGE] = 2,
599 .limits[RAPL_DOMAIN_DRAM] = 2,
604 static int proc_thermal_rapl_add(struct pci_dev *pdev,
605 struct proc_thermal_device *proc_priv,
606 struct rapl_mmio_regs *rapl_regs)
610 static void proc_thermal_rapl_remove(void) {}
611 static const struct rapl_mmio_regs rapl_mmio_hsw;
613 #endif /* CONFIG_MMIO_RAPL */
615 static int proc_thermal_pci_probe(struct pci_dev *pdev,
616 const struct pci_device_id *id)
618 struct proc_thermal_device *proc_priv;
621 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
622 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
626 ret = pcim_enable_device(pdev);
628 dev_err(&pdev->dev, "error: could not enable device\n");
632 ret = proc_thermal_add(&pdev->dev, &proc_priv);
636 ret = proc_thermal_rapl_add(pdev, proc_priv,
637 (struct rapl_mmio_regs *)id->driver_data);
639 dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
640 proc_thermal_remove(proc_priv);
644 pci_set_drvdata(pdev, proc_priv);
645 proc_thermal_emum_mode = PROC_THERMAL_PCI;
647 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
649 * Enumerate additional DTS sensors available via IOSF.
650 * But we are not treating as a failure condition, if
651 * there are no aux DTSs enabled or fails. This driver
652 * already exposes sensors, which can be accessed via
653 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
655 proc_priv->soc_dts = intel_soc_dts_iosf_init(
656 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
658 if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
659 ret = pci_enable_msi(pdev);
661 ret = request_threaded_irq(pdev->irq, NULL,
662 proc_thermal_pci_msi_irq,
663 IRQF_ONESHOT, "proc_thermal",
666 intel_soc_dts_iosf_exit(
668 pci_disable_msi(pdev);
669 proc_priv->soc_dts = NULL;
673 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
676 dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
678 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
682 ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
684 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
689 static void proc_thermal_pci_remove(struct pci_dev *pdev)
691 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
693 if (proc_priv->soc_dts) {
694 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
696 free_irq(pdev->irq, pdev);
697 pci_disable_msi(pdev);
700 proc_thermal_rapl_remove();
701 proc_thermal_remove(proc_priv);
704 #ifdef CONFIG_PM_SLEEP
705 static int proc_thermal_resume(struct device *dev)
707 struct proc_thermal_device *proc_dev;
709 proc_dev = dev_get_drvdata(dev);
710 proc_thermal_read_ppcc(proc_dev);
712 if (tcc_offset_save >= 0)
713 tcc_offset_update(tcc_offset_save);
718 #define proc_thermal_resume NULL
721 static SIMPLE_DEV_PM_OPS(proc_thermal_pm, NULL, proc_thermal_resume);
723 static const struct pci_device_id proc_thermal_pci_ids[] = {
724 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
725 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
726 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL),
727 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
728 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
729 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
730 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
731 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
732 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
733 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)},
734 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)},
735 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_GLK_THERMAL)},
736 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_ICL_THERMAL),
737 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
738 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_JSL_THERMAL)},
739 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_TGL_THERMAL),
740 .driver_data = (kernel_ulong_t)&rapl_mmio_hsw, },
744 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
746 static struct pci_driver proc_thermal_pci_driver = {
748 .probe = proc_thermal_pci_probe,
749 .remove = proc_thermal_pci_remove,
750 .id_table = proc_thermal_pci_ids,
751 .driver.pm = &proc_thermal_pm,
754 static const struct acpi_device_id int3401_device_ids[] = {
758 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
760 static struct platform_driver int3401_driver = {
761 .probe = int3401_add,
762 .remove = int3401_remove,
764 .name = "int3401 thermal",
765 .acpi_match_table = int3401_device_ids,
766 .pm = &proc_thermal_pm,
770 static int __init proc_thermal_init(void)
774 ret = platform_driver_register(&int3401_driver);
778 ret = pci_register_driver(&proc_thermal_pci_driver);
783 static void __exit proc_thermal_exit(void)
785 platform_driver_unregister(&int3401_driver);
786 pci_unregister_driver(&proc_thermal_pci_driver);
789 module_init(proc_thermal_init);
790 module_exit(proc_thermal_exit);
792 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
793 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
794 MODULE_LICENSE("GPL v2");