tpm_tis.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 <acpi/actbl2.h>
  32. #include "tpm.h"
  33. enum tis_access {
  34. TPM_ACCESS_VALID = 0x80,
  35. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  36. TPM_ACCESS_REQUEST_PENDING = 0x04,
  37. TPM_ACCESS_REQUEST_USE = 0x02,
  38. };
  39. enum tis_status {
  40. TPM_STS_VALID = 0x80,
  41. TPM_STS_COMMAND_READY = 0x40,
  42. TPM_STS_GO = 0x20,
  43. TPM_STS_DATA_AVAIL = 0x10,
  44. TPM_STS_DATA_EXPECT = 0x08,
  45. };
  46. enum tis_int_flags {
  47. TPM_GLOBAL_INT_ENABLE = 0x80000000,
  48. TPM_INTF_BURST_COUNT_STATIC = 0x100,
  49. TPM_INTF_CMD_READY_INT = 0x080,
  50. TPM_INTF_INT_EDGE_FALLING = 0x040,
  51. TPM_INTF_INT_EDGE_RISING = 0x020,
  52. TPM_INTF_INT_LEVEL_LOW = 0x010,
  53. TPM_INTF_INT_LEVEL_HIGH = 0x008,
  54. TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
  55. TPM_INTF_STS_VALID_INT = 0x002,
  56. TPM_INTF_DATA_AVAIL_INT = 0x001,
  57. };
  58. enum tis_defaults {
  59. TIS_MEM_BASE = 0xFED40000,
  60. TIS_MEM_LEN = 0x5000,
  61. TIS_SHORT_TIMEOUT = 750, /* ms */
  62. TIS_LONG_TIMEOUT = 2000, /* 2 sec */
  63. };
  64. struct tpm_info {
  65. unsigned long start;
  66. unsigned long len;
  67. unsigned int irq;
  68. };
  69. static struct tpm_info tis_default_info = {
  70. .start = TIS_MEM_BASE,
  71. .len = TIS_MEM_LEN,
  72. .irq = 0,
  73. };
  74. /* Some timeout values are needed before it is known whether the chip is
  75. * TPM 1.0 or TPM 2.0.
  76. */
  77. #define TIS_TIMEOUT_A_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
  78. #define TIS_TIMEOUT_B_MAX max(TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
  79. #define TIS_TIMEOUT_C_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
  80. #define TIS_TIMEOUT_D_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
  81. #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
  82. #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
  83. #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
  84. #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
  85. #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
  86. #define TPM_STS(l) (0x0018 | ((l) << 12))
  87. #define TPM_STS3(l) (0x001b | ((l) << 12))
  88. #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
  89. #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
  90. #define TPM_RID(l) (0x0F04 | ((l) << 12))
  91. struct priv_data {
  92. bool irq_tested;
  93. };
  94. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  95. static int has_hid(struct acpi_device *dev, const char *hid)
  96. {
  97. struct acpi_hardware_id *id;
  98. list_for_each_entry(id, &dev->pnp.ids, list)
  99. if (!strcmp(hid, id->id))
  100. return 1;
  101. return 0;
  102. }
  103. static inline int is_itpm(struct acpi_device *dev)
  104. {
  105. return has_hid(dev, "INTC0102");
  106. }
  107. static inline int is_fifo(struct acpi_device *dev)
  108. {
  109. struct acpi_table_tpm2 *tbl;
  110. acpi_status st;
  111. /* TPM 1.2 FIFO */
  112. if (!has_hid(dev, "MSFT0101"))
  113. return 1;
  114. st = acpi_get_table(ACPI_SIG_TPM2, 1,
  115. (struct acpi_table_header **) &tbl);
  116. if (ACPI_FAILURE(st)) {
  117. dev_err(&dev->dev, "failed to get TPM2 ACPI table\n");
  118. return 0;
  119. }
  120. if (le32_to_cpu(tbl->start_method) != TPM2_START_FIFO)
  121. return 0;
  122. /* TPM 2.0 FIFO */
  123. return 1;
  124. }
  125. #else
  126. static inline int is_itpm(struct acpi_device *dev)
  127. {
  128. return 0;
  129. }
  130. static inline int is_fifo(struct acpi_device *dev)
  131. {
  132. return 1;
  133. }
  134. #endif
  135. /* Before we attempt to access the TPM we must see that the valid bit is set.
  136. * The specification says that this bit is 0 at reset and remains 0 until the
  137. * 'TPM has gone through its self test and initialization and has established
  138. * correct values in the other bits.' */
  139. static int wait_startup(struct tpm_chip *chip, int l)
  140. {
  141. unsigned long stop = jiffies + chip->vendor.timeout_a;
  142. do {
  143. if (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  144. TPM_ACCESS_VALID)
  145. return 0;
  146. msleep(TPM_TIMEOUT);
  147. } while (time_before(jiffies, stop));
  148. return -1;
  149. }
  150. static int check_locality(struct tpm_chip *chip, int l)
  151. {
  152. if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  153. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  154. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  155. return chip->vendor.locality = l;
  156. return -1;
  157. }
  158. static void release_locality(struct tpm_chip *chip, int l, int force)
  159. {
  160. if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  161. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
  162. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
  163. iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
  164. chip->vendor.iobase + TPM_ACCESS(l));
  165. }
  166. static int request_locality(struct tpm_chip *chip, int l)
  167. {
  168. unsigned long stop, timeout;
  169. long rc;
  170. if (check_locality(chip, l) >= 0)
  171. return l;
  172. iowrite8(TPM_ACCESS_REQUEST_USE,
  173. chip->vendor.iobase + TPM_ACCESS(l));
  174. stop = jiffies + chip->vendor.timeout_a;
  175. if (chip->vendor.irq) {
  176. again:
  177. timeout = stop - jiffies;
  178. if ((long)timeout <= 0)
  179. return -1;
  180. rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
  181. (check_locality
  182. (chip, l) >= 0),
  183. timeout);
  184. if (rc > 0)
  185. return l;
  186. if (rc == -ERESTARTSYS && freezing(current)) {
  187. clear_thread_flag(TIF_SIGPENDING);
  188. goto again;
  189. }
  190. } else {
  191. /* wait for burstcount */
  192. do {
  193. if (check_locality(chip, l) >= 0)
  194. return l;
  195. msleep(TPM_TIMEOUT);
  196. }
  197. while (time_before(jiffies, stop));
  198. }
  199. return -1;
  200. }
  201. static u8 tpm_tis_status(struct tpm_chip *chip)
  202. {
  203. return ioread8(chip->vendor.iobase +
  204. TPM_STS(chip->vendor.locality));
  205. }
  206. static void tpm_tis_ready(struct tpm_chip *chip)
  207. {
  208. /* this causes the current command to be aborted */
  209. iowrite8(TPM_STS_COMMAND_READY,
  210. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  211. }
  212. static int get_burstcount(struct tpm_chip *chip)
  213. {
  214. unsigned long stop;
  215. int burstcnt;
  216. /* wait for burstcount */
  217. /* which timeout value, spec has 2 answers (c & d) */
  218. stop = jiffies + chip->vendor.timeout_d;
  219. do {
  220. burstcnt = ioread8(chip->vendor.iobase +
  221. TPM_STS(chip->vendor.locality) + 1);
  222. burstcnt += ioread8(chip->vendor.iobase +
  223. TPM_STS(chip->vendor.locality) +
  224. 2) << 8;
  225. if (burstcnt)
  226. return burstcnt;
  227. msleep(TPM_TIMEOUT);
  228. } while (time_before(jiffies, stop));
  229. return -EBUSY;
  230. }
  231. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  232. {
  233. int size = 0, burstcnt;
  234. while (size < count &&
  235. wait_for_tpm_stat(chip,
  236. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  237. chip->vendor.timeout_c,
  238. &chip->vendor.read_queue, true)
  239. == 0) {
  240. burstcnt = get_burstcount(chip);
  241. for (; burstcnt > 0 && size < count; burstcnt--)
  242. buf[size++] = ioread8(chip->vendor.iobase +
  243. TPM_DATA_FIFO(chip->vendor.
  244. locality));
  245. }
  246. return size;
  247. }
  248. static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  249. {
  250. int size = 0;
  251. int status;
  252. u32 expected;
  253. if (count < TPM_HEADER_SIZE) {
  254. size = -EIO;
  255. goto out;
  256. }
  257. /* read first 10 bytes, including tag, paramsize, and result */
  258. if ((size =
  259. recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
  260. dev_err(&chip->dev, "Unable to read header\n");
  261. goto out;
  262. }
  263. expected = be32_to_cpu(*(__be32 *) (buf + 2));
  264. if (expected > count || expected < TPM_HEADER_SIZE) {
  265. size = -EIO;
  266. goto out;
  267. }
  268. if ((size +=
  269. recv_data(chip, &buf[TPM_HEADER_SIZE],
  270. expected - TPM_HEADER_SIZE)) < expected) {
  271. dev_err(&chip->dev, "Unable to read remainder of result\n");
  272. size = -ETIME;
  273. goto out;
  274. }
  275. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  276. &chip->vendor.int_queue, false);
  277. status = tpm_tis_status(chip);
  278. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  279. dev_err(&chip->dev, "Error left over data\n");
  280. size = -EIO;
  281. goto out;
  282. }
  283. out:
  284. tpm_tis_ready(chip);
  285. release_locality(chip, chip->vendor.locality, 0);
  286. return size;
  287. }
  288. static bool itpm;
  289. module_param(itpm, bool, 0444);
  290. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  291. /*
  292. * If interrupts are used (signaled by an irq set in the vendor structure)
  293. * tpm.c can skip polling for the data to be available as the interrupt is
  294. * waited for here
  295. */
  296. static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
  297. {
  298. int rc, status, burstcnt;
  299. size_t count = 0;
  300. if (request_locality(chip, 0) < 0)
  301. return -EBUSY;
  302. status = tpm_tis_status(chip);
  303. if ((status & TPM_STS_COMMAND_READY) == 0) {
  304. tpm_tis_ready(chip);
  305. if (wait_for_tpm_stat
  306. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  307. &chip->vendor.int_queue, false) < 0) {
  308. rc = -ETIME;
  309. goto out_err;
  310. }
  311. }
  312. while (count < len - 1) {
  313. burstcnt = get_burstcount(chip);
  314. for (; burstcnt > 0 && count < len - 1; burstcnt--) {
  315. iowrite8(buf[count], chip->vendor.iobase +
  316. TPM_DATA_FIFO(chip->vendor.locality));
  317. count++;
  318. }
  319. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  320. &chip->vendor.int_queue, false);
  321. status = tpm_tis_status(chip);
  322. if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
  323. rc = -EIO;
  324. goto out_err;
  325. }
  326. }
  327. /* write last byte */
  328. iowrite8(buf[count],
  329. chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
  330. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  331. &chip->vendor.int_queue, false);
  332. status = tpm_tis_status(chip);
  333. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  334. rc = -EIO;
  335. goto out_err;
  336. }
  337. return 0;
  338. out_err:
  339. tpm_tis_ready(chip);
  340. release_locality(chip, chip->vendor.locality, 0);
  341. return rc;
  342. }
  343. static void disable_interrupts(struct tpm_chip *chip)
  344. {
  345. u32 intmask;
  346. intmask =
  347. ioread32(chip->vendor.iobase +
  348. TPM_INT_ENABLE(chip->vendor.locality));
  349. intmask &= ~TPM_GLOBAL_INT_ENABLE;
  350. iowrite32(intmask,
  351. chip->vendor.iobase +
  352. TPM_INT_ENABLE(chip->vendor.locality));
  353. devm_free_irq(&chip->dev, chip->vendor.irq, chip);
  354. chip->vendor.irq = 0;
  355. }
  356. /*
  357. * If interrupts are used (signaled by an irq set in the vendor structure)
  358. * tpm.c can skip polling for the data to be available as the interrupt is
  359. * waited for here
  360. */
  361. static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
  362. {
  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. iowrite8(TPM_STS_GO,
  371. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  372. if (chip->vendor.irq) {
  373. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  374. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  375. dur = tpm2_calc_ordinal_duration(chip, ordinal);
  376. else
  377. dur = tpm_calc_ordinal_duration(chip, ordinal);
  378. if (wait_for_tpm_stat
  379. (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
  380. &chip->vendor.read_queue, false) < 0) {
  381. rc = -ETIME;
  382. goto out_err;
  383. }
  384. }
  385. return len;
  386. out_err:
  387. tpm_tis_ready(chip);
  388. release_locality(chip, chip->vendor.locality, 0);
  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 priv_data *priv = chip->vendor.priv;
  395. if (!chip->vendor.irq || priv->irq_tested)
  396. return tpm_tis_send_main(chip, buf, len);
  397. /* Verify receipt of the expected IRQ */
  398. irq = chip->vendor.irq;
  399. chip->vendor.irq = 0;
  400. rc = tpm_tis_send_main(chip, buf, len);
  401. chip->vendor.irq = irq;
  402. if (!priv->irq_tested)
  403. msleep(1);
  404. if (!priv->irq_tested) {
  405. disable_interrupts(chip);
  406. dev_err(&chip->dev,
  407. FW_BUG "TPM interrupt not working, polling instead\n");
  408. }
  409. priv->irq_tested = true;
  410. return rc;
  411. }
  412. struct tis_vendor_timeout_override {
  413. u32 did_vid;
  414. unsigned long timeout_us[4];
  415. };
  416. static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
  417. /* Atmel 3204 */
  418. { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
  419. (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
  420. };
  421. static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
  422. unsigned long *timeout_cap)
  423. {
  424. int i;
  425. u32 did_vid;
  426. did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  427. for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
  428. if (vendor_timeout_overrides[i].did_vid != did_vid)
  429. continue;
  430. memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
  431. sizeof(vendor_timeout_overrides[i].timeout_us));
  432. return true;
  433. }
  434. return false;
  435. }
  436. /*
  437. * Early probing for iTPM with STS_DATA_EXPECT flaw.
  438. * Try sending command without itpm flag set and if that
  439. * fails, repeat with itpm flag set.
  440. */
  441. static int probe_itpm(struct tpm_chip *chip)
  442. {
  443. int rc = 0;
  444. u8 cmd_getticks[] = {
  445. 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
  446. 0x00, 0x00, 0x00, 0xf1
  447. };
  448. size_t len = sizeof(cmd_getticks);
  449. bool rem_itpm = itpm;
  450. u16 vendor = ioread16(chip->vendor.iobase + TPM_DID_VID(0));
  451. /* probe only iTPMS */
  452. if (vendor != TPM_VID_INTEL)
  453. return 0;
  454. itpm = false;
  455. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  456. if (rc == 0)
  457. goto out;
  458. tpm_tis_ready(chip);
  459. release_locality(chip, chip->vendor.locality, 0);
  460. itpm = true;
  461. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  462. if (rc == 0) {
  463. dev_info(&chip->dev, "Detected an iTPM.\n");
  464. rc = 1;
  465. } else
  466. rc = -EFAULT;
  467. out:
  468. itpm = rem_itpm;
  469. tpm_tis_ready(chip);
  470. release_locality(chip, chip->vendor.locality, 0);
  471. return rc;
  472. }
  473. static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
  474. {
  475. switch (chip->vendor.manufacturer_id) {
  476. case TPM_VID_WINBOND:
  477. return ((status == TPM_STS_VALID) ||
  478. (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
  479. case TPM_VID_STM:
  480. return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
  481. default:
  482. return (status == TPM_STS_COMMAND_READY);
  483. }
  484. }
  485. static const struct tpm_class_ops tpm_tis = {
  486. .status = tpm_tis_status,
  487. .recv = tpm_tis_recv,
  488. .send = tpm_tis_send,
  489. .cancel = tpm_tis_ready,
  490. .update_timeouts = tpm_tis_update_timeouts,
  491. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  492. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  493. .req_canceled = tpm_tis_req_canceled,
  494. };
  495. static irqreturn_t tis_int_probe(int irq, void *dev_id)
  496. {
  497. struct tpm_chip *chip = dev_id;
  498. u32 interrupt;
  499. interrupt = ioread32(chip->vendor.iobase +
  500. TPM_INT_STATUS(chip->vendor.locality));
  501. if (interrupt == 0)
  502. return IRQ_NONE;
  503. chip->vendor.probed_irq = irq;
  504. /* Clear interrupts handled with TPM_EOI */
  505. iowrite32(interrupt,
  506. chip->vendor.iobase +
  507. TPM_INT_STATUS(chip->vendor.locality));
  508. return IRQ_HANDLED;
  509. }
  510. static irqreturn_t tis_int_handler(int dummy, void *dev_id)
  511. {
  512. struct tpm_chip *chip = dev_id;
  513. u32 interrupt;
  514. int i;
  515. interrupt = ioread32(chip->vendor.iobase +
  516. TPM_INT_STATUS(chip->vendor.locality));
  517. if (interrupt == 0)
  518. return IRQ_NONE;
  519. ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
  520. if (interrupt & TPM_INTF_DATA_AVAIL_INT)
  521. wake_up_interruptible(&chip->vendor.read_queue);
  522. if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
  523. for (i = 0; i < 5; i++)
  524. if (check_locality(chip, i) >= 0)
  525. break;
  526. if (interrupt &
  527. (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
  528. TPM_INTF_CMD_READY_INT))
  529. wake_up_interruptible(&chip->vendor.int_queue);
  530. /* Clear interrupts handled with TPM_EOI */
  531. iowrite32(interrupt,
  532. chip->vendor.iobase +
  533. TPM_INT_STATUS(chip->vendor.locality));
  534. ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
  535. return IRQ_HANDLED;
  536. }
  537. static bool interrupts = true;
  538. module_param(interrupts, bool, 0444);
  539. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  540. static void tpm_tis_remove(struct tpm_chip *chip)
  541. {
  542. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  543. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  544. iowrite32(~TPM_GLOBAL_INT_ENABLE &
  545. ioread32(chip->vendor.iobase +
  546. TPM_INT_ENABLE(chip->vendor.
  547. locality)),
  548. chip->vendor.iobase +
  549. TPM_INT_ENABLE(chip->vendor.locality));
  550. release_locality(chip, chip->vendor.locality, 1);
  551. }
  552. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info,
  553. acpi_handle acpi_dev_handle)
  554. {
  555. u32 vendor, intfcaps, intmask;
  556. int rc, i, irq_s, irq_e, probe;
  557. int irq_r = -1;
  558. struct tpm_chip *chip;
  559. struct priv_data *priv;
  560. priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
  561. if (priv == NULL)
  562. return -ENOMEM;
  563. chip = tpmm_chip_alloc(dev, &tpm_tis);
  564. if (IS_ERR(chip))
  565. return PTR_ERR(chip);
  566. chip->vendor.priv = priv;
  567. #ifdef CONFIG_ACPI
  568. chip->acpi_dev_handle = acpi_dev_handle;
  569. #endif
  570. chip->vendor.iobase = devm_ioremap(dev, tpm_info->start, tpm_info->len);
  571. if (!chip->vendor.iobase)
  572. return -EIO;
  573. /* Maximum timeouts */
  574. chip->vendor.timeout_a = TIS_TIMEOUT_A_MAX;
  575. chip->vendor.timeout_b = TIS_TIMEOUT_B_MAX;
  576. chip->vendor.timeout_c = TIS_TIMEOUT_C_MAX;
  577. chip->vendor.timeout_d = TIS_TIMEOUT_D_MAX;
  578. if (wait_startup(chip, 0) != 0) {
  579. rc = -ENODEV;
  580. goto out_err;
  581. }
  582. if (request_locality(chip, 0) != 0) {
  583. rc = -ENODEV;
  584. goto out_err;
  585. }
  586. rc = tpm2_probe(chip);
  587. if (rc)
  588. goto out_err;
  589. vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  590. chip->vendor.manufacturer_id = vendor;
  591. dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
  592. (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
  593. vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
  594. if (!itpm) {
  595. probe = probe_itpm(chip);
  596. if (probe < 0) {
  597. rc = -ENODEV;
  598. goto out_err;
  599. }
  600. itpm = !!probe;
  601. }
  602. if (itpm)
  603. dev_info(dev, "Intel iTPM workaround enabled\n");
  604. /* Figure out the capabilities */
  605. intfcaps =
  606. ioread32(chip->vendor.iobase +
  607. TPM_INTF_CAPS(chip->vendor.locality));
  608. dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
  609. intfcaps);
  610. if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
  611. dev_dbg(dev, "\tBurst Count Static\n");
  612. if (intfcaps & TPM_INTF_CMD_READY_INT)
  613. dev_dbg(dev, "\tCommand Ready Int Support\n");
  614. if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
  615. dev_dbg(dev, "\tInterrupt Edge Falling\n");
  616. if (intfcaps & TPM_INTF_INT_EDGE_RISING)
  617. dev_dbg(dev, "\tInterrupt Edge Rising\n");
  618. if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
  619. dev_dbg(dev, "\tInterrupt Level Low\n");
  620. if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
  621. dev_dbg(dev, "\tInterrupt Level High\n");
  622. if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
  623. dev_dbg(dev, "\tLocality Change Int Support\n");
  624. if (intfcaps & TPM_INTF_STS_VALID_INT)
  625. dev_dbg(dev, "\tSts Valid Int Support\n");
  626. if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
  627. dev_dbg(dev, "\tData Avail Int Support\n");
  628. /* INTERRUPT Setup */
  629. init_waitqueue_head(&chip->vendor.read_queue);
  630. init_waitqueue_head(&chip->vendor.int_queue);
  631. intmask =
  632. ioread32(chip->vendor.iobase +
  633. TPM_INT_ENABLE(chip->vendor.locality));
  634. intmask |= TPM_INTF_CMD_READY_INT
  635. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  636. | TPM_INTF_STS_VALID_INT;
  637. iowrite32(intmask,
  638. chip->vendor.iobase +
  639. TPM_INT_ENABLE(chip->vendor.locality));
  640. if (interrupts)
  641. chip->vendor.irq = tpm_info->irq;
  642. if (interrupts && !chip->vendor.irq) {
  643. irq_s =
  644. ioread8(chip->vendor.iobase +
  645. TPM_INT_VECTOR(chip->vendor.locality));
  646. irq_r = irq_s;
  647. if (irq_s) {
  648. irq_e = irq_s;
  649. } else {
  650. irq_s = 3;
  651. irq_e = 15;
  652. }
  653. for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
  654. iowrite8(i, chip->vendor.iobase +
  655. TPM_INT_VECTOR(chip->vendor.locality));
  656. if (devm_request_irq
  657. (dev, i, tis_int_probe, IRQF_SHARED,
  658. chip->devname, chip) != 0) {
  659. dev_info(&chip->dev,
  660. "Unable to request irq: %d for probe\n",
  661. i);
  662. continue;
  663. }
  664. /* Clear all existing */
  665. iowrite32(ioread32
  666. (chip->vendor.iobase +
  667. TPM_INT_STATUS(chip->vendor.locality)),
  668. chip->vendor.iobase +
  669. TPM_INT_STATUS(chip->vendor.locality));
  670. /* Turn on */
  671. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  672. chip->vendor.iobase +
  673. TPM_INT_ENABLE(chip->vendor.locality));
  674. chip->vendor.probed_irq = 0;
  675. /* Generate Interrupts */
  676. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  677. tpm2_gen_interrupt(chip);
  678. else
  679. tpm_gen_interrupt(chip);
  680. chip->vendor.irq = chip->vendor.probed_irq;
  681. /* free_irq will call into tis_int_probe;
  682. clear all irqs we haven't seen while doing
  683. tpm_gen_interrupt */
  684. iowrite32(ioread32
  685. (chip->vendor.iobase +
  686. TPM_INT_STATUS(chip->vendor.locality)),
  687. chip->vendor.iobase +
  688. TPM_INT_STATUS(chip->vendor.locality));
  689. /* Turn off */
  690. iowrite32(intmask,
  691. chip->vendor.iobase +
  692. TPM_INT_ENABLE(chip->vendor.locality));
  693. devm_free_irq(dev, i, chip);
  694. }
  695. }
  696. if (chip->vendor.irq) {
  697. iowrite8(chip->vendor.irq,
  698. chip->vendor.iobase +
  699. TPM_INT_VECTOR(chip->vendor.locality));
  700. if (devm_request_irq
  701. (dev, chip->vendor.irq, tis_int_handler, IRQF_SHARED,
  702. chip->devname, chip) != 0) {
  703. dev_info(&chip->dev,
  704. "Unable to request irq: %d for use\n",
  705. chip->vendor.irq);
  706. chip->vendor.irq = 0;
  707. } else {
  708. /* Clear all existing */
  709. iowrite32(ioread32
  710. (chip->vendor.iobase +
  711. TPM_INT_STATUS(chip->vendor.locality)),
  712. chip->vendor.iobase +
  713. TPM_INT_STATUS(chip->vendor.locality));
  714. /* Turn on */
  715. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  716. chip->vendor.iobase +
  717. TPM_INT_ENABLE(chip->vendor.locality));
  718. }
  719. } else if (irq_r != -1)
  720. iowrite8(irq_r, chip->vendor.iobase +
  721. TPM_INT_VECTOR(chip->vendor.locality));
  722. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  723. chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  724. chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  725. chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  726. chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  727. chip->vendor.duration[TPM_SHORT] =
  728. msecs_to_jiffies(TPM2_DURATION_SHORT);
  729. chip->vendor.duration[TPM_MEDIUM] =
  730. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  731. chip->vendor.duration[TPM_LONG] =
  732. msecs_to_jiffies(TPM2_DURATION_LONG);
  733. rc = tpm2_do_selftest(chip);
  734. if (rc == TPM2_RC_INITIALIZE) {
  735. dev_warn(dev, "Firmware has not started TPM\n");
  736. rc = tpm2_startup(chip, TPM2_SU_CLEAR);
  737. if (!rc)
  738. rc = tpm2_do_selftest(chip);
  739. }
  740. if (rc) {
  741. dev_err(dev, "TPM self test failed\n");
  742. if (rc > 0)
  743. rc = -ENODEV;
  744. goto out_err;
  745. }
  746. } else {
  747. if (tpm_get_timeouts(chip)) {
  748. dev_err(dev, "Could not get TPM timeouts and durations\n");
  749. rc = -ENODEV;
  750. goto out_err;
  751. }
  752. if (tpm_do_selftest(chip)) {
  753. dev_err(dev, "TPM self test failed\n");
  754. rc = -ENODEV;
  755. goto out_err;
  756. }
  757. }
  758. return tpm_chip_register(chip);
  759. out_err:
  760. tpm_tis_remove(chip);
  761. return rc;
  762. }
  763. #ifdef CONFIG_PM_SLEEP
  764. static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
  765. {
  766. u32 intmask;
  767. /* reenable interrupts that device may have lost or
  768. BIOS/firmware may have disabled */
  769. iowrite8(chip->vendor.irq, chip->vendor.iobase +
  770. TPM_INT_VECTOR(chip->vendor.locality));
  771. intmask =
  772. ioread32(chip->vendor.iobase +
  773. TPM_INT_ENABLE(chip->vendor.locality));
  774. intmask |= TPM_INTF_CMD_READY_INT
  775. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  776. | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
  777. iowrite32(intmask,
  778. chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
  779. }
  780. static int tpm_tis_resume(struct device *dev)
  781. {
  782. struct tpm_chip *chip = dev_get_drvdata(dev);
  783. int ret;
  784. if (chip->vendor.irq)
  785. tpm_tis_reenable_interrupts(chip);
  786. ret = tpm_pm_resume(dev);
  787. if (ret)
  788. return ret;
  789. /* TPM 1.2 requires self-test on resume. This function actually returns
  790. * an error code but for unknown reason it isn't handled.
  791. */
  792. if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
  793. tpm_do_selftest(chip);
  794. return 0;
  795. }
  796. #endif
  797. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  798. #ifdef CONFIG_PNP
  799. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  800. const struct pnp_device_id *pnp_id)
  801. {
  802. struct tpm_info tpm_info = tis_default_info;
  803. acpi_handle acpi_dev_handle = NULL;
  804. tpm_info.start = pnp_mem_start(pnp_dev, 0);
  805. tpm_info.len = pnp_mem_len(pnp_dev, 0);
  806. if (pnp_irq_valid(pnp_dev, 0))
  807. tpm_info.irq = pnp_irq(pnp_dev, 0);
  808. else
  809. interrupts = false;
  810. #ifdef CONFIG_ACPI
  811. if (pnp_acpi_device(pnp_dev)) {
  812. if (is_itpm(pnp_acpi_device(pnp_dev)))
  813. itpm = true;
  814. acpi_dev_handle = pnp_acpi_device(pnp_dev)->handle;
  815. }
  816. #endif
  817. return tpm_tis_init(&pnp_dev->dev, &tpm_info, acpi_dev_handle);
  818. }
  819. static struct pnp_device_id tpm_pnp_tbl[] = {
  820. {"PNP0C31", 0}, /* TPM */
  821. {"ATM1200", 0}, /* Atmel */
  822. {"IFX0102", 0}, /* Infineon */
  823. {"BCM0101", 0}, /* Broadcom */
  824. {"BCM0102", 0}, /* Broadcom */
  825. {"NSC1200", 0}, /* National */
  826. {"ICO0102", 0}, /* Intel */
  827. /* Add new here */
  828. {"", 0}, /* User Specified */
  829. {"", 0} /* Terminator */
  830. };
  831. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  832. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  833. {
  834. struct tpm_chip *chip = pnp_get_drvdata(dev);
  835. tpm_chip_unregister(chip);
  836. tpm_tis_remove(chip);
  837. }
  838. static struct pnp_driver tis_pnp_driver = {
  839. .name = "tpm_tis",
  840. .id_table = tpm_pnp_tbl,
  841. .probe = tpm_tis_pnp_init,
  842. .remove = tpm_tis_pnp_remove,
  843. .driver = {
  844. .pm = &tpm_tis_pm,
  845. },
  846. };
  847. #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
  848. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  849. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  850. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  851. #endif
  852. #ifdef CONFIG_ACPI
  853. static int tpm_check_resource(struct acpi_resource *ares, void *data)
  854. {
  855. struct tpm_info *tpm_info = (struct tpm_info *) data;
  856. struct resource res;
  857. if (acpi_dev_resource_interrupt(ares, 0, &res)) {
  858. tpm_info->irq = res.start;
  859. } else if (acpi_dev_resource_memory(ares, &res)) {
  860. tpm_info->start = res.start;
  861. tpm_info->len = resource_size(&res);
  862. }
  863. return 1;
  864. }
  865. static int tpm_tis_acpi_init(struct acpi_device *acpi_dev)
  866. {
  867. struct list_head resources;
  868. struct tpm_info tpm_info = tis_default_info;
  869. int ret;
  870. if (!is_fifo(acpi_dev))
  871. return -ENODEV;
  872. INIT_LIST_HEAD(&resources);
  873. ret = acpi_dev_get_resources(acpi_dev, &resources, tpm_check_resource,
  874. &tpm_info);
  875. if (ret < 0)
  876. return ret;
  877. acpi_dev_free_resource_list(&resources);
  878. if (!tpm_info.irq)
  879. interrupts = false;
  880. if (is_itpm(acpi_dev))
  881. itpm = true;
  882. return tpm_tis_init(&acpi_dev->dev, &tpm_info, acpi_dev->handle);
  883. }
  884. static int tpm_tis_acpi_remove(struct acpi_device *dev)
  885. {
  886. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  887. tpm_chip_unregister(chip);
  888. tpm_tis_remove(chip);
  889. return 0;
  890. }
  891. static struct acpi_device_id tpm_acpi_tbl[] = {
  892. {"MSFT0101", 0}, /* TPM 2.0 */
  893. /* Add new here */
  894. {"", 0}, /* User Specified */
  895. {"", 0} /* Terminator */
  896. };
  897. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  898. static struct acpi_driver tis_acpi_driver = {
  899. .name = "tpm_tis",
  900. .ids = tpm_acpi_tbl,
  901. .ops = {
  902. .add = tpm_tis_acpi_init,
  903. .remove = tpm_tis_acpi_remove,
  904. },
  905. .drv = {
  906. .pm = &tpm_tis_pm,
  907. },
  908. };
  909. #endif
  910. static struct platform_driver tis_drv = {
  911. .driver = {
  912. .name = "tpm_tis",
  913. .pm = &tpm_tis_pm,
  914. },
  915. };
  916. static struct platform_device *pdev;
  917. static bool force;
  918. module_param(force, bool, 0444);
  919. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  920. static int __init init_tis(void)
  921. {
  922. int rc;
  923. #ifdef CONFIG_PNP
  924. if (!force) {
  925. rc = pnp_register_driver(&tis_pnp_driver);
  926. if (rc)
  927. return rc;
  928. }
  929. #endif
  930. #ifdef CONFIG_ACPI
  931. if (!force) {
  932. rc = acpi_bus_register_driver(&tis_acpi_driver);
  933. if (rc) {
  934. #ifdef CONFIG_PNP
  935. pnp_unregister_driver(&tis_pnp_driver);
  936. #endif
  937. return rc;
  938. }
  939. }
  940. #endif
  941. if (!force)
  942. return 0;
  943. rc = platform_driver_register(&tis_drv);
  944. if (rc < 0)
  945. return rc;
  946. pdev = platform_device_register_simple("tpm_tis", -1, NULL, 0);
  947. if (IS_ERR(pdev)) {
  948. rc = PTR_ERR(pdev);
  949. goto err_dev;
  950. }
  951. rc = tpm_tis_init(&pdev->dev, &tis_default_info, NULL);
  952. if (rc)
  953. goto err_init;
  954. return 0;
  955. err_init:
  956. platform_device_unregister(pdev);
  957. err_dev:
  958. platform_driver_unregister(&tis_drv);
  959. return rc;
  960. }
  961. static void __exit cleanup_tis(void)
  962. {
  963. struct tpm_chip *chip;
  964. #if defined(CONFIG_PNP) || defined(CONFIG_ACPI)
  965. if (!force) {
  966. #ifdef CONFIG_ACPI
  967. acpi_bus_unregister_driver(&tis_acpi_driver);
  968. #endif
  969. #ifdef CONFIG_PNP
  970. pnp_unregister_driver(&tis_pnp_driver);
  971. #endif
  972. return;
  973. }
  974. #endif
  975. chip = dev_get_drvdata(&pdev->dev);
  976. tpm_chip_unregister(chip);
  977. tpm_tis_remove(chip);
  978. platform_device_unregister(pdev);
  979. platform_driver_unregister(&tis_drv);
  980. }
  981. module_init(init_tis);
  982. module_exit(cleanup_tis);
  983. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  984. MODULE_DESCRIPTION("TPM Driver");
  985. MODULE_VERSION("2.0");
  986. MODULE_LICENSE("GPL");