1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * processor_thermal_device.c
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/pci.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"
18 
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
22 
23 /* Skylake thermal reporting device */
24 #define PCI_DEVICE_ID_PROC_SKL_THERMAL	0x1903
25 
26 /* CannonLake thermal reporting device */
27 #define PCI_DEVICE_ID_PROC_CNL_THERMAL	0x5a03
28 #define PCI_DEVICE_ID_PROC_CFL_THERMAL	0x3E83
29 
30 /* Braswell thermal reporting device */
31 #define PCI_DEVICE_ID_PROC_BSW_THERMAL	0x22DC
32 
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
38 
39 /* GeminiLake thermal reporting device */
40 #define PCI_DEVICE_ID_PROC_GLK_THERMAL	0x318C
41 
42 /* IceLake thermal reporting device */
43 #define PCI_DEVICE_ID_PROC_ICL_THERMAL	0x8a03
44 
45 /* JasperLake thermal reporting device */
46 #define PCI_DEVICE_ID_PROC_JSL_THERMAL	0x4503
47 
48 /* TigerLake thermal reporting device */
49 #define PCI_DEVICE_ID_PROC_TGL_THERMAL	0x9A03
50 
51 #define DRV_NAME "proc_thermal"
52 
53 struct power_config {
54 	u32	index;
55 	u32	min_uw;
56 	u32	max_uw;
57 	u32	tmin_us;
58 	u32	tmax_us;
59 	u32	step_uw;
60 };
61 
62 struct proc_thermal_device {
63 	struct device *dev;
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;
69 };
70 
71 enum proc_thermal_emum_mode_type {
72 	PROC_THERMAL_NONE,
73 	PROC_THERMAL_PCI,
74 	PROC_THERMAL_PLATFORM_DEV
75 };
76 
77 struct rapl_mmio_regs {
78 	u64 reg_unit;
79 	u64 regs[RAPL_DOMAIN_MAX][RAPL_DOMAIN_REG_MAX];
80 	int limits[RAPL_DOMAIN_MAX];
81 };
82 
83 /*
84  * We can have only one type of enumeration, PCI or Platform,
85  * not both. So we don't need instance specific data.
86  */
87 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
88 							PROC_THERMAL_NONE;
89 
90 #define POWER_LIMIT_SHOW(index, suffix) \
91 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
92 					struct device_attribute *attr, \
93 					char *buf) \
94 { \
95 	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
96 	\
97 	if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \
98 		dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \
99 		return 0; \
100 	} \
101 	\
102 	return sprintf(buf, "%lu\n",\
103 	(unsigned long)proc_dev->power_limits[index].suffix * 1000); \
104 }
105 
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)
111 
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)
117 
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);
123 
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);
129 
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,
141 	NULL
142 };
143 
144 static const struct attribute_group power_limit_attribute_group = {
145 	.attrs = power_limit_attrs,
146 	.name = "power_limits"
147 };
148 
149 static ssize_t tcc_offset_degree_celsius_show(struct device *dev,
150 			       struct device_attribute *attr, char *buf)
151 {
152 	u64 val;
153 	int err;
154 
155 	err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
156 	if (err)
157 		return err;
158 
159 	val = (val >> 24) & 0xff;
160 	return sprintf(buf, "%d\n", (int)val);
161 }
162 
163 static int tcc_offset_update(int tcc)
164 {
165 	u64 val;
166 	int err;
167 
168 	if (!tcc)
169 		return -EINVAL;
170 
171 	err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
172 	if (err)
173 		return err;
174 
175 	val &= ~GENMASK_ULL(31, 24);
176 	val |= (tcc & 0xff) << 24;
177 
178 	err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
179 	if (err)
180 		return err;
181 
182 	return 0;
183 }
184 
185 static int tcc_offset_save;
186 
187 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
188 				struct device_attribute *attr, const char *buf,
189 				size_t count)
190 {
191 	u64 val;
192 	int tcc, err;
193 
194 	err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
195 	if (err)
196 		return err;
197 
198 	if (!(val & BIT(30)))
199 		return -EACCES;
200 
201 	if (kstrtoint(buf, 0, &tcc))
202 		return -EINVAL;
203 
204 	err = tcc_offset_update(tcc);
205 	if (err)
206 		return err;
207 
208 	tcc_offset_save = tcc;
209 
210 	return count;
211 }
212 
213 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
214 
215 static int stored_tjmax; /* since it is fixed, we can have local storage */
216 
217 static int get_tjmax(void)
218 {
219 	u32 eax, edx;
220 	u32 val;
221 	int err;
222 
223 	err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
224 	if (err)
225 		return err;
226 
227 	val = (eax >> 16) & 0xff;
228 	if (val)
229 		return val;
230 
231 	return -EINVAL;
232 }
233 
234 static int read_temp_msr(int *temp)
235 {
236 	int cpu;
237 	u32 eax, edx;
238 	int err;
239 	unsigned long curr_temp_off = 0;
240 
241 	*temp = 0;
242 
243 	for_each_online_cpu(cpu) {
244 		err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
245 					&edx);
246 		if (err)
247 			goto err_ret;
248 		else {
249 			if (eax & 0x80000000) {
250 				curr_temp_off = (eax >> 16) & 0x7f;
251 				if (!*temp || curr_temp_off < *temp)
252 					*temp = curr_temp_off;
253 			} else {
254 				err = -EINVAL;
255 				goto err_ret;
256 			}
257 		}
258 	}
259 
260 	return 0;
261 err_ret:
262 	return err;
263 }
264 
265 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
266 					 int *temp)
267 {
268 	int ret;
269 
270 	ret = read_temp_msr(temp);
271 	if (!ret)
272 		*temp = (stored_tjmax - *temp) * 1000;
273 
274 	return ret;
275 }
276 
277 static struct thermal_zone_device_ops proc_thermal_local_ops = {
278 	.get_temp       = proc_thermal_get_zone_temp,
279 };
280 
281 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
282 {
283 	int i;
284 	acpi_status status;
285 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
286 	union acpi_object *elements, *ppcc;
287 	union acpi_object *p;
288 	int ret = 0;
289 
290 	status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
291 				      NULL, &buf);
292 	if (ACPI_FAILURE(status))
293 		return -ENODEV;
294 
295 	p = buf.pointer;
296 	if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
297 		dev_err(proc_priv->dev, "Invalid PPCC data\n");
298 		ret = -EFAULT;
299 		goto free_buffer;
300 	}
301 
302 	if (!p->package.count) {
303 		dev_err(proc_priv->dev, "Invalid PPCC package size\n");
304 		ret = -EFAULT;
305 		goto free_buffer;
306 	}
307 
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) {
312 			ret = -EFAULT;
313 			goto free_buffer;
314 		}
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;
322 	}
323 
324 free_buffer:
325 	kfree(buf.pointer);
326 
327 	return ret;
328 }
329 
330 #define PROC_POWER_CAPABILITY_CHANGED	0x83
331 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
332 {
333 	struct proc_thermal_device *proc_priv = data;
334 
335 	if (!proc_priv)
336 		return;
337 
338 	switch (event) {
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);
343 		break;
344 	default:
345 		dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
346 		break;
347 	}
348 }
349 
350 
351 static int proc_thermal_add(struct device *dev,
352 			    struct proc_thermal_device **priv)
353 {
354 	struct proc_thermal_device *proc_priv;
355 	struct acpi_device *adev;
356 	acpi_status status;
357 	unsigned long long tmp;
358 	struct thermal_zone_device_ops *ops = NULL;
359 	int ret;
360 
361 	adev = ACPI_COMPANION(dev);
362 	if (!adev)
363 		return -ENODEV;
364 
365 	proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
366 	if (!proc_priv)
367 		return -ENOMEM;
368 
369 	proc_priv->dev = dev;
370 	proc_priv->adev = adev;
371 	*priv = proc_priv;
372 
373 	ret = proc_thermal_read_ppcc(proc_priv);
374 	if (ret)
375 		return ret;
376 
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;
383 	}
384 
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);
388 	} else
389 		ret = 0;
390 
391 	ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
392 					  proc_thermal_notify,
393 					  (void *)proc_priv);
394 	if (ret)
395 		goto remove_zone;
396 
397 	return 0;
398 
399 remove_zone:
400 	int340x_thermal_zone_remove(proc_priv->int340x_zone);
401 
402 	return ret;
403 }
404 
405 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
406 {
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);
413 }
414 
415 static int int3401_add(struct platform_device *pdev)
416 {
417 	struct proc_thermal_device *proc_priv;
418 	int ret;
419 
420 	if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
421 		dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
422 		return -ENODEV;
423 	}
424 
425 	ret = proc_thermal_add(&pdev->dev, &proc_priv);
426 	if (ret)
427 		return ret;
428 
429 	platform_set_drvdata(pdev, proc_priv);
430 	proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
431 
432 	dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
433 
434 	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
435 	if (ret)
436 		return ret;
437 
438 	ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
439 	if (ret)
440 		sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
441 
442 	return ret;
443 }
444 
445 static int int3401_remove(struct platform_device *pdev)
446 {
447 	proc_thermal_remove(platform_get_drvdata(pdev));
448 
449 	return 0;
450 }
451 
452 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
453 {
454 	struct proc_thermal_device *proc_priv;
455 	struct pci_dev *pdev = devid;
456 
457 	proc_priv = pci_get_drvdata(pdev);
458 
459 	intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
460 
461 	return IRQ_HANDLED;
462 }
463 
464 #ifdef CONFIG_PROC_THERMAL_MMIO_RAPL
465 
466 #define MCHBAR 0
467 
468 /* RAPL Support via MMIO interface */
469 static struct rapl_if_priv rapl_mmio_priv;
470 
471 static int rapl_mmio_cpu_online(unsigned int cpu)
472 {
473 	struct rapl_package *rp;
474 
475 	/* mmio rapl supports package 0 only for now */
476 	if (topology_physical_package_id(cpu))
477 		return 0;
478 
479 	rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
480 	if (!rp) {
481 		rp = rapl_add_package(cpu, &rapl_mmio_priv);
482 		if (IS_ERR(rp))
483 			return PTR_ERR(rp);
484 	}
485 	cpumask_set_cpu(cpu, &rp->cpumask);
486 	return 0;
487 }
488 
489 static int rapl_mmio_cpu_down_prep(unsigned int cpu)
490 {
491 	struct rapl_package *rp;
492 	int lead_cpu;
493 
494 	rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
495 	if (!rp)
496 		return 0;
497 
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;
504 	return 0;
505 }
506 
507 static int rapl_mmio_read_raw(int cpu, struct reg_action *ra)
508 {
509 	if (!ra->reg)
510 		return -EINVAL;
511 
512 	ra->value = readq((void __iomem *)ra->reg);
513 	ra->value &= ra->mask;
514 	return 0;
515 }
516 
517 static int rapl_mmio_write_raw(int cpu, struct reg_action *ra)
518 {
519 	u64 val;
520 
521 	if (!ra->reg)
522 		return -EINVAL;
523 
524 	val = readq((void __iomem *)ra->reg);
525 	val &= ~ra->mask;
526 	val |= ra->value;
527 	writeq(val, (void __iomem *)ra->reg);
528 	return 0;
529 }
530 
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)
534 {
535 	enum rapl_domain_reg_id reg;
536 	enum rapl_domain_type domain;
537 	int ret;
538 
539 	if (!rapl_regs)
540 		return 0;
541 
542 	ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
543 	if (ret) {
544 		dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
545 		return -ENOMEM;
546 	}
547 
548 	proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
549 
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];
557 	}
558 	rapl_mmio_priv.reg_unit = (u64)proc_priv->mmio_base + rapl_regs->reg_unit;
559 
560 	rapl_mmio_priv.read_raw = rapl_mmio_read_raw;
561 	rapl_mmio_priv.write_raw = rapl_mmio_write_raw;
562 
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);
567 	}
568 
569 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
570 				rapl_mmio_cpu_online, rapl_mmio_cpu_down_prep);
571 	if (ret < 0) {
572 		powercap_unregister_control_type(rapl_mmio_priv.control_type);
573 		rapl_mmio_priv.control_type = NULL;
574 		return ret;
575 	}
576 	rapl_mmio_priv.pcap_rapl_online = ret;
577 
578 	return 0;
579 }
580 
581 static void proc_thermal_rapl_remove(void)
582 {
583 	if (IS_ERR_OR_NULL(rapl_mmio_priv.control_type))
584 		return;
585 
586 	cpuhp_remove_state(rapl_mmio_priv.pcap_rapl_online);
587 	powercap_unregister_control_type(rapl_mmio_priv.control_type);
588 }
589 
590 static const struct rapl_mmio_regs rapl_mmio_hsw = {
591 	.reg_unit = 0x5938,
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,
596 };
597 
598 #else
599 
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)
603 {
604 	return 0;
605 }
606 static void proc_thermal_rapl_remove(void) {}
607 static const struct rapl_mmio_regs rapl_mmio_hsw;
608 
609 #endif /* CONFIG_MMIO_RAPL */
610 
611 static int  proc_thermal_pci_probe(struct pci_dev *pdev,
612 				   const struct pci_device_id *id)
613 {
614 	struct proc_thermal_device *proc_priv;
615 	int ret;
616 
617 	if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
618 		dev_err(&pdev->dev, "error: enumerated as platform dev\n");
619 		return -ENODEV;
620 	}
621 
622 	ret = pcim_enable_device(pdev);
623 	if (ret < 0) {
624 		dev_err(&pdev->dev, "error: could not enable device\n");
625 		return ret;
626 	}
627 
628 	ret = proc_thermal_add(&pdev->dev, &proc_priv);
629 	if (ret)
630 		return ret;
631 
632 	ret = proc_thermal_rapl_add(pdev, proc_priv,
633 				(struct rapl_mmio_regs *)id->driver_data);
634 	if (ret) {
635 		dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
636 		proc_thermal_remove(proc_priv);
637 		return ret;
638 	}
639 
640 	pci_set_drvdata(pdev, proc_priv);
641 	proc_thermal_emum_mode = PROC_THERMAL_PCI;
642 
643 	if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
644 		/*
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.
650 		 */
651 		proc_priv->soc_dts = intel_soc_dts_iosf_init(
652 					INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
653 
654 		if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
655 			ret = pci_enable_msi(pdev);
656 			if (!ret) {
657 				ret = request_threaded_irq(pdev->irq, NULL,
658 						proc_thermal_pci_msi_irq,
659 						IRQF_ONESHOT, "proc_thermal",
660 						pdev);
661 				if (ret) {
662 					intel_soc_dts_iosf_exit(
663 							proc_priv->soc_dts);
664 					pci_disable_msi(pdev);
665 					proc_priv->soc_dts = NULL;
666 				}
667 			}
668 		} else
669 			dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
670 	}
671 
672 	dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
673 
674 	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
675 	if (ret)
676 		return ret;
677 
678 	ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
679 	if (ret)
680 		sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
681 
682 	return ret;
683 }
684 
685 static void  proc_thermal_pci_remove(struct pci_dev *pdev)
686 {
687 	struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
688 
689 	if (proc_priv->soc_dts) {
690 		intel_soc_dts_iosf_exit(proc_priv->soc_dts);
691 		if (pdev->irq) {
692 			free_irq(pdev->irq, pdev);
693 			pci_disable_msi(pdev);
694 		}
695 	}
696 	proc_thermal_rapl_remove();
697 	proc_thermal_remove(proc_priv);
698 }
699 
700 #ifdef CONFIG_PM_SLEEP
701 static int proc_thermal_resume(struct device *dev)
702 {
703 	struct proc_thermal_device *proc_dev;
704 
705 	proc_dev = dev_get_drvdata(dev);
706 	proc_thermal_read_ppcc(proc_dev);
707 
708 	tcc_offset_update(tcc_offset_save);
709 
710 	return 0;
711 }
712 #else
713 #define proc_thermal_resume NULL
714 #endif
715 
716 static SIMPLE_DEV_PM_OPS(proc_thermal_pm, NULL, proc_thermal_resume);
717 
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, },
736 	{ 0, },
737 };
738 
739 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
740 
741 static struct pci_driver proc_thermal_pci_driver = {
742 	.name		= DRV_NAME,
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,
747 };
748 
749 static const struct acpi_device_id int3401_device_ids[] = {
750 	{"INT3401", 0},
751 	{"", 0},
752 };
753 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
754 
755 static struct platform_driver int3401_driver = {
756 	.probe = int3401_add,
757 	.remove = int3401_remove,
758 	.driver = {
759 		.name = "int3401 thermal",
760 		.acpi_match_table = int3401_device_ids,
761 		.pm = &proc_thermal_pm,
762 	},
763 };
764 
765 static int __init proc_thermal_init(void)
766 {
767 	int ret;
768 
769 	ret = platform_driver_register(&int3401_driver);
770 	if (ret)
771 		return ret;
772 
773 	ret = pci_register_driver(&proc_thermal_pci_driver);
774 
775 	return ret;
776 }
777 
778 static void __exit proc_thermal_exit(void)
779 {
780 	platform_driver_unregister(&int3401_driver);
781 	pci_unregister_driver(&proc_thermal_pci_driver);
782 }
783 
784 module_init(proc_thermal_init);
785 module_exit(proc_thermal_exit);
786 
787 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
788 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
789 MODULE_LICENSE("GPL v2");
790