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"
30 static const guid_t crb_acpi_start_guid =
31 GUID_INIT(0x6BBF6CAB, 0x5463, 0x4714,
32 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4);
35 CRB_ACPI_START_REVISION_ID = 1,
36 CRB_ACPI_START_INDEX = 1,
40 CRB_LOC_CTRL_REQUEST_ACCESS = BIT(0),
41 CRB_LOC_CTRL_RELINQUISH = BIT(1),
45 CRB_LOC_STATE_LOC_ASSIGNED = BIT(1),
46 CRB_LOC_STATE_TPM_REG_VALID_STS = BIT(7),
50 CRB_CTRL_REQ_CMD_READY = BIT(0),
51 CRB_CTRL_REQ_GO_IDLE = BIT(1),
55 CRB_CTRL_STS_ERROR = BIT(0),
56 CRB_CTRL_STS_TPM_IDLE = BIT(1),
60 CRB_START_INVOKE = BIT(0),
64 CRB_CANCEL_INVOKE = BIT(0),
67 struct crb_regs_head {
77 struct crb_regs_tail {
92 CRB_DRV_STS_COMPLETE = BIT(0),
96 CRB_FL_ACPI_START = BIT(0),
97 CRB_FL_CRB_START = BIT(1),
98 CRB_FL_CRB_SMC_START = BIT(2),
103 void __iomem *iobase;
104 struct crb_regs_head __iomem *regs_h;
105 struct crb_regs_tail __iomem *regs_t;
112 struct tpm2_crb_smc {
120 static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
121 unsigned long timeout)
127 stop = ktime_add(start, ms_to_ktime(timeout));
130 if ((ioread32(reg) & mask) == value)
133 usleep_range(50, 100);
134 } while (ktime_before(ktime_get(), stop));
136 return ((ioread32(reg) & mask) == value);
140 * __crb_go_idle - request tpm crb device to go the idle state
143 * @priv: crb private data
145 * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
146 * The device should respond within TIMEOUT_C by clearing the bit.
147 * Anyhow, we do not wait here as a consequent CMD_READY request
148 * will be handled correctly even if idle was not completed.
150 * The function does nothing for devices with ACPI-start method.
154 static int __crb_go_idle(struct device *dev, struct crb_priv *priv)
156 if ((priv->flags & CRB_FL_ACPI_START) ||
157 (priv->flags & CRB_FL_CRB_SMC_START))
160 iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
162 if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
163 CRB_CTRL_REQ_GO_IDLE/* mask */,
166 dev_warn(dev, "goIdle timed out\n");
173 static int crb_go_idle(struct tpm_chip *chip)
175 struct device *dev = &chip->dev;
176 struct crb_priv *priv = dev_get_drvdata(dev);
178 return __crb_go_idle(dev, priv);
182 * __crb_cmd_ready - request tpm crb device to enter ready state
185 * @priv: crb private data
187 * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
188 * and poll till the device acknowledge it by clearing the bit.
189 * The device should respond within TIMEOUT_C.
191 * The function does nothing for devices with ACPI-start method
193 * Return: 0 on success -ETIME on timeout;
195 static int __crb_cmd_ready(struct device *dev, struct crb_priv *priv)
197 if ((priv->flags & CRB_FL_ACPI_START) ||
198 (priv->flags & CRB_FL_CRB_SMC_START))
201 iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
202 if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
203 CRB_CTRL_REQ_CMD_READY /* mask */,
206 dev_warn(dev, "cmdReady timed out\n");
213 static int crb_cmd_ready(struct tpm_chip *chip)
215 struct device *dev = &chip->dev;
216 struct crb_priv *priv = dev_get_drvdata(dev);
218 return __crb_cmd_ready(dev, priv);
221 static int __crb_request_locality(struct device *dev,
222 struct crb_priv *priv, int loc)
224 u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
225 CRB_LOC_STATE_TPM_REG_VALID_STS;
230 iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h->loc_ctrl);
231 if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
233 dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
240 static int crb_request_locality(struct tpm_chip *chip, int loc)
242 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
244 return __crb_request_locality(&chip->dev, priv, loc);
247 static int __crb_relinquish_locality(struct device *dev,
248 struct crb_priv *priv, int loc)
250 u32 mask = CRB_LOC_STATE_LOC_ASSIGNED |
251 CRB_LOC_STATE_TPM_REG_VALID_STS;
252 u32 value = CRB_LOC_STATE_TPM_REG_VALID_STS;
257 iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);
258 if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, mask, value,
260 dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
267 static int crb_relinquish_locality(struct tpm_chip *chip, int loc)
269 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
271 return __crb_relinquish_locality(&chip->dev, priv, loc);
274 static u8 crb_status(struct tpm_chip *chip)
276 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
279 if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
281 sts |= CRB_DRV_STS_COMPLETE;
286 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
288 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
289 unsigned int expected;
291 /* A sanity check that the upper layer wants to get at least the header
292 * as that is the minimum size for any TPM response.
294 if (count < TPM_HEADER_SIZE)
297 /* If this bit is set, according to the spec, the TPM is in
298 * unrecoverable condition.
300 if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
303 /* Read the first 8 bytes in order to get the length of the response.
304 * We read exactly a quad word in order to make sure that the remaining
305 * reads will be aligned.
307 memcpy_fromio(buf, priv->rsp, 8);
309 expected = be32_to_cpup((__be32 *)&buf[2]);
310 if (expected > count || expected < TPM_HEADER_SIZE)
313 memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8);
318 static int crb_do_acpi_start(struct tpm_chip *chip)
320 union acpi_object *obj;
323 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
324 &crb_acpi_start_guid,
325 CRB_ACPI_START_REVISION_ID,
326 CRB_ACPI_START_INDEX,
330 rc = obj->integer.value == 0 ? 0 : -ENXIO;
337 * This is a TPM Command Response Buffer start method that invokes a
338 * Secure Monitor Call to requrest the firmware to execute or cancel
341 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
343 struct arm_smccc_res res;
345 arm_smccc_smc(func_id, 0, 0, 0, 0, 0, 0, 0, &res);
348 FW_BUG "tpm_crb_smc_start() returns res.a0 = 0x%lx\n",
356 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
358 dev_err(dev, FW_BUG "tpm_crb: incorrect start method\n");
363 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
365 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
368 /* Zero the cancel register so that the next command will not get
371 iowrite32(0, &priv->regs_t->ctrl_cancel);
373 if (len > priv->cmd_size) {
374 dev_err(&chip->dev, "invalid command count value %zd %d\n",
375 len, priv->cmd_size);
379 memcpy_toio(priv->cmd, buf, len);
381 /* Make sure that cmd is populated before issuing start. */
384 if (priv->flags & CRB_FL_CRB_START)
385 iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
387 if (priv->flags & CRB_FL_ACPI_START)
388 rc = crb_do_acpi_start(chip);
390 if (priv->flags & CRB_FL_CRB_SMC_START) {
391 iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
392 rc = tpm_crb_smc_start(&chip->dev, priv->smc_func_id);
398 static void crb_cancel(struct tpm_chip *chip)
400 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
402 iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
404 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
405 dev_err(&chip->dev, "ACPI Start failed\n");
408 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
410 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
411 u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
413 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
416 static const struct tpm_class_ops tpm_crb = {
417 .flags = TPM_OPS_AUTO_STARTUP,
418 .status = crb_status,
421 .cancel = crb_cancel,
422 .req_canceled = crb_req_canceled,
423 .go_idle = crb_go_idle,
424 .cmd_ready = crb_cmd_ready,
425 .request_locality = crb_request_locality,
426 .relinquish_locality = crb_relinquish_locality,
427 .req_complete_mask = CRB_DRV_STS_COMPLETE,
428 .req_complete_val = CRB_DRV_STS_COMPLETE,
431 static int crb_check_resource(struct acpi_resource *ares, void *data)
433 struct resource *io_res = data;
434 struct resource_win win;
435 struct resource *res = &(win.res);
437 if (acpi_dev_resource_memory(ares, res) ||
438 acpi_dev_resource_address_space(ares, &win)) {
446 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
447 struct resource *io_res, u64 start, u32 size)
449 struct resource new_res = {
451 .end = start + size - 1,
452 .flags = IORESOURCE_MEM,
455 /* Detect a 64 bit address on a 32 bit system */
456 if (start != new_res.start)
457 return (void __iomem *) ERR_PTR(-EINVAL);
459 if (!resource_contains(io_res, &new_res))
460 return devm_ioremap_resource(dev, &new_res);
462 return priv->iobase + (new_res.start - io_res->start);
466 * Work around broken BIOSs that return inconsistent values from the ACPI
467 * region vs the registers. Trust the ACPI region. Such broken systems
468 * probably cannot send large TPM commands since the buffer will be truncated.
470 static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
473 if (io_res->start > start || io_res->end < start)
476 if (start + size - 1 <= io_res->end)
480 FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
481 io_res, start, size);
483 return io_res->end - start + 1;
486 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
487 struct acpi_table_tpm2 *buf)
489 struct list_head resources;
490 struct resource io_res;
491 struct device *dev = &device->dev;
499 INIT_LIST_HEAD(&resources);
500 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
504 acpi_dev_free_resource_list(&resources);
506 if (resource_type(&io_res) != IORESOURCE_MEM) {
507 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
511 priv->iobase = devm_ioremap_resource(dev, &io_res);
512 if (IS_ERR(priv->iobase))
513 return PTR_ERR(priv->iobase);
515 /* The ACPI IO region starts at the head area and continues to include
516 * the control area, as one nice sane region except for some older
517 * stuff that puts the control area outside the ACPI IO region.
519 if (!(priv->flags & CRB_FL_ACPI_START)) {
520 if (buf->control_address == io_res.start +
521 sizeof(*priv->regs_h))
522 priv->regs_h = priv->iobase;
524 dev_warn(dev, FW_BUG "Bad ACPI memory layout");
527 ret = __crb_request_locality(dev, priv, 0);
531 priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
532 sizeof(struct crb_regs_tail));
533 if (IS_ERR(priv->regs_t)) {
534 ret = PTR_ERR(priv->regs_t);
535 goto out_relinquish_locality;
539 * PTT HW bug w/a: wake up the device to access
540 * possibly not retained registers.
542 ret = __crb_cmd_ready(dev, priv);
544 goto out_relinquish_locality;
546 pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
547 pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
548 cmd_pa = ((u64)pa_high << 32) | pa_low;
549 cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
550 ioread32(&priv->regs_t->ctrl_cmd_size));
552 dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
553 pa_high, pa_low, cmd_size);
555 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
556 if (IS_ERR(priv->cmd)) {
557 ret = PTR_ERR(priv->cmd);
561 memcpy_fromio(&rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
562 rsp_pa = le64_to_cpu(rsp_pa);
563 rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
564 ioread32(&priv->regs_t->ctrl_rsp_size));
566 if (cmd_pa != rsp_pa) {
567 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
568 ret = PTR_ERR_OR_ZERO(priv->rsp);
572 /* According to the PTP specification, overlapping command and response
573 * buffer sizes must be identical.
575 if (cmd_size != rsp_size) {
576 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
581 priv->rsp = priv->cmd;
585 priv->cmd_size = cmd_size;
587 __crb_go_idle(dev, priv);
589 out_relinquish_locality:
591 __crb_relinquish_locality(dev, priv, 0);
596 static int crb_acpi_add(struct acpi_device *device)
598 struct acpi_table_tpm2 *buf;
599 struct crb_priv *priv;
600 struct tpm_chip *chip;
601 struct device *dev = &device->dev;
602 struct tpm2_crb_smc *crb_smc;
607 status = acpi_get_table(ACPI_SIG_TPM2, 1,
608 (struct acpi_table_header **) &buf);
609 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
610 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
614 /* Should the FIFO driver handle this? */
615 sm = buf->start_method;
616 if (sm == ACPI_TPM2_MEMORY_MAPPED)
619 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
623 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
624 * report only ACPI start but in practice seems to require both
625 * ACPI start and CRB start.
627 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
628 !strcmp(acpi_device_hid(device), "MSFT0101"))
629 priv->flags |= CRB_FL_CRB_START;
631 if (sm == ACPI_TPM2_START_METHOD ||
632 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
633 priv->flags |= CRB_FL_ACPI_START;
635 if (sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
636 if (buf->header.length < (sizeof(*buf) + sizeof(*crb_smc))) {
638 FW_BUG "TPM2 ACPI table has wrong size %u for start method type %d\n",
640 ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC);
643 crb_smc = ACPI_ADD_PTR(struct tpm2_crb_smc, buf, sizeof(*buf));
644 priv->smc_func_id = crb_smc->smc_func_id;
645 priv->flags |= CRB_FL_CRB_SMC_START;
648 rc = crb_map_io(device, priv, buf);
652 chip = tpmm_chip_alloc(dev, &tpm_crb);
654 return PTR_ERR(chip);
656 dev_set_drvdata(&chip->dev, priv);
657 chip->acpi_dev_handle = device->handle;
658 chip->flags = TPM_CHIP_FLAG_TPM2;
660 return tpm_chip_register(chip);
663 static int crb_acpi_remove(struct acpi_device *device)
665 struct device *dev = &device->dev;
666 struct tpm_chip *chip = dev_get_drvdata(dev);
668 tpm_chip_unregister(chip);
673 static const struct dev_pm_ops crb_pm = {
674 SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume)
677 static const struct acpi_device_id crb_device_ids[] = {
681 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
683 static struct acpi_driver crb_acpi_driver = {
685 .ids = crb_device_ids,
688 .remove = crb_acpi_remove,
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");