• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	0x4E03
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 
tcc_offset_degree_celsius_show(struct device * dev,struct device_attribute * attr,char * buf)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) & 0x3f;
160 	return sprintf(buf, "%d\n", (int)val);
161 }
162 
tcc_offset_update(unsigned int tcc)163 static int tcc_offset_update(unsigned int tcc)
164 {
165 	u64 val;
166 	int err;
167 
168 	if (tcc > 63)
169 		return -EINVAL;
170 
171 	err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
172 	if (err)
173 		return err;
174 
175 	if (val & BIT(31))
176 		return -EPERM;
177 
178 	val &= ~GENMASK_ULL(29, 24);
179 	val |= (tcc & 0x3f) << 24;
180 
181 	err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
182 	if (err)
183 		return err;
184 
185 	return 0;
186 }
187 
188 static int tcc_offset_save = -1;
189 
tcc_offset_degree_celsius_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)190 static ssize_t tcc_offset_degree_celsius_store(struct device *dev,
191 				struct device_attribute *attr, const char *buf,
192 				size_t count)
193 {
194 	unsigned int tcc;
195 	u64 val;
196 	int err;
197 
198 	err = rdmsrl_safe(MSR_PLATFORM_INFO, &val);
199 	if (err)
200 		return err;
201 
202 	if (!(val & BIT(30)))
203 		return -EACCES;
204 
205 	if (kstrtouint(buf, 0, &tcc))
206 		return -EINVAL;
207 
208 	err = tcc_offset_update(tcc);
209 	if (err)
210 		return err;
211 
212 	tcc_offset_save = tcc;
213 
214 	return count;
215 }
216 
217 static DEVICE_ATTR_RW(tcc_offset_degree_celsius);
218 
219 static int stored_tjmax; /* since it is fixed, we can have local storage */
220 
get_tjmax(void)221 static int get_tjmax(void)
222 {
223 	u32 eax, edx;
224 	u32 val;
225 	int err;
226 
227 	err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
228 	if (err)
229 		return err;
230 
231 	val = (eax >> 16) & 0xff;
232 	if (val)
233 		return val;
234 
235 	return -EINVAL;
236 }
237 
read_temp_msr(int * temp)238 static int read_temp_msr(int *temp)
239 {
240 	int cpu;
241 	u32 eax, edx;
242 	int err;
243 	unsigned long curr_temp_off = 0;
244 
245 	*temp = 0;
246 
247 	for_each_online_cpu(cpu) {
248 		err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
249 					&edx);
250 		if (err)
251 			goto err_ret;
252 		else {
253 			if (eax & 0x80000000) {
254 				curr_temp_off = (eax >> 16) & 0x7f;
255 				if (!*temp || curr_temp_off < *temp)
256 					*temp = curr_temp_off;
257 			} else {
258 				err = -EINVAL;
259 				goto err_ret;
260 			}
261 		}
262 	}
263 
264 	return 0;
265 err_ret:
266 	return err;
267 }
268 
proc_thermal_get_zone_temp(struct thermal_zone_device * zone,int * temp)269 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
270 					 int *temp)
271 {
272 	int ret;
273 
274 	ret = read_temp_msr(temp);
275 	if (!ret)
276 		*temp = (stored_tjmax - *temp) * 1000;
277 
278 	return ret;
279 }
280 
281 static struct thermal_zone_device_ops proc_thermal_local_ops = {
282 	.get_temp       = proc_thermal_get_zone_temp,
283 };
284 
proc_thermal_read_ppcc(struct proc_thermal_device * proc_priv)285 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
286 {
287 	int i;
288 	acpi_status status;
289 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
290 	union acpi_object *elements, *ppcc;
291 	union acpi_object *p;
292 	int ret = 0;
293 
294 	status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
295 				      NULL, &buf);
296 	if (ACPI_FAILURE(status))
297 		return -ENODEV;
298 
299 	p = buf.pointer;
300 	if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
301 		dev_err(proc_priv->dev, "Invalid PPCC data\n");
302 		ret = -EFAULT;
303 		goto free_buffer;
304 	}
305 
306 	if (!p->package.count) {
307 		dev_err(proc_priv->dev, "Invalid PPCC package size\n");
308 		ret = -EFAULT;
309 		goto free_buffer;
310 	}
311 
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) {
316 			ret = -EFAULT;
317 			goto free_buffer;
318 		}
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;
326 	}
327 
328 free_buffer:
329 	kfree(buf.pointer);
330 
331 	return ret;
332 }
333 
334 #define PROC_POWER_CAPABILITY_CHANGED	0x83
proc_thermal_notify(acpi_handle handle,u32 event,void * data)335 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
336 {
337 	struct proc_thermal_device *proc_priv = data;
338 
339 	if (!proc_priv)
340 		return;
341 
342 	switch (event) {
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);
347 		break;
348 	default:
349 		dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
350 		break;
351 	}
352 }
353 
354 
proc_thermal_add(struct device * dev,struct proc_thermal_device ** priv)355 static int proc_thermal_add(struct device *dev,
356 			    struct proc_thermal_device **priv)
357 {
358 	struct proc_thermal_device *proc_priv;
359 	struct acpi_device *adev;
360 	acpi_status status;
361 	unsigned long long tmp;
362 	struct thermal_zone_device_ops *ops = NULL;
363 	int ret;
364 
365 	adev = ACPI_COMPANION(dev);
366 	if (!adev)
367 		return -ENODEV;
368 
369 	proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
370 	if (!proc_priv)
371 		return -ENOMEM;
372 
373 	proc_priv->dev = dev;
374 	proc_priv->adev = adev;
375 	*priv = proc_priv;
376 
377 	ret = proc_thermal_read_ppcc(proc_priv);
378 	if (ret)
379 		return ret;
380 
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;
387 	}
388 
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);
392 	} else
393 		ret = 0;
394 
395 	ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
396 					  proc_thermal_notify,
397 					  (void *)proc_priv);
398 	if (ret)
399 		goto remove_zone;
400 
401 	return 0;
402 
403 remove_zone:
404 	int340x_thermal_zone_remove(proc_priv->int340x_zone);
405 
406 	return ret;
407 }
408 
proc_thermal_remove(struct proc_thermal_device * proc_priv)409 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
410 {
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);
417 }
418 
int3401_add(struct platform_device * pdev)419 static int int3401_add(struct platform_device *pdev)
420 {
421 	struct proc_thermal_device *proc_priv;
422 	int ret;
423 
424 	if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
425 		dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
426 		return -ENODEV;
427 	}
428 
429 	ret = proc_thermal_add(&pdev->dev, &proc_priv);
430 	if (ret)
431 		return ret;
432 
433 	platform_set_drvdata(pdev, proc_priv);
434 	proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
435 
436 	dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
437 
438 	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
439 	if (ret)
440 		return ret;
441 
442 	ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
443 	if (ret)
444 		sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
445 
446 	return ret;
447 }
448 
int3401_remove(struct platform_device * pdev)449 static int int3401_remove(struct platform_device *pdev)
450 {
451 	proc_thermal_remove(platform_get_drvdata(pdev));
452 
453 	return 0;
454 }
455 
proc_thermal_pci_msi_irq(int irq,void * devid)456 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
457 {
458 	struct proc_thermal_device *proc_priv;
459 	struct pci_dev *pdev = devid;
460 
461 	proc_priv = pci_get_drvdata(pdev);
462 
463 	intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
464 
465 	return IRQ_HANDLED;
466 }
467 
468 #ifdef CONFIG_PROC_THERMAL_MMIO_RAPL
469 
470 #define MCHBAR 0
471 
472 /* RAPL Support via MMIO interface */
473 static struct rapl_if_priv rapl_mmio_priv;
474 
rapl_mmio_cpu_online(unsigned int cpu)475 static int rapl_mmio_cpu_online(unsigned int cpu)
476 {
477 	struct rapl_package *rp;
478 
479 	/* mmio rapl supports package 0 only for now */
480 	if (topology_physical_package_id(cpu))
481 		return 0;
482 
483 	rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
484 	if (!rp) {
485 		rp = rapl_add_package(cpu, &rapl_mmio_priv);
486 		if (IS_ERR(rp))
487 			return PTR_ERR(rp);
488 	}
489 	cpumask_set_cpu(cpu, &rp->cpumask);
490 	return 0;
491 }
492 
rapl_mmio_cpu_down_prep(unsigned int cpu)493 static int rapl_mmio_cpu_down_prep(unsigned int cpu)
494 {
495 	struct rapl_package *rp;
496 	int lead_cpu;
497 
498 	rp = rapl_find_package_domain(cpu, &rapl_mmio_priv);
499 	if (!rp)
500 		return 0;
501 
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;
508 	return 0;
509 }
510 
rapl_mmio_read_raw(int cpu,struct reg_action * ra)511 static int rapl_mmio_read_raw(int cpu, struct reg_action *ra)
512 {
513 	if (!ra->reg)
514 		return -EINVAL;
515 
516 	ra->value = readq((void __iomem *)ra->reg);
517 	ra->value &= ra->mask;
518 	return 0;
519 }
520 
rapl_mmio_write_raw(int cpu,struct reg_action * ra)521 static int rapl_mmio_write_raw(int cpu, struct reg_action *ra)
522 {
523 	u64 val;
524 
525 	if (!ra->reg)
526 		return -EINVAL;
527 
528 	val = readq((void __iomem *)ra->reg);
529 	val &= ~ra->mask;
530 	val |= ra->value;
531 	writeq(val, (void __iomem *)ra->reg);
532 	return 0;
533 }
534 
proc_thermal_rapl_add(struct pci_dev * pdev,struct proc_thermal_device * proc_priv,struct rapl_mmio_regs * rapl_regs)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)
538 {
539 	enum rapl_domain_reg_id reg;
540 	enum rapl_domain_type domain;
541 	int ret;
542 
543 	if (!rapl_regs)
544 		return 0;
545 
546 	ret = pcim_iomap_regions(pdev, 1 << MCHBAR, DRV_NAME);
547 	if (ret) {
548 		dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
549 		return -ENOMEM;
550 	}
551 
552 	proc_priv->mmio_base = pcim_iomap_table(pdev)[MCHBAR];
553 
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];
561 	}
562 	rapl_mmio_priv.reg_unit = (u64)proc_priv->mmio_base + rapl_regs->reg_unit;
563 
564 	rapl_mmio_priv.read_raw = rapl_mmio_read_raw;
565 	rapl_mmio_priv.write_raw = rapl_mmio_write_raw;
566 
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);
571 	}
572 
573 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
574 				rapl_mmio_cpu_online, rapl_mmio_cpu_down_prep);
575 	if (ret < 0) {
576 		powercap_unregister_control_type(rapl_mmio_priv.control_type);
577 		rapl_mmio_priv.control_type = NULL;
578 		return ret;
579 	}
580 	rapl_mmio_priv.pcap_rapl_online = ret;
581 
582 	return 0;
583 }
584 
proc_thermal_rapl_remove(void)585 static void proc_thermal_rapl_remove(void)
586 {
587 	if (IS_ERR_OR_NULL(rapl_mmio_priv.control_type))
588 		return;
589 
590 	cpuhp_remove_state(rapl_mmio_priv.pcap_rapl_online);
591 	powercap_unregister_control_type(rapl_mmio_priv.control_type);
592 }
593 
594 static const struct rapl_mmio_regs rapl_mmio_hsw = {
595 	.reg_unit = 0x5938,
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,
600 };
601 
602 #else
603 
proc_thermal_rapl_add(struct pci_dev * pdev,struct proc_thermal_device * proc_priv,struct rapl_mmio_regs * rapl_regs)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)
607 {
608 	return 0;
609 }
proc_thermal_rapl_remove(void)610 static void proc_thermal_rapl_remove(void) {}
611 static const struct rapl_mmio_regs rapl_mmio_hsw;
612 
613 #endif /* CONFIG_MMIO_RAPL */
614 
proc_thermal_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)615 static int  proc_thermal_pci_probe(struct pci_dev *pdev,
616 				   const struct pci_device_id *id)
617 {
618 	struct proc_thermal_device *proc_priv;
619 	int ret;
620 
621 	if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
622 		dev_err(&pdev->dev, "error: enumerated as platform dev\n");
623 		return -ENODEV;
624 	}
625 
626 	ret = pcim_enable_device(pdev);
627 	if (ret < 0) {
628 		dev_err(&pdev->dev, "error: could not enable device\n");
629 		return ret;
630 	}
631 
632 	ret = proc_thermal_add(&pdev->dev, &proc_priv);
633 	if (ret)
634 		return ret;
635 
636 	ret = proc_thermal_rapl_add(pdev, proc_priv,
637 				(struct rapl_mmio_regs *)id->driver_data);
638 	if (ret) {
639 		dev_err(&pdev->dev, "failed to add RAPL MMIO interface\n");
640 		proc_thermal_remove(proc_priv);
641 		return ret;
642 	}
643 
644 	pci_set_drvdata(pdev, proc_priv);
645 	proc_thermal_emum_mode = PROC_THERMAL_PCI;
646 
647 	if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
648 		/*
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.
654 		 */
655 		proc_priv->soc_dts = intel_soc_dts_iosf_init(
656 					INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
657 
658 		if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
659 			ret = pci_enable_msi(pdev);
660 			if (!ret) {
661 				ret = request_threaded_irq(pdev->irq, NULL,
662 						proc_thermal_pci_msi_irq,
663 						IRQF_ONESHOT, "proc_thermal",
664 						pdev);
665 				if (ret) {
666 					intel_soc_dts_iosf_exit(
667 							proc_priv->soc_dts);
668 					pci_disable_msi(pdev);
669 					proc_priv->soc_dts = NULL;
670 				}
671 			}
672 		} else
673 			dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
674 	}
675 
676 	dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
677 
678 	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
679 	if (ret)
680 		return ret;
681 
682 	ret = sysfs_create_group(&pdev->dev.kobj, &power_limit_attribute_group);
683 	if (ret)
684 		sysfs_remove_file(&pdev->dev.kobj, &dev_attr_tcc_offset_degree_celsius.attr);
685 
686 	return ret;
687 }
688 
proc_thermal_pci_remove(struct pci_dev * pdev)689 static void  proc_thermal_pci_remove(struct pci_dev *pdev)
690 {
691 	struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
692 
693 	if (proc_priv->soc_dts) {
694 		intel_soc_dts_iosf_exit(proc_priv->soc_dts);
695 		if (pdev->irq) {
696 			free_irq(pdev->irq, pdev);
697 			pci_disable_msi(pdev);
698 		}
699 	}
700 	proc_thermal_rapl_remove();
701 	proc_thermal_remove(proc_priv);
702 }
703 
704 #ifdef CONFIG_PM_SLEEP
proc_thermal_resume(struct device * dev)705 static int proc_thermal_resume(struct device *dev)
706 {
707 	struct proc_thermal_device *proc_dev;
708 
709 	proc_dev = dev_get_drvdata(dev);
710 	proc_thermal_read_ppcc(proc_dev);
711 
712 	if (tcc_offset_save >= 0)
713 		tcc_offset_update(tcc_offset_save);
714 
715 	return 0;
716 }
717 #else
718 #define proc_thermal_resume NULL
719 #endif
720 
721 static SIMPLE_DEV_PM_OPS(proc_thermal_pm, NULL, proc_thermal_resume);
722 
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, },
741 	{ 0, },
742 };
743 
744 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
745 
746 static struct pci_driver proc_thermal_pci_driver = {
747 	.name		= DRV_NAME,
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,
752 };
753 
754 static const struct acpi_device_id int3401_device_ids[] = {
755 	{"INT3401", 0},
756 	{"", 0},
757 };
758 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
759 
760 static struct platform_driver int3401_driver = {
761 	.probe = int3401_add,
762 	.remove = int3401_remove,
763 	.driver = {
764 		.name = "int3401 thermal",
765 		.acpi_match_table = int3401_device_ids,
766 		.pm = &proc_thermal_pm,
767 	},
768 };
769 
proc_thermal_init(void)770 static int __init proc_thermal_init(void)
771 {
772 	int ret;
773 
774 	ret = platform_driver_register(&int3401_driver);
775 	if (ret)
776 		return ret;
777 
778 	ret = pci_register_driver(&proc_thermal_pci_driver);
779 
780 	return ret;
781 }
782 
proc_thermal_exit(void)783 static void __exit proc_thermal_exit(void)
784 {
785 	platform_driver_unregister(&int3401_driver);
786 	pci_unregister_driver(&proc_thermal_pci_driver);
787 }
788 
789 module_init(proc_thermal_init);
790 module_exit(proc_thermal_exit);
791 
792 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
793 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
794 MODULE_LICENSE("GPL v2");
795