xref: /linux/drivers/acpi/fan_core.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  fan_core.c - ACPI Fan core Driver
4  *
5  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7  *  Copyright (C) 2022 Intel Corporation. All rights reserved.
8  */
9 
10 #include <linux/bits.h>
11 #include <linux/kernel.h>
12 #include <linux/limits.h>
13 #include <linux/math.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/uuid.h>
20 #include <linux/thermal.h>
21 #include <linux/acpi.h>
22 #include <linux/platform_device.h>
23 #include <linux/sort.h>
24 
25 #include "fan.h"
26 
27 #define ACPI_FAN_NOTIFY_STATE_CHANGED	0x80
28 
29 /*
30  * Defined inside the "Fan Noise Signal" section at
31  * https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/design-guide.
32  */
33 static const guid_t acpi_fan_microsoft_guid = GUID_INIT(0xA7611840, 0x99FE, 0x41AE, 0xA4, 0x88,
34 							0x35, 0xC7, 0x59, 0x26, 0xC8, 0xEB);
35 #define ACPI_FAN_DSM_GET_TRIP_POINT_GRANULARITY 1
36 #define ACPI_FAN_DSM_SET_TRIP_POINTS		2
37 #define ACPI_FAN_DSM_GET_OPERATING_RANGES	3
38 
39 /*
40  * Ensures that fans with a very low trip point granularity
41  * do not send too many notifications.
42  */
43 static uint min_trip_distance = 100;
44 module_param(min_trip_distance, uint, 0);
45 MODULE_PARM_DESC(min_trip_distance, "Minimum distance between fan speed trip points in RPM");
46 
47 static const struct acpi_device_id fan_device_ids[] = {
48 	ACPI_FAN_DEVICE_IDS,
49 	{"", 0},
50 };
51 MODULE_DEVICE_TABLE(acpi, fan_device_ids);
52 
53 /* thermal cooling device callbacks */
54 static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
55 			     *state)
56 {
57 	struct acpi_fan *fan = cdev->devdata;
58 
59 	if (fan->acpi4) {
60 		if (fan->fif.fine_grain_ctrl)
61 			*state = 100 / fan->fif.step_size;
62 		else
63 			*state = fan->fps_count - 1;
64 	} else {
65 		*state = 1;
66 	}
67 
68 	return 0;
69 }
70 
71 int acpi_fan_get_fst(acpi_handle handle, struct acpi_fan_fst *fst)
72 {
73 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
74 	acpi_status status;
75 
76 	status = acpi_evaluate_object(handle, "_FST", NULL, &buffer);
77 	if (ACPI_FAILURE(status))
78 		return -ENXIO;
79 
80 	union acpi_object *obj __free(acpi_object_free) = buffer.pointer;
81 	if (!obj)
82 		return -ENODATA;
83 
84 	if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 3)
85 		return -EPROTO;
86 
87 	if (obj->package.elements[0].type != ACPI_TYPE_INTEGER ||
88 	    obj->package.elements[1].type != ACPI_TYPE_INTEGER ||
89 	    obj->package.elements[2].type != ACPI_TYPE_INTEGER)
90 		return -EPROTO;
91 
92 	fst->revision = obj->package.elements[0].integer.value;
93 	fst->control = obj->package.elements[1].integer.value;
94 	fst->speed = obj->package.elements[2].integer.value;
95 
96 	return 0;
97 }
98 
99 static int fan_get_state_acpi4(struct acpi_fan *fan, unsigned long *state)
100 {
101 	struct acpi_device *device = fan->adev;
102 	struct acpi_fan_fst fst;
103 	int status, i;
104 
105 	status = acpi_fan_get_fst(device->handle, &fst);
106 	if (status)
107 		return status;
108 
109 	if (fan->fif.fine_grain_ctrl) {
110 		/* This control should be same what we set using _FSL by spec */
111 		if (fst.control > 100) {
112 			dev_dbg(&device->dev, "Invalid control value returned\n");
113 			goto match_fps;
114 		}
115 
116 		*state = (int) fst.control / fan->fif.step_size;
117 		return 0;
118 	}
119 
120 match_fps:
121 	for (i = 0; i < fan->fps_count; i++) {
122 		if (fst.control == fan->fps[i].control)
123 			break;
124 	}
125 	if (i == fan->fps_count) {
126 		dev_dbg(&device->dev, "No matching fps control value\n");
127 		return -EINVAL;
128 	}
129 
130 	*state = i;
131 
132 	return status;
133 }
134 
135 static int fan_get_state(struct acpi_device *device, unsigned long *state)
136 {
137 	int result;
138 	int acpi_state = ACPI_STATE_D0;
139 
140 	result = acpi_device_update_power(device, &acpi_state);
141 	if (result)
142 		return result;
143 
144 	*state = acpi_state == ACPI_STATE_D3_COLD
145 			|| acpi_state == ACPI_STATE_D3_HOT ?
146 		0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
147 	return 0;
148 }
149 
150 static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
151 			     *state)
152 {
153 	struct acpi_fan *fan = cdev->devdata;
154 
155 	if (fan->acpi4)
156 		return fan_get_state_acpi4(fan, state);
157 	else
158 		return fan_get_state(fan->adev, state);
159 }
160 
161 static int fan_set_state(struct acpi_device *device, unsigned long state)
162 {
163 	if (state != 0 && state != 1)
164 		return -EINVAL;
165 
166 	return acpi_device_set_power(device,
167 				     state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
168 }
169 
170 static int fan_set_state_acpi4(struct acpi_fan *fan, unsigned long state)
171 {
172 	struct acpi_device *device = fan->adev;
173 	acpi_status status;
174 	u64 value = state;
175 	int max_state;
176 
177 	if (fan->fif.fine_grain_ctrl)
178 		max_state = 100 / fan->fif.step_size;
179 	else
180 		max_state = fan->fps_count - 1;
181 
182 	if (state > max_state)
183 		return -EINVAL;
184 
185 	if (fan->fif.fine_grain_ctrl) {
186 		value *= fan->fif.step_size;
187 		/* Spec allows compensate the last step only */
188 		if (value + fan->fif.step_size > 100)
189 			value = 100;
190 	} else {
191 		value = fan->fps[state].control;
192 	}
193 
194 	status = acpi_execute_simple_method(device->handle, "_FSL", value);
195 	if (ACPI_FAILURE(status)) {
196 		dev_dbg(&device->dev, "Failed to set state by _FSL\n");
197 		return -ENODEV;
198 	}
199 
200 	return 0;
201 }
202 
203 static int
204 fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
205 {
206 	struct acpi_fan *fan = cdev->devdata;
207 
208 	if (fan->acpi4)
209 		return fan_set_state_acpi4(fan, state);
210 	else
211 		return fan_set_state(fan->adev, state);
212 }
213 
214 static const struct thermal_cooling_device_ops fan_cooling_ops = {
215 	.get_max_state = fan_get_max_state,
216 	.get_cur_state = fan_get_cur_state,
217 	.set_cur_state = fan_set_cur_state,
218 };
219 
220 /* --------------------------------------------------------------------------
221  *                               Driver Interface
222  * --------------------------------------------------------------------------
223 */
224 
225 static int acpi_fan_get_fif(struct acpi_device *device)
226 {
227 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
228 	struct acpi_fan *fan = acpi_driver_data(device);
229 	struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
230 	u64 fields[4];
231 	struct acpi_buffer fif = { sizeof(fields), fields };
232 	acpi_status status;
233 
234 	status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
235 	if (ACPI_FAILURE(status))
236 		return -ENXIO;
237 
238 	union acpi_object *obj __free(acpi_object_free) = buffer.pointer;
239 	if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
240 		dev_err(&device->dev, "Invalid _FIF data\n");
241 		return -ENODATA;
242 	}
243 
244 	status = acpi_extract_package(obj, &format, &fif);
245 	if (ACPI_FAILURE(status)) {
246 		dev_err(&device->dev, "Invalid _FIF element\n");
247 		return -ENODATA;
248 	}
249 
250 	fan->fif.revision = fields[0];
251 	fan->fif.fine_grain_ctrl = fields[1];
252 	fan->fif.step_size = fields[2];
253 	fan->fif.low_speed_notification = fields[3];
254 
255 	/* If there is a bug in step size and set as 0, change to 1 */
256 	if (!fan->fif.step_size)
257 		fan->fif.step_size = 1;
258 	/* If step size > 9, change to 9 (by spec valid values 1-9) */
259 	else if (fan->fif.step_size > 9)
260 		fan->fif.step_size = 9;
261 
262 	return 0;
263 }
264 
265 static int acpi_fan_speed_cmp(const void *a, const void *b)
266 {
267 	const struct acpi_fan_fps *fps1 = a;
268 	const struct acpi_fan_fps *fps2 = b;
269 	return fps1->speed - fps2->speed;
270 }
271 
272 static int acpi_fan_get_fps(struct device *dev, struct acpi_device *device)
273 {
274 	struct acpi_fan *fan = acpi_driver_data(device);
275 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
276 	acpi_status status;
277 	int i;
278 
279 	status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
280 	if (ACPI_FAILURE(status))
281 		return -ENXIO;
282 
283 	union acpi_object *obj __free(acpi_object_free) = buffer.pointer;
284 	if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
285 		dev_err(&device->dev, "Invalid _FPS data\n");
286 		return -ENODATA;
287 	}
288 
289 	fan->fps_count = obj->package.count - 1; /* minus revision field */
290 	fan->fps = devm_kcalloc(dev, fan->fps_count, sizeof(*fan->fps), GFP_KERNEL);
291 	if (!fan->fps)
292 		return -ENOMEM;
293 
294 	for (i = 0; i < fan->fps_count; i++) {
295 		struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
296 		struct acpi_buffer fps = { offsetof(struct acpi_fan_fps, name),
297 						&fan->fps[i] };
298 		status = acpi_extract_package(&obj->package.elements[i + 1],
299 					      &format, &fps);
300 		if (ACPI_FAILURE(status)) {
301 			dev_err(&device->dev, "Invalid _FPS element\n");
302 			return -ENODATA;
303 		}
304 	}
305 
306 	/* sort the state array according to fan speed in increase order */
307 	sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
308 	     acpi_fan_speed_cmp, NULL);
309 
310 	return 0;
311 }
312 
313 static int acpi_fan_dsm_init(struct device *dev)
314 {
315 	union acpi_object dummy = {
316 		.package = {
317 			.type = ACPI_TYPE_PACKAGE,
318 			.count = 0,
319 			.elements = NULL,
320 		},
321 	};
322 	struct acpi_fan *fan = dev_get_drvdata(dev);
323 	acpi_handle fan_handle = fan->adev->handle;
324 
325 	if (!acpi_check_dsm(fan_handle, &acpi_fan_microsoft_guid, 0,
326 			    BIT(ACPI_FAN_DSM_GET_TRIP_POINT_GRANULARITY) |
327 			    BIT(ACPI_FAN_DSM_SET_TRIP_POINTS)))
328 		return 0;
329 
330 	dev_info(dev, "Using Microsoft fan extensions\n");
331 
332 	union acpi_object *obj __free(acpi_object_free) =
333 		acpi_evaluate_dsm_typed(fan_handle, &acpi_fan_microsoft_guid, 0,
334 					ACPI_FAN_DSM_GET_TRIP_POINT_GRANULARITY,
335 					&dummy, ACPI_TYPE_INTEGER);
336 	if (!obj)
337 		return -ENXIO;
338 
339 	if (obj->integer.value > U32_MAX)
340 		return -EOVERFLOW;
341 
342 	fan->fan_trip_granularity = obj->integer.value;
343 
344 	return 0;
345 }
346 
347 static int acpi_fan_dsm_set_trip_points(struct device *dev, u64 upper, u64 lower)
348 {
349 	union acpi_object args[2] = {
350 		{
351 			.integer = {
352 				.type = ACPI_TYPE_INTEGER,
353 				.value = lower,
354 			},
355 		},
356 		{
357 			.integer = {
358 				.type = ACPI_TYPE_INTEGER,
359 				.value = upper,
360 			},
361 		},
362 	};
363 	struct acpi_fan *fan = dev_get_drvdata(dev);
364 	union acpi_object in = {
365 		.package = {
366 			.type = ACPI_TYPE_PACKAGE,
367 			.count = ARRAY_SIZE(args),
368 			.elements = args,
369 		},
370 	};
371 	union acpi_object *obj;
372 
373 	obj = acpi_evaluate_dsm(fan->adev->handle, &acpi_fan_microsoft_guid,
374 				0, ACPI_FAN_DSM_SET_TRIP_POINTS, &in);
375 	ACPI_FREE(obj);
376 
377 	return 0;
378 }
379 
380 static int acpi_fan_dsm_start(struct device *dev)
381 {
382 	struct acpi_fan *fan = dev_get_drvdata(dev);
383 	int ret;
384 
385 	if (!fan->fan_trip_granularity)
386 		return 0;
387 
388 	/*
389 	 * Some firmware implementations only update the values returned by the
390 	 * _FST control method when a notification is received. This usually
391 	 * works with Microsoft Windows as setting up trip points will keep
392 	 * triggering said notifications, but will cause issues when using _FST
393 	 * without the Microsoft-specific trip point extension.
394 	 *
395 	 * Because of this, an initial notification needs to be triggered to
396 	 * start the cycle of trip points updates. This is achieved by setting
397 	 * the trip points sequencially to two separate ranges. As by the
398 	 * Microsoft specification the firmware should trigger a notification
399 	 * immediately if the fan speed is outside the trip point range. This
400 	 * _should_ result in at least one notification as both ranges do not
401 	 * overlap, meaning that the current fan speed needs to be outside at
402 	 * least one range.
403 	 */
404 	ret = acpi_fan_dsm_set_trip_points(dev, fan->fan_trip_granularity, 0);
405 	if (ret < 0)
406 		return ret;
407 
408 	return acpi_fan_dsm_set_trip_points(dev, fan->fan_trip_granularity * 3,
409 					    fan->fan_trip_granularity * 2);
410 }
411 
412 static int acpi_fan_dsm_update_trips_points(struct device *dev, struct acpi_fan_fst *fst)
413 {
414 	struct acpi_fan *fan = dev_get_drvdata(dev);
415 	u64 upper, lower;
416 
417 	if (!fan->fan_trip_granularity)
418 		return 0;
419 
420 	if (!acpi_fan_speed_valid(fst->speed))
421 		return -EINVAL;
422 
423 	upper = roundup_u64(fst->speed + min_trip_distance, fan->fan_trip_granularity);
424 	if (fst->speed <= min_trip_distance) {
425 		lower = 0;
426 	} else {
427 		/*
428 		 * Valid fan speed values cannot be larger than 32 bit, so
429 		 * we can safely assume that no overflow will happen here.
430 		 */
431 		lower = rounddown((u32)fst->speed - min_trip_distance, fan->fan_trip_granularity);
432 	}
433 
434 	return acpi_fan_dsm_set_trip_points(dev, upper, lower);
435 }
436 
437 static void acpi_fan_notify_handler(acpi_handle handle, u32 event, void *context)
438 {
439 	struct device *dev = context;
440 	struct acpi_fan_fst fst;
441 	int ret;
442 
443 	switch (event) {
444 	case ACPI_FAN_NOTIFY_STATE_CHANGED:
445 		/*
446 		 * The ACPI specification says that we must evaluate _FST when we
447 		 * receive an ACPI event indicating that the fan state has changed.
448 		 */
449 		ret = acpi_fan_get_fst(handle, &fst);
450 		if (ret < 0) {
451 			dev_err(dev, "Error retrieving current fan status: %d\n", ret);
452 		} else {
453 			ret = acpi_fan_dsm_update_trips_points(dev, &fst);
454 			if (ret < 0)
455 				dev_err(dev, "Failed to update trip points: %d\n", ret);
456 		}
457 
458 		acpi_fan_notify_hwmon(dev);
459 		acpi_bus_generate_netlink_event("fan", dev_name(dev), event, 0);
460 		break;
461 	default:
462 		dev_dbg(dev, "Unsupported ACPI notification 0x%x\n", event);
463 		break;
464 	}
465 }
466 
467 static int acpi_fan_probe(struct platform_device *pdev)
468 {
469 	int result = 0;
470 	struct thermal_cooling_device *cdev;
471 	struct acpi_fan *fan;
472 	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
473 	char *name;
474 
475 	if (!device)
476 		return -ENODEV;
477 
478 	fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
479 	if (!fan) {
480 		dev_err(&device->dev, "No memory for fan\n");
481 		return -ENOMEM;
482 	}
483 
484 	fan->adev = device;
485 	device->driver_data = fan;
486 	platform_set_drvdata(pdev, fan);
487 
488 	if (acpi_has_method(device->handle, "_FST")) {
489 		fan->has_fst = true;
490 		fan->acpi4 = acpi_has_method(device->handle, "_FIF") &&
491 				acpi_has_method(device->handle, "_FPS") &&
492 				acpi_has_method(device->handle, "_FSL");
493 	}
494 
495 	if (fan->acpi4) {
496 		result = acpi_fan_get_fif(device);
497 		if (result)
498 			return result;
499 
500 		result = acpi_fan_get_fps(&pdev->dev, device);
501 		if (result)
502 			return result;
503 	}
504 
505 	if (fan->has_fst) {
506 		result = acpi_fan_dsm_init(&pdev->dev);
507 		if (result)
508 			return result;
509 
510 		result = devm_acpi_fan_create_hwmon(&pdev->dev);
511 		if (result)
512 			return result;
513 
514 		result = devm_acpi_install_notify_handler(&pdev->dev,
515 							  ACPI_DEVICE_NOTIFY,
516 							  acpi_fan_notify_handler,
517 							  &pdev->dev);
518 		if (result)
519 			return result;
520 
521 		result = acpi_fan_dsm_start(&pdev->dev);
522 		if (result) {
523 			dev_err(&pdev->dev, "Failed to start Microsoft fan extensions\n");
524 			return result;
525 		}
526 
527 		result = acpi_fan_create_attributes(device);
528 		if (result)
529 			return result;
530 	}
531 
532 	if (!fan->acpi4) {
533 		result = acpi_device_update_power(device, NULL);
534 		if (result) {
535 			dev_err(&device->dev, "Failed to set initial power state\n");
536 			goto err_end;
537 		}
538 	}
539 
540 	if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
541 		name = "Fan";
542 	else
543 		name = acpi_device_bid(device);
544 
545 	cdev = thermal_cooling_device_create(&pdev->dev, name, fan, &fan_cooling_ops);
546 	if (IS_ERR(cdev)) {
547 		result = PTR_ERR(cdev);
548 		goto err_end;
549 	}
550 
551 	dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
552 
553 	fan->cdev = cdev;
554 
555 	return 0;
556 
557 err_end:
558 	if (fan->has_fst)
559 		acpi_fan_delete_attributes(device);
560 
561 	return result;
562 }
563 
564 static void acpi_fan_remove(struct platform_device *pdev)
565 {
566 	struct acpi_fan *fan = platform_get_drvdata(pdev);
567 
568 	if (fan->has_fst) {
569 		struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
570 
571 		acpi_fan_delete_attributes(device);
572 	}
573 	thermal_cooling_device_unregister(fan->cdev);
574 }
575 
576 #ifdef CONFIG_PM_SLEEP
577 static int acpi_fan_suspend(struct device *dev)
578 {
579 	struct acpi_fan *fan = dev_get_drvdata(dev);
580 	if (fan->acpi4)
581 		return 0;
582 
583 	acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
584 
585 	return AE_OK;
586 }
587 
588 static int acpi_fan_resume(struct device *dev)
589 {
590 	struct acpi_fan *fan = dev_get_drvdata(dev);
591 	int result;
592 
593 	if (fan->has_fst) {
594 		result = acpi_fan_dsm_start(dev);
595 		if (result)
596 			dev_err(dev, "Failed to start Microsoft fan extensions: %d\n", result);
597 	}
598 
599 	if (fan->acpi4)
600 		return 0;
601 
602 	result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
603 	if (result)
604 		dev_err(dev, "Error updating fan power state\n");
605 
606 	return result;
607 }
608 
609 static const struct dev_pm_ops acpi_fan_pm = {
610 	.resume = acpi_fan_resume,
611 	.freeze = acpi_fan_suspend,
612 	.thaw = acpi_fan_resume,
613 	.restore = acpi_fan_resume,
614 };
615 #define FAN_PM_OPS_PTR (&acpi_fan_pm)
616 
617 #else
618 
619 #define FAN_PM_OPS_PTR NULL
620 
621 #endif
622 
623 static struct platform_driver acpi_fan_driver = {
624 	.probe = acpi_fan_probe,
625 	.remove = acpi_fan_remove,
626 	.driver = {
627 		.name = "acpi-fan",
628 		.acpi_match_table = fan_device_ids,
629 		.pm = FAN_PM_OPS_PTR,
630 	},
631 };
632 
633 module_platform_driver(acpi_fan_driver);
634 
635 MODULE_AUTHOR("Paul Diefenbaugh");
636 MODULE_DESCRIPTION("ACPI Fan Driver");
637 MODULE_LICENSE("GPL");
638