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