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 "sysemu/tpm.h"
19 #include "qemu/thread.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "block/thread-pool.h"
23 #include "qemu/error-report.h"
25 static void tpm_backend_request_completed(void *opaque
, int ret
)
27 TPMBackend
*s
= TPM_BACKEND(opaque
);
28 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
30 tic
->request_completed(s
->tpmif
, ret
);
32 /* no need for atomic, as long the BQL is taken */
34 object_unref(OBJECT(s
));
37 static int tpm_backend_worker_thread(gpointer data
)
39 TPMBackend
*s
= TPM_BACKEND(data
);
40 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
43 k
->handle_request(s
, s
->cmd
, &err
);
45 error_report_err(err
);
52 void tpm_backend_finish_sync(TPMBackend
*s
)
55 aio_poll(qemu_get_aio_context(), true);
59 enum TpmType
tpm_backend_get_type(TPMBackend
*s
)
61 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
66 int tpm_backend_init(TPMBackend
*s
, TPMIf
*tpmif
, Error
**errp
)
69 error_setg(errp
, "TPM backend '%s' is already initialized", s
->id
);
74 object_ref(OBJECT(tpmif
));
76 s
->had_startup_error
= false;
81 int tpm_backend_startup_tpm(TPMBackend
*s
, size_t buffersize
)
84 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
86 /* terminate a running TPM */
87 tpm_backend_finish_sync(s
);
89 res
= k
->startup_tpm
? k
->startup_tpm(s
, buffersize
) : 0;
91 s
->had_startup_error
= (res
!= 0);
96 bool tpm_backend_had_startup_error(TPMBackend
*s
)
98 return s
->had_startup_error
;
101 void tpm_backend_deliver_request(TPMBackend
*s
, TPMBackendCmd
*cmd
)
103 ThreadPool
*pool
= aio_get_thread_pool(qemu_get_aio_context());
105 if (s
->cmd
!= NULL
) {
106 error_report("There is a TPM request pending");
111 object_ref(OBJECT(s
));
112 thread_pool_submit_aio(pool
, tpm_backend_worker_thread
, s
,
113 tpm_backend_request_completed
, s
);
116 void tpm_backend_reset(TPMBackend
*s
)
118 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
124 tpm_backend_finish_sync(s
);
126 s
->had_startup_error
= false;
129 void tpm_backend_cancel_cmd(TPMBackend
*s
)
131 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
136 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
138 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
140 return k
->get_tpm_established_flag
?
141 k
->get_tpm_established_flag(s
) : false;
144 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
146 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
148 return k
->reset_tpm_established_flag
?
149 k
->reset_tpm_established_flag(s
, locty
) : 0;
152 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
154 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
156 return k
->get_tpm_version(s
);
159 size_t tpm_backend_get_buffer_size(TPMBackend
*s
)
161 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
163 return k
->get_buffer_size(s
);
166 TPMInfo
*tpm_backend_query_tpm(TPMBackend
*s
)
168 TPMInfo
*info
= g_new0(TPMInfo
, 1);
169 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
170 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
172 info
->id
= g_strdup(s
->id
);
173 info
->model
= tic
->model
;
174 info
->options
= k
->get_tpm_options(s
);
179 static void tpm_backend_instance_finalize(Object
*obj
)
181 TPMBackend
*s
= TPM_BACKEND(obj
);
183 object_unref(OBJECT(s
->tpmif
));
187 static const TypeInfo tpm_backend_info
= {
188 .name
= TYPE_TPM_BACKEND
,
189 .parent
= TYPE_OBJECT
,
190 .instance_size
= sizeof(TPMBackend
),
191 .instance_finalize
= tpm_backend_instance_finalize
,
192 .class_size
= sizeof(TPMBackendClass
),
196 static const TypeInfo tpm_if_info
= {
198 .parent
= TYPE_INTERFACE
,
199 .class_size
= sizeof(TPMIfClass
),
202 static void register_types(void)
204 type_register_static(&tpm_backend_info
);
205 type_register_static(&tpm_if_info
);
208 type_init(register_types
);