gsmi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2010 Google Inc. All Rights Reserved.
  4. * Author: [email protected] (Duncan Laurie)
  5. *
  6. * Re-worked to expose sysfs APIs by [email protected] (Mike Waychison)
  7. *
  8. * EFI SMI interface for Google platforms
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/fs.h>
  20. #include <linux/slab.h>
  21. #include <linux/panic_notifier.h>
  22. #include <linux/ioctl.h>
  23. #include <linux/acpi.h>
  24. #include <linux/io.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/dmi.h>
  27. #include <linux/kdebug.h>
  28. #include <linux/reboot.h>
  29. #include <linux/efi.h>
  30. #include <linux/module.h>
  31. #include <linux/ucs2_string.h>
  32. #include <linux/suspend.h>
  33. #define GSMI_SHUTDOWN_CLEAN 0 /* Clean Shutdown */
  34. /* TODO([email protected]): Tie in HARDLOCKUP_DETECTOR with NMIWDT */
  35. #define GSMI_SHUTDOWN_NMIWDT 1 /* NMI Watchdog */
  36. #define GSMI_SHUTDOWN_PANIC 2 /* Panic */
  37. #define GSMI_SHUTDOWN_OOPS 3 /* Oops */
  38. #define GSMI_SHUTDOWN_DIE 4 /* Die -- No longer meaningful */
  39. #define GSMI_SHUTDOWN_MCE 5 /* Machine Check */
  40. #define GSMI_SHUTDOWN_SOFTWDT 6 /* Software Watchdog */
  41. #define GSMI_SHUTDOWN_MBE 7 /* Uncorrected ECC */
  42. #define GSMI_SHUTDOWN_TRIPLE 8 /* Triple Fault */
  43. #define DRIVER_VERSION "1.0"
  44. #define GSMI_GUID_SIZE 16
  45. #define GSMI_BUF_SIZE 1024
  46. #define GSMI_BUF_ALIGN sizeof(u64)
  47. #define GSMI_CALLBACK 0xef
  48. /* SMI return codes */
  49. #define GSMI_SUCCESS 0x00
  50. #define GSMI_UNSUPPORTED2 0x03
  51. #define GSMI_LOG_FULL 0x0b
  52. #define GSMI_VAR_NOT_FOUND 0x0e
  53. #define GSMI_HANDSHAKE_SPIN 0x7d
  54. #define GSMI_HANDSHAKE_CF 0x7e
  55. #define GSMI_HANDSHAKE_NONE 0x7f
  56. #define GSMI_INVALID_PARAMETER 0x82
  57. #define GSMI_UNSUPPORTED 0x83
  58. #define GSMI_BUFFER_TOO_SMALL 0x85
  59. #define GSMI_NOT_READY 0x86
  60. #define GSMI_DEVICE_ERROR 0x87
  61. #define GSMI_NOT_FOUND 0x8e
  62. #define QUIRKY_BOARD_HASH 0x78a30a50
  63. /* Internally used commands passed to the firmware */
  64. #define GSMI_CMD_GET_NVRAM_VAR 0x01
  65. #define GSMI_CMD_GET_NEXT_VAR 0x02
  66. #define GSMI_CMD_SET_NVRAM_VAR 0x03
  67. #define GSMI_CMD_SET_EVENT_LOG 0x08
  68. #define GSMI_CMD_CLEAR_EVENT_LOG 0x09
  69. #define GSMI_CMD_LOG_S0IX_SUSPEND 0x0a
  70. #define GSMI_CMD_LOG_S0IX_RESUME 0x0b
  71. #define GSMI_CMD_CLEAR_CONFIG 0x20
  72. #define GSMI_CMD_HANDSHAKE_TYPE 0xC1
  73. #define GSMI_CMD_RESERVED 0xff
  74. /* Magic entry type for kernel events */
  75. #define GSMI_LOG_ENTRY_TYPE_KERNEL 0xDEAD
  76. /* SMI buffers must be in 32bit physical address space */
  77. struct gsmi_buf {
  78. u8 *start; /* start of buffer */
  79. size_t length; /* length of buffer */
  80. u32 address; /* physical address of buffer */
  81. };
  82. static struct gsmi_device {
  83. struct platform_device *pdev; /* platform device */
  84. struct gsmi_buf *name_buf; /* variable name buffer */
  85. struct gsmi_buf *data_buf; /* generic data buffer */
  86. struct gsmi_buf *param_buf; /* parameter buffer */
  87. spinlock_t lock; /* serialize access to SMIs */
  88. u16 smi_cmd; /* SMI command port */
  89. int handshake_type; /* firmware handler interlock type */
  90. struct kmem_cache *mem_pool; /* kmem cache for gsmi_buf allocations */
  91. } gsmi_dev;
  92. /* Packed structures for communicating with the firmware */
  93. struct gsmi_nvram_var_param {
  94. efi_guid_t guid;
  95. u32 name_ptr;
  96. u32 attributes;
  97. u32 data_len;
  98. u32 data_ptr;
  99. } __packed;
  100. struct gsmi_get_next_var_param {
  101. u8 guid[GSMI_GUID_SIZE];
  102. u32 name_ptr;
  103. u32 name_len;
  104. } __packed;
  105. struct gsmi_set_eventlog_param {
  106. u32 data_ptr;
  107. u32 data_len;
  108. u32 type;
  109. } __packed;
  110. /* Event log formats */
  111. struct gsmi_log_entry_type_1 {
  112. u16 type;
  113. u32 instance;
  114. } __packed;
  115. /*
  116. * Some platforms don't have explicit SMI handshake
  117. * and need to wait for SMI to complete.
  118. */
  119. #define GSMI_DEFAULT_SPINCOUNT 0x10000
  120. static unsigned int spincount = GSMI_DEFAULT_SPINCOUNT;
  121. module_param(spincount, uint, 0600);
  122. MODULE_PARM_DESC(spincount,
  123. "The number of loop iterations to use when using the spin handshake.");
  124. /*
  125. * Some older platforms with Apollo Lake chipsets do not support S0ix logging
  126. * in their GSMI handlers, and behaved poorly when resuming via power button
  127. * press if the logging was attempted. Updated firmware with proper behavior
  128. * has long since shipped, removing the need for this opt-in parameter. It
  129. * now exists as an opt-out parameter for folks defiantly running old
  130. * firmware, or unforeseen circumstances. After the change from opt-in to
  131. * opt-out has baked sufficiently, this parameter should probably be removed
  132. * entirely.
  133. */
  134. static bool s0ix_logging_enable = true;
  135. module_param(s0ix_logging_enable, bool, 0600);
  136. static struct gsmi_buf *gsmi_buf_alloc(void)
  137. {
  138. struct gsmi_buf *smibuf;
  139. smibuf = kzalloc(sizeof(*smibuf), GFP_KERNEL);
  140. if (!smibuf) {
  141. printk(KERN_ERR "gsmi: out of memory\n");
  142. return NULL;
  143. }
  144. /* allocate buffer in 32bit address space */
  145. smibuf->start = kmem_cache_alloc(gsmi_dev.mem_pool, GFP_KERNEL);
  146. if (!smibuf->start) {
  147. printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
  148. kfree(smibuf);
  149. return NULL;
  150. }
  151. /* fill in the buffer handle */
  152. smibuf->length = GSMI_BUF_SIZE;
  153. smibuf->address = (u32)virt_to_phys(smibuf->start);
  154. return smibuf;
  155. }
  156. static void gsmi_buf_free(struct gsmi_buf *smibuf)
  157. {
  158. if (smibuf) {
  159. if (smibuf->start)
  160. kmem_cache_free(gsmi_dev.mem_pool, smibuf->start);
  161. kfree(smibuf);
  162. }
  163. }
  164. /*
  165. * Make a call to gsmi func(sub). GSMI error codes are translated to
  166. * in-kernel errnos (0 on success, -ERRNO on error).
  167. */
  168. static int gsmi_exec(u8 func, u8 sub)
  169. {
  170. u16 cmd = (sub << 8) | func;
  171. u16 result = 0;
  172. int rc = 0;
  173. /*
  174. * AH : Subfunction number
  175. * AL : Function number
  176. * EBX : Parameter block address
  177. * DX : SMI command port
  178. *
  179. * Three protocols here. See also the comment in gsmi_init().
  180. */
  181. if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
  182. /*
  183. * If handshake_type == HANDSHAKE_CF then set CF on the
  184. * way in and wait for the handler to clear it; this avoids
  185. * corrupting register state on those chipsets which have
  186. * a delay between writing the SMI trigger register and
  187. * entering SMM.
  188. */
  189. asm volatile (
  190. "stc\n"
  191. "outb %%al, %%dx\n"
  192. "1: jc 1b\n"
  193. : "=a" (result)
  194. : "0" (cmd),
  195. "d" (gsmi_dev.smi_cmd),
  196. "b" (gsmi_dev.param_buf->address)
  197. : "memory", "cc"
  198. );
  199. } else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
  200. /*
  201. * If handshake_type == HANDSHAKE_SPIN we spin a
  202. * hundred-ish usecs to ensure the SMI has triggered.
  203. */
  204. asm volatile (
  205. "outb %%al, %%dx\n"
  206. "1: loop 1b\n"
  207. : "=a" (result)
  208. : "0" (cmd),
  209. "d" (gsmi_dev.smi_cmd),
  210. "b" (gsmi_dev.param_buf->address),
  211. "c" (spincount)
  212. : "memory", "cc"
  213. );
  214. } else {
  215. /*
  216. * If handshake_type == HANDSHAKE_NONE we do nothing;
  217. * either we don't need to or it's legacy firmware that
  218. * doesn't understand the CF protocol.
  219. */
  220. asm volatile (
  221. "outb %%al, %%dx\n\t"
  222. : "=a" (result)
  223. : "0" (cmd),
  224. "d" (gsmi_dev.smi_cmd),
  225. "b" (gsmi_dev.param_buf->address)
  226. : "memory", "cc"
  227. );
  228. }
  229. /* check return code from SMI handler */
  230. switch (result) {
  231. case GSMI_SUCCESS:
  232. break;
  233. case GSMI_VAR_NOT_FOUND:
  234. /* not really an error, but let the caller know */
  235. rc = 1;
  236. break;
  237. case GSMI_INVALID_PARAMETER:
  238. printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
  239. rc = -EINVAL;
  240. break;
  241. case GSMI_BUFFER_TOO_SMALL:
  242. printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
  243. rc = -ENOMEM;
  244. break;
  245. case GSMI_UNSUPPORTED:
  246. case GSMI_UNSUPPORTED2:
  247. if (sub != GSMI_CMD_HANDSHAKE_TYPE)
  248. printk(KERN_ERR "gsmi: exec 0x%04x: Not supported\n",
  249. cmd);
  250. rc = -ENOSYS;
  251. break;
  252. case GSMI_NOT_READY:
  253. printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
  254. rc = -EBUSY;
  255. break;
  256. case GSMI_DEVICE_ERROR:
  257. printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
  258. rc = -EFAULT;
  259. break;
  260. case GSMI_NOT_FOUND:
  261. printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
  262. rc = -ENOENT;
  263. break;
  264. case GSMI_LOG_FULL:
  265. printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
  266. rc = -ENOSPC;
  267. break;
  268. case GSMI_HANDSHAKE_CF:
  269. case GSMI_HANDSHAKE_SPIN:
  270. case GSMI_HANDSHAKE_NONE:
  271. rc = result;
  272. break;
  273. default:
  274. printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
  275. cmd, result);
  276. rc = -ENXIO;
  277. }
  278. return rc;
  279. }
  280. #ifdef CONFIG_EFI
  281. static struct efivars efivars;
  282. static efi_status_t gsmi_get_variable(efi_char16_t *name,
  283. efi_guid_t *vendor, u32 *attr,
  284. unsigned long *data_size,
  285. void *data)
  286. {
  287. struct gsmi_nvram_var_param param = {
  288. .name_ptr = gsmi_dev.name_buf->address,
  289. .data_ptr = gsmi_dev.data_buf->address,
  290. .data_len = (u32)*data_size,
  291. };
  292. efi_status_t ret = EFI_SUCCESS;
  293. unsigned long flags;
  294. size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
  295. int rc;
  296. if (name_len >= GSMI_BUF_SIZE / 2)
  297. return EFI_BAD_BUFFER_SIZE;
  298. spin_lock_irqsave(&gsmi_dev.lock, flags);
  299. /* Vendor guid */
  300. memcpy(&param.guid, vendor, sizeof(param.guid));
  301. /* variable name, already in UTF-16 */
  302. memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
  303. memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
  304. /* data pointer */
  305. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  306. /* parameter buffer */
  307. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  308. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  309. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
  310. if (rc < 0) {
  311. printk(KERN_ERR "gsmi: Get Variable failed\n");
  312. ret = EFI_LOAD_ERROR;
  313. } else if (rc == 1) {
  314. /* variable was not found */
  315. ret = EFI_NOT_FOUND;
  316. } else {
  317. /* Get the arguments back */
  318. memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
  319. /* The size reported is the min of all of our buffers */
  320. *data_size = min_t(unsigned long, *data_size,
  321. gsmi_dev.data_buf->length);
  322. *data_size = min_t(unsigned long, *data_size, param.data_len);
  323. /* Copy data back to return buffer. */
  324. memcpy(data, gsmi_dev.data_buf->start, *data_size);
  325. /* All variables are have the following attributes */
  326. if (attr)
  327. *attr = EFI_VARIABLE_NON_VOLATILE |
  328. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  329. EFI_VARIABLE_RUNTIME_ACCESS;
  330. }
  331. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  332. return ret;
  333. }
  334. static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
  335. efi_char16_t *name,
  336. efi_guid_t *vendor)
  337. {
  338. struct gsmi_get_next_var_param param = {
  339. .name_ptr = gsmi_dev.name_buf->address,
  340. .name_len = gsmi_dev.name_buf->length,
  341. };
  342. efi_status_t ret = EFI_SUCCESS;
  343. int rc;
  344. unsigned long flags;
  345. /* For the moment, only support buffers that exactly match in size */
  346. if (*name_size != GSMI_BUF_SIZE)
  347. return EFI_BAD_BUFFER_SIZE;
  348. /* Let's make sure the thing is at least null-terminated */
  349. if (ucs2_strnlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
  350. return EFI_INVALID_PARAMETER;
  351. spin_lock_irqsave(&gsmi_dev.lock, flags);
  352. /* guid */
  353. memcpy(&param.guid, vendor, sizeof(param.guid));
  354. /* variable name, already in UTF-16 */
  355. memcpy(gsmi_dev.name_buf->start, name, *name_size);
  356. /* parameter buffer */
  357. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  358. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  359. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
  360. if (rc < 0) {
  361. printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
  362. ret = EFI_LOAD_ERROR;
  363. } else if (rc == 1) {
  364. /* variable not found -- end of list */
  365. ret = EFI_NOT_FOUND;
  366. } else {
  367. /* copy variable data back to return buffer */
  368. memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
  369. /* Copy the name back */
  370. memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
  371. *name_size = ucs2_strnlen(name, GSMI_BUF_SIZE / 2) * 2;
  372. /* copy guid to return buffer */
  373. memcpy(vendor, &param.guid, sizeof(param.guid));
  374. ret = EFI_SUCCESS;
  375. }
  376. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  377. return ret;
  378. }
  379. static efi_status_t gsmi_set_variable(efi_char16_t *name,
  380. efi_guid_t *vendor,
  381. u32 attr,
  382. unsigned long data_size,
  383. void *data)
  384. {
  385. struct gsmi_nvram_var_param param = {
  386. .name_ptr = gsmi_dev.name_buf->address,
  387. .data_ptr = gsmi_dev.data_buf->address,
  388. .data_len = (u32)data_size,
  389. .attributes = EFI_VARIABLE_NON_VOLATILE |
  390. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  391. EFI_VARIABLE_RUNTIME_ACCESS,
  392. };
  393. size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
  394. efi_status_t ret = EFI_SUCCESS;
  395. int rc;
  396. unsigned long flags;
  397. if (name_len >= GSMI_BUF_SIZE / 2)
  398. return EFI_BAD_BUFFER_SIZE;
  399. spin_lock_irqsave(&gsmi_dev.lock, flags);
  400. /* guid */
  401. memcpy(&param.guid, vendor, sizeof(param.guid));
  402. /* variable name, already in UTF-16 */
  403. memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
  404. memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
  405. /* data pointer */
  406. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  407. memcpy(gsmi_dev.data_buf->start, data, data_size);
  408. /* parameter buffer */
  409. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  410. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  411. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
  412. if (rc < 0) {
  413. printk(KERN_ERR "gsmi: Set Variable failed\n");
  414. ret = EFI_INVALID_PARAMETER;
  415. }
  416. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  417. return ret;
  418. }
  419. static const struct efivar_operations efivar_ops = {
  420. .get_variable = gsmi_get_variable,
  421. .set_variable = gsmi_set_variable,
  422. .get_next_variable = gsmi_get_next_variable,
  423. };
  424. #endif /* CONFIG_EFI */
  425. static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
  426. struct bin_attribute *bin_attr,
  427. char *buf, loff_t pos, size_t count)
  428. {
  429. struct gsmi_set_eventlog_param param = {
  430. .data_ptr = gsmi_dev.data_buf->address,
  431. };
  432. int rc = 0;
  433. unsigned long flags;
  434. /* Pull the type out */
  435. if (count < sizeof(u32))
  436. return -EINVAL;
  437. param.type = *(u32 *)buf;
  438. buf += sizeof(u32);
  439. /* The remaining buffer is the data payload */
  440. if ((count - sizeof(u32)) > gsmi_dev.data_buf->length)
  441. return -EINVAL;
  442. param.data_len = count - sizeof(u32);
  443. spin_lock_irqsave(&gsmi_dev.lock, flags);
  444. /* data pointer */
  445. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  446. memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
  447. /* parameter buffer */
  448. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  449. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  450. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
  451. if (rc < 0)
  452. printk(KERN_ERR "gsmi: Set Event Log failed\n");
  453. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  454. return (rc == 0) ? count : rc;
  455. }
  456. static struct bin_attribute eventlog_bin_attr = {
  457. .attr = {.name = "append_to_eventlog", .mode = 0200},
  458. .write = eventlog_write,
  459. };
  460. static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
  461. struct kobj_attribute *attr,
  462. const char *buf, size_t count)
  463. {
  464. int rc;
  465. unsigned long flags;
  466. unsigned long val;
  467. struct {
  468. u32 percentage;
  469. u32 data_type;
  470. } param;
  471. rc = kstrtoul(buf, 0, &val);
  472. if (rc)
  473. return rc;
  474. /*
  475. * Value entered is a percentage, 0 through 100, anything else
  476. * is invalid.
  477. */
  478. if (val > 100)
  479. return -EINVAL;
  480. /* data_type here selects the smbios event log. */
  481. param.percentage = val;
  482. param.data_type = 0;
  483. spin_lock_irqsave(&gsmi_dev.lock, flags);
  484. /* parameter buffer */
  485. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  486. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  487. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
  488. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  489. if (rc)
  490. return rc;
  491. return count;
  492. }
  493. static struct kobj_attribute gsmi_clear_eventlog_attr = {
  494. .attr = {.name = "clear_eventlog", .mode = 0200},
  495. .store = gsmi_clear_eventlog_store,
  496. };
  497. static ssize_t gsmi_clear_config_store(struct kobject *kobj,
  498. struct kobj_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. int rc;
  502. unsigned long flags;
  503. spin_lock_irqsave(&gsmi_dev.lock, flags);
  504. /* clear parameter buffer */
  505. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  506. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
  507. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  508. if (rc)
  509. return rc;
  510. return count;
  511. }
  512. static struct kobj_attribute gsmi_clear_config_attr = {
  513. .attr = {.name = "clear_config", .mode = 0200},
  514. .store = gsmi_clear_config_store,
  515. };
  516. static const struct attribute *gsmi_attrs[] = {
  517. &gsmi_clear_config_attr.attr,
  518. &gsmi_clear_eventlog_attr.attr,
  519. NULL,
  520. };
  521. static int gsmi_shutdown_reason(int reason)
  522. {
  523. struct gsmi_log_entry_type_1 entry = {
  524. .type = GSMI_LOG_ENTRY_TYPE_KERNEL,
  525. .instance = reason,
  526. };
  527. struct gsmi_set_eventlog_param param = {
  528. .data_len = sizeof(entry),
  529. .type = 1,
  530. };
  531. static int saved_reason;
  532. int rc = 0;
  533. unsigned long flags;
  534. /* avoid duplicate entries in the log */
  535. if (saved_reason & (1 << reason))
  536. return 0;
  537. spin_lock_irqsave(&gsmi_dev.lock, flags);
  538. saved_reason |= (1 << reason);
  539. /* data pointer */
  540. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  541. memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
  542. /* parameter buffer */
  543. param.data_ptr = gsmi_dev.data_buf->address;
  544. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  545. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  546. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
  547. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  548. if (rc < 0)
  549. printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
  550. else
  551. printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
  552. reason);
  553. return rc;
  554. }
  555. static int gsmi_reboot_callback(struct notifier_block *nb,
  556. unsigned long reason, void *arg)
  557. {
  558. gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
  559. return NOTIFY_DONE;
  560. }
  561. static struct notifier_block gsmi_reboot_notifier = {
  562. .notifier_call = gsmi_reboot_callback
  563. };
  564. static int gsmi_die_callback(struct notifier_block *nb,
  565. unsigned long reason, void *arg)
  566. {
  567. if (reason == DIE_OOPS)
  568. gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
  569. return NOTIFY_DONE;
  570. }
  571. static struct notifier_block gsmi_die_notifier = {
  572. .notifier_call = gsmi_die_callback
  573. };
  574. static int gsmi_panic_callback(struct notifier_block *nb,
  575. unsigned long reason, void *arg)
  576. {
  577. /*
  578. * Panic callbacks are executed with all other CPUs stopped,
  579. * so we must not attempt to spin waiting for gsmi_dev.lock
  580. * to be released.
  581. */
  582. if (spin_is_locked(&gsmi_dev.lock))
  583. return NOTIFY_DONE;
  584. gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
  585. return NOTIFY_DONE;
  586. }
  587. static struct notifier_block gsmi_panic_notifier = {
  588. .notifier_call = gsmi_panic_callback,
  589. };
  590. /*
  591. * This hash function was blatantly copied from include/linux/hash.h.
  592. * It is used by this driver to obfuscate a board name that requires a
  593. * quirk within this driver.
  594. *
  595. * Please do not remove this copy of the function as any changes to the
  596. * global utility hash_64() function would break this driver's ability
  597. * to identify a board and provide the appropriate quirk -- [email protected]
  598. */
  599. static u64 __init local_hash_64(u64 val, unsigned bits)
  600. {
  601. u64 hash = val;
  602. /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
  603. u64 n = hash;
  604. n <<= 18;
  605. hash -= n;
  606. n <<= 33;
  607. hash -= n;
  608. n <<= 3;
  609. hash += n;
  610. n <<= 3;
  611. hash -= n;
  612. n <<= 4;
  613. hash += n;
  614. n <<= 2;
  615. hash += n;
  616. /* High bits are more random, so use them. */
  617. return hash >> (64 - bits);
  618. }
  619. static u32 __init hash_oem_table_id(char s[8])
  620. {
  621. u64 input;
  622. memcpy(&input, s, 8);
  623. return local_hash_64(input, 32);
  624. }
  625. static const struct dmi_system_id gsmi_dmi_table[] __initconst = {
  626. {
  627. .ident = "Google Board",
  628. .matches = {
  629. DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
  630. },
  631. },
  632. {
  633. .ident = "Coreboot Firmware",
  634. .matches = {
  635. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  636. },
  637. },
  638. {}
  639. };
  640. MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
  641. static __init int gsmi_system_valid(void)
  642. {
  643. u32 hash;
  644. u16 cmd, result;
  645. if (!dmi_check_system(gsmi_dmi_table))
  646. return -ENODEV;
  647. /*
  648. * Only newer firmware supports the gsmi interface. All older
  649. * firmware that didn't support this interface used to plug the
  650. * table name in the first four bytes of the oem_table_id field.
  651. * Newer firmware doesn't do that though, so use that as the
  652. * discriminant factor. We have to do this in order to
  653. * whitewash our board names out of the public driver.
  654. */
  655. if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
  656. printk(KERN_INFO "gsmi: Board is too old\n");
  657. return -ENODEV;
  658. }
  659. /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
  660. hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
  661. if (hash == QUIRKY_BOARD_HASH) {
  662. const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
  663. if (strncmp(bios_ver, "1.0", 3) == 0) {
  664. pr_info("gsmi: disabled on this board's BIOS %s\n",
  665. bios_ver);
  666. return -ENODEV;
  667. }
  668. }
  669. /* check for valid SMI command port in ACPI FADT */
  670. if (acpi_gbl_FADT.smi_command == 0) {
  671. pr_info("gsmi: missing smi_command\n");
  672. return -ENODEV;
  673. }
  674. /* Test the smihandler with a bogus command. If it leaves the
  675. * calling argument in %ax untouched, there is no handler for
  676. * GSMI commands.
  677. */
  678. cmd = GSMI_CALLBACK | GSMI_CMD_RESERVED << 8;
  679. asm volatile (
  680. "outb %%al, %%dx\n\t"
  681. : "=a" (result)
  682. : "0" (cmd),
  683. "d" (acpi_gbl_FADT.smi_command)
  684. : "memory", "cc"
  685. );
  686. if (cmd == result) {
  687. pr_info("gsmi: no gsmi handler in firmware\n");
  688. return -ENODEV;
  689. }
  690. /* Found */
  691. return 0;
  692. }
  693. static struct kobject *gsmi_kobj;
  694. static const struct platform_device_info gsmi_dev_info = {
  695. .name = "gsmi",
  696. .id = -1,
  697. /* SMI callbacks require 32bit addresses */
  698. .dma_mask = DMA_BIT_MASK(32),
  699. };
  700. #ifdef CONFIG_PM
  701. static void gsmi_log_s0ix_info(u8 cmd)
  702. {
  703. unsigned long flags;
  704. /*
  705. * If platform has not enabled S0ix logging, then no action is
  706. * necessary.
  707. */
  708. if (!s0ix_logging_enable)
  709. return;
  710. spin_lock_irqsave(&gsmi_dev.lock, flags);
  711. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  712. gsmi_exec(GSMI_CALLBACK, cmd);
  713. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  714. }
  715. static int gsmi_log_s0ix_suspend(struct device *dev)
  716. {
  717. /*
  718. * If system is not suspending via firmware using the standard ACPI Sx
  719. * types, then make a GSMI call to log the suspend info.
  720. */
  721. if (!pm_suspend_via_firmware())
  722. gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND);
  723. /*
  724. * Always return success, since we do not want suspend
  725. * to fail just because of logging failure.
  726. */
  727. return 0;
  728. }
  729. static int gsmi_log_s0ix_resume(struct device *dev)
  730. {
  731. /*
  732. * If system did not resume via firmware, then make a GSMI call to log
  733. * the resume info and wake source.
  734. */
  735. if (!pm_resume_via_firmware())
  736. gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME);
  737. /*
  738. * Always return success, since we do not want resume
  739. * to fail just because of logging failure.
  740. */
  741. return 0;
  742. }
  743. static const struct dev_pm_ops gsmi_pm_ops = {
  744. .suspend_noirq = gsmi_log_s0ix_suspend,
  745. .resume_noirq = gsmi_log_s0ix_resume,
  746. };
  747. static int gsmi_platform_driver_probe(struct platform_device *dev)
  748. {
  749. return 0;
  750. }
  751. static struct platform_driver gsmi_driver_info = {
  752. .driver = {
  753. .name = "gsmi",
  754. .pm = &gsmi_pm_ops,
  755. },
  756. .probe = gsmi_platform_driver_probe,
  757. };
  758. #endif
  759. static __init int gsmi_init(void)
  760. {
  761. unsigned long flags;
  762. int ret;
  763. ret = gsmi_system_valid();
  764. if (ret)
  765. return ret;
  766. gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
  767. #ifdef CONFIG_PM
  768. ret = platform_driver_register(&gsmi_driver_info);
  769. if (unlikely(ret)) {
  770. printk(KERN_ERR "gsmi: unable to register platform driver\n");
  771. return ret;
  772. }
  773. #endif
  774. /* register device */
  775. gsmi_dev.pdev = platform_device_register_full(&gsmi_dev_info);
  776. if (IS_ERR(gsmi_dev.pdev)) {
  777. printk(KERN_ERR "gsmi: unable to register platform device\n");
  778. return PTR_ERR(gsmi_dev.pdev);
  779. }
  780. /* SMI access needs to be serialized */
  781. spin_lock_init(&gsmi_dev.lock);
  782. ret = -ENOMEM;
  783. /*
  784. * SLAB cache is created using SLAB_CACHE_DMA32 to ensure that the
  785. * allocations for gsmi_buf come from the DMA32 memory zone. These
  786. * buffers have nothing to do with DMA. They are required for
  787. * communication with firmware executing in SMI mode which can only
  788. * access the bottom 4GiB of physical memory. Since DMA32 memory zone
  789. * guarantees allocation under the 4GiB boundary, this driver creates
  790. * a SLAB cache with SLAB_CACHE_DMA32 flag.
  791. */
  792. gsmi_dev.mem_pool = kmem_cache_create("gsmi", GSMI_BUF_SIZE,
  793. GSMI_BUF_ALIGN,
  794. SLAB_CACHE_DMA32, NULL);
  795. if (!gsmi_dev.mem_pool)
  796. goto out_err;
  797. /*
  798. * pre-allocate buffers because sometimes we are called when
  799. * this is not feasible: oops, panic, die, mce, etc
  800. */
  801. gsmi_dev.name_buf = gsmi_buf_alloc();
  802. if (!gsmi_dev.name_buf) {
  803. printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
  804. goto out_err;
  805. }
  806. gsmi_dev.data_buf = gsmi_buf_alloc();
  807. if (!gsmi_dev.data_buf) {
  808. printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
  809. goto out_err;
  810. }
  811. gsmi_dev.param_buf = gsmi_buf_alloc();
  812. if (!gsmi_dev.param_buf) {
  813. printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
  814. goto out_err;
  815. }
  816. /*
  817. * Determine type of handshake used to serialize the SMI
  818. * entry. See also gsmi_exec().
  819. *
  820. * There's a "behavior" present on some chipsets where writing the
  821. * SMI trigger register in the southbridge doesn't result in an
  822. * immediate SMI. Rather, the processor can execute "a few" more
  823. * instructions before the SMI takes effect. To ensure synchronous
  824. * behavior, implement a handshake between the kernel driver and the
  825. * firmware handler to spin until released. This ioctl determines
  826. * the type of handshake.
  827. *
  828. * NONE: The firmware handler does not implement any
  829. * handshake. Either it doesn't need to, or it's legacy firmware
  830. * that doesn't know it needs to and never will.
  831. *
  832. * CF: The firmware handler will clear the CF in the saved
  833. * state before returning. The driver may set the CF and test for
  834. * it to clear before proceeding.
  835. *
  836. * SPIN: The firmware handler does not implement any handshake
  837. * but the driver should spin for a hundred or so microseconds
  838. * to ensure the SMI has triggered.
  839. *
  840. * Finally, the handler will return -ENOSYS if
  841. * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
  842. * HANDSHAKE_NONE.
  843. */
  844. spin_lock_irqsave(&gsmi_dev.lock, flags);
  845. gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
  846. gsmi_dev.handshake_type =
  847. gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
  848. if (gsmi_dev.handshake_type == -ENOSYS)
  849. gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
  850. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  851. /* Remove and clean up gsmi if the handshake could not complete. */
  852. if (gsmi_dev.handshake_type == -ENXIO) {
  853. printk(KERN_INFO "gsmi version " DRIVER_VERSION
  854. " failed to load\n");
  855. ret = -ENODEV;
  856. goto out_err;
  857. }
  858. /* Register in the firmware directory */
  859. ret = -ENOMEM;
  860. gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
  861. if (!gsmi_kobj) {
  862. printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
  863. goto out_err;
  864. }
  865. /* Setup eventlog access */
  866. ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
  867. if (ret) {
  868. printk(KERN_INFO "gsmi: Failed to setup eventlog");
  869. goto out_err;
  870. }
  871. /* Other attributes */
  872. ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
  873. if (ret) {
  874. printk(KERN_INFO "gsmi: Failed to add attrs");
  875. goto out_remove_bin_file;
  876. }
  877. #ifdef CONFIG_EFI
  878. ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
  879. if (ret) {
  880. printk(KERN_INFO "gsmi: Failed to register efivars\n");
  881. sysfs_remove_files(gsmi_kobj, gsmi_attrs);
  882. goto out_remove_bin_file;
  883. }
  884. #endif
  885. register_reboot_notifier(&gsmi_reboot_notifier);
  886. register_die_notifier(&gsmi_die_notifier);
  887. atomic_notifier_chain_register(&panic_notifier_list,
  888. &gsmi_panic_notifier);
  889. printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
  890. return 0;
  891. out_remove_bin_file:
  892. sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
  893. out_err:
  894. kobject_put(gsmi_kobj);
  895. gsmi_buf_free(gsmi_dev.param_buf);
  896. gsmi_buf_free(gsmi_dev.data_buf);
  897. gsmi_buf_free(gsmi_dev.name_buf);
  898. kmem_cache_destroy(gsmi_dev.mem_pool);
  899. platform_device_unregister(gsmi_dev.pdev);
  900. pr_info("gsmi: failed to load: %d\n", ret);
  901. #ifdef CONFIG_PM
  902. platform_driver_unregister(&gsmi_driver_info);
  903. #endif
  904. return ret;
  905. }
  906. static void __exit gsmi_exit(void)
  907. {
  908. unregister_reboot_notifier(&gsmi_reboot_notifier);
  909. unregister_die_notifier(&gsmi_die_notifier);
  910. atomic_notifier_chain_unregister(&panic_notifier_list,
  911. &gsmi_panic_notifier);
  912. #ifdef CONFIG_EFI
  913. efivars_unregister(&efivars);
  914. #endif
  915. sysfs_remove_files(gsmi_kobj, gsmi_attrs);
  916. sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
  917. kobject_put(gsmi_kobj);
  918. gsmi_buf_free(gsmi_dev.param_buf);
  919. gsmi_buf_free(gsmi_dev.data_buf);
  920. gsmi_buf_free(gsmi_dev.name_buf);
  921. kmem_cache_destroy(gsmi_dev.mem_pool);
  922. platform_device_unregister(gsmi_dev.pdev);
  923. #ifdef CONFIG_PM
  924. platform_driver_unregister(&gsmi_driver_info);
  925. #endif
  926. }
  927. module_init(gsmi_init);
  928. module_exit(gsmi_exit);
  929. MODULE_AUTHOR("Google, Inc.");
  930. MODULE_LICENSE("GPL");