gsmi.c 25 KB

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