xref: /openbmc/linux/drivers/char/tpm/tpm_crb.c (revision 2d972b6a)
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 	return 0;
167 }
168 
169 /**
170  * crb_cmd_ready - request tpm crb device to enter ready state
171  *
172  * @dev:  crb device
173  * @priv: crb private data
174  *
175  * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
176  * and poll till the device acknowledge it by clearing the bit.
177  * The device should respond within TIMEOUT_C.
178  *
179  * The function does nothing for devices with ACPI-start method
180  * or SMC-start method.
181  *
182  * Return: 0 on success -ETIME on timeout;
183  */
184 static int crb_cmd_ready(struct device *dev, struct crb_priv *priv)
185 {
186 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
187 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
188 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC))
189 		return 0;
190 
191 	iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
192 	if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
193 				 CRB_CTRL_REQ_CMD_READY /* mask */,
194 				 0, /* value */
195 				 TPM2_TIMEOUT_C)) {
196 		dev_warn(dev, "cmdReady timed out\n");
197 		return -ETIME;
198 	}
199 
200 	return 0;
201 }
202 
203 static int __crb_request_locality(struct device *dev,
204 				  struct crb_priv *priv, int loc)
205 {
206 	u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
207 		    CRB_LOC_STATE_TPM_REG_VALID_STS;
208 
209 	if (!priv->regs_h)
210 		return 0;
211 
212 	iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h->loc_ctrl);
213 	if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
214 				 TPM2_TIMEOUT_C)) {
215 		dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
216 		return -ETIME;
217 	}
218 
219 	return 0;
220 }
221 
222 static int crb_request_locality(struct tpm_chip *chip, int loc)
223 {
224 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
225 
226 	return __crb_request_locality(&chip->dev, priv, loc);
227 }
228 
229 static int __crb_relinquish_locality(struct device *dev,
230 				     struct crb_priv *priv, int loc)
231 {
232 	u32 mask = CRB_LOC_STATE_LOC_ASSIGNED |
233 		   CRB_LOC_STATE_TPM_REG_VALID_STS;
234 	u32 value = CRB_LOC_STATE_TPM_REG_VALID_STS;
235 
236 	if (!priv->regs_h)
237 		return 0;
238 
239 	iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);
240 	if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, mask, value,
241 				 TPM2_TIMEOUT_C)) {
242 		dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
243 		return -ETIME;
244 	}
245 
246 	return 0;
247 }
248 
249 static int crb_relinquish_locality(struct tpm_chip *chip, int loc)
250 {
251 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
252 
253 	return __crb_relinquish_locality(&chip->dev, priv, loc);
254 }
255 
256 static u8 crb_status(struct tpm_chip *chip)
257 {
258 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
259 	u8 sts = 0;
260 
261 	if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
262 	    CRB_START_INVOKE)
263 		sts |= CRB_DRV_STS_COMPLETE;
264 
265 	return sts;
266 }
267 
268 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
269 {
270 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
271 	unsigned int expected;
272 
273 	/* sanity check */
274 	if (count < 6)
275 		return -EIO;
276 
277 	if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
278 		return -EIO;
279 
280 	memcpy_fromio(buf, priv->rsp, 6);
281 	expected = be32_to_cpup((__be32 *) &buf[2]);
282 	if (expected > count || expected < 6)
283 		return -EIO;
284 
285 	memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
286 
287 	return expected;
288 }
289 
290 static int crb_do_acpi_start(struct tpm_chip *chip)
291 {
292 	union acpi_object *obj;
293 	int rc;
294 
295 	obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
296 				&crb_acpi_start_guid,
297 				CRB_ACPI_START_REVISION_ID,
298 				CRB_ACPI_START_INDEX,
299 				NULL);
300 	if (!obj)
301 		return -ENXIO;
302 	rc = obj->integer.value == 0 ? 0 : -ENXIO;
303 	ACPI_FREE(obj);
304 	return rc;
305 }
306 
307 #ifdef CONFIG_ARM64
308 /*
309  * This is a TPM Command Response Buffer start method that invokes a
310  * Secure Monitor Call to requrest the firmware to execute or cancel
311  * a TPM 2.0 command.
312  */
313 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
314 {
315 	struct arm_smccc_res res;
316 
317 	arm_smccc_smc(func_id, 0, 0, 0, 0, 0, 0, 0, &res);
318 	if (res.a0 != 0) {
319 		dev_err(dev,
320 			FW_BUG "tpm_crb_smc_start() returns res.a0 = 0x%lx\n",
321 			res.a0);
322 		return -EIO;
323 	}
324 
325 	return 0;
326 }
327 #else
328 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
329 {
330 	dev_err(dev, FW_BUG "tpm_crb: incorrect start method\n");
331 	return -EINVAL;
332 }
333 #endif
334 
335 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
336 {
337 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
338 	int rc = 0;
339 
340 	/* Zero the cancel register so that the next command will not get
341 	 * canceled.
342 	 */
343 	iowrite32(0, &priv->regs_t->ctrl_cancel);
344 
345 	if (len > priv->cmd_size) {
346 		dev_err(&chip->dev, "invalid command count value %zd %d\n",
347 			len, priv->cmd_size);
348 		return -E2BIG;
349 	}
350 
351 	memcpy_toio(priv->cmd, buf, len);
352 
353 	/* Make sure that cmd is populated before issuing start. */
354 	wmb();
355 
356 	/* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
357 	 * report only ACPI start but in practice seems to require both
358 	 * CRB start, hence invoking CRB start method if hid == MSFT0101.
359 	 */
360 	if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
361 	    (priv->sm == ACPI_TPM2_MEMORY_MAPPED) ||
362 	    (!strcmp(priv->hid, "MSFT0101")))
363 		iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
364 
365 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
366 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD))
367 		rc = crb_do_acpi_start(chip);
368 
369 	if (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
370 		iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
371 		rc = tpm_crb_smc_start(&chip->dev, priv->smc_func_id);
372 	}
373 
374 	return rc;
375 }
376 
377 static void crb_cancel(struct tpm_chip *chip)
378 {
379 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
380 
381 	iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
382 
383 	if (((priv->sm == ACPI_TPM2_START_METHOD) ||
384 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)) &&
385 	     crb_do_acpi_start(chip))
386 		dev_err(&chip->dev, "ACPI Start failed\n");
387 }
388 
389 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
390 {
391 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
392 	u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
393 
394 	return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
395 }
396 
397 static const struct tpm_class_ops tpm_crb = {
398 	.flags = TPM_OPS_AUTO_STARTUP,
399 	.status = crb_status,
400 	.recv = crb_recv,
401 	.send = crb_send,
402 	.cancel = crb_cancel,
403 	.req_canceled = crb_req_canceled,
404 	.request_locality = crb_request_locality,
405 	.relinquish_locality = crb_relinquish_locality,
406 	.req_complete_mask = CRB_DRV_STS_COMPLETE,
407 	.req_complete_val = CRB_DRV_STS_COMPLETE,
408 };
409 
410 static int crb_check_resource(struct acpi_resource *ares, void *data)
411 {
412 	struct resource *io_res = data;
413 	struct resource_win win;
414 	struct resource *res = &(win.res);
415 
416 	if (acpi_dev_resource_memory(ares, res) ||
417 	    acpi_dev_resource_address_space(ares, &win)) {
418 		*io_res = *res;
419 		io_res->name = NULL;
420 	}
421 
422 	return 1;
423 }
424 
425 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
426 				 struct resource *io_res, u64 start, u32 size)
427 {
428 	struct resource new_res = {
429 		.start	= start,
430 		.end	= start + size - 1,
431 		.flags	= IORESOURCE_MEM,
432 	};
433 
434 	/* Detect a 64 bit address on a 32 bit system */
435 	if (start != new_res.start)
436 		return (void __iomem *) ERR_PTR(-EINVAL);
437 
438 	if (!resource_contains(io_res, &new_res))
439 		return devm_ioremap_resource(dev, &new_res);
440 
441 	return priv->iobase + (new_res.start - io_res->start);
442 }
443 
444 /*
445  * Work around broken BIOSs that return inconsistent values from the ACPI
446  * region vs the registers. Trust the ACPI region. Such broken systems
447  * probably cannot send large TPM commands since the buffer will be truncated.
448  */
449 static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
450 			      u64 start, u64 size)
451 {
452 	if (io_res->start > start || io_res->end < start)
453 		return size;
454 
455 	if (start + size - 1 <= io_res->end)
456 		return size;
457 
458 	dev_err(dev,
459 		FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
460 		io_res, start, size);
461 
462 	return io_res->end - start + 1;
463 }
464 
465 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
466 		      struct acpi_table_tpm2 *buf)
467 {
468 	struct list_head resources;
469 	struct resource io_res;
470 	struct device *dev = &device->dev;
471 	u32 pa_high, pa_low;
472 	u64 cmd_pa;
473 	u32 cmd_size;
474 	__le64 __rsp_pa;
475 	u64 rsp_pa;
476 	u32 rsp_size;
477 	int ret;
478 
479 	INIT_LIST_HEAD(&resources);
480 	ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
481 				     &io_res);
482 	if (ret < 0)
483 		return ret;
484 	acpi_dev_free_resource_list(&resources);
485 
486 	if (resource_type(&io_res) != IORESOURCE_MEM) {
487 		dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
488 		return -EINVAL;
489 	}
490 
491 	priv->iobase = devm_ioremap_resource(dev, &io_res);
492 	if (IS_ERR(priv->iobase))
493 		return PTR_ERR(priv->iobase);
494 
495 	/* The ACPI IO region starts at the head area and continues to include
496 	 * the control area, as one nice sane region except for some older
497 	 * stuff that puts the control area outside the ACPI IO region.
498 	 */
499 	if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
500 	    (priv->sm == ACPI_TPM2_MEMORY_MAPPED)) {
501 		if (buf->control_address == io_res.start +
502 		    sizeof(*priv->regs_h))
503 			priv->regs_h = priv->iobase;
504 		else
505 			dev_warn(dev, FW_BUG "Bad ACPI memory layout");
506 	}
507 
508 	ret = __crb_request_locality(dev, priv, 0);
509 	if (ret)
510 		return ret;
511 
512 	priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
513 				   sizeof(struct crb_regs_tail));
514 	if (IS_ERR(priv->regs_t))
515 		return PTR_ERR(priv->regs_t);
516 
517 	/*
518 	 * PTT HW bug w/a: wake up the device to access
519 	 * possibly not retained registers.
520 	 */
521 	ret = crb_cmd_ready(dev, priv);
522 	if (ret)
523 		return ret;
524 
525 	pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
526 	pa_low  = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
527 	cmd_pa = ((u64)pa_high << 32) | pa_low;
528 	cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
529 				      ioread32(&priv->regs_t->ctrl_cmd_size));
530 
531 	dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
532 		pa_high, pa_low, cmd_size);
533 
534 	priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
535 	if (IS_ERR(priv->cmd)) {
536 		ret = PTR_ERR(priv->cmd);
537 		goto out;
538 	}
539 
540 	memcpy_fromio(&__rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
541 	rsp_pa = le64_to_cpu(__rsp_pa);
542 	rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
543 				      ioread32(&priv->regs_t->ctrl_rsp_size));
544 
545 	if (cmd_pa != rsp_pa) {
546 		priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
547 		ret = PTR_ERR_OR_ZERO(priv->rsp);
548 		goto out;
549 	}
550 
551 	/* According to the PTP specification, overlapping command and response
552 	 * buffer sizes must be identical.
553 	 */
554 	if (cmd_size != rsp_size) {
555 		dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
556 		ret = -EINVAL;
557 		goto out;
558 	}
559 
560 	priv->rsp = priv->cmd;
561 
562 out:
563 	if (!ret)
564 		priv->cmd_size = cmd_size;
565 
566 	crb_go_idle(dev, priv);
567 
568 	__crb_relinquish_locality(dev, priv, 0);
569 
570 	return ret;
571 }
572 
573 static int crb_acpi_add(struct acpi_device *device)
574 {
575 	struct acpi_table_tpm2 *buf;
576 	struct crb_priv *priv;
577 	struct tpm_chip *chip;
578 	struct device *dev = &device->dev;
579 	struct tpm2_crb_smc *crb_smc;
580 	acpi_status status;
581 	u32 sm;
582 	int rc;
583 
584 	status = acpi_get_table(ACPI_SIG_TPM2, 1,
585 				(struct acpi_table_header **) &buf);
586 	if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
587 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
588 		return -EINVAL;
589 	}
590 
591 	/* Should the FIFO driver handle this? */
592 	sm = buf->start_method;
593 	if (sm == ACPI_TPM2_MEMORY_MAPPED)
594 		return -ENODEV;
595 
596 	priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
597 	if (!priv)
598 		return -ENOMEM;
599 
600 	if (sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
601 		if (buf->header.length < (sizeof(*buf) + sizeof(*crb_smc))) {
602 			dev_err(dev,
603 				FW_BUG "TPM2 ACPI table has wrong size %u for start method type %d\n",
604 				buf->header.length,
605 				ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC);
606 			return -EINVAL;
607 		}
608 		crb_smc = ACPI_ADD_PTR(struct tpm2_crb_smc, buf, sizeof(*buf));
609 		priv->smc_func_id = crb_smc->smc_func_id;
610 	}
611 
612 	priv->sm = sm;
613 	priv->hid = acpi_device_hid(device);
614 
615 	rc = crb_map_io(device, priv, buf);
616 	if (rc)
617 		return rc;
618 
619 	chip = tpmm_chip_alloc(dev, &tpm_crb);
620 	if (IS_ERR(chip))
621 		return PTR_ERR(chip);
622 
623 	dev_set_drvdata(&chip->dev, priv);
624 	chip->acpi_dev_handle = device->handle;
625 	chip->flags = TPM_CHIP_FLAG_TPM2;
626 
627 	rc = __crb_request_locality(dev, priv, 0);
628 	if (rc)
629 		return rc;
630 
631 	rc  = crb_cmd_ready(dev, priv);
632 	if (rc)
633 		goto out;
634 
635 	pm_runtime_get_noresume(dev);
636 	pm_runtime_set_active(dev);
637 	pm_runtime_enable(dev);
638 
639 	rc = tpm_chip_register(chip);
640 	if (rc) {
641 		crb_go_idle(dev, priv);
642 		pm_runtime_put_noidle(dev);
643 		pm_runtime_disable(dev);
644 		goto out;
645 	}
646 
647 	pm_runtime_put_sync(dev);
648 
649 out:
650 	__crb_relinquish_locality(dev, priv, 0);
651 
652 	return rc;
653 }
654 
655 static int crb_acpi_remove(struct acpi_device *device)
656 {
657 	struct device *dev = &device->dev;
658 	struct tpm_chip *chip = dev_get_drvdata(dev);
659 
660 	tpm_chip_unregister(chip);
661 
662 	pm_runtime_disable(dev);
663 
664 	return 0;
665 }
666 
667 static int __maybe_unused crb_pm_runtime_suspend(struct device *dev)
668 {
669 	struct tpm_chip *chip = dev_get_drvdata(dev);
670 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
671 
672 	return crb_go_idle(dev, priv);
673 }
674 
675 static int __maybe_unused crb_pm_runtime_resume(struct device *dev)
676 {
677 	struct tpm_chip *chip = dev_get_drvdata(dev);
678 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
679 
680 	return crb_cmd_ready(dev, priv);
681 }
682 
683 static int __maybe_unused crb_pm_suspend(struct device *dev)
684 {
685 	int ret;
686 
687 	ret = tpm_pm_suspend(dev);
688 	if (ret)
689 		return ret;
690 
691 	return crb_pm_runtime_suspend(dev);
692 }
693 
694 static int __maybe_unused crb_pm_resume(struct device *dev)
695 {
696 	int ret;
697 
698 	ret = crb_pm_runtime_resume(dev);
699 	if (ret)
700 		return ret;
701 
702 	return tpm_pm_resume(dev);
703 }
704 
705 static const struct dev_pm_ops crb_pm = {
706 	SET_SYSTEM_SLEEP_PM_OPS(crb_pm_suspend, crb_pm_resume)
707 	SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
708 };
709 
710 static const struct acpi_device_id crb_device_ids[] = {
711 	{"MSFT0101", 0},
712 	{"", 0},
713 };
714 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
715 
716 static struct acpi_driver crb_acpi_driver = {
717 	.name = "tpm_crb",
718 	.ids = crb_device_ids,
719 	.ops = {
720 		.add = crb_acpi_add,
721 		.remove = crb_acpi_remove,
722 	},
723 	.drv = {
724 		.pm = &crb_pm,
725 	},
726 };
727 
728 module_acpi_driver(crb_acpi_driver);
729 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
730 MODULE_DESCRIPTION("TPM2 Driver");
731 MODULE_VERSION("0.1");
732 MODULE_LICENSE("GPL");
733