xref: /qemu/hw/tpm/tpm_tis_isa.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1ac90053dSEric Auger /*
2ac90053dSEric Auger  * tpm_tis_isa.c - QEMU's TPM TIS ISA Device
3ac90053dSEric Auger  *
4ac90053dSEric Auger  * Copyright (C) 2006,2010-2013 IBM Corporation
5ac90053dSEric Auger  *
6ac90053dSEric Auger  * Authors:
7ac90053dSEric Auger  *  Stefan Berger <stefanb@us.ibm.com>
8ac90053dSEric Auger  *  David Safford <safford@us.ibm.com>
9ac90053dSEric Auger  *
10ac90053dSEric Auger  * Xen 4 support: Andrease Niederl <andreas.niederl@iaik.tugraz.at>
11ac90053dSEric Auger  *
12ac90053dSEric Auger  * This work is licensed under the terms of the GNU GPL, version 2 or later.
13ac90053dSEric Auger  * See the COPYING file in the top-level directory.
14ac90053dSEric Auger  *
15ac90053dSEric Auger  * Implementation of the TIS interface according to specs found at
16ac90053dSEric Auger  * http://www.trustedcomputinggroup.org. This implementation currently
17ac90053dSEric Auger  * supports version 1.3, 21 March 2013
18ac90053dSEric Auger  * In the developers menu choose the PC Client section then find the TIS
19ac90053dSEric Auger  * specification.
20ac90053dSEric Auger  *
21ac90053dSEric Auger  * TPM TIS for TPM 2 implementation following TCG PC Client Platform
22ac90053dSEric Auger  * TPM Profile (PTP) Specification, Familiy 2.0, Revision 00.43
23ac90053dSEric Auger  */
24ac90053dSEric Auger 
25ac90053dSEric Auger #include "qemu/osdep.h"
26ac90053dSEric Auger #include "hw/isa/isa.h"
27ac90053dSEric Auger #include "hw/qdev-properties.h"
28ac90053dSEric Auger #include "migration/vmstate.h"
2940214766SPhilippe Mathieu-Daudé #include "hw/acpi/tpm.h"
30a3500613SPhilippe Mathieu-Daudé #include "tpm_prop.h"
31ac90053dSEric Auger #include "tpm_tis.h"
32*db1015e9SEduardo Habkost #include "qom/object.h"
33ac90053dSEric Auger 
34*db1015e9SEduardo Habkost struct TPMStateISA {
35ac90053dSEric Auger     /*< private >*/
36ac90053dSEric Auger     ISADevice parent_obj;
37ac90053dSEric Auger 
38ac90053dSEric Auger     /*< public >*/
39ac90053dSEric Auger     TPMState state; /* not a QOM object */
40*db1015e9SEduardo Habkost };
41*db1015e9SEduardo Habkost typedef struct TPMStateISA TPMStateISA;
42ac90053dSEric Auger 
43ac90053dSEric Auger #define TPM_TIS_ISA(obj) OBJECT_CHECK(TPMStateISA, (obj), TYPE_TPM_TIS_ISA)
44ac90053dSEric Auger 
45ac90053dSEric Auger static int tpm_tis_pre_save_isa(void *opaque)
46ac90053dSEric Auger {
47ac90053dSEric Auger     TPMStateISA *isadev = opaque;
48ac90053dSEric Auger 
49ac90053dSEric Auger     return tpm_tis_pre_save(&isadev->state);
50ac90053dSEric Auger }
51ac90053dSEric Auger 
52ac90053dSEric Auger static const VMStateDescription vmstate_tpm_tis_isa = {
53ac90053dSEric Auger     .name = "tpm-tis",
54ac90053dSEric Auger     .version_id = 0,
55ac90053dSEric Auger     .pre_save  = tpm_tis_pre_save_isa,
56ac90053dSEric Auger     .fields = (VMStateField[]) {
57ac90053dSEric Auger         VMSTATE_BUFFER(state.buffer, TPMStateISA),
58ac90053dSEric Auger         VMSTATE_UINT16(state.rw_offset, TPMStateISA),
59ac90053dSEric Auger         VMSTATE_UINT8(state.active_locty, TPMStateISA),
60ac90053dSEric Auger         VMSTATE_UINT8(state.aborting_locty, TPMStateISA),
61ac90053dSEric Auger         VMSTATE_UINT8(state.next_locty, TPMStateISA),
62ac90053dSEric Auger 
63ac90053dSEric Auger         VMSTATE_STRUCT_ARRAY(state.loc, TPMStateISA, TPM_TIS_NUM_LOCALITIES, 0,
64ac90053dSEric Auger                              vmstate_locty, TPMLocality),
65ac90053dSEric Auger 
66ac90053dSEric Auger         VMSTATE_END_OF_LIST()
67ac90053dSEric Auger     }
68ac90053dSEric Auger };
69ac90053dSEric Auger 
70ac90053dSEric Auger static void tpm_tis_isa_request_completed(TPMIf *ti, int ret)
71ac90053dSEric Auger {
72ac90053dSEric Auger     TPMStateISA *isadev = TPM_TIS_ISA(ti);
73ac90053dSEric Auger     TPMState *s = &isadev->state;
74ac90053dSEric Auger 
75ac90053dSEric Auger     tpm_tis_request_completed(s, ret);
76ac90053dSEric Auger }
77ac90053dSEric Auger 
78ac90053dSEric Auger static enum TPMVersion tpm_tis_isa_get_tpm_version(TPMIf *ti)
79ac90053dSEric Auger {
80ac90053dSEric Auger     TPMStateISA *isadev = TPM_TIS_ISA(ti);
81ac90053dSEric Auger     TPMState *s = &isadev->state;
82ac90053dSEric Auger 
83ac90053dSEric Auger     return tpm_tis_get_tpm_version(s);
84ac90053dSEric Auger }
85ac90053dSEric Auger 
86ac90053dSEric Auger static void tpm_tis_isa_reset(DeviceState *dev)
87ac90053dSEric Auger {
88ac90053dSEric Auger     TPMStateISA *isadev = TPM_TIS_ISA(dev);
89ac90053dSEric Auger     TPMState *s = &isadev->state;
90ac90053dSEric Auger 
91ac90053dSEric Auger     return tpm_tis_reset(s);
92ac90053dSEric Auger }
93ac90053dSEric Auger 
94ac90053dSEric Auger static Property tpm_tis_isa_properties[] = {
95ac90053dSEric Auger     DEFINE_PROP_UINT32("irq", TPMStateISA, state.irq_num, TPM_TIS_IRQ),
96ac90053dSEric Auger     DEFINE_PROP_TPMBE("tpmdev", TPMStateISA, state.be_driver),
97ac90053dSEric Auger     DEFINE_PROP_BOOL("ppi", TPMStateISA, state.ppi_enabled, true),
98ac90053dSEric Auger     DEFINE_PROP_END_OF_LIST(),
99ac90053dSEric Auger };
100ac90053dSEric Auger 
101ac90053dSEric Auger static void tpm_tis_isa_initfn(Object *obj)
102ac90053dSEric Auger {
103ac90053dSEric Auger     TPMStateISA *isadev = TPM_TIS_ISA(obj);
104ac90053dSEric Auger     TPMState *s = &isadev->state;
105ac90053dSEric Auger 
106ac90053dSEric Auger     memory_region_init_io(&s->mmio, obj, &tpm_tis_memory_ops,
107ac90053dSEric Auger                           s, "tpm-tis-mmio",
108ac90053dSEric Auger                           TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
109ac90053dSEric Auger }
110ac90053dSEric Auger 
111ac90053dSEric Auger static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
112ac90053dSEric Auger {
113ac90053dSEric Auger     TPMStateISA *isadev = TPM_TIS_ISA(dev);
114ac90053dSEric Auger     TPMState *s = &isadev->state;
115ac90053dSEric Auger 
116ac90053dSEric Auger     if (!tpm_find()) {
117ac90053dSEric Auger         error_setg(errp, "at most one TPM device is permitted");
118ac90053dSEric Auger         return;
119ac90053dSEric Auger     }
120ac90053dSEric Auger 
121ac90053dSEric Auger     if (!s->be_driver) {
122ac90053dSEric Auger         error_setg(errp, "'tpmdev' property is required");
123ac90053dSEric Auger         return;
124ac90053dSEric Auger     }
125ac90053dSEric Auger     if (s->irq_num > 15) {
126ac90053dSEric Auger         error_setg(errp, "IRQ %d is outside valid range of 0 to 15",
127ac90053dSEric Auger                    s->irq_num);
128ac90053dSEric Auger         return;
129ac90053dSEric Auger     }
130ac90053dSEric Auger 
131ac90053dSEric Auger     isa_init_irq(ISA_DEVICE(dev), &s->irq, s->irq_num);
132ac90053dSEric Auger 
133ac90053dSEric Auger     memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
134ac90053dSEric Auger                                 TPM_TIS_ADDR_BASE, &s->mmio);
135ac90053dSEric Auger 
136ac90053dSEric Auger     if (s->ppi_enabled) {
137ac90053dSEric Auger         tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
138ac90053dSEric Auger                      TPM_PPI_ADDR_BASE, OBJECT(dev));
139ac90053dSEric Auger     }
140ac90053dSEric Auger }
141ac90053dSEric Auger 
142ac90053dSEric Auger static void tpm_tis_isa_class_init(ObjectClass *klass, void *data)
143ac90053dSEric Auger {
144ac90053dSEric Auger     DeviceClass *dc = DEVICE_CLASS(klass);
145ac90053dSEric Auger     TPMIfClass *tc = TPM_IF_CLASS(klass);
146ac90053dSEric Auger 
147ac90053dSEric Auger     device_class_set_props(dc, tpm_tis_isa_properties);
148ac90053dSEric Auger     dc->vmsd  = &vmstate_tpm_tis_isa;
149ac90053dSEric Auger     tc->model = TPM_MODEL_TPM_TIS;
150ac90053dSEric Auger     dc->realize = tpm_tis_isa_realizefn;
151ac90053dSEric Auger     dc->reset = tpm_tis_isa_reset;
152ac90053dSEric Auger     tc->request_completed = tpm_tis_isa_request_completed;
153ac90053dSEric Auger     tc->get_version = tpm_tis_isa_get_tpm_version;
154ac90053dSEric Auger }
155ac90053dSEric Auger 
156ac90053dSEric Auger static const TypeInfo tpm_tis_isa_info = {
157ac90053dSEric Auger     .name = TYPE_TPM_TIS_ISA,
158ac90053dSEric Auger     .parent = TYPE_ISA_DEVICE,
159ac90053dSEric Auger     .instance_size = sizeof(TPMStateISA),
160ac90053dSEric Auger     .instance_init = tpm_tis_isa_initfn,
161ac90053dSEric Auger     .class_init  = tpm_tis_isa_class_init,
162ac90053dSEric Auger     .interfaces = (InterfaceInfo[]) {
163ac90053dSEric Auger         { TYPE_TPM_IF },
164ac90053dSEric Auger         { }
165ac90053dSEric Auger     }
166ac90053dSEric Auger };
167ac90053dSEric Auger 
168ac90053dSEric Auger static void tpm_tis_isa_register(void)
169ac90053dSEric Auger {
170ac90053dSEric Auger     type_register_static(&tpm_tis_isa_info);
171ac90053dSEric Auger }
172ac90053dSEric Auger 
173ac90053dSEric Auger type_init(tpm_tis_isa_register)
174