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 "int340x_thermal_zone.h"
15 #include "../intel_soc_dts_iosf.h"
16 
17 /* Broadwell-U/HSB thermal reporting device */
18 #define PCI_DEVICE_ID_PROC_BDW_THERMAL	0x1603
19 #define PCI_DEVICE_ID_PROC_HSB_THERMAL	0x0A03
20 
21 /* Skylake thermal reporting device */
22 #define PCI_DEVICE_ID_PROC_SKL_THERMAL	0x1903
23 
24 /* CannonLake thermal reporting device */
25 #define PCI_DEVICE_ID_PROC_CNL_THERMAL	0x5a03
26 #define PCI_DEVICE_ID_PROC_CFL_THERMAL	0x3E83
27 
28 /* Braswell thermal reporting device */
29 #define PCI_DEVICE_ID_PROC_BSW_THERMAL	0x22DC
30 
31 /* Broxton thermal reporting device */
32 #define PCI_DEVICE_ID_PROC_BXT0_THERMAL  0x0A8C
33 #define PCI_DEVICE_ID_PROC_BXT1_THERMAL  0x1A8C
34 #define PCI_DEVICE_ID_PROC_BXTX_THERMAL  0x4A8C
35 #define PCI_DEVICE_ID_PROC_BXTP_THERMAL  0x5A8C
36 
37 /* GeminiLake thermal reporting device */
38 #define PCI_DEVICE_ID_PROC_GLK_THERMAL	0x318C
39 
40 struct power_config {
41 	u32	index;
42 	u32	min_uw;
43 	u32	max_uw;
44 	u32	tmin_us;
45 	u32	tmax_us;
46 	u32	step_uw;
47 };
48 
49 struct proc_thermal_device {
50 	struct device *dev;
51 	struct acpi_device *adev;
52 	struct power_config power_limits[2];
53 	struct int34x_thermal_zone *int340x_zone;
54 	struct intel_soc_dts_sensors *soc_dts;
55 };
56 
57 enum proc_thermal_emum_mode_type {
58 	PROC_THERMAL_NONE,
59 	PROC_THERMAL_PCI,
60 	PROC_THERMAL_PLATFORM_DEV
61 };
62 
63 /*
64  * We can have only one type of enumeration, PCI or Platform,
65  * not both. So we don't need instance specific data.
66  */
67 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
68 							PROC_THERMAL_NONE;
69 
70 #define POWER_LIMIT_SHOW(index, suffix) \
71 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
72 					struct device_attribute *attr, \
73 					char *buf) \
74 { \
75 	struct proc_thermal_device *proc_dev = dev_get_drvdata(dev); \
76 	\
77 	if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \
78 		dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \
79 		return 0; \
80 	} \
81 	\
82 	return sprintf(buf, "%lu\n",\
83 	(unsigned long)proc_dev->power_limits[index].suffix * 1000); \
84 }
85 
86 POWER_LIMIT_SHOW(0, min_uw)
87 POWER_LIMIT_SHOW(0, max_uw)
88 POWER_LIMIT_SHOW(0, step_uw)
89 POWER_LIMIT_SHOW(0, tmin_us)
90 POWER_LIMIT_SHOW(0, tmax_us)
91 
92 POWER_LIMIT_SHOW(1, min_uw)
93 POWER_LIMIT_SHOW(1, max_uw)
94 POWER_LIMIT_SHOW(1, step_uw)
95 POWER_LIMIT_SHOW(1, tmin_us)
96 POWER_LIMIT_SHOW(1, tmax_us)
97 
98 static DEVICE_ATTR_RO(power_limit_0_min_uw);
99 static DEVICE_ATTR_RO(power_limit_0_max_uw);
100 static DEVICE_ATTR_RO(power_limit_0_step_uw);
101 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
102 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
103 
104 static DEVICE_ATTR_RO(power_limit_1_min_uw);
105 static DEVICE_ATTR_RO(power_limit_1_max_uw);
106 static DEVICE_ATTR_RO(power_limit_1_step_uw);
107 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
108 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
109 
110 static struct attribute *power_limit_attrs[] = {
111 	&dev_attr_power_limit_0_min_uw.attr,
112 	&dev_attr_power_limit_1_min_uw.attr,
113 	&dev_attr_power_limit_0_max_uw.attr,
114 	&dev_attr_power_limit_1_max_uw.attr,
115 	&dev_attr_power_limit_0_step_uw.attr,
116 	&dev_attr_power_limit_1_step_uw.attr,
117 	&dev_attr_power_limit_0_tmin_us.attr,
118 	&dev_attr_power_limit_1_tmin_us.attr,
119 	&dev_attr_power_limit_0_tmax_us.attr,
120 	&dev_attr_power_limit_1_tmax_us.attr,
121 	NULL
122 };
123 
124 static const struct attribute_group power_limit_attribute_group = {
125 	.attrs = power_limit_attrs,
126 	.name = "power_limits"
127 };
128 
129 static int stored_tjmax; /* since it is fixed, we can have local storage */
130 
131 static int get_tjmax(void)
132 {
133 	u32 eax, edx;
134 	u32 val;
135 	int err;
136 
137 	err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
138 	if (err)
139 		return err;
140 
141 	val = (eax >> 16) & 0xff;
142 	if (val)
143 		return val;
144 
145 	return -EINVAL;
146 }
147 
148 static int read_temp_msr(int *temp)
149 {
150 	int cpu;
151 	u32 eax, edx;
152 	int err;
153 	unsigned long curr_temp_off = 0;
154 
155 	*temp = 0;
156 
157 	for_each_online_cpu(cpu) {
158 		err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
159 					&edx);
160 		if (err)
161 			goto err_ret;
162 		else {
163 			if (eax & 0x80000000) {
164 				curr_temp_off = (eax >> 16) & 0x7f;
165 				if (!*temp || curr_temp_off < *temp)
166 					*temp = curr_temp_off;
167 			} else {
168 				err = -EINVAL;
169 				goto err_ret;
170 			}
171 		}
172 	}
173 
174 	return 0;
175 err_ret:
176 	return err;
177 }
178 
179 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
180 					 int *temp)
181 {
182 	int ret;
183 
184 	ret = read_temp_msr(temp);
185 	if (!ret)
186 		*temp = (stored_tjmax - *temp) * 1000;
187 
188 	return ret;
189 }
190 
191 static struct thermal_zone_device_ops proc_thermal_local_ops = {
192 	.get_temp       = proc_thermal_get_zone_temp,
193 };
194 
195 static int proc_thermal_read_ppcc(struct proc_thermal_device *proc_priv)
196 {
197 	int i;
198 	acpi_status status;
199 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
200 	union acpi_object *elements, *ppcc;
201 	union acpi_object *p;
202 	int ret = 0;
203 
204 	status = acpi_evaluate_object(proc_priv->adev->handle, "PPCC",
205 				      NULL, &buf);
206 	if (ACPI_FAILURE(status))
207 		return -ENODEV;
208 
209 	p = buf.pointer;
210 	if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
211 		dev_err(proc_priv->dev, "Invalid PPCC data\n");
212 		ret = -EFAULT;
213 		goto free_buffer;
214 	}
215 
216 	if (!p->package.count) {
217 		dev_err(proc_priv->dev, "Invalid PPCC package size\n");
218 		ret = -EFAULT;
219 		goto free_buffer;
220 	}
221 
222 	for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
223 		elements = &(p->package.elements[i+1]);
224 		if (elements->type != ACPI_TYPE_PACKAGE ||
225 		    elements->package.count != 6) {
226 			ret = -EFAULT;
227 			goto free_buffer;
228 		}
229 		ppcc = elements->package.elements;
230 		proc_priv->power_limits[i].index = ppcc[0].integer.value;
231 		proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
232 		proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
233 		proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
234 		proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
235 		proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
236 	}
237 
238 free_buffer:
239 	kfree(buf.pointer);
240 
241 	return ret;
242 }
243 
244 #define PROC_POWER_CAPABILITY_CHANGED	0x83
245 static void proc_thermal_notify(acpi_handle handle, u32 event, void *data)
246 {
247 	struct proc_thermal_device *proc_priv = data;
248 
249 	if (!proc_priv)
250 		return;
251 
252 	switch (event) {
253 	case PROC_POWER_CAPABILITY_CHANGED:
254 		proc_thermal_read_ppcc(proc_priv);
255 		int340x_thermal_zone_device_update(proc_priv->int340x_zone,
256 				THERMAL_DEVICE_POWER_CAPABILITY_CHANGED);
257 		break;
258 	default:
259 		dev_dbg(proc_priv->dev, "Unsupported event [0x%x]\n", event);
260 		break;
261 	}
262 }
263 
264 
265 static int proc_thermal_add(struct device *dev,
266 			    struct proc_thermal_device **priv)
267 {
268 	struct proc_thermal_device *proc_priv;
269 	struct acpi_device *adev;
270 	acpi_status status;
271 	unsigned long long tmp;
272 	struct thermal_zone_device_ops *ops = NULL;
273 	int ret;
274 
275 	adev = ACPI_COMPANION(dev);
276 	if (!adev)
277 		return -ENODEV;
278 
279 	proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
280 	if (!proc_priv)
281 		return -ENOMEM;
282 
283 	proc_priv->dev = dev;
284 	proc_priv->adev = adev;
285 	*priv = proc_priv;
286 
287 	ret = proc_thermal_read_ppcc(proc_priv);
288 	if (ret)
289 		return ret;
290 
291 	status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
292 	if (ACPI_FAILURE(status)) {
293 		/* there is no _TMP method, add local method */
294 		stored_tjmax = get_tjmax();
295 		if (stored_tjmax > 0)
296 			ops = &proc_thermal_local_ops;
297 	}
298 
299 	proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
300 	if (IS_ERR(proc_priv->int340x_zone)) {
301 		return PTR_ERR(proc_priv->int340x_zone);
302 	} else
303 		ret = 0;
304 
305 	ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
306 					  proc_thermal_notify,
307 					  (void *)proc_priv);
308 	if (ret)
309 		goto remove_zone;
310 
311 	return 0;
312 
313 remove_zone:
314 	int340x_thermal_zone_remove(proc_priv->int340x_zone);
315 
316 	return ret;
317 }
318 
319 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
320 {
321 	acpi_remove_notify_handler(proc_priv->adev->handle,
322 				   ACPI_DEVICE_NOTIFY, proc_thermal_notify);
323 	int340x_thermal_zone_remove(proc_priv->int340x_zone);
324 	sysfs_remove_group(&proc_priv->dev->kobj,
325 			   &power_limit_attribute_group);
326 }
327 
328 static int int3401_add(struct platform_device *pdev)
329 {
330 	struct proc_thermal_device *proc_priv;
331 	int ret;
332 
333 	if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
334 		dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
335 		return -ENODEV;
336 	}
337 
338 	ret = proc_thermal_add(&pdev->dev, &proc_priv);
339 	if (ret)
340 		return ret;
341 
342 	platform_set_drvdata(pdev, proc_priv);
343 	proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
344 
345 	dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n");
346 
347 	return sysfs_create_group(&pdev->dev.kobj,
348 					 &power_limit_attribute_group);
349 }
350 
351 static int int3401_remove(struct platform_device *pdev)
352 {
353 	proc_thermal_remove(platform_get_drvdata(pdev));
354 
355 	return 0;
356 }
357 
358 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
359 {
360 	struct proc_thermal_device *proc_priv;
361 	struct pci_dev *pdev = devid;
362 
363 	proc_priv = pci_get_drvdata(pdev);
364 
365 	intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
366 
367 	return IRQ_HANDLED;
368 }
369 
370 static int  proc_thermal_pci_probe(struct pci_dev *pdev,
371 				   const struct pci_device_id *unused)
372 {
373 	struct proc_thermal_device *proc_priv;
374 	int ret;
375 
376 	if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
377 		dev_err(&pdev->dev, "error: enumerated as platform dev\n");
378 		return -ENODEV;
379 	}
380 
381 	ret = pci_enable_device(pdev);
382 	if (ret < 0) {
383 		dev_err(&pdev->dev, "error: could not enable device\n");
384 		return ret;
385 	}
386 
387 	ret = proc_thermal_add(&pdev->dev, &proc_priv);
388 	if (ret) {
389 		pci_disable_device(pdev);
390 		return ret;
391 	}
392 
393 	pci_set_drvdata(pdev, proc_priv);
394 	proc_thermal_emum_mode = PROC_THERMAL_PCI;
395 
396 	if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
397 		/*
398 		 * Enumerate additional DTS sensors available via IOSF.
399 		 * But we are not treating as a failure condition, if
400 		 * there are no aux DTSs enabled or fails. This driver
401 		 * already exposes sensors, which can be accessed via
402 		 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
403 		 */
404 		proc_priv->soc_dts = intel_soc_dts_iosf_init(
405 					INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
406 
407 		if (!IS_ERR(proc_priv->soc_dts) && pdev->irq) {
408 			ret = pci_enable_msi(pdev);
409 			if (!ret) {
410 				ret = request_threaded_irq(pdev->irq, NULL,
411 						proc_thermal_pci_msi_irq,
412 						IRQF_ONESHOT, "proc_thermal",
413 						pdev);
414 				if (ret) {
415 					intel_soc_dts_iosf_exit(
416 							proc_priv->soc_dts);
417 					pci_disable_msi(pdev);
418 					proc_priv->soc_dts = NULL;
419 				}
420 			}
421 		} else
422 			dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
423 	}
424 
425 	dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n");
426 
427 	return sysfs_create_group(&pdev->dev.kobj,
428 					 &power_limit_attribute_group);
429 }
430 
431 static void  proc_thermal_pci_remove(struct pci_dev *pdev)
432 {
433 	struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
434 
435 	if (proc_priv->soc_dts) {
436 		intel_soc_dts_iosf_exit(proc_priv->soc_dts);
437 		if (pdev->irq) {
438 			free_irq(pdev->irq, pdev);
439 			pci_disable_msi(pdev);
440 		}
441 	}
442 	proc_thermal_remove(proc_priv);
443 	pci_disable_device(pdev);
444 }
445 
446 static const struct pci_device_id proc_thermal_pci_ids[] = {
447 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
448 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
449 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)},
450 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
451 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT0_THERMAL)},
452 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXT1_THERMAL)},
453 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTX_THERMAL)},
454 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BXTP_THERMAL)},
455 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CNL_THERMAL)},
456 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_CFL_THERMAL)},
457 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_GLK_THERMAL)},
458 	{ 0, },
459 };
460 
461 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
462 
463 static struct pci_driver proc_thermal_pci_driver = {
464 	.name		= "proc_thermal",
465 	.probe		= proc_thermal_pci_probe,
466 	.remove		= proc_thermal_pci_remove,
467 	.id_table	= proc_thermal_pci_ids,
468 };
469 
470 static const struct acpi_device_id int3401_device_ids[] = {
471 	{"INT3401", 0},
472 	{"", 0},
473 };
474 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
475 
476 static struct platform_driver int3401_driver = {
477 	.probe = int3401_add,
478 	.remove = int3401_remove,
479 	.driver = {
480 		.name = "int3401 thermal",
481 		.acpi_match_table = int3401_device_ids,
482 	},
483 };
484 
485 static int __init proc_thermal_init(void)
486 {
487 	int ret;
488 
489 	ret = platform_driver_register(&int3401_driver);
490 	if (ret)
491 		return ret;
492 
493 	ret = pci_register_driver(&proc_thermal_pci_driver);
494 
495 	return ret;
496 }
497 
498 static void __exit proc_thermal_exit(void)
499 {
500 	platform_driver_unregister(&int3401_driver);
501 	pci_unregister_driver(&proc_thermal_pci_driver);
502 }
503 
504 module_init(proc_thermal_init);
505 module_exit(proc_thermal_exit);
506 
507 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
508 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
509 MODULE_LICENSE("GPL v2");
510