tpm_crb.c 21 KB

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