xref: /openbmc/linux/drivers/firmware/google/gsmi.c (revision 84b102f5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2010 Google Inc. All Rights Reserved.
4  * Author: dlaurie@google.com (Duncan Laurie)
5  *
6  * Re-worked to expose sysfs APIs by mikew@google.com (Mike Waychison)
7  *
8  * EFI SMI interface for Google platforms
9  */
10 
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/fs.h>
21 #include <linux/slab.h>
22 #include <linux/ioctl.h>
23 #include <linux/acpi.h>
24 #include <linux/io.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>
33 
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 */
44 
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
50 
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
65 
66 #define QUIRKY_BOARD_HASH 0x78a30a50
67 
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
79 
80 /* Magic entry type for kernel events */
81 #define GSMI_LOG_ENTRY_TYPE_KERNEL     0xDEAD
82 
83 /* SMI buffers must be in 32bit physical address space */
84 struct gsmi_buf {
85 	u8 *start;			/* start of buffer */
86 	size_t length;			/* length of buffer */
87 	u32 address;			/* physical address of buffer */
88 };
89 
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 */
99 } gsmi_dev;
100 
101 /* Packed structures for communicating with the firmware */
102 struct gsmi_nvram_var_param {
103 	efi_guid_t	guid;
104 	u32		name_ptr;
105 	u32		attributes;
106 	u32		data_len;
107 	u32		data_ptr;
108 } __packed;
109 
110 struct gsmi_get_next_var_param {
111 	u8	guid[GSMI_GUID_SIZE];
112 	u32	name_ptr;
113 	u32	name_len;
114 } __packed;
115 
116 struct gsmi_set_eventlog_param {
117 	u32	data_ptr;
118 	u32	data_len;
119 	u32	type;
120 } __packed;
121 
122 /* Event log formats */
123 struct gsmi_log_entry_type_1 {
124 	u16	type;
125 	u32	instance;
126 } __packed;
127 
128 /*
129  * Some platforms don't have explicit SMI handshake
130  * and need to wait for SMI to complete.
131  */
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.");
137 
138 /*
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
142  * option.
143  */
144 static bool s0ix_logging_enable;
145 module_param(s0ix_logging_enable, bool, 0600);
146 
147 static struct gsmi_buf *gsmi_buf_alloc(void)
148 {
149 	struct gsmi_buf *smibuf;
150 
151 	smibuf = kzalloc(sizeof(*smibuf), GFP_KERNEL);
152 	if (!smibuf) {
153 		printk(KERN_ERR "gsmi: out of memory\n");
154 		return NULL;
155 	}
156 
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");
161 		kfree(smibuf);
162 		return NULL;
163 	}
164 
165 	/* fill in the buffer handle */
166 	smibuf->length = GSMI_BUF_SIZE;
167 	smibuf->address = (u32)virt_to_phys(smibuf->start);
168 
169 	return smibuf;
170 }
171 
172 static void gsmi_buf_free(struct gsmi_buf *smibuf)
173 {
174 	if (smibuf) {
175 		if (smibuf->start)
176 			kmem_cache_free(gsmi_dev.mem_pool, smibuf->start);
177 		kfree(smibuf);
178 	}
179 }
180 
181 /*
182  * Make a call to gsmi func(sub).  GSMI error codes are translated to
183  * in-kernel errnos (0 on success, -ERRNO on error).
184  */
185 static int gsmi_exec(u8 func, u8 sub)
186 {
187 	u16 cmd = (sub << 8) | func;
188 	u16 result = 0;
189 	int rc = 0;
190 
191 	/*
192 	 * AH  : Subfunction number
193 	 * AL  : Function number
194 	 * EBX : Parameter block address
195 	 * DX  : SMI command port
196 	 *
197 	 * Three protocols here. See also the comment in gsmi_init().
198 	 */
199 	if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
200 		/*
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
205 		 * entering SMM.
206 		 */
207 		asm volatile (
208 			"stc\n"
209 			"outb %%al, %%dx\n"
210 		"1:      jc 1b\n"
211 			: "=a" (result)
212 			: "0" (cmd),
213 			  "d" (gsmi_dev.smi_cmd),
214 			  "b" (gsmi_dev.param_buf->address)
215 			: "memory", "cc"
216 		);
217 	} else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
218 		/*
219 		 * If handshake_type == HANDSHAKE_SPIN we spin a
220 		 * hundred-ish usecs to ensure the SMI has triggered.
221 		 */
222 		asm volatile (
223 			"outb %%al, %%dx\n"
224 		"1:      loop 1b\n"
225 			: "=a" (result)
226 			: "0" (cmd),
227 			  "d" (gsmi_dev.smi_cmd),
228 			  "b" (gsmi_dev.param_buf->address),
229 			  "c" (spincount)
230 			: "memory", "cc"
231 		);
232 	} else {
233 		/*
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.
237 		 */
238 		asm volatile (
239 			"outb %%al, %%dx\n\t"
240 			: "=a" (result)
241 			: "0" (cmd),
242 			  "d" (gsmi_dev.smi_cmd),
243 			  "b" (gsmi_dev.param_buf->address)
244 			: "memory", "cc"
245 		);
246 	}
247 
248 	/* check return code from SMI handler */
249 	switch (result) {
250 	case GSMI_SUCCESS:
251 		break;
252 	case GSMI_VAR_NOT_FOUND:
253 		/* not really an error, but let the caller know */
254 		rc = 1;
255 		break;
256 	case GSMI_INVALID_PARAMETER:
257 		printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
258 		rc = -EINVAL;
259 		break;
260 	case GSMI_BUFFER_TOO_SMALL:
261 		printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
262 		rc = -ENOMEM;
263 		break;
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",
268 			       cmd);
269 		rc = -ENOSYS;
270 		break;
271 	case GSMI_NOT_READY:
272 		printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
273 		rc = -EBUSY;
274 		break;
275 	case GSMI_DEVICE_ERROR:
276 		printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
277 		rc = -EFAULT;
278 		break;
279 	case GSMI_NOT_FOUND:
280 		printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
281 		rc = -ENOENT;
282 		break;
283 	case GSMI_LOG_FULL:
284 		printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
285 		rc = -ENOSPC;
286 		break;
287 	case GSMI_HANDSHAKE_CF:
288 	case GSMI_HANDSHAKE_SPIN:
289 	case GSMI_HANDSHAKE_NONE:
290 		rc = result;
291 		break;
292 	default:
293 		printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
294 		       cmd, result);
295 		rc = -ENXIO;
296 	}
297 
298 	return rc;
299 }
300 
301 #ifdef CONFIG_EFI
302 
303 static struct efivars efivars;
304 
305 static efi_status_t gsmi_get_variable(efi_char16_t *name,
306 				      efi_guid_t *vendor, u32 *attr,
307 				      unsigned long *data_size,
308 				      void *data)
309 {
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,
314 	};
315 	efi_status_t ret = EFI_SUCCESS;
316 	unsigned long flags;
317 	size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
318 	int rc;
319 
320 	if (name_len >= GSMI_BUF_SIZE / 2)
321 		return EFI_BAD_BUFFER_SIZE;
322 
323 	spin_lock_irqsave(&gsmi_dev.lock, flags);
324 
325 	/* Vendor guid */
326 	memcpy(&param.guid, vendor, sizeof(param.guid));
327 
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);
331 
332 	/* data pointer */
333 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
334 
335 	/* parameter buffer */
336 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
337 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
338 
339 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
340 	if (rc < 0) {
341 		printk(KERN_ERR "gsmi: Get Variable failed\n");
342 		ret = EFI_LOAD_ERROR;
343 	} else if (rc == 1) {
344 		/* variable was not found */
345 		ret = EFI_NOT_FOUND;
346 	} else {
347 		/* Get the arguments back */
348 		memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
349 
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);
354 
355 		/* Copy data back to return buffer. */
356 		memcpy(data, gsmi_dev.data_buf->start, *data_size);
357 
358 		/* All variables are have the following attributes */
359 		*attr = EFI_VARIABLE_NON_VOLATILE |
360 			EFI_VARIABLE_BOOTSERVICE_ACCESS |
361 			EFI_VARIABLE_RUNTIME_ACCESS;
362 	}
363 
364 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
365 
366 	return ret;
367 }
368 
369 static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
370 					   efi_char16_t *name,
371 					   efi_guid_t *vendor)
372 {
373 	struct gsmi_get_next_var_param param = {
374 		.name_ptr = gsmi_dev.name_buf->address,
375 		.name_len = gsmi_dev.name_buf->length,
376 	};
377 	efi_status_t ret = EFI_SUCCESS;
378 	int rc;
379 	unsigned long flags;
380 
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;
384 
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;
388 
389 	spin_lock_irqsave(&gsmi_dev.lock, flags);
390 
391 	/* guid */
392 	memcpy(&param.guid, vendor, sizeof(param.guid));
393 
394 	/* variable name, already in UTF-16 */
395 	memcpy(gsmi_dev.name_buf->start, name, *name_size);
396 
397 	/* parameter buffer */
398 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
399 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
400 
401 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
402 	if (rc < 0) {
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 */
407 		ret = EFI_NOT_FOUND;
408 	} else {
409 		/* copy variable data back to return buffer */
410 		memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
411 
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;
415 
416 		/* copy guid to return buffer */
417 		memcpy(vendor, &param.guid, sizeof(param.guid));
418 		ret = EFI_SUCCESS;
419 	}
420 
421 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
422 
423 	return ret;
424 }
425 
426 static efi_status_t gsmi_set_variable(efi_char16_t *name,
427 				      efi_guid_t *vendor,
428 				      u32 attr,
429 				      unsigned long data_size,
430 				      void *data)
431 {
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,
439 	};
440 	size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
441 	efi_status_t ret = EFI_SUCCESS;
442 	int rc;
443 	unsigned long flags;
444 
445 	if (name_len >= GSMI_BUF_SIZE / 2)
446 		return EFI_BAD_BUFFER_SIZE;
447 
448 	spin_lock_irqsave(&gsmi_dev.lock, flags);
449 
450 	/* guid */
451 	memcpy(&param.guid, vendor, sizeof(param.guid));
452 
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);
456 
457 	/* data pointer */
458 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
459 	memcpy(gsmi_dev.data_buf->start, data, data_size);
460 
461 	/* parameter buffer */
462 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
463 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
464 
465 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
466 	if (rc < 0) {
467 		printk(KERN_ERR "gsmi: Set Variable failed\n");
468 		ret = EFI_INVALID_PARAMETER;
469 	}
470 
471 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
472 
473 	return ret;
474 }
475 
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,
480 };
481 
482 #endif /* CONFIG_EFI */
483 
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)
487 {
488 	struct gsmi_set_eventlog_param param = {
489 		.data_ptr = gsmi_dev.data_buf->address,
490 	};
491 	int rc = 0;
492 	unsigned long flags;
493 
494 	/* Pull the type out */
495 	if (count < sizeof(u32))
496 		return -EINVAL;
497 	param.type = *(u32 *)buf;
498 	buf += sizeof(u32);
499 
500 	/* The remaining buffer is the data payload */
501 	if ((count - sizeof(u32)) > gsmi_dev.data_buf->length)
502 		return -EINVAL;
503 	param.data_len = count - sizeof(u32);
504 
505 	spin_lock_irqsave(&gsmi_dev.lock, flags);
506 
507 	/* data pointer */
508 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
509 	memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
510 
511 	/* parameter buffer */
512 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
513 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
514 
515 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
516 	if (rc < 0)
517 		printk(KERN_ERR "gsmi: Set Event Log failed\n");
518 
519 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
520 
521 	return (rc == 0) ? count : rc;
522 
523 }
524 
525 static struct bin_attribute eventlog_bin_attr = {
526 	.attr = {.name = "append_to_eventlog", .mode = 0200},
527 	.write = eventlog_write,
528 };
529 
530 static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
531 					 struct kobj_attribute *attr,
532 					 const char *buf, size_t count)
533 {
534 	int rc;
535 	unsigned long flags;
536 	unsigned long val;
537 	struct {
538 		u32 percentage;
539 		u32 data_type;
540 	} param;
541 
542 	rc = kstrtoul(buf, 0, &val);
543 	if (rc)
544 		return rc;
545 
546 	/*
547 	 * Value entered is a percentage, 0 through 100, anything else
548 	 * is invalid.
549 	 */
550 	if (val > 100)
551 		return -EINVAL;
552 
553 	/* data_type here selects the smbios event log. */
554 	param.percentage = val;
555 	param.data_type = 0;
556 
557 	spin_lock_irqsave(&gsmi_dev.lock, flags);
558 
559 	/* parameter buffer */
560 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
561 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
562 
563 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
564 
565 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
566 
567 	if (rc)
568 		return rc;
569 	return count;
570 }
571 
572 static struct kobj_attribute gsmi_clear_eventlog_attr = {
573 	.attr = {.name = "clear_eventlog", .mode = 0200},
574 	.store = gsmi_clear_eventlog_store,
575 };
576 
577 static ssize_t gsmi_clear_config_store(struct kobject *kobj,
578 				       struct kobj_attribute *attr,
579 				       const char *buf, size_t count)
580 {
581 	int rc;
582 	unsigned long flags;
583 
584 	spin_lock_irqsave(&gsmi_dev.lock, flags);
585 
586 	/* clear parameter buffer */
587 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
588 
589 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
590 
591 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
592 
593 	if (rc)
594 		return rc;
595 	return count;
596 }
597 
598 static struct kobj_attribute gsmi_clear_config_attr = {
599 	.attr = {.name = "clear_config", .mode = 0200},
600 	.store = gsmi_clear_config_store,
601 };
602 
603 static const struct attribute *gsmi_attrs[] = {
604 	&gsmi_clear_config_attr.attr,
605 	&gsmi_clear_eventlog_attr.attr,
606 	NULL,
607 };
608 
609 static int gsmi_shutdown_reason(int reason)
610 {
611 	struct gsmi_log_entry_type_1 entry = {
612 		.type     = GSMI_LOG_ENTRY_TYPE_KERNEL,
613 		.instance = reason,
614 	};
615 	struct gsmi_set_eventlog_param param = {
616 		.data_len = sizeof(entry),
617 		.type     = 1,
618 	};
619 	static int saved_reason;
620 	int rc = 0;
621 	unsigned long flags;
622 
623 	/* avoid duplicate entries in the log */
624 	if (saved_reason & (1 << reason))
625 		return 0;
626 
627 	spin_lock_irqsave(&gsmi_dev.lock, flags);
628 
629 	saved_reason |= (1 << reason);
630 
631 	/* data pointer */
632 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
633 	memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
634 
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, &param, sizeof(param));
639 
640 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
641 
642 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
643 
644 	if (rc < 0)
645 		printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
646 	else
647 		printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
648 		       reason);
649 
650 	return rc;
651 }
652 
653 static int gsmi_reboot_callback(struct notifier_block *nb,
654 				unsigned long reason, void *arg)
655 {
656 	gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
657 	return NOTIFY_DONE;
658 }
659 
660 static struct notifier_block gsmi_reboot_notifier = {
661 	.notifier_call = gsmi_reboot_callback
662 };
663 
664 static int gsmi_die_callback(struct notifier_block *nb,
665 			     unsigned long reason, void *arg)
666 {
667 	if (reason == DIE_OOPS)
668 		gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
669 	return NOTIFY_DONE;
670 }
671 
672 static struct notifier_block gsmi_die_notifier = {
673 	.notifier_call = gsmi_die_callback
674 };
675 
676 static int gsmi_panic_callback(struct notifier_block *nb,
677 			       unsigned long reason, void *arg)
678 {
679 	gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
680 	return NOTIFY_DONE;
681 }
682 
683 static struct notifier_block gsmi_panic_notifier = {
684 	.notifier_call = gsmi_panic_callback,
685 };
686 
687 /*
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.
691  *
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
695  */
696 static u64 __init local_hash_64(u64 val, unsigned bits)
697 {
698 	u64 hash = val;
699 
700 	/*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
701 	u64 n = hash;
702 	n <<= 18;
703 	hash -= n;
704 	n <<= 33;
705 	hash -= n;
706 	n <<= 3;
707 	hash += n;
708 	n <<= 3;
709 	hash -= n;
710 	n <<= 4;
711 	hash += n;
712 	n <<= 2;
713 	hash += n;
714 
715 	/* High bits are more random, so use them. */
716 	return hash >> (64 - bits);
717 }
718 
719 static u32 __init hash_oem_table_id(char s[8])
720 {
721 	u64 input;
722 	memcpy(&input, s, 8);
723 	return local_hash_64(input, 32);
724 }
725 
726 static const struct dmi_system_id gsmi_dmi_table[] __initconst = {
727 	{
728 		.ident = "Google Board",
729 		.matches = {
730 			DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
731 		},
732 	},
733 	{
734 		.ident = "Coreboot Firmware",
735 		.matches = {
736 			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
737 		},
738 	},
739 	{}
740 };
741 MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
742 
743 static __init int gsmi_system_valid(void)
744 {
745 	u32 hash;
746 	u16 cmd, result;
747 
748 	if (!dmi_check_system(gsmi_dmi_table))
749 		return -ENODEV;
750 
751 	/*
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.
758 	 */
759 	if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
760 		printk(KERN_INFO "gsmi: Board is too old\n");
761 		return -ENODEV;
762 	}
763 
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",
770 				bios_ver);
771 			return -ENODEV;
772 		}
773 	}
774 
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");
778 		return -ENODEV;
779 	}
780 
781 	/* Test the smihandler with a bogus command. If it leaves the
782 	 * calling argument in %ax untouched, there is no handler for
783 	 * GSMI commands.
784 	 */
785 	cmd = GSMI_CALLBACK | GSMI_CMD_RESERVED << 8;
786 	asm volatile (
787 		"outb %%al, %%dx\n\t"
788 		: "=a" (result)
789 		: "0" (cmd),
790 		  "d" (acpi_gbl_FADT.smi_command)
791 		: "memory", "cc"
792 		);
793 	if (cmd == result) {
794 		pr_info("gsmi: no gsmi handler in firmware\n");
795 		return -ENODEV;
796 	}
797 
798 	/* Found */
799 	return 0;
800 }
801 
802 static struct kobject *gsmi_kobj;
803 
804 static const struct platform_device_info gsmi_dev_info = {
805 	.name		= "gsmi",
806 	.id		= -1,
807 	/* SMI callbacks require 32bit addresses */
808 	.dma_mask	= DMA_BIT_MASK(32),
809 };
810 
811 #ifdef CONFIG_PM
812 static void gsmi_log_s0ix_info(u8 cmd)
813 {
814 	unsigned long flags;
815 
816 	/*
817 	 * If platform has not enabled S0ix logging, then no action is
818 	 * necessary.
819 	 */
820 	if (!s0ix_logging_enable)
821 		return;
822 
823 	spin_lock_irqsave(&gsmi_dev.lock, flags);
824 
825 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
826 
827 	gsmi_exec(GSMI_CALLBACK, cmd);
828 
829 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
830 }
831 
832 static int gsmi_log_s0ix_suspend(struct device *dev)
833 {
834 	/*
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.
837 	 */
838 	if (!pm_suspend_via_firmware())
839 		gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND);
840 
841 	/*
842 	 * Always return success, since we do not want suspend
843 	 * to fail just because of logging failure.
844 	 */
845 	return 0;
846 }
847 
848 static int gsmi_log_s0ix_resume(struct device *dev)
849 {
850 	/*
851 	 * If system did not resume via firmware, then make a GSMI call to log
852 	 * the resume info and wake source.
853 	 */
854 	if (!pm_resume_via_firmware())
855 		gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME);
856 
857 	/*
858 	 * Always return success, since we do not want resume
859 	 * to fail just because of logging failure.
860 	 */
861 	return 0;
862 }
863 
864 static const struct dev_pm_ops gsmi_pm_ops = {
865 	.suspend_noirq = gsmi_log_s0ix_suspend,
866 	.resume_noirq = gsmi_log_s0ix_resume,
867 };
868 
869 static int gsmi_platform_driver_probe(struct platform_device *dev)
870 {
871 	return 0;
872 }
873 
874 static struct platform_driver gsmi_driver_info = {
875 	.driver = {
876 		.name = "gsmi",
877 		.pm = &gsmi_pm_ops,
878 	},
879 	.probe = gsmi_platform_driver_probe,
880 };
881 #endif
882 
883 static __init int gsmi_init(void)
884 {
885 	unsigned long flags;
886 	int ret;
887 
888 	ret = gsmi_system_valid();
889 	if (ret)
890 		return ret;
891 
892 	gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
893 
894 #ifdef CONFIG_PM
895 	ret = platform_driver_register(&gsmi_driver_info);
896 	if (unlikely(ret)) {
897 		printk(KERN_ERR "gsmi: unable to register platform driver\n");
898 		return ret;
899 	}
900 #endif
901 
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);
907 	}
908 
909 	/* SMI access needs to be serialized */
910 	spin_lock_init(&gsmi_dev.lock);
911 
912 	ret = -ENOMEM;
913 
914 	/*
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.
922 	 */
923 	gsmi_dev.mem_pool = kmem_cache_create("gsmi", GSMI_BUF_SIZE,
924 					      GSMI_BUF_ALIGN,
925 					      SLAB_CACHE_DMA32, NULL);
926 	if (!gsmi_dev.mem_pool)
927 		goto out_err;
928 
929 	/*
930 	 * pre-allocate buffers because sometimes we are called when
931 	 * this is not feasible: oops, panic, die, mce, etc
932 	 */
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");
936 		goto out_err;
937 	}
938 
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");
942 		goto out_err;
943 	}
944 
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");
948 		goto out_err;
949 	}
950 
951 	/*
952 	 * Determine type of handshake used to serialize the SMI
953 	 * entry. See also gsmi_exec().
954 	 *
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.
962 	 *
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.
966 	 *
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.
970 	 *
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.
974 	 *
975 	 * Finally, the handler will return -ENOSYS if
976 	 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
977 	 * HANDSHAKE_NONE.
978 	 */
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);
986 
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");
991 		ret = -ENODEV;
992 		goto out_err;
993 	}
994 
995 	/* Register in the firmware directory */
996 	ret = -ENOMEM;
997 	gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
998 	if (!gsmi_kobj) {
999 		printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
1000 		goto out_err;
1001 	}
1002 
1003 	/* Setup eventlog access */
1004 	ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
1005 	if (ret) {
1006 		printk(KERN_INFO "gsmi: Failed to setup eventlog");
1007 		goto out_err;
1008 	}
1009 
1010 	/* Other attributes */
1011 	ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
1012 	if (ret) {
1013 		printk(KERN_INFO "gsmi: Failed to add attrs");
1014 		goto out_remove_bin_file;
1015 	}
1016 
1017 #ifdef CONFIG_EFI
1018 	ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
1019 	if (ret) {
1020 		printk(KERN_INFO "gsmi: Failed to register efivars\n");
1021 		sysfs_remove_files(gsmi_kobj, gsmi_attrs);
1022 		goto out_remove_bin_file;
1023 	}
1024 #endif
1025 
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);
1030 
1031 	printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
1032 
1033 	return 0;
1034 
1035 out_remove_bin_file:
1036 	sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1037 out_err:
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);
1045 #ifdef CONFIG_PM
1046 	platform_driver_unregister(&gsmi_driver_info);
1047 #endif
1048 	return ret;
1049 }
1050 
1051 static void __exit gsmi_exit(void)
1052 {
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);
1057 #ifdef CONFIG_EFI
1058 	efivars_unregister(&efivars);
1059 #endif
1060 
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);
1069 #ifdef CONFIG_PM
1070 	platform_driver_unregister(&gsmi_driver_info);
1071 #endif
1072 }
1073 
1074 module_init(gsmi_init);
1075 module_exit(gsmi_exit);
1076 
1077 MODULE_AUTHOR("Google, Inc.");
1078 MODULE_LICENSE("GPL");
1079