blob: 7f602dc1f9bb4d6dc4ce05940936bf4b690e8525 [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 Sakkinen7fd10d62016-09-02 22:34:19 +030037enum crb_ctrl_req {
Jarkko Sakkinenf39a9e92016-09-02 22:34:20 +030038 CRB_CTRL_REQ_CMD_READY = BIT(0),
39 CRB_CTRL_REQ_GO_IDLE = BIT(1),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080040};
41
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030042enum crb_ctrl_sts {
43 CRB_CTRL_STS_ERROR = BIT(0),
44 CRB_CTRL_STS_TPM_IDLE = BIT(1),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080045};
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 {
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030070 CRB_DRV_STS_COMPLETE = BIT(0),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080071};
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{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +020090 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080091 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)
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030095 sts |= CRB_DRV_STS_COMPLETE;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080096
97 return sts;
98}
99
100static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
101{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200102 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800103 unsigned int expected;
104
105 /* sanity check */
106 if (count < 6)
107 return -EIO;
108
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +0300109 if (ioread32(&priv->cca->sts) & CRB_CTRL_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{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200142 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800143 int rc = 0;
144
Jarkko Sakkinen72fd50e2016-09-02 22:34:17 +0300145 /* Zero the cancel register so that the next command will not get
146 * canceled.
147 */
148 iowrite32(0, &priv->cca->cancel);
149
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -0700150 if (len > ioread32(&priv->cca->cmd_size)) {
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800151 dev_err(&chip->dev,
152 "invalid command count value %x %zx\n",
153 (unsigned int) len,
Jason Gunthorpe1e3ed592016-01-07 17:36:25 -0700154 (size_t) ioread32(&priv->cca->cmd_size));
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800155 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{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200174 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800175
176 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
177
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800178 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
179 dev_err(&chip->dev, "ACPI Start failed\n");
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800180}
181
182static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
183{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200184 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
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 = {
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600191 .flags = TPM_OPS_AUTO_STARTUP,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800192 .status = crb_status,
193 .recv = crb_recv,
194 .send = crb_send,
195 .cancel = crb_cancel,
196 .req_canceled = crb_req_canceled,
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +0300197 .req_complete_mask = CRB_DRV_STS_COMPLETE,
198 .req_complete_val = CRB_DRV_STS_COMPLETE,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800199};
200
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700201static int crb_init(struct acpi_device *device, struct crb_priv *priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800202{
203 struct tpm_chip *chip;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700204
205 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
206 if (IS_ERR(chip))
207 return PTR_ERR(chip);
208
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200209 dev_set_drvdata(&chip->dev, priv);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700210 chip->acpi_dev_handle = device->handle;
211 chip->flags = TPM_CHIP_FLAG_TPM2;
212
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700213 return tpm_chip_register(chip);
214}
215
216static int crb_check_resource(struct acpi_resource *ares, void *data)
217{
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200218 struct resource *io_res = data;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700219 struct resource res;
220
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200221 if (acpi_dev_resource_memory(ares, &res)) {
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200222 *io_res = res;
223 io_res->name = NULL;
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200224 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700225
226 return 1;
227}
228
229static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200230 struct resource *io_res, u64 start, u32 size)
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700231{
232 struct resource new_res = {
233 .start = start,
234 .end = start + size - 1,
235 .flags = IORESOURCE_MEM,
236 };
237
238 /* Detect a 64 bit address on a 32 bit system */
239 if (start != new_res.start)
Jarkko Sakkinenf786b752016-06-17 16:39:29 +0200240 return (void __iomem *) ERR_PTR(-EINVAL);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700241
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200242 if (!resource_contains(io_res, &new_res))
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700243 return devm_ioremap_resource(dev, &new_res);
244
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200245 return priv->iobase + (new_res.start - io_res->start);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700246}
247
248static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
249 struct acpi_table_tpm2 *buf)
250{
251 struct list_head resources;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200252 struct resource io_res;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700253 struct device *dev = &device->dev;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300254 u64 cmd_pa;
255 u32 cmd_size;
256 u64 rsp_pa;
257 u32 rsp_size;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700258 int ret;
259
260 INIT_LIST_HEAD(&resources);
261 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200262 &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700263 if (ret < 0)
264 return ret;
265 acpi_dev_free_resource_list(&resources);
266
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200267 if (resource_type(&io_res) != IORESOURCE_MEM) {
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700268 dev_err(dev,
269 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
270 return -EINVAL;
271 }
272
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200273 priv->iobase = devm_ioremap_resource(dev, &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700274 if (IS_ERR(priv->iobase))
275 return PTR_ERR(priv->iobase);
276
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200277 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300278 sizeof(struct crb_control_area));
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700279 if (IS_ERR(priv->cca))
280 return PTR_ERR(priv->cca);
281
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300282 cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
283 (u64) ioread32(&priv->cca->cmd_pa_low);
284 cmd_size = ioread32(&priv->cca->cmd_size);
285 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700286 if (IS_ERR(priv->cmd))
287 return PTR_ERR(priv->cmd);
288
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300289 memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
290 rsp_pa = le64_to_cpu(rsp_pa);
291 rsp_size = ioread32(&priv->cca->rsp_size);
292
293 if (cmd_pa != rsp_pa) {
294 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
295 return PTR_ERR_OR_ZERO(priv->rsp);
296 }
297
298 /* According to the PTP specification, overlapping command and response
299 * buffer sizes must be identical.
300 */
301 if (cmd_size != rsp_size) {
302 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
303 return -EINVAL;
304 }
305
306 priv->rsp = priv->cmd;
307 return 0;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700308}
309
310static int crb_acpi_add(struct acpi_device *device)
311{
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700312 struct acpi_table_tpm2 *buf;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800313 struct crb_priv *priv;
314 struct device *dev = &device->dev;
315 acpi_status status;
316 u32 sm;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800317 int rc;
318
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800319 status = acpi_get_table(ACPI_SIG_TPM2, 1,
320 (struct acpi_table_header **) &buf);
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700321 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
322 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700323 return -EINVAL;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800324 }
325
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300326 /* Should the FIFO driver handle this? */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700327 sm = buf->start_method;
328 if (sm == ACPI_TPM2_MEMORY_MAPPED)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300329 return -ENODEV;
330
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700331 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
332 if (!priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800333 return -ENOMEM;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800334
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800335 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
336 * report only ACPI start but in practice seems to require both
337 * ACPI start and CRB start.
338 */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700339 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800340 !strcmp(acpi_device_hid(device), "MSFT0101"))
341 priv->flags |= CRB_FL_CRB_START;
342
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700343 if (sm == ACPI_TPM2_START_METHOD ||
344 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800345 priv->flags |= CRB_FL_ACPI_START;
346
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700347 rc = crb_map_io(device, priv, buf);
Jason Gunthorpe25112042015-11-25 14:05:32 -0700348 if (rc)
349 return rc;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800350
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700351 return crb_init(device, priv);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800352}
353
354static int crb_acpi_remove(struct acpi_device *device)
355{
356 struct device *dev = &device->dev;
357 struct tpm_chip *chip = dev_get_drvdata(dev);
358
Jarkko Sakkinen99cda8c2016-02-18 22:11:29 +0200359 tpm_chip_unregister(chip);
360
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800361 return 0;
362}
363
364static struct acpi_device_id crb_device_ids[] = {
365 {"MSFT0101", 0},
366 {"", 0},
367};
368MODULE_DEVICE_TABLE(acpi, crb_device_ids);
369
370static struct acpi_driver crb_acpi_driver = {
371 .name = "tpm_crb",
372 .ids = crb_device_ids,
373 .ops = {
374 .add = crb_acpi_add,
375 .remove = crb_acpi_remove,
376 },
377 .drv = {
378 .pm = &crb_pm,
379 },
380};
381
382module_acpi_driver(crb_acpi_driver);
383MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
384MODULE_DESCRIPTION("TPM2 Driver");
385MODULE_VERSION("0.1");
386MODULE_LICENSE("GPL");