]> git.proxmox.com Git - mirror_qemu.git/blame - hw/tpm/tpm_tis.c
tpm_tis: move r/w_offsets to TPMState
[mirror_qemu.git] / hw / tpm / tpm_tis.c
CommitLineData
edff8678
SB
1/*
2 * tpm_tis.c - QEMU's TPM TIS interface emulator
3 *
4 * Copyright (C) 2006,2010-2013 IBM Corporation
5 *
6 * Authors:
7 * Stefan Berger <stefanb@us.ibm.com>
8 * David Safford <safford@us.ibm.com>
9 *
10 * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 *
15 * Implementation of the TIS interface according to specs found at
16 * http://www.trustedcomputinggroup.org. This implementation currently
9dd5c40d 17 * supports version 1.3, 21 March 2013
edff8678
SB
18 * In the developers menu choose the PC Client section then find the TIS
19 * specification.
116694c3
SB
20 *
21 * TPM TIS for TPM 2 implementation following TCG PC Client Platform
22 * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
edff8678
SB
23 */
24
0430891c 25#include "qemu/osdep.h"
732cd587 26#include "hw/isa/isa.h"
da34e65c 27#include "qapi/error.h"
023299d8 28
732cd587 29#include "hw/acpi/tpm.h"
023299d8
MAL
30#include "hw/pci/pci_ids.h"
31#include "sysemu/tpm_backend.h"
32#include "tpm_int.h"
5cf954d0 33#include "tpm_util.h"
732cd587
MAL
34
35#define TPM_TIS_NUM_LOCALITIES 5 /* per spec */
36#define TPM_TIS_LOCALITY_SHIFT 12
37#define TPM_TIS_NO_LOCALITY 0xff
38
39#define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES)
40
41#define TPM_TIS_BUFFER_MAX 4096
42
43typedef enum {
44 TPM_TIS_STATE_IDLE = 0,
45 TPM_TIS_STATE_READY,
46 TPM_TIS_STATE_COMPLETION,
47 TPM_TIS_STATE_EXECUTION,
48 TPM_TIS_STATE_RECEPTION,
49} TPMTISState;
50
51typedef struct TPMSizedBuffer {
52 uint32_t size;
53 uint8_t *buffer;
54} TPMSizedBuffer;
55
56/* locality data -- all fields are persisted */
57typedef struct TPMLocality {
58 TPMTISState state;
59 uint8_t access;
60 uint32_t sts;
61 uint32_t iface_id;
62 uint32_t inte;
63 uint32_t ints;
732cd587
MAL
64} TPMLocality;
65
36e86589 66typedef struct TPMState {
3d4960c7
MAL
67 ISADevice busdev;
68 MemoryRegion mmio;
69
c5496b97 70 unsigned char buffer[TPM_TIS_BUFFER_MAX];
e6b30c71
SB
71 uint16_t w_offset;
72 uint16_t r_offset;
732cd587
MAL
73
74 uint8_t active_locty;
75 uint8_t aborting_locty;
76 uint8_t next_locty;
77
78 TPMLocality loc[TPM_TIS_NUM_LOCALITIES];
79
80 qemu_irq irq;
81 uint32_t irq_num;
732cd587 82
732cd587
MAL
83 TPMBackendCmd cmd;
84
732cd587
MAL
85 TPMBackend *be_driver;
86 TPMVersion be_tpm_version;
b21e6aaf
SB
87
88 size_t be_buffer_size;
36e86589 89} TPMState;
732cd587
MAL
90
91#define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
edff8678 92
4d1ba9c4 93#define DEBUG_TIS 0
edff8678 94
4d1ba9c4
SB
95#define DPRINTF(fmt, ...) do { \
96 if (DEBUG_TIS) { \
97 printf(fmt, ## __VA_ARGS__); \
98 } \
99} while (0);
edff8678 100
edff8678
SB
101/* tis registers */
102#define TPM_TIS_REG_ACCESS 0x00
103#define TPM_TIS_REG_INT_ENABLE 0x08
104#define TPM_TIS_REG_INT_VECTOR 0x0c
105#define TPM_TIS_REG_INT_STATUS 0x10
106#define TPM_TIS_REG_INTF_CAPABILITY 0x14
107#define TPM_TIS_REG_STS 0x18
108#define TPM_TIS_REG_DATA_FIFO 0x24
116694c3 109#define TPM_TIS_REG_INTERFACE_ID 0x30
2eae8c75
SB
110#define TPM_TIS_REG_DATA_XFIFO 0x80
111#define TPM_TIS_REG_DATA_XFIFO_END 0xbc
edff8678
SB
112#define TPM_TIS_REG_DID_VID 0xf00
113#define TPM_TIS_REG_RID 0xf04
114
8db7c415
SB
115/* vendor-specific registers */
116#define TPM_TIS_REG_DEBUG 0xf90
117
116694c3
SB
118#define TPM_TIS_STS_TPM_FAMILY_MASK (0x3 << 26)/* TPM 2.0 */
119#define TPM_TIS_STS_TPM_FAMILY1_2 (0 << 26) /* TPM 2.0 */
120#define TPM_TIS_STS_TPM_FAMILY2_0 (1 << 26) /* TPM 2.0 */
121#define TPM_TIS_STS_RESET_ESTABLISHMENT_BIT (1 << 25) /* TPM 2.0 */
122#define TPM_TIS_STS_COMMAND_CANCEL (1 << 24) /* TPM 2.0 */
123
edff8678
SB
124#define TPM_TIS_STS_VALID (1 << 7)
125#define TPM_TIS_STS_COMMAND_READY (1 << 6)
126#define TPM_TIS_STS_TPM_GO (1 << 5)
127#define TPM_TIS_STS_DATA_AVAILABLE (1 << 4)
128#define TPM_TIS_STS_EXPECT (1 << 3)
fd859081 129#define TPM_TIS_STS_SELFTEST_DONE (1 << 2)
edff8678
SB
130#define TPM_TIS_STS_RESPONSE_RETRY (1 << 1)
131
132#define TPM_TIS_BURST_COUNT_SHIFT 8
133#define TPM_TIS_BURST_COUNT(X) \
134 ((X) << TPM_TIS_BURST_COUNT_SHIFT)
135
136#define TPM_TIS_ACCESS_TPM_REG_VALID_STS (1 << 7)
137#define TPM_TIS_ACCESS_ACTIVE_LOCALITY (1 << 5)
138#define TPM_TIS_ACCESS_BEEN_SEIZED (1 << 4)
139#define TPM_TIS_ACCESS_SEIZE (1 << 3)
140#define TPM_TIS_ACCESS_PENDING_REQUEST (1 << 2)
141#define TPM_TIS_ACCESS_REQUEST_USE (1 << 1)
142#define TPM_TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0)
143
144#define TPM_TIS_INT_ENABLED (1 << 31)
145#define TPM_TIS_INT_DATA_AVAILABLE (1 << 0)
146#define TPM_TIS_INT_STS_VALID (1 << 1)
147#define TPM_TIS_INT_LOCALITY_CHANGED (1 << 2)
148#define TPM_TIS_INT_COMMAND_READY (1 << 7)
149
150#define TPM_TIS_INT_POLARITY_MASK (3 << 3)
151#define TPM_TIS_INT_POLARITY_LOW_LEVEL (1 << 3)
152
edff8678
SB
153#define TPM_TIS_INTERRUPTS_SUPPORTED (TPM_TIS_INT_LOCALITY_CHANGED | \
154 TPM_TIS_INT_DATA_AVAILABLE | \
155 TPM_TIS_INT_STS_VALID | \
156 TPM_TIS_INT_COMMAND_READY)
157
9dd5c40d 158#define TPM_TIS_CAP_INTERFACE_VERSION1_3 (2 << 28)
116694c3 159#define TPM_TIS_CAP_INTERFACE_VERSION1_3_FOR_TPM2_0 (3 << 28)
9dd5c40d
SB
160#define TPM_TIS_CAP_DATA_TRANSFER_64B (3 << 9)
161#define TPM_TIS_CAP_DATA_TRANSFER_LEGACY (0 << 9)
162#define TPM_TIS_CAP_BURST_COUNT_DYNAMIC (0 << 8)
edff8678 163#define TPM_TIS_CAP_INTERRUPT_LOW_LEVEL (1 << 4) /* support is mandatory */
116694c3
SB
164#define TPM_TIS_CAPABILITIES_SUPPORTED1_3 \
165 (TPM_TIS_CAP_INTERRUPT_LOW_LEVEL | \
166 TPM_TIS_CAP_BURST_COUNT_DYNAMIC | \
167 TPM_TIS_CAP_DATA_TRANSFER_64B | \
168 TPM_TIS_CAP_INTERFACE_VERSION1_3 | \
169 TPM_TIS_INTERRUPTS_SUPPORTED)
170
171#define TPM_TIS_CAPABILITIES_SUPPORTED2_0 \
172 (TPM_TIS_CAP_INTERRUPT_LOW_LEVEL | \
173 TPM_TIS_CAP_BURST_COUNT_DYNAMIC | \
174 TPM_TIS_CAP_DATA_TRANSFER_64B | \
175 TPM_TIS_CAP_INTERFACE_VERSION1_3_FOR_TPM2_0 | \
176 TPM_TIS_INTERRUPTS_SUPPORTED)
177
178#define TPM_TIS_IFACE_ID_INTERFACE_TIS1_3 (0xf) /* TPM 2.0 */
179#define TPM_TIS_IFACE_ID_INTERFACE_FIFO (0x0) /* TPM 2.0 */
180#define TPM_TIS_IFACE_ID_INTERFACE_VER_FIFO (0 << 4) /* TPM 2.0 */
181#define TPM_TIS_IFACE_ID_CAP_5_LOCALITIES (1 << 8) /* TPM 2.0 */
182#define TPM_TIS_IFACE_ID_CAP_TIS_SUPPORTED (1 << 13) /* TPM 2.0 */
183#define TPM_TIS_IFACE_ID_INT_SEL_LOCK (1 << 19) /* TPM 2.0 */
184
185#define TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3 \
186 (TPM_TIS_IFACE_ID_INTERFACE_TIS1_3 | \
886ce6f8 187 (~0u << 4)/* all of it is don't care */)
116694c3
SB
188
189/* if backend was a TPM 2.0: */
190#define TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0 \
191 (TPM_TIS_IFACE_ID_INTERFACE_FIFO | \
192 TPM_TIS_IFACE_ID_INTERFACE_VER_FIFO | \
193 TPM_TIS_IFACE_ID_CAP_5_LOCALITIES | \
194 TPM_TIS_IFACE_ID_CAP_TIS_SUPPORTED)
edff8678
SB
195
196#define TPM_TIS_TPM_DID 0x0001
197#define TPM_TIS_TPM_VID PCI_VENDOR_ID_IBM
198#define TPM_TIS_TPM_RID 0x0001
199
200#define TPM_TIS_NO_DATA_BYTE 0xff
201
8db7c415
SB
202/* local prototypes */
203
204static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
205 unsigned size);
206
edff8678
SB
207/* utility functions */
208
209static uint8_t tpm_tis_locality_from_addr(hwaddr addr)
210{
211 return (uint8_t)((addr >> TPM_TIS_LOCALITY_SHIFT) & 0x7);
212}
213
e6b703f6
SB
214static void tpm_tis_show_buffer(const unsigned char *buffer,
215 size_t buffer_size, const char *string)
edff8678
SB
216{
217#ifdef DEBUG_TIS
218 uint32_t len, i;
219
e6b703f6 220 len = MIN(tpm_cmd_get_size(buffer), buffer_size);
edff8678
SB
221 DPRINTF("tpm_tis: %s length = %d\n", string, len);
222 for (i = 0; i < len; i++) {
223 if (i && !(i % 16)) {
224 DPRINTF("\n");
225 }
e6b703f6 226 DPRINTF("%.2X ", buffer[i]);
edff8678
SB
227 }
228 DPRINTF("\n");
229#endif
230}
231
fd859081
SB
232/*
233 * Set the given flags in the STS register by clearing the register but
116694c3
SB
234 * preserving the SELFTEST_DONE and TPM_FAMILY_MASK flags and then setting
235 * the new flags.
fd859081
SB
236 *
237 * The SELFTEST_DONE flag is acquired from the backend that determines it by
238 * peeking into TPM commands.
239 *
240 * A VM suspend/resume will preserve the flag by storing it into the VM
241 * device state, but the backend will not remember it when QEMU is started
242 * again. Therefore, we cache the flag here. Once set, it will not be unset
243 * except by a reset.
244 */
245static void tpm_tis_sts_set(TPMLocality *l, uint32_t flags)
246{
116694c3 247 l->sts &= TPM_TIS_STS_SELFTEST_DONE | TPM_TIS_STS_TPM_FAMILY_MASK;
fd859081
SB
248 l->sts |= flags;
249}
250
edff8678
SB
251/*
252 * Send a request to the TPM.
253 */
254static void tpm_tis_tpm_send(TPMState *s, uint8_t locty)
255{
c5496b97 256 tpm_tis_show_buffer(s->buffer, s->be_buffer_size,
e6b703f6 257 "tpm_tis: To TPM");
edff8678 258
edff8678
SB
259 /*
260 * w_offset serves as length indicator for length of data;
261 * it's reset when the response comes back
262 */
3d4960c7 263 s->loc[locty].state = TPM_TIS_STATE_EXECUTION;
edff8678 264
0e43b7e6
MAL
265 s->cmd = (TPMBackendCmd) {
266 .locty = locty,
c5496b97 267 .in = s->buffer,
e6b30c71 268 .in_len = s->w_offset,
c5496b97 269 .out = s->buffer,
e6b703f6 270 .out_len = s->be_buffer_size,
0e43b7e6
MAL
271 };
272
273 tpm_backend_deliver_request(s->be_driver, &s->cmd);
edff8678
SB
274}
275
276/* raise an interrupt if allowed */
277static void tpm_tis_raise_irq(TPMState *s, uint8_t locty, uint32_t irqmask)
278{
edff8678
SB
279 if (!TPM_TIS_IS_VALID_LOCTY(locty)) {
280 return;
281 }
282
3d4960c7
MAL
283 if ((s->loc[locty].inte & TPM_TIS_INT_ENABLED) &&
284 (s->loc[locty].inte & irqmask)) {
edff8678 285 DPRINTF("tpm_tis: Raising IRQ for flag %08x\n", irqmask);
3d4960c7
MAL
286 qemu_irq_raise(s->irq);
287 s->loc[locty].ints |= irqmask;
edff8678
SB
288 }
289}
290
291static uint32_t tpm_tis_check_request_use_except(TPMState *s, uint8_t locty)
292{
293 uint8_t l;
294
295 for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) {
296 if (l == locty) {
297 continue;
298 }
3d4960c7 299 if ((s->loc[l].access & TPM_TIS_ACCESS_REQUEST_USE)) {
edff8678
SB
300 return 1;
301 }
302 }
303
304 return 0;
305}
306
307static void tpm_tis_new_active_locality(TPMState *s, uint8_t new_active_locty)
308{
3d4960c7 309 bool change = (s->active_locty != new_active_locty);
edff8678
SB
310 bool is_seize;
311 uint8_t mask;
312
3d4960c7 313 if (change && TPM_TIS_IS_VALID_LOCTY(s->active_locty)) {
edff8678 314 is_seize = TPM_TIS_IS_VALID_LOCTY(new_active_locty) &&
3d4960c7 315 s->loc[new_active_locty].access & TPM_TIS_ACCESS_SEIZE;
edff8678
SB
316
317 if (is_seize) {
318 mask = ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY);
319 } else {
320 mask = ~(TPM_TIS_ACCESS_ACTIVE_LOCALITY|
321 TPM_TIS_ACCESS_REQUEST_USE);
322 }
323 /* reset flags on the old active locality */
3d4960c7 324 s->loc[s->active_locty].access &= mask;
edff8678
SB
325
326 if (is_seize) {
3d4960c7 327 s->loc[s->active_locty].access |= TPM_TIS_ACCESS_BEEN_SEIZED;
edff8678
SB
328 }
329 }
330
3d4960c7 331 s->active_locty = new_active_locty;
edff8678 332
3d4960c7 333 DPRINTF("tpm_tis: Active locality is now %d\n", s->active_locty);
edff8678
SB
334
335 if (TPM_TIS_IS_VALID_LOCTY(new_active_locty)) {
336 /* set flags on the new active locality */
3d4960c7
MAL
337 s->loc[new_active_locty].access |= TPM_TIS_ACCESS_ACTIVE_LOCALITY;
338 s->loc[new_active_locty].access &= ~(TPM_TIS_ACCESS_REQUEST_USE |
edff8678
SB
339 TPM_TIS_ACCESS_SEIZE);
340 }
341
342 if (change) {
3d4960c7 343 tpm_tis_raise_irq(s, s->active_locty, TPM_TIS_INT_LOCALITY_CHANGED);
edff8678
SB
344 }
345}
346
347/* abort -- this function switches the locality */
348static void tpm_tis_abort(TPMState *s, uint8_t locty)
349{
e6b30c71
SB
350 s->r_offset = 0;
351 s->w_offset = 0;
edff8678 352
3d4960c7 353 DPRINTF("tpm_tis: tis_abort: new active locality is %d\n", s->next_locty);
edff8678
SB
354
355 /*
356 * Need to react differently depending on who's aborting now and
357 * which locality will become active afterwards.
358 */
3d4960c7
MAL
359 if (s->aborting_locty == s->next_locty) {
360 s->loc[s->aborting_locty].state = TPM_TIS_STATE_READY;
361 tpm_tis_sts_set(&s->loc[s->aborting_locty],
fd859081 362 TPM_TIS_STS_COMMAND_READY);
3d4960c7 363 tpm_tis_raise_irq(s, s->aborting_locty, TPM_TIS_INT_COMMAND_READY);
edff8678
SB
364 }
365
366 /* locality after abort is another one than the current one */
3d4960c7 367 tpm_tis_new_active_locality(s, s->next_locty);
edff8678 368
3d4960c7 369 s->next_locty = TPM_TIS_NO_LOCALITY;
edff8678 370 /* nobody's aborting a command anymore */
3d4960c7 371 s->aborting_locty = TPM_TIS_NO_LOCALITY;
edff8678
SB
372}
373
374/* prepare aborting current command */
375static void tpm_tis_prep_abort(TPMState *s, uint8_t locty, uint8_t newlocty)
376{
edff8678
SB
377 uint8_t busy_locty;
378
3d4960c7
MAL
379 s->aborting_locty = locty;
380 s->next_locty = newlocty; /* locality after successful abort */
edff8678
SB
381
382 /*
383 * only abort a command using an interrupt if currently executing
384 * a command AND if there's a valid connection to the vTPM.
385 */
386 for (busy_locty = 0; busy_locty < TPM_TIS_NUM_LOCALITIES; busy_locty++) {
3d4960c7 387 if (s->loc[busy_locty].state == TPM_TIS_STATE_EXECUTION) {
edff8678
SB
388 /*
389 * request the backend to cancel. Some backends may not
390 * support it
391 */
8f0605cc 392 tpm_backend_cancel_cmd(s->be_driver);
edff8678
SB
393 return;
394 }
395 }
396
397 tpm_tis_abort(s, locty);
398}
399
68999059
MAL
400/*
401 * Callback from the TPM to indicate that the response was received.
402 */
403static void tpm_tis_request_completed(TPMIf *ti)
edff8678 404{
68999059 405 TPMState *s = TPM(ti);
0e43b7e6 406 uint8_t locty = s->cmd.locty;
68999059
MAL
407 uint8_t l;
408
409 if (s->cmd.selftest_done) {
410 for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) {
411 s->loc[locty].sts |= TPM_TIS_STS_SELFTEST_DONE;
412 }
413 }
edff8678 414
3d4960c7 415 tpm_tis_sts_set(&s->loc[locty],
fd859081 416 TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE);
3d4960c7 417 s->loc[locty].state = TPM_TIS_STATE_COMPLETION;
e6b30c71
SB
418 s->r_offset = 0;
419 s->w_offset = 0;
edff8678 420
c5496b97 421 tpm_tis_show_buffer(s->buffer, s->be_buffer_size,
e6b703f6 422 "tpm_tis: From TPM");
298d8b81 423
3d4960c7 424 if (TPM_TIS_IS_VALID_LOCTY(s->next_locty)) {
edff8678
SB
425 tpm_tis_abort(s, locty);
426 }
427
edff8678
SB
428 tpm_tis_raise_irq(s, locty,
429 TPM_TIS_INT_DATA_AVAILABLE | TPM_TIS_INT_STS_VALID);
edff8678
SB
430}
431
edff8678
SB
432/*
433 * Read a byte of response data
434 */
435static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
436{
edff8678
SB
437 uint32_t ret = TPM_TIS_NO_DATA_BYTE;
438 uint16_t len;
439
3d4960c7 440 if ((s->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) {
c5496b97 441 len = MIN(tpm_cmd_get_size(&s->buffer),
e6b703f6 442 s->be_buffer_size);
edff8678 443
e6b30c71
SB
444 ret = s->buffer[s->r_offset++];
445 if (s->r_offset >= len) {
edff8678 446 /* got last byte */
3d4960c7 447 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
edff8678 448 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID);
edff8678
SB
449 }
450 DPRINTF("tpm_tis: tpm_tis_data_read byte 0x%02x [%d]\n",
e6b30c71 451 ret, s->r_offset - 1);
edff8678
SB
452 }
453
454 return ret;
455}
456
8db7c415
SB
457#ifdef DEBUG_TIS
458static void tpm_tis_dump_state(void *opaque, hwaddr addr)
459{
460 static const unsigned regs[] = {
461 TPM_TIS_REG_ACCESS,
462 TPM_TIS_REG_INT_ENABLE,
463 TPM_TIS_REG_INT_VECTOR,
464 TPM_TIS_REG_INT_STATUS,
465 TPM_TIS_REG_INTF_CAPABILITY,
466 TPM_TIS_REG_STS,
467 TPM_TIS_REG_DID_VID,
468 TPM_TIS_REG_RID,
469 0xfff};
470 int idx;
471 uint8_t locty = tpm_tis_locality_from_addr(addr);
472 hwaddr base = addr & ~0xfff;
473 TPMState *s = opaque;
8db7c415
SB
474
475 DPRINTF("tpm_tis: active locality : %d\n"
476 "tpm_tis: state of locality %d : %d\n"
477 "tpm_tis: register dump:\n",
3d4960c7
MAL
478 s->active_locty,
479 locty, s->loc[locty].state);
8db7c415
SB
480
481 for (idx = 0; regs[idx] != 0xfff; idx++) {
482 DPRINTF("tpm_tis: 0x%04x : 0x%08x\n", regs[idx],
070c7607 483 (int)tpm_tis_mmio_read(opaque, base + regs[idx], 4));
8db7c415
SB
484 }
485
486 DPRINTF("tpm_tis: read offset : %d\n"
487 "tpm_tis: result buffer : ",
e6b30c71 488 s->r_offset);
8db7c415 489 for (idx = 0;
c5496b97 490 idx < MIN(tpm_cmd_get_size(&s->buffer), s->be_buffer_size);
8db7c415
SB
491 idx++) {
492 DPRINTF("%c%02x%s",
e6b30c71 493 s->r_offset == idx ? '>' : ' ',
c5496b97 494 s->buffer[idx],
8db7c415
SB
495 ((idx & 0xf) == 0xf) ? "\ntpm_tis: " : "");
496 }
497 DPRINTF("\n"
498 "tpm_tis: write offset : %d\n"
499 "tpm_tis: request buffer: ",
e6b30c71 500 s->w_offset);
8db7c415 501 for (idx = 0;
c5496b97 502 idx < MIN(tpm_cmd_get_size(s->buffer), s->be_buffer_size);
8db7c415
SB
503 idx++) {
504 DPRINTF("%c%02x%s",
e6b30c71 505 s->w_offset == idx ? '>' : ' ',
c5496b97 506 s->buffer[idx],
8db7c415
SB
507 ((idx & 0xf) == 0xf) ? "\ntpm_tis: " : "");
508 }
509 DPRINTF("\n");
510}
511#endif
512
edff8678
SB
513/*
514 * Read a register of the TIS interface
515 * See specs pages 33-63 for description of the registers
516 */
517static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
518 unsigned size)
519{
520 TPMState *s = opaque;
edff8678
SB
521 uint16_t offset = addr & 0xffc;
522 uint8_t shift = (addr & 0x3) * 8;
523 uint32_t val = 0xffffffff;
524 uint8_t locty = tpm_tis_locality_from_addr(addr);
525 uint32_t avail;
feeb755f 526 uint8_t v;
edff8678 527
8f0605cc 528 if (tpm_backend_had_startup_error(s->be_driver)) {
6cd65969 529 return 0;
edff8678
SB
530 }
531
532 switch (offset) {
533 case TPM_TIS_REG_ACCESS:
534 /* never show the SEIZE flag even though we use it internally */
3d4960c7 535 val = s->loc[locty].access & ~TPM_TIS_ACCESS_SEIZE;
edff8678
SB
536 /* the pending flag is always calculated */
537 if (tpm_tis_check_request_use_except(s, locty)) {
538 val |= TPM_TIS_ACCESS_PENDING_REQUEST;
539 }
8f0605cc 540 val |= !tpm_backend_get_tpm_established_flag(s->be_driver);
edff8678
SB
541 break;
542 case TPM_TIS_REG_INT_ENABLE:
3d4960c7 543 val = s->loc[locty].inte;
edff8678
SB
544 break;
545 case TPM_TIS_REG_INT_VECTOR:
3d4960c7 546 val = s->irq_num;
edff8678
SB
547 break;
548 case TPM_TIS_REG_INT_STATUS:
3d4960c7 549 val = s->loc[locty].ints;
edff8678
SB
550 break;
551 case TPM_TIS_REG_INTF_CAPABILITY:
116694c3
SB
552 switch (s->be_tpm_version) {
553 case TPM_VERSION_UNSPEC:
554 val = 0;
555 break;
556 case TPM_VERSION_1_2:
557 val = TPM_TIS_CAPABILITIES_SUPPORTED1_3;
558 break;
559 case TPM_VERSION_2_0:
560 val = TPM_TIS_CAPABILITIES_SUPPORTED2_0;
561 break;
562 }
edff8678
SB
563 break;
564 case TPM_TIS_REG_STS:
3d4960c7
MAL
565 if (s->active_locty == locty) {
566 if ((s->loc[locty].sts & TPM_TIS_STS_DATA_AVAILABLE)) {
edff8678 567 val = TPM_TIS_BURST_COUNT(
c5496b97 568 MIN(tpm_cmd_get_size(&s->buffer),
e6b703f6 569 s->be_buffer_size)
e6b30c71 570 - s->r_offset) | s->loc[locty].sts;
edff8678 571 } else {
e6b30c71 572 avail = s->be_buffer_size - s->w_offset;
edff8678
SB
573 /*
574 * byte-sized reads should not return 0x00 for 0x100
575 * available bytes.
576 */
577 if (size == 1 && avail > 0xff) {
578 avail = 0xff;
579 }
3d4960c7 580 val = TPM_TIS_BURST_COUNT(avail) | s->loc[locty].sts;
edff8678
SB
581 }
582 }
583 break;
584 case TPM_TIS_REG_DATA_FIFO:
2eae8c75 585 case TPM_TIS_REG_DATA_XFIFO ... TPM_TIS_REG_DATA_XFIFO_END:
3d4960c7 586 if (s->active_locty == locty) {
feeb755f
SB
587 if (size > 4 - (addr & 0x3)) {
588 /* prevent access beyond FIFO */
589 size = 4 - (addr & 0x3);
590 }
591 val = 0;
592 shift = 0;
593 while (size > 0) {
3d4960c7 594 switch (s->loc[locty].state) {
feeb755f
SB
595 case TPM_TIS_STATE_COMPLETION:
596 v = tpm_tis_data_read(s, locty);
597 break;
598 default:
599 v = TPM_TIS_NO_DATA_BYTE;
600 break;
601 }
602 val |= (v << shift);
603 shift += 8;
604 size--;
edff8678 605 }
feeb755f 606 shift = 0; /* no more adjustments */
edff8678
SB
607 }
608 break;
116694c3 609 case TPM_TIS_REG_INTERFACE_ID:
3d4960c7 610 val = s->loc[locty].iface_id;
116694c3 611 break;
edff8678
SB
612 case TPM_TIS_REG_DID_VID:
613 val = (TPM_TIS_TPM_DID << 16) | TPM_TIS_TPM_VID;
614 break;
615 case TPM_TIS_REG_RID:
616 val = TPM_TIS_TPM_RID;
617 break;
8db7c415
SB
618#ifdef DEBUG_TIS
619 case TPM_TIS_REG_DEBUG:
620 tpm_tis_dump_state(opaque, addr);
621 break;
622#endif
edff8678
SB
623 }
624
625 if (shift) {
626 val >>= shift;
627 }
628
070c7607 629 DPRINTF("tpm_tis: read.%u(%08x) = %08x\n", size, (int)addr, (int)val);
edff8678
SB
630
631 return val;
632}
633
634/*
635 * Write a value to a register of the TIS interface
636 * See specs pages 33-63 for description of the registers
637 */
ff2bc0c1
MAL
638static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
639 uint64_t val, unsigned size)
edff8678
SB
640{
641 TPMState *s = opaque;
feeb755f
SB
642 uint16_t off = addr & 0xffc;
643 uint8_t shift = (addr & 0x3) * 8;
edff8678
SB
644 uint8_t locty = tpm_tis_locality_from_addr(addr);
645 uint8_t active_locty, l;
646 int c, set_new_locty = 1;
647 uint16_t len;
feeb755f 648 uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0);
edff8678 649
070c7607 650 DPRINTF("tpm_tis: write.%u(%08x) = %08x\n", size, (int)addr, (int)val);
edff8678 651
ff2bc0c1 652 if (locty == 4) {
edff8678
SB
653 DPRINTF("tpm_tis: Access to locality 4 only allowed from hardware\n");
654 return;
655 }
656
8f0605cc 657 if (tpm_backend_had_startup_error(s->be_driver)) {
edff8678
SB
658 return;
659 }
660
feeb755f
SB
661 val &= mask;
662
663 if (shift) {
664 val <<= shift;
665 mask <<= shift;
666 }
667
668 mask ^= 0xffffffff;
669
edff8678
SB
670 switch (off) {
671 case TPM_TIS_REG_ACCESS:
672
673 if ((val & TPM_TIS_ACCESS_SEIZE)) {
674 val &= ~(TPM_TIS_ACCESS_REQUEST_USE |
675 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
676 }
677
3d4960c7 678 active_locty = s->active_locty;
edff8678
SB
679
680 if ((val & TPM_TIS_ACCESS_ACTIVE_LOCALITY)) {
681 /* give up locality if currently owned */
3d4960c7 682 if (s->active_locty == locty) {
edff8678
SB
683 DPRINTF("tpm_tis: Releasing locality %d\n", locty);
684
685 uint8_t newlocty = TPM_TIS_NO_LOCALITY;
686 /* anybody wants the locality ? */
687 for (c = TPM_TIS_NUM_LOCALITIES - 1; c >= 0; c--) {
3d4960c7 688 if ((s->loc[c].access & TPM_TIS_ACCESS_REQUEST_USE)) {
edff8678
SB
689 DPRINTF("tpm_tis: Locality %d requests use.\n", c);
690 newlocty = c;
691 break;
692 }
693 }
694 DPRINTF("tpm_tis: TPM_TIS_ACCESS_ACTIVE_LOCALITY: "
695 "Next active locality: %d\n",
696 newlocty);
697
698 if (TPM_TIS_IS_VALID_LOCTY(newlocty)) {
699 set_new_locty = 0;
700 tpm_tis_prep_abort(s, locty, newlocty);
701 } else {
702 active_locty = TPM_TIS_NO_LOCALITY;
703 }
704 } else {
705 /* not currently the owner; clear a pending request */
3d4960c7 706 s->loc[locty].access &= ~TPM_TIS_ACCESS_REQUEST_USE;
edff8678
SB
707 }
708 }
709
710 if ((val & TPM_TIS_ACCESS_BEEN_SEIZED)) {
3d4960c7 711 s->loc[locty].access &= ~TPM_TIS_ACCESS_BEEN_SEIZED;
edff8678
SB
712 }
713
714 if ((val & TPM_TIS_ACCESS_SEIZE)) {
715 /*
716 * allow seize if a locality is active and the requesting
717 * locality is higher than the one that's active
718 * OR
719 * allow seize for requesting locality if no locality is
720 * active
721 */
3d4960c7
MAL
722 while ((TPM_TIS_IS_VALID_LOCTY(s->active_locty) &&
723 locty > s->active_locty) ||
724 !TPM_TIS_IS_VALID_LOCTY(s->active_locty)) {
edff8678
SB
725 bool higher_seize = FALSE;
726
727 /* already a pending SEIZE ? */
3d4960c7 728 if ((s->loc[locty].access & TPM_TIS_ACCESS_SEIZE)) {
edff8678
SB
729 break;
730 }
731
732 /* check for ongoing seize by a higher locality */
733 for (l = locty + 1; l < TPM_TIS_NUM_LOCALITIES; l++) {
3d4960c7 734 if ((s->loc[l].access & TPM_TIS_ACCESS_SEIZE)) {
edff8678
SB
735 higher_seize = TRUE;
736 break;
737 }
738 }
739
740 if (higher_seize) {
741 break;
742 }
743
744 /* cancel any seize by a lower locality */
745 for (l = 0; l < locty - 1; l++) {
3d4960c7 746 s->loc[l].access &= ~TPM_TIS_ACCESS_SEIZE;
edff8678
SB
747 }
748
3d4960c7 749 s->loc[locty].access |= TPM_TIS_ACCESS_SEIZE;
edff8678
SB
750 DPRINTF("tpm_tis: TPM_TIS_ACCESS_SEIZE: "
751 "Locality %d seized from locality %d\n",
3d4960c7 752 locty, s->active_locty);
edff8678
SB
753 DPRINTF("tpm_tis: TPM_TIS_ACCESS_SEIZE: Initiating abort.\n");
754 set_new_locty = 0;
3d4960c7 755 tpm_tis_prep_abort(s, s->active_locty, locty);
edff8678
SB
756 break;
757 }
758 }
759
760 if ((val & TPM_TIS_ACCESS_REQUEST_USE)) {
3d4960c7
MAL
761 if (s->active_locty != locty) {
762 if (TPM_TIS_IS_VALID_LOCTY(s->active_locty)) {
763 s->loc[locty].access |= TPM_TIS_ACCESS_REQUEST_USE;
edff8678
SB
764 } else {
765 /* no locality active -> make this one active now */
766 active_locty = locty;
767 }
768 }
769 }
770
771 if (set_new_locty) {
772 tpm_tis_new_active_locality(s, active_locty);
773 }
774
775 break;
776 case TPM_TIS_REG_INT_ENABLE:
3d4960c7 777 if (s->active_locty != locty) {
edff8678
SB
778 break;
779 }
780
3d4960c7
MAL
781 s->loc[locty].inte &= mask;
782 s->loc[locty].inte |= (val & (TPM_TIS_INT_ENABLED |
feeb755f
SB
783 TPM_TIS_INT_POLARITY_MASK |
784 TPM_TIS_INTERRUPTS_SUPPORTED));
edff8678
SB
785 break;
786 case TPM_TIS_REG_INT_VECTOR:
787 /* hard wired -- ignore */
788 break;
789 case TPM_TIS_REG_INT_STATUS:
3d4960c7 790 if (s->active_locty != locty) {
edff8678
SB
791 break;
792 }
793
794 /* clearing of interrupt flags */
795 if (((val & TPM_TIS_INTERRUPTS_SUPPORTED)) &&
3d4960c7
MAL
796 (s->loc[locty].ints & TPM_TIS_INTERRUPTS_SUPPORTED)) {
797 s->loc[locty].ints &= ~val;
798 if (s->loc[locty].ints == 0) {
799 qemu_irq_lower(s->irq);
edff8678
SB
800 DPRINTF("tpm_tis: Lowering IRQ\n");
801 }
802 }
3d4960c7 803 s->loc[locty].ints &= ~(val & TPM_TIS_INTERRUPTS_SUPPORTED);
edff8678
SB
804 break;
805 case TPM_TIS_REG_STS:
3d4960c7 806 if (s->active_locty != locty) {
edff8678
SB
807 break;
808 }
809
116694c3
SB
810 if (s->be_tpm_version == TPM_VERSION_2_0) {
811 /* some flags that are only supported for TPM 2 */
812 if (val & TPM_TIS_STS_COMMAND_CANCEL) {
3d4960c7 813 if (s->loc[locty].state == TPM_TIS_STATE_EXECUTION) {
116694c3
SB
814 /*
815 * request the backend to cancel. Some backends may not
816 * support it
817 */
818 tpm_backend_cancel_cmd(s->be_driver);
819 }
820 }
821
822 if (val & TPM_TIS_STS_RESET_ESTABLISHMENT_BIT) {
823 if (locty == 3 || locty == 4) {
824 tpm_backend_reset_tpm_established_flag(s->be_driver, locty);
825 }
826 }
827 }
828
edff8678
SB
829 val &= (TPM_TIS_STS_COMMAND_READY | TPM_TIS_STS_TPM_GO |
830 TPM_TIS_STS_RESPONSE_RETRY);
831
832 if (val == TPM_TIS_STS_COMMAND_READY) {
3d4960c7 833 switch (s->loc[locty].state) {
edff8678
SB
834
835 case TPM_TIS_STATE_READY:
e6b30c71
SB
836 s->w_offset = 0;
837 s->r_offset = 0;
edff8678
SB
838 break;
839
840 case TPM_TIS_STATE_IDLE:
3d4960c7
MAL
841 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_COMMAND_READY);
842 s->loc[locty].state = TPM_TIS_STATE_READY;
edff8678
SB
843 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_COMMAND_READY);
844 break;
845
846 case TPM_TIS_STATE_EXECUTION:
847 case TPM_TIS_STATE_RECEPTION:
848 /* abort currently running command */
849 DPRINTF("tpm_tis: %s: Initiating abort.\n",
850 __func__);
851 tpm_tis_prep_abort(s, locty, locty);
852 break;
853
854 case TPM_TIS_STATE_COMPLETION:
e6b30c71
SB
855 s->w_offset = 0;
856 s->r_offset = 0;
edff8678 857 /* shortcut to ready state with C/R set */
3d4960c7
MAL
858 s->loc[locty].state = TPM_TIS_STATE_READY;
859 if (!(s->loc[locty].sts & TPM_TIS_STS_COMMAND_READY)) {
860 tpm_tis_sts_set(&s->loc[locty],
fd859081 861 TPM_TIS_STS_COMMAND_READY);
edff8678
SB
862 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_COMMAND_READY);
863 }
3d4960c7 864 s->loc[locty].sts &= ~(TPM_TIS_STS_DATA_AVAILABLE);
edff8678
SB
865 break;
866
867 }
868 } else if (val == TPM_TIS_STS_TPM_GO) {
3d4960c7 869 switch (s->loc[locty].state) {
edff8678 870 case TPM_TIS_STATE_RECEPTION:
3d4960c7 871 if ((s->loc[locty].sts & TPM_TIS_STS_EXPECT) == 0) {
edff8678
SB
872 tpm_tis_tpm_send(s, locty);
873 }
874 break;
875 default:
876 /* ignore */
877 break;
878 }
879 } else if (val == TPM_TIS_STS_RESPONSE_RETRY) {
3d4960c7 880 switch (s->loc[locty].state) {
edff8678 881 case TPM_TIS_STATE_COMPLETION:
e6b30c71 882 s->r_offset = 0;
3d4960c7 883 tpm_tis_sts_set(&s->loc[locty],
fd859081
SB
884 TPM_TIS_STS_VALID|
885 TPM_TIS_STS_DATA_AVAILABLE);
edff8678
SB
886 break;
887 default:
888 /* ignore */
889 break;
890 }
891 }
892 break;
893 case TPM_TIS_REG_DATA_FIFO:
2eae8c75 894 case TPM_TIS_REG_DATA_XFIFO ... TPM_TIS_REG_DATA_XFIFO_END:
edff8678 895 /* data fifo */
3d4960c7 896 if (s->active_locty != locty) {
edff8678
SB
897 break;
898 }
899
3d4960c7
MAL
900 if (s->loc[locty].state == TPM_TIS_STATE_IDLE ||
901 s->loc[locty].state == TPM_TIS_STATE_EXECUTION ||
902 s->loc[locty].state == TPM_TIS_STATE_COMPLETION) {
edff8678
SB
903 /* drop the byte */
904 } else {
feeb755f 905 DPRINTF("tpm_tis: Data to send to TPM: %08x (size=%d)\n",
070c7607 906 (int)val, size);
3d4960c7
MAL
907 if (s->loc[locty].state == TPM_TIS_STATE_READY) {
908 s->loc[locty].state = TPM_TIS_STATE_RECEPTION;
909 tpm_tis_sts_set(&s->loc[locty],
fd859081 910 TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID);
edff8678
SB
911 }
912
feeb755f
SB
913 val >>= shift;
914 if (size > 4 - (addr & 0x3)) {
915 /* prevent access beyond FIFO */
916 size = 4 - (addr & 0x3);
917 }
918
3d4960c7 919 while ((s->loc[locty].sts & TPM_TIS_STS_EXPECT) && size > 0) {
e6b30c71
SB
920 if (s->w_offset < s->be_buffer_size) {
921 s->buffer[s->w_offset++] =
e6b703f6 922 (uint8_t)val;
feeb755f
SB
923 val >>= 8;
924 size--;
edff8678 925 } else {
3d4960c7 926 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
edff8678
SB
927 }
928 }
929
930 /* check for complete packet */
e6b30c71 931 if (s->w_offset > 5 &&
3d4960c7 932 (s->loc[locty].sts & TPM_TIS_STS_EXPECT)) {
edff8678 933 /* we have a packet length - see if we have all of it */
3d4960c7 934 bool need_irq = !(s->loc[locty].sts & TPM_TIS_STS_VALID);
d8383d61 935
c5496b97 936 len = tpm_cmd_get_size(&s->buffer);
e6b30c71 937 if (len > s->w_offset) {
3d4960c7 938 tpm_tis_sts_set(&s->loc[locty],
fd859081 939 TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID);
edff8678
SB
940 } else {
941 /* packet complete */
3d4960c7 942 tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
edff8678 943 }
29b558d8 944 if (need_irq) {
edff8678
SB
945 tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID);
946 }
edff8678
SB
947 }
948 }
949 break;
116694c3
SB
950 case TPM_TIS_REG_INTERFACE_ID:
951 if (val & TPM_TIS_IFACE_ID_INT_SEL_LOCK) {
952 for (l = 0; l < TPM_TIS_NUM_LOCALITIES; l++) {
3d4960c7 953 s->loc[l].iface_id |= TPM_TIS_IFACE_ID_INT_SEL_LOCK;
116694c3
SB
954 }
955 }
956 break;
edff8678
SB
957 }
958}
959
edff8678
SB
960static const MemoryRegionOps tpm_tis_memory_ops = {
961 .read = tpm_tis_mmio_read,
962 .write = tpm_tis_mmio_write,
963 .endianness = DEVICE_LITTLE_ENDIAN,
964 .valid = {
965 .min_access_size = 1,
966 .max_access_size = 4,
967 },
968};
969
8a2306c7 970static int tpm_tis_do_startup_tpm(TPMState *s, size_t buffersize)
edff8678 971{
9375c44f 972 return tpm_backend_startup_tpm(s->be_driver, buffersize);
edff8678
SB
973}
974
5cb18b3d
SB
975/*
976 * Get the TPMVersion of the backend device being used
977 */
9af7a721 978static enum TPMVersion tpm_tis_get_tpm_version(TPMIf *ti)
5cb18b3d 979{
9af7a721 980 TPMState *s = TPM(ti);
5cb18b3d 981
ad4aca69
SB
982 if (tpm_backend_had_startup_error(s->be_driver)) {
983 return TPM_VERSION_UNSPEC;
984 }
985
5cb18b3d
SB
986 return tpm_backend_get_tpm_version(s->be_driver);
987}
988
edff8678
SB
989/*
990 * This function is called when the machine starts, resets or due to
991 * S3 resume.
992 */
993static void tpm_tis_reset(DeviceState *dev)
994{
995 TPMState *s = TPM(dev);
edff8678
SB
996 int c;
997
116694c3 998 s->be_tpm_version = tpm_backend_get_tpm_version(s->be_driver);
1af3d63e
SB
999 s->be_buffer_size = MIN(tpm_backend_get_buffer_size(s->be_driver),
1000 TPM_TIS_BUFFER_MAX);
116694c3 1001
8f0605cc 1002 tpm_backend_reset(s->be_driver);
edff8678 1003
3d4960c7
MAL
1004 s->active_locty = TPM_TIS_NO_LOCALITY;
1005 s->next_locty = TPM_TIS_NO_LOCALITY;
1006 s->aborting_locty = TPM_TIS_NO_LOCALITY;
edff8678
SB
1007
1008 for (c = 0; c < TPM_TIS_NUM_LOCALITIES; c++) {
3d4960c7 1009 s->loc[c].access = TPM_TIS_ACCESS_TPM_REG_VALID_STS;
116694c3
SB
1010 switch (s->be_tpm_version) {
1011 case TPM_VERSION_UNSPEC:
1012 break;
1013 case TPM_VERSION_1_2:
3d4960c7
MAL
1014 s->loc[c].sts = TPM_TIS_STS_TPM_FAMILY1_2;
1015 s->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3;
116694c3
SB
1016 break;
1017 case TPM_VERSION_2_0:
3d4960c7
MAL
1018 s->loc[c].sts = TPM_TIS_STS_TPM_FAMILY2_0;
1019 s->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0;
116694c3
SB
1020 break;
1021 }
3d4960c7
MAL
1022 s->loc[c].inte = TPM_TIS_INT_POLARITY_LOW_LEVEL;
1023 s->loc[c].ints = 0;
1024 s->loc[c].state = TPM_TIS_STATE_IDLE;
1025
e6b30c71
SB
1026 s->w_offset = 0;
1027 s->r_offset = 0;
edff8678
SB
1028 }
1029
1af3d63e 1030 tpm_tis_do_startup_tpm(s, s->be_buffer_size);
edff8678
SB
1031}
1032
1033static const VMStateDescription vmstate_tpm_tis = {
1034 .name = "tpm",
1035 .unmigratable = 1,
1036};
1037
1038static Property tpm_tis_properties[] = {
3d4960c7 1039 DEFINE_PROP_UINT32("irq", TPMState, irq_num, TPM_TIS_IRQ),
c0378544 1040 DEFINE_PROP_TPMBE("tpmdev", TPMState, be_driver),
edff8678
SB
1041 DEFINE_PROP_END_OF_LIST(),
1042};
1043
1044static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
1045{
1046 TPMState *s = TPM(dev);
edff8678 1047
51a837e9
MAL
1048 if (!tpm_find()) {
1049 error_setg(errp, "at most one TPM device is permitted");
1050 return;
1051 }
1052
edff8678 1053 if (!s->be_driver) {
c0378544 1054 error_setg(errp, "'tpmdev' property is required");
edff8678
SB
1055 return;
1056 }
3d4960c7 1057 if (s->irq_num > 15) {
c87b35fa
MAL
1058 error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
1059 s->irq_num);
edff8678
SB
1060 return;
1061 }
1062
3d4960c7 1063 isa_init_irq(&s->busdev, &s->irq, s->irq_num);
9dfd24ed
SB
1064
1065 memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
1066 TPM_TIS_ADDR_BASE, &s->mmio);
edff8678
SB
1067}
1068
1069static void tpm_tis_initfn(Object *obj)
1070{
edff8678
SB
1071 TPMState *s = TPM(obj);
1072
853dca12
PB
1073 memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
1074 s, "tpm-tis-mmio",
edff8678 1075 TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
edff8678
SB
1076}
1077
edff8678
SB
1078static void tpm_tis_class_init(ObjectClass *klass, void *data)
1079{
1080 DeviceClass *dc = DEVICE_CLASS(klass);
05a69998 1081 TPMIfClass *tc = TPM_IF_CLASS(klass);
edff8678
SB
1082
1083 dc->realize = tpm_tis_realizefn;
1084 dc->props = tpm_tis_properties;
1085 dc->reset = tpm_tis_reset;
1086 dc->vmsd = &vmstate_tpm_tis;
191adc94 1087 tc->model = TPM_MODEL_TPM_TIS;
9af7a721 1088 tc->get_version = tpm_tis_get_tpm_version;
05a69998 1089 tc->request_completed = tpm_tis_request_completed;
edff8678
SB
1090}
1091
1092static const TypeInfo tpm_tis_info = {
1093 .name = TYPE_TPM_TIS,
1094 .parent = TYPE_ISA_DEVICE,
1095 .instance_size = sizeof(TPMState),
1096 .instance_init = tpm_tis_initfn,
edff8678 1097 .class_init = tpm_tis_class_init,
698f5daa
MAL
1098 .interfaces = (InterfaceInfo[]) {
1099 { TYPE_TPM_IF },
1100 { }
1101 }
edff8678
SB
1102};
1103
1104static void tpm_tis_register(void)
1105{
1106 type_register_static(&tpm_tis_info);
edff8678
SB
1107}
1108
1109type_init(tpm_tis_register)