xref: /openbmc/qemu/accel/accel-target.c (revision 2492008d0de0380ee910730dc10159a8e29b13a9)
1 /*
2  * QEMU accel class, components common to system emulation and user mode
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 "qemu/target-info.h"
29 
30 #include "cpu.h"
31 #include "accel/accel-cpu-target.h"
32 
33 #ifndef CONFIG_USER_ONLY
34 #include "accel-system.h"
35 #endif /* !CONFIG_USER_ONLY */
36 
37 static const TypeInfo accel_type = {
38     .name = TYPE_ACCEL,
39     .parent = TYPE_OBJECT,
40     .class_size = sizeof(AccelClass),
41     .instance_size = sizeof(AccelState),
42     .abstract = true,
43 };
44 
45 /* Lookup AccelClass from opt_name. Returns NULL if not found */
46 AccelClass *accel_find(const char *opt_name)
47 {
48     char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
49     AccelClass *ac = ACCEL_CLASS(module_object_class_by_name(class_name));
50     g_free(class_name);
51     return ac;
52 }
53 
54 /* Return the name of the current accelerator */
55 const char *current_accel_name(void)
56 {
57     AccelClass *ac = ACCEL_GET_CLASS(current_accel());
58 
59     return ac->name;
60 }
61 
62 static void accel_init_cpu_int_aux(ObjectClass *klass, void *opaque)
63 {
64     CPUClass *cc = CPU_CLASS(klass);
65     AccelCPUClass *accel_cpu = opaque;
66 
67     /*
68      * The first callback allows accel-cpu to run initializations
69      * for the CPU, customizing CPU behavior according to the accelerator.
70      *
71      * The second one allows the CPU to customize the accel-cpu
72      * behavior according to the CPU.
73      *
74      * The second is currently only used by TCG, to specialize the
75      * TCGCPUOps depending on the CPU type.
76      */
77     cc->accel_cpu = accel_cpu;
78     if (accel_cpu->cpu_class_init) {
79         accel_cpu->cpu_class_init(cc);
80     }
81     if (cc->init_accel_cpu) {
82         cc->init_accel_cpu(accel_cpu, cc);
83     }
84 }
85 
86 /* initialize the arch-specific accel CpuClass interfaces */
87 static void accel_init_cpu_interfaces(AccelClass *ac)
88 {
89     const char *ac_name; /* AccelClass name */
90     char *acc_name;      /* AccelCPUClass name */
91     ObjectClass *acc;    /* AccelCPUClass */
92     const char *cpu_resolving_type = target_cpu_type();
93 
94     ac_name = object_class_get_name(OBJECT_CLASS(ac));
95     g_assert(ac_name != NULL);
96 
97     acc_name = g_strdup_printf("%s-%s", ac_name, cpu_resolving_type);
98     acc = object_class_by_name(acc_name);
99     g_free(acc_name);
100 
101     if (acc) {
102         object_class_foreach(accel_init_cpu_int_aux,
103                              cpu_resolving_type, false, acc);
104     }
105 }
106 
107 void accel_init_interfaces(AccelClass *ac)
108 {
109 #ifndef CONFIG_USER_ONLY
110     accel_system_init_ops_interfaces(ac);
111 #endif /* !CONFIG_USER_ONLY */
112 
113     accel_init_cpu_interfaces(ac);
114 }
115 
116 void accel_cpu_instance_init(CPUState *cpu)
117 {
118     if (cpu->cc->accel_cpu && cpu->cc->accel_cpu->cpu_instance_init) {
119         cpu->cc->accel_cpu->cpu_instance_init(cpu);
120     }
121 }
122 
123 bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
124 {
125     AccelState *accel = current_accel();
126     AccelClass *acc = ACCEL_GET_CLASS(accel);
127 
128     /* target specific realization */
129     if (cpu->cc->accel_cpu
130         && cpu->cc->accel_cpu->cpu_target_realize
131         && !cpu->cc->accel_cpu->cpu_target_realize(cpu, errp)) {
132         return false;
133     }
134 
135     /* generic realization */
136     if (acc->cpu_common_realize && !acc->cpu_common_realize(cpu, errp)) {
137         return false;
138     }
139 
140     return true;
141 }
142 
143 void accel_cpu_common_unrealize(CPUState *cpu)
144 {
145     AccelState *accel = current_accel();
146     AccelClass *acc = ACCEL_GET_CLASS(accel);
147 
148     /* generic unrealization */
149     if (acc->cpu_common_unrealize) {
150         acc->cpu_common_unrealize(cpu);
151     }
152 }
153 
154 int accel_supported_gdbstub_sstep_flags(void)
155 {
156     AccelState *accel = current_accel();
157     AccelClass *acc = ACCEL_GET_CLASS(accel);
158     if (acc->gdbstub_supported_sstep_flags) {
159         return acc->gdbstub_supported_sstep_flags();
160     }
161     return 0;
162 }
163 
164 static const TypeInfo accel_cpu_type = {
165     .name = TYPE_ACCEL_CPU,
166     .parent = TYPE_OBJECT,
167     .abstract = true,
168     .class_size = sizeof(AccelCPUClass),
169 };
170 
171 static void register_accel_types(void)
172 {
173     type_register_static(&accel_type);
174     type_register_static(&accel_cpu_type);
175 }
176 
177 type_init(register_accel_types);
178