xref: /openbmc/linux/drivers/char/tpm/tpm_tis.c (revision fb960bd2)
1 /*
2  * Copyright (C) 2005, 2006 IBM Corporation
3  * Copyright (C) 2014, 2015 Intel Corporation
4  *
5  * Authors:
6  * Leendert van Doorn <leendert@watson.ibm.com>
7  * Kylene Hall <kjhall@us.ibm.com>
8  *
9  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10  *
11  * Device driver for TCG/TCPA TPM (trusted platform module).
12  * Specifications at www.trustedcomputinggroup.org
13  *
14  * This device driver implements the TPM interface as defined in
15  * the TCG TPM Interface Spec version 1.2, revision 1.0.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation, version 2 of the
20  * License.
21  */
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/pnp.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/acpi.h>
30 #include <linux/freezer.h>
31 #include <linux/of.h>
32 #include <linux/of_device.h>
33 #include <linux/kernel.h>
34 #include "tpm.h"
35 #include "tpm_tis_core.h"
36 
37 struct tpm_info {
38 	struct resource res;
39 	/* irq > 0 means: use irq $irq;
40 	 * irq = 0 means: autoprobe for an irq;
41 	 * irq = -1 means: no irq support
42 	 */
43 	int irq;
44 };
45 
46 struct tpm_tis_tcg_phy {
47 	struct tpm_tis_data priv;
48 	void __iomem *iobase;
49 };
50 
51 static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
52 {
53 	return container_of(data, struct tpm_tis_tcg_phy, priv);
54 }
55 
56 static bool interrupts = true;
57 module_param(interrupts, bool, 0444);
58 MODULE_PARM_DESC(interrupts, "Enable interrupts");
59 
60 static bool itpm;
61 module_param(itpm, bool, 0444);
62 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
63 
64 static bool force;
65 #ifdef CONFIG_X86
66 module_param(force, bool, 0444);
67 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
68 #endif
69 
70 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
71 static int has_hid(struct acpi_device *dev, const char *hid)
72 {
73 	struct acpi_hardware_id *id;
74 
75 	list_for_each_entry(id, &dev->pnp.ids, list)
76 		if (!strcmp(hid, id->id))
77 			return 1;
78 
79 	return 0;
80 }
81 
82 static inline int is_itpm(struct acpi_device *dev)
83 {
84 	if (!dev)
85 		return 0;
86 	return has_hid(dev, "INTC0102");
87 }
88 #else
89 static inline int is_itpm(struct acpi_device *dev)
90 {
91 	return 0;
92 }
93 #endif
94 
95 #if defined(CONFIG_ACPI)
96 #define DEVICE_IS_TPM2 1
97 
98 static const struct acpi_device_id tpm_acpi_tbl[] = {
99 	{"MSFT0101", DEVICE_IS_TPM2},
100 	{},
101 };
102 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
103 
104 static int check_acpi_tpm2(struct device *dev)
105 {
106 	const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
107 	struct acpi_table_tpm2 *tbl;
108 	acpi_status st;
109 
110 	if (!aid || aid->driver_data != DEVICE_IS_TPM2)
111 		return 0;
112 
113 	/* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
114 	 * table is mandatory
115 	 */
116 	st =
117 	    acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
118 	if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
119 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
120 		return -EINVAL;
121 	}
122 
123 	/* The tpm2_crb driver handles this device */
124 	if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
125 		return -ENODEV;
126 
127 	return 0;
128 }
129 #else
130 static int check_acpi_tpm2(struct device *dev)
131 {
132 	return 0;
133 }
134 #endif
135 
136 #ifdef CONFIG_X86
137 #define INTEL_LEGACY_BLK_BASE_ADDR      0xFED08000
138 #define ILB_REMAP_SIZE			0x100
139 #define LPC_CNTRL_REG_OFFSET            0x84
140 #define LPC_CLKRUN_EN                   (1 << 2)
141 
142 static void __iomem *ilb_base_addr;
143 
144 static inline bool is_bsw(void)
145 {
146 	return ((boot_cpu_data.x86_model == INTEL_FAM6_ATOM_AIRMONT) ? 1 : 0);
147 }
148 
149 /**
150  * tpm_platform_begin_xfer() - clear LPC CLKRUN_EN i.e. clocks will be running
151  */
152 static void tpm_platform_begin_xfer(void)
153 {
154 	u32 clkrun_val;
155 
156 	if (!is_bsw())
157 		return;
158 
159 	clkrun_val = ioread32(ilb_base_addr + LPC_CNTRL_REG_OFFSET);
160 
161 	/* Disable LPC CLKRUN# */
162 	clkrun_val &= ~LPC_CLKRUN_EN;
163 	iowrite32(clkrun_val, ilb_base_addr + LPC_CNTRL_REG_OFFSET);
164 
165 	/*
166 	 * Write any random value on port 0x80 which is on LPC, to make
167 	 * sure LPC clock is running before sending any TPM command.
168 	 */
169 	outb(0xCC, 0x80);
170 
171 }
172 
173 /**
174  * tpm_platform_end_xfer() - set LPC CLKRUN_EN i.e. clocks can be turned off
175  */
176 static void tpm_platform_end_xfer(void)
177 {
178 	u32 clkrun_val;
179 
180 	if (!is_bsw())
181 		return;
182 
183 	clkrun_val = ioread32(ilb_base_addr + LPC_CNTRL_REG_OFFSET);
184 
185 	/* Enable LPC CLKRUN# */
186 	clkrun_val |= LPC_CLKRUN_EN;
187 	iowrite32(clkrun_val, ilb_base_addr + LPC_CNTRL_REG_OFFSET);
188 
189 	/*
190 	 * Write any random value on port 0x80 which is on LPC, to make
191 	 * sure LPC clock is running before sending any TPM command.
192 	 */
193 	outb(0xCC, 0x80);
194 
195 }
196 #else
197 static inline bool is_bsw(void)
198 {
199 	return false;
200 }
201 
202 static void tpm_platform_begin_xfer(void)
203 {
204 }
205 
206 static void tpm_platform_end_xfer(void)
207 {
208 }
209 #endif
210 
211 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
212 			      u8 *result)
213 {
214 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
215 
216 	tpm_platform_begin_xfer();
217 
218 	while (len--)
219 		*result++ = ioread8(phy->iobase + addr);
220 
221 	tpm_platform_end_xfer();
222 
223 	return 0;
224 }
225 
226 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
227 			       const u8 *value)
228 {
229 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
230 
231 	tpm_platform_begin_xfer();
232 
233 	while (len--)
234 		iowrite8(*value++, phy->iobase + addr);
235 
236 	tpm_platform_end_xfer();
237 
238 	return 0;
239 }
240 
241 static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
242 {
243 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
244 
245 	tpm_platform_begin_xfer();
246 
247 	*result = ioread16(phy->iobase + addr);
248 
249 	tpm_platform_end_xfer();
250 
251 	return 0;
252 }
253 
254 static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
255 {
256 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
257 
258 	tpm_platform_begin_xfer();
259 
260 	*result = ioread32(phy->iobase + addr);
261 
262 	tpm_platform_end_xfer();
263 
264 	return 0;
265 }
266 
267 static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
268 {
269 	struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
270 
271 	tpm_platform_begin_xfer();
272 
273 	iowrite32(value, phy->iobase + addr);
274 
275 	tpm_platform_end_xfer();
276 
277 	return 0;
278 }
279 
280 static const struct tpm_tis_phy_ops tpm_tcg = {
281 	.read_bytes = tpm_tcg_read_bytes,
282 	.write_bytes = tpm_tcg_write_bytes,
283 	.read16 = tpm_tcg_read16,
284 	.read32 = tpm_tcg_read32,
285 	.write32 = tpm_tcg_write32,
286 };
287 
288 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
289 {
290 	struct tpm_tis_tcg_phy *phy;
291 	int irq = -1;
292 	int rc;
293 
294 	rc = check_acpi_tpm2(dev);
295 	if (rc)
296 		return rc;
297 
298 	phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
299 	if (phy == NULL)
300 		return -ENOMEM;
301 
302 	phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
303 	if (IS_ERR(phy->iobase))
304 		return PTR_ERR(phy->iobase);
305 
306 	if (interrupts)
307 		irq = tpm_info->irq;
308 
309 	if (itpm || is_itpm(ACPI_COMPANION(dev)))
310 		phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
311 
312 	return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
313 				 ACPI_HANDLE(dev));
314 }
315 
316 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
317 
318 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
319 			    const struct pnp_device_id *pnp_id)
320 {
321 	struct tpm_info tpm_info = {};
322 	struct resource *res;
323 
324 	res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
325 	if (!res)
326 		return -ENODEV;
327 	tpm_info.res = *res;
328 
329 	if (pnp_irq_valid(pnp_dev, 0))
330 		tpm_info.irq = pnp_irq(pnp_dev, 0);
331 	else
332 		tpm_info.irq = -1;
333 
334 	return tpm_tis_init(&pnp_dev->dev, &tpm_info);
335 }
336 
337 static struct pnp_device_id tpm_pnp_tbl[] = {
338 	{"PNP0C31", 0},		/* TPM */
339 	{"ATM1200", 0},		/* Atmel */
340 	{"IFX0102", 0},		/* Infineon */
341 	{"BCM0101", 0},		/* Broadcom */
342 	{"BCM0102", 0},		/* Broadcom */
343 	{"NSC1200", 0},		/* National */
344 	{"ICO0102", 0},		/* Intel */
345 	/* Add new here */
346 	{"", 0},		/* User Specified */
347 	{"", 0}			/* Terminator */
348 };
349 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
350 
351 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
352 {
353 	struct tpm_chip *chip = pnp_get_drvdata(dev);
354 
355 	tpm_chip_unregister(chip);
356 	tpm_tis_remove(chip);
357 }
358 
359 static struct pnp_driver tis_pnp_driver = {
360 	.name = "tpm_tis",
361 	.id_table = tpm_pnp_tbl,
362 	.probe = tpm_tis_pnp_init,
363 	.remove = tpm_tis_pnp_remove,
364 	.driver	= {
365 		.pm = &tpm_tis_pm,
366 	},
367 };
368 
369 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
370 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
371 		    sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
372 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
373 
374 static struct platform_device *force_pdev;
375 
376 static int tpm_tis_plat_probe(struct platform_device *pdev)
377 {
378 	struct tpm_info tpm_info = {};
379 	struct resource *res;
380 
381 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
382 	if (res == NULL) {
383 		dev_err(&pdev->dev, "no memory resource defined\n");
384 		return -ENODEV;
385 	}
386 	tpm_info.res = *res;
387 
388 	tpm_info.irq = platform_get_irq(pdev, 0);
389 	if (tpm_info.irq <= 0) {
390 		if (pdev != force_pdev)
391 			tpm_info.irq = -1;
392 		else
393 			/* When forcing auto probe the IRQ */
394 			tpm_info.irq = 0;
395 	}
396 
397 	return tpm_tis_init(&pdev->dev, &tpm_info);
398 }
399 
400 static int tpm_tis_plat_remove(struct platform_device *pdev)
401 {
402 	struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
403 
404 	tpm_chip_unregister(chip);
405 	tpm_tis_remove(chip);
406 
407 	return 0;
408 }
409 
410 #ifdef CONFIG_OF
411 static const struct of_device_id tis_of_platform_match[] = {
412 	{.compatible = "tcg,tpm-tis-mmio"},
413 	{},
414 };
415 MODULE_DEVICE_TABLE(of, tis_of_platform_match);
416 #endif
417 
418 static struct platform_driver tis_drv = {
419 	.probe = tpm_tis_plat_probe,
420 	.remove = tpm_tis_plat_remove,
421 	.driver = {
422 		.name		= "tpm_tis",
423 		.pm		= &tpm_tis_pm,
424 		.of_match_table = of_match_ptr(tis_of_platform_match),
425 		.acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
426 	},
427 };
428 
429 static int tpm_tis_force_device(void)
430 {
431 	struct platform_device *pdev;
432 	static const struct resource x86_resources[] = {
433 		{
434 			.start = 0xFED40000,
435 			.end = 0xFED40000 + TIS_MEM_LEN - 1,
436 			.flags = IORESOURCE_MEM,
437 		},
438 	};
439 
440 	if (!force)
441 		return 0;
442 
443 	/* The driver core will match the name tpm_tis of the device to
444 	 * the tpm_tis platform driver and complete the setup via
445 	 * tpm_tis_plat_probe
446 	 */
447 	pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
448 					       ARRAY_SIZE(x86_resources));
449 	if (IS_ERR(pdev))
450 		return PTR_ERR(pdev);
451 	force_pdev = pdev;
452 
453 	return 0;
454 }
455 
456 static int __init init_tis(void)
457 {
458 	int rc;
459 
460 	rc = tpm_tis_force_device();
461 	if (rc)
462 		goto err_force;
463 
464 #ifdef CONFIG_X86
465 	if (is_bsw())
466 		ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
467 					ILB_REMAP_SIZE);
468 #endif
469 	rc = platform_driver_register(&tis_drv);
470 	if (rc)
471 		goto err_platform;
472 
473 
474 	if (IS_ENABLED(CONFIG_PNP)) {
475 		rc = pnp_register_driver(&tis_pnp_driver);
476 		if (rc)
477 			goto err_pnp;
478 	}
479 
480 	return 0;
481 
482 err_pnp:
483 	platform_driver_unregister(&tis_drv);
484 err_platform:
485 	if (force_pdev)
486 		platform_device_unregister(force_pdev);
487 #ifdef CONFIG_X86
488 	if (is_bsw())
489 		iounmap(ilb_base_addr);
490 #endif
491 err_force:
492 	return rc;
493 }
494 
495 static void __exit cleanup_tis(void)
496 {
497 	pnp_unregister_driver(&tis_pnp_driver);
498 	platform_driver_unregister(&tis_drv);
499 
500 #ifdef CONFIG_X86
501 	if (is_bsw())
502 		iounmap(ilb_base_addr);
503 #endif
504 	if (force_pdev)
505 		platform_device_unregister(force_pdev);
506 }
507 
508 module_init(init_tis);
509 module_exit(cleanup_tis);
510 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
511 MODULE_DESCRIPTION("TPM Driver");
512 MODULE_VERSION("2.0");
513 MODULE_LICENSE("GPL");
514