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/of_device.h> 29 #include <linux/kernel.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 bool interrupts; 92 module_param(interrupts, bool, 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 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI) 106 static int has_hid(struct acpi_device *dev, const char *hid) 107 { 108 struct acpi_hardware_id *id; 109 110 list_for_each_entry(id, &dev->pnp.ids, list) 111 if (!strcmp(hid, id->id)) 112 return 1; 113 114 return 0; 115 } 116 117 static inline int is_itpm(struct acpi_device *dev) 118 { 119 if (!dev) 120 return 0; 121 return has_hid(dev, "INTC0102"); 122 } 123 #else 124 static inline int is_itpm(struct acpi_device *dev) 125 { 126 return 0; 127 } 128 #endif 129 130 #if defined(CONFIG_ACPI) 131 #define DEVICE_IS_TPM2 1 132 133 static const struct acpi_device_id tpm_acpi_tbl[] = { 134 {"MSFT0101", DEVICE_IS_TPM2}, 135 {}, 136 }; 137 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl); 138 139 static int check_acpi_tpm2(struct device *dev) 140 { 141 const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev); 142 struct acpi_table_tpm2 *tbl; 143 acpi_status st; 144 int ret = 0; 145 146 if (!aid || aid->driver_data != DEVICE_IS_TPM2) 147 return 0; 148 149 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2 150 * table is mandatory 151 */ 152 st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl); 153 if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) { 154 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n"); 155 return -EINVAL; 156 } 157 158 /* The tpm2_crb driver handles this device */ 159 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED) 160 ret = -ENODEV; 161 162 acpi_put_table((struct acpi_table_header *)tbl); 163 return ret; 164 } 165 #else 166 static int check_acpi_tpm2(struct device *dev) 167 { 168 return 0; 169 } 170 #endif 171 172 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len, 173 u8 *result, enum tpm_tis_io_mode io_mode) 174 { 175 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 176 __le16 result_le16; 177 __le32 result_le32; 178 179 switch (io_mode) { 180 case TPM_TIS_PHYS_8: 181 while (len--) 182 *result++ = ioread8(phy->iobase + addr); 183 break; 184 case TPM_TIS_PHYS_16: 185 result_le16 = cpu_to_le16(ioread16(phy->iobase + addr)); 186 memcpy(result, &result_le16, sizeof(u16)); 187 break; 188 case TPM_TIS_PHYS_32: 189 result_le32 = cpu_to_le32(ioread32(phy->iobase + addr)); 190 memcpy(result, &result_le32, sizeof(u32)); 191 break; 192 } 193 194 return 0; 195 } 196 197 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len, 198 const u8 *value, enum tpm_tis_io_mode io_mode) 199 { 200 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); 201 202 switch (io_mode) { 203 case TPM_TIS_PHYS_8: 204 while (len--) 205 tpm_tis_iowrite8(*value++, phy->iobase, addr); 206 break; 207 case TPM_TIS_PHYS_16: 208 return -EINVAL; 209 case TPM_TIS_PHYS_32: 210 tpm_tis_iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase, addr); 211 break; 212 } 213 214 return 0; 215 } 216 217 static const struct tpm_tis_phy_ops tpm_tcg = { 218 .read_bytes = tpm_tcg_read_bytes, 219 .write_bytes = tpm_tcg_write_bytes, 220 }; 221 222 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info) 223 { 224 struct tpm_tis_tcg_phy *phy; 225 int irq = -1; 226 int rc; 227 228 rc = check_acpi_tpm2(dev); 229 if (rc) 230 return rc; 231 232 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL); 233 if (phy == NULL) 234 return -ENOMEM; 235 236 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res); 237 if (IS_ERR(phy->iobase)) 238 return PTR_ERR(phy->iobase); 239 240 if (interrupts) 241 irq = tpm_info->irq; 242 243 if (itpm || is_itpm(ACPI_COMPANION(dev))) 244 set_bit(TPM_TIS_ITPM_WORKAROUND, &phy->priv.flags); 245 246 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg, 247 ACPI_HANDLE(dev)); 248 } 249 250 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume); 251 252 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev, 253 const struct pnp_device_id *pnp_id) 254 { 255 struct tpm_info tpm_info = {}; 256 struct resource *res; 257 258 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0); 259 if (!res) 260 return -ENODEV; 261 tpm_info.res = *res; 262 263 if (pnp_irq_valid(pnp_dev, 0)) 264 tpm_info.irq = pnp_irq(pnp_dev, 0); 265 else 266 tpm_info.irq = -1; 267 268 return tpm_tis_init(&pnp_dev->dev, &tpm_info); 269 } 270 271 /* 272 * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module 273 * parameter"). This commit added IFX0102 device ID, which is also used by 274 * tpm_infineon but ignored to add quirks to probe which driver ought to be 275 * used. 276 */ 277 278 static struct pnp_device_id tpm_pnp_tbl[] = { 279 {"PNP0C31", 0}, /* TPM */ 280 {"ATM1200", 0}, /* Atmel */ 281 {"IFX0102", 0}, /* Infineon */ 282 {"BCM0101", 0}, /* Broadcom */ 283 {"BCM0102", 0}, /* Broadcom */ 284 {"NSC1200", 0}, /* National */ 285 {"ICO0102", 0}, /* Intel */ 286 /* Add new here */ 287 {"", 0}, /* User Specified */ 288 {"", 0} /* Terminator */ 289 }; 290 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl); 291 292 static void tpm_tis_pnp_remove(struct pnp_dev *dev) 293 { 294 struct tpm_chip *chip = pnp_get_drvdata(dev); 295 296 tpm_chip_unregister(chip); 297 tpm_tis_remove(chip); 298 } 299 300 static struct pnp_driver tis_pnp_driver = { 301 .name = "tpm_tis", 302 .id_table = tpm_pnp_tbl, 303 .probe = tpm_tis_pnp_init, 304 .remove = tpm_tis_pnp_remove, 305 .driver = { 306 .pm = &tpm_tis_pm, 307 }, 308 }; 309 310 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2) 311 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id, 312 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444); 313 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe"); 314 315 static struct platform_device *force_pdev; 316 317 static int tpm_tis_plat_probe(struct platform_device *pdev) 318 { 319 struct tpm_info tpm_info = {}; 320 struct resource *res; 321 322 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 323 if (res == NULL) { 324 dev_err(&pdev->dev, "no memory resource defined\n"); 325 return -ENODEV; 326 } 327 tpm_info.res = *res; 328 329 tpm_info.irq = platform_get_irq_optional(pdev, 0); 330 if (tpm_info.irq <= 0) { 331 if (pdev != force_pdev) 332 tpm_info.irq = -1; 333 else 334 /* When forcing auto probe the IRQ */ 335 tpm_info.irq = 0; 336 } 337 338 return tpm_tis_init(&pdev->dev, &tpm_info); 339 } 340 341 static void tpm_tis_plat_remove(struct platform_device *pdev) 342 { 343 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev); 344 345 tpm_chip_unregister(chip); 346 tpm_tis_remove(chip); 347 } 348 349 #ifdef CONFIG_OF 350 static const struct of_device_id tis_of_platform_match[] = { 351 {.compatible = "tcg,tpm-tis-mmio"}, 352 {}, 353 }; 354 MODULE_DEVICE_TABLE(of, tis_of_platform_match); 355 #endif 356 357 static struct platform_driver tis_drv = { 358 .probe = tpm_tis_plat_probe, 359 .remove_new = tpm_tis_plat_remove, 360 .driver = { 361 .name = "tpm_tis", 362 .pm = &tpm_tis_pm, 363 .of_match_table = of_match_ptr(tis_of_platform_match), 364 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl), 365 }, 366 }; 367 368 static int tpm_tis_force_device(void) 369 { 370 struct platform_device *pdev; 371 static const struct resource x86_resources[] = { 372 DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN) 373 }; 374 375 if (!force) 376 return 0; 377 378 /* The driver core will match the name tpm_tis of the device to 379 * the tpm_tis platform driver and complete the setup via 380 * tpm_tis_plat_probe 381 */ 382 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources, 383 ARRAY_SIZE(x86_resources)); 384 if (IS_ERR(pdev)) 385 return PTR_ERR(pdev); 386 force_pdev = pdev; 387 388 return 0; 389 } 390 391 static int __init init_tis(void) 392 { 393 int rc; 394 395 rc = tpm_tis_force_device(); 396 if (rc) 397 goto err_force; 398 399 rc = platform_driver_register(&tis_drv); 400 if (rc) 401 goto err_platform; 402 403 404 if (IS_ENABLED(CONFIG_PNP)) { 405 rc = pnp_register_driver(&tis_pnp_driver); 406 if (rc) 407 goto err_pnp; 408 } 409 410 return 0; 411 412 err_pnp: 413 platform_driver_unregister(&tis_drv); 414 err_platform: 415 if (force_pdev) 416 platform_device_unregister(force_pdev); 417 err_force: 418 return rc; 419 } 420 421 static void __exit cleanup_tis(void) 422 { 423 pnp_unregister_driver(&tis_pnp_driver); 424 platform_driver_unregister(&tis_drv); 425 426 if (force_pdev) 427 platform_device_unregister(force_pdev); 428 } 429 430 module_init(init_tis); 431 module_exit(cleanup_tis); 432 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 433 MODULE_DESCRIPTION("TPM Driver"); 434 MODULE_VERSION("2.0"); 435 MODULE_LICENSE("GPL"); 436