tpm_tis.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 expected, status;
  252. if (count < TPM_HEADER_SIZE) {
  253. size = -EIO;
  254. goto out;
  255. }
  256. /* read first 10 bytes, including tag, paramsize, and result */
  257. if ((size =
  258. recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
  259. dev_err(chip->pdev, "Unable to read header\n");
  260. goto out;
  261. }
  262. expected = be32_to_cpu(*(__be32 *) (buf + 2));
  263. if (expected > count) {
  264. size = -EIO;
  265. goto out;
  266. }
  267. if ((size +=
  268. recv_data(chip, &buf[TPM_HEADER_SIZE],
  269. expected - TPM_HEADER_SIZE)) < expected) {
  270. dev_err(chip->pdev, "Unable to read remainder of result\n");
  271. size = -ETIME;
  272. goto out;
  273. }
  274. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  275. &chip->vendor.int_queue, false);
  276. status = tpm_tis_status(chip);
  277. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  278. dev_err(chip->pdev, "Error left over data\n");
  279. size = -EIO;
  280. goto out;
  281. }
  282. out:
  283. tpm_tis_ready(chip);
  284. release_locality(chip, chip->vendor.locality, 0);
  285. return size;
  286. }
  287. static bool itpm;
  288. module_param(itpm, bool, 0444);
  289. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  290. /*
  291. * If interrupts are used (signaled by an irq set in the vendor structure)
  292. * tpm.c can skip polling for the data to be available as the interrupt is
  293. * waited for here
  294. */
  295. static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
  296. {
  297. int rc, status, burstcnt;
  298. size_t count = 0;
  299. if (request_locality(chip, 0) < 0)
  300. return -EBUSY;
  301. status = tpm_tis_status(chip);
  302. if ((status & TPM_STS_COMMAND_READY) == 0) {
  303. tpm_tis_ready(chip);
  304. if (wait_for_tpm_stat
  305. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  306. &chip->vendor.int_queue, false) < 0) {
  307. rc = -ETIME;
  308. goto out_err;
  309. }
  310. }
  311. while (count < len - 1) {
  312. burstcnt = get_burstcount(chip);
  313. for (; burstcnt > 0 && count < len - 1; burstcnt--) {
  314. iowrite8(buf[count], chip->vendor.iobase +
  315. TPM_DATA_FIFO(chip->vendor.locality));
  316. count++;
  317. }
  318. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  319. &chip->vendor.int_queue, false);
  320. status = tpm_tis_status(chip);
  321. if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
  322. rc = -EIO;
  323. goto out_err;
  324. }
  325. }
  326. /* write last byte */
  327. iowrite8(buf[count],
  328. chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
  329. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  330. &chip->vendor.int_queue, false);
  331. status = tpm_tis_status(chip);
  332. if ((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. release_locality(chip, chip->vendor.locality, 0);
  340. return rc;
  341. }
  342. static void disable_interrupts(struct tpm_chip *chip)
  343. {
  344. u32 intmask;
  345. intmask =
  346. ioread32(chip->vendor.iobase +
  347. TPM_INT_ENABLE(chip->vendor.locality));
  348. intmask &= ~TPM_GLOBAL_INT_ENABLE;
  349. iowrite32(intmask,
  350. chip->vendor.iobase +
  351. TPM_INT_ENABLE(chip->vendor.locality));
  352. free_irq(chip->vendor.irq, chip);
  353. chip->vendor.irq = 0;
  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, u8 *buf, size_t len)
  361. {
  362. int rc;
  363. u32 ordinal;
  364. unsigned long dur;
  365. rc = tpm_tis_send_data(chip, buf, len);
  366. if (rc < 0)
  367. return rc;
  368. /* go and do it */
  369. iowrite8(TPM_STS_GO,
  370. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  371. if (chip->vendor.irq) {
  372. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  373. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  374. dur = tpm2_calc_ordinal_duration(chip, ordinal);
  375. else
  376. dur = tpm_calc_ordinal_duration(chip, ordinal);
  377. if (wait_for_tpm_stat
  378. (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
  379. &chip->vendor.read_queue, false) < 0) {
  380. rc = -ETIME;
  381. goto out_err;
  382. }
  383. }
  384. return len;
  385. out_err:
  386. tpm_tis_ready(chip);
  387. release_locality(chip, chip->vendor.locality, 0);
  388. return rc;
  389. }
  390. static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
  391. {
  392. int rc, irq;
  393. struct priv_data *priv = chip->vendor.priv;
  394. if (!chip->vendor.irq || priv->irq_tested)
  395. return tpm_tis_send_main(chip, buf, len);
  396. /* Verify receipt of the expected IRQ */
  397. irq = chip->vendor.irq;
  398. chip->vendor.irq = 0;
  399. rc = tpm_tis_send_main(chip, buf, len);
  400. chip->vendor.irq = irq;
  401. if (!priv->irq_tested)
  402. msleep(1);
  403. if (!priv->irq_tested) {
  404. disable_interrupts(chip);
  405. dev_err(chip->pdev,
  406. FW_BUG "TPM interrupt not working, polling instead\n");
  407. }
  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. int i;
  424. u32 did_vid;
  425. did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  426. for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
  427. if (vendor_timeout_overrides[i].did_vid != did_vid)
  428. continue;
  429. memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
  430. sizeof(vendor_timeout_overrides[i].timeout_us));
  431. return true;
  432. }
  433. return false;
  434. }
  435. /*
  436. * Early probing for iTPM with STS_DATA_EXPECT flaw.
  437. * Try sending command without itpm flag set and if that
  438. * fails, repeat with itpm flag set.
  439. */
  440. static int probe_itpm(struct tpm_chip *chip)
  441. {
  442. int rc = 0;
  443. u8 cmd_getticks[] = {
  444. 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
  445. 0x00, 0x00, 0x00, 0xf1
  446. };
  447. size_t len = sizeof(cmd_getticks);
  448. bool rem_itpm = itpm;
  449. u16 vendor = ioread16(chip->vendor.iobase + TPM_DID_VID(0));
  450. /* probe only iTPMS */
  451. if (vendor != TPM_VID_INTEL)
  452. return 0;
  453. itpm = false;
  454. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  455. if (rc == 0)
  456. goto out;
  457. tpm_tis_ready(chip);
  458. release_locality(chip, chip->vendor.locality, 0);
  459. itpm = true;
  460. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  461. if (rc == 0) {
  462. dev_info(chip->pdev, "Detected an iTPM.\n");
  463. rc = 1;
  464. } else
  465. rc = -EFAULT;
  466. out:
  467. itpm = rem_itpm;
  468. tpm_tis_ready(chip);
  469. release_locality(chip, chip->vendor.locality, 0);
  470. return rc;
  471. }
  472. static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
  473. {
  474. switch (chip->vendor.manufacturer_id) {
  475. case TPM_VID_WINBOND:
  476. return ((status == TPM_STS_VALID) ||
  477. (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
  478. case TPM_VID_STM:
  479. return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
  480. default:
  481. return (status == TPM_STS_COMMAND_READY);
  482. }
  483. }
  484. static const struct tpm_class_ops tpm_tis = {
  485. .status = tpm_tis_status,
  486. .recv = tpm_tis_recv,
  487. .send = tpm_tis_send,
  488. .cancel = tpm_tis_ready,
  489. .update_timeouts = tpm_tis_update_timeouts,
  490. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  491. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  492. .req_canceled = tpm_tis_req_canceled,
  493. };
  494. static irqreturn_t tis_int_probe(int irq, void *dev_id)
  495. {
  496. struct tpm_chip *chip = dev_id;
  497. u32 interrupt;
  498. interrupt = ioread32(chip->vendor.iobase +
  499. TPM_INT_STATUS(chip->vendor.locality));
  500. if (interrupt == 0)
  501. return IRQ_NONE;
  502. chip->vendor.probed_irq = irq;
  503. /* Clear interrupts handled with TPM_EOI */
  504. iowrite32(interrupt,
  505. chip->vendor.iobase +
  506. TPM_INT_STATUS(chip->vendor.locality));
  507. return IRQ_HANDLED;
  508. }
  509. static irqreturn_t tis_int_handler(int dummy, void *dev_id)
  510. {
  511. struct tpm_chip *chip = dev_id;
  512. u32 interrupt;
  513. int i;
  514. interrupt = ioread32(chip->vendor.iobase +
  515. TPM_INT_STATUS(chip->vendor.locality));
  516. if (interrupt == 0)
  517. return IRQ_NONE;
  518. ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
  519. if (interrupt & TPM_INTF_DATA_AVAIL_INT)
  520. wake_up_interruptible(&chip->vendor.read_queue);
  521. if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
  522. for (i = 0; i < 5; i++)
  523. if (check_locality(chip, i) >= 0)
  524. break;
  525. if (interrupt &
  526. (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
  527. TPM_INTF_CMD_READY_INT))
  528. wake_up_interruptible(&chip->vendor.int_queue);
  529. /* Clear interrupts handled with TPM_EOI */
  530. iowrite32(interrupt,
  531. chip->vendor.iobase +
  532. TPM_INT_STATUS(chip->vendor.locality));
  533. ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
  534. return IRQ_HANDLED;
  535. }
  536. static bool interrupts = true;
  537. module_param(interrupts, bool, 0444);
  538. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  539. static void tpm_tis_remove(struct tpm_chip *chip)
  540. {
  541. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  542. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  543. iowrite32(~TPM_GLOBAL_INT_ENABLE &
  544. ioread32(chip->vendor.iobase +
  545. TPM_INT_ENABLE(chip->vendor.
  546. locality)),
  547. chip->vendor.iobase +
  548. TPM_INT_ENABLE(chip->vendor.locality));
  549. release_locality(chip, chip->vendor.locality, 1);
  550. }
  551. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info,
  552. acpi_handle acpi_dev_handle)
  553. {
  554. u32 vendor, intfcaps, intmask;
  555. int rc, i, irq_s, irq_e, probe;
  556. struct tpm_chip *chip;
  557. struct priv_data *priv;
  558. priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
  559. if (priv == NULL)
  560. return -ENOMEM;
  561. chip = tpmm_chip_alloc(dev, &tpm_tis);
  562. if (IS_ERR(chip))
  563. return PTR_ERR(chip);
  564. chip->vendor.priv = priv;
  565. #ifdef CONFIG_ACPI
  566. chip->acpi_dev_handle = acpi_dev_handle;
  567. #endif
  568. chip->vendor.iobase = devm_ioremap(dev, tpm_info->start, tpm_info->len);
  569. if (!chip->vendor.iobase)
  570. return -EIO;
  571. /* Maximum timeouts */
  572. chip->vendor.timeout_a = TIS_TIMEOUT_A_MAX;
  573. chip->vendor.timeout_b = TIS_TIMEOUT_B_MAX;
  574. chip->vendor.timeout_c = TIS_TIMEOUT_C_MAX;
  575. chip->vendor.timeout_d = TIS_TIMEOUT_D_MAX;
  576. if (wait_startup(chip, 0) != 0) {
  577. rc = -ENODEV;
  578. goto out_err;
  579. }
  580. if (request_locality(chip, 0) != 0) {
  581. rc = -ENODEV;
  582. goto out_err;
  583. }
  584. rc = tpm2_probe(chip);
  585. if (rc)
  586. goto out_err;
  587. vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  588. chip->vendor.manufacturer_id = vendor;
  589. dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
  590. (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
  591. vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
  592. if (!itpm) {
  593. probe = probe_itpm(chip);
  594. if (probe < 0) {
  595. rc = -ENODEV;
  596. goto out_err;
  597. }
  598. itpm = !!probe;
  599. }
  600. if (itpm)
  601. dev_info(dev, "Intel iTPM workaround enabled\n");
  602. /* Figure out the capabilities */
  603. intfcaps =
  604. ioread32(chip->vendor.iobase +
  605. TPM_INTF_CAPS(chip->vendor.locality));
  606. dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
  607. intfcaps);
  608. if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
  609. dev_dbg(dev, "\tBurst Count Static\n");
  610. if (intfcaps & TPM_INTF_CMD_READY_INT)
  611. dev_dbg(dev, "\tCommand Ready Int Support\n");
  612. if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
  613. dev_dbg(dev, "\tInterrupt Edge Falling\n");
  614. if (intfcaps & TPM_INTF_INT_EDGE_RISING)
  615. dev_dbg(dev, "\tInterrupt Edge Rising\n");
  616. if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
  617. dev_dbg(dev, "\tInterrupt Level Low\n");
  618. if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
  619. dev_dbg(dev, "\tInterrupt Level High\n");
  620. if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
  621. dev_dbg(dev, "\tLocality Change Int Support\n");
  622. if (intfcaps & TPM_INTF_STS_VALID_INT)
  623. dev_dbg(dev, "\tSts Valid Int Support\n");
  624. if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
  625. dev_dbg(dev, "\tData Avail Int Support\n");
  626. /* INTERRUPT Setup */
  627. init_waitqueue_head(&chip->vendor.read_queue);
  628. init_waitqueue_head(&chip->vendor.int_queue);
  629. intmask =
  630. ioread32(chip->vendor.iobase +
  631. TPM_INT_ENABLE(chip->vendor.locality));
  632. intmask |= TPM_INTF_CMD_READY_INT
  633. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  634. | TPM_INTF_STS_VALID_INT;
  635. iowrite32(intmask,
  636. chip->vendor.iobase +
  637. TPM_INT_ENABLE(chip->vendor.locality));
  638. if (interrupts)
  639. chip->vendor.irq = tpm_info->irq;
  640. if (interrupts && !chip->vendor.irq) {
  641. irq_s =
  642. ioread8(chip->vendor.iobase +
  643. TPM_INT_VECTOR(chip->vendor.locality));
  644. if (irq_s) {
  645. irq_e = irq_s;
  646. } else {
  647. irq_s = 3;
  648. irq_e = 15;
  649. }
  650. for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
  651. iowrite8(i, chip->vendor.iobase +
  652. TPM_INT_VECTOR(chip->vendor.locality));
  653. if (devm_request_irq
  654. (dev, i, tis_int_probe, IRQF_SHARED,
  655. chip->devname, chip) != 0) {
  656. dev_info(chip->pdev,
  657. "Unable to request irq: %d for probe\n",
  658. i);
  659. continue;
  660. }
  661. /* Clear all existing */
  662. iowrite32(ioread32
  663. (chip->vendor.iobase +
  664. TPM_INT_STATUS(chip->vendor.locality)),
  665. chip->vendor.iobase +
  666. TPM_INT_STATUS(chip->vendor.locality));
  667. /* Turn on */
  668. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  669. chip->vendor.iobase +
  670. TPM_INT_ENABLE(chip->vendor.locality));
  671. chip->vendor.probed_irq = 0;
  672. /* Generate Interrupts */
  673. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  674. tpm2_gen_interrupt(chip);
  675. else
  676. tpm_gen_interrupt(chip);
  677. chip->vendor.irq = chip->vendor.probed_irq;
  678. /* free_irq will call into tis_int_probe;
  679. clear all irqs we haven't seen while doing
  680. tpm_gen_interrupt */
  681. iowrite32(ioread32
  682. (chip->vendor.iobase +
  683. TPM_INT_STATUS(chip->vendor.locality)),
  684. chip->vendor.iobase +
  685. TPM_INT_STATUS(chip->vendor.locality));
  686. /* Turn off */
  687. iowrite32(intmask,
  688. chip->vendor.iobase +
  689. TPM_INT_ENABLE(chip->vendor.locality));
  690. }
  691. }
  692. if (chip->vendor.irq) {
  693. iowrite8(chip->vendor.irq,
  694. chip->vendor.iobase +
  695. TPM_INT_VECTOR(chip->vendor.locality));
  696. if (devm_request_irq
  697. (dev, chip->vendor.irq, tis_int_handler, IRQF_SHARED,
  698. chip->devname, chip) != 0) {
  699. dev_info(chip->pdev,
  700. "Unable to request irq: %d for use\n",
  701. chip->vendor.irq);
  702. chip->vendor.irq = 0;
  703. } else {
  704. /* Clear all existing */
  705. iowrite32(ioread32
  706. (chip->vendor.iobase +
  707. TPM_INT_STATUS(chip->vendor.locality)),
  708. chip->vendor.iobase +
  709. TPM_INT_STATUS(chip->vendor.locality));
  710. /* Turn on */
  711. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  712. chip->vendor.iobase +
  713. TPM_INT_ENABLE(chip->vendor.locality));
  714. }
  715. }
  716. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  717. chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  718. chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  719. chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  720. chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  721. chip->vendor.duration[TPM_SHORT] =
  722. msecs_to_jiffies(TPM2_DURATION_SHORT);
  723. chip->vendor.duration[TPM_MEDIUM] =
  724. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  725. chip->vendor.duration[TPM_LONG] =
  726. msecs_to_jiffies(TPM2_DURATION_LONG);
  727. rc = tpm2_do_selftest(chip);
  728. if (rc == TPM2_RC_INITIALIZE) {
  729. dev_warn(dev, "Firmware has not started TPM\n");
  730. rc = tpm2_startup(chip, TPM2_SU_CLEAR);
  731. if (!rc)
  732. rc = tpm2_do_selftest(chip);
  733. }
  734. if (rc) {
  735. dev_err(dev, "TPM self test failed\n");
  736. if (rc > 0)
  737. rc = -ENODEV;
  738. goto out_err;
  739. }
  740. } else {
  741. if (tpm_get_timeouts(chip)) {
  742. dev_err(dev, "Could not get TPM timeouts and durations\n");
  743. rc = -ENODEV;
  744. goto out_err;
  745. }
  746. if (tpm_do_selftest(chip)) {
  747. dev_err(dev, "TPM self test failed\n");
  748. rc = -ENODEV;
  749. goto out_err;
  750. }
  751. }
  752. return tpm_chip_register(chip);
  753. out_err:
  754. tpm_tis_remove(chip);
  755. return rc;
  756. }
  757. #ifdef CONFIG_PM_SLEEP
  758. static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
  759. {
  760. u32 intmask;
  761. /* reenable interrupts that device may have lost or
  762. BIOS/firmware may have disabled */
  763. iowrite8(chip->vendor.irq, chip->vendor.iobase +
  764. TPM_INT_VECTOR(chip->vendor.locality));
  765. intmask =
  766. ioread32(chip->vendor.iobase +
  767. TPM_INT_ENABLE(chip->vendor.locality));
  768. intmask |= TPM_INTF_CMD_READY_INT
  769. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  770. | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
  771. iowrite32(intmask,
  772. chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
  773. }
  774. static int tpm_tis_resume(struct device *dev)
  775. {
  776. struct tpm_chip *chip = dev_get_drvdata(dev);
  777. int ret;
  778. if (chip->vendor.irq)
  779. tpm_tis_reenable_interrupts(chip);
  780. ret = tpm_pm_resume(dev);
  781. if (ret)
  782. return ret;
  783. /* TPM 1.2 requires self-test on resume. This function actually returns
  784. * an error code but for unknown reason it isn't handled.
  785. */
  786. if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
  787. tpm_do_selftest(chip);
  788. return 0;
  789. }
  790. #endif
  791. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  792. #ifdef CONFIG_PNP
  793. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  794. const struct pnp_device_id *pnp_id)
  795. {
  796. struct tpm_info tpm_info = tis_default_info;
  797. acpi_handle acpi_dev_handle = NULL;
  798. tpm_info.start = pnp_mem_start(pnp_dev, 0);
  799. tpm_info.len = pnp_mem_len(pnp_dev, 0);
  800. if (pnp_irq_valid(pnp_dev, 0))
  801. tpm_info.irq = pnp_irq(pnp_dev, 0);
  802. else
  803. interrupts = false;
  804. #ifdef CONFIG_ACPI
  805. if (pnp_acpi_device(pnp_dev)) {
  806. if (is_itpm(pnp_acpi_device(pnp_dev)))
  807. itpm = true;
  808. acpi_dev_handle = pnp_acpi_device(pnp_dev)->handle;
  809. }
  810. #endif
  811. return tpm_tis_init(&pnp_dev->dev, &tpm_info, acpi_dev_handle);
  812. }
  813. static struct pnp_device_id tpm_pnp_tbl[] = {
  814. {"PNP0C31", 0}, /* TPM */
  815. {"ATM1200", 0}, /* Atmel */
  816. {"IFX0102", 0}, /* Infineon */
  817. {"BCM0101", 0}, /* Broadcom */
  818. {"BCM0102", 0}, /* Broadcom */
  819. {"NSC1200", 0}, /* National */
  820. {"ICO0102", 0}, /* Intel */
  821. /* Add new here */
  822. {"", 0}, /* User Specified */
  823. {"", 0} /* Terminator */
  824. };
  825. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  826. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  827. {
  828. struct tpm_chip *chip = pnp_get_drvdata(dev);
  829. tpm_chip_unregister(chip);
  830. tpm_tis_remove(chip);
  831. }
  832. static struct pnp_driver tis_pnp_driver = {
  833. .name = "tpm_tis",
  834. .id_table = tpm_pnp_tbl,
  835. .probe = tpm_tis_pnp_init,
  836. .remove = tpm_tis_pnp_remove,
  837. .driver = {
  838. .pm = &tpm_tis_pm,
  839. },
  840. };
  841. #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
  842. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  843. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  844. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  845. #endif
  846. #ifdef CONFIG_ACPI
  847. static int tpm_check_resource(struct acpi_resource *ares, void *data)
  848. {
  849. struct tpm_info *tpm_info = (struct tpm_info *) data;
  850. struct resource res;
  851. if (acpi_dev_resource_interrupt(ares, 0, &res)) {
  852. tpm_info->irq = res.start;
  853. } else if (acpi_dev_resource_memory(ares, &res)) {
  854. tpm_info->start = res.start;
  855. tpm_info->len = resource_size(&res);
  856. }
  857. return 1;
  858. }
  859. static int tpm_tis_acpi_init(struct acpi_device *acpi_dev)
  860. {
  861. struct list_head resources;
  862. struct tpm_info tpm_info = tis_default_info;
  863. int ret;
  864. if (!is_fifo(acpi_dev))
  865. return -ENODEV;
  866. INIT_LIST_HEAD(&resources);
  867. ret = acpi_dev_get_resources(acpi_dev, &resources, tpm_check_resource,
  868. &tpm_info);
  869. if (ret < 0)
  870. return ret;
  871. acpi_dev_free_resource_list(&resources);
  872. if (!tpm_info.irq)
  873. interrupts = false;
  874. if (is_itpm(acpi_dev))
  875. itpm = true;
  876. return tpm_tis_init(&acpi_dev->dev, &tpm_info, acpi_dev->handle);
  877. }
  878. static int tpm_tis_acpi_remove(struct acpi_device *dev)
  879. {
  880. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  881. tpm_chip_unregister(chip);
  882. tpm_tis_remove(chip);
  883. return 0;
  884. }
  885. static struct acpi_device_id tpm_acpi_tbl[] = {
  886. {"MSFT0101", 0}, /* TPM 2.0 */
  887. /* Add new here */
  888. {"", 0}, /* User Specified */
  889. {"", 0} /* Terminator */
  890. };
  891. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  892. static struct acpi_driver tis_acpi_driver = {
  893. .name = "tpm_tis",
  894. .ids = tpm_acpi_tbl,
  895. .ops = {
  896. .add = tpm_tis_acpi_init,
  897. .remove = tpm_tis_acpi_remove,
  898. },
  899. .drv = {
  900. .pm = &tpm_tis_pm,
  901. },
  902. };
  903. #endif
  904. static struct platform_driver tis_drv = {
  905. .driver = {
  906. .name = "tpm_tis",
  907. .pm = &tpm_tis_pm,
  908. },
  909. };
  910. static struct platform_device *pdev;
  911. static bool force;
  912. module_param(force, bool, 0444);
  913. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  914. static int __init init_tis(void)
  915. {
  916. int rc;
  917. #ifdef CONFIG_PNP
  918. if (!force) {
  919. rc = pnp_register_driver(&tis_pnp_driver);
  920. if (rc)
  921. return rc;
  922. }
  923. #endif
  924. #ifdef CONFIG_ACPI
  925. if (!force) {
  926. rc = acpi_bus_register_driver(&tis_acpi_driver);
  927. if (rc) {
  928. #ifdef CONFIG_PNP
  929. pnp_unregister_driver(&tis_pnp_driver);
  930. #endif
  931. return rc;
  932. }
  933. }
  934. #endif
  935. if (!force)
  936. return 0;
  937. rc = platform_driver_register(&tis_drv);
  938. if (rc < 0)
  939. return rc;
  940. pdev = platform_device_register_simple("tpm_tis", -1, NULL, 0);
  941. if (IS_ERR(pdev)) {
  942. rc = PTR_ERR(pdev);
  943. goto err_dev;
  944. }
  945. rc = tpm_tis_init(&pdev->dev, &tis_default_info, NULL);
  946. if (rc)
  947. goto err_init;
  948. return 0;
  949. err_init:
  950. platform_device_unregister(pdev);
  951. err_dev:
  952. platform_driver_unregister(&tis_drv);
  953. return rc;
  954. }
  955. static void __exit cleanup_tis(void)
  956. {
  957. struct tpm_chip *chip;
  958. #if defined(CONFIG_PNP) || defined(CONFIG_ACPI)
  959. if (!force) {
  960. #ifdef CONFIG_ACPI
  961. acpi_bus_unregister_driver(&tis_acpi_driver);
  962. #endif
  963. #ifdef CONFIG_PNP
  964. pnp_unregister_driver(&tis_pnp_driver);
  965. #endif
  966. return;
  967. }
  968. #endif
  969. chip = dev_get_drvdata(&pdev->dev);
  970. tpm_chip_unregister(chip);
  971. tpm_tis_remove(chip);
  972. platform_device_unregister(pdev);
  973. platform_driver_unregister(&tis_drv);
  974. }
  975. module_init(init_tis);
  976. module_exit(cleanup_tis);
  977. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  978. MODULE_DESCRIPTION("TPM Driver");
  979. MODULE_VERSION("2.0");
  980. MODULE_LICENSE("GPL");