tpm_crb.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_ca_request {
  33. CRB_CA_REQ_GO_IDLE = BIT(0),
  34. CRB_CA_REQ_CMD_READY = BIT(1),
  35. };
  36. enum crb_ca_status {
  37. CRB_CA_STS_ERROR = BIT(0),
  38. CRB_CA_STS_TPM_IDLE = BIT(1),
  39. };
  40. enum crb_start {
  41. CRB_START_INVOKE = BIT(0),
  42. };
  43. enum crb_cancel {
  44. CRB_CANCEL_INVOKE = BIT(0),
  45. };
  46. struct crb_control_area {
  47. u32 req;
  48. u32 sts;
  49. u32 cancel;
  50. u32 start;
  51. u32 int_enable;
  52. u32 int_sts;
  53. u32 cmd_size;
  54. u32 cmd_pa_low;
  55. u32 cmd_pa_high;
  56. u32 rsp_size;
  57. u64 rsp_pa;
  58. } __packed;
  59. enum crb_status {
  60. CRB_STS_COMPLETE = BIT(0),
  61. };
  62. enum crb_flags {
  63. CRB_FL_ACPI_START = BIT(0),
  64. CRB_FL_CRB_START = BIT(1),
  65. };
  66. struct crb_priv {
  67. unsigned int flags;
  68. void __iomem *iobase;
  69. struct crb_control_area __iomem *cca;
  70. u8 __iomem *cmd;
  71. u8 __iomem *rsp;
  72. };
  73. static SIMPLE_DEV_PM_OPS(crb_pm, tpm_pm_suspend, tpm_pm_resume);
  74. static u8 crb_status(struct tpm_chip *chip)
  75. {
  76. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  77. u8 sts = 0;
  78. if ((ioread32(&priv->cca->start) & CRB_START_INVOKE) !=
  79. CRB_START_INVOKE)
  80. sts |= CRB_STS_COMPLETE;
  81. return sts;
  82. }
  83. static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  84. {
  85. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  86. unsigned int expected;
  87. /* sanity check */
  88. if (count < 6)
  89. return -EIO;
  90. if (ioread32(&priv->cca->sts) & CRB_CA_STS_ERROR)
  91. return -EIO;
  92. memcpy_fromio(buf, priv->rsp, 6);
  93. expected = be32_to_cpup((__be32 *) &buf[2]);
  94. if (expected > count)
  95. return -EIO;
  96. memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
  97. return expected;
  98. }
  99. static int crb_do_acpi_start(struct tpm_chip *chip)
  100. {
  101. union acpi_object *obj;
  102. int rc;
  103. obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
  104. CRB_ACPI_START_UUID,
  105. CRB_ACPI_START_REVISION_ID,
  106. CRB_ACPI_START_INDEX,
  107. NULL);
  108. if (!obj)
  109. return -ENXIO;
  110. rc = obj->integer.value == 0 ? 0 : -ENXIO;
  111. ACPI_FREE(obj);
  112. return rc;
  113. }
  114. static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
  115. {
  116. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  117. int rc = 0;
  118. if (len > ioread32(&priv->cca->cmd_size)) {
  119. dev_err(&chip->dev,
  120. "invalid command count value %x %zx\n",
  121. (unsigned int) len,
  122. (size_t) ioread32(&priv->cca->cmd_size));
  123. return -E2BIG;
  124. }
  125. memcpy_toio(priv->cmd, buf, len);
  126. /* Make sure that cmd is populated before issuing start. */
  127. wmb();
  128. if (priv->flags & CRB_FL_CRB_START)
  129. iowrite32(cpu_to_le32(CRB_START_INVOKE), &priv->cca->start);
  130. if (priv->flags & CRB_FL_ACPI_START)
  131. rc = crb_do_acpi_start(chip);
  132. return rc;
  133. }
  134. static void crb_cancel(struct tpm_chip *chip)
  135. {
  136. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  137. iowrite32(cpu_to_le32(CRB_CANCEL_INVOKE), &priv->cca->cancel);
  138. /* Make sure that cmd is populated before issuing cancel. */
  139. wmb();
  140. if ((priv->flags & CRB_FL_ACPI_START) && crb_do_acpi_start(chip))
  141. dev_err(&chip->dev, "ACPI Start failed\n");
  142. iowrite32(0, &priv->cca->cancel);
  143. }
  144. static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
  145. {
  146. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  147. u32 cancel = ioread32(&priv->cca->cancel);
  148. return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
  149. }
  150. static const struct tpm_class_ops tpm_crb = {
  151. .flags = TPM_OPS_AUTO_STARTUP,
  152. .status = crb_status,
  153. .recv = crb_recv,
  154. .send = crb_send,
  155. .cancel = crb_cancel,
  156. .req_canceled = crb_req_canceled,
  157. .req_complete_mask = CRB_STS_COMPLETE,
  158. .req_complete_val = CRB_STS_COMPLETE,
  159. };
  160. static int crb_init(struct acpi_device *device, struct crb_priv *priv)
  161. {
  162. struct tpm_chip *chip;
  163. chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
  164. if (IS_ERR(chip))
  165. return PTR_ERR(chip);
  166. dev_set_drvdata(&chip->dev, priv);
  167. chip->acpi_dev_handle = device->handle;
  168. chip->flags = TPM_CHIP_FLAG_TPM2;
  169. return tpm_chip_register(chip);
  170. }
  171. static int crb_check_resource(struct acpi_resource *ares, void *data)
  172. {
  173. struct resource *io_res = data;
  174. struct resource res;
  175. if (acpi_dev_resource_memory(ares, &res)) {
  176. *io_res = res;
  177. io_res->name = NULL;
  178. }
  179. return 1;
  180. }
  181. static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
  182. struct resource *io_res, u64 start, u32 size)
  183. {
  184. struct resource new_res = {
  185. .start = start,
  186. .end = start + size - 1,
  187. .flags = IORESOURCE_MEM,
  188. };
  189. /* Detect a 64 bit address on a 32 bit system */
  190. if (start != new_res.start)
  191. return (void __iomem *) ERR_PTR(-EINVAL);
  192. if (!resource_contains(io_res, &new_res))
  193. return devm_ioremap_resource(dev, &new_res);
  194. return priv->iobase + (new_res.start - io_res->start);
  195. }
  196. static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
  197. struct acpi_table_tpm2 *buf)
  198. {
  199. struct list_head resources;
  200. struct resource io_res;
  201. struct device *dev = &device->dev;
  202. u64 cmd_pa;
  203. u32 cmd_size;
  204. u64 rsp_pa;
  205. u32 rsp_size;
  206. int ret;
  207. INIT_LIST_HEAD(&resources);
  208. ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
  209. &io_res);
  210. if (ret < 0)
  211. return ret;
  212. acpi_dev_free_resource_list(&resources);
  213. if (resource_type(&io_res) != IORESOURCE_MEM) {
  214. dev_err(dev,
  215. FW_BUG "TPM2 ACPI table does not define a memory resource\n");
  216. return -EINVAL;
  217. }
  218. priv->iobase = devm_ioremap_resource(dev, &io_res);
  219. if (IS_ERR(priv->iobase))
  220. return PTR_ERR(priv->iobase);
  221. priv->cca = crb_map_res(dev, priv, &io_res, buf->control_address,
  222. sizeof(struct crb_control_area));
  223. if (IS_ERR(priv->cca))
  224. return PTR_ERR(priv->cca);
  225. cmd_pa = ((u64) ioread32(&priv->cca->cmd_pa_high) << 32) |
  226. (u64) ioread32(&priv->cca->cmd_pa_low);
  227. cmd_size = ioread32(&priv->cca->cmd_size);
  228. priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
  229. if (IS_ERR(priv->cmd))
  230. return PTR_ERR(priv->cmd);
  231. memcpy_fromio(&rsp_pa, &priv->cca->rsp_pa, 8);
  232. rsp_pa = le64_to_cpu(rsp_pa);
  233. rsp_size = ioread32(&priv->cca->rsp_size);
  234. if (cmd_pa != rsp_pa) {
  235. priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
  236. return PTR_ERR_OR_ZERO(priv->rsp);
  237. }
  238. /* According to the PTP specification, overlapping command and response
  239. * buffer sizes must be identical.
  240. */
  241. if (cmd_size != rsp_size) {
  242. dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
  243. return -EINVAL;
  244. }
  245. priv->rsp = priv->cmd;
  246. return 0;
  247. }
  248. static int crb_acpi_add(struct acpi_device *device)
  249. {
  250. struct acpi_table_tpm2 *buf;
  251. struct crb_priv *priv;
  252. struct device *dev = &device->dev;
  253. acpi_status status;
  254. u32 sm;
  255. int rc;
  256. status = acpi_get_table(ACPI_SIG_TPM2, 1,
  257. (struct acpi_table_header **) &buf);
  258. if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
  259. dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
  260. return -EINVAL;
  261. }
  262. /* Should the FIFO driver handle this? */
  263. sm = buf->start_method;
  264. if (sm == ACPI_TPM2_MEMORY_MAPPED)
  265. return -ENODEV;
  266. priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
  267. if (!priv)
  268. return -ENOMEM;
  269. /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
  270. * report only ACPI start but in practice seems to require both
  271. * ACPI start and CRB start.
  272. */
  273. if (sm == ACPI_TPM2_COMMAND_BUFFER || sm == ACPI_TPM2_MEMORY_MAPPED ||
  274. !strcmp(acpi_device_hid(device), "MSFT0101"))
  275. priv->flags |= CRB_FL_CRB_START;
  276. if (sm == ACPI_TPM2_START_METHOD ||
  277. sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)
  278. priv->flags |= CRB_FL_ACPI_START;
  279. rc = crb_map_io(device, priv, buf);
  280. if (rc)
  281. return rc;
  282. return crb_init(device, priv);
  283. }
  284. static int crb_acpi_remove(struct acpi_device *device)
  285. {
  286. struct device *dev = &device->dev;
  287. struct tpm_chip *chip = dev_get_drvdata(dev);
  288. tpm_chip_unregister(chip);
  289. return 0;
  290. }
  291. static struct acpi_device_id crb_device_ids[] = {
  292. {"MSFT0101", 0},
  293. {"", 0},
  294. };
  295. MODULE_DEVICE_TABLE(acpi, crb_device_ids);
  296. static struct acpi_driver crb_acpi_driver = {
  297. .name = "tpm_crb",
  298. .ids = crb_device_ids,
  299. .ops = {
  300. .add = crb_acpi_add,
  301. .remove = crb_acpi_remove,
  302. },
  303. .drv = {
  304. .pm = &crb_pm,
  305. },
  306. };
  307. module_acpi_driver(crb_acpi_driver);
  308. MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
  309. MODULE_DESCRIPTION("TPM2 Driver");
  310. MODULE_VERSION("0.1");
  311. MODULE_LICENSE("GPL");