2 * tpm_crb.c - QEMU's TPM CRB interface emulator
4 * Copyright (c) 2018 Red Hat, Inc.
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
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.
12 * tpm_crb is a device for TPM 2.0 Command Response Buffer (CRB) Interface
13 * as defined in TCG PC Client Platform TPM Profile (PTP) Specification
14 * Family “2.0” Level 00 Revision 01.03 v22
17 #include "qemu/osdep.h"
19 #include "qemu-common.h"
20 #include "qapi/error.h"
21 #include "exec/address-spaces.h"
23 #include "hw/qdev-core.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/pci/pci_ids.h"
26 #include "hw/acpi/tpm.h"
27 #include "migration/vmstate.h"
28 #include "sysemu/tpm_backend.h"
29 #include "sysemu/reset.h"
34 typedef struct CRBState
{
35 DeviceState parent_obj
;
39 uint32_t regs
[TPM_CRB_R_MAX
];
43 size_t be_buffer_size
;
46 #define CRB(obj) OBJECT_CHECK(CRBState, (obj), TYPE_TPM_CRB)
48 #define CRB_INTF_TYPE_CRB_ACTIVE 0b1
49 #define CRB_INTF_VERSION_CRB 0b1
50 #define CRB_INTF_CAP_LOCALITY_0_ONLY 0b0
51 #define CRB_INTF_CAP_IDLE_FAST 0b0
52 #define CRB_INTF_CAP_XFER_SIZE_64 0b11
53 #define CRB_INTF_CAP_FIFO_NOT_SUPPORTED 0b0
54 #define CRB_INTF_CAP_CRB_SUPPORTED 0b1
55 #define CRB_INTF_IF_SELECTOR_CRB 0b1
57 #define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_SIZE - A_CRB_DATA_BUFFER)
60 CRB_LOC_CTRL_REQUEST_ACCESS
= BIT(0),
61 CRB_LOC_CTRL_RELINQUISH
= BIT(1),
62 CRB_LOC_CTRL_SEIZE
= BIT(2),
63 CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT
= BIT(3),
67 CRB_CTRL_REQ_CMD_READY
= BIT(0),
68 CRB_CTRL_REQ_GO_IDLE
= BIT(1),
72 CRB_START_INVOKE
= BIT(0),
76 CRB_CANCEL_INVOKE
= BIT(0),
79 #define TPM_CRB_NO_LOCALITY 0xff
81 static uint64_t tpm_crb_mmio_read(void *opaque
, hwaddr addr
,
84 CRBState
*s
= CRB(opaque
);
85 void *regs
= (void *)&s
->regs
+ (addr
& ~3);
86 unsigned offset
= addr
& 3;
87 uint32_t val
= *(uint32_t *)regs
>> (8 * offset
);
91 val
|= !tpm_backend_get_tpm_established_flag(s
->tpmbe
);
95 trace_tpm_crb_mmio_read(addr
, size
, val
);
100 static uint8_t tpm_crb_get_active_locty(CRBState
*s
)
102 if (!ARRAY_FIELD_EX32(s
->regs
, CRB_LOC_STATE
, locAssigned
)) {
103 return TPM_CRB_NO_LOCALITY
;
105 return ARRAY_FIELD_EX32(s
->regs
, CRB_LOC_STATE
, activeLocality
);
108 static void tpm_crb_mmio_write(void *opaque
, hwaddr addr
,
109 uint64_t val
, unsigned size
)
111 CRBState
*s
= CRB(opaque
);
112 uint8_t locty
= addr
>> 12;
114 trace_tpm_crb_mmio_write(addr
, size
, val
);
119 case CRB_CTRL_REQ_CMD_READY
:
120 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
123 case CRB_CTRL_REQ_GO_IDLE
:
124 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
129 case A_CRB_CTRL_CANCEL
:
130 if (val
== CRB_CANCEL_INVOKE
&&
131 s
->regs
[R_CRB_CTRL_START
] & CRB_START_INVOKE
) {
132 tpm_backend_cancel_cmd(s
->tpmbe
);
135 case A_CRB_CTRL_START
:
136 if (val
== CRB_START_INVOKE
&&
137 !(s
->regs
[R_CRB_CTRL_START
] & CRB_START_INVOKE
) &&
138 tpm_crb_get_active_locty(s
) == locty
) {
139 void *mem
= memory_region_get_ram_ptr(&s
->cmdmem
);
141 s
->regs
[R_CRB_CTRL_START
] |= CRB_START_INVOKE
;
142 s
->cmd
= (TPMBackendCmd
) {
144 .in_len
= MIN(tpm_cmd_get_size(mem
), s
->be_buffer_size
),
146 .out_len
= s
->be_buffer_size
,
149 tpm_backend_deliver_request(s
->tpmbe
, &s
->cmd
);
154 case CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT
:
157 case CRB_LOC_CTRL_RELINQUISH
:
158 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
160 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
163 case CRB_LOC_CTRL_REQUEST_ACCESS
:
164 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
166 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
168 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
176 static const MemoryRegionOps tpm_crb_memory_ops
= {
177 .read
= tpm_crb_mmio_read
,
178 .write
= tpm_crb_mmio_write
,
179 .endianness
= DEVICE_LITTLE_ENDIAN
,
181 .min_access_size
= 1,
182 .max_access_size
= 4,
186 static void tpm_crb_request_completed(TPMIf
*ti
, int ret
)
188 CRBState
*s
= CRB(ti
);
190 s
->regs
[R_CRB_CTRL_START
] &= ~CRB_START_INVOKE
;
192 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
193 tpmSts
, 1); /* fatal error */
197 static enum TPMVersion
tpm_crb_get_version(TPMIf
*ti
)
199 CRBState
*s
= CRB(ti
);
201 return tpm_backend_get_tpm_version(s
->tpmbe
);
204 static int tpm_crb_pre_save(void *opaque
)
206 CRBState
*s
= opaque
;
208 tpm_backend_finish_sync(s
->tpmbe
);
213 static const VMStateDescription vmstate_tpm_crb
= {
215 .pre_save
= tpm_crb_pre_save
,
216 .fields
= (VMStateField
[]) {
217 VMSTATE_UINT32_ARRAY(regs
, CRBState
, TPM_CRB_R_MAX
),
218 VMSTATE_END_OF_LIST(),
222 static Property tpm_crb_properties
[] = {
223 DEFINE_PROP_TPMBE("tpmdev", CRBState
, tpmbe
),
224 DEFINE_PROP_END_OF_LIST(),
227 static void tpm_crb_reset(void *dev
)
229 CRBState
*s
= CRB(dev
);
231 tpm_backend_reset(s
->tpmbe
);
233 memset(s
->regs
, 0, sizeof(s
->regs
));
235 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
237 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
239 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
240 InterfaceType
, CRB_INTF_TYPE_CRB_ACTIVE
);
241 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
242 InterfaceVersion
, CRB_INTF_VERSION_CRB
);
243 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
244 CapLocality
, CRB_INTF_CAP_LOCALITY_0_ONLY
);
245 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
246 CapCRBIdleBypass
, CRB_INTF_CAP_IDLE_FAST
);
247 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
248 CapDataXferSizeSupport
, CRB_INTF_CAP_XFER_SIZE_64
);
249 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
250 CapFIFO
, CRB_INTF_CAP_FIFO_NOT_SUPPORTED
);
251 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
252 CapCRB
, CRB_INTF_CAP_CRB_SUPPORTED
);
253 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
254 InterfaceSelector
, CRB_INTF_IF_SELECTOR_CRB
);
255 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
257 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID2
,
258 VID
, PCI_VENDOR_ID_IBM
);
260 s
->regs
[R_CRB_CTRL_CMD_SIZE
] = CRB_CTRL_CMD_SIZE
;
261 s
->regs
[R_CRB_CTRL_CMD_LADDR
] = TPM_CRB_ADDR_BASE
+ A_CRB_DATA_BUFFER
;
262 s
->regs
[R_CRB_CTRL_RSP_SIZE
] = CRB_CTRL_CMD_SIZE
;
263 s
->regs
[R_CRB_CTRL_RSP_ADDR
] = TPM_CRB_ADDR_BASE
+ A_CRB_DATA_BUFFER
;
265 s
->be_buffer_size
= MIN(tpm_backend_get_buffer_size(s
->tpmbe
),
268 tpm_backend_startup_tpm(s
->tpmbe
, s
->be_buffer_size
);
271 static void tpm_crb_realize(DeviceState
*dev
, Error
**errp
)
273 CRBState
*s
= CRB(dev
);
276 error_setg(errp
, "at most one TPM device is permitted");
280 error_setg(errp
, "'tpmdev' property is required");
284 memory_region_init_io(&s
->mmio
, OBJECT(s
), &tpm_crb_memory_ops
, s
,
285 "tpm-crb-mmio", sizeof(s
->regs
));
286 memory_region_init_ram(&s
->cmdmem
, OBJECT(s
),
287 "tpm-crb-cmd", CRB_CTRL_CMD_SIZE
, errp
);
289 memory_region_add_subregion(get_system_memory(),
290 TPM_CRB_ADDR_BASE
, &s
->mmio
);
291 memory_region_add_subregion(get_system_memory(),
292 TPM_CRB_ADDR_BASE
+ sizeof(s
->regs
), &s
->cmdmem
);
294 qemu_register_reset(tpm_crb_reset
, dev
);
297 static void tpm_crb_class_init(ObjectClass
*klass
, void *data
)
299 DeviceClass
*dc
= DEVICE_CLASS(klass
);
300 TPMIfClass
*tc
= TPM_IF_CLASS(klass
);
302 dc
->realize
= tpm_crb_realize
;
303 dc
->props
= tpm_crb_properties
;
304 dc
->vmsd
= &vmstate_tpm_crb
;
305 dc
->user_creatable
= true;
306 tc
->model
= TPM_MODEL_TPM_CRB
;
307 tc
->get_version
= tpm_crb_get_version
;
308 tc
->request_completed
= tpm_crb_request_completed
;
310 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
313 static const TypeInfo tpm_crb_info
= {
314 .name
= TYPE_TPM_CRB
,
315 /* could be TYPE_SYS_BUS_DEVICE (or LPC etc) */
316 .parent
= TYPE_DEVICE
,
317 .instance_size
= sizeof(CRBState
),
318 .class_init
= tpm_crb_class_init
,
319 .interfaces
= (InterfaceInfo
[]) {
325 static void tpm_crb_register(void)
327 type_register_static(&tpm_crb_info
);
330 type_init(tpm_crb_register
)