tpm_crb.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2014 Intel Corporation
  3. *
  4. * Authors:
  5. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  6. *
  7. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  8. *
  9. * This device driver implements the TPM interface as defined in
  10. * the TCG CRB 2.0 TPM specification.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; version 2
  15. * of the License.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/highmem.h>
  19. #include <linux/rculist.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include "tpm.h"
  23. #define ACPI_SIG_TPM2 "TPM2"
  24. static const u8 CRB_ACPI_START_UUID[] = {
  25. /* 0000 */ 0xAB, 0x6C, 0xBF, 0x6B, 0x63, 0x54, 0x14, 0x47,
  26. /* 0008 */ 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4
  27. };
  28. enum crb_defaults {
  29. CRB_ACPI_START_REVISION_ID = 1,
  30. CRB_ACPI_START_INDEX = 1,
  31. };
  32. enum crb_start_method {
  33. CRB_SM_ACPI_START = 2,
  34. CRB_SM_CRB = 7,
  35. CRB_SM_CRB_WITH_ACPI_START = 8,
  36. };
  37. struct acpi_tpm2 {
  38. struct acpi_table_header hdr;
  39. u16 platform_class;
  40. u16 reserved;
  41. u64 control_area_pa;
  42. u32 start_method;
  43. } __packed;
  44. enum crb_ca_request {
  45. CRB_CA_REQ_GO_IDLE = BIT(0),
  46. CRB_CA_REQ_CMD_READY = BIT(1),
  47. };
  48. enum crb_ca_status {
  49. CRB_CA_STS_ERROR = BIT(0),
  50. CRB_CA_STS_TPM_IDLE = BIT(1),
  51. };
  52. enum crb_start {
  53. CRB_START_INVOKE = BIT(0),
  54. };
  55. enum crb_cancel {
  56. CRB_CANCEL_INVOKE = BIT(0),
  57. };
  58. struct crb_control_area {
  59. u32 req;
  60. u32 sts;
  61. u32 cancel;
  62. u32 start;
  63. u32 int_enable;
  64. u32 int_sts;
  65. u32 cmd_size;
  66. u64 cmd_pa;
  67. u32 rsp_size;
  68. u64 rsp_pa;
  69. } __packed;
  70. enum crb_status {
  71. CRB_STS_COMPLETE = BIT(0),
  72. };
  73. enum crb_flags {
  74. CRB_FL_ACPI_START = BIT(0),
  75. CRB_FL_CRB_START = BIT(1),
  76. };
  77. struct crb_priv {
  78. unsigned int flags;
  79. struct crb_control_area __iomem *cca;
  80. u8 __iomem *cmd;
  81. u8 __iomem *rsp;
  82. };
  83. static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
  84. static u8 crb_status(struct tpm_chip *chip)
  85. {
  86. struct crb_priv *priv = chip->vendor.priv;
  87. u8 sts = 0;
  88. if ((le32_to_cpu(ioread32(&priv->cca->start)) & CRB_START_INVOKE) !=
  89. CRB_START_INVOKE)
  90. sts |= CRB_STS_COMPLETE;
  91. return sts;
  92. }
  93. static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  94. {
  95. struct crb_priv *priv = chip->vendor.priv;
  96. unsigned int expected;
  97. /* sanity check */
  98. if (count < 6)
  99. return -EIO;
  100. if (le32_to_cpu(ioread32(&priv->cca->sts)) & CRB_CA_STS_ERROR)
  101. return -EIO;
  102. memcpy_fromio(buf, priv->rsp, 6);
  103. expected = be32_to_cpup((__be32 *) &buf[2]);
  104. if (expected > count)
  105. return -EIO;
  106. memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
  107. return expected;
  108. }
  109. static int crb_do_acpi_start(struct tpm_chip *chip)
  110. {
  111. union acpi_object *obj;
  112. int rc;
  113. obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
  114. CRB_ACPI_START_UUID,
  115. CRB_ACPI_START_REVISION_ID,
  116. CRB_ACPI_START_INDEX,
  117. NULL);
  118. if (!obj)
  119. return -ENXIO;
  120. rc = obj->integer.value == 0 ? 0 : -ENXIO;
  121. ACPI_FREE(obj);
  122. return rc;
  123. }
  124. static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
  125. {
  126. struct crb_priv *priv = chip->vendor.priv;
  127. int rc = 0;
  128. if (len > le32_to_cpu(ioread32(&priv->cca->cmd_size))) {
  129. dev_err(&chip->dev,
  130. "invalid command count value %x %zx\n",
  131. (unsigned int) len,
  132. (size_t) le32_to_cpu(ioread32(&priv->cca->cmd_size)));
  133. return -E2BIG;
  134. }
  135. memcpy_toio(priv->cmd, buf, len);
  136. /* Make sure that cmd is populated before issuing start. */
  137. wmb();
  138. if (priv->flags & CRB_FL_CRB_START)
  139. iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
  140. if (priv->flags & CRB_FL_ACPI_START)
  141. rc = crb_do_acpi_start(chip);
  142. return rc;
  143. }
  144. static void crb_cancel(struct tpm_chip *chip)
  145. {
  146. struct crb_priv *priv = chip->vendor.priv;
  147. iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
  148. /* Make sure that cmd is populated before issuing cancel. */
  149. wmb();
  150. if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
  151. dev_err(&chip->dev, "ACPI Start failed\n");
  152. iowrite32(0, &priv->cca->cancel);
  153. }
  154. static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
  155. {
  156. struct crb_priv *priv = chip->vendor.priv;
  157. u32 cancel = le32_to_cpu(ioread32(&priv->cca->cancel));
  158. return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
  159. }
  160. static const struct tpm_class_ops tpm_crb = {
  161. .status = crb_status,
  162. .recv = crb_recv,
  163. .send = crb_send,
  164. .cancel = crb_cancel,
  165. .req_canceled = crb_req_canceled,
  166. .req_complete_mask = CRB_STS_COMPLETE,
  167. .req_complete_val = CRB_STS_COMPLETE,
  168. };
  169. static int crb_acpi_add(struct acpi_device *device)
  170. {
  171. struct tpm_chip *chip;
  172. struct acpi_tpm2 *buf;
  173. struct crb_priv *priv;
  174. struct device *dev = &device->dev;
  175. acpi_status status;
  176. u32 sm;
  177. u64 pa;
  178. int rc;
  179. chip = tpmm_chip_alloc(dev, &tpm_crb);
  180. if (IS_ERR(chip))
  181. return PTR_ERR(chip);
  182. chip->flags = TPM_CHIP_FLAG_TPM2;
  183. status = acpi_get_table(ACPI_SIG_TPM2, 1,
  184. (struct acpi_table_header **) &buf);
  185. if (ACPI_FAILURE(status)) {
  186. dev_err(dev, "failed to get TPM2 ACPI table\n");
  187. return -ENODEV;
  188. }
  189. if (buf->hdr.length < sizeof(struct acpi_tpm2)) {
  190. dev_err(dev, "TPM2 ACPI table has wrong size");
  191. return -EINVAL;
  192. }
  193. priv = (struct crb_priv *) devm_kzalloc(dev, sizeof(struct crb_priv),
  194. GFP_KERNEL);
  195. if (!priv) {
  196. dev_err(dev, "failed to devm_kzalloc for private data\n");
  197. return -ENOMEM;
  198. }
  199. sm = le32_to_cpu(buf->start_method);
  200. /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
  201. * report only ACPI start but in practice seems to require both
  202. * ACPI start and CRB start.
  203. */
  204. if (sm == CRB_SM_CRB || sm == CRB_SM_CRB_WITH_ACPI_START ||
  205. !strcmp(acpi_device_hid(device), "MSFT0101"))
  206. priv->flags |= CRB_FL_CRB_START;
  207. if (sm == CRB_SM_ACPI_START || sm == CRB_SM_CRB_WITH_ACPI_START)
  208. priv->flags |= CRB_FL_ACPI_START;
  209. priv->cca = (struct crb_control_area __iomem *)
  210. devm_ioremap_nocache(dev, buf->control_area_pa, 0x1000);
  211. if (!priv->cca) {
  212. dev_err(dev, "ioremap of the control area failed\n");
  213. return -ENOMEM;
  214. }
  215. memcpy_fromio(&pa, &priv->cca->cmd_pa, 8);
  216. pa = le64_to_cpu(pa);
  217. priv->cmd = devm_ioremap_nocache(dev, pa,
  218. ioread32(&priv->cca->cmd_size));
  219. if (!priv->cmd) {
  220. dev_err(dev, "ioremap of the command buffer failed\n");
  221. return -ENOMEM;
  222. }
  223. memcpy_fromio(&pa, &priv->cca->rsp_pa, 8);
  224. pa = le64_to_cpu(pa);
  225. priv->rsp = devm_ioremap_nocache(dev, pa,
  226. ioread32(&priv->cca->rsp_size));
  227. if (!priv->rsp) {
  228. dev_err(dev, "ioremap of the response buffer failed\n");
  229. return -ENOMEM;
  230. }
  231. chip->vendor.priv = priv;
  232. /* Default timeouts and durations */
  233. chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  234. chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  235. chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  236. chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  237. chip->vendor.duration[TPM_SHORT] =
  238. msecs_to_jiffies(TPM2_DURATION_SHORT);
  239. chip->vendor.duration[TPM_MEDIUM] =
  240. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  241. chip->vendor.duration[TPM_LONG] =
  242. msecs_to_jiffies(TPM2_DURATION_LONG);
  243. chip->acpi_dev_handle = device->handle;
  244. rc = tpm2_do_selftest(chip);
  245. if (rc)
  246. return rc;
  247. return tpm_chip_register(chip);
  248. }
  249. static int crb_acpi_remove(struct acpi_device *device)
  250. {
  251. struct device *dev = &device->dev;
  252. struct tpm_chip *chip = dev_get_drvdata(dev);
  253. tpm_chip_unregister(chip);
  254. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  255. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  256. return 0;
  257. }
  258. static struct acpi_device_id crb_device_ids[] = {
  259. {"MSFT0101", 0},
  260. {"", 0},
  261. };
  262. MODULE_DEVICE_TABLE(acpi, crb_device_ids);
  263. static struct acpi_driver crb_acpi_driver = {
  264. .name = "tpm_crb",
  265. .ids = crb_device_ids,
  266. .ops = {
  267. .add = crb_acpi_add,
  268. .remove = crb_acpi_remove,
  269. },
  270. .drv = {
  271. .pm = &crb_pm,
  272. },
  273. };
  274. module_acpi_driver(crb_acpi_driver);
  275. MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
  276. MODULE_DESCRIPTION("TPM2 Driver");
  277. MODULE_VERSION("0.1");
  278. MODULE_LICENSE("GPL");