xref: /openbmc/qemu/accel/accel-system.c (revision f7a7e7dd2179e7189064e0b7b637e6906617221f)
1 /*
2  * QEMU accel class, system emulation components
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2014 Red Hat Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #include "qemu/osdep.h"
27 #include "qemu/accel.h"
28 #include "hw/boards.h"
29 #include "accel/accel-ops.h"
30 #include "accel/accel-cpu-ops.h"
31 #include "system/cpus.h"
32 #include "qemu/error-report.h"
33 #include "accel-internal.h"
34 
35 int accel_init_machine(AccelState *accel, MachineState *ms)
36 {
37     AccelClass *acc = ACCEL_GET_CLASS(accel);
38     int ret;
39     ms->accelerator = accel;
40     *(acc->allowed) = true;
41     ret = acc->init_machine(accel, ms);
42     if (ret < 0) {
43         ms->accelerator = NULL;
44         *(acc->allowed) = false;
45         object_unref(OBJECT(accel));
46     } else {
47         object_set_accelerator_compat_props(acc->compat_props);
48     }
49     return ret;
50 }
51 
52 AccelState *current_accel(void)
53 {
54     return current_machine->accelerator;
55 }
56 
57 void accel_setup_post(MachineState *ms)
58 {
59     AccelState *accel = ms->accelerator;
60     AccelClass *acc = ACCEL_GET_CLASS(accel);
61     if (acc->setup_post) {
62         acc->setup_post(accel);
63     }
64 }
65 
66 void accel_pre_resume(MachineState *ms, bool step_pending)
67 {
68     AccelState *accel = ms->accelerator;
69     AccelClass *acc = ACCEL_GET_CLASS(accel);
70     if (acc->pre_resume_vm) {
71         acc->pre_resume_vm(accel, step_pending);
72     }
73 }
74 
75 /* initialize the arch-independent accel operation interfaces */
76 void accel_init_ops_interfaces(AccelClass *ac)
77 {
78     const char *ac_name;
79     char *ops_name;
80     ObjectClass *oc;
81     AccelOpsClass *ops;
82 
83     ac_name = object_class_get_name(OBJECT_CLASS(ac));
84     g_assert(ac_name != NULL);
85 
86     ops_name = g_strdup_printf("%s" ACCEL_OPS_SUFFIX, ac_name);
87     oc = module_object_class_by_name(ops_name);
88     if (!oc) {
89         error_report("fatal: could not load module for type '%s'", ops_name);
90         exit(1);
91     }
92     g_free(ops_name);
93     /*
94      * all accelerators need to define ops, providing at least a mandatory
95      * non-NULL create_vcpu_thread operation.
96      */
97     ops = ACCEL_OPS_CLASS(oc);
98     ac->ops = ops;
99     if (ops->ops_init) {
100         ops->ops_init(ac);
101     }
102     cpus_register_accel(ops);
103 }
104 
105 static const TypeInfo accel_ops_type_info = {
106     .name = TYPE_ACCEL_OPS,
107     .parent = TYPE_OBJECT,
108     .abstract = true,
109     .class_size = sizeof(AccelOpsClass),
110 };
111 
112 static void accel_system_register_types(void)
113 {
114     type_register_static(&accel_ops_type_info);
115 }
116 type_init(accel_system_register_types);
117