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/module.h"
20 #include "qapi/error.h"
21 #include "exec/address-spaces.h"
22 #include "hw/qdev-properties.h"
23 #include "hw/pci/pci_ids.h"
24 #include "hw/acpi/tpm.h"
25 #include "migration/vmstate.h"
26 #include "sysemu/tpm_backend.h"
27 #include "sysemu/reset.h"
33 typedef struct CRBState
{
34 DeviceState parent_obj
;
38 uint32_t regs
[TPM_CRB_R_MAX
];
42 size_t be_buffer_size
;
48 #define CRB(obj) OBJECT_CHECK(CRBState, (obj), TYPE_TPM_CRB)
50 #define CRB_INTF_TYPE_CRB_ACTIVE 0b1
51 #define CRB_INTF_VERSION_CRB 0b1
52 #define CRB_INTF_CAP_LOCALITY_0_ONLY 0b0
53 #define CRB_INTF_CAP_IDLE_FAST 0b0
54 #define CRB_INTF_CAP_XFER_SIZE_64 0b11
55 #define CRB_INTF_CAP_FIFO_NOT_SUPPORTED 0b0
56 #define CRB_INTF_CAP_CRB_SUPPORTED 0b1
57 #define CRB_INTF_IF_SELECTOR_CRB 0b1
59 #define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_SIZE - A_CRB_DATA_BUFFER)
62 CRB_LOC_CTRL_REQUEST_ACCESS
= BIT(0),
63 CRB_LOC_CTRL_RELINQUISH
= BIT(1),
64 CRB_LOC_CTRL_SEIZE
= BIT(2),
65 CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT
= BIT(3),
69 CRB_CTRL_REQ_CMD_READY
= BIT(0),
70 CRB_CTRL_REQ_GO_IDLE
= BIT(1),
74 CRB_START_INVOKE
= BIT(0),
78 CRB_CANCEL_INVOKE
= BIT(0),
81 #define TPM_CRB_NO_LOCALITY 0xff
83 static uint64_t tpm_crb_mmio_read(void *opaque
, hwaddr addr
,
86 CRBState
*s
= CRB(opaque
);
87 void *regs
= (void *)&s
->regs
+ (addr
& ~3);
88 unsigned offset
= addr
& 3;
89 uint32_t val
= *(uint32_t *)regs
>> (8 * offset
);
93 val
|= !tpm_backend_get_tpm_established_flag(s
->tpmbe
);
97 trace_tpm_crb_mmio_read(addr
, size
, val
);
102 static uint8_t tpm_crb_get_active_locty(CRBState
*s
)
104 if (!ARRAY_FIELD_EX32(s
->regs
, CRB_LOC_STATE
, locAssigned
)) {
105 return TPM_CRB_NO_LOCALITY
;
107 return ARRAY_FIELD_EX32(s
->regs
, CRB_LOC_STATE
, activeLocality
);
110 static void tpm_crb_mmio_write(void *opaque
, hwaddr addr
,
111 uint64_t val
, unsigned size
)
113 CRBState
*s
= CRB(opaque
);
114 uint8_t locty
= addr
>> 12;
116 trace_tpm_crb_mmio_write(addr
, size
, val
);
121 case CRB_CTRL_REQ_CMD_READY
:
122 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
125 case CRB_CTRL_REQ_GO_IDLE
:
126 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
131 case A_CRB_CTRL_CANCEL
:
132 if (val
== CRB_CANCEL_INVOKE
&&
133 s
->regs
[R_CRB_CTRL_START
] & CRB_START_INVOKE
) {
134 tpm_backend_cancel_cmd(s
->tpmbe
);
137 case A_CRB_CTRL_START
:
138 if (val
== CRB_START_INVOKE
&&
139 !(s
->regs
[R_CRB_CTRL_START
] & CRB_START_INVOKE
) &&
140 tpm_crb_get_active_locty(s
) == locty
) {
141 void *mem
= memory_region_get_ram_ptr(&s
->cmdmem
);
143 s
->regs
[R_CRB_CTRL_START
] |= CRB_START_INVOKE
;
144 s
->cmd
= (TPMBackendCmd
) {
146 .in_len
= MIN(tpm_cmd_get_size(mem
), s
->be_buffer_size
),
148 .out_len
= s
->be_buffer_size
,
151 tpm_backend_deliver_request(s
->tpmbe
, &s
->cmd
);
156 case CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT
:
159 case CRB_LOC_CTRL_RELINQUISH
:
160 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
162 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
165 case CRB_LOC_CTRL_REQUEST_ACCESS
:
166 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
168 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
170 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
178 static const MemoryRegionOps tpm_crb_memory_ops
= {
179 .read
= tpm_crb_mmio_read
,
180 .write
= tpm_crb_mmio_write
,
181 .endianness
= DEVICE_LITTLE_ENDIAN
,
183 .min_access_size
= 1,
184 .max_access_size
= 4,
188 static void tpm_crb_request_completed(TPMIf
*ti
, int ret
)
190 CRBState
*s
= CRB(ti
);
192 s
->regs
[R_CRB_CTRL_START
] &= ~CRB_START_INVOKE
;
194 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
195 tpmSts
, 1); /* fatal error */
199 static enum TPMVersion
tpm_crb_get_version(TPMIf
*ti
)
201 CRBState
*s
= CRB(ti
);
203 return tpm_backend_get_tpm_version(s
->tpmbe
);
206 static int tpm_crb_pre_save(void *opaque
)
208 CRBState
*s
= opaque
;
210 tpm_backend_finish_sync(s
->tpmbe
);
215 static const VMStateDescription vmstate_tpm_crb
= {
217 .pre_save
= tpm_crb_pre_save
,
218 .fields
= (VMStateField
[]) {
219 VMSTATE_UINT32_ARRAY(regs
, CRBState
, TPM_CRB_R_MAX
),
220 VMSTATE_END_OF_LIST(),
224 static Property tpm_crb_properties
[] = {
225 DEFINE_PROP_TPMBE("tpmdev", CRBState
, tpmbe
),
226 DEFINE_PROP_BOOL("ppi", CRBState
, ppi_enabled
, true),
227 DEFINE_PROP_END_OF_LIST(),
230 static void tpm_crb_reset(void *dev
)
232 CRBState
*s
= CRB(dev
);
234 if (s
->ppi_enabled
) {
235 tpm_ppi_reset(&s
->ppi
);
237 tpm_backend_reset(s
->tpmbe
);
239 memset(s
->regs
, 0, sizeof(s
->regs
));
241 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
243 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
245 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
246 InterfaceType
, CRB_INTF_TYPE_CRB_ACTIVE
);
247 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
248 InterfaceVersion
, CRB_INTF_VERSION_CRB
);
249 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
250 CapLocality
, CRB_INTF_CAP_LOCALITY_0_ONLY
);
251 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
252 CapCRBIdleBypass
, CRB_INTF_CAP_IDLE_FAST
);
253 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
254 CapDataXferSizeSupport
, CRB_INTF_CAP_XFER_SIZE_64
);
255 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
256 CapFIFO
, CRB_INTF_CAP_FIFO_NOT_SUPPORTED
);
257 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
258 CapCRB
, CRB_INTF_CAP_CRB_SUPPORTED
);
259 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
260 InterfaceSelector
, CRB_INTF_IF_SELECTOR_CRB
);
261 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
263 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID2
,
264 VID
, PCI_VENDOR_ID_IBM
);
266 s
->regs
[R_CRB_CTRL_CMD_SIZE
] = CRB_CTRL_CMD_SIZE
;
267 s
->regs
[R_CRB_CTRL_CMD_LADDR
] = TPM_CRB_ADDR_BASE
+ A_CRB_DATA_BUFFER
;
268 s
->regs
[R_CRB_CTRL_RSP_SIZE
] = CRB_CTRL_CMD_SIZE
;
269 s
->regs
[R_CRB_CTRL_RSP_ADDR
] = TPM_CRB_ADDR_BASE
+ A_CRB_DATA_BUFFER
;
271 s
->be_buffer_size
= MIN(tpm_backend_get_buffer_size(s
->tpmbe
),
274 if (tpm_backend_startup_tpm(s
->tpmbe
, s
->be_buffer_size
) < 0) {
279 static void tpm_crb_realize(DeviceState
*dev
, Error
**errp
)
281 CRBState
*s
= CRB(dev
);
284 error_setg(errp
, "at most one TPM device is permitted");
288 error_setg(errp
, "'tpmdev' property is required");
292 memory_region_init_io(&s
->mmio
, OBJECT(s
), &tpm_crb_memory_ops
, s
,
293 "tpm-crb-mmio", sizeof(s
->regs
));
294 memory_region_init_ram(&s
->cmdmem
, OBJECT(s
),
295 "tpm-crb-cmd", CRB_CTRL_CMD_SIZE
, errp
);
297 memory_region_add_subregion(get_system_memory(),
298 TPM_CRB_ADDR_BASE
, &s
->mmio
);
299 memory_region_add_subregion(get_system_memory(),
300 TPM_CRB_ADDR_BASE
+ sizeof(s
->regs
), &s
->cmdmem
);
302 if (s
->ppi_enabled
) {
303 tpm_ppi_init(&s
->ppi
, get_system_memory(),
304 TPM_PPI_ADDR_BASE
, OBJECT(s
));
307 qemu_register_reset(tpm_crb_reset
, dev
);
310 static void tpm_crb_class_init(ObjectClass
*klass
, void *data
)
312 DeviceClass
*dc
= DEVICE_CLASS(klass
);
313 TPMIfClass
*tc
= TPM_IF_CLASS(klass
);
315 dc
->realize
= tpm_crb_realize
;
316 device_class_set_props(dc
, tpm_crb_properties
);
317 dc
->vmsd
= &vmstate_tpm_crb
;
318 dc
->user_creatable
= true;
319 tc
->model
= TPM_MODEL_TPM_CRB
;
320 tc
->get_version
= tpm_crb_get_version
;
321 tc
->request_completed
= tpm_crb_request_completed
;
323 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
326 static const TypeInfo tpm_crb_info
= {
327 .name
= TYPE_TPM_CRB
,
328 /* could be TYPE_SYS_BUS_DEVICE (or LPC etc) */
329 .parent
= TYPE_DEVICE
,
330 .instance_size
= sizeof(CRBState
),
331 .class_init
= tpm_crb_class_init
,
332 .interfaces
= (InterfaceInfo
[]) {
338 static void tpm_crb_register(void)
340 type_register_static(&tpm_crb_info
);
343 type_init(tpm_crb_register
)