xref: /openbmc/qemu/hw/core/machine.c (revision c964b660)
1 /*
2  * QEMU Machine
3  *
4  * Copyright (C) 2014 Red Hat Inc
5  *
6  * Authors:
7  *   Marcel Apfelbaum <marcel.a@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "hw/boards.h"
15 #include "qapi-visit.h"
16 #include "qapi/visitor.h"
17 #include "hw/sysbus.h"
18 #include "sysemu/sysemu.h"
19 #include "qemu/error-report.h"
20 
21 static char *machine_get_accel(Object *obj, Error **errp)
22 {
23     MachineState *ms = MACHINE(obj);
24 
25     return g_strdup(ms->accel);
26 }
27 
28 static void machine_set_accel(Object *obj, const char *value, Error **errp)
29 {
30     MachineState *ms = MACHINE(obj);
31 
32     g_free(ms->accel);
33     ms->accel = g_strdup(value);
34 }
35 
36 static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
37                                        const char *name, void *opaque,
38                                        Error **errp)
39 {
40     Error *err = NULL;
41     MachineState *ms = MACHINE(obj);
42     OnOffSplit mode;
43 
44     visit_type_OnOffSplit(v, name, &mode, &err);
45     if (err) {
46         error_propagate(errp, err);
47         return;
48     } else {
49         switch (mode) {
50         case ON_OFF_SPLIT_ON:
51             ms->kernel_irqchip_allowed = true;
52             ms->kernel_irqchip_required = true;
53             ms->kernel_irqchip_split = false;
54             break;
55         case ON_OFF_SPLIT_OFF:
56             ms->kernel_irqchip_allowed = false;
57             ms->kernel_irqchip_required = false;
58             ms->kernel_irqchip_split = false;
59             break;
60         case ON_OFF_SPLIT_SPLIT:
61             ms->kernel_irqchip_allowed = true;
62             ms->kernel_irqchip_required = true;
63             ms->kernel_irqchip_split = true;
64             break;
65         default:
66             abort();
67         }
68     }
69 }
70 
71 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
72                                        const char *name, void *opaque,
73                                        Error **errp)
74 {
75     MachineState *ms = MACHINE(obj);
76     int64_t value = ms->kvm_shadow_mem;
77 
78     visit_type_int(v, name, &value, errp);
79 }
80 
81 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
82                                        const char *name, void *opaque,
83                                        Error **errp)
84 {
85     MachineState *ms = MACHINE(obj);
86     Error *error = NULL;
87     int64_t value;
88 
89     visit_type_int(v, name, &value, &error);
90     if (error) {
91         error_propagate(errp, error);
92         return;
93     }
94 
95     ms->kvm_shadow_mem = value;
96 }
97 
98 static char *machine_get_kernel(Object *obj, Error **errp)
99 {
100     MachineState *ms = MACHINE(obj);
101 
102     return g_strdup(ms->kernel_filename);
103 }
104 
105 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
106 {
107     MachineState *ms = MACHINE(obj);
108 
109     g_free(ms->kernel_filename);
110     ms->kernel_filename = g_strdup(value);
111 }
112 
113 static char *machine_get_initrd(Object *obj, Error **errp)
114 {
115     MachineState *ms = MACHINE(obj);
116 
117     return g_strdup(ms->initrd_filename);
118 }
119 
120 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
121 {
122     MachineState *ms = MACHINE(obj);
123 
124     g_free(ms->initrd_filename);
125     ms->initrd_filename = g_strdup(value);
126 }
127 
128 static char *machine_get_append(Object *obj, Error **errp)
129 {
130     MachineState *ms = MACHINE(obj);
131 
132     return g_strdup(ms->kernel_cmdline);
133 }
134 
135 static void machine_set_append(Object *obj, const char *value, Error **errp)
136 {
137     MachineState *ms = MACHINE(obj);
138 
139     g_free(ms->kernel_cmdline);
140     ms->kernel_cmdline = g_strdup(value);
141 }
142 
143 static char *machine_get_dtb(Object *obj, Error **errp)
144 {
145     MachineState *ms = MACHINE(obj);
146 
147     return g_strdup(ms->dtb);
148 }
149 
150 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
151 {
152     MachineState *ms = MACHINE(obj);
153 
154     g_free(ms->dtb);
155     ms->dtb = g_strdup(value);
156 }
157 
158 static char *machine_get_dumpdtb(Object *obj, Error **errp)
159 {
160     MachineState *ms = MACHINE(obj);
161 
162     return g_strdup(ms->dumpdtb);
163 }
164 
165 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
166 {
167     MachineState *ms = MACHINE(obj);
168 
169     g_free(ms->dumpdtb);
170     ms->dumpdtb = g_strdup(value);
171 }
172 
173 static void machine_get_phandle_start(Object *obj, Visitor *v,
174                                       const char *name, void *opaque,
175                                       Error **errp)
176 {
177     MachineState *ms = MACHINE(obj);
178     int64_t value = ms->phandle_start;
179 
180     visit_type_int(v, name, &value, errp);
181 }
182 
183 static void machine_set_phandle_start(Object *obj, Visitor *v,
184                                       const char *name, void *opaque,
185                                       Error **errp)
186 {
187     MachineState *ms = MACHINE(obj);
188     Error *error = NULL;
189     int64_t value;
190 
191     visit_type_int(v, name, &value, &error);
192     if (error) {
193         error_propagate(errp, error);
194         return;
195     }
196 
197     ms->phandle_start = value;
198 }
199 
200 static char *machine_get_dt_compatible(Object *obj, Error **errp)
201 {
202     MachineState *ms = MACHINE(obj);
203 
204     return g_strdup(ms->dt_compatible);
205 }
206 
207 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
208 {
209     MachineState *ms = MACHINE(obj);
210 
211     g_free(ms->dt_compatible);
212     ms->dt_compatible = g_strdup(value);
213 }
214 
215 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
216 {
217     MachineState *ms = MACHINE(obj);
218 
219     return ms->dump_guest_core;
220 }
221 
222 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
223 {
224     MachineState *ms = MACHINE(obj);
225 
226     ms->dump_guest_core = value;
227 }
228 
229 static bool machine_get_mem_merge(Object *obj, Error **errp)
230 {
231     MachineState *ms = MACHINE(obj);
232 
233     return ms->mem_merge;
234 }
235 
236 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
237 {
238     MachineState *ms = MACHINE(obj);
239 
240     ms->mem_merge = value;
241 }
242 
243 static bool machine_get_usb(Object *obj, Error **errp)
244 {
245     MachineState *ms = MACHINE(obj);
246 
247     return ms->usb;
248 }
249 
250 static void machine_set_usb(Object *obj, bool value, Error **errp)
251 {
252     MachineState *ms = MACHINE(obj);
253 
254     ms->usb = value;
255     ms->usb_disabled = !value;
256 }
257 
258 static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
259 {
260     MachineState *ms = MACHINE(obj);
261 
262     return ms->igd_gfx_passthru;
263 }
264 
265 static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
266 {
267     MachineState *ms = MACHINE(obj);
268 
269     ms->igd_gfx_passthru = value;
270 }
271 
272 static char *machine_get_firmware(Object *obj, Error **errp)
273 {
274     MachineState *ms = MACHINE(obj);
275 
276     return g_strdup(ms->firmware);
277 }
278 
279 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
280 {
281     MachineState *ms = MACHINE(obj);
282 
283     g_free(ms->firmware);
284     ms->firmware = g_strdup(value);
285 }
286 
287 static bool machine_get_iommu(Object *obj, Error **errp)
288 {
289     MachineState *ms = MACHINE(obj);
290 
291     return ms->iommu;
292 }
293 
294 static void machine_set_iommu(Object *obj, bool value, Error **errp)
295 {
296     MachineState *ms = MACHINE(obj);
297 
298     ms->iommu = value;
299 }
300 
301 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
302 {
303     MachineState *ms = MACHINE(obj);
304 
305     ms->suppress_vmdesc = value;
306 }
307 
308 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
309 {
310     MachineState *ms = MACHINE(obj);
311 
312     return ms->suppress_vmdesc;
313 }
314 
315 static int error_on_sysbus_device(SysBusDevice *sbdev, void *opaque)
316 {
317     error_report("Option '-device %s' cannot be handled by this machine",
318                  object_class_get_name(object_get_class(OBJECT(sbdev))));
319     exit(1);
320 }
321 
322 static void machine_init_notify(Notifier *notifier, void *data)
323 {
324     Object *machine = qdev_get_machine();
325     ObjectClass *oc = object_get_class(machine);
326     MachineClass *mc = MACHINE_CLASS(oc);
327 
328     if (mc->has_dynamic_sysbus) {
329         /* Our machine can handle dynamic sysbus devices, we're all good */
330         return;
331     }
332 
333     /*
334      * Loop through all dynamically created devices and check whether there
335      * are sysbus devices among them. If there are, error out.
336      */
337     foreach_dynamic_sysbus_device(error_on_sysbus_device, NULL);
338 }
339 
340 static void machine_class_init(ObjectClass *oc, void *data)
341 {
342     MachineClass *mc = MACHINE_CLASS(oc);
343 
344     /* Default 128 MB as guest ram size */
345     mc->default_ram_size = 128 * M_BYTE;
346     mc->rom_file_has_mr = true;
347 }
348 
349 static void machine_class_base_init(ObjectClass *oc, void *data)
350 {
351     if (!object_class_is_abstract(oc)) {
352         MachineClass *mc = MACHINE_CLASS(oc);
353         const char *cname = object_class_get_name(oc);
354         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
355         mc->name = g_strndup(cname,
356                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
357     }
358 }
359 
360 static void machine_initfn(Object *obj)
361 {
362     MachineState *ms = MACHINE(obj);
363 
364     ms->kernel_irqchip_allowed = true;
365     ms->kvm_shadow_mem = -1;
366     ms->dump_guest_core = true;
367     ms->mem_merge = true;
368 
369     object_property_add_str(obj, "accel",
370                             machine_get_accel, machine_set_accel, NULL);
371     object_property_set_description(obj, "accel",
372                                     "Accelerator list",
373                                     NULL);
374     object_property_add(obj, "kernel-irqchip", "OnOffSplit",
375                         NULL,
376                         machine_set_kernel_irqchip,
377                         NULL, NULL, NULL);
378     object_property_set_description(obj, "kernel-irqchip",
379                                     "Configure KVM in-kernel irqchip",
380                                     NULL);
381     object_property_add(obj, "kvm-shadow-mem", "int",
382                         machine_get_kvm_shadow_mem,
383                         machine_set_kvm_shadow_mem,
384                         NULL, NULL, NULL);
385     object_property_set_description(obj, "kvm-shadow-mem",
386                                     "KVM shadow MMU size",
387                                     NULL);
388     object_property_add_str(obj, "kernel",
389                             machine_get_kernel, machine_set_kernel, NULL);
390     object_property_set_description(obj, "kernel",
391                                     "Linux kernel image file",
392                                     NULL);
393     object_property_add_str(obj, "initrd",
394                             machine_get_initrd, machine_set_initrd, NULL);
395     object_property_set_description(obj, "initrd",
396                                     "Linux initial ramdisk file",
397                                     NULL);
398     object_property_add_str(obj, "append",
399                             machine_get_append, machine_set_append, NULL);
400     object_property_set_description(obj, "append",
401                                     "Linux kernel command line",
402                                     NULL);
403     object_property_add_str(obj, "dtb",
404                             machine_get_dtb, machine_set_dtb, NULL);
405     object_property_set_description(obj, "dtb",
406                                     "Linux kernel device tree file",
407                                     NULL);
408     object_property_add_str(obj, "dumpdtb",
409                             machine_get_dumpdtb, machine_set_dumpdtb, NULL);
410     object_property_set_description(obj, "dumpdtb",
411                                     "Dump current dtb to a file and quit",
412                                     NULL);
413     object_property_add(obj, "phandle-start", "int",
414                         machine_get_phandle_start,
415                         machine_set_phandle_start,
416                         NULL, NULL, NULL);
417     object_property_set_description(obj, "phandle-start",
418                                     "The first phandle ID we may generate dynamically",
419                                     NULL);
420     object_property_add_str(obj, "dt-compatible",
421                             machine_get_dt_compatible,
422                             machine_set_dt_compatible,
423                             NULL);
424     object_property_set_description(obj, "dt-compatible",
425                                     "Overrides the \"compatible\" property of the dt root node",
426                                     NULL);
427     object_property_add_bool(obj, "dump-guest-core",
428                              machine_get_dump_guest_core,
429                              machine_set_dump_guest_core,
430                              NULL);
431     object_property_set_description(obj, "dump-guest-core",
432                                     "Include guest memory in  a core dump",
433                                     NULL);
434     object_property_add_bool(obj, "mem-merge",
435                              machine_get_mem_merge,
436                              machine_set_mem_merge, NULL);
437     object_property_set_description(obj, "mem-merge",
438                                     "Enable/disable memory merge support",
439                                     NULL);
440     object_property_add_bool(obj, "usb",
441                              machine_get_usb,
442                              machine_set_usb, NULL);
443     object_property_set_description(obj, "usb",
444                                     "Set on/off to enable/disable usb",
445                                     NULL);
446     object_property_add_bool(obj, "igd-passthru",
447                              machine_get_igd_gfx_passthru,
448                              machine_set_igd_gfx_passthru, NULL);
449     object_property_set_description(obj, "igd-passthru",
450                                     "Set on/off to enable/disable igd passthrou",
451                                     NULL);
452     object_property_add_str(obj, "firmware",
453                             machine_get_firmware,
454                             machine_set_firmware, NULL);
455     object_property_set_description(obj, "firmware",
456                                     "Firmware image",
457                                     NULL);
458     object_property_add_bool(obj, "iommu",
459                              machine_get_iommu,
460                              machine_set_iommu, NULL);
461     object_property_set_description(obj, "iommu",
462                                     "Set on/off to enable/disable Intel IOMMU (VT-d)",
463                                     NULL);
464     object_property_add_bool(obj, "suppress-vmdesc",
465                              machine_get_suppress_vmdesc,
466                              machine_set_suppress_vmdesc, NULL);
467     object_property_set_description(obj, "suppress-vmdesc",
468                                     "Set on to disable self-describing migration",
469                                     NULL);
470 
471     /* Register notifier when init is done for sysbus sanity checks */
472     ms->sysbus_notifier.notify = machine_init_notify;
473     qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
474 }
475 
476 static void machine_finalize(Object *obj)
477 {
478     MachineState *ms = MACHINE(obj);
479 
480     g_free(ms->accel);
481     g_free(ms->kernel_filename);
482     g_free(ms->initrd_filename);
483     g_free(ms->kernel_cmdline);
484     g_free(ms->dtb);
485     g_free(ms->dumpdtb);
486     g_free(ms->dt_compatible);
487     g_free(ms->firmware);
488 }
489 
490 bool machine_usb(MachineState *machine)
491 {
492     return machine->usb;
493 }
494 
495 bool machine_kernel_irqchip_allowed(MachineState *machine)
496 {
497     return machine->kernel_irqchip_allowed;
498 }
499 
500 bool machine_kernel_irqchip_required(MachineState *machine)
501 {
502     return machine->kernel_irqchip_required;
503 }
504 
505 bool machine_kernel_irqchip_split(MachineState *machine)
506 {
507     return machine->kernel_irqchip_split;
508 }
509 
510 int machine_kvm_shadow_mem(MachineState *machine)
511 {
512     return machine->kvm_shadow_mem;
513 }
514 
515 int machine_phandle_start(MachineState *machine)
516 {
517     return machine->phandle_start;
518 }
519 
520 bool machine_dump_guest_core(MachineState *machine)
521 {
522     return machine->dump_guest_core;
523 }
524 
525 bool machine_mem_merge(MachineState *machine)
526 {
527     return machine->mem_merge;
528 }
529 
530 static const TypeInfo machine_info = {
531     .name = TYPE_MACHINE,
532     .parent = TYPE_OBJECT,
533     .abstract = true,
534     .class_size = sizeof(MachineClass),
535     .class_init    = machine_class_init,
536     .class_base_init = machine_class_base_init,
537     .instance_size = sizeof(MachineState),
538     .instance_init = machine_initfn,
539     .instance_finalize = machine_finalize,
540 };
541 
542 static void machine_register_types(void)
543 {
544     type_register_static(&machine_info);
545 }
546 
547 type_init(machine_register_types)
548