blob: 324561845dc2375a4264101851ab7454cbff44e2 [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]);
201
202 if (expected > count)
203 return -EIO;
204
205 memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
206
207 return expected;
208}
209
210static int crb_do_acpi_start(struct tpm_chip *chip)
211{
212 union acpi_object *obj;
213 int rc;
214
215 obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
216 CRB_ACPI_START_UUID,
217 CRB_ACPI_START_REVISION_ID,
218 CRB_ACPI_START_INDEX,
219 NULL);
220 if (!obj)
221 return -ENXIO;
222 rc = obj->integer.value == 0 ? 0 : -ENXIO;
223 ACPI_FREE(obj);
224 return rc;
225}
226
227static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
228{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200229 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800230 int rc = 0;
231
Jarkko Sakkinen72fd50e2016-09-02 22:34:17 +0300232 /* Zero the cancel register so that the next command will not get
233 * canceled.
234 */
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200235 iowrite32(0, &priv->regs_t->ctrl_cancel);
Jarkko Sakkinen72fd50e2016-09-02 22:34:17 +0300236
Tomas Winkleraa77ea02016-09-12 13:52:10 +0300237 if (len > priv->cmd_size) {
238 dev_err(&chip->dev, "invalid command count value %zd %d\n",
239 len, priv->cmd_size);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800240 return -E2BIG;
241 }
242
243 memcpy_toio(priv->cmd, buf, len);
244
245 /* Make sure that cmd is populated before issuing start. */
246 wmb();
247
248 if (priv->flags & CRB_FL_CRB_START)
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200249 iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800250
251 if (priv->flags & CRB_FL_ACPI_START)
252 rc = crb_do_acpi_start(chip);
253
254 return rc;
255}
256
257static void crb_cancel(struct tpm_chip *chip)
258{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200259 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800260
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200261 iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800262
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800263 if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
264 dev_err(&chip->dev, "ACPI Start failed\n");
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800265}
266
267static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
268{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200269 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200270 u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800271
272 return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
273}
274
275static const struct tpm_class_ops tpm_crb = {
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600276 .flags = TPM_OPS_AUTO_STARTUP,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800277 .status = crb_status,
278 .recv = crb_recv,
279 .send = crb_send,
280 .cancel = crb_cancel,
281 .req_canceled = crb_req_canceled,
Jarkko Sakkinen7fd10d62016-09-02 22:34:19 +0300282 .req_complete_mask = CRB_DRV_STS_COMPLETE,
283 .req_complete_val = CRB_DRV_STS_COMPLETE,
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800284};
285
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700286static int crb_check_resource(struct acpi_resource *ares, void *data)
287{
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200288 struct resource *io_res = data;
Jiandi An19b7bf52016-12-18 22:20:53 -0600289 struct resource_win win;
290 struct resource *res = &(win.res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700291
Jiandi An19b7bf52016-12-18 22:20:53 -0600292 if (acpi_dev_resource_memory(ares, res) ||
293 acpi_dev_resource_address_space(ares, &win)) {
294 *io_res = *res;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200295 io_res->name = NULL;
Jarkko Sakkinen30f9c8c2016-02-17 02:10:52 +0200296 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700297
298 return 1;
299}
300
301static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200302 struct resource *io_res, u64 start, u32 size)
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700303{
304 struct resource new_res = {
305 .start = start,
306 .end = start + size - 1,
307 .flags = IORESOURCE_MEM,
308 };
309
310 /* Detect a 64 bit address on a 32 bit system */
311 if (start != new_res.start)
Jarkko Sakkinenf786b752016-06-17 16:39:29 +0200312 return (void __iomem *) ERR_PTR(-EINVAL);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700313
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200314 if (!resource_contains(io_res, &new_res))
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700315 return devm_ioremap_resource(dev, &new_res);
316
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200317 return priv->iobase + (new_res.start - io_res->start);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700318}
319
Jason Gunthorpeb4e2eb02017-02-21 14:14:24 -0700320/*
321 * Work around broken BIOSs that return inconsistent values from the ACPI
322 * region vs the registers. Trust the ACPI region. Such broken systems
323 * probably cannot send large TPM commands since the buffer will be truncated.
324 */
325static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
326 u64 start, u64 size)
327{
328 if (io_res->start > start || io_res->end < start)
329 return size;
330
331 if (start + size - 1 <= io_res->end)
332 return size;
333
334 dev_err(dev,
335 FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
336 io_res, start, size);
337
338 return io_res->end - start + 1;
339}
340
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700341static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
342 struct acpi_table_tpm2 *buf)
343{
344 struct list_head resources;
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200345 struct resource io_res;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700346 struct device *dev = &device->dev;
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300347 u32 pa_high, pa_low;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300348 u64 cmd_pa;
349 u32 cmd_size;
350 u64 rsp_pa;
351 u32 rsp_size;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700352 int ret;
353
354 INIT_LIST_HEAD(&resources);
355 ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200356 &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700357 if (ret < 0)
358 return ret;
359 acpi_dev_free_resource_list(&resources);
360
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200361 if (resource_type(&io_res) != IORESOURCE_MEM) {
Tomas Winkler64fba5302016-09-12 02:03:55 +0300362 dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700363 return -EINVAL;
364 }
365
Jarkko Sakkinen14ddfbf2016-03-15 21:41:40 +0200366 priv->iobase = devm_ioremap_resource(dev, &io_res);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700367 if (IS_ERR(priv->iobase))
368 return PTR_ERR(priv->iobase);
369
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200370 /* The ACPI IO region starts at the head area and continues to include
371 * the control area, as one nice sane region except for some older
372 * stuff that puts the control area outside the ACPI IO region.
373 */
374 if (!(priv->flags & CRB_FL_ACPI_START)) {
375 if (buf->control_address == io_res.start +
376 sizeof(*priv->regs_h))
377 priv->regs_h = priv->iobase;
378 else
379 dev_warn(dev, FW_BUG "Bad ACPI memory layout");
380 }
381
382 priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
383 sizeof(struct crb_regs_tail));
384 if (IS_ERR(priv->regs_t))
385 return PTR_ERR(priv->regs_t);
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700386
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300387 /*
388 * PTT HW bug w/a: wake up the device to access
389 * possibly not retained registers.
390 */
391 ret = crb_cmd_ready(dev, priv);
392 if (ret)
393 return ret;
394
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200395 pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
396 pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300397 cmd_pa = ((u64)pa_high << 32) | pa_low;
Jason Gunthorpeb4e2eb02017-02-21 14:14:24 -0700398 cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200399 ioread32(&priv->regs_t->ctrl_cmd_size));
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300400
401 dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
402 pa_high, pa_low, cmd_size);
403
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300404 priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300405 if (IS_ERR(priv->cmd)) {
406 ret = PTR_ERR(priv->cmd);
407 goto out;
408 }
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700409
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200410 memcpy_fromio(&rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300411 rsp_pa = le64_to_cpu(rsp_pa);
Jason Gunthorpeb4e2eb02017-02-21 14:14:24 -0700412 rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
Jarkko Sakkinen13b1f4a2017-02-08 13:11:35 +0200413 ioread32(&priv->regs_t->ctrl_rsp_size));
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300414
415 if (cmd_pa != rsp_pa) {
416 priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300417 ret = PTR_ERR_OR_ZERO(priv->rsp);
418 goto out;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300419 }
420
421 /* According to the PTP specification, overlapping command and response
422 * buffer sizes must be identical.
423 */
424 if (cmd_size != rsp_size) {
425 dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300426 ret = -EINVAL;
427 goto out;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300428 }
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300429
Tomas Winkleraa77ea02016-09-12 13:52:10 +0300430 priv->cmd_size = cmd_size;
Jarkko Sakkinen422eac32016-04-19 12:54:18 +0300431
432 priv->rsp = priv->cmd;
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300433
434out:
435 crb_go_idle(dev, priv);
436
437 return ret;
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700438}
439
440static int crb_acpi_add(struct acpi_device *device)
441{
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700442 struct acpi_table_tpm2 *buf;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800443 struct crb_priv *priv;
Winkler, Tomasc58bd34c2016-09-12 16:04:20 +0300444 struct tpm_chip *chip;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800445 struct device *dev = &device->dev;
446 acpi_status status;
447 u32 sm;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800448 int rc;
449
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800450 status = acpi_get_table(ACPI_SIG_TPM2, 1,
451 (struct acpi_table_header **) &buf);
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700452 if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
453 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700454 return -EINVAL;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800455 }
456
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300457 /* Should the FIFO driver handle this? */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700458 sm = buf->start_method;
459 if (sm == ACPI_TPM2_MEMORY_MAPPED)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300460 return -ENODEV;
461
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700462 priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
463 if (!priv)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800464 return -ENOMEM;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800465
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800466 /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
467 * report only ACPI start but in practice seems to require both
468 * ACPI start and CRB start.
469 */
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700470 if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800471 !strcmp(acpi_device_hid(device), "MSFT0101"))
472 priv->flags |= CRB_FL_CRB_START;
473
Jason Gunthorpe55a889c2016-01-07 17:36:20 -0700474 if (sm == ACPI_TPM2_START_METHOD ||
475 sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800476 priv->flags |= CRB_FL_ACPI_START;
477
Jason Gunthorpe1bd047b2016-01-07 17:36:26 -0700478 rc = crb_map_io(device, priv, buf);
Jason Gunthorpe25112042015-11-25 14:05:32 -0700479 if (rc)
480 return rc;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800481
Winkler, Tomasc58bd34c2016-09-12 16:04:20 +0300482 chip = tpmm_chip_alloc(dev, &tpm_crb);
483 if (IS_ERR(chip))
484 return PTR_ERR(chip);
485
486 dev_set_drvdata(&chip->dev, priv);
487 chip->acpi_dev_handle = device->handle;
488 chip->flags = TPM_CHIP_FLAG_TPM2;
489
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300490 rc = crb_cmd_ready(dev, priv);
491 if (rc)
492 return rc;
493
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300494 pm_runtime_get_noresume(dev);
495 pm_runtime_set_active(dev);
496 pm_runtime_enable(dev);
Winkler, Tomasbc33c5d2016-09-12 16:04:19 +0300497
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300498 rc = tpm_chip_register(chip);
499 if (rc) {
500 crb_go_idle(dev, priv);
501 pm_runtime_put_noidle(dev);
502 pm_runtime_disable(dev);
503 return rc;
504 }
505
506 pm_runtime_put(dev);
507
508 return 0;
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800509}
510
511static int crb_acpi_remove(struct acpi_device *device)
512{
513 struct device *dev = &device->dev;
514 struct tpm_chip *chip = dev_get_drvdata(dev);
515
Jarkko Sakkinen99cda8c2016-02-18 22:11:29 +0200516 tpm_chip_unregister(chip);
517
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300518 pm_runtime_disable(dev);
519
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800520 return 0;
521}
522
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300523#ifdef CONFIG_PM
524static int crb_pm_runtime_suspend(struct device *dev)
525{
526 struct tpm_chip *chip = dev_get_drvdata(dev);
527 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
528
529 return crb_go_idle(dev, priv);
530}
531
532static int crb_pm_runtime_resume(struct device *dev)
533{
534 struct tpm_chip *chip = dev_get_drvdata(dev);
535 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
536
537 return crb_cmd_ready(dev, priv);
538}
Winkler, Tomas095fc302017-03-06 01:53:35 +0200539
540static int crb_pm_suspend(struct device *dev)
541{
542 int ret;
543
544 ret = tpm_pm_suspend(dev);
545 if (ret)
546 return ret;
547
548 return crb_pm_runtime_suspend(dev);
549}
550
551static int crb_pm_resume(struct device *dev)
552{
553 int ret;
554
555 ret = crb_pm_runtime_resume(dev);
556 if (ret)
557 return ret;
558
559 return tpm_pm_resume(dev);
560}
561
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300562#endif /* CONFIG_PM */
563
564static const struct dev_pm_ops crb_pm = {
Winkler, Tomas095fc302017-03-06 01:53:35 +0200565 SET_SYSTEM_SLEEP_PM_OPS(crb_pm_suspend, crb_pm_resume)
Winkler, Tomase74f2f72016-10-08 14:59:39 +0300566 SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
567};
568
Jarkko Sakkinen30fc8d12014-12-12 11:46:39 -0800569static struct acpi_device_id crb_device_ids[] = {
570 {"MSFT0101", 0},
571 {"", 0},
572};
573MODULE_DEVICE_TABLE(acpi, crb_device_ids);
574
575static struct acpi_driver crb_acpi_driver = {
576 .name = "tpm_crb",
577 .ids = crb_device_ids,
578 .ops = {
579 .add = crb_acpi_add,
580 .remove = crb_acpi_remove,
581 },
582 .drv = {
583 .pm = &crb_pm,
584 },
585};
586
587module_acpi_driver(crb_acpi_driver);
588MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
589MODULE_DESCRIPTION("TPM2 Driver");
590MODULE_VERSION("0.1");
591MODULE_LICENSE("GPL");