xref: /qemu/include/hw/boards.h (revision 4be0fce498d0a08f18b3a9accdb9ded79484d30a)
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 #include "hw/resettable.h"
14 
15 #define TYPE_MACHINE_SUFFIX "-machine"
16 
17 /* Machine class name that needs to be used for class-name-based machine
18  * type lookup to work.
19  */
20 #define MACHINE_TYPE_NAME(machinename) (machinename TYPE_MACHINE_SUFFIX)
21 
22 #define TYPE_MACHINE "machine"
23 #undef MACHINE  /* BSD defines it and QEMU does not use it */
24 OBJECT_DECLARE_TYPE(MachineState, MachineClass, MACHINE)
25 
26 extern MachineState *current_machine;
27 
28 /**
29  * machine_class_default_cpu_type: Return the machine default CPU type.
30  * @mc: Machine class
31  */
32 const char *machine_class_default_cpu_type(MachineClass *mc);
33 
34 void machine_add_audiodev_property(MachineClass *mc);
35 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp);
36 bool machine_usb(MachineState *machine);
37 int machine_phandle_start(MachineState *machine);
38 bool machine_dump_guest_core(MachineState *machine);
39 bool machine_mem_merge(MachineState *machine);
40 bool machine_require_guest_memfd(MachineState *machine);
41 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
42 void machine_set_cpu_numa_node(MachineState *machine,
43                                const CpuInstanceProperties *props,
44                                Error **errp);
45 void machine_parse_smp_config(MachineState *ms,
46                               const SMPConfiguration *config, Error **errp);
47 bool machine_parse_smp_cache(MachineState *ms,
48                              const SmpCachePropertiesList *caches,
49                              Error **errp);
50 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms);
51 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms);
52 CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
53                                               CacheLevelAndType cache);
54 void machine_set_cache_topo_level(MachineState *ms, CacheLevelAndType cache,
55                                   CpuTopologyLevel level);
56 bool machine_check_smp_cache(const MachineState *ms, Error **errp);
57 void machine_memory_devices_init(MachineState *ms, hwaddr base, uint64_t size);
58 
59 /**
60  * machine_class_allow_dynamic_sysbus_dev: Add type to list of valid devices
61  * @mc: Machine class
62  * @type: type to allow (should be a subtype of TYPE_SYS_BUS_DEVICE)
63  *
64  * Add the QOM type @type to the list of devices of which are subtypes
65  * of TYPE_SYS_BUS_DEVICE but which are still permitted to be dynamically
66  * created (eg by the user on the command line with -device).
67  * By default if the user tries to create any devices on the command line
68  * that are subtypes of TYPE_SYS_BUS_DEVICE they will get an error message;
69  * for the special cases which are permitted for this machine model, the
70  * machine model class init code must call this function to add them
71  * to the list of specifically permitted devices.
72  */
73 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
74 
75 /**
76  * device_type_is_dynamic_sysbus: Check if type is an allowed sysbus device
77  * type for the machine class.
78  * @mc: Machine class
79  * @type: type to check (should be a subtype of TYPE_SYS_BUS_DEVICE)
80  *
81  * Returns: true if @type is a type in the machine's list of
82  * dynamically pluggable sysbus devices; otherwise false.
83  *
84  * Check if the QOM type @type is in the list of allowed sysbus device
85  * types (see machine_class_allowed_dynamic_sysbus_dev()).
86  * Note that if @type has a parent type in the list, it is allowed too.
87  */
88 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type);
89 
90 /**
91  * device_is_dynamic_sysbus: test whether device is a dynamic sysbus device
92  * @mc: Machine class
93  * @dev: device to check
94  *
95  * Returns: true if @dev is a sysbus device on the machine's list
96  * of dynamically pluggable sysbus devices; otherwise false.
97  *
98  * This function checks whether @dev is a valid dynamic sysbus device,
99  * by first confirming that it is a sysbus device and then checking it
100  * against the list of permitted dynamic sysbus devices which has been
101  * set up by the machine using machine_class_allow_dynamic_sysbus_dev().
102  *
103  * It is valid to call this with something that is not a subclass of
104  * TYPE_SYS_BUS_DEVICE; the function will return false in this case.
105  * This allows hotplug callback functions to be written as:
106  *     if (device_is_dynamic_sysbus(mc, dev)) {
107  *         handle dynamic sysbus case;
108  *     } else if (some other kind of hotplug) {
109  *         handle that;
110  *     }
111  */
112 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev);
113 
114 /*
115  * Checks that backend isn't used, preps it for exclusive usage and
116  * returns migratable MemoryRegion provided by backend.
117  */
118 MemoryRegion *machine_consume_memdev(MachineState *machine,
119                                      HostMemoryBackend *backend);
120 
121 /**
122  * CPUArchId:
123  * @arch_id - architecture-dependent CPU ID of present or possible CPU
124  * @cpu - pointer to corresponding CPU object if it's present on NULL otherwise
125  * @type - QOM class name of possible @cpu object
126  * @props - CPU object properties, initialized by board
127  * #vcpus_count - number of threads provided by @cpu object
128  */
129 typedef struct CPUArchId {
130     uint64_t arch_id;
131     int64_t vcpus_count;
132     CpuInstanceProperties props;
133     CPUState *cpu;
134     const char *type;
135 } CPUArchId;
136 
137 /**
138  * CPUArchIdList:
139  * @len - number of @CPUArchId items in @cpus array
140  * @cpus - array of present or possible CPUs for current machine configuration
141  */
142 typedef struct {
143     int len;
144     CPUArchId cpus[];
145 } CPUArchIdList;
146 
147 /**
148  * SMPCompatProps:
149  * @prefer_sockets - whether sockets are preferred over cores in smp parsing
150  * @dies_supported - whether dies are supported by the machine
151  * @clusters_supported - whether clusters are supported by the machine
152  * @has_clusters - whether clusters are explicitly specified in the user
153  *                 provided SMP configuration
154  * @books_supported - whether books are supported by the machine
155  * @drawers_supported - whether drawers are supported by the machine
156  * @modules_supported - whether modules are supported by the machine
157  * @cache_supported - whether cache (l1d, l1i, l2 and l3) configuration are
158  *                    supported by the machine
159  */
160 typedef struct {
161     bool prefer_sockets;
162     bool dies_supported;
163     bool clusters_supported;
164     bool has_clusters;
165     bool books_supported;
166     bool drawers_supported;
167     bool modules_supported;
168     bool cache_supported[CACHE_LEVEL_AND_TYPE__MAX];
169 } SMPCompatProps;
170 
171 /**
172  * MachineClass:
173  * @deprecation_reason: If set, the machine is marked as deprecated. The
174  *    string should provide some clear information about what to use instead.
175  * @max_cpus: maximum number of CPUs supported. Default: 1
176  * @min_cpus: minimum number of CPUs supported. Default: 1
177  * @default_cpus: number of CPUs instantiated if none are specified. Default: 1
178  * @is_default:
179  *    If true QEMU will use this machine by default if no '-M' option is given.
180  * @get_hotplug_handler: this function is called during bus-less
181  *    device hotplug. If defined it returns pointer to an instance
182  *    of HotplugHandler object, which handles hotplug operation
183  *    for a given @dev. It may return NULL if @dev doesn't require
184  *    any actions to be performed by hotplug handler.
185  * @cpu_index_to_instance_props:
186  *    used to provide @cpu_index to socket/core/thread number mapping, allowing
187  *    legacy code to perform mapping from cpu_index to topology properties
188  *    Returns: tuple of socket/core/thread ids given cpu_index belongs to.
189  *    used to provide @cpu_index to socket number mapping, allowing
190  *    a machine to group CPU threads belonging to the same socket/package
191  *    Returns: socket number given cpu_index belongs to.
192  * @hw_version:
193  *    Value of QEMU_VERSION when the machine was added to QEMU.
194  *    Set only by old machines because they need to keep
195  *    compatibility on code that exposed QEMU_VERSION to guests in
196  *    the past (and now use qemu_hw_version()).
197  * @possible_cpu_arch_ids:
198  *    Returns an array of @CPUArchId architecture-dependent CPU IDs
199  *    which includes CPU IDs for present and possible to hotplug CPUs.
200  *    Caller is responsible for freeing returned list.
201  * @get_default_cpu_node_id:
202  *    returns default board specific node_id value for CPU slot specified by
203  *    index @idx in @ms->possible_cpus[]
204  * @has_hotpluggable_cpus:
205  *    If true, board supports CPUs creation with -device/device_add.
206  * @default_cpu_type:
207  *    specifies default CPU_TYPE, which will be used for parsing target
208  *    specific features and for creating CPUs if CPU name wasn't provided
209  *    explicitly at CLI
210  * @minimum_page_bits:
211  *    If non-zero, the board promises never to create a CPU with a page size
212  *    smaller than this, so QEMU can use a more efficient larger page
213  *    size than the target architecture's minimum. (Attempting to create
214  *    such a CPU will fail.) Note that changing this is a migration
215  *    compatibility break for the machine.
216  * @ignore_memory_transaction_failures:
217  *    If this is flag is true then the CPU will ignore memory transaction
218  *    failures which should cause the CPU to take an exception due to an
219  *    access to an unassigned physical address; the transaction will instead
220  *    return zero (for a read) or be ignored (for a write). This should be
221  *    set only by legacy board models which rely on the old RAZ/WI behaviour
222  *    for handling devices that QEMU does not yet model. New board models
223  *    should instead use "unimplemented-device" for all memory ranges where
224  *    the guest will attempt to probe for a device that QEMU doesn't
225  *    implement and a stub device is required.
226  * @kvm_type:
227  *    Return the type of KVM corresponding to the kvm-type string option or
228  *    computed based on other criteria such as the host kernel capabilities.
229  *    kvm-type may be NULL if it is not needed.
230  * @hvf_get_physical_address_range:
231  *    Returns the physical address range in bits to use for the HVF virtual
232  *    machine based on the current boards memory map. This may be NULL if it
233  *    is not needed.
234  * @numa_mem_supported:
235  *    true if '--numa node.mem' option is supported and false otherwise
236  * @hotplug_allowed:
237  *    If the hook is provided, then it'll be called for each device
238  *    hotplug to check whether the device hotplug is allowed.  Return
239  *    true to grant allowance or false to reject the hotplug.  When
240  *    false is returned, an error must be set to show the reason of
241  *    the rejection.  If the hook is not provided, all hotplug will be
242  *    allowed.
243  * @default_ram_id:
244  *    Specifies initial RAM MemoryRegion name to be used for default backend
245  *    creation if user explicitly hasn't specified backend with "memory-backend"
246  *    property.
247  *    It also will be used as a way to option into "-m" option support.
248  *    If it's not set by board, '-m' will be ignored and generic code will
249  *    not create default RAM MemoryRegion.
250  * @fixup_ram_size:
251  *    Amends user provided ram size (with -m option) using machine
252  *    specific algorithm. To be used by old machine types for compat
253  *    purposes only.
254  *    Applies only to default memory backend, i.e., explicit memory backend
255  *    wasn't used.
256  * @smbios_memory_device_size:
257  *    Default size of memory device,
258  *    SMBIOS 3.1.0 "7.18 Memory Device (Type 17)"
259  */
260 struct MachineClass {
261     /*< private >*/
262     ObjectClass parent_class;
263     /*< public >*/
264 
265     const char *family; /* NULL iff @name identifies a standalone machtype */
266     char *name;
267     const char *alias;
268     const char *desc;
269     const char *deprecation_reason;
270 
271     void (*init)(MachineState *state);
272     void (*reset)(MachineState *state, ResetType type);
273     void (*wakeup)(MachineState *state);
274     int (*kvm_type)(MachineState *machine, const char *arg);
275     int (*hvf_get_physical_address_range)(MachineState *machine);
276 
277     BlockInterfaceType block_default_type;
278     int units_per_default_bus;
279     int max_cpus;
280     int min_cpus;
281     int default_cpus;
282     unsigned int no_serial:1,
283         no_parallel:1,
284         no_floppy:1,
285         no_cdrom:1,
286         no_sdcard:1,
287         pci_allow_0_address:1,
288         legacy_fw_cfg_order:1;
289     bool is_default;
290     const char *default_machine_opts;
291     const char *default_boot_order;
292     const char *default_display;
293     const char *default_nic;
294     GPtrArray *compat_props;
295     const char *hw_version;
296     ram_addr_t default_ram_size;
297     const char *default_cpu_type;
298     bool default_kernel_irqchip_split;
299     bool option_rom_has_mr;
300     bool rom_file_has_mr;
301     int minimum_page_bits;
302     bool has_hotpluggable_cpus;
303     bool ignore_memory_transaction_failures;
304     int numa_mem_align_shift;
305     const char * const *valid_cpu_types;
306     strList *allowed_dynamic_sysbus_devices;
307     bool auto_enable_numa_with_memhp;
308     bool auto_enable_numa_with_memdev;
309     bool ignore_boot_device_suffixes;
310     bool smbus_no_migration_support;
311     bool nvdimm_supported;
312     bool numa_mem_supported;
313     bool auto_enable_numa;
314     bool cpu_cluster_has_numa_boundary;
315     SMPCompatProps smp_props;
316     const char *default_ram_id;
317 
318     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
319                                            DeviceState *dev);
320     bool (*hotplug_allowed)(MachineState *state, DeviceState *dev,
321                             Error **errp);
322     CpuInstanceProperties (*cpu_index_to_instance_props)(MachineState *machine,
323                                                          unsigned cpu_index);
324     const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine);
325     int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx);
326     ram_addr_t (*fixup_ram_size)(ram_addr_t size);
327     uint64_t smbios_memory_device_size;
328     bool (*create_default_memdev)(MachineState *ms, const char *path,
329                                   Error **errp);
330 };
331 
332 /**
333  * DeviceMemoryState:
334  * @base: address in guest physical address space where the memory
335  * address space for memory devices starts
336  * @mr: memory region container for memory devices
337  * @as: address space for memory devices
338  * @listener: memory listener used to track used memslots in the address space
339  * @dimm_size: the sum of plugged DIMMs' sizes
340  * @used_region_size: the part of @mr already used by memory devices
341  * @required_memslots: the number of memslots required by memory devices
342  * @used_memslots: the number of memslots currently used by memory devices
343  * @memslot_auto_decision_active: whether any plugged memory device
344  *                                automatically decided to use more than
345  *                                one memslot
346  */
347 typedef struct DeviceMemoryState {
348     hwaddr base;
349     MemoryRegion mr;
350     AddressSpace as;
351     MemoryListener listener;
352     uint64_t dimm_size;
353     uint64_t used_region_size;
354     unsigned int required_memslots;
355     unsigned int used_memslots;
356     unsigned int memslot_auto_decision_active;
357 } DeviceMemoryState;
358 
359 /**
360  * CpuTopology:
361  * @cpus: the number of present logical processors on the machine
362  * @drawers: the number of drawers on the machine
363  * @books: the number of books in one drawer
364  * @sockets: the number of sockets in one book
365  * @dies: the number of dies in one socket
366  * @clusters: the number of clusters in one die
367  * @modules: the number of modules in one cluster
368  * @cores: the number of cores in one cluster
369  * @threads: the number of threads in one core
370  * @max_cpus: the maximum number of logical processors on the machine
371  */
372 typedef struct CpuTopology {
373     unsigned int cpus;
374     unsigned int drawers;
375     unsigned int books;
376     unsigned int sockets;
377     unsigned int dies;
378     unsigned int clusters;
379     unsigned int modules;
380     unsigned int cores;
381     unsigned int threads;
382     unsigned int max_cpus;
383 } CpuTopology;
384 
385 typedef struct SmpCache {
386     SmpCacheProperties props[CACHE_LEVEL_AND_TYPE__MAX];
387 } SmpCache;
388 
389 /**
390  * MachineState:
391  */
392 struct MachineState {
393     /*< private >*/
394     Object parent_obj;
395 
396     /*< public >*/
397 
398     void *fdt;
399     char *dtb;
400     char *dumpdtb;
401     int phandle_start;
402     char *dt_compatible;
403     bool dump_guest_core;
404     bool mem_merge;
405     bool usb;
406     bool usb_disabled;
407     char *firmware;
408     bool iommu;
409     bool suppress_vmdesc;
410     bool enable_graphics;
411     ConfidentialGuestSupport *cgs;
412     HostMemoryBackend *memdev;
413     /*
414      * convenience alias to ram_memdev_id backend memory region
415      * or to numa container memory region
416      */
417     MemoryRegion *ram;
418     DeviceMemoryState *device_memory;
419 
420     /*
421      * Included in MachineState for simplicity, but not supported
422      * unless machine_add_audiodev_property is called.  Boards
423      * that have embedded audio devices can call it from the
424      * machine init function and forward the property to the device.
425      */
426     char *audiodev;
427 
428     ram_addr_t ram_size;
429     ram_addr_t maxram_size;
430     uint64_t   ram_slots;
431     BootConfiguration boot_config;
432     char *kernel_filename;
433     char *kernel_cmdline;
434     char *shim_filename;
435     char *initrd_filename;
436     const char *cpu_type;
437     AccelState *accelerator;
438     CPUArchIdList *possible_cpus;
439     CpuTopology smp;
440     SmpCache smp_cache;
441     struct NVDIMMState *nvdimms_state;
442     struct NumaState *numa_state;
443 };
444 
445 /*
446  * The macros which follow are intended to facilitate the
447  * definition of versioned machine types, using a somewhat
448  * similar pattern across targets.
449  *
450  * For example, a macro that can be used to define versioned
451  * 'virt' machine types would look like:
452  *
453  *  #define DEFINE_VIRT_MACHINE_IMPL(latest, ...) \
454  *      static void MACHINE_VER_SYM(class_init, virt, __VA_ARGS__)( \
455  *          ObjectClass *oc, \
456  *          void *data) \
457  *      { \
458  *          MachineClass *mc = MACHINE_CLASS(oc); \
459  *          MACHINE_VER_SYM(options, virt, __VA_ARGS__)(mc); \
460  *          mc->desc = "QEMU " MACHINE_VER_STR(__VA_ARGS__) " Virtual Machine"; \
461  *          MACHINE_VER_DEPRECATION(__VA_ARGS__); \
462  *          if (latest) { \
463  *              mc->alias = "virt"; \
464  *          } \
465  *      } \
466  *      static const TypeInfo MACHINE_VER_SYM(info, virt, __VA_ARGS__) = { \
467  *          .name = MACHINE_VER_TYPE_NAME("virt", __VA_ARGS__), \
468  *          .parent = TYPE_VIRT_MACHINE, \
469  *          .class_init = MACHINE_VER_SYM(class_init, virt, __VA_ARGS__), \
470  *      }; \
471  *      static void MACHINE_VER_SYM(register, virt, __VA_ARGS__)(void) \
472  *      { \
473  *          MACHINE_VER_DELETION(__VA_ARGS__); \
474  *          type_register_static(&MACHINE_VER_SYM(info, virt, __VA_ARGS__)); \
475  *      } \
476  *      type_init(MACHINE_VER_SYM(register, virt, __VA_ARGS__));
477  *
478  * Following this, one (or more) helpers can be added for
479  * whichever scenarios need to be catered for with a machine:
480  *
481  *  // Normal 2 digit, marked as latest e.g. 'virt-9.0'
482  *  #define DEFINE_VIRT_MACHINE_LATEST(major, minor) \
483  *      DEFINE_VIRT_MACHINE_IMPL(true, major, minor)
484  *
485  *  // Normal 2 digit e.g. 'virt-9.0'
486  *  #define DEFINE_VIRT_MACHINE(major, minor) \
487  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor)
488  *
489  *  // Bugfix 3 digit e.g. 'virt-9.0.1'
490  *  #define DEFINE_VIRT_MACHINE_BUGFIX(major, minor, micro) \
491  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor, micro)
492  *
493  *  // Tagged 2 digit e.g. 'virt-9.0-extra'
494  *  #define DEFINE_VIRT_MACHINE_TAGGED(major, minor, tag) \
495  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor, _, tag)
496  *
497  *  // Tagged bugfix 2 digit e.g. 'virt-9.0.1-extra'
498  *  #define DEFINE_VIRT_MACHINE_TAGGED(major, minor, micro, tag) \
499  *      DEFINE_VIRT_MACHINE_IMPL(false, major, minor, micro, _, tag)
500  */
501 
502 /*
503  * Helper for dispatching different macros based on how
504  * many __VA_ARGS__ are passed. Supports 1 to 5 variadic
505  * arguments, with the called target able to be prefixed
506  * with 0 or more fixed arguments too. To be called thus:
507  *
508  *  _MACHINE_VER_PICK(__VA_ARGS,
509  *                    MACRO_MATCHING_5_ARGS,
510  *                    MACRO_MATCHING_4_ARGS,
511  *                    MACRO_MATCHING_3_ARGS,
512  *                    MACRO_MATCHING_2_ARGS,
513  *                    MACRO_MATCHING_1_ARG) (FIXED-ARG-1,
514  *                                           ...,
515  *                                           FIXED-ARG-N,
516  *                                           __VA_ARGS__)
517  */
518 #define _MACHINE_VER_PICK(x1, x2, x3, x4, x5, x6, ...) x6
519 
520 /*
521  * Construct a human targeted machine version string.
522  *
523  * Can be invoked with various signatures
524  *
525  *  MACHINE_VER_STR(sym, prefix, major, minor)
526  *  MACHINE_VER_STR(sym, prefix, major, minor, micro)
527  *  MACHINE_VER_STR(sym, prefix, major, minor, _, tag)
528  *  MACHINE_VER_STR(sym, prefix, major, minor, micro, _, tag)
529  *
530  * Respectively emitting symbols with the format
531  *
532  *   "{major}.{minor}"
533  *   "{major}.{minor}-{tag}"
534  *   "{major}.{minor}.{micro}"
535  *   "{major}.{minor}.{micro}-{tag}"
536  */
537 #define _MACHINE_VER_STR2(major, minor) \
538     #major "." #minor
539 
540 #define _MACHINE_VER_STR3(major, minor, micro) \
541     #major "." #minor "." #micro
542 
543 #define _MACHINE_VER_STR4(major, minor, _unused_, tag) \
544     #major "." #minor "-" #tag
545 
546 #define _MACHINE_VER_STR5(major, minor, micro, _unused_, tag) \
547     #major "." #minor "." #micro "-" #tag
548 
549 #define MACHINE_VER_STR(...) \
550     _MACHINE_VER_PICK(__VA_ARGS__, \
551                       _MACHINE_VER_STR5, \
552                       _MACHINE_VER_STR4, \
553                       _MACHINE_VER_STR3, \
554                       _MACHINE_VER_STR2) (__VA_ARGS__)
555 
556 
557 /*
558  * Construct a QAPI type name for a versioned machine
559  * type
560  *
561  * Can be invoked with various signatures
562  *
563  *  MACHINE_VER_TYPE_NAME(prefix, major, minor)
564  *  MACHINE_VER_TYPE_NAME(prefix, major, minor, micro)
565  *  MACHINE_VER_TYPE_NAME(prefix, major, minor, _, tag)
566  *  MACHINE_VER_TYPE_NAME(prefix, major, minor, micro, _, tag)
567  *
568  * Respectively emitting symbols with the format
569  *
570  *   "{prefix}-{major}.{minor}"
571  *   "{prefix}-{major}.{minor}.{micro}"
572  *   "{prefix}-{major}.{minor}-{tag}"
573  *   "{prefix}-{major}.{minor}.{micro}-{tag}"
574  */
575 #define _MACHINE_VER_TYPE_NAME2(prefix, major, minor)   \
576     prefix "-" #major "." #minor TYPE_MACHINE_SUFFIX
577 
578 #define _MACHINE_VER_TYPE_NAME3(prefix, major, minor, micro) \
579     prefix "-" #major "." #minor "." #micro TYPE_MACHINE_SUFFIX
580 
581 #define _MACHINE_VER_TYPE_NAME4(prefix, major, minor, _unused_, tag) \
582     prefix "-" #major "." #minor "-" #tag TYPE_MACHINE_SUFFIX
583 
584 #define _MACHINE_VER_TYPE_NAME5(prefix, major, minor, micro, _unused_, tag) \
585     prefix "-" #major "." #minor "." #micro "-" #tag TYPE_MACHINE_SUFFIX
586 
587 #define MACHINE_VER_TYPE_NAME(prefix, ...) \
588     _MACHINE_VER_PICK(__VA_ARGS__, \
589                       _MACHINE_VER_TYPE_NAME5, \
590                       _MACHINE_VER_TYPE_NAME4, \
591                       _MACHINE_VER_TYPE_NAME3, \
592                       _MACHINE_VER_TYPE_NAME2) (prefix, __VA_ARGS__)
593 
594 /*
595  * Construct a name for a versioned machine type that is
596  * suitable for use as a C symbol (function/variable/etc).
597  *
598  * Can be invoked with various signatures
599  *
600  *  MACHINE_VER_SYM(sym, prefix, major, minor)
601  *  MACHINE_VER_SYM(sym, prefix, major, minor, micro)
602  *  MACHINE_VER_SYM(sym, prefix, major, minor, _, tag)
603  *  MACHINE_VER_SYM(sym, prefix, major, minor, micro, _, tag)
604  *
605  * Respectively emitting symbols with the format
606  *
607  *   {prefix}_machine_{major}_{minor}_{sym}
608  *   {prefix}_machine_{major}_{minor}_{micro}_{sym}
609  *   {prefix}_machine_{major}_{minor}_{tag}_{sym}
610  *   {prefix}_machine_{major}_{minor}_{micro}_{tag}_{sym}
611  */
612 #define _MACHINE_VER_SYM2(sym, prefix, major, minor) \
613     prefix ## _machine_ ## major ## _ ## minor ## _ ## sym
614 
615 #define _MACHINE_VER_SYM3(sym, prefix, major, minor, micro) \
616     prefix ## _machine_ ## major ## _ ## minor ## _ ## micro ## _ ## sym
617 
618 #define _MACHINE_VER_SYM4(sym, prefix, major, minor, _unused_, tag) \
619     prefix ## _machine_ ## major ## _ ## minor ## _ ## tag ## _ ## sym
620 
621 #define _MACHINE_VER_SYM5(sym, prefix, major, minor, micro, _unused_, tag) \
622     prefix ## _machine_ ## major ## _ ## minor ## _ ## micro ## _ ## tag ## _ ## sym
623 
624 #define MACHINE_VER_SYM(sym, prefix, ...) \
625     _MACHINE_VER_PICK(__VA_ARGS__, \
626                       _MACHINE_VER_SYM5, \
627                       _MACHINE_VER_SYM4, \
628                       _MACHINE_VER_SYM3, \
629                       _MACHINE_VER_SYM2) (sym, prefix, __VA_ARGS__)
630 
631 
632 /*
633  * How many years/major releases for each phase
634  * of the life cycle. Assumes use of versioning
635  * scheme where major is bumped each year
636  */
637 #define MACHINE_VER_DELETION_MAJOR 6
638 #define MACHINE_VER_DEPRECATION_MAJOR 3
639 
640 /*
641  * Expands to a static string containing a deprecation
642  * message for a versioned machine type
643  */
644 #define MACHINE_VER_DEPRECATION_MSG \
645     "machines more than " stringify(MACHINE_VER_DEPRECATION_MAJOR) \
646     " years old are subject to deletion after " \
647     stringify(MACHINE_VER_DELETION_MAJOR) " years"
648 
649 #define _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor) \
650     (((QEMU_VERSION_MAJOR - major) > cutoff) || \
651      (((QEMU_VERSION_MAJOR - major) == cutoff) && \
652       (QEMU_VERSION_MINOR - minor) >= 0))
653 
654 #define _MACHINE_VER_IS_EXPIRED2(cutoff, major, minor) \
655     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
656 #define _MACHINE_VER_IS_EXPIRED3(cutoff, major, minor, micro) \
657     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
658 #define _MACHINE_VER_IS_EXPIRED4(cutoff, major, minor, _unused, tag) \
659     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
660 #define _MACHINE_VER_IS_EXPIRED5(cutoff, major, minor, micro, _unused, tag)   \
661     _MACHINE_VER_IS_EXPIRED_IMPL(cutoff, major, minor)
662 
663 #define _MACHINE_IS_EXPIRED(cutoff, ...) \
664     _MACHINE_VER_PICK(__VA_ARGS__, \
665                       _MACHINE_VER_IS_EXPIRED5, \
666                       _MACHINE_VER_IS_EXPIRED4, \
667                       _MACHINE_VER_IS_EXPIRED3, \
668                       _MACHINE_VER_IS_EXPIRED2) (cutoff, __VA_ARGS__)
669 
670 /*
671  * Evaluates true when a machine type with (major, minor)
672  * or (major, minor, micro) version should be considered
673  * deprecated based on the current versioned machine type
674  * lifecycle rules
675  */
676 #define MACHINE_VER_IS_DEPRECATED(...) \
677     _MACHINE_IS_EXPIRED(MACHINE_VER_DEPRECATION_MAJOR, __VA_ARGS__)
678 
679 /*
680  * Evaluates true when a machine type with (major, minor)
681  * or (major, minor, micro) version should be considered
682  * for deletion based on the current versioned machine type
683  * lifecycle rules
684  */
685 #define MACHINE_VER_SHOULD_DELETE(...) \
686     _MACHINE_IS_EXPIRED(MACHINE_VER_DELETION_MAJOR, __VA_ARGS__)
687 
688 /*
689  * Sets the deprecation reason for a versioned machine based
690  * on its age
691  *
692  * This must be unconditionally used in the _class_init
693  * function for all machine types which support versioning.
694  *
695  * Initially it will effectively be a no-op, but after a
696  * suitable period of time has passed, it will set the
697  * 'deprecation_reason' field on the machine, to warn users
698  * about forthcoming removal.
699  */
700 #define MACHINE_VER_DEPRECATION(...) \
701     do { \
702         if (MACHINE_VER_IS_DEPRECATED(__VA_ARGS__)) { \
703             mc->deprecation_reason = MACHINE_VER_DEPRECATION_MSG; \
704         } \
705     } while (0)
706 
707 /*
708  * Prevents registration of a versioned machined based on
709  * its age
710  *
711  * This must be unconditionally used in the register
712  * method for all machine types which support versioning.
713  *
714  * Inijtially it will effectively be a no-op, but after a
715  * suitable period of time has passed, it will cause
716  * execution of the method to return, avoiding registration
717  * of the machine
718  *
719  * The new deprecation and deletion policy for versioned
720  * machine types was introduced in QEMU 9.1.0.
721  *
722  * Under the new policy a number of old machine types (any
723  * prior to 2.12) would be liable for immediate deletion
724  * which would be a violation of our historical deprecation
725  * and removal policy
726  *
727  * Thus deletions are temporarily gated on existance of
728  * the env variable "QEMU_DELETE_MACHINES" / QEMU version
729  * number >= 10.1.0. This gate can be deleted in the 10.1.0
730  * dev cycle
731  */
732 #define MACHINE_VER_DELETION(...) \
733     do { \
734         if (MACHINE_VER_SHOULD_DELETE(__VA_ARGS__)) { \
735             if (getenv("QEMU_DELETE_MACHINES") || \
736                 QEMU_VERSION_MAJOR > 10 || (QEMU_VERSION_MAJOR == 10 && \
737                                             QEMU_VERSION_MINOR >= 1)) { \
738                 return; \
739             } \
740         } \
741     } while (0)
742 
743 #define DEFINE_MACHINE(namestr, machine_initfn) \
744     static void machine_initfn##_class_init(ObjectClass *oc, void *data) \
745     { \
746         MachineClass *mc = MACHINE_CLASS(oc); \
747         machine_initfn(mc); \
748     } \
749     static const TypeInfo machine_initfn##_typeinfo = { \
750         .name       = MACHINE_TYPE_NAME(namestr), \
751         .parent     = TYPE_MACHINE, \
752         .class_init = machine_initfn##_class_init, \
753     }; \
754     static void machine_initfn##_register_types(void) \
755     { \
756         type_register_static(&machine_initfn##_typeinfo); \
757     } \
758     type_init(machine_initfn##_register_types)
759 
760 extern GlobalProperty hw_compat_9_2[];
761 extern const size_t hw_compat_9_2_len;
762 
763 extern GlobalProperty hw_compat_9_1[];
764 extern const size_t hw_compat_9_1_len;
765 
766 extern GlobalProperty hw_compat_9_0[];
767 extern const size_t hw_compat_9_0_len;
768 
769 extern GlobalProperty hw_compat_8_2[];
770 extern const size_t hw_compat_8_2_len;
771 
772 extern GlobalProperty hw_compat_8_1[];
773 extern const size_t hw_compat_8_1_len;
774 
775 extern GlobalProperty hw_compat_8_0[];
776 extern const size_t hw_compat_8_0_len;
777 
778 extern GlobalProperty hw_compat_7_2[];
779 extern const size_t hw_compat_7_2_len;
780 
781 extern GlobalProperty hw_compat_7_1[];
782 extern const size_t hw_compat_7_1_len;
783 
784 extern GlobalProperty hw_compat_7_0[];
785 extern const size_t hw_compat_7_0_len;
786 
787 extern GlobalProperty hw_compat_6_2[];
788 extern const size_t hw_compat_6_2_len;
789 
790 extern GlobalProperty hw_compat_6_1[];
791 extern const size_t hw_compat_6_1_len;
792 
793 extern GlobalProperty hw_compat_6_0[];
794 extern const size_t hw_compat_6_0_len;
795 
796 extern GlobalProperty hw_compat_5_2[];
797 extern const size_t hw_compat_5_2_len;
798 
799 extern GlobalProperty hw_compat_5_1[];
800 extern const size_t hw_compat_5_1_len;
801 
802 extern GlobalProperty hw_compat_5_0[];
803 extern const size_t hw_compat_5_0_len;
804 
805 extern GlobalProperty hw_compat_4_2[];
806 extern const size_t hw_compat_4_2_len;
807 
808 extern GlobalProperty hw_compat_4_1[];
809 extern const size_t hw_compat_4_1_len;
810 
811 extern GlobalProperty hw_compat_4_0[];
812 extern const size_t hw_compat_4_0_len;
813 
814 extern GlobalProperty hw_compat_3_1[];
815 extern const size_t hw_compat_3_1_len;
816 
817 extern GlobalProperty hw_compat_3_0[];
818 extern const size_t hw_compat_3_0_len;
819 
820 extern GlobalProperty hw_compat_2_12[];
821 extern const size_t hw_compat_2_12_len;
822 
823 extern GlobalProperty hw_compat_2_11[];
824 extern const size_t hw_compat_2_11_len;
825 
826 extern GlobalProperty hw_compat_2_10[];
827 extern const size_t hw_compat_2_10_len;
828 
829 extern GlobalProperty hw_compat_2_9[];
830 extern const size_t hw_compat_2_9_len;
831 
832 extern GlobalProperty hw_compat_2_8[];
833 extern const size_t hw_compat_2_8_len;
834 
835 extern GlobalProperty hw_compat_2_7[];
836 extern const size_t hw_compat_2_7_len;
837 
838 extern GlobalProperty hw_compat_2_6[];
839 extern const size_t hw_compat_2_6_len;
840 
841 extern GlobalProperty hw_compat_2_5[];
842 extern const size_t hw_compat_2_5_len;
843 
844 extern GlobalProperty hw_compat_2_4[];
845 extern const size_t hw_compat_2_4_len;
846 
847 #endif
848