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