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 {
121 * crb_go_idle - request tpm crb device to go the idle state
124 * @priv: crb private data
126 * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
127 * The device should respond within TIMEOUT_C by clearing the bit.
128 * Anyhow, we do not wait here as a consequent CMD_READY request
129 * will be handled correctly even if idle was not completed.
131 * The function does nothing for devices with ACPI-start method.
135 static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
137 if ((priv->flags & CRB_FL_ACPI_START) ||
138 (priv->flags & CRB_FL_CRB_SMC_START))
141 iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
142 /* we don't really care when this settles */
147 static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
148 unsigned long timeout)
154 stop = ktime_add(start, ms_to_ktime(timeout));
157 if ((ioread32(reg) & mask) == value)
160 usleep_range(50, 100);
161 } while (ktime_before(ktime_get(), stop));
167 * crb_cmd_ready - request tpm crb device to enter ready state
170 * @priv: crb private data
172 * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
173 * and poll till the device acknowledge it by clearing the bit.
174 * The device should respond within TIMEOUT_C.
176 * The function does nothing for devices with ACPI-start method
178 * Return: 0 on success -ETIME on timeout;
180 static int __maybe_unused crb_cmd_ready(struct device *dev,
181 struct crb_priv *priv)
183 if ((priv->flags & CRB_FL_ACPI_START) ||
184 (priv->flags & CRB_FL_CRB_SMC_START))
187 iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
188 if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
189 CRB_CTRL_REQ_CMD_READY /* mask */,
192 dev_warn(dev, "cmdReady timed out\n");
199 static int crb_request_locality(struct tpm_chip *chip, int loc)
201 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
202 u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
203 CRB_LOC_STATE_TPM_REG_VALID_STS;
208 iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h->loc_ctrl);
209 if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
211 dev_warn(&chip->dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
218 static void crb_relinquish_locality(struct tpm_chip *chip, int loc)
220 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
225 iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);
228 static u8 crb_status(struct tpm_chip *chip)
230 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
233 if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
235 sts |= CRB_DRV_STS_COMPLETE;
240 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
242 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
243 unsigned int expected;
249 if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
252 memcpy_fromio(buf, priv->rsp, 6);
253 expected = be32_to_cpup((__be32 *) &buf[2]);
254 if (expected > count || expected < 6)
257 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
262 static int crb_do_acpi_start(struct tpm_chip *chip)
264 union acpi_object *obj;
267 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
268 &crb_acpi_start_guid,
269 CRB_ACPI_START_REVISION_ID,
270 CRB_ACPI_START_INDEX,
274 rc = obj->integer.value == 0 ? 0 : -ENXIO;
281 * This is a TPM Command Response Buffer start method that invokes a
282 * Secure Monitor Call to requrest the firmware to execute or cancel
285 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
287 struct arm_smccc_res res;
289 arm_smccc_smc(func_id, 0, 0, 0, 0, 0, 0, 0, &res);
292 FW_BUG "tpm_crb_smc_start() returns res.a0 = 0x%lx\n",
300 static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
302 dev_err(dev, FW_BUG "tpm_crb: incorrect start method\n");
307 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
309 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
312 /* Zero the cancel register so that the next command will not get
315 iowrite32(0, &priv->regs_t->ctrl_cancel);
317 if (len > priv->cmd_size) {
318 dev_err(&chip->dev, "invalid command count value %zd %d\n",
319 len, priv->cmd_size);
323 memcpy_toio(priv->cmd, buf, len);
325 /* Make sure that cmd is populated before issuing start. */
328 if (priv->flags & CRB_FL_CRB_START)
329 iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
331 if (priv->flags & CRB_FL_ACPI_START)
332 rc = crb_do_acpi_start(chip);
334 if (priv->flags & CRB_FL_CRB_SMC_START) {
335 iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
336 rc = tpm_crb_smc_start(&chip->dev, priv->smc_func_id);
342 static void crb_cancel(struct tpm_chip *chip)
344 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
346 iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
348 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
349 dev_err(&chip->dev, "ACPI Start failed\n");
352 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
354 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
355 u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
357 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
360 static const struct tpm_class_ops tpm_crb = {
361 .flags = TPM_OPS_AUTO_STARTUP,
362 .status = crb_status,
365 .cancel = crb_cancel,
366 .req_canceled = crb_req_canceled,
367 .request_locality = crb_request_locality,
368 .relinquish_locality = crb_relinquish_locality,
369 .req_complete_mask = CRB_DRV_STS_COMPLETE,
370 .req_complete_val = CRB_DRV_STS_COMPLETE,
373 static int crb_check_resource(struct acpi_resource *ares, void *data)
375 struct resource *io_res = data;
376 struct resource_win win;
377 struct resource *res = &(win.res);
379 if (acpi_dev_resource_memory(ares, res) ||
380 acpi_dev_resource_address_space(ares, &win)) {
388 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
389 struct resource *io_res, u64 start, u32 size)
391 struct resource new_res = {
393 .end = start + size - 1,
394 .flags = IORESOURCE_MEM,
397 /* Detect a 64 bit address on a 32 bit system */
398 if (start != new_res.start)
399 return (void __iomem *) ERR_PTR(-EINVAL);
401 if (!resource_contains(io_res, &new_res))
402 return devm_ioremap_resource(dev, &new_res);
404 return priv->iobase + (new_res.start - io_res->start);
408 * Work around broken BIOSs that return inconsistent values from the ACPI
409 * region vs the registers. Trust the ACPI region. Such broken systems
410 * probably cannot send large TPM commands since the buffer will be truncated.
412 static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
415 if (io_res->start > start || io_res->end < start)
418 if (start + size - 1 <= io_res->end)
422 FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
423 io_res, start, size);
425 return io_res->end - start + 1;
428 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
429 struct acpi_table_tpm2 *buf)
431 struct list_head resources;
432 struct resource io_res;
433 struct device *dev = &device->dev;
441 INIT_LIST_HEAD(&resources);
442 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
446 acpi_dev_free_resource_list(&resources);
448 if (resource_type(&io_res) != IORESOURCE_MEM) {
449 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
453 priv->iobase = devm_ioremap_resource(dev, &io_res);
454 if (IS_ERR(priv->iobase))
455 return PTR_ERR(priv->iobase);
457 /* The ACPI IO region starts at the head area and continues to include
458 * the control area, as one nice sane region except for some older
459 * stuff that puts the control area outside the ACPI IO region.
461 if (!(priv->flags & CRB_FL_ACPI_START)) {
462 if (buf->control_address == io_res.start +
463 sizeof(*priv->regs_h))
464 priv->regs_h = priv->iobase;
466 dev_warn(dev, FW_BUG "Bad ACPI memory layout");
469 priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
470 sizeof(struct crb_regs_tail));
471 if (IS_ERR(priv->regs_t))
472 return PTR_ERR(priv->regs_t);
475 * PTT HW bug w/a: wake up the device to access
476 * possibly not retained registers.
478 ret = crb_cmd_ready(dev, priv);
482 pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
483 pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
484 cmd_pa = ((u64)pa_high << 32) | pa_low;
485 cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
486 ioread32(&priv->regs_t->ctrl_cmd_size));
488 dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
489 pa_high, pa_low, cmd_size);
491 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
492 if (IS_ERR(priv->cmd)) {
493 ret = PTR_ERR(priv->cmd);
497 memcpy_fromio(&rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
498 rsp_pa = le64_to_cpu(rsp_pa);
499 rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
500 ioread32(&priv->regs_t->ctrl_rsp_size));
502 if (cmd_pa != rsp_pa) {
503 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
504 ret = PTR_ERR_OR_ZERO(priv->rsp);
508 /* According to the PTP specification, overlapping command and response
509 * buffer sizes must be identical.
511 if (cmd_size != rsp_size) {
512 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
517 priv->rsp = priv->cmd;
521 priv->cmd_size = cmd_size;
523 crb_go_idle(dev, priv);
528 static int crb_acpi_add(struct acpi_device *device)
530 struct acpi_table_tpm2 *buf;
531 struct crb_priv *priv;
532 struct tpm_chip *chip;
533 struct device *dev = &device->dev;
534 struct tpm2_crb_smc *crb_smc;
539 status = acpi_get_table(ACPI_SIG_TPM2, 1,
540 (struct acpi_table_header **) &buf);
541 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
542 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
546 /* Should the FIFO driver handle this? */
547 sm = buf->start_method;
548 if (sm == ACPI_TPM2_MEMORY_MAPPED)
551 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
555 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
556 * report only ACPI start but in practice seems to require both
557 * ACPI start and CRB start.
559 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
560 !strcmp(acpi_device_hid(device), "MSFT0101"))
561 priv->flags |= CRB_FL_CRB_START;
563 if (sm == ACPI_TPM2_START_METHOD ||
564 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
565 priv->flags |= CRB_FL_ACPI_START;
567 if (sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
568 if (buf->header.length < (sizeof(*buf) + sizeof(*crb_smc))) {
570 FW_BUG "TPM2 ACPI table has wrong size %u for start method type %d\n",
572 ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC);
575 crb_smc = ACPI_ADD_PTR(struct tpm2_crb_smc, buf, sizeof(*buf));
576 priv->smc_func_id = crb_smc->smc_func_id;
577 priv->flags |= CRB_FL_CRB_SMC_START;
580 rc = crb_map_io(device, priv, buf);
584 chip = tpmm_chip_alloc(dev, &tpm_crb);
586 return PTR_ERR(chip);
588 dev_set_drvdata(&chip->dev, priv);
589 chip->acpi_dev_handle = device->handle;
590 chip->flags = TPM_CHIP_FLAG_TPM2;
592 rc = crb_cmd_ready(dev, priv);
596 pm_runtime_get_noresume(dev);
597 pm_runtime_set_active(dev);
598 pm_runtime_enable(dev);
600 rc = tpm_chip_register(chip);
602 crb_go_idle(dev, priv);
603 pm_runtime_put_noidle(dev);
604 pm_runtime_disable(dev);
613 static int crb_acpi_remove(struct acpi_device *device)
615 struct device *dev = &device->dev;
616 struct tpm_chip *chip = dev_get_drvdata(dev);
618 tpm_chip_unregister(chip);
620 pm_runtime_disable(dev);
625 static int __maybe_unused crb_pm_runtime_suspend(struct device *dev)
627 struct tpm_chip *chip = dev_get_drvdata(dev);
628 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
630 return crb_go_idle(dev, priv);
633 static int __maybe_unused crb_pm_runtime_resume(struct device *dev)
635 struct tpm_chip *chip = dev_get_drvdata(dev);
636 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
638 return crb_cmd_ready(dev, priv);
641 static int __maybe_unused crb_pm_suspend(struct device *dev)
645 ret = tpm_pm_suspend(dev);
649 return crb_pm_runtime_suspend(dev);
652 static int __maybe_unused crb_pm_resume(struct device *dev)
656 ret = crb_pm_runtime_resume(dev);
660 return tpm_pm_resume(dev);
663 static const struct dev_pm_ops crb_pm = {
664 SET_SYSTEM_SLEEP_PM_OPS(crb_pm_suspend, crb_pm_resume)
665 SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
668 static struct acpi_device_id crb_device_ids[] = {
672 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
674 static struct acpi_driver crb_acpi_driver = {
676 .ids = crb_device_ids,
679 .remove = crb_acpi_remove,
686 module_acpi_driver(crb_acpi_driver);
687 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
688 MODULE_DESCRIPTION("TPM2 Driver");
689 MODULE_VERSION("0.1");
690 MODULE_LICENSE("GPL");