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