gsmi.c 27 KB

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