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 | |
| 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; |
Jarkko Sakkinen | 149789c | 2015-09-15 20:05:40 +0300 | [diff] [blame] | 63 | u32 cmd_pa_low; |
| 64 | u32 cmd_pa_high; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 65 | u32 rsp_size; |
| 66 | u64 rsp_pa; |
| 67 | } __packed; |
| 68 | |
| 69 | enum crb_status { |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 70 | CRB_DRV_STS_COMPLETE = BIT(0), |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 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; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 80 | void __iomem *iobase; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 81 | struct crb_control_area __iomem *cca; |
| 82 | u8 __iomem *cmd; |
| 83 | u8 __iomem *rsp; |
Tomas Winkler | aa77ea0 | 2016-09-12 13:52:10 +0300 | [diff] [blame] | 84 | u32 cmd_size; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Winkler, Tomas | ba5287b | 2016-09-15 10:27:38 +0300 | [diff] [blame] | 87 | /** |
| 88 | * crb_go_idle - request tpm crb device to go the idle state |
| 89 | * |
| 90 | * @dev: crb device |
| 91 | * @priv: crb private data |
| 92 | * |
| 93 | * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ |
| 94 | * The device should respond within TIMEOUT_C by clearing the bit. |
| 95 | * Anyhow, we do not wait here as a consequent CMD_READY request |
| 96 | * will be handled correctly even if idle was not completed. |
| 97 | * |
| 98 | * The function does nothing for devices with ACPI-start method. |
| 99 | * |
| 100 | * Return: 0 always |
| 101 | */ |
| 102 | static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv) |
| 103 | { |
| 104 | if (priv->flags & CRB_FL_ACPI_START) |
| 105 | return 0; |
| 106 | |
| 107 | iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->cca->req); |
| 108 | /* we don't really care when this settles */ |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * crb_cmd_ready - request tpm crb device to enter ready state |
| 115 | * |
| 116 | * @dev: crb device |
| 117 | * @priv: crb private data |
| 118 | * |
| 119 | * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ |
| 120 | * and poll till the device acknowledge it by clearing the bit. |
| 121 | * The device should respond within TIMEOUT_C. |
| 122 | * |
| 123 | * The function does nothing for devices with ACPI-start method |
| 124 | * |
| 125 | * Return: 0 on success -ETIME on timeout; |
| 126 | */ |
| 127 | static int __maybe_unused crb_cmd_ready(struct device *dev, |
| 128 | struct crb_priv *priv) |
| 129 | { |
| 130 | ktime_t stop, start; |
| 131 | |
| 132 | if (priv->flags & CRB_FL_ACPI_START) |
| 133 | return 0; |
| 134 | |
| 135 | iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->cca->req); |
| 136 | |
| 137 | start = ktime_get(); |
| 138 | stop = ktime_add(start, ms_to_ktime(TPM2_TIMEOUT_C)); |
| 139 | do { |
| 140 | if (!(ioread32(&priv->cca->req) & CRB_CTRL_REQ_CMD_READY)) |
| 141 | return 0; |
| 142 | usleep_range(50, 100); |
| 143 | } while (ktime_before(ktime_get(), stop)); |
| 144 | |
| 145 | if (ioread32(&priv->cca->req) & CRB_CTRL_REQ_CMD_READY) { |
| 146 | dev_warn(dev, "cmdReady timed out\n"); |
| 147 | return -ETIME; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 153 | static u8 crb_status(struct tpm_chip *chip) |
| 154 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 155 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 156 | u8 sts = 0; |
| 157 | |
Jason Gunthorpe | 1e3ed59d | 2016-01-07 17:36:25 -0700 | [diff] [blame] | 158 | if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) != |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 159 | CRB_START_INVOKE) |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 160 | sts |= CRB_DRV_STS_COMPLETE; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 161 | |
| 162 | return sts; |
| 163 | } |
| 164 | |
| 165 | static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 166 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 167 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 168 | unsigned int expected; |
| 169 | |
| 170 | /* sanity check */ |
| 171 | if (count < 6) |
| 172 | return -EIO; |
| 173 | |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 174 | if (ioread32(&priv->cca->sts) & CRB_CTRL_STS_ERROR) |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 175 | return -EIO; |
| 176 | |
| 177 | memcpy_fromio(buf, priv->rsp, 6); |
| 178 | expected = be32_to_cpup((__be32 *) &buf[2]); |
| 179 | |
| 180 | if (expected > count) |
| 181 | return -EIO; |
| 182 | |
| 183 | memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6); |
| 184 | |
| 185 | return expected; |
| 186 | } |
| 187 | |
| 188 | static int crb_do_acpi_start(struct tpm_chip *chip) |
| 189 | { |
| 190 | union acpi_object *obj; |
| 191 | int rc; |
| 192 | |
| 193 | obj = acpi_evaluate_dsm(chip->acpi_dev_handle, |
| 194 | CRB_ACPI_START_UUID, |
| 195 | CRB_ACPI_START_REVISION_ID, |
| 196 | CRB_ACPI_START_INDEX, |
| 197 | NULL); |
| 198 | if (!obj) |
| 199 | return -ENXIO; |
| 200 | rc = obj->integer.value == 0 ? 0 : -ENXIO; |
| 201 | ACPI_FREE(obj); |
| 202 | return rc; |
| 203 | } |
| 204 | |
| 205 | static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 206 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 207 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 208 | int rc = 0; |
| 209 | |
Jarkko Sakkinen | 72fd50e | 2016-09-02 22:34:17 +0300 | [diff] [blame] | 210 | /* Zero the cancel register so that the next command will not get |
| 211 | * canceled. |
| 212 | */ |
| 213 | iowrite32(0, &priv->cca->cancel); |
| 214 | |
Tomas Winkler | aa77ea0 | 2016-09-12 13:52:10 +0300 | [diff] [blame] | 215 | if (len > priv->cmd_size) { |
| 216 | dev_err(&chip->dev, "invalid command count value %zd %d\n", |
| 217 | len, priv->cmd_size); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 218 | return -E2BIG; |
| 219 | } |
| 220 | |
| 221 | memcpy_toio(priv->cmd, buf, len); |
| 222 | |
| 223 | /* Make sure that cmd is populated before issuing start. */ |
| 224 | wmb(); |
| 225 | |
| 226 | if (priv->flags & CRB_FL_CRB_START) |
Tomas Winkler | 47de683 | 2016-09-12 14:27:09 +0300 | [diff] [blame] | 227 | iowrite32(CRB_START_INVOKE, &priv->cca->start); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 228 | |
| 229 | if (priv->flags & CRB_FL_ACPI_START) |
| 230 | rc = crb_do_acpi_start(chip); |
| 231 | |
| 232 | return rc; |
| 233 | } |
| 234 | |
| 235 | static void crb_cancel(struct tpm_chip *chip) |
| 236 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 237 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 238 | |
Tomas Winkler | 47de683 | 2016-09-12 14:27:09 +0300 | [diff] [blame] | 239 | iowrite32(CRB_CANCEL_INVOKE, &priv->cca->cancel); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 240 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 241 | if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip)) |
| 242 | dev_err(&chip->dev, "ACPI Start failed\n"); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static bool crb_req_canceled(struct tpm_chip *chip, u8 status) |
| 246 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 247 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
Jason Gunthorpe | 1e3ed59d | 2016-01-07 17:36:25 -0700 | [diff] [blame] | 248 | u32 cancel = ioread32(&priv->cca->cancel); |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 249 | |
| 250 | return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE; |
| 251 | } |
| 252 | |
| 253 | static const struct tpm_class_ops tpm_crb = { |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 254 | .flags = TPM_OPS_AUTO_STARTUP, |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 255 | .status = crb_status, |
| 256 | .recv = crb_recv, |
| 257 | .send = crb_send, |
| 258 | .cancel = crb_cancel, |
| 259 | .req_canceled = crb_req_canceled, |
Jarkko Sakkinen | 7fd10d6 | 2016-09-02 22:34:19 +0300 | [diff] [blame] | 260 | .req_complete_mask = CRB_DRV_STS_COMPLETE, |
| 261 | .req_complete_val = CRB_DRV_STS_COMPLETE, |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 264 | static int crb_check_resource(struct acpi_resource *ares, void *data) |
| 265 | { |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 266 | struct resource *io_res = data; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 267 | struct resource res; |
| 268 | |
Jarkko Sakkinen | 30f9c8c | 2016-02-17 02:10:52 +0200 | [diff] [blame] | 269 | if (acpi_dev_resource_memory(ares, &res)) { |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 270 | *io_res = res; |
| 271 | io_res->name = NULL; |
Jarkko Sakkinen | 30f9c8c | 2016-02-17 02:10:52 +0200 | [diff] [blame] | 272 | } |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 273 | |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | 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] | 278 | struct resource *io_res, u64 start, u32 size) |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 279 | { |
| 280 | struct resource new_res = { |
| 281 | .start = start, |
| 282 | .end = start + size - 1, |
| 283 | .flags = IORESOURCE_MEM, |
| 284 | }; |
| 285 | |
| 286 | /* Detect a 64 bit address on a 32 bit system */ |
| 287 | if (start != new_res.start) |
Jarkko Sakkinen | f786b75 | 2016-06-17 16:39:29 +0200 | [diff] [blame] | 288 | return (void __iomem *) ERR_PTR(-EINVAL); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 289 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 290 | if (!resource_contains(io_res, &new_res)) |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 291 | return devm_ioremap_resource(dev, &new_res); |
| 292 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 293 | return priv->iobase + (new_res.start - io_res->start); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int crb_map_io(struct acpi_device *device, struct crb_priv *priv, |
| 297 | struct acpi_table_tpm2 *buf) |
| 298 | { |
| 299 | struct list_head resources; |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 300 | struct resource io_res; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 301 | struct device *dev = &device->dev; |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 302 | u32 pa_high, pa_low; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 303 | u64 cmd_pa; |
| 304 | u32 cmd_size; |
| 305 | u64 rsp_pa; |
| 306 | u32 rsp_size; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 307 | int ret; |
| 308 | |
| 309 | INIT_LIST_HEAD(&resources); |
| 310 | ret = acpi_dev_get_resources(device, &resources, crb_check_resource, |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 311 | &io_res); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 312 | if (ret < 0) |
| 313 | return ret; |
| 314 | acpi_dev_free_resource_list(&resources); |
| 315 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 316 | if (resource_type(&io_res) != IORESOURCE_MEM) { |
Tomas Winkler | 64fba530 | 2016-09-12 02:03:55 +0300 | [diff] [blame] | 317 | 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] | 318 | return -EINVAL; |
| 319 | } |
| 320 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 321 | priv->iobase = devm_ioremap_resource(dev, &io_res); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 322 | if (IS_ERR(priv->iobase)) |
| 323 | return PTR_ERR(priv->iobase); |
| 324 | |
Jarkko Sakkinen | 14ddfbf | 2016-03-15 21:41:40 +0200 | [diff] [blame] | 325 | priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address, |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 326 | sizeof(struct crb_control_area)); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 327 | if (IS_ERR(priv->cca)) |
| 328 | return PTR_ERR(priv->cca); |
| 329 | |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 330 | /* |
| 331 | * PTT HW bug w/a: wake up the device to access |
| 332 | * possibly not retained registers. |
| 333 | */ |
| 334 | ret = crb_cmd_ready(dev, priv); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | |
| 338 | pa_high = ioread32(&priv->cca->cmd_pa_high); |
| 339 | pa_low = ioread32(&priv->cca->cmd_pa_low); |
| 340 | cmd_pa = ((u64)pa_high << 32) | pa_low; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 341 | cmd_size = ioread32(&priv->cca->cmd_size); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 342 | |
| 343 | dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n", |
| 344 | pa_high, pa_low, cmd_size); |
| 345 | |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 346 | 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] | 347 | if (IS_ERR(priv->cmd)) { |
| 348 | ret = PTR_ERR(priv->cmd); |
| 349 | goto out; |
| 350 | } |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 351 | |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 352 | memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8); |
| 353 | rsp_pa = le64_to_cpu(rsp_pa); |
| 354 | rsp_size = ioread32(&priv->cca->rsp_size); |
| 355 | |
| 356 | if (cmd_pa != rsp_pa) { |
| 357 | 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] | 358 | ret = PTR_ERR_OR_ZERO(priv->rsp); |
| 359 | goto out; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* According to the PTP specification, overlapping command and response |
| 363 | * buffer sizes must be identical. |
| 364 | */ |
| 365 | if (cmd_size != rsp_size) { |
| 366 | 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] | 367 | ret = -EINVAL; |
| 368 | goto out; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 369 | } |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 370 | |
Tomas Winkler | aa77ea0 | 2016-09-12 13:52:10 +0300 | [diff] [blame] | 371 | priv->cmd_size = cmd_size; |
Jarkko Sakkinen | 422eac3 | 2016-04-19 12:54:18 +0300 | [diff] [blame] | 372 | |
| 373 | priv->rsp = priv->cmd; |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 374 | |
| 375 | out: |
| 376 | crb_go_idle(dev, priv); |
| 377 | |
| 378 | return ret; |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int crb_acpi_add(struct acpi_device *device) |
| 382 | { |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 383 | struct acpi_table_tpm2 *buf; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 384 | struct crb_priv *priv; |
Winkler, Tomas | c58bd34c | 2016-09-12 16:04:20 +0300 | [diff] [blame] | 385 | struct tpm_chip *chip; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 386 | struct device *dev = &device->dev; |
| 387 | acpi_status status; |
| 388 | u32 sm; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 389 | int rc; |
| 390 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 391 | status = acpi_get_table(ACPI_SIG_TPM2, 1, |
| 392 | (struct acpi_table_header **) &buf); |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 393 | if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) { |
| 394 | dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n"); |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 395 | return -EINVAL; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 398 | /* Should the FIFO driver handle this? */ |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 399 | sm = buf->start_method; |
| 400 | if (sm == ACPI_TPM2_MEMORY_MAPPED) |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 401 | return -ENODEV; |
| 402 | |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 403 | priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL); |
| 404 | if (!priv) |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 405 | return -ENOMEM; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 406 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 407 | /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs |
| 408 | * report only ACPI start but in practice seems to require both |
| 409 | * ACPI start and CRB start. |
| 410 | */ |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 411 | if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED || |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 412 | !strcmp(acpi_device_hid(device), "MSFT0101")) |
| 413 | priv->flags |= CRB_FL_CRB_START; |
| 414 | |
Jason Gunthorpe | 55a889c | 2016-01-07 17:36:20 -0700 | [diff] [blame] | 415 | if (sm == ACPI_TPM2_START_METHOD || |
| 416 | sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 417 | priv->flags |= CRB_FL_ACPI_START; |
| 418 | |
Jason Gunthorpe | 1bd047b | 2016-01-07 17:36:26 -0700 | [diff] [blame] | 419 | rc = crb_map_io(device, priv, buf); |
Jason Gunthorpe | 2511204 | 2015-11-25 14:05:32 -0700 | [diff] [blame] | 420 | if (rc) |
| 421 | return rc; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 422 | |
Winkler, Tomas | c58bd34c | 2016-09-12 16:04:20 +0300 | [diff] [blame] | 423 | chip = tpmm_chip_alloc(dev, &tpm_crb); |
| 424 | if (IS_ERR(chip)) |
| 425 | return PTR_ERR(chip); |
| 426 | |
| 427 | dev_set_drvdata(&chip->dev, priv); |
| 428 | chip->acpi_dev_handle = device->handle; |
| 429 | chip->flags = TPM_CHIP_FLAG_TPM2; |
| 430 | |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 431 | rc = crb_cmd_ready(dev, priv); |
| 432 | if (rc) |
| 433 | return rc; |
| 434 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame^] | 435 | pm_runtime_get_noresume(dev); |
| 436 | pm_runtime_set_active(dev); |
| 437 | pm_runtime_enable(dev); |
Winkler, Tomas | bc33c5d | 2016-09-12 16:04:19 +0300 | [diff] [blame] | 438 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame^] | 439 | rc = tpm_chip_register(chip); |
| 440 | if (rc) { |
| 441 | crb_go_idle(dev, priv); |
| 442 | pm_runtime_put_noidle(dev); |
| 443 | pm_runtime_disable(dev); |
| 444 | return rc; |
| 445 | } |
| 446 | |
| 447 | pm_runtime_put(dev); |
| 448 | |
| 449 | return 0; |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int crb_acpi_remove(struct acpi_device *device) |
| 453 | { |
| 454 | struct device *dev = &device->dev; |
| 455 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 456 | |
Jarkko Sakkinen | 99cda8c | 2016-02-18 22:11:29 +0200 | [diff] [blame] | 457 | tpm_chip_unregister(chip); |
| 458 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame^] | 459 | pm_runtime_disable(dev); |
| 460 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
Winkler, Tomas | e74f2f7 | 2016-10-08 14:59:39 +0300 | [diff] [blame^] | 464 | #ifdef CONFIG_PM |
| 465 | static int crb_pm_runtime_suspend(struct device *dev) |
| 466 | { |
| 467 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 468 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
| 469 | |
| 470 | return crb_go_idle(dev, priv); |
| 471 | } |
| 472 | |
| 473 | static int crb_pm_runtime_resume(struct device *dev) |
| 474 | { |
| 475 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 476 | struct crb_priv *priv = dev_get_drvdata(&chip->dev); |
| 477 | |
| 478 | return crb_cmd_ready(dev, priv); |
| 479 | } |
| 480 | #endif /* CONFIG_PM */ |
| 481 | |
| 482 | static const struct dev_pm_ops crb_pm = { |
| 483 | SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume) |
| 484 | SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL) |
| 485 | }; |
| 486 | |
Jarkko Sakkinen | 30fc8d1 | 2014-12-12 11:46:39 -0800 | [diff] [blame] | 487 | static struct acpi_device_id crb_device_ids[] = { |
| 488 | {"MSFT0101", 0}, |
| 489 | {"", 0}, |
| 490 | }; |
| 491 | MODULE_DEVICE_TABLE(acpi, crb_device_ids); |
| 492 | |
| 493 | static struct acpi_driver crb_acpi_driver = { |
| 494 | .name = "tpm_crb", |
| 495 | .ids = crb_device_ids, |
| 496 | .ops = { |
| 497 | .add = crb_acpi_add, |
| 498 | .remove = crb_acpi_remove, |
| 499 | }, |
| 500 | .drv = { |
| 501 | .pm = &crb_pm, |
| 502 | }, |
| 503 | }; |
| 504 | |
| 505 | module_acpi_driver(crb_acpi_driver); |
| 506 | MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>"); |
| 507 | MODULE_DESCRIPTION("TPM2 Driver"); |
| 508 | MODULE_VERSION("0.1"); |
| 509 | MODULE_LICENSE("GPL"); |