gsmi.c 24 KB

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