tpm_tis.c 11 KB

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