dell-smm-hwmon.c 28 KB

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