4 * Copyright IBM, Corp. 2013
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 "qemu/thread.h"
21 #include "qemu/main-loop.h"
23 static void tpm_backend_request_completed_bh(void *opaque
)
25 TPMBackend
*s
= TPM_BACKEND(opaque
);
26 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
28 tic
->request_completed(s
->tpmif
);
31 static void tpm_backend_worker_thread(gpointer data
, gpointer user_data
)
33 TPMBackend
*s
= TPM_BACKEND(user_data
);
34 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
36 k
->handle_request(s
, (TPMBackendCmd
*)data
);
38 qemu_bh_schedule(s
->bh
);
41 static void tpm_backend_thread_end(TPMBackend
*s
)
44 g_thread_pool_free(s
->thread_pool
, FALSE
, TRUE
);
45 s
->thread_pool
= NULL
;
49 enum TpmType
tpm_backend_get_type(TPMBackend
*s
)
51 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
56 int tpm_backend_init(TPMBackend
*s
, TPMIf
*tpmif
, Error
**errp
)
59 error_setg(errp
, "TPM backend '%s' is already initialized", s
->id
);
64 object_ref(OBJECT(tpmif
));
66 s
->had_startup_error
= false;
71 int tpm_backend_startup_tpm(TPMBackend
*s
, size_t buffersize
)
74 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
76 /* terminate a running TPM */
77 tpm_backend_thread_end(s
);
79 s
->thread_pool
= g_thread_pool_new(tpm_backend_worker_thread
, s
, 1, TRUE
,
82 res
= k
->startup_tpm
? k
->startup_tpm(s
, buffersize
) : 0;
84 s
->had_startup_error
= (res
!= 0);
89 bool tpm_backend_had_startup_error(TPMBackend
*s
)
91 return s
->had_startup_error
;
94 void tpm_backend_deliver_request(TPMBackend
*s
, TPMBackendCmd
*cmd
)
96 g_thread_pool_push(s
->thread_pool
, cmd
, NULL
);
99 void tpm_backend_reset(TPMBackend
*s
)
101 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
107 tpm_backend_thread_end(s
);
109 s
->had_startup_error
= false;
112 void tpm_backend_cancel_cmd(TPMBackend
*s
)
114 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
119 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
121 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
123 return k
->get_tpm_established_flag
?
124 k
->get_tpm_established_flag(s
) : false;
127 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
129 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
131 return k
->reset_tpm_established_flag
?
132 k
->reset_tpm_established_flag(s
, locty
) : 0;
135 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
137 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
139 return k
->get_tpm_version(s
);
142 size_t tpm_backend_get_buffer_size(TPMBackend
*s
)
144 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
146 return k
->get_buffer_size(s
);
149 TPMInfo
*tpm_backend_query_tpm(TPMBackend
*s
)
151 TPMInfo
*info
= g_new0(TPMInfo
, 1);
152 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
153 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
155 info
->id
= g_strdup(s
->id
);
156 info
->model
= tic
->model
;
157 info
->options
= k
->get_tpm_options(s
);
162 static void tpm_backend_instance_init(Object
*obj
)
164 TPMBackend
*s
= TPM_BACKEND(obj
);
166 s
->bh
= qemu_bh_new(tpm_backend_request_completed_bh
, s
);
169 static void tpm_backend_instance_finalize(Object
*obj
)
171 TPMBackend
*s
= TPM_BACKEND(obj
);
173 object_unref(OBJECT(s
->tpmif
));
175 tpm_backend_thread_end(s
);
176 qemu_bh_delete(s
->bh
);
179 static const TypeInfo tpm_backend_info
= {
180 .name
= TYPE_TPM_BACKEND
,
181 .parent
= TYPE_OBJECT
,
182 .instance_size
= sizeof(TPMBackend
),
183 .instance_init
= tpm_backend_instance_init
,
184 .instance_finalize
= tpm_backend_instance_finalize
,
185 .class_size
= sizeof(TPMBackendClass
),
189 static const TypeInfo tpm_if_info
= {
191 .parent
= TYPE_INTERFACE
,
192 .class_size
= sizeof(TPMIfClass
),
195 static void register_types(void)
197 type_register_static(&tpm_backend_info
);
198 type_register_static(&tpm_if_info
);
201 type_init(register_types
);