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
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 dma_addr_t handle; /* dma allocation handle */
88 u32 address; /* physical address of buffer */
91 static struct gsmi_device {
92 struct platform_device *pdev; /* platform device */
93 struct gsmi_buf *name_buf; /* variable name buffer */
94 struct gsmi_buf *data_buf; /* generic data buffer */
95 struct gsmi_buf *param_buf; /* parameter buffer */
96 spinlock_t lock; /* serialize access to SMIs */
97 u16 smi_cmd; /* SMI command port */
98 int handshake_type; /* firmware handler interlock type */
99 struct dma_pool *dma_pool; /* DMA buffer pool */
102 /* Packed structures for communicating with the firmware */
103 struct gsmi_nvram_var_param {
111 struct gsmi_get_next_var_param {
112 u8 guid[GSMI_GUID_SIZE];
117 struct gsmi_set_eventlog_param {
123 /* Event log formats */
124 struct gsmi_log_entry_type_1 {
130 * Some platforms don't have explicit SMI handshake
131 * and need to wait for SMI to complete.
133 #define GSMI_DEFAULT_SPINCOUNT 0x10000
134 static unsigned int spincount = GSMI_DEFAULT_SPINCOUNT;
135 module_param(spincount, uint, 0600);
136 MODULE_PARM_DESC(spincount,
137 "The number of loop iterations to use when using the spin handshake.");
140 * Platforms might not support S0ix logging in their GSMI handlers. In order to
141 * avoid any side-effects of generating an SMI for S0ix logging, use the S0ix
142 * related GSMI commands only for those platforms that explicitly enable this
145 static bool s0ix_logging_enable;
146 module_param(s0ix_logging_enable, bool, 0600);
148 static struct gsmi_buf *gsmi_buf_alloc(void)
150 struct gsmi_buf *smibuf;
152 smibuf = kzalloc(sizeof(*smibuf), GFP_KERNEL);
154 printk(KERN_ERR "gsmi: out of memory\n");
158 /* allocate buffer in 32bit address space */
159 smibuf->start = dma_pool_alloc(gsmi_dev.dma_pool, GFP_KERNEL,
161 if (!smibuf->start) {
162 printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
167 /* fill in the buffer handle */
168 smibuf->length = GSMI_BUF_SIZE;
169 smibuf->address = (u32)virt_to_phys(smibuf->start);
174 static void gsmi_buf_free(struct gsmi_buf *smibuf)
178 dma_pool_free(gsmi_dev.dma_pool, smibuf->start,
185 * Make a call to gsmi func(sub). GSMI error codes are translated to
186 * in-kernel errnos (0 on success, -ERRNO on error).
188 static int gsmi_exec(u8 func, u8 sub)
190 u16 cmd = (sub << 8) | func;
195 * AH : Subfunction number
196 * AL : Function number
197 * EBX : Parameter block address
198 * DX : SMI command port
200 * Three protocols here. See also the comment in gsmi_init().
202 if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
204 * If handshake_type == HANDSHAKE_CF then set CF on the
205 * way in and wait for the handler to clear it; this avoids
206 * corrupting register state on those chipsets which have
207 * a delay between writing the SMI trigger register and
216 "d" (gsmi_dev.smi_cmd),
217 "b" (gsmi_dev.param_buf->address)
220 } else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
222 * If handshake_type == HANDSHAKE_SPIN we spin a
223 * hundred-ish usecs to ensure the SMI has triggered.
230 "d" (gsmi_dev.smi_cmd),
231 "b" (gsmi_dev.param_buf->address),
237 * If handshake_type == HANDSHAKE_NONE we do nothing;
238 * either we don't need to or it's legacy firmware that
239 * doesn't understand the CF protocol.
242 "outb %%al, %%dx\n\t"
245 "d" (gsmi_dev.smi_cmd),
246 "b" (gsmi_dev.param_buf->address)
251 /* check return code from SMI handler */
255 case GSMI_VAR_NOT_FOUND:
256 /* not really an error, but let the caller know */
259 case GSMI_INVALID_PARAMETER:
260 printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
263 case GSMI_BUFFER_TOO_SMALL:
264 printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
267 case GSMI_UNSUPPORTED:
268 case GSMI_UNSUPPORTED2:
269 if (sub != GSMI_CMD_HANDSHAKE_TYPE)
270 printk(KERN_ERR "gsmi: exec 0x%04x: Not supported\n",
275 printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
278 case GSMI_DEVICE_ERROR:
279 printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
283 printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
287 printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
290 case GSMI_HANDSHAKE_CF:
291 case GSMI_HANDSHAKE_SPIN:
292 case GSMI_HANDSHAKE_NONE:
296 printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
304 #ifdef CONFIG_EFI_VARS
306 static struct efivars efivars;
308 static efi_status_t gsmi_get_variable(efi_char16_t *name,
309 efi_guid_t *vendor, u32 *attr,
310 unsigned long *data_size,
313 struct gsmi_nvram_var_param param = {
314 .name_ptr = gsmi_dev.name_buf->address,
315 .data_ptr = gsmi_dev.data_buf->address,
316 .data_len = (u32)*data_size,
318 efi_status_t ret = EFI_SUCCESS;
320 size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
323 if (name_len >= GSMI_BUF_SIZE / 2)
324 return EFI_BAD_BUFFER_SIZE;
326 spin_lock_irqsave(&gsmi_dev.lock, flags);
329 memcpy(¶m.guid, vendor, sizeof(param.guid));
331 /* variable name, already in UTF-16 */
332 memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
333 memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
336 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
338 /* parameter buffer */
339 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
340 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
342 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
344 printk(KERN_ERR "gsmi: Get Variable failed\n");
345 ret = EFI_LOAD_ERROR;
346 } else if (rc == 1) {
347 /* variable was not found */
350 /* Get the arguments back */
351 memcpy(¶m, gsmi_dev.param_buf->start, sizeof(param));
353 /* The size reported is the min of all of our buffers */
354 *data_size = min_t(unsigned long, *data_size,
355 gsmi_dev.data_buf->length);
356 *data_size = min_t(unsigned long, *data_size, param.data_len);
358 /* Copy data back to return buffer. */
359 memcpy(data, gsmi_dev.data_buf->start, *data_size);
361 /* All variables are have the following attributes */
362 *attr = EFI_VARIABLE_NON_VOLATILE |
363 EFI_VARIABLE_BOOTSERVICE_ACCESS |
364 EFI_VARIABLE_RUNTIME_ACCESS;
367 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
372 static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
376 struct gsmi_get_next_var_param param = {
377 .name_ptr = gsmi_dev.name_buf->address,
378 .name_len = gsmi_dev.name_buf->length,
380 efi_status_t ret = EFI_SUCCESS;
384 /* For the moment, only support buffers that exactly match in size */
385 if (*name_size != GSMI_BUF_SIZE)
386 return EFI_BAD_BUFFER_SIZE;
388 /* Let's make sure the thing is at least null-terminated */
389 if (ucs2_strnlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
390 return EFI_INVALID_PARAMETER;
392 spin_lock_irqsave(&gsmi_dev.lock, flags);
395 memcpy(¶m.guid, vendor, sizeof(param.guid));
397 /* variable name, already in UTF-16 */
398 memcpy(gsmi_dev.name_buf->start, name, *name_size);
400 /* parameter buffer */
401 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
402 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
404 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
406 printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
407 ret = EFI_LOAD_ERROR;
408 } else if (rc == 1) {
409 /* variable not found -- end of list */
412 /* copy variable data back to return buffer */
413 memcpy(¶m, gsmi_dev.param_buf->start, sizeof(param));
415 /* Copy the name back */
416 memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
417 *name_size = ucs2_strnlen(name, GSMI_BUF_SIZE / 2) * 2;
419 /* copy guid to return buffer */
420 memcpy(vendor, ¶m.guid, sizeof(param.guid));
424 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
429 static efi_status_t gsmi_set_variable(efi_char16_t *name,
432 unsigned long data_size,
435 struct gsmi_nvram_var_param param = {
436 .name_ptr = gsmi_dev.name_buf->address,
437 .data_ptr = gsmi_dev.data_buf->address,
438 .data_len = (u32)data_size,
439 .attributes = EFI_VARIABLE_NON_VOLATILE |
440 EFI_VARIABLE_BOOTSERVICE_ACCESS |
441 EFI_VARIABLE_RUNTIME_ACCESS,
443 size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
444 efi_status_t ret = EFI_SUCCESS;
448 if (name_len >= GSMI_BUF_SIZE / 2)
449 return EFI_BAD_BUFFER_SIZE;
451 spin_lock_irqsave(&gsmi_dev.lock, flags);
454 memcpy(¶m.guid, vendor, sizeof(param.guid));
456 /* variable name, already in UTF-16 */
457 memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
458 memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
461 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
462 memcpy(gsmi_dev.data_buf->start, data, data_size);
464 /* parameter buffer */
465 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
466 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
468 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
470 printk(KERN_ERR "gsmi: Set Variable failed\n");
471 ret = EFI_INVALID_PARAMETER;
474 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
479 static const struct efivar_operations efivar_ops = {
480 .get_variable = gsmi_get_variable,
481 .set_variable = gsmi_set_variable,
482 .get_next_variable = gsmi_get_next_variable,
485 #endif /* CONFIG_EFI_VARS */
487 static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
488 struct bin_attribute *bin_attr,
489 char *buf, loff_t pos, size_t count)
491 struct gsmi_set_eventlog_param param = {
492 .data_ptr = gsmi_dev.data_buf->address,
497 /* Pull the type out */
498 if (count < sizeof(u32))
500 param.type = *(u32 *)buf;
503 /* The remaining buffer is the data payload */
504 if ((count - sizeof(u32)) > gsmi_dev.data_buf->length)
506 param.data_len = count - sizeof(u32);
508 spin_lock_irqsave(&gsmi_dev.lock, flags);
511 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
512 memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
514 /* parameter buffer */
515 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
516 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
518 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
520 printk(KERN_ERR "gsmi: Set Event Log failed\n");
522 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
524 return (rc == 0) ? count : rc;
528 static struct bin_attribute eventlog_bin_attr = {
529 .attr = {.name = "append_to_eventlog", .mode = 0200},
530 .write = eventlog_write,
533 static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
534 struct kobj_attribute *attr,
535 const char *buf, size_t count)
545 rc = kstrtoul(buf, 0, &val);
550 * Value entered is a percentage, 0 through 100, anything else
556 /* data_type here selects the smbios event log. */
557 param.percentage = val;
560 spin_lock_irqsave(&gsmi_dev.lock, flags);
562 /* parameter buffer */
563 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
564 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
566 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
568 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
575 static struct kobj_attribute gsmi_clear_eventlog_attr = {
576 .attr = {.name = "clear_eventlog", .mode = 0200},
577 .store = gsmi_clear_eventlog_store,
580 static ssize_t gsmi_clear_config_store(struct kobject *kobj,
581 struct kobj_attribute *attr,
582 const char *buf, size_t count)
587 spin_lock_irqsave(&gsmi_dev.lock, flags);
589 /* clear parameter buffer */
590 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
592 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
594 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
601 static struct kobj_attribute gsmi_clear_config_attr = {
602 .attr = {.name = "clear_config", .mode = 0200},
603 .store = gsmi_clear_config_store,
606 static const struct attribute *gsmi_attrs[] = {
607 &gsmi_clear_config_attr.attr,
608 &gsmi_clear_eventlog_attr.attr,
612 static int gsmi_shutdown_reason(int reason)
614 struct gsmi_log_entry_type_1 entry = {
615 .type = GSMI_LOG_ENTRY_TYPE_KERNEL,
618 struct gsmi_set_eventlog_param param = {
619 .data_len = sizeof(entry),
622 static int saved_reason;
626 /* avoid duplicate entries in the log */
627 if (saved_reason & (1 << reason))
630 spin_lock_irqsave(&gsmi_dev.lock, flags);
632 saved_reason |= (1 << reason);
635 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
636 memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
638 /* parameter buffer */
639 param.data_ptr = gsmi_dev.data_buf->address;
640 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
641 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
643 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
645 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
648 printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
650 printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
656 static int gsmi_reboot_callback(struct notifier_block *nb,
657 unsigned long reason, void *arg)
659 gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
663 static struct notifier_block gsmi_reboot_notifier = {
664 .notifier_call = gsmi_reboot_callback
667 static int gsmi_die_callback(struct notifier_block *nb,
668 unsigned long reason, void *arg)
670 if (reason == DIE_OOPS)
671 gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
675 static struct notifier_block gsmi_die_notifier = {
676 .notifier_call = gsmi_die_callback
679 static int gsmi_panic_callback(struct notifier_block *nb,
680 unsigned long reason, void *arg)
682 gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
686 static struct notifier_block gsmi_panic_notifier = {
687 .notifier_call = gsmi_panic_callback,
691 * This hash function was blatantly copied from include/linux/hash.h.
692 * It is used by this driver to obfuscate a board name that requires a
693 * quirk within this driver.
695 * Please do not remove this copy of the function as any changes to the
696 * global utility hash_64() function would break this driver's ability
697 * to identify a board and provide the appropriate quirk -- mikew@google.com
699 static u64 __init local_hash_64(u64 val, unsigned bits)
703 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
718 /* High bits are more random, so use them. */
719 return hash >> (64 - bits);
722 static u32 __init hash_oem_table_id(char s[8])
725 memcpy(&input, s, 8);
726 return local_hash_64(input, 32);
729 static const struct dmi_system_id gsmi_dmi_table[] __initconst = {
731 .ident = "Google Board",
733 DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
737 .ident = "Coreboot Firmware",
739 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
744 MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
746 static __init int gsmi_system_valid(void)
750 if (!dmi_check_system(gsmi_dmi_table))
754 * Only newer firmware supports the gsmi interface. All older
755 * firmware that didn't support this interface used to plug the
756 * table name in the first four bytes of the oem_table_id field.
757 * Newer firmware doesn't do that though, so use that as the
758 * discriminant factor. We have to do this in order to
759 * whitewash our board names out of the public driver.
761 if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
762 printk(KERN_INFO "gsmi: Board is too old\n");
766 /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
767 hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
768 if (hash == QUIRKY_BOARD_HASH) {
769 const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
770 if (strncmp(bios_ver, "1.0", 3) == 0) {
771 pr_info("gsmi: disabled on this board's BIOS %s\n",
777 /* check for valid SMI command port in ACPI FADT */
778 if (acpi_gbl_FADT.smi_command == 0) {
779 pr_info("gsmi: missing smi_command\n");
787 static struct kobject *gsmi_kobj;
789 static const struct platform_device_info gsmi_dev_info = {
792 /* SMI callbacks require 32bit addresses */
793 .dma_mask = DMA_BIT_MASK(32),
797 static void gsmi_log_s0ix_info(u8 cmd)
802 * If platform has not enabled S0ix logging, then no action is
805 if (!s0ix_logging_enable)
808 spin_lock_irqsave(&gsmi_dev.lock, flags);
810 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
812 gsmi_exec(GSMI_CALLBACK, cmd);
814 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
817 static int gsmi_log_s0ix_suspend(struct device *dev)
820 * If system is not suspending via firmware using the standard ACPI Sx
821 * types, then make a GSMI call to log the suspend info.
823 if (!pm_suspend_via_firmware())
824 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND);
827 * Always return success, since we do not want suspend
828 * to fail just because of logging failure.
833 static int gsmi_log_s0ix_resume(struct device *dev)
836 * If system did not resume via firmware, then make a GSMI call to log
837 * the resume info and wake source.
839 if (!pm_resume_via_firmware())
840 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME);
843 * Always return success, since we do not want resume
844 * to fail just because of logging failure.
849 static const struct dev_pm_ops gsmi_pm_ops = {
850 .suspend_noirq = gsmi_log_s0ix_suspend,
851 .resume_noirq = gsmi_log_s0ix_resume,
854 static int gsmi_platform_driver_probe(struct platform_device *dev)
859 static struct platform_driver gsmi_driver_info = {
864 .probe = gsmi_platform_driver_probe,
868 static __init int gsmi_init(void)
873 ret = gsmi_system_valid();
877 gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
880 ret = platform_driver_register(&gsmi_driver_info);
882 printk(KERN_ERR "gsmi: unable to register platform driver\n");
887 /* register device */
888 gsmi_dev.pdev = platform_device_register_full(&gsmi_dev_info);
889 if (IS_ERR(gsmi_dev.pdev)) {
890 printk(KERN_ERR "gsmi: unable to register platform device\n");
891 return PTR_ERR(gsmi_dev.pdev);
894 /* SMI access needs to be serialized */
895 spin_lock_init(&gsmi_dev.lock);
898 gsmi_dev.dma_pool = dma_pool_create("gsmi", &gsmi_dev.pdev->dev,
899 GSMI_BUF_SIZE, GSMI_BUF_ALIGN, 0);
900 if (!gsmi_dev.dma_pool)
904 * pre-allocate buffers because sometimes we are called when
905 * this is not feasible: oops, panic, die, mce, etc
907 gsmi_dev.name_buf = gsmi_buf_alloc();
908 if (!gsmi_dev.name_buf) {
909 printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
913 gsmi_dev.data_buf = gsmi_buf_alloc();
914 if (!gsmi_dev.data_buf) {
915 printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
919 gsmi_dev.param_buf = gsmi_buf_alloc();
920 if (!gsmi_dev.param_buf) {
921 printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
926 * Determine type of handshake used to serialize the SMI
927 * entry. See also gsmi_exec().
929 * There's a "behavior" present on some chipsets where writing the
930 * SMI trigger register in the southbridge doesn't result in an
931 * immediate SMI. Rather, the processor can execute "a few" more
932 * instructions before the SMI takes effect. To ensure synchronous
933 * behavior, implement a handshake between the kernel driver and the
934 * firmware handler to spin until released. This ioctl determines
935 * the type of handshake.
937 * NONE: The firmware handler does not implement any
938 * handshake. Either it doesn't need to, or it's legacy firmware
939 * that doesn't know it needs to and never will.
941 * CF: The firmware handler will clear the CF in the saved
942 * state before returning. The driver may set the CF and test for
943 * it to clear before proceeding.
945 * SPIN: The firmware handler does not implement any handshake
946 * but the driver should spin for a hundred or so microseconds
947 * to ensure the SMI has triggered.
949 * Finally, the handler will return -ENOSYS if
950 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
953 spin_lock_irqsave(&gsmi_dev.lock, flags);
954 gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
955 gsmi_dev.handshake_type =
956 gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
957 if (gsmi_dev.handshake_type == -ENOSYS)
958 gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
959 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
961 /* Remove and clean up gsmi if the handshake could not complete. */
962 if (gsmi_dev.handshake_type == -ENXIO) {
963 printk(KERN_INFO "gsmi version " DRIVER_VERSION
964 " failed to load\n");
969 /* Register in the firmware directory */
971 gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
973 printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
977 /* Setup eventlog access */
978 ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
980 printk(KERN_INFO "gsmi: Failed to setup eventlog");
984 /* Other attributes */
985 ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
987 printk(KERN_INFO "gsmi: Failed to add attrs");
988 goto out_remove_bin_file;
991 #ifdef CONFIG_EFI_VARS
992 ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
994 printk(KERN_INFO "gsmi: Failed to register efivars\n");
995 sysfs_remove_files(gsmi_kobj, gsmi_attrs);
996 goto out_remove_bin_file;
1000 register_reboot_notifier(&gsmi_reboot_notifier);
1001 register_die_notifier(&gsmi_die_notifier);
1002 atomic_notifier_chain_register(&panic_notifier_list,
1003 &gsmi_panic_notifier);
1005 printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
1009 out_remove_bin_file:
1010 sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1012 kobject_put(gsmi_kobj);
1013 gsmi_buf_free(gsmi_dev.param_buf);
1014 gsmi_buf_free(gsmi_dev.data_buf);
1015 gsmi_buf_free(gsmi_dev.name_buf);
1016 dma_pool_destroy(gsmi_dev.dma_pool);
1017 platform_device_unregister(gsmi_dev.pdev);
1018 pr_info("gsmi: failed to load: %d\n", ret);
1022 static void __exit gsmi_exit(void)
1024 unregister_reboot_notifier(&gsmi_reboot_notifier);
1025 unregister_die_notifier(&gsmi_die_notifier);
1026 atomic_notifier_chain_unregister(&panic_notifier_list,
1027 &gsmi_panic_notifier);
1028 #ifdef CONFIG_EFI_VARS
1029 efivars_unregister(&efivars);
1032 sysfs_remove_files(gsmi_kobj, gsmi_attrs);
1033 sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1034 kobject_put(gsmi_kobj);
1035 gsmi_buf_free(gsmi_dev.param_buf);
1036 gsmi_buf_free(gsmi_dev.data_buf);
1037 gsmi_buf_free(gsmi_dev.name_buf);
1038 dma_pool_destroy(gsmi_dev.dma_pool);
1039 platform_device_unregister(gsmi_dev.pdev);
1042 module_init(gsmi_init);
1043 module_exit(gsmi_exit);
1045 MODULE_AUTHOR("Google, Inc.");
1046 MODULE_LICENSE("GPL");