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