tpm/tpm_crb: fix priv->cmd_size initialisation
[linux-2.6-block.git] / drivers / char / tpm / tpm_tis.c
CommitLineData
27084efe
LD
1/*
2 * Copyright (C) 2005, 2006 IBM Corporation
399235dc 3 * Copyright (C) 2014, 2015 Intel Corporation
27084efe
LD
4 *
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
8e81cc13
KY
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 *
27084efe
LD
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 */
57135568
KJH
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
27084efe 25#include <linux/pnp.h>
5a0e3ad6 26#include <linux/slab.h>
27084efe
LD
27#include <linux/interrupt.h>
28#include <linux/wait.h>
3f0d3d01 29#include <linux/acpi.h>
20b87bbf 30#include <linux/freezer.h>
420d4398
JG
31#include <linux/of.h>
32#include <linux/of_device.h>
27084efe 33#include "tpm.h"
57dacc2b 34#include "tpm_tis_core.h"
27084efe 35
399235dc 36struct tpm_info {
51dd43df 37 struct resource res;
ef7b81dc
JG
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;
399235dc
JS
43};
44
57dacc2b
CR
45struct tpm_tis_tcg_phy {
46 struct tpm_tis_data priv;
4eea703c 47 void __iomem *iobase;
448e9c55
SD
48};
49
57dacc2b
CR
50static 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
41a5e1cf
CR
55static bool interrupts = true;
56module_param(interrupts, bool, 0444);
57MODULE_PARM_DESC(interrupts, "Enable interrupts");
58
59static bool itpm;
60module_param(itpm, bool, 0444);
61MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
62
63static bool force;
64#ifdef CONFIG_X86
65module_param(force, bool, 0444);
66MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
67#endif
68
1560ffe6 69#if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
399235dc 70static int has_hid(struct acpi_device *dev, const char *hid)
3f0d3d01 71{
3f0d3d01
MG
72 struct acpi_hardware_id *id;
73
399235dc
JS
74 list_for_each_entry(id, &dev->pnp.ids, list)
75 if (!strcmp(hid, id->id))
3f0d3d01 76 return 1;
3f0d3d01
MG
77
78 return 0;
79}
399235dc
JS
80
81static inline int is_itpm(struct acpi_device *dev)
82{
4cb586a1
JG
83 if (!dev)
84 return 0;
399235dc
JS
85 return has_hid(dev, "INTC0102");
86}
1560ffe6 87#else
399235dc 88static inline int is_itpm(struct acpi_device *dev)
1560ffe6
RD
89{
90 return 0;
91}
3f0d3d01
MG
92#endif
93
4cb586a1
JG
94#if defined(CONFIG_ACPI)
95#define DEVICE_IS_TPM2 1
96
97static const struct acpi_device_id tpm_acpi_tbl[] = {
98 {"MSFT0101", DEVICE_IS_TPM2},
99 {},
100};
101MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
102
103static 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
129static int check_acpi_tpm2(struct device *dev)
130{
131 return 0;
132}
133#endif
134
1107d065
CR
135static 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 return 0;
143}
144
145static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
146 u8 *value)
147{
148 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
149
150 while (len--)
151 iowrite8(*value++, phy->iobase + addr);
152 return 0;
153}
154
155static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
156{
157 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
158
159 *result = ioread16(phy->iobase + addr);
160 return 0;
161}
162
163static 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 return 0;
169}
170
171static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
172{
173 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
174
175 iowrite32(value, phy->iobase + addr);
176 return 0;
177}
178
179static const struct tpm_tis_phy_ops tpm_tcg = {
180 .read_bytes = tpm_tcg_read_bytes,
181 .write_bytes = tpm_tcg_write_bytes,
182 .read16 = tpm_tcg_read16,
183 .read32 = tpm_tcg_read32,
184 .write32 = tpm_tcg_write32,
185};
186
4cb586a1 187static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
27084efe 188{
57dacc2b 189 struct tpm_tis_tcg_phy *phy;
41a5e1cf 190 int irq = -1;
4cb586a1
JG
191 int rc;
192
193 rc = check_acpi_tpm2(dev);
194 if (rc)
195 return rc;
27084efe 196
57dacc2b
CR
197 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
198 if (phy == NULL)
448e9c55 199 return -ENOMEM;
afb5abc2 200
57dacc2b
CR
201 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
202 if (IS_ERR(phy->iobase))
203 return PTR_ERR(phy->iobase);
27084efe 204
41a5e1cf
CR
205 if (interrupts)
206 irq = tpm_info->irq;
9519de3f 207
4cb586a1 208 if (itpm || is_itpm(ACPI_COMPANION(dev)))
1d70fe9d 209 phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
25112048 210
41a5e1cf 211 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
4cb586a1 212 ACPI_HANDLE(dev));
27084efe 213}
96854310 214
a2fa3fb0
SK
215static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
216
afc6d369 217static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
ef7b81dc 218 const struct pnp_device_id *pnp_id)
9e323d3e 219{
ef7b81dc 220 struct tpm_info tpm_info = {};
51dd43df 221 struct resource *res;
7917ff9a 222
51dd43df
JG
223 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
224 if (!res)
225 return -ENODEV;
226 tpm_info.res = *res;
9e323d3e 227
7917ff9a 228 if (pnp_irq_valid(pnp_dev, 0))
399235dc 229 tpm_info.irq = pnp_irq(pnp_dev, 0);
7917ff9a 230 else
ef7b81dc 231 tpm_info.irq = -1;
7917ff9a 232
4cb586a1 233 return tpm_tis_init(&pnp_dev->dev, &tpm_info);
9e323d3e
KJH
234}
235
0bbed20e 236static struct pnp_device_id tpm_pnp_tbl[] = {
27084efe 237 {"PNP0C31", 0}, /* TPM */
93e1b7d4
KJH
238 {"ATM1200", 0}, /* Atmel */
239 {"IFX0102", 0}, /* Infineon */
240 {"BCM0101", 0}, /* Broadcom */
061991ec 241 {"BCM0102", 0}, /* Broadcom */
93e1b7d4 242 {"NSC1200", 0}, /* National */
fb0e7e11 243 {"ICO0102", 0}, /* Intel */
93e1b7d4
KJH
244 /* Add new here */
245 {"", 0}, /* User Specified */
246 {"", 0} /* Terminator */
27084efe 247};
31bde71c 248MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
27084efe 249
39af33fc 250static void tpm_tis_pnp_remove(struct pnp_dev *dev)
253115b7
RA
251{
252 struct tpm_chip *chip = pnp_get_drvdata(dev);
399235dc 253
afb5abc2
JS
254 tpm_chip_unregister(chip);
255 tpm_tis_remove(chip);
253115b7
RA
256}
257
27084efe
LD
258static struct pnp_driver tis_pnp_driver = {
259 .name = "tpm_tis",
260 .id_table = tpm_pnp_tbl,
261 .probe = tpm_tis_pnp_init,
253115b7 262 .remove = tpm_tis_pnp_remove,
a2fa3fb0
SK
263 .driver = {
264 .pm = &tpm_tis_pm,
265 },
27084efe
LD
266};
267
93e1b7d4
KJH
268#define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
269module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
270 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
271MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
7a192ec3 272
00194826
JG
273static struct platform_device *force_pdev;
274
275static int tpm_tis_plat_probe(struct platform_device *pdev)
276{
277 struct tpm_info tpm_info = {};
278 struct resource *res;
279
280 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281 if (res == NULL) {
282 dev_err(&pdev->dev, "no memory resource defined\n");
283 return -ENODEV;
284 }
285 tpm_info.res = *res;
286
fc0e1322
JG
287 tpm_info.irq = platform_get_irq(pdev, 0);
288 if (tpm_info.irq <= 0) {
d27f81f0 289 if (pdev != force_pdev)
00194826
JG
290 tpm_info.irq = -1;
291 else
292 /* When forcing auto probe the IRQ */
293 tpm_info.irq = 0;
294 }
295
4cb586a1 296 return tpm_tis_init(&pdev->dev, &tpm_info);
00194826
JG
297}
298
299static int tpm_tis_plat_remove(struct platform_device *pdev)
300{
301 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
302
303 tpm_chip_unregister(chip);
304 tpm_tis_remove(chip);
305
306 return 0;
307}
308
420d4398
JG
309#ifdef CONFIG_OF
310static const struct of_device_id tis_of_platform_match[] = {
311 {.compatible = "tcg,tpm-tis-mmio"},
312 {},
313};
314MODULE_DEVICE_TABLE(of, tis_of_platform_match);
315#endif
316
7a192ec3 317static struct platform_driver tis_drv = {
00194826
JG
318 .probe = tpm_tis_plat_probe,
319 .remove = tpm_tis_plat_remove,
7a192ec3 320 .driver = {
afb5abc2 321 .name = "tpm_tis",
b633f050 322 .pm = &tpm_tis_pm,
420d4398 323 .of_match_table = of_match_ptr(tis_of_platform_match),
4cb586a1 324 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
7a192ec3 325 },
9e323d3e
KJH
326};
327
00194826
JG
328static int tpm_tis_force_device(void)
329{
330 struct platform_device *pdev;
331 static const struct resource x86_resources[] = {
332 {
333 .start = 0xFED40000,
334 .end = 0xFED40000 + TIS_MEM_LEN - 1,
335 .flags = IORESOURCE_MEM,
336 },
337 };
338
339 if (!force)
340 return 0;
341
342 /* The driver core will match the name tpm_tis of the device to
343 * the tpm_tis platform driver and complete the setup via
344 * tpm_tis_plat_probe
345 */
346 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
347 ARRAY_SIZE(x86_resources));
348 if (IS_ERR(pdev))
349 return PTR_ERR(pdev);
350 force_pdev = pdev;
351
352 return 0;
353}
354
27084efe
LD
355static int __init init_tis(void)
356{
9e323d3e 357 int rc;
00194826
JG
358
359 rc = tpm_tis_force_device();
360 if (rc)
361 goto err_force;
362
363 rc = platform_driver_register(&tis_drv);
364 if (rc)
365 goto err_platform;
366
9e323d3e 367
00194826
JG
368 if (IS_ENABLED(CONFIG_PNP)) {
369 rc = pnp_register_driver(&tis_pnp_driver);
370 if (rc)
371 goto err_pnp;
9e323d3e 372 }
00194826 373
4fba3c3b 374 return 0;
00194826
JG
375
376err_pnp:
5939eaf4 377 platform_driver_unregister(&tis_drv);
00194826
JG
378err_platform:
379 if (force_pdev)
380 platform_device_unregister(force_pdev);
381err_force:
7f2ab000 382 return rc;
27084efe
LD
383}
384
385static void __exit cleanup_tis(void)
386{
00194826 387 pnp_unregister_driver(&tis_pnp_driver);
7f2ab000 388 platform_driver_unregister(&tis_drv);
00194826
JG
389
390 if (force_pdev)
391 platform_device_unregister(force_pdev);
27084efe
LD
392}
393
394module_init(init_tis);
395module_exit(cleanup_tis);
396MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
397MODULE_DESCRIPTION("TPM Driver");
398MODULE_VERSION("2.0");
399MODULE_LICENSE("GPL");