1 /* 2 * tpm_tis_sysbus.c - QEMU's TPM TIS SYSBUS Device 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 17 * supports version 1.3, 21 March 2013 18 * In the developers menu choose the PC Client section then find the TIS 19 * specification. 20 * 21 * TPM TIS for TPM 2 implementation following TCG PC Client Platform 22 * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43 23 */ 24 25 #include "qemu/osdep.h" 26 #include "hw/qdev-properties.h" 27 #include "migration/vmstate.h" 28 #include "hw/acpi/tpm.h" 29 #include "tpm_prop.h" 30 #include "hw/sysbus.h" 31 #include "tpm_tis.h" 32 #include "qom/object.h" 33 34 struct TPMStateSysBus { 35 /*< private >*/ 36 SysBusDevice parent_obj; 37 38 /*< public >*/ 39 TPMState state; /* not a QOM object */ 40 }; 41 42 OBJECT_DECLARE_SIMPLE_TYPE(TPMStateSysBus, TPM_TIS_SYSBUS) 43 44 static int tpm_tis_pre_save_sysbus(void *opaque) 45 { 46 TPMStateSysBus *sbdev = opaque; 47 48 return tpm_tis_pre_save(&sbdev->state); 49 } 50 51 static const VMStateDescription vmstate_tpm_tis_sysbus = { 52 .name = "tpm-tis", 53 .version_id = 0, 54 .pre_save = tpm_tis_pre_save_sysbus, 55 .fields = (VMStateField[]) { 56 VMSTATE_BUFFER(state.buffer, TPMStateSysBus), 57 VMSTATE_UINT16(state.rw_offset, TPMStateSysBus), 58 VMSTATE_UINT8(state.active_locty, TPMStateSysBus), 59 VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus), 60 VMSTATE_UINT8(state.next_locty, TPMStateSysBus), 61 62 VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus, TPM_TIS_NUM_LOCALITIES, 63 0, vmstate_locty, TPMLocality), 64 65 VMSTATE_END_OF_LIST() 66 } 67 }; 68 69 static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret) 70 { 71 TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti); 72 TPMState *s = &sbdev->state; 73 74 tpm_tis_request_completed(s, ret); 75 } 76 77 static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti) 78 { 79 TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti); 80 TPMState *s = &sbdev->state; 81 82 return tpm_tis_get_tpm_version(s); 83 } 84 85 static void tpm_tis_sysbus_reset(DeviceState *dev) 86 { 87 TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev); 88 TPMState *s = &sbdev->state; 89 90 return tpm_tis_reset(s); 91 } 92 93 static Property tpm_tis_sysbus_properties[] = { 94 DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num, TPM_TIS_IRQ), 95 DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver), 96 DEFINE_PROP_END_OF_LIST(), 97 }; 98 99 static void tpm_tis_sysbus_initfn(Object *obj) 100 { 101 TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj); 102 TPMState *s = &sbdev->state; 103 104 memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops, 105 s, "tpm-tis-mmio", 106 TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT); 107 108 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); 109 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq); 110 } 111 112 static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp) 113 { 114 TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev); 115 TPMState *s = &sbdev->state; 116 117 if (!tpm_find()) { 118 error_setg(errp, "at most one TPM device is permitted"); 119 return; 120 } 121 122 if (!s->be_driver) { 123 error_setg(errp, "'tpmdev' property is required"); 124 return; 125 } 126 } 127 128 static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data) 129 { 130 DeviceClass *dc = DEVICE_CLASS(klass); 131 TPMIfClass *tc = TPM_IF_CLASS(klass); 132 133 device_class_set_props(dc, tpm_tis_sysbus_properties); 134 dc->vmsd = &vmstate_tpm_tis_sysbus; 135 tc->model = TPM_MODEL_TPM_TIS; 136 dc->realize = tpm_tis_sysbus_realizefn; 137 dc->user_creatable = true; 138 dc->reset = tpm_tis_sysbus_reset; 139 tc->request_completed = tpm_tis_sysbus_request_completed; 140 tc->get_version = tpm_tis_sysbus_get_tpm_version; 141 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 142 } 143 144 static const TypeInfo tpm_tis_sysbus_info = { 145 .name = TYPE_TPM_TIS_SYSBUS, 146 .parent = TYPE_SYS_BUS_DEVICE, 147 .instance_size = sizeof(TPMStateSysBus), 148 .instance_init = tpm_tis_sysbus_initfn, 149 .class_init = tpm_tis_sysbus_class_init, 150 .interfaces = (InterfaceInfo[]) { 151 { TYPE_TPM_IF }, 152 { } 153 } 154 }; 155 156 static void tpm_tis_sysbus_register(void) 157 { 158 type_register_static(&tpm_tis_sysbus_info); 159 } 160 161 type_init(tpm_tis_sysbus_register) 162