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