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