xref: /openbmc/qemu/hw/tpm/tpm_tis_sysbus.c (revision 8a49b300)
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 "tpm_util.h"
29 #include "hw/sysbus.h"
30 #include "tpm_tis.h"
31 
32 typedef struct TPMStateSysBus {
33     /*< private >*/
34     SysBusDevice parent_obj;
35 
36     /*< public >*/
37     TPMState state; /* not a QOM object */
38 } TPMStateSysBus;
39 
40 #define TPM_TIS_SYSBUS(obj) OBJECT_CHECK(TPMStateSysBus, (obj), TYPE_TPM_TIS_SYSBUS)
41 
42 static int tpm_tis_pre_save_sysbus(void *opaque)
43 {
44     TPMStateSysBus *sbdev = opaque;
45 
46     return tpm_tis_pre_save(&sbdev->state);
47 }
48 
49 static const VMStateDescription vmstate_tpm_tis_sysbus = {
50     .name = "tpm-tis",
51     .version_id = 0,
52     .pre_save  = tpm_tis_pre_save_sysbus,
53     .fields = (VMStateField[]) {
54         VMSTATE_BUFFER(state.buffer, TPMStateSysBus),
55         VMSTATE_UINT16(state.rw_offset, TPMStateSysBus),
56         VMSTATE_UINT8(state.active_locty, TPMStateSysBus),
57         VMSTATE_UINT8(state.aborting_locty, TPMStateSysBus),
58         VMSTATE_UINT8(state.next_locty, TPMStateSysBus),
59 
60         VMSTATE_STRUCT_ARRAY(state.loc, TPMStateSysBus, TPM_TIS_NUM_LOCALITIES,
61                              0, vmstate_locty, TPMLocality),
62 
63         VMSTATE_END_OF_LIST()
64     }
65 };
66 
67 static void tpm_tis_sysbus_request_completed(TPMIf *ti, int ret)
68 {
69     TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
70     TPMState *s = &sbdev->state;
71 
72     tpm_tis_request_completed(s, ret);
73 }
74 
75 static enum TPMVersion tpm_tis_sysbus_get_tpm_version(TPMIf *ti)
76 {
77     TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(ti);
78     TPMState *s = &sbdev->state;
79 
80     return tpm_tis_get_tpm_version(s);
81 }
82 
83 static void tpm_tis_sysbus_reset(DeviceState *dev)
84 {
85     TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
86     TPMState *s = &sbdev->state;
87 
88     return tpm_tis_reset(s);
89 }
90 
91 static Property tpm_tis_sysbus_properties[] = {
92     DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num, TPM_TIS_IRQ),
93     DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
94     DEFINE_PROP_BOOL("ppi", TPMStateSysBus, state.ppi_enabled, false),
95     DEFINE_PROP_END_OF_LIST(),
96 };
97 
98 static void tpm_tis_sysbus_initfn(Object *obj)
99 {
100     TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
101     TPMState *s = &sbdev->state;
102 
103     memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
104                           s, "tpm-tis-mmio",
105                           TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
106 
107     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
108     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
109 }
110 
111 static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
112 {
113     TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(dev);
114     TPMState *s = &sbdev->state;
115 
116     if (!tpm_find()) {
117         error_setg(errp, "at most one TPM device is permitted");
118         return;
119     }
120 
121     if (!s->be_driver) {
122         error_setg(errp, "'tpmdev' property is required");
123         return;
124     }
125 }
126 
127 static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
128 {
129     DeviceClass *dc = DEVICE_CLASS(klass);
130     TPMIfClass *tc = TPM_IF_CLASS(klass);
131 
132     device_class_set_props(dc, tpm_tis_sysbus_properties);
133     dc->vmsd  = &vmstate_tpm_tis_sysbus;
134     tc->model = TPM_MODEL_TPM_TIS;
135     dc->realize = tpm_tis_sysbus_realizefn;
136     dc->user_creatable = true;
137     dc->reset = tpm_tis_sysbus_reset;
138     tc->request_completed = tpm_tis_sysbus_request_completed;
139     tc->get_version = tpm_tis_sysbus_get_tpm_version;
140 }
141 
142 static const TypeInfo tpm_tis_sysbus_info = {
143     .name = TYPE_TPM_TIS_SYSBUS,
144     .parent = TYPE_SYS_BUS_DEVICE,
145     .instance_size = sizeof(TPMStateSysBus),
146     .instance_init = tpm_tis_sysbus_initfn,
147     .class_init  = tpm_tis_sysbus_class_init,
148     .interfaces = (InterfaceInfo[]) {
149         { TYPE_TPM_IF },
150         { }
151     }
152 };
153 
154 static void tpm_tis_sysbus_register(void)
155 {
156     type_register_static(&tpm_tis_sysbus_info);
157 }
158 
159 type_init(tpm_tis_sysbus_register)
160