gsmi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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_t(unsigned long, *data_size,
  313. gsmi_dev.data_buf->length);
  314. *data_size = min_t(unsigned long, *data_size, param.data_len);
  315. /* Copy data back to return buffer. */
  316. memcpy(data, gsmi_dev.data_buf->start, *data_size);
  317. /* All variables are have the following attributes */
  318. *attr = EFI_VARIABLE_NON_VOLATILE |
  319. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  320. EFI_VARIABLE_RUNTIME_ACCESS;
  321. }
  322. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  323. return ret;
  324. }
  325. static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
  326. efi_char16_t *name,
  327. efi_guid_t *vendor)
  328. {
  329. struct gsmi_get_next_var_param param = {
  330. .name_ptr = gsmi_dev.name_buf->address,
  331. .name_len = gsmi_dev.name_buf->length,
  332. };
  333. efi_status_t ret = EFI_SUCCESS;
  334. int rc;
  335. unsigned long flags;
  336. /* For the moment, only support buffers that exactly match in size */
  337. if (*name_size != GSMI_BUF_SIZE)
  338. return EFI_BAD_BUFFER_SIZE;
  339. /* Let's make sure the thing is at least null-terminated */
  340. if (utf16_strlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
  341. return EFI_INVALID_PARAMETER;
  342. spin_lock_irqsave(&gsmi_dev.lock, flags);
  343. /* guid */
  344. memcpy(&param.guid, vendor, sizeof(param.guid));
  345. /* variable name, already in UTF-16 */
  346. memcpy(gsmi_dev.name_buf->start, name, *name_size);
  347. /* parameter buffer */
  348. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  349. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  350. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
  351. if (rc < 0) {
  352. printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
  353. ret = EFI_LOAD_ERROR;
  354. } else if (rc == 1) {
  355. /* variable not found -- end of list */
  356. ret = EFI_NOT_FOUND;
  357. } else {
  358. /* copy variable data back to return buffer */
  359. memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
  360. /* Copy the name back */
  361. memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
  362. *name_size = utf16_strlen(name, GSMI_BUF_SIZE / 2) * 2;
  363. /* copy guid to return buffer */
  364. memcpy(vendor, &param.guid, sizeof(param.guid));
  365. ret = EFI_SUCCESS;
  366. }
  367. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  368. return ret;
  369. }
  370. static efi_status_t gsmi_set_variable(efi_char16_t *name,
  371. efi_guid_t *vendor,
  372. u32 attr,
  373. unsigned long data_size,
  374. void *data)
  375. {
  376. struct gsmi_nvram_var_param param = {
  377. .name_ptr = gsmi_dev.name_buf->address,
  378. .data_ptr = gsmi_dev.data_buf->address,
  379. .data_len = (u32)data_size,
  380. .attributes = EFI_VARIABLE_NON_VOLATILE |
  381. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  382. EFI_VARIABLE_RUNTIME_ACCESS,
  383. };
  384. size_t name_len = utf16_strlen(name, GSMI_BUF_SIZE / 2);
  385. efi_status_t ret = EFI_SUCCESS;
  386. int rc;
  387. unsigned long flags;
  388. if (name_len >= GSMI_BUF_SIZE / 2)
  389. return EFI_BAD_BUFFER_SIZE;
  390. spin_lock_irqsave(&gsmi_dev.lock, flags);
  391. /* guid */
  392. memcpy(&param.guid, vendor, sizeof(param.guid));
  393. /* variable name, already in UTF-16 */
  394. memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
  395. memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
  396. /* data pointer */
  397. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  398. memcpy(gsmi_dev.data_buf->start, data, data_size);
  399. /* parameter buffer */
  400. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  401. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  402. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
  403. if (rc < 0) {
  404. printk(KERN_ERR "gsmi: Set Variable failed\n");
  405. ret = EFI_INVALID_PARAMETER;
  406. }
  407. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  408. return ret;
  409. }
  410. static const struct efivar_operations efivar_ops = {
  411. .get_variable = gsmi_get_variable,
  412. .set_variable = gsmi_set_variable,
  413. .get_next_variable = gsmi_get_next_variable,
  414. };
  415. static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
  416. struct bin_attribute *bin_attr,
  417. char *buf, loff_t pos, size_t count)
  418. {
  419. struct gsmi_set_eventlog_param param = {
  420. .data_ptr = gsmi_dev.data_buf->address,
  421. };
  422. int rc = 0;
  423. unsigned long flags;
  424. /* Pull the type out */
  425. if (count < sizeof(u32))
  426. return -EINVAL;
  427. param.type = *(u32 *)buf;
  428. count -= sizeof(u32);
  429. buf += sizeof(u32);
  430. /* The remaining buffer is the data payload */
  431. if (count > gsmi_dev.data_buf->length)
  432. return -EINVAL;
  433. param.data_len = count - sizeof(u32);
  434. spin_lock_irqsave(&gsmi_dev.lock, flags);
  435. /* data pointer */
  436. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  437. memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
  438. /* parameter buffer */
  439. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  440. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  441. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
  442. if (rc < 0)
  443. printk(KERN_ERR "gsmi: Set Event Log failed\n");
  444. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  445. return rc;
  446. }
  447. static struct bin_attribute eventlog_bin_attr = {
  448. .attr = {.name = "append_to_eventlog", .mode = 0200},
  449. .write = eventlog_write,
  450. };
  451. static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
  452. struct kobj_attribute *attr,
  453. const char *buf, size_t count)
  454. {
  455. int rc;
  456. unsigned long flags;
  457. unsigned long val;
  458. struct {
  459. u32 percentage;
  460. u32 data_type;
  461. } param;
  462. rc = strict_strtoul(buf, 0, &val);
  463. if (rc)
  464. return rc;
  465. /*
  466. * Value entered is a percentage, 0 through 100, anything else
  467. * is invalid.
  468. */
  469. if (val > 100)
  470. return -EINVAL;
  471. /* data_type here selects the smbios event log. */
  472. param.percentage = val;
  473. param.data_type = 0;
  474. spin_lock_irqsave(&gsmi_dev.lock, flags);
  475. /* parameter buffer */
  476. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  477. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  478. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
  479. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  480. if (rc)
  481. return rc;
  482. return count;
  483. }
  484. static struct kobj_attribute gsmi_clear_eventlog_attr = {
  485. .attr = {.name = "clear_eventlog", .mode = 0200},
  486. .store = gsmi_clear_eventlog_store,
  487. };
  488. static ssize_t gsmi_clear_config_store(struct kobject *kobj,
  489. struct kobj_attribute *attr,
  490. const char *buf, size_t count)
  491. {
  492. int rc;
  493. unsigned long flags;
  494. spin_lock_irqsave(&gsmi_dev.lock, flags);
  495. /* clear parameter buffer */
  496. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  497. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
  498. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  499. if (rc)
  500. return rc;
  501. return count;
  502. }
  503. static struct kobj_attribute gsmi_clear_config_attr = {
  504. .attr = {.name = "clear_config", .mode = 0200},
  505. .store = gsmi_clear_config_store,
  506. };
  507. static const struct attribute *gsmi_attrs[] = {
  508. &gsmi_clear_config_attr.attr,
  509. &gsmi_clear_eventlog_attr.attr,
  510. NULL,
  511. };
  512. static int gsmi_shutdown_reason(int reason)
  513. {
  514. struct gsmi_log_entry_type_1 entry = {
  515. .type = GSMI_LOG_ENTRY_TYPE_KERNEL,
  516. .instance = reason,
  517. };
  518. struct gsmi_set_eventlog_param param = {
  519. .data_len = sizeof(entry),
  520. .type = 1,
  521. };
  522. static int saved_reason;
  523. int rc = 0;
  524. unsigned long flags;
  525. /* avoid duplicate entries in the log */
  526. if (saved_reason & (1 << reason))
  527. return 0;
  528. spin_lock_irqsave(&gsmi_dev.lock, flags);
  529. saved_reason |= (1 << reason);
  530. /* data pointer */
  531. memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
  532. memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
  533. /* parameter buffer */
  534. param.data_ptr = gsmi_dev.data_buf->address;
  535. memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
  536. memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
  537. rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
  538. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  539. if (rc < 0)
  540. printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
  541. else
  542. printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
  543. reason);
  544. return rc;
  545. }
  546. static int gsmi_reboot_callback(struct notifier_block *nb,
  547. unsigned long reason, void *arg)
  548. {
  549. gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
  550. return NOTIFY_DONE;
  551. }
  552. static struct notifier_block gsmi_reboot_notifier = {
  553. .notifier_call = gsmi_reboot_callback
  554. };
  555. static int gsmi_die_callback(struct notifier_block *nb,
  556. unsigned long reason, void *arg)
  557. {
  558. if (reason == DIE_OOPS)
  559. gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
  560. return NOTIFY_DONE;
  561. }
  562. static struct notifier_block gsmi_die_notifier = {
  563. .notifier_call = gsmi_die_callback
  564. };
  565. static int gsmi_panic_callback(struct notifier_block *nb,
  566. unsigned long reason, void *arg)
  567. {
  568. gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
  569. return NOTIFY_DONE;
  570. }
  571. static struct notifier_block gsmi_panic_notifier = {
  572. .notifier_call = gsmi_panic_callback,
  573. };
  574. /*
  575. * This hash function was blatantly copied from include/linux/hash.h.
  576. * It is used by this driver to obfuscate a board name that requires a
  577. * quirk within this driver.
  578. *
  579. * Please do not remove this copy of the function as any changes to the
  580. * global utility hash_64() function would break this driver's ability
  581. * to identify a board and provide the appropriate quirk -- mikew@google.com
  582. */
  583. static u64 __init local_hash_64(u64 val, unsigned bits)
  584. {
  585. u64 hash = val;
  586. /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
  587. u64 n = hash;
  588. n <<= 18;
  589. hash -= n;
  590. n <<= 33;
  591. hash -= n;
  592. n <<= 3;
  593. hash += n;
  594. n <<= 3;
  595. hash -= n;
  596. n <<= 4;
  597. hash += n;
  598. n <<= 2;
  599. hash += n;
  600. /* High bits are more random, so use them. */
  601. return hash >> (64 - bits);
  602. }
  603. static u32 __init hash_oem_table_id(char s[8])
  604. {
  605. u64 input;
  606. memcpy(&input, s, 8);
  607. return local_hash_64(input, 32);
  608. }
  609. static struct dmi_system_id gsmi_dmi_table[] __initdata = {
  610. {
  611. .ident = "Google Board",
  612. .matches = {
  613. DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
  614. },
  615. },
  616. {}
  617. };
  618. MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
  619. static __init int gsmi_system_valid(void)
  620. {
  621. u32 hash;
  622. if (!dmi_check_system(gsmi_dmi_table))
  623. return -ENODEV;
  624. /*
  625. * Only newer firmware supports the gsmi interface. All older
  626. * firmware that didn't support this interface used to plug the
  627. * table name in the first four bytes of the oem_table_id field.
  628. * Newer firmware doesn't do that though, so use that as the
  629. * discriminant factor. We have to do this in order to
  630. * whitewash our board names out of the public driver.
  631. */
  632. if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
  633. printk(KERN_INFO "gsmi: Board is too old\n");
  634. return -ENODEV;
  635. }
  636. /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
  637. hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
  638. if (hash == QUIRKY_BOARD_HASH) {
  639. const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
  640. if (strncmp(bios_ver, "1.0", 3) == 0) {
  641. pr_info("gsmi: disabled on this board's BIOS %s\n",
  642. bios_ver);
  643. return -ENODEV;
  644. }
  645. }
  646. /* check for valid SMI command port in ACPI FADT */
  647. if (acpi_gbl_FADT.smi_command == 0) {
  648. pr_info("gsmi: missing smi_command\n");
  649. return -ENODEV;
  650. }
  651. /* Found */
  652. return 0;
  653. }
  654. static struct kobject *gsmi_kobj;
  655. static struct efivars efivars;
  656. static __init int gsmi_init(void)
  657. {
  658. unsigned long flags;
  659. int ret;
  660. ret = gsmi_system_valid();
  661. if (ret)
  662. return ret;
  663. gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
  664. /* register device */
  665. gsmi_dev.pdev = platform_device_register_simple("gsmi", -1, NULL, 0);
  666. if (IS_ERR(gsmi_dev.pdev)) {
  667. printk(KERN_ERR "gsmi: unable to register platform device\n");
  668. return PTR_ERR(gsmi_dev.pdev);
  669. }
  670. /* SMI access needs to be serialized */
  671. spin_lock_init(&gsmi_dev.lock);
  672. /* SMI callbacks require 32bit addresses */
  673. gsmi_dev.pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  674. gsmi_dev.pdev->dev.dma_mask =
  675. &gsmi_dev.pdev->dev.coherent_dma_mask;
  676. ret = -ENOMEM;
  677. gsmi_dev.dma_pool = dma_pool_create("gsmi", &gsmi_dev.pdev->dev,
  678. GSMI_BUF_SIZE, GSMI_BUF_ALIGN, 0);
  679. if (!gsmi_dev.dma_pool)
  680. goto out_err;
  681. /*
  682. * pre-allocate buffers because sometimes we are called when
  683. * this is not feasible: oops, panic, die, mce, etc
  684. */
  685. gsmi_dev.name_buf = gsmi_buf_alloc();
  686. if (!gsmi_dev.name_buf) {
  687. printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
  688. goto out_err;
  689. }
  690. gsmi_dev.data_buf = gsmi_buf_alloc();
  691. if (!gsmi_dev.data_buf) {
  692. printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
  693. goto out_err;
  694. }
  695. gsmi_dev.param_buf = gsmi_buf_alloc();
  696. if (!gsmi_dev.param_buf) {
  697. printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
  698. goto out_err;
  699. }
  700. /*
  701. * Determine type of handshake used to serialize the SMI
  702. * entry. See also gsmi_exec().
  703. *
  704. * There's a "behavior" present on some chipsets where writing the
  705. * SMI trigger register in the southbridge doesn't result in an
  706. * immediate SMI. Rather, the processor can execute "a few" more
  707. * instructions before the SMI takes effect. To ensure synchronous
  708. * behavior, implement a handshake between the kernel driver and the
  709. * firmware handler to spin until released. This ioctl determines
  710. * the type of handshake.
  711. *
  712. * NONE: The firmware handler does not implement any
  713. * handshake. Either it doesn't need to, or it's legacy firmware
  714. * that doesn't know it needs to and never will.
  715. *
  716. * CF: The firmware handler will clear the CF in the saved
  717. * state before returning. The driver may set the CF and test for
  718. * it to clear before proceeding.
  719. *
  720. * SPIN: The firmware handler does not implement any handshake
  721. * but the driver should spin for a hundred or so microseconds
  722. * to ensure the SMI has triggered.
  723. *
  724. * Finally, the handler will return -ENOSYS if
  725. * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
  726. * HANDSHAKE_NONE.
  727. */
  728. spin_lock_irqsave(&gsmi_dev.lock, flags);
  729. gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
  730. gsmi_dev.handshake_type =
  731. gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
  732. if (gsmi_dev.handshake_type == -ENOSYS)
  733. gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
  734. spin_unlock_irqrestore(&gsmi_dev.lock, flags);
  735. /* Remove and clean up gsmi if the handshake could not complete. */
  736. if (gsmi_dev.handshake_type == -ENXIO) {
  737. printk(KERN_INFO "gsmi version " DRIVER_VERSION
  738. " failed to load\n");
  739. ret = -ENODEV;
  740. goto out_err;
  741. }
  742. /* Register in the firmware directory */
  743. ret = -ENOMEM;
  744. gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
  745. if (!gsmi_kobj) {
  746. printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
  747. goto out_err;
  748. }
  749. /* Setup eventlog access */
  750. ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
  751. if (ret) {
  752. printk(KERN_INFO "gsmi: Failed to setup eventlog");
  753. goto out_err;
  754. }
  755. /* Other attributes */
  756. ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
  757. if (ret) {
  758. printk(KERN_INFO "gsmi: Failed to add attrs");
  759. goto out_remove_bin_file;
  760. }
  761. ret = register_efivars(&efivars, &efivar_ops, gsmi_kobj);
  762. if (ret) {
  763. printk(KERN_INFO "gsmi: Failed to register efivars\n");
  764. goto out_remove_sysfs_files;
  765. }
  766. register_reboot_notifier(&gsmi_reboot_notifier);
  767. register_die_notifier(&gsmi_die_notifier);
  768. atomic_notifier_chain_register(&panic_notifier_list,
  769. &gsmi_panic_notifier);
  770. printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
  771. return 0;
  772. out_remove_sysfs_files:
  773. sysfs_remove_files(gsmi_kobj, gsmi_attrs);
  774. out_remove_bin_file:
  775. sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
  776. out_err:
  777. kobject_put(gsmi_kobj);
  778. gsmi_buf_free(gsmi_dev.param_buf);
  779. gsmi_buf_free(gsmi_dev.data_buf);
  780. gsmi_buf_free(gsmi_dev.name_buf);
  781. if (gsmi_dev.dma_pool)
  782. dma_pool_destroy(gsmi_dev.dma_pool);
  783. platform_device_unregister(gsmi_dev.pdev);
  784. pr_info("gsmi: failed to load: %d\n", ret);
  785. return ret;
  786. }
  787. static void __exit gsmi_exit(void)
  788. {
  789. unregister_reboot_notifier(&gsmi_reboot_notifier);
  790. unregister_die_notifier(&gsmi_die_notifier);
  791. atomic_notifier_chain_unregister(&panic_notifier_list,
  792. &gsmi_panic_notifier);
  793. unregister_efivars(&efivars);
  794. sysfs_remove_files(gsmi_kobj, gsmi_attrs);
  795. sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
  796. kobject_put(gsmi_kobj);
  797. gsmi_buf_free(gsmi_dev.param_buf);
  798. gsmi_buf_free(gsmi_dev.data_buf);
  799. gsmi_buf_free(gsmi_dev.name_buf);
  800. dma_pool_destroy(gsmi_dev.dma_pool);
  801. platform_device_unregister(gsmi_dev.pdev);
  802. }
  803. module_init(gsmi_init);
  804. module_exit(gsmi_exit);
  805. MODULE_AUTHOR("Google, Inc.");
  806. MODULE_LICENSE("GPL");