tests: add test to check VirtQueue object
[qemu.git] / backends / tpm.c
blob5763f6f369fb235529d23e77dcceff7239d81cf3
1 /*
2 * QEMU TPM Backend
4 * Copyright IBM, Corp. 2013
6 * Authors:
7 * Stefan Berger <stefanb@us.ibm.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 * Based on backends/rng.c by Anthony Liguori
15 #include "qemu/osdep.h"
16 #include "sysemu/tpm_backend.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "sysemu/tpm.h"
20 #include "hw/tpm/tpm_int.h"
21 #include "qemu/thread.h"
23 static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
25 TPMBackend *s = TPM_BACKEND(user_data);
26 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
28 assert(k->handle_request != NULL);
29 k->handle_request(s, (TPMBackendCmd *)data);
32 static void tpm_backend_thread_end(TPMBackend *s)
34 if (s->thread_pool) {
35 g_thread_pool_free(s->thread_pool, FALSE, TRUE);
36 s->thread_pool = NULL;
40 enum TpmType tpm_backend_get_type(TPMBackend *s)
42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
44 return k->type;
47 int tpm_backend_init(TPMBackend *s, TPMState *state)
49 s->tpm_state = state;
50 s->had_startup_error = false;
52 return 0;
55 int tpm_backend_startup_tpm(TPMBackend *s)
57 int res = 0;
58 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
60 /* terminate a running TPM */
61 tpm_backend_thread_end(s);
63 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
64 NULL);
66 res = k->startup_tpm ? k->startup_tpm(s) : 0;
68 s->had_startup_error = (res != 0);
70 return res;
73 bool tpm_backend_had_startup_error(TPMBackend *s)
75 return s->had_startup_error;
78 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
80 g_thread_pool_push(s->thread_pool, cmd, NULL);
83 void tpm_backend_reset(TPMBackend *s)
85 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
87 if (k->reset) {
88 k->reset(s);
91 tpm_backend_thread_end(s);
93 s->had_startup_error = false;
96 void tpm_backend_cancel_cmd(TPMBackend *s)
98 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
100 assert(k->cancel_cmd);
102 k->cancel_cmd(s);
105 bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
107 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
109 return k->get_tpm_established_flag ?
110 k->get_tpm_established_flag(s) : false;
113 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
115 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
117 return k->reset_tpm_established_flag ?
118 k->reset_tpm_established_flag(s, locty) : 0;
121 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
123 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
125 assert(k->get_tpm_version);
127 return k->get_tpm_version(s);
130 TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
132 TPMInfo *info = g_new0(TPMInfo, 1);
133 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
135 info->id = g_strdup(s->id);
136 info->model = s->fe_model;
137 if (k->get_tpm_options) {
138 info->options = k->get_tpm_options(s);
141 return info;
144 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
146 TPMBackend *s = TPM_BACKEND(obj);
148 return s->opened;
151 void tpm_backend_open(TPMBackend *s, Error **errp)
153 object_property_set_bool(OBJECT(s), true, "opened", errp);
156 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
158 TPMBackend *s = TPM_BACKEND(obj);
159 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
160 Error *local_err = NULL;
162 if (value == s->opened) {
163 return;
166 if (!value && s->opened) {
167 error_setg(errp, QERR_PERMISSION_DENIED);
168 return;
171 if (k->opened) {
172 k->opened(s, &local_err);
173 if (local_err) {
174 error_propagate(errp, local_err);
175 return;
179 s->opened = true;
182 static void tpm_backend_instance_init(Object *obj)
184 TPMBackend *s = TPM_BACKEND(obj);
186 object_property_add_bool(obj, "opened",
187 tpm_backend_prop_get_opened,
188 tpm_backend_prop_set_opened,
189 NULL);
190 s->fe_model = -1;
193 static void tpm_backend_instance_finalize(Object *obj)
195 TPMBackend *s = TPM_BACKEND(obj);
197 g_free(s->id);
198 tpm_backend_thread_end(s);
201 static const TypeInfo tpm_backend_info = {
202 .name = TYPE_TPM_BACKEND,
203 .parent = TYPE_OBJECT,
204 .instance_size = sizeof(TPMBackend),
205 .instance_init = tpm_backend_instance_init,
206 .instance_finalize = tpm_backend_instance_finalize,
207 .class_size = sizeof(TPMBackendClass),
208 .abstract = true,
211 static const TypeInfo tpm_if_info = {
212 .name = TYPE_TPM_IF,
213 .parent = TYPE_INTERFACE,
214 .class_size = sizeof(TPMIfClass),
217 static void register_types(void)
219 type_register_static(&tpm_backend_info);
220 type_register_static(&tpm_if_info);
223 type_init(register_types);