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