1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005, 2006 IBM Corporation
4 * Copyright (C) 2014, 2015 Intel Corporation
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This device driver implements the TPM interface as defined in
16 * the TCG TPM Interface Spec version 1.2, revision 1.0.
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>
28 #include <linux/of_device.h>
29 #include <linux/kernel.h>
30 #include <linux/dmi.h>
32 #include "tpm_tis_core.h"
36 /* irq > 0 means: use irq $irq;
37 * irq = 0 means: autoprobe for an irq;
38 * irq = -1 means: no irq support
43 struct tpm_tis_tcg_phy {
44 struct tpm_tis_data priv;
48 static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
50 return container_of(data, struct tpm_tis_tcg_phy, priv);
53 #ifdef CONFIG_PREEMPT_RT
55 * Flush previous write operations with a dummy read operation to the
56 * TPM MMIO base address.
58 static inline void tpm_tis_flush(void __iomem *iobase)
60 ioread8(iobase + TPM_ACCESS(0));
63 #define tpm_tis_flush(iobase) do { } while (0)
67 * Write a byte word to the TPM MMIO address, and flush the write queue.
68 * The flush ensures that the data is sent immediately over the bus and not
69 * aggregated with further requests and transferred later in a batch. The large
70 * write requests can lead to unwanted latency spikes by blocking the CPU until
71 * the complete batch has been transferred.
73 static inline void tpm_tis_iowrite8(u8 b, void __iomem *iobase, u32 addr)
75 iowrite8(b, iobase + addr);
76 tpm_tis_flush(iobase);
80 * Write a 32-bit word to the TPM MMIO address, and flush the write queue.
81 * The flush ensures that the data is sent immediately over the bus and not
82 * aggregated with further requests and transferred later in a batch. The large
83 * write requests can lead to unwanted latency spikes by blocking the CPU until
84 * the complete batch has been transferred.
86 static inline void tpm_tis_iowrite32(u32 b, void __iomem *iobase, u32 addr)
88 iowrite32(b, iobase + addr);
89 tpm_tis_flush(iobase);
92 static int interrupts = -1;
93 module_param(interrupts, int, 0444);
94 MODULE_PARM_DESC(interrupts, "Enable interrupts");
97 module_param(itpm, bool, 0444);
98 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
102 module_param(force, bool, 0444);
103 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
106 static int tpm_tis_disable_irq(const struct dmi_system_id *d)
108 if (interrupts == -1) {
109 pr_notice("tpm_tis: %s detected: disabling interrupts.\n", d->ident);
116 static const struct dmi_system_id tpm_tis_dmi_table[] = {
118 .callback = tpm_tis_disable_irq,
119 .ident = "ThinkPad T490s",
121 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
122 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T490s"),
128 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
129 static int has_hid(struct acpi_device *dev, const char *hid)
131 struct acpi_hardware_id *id;
133 list_for_each_entry(id, &dev->pnp.ids, list)
134 if (!strcmp(hid, id->id))
140 static inline int is_itpm(struct acpi_device *dev)
144 return has_hid(dev, "INTC0102");
147 static inline int is_itpm(struct acpi_device *dev)
153 #if defined(CONFIG_ACPI)
154 #define DEVICE_IS_TPM2 1
156 static const struct acpi_device_id tpm_acpi_tbl[] = {
157 {"MSFT0101", DEVICE_IS_TPM2},
160 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
162 static int check_acpi_tpm2(struct device *dev)
164 const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
165 struct acpi_table_tpm2 *tbl;
169 if (!aid || aid->driver_data != DEVICE_IS_TPM2)
172 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
175 st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
176 if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
177 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
181 /* The tpm2_crb driver handles this device */
182 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
185 acpi_put_table((struct acpi_table_header *)tbl);
189 static int check_acpi_tpm2(struct device *dev)
195 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
196 u8 *result, enum tpm_tis_io_mode io_mode)
198 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
205 *result++ = ioread8(phy->iobase + addr);
207 case TPM_TIS_PHYS_16:
208 result_le16 = cpu_to_le16(ioread16(phy->iobase + addr));
209 memcpy(result, &result_le16, sizeof(u16));
211 case TPM_TIS_PHYS_32:
212 result_le32 = cpu_to_le32(ioread32(phy->iobase + addr));
213 memcpy(result, &result_le32, sizeof(u32));
220 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
221 const u8 *value, enum tpm_tis_io_mode io_mode)
223 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
228 tpm_tis_iowrite8(*value++, phy->iobase, addr);
230 case TPM_TIS_PHYS_16:
232 case TPM_TIS_PHYS_32:
233 tpm_tis_iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase, addr);
240 static const struct tpm_tis_phy_ops tpm_tcg = {
241 .read_bytes = tpm_tcg_read_bytes,
242 .write_bytes = tpm_tcg_write_bytes,
245 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
247 struct tpm_tis_tcg_phy *phy;
251 dmi_check_system(tpm_tis_dmi_table);
253 rc = check_acpi_tpm2(dev);
257 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
261 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
262 if (IS_ERR(phy->iobase))
263 return PTR_ERR(phy->iobase);
268 if (itpm || is_itpm(ACPI_COMPANION(dev)))
269 set_bit(TPM_TIS_ITPM_WORKAROUND, &phy->priv.flags);
271 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
275 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
277 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
278 const struct pnp_device_id *pnp_id)
280 struct tpm_info tpm_info = {};
281 struct resource *res;
283 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
288 if (pnp_irq_valid(pnp_dev, 0))
289 tpm_info.irq = pnp_irq(pnp_dev, 0);
293 return tpm_tis_init(&pnp_dev->dev, &tpm_info);
297 * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module
298 * parameter"). This commit added IFX0102 device ID, which is also used by
299 * tpm_infineon but ignored to add quirks to probe which driver ought to be
303 static struct pnp_device_id tpm_pnp_tbl[] = {
304 {"PNP0C31", 0}, /* TPM */
305 {"ATM1200", 0}, /* Atmel */
306 {"IFX0102", 0}, /* Infineon */
307 {"BCM0101", 0}, /* Broadcom */
308 {"BCM0102", 0}, /* Broadcom */
309 {"NSC1200", 0}, /* National */
310 {"ICO0102", 0}, /* Intel */
312 {"", 0}, /* User Specified */
313 {"", 0} /* Terminator */
315 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
317 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
319 struct tpm_chip *chip = pnp_get_drvdata(dev);
321 tpm_chip_unregister(chip);
322 tpm_tis_remove(chip);
325 static struct pnp_driver tis_pnp_driver = {
327 .id_table = tpm_pnp_tbl,
328 .probe = tpm_tis_pnp_init,
329 .remove = tpm_tis_pnp_remove,
335 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
336 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
337 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
338 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
340 static struct platform_device *force_pdev;
342 static int tpm_tis_plat_probe(struct platform_device *pdev)
344 struct tpm_info tpm_info = {};
345 struct resource *res;
347 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
349 dev_err(&pdev->dev, "no memory resource defined\n");
354 tpm_info.irq = platform_get_irq_optional(pdev, 0);
355 if (tpm_info.irq <= 0) {
356 if (pdev != force_pdev)
359 /* When forcing auto probe the IRQ */
363 return tpm_tis_init(&pdev->dev, &tpm_info);
366 static void tpm_tis_plat_remove(struct platform_device *pdev)
368 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
370 tpm_chip_unregister(chip);
371 tpm_tis_remove(chip);
375 static const struct of_device_id tis_of_platform_match[] = {
376 {.compatible = "tcg,tpm-tis-mmio"},
379 MODULE_DEVICE_TABLE(of, tis_of_platform_match);
382 static struct platform_driver tis_drv = {
383 .probe = tpm_tis_plat_probe,
384 .remove_new = tpm_tis_plat_remove,
388 .of_match_table = of_match_ptr(tis_of_platform_match),
389 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
393 static int tpm_tis_force_device(void)
395 struct platform_device *pdev;
396 static const struct resource x86_resources[] = {
397 DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN)
403 /* The driver core will match the name tpm_tis of the device to
404 * the tpm_tis platform driver and complete the setup via
407 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
408 ARRAY_SIZE(x86_resources));
410 return PTR_ERR(pdev);
416 static int __init init_tis(void)
420 rc = tpm_tis_force_device();
424 rc = platform_driver_register(&tis_drv);
429 if (IS_ENABLED(CONFIG_PNP)) {
430 rc = pnp_register_driver(&tis_pnp_driver);
438 platform_driver_unregister(&tis_drv);
441 platform_device_unregister(force_pdev);
446 static void __exit cleanup_tis(void)
448 pnp_unregister_driver(&tis_pnp_driver);
449 platform_driver_unregister(&tis_drv);
452 platform_device_unregister(force_pdev);
455 module_init(init_tis);
456 module_exit(cleanup_tis);
458 MODULE_DESCRIPTION("TPM Driver");
459 MODULE_VERSION("2.0");
460 MODULE_LICENSE("GPL");