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