xref: /qemu/include/hw/boards.h (revision 84be629d)
1 /* Declarations for use by board files for creating devices.  */
2 
3 #ifndef HW_BOARDS_H
4 #define HW_BOARDS_H
5 
6 #include "sysemu/blockdev.h"
7 #include "sysemu/accel.h"
8 #include "hw/qdev.h"
9 #include "qom/object.h"
10 #include "qom/cpu.h"
11 
12 /**
13  * memory_region_allocate_system_memory - Allocate a board's main memory
14  * @mr: the #MemoryRegion to be initialized
15  * @owner: the object that tracks the region's reference count
16  * @name: name of the memory region
17  * @ram_size: size of the region in bytes
18  *
19  * This function allocates the main memory for a board model, and
20  * initializes @mr appropriately. It also arranges for the memory
21  * to be migrated (by calling vmstate_register_ram_global()).
22  *
23  * Memory allocated via this function will be backed with the memory
24  * backend the user provided using "-mem-path" or "-numa node,memdev=..."
25  * if appropriate; this is typically used to cause host huge pages to be
26  * used. This function should therefore be called by a board exactly once,
27  * for the primary or largest RAM area it implements.
28  *
29  * For boards where the major RAM is split into two parts in the memory
30  * map, you can deal with this by calling memory_region_allocate_system_memory()
31  * once to get a MemoryRegion with enough RAM for both parts, and then
32  * creating alias MemoryRegions via memory_region_init_alias() which
33  * alias into different parts of the RAM MemoryRegion and can be mapped
34  * into the memory map in the appropriate places.
35  *
36  * Smaller pieces of memory (display RAM, static RAMs, etc) don't need
37  * to be backed via the -mem-path memory backend and can simply
38  * be created via memory_region_allocate_aux_memory() or
39  * memory_region_init_ram().
40  */
41 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
42                                           const char *name,
43                                           uint64_t ram_size);
44 
45 #define TYPE_MACHINE_SUFFIX "-machine"
46 
47 /* Machine class name that needs to be used for class-name-based machine
48  * type lookup to work.
49  */
50 #define MACHINE_TYPE_NAME(machinename) (machinename TYPE_MACHINE_SUFFIX)
51 
52 #define TYPE_MACHINE "machine"
53 #undef MACHINE  /* BSD defines it and QEMU does not use it */
54 #define MACHINE(obj) \
55     OBJECT_CHECK(MachineState, (obj), TYPE_MACHINE)
56 #define MACHINE_GET_CLASS(obj) \
57     OBJECT_GET_CLASS(MachineClass, (obj), TYPE_MACHINE)
58 #define MACHINE_CLASS(klass) \
59     OBJECT_CLASS_CHECK(MachineClass, (klass), TYPE_MACHINE)
60 
61 MachineClass *find_default_machine(void);
62 extern MachineState *current_machine;
63 
64 void machine_run_board_init(MachineState *machine);
65 bool machine_usb(MachineState *machine);
66 bool machine_kernel_irqchip_allowed(MachineState *machine);
67 bool machine_kernel_irqchip_required(MachineState *machine);
68 bool machine_kernel_irqchip_split(MachineState *machine);
69 int machine_kvm_shadow_mem(MachineState *machine);
70 int machine_phandle_start(MachineState *machine);
71 bool machine_dump_guest_core(MachineState *machine);
72 bool machine_mem_merge(MachineState *machine);
73 void machine_register_compat_props(MachineState *machine);
74 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine);
75 void machine_set_cpu_numa_node(MachineState *machine,
76                                const CpuInstanceProperties *props,
77                                Error **errp);
78 
79 /**
80  * CPUArchId:
81  * @arch_id - architecture-dependent CPU ID of present or possible CPU
82  * @cpu - pointer to corresponding CPU object if it's present on NULL otherwise
83  * @props - CPU object properties, initialized by board
84  * #vcpus_count - number of threads provided by @cpu object
85  */
86 typedef struct {
87     uint64_t arch_id;
88     int64_t vcpus_count;
89     CpuInstanceProperties props;
90     Object *cpu;
91 } CPUArchId;
92 
93 /**
94  * CPUArchIdList:
95  * @len - number of @CPUArchId items in @cpus array
96  * @cpus - array of present or possible CPUs for current machine configuration
97  */
98 typedef struct {
99     int len;
100     CPUArchId cpus[0];
101 } CPUArchIdList;
102 
103 /**
104  * MachineClass:
105  * @max_cpus: maximum number of CPUs supported. Default: 1
106  * @min_cpus: minimum number of CPUs supported. Default: 1
107  * @default_cpus: number of CPUs instantiated if none are specified. Default: 1
108  * @get_hotplug_handler: this function is called during bus-less
109  *    device hotplug. If defined it returns pointer to an instance
110  *    of HotplugHandler object, which handles hotplug operation
111  *    for a given @dev. It may return NULL if @dev doesn't require
112  *    any actions to be performed by hotplug handler.
113  * @cpu_index_to_instance_props:
114  *    used to provide @cpu_index to socket/core/thread number mapping, allowing
115  *    legacy code to perform maping from cpu_index to topology properties
116  *    Returns: tuple of socket/core/thread ids given cpu_index belongs to.
117  *    used to provide @cpu_index to socket number mapping, allowing
118  *    a machine to group CPU threads belonging to the same socket/package
119  *    Returns: socket number given cpu_index belongs to.
120  * @hw_version:
121  *    Value of QEMU_VERSION when the machine was added to QEMU.
122  *    Set only by old machines because they need to keep
123  *    compatibility on code that exposed QEMU_VERSION to guests in
124  *    the past (and now use qemu_hw_version()).
125  * @possible_cpu_arch_ids:
126  *    Returns an array of @CPUArchId architecture-dependent CPU IDs
127  *    which includes CPU IDs for present and possible to hotplug CPUs.
128  *    Caller is responsible for freeing returned list.
129  * @get_default_cpu_node_id:
130  *    returns default board specific node_id value for CPU slot specified by
131  *    index @idx in @ms->possible_cpus[]
132  * @has_hotpluggable_cpus:
133  *    If true, board supports CPUs creation with -device/device_add.
134  * @default_cpu_type:
135  *    specifies default CPU_TYPE, which will be used for parsing target
136  *    specific features and for creating CPUs if CPU name wasn't provided
137  *    explicitly at CLI
138  * @minimum_page_bits:
139  *    If non-zero, the board promises never to create a CPU with a page size
140  *    smaller than this, so QEMU can use a more efficient larger page
141  *    size than the target architecture's minimum. (Attempting to create
142  *    such a CPU will fail.) Note that changing this is a migration
143  *    compatibility break for the machine.
144  * @ignore_memory_transaction_failures:
145  *    If this is flag is true then the CPU will ignore memory transaction
146  *    failures which should cause the CPU to take an exception due to an
147  *    access to an unassigned physical address; the transaction will instead
148  *    return zero (for a read) or be ignored (for a write). This should be
149  *    set only by legacy board models which rely on the old RAZ/WI behaviour
150  *    for handling devices that QEMU does not yet model. New board models
151  *    should instead use "unimplemented-device" for all memory ranges where
152  *    the guest will attempt to probe for a device that QEMU doesn't
153  *    implement and a stub device is required.
154  */
155 struct MachineClass {
156     /*< private >*/
157     ObjectClass parent_class;
158     /*< public >*/
159 
160     const char *family; /* NULL iff @name identifies a standalone machtype */
161     char *name;
162     const char *alias;
163     const char *desc;
164 
165     void (*init)(MachineState *state);
166     void (*reset)(void);
167     void (*hot_add_cpu)(const int64_t id, Error **errp);
168     int (*kvm_type)(const char *arg);
169 
170     BlockInterfaceType block_default_type;
171     int units_per_default_bus;
172     int max_cpus;
173     int min_cpus;
174     int default_cpus;
175     unsigned int no_serial:1,
176         no_parallel:1,
177         use_virtcon:1,
178         use_sclp:1,
179         no_floppy:1,
180         no_cdrom:1,
181         no_sdcard:1,
182         has_dynamic_sysbus:1,
183         pci_allow_0_address:1,
184         legacy_fw_cfg_order:1;
185     int is_default;
186     const char *default_machine_opts;
187     const char *default_boot_order;
188     const char *default_display;
189     GArray *compat_props;
190     const char *hw_version;
191     ram_addr_t default_ram_size;
192     const char *default_cpu_type;
193     bool option_rom_has_mr;
194     bool rom_file_has_mr;
195     int minimum_page_bits;
196     bool has_hotpluggable_cpus;
197     bool ignore_memory_transaction_failures;
198     int numa_mem_align_shift;
199     const char **valid_cpu_types;
200     bool auto_enable_numa_with_memhp;
201     void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,
202                                  int nb_nodes, ram_addr_t size);
203 
204     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
205                                            DeviceState *dev);
206     CpuInstanceProperties (*cpu_index_to_instance_props)(MachineState *machine,
207                                                          unsigned cpu_index);
208     const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine);
209     int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx);
210 };
211 
212 /**
213  * MachineState:
214  */
215 struct MachineState {
216     /*< private >*/
217     Object parent_obj;
218     Notifier sysbus_notifier;
219 
220     /*< public >*/
221 
222     char *accel;
223     bool kernel_irqchip_allowed;
224     bool kernel_irqchip_required;
225     bool kernel_irqchip_split;
226     int kvm_shadow_mem;
227     char *dtb;
228     char *dumpdtb;
229     int phandle_start;
230     char *dt_compatible;
231     bool dump_guest_core;
232     bool mem_merge;
233     bool usb;
234     bool usb_disabled;
235     bool igd_gfx_passthru;
236     char *firmware;
237     bool iommu;
238     bool suppress_vmdesc;
239     bool enforce_config_section;
240     bool enable_graphics;
241 
242     ram_addr_t ram_size;
243     ram_addr_t maxram_size;
244     uint64_t   ram_slots;
245     const char *boot_order;
246     char *kernel_filename;
247     char *kernel_cmdline;
248     char *initrd_filename;
249     const char *cpu_model;
250     const char *cpu_type;
251     AccelState *accelerator;
252     CPUArchIdList *possible_cpus;
253 };
254 
255 #define DEFINE_MACHINE(namestr, machine_initfn) \
256     static void machine_initfn##_class_init(ObjectClass *oc, void *data) \
257     { \
258         MachineClass *mc = MACHINE_CLASS(oc); \
259         machine_initfn(mc); \
260     } \
261     static const TypeInfo machine_initfn##_typeinfo = { \
262         .name       = MACHINE_TYPE_NAME(namestr), \
263         .parent     = TYPE_MACHINE, \
264         .class_init = machine_initfn##_class_init, \
265     }; \
266     static void machine_initfn##_register_types(void) \
267     { \
268         type_register_static(&machine_initfn##_typeinfo); \
269     } \
270     type_init(machine_initfn##_register_types)
271 
272 #define SET_MACHINE_COMPAT(m, COMPAT) \
273     do {                              \
274         int i;                        \
275         static GlobalProperty props[] = {       \
276             COMPAT                              \
277             { /* end of list */ }               \
278         };                                      \
279         if (!m->compat_props) { \
280             m->compat_props = g_array_new(false, false, sizeof(void *)); \
281         } \
282         for (i = 0; props[i].driver != NULL; i++) {    \
283             GlobalProperty *prop = &props[i];          \
284             g_array_append_val(m->compat_props, prop); \
285         }                                              \
286     } while (0)
287 
288 #endif
289