2 * Copyright (C) 2014 Intel Corporation
5 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9 * This device driver implements the TPM interface as defined in
10 * the TCG CRB 2.0 TPM specification.
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
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>
24 #include <linux/arm-smccc.h>
28 #define ACPI_SIG_TPM2 "TPM2"
29 #define TPM_CRB_MAX_RESOURCES 3
31 static const guid_t crb_acpi_start_guid =
32 GUID_INIT(0x6BBF6CAB, 0x5463, 0x4714,
33 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4);
36 CRB_ACPI_START_REVISION_ID = 1,
37 CRB_ACPI_START_INDEX = 1,
41 CRB_LOC_CTRL_REQUEST_ACCESS = BIT(0),
42 CRB_LOC_CTRL_RELINQUISH = BIT(1),
46 CRB_LOC_STATE_LOC_ASSIGNED = BIT(1),
47 CRB_LOC_STATE_TPM_REG_VALID_STS = BIT(7),
51 CRB_CTRL_REQ_CMD_READY = BIT(0),
52 CRB_CTRL_REQ_GO_IDLE = BIT(1),
56 CRB_CTRL_STS_ERROR = BIT(0),
57 CRB_CTRL_STS_TPM_IDLE = BIT(1),
61 CRB_START_INVOKE = BIT(0),
65 CRB_CANCEL_INVOKE = BIT(0),
68 struct crb_regs_head {
78 struct crb_regs_tail {
93 CRB_DRV_STS_COMPLETE = BIT(0),
99 struct crb_regs_head __iomem *regs_h;
100 struct crb_regs_tail __iomem *regs_t;
107 struct tpm2_crb_smc {
115 static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
116 unsigned long timeout)
122 stop = ktime_add(start, ms_to_ktime(timeout));
125 if ((ioread32(reg) & mask) == value)
128 usleep_range(50, 100);
129 } while (ktime_before(ktime_get(), stop));
131 return ((ioread32(reg) & mask) == value);
135 * __crb_go_idle - request tpm crb device to go the idle state
138 * @priv: crb private data
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.
145 * The function does nothing for devices with ACPI-start method
146 * or SMC-start method.
150 static int __crb_go_idle(struct device *dev, struct crb_priv *priv)
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))
157 iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
159 if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
160 CRB_CTRL_REQ_GO_IDLE/* mask */,
163 dev_warn(dev, "goIdle timed out\n");
170 static int crb_go_idle(struct tpm_chip *chip)
172 struct device *dev = &chip->dev;
173 struct crb_priv *priv = dev_get_drvdata(dev);
175 return __crb_go_idle(dev, priv);
179 * __crb_cmd_ready - request tpm crb device to enter ready state
182 * @priv: crb private data
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.
188 * The function does nothing for devices with ACPI-start method
189 * or SMC-start method.
191 * Return: 0 on success -ETIME on timeout;
193 static int __crb_cmd_ready(struct device *dev, struct crb_priv *priv)
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))
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 */,
205 dev_warn(dev, "cmdReady timed out\n");
212 static int crb_cmd_ready(struct tpm_chip *chip)
214 struct device *dev = &chip->dev;
215 struct crb_priv *priv = dev_get_drvdata(dev);
217 return __crb_cmd_ready(dev, priv);
220 static int __crb_request_locality(struct device *dev,
221 struct crb_priv *priv, int loc)
223 u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
224 CRB_LOC_STATE_TPM_REG_VALID_STS;
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,
232 dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
239 static int crb_request_locality(struct tpm_chip *chip, int loc)
241 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
243 return __crb_request_locality(&chip->dev, priv, loc);
246 static int __crb_relinquish_locality(struct device *dev,
247 struct crb_priv *priv, int loc)
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;
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,
259 dev_warn(dev, "TPM_LOC_STATE_x.Relinquish timed out\n");
266 static int crb_relinquish_locality(struct tpm_chip *chip, int loc)
268 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
270 return __crb_relinquish_locality(&chip->dev, priv, loc);
273 static u8 crb_status(struct tpm_chip *chip)
275 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
278 if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
280 sts |= CRB_DRV_STS_COMPLETE;
285 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
287 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
288 unsigned int expected;
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.
293 if (count < TPM_HEADER_SIZE)
296 /* If this bit is set, according to the spec, the TPM is in
297 * unrecoverable condition.
299 if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
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.
306 memcpy_fromio(buf, priv->rsp, 8);
308 expected = be32_to_cpup((__be32 *)&buf[2]);
309 if (expected > count || expected < TPM_HEADER_SIZE)
312 memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8);
317 static int crb_do_acpi_start(struct tpm_chip *chip)
319 union acpi_object *obj;
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,
329 rc = obj->integer.value == 0 ? 0 : -ENXIO;
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
340 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
342 struct arm_smccc_res res;
344 arm_smccc_smc(func_id, 0, 0, 0, 0, 0, 0, 0, &res);
347 FW_BUG "tpm_crb_smc_start() returns res.a0 = 0x%lx\n",
355 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
357 dev_err(dev, FW_BUG "tpm_crb: incorrect start method\n");
362 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
364 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
367 /* Zero the cancel register so that the next command will not get
370 iowrite32(0, &priv->regs_t->ctrl_cancel);
372 if (len > priv->cmd_size) {
373 dev_err(&chip->dev, "invalid command count value %zd %d\n",
374 len, priv->cmd_size);
378 memcpy_toio(priv->cmd, buf, len);
380 /* Make sure that cmd is populated before issuing start. */
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.
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);
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);
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);
404 static void crb_cancel(struct tpm_chip *chip)
406 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
408 iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
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");
416 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
418 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
419 u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
421 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
424 static const struct tpm_class_ops tpm_crb = {
425 .flags = TPM_OPS_AUTO_STARTUP,
426 .status = crb_status,
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,
439 static int crb_check_resource(struct acpi_resource *ares, void *data)
441 struct resource *iores_array = data;
442 struct resource_win win;
443 struct resource *res = &(win.res);
446 if (acpi_dev_resource_memory(ares, res) ||
447 acpi_dev_resource_address_space(ares, &win)) {
448 for (i = 0; i < TPM_CRB_MAX_RESOURCES + 1; ++i) {
449 if (resource_type(iores_array + i) != IORESOURCE_MEM) {
450 iores_array[i] = *res;
451 iores_array[i].name = NULL;
460 static void __iomem *crb_map_res(struct device *dev, struct resource *iores,
461 void __iomem **iobase_ptr, u64 start, u32 size)
463 struct resource new_res = {
465 .end = start + size - 1,
466 .flags = IORESOURCE_MEM,
469 /* Detect a 64 bit address on a 32 bit system */
470 if (start != new_res.start)
471 return (void __iomem *) ERR_PTR(-EINVAL);
474 return devm_ioremap_resource(dev, &new_res);
477 *iobase_ptr = devm_ioremap_resource(dev, iores);
478 if (IS_ERR(*iobase_ptr))
482 return *iobase_ptr + (new_res.start - iores->start);
486 * Work around broken BIOSs that return inconsistent values from the ACPI
487 * region vs the registers. Trust the ACPI region. Such broken systems
488 * probably cannot send large TPM commands since the buffer will be truncated.
490 static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
493 if (io_res->start > start || io_res->end < start)
496 if (start + size - 1 <= io_res->end)
500 FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
501 io_res, start, size);
503 return io_res->end - start + 1;
506 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
507 struct acpi_table_tpm2 *buf)
509 struct list_head acpi_resource_list;
510 struct resource iores_array[TPM_CRB_MAX_RESOURCES + 1] = { {0} };
511 void __iomem *iobase_array[TPM_CRB_MAX_RESOURCES] = {NULL};
512 struct device *dev = &device->dev;
513 struct resource *iores;
514 void __iomem **iobase_ptr;
524 INIT_LIST_HEAD(&acpi_resource_list);
525 ret = acpi_dev_get_resources(device, &acpi_resource_list,
526 crb_check_resource, iores_array);
529 acpi_dev_free_resource_list(&acpi_resource_list);
531 if (resource_type(iores_array) != IORESOURCE_MEM) {
532 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
534 } else if (resource_type(iores_array + TPM_CRB_MAX_RESOURCES) ==
536 dev_warn(dev, "TPM2 ACPI table defines too many memory resources\n");
537 memset(iores_array + TPM_CRB_MAX_RESOURCES,
538 0, sizeof(*iores_array));
539 iores_array[TPM_CRB_MAX_RESOURCES].flags = 0;
544 for (i = 0; resource_type(iores_array + i) == IORESOURCE_MEM; ++i) {
545 if (buf->control_address >= iores_array[i].start &&
546 buf->control_address + sizeof(struct crb_regs_tail) - 1 <=
547 iores_array[i].end) {
548 iores = iores_array + i;
549 iobase_ptr = iobase_array + i;
554 priv->regs_t = crb_map_res(dev, iores, iobase_ptr, buf->control_address,
555 sizeof(struct crb_regs_tail));
557 if (IS_ERR(priv->regs_t))
558 return PTR_ERR(priv->regs_t);
560 /* The ACPI IO region starts at the head area and continues to include
561 * the control area, as one nice sane region except for some older
562 * stuff that puts the control area outside the ACPI IO region.
564 if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
565 (priv->sm == ACPI_TPM2_MEMORY_MAPPED)) {
567 buf->control_address == iores->start +
568 sizeof(*priv->regs_h))
569 priv->regs_h = *iobase_ptr;
571 dev_warn(dev, FW_BUG "Bad ACPI memory layout");
574 ret = __crb_request_locality(dev, priv, 0);
579 * PTT HW bug w/a: wake up the device to access
580 * possibly not retained registers.
582 ret = __crb_cmd_ready(dev, priv);
584 goto out_relinquish_locality;
586 pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
587 pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
588 cmd_pa = ((u64)pa_high << 32) | pa_low;
589 cmd_size = ioread32(&priv->regs_t->ctrl_cmd_size);
593 for (i = 0; iores_array[i].end; ++i) {
594 if (cmd_pa >= iores_array[i].start &&
595 cmd_pa <= iores_array[i].end) {
596 iores = iores_array + i;
597 iobase_ptr = iobase_array + i;
603 cmd_size = crb_fixup_cmd_size(dev, iores, cmd_pa, cmd_size);
605 dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
606 pa_high, pa_low, cmd_size);
608 priv->cmd = crb_map_res(dev, iores, iobase_ptr, cmd_pa, cmd_size);
609 if (IS_ERR(priv->cmd)) {
610 ret = PTR_ERR(priv->cmd);
614 memcpy_fromio(&__rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
615 rsp_pa = le64_to_cpu(__rsp_pa);
616 rsp_size = ioread32(&priv->regs_t->ctrl_rsp_size);
620 for (i = 0; resource_type(iores_array + i) == IORESOURCE_MEM; ++i) {
621 if (rsp_pa >= iores_array[i].start &&
622 rsp_pa <= iores_array[i].end) {
623 iores = iores_array + i;
624 iobase_ptr = iobase_array + i;
630 rsp_size = crb_fixup_cmd_size(dev, iores, rsp_pa, rsp_size);
632 if (cmd_pa != rsp_pa) {
633 priv->rsp = crb_map_res(dev, iores, iobase_ptr,
635 ret = PTR_ERR_OR_ZERO(priv->rsp);
639 /* According to the PTP specification, overlapping command and response
640 * buffer sizes must be identical.
642 if (cmd_size != rsp_size) {
643 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
648 priv->rsp = priv->cmd;
652 priv->cmd_size = cmd_size;
654 __crb_go_idle(dev, priv);
656 out_relinquish_locality:
658 __crb_relinquish_locality(dev, priv, 0);
663 static int crb_acpi_add(struct acpi_device *device)
665 struct acpi_table_tpm2 *buf;
666 struct crb_priv *priv;
667 struct tpm_chip *chip;
668 struct device *dev = &device->dev;
669 struct tpm2_crb_smc *crb_smc;
674 status = acpi_get_table(ACPI_SIG_TPM2, 1,
675 (struct acpi_table_header **) &buf);
676 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
677 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
681 /* Should the FIFO driver handle this? */
682 sm = buf->start_method;
683 if (sm == ACPI_TPM2_MEMORY_MAPPED) {
688 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
694 if (sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
695 if (buf->header.length < (sizeof(*buf) + sizeof(*crb_smc))) {
697 FW_BUG "TPM2 ACPI table has wrong size %u for start method type %d\n",
699 ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC);
703 crb_smc = ACPI_ADD_PTR(struct tpm2_crb_smc, buf, sizeof(*buf));
704 priv->smc_func_id = crb_smc->smc_func_id;
708 priv->hid = acpi_device_hid(device);
710 rc = crb_map_io(device, priv, buf);
714 chip = tpmm_chip_alloc(dev, &tpm_crb);
720 dev_set_drvdata(&chip->dev, priv);
721 chip->acpi_dev_handle = device->handle;
722 chip->flags = TPM_CHIP_FLAG_TPM2;
724 rc = tpm_chip_register(chip);
727 acpi_put_table((struct acpi_table_header *)buf);
731 static int crb_acpi_remove(struct acpi_device *device)
733 struct device *dev = &device->dev;
734 struct tpm_chip *chip = dev_get_drvdata(dev);
736 tpm_chip_unregister(chip);
741 static const struct dev_pm_ops crb_pm = {
742 SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume)
745 static const struct acpi_device_id crb_device_ids[] = {
749 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
751 static struct acpi_driver crb_acpi_driver = {
753 .ids = crb_device_ids,
756 .remove = crb_acpi_remove,
763 module_acpi_driver(crb_acpi_driver);
764 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
765 MODULE_DESCRIPTION("TPM2 Driver");
766 MODULE_VERSION("0.1");
767 MODULE_LICENSE("GPL");