1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005, 2006 IBM Corporation
4 * Copyright (C) 2014, 2015 Intel Corporation
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
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/kernel.h>
30 #include "tpm_tis_core.h"
34 /* irq > 0 means: use irq $irq;
35 * irq = 0 means: autoprobe for an irq;
36 * irq = -1 means: no irq support
41 struct tpm_tis_tcg_phy
{
42 struct tpm_tis_data priv
;
46 static inline struct tpm_tis_tcg_phy
*to_tpm_tis_tcg_phy(struct tpm_tis_data
*data
)
48 return container_of(data
, struct tpm_tis_tcg_phy
, priv
);
51 #ifdef CONFIG_PREEMPT_RT
53 * Flush previous write operations with a dummy read operation to the
54 * TPM MMIO base address.
56 static inline void tpm_tis_flush(void __iomem
*iobase
)
58 ioread8(iobase
+ TPM_ACCESS(0));
61 #define tpm_tis_flush(iobase) do { } while (0)
65 * Write a byte word to the TPM MMIO address, and flush the write queue.
66 * The flush ensures that the data is sent immediately over the bus and not
67 * aggregated with further requests and transferred later in a batch. The large
68 * write requests can lead to unwanted latency spikes by blocking the CPU until
69 * the complete batch has been transferred.
71 static inline void tpm_tis_iowrite8(u8 b
, void __iomem
*iobase
, u32 addr
)
73 iowrite8(b
, iobase
+ addr
);
74 tpm_tis_flush(iobase
);
78 * Write a 32-bit word to the TPM MMIO address, and flush the write queue.
79 * The flush ensures that the data is sent immediately over the bus and not
80 * aggregated with further requests and transferred later in a batch. The large
81 * write requests can lead to unwanted latency spikes by blocking the CPU until
82 * the complete batch has been transferred.
84 static inline void tpm_tis_iowrite32(u32 b
, void __iomem
*iobase
, u32 addr
)
86 iowrite32(b
, iobase
+ addr
);
87 tpm_tis_flush(iobase
);
90 static bool interrupts
;
91 module_param(interrupts
, bool, 0444);
92 MODULE_PARM_DESC(interrupts
, "Enable interrupts");
95 module_param(itpm
, bool, 0444);
96 MODULE_PARM_DESC(itpm
, "Force iTPM workarounds (found on some Lenovo laptops)");
100 module_param(force
, bool, 0444);
101 MODULE_PARM_DESC(force
, "Force device probe rather than using ACPI entry");
104 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
105 static int has_hid(struct acpi_device
*dev
, const char *hid
)
107 struct acpi_hardware_id
*id
;
109 list_for_each_entry(id
, &dev
->pnp
.ids
, list
)
110 if (!strcmp(hid
, id
->id
))
116 static inline int is_itpm(struct acpi_device
*dev
)
120 return has_hid(dev
, "INTC0102");
123 static inline int is_itpm(struct acpi_device
*dev
)
129 #if defined(CONFIG_ACPI)
130 #define DEVICE_IS_TPM2 1
132 static const struct acpi_device_id tpm_acpi_tbl
[] = {
133 {"MSFT0101", DEVICE_IS_TPM2
},
136 MODULE_DEVICE_TABLE(acpi
, tpm_acpi_tbl
);
138 static int check_acpi_tpm2(struct device
*dev
)
140 const struct acpi_device_id
*aid
= acpi_match_device(tpm_acpi_tbl
, dev
);
141 struct acpi_table_tpm2
*tbl
;
145 if (!aid
|| aid
->driver_data
!= DEVICE_IS_TPM2
)
148 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
151 st
= acpi_get_table(ACPI_SIG_TPM2
, 1, (struct acpi_table_header
**)&tbl
);
152 if (ACPI_FAILURE(st
) || tbl
->header
.length
< sizeof(*tbl
)) {
153 dev_err(dev
, FW_BUG
"failed to get TPM2 ACPI table\n");
157 /* The tpm2_crb driver handles this device */
158 if (tbl
->start_method
!= ACPI_TPM2_MEMORY_MAPPED
)
161 acpi_put_table((struct acpi_table_header
*)tbl
);
165 static int check_acpi_tpm2(struct device
*dev
)
171 static int tpm_tcg_read_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
172 u8
*result
, enum tpm_tis_io_mode io_mode
)
174 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
181 *result
++ = ioread8(phy
->iobase
+ addr
);
183 case TPM_TIS_PHYS_16
:
184 result_le16
= cpu_to_le16(ioread16(phy
->iobase
+ addr
));
185 memcpy(result
, &result_le16
, sizeof(u16
));
187 case TPM_TIS_PHYS_32
:
188 result_le32
= cpu_to_le32(ioread32(phy
->iobase
+ addr
));
189 memcpy(result
, &result_le32
, sizeof(u32
));
196 static int tpm_tcg_write_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
197 const u8
*value
, enum tpm_tis_io_mode io_mode
)
199 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
204 tpm_tis_iowrite8(*value
++, phy
->iobase
, addr
);
206 case TPM_TIS_PHYS_16
:
208 case TPM_TIS_PHYS_32
:
209 tpm_tis_iowrite32(le32_to_cpu(*((__le32
*)value
)), phy
->iobase
, addr
);
216 static const struct tpm_tis_phy_ops tpm_tcg
= {
217 .read_bytes
= tpm_tcg_read_bytes
,
218 .write_bytes
= tpm_tcg_write_bytes
,
221 static int tpm_tis_init(struct device
*dev
, struct tpm_info
*tpm_info
)
223 struct tpm_tis_tcg_phy
*phy
;
227 rc
= check_acpi_tpm2(dev
);
231 phy
= devm_kzalloc(dev
, sizeof(struct tpm_tis_tcg_phy
), GFP_KERNEL
);
235 phy
->iobase
= devm_ioremap_resource(dev
, &tpm_info
->res
);
236 if (IS_ERR(phy
->iobase
))
237 return PTR_ERR(phy
->iobase
);
242 if (itpm
|| is_itpm(ACPI_COMPANION(dev
)))
243 set_bit(TPM_TIS_ITPM_WORKAROUND
, &phy
->priv
.flags
);
245 return tpm_tis_core_init(dev
, &phy
->priv
, irq
, &tpm_tcg
,
249 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
251 static int tpm_tis_pnp_init(struct pnp_dev
*pnp_dev
,
252 const struct pnp_device_id
*pnp_id
)
254 struct tpm_info tpm_info
= {};
255 struct resource
*res
;
257 res
= pnp_get_resource(pnp_dev
, IORESOURCE_MEM
, 0);
262 if (pnp_irq_valid(pnp_dev
, 0))
263 tpm_info
.irq
= pnp_irq(pnp_dev
, 0);
267 return tpm_tis_init(&pnp_dev
->dev
, &tpm_info
);
271 * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module
272 * parameter"). This commit added IFX0102 device ID, which is also used by
273 * tpm_infineon but ignored to add quirks to probe which driver ought to be
277 static struct pnp_device_id tpm_pnp_tbl
[] = {
278 {"PNP0C31", 0}, /* TPM */
279 {"ATM1200", 0}, /* Atmel */
280 {"IFX0102", 0}, /* Infineon */
281 {"BCM0101", 0}, /* Broadcom */
282 {"BCM0102", 0}, /* Broadcom */
283 {"NSC1200", 0}, /* National */
284 {"ICO0102", 0}, /* Intel */
286 {"", 0}, /* User Specified */
287 {"", 0} /* Terminator */
289 MODULE_DEVICE_TABLE(pnp
, tpm_pnp_tbl
);
291 static void tpm_tis_pnp_remove(struct pnp_dev
*dev
)
293 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
295 tpm_chip_unregister(chip
);
296 tpm_tis_remove(chip
);
299 static struct pnp_driver tis_pnp_driver
= {
301 .id_table
= tpm_pnp_tbl
,
302 .probe
= tpm_tis_pnp_init
,
303 .remove
= tpm_tis_pnp_remove
,
309 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
310 module_param_string(hid
, tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
,
311 sizeof(tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
), 0444);
312 MODULE_PARM_DESC(hid
, "Set additional specific HID for this driver to probe");
314 static struct platform_device
*force_pdev
;
316 static int tpm_tis_plat_probe(struct platform_device
*pdev
)
318 struct tpm_info tpm_info
= {};
319 struct resource
*res
;
321 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
323 dev_err(&pdev
->dev
, "no memory resource defined\n");
328 tpm_info
.irq
= platform_get_irq_optional(pdev
, 0);
329 if (tpm_info
.irq
<= 0) {
330 if (pdev
!= force_pdev
)
333 /* When forcing auto probe the IRQ */
337 return tpm_tis_init(&pdev
->dev
, &tpm_info
);
340 static void tpm_tis_plat_remove(struct platform_device
*pdev
)
342 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
344 tpm_chip_unregister(chip
);
345 tpm_tis_remove(chip
);
349 static const struct of_device_id tis_of_platform_match
[] = {
350 {.compatible
= "atmel,at97sc3204"},
351 {.compatible
= "tcg,tpm-tis-mmio"},
354 MODULE_DEVICE_TABLE(of
, tis_of_platform_match
);
357 static struct platform_driver tis_drv
= {
358 .probe
= tpm_tis_plat_probe
,
359 .remove
= tpm_tis_plat_remove
,
363 .of_match_table
= of_match_ptr(tis_of_platform_match
),
364 .acpi_match_table
= ACPI_PTR(tpm_acpi_tbl
),
368 static int tpm_tis_force_device(void)
370 struct platform_device
*pdev
;
371 static const struct resource x86_resources
[] = {
372 DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN
)
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
382 pdev
= platform_device_register_simple("tpm_tis", -1, x86_resources
,
383 ARRAY_SIZE(x86_resources
));
385 return PTR_ERR(pdev
);
391 static int __init
init_tis(void)
395 rc
= tpm_tis_force_device();
399 rc
= platform_driver_register(&tis_drv
);
404 if (IS_ENABLED(CONFIG_PNP
)) {
405 rc
= pnp_register_driver(&tis_pnp_driver
);
413 platform_driver_unregister(&tis_drv
);
416 platform_device_unregister(force_pdev
);
421 static void __exit
cleanup_tis(void)
423 pnp_unregister_driver(&tis_pnp_driver
);
424 platform_driver_unregister(&tis_drv
);
427 platform_device_unregister(force_pdev
);
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");