1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2010 Google Inc. All Rights Reserved.
4 * Author: dlaurie@google.com (Duncan Laurie)
6 * Re-worked to expose sysfs APIs by mikew@google.com (Mike Waychison)
8 * EFI SMI interface for Google platforms
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/device.h>
15 #include <linux/platform_device.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/spinlock.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/dmapool.h>
22 #include <linux/slab.h>
23 #include <linux/ioctl.h>
24 #include <linux/acpi.h>
26 #include <linux/uaccess.h>
27 #include <linux/dmi.h>
28 #include <linux/kdebug.h>
29 #include <linux/reboot.h>
30 #include <linux/efi.h>
31 #include <linux/module.h>
32 #include <linux/ucs2_string.h>
33 #include <linux/suspend.h>
35 #define GSMI_SHUTDOWN_CLEAN 0 /* Clean Shutdown */
36 /* TODO(mikew@google.com): Tie in HARDLOCKUP_DETECTOR with NMIWDT */
37 #define GSMI_SHUTDOWN_NMIWDT 1 /* NMI Watchdog */
38 #define GSMI_SHUTDOWN_PANIC 2 /* Panic */
39 #define GSMI_SHUTDOWN_OOPS 3 /* Oops */
40 #define GSMI_SHUTDOWN_DIE 4 /* Die -- No longer meaningful */
41 #define GSMI_SHUTDOWN_MCE 5 /* Machine Check */
42 #define GSMI_SHUTDOWN_SOFTWDT 6 /* Software Watchdog */
43 #define GSMI_SHUTDOWN_MBE 7 /* Uncorrected ECC */
44 #define GSMI_SHUTDOWN_TRIPLE 8 /* Triple Fault */
46 #define DRIVER_VERSION "1.0"
47 #define GSMI_GUID_SIZE 16
48 #define GSMI_BUF_SIZE 1024
49 #define GSMI_BUF_ALIGN sizeof(u64)
50 #define GSMI_CALLBACK 0xef
52 /* SMI return codes */
53 #define GSMI_SUCCESS 0x00
54 #define GSMI_UNSUPPORTED2 0x03
55 #define GSMI_LOG_FULL 0x0b
56 #define GSMI_VAR_NOT_FOUND 0x0e
57 #define GSMI_HANDSHAKE_SPIN 0x7d
58 #define GSMI_HANDSHAKE_CF 0x7e
59 #define GSMI_HANDSHAKE_NONE 0x7f
60 #define GSMI_INVALID_PARAMETER 0x82
61 #define GSMI_UNSUPPORTED 0x83
62 #define GSMI_BUFFER_TOO_SMALL 0x85
63 #define GSMI_NOT_READY 0x86
64 #define GSMI_DEVICE_ERROR 0x87
65 #define GSMI_NOT_FOUND 0x8e
67 #define QUIRKY_BOARD_HASH 0x78a30a50
69 /* Internally used commands passed to the firmware */
70 #define GSMI_CMD_GET_NVRAM_VAR 0x01
71 #define GSMI_CMD_GET_NEXT_VAR 0x02
72 #define GSMI_CMD_SET_NVRAM_VAR 0x03
73 #define GSMI_CMD_SET_EVENT_LOG 0x08
74 #define GSMI_CMD_CLEAR_EVENT_LOG 0x09
75 #define GSMI_CMD_LOG_S0IX_SUSPEND 0x0a
76 #define GSMI_CMD_LOG_S0IX_RESUME 0x0b
77 #define GSMI_CMD_CLEAR_CONFIG 0x20
78 #define GSMI_CMD_HANDSHAKE_TYPE 0xC1
79 #define GSMI_CMD_RESERVED 0xff
81 /* Magic entry type for kernel events */
82 #define GSMI_LOG_ENTRY_TYPE_KERNEL 0xDEAD
84 /* SMI buffers must be in 32bit physical address space */
86 u8
*start
; /* start of buffer */
87 size_t length
; /* length of buffer */
88 dma_addr_t handle
; /* dma allocation handle */
89 u32 address
; /* physical address of buffer */
92 static struct gsmi_device
{
93 struct platform_device
*pdev
; /* platform device */
94 struct gsmi_buf
*name_buf
; /* variable name buffer */
95 struct gsmi_buf
*data_buf
; /* generic data buffer */
96 struct gsmi_buf
*param_buf
; /* parameter buffer */
97 spinlock_t lock
; /* serialize access to SMIs */
98 u16 smi_cmd
; /* SMI command port */
99 int handshake_type
; /* firmware handler interlock type */
100 struct dma_pool
*dma_pool
; /* DMA buffer pool */
103 /* Packed structures for communicating with the firmware */
104 struct gsmi_nvram_var_param
{
112 struct gsmi_get_next_var_param
{
113 u8 guid
[GSMI_GUID_SIZE
];
118 struct gsmi_set_eventlog_param
{
124 /* Event log formats */
125 struct gsmi_log_entry_type_1
{
131 * Some platforms don't have explicit SMI handshake
132 * and need to wait for SMI to complete.
134 #define GSMI_DEFAULT_SPINCOUNT 0x10000
135 static unsigned int spincount
= GSMI_DEFAULT_SPINCOUNT
;
136 module_param(spincount
, uint
, 0600);
137 MODULE_PARM_DESC(spincount
,
138 "The number of loop iterations to use when using the spin handshake.");
141 * Platforms might not support S0ix logging in their GSMI handlers. In order to
142 * avoid any side-effects of generating an SMI for S0ix logging, use the S0ix
143 * related GSMI commands only for those platforms that explicitly enable this
146 static bool s0ix_logging_enable
;
147 module_param(s0ix_logging_enable
, bool, 0600);
149 static struct gsmi_buf
*gsmi_buf_alloc(void)
151 struct gsmi_buf
*smibuf
;
153 smibuf
= kzalloc(sizeof(*smibuf
), GFP_KERNEL
);
155 printk(KERN_ERR
"gsmi: out of memory\n");
159 /* allocate buffer in 32bit address space */
160 smibuf
->start
= dma_pool_alloc(gsmi_dev
.dma_pool
, GFP_KERNEL
,
162 if (!smibuf
->start
) {
163 printk(KERN_ERR
"gsmi: failed to allocate name buffer\n");
168 /* fill in the buffer handle */
169 smibuf
->length
= GSMI_BUF_SIZE
;
170 smibuf
->address
= (u32
)virt_to_phys(smibuf
->start
);
175 static void gsmi_buf_free(struct gsmi_buf
*smibuf
)
179 dma_pool_free(gsmi_dev
.dma_pool
, smibuf
->start
,
186 * Make a call to gsmi func(sub). GSMI error codes are translated to
187 * in-kernel errnos (0 on success, -ERRNO on error).
189 static int gsmi_exec(u8 func
, u8 sub
)
191 u16 cmd
= (sub
<< 8) | func
;
196 * AH : Subfunction number
197 * AL : Function number
198 * EBX : Parameter block address
199 * DX : SMI command port
201 * Three protocols here. See also the comment in gsmi_init().
203 if (gsmi_dev
.handshake_type
== GSMI_HANDSHAKE_CF
) {
205 * If handshake_type == HANDSHAKE_CF then set CF on the
206 * way in and wait for the handler to clear it; this avoids
207 * corrupting register state on those chipsets which have
208 * a delay between writing the SMI trigger register and
217 "d" (gsmi_dev
.smi_cmd
),
218 "b" (gsmi_dev
.param_buf
->address
)
221 } else if (gsmi_dev
.handshake_type
== GSMI_HANDSHAKE_SPIN
) {
223 * If handshake_type == HANDSHAKE_SPIN we spin a
224 * hundred-ish usecs to ensure the SMI has triggered.
231 "d" (gsmi_dev
.smi_cmd
),
232 "b" (gsmi_dev
.param_buf
->address
),
238 * If handshake_type == HANDSHAKE_NONE we do nothing;
239 * either we don't need to or it's legacy firmware that
240 * doesn't understand the CF protocol.
243 "outb %%al, %%dx\n\t"
246 "d" (gsmi_dev
.smi_cmd
),
247 "b" (gsmi_dev
.param_buf
->address
)
252 /* check return code from SMI handler */
256 case GSMI_VAR_NOT_FOUND
:
257 /* not really an error, but let the caller know */
260 case GSMI_INVALID_PARAMETER
:
261 printk(KERN_ERR
"gsmi: exec 0x%04x: Invalid parameter\n", cmd
);
264 case GSMI_BUFFER_TOO_SMALL
:
265 printk(KERN_ERR
"gsmi: exec 0x%04x: Buffer too small\n", cmd
);
268 case GSMI_UNSUPPORTED
:
269 case GSMI_UNSUPPORTED2
:
270 if (sub
!= GSMI_CMD_HANDSHAKE_TYPE
)
271 printk(KERN_ERR
"gsmi: exec 0x%04x: Not supported\n",
276 printk(KERN_ERR
"gsmi: exec 0x%04x: Not ready\n", cmd
);
279 case GSMI_DEVICE_ERROR
:
280 printk(KERN_ERR
"gsmi: exec 0x%04x: Device error\n", cmd
);
284 printk(KERN_ERR
"gsmi: exec 0x%04x: Data not found\n", cmd
);
288 printk(KERN_ERR
"gsmi: exec 0x%04x: Log full\n", cmd
);
291 case GSMI_HANDSHAKE_CF
:
292 case GSMI_HANDSHAKE_SPIN
:
293 case GSMI_HANDSHAKE_NONE
:
297 printk(KERN_ERR
"gsmi: exec 0x%04x: Unknown error 0x%04x\n",
305 #ifdef CONFIG_EFI_VARS
307 static struct efivars efivars
;
309 static efi_status_t
gsmi_get_variable(efi_char16_t
*name
,
310 efi_guid_t
*vendor
, u32
*attr
,
311 unsigned long *data_size
,
314 struct gsmi_nvram_var_param param
= {
315 .name_ptr
= gsmi_dev
.name_buf
->address
,
316 .data_ptr
= gsmi_dev
.data_buf
->address
,
317 .data_len
= (u32
)*data_size
,
319 efi_status_t ret
= EFI_SUCCESS
;
321 size_t name_len
= ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2);
324 if (name_len
>= GSMI_BUF_SIZE
/ 2)
325 return EFI_BAD_BUFFER_SIZE
;
327 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
330 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
332 /* variable name, already in UTF-16 */
333 memset(gsmi_dev
.name_buf
->start
, 0, gsmi_dev
.name_buf
->length
);
334 memcpy(gsmi_dev
.name_buf
->start
, name
, name_len
* 2);
337 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
339 /* parameter buffer */
340 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
341 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
343 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_GET_NVRAM_VAR
);
345 printk(KERN_ERR
"gsmi: Get Variable failed\n");
346 ret
= EFI_LOAD_ERROR
;
347 } else if (rc
== 1) {
348 /* variable was not found */
351 /* Get the arguments back */
352 memcpy(¶m
, gsmi_dev
.param_buf
->start
, sizeof(param
));
354 /* The size reported is the min of all of our buffers */
355 *data_size
= min_t(unsigned long, *data_size
,
356 gsmi_dev
.data_buf
->length
);
357 *data_size
= min_t(unsigned long, *data_size
, param
.data_len
);
359 /* Copy data back to return buffer. */
360 memcpy(data
, gsmi_dev
.data_buf
->start
, *data_size
);
362 /* All variables are have the following attributes */
363 *attr
= EFI_VARIABLE_NON_VOLATILE
|
364 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
365 EFI_VARIABLE_RUNTIME_ACCESS
;
368 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
373 static efi_status_t
gsmi_get_next_variable(unsigned long *name_size
,
377 struct gsmi_get_next_var_param param
= {
378 .name_ptr
= gsmi_dev
.name_buf
->address
,
379 .name_len
= gsmi_dev
.name_buf
->length
,
381 efi_status_t ret
= EFI_SUCCESS
;
385 /* For the moment, only support buffers that exactly match in size */
386 if (*name_size
!= GSMI_BUF_SIZE
)
387 return EFI_BAD_BUFFER_SIZE
;
389 /* Let's make sure the thing is at least null-terminated */
390 if (ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2) == GSMI_BUF_SIZE
/ 2)
391 return EFI_INVALID_PARAMETER
;
393 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
396 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
398 /* variable name, already in UTF-16 */
399 memcpy(gsmi_dev
.name_buf
->start
, name
, *name_size
);
401 /* parameter buffer */
402 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
403 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
405 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_GET_NEXT_VAR
);
407 printk(KERN_ERR
"gsmi: Get Next Variable Name failed\n");
408 ret
= EFI_LOAD_ERROR
;
409 } else if (rc
== 1) {
410 /* variable not found -- end of list */
413 /* copy variable data back to return buffer */
414 memcpy(¶m
, gsmi_dev
.param_buf
->start
, sizeof(param
));
416 /* Copy the name back */
417 memcpy(name
, gsmi_dev
.name_buf
->start
, GSMI_BUF_SIZE
);
418 *name_size
= ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2) * 2;
420 /* copy guid to return buffer */
421 memcpy(vendor
, ¶m
.guid
, sizeof(param
.guid
));
425 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
430 static efi_status_t
gsmi_set_variable(efi_char16_t
*name
,
433 unsigned long data_size
,
436 struct gsmi_nvram_var_param param
= {
437 .name_ptr
= gsmi_dev
.name_buf
->address
,
438 .data_ptr
= gsmi_dev
.data_buf
->address
,
439 .data_len
= (u32
)data_size
,
440 .attributes
= EFI_VARIABLE_NON_VOLATILE
|
441 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
442 EFI_VARIABLE_RUNTIME_ACCESS
,
444 size_t name_len
= ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2);
445 efi_status_t ret
= EFI_SUCCESS
;
449 if (name_len
>= GSMI_BUF_SIZE
/ 2)
450 return EFI_BAD_BUFFER_SIZE
;
452 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
455 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
457 /* variable name, already in UTF-16 */
458 memset(gsmi_dev
.name_buf
->start
, 0, gsmi_dev
.name_buf
->length
);
459 memcpy(gsmi_dev
.name_buf
->start
, name
, name_len
* 2);
462 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
463 memcpy(gsmi_dev
.data_buf
->start
, data
, data_size
);
465 /* parameter buffer */
466 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
467 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
469 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_NVRAM_VAR
);
471 printk(KERN_ERR
"gsmi: Set Variable failed\n");
472 ret
= EFI_INVALID_PARAMETER
;
475 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
480 static const struct efivar_operations efivar_ops
= {
481 .get_variable
= gsmi_get_variable
,
482 .set_variable
= gsmi_set_variable
,
483 .get_next_variable
= gsmi_get_next_variable
,
486 #endif /* CONFIG_EFI_VARS */
488 static ssize_t
eventlog_write(struct file
*filp
, struct kobject
*kobj
,
489 struct bin_attribute
*bin_attr
,
490 char *buf
, loff_t pos
, size_t count
)
492 struct gsmi_set_eventlog_param param
= {
493 .data_ptr
= gsmi_dev
.data_buf
->address
,
498 /* Pull the type out */
499 if (count
< sizeof(u32
))
501 param
.type
= *(u32
*)buf
;
504 /* The remaining buffer is the data payload */
505 if ((count
- sizeof(u32
)) > gsmi_dev
.data_buf
->length
)
507 param
.data_len
= count
- sizeof(u32
);
509 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
512 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
513 memcpy(gsmi_dev
.data_buf
->start
, buf
, param
.data_len
);
515 /* parameter buffer */
516 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
517 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
519 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_EVENT_LOG
);
521 printk(KERN_ERR
"gsmi: Set Event Log failed\n");
523 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
525 return (rc
== 0) ? count
: rc
;
529 static struct bin_attribute eventlog_bin_attr
= {
530 .attr
= {.name
= "append_to_eventlog", .mode
= 0200},
531 .write
= eventlog_write
,
534 static ssize_t
gsmi_clear_eventlog_store(struct kobject
*kobj
,
535 struct kobj_attribute
*attr
,
536 const char *buf
, size_t count
)
546 rc
= kstrtoul(buf
, 0, &val
);
551 * Value entered is a percentage, 0 through 100, anything else
557 /* data_type here selects the smbios event log. */
558 param
.percentage
= val
;
561 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
563 /* parameter buffer */
564 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
565 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
567 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_CLEAR_EVENT_LOG
);
569 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
576 static struct kobj_attribute gsmi_clear_eventlog_attr
= {
577 .attr
= {.name
= "clear_eventlog", .mode
= 0200},
578 .store
= gsmi_clear_eventlog_store
,
581 static ssize_t
gsmi_clear_config_store(struct kobject
*kobj
,
582 struct kobj_attribute
*attr
,
583 const char *buf
, size_t count
)
588 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
590 /* clear parameter buffer */
591 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
593 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_CLEAR_CONFIG
);
595 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
602 static struct kobj_attribute gsmi_clear_config_attr
= {
603 .attr
= {.name
= "clear_config", .mode
= 0200},
604 .store
= gsmi_clear_config_store
,
607 static const struct attribute
*gsmi_attrs
[] = {
608 &gsmi_clear_config_attr
.attr
,
609 &gsmi_clear_eventlog_attr
.attr
,
613 static int gsmi_shutdown_reason(int reason
)
615 struct gsmi_log_entry_type_1 entry
= {
616 .type
= GSMI_LOG_ENTRY_TYPE_KERNEL
,
619 struct gsmi_set_eventlog_param param
= {
620 .data_len
= sizeof(entry
),
623 static int saved_reason
;
627 /* avoid duplicate entries in the log */
628 if (saved_reason
& (1 << reason
))
631 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
633 saved_reason
|= (1 << reason
);
636 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
637 memcpy(gsmi_dev
.data_buf
->start
, &entry
, sizeof(entry
));
639 /* parameter buffer */
640 param
.data_ptr
= gsmi_dev
.data_buf
->address
;
641 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
642 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
644 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_EVENT_LOG
);
646 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
649 printk(KERN_ERR
"gsmi: Log Shutdown Reason failed\n");
651 printk(KERN_EMERG
"gsmi: Log Shutdown Reason 0x%02x\n",
657 static int gsmi_reboot_callback(struct notifier_block
*nb
,
658 unsigned long reason
, void *arg
)
660 gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN
);
664 static struct notifier_block gsmi_reboot_notifier
= {
665 .notifier_call
= gsmi_reboot_callback
668 static int gsmi_die_callback(struct notifier_block
*nb
,
669 unsigned long reason
, void *arg
)
671 if (reason
== DIE_OOPS
)
672 gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS
);
676 static struct notifier_block gsmi_die_notifier
= {
677 .notifier_call
= gsmi_die_callback
680 static int gsmi_panic_callback(struct notifier_block
*nb
,
681 unsigned long reason
, void *arg
)
683 gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC
);
687 static struct notifier_block gsmi_panic_notifier
= {
688 .notifier_call
= gsmi_panic_callback
,
692 * This hash function was blatantly copied from include/linux/hash.h.
693 * It is used by this driver to obfuscate a board name that requires a
694 * quirk within this driver.
696 * Please do not remove this copy of the function as any changes to the
697 * global utility hash_64() function would break this driver's ability
698 * to identify a board and provide the appropriate quirk -- mikew@google.com
700 static u64 __init
local_hash_64(u64 val
, unsigned bits
)
704 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
719 /* High bits are more random, so use them. */
720 return hash
>> (64 - bits
);
723 static u32 __init
hash_oem_table_id(char s
[8])
726 memcpy(&input
, s
, 8);
727 return local_hash_64(input
, 32);
730 static const struct dmi_system_id gsmi_dmi_table
[] __initconst
= {
732 .ident
= "Google Board",
734 DMI_MATCH(DMI_BOARD_VENDOR
, "Google, Inc."),
738 .ident
= "Coreboot Firmware",
740 DMI_MATCH(DMI_BIOS_VENDOR
, "coreboot"),
745 MODULE_DEVICE_TABLE(dmi
, gsmi_dmi_table
);
747 static __init
int gsmi_system_valid(void)
752 if (!dmi_check_system(gsmi_dmi_table
))
756 * Only newer firmware supports the gsmi interface. All older
757 * firmware that didn't support this interface used to plug the
758 * table name in the first four bytes of the oem_table_id field.
759 * Newer firmware doesn't do that though, so use that as the
760 * discriminant factor. We have to do this in order to
761 * whitewash our board names out of the public driver.
763 if (!strncmp(acpi_gbl_FADT
.header
.oem_table_id
, "FACP", 4)) {
764 printk(KERN_INFO
"gsmi: Board is too old\n");
768 /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
769 hash
= hash_oem_table_id(acpi_gbl_FADT
.header
.oem_table_id
);
770 if (hash
== QUIRKY_BOARD_HASH
) {
771 const char *bios_ver
= dmi_get_system_info(DMI_BIOS_VERSION
);
772 if (strncmp(bios_ver
, "1.0", 3) == 0) {
773 pr_info("gsmi: disabled on this board's BIOS %s\n",
779 /* check for valid SMI command port in ACPI FADT */
780 if (acpi_gbl_FADT
.smi_command
== 0) {
781 pr_info("gsmi: missing smi_command\n");
785 /* Test the smihandler with a bogus command. If it leaves the
786 * calling argument in %ax untouched, there is no handler for
789 cmd
= GSMI_CALLBACK
| GSMI_CMD_RESERVED
<< 8;
791 "outb %%al, %%dx\n\t"
794 "d" (acpi_gbl_FADT
.smi_command
)
798 pr_info("gsmi: no gsmi handler in firmware\n");
806 static struct kobject
*gsmi_kobj
;
808 static const struct platform_device_info gsmi_dev_info
= {
811 /* SMI callbacks require 32bit addresses */
812 .dma_mask
= DMA_BIT_MASK(32),
816 static void gsmi_log_s0ix_info(u8 cmd
)
821 * If platform has not enabled S0ix logging, then no action is
824 if (!s0ix_logging_enable
)
827 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
829 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
831 gsmi_exec(GSMI_CALLBACK
, cmd
);
833 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
836 static int gsmi_log_s0ix_suspend(struct device
*dev
)
839 * If system is not suspending via firmware using the standard ACPI Sx
840 * types, then make a GSMI call to log the suspend info.
842 if (!pm_suspend_via_firmware())
843 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND
);
846 * Always return success, since we do not want suspend
847 * to fail just because of logging failure.
852 static int gsmi_log_s0ix_resume(struct device
*dev
)
855 * If system did not resume via firmware, then make a GSMI call to log
856 * the resume info and wake source.
858 if (!pm_resume_via_firmware())
859 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME
);
862 * Always return success, since we do not want resume
863 * to fail just because of logging failure.
868 static const struct dev_pm_ops gsmi_pm_ops
= {
869 .suspend_noirq
= gsmi_log_s0ix_suspend
,
870 .resume_noirq
= gsmi_log_s0ix_resume
,
873 static int gsmi_platform_driver_probe(struct platform_device
*dev
)
878 static struct platform_driver gsmi_driver_info
= {
883 .probe
= gsmi_platform_driver_probe
,
887 static __init
int gsmi_init(void)
892 ret
= gsmi_system_valid();
896 gsmi_dev
.smi_cmd
= acpi_gbl_FADT
.smi_command
;
899 ret
= platform_driver_register(&gsmi_driver_info
);
901 printk(KERN_ERR
"gsmi: unable to register platform driver\n");
906 /* register device */
907 gsmi_dev
.pdev
= platform_device_register_full(&gsmi_dev_info
);
908 if (IS_ERR(gsmi_dev
.pdev
)) {
909 printk(KERN_ERR
"gsmi: unable to register platform device\n");
910 return PTR_ERR(gsmi_dev
.pdev
);
913 /* SMI access needs to be serialized */
914 spin_lock_init(&gsmi_dev
.lock
);
917 gsmi_dev
.dma_pool
= dma_pool_create("gsmi", &gsmi_dev
.pdev
->dev
,
918 GSMI_BUF_SIZE
, GSMI_BUF_ALIGN
, 0);
919 if (!gsmi_dev
.dma_pool
)
923 * pre-allocate buffers because sometimes we are called when
924 * this is not feasible: oops, panic, die, mce, etc
926 gsmi_dev
.name_buf
= gsmi_buf_alloc();
927 if (!gsmi_dev
.name_buf
) {
928 printk(KERN_ERR
"gsmi: failed to allocate name buffer\n");
932 gsmi_dev
.data_buf
= gsmi_buf_alloc();
933 if (!gsmi_dev
.data_buf
) {
934 printk(KERN_ERR
"gsmi: failed to allocate data buffer\n");
938 gsmi_dev
.param_buf
= gsmi_buf_alloc();
939 if (!gsmi_dev
.param_buf
) {
940 printk(KERN_ERR
"gsmi: failed to allocate param buffer\n");
945 * Determine type of handshake used to serialize the SMI
946 * entry. See also gsmi_exec().
948 * There's a "behavior" present on some chipsets where writing the
949 * SMI trigger register in the southbridge doesn't result in an
950 * immediate SMI. Rather, the processor can execute "a few" more
951 * instructions before the SMI takes effect. To ensure synchronous
952 * behavior, implement a handshake between the kernel driver and the
953 * firmware handler to spin until released. This ioctl determines
954 * the type of handshake.
956 * NONE: The firmware handler does not implement any
957 * handshake. Either it doesn't need to, or it's legacy firmware
958 * that doesn't know it needs to and never will.
960 * CF: The firmware handler will clear the CF in the saved
961 * state before returning. The driver may set the CF and test for
962 * it to clear before proceeding.
964 * SPIN: The firmware handler does not implement any handshake
965 * but the driver should spin for a hundred or so microseconds
966 * to ensure the SMI has triggered.
968 * Finally, the handler will return -ENOSYS if
969 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
972 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
973 gsmi_dev
.handshake_type
= GSMI_HANDSHAKE_SPIN
;
974 gsmi_dev
.handshake_type
=
975 gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_HANDSHAKE_TYPE
);
976 if (gsmi_dev
.handshake_type
== -ENOSYS
)
977 gsmi_dev
.handshake_type
= GSMI_HANDSHAKE_NONE
;
978 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
980 /* Remove and clean up gsmi if the handshake could not complete. */
981 if (gsmi_dev
.handshake_type
== -ENXIO
) {
982 printk(KERN_INFO
"gsmi version " DRIVER_VERSION
983 " failed to load\n");
988 /* Register in the firmware directory */
990 gsmi_kobj
= kobject_create_and_add("gsmi", firmware_kobj
);
992 printk(KERN_INFO
"gsmi: Failed to create firmware kobj\n");
996 /* Setup eventlog access */
997 ret
= sysfs_create_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
999 printk(KERN_INFO
"gsmi: Failed to setup eventlog");
1003 /* Other attributes */
1004 ret
= sysfs_create_files(gsmi_kobj
, gsmi_attrs
);
1006 printk(KERN_INFO
"gsmi: Failed to add attrs");
1007 goto out_remove_bin_file
;
1010 #ifdef CONFIG_EFI_VARS
1011 ret
= efivars_register(&efivars
, &efivar_ops
, gsmi_kobj
);
1013 printk(KERN_INFO
"gsmi: Failed to register efivars\n");
1014 sysfs_remove_files(gsmi_kobj
, gsmi_attrs
);
1015 goto out_remove_bin_file
;
1019 register_reboot_notifier(&gsmi_reboot_notifier
);
1020 register_die_notifier(&gsmi_die_notifier
);
1021 atomic_notifier_chain_register(&panic_notifier_list
,
1022 &gsmi_panic_notifier
);
1024 printk(KERN_INFO
"gsmi version " DRIVER_VERSION
" loaded\n");
1028 out_remove_bin_file
:
1029 sysfs_remove_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
1031 kobject_put(gsmi_kobj
);
1032 gsmi_buf_free(gsmi_dev
.param_buf
);
1033 gsmi_buf_free(gsmi_dev
.data_buf
);
1034 gsmi_buf_free(gsmi_dev
.name_buf
);
1035 dma_pool_destroy(gsmi_dev
.dma_pool
);
1036 platform_device_unregister(gsmi_dev
.pdev
);
1037 pr_info("gsmi: failed to load: %d\n", ret
);
1039 platform_driver_unregister(&gsmi_driver_info
);
1044 static void __exit
gsmi_exit(void)
1046 unregister_reboot_notifier(&gsmi_reboot_notifier
);
1047 unregister_die_notifier(&gsmi_die_notifier
);
1048 atomic_notifier_chain_unregister(&panic_notifier_list
,
1049 &gsmi_panic_notifier
);
1050 #ifdef CONFIG_EFI_VARS
1051 efivars_unregister(&efivars
);
1054 sysfs_remove_files(gsmi_kobj
, gsmi_attrs
);
1055 sysfs_remove_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
1056 kobject_put(gsmi_kobj
);
1057 gsmi_buf_free(gsmi_dev
.param_buf
);
1058 gsmi_buf_free(gsmi_dev
.data_buf
);
1059 gsmi_buf_free(gsmi_dev
.name_buf
);
1060 dma_pool_destroy(gsmi_dev
.dma_pool
);
1061 platform_device_unregister(gsmi_dev
.pdev
);
1063 platform_driver_unregister(&gsmi_driver_info
);
1067 module_init(gsmi_init
);
1068 module_exit(gsmi_exit
);
1070 MODULE_AUTHOR("Google, Inc.");
1071 MODULE_LICENSE("GPL");