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 CRB_CA_REQ_GO_IDLE = BIT(0),
39 CRB_CA_REQ_CMD_READY = BIT(1),
43 CRB_CA_STS_ERROR = BIT(0),
44 CRB_CA_STS_TPM_IDLE = BIT(1),
48 CRB_START_INVOKE = BIT(0),
52 CRB_CANCEL_INVOKE = BIT(0),
55 struct crb_control_area {
70 CRB_STS_COMPLETE = BIT(0),
74 CRB_FL_ACPI_START = BIT(0),
75 CRB_FL_CRB_START = BIT(1),
81 struct crb_control_area __iomem *cca;
86 static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
88 static u8 crb_status(struct tpm_chip *chip)
90 struct crb_priv *priv = chip->vendor.priv;
93 if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
95 sts |= CRB_STS_COMPLETE;
100 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
102 struct crb_priv *priv = chip->vendor.priv;
103 unsigned int expected;
109 if (ioread32(&priv->cca->sts) & CRB_CA_STS_ERROR)
112 memcpy_fromio(buf, priv->rsp, 6);
113 expected = be32_to_cpup((__be32 *) &buf[2]);
115 if (expected > count)
118 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
123 static int crb_do_acpi_start(struct tpm_chip *chip)
125 union acpi_object *obj;
128 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
130 CRB_ACPI_START_REVISION_ID,
131 CRB_ACPI_START_INDEX,
135 rc = obj->integer.value == 0 ? 0 : -ENXIO;
140 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
142 struct crb_priv *priv = chip->vendor.priv;
145 if (len > ioread32(&priv->cca->cmd_size)) {
147 "invalid command count value %x %zx\n",
149 (size_t) ioread32(&priv->cca->cmd_size));
153 memcpy_toio(priv->cmd, buf, len);
155 /* Make sure that cmd is populated before issuing start. */
158 if (priv->flags & CRB_FL_CRB_START)
159 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
161 if (priv->flags & CRB_FL_ACPI_START)
162 rc = crb_do_acpi_start(chip);
167 static void crb_cancel(struct tpm_chip *chip)
169 struct crb_priv *priv = chip->vendor.priv;
171 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
173 /* Make sure that cmd is populated before issuing cancel. */
176 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
177 dev_err(&chip->dev, "ACPI Start failed\n");
179 iowrite32(0, &priv->cca->cancel);
182 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
184 struct crb_priv *priv = chip->vendor.priv;
185 u32 cancel = ioread32(&priv->cca->cancel);
187 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
190 static const struct tpm_class_ops tpm_crb = {
191 .status = crb_status,
194 .cancel = crb_cancel,
195 .req_canceled = crb_req_canceled,
196 .req_complete_mask = CRB_STS_COMPLETE,
197 .req_complete_val = CRB_STS_COMPLETE,
200 static int crb_init(struct acpi_device *device, struct crb_priv *priv)
202 struct tpm_chip *chip;
205 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
207 return PTR_ERR(chip);
209 chip->vendor.priv = priv;
210 chip->acpi_dev_handle = device->handle;
211 chip->flags = TPM_CHIP_FLAG_TPM2;
213 rc = tpm_get_timeouts(chip);
217 rc = tpm2_do_selftest(chip);
221 return tpm_chip_register(chip);
224 static int crb_check_resource(struct acpi_resource *ares, void *data)
226 struct resource *io_res = data;
229 if (acpi_dev_resource_memory(ares, &res)) {
237 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
238 struct resource *io_res, u64 start, u32 size)
240 struct resource new_res = {
242 .end = start + size - 1,
243 .flags = IORESOURCE_MEM,
246 /* Detect a 64 bit address on a 32 bit system */
247 if (start != new_res.start)
248 return ERR_PTR(-EINVAL);
250 if (!resource_contains(io_res, &new_res))
251 return devm_ioremap_resource(dev, &new_res);
253 return priv->iobase + (new_res.start - io_res->start);
256 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
257 struct acpi_table_tpm2 *buf)
259 struct list_head resources;
260 struct resource io_res;
261 struct device *dev = &device->dev;
268 INIT_LIST_HEAD(&resources);
269 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
273 acpi_dev_free_resource_list(&resources);
275 if (resource_type(&io_res) != IORESOURCE_MEM) {
277 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
281 priv->iobase = devm_ioremap_resource(dev, &io_res);
282 if (IS_ERR(priv->iobase))
283 return PTR_ERR(priv->iobase);
285 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
286 sizeof(struct crb_control_area));
287 if (IS_ERR(priv->cca))
288 return PTR_ERR(priv->cca);
290 cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
291 (u64) ioread32(&priv->cca->cmd_pa_low);
292 cmd_size = ioread32(&priv->cca->cmd_size);
293 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
294 if (IS_ERR(priv->cmd))
295 return PTR_ERR(priv->cmd);
297 memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
298 rsp_pa = le64_to_cpu(rsp_pa);
299 rsp_size = ioread32(&priv->cca->rsp_size);
301 if (cmd_pa != rsp_pa) {
302 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
303 return PTR_ERR_OR_ZERO(priv->rsp);
306 /* According to the PTP specification, overlapping command and response
307 * buffer sizes must be identical.
309 if (cmd_size != rsp_size) {
310 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
314 priv->rsp = priv->cmd;
318 static int crb_acpi_add(struct acpi_device *device)
320 struct acpi_table_tpm2 *buf;
321 struct crb_priv *priv;
322 struct device *dev = &device->dev;
327 status = acpi_get_table(ACPI_SIG_TPM2, 1,
328 (struct acpi_table_header **) &buf);
329 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
330 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
334 /* Should the FIFO driver handle this? */
335 sm = buf->start_method;
336 if (sm == ACPI_TPM2_MEMORY_MAPPED)
339 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
343 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
344 * report only ACPI start but in practice seems to require both
345 * ACPI start and CRB start.
347 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
348 !strcmp(acpi_device_hid(device), "MSFT0101"))
349 priv->flags |= CRB_FL_CRB_START;
351 if (sm == ACPI_TPM2_START_METHOD ||
352 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
353 priv->flags |= CRB_FL_ACPI_START;
355 rc = crb_map_io(device, priv, buf);
359 return crb_init(device, priv);
362 static int crb_acpi_remove(struct acpi_device *device)
364 struct device *dev = &device->dev;
365 struct tpm_chip *chip = dev_get_drvdata(dev);
367 tpm_chip_unregister(chip);
372 static struct acpi_device_id crb_device_ids[] = {
376 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
378 static struct acpi_driver crb_acpi_driver = {
380 .ids = crb_device_ids,
383 .remove = crb_acpi_remove,
390 module_acpi_driver(crb_acpi_driver);
391 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
392 MODULE_DESCRIPTION("TPM2 Driver");
393 MODULE_VERSION("0.1");
394 MODULE_LICENSE("GPL");