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 "tpm.h"
23
24 #define ACPI_SIG_TPM2 "TPM2"
25
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
29 };
30
31 enum crb_defaults {
32 CRB_ACPI_START_REVISION_ID = 1,
33 CRB_ACPI_START_INDEX = 1,
34 };
35
36 enum crb_ctrl_req {
37 CRB_CTRL_REQ_CMD_READY = BIT(0),
38 CRB_CTRL_REQ_GO_IDLE = BIT(1),
39 };
40
41 enum crb_ctrl_sts {
42 CRB_CTRL_STS_ERROR = BIT(0),
43 CRB_CTRL_STS_TPM_IDLE = BIT(1),
44 };
45
46 enum crb_start {
47 CRB_START_INVOKE = BIT(0),
48 };
49
50 enum crb_cancel {
51 CRB_CANCEL_INVOKE = BIT(0),
52 };
53
54 struct 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;
62 u32 cmd_pa_low;
63 u32 cmd_pa_high;
64 u32 rsp_size;
65 u64 rsp_pa;
66 } __packed;
67
68 enum crb_status {
69 CRB_DRV_STS_COMPLETE = BIT(0),
70 };
71
72 enum crb_flags {
73 CRB_FL_ACPI_START = BIT(0),
74 CRB_FL_CRB_START = BIT(1),
75 };
76
77 struct crb_priv {
78 unsigned int flags;
79 void __iomem *iobase;
80 struct crb_control_area __iomem *cca;
81 u8 __iomem *cmd;
82 u8 __iomem *rsp;
83 u32 cmd_size;
84 };
85
86 static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
87
crb_status(struct tpm_chip * chip)88 static u8 crb_status(struct tpm_chip *chip)
89 {
90 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
91 u8 sts = 0;
92
93 if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
94 CRB_START_INVOKE)
95 sts |= CRB_DRV_STS_COMPLETE;
96
97 return sts;
98 }
99
crb_recv(struct tpm_chip * chip,u8 * buf,size_t count)100 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
101 {
102 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
103 unsigned int expected;
104
105 /* sanity check */
106 if (count < 6)
107 return -EIO;
108
109 if (ioread32(&priv->cca->sts) & CRB_CTRL_STS_ERROR)
110 return -EIO;
111
112 memcpy_fromio(buf, priv->rsp, 6);
113 expected = be32_to_cpup((__be32 *) &buf[2]);
114 if (expected > count || expected < 6)
115 return -EIO;
116
117 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
118
119 return expected;
120 }
121
crb_do_acpi_start(struct tpm_chip * chip)122 static int crb_do_acpi_start(struct tpm_chip *chip)
123 {
124 union acpi_object *obj;
125 int rc;
126
127 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
128 CRB_ACPI_START_UUID,
129 CRB_ACPI_START_REVISION_ID,
130 CRB_ACPI_START_INDEX,
131 NULL);
132 if (!obj)
133 return -ENXIO;
134 rc = obj->integer.value == 0 ? 0 : -ENXIO;
135 ACPI_FREE(obj);
136 return rc;
137 }
138
crb_send(struct tpm_chip * chip,u8 * buf,size_t len)139 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
140 {
141 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
142 int rc = 0;
143
144 /* Zero the cancel register so that the next command will not get
145 * canceled.
146 */
147 iowrite32(0, &priv->cca->cancel);
148
149 if (len > priv->cmd_size) {
150 dev_err(&chip->dev, "invalid command count value %zd %d\n",
151 len, priv->cmd_size);
152 return -E2BIG;
153 }
154
155 memcpy_toio(priv->cmd, buf, len);
156
157 /* Make sure that cmd is populated before issuing start. */
158 wmb();
159
160 if (priv->flags & CRB_FL_CRB_START)
161 iowrite32(CRB_START_INVOKE, &priv->cca->start);
162
163 if (priv->flags & CRB_FL_ACPI_START)
164 rc = crb_do_acpi_start(chip);
165
166 return rc;
167 }
168
crb_cancel(struct tpm_chip * chip)169 static void crb_cancel(struct tpm_chip *chip)
170 {
171 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
172
173 iowrite32(CRB_CANCEL_INVOKE, &priv->cca->cancel);
174
175 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
176 dev_err(&chip->dev, "ACPI Start failed\n");
177 }
178
crb_req_canceled(struct tpm_chip * chip,u8 status)179 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
180 {
181 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
182 u32 cancel = ioread32(&priv->cca->cancel);
183
184 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
185 }
186
187 static const struct tpm_class_ops tpm_crb = {
188 .flags = TPM_OPS_AUTO_STARTUP,
189 .status = crb_status,
190 .recv = crb_recv,
191 .send = crb_send,
192 .cancel = crb_cancel,
193 .req_canceled = crb_req_canceled,
194 .req_complete_mask = CRB_DRV_STS_COMPLETE,
195 .req_complete_val = CRB_DRV_STS_COMPLETE,
196 };
197
crb_init(struct acpi_device * device,struct crb_priv * priv)198 static int crb_init(struct acpi_device *device, struct crb_priv *priv)
199 {
200 struct tpm_chip *chip;
201
202 chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
203 if (IS_ERR(chip))
204 return PTR_ERR(chip);
205
206 dev_set_drvdata(&chip->dev, priv);
207 chip->acpi_dev_handle = device->handle;
208 chip->flags = TPM_CHIP_FLAG_TPM2;
209
210 return tpm_chip_register(chip);
211 }
212
crb_check_resource(struct acpi_resource * ares,void * data)213 static int crb_check_resource(struct acpi_resource *ares, void *data)
214 {
215 struct resource *io_res = data;
216 struct resource res;
217
218 if (acpi_dev_resource_memory(ares, &res)) {
219 *io_res = res;
220 io_res->name = NULL;
221 }
222
223 return 1;
224 }
225
crb_map_res(struct device * dev,struct crb_priv * priv,struct resource * io_res,u64 start,u32 size)226 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
227 struct resource *io_res, u64 start, u32 size)
228 {
229 struct resource new_res = {
230 .start = start,
231 .end = start + size - 1,
232 .flags = IORESOURCE_MEM,
233 };
234
235 /* Detect a 64 bit address on a 32 bit system */
236 if (start != new_res.start)
237 return (void __iomem *) ERR_PTR(-EINVAL);
238
239 if (!resource_contains(io_res, &new_res))
240 return devm_ioremap_resource(dev, &new_res);
241
242 return priv->iobase + (new_res.start - io_res->start);
243 }
244
crb_map_io(struct acpi_device * device,struct crb_priv * priv,struct acpi_table_tpm2 * buf)245 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
246 struct acpi_table_tpm2 *buf)
247 {
248 struct list_head resources;
249 struct resource io_res;
250 struct device *dev = &device->dev;
251 u64 cmd_pa;
252 u32 cmd_size;
253 u64 rsp_pa;
254 u32 rsp_size;
255 int ret;
256
257 INIT_LIST_HEAD(&resources);
258 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
259 &io_res);
260 if (ret < 0)
261 return ret;
262 acpi_dev_free_resource_list(&resources);
263
264 if (resource_type(&io_res) != IORESOURCE_MEM) {
265 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
266 return -EINVAL;
267 }
268
269 priv->iobase = devm_ioremap_resource(dev, &io_res);
270 if (IS_ERR(priv->iobase))
271 return PTR_ERR(priv->iobase);
272
273 priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
274 sizeof(struct crb_control_area));
275 if (IS_ERR(priv->cca))
276 return PTR_ERR(priv->cca);
277
278 cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
279 (u64) ioread32(&priv->cca->cmd_pa_low);
280 cmd_size = ioread32(&priv->cca->cmd_size);
281 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
282 if (IS_ERR(priv->cmd))
283 return PTR_ERR(priv->cmd);
284
285 memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
286 rsp_pa = le64_to_cpu(rsp_pa);
287 rsp_size = ioread32(&priv->cca->rsp_size);
288
289 if (cmd_pa != rsp_pa) {
290 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
291 return PTR_ERR_OR_ZERO(priv->rsp);
292 }
293
294 /* According to the PTP specification, overlapping command and response
295 * buffer sizes must be identical.
296 */
297 if (cmd_size != rsp_size) {
298 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
299 return -EINVAL;
300 }
301 priv->cmd_size = cmd_size;
302
303 priv->rsp = priv->cmd;
304 return 0;
305 }
306
crb_acpi_add(struct acpi_device * device)307 static int crb_acpi_add(struct acpi_device *device)
308 {
309 struct acpi_table_tpm2 *buf;
310 struct crb_priv *priv;
311 struct device *dev = &device->dev;
312 acpi_status status;
313 u32 sm;
314 int rc;
315
316 status = acpi_get_table(ACPI_SIG_TPM2, 1,
317 (struct acpi_table_header **) &buf);
318 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
319 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
320 return -EINVAL;
321 }
322
323 /* Should the FIFO driver handle this? */
324 sm = buf->start_method;
325 if (sm == ACPI_TPM2_MEMORY_MAPPED)
326 return -ENODEV;
327
328 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
329 if (!priv)
330 return -ENOMEM;
331
332 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
333 * report only ACPI start but in practice seems to require both
334 * ACPI start and CRB start.
335 */
336 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
337 !strcmp(acpi_device_hid(device), "MSFT0101"))
338 priv->flags |= CRB_FL_CRB_START;
339
340 if (sm == ACPI_TPM2_START_METHOD ||
341 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
342 priv->flags |= CRB_FL_ACPI_START;
343
344 rc = crb_map_io(device, priv, buf);
345 if (rc)
346 return rc;
347
348 return crb_init(device, priv);
349 }
350
crb_acpi_remove(struct acpi_device * device)351 static int crb_acpi_remove(struct acpi_device *device)
352 {
353 struct device *dev = &device->dev;
354 struct tpm_chip *chip = dev_get_drvdata(dev);
355
356 tpm_chip_unregister(chip);
357
358 return 0;
359 }
360
361 static struct acpi_device_id crb_device_ids[] = {
362 {"MSFT0101", 0},
363 {"", 0},
364 };
365 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
366
367 static struct acpi_driver crb_acpi_driver = {
368 .name = "tpm_crb",
369 .ids = crb_device_ids,
370 .ops = {
371 .add = crb_acpi_add,
372 .remove = crb_acpi_remove,
373 },
374 .drv = {
375 .pm = &crb_pm,
376 },
377 };
378
379 module_acpi_driver(crb_acpi_driver);
380 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
381 MODULE_DESCRIPTION("TPM2 Driver");
382 MODULE_VERSION("0.1");
383 MODULE_LICENSE("GPL");
384