x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / firmware / google / gsmi.c
blob6eb535ffeddc2ea7787f648d1d35734f24c305d5
1 /*
2 * Copyright 2010 Google Inc. All Rights Reserved.
3 * Author: dlaurie@google.com (Duncan Laurie)
5 * Re-worked to expose sysfs APIs by mikew@google.com (Mike Waychison)
7 * EFI SMI interface for Google platforms
8 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/device.h>
14 #include <linux/platform_device.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/spinlock.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/dmapool.h>
20 #include <linux/fs.h>
21 #include <linux/slab.h>
22 #include <linux/ioctl.h>
23 #include <linux/acpi.h>
24 #include <linux/io.h>
25 #include <linux/uaccess.h>
26 #include <linux/dmi.h>
27 #include <linux/kdebug.h>
28 #include <linux/reboot.h>
29 #include <linux/efi.h>
30 #include <linux/module.h>
31 #include <linux/ucs2_string.h>
33 #define GSMI_SHUTDOWN_CLEAN 0 /* Clean Shutdown */
34 /* TODO(mikew@google.com): Tie in HARDLOCKUP_DETECTOR with NMIWDT */
35 #define GSMI_SHUTDOWN_NMIWDT 1 /* NMI Watchdog */
36 #define GSMI_SHUTDOWN_PANIC 2 /* Panic */
37 #define GSMI_SHUTDOWN_OOPS 3 /* Oops */
38 #define GSMI_SHUTDOWN_DIE 4 /* Die -- No longer meaningful */
39 #define GSMI_SHUTDOWN_MCE 5 /* Machine Check */
40 #define GSMI_SHUTDOWN_SOFTWDT 6 /* Software Watchdog */
41 #define GSMI_SHUTDOWN_MBE 7 /* Uncorrected ECC */
42 #define GSMI_SHUTDOWN_TRIPLE 8 /* Triple Fault */
44 #define DRIVER_VERSION "1.0"
45 #define GSMI_GUID_SIZE 16
46 #define GSMI_BUF_SIZE 1024
47 #define GSMI_BUF_ALIGN sizeof(u64)
48 #define GSMI_CALLBACK 0xef
50 /* SMI return codes */
51 #define GSMI_SUCCESS 0x00
52 #define GSMI_UNSUPPORTED2 0x03
53 #define GSMI_LOG_FULL 0x0b
54 #define GSMI_VAR_NOT_FOUND 0x0e
55 #define GSMI_HANDSHAKE_SPIN 0x7d
56 #define GSMI_HANDSHAKE_CF 0x7e
57 #define GSMI_HANDSHAKE_NONE 0x7f
58 #define GSMI_INVALID_PARAMETER 0x82
59 #define GSMI_UNSUPPORTED 0x83
60 #define GSMI_BUFFER_TOO_SMALL 0x85
61 #define GSMI_NOT_READY 0x86
62 #define GSMI_DEVICE_ERROR 0x87
63 #define GSMI_NOT_FOUND 0x8e
65 #define QUIRKY_BOARD_HASH 0x78a30a50
67 /* Internally used commands passed to the firmware */
68 #define GSMI_CMD_GET_NVRAM_VAR 0x01
69 #define GSMI_CMD_GET_NEXT_VAR 0x02
70 #define GSMI_CMD_SET_NVRAM_VAR 0x03
71 #define GSMI_CMD_SET_EVENT_LOG 0x08
72 #define GSMI_CMD_CLEAR_EVENT_LOG 0x09
73 #define GSMI_CMD_CLEAR_CONFIG 0x20
74 #define GSMI_CMD_HANDSHAKE_TYPE 0xC1
76 /* Magic entry type for kernel events */
77 #define GSMI_LOG_ENTRY_TYPE_KERNEL 0xDEAD
79 /* SMI buffers must be in 32bit physical address space */
80 struct gsmi_buf {
81 u8 *start; /* start of buffer */
82 size_t length; /* length of buffer */
83 dma_addr_t handle; /* dma allocation handle */
84 u32 address; /* physical address of buffer */
87 struct gsmi_device {
88 struct platform_device *pdev; /* platform device */
89 struct gsmi_buf *name_buf; /* variable name buffer */
90 struct gsmi_buf *data_buf; /* generic data buffer */
91 struct gsmi_buf *param_buf; /* parameter buffer */
92 spinlock_t lock; /* serialize access to SMIs */
93 u16 smi_cmd; /* SMI command port */
94 int handshake_type; /* firmware handler interlock type */
95 struct dma_pool *dma_pool; /* DMA buffer pool */
96 } gsmi_dev;
98 /* Packed structures for communicating with the firmware */
99 struct gsmi_nvram_var_param {
100 efi_guid_t guid;
101 u32 name_ptr;
102 u32 attributes;
103 u32 data_len;
104 u32 data_ptr;
105 } __packed;
107 struct gsmi_get_next_var_param {
108 u8 guid[GSMI_GUID_SIZE];
109 u32 name_ptr;
110 u32 name_len;
111 } __packed;
113 struct gsmi_set_eventlog_param {
114 u32 data_ptr;
115 u32 data_len;
116 u32 type;
117 } __packed;
119 /* Event log formats */
120 struct gsmi_log_entry_type_1 {
121 u16 type;
122 u32 instance;
123 } __packed;
127 * Some platforms don't have explicit SMI handshake
128 * and need to wait for SMI to complete.
130 #define GSMI_DEFAULT_SPINCOUNT 0x10000
131 static unsigned int spincount = GSMI_DEFAULT_SPINCOUNT;
132 module_param(spincount, uint, 0600);
133 MODULE_PARM_DESC(spincount,
134 "The number of loop iterations to use when using the spin handshake.");
136 static struct gsmi_buf *gsmi_buf_alloc(void)
138 struct gsmi_buf *smibuf;
140 smibuf = kzalloc(sizeof(*smibuf), GFP_KERNEL);
141 if (!smibuf) {
142 printk(KERN_ERR "gsmi: out of memory\n");
143 return NULL;
146 /* allocate buffer in 32bit address space */
147 smibuf->start = dma_pool_alloc(gsmi_dev.dma_pool, GFP_KERNEL,
148 &smibuf->handle);
149 if (!smibuf->start) {
150 printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
151 kfree(smibuf);
152 return NULL;
155 /* fill in the buffer handle */
156 smibuf->length = GSMI_BUF_SIZE;
157 smibuf->address = (u32)virt_to_phys(smibuf->start);
159 return smibuf;
162 static void gsmi_buf_free(struct gsmi_buf *smibuf)
164 if (smibuf) {
165 if (smibuf->start)
166 dma_pool_free(gsmi_dev.dma_pool, smibuf->start,
167 smibuf->handle);
168 kfree(smibuf);
173 * Make a call to gsmi func(sub). GSMI error codes are translated to
174 * in-kernel errnos (0 on success, -ERRNO on error).
176 static int gsmi_exec(u8 func, u8 sub)
178 u16 cmd = (sub << 8) | func;
179 u16 result = 0;
180 int rc = 0;
183 * AH : Subfunction number
184 * AL : Function number
185 * EBX : Parameter block address
186 * DX : SMI command port
188 * Three protocols here. See also the comment in gsmi_init().
190 if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
192 * If handshake_type == HANDSHAKE_CF then set CF on the
193 * way in and wait for the handler to clear it; this avoids
194 * corrupting register state on those chipsets which have
195 * a delay between writing the SMI trigger register and
196 * entering SMM.
198 asm volatile (
199 "stc\n"
200 "outb %%al, %%dx\n"
201 "1: jc 1b\n"
202 : "=a" (result)
203 : "0" (cmd),
204 "d" (gsmi_dev.smi_cmd),
205 "b" (gsmi_dev.param_buf->address)
206 : "memory", "cc"
208 } else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
210 * If handshake_type == HANDSHAKE_SPIN we spin a
211 * hundred-ish usecs to ensure the SMI has triggered.
213 asm volatile (
214 "outb %%al, %%dx\n"
215 "1: loop 1b\n"
216 : "=a" (result)
217 : "0" (cmd),
218 "d" (gsmi_dev.smi_cmd),
219 "b" (gsmi_dev.param_buf->address),
220 "c" (spincount)
221 : "memory", "cc"
223 } else {
225 * If handshake_type == HANDSHAKE_NONE we do nothing;
226 * either we don't need to or it's legacy firmware that
227 * doesn't understand the CF protocol.
229 asm volatile (
230 "outb %%al, %%dx\n\t"
231 : "=a" (result)
232 : "0" (cmd),
233 "d" (gsmi_dev.smi_cmd),
234 "b" (gsmi_dev.param_buf->address)
235 : "memory", "cc"
239 /* check return code from SMI handler */
240 switch (result) {
241 case GSMI_SUCCESS:
242 break;
243 case GSMI_VAR_NOT_FOUND:
244 /* not really an error, but let the caller know */
245 rc = 1;
246 break;
247 case GSMI_INVALID_PARAMETER:
248 printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
249 rc = -EINVAL;
250 break;
251 case GSMI_BUFFER_TOO_SMALL:
252 printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
253 rc = -ENOMEM;
254 break;
255 case GSMI_UNSUPPORTED:
256 case GSMI_UNSUPPORTED2:
257 if (sub != GSMI_CMD_HANDSHAKE_TYPE)
258 printk(KERN_ERR "gsmi: exec 0x%04x: Not supported\n",
259 cmd);
260 rc = -ENOSYS;
261 break;
262 case GSMI_NOT_READY:
263 printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
264 rc = -EBUSY;
265 break;
266 case GSMI_DEVICE_ERROR:
267 printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
268 rc = -EFAULT;
269 break;
270 case GSMI_NOT_FOUND:
271 printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
272 rc = -ENOENT;
273 break;
274 case GSMI_LOG_FULL:
275 printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
276 rc = -ENOSPC;
277 break;
278 case GSMI_HANDSHAKE_CF:
279 case GSMI_HANDSHAKE_SPIN:
280 case GSMI_HANDSHAKE_NONE:
281 rc = result;
282 break;
283 default:
284 printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
285 cmd, result);
286 rc = -ENXIO;
289 return rc;
292 static efi_status_t gsmi_get_variable(efi_char16_t *name,
293 efi_guid_t *vendor, u32 *attr,
294 unsigned long *data_size,
295 void *data)
297 struct gsmi_nvram_var_param param = {
298 .name_ptr = gsmi_dev.name_buf->address,
299 .data_ptr = gsmi_dev.data_buf->address,
300 .data_len = (u32)*data_size,
302 efi_status_t ret = EFI_SUCCESS;
303 unsigned long flags;
304 size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
305 int rc;
307 if (name_len >= GSMI_BUF_SIZE / 2)
308 return EFI_BAD_BUFFER_SIZE;
310 spin_lock_irqsave(&gsmi_dev.lock, flags);
312 /* Vendor guid */
313 memcpy(&param.guid, vendor, sizeof(param.guid));
315 /* variable name, already in UTF-16 */
316 memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
317 memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
319 /* data pointer */
320 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
322 /* parameter buffer */
323 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
324 memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
326 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
327 if (rc < 0) {
328 printk(KERN_ERR "gsmi: Get Variable failed\n");
329 ret = EFI_LOAD_ERROR;
330 } else if (rc == 1) {
331 /* variable was not found */
332 ret = EFI_NOT_FOUND;
333 } else {
334 /* Get the arguments back */
335 memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
337 /* The size reported is the min of all of our buffers */
338 *data_size = min_t(unsigned long, *data_size,
339 gsmi_dev.data_buf->length);
340 *data_size = min_t(unsigned long, *data_size, param.data_len);
342 /* Copy data back to return buffer. */
343 memcpy(data, gsmi_dev.data_buf->start, *data_size);
345 /* All variables are have the following attributes */
346 *attr = EFI_VARIABLE_NON_VOLATILE |
347 EFI_VARIABLE_BOOTSERVICE_ACCESS |
348 EFI_VARIABLE_RUNTIME_ACCESS;
351 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
353 return ret;
356 static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
357 efi_char16_t *name,
358 efi_guid_t *vendor)
360 struct gsmi_get_next_var_param param = {
361 .name_ptr = gsmi_dev.name_buf->address,
362 .name_len = gsmi_dev.name_buf->length,
364 efi_status_t ret = EFI_SUCCESS;
365 int rc;
366 unsigned long flags;
368 /* For the moment, only support buffers that exactly match in size */
369 if (*name_size != GSMI_BUF_SIZE)
370 return EFI_BAD_BUFFER_SIZE;
372 /* Let's make sure the thing is at least null-terminated */
373 if (ucs2_strnlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
374 return EFI_INVALID_PARAMETER;
376 spin_lock_irqsave(&gsmi_dev.lock, flags);
378 /* guid */
379 memcpy(&param.guid, vendor, sizeof(param.guid));
381 /* variable name, already in UTF-16 */
382 memcpy(gsmi_dev.name_buf->start, name, *name_size);
384 /* parameter buffer */
385 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
386 memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
388 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
389 if (rc < 0) {
390 printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
391 ret = EFI_LOAD_ERROR;
392 } else if (rc == 1) {
393 /* variable not found -- end of list */
394 ret = EFI_NOT_FOUND;
395 } else {
396 /* copy variable data back to return buffer */
397 memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
399 /* Copy the name back */
400 memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
401 *name_size = ucs2_strnlen(name, GSMI_BUF_SIZE / 2) * 2;
403 /* copy guid to return buffer */
404 memcpy(vendor, &param.guid, sizeof(param.guid));
405 ret = EFI_SUCCESS;
408 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
410 return ret;
413 static efi_status_t gsmi_set_variable(efi_char16_t *name,
414 efi_guid_t *vendor,
415 u32 attr,
416 unsigned long data_size,
417 void *data)
419 struct gsmi_nvram_var_param param = {
420 .name_ptr = gsmi_dev.name_buf->address,
421 .data_ptr = gsmi_dev.data_buf->address,
422 .data_len = (u32)data_size,
423 .attributes = EFI_VARIABLE_NON_VOLATILE |
424 EFI_VARIABLE_BOOTSERVICE_ACCESS |
425 EFI_VARIABLE_RUNTIME_ACCESS,
427 size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
428 efi_status_t ret = EFI_SUCCESS;
429 int rc;
430 unsigned long flags;
432 if (name_len >= GSMI_BUF_SIZE / 2)
433 return EFI_BAD_BUFFER_SIZE;
435 spin_lock_irqsave(&gsmi_dev.lock, flags);
437 /* guid */
438 memcpy(&param.guid, vendor, sizeof(param.guid));
440 /* variable name, already in UTF-16 */
441 memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
442 memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
444 /* data pointer */
445 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
446 memcpy(gsmi_dev.data_buf->start, data, data_size);
448 /* parameter buffer */
449 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
450 memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
452 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
453 if (rc < 0) {
454 printk(KERN_ERR "gsmi: Set Variable failed\n");
455 ret = EFI_INVALID_PARAMETER;
458 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
460 return ret;
463 static const struct efivar_operations efivar_ops = {
464 .get_variable = gsmi_get_variable,
465 .set_variable = gsmi_set_variable,
466 .get_next_variable = gsmi_get_next_variable,
469 static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
470 struct bin_attribute *bin_attr,
471 char *buf, loff_t pos, size_t count)
473 struct gsmi_set_eventlog_param param = {
474 .data_ptr = gsmi_dev.data_buf->address,
476 int rc = 0;
477 unsigned long flags;
479 /* Pull the type out */
480 if (count < sizeof(u32))
481 return -EINVAL;
482 param.type = *(u32 *)buf;
483 count -= sizeof(u32);
484 buf += sizeof(u32);
486 /* The remaining buffer is the data payload */
487 if (count > gsmi_dev.data_buf->length)
488 return -EINVAL;
489 param.data_len = count - sizeof(u32);
491 spin_lock_irqsave(&gsmi_dev.lock, flags);
493 /* data pointer */
494 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
495 memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
497 /* parameter buffer */
498 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
499 memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
501 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
502 if (rc < 0)
503 printk(KERN_ERR "gsmi: Set Event Log failed\n");
505 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
507 return rc;
511 static struct bin_attribute eventlog_bin_attr = {
512 .attr = {.name = "append_to_eventlog", .mode = 0200},
513 .write = eventlog_write,
516 static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
517 struct kobj_attribute *attr,
518 const char *buf, size_t count)
520 int rc;
521 unsigned long flags;
522 unsigned long val;
523 struct {
524 u32 percentage;
525 u32 data_type;
526 } param;
528 rc = kstrtoul(buf, 0, &val);
529 if (rc)
530 return rc;
533 * Value entered is a percentage, 0 through 100, anything else
534 * is invalid.
536 if (val > 100)
537 return -EINVAL;
539 /* data_type here selects the smbios event log. */
540 param.percentage = val;
541 param.data_type = 0;
543 spin_lock_irqsave(&gsmi_dev.lock, flags);
545 /* parameter buffer */
546 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
547 memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
549 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
551 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
553 if (rc)
554 return rc;
555 return count;
558 static struct kobj_attribute gsmi_clear_eventlog_attr = {
559 .attr = {.name = "clear_eventlog", .mode = 0200},
560 .store = gsmi_clear_eventlog_store,
563 static ssize_t gsmi_clear_config_store(struct kobject *kobj,
564 struct kobj_attribute *attr,
565 const char *buf, size_t count)
567 int rc;
568 unsigned long flags;
570 spin_lock_irqsave(&gsmi_dev.lock, flags);
572 /* clear parameter buffer */
573 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
575 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
577 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
579 if (rc)
580 return rc;
581 return count;
584 static struct kobj_attribute gsmi_clear_config_attr = {
585 .attr = {.name = "clear_config", .mode = 0200},
586 .store = gsmi_clear_config_store,
589 static const struct attribute *gsmi_attrs[] = {
590 &gsmi_clear_config_attr.attr,
591 &gsmi_clear_eventlog_attr.attr,
592 NULL,
595 static int gsmi_shutdown_reason(int reason)
597 struct gsmi_log_entry_type_1 entry = {
598 .type = GSMI_LOG_ENTRY_TYPE_KERNEL,
599 .instance = reason,
601 struct gsmi_set_eventlog_param param = {
602 .data_len = sizeof(entry),
603 .type = 1,
605 static int saved_reason;
606 int rc = 0;
607 unsigned long flags;
609 /* avoid duplicate entries in the log */
610 if (saved_reason & (1 << reason))
611 return 0;
613 spin_lock_irqsave(&gsmi_dev.lock, flags);
615 saved_reason |= (1 << reason);
617 /* data pointer */
618 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
619 memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
621 /* parameter buffer */
622 param.data_ptr = gsmi_dev.data_buf->address;
623 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
624 memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
626 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
628 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
630 if (rc < 0)
631 printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
632 else
633 printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
634 reason);
636 return rc;
639 static int gsmi_reboot_callback(struct notifier_block *nb,
640 unsigned long reason, void *arg)
642 gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
643 return NOTIFY_DONE;
646 static struct notifier_block gsmi_reboot_notifier = {
647 .notifier_call = gsmi_reboot_callback
650 static int gsmi_die_callback(struct notifier_block *nb,
651 unsigned long reason, void *arg)
653 if (reason == DIE_OOPS)
654 gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
655 return NOTIFY_DONE;
658 static struct notifier_block gsmi_die_notifier = {
659 .notifier_call = gsmi_die_callback
662 static int gsmi_panic_callback(struct notifier_block *nb,
663 unsigned long reason, void *arg)
665 gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
666 return NOTIFY_DONE;
669 static struct notifier_block gsmi_panic_notifier = {
670 .notifier_call = gsmi_panic_callback,
674 * This hash function was blatantly copied from include/linux/hash.h.
675 * It is used by this driver to obfuscate a board name that requires a
676 * quirk within this driver.
678 * Please do not remove this copy of the function as any changes to the
679 * global utility hash_64() function would break this driver's ability
680 * to identify a board and provide the appropriate quirk -- mikew@google.com
682 static u64 __init local_hash_64(u64 val, unsigned bits)
684 u64 hash = val;
686 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
687 u64 n = hash;
688 n <<= 18;
689 hash -= n;
690 n <<= 33;
691 hash -= n;
692 n <<= 3;
693 hash += n;
694 n <<= 3;
695 hash -= n;
696 n <<= 4;
697 hash += n;
698 n <<= 2;
699 hash += n;
701 /* High bits are more random, so use them. */
702 return hash >> (64 - bits);
705 static u32 __init hash_oem_table_id(char s[8])
707 u64 input;
708 memcpy(&input, s, 8);
709 return local_hash_64(input, 32);
712 static struct dmi_system_id gsmi_dmi_table[] __initdata = {
714 .ident = "Google Board",
715 .matches = {
716 DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
721 MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
723 static __init int gsmi_system_valid(void)
725 u32 hash;
727 if (!dmi_check_system(gsmi_dmi_table))
728 return -ENODEV;
731 * Only newer firmware supports the gsmi interface. All older
732 * firmware that didn't support this interface used to plug the
733 * table name in the first four bytes of the oem_table_id field.
734 * Newer firmware doesn't do that though, so use that as the
735 * discriminant factor. We have to do this in order to
736 * whitewash our board names out of the public driver.
738 if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
739 printk(KERN_INFO "gsmi: Board is too old\n");
740 return -ENODEV;
743 /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
744 hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
745 if (hash == QUIRKY_BOARD_HASH) {
746 const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
747 if (strncmp(bios_ver, "1.0", 3) == 0) {
748 pr_info("gsmi: disabled on this board's BIOS %s\n",
749 bios_ver);
750 return -ENODEV;
754 /* check for valid SMI command port in ACPI FADT */
755 if (acpi_gbl_FADT.smi_command == 0) {
756 pr_info("gsmi: missing smi_command\n");
757 return -ENODEV;
760 /* Found */
761 return 0;
764 static struct kobject *gsmi_kobj;
765 static struct efivars efivars;
767 static __init int gsmi_init(void)
769 unsigned long flags;
770 int ret;
772 ret = gsmi_system_valid();
773 if (ret)
774 return ret;
776 gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
778 /* register device */
779 gsmi_dev.pdev = platform_device_register_simple("gsmi", -1, NULL, 0);
780 if (IS_ERR(gsmi_dev.pdev)) {
781 printk(KERN_ERR "gsmi: unable to register platform device\n");
782 return PTR_ERR(gsmi_dev.pdev);
785 /* SMI access needs to be serialized */
786 spin_lock_init(&gsmi_dev.lock);
788 /* SMI callbacks require 32bit addresses */
789 gsmi_dev.pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
790 gsmi_dev.pdev->dev.dma_mask =
791 &gsmi_dev.pdev->dev.coherent_dma_mask;
792 ret = -ENOMEM;
793 gsmi_dev.dma_pool = dma_pool_create("gsmi", &gsmi_dev.pdev->dev,
794 GSMI_BUF_SIZE, GSMI_BUF_ALIGN, 0);
795 if (!gsmi_dev.dma_pool)
796 goto out_err;
799 * pre-allocate buffers because sometimes we are called when
800 * this is not feasible: oops, panic, die, mce, etc
802 gsmi_dev.name_buf = gsmi_buf_alloc();
803 if (!gsmi_dev.name_buf) {
804 printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
805 goto out_err;
808 gsmi_dev.data_buf = gsmi_buf_alloc();
809 if (!gsmi_dev.data_buf) {
810 printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
811 goto out_err;
814 gsmi_dev.param_buf = gsmi_buf_alloc();
815 if (!gsmi_dev.param_buf) {
816 printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
817 goto out_err;
821 * Determine type of handshake used to serialize the SMI
822 * entry. See also gsmi_exec().
824 * There's a "behavior" present on some chipsets where writing the
825 * SMI trigger register in the southbridge doesn't result in an
826 * immediate SMI. Rather, the processor can execute "a few" more
827 * instructions before the SMI takes effect. To ensure synchronous
828 * behavior, implement a handshake between the kernel driver and the
829 * firmware handler to spin until released. This ioctl determines
830 * the type of handshake.
832 * NONE: The firmware handler does not implement any
833 * handshake. Either it doesn't need to, or it's legacy firmware
834 * that doesn't know it needs to and never will.
836 * CF: The firmware handler will clear the CF in the saved
837 * state before returning. The driver may set the CF and test for
838 * it to clear before proceeding.
840 * SPIN: The firmware handler does not implement any handshake
841 * but the driver should spin for a hundred or so microseconds
842 * to ensure the SMI has triggered.
844 * Finally, the handler will return -ENOSYS if
845 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
846 * HANDSHAKE_NONE.
848 spin_lock_irqsave(&gsmi_dev.lock, flags);
849 gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
850 gsmi_dev.handshake_type =
851 gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
852 if (gsmi_dev.handshake_type == -ENOSYS)
853 gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
854 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
856 /* Remove and clean up gsmi if the handshake could not complete. */
857 if (gsmi_dev.handshake_type == -ENXIO) {
858 printk(KERN_INFO "gsmi version " DRIVER_VERSION
859 " failed to load\n");
860 ret = -ENODEV;
861 goto out_err;
864 /* Register in the firmware directory */
865 ret = -ENOMEM;
866 gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
867 if (!gsmi_kobj) {
868 printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
869 goto out_err;
872 /* Setup eventlog access */
873 ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
874 if (ret) {
875 printk(KERN_INFO "gsmi: Failed to setup eventlog");
876 goto out_err;
879 /* Other attributes */
880 ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
881 if (ret) {
882 printk(KERN_INFO "gsmi: Failed to add attrs");
883 goto out_remove_bin_file;
886 ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
887 if (ret) {
888 printk(KERN_INFO "gsmi: Failed to register efivars\n");
889 goto out_remove_sysfs_files;
892 ret = efivars_sysfs_init();
893 if (ret) {
894 printk(KERN_INFO "gsmi: Failed to create efivars files\n");
895 efivars_unregister(&efivars);
896 goto out_remove_sysfs_files;
899 register_reboot_notifier(&gsmi_reboot_notifier);
900 register_die_notifier(&gsmi_die_notifier);
901 atomic_notifier_chain_register(&panic_notifier_list,
902 &gsmi_panic_notifier);
904 printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
906 return 0;
908 out_remove_sysfs_files:
909 sysfs_remove_files(gsmi_kobj, gsmi_attrs);
910 out_remove_bin_file:
911 sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
912 out_err:
913 kobject_put(gsmi_kobj);
914 gsmi_buf_free(gsmi_dev.param_buf);
915 gsmi_buf_free(gsmi_dev.data_buf);
916 gsmi_buf_free(gsmi_dev.name_buf);
917 if (gsmi_dev.dma_pool)
918 dma_pool_destroy(gsmi_dev.dma_pool);
919 platform_device_unregister(gsmi_dev.pdev);
920 pr_info("gsmi: failed to load: %d\n", ret);
921 return ret;
924 static void __exit gsmi_exit(void)
926 unregister_reboot_notifier(&gsmi_reboot_notifier);
927 unregister_die_notifier(&gsmi_die_notifier);
928 atomic_notifier_chain_unregister(&panic_notifier_list,
929 &gsmi_panic_notifier);
930 efivars_unregister(&efivars);
932 sysfs_remove_files(gsmi_kobj, gsmi_attrs);
933 sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
934 kobject_put(gsmi_kobj);
935 gsmi_buf_free(gsmi_dev.param_buf);
936 gsmi_buf_free(gsmi_dev.data_buf);
937 gsmi_buf_free(gsmi_dev.name_buf);
938 dma_pool_destroy(gsmi_dev.dma_pool);
939 platform_device_unregister(gsmi_dev.pdev);
942 module_init(gsmi_init);
943 module_exit(gsmi_exit);
945 MODULE_AUTHOR("Google, Inc.");
946 MODULE_LICENSE("GPL");