xref: /openbmc/qemu/include/hw/boards.h (revision a0258e4afa10a8e9dba4901b7a8202dac24c72e2)
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 "sysemu/accel.h"
10 #include "qapi/qapi-types-machine.h"
11 #include "qemu/module.h"
12 #include "qom/object.h"
13 #include "hw/core/cpu.h"
14 
15 /**
16  * memory_region_allocate_system_memory - Allocate a board's main memory
17  * @mr: the #MemoryRegion to be initialized
18  * @owner: the object that tracks the region's reference count
19  * @name: name of the memory region
20  * @ram_size: size of the region in bytes
21  *
22  * This function allocates the main memory for a board model, and
23  * initializes @mr appropriately. It also arranges for the memory
24  * to be migrated (by calling vmstate_register_ram_global()).
25  *
26  * Memory allocated via this function will be backed with the memory
27  * backend the user provided using "-mem-path" or "-numa node,memdev=..."
28  * if appropriate; this is typically used to cause host huge pages to be
29  * used. This function should therefore be called by a board exactly once,
30  * for the primary or largest RAM area it implements.
31  *
32  * For boards where the major RAM is split into two parts in the memory
33  * map, you can deal with this by calling memory_region_allocate_system_memory()
34  * once to get a MemoryRegion with enough RAM for both parts, and then
35  * creating alias MemoryRegions via memory_region_init_alias() which
36  * alias into different parts of the RAM MemoryRegion and can be mapped
37  * into the memory map in the appropriate places.
38  *
39  * Smaller pieces of memory (display RAM, static RAMs, etc) don't need
40  * to be backed via the -mem-path memory backend and can simply
41  * be created via memory_region_init_ram().
42  */
43 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
44                                           const char *name,
45                                           uint64_t ram_size);
46 
47 #define TYPE_MACHINE_SUFFIX "-machine"
48 
49 /* Machine class name that needs to be used for class-name-based machine
50  * type lookup to work.
51  */
52 #define MACHINE_TYPE_NAME(machinename) (machinename TYPE_MACHINE_SUFFIX)
53 
54 #define TYPE_MACHINE "machine"
55 #undef MACHINE  /* BSD defines it and QEMU does not use it */
56 #define MACHINE(obj) \
57     OBJECT_CHECK(MachineState, (obj), TYPE_MACHINE)
58 #define MACHINE_GET_CLASS(obj) \
59     OBJECT_GET_CLASS(MachineClass, (obj), TYPE_MACHINE)
60 #define MACHINE_CLASS(klass) \
61     OBJECT_CLASS_CHECK(MachineClass, (klass), TYPE_MACHINE)
62 
63 extern MachineState *current_machine;
64 
65 void machine_run_board_init(MachineState *machine);
66 bool machine_usb(MachineState *machine);
67 int machine_phandle_start(MachineState *machine);
68 bool machine_dump_guest_core(MachineState *machine);
69 bool machine_mem_merge(MachineState *machine);
70 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
71 void machine_set_cpu_numa_node(MachineState *machine,
72                                const CpuInstanceProperties *props,
73                                Error **errp);
74 
75 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);
76 /*
77  * Checks that backend isn't used, preps it for exclusive usage and
78  * returns migratable MemoryRegion provided by backend.
79  */
80 MemoryRegion *machine_consume_memdev(MachineState *machine,
81                                      HostMemoryBackend *backend);
82 
83 /**
84  * CPUArchId:
85  * @arch_id - architecture-dependent CPU ID of present or possible CPU
86  * @cpu - pointer to corresponding CPU object if it's present on NULL otherwise
87  * @type - QOM class name of possible @cpu object
88  * @props - CPU object properties, initialized by board
89  * #vcpus_count - number of threads provided by @cpu object
90  */
91 typedef struct CPUArchId {
92     uint64_t arch_id;
93     int64_t vcpus_count;
94     CpuInstanceProperties props;
95     Object *cpu;
96     const char *type;
97 } CPUArchId;
98 
99 /**
100  * CPUArchIdList:
101  * @len - number of @CPUArchId items in @cpus array
102  * @cpus - array of present or possible CPUs for current machine configuration
103  */
104 typedef struct {
105     int len;
106     CPUArchId cpus[0];
107 } CPUArchIdList;
108 
109 /**
110  * MachineClass:
111  * @deprecation_reason: If set, the machine is marked as deprecated. The
112  *    string should provide some clear information about what to use instead.
113  * @max_cpus: maximum number of CPUs supported. Default: 1
114  * @min_cpus: minimum number of CPUs supported. Default: 1
115  * @default_cpus: number of CPUs instantiated if none are specified. Default: 1
116  * @get_hotplug_handler: this function is called during bus-less
117  *    device hotplug. If defined it returns pointer to an instance
118  *    of HotplugHandler object, which handles hotplug operation
119  *    for a given @dev. It may return NULL if @dev doesn't require
120  *    any actions to be performed by hotplug handler.
121  * @cpu_index_to_instance_props:
122  *    used to provide @cpu_index to socket/core/thread number mapping, allowing
123  *    legacy code to perform maping from cpu_index to topology properties
124  *    Returns: tuple of socket/core/thread ids given cpu_index belongs to.
125  *    used to provide @cpu_index to socket number mapping, allowing
126  *    a machine to group CPU threads belonging to the same socket/package
127  *    Returns: socket number given cpu_index belongs to.
128  * @hw_version:
129  *    Value of QEMU_VERSION when the machine was added to QEMU.
130  *    Set only by old machines because they need to keep
131  *    compatibility on code that exposed QEMU_VERSION to guests in
132  *    the past (and now use qemu_hw_version()).
133  * @possible_cpu_arch_ids:
134  *    Returns an array of @CPUArchId architecture-dependent CPU IDs
135  *    which includes CPU IDs for present and possible to hotplug CPUs.
136  *    Caller is responsible for freeing returned list.
137  * @get_default_cpu_node_id:
138  *    returns default board specific node_id value for CPU slot specified by
139  *    index @idx in @ms->possible_cpus[]
140  * @has_hotpluggable_cpus:
141  *    If true, board supports CPUs creation with -device/device_add.
142  * @default_cpu_type:
143  *    specifies default CPU_TYPE, which will be used for parsing target
144  *    specific features and for creating CPUs if CPU name wasn't provided
145  *    explicitly at CLI
146  * @minimum_page_bits:
147  *    If non-zero, the board promises never to create a CPU with a page size
148  *    smaller than this, so QEMU can use a more efficient larger page
149  *    size than the target architecture's minimum. (Attempting to create
150  *    such a CPU will fail.) Note that changing this is a migration
151  *    compatibility break for the machine.
152  * @ignore_memory_transaction_failures:
153  *    If this is flag is true then the CPU will ignore memory transaction
154  *    failures which should cause the CPU to take an exception due to an
155  *    access to an unassigned physical address; the transaction will instead
156  *    return zero (for a read) or be ignored (for a write). This should be
157  *    set only by legacy board models which rely on the old RAZ/WI behaviour
158  *    for handling devices that QEMU does not yet model. New board models
159  *    should instead use "unimplemented-device" for all memory ranges where
160  *    the guest will attempt to probe for a device that QEMU doesn't
161  *    implement and a stub device is required.
162  * @kvm_type:
163  *    Return the type of KVM corresponding to the kvm-type string option or
164  *    computed based on other criteria such as the host kernel capabilities.
165  * @numa_mem_supported:
166  *    true if '--numa node.mem' option is supported and false otherwise
167  * @smp_parse:
168  *    The function pointer to hook different machine specific functions for
169  *    parsing "smp-opts" from QemuOpts to MachineState::CpuTopology and more
170  *    machine specific topology fields, such as smp_dies for PCMachine.
171  * @hotplug_allowed:
172  *    If the hook is provided, then it'll be called for each device
173  *    hotplug to check whether the device hotplug is allowed.  Return
174  *    true to grant allowance or false to reject the hotplug.  When
175  *    false is returned, an error must be set to show the reason of
176  *    the rejection.  If the hook is not provided, all hotplug will be
177  *    allowed.
178  * @default_ram_id:
179  *    Specifies inital RAM MemoryRegion name to be used for default backend
180  *    creation if user explicitly hasn't specified backend with "memory-backend"
181  *    property.
182  *    It also will be used as a way to optin into "-m" option support.
183  *    If it's not set by board, '-m' will be ignored and generic code will
184  *    not create default RAM MemoryRegion.
185  */
186 struct MachineClass {
187     /*< private >*/
188     ObjectClass parent_class;
189     /*< public >*/
190 
191     const char *family; /* NULL iff @name identifies a standalone machtype */
192     char *name;
193     const char *alias;
194     const char *desc;
195     const char *deprecation_reason;
196 
197     void (*init)(MachineState *state);
198     void (*reset)(MachineState *state);
199     void (*wakeup)(MachineState *state);
200     void (*hot_add_cpu)(MachineState *state, const int64_t id, Error **errp);
201     int (*kvm_type)(MachineState *machine, const char *arg);
202     void (*smp_parse)(MachineState *ms, QemuOpts *opts);
203 
204     BlockInterfaceType block_default_type;
205     int units_per_default_bus;
206     int max_cpus;
207     int min_cpus;
208     int default_cpus;
209     unsigned int no_serial:1,
210         no_parallel:1,
211         no_floppy:1,
212         no_cdrom:1,
213         no_sdcard:1,
214         pci_allow_0_address:1,
215         legacy_fw_cfg_order:1;
216     int is_default;
217     const char *default_machine_opts;
218     const char *default_boot_order;
219     const char *default_display;
220     GPtrArray *compat_props;
221     const char *hw_version;
222     ram_addr_t default_ram_size;
223     const char *default_cpu_type;
224     bool default_kernel_irqchip_split;
225     bool option_rom_has_mr;
226     bool rom_file_has_mr;
227     int minimum_page_bits;
228     bool has_hotpluggable_cpus;
229     bool ignore_memory_transaction_failures;
230     int numa_mem_align_shift;
231     const char **valid_cpu_types;
232     strList *allowed_dynamic_sysbus_devices;
233     bool auto_enable_numa_with_memhp;
234     void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,
235                                  int nb_nodes, ram_addr_t size);
236     bool ignore_boot_device_suffixes;
237     bool smbus_no_migration_support;
238     bool nvdimm_supported;
239     bool numa_mem_supported;
240     bool auto_enable_numa;
241     const char *default_ram_id;
242 
243     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
244                                            DeviceState *dev);
245     bool (*hotplug_allowed)(MachineState *state, DeviceState *dev,
246                             Error **errp);
247     CpuInstanceProperties (*cpu_index_to_instance_props)(MachineState *machine,
248                                                          unsigned cpu_index);
249     const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine);
250     int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx);
251 };
252 
253 /**
254  * DeviceMemoryState:
255  * @base: address in guest physical address space where the memory
256  * address space for memory devices starts
257  * @mr: address space container for memory devices
258  */
259 typedef struct DeviceMemoryState {
260     hwaddr base;
261     MemoryRegion mr;
262 } DeviceMemoryState;
263 
264 /**
265  * CpuTopology:
266  * @cpus: the number of present logical processors on the machine
267  * @cores: the number of cores in one package
268  * @threads: the number of threads in one core
269  * @max_cpus: the maximum number of logical processors on the machine
270  */
271 typedef struct CpuTopology {
272     unsigned int cpus;
273     unsigned int cores;
274     unsigned int threads;
275     unsigned int max_cpus;
276 } CpuTopology;
277 
278 /**
279  * MachineState:
280  */
281 struct MachineState {
282     /*< private >*/
283     Object parent_obj;
284     Notifier sysbus_notifier;
285 
286     /*< public >*/
287 
288     char *dtb;
289     char *dumpdtb;
290     int phandle_start;
291     char *dt_compatible;
292     bool dump_guest_core;
293     bool mem_merge;
294     bool usb;
295     bool usb_disabled;
296     char *firmware;
297     bool iommu;
298     bool suppress_vmdesc;
299     bool enforce_config_section;
300     bool enable_graphics;
301     char *memory_encryption;
302     char *ram_memdev_id;
303     /*
304      * convenience alias to ram_memdev_id backend memory region
305      * or to numa container memory region
306      */
307     MemoryRegion *ram;
308     DeviceMemoryState *device_memory;
309 
310     ram_addr_t ram_size;
311     ram_addr_t maxram_size;
312     uint64_t   ram_slots;
313     const char *boot_order;
314     char *kernel_filename;
315     char *kernel_cmdline;
316     char *initrd_filename;
317     const char *cpu_type;
318     AccelState *accelerator;
319     CPUArchIdList *possible_cpus;
320     CpuTopology smp;
321     struct NVDIMMState *nvdimms_state;
322     struct NumaState *numa_state;
323 };
324 
325 #define DEFINE_MACHINE(namestr, machine_initfn) \
326     static void machine_initfn##_class_init(ObjectClass *oc, void *data) \
327     { \
328         MachineClass *mc = MACHINE_CLASS(oc); \
329         machine_initfn(mc); \
330     } \
331     static const TypeInfo machine_initfn##_typeinfo = { \
332         .name       = MACHINE_TYPE_NAME(namestr), \
333         .parent     = TYPE_MACHINE, \
334         .class_init = machine_initfn##_class_init, \
335     }; \
336     static void machine_initfn##_register_types(void) \
337     { \
338         type_register_static(&machine_initfn##_typeinfo); \
339     } \
340     type_init(machine_initfn##_register_types)
341 
342 extern GlobalProperty hw_compat_4_2[];
343 extern const size_t hw_compat_4_2_len;
344 
345 extern GlobalProperty hw_compat_4_1[];
346 extern const size_t hw_compat_4_1_len;
347 
348 extern GlobalProperty hw_compat_4_0[];
349 extern const size_t hw_compat_4_0_len;
350 
351 extern GlobalProperty hw_compat_3_1[];
352 extern const size_t hw_compat_3_1_len;
353 
354 extern GlobalProperty hw_compat_3_0[];
355 extern const size_t hw_compat_3_0_len;
356 
357 extern GlobalProperty hw_compat_2_12[];
358 extern const size_t hw_compat_2_12_len;
359 
360 extern GlobalProperty hw_compat_2_11[];
361 extern const size_t hw_compat_2_11_len;
362 
363 extern GlobalProperty hw_compat_2_10[];
364 extern const size_t hw_compat_2_10_len;
365 
366 extern GlobalProperty hw_compat_2_9[];
367 extern const size_t hw_compat_2_9_len;
368 
369 extern GlobalProperty hw_compat_2_8[];
370 extern const size_t hw_compat_2_8_len;
371 
372 extern GlobalProperty hw_compat_2_7[];
373 extern const size_t hw_compat_2_7_len;
374 
375 extern GlobalProperty hw_compat_2_6[];
376 extern const size_t hw_compat_2_6_len;
377 
378 extern GlobalProperty hw_compat_2_5[];
379 extern const size_t hw_compat_2_5_len;
380 
381 extern GlobalProperty hw_compat_2_4[];
382 extern const size_t hw_compat_2_4_len;
383 
384 extern GlobalProperty hw_compat_2_3[];
385 extern const size_t hw_compat_2_3_len;
386 
387 extern GlobalProperty hw_compat_2_2[];
388 extern const size_t hw_compat_2_2_len;
389 
390 extern GlobalProperty hw_compat_2_1[];
391 extern const size_t hw_compat_2_1_len;
392 
393 #endif
394