Linux Audio

Check our new training course

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