• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* intel_pch_thermal.c - Intel PCH Thermal driver
3  *
4  * Copyright (c) 2015, Intel Corporation.
5  *
6  * Authors:
7  *     Tushar Dave <tushar.n.dave@intel.com>
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/pci.h>
15 #include <linux/pm.h>
16 #include <linux/suspend.h>
17 #include <linux/thermal.h>
18 #include <linux/types.h>
19 #include <linux/units.h>
20 
21 /* Intel PCH thermal Device IDs */
22 #define PCH_THERMAL_DID_HSW_1	0x9C24 /* Haswell PCH */
23 #define PCH_THERMAL_DID_HSW_2	0x8C24 /* Haswell PCH */
24 #define PCH_THERMAL_DID_WPT	0x9CA4 /* Wildcat Point */
25 #define PCH_THERMAL_DID_SKL	0x9D31 /* Skylake PCH */
26 #define PCH_THERMAL_DID_SKL_H	0xA131 /* Skylake PCH 100 series */
27 #define PCH_THERMAL_DID_CNL	0x9Df9 /* CNL PCH */
28 #define PCH_THERMAL_DID_CNL_H	0xA379 /* CNL-H PCH */
29 #define PCH_THERMAL_DID_CNL_LP	0x02F9 /* CNL-LP PCH */
30 #define PCH_THERMAL_DID_CML_H	0X06F9 /* CML-H PCH */
31 #define PCH_THERMAL_DID_LWB	0xA1B1 /* Lewisburg PCH */
32 #define PCH_THERMAL_DID_WBG	0x8D24 /* Wellsburg PCH */
33 
34 /* Wildcat Point-LP  PCH Thermal registers */
35 #define WPT_TEMP	0x0000	/* Temperature */
36 #define WPT_TSC	0x04	/* Thermal Sensor Control */
37 #define WPT_TSS	0x06	/* Thermal Sensor Status */
38 #define WPT_TSEL	0x08	/* Thermal Sensor Enable and Lock */
39 #define WPT_TSREL	0x0A	/* Thermal Sensor Report Enable and Lock */
40 #define WPT_TSMIC	0x0C	/* Thermal Sensor SMI Control */
41 #define WPT_CTT	0x0010	/* Catastrophic Trip Point */
42 #define WPT_TSPM	0x001C	/* Thermal Sensor Power Management */
43 #define WPT_TAHV	0x0014	/* Thermal Alert High Value */
44 #define WPT_TALV	0x0018	/* Thermal Alert Low Value */
45 #define WPT_TL		0x00000040	/* Throttle Value */
46 #define WPT_PHL	0x0060	/* PCH Hot Level */
47 #define WPT_PHLC	0x62	/* PHL Control */
48 #define WPT_TAS	0x80	/* Thermal Alert Status */
49 #define WPT_TSPIEN	0x82	/* PCI Interrupt Event Enables */
50 #define WPT_TSGPEN	0x84	/* General Purpose Event Enables */
51 
52 /*  Wildcat Point-LP  PCH Thermal Register bit definitions */
53 #define WPT_TEMP_TSR	0x01ff	/* Temp TS Reading */
54 #define WPT_TSC_CPDE	0x01	/* Catastrophic Power-Down Enable */
55 #define WPT_TSS_TSDSS	0x10	/* Thermal Sensor Dynamic Shutdown Status */
56 #define WPT_TSS_GPES	0x08	/* GPE status */
57 #define WPT_TSEL_ETS	0x01    /* Enable TS */
58 #define WPT_TSEL_PLDB	0x80	/* TSEL Policy Lock-Down Bit */
59 #define WPT_TL_TOL	0x000001FF	/* T0 Level */
60 #define WPT_TL_T1L	0x1ff00000	/* T1 Level */
61 #define WPT_TL_TTEN	0x20000000	/* TT Enable */
62 
63 /* Resolution of 1/2 degree C and an offset of -50C */
64 #define PCH_TEMP_OFFSET	(-50)
65 #define GET_WPT_TEMP(x)	((x) * MILLIDEGREE_PER_DEGREE / 2 + WPT_TEMP_OFFSET)
66 #define WPT_TEMP_OFFSET	(PCH_TEMP_OFFSET * MILLIDEGREE_PER_DEGREE)
67 #define GET_PCH_TEMP(x)	(((x) / 2) + PCH_TEMP_OFFSET)
68 
69 /* Amount of time for each cooling delay, 100ms by default for now */
70 static unsigned int delay_timeout = 100;
71 module_param(delay_timeout, int, 0644);
72 MODULE_PARM_DESC(delay_timeout, "amount of time delay for each iteration.");
73 
74 /* Number of iterations for cooling delay, 10 counts by default for now */
75 static unsigned int delay_cnt = 10;
76 module_param(delay_cnt, int, 0644);
77 MODULE_PARM_DESC(delay_cnt, "total number of iterations for time delay.");
78 
79 static char driver_name[] = "Intel PCH thermal driver";
80 
81 struct pch_thermal_device {
82 	void __iomem *hw_base;
83 	const struct pch_dev_ops *ops;
84 	struct pci_dev *pdev;
85 	struct thermal_zone_device *tzd;
86 	int crt_trip_id;
87 	unsigned long crt_temp;
88 	int hot_trip_id;
89 	unsigned long hot_temp;
90 	int psv_trip_id;
91 	unsigned long psv_temp;
92 	bool bios_enabled;
93 };
94 
95 #ifdef CONFIG_ACPI
96 
97 /*
98  * On some platforms, there is a companion ACPI device, which adds
99  * passive trip temperature using _PSV method. There is no specific
100  * passive temperature setting in MMIO interface of this PCI device.
101  */
pch_wpt_add_acpi_psv_trip(struct pch_thermal_device * ptd,int * nr_trips)102 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
103 				      int *nr_trips)
104 {
105 	struct acpi_device *adev;
106 
107 	ptd->psv_trip_id = -1;
108 
109 	adev = ACPI_COMPANION(&ptd->pdev->dev);
110 	if (adev) {
111 		unsigned long long r;
112 		acpi_status status;
113 
114 		status = acpi_evaluate_integer(adev->handle, "_PSV", NULL,
115 					       &r);
116 		if (ACPI_SUCCESS(status)) {
117 			unsigned long trip_temp;
118 
119 			trip_temp = deci_kelvin_to_millicelsius(r);
120 			if (trip_temp) {
121 				ptd->psv_temp = trip_temp;
122 				ptd->psv_trip_id = *nr_trips;
123 				++(*nr_trips);
124 			}
125 		}
126 	}
127 }
128 #else
pch_wpt_add_acpi_psv_trip(struct pch_thermal_device * ptd,int * nr_trips)129 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
130 				      int *nr_trips)
131 {
132 	ptd->psv_trip_id = -1;
133 
134 }
135 #endif
136 
pch_wpt_init(struct pch_thermal_device * ptd,int * nr_trips)137 static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
138 {
139 	u8 tsel;
140 	u16 trip_temp;
141 
142 	*nr_trips = 0;
143 
144 	/* Check if BIOS has already enabled thermal sensor */
145 	if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
146 		ptd->bios_enabled = true;
147 		goto read_trips;
148 	}
149 
150 	tsel = readb(ptd->hw_base + WPT_TSEL);
151 	/*
152 	 * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
153 	 * If so, thermal sensor cannot enable. Bail out.
154 	 */
155 	if (tsel & WPT_TSEL_PLDB) {
156 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
157 		return -ENODEV;
158 	}
159 
160 	writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
161 	if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
162 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
163 		return -ENODEV;
164 	}
165 
166 read_trips:
167 	ptd->crt_trip_id = -1;
168 	trip_temp = readw(ptd->hw_base + WPT_CTT);
169 	trip_temp &= 0x1FF;
170 	if (trip_temp) {
171 		ptd->crt_temp = GET_WPT_TEMP(trip_temp);
172 		ptd->crt_trip_id = 0;
173 		++(*nr_trips);
174 	}
175 
176 	ptd->hot_trip_id = -1;
177 	trip_temp = readw(ptd->hw_base + WPT_PHL);
178 	trip_temp &= 0x1FF;
179 	if (trip_temp) {
180 		ptd->hot_temp = GET_WPT_TEMP(trip_temp);
181 		ptd->hot_trip_id = *nr_trips;
182 		++(*nr_trips);
183 	}
184 
185 	pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
186 
187 	return 0;
188 }
189 
pch_wpt_get_temp(struct pch_thermal_device * ptd,int * temp)190 static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
191 {
192 	*temp = GET_WPT_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
193 
194 	return 0;
195 }
196 
pch_wpt_suspend(struct pch_thermal_device * ptd)197 static int pch_wpt_suspend(struct pch_thermal_device *ptd)
198 {
199 	u8 tsel;
200 	u8 pch_delay_cnt = 1;
201 	u16 pch_thr_temp, pch_cur_temp;
202 
203 	/* Shutdown the thermal sensor if it is not enabled by BIOS */
204 	if (!ptd->bios_enabled) {
205 		tsel = readb(ptd->hw_base + WPT_TSEL);
206 		writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
207 		return 0;
208 	}
209 
210 	/* Do not check temperature if it is not a S0ix capable platform */
211 #ifdef CONFIG_ACPI
212 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
213 		return 0;
214 #else
215 	return 0;
216 #endif
217 
218 	/* Do not check temperature if it is not s2idle */
219 	if (pm_suspend_via_firmware())
220 		return 0;
221 
222 	/* Get the PCH temperature threshold value */
223 	pch_thr_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TSPM));
224 
225 	/* Get the PCH current temperature value */
226 	pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
227 
228 	/*
229 	 * If current PCH temperature is higher than configured PCH threshold
230 	 * value, run some delay loop with sleep to let the current temperature
231 	 * go down below the threshold value which helps to allow system enter
232 	 * lower power S0ix suspend state. Even after delay loop if PCH current
233 	 * temperature stays above threshold, notify the warning message
234 	 * which helps to indentify the reason why S0ix entry was rejected.
235 	 */
236 	while (pch_delay_cnt <= delay_cnt) {
237 		if (pch_cur_temp <= pch_thr_temp)
238 			break;
239 
240 		dev_warn(&ptd->pdev->dev,
241 			"CPU-PCH current temp [%dC] higher than the threshold temp [%dC], sleep %d times for %d ms duration\n",
242 			pch_cur_temp, pch_thr_temp, pch_delay_cnt, delay_timeout);
243 		msleep(delay_timeout);
244 		/* Read the PCH current temperature for next cycle. */
245 		pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
246 		pch_delay_cnt++;
247 	}
248 
249 	if (pch_cur_temp > pch_thr_temp)
250 		dev_warn(&ptd->pdev->dev,
251 			"CPU-PCH is hot [%dC] even after delay, continue to suspend. S0ix might fail\n",
252 			pch_cur_temp);
253 	else
254 		dev_info(&ptd->pdev->dev,
255 			"CPU-PCH is cool [%dC], continue to suspend\n", pch_cur_temp);
256 
257 	return 0;
258 }
259 
pch_wpt_resume(struct pch_thermal_device * ptd)260 static int pch_wpt_resume(struct pch_thermal_device *ptd)
261 {
262 	u8 tsel;
263 
264 	if (ptd->bios_enabled)
265 		return 0;
266 
267 	tsel = readb(ptd->hw_base + WPT_TSEL);
268 
269 	writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
270 
271 	return 0;
272 }
273 
274 struct pch_dev_ops {
275 	int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
276 	int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
277 	int (*suspend)(struct pch_thermal_device *ptd);
278 	int (*resume)(struct pch_thermal_device *ptd);
279 };
280 
281 
282 /* dev ops for Wildcat Point */
283 static const struct pch_dev_ops pch_dev_ops_wpt = {
284 	.hw_init = pch_wpt_init,
285 	.get_temp = pch_wpt_get_temp,
286 	.suspend = pch_wpt_suspend,
287 	.resume = pch_wpt_resume,
288 };
289 
pch_thermal_get_temp(struct thermal_zone_device * tzd,int * temp)290 static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
291 {
292 	struct pch_thermal_device *ptd = tzd->devdata;
293 
294 	return	ptd->ops->get_temp(ptd, temp);
295 }
296 
pch_get_trip_type(struct thermal_zone_device * tzd,int trip,enum thermal_trip_type * type)297 static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
298 			     enum thermal_trip_type *type)
299 {
300 	struct pch_thermal_device *ptd = tzd->devdata;
301 
302 	if (ptd->crt_trip_id == trip)
303 		*type = THERMAL_TRIP_CRITICAL;
304 	else if (ptd->hot_trip_id == trip)
305 		*type = THERMAL_TRIP_HOT;
306 	else if (ptd->psv_trip_id == trip)
307 		*type = THERMAL_TRIP_PASSIVE;
308 	else
309 		return -EINVAL;
310 
311 	return 0;
312 }
313 
pch_get_trip_temp(struct thermal_zone_device * tzd,int trip,int * temp)314 static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
315 {
316 	struct pch_thermal_device *ptd = tzd->devdata;
317 
318 	if (ptd->crt_trip_id == trip)
319 		*temp = ptd->crt_temp;
320 	else if (ptd->hot_trip_id == trip)
321 		*temp = ptd->hot_temp;
322 	else if (ptd->psv_trip_id == trip)
323 		*temp = ptd->psv_temp;
324 	else
325 		return -EINVAL;
326 
327 	return 0;
328 }
329 
pch_critical(struct thermal_zone_device * tzd)330 static void pch_critical(struct thermal_zone_device *tzd)
331 {
332 	dev_dbg(&tzd->device, "%s: critical temperature reached\n", tzd->type);
333 }
334 
335 static struct thermal_zone_device_ops tzd_ops = {
336 	.get_temp = pch_thermal_get_temp,
337 	.get_trip_type = pch_get_trip_type,
338 	.get_trip_temp = pch_get_trip_temp,
339 	.critical = pch_critical,
340 };
341 
342 enum board_ids {
343 	board_hsw,
344 	board_wpt,
345 	board_skl,
346 	board_cnl,
347 	board_cml,
348 	board_lwb,
349 	board_wbg,
350 };
351 
352 static const struct board_info {
353 	const char *name;
354 	const struct pch_dev_ops *ops;
355 } board_info[] = {
356 	[board_hsw] = {
357 		.name = "pch_haswell",
358 		.ops = &pch_dev_ops_wpt,
359 	},
360 	[board_wpt] = {
361 		.name = "pch_wildcat_point",
362 		.ops = &pch_dev_ops_wpt,
363 	},
364 	[board_skl] = {
365 		.name = "pch_skylake",
366 		.ops = &pch_dev_ops_wpt,
367 	},
368 	[board_cnl] = {
369 		.name = "pch_cannonlake",
370 		.ops = &pch_dev_ops_wpt,
371 	},
372 	[board_cml] = {
373 		.name = "pch_cometlake",
374 		.ops = &pch_dev_ops_wpt,
375 	},
376 	[board_lwb] = {
377 		.name = "pch_lewisburg",
378 		.ops = &pch_dev_ops_wpt,
379 	},
380 	[board_wbg] = {
381 		.name = "pch_wellsburg",
382 		.ops = &pch_dev_ops_wpt,
383 	},
384 };
385 
intel_pch_thermal_probe(struct pci_dev * pdev,const struct pci_device_id * id)386 static int intel_pch_thermal_probe(struct pci_dev *pdev,
387 				   const struct pci_device_id *id)
388 {
389 	enum board_ids board_id = id->driver_data;
390 	const struct board_info *bi = &board_info[board_id];
391 	struct pch_thermal_device *ptd;
392 	int err;
393 	int nr_trips;
394 
395 	ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
396 	if (!ptd)
397 		return -ENOMEM;
398 
399 	ptd->ops = bi->ops;
400 
401 	pci_set_drvdata(pdev, ptd);
402 	ptd->pdev = pdev;
403 
404 	err = pci_enable_device(pdev);
405 	if (err) {
406 		dev_err(&pdev->dev, "failed to enable pci device\n");
407 		return err;
408 	}
409 
410 	err = pci_request_regions(pdev, driver_name);
411 	if (err) {
412 		dev_err(&pdev->dev, "failed to request pci region\n");
413 		goto error_disable;
414 	}
415 
416 	ptd->hw_base = pci_ioremap_bar(pdev, 0);
417 	if (!ptd->hw_base) {
418 		err = -ENOMEM;
419 		dev_err(&pdev->dev, "failed to map mem base\n");
420 		goto error_release;
421 	}
422 
423 	err = ptd->ops->hw_init(ptd, &nr_trips);
424 	if (err)
425 		goto error_cleanup;
426 
427 	ptd->tzd = thermal_zone_device_register(bi->name, nr_trips, 0, ptd,
428 						&tzd_ops, NULL, 0, 0);
429 	if (IS_ERR(ptd->tzd)) {
430 		dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
431 			bi->name);
432 		err = PTR_ERR(ptd->tzd);
433 		goto error_cleanup;
434 	}
435 	err = thermal_zone_device_enable(ptd->tzd);
436 	if (err)
437 		goto err_unregister;
438 
439 	return 0;
440 
441 err_unregister:
442 	thermal_zone_device_unregister(ptd->tzd);
443 error_cleanup:
444 	iounmap(ptd->hw_base);
445 error_release:
446 	pci_release_regions(pdev);
447 error_disable:
448 	pci_disable_device(pdev);
449 	dev_err(&pdev->dev, "pci device failed to probe\n");
450 	return err;
451 }
452 
intel_pch_thermal_remove(struct pci_dev * pdev)453 static void intel_pch_thermal_remove(struct pci_dev *pdev)
454 {
455 	struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
456 
457 	thermal_zone_device_unregister(ptd->tzd);
458 	iounmap(ptd->hw_base);
459 	pci_set_drvdata(pdev, NULL);
460 	pci_release_regions(pdev);
461 	pci_disable_device(pdev);
462 }
463 
intel_pch_thermal_suspend(struct device * device)464 static int intel_pch_thermal_suspend(struct device *device)
465 {
466 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
467 
468 	return ptd->ops->suspend(ptd);
469 }
470 
intel_pch_thermal_resume(struct device * device)471 static int intel_pch_thermal_resume(struct device *device)
472 {
473 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
474 
475 	return ptd->ops->resume(ptd);
476 }
477 
478 static const struct pci_device_id intel_pch_thermal_id[] = {
479 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
480 		.driver_data = board_hsw, },
481 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
482 		.driver_data = board_hsw, },
483 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
484 		.driver_data = board_wpt, },
485 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
486 		.driver_data = board_skl, },
487 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
488 		.driver_data = board_skl, },
489 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL),
490 		.driver_data = board_cnl, },
491 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_H),
492 		.driver_data = board_cnl, },
493 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_LP),
494 		.driver_data = board_cnl, },
495 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CML_H),
496 		.driver_data = board_cml, },
497 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_LWB),
498 		.driver_data = board_lwb, },
499 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WBG),
500 		.driver_data = board_wbg, },
501 	{ 0, },
502 };
503 MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
504 
505 static const struct dev_pm_ops intel_pch_pm_ops = {
506 	.suspend = intel_pch_thermal_suspend,
507 	.resume = intel_pch_thermal_resume,
508 };
509 
510 static struct pci_driver intel_pch_thermal_driver = {
511 	.name		= "intel_pch_thermal",
512 	.id_table	= intel_pch_thermal_id,
513 	.probe		= intel_pch_thermal_probe,
514 	.remove		= intel_pch_thermal_remove,
515 	.driver.pm	= &intel_pch_pm_ops,
516 };
517 
518 module_pci_driver(intel_pch_thermal_driver);
519 
520 MODULE_LICENSE("GPL v2");
521 MODULE_DESCRIPTION("Intel PCH Thermal driver");
522