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
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>
21 #include <linux/slab.h>
22 #include <linux/ioctl.h>
23 #include <linux/acpi.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>
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 */
42 #define DRIVER_VERSION "1.0"
43 #define GSMI_GUID_SIZE 16
44 #define GSMI_BUF_SIZE 1024
45 #define GSMI_BUF_ALIGN sizeof(u64)
46 #define GSMI_CALLBACK 0xef
48 /* SMI return codes */
49 #define GSMI_SUCCESS 0x00
50 #define GSMI_UNSUPPORTED2 0x03
51 #define GSMI_LOG_FULL 0x0b
52 #define GSMI_VAR_NOT_FOUND 0x0e
53 #define GSMI_HANDSHAKE_SPIN 0x7d
54 #define GSMI_HANDSHAKE_CF 0x7e
55 #define GSMI_HANDSHAKE_NONE 0x7f
56 #define GSMI_INVALID_PARAMETER 0x82
57 #define GSMI_UNSUPPORTED 0x83
58 #define GSMI_BUFFER_TOO_SMALL 0x85
59 #define GSMI_NOT_READY 0x86
60 #define GSMI_DEVICE_ERROR 0x87
61 #define GSMI_NOT_FOUND 0x8e
63 #define QUIRKY_BOARD_HASH 0x78a30a50
65 /* Internally used commands passed to the firmware */
66 #define GSMI_CMD_GET_NVRAM_VAR 0x01
67 #define GSMI_CMD_GET_NEXT_VAR 0x02
68 #define GSMI_CMD_SET_NVRAM_VAR 0x03
69 #define GSMI_CMD_SET_EVENT_LOG 0x08
70 #define GSMI_CMD_CLEAR_EVENT_LOG 0x09
71 #define GSMI_CMD_CLEAR_CONFIG 0x20
72 #define GSMI_CMD_HANDSHAKE_TYPE 0xC1
74 /* Magic entry type for kernel events */
75 #define GSMI_LOG_ENTRY_TYPE_KERNEL 0xDEAD
77 /* SMI buffers must be in 32bit physical address space */
79 u8
*start
; /* start of buffer */
80 size_t length
; /* length of buffer */
81 dma_addr_t handle
; /* dma allocation handle */
82 u32 address
; /* physical address of buffer */
86 struct platform_device
*pdev
; /* platform device */
87 struct gsmi_buf
*name_buf
; /* variable name buffer */
88 struct gsmi_buf
*data_buf
; /* generic data buffer */
89 struct gsmi_buf
*param_buf
; /* parameter buffer */
90 spinlock_t lock
; /* serialize access to SMIs */
91 u16 smi_cmd
; /* SMI command port */
92 int handshake_type
; /* firmware handler interlock type */
93 struct dma_pool
*dma_pool
; /* DMA buffer pool */
96 /* Packed structures for communicating with the firmware */
97 struct gsmi_nvram_var_param
{
105 struct gsmi_get_next_var_param
{
106 u8 guid
[GSMI_GUID_SIZE
];
111 struct gsmi_set_eventlog_param
{
117 /* Event log formats */
118 struct gsmi_log_entry_type_1
{
125 * Some platforms don't have explicit SMI handshake
126 * and need to wait for SMI to complete.
128 #define GSMI_DEFAULT_SPINCOUNT 0x10000
129 static unsigned int spincount
= GSMI_DEFAULT_SPINCOUNT
;
130 module_param(spincount
, uint
, 0600);
131 MODULE_PARM_DESC(spincount
,
132 "The number of loop iterations to use when using the spin handshake.");
134 static struct gsmi_buf
*gsmi_buf_alloc(void)
136 struct gsmi_buf
*smibuf
;
138 smibuf
= kzalloc(sizeof(*smibuf
), GFP_KERNEL
);
140 printk(KERN_ERR
"gsmi: out of memory\n");
144 /* allocate buffer in 32bit address space */
145 smibuf
->start
= dma_pool_alloc(gsmi_dev
.dma_pool
, GFP_KERNEL
,
147 if (!smibuf
->start
) {
148 printk(KERN_ERR
"gsmi: failed to allocate name buffer\n");
153 /* fill in the buffer handle */
154 smibuf
->length
= GSMI_BUF_SIZE
;
155 smibuf
->address
= (u32
)virt_to_phys(smibuf
->start
);
160 static void gsmi_buf_free(struct gsmi_buf
*smibuf
)
164 dma_pool_free(gsmi_dev
.dma_pool
, smibuf
->start
,
171 * Make a call to gsmi func(sub). GSMI error codes are translated to
172 * in-kernel errnos (0 on success, -ERRNO on error).
174 static int gsmi_exec(u8 func
, u8 sub
)
176 u16 cmd
= (sub
<< 8) | func
;
181 * AH : Subfunction number
182 * AL : Function number
183 * EBX : Parameter block address
184 * DX : SMI command port
186 * Three protocols here. See also the comment in gsmi_init().
188 if (gsmi_dev
.handshake_type
== GSMI_HANDSHAKE_CF
) {
190 * If handshake_type == HANDSHAKE_CF then set CF on the
191 * way in and wait for the handler to clear it; this avoids
192 * corrupting register state on those chipsets which have
193 * a delay between writing the SMI trigger register and
202 "d" (gsmi_dev
.smi_cmd
),
203 "b" (gsmi_dev
.param_buf
->address
)
206 } else if (gsmi_dev
.handshake_type
== GSMI_HANDSHAKE_SPIN
) {
208 * If handshake_type == HANDSHAKE_SPIN we spin a
209 * hundred-ish usecs to ensure the SMI has triggered.
216 "d" (gsmi_dev
.smi_cmd
),
217 "b" (gsmi_dev
.param_buf
->address
),
223 * If handshake_type == HANDSHAKE_NONE we do nothing;
224 * either we don't need to or it's legacy firmware that
225 * doesn't understand the CF protocol.
228 "outb %%al, %%dx\n\t"
231 "d" (gsmi_dev
.smi_cmd
),
232 "b" (gsmi_dev
.param_buf
->address
)
237 /* check return code from SMI handler */
241 case GSMI_VAR_NOT_FOUND
:
242 /* not really an error, but let the caller know */
245 case GSMI_INVALID_PARAMETER
:
246 printk(KERN_ERR
"gsmi: exec 0x%04x: Invalid parameter\n", cmd
);
249 case GSMI_BUFFER_TOO_SMALL
:
250 printk(KERN_ERR
"gsmi: exec 0x%04x: Buffer too small\n", cmd
);
253 case GSMI_UNSUPPORTED
:
254 case GSMI_UNSUPPORTED2
:
255 if (sub
!= GSMI_CMD_HANDSHAKE_TYPE
)
256 printk(KERN_ERR
"gsmi: exec 0x%04x: Not supported\n",
261 printk(KERN_ERR
"gsmi: exec 0x%04x: Not ready\n", cmd
);
264 case GSMI_DEVICE_ERROR
:
265 printk(KERN_ERR
"gsmi: exec 0x%04x: Device error\n", cmd
);
269 printk(KERN_ERR
"gsmi: exec 0x%04x: Data not found\n", cmd
);
273 printk(KERN_ERR
"gsmi: exec 0x%04x: Log full\n", cmd
);
276 case GSMI_HANDSHAKE_CF
:
277 case GSMI_HANDSHAKE_SPIN
:
278 case GSMI_HANDSHAKE_NONE
:
282 printk(KERN_ERR
"gsmi: exec 0x%04x: Unknown error 0x%04x\n",
290 /* Return the number of unicode characters in data */
292 utf16_strlen(efi_char16_t
*data
, unsigned long maxlength
)
294 unsigned long length
= 0;
296 while (*data
++ != 0 && length
< maxlength
)
301 static efi_status_t
gsmi_get_variable(efi_char16_t
*name
,
302 efi_guid_t
*vendor
, u32
*attr
,
303 unsigned long *data_size
,
306 struct gsmi_nvram_var_param param
= {
307 .name_ptr
= gsmi_dev
.name_buf
->address
,
308 .data_ptr
= gsmi_dev
.data_buf
->address
,
309 .data_len
= (u32
)*data_size
,
311 efi_status_t ret
= EFI_SUCCESS
;
313 size_t name_len
= utf16_strlen(name
, GSMI_BUF_SIZE
/ 2);
316 if (name_len
>= GSMI_BUF_SIZE
/ 2)
317 return EFI_BAD_BUFFER_SIZE
;
319 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
322 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
324 /* variable name, already in UTF-16 */
325 memset(gsmi_dev
.name_buf
->start
, 0, gsmi_dev
.name_buf
->length
);
326 memcpy(gsmi_dev
.name_buf
->start
, name
, name_len
* 2);
329 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
331 /* parameter buffer */
332 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
333 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
335 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_GET_NVRAM_VAR
);
337 printk(KERN_ERR
"gsmi: Get Variable failed\n");
338 ret
= EFI_LOAD_ERROR
;
339 } else if (rc
== 1) {
340 /* variable was not found */
343 /* Get the arguments back */
344 memcpy(¶m
, gsmi_dev
.param_buf
->start
, sizeof(param
));
346 /* The size reported is the min of all of our buffers */
347 *data_size
= min(*data_size
, gsmi_dev
.data_buf
->length
);
348 *data_size
= min_t(unsigned long, *data_size
, param
.data_len
);
350 /* Copy data back to return buffer. */
351 memcpy(data
, gsmi_dev
.data_buf
->start
, *data_size
);
353 /* All variables are have the following attributes */
354 *attr
= EFI_VARIABLE_NON_VOLATILE
|
355 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
356 EFI_VARIABLE_RUNTIME_ACCESS
;
359 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
364 static efi_status_t
gsmi_get_next_variable(unsigned long *name_size
,
368 struct gsmi_get_next_var_param param
= {
369 .name_ptr
= gsmi_dev
.name_buf
->address
,
370 .name_len
= gsmi_dev
.name_buf
->length
,
372 efi_status_t ret
= EFI_SUCCESS
;
376 /* For the moment, only support buffers that exactly match in size */
377 if (*name_size
!= GSMI_BUF_SIZE
)
378 return EFI_BAD_BUFFER_SIZE
;
380 /* Let's make sure the thing is at least null-terminated */
381 if (utf16_strlen(name
, GSMI_BUF_SIZE
/ 2) == GSMI_BUF_SIZE
/ 2)
382 return EFI_INVALID_PARAMETER
;
384 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
387 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
389 /* variable name, already in UTF-16 */
390 memcpy(gsmi_dev
.name_buf
->start
, name
, *name_size
);
392 /* parameter buffer */
393 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
394 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
396 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_GET_NEXT_VAR
);
398 printk(KERN_ERR
"gsmi: Get Next Variable Name failed\n");
399 ret
= EFI_LOAD_ERROR
;
400 } else if (rc
== 1) {
401 /* variable not found -- end of list */
404 /* copy variable data back to return buffer */
405 memcpy(¶m
, gsmi_dev
.param_buf
->start
, sizeof(param
));
407 /* Copy the name back */
408 memcpy(name
, gsmi_dev
.name_buf
->start
, GSMI_BUF_SIZE
);
409 *name_size
= utf16_strlen(name
, GSMI_BUF_SIZE
/ 2) * 2;
411 /* copy guid to return buffer */
412 memcpy(vendor
, ¶m
.guid
, sizeof(param
.guid
));
416 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
421 static efi_status_t
gsmi_set_variable(efi_char16_t
*name
,
424 unsigned long data_size
,
427 struct gsmi_nvram_var_param param
= {
428 .name_ptr
= gsmi_dev
.name_buf
->address
,
429 .data_ptr
= gsmi_dev
.data_buf
->address
,
430 .data_len
= (u32
)data_size
,
431 .attributes
= EFI_VARIABLE_NON_VOLATILE
|
432 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
433 EFI_VARIABLE_RUNTIME_ACCESS
,
435 size_t name_len
= utf16_strlen(name
, GSMI_BUF_SIZE
/ 2);
436 efi_status_t ret
= EFI_SUCCESS
;
440 if (name_len
>= GSMI_BUF_SIZE
/ 2)
441 return EFI_BAD_BUFFER_SIZE
;
443 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
446 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
448 /* variable name, already in UTF-16 */
449 memset(gsmi_dev
.name_buf
->start
, 0, gsmi_dev
.name_buf
->length
);
450 memcpy(gsmi_dev
.name_buf
->start
, name
, name_len
* 2);
453 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
454 memcpy(gsmi_dev
.data_buf
->start
, data
, data_size
);
456 /* parameter buffer */
457 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
458 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
460 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_NVRAM_VAR
);
462 printk(KERN_ERR
"gsmi: Set Variable failed\n");
463 ret
= EFI_INVALID_PARAMETER
;
466 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
471 static const struct efivar_operations efivar_ops
= {
472 .get_variable
= gsmi_get_variable
,
473 .set_variable
= gsmi_set_variable
,
474 .get_next_variable
= gsmi_get_next_variable
,
477 static ssize_t
eventlog_write(struct file
*filp
, struct kobject
*kobj
,
478 struct bin_attribute
*bin_attr
,
479 char *buf
, loff_t pos
, size_t count
)
481 struct gsmi_set_eventlog_param param
= {
482 .data_ptr
= gsmi_dev
.data_buf
->address
,
487 /* Pull the type out */
488 if (count
< sizeof(u32
))
490 param
.type
= *(u32
*)buf
;
491 count
-= sizeof(u32
);
494 /* The remaining buffer is the data payload */
495 if (count
> gsmi_dev
.data_buf
->length
)
497 param
.data_len
= count
- sizeof(u32
);
499 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
502 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
503 memcpy(gsmi_dev
.data_buf
->start
, buf
, param
.data_len
);
505 /* parameter buffer */
506 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
507 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
509 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_EVENT_LOG
);
511 printk(KERN_ERR
"gsmi: Set Event Log failed\n");
513 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
519 static struct bin_attribute eventlog_bin_attr
= {
520 .attr
= {.name
= "append_to_eventlog", .mode
= 0200},
521 .write
= eventlog_write
,
524 static ssize_t
gsmi_clear_eventlog_store(struct kobject
*kobj
,
525 struct kobj_attribute
*attr
,
526 const char *buf
, size_t count
)
536 rc
= strict_strtoul(buf
, 0, &val
);
541 * Value entered is a percentage, 0 through 100, anything else
547 /* data_type here selects the smbios event log. */
548 param
.percentage
= val
;
551 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
553 /* parameter buffer */
554 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
555 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
557 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_CLEAR_EVENT_LOG
);
559 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
566 static struct kobj_attribute gsmi_clear_eventlog_attr
= {
567 .attr
= {.name
= "clear_eventlog", .mode
= 0200},
568 .store
= gsmi_clear_eventlog_store
,
571 static ssize_t
gsmi_clear_config_store(struct kobject
*kobj
,
572 struct kobj_attribute
*attr
,
573 const char *buf
, size_t count
)
578 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
580 /* clear parameter buffer */
581 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
583 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_CLEAR_CONFIG
);
585 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
592 static struct kobj_attribute gsmi_clear_config_attr
= {
593 .attr
= {.name
= "clear_config", .mode
= 0200},
594 .store
= gsmi_clear_config_store
,
597 static const struct attribute
*gsmi_attrs
[] = {
598 &gsmi_clear_config_attr
.attr
,
599 &gsmi_clear_eventlog_attr
.attr
,
603 static int gsmi_shutdown_reason(int reason
)
605 struct gsmi_log_entry_type_1 entry
= {
606 .type
= GSMI_LOG_ENTRY_TYPE_KERNEL
,
609 struct gsmi_set_eventlog_param param
= {
610 .data_len
= sizeof(entry
),
613 static int saved_reason
;
617 /* avoid duplicate entries in the log */
618 if (saved_reason
& (1 << reason
))
621 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
623 saved_reason
|= (1 << reason
);
626 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
627 memcpy(gsmi_dev
.data_buf
->start
, &entry
, sizeof(entry
));
629 /* parameter buffer */
630 param
.data_ptr
= gsmi_dev
.data_buf
->address
;
631 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
632 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
634 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_EVENT_LOG
);
636 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
639 printk(KERN_ERR
"gsmi: Log Shutdown Reason failed\n");
641 printk(KERN_EMERG
"gsmi: Log Shutdown Reason 0x%02x\n",
647 static int gsmi_reboot_callback(struct notifier_block
*nb
,
648 unsigned long reason
, void *arg
)
650 gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN
);
654 static struct notifier_block gsmi_reboot_notifier
= {
655 .notifier_call
= gsmi_reboot_callback
658 static int gsmi_die_callback(struct notifier_block
*nb
,
659 unsigned long reason
, void *arg
)
661 if (reason
== DIE_OOPS
)
662 gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS
);
666 static struct notifier_block gsmi_die_notifier
= {
667 .notifier_call
= gsmi_die_callback
670 static int gsmi_panic_callback(struct notifier_block
*nb
,
671 unsigned long reason
, void *arg
)
673 gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC
);
677 static struct notifier_block gsmi_panic_notifier
= {
678 .notifier_call
= gsmi_panic_callback
,
682 * This hash function was blatantly copied from include/linux/hash.h.
683 * It is used by this driver to obfuscate a board name that requires a
684 * quirk within this driver.
686 * Please do not remove this copy of the function as any changes to the
687 * global utility hash_64() function would break this driver's ability
688 * to identify a board and provide the appropriate quirk -- mikew@google.com
690 static u64 __init
local_hash_64(u64 val
, unsigned bits
)
694 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
709 /* High bits are more random, so use them. */
710 return hash
>> (64 - bits
);
713 static u32 __init
hash_oem_table_id(char s
[8])
716 memcpy(&input
, s
, 8);
717 return local_hash_64(input
, 32);
720 static struct dmi_system_id gsmi_dmi_table
[] __initdata
= {
722 .ident
= "Google Board",
724 DMI_MATCH(DMI_BOARD_VENDOR
, "Google, Inc."),
729 MODULE_DEVICE_TABLE(dmi
, gsmi_dmi_table
);
731 static __init
int gsmi_system_valid(void)
735 if (!dmi_check_system(gsmi_dmi_table
))
739 * Only newer firmware supports the gsmi interface. All older
740 * firmware that didn't support this interface used to plug the
741 * table name in the first four bytes of the oem_table_id field.
742 * Newer firmware doesn't do that though, so use that as the
743 * discriminant factor. We have to do this in order to
744 * whitewash our board names out of the public driver.
746 if (!strncmp(acpi_gbl_FADT
.header
.oem_table_id
, "FACP", 4)) {
747 printk(KERN_INFO
"gsmi: Board is too old\n");
751 /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
752 hash
= hash_oem_table_id(acpi_gbl_FADT
.header
.oem_table_id
);
753 if (hash
== QUIRKY_BOARD_HASH
) {
754 const char *bios_ver
= dmi_get_system_info(DMI_BIOS_VERSION
);
755 if (strncmp(bios_ver
, "1.0", 3) == 0) {
756 pr_info("gsmi: disabled on this board's BIOS %s\n",
762 /* check for valid SMI command port in ACPI FADT */
763 if (acpi_gbl_FADT
.smi_command
== 0) {
764 pr_info("gsmi: missing smi_command\n");
772 static struct kobject
*gsmi_kobj
;
773 static struct efivars efivars
;
775 static __init
int gsmi_init(void)
780 ret
= gsmi_system_valid();
784 gsmi_dev
.smi_cmd
= acpi_gbl_FADT
.smi_command
;
786 /* register device */
787 gsmi_dev
.pdev
= platform_device_register_simple("gsmi", -1, NULL
, 0);
788 if (IS_ERR(gsmi_dev
.pdev
)) {
789 printk(KERN_ERR
"gsmi: unable to register platform device\n");
790 return PTR_ERR(gsmi_dev
.pdev
);
793 /* SMI access needs to be serialized */
794 spin_lock_init(&gsmi_dev
.lock
);
796 /* SMI callbacks require 32bit addresses */
797 gsmi_dev
.pdev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
798 gsmi_dev
.pdev
->dev
.dma_mask
=
799 &gsmi_dev
.pdev
->dev
.coherent_dma_mask
;
801 gsmi_dev
.dma_pool
= dma_pool_create("gsmi", &gsmi_dev
.pdev
->dev
,
802 GSMI_BUF_SIZE
, GSMI_BUF_ALIGN
, 0);
803 if (!gsmi_dev
.dma_pool
)
807 * pre-allocate buffers because sometimes we are called when
808 * this is not feasible: oops, panic, die, mce, etc
810 gsmi_dev
.name_buf
= gsmi_buf_alloc();
811 if (!gsmi_dev
.name_buf
) {
812 printk(KERN_ERR
"gsmi: failed to allocate name buffer\n");
816 gsmi_dev
.data_buf
= gsmi_buf_alloc();
817 if (!gsmi_dev
.data_buf
) {
818 printk(KERN_ERR
"gsmi: failed to allocate data buffer\n");
822 gsmi_dev
.param_buf
= gsmi_buf_alloc();
823 if (!gsmi_dev
.param_buf
) {
824 printk(KERN_ERR
"gsmi: failed to allocate param buffer\n");
829 * Determine type of handshake used to serialize the SMI
830 * entry. See also gsmi_exec().
832 * There's a "behavior" present on some chipsets where writing the
833 * SMI trigger register in the southbridge doesn't result in an
834 * immediate SMI. Rather, the processor can execute "a few" more
835 * instructions before the SMI takes effect. To ensure synchronous
836 * behavior, implement a handshake between the kernel driver and the
837 * firmware handler to spin until released. This ioctl determines
838 * the type of handshake.
840 * NONE: The firmware handler does not implement any
841 * handshake. Either it doesn't need to, or it's legacy firmware
842 * that doesn't know it needs to and never will.
844 * CF: The firmware handler will clear the CF in the saved
845 * state before returning. The driver may set the CF and test for
846 * it to clear before proceeding.
848 * SPIN: The firmware handler does not implement any handshake
849 * but the driver should spin for a hundred or so microseconds
850 * to ensure the SMI has triggered.
852 * Finally, the handler will return -ENOSYS if
853 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
856 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
857 gsmi_dev
.handshake_type
= GSMI_HANDSHAKE_SPIN
;
858 gsmi_dev
.handshake_type
=
859 gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_HANDSHAKE_TYPE
);
860 if (gsmi_dev
.handshake_type
== -ENOSYS
)
861 gsmi_dev
.handshake_type
= GSMI_HANDSHAKE_NONE
;
862 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
864 /* Remove and clean up gsmi if the handshake could not complete. */
865 if (gsmi_dev
.handshake_type
== -ENXIO
) {
866 printk(KERN_INFO
"gsmi version " DRIVER_VERSION
867 " failed to load\n");
872 printk(KERN_INFO
"gsmi version " DRIVER_VERSION
" loaded\n");
874 /* Register in the firmware directory */
876 gsmi_kobj
= kobject_create_and_add("gsmi", firmware_kobj
);
878 printk(KERN_INFO
"gsmi: Failed to create firmware kobj\n");
882 /* Setup eventlog access */
883 ret
= sysfs_create_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
885 printk(KERN_INFO
"gsmi: Failed to setup eventlog");
889 /* Other attributes */
890 ret
= sysfs_create_files(gsmi_kobj
, gsmi_attrs
);
892 printk(KERN_INFO
"gsmi: Failed to add attrs");
896 if (register_efivars(&efivars
, &efivar_ops
, gsmi_kobj
)) {
897 printk(KERN_INFO
"gsmi: Failed to register efivars\n");
901 register_reboot_notifier(&gsmi_reboot_notifier
);
902 register_die_notifier(&gsmi_die_notifier
);
903 atomic_notifier_chain_register(&panic_notifier_list
,
904 &gsmi_panic_notifier
);
909 kobject_put(gsmi_kobj
);
910 gsmi_buf_free(gsmi_dev
.param_buf
);
911 gsmi_buf_free(gsmi_dev
.data_buf
);
912 gsmi_buf_free(gsmi_dev
.name_buf
);
913 if (gsmi_dev
.dma_pool
)
914 dma_pool_destroy(gsmi_dev
.dma_pool
);
915 platform_device_unregister(gsmi_dev
.pdev
);
916 pr_info("gsmi: failed to load: %d\n", ret
);
920 static void __exit
gsmi_exit(void)
922 unregister_reboot_notifier(&gsmi_reboot_notifier
);
923 unregister_die_notifier(&gsmi_die_notifier
);
924 atomic_notifier_chain_unregister(&panic_notifier_list
,
925 &gsmi_panic_notifier
);
926 unregister_efivars(&efivars
);
928 kobject_put(gsmi_kobj
);
929 gsmi_buf_free(gsmi_dev
.param_buf
);
930 gsmi_buf_free(gsmi_dev
.data_buf
);
931 gsmi_buf_free(gsmi_dev
.name_buf
);
932 dma_pool_destroy(gsmi_dev
.dma_pool
);
933 platform_device_unregister(gsmi_dev
.pdev
);
936 module_init(gsmi_init
);
937 module_exit(gsmi_exit
);
939 MODULE_AUTHOR("Google, Inc.");
940 MODULE_LICENSE("GPL");