Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 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> |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 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 | |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 37 | enum crb_ctrl_req { |
Jarkko Sakkinen | f39a9e9 | 2016-09-02 22:34:20 +0300 | [diff] [blame] | 38 | CRB_CTRL_REQ_CMD_READY = BIT(0), |
| 39 | CRB_CTRL_REQ_GO_IDLE = BIT(1), |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 42 | enum crb_ctrl_sts { |
| 43 | CRB_CTRL_STS_ERROR = BIT(0), |
| 44 | CRB_CTRL_STS_TPM_IDLE = BIT(1), |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 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 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 55 | struct 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 | |
| 65 | struct 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 Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 77 | } __packed; |
| 78 | |
| 79 | enum crb_status { |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 80 | CRB_DRV_STS_COMPLETE = BIT(0), |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | enum crb_flags { |
| 84 | CRB_FL_ACPI_START = BIT(0), |
| 85 | CRB_FL_CRB_START = BIT(1), |
| 86 | }; |
| 87 | |
| 88 | struct crb_priv { |
| 89 | unsigned int flags; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 90 | void __iomem *iobase; |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 91 | struct crb_regs_head __iomem *regs_h; |
| 92 | struct crb_regs_tail __iomem *regs_t; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 93 | u8 __iomem *cmd; |
| 94 | u8 __iomem *rsp; |
Tomas Winkler | aa77ea0 | 2016-09-12 13:52:10 +0300 | [diff] [blame] | 95 | u32 cmd_size; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
Winkler, Tomas | ba5287b | 2016-09-15 10:27:38 +0300 | [diff] [blame] | 98 | /** |
| 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 | */ |
| 113 | static 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 Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 118 | iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req); |
Winkler, Tomas | ba5287b | 2016-09-15 10:27:38 +0300 | [diff] [blame] | 119 | /* we don't really care when this settles */ |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Jarkko Sakkinen | 38eb24e | 2017-02-08 13:11:36 +0200 | [diff] [blame] | 124 | static 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, Tomas | ba5287b | 2016-09-15 10:27:38 +0300 | [diff] [blame] | 143 | /** |
| 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 | */ |
| 157 | static int __maybe_unused crb_cmd_ready(struct device *dev, |
| 158 | struct crb_priv *priv) |
| 159 | { |
Winkler, Tomas | ba5287b | 2016-09-15 10:27:38 +0300 | [diff] [blame] | 160 | if (priv->flags & CRB_FL_ACPI_START) |
| 161 | return 0; |
| 162 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 163 | iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req); |
Jarkko Sakkinen | 38eb24e | 2017-02-08 13:11:36 +0200 | [diff] [blame] | 164 | 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, Tomas | ba5287b | 2016-09-15 10:27:38 +0300 | [diff] [blame] | 168 | dev_warn(dev, "cmdReady timed out\n"); |
| 169 | return -ETIME; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 175 | static u8 crb_status(struct tpm_chip *chip) |
| 176 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 177 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 178 | u8 sts = 0; |
| 179 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 180 | if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) != |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 181 | CRB_START_INVOKE) |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 182 | sts |= CRB_DRV_STS_COMPLETE; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 183 | |
| 184 | return sts; |
| 185 | } |
| 186 | |
| 187 | static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 188 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 189 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 190 | unsigned int expected; |
| 191 | |
| 192 | /* sanity check */ |
| 193 | if (count < 6) |
| 194 | return -EIO; |
| 195 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 196 | if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR) |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 197 | 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 | |
| 210 | static 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 | |
| 227 | static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 228 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 229 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 230 | int rc = 0; |
| 231 | |
Jarkko Sakkinen | 72fd50e | 2016-09-02 22:34:17 +0300 | [diff] [blame] | 232 | /* Zero the cancel register so that the next command will not get |
| 233 | * canceled. |
| 234 | */ |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 235 | iowrite32(0, &priv->regs_t->ctrl_cancel); |
Jarkko Sakkinen | 72fd50e | 2016-09-02 22:34:17 +0300 | [diff] [blame] | 236 | |
Tomas Winkler | aa77ea0 | 2016-09-12 13:52:10 +0300 | [diff] [blame] | 237 | if (len > priv->cmd_size) { |
| 238 | dev_err(&chip->dev, "invalid command count value %zd %d\n", |
| 239 | len, priv->cmd_size); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 240 | 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 Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 249 | iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 250 | |
| 251 | if (priv->flags & CRB_FL_ACPI_START) |
| 252 | rc = crb_do_acpi_start(chip); |
| 253 | |
| 254 | return rc; |
| 255 | } |
| 256 | |
| 257 | static void crb_cancel(struct tpm_chip *chip) |
| 258 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 259 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 260 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 261 | iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 262 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 263 | if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip)) |
| 264 | dev_err(&chip->dev, "ACPI Start failed\n"); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static bool crb_req_canceled(struct tpm_chip *chip, u8 status) |
| 268 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 269 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 270 | u32 cancel = ioread32(&priv->regs_t->ctrl_cancel); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 271 | |
| 272 | return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE; |
| 273 | } |
| 274 | |
| 275 | static const struct tpm_class_ops tpm_crb = { |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 276 | .flags = TPM_OPS_AUTO_STARTUP, |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 277 | .status = crb_status, |
| 278 | .recv = crb_recv, |
| 279 | .send = crb_send, |
| 280 | .cancel = crb_cancel, |
| 281 | .req_canceled = crb_req_canceled, |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 282 | .req_complete_mask = CRB_DRV_STS_COMPLETE, |
| 283 | .req_complete_val = CRB_DRV_STS_COMPLETE, |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 284 | }; |
| 285 | |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 286 | static int crb_check_resource(struct acpi_resource *ares, void *data) |
| 287 | { |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 288 | struct resource *io_res = data; |
Jiandi An | 19b7bf5 | 2016-12-18 22:20:53 -0600 | [diff] [blame] | 289 | struct resource_win win; |
| 290 | struct resource *res = &(win.res); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 291 | |
Jiandi An | 19b7bf5 | 2016-12-18 22:20:53 -0600 | [diff] [blame] | 292 | if (acpi_dev_resource_memory(ares, res) || |
| 293 | acpi_dev_resource_address_space(ares, &win)) { |
| 294 | *io_res = *res; |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 295 | io_res->name = NULL; |
Jarkko Sakkinen | 30f9c8c | 2016-02-17 02:10:52 +0200 | [diff] [blame] | 296 | } |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 297 | |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv, |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 302 | struct resource *io_res, u64 start, u32 size) |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 303 | { |
| 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 Sakkinen | f786b75 | 2016-06-17 16:39:29 +0200 | [diff] [blame] | 312 | return (void __iomem *) ERR_PTR(-EINVAL); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 313 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 314 | if (!resource_contains(io_res, &new_res)) |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 315 | return devm_ioremap_resource(dev, &new_res); |
| 316 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 317 | return priv->iobase + (new_res.start - io_res->start); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Jason Gunthorpe | b4e2eb0 | 2017-02-21 14:14:24 -0700 | [diff] [blame] | 320 | /* |
| 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 | */ |
| 325 | static 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 Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 341 | static 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 Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 345 | struct resource io_res; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 346 | struct device *dev = &device->dev; |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 347 | u32 pa_high, pa_low; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 348 | u64 cmd_pa; |
| 349 | u32 cmd_size; |
| 350 | u64 rsp_pa; |
| 351 | u32 rsp_size; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 352 | int ret; |
| 353 | |
| 354 | INIT_LIST_HEAD(&resources); |
| 355 | ret = acpi_dev_get_resources(device, &resources, crb_check_resource, |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 356 | &io_res); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 357 | if (ret < 0) |
| 358 | return ret; |
| 359 | acpi_dev_free_resource_list(&resources); |
| 360 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 361 | if (resource_type(&io_res) != IORESOURCE_MEM) { |
Tomas Winkler | 64fba530 | 2016-09-12 02:03:55 +0300 | [diff] [blame] | 362 | dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n"); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | } |
| 365 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 366 | priv->iobase = devm_ioremap_resource(dev, &io_res); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 367 | if (IS_ERR(priv->iobase)) |
| 368 | return PTR_ERR(priv->iobase); |
| 369 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 370 | /* 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 Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 386 | |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 387 | /* |
| 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 Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 395 | pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high); |
| 396 | pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 397 | cmd_pa = ((u64)pa_high << 32) | pa_low; |
Jason Gunthorpe | b4e2eb0 | 2017-02-21 14:14:24 -0700 | [diff] [blame] | 398 | cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa, |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 399 | ioread32(&priv->regs_t->ctrl_cmd_size)); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 400 | |
| 401 | dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n", |
| 402 | pa_high, pa_low, cmd_size); |
| 403 | |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 404 | priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 405 | if (IS_ERR(priv->cmd)) { |
| 406 | ret = PTR_ERR(priv->cmd); |
| 407 | goto out; |
| 408 | } |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 409 | |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 410 | memcpy_fromio(&rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8); |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 411 | rsp_pa = le64_to_cpu(rsp_pa); |
Jason Gunthorpe | b4e2eb0 | 2017-02-21 14:14:24 -0700 | [diff] [blame] | 412 | rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa, |
Jarkko Sakkinen | 13b1f4a | 2017-02-08 13:11:35 +0200 | [diff] [blame] | 413 | ioread32(&priv->regs_t->ctrl_rsp_size)); |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 414 | |
| 415 | if (cmd_pa != rsp_pa) { |
| 416 | priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 417 | ret = PTR_ERR_OR_ZERO(priv->rsp); |
| 418 | goto out; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 419 | } |
| 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, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 426 | ret = -EINVAL; |
| 427 | goto out; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 428 | } |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 429 | |
Tomas Winkler | aa77ea0 | 2016-09-12 13:52:10 +0300 | [diff] [blame] | 430 | priv->cmd_size = cmd_size; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 431 | |
| 432 | priv->rsp = priv->cmd; |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 433 | |
| 434 | out: |
| 435 | crb_go_idle(dev, priv); |
| 436 | |
| 437 | return ret; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int crb_acpi_add(struct acpi_device *device) |
| 441 | { |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 442 | struct acpi_table_tpm2 *buf; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 443 | struct crb_priv *priv; |
Winkler, Tomas | c58bd34c | 2016-09-12 16:04:20 +0300 | [diff] [blame] | 444 | struct tpm_chip *chip; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 445 | struct device *dev = &device->dev; |
| 446 | acpi_status status; |
| 447 | u32 sm; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 448 | int rc; |
| 449 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 450 | status = acpi_get_table(ACPI_SIG_TPM2, 1, |
| 451 | (struct acpi_table_header **) &buf); |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 452 | if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) { |
| 453 | dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n"); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 454 | return -EINVAL; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 457 | /* Should the FIFO driver handle this? */ |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 458 | sm = buf->start_method; |
| 459 | if (sm == ACPI_TPM2_MEMORY_MAPPED) |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 460 | return -ENODEV; |
| 461 | |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 462 | priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL); |
| 463 | if (!priv) |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 464 | return -ENOMEM; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 465 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 466 | /* 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 Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 470 | if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED || |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 471 | !strcmp(acpi_device_hid(device), "MSFT0101")) |
| 472 | priv->flags |= CRB_FL_CRB_START; |
| 473 | |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 474 | if (sm == ACPI_TPM2_START_METHOD || |
| 475 | sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 476 | priv->flags |= CRB_FL_ACPI_START; |
| 477 | |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 478 | rc = crb_map_io(device, priv, buf); |
Jason Gunthorpe | 2511204 | 2015-11-25 14:05:32 -0700 | [diff] [blame] | 479 | if (rc) |
| 480 | return rc; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 481 | |
Winkler, Tomas | c58bd34c | 2016-09-12 16:04:20 +0300 | [diff] [blame] | 482 | 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, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 490 | rc = crb_cmd_ready(dev, priv); |
| 491 | if (rc) |
| 492 | return rc; |
| 493 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 494 | pm_runtime_get_noresume(dev); |
| 495 | pm_runtime_set_active(dev); |
| 496 | pm_runtime_enable(dev); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 497 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 498 | 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 Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static 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 Sakkinen | 99cda8c | 2016-02-18 22:11:29 +0200 | [diff] [blame] | 516 | tpm_chip_unregister(chip); |
| 517 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 518 | pm_runtime_disable(dev); |
| 519 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 523 | #ifdef CONFIG_PM |
| 524 | static 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 | |
| 532 | static 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, Tomas | 095fc30 | 2017-03-06 01:53:35 +0200 | [diff] [blame] | 539 | |
| 540 | static 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 | |
| 551 | static 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, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 562 | #endif /* CONFIG_PM */ |
| 563 | |
| 564 | static const struct dev_pm_ops crb_pm = { |
Winkler, Tomas | 095fc30 | 2017-03-06 01:53:35 +0200 | [diff] [blame] | 565 | SET_SYSTEM_SLEEP_PM_OPS(crb_pm_suspend, crb_pm_resume) |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame] | 566 | SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL) |
| 567 | }; |
| 568 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 569 | static struct acpi_device_id crb_device_ids[] = { |
| 570 | {"MSFT0101", 0}, |
| 571 | {"", 0}, |
| 572 | }; |
| 573 | MODULE_DEVICE_TABLE(acpi, crb_device_ids); |
| 574 | |
| 575 | static 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 | |
| 587 | module_acpi_driver(crb_acpi_driver); |
| 588 | MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>"); |
| 589 | MODULE_DESCRIPTION("TPM2 Driver"); |
| 590 | MODULE_VERSION("0.1"); |
| 591 | MODULE_LICENSE("GPL"); |