tpm_tis_core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * Copyright (C) 2005, 2006 IBM Corporation
  3. * Copyright (C) 2014, 2015 Intel Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Kylene Hall <kjhall@us.ibm.com>
  8. *
  9. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  10. *
  11. * Device driver for TCG/TCPA TPM (trusted platform module).
  12. * Specifications at www.trustedcomputinggroup.org
  13. *
  14. * This device driver implements the TPM interface as defined in
  15. * the TCG TPM Interface Spec version 1.2, revision 1.0.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation, version 2 of the
  20. * License.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/pnp.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/wait.h>
  29. #include <linux/acpi.h>
  30. #include <linux/freezer.h>
  31. #include "tpm.h"
  32. #include "tpm_tis_core.h"
  33. static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value);
  34. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  35. bool check_cancel, bool *canceled)
  36. {
  37. u8 status = chip->ops->status(chip);
  38. *canceled = false;
  39. if ((status & mask) == mask)
  40. return true;
  41. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  42. *canceled = true;
  43. return true;
  44. }
  45. return false;
  46. }
  47. static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
  48. unsigned long timeout, wait_queue_head_t *queue,
  49. bool check_cancel)
  50. {
  51. unsigned long stop;
  52. long rc;
  53. u8 status;
  54. bool canceled = false;
  55. /* check current status */
  56. status = chip->ops->status(chip);
  57. if ((status & mask) == mask)
  58. return 0;
  59. stop = jiffies + timeout;
  60. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  61. again:
  62. timeout = stop - jiffies;
  63. if ((long)timeout <= 0)
  64. return -ETIME;
  65. rc = wait_event_interruptible_timeout(*queue,
  66. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  67. &canceled),
  68. timeout);
  69. if (rc > 0) {
  70. if (canceled)
  71. return -ECANCELED;
  72. return 0;
  73. }
  74. if (rc == -ERESTARTSYS && freezing(current)) {
  75. clear_thread_flag(TIF_SIGPENDING);
  76. goto again;
  77. }
  78. } else {
  79. do {
  80. usleep_range(TPM_TIMEOUT_USECS_MIN,
  81. TPM_TIMEOUT_USECS_MAX);
  82. status = chip->ops->status(chip);
  83. if ((status & mask) == mask)
  84. return 0;
  85. } while (time_before(jiffies, stop));
  86. }
  87. return -ETIME;
  88. }
  89. /* Before we attempt to access the TPM we must see that the valid bit is set.
  90. * The specification says that this bit is 0 at reset and remains 0 until the
  91. * 'TPM has gone through its self test and initialization and has established
  92. * correct values in the other bits.'
  93. */
  94. static int wait_startup(struct tpm_chip *chip, int l)
  95. {
  96. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  97. unsigned long stop = jiffies + chip->timeout_a;
  98. do {
  99. int rc;
  100. u8 access;
  101. rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
  102. if (rc < 0)
  103. return rc;
  104. if (access & TPM_ACCESS_VALID)
  105. return 0;
  106. tpm_msleep(TPM_TIMEOUT);
  107. } while (time_before(jiffies, stop));
  108. return -1;
  109. }
  110. static bool check_locality(struct tpm_chip *chip, int l)
  111. {
  112. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  113. int rc;
  114. u8 access;
  115. rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
  116. if (rc < 0)
  117. return false;
  118. if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  119. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
  120. priv->locality = l;
  121. return true;
  122. }
  123. return false;
  124. }
  125. static bool locality_inactive(struct tpm_chip *chip, int l)
  126. {
  127. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  128. int rc;
  129. u8 access;
  130. rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
  131. if (rc < 0)
  132. return false;
  133. if ((access & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
  134. == TPM_ACCESS_VALID)
  135. return true;
  136. return false;
  137. }
  138. static int release_locality(struct tpm_chip *chip, int l)
  139. {
  140. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  141. unsigned long stop, timeout;
  142. long rc;
  143. tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
  144. stop = jiffies + chip->timeout_a;
  145. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  146. again:
  147. timeout = stop - jiffies;
  148. if ((long)timeout <= 0)
  149. return -1;
  150. rc = wait_event_interruptible_timeout(priv->int_queue,
  151. (locality_inactive(chip, l)),
  152. timeout);
  153. if (rc > 0)
  154. return 0;
  155. if (rc == -ERESTARTSYS && freezing(current)) {
  156. clear_thread_flag(TIF_SIGPENDING);
  157. goto again;
  158. }
  159. } else {
  160. do {
  161. if (locality_inactive(chip, l))
  162. return 0;
  163. tpm_msleep(TPM_TIMEOUT);
  164. } while (time_before(jiffies, stop));
  165. }
  166. return -1;
  167. }
  168. static int request_locality(struct tpm_chip *chip, int l)
  169. {
  170. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  171. unsigned long stop, timeout;
  172. long rc;
  173. if (check_locality(chip, l))
  174. return l;
  175. rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
  176. if (rc < 0)
  177. return rc;
  178. stop = jiffies + chip->timeout_a;
  179. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  180. again:
  181. timeout = stop - jiffies;
  182. if ((long)timeout <= 0)
  183. return -1;
  184. rc = wait_event_interruptible_timeout(priv->int_queue,
  185. (check_locality
  186. (chip, l)),
  187. timeout);
  188. if (rc > 0)
  189. return l;
  190. if (rc == -ERESTARTSYS && freezing(current)) {
  191. clear_thread_flag(TIF_SIGPENDING);
  192. goto again;
  193. }
  194. } else {
  195. /* wait for burstcount */
  196. do {
  197. if (check_locality(chip, l))
  198. return l;
  199. tpm_msleep(TPM_TIMEOUT);
  200. } while (time_before(jiffies, stop));
  201. }
  202. return -1;
  203. }
  204. static u8 tpm_tis_status(struct tpm_chip *chip)
  205. {
  206. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  207. int rc;
  208. u8 status;
  209. rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
  210. if (rc < 0)
  211. return 0;
  212. return status;
  213. }
  214. static void tpm_tis_ready(struct tpm_chip *chip)
  215. {
  216. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  217. /* this causes the current command to be aborted */
  218. tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
  219. }
  220. static int get_burstcount(struct tpm_chip *chip)
  221. {
  222. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  223. unsigned long stop;
  224. int burstcnt, rc;
  225. u32 value;
  226. /* wait for burstcount */
  227. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  228. stop = jiffies + chip->timeout_a;
  229. else
  230. stop = jiffies + chip->timeout_d;
  231. do {
  232. rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
  233. if (rc < 0)
  234. return rc;
  235. burstcnt = (value >> 8) & 0xFFFF;
  236. if (burstcnt)
  237. return burstcnt;
  238. usleep_range(TPM_TIMEOUT_USECS_MIN, TPM_TIMEOUT_USECS_MAX);
  239. } while (time_before(jiffies, stop));
  240. return -EBUSY;
  241. }
  242. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  243. {
  244. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  245. int size = 0, burstcnt, rc;
  246. while (size < count) {
  247. rc = wait_for_tpm_stat(chip,
  248. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  249. chip->timeout_c,
  250. &priv->read_queue, true);
  251. if (rc < 0)
  252. return rc;
  253. burstcnt = get_burstcount(chip);
  254. if (burstcnt < 0) {
  255. dev_err(&chip->dev, "Unable to read burstcount\n");
  256. return burstcnt;
  257. }
  258. burstcnt = min_t(int, burstcnt, count - size);
  259. rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
  260. burstcnt, buf + size);
  261. if (rc < 0)
  262. return rc;
  263. size += burstcnt;
  264. }
  265. return size;
  266. }
  267. static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  268. {
  269. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  270. int size = 0;
  271. int status;
  272. u32 expected;
  273. if (count < TPM_HEADER_SIZE) {
  274. size = -EIO;
  275. goto out;
  276. }
  277. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  278. /* read first 10 bytes, including tag, paramsize, and result */
  279. if (size < TPM_HEADER_SIZE) {
  280. dev_err(&chip->dev, "Unable to read header\n");
  281. goto out;
  282. }
  283. expected = be32_to_cpu(*(__be32 *) (buf + 2));
  284. if (expected > count || expected < TPM_HEADER_SIZE) {
  285. size = -EIO;
  286. goto out;
  287. }
  288. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  289. expected - TPM_HEADER_SIZE);
  290. if (size < expected) {
  291. dev_err(&chip->dev, "Unable to read remainder of result\n");
  292. size = -ETIME;
  293. goto out;
  294. }
  295. if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
  296. &priv->int_queue, false) < 0) {
  297. size = -ETIME;
  298. goto out;
  299. }
  300. status = tpm_tis_status(chip);
  301. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  302. dev_err(&chip->dev, "Error left over data\n");
  303. size = -EIO;
  304. goto out;
  305. }
  306. out:
  307. tpm_tis_ready(chip);
  308. return size;
  309. }
  310. /*
  311. * If interrupts are used (signaled by an irq set in the vendor structure)
  312. * tpm.c can skip polling for the data to be available as the interrupt is
  313. * waited for here
  314. */
  315. static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
  316. {
  317. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  318. int rc, status, burstcnt;
  319. size_t count = 0;
  320. bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
  321. status = tpm_tis_status(chip);
  322. if ((status & TPM_STS_COMMAND_READY) == 0) {
  323. tpm_tis_ready(chip);
  324. if (wait_for_tpm_stat
  325. (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
  326. &priv->int_queue, false) < 0) {
  327. rc = -ETIME;
  328. goto out_err;
  329. }
  330. }
  331. while (count < len - 1) {
  332. burstcnt = get_burstcount(chip);
  333. if (burstcnt < 0) {
  334. dev_err(&chip->dev, "Unable to read burstcount\n");
  335. rc = burstcnt;
  336. goto out_err;
  337. }
  338. burstcnt = min_t(int, burstcnt, len - count - 1);
  339. rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
  340. burstcnt, buf + count);
  341. if (rc < 0)
  342. goto out_err;
  343. count += burstcnt;
  344. if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
  345. &priv->int_queue, false) < 0) {
  346. rc = -ETIME;
  347. goto out_err;
  348. }
  349. status = tpm_tis_status(chip);
  350. if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
  351. rc = -EIO;
  352. goto out_err;
  353. }
  354. }
  355. /* write last byte */
  356. rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
  357. if (rc < 0)
  358. goto out_err;
  359. if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
  360. &priv->int_queue, false) < 0) {
  361. rc = -ETIME;
  362. goto out_err;
  363. }
  364. status = tpm_tis_status(chip);
  365. if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
  366. rc = -EIO;
  367. goto out_err;
  368. }
  369. return 0;
  370. out_err:
  371. tpm_tis_ready(chip);
  372. return rc;
  373. }
  374. static void disable_interrupts(struct tpm_chip *chip)
  375. {
  376. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  377. u32 intmask;
  378. int rc;
  379. rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
  380. if (rc < 0)
  381. intmask = 0;
  382. intmask &= ~TPM_GLOBAL_INT_ENABLE;
  383. rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
  384. devm_free_irq(chip->dev.parent, priv->irq, chip);
  385. priv->irq = 0;
  386. chip->flags &= ~TPM_CHIP_FLAG_IRQ;
  387. }
  388. /*
  389. * If interrupts are used (signaled by an irq set in the vendor structure)
  390. * tpm.c can skip polling for the data to be available as the interrupt is
  391. * waited for here
  392. */
  393. static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
  394. {
  395. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  396. int rc;
  397. u32 ordinal;
  398. unsigned long dur;
  399. rc = tpm_tis_send_data(chip, buf, len);
  400. if (rc < 0)
  401. return rc;
  402. /* go and do it */
  403. rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
  404. if (rc < 0)
  405. goto out_err;
  406. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  407. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  408. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  409. dur = tpm2_calc_ordinal_duration(chip, ordinal);
  410. else
  411. dur = tpm_calc_ordinal_duration(chip, ordinal);
  412. if (wait_for_tpm_stat
  413. (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
  414. &priv->read_queue, false) < 0) {
  415. rc = -ETIME;
  416. goto out_err;
  417. }
  418. }
  419. return 0;
  420. out_err:
  421. tpm_tis_ready(chip);
  422. return rc;
  423. }
  424. static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
  425. {
  426. int rc, irq;
  427. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  428. if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
  429. return tpm_tis_send_main(chip, buf, len);
  430. /* Verify receipt of the expected IRQ */
  431. irq = priv->irq;
  432. priv->irq = 0;
  433. chip->flags &= ~TPM_CHIP_FLAG_IRQ;
  434. rc = tpm_tis_send_main(chip, buf, len);
  435. priv->irq = irq;
  436. chip->flags |= TPM_CHIP_FLAG_IRQ;
  437. if (!priv->irq_tested)
  438. tpm_msleep(1);
  439. if (!priv->irq_tested)
  440. disable_interrupts(chip);
  441. priv->irq_tested = true;
  442. return rc;
  443. }
  444. struct tis_vendor_timeout_override {
  445. u32 did_vid;
  446. unsigned long timeout_us[4];
  447. };
  448. static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
  449. /* Atmel 3204 */
  450. { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
  451. (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
  452. };
  453. static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
  454. unsigned long *timeout_cap)
  455. {
  456. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  457. int i, rc;
  458. u32 did_vid;
  459. if (chip->ops->clk_enable != NULL)
  460. chip->ops->clk_enable(chip, true);
  461. rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
  462. if (rc < 0)
  463. goto out;
  464. for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
  465. if (vendor_timeout_overrides[i].did_vid != did_vid)
  466. continue;
  467. memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
  468. sizeof(vendor_timeout_overrides[i].timeout_us));
  469. rc = true;
  470. }
  471. rc = false;
  472. out:
  473. if (chip->ops->clk_enable != NULL)
  474. chip->ops->clk_enable(chip, false);
  475. return rc;
  476. }
  477. /*
  478. * Early probing for iTPM with STS_DATA_EXPECT flaw.
  479. * Try sending command without itpm flag set and if that
  480. * fails, repeat with itpm flag set.
  481. */
  482. static int probe_itpm(struct tpm_chip *chip)
  483. {
  484. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  485. int rc = 0;
  486. static const u8 cmd_getticks[] = {
  487. 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
  488. 0x00, 0x00, 0x00, 0xf1
  489. };
  490. size_t len = sizeof(cmd_getticks);
  491. u16 vendor;
  492. if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
  493. return 0;
  494. rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
  495. if (rc < 0)
  496. return rc;
  497. /* probe only iTPMS */
  498. if (vendor != TPM_VID_INTEL)
  499. return 0;
  500. if (request_locality(chip, 0) != 0)
  501. return -EBUSY;
  502. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  503. if (rc == 0)
  504. goto out;
  505. tpm_tis_ready(chip);
  506. priv->flags |= TPM_TIS_ITPM_WORKAROUND;
  507. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  508. if (rc == 0)
  509. dev_info(&chip->dev, "Detected an iTPM.\n");
  510. else {
  511. priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
  512. rc = -EFAULT;
  513. }
  514. out:
  515. tpm_tis_ready(chip);
  516. release_locality(chip, priv->locality);
  517. return rc;
  518. }
  519. static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
  520. {
  521. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  522. switch (priv->manufacturer_id) {
  523. case TPM_VID_WINBOND:
  524. return ((status == TPM_STS_VALID) ||
  525. (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
  526. case TPM_VID_STM:
  527. return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
  528. default:
  529. return (status == TPM_STS_COMMAND_READY);
  530. }
  531. }
  532. static irqreturn_t tis_int_handler(int dummy, void *dev_id)
  533. {
  534. struct tpm_chip *chip = dev_id;
  535. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  536. u32 interrupt;
  537. int i, rc;
  538. rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
  539. if (rc < 0)
  540. return IRQ_NONE;
  541. if (interrupt == 0)
  542. return IRQ_NONE;
  543. priv->irq_tested = true;
  544. if (interrupt & TPM_INTF_DATA_AVAIL_INT)
  545. wake_up_interruptible(&priv->read_queue);
  546. if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
  547. for (i = 0; i < 5; i++)
  548. if (check_locality(chip, i))
  549. break;
  550. if (interrupt &
  551. (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
  552. TPM_INTF_CMD_READY_INT))
  553. wake_up_interruptible(&priv->int_queue);
  554. /* Clear interrupts handled with TPM_EOI */
  555. rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
  556. if (rc < 0)
  557. return IRQ_NONE;
  558. tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
  559. return IRQ_HANDLED;
  560. }
  561. static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
  562. {
  563. const char *desc = "attempting to generate an interrupt";
  564. u32 cap2;
  565. cap_t cap;
  566. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  567. return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
  568. else
  569. return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
  570. 0);
  571. }
  572. /* Register the IRQ and issue a command that will cause an interrupt. If an
  573. * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
  574. * everything and leave in polling mode. Returns 0 on success.
  575. */
  576. static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
  577. int flags, int irq)
  578. {
  579. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  580. u8 original_int_vec;
  581. int rc;
  582. u32 int_status;
  583. if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
  584. dev_name(&chip->dev), chip) != 0) {
  585. dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
  586. irq);
  587. return -1;
  588. }
  589. priv->irq = irq;
  590. rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
  591. &original_int_vec);
  592. if (rc < 0)
  593. return rc;
  594. rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
  595. if (rc < 0)
  596. return rc;
  597. rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
  598. if (rc < 0)
  599. return rc;
  600. /* Clear all existing */
  601. rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
  602. if (rc < 0)
  603. return rc;
  604. /* Turn on */
  605. rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
  606. intmask | TPM_GLOBAL_INT_ENABLE);
  607. if (rc < 0)
  608. return rc;
  609. priv->irq_tested = false;
  610. /* Generate an interrupt by having the core call through to
  611. * tpm_tis_send
  612. */
  613. rc = tpm_tis_gen_interrupt(chip);
  614. if (rc < 0)
  615. return rc;
  616. /* tpm_tis_send will either confirm the interrupt is working or it
  617. * will call disable_irq which undoes all of the above.
  618. */
  619. if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
  620. rc = tpm_tis_write8(priv, original_int_vec,
  621. TPM_INT_VECTOR(priv->locality));
  622. if (rc < 0)
  623. return rc;
  624. return 1;
  625. }
  626. return 0;
  627. }
  628. /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
  629. * do not have ACPI/etc. We typically expect the interrupt to be declared if
  630. * present.
  631. */
  632. static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
  633. {
  634. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  635. u8 original_int_vec;
  636. int i, rc;
  637. rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
  638. &original_int_vec);
  639. if (rc < 0)
  640. return;
  641. if (!original_int_vec) {
  642. if (IS_ENABLED(CONFIG_X86))
  643. for (i = 3; i <= 15; i++)
  644. if (!tpm_tis_probe_irq_single(chip, intmask, 0,
  645. i))
  646. return;
  647. } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
  648. original_int_vec))
  649. return;
  650. }
  651. void tpm_tis_remove(struct tpm_chip *chip)
  652. {
  653. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  654. u32 reg = TPM_INT_ENABLE(priv->locality);
  655. u32 interrupt;
  656. int rc;
  657. tpm_tis_clkrun_enable(chip, true);
  658. rc = tpm_tis_read32(priv, reg, &interrupt);
  659. if (rc < 0)
  660. interrupt = 0;
  661. tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
  662. tpm_tis_clkrun_enable(chip, false);
  663. if (priv->ilb_base_addr)
  664. iounmap(priv->ilb_base_addr);
  665. }
  666. EXPORT_SYMBOL_GPL(tpm_tis_remove);
  667. /**
  668. * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
  669. * of a single TPM command
  670. * @chip: TPM chip to use
  671. * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
  672. * 0 - Enable CLKRUN protocol
  673. * Call this function directly in tpm_tis_remove() in error or driver removal
  674. * path, since the chip->ops is set to NULL in tpm_chip_unregister().
  675. */
  676. static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
  677. {
  678. struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
  679. u32 clkrun_val;
  680. if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
  681. !data->ilb_base_addr)
  682. return;
  683. if (value) {
  684. data->clkrun_enabled++;
  685. if (data->clkrun_enabled > 1)
  686. return;
  687. clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
  688. /* Disable LPC CLKRUN# */
  689. clkrun_val &= ~LPC_CLKRUN_EN;
  690. iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
  691. /*
  692. * Write any random value on port 0x80 which is on LPC, to make
  693. * sure LPC clock is running before sending any TPM command.
  694. */
  695. outb(0xCC, 0x80);
  696. } else {
  697. data->clkrun_enabled--;
  698. if (data->clkrun_enabled)
  699. return;
  700. clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
  701. /* Enable LPC CLKRUN# */
  702. clkrun_val |= LPC_CLKRUN_EN;
  703. iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
  704. /*
  705. * Write any random value on port 0x80 which is on LPC, to make
  706. * sure LPC clock is running before sending any TPM command.
  707. */
  708. outb(0xCC, 0x80);
  709. }
  710. }
  711. static const struct tpm_class_ops tpm_tis = {
  712. .flags = TPM_OPS_AUTO_STARTUP,
  713. .status = tpm_tis_status,
  714. .recv = tpm_tis_recv,
  715. .send = tpm_tis_send,
  716. .cancel = tpm_tis_ready,
  717. .update_timeouts = tpm_tis_update_timeouts,
  718. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  719. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  720. .req_canceled = tpm_tis_req_canceled,
  721. .request_locality = request_locality,
  722. .relinquish_locality = release_locality,
  723. .clk_enable = tpm_tis_clkrun_enable,
  724. };
  725. int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
  726. const struct tpm_tis_phy_ops *phy_ops,
  727. acpi_handle acpi_dev_handle)
  728. {
  729. u32 vendor;
  730. u32 intfcaps;
  731. u32 intmask;
  732. u32 clkrun_val;
  733. u8 rid;
  734. int rc, probe;
  735. struct tpm_chip *chip;
  736. chip = tpmm_chip_alloc(dev, &tpm_tis);
  737. if (IS_ERR(chip))
  738. return PTR_ERR(chip);
  739. #ifdef CONFIG_ACPI
  740. chip->acpi_dev_handle = acpi_dev_handle;
  741. #endif
  742. chip->hwrng.quality = priv->rng_quality;
  743. /* Maximum timeouts */
  744. chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
  745. chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
  746. chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
  747. chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
  748. priv->phy_ops = phy_ops;
  749. dev_set_drvdata(&chip->dev, priv);
  750. if (is_bsw()) {
  751. priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
  752. ILB_REMAP_SIZE);
  753. if (!priv->ilb_base_addr)
  754. return -ENOMEM;
  755. clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
  756. /* Check if CLKRUN# is already not enabled in the LPC bus */
  757. if (!(clkrun_val & LPC_CLKRUN_EN)) {
  758. iounmap(priv->ilb_base_addr);
  759. priv->ilb_base_addr = NULL;
  760. }
  761. }
  762. if (chip->ops->clk_enable != NULL)
  763. chip->ops->clk_enable(chip, true);
  764. if (wait_startup(chip, 0) != 0) {
  765. rc = -ENODEV;
  766. goto out_err;
  767. }
  768. /* Take control of the TPM's interrupt hardware and shut it off */
  769. rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
  770. if (rc < 0)
  771. goto out_err;
  772. intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
  773. TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
  774. intmask &= ~TPM_GLOBAL_INT_ENABLE;
  775. tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
  776. rc = tpm2_probe(chip);
  777. if (rc)
  778. goto out_err;
  779. rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
  780. if (rc < 0)
  781. goto out_err;
  782. priv->manufacturer_id = vendor;
  783. rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
  784. if (rc < 0)
  785. goto out_err;
  786. dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
  787. (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
  788. vendor >> 16, rid);
  789. probe = probe_itpm(chip);
  790. if (probe < 0) {
  791. rc = -ENODEV;
  792. goto out_err;
  793. }
  794. /* Figure out the capabilities */
  795. rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
  796. if (rc < 0)
  797. goto out_err;
  798. dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
  799. intfcaps);
  800. if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
  801. dev_dbg(dev, "\tBurst Count Static\n");
  802. if (intfcaps & TPM_INTF_CMD_READY_INT)
  803. dev_dbg(dev, "\tCommand Ready Int Support\n");
  804. if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
  805. dev_dbg(dev, "\tInterrupt Edge Falling\n");
  806. if (intfcaps & TPM_INTF_INT_EDGE_RISING)
  807. dev_dbg(dev, "\tInterrupt Edge Rising\n");
  808. if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
  809. dev_dbg(dev, "\tInterrupt Level Low\n");
  810. if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
  811. dev_dbg(dev, "\tInterrupt Level High\n");
  812. if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
  813. dev_dbg(dev, "\tLocality Change Int Support\n");
  814. if (intfcaps & TPM_INTF_STS_VALID_INT)
  815. dev_dbg(dev, "\tSts Valid Int Support\n");
  816. if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
  817. dev_dbg(dev, "\tData Avail Int Support\n");
  818. /* INTERRUPT Setup */
  819. init_waitqueue_head(&priv->read_queue);
  820. init_waitqueue_head(&priv->int_queue);
  821. if (irq != -1) {
  822. /* Before doing irq testing issue a command to the TPM in polling mode
  823. * to make sure it works. May as well use that command to set the
  824. * proper timeouts for the driver.
  825. */
  826. if (tpm_get_timeouts(chip)) {
  827. dev_err(dev, "Could not get TPM timeouts and durations\n");
  828. rc = -ENODEV;
  829. goto out_err;
  830. }
  831. if (irq) {
  832. tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
  833. irq);
  834. if (!(chip->flags & TPM_CHIP_FLAG_IRQ))
  835. dev_err(&chip->dev, FW_BUG
  836. "TPM interrupt not working, polling instead\n");
  837. } else {
  838. tpm_tis_probe_irq(chip, intmask);
  839. }
  840. }
  841. rc = tpm_chip_register(chip);
  842. if (rc)
  843. goto out_err;
  844. if (chip->ops->clk_enable != NULL)
  845. chip->ops->clk_enable(chip, false);
  846. return 0;
  847. out_err:
  848. if ((chip->ops != NULL) && (chip->ops->clk_enable != NULL))
  849. chip->ops->clk_enable(chip, false);
  850. tpm_tis_remove(chip);
  851. return rc;
  852. }
  853. EXPORT_SYMBOL_GPL(tpm_tis_core_init);
  854. #ifdef CONFIG_PM_SLEEP
  855. static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
  856. {
  857. struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  858. u32 intmask;
  859. int rc;
  860. if (chip->ops->clk_enable != NULL)
  861. chip->ops->clk_enable(chip, true);
  862. /* reenable interrupts that device may have lost or
  863. * BIOS/firmware may have disabled
  864. */
  865. rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
  866. if (rc < 0)
  867. goto out;
  868. rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
  869. if (rc < 0)
  870. goto out;
  871. intmask |= TPM_INTF_CMD_READY_INT
  872. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  873. | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
  874. tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
  875. out:
  876. if (chip->ops->clk_enable != NULL)
  877. chip->ops->clk_enable(chip, false);
  878. return;
  879. }
  880. int tpm_tis_resume(struct device *dev)
  881. {
  882. struct tpm_chip *chip = dev_get_drvdata(dev);
  883. int ret;
  884. if (chip->flags & TPM_CHIP_FLAG_IRQ)
  885. tpm_tis_reenable_interrupts(chip);
  886. ret = tpm_pm_resume(dev);
  887. if (ret)
  888. return ret;
  889. /* TPM 1.2 requires self-test on resume. This function actually returns
  890. * an error code but for unknown reason it isn't handled.
  891. */
  892. if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
  893. tpm_do_selftest(chip);
  894. return 0;
  895. }
  896. EXPORT_SYMBOL_GPL(tpm_tis_resume);
  897. #endif
  898. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  899. MODULE_DESCRIPTION("TPM Driver");
  900. MODULE_VERSION("2.0");
  901. MODULE_LICENSE("GPL");