Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2010 Google Inc. All Rights Reserved.
0004  * Author: dlaurie@google.com (Duncan Laurie)
0005  *
0006  * Re-worked to expose sysfs APIs by mikew@google.com (Mike Waychison)
0007  *
0008  * EFI SMI interface for Google platforms
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <linux/types.h>
0014 #include <linux/device.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/errno.h>
0017 #include <linux/string.h>
0018 #include <linux/spinlock.h>
0019 #include <linux/dma-mapping.h>
0020 #include <linux/fs.h>
0021 #include <linux/slab.h>
0022 #include <linux/panic_notifier.h>
0023 #include <linux/ioctl.h>
0024 #include <linux/acpi.h>
0025 #include <linux/io.h>
0026 #include <linux/uaccess.h>
0027 #include <linux/dmi.h>
0028 #include <linux/kdebug.h>
0029 #include <linux/reboot.h>
0030 #include <linux/efi.h>
0031 #include <linux/module.h>
0032 #include <linux/ucs2_string.h>
0033 #include <linux/suspend.h>
0034 
0035 #define GSMI_SHUTDOWN_CLEAN 0   /* Clean Shutdown */
0036 /* TODO(mikew@google.com): Tie in HARDLOCKUP_DETECTOR with NMIWDT */
0037 #define GSMI_SHUTDOWN_NMIWDT    1   /* NMI Watchdog */
0038 #define GSMI_SHUTDOWN_PANIC 2   /* Panic */
0039 #define GSMI_SHUTDOWN_OOPS  3   /* Oops */
0040 #define GSMI_SHUTDOWN_DIE   4   /* Die -- No longer meaningful */
0041 #define GSMI_SHUTDOWN_MCE   5   /* Machine Check */
0042 #define GSMI_SHUTDOWN_SOFTWDT   6   /* Software Watchdog */
0043 #define GSMI_SHUTDOWN_MBE   7   /* Uncorrected ECC */
0044 #define GSMI_SHUTDOWN_TRIPLE    8   /* Triple Fault */
0045 
0046 #define DRIVER_VERSION      "1.0"
0047 #define GSMI_GUID_SIZE      16
0048 #define GSMI_BUF_SIZE       1024
0049 #define GSMI_BUF_ALIGN      sizeof(u64)
0050 #define GSMI_CALLBACK       0xef
0051 
0052 /* SMI return codes */
0053 #define GSMI_SUCCESS        0x00
0054 #define GSMI_UNSUPPORTED2   0x03
0055 #define GSMI_LOG_FULL       0x0b
0056 #define GSMI_VAR_NOT_FOUND  0x0e
0057 #define GSMI_HANDSHAKE_SPIN 0x7d
0058 #define GSMI_HANDSHAKE_CF   0x7e
0059 #define GSMI_HANDSHAKE_NONE 0x7f
0060 #define GSMI_INVALID_PARAMETER  0x82
0061 #define GSMI_UNSUPPORTED    0x83
0062 #define GSMI_BUFFER_TOO_SMALL   0x85
0063 #define GSMI_NOT_READY      0x86
0064 #define GSMI_DEVICE_ERROR   0x87
0065 #define GSMI_NOT_FOUND      0x8e
0066 
0067 #define QUIRKY_BOARD_HASH 0x78a30a50
0068 
0069 /* Internally used commands passed to the firmware */
0070 #define GSMI_CMD_GET_NVRAM_VAR      0x01
0071 #define GSMI_CMD_GET_NEXT_VAR       0x02
0072 #define GSMI_CMD_SET_NVRAM_VAR      0x03
0073 #define GSMI_CMD_SET_EVENT_LOG      0x08
0074 #define GSMI_CMD_CLEAR_EVENT_LOG    0x09
0075 #define GSMI_CMD_LOG_S0IX_SUSPEND   0x0a
0076 #define GSMI_CMD_LOG_S0IX_RESUME    0x0b
0077 #define GSMI_CMD_CLEAR_CONFIG       0x20
0078 #define GSMI_CMD_HANDSHAKE_TYPE     0xC1
0079 #define GSMI_CMD_RESERVED       0xff
0080 
0081 /* Magic entry type for kernel events */
0082 #define GSMI_LOG_ENTRY_TYPE_KERNEL     0xDEAD
0083 
0084 /* SMI buffers must be in 32bit physical address space */
0085 struct gsmi_buf {
0086     u8 *start;          /* start of buffer */
0087     size_t length;          /* length of buffer */
0088     u32 address;            /* physical address of buffer */
0089 };
0090 
0091 static struct gsmi_device {
0092     struct platform_device *pdev;   /* platform device */
0093     struct gsmi_buf *name_buf;  /* variable name buffer */
0094     struct gsmi_buf *data_buf;  /* generic data buffer */
0095     struct gsmi_buf *param_buf; /* parameter buffer */
0096     spinlock_t lock;        /* serialize access to SMIs */
0097     u16 smi_cmd;            /* SMI command port */
0098     int handshake_type;     /* firmware handler interlock type */
0099     struct kmem_cache *mem_pool;    /* kmem cache for gsmi_buf allocations */
0100 } gsmi_dev;
0101 
0102 /* Packed structures for communicating with the firmware */
0103 struct gsmi_nvram_var_param {
0104     efi_guid_t  guid;
0105     u32     name_ptr;
0106     u32     attributes;
0107     u32     data_len;
0108     u32     data_ptr;
0109 } __packed;
0110 
0111 struct gsmi_get_next_var_param {
0112     u8  guid[GSMI_GUID_SIZE];
0113     u32 name_ptr;
0114     u32 name_len;
0115 } __packed;
0116 
0117 struct gsmi_set_eventlog_param {
0118     u32 data_ptr;
0119     u32 data_len;
0120     u32 type;
0121 } __packed;
0122 
0123 /* Event log formats */
0124 struct gsmi_log_entry_type_1 {
0125     u16 type;
0126     u32 instance;
0127 } __packed;
0128 
0129 /*
0130  * Some platforms don't have explicit SMI handshake
0131  * and need to wait for SMI to complete.
0132  */
0133 #define GSMI_DEFAULT_SPINCOUNT  0x10000
0134 static unsigned int spincount = GSMI_DEFAULT_SPINCOUNT;
0135 module_param(spincount, uint, 0600);
0136 MODULE_PARM_DESC(spincount,
0137     "The number of loop iterations to use when using the spin handshake.");
0138 
0139 /*
0140  * Some older platforms with Apollo Lake chipsets do not support S0ix logging
0141  * in their GSMI handlers, and behaved poorly when resuming via power button
0142  * press if the logging was attempted. Updated firmware with proper behavior
0143  * has long since shipped, removing the need for this opt-in parameter. It
0144  * now exists as an opt-out parameter for folks defiantly running old
0145  * firmware, or unforeseen circumstances. After the change from opt-in to
0146  * opt-out has baked sufficiently, this parameter should probably be removed
0147  * entirely.
0148  */
0149 static bool s0ix_logging_enable = true;
0150 module_param(s0ix_logging_enable, bool, 0600);
0151 
0152 static struct gsmi_buf *gsmi_buf_alloc(void)
0153 {
0154     struct gsmi_buf *smibuf;
0155 
0156     smibuf = kzalloc(sizeof(*smibuf), GFP_KERNEL);
0157     if (!smibuf) {
0158         printk(KERN_ERR "gsmi: out of memory\n");
0159         return NULL;
0160     }
0161 
0162     /* allocate buffer in 32bit address space */
0163     smibuf->start = kmem_cache_alloc(gsmi_dev.mem_pool, GFP_KERNEL);
0164     if (!smibuf->start) {
0165         printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
0166         kfree(smibuf);
0167         return NULL;
0168     }
0169 
0170     /* fill in the buffer handle */
0171     smibuf->length = GSMI_BUF_SIZE;
0172     smibuf->address = (u32)virt_to_phys(smibuf->start);
0173 
0174     return smibuf;
0175 }
0176 
0177 static void gsmi_buf_free(struct gsmi_buf *smibuf)
0178 {
0179     if (smibuf) {
0180         if (smibuf->start)
0181             kmem_cache_free(gsmi_dev.mem_pool, smibuf->start);
0182         kfree(smibuf);
0183     }
0184 }
0185 
0186 /*
0187  * Make a call to gsmi func(sub).  GSMI error codes are translated to
0188  * in-kernel errnos (0 on success, -ERRNO on error).
0189  */
0190 static int gsmi_exec(u8 func, u8 sub)
0191 {
0192     u16 cmd = (sub << 8) | func;
0193     u16 result = 0;
0194     int rc = 0;
0195 
0196     /*
0197      * AH  : Subfunction number
0198      * AL  : Function number
0199      * EBX : Parameter block address
0200      * DX  : SMI command port
0201      *
0202      * Three protocols here. See also the comment in gsmi_init().
0203      */
0204     if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
0205         /*
0206          * If handshake_type == HANDSHAKE_CF then set CF on the
0207          * way in and wait for the handler to clear it; this avoids
0208          * corrupting register state on those chipsets which have
0209          * a delay between writing the SMI trigger register and
0210          * entering SMM.
0211          */
0212         asm volatile (
0213             "stc\n"
0214             "outb %%al, %%dx\n"
0215         "1:      jc 1b\n"
0216             : "=a" (result)
0217             : "0" (cmd),
0218               "d" (gsmi_dev.smi_cmd),
0219               "b" (gsmi_dev.param_buf->address)
0220             : "memory", "cc"
0221         );
0222     } else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
0223         /*
0224          * If handshake_type == HANDSHAKE_SPIN we spin a
0225          * hundred-ish usecs to ensure the SMI has triggered.
0226          */
0227         asm volatile (
0228             "outb %%al, %%dx\n"
0229         "1:      loop 1b\n"
0230             : "=a" (result)
0231             : "0" (cmd),
0232               "d" (gsmi_dev.smi_cmd),
0233               "b" (gsmi_dev.param_buf->address),
0234               "c" (spincount)
0235             : "memory", "cc"
0236         );
0237     } else {
0238         /*
0239          * If handshake_type == HANDSHAKE_NONE we do nothing;
0240          * either we don't need to or it's legacy firmware that
0241          * doesn't understand the CF protocol.
0242          */
0243         asm volatile (
0244             "outb %%al, %%dx\n\t"
0245             : "=a" (result)
0246             : "0" (cmd),
0247               "d" (gsmi_dev.smi_cmd),
0248               "b" (gsmi_dev.param_buf->address)
0249             : "memory", "cc"
0250         );
0251     }
0252 
0253     /* check return code from SMI handler */
0254     switch (result) {
0255     case GSMI_SUCCESS:
0256         break;
0257     case GSMI_VAR_NOT_FOUND:
0258         /* not really an error, but let the caller know */
0259         rc = 1;
0260         break;
0261     case GSMI_INVALID_PARAMETER:
0262         printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
0263         rc = -EINVAL;
0264         break;
0265     case GSMI_BUFFER_TOO_SMALL:
0266         printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
0267         rc = -ENOMEM;
0268         break;
0269     case GSMI_UNSUPPORTED:
0270     case GSMI_UNSUPPORTED2:
0271         if (sub != GSMI_CMD_HANDSHAKE_TYPE)
0272             printk(KERN_ERR "gsmi: exec 0x%04x: Not supported\n",
0273                    cmd);
0274         rc = -ENOSYS;
0275         break;
0276     case GSMI_NOT_READY:
0277         printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
0278         rc = -EBUSY;
0279         break;
0280     case GSMI_DEVICE_ERROR:
0281         printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
0282         rc = -EFAULT;
0283         break;
0284     case GSMI_NOT_FOUND:
0285         printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
0286         rc = -ENOENT;
0287         break;
0288     case GSMI_LOG_FULL:
0289         printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
0290         rc = -ENOSPC;
0291         break;
0292     case GSMI_HANDSHAKE_CF:
0293     case GSMI_HANDSHAKE_SPIN:
0294     case GSMI_HANDSHAKE_NONE:
0295         rc = result;
0296         break;
0297     default:
0298         printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
0299                cmd, result);
0300         rc = -ENXIO;
0301     }
0302 
0303     return rc;
0304 }
0305 
0306 #ifdef CONFIG_EFI
0307 
0308 static struct efivars efivars;
0309 
0310 static efi_status_t gsmi_get_variable(efi_char16_t *name,
0311                       efi_guid_t *vendor, u32 *attr,
0312                       unsigned long *data_size,
0313                       void *data)
0314 {
0315     struct gsmi_nvram_var_param param = {
0316         .name_ptr = gsmi_dev.name_buf->address,
0317         .data_ptr = gsmi_dev.data_buf->address,
0318         .data_len = (u32)*data_size,
0319     };
0320     efi_status_t ret = EFI_SUCCESS;
0321     unsigned long flags;
0322     size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
0323     int rc;
0324 
0325     if (name_len >= GSMI_BUF_SIZE / 2)
0326         return EFI_BAD_BUFFER_SIZE;
0327 
0328     spin_lock_irqsave(&gsmi_dev.lock, flags);
0329 
0330     /* Vendor guid */
0331     memcpy(&param.guid, vendor, sizeof(param.guid));
0332 
0333     /* variable name, already in UTF-16 */
0334     memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
0335     memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
0336 
0337     /* data pointer */
0338     memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
0339 
0340     /* parameter buffer */
0341     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0342     memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
0343 
0344     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
0345     if (rc < 0) {
0346         printk(KERN_ERR "gsmi: Get Variable failed\n");
0347         ret = EFI_LOAD_ERROR;
0348     } else if (rc == 1) {
0349         /* variable was not found */
0350         ret = EFI_NOT_FOUND;
0351     } else {
0352         /* Get the arguments back */
0353         memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
0354 
0355         /* The size reported is the min of all of our buffers */
0356         *data_size = min_t(unsigned long, *data_size,
0357                         gsmi_dev.data_buf->length);
0358         *data_size = min_t(unsigned long, *data_size, param.data_len);
0359 
0360         /* Copy data back to return buffer. */
0361         memcpy(data, gsmi_dev.data_buf->start, *data_size);
0362 
0363         /* All variables are have the following attributes */
0364         *attr = EFI_VARIABLE_NON_VOLATILE |
0365             EFI_VARIABLE_BOOTSERVICE_ACCESS |
0366             EFI_VARIABLE_RUNTIME_ACCESS;
0367     }
0368 
0369     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0370 
0371     return ret;
0372 }
0373 
0374 static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
0375                        efi_char16_t *name,
0376                        efi_guid_t *vendor)
0377 {
0378     struct gsmi_get_next_var_param param = {
0379         .name_ptr = gsmi_dev.name_buf->address,
0380         .name_len = gsmi_dev.name_buf->length,
0381     };
0382     efi_status_t ret = EFI_SUCCESS;
0383     int rc;
0384     unsigned long flags;
0385 
0386     /* For the moment, only support buffers that exactly match in size */
0387     if (*name_size != GSMI_BUF_SIZE)
0388         return EFI_BAD_BUFFER_SIZE;
0389 
0390     /* Let's make sure the thing is at least null-terminated */
0391     if (ucs2_strnlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
0392         return EFI_INVALID_PARAMETER;
0393 
0394     spin_lock_irqsave(&gsmi_dev.lock, flags);
0395 
0396     /* guid */
0397     memcpy(&param.guid, vendor, sizeof(param.guid));
0398 
0399     /* variable name, already in UTF-16 */
0400     memcpy(gsmi_dev.name_buf->start, name, *name_size);
0401 
0402     /* parameter buffer */
0403     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0404     memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
0405 
0406     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
0407     if (rc < 0) {
0408         printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
0409         ret = EFI_LOAD_ERROR;
0410     } else if (rc == 1) {
0411         /* variable not found -- end of list */
0412         ret = EFI_NOT_FOUND;
0413     } else {
0414         /* copy variable data back to return buffer */
0415         memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
0416 
0417         /* Copy the name back */
0418         memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
0419         *name_size = ucs2_strnlen(name, GSMI_BUF_SIZE / 2) * 2;
0420 
0421         /* copy guid to return buffer */
0422         memcpy(vendor, &param.guid, sizeof(param.guid));
0423         ret = EFI_SUCCESS;
0424     }
0425 
0426     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0427 
0428     return ret;
0429 }
0430 
0431 static efi_status_t gsmi_set_variable(efi_char16_t *name,
0432                       efi_guid_t *vendor,
0433                       u32 attr,
0434                       unsigned long data_size,
0435                       void *data)
0436 {
0437     struct gsmi_nvram_var_param param = {
0438         .name_ptr = gsmi_dev.name_buf->address,
0439         .data_ptr = gsmi_dev.data_buf->address,
0440         .data_len = (u32)data_size,
0441         .attributes = EFI_VARIABLE_NON_VOLATILE |
0442                   EFI_VARIABLE_BOOTSERVICE_ACCESS |
0443                   EFI_VARIABLE_RUNTIME_ACCESS,
0444     };
0445     size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
0446     efi_status_t ret = EFI_SUCCESS;
0447     int rc;
0448     unsigned long flags;
0449 
0450     if (name_len >= GSMI_BUF_SIZE / 2)
0451         return EFI_BAD_BUFFER_SIZE;
0452 
0453     spin_lock_irqsave(&gsmi_dev.lock, flags);
0454 
0455     /* guid */
0456     memcpy(&param.guid, vendor, sizeof(param.guid));
0457 
0458     /* variable name, already in UTF-16 */
0459     memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
0460     memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
0461 
0462     /* data pointer */
0463     memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
0464     memcpy(gsmi_dev.data_buf->start, data, data_size);
0465 
0466     /* parameter buffer */
0467     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0468     memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
0469 
0470     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
0471     if (rc < 0) {
0472         printk(KERN_ERR "gsmi: Set Variable failed\n");
0473         ret = EFI_INVALID_PARAMETER;
0474     }
0475 
0476     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0477 
0478     return ret;
0479 }
0480 
0481 static const struct efivar_operations efivar_ops = {
0482     .get_variable = gsmi_get_variable,
0483     .set_variable = gsmi_set_variable,
0484     .get_next_variable = gsmi_get_next_variable,
0485 };
0486 
0487 #endif /* CONFIG_EFI */
0488 
0489 static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
0490                    struct bin_attribute *bin_attr,
0491                    char *buf, loff_t pos, size_t count)
0492 {
0493     struct gsmi_set_eventlog_param param = {
0494         .data_ptr = gsmi_dev.data_buf->address,
0495     };
0496     int rc = 0;
0497     unsigned long flags;
0498 
0499     /* Pull the type out */
0500     if (count < sizeof(u32))
0501         return -EINVAL;
0502     param.type = *(u32 *)buf;
0503     buf += sizeof(u32);
0504 
0505     /* The remaining buffer is the data payload */
0506     if ((count - sizeof(u32)) > gsmi_dev.data_buf->length)
0507         return -EINVAL;
0508     param.data_len = count - sizeof(u32);
0509 
0510     spin_lock_irqsave(&gsmi_dev.lock, flags);
0511 
0512     /* data pointer */
0513     memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
0514     memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
0515 
0516     /* parameter buffer */
0517     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0518     memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
0519 
0520     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
0521     if (rc < 0)
0522         printk(KERN_ERR "gsmi: Set Event Log failed\n");
0523 
0524     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0525 
0526     return (rc == 0) ? count : rc;
0527 
0528 }
0529 
0530 static struct bin_attribute eventlog_bin_attr = {
0531     .attr = {.name = "append_to_eventlog", .mode = 0200},
0532     .write = eventlog_write,
0533 };
0534 
0535 static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
0536                      struct kobj_attribute *attr,
0537                      const char *buf, size_t count)
0538 {
0539     int rc;
0540     unsigned long flags;
0541     unsigned long val;
0542     struct {
0543         u32 percentage;
0544         u32 data_type;
0545     } param;
0546 
0547     rc = kstrtoul(buf, 0, &val);
0548     if (rc)
0549         return rc;
0550 
0551     /*
0552      * Value entered is a percentage, 0 through 100, anything else
0553      * is invalid.
0554      */
0555     if (val > 100)
0556         return -EINVAL;
0557 
0558     /* data_type here selects the smbios event log. */
0559     param.percentage = val;
0560     param.data_type = 0;
0561 
0562     spin_lock_irqsave(&gsmi_dev.lock, flags);
0563 
0564     /* parameter buffer */
0565     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0566     memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
0567 
0568     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
0569 
0570     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0571 
0572     if (rc)
0573         return rc;
0574     return count;
0575 }
0576 
0577 static struct kobj_attribute gsmi_clear_eventlog_attr = {
0578     .attr = {.name = "clear_eventlog", .mode = 0200},
0579     .store = gsmi_clear_eventlog_store,
0580 };
0581 
0582 static ssize_t gsmi_clear_config_store(struct kobject *kobj,
0583                        struct kobj_attribute *attr,
0584                        const char *buf, size_t count)
0585 {
0586     int rc;
0587     unsigned long flags;
0588 
0589     spin_lock_irqsave(&gsmi_dev.lock, flags);
0590 
0591     /* clear parameter buffer */
0592     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0593 
0594     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
0595 
0596     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0597 
0598     if (rc)
0599         return rc;
0600     return count;
0601 }
0602 
0603 static struct kobj_attribute gsmi_clear_config_attr = {
0604     .attr = {.name = "clear_config", .mode = 0200},
0605     .store = gsmi_clear_config_store,
0606 };
0607 
0608 static const struct attribute *gsmi_attrs[] = {
0609     &gsmi_clear_config_attr.attr,
0610     &gsmi_clear_eventlog_attr.attr,
0611     NULL,
0612 };
0613 
0614 static int gsmi_shutdown_reason(int reason)
0615 {
0616     struct gsmi_log_entry_type_1 entry = {
0617         .type     = GSMI_LOG_ENTRY_TYPE_KERNEL,
0618         .instance = reason,
0619     };
0620     struct gsmi_set_eventlog_param param = {
0621         .data_len = sizeof(entry),
0622         .type     = 1,
0623     };
0624     static int saved_reason;
0625     int rc = 0;
0626     unsigned long flags;
0627 
0628     /* avoid duplicate entries in the log */
0629     if (saved_reason & (1 << reason))
0630         return 0;
0631 
0632     spin_lock_irqsave(&gsmi_dev.lock, flags);
0633 
0634     saved_reason |= (1 << reason);
0635 
0636     /* data pointer */
0637     memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
0638     memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
0639 
0640     /* parameter buffer */
0641     param.data_ptr = gsmi_dev.data_buf->address;
0642     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0643     memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
0644 
0645     rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
0646 
0647     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0648 
0649     if (rc < 0)
0650         printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
0651     else
0652         printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
0653                reason);
0654 
0655     return rc;
0656 }
0657 
0658 static int gsmi_reboot_callback(struct notifier_block *nb,
0659                 unsigned long reason, void *arg)
0660 {
0661     gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
0662     return NOTIFY_DONE;
0663 }
0664 
0665 static struct notifier_block gsmi_reboot_notifier = {
0666     .notifier_call = gsmi_reboot_callback
0667 };
0668 
0669 static int gsmi_die_callback(struct notifier_block *nb,
0670                  unsigned long reason, void *arg)
0671 {
0672     if (reason == DIE_OOPS)
0673         gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
0674     return NOTIFY_DONE;
0675 }
0676 
0677 static struct notifier_block gsmi_die_notifier = {
0678     .notifier_call = gsmi_die_callback
0679 };
0680 
0681 static int gsmi_panic_callback(struct notifier_block *nb,
0682                    unsigned long reason, void *arg)
0683 {
0684     gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
0685     return NOTIFY_DONE;
0686 }
0687 
0688 static struct notifier_block gsmi_panic_notifier = {
0689     .notifier_call = gsmi_panic_callback,
0690 };
0691 
0692 /*
0693  * This hash function was blatantly copied from include/linux/hash.h.
0694  * It is used by this driver to obfuscate a board name that requires a
0695  * quirk within this driver.
0696  *
0697  * Please do not remove this copy of the function as any changes to the
0698  * global utility hash_64() function would break this driver's ability
0699  * to identify a board and provide the appropriate quirk -- mikew@google.com
0700  */
0701 static u64 __init local_hash_64(u64 val, unsigned bits)
0702 {
0703     u64 hash = val;
0704 
0705     /*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
0706     u64 n = hash;
0707     n <<= 18;
0708     hash -= n;
0709     n <<= 33;
0710     hash -= n;
0711     n <<= 3;
0712     hash += n;
0713     n <<= 3;
0714     hash -= n;
0715     n <<= 4;
0716     hash += n;
0717     n <<= 2;
0718     hash += n;
0719 
0720     /* High bits are more random, so use them. */
0721     return hash >> (64 - bits);
0722 }
0723 
0724 static u32 __init hash_oem_table_id(char s[8])
0725 {
0726     u64 input;
0727     memcpy(&input, s, 8);
0728     return local_hash_64(input, 32);
0729 }
0730 
0731 static const struct dmi_system_id gsmi_dmi_table[] __initconst = {
0732     {
0733         .ident = "Google Board",
0734         .matches = {
0735             DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
0736         },
0737     },
0738     {
0739         .ident = "Coreboot Firmware",
0740         .matches = {
0741             DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
0742         },
0743     },
0744     {}
0745 };
0746 MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
0747 
0748 static __init int gsmi_system_valid(void)
0749 {
0750     u32 hash;
0751     u16 cmd, result;
0752 
0753     if (!dmi_check_system(gsmi_dmi_table))
0754         return -ENODEV;
0755 
0756     /*
0757      * Only newer firmware supports the gsmi interface.  All older
0758      * firmware that didn't support this interface used to plug the
0759      * table name in the first four bytes of the oem_table_id field.
0760      * Newer firmware doesn't do that though, so use that as the
0761      * discriminant factor.  We have to do this in order to
0762      * whitewash our board names out of the public driver.
0763      */
0764     if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
0765         printk(KERN_INFO "gsmi: Board is too old\n");
0766         return -ENODEV;
0767     }
0768 
0769     /* Disable on board with 1.0 BIOS due to Google bug 2602657 */
0770     hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
0771     if (hash == QUIRKY_BOARD_HASH) {
0772         const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
0773         if (strncmp(bios_ver, "1.0", 3) == 0) {
0774             pr_info("gsmi: disabled on this board's BIOS %s\n",
0775                 bios_ver);
0776             return -ENODEV;
0777         }
0778     }
0779 
0780     /* check for valid SMI command port in ACPI FADT */
0781     if (acpi_gbl_FADT.smi_command == 0) {
0782         pr_info("gsmi: missing smi_command\n");
0783         return -ENODEV;
0784     }
0785 
0786     /* Test the smihandler with a bogus command. If it leaves the
0787      * calling argument in %ax untouched, there is no handler for
0788      * GSMI commands.
0789      */
0790     cmd = GSMI_CALLBACK | GSMI_CMD_RESERVED << 8;
0791     asm volatile (
0792         "outb %%al, %%dx\n\t"
0793         : "=a" (result)
0794         : "0" (cmd),
0795           "d" (acpi_gbl_FADT.smi_command)
0796         : "memory", "cc"
0797         );
0798     if (cmd == result) {
0799         pr_info("gsmi: no gsmi handler in firmware\n");
0800         return -ENODEV;
0801     }
0802 
0803     /* Found */
0804     return 0;
0805 }
0806 
0807 static struct kobject *gsmi_kobj;
0808 
0809 static const struct platform_device_info gsmi_dev_info = {
0810     .name       = "gsmi",
0811     .id     = -1,
0812     /* SMI callbacks require 32bit addresses */
0813     .dma_mask   = DMA_BIT_MASK(32),
0814 };
0815 
0816 #ifdef CONFIG_PM
0817 static void gsmi_log_s0ix_info(u8 cmd)
0818 {
0819     unsigned long flags;
0820 
0821     /*
0822      * If platform has not enabled S0ix logging, then no action is
0823      * necessary.
0824      */
0825     if (!s0ix_logging_enable)
0826         return;
0827 
0828     spin_lock_irqsave(&gsmi_dev.lock, flags);
0829 
0830     memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
0831 
0832     gsmi_exec(GSMI_CALLBACK, cmd);
0833 
0834     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0835 }
0836 
0837 static int gsmi_log_s0ix_suspend(struct device *dev)
0838 {
0839     /*
0840      * If system is not suspending via firmware using the standard ACPI Sx
0841      * types, then make a GSMI call to log the suspend info.
0842      */
0843     if (!pm_suspend_via_firmware())
0844         gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND);
0845 
0846     /*
0847      * Always return success, since we do not want suspend
0848      * to fail just because of logging failure.
0849      */
0850     return 0;
0851 }
0852 
0853 static int gsmi_log_s0ix_resume(struct device *dev)
0854 {
0855     /*
0856      * If system did not resume via firmware, then make a GSMI call to log
0857      * the resume info and wake source.
0858      */
0859     if (!pm_resume_via_firmware())
0860         gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME);
0861 
0862     /*
0863      * Always return success, since we do not want resume
0864      * to fail just because of logging failure.
0865      */
0866     return 0;
0867 }
0868 
0869 static const struct dev_pm_ops gsmi_pm_ops = {
0870     .suspend_noirq = gsmi_log_s0ix_suspend,
0871     .resume_noirq = gsmi_log_s0ix_resume,
0872 };
0873 
0874 static int gsmi_platform_driver_probe(struct platform_device *dev)
0875 {
0876     return 0;
0877 }
0878 
0879 static struct platform_driver gsmi_driver_info = {
0880     .driver = {
0881         .name = "gsmi",
0882         .pm = &gsmi_pm_ops,
0883     },
0884     .probe = gsmi_platform_driver_probe,
0885 };
0886 #endif
0887 
0888 static __init int gsmi_init(void)
0889 {
0890     unsigned long flags;
0891     int ret;
0892 
0893     ret = gsmi_system_valid();
0894     if (ret)
0895         return ret;
0896 
0897     gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
0898 
0899 #ifdef CONFIG_PM
0900     ret = platform_driver_register(&gsmi_driver_info);
0901     if (unlikely(ret)) {
0902         printk(KERN_ERR "gsmi: unable to register platform driver\n");
0903         return ret;
0904     }
0905 #endif
0906 
0907     /* register device */
0908     gsmi_dev.pdev = platform_device_register_full(&gsmi_dev_info);
0909     if (IS_ERR(gsmi_dev.pdev)) {
0910         printk(KERN_ERR "gsmi: unable to register platform device\n");
0911         return PTR_ERR(gsmi_dev.pdev);
0912     }
0913 
0914     /* SMI access needs to be serialized */
0915     spin_lock_init(&gsmi_dev.lock);
0916 
0917     ret = -ENOMEM;
0918 
0919     /*
0920      * SLAB cache is created using SLAB_CACHE_DMA32 to ensure that the
0921      * allocations for gsmi_buf come from the DMA32 memory zone. These
0922      * buffers have nothing to do with DMA. They are required for
0923      * communication with firmware executing in SMI mode which can only
0924      * access the bottom 4GiB of physical memory. Since DMA32 memory zone
0925      * guarantees allocation under the 4GiB boundary, this driver creates
0926      * a SLAB cache with SLAB_CACHE_DMA32 flag.
0927      */
0928     gsmi_dev.mem_pool = kmem_cache_create("gsmi", GSMI_BUF_SIZE,
0929                           GSMI_BUF_ALIGN,
0930                           SLAB_CACHE_DMA32, NULL);
0931     if (!gsmi_dev.mem_pool)
0932         goto out_err;
0933 
0934     /*
0935      * pre-allocate buffers because sometimes we are called when
0936      * this is not feasible: oops, panic, die, mce, etc
0937      */
0938     gsmi_dev.name_buf = gsmi_buf_alloc();
0939     if (!gsmi_dev.name_buf) {
0940         printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
0941         goto out_err;
0942     }
0943 
0944     gsmi_dev.data_buf = gsmi_buf_alloc();
0945     if (!gsmi_dev.data_buf) {
0946         printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
0947         goto out_err;
0948     }
0949 
0950     gsmi_dev.param_buf = gsmi_buf_alloc();
0951     if (!gsmi_dev.param_buf) {
0952         printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
0953         goto out_err;
0954     }
0955 
0956     /*
0957      * Determine type of handshake used to serialize the SMI
0958      * entry. See also gsmi_exec().
0959      *
0960      * There's a "behavior" present on some chipsets where writing the
0961      * SMI trigger register in the southbridge doesn't result in an
0962      * immediate SMI. Rather, the processor can execute "a few" more
0963      * instructions before the SMI takes effect. To ensure synchronous
0964      * behavior, implement a handshake between the kernel driver and the
0965      * firmware handler to spin until released. This ioctl determines
0966      * the type of handshake.
0967      *
0968      * NONE: The firmware handler does not implement any
0969      * handshake. Either it doesn't need to, or it's legacy firmware
0970      * that doesn't know it needs to and never will.
0971      *
0972      * CF: The firmware handler will clear the CF in the saved
0973      * state before returning. The driver may set the CF and test for
0974      * it to clear before proceeding.
0975      *
0976      * SPIN: The firmware handler does not implement any handshake
0977      * but the driver should spin for a hundred or so microseconds
0978      * to ensure the SMI has triggered.
0979      *
0980      * Finally, the handler will return -ENOSYS if
0981      * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
0982      * HANDSHAKE_NONE.
0983      */
0984     spin_lock_irqsave(&gsmi_dev.lock, flags);
0985     gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
0986     gsmi_dev.handshake_type =
0987         gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
0988     if (gsmi_dev.handshake_type == -ENOSYS)
0989         gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
0990     spin_unlock_irqrestore(&gsmi_dev.lock, flags);
0991 
0992     /* Remove and clean up gsmi if the handshake could not complete. */
0993     if (gsmi_dev.handshake_type == -ENXIO) {
0994         printk(KERN_INFO "gsmi version " DRIVER_VERSION
0995                " failed to load\n");
0996         ret = -ENODEV;
0997         goto out_err;
0998     }
0999 
1000     /* Register in the firmware directory */
1001     ret = -ENOMEM;
1002     gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
1003     if (!gsmi_kobj) {
1004         printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
1005         goto out_err;
1006     }
1007 
1008     /* Setup eventlog access */
1009     ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
1010     if (ret) {
1011         printk(KERN_INFO "gsmi: Failed to setup eventlog");
1012         goto out_err;
1013     }
1014 
1015     /* Other attributes */
1016     ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
1017     if (ret) {
1018         printk(KERN_INFO "gsmi: Failed to add attrs");
1019         goto out_remove_bin_file;
1020     }
1021 
1022 #ifdef CONFIG_EFI
1023     ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
1024     if (ret) {
1025         printk(KERN_INFO "gsmi: Failed to register efivars\n");
1026         sysfs_remove_files(gsmi_kobj, gsmi_attrs);
1027         goto out_remove_bin_file;
1028     }
1029 #endif
1030 
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);
1035 
1036     printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
1037 
1038     return 0;
1039 
1040 out_remove_bin_file:
1041     sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1042 out_err:
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);
1050 #ifdef CONFIG_PM
1051     platform_driver_unregister(&gsmi_driver_info);
1052 #endif
1053     return ret;
1054 }
1055 
1056 static void __exit gsmi_exit(void)
1057 {
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);
1062 #ifdef CONFIG_EFI
1063     efivars_unregister(&efivars);
1064 #endif
1065 
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);
1074 #ifdef CONFIG_PM
1075     platform_driver_unregister(&gsmi_driver_info);
1076 #endif
1077 }
1078 
1079 module_init(gsmi_init);
1080 module_exit(gsmi_exit);
1081 
1082 MODULE_AUTHOR("Google, Inc.");
1083 MODULE_LICENSE("GPL");