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/platform_device.h>
25 #define ACPI_SIG_TPM2 "TPM2"
27 static const u8 CRB_ACPI_START_UUID[] = {
28 /* 0000 */ 0xAB, 0x6C, 0xBF, 0x6B, 0x63, 0x54, 0x14, 0x47,
29 /* 0008 */ 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4
33 CRB_ACPI_START_REVISION_ID = 1,
34 CRB_ACPI_START_INDEX = 1,
38 struct acpi_table_header hdr;
46 CRB_CA_REQ_GO_IDLE = BIT(0),
47 CRB_CA_REQ_CMD_READY = BIT(1),
51 CRB_CA_STS_ERROR = BIT(0),
52 CRB_CA_STS_TPM_IDLE = BIT(1),
56 CRB_START_INVOKE = BIT(0),
60 CRB_CANCEL_INVOKE = BIT(0),
63 struct crb_control_area {
78 CRB_STS_COMPLETE = BIT(0),
82 CRB_FL_ACPI_START = BIT(0),
83 CRB_FL_CRB_START = BIT(1),
88 struct crb_control_area __iomem *cca;
93 static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
95 static u8 crb_status(struct tpm_chip *chip)
97 struct crb_priv *priv = chip->vendor.priv;
100 if ((le32_to_cpu(ioread32(&priv->cca->start)) & CRB_START_INVOKE) !=
102 sts |= CRB_STS_COMPLETE;
107 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
109 struct crb_priv *priv = chip->vendor.priv;
110 unsigned int expected;
112 /* A sanity check that the upper layer wants to get at least the header
113 * as that is the minimum size for any TPM response.
115 if (count < TPM_HEADER_SIZE)
118 /* If this bit is set, according to the spec, the TPM is in
119 * unrecoverable condition.
121 if (le32_to_cpu(ioread32(&priv->cca->sts)) & CRB_CA_STS_ERROR)
124 /* Read the first 8 bytes in order to get the length of the response.
125 * We read exactly a quad word in order to make sure that the remaining
126 * reads will be aligned.
128 memcpy_fromio(buf, priv->rsp, 8);
130 expected = be32_to_cpup((__be32 *)&buf[2]);
131 if (expected > count || expected < TPM_HEADER_SIZE)
134 memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8);
139 static int crb_do_acpi_start(struct tpm_chip *chip)
141 union acpi_object *obj;
144 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
146 CRB_ACPI_START_REVISION_ID,
147 CRB_ACPI_START_INDEX,
151 rc = obj->integer.value == 0 ? 0 : -ENXIO;
156 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
158 struct crb_priv *priv = chip->vendor.priv;
161 /* Zero the cancel register so that the next command will not get
164 iowrite32(0, &priv->cca->cancel);
166 if (len > le32_to_cpu(ioread32(&priv->cca->cmd_size))) {
168 "invalid command count value %x %zx\n",
170 (size_t) le32_to_cpu(ioread32(&priv->cca->cmd_size)));
174 memcpy_toio(priv->cmd, buf, len);
176 /* Make sure that cmd is populated before issuing start. */
179 if (priv->flags & CRB_FL_CRB_START)
180 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
182 if (priv->flags & CRB_FL_ACPI_START)
183 rc = crb_do_acpi_start(chip);
188 static void crb_cancel(struct tpm_chip *chip)
190 struct crb_priv *priv = chip->vendor.priv;
192 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
194 /* Make sure that cmd is populated before issuing cancel. */
197 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
198 dev_err(&chip->dev, "ACPI Start failed\n");
201 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
203 struct crb_priv *priv = chip->vendor.priv;
204 u32 cancel = le32_to_cpu(ioread32(&priv->cca->cancel));
206 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
209 static const struct tpm_class_ops tpm_crb = {
210 .status = crb_status,
213 .cancel = crb_cancel,
214 .req_canceled = crb_req_canceled,
215 .req_complete_mask = CRB_STS_COMPLETE,
216 .req_complete_val = CRB_STS_COMPLETE,
219 static int crb_acpi_add(struct acpi_device *device)
221 struct tpm_chip *chip;
222 struct acpi_tpm2 *buf;
223 struct crb_priv *priv;
224 struct device *dev = &device->dev;
230 status = acpi_get_table(ACPI_SIG_TPM2, 1,
231 (struct acpi_table_header **) &buf);
232 if (ACPI_FAILURE(status)) {
233 dev_err(dev, "failed to get TPM2 ACPI table\n");
237 /* Should the FIFO driver handle this? */
238 if (buf->start_method == TPM2_START_FIFO)
241 chip = tpmm_chip_alloc(dev, &tpm_crb);
243 return PTR_ERR(chip);
245 chip->flags = TPM_CHIP_FLAG_TPM2;
247 if (buf->hdr.length < sizeof(struct acpi_tpm2)) {
248 dev_err(dev, "TPM2 ACPI table has wrong size");
252 priv = (struct crb_priv *) devm_kzalloc(dev, sizeof(struct crb_priv),
255 dev_err(dev, "failed to devm_kzalloc for private data\n");
259 sm = le32_to_cpu(buf->start_method);
261 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
262 * report only ACPI start but in practice seems to require both
263 * ACPI start and CRB start.
265 if (sm == TPM2_START_CRB || sm == TPM2_START_FIFO ||
266 !strcmp(acpi_device_hid(device), "MSFT0101"))
267 priv->flags |= CRB_FL_CRB_START;
269 if (sm == TPM2_START_ACPI || sm == TPM2_START_CRB_WITH_ACPI)
270 priv->flags |= CRB_FL_ACPI_START;
272 priv->cca = (struct crb_control_area __iomem *)
273 devm_ioremap_nocache(dev, buf->control_area_pa, 0x1000);
275 dev_err(dev, "ioremap of the control area failed\n");
279 pa = ((u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_high)) << 32) |
280 (u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_low));
281 priv->cmd = devm_ioremap_nocache(dev, pa,
282 ioread32(&priv->cca->cmd_size));
284 dev_err(dev, "ioremap of the command buffer failed\n");
288 memcpy_fromio(&pa, &priv->cca->rsp_pa, 8);
289 pa = le64_to_cpu(pa);
290 priv->rsp = devm_ioremap_nocache(dev, pa,
291 ioread32(&priv->cca->rsp_size));
293 dev_err(dev, "ioremap of the response buffer failed\n");
297 chip->vendor.priv = priv;
299 /* Default timeouts and durations */
300 chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
301 chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
302 chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
303 chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
304 chip->vendor.duration[TPM_SHORT] =
305 msecs_to_jiffies(TPM2_DURATION_SHORT);
306 chip->vendor.duration[TPM_MEDIUM] =
307 msecs_to_jiffies(TPM2_DURATION_MEDIUM);
308 chip->vendor.duration[TPM_LONG] =
309 msecs_to_jiffies(TPM2_DURATION_LONG);
311 chip->acpi_dev_handle = device->handle;
313 rc = tpm2_do_selftest(chip);
317 return tpm_chip_register(chip);
320 static int crb_acpi_remove(struct acpi_device *device)
322 struct device *dev = &device->dev;
323 struct tpm_chip *chip = dev_get_drvdata(dev);
325 if (chip->flags & TPM_CHIP_FLAG_TPM2)
326 tpm2_shutdown(chip, TPM2_SU_CLEAR);
328 tpm_chip_unregister(chip);
333 static struct acpi_device_id crb_device_ids[] = {
337 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
339 static struct acpi_driver crb_acpi_driver = {
341 .ids = crb_device_ids,
344 .remove = crb_acpi_remove,
351 module_acpi_driver(crb_acpi_driver);
352 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
353 MODULE_DESCRIPTION("TPM2 Driver");
354 MODULE_VERSION("0.1");
355 MODULE_LICENSE("GPL");