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