gsmi.c 25 KB

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