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