tpm_tis_core.c 25 KB

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