1/*
2 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
3 *
4 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
5 *
6 * Hwmon integration:
7 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
8 * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
9 * Copyright (C) 2014, 2015 Pali Rohár <pali.rohar@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
14 * later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 */
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#include <linux/cpu.h>
25#include <linux/delay.h>
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/init.h>
29#include <linux/proc_fs.h>
30#include <linux/seq_file.h>
31#include <linux/dmi.h>
32#include <linux/capability.h>
33#include <linux/mutex.h>
34#include <linux/hwmon.h>
35#include <linux/hwmon-sysfs.h>
36#include <linux/uaccess.h>
37#include <linux/io.h>
38#include <linux/sched.h>
39#include <linux/ctype.h>
40#include <linux/smp.h>
41
42#include <linux/i8k.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#define I8K_FAN_MULT 30
57#define I8K_FAN_MAX_RPM 30000
58#define I8K_MAX_TEMP 127
59
60#define I8K_FN_NONE 0x00
61#define I8K_FN_UP 0x01
62#define I8K_FN_DOWN 0x02
63#define I8K_FN_MUTE 0x04
64#define I8K_FN_MASK 0x07
65#define I8K_FN_SHIFT 8
66
67#define I8K_POWER_AC 0x05
68#define I8K_POWER_BATTERY 0x01
69
70static DEFINE_MUTEX(i8k_mutex);
71static char bios_version[4];
72static char bios_machineid[16];
73static struct device *i8k_hwmon_dev;
74static u32 i8k_hwmon_flags;
75static uint i8k_fan_mult = I8K_FAN_MULT;
76static uint i8k_pwm_mult;
77static uint i8k_fan_max = I8K_FAN_HIGH;
78static bool disallow_fan_type_call;
79static bool disallow_fan_support;
80
81#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
82#define I8K_HWMON_HAVE_TEMP2 (1 << 1)
83#define I8K_HWMON_HAVE_TEMP3 (1 << 2)
84#define I8K_HWMON_HAVE_TEMP4 (1 << 3)
85#define I8K_HWMON_HAVE_TEMP5 (1 << 4)
86#define I8K_HWMON_HAVE_TEMP6 (1 << 5)
87#define I8K_HWMON_HAVE_TEMP7 (1 << 6)
88#define I8K_HWMON_HAVE_TEMP8 (1 << 7)
89#define I8K_HWMON_HAVE_TEMP9 (1 << 8)
90#define I8K_HWMON_HAVE_TEMP10 (1 << 9)
91#define I8K_HWMON_HAVE_FAN1 (1 << 10)
92#define I8K_HWMON_HAVE_FAN2 (1 << 11)
93#define I8K_HWMON_HAVE_FAN3 (1 << 12)
94
95MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
96MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
97MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
98MODULE_LICENSE("GPL");
99MODULE_ALIAS("i8k");
100
101static bool force;
102module_param(force, bool, 0);
103MODULE_PARM_DESC(force, "Force loading without checking for supported models");
104
105static bool ignore_dmi;
106module_param(ignore_dmi, bool, 0);
107MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
108
109#if IS_ENABLED(CONFIG_I8K)
110static bool restricted = true;
111module_param(restricted, bool, 0);
112MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
113
114static bool power_status;
115module_param(power_status, bool, 0600);
116MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
117#endif
118
119static uint fan_mult;
120module_param(fan_mult, uint, 0);
121MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
122
123static uint fan_max;
124module_param(fan_max, uint, 0);
125MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
126
127struct smm_regs {
128 unsigned int eax;
129 unsigned int ebx __packed;
130 unsigned int ecx __packed;
131 unsigned int edx __packed;
132 unsigned int esi __packed;
133 unsigned int edi __packed;
134};
135
136static inline const char *i8k_get_dmi_data(int field)
137{
138 const char *dmi_data = dmi_get_system_info(field);
139
140 return dmi_data && *dmi_data ? dmi_data : "?";
141}
142
143/*
144 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
145 */
146static int i8k_smm_func(void *par)
147{
148 int rc;
149 struct smm_regs *regs = par;
150 int eax = regs->eax;
151
152#ifdef DEBUG
153 int ebx = regs->ebx;
154 unsigned long duration;
155 ktime_t calltime, delta, rettime;
156
157 calltime = ktime_get();
158#endif
159
160 /* SMM requires CPU 0 */
161 if (smp_processor_id() != 0)
162 return -EBUSY;
163
164#if defined(CONFIG_X86_64)
165 asm volatile("pushq %%rax\n\t"
166 "movl 0(%%rax),%%edx\n\t"
167 "pushq %%rdx\n\t"
168 "movl 4(%%rax),%%ebx\n\t"
169 "movl 8(%%rax),%%ecx\n\t"
170 "movl 12(%%rax),%%edx\n\t"
171 "movl 16(%%rax),%%esi\n\t"
172 "movl 20(%%rax),%%edi\n\t"
173 "popq %%rax\n\t"
174 "out %%al,$0xb2\n\t"
175 "out %%al,$0x84\n\t"
176 "xchgq %%rax,(%%rsp)\n\t"
177 "movl %%ebx,4(%%rax)\n\t"
178 "movl %%ecx,8(%%rax)\n\t"
179 "movl %%edx,12(%%rax)\n\t"
180 "movl %%esi,16(%%rax)\n\t"
181 "movl %%edi,20(%%rax)\n\t"
182 "popq %%rdx\n\t"
183 "movl %%edx,0(%%rax)\n\t"
184 "pushfq\n\t"
185 "popq %%rax\n\t"
186 "andl $1,%%eax\n"
187 : "=a"(rc)
188 : "a"(regs)
189 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
190#else
191 asm volatile("pushl %%eax\n\t"
192 "movl 0(%%eax),%%edx\n\t"
193 "push %%edx\n\t"
194 "movl 4(%%eax),%%ebx\n\t"
195 "movl 8(%%eax),%%ecx\n\t"
196 "movl 12(%%eax),%%edx\n\t"
197 "movl 16(%%eax),%%esi\n\t"
198 "movl 20(%%eax),%%edi\n\t"
199 "popl %%eax\n\t"
200 "out %%al,$0xb2\n\t"
201 "out %%al,$0x84\n\t"
202 "xchgl %%eax,(%%esp)\n\t"
203 "movl %%ebx,4(%%eax)\n\t"
204 "movl %%ecx,8(%%eax)\n\t"
205 "movl %%edx,12(%%eax)\n\t"
206 "movl %%esi,16(%%eax)\n\t"
207 "movl %%edi,20(%%eax)\n\t"
208 "popl %%edx\n\t"
209 "movl %%edx,0(%%eax)\n\t"
210 "lahf\n\t"
211 "shrl $8,%%eax\n\t"
212 "andl $1,%%eax\n"
213 : "=a"(rc)
214 : "a"(regs)
215 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
216#endif
217 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
218 rc = -EINVAL;
219
220#ifdef DEBUG
221 rettime = ktime_get();
222 delta = ktime_sub(rettime, calltime);
223 duration = ktime_to_ns(delta) >> 10;
224 pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x (took %7lu usecs)\n", eax, ebx,
225 (rc ? 0xffff : regs->eax & 0xffff), duration);
226#endif
227
228 return rc;
229}
230
231/*
232 * Call the System Management Mode BIOS.
233 */
234static int i8k_smm(struct smm_regs *regs)
235{
236 int ret;
237
238 get_online_cpus();
239 ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
240 put_online_cpus();
241
242 return ret;
243}
244
245/*
246 * Read the fan status.
247 */
248static int i8k_get_fan_status(int fan)
249{
250 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
251
252 if (disallow_fan_support)
253 return -EINVAL;
254
255 regs.ebx = fan & 0xff;
256 return i8k_smm(&regs) ? : regs.eax & 0xff;
257}
258
259/*
260 * Read the fan speed in RPM.
261 */
262static int i8k_get_fan_speed(int fan)
263{
264 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
265
266 if (disallow_fan_support)
267 return -EINVAL;
268
269 regs.ebx = fan & 0xff;
270 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
271}
272
273/*
274 * Read the fan type.
275 */
276static int _i8k_get_fan_type(int fan)
277{
278 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
279
280 if (disallow_fan_support || disallow_fan_type_call)
281 return -EINVAL;
282
283 regs.ebx = fan & 0xff;
284 return i8k_smm(&regs) ? : regs.eax & 0xff;
285}
286
287static int i8k_get_fan_type(int fan)
288{
289 /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
290 static int types[3] = { INT_MIN, INT_MIN, INT_MIN };
291
292 if (types[fan] == INT_MIN)
293 types[fan] = _i8k_get_fan_type(fan);
294
295 return types[fan];
296}
297
298/*
299 * Read the fan nominal rpm for specific fan speed.
300 */
301static int i8k_get_fan_nominal_speed(int fan, int speed)
302{
303 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
304
305 if (disallow_fan_support)
306 return -EINVAL;
307
308 regs.ebx = (fan & 0xff) | (speed << 8);
309 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
310}
311
312/*
313 * Set the fan speed (off, low, high). Returns the new fan status.
314 */
315static int i8k_set_fan(int fan, int speed)
316{
317 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
318
319 if (disallow_fan_support)
320 return -EINVAL;
321
322 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
323 regs.ebx = (fan & 0xff) | (speed << 8);
324
325 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
326}
327
328static int i8k_get_temp_type(int sensor)
329{
330 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
331
332 regs.ebx = sensor & 0xff;
333 return i8k_smm(&regs) ? : regs.eax & 0xff;
334}
335
336/*
337 * Read the cpu temperature.
338 */
339static int _i8k_get_temp(int sensor)
340{
341 struct smm_regs regs = {
342 .eax = I8K_SMM_GET_TEMP,
343 .ebx = sensor & 0xff,
344 };
345
346 return i8k_smm(&regs) ? : regs.eax & 0xff;
347}
348
349static int i8k_get_temp(int sensor)
350{
351 int temp = _i8k_get_temp(sensor);
352
353 /*
354 * Sometimes the temperature sensor returns 0x99, which is out of range.
355 * In this case we retry (once) before returning an error.
356 # 1003655137 00000058 00005a4b
357 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
358 # 1003655139 00000054 00005c52
359 */
360 if (temp == 0x99) {
361 msleep(100);
362 temp = _i8k_get_temp(sensor);
363 }
364 /*
365 * Return -ENODATA for all invalid temperatures.
366 *
367 * Known instances are the 0x99 value as seen above as well as
368 * 0xc1 (193), which may be returned when trying to read the GPU
369 * temperature if the system supports a GPU and it is currently
370 * turned off.
371 */
372 if (temp > I8K_MAX_TEMP)
373 return -ENODATA;
374
375 return temp;
376}
377
378static int i8k_get_dell_signature(int req_fn)
379{
380 struct smm_regs regs = { .eax = req_fn, };
381 int rc;
382
383 rc = i8k_smm(&regs);
384 if (rc < 0)
385 return rc;
386
387 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
388}
389
390#if IS_ENABLED(CONFIG_I8K)
391
392/*
393 * Read the Fn key status.
394 */
395static int i8k_get_fn_status(void)
396{
397 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
398 int rc;
399
400 rc = i8k_smm(&regs);
401 if (rc < 0)
402 return rc;
403
404 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
405 case I8K_FN_UP:
406 return I8K_VOL_UP;
407 case I8K_FN_DOWN:
408 return I8K_VOL_DOWN;
409 case I8K_FN_MUTE:
410 return I8K_VOL_MUTE;
411 default:
412 return 0;
413 }
414}
415
416/*
417 * Read the power status.
418 */
419static int i8k_get_power_status(void)
420{
421 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
422 int rc;
423
424 rc = i8k_smm(&regs);
425 if (rc < 0)
426 return rc;
427
428 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
429}
430
431/*
432 * Procfs interface
433 */
434
435static int
436i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
437{
438 int val = 0;
439 int speed;
440 unsigned char buff[16];
441 int __user *argp = (int __user *)arg;
442
443 if (!argp)
444 return -EINVAL;
445
446 switch (cmd) {
447 case I8K_BIOS_VERSION:
448 if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
449 !isdigit(bios_version[2]))
450 return -EINVAL;
451
452 val = (bios_version[0] << 16) |
453 (bios_version[1] << 8) | bios_version[2];
454 break;
455
456 case I8K_MACHINE_ID:
457 if (restricted && !capable(CAP_SYS_ADMIN))
458 return -EPERM;
459
460 memset(buff, 0, sizeof(buff));
461 strlcpy(buff, bios_machineid, sizeof(buff));
462 break;
463
464 case I8K_FN_STATUS:
465 val = i8k_get_fn_status();
466 break;
467
468 case I8K_POWER_STATUS:
469 val = i8k_get_power_status();
470 break;
471
472 case I8K_GET_TEMP:
473 val = i8k_get_temp(0);
474 break;
475
476 case I8K_GET_SPEED:
477 if (copy_from_user(&val, argp, sizeof(int)))
478 return -EFAULT;
479
480 val = i8k_get_fan_speed(val);
481 break;
482
483 case I8K_GET_FAN:
484 if (copy_from_user(&val, argp, sizeof(int)))
485 return -EFAULT;
486
487 val = i8k_get_fan_status(val);
488 break;
489
490 case I8K_SET_FAN:
491 if (restricted && !capable(CAP_SYS_ADMIN))
492 return -EPERM;
493
494 if (copy_from_user(&val, argp, sizeof(int)))
495 return -EFAULT;
496
497 if (copy_from_user(&speed, argp + 1, sizeof(int)))
498 return -EFAULT;
499
500 val = i8k_set_fan(val, speed);
501 break;
502
503 default:
504 return -EINVAL;
505 }
506
507 if (val < 0)
508 return val;
509
510 switch (cmd) {
511 case I8K_BIOS_VERSION:
512 if (copy_to_user(argp, &val, 4))
513 return -EFAULT;
514
515 break;
516 case I8K_MACHINE_ID:
517 if (copy_to_user(argp, buff, 16))
518 return -EFAULT;
519
520 break;
521 default:
522 if (copy_to_user(argp, &val, sizeof(int)))
523 return -EFAULT;
524
525 break;
526 }
527
528 return 0;
529}
530
531static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
532{
533 long ret;
534
535 mutex_lock(&i8k_mutex);
536 ret = i8k_ioctl_unlocked(fp, cmd, arg);
537 mutex_unlock(&i8k_mutex);
538
539 return ret;
540}
541
542/*
543 * Print the information for /proc/i8k.
544 */
545static int i8k_proc_show(struct seq_file *seq, void *offset)
546{
547 int fn_key, cpu_temp, ac_power;
548 int left_fan, right_fan, left_speed, right_speed;
549
550 cpu_temp = i8k_get_temp(0); /* 11100 µs */
551 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
552 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
553 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
554 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
555 fn_key = i8k_get_fn_status(); /* 750 µs */
556 if (power_status)
557 ac_power = i8k_get_power_status(); /* 14700 µs */
558 else
559 ac_power = -1;
560
561 /*
562 * Info:
563 *
564 * 1) Format version (this will change if format changes)
565 * 2) BIOS version
566 * 3) BIOS machine ID
567 * 4) Cpu temperature
568 * 5) Left fan status
569 * 6) Right fan status
570 * 7) Left fan speed
571 * 8) Right fan speed
572 * 9) AC power
573 * 10) Fn Key status
574 */
575 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
576 I8K_PROC_FMT,
577 bios_version,
578 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
579 cpu_temp,
580 left_fan, right_fan, left_speed, right_speed,
581 ac_power, fn_key);
582
583 return 0;
584}
585
586static int i8k_open_fs(struct inode *inode, struct file *file)
587{
588 return single_open(file, i8k_proc_show, NULL);
589}
590
591static const struct file_operations i8k_fops = {
592 .owner = THIS_MODULE,
593 .open = i8k_open_fs,
594 .read = seq_read,
595 .llseek = seq_lseek,
596 .release = single_release,
597 .unlocked_ioctl = i8k_ioctl,
598};
599
600static void __init i8k_init_procfs(void)
601{
602 /* Register the proc entry */
603 proc_create("i8k", 0, NULL, &i8k_fops);
604}
605
606static void __exit i8k_exit_procfs(void)
607{
608 remove_proc_entry("i8k", NULL);
609}
610
611#else
612
613static inline void __init i8k_init_procfs(void)
614{
615}
616
617static inline void __exit i8k_exit_procfs(void)
618{
619}
620
621#endif
622
623/*
624 * Hwmon interface
625 */
626
627static ssize_t i8k_hwmon_temp_label_show(struct device *dev,
628 struct device_attribute *devattr,
629 char *buf)
630{
631 static const char * const labels[] = {
632 "CPU",
633 "GPU",
634 "SODIMM",
635 "Other",
636 "Ambient",
637 "Other",
638 };
639 int index = to_sensor_dev_attr(devattr)->index;
640 int type;
641
642 type = i8k_get_temp_type(index);
643 if (type < 0)
644 return type;
645 if (type >= ARRAY_SIZE(labels))
646 type = ARRAY_SIZE(labels) - 1;
647 return sprintf(buf, "%s\n", labels[type]);
648}
649
650static ssize_t i8k_hwmon_temp_show(struct device *dev,
651 struct device_attribute *devattr,
652 char *buf)
653{
654 int index = to_sensor_dev_attr(devattr)->index;
655 int temp;
656
657 temp = i8k_get_temp(index);
658 if (temp < 0)
659 return temp;
660 return sprintf(buf, "%d\n", temp * 1000);
661}
662
663static ssize_t i8k_hwmon_fan_label_show(struct device *dev,
664 struct device_attribute *devattr,
665 char *buf)
666{
667 static const char * const labels[] = {
668 "Processor Fan",
669 "Motherboard Fan",
670 "Video Fan",
671 "Power Supply Fan",
672 "Chipset Fan",
673 "Other Fan",
674 };
675 int index = to_sensor_dev_attr(devattr)->index;
676 bool dock = false;
677 int type;
678
679 type = i8k_get_fan_type(index);
680 if (type < 0)
681 return type;
682
683 if (type & 0x10) {
684 dock = true;
685 type &= 0x0F;
686 }
687
688 if (type >= ARRAY_SIZE(labels))
689 type = (ARRAY_SIZE(labels) - 1);
690
691 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
692}
693
694static ssize_t i8k_hwmon_fan_show(struct device *dev,
695 struct device_attribute *devattr, char *buf)
696{
697 int index = to_sensor_dev_attr(devattr)->index;
698 int fan_speed;
699
700 fan_speed = i8k_get_fan_speed(index);
701 if (fan_speed < 0)
702 return fan_speed;
703 return sprintf(buf, "%d\n", fan_speed);
704}
705
706static ssize_t i8k_hwmon_pwm_show(struct device *dev,
707 struct device_attribute *devattr, char *buf)
708{
709 int index = to_sensor_dev_attr(devattr)->index;
710 int status;
711
712 status = i8k_get_fan_status(index);
713 if (status < 0)
714 return -EIO;
715 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
716}
717
718static ssize_t i8k_hwmon_pwm_store(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf, size_t count)
721{
722 int index = to_sensor_dev_attr(attr)->index;
723 unsigned long val;
724 int err;
725
726 err = kstrtoul(buf, 10, &val);
727 if (err)
728 return err;
729 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
730
731 mutex_lock(&i8k_mutex);
732 err = i8k_set_fan(index, val);
733 mutex_unlock(&i8k_mutex);
734
735 return err < 0 ? -EIO : count;
736}
737
738static SENSOR_DEVICE_ATTR_RO(temp1_input, i8k_hwmon_temp, 0);
739static SENSOR_DEVICE_ATTR_RO(temp1_label, i8k_hwmon_temp_label, 0);
740static SENSOR_DEVICE_ATTR_RO(temp2_input, i8k_hwmon_temp, 1);
741static SENSOR_DEVICE_ATTR_RO(temp2_label, i8k_hwmon_temp_label, 1);
742static SENSOR_DEVICE_ATTR_RO(temp3_input, i8k_hwmon_temp, 2);
743static SENSOR_DEVICE_ATTR_RO(temp3_label, i8k_hwmon_temp_label, 2);
744static SENSOR_DEVICE_ATTR_RO(temp4_input, i8k_hwmon_temp, 3);
745static SENSOR_DEVICE_ATTR_RO(temp4_label, i8k_hwmon_temp_label, 3);
746static SENSOR_DEVICE_ATTR_RO(temp5_input, i8k_hwmon_temp, 4);
747static SENSOR_DEVICE_ATTR_RO(temp5_label, i8k_hwmon_temp_label, 4);
748static SENSOR_DEVICE_ATTR_RO(temp6_input, i8k_hwmon_temp, 5);
749static SENSOR_DEVICE_ATTR_RO(temp6_label, i8k_hwmon_temp_label, 5);
750static SENSOR_DEVICE_ATTR_RO(temp7_input, i8k_hwmon_temp, 6);
751static SENSOR_DEVICE_ATTR_RO(temp7_label, i8k_hwmon_temp_label, 6);
752static SENSOR_DEVICE_ATTR_RO(temp8_input, i8k_hwmon_temp, 7);
753static SENSOR_DEVICE_ATTR_RO(temp8_label, i8k_hwmon_temp_label, 7);
754static SENSOR_DEVICE_ATTR_RO(temp9_input, i8k_hwmon_temp, 8);
755static SENSOR_DEVICE_ATTR_RO(temp9_label, i8k_hwmon_temp_label, 8);
756static SENSOR_DEVICE_ATTR_RO(temp10_input, i8k_hwmon_temp, 9);
757static SENSOR_DEVICE_ATTR_RO(temp10_label, i8k_hwmon_temp_label, 9);
758static SENSOR_DEVICE_ATTR_RO(fan1_input, i8k_hwmon_fan, 0);
759static SENSOR_DEVICE_ATTR_RO(fan1_label, i8k_hwmon_fan_label, 0);
760static SENSOR_DEVICE_ATTR_RW(pwm1, i8k_hwmon_pwm, 0);
761static SENSOR_DEVICE_ATTR_RO(fan2_input, i8k_hwmon_fan, 1);
762static SENSOR_DEVICE_ATTR_RO(fan2_label, i8k_hwmon_fan_label, 1);
763static SENSOR_DEVICE_ATTR_RW(pwm2, i8k_hwmon_pwm, 1);
764static SENSOR_DEVICE_ATTR_RO(fan3_input, i8k_hwmon_fan, 2);
765static SENSOR_DEVICE_ATTR_RO(fan3_label, i8k_hwmon_fan_label, 2);
766static SENSOR_DEVICE_ATTR_RW(pwm3, i8k_hwmon_pwm, 2);
767
768static struct attribute *i8k_attrs[] = {
769 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
770 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
771 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
772 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
773 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
774 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
775 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
776 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
777 &sensor_dev_attr_temp5_input.dev_attr.attr, /* 8 */
778 &sensor_dev_attr_temp5_label.dev_attr.attr, /* 9 */
779 &sensor_dev_attr_temp6_input.dev_attr.attr, /* 10 */
780 &sensor_dev_attr_temp6_label.dev_attr.attr, /* 11 */
781 &sensor_dev_attr_temp7_input.dev_attr.attr, /* 12 */
782 &sensor_dev_attr_temp7_label.dev_attr.attr, /* 13 */
783 &sensor_dev_attr_temp8_input.dev_attr.attr, /* 14 */
784 &sensor_dev_attr_temp8_label.dev_attr.attr, /* 15 */
785 &sensor_dev_attr_temp9_input.dev_attr.attr, /* 16 */
786 &sensor_dev_attr_temp9_label.dev_attr.attr, /* 17 */
787 &sensor_dev_attr_temp10_input.dev_attr.attr, /* 18 */
788 &sensor_dev_attr_temp10_label.dev_attr.attr, /* 19 */
789 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 20 */
790 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 21 */
791 &sensor_dev_attr_pwm1.dev_attr.attr, /* 22 */
792 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 23 */
793 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 24 */
794 &sensor_dev_attr_pwm2.dev_attr.attr, /* 25 */
795 &sensor_dev_attr_fan3_input.dev_attr.attr, /* 26 */
796 &sensor_dev_attr_fan3_label.dev_attr.attr, /* 27 */
797 &sensor_dev_attr_pwm3.dev_attr.attr, /* 28 */
798 NULL
799};
800
801static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
802 int index)
803{
804 if (disallow_fan_support && index >= 8)
805 return 0;
806 if (disallow_fan_type_call &&
807 (index == 9 || index == 12 || index == 15))
808 return 0;
809 if (index >= 0 && index <= 1 &&
810 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
811 return 0;
812 if (index >= 2 && index <= 3 &&
813 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
814 return 0;
815 if (index >= 4 && index <= 5 &&
816 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
817 return 0;
818 if (index >= 6 && index <= 7 &&
819 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
820 return 0;
821 if (index >= 8 && index <= 9 &&
822 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP5))
823 return 0;
824 if (index >= 10 && index <= 11 &&
825 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP6))
826 return 0;
827 if (index >= 12 && index <= 13 &&
828 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP7))
829 return 0;
830 if (index >= 14 && index <= 15 &&
831 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP8))
832 return 0;
833 if (index >= 16 && index <= 17 &&
834 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP9))
835 return 0;
836 if (index >= 18 && index <= 19 &&
837 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP10))
838 return 0;
839
840 if (index >= 20 && index <= 22 &&
841 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
842 return 0;
843 if (index >= 23 && index <= 25 &&
844 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
845 return 0;
846 if (index >= 26 && index <= 28 &&
847 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3))
848 return 0;
849
850 return attr->mode;
851}
852
853static const struct attribute_group i8k_group = {
854 .attrs = i8k_attrs,
855 .is_visible = i8k_is_visible,
856};
857__ATTRIBUTE_GROUPS(i8k);
858
859static int __init i8k_init_hwmon(void)
860{
861 int err;
862
863 i8k_hwmon_flags = 0;
864
865 /* CPU temperature attributes, if temperature type is OK */
866 err = i8k_get_temp_type(0);
867 if (err >= 0)
868 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
869 /* check for additional temperature sensors */
870 err = i8k_get_temp_type(1);
871 if (err >= 0)
872 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
873 err = i8k_get_temp_type(2);
874 if (err >= 0)
875 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
876 err = i8k_get_temp_type(3);
877 if (err >= 0)
878 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
879 err = i8k_get_temp_type(4);
880 if (err >= 0)
881 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP5;
882 err = i8k_get_temp_type(5);
883 if (err >= 0)
884 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP6;
885 err = i8k_get_temp_type(6);
886 if (err >= 0)
887 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP7;
888 err = i8k_get_temp_type(7);
889 if (err >= 0)
890 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP8;
891 err = i8k_get_temp_type(8);
892 if (err >= 0)
893 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP9;
894 err = i8k_get_temp_type(9);
895 if (err >= 0)
896 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP10;
897
898 /* First fan attributes, if fan status or type is OK */
899 err = i8k_get_fan_status(0);
900 if (err < 0)
901 err = i8k_get_fan_type(0);
902 if (err >= 0)
903 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
904
905 /* Second fan attributes, if fan status or type is OK */
906 err = i8k_get_fan_status(1);
907 if (err < 0)
908 err = i8k_get_fan_type(1);
909 if (err >= 0)
910 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
911
912 /* Third fan attributes, if fan status or type is OK */
913 err = i8k_get_fan_status(2);
914 if (err < 0)
915 err = i8k_get_fan_type(2);
916 if (err >= 0)
917 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3;
918
919 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
920 NULL, i8k_groups);
921 if (IS_ERR(i8k_hwmon_dev)) {
922 err = PTR_ERR(i8k_hwmon_dev);
923 i8k_hwmon_dev = NULL;
924 pr_err("hwmon registration failed (%d)\n", err);
925 return err;
926 }
927 return 0;
928}
929
930struct i8k_config_data {
931 uint fan_mult;
932 uint fan_max;
933};
934
935enum i8k_configs {
936 DELL_LATITUDE_D520,
937 DELL_PRECISION_490,
938 DELL_STUDIO,
939 DELL_XPS,
940};
941
942static const struct i8k_config_data i8k_config_data[] = {
943 [DELL_LATITUDE_D520] = {
944 .fan_mult = 1,
945 .fan_max = I8K_FAN_TURBO,
946 },
947 [DELL_PRECISION_490] = {
948 .fan_mult = 1,
949 .fan_max = I8K_FAN_TURBO,
950 },
951 [DELL_STUDIO] = {
952 .fan_mult = 1,
953 .fan_max = I8K_FAN_HIGH,
954 },
955 [DELL_XPS] = {
956 .fan_mult = 1,
957 .fan_max = I8K_FAN_HIGH,
958 },
959};
960
961static const struct dmi_system_id i8k_dmi_table[] __initconst = {
962 {
963 .ident = "Dell Inspiron",
964 .matches = {
965 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
966 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
967 },
968 },
969 {
970 .ident = "Dell Latitude",
971 .matches = {
972 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
973 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
974 },
975 },
976 {
977 .ident = "Dell Inspiron 2",
978 .matches = {
979 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
980 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
981 },
982 },
983 {
984 .ident = "Dell Latitude D520",
985 .matches = {
986 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
987 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
988 },
989 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
990 },
991 {
992 .ident = "Dell Latitude 2",
993 .matches = {
994 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
995 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
996 },
997 },
998 { /* UK Inspiron 6400 */
999 .ident = "Dell Inspiron 3",
1000 .matches = {
1001 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1002 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
1003 },
1004 },
1005 {
1006 .ident = "Dell Inspiron 3",
1007 .matches = {
1008 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1009 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
1010 },
1011 },
1012 {
1013 .ident = "Dell Precision 490",
1014 .matches = {
1015 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1016 DMI_MATCH(DMI_PRODUCT_NAME,
1017 "Precision WorkStation 490"),
1018 },
1019 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
1020 },
1021 {
1022 .ident = "Dell Precision",
1023 .matches = {
1024 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1025 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
1026 },
1027 },
1028 {
1029 .ident = "Dell Vostro",
1030 .matches = {
1031 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1032 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
1033 },
1034 },
1035 {
1036 .ident = "Dell XPS421",
1037 .matches = {
1038 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1039 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
1040 },
1041 },
1042 {
1043 .ident = "Dell Studio",
1044 .matches = {
1045 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1046 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1047 },
1048 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1049 },
1050 {
1051 .ident = "Dell XPS 13",
1052 .matches = {
1053 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1054 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
1055 },
1056 .driver_data = (void *)&i8k_config_data[DELL_XPS],
1057 },
1058 {
1059 .ident = "Dell XPS M140",
1060 .matches = {
1061 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1062 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1063 },
1064 .driver_data = (void *)&i8k_config_data[DELL_XPS],
1065 },
1066 {
1067 .ident = "Dell XPS 15 9560",
1068 .matches = {
1069 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1070 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 15 9560"),
1071 },
1072 },
1073 {
1074 .ident = "Dell XPS 15 9570",
1075 .matches = {
1076 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1077 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 15 9570"),
1078 },
1079 },
1080 { }
1081};
1082
1083MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1084
1085/*
1086 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1087 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1088 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1089 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1090 */
1091static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1092 {
1093 .ident = "Dell Studio XPS 8000",
1094 .matches = {
1095 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1096 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1097 },
1098 },
1099 {
1100 .ident = "Dell Studio XPS 8100",
1101 .matches = {
1102 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1103 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1104 },
1105 },
1106 {
1107 .ident = "Dell Inspiron 580",
1108 .matches = {
1109 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1110 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1111 },
1112 },
1113 { }
1114};
1115
1116/*
1117 * On some machines all fan related SMM functions implemented by Dell BIOS
1118 * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan
1119 * support for affected blacklisted Dell machines stay disabled.
1120 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751
1121 */
1122static struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initdata = {
1123 {
1124 .ident = "Dell Inspiron 7720",
1125 .matches = {
1126 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1127 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1128 },
1129 },
1130 {
1131 .ident = "Dell Vostro 3360",
1132 .matches = {
1133 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1134 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1135 },
1136 },
1137 {
1138 .ident = "Dell XPS13 9333",
1139 .matches = {
1140 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1141 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1142 },
1143 },
1144 { }
1145};
1146
1147/*
1148 * Probe for the presence of a supported laptop.
1149 */
1150static int __init i8k_probe(void)
1151{
1152 const struct dmi_system_id *id;
1153 int fan, ret;
1154
1155 /*
1156 * Get DMI information
1157 */
1158 if (!dmi_check_system(i8k_dmi_table)) {
1159 if (!ignore_dmi && !force)
1160 return -ENODEV;
1161
1162 pr_info("not running on a supported Dell system.\n");
1163 pr_info("vendor=%s, model=%s, version=%s\n",
1164 i8k_get_dmi_data(DMI_SYS_VENDOR),
1165 i8k_get_dmi_data(DMI_PRODUCT_NAME),
1166 i8k_get_dmi_data(DMI_BIOS_VERSION));
1167 }
1168
1169 if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1170 pr_warn("broken Dell BIOS detected, disallow fan support\n");
1171 if (!force)
1172 disallow_fan_support = true;
1173 }
1174
1175 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1176 pr_warn("broken Dell BIOS detected, disallow fan type call\n");
1177 if (!force)
1178 disallow_fan_type_call = true;
1179 }
1180
1181 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1182 sizeof(bios_version));
1183 strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1184 sizeof(bios_machineid));
1185
1186 /*
1187 * Get SMM Dell signature
1188 */
1189 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1190 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
1191 pr_err("unable to get SMM Dell signature\n");
1192 if (!force)
1193 return -ENODEV;
1194 }
1195
1196 /*
1197 * Set fan multiplier and maximal fan speed from dmi config
1198 * Values specified in module parameters override values from dmi
1199 */
1200 id = dmi_first_match(i8k_dmi_table);
1201 if (id && id->driver_data) {
1202 const struct i8k_config_data *conf = id->driver_data;
1203 if (!fan_mult && conf->fan_mult)
1204 fan_mult = conf->fan_mult;
1205 if (!fan_max && conf->fan_max)
1206 fan_max = conf->fan_max;
1207 }
1208
1209 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
1210 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
1211
1212 if (!fan_mult) {
1213 /*
1214 * Autodetect fan multiplier based on nominal rpm
1215 * If fan reports rpm value too high then set multiplier to 1
1216 */
1217 for (fan = 0; fan < 2; ++fan) {
1218 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1219 if (ret < 0)
1220 continue;
1221 if (ret > I8K_FAN_MAX_RPM)
1222 i8k_fan_mult = 1;
1223 break;
1224 }
1225 } else {
1226 /* Fan multiplier was specified in module param or in dmi */
1227 i8k_fan_mult = fan_mult;
1228 }
1229
1230 return 0;
1231}
1232
1233static int __init i8k_init(void)
1234{
1235 int err;
1236
1237 /* Are we running on an supported laptop? */
1238 if (i8k_probe())
1239 return -ENODEV;
1240
1241 err = i8k_init_hwmon();
1242 if (err)
1243 return err;
1244
1245 i8k_init_procfs();
1246 return 0;
1247}
1248
1249static void __exit i8k_exit(void)
1250{
1251 hwmon_device_unregister(i8k_hwmon_dev);
1252 i8k_exit_procfs();
1253}
1254
1255module_init(i8k_init);
1256module_exit(i8k_exit);
1257