tpm_crb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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/pm_runtime.h>
  22. #ifdef CONFIG_ARM64
  23. #include <linux/arm-smccc.h>
  24. #endif
  25. #include "tpm.h"
  26. #define ACPI_SIG_TPM2 "TPM2"
  27. static const guid_t crb_acpi_start_guid =
  28. GUID_INIT(0x6BBF6CAB, 0x5463, 0x4714,
  29. 0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4);
  30. enum crb_defaults {
  31. CRB_ACPI_START_REVISION_ID = 1,
  32. CRB_ACPI_START_INDEX = 1,
  33. };
  34. enum crb_loc_ctrl {
  35. CRB_LOC_CTRL_REQUEST_ACCESS = BIT(0),
  36. CRB_LOC_CTRL_RELINQUISH = BIT(1),
  37. };
  38. enum crb_loc_state {
  39. CRB_LOC_STATE_LOC_ASSIGNED = BIT(1),
  40. CRB_LOC_STATE_TPM_REG_VALID_STS = BIT(7),
  41. };
  42. enum crb_ctrl_req {
  43. CRB_CTRL_REQ_CMD_READY = BIT(0),
  44. CRB_CTRL_REQ_GO_IDLE = BIT(1),
  45. };
  46. enum crb_ctrl_sts {
  47. CRB_CTRL_STS_ERROR = BIT(0),
  48. CRB_CTRL_STS_TPM_IDLE = BIT(1),
  49. };
  50. enum crb_start {
  51. CRB_START_INVOKE = BIT(0),
  52. };
  53. enum crb_cancel {
  54. CRB_CANCEL_INVOKE = BIT(0),
  55. };
  56. struct crb_regs_head {
  57. u32 loc_state;
  58. u32 reserved1;
  59. u32 loc_ctrl;
  60. u32 loc_sts;
  61. u8 reserved2[32];
  62. u64 intf_id;
  63. u64 ctrl_ext;
  64. } __packed;
  65. struct crb_regs_tail {
  66. u32 ctrl_req;
  67. u32 ctrl_sts;
  68. u32 ctrl_cancel;
  69. u32 ctrl_start;
  70. u32 ctrl_int_enable;
  71. u32 ctrl_int_sts;
  72. u32 ctrl_cmd_size;
  73. u32 ctrl_cmd_pa_low;
  74. u32 ctrl_cmd_pa_high;
  75. u32 ctrl_rsp_size;
  76. u64 ctrl_rsp_pa;
  77. } __packed;
  78. enum crb_status {
  79. CRB_DRV_STS_COMPLETE = BIT(0),
  80. };
  81. struct crb_priv {
  82. u32 sm;
  83. const char *hid;
  84. void __iomem *iobase;
  85. struct crb_regs_head __iomem *regs_h;
  86. struct crb_regs_tail __iomem *regs_t;
  87. u8 __iomem *cmd;
  88. u8 __iomem *rsp;
  89. u32 cmd_size;
  90. u32 smc_func_id;
  91. };
  92. struct tpm2_crb_smc {
  93. u32 interrupt;
  94. u8 interrupt_flags;
  95. u8 op_flags;
  96. u16 reserved2;
  97. u32 smc_func_id;
  98. };
  99. static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
  100. unsigned long timeout)
  101. {
  102. ktime_t start;
  103. ktime_t stop;
  104. start = ktime_get();
  105. stop = ktime_add(start, ms_to_ktime(timeout));
  106. do {
  107. if ((ioread32(reg) & mask) == value)
  108. return true;
  109. usleep_range(50, 100);
  110. } while (ktime_before(ktime_get(), stop));
  111. return ((ioread32(reg) & mask) == value);
  112. }
  113. /**
  114. * __crb_go_idle - request tpm crb device to go the idle state
  115. *
  116. * @dev: crb device
  117. * @priv: crb private data
  118. *
  119. * Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
  120. * The device should respond within TIMEOUT_C by clearing the bit.
  121. * Anyhow, we do not wait here as a consequent CMD_READY request
  122. * will be handled correctly even if idle was not completed.
  123. *
  124. * The function does nothing for devices with ACPI-start method
  125. * or SMC-start method.
  126. *
  127. * Return: 0 always
  128. */
  129. static int __crb_go_idle(struct device *dev, struct crb_priv *priv)
  130. {
  131. if ((priv->sm == ACPI_TPM2_START_METHOD) ||
  132. (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
  133. (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC))
  134. return 0;
  135. iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
  136. if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
  137. CRB_CTRL_REQ_GO_IDLE/* mask */,
  138. 0, /* value */
  139. TPM2_TIMEOUT_C)) {
  140. dev_warn(dev, "goIdle timed out\n");
  141. return -ETIME;
  142. }
  143. return 0;
  144. }
  145. static int crb_go_idle(struct tpm_chip *chip)
  146. {
  147. struct device *dev = &chip->dev;
  148. struct crb_priv *priv = dev_get_drvdata(dev);
  149. return __crb_go_idle(dev, priv);
  150. }
  151. /**
  152. * __crb_cmd_ready - request tpm crb device to enter ready state
  153. *
  154. * @dev: crb device
  155. * @priv: crb private data
  156. *
  157. * Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
  158. * and poll till the device acknowledge it by clearing the bit.
  159. * The device should respond within TIMEOUT_C.
  160. *
  161. * The function does nothing for devices with ACPI-start method
  162. * or SMC-start method.
  163. *
  164. * Return: 0 on success -ETIME on timeout;
  165. */
  166. static int __crb_cmd_ready(struct device *dev, struct crb_priv *priv)
  167. {
  168. if ((priv->sm == ACPI_TPM2_START_METHOD) ||
  169. (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
  170. (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC))
  171. return 0;
  172. iowrite32(CRB_CTRL_REQ_CMD_READY, &priv->regs_t->ctrl_req);
  173. if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
  174. CRB_CTRL_REQ_CMD_READY /* mask */,
  175. 0, /* value */
  176. TPM2_TIMEOUT_C)) {
  177. dev_warn(dev, "cmdReady timed out\n");
  178. return -ETIME;
  179. }
  180. return 0;
  181. }
  182. static int crb_cmd_ready(struct tpm_chip *chip)
  183. {
  184. struct device *dev = &chip->dev;
  185. struct crb_priv *priv = dev_get_drvdata(dev);
  186. return __crb_cmd_ready(dev, priv);
  187. }
  188. static int __crb_request_locality(struct device *dev,
  189. struct crb_priv *priv, int loc)
  190. {
  191. u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
  192. CRB_LOC_STATE_TPM_REG_VALID_STS;
  193. if (!priv->regs_h)
  194. return 0;
  195. iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h->loc_ctrl);
  196. if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
  197. TPM2_TIMEOUT_C)) {
  198. dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
  199. return -ETIME;
  200. }
  201. return 0;
  202. }
  203. static int crb_request_locality(struct tpm_chip *chip, int loc)
  204. {
  205. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  206. return __crb_request_locality(&chip->dev, priv, loc);
  207. }
  208. static int __crb_relinquish_locality(struct device *dev,
  209. struct crb_priv *priv, int loc)
  210. {
  211. u32 mask = CRB_LOC_STATE_LOC_ASSIGNED |
  212. CRB_LOC_STATE_TPM_REG_VALID_STS;
  213. u32 value = CRB_LOC_STATE_TPM_REG_VALID_STS;
  214. if (!priv->regs_h)
  215. return 0;
  216. iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);
  217. if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, mask, value,
  218. TPM2_TIMEOUT_C)) {
  219. dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
  220. return -ETIME;
  221. }
  222. return 0;
  223. }
  224. static int crb_relinquish_locality(struct tpm_chip *chip, int loc)
  225. {
  226. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  227. return __crb_relinquish_locality(&chip->dev, priv, loc);
  228. }
  229. static u8 crb_status(struct tpm_chip *chip)
  230. {
  231. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  232. u8 sts = 0;
  233. if ((ioread32(&priv->regs_t->ctrl_start) & CRB_START_INVOKE) !=
  234. CRB_START_INVOKE)
  235. sts |= CRB_DRV_STS_COMPLETE;
  236. return sts;
  237. }
  238. static int crb_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  239. {
  240. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  241. unsigned int expected;
  242. /* A sanity check that the upper layer wants to get at least the header
  243. * as that is the minimum size for any TPM response.
  244. */
  245. if (count < TPM_HEADER_SIZE)
  246. return -EIO;
  247. /* If this bit is set, according to the spec, the TPM is in
  248. * unrecoverable condition.
  249. */
  250. if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
  251. return -EIO;
  252. /* Read the first 8 bytes in order to get the length of the response.
  253. * We read exactly a quad word in order to make sure that the remaining
  254. * reads will be aligned.
  255. */
  256. memcpy_fromio(buf, priv->rsp, 8);
  257. expected = be32_to_cpup((__be32 *)&buf[2]);
  258. if (expected > count || expected < TPM_HEADER_SIZE)
  259. return -EIO;
  260. memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8);
  261. return expected;
  262. }
  263. static int crb_do_acpi_start(struct tpm_chip *chip)
  264. {
  265. union acpi_object *obj;
  266. int rc;
  267. obj = acpi_evaluate_dsm(chip->acpi_dev_handle,
  268. &crb_acpi_start_guid,
  269. CRB_ACPI_START_REVISION_ID,
  270. CRB_ACPI_START_INDEX,
  271. NULL);
  272. if (!obj)
  273. return -ENXIO;
  274. rc = obj->integer.value == 0 ? 0 : -ENXIO;
  275. ACPI_FREE(obj);
  276. return rc;
  277. }
  278. #ifdef CONFIG_ARM64
  279. /*
  280. * This is a TPM Command Response Buffer start method that invokes a
  281. * Secure Monitor Call to requrest the firmware to execute or cancel
  282. * a TPM 2.0 command.
  283. */
  284. static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
  285. {
  286. struct arm_smccc_res res;
  287. arm_smccc_smc(func_id, 0, 0, 0, 0, 0, 0, 0, &res);
  288. if (res.a0 != 0) {
  289. dev_err(dev,
  290. FW_BUG "tpm_crb_smc_start() returns res.a0 = 0x%lx\n",
  291. res.a0);
  292. return -EIO;
  293. }
  294. return 0;
  295. }
  296. #else
  297. static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)
  298. {
  299. dev_err(dev, FW_BUG "tpm_crb: incorrect start method\n");
  300. return -EINVAL;
  301. }
  302. #endif
  303. static int crb_send(struct tpm_chip *chip, u8 *buf, size_t len)
  304. {
  305. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  306. int rc = 0;
  307. /* Zero the cancel register so that the next command will not get
  308. * canceled.
  309. */
  310. iowrite32(0, &priv->regs_t->ctrl_cancel);
  311. if (len > priv->cmd_size) {
  312. dev_err(&chip->dev, "invalid command count value %zd %d\n",
  313. len, priv->cmd_size);
  314. return -E2BIG;
  315. }
  316. memcpy_toio(priv->cmd, buf, len);
  317. /* Make sure that cmd is populated before issuing start. */
  318. wmb();
  319. /* The reason for the extra quirk is that the PTT in 4th Gen Core CPUs
  320. * report only ACPI start but in practice seems to require both
  321. * CRB start, hence invoking CRB start method if hid == MSFT0101.
  322. */
  323. if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
  324. (priv->sm == ACPI_TPM2_MEMORY_MAPPED) ||
  325. (!strcmp(priv->hid, "MSFT0101")))
  326. iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
  327. if ((priv->sm == ACPI_TPM2_START_METHOD) ||
  328. (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD))
  329. rc = crb_do_acpi_start(chip);
  330. if (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
  331. iowrite32(CRB_START_INVOKE, &priv->regs_t->ctrl_start);
  332. rc = tpm_crb_smc_start(&chip->dev, priv->smc_func_id);
  333. }
  334. return rc;
  335. }
  336. static void crb_cancel(struct tpm_chip *chip)
  337. {
  338. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  339. iowrite32(CRB_CANCEL_INVOKE, &priv->regs_t->ctrl_cancel);
  340. if (((priv->sm == ACPI_TPM2_START_METHOD) ||
  341. (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD)) &&
  342. crb_do_acpi_start(chip))
  343. dev_err(&chip->dev, "ACPI Start failed\n");
  344. }
  345. static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
  346. {
  347. struct crb_priv *priv = dev_get_drvdata(&chip->dev);
  348. u32 cancel = ioread32(&priv->regs_t->ctrl_cancel);
  349. return (cancel & CRB_CANCEL_INVOKE) == CRB_CANCEL_INVOKE;
  350. }
  351. static const struct tpm_class_ops tpm_crb = {
  352. .flags = TPM_OPS_AUTO_STARTUP,
  353. .status = crb_status,
  354. .recv = crb_recv,
  355. .send = crb_send,
  356. .cancel = crb_cancel,
  357. .req_canceled = crb_req_canceled,
  358. .go_idle = crb_go_idle,
  359. .cmd_ready = crb_cmd_ready,
  360. .request_locality = crb_request_locality,
  361. .relinquish_locality = crb_relinquish_locality,
  362. .req_complete_mask = CRB_DRV_STS_COMPLETE,
  363. .req_complete_val = CRB_DRV_STS_COMPLETE,
  364. };
  365. static int crb_check_resource(struct acpi_resource *ares, void *data)
  366. {
  367. struct resource *io_res = data;
  368. struct resource_win win;
  369. struct resource *res = &(win.res);
  370. if (acpi_dev_resource_memory(ares, res) ||
  371. acpi_dev_resource_address_space(ares, &win)) {
  372. *io_res = *res;
  373. io_res->name = NULL;
  374. }
  375. return 1;
  376. }
  377. static void __iomem *crb_map_res(struct device *dev, struct crb_priv *priv,
  378. struct resource *io_res, u64 start, u32 size)
  379. {
  380. struct resource new_res = {
  381. .start = start,
  382. .end = start + size - 1,
  383. .flags = IORESOURCE_MEM,
  384. };
  385. /* Detect a 64 bit address on a 32 bit system */
  386. if (start != new_res.start)
  387. return (void __iomem *) ERR_PTR(-EINVAL);
  388. if (!resource_contains(io_res, &new_res))
  389. return devm_ioremap_resource(dev, &new_res);
  390. return priv->iobase + (new_res.start - io_res->start);
  391. }
  392. /*
  393. * Work around broken BIOSs that return inconsistent values from the ACPI
  394. * region vs the registers. Trust the ACPI region. Such broken systems
  395. * probably cannot send large TPM commands since the buffer will be truncated.
  396. */
  397. static u64 crb_fixup_cmd_size(struct device *dev, struct resource *io_res,
  398. u64 start, u64 size)
  399. {
  400. if (io_res->start > start || io_res->end < start)
  401. return size;
  402. if (start + size - 1 <= io_res->end)
  403. return size;
  404. dev_err(dev,
  405. FW_BUG "ACPI region does not cover the entire command/response buffer. %pr vs %llx %llx\n",
  406. io_res, start, size);
  407. return io_res->end - start + 1;
  408. }
  409. static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
  410. struct acpi_table_tpm2 *buf)
  411. {
  412. struct list_head resources;
  413. struct resource io_res;
  414. struct device *dev = &device->dev;
  415. u32 pa_high, pa_low;
  416. u64 cmd_pa;
  417. u32 cmd_size;
  418. __le64 __rsp_pa;
  419. u64 rsp_pa;
  420. u32 rsp_size;
  421. int ret;
  422. INIT_LIST_HEAD(&resources);
  423. ret = acpi_dev_get_resources(device, &resources, crb_check_resource,
  424. &io_res);
  425. if (ret < 0)
  426. return ret;
  427. acpi_dev_free_resource_list(&resources);
  428. if (resource_type(&io_res) != IORESOURCE_MEM) {
  429. dev_err(dev, FW_BUG "TPM2 ACPI table does not define a memory resource\n");
  430. return -EINVAL;
  431. }
  432. priv->iobase = devm_ioremap_resource(dev, &io_res);
  433. if (IS_ERR(priv->iobase))
  434. return PTR_ERR(priv->iobase);
  435. /* The ACPI IO region starts at the head area and continues to include
  436. * the control area, as one nice sane region except for some older
  437. * stuff that puts the control area outside the ACPI IO region.
  438. */
  439. if ((priv->sm == ACPI_TPM2_COMMAND_BUFFER) ||
  440. (priv->sm == ACPI_TPM2_MEMORY_MAPPED)) {
  441. if (buf->control_address == io_res.start +
  442. sizeof(*priv->regs_h))
  443. priv->regs_h = priv->iobase;
  444. else
  445. dev_warn(dev, FW_BUG "Bad ACPI memory layout");
  446. }
  447. ret = __crb_request_locality(dev, priv, 0);
  448. if (ret)
  449. return ret;
  450. priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
  451. sizeof(struct crb_regs_tail));
  452. if (IS_ERR(priv->regs_t)) {
  453. ret = PTR_ERR(priv->regs_t);
  454. goto out_relinquish_locality;
  455. }
  456. /*
  457. * PTT HW bug w/a: wake up the device to access
  458. * possibly not retained registers.
  459. */
  460. ret = __crb_cmd_ready(dev, priv);
  461. if (ret)
  462. goto out_relinquish_locality;
  463. pa_high = ioread32(&priv->regs_t->ctrl_cmd_pa_high);
  464. pa_low = ioread32(&priv->regs_t->ctrl_cmd_pa_low);
  465. cmd_pa = ((u64)pa_high << 32) | pa_low;
  466. cmd_size = crb_fixup_cmd_size(dev, &io_res, cmd_pa,
  467. ioread32(&priv->regs_t->ctrl_cmd_size));
  468. dev_dbg(dev, "cmd_hi = %X cmd_low = %X cmd_size %X\n",
  469. pa_high, pa_low, cmd_size);
  470. priv->cmd = crb_map_res(dev, priv, &io_res, cmd_pa, cmd_size);
  471. if (IS_ERR(priv->cmd)) {
  472. ret = PTR_ERR(priv->cmd);
  473. goto out;
  474. }
  475. memcpy_fromio(&__rsp_pa, &priv->regs_t->ctrl_rsp_pa, 8);
  476. rsp_pa = le64_to_cpu(__rsp_pa);
  477. rsp_size = crb_fixup_cmd_size(dev, &io_res, rsp_pa,
  478. ioread32(&priv->regs_t->ctrl_rsp_size));
  479. if (cmd_pa != rsp_pa) {
  480. priv->rsp = crb_map_res(dev, priv, &io_res, rsp_pa, rsp_size);
  481. ret = PTR_ERR_OR_ZERO(priv->rsp);
  482. goto out;
  483. }
  484. /* According to the PTP specification, overlapping command and response
  485. * buffer sizes must be identical.
  486. */
  487. if (cmd_size != rsp_size) {
  488. dev_err(dev, FW_BUG "overlapping command and response buffer sizes are not identical");
  489. ret = -EINVAL;
  490. goto out;
  491. }
  492. priv->rsp = priv->cmd;
  493. out:
  494. if (!ret)
  495. priv->cmd_size = cmd_size;
  496. __crb_go_idle(dev, priv);
  497. out_relinquish_locality:
  498. __crb_relinquish_locality(dev, priv, 0);
  499. return ret;
  500. }
  501. static int crb_acpi_add(struct acpi_device *device)
  502. {
  503. struct acpi_table_tpm2 *buf;
  504. struct crb_priv *priv;
  505. struct tpm_chip *chip;
  506. struct device *dev = &device->dev;
  507. struct tpm2_crb_smc *crb_smc;
  508. acpi_status status;
  509. u32 sm;
  510. int rc;
  511. status = acpi_get_table(ACPI_SIG_TPM2, 1,
  512. (struct acpi_table_header **) &buf);
  513. if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
  514. dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
  515. return -EINVAL;
  516. }
  517. /* Should the FIFO driver handle this? */
  518. sm = buf->start_method;
  519. if (sm == ACPI_TPM2_MEMORY_MAPPED)
  520. return -ENODEV;
  521. priv = devm_kzalloc(dev, sizeof(struct crb_priv), GFP_KERNEL);
  522. if (!priv)
  523. return -ENOMEM;
  524. if (sm == ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC) {
  525. if (buf->header.length < (sizeof(*buf) + sizeof(*crb_smc))) {
  526. dev_err(dev,
  527. FW_BUG "TPM2 ACPI table has wrong size %u for start method type %d\n",
  528. buf->header.length,
  529. ACPI_TPM2_COMMAND_BUFFER_WITH_ARM_SMC);
  530. return -EINVAL;
  531. }
  532. crb_smc = ACPI_ADD_PTR(struct tpm2_crb_smc, buf, sizeof(*buf));
  533. priv->smc_func_id = crb_smc->smc_func_id;
  534. }
  535. priv->sm = sm;
  536. priv->hid = acpi_device_hid(device);
  537. rc = crb_map_io(device, priv, buf);
  538. if (rc)
  539. return rc;
  540. chip = tpmm_chip_alloc(dev, &tpm_crb);
  541. if (IS_ERR(chip))
  542. return PTR_ERR(chip);
  543. dev_set_drvdata(&chip->dev, priv);
  544. chip->acpi_dev_handle = device->handle;
  545. chip->flags = TPM_CHIP_FLAG_TPM2;
  546. return tpm_chip_register(chip);
  547. }
  548. static int crb_acpi_remove(struct acpi_device *device)
  549. {
  550. struct device *dev = &device->dev;
  551. struct tpm_chip *chip = dev_get_drvdata(dev);
  552. tpm_chip_unregister(chip);
  553. return 0;
  554. }
  555. static const struct dev_pm_ops crb_pm = {
  556. SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume)
  557. };
  558. static const struct acpi_device_id crb_device_ids[] = {
  559. {"MSFT0101", 0},
  560. {"", 0},
  561. };
  562. MODULE_DEVICE_TABLE(acpi, crb_device_ids);
  563. static struct acpi_driver crb_acpi_driver = {
  564. .name = "tpm_crb",
  565. .ids = crb_device_ids,
  566. .ops = {
  567. .add = crb_acpi_add,
  568. .remove = crb_acpi_remove,
  569. },
  570. .drv = {
  571. .pm = &crb_pm,
  572. },
  573. };
  574. module_acpi_driver(crb_acpi_driver);
  575. MODULE_AUTHOR("Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>");
  576. MODULE_DESCRIPTION("TPM2 Driver");
  577. MODULE_VERSION("0.1");
  578. MODULE_LICENSE("GPL");