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