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 #define PCH_MAX_TRIPS 3 /* critical, hot, passive */
70 
71 /* Amount of time for each cooling delay, 100ms by default for now */
72 static unsigned int delay_timeout = 100;
73 module_param(delay_timeout, int, 0644);
74 MODULE_PARM_DESC(delay_timeout, "amount of time delay for each iteration.");
75 
76 /* Number of iterations for cooling delay, 600 counts by default for now */
77 static unsigned int delay_cnt = 600;
78 module_param(delay_cnt, int, 0644);
79 MODULE_PARM_DESC(delay_cnt, "total number of iterations for time delay.");
80 
81 static char driver_name[] = "Intel PCH thermal driver";
82 
83 struct pch_thermal_device {
84 	void __iomem *hw_base;
85 	struct pci_dev *pdev;
86 	struct thermal_zone_device *tzd;
87 	struct thermal_trip trips[PCH_MAX_TRIPS];
88 	bool bios_enabled;
89 };
90 
91 #ifdef CONFIG_ACPI
92 /*
93  * On some platforms, there is a companion ACPI device, which adds
94  * passive trip temperature using _PSV method. There is no specific
95  * passive temperature setting in MMIO interface of this PCI device.
96  */
97 static int pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd, int trip)
98 {
99 	struct acpi_device *adev;
100 	int temp;
101 
102 	adev = ACPI_COMPANION(&ptd->pdev->dev);
103 	if (!adev)
104 		return 0;
105 
106 	if (thermal_acpi_passive_trip_temp(adev, &temp) || temp <= 0)
107 		return 0;
108 
109 	ptd->trips[trip].type = THERMAL_TRIP_PASSIVE;
110 	ptd->trips[trip].temperature = temp;
111 	return 1;
112 }
113 #else
114 static int pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd, int trip)
115 {
116 	return 0;
117 }
118 #endif
119 
120 /* Cool the PCH when it's overheat in .suspend_noirq phase */
121 static int pch_suspend(struct pch_thermal_device *ptd)
122 {
123 	u8 tsel;
124 	int pch_delay_cnt = 0;
125 	u16 pch_thr_temp, pch_cur_temp;
126 
127 	/* Shutdown the thermal sensor if it is not enabled by BIOS */
128 	if (!ptd->bios_enabled) {
129 		tsel = readb(ptd->hw_base + WPT_TSEL);
130 		writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
131 		return 0;
132 	}
133 
134 	/* Do not check temperature if it is not s2idle */
135 	if (pm_suspend_via_firmware())
136 		return 0;
137 
138 	/* Get the PCH temperature threshold value */
139 	pch_thr_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TSPM));
140 
141 	/* Get the PCH current temperature value */
142 	pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
143 
144 	/*
145 	 * If current PCH temperature is higher than configured PCH threshold
146 	 * value, run some delay loop with sleep to let the current temperature
147 	 * go down below the threshold value which helps to allow system enter
148 	 * lower power S0ix suspend state. Even after delay loop if PCH current
149 	 * temperature stays above threshold, notify the warning message
150 	 * which helps to indentify the reason why S0ix entry was rejected.
151 	 */
152 	while (pch_delay_cnt < delay_cnt) {
153 		if (pch_cur_temp < pch_thr_temp)
154 			break;
155 
156 		if (pm_wakeup_pending()) {
157 			dev_warn(&ptd->pdev->dev, "Wakeup event detected, abort cooling\n");
158 			return 0;
159 		}
160 
161 		pch_delay_cnt++;
162 		dev_dbg(&ptd->pdev->dev,
163 			"CPU-PCH current temp [%dC] higher than the threshold temp [%dC], sleep %d times for %d ms duration\n",
164 			pch_cur_temp, pch_thr_temp, pch_delay_cnt, delay_timeout);
165 		msleep(delay_timeout);
166 		/* Read the PCH current temperature for next cycle. */
167 		pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
168 	}
169 
170 	if (pch_cur_temp >= pch_thr_temp)
171 		dev_warn(&ptd->pdev->dev,
172 			"CPU-PCH is hot [%dC] after %d ms delay. S0ix might fail\n",
173 			pch_cur_temp, pch_delay_cnt * delay_timeout);
174 	else {
175 		if (pch_delay_cnt)
176 			dev_info(&ptd->pdev->dev,
177 				"CPU-PCH is cool [%dC] after %d ms delay\n",
178 				pch_cur_temp, pch_delay_cnt * delay_timeout);
179 		else
180 			dev_info(&ptd->pdev->dev,
181 				"CPU-PCH is cool [%dC]\n",
182 				pch_cur_temp);
183 	}
184 
185 	return 0;
186 }
187 
188 static int pch_resume(struct pch_thermal_device *ptd)
189 {
190 	u8 tsel;
191 
192 	if (ptd->bios_enabled)
193 		return 0;
194 
195 	tsel = readb(ptd->hw_base + WPT_TSEL);
196 
197 	writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
198 
199 	return 0;
200 }
201 
202 static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
203 {
204 	struct pch_thermal_device *ptd = tzd->devdata;
205 
206 	*temp = GET_WPT_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
207 	return 0;
208 }
209 
210 static void pch_critical(struct thermal_zone_device *tzd)
211 {
212 	dev_dbg(&tzd->device, "%s: critical temperature reached\n", tzd->type);
213 }
214 
215 static struct thermal_zone_device_ops tzd_ops = {
216 	.get_temp = pch_thermal_get_temp,
217 	.critical = pch_critical,
218 };
219 
220 enum board_ids {
221 	board_hsw,
222 	board_wpt,
223 	board_skl,
224 	board_cnl,
225 	board_cml,
226 	board_lwb,
227 	board_wbg,
228 };
229 
230 static const struct board_info {
231 	const char *name;
232 } board_info[] = {
233 	[board_hsw] = {
234 		.name = "pch_haswell",
235 	},
236 	[board_wpt] = {
237 		.name = "pch_wildcat_point",
238 	},
239 	[board_skl] = {
240 		.name = "pch_skylake",
241 	},
242 	[board_cnl] = {
243 		.name = "pch_cannonlake",
244 	},
245 	[board_cml] = {
246 		.name = "pch_cometlake",
247 	},
248 	[board_lwb] = {
249 		.name = "pch_lewisburg",
250 	},
251 	[board_wbg] = {
252 		.name = "pch_wellsburg",
253 	},
254 };
255 
256 static int intel_pch_thermal_probe(struct pci_dev *pdev,
257 				   const struct pci_device_id *id)
258 {
259 	enum board_ids board_id = id->driver_data;
260 	const struct board_info *bi = &board_info[board_id];
261 	struct pch_thermal_device *ptd;
262 	int nr_trips = 0;
263 	u16 trip_temp;
264 	u8 tsel;
265 	int err;
266 
267 	ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
268 	if (!ptd)
269 		return -ENOMEM;
270 
271 	pci_set_drvdata(pdev, ptd);
272 	ptd->pdev = pdev;
273 
274 	err = pci_enable_device(pdev);
275 	if (err) {
276 		dev_err(&pdev->dev, "failed to enable pci device\n");
277 		return err;
278 	}
279 
280 	err = pci_request_regions(pdev, driver_name);
281 	if (err) {
282 		dev_err(&pdev->dev, "failed to request pci region\n");
283 		goto error_disable;
284 	}
285 
286 	ptd->hw_base = pci_ioremap_bar(pdev, 0);
287 	if (!ptd->hw_base) {
288 		err = -ENOMEM;
289 		dev_err(&pdev->dev, "failed to map mem base\n");
290 		goto error_release;
291 	}
292 
293 	/* Check if BIOS has already enabled thermal sensor */
294 	if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
295 		ptd->bios_enabled = true;
296 		goto read_trips;
297 	}
298 
299 	tsel = readb(ptd->hw_base + WPT_TSEL);
300 	/*
301 	 * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
302 	 * If so, thermal sensor cannot enable. Bail out.
303 	 */
304 	if (tsel & WPT_TSEL_PLDB) {
305 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
306 		err = -ENODEV;
307 		goto error_cleanup;
308 	}
309 
310 	writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
311 	if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
312 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
313 		err = -ENODEV;
314 		goto error_cleanup;
315 	}
316 
317 read_trips:
318 	trip_temp = readw(ptd->hw_base + WPT_CTT);
319 	trip_temp &= 0x1FF;
320 	if (trip_temp) {
321 		ptd->trips[nr_trips].temperature = GET_WPT_TEMP(trip_temp);
322 		ptd->trips[nr_trips++].type = THERMAL_TRIP_CRITICAL;
323 	}
324 
325 	trip_temp = readw(ptd->hw_base + WPT_PHL);
326 	trip_temp &= 0x1FF;
327 	if (trip_temp) {
328 		ptd->trips[nr_trips].temperature = GET_WPT_TEMP(trip_temp);
329 		ptd->trips[nr_trips++].type = THERMAL_TRIP_HOT;
330 	}
331 
332 	nr_trips += pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
333 
334 	ptd->tzd = thermal_zone_device_register_with_trips(bi->name, ptd->trips,
335 							   nr_trips, 0, ptd,
336 							   &tzd_ops, NULL, 0, 0);
337 	if (IS_ERR(ptd->tzd)) {
338 		dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
339 			bi->name);
340 		err = PTR_ERR(ptd->tzd);
341 		goto error_cleanup;
342 	}
343 	err = thermal_zone_device_enable(ptd->tzd);
344 	if (err)
345 		goto err_unregister;
346 
347 	return 0;
348 
349 err_unregister:
350 	thermal_zone_device_unregister(ptd->tzd);
351 error_cleanup:
352 	iounmap(ptd->hw_base);
353 error_release:
354 	pci_release_regions(pdev);
355 error_disable:
356 	pci_disable_device(pdev);
357 	dev_err(&pdev->dev, "pci device failed to probe\n");
358 	return err;
359 }
360 
361 static void intel_pch_thermal_remove(struct pci_dev *pdev)
362 {
363 	struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
364 
365 	thermal_zone_device_unregister(ptd->tzd);
366 	iounmap(ptd->hw_base);
367 	pci_set_drvdata(pdev, NULL);
368 	pci_release_regions(pdev);
369 	pci_disable_device(pdev);
370 }
371 
372 static int intel_pch_thermal_suspend_noirq(struct device *device)
373 {
374 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
375 
376 	return pch_suspend(ptd);
377 }
378 
379 static int intel_pch_thermal_resume(struct device *device)
380 {
381 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
382 
383 	return pch_resume(ptd);
384 }
385 
386 static const struct pci_device_id intel_pch_thermal_id[] = {
387 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
388 		.driver_data = board_hsw, },
389 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
390 		.driver_data = board_hsw, },
391 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
392 		.driver_data = board_wpt, },
393 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
394 		.driver_data = board_skl, },
395 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
396 		.driver_data = board_skl, },
397 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL),
398 		.driver_data = board_cnl, },
399 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_H),
400 		.driver_data = board_cnl, },
401 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_LP),
402 		.driver_data = board_cnl, },
403 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CML_H),
404 		.driver_data = board_cml, },
405 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_LWB),
406 		.driver_data = board_lwb, },
407 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WBG),
408 		.driver_data = board_wbg, },
409 	{ 0, },
410 };
411 MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
412 
413 static const struct dev_pm_ops intel_pch_pm_ops = {
414 	.suspend_noirq = intel_pch_thermal_suspend_noirq,
415 	.resume = intel_pch_thermal_resume,
416 };
417 
418 static struct pci_driver intel_pch_thermal_driver = {
419 	.name		= "intel_pch_thermal",
420 	.id_table	= intel_pch_thermal_id,
421 	.probe		= intel_pch_thermal_probe,
422 	.remove		= intel_pch_thermal_remove,
423 	.driver.pm	= &intel_pch_pm_ops,
424 };
425 
426 module_pci_driver(intel_pch_thermal_driver);
427 
428 MODULE_LICENSE("GPL v2");
429 MODULE_DESCRIPTION("Intel PCH Thermal driver");
430