gsmi.c 27 KB

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