tpm_crb: Use the common ACPI definition of struct acpi_tpm2
[cascardo/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/platform_device.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_ca_request {
38         CRB_CA_REQ_GO_IDLE      = BIT(0),
39         CRB_CA_REQ_CMD_READY    = BIT(1),
40 };
41
42 enum crb_ca_status {
43         CRB_CA_STS_ERROR        = BIT(0),
44         CRB_CA_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_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         struct crb_control_area __iomem *cca;
81         u8 __iomem *cmd;
82         u8 __iomem *rsp;
83 };
84
85 static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
86
87 static u8 crb_status(struct tpm_chip *chip)
88 {
89         struct crb_priv *priv = chip->vendor.priv;
90         u8 sts = 0;
91
92         if ((le32_to_cpu(ioread32(&priv->cca->start)) & CRB_START_INVOKE) !=
93             CRB_START_INVOKE)
94                 sts |= CRB_STS_COMPLETE;
95
96         return sts;
97 }
98
99 static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
100 {
101         struct crb_priv *priv = chip->vendor.priv;
102         unsigned int expected;
103
104         /* sanity check */
105         if (count < 6)
106                 return -EIO;
107
108         if (le32_to_cpu(ioread32(&priv->cca->sts)) & CRB_CA_STS_ERROR)
109                 return -EIO;
110
111         memcpy_fromio(buf, priv->rsp, 6);
112         expected = be32_to_cpup((__be32 *) &buf[2]);
113
114         if (expected > count)
115                 return -EIO;
116
117         memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
118
119         return expected;
120 }
121
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
139 static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
140 {
141         struct crb_priv *priv = chip->vendor.priv;
142         int rc = 0;
143
144         if (len > le32_to_cpu(ioread32(&priv->cca->cmd_size))) {
145                 dev_err(&chip->dev,
146                         "invalid command count value %x %zx\n",
147                         (unsigned int) len,
148                         (size_t) le32_to_cpu(ioread32(&priv->cca->cmd_size)));
149                 return -E2BIG;
150         }
151
152         memcpy_toio(priv->cmd, buf, len);
153
154         /* Make sure that cmd is populated before issuing start. */
155         wmb();
156
157         if (priv->flags & CRB_FL_CRB_START)
158                 iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
159
160         if (priv->flags & CRB_FL_ACPI_START)
161                 rc = crb_do_acpi_start(chip);
162
163         return rc;
164 }
165
166 static void crb_cancel(struct tpm_chip *chip)
167 {
168         struct crb_priv *priv = chip->vendor.priv;
169
170         iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
171
172         /* Make sure that cmd is populated before issuing cancel. */
173         wmb();
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         iowrite32(0, &priv->cca->cancel);
179 }
180
181 static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
182 {
183         struct crb_priv *priv = chip->vendor.priv;
184         u32 cancel = le32_to_cpu(ioread32(&priv->cca->cancel));
185
186         return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
187 }
188
189 static const struct tpm_class_ops tpm_crb = {
190         .status = crb_status,
191         .recv = crb_recv,
192         .send = crb_send,
193         .cancel = crb_cancel,
194         .req_canceled = crb_req_canceled,
195         .req_complete_mask = CRB_STS_COMPLETE,
196         .req_complete_val = CRB_STS_COMPLETE,
197 };
198
199 static int crb_acpi_add(struct acpi_device *device)
200 {
201         struct tpm_chip *chip;
202         struct acpi_table_tpm2 *buf;
203         struct crb_priv *priv;
204         struct device *dev = &device->dev;
205         acpi_status status;
206         u32 sm;
207         u64 pa;
208         int rc;
209
210         status = acpi_get_table(ACPI_SIG_TPM2, 1,
211                                 (struct acpi_table_header **) &buf);
212         if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
213                 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
214                 return -ENODEV;
215         }
216
217         /* Should the FIFO driver handle this? */
218         sm = buf->start_method;
219         if (sm == ACPI_TPM2_MEMORY_MAPPED)
220                 return -ENODEV;
221
222         chip = tpmm_chip_alloc(dev, &tpm_crb);
223         if (IS_ERR(chip))
224                 return PTR_ERR(chip);
225
226         chip->flags = TPM_CHIP_FLAG_TPM2;
227
228         priv = (struct crb_priv *) devm_kzalloc(dev, sizeof(struct crb_priv),
229                                                 GFP_KERNEL);
230         if (!priv) {
231                 dev_err(dev, "failed to devm_kzalloc for private data\n");
232                 return -ENOMEM;
233         }
234
235         /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
236          * report only ACPI start but in practice seems to require both
237          * ACPI start and CRB start.
238          */
239         if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
240             !strcmp(acpi_device_hid(device), "MSFT0101"))
241                 priv->flags |= CRB_FL_CRB_START;
242
243         if (sm == ACPI_TPM2_START_METHOD ||
244             sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
245                 priv->flags |= CRB_FL_ACPI_START;
246
247         priv->cca = (struct crb_control_area __iomem *)
248                 devm_ioremap_nocache(dev, buf->control_address, 0x1000);
249         if (!priv->cca) {
250                 dev_err(dev, "ioremap of the control area failed\n");
251                 return -ENOMEM;
252         }
253
254         pa = ((u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_high)) << 32) |
255                 (u64) le32_to_cpu(ioread32(&priv->cca->cmd_pa_low));
256         priv->cmd = devm_ioremap_nocache(dev, pa,
257                                          ioread32(&priv->cca->cmd_size));
258         if (!priv->cmd) {
259                 dev_err(dev, "ioremap of the command buffer failed\n");
260                 return -ENOMEM;
261         }
262
263         memcpy_fromio(&pa, &priv->cca->rsp_pa, 8);
264         pa = le64_to_cpu(pa);
265         priv->rsp = devm_ioremap_nocache(dev, pa,
266                                          ioread32(&priv->cca->rsp_size));
267         if (!priv->rsp) {
268                 dev_err(dev, "ioremap of the response buffer failed\n");
269                 return -ENOMEM;
270         }
271
272         chip->vendor.priv = priv;
273
274         rc = tpm_get_timeouts(chip);
275         if (rc)
276                 return rc;
277
278         chip->acpi_dev_handle = device->handle;
279
280         rc = tpm2_do_selftest(chip);
281         if (rc)
282                 return rc;
283
284         return tpm_chip_register(chip);
285 }
286
287 static int crb_acpi_remove(struct acpi_device *device)
288 {
289         struct device *dev = &device->dev;
290         struct tpm_chip *chip = dev_get_drvdata(dev);
291
292         tpm_chip_unregister(chip);
293
294         if (chip->flags & TPM_CHIP_FLAG_TPM2)
295                 tpm2_shutdown(chip, TPM2_SU_CLEAR);
296
297         return 0;
298 }
299
300 static struct acpi_device_id crb_device_ids[] = {
301         {"MSFT0101", 0},
302         {"", 0},
303 };
304 MODULE_DEVICE_TABLE(acpi, crb_device_ids);
305
306 static struct acpi_driver crb_acpi_driver = {
307         .name = "tpm_crb",
308         .ids = crb_device_ids,
309         .ops = {
310                 .add = crb_acpi_add,
311                 .remove = crb_acpi_remove,
312         },
313         .drv = {
314                 .pm = &crb_pm,
315         },
316 };
317
318 module_acpi_driver(crb_acpi_driver);
319 MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
320 MODULE_DESCRIPTION("TPM2 Driver");
321 MODULE_VERSION("0.1");
322 MODULE_LICENSE("GPL");