blob: 18b5b83af36d61f7b15bb947b1193516bf8e269c [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>
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080022#include "tpm.h"
23
24#define ACPI_SIG_TPM2 "TPM2"
25
26static 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
29};
30
31enum crb_defaults {
32 CRB_ACPI_START_REVISION_ID = 1,
33 CRB_ACPI_START_INDEX = 1,
34};
35
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030036enum crb_ctrl_req {
Jarkko Sakkinenf39a9e92016-09-02 22:34:20 +030037 CRB_CTRL_REQ_CMD_READY = BIT(0),
38 CRB_CTRL_REQ_GO_IDLE = BIT(1),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080039};
40
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030041enum crb_ctrl_sts {
42 CRB_CTRL_STS_ERROR = BIT(0),
43 CRB_CTRL_STS_TPM_IDLE = BIT(1),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080044};
45
46enum crb_start {
47 CRB_START_INVOKE = BIT(0),
48};
49
50enum crb_cancel {
51 CRB_CANCEL_INVOKE = BIT(0),
52};
53
54struct crb_control_area {
55 u32 req;
56 u32 sts;
57 u32 cancel;
58 u32 start;
59 u32 int_enable;
60 u32 int_sts;
61 u32 cmd_size;
Jarkko Sakkinen149789c2015-09-15 20:05:40 +030062 u32 cmd_pa_low;
63 u32 cmd_pa_high;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080064 u32 rsp_size;
65 u64 rsp_pa;
66} __packed;
67
68enum crb_status {
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030069 CRB_DRV_STS_COMPLETE = BIT(0),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080070};
71
72enum crb_flags {
73 CRB_FL_ACPI_START = BIT(0),
74 CRB_FL_CRB_START = BIT(1),
75};
76
77struct crb_priv {
78 unsigned int flags;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -070079 void __iomem *iobase;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080080 struct crb_control_area __iomem *cca;
81 u8 __iomem *cmd;
82 u8 __iomem *rsp;
Tomas Winkleraa77ea02016-09-12 13:52:10 +030083 u32 cmd_size;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080084};
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 Gunthorpe1e3ed59d2016-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
Tomas Winkleraa77ea02016-09-12 13:52:10 +0300150 if (len > priv->cmd_size) {
151 dev_err(&chip->dev, "invalid command count value %zd %d\n",
152 len, priv->cmd_size);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800153 return -E2BIG;
154 }
155
156 memcpy_toio(priv->cmd, buf, len);
157
158 /* Make sure that cmd is populated before issuing start. */
159 wmb();
160
161 if (priv->flags & CRB_FL_CRB_START)
162 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
163
164 if (priv->flags & CRB_FL_ACPI_START)
165 rc = crb_do_acpi_start(chip);
166
167 return rc;
168}
169
170static void crb_cancel(struct tpm_chip *chip)
171{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200172 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800173
174 iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
175
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800176 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
177 dev_err(&chip->dev, "ACPI Start failed\n");
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800178}
179
180static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
181{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200182 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jason Gunthorpe1e3ed59d2016-01-07 17:36:25 -0700183 u32 cancel = ioread32(&priv->cca->cancel);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800184
185 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
186}
187
188static const struct tpm_class_ops tpm_crb = {
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600189 .flags = TPM_OPS_AUTO_STARTUP,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800190 .status = crb_status,
191 .recv = crb_recv,
192 .send = crb_send,
193 .cancel = crb_cancel,
194 .req_canceled = crb_req_canceled,
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +0300195 .req_complete_mask = CRB_DRV_STS_COMPLETE,
196 .req_complete_val = CRB_DRV_STS_COMPLETE,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800197};
198
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700199static int crb_init(struct acpi_device *device, struct crb_priv *priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800200{
201 struct tpm_chip *chip;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700202
203 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
204 if (IS_ERR(chip))
205 return PTR_ERR(chip);
206
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200207 dev_set_drvdata(&chip->dev, priv);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700208 chip->acpi_dev_handle = device->handle;
209 chip->flags = TPM_CHIP_FLAG_TPM2;
210
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700211 return tpm_chip_register(chip);
212}
213
214static int crb_check_resource(struct acpi_resource *ares, void *data)
215{
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200216 struct resource *io_res = data;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700217 struct resource res;
218
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200219 if (acpi_dev_resource_memory(ares, &res)) {
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200220 *io_res = res;
221 io_res->name = NULL;
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200222 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700223
224 return 1;
225}
226
227static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200228 struct resource *io_res, u64 start, u32 size)
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700229{
230 struct resource new_res = {
231 .start = start,
232 .end = start + size - 1,
233 .flags = IORESOURCE_MEM,
234 };
235
236 /* Detect a 64 bit address on a 32 bit system */
237 if (start != new_res.start)
Jarkko Sakkinenf786b752016-06-17 16:39:29 +0200238 return (void __iomem *) ERR_PTR(-EINVAL);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700239
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200240 if (!resource_contains(io_res, &new_res))
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700241 return devm_ioremap_resource(dev, &new_res);
242
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200243 return priv->iobase + (new_res.start - io_res->start);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700244}
245
246static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
247 struct acpi_table_tpm2 *buf)
248{
249 struct list_head resources;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200250 struct resource io_res;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700251 struct device *dev = &device->dev;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300252 u64 cmd_pa;
253 u32 cmd_size;
254 u64 rsp_pa;
255 u32 rsp_size;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700256 int ret;
257
258 INIT_LIST_HEAD(&resources);
259 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200260 &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700261 if (ret < 0)
262 return ret;
263 acpi_dev_free_resource_list(&resources);
264
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200265 if (resource_type(&io_res) != IORESOURCE_MEM) {
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700266 dev_err(dev,
267 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
268 return -EINVAL;
269 }
270
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200271 priv->iobase = devm_ioremap_resource(dev, &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700272 if (IS_ERR(priv->iobase))
273 return PTR_ERR(priv->iobase);
274
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200275 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300276 sizeof(struct crb_control_area));
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700277 if (IS_ERR(priv->cca))
278 return PTR_ERR(priv->cca);
279
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300280 cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
281 (u64) ioread32(&priv->cca->cmd_pa_low);
282 cmd_size = ioread32(&priv->cca->cmd_size);
283 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700284 if (IS_ERR(priv->cmd))
285 return PTR_ERR(priv->cmd);
286
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300287 memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
288 rsp_pa = le64_to_cpu(rsp_pa);
289 rsp_size = ioread32(&priv->cca->rsp_size);
290
291 if (cmd_pa != rsp_pa) {
292 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
293 return PTR_ERR_OR_ZERO(priv->rsp);
294 }
295
296 /* According to the PTP specification, overlapping command and response
297 * buffer sizes must be identical.
298 */
299 if (cmd_size != rsp_size) {
300 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
301 return -EINVAL;
302 }
Tomas Winkleraa77ea02016-09-12 13:52:10 +0300303 priv->cmd_size = cmd_size;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300304
305 priv->rsp = priv->cmd;
306 return 0;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700307}
308
309static int crb_acpi_add(struct acpi_device *device)
310{
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700311 struct acpi_table_tpm2 *buf;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800312 struct crb_priv *priv;
313 struct device *dev = &device->dev;
314 acpi_status status;
315 u32 sm;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800316 int rc;
317
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800318 status = acpi_get_table(ACPI_SIG_TPM2, 1,
319 (struct acpi_table_header **) &buf);
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700320 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
321 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700322 return -EINVAL;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800323 }
324
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300325 /* Should the FIFO driver handle this? */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700326 sm = buf->start_method;
327 if (sm == ACPI_TPM2_MEMORY_MAPPED)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300328 return -ENODEV;
329
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700330 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
331 if (!priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800332 return -ENOMEM;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800333
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800334 /* 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.
337 */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700338 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800339 !strcmp(acpi_device_hid(device), "MSFT0101"))
340 priv->flags |= CRB_FL_CRB_START;
341
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700342 if (sm == ACPI_TPM2_START_METHOD ||
343 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800344 priv->flags |= CRB_FL_ACPI_START;
345
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700346 rc = crb_map_io(device, priv, buf);
Jason Gunthorpe25112042015-11-25 14:05:32 -0700347 if (rc)
348 return rc;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800349
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700350 return crb_init(device, priv);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800351}
352
353static int crb_acpi_remove(struct acpi_device *device)
354{
355 struct device *dev = &device->dev;
356 struct tpm_chip *chip = dev_get_drvdata(dev);
357
Jarkko Sakkinen99cda8c2016-02-18 22:11:29 +0200358 tpm_chip_unregister(chip);
359
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800360 return 0;
361}
362
363static struct acpi_device_id crb_device_ids[] = {
364 {"MSFT0101", 0},
365 {"", 0},
366};
367MODULE_DEVICE_TABLE(acpi, crb_device_ids);
368
369static struct acpi_driver crb_acpi_driver = {
370 .name = "tpm_crb",
371 .ids = crb_device_ids,
372 .ops = {
373 .add = crb_acpi_add,
374 .remove = crb_acpi_remove,
375 },
376 .drv = {
377 .pm = &crb_pm,
378 },
379};
380
381module_acpi_driver(crb_acpi_driver);
382MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
383MODULE_DESCRIPTION("TPM2 Driver");
384MODULE_VERSION("0.1");
385MODULE_LICENSE("GPL");