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