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