1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2010 Google Inc. All Rights Reserved.
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/panic_notifier.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 */
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 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 kmem_cache *mem_pool; /* kmem cache for gsmi_buf allocations */
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 * Some older platforms with Apollo Lake chipsets do not support S0ix logging
141 * in their GSMI handlers, and behaved poorly when resuming via power button
142 * press if the logging was attempted. Updated firmware with proper behavior
143 * has long since shipped, removing the need for this opt-in parameter. It
144 * now exists as an opt-out parameter for folks defiantly running old
145 * firmware, or unforeseen circumstances. After the change from opt-in to
146 * opt-out has baked sufficiently, this parameter should probably be removed
149 static bool s0ix_logging_enable = true;
150 module_param(s0ix_logging_enable, bool, 0600);
152 static struct gsmi_buf *gsmi_buf_alloc(void)
154 struct gsmi_buf *smibuf;
156 smibuf = kzalloc(sizeof(*smibuf), GFP_KERNEL);
158 printk(KERN_ERR "gsmi: out of memory\n");
162 /* allocate buffer in 32bit address space */
163 smibuf->start = kmem_cache_alloc(gsmi_dev.mem_pool, GFP_KERNEL);
164 if (!smibuf->start) {
165 printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
170 /* fill in the buffer handle */
171 smibuf->length = GSMI_BUF_SIZE;
172 smibuf->address = (u32)virt_to_phys(smibuf->start);
177 static void gsmi_buf_free(struct gsmi_buf *smibuf)
181 kmem_cache_free(gsmi_dev.mem_pool, smibuf->start);
187 * Make a call to gsmi func(sub). GSMI error codes are translated to
188 * in-kernel errnos (0 on success, -ERRNO on error).
190 static int gsmi_exec(u8 func, u8 sub)
192 u16 cmd = (sub << 8) | func;
197 * AH : Subfunction number
198 * AL : Function number
199 * EBX : Parameter block address
200 * DX : SMI command port
202 * Three protocols here. See also the comment in gsmi_init().
204 if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
206 * If handshake_type == HANDSHAKE_CF then set CF on the
207 * way in and wait for the handler to clear it; this avoids
208 * corrupting register state on those chipsets which have
209 * a delay between writing the SMI trigger register and
218 "d" (gsmi_dev.smi_cmd),
219 "b" (gsmi_dev.param_buf->address)
222 } else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
224 * If handshake_type == HANDSHAKE_SPIN we spin a
225 * hundred-ish usecs to ensure the SMI has triggered.
232 "d" (gsmi_dev.smi_cmd),
233 "b" (gsmi_dev.param_buf->address),
239 * If handshake_type == HANDSHAKE_NONE we do nothing;
240 * either we don't need to or it's legacy firmware that
241 * doesn't understand the CF protocol.
244 "outb %%al, %%dx\n\t"
247 "d" (gsmi_dev.smi_cmd),
248 "b" (gsmi_dev.param_buf->address)
253 /* check return code from SMI handler */
257 case GSMI_VAR_NOT_FOUND:
258 /* not really an error, but let the caller know */
261 case GSMI_INVALID_PARAMETER:
262 printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
265 case GSMI_BUFFER_TOO_SMALL:
266 printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
269 case GSMI_UNSUPPORTED:
270 case GSMI_UNSUPPORTED2:
271 if (sub != GSMI_CMD_HANDSHAKE_TYPE)
272 printk(KERN_ERR "gsmi: exec 0x%04x: Not supported\n",
277 printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
280 case GSMI_DEVICE_ERROR:
281 printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
285 printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
289 printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
292 case GSMI_HANDSHAKE_CF:
293 case GSMI_HANDSHAKE_SPIN:
294 case GSMI_HANDSHAKE_NONE:
298 printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
308 static struct efivars efivars;
310 static efi_status_t gsmi_get_variable(efi_char16_t *name,
311 efi_guid_t *vendor, u32 *attr,
312 unsigned long *data_size,
315 struct gsmi_nvram_var_param param = {
316 .name_ptr = gsmi_dev.name_buf->address,
317 .data_ptr = gsmi_dev.data_buf->address,
318 .data_len = (u32)*data_size,
320 efi_status_t ret = EFI_SUCCESS;
322 size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
325 if (name_len >= GSMI_BUF_SIZE / 2)
326 return EFI_BAD_BUFFER_SIZE;
328 spin_lock_irqsave(&gsmi_dev.lock, flags);
331 memcpy(¶m.guid, vendor, sizeof(param.guid));
333 /* variable name, already in UTF-16 */
334 memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
335 memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
338 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
340 /* parameter buffer */
341 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
342 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
344 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
346 printk(KERN_ERR "gsmi: Get Variable failed\n");
347 ret = EFI_LOAD_ERROR;
348 } else if (rc == 1) {
349 /* variable was not found */
352 /* Get the arguments back */
353 memcpy(¶m, gsmi_dev.param_buf->start, sizeof(param));
355 /* The size reported is the min of all of our buffers */
356 *data_size = min_t(unsigned long, *data_size,
357 gsmi_dev.data_buf->length);
358 *data_size = min_t(unsigned long, *data_size, param.data_len);
360 /* Copy data back to return buffer. */
361 memcpy(data, gsmi_dev.data_buf->start, *data_size);
363 /* All variables are have the following attributes */
364 *attr = EFI_VARIABLE_NON_VOLATILE |
365 EFI_VARIABLE_BOOTSERVICE_ACCESS |
366 EFI_VARIABLE_RUNTIME_ACCESS;
369 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
374 static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
378 struct gsmi_get_next_var_param param = {
379 .name_ptr = gsmi_dev.name_buf->address,
380 .name_len = gsmi_dev.name_buf->length,
382 efi_status_t ret = EFI_SUCCESS;
386 /* For the moment, only support buffers that exactly match in size */
387 if (*name_size != GSMI_BUF_SIZE)
388 return EFI_BAD_BUFFER_SIZE;
390 /* Let's make sure the thing is at least null-terminated */
391 if (ucs2_strnlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
392 return EFI_INVALID_PARAMETER;
394 spin_lock_irqsave(&gsmi_dev.lock, flags);
397 memcpy(¶m.guid, vendor, sizeof(param.guid));
399 /* variable name, already in UTF-16 */
400 memcpy(gsmi_dev.name_buf->start, name, *name_size);
402 /* parameter buffer */
403 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
404 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
406 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
408 printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
409 ret = EFI_LOAD_ERROR;
410 } else if (rc == 1) {
411 /* variable not found -- end of list */
414 /* copy variable data back to return buffer */
415 memcpy(¶m, gsmi_dev.param_buf->start, sizeof(param));
417 /* Copy the name back */
418 memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
419 *name_size = ucs2_strnlen(name, GSMI_BUF_SIZE / 2) * 2;
421 /* copy guid to return buffer */
422 memcpy(vendor, ¶m.guid, sizeof(param.guid));
426 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
431 static efi_status_t gsmi_set_variable(efi_char16_t *name,
434 unsigned long data_size,
437 struct gsmi_nvram_var_param param = {
438 .name_ptr = gsmi_dev.name_buf->address,
439 .data_ptr = gsmi_dev.data_buf->address,
440 .data_len = (u32)data_size,
441 .attributes = EFI_VARIABLE_NON_VOLATILE |
442 EFI_VARIABLE_BOOTSERVICE_ACCESS |
443 EFI_VARIABLE_RUNTIME_ACCESS,
445 size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
446 efi_status_t ret = EFI_SUCCESS;
450 if (name_len >= GSMI_BUF_SIZE / 2)
451 return EFI_BAD_BUFFER_SIZE;
453 spin_lock_irqsave(&gsmi_dev.lock, flags);
456 memcpy(¶m.guid, vendor, sizeof(param.guid));
458 /* variable name, already in UTF-16 */
459 memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
460 memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
463 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
464 memcpy(gsmi_dev.data_buf->start, data, data_size);
466 /* parameter buffer */
467 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
468 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
470 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
472 printk(KERN_ERR "gsmi: Set Variable failed\n");
473 ret = EFI_INVALID_PARAMETER;
476 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
481 static const struct efivar_operations efivar_ops = {
482 .get_variable = gsmi_get_variable,
483 .set_variable = gsmi_set_variable,
484 .get_next_variable = gsmi_get_next_variable,
487 #endif /* CONFIG_EFI */
489 static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
490 struct bin_attribute *bin_attr,
491 char *buf, loff_t pos, size_t count)
493 struct gsmi_set_eventlog_param param = {
494 .data_ptr = gsmi_dev.data_buf->address,
499 /* Pull the type out */
500 if (count < sizeof(u32))
502 param.type = *(u32 *)buf;
505 /* The remaining buffer is the data payload */
506 if ((count - sizeof(u32)) > gsmi_dev.data_buf->length)
508 param.data_len = count - sizeof(u32);
510 spin_lock_irqsave(&gsmi_dev.lock, flags);
513 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
514 memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
516 /* parameter buffer */
517 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
518 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
520 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
522 printk(KERN_ERR "gsmi: Set Event Log failed\n");
524 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
526 return (rc == 0) ? count : rc;
530 static struct bin_attribute eventlog_bin_attr = {
531 .attr = {.name = "append_to_eventlog", .mode = 0200},
532 .write = eventlog_write,
535 static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
536 struct kobj_attribute *attr,
537 const char *buf, size_t count)
547 rc = kstrtoul(buf, 0, &val);
552 * Value entered is a percentage, 0 through 100, anything else
558 /* data_type here selects the smbios event log. */
559 param.percentage = val;
562 spin_lock_irqsave(&gsmi_dev.lock, flags);
564 /* parameter buffer */
565 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
566 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
568 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
570 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
577 static struct kobj_attribute gsmi_clear_eventlog_attr = {
578 .attr = {.name = "clear_eventlog", .mode = 0200},
579 .store = gsmi_clear_eventlog_store,
582 static ssize_t gsmi_clear_config_store(struct kobject *kobj,
583 struct kobj_attribute *attr,
584 const char *buf, size_t count)
589 spin_lock_irqsave(&gsmi_dev.lock, flags);
591 /* clear parameter buffer */
592 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
594 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
596 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
603 static struct kobj_attribute gsmi_clear_config_attr = {
604 .attr = {.name = "clear_config", .mode = 0200},
605 .store = gsmi_clear_config_store,
608 static const struct attribute *gsmi_attrs[] = {
609 &gsmi_clear_config_attr.attr,
610 &gsmi_clear_eventlog_attr.attr,
614 static int gsmi_shutdown_reason(int reason)
616 struct gsmi_log_entry_type_1 entry = {
617 .type = GSMI_LOG_ENTRY_TYPE_KERNEL,
620 struct gsmi_set_eventlog_param param = {
621 .data_len = sizeof(entry),
624 static int saved_reason;
628 /* avoid duplicate entries in the log */
629 if (saved_reason & (1 << reason))
632 spin_lock_irqsave(&gsmi_dev.lock, flags);
634 saved_reason |= (1 << reason);
637 memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
638 memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
640 /* parameter buffer */
641 param.data_ptr = gsmi_dev.data_buf->address;
642 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
643 memcpy(gsmi_dev.param_buf->start, ¶m, sizeof(param));
645 rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
647 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
650 printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
652 printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
658 static int gsmi_reboot_callback(struct notifier_block *nb,
659 unsigned long reason, void *arg)
661 gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
665 static struct notifier_block gsmi_reboot_notifier = {
666 .notifier_call = gsmi_reboot_callback
669 static int gsmi_die_callback(struct notifier_block *nb,
670 unsigned long reason, void *arg)
672 if (reason == DIE_OOPS)
673 gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
677 static struct notifier_block gsmi_die_notifier = {
678 .notifier_call = gsmi_die_callback
681 static int gsmi_panic_callback(struct notifier_block *nb,
682 unsigned long reason, void *arg)
684 gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
688 static struct notifier_block gsmi_panic_notifier = {
689 .notifier_call = gsmi_panic_callback,
693 * This hash function was blatantly copied from include/linux/hash.h.
694 * It is used by this driver to obfuscate a board name that requires a
695 * quirk within this driver.
697 * Please do not remove this copy of the function as any changes to the
698 * global utility hash_64() function would break this driver's ability
701 static u64 __init local_hash_64(u64 val, unsigned bits)
705 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
720 /* High bits are more random, so use them. */
721 return hash >> (64 - bits);
724 static u32 __init hash_oem_table_id(char s[8])
727 memcpy(&input, s, 8);
728 return local_hash_64(input, 32);
731 static const struct dmi_system_id gsmi_dmi_table[] __initconst = {
733 .ident = "Google Board",
735 DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
739 .ident = "Coreboot Firmware",
741 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
746 MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
748 static __init int gsmi_system_valid(void)
753 if (!dmi_check_system(gsmi_dmi_table))
757 * Only newer firmware supports the gsmi interface. All older
758 * firmware that didn't support this interface used to plug the
759 * table name in the first four bytes of the oem_table_id field.
760 * Newer firmware doesn't do that though, so use that as the
761 * discriminant factor. We have to do this in order to
762 * whitewash our board names out of the public driver.
764 if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
765 printk(KERN_INFO "gsmi: Board is too old\n");
769 /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
770 hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
771 if (hash == QUIRKY_BOARD_HASH) {
772 const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
773 if (strncmp(bios_ver, "1.0", 3) == 0) {
774 pr_info("gsmi: disabled on this board's BIOS %s\n",
780 /* check for valid SMI command port in ACPI FADT */
781 if (acpi_gbl_FADT.smi_command == 0) {
782 pr_info("gsmi: missing smi_command\n");
786 /* Test the smihandler with a bogus command. If it leaves the
787 * calling argument in %ax untouched, there is no handler for
790 cmd = GSMI_CALLBACK | GSMI_CMD_RESERVED << 8;
792 "outb %%al, %%dx\n\t"
795 "d" (acpi_gbl_FADT.smi_command)
799 pr_info("gsmi: no gsmi handler in firmware\n");
807 static struct kobject *gsmi_kobj;
809 static const struct platform_device_info gsmi_dev_info = {
812 /* SMI callbacks require 32bit addresses */
813 .dma_mask = DMA_BIT_MASK(32),
817 static void gsmi_log_s0ix_info(u8 cmd)
822 * If platform has not enabled S0ix logging, then no action is
825 if (!s0ix_logging_enable)
828 spin_lock_irqsave(&gsmi_dev.lock, flags);
830 memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
832 gsmi_exec(GSMI_CALLBACK, cmd);
834 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
837 static int gsmi_log_s0ix_suspend(struct device *dev)
840 * If system is not suspending via firmware using the standard ACPI Sx
841 * types, then make a GSMI call to log the suspend info.
843 if (!pm_suspend_via_firmware())
844 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND);
847 * Always return success, since we do not want suspend
848 * to fail just because of logging failure.
853 static int gsmi_log_s0ix_resume(struct device *dev)
856 * If system did not resume via firmware, then make a GSMI call to log
857 * the resume info and wake source.
859 if (!pm_resume_via_firmware())
860 gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME);
863 * Always return success, since we do not want resume
864 * to fail just because of logging failure.
869 static const struct dev_pm_ops gsmi_pm_ops = {
870 .suspend_noirq = gsmi_log_s0ix_suspend,
871 .resume_noirq = gsmi_log_s0ix_resume,
874 static int gsmi_platform_driver_probe(struct platform_device *dev)
879 static struct platform_driver gsmi_driver_info = {
884 .probe = gsmi_platform_driver_probe,
888 static __init int gsmi_init(void)
893 ret = gsmi_system_valid();
897 gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
900 ret = platform_driver_register(&gsmi_driver_info);
902 printk(KERN_ERR "gsmi: unable to register platform driver\n");
907 /* register device */
908 gsmi_dev.pdev = platform_device_register_full(&gsmi_dev_info);
909 if (IS_ERR(gsmi_dev.pdev)) {
910 printk(KERN_ERR "gsmi: unable to register platform device\n");
911 return PTR_ERR(gsmi_dev.pdev);
914 /* SMI access needs to be serialized */
915 spin_lock_init(&gsmi_dev.lock);
920 * SLAB cache is created using SLAB_CACHE_DMA32 to ensure that the
921 * allocations for gsmi_buf come from the DMA32 memory zone. These
922 * buffers have nothing to do with DMA. They are required for
923 * communication with firmware executing in SMI mode which can only
924 * access the bottom 4GiB of physical memory. Since DMA32 memory zone
925 * guarantees allocation under the 4GiB boundary, this driver creates
926 * a SLAB cache with SLAB_CACHE_DMA32 flag.
928 gsmi_dev.mem_pool = kmem_cache_create("gsmi", GSMI_BUF_SIZE,
930 SLAB_CACHE_DMA32, NULL);
931 if (!gsmi_dev.mem_pool)
935 * pre-allocate buffers because sometimes we are called when
936 * this is not feasible: oops, panic, die, mce, etc
938 gsmi_dev.name_buf = gsmi_buf_alloc();
939 if (!gsmi_dev.name_buf) {
940 printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
944 gsmi_dev.data_buf = gsmi_buf_alloc();
945 if (!gsmi_dev.data_buf) {
946 printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
950 gsmi_dev.param_buf = gsmi_buf_alloc();
951 if (!gsmi_dev.param_buf) {
952 printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
957 * Determine type of handshake used to serialize the SMI
958 * entry. See also gsmi_exec().
960 * There's a "behavior" present on some chipsets where writing the
961 * SMI trigger register in the southbridge doesn't result in an
962 * immediate SMI. Rather, the processor can execute "a few" more
963 * instructions before the SMI takes effect. To ensure synchronous
964 * behavior, implement a handshake between the kernel driver and the
965 * firmware handler to spin until released. This ioctl determines
966 * the type of handshake.
968 * NONE: The firmware handler does not implement any
969 * handshake. Either it doesn't need to, or it's legacy firmware
970 * that doesn't know it needs to and never will.
972 * CF: The firmware handler will clear the CF in the saved
973 * state before returning. The driver may set the CF and test for
974 * it to clear before proceeding.
976 * SPIN: The firmware handler does not implement any handshake
977 * but the driver should spin for a hundred or so microseconds
978 * to ensure the SMI has triggered.
980 * Finally, the handler will return -ENOSYS if
981 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
984 spin_lock_irqsave(&gsmi_dev.lock, flags);
985 gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
986 gsmi_dev.handshake_type =
987 gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
988 if (gsmi_dev.handshake_type == -ENOSYS)
989 gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
990 spin_unlock_irqrestore(&gsmi_dev.lock, flags);
992 /* Remove and clean up gsmi if the handshake could not complete. */
993 if (gsmi_dev.handshake_type == -ENXIO) {
994 printk(KERN_INFO "gsmi version " DRIVER_VERSION
995 " failed to load\n");
1000 /* Register in the firmware directory */
1002 gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
1004 printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
1008 /* Setup eventlog access */
1009 ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
1011 printk(KERN_INFO "gsmi: Failed to setup eventlog");
1015 /* Other attributes */
1016 ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
1018 printk(KERN_INFO "gsmi: Failed to add attrs");
1019 goto out_remove_bin_file;
1023 ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
1025 printk(KERN_INFO "gsmi: Failed to register efivars\n");
1026 sysfs_remove_files(gsmi_kobj, gsmi_attrs);
1027 goto out_remove_bin_file;
1031 register_reboot_notifier(&gsmi_reboot_notifier);
1032 register_die_notifier(&gsmi_die_notifier);
1033 atomic_notifier_chain_register(&panic_notifier_list,
1034 &gsmi_panic_notifier);
1036 printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
1040 out_remove_bin_file:
1041 sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1043 kobject_put(gsmi_kobj);
1044 gsmi_buf_free(gsmi_dev.param_buf);
1045 gsmi_buf_free(gsmi_dev.data_buf);
1046 gsmi_buf_free(gsmi_dev.name_buf);
1047 kmem_cache_destroy(gsmi_dev.mem_pool);
1048 platform_device_unregister(gsmi_dev.pdev);
1049 pr_info("gsmi: failed to load: %d\n", ret);
1051 platform_driver_unregister(&gsmi_driver_info);
1056 static void __exit gsmi_exit(void)
1058 unregister_reboot_notifier(&gsmi_reboot_notifier);
1059 unregister_die_notifier(&gsmi_die_notifier);
1060 atomic_notifier_chain_unregister(&panic_notifier_list,
1061 &gsmi_panic_notifier);
1063 efivars_unregister(&efivars);
1066 sysfs_remove_files(gsmi_kobj, gsmi_attrs);
1067 sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1068 kobject_put(gsmi_kobj);
1069 gsmi_buf_free(gsmi_dev.param_buf);
1070 gsmi_buf_free(gsmi_dev.data_buf);
1071 gsmi_buf_free(gsmi_dev.name_buf);
1072 kmem_cache_destroy(gsmi_dev.mem_pool);
1073 platform_device_unregister(gsmi_dev.pdev);
1075 platform_driver_unregister(&gsmi_driver_info);
1079 module_init(gsmi_init);
1080 module_exit(gsmi_exit);
1082 MODULE_AUTHOR("Google, Inc.");
1083 MODULE_LICENSE("GPL");