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 "qapi/qapi-commands-accelerator.h"
29 #include "monitor/monitor.h"
30 #include "hw/boards.h"
31 #include "hw/core/cpu.h"
32 #include "accel/accel-ops.h"
33 #include "accel/accel-cpu-ops.h"
34 #include "system/cpus.h"
35 #include "qemu/error-report.h"
36 #include "accel-internal.h"
37
accel_init_machine(AccelState * accel,MachineState * ms)38 int accel_init_machine(AccelState *accel, MachineState *ms)
39 {
40 AccelClass *acc = ACCEL_GET_CLASS(accel);
41 int ret;
42 ms->accelerator = accel;
43 *(acc->allowed) = true;
44 ret = acc->init_machine(accel, ms);
45 if (ret < 0) {
46 ms->accelerator = NULL;
47 *(acc->allowed) = false;
48 object_unref(OBJECT(accel));
49 } else {
50 object_set_accelerator_compat_props(acc->compat_props);
51 }
52 return ret;
53 }
54
current_accel(void)55 AccelState *current_accel(void)
56 {
57 return current_machine->accelerator;
58 }
59
accel_setup_post(MachineState * ms)60 void accel_setup_post(MachineState *ms)
61 {
62 AccelState *accel = ms->accelerator;
63 AccelClass *acc = ACCEL_GET_CLASS(accel);
64 if (acc->setup_post) {
65 acc->setup_post(accel);
66 }
67 }
68
accel_pre_resume(MachineState * ms,bool step_pending)69 void accel_pre_resume(MachineState *ms, bool step_pending)
70 {
71 AccelState *accel = ms->accelerator;
72 AccelClass *acc = ACCEL_GET_CLASS(accel);
73 if (acc->pre_resume_vm) {
74 acc->pre_resume_vm(accel, step_pending);
75 }
76 }
77
78 /* initialize the arch-independent accel operation interfaces */
accel_init_ops_interfaces(AccelClass * ac)79 void accel_init_ops_interfaces(AccelClass *ac)
80 {
81 const char *ac_name;
82 char *ops_name;
83 ObjectClass *oc;
84 AccelOpsClass *ops;
85
86 ac_name = object_class_get_name(OBJECT_CLASS(ac));
87 g_assert(ac_name != NULL);
88
89 ops_name = g_strdup_printf("%s" ACCEL_OPS_SUFFIX, ac_name);
90 oc = module_object_class_by_name(ops_name);
91 if (!oc) {
92 error_report("fatal: could not load module for type '%s'", ops_name);
93 exit(1);
94 }
95 g_free(ops_name);
96 /*
97 * all accelerators need to define ops, providing at least a mandatory
98 * non-NULL create_vcpu_thread operation.
99 */
100 ops = ACCEL_OPS_CLASS(oc);
101 ac->ops = ops;
102 if (ops->ops_init) {
103 ops->ops_init(ac);
104 }
105 cpus_register_accel(ops);
106 }
107
accel_ops_class_init(ObjectClass * oc,const void * data)108 static void accel_ops_class_init(ObjectClass *oc, const void *data)
109 {
110 monitor_register_hmp_info_hrt("accel", qmp_x_accel_stats);
111 }
112
113 static const TypeInfo accel_ops_type_info = {
114 .name = TYPE_ACCEL_OPS,
115 .parent = TYPE_OBJECT,
116 .abstract = true,
117 .class_size = sizeof(AccelOpsClass),
118 .class_init = accel_ops_class_init,
119 };
120
accel_system_register_types(void)121 static void accel_system_register_types(void)
122 {
123 type_register_static(&accel_ops_type_info);
124 }
125 type_init(accel_system_register_types);
126