blob: 733cd0e20598be36415c8584a59a0d374b867a60 [file] [log] [blame]
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -08001/*
2 * Copyright (C) 2014 Intel Corporation
3 *
4 * Authors:
5 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
6 *
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 *
9 * This device driver implements the TPM interface as defined in
10 * the TCG CRB 2.0 TPM specification.
11 *
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
15 * of the License.
16 */
17
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>
23#include "tpm.h"
24
25#define ACPI_SIG_TPM2 "TPM2"
26
27static 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
30};
31
32enum crb_defaults {
33 CRB_ACPI_START_REVISION_ID = 1,
34 CRB_ACPI_START_INDEX = 1,
35};
36
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080037enum crb_ca_request {
38 CRB_CA_REQ_GO_IDLE = BIT(0),
39 CRB_CA_REQ_CMD_READY = BIT(1),
40};
41
42enum crb_ca_status {
43 CRB_CA_STS_ERROR = BIT(0),
44 CRB_CA_STS_TPM_IDLE = BIT(1),
45};
46
47enum crb_start {
48 CRB_START_INVOKE = BIT(0),
49};
50
51enum crb_cancel {
52 CRB_CANCEL_INVOKE = BIT(0),
53};
54
55struct crb_control_area {
56 u32 req;
57 u32 sts;
58 u32 cancel;
59 u32 start;
60 u32 int_enable;
61 u32 int_sts;
62 u32 cmd_size;
Jarkko Sakkinen149789c2015-09-15 20:05:40 +030063 u32 cmd_pa_low;
64 u32 cmd_pa_high;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080065 u32 rsp_size;
66 u64 rsp_pa;
67} __packed;
68
69enum crb_status {
70 CRB_STS_COMPLETE = BIT(0),
71};
72
73enum crb_flags {
74 CRB_FL_ACPI_START = BIT(0),
75 CRB_FL_CRB_START = BIT(1),
76};
77
78struct crb_priv {
79 unsigned int flags;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -070080 void __iomem *iobase;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080081 struct crb_control_area __iomem *cca;
82 u8 __iomem *cmd;
83 u8 __iomem *rsp;
84};
85
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +020086static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080087
88static u8 crb_status(struct tpm_chip *chip)
89{
90 struct crb_priv *priv = chip->vendor.priv;
91 u8 sts = 0;
92
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -070093 if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080094 CRB_START_INVOKE)
95 sts |= CRB_STS_COMPLETE;
96
97 return sts;
98}
99
100static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
101{
102 struct crb_priv *priv = chip->vendor.priv;
103 unsigned int expected;
104
105 /* sanity check */
106 if (count < 6)
107 return -EIO;
108
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -0700109 if (ioread32(&priv->cca->sts) & CRB_CA_STS_ERROR)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800110 return -EIO;
111
112 memcpy_fromio(buf, priv->rsp, 6);
113 expected = be32_to_cpup((__be32 *) &buf[2]);
114
115 if (expected > count)
116 return -EIO;
117
118 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
119
120 return expected;
121}
122
123static int crb_do_acpi_start(struct tpm_chip *chip)
124{
125 union acpi_object *obj;
126 int rc;
127
128 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
129 CRB_ACPI_START_UUID,
130 CRB_ACPI_START_REVISION_ID,
131 CRB_ACPI_START_INDEX,
132 NULL);
133 if (!obj)
134 return -ENXIO;
135 rc = obj->integer.value == 0 ? 0 : -ENXIO;
136 ACPI_FREE(obj);
137 return rc;
138}
139
140static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
141{
142 struct crb_priv *priv = chip->vendor.priv;
143 int rc = 0;
144
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -0700145 if (len > ioread32(&priv->cca->cmd_size)) {
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800146 dev_err(&chip->dev,
147 "invalid command count value %x %zx\n",
148 (unsigned int) len,
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -0700149 (size_t) ioread32(&priv->cca->cmd_size));
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800150 return -E2BIG;
151 }
152
153 memcpy_toio(priv->cmd, buf, len);
154
155 /* Make sure that cmd is populated before issuing start. */
156 wmb();
157
158 if (priv->flags & CRB_FL_CRB_START)
159 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
160
161 if (priv->flags & CRB_FL_ACPI_START)
162 rc = crb_do_acpi_start(chip);
163
164 return rc;
165}
166
167static void crb_cancel(struct tpm_chip *chip)
168{
169 struct crb_priv *priv = chip->vendor.priv;
170
171 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
172
173 /* Make sure that cmd is populated before issuing cancel. */
174 wmb();
175
176 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
177 dev_err(&chip->dev, "ACPI Start failed\n");
178
179 iowrite32(0, &priv->cca->cancel);
180}
181
182static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
183{
184 struct crb_priv *priv = chip->vendor.priv;
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -0700185 u32 cancel = ioread32(&priv->cca->cancel);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800186
187 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
188}
189
190static const struct tpm_class_ops tpm_crb = {
191 .status = crb_status,
192 .recv = crb_recv,
193 .send = crb_send,
194 .cancel = crb_cancel,
195 .req_canceled = crb_req_canceled,
196 .req_complete_mask = CRB_STS_COMPLETE,
197 .req_complete_val = CRB_STS_COMPLETE,
198};
199
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700200static int crb_init(struct acpi_device *device, struct crb_priv *priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800201{
202 struct tpm_chip *chip;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700203 int rc;
204
205 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
206 if (IS_ERR(chip))
207 return PTR_ERR(chip);
208
209 chip->vendor.priv = priv;
210 chip->acpi_dev_handle = device->handle;
211 chip->flags = TPM_CHIP_FLAG_TPM2;
212
213 rc = tpm_get_timeouts(chip);
214 if (rc)
215 return rc;
216
217 rc = tpm2_do_selftest(chip);
218 if (rc)
219 return rc;
220
221 return tpm_chip_register(chip);
222}
223
224static int crb_check_resource(struct acpi_resource *ares, void *data)
225{
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200226 struct resource *io_res = data;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700227 struct resource res;
228
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200229 if (acpi_dev_resource_memory(ares, &res)) {
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200230 *io_res = res;
231 io_res->name = NULL;
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200232 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700233
234 return 1;
235}
236
237static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200238 struct resource *io_res, u64 start, u32 size)
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700239{
240 struct resource new_res = {
241 .start = start,
242 .end = start + size - 1,
243 .flags = IORESOURCE_MEM,
244 };
245
246 /* Detect a 64 bit address on a 32 bit system */
247 if (start != new_res.start)
248 return ERR_PTR(-EINVAL);
249
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200250 if (!resource_contains(io_res, &new_res))
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700251 return devm_ioremap_resource(dev, &new_res);
252
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200253 return priv->iobase + (new_res.start - io_res->start);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700254}
255
256static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
257 struct acpi_table_tpm2 *buf)
258{
259 struct list_head resources;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200260 struct resource io_res;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700261 struct device *dev = &device->dev;
262 u64 pa;
263 int ret;
264
265 INIT_LIST_HEAD(&resources);
266 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200267 &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700268 if (ret < 0)
269 return ret;
270 acpi_dev_free_resource_list(&resources);
271
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200272 if (resource_type(&io_res) != IORESOURCE_MEM) {
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700273 dev_err(dev,
274 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
275 return -EINVAL;
276 }
277
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200278 priv->iobase = devm_ioremap_resource(dev, &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700279 if (IS_ERR(priv->iobase))
280 return PTR_ERR(priv->iobase);
281
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200282 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
283 0x1000);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700284 if (IS_ERR(priv->cca))
285 return PTR_ERR(priv->cca);
286
287 pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
288 (u64) ioread32(&priv->cca->cmd_pa_low);
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200289 priv->cmd = crb_map_res(dev, priv, &io_res, pa,
290 ioread32(&priv->cca->cmd_size));
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700291 if (IS_ERR(priv->cmd))
292 return PTR_ERR(priv->cmd);
293
294 memcpy_fromio(&pa, &priv->cca->rsp_pa, 8);
295 pa = le64_to_cpu(pa);
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200296 priv->rsp = crb_map_res(dev, priv, &io_res, pa,
297 ioread32(&priv->cca->rsp_size));
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700298 return PTR_ERR_OR_ZERO(priv->rsp);
299}
300
301static int crb_acpi_add(struct acpi_device *device)
302{
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700303 struct acpi_table_tpm2 *buf;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800304 struct crb_priv *priv;
305 struct device *dev = &device->dev;
306 acpi_status status;
307 u32 sm;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800308 int rc;
309
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800310 status = acpi_get_table(ACPI_SIG_TPM2, 1,
311 (struct acpi_table_header **) &buf);
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700312 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
313 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700314 return -EINVAL;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800315 }
316
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300317 /* Should the FIFO driver handle this? */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700318 sm = buf->start_method;
319 if (sm == ACPI_TPM2_MEMORY_MAPPED)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300320 return -ENODEV;
321
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700322 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
323 if (!priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800324 return -ENOMEM;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800325
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800326 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
327 * report only ACPI start but in practice seems to require both
328 * ACPI start and CRB start.
329 */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700330 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800331 !strcmp(acpi_device_hid(device), "MSFT0101"))
332 priv->flags |= CRB_FL_CRB_START;
333
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700334 if (sm == ACPI_TPM2_START_METHOD ||
335 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800336 priv->flags |= CRB_FL_ACPI_START;
337
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700338 rc = crb_map_io(device, priv, buf);
Jason Gunthorpe25112042015-11-25 14:05:32 -0700339 if (rc)
340 return rc;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800341
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700342 return crb_init(device, priv);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800343}
344
345static int crb_acpi_remove(struct acpi_device *device)
346{
347 struct device *dev = &device->dev;
348 struct tpm_chip *chip = dev_get_drvdata(dev);
349
Jarkko Sakkinen99cda8c2016-02-18 22:11:29 +0200350 tpm_chip_unregister(chip);
351
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800352 return 0;
353}
354
355static struct acpi_device_id crb_device_ids[] = {
356 {"MSFT0101", 0},
357 {"", 0},
358};
359MODULE_DEVICE_TABLE(acpi, crb_device_ids);
360
361static struct acpi_driver crb_acpi_driver = {
362 .name = "tpm_crb",
363 .ids = crb_device_ids,
364 .ops = {
365 .add = crb_acpi_add,
366 .remove = crb_acpi_remove,
367 },
368 .drv = {
369 .pm = &crb_pm,
370 },
371};
372
373module_acpi_driver(crb_acpi_driver);
374MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
375MODULE_DESCRIPTION("TPM2 Driver");
376MODULE_VERSION("0.1");
377MODULE_LICENSE("GPL");