master
c 202 lines 6 KB
Raw
1 /*
2 * tpm_tis_isa.c - QEMU's TPM TIS ISA 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, Family 2.0, Revision 00.43
23 */
24
25 #include "qemu/osdep.h"
26 #include "hw/isa/isa.h"
27 #include "hw/core/qdev-properties.h"
28 #include "migration/vmstate.h"
29 #include "hw/acpi/tpm.h"
30 #include "tpm_prop.h"
31 #include "tpm_tis.h"
32 #include "qom/object.h"
33 #include "hw/acpi/acpi_aml_interface.h"
34
35 struct TPMStateISA {
36 /*< private >*/
37 ISADevice parent_obj;
38
39 /*< public >*/
40 TPMState state; /* not a QOM object */
41 };
42
43 OBJECT_DECLARE_SIMPLE_TYPE(TPMStateISA, TPM_TIS_ISA)
44
45 static int tpm_tis_pre_save_isa(void *opaque)
46 {
47 TPMStateISA *isadev = opaque;
48
49 return tpm_tis_pre_save(&isadev->state);
50 }
51
52 static const VMStateDescription vmstate_tpm_tis_isa = {
53 .name = "tpm-tis",
54 .version_id = 0,
55 .pre_save = tpm_tis_pre_save_isa,
56 .fields = (const VMStateField[]) {
57 VMSTATE_BUFFER(state.buffer, TPMStateISA),
58 VMSTATE_UINT16(state.rw_offset, TPMStateISA),
59 VMSTATE_UINT8(state.active_locty, TPMStateISA),
60 VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
61 VMSTATE_UINT8(state.next_locty, TPMStateISA),
62
63 VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
64 vmstate_locty, TPMLocality),
65
66 VMSTATE_END_OF_LIST()
67 }
68 };
69
70 static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
71 {
72 TPMStateISA *isadev = TPM_TIS_ISA(ti);
73 TPMState *s = &isadev->state;
74
75 tpm_tis_request_completed(s, ret);
76 }
77
78 static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
79 {
80 TPMStateISA *isadev = TPM_TIS_ISA(ti);
81 TPMState *s = &isadev->state;
82
83 return tpm_tis_get_tpm_version(s);
84 }
85
86 static void tpm_tis_isa_reset(DeviceState *dev)
87 {
88 TPMStateISA *isadev = TPM_TIS_ISA(dev);
89 TPMState *s = &isadev->state;
90
91 return tpm_tis_reset(s, true);
92 }
93
94 static const Property tpm_tis_isa_properties[] = {
95 DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
96 DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
97 };
98
99 static void tpm_tis_isa_initfn(Object *obj)
100 {
101 TPMStateISA *isadev = TPM_TIS_ISA(obj);
102 TPMState *s = &isadev->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
109 static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
110 {
111 TPMStateISA *isadev = TPM_TIS_ISA(dev);
112 TPMState *s = &isadev->state;
113
114 if (!tpm_find()) {
115 error_setg(errp, "at most one TPM device is permitted");
116 return;
117 }
118
119 if (!s->be_driver) {
120 error_setg(errp, "'tpmdev' property is required");
121 return;
122 }
123 if (s->irq_num > 15) {
124 error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
125 s->irq_num);
126 return;
127 }
128
129 s->irq = isa_get_irq(ISA_DEVICE(dev), s->irq_num);
130
131 memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
132 TPM_TIS_ADDR_BASE, &s->mmio);
133
134 tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
135 TPM_PPI_ADDR_BASE, OBJECT(dev));
136 }
137
138 static void build_tpm_tis_isa_aml(AcpiDevAmlIf *adev, Aml *scope)
139 {
140 Aml *dev, *crs;
141 TPMStateISA *isadev = TPM_TIS_ISA(adev);
142 TPMIf *ti = TPM_IF(isadev);
143
144 dev = aml_device("TPM");
145 if (tpm_tis_isa_get_tpm_version(ti) == TPM_VERSION_2_0) {
146 aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
147 aml_append(dev, aml_name_decl("_STR", aml_string("TPM 2.0 Device")));
148 } else {
149 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
150 }
151 aml_append(dev, aml_name_decl("_UID", aml_int(1)));
152 aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
153 crs = aml_resource_template();
154 aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE, TPM_TIS_ADDR_SIZE,
155 AML_READ_WRITE));
156 /*
157 * FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
158 * fix default TPM_TIS_IRQ value there to use some unused IRQ
159 */
160 /* aml_append(crs, aml_irq_no_flags(isadev->state.irq_num)); */
161 aml_append(dev, aml_name_decl("_CRS", crs));
162 tpm_build_ppi_acpi(ti, dev, TPM_PPI_ADDR_BASE);
163 aml_append(scope, dev);
164 }
165
166 static void tpm_tis_isa_class_init(ObjectClass *klass, const void *data)
167 {
168 DeviceClass *dc = DEVICE_CLASS(klass);
169 TPMIfClass *tc = TPM_IF_CLASS(klass);
170 AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
171
172 device_class_set_props(dc, tpm_tis_isa_properties);
173 dc->vmsd = &vmstate_tpm_tis_isa;
174 tc->model = TPM_MODEL_TPM_TIS;
175 tc->ppi_enabled = true;
176 dc->realize = tpm_tis_isa_realizefn;
177 device_class_set_legacy_reset(dc, tpm_tis_isa_reset);
178 tc->request_completed = tpm_tis_isa_request_completed;
179 tc->get_version = tpm_tis_isa_get_tpm_version;
180 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
181 adevc->build_dev_aml = build_tpm_tis_isa_aml;
182 }
183
184 static const TypeInfo tpm_tis_isa_info = {
185 .name = TYPE_TPM_TIS_ISA,
186 .parent = TYPE_ISA_DEVICE,
187 .instance_size = sizeof(TPMStateISA),
188 .instance_init = tpm_tis_isa_initfn,
189 .class_init = tpm_tis_isa_class_init,
190 .interfaces = (const InterfaceInfo[]) {
191 { TYPE_TPM_IF },
192 { TYPE_ACPI_DEV_AML_IF },
193 { }
194 }
195 };
196
197 static void tpm_tis_isa_register(void)
198 {
199 type_register_static(&tpm_tis_isa_info);
200 }
201
202 type_init(tpm_tis_isa_register)