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>
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>
30 #include <linux/module.h>
31 #include <linux/ucs2_string.h>
32 #include <linux/suspend.h>
34 #define GSMI_SHUTDOWN_CLEAN 0 /* Clean Shutdown */
35 /* TODO(mikew@google.com): Tie in HARDLOCKUP_DETECTOR with NMIWDT */
36 #define GSMI_SHUTDOWN_NMIWDT 1 /* NMI Watchdog */
37 #define GSMI_SHUTDOWN_PANIC 2 /* Panic */
38 #define GSMI_SHUTDOWN_OOPS 3 /* Oops */
39 #define GSMI_SHUTDOWN_DIE 4 /* Die -- No longer meaningful */
40 #define GSMI_SHUTDOWN_MCE 5 /* Machine Check */
41 #define GSMI_SHUTDOWN_SOFTWDT 6 /* Software Watchdog */
42 #define GSMI_SHUTDOWN_MBE 7 /* Uncorrected ECC */
43 #define GSMI_SHUTDOWN_TRIPLE 8 /* Triple Fault */
45 #define DRIVER_VERSION "1.0"
46 #define GSMI_GUID_SIZE 16
47 #define GSMI_BUF_SIZE 1024
48 #define GSMI_BUF_ALIGN sizeof(u64)
49 #define GSMI_CALLBACK 0xef
51 /* SMI return codes */
52 #define GSMI_SUCCESS 0x00
53 #define GSMI_UNSUPPORTED2 0x03
54 #define GSMI_LOG_FULL 0x0b
55 #define GSMI_VAR_NOT_FOUND 0x0e
56 #define GSMI_HANDSHAKE_SPIN 0x7d
57 #define GSMI_HANDSHAKE_CF 0x7e
58 #define GSMI_HANDSHAKE_NONE 0x7f
59 #define GSMI_INVALID_PARAMETER 0x82
60 #define GSMI_UNSUPPORTED 0x83
61 #define GSMI_BUFFER_TOO_SMALL 0x85
62 #define GSMI_NOT_READY 0x86
63 #define GSMI_DEVICE_ERROR 0x87
64 #define GSMI_NOT_FOUND 0x8e
66 #define QUIRKY_BOARD_HASH 0x78a30a50
68 /* Internally used commands passed to the firmware */
69 #define GSMI_CMD_GET_NVRAM_VAR 0x01
70 #define GSMI_CMD_GET_NEXT_VAR 0x02
71 #define GSMI_CMD_SET_NVRAM_VAR 0x03
72 #define GSMI_CMD_SET_EVENT_LOG 0x08
73 #define GSMI_CMD_CLEAR_EVENT_LOG 0x09
74 #define GSMI_CMD_LOG_S0IX_SUSPEND 0x0a
75 #define GSMI_CMD_LOG_S0IX_RESUME 0x0b
76 #define GSMI_CMD_CLEAR_CONFIG 0x20
77 #define GSMI_CMD_HANDSHAKE_TYPE 0xC1
78 #define GSMI_CMD_RESERVED 0xff
80 /* Magic entry type for kernel events */
81 #define GSMI_LOG_ENTRY_TYPE_KERNEL 0xDEAD
83 /* SMI buffers must be in 32bit physical address space */
85 u8
*start
; /* start of buffer */
86 size_t length
; /* length of buffer */
87 u32 address
; /* physical address of buffer */
90 static struct gsmi_device
{
91 struct platform_device
*pdev
; /* platform device */
92 struct gsmi_buf
*name_buf
; /* variable name buffer */
93 struct gsmi_buf
*data_buf
; /* generic data buffer */
94 struct gsmi_buf
*param_buf
; /* parameter buffer */
95 spinlock_t lock
; /* serialize access to SMIs */
96 u16 smi_cmd
; /* SMI command port */
97 int handshake_type
; /* firmware handler interlock type */
98 struct kmem_cache
*mem_pool
; /* kmem cache for gsmi_buf allocations */
101 /* Packed structures for communicating with the firmware */
102 struct gsmi_nvram_var_param
{
110 struct gsmi_get_next_var_param
{
111 u8 guid
[GSMI_GUID_SIZE
];
116 struct gsmi_set_eventlog_param
{
122 /* Event log formats */
123 struct gsmi_log_entry_type_1
{
129 * Some platforms don't have explicit SMI handshake
130 * and need to wait for SMI to complete.
132 #define GSMI_DEFAULT_SPINCOUNT 0x10000
133 static unsigned int spincount
= GSMI_DEFAULT_SPINCOUNT
;
134 module_param(spincount
, uint
, 0600);
135 MODULE_PARM_DESC(spincount
,
136 "The number of loop iterations to use when using the spin handshake.");
139 * Platforms might not support S0ix logging in their GSMI handlers. In order to
140 * avoid any side-effects of generating an SMI for S0ix logging, use the S0ix
141 * related GSMI commands only for those platforms that explicitly enable this
144 static bool s0ix_logging_enable
;
145 module_param(s0ix_logging_enable
, bool, 0600);
147 static struct gsmi_buf
*gsmi_buf_alloc(void)
149 struct gsmi_buf
*smibuf
;
151 smibuf
= kzalloc(sizeof(*smibuf
), GFP_KERNEL
);
153 printk(KERN_ERR
"gsmi: out of memory\n");
157 /* allocate buffer in 32bit address space */
158 smibuf
->start
= kmem_cache_alloc(gsmi_dev
.mem_pool
, GFP_KERNEL
);
159 if (!smibuf
->start
) {
160 printk(KERN_ERR
"gsmi: failed to allocate name buffer\n");
165 /* fill in the buffer handle */
166 smibuf
->length
= GSMI_BUF_SIZE
;
167 smibuf
->address
= (u32
)virt_to_phys(smibuf
->start
);
172 static void gsmi_buf_free(struct gsmi_buf
*smibuf
)
176 kmem_cache_free(gsmi_dev
.mem_pool
, smibuf
->start
);
182 * Make a call to gsmi func(sub). GSMI error codes are translated to
183 * in-kernel errnos (0 on success, -ERRNO on error).
185 static int gsmi_exec(u8 func
, u8 sub
)
187 u16 cmd
= (sub
<< 8) | func
;
192 * AH : Subfunction number
193 * AL : Function number
194 * EBX : Parameter block address
195 * DX : SMI command port
197 * Three protocols here. See also the comment in gsmi_init().
199 if (gsmi_dev
.handshake_type
== GSMI_HANDSHAKE_CF
) {
201 * If handshake_type == HANDSHAKE_CF then set CF on the
202 * way in and wait for the handler to clear it; this avoids
203 * corrupting register state on those chipsets which have
204 * a delay between writing the SMI trigger register and
213 "d" (gsmi_dev
.smi_cmd
),
214 "b" (gsmi_dev
.param_buf
->address
)
217 } else if (gsmi_dev
.handshake_type
== GSMI_HANDSHAKE_SPIN
) {
219 * If handshake_type == HANDSHAKE_SPIN we spin a
220 * hundred-ish usecs to ensure the SMI has triggered.
227 "d" (gsmi_dev
.smi_cmd
),
228 "b" (gsmi_dev
.param_buf
->address
),
234 * If handshake_type == HANDSHAKE_NONE we do nothing;
235 * either we don't need to or it's legacy firmware that
236 * doesn't understand the CF protocol.
239 "outb %%al, %%dx\n\t"
242 "d" (gsmi_dev
.smi_cmd
),
243 "b" (gsmi_dev
.param_buf
->address
)
248 /* check return code from SMI handler */
252 case GSMI_VAR_NOT_FOUND
:
253 /* not really an error, but let the caller know */
256 case GSMI_INVALID_PARAMETER
:
257 printk(KERN_ERR
"gsmi: exec 0x%04x: Invalid parameter\n", cmd
);
260 case GSMI_BUFFER_TOO_SMALL
:
261 printk(KERN_ERR
"gsmi: exec 0x%04x: Buffer too small\n", cmd
);
264 case GSMI_UNSUPPORTED
:
265 case GSMI_UNSUPPORTED2
:
266 if (sub
!= GSMI_CMD_HANDSHAKE_TYPE
)
267 printk(KERN_ERR
"gsmi: exec 0x%04x: Not supported\n",
272 printk(KERN_ERR
"gsmi: exec 0x%04x: Not ready\n", cmd
);
275 case GSMI_DEVICE_ERROR
:
276 printk(KERN_ERR
"gsmi: exec 0x%04x: Device error\n", cmd
);
280 printk(KERN_ERR
"gsmi: exec 0x%04x: Data not found\n", cmd
);
284 printk(KERN_ERR
"gsmi: exec 0x%04x: Log full\n", cmd
);
287 case GSMI_HANDSHAKE_CF
:
288 case GSMI_HANDSHAKE_SPIN
:
289 case GSMI_HANDSHAKE_NONE
:
293 printk(KERN_ERR
"gsmi: exec 0x%04x: Unknown error 0x%04x\n",
303 static struct efivars efivars
;
305 static efi_status_t
gsmi_get_variable(efi_char16_t
*name
,
306 efi_guid_t
*vendor
, u32
*attr
,
307 unsigned long *data_size
,
310 struct gsmi_nvram_var_param param
= {
311 .name_ptr
= gsmi_dev
.name_buf
->address
,
312 .data_ptr
= gsmi_dev
.data_buf
->address
,
313 .data_len
= (u32
)*data_size
,
315 efi_status_t ret
= EFI_SUCCESS
;
317 size_t name_len
= ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2);
320 if (name_len
>= GSMI_BUF_SIZE
/ 2)
321 return EFI_BAD_BUFFER_SIZE
;
323 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
326 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
328 /* variable name, already in UTF-16 */
329 memset(gsmi_dev
.name_buf
->start
, 0, gsmi_dev
.name_buf
->length
);
330 memcpy(gsmi_dev
.name_buf
->start
, name
, name_len
* 2);
333 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
335 /* parameter buffer */
336 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
337 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
339 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_GET_NVRAM_VAR
);
341 printk(KERN_ERR
"gsmi: Get Variable failed\n");
342 ret
= EFI_LOAD_ERROR
;
343 } else if (rc
== 1) {
344 /* variable was not found */
347 /* Get the arguments back */
348 memcpy(¶m
, gsmi_dev
.param_buf
->start
, sizeof(param
));
350 /* The size reported is the min of all of our buffers */
351 *data_size
= min_t(unsigned long, *data_size
,
352 gsmi_dev
.data_buf
->length
);
353 *data_size
= min_t(unsigned long, *data_size
, param
.data_len
);
355 /* Copy data back to return buffer. */
356 memcpy(data
, gsmi_dev
.data_buf
->start
, *data_size
);
358 /* All variables are have the following attributes */
359 *attr
= EFI_VARIABLE_NON_VOLATILE
|
360 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
361 EFI_VARIABLE_RUNTIME_ACCESS
;
364 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
369 static efi_status_t
gsmi_get_next_variable(unsigned long *name_size
,
373 struct gsmi_get_next_var_param param
= {
374 .name_ptr
= gsmi_dev
.name_buf
->address
,
375 .name_len
= gsmi_dev
.name_buf
->length
,
377 efi_status_t ret
= EFI_SUCCESS
;
381 /* For the moment, only support buffers that exactly match in size */
382 if (*name_size
!= GSMI_BUF_SIZE
)
383 return EFI_BAD_BUFFER_SIZE
;
385 /* Let's make sure the thing is at least null-terminated */
386 if (ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2) == GSMI_BUF_SIZE
/ 2)
387 return EFI_INVALID_PARAMETER
;
389 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
392 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
394 /* variable name, already in UTF-16 */
395 memcpy(gsmi_dev
.name_buf
->start
, name
, *name_size
);
397 /* parameter buffer */
398 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
399 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
401 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_GET_NEXT_VAR
);
403 printk(KERN_ERR
"gsmi: Get Next Variable Name failed\n");
404 ret
= EFI_LOAD_ERROR
;
405 } else if (rc
== 1) {
406 /* variable not found -- end of list */
409 /* copy variable data back to return buffer */
410 memcpy(¶m
, gsmi_dev
.param_buf
->start
, sizeof(param
));
412 /* Copy the name back */
413 memcpy(name
, gsmi_dev
.name_buf
->start
, GSMI_BUF_SIZE
);
414 *name_size
= ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2) * 2;
416 /* copy guid to return buffer */
417 memcpy(vendor
, ¶m
.guid
, sizeof(param
.guid
));
421 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
426 static efi_status_t
gsmi_set_variable(efi_char16_t
*name
,
429 unsigned long data_size
,
432 struct gsmi_nvram_var_param param
= {
433 .name_ptr
= gsmi_dev
.name_buf
->address
,
434 .data_ptr
= gsmi_dev
.data_buf
->address
,
435 .data_len
= (u32
)data_size
,
436 .attributes
= EFI_VARIABLE_NON_VOLATILE
|
437 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
438 EFI_VARIABLE_RUNTIME_ACCESS
,
440 size_t name_len
= ucs2_strnlen(name
, GSMI_BUF_SIZE
/ 2);
441 efi_status_t ret
= EFI_SUCCESS
;
445 if (name_len
>= GSMI_BUF_SIZE
/ 2)
446 return EFI_BAD_BUFFER_SIZE
;
448 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
451 memcpy(¶m
.guid
, vendor
, sizeof(param
.guid
));
453 /* variable name, already in UTF-16 */
454 memset(gsmi_dev
.name_buf
->start
, 0, gsmi_dev
.name_buf
->length
);
455 memcpy(gsmi_dev
.name_buf
->start
, name
, name_len
* 2);
458 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
459 memcpy(gsmi_dev
.data_buf
->start
, data
, data_size
);
461 /* parameter buffer */
462 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
463 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
465 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_NVRAM_VAR
);
467 printk(KERN_ERR
"gsmi: Set Variable failed\n");
468 ret
= EFI_INVALID_PARAMETER
;
471 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
476 static const struct efivar_operations efivar_ops
= {
477 .get_variable
= gsmi_get_variable
,
478 .set_variable
= gsmi_set_variable
,
479 .get_next_variable
= gsmi_get_next_variable
,
482 #endif /* CONFIG_EFI */
484 static ssize_t
eventlog_write(struct file
*filp
, struct kobject
*kobj
,
485 struct bin_attribute
*bin_attr
,
486 char *buf
, loff_t pos
, size_t count
)
488 struct gsmi_set_eventlog_param param
= {
489 .data_ptr
= gsmi_dev
.data_buf
->address
,
494 /* Pull the type out */
495 if (count
< sizeof(u32
))
497 param
.type
= *(u32
*)buf
;
500 /* The remaining buffer is the data payload */
501 if ((count
- sizeof(u32
)) > gsmi_dev
.data_buf
->length
)
503 param
.data_len
= count
- sizeof(u32
);
505 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
508 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
509 memcpy(gsmi_dev
.data_buf
->start
, buf
, param
.data_len
);
511 /* parameter buffer */
512 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
513 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
515 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_EVENT_LOG
);
517 printk(KERN_ERR
"gsmi: Set Event Log failed\n");
519 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
521 return (rc
== 0) ? count
: rc
;
525 static struct bin_attribute eventlog_bin_attr
= {
526 .attr
= {.name
= "append_to_eventlog", .mode
= 0200},
527 .write
= eventlog_write
,
530 static ssize_t
gsmi_clear_eventlog_store(struct kobject
*kobj
,
531 struct kobj_attribute
*attr
,
532 const char *buf
, size_t count
)
542 rc
= kstrtoul(buf
, 0, &val
);
547 * Value entered is a percentage, 0 through 100, anything else
553 /* data_type here selects the smbios event log. */
554 param
.percentage
= val
;
557 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
559 /* parameter buffer */
560 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
561 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
563 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_CLEAR_EVENT_LOG
);
565 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
572 static struct kobj_attribute gsmi_clear_eventlog_attr
= {
573 .attr
= {.name
= "clear_eventlog", .mode
= 0200},
574 .store
= gsmi_clear_eventlog_store
,
577 static ssize_t
gsmi_clear_config_store(struct kobject
*kobj
,
578 struct kobj_attribute
*attr
,
579 const char *buf
, size_t count
)
584 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
586 /* clear parameter buffer */
587 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
589 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_CLEAR_CONFIG
);
591 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
598 static struct kobj_attribute gsmi_clear_config_attr
= {
599 .attr
= {.name
= "clear_config", .mode
= 0200},
600 .store
= gsmi_clear_config_store
,
603 static const struct attribute
*gsmi_attrs
[] = {
604 &gsmi_clear_config_attr
.attr
,
605 &gsmi_clear_eventlog_attr
.attr
,
609 static int gsmi_shutdown_reason(int reason
)
611 struct gsmi_log_entry_type_1 entry
= {
612 .type
= GSMI_LOG_ENTRY_TYPE_KERNEL
,
615 struct gsmi_set_eventlog_param param
= {
616 .data_len
= sizeof(entry
),
619 static int saved_reason
;
623 /* avoid duplicate entries in the log */
624 if (saved_reason
& (1 << reason
))
627 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
629 saved_reason
|= (1 << reason
);
632 memset(gsmi_dev
.data_buf
->start
, 0, gsmi_dev
.data_buf
->length
);
633 memcpy(gsmi_dev
.data_buf
->start
, &entry
, sizeof(entry
));
635 /* parameter buffer */
636 param
.data_ptr
= gsmi_dev
.data_buf
->address
;
637 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
638 memcpy(gsmi_dev
.param_buf
->start
, ¶m
, sizeof(param
));
640 rc
= gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_SET_EVENT_LOG
);
642 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
645 printk(KERN_ERR
"gsmi: Log Shutdown Reason failed\n");
647 printk(KERN_EMERG
"gsmi: Log Shutdown Reason 0x%02x\n",
653 static int gsmi_reboot_callback(struct notifier_block
*nb
,
654 unsigned long reason
, void *arg
)
656 gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN
);
660 static struct notifier_block gsmi_reboot_notifier
= {
661 .notifier_call
= gsmi_reboot_callback
664 static int gsmi_die_callback(struct notifier_block
*nb
,
665 unsigned long reason
, void *arg
)
667 if (reason
== DIE_OOPS
)
668 gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS
);
672 static struct notifier_block gsmi_die_notifier
= {
673 .notifier_call
= gsmi_die_callback
676 static int gsmi_panic_callback(struct notifier_block
*nb
,
677 unsigned long reason
, void *arg
)
679 gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC
);
683 static struct notifier_block gsmi_panic_notifier
= {
684 .notifier_call
= gsmi_panic_callback
,
688 * This hash function was blatantly copied from include/linux/hash.h.
689 * It is used by this driver to obfuscate a board name that requires a
690 * quirk within this driver.
692 * Please do not remove this copy of the function as any changes to the
693 * global utility hash_64() function would break this driver's ability
694 * to identify a board and provide the appropriate quirk -- mikew@google.com
696 static u64 __init
local_hash_64(u64 val
, unsigned bits
)
700 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
715 /* High bits are more random, so use them. */
716 return hash
>> (64 - bits
);
719 static u32 __init
hash_oem_table_id(char s
[8])
722 memcpy(&input
, s
, 8);
723 return local_hash_64(input
, 32);
726 static const struct dmi_system_id gsmi_dmi_table
[] __initconst
= {
728 .ident
= "Google Board",
730 DMI_MATCH(DMI_BOARD_VENDOR
, "Google, Inc."),
734 .ident
= "Coreboot Firmware",
736 DMI_MATCH(DMI_BIOS_VENDOR
, "coreboot"),
741 MODULE_DEVICE_TABLE(dmi
, gsmi_dmi_table
);
743 static __init
int gsmi_system_valid(void)
748 if (!dmi_check_system(gsmi_dmi_table
))
752 * Only newer firmware supports the gsmi interface. All older
753 * firmware that didn't support this interface used to plug the
754 * table name in the first four bytes of the oem_table_id field.
755 * Newer firmware doesn't do that though, so use that as the
756 * discriminant factor. We have to do this in order to
757 * whitewash our board names out of the public driver.
759 if (!strncmp(acpi_gbl_FADT
.header
.oem_table_id
, "FACP", 4)) {
760 printk(KERN_INFO
"gsmi: Board is too old\n");
764 /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
765 hash
= hash_oem_table_id(acpi_gbl_FADT
.header
.oem_table_id
);
766 if (hash
== QUIRKY_BOARD_HASH
) {
767 const char *bios_ver
= dmi_get_system_info(DMI_BIOS_VERSION
);
768 if (strncmp(bios_ver
, "1.0", 3) == 0) {
769 pr_info("gsmi: disabled on this board's BIOS %s\n",
775 /* check for valid SMI command port in ACPI FADT */
776 if (acpi_gbl_FADT
.smi_command
== 0) {
777 pr_info("gsmi: missing smi_command\n");
781 /* Test the smihandler with a bogus command. If it leaves the
782 * calling argument in %ax untouched, there is no handler for
785 cmd
= GSMI_CALLBACK
| GSMI_CMD_RESERVED
<< 8;
787 "outb %%al, %%dx\n\t"
790 "d" (acpi_gbl_FADT
.smi_command
)
794 pr_info("gsmi: no gsmi handler in firmware\n");
802 static struct kobject
*gsmi_kobj
;
804 static const struct platform_device_info gsmi_dev_info
= {
807 /* SMI callbacks require 32bit addresses */
808 .dma_mask
= DMA_BIT_MASK(32),
812 static void gsmi_log_s0ix_info(u8 cmd
)
817 * If platform has not enabled S0ix logging, then no action is
820 if (!s0ix_logging_enable
)
823 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
825 memset(gsmi_dev
.param_buf
->start
, 0, gsmi_dev
.param_buf
->length
);
827 gsmi_exec(GSMI_CALLBACK
, cmd
);
829 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
832 static int gsmi_log_s0ix_suspend(struct device
*dev
)
835 * If system is not suspending via firmware using the standard ACPI Sx
836 * types, then make a GSMI call to log the suspend info.
838 if (!pm_suspend_via_firmware())
839 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND
);
842 * Always return success, since we do not want suspend
843 * to fail just because of logging failure.
848 static int gsmi_log_s0ix_resume(struct device
*dev
)
851 * If system did not resume via firmware, then make a GSMI call to log
852 * the resume info and wake source.
854 if (!pm_resume_via_firmware())
855 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME
);
858 * Always return success, since we do not want resume
859 * to fail just because of logging failure.
864 static const struct dev_pm_ops gsmi_pm_ops
= {
865 .suspend_noirq
= gsmi_log_s0ix_suspend
,
866 .resume_noirq
= gsmi_log_s0ix_resume
,
869 static int gsmi_platform_driver_probe(struct platform_device
*dev
)
874 static struct platform_driver gsmi_driver_info
= {
879 .probe
= gsmi_platform_driver_probe
,
883 static __init
int gsmi_init(void)
888 ret
= gsmi_system_valid();
892 gsmi_dev
.smi_cmd
= acpi_gbl_FADT
.smi_command
;
895 ret
= platform_driver_register(&gsmi_driver_info
);
897 printk(KERN_ERR
"gsmi: unable to register platform driver\n");
902 /* register device */
903 gsmi_dev
.pdev
= platform_device_register_full(&gsmi_dev_info
);
904 if (IS_ERR(gsmi_dev
.pdev
)) {
905 printk(KERN_ERR
"gsmi: unable to register platform device\n");
906 return PTR_ERR(gsmi_dev
.pdev
);
909 /* SMI access needs to be serialized */
910 spin_lock_init(&gsmi_dev
.lock
);
915 * SLAB cache is created using SLAB_CACHE_DMA32 to ensure that the
916 * allocations for gsmi_buf come from the DMA32 memory zone. These
917 * buffers have nothing to do with DMA. They are required for
918 * communication with firmware executing in SMI mode which can only
919 * access the bottom 4GiB of physical memory. Since DMA32 memory zone
920 * guarantees allocation under the 4GiB boundary, this driver creates
921 * a SLAB cache with SLAB_CACHE_DMA32 flag.
923 gsmi_dev
.mem_pool
= kmem_cache_create("gsmi", GSMI_BUF_SIZE
,
925 SLAB_CACHE_DMA32
, NULL
);
926 if (!gsmi_dev
.mem_pool
)
930 * pre-allocate buffers because sometimes we are called when
931 * this is not feasible: oops, panic, die, mce, etc
933 gsmi_dev
.name_buf
= gsmi_buf_alloc();
934 if (!gsmi_dev
.name_buf
) {
935 printk(KERN_ERR
"gsmi: failed to allocate name buffer\n");
939 gsmi_dev
.data_buf
= gsmi_buf_alloc();
940 if (!gsmi_dev
.data_buf
) {
941 printk(KERN_ERR
"gsmi: failed to allocate data buffer\n");
945 gsmi_dev
.param_buf
= gsmi_buf_alloc();
946 if (!gsmi_dev
.param_buf
) {
947 printk(KERN_ERR
"gsmi: failed to allocate param buffer\n");
952 * Determine type of handshake used to serialize the SMI
953 * entry. See also gsmi_exec().
955 * There's a "behavior" present on some chipsets where writing the
956 * SMI trigger register in the southbridge doesn't result in an
957 * immediate SMI. Rather, the processor can execute "a few" more
958 * instructions before the SMI takes effect. To ensure synchronous
959 * behavior, implement a handshake between the kernel driver and the
960 * firmware handler to spin until released. This ioctl determines
961 * the type of handshake.
963 * NONE: The firmware handler does not implement any
964 * handshake. Either it doesn't need to, or it's legacy firmware
965 * that doesn't know it needs to and never will.
967 * CF: The firmware handler will clear the CF in the saved
968 * state before returning. The driver may set the CF and test for
969 * it to clear before proceeding.
971 * SPIN: The firmware handler does not implement any handshake
972 * but the driver should spin for a hundred or so microseconds
973 * to ensure the SMI has triggered.
975 * Finally, the handler will return -ENOSYS if
976 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
979 spin_lock_irqsave(&gsmi_dev
.lock
, flags
);
980 gsmi_dev
.handshake_type
= GSMI_HANDSHAKE_SPIN
;
981 gsmi_dev
.handshake_type
=
982 gsmi_exec(GSMI_CALLBACK
, GSMI_CMD_HANDSHAKE_TYPE
);
983 if (gsmi_dev
.handshake_type
== -ENOSYS
)
984 gsmi_dev
.handshake_type
= GSMI_HANDSHAKE_NONE
;
985 spin_unlock_irqrestore(&gsmi_dev
.lock
, flags
);
987 /* Remove and clean up gsmi if the handshake could not complete. */
988 if (gsmi_dev
.handshake_type
== -ENXIO
) {
989 printk(KERN_INFO
"gsmi version " DRIVER_VERSION
990 " failed to load\n");
995 /* Register in the firmware directory */
997 gsmi_kobj
= kobject_create_and_add("gsmi", firmware_kobj
);
999 printk(KERN_INFO
"gsmi: Failed to create firmware kobj\n");
1003 /* Setup eventlog access */
1004 ret
= sysfs_create_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
1006 printk(KERN_INFO
"gsmi: Failed to setup eventlog");
1010 /* Other attributes */
1011 ret
= sysfs_create_files(gsmi_kobj
, gsmi_attrs
);
1013 printk(KERN_INFO
"gsmi: Failed to add attrs");
1014 goto out_remove_bin_file
;
1018 ret
= efivars_register(&efivars
, &efivar_ops
, gsmi_kobj
);
1020 printk(KERN_INFO
"gsmi: Failed to register efivars\n");
1021 sysfs_remove_files(gsmi_kobj
, gsmi_attrs
);
1022 goto out_remove_bin_file
;
1026 register_reboot_notifier(&gsmi_reboot_notifier
);
1027 register_die_notifier(&gsmi_die_notifier
);
1028 atomic_notifier_chain_register(&panic_notifier_list
,
1029 &gsmi_panic_notifier
);
1031 printk(KERN_INFO
"gsmi version " DRIVER_VERSION
" loaded\n");
1035 out_remove_bin_file
:
1036 sysfs_remove_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
1038 kobject_put(gsmi_kobj
);
1039 gsmi_buf_free(gsmi_dev
.param_buf
);
1040 gsmi_buf_free(gsmi_dev
.data_buf
);
1041 gsmi_buf_free(gsmi_dev
.name_buf
);
1042 kmem_cache_destroy(gsmi_dev
.mem_pool
);
1043 platform_device_unregister(gsmi_dev
.pdev
);
1044 pr_info("gsmi: failed to load: %d\n", ret
);
1046 platform_driver_unregister(&gsmi_driver_info
);
1051 static void __exit
gsmi_exit(void)
1053 unregister_reboot_notifier(&gsmi_reboot_notifier
);
1054 unregister_die_notifier(&gsmi_die_notifier
);
1055 atomic_notifier_chain_unregister(&panic_notifier_list
,
1056 &gsmi_panic_notifier
);
1058 efivars_unregister(&efivars
);
1061 sysfs_remove_files(gsmi_kobj
, gsmi_attrs
);
1062 sysfs_remove_bin_file(gsmi_kobj
, &eventlog_bin_attr
);
1063 kobject_put(gsmi_kobj
);
1064 gsmi_buf_free(gsmi_dev
.param_buf
);
1065 gsmi_buf_free(gsmi_dev
.data_buf
);
1066 gsmi_buf_free(gsmi_dev
.name_buf
);
1067 kmem_cache_destroy(gsmi_dev
.mem_pool
);
1068 platform_device_unregister(gsmi_dev
.pdev
);
1070 platform_driver_unregister(&gsmi_driver_info
);
1074 module_init(gsmi_init
);
1075 module_exit(gsmi_exit
);
1077 MODULE_AUTHOR("Google, Inc.");
1078 MODULE_LICENSE("GPL");