tpm_tis_i2c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * tpm_tis_i2c.c - QEMU's TPM TIS I2C Device
  3. *
  4. * Copyright (c) 2023 IBM Corporation
  5. *
  6. * Authors:
  7. * Ninad Palsule <ninad@linux.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. * TPM I2C implementation follows TCG TPM I2c Interface specification,
  13. * Family 2.0, Level 00, Revision 1.00
  14. *
  15. * TPM TIS for TPM 2 implementation following TCG PC Client Platform
  16. * TPM Profile (PTP) Specification, Family 2.0, Revision 00.43
  17. *
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/i2c/i2c.h"
  21. #include "hw/sysbus.h"
  22. #include "hw/acpi/tpm.h"
  23. #include "migration/vmstate.h"
  24. #include "tpm_prop.h"
  25. #include "qemu/log.h"
  26. #include "trace.h"
  27. #include "tpm_tis.h"
  28. /* Operations */
  29. #define OP_SEND 1
  30. #define OP_RECV 2
  31. /* Is locality valid */
  32. #define TPM_TIS_I2C_IS_VALID_LOCTY(x) TPM_TIS_IS_VALID_LOCTY(x)
  33. typedef struct TPMStateI2C {
  34. /*< private >*/
  35. I2CSlave parent_obj;
  36. uint8_t offset; /* offset into data[] */
  37. uint8_t operation; /* OP_SEND & OP_RECV */
  38. uint8_t data[5]; /* Data */
  39. /* i2c registers */
  40. uint8_t loc_sel; /* Current locality */
  41. uint8_t csum_enable; /* Is checksum enabled */
  42. /* Derived from the above */
  43. const char *reg_name; /* Register name */
  44. uint32_t tis_addr; /* Converted tis address including locty */
  45. /*< public >*/
  46. TPMState state; /* not a QOM object */
  47. } TPMStateI2C;
  48. DECLARE_INSTANCE_CHECKER(TPMStateI2C, TPM_TIS_I2C,
  49. TYPE_TPM_TIS_I2C)
  50. /* Prototype */
  51. static inline void tpm_tis_i2c_to_tis_reg(TPMStateI2C *i2cst, uint8_t i2c_reg);
  52. /* Register map */
  53. typedef struct regMap {
  54. uint8_t i2c_reg; /* I2C register */
  55. uint16_t tis_reg; /* TIS register */
  56. const char *reg_name; /* Register name */
  57. } I2CRegMap;
  58. /*
  59. * The register values in the common code is different than the latest
  60. * register numbers as per the spec hence add the conversion map
  61. */
  62. static const I2CRegMap tpm_tis_reg_map[] = {
  63. /*
  64. * These registers are sent to TIS layer. The register with UNKNOWN
  65. * mapping are not sent to TIS layer and handled in I2c layer.
  66. * NOTE: Adding frequently used registers at the start
  67. */
  68. { TPM_I2C_REG_DATA_FIFO, TPM_TIS_REG_DATA_FIFO, "FIFO", },
  69. { TPM_I2C_REG_STS, TPM_TIS_REG_STS, "STS", },
  70. { TPM_I2C_REG_DATA_CSUM_GET, TPM_I2C_REG_UNKNOWN, "CSUM_GET", },
  71. { TPM_I2C_REG_LOC_SEL, TPM_I2C_REG_UNKNOWN, "LOC_SEL", },
  72. { TPM_I2C_REG_ACCESS, TPM_TIS_REG_ACCESS, "ACCESS", },
  73. { TPM_I2C_REG_INT_ENABLE, TPM_TIS_REG_INT_ENABLE, "INTR_ENABLE",},
  74. { TPM_I2C_REG_INT_CAPABILITY, TPM_I2C_REG_UNKNOWN, "INTR_CAP", },
  75. { TPM_I2C_REG_INTF_CAPABILITY, TPM_TIS_REG_INTF_CAPABILITY, "INTF_CAP", },
  76. { TPM_I2C_REG_DID_VID, TPM_TIS_REG_DID_VID, "DID_VID", },
  77. { TPM_I2C_REG_RID, TPM_TIS_REG_RID, "RID", },
  78. { TPM_I2C_REG_I2C_DEV_ADDRESS, TPM_I2C_REG_UNKNOWN, "DEV_ADDRESS",},
  79. { TPM_I2C_REG_DATA_CSUM_ENABLE, TPM_I2C_REG_UNKNOWN, "CSUM_ENABLE",},
  80. };
  81. static int tpm_tis_i2c_pre_save(void *opaque)
  82. {
  83. TPMStateI2C *i2cst = opaque;
  84. return tpm_tis_pre_save(&i2cst->state);
  85. }
  86. static int tpm_tis_i2c_post_load(void *opaque, int version_id)
  87. {
  88. TPMStateI2C *i2cst = opaque;
  89. if (i2cst->offset >= 1) {
  90. tpm_tis_i2c_to_tis_reg(i2cst, i2cst->data[0]);
  91. }
  92. return 0;
  93. }
  94. static const VMStateDescription vmstate_tpm_tis_i2c = {
  95. .name = "tpm-tis-i2c",
  96. .version_id = 0,
  97. .pre_save = tpm_tis_i2c_pre_save,
  98. .post_load = tpm_tis_i2c_post_load,
  99. .fields = (const VMStateField[]) {
  100. VMSTATE_BUFFER(state.buffer, TPMStateI2C),
  101. VMSTATE_UINT16(state.rw_offset, TPMStateI2C),
  102. VMSTATE_UINT8(state.active_locty, TPMStateI2C),
  103. VMSTATE_UINT8(state.aborting_locty, TPMStateI2C),
  104. VMSTATE_UINT8(state.next_locty, TPMStateI2C),
  105. VMSTATE_STRUCT_ARRAY(state.loc, TPMStateI2C, TPM_TIS_NUM_LOCALITIES, 0,
  106. vmstate_locty, TPMLocality),
  107. /* i2c specifics */
  108. VMSTATE_UINT8(offset, TPMStateI2C),
  109. VMSTATE_UINT8(operation, TPMStateI2C),
  110. VMSTATE_BUFFER(data, TPMStateI2C),
  111. VMSTATE_UINT8(loc_sel, TPMStateI2C),
  112. VMSTATE_UINT8(csum_enable, TPMStateI2C),
  113. VMSTATE_END_OF_LIST()
  114. }
  115. };
  116. /*
  117. * Set data value. The i2cst->offset is not updated as called in
  118. * the read path.
  119. */
  120. static void tpm_tis_i2c_set_data(TPMStateI2C *i2cst, uint32_t data)
  121. {
  122. i2cst->data[1] = data;
  123. i2cst->data[2] = data >> 8;
  124. i2cst->data[3] = data >> 16;
  125. i2cst->data[4] = data >> 24;
  126. }
  127. /*
  128. * Generate interface capability based on what is returned by TIS and what is
  129. * expected by I2C. Save the capability in the data array overwriting the TIS
  130. * capability.
  131. */
  132. static uint32_t tpm_tis_i2c_interface_capability(TPMStateI2C *i2cst,
  133. uint32_t tis_cap)
  134. {
  135. uint32_t i2c_cap;
  136. /* Now generate i2c capability */
  137. i2c_cap = (TPM_I2C_CAP_INTERFACE_TYPE |
  138. TPM_I2C_CAP_INTERFACE_VER |
  139. TPM_I2C_CAP_TPM2_FAMILY |
  140. TPM_I2C_CAP_LOCALITY_CAP |
  141. TPM_I2C_CAP_BUS_SPEED |
  142. TPM_I2C_CAP_DEV_ADDR_CHANGE);
  143. /* Now check the TIS and set some capabilities */
  144. /* Static burst count set */
  145. if (tis_cap & TPM_TIS_CAP_BURST_COUNT_STATIC) {
  146. i2c_cap |= TPM_I2C_CAP_BURST_COUNT_STATIC;
  147. }
  148. return i2c_cap;
  149. }
  150. /* Convert I2C register to TIS address and returns the name of the register */
  151. static inline void tpm_tis_i2c_to_tis_reg(TPMStateI2C *i2cst, uint8_t i2c_reg)
  152. {
  153. const I2CRegMap *reg_map;
  154. int i;
  155. i2cst->tis_addr = 0xffffffff;
  156. /* Special case for the STS register. */
  157. if (i2c_reg >= TPM_I2C_REG_STS && i2c_reg <= TPM_I2C_REG_STS + 3) {
  158. i2c_reg = TPM_I2C_REG_STS;
  159. }
  160. for (i = 0; i < ARRAY_SIZE(tpm_tis_reg_map); i++) {
  161. reg_map = &tpm_tis_reg_map[i];
  162. if (reg_map->i2c_reg == i2c_reg) {
  163. i2cst->reg_name = reg_map->reg_name;
  164. i2cst->tis_addr = reg_map->tis_reg;
  165. /* Include the locality in the address. */
  166. assert(TPM_TIS_I2C_IS_VALID_LOCTY(i2cst->loc_sel));
  167. i2cst->tis_addr += (i2cst->loc_sel << TPM_TIS_LOCALITY_SHIFT);
  168. break;
  169. }
  170. }
  171. }
  172. /* Clear some fields from the structure. */
  173. static inline void tpm_tis_i2c_clear_data(TPMStateI2C *i2cst)
  174. {
  175. /* Clear operation and offset */
  176. i2cst->operation = 0;
  177. i2cst->offset = 0;
  178. i2cst->tis_addr = 0xffffffff;
  179. i2cst->reg_name = NULL;
  180. memset(i2cst->data, 0, sizeof(i2cst->data));
  181. return;
  182. }
  183. /* Send data to TPM */
  184. static inline void tpm_tis_i2c_tpm_send(TPMStateI2C *i2cst)
  185. {
  186. uint32_t data;
  187. size_t offset = 0;
  188. uint32_t sz = 4;
  189. if ((i2cst->operation == OP_SEND) && (i2cst->offset > 1)) {
  190. switch (i2cst->data[0]) {
  191. case TPM_I2C_REG_DATA_CSUM_ENABLE:
  192. /*
  193. * Checksum is not handled by TIS code hence we will consume the
  194. * register here.
  195. */
  196. i2cst->csum_enable = i2cst->data[1] & TPM_DATA_CSUM_ENABLED;
  197. break;
  198. case TPM_I2C_REG_DATA_FIFO:
  199. /* Handled in the main i2c_send function */
  200. break;
  201. case TPM_I2C_REG_LOC_SEL:
  202. /*
  203. * This register is not handled by TIS so save the locality
  204. * locally
  205. */
  206. if (TPM_TIS_I2C_IS_VALID_LOCTY(i2cst->data[1])) {
  207. i2cst->loc_sel = i2cst->data[1];
  208. }
  209. break;
  210. default:
  211. /* We handle non-FIFO here */
  212. /* Index 0 is a register. Convert byte stream to uint32_t */
  213. data = i2cst->data[1];
  214. data |= i2cst->data[2] << 8;
  215. data |= i2cst->data[3] << 16;
  216. data |= i2cst->data[4] << 24;
  217. /* Add register specific masking */
  218. switch (i2cst->data[0]) {
  219. case TPM_I2C_REG_INT_ENABLE:
  220. data &= TPM_I2C_INT_ENABLE_MASK;
  221. break;
  222. case TPM_I2C_REG_STS ... TPM_I2C_REG_STS + 3:
  223. /*
  224. * STS register has 4 bytes data.
  225. * As per the specs following writes must be allowed.
  226. * - From base address 1 to 4 bytes are allowed.
  227. * - Single byte write to first or last byte must
  228. * be allowed.
  229. */
  230. offset = i2cst->data[0] - TPM_I2C_REG_STS;
  231. if (offset > 0) {
  232. sz = 1;
  233. }
  234. data &= (TPM_I2C_STS_WRITE_MASK >> (offset * 8));
  235. break;
  236. }
  237. tpm_tis_write_data(&i2cst->state, i2cst->tis_addr + offset, data,
  238. sz);
  239. break;
  240. }
  241. tpm_tis_i2c_clear_data(i2cst);
  242. }
  243. return;
  244. }
  245. /* Callback from TPM to indicate that response is copied */
  246. static void tpm_tis_i2c_request_completed(TPMIf *ti, int ret)
  247. {
  248. TPMStateI2C *i2cst = TPM_TIS_I2C(ti);
  249. TPMState *s = &i2cst->state;
  250. /* Inform the common code. */
  251. tpm_tis_request_completed(s, ret);
  252. }
  253. static enum TPMVersion tpm_tis_i2c_get_tpm_version(TPMIf *ti)
  254. {
  255. TPMStateI2C *i2cst = TPM_TIS_I2C(ti);
  256. TPMState *s = &i2cst->state;
  257. return tpm_tis_get_tpm_version(s);
  258. }
  259. static int tpm_tis_i2c_event(I2CSlave *i2c, enum i2c_event event)
  260. {
  261. TPMStateI2C *i2cst = TPM_TIS_I2C(i2c);
  262. int ret = 0;
  263. switch (event) {
  264. case I2C_START_RECV:
  265. trace_tpm_tis_i2c_event("START_RECV");
  266. break;
  267. case I2C_START_SEND:
  268. trace_tpm_tis_i2c_event("START_SEND");
  269. tpm_tis_i2c_clear_data(i2cst);
  270. break;
  271. case I2C_FINISH:
  272. trace_tpm_tis_i2c_event("FINISH");
  273. if (i2cst->operation == OP_SEND) {
  274. tpm_tis_i2c_tpm_send(i2cst);
  275. } else {
  276. tpm_tis_i2c_clear_data(i2cst);
  277. }
  278. break;
  279. default:
  280. break;
  281. }
  282. return ret;
  283. }
  284. /*
  285. * If data is for FIFO then it is received from tpm_tis_common buffer
  286. * otherwise it will be handled using single call to common code and
  287. * cached in the local buffer.
  288. */
  289. static uint8_t tpm_tis_i2c_recv(I2CSlave *i2c)
  290. {
  291. int ret = 0;
  292. uint32_t data_read;
  293. TPMStateI2C *i2cst = TPM_TIS_I2C(i2c);
  294. TPMState *s = &i2cst->state;
  295. uint16_t i2c_reg = i2cst->data[0];
  296. size_t offset;
  297. if (i2cst->operation == OP_RECV) {
  298. /* Do not cache FIFO data. */
  299. if (i2cst->data[0] == TPM_I2C_REG_DATA_FIFO) {
  300. data_read = tpm_tis_read_data(s, i2cst->tis_addr, 1);
  301. ret = (data_read & 0xff);
  302. } else if (i2cst->offset < sizeof(i2cst->data)) {
  303. ret = i2cst->data[i2cst->offset++];
  304. }
  305. } else if ((i2cst->operation == OP_SEND) && (i2cst->offset < 2)) {
  306. /* First receive call after send */
  307. i2cst->operation = OP_RECV;
  308. switch (i2c_reg) {
  309. case TPM_I2C_REG_LOC_SEL:
  310. /* Location selection register is managed by i2c */
  311. tpm_tis_i2c_set_data(i2cst, i2cst->loc_sel);
  312. break;
  313. case TPM_I2C_REG_DATA_FIFO:
  314. /* FIFO data is directly read from TPM TIS */
  315. data_read = tpm_tis_read_data(s, i2cst->tis_addr, 1);
  316. tpm_tis_i2c_set_data(i2cst, (data_read & 0xff));
  317. break;
  318. case TPM_I2C_REG_DATA_CSUM_ENABLE:
  319. tpm_tis_i2c_set_data(i2cst, i2cst->csum_enable);
  320. break;
  321. case TPM_I2C_REG_INT_CAPABILITY:
  322. /*
  323. * Interrupt is not supported in the linux kernel hence we cannot
  324. * test this model with interrupts.
  325. */
  326. tpm_tis_i2c_set_data(i2cst, TPM_I2C_INT_ENABLE_MASK);
  327. break;
  328. case TPM_I2C_REG_DATA_CSUM_GET:
  329. /*
  330. * Checksum registers are not supported by common code hence
  331. * call a common code to get the checksum.
  332. */
  333. data_read = tpm_tis_get_checksum(s);
  334. /* Save the byte stream in data field */
  335. tpm_tis_i2c_set_data(i2cst, data_read);
  336. break;
  337. default:
  338. data_read = tpm_tis_read_data(s, i2cst->tis_addr, 4);
  339. switch (i2c_reg) {
  340. case TPM_I2C_REG_INTF_CAPABILITY:
  341. /* Prepare the capabilities as per I2C interface */
  342. data_read = tpm_tis_i2c_interface_capability(i2cst,
  343. data_read);
  344. break;
  345. case TPM_I2C_REG_STS ... TPM_I2C_REG_STS + 3:
  346. offset = i2c_reg - TPM_I2C_REG_STS;
  347. /*
  348. * As per specs, STS bit 31:26 are reserved and must
  349. * be set to 0
  350. */
  351. data_read &= TPM_I2C_STS_READ_MASK;
  352. /*
  353. * STS register has 4 bytes data.
  354. * As per the specs following reads must be allowed.
  355. * - From base address 1 to 4 bytes are allowed.
  356. * - Last byte must be allowed to read as a single byte
  357. * - Second and third byte must be allowed to read as two
  358. * two bytes.
  359. */
  360. data_read >>= (offset * 8);
  361. break;
  362. }
  363. /* Save byte stream in data[] */
  364. tpm_tis_i2c_set_data(i2cst, data_read);
  365. break;
  366. }
  367. /* Return first byte with this call */
  368. i2cst->offset = 1; /* keep the register value intact for debug */
  369. ret = i2cst->data[i2cst->offset++];
  370. } else {
  371. i2cst->operation = OP_RECV;
  372. }
  373. trace_tpm_tis_i2c_recv(ret);
  374. return ret;
  375. }
  376. /*
  377. * Send function only remembers data in the buffer and then calls
  378. * TPM TIS common code during FINISH event.
  379. */
  380. static int tpm_tis_i2c_send(I2CSlave *i2c, uint8_t data)
  381. {
  382. TPMStateI2C *i2cst = TPM_TIS_I2C(i2c);
  383. /* Reject non-supported registers. */
  384. if (i2cst->offset == 0) {
  385. /* Convert I2C register to TIS register */
  386. tpm_tis_i2c_to_tis_reg(i2cst, data);
  387. if (i2cst->tis_addr == 0xffffffff) {
  388. return 0xffffffff;
  389. }
  390. trace_tpm_tis_i2c_send_reg(i2cst->reg_name, data);
  391. /* We do not support device address change */
  392. if (data == TPM_I2C_REG_I2C_DEV_ADDRESS) {
  393. qemu_log_mask(LOG_UNIMP, "%s: Device address change "
  394. "is not supported.\n", __func__);
  395. return 0xffffffff;
  396. }
  397. } else {
  398. trace_tpm_tis_i2c_send(data);
  399. }
  400. if (i2cst->offset < sizeof(i2cst->data)) {
  401. i2cst->operation = OP_SEND;
  402. /*
  403. * In two cases, we save values in the local buffer.
  404. * 1) The first value is always a register.
  405. * 2) In case of non-FIFO multibyte registers, TIS expects full
  406. * register value hence I2C layer cache the register value and send
  407. * to TIS during FINISH event.
  408. */
  409. if ((i2cst->offset == 0) ||
  410. (i2cst->data[0] != TPM_I2C_REG_DATA_FIFO)) {
  411. i2cst->data[i2cst->offset++] = data;
  412. } else {
  413. /*
  414. * The TIS can process FIFO data one byte at a time hence the FIFO
  415. * data is sent to TIS directly.
  416. */
  417. tpm_tis_write_data(&i2cst->state, i2cst->tis_addr, data, 1);
  418. }
  419. return 0;
  420. }
  421. /* Return non-zero to indicate NAK */
  422. return 1;
  423. }
  424. static const Property tpm_tis_i2c_properties[] = {
  425. DEFINE_PROP_TPMBE("tpmdev", TPMStateI2C, state.be_driver),
  426. };
  427. static void tpm_tis_i2c_realizefn(DeviceState *dev, Error **errp)
  428. {
  429. TPMStateI2C *i2cst = TPM_TIS_I2C(dev);
  430. TPMState *s = &i2cst->state;
  431. if (!tpm_find()) {
  432. error_setg(errp, "at most one TPM device is permitted");
  433. return;
  434. }
  435. /*
  436. * Get the backend pointer. It is not initialized properly during
  437. * device_class_set_props
  438. */
  439. s->be_driver = qemu_find_tpm_be("tpm0");
  440. if (!s->be_driver) {
  441. error_setg(errp, "'tpmdev' property is required");
  442. return;
  443. }
  444. }
  445. static void tpm_tis_i2c_reset(DeviceState *dev)
  446. {
  447. TPMStateI2C *i2cst = TPM_TIS_I2C(dev);
  448. TPMState *s = &i2cst->state;
  449. tpm_tis_i2c_clear_data(i2cst);
  450. i2cst->csum_enable = 0;
  451. i2cst->loc_sel = 0x00;
  452. return tpm_tis_reset(s);
  453. }
  454. static void tpm_tis_i2c_class_init(ObjectClass *klass, void *data)
  455. {
  456. DeviceClass *dc = DEVICE_CLASS(klass);
  457. I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
  458. TPMIfClass *tc = TPM_IF_CLASS(klass);
  459. dc->realize = tpm_tis_i2c_realizefn;
  460. device_class_set_legacy_reset(dc, tpm_tis_i2c_reset);
  461. dc->vmsd = &vmstate_tpm_tis_i2c;
  462. device_class_set_props(dc, tpm_tis_i2c_properties);
  463. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  464. k->event = tpm_tis_i2c_event;
  465. k->recv = tpm_tis_i2c_recv;
  466. k->send = tpm_tis_i2c_send;
  467. tc->model = TPM_MODEL_TPM_TIS;
  468. tc->request_completed = tpm_tis_i2c_request_completed;
  469. tc->get_version = tpm_tis_i2c_get_tpm_version;
  470. }
  471. static const TypeInfo tpm_tis_i2c_info = {
  472. .name = TYPE_TPM_TIS_I2C,
  473. .parent = TYPE_I2C_SLAVE,
  474. .instance_size = sizeof(TPMStateI2C),
  475. .class_init = tpm_tis_i2c_class_init,
  476. .interfaces = (InterfaceInfo[]) {
  477. { TYPE_TPM_IF },
  478. { }
  479. }
  480. };
  481. static void tpm_tis_i2c_register_types(void)
  482. {
  483. type_register_static(&tpm_tis_i2c_info);
  484. }
  485. type_init(tpm_tis_i2c_register_types)