]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/char/tpm/tpm_crb.c
tpm crb: Work around BIOS's that report the wrong ACPI region size
[linux.git] / drivers / char / tpm / tpm_crb.c
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/pm_runtime.h>
23 #include "tpm.h"
24
25 #define ACPI_SIG_TPM2 "TPM2"
26
27 static 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
32 enum crb_defaults {
33         CRB_ACPI_START_REVISION_ID = 1,
34         CRB_ACPI_START_INDEX = 1,
35 };
36
37 enum crb_ctrl_req {
38         CRB_CTRL_REQ_CMD_READY  = BIT(0),
39         CRB_CTRL_REQ_GO_IDLE    = BIT(1),
40 };
41
42 enum crb_ctrl_sts {
43         CRB_CTRL_STS_ERROR      = BIT(0),
44         CRB_CTRL_STS_TPM_IDLE   = BIT(1),
45 };
46
47 enum crb_start {
48         CRB_START_INVOKE        = BIT(0),
49 };
50
51 enum crb_cancel {
52         CRB_CANCEL_INVOKE       = BIT(0),
53 };
54
55 struct 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;
63         u32 cmd_pa_low;
64         u32 cmd_pa_high;
65         u32 rsp_size;
66         u64 rsp_pa;
67 } __packed;
68
69 enum crb_status {
70         CRB_DRV_STS_COMPLETE    = BIT(0),
71 };
72
73 enum crb_flags {
74         CRB_FL_ACPI_START       = BIT(0),
75         CRB_FL_CRB_START        = BIT(1),
76 };
77
78 struct crb_priv {
79         unsigned int flags;
80         void __iomem *iobase;
81         struct crb_control_area __iomem *cca;
82         u8 __iomem *cmd;
83         u8 __iomem *rsp;
84         u32 cmd_size;
85 };
86
87 /**
88  * crb_go_idle - request tpm crb device to go the idle state
89  *
90  * @dev:  crb device
91  * @priv: crb private data
92  *
93  * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
94  * The device should respond within TIMEOUT_C by clearing the bit.
95  * Anyhow, we do not wait here as a consequent CMD_READY request
96  * will be handled correctly even if idle was not completed.
97  *
98  * The function does nothing for devices with ACPI-start method.
99  *
100  * Return: 0 always
101  */
102 static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
103 {
104         if (priv->flags & CRB_FL_ACPI_START)
105                 return 0;
106
107         iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->cca->req);
108         /* we don't really care when this settles */
109
110         return 0;
111 }
112
113 /**
114  * crb_cmd_ready - request tpm crb device to enter ready state
115  *
116  * @dev:  crb device
117  * @priv: crb private data
118  *
119  * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
120  * and poll till the device acknowledge it by clearing the bit.
121  * The device should respond within TIMEOUT_C.
122  *
123  * The function does nothing for devices with ACPI-start method
124  *
125  * Return: 0 on success -ETIME on timeout;
126  */
127 static int __maybe_unused crb_cmd_ready(struct device *dev,
128                                         struct crb_priv *priv)
129 {
130         ktime_t stop, start;
131
132         if (priv->flags & CRB_FL_ACPI_START)
133                 return 0;
134
135         iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->cca->req);
136
137         start = ktime_get();
138         stop = ktime_add(start, ms_to_ktime(TPM2_TIMEOUT_C));
139         do {
140                 if (!(ioread32(&priv->cca->req) & CRB_CTRL_REQ_CMD_READY))
141                         return 0;
142                 usleep_range(50, 100);
143         } while (ktime_before(ktime_get(), stop));
144
145         if (ioread32(&priv->cca->req) & CRB_CTRL_REQ_CMD_READY) {
146                 dev_warn(dev, "cmdReady timed out\n");
147                 return -ETIME;
148         }
149
150         return 0;
151 }
152
153 static u8 crb_status(struct tpm_chip *chip)
154 {
155         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
156         u8 sts = 0;
157
158         if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
159             CRB_START_INVOKE)
160                 sts |= CRB_DRV_STS_COMPLETE;
161
162         return sts;
163 }
164
165 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
166 {
167         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
168         unsigned int expected;
169
170         /* sanity check */
171         if (count < 6)
172                 return -EIO;
173
174         if (ioread32(&priv->cca->sts) & CRB_CTRL_STS_ERROR)
175                 return -EIO;
176
177         memcpy_fromio(buf, priv->rsp, 6);
178         expected = be32_to_cpup((__be32 *) &buf[2]);
179
180         if (expected > count)
181                 return -EIO;
182
183         memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
184
185         return expected;
186 }
187
188 static int crb_do_acpi_start(struct tpm_chip *chip)
189 {
190         union acpi_object *obj;
191         int rc;
192
193         obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
194                                 CRB_ACPI_START_UUID,
195                                 CRB_ACPI_START_REVISION_ID,
196                                 CRB_ACPI_START_INDEX,
197                                 NULL);
198         if (!obj)
199                 return -ENXIO;
200         rc = obj->integer.value == 0 ? 0 : -ENXIO;
201         ACPI_FREE(obj);
202         return rc;
203 }
204
205 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
206 {
207         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
208         int rc = 0;
209
210         /* Zero the cancel register so that the next command will not get
211          * canceled.
212          */
213         iowrite32(0, &priv->cca->cancel);
214
215         if (len > priv->cmd_size) {
216                 dev_err(&chip->dev, "invalid command count value %zd %d\n",
217                         len, priv->cmd_size);
218                 return -E2BIG;
219         }
220
221         memcpy_toio(priv->cmd, buf, len);
222
223         /* Make sure that cmd is populated before issuing start. */
224         wmb();
225
226         if (priv->flags & CRB_FL_CRB_START)
227                 iowrite32(CRB_START_INVOKE, &priv->cca->start);
228
229         if (priv->flags & CRB_FL_ACPI_START)
230                 rc = crb_do_acpi_start(chip);
231
232         return rc;
233 }
234
235 static void crb_cancel(struct tpm_chip *chip)
236 {
237         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
238
239         iowrite32(CRB_CANCEL_INVOKE, &priv->cca->cancel);
240
241         if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
242                 dev_err(&chip->dev, "ACPI Start failed\n");
243 }
244
245 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
246 {
247         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
248         u32 cancel = ioread32(&priv->cca->cancel);
249
250         return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
251 }
252
253 static const struct tpm_class_ops tpm_crb = {
254         .flags = TPM_OPS_AUTO_STARTUP,
255         .status = crb_status,
256         .recv = crb_recv,
257         .send = crb_send,
258         .cancel = crb_cancel,
259         .req_canceled = crb_req_canceled,
260         .req_complete_mask = CRB_DRV_STS_COMPLETE,
261         .req_complete_val = CRB_DRV_STS_COMPLETE,
262 };
263
264 static int crb_check_resource(struct acpi_resource *ares, void *data)
265 {
266         struct resource *io_res = data;
267         struct resource_win win;
268         struct resource *res = &(win.res);
269
270         if (acpi_dev_resource_memory(ares, res) ||
271             acpi_dev_resource_address_space(ares, &win)) {
272                 *io_res = *res;
273                 io_res->name = NULL;
274         }
275
276         return 1;
277 }
278
279 static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
280                                  struct resource *io_res, u64 start, u32 size)
281 {
282         struct resource new_res = {
283                 .start  = start,
284                 .end    = start + size - 1,
285                 .flags  = IORESOURCE_MEM,
286         };
287
288         /* Detect a 64 bit address on a 32 bit system */
289         if (start != new_res.start)
290                 return (void __iomem *) ERR_PTR(-EINVAL);
291
292         if (!resource_contains(io_res, &new_res))
293                 return devm_ioremap_resource(dev, &new_res);
294
295         return priv->iobase + (new_res.start - io_res->start);
296 }
297
298 /*
299  * Work around broken BIOSs that return inconsistent values from the ACPI
300  * region vs the registers. Trust the ACPI region. Such broken systems
301  * probably cannot send large TPM commands since the buffer will be truncated.
302  */
303 static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
304                               u64 start, u64 size)
305 {
306         if (io_res->start > start || io_res->end < start)
307                 return size;
308
309         if (start + size - 1 <= io_res->end)
310                 return size;
311
312         dev_err(dev,
313                 FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
314                 io_res, start, size);
315
316         return io_res->end - start + 1;
317 }
318
319 static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
320                       struct acpi_table_tpm2 *buf)
321 {
322         struct list_head resources;
323         struct resource io_res;
324         struct device *dev = &device->dev;
325         u32 pa_high, pa_low;
326         u64 cmd_pa;
327         u32 cmd_size;
328         u64 rsp_pa;
329         u32 rsp_size;
330         int ret;
331
332         INIT_LIST_HEAD(&resources);
333         ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
334                                      &io_res);
335         if (ret < 0)
336                 return ret;
337         acpi_dev_free_resource_list(&resources);
338
339         if (resource_type(&io_res) != IORESOURCE_MEM) {
340                 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
341                 return -EINVAL;
342         }
343
344         priv->iobase = devm_ioremap_resource(dev, &io_res);
345         if (IS_ERR(priv->iobase))
346                 return PTR_ERR(priv->iobase);
347
348         priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
349                                 sizeof(struct crb_control_area));
350         if (IS_ERR(priv->cca))
351                 return PTR_ERR(priv->cca);
352
353         /*
354          * PTT HW bug w/a: wake up the device to access
355          * possibly not retained registers.
356          */
357         ret = crb_cmd_ready(dev, priv);
358         if (ret)
359                 return ret;
360
361         pa_high = ioread32(&priv->cca->cmd_pa_high);
362         pa_low  = ioread32(&priv->cca->cmd_pa_low);
363         cmd_pa = ((u64)pa_high << 32) | pa_low;
364         cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
365                                       ioread32(&priv->cca->cmd_size));
366
367         dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
368                 pa_high, pa_low, cmd_size);
369
370         priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
371         if (IS_ERR(priv->cmd)) {
372                 ret = PTR_ERR(priv->cmd);
373                 goto out;
374         }
375
376         memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
377         rsp_pa = le64_to_cpu(rsp_pa);
378         rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
379                                       ioread32(&priv->cca->rsp_size));
380
381         if (cmd_pa != rsp_pa) {
382                 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
383                 ret = PTR_ERR_OR_ZERO(priv->rsp);
384                 goto out;
385         }
386
387         /* According to the PTP specification, overlapping command and response
388          * buffer sizes must be identical.
389          */
390         if (cmd_size != rsp_size) {
391                 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
392                 ret = -EINVAL;
393                 goto out;
394         }
395
396         priv->cmd_size = cmd_size;
397
398         priv->rsp = priv->cmd;
399
400 out:
401         crb_go_idle(dev, priv);
402
403         return ret;
404 }
405
406 static int crb_acpi_add(struct acpi_device *device)
407 {
408         struct acpi_table_tpm2 *buf;
409         struct crb_priv *priv;
410         struct tpm_chip *chip;
411         struct device *dev = &device->dev;
412         acpi_status status;
413         u32 sm;
414         int rc;
415
416         status = acpi_get_table(ACPI_SIG_TPM2, 1,
417                                 (struct acpi_table_header **) &buf);
418         if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
419                 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
420                 return -EINVAL;
421         }
422
423         /* Should the FIFO driver handle this? */
424         sm = buf->start_method;
425         if (sm == ACPI_TPM2_MEMORY_MAPPED)
426                 return -ENODEV;
427
428         priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
429         if (!priv)
430                 return -ENOMEM;
431
432         /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
433          * report only ACPI start but in practice seems to require both
434          * ACPI start and CRB start.
435          */
436         if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
437             !strcmp(acpi_device_hid(device), "MSFT0101"))
438                 priv->flags |= CRB_FL_CRB_START;
439
440         if (sm == ACPI_TPM2_START_METHOD ||
441             sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
442                 priv->flags |= CRB_FL_ACPI_START;
443
444         rc = crb_map_io(device, priv, buf);
445         if (rc)
446                 return rc;
447
448         chip = tpmm_chip_alloc(dev, &tpm_crb);
449         if (IS_ERR(chip))
450                 return PTR_ERR(chip);
451
452         dev_set_drvdata(&chip->dev, priv);
453         chip->acpi_dev_handle = device->handle;
454         chip->flags = TPM_CHIP_FLAG_TPM2;
455
456         rc  = crb_cmd_ready(dev, priv);
457         if (rc)
458                 return rc;
459
460         pm_runtime_get_noresume(dev);
461         pm_runtime_set_active(dev);
462         pm_runtime_enable(dev);
463
464         rc = tpm_chip_register(chip);
465         if (rc) {
466                 crb_go_idle(dev, priv);
467                 pm_runtime_put_noidle(dev);
468                 pm_runtime_disable(dev);
469                 return rc;
470         }
471
472         pm_runtime_put(dev);
473
474         return 0;
475 }
476
477 static int crb_acpi_remove(struct acpi_device *device)
478 {
479         struct device *dev = &device->dev;
480         struct tpm_chip *chip = dev_get_drvdata(dev);
481
482         tpm_chip_unregister(chip);
483
484         pm_runtime_disable(dev);
485
486         return 0;
487 }
488
489 #ifdef CONFIG_PM
490 static int crb_pm_runtime_suspend(struct device *dev)
491 {
492         struct tpm_chip *chip = dev_get_drvdata(dev);
493         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
494
495         return crb_go_idle(dev, priv);
496 }
497
498 static int crb_pm_runtime_resume(struct device *dev)
499 {
500         struct tpm_chip *chip = dev_get_drvdata(dev);
501         struct crb_priv *priv = dev_get_drvdata(&chip->dev);
502
503         return crb_cmd_ready(dev, priv);
504 }
505 #endif /* CONFIG_PM */
506
507 static const struct dev_pm_ops crb_pm = {
508         SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume)
509         SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
510 };
511
512 static struct acpi_device_id crb_device_ids[] = {
513         {"MSFT0101", 0},
514         {"", 0},
515 };
516 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
517
518 static struct acpi_driver crb_acpi_driver = {
519         .name = "tpm_crb",
520         .ids = crb_device_ids,
521         .ops = {
522                 .add = crb_acpi_add,
523                 .remove = crb_acpi_remove,
524         },
525         .drv = {
526                 .pm = &crb_pm,
527         },
528 };
529
530 module_acpi_driver(crb_acpi_driver);
531 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
532 MODULE_DESCRIPTION("TPM2 Driver");
533 MODULE_VERSION("0.1");
534 MODULE_LICENSE("GPL");