1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2005, 2006 IBM Corporation 4 * Copyright (C) 2014, 2015 Intel Corporation 5 * 6 * Authors: 7 * Leendert van Doorn <leendert@watson.ibm.com> 8 * Kylene Hall <kjhall@us.ibm.com> 9 * 10 * Maintained by: <tpmdd-devel@lists.sourceforge.net> 11 * 12 * Device driver for TCG/TCPA TPM (trusted platform module). 13 * Specifications at www.trustedcomputinggroup.org 14 * 15 * This device driver implements the TPM interface as defined in 16 * the TCG TPM Interface Spec version 1.2, revision 1.0. 17 */ 18 #include <linux/init.h> 19 #include <linux/module.h> 20 #include <linux/moduleparam.h> 21 #include <linux/pnp.h> 22 #include <linux/slab.h> 23 #include <linux/interrupt.h> 24 #include <linux/wait.h> 25 #include <linux/acpi.h> 26 #include <linux/freezer.h> 27 #include <linux/of.h> 28 #include <linux/kernel.h> 29 #include <linux/dmi.h> 30 #include "tpm.h" 31 #include "tpm_tis_core.h" 32 33 struct tpm_info { 34 struct resource res; 35 /* irq > 0 means: use irq $irq; 36 * irq = 0 means: autoprobe for an irq; 37 * irq = -1 means: no irq support 38 */ 39 int irq; 40 }; 41 42 struct tpm_tis_tcg_phy { 43 struct tpm_tis_data priv; 44 void __iomem *iobase; 45 }; 46 47 static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data) 48 { 49 return container_of(data, struct tpm_tis_tcg_phy, priv); 50 } 51 52 #ifdef CONFIG_PREEMPT_RT 53 /* 54 * Flush previous write operations with a dummy read operation to the 55 * TPM MMIO base address. 56 */ 57 static inline void tpm_tis_flush(void __iomem *iobase) 58 { 59 ioread8(iobase + TPM_ACCESS(0)); 60 } 61 #else 62 #define tpm_tis_flush(iobase) do { } while (0) 63 #endif 64 65 /* 66 * Write a byte word to the TPM MMIO address, and flush the write queue. 67 * The flush ensures that the data is sent immediately over the bus and not 68 * aggregated with further requests and transferred later in a batch. The large 69 * write requests can lead to unwanted latency spikes by blocking the CPU until 70 * the complete batch has been transferred. 71 */ 72 static inline void tpm_tis_iowrite8(u8 b, void __iomem *iobase, u32 addr) 73 { 74 iowrite8(b, iobase + addr); 75 tpm_tis_flush(iobase); 76 } 77 78 /* 79 * Write a 32-bit word to the TPM MMIO address, and flush the write queue. 80 * The flush ensures that the data is sent immediately over the bus and not 81 * aggregated with further requests and transferred later in a batch. The large 82 * write requests can lead to unwanted latency spikes by blocking the CPU until 83 * the complete batch has been transferred. 84 */ 85 static inline void tpm_tis_iowrite32(u32 b, void __iomem *iobase, u32 addr) 86 { 87 iowrite32(b, iobase + addr); 88 tpm_tis_flush(iobase); 89 } 90 91 static int interrupts = -1; 92 module_param(interrupts, int, 0444); 93 MODULE_PARM_DESC(interrupts, "Enable interrupts"); 94 95 static bool itpm; 96 module_param(itpm, bool, 0444); 97 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)"); 98 99 static bool force; 100 #ifdef CONFIG_X86 101 module_param(force, bool, 0444); 102 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry"); 103 #endif 104 105 static int tpm_tis_disable_irq(const struct dmi_system_id *d) 106 { 107 if (interrupts == -1) { 108 pr_notice("tpm_tis: %s detected: disabling interrupts.\n", d->ident); 109 interrupts = 0; 110 } 111 112 return 0; 113 } 114 115 static const struct dmi_system_id tpm_tis_dmi_table[] = { 116 { 117 .callback = tpm_tis_disable_irq, 118 .ident = "ThinkPad T490s", 119 .matches = { 120 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 121 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T490s"), 122 }, 123 }, 124 { 125 .callback = tpm_tis_disable_irq, 126 .ident = "ThinkStation P360 Tiny", 127 .matches = { 128 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 129 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkStation P360 Tiny"), 130 }, 131 }, 132 { 133 .callback = tpm_tis_disable_irq, 134 .ident = "ThinkPad L490", 135 .matches = { 136 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 137 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad L490"), 138 }, 139 }, 140 { 141 .callback = tpm_tis_disable_irq, 142 .ident = "UPX-TGL", 143 .matches = { 144 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"), 145 }, 146 }, 147 {} 148 }; 149 150 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI) 151 static int has_hid(struct acpi_device *dev, const char *hid) 152 { 153 struct acpi_hardware_id *id; 154 155 list_for_each_entry(id, &dev->pnp.ids, list) 156 if (!strcmp(hid, id->id)) 157 return 1; 158 159 return 0; 160 } 161 162 static inline int is_itpm(struct acpi_device *dev) 163 { 164 if (!dev) 165 return 0; 166 return has_hid(dev, "INTC0102"); 167 } 168 #else 169 static inline int is_itpm(struct acpi_device *dev) 170 { 171 return 0; 172 } 173 #endif 174 175 #if defined(CONFIG_ACPI) 176 #define DEVICE_IS_TPM2 1 177 178 static const struct acpi_device_id tpm_acpi_tbl[] = { 179 {"MSFT0101", DEVICE_IS_TPM2}, 180 {}, 181 }; 182 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl); 183 184 static int check_acpi_tpm2(struct device *dev) 185 { 186 const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev); 187 struct acpi_table_tpm2 *tbl; 188 acpi_status st; 189 int ret = 0; 190 191 if (!aid || aid->driver_data != DEVICE_IS_TPM2) 192 return 0; 193 194 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2 195 * table is mandatory 196 */ 197 st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl); 198 if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) { 199 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n"); 200 return -EINVAL; 201 } 202 203 /* The tpm2_crb driver handles this device */ 204 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED) 205 ret = -ENODEV; 206 207 acpi_put_table((struct acpi_table_header *)tbl); 208 return ret; 209 } 210 #else 211 static int check_acpi_tpm2(struct device *dev) 212 { 213 return 0; 214 } 215 #endif 216 217 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len, 218 u8 *result, enum tpm_tis_io_mode io_mode) 219 { 220 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 221 __le16 result_le16; 222 __le32 result_le32; 223 224 switch (io_mode) { 225 case TPM_TIS_PHYS_8: 226 while (len--) 227 *result++ = ioread8(phy->iobase + addr); 228 break; 229 case TPM_TIS_PHYS_16: 230 result_le16 = cpu_to_le16(ioread16(phy->iobase + addr)); 231 memcpy(result, &result_le16, sizeof(u16)); 232 break; 233 case TPM_TIS_PHYS_32: 234 result_le32 = cpu_to_le32(ioread32(phy->iobase + addr)); 235 memcpy(result, &result_le32, sizeof(u32)); 236 break; 237 } 238 239 return 0; 240 } 241 242 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len, 243 const u8 *value, enum tpm_tis_io_mode io_mode) 244 { 245 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 246 247 switch (io_mode) { 248 case TPM_TIS_PHYS_8: 249 while (len--) 250 tpm_tis_iowrite8(*value++, phy->iobase, addr); 251 break; 252 case TPM_TIS_PHYS_16: 253 return -EINVAL; 254 case TPM_TIS_PHYS_32: 255 tpm_tis_iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase, addr); 256 break; 257 } 258 259 return 0; 260 } 261 262 static const struct tpm_tis_phy_ops tpm_tcg = { 263 .read_bytes = tpm_tcg_read_bytes, 264 .write_bytes = tpm_tcg_write_bytes, 265 }; 266 267 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info) 268 { 269 struct tpm_tis_tcg_phy *phy; 270 int irq = -1; 271 int rc; 272 273 dmi_check_system(tpm_tis_dmi_table); 274 275 rc = check_acpi_tpm2(dev); 276 if (rc) 277 return rc; 278 279 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL); 280 if (phy == NULL) 281 return -ENOMEM; 282 283 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res); 284 if (IS_ERR(phy->iobase)) 285 return PTR_ERR(phy->iobase); 286 287 if (interrupts) 288 irq = tpm_info->irq; 289 290 if (itpm || is_itpm(ACPI_COMPANION(dev))) 291 set_bit(TPM_TIS_ITPM_WORKAROUND, &phy->priv.flags); 292 293 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg, 294 ACPI_HANDLE(dev)); 295 } 296 297 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume); 298 299 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev, 300 const struct pnp_device_id *pnp_id) 301 { 302 struct tpm_info tpm_info = {}; 303 struct resource *res; 304 305 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0); 306 if (!res) 307 return -ENODEV; 308 tpm_info.res = *res; 309 310 if (pnp_irq_valid(pnp_dev, 0)) 311 tpm_info.irq = pnp_irq(pnp_dev, 0); 312 else 313 tpm_info.irq = -1; 314 315 return tpm_tis_init(&pnp_dev->dev, &tpm_info); 316 } 317 318 /* 319 * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module 320 * parameter"). This commit added IFX0102 device ID, which is also used by 321 * tpm_infineon but ignored to add quirks to probe which driver ought to be 322 * used. 323 */ 324 325 static struct pnp_device_id tpm_pnp_tbl[] = { 326 {"PNP0C31", 0}, /* TPM */ 327 {"ATM1200", 0}, /* Atmel */ 328 {"IFX0102", 0}, /* Infineon */ 329 {"BCM0101", 0}, /* Broadcom */ 330 {"BCM0102", 0}, /* Broadcom */ 331 {"NSC1200", 0}, /* National */ 332 {"ICO0102", 0}, /* Intel */ 333 /* Add new here */ 334 {"", 0}, /* User Specified */ 335 {"", 0} /* Terminator */ 336 }; 337 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl); 338 339 static void tpm_tis_pnp_remove(struct pnp_dev *dev) 340 { 341 struct tpm_chip *chip = pnp_get_drvdata(dev); 342 343 tpm_chip_unregister(chip); 344 tpm_tis_remove(chip); 345 } 346 347 static struct pnp_driver tis_pnp_driver = { 348 .name = "tpm_tis", 349 .id_table = tpm_pnp_tbl, 350 .probe = tpm_tis_pnp_init, 351 .remove = tpm_tis_pnp_remove, 352 .driver = { 353 .pm = &tpm_tis_pm, 354 }, 355 }; 356 357 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2) 358 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id, 359 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444); 360 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe"); 361 362 static struct platform_device *force_pdev; 363 364 static int tpm_tis_plat_probe(struct platform_device *pdev) 365 { 366 struct tpm_info tpm_info = {}; 367 struct resource *res; 368 369 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 370 if (res == NULL) { 371 dev_err(&pdev->dev, "no memory resource defined\n"); 372 return -ENODEV; 373 } 374 tpm_info.res = *res; 375 376 tpm_info.irq = platform_get_irq_optional(pdev, 0); 377 if (tpm_info.irq <= 0) { 378 if (pdev != force_pdev) 379 tpm_info.irq = -1; 380 else 381 /* When forcing auto probe the IRQ */ 382 tpm_info.irq = 0; 383 } 384 385 return tpm_tis_init(&pdev->dev, &tpm_info); 386 } 387 388 static void tpm_tis_plat_remove(struct platform_device *pdev) 389 { 390 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev); 391 392 tpm_chip_unregister(chip); 393 tpm_tis_remove(chip); 394 } 395 396 #ifdef CONFIG_OF 397 static const struct of_device_id tis_of_platform_match[] = { 398 {.compatible = "tcg,tpm-tis-mmio"}, 399 {}, 400 }; 401 MODULE_DEVICE_TABLE(of, tis_of_platform_match); 402 #endif 403 404 static struct platform_driver tis_drv = { 405 .probe = tpm_tis_plat_probe, 406 .remove_new = tpm_tis_plat_remove, 407 .driver = { 408 .name = "tpm_tis", 409 .pm = &tpm_tis_pm, 410 .of_match_table = of_match_ptr(tis_of_platform_match), 411 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl), 412 }, 413 }; 414 415 static int tpm_tis_force_device(void) 416 { 417 struct platform_device *pdev; 418 static const struct resource x86_resources[] = { 419 DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN) 420 }; 421 422 if (!force) 423 return 0; 424 425 /* The driver core will match the name tpm_tis of the device to 426 * the tpm_tis platform driver and complete the setup via 427 * tpm_tis_plat_probe 428 */ 429 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources, 430 ARRAY_SIZE(x86_resources)); 431 if (IS_ERR(pdev)) 432 return PTR_ERR(pdev); 433 force_pdev = pdev; 434 435 return 0; 436 } 437 438 static int __init init_tis(void) 439 { 440 int rc; 441 442 rc = tpm_tis_force_device(); 443 if (rc) 444 goto err_force; 445 446 rc = platform_driver_register(&tis_drv); 447 if (rc) 448 goto err_platform; 449 450 451 if (IS_ENABLED(CONFIG_PNP)) { 452 rc = pnp_register_driver(&tis_pnp_driver); 453 if (rc) 454 goto err_pnp; 455 } 456 457 return 0; 458 459 err_pnp: 460 platform_driver_unregister(&tis_drv); 461 err_platform: 462 if (force_pdev) 463 platform_device_unregister(force_pdev); 464 err_force: 465 return rc; 466 } 467 468 static void __exit cleanup_tis(void) 469 { 470 pnp_unregister_driver(&tis_pnp_driver); 471 platform_driver_unregister(&tis_drv); 472 473 if (force_pdev) 474 platform_device_unregister(force_pdev); 475 } 476 477 module_init(init_tis); 478 module_exit(cleanup_tis); 479 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 480 MODULE_DESCRIPTION("TPM Driver"); 481 MODULE_VERSION("2.0"); 482 MODULE_LICENSE("GPL"); 483