xref: /qemu/include/hw/boards.h (revision 1f2355f5)
1 /* Declarations for use by board files for creating devices.  */
2 
3 #ifndef HW_BOARDS_H
4 #define HW_BOARDS_H
5 
6 #include "exec/memory.h"
7 #include "sysemu/hostmem.h"
8 #include "sysemu/blockdev.h"
9 #include "qapi/qapi-types-machine.h"
10 #include "qemu/module.h"
11 #include "qom/object.h"
12 #include "hw/core/cpu.h"
13 
14 #define TYPE_MACHINE_SUFFIX "-machine"
15 
16 /* Machine class name that needs to be used for class-name-based machine
17  * type lookup to work.
18  */
19 #define MACHINE_TYPE_NAME(machinename) (machinename TYPE_MACHINE_SUFFIX)
20 
21 #define TYPE_MACHINE "machine"
22 #undef MACHINE  /* BSD defines it and QEMU does not use it */
23 OBJECT_DECLARE_TYPE(MachineState, MachineClass, MACHINE)
24 
25 extern MachineState *current_machine;
26 
27 /**
28  * machine_class_default_cpu_type: Return the machine default CPU type.
29  * @mc: Machine class
30  */
31 const char *machine_class_default_cpu_type(MachineClass *mc);
32 
33 void machine_add_audiodev_property(MachineClass *mc);
34 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp);
35 bool machine_usb(MachineState *machine);
36 int machine_phandle_start(MachineState *machine);
37 bool machine_dump_guest_core(MachineState *machine);
38 bool machine_mem_merge(MachineState *machine);
39 bool machine_require_guest_memfd(MachineState *machine);
40 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
41 void machine_set_cpu_numa_node(MachineState *machine,
42                                const CpuInstanceProperties *props,
43                                Error **errp);
44 void machine_parse_smp_config(MachineState *ms,
45                               const SMPConfiguration *config, Error **errp);
46 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms);
47 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms);
48 void machine_memory_devices_init(MachineState *ms, hwaddr base, uint64_t size);
49 
50 /**
51  * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices
52  * @mc: Machine class
53  * @type: type to allow (should be a subtype of TYPE_SYS_BUS_DEVICE)
54  *
55  * Add the QOM type @type to the list of devices of which are subtypes
56  * of TYPE_SYS_BUS_DEVICE but which are still permitted to be dynamically
57  * created (eg by the user on the command line with -device).
58  * By default if the user tries to create any devices on the command line
59  * that are subtypes of TYPE_SYS_BUS_DEVICE they will get an error message;
60  * for the special cases which are permitted for this machine model, the
61  * machine model class init code must call this function to add them
62  * to the list of specifically permitted devices.
63  */
64 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
65 
66 /**
67  * device_type_is_dynamic_sysbus: Check if type is an allowed sysbus device
68  * type for the machine class.
69  * @mc: Machine class
70  * @type: type to check (should be a subtype of TYPE_SYS_BUS_DEVICE)
71  *
72  * Returns: true if @type is a type in the machine's list of
73  * dynamically pluggable sysbus devices; otherwise false.
74  *
75  * Check if the QOM type @type is in the list of allowed sysbus device
76  * types (see machine_class_allowed_dynamic_sysbus_dev()).
77  * Note that if @type has a parent type in the list, it is allowed too.
78  */
79 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type);
80 
81 /**
82  * device_is_dynamic_sysbus: test whether device is a dynamic sysbus device
83  * @mc: Machine class
84  * @dev: device to check
85  *
86  * Returns: true if @dev is a sysbus device on the machine's list
87  * of dynamically pluggable sysbus devices; otherwise false.
88  *
89  * This function checks whether @dev is a valid dynamic sysbus device,
90  * by first confirming that it is a sysbus device and then checking it
91  * against the list of permitted dynamic sysbus devices which has been
92  * set up by the machine using machine_class_allow_dynamic_sysbus_dev().
93  *
94  * It is valid to call this with something that is not a subclass of
95  * TYPE_SYS_BUS_DEVICE; the function will return false in this case.
96  * This allows hotplug callback functions to be written as:
97  *     if (device_is_dynamic_sysbus(mc, dev)) {
98  *         handle dynamic sysbus case;
99  *     } else if (some other kind of hotplug) {
100  *         handle that;
101  *     }
102  */
103 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev);
104 
105 /*
106  * Checks that backend isn't used, preps it for exclusive usage and
107  * returns migratable MemoryRegion provided by backend.
108  */
109 MemoryRegion *machine_consume_memdev(MachineState *machine,
110                                      HostMemoryBackend *backend);
111 
112 /**
113  * CPUArchId:
114  * @arch_id - architecture-dependent CPU ID of present or possible CPU
115  * @cpu - pointer to corresponding CPU object if it's present on NULL otherwise
116  * @type - QOM class name of possible @cpu object
117  * @props - CPU object properties, initialized by board
118  * #vcpus_count - number of threads provided by @cpu object
119  */
120 typedef struct CPUArchId {
121     uint64_t arch_id;
122     int64_t vcpus_count;
123     CpuInstanceProperties props;
124     CPUState *cpu;
125     const char *type;
126 } CPUArchId;
127 
128 /**
129  * CPUArchIdList:
130  * @len - number of @CPUArchId items in @cpus array
131  * @cpus - array of present or possible CPUs for current machine configuration
132  */
133 typedef struct {
134     int len;
135     CPUArchId cpus[];
136 } CPUArchIdList;
137 
138 /**
139  * SMPCompatProps:
140  * @prefer_sockets - whether sockets are preferred over cores in smp parsing
141  * @dies_supported - whether dies are supported by the machine
142  * @clusters_supported - whether clusters are supported by the machine
143  * @has_clusters - whether clusters are explicitly specified in the user
144  *                 provided SMP configuration
145  * @books_supported - whether books are supported by the machine
146  * @drawers_supported - whether drawers are supported by the machine
147  */
148 typedef struct {
149     bool prefer_sockets;
150     bool dies_supported;
151     bool clusters_supported;
152     bool has_clusters;
153     bool books_supported;
154     bool drawers_supported;
155 } SMPCompatProps;
156 
157 /**
158  * MachineClass:
159  * @deprecation_reason: If set, the machine is marked as deprecated. The
160  *    string should provide some clear information about what to use instead.
161  * @max_cpus: maximum number of CPUs supported. Default: 1
162  * @min_cpus: minimum number of CPUs supported. Default: 1
163  * @default_cpus: number of CPUs instantiated if none are specified. Default: 1
164  * @is_default:
165  *    If true QEMU will use this machine by default if no '-M' option is given.
166  * @get_hotplug_handler: this function is called during bus-less
167  *    device hotplug. If defined it returns pointer to an instance
168  *    of HotplugHandler object, which handles hotplug operation
169  *    for a given @dev. It may return NULL if @dev doesn't require
170  *    any actions to be performed by hotplug handler.
171  * @cpu_index_to_instance_props:
172  *    used to provide @cpu_index to socket/core/thread number mapping, allowing
173  *    legacy code to perform mapping from cpu_index to topology properties
174  *    Returns: tuple of socket/core/thread ids given cpu_index belongs to.
175  *    used to provide @cpu_index to socket number mapping, allowing
176  *    a machine to group CPU threads belonging to the same socket/package
177  *    Returns: socket number given cpu_index belongs to.
178  * @hw_version:
179  *    Value of QEMU_VERSION when the machine was added to QEMU.
180  *    Set only by old machines because they need to keep
181  *    compatibility on code that exposed QEMU_VERSION to guests in
182  *    the past (and now use qemu_hw_version()).
183  * @possible_cpu_arch_ids:
184  *    Returns an array of @CPUArchId architecture-dependent CPU IDs
185  *    which includes CPU IDs for present and possible to hotplug CPUs.
186  *    Caller is responsible for freeing returned list.
187  * @get_default_cpu_node_id:
188  *    returns default board specific node_id value for CPU slot specified by
189  *    index @idx in @ms->possible_cpus[]
190  * @has_hotpluggable_cpus:
191  *    If true, board supports CPUs creation with -device/device_add.
192  * @default_cpu_type:
193  *    specifies default CPU_TYPE, which will be used for parsing target
194  *    specific features and for creating CPUs if CPU name wasn't provided
195  *    explicitly at CLI
196  * @minimum_page_bits:
197  *    If non-zero, the board promises never to create a CPU with a page size
198  *    smaller than this, so QEMU can use a more efficient larger page
199  *    size than the target architecture's minimum. (Attempting to create
200  *    such a CPU will fail.) Note that changing this is a migration
201  *    compatibility break for the machine.
202  * @ignore_memory_transaction_failures:
203  *    If this is flag is true then the CPU will ignore memory transaction
204  *    failures which should cause the CPU to take an exception due to an
205  *    access to an unassigned physical address; the transaction will instead
206  *    return zero (for a read) or be ignored (for a write). This should be
207  *    set only by legacy board models which rely on the old RAZ/WI behaviour
208  *    for handling devices that QEMU does not yet model. New board models
209  *    should instead use "unimplemented-device" for all memory ranges where
210  *    the guest will attempt to probe for a device that QEMU doesn't
211  *    implement and a stub device is required.
212  * @kvm_type:
213  *    Return the type of KVM corresponding to the kvm-type string option or
214  *    computed based on other criteria such as the host kernel capabilities.
215  *    kvm-type may be NULL if it is not needed.
216  * @numa_mem_supported:
217  *    true if '--numa node.mem' option is supported and false otherwise
218  * @hotplug_allowed:
219  *    If the hook is provided, then it'll be called for each device
220  *    hotplug to check whether the device hotplug is allowed.  Return
221  *    true to grant allowance or false to reject the hotplug.  When
222  *    false is returned, an error must be set to show the reason of
223  *    the rejection.  If the hook is not provided, all hotplug will be
224  *    allowed.
225  * @default_ram_id:
226  *    Specifies initial RAM MemoryRegion name to be used for default backend
227  *    creation if user explicitly hasn't specified backend with "memory-backend"
228  *    property.
229  *    It also will be used as a way to option into "-m" option support.
230  *    If it's not set by board, '-m' will be ignored and generic code will
231  *    not create default RAM MemoryRegion.
232  * @fixup_ram_size:
233  *    Amends user provided ram size (with -m option) using machine
234  *    specific algorithm. To be used by old machine types for compat
235  *    purposes only.
236  *    Applies only to default memory backend, i.e., explicit memory backend
237  *    wasn't used.
238  */
239 struct MachineClass {
240     /*< private >*/
241     ObjectClass parent_class;
242     /*< public >*/
243 
244     const char *family; /* NULL iff @name identifies a standalone machtype */
245     char *name;
246     const char *alias;
247     const char *desc;
248     const char *deprecation_reason;
249 
250     void (*init)(MachineState *state);
251     void (*reset)(MachineState *state, ShutdownCause reason);
252     void (*wakeup)(MachineState *state);
253     int (*kvm_type)(MachineState *machine, const char *arg);
254 
255     BlockInterfaceType block_default_type;
256     int units_per_default_bus;
257     int max_cpus;
258     int min_cpus;
259     int default_cpus;
260     unsigned int no_serial:1,
261         no_parallel:1,
262         no_floppy:1,
263         no_cdrom:1,
264         no_sdcard:1,
265         pci_allow_0_address:1,
266         legacy_fw_cfg_order:1;
267     bool is_default;
268     const char *default_machine_opts;
269     const char *default_boot_order;
270     const char *default_display;
271     const char *default_nic;
272     GPtrArray *compat_props;
273     const char *hw_version;
274     ram_addr_t default_ram_size;
275     const char *default_cpu_type;
276     bool default_kernel_irqchip_split;
277     bool option_rom_has_mr;
278     bool rom_file_has_mr;
279     int minimum_page_bits;
280     bool has_hotpluggable_cpus;
281     bool ignore_memory_transaction_failures;
282     int numa_mem_align_shift;
283     const char * const *valid_cpu_types;
284     strList *allowed_dynamic_sysbus_devices;
285     bool auto_enable_numa_with_memhp;
286     bool auto_enable_numa_with_memdev;
287     bool ignore_boot_device_suffixes;
288     bool smbus_no_migration_support;
289     bool nvdimm_supported;
290     bool numa_mem_supported;
291     bool auto_enable_numa;
292     bool cpu_cluster_has_numa_boundary;
293     SMPCompatProps smp_props;
294     const char *default_ram_id;
295 
296     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
297                                            DeviceState *dev);
298     bool (*hotplug_allowed)(MachineState *state, DeviceState *dev,
299                             Error **errp);
300     CpuInstanceProperties (*cpu_index_to_instance_props)(MachineState *machine,
301                                                          unsigned cpu_index);
302     const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine);
303     int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx);
304     ram_addr_t (*fixup_ram_size)(ram_addr_t size);
305 };
306 
307 /**
308  * DeviceMemoryState:
309  * @base: address in guest physical address space where the memory
310  * address space for memory devices starts
311  * @mr: memory region container for memory devices
312  * @as: address space for memory devices
313  * @listener: memory listener used to track used memslots in the address space
314  * @dimm_size: the sum of plugged DIMMs' sizes
315  * @used_region_size: the part of @mr already used by memory devices
316  * @required_memslots: the number of memslots required by memory devices
317  * @used_memslots: the number of memslots currently used by memory devices
318  * @memslot_auto_decision_active: whether any plugged memory device
319  *                                automatically decided to use more than
320  *                                one memslot
321  */
322 typedef struct DeviceMemoryState {
323     hwaddr base;
324     MemoryRegion mr;
325     AddressSpace as;
326     MemoryListener listener;
327     uint64_t dimm_size;
328     uint64_t used_region_size;
329     unsigned int required_memslots;
330     unsigned int used_memslots;
331     unsigned int memslot_auto_decision_active;
332 } DeviceMemoryState;
333 
334 /**
335  * CpuTopology:
336  * @cpus: the number of present logical processors on the machine
337  * @drawers: the number of drawers on the machine
338  * @books: the number of books in one drawer
339  * @sockets: the number of sockets in one book
340  * @dies: the number of dies in one socket
341  * @clusters: the number of clusters in one die
342  * @cores: the number of cores in one cluster
343  * @threads: the number of threads in one core
344  * @max_cpus: the maximum number of logical processors on the machine
345  */
346 typedef struct CpuTopology {
347     unsigned int cpus;
348     unsigned int drawers;
349     unsigned int books;
350     unsigned int sockets;
351     unsigned int dies;
352     unsigned int clusters;
353     unsigned int cores;
354     unsigned int threads;
355     unsigned int max_cpus;
356 } CpuTopology;
357 
358 /**
359  * MachineState:
360  */
361 struct MachineState {
362     /*< private >*/
363     Object parent_obj;
364 
365     /*< public >*/
366 
367     void *fdt;
368     char *dtb;
369     char *dumpdtb;
370     int phandle_start;
371     char *dt_compatible;
372     bool dump_guest_core;
373     bool mem_merge;
374     bool require_guest_memfd;
375     bool usb;
376     bool usb_disabled;
377     char *firmware;
378     bool iommu;
379     bool suppress_vmdesc;
380     bool enable_graphics;
381     ConfidentialGuestSupport *cgs;
382     HostMemoryBackend *memdev;
383     /*
384      * convenience alias to ram_memdev_id backend memory region
385      * or to numa container memory region
386      */
387     MemoryRegion *ram;
388     DeviceMemoryState *device_memory;
389 
390     /*
391      * Included in MachineState for simplicity, but not supported
392      * unless machine_add_audiodev_property is called.  Boards
393      * that have embedded audio devices can call it from the
394      * machine init function and forward the property to the device.
395      */
396     char *audiodev;
397 
398     ram_addr_t ram_size;
399     ram_addr_t maxram_size;
400     uint64_t   ram_slots;
401     BootConfiguration boot_config;
402     char *kernel_filename;
403     char *kernel_cmdline;
404     char *initrd_filename;
405     const char *cpu_type;
406     AccelState *accelerator;
407     CPUArchIdList *possible_cpus;
408     CpuTopology smp;
409     struct NVDIMMState *nvdimms_state;
410     struct NumaState *numa_state;
411 };
412 
413 #define DEFINE_MACHINE(namestr, machine_initfn) \
414     static void machine_initfn##_class_init(ObjectClass *oc, void *data) \
415     { \
416         MachineClass *mc = MACHINE_CLASS(oc); \
417         machine_initfn(mc); \
418     } \
419     static const TypeInfo machine_initfn##_typeinfo = { \
420         .name       = MACHINE_TYPE_NAME(namestr), \
421         .parent     = TYPE_MACHINE, \
422         .class_init = machine_initfn##_class_init, \
423     }; \
424     static void machine_initfn##_register_types(void) \
425     { \
426         type_register_static(&machine_initfn##_typeinfo); \
427     } \
428     type_init(machine_initfn##_register_types)
429 
430 extern GlobalProperty hw_compat_9_0[];
431 extern const size_t hw_compat_9_0_len;
432 
433 extern GlobalProperty hw_compat_8_2[];
434 extern const size_t hw_compat_8_2_len;
435 
436 extern GlobalProperty hw_compat_8_1[];
437 extern const size_t hw_compat_8_1_len;
438 
439 extern GlobalProperty hw_compat_8_0[];
440 extern const size_t hw_compat_8_0_len;
441 
442 extern GlobalProperty hw_compat_7_2[];
443 extern const size_t hw_compat_7_2_len;
444 
445 extern GlobalProperty hw_compat_7_1[];
446 extern const size_t hw_compat_7_1_len;
447 
448 extern GlobalProperty hw_compat_7_0[];
449 extern const size_t hw_compat_7_0_len;
450 
451 extern GlobalProperty hw_compat_6_2[];
452 extern const size_t hw_compat_6_2_len;
453 
454 extern GlobalProperty hw_compat_6_1[];
455 extern const size_t hw_compat_6_1_len;
456 
457 extern GlobalProperty hw_compat_6_0[];
458 extern const size_t hw_compat_6_0_len;
459 
460 extern GlobalProperty hw_compat_5_2[];
461 extern const size_t hw_compat_5_2_len;
462 
463 extern GlobalProperty hw_compat_5_1[];
464 extern const size_t hw_compat_5_1_len;
465 
466 extern GlobalProperty hw_compat_5_0[];
467 extern const size_t hw_compat_5_0_len;
468 
469 extern GlobalProperty hw_compat_4_2[];
470 extern const size_t hw_compat_4_2_len;
471 
472 extern GlobalProperty hw_compat_4_1[];
473 extern const size_t hw_compat_4_1_len;
474 
475 extern GlobalProperty hw_compat_4_0[];
476 extern const size_t hw_compat_4_0_len;
477 
478 extern GlobalProperty hw_compat_3_1[];
479 extern const size_t hw_compat_3_1_len;
480 
481 extern GlobalProperty hw_compat_3_0[];
482 extern const size_t hw_compat_3_0_len;
483 
484 extern GlobalProperty hw_compat_2_12[];
485 extern const size_t hw_compat_2_12_len;
486 
487 extern GlobalProperty hw_compat_2_11[];
488 extern const size_t hw_compat_2_11_len;
489 
490 extern GlobalProperty hw_compat_2_10[];
491 extern const size_t hw_compat_2_10_len;
492 
493 extern GlobalProperty hw_compat_2_9[];
494 extern const size_t hw_compat_2_9_len;
495 
496 extern GlobalProperty hw_compat_2_8[];
497 extern const size_t hw_compat_2_8_len;
498 
499 extern GlobalProperty hw_compat_2_7[];
500 extern const size_t hw_compat_2_7_len;
501 
502 extern GlobalProperty hw_compat_2_6[];
503 extern const size_t hw_compat_2_6_len;
504 
505 extern GlobalProperty hw_compat_2_5[];
506 extern const size_t hw_compat_2_5_len;
507 
508 extern GlobalProperty hw_compat_2_4[];
509 extern const size_t hw_compat_2_4_len;
510 
511 extern GlobalProperty hw_compat_2_3[];
512 extern const size_t hw_compat_2_3_len;
513 
514 extern GlobalProperty hw_compat_2_2[];
515 extern const size_t hw_compat_2_2_len;
516 
517 extern GlobalProperty hw_compat_2_1[];
518 extern const size_t hw_compat_2_1_len;
519 
520 #endif
521