tpm_crb.c 8.7 KB

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