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 "hw/qdev-properties.h"
22 #include "hw/pci/pci_ids.h"
23 #include "hw/acpi/tpm.h"
24 #include "migration/vmstate.h"
25 #include "sysemu/tpm_backend.h"
26 #include "sysemu/tpm_util.h"
27 #include "sysemu/reset.h"
31 #include "qom/object.h"
34 DeviceState parent_obj
;
38 uint32_t regs
[TPM_CRB_R_MAX
];
42 size_t be_buffer_size
;
47 typedef struct CRBState CRBState
;
49 DECLARE_INSTANCE_CHECKER(CRBState
, CRB
,
52 #define CRB_INTF_TYPE_CRB_ACTIVE 0b1
53 #define CRB_INTF_VERSION_CRB 0b1
54 #define CRB_INTF_CAP_LOCALITY_0_ONLY 0b0
55 #define CRB_INTF_CAP_IDLE_FAST 0b0
56 #define CRB_INTF_CAP_XFER_SIZE_64 0b11
57 #define CRB_INTF_CAP_FIFO_NOT_SUPPORTED 0b0
58 #define CRB_INTF_CAP_CRB_SUPPORTED 0b1
59 #define CRB_INTF_IF_SELECTOR_CRB 0b1
61 #define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_SIZE - A_CRB_DATA_BUFFER)
64 CRB_LOC_CTRL_REQUEST_ACCESS
= BIT(0),
65 CRB_LOC_CTRL_RELINQUISH
= BIT(1),
66 CRB_LOC_CTRL_SEIZE
= BIT(2),
67 CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT
= BIT(3),
71 CRB_CTRL_REQ_CMD_READY
= BIT(0),
72 CRB_CTRL_REQ_GO_IDLE
= BIT(1),
76 CRB_START_INVOKE
= BIT(0),
80 CRB_CANCEL_INVOKE
= BIT(0),
83 #define TPM_CRB_NO_LOCALITY 0xff
85 static uint64_t tpm_crb_mmio_read(void *opaque
, hwaddr addr
,
88 CRBState
*s
= CRB(opaque
);
89 void *regs
= (void *)&s
->regs
+ (addr
& ~3);
90 unsigned offset
= addr
& 3;
91 uint32_t val
= *(uint32_t *)regs
>> (8 * offset
);
95 val
|= !tpm_backend_get_tpm_established_flag(s
->tpmbe
);
99 trace_tpm_crb_mmio_read(addr
, size
, val
);
104 static uint8_t tpm_crb_get_active_locty(CRBState
*s
)
106 if (!ARRAY_FIELD_EX32(s
->regs
, CRB_LOC_STATE
, locAssigned
)) {
107 return TPM_CRB_NO_LOCALITY
;
109 return ARRAY_FIELD_EX32(s
->regs
, CRB_LOC_STATE
, activeLocality
);
112 static void tpm_crb_mmio_write(void *opaque
, hwaddr addr
,
113 uint64_t val
, unsigned size
)
115 CRBState
*s
= CRB(opaque
);
116 uint8_t locty
= addr
>> 12;
118 trace_tpm_crb_mmio_write(addr
, size
, val
);
123 case CRB_CTRL_REQ_CMD_READY
:
124 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
127 case CRB_CTRL_REQ_GO_IDLE
:
128 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
133 case A_CRB_CTRL_CANCEL
:
134 if (val
== CRB_CANCEL_INVOKE
&&
135 s
->regs
[R_CRB_CTRL_START
] & CRB_START_INVOKE
) {
136 tpm_backend_cancel_cmd(s
->tpmbe
);
139 case A_CRB_CTRL_START
:
140 if (val
== CRB_START_INVOKE
&&
141 !(s
->regs
[R_CRB_CTRL_START
] & CRB_START_INVOKE
) &&
142 tpm_crb_get_active_locty(s
) == locty
) {
143 void *mem
= memory_region_get_ram_ptr(&s
->cmdmem
);
145 s
->regs
[R_CRB_CTRL_START
] |= CRB_START_INVOKE
;
146 s
->cmd
= (TPMBackendCmd
) {
148 .in_len
= MIN(tpm_cmd_get_size(mem
), s
->be_buffer_size
),
150 .out_len
= s
->be_buffer_size
,
153 tpm_backend_deliver_request(s
->tpmbe
, &s
->cmd
);
158 case CRB_LOC_CTRL_RESET_ESTABLISHMENT_BIT
:
161 case CRB_LOC_CTRL_RELINQUISH
:
162 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
164 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
167 case CRB_LOC_CTRL_REQUEST_ACCESS
:
168 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
170 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STS
,
172 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
180 static const MemoryRegionOps tpm_crb_memory_ops
= {
181 .read
= tpm_crb_mmio_read
,
182 .write
= tpm_crb_mmio_write
,
183 .endianness
= DEVICE_LITTLE_ENDIAN
,
185 .min_access_size
= 1,
186 .max_access_size
= 4,
190 static void tpm_crb_request_completed(TPMIf
*ti
, int ret
)
192 CRBState
*s
= CRB(ti
);
194 s
->regs
[R_CRB_CTRL_START
] &= ~CRB_START_INVOKE
;
196 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
197 tpmSts
, 1); /* fatal error */
201 static enum TPMVersion
tpm_crb_get_version(TPMIf
*ti
)
203 CRBState
*s
= CRB(ti
);
205 return tpm_backend_get_tpm_version(s
->tpmbe
);
208 static int tpm_crb_pre_save(void *opaque
)
210 CRBState
*s
= opaque
;
212 tpm_backend_finish_sync(s
->tpmbe
);
217 static const VMStateDescription vmstate_tpm_crb
= {
219 .pre_save
= tpm_crb_pre_save
,
220 .fields
= (VMStateField
[]) {
221 VMSTATE_UINT32_ARRAY(regs
, CRBState
, TPM_CRB_R_MAX
),
222 VMSTATE_END_OF_LIST(),
226 static Property tpm_crb_properties
[] = {
227 DEFINE_PROP_TPMBE("tpmdev", CRBState
, tpmbe
),
228 DEFINE_PROP_BOOL("ppi", CRBState
, ppi_enabled
, true),
229 DEFINE_PROP_END_OF_LIST(),
232 static void tpm_crb_reset(void *dev
)
234 CRBState
*s
= CRB(dev
);
236 if (s
->ppi_enabled
) {
237 tpm_ppi_reset(&s
->ppi
);
239 tpm_backend_reset(s
->tpmbe
);
241 memset(s
->regs
, 0, sizeof(s
->regs
));
243 ARRAY_FIELD_DP32(s
->regs
, CRB_LOC_STATE
,
245 ARRAY_FIELD_DP32(s
->regs
, CRB_CTRL_STS
,
247 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
248 InterfaceType
, CRB_INTF_TYPE_CRB_ACTIVE
);
249 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
250 InterfaceVersion
, CRB_INTF_VERSION_CRB
);
251 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
252 CapLocality
, CRB_INTF_CAP_LOCALITY_0_ONLY
);
253 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
254 CapCRBIdleBypass
, CRB_INTF_CAP_IDLE_FAST
);
255 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
256 CapDataXferSizeSupport
, CRB_INTF_CAP_XFER_SIZE_64
);
257 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
258 CapFIFO
, CRB_INTF_CAP_FIFO_NOT_SUPPORTED
);
259 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
260 CapCRB
, CRB_INTF_CAP_CRB_SUPPORTED
);
261 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
262 InterfaceSelector
, CRB_INTF_IF_SELECTOR_CRB
);
263 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID
,
265 ARRAY_FIELD_DP32(s
->regs
, CRB_INTF_ID2
,
266 VID
, PCI_VENDOR_ID_IBM
);
268 s
->regs
[R_CRB_CTRL_CMD_SIZE
] = CRB_CTRL_CMD_SIZE
;
269 s
->regs
[R_CRB_CTRL_CMD_LADDR
] = TPM_CRB_ADDR_BASE
+ A_CRB_DATA_BUFFER
;
270 s
->regs
[R_CRB_CTRL_RSP_SIZE
] = CRB_CTRL_CMD_SIZE
;
271 s
->regs
[R_CRB_CTRL_RSP_ADDR
] = TPM_CRB_ADDR_BASE
+ A_CRB_DATA_BUFFER
;
273 s
->be_buffer_size
= MIN(tpm_backend_get_buffer_size(s
->tpmbe
),
276 if (tpm_backend_startup_tpm(s
->tpmbe
, s
->be_buffer_size
) < 0) {
281 static void tpm_crb_realize(DeviceState
*dev
, Error
**errp
)
283 CRBState
*s
= CRB(dev
);
286 error_setg(errp
, "at most one TPM device is permitted");
290 error_setg(errp
, "'tpmdev' property is required");
294 memory_region_init_io(&s
->mmio
, OBJECT(s
), &tpm_crb_memory_ops
, s
,
295 "tpm-crb-mmio", sizeof(s
->regs
));
296 memory_region_init_ram(&s
->cmdmem
, OBJECT(s
),
297 "tpm-crb-cmd", CRB_CTRL_CMD_SIZE
, errp
);
299 memory_region_add_subregion(get_system_memory(),
300 TPM_CRB_ADDR_BASE
, &s
->mmio
);
301 memory_region_add_subregion(get_system_memory(),
302 TPM_CRB_ADDR_BASE
+ sizeof(s
->regs
), &s
->cmdmem
);
304 if (s
->ppi_enabled
) {
305 tpm_ppi_init(&s
->ppi
, get_system_memory(),
306 TPM_PPI_ADDR_BASE
, OBJECT(s
));
309 qemu_register_reset(tpm_crb_reset
, dev
);
312 static void tpm_crb_class_init(ObjectClass
*klass
, void *data
)
314 DeviceClass
*dc
= DEVICE_CLASS(klass
);
315 TPMIfClass
*tc
= TPM_IF_CLASS(klass
);
317 dc
->realize
= tpm_crb_realize
;
318 device_class_set_props(dc
, tpm_crb_properties
);
319 dc
->vmsd
= &vmstate_tpm_crb
;
320 dc
->user_creatable
= true;
321 tc
->model
= TPM_MODEL_TPM_CRB
;
322 tc
->get_version
= tpm_crb_get_version
;
323 tc
->request_completed
= tpm_crb_request_completed
;
325 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
328 static const TypeInfo tpm_crb_info
= {
329 .name
= TYPE_TPM_CRB
,
330 /* could be TYPE_SYS_BUS_DEVICE (or LPC etc) */
331 .parent
= TYPE_DEVICE
,
332 .instance_size
= sizeof(CRBState
),
333 .class_init
= tpm_crb_class_init
,
334 .interfaces
= (InterfaceInfo
[]) {
340 static void tpm_crb_register(void)
342 type_register_static(&tpm_crb_info
);
345 type_init(tpm_crb_register
)