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>
24 #define ACPI_SIG_TPM2 "TPM2"
26 static const u8 CRB_ACPI_START_UUID[] = {
27 /* 0000 */ 0xAB, 0x6C, 0xBF, 0x6B, 0x63, 0x54, 0x14, 0x47,
28 /* 0008 */ 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4
32 CRB_ACPI_START_REVISION_ID = 1,
33 CRB_ACPI_START_INDEX = 1,
37 CRB_CTRL_REQ_CMD_READY = BIT(0),
38 CRB_CTRL_REQ_GO_IDLE = BIT(1),
42 CRB_CTRL_STS_ERROR = BIT(0),
43 CRB_CTRL_STS_TPM_IDLE = BIT(1),
47 CRB_START_INVOKE = BIT(0),
51 CRB_CANCEL_INVOKE = BIT(0),
54 struct crb_control_area {
69 CRB_DRV_STS_COMPLETE = BIT(0),
73 CRB_FL_ACPI_START = BIT(0),
74 CRB_FL_CRB_START = BIT(1),
80 struct crb_control_area __iomem *cca;
85 static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
87 static u8 crb_status(struct tpm_chip *chip)
89 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
92 if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
94 sts |= CRB_DRV_STS_COMPLETE;
99 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
101 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
102 unsigned int expected;
108 if (ioread32(&priv->cca->sts) & CRB_CTRL_STS_ERROR)
111 memcpy_fromio(buf, priv->rsp, 6);
112 expected = be32_to_cpup((__be32 *) &buf[2]);
114 if (expected > count)
117 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
122 static int crb_do_acpi_start(struct tpm_chip *chip)
124 union acpi_object *obj;
127 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
129 CRB_ACPI_START_REVISION_ID,
130 CRB_ACPI_START_INDEX,
134 rc = obj->integer.value == 0 ? 0 : -ENXIO;
139 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
141 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
144 /* Zero the cancel register so that the next command will not get
147 iowrite32(0, &priv->cca->cancel);
149 if (len > ioread32(&priv->cca->cmd_size)) {
151 "invalid command count value %x %zx\n",
153 (size_t) ioread32(&priv->cca->cmd_size));
157 memcpy_toio(priv->cmd, buf, len);
159 /* Make sure that cmd is populated before issuing start. */
162 if (priv->flags & CRB_FL_CRB_START)
163 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
165 if (priv->flags & CRB_FL_ACPI_START)
166 rc = crb_do_acpi_start(chip);
171 static void crb_cancel(struct tpm_chip *chip)
173 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
175 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
177 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
178 dev_err(&chip->dev, "ACPI Start failed\n");
181 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
183 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
184 u32 cancel = ioread32(&priv->cca->cancel);
186 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
189 static const struct tpm_class_ops tpm_crb = {
190 .flags = TPM_OPS_AUTO_STARTUP,
191 .status = crb_status,
194 .cancel = crb_cancel,
195 .req_canceled = crb_req_canceled,
196 .req_complete_mask = CRB_DRV_STS_COMPLETE,
197 .req_complete_val = CRB_DRV_STS_COMPLETE,
200 static int crb_init(struct acpi_device *device, struct crb_priv *priv)
202 struct tpm_chip *chip;
204 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
206 return PTR_ERR(chip);
208 dev_set_drvdata(&chip->dev, priv);
209 chip->acpi_dev_handle = device->handle;
210 chip->flags = TPM_CHIP_FLAG_TPM2;
212 return tpm_chip_register(chip);
215 static int crb_check_resource(struct acpi_resource *ares, void *data)
217 struct resource *io_res = data;
220 if (acpi_dev_resource_memory(ares, &res)) {
228 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
229 struct resource *io_res, u64 start, u32 size)
231 struct resource new_res = {
233 .end = start + size - 1,
234 .flags = IORESOURCE_MEM,
237 /* Detect a 64 bit address on a 32 bit system */
238 if (start != new_res.start)
239 return (void __iomem *) ERR_PTR(-EINVAL);
241 if (!resource_contains(io_res, &new_res))
242 return devm_ioremap_resource(dev, &new_res);
244 return priv->iobase + (new_res.start - io_res->start);
247 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
248 struct acpi_table_tpm2 *buf)
250 struct list_head resources;
251 struct resource io_res;
252 struct device *dev = &device->dev;
259 INIT_LIST_HEAD(&resources);
260 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
264 acpi_dev_free_resource_list(&resources);
266 if (resource_type(&io_res) != IORESOURCE_MEM) {
268 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
272 priv->iobase = devm_ioremap_resource(dev, &io_res);
273 if (IS_ERR(priv->iobase))
274 return PTR_ERR(priv->iobase);
276 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
277 sizeof(struct crb_control_area));
278 if (IS_ERR(priv->cca))
279 return PTR_ERR(priv->cca);
281 cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
282 (u64) ioread32(&priv->cca->cmd_pa_low);
283 cmd_size = ioread32(&priv->cca->cmd_size);
284 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
285 if (IS_ERR(priv->cmd))
286 return PTR_ERR(priv->cmd);
288 memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
289 rsp_pa = le64_to_cpu(rsp_pa);
290 rsp_size = ioread32(&priv->cca->rsp_size);
292 if (cmd_pa != rsp_pa) {
293 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
294 return PTR_ERR_OR_ZERO(priv->rsp);
297 /* According to the PTP specification, overlapping command and response
298 * buffer sizes must be identical.
300 if (cmd_size != rsp_size) {
301 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
305 priv->rsp = priv->cmd;
309 static int crb_acpi_add(struct acpi_device *device)
311 struct acpi_table_tpm2 *buf;
312 struct crb_priv *priv;
313 struct device *dev = &device->dev;
318 status = acpi_get_table(ACPI_SIG_TPM2, 1,
319 (struct acpi_table_header **) &buf);
320 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
321 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
325 /* Should the FIFO driver handle this? */
326 sm = buf->start_method;
327 if (sm == ACPI_TPM2_MEMORY_MAPPED)
330 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
334 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
335 * report only ACPI start but in practice seems to require both
336 * ACPI start and CRB start.
338 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
339 !strcmp(acpi_device_hid(device), "MSFT0101"))
340 priv->flags |= CRB_FL_CRB_START;
342 if (sm == ACPI_TPM2_START_METHOD ||
343 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
344 priv->flags |= CRB_FL_ACPI_START;
346 rc = crb_map_io(device, priv, buf);
350 return crb_init(device, priv);
353 static int crb_acpi_remove(struct acpi_device *device)
355 struct device *dev = &device->dev;
356 struct tpm_chip *chip = dev_get_drvdata(dev);
358 tpm_chip_unregister(chip);
363 static struct acpi_device_id crb_device_ids[] = {
367 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
369 static struct acpi_driver crb_acpi_driver = {
371 .ids = crb_device_ids,
374 .remove = crb_acpi_remove,
381 module_acpi_driver(crb_acpi_driver);
382 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
383 MODULE_DESCRIPTION("TPM2 Driver");
384 MODULE_VERSION("0.1");
385 MODULE_LICENSE("GPL");