xref: /openbmc/linux/drivers/firmware/google/gsmi.c (revision 5ff32883)
1 /*
2  * Copyright 2010 Google Inc. All Rights Reserved.
3  * Author: dlaurie@google.com (Duncan Laurie)
4  *
5  * Re-worked to expose sysfs APIs by mikew@google.com (Mike Waychison)
6  *
7  * EFI SMI interface for Google platforms
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/device.h>
14 #include <linux/platform_device.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/spinlock.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/dmapool.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 
79 /* Magic entry type for kernel events */
80 #define GSMI_LOG_ENTRY_TYPE_KERNEL     0xDEAD
81 
82 /* SMI buffers must be in 32bit physical address space */
83 struct gsmi_buf {
84 	u8 *start;			/* start of buffer */
85 	size_t length;			/* length of buffer */
86 	dma_addr_t handle;		/* dma allocation handle */
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 dma_pool *dma_pool;	/* DMA buffer pool */
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 = dma_pool_alloc(gsmi_dev.dma_pool, GFP_KERNEL,
159 				       &smibuf->handle);
160 	if (!smibuf->start) {
161 		printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
162 		kfree(smibuf);
163 		return NULL;
164 	}
165 
166 	/* fill in the buffer handle */
167 	smibuf->length = GSMI_BUF_SIZE;
168 	smibuf->address = (u32)virt_to_phys(smibuf->start);
169 
170 	return smibuf;
171 }
172 
173 static void gsmi_buf_free(struct gsmi_buf *smibuf)
174 {
175 	if (smibuf) {
176 		if (smibuf->start)
177 			dma_pool_free(gsmi_dev.dma_pool, smibuf->start,
178 				      smibuf->handle);
179 		kfree(smibuf);
180 	}
181 }
182 
183 /*
184  * Make a call to gsmi func(sub).  GSMI error codes are translated to
185  * in-kernel errnos (0 on success, -ERRNO on error).
186  */
187 static int gsmi_exec(u8 func, u8 sub)
188 {
189 	u16 cmd = (sub << 8) | func;
190 	u16 result = 0;
191 	int rc = 0;
192 
193 	/*
194 	 * AH  : Subfunction number
195 	 * AL  : Function number
196 	 * EBX : Parameter block address
197 	 * DX  : SMI command port
198 	 *
199 	 * Three protocols here. See also the comment in gsmi_init().
200 	 */
201 	if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_CF) {
202 		/*
203 		 * If handshake_type == HANDSHAKE_CF then set CF on the
204 		 * way in and wait for the handler to clear it; this avoids
205 		 * corrupting register state on those chipsets which have
206 		 * a delay between writing the SMI trigger register and
207 		 * entering SMM.
208 		 */
209 		asm volatile (
210 			"stc\n"
211 			"outb %%al, %%dx\n"
212 		"1:      jc 1b\n"
213 			: "=a" (result)
214 			: "0" (cmd),
215 			  "d" (gsmi_dev.smi_cmd),
216 			  "b" (gsmi_dev.param_buf->address)
217 			: "memory", "cc"
218 		);
219 	} else if (gsmi_dev.handshake_type == GSMI_HANDSHAKE_SPIN) {
220 		/*
221 		 * If handshake_type == HANDSHAKE_SPIN we spin a
222 		 * hundred-ish usecs to ensure the SMI has triggered.
223 		 */
224 		asm volatile (
225 			"outb %%al, %%dx\n"
226 		"1:      loop 1b\n"
227 			: "=a" (result)
228 			: "0" (cmd),
229 			  "d" (gsmi_dev.smi_cmd),
230 			  "b" (gsmi_dev.param_buf->address),
231 			  "c" (spincount)
232 			: "memory", "cc"
233 		);
234 	} else {
235 		/*
236 		 * If handshake_type == HANDSHAKE_NONE we do nothing;
237 		 * either we don't need to or it's legacy firmware that
238 		 * doesn't understand the CF protocol.
239 		 */
240 		asm volatile (
241 			"outb %%al, %%dx\n\t"
242 			: "=a" (result)
243 			: "0" (cmd),
244 			  "d" (gsmi_dev.smi_cmd),
245 			  "b" (gsmi_dev.param_buf->address)
246 			: "memory", "cc"
247 		);
248 	}
249 
250 	/* check return code from SMI handler */
251 	switch (result) {
252 	case GSMI_SUCCESS:
253 		break;
254 	case GSMI_VAR_NOT_FOUND:
255 		/* not really an error, but let the caller know */
256 		rc = 1;
257 		break;
258 	case GSMI_INVALID_PARAMETER:
259 		printk(KERN_ERR "gsmi: exec 0x%04x: Invalid parameter\n", cmd);
260 		rc = -EINVAL;
261 		break;
262 	case GSMI_BUFFER_TOO_SMALL:
263 		printk(KERN_ERR "gsmi: exec 0x%04x: Buffer too small\n", cmd);
264 		rc = -ENOMEM;
265 		break;
266 	case GSMI_UNSUPPORTED:
267 	case GSMI_UNSUPPORTED2:
268 		if (sub != GSMI_CMD_HANDSHAKE_TYPE)
269 			printk(KERN_ERR "gsmi: exec 0x%04x: Not supported\n",
270 			       cmd);
271 		rc = -ENOSYS;
272 		break;
273 	case GSMI_NOT_READY:
274 		printk(KERN_ERR "gsmi: exec 0x%04x: Not ready\n", cmd);
275 		rc = -EBUSY;
276 		break;
277 	case GSMI_DEVICE_ERROR:
278 		printk(KERN_ERR "gsmi: exec 0x%04x: Device error\n", cmd);
279 		rc = -EFAULT;
280 		break;
281 	case GSMI_NOT_FOUND:
282 		printk(KERN_ERR "gsmi: exec 0x%04x: Data not found\n", cmd);
283 		rc = -ENOENT;
284 		break;
285 	case GSMI_LOG_FULL:
286 		printk(KERN_ERR "gsmi: exec 0x%04x: Log full\n", cmd);
287 		rc = -ENOSPC;
288 		break;
289 	case GSMI_HANDSHAKE_CF:
290 	case GSMI_HANDSHAKE_SPIN:
291 	case GSMI_HANDSHAKE_NONE:
292 		rc = result;
293 		break;
294 	default:
295 		printk(KERN_ERR "gsmi: exec 0x%04x: Unknown error 0x%04x\n",
296 		       cmd, result);
297 		rc = -ENXIO;
298 	}
299 
300 	return rc;
301 }
302 
303 #ifdef CONFIG_EFI_VARS
304 
305 static struct efivars efivars;
306 
307 static efi_status_t gsmi_get_variable(efi_char16_t *name,
308 				      efi_guid_t *vendor, u32 *attr,
309 				      unsigned long *data_size,
310 				      void *data)
311 {
312 	struct gsmi_nvram_var_param param = {
313 		.name_ptr = gsmi_dev.name_buf->address,
314 		.data_ptr = gsmi_dev.data_buf->address,
315 		.data_len = (u32)*data_size,
316 	};
317 	efi_status_t ret = EFI_SUCCESS;
318 	unsigned long flags;
319 	size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
320 	int rc;
321 
322 	if (name_len >= GSMI_BUF_SIZE / 2)
323 		return EFI_BAD_BUFFER_SIZE;
324 
325 	spin_lock_irqsave(&gsmi_dev.lock, flags);
326 
327 	/* Vendor guid */
328 	memcpy(&param.guid, vendor, sizeof(param.guid));
329 
330 	/* variable name, already in UTF-16 */
331 	memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
332 	memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
333 
334 	/* data pointer */
335 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
336 
337 	/* parameter buffer */
338 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
339 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
340 
341 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NVRAM_VAR);
342 	if (rc < 0) {
343 		printk(KERN_ERR "gsmi: Get Variable failed\n");
344 		ret = EFI_LOAD_ERROR;
345 	} else if (rc == 1) {
346 		/* variable was not found */
347 		ret = EFI_NOT_FOUND;
348 	} else {
349 		/* Get the arguments back */
350 		memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
351 
352 		/* The size reported is the min of all of our buffers */
353 		*data_size = min_t(unsigned long, *data_size,
354 						gsmi_dev.data_buf->length);
355 		*data_size = min_t(unsigned long, *data_size, param.data_len);
356 
357 		/* Copy data back to return buffer. */
358 		memcpy(data, gsmi_dev.data_buf->start, *data_size);
359 
360 		/* All variables are have the following attributes */
361 		*attr = EFI_VARIABLE_NON_VOLATILE |
362 			EFI_VARIABLE_BOOTSERVICE_ACCESS |
363 			EFI_VARIABLE_RUNTIME_ACCESS;
364 	}
365 
366 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
367 
368 	return ret;
369 }
370 
371 static efi_status_t gsmi_get_next_variable(unsigned long *name_size,
372 					   efi_char16_t *name,
373 					   efi_guid_t *vendor)
374 {
375 	struct gsmi_get_next_var_param param = {
376 		.name_ptr = gsmi_dev.name_buf->address,
377 		.name_len = gsmi_dev.name_buf->length,
378 	};
379 	efi_status_t ret = EFI_SUCCESS;
380 	int rc;
381 	unsigned long flags;
382 
383 	/* For the moment, only support buffers that exactly match in size */
384 	if (*name_size != GSMI_BUF_SIZE)
385 		return EFI_BAD_BUFFER_SIZE;
386 
387 	/* Let's make sure the thing is at least null-terminated */
388 	if (ucs2_strnlen(name, GSMI_BUF_SIZE / 2) == GSMI_BUF_SIZE / 2)
389 		return EFI_INVALID_PARAMETER;
390 
391 	spin_lock_irqsave(&gsmi_dev.lock, flags);
392 
393 	/* guid */
394 	memcpy(&param.guid, vendor, sizeof(param.guid));
395 
396 	/* variable name, already in UTF-16 */
397 	memcpy(gsmi_dev.name_buf->start, name, *name_size);
398 
399 	/* parameter buffer */
400 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
401 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
402 
403 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_GET_NEXT_VAR);
404 	if (rc < 0) {
405 		printk(KERN_ERR "gsmi: Get Next Variable Name failed\n");
406 		ret = EFI_LOAD_ERROR;
407 	} else if (rc == 1) {
408 		/* variable not found -- end of list */
409 		ret = EFI_NOT_FOUND;
410 	} else {
411 		/* copy variable data back to return buffer */
412 		memcpy(&param, gsmi_dev.param_buf->start, sizeof(param));
413 
414 		/* Copy the name back */
415 		memcpy(name, gsmi_dev.name_buf->start, GSMI_BUF_SIZE);
416 		*name_size = ucs2_strnlen(name, GSMI_BUF_SIZE / 2) * 2;
417 
418 		/* copy guid to return buffer */
419 		memcpy(vendor, &param.guid, sizeof(param.guid));
420 		ret = EFI_SUCCESS;
421 	}
422 
423 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
424 
425 	return ret;
426 }
427 
428 static efi_status_t gsmi_set_variable(efi_char16_t *name,
429 				      efi_guid_t *vendor,
430 				      u32 attr,
431 				      unsigned long data_size,
432 				      void *data)
433 {
434 	struct gsmi_nvram_var_param param = {
435 		.name_ptr = gsmi_dev.name_buf->address,
436 		.data_ptr = gsmi_dev.data_buf->address,
437 		.data_len = (u32)data_size,
438 		.attributes = EFI_VARIABLE_NON_VOLATILE |
439 			      EFI_VARIABLE_BOOTSERVICE_ACCESS |
440 			      EFI_VARIABLE_RUNTIME_ACCESS,
441 	};
442 	size_t name_len = ucs2_strnlen(name, GSMI_BUF_SIZE / 2);
443 	efi_status_t ret = EFI_SUCCESS;
444 	int rc;
445 	unsigned long flags;
446 
447 	if (name_len >= GSMI_BUF_SIZE / 2)
448 		return EFI_BAD_BUFFER_SIZE;
449 
450 	spin_lock_irqsave(&gsmi_dev.lock, flags);
451 
452 	/* guid */
453 	memcpy(&param.guid, vendor, sizeof(param.guid));
454 
455 	/* variable name, already in UTF-16 */
456 	memset(gsmi_dev.name_buf->start, 0, gsmi_dev.name_buf->length);
457 	memcpy(gsmi_dev.name_buf->start, name, name_len * 2);
458 
459 	/* data pointer */
460 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
461 	memcpy(gsmi_dev.data_buf->start, data, data_size);
462 
463 	/* parameter buffer */
464 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
465 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
466 
467 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_NVRAM_VAR);
468 	if (rc < 0) {
469 		printk(KERN_ERR "gsmi: Set Variable failed\n");
470 		ret = EFI_INVALID_PARAMETER;
471 	}
472 
473 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
474 
475 	return ret;
476 }
477 
478 static const struct efivar_operations efivar_ops = {
479 	.get_variable = gsmi_get_variable,
480 	.set_variable = gsmi_set_variable,
481 	.get_next_variable = gsmi_get_next_variable,
482 };
483 
484 #endif /* CONFIG_EFI_VARS */
485 
486 static ssize_t eventlog_write(struct file *filp, struct kobject *kobj,
487 			       struct bin_attribute *bin_attr,
488 			       char *buf, loff_t pos, size_t count)
489 {
490 	struct gsmi_set_eventlog_param param = {
491 		.data_ptr = gsmi_dev.data_buf->address,
492 	};
493 	int rc = 0;
494 	unsigned long flags;
495 
496 	/* Pull the type out */
497 	if (count < sizeof(u32))
498 		return -EINVAL;
499 	param.type = *(u32 *)buf;
500 	buf += sizeof(u32);
501 
502 	/* The remaining buffer is the data payload */
503 	if ((count - sizeof(u32)) > gsmi_dev.data_buf->length)
504 		return -EINVAL;
505 	param.data_len = count - sizeof(u32);
506 
507 	spin_lock_irqsave(&gsmi_dev.lock, flags);
508 
509 	/* data pointer */
510 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
511 	memcpy(gsmi_dev.data_buf->start, buf, param.data_len);
512 
513 	/* parameter buffer */
514 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
515 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
516 
517 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
518 	if (rc < 0)
519 		printk(KERN_ERR "gsmi: Set Event Log failed\n");
520 
521 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
522 
523 	return (rc == 0) ? count : rc;
524 
525 }
526 
527 static struct bin_attribute eventlog_bin_attr = {
528 	.attr = {.name = "append_to_eventlog", .mode = 0200},
529 	.write = eventlog_write,
530 };
531 
532 static ssize_t gsmi_clear_eventlog_store(struct kobject *kobj,
533 					 struct kobj_attribute *attr,
534 					 const char *buf, size_t count)
535 {
536 	int rc;
537 	unsigned long flags;
538 	unsigned long val;
539 	struct {
540 		u32 percentage;
541 		u32 data_type;
542 	} param;
543 
544 	rc = kstrtoul(buf, 0, &val);
545 	if (rc)
546 		return rc;
547 
548 	/*
549 	 * Value entered is a percentage, 0 through 100, anything else
550 	 * is invalid.
551 	 */
552 	if (val > 100)
553 		return -EINVAL;
554 
555 	/* data_type here selects the smbios event log. */
556 	param.percentage = val;
557 	param.data_type = 0;
558 
559 	spin_lock_irqsave(&gsmi_dev.lock, flags);
560 
561 	/* parameter buffer */
562 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
563 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
564 
565 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_EVENT_LOG);
566 
567 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
568 
569 	if (rc)
570 		return rc;
571 	return count;
572 }
573 
574 static struct kobj_attribute gsmi_clear_eventlog_attr = {
575 	.attr = {.name = "clear_eventlog", .mode = 0200},
576 	.store = gsmi_clear_eventlog_store,
577 };
578 
579 static ssize_t gsmi_clear_config_store(struct kobject *kobj,
580 				       struct kobj_attribute *attr,
581 				       const char *buf, size_t count)
582 {
583 	int rc;
584 	unsigned long flags;
585 
586 	spin_lock_irqsave(&gsmi_dev.lock, flags);
587 
588 	/* clear parameter buffer */
589 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
590 
591 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_CLEAR_CONFIG);
592 
593 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
594 
595 	if (rc)
596 		return rc;
597 	return count;
598 }
599 
600 static struct kobj_attribute gsmi_clear_config_attr = {
601 	.attr = {.name = "clear_config", .mode = 0200},
602 	.store = gsmi_clear_config_store,
603 };
604 
605 static const struct attribute *gsmi_attrs[] = {
606 	&gsmi_clear_config_attr.attr,
607 	&gsmi_clear_eventlog_attr.attr,
608 	NULL,
609 };
610 
611 static int gsmi_shutdown_reason(int reason)
612 {
613 	struct gsmi_log_entry_type_1 entry = {
614 		.type     = GSMI_LOG_ENTRY_TYPE_KERNEL,
615 		.instance = reason,
616 	};
617 	struct gsmi_set_eventlog_param param = {
618 		.data_len = sizeof(entry),
619 		.type     = 1,
620 	};
621 	static int saved_reason;
622 	int rc = 0;
623 	unsigned long flags;
624 
625 	/* avoid duplicate entries in the log */
626 	if (saved_reason & (1 << reason))
627 		return 0;
628 
629 	spin_lock_irqsave(&gsmi_dev.lock, flags);
630 
631 	saved_reason |= (1 << reason);
632 
633 	/* data pointer */
634 	memset(gsmi_dev.data_buf->start, 0, gsmi_dev.data_buf->length);
635 	memcpy(gsmi_dev.data_buf->start, &entry, sizeof(entry));
636 
637 	/* parameter buffer */
638 	param.data_ptr = gsmi_dev.data_buf->address;
639 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
640 	memcpy(gsmi_dev.param_buf->start, &param, sizeof(param));
641 
642 	rc = gsmi_exec(GSMI_CALLBACK, GSMI_CMD_SET_EVENT_LOG);
643 
644 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
645 
646 	if (rc < 0)
647 		printk(KERN_ERR "gsmi: Log Shutdown Reason failed\n");
648 	else
649 		printk(KERN_EMERG "gsmi: Log Shutdown Reason 0x%02x\n",
650 		       reason);
651 
652 	return rc;
653 }
654 
655 static int gsmi_reboot_callback(struct notifier_block *nb,
656 				unsigned long reason, void *arg)
657 {
658 	gsmi_shutdown_reason(GSMI_SHUTDOWN_CLEAN);
659 	return NOTIFY_DONE;
660 }
661 
662 static struct notifier_block gsmi_reboot_notifier = {
663 	.notifier_call = gsmi_reboot_callback
664 };
665 
666 static int gsmi_die_callback(struct notifier_block *nb,
667 			     unsigned long reason, void *arg)
668 {
669 	if (reason == DIE_OOPS)
670 		gsmi_shutdown_reason(GSMI_SHUTDOWN_OOPS);
671 	return NOTIFY_DONE;
672 }
673 
674 static struct notifier_block gsmi_die_notifier = {
675 	.notifier_call = gsmi_die_callback
676 };
677 
678 static int gsmi_panic_callback(struct notifier_block *nb,
679 			       unsigned long reason, void *arg)
680 {
681 	gsmi_shutdown_reason(GSMI_SHUTDOWN_PANIC);
682 	return NOTIFY_DONE;
683 }
684 
685 static struct notifier_block gsmi_panic_notifier = {
686 	.notifier_call = gsmi_panic_callback,
687 };
688 
689 /*
690  * This hash function was blatantly copied from include/linux/hash.h.
691  * It is used by this driver to obfuscate a board name that requires a
692  * quirk within this driver.
693  *
694  * Please do not remove this copy of the function as any changes to the
695  * global utility hash_64() function would break this driver's ability
696  * to identify a board and provide the appropriate quirk -- mikew@google.com
697  */
698 static u64 __init local_hash_64(u64 val, unsigned bits)
699 {
700 	u64 hash = val;
701 
702 	/*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
703 	u64 n = hash;
704 	n <<= 18;
705 	hash -= n;
706 	n <<= 33;
707 	hash -= n;
708 	n <<= 3;
709 	hash += n;
710 	n <<= 3;
711 	hash -= n;
712 	n <<= 4;
713 	hash += n;
714 	n <<= 2;
715 	hash += n;
716 
717 	/* High bits are more random, so use them. */
718 	return hash >> (64 - bits);
719 }
720 
721 static u32 __init hash_oem_table_id(char s[8])
722 {
723 	u64 input;
724 	memcpy(&input, s, 8);
725 	return local_hash_64(input, 32);
726 }
727 
728 static const struct dmi_system_id gsmi_dmi_table[] __initconst = {
729 	{
730 		.ident = "Google Board",
731 		.matches = {
732 			DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
733 		},
734 	},
735 	{
736 		.ident = "Coreboot Firmware",
737 		.matches = {
738 			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
739 		},
740 	},
741 	{}
742 };
743 MODULE_DEVICE_TABLE(dmi, gsmi_dmi_table);
744 
745 static __init int gsmi_system_valid(void)
746 {
747 	u32 hash;
748 
749 	if (!dmi_check_system(gsmi_dmi_table))
750 		return -ENODEV;
751 
752 	/*
753 	 * Only newer firmware supports the gsmi interface.  All older
754 	 * firmware that didn't support this interface used to plug the
755 	 * table name in the first four bytes of the oem_table_id field.
756 	 * Newer firmware doesn't do that though, so use that as the
757 	 * discriminant factor.  We have to do this in order to
758 	 * whitewash our board names out of the public driver.
759 	 */
760 	if (!strncmp(acpi_gbl_FADT.header.oem_table_id, "FACP", 4)) {
761 		printk(KERN_INFO "gsmi: Board is too old\n");
762 		return -ENODEV;
763 	}
764 
765 	/* Disable on board with 1.0 BIOS due to Google bug 2602657 */
766 	hash = hash_oem_table_id(acpi_gbl_FADT.header.oem_table_id);
767 	if (hash == QUIRKY_BOARD_HASH) {
768 		const char *bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
769 		if (strncmp(bios_ver, "1.0", 3) == 0) {
770 			pr_info("gsmi: disabled on this board's BIOS %s\n",
771 				bios_ver);
772 			return -ENODEV;
773 		}
774 	}
775 
776 	/* check for valid SMI command port in ACPI FADT */
777 	if (acpi_gbl_FADT.smi_command == 0) {
778 		pr_info("gsmi: missing smi_command\n");
779 		return -ENODEV;
780 	}
781 
782 	/* Found */
783 	return 0;
784 }
785 
786 static struct kobject *gsmi_kobj;
787 
788 static const struct platform_device_info gsmi_dev_info = {
789 	.name		= "gsmi",
790 	.id		= -1,
791 	/* SMI callbacks require 32bit addresses */
792 	.dma_mask	= DMA_BIT_MASK(32),
793 };
794 
795 #ifdef CONFIG_PM
796 static void gsmi_log_s0ix_info(u8 cmd)
797 {
798 	unsigned long flags;
799 
800 	/*
801 	 * If platform has not enabled S0ix logging, then no action is
802 	 * necessary.
803 	 */
804 	if (!s0ix_logging_enable)
805 		return;
806 
807 	spin_lock_irqsave(&gsmi_dev.lock, flags);
808 
809 	memset(gsmi_dev.param_buf->start, 0, gsmi_dev.param_buf->length);
810 
811 	gsmi_exec(GSMI_CALLBACK, cmd);
812 
813 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
814 }
815 
816 static int gsmi_log_s0ix_suspend(struct device *dev)
817 {
818 	/*
819 	 * If system is not suspending via firmware using the standard ACPI Sx
820 	 * types, then make a GSMI call to log the suspend info.
821 	 */
822 	if (!pm_suspend_via_firmware())
823 		gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_SUSPEND);
824 
825 	/*
826 	 * Always return success, since we do not want suspend
827 	 * to fail just because of logging failure.
828 	 */
829 	return 0;
830 }
831 
832 static int gsmi_log_s0ix_resume(struct device *dev)
833 {
834 	/*
835 	 * If system did not resume via firmware, then make a GSMI call to log
836 	 * the resume info and wake source.
837 	 */
838 	if (!pm_resume_via_firmware())
839 		gsmi_log_s0ix_info(GSMI_CMD_LOG_S0IX_RESUME);
840 
841 	/*
842 	 * Always return success, since we do not want resume
843 	 * to fail just because of logging failure.
844 	 */
845 	return 0;
846 }
847 
848 static const struct dev_pm_ops gsmi_pm_ops = {
849 	.suspend_noirq = gsmi_log_s0ix_suspend,
850 	.resume_noirq = gsmi_log_s0ix_resume,
851 };
852 
853 static int gsmi_platform_driver_probe(struct platform_device *dev)
854 {
855 	return 0;
856 }
857 
858 static struct platform_driver gsmi_driver_info = {
859 	.driver = {
860 		.name = "gsmi",
861 		.pm = &gsmi_pm_ops,
862 	},
863 	.probe = gsmi_platform_driver_probe,
864 };
865 #endif
866 
867 static __init int gsmi_init(void)
868 {
869 	unsigned long flags;
870 	int ret;
871 
872 	ret = gsmi_system_valid();
873 	if (ret)
874 		return ret;
875 
876 	gsmi_dev.smi_cmd = acpi_gbl_FADT.smi_command;
877 
878 #ifdef CONFIG_PM
879 	ret = platform_driver_register(&gsmi_driver_info);
880 	if (unlikely(ret)) {
881 		printk(KERN_ERR "gsmi: unable to register platform driver\n");
882 		return ret;
883 	}
884 #endif
885 
886 	/* register device */
887 	gsmi_dev.pdev = platform_device_register_full(&gsmi_dev_info);
888 	if (IS_ERR(gsmi_dev.pdev)) {
889 		printk(KERN_ERR "gsmi: unable to register platform device\n");
890 		return PTR_ERR(gsmi_dev.pdev);
891 	}
892 
893 	/* SMI access needs to be serialized */
894 	spin_lock_init(&gsmi_dev.lock);
895 
896 	ret = -ENOMEM;
897 	gsmi_dev.dma_pool = dma_pool_create("gsmi", &gsmi_dev.pdev->dev,
898 					     GSMI_BUF_SIZE, GSMI_BUF_ALIGN, 0);
899 	if (!gsmi_dev.dma_pool)
900 		goto out_err;
901 
902 	/*
903 	 * pre-allocate buffers because sometimes we are called when
904 	 * this is not feasible: oops, panic, die, mce, etc
905 	 */
906 	gsmi_dev.name_buf = gsmi_buf_alloc();
907 	if (!gsmi_dev.name_buf) {
908 		printk(KERN_ERR "gsmi: failed to allocate name buffer\n");
909 		goto out_err;
910 	}
911 
912 	gsmi_dev.data_buf = gsmi_buf_alloc();
913 	if (!gsmi_dev.data_buf) {
914 		printk(KERN_ERR "gsmi: failed to allocate data buffer\n");
915 		goto out_err;
916 	}
917 
918 	gsmi_dev.param_buf = gsmi_buf_alloc();
919 	if (!gsmi_dev.param_buf) {
920 		printk(KERN_ERR "gsmi: failed to allocate param buffer\n");
921 		goto out_err;
922 	}
923 
924 	/*
925 	 * Determine type of handshake used to serialize the SMI
926 	 * entry. See also gsmi_exec().
927 	 *
928 	 * There's a "behavior" present on some chipsets where writing the
929 	 * SMI trigger register in the southbridge doesn't result in an
930 	 * immediate SMI. Rather, the processor can execute "a few" more
931 	 * instructions before the SMI takes effect. To ensure synchronous
932 	 * behavior, implement a handshake between the kernel driver and the
933 	 * firmware handler to spin until released. This ioctl determines
934 	 * the type of handshake.
935 	 *
936 	 * NONE: The firmware handler does not implement any
937 	 * handshake. Either it doesn't need to, or it's legacy firmware
938 	 * that doesn't know it needs to and never will.
939 	 *
940 	 * CF: The firmware handler will clear the CF in the saved
941 	 * state before returning. The driver may set the CF and test for
942 	 * it to clear before proceeding.
943 	 *
944 	 * SPIN: The firmware handler does not implement any handshake
945 	 * but the driver should spin for a hundred or so microseconds
946 	 * to ensure the SMI has triggered.
947 	 *
948 	 * Finally, the handler will return -ENOSYS if
949 	 * GSMI_CMD_HANDSHAKE_TYPE is unimplemented, which implies
950 	 * HANDSHAKE_NONE.
951 	 */
952 	spin_lock_irqsave(&gsmi_dev.lock, flags);
953 	gsmi_dev.handshake_type = GSMI_HANDSHAKE_SPIN;
954 	gsmi_dev.handshake_type =
955 	    gsmi_exec(GSMI_CALLBACK, GSMI_CMD_HANDSHAKE_TYPE);
956 	if (gsmi_dev.handshake_type == -ENOSYS)
957 		gsmi_dev.handshake_type = GSMI_HANDSHAKE_NONE;
958 	spin_unlock_irqrestore(&gsmi_dev.lock, flags);
959 
960 	/* Remove and clean up gsmi if the handshake could not complete. */
961 	if (gsmi_dev.handshake_type == -ENXIO) {
962 		printk(KERN_INFO "gsmi version " DRIVER_VERSION
963 		       " failed to load\n");
964 		ret = -ENODEV;
965 		goto out_err;
966 	}
967 
968 	/* Register in the firmware directory */
969 	ret = -ENOMEM;
970 	gsmi_kobj = kobject_create_and_add("gsmi", firmware_kobj);
971 	if (!gsmi_kobj) {
972 		printk(KERN_INFO "gsmi: Failed to create firmware kobj\n");
973 		goto out_err;
974 	}
975 
976 	/* Setup eventlog access */
977 	ret = sysfs_create_bin_file(gsmi_kobj, &eventlog_bin_attr);
978 	if (ret) {
979 		printk(KERN_INFO "gsmi: Failed to setup eventlog");
980 		goto out_err;
981 	}
982 
983 	/* Other attributes */
984 	ret = sysfs_create_files(gsmi_kobj, gsmi_attrs);
985 	if (ret) {
986 		printk(KERN_INFO "gsmi: Failed to add attrs");
987 		goto out_remove_bin_file;
988 	}
989 
990 #ifdef CONFIG_EFI_VARS
991 	ret = efivars_register(&efivars, &efivar_ops, gsmi_kobj);
992 	if (ret) {
993 		printk(KERN_INFO "gsmi: Failed to register efivars\n");
994 		sysfs_remove_files(gsmi_kobj, gsmi_attrs);
995 		goto out_remove_bin_file;
996 	}
997 #endif
998 
999 	register_reboot_notifier(&gsmi_reboot_notifier);
1000 	register_die_notifier(&gsmi_die_notifier);
1001 	atomic_notifier_chain_register(&panic_notifier_list,
1002 				       &gsmi_panic_notifier);
1003 
1004 	printk(KERN_INFO "gsmi version " DRIVER_VERSION " loaded\n");
1005 
1006 	return 0;
1007 
1008 out_remove_bin_file:
1009 	sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1010 out_err:
1011 	kobject_put(gsmi_kobj);
1012 	gsmi_buf_free(gsmi_dev.param_buf);
1013 	gsmi_buf_free(gsmi_dev.data_buf);
1014 	gsmi_buf_free(gsmi_dev.name_buf);
1015 	dma_pool_destroy(gsmi_dev.dma_pool);
1016 	platform_device_unregister(gsmi_dev.pdev);
1017 	pr_info("gsmi: failed to load: %d\n", ret);
1018 	return ret;
1019 }
1020 
1021 static void __exit gsmi_exit(void)
1022 {
1023 	unregister_reboot_notifier(&gsmi_reboot_notifier);
1024 	unregister_die_notifier(&gsmi_die_notifier);
1025 	atomic_notifier_chain_unregister(&panic_notifier_list,
1026 					 &gsmi_panic_notifier);
1027 #ifdef CONFIG_EFI_VARS
1028 	efivars_unregister(&efivars);
1029 #endif
1030 
1031 	sysfs_remove_files(gsmi_kobj, gsmi_attrs);
1032 	sysfs_remove_bin_file(gsmi_kobj, &eventlog_bin_attr);
1033 	kobject_put(gsmi_kobj);
1034 	gsmi_buf_free(gsmi_dev.param_buf);
1035 	gsmi_buf_free(gsmi_dev.data_buf);
1036 	gsmi_buf_free(gsmi_dev.name_buf);
1037 	dma_pool_destroy(gsmi_dev.dma_pool);
1038 	platform_device_unregister(gsmi_dev.pdev);
1039 }
1040 
1041 module_init(gsmi_init);
1042 module_exit(gsmi_exit);
1043 
1044 MODULE_AUTHOR("Google, Inc.");
1045 MODULE_LICENSE("GPL");
1046