tpm_crb: fix crb_req_canceled behavior
[deliverable/linux.git] / drivers / char / tpm / tpm_crb.c
CommitLineData
30fc8d13
JS
1/*
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
30fc8d13
JS
37enum 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;
149789ce
JS
63 u32 cmd_pa_low;
64 u32 cmd_pa_high;
30fc8d13
JS
65 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;
1bd047be 80 void __iomem *iobase;
30fc8d13
JS
81 struct crb_control_area __iomem *cca;
82 u8 __iomem *cmd;
83 u8 __iomem *rsp;
84};
85
74d6b3ce 86static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
30fc8d13
JS
87
88static u8 crb_status(struct tpm_chip *chip)
89{
9e0d39d8 90 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
30fc8d13
JS
91 u8 sts = 0;
92
1e3ed59d 93 if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
30fc8d13
JS
94 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{
9e0d39d8 102 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
30fc8d13
JS
103 unsigned int expected;
104
105 /* sanity check */
106 if (count < 6)
107 return -EIO;
108
1e3ed59d 109 if (ioread32(&priv->cca->sts) & CRB_CA_STS_ERROR)
30fc8d13
JS
110 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{
9e0d39d8 142 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
30fc8d13
JS
143 int rc = 0;
144
87d6616d
JS
145 /* Zero the cancel register so that the next command will not get
146 * canceled.
147 */
148 iowrite32(0, &priv->cca->cancel);
149
1e3ed59d 150 if (len > ioread32(&priv->cca->cmd_size)) {
30fc8d13
JS
151 dev_err(&chip->dev,
152 "invalid command count value %x %zx\n",
153 (unsigned int) len,
1e3ed59d 154 (size_t) ioread32(&priv->cca->cmd_size));
30fc8d13
JS
155 return -E2BIG;
156 }
157
158 memcpy_toio(priv->cmd, buf, len);
159
160 /* Make sure that cmd is populated before issuing start. */
161 wmb();
162
163 if (priv->flags & CRB_FL_CRB_START)
164 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
165
166 if (priv->flags & CRB_FL_ACPI_START)
167 rc = crb_do_acpi_start(chip);
168
169 return rc;
170}
171
172static void crb_cancel(struct tpm_chip *chip)
173{
9e0d39d8 174 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
30fc8d13
JS
175
176 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
177
178 /* Make sure that cmd is populated before issuing cancel. */
179 wmb();
180
181 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
182 dev_err(&chip->dev, "ACPI Start failed\n");
30fc8d13
JS
183}
184
185static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
186{
9e0d39d8 187 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
1e3ed59d 188 u32 cancel = ioread32(&priv->cca->cancel);
30fc8d13
JS
189
190 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
191}
192
193static const struct tpm_class_ops tpm_crb = {
cae8b441 194 .flags = TPM_OPS_AUTO_STARTUP,
30fc8d13
JS
195 .status = crb_status,
196 .recv = crb_recv,
197 .send = crb_send,
198 .cancel = crb_cancel,
199 .req_canceled = crb_req_canceled,
200 .req_complete_mask = CRB_STS_COMPLETE,
201 .req_complete_val = CRB_STS_COMPLETE,
202};
203
1bd047be 204static int crb_init(struct acpi_device *device, struct crb_priv *priv)
30fc8d13
JS
205{
206 struct tpm_chip *chip;
1bd047be
JG
207
208 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
209 if (IS_ERR(chip))
210 return PTR_ERR(chip);
211
9e0d39d8 212 dev_set_drvdata(&chip->dev, priv);
1bd047be
JG
213 chip->acpi_dev_handle = device->handle;
214 chip->flags = TPM_CHIP_FLAG_TPM2;
215
1bd047be
JG
216 return tpm_chip_register(chip);
217}
218
219static int crb_check_resource(struct acpi_resource *ares, void *data)
220{
14ddfbf4 221 struct resource *io_res = data;
1bd047be
JG
222 struct resource res;
223
30f9c8c9 224 if (acpi_dev_resource_memory(ares, &res)) {
14ddfbf4
JS
225 *io_res = res;
226 io_res->name = NULL;
30f9c8c9 227 }
1bd047be
JG
228
229 return 1;
230}
231
232static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
14ddfbf4 233 struct resource *io_res, u64 start, u32 size)
1bd047be
JG
234{
235 struct resource new_res = {
236 .start = start,
237 .end = start + size - 1,
238 .flags = IORESOURCE_MEM,
239 };
240
241 /* Detect a 64 bit address on a 32 bit system */
242 if (start != new_res.start)
f786b752 243 return (void __iomem *) ERR_PTR(-EINVAL);
1bd047be 244
14ddfbf4 245 if (!resource_contains(io_res, &new_res))
1bd047be
JG
246 return devm_ioremap_resource(dev, &new_res);
247
14ddfbf4 248 return priv->iobase + (new_res.start - io_res->start);
1bd047be
JG
249}
250
251static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
252 struct acpi_table_tpm2 *buf)
253{
254 struct list_head resources;
14ddfbf4 255 struct resource io_res;
1bd047be 256 struct device *dev = &device->dev;
422eac3f
JS
257 u64 cmd_pa;
258 u32 cmd_size;
259 u64 rsp_pa;
260 u32 rsp_size;
1bd047be
JG
261 int ret;
262
263 INIT_LIST_HEAD(&resources);
264 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
14ddfbf4 265 &io_res);
1bd047be
JG
266 if (ret < 0)
267 return ret;
268 acpi_dev_free_resource_list(&resources);
269
14ddfbf4 270 if (resource_type(&io_res) != IORESOURCE_MEM) {
1bd047be
JG
271 dev_err(dev,
272 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
273 return -EINVAL;
274 }
275
14ddfbf4 276 priv->iobase = devm_ioremap_resource(dev, &io_res);
1bd047be
JG
277 if (IS_ERR(priv->iobase))
278 return PTR_ERR(priv->iobase);
279
14ddfbf4 280 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
422eac3f 281 sizeof(struct crb_control_area));
1bd047be
JG
282 if (IS_ERR(priv->cca))
283 return PTR_ERR(priv->cca);
284
422eac3f
JS
285 cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
286 (u64) ioread32(&priv->cca->cmd_pa_low);
287 cmd_size = ioread32(&priv->cca->cmd_size);
288 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
1bd047be
JG
289 if (IS_ERR(priv->cmd))
290 return PTR_ERR(priv->cmd);
291
422eac3f
JS
292 memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
293 rsp_pa = le64_to_cpu(rsp_pa);
294 rsp_size = ioread32(&priv->cca->rsp_size);
295
296 if (cmd_pa != rsp_pa) {
297 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
298 return PTR_ERR_OR_ZERO(priv->rsp);
299 }
300
301 /* According to the PTP specification, overlapping command and response
302 * buffer sizes must be identical.
303 */
304 if (cmd_size != rsp_size) {
305 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
306 return -EINVAL;
307 }
308
309 priv->rsp = priv->cmd;
310 return 0;
1bd047be
JG
311}
312
313static int crb_acpi_add(struct acpi_device *device)
314{
55a889c2 315 struct acpi_table_tpm2 *buf;
30fc8d13
JS
316 struct crb_priv *priv;
317 struct device *dev = &device->dev;
318 acpi_status status;
319 u32 sm;
30fc8d13
JS
320 int rc;
321
30fc8d13
JS
322 status = acpi_get_table(ACPI_SIG_TPM2, 1,
323 (struct acpi_table_header **) &buf);
55a889c2
JG
324 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
325 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
1bd047be 326 return -EINVAL;
30fc8d13
JS
327 }
328
399235dc 329 /* Should the FIFO driver handle this? */
55a889c2
JG
330 sm = buf->start_method;
331 if (sm == ACPI_TPM2_MEMORY_MAPPED)
399235dc
JS
332 return -ENODEV;
333
1bd047be
JG
334 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
335 if (!priv)
30fc8d13 336 return -ENOMEM;
30fc8d13 337
30fc8d13
JS
338 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
339 * report only ACPI start but in practice seems to require both
340 * ACPI start and CRB start.
341 */
55a889c2 342 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
30fc8d13
JS
343 !strcmp(acpi_device_hid(device), "MSFT0101"))
344 priv->flags |= CRB_FL_CRB_START;
345
55a889c2
JG
346 if (sm == ACPI_TPM2_START_METHOD ||
347 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
30fc8d13
JS
348 priv->flags |= CRB_FL_ACPI_START;
349
1bd047be 350 rc = crb_map_io(device, priv, buf);
25112048
JG
351 if (rc)
352 return rc;
30fc8d13 353
1bd047be 354 return crb_init(device, priv);
30fc8d13
JS
355}
356
357static int crb_acpi_remove(struct acpi_device *device)
358{
359 struct device *dev = &device->dev;
360 struct tpm_chip *chip = dev_get_drvdata(dev);
361
99cda8cb
JS
362 tpm_chip_unregister(chip);
363
30fc8d13
JS
364 return 0;
365}
366
367static struct acpi_device_id crb_device_ids[] = {
368 {"MSFT0101", 0},
369 {"", 0},
370};
371MODULE_DEVICE_TABLE(acpi, crb_device_ids);
372
373static struct acpi_driver crb_acpi_driver = {
374 .name = "tpm_crb",
375 .ids = crb_device_ids,
376 .ops = {
377 .add = crb_acpi_add,
378 .remove = crb_acpi_remove,
379 },
380 .drv = {
381 .pm = &crb_pm,
382 },
383};
384
385module_acpi_driver(crb_acpi_driver);
386MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
387MODULE_DESCRIPTION("TPM2 Driver");
388MODULE_VERSION("0.1");
389MODULE_LICENSE("GPL");
This page took 0.089977 seconds and 5 git commands to generate.