xref: /linux/drivers/hwmon/dell-smm-hwmon.c (revision f2161d5f1aae21a42b0a64d87e10cb31db423f42)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
4  *
5  * Copyright (C) 2001  Massimo Dal Zotto <dz@debian.org>
6  *
7  * Hwmon integration:
8  * Copyright (C) 2011  Jean Delvare <jdelvare@suse.de>
9  * Copyright (C) 2013, 2014  Guenter Roeck <linux@roeck-us.net>
10  * Copyright (C) 2014, 2015  Pali Rohár <pali@kernel.org>
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/acpi.h>
16 #include <linux/capability.h>
17 #include <linux/cpu.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20 #include <linux/dmi.h>
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/hwmon.h>
24 #include <linux/init.h>
25 #include <linux/kconfig.h>
26 #include <linux/kernel.h>
27 #include <linux/minmax.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/platform_device.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/slab.h>
34 #include <linux/smp.h>
35 #include <linux/string.h>
36 #include <linux/thermal.h>
37 #include <linux/types.h>
38 #include <linux/uaccess.h>
39 #include <linux/wmi.h>
40 
41 #include <linux/i8k.h>
42 #include <linux/unaligned.h>
43 
44 #define I8K_SMM_FN_STATUS	0x0025
45 #define I8K_SMM_POWER_STATUS	0x0069
46 #define I8K_SMM_SET_FAN		0x01a3
47 #define I8K_SMM_GET_FAN		0x00a3
48 #define I8K_SMM_GET_SPEED	0x02a3
49 #define I8K_SMM_GET_FAN_TYPE	0x03a3
50 #define I8K_SMM_GET_NOM_SPEED	0x04a3
51 #define I8K_SMM_GET_TEMP	0x10a3
52 #define I8K_SMM_GET_TEMP_TYPE	0x11a3
53 #define I8K_SMM_GET_DELL_SIG1	0xfea3
54 #define I8K_SMM_GET_DELL_SIG2	0xffa3
55 
56 /* in usecs */
57 #define DELL_SMM_MAX_DURATION  250000
58 
59 #define I8K_FAN_MULT		30
60 #define I8K_FAN_RPM_THRESHOLD	1000
61 #define I8K_MAX_TEMP		127
62 
63 #define I8K_FN_NONE		0x00
64 #define I8K_FN_UP		0x01
65 #define I8K_FN_DOWN		0x02
66 #define I8K_FN_MUTE		0x04
67 #define I8K_FN_MASK		0x07
68 #define I8K_FN_SHIFT		8
69 
70 #define I8K_POWER_AC		0x05
71 #define I8K_POWER_BATTERY	0x01
72 
73 #define DELL_SMM_WMI_GUID	"F1DDEE52-063C-4784-A11E-8A06684B9B01"
74 #define DELL_SMM_LEGACY_EXECUTE	0x1
75 
76 #define DELL_SMM_NO_TEMP	10
77 #define DELL_SMM_NO_FANS	4
78 
79 /* limit fan multiplier to avoid overflow */
80 #define DELL_SMM_MAX_FAN_MULT (INT_MAX / U16_MAX)
81 
82 struct smm_regs {
83 	unsigned int eax;
84 	unsigned int ebx;
85 	unsigned int ecx;
86 	unsigned int edx;
87 	unsigned int esi;
88 	unsigned int edi;
89 };
90 
91 struct dell_smm_ops {
92 	struct device *smm_dev;
93 	int (*smm_call)(struct device *smm_dev, struct smm_regs *regs);
94 };
95 
96 struct dell_smm_data {
97 	struct mutex i8k_mutex; /* lock for sensors writes */
98 	char bios_version[4];
99 	char bios_machineid[16];
100 	uint i8k_fan_mult;
101 	uint i8k_pwm_mult;
102 	uint i8k_fan_max;
103 	int temp_type[DELL_SMM_NO_TEMP];
104 	bool fan[DELL_SMM_NO_FANS];
105 	int fan_type[DELL_SMM_NO_FANS];
106 	int *fan_nominal_speed[DELL_SMM_NO_FANS];
107 	const struct dell_smm_ops *ops;
108 };
109 
110 struct dell_smm_cooling_data {
111 	u8 fan_num;
112 	struct dell_smm_data *data;
113 };
114 
115 MODULE_AUTHOR("Massimo Dal Zotto <dz@debian.org>");
116 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
117 MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
118 MODULE_LICENSE("GPL");
119 MODULE_ALIAS("i8k");
120 
121 static bool force;
122 module_param_unsafe(force, bool, 0);
123 MODULE_PARM_DESC(force, "Force loading without checking for supported models and features");
124 
125 static bool ignore_dmi;
126 module_param(ignore_dmi, bool, 0);
127 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
128 
129 #if IS_ENABLED(CONFIG_I8K)
130 static bool restricted = true;
131 module_param(restricted, bool, 0);
132 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
133 
134 static bool power_status;
135 module_param(power_status, bool, 0600);
136 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
137 #endif
138 
139 static uint fan_mult;
140 module_param(fan_mult, uint, 0);
141 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
142 
143 static uint fan_max;
144 module_param(fan_max, uint, 0);
145 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
146 
147 static bool disallow_fan_type_call, disallow_fan_support;
148 
149 static unsigned int manual_fan, auto_fan;
150 
151 static const char * const temp_labels[] = {
152 	"CPU",
153 	"GPU",
154 	"SODIMM",
155 	"Other",
156 	"Ambient",
157 	"Other",
158 };
159 
160 static const char * const fan_labels[] = {
161 	"Processor Fan",
162 	"Motherboard Fan",
163 	"Video Fan",
164 	"Power Supply Fan",
165 	"Chipset Fan",
166 	"Other Fan",
167 };
168 
169 static const char * const docking_labels[] = {
170 	"Docking Processor Fan",
171 	"Docking Motherboard Fan",
172 	"Docking Video Fan",
173 	"Docking Power Supply Fan",
174 	"Docking Chipset Fan",
175 	"Docking Other Fan",
176 };
177 
i8k_get_dmi_data(int field)178 static inline const char __init *i8k_get_dmi_data(int field)
179 {
180 	const char *dmi_data = dmi_get_system_info(field);
181 
182 	return dmi_data && *dmi_data ? dmi_data : "?";
183 }
184 
185 /*
186  * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
187  */
i8k_smm_func(void * par)188 static int i8k_smm_func(void *par)
189 {
190 	struct smm_regs *regs = par;
191 	unsigned char carry;
192 
193 	/* SMM requires CPU 0 */
194 	if (smp_processor_id() != 0)
195 		return -EBUSY;
196 
197 	asm volatile("out %%al,$0xb2\n\t"
198 		     "out %%al,$0x84\n\t"
199 		     "setc %0\n"
200 		     : "=mr" (carry),
201 		       "+a" (regs->eax),
202 		       "+b" (regs->ebx),
203 		       "+c" (regs->ecx),
204 		       "+d" (regs->edx),
205 		       "+S" (regs->esi),
206 		       "+D" (regs->edi));
207 
208 	if (carry)
209 		return -EINVAL;
210 
211 	return 0;
212 }
213 
214 /*
215  * Call the System Management Mode BIOS.
216  */
i8k_smm_call(struct device * dummy,struct smm_regs * regs)217 static int i8k_smm_call(struct device *dummy, struct smm_regs *regs)
218 {
219 	int ret;
220 
221 	cpus_read_lock();
222 	ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
223 	cpus_read_unlock();
224 
225 	return ret;
226 }
227 
228 static const struct dell_smm_ops i8k_smm_ops = {
229 	.smm_call = i8k_smm_call,
230 };
231 
232 /*
233  * Call the System Management Mode BIOS over WMI.
234  */
wmi_parse_register(u8 * buffer,u32 length,unsigned int * reg)235 static ssize_t wmi_parse_register(u8 *buffer, u32 length, unsigned int *reg)
236 {
237 	__le32 value;
238 	u32 reg_size;
239 
240 	if (length <= sizeof(reg_size))
241 		return -ENODATA;
242 
243 	reg_size = get_unaligned_le32(buffer);
244 	if (!reg_size || reg_size > sizeof(value))
245 		return -ENOMSG;
246 
247 	if (length < sizeof(reg_size) + reg_size)
248 		return -ENODATA;
249 
250 	memcpy_and_pad(&value, sizeof(value), buffer + sizeof(reg_size), reg_size, 0);
251 	*reg = le32_to_cpu(value);
252 
253 	return reg_size + sizeof(reg_size);
254 }
255 
wmi_parse_response(u8 * buffer,u32 length,struct smm_regs * regs)256 static int wmi_parse_response(u8 *buffer, u32 length, struct smm_regs *regs)
257 {
258 	unsigned int *registers[] = {
259 		&regs->eax,
260 		&regs->ebx,
261 		&regs->ecx,
262 		&regs->edx
263 	};
264 	u32 offset = 0;
265 	ssize_t ret;
266 	int i;
267 
268 	for (i = 0; i < ARRAY_SIZE(registers); i++) {
269 		if (offset >= length)
270 			return -ENODATA;
271 
272 		ret = wmi_parse_register(buffer + offset, length - offset, registers[i]);
273 		if (ret < 0)
274 			return ret;
275 
276 		offset += ret;
277 	}
278 
279 	if (offset != length)
280 		return -ENOMSG;
281 
282 	return 0;
283 }
284 
wmi_smm_call(struct device * dev,struct smm_regs * regs)285 static int wmi_smm_call(struct device *dev, struct smm_regs *regs)
286 {
287 	struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
288 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
289 	u32 wmi_payload[] = {
290 		sizeof(regs->eax),
291 		regs->eax,
292 		sizeof(regs->ebx),
293 		regs->ebx,
294 		sizeof(regs->ecx),
295 		regs->ecx,
296 		sizeof(regs->edx),
297 		regs->edx
298 	};
299 	const struct acpi_buffer in = {
300 		.length = sizeof(wmi_payload),
301 		.pointer = &wmi_payload,
302 	};
303 	union acpi_object *obj;
304 	acpi_status status;
305 	int ret;
306 
307 	status = wmidev_evaluate_method(wdev, 0x0, DELL_SMM_LEGACY_EXECUTE, &in, &out);
308 	if (ACPI_FAILURE(status))
309 		return -EIO;
310 
311 	obj = out.pointer;
312 	if (!obj)
313 		return -ENODATA;
314 
315 	if (obj->type != ACPI_TYPE_BUFFER) {
316 		ret = -ENOMSG;
317 
318 		goto err_free;
319 	}
320 
321 	ret = wmi_parse_response(obj->buffer.pointer, obj->buffer.length, regs);
322 
323 err_free:
324 	kfree(obj);
325 
326 	return ret;
327 }
328 
dell_smm_call(const struct dell_smm_ops * ops,struct smm_regs * regs)329 static int dell_smm_call(const struct dell_smm_ops *ops, struct smm_regs *regs)
330 {
331 	unsigned int eax = regs->eax;
332 	unsigned int ebx = regs->ebx;
333 	long long duration;
334 	ktime_t calltime;
335 	int ret;
336 
337 	calltime = ktime_get();
338 	ret = ops->smm_call(ops->smm_dev, regs);
339 	duration = ktime_us_delta(ktime_get(), calltime);
340 
341 	pr_debug("SMM(0x%.4x 0x%.4x) = 0x%.4x status: %d (took %7lld usecs)\n",
342 		 eax, ebx, regs->eax & 0xffff, ret, duration);
343 
344 	if (duration > DELL_SMM_MAX_DURATION)
345 		pr_warn_once("SMM call took %lld usecs!\n", duration);
346 
347 	if (ret < 0)
348 		return ret;
349 
350 	if ((regs->eax & 0xffff) == 0xffff || regs->eax == eax)
351 		return -EINVAL;
352 
353 	return 0;
354 }
355 
356 /*
357  * Read the fan status.
358  */
i8k_get_fan_status(const struct dell_smm_data * data,u8 fan)359 static int i8k_get_fan_status(const struct dell_smm_data *data, u8 fan)
360 {
361 	struct smm_regs regs = {
362 		.eax = I8K_SMM_GET_FAN,
363 		.ebx = fan,
364 	};
365 
366 	if (disallow_fan_support)
367 		return -EINVAL;
368 
369 	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
370 }
371 
372 /*
373  * Read the fan speed in RPM.
374  */
i8k_get_fan_speed(const struct dell_smm_data * data,u8 fan)375 static int i8k_get_fan_speed(const struct dell_smm_data *data, u8 fan)
376 {
377 	struct smm_regs regs = {
378 		.eax = I8K_SMM_GET_SPEED,
379 		.ebx = fan,
380 	};
381 
382 	if (disallow_fan_support)
383 		return -EINVAL;
384 
385 	return dell_smm_call(data->ops, &regs) ? : (regs.eax & 0xffff) * data->i8k_fan_mult;
386 }
387 
388 /*
389  * Read the fan type.
390  */
_i8k_get_fan_type(const struct dell_smm_data * data,u8 fan)391 static int _i8k_get_fan_type(const struct dell_smm_data *data, u8 fan)
392 {
393 	struct smm_regs regs = {
394 		.eax = I8K_SMM_GET_FAN_TYPE,
395 		.ebx = fan,
396 	};
397 
398 	if (disallow_fan_support || disallow_fan_type_call)
399 		return -EINVAL;
400 
401 	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
402 }
403 
i8k_get_fan_type(struct dell_smm_data * data,u8 fan)404 static int i8k_get_fan_type(struct dell_smm_data *data, u8 fan)
405 {
406 	/* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
407 	if (data->fan_type[fan] == INT_MIN)
408 		data->fan_type[fan] = _i8k_get_fan_type(data, fan);
409 
410 	return data->fan_type[fan];
411 }
412 
413 /*
414  * Read the fan nominal rpm for specific fan speed.
415  */
i8k_get_fan_nominal_speed(const struct dell_smm_data * data,u8 fan,int speed)416 static int i8k_get_fan_nominal_speed(const struct dell_smm_data *data, u8 fan, int speed)
417 {
418 	struct smm_regs regs = {
419 		.eax = I8K_SMM_GET_NOM_SPEED,
420 		.ebx = fan | (speed << 8),
421 	};
422 
423 	if (disallow_fan_support)
424 		return -EINVAL;
425 
426 	return dell_smm_call(data->ops, &regs) ? : (regs.eax & 0xffff);
427 }
428 
429 /*
430  * Enable or disable automatic BIOS fan control support
431  */
i8k_enable_fan_auto_mode(const struct dell_smm_data * data,bool enable)432 static int i8k_enable_fan_auto_mode(const struct dell_smm_data *data, bool enable)
433 {
434 	struct smm_regs regs = { };
435 
436 	if (disallow_fan_support)
437 		return -EINVAL;
438 
439 	regs.eax = enable ? auto_fan : manual_fan;
440 	return dell_smm_call(data->ops, &regs);
441 }
442 
443 /*
444  * Set the fan speed (off, low, high, ...).
445  */
i8k_set_fan(const struct dell_smm_data * data,u8 fan,int speed)446 static int i8k_set_fan(const struct dell_smm_data *data, u8 fan, int speed)
447 {
448 	struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
449 
450 	if (disallow_fan_support)
451 		return -EINVAL;
452 
453 	regs.ebx = fan | (speed << 8);
454 
455 	return dell_smm_call(data->ops, &regs);
456 }
457 
i8k_get_temp_type(const struct dell_smm_data * data,u8 sensor)458 static int i8k_get_temp_type(const struct dell_smm_data *data, u8 sensor)
459 {
460 	struct smm_regs regs = {
461 		.eax = I8K_SMM_GET_TEMP_TYPE,
462 		.ebx = sensor,
463 	};
464 
465 	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
466 }
467 
468 /*
469  * Read the cpu temperature.
470  */
_i8k_get_temp(const struct dell_smm_data * data,u8 sensor)471 static int _i8k_get_temp(const struct dell_smm_data *data, u8 sensor)
472 {
473 	struct smm_regs regs = {
474 		.eax = I8K_SMM_GET_TEMP,
475 		.ebx = sensor,
476 	};
477 
478 	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
479 }
480 
i8k_get_temp(const struct dell_smm_data * data,u8 sensor)481 static int i8k_get_temp(const struct dell_smm_data *data, u8 sensor)
482 {
483 	int temp = _i8k_get_temp(data, sensor);
484 
485 	/*
486 	 * Sometimes the temperature sensor returns 0x99, which is out of range.
487 	 * In this case we retry (once) before returning an error.
488 	 # 1003655137 00000058 00005a4b
489 	 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
490 	 # 1003655139 00000054 00005c52
491 	 */
492 	if (temp == 0x99) {
493 		msleep(100);
494 		temp = _i8k_get_temp(data, sensor);
495 	}
496 	/*
497 	 * Return -ENODATA for all invalid temperatures.
498 	 *
499 	 * Known instances are the 0x99 value as seen above as well as
500 	 * 0xc1 (193), which may be returned when trying to read the GPU
501 	 * temperature if the system supports a GPU and it is currently
502 	 * turned off.
503 	 */
504 	if (temp > I8K_MAX_TEMP)
505 		return -ENODATA;
506 
507 	return temp;
508 }
509 
dell_smm_get_signature(const struct dell_smm_ops * ops,int req_fn)510 static int dell_smm_get_signature(const struct dell_smm_ops *ops, int req_fn)
511 {
512 	struct smm_regs regs = { .eax = req_fn, };
513 	int rc;
514 
515 	rc = dell_smm_call(ops, &regs);
516 	if (rc < 0)
517 		return rc;
518 
519 	return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
520 }
521 
522 #if IS_ENABLED(CONFIG_I8K)
523 
524 /*
525  * Read the Fn key status.
526  */
i8k_get_fn_status(const struct dell_smm_data * data)527 static int i8k_get_fn_status(const struct dell_smm_data *data)
528 {
529 	struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
530 	int rc;
531 
532 	rc = dell_smm_call(data->ops, &regs);
533 	if (rc < 0)
534 		return rc;
535 
536 	switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
537 	case I8K_FN_UP:
538 		return I8K_VOL_UP;
539 	case I8K_FN_DOWN:
540 		return I8K_VOL_DOWN;
541 	case I8K_FN_MUTE:
542 		return I8K_VOL_MUTE;
543 	default:
544 		return 0;
545 	}
546 }
547 
548 /*
549  * Read the power status.
550  */
i8k_get_power_status(const struct dell_smm_data * data)551 static int i8k_get_power_status(const struct dell_smm_data *data)
552 {
553 	struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
554 	int rc;
555 
556 	rc = dell_smm_call(data->ops, &regs);
557 	if (rc < 0)
558 		return rc;
559 
560 	return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
561 }
562 
563 /*
564  * Procfs interface
565  */
566 
i8k_ioctl(struct file * fp,unsigned int cmd,unsigned long arg)567 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
568 {
569 	struct dell_smm_data *data = pde_data(file_inode(fp));
570 	int __user *argp = (int __user *)arg;
571 	int speed, err;
572 	int val = 0;
573 
574 	if (!argp)
575 		return -EINVAL;
576 
577 	switch (cmd) {
578 	case I8K_BIOS_VERSION:
579 		if (!isdigit(data->bios_version[0]) || !isdigit(data->bios_version[1]) ||
580 		    !isdigit(data->bios_version[2]))
581 			return -EINVAL;
582 
583 		val = (data->bios_version[0] << 16) |
584 				(data->bios_version[1] << 8) | data->bios_version[2];
585 
586 		if (copy_to_user(argp, &val, sizeof(val)))
587 			return -EFAULT;
588 
589 		return 0;
590 	case I8K_MACHINE_ID:
591 		if (restricted && !capable(CAP_SYS_ADMIN))
592 			return -EPERM;
593 
594 		if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid)))
595 			return -EFAULT;
596 
597 		return 0;
598 	case I8K_FN_STATUS:
599 		val = i8k_get_fn_status(data);
600 		break;
601 
602 	case I8K_POWER_STATUS:
603 		val = i8k_get_power_status(data);
604 		break;
605 
606 	case I8K_GET_TEMP:
607 		val = i8k_get_temp(data, 0);
608 		break;
609 
610 	case I8K_GET_SPEED:
611 		if (copy_from_user(&val, argp, sizeof(int)))
612 			return -EFAULT;
613 
614 		if (val > U8_MAX || val < 0)
615 			return -EINVAL;
616 
617 		val = i8k_get_fan_speed(data, val);
618 		break;
619 
620 	case I8K_GET_FAN:
621 		if (copy_from_user(&val, argp, sizeof(int)))
622 			return -EFAULT;
623 
624 		if (val > U8_MAX || val < 0)
625 			return -EINVAL;
626 
627 		val = i8k_get_fan_status(data, val);
628 		break;
629 
630 	case I8K_SET_FAN:
631 		if (restricted && !capable(CAP_SYS_ADMIN))
632 			return -EPERM;
633 
634 		if (copy_from_user(&val, argp, sizeof(int)))
635 			return -EFAULT;
636 
637 		if (val > U8_MAX || val < 0)
638 			return -EINVAL;
639 
640 		if (copy_from_user(&speed, argp + 1, sizeof(int)))
641 			return -EFAULT;
642 
643 		speed = clamp_val(speed, 0, data->i8k_fan_max);
644 
645 		mutex_lock(&data->i8k_mutex);
646 		err = i8k_set_fan(data, val, speed);
647 		if (err < 0)
648 			val = err;
649 		else
650 			val = i8k_get_fan_status(data, val);
651 		mutex_unlock(&data->i8k_mutex);
652 		break;
653 
654 	default:
655 		return -ENOIOCTLCMD;
656 	}
657 
658 	if (val < 0)
659 		return val;
660 
661 	if (copy_to_user(argp, &val, sizeof(int)))
662 		return -EFAULT;
663 
664 	return 0;
665 }
666 
667 /*
668  * Print the information for /proc/i8k.
669  */
i8k_proc_show(struct seq_file * seq,void * offset)670 static int i8k_proc_show(struct seq_file *seq, void *offset)
671 {
672 	struct dell_smm_data *data = seq->private;
673 	int fn_key, cpu_temp, ac_power;
674 	int left_fan, right_fan, left_speed, right_speed;
675 
676 	cpu_temp	= i8k_get_temp(data, 0);			/* 11100 µs */
677 	left_fan	= i8k_get_fan_status(data, I8K_FAN_LEFT);	/*   580 µs */
678 	right_fan	= i8k_get_fan_status(data, I8K_FAN_RIGHT);	/*   580 µs */
679 	left_speed	= i8k_get_fan_speed(data, I8K_FAN_LEFT);	/*   580 µs */
680 	right_speed	= i8k_get_fan_speed(data, I8K_FAN_RIGHT);	/*   580 µs */
681 	fn_key		= i8k_get_fn_status(data);			/*   750 µs */
682 	if (power_status)
683 		ac_power = i8k_get_power_status(data);			/* 14700 µs */
684 	else
685 		ac_power = -1;
686 
687 	/*
688 	 * Info:
689 	 *
690 	 * 1)  Format version (this will change if format changes)
691 	 * 2)  BIOS version
692 	 * 3)  BIOS machine ID
693 	 * 4)  Cpu temperature
694 	 * 5)  Left fan status
695 	 * 6)  Right fan status
696 	 * 7)  Left fan speed
697 	 * 8)  Right fan speed
698 	 * 9)  AC power
699 	 * 10) Fn Key status
700 	 */
701 	seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
702 		   I8K_PROC_FMT,
703 		   data->bios_version,
704 		   (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : data->bios_machineid,
705 		   cpu_temp,
706 		   left_fan, right_fan, left_speed, right_speed,
707 		   ac_power, fn_key);
708 
709 	return 0;
710 }
711 
i8k_open_fs(struct inode * inode,struct file * file)712 static int i8k_open_fs(struct inode *inode, struct file *file)
713 {
714 	return single_open(file, i8k_proc_show, pde_data(inode));
715 }
716 
717 static const struct proc_ops i8k_proc_ops = {
718 	.proc_open	= i8k_open_fs,
719 	.proc_read	= seq_read,
720 	.proc_lseek	= seq_lseek,
721 	.proc_release	= single_release,
722 	.proc_ioctl	= i8k_ioctl,
723 };
724 
i8k_exit_procfs(void * param)725 static void i8k_exit_procfs(void *param)
726 {
727 	remove_proc_entry("i8k", NULL);
728 }
729 
i8k_init_procfs(struct device * dev)730 static void __init i8k_init_procfs(struct device *dev)
731 {
732 	struct dell_smm_data *data = dev_get_drvdata(dev);
733 
734 	strscpy(data->bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
735 		sizeof(data->bios_version));
736 	strscpy(data->bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
737 		sizeof(data->bios_machineid));
738 
739 	/* Only register exit function if creation was successful */
740 	if (proc_create_data("i8k", 0, NULL, &i8k_proc_ops, data))
741 		devm_add_action_or_reset(dev, i8k_exit_procfs, NULL);
742 }
743 
744 #else
745 
i8k_init_procfs(struct device * dev)746 static void __init i8k_init_procfs(struct device *dev)
747 {
748 }
749 
750 #endif
751 
dell_smm_get_max_state(struct thermal_cooling_device * dev,unsigned long * state)752 static int dell_smm_get_max_state(struct thermal_cooling_device *dev, unsigned long *state)
753 {
754 	struct dell_smm_cooling_data *cdata = dev->devdata;
755 
756 	*state = cdata->data->i8k_fan_max;
757 
758 	return 0;
759 }
760 
dell_smm_get_cur_state(struct thermal_cooling_device * dev,unsigned long * state)761 static int dell_smm_get_cur_state(struct thermal_cooling_device *dev, unsigned long *state)
762 {
763 	struct dell_smm_cooling_data *cdata = dev->devdata;
764 	int ret;
765 
766 	ret = i8k_get_fan_status(cdata->data, cdata->fan_num);
767 	if (ret < 0)
768 		return ret;
769 
770 	/*
771 	 * A fan state bigger than i8k_fan_max might indicate that
772 	 * the fan is currently in automatic mode.
773 	 */
774 	if (ret > cdata->data->i8k_fan_max)
775 		return -ENODATA;
776 
777 	*state = ret;
778 
779 	return 0;
780 }
781 
dell_smm_set_cur_state(struct thermal_cooling_device * dev,unsigned long state)782 static int dell_smm_set_cur_state(struct thermal_cooling_device *dev, unsigned long state)
783 {
784 	struct dell_smm_cooling_data *cdata = dev->devdata;
785 	struct dell_smm_data *data = cdata->data;
786 	int ret;
787 
788 	if (state > data->i8k_fan_max)
789 		return -EINVAL;
790 
791 	mutex_lock(&data->i8k_mutex);
792 	ret = i8k_set_fan(data, cdata->fan_num, (int)state);
793 	mutex_unlock(&data->i8k_mutex);
794 
795 	return ret;
796 }
797 
798 static const struct thermal_cooling_device_ops dell_smm_cooling_ops = {
799 	.get_max_state = dell_smm_get_max_state,
800 	.get_cur_state = dell_smm_get_cur_state,
801 	.set_cur_state = dell_smm_set_cur_state,
802 };
803 
dell_smm_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)804 static umode_t dell_smm_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
805 				   int channel)
806 {
807 	const struct dell_smm_data *data = drvdata;
808 
809 	switch (type) {
810 	case hwmon_temp:
811 		switch (attr) {
812 		case hwmon_temp_input:
813 			/* _i8k_get_temp() is fine since we do not care about the actual value */
814 			if (data->temp_type[channel] >= 0 || _i8k_get_temp(data, channel) >= 0)
815 				return 0444;
816 
817 			break;
818 		case hwmon_temp_label:
819 			if (data->temp_type[channel] >= 0)
820 				return 0444;
821 
822 			break;
823 		default:
824 			break;
825 		}
826 		break;
827 	case hwmon_fan:
828 		if (disallow_fan_support)
829 			break;
830 
831 		switch (attr) {
832 		case hwmon_fan_input:
833 			if (data->fan[channel])
834 				return 0444;
835 
836 			break;
837 		case hwmon_fan_label:
838 			if (data->fan[channel] && !disallow_fan_type_call)
839 				return 0444;
840 
841 			break;
842 		case hwmon_fan_min:
843 		case hwmon_fan_max:
844 		case hwmon_fan_target:
845 			if (data->fan_nominal_speed[channel])
846 				return 0444;
847 
848 			break;
849 		default:
850 			break;
851 		}
852 		break;
853 	case hwmon_pwm:
854 		if (disallow_fan_support)
855 			break;
856 
857 		switch (attr) {
858 		case hwmon_pwm_input:
859 			if (data->fan[channel])
860 				return 0644;
861 
862 			break;
863 		case hwmon_pwm_enable:
864 			if (auto_fan) {
865 				/*
866 				 * The setting affects all fans, so only create a
867 				 * single attribute for the first fan channel.
868 				 */
869 				if (channel != 0)
870 					return 0;
871 
872 				/*
873 				 * There is no command for retrieve the current status
874 				 * from BIOS, and userspace/firmware itself can change
875 				 * it.
876 				 * Thus we can only provide write-only access for now.
877 				 */
878 				return 0200;
879 			}
880 
881 			if (data->fan[channel] && data->i8k_fan_max < I8K_FAN_AUTO)
882 				return 0644;
883 
884 			break;
885 		default:
886 			break;
887 		}
888 		break;
889 	default:
890 		break;
891 	}
892 
893 	return 0;
894 }
895 
dell_smm_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)896 static int dell_smm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
897 			 long *val)
898 {
899 	struct dell_smm_data *data = dev_get_drvdata(dev);
900 	int mult = data->i8k_fan_mult;
901 	int ret;
902 
903 	switch (type) {
904 	case hwmon_temp:
905 		switch (attr) {
906 		case hwmon_temp_input:
907 			ret = i8k_get_temp(data, channel);
908 			if (ret < 0)
909 				return ret;
910 
911 			*val = ret * 1000;
912 
913 			return 0;
914 		default:
915 			break;
916 		}
917 		break;
918 	case hwmon_fan:
919 		switch (attr) {
920 		case hwmon_fan_input:
921 			ret = i8k_get_fan_speed(data, channel);
922 			if (ret < 0)
923 				return ret;
924 
925 			*val = ret;
926 
927 			return 0;
928 		case hwmon_fan_min:
929 			*val = data->fan_nominal_speed[channel][0] * mult;
930 
931 			return 0;
932 		case hwmon_fan_max:
933 			*val = data->fan_nominal_speed[channel][data->i8k_fan_max] * mult;
934 
935 			return 0;
936 		case hwmon_fan_target:
937 			ret = i8k_get_fan_status(data, channel);
938 			if (ret < 0)
939 				return ret;
940 
941 			if (ret > data->i8k_fan_max)
942 				ret = data->i8k_fan_max;
943 
944 			*val = data->fan_nominal_speed[channel][ret] * mult;
945 
946 			return 0;
947 		default:
948 			break;
949 		}
950 		break;
951 	case hwmon_pwm:
952 		ret = i8k_get_fan_status(data, channel);
953 		if (ret < 0)
954 			return ret;
955 
956 		switch (attr) {
957 		case hwmon_pwm_input:
958 			/*
959 			 * A fan state bigger than i8k_fan_max might indicate that
960 			 * the fan is currently in automatic mode.
961 			 */
962 			if (ret > data->i8k_fan_max)
963 				return -ENODATA;
964 
965 			*val = clamp_val(ret * data->i8k_pwm_mult, 0, 255);
966 
967 			return 0;
968 		case hwmon_pwm_enable:
969 			if (ret == I8K_FAN_AUTO)
970 				*val = 2;
971 			else
972 				*val = 1;
973 
974 			return 0;
975 		default:
976 			break;
977 		}
978 		break;
979 	default:
980 		break;
981 	}
982 
983 	return -EOPNOTSUPP;
984 }
985 
dell_smm_fan_label(struct dell_smm_data * data,int channel)986 static const char *dell_smm_fan_label(struct dell_smm_data *data, int channel)
987 {
988 	bool dock = false;
989 	int type = i8k_get_fan_type(data, channel);
990 
991 	if (type < 0)
992 		return ERR_PTR(type);
993 
994 	if (type & 0x10) {
995 		dock = true;
996 		type &= 0x0F;
997 	}
998 
999 	if (type >= ARRAY_SIZE(fan_labels))
1000 		type = ARRAY_SIZE(fan_labels) - 1;
1001 
1002 	return dock ? docking_labels[type] : fan_labels[type];
1003 }
1004 
dell_smm_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)1005 static int dell_smm_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
1006 				int channel, const char **str)
1007 {
1008 	struct dell_smm_data *data = dev_get_drvdata(dev);
1009 
1010 	switch (type) {
1011 	case hwmon_temp:
1012 		switch (attr) {
1013 		case hwmon_temp_label:
1014 			*str = temp_labels[data->temp_type[channel]];
1015 			return 0;
1016 		default:
1017 			break;
1018 		}
1019 		break;
1020 	case hwmon_fan:
1021 		switch (attr) {
1022 		case hwmon_fan_label:
1023 			*str = dell_smm_fan_label(data, channel);
1024 			return PTR_ERR_OR_ZERO(*str);
1025 		default:
1026 			break;
1027 		}
1028 		break;
1029 	default:
1030 		break;
1031 	}
1032 
1033 	return -EOPNOTSUPP;
1034 }
1035 
dell_smm_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)1036 static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
1037 			  long val)
1038 {
1039 	struct dell_smm_data *data = dev_get_drvdata(dev);
1040 	unsigned long pwm;
1041 	bool enable;
1042 	int err;
1043 
1044 	switch (type) {
1045 	case hwmon_pwm:
1046 		switch (attr) {
1047 		case hwmon_pwm_input:
1048 			pwm = clamp_val(DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult), 0,
1049 					data->i8k_fan_max);
1050 
1051 			mutex_lock(&data->i8k_mutex);
1052 			err = i8k_set_fan(data, channel, pwm);
1053 			mutex_unlock(&data->i8k_mutex);
1054 
1055 			if (err < 0)
1056 				return err;
1057 
1058 			return 0;
1059 		case hwmon_pwm_enable:
1060 			switch (val) {
1061 			case 1:
1062 				enable = false;
1063 				break;
1064 			case 2:
1065 				enable = true;
1066 				break;
1067 			default:
1068 				return -EINVAL;
1069 			}
1070 
1071 			mutex_lock(&data->i8k_mutex);
1072 			if (auto_fan) {
1073 				err = i8k_enable_fan_auto_mode(data, enable);
1074 			} else {
1075 				/*
1076 				 * When putting the fan into manual control mode we have to ensure
1077 				 * that the device does not overheat until the userspace fan control
1078 				 * software takes over. Because of this we set the fan speed to
1079 				 * i8k_fan_max when disabling automatic fan control.
1080 				 */
1081 				if (enable)
1082 					err = i8k_set_fan(data, channel, I8K_FAN_AUTO);
1083 				else
1084 					err = i8k_set_fan(data, channel, data->i8k_fan_max);
1085 			}
1086 			mutex_unlock(&data->i8k_mutex);
1087 
1088 			if (err < 0)
1089 				return err;
1090 
1091 			return 0;
1092 		default:
1093 			break;
1094 		}
1095 		break;
1096 	default:
1097 		break;
1098 	}
1099 
1100 	return -EOPNOTSUPP;
1101 }
1102 
1103 static const struct hwmon_ops dell_smm_ops = {
1104 	.is_visible = dell_smm_is_visible,
1105 	.read = dell_smm_read,
1106 	.read_string = dell_smm_read_string,
1107 	.write = dell_smm_write,
1108 };
1109 
1110 static const struct hwmon_channel_info * const dell_smm_info[] = {
1111 	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
1112 	HWMON_CHANNEL_INFO(temp,
1113 			   HWMON_T_INPUT | HWMON_T_LABEL,
1114 			   HWMON_T_INPUT | HWMON_T_LABEL,
1115 			   HWMON_T_INPUT | HWMON_T_LABEL,
1116 			   HWMON_T_INPUT | HWMON_T_LABEL,
1117 			   HWMON_T_INPUT | HWMON_T_LABEL,
1118 			   HWMON_T_INPUT | HWMON_T_LABEL,
1119 			   HWMON_T_INPUT | HWMON_T_LABEL,
1120 			   HWMON_T_INPUT | HWMON_T_LABEL,
1121 			   HWMON_T_INPUT | HWMON_T_LABEL,
1122 			   HWMON_T_INPUT | HWMON_T_LABEL
1123 			   ),
1124 	HWMON_CHANNEL_INFO(fan,
1125 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1126 			   HWMON_F_TARGET,
1127 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1128 			   HWMON_F_TARGET,
1129 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1130 			   HWMON_F_TARGET,
1131 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1132 			   HWMON_F_TARGET
1133 			   ),
1134 	HWMON_CHANNEL_INFO(pwm,
1135 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1136 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1137 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1138 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE
1139 			   ),
1140 	NULL
1141 };
1142 
1143 static const struct hwmon_chip_info dell_smm_chip_info = {
1144 	.ops = &dell_smm_ops,
1145 	.info = dell_smm_info,
1146 };
1147 
dell_smm_init_cdev(struct device * dev,u8 fan_num)1148 static int dell_smm_init_cdev(struct device *dev, u8 fan_num)
1149 {
1150 	struct dell_smm_data *data = dev_get_drvdata(dev);
1151 	struct thermal_cooling_device *cdev;
1152 	struct dell_smm_cooling_data *cdata;
1153 	int ret = 0;
1154 	char *name;
1155 
1156 	name = kasprintf(GFP_KERNEL, "dell-smm-fan%u", fan_num + 1);
1157 	if (!name)
1158 		return -ENOMEM;
1159 
1160 	cdata = devm_kmalloc(dev, sizeof(*cdata), GFP_KERNEL);
1161 	if (cdata) {
1162 		cdata->fan_num = fan_num;
1163 		cdata->data = data;
1164 		cdev = devm_thermal_of_cooling_device_register(dev, NULL, name, cdata,
1165 							       &dell_smm_cooling_ops);
1166 		if (IS_ERR(cdev)) {
1167 			devm_kfree(dev, cdata);
1168 			ret = PTR_ERR(cdev);
1169 		}
1170 	} else {
1171 		ret = -ENOMEM;
1172 	}
1173 
1174 	kfree(name);
1175 
1176 	return ret;
1177 }
1178 
dell_smm_init_hwmon(struct device * dev)1179 static int dell_smm_init_hwmon(struct device *dev)
1180 {
1181 	struct dell_smm_data *data = dev_get_drvdata(dev);
1182 	struct device *dell_smm_hwmon_dev;
1183 	int state, err;
1184 	u8 i;
1185 
1186 	for (i = 0; i < DELL_SMM_NO_TEMP; i++) {
1187 		data->temp_type[i] = i8k_get_temp_type(data, i);
1188 		if (data->temp_type[i] < 0)
1189 			continue;
1190 
1191 		if (data->temp_type[i] >= ARRAY_SIZE(temp_labels))
1192 			data->temp_type[i] = ARRAY_SIZE(temp_labels) - 1;
1193 	}
1194 
1195 	for (i = 0; i < DELL_SMM_NO_FANS; i++) {
1196 		data->fan_type[i] = INT_MIN;
1197 		err = i8k_get_fan_status(data, i);
1198 		if (err < 0)
1199 			err = i8k_get_fan_type(data, i);
1200 
1201 		if (err < 0)
1202 			continue;
1203 
1204 		data->fan[i] = true;
1205 
1206 		/* the cooling device is not critical, ignore failures */
1207 		if (IS_REACHABLE(CONFIG_THERMAL)) {
1208 			err = dell_smm_init_cdev(dev, i);
1209 			if (err < 0)
1210 				dev_warn(dev, "Failed to register cooling device for fan %u\n",
1211 					 i + 1);
1212 		}
1213 
1214 		data->fan_nominal_speed[i] = devm_kmalloc_array(dev, data->i8k_fan_max + 1,
1215 								sizeof(*data->fan_nominal_speed[i]),
1216 								GFP_KERNEL);
1217 		if (!data->fan_nominal_speed[i])
1218 			continue;
1219 
1220 		for (state = 0; state <= data->i8k_fan_max; state++) {
1221 			err = i8k_get_fan_nominal_speed(data, i, state);
1222 			if (err < 0) {
1223 				/* Mark nominal speed table as invalid in case of error */
1224 				devm_kfree(dev, data->fan_nominal_speed[i]);
1225 				data->fan_nominal_speed[i] = NULL;
1226 				break;
1227 			}
1228 			data->fan_nominal_speed[i][state] = err;
1229 			/*
1230 			 * Autodetect fan multiplier based on nominal rpm if multiplier
1231 			 * was not specified as module param or in DMI. If fan reports
1232 			 * rpm value too high then set multiplier to 1.
1233 			 */
1234 			if (!fan_mult && err > I8K_FAN_RPM_THRESHOLD)
1235 				data->i8k_fan_mult = 1;
1236 		}
1237 	}
1238 
1239 	dell_smm_hwmon_dev = devm_hwmon_device_register_with_info(dev, "dell_smm", data,
1240 								  &dell_smm_chip_info, NULL);
1241 
1242 	return PTR_ERR_OR_ZERO(dell_smm_hwmon_dev);
1243 }
1244 
dell_smm_init_data(struct device * dev,const struct dell_smm_ops * ops)1245 static int dell_smm_init_data(struct device *dev, const struct dell_smm_ops *ops)
1246 {
1247 	struct dell_smm_data *data;
1248 
1249 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1250 	if (!data)
1251 		return -ENOMEM;
1252 
1253 	mutex_init(&data->i8k_mutex);
1254 	dev_set_drvdata(dev, data);
1255 
1256 	data->ops = ops;
1257 	/* All options must not be 0 */
1258 	data->i8k_fan_mult = fan_mult ? : I8K_FAN_MULT;
1259 	if (data->i8k_fan_mult > DELL_SMM_MAX_FAN_MULT) {
1260 		dev_err(dev,
1261 			"fan multiplier %u is too large (max %u)\n",
1262 			data->i8k_fan_mult, DELL_SMM_MAX_FAN_MULT);
1263 		return -EINVAL;
1264 	}
1265 	data->i8k_fan_max = fan_max ? : I8K_FAN_HIGH;
1266 	data->i8k_pwm_mult = DIV_ROUND_UP(255, data->i8k_fan_max);
1267 
1268 	return 0;
1269 }
1270 
1271 static const struct dmi_system_id i8k_dmi_table[] __initconst = {
1272 	{
1273 		.ident = "Dell G5 5590",
1274 		.matches = {
1275 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1276 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5590"),
1277 		},
1278 	},
1279 	{
1280 		.ident = "Dell G5 5505",
1281 		.matches = {
1282 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1283 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5505"),
1284 		},
1285 	},
1286 	{
1287 		.ident = "Dell Inspiron",
1288 		.matches = {
1289 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1290 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1291 		},
1292 	},
1293 	{
1294 		.ident = "Dell Latitude",
1295 		.matches = {
1296 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1297 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1298 		},
1299 	},
1300 	{
1301 		.ident = "Dell Inspiron 2",
1302 		.matches = {
1303 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1304 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1305 		},
1306 	},
1307 	{
1308 		.ident = "Dell Latitude 2",
1309 		.matches = {
1310 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1311 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1312 		},
1313 	},
1314 	{	/* UK Inspiron 6400  */
1315 		.ident = "Dell Inspiron 3",
1316 		.matches = {
1317 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1318 			DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
1319 		},
1320 	},
1321 	{
1322 		.ident = "Dell Inspiron 3",
1323 		.matches = {
1324 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1325 			DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
1326 		},
1327 	},
1328 	{
1329 		.ident = "Dell OptiPlex 7060",
1330 		.matches = {
1331 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1332 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7060"),
1333 		},
1334 	},
1335 	{
1336 		.ident = "Dell OptiPlex 7050",
1337 		.matches = {
1338 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1339 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7050"),
1340 		},
1341 	},
1342 	{
1343 		.ident = "Dell OptiPlex 7040",
1344 		.matches = {
1345 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1346 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7040"),
1347 		},
1348 	},
1349 	{
1350 		.ident = "Dell Precision",
1351 		.matches = {
1352 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1353 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
1354 		},
1355 	},
1356 	{
1357 		.ident = "Dell Vostro",
1358 		.matches = {
1359 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1360 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
1361 		},
1362 	},
1363 	{
1364 		.ident = "Dell Studio",
1365 		.matches = {
1366 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1367 			DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1368 		},
1369 	},
1370 	{
1371 		.ident = "Dell XPS M140",
1372 		.matches = {
1373 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1374 			DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1375 		},
1376 	},
1377 	{
1378 		.ident = "Dell XPS",
1379 		.matches = {
1380 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1381 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS"),
1382 		},
1383 	},
1384 	{ }
1385 };
1386 
1387 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1388 
1389 /*
1390  * Only use for machines which need some special configuration
1391  * in order to work correctly (e.g. if autoconfig fails on this machines).
1392  */
1393 struct i8k_config_data {
1394 	uint fan_mult;
1395 	uint fan_max;
1396 };
1397 
1398 enum i8k_configs {
1399 	DELL_LATITUDE_D520,
1400 	DELL_STUDIO,
1401 	DELL_XPS,
1402 };
1403 
1404 static const struct i8k_config_data i8k_config_data[] __initconst = {
1405 	[DELL_LATITUDE_D520] = {
1406 		.fan_mult = 1,
1407 		.fan_max = I8K_FAN_TURBO,
1408 	},
1409 	[DELL_STUDIO] = {
1410 		.fan_mult = 1,
1411 		.fan_max = I8K_FAN_HIGH,
1412 	},
1413 	[DELL_XPS] = {
1414 		.fan_mult = 1,
1415 		.fan_max = I8K_FAN_HIGH,
1416 	},
1417 };
1418 
1419 static const struct dmi_system_id i8k_config_dmi_table[] __initconst = {
1420 	{
1421 		.ident = "Dell Latitude D520",
1422 		.matches = {
1423 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1424 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
1425 		},
1426 		.driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
1427 	},
1428 	{
1429 		.ident = "Dell Studio",
1430 		.matches = {
1431 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1432 			DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1433 		},
1434 		.driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1435 	},
1436 	{
1437 		.ident = "Dell XPS M140",
1438 		.matches = {
1439 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1440 			DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1441 		},
1442 		.driver_data = (void *)&i8k_config_data[DELL_XPS],
1443 	},
1444 	{ }
1445 };
1446 
1447 /*
1448  * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1449  * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1450  * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1451  * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1452  */
1453 static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1454 	{
1455 		.ident = "Dell Studio XPS 8000",
1456 		.matches = {
1457 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1458 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1459 		},
1460 	},
1461 	{
1462 		.ident = "Dell Studio XPS 8100",
1463 		.matches = {
1464 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1465 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1466 		},
1467 	},
1468 	{
1469 		.ident = "Dell Inspiron 580",
1470 		.matches = {
1471 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1472 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1473 		},
1474 	},
1475 	{
1476 		.ident = "Dell Inspiron 3505",
1477 		.matches = {
1478 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1479 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 3505"),
1480 		},
1481 	},
1482 	{ }
1483 };
1484 
1485 /*
1486  * On some machines all fan related SMM functions implemented by Dell BIOS
1487  * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan
1488  * support for affected blacklisted Dell machines stay disabled.
1489  * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751
1490  */
1491 static const struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initconst = {
1492 	{
1493 		.ident = "Dell Inspiron 7720",
1494 		.matches = {
1495 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1496 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1497 		},
1498 	},
1499 	{
1500 		.ident = "Dell Vostro 3360",
1501 		.matches = {
1502 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1503 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1504 		},
1505 	},
1506 	{
1507 		.ident = "Dell XPS13 9333",
1508 		.matches = {
1509 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1510 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1511 		},
1512 	},
1513 	{
1514 		.ident = "Dell XPS 15 L502X",
1515 		.matches = {
1516 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1517 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"),
1518 		},
1519 	},
1520 	{ }
1521 };
1522 
1523 struct i8k_fan_control_data {
1524 	unsigned int manual_fan;
1525 	unsigned int auto_fan;
1526 };
1527 
1528 enum i8k_fan_controls {
1529 	I8K_FAN_30A3_31A3,
1530 	I8K_FAN_34A3_35A3,
1531 };
1532 
1533 static const struct i8k_fan_control_data i8k_fan_control_data[] __initconst = {
1534 	[I8K_FAN_30A3_31A3] = {
1535 		.manual_fan = 0x30a3,
1536 		.auto_fan = 0x31a3,
1537 	},
1538 	[I8K_FAN_34A3_35A3] = {
1539 		.manual_fan = 0x34a3,
1540 		.auto_fan = 0x35a3,
1541 	},
1542 };
1543 
1544 static const struct dmi_system_id i8k_whitelist_fan_control[] __initconst = {
1545 	{
1546 		.ident = "Dell G5 5505",
1547 		.matches = {
1548 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1549 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5505"),
1550 
1551 		},
1552 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1553 	},
1554 	{
1555 		.ident = "Dell Latitude 5480",
1556 		.matches = {
1557 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1558 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"),
1559 		},
1560 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1561 	},
1562 	{
1563 		.ident = "Dell Latitude 7320",
1564 		.matches = {
1565 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1566 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 7320"),
1567 		},
1568 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1569 	},
1570 	{
1571 		.ident = "Dell Latitude E6440",
1572 		.matches = {
1573 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1574 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
1575 		},
1576 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1577 	},
1578 	{
1579 		.ident = "Dell Latitude E7440",
1580 		.matches = {
1581 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1582 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E7440"),
1583 		},
1584 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1585 	},
1586 	{
1587 		.ident = "Dell Precision 5530",
1588 		.matches = {
1589 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1590 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"),
1591 		},
1592 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1593 	},
1594 	{
1595 		.ident = "Dell Precision 7510",
1596 		.matches = {
1597 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1598 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7510"),
1599 		},
1600 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1601 	},
1602 	{
1603 		.ident = "Dell Precision 7540",
1604 		.matches = {
1605 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1606 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7540"),
1607 		},
1608 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1609 	},
1610 	{
1611 		.ident = "Dell XPS 13 7390",
1612 		.matches = {
1613 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1614 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 7390"),
1615 		},
1616 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1617 	},
1618 	{
1619 		.ident = "Dell XPS 13 9370",
1620 		.matches = {
1621 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1622 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
1623 		},
1624 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1625 	},
1626 	{
1627 		.ident = "Dell Optiplex 7000",
1628 		.matches = {
1629 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1630 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7000"),
1631 		},
1632 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1633 	},
1634 	{
1635 		.ident = "Dell XPS 9315",
1636 		.matches = {
1637 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1638 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 9315"),
1639 		},
1640 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1641 	},
1642 	{
1643 		.ident = "Dell G15 5511",
1644 		.matches = {
1645 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1646 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell G15 5511"),
1647 		},
1648 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1649 	},
1650 	{ }
1651 };
1652 
1653 /*
1654  * Legacy SMM backend driver.
1655  */
dell_smm_probe(struct platform_device * pdev)1656 static int __init dell_smm_probe(struct platform_device *pdev)
1657 {
1658 	int ret;
1659 
1660 	ret = dell_smm_init_data(&pdev->dev, &i8k_smm_ops);
1661 	if (ret < 0)
1662 		return ret;
1663 
1664 	ret = dell_smm_init_hwmon(&pdev->dev);
1665 	if (ret)
1666 		return ret;
1667 
1668 	i8k_init_procfs(&pdev->dev);
1669 
1670 	return 0;
1671 }
1672 
1673 static struct platform_driver dell_smm_driver = {
1674 	.driver		= {
1675 		.name	= KBUILD_MODNAME,
1676 	},
1677 };
1678 
1679 static struct platform_device *dell_smm_device;
1680 
1681 /*
1682  * WMI SMM backend driver.
1683  */
dell_smm_wmi_probe(struct wmi_device * wdev,const void * context)1684 static int dell_smm_wmi_probe(struct wmi_device *wdev, const void *context)
1685 {
1686 	struct dell_smm_ops *ops;
1687 	int ret;
1688 
1689 	ops = devm_kzalloc(&wdev->dev, sizeof(*ops), GFP_KERNEL);
1690 	if (!ops)
1691 		return -ENOMEM;
1692 
1693 	ops->smm_call = wmi_smm_call;
1694 	ops->smm_dev = &wdev->dev;
1695 
1696 	if (dell_smm_get_signature(ops, I8K_SMM_GET_DELL_SIG1) &&
1697 	    dell_smm_get_signature(ops, I8K_SMM_GET_DELL_SIG2))
1698 		return -ENODEV;
1699 
1700 	ret = dell_smm_init_data(&wdev->dev, ops);
1701 	if (ret < 0)
1702 		return ret;
1703 
1704 	return dell_smm_init_hwmon(&wdev->dev);
1705 }
1706 
1707 static const struct wmi_device_id dell_smm_wmi_id_table[] = {
1708 	{ DELL_SMM_WMI_GUID, NULL },
1709 	{ }
1710 };
1711 MODULE_DEVICE_TABLE(wmi, dell_smm_wmi_id_table);
1712 
1713 static struct wmi_driver dell_smm_wmi_driver = {
1714 	.driver = {
1715 		.name = KBUILD_MODNAME,
1716 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1717 	},
1718 	.id_table = dell_smm_wmi_id_table,
1719 	.probe = dell_smm_wmi_probe,
1720 	.no_singleton = true,
1721 };
1722 
1723 /*
1724  * Probe for the presence of a supported laptop.
1725  */
dell_smm_init_dmi(void)1726 static void __init dell_smm_init_dmi(void)
1727 {
1728 	struct i8k_fan_control_data *control;
1729 	struct i8k_config_data *config;
1730 	const struct dmi_system_id *id;
1731 
1732 	if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1733 		if (!force) {
1734 			pr_notice("Disabling fan support due to BIOS bugs\n");
1735 			disallow_fan_support = true;
1736 		} else {
1737 			pr_warn("Enabling fan support despite BIOS bugs\n");
1738 		}
1739 	}
1740 
1741 	if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1742 		if (!force) {
1743 			pr_notice("Disabling fan type call due to BIOS bugs\n");
1744 			disallow_fan_type_call = true;
1745 		} else {
1746 			pr_warn("Enabling fan type call despite BIOS bugs\n");
1747 		}
1748 	}
1749 
1750 	/*
1751 	 * Set fan multiplier and maximal fan speed from DMI config.
1752 	 * Values specified in module parameters override values from DMI.
1753 	 */
1754 	id = dmi_first_match(i8k_config_dmi_table);
1755 	if (id && id->driver_data) {
1756 		config = id->driver_data;
1757 		if (!fan_mult && config->fan_mult)
1758 			fan_mult = config->fan_mult;
1759 
1760 		if (!fan_max && config->fan_max)
1761 			fan_max = config->fan_max;
1762 	}
1763 
1764 	id = dmi_first_match(i8k_whitelist_fan_control);
1765 	if (id && id->driver_data) {
1766 		control = id->driver_data;
1767 		manual_fan = control->manual_fan;
1768 		auto_fan = control->auto_fan;
1769 
1770 		pr_info("Enabling support for setting automatic/manual fan control\n");
1771 	}
1772 }
1773 
dell_smm_legacy_check(void)1774 static int __init dell_smm_legacy_check(void)
1775 {
1776 	if (!dmi_check_system(i8k_dmi_table)) {
1777 		if (!ignore_dmi && !force)
1778 			return -ENODEV;
1779 
1780 		pr_info("Probing for legacy SMM handler on unsupported machine\n");
1781 		pr_info("vendor=%s, model=%s, version=%s\n",
1782 			i8k_get_dmi_data(DMI_SYS_VENDOR),
1783 			i8k_get_dmi_data(DMI_PRODUCT_NAME),
1784 			i8k_get_dmi_data(DMI_BIOS_VERSION));
1785 	}
1786 
1787 	if (dell_smm_get_signature(&i8k_smm_ops, I8K_SMM_GET_DELL_SIG1) &&
1788 	    dell_smm_get_signature(&i8k_smm_ops, I8K_SMM_GET_DELL_SIG2)) {
1789 		if (!force)
1790 			return -ENODEV;
1791 
1792 		pr_warn("Forcing legacy SMM calls on a possibly incompatible machine\n");
1793 	}
1794 
1795 	return 0;
1796 }
1797 
i8k_init(void)1798 static int __init i8k_init(void)
1799 {
1800 	int ret;
1801 
1802 	dell_smm_init_dmi();
1803 
1804 	ret = dell_smm_legacy_check();
1805 	if (ret < 0) {
1806 		/*
1807 		 * On modern machines, SMM communication happens over WMI, meaning
1808 		 * the SMM handler might not react to legacy SMM calls.
1809 		 */
1810 		return wmi_driver_register(&dell_smm_wmi_driver);
1811 	}
1812 
1813 	dell_smm_device = platform_create_bundle(&dell_smm_driver, dell_smm_probe, NULL, 0, NULL,
1814 						 0);
1815 
1816 	return PTR_ERR_OR_ZERO(dell_smm_device);
1817 }
1818 
i8k_exit(void)1819 static void __exit i8k_exit(void)
1820 {
1821 	if (dell_smm_device) {
1822 		platform_device_unregister(dell_smm_device);
1823 		platform_driver_unregister(&dell_smm_driver);
1824 	} else {
1825 		wmi_driver_unregister(&dell_smm_wmi_driver);
1826 	}
1827 }
1828 
1829 module_init(i8k_init);
1830 module_exit(i8k_exit);
1831