xref: /openbmc/linux/drivers/char/tpm/tpm_crb.c (revision ae213c44)
1 /*
2  * Copyright (C) 2014 Intel Corporation
3  *
4  * Authors:
5  * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
6  *
7  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8  *
9  * This device driver implements the TPM interface as defined in
10  * the TCG CRB 2.0 TPM specification.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; version 2
15  * of the License.
16  */
17 
18 #include <linux/acpi.h>
19 #include <linux/highmem.h>
20 #include <linux/rculist.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #ifdef CONFIG_ARM64
24 #include <linux/arm-smccc.h>
25 #endif
26 #include "tpm.h"
27 
28 #define ACPI_SIG_TPM2 "TPM2"
29 
30 static const guid_t crb_acpi_start_guid =
31 	GUID_INIT(0x6BBF6CAB, 0x5463, 0x4714,
32 		  0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4);
33 
34 enum crb_defaults {
35 	CRB_ACPI_START_REVISION_ID = 1,
36 	CRB_ACPI_START_INDEX = 1,
37 };
38 
39 enum crb_loc_ctrl {
40 	CRB_LOC_CTRL_REQUEST_ACCESS	= BIT(0),
41 	CRB_LOC_CTRL_RELINQUISH		= BIT(1),
42 };
43 
44 enum crb_loc_state {
45 	CRB_LOC_STATE_LOC_ASSIGNED	= BIT(1),
46 	CRB_LOC_STATE_TPM_REG_VALID_STS	= BIT(7),
47 };
48 
49 enum crb_ctrl_req {
50 	CRB_CTRL_REQ_CMD_READY	= BIT(0),
51 	CRB_CTRL_REQ_GO_IDLE	= BIT(1),
52 };
53 
54 enum crb_ctrl_sts {
55 	CRB_CTRL_STS_ERROR	= BIT(0),
56 	CRB_CTRL_STS_TPM_IDLE	= BIT(1),
57 };
58 
59 enum crb_start {
60 	CRB_START_INVOKE	= BIT(0),
61 };
62 
63 enum crb_cancel {
64 	CRB_CANCEL_INVOKE	= BIT(0),
65 };
66 
67 struct crb_regs_head {
68 	u32 loc_state;
69 	u32 reserved1;
70 	u32 loc_ctrl;
71 	u32 loc_sts;
72 	u8 reserved2[32];
73 	u64 intf_id;
74 	u64 ctrl_ext;
75 } __packed;
76 
77 struct crb_regs_tail {
78 	u32 ctrl_req;
79 	u32 ctrl_sts;
80 	u32 ctrl_cancel;
81 	u32 ctrl_start;
82 	u32 ctrl_int_enable;
83 	u32 ctrl_int_sts;
84 	u32 ctrl_cmd_size;
85 	u32 ctrl_cmd_pa_low;
86 	u32 ctrl_cmd_pa_high;
87 	u32 ctrl_rsp_size;
88 	u64 ctrl_rsp_pa;
89 } __packed;
90 
91 enum crb_status {
92 	CRB_DRV_STS_COMPLETE	= BIT(0),
93 };
94 
95 struct crb_priv {
96 	u32 sm;
97 	const char *hid;
98 	void __iomem *iobase;
99 	struct crb_regs_head __iomem *regs_h;
100 	struct crb_regs_tail __iomem *regs_t;
101 	u8 __iomem *cmd;
102 	u8 __iomem *rsp;
103 	u32 cmd_size;
104 	u32 smc_func_id;
105 };
106 
107 struct tpm2_crb_smc {
108 	u32 interrupt;
109 	u8 interrupt_flags;
110 	u8 op_flags;
111 	u16 reserved2;
112 	u32 smc_func_id;
113 };
114 
115 static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
116 				unsigned long timeout)
117 {
118 	ktime_t start;
119 	ktime_t stop;
120 
121 	start = ktime_get();
122 	stop = ktime_add(start, ms_to_ktime(timeout));
123 
124 	do {
125 		if ((ioread32(reg) & mask) == value)
126 			return true;
127 
128 		usleep_range(50, 100);
129 	} while (ktime_before(ktime_get(), stop));
130 
131 	return ((ioread32(reg) & mask) == value);
132 }
133 
134 /**
135  * __crb_go_idle - request tpm crb device to go the idle state
136  *
137  * @dev:  crb device
138  * @priv: crb private data
139  *
140  * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
141  * The device should respond within TIMEOUT_C by clearing the bit.
142  * Anyhow, we do not wait here as a consequent CMD_READY request
143  * will be handled correctly even if idle was not completed.
144  *
145  * The function does nothing for devices with ACPI-start method
146  * or SMC-start method.
147  *
148  * Return: 0 always
149  */
150 static int __crb_go_idle(struct device *dev, struct crb_priv *priv)
151 {
152 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
153 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
154 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC))
155 		return 0;
156 
157 	iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
158 
159 	if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
160 				 CRB_CTRL_REQ_GO_IDLE/* mask */,
161 				 0, /* value */
162 				 TPM2_TIMEOUT_C)) {
163 		dev_warn(dev, "goIdle timed out\n");
164 		return -ETIME;
165 	}
166 
167 	return 0;
168 }
169 
170 static int crb_go_idle(struct tpm_chip *chip)
171 {
172 	struct device *dev = &chip->dev;
173 	struct crb_priv *priv = dev_get_drvdata(dev);
174 
175 	return __crb_go_idle(dev, priv);
176 }
177 
178 /**
179  * __crb_cmd_ready - request tpm crb device to enter ready state
180  *
181  * @dev:  crb device
182  * @priv: crb private data
183  *
184  * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
185  * and poll till the device acknowledge it by clearing the bit.
186  * The device should respond within TIMEOUT_C.
187  *
188  * The function does nothing for devices with ACPI-start method
189  * or SMC-start method.
190  *
191  * Return: 0 on success -ETIME on timeout;
192  */
193 static int __crb_cmd_ready(struct device *dev, struct crb_priv *priv)
194 {
195 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
196 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
197 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC))
198 		return 0;
199 
200 	iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
201 	if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
202 				 CRB_CTRL_REQ_CMD_READY /* mask */,
203 				 0, /* value */
204 				 TPM2_TIMEOUT_C)) {
205 		dev_warn(dev, "cmdReady timed out\n");
206 		return -ETIME;
207 	}
208 
209 	return 0;
210 }
211 
212 static int crb_cmd_ready(struct tpm_chip *chip)
213 {
214 	struct device *dev = &chip->dev;
215 	struct crb_priv *priv = dev_get_drvdata(dev);
216 
217 	return __crb_cmd_ready(dev, priv);
218 }
219 
220 static int __crb_request_locality(struct device *dev,
221 				  struct crb_priv *priv, int loc)
222 {
223 	u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
224 		    CRB_LOC_STATE_TPM_REG_VALID_STS;
225 
226 	if (!priv->regs_h)
227 		return 0;
228 
229 	iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h->loc_ctrl);
230 	if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
231 				 TPM2_TIMEOUT_C)) {
232 		dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
233 		return -ETIME;
234 	}
235 
236 	return 0;
237 }
238 
239 static int crb_request_locality(struct tpm_chip *chip, int loc)
240 {
241 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
242 
243 	return __crb_request_locality(&chip->dev, priv, loc);
244 }
245 
246 static int __crb_relinquish_locality(struct device *dev,
247 				     struct crb_priv *priv, int loc)
248 {
249 	u32 mask = CRB_LOC_STATE_LOC_ASSIGNED |
250 		   CRB_LOC_STATE_TPM_REG_VALID_STS;
251 	u32 value = CRB_LOC_STATE_TPM_REG_VALID_STS;
252 
253 	if (!priv->regs_h)
254 		return 0;
255 
256 	iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);
257 	if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, mask, value,
258 				 TPM2_TIMEOUT_C)) {
259 		dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
260 		return -ETIME;
261 	}
262 
263 	return 0;
264 }
265 
266 static int crb_relinquish_locality(struct tpm_chip *chip, int loc)
267 {
268 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
269 
270 	return __crb_relinquish_locality(&chip->dev, priv, loc);
271 }
272 
273 static u8 crb_status(struct tpm_chip *chip)
274 {
275 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
276 	u8 sts = 0;
277 
278 	if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
279 	    CRB_START_INVOKE)
280 		sts |= CRB_DRV_STS_COMPLETE;
281 
282 	return sts;
283 }
284 
285 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
286 {
287 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
288 	unsigned int expected;
289 
290 	/* A sanity check that the upper layer wants to get at least the header
291 	 * as that is the minimum size for any TPM response.
292 	 */
293 	if (count < TPM_HEADER_SIZE)
294 		return -EIO;
295 
296 	/* If this bit is set, according to the spec, the TPM is in
297 	 * unrecoverable condition.
298 	 */
299 	if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
300 		return -EIO;
301 
302 	/* Read the first 8 bytes in order to get the length of the response.
303 	 * We read exactly a quad word in order to make sure that the remaining
304 	 * reads will be aligned.
305 	 */
306 	memcpy_fromio(buf, priv->rsp, 8);
307 
308 	expected = be32_to_cpup((__be32 *)&buf[2]);
309 	if (expected > count || expected < TPM_HEADER_SIZE)
310 		return -EIO;
311 
312 	memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8);
313 
314 	return expected;
315 }
316 
317 static int crb_do_acpi_start(struct tpm_chip *chip)
318 {
319 	union acpi_object *obj;
320 	int rc;
321 
322 	obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
323 				&crb_acpi_start_guid,
324 				CRB_ACPI_START_REVISION_ID,
325 				CRB_ACPI_START_INDEX,
326 				NULL);
327 	if (!obj)
328 		return -ENXIO;
329 	rc = obj->integer.value == 0 ? 0 : -ENXIO;
330 	ACPI_FREE(obj);
331 	return rc;
332 }
333 
334 #ifdef CONFIG_ARM64
335 /*
336  * This is a TPM Command Response Buffer start method that invokes a
337  * Secure Monitor Call to requrest the firmware to execute or cancel
338  * a TPM 2.0 command.
339  */
340 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
341 {
342 	struct arm_smccc_res res;
343 
344 	arm_smccc_smc(func_id, 0, 0, 0, 0, 0, 0, 0, &res);
345 	if (res.a0 != 0) {
346 		dev_err(dev,
347 			FW_BUG "tpm_crb_smc_start() returns res.a0 = 0x%lx\n",
348 			res.a0);
349 		return -EIO;
350 	}
351 
352 	return 0;
353 }
354 #else
355 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
356 {
357 	dev_err(dev, FW_BUG "tpm_crb: incorrect start method\n");
358 	return -EINVAL;
359 }
360 #endif
361 
362 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
363 {
364 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
365 	int rc = 0;
366 
367 	/* Zero the cancel register so that the next command will not get
368 	 * canceled.
369 	 */
370 	iowrite32(0, &priv->regs_t->ctrl_cancel);
371 
372 	if (len > priv->cmd_size) {
373 		dev_err(&chip->dev, "invalid command count value %zd %d\n",
374 			len, priv->cmd_size);
375 		return -E2BIG;
376 	}
377 
378 	memcpy_toio(priv->cmd, buf, len);
379 
380 	/* Make sure that cmd is populated before issuing start. */
381 	wmb();
382 
383 	/* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
384 	 * report only ACPI start but in practice seems to require both
385 	 * CRB start, hence invoking CRB start method if hid == MSFT0101.
386 	 */
387 	if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
388 	    (priv->sm == ACPI_TPM2_MEMORY_MAPPED) ||
389 	    (!strcmp(priv->hid, "MSFT0101")))
390 		iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
391 
392 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
393 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD))
394 		rc = crb_do_acpi_start(chip);
395 
396 	if (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
397 		iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
398 		rc = tpm_crb_smc_start(&chip->dev, priv->smc_func_id);
399 	}
400 
401 	return rc;
402 }
403 
404 static void crb_cancel(struct tpm_chip *chip)
405 {
406 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
407 
408 	iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
409 
410 	if (((priv->sm == ACPI_TPM2_START_METHOD) ||
411 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)) &&
412 	     crb_do_acpi_start(chip))
413 		dev_err(&chip->dev, "ACPI Start failed\n");
414 }
415 
416 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
417 {
418 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
419 	u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
420 
421 	return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
422 }
423 
424 static const struct tpm_class_ops tpm_crb = {
425 	.flags = TPM_OPS_AUTO_STARTUP,
426 	.status = crb_status,
427 	.recv = crb_recv,
428 	.send = crb_send,
429 	.cancel = crb_cancel,
430 	.req_canceled = crb_req_canceled,
431 	.go_idle  = crb_go_idle,
432 	.cmd_ready = crb_cmd_ready,
433 	.request_locality = crb_request_locality,
434 	.relinquish_locality = crb_relinquish_locality,
435 	.req_complete_mask = CRB_DRV_STS_COMPLETE,
436 	.req_complete_val = CRB_DRV_STS_COMPLETE,
437 };
438 
439 static int crb_check_resource(struct acpi_resource *ares, void *data)
440 {
441 	struct resource *io_res = data;
442 	struct resource_win win;
443 	struct resource *res = &(win.res);
444 
445 	if (acpi_dev_resource_memory(ares, res) ||
446 	    acpi_dev_resource_address_space(ares, &win)) {
447 		*io_res = *res;
448 		io_res->name = NULL;
449 	}
450 
451 	return 1;
452 }
453 
454 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
455 				 struct resource *io_res, u64 start, u32 size)
456 {
457 	struct resource new_res = {
458 		.start	= start,
459 		.end	= start + size - 1,
460 		.flags	= IORESOURCE_MEM,
461 	};
462 
463 	/* Detect a 64 bit address on a 32 bit system */
464 	if (start != new_res.start)
465 		return (void __iomem *) ERR_PTR(-EINVAL);
466 
467 	if (!resource_contains(io_res, &new_res))
468 		return devm_ioremap_resource(dev, &new_res);
469 
470 	return priv->iobase + (new_res.start - io_res->start);
471 }
472 
473 /*
474  * Work around broken BIOSs that return inconsistent values from the ACPI
475  * region vs the registers. Trust the ACPI region. Such broken systems
476  * probably cannot send large TPM commands since the buffer will be truncated.
477  */
478 static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
479 			      u64 start, u64 size)
480 {
481 	if (io_res->start > start || io_res->end < start)
482 		return size;
483 
484 	if (start + size - 1 <= io_res->end)
485 		return size;
486 
487 	dev_err(dev,
488 		FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
489 		io_res, start, size);
490 
491 	return io_res->end - start + 1;
492 }
493 
494 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
495 		      struct acpi_table_tpm2 *buf)
496 {
497 	struct list_head resources;
498 	struct resource io_res;
499 	struct device *dev = &device->dev;
500 	u32 pa_high, pa_low;
501 	u64 cmd_pa;
502 	u32 cmd_size;
503 	__le64 __rsp_pa;
504 	u64 rsp_pa;
505 	u32 rsp_size;
506 	int ret;
507 
508 	INIT_LIST_HEAD(&resources);
509 	ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
510 				     &io_res);
511 	if (ret < 0)
512 		return ret;
513 	acpi_dev_free_resource_list(&resources);
514 
515 	if (resource_type(&io_res) != IORESOURCE_MEM) {
516 		dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
517 		return -EINVAL;
518 	}
519 
520 	priv->iobase = devm_ioremap_resource(dev, &io_res);
521 	if (IS_ERR(priv->iobase))
522 		return PTR_ERR(priv->iobase);
523 
524 	/* The ACPI IO region starts at the head area and continues to include
525 	 * the control area, as one nice sane region except for some older
526 	 * stuff that puts the control area outside the ACPI IO region.
527 	 */
528 	if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
529 	    (priv->sm == ACPI_TPM2_MEMORY_MAPPED)) {
530 		if (buf->control_address == io_res.start +
531 		    sizeof(*priv->regs_h))
532 			priv->regs_h = priv->iobase;
533 		else
534 			dev_warn(dev, FW_BUG "Bad ACPI memory layout");
535 	}
536 
537 	ret = __crb_request_locality(dev, priv, 0);
538 	if (ret)
539 		return ret;
540 
541 	priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
542 				   sizeof(struct crb_regs_tail));
543 	if (IS_ERR(priv->regs_t)) {
544 		ret = PTR_ERR(priv->regs_t);
545 		goto out_relinquish_locality;
546 	}
547 
548 	/*
549 	 * PTT HW bug w/a: wake up the device to access
550 	 * possibly not retained registers.
551 	 */
552 	ret = __crb_cmd_ready(dev, priv);
553 	if (ret)
554 		goto out_relinquish_locality;
555 
556 	pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
557 	pa_low  = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
558 	cmd_pa = ((u64)pa_high << 32) | pa_low;
559 	cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
560 				      ioread32(&priv->regs_t->ctrl_cmd_size));
561 
562 	dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
563 		pa_high, pa_low, cmd_size);
564 
565 	priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
566 	if (IS_ERR(priv->cmd)) {
567 		ret = PTR_ERR(priv->cmd);
568 		goto out;
569 	}
570 
571 	memcpy_fromio(&__rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
572 	rsp_pa = le64_to_cpu(__rsp_pa);
573 	rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
574 				      ioread32(&priv->regs_t->ctrl_rsp_size));
575 
576 	if (cmd_pa != rsp_pa) {
577 		priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
578 		ret = PTR_ERR_OR_ZERO(priv->rsp);
579 		goto out;
580 	}
581 
582 	/* According to the PTP specification, overlapping command and response
583 	 * buffer sizes must be identical.
584 	 */
585 	if (cmd_size != rsp_size) {
586 		dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
587 		ret = -EINVAL;
588 		goto out;
589 	}
590 
591 	priv->rsp = priv->cmd;
592 
593 out:
594 	if (!ret)
595 		priv->cmd_size = cmd_size;
596 
597 	__crb_go_idle(dev, priv);
598 
599 out_relinquish_locality:
600 
601 	__crb_relinquish_locality(dev, priv, 0);
602 
603 	return ret;
604 }
605 
606 static int crb_acpi_add(struct acpi_device *device)
607 {
608 	struct acpi_table_tpm2 *buf;
609 	struct crb_priv *priv;
610 	struct tpm_chip *chip;
611 	struct device *dev = &device->dev;
612 	struct tpm2_crb_smc *crb_smc;
613 	acpi_status status;
614 	u32 sm;
615 	int rc;
616 
617 	status = acpi_get_table(ACPI_SIG_TPM2, 1,
618 				(struct acpi_table_header **) &buf);
619 	if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
620 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
621 		return -EINVAL;
622 	}
623 
624 	/* Should the FIFO driver handle this? */
625 	sm = buf->start_method;
626 	if (sm == ACPI_TPM2_MEMORY_MAPPED)
627 		return -ENODEV;
628 
629 	priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
630 	if (!priv)
631 		return -ENOMEM;
632 
633 	if (sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
634 		if (buf->header.length < (sizeof(*buf) + sizeof(*crb_smc))) {
635 			dev_err(dev,
636 				FW_BUG "TPM2 ACPI table has wrong size %u for start method type %d\n",
637 				buf->header.length,
638 				ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC);
639 			return -EINVAL;
640 		}
641 		crb_smc = ACPI_ADD_PTR(struct tpm2_crb_smc, buf, sizeof(*buf));
642 		priv->smc_func_id = crb_smc->smc_func_id;
643 	}
644 
645 	priv->sm = sm;
646 	priv->hid = acpi_device_hid(device);
647 
648 	rc = crb_map_io(device, priv, buf);
649 	if (rc)
650 		return rc;
651 
652 	chip = tpmm_chip_alloc(dev, &tpm_crb);
653 	if (IS_ERR(chip))
654 		return PTR_ERR(chip);
655 
656 	dev_set_drvdata(&chip->dev, priv);
657 	chip->acpi_dev_handle = device->handle;
658 	chip->flags = TPM_CHIP_FLAG_TPM2;
659 
660 	return tpm_chip_register(chip);
661 }
662 
663 static int crb_acpi_remove(struct acpi_device *device)
664 {
665 	struct device *dev = &device->dev;
666 	struct tpm_chip *chip = dev_get_drvdata(dev);
667 
668 	tpm_chip_unregister(chip);
669 
670 	return 0;
671 }
672 
673 static const struct dev_pm_ops crb_pm = {
674 	SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume)
675 };
676 
677 static const struct acpi_device_id crb_device_ids[] = {
678 	{"MSFT0101", 0},
679 	{"", 0},
680 };
681 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
682 
683 static struct acpi_driver crb_acpi_driver = {
684 	.name = "tpm_crb",
685 	.ids = crb_device_ids,
686 	.ops = {
687 		.add = crb_acpi_add,
688 		.remove = crb_acpi_remove,
689 	},
690 	.drv = {
691 		.pm = &crb_pm,
692 	},
693 };
694 
695 module_acpi_driver(crb_acpi_driver);
696 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
697 MODULE_DESCRIPTION("TPM2 Driver");
698 MODULE_VERSION("0.1");
699 MODULE_LICENSE("GPL");
700