blob: 1dfc37e33c02de7d86b289021b10625a2c3340a9 [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>
Winkler, Tomase74f2f72016-10-08 14:59:39 +030022#include <linux/pm_runtime.h>
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080023#include "tpm.h"
24
25#define ACPI_SIG_TPM2 "TPM2"
26
27static 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
32enum crb_defaults {
33 CRB_ACPI_START_REVISION_ID = 1,
34 CRB_ACPI_START_INDEX = 1,
35};
36
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030037enum crb_ctrl_req {
Jarkko Sakkinenf39a9e92016-09-02 22:34:20 +030038 CRB_CTRL_REQ_CMD_READY = BIT(0),
39 CRB_CTRL_REQ_GO_IDLE = BIT(1),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080040};
41
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030042enum crb_ctrl_sts {
43 CRB_CTRL_STS_ERROR = BIT(0),
44 CRB_CTRL_STS_TPM_IDLE = BIT(1),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080045};
46
47enum crb_start {
48 CRB_START_INVOKE = BIT(0),
49};
50
51enum crb_cancel {
52 CRB_CANCEL_INVOKE = BIT(0),
53};
54
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +020055struct crb_regs_head {
56 u32 loc_state;
57 u32 reserved1;
58 u32 loc_ctrl;
59 u32 loc_sts;
60 u8 reserved2[32];
61 u64 intf_id;
62 u64 ctrl_ext;
63} __packed;
64
65struct crb_regs_tail {
66 u32 ctrl_req;
67 u32 ctrl_sts;
68 u32 ctrl_cancel;
69 u32 ctrl_start;
70 u32 ctrl_int_enable;
71 u32 ctrl_int_sts;
72 u32 ctrl_cmd_size;
73 u32 ctrl_cmd_pa_low;
74 u32 ctrl_cmd_pa_high;
75 u32 ctrl_rsp_size;
76 u64 ctrl_rsp_pa;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080077} __packed;
78
79enum crb_status {
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +030080 CRB_DRV_STS_COMPLETE = BIT(0),
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080081};
82
83enum crb_flags {
84 CRB_FL_ACPI_START = BIT(0),
85 CRB_FL_CRB_START = BIT(1),
86};
87
88struct crb_priv {
89 unsigned int flags;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -070090 void __iomem *iobase;
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +020091 struct crb_regs_head __iomem *regs_h;
92 struct crb_regs_tail __iomem *regs_t;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080093 u8 __iomem *cmd;
94 u8 __iomem *rsp;
Tomas Winkleraa77ea02016-09-12 13:52:10 +030095 u32 cmd_size;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -080096};
97
Winkler, Tomasba5287b2016-09-15 10:27:38 +030098/**
99 * crb_go_idle - request tpm crb device to go the idle state
100 *
101 * @dev: crb device
102 * @priv: crb private data
103 *
104 * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
105 * The device should respond within TIMEOUT_C by clearing the bit.
106 * Anyhow, we do not wait here as a consequent CMD_READY request
107 * will be handled correctly even if idle was not completed.
108 *
109 * The function does nothing for devices with ACPI-start method.
110 *
111 * Return: 0 always
112 */
113static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
114{
115 if (priv->flags & CRB_FL_ACPI_START)
116 return 0;
117
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200118 iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
Winkler, Tomasba5287b2016-09-15 10:27:38 +0300119 /* we don't really care when this settles */
120
121 return 0;
122}
123
Jarkko Sakkinen38eb24e2017-02-08 13:11:36 +0200124static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
125 unsigned long timeout)
126{
127 ktime_t start;
128 ktime_t stop;
129
130 start = ktime_get();
131 stop = ktime_add(start, ms_to_ktime(timeout));
132
133 do {
134 if ((ioread32(reg) & mask) == value)
135 return true;
136
137 usleep_range(50, 100);
138 } while (ktime_before(ktime_get(), stop));
139
140 return false;
141}
142
Winkler, Tomasba5287b2016-09-15 10:27:38 +0300143/**
144 * crb_cmd_ready - request tpm crb device to enter ready state
145 *
146 * @dev: crb device
147 * @priv: crb private data
148 *
149 * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
150 * and poll till the device acknowledge it by clearing the bit.
151 * The device should respond within TIMEOUT_C.
152 *
153 * The function does nothing for devices with ACPI-start method
154 *
155 * Return: 0 on success -ETIME on timeout;
156 */
157static int __maybe_unused crb_cmd_ready(struct device *dev,
158 struct crb_priv *priv)
159{
Winkler, Tomasba5287b2016-09-15 10:27:38 +0300160 if (priv->flags & CRB_FL_ACPI_START)
161 return 0;
162
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200163 iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
Jarkko Sakkinen38eb24e2017-02-08 13:11:36 +0200164 if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
165 CRB_CTRL_REQ_CMD_READY /* mask */,
166 0, /* value */
167 TPM2_TIMEOUT_C)) {
Winkler, Tomasba5287b2016-09-15 10:27:38 +0300168 dev_warn(dev, "cmdReady timed out\n");
169 return -ETIME;
170 }
171
172 return 0;
173}
174
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800175static u8 crb_status(struct tpm_chip *chip)
176{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200177 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800178 u8 sts = 0;
179
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200180 if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800181 CRB_START_INVOKE)
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +0300182 sts |= CRB_DRV_STS_COMPLETE;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800183
184 return sts;
185}
186
187static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
188{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200189 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800190 unsigned int expected;
191
192 /* sanity check */
193 if (count < 6)
194 return -EIO;
195
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200196 if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800197 return -EIO;
198
199 memcpy_fromio(buf, priv->rsp, 6);
200 expected = be32_to_cpup((__be32 *) &buf[2]);
Jerry Snitselaar8569def2017-03-10 17:46:04 -0700201 if (expected > count || expected < 6)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800202 return -EIO;
203
204 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
205
206 return expected;
207}
208
209static int crb_do_acpi_start(struct tpm_chip *chip)
210{
211 union acpi_object *obj;
212 int rc;
213
214 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
215 CRB_ACPI_START_UUID,
216 CRB_ACPI_START_REVISION_ID,
217 CRB_ACPI_START_INDEX,
218 NULL);
219 if (!obj)
220 return -ENXIO;
221 rc = obj->integer.value == 0 ? 0 : -ENXIO;
222 ACPI_FREE(obj);
223 return rc;
224}
225
226static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
227{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200228 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800229 int rc = 0;
230
Jarkko Sakkinen72fd50e2016-09-02 22:34:17 +0300231 /* Zero the cancel register so that the next command will not get
232 * canceled.
233 */
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200234 iowrite32(0, &priv->regs_t->ctrl_cancel);
Jarkko Sakkinen72fd50e2016-09-02 22:34:17 +0300235
Tomas Winkleraa77ea02016-09-12 13:52:10 +0300236 if (len > priv->cmd_size) {
237 dev_err(&chip->dev, "invalid command count value %zd %d\n",
238 len, priv->cmd_size);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800239 return -E2BIG;
240 }
241
242 memcpy_toio(priv->cmd, buf, len);
243
244 /* Make sure that cmd is populated before issuing start. */
245 wmb();
246
247 if (priv->flags & CRB_FL_CRB_START)
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200248 iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800249
250 if (priv->flags & CRB_FL_ACPI_START)
251 rc = crb_do_acpi_start(chip);
252
253 return rc;
254}
255
256static void crb_cancel(struct tpm_chip *chip)
257{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200258 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800259
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200260 iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800261
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800262 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
263 dev_err(&chip->dev, "ACPI Start failed\n");
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800264}
265
266static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
267{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200268 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200269 u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800270
271 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
272}
273
274static const struct tpm_class_ops tpm_crb = {
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600275 .flags = TPM_OPS_AUTO_STARTUP,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800276 .status = crb_status,
277 .recv = crb_recv,
278 .send = crb_send,
279 .cancel = crb_cancel,
280 .req_canceled = crb_req_canceled,
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +0300281 .req_complete_mask = CRB_DRV_STS_COMPLETE,
282 .req_complete_val = CRB_DRV_STS_COMPLETE,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800283};
284
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700285static int crb_check_resource(struct acpi_resource *ares, void *data)
286{
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200287 struct resource *io_res = data;
Jiandi An19b7bf52016-12-18 22:20:53 -0600288 struct resource_win win;
289 struct resource *res = &(win.res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700290
Jiandi An19b7bf52016-12-18 22:20:53 -0600291 if (acpi_dev_resource_memory(ares, res) ||
292 acpi_dev_resource_address_space(ares, &win)) {
293 *io_res = *res;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200294 io_res->name = NULL;
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200295 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700296
297 return 1;
298}
299
300static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200301 struct resource *io_res, u64 start, u32 size)
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700302{
303 struct resource new_res = {
304 .start = start,
305 .end = start + size - 1,
306 .flags = IORESOURCE_MEM,
307 };
308
309 /* Detect a 64 bit address on a 32 bit system */
310 if (start != new_res.start)
Jarkko Sakkinenf786b752016-06-17 16:39:29 +0200311 return (void __iomem *) ERR_PTR(-EINVAL);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700312
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200313 if (!resource_contains(io_res, &new_res))
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700314 return devm_ioremap_resource(dev, &new_res);
315
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200316 return priv->iobase + (new_res.start - io_res->start);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700317}
318
Jason Gunthorpeb4e2eb02017-02-21 14:14:24 -0700319/*
320 * Work around broken BIOSs that return inconsistent values from the ACPI
321 * region vs the registers. Trust the ACPI region. Such broken systems
322 * probably cannot send large TPM commands since the buffer will be truncated.
323 */
324static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
325 u64 start, u64 size)
326{
327 if (io_res->start > start || io_res->end < start)
328 return size;
329
330 if (start + size - 1 <= io_res->end)
331 return size;
332
333 dev_err(dev,
334 FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
335 io_res, start, size);
336
337 return io_res->end - start + 1;
338}
339
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700340static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
341 struct acpi_table_tpm2 *buf)
342{
343 struct list_head resources;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200344 struct resource io_res;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700345 struct device *dev = &device->dev;
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300346 u32 pa_high, pa_low;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300347 u64 cmd_pa;
348 u32 cmd_size;
349 u64 rsp_pa;
350 u32 rsp_size;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700351 int ret;
352
353 INIT_LIST_HEAD(&resources);
354 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200355 &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700356 if (ret < 0)
357 return ret;
358 acpi_dev_free_resource_list(&resources);
359
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200360 if (resource_type(&io_res) != IORESOURCE_MEM) {
Tomas Winkler64fba5302016-09-12 02:03:55 +0300361 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700362 return -EINVAL;
363 }
364
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200365 priv->iobase = devm_ioremap_resource(dev, &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700366 if (IS_ERR(priv->iobase))
367 return PTR_ERR(priv->iobase);
368
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200369 /* The ACPI IO region starts at the head area and continues to include
370 * the control area, as one nice sane region except for some older
371 * stuff that puts the control area outside the ACPI IO region.
372 */
373 if (!(priv->flags & CRB_FL_ACPI_START)) {
374 if (buf->control_address == io_res.start +
375 sizeof(*priv->regs_h))
376 priv->regs_h = priv->iobase;
377 else
378 dev_warn(dev, FW_BUG "Bad ACPI memory layout");
379 }
380
381 priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
382 sizeof(struct crb_regs_tail));
383 if (IS_ERR(priv->regs_t))
384 return PTR_ERR(priv->regs_t);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700385
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300386 /*
387 * PTT HW bug w/a: wake up the device to access
388 * possibly not retained registers.
389 */
390 ret = crb_cmd_ready(dev, priv);
391 if (ret)
392 return ret;
393
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200394 pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
395 pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300396 cmd_pa = ((u64)pa_high << 32) | pa_low;
Jason Gunthorpeb4e2eb02017-02-21 14:14:24 -0700397 cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200398 ioread32(&priv->regs_t->ctrl_cmd_size));
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300399
400 dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
401 pa_high, pa_low, cmd_size);
402
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300403 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300404 if (IS_ERR(priv->cmd)) {
405 ret = PTR_ERR(priv->cmd);
406 goto out;
407 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700408
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200409 memcpy_fromio(&rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300410 rsp_pa = le64_to_cpu(rsp_pa);
Jason Gunthorpeb4e2eb02017-02-21 14:14:24 -0700411 rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200412 ioread32(&priv->regs_t->ctrl_rsp_size));
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300413
414 if (cmd_pa != rsp_pa) {
415 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300416 ret = PTR_ERR_OR_ZERO(priv->rsp);
417 goto out;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300418 }
419
420 /* According to the PTP specification, overlapping command and response
421 * buffer sizes must be identical.
422 */
423 if (cmd_size != rsp_size) {
424 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300425 ret = -EINVAL;
426 goto out;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300427 }
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300428
Tomas Winkleraa77ea02016-09-12 13:52:10 +0300429 priv->cmd_size = cmd_size;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300430
431 priv->rsp = priv->cmd;
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300432
433out:
434 crb_go_idle(dev, priv);
435
436 return ret;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700437}
438
439static int crb_acpi_add(struct acpi_device *device)
440{
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700441 struct acpi_table_tpm2 *buf;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800442 struct crb_priv *priv;
Winkler, Tomasc58bd34c2016-09-12 16:04:20 +0300443 struct tpm_chip *chip;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800444 struct device *dev = &device->dev;
445 acpi_status status;
446 u32 sm;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800447 int rc;
448
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800449 status = acpi_get_table(ACPI_SIG_TPM2, 1,
450 (struct acpi_table_header **) &buf);
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700451 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
452 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700453 return -EINVAL;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800454 }
455
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300456 /* Should the FIFO driver handle this? */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700457 sm = buf->start_method;
458 if (sm == ACPI_TPM2_MEMORY_MAPPED)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300459 return -ENODEV;
460
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700461 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
462 if (!priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800463 return -ENOMEM;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800464
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800465 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
466 * report only ACPI start but in practice seems to require both
467 * ACPI start and CRB start.
468 */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700469 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800470 !strcmp(acpi_device_hid(device), "MSFT0101"))
471 priv->flags |= CRB_FL_CRB_START;
472
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700473 if (sm == ACPI_TPM2_START_METHOD ||
474 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800475 priv->flags |= CRB_FL_ACPI_START;
476
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700477 rc = crb_map_io(device, priv, buf);
Jason Gunthorpe25112042015-11-25 14:05:32 -0700478 if (rc)
479 return rc;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800480
Winkler, Tomasc58bd34c2016-09-12 16:04:20 +0300481 chip = tpmm_chip_alloc(dev, &tpm_crb);
482 if (IS_ERR(chip))
483 return PTR_ERR(chip);
484
485 dev_set_drvdata(&chip->dev, priv);
486 chip->acpi_dev_handle = device->handle;
487 chip->flags = TPM_CHIP_FLAG_TPM2;
488
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300489 rc = crb_cmd_ready(dev, priv);
490 if (rc)
491 return rc;
492
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300493 pm_runtime_get_noresume(dev);
494 pm_runtime_set_active(dev);
495 pm_runtime_enable(dev);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300496
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300497 rc = tpm_chip_register(chip);
498 if (rc) {
499 crb_go_idle(dev, priv);
500 pm_runtime_put_noidle(dev);
501 pm_runtime_disable(dev);
502 return rc;
503 }
504
505 pm_runtime_put(dev);
506
507 return 0;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800508}
509
510static int crb_acpi_remove(struct acpi_device *device)
511{
512 struct device *dev = &device->dev;
513 struct tpm_chip *chip = dev_get_drvdata(dev);
514
Jarkko Sakkinen99cda8c2016-02-18 22:11:29 +0200515 tpm_chip_unregister(chip);
516
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300517 pm_runtime_disable(dev);
518
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800519 return 0;
520}
521
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300522#ifdef CONFIG_PM
523static int crb_pm_runtime_suspend(struct device *dev)
524{
525 struct tpm_chip *chip = dev_get_drvdata(dev);
526 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
527
528 return crb_go_idle(dev, priv);
529}
530
531static int crb_pm_runtime_resume(struct device *dev)
532{
533 struct tpm_chip *chip = dev_get_drvdata(dev);
534 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
535
536 return crb_cmd_ready(dev, priv);
537}
Winkler, Tomas095fc302017-03-06 01:53:35 +0200538
539static int crb_pm_suspend(struct device *dev)
540{
541 int ret;
542
543 ret = tpm_pm_suspend(dev);
544 if (ret)
545 return ret;
546
547 return crb_pm_runtime_suspend(dev);
548}
549
550static int crb_pm_resume(struct device *dev)
551{
552 int ret;
553
554 ret = crb_pm_runtime_resume(dev);
555 if (ret)
556 return ret;
557
558 return tpm_pm_resume(dev);
559}
560
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300561#endif /* CONFIG_PM */
562
563static const struct dev_pm_ops crb_pm = {
Winkler, Tomas095fc302017-03-06 01:53:35 +0200564 SET_SYSTEM_SLEEP_PM_OPS(crb_pm_suspend, crb_pm_resume)
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300565 SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
566};
567
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800568static struct acpi_device_id crb_device_ids[] = {
569 {"MSFT0101", 0},
570 {"", 0},
571};
572MODULE_DEVICE_TABLE(acpi, crb_device_ids);
573
574static struct acpi_driver crb_acpi_driver = {
575 .name = "tpm_crb",
576 .ids = crb_device_ids,
577 .ops = {
578 .add = crb_acpi_add,
579 .remove = crb_acpi_remove,
580 },
581 .drv = {
582 .pm = &crb_pm,
583 },
584};
585
586module_acpi_driver(crb_acpi_driver);
587MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
588MODULE_DESCRIPTION("TPM2 Driver");
589MODULE_VERSION("0.1");
590MODULE_LICENSE("GPL");