spapr: Fold h_cas_compose_response() into h_client_architecture_support()
[qemu/kevin.git] / hw / ppc / spapr.c
blob3dedb41d48a8c50408b3a5e8de27ad7b09974716
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * Copyright (c) 2004-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 * Copyright (c) 2010 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "qemu/osdep.h"
28 #include "qemu-common.h"
29 #include "qapi/error.h"
30 #include "qapi/visitor.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/hostmem.h"
33 #include "sysemu/numa.h"
34 #include "sysemu/qtest.h"
35 #include "sysemu/reset.h"
36 #include "sysemu/runstate.h"
37 #include "qemu/log.h"
38 #include "hw/fw-path-provider.h"
39 #include "elf.h"
40 #include "net/net.h"
41 #include "sysemu/device_tree.h"
42 #include "sysemu/cpus.h"
43 #include "sysemu/hw_accel.h"
44 #include "kvm_ppc.h"
45 #include "migration/misc.h"
46 #include "migration/qemu-file-types.h"
47 #include "migration/global_state.h"
48 #include "migration/register.h"
49 #include "mmu-hash64.h"
50 #include "mmu-book3s-v3.h"
51 #include "cpu-models.h"
52 #include "hw/core/cpu.h"
54 #include "hw/boards.h"
55 #include "hw/ppc/ppc.h"
56 #include "hw/loader.h"
58 #include "hw/ppc/fdt.h"
59 #include "hw/ppc/spapr.h"
60 #include "hw/ppc/spapr_vio.h"
61 #include "hw/qdev-properties.h"
62 #include "hw/pci-host/spapr.h"
63 #include "hw/pci/msi.h"
65 #include "hw/pci/pci.h"
66 #include "hw/scsi/scsi.h"
67 #include "hw/virtio/virtio-scsi.h"
68 #include "hw/virtio/vhost-scsi-common.h"
70 #include "exec/address-spaces.h"
71 #include "exec/ram_addr.h"
72 #include "hw/usb.h"
73 #include "qemu/config-file.h"
74 #include "qemu/error-report.h"
75 #include "trace.h"
76 #include "hw/nmi.h"
77 #include "hw/intc/intc.h"
79 #include "hw/ppc/spapr_cpu_core.h"
80 #include "hw/mem/memory-device.h"
81 #include "hw/ppc/spapr_tpm_proxy.h"
83 #include "monitor/monitor.h"
85 #include <libfdt.h>
87 /* SLOF memory layout:
89 * SLOF raw image loaded at 0, copies its romfs right below the flat
90 * device-tree, then position SLOF itself 31M below that
92 * So we set FW_OVERHEAD to 40MB which should account for all of that
93 * and more
95 * We load our kernel at 4M, leaving space for SLOF initial image
97 #define FDT_MAX_SIZE 0x100000
98 #define RTAS_MAX_ADDR 0x80000000 /* RTAS must stay below that */
99 #define FW_MAX_SIZE 0x400000
100 #define FW_FILE_NAME "slof.bin"
101 #define FW_OVERHEAD 0x2800000
102 #define KERNEL_LOAD_ADDR FW_MAX_SIZE
104 #define MIN_RMA_SLOF 128UL
106 #define PHANDLE_INTC 0x00001111
108 /* These two functions implement the VCPU id numbering: one to compute them
109 * all and one to identify thread 0 of a VCORE. Any change to the first one
110 * is likely to have an impact on the second one, so let's keep them close.
112 static int spapr_vcpu_id(SpaprMachineState *spapr, int cpu_index)
114 MachineState *ms = MACHINE(spapr);
115 unsigned int smp_threads = ms->smp.threads;
117 assert(spapr->vsmt);
118 return
119 (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads;
121 static bool spapr_is_thread0_in_vcore(SpaprMachineState *spapr,
122 PowerPCCPU *cpu)
124 assert(spapr->vsmt);
125 return spapr_get_vcpu_id(cpu) % spapr->vsmt == 0;
128 static bool pre_2_10_vmstate_dummy_icp_needed(void *opaque)
130 /* Dummy entries correspond to unused ICPState objects in older QEMUs,
131 * and newer QEMUs don't even have them. In both cases, we don't want
132 * to send anything on the wire.
134 return false;
137 static const VMStateDescription pre_2_10_vmstate_dummy_icp = {
138 .name = "icp/server",
139 .version_id = 1,
140 .minimum_version_id = 1,
141 .needed = pre_2_10_vmstate_dummy_icp_needed,
142 .fields = (VMStateField[]) {
143 VMSTATE_UNUSED(4), /* uint32_t xirr */
144 VMSTATE_UNUSED(1), /* uint8_t pending_priority */
145 VMSTATE_UNUSED(1), /* uint8_t mfrr */
146 VMSTATE_END_OF_LIST()
150 static void pre_2_10_vmstate_register_dummy_icp(int i)
152 vmstate_register(NULL, i, &pre_2_10_vmstate_dummy_icp,
153 (void *)(uintptr_t) i);
156 static void pre_2_10_vmstate_unregister_dummy_icp(int i)
158 vmstate_unregister(NULL, &pre_2_10_vmstate_dummy_icp,
159 (void *)(uintptr_t) i);
162 int spapr_max_server_number(SpaprMachineState *spapr)
164 MachineState *ms = MACHINE(spapr);
166 assert(spapr->vsmt);
167 return DIV_ROUND_UP(ms->smp.max_cpus * spapr->vsmt, ms->smp.threads);
170 static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
171 int smt_threads)
173 int i, ret = 0;
174 uint32_t servers_prop[smt_threads];
175 uint32_t gservers_prop[smt_threads * 2];
176 int index = spapr_get_vcpu_id(cpu);
178 if (cpu->compat_pvr) {
179 ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
180 if (ret < 0) {
181 return ret;
185 /* Build interrupt servers and gservers properties */
186 for (i = 0; i < smt_threads; i++) {
187 servers_prop[i] = cpu_to_be32(index + i);
188 /* Hack, direct the group queues back to cpu 0 */
189 gservers_prop[i*2] = cpu_to_be32(index + i);
190 gservers_prop[i*2 + 1] = 0;
192 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
193 servers_prop, sizeof(servers_prop));
194 if (ret < 0) {
195 return ret;
197 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
198 gservers_prop, sizeof(gservers_prop));
200 return ret;
203 static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu)
205 int index = spapr_get_vcpu_id(cpu);
206 uint32_t associativity[] = {cpu_to_be32(0x5),
207 cpu_to_be32(0x0),
208 cpu_to_be32(0x0),
209 cpu_to_be32(0x0),
210 cpu_to_be32(cpu->node_id),
211 cpu_to_be32(index)};
213 /* Advertise NUMA via ibm,associativity */
214 return fdt_setprop(fdt, offset, "ibm,associativity", associativity,
215 sizeof(associativity));
218 /* Populate the "ibm,pa-features" property */
219 static void spapr_populate_pa_features(SpaprMachineState *spapr,
220 PowerPCCPU *cpu,
221 void *fdt, int offset)
223 uint8_t pa_features_206[] = { 6, 0,
224 0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
225 uint8_t pa_features_207[] = { 24, 0,
226 0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
227 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
229 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
230 uint8_t pa_features_300[] = { 66, 0,
231 /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
232 /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
233 0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
234 /* 6: DS207 */
235 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
236 /* 16: Vector */
237 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
238 /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
239 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
240 /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
241 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
242 /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
243 0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
244 /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
245 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
246 /* 42: PM, 44: PC RA, 46: SC vec'd */
247 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
248 /* 48: SIMD, 50: QP BFP, 52: String */
249 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
250 /* 54: DecFP, 56: DecI, 58: SHA */
251 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
252 /* 60: NM atomic, 62: RNG */
253 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
255 uint8_t *pa_features = NULL;
256 size_t pa_size;
258 if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_06, 0, cpu->compat_pvr)) {
259 pa_features = pa_features_206;
260 pa_size = sizeof(pa_features_206);
262 if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_07, 0, cpu->compat_pvr)) {
263 pa_features = pa_features_207;
264 pa_size = sizeof(pa_features_207);
266 if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0, cpu->compat_pvr)) {
267 pa_features = pa_features_300;
268 pa_size = sizeof(pa_features_300);
270 if (!pa_features) {
271 return;
274 if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) {
276 * Note: we keep CI large pages off by default because a 64K capable
277 * guest provisioned with large pages might otherwise try to map a qemu
278 * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
279 * even if that qemu runs on a 4k host.
280 * We dd this bit back here if we are confident this is not an issue
282 pa_features[3] |= 0x20;
284 if ((spapr_get_cap(spapr, SPAPR_CAP_HTM) != 0) && pa_size > 24) {
285 pa_features[24] |= 0x80; /* Transactional memory support */
287 if (spapr->cas_pre_isa3_guest && pa_size > 40) {
288 /* Workaround for broken kernels that attempt (guest) radix
289 * mode when they can't handle it, if they see the radix bit set
290 * in pa-features. So hide it from them. */
291 pa_features[40 + 2] &= ~0x80; /* Radix MMU */
294 _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
297 static hwaddr spapr_node0_size(MachineState *machine)
299 if (machine->numa_state->num_nodes) {
300 int i;
301 for (i = 0; i < machine->numa_state->num_nodes; ++i) {
302 if (machine->numa_state->nodes[i].node_mem) {
303 return MIN(pow2floor(machine->numa_state->nodes[i].node_mem),
304 machine->ram_size);
308 return machine->ram_size;
311 static void add_str(GString *s, const gchar *s1)
313 g_string_append_len(s, s1, strlen(s1) + 1);
316 static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
317 hwaddr size)
319 uint32_t associativity[] = {
320 cpu_to_be32(0x4), /* length */
321 cpu_to_be32(0x0), cpu_to_be32(0x0),
322 cpu_to_be32(0x0), cpu_to_be32(nodeid)
324 char mem_name[32];
325 uint64_t mem_reg_property[2];
326 int off;
328 mem_reg_property[0] = cpu_to_be64(start);
329 mem_reg_property[1] = cpu_to_be64(size);
331 sprintf(mem_name, "memory@%" HWADDR_PRIx, start);
332 off = fdt_add_subnode(fdt, 0, mem_name);
333 _FDT(off);
334 _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
335 _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
336 sizeof(mem_reg_property))));
337 _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
338 sizeof(associativity))));
339 return off;
342 static int spapr_populate_memory(SpaprMachineState *spapr, void *fdt)
344 MachineState *machine = MACHINE(spapr);
345 hwaddr mem_start, node_size;
346 int i, nb_nodes = machine->numa_state->num_nodes;
347 NodeInfo *nodes = machine->numa_state->nodes;
349 for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
350 if (!nodes[i].node_mem) {
351 continue;
353 if (mem_start >= machine->ram_size) {
354 node_size = 0;
355 } else {
356 node_size = nodes[i].node_mem;
357 if (node_size > machine->ram_size - mem_start) {
358 node_size = machine->ram_size - mem_start;
361 if (!mem_start) {
362 /* spapr_machine_init() checks for rma_size <= node0_size
363 * already */
364 spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
365 mem_start += spapr->rma_size;
366 node_size -= spapr->rma_size;
368 for ( ; node_size; ) {
369 hwaddr sizetmp = pow2floor(node_size);
371 /* mem_start != 0 here */
372 if (ctzl(mem_start) < ctzl(sizetmp)) {
373 sizetmp = 1ULL << ctzl(mem_start);
376 spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
377 node_size -= sizetmp;
378 mem_start += sizetmp;
382 return 0;
385 static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
386 SpaprMachineState *spapr)
388 MachineState *ms = MACHINE(spapr);
389 PowerPCCPU *cpu = POWERPC_CPU(cs);
390 CPUPPCState *env = &cpu->env;
391 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
392 int index = spapr_get_vcpu_id(cpu);
393 uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
394 0xffffffff, 0xffffffff};
395 uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
396 : SPAPR_TIMEBASE_FREQ;
397 uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
398 uint32_t page_sizes_prop[64];
399 size_t page_sizes_prop_size;
400 unsigned int smp_threads = ms->smp.threads;
401 uint32_t vcpus_per_socket = smp_threads * ms->smp.cores;
402 uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
403 int compat_smt = MIN(smp_threads, ppc_compat_max_vthreads(cpu));
404 SpaprDrc *drc;
405 int drc_index;
406 uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
407 int i;
409 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index);
410 if (drc) {
411 drc_index = spapr_drc_index(drc);
412 _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
415 _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
416 _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
418 _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
419 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
420 env->dcache_line_size)));
421 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
422 env->dcache_line_size)));
423 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
424 env->icache_line_size)));
425 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
426 env->icache_line_size)));
428 if (pcc->l1_dcache_size) {
429 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
430 pcc->l1_dcache_size)));
431 } else {
432 warn_report("Unknown L1 dcache size for cpu");
434 if (pcc->l1_icache_size) {
435 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
436 pcc->l1_icache_size)));
437 } else {
438 warn_report("Unknown L1 icache size for cpu");
441 _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
442 _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
443 _FDT((fdt_setprop_cell(fdt, offset, "slb-size", cpu->hash64_opts->slb_size)));
444 _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", cpu->hash64_opts->slb_size)));
445 _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
446 _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
448 if (env->spr_cb[SPR_PURR].oea_read) {
449 _FDT((fdt_setprop_cell(fdt, offset, "ibm,purr", 1)));
451 if (env->spr_cb[SPR_SPURR].oea_read) {
452 _FDT((fdt_setprop_cell(fdt, offset, "ibm,spurr", 1)));
455 if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)) {
456 _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
457 segs, sizeof(segs))));
460 /* Advertise VSX (vector extensions) if available
461 * 1 == VMX / Altivec available
462 * 2 == VSX available
464 * Only CPUs for which we create core types in spapr_cpu_core.c
465 * are possible, and all of those have VMX */
466 if (spapr_get_cap(spapr, SPAPR_CAP_VSX) != 0) {
467 _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", 2)));
468 } else {
469 _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", 1)));
472 /* Advertise DFP (Decimal Floating Point) if available
473 * 0 / no property == no DFP
474 * 1 == DFP available */
475 if (spapr_get_cap(spapr, SPAPR_CAP_DFP) != 0) {
476 _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
479 page_sizes_prop_size = ppc_create_page_sizes_prop(cpu, page_sizes_prop,
480 sizeof(page_sizes_prop));
481 if (page_sizes_prop_size) {
482 _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
483 page_sizes_prop, page_sizes_prop_size)));
486 spapr_populate_pa_features(spapr, cpu, fdt, offset);
488 _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
489 cs->cpu_index / vcpus_per_socket)));
491 _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
492 pft_size_prop, sizeof(pft_size_prop))));
494 if (ms->numa_state->num_nodes > 1) {
495 _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cpu));
498 _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
500 if (pcc->radix_page_info) {
501 for (i = 0; i < pcc->radix_page_info->count; i++) {
502 radix_AP_encodings[i] =
503 cpu_to_be32(pcc->radix_page_info->entries[i]);
505 _FDT((fdt_setprop(fdt, offset, "ibm,processor-radix-AP-encodings",
506 radix_AP_encodings,
507 pcc->radix_page_info->count *
508 sizeof(radix_AP_encodings[0]))));
512 * We set this property to let the guest know that it can use the large
513 * decrementer and its width in bits.
515 if (spapr_get_cap(spapr, SPAPR_CAP_LARGE_DECREMENTER) != SPAPR_CAP_OFF)
516 _FDT((fdt_setprop_u32(fdt, offset, "ibm,dec-bits",
517 pcc->lrg_decr_bits)));
520 static void spapr_populate_cpus_dt_node(void *fdt, SpaprMachineState *spapr)
522 CPUState **rev;
523 CPUState *cs;
524 int n_cpus;
525 int cpus_offset;
526 char *nodename;
527 int i;
529 cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
530 _FDT(cpus_offset);
531 _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
532 _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
535 * We walk the CPUs in reverse order to ensure that CPU DT nodes
536 * created by fdt_add_subnode() end up in the right order in FDT
537 * for the guest kernel the enumerate the CPUs correctly.
539 * The CPU list cannot be traversed in reverse order, so we need
540 * to do extra work.
542 n_cpus = 0;
543 rev = NULL;
544 CPU_FOREACH(cs) {
545 rev = g_renew(CPUState *, rev, n_cpus + 1);
546 rev[n_cpus++] = cs;
549 for (i = n_cpus - 1; i >= 0; i--) {
550 CPUState *cs = rev[i];
551 PowerPCCPU *cpu = POWERPC_CPU(cs);
552 int index = spapr_get_vcpu_id(cpu);
553 DeviceClass *dc = DEVICE_GET_CLASS(cs);
554 int offset;
556 if (!spapr_is_thread0_in_vcore(spapr, cpu)) {
557 continue;
560 nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
561 offset = fdt_add_subnode(fdt, cpus_offset, nodename);
562 g_free(nodename);
563 _FDT(offset);
564 spapr_populate_cpu_dt(cs, fdt, offset, spapr);
567 g_free(rev);
570 static int spapr_rng_populate_dt(void *fdt)
572 int node;
573 int ret;
575 node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities");
576 if (node <= 0) {
577 return -1;
579 ret = fdt_setprop_string(fdt, node, "device_type",
580 "ibm,platform-facilities");
581 ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1);
582 ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0);
584 node = fdt_add_subnode(fdt, node, "ibm,random-v1");
585 if (node <= 0) {
586 return -1;
588 ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random");
590 return ret ? -1 : 0;
593 static uint32_t spapr_pc_dimm_node(MemoryDeviceInfoList *list, ram_addr_t addr)
595 MemoryDeviceInfoList *info;
597 for (info = list; info; info = info->next) {
598 MemoryDeviceInfo *value = info->value;
600 if (value && value->type == MEMORY_DEVICE_INFO_KIND_DIMM) {
601 PCDIMMDeviceInfo *pcdimm_info = value->u.dimm.data;
603 if (addr >= pcdimm_info->addr &&
604 addr < (pcdimm_info->addr + pcdimm_info->size)) {
605 return pcdimm_info->node;
610 return -1;
613 struct sPAPRDrconfCellV2 {
614 uint32_t seq_lmbs;
615 uint64_t base_addr;
616 uint32_t drc_index;
617 uint32_t aa_index;
618 uint32_t flags;
619 } QEMU_PACKED;
621 typedef struct DrconfCellQueue {
622 struct sPAPRDrconfCellV2 cell;
623 QSIMPLEQ_ENTRY(DrconfCellQueue) entry;
624 } DrconfCellQueue;
626 static DrconfCellQueue *
627 spapr_get_drconf_cell(uint32_t seq_lmbs, uint64_t base_addr,
628 uint32_t drc_index, uint32_t aa_index,
629 uint32_t flags)
631 DrconfCellQueue *elem;
633 elem = g_malloc0(sizeof(*elem));
634 elem->cell.seq_lmbs = cpu_to_be32(seq_lmbs);
635 elem->cell.base_addr = cpu_to_be64(base_addr);
636 elem->cell.drc_index = cpu_to_be32(drc_index);
637 elem->cell.aa_index = cpu_to_be32(aa_index);
638 elem->cell.flags = cpu_to_be32(flags);
640 return elem;
643 /* ibm,dynamic-memory-v2 */
644 static int spapr_populate_drmem_v2(SpaprMachineState *spapr, void *fdt,
645 int offset, MemoryDeviceInfoList *dimms)
647 MachineState *machine = MACHINE(spapr);
648 uint8_t *int_buf, *cur_index;
649 int ret;
650 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
651 uint64_t addr, cur_addr, size;
652 uint32_t nr_boot_lmbs = (machine->device_memory->base / lmb_size);
653 uint64_t mem_end = machine->device_memory->base +
654 memory_region_size(&machine->device_memory->mr);
655 uint32_t node, buf_len, nr_entries = 0;
656 SpaprDrc *drc;
657 DrconfCellQueue *elem, *next;
658 MemoryDeviceInfoList *info;
659 QSIMPLEQ_HEAD(, DrconfCellQueue) drconf_queue
660 = QSIMPLEQ_HEAD_INITIALIZER(drconf_queue);
662 /* Entry to cover RAM and the gap area */
663 elem = spapr_get_drconf_cell(nr_boot_lmbs, 0, 0, -1,
664 SPAPR_LMB_FLAGS_RESERVED |
665 SPAPR_LMB_FLAGS_DRC_INVALID);
666 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
667 nr_entries++;
669 cur_addr = machine->device_memory->base;
670 for (info = dimms; info; info = info->next) {
671 PCDIMMDeviceInfo *di = info->value->u.dimm.data;
673 addr = di->addr;
674 size = di->size;
675 node = di->node;
677 /* Entry for hot-pluggable area */
678 if (cur_addr < addr) {
679 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, cur_addr / lmb_size);
680 g_assert(drc);
681 elem = spapr_get_drconf_cell((addr - cur_addr) / lmb_size,
682 cur_addr, spapr_drc_index(drc), -1, 0);
683 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
684 nr_entries++;
687 /* Entry for DIMM */
688 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, addr / lmb_size);
689 g_assert(drc);
690 elem = spapr_get_drconf_cell(size / lmb_size, addr,
691 spapr_drc_index(drc), node,
692 SPAPR_LMB_FLAGS_ASSIGNED);
693 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
694 nr_entries++;
695 cur_addr = addr + size;
698 /* Entry for remaining hotpluggable area */
699 if (cur_addr < mem_end) {
700 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, cur_addr / lmb_size);
701 g_assert(drc);
702 elem = spapr_get_drconf_cell((mem_end - cur_addr) / lmb_size,
703 cur_addr, spapr_drc_index(drc), -1, 0);
704 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
705 nr_entries++;
708 buf_len = nr_entries * sizeof(struct sPAPRDrconfCellV2) + sizeof(uint32_t);
709 int_buf = cur_index = g_malloc0(buf_len);
710 *(uint32_t *)int_buf = cpu_to_be32(nr_entries);
711 cur_index += sizeof(nr_entries);
713 QSIMPLEQ_FOREACH_SAFE(elem, &drconf_queue, entry, next) {
714 memcpy(cur_index, &elem->cell, sizeof(elem->cell));
715 cur_index += sizeof(elem->cell);
716 QSIMPLEQ_REMOVE(&drconf_queue, elem, DrconfCellQueue, entry);
717 g_free(elem);
720 ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory-v2", int_buf, buf_len);
721 g_free(int_buf);
722 if (ret < 0) {
723 return -1;
725 return 0;
728 /* ibm,dynamic-memory */
729 static int spapr_populate_drmem_v1(SpaprMachineState *spapr, void *fdt,
730 int offset, MemoryDeviceInfoList *dimms)
732 MachineState *machine = MACHINE(spapr);
733 int i, ret;
734 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
735 uint32_t device_lmb_start = machine->device_memory->base / lmb_size;
736 uint32_t nr_lmbs = (machine->device_memory->base +
737 memory_region_size(&machine->device_memory->mr)) /
738 lmb_size;
739 uint32_t *int_buf, *cur_index, buf_len;
742 * Allocate enough buffer size to fit in ibm,dynamic-memory
744 buf_len = (nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1) * sizeof(uint32_t);
745 cur_index = int_buf = g_malloc0(buf_len);
746 int_buf[0] = cpu_to_be32(nr_lmbs);
747 cur_index++;
748 for (i = 0; i < nr_lmbs; i++) {
749 uint64_t addr = i * lmb_size;
750 uint32_t *dynamic_memory = cur_index;
752 if (i >= device_lmb_start) {
753 SpaprDrc *drc;
755 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, i);
756 g_assert(drc);
758 dynamic_memory[0] = cpu_to_be32(addr >> 32);
759 dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
760 dynamic_memory[2] = cpu_to_be32(spapr_drc_index(drc));
761 dynamic_memory[3] = cpu_to_be32(0); /* reserved */
762 dynamic_memory[4] = cpu_to_be32(spapr_pc_dimm_node(dimms, addr));
763 if (memory_region_present(get_system_memory(), addr)) {
764 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
765 } else {
766 dynamic_memory[5] = cpu_to_be32(0);
768 } else {
770 * LMB information for RMA, boot time RAM and gap b/n RAM and
771 * device memory region -- all these are marked as reserved
772 * and as having no valid DRC.
774 dynamic_memory[0] = cpu_to_be32(addr >> 32);
775 dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
776 dynamic_memory[2] = cpu_to_be32(0);
777 dynamic_memory[3] = cpu_to_be32(0); /* reserved */
778 dynamic_memory[4] = cpu_to_be32(-1);
779 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
780 SPAPR_LMB_FLAGS_DRC_INVALID);
783 cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
785 ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
786 g_free(int_buf);
787 if (ret < 0) {
788 return -1;
790 return 0;
794 * Adds ibm,dynamic-reconfiguration-memory node.
795 * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
796 * of this device tree node.
798 static int spapr_populate_drconf_memory(SpaprMachineState *spapr, void *fdt)
800 MachineState *machine = MACHINE(spapr);
801 int nb_numa_nodes = machine->numa_state->num_nodes;
802 int ret, i, offset;
803 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
804 uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
805 uint32_t *int_buf, *cur_index, buf_len;
806 int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
807 MemoryDeviceInfoList *dimms = NULL;
810 * Don't create the node if there is no device memory
812 if (machine->ram_size == machine->maxram_size) {
813 return 0;
816 offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
818 ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
819 sizeof(prop_lmb_size));
820 if (ret < 0) {
821 return ret;
824 ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
825 if (ret < 0) {
826 return ret;
829 ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
830 if (ret < 0) {
831 return ret;
834 /* ibm,dynamic-memory or ibm,dynamic-memory-v2 */
835 dimms = qmp_memory_device_list();
836 if (spapr_ovec_test(spapr->ov5_cas, OV5_DRMEM_V2)) {
837 ret = spapr_populate_drmem_v2(spapr, fdt, offset, dimms);
838 } else {
839 ret = spapr_populate_drmem_v1(spapr, fdt, offset, dimms);
841 qapi_free_MemoryDeviceInfoList(dimms);
843 if (ret < 0) {
844 return ret;
847 /* ibm,associativity-lookup-arrays */
848 buf_len = (nr_nodes * 4 + 2) * sizeof(uint32_t);
849 cur_index = int_buf = g_malloc0(buf_len);
850 int_buf[0] = cpu_to_be32(nr_nodes);
851 int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
852 cur_index += 2;
853 for (i = 0; i < nr_nodes; i++) {
854 uint32_t associativity[] = {
855 cpu_to_be32(0x0),
856 cpu_to_be32(0x0),
857 cpu_to_be32(0x0),
858 cpu_to_be32(i)
860 memcpy(cur_index, associativity, sizeof(associativity));
861 cur_index += 4;
863 ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
864 (cur_index - int_buf) * sizeof(uint32_t));
865 g_free(int_buf);
867 return ret;
870 static int spapr_dt_cas_updates(SpaprMachineState *spapr, void *fdt,
871 SpaprOptionVector *ov5_updates)
873 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
874 int ret = 0, offset;
876 /* Generate ibm,dynamic-reconfiguration-memory node if required */
877 if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
878 g_assert(smc->dr_lmb_enabled);
879 ret = spapr_populate_drconf_memory(spapr, fdt);
880 if (ret) {
881 goto out;
885 offset = fdt_path_offset(fdt, "/chosen");
886 if (offset < 0) {
887 offset = fdt_add_subnode(fdt, 0, "chosen");
888 if (offset < 0) {
889 return offset;
892 ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
893 "ibm,architecture-vec-5");
895 out:
896 return ret;
899 static void spapr_dt_rtas(SpaprMachineState *spapr, void *fdt)
901 MachineState *ms = MACHINE(spapr);
902 int rtas;
903 GString *hypertas = g_string_sized_new(256);
904 GString *qemu_hypertas = g_string_sized_new(256);
905 uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
906 uint64_t max_device_addr = MACHINE(spapr)->device_memory->base +
907 memory_region_size(&MACHINE(spapr)->device_memory->mr);
908 uint32_t lrdr_capacity[] = {
909 cpu_to_be32(max_device_addr >> 32),
910 cpu_to_be32(max_device_addr & 0xffffffff),
911 0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
912 cpu_to_be32(ms->smp.max_cpus / ms->smp.threads),
914 uint32_t maxdomain = cpu_to_be32(spapr->gpu_numa_id > 1 ? 1 : 0);
915 uint32_t maxdomains[] = {
916 cpu_to_be32(4),
917 maxdomain,
918 maxdomain,
919 maxdomain,
920 cpu_to_be32(spapr->gpu_numa_id),
923 _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
925 /* hypertas */
926 add_str(hypertas, "hcall-pft");
927 add_str(hypertas, "hcall-term");
928 add_str(hypertas, "hcall-dabr");
929 add_str(hypertas, "hcall-interrupt");
930 add_str(hypertas, "hcall-tce");
931 add_str(hypertas, "hcall-vio");
932 add_str(hypertas, "hcall-splpar");
933 add_str(hypertas, "hcall-join");
934 add_str(hypertas, "hcall-bulk");
935 add_str(hypertas, "hcall-set-mode");
936 add_str(hypertas, "hcall-sprg0");
937 add_str(hypertas, "hcall-copy");
938 add_str(hypertas, "hcall-debug");
939 add_str(hypertas, "hcall-vphn");
940 add_str(qemu_hypertas, "hcall-memop1");
942 if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
943 add_str(hypertas, "hcall-multi-tce");
946 if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
947 add_str(hypertas, "hcall-hpt-resize");
950 _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
951 hypertas->str, hypertas->len));
952 g_string_free(hypertas, TRUE);
953 _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
954 qemu_hypertas->str, qemu_hypertas->len));
955 g_string_free(qemu_hypertas, TRUE);
957 _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
958 refpoints, sizeof(refpoints)));
960 _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains",
961 maxdomains, sizeof(maxdomains)));
963 _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
964 RTAS_ERROR_LOG_MAX));
965 _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
966 RTAS_EVENT_SCAN_RATE));
968 g_assert(msi_nonbroken);
969 _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
972 * According to PAPR, rtas ibm,os-term does not guarantee a return
973 * back to the guest cpu.
975 * While an additional ibm,extended-os-term property indicates
976 * that rtas call return will always occur. Set this property.
978 _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
980 _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
981 lrdr_capacity, sizeof(lrdr_capacity)));
983 spapr_dt_rtas_tokens(fdt, rtas);
987 * Prepare ibm,arch-vec-5-platform-support, which indicates the MMU
988 * and the XIVE features that the guest may request and thus the valid
989 * values for bytes 23..26 of option vector 5:
991 static void spapr_dt_ov5_platform_support(SpaprMachineState *spapr, void *fdt,
992 int chosen)
994 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
996 char val[2 * 4] = {
997 23, 0x00, /* XICS / XIVE mode */
998 24, 0x00, /* Hash/Radix, filled in below. */
999 25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
1000 26, 0x40, /* Radix options: GTSE == yes. */
1003 if (spapr->irq->xics && spapr->irq->xive) {
1004 val[1] = SPAPR_OV5_XIVE_BOTH;
1005 } else if (spapr->irq->xive) {
1006 val[1] = SPAPR_OV5_XIVE_EXPLOIT;
1007 } else {
1008 assert(spapr->irq->xics);
1009 val[1] = SPAPR_OV5_XIVE_LEGACY;
1012 if (!ppc_check_compat(first_ppc_cpu, CPU_POWERPC_LOGICAL_3_00, 0,
1013 first_ppc_cpu->compat_pvr)) {
1015 * If we're in a pre POWER9 compat mode then the guest should
1016 * do hash and use the legacy interrupt mode
1018 val[1] = SPAPR_OV5_XIVE_LEGACY; /* XICS */
1019 val[3] = 0x00; /* Hash */
1020 } else if (kvm_enabled()) {
1021 if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
1022 val[3] = 0x80; /* OV5_MMU_BOTH */
1023 } else if (kvmppc_has_cap_mmu_radix()) {
1024 val[3] = 0x40; /* OV5_MMU_RADIX_300 */
1025 } else {
1026 val[3] = 0x00; /* Hash */
1028 } else {
1029 /* V3 MMU supports both hash and radix in tcg (with dynamic switching) */
1030 val[3] = 0xC0;
1032 _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
1033 val, sizeof(val)));
1036 static void spapr_dt_chosen(SpaprMachineState *spapr, void *fdt)
1038 MachineState *machine = MACHINE(spapr);
1039 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
1040 int chosen;
1041 const char *boot_device = machine->boot_order;
1042 char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
1043 size_t cb = 0;
1044 char *bootlist = get_boot_devices_list(&cb);
1046 _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
1048 if (machine->kernel_cmdline && machine->kernel_cmdline[0]) {
1049 _FDT(fdt_setprop_string(fdt, chosen, "bootargs",
1050 machine->kernel_cmdline));
1052 if (spapr->initrd_size) {
1053 _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
1054 spapr->initrd_base));
1055 _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
1056 spapr->initrd_base + spapr->initrd_size));
1059 if (spapr->kernel_size) {
1060 uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
1061 cpu_to_be64(spapr->kernel_size) };
1063 _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
1064 &kprop, sizeof(kprop)));
1065 if (spapr->kernel_le) {
1066 _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
1069 if (boot_menu) {
1070 _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
1072 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
1073 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
1074 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
1076 if (cb && bootlist) {
1077 int i;
1079 for (i = 0; i < cb; i++) {
1080 if (bootlist[i] == '\n') {
1081 bootlist[i] = ' ';
1084 _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
1087 if (boot_device && strlen(boot_device)) {
1088 _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
1091 if (!spapr->has_graphics && stdout_path) {
1093 * "linux,stdout-path" and "stdout" properties are deprecated by linux
1094 * kernel. New platforms should only use the "stdout-path" property. Set
1095 * the new property and continue using older property to remain
1096 * compatible with the existing firmware.
1098 _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
1099 _FDT(fdt_setprop_string(fdt, chosen, "stdout-path", stdout_path));
1102 /* We can deal with BAR reallocation just fine, advertise it to the guest */
1103 if (smc->linux_pci_probe) {
1104 _FDT(fdt_setprop_cell(fdt, chosen, "linux,pci-probe-only", 0));
1107 spapr_dt_ov5_platform_support(spapr, fdt, chosen);
1109 g_free(stdout_path);
1110 g_free(bootlist);
1113 static void spapr_dt_hypervisor(SpaprMachineState *spapr, void *fdt)
1115 /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
1116 * KVM to work under pHyp with some guest co-operation */
1117 int hypervisor;
1118 uint8_t hypercall[16];
1120 _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
1121 /* indicate KVM hypercall interface */
1122 _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
1123 if (kvmppc_has_cap_fixup_hcalls()) {
1125 * Older KVM versions with older guest kernels were broken
1126 * with the magic page, don't allow the guest to map it.
1128 if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
1129 sizeof(hypercall))) {
1130 _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
1131 hypercall, sizeof(hypercall)));
1136 void *spapr_build_fdt(SpaprMachineState *spapr, bool reset, size_t space)
1138 MachineState *machine = MACHINE(spapr);
1139 MachineClass *mc = MACHINE_GET_CLASS(machine);
1140 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
1141 int ret;
1142 void *fdt;
1143 SpaprPhbState *phb;
1144 char *buf;
1146 fdt = g_malloc0(space);
1147 _FDT((fdt_create_empty_tree(fdt, space)));
1149 /* Root node */
1150 _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
1151 _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
1152 _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
1154 /* Guest UUID & Name*/
1155 buf = qemu_uuid_unparse_strdup(&qemu_uuid);
1156 _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1157 if (qemu_uuid_set) {
1158 _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1160 g_free(buf);
1162 if (qemu_get_vm_name()) {
1163 _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1164 qemu_get_vm_name()));
1167 /* Host Model & Serial Number */
1168 if (spapr->host_model) {
1169 _FDT(fdt_setprop_string(fdt, 0, "host-model", spapr->host_model));
1170 } else if (smc->broken_host_serial_model && kvmppc_get_host_model(&buf)) {
1171 _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1172 g_free(buf);
1175 if (spapr->host_serial) {
1176 _FDT(fdt_setprop_string(fdt, 0, "host-serial", spapr->host_serial));
1177 } else if (smc->broken_host_serial_model && kvmppc_get_host_serial(&buf)) {
1178 _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1179 g_free(buf);
1182 _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1183 _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
1185 /* /interrupt controller */
1186 spapr_irq_dt(spapr, spapr_max_server_number(spapr), fdt, PHANDLE_INTC);
1188 ret = spapr_populate_memory(spapr, fdt);
1189 if (ret < 0) {
1190 error_report("couldn't setup memory nodes in fdt");
1191 exit(1);
1194 /* /vdevice */
1195 spapr_dt_vdevice(spapr->vio_bus, fdt);
1197 if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
1198 ret = spapr_rng_populate_dt(fdt);
1199 if (ret < 0) {
1200 error_report("could not set up rng device in the fdt");
1201 exit(1);
1205 QLIST_FOREACH(phb, &spapr->phbs, list) {
1206 ret = spapr_dt_phb(spapr, phb, PHANDLE_INTC, fdt, NULL);
1207 if (ret < 0) {
1208 error_report("couldn't setup PCI devices in fdt");
1209 exit(1);
1213 /* cpus */
1214 spapr_populate_cpus_dt_node(fdt, spapr);
1216 if (smc->dr_lmb_enabled) {
1217 _FDT(spapr_dt_drc(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1220 if (mc->has_hotpluggable_cpus) {
1221 int offset = fdt_path_offset(fdt, "/cpus");
1222 ret = spapr_dt_drc(fdt, offset, NULL, SPAPR_DR_CONNECTOR_TYPE_CPU);
1223 if (ret < 0) {
1224 error_report("Couldn't set up CPU DR device tree properties");
1225 exit(1);
1229 /* /event-sources */
1230 spapr_dt_events(spapr, fdt);
1232 /* /rtas */
1233 spapr_dt_rtas(spapr, fdt);
1235 /* /chosen */
1236 if (reset) {
1237 spapr_dt_chosen(spapr, fdt);
1240 /* /hypervisor */
1241 if (kvm_enabled()) {
1242 spapr_dt_hypervisor(spapr, fdt);
1245 /* Build memory reserve map */
1246 if (reset) {
1247 if (spapr->kernel_size) {
1248 _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1250 if (spapr->initrd_size) {
1251 _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base,
1252 spapr->initrd_size)));
1256 /* ibm,client-architecture-support updates */
1257 ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
1258 if (ret < 0) {
1259 error_report("couldn't setup CAS properties fdt");
1260 exit(1);
1263 if (smc->dr_phb_enabled) {
1264 ret = spapr_dt_drc(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_PHB);
1265 if (ret < 0) {
1266 error_report("Couldn't set up PHB DR device tree properties");
1267 exit(1);
1271 return fdt;
1274 static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
1276 return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
1279 static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
1280 PowerPCCPU *cpu)
1282 CPUPPCState *env = &cpu->env;
1284 /* The TCG path should also be holding the BQL at this point */
1285 g_assert(qemu_mutex_iothread_locked());
1287 if (msr_pr) {
1288 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
1289 env->gpr[3] = H_PRIVILEGE;
1290 } else {
1291 env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
1295 struct LPCRSyncState {
1296 target_ulong value;
1297 target_ulong mask;
1300 static void do_lpcr_sync(CPUState *cs, run_on_cpu_data arg)
1302 struct LPCRSyncState *s = arg.host_ptr;
1303 PowerPCCPU *cpu = POWERPC_CPU(cs);
1304 CPUPPCState *env = &cpu->env;
1305 target_ulong lpcr;
1307 cpu_synchronize_state(cs);
1308 lpcr = env->spr[SPR_LPCR];
1309 lpcr &= ~s->mask;
1310 lpcr |= s->value;
1311 ppc_store_lpcr(cpu, lpcr);
1314 void spapr_set_all_lpcrs(target_ulong value, target_ulong mask)
1316 CPUState *cs;
1317 struct LPCRSyncState s = {
1318 .value = value,
1319 .mask = mask
1321 CPU_FOREACH(cs) {
1322 run_on_cpu(cs, do_lpcr_sync, RUN_ON_CPU_HOST_PTR(&s));
1326 static void spapr_get_pate(PPCVirtualHypervisor *vhyp, ppc_v3_pate_t *entry)
1328 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1330 /* Copy PATE1:GR into PATE0:HR */
1331 entry->dw0 = spapr->patb_entry & PATE0_HR;
1332 entry->dw1 = spapr->patb_entry;
1335 #define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1336 #define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1337 #define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1338 #define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1339 #define DIRTY_HPTE(_hpte) ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1342 * Get the fd to access the kernel htab, re-opening it if necessary
1344 static int get_htab_fd(SpaprMachineState *spapr)
1346 Error *local_err = NULL;
1348 if (spapr->htab_fd >= 0) {
1349 return spapr->htab_fd;
1352 spapr->htab_fd = kvmppc_get_htab_fd(false, 0, &local_err);
1353 if (spapr->htab_fd < 0) {
1354 error_report_err(local_err);
1357 return spapr->htab_fd;
1360 void close_htab_fd(SpaprMachineState *spapr)
1362 if (spapr->htab_fd >= 0) {
1363 close(spapr->htab_fd);
1365 spapr->htab_fd = -1;
1368 static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1370 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1372 return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1375 static target_ulong spapr_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
1377 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1379 assert(kvm_enabled());
1381 if (!spapr->htab) {
1382 return 0;
1385 return (target_ulong)(uintptr_t)spapr->htab | (spapr->htab_shift - 18);
1388 static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1389 hwaddr ptex, int n)
1391 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1392 hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1394 if (!spapr->htab) {
1396 * HTAB is controlled by KVM. Fetch into temporary buffer
1398 ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1399 kvmppc_read_hptes(hptes, ptex, n);
1400 return hptes;
1404 * HTAB is controlled by QEMU. Just point to the internally
1405 * accessible PTEG.
1407 return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1410 static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1411 const ppc_hash_pte64_t *hptes,
1412 hwaddr ptex, int n)
1414 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1416 if (!spapr->htab) {
1417 g_free((void *)hptes);
1420 /* Nothing to do for qemu managed HPT */
1423 void spapr_store_hpte(PowerPCCPU *cpu, hwaddr ptex,
1424 uint64_t pte0, uint64_t pte1)
1426 SpaprMachineState *spapr = SPAPR_MACHINE(cpu->vhyp);
1427 hwaddr offset = ptex * HASH_PTE_SIZE_64;
1429 if (!spapr->htab) {
1430 kvmppc_write_hpte(ptex, pte0, pte1);
1431 } else {
1432 if (pte0 & HPTE64_V_VALID) {
1433 stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1435 * When setting valid, we write PTE1 first. This ensures
1436 * proper synchronization with the reading code in
1437 * ppc_hash64_pteg_search()
1439 smp_wmb();
1440 stq_p(spapr->htab + offset, pte0);
1441 } else {
1442 stq_p(spapr->htab + offset, pte0);
1444 * When clearing it we set PTE0 first. This ensures proper
1445 * synchronization with the reading code in
1446 * ppc_hash64_pteg_search()
1448 smp_wmb();
1449 stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1454 static void spapr_hpte_set_c(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1455 uint64_t pte1)
1457 hwaddr offset = ptex * HASH_PTE_SIZE_64 + 15;
1458 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1460 if (!spapr->htab) {
1461 /* There should always be a hash table when this is called */
1462 error_report("spapr_hpte_set_c called with no hash table !");
1463 return;
1466 /* The HW performs a non-atomic byte update */
1467 stb_p(spapr->htab + offset, (pte1 & 0xff) | 0x80);
1470 static void spapr_hpte_set_r(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1471 uint64_t pte1)
1473 hwaddr offset = ptex * HASH_PTE_SIZE_64 + 14;
1474 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1476 if (!spapr->htab) {
1477 /* There should always be a hash table when this is called */
1478 error_report("spapr_hpte_set_r called with no hash table !");
1479 return;
1482 /* The HW performs a non-atomic byte update */
1483 stb_p(spapr->htab + offset, ((pte1 >> 8) & 0xff) | 0x01);
1486 int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
1488 int shift;
1490 /* We aim for a hash table of size 1/128 the size of RAM (rounded
1491 * up). The PAPR recommendation is actually 1/64 of RAM size, but
1492 * that's much more than is needed for Linux guests */
1493 shift = ctz64(pow2ceil(ramsize)) - 7;
1494 shift = MAX(shift, 18); /* Minimum architected size */
1495 shift = MIN(shift, 46); /* Maximum architected size */
1496 return shift;
1499 void spapr_free_hpt(SpaprMachineState *spapr)
1501 g_free(spapr->htab);
1502 spapr->htab = NULL;
1503 spapr->htab_shift = 0;
1504 close_htab_fd(spapr);
1507 void spapr_reallocate_hpt(SpaprMachineState *spapr, int shift,
1508 Error **errp)
1510 long rc;
1512 /* Clean up any HPT info from a previous boot */
1513 spapr_free_hpt(spapr);
1515 rc = kvmppc_reset_htab(shift);
1516 if (rc < 0) {
1517 /* kernel-side HPT needed, but couldn't allocate one */
1518 error_setg_errno(errp, errno,
1519 "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1520 shift);
1521 /* This is almost certainly fatal, but if the caller really
1522 * wants to carry on with shift == 0, it's welcome to try */
1523 } else if (rc > 0) {
1524 /* kernel-side HPT allocated */
1525 if (rc != shift) {
1526 error_setg(errp,
1527 "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1528 shift, rc);
1531 spapr->htab_shift = shift;
1532 spapr->htab = NULL;
1533 } else {
1534 /* kernel-side HPT not needed, allocate in userspace instead */
1535 size_t size = 1ULL << shift;
1536 int i;
1538 spapr->htab = qemu_memalign(size, size);
1539 if (!spapr->htab) {
1540 error_setg_errno(errp, errno,
1541 "Could not allocate HPT of order %d", shift);
1542 return;
1545 memset(spapr->htab, 0, size);
1546 spapr->htab_shift = shift;
1548 for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1549 DIRTY_HPTE(HPTE(spapr->htab, i));
1552 /* We're setting up a hash table, so that means we're not radix */
1553 spapr->patb_entry = 0;
1554 spapr_set_all_lpcrs(0, LPCR_HR | LPCR_UPRT);
1557 void spapr_setup_hpt_and_vrma(SpaprMachineState *spapr)
1559 int hpt_shift;
1561 if ((spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED)
1562 || (spapr->cas_reboot
1563 && !spapr_ovec_test(spapr->ov5_cas, OV5_HPT_RESIZE))) {
1564 hpt_shift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size);
1565 } else {
1566 uint64_t current_ram_size;
1568 current_ram_size = MACHINE(spapr)->ram_size + get_plugged_memory_size();
1569 hpt_shift = spapr_hpt_shift_for_ramsize(current_ram_size);
1571 spapr_reallocate_hpt(spapr, hpt_shift, &error_fatal);
1573 if (spapr->vrma_adjust) {
1574 spapr->rma_size = kvmppc_rma_size(spapr_node0_size(MACHINE(spapr)),
1575 spapr->htab_shift);
1579 static int spapr_reset_drcs(Object *child, void *opaque)
1581 SpaprDrc *drc =
1582 (SpaprDrc *) object_dynamic_cast(child,
1583 TYPE_SPAPR_DR_CONNECTOR);
1585 if (drc) {
1586 spapr_drc_reset(drc);
1589 return 0;
1592 static void spapr_machine_reset(MachineState *machine)
1594 SpaprMachineState *spapr = SPAPR_MACHINE(machine);
1595 PowerPCCPU *first_ppc_cpu;
1596 hwaddr fdt_addr;
1597 void *fdt;
1598 int rc;
1600 spapr_caps_apply(spapr);
1602 first_ppc_cpu = POWERPC_CPU(first_cpu);
1603 if (kvm_enabled() && kvmppc_has_cap_mmu_radix() &&
1604 ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00, 0,
1605 spapr->max_compat_pvr)) {
1607 * If using KVM with radix mode available, VCPUs can be started
1608 * without a HPT because KVM will start them in radix mode.
1609 * Set the GR bit in PATE so that we know there is no HPT.
1611 spapr->patb_entry = PATE1_GR;
1612 spapr_set_all_lpcrs(LPCR_HR | LPCR_UPRT, LPCR_HR | LPCR_UPRT);
1613 } else {
1614 spapr_setup_hpt_and_vrma(spapr);
1617 qemu_devices_reset();
1620 * If this reset wasn't generated by CAS, we should reset our
1621 * negotiated options and start from scratch
1623 if (!spapr->cas_reboot) {
1624 spapr_ovec_cleanup(spapr->ov5_cas);
1625 spapr->ov5_cas = spapr_ovec_new();
1627 ppc_set_compat_all(spapr->max_compat_pvr, &error_fatal);
1631 * This is fixing some of the default configuration of the XIVE
1632 * devices. To be called after the reset of the machine devices.
1634 spapr_irq_reset(spapr, &error_fatal);
1637 * There is no CAS under qtest. Simulate one to please the code that
1638 * depends on spapr->ov5_cas. This is especially needed to test device
1639 * unplug, so we do that before resetting the DRCs.
1641 if (qtest_enabled()) {
1642 spapr_ovec_cleanup(spapr->ov5_cas);
1643 spapr->ov5_cas = spapr_ovec_clone(spapr->ov5);
1646 /* DRC reset may cause a device to be unplugged. This will cause troubles
1647 * if this device is used by another device (eg, a running vhost backend
1648 * will crash QEMU if the DIMM holding the vring goes away). To avoid such
1649 * situations, we reset DRCs after all devices have been reset.
1651 object_child_foreach_recursive(object_get_root(), spapr_reset_drcs, NULL);
1653 spapr_clear_pending_events(spapr);
1656 * We place the device tree and RTAS just below either the top of the RMA,
1657 * or just below 2GB, whichever is lower, so that it can be
1658 * processed with 32-bit real mode code if necessary
1660 fdt_addr = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FDT_MAX_SIZE;
1662 fdt = spapr_build_fdt(spapr, true, FDT_MAX_SIZE);
1664 rc = fdt_pack(fdt);
1666 /* Should only fail if we've built a corrupted tree */
1667 assert(rc == 0);
1669 /* Load the fdt */
1670 qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1671 cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1672 g_free(spapr->fdt_blob);
1673 spapr->fdt_size = fdt_totalsize(fdt);
1674 spapr->fdt_initial_size = spapr->fdt_size;
1675 spapr->fdt_blob = fdt;
1677 /* Set up the entry state */
1678 spapr_cpu_set_entry_state(first_ppc_cpu, SPAPR_ENTRY_POINT, fdt_addr);
1679 first_ppc_cpu->env.gpr[5] = 0;
1681 spapr->cas_reboot = false;
1684 static void spapr_create_nvram(SpaprMachineState *spapr)
1686 DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
1687 DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
1689 if (dinfo) {
1690 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
1691 &error_fatal);
1694 qdev_init_nofail(dev);
1696 spapr->nvram = (struct SpaprNvram *)dev;
1699 static void spapr_rtc_create(SpaprMachineState *spapr)
1701 object_initialize_child(OBJECT(spapr), "rtc",
1702 &spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC,
1703 &error_fatal, NULL);
1704 object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1705 &error_fatal);
1706 object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1707 "date", &error_fatal);
1710 /* Returns whether we want to use VGA or not */
1711 static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
1713 switch (vga_interface_type) {
1714 case VGA_NONE:
1715 return false;
1716 case VGA_DEVICE:
1717 return true;
1718 case VGA_STD:
1719 case VGA_VIRTIO:
1720 case VGA_CIRRUS:
1721 return pci_vga_init(pci_bus) != NULL;
1722 default:
1723 error_setg(errp,
1724 "Unsupported VGA mode, only -vga std or -vga virtio is supported");
1725 return false;
1729 static int spapr_pre_load(void *opaque)
1731 int rc;
1733 rc = spapr_caps_pre_load(opaque);
1734 if (rc) {
1735 return rc;
1738 return 0;
1741 static int spapr_post_load(void *opaque, int version_id)
1743 SpaprMachineState *spapr = (SpaprMachineState *)opaque;
1744 int err = 0;
1746 err = spapr_caps_post_migration(spapr);
1747 if (err) {
1748 return err;
1752 * In earlier versions, there was no separate qdev for the PAPR
1753 * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1754 * So when migrating from those versions, poke the incoming offset
1755 * value into the RTC device
1757 if (version_id < 3) {
1758 err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
1759 if (err) {
1760 return err;
1764 if (kvm_enabled() && spapr->patb_entry) {
1765 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
1766 bool radix = !!(spapr->patb_entry & PATE1_GR);
1767 bool gtse = !!(cpu->env.spr[SPR_LPCR] & LPCR_GTSE);
1770 * Update LPCR:HR and UPRT as they may not be set properly in
1771 * the stream
1773 spapr_set_all_lpcrs(radix ? (LPCR_HR | LPCR_UPRT) : 0,
1774 LPCR_HR | LPCR_UPRT);
1776 err = kvmppc_configure_v3_mmu(cpu, radix, gtse, spapr->patb_entry);
1777 if (err) {
1778 error_report("Process table config unsupported by the host");
1779 return -EINVAL;
1783 err = spapr_irq_post_load(spapr, version_id);
1784 if (err) {
1785 return err;
1788 return err;
1791 static int spapr_pre_save(void *opaque)
1793 int rc;
1795 rc = spapr_caps_pre_save(opaque);
1796 if (rc) {
1797 return rc;
1800 return 0;
1803 static bool version_before_3(void *opaque, int version_id)
1805 return version_id < 3;
1808 static bool spapr_pending_events_needed(void *opaque)
1810 SpaprMachineState *spapr = (SpaprMachineState *)opaque;
1811 return !QTAILQ_EMPTY(&spapr->pending_events);
1814 static const VMStateDescription vmstate_spapr_event_entry = {
1815 .name = "spapr_event_log_entry",
1816 .version_id = 1,
1817 .minimum_version_id = 1,
1818 .fields = (VMStateField[]) {
1819 VMSTATE_UINT32(summary, SpaprEventLogEntry),
1820 VMSTATE_UINT32(extended_length, SpaprEventLogEntry),
1821 VMSTATE_VBUFFER_ALLOC_UINT32(extended_log, SpaprEventLogEntry, 0,
1822 NULL, extended_length),
1823 VMSTATE_END_OF_LIST()
1827 static const VMStateDescription vmstate_spapr_pending_events = {
1828 .name = "spapr_pending_events",
1829 .version_id = 1,
1830 .minimum_version_id = 1,
1831 .needed = spapr_pending_events_needed,
1832 .fields = (VMStateField[]) {
1833 VMSTATE_QTAILQ_V(pending_events, SpaprMachineState, 1,
1834 vmstate_spapr_event_entry, SpaprEventLogEntry, next),
1835 VMSTATE_END_OF_LIST()
1839 static bool spapr_ov5_cas_needed(void *opaque)
1841 SpaprMachineState *spapr = opaque;
1842 SpaprOptionVector *ov5_mask = spapr_ovec_new();
1843 SpaprOptionVector *ov5_legacy = spapr_ovec_new();
1844 SpaprOptionVector *ov5_removed = spapr_ovec_new();
1845 bool cas_needed;
1847 /* Prior to the introduction of SpaprOptionVector, we had two option
1848 * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
1849 * Both of these options encode machine topology into the device-tree
1850 * in such a way that the now-booted OS should still be able to interact
1851 * appropriately with QEMU regardless of what options were actually
1852 * negotiatied on the source side.
1854 * As such, we can avoid migrating the CAS-negotiated options if these
1855 * are the only options available on the current machine/platform.
1856 * Since these are the only options available for pseries-2.7 and
1857 * earlier, this allows us to maintain old->new/new->old migration
1858 * compatibility.
1860 * For QEMU 2.8+, there are additional CAS-negotiatable options available
1861 * via default pseries-2.8 machines and explicit command-line parameters.
1862 * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
1863 * of the actual CAS-negotiated values to continue working properly. For
1864 * example, availability of memory unplug depends on knowing whether
1865 * OV5_HP_EVT was negotiated via CAS.
1867 * Thus, for any cases where the set of available CAS-negotiatable
1868 * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
1869 * include the CAS-negotiated options in the migration stream, unless
1870 * if they affect boot time behaviour only.
1872 spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
1873 spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
1874 spapr_ovec_set(ov5_mask, OV5_DRMEM_V2);
1876 /* spapr_ovec_diff returns true if bits were removed. we avoid using
1877 * the mask itself since in the future it's possible "legacy" bits may be
1878 * removed via machine options, which could generate a false positive
1879 * that breaks migration.
1881 spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
1882 cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
1884 spapr_ovec_cleanup(ov5_mask);
1885 spapr_ovec_cleanup(ov5_legacy);
1886 spapr_ovec_cleanup(ov5_removed);
1888 return cas_needed;
1891 static const VMStateDescription vmstate_spapr_ov5_cas = {
1892 .name = "spapr_option_vector_ov5_cas",
1893 .version_id = 1,
1894 .minimum_version_id = 1,
1895 .needed = spapr_ov5_cas_needed,
1896 .fields = (VMStateField[]) {
1897 VMSTATE_STRUCT_POINTER_V(ov5_cas, SpaprMachineState, 1,
1898 vmstate_spapr_ovec, SpaprOptionVector),
1899 VMSTATE_END_OF_LIST()
1903 static bool spapr_patb_entry_needed(void *opaque)
1905 SpaprMachineState *spapr = opaque;
1907 return !!spapr->patb_entry;
1910 static const VMStateDescription vmstate_spapr_patb_entry = {
1911 .name = "spapr_patb_entry",
1912 .version_id = 1,
1913 .minimum_version_id = 1,
1914 .needed = spapr_patb_entry_needed,
1915 .fields = (VMStateField[]) {
1916 VMSTATE_UINT64(patb_entry, SpaprMachineState),
1917 VMSTATE_END_OF_LIST()
1921 static bool spapr_irq_map_needed(void *opaque)
1923 SpaprMachineState *spapr = opaque;
1925 return spapr->irq_map && !bitmap_empty(spapr->irq_map, spapr->irq_map_nr);
1928 static const VMStateDescription vmstate_spapr_irq_map = {
1929 .name = "spapr_irq_map",
1930 .version_id = 1,
1931 .minimum_version_id = 1,
1932 .needed = spapr_irq_map_needed,
1933 .fields = (VMStateField[]) {
1934 VMSTATE_BITMAP(irq_map, SpaprMachineState, 0, irq_map_nr),
1935 VMSTATE_END_OF_LIST()
1939 static bool spapr_dtb_needed(void *opaque)
1941 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(opaque);
1943 return smc->update_dt_enabled;
1946 static int spapr_dtb_pre_load(void *opaque)
1948 SpaprMachineState *spapr = (SpaprMachineState *)opaque;
1950 g_free(spapr->fdt_blob);
1951 spapr->fdt_blob = NULL;
1952 spapr->fdt_size = 0;
1954 return 0;
1957 static const VMStateDescription vmstate_spapr_dtb = {
1958 .name = "spapr_dtb",
1959 .version_id = 1,
1960 .minimum_version_id = 1,
1961 .needed = spapr_dtb_needed,
1962 .pre_load = spapr_dtb_pre_load,
1963 .fields = (VMStateField[]) {
1964 VMSTATE_UINT32(fdt_initial_size, SpaprMachineState),
1965 VMSTATE_UINT32(fdt_size, SpaprMachineState),
1966 VMSTATE_VBUFFER_ALLOC_UINT32(fdt_blob, SpaprMachineState, 0, NULL,
1967 fdt_size),
1968 VMSTATE_END_OF_LIST()
1972 static const VMStateDescription vmstate_spapr = {
1973 .name = "spapr",
1974 .version_id = 3,
1975 .minimum_version_id = 1,
1976 .pre_load = spapr_pre_load,
1977 .post_load = spapr_post_load,
1978 .pre_save = spapr_pre_save,
1979 .fields = (VMStateField[]) {
1980 /* used to be @next_irq */
1981 VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
1983 /* RTC offset */
1984 VMSTATE_UINT64_TEST(rtc_offset, SpaprMachineState, version_before_3),
1986 VMSTATE_PPC_TIMEBASE_V(tb, SpaprMachineState, 2),
1987 VMSTATE_END_OF_LIST()
1989 .subsections = (const VMStateDescription*[]) {
1990 &vmstate_spapr_ov5_cas,
1991 &vmstate_spapr_patb_entry,
1992 &vmstate_spapr_pending_events,
1993 &vmstate_spapr_cap_htm,
1994 &vmstate_spapr_cap_vsx,
1995 &vmstate_spapr_cap_dfp,
1996 &vmstate_spapr_cap_cfpc,
1997 &vmstate_spapr_cap_sbbc,
1998 &vmstate_spapr_cap_ibs,
1999 &vmstate_spapr_cap_hpt_maxpagesize,
2000 &vmstate_spapr_irq_map,
2001 &vmstate_spapr_cap_nested_kvm_hv,
2002 &vmstate_spapr_dtb,
2003 &vmstate_spapr_cap_large_decr,
2004 &vmstate_spapr_cap_ccf_assist,
2005 NULL
2009 static int htab_save_setup(QEMUFile *f, void *opaque)
2011 SpaprMachineState *spapr = opaque;
2013 /* "Iteration" header */
2014 if (!spapr->htab_shift) {
2015 qemu_put_be32(f, -1);
2016 } else {
2017 qemu_put_be32(f, spapr->htab_shift);
2020 if (spapr->htab) {
2021 spapr->htab_save_index = 0;
2022 spapr->htab_first_pass = true;
2023 } else {
2024 if (spapr->htab_shift) {
2025 assert(kvm_enabled());
2030 return 0;
2033 static void htab_save_chunk(QEMUFile *f, SpaprMachineState *spapr,
2034 int chunkstart, int n_valid, int n_invalid)
2036 qemu_put_be32(f, chunkstart);
2037 qemu_put_be16(f, n_valid);
2038 qemu_put_be16(f, n_invalid);
2039 qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
2040 HASH_PTE_SIZE_64 * n_valid);
2043 static void htab_save_end_marker(QEMUFile *f)
2045 qemu_put_be32(f, 0);
2046 qemu_put_be16(f, 0);
2047 qemu_put_be16(f, 0);
2050 static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
2051 int64_t max_ns)
2053 bool has_timeout = max_ns != -1;
2054 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
2055 int index = spapr->htab_save_index;
2056 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2058 assert(spapr->htab_first_pass);
2060 do {
2061 int chunkstart;
2063 /* Consume invalid HPTEs */
2064 while ((index < htabslots)
2065 && !HPTE_VALID(HPTE(spapr->htab, index))) {
2066 CLEAN_HPTE(HPTE(spapr->htab, index));
2067 index++;
2070 /* Consume valid HPTEs */
2071 chunkstart = index;
2072 while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
2073 && HPTE_VALID(HPTE(spapr->htab, index))) {
2074 CLEAN_HPTE(HPTE(spapr->htab, index));
2075 index++;
2078 if (index > chunkstart) {
2079 int n_valid = index - chunkstart;
2081 htab_save_chunk(f, spapr, chunkstart, n_valid, 0);
2083 if (has_timeout &&
2084 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
2085 break;
2088 } while ((index < htabslots) && !qemu_file_rate_limit(f));
2090 if (index >= htabslots) {
2091 assert(index == htabslots);
2092 index = 0;
2093 spapr->htab_first_pass = false;
2095 spapr->htab_save_index = index;
2098 static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
2099 int64_t max_ns)
2101 bool final = max_ns < 0;
2102 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
2103 int examined = 0, sent = 0;
2104 int index = spapr->htab_save_index;
2105 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2107 assert(!spapr->htab_first_pass);
2109 do {
2110 int chunkstart, invalidstart;
2112 /* Consume non-dirty HPTEs */
2113 while ((index < htabslots)
2114 && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
2115 index++;
2116 examined++;
2119 chunkstart = index;
2120 /* Consume valid dirty HPTEs */
2121 while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
2122 && HPTE_DIRTY(HPTE(spapr->htab, index))
2123 && HPTE_VALID(HPTE(spapr->htab, index))) {
2124 CLEAN_HPTE(HPTE(spapr->htab, index));
2125 index++;
2126 examined++;
2129 invalidstart = index;
2130 /* Consume invalid dirty HPTEs */
2131 while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
2132 && HPTE_DIRTY(HPTE(spapr->htab, index))
2133 && !HPTE_VALID(HPTE(spapr->htab, index))) {
2134 CLEAN_HPTE(HPTE(spapr->htab, index));
2135 index++;
2136 examined++;
2139 if (index > chunkstart) {
2140 int n_valid = invalidstart - chunkstart;
2141 int n_invalid = index - invalidstart;
2143 htab_save_chunk(f, spapr, chunkstart, n_valid, n_invalid);
2144 sent += index - chunkstart;
2146 if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
2147 break;
2151 if (examined >= htabslots) {
2152 break;
2155 if (index >= htabslots) {
2156 assert(index == htabslots);
2157 index = 0;
2159 } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
2161 if (index >= htabslots) {
2162 assert(index == htabslots);
2163 index = 0;
2166 spapr->htab_save_index = index;
2168 return (examined >= htabslots) && (sent == 0) ? 1 : 0;
2171 #define MAX_ITERATION_NS 5000000 /* 5 ms */
2172 #define MAX_KVM_BUF_SIZE 2048
2174 static int htab_save_iterate(QEMUFile *f, void *opaque)
2176 SpaprMachineState *spapr = opaque;
2177 int fd;
2178 int rc = 0;
2180 /* Iteration header */
2181 if (!spapr->htab_shift) {
2182 qemu_put_be32(f, -1);
2183 return 1;
2184 } else {
2185 qemu_put_be32(f, 0);
2188 if (!spapr->htab) {
2189 assert(kvm_enabled());
2191 fd = get_htab_fd(spapr);
2192 if (fd < 0) {
2193 return fd;
2196 rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
2197 if (rc < 0) {
2198 return rc;
2200 } else if (spapr->htab_first_pass) {
2201 htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
2202 } else {
2203 rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
2206 htab_save_end_marker(f);
2208 return rc;
2211 static int htab_save_complete(QEMUFile *f, void *opaque)
2213 SpaprMachineState *spapr = opaque;
2214 int fd;
2216 /* Iteration header */
2217 if (!spapr->htab_shift) {
2218 qemu_put_be32(f, -1);
2219 return 0;
2220 } else {
2221 qemu_put_be32(f, 0);
2224 if (!spapr->htab) {
2225 int rc;
2227 assert(kvm_enabled());
2229 fd = get_htab_fd(spapr);
2230 if (fd < 0) {
2231 return fd;
2234 rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
2235 if (rc < 0) {
2236 return rc;
2238 } else {
2239 if (spapr->htab_first_pass) {
2240 htab_save_first_pass(f, spapr, -1);
2242 htab_save_later_pass(f, spapr, -1);
2245 /* End marker */
2246 htab_save_end_marker(f);
2248 return 0;
2251 static int htab_load(QEMUFile *f, void *opaque, int version_id)
2253 SpaprMachineState *spapr = opaque;
2254 uint32_t section_hdr;
2255 int fd = -1;
2256 Error *local_err = NULL;
2258 if (version_id < 1 || version_id > 1) {
2259 error_report("htab_load() bad version");
2260 return -EINVAL;
2263 section_hdr = qemu_get_be32(f);
2265 if (section_hdr == -1) {
2266 spapr_free_hpt(spapr);
2267 return 0;
2270 if (section_hdr) {
2271 /* First section gives the htab size */
2272 spapr_reallocate_hpt(spapr, section_hdr, &local_err);
2273 if (local_err) {
2274 error_report_err(local_err);
2275 return -EINVAL;
2277 return 0;
2280 if (!spapr->htab) {
2281 assert(kvm_enabled());
2283 fd = kvmppc_get_htab_fd(true, 0, &local_err);
2284 if (fd < 0) {
2285 error_report_err(local_err);
2286 return fd;
2290 while (true) {
2291 uint32_t index;
2292 uint16_t n_valid, n_invalid;
2294 index = qemu_get_be32(f);
2295 n_valid = qemu_get_be16(f);
2296 n_invalid = qemu_get_be16(f);
2298 if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
2299 /* End of Stream */
2300 break;
2303 if ((index + n_valid + n_invalid) >
2304 (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
2305 /* Bad index in stream */
2306 error_report(
2307 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
2308 index, n_valid, n_invalid, spapr->htab_shift);
2309 return -EINVAL;
2312 if (spapr->htab) {
2313 if (n_valid) {
2314 qemu_get_buffer(f, HPTE(spapr->htab, index),
2315 HASH_PTE_SIZE_64 * n_valid);
2317 if (n_invalid) {
2318 memset(HPTE(spapr->htab, index + n_valid), 0,
2319 HASH_PTE_SIZE_64 * n_invalid);
2321 } else {
2322 int rc;
2324 assert(fd >= 0);
2326 rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
2327 if (rc < 0) {
2328 return rc;
2333 if (!spapr->htab) {
2334 assert(fd >= 0);
2335 close(fd);
2338 return 0;
2341 static void htab_save_cleanup(void *opaque)
2343 SpaprMachineState *spapr = opaque;
2345 close_htab_fd(spapr);
2348 static SaveVMHandlers savevm_htab_handlers = {
2349 .save_setup = htab_save_setup,
2350 .save_live_iterate = htab_save_iterate,
2351 .save_live_complete_precopy = htab_save_complete,
2352 .save_cleanup = htab_save_cleanup,
2353 .load_state = htab_load,
2356 static void spapr_boot_set(void *opaque, const char *boot_device,
2357 Error **errp)
2359 MachineState *machine = MACHINE(opaque);
2360 machine->boot_order = g_strdup(boot_device);
2363 static void spapr_create_lmb_dr_connectors(SpaprMachineState *spapr)
2365 MachineState *machine = MACHINE(spapr);
2366 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
2367 uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
2368 int i;
2370 for (i = 0; i < nr_lmbs; i++) {
2371 uint64_t addr;
2373 addr = i * lmb_size + machine->device_memory->base;
2374 spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_LMB,
2375 addr / lmb_size);
2380 * If RAM size, maxmem size and individual node mem sizes aren't aligned
2381 * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
2382 * since we can't support such unaligned sizes with DRCONF_MEMORY.
2384 static void spapr_validate_node_memory(MachineState *machine, Error **errp)
2386 int i;
2388 if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
2389 error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
2390 " is not aligned to %" PRIu64 " MiB",
2391 machine->ram_size,
2392 SPAPR_MEMORY_BLOCK_SIZE / MiB);
2393 return;
2396 if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
2397 error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
2398 " is not aligned to %" PRIu64 " MiB",
2399 machine->ram_size,
2400 SPAPR_MEMORY_BLOCK_SIZE / MiB);
2401 return;
2404 for (i = 0; i < machine->numa_state->num_nodes; i++) {
2405 if (machine->numa_state->nodes[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
2406 error_setg(errp,
2407 "Node %d memory size 0x%" PRIx64
2408 " is not aligned to %" PRIu64 " MiB",
2409 i, machine->numa_state->nodes[i].node_mem,
2410 SPAPR_MEMORY_BLOCK_SIZE / MiB);
2411 return;
2416 /* find cpu slot in machine->possible_cpus by core_id */
2417 static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
2419 int index = id / ms->smp.threads;
2421 if (index >= ms->possible_cpus->len) {
2422 return NULL;
2424 if (idx) {
2425 *idx = index;
2427 return &ms->possible_cpus->cpus[index];
2430 static void spapr_set_vsmt_mode(SpaprMachineState *spapr, Error **errp)
2432 MachineState *ms = MACHINE(spapr);
2433 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
2434 Error *local_err = NULL;
2435 bool vsmt_user = !!spapr->vsmt;
2436 int kvm_smt = kvmppc_smt_threads();
2437 int ret;
2438 unsigned int smp_threads = ms->smp.threads;
2440 if (!kvm_enabled() && (smp_threads > 1)) {
2441 error_setg(&local_err, "TCG cannot support more than 1 thread/core "
2442 "on a pseries machine");
2443 goto out;
2445 if (!is_power_of_2(smp_threads)) {
2446 error_setg(&local_err, "Cannot support %d threads/core on a pseries "
2447 "machine because it must be a power of 2", smp_threads);
2448 goto out;
2451 /* Detemine the VSMT mode to use: */
2452 if (vsmt_user) {
2453 if (spapr->vsmt < smp_threads) {
2454 error_setg(&local_err, "Cannot support VSMT mode %d"
2455 " because it must be >= threads/core (%d)",
2456 spapr->vsmt, smp_threads);
2457 goto out;
2459 /* In this case, spapr->vsmt has been set by the command line */
2460 } else if (!smc->smp_threads_vsmt) {
2462 * Default VSMT value is tricky, because we need it to be as
2463 * consistent as possible (for migration), but this requires
2464 * changing it for at least some existing cases. We pick 8 as
2465 * the value that we'd get with KVM on POWER8, the
2466 * overwhelmingly common case in production systems.
2468 spapr->vsmt = MAX(8, smp_threads);
2469 } else {
2470 spapr->vsmt = smp_threads;
2473 /* KVM: If necessary, set the SMT mode: */
2474 if (kvm_enabled() && (spapr->vsmt != kvm_smt)) {
2475 ret = kvmppc_set_smt_threads(spapr->vsmt);
2476 if (ret) {
2477 /* Looks like KVM isn't able to change VSMT mode */
2478 error_setg(&local_err,
2479 "Failed to set KVM's VSMT mode to %d (errno %d)",
2480 spapr->vsmt, ret);
2481 /* We can live with that if the default one is big enough
2482 * for the number of threads, and a submultiple of the one
2483 * we want. In this case we'll waste some vcpu ids, but
2484 * behaviour will be correct */
2485 if ((kvm_smt >= smp_threads) && ((spapr->vsmt % kvm_smt) == 0)) {
2486 warn_report_err(local_err);
2487 local_err = NULL;
2488 goto out;
2489 } else {
2490 if (!vsmt_user) {
2491 error_append_hint(&local_err,
2492 "On PPC, a VM with %d threads/core"
2493 " on a host with %d threads/core"
2494 " requires the use of VSMT mode %d.\n",
2495 smp_threads, kvm_smt, spapr->vsmt);
2497 kvmppc_error_append_smt_possible_hint(&local_err);
2498 goto out;
2502 /* else TCG: nothing to do currently */
2503 out:
2504 error_propagate(errp, local_err);
2507 static void spapr_init_cpus(SpaprMachineState *spapr)
2509 MachineState *machine = MACHINE(spapr);
2510 MachineClass *mc = MACHINE_GET_CLASS(machine);
2511 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
2512 const char *type = spapr_get_cpu_core_type(machine->cpu_type);
2513 const CPUArchIdList *possible_cpus;
2514 unsigned int smp_cpus = machine->smp.cpus;
2515 unsigned int smp_threads = machine->smp.threads;
2516 unsigned int max_cpus = machine->smp.max_cpus;
2517 int boot_cores_nr = smp_cpus / smp_threads;
2518 int i;
2520 possible_cpus = mc->possible_cpu_arch_ids(machine);
2521 if (mc->has_hotpluggable_cpus) {
2522 if (smp_cpus % smp_threads) {
2523 error_report("smp_cpus (%u) must be multiple of threads (%u)",
2524 smp_cpus, smp_threads);
2525 exit(1);
2527 if (max_cpus % smp_threads) {
2528 error_report("max_cpus (%u) must be multiple of threads (%u)",
2529 max_cpus, smp_threads);
2530 exit(1);
2532 } else {
2533 if (max_cpus != smp_cpus) {
2534 error_report("This machine version does not support CPU hotplug");
2535 exit(1);
2537 boot_cores_nr = possible_cpus->len;
2540 if (smc->pre_2_10_has_unused_icps) {
2541 int i;
2543 for (i = 0; i < spapr_max_server_number(spapr); i++) {
2544 /* Dummy entries get deregistered when real ICPState objects
2545 * are registered during CPU core hotplug.
2547 pre_2_10_vmstate_register_dummy_icp(i);
2551 for (i = 0; i < possible_cpus->len; i++) {
2552 int core_id = i * smp_threads;
2554 if (mc->has_hotpluggable_cpus) {
2555 spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU,
2556 spapr_vcpu_id(spapr, core_id));
2559 if (i < boot_cores_nr) {
2560 Object *core = object_new(type);
2561 int nr_threads = smp_threads;
2563 /* Handle the partially filled core for older machine types */
2564 if ((i + 1) * smp_threads >= smp_cpus) {
2565 nr_threads = smp_cpus - i * smp_threads;
2568 object_property_set_int(core, nr_threads, "nr-threads",
2569 &error_fatal);
2570 object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
2571 &error_fatal);
2572 object_property_set_bool(core, true, "realized", &error_fatal);
2574 object_unref(core);
2579 static PCIHostState *spapr_create_default_phb(void)
2581 DeviceState *dev;
2583 dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE);
2584 qdev_prop_set_uint32(dev, "index", 0);
2585 qdev_init_nofail(dev);
2587 return PCI_HOST_BRIDGE(dev);
2590 /* pSeries LPAR / sPAPR hardware init */
2591 static void spapr_machine_init(MachineState *machine)
2593 SpaprMachineState *spapr = SPAPR_MACHINE(machine);
2594 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
2595 const char *kernel_filename = machine->kernel_filename;
2596 const char *initrd_filename = machine->initrd_filename;
2597 PCIHostState *phb;
2598 int i;
2599 MemoryRegion *sysmem = get_system_memory();
2600 MemoryRegion *ram = g_new(MemoryRegion, 1);
2601 hwaddr node0_size = spapr_node0_size(machine);
2602 long load_limit, fw_size;
2603 char *filename;
2604 Error *resize_hpt_err = NULL;
2606 msi_nonbroken = true;
2608 QLIST_INIT(&spapr->phbs);
2609 QTAILQ_INIT(&spapr->pending_dimm_unplugs);
2611 /* Determine capabilities to run with */
2612 spapr_caps_init(spapr);
2614 kvmppc_check_papr_resize_hpt(&resize_hpt_err);
2615 if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DEFAULT) {
2617 * If the user explicitly requested a mode we should either
2618 * supply it, or fail completely (which we do below). But if
2619 * it's not set explicitly, we reset our mode to something
2620 * that works
2622 if (resize_hpt_err) {
2623 spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
2624 error_free(resize_hpt_err);
2625 resize_hpt_err = NULL;
2626 } else {
2627 spapr->resize_hpt = smc->resize_hpt_default;
2631 assert(spapr->resize_hpt != SPAPR_RESIZE_HPT_DEFAULT);
2633 if ((spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) && resize_hpt_err) {
2635 * User requested HPT resize, but this host can't supply it. Bail out
2637 error_report_err(resize_hpt_err);
2638 exit(1);
2641 spapr->rma_size = node0_size;
2643 /* With KVM, we don't actually know whether KVM supports an
2644 * unbounded RMA (PR KVM) or is limited by the hash table size
2645 * (HV KVM using VRMA), so we always assume the latter
2647 * In that case, we also limit the initial allocations for RTAS
2648 * etc... to 256M since we have no way to know what the VRMA size
2649 * is going to be as it depends on the size of the hash table
2650 * which isn't determined yet.
2652 if (kvm_enabled()) {
2653 spapr->vrma_adjust = 1;
2654 spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
2657 /* Actually we don't support unbounded RMA anymore since we added
2658 * proper emulation of HV mode. The max we can get is 16G which
2659 * also happens to be what we configure for PAPR mode so make sure
2660 * we don't do anything bigger than that
2662 spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
2664 if (spapr->rma_size > node0_size) {
2665 error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2666 spapr->rma_size);
2667 exit(1);
2670 /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2671 load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
2674 * VSMT must be set in order to be able to compute VCPU ids, ie to
2675 * call spapr_max_server_number() or spapr_vcpu_id().
2677 spapr_set_vsmt_mode(spapr, &error_fatal);
2679 /* Set up Interrupt Controller before we create the VCPUs */
2680 spapr_irq_init(spapr, &error_fatal);
2682 /* Set up containers for ibm,client-architecture-support negotiated options
2684 spapr->ov5 = spapr_ovec_new();
2685 spapr->ov5_cas = spapr_ovec_new();
2687 if (smc->dr_lmb_enabled) {
2688 spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
2689 spapr_validate_node_memory(machine, &error_fatal);
2692 spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2694 /* advertise support for dedicated HP event source to guests */
2695 if (spapr->use_hotplug_event_source) {
2696 spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2699 /* advertise support for HPT resizing */
2700 if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
2701 spapr_ovec_set(spapr->ov5, OV5_HPT_RESIZE);
2704 /* advertise support for ibm,dyamic-memory-v2 */
2705 spapr_ovec_set(spapr->ov5, OV5_DRMEM_V2);
2707 /* advertise XIVE on POWER9 machines */
2708 if (spapr->irq->xive) {
2709 spapr_ovec_set(spapr->ov5, OV5_XIVE_EXPLOIT);
2712 /* init CPUs */
2713 spapr_init_cpus(spapr);
2716 * check we don't have a memory-less/cpu-less NUMA node
2717 * Firmware relies on the existing memory/cpu topology to provide the
2718 * NUMA topology to the kernel.
2719 * And the linux kernel needs to know the NUMA topology at start
2720 * to be able to hotplug CPUs later.
2722 if (machine->numa_state->num_nodes) {
2723 for (i = 0; i < machine->numa_state->num_nodes; ++i) {
2724 /* check for memory-less node */
2725 if (machine->numa_state->nodes[i].node_mem == 0) {
2726 CPUState *cs;
2727 int found = 0;
2728 /* check for cpu-less node */
2729 CPU_FOREACH(cs) {
2730 PowerPCCPU *cpu = POWERPC_CPU(cs);
2731 if (cpu->node_id == i) {
2732 found = 1;
2733 break;
2736 /* memory-less and cpu-less node */
2737 if (!found) {
2738 error_report(
2739 "Memory-less/cpu-less nodes are not supported (node %d)",
2741 exit(1);
2749 * NVLink2-connected GPU RAM needs to be placed on a separate NUMA node.
2750 * We assign a new numa ID per GPU in spapr_pci_collect_nvgpu() which is
2751 * called from vPHB reset handler so we initialize the counter here.
2752 * If no NUMA is configured from the QEMU side, we start from 1 as GPU RAM
2753 * must be equally distant from any other node.
2754 * The final value of spapr->gpu_numa_id is going to be written to
2755 * max-associativity-domains in spapr_build_fdt().
2757 spapr->gpu_numa_id = MAX(1, machine->numa_state->num_nodes);
2759 if ((!kvm_enabled() || kvmppc_has_cap_mmu_radix()) &&
2760 ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00, 0,
2761 spapr->max_compat_pvr)) {
2762 /* KVM and TCG always allow GTSE with radix... */
2763 spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
2765 /* ... but not with hash (currently). */
2767 if (kvm_enabled()) {
2768 /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2769 kvmppc_enable_logical_ci_hcalls();
2770 kvmppc_enable_set_mode_hcall();
2772 /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
2773 kvmppc_enable_clear_ref_mod_hcalls();
2775 /* Enable H_PAGE_INIT */
2776 kvmppc_enable_h_page_init();
2779 /* allocate RAM */
2780 memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2781 machine->ram_size);
2782 memory_region_add_subregion(sysmem, 0, ram);
2784 /* always allocate the device memory information */
2785 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
2787 /* initialize hotplug memory address space */
2788 if (machine->ram_size < machine->maxram_size) {
2789 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
2791 * Limit the number of hotpluggable memory slots to half the number
2792 * slots that KVM supports, leaving the other half for PCI and other
2793 * devices. However ensure that number of slots doesn't drop below 32.
2795 int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
2796 SPAPR_MAX_RAM_SLOTS;
2798 if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
2799 max_memslots = SPAPR_MAX_RAM_SLOTS;
2801 if (machine->ram_slots > max_memslots) {
2802 error_report("Specified number of memory slots %"
2803 PRIu64" exceeds max supported %d",
2804 machine->ram_slots, max_memslots);
2805 exit(1);
2808 machine->device_memory->base = ROUND_UP(machine->ram_size,
2809 SPAPR_DEVICE_MEM_ALIGN);
2810 memory_region_init(&machine->device_memory->mr, OBJECT(spapr),
2811 "device-memory", device_mem_size);
2812 memory_region_add_subregion(sysmem, machine->device_memory->base,
2813 &machine->device_memory->mr);
2816 if (smc->dr_lmb_enabled) {
2817 spapr_create_lmb_dr_connectors(spapr);
2820 /* Set up RTAS event infrastructure */
2821 spapr_events_init(spapr);
2823 /* Set up the RTC RTAS interfaces */
2824 spapr_rtc_create(spapr);
2826 /* Set up VIO bus */
2827 spapr->vio_bus = spapr_vio_bus_init();
2829 for (i = 0; i < serial_max_hds(); i++) {
2830 if (serial_hd(i)) {
2831 spapr_vty_create(spapr->vio_bus, serial_hd(i));
2835 /* We always have at least the nvram device on VIO */
2836 spapr_create_nvram(spapr);
2839 * Setup hotplug / dynamic-reconfiguration connectors. top-level
2840 * connectors (described in root DT node's "ibm,drc-types" property)
2841 * are pre-initialized here. additional child connectors (such as
2842 * connectors for a PHBs PCI slots) are added as needed during their
2843 * parent's realization.
2845 if (smc->dr_phb_enabled) {
2846 for (i = 0; i < SPAPR_MAX_PHBS; i++) {
2847 spapr_dr_connector_new(OBJECT(machine), TYPE_SPAPR_DRC_PHB, i);
2851 /* Set up PCI */
2852 spapr_pci_rtas_init();
2854 phb = spapr_create_default_phb();
2856 for (i = 0; i < nb_nics; i++) {
2857 NICInfo *nd = &nd_table[i];
2859 if (!nd->model) {
2860 nd->model = g_strdup("spapr-vlan");
2863 if (g_str_equal(nd->model, "spapr-vlan") ||
2864 g_str_equal(nd->model, "ibmveth")) {
2865 spapr_vlan_create(spapr->vio_bus, nd);
2866 } else {
2867 pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
2871 for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
2872 spapr_vscsi_create(spapr->vio_bus);
2875 /* Graphics */
2876 if (spapr_vga_init(phb->bus, &error_fatal)) {
2877 spapr->has_graphics = true;
2878 machine->usb |= defaults_enabled() && !machine->usb_disabled;
2881 if (machine->usb) {
2882 if (smc->use_ohci_by_default) {
2883 pci_create_simple(phb->bus, -1, "pci-ohci");
2884 } else {
2885 pci_create_simple(phb->bus, -1, "nec-usb-xhci");
2888 if (spapr->has_graphics) {
2889 USBBus *usb_bus = usb_bus_find(-1);
2891 usb_create_simple(usb_bus, "usb-kbd");
2892 usb_create_simple(usb_bus, "usb-mouse");
2896 if (spapr->rma_size < (MIN_RMA_SLOF * MiB)) {
2897 error_report(
2898 "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2899 MIN_RMA_SLOF);
2900 exit(1);
2903 if (kernel_filename) {
2904 uint64_t lowaddr = 0;
2906 spapr->kernel_size = load_elf(kernel_filename, NULL,
2907 translate_kernel_address, NULL,
2908 NULL, &lowaddr, NULL, 1,
2909 PPC_ELF_MACHINE, 0, 0);
2910 if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2911 spapr->kernel_size = load_elf(kernel_filename, NULL,
2912 translate_kernel_address, NULL, NULL,
2913 &lowaddr, NULL, 0, PPC_ELF_MACHINE,
2914 0, 0);
2915 spapr->kernel_le = spapr->kernel_size > 0;
2917 if (spapr->kernel_size < 0) {
2918 error_report("error loading %s: %s", kernel_filename,
2919 load_elf_strerror(spapr->kernel_size));
2920 exit(1);
2923 /* load initrd */
2924 if (initrd_filename) {
2925 /* Try to locate the initrd in the gap between the kernel
2926 * and the firmware. Add a bit of space just in case
2928 spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2929 + 0x1ffff) & ~0xffff;
2930 spapr->initrd_size = load_image_targphys(initrd_filename,
2931 spapr->initrd_base,
2932 load_limit
2933 - spapr->initrd_base);
2934 if (spapr->initrd_size < 0) {
2935 error_report("could not load initial ram disk '%s'",
2936 initrd_filename);
2937 exit(1);
2942 if (bios_name == NULL) {
2943 bios_name = FW_FILE_NAME;
2945 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
2946 if (!filename) {
2947 error_report("Could not find LPAR firmware '%s'", bios_name);
2948 exit(1);
2950 fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
2951 if (fw_size <= 0) {
2952 error_report("Could not load LPAR firmware '%s'", filename);
2953 exit(1);
2955 g_free(filename);
2957 /* FIXME: Should register things through the MachineState's qdev
2958 * interface, this is a legacy from the sPAPREnvironment structure
2959 * which predated MachineState but had a similar function */
2960 vmstate_register(NULL, 0, &vmstate_spapr, spapr);
2961 register_savevm_live("spapr/htab", -1, 1,
2962 &savevm_htab_handlers, spapr);
2964 qbus_set_hotplug_handler(sysbus_get_default(), OBJECT(machine),
2965 &error_fatal);
2967 qemu_register_boot_set(spapr_boot_set, spapr);
2970 * Nothing needs to be done to resume a suspended guest because
2971 * suspending does not change the machine state, so no need for
2972 * a ->wakeup method.
2974 qemu_register_wakeup_support();
2976 if (kvm_enabled()) {
2977 /* to stop and start vmclock */
2978 qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
2979 &spapr->tb);
2981 kvmppc_spapr_enable_inkernel_multitce();
2985 static int spapr_kvm_type(MachineState *machine, const char *vm_type)
2987 if (!vm_type) {
2988 return 0;
2991 if (!strcmp(vm_type, "HV")) {
2992 return 1;
2995 if (!strcmp(vm_type, "PR")) {
2996 return 2;
2999 error_report("Unknown kvm-type specified '%s'", vm_type);
3000 exit(1);
3004 * Implementation of an interface to adjust firmware path
3005 * for the bootindex property handling.
3007 static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
3008 DeviceState *dev)
3010 #define CAST(type, obj, name) \
3011 ((type *)object_dynamic_cast(OBJECT(obj), (name)))
3012 SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE);
3013 SpaprPhbState *phb = CAST(SpaprPhbState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
3014 VHostSCSICommon *vsc = CAST(VHostSCSICommon, dev, TYPE_VHOST_SCSI_COMMON);
3016 if (d) {
3017 void *spapr = CAST(void, bus->parent, "spapr-vscsi");
3018 VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
3019 USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
3021 if (spapr) {
3023 * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
3024 * In the top 16 bits of the 64-bit LUN, we use SRP luns of the form
3025 * 0x8000 | (target << 8) | (bus << 5) | lun
3026 * (see the "Logical unit addressing format" table in SAM5)
3028 unsigned id = 0x8000 | (d->id << 8) | (d->channel << 5) | d->lun;
3029 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
3030 (uint64_t)id << 48);
3031 } else if (virtio) {
3033 * We use SRP luns of the form 01000000 | (target << 8) | lun
3034 * in the top 32 bits of the 64-bit LUN
3035 * Note: the quote above is from SLOF and it is wrong,
3036 * the actual binding is:
3037 * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
3039 unsigned id = 0x1000000 | (d->id << 16) | d->lun;
3040 if (d->lun >= 256) {
3041 /* Use the LUN "flat space addressing method" */
3042 id |= 0x4000;
3044 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
3045 (uint64_t)id << 32);
3046 } else if (usb) {
3048 * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
3049 * in the top 32 bits of the 64-bit LUN
3051 unsigned usb_port = atoi(usb->port->path);
3052 unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
3053 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
3054 (uint64_t)id << 32);
3059 * SLOF probes the USB devices, and if it recognizes that the device is a
3060 * storage device, it changes its name to "storage" instead of "usb-host",
3061 * and additionally adds a child node for the SCSI LUN, so the correct
3062 * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
3064 if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
3065 USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
3066 if (usb_host_dev_is_scsi_storage(usbdev)) {
3067 return g_strdup_printf("storage@%s/disk", usbdev->port->path);
3071 if (phb) {
3072 /* Replace "pci" with "pci@800000020000000" */
3073 return g_strdup_printf("pci@%"PRIX64, phb->buid);
3076 if (vsc) {
3077 /* Same logic as virtio above */
3078 unsigned id = 0x1000000 | (vsc->target << 16) | vsc->lun;
3079 return g_strdup_printf("disk@%"PRIX64, (uint64_t)id << 32);
3082 if (g_str_equal("pci-bridge", qdev_fw_name(dev))) {
3083 /* SLOF uses "pci" instead of "pci-bridge" for PCI bridges */
3084 PCIDevice *pcidev = CAST(PCIDevice, dev, TYPE_PCI_DEVICE);
3085 return g_strdup_printf("pci@%x", PCI_SLOT(pcidev->devfn));
3088 return NULL;
3091 static char *spapr_get_kvm_type(Object *obj, Error **errp)
3093 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3095 return g_strdup(spapr->kvm_type);
3098 static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
3100 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3102 g_free(spapr->kvm_type);
3103 spapr->kvm_type = g_strdup(value);
3106 static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
3108 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3110 return spapr->use_hotplug_event_source;
3113 static void spapr_set_modern_hotplug_events(Object *obj, bool value,
3114 Error **errp)
3116 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3118 spapr->use_hotplug_event_source = value;
3121 static bool spapr_get_msix_emulation(Object *obj, Error **errp)
3123 return true;
3126 static char *spapr_get_resize_hpt(Object *obj, Error **errp)
3128 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3130 switch (spapr->resize_hpt) {
3131 case SPAPR_RESIZE_HPT_DEFAULT:
3132 return g_strdup("default");
3133 case SPAPR_RESIZE_HPT_DISABLED:
3134 return g_strdup("disabled");
3135 case SPAPR_RESIZE_HPT_ENABLED:
3136 return g_strdup("enabled");
3137 case SPAPR_RESIZE_HPT_REQUIRED:
3138 return g_strdup("required");
3140 g_assert_not_reached();
3143 static void spapr_set_resize_hpt(Object *obj, const char *value, Error **errp)
3145 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3147 if (strcmp(value, "default") == 0) {
3148 spapr->resize_hpt = SPAPR_RESIZE_HPT_DEFAULT;
3149 } else if (strcmp(value, "disabled") == 0) {
3150 spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
3151 } else if (strcmp(value, "enabled") == 0) {
3152 spapr->resize_hpt = SPAPR_RESIZE_HPT_ENABLED;
3153 } else if (strcmp(value, "required") == 0) {
3154 spapr->resize_hpt = SPAPR_RESIZE_HPT_REQUIRED;
3155 } else {
3156 error_setg(errp, "Bad value for \"resize-hpt\" property");
3160 static void spapr_get_vsmt(Object *obj, Visitor *v, const char *name,
3161 void *opaque, Error **errp)
3163 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
3166 static void spapr_set_vsmt(Object *obj, Visitor *v, const char *name,
3167 void *opaque, Error **errp)
3169 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
3172 static char *spapr_get_ic_mode(Object *obj, Error **errp)
3174 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3176 if (spapr->irq == &spapr_irq_xics_legacy) {
3177 return g_strdup("legacy");
3178 } else if (spapr->irq == &spapr_irq_xics) {
3179 return g_strdup("xics");
3180 } else if (spapr->irq == &spapr_irq_xive) {
3181 return g_strdup("xive");
3182 } else if (spapr->irq == &spapr_irq_dual) {
3183 return g_strdup("dual");
3185 g_assert_not_reached();
3188 static void spapr_set_ic_mode(Object *obj, const char *value, Error **errp)
3190 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3192 if (SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
3193 error_setg(errp, "This machine only uses the legacy XICS backend, don't pass ic-mode");
3194 return;
3197 /* The legacy IRQ backend can not be set */
3198 if (strcmp(value, "xics") == 0) {
3199 spapr->irq = &spapr_irq_xics;
3200 } else if (strcmp(value, "xive") == 0) {
3201 spapr->irq = &spapr_irq_xive;
3202 } else if (strcmp(value, "dual") == 0) {
3203 spapr->irq = &spapr_irq_dual;
3204 } else {
3205 error_setg(errp, "Bad value for \"ic-mode\" property");
3209 static char *spapr_get_host_model(Object *obj, Error **errp)
3211 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3213 return g_strdup(spapr->host_model);
3216 static void spapr_set_host_model(Object *obj, const char *value, Error **errp)
3218 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3220 g_free(spapr->host_model);
3221 spapr->host_model = g_strdup(value);
3224 static char *spapr_get_host_serial(Object *obj, Error **errp)
3226 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3228 return g_strdup(spapr->host_serial);
3231 static void spapr_set_host_serial(Object *obj, const char *value, Error **errp)
3233 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3235 g_free(spapr->host_serial);
3236 spapr->host_serial = g_strdup(value);
3239 static void spapr_instance_init(Object *obj)
3241 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3242 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
3244 spapr->htab_fd = -1;
3245 spapr->use_hotplug_event_source = true;
3246 object_property_add_str(obj, "kvm-type",
3247 spapr_get_kvm_type, spapr_set_kvm_type, NULL);
3248 object_property_set_description(obj, "kvm-type",
3249 "Specifies the KVM virtualization mode (HV, PR)",
3250 NULL);
3251 object_property_add_bool(obj, "modern-hotplug-events",
3252 spapr_get_modern_hotplug_events,
3253 spapr_set_modern_hotplug_events,
3254 NULL);
3255 object_property_set_description(obj, "modern-hotplug-events",
3256 "Use dedicated hotplug event mechanism in"
3257 " place of standard EPOW events when possible"
3258 " (required for memory hot-unplug support)",
3259 NULL);
3260 ppc_compat_add_property(obj, "max-cpu-compat", &spapr->max_compat_pvr,
3261 "Maximum permitted CPU compatibility mode",
3262 &error_fatal);
3264 object_property_add_str(obj, "resize-hpt",
3265 spapr_get_resize_hpt, spapr_set_resize_hpt, NULL);
3266 object_property_set_description(obj, "resize-hpt",
3267 "Resizing of the Hash Page Table (enabled, disabled, required)",
3268 NULL);
3269 object_property_add(obj, "vsmt", "uint32", spapr_get_vsmt,
3270 spapr_set_vsmt, NULL, &spapr->vsmt, &error_abort);
3271 object_property_set_description(obj, "vsmt",
3272 "Virtual SMT: KVM behaves as if this were"
3273 " the host's SMT mode", &error_abort);
3274 object_property_add_bool(obj, "vfio-no-msix-emulation",
3275 spapr_get_msix_emulation, NULL, NULL);
3277 /* The machine class defines the default interrupt controller mode */
3278 spapr->irq = smc->irq;
3279 object_property_add_str(obj, "ic-mode", spapr_get_ic_mode,
3280 spapr_set_ic_mode, NULL);
3281 object_property_set_description(obj, "ic-mode",
3282 "Specifies the interrupt controller mode (xics, xive, dual)",
3283 NULL);
3285 object_property_add_str(obj, "host-model",
3286 spapr_get_host_model, spapr_set_host_model,
3287 &error_abort);
3288 object_property_set_description(obj, "host-model",
3289 "Host model to advertise in guest device tree", &error_abort);
3290 object_property_add_str(obj, "host-serial",
3291 spapr_get_host_serial, spapr_set_host_serial,
3292 &error_abort);
3293 object_property_set_description(obj, "host-serial",
3294 "Host serial number to advertise in guest device tree", &error_abort);
3297 static void spapr_machine_finalizefn(Object *obj)
3299 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3301 g_free(spapr->kvm_type);
3304 void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
3306 cpu_synchronize_state(cs);
3307 ppc_cpu_do_system_reset(cs);
3310 static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
3312 CPUState *cs;
3314 CPU_FOREACH(cs) {
3315 async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
3319 int spapr_lmb_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
3320 void *fdt, int *fdt_start_offset, Error **errp)
3322 uint64_t addr;
3323 uint32_t node;
3325 addr = spapr_drc_index(drc) * SPAPR_MEMORY_BLOCK_SIZE;
3326 node = object_property_get_uint(OBJECT(drc->dev), PC_DIMM_NODE_PROP,
3327 &error_abort);
3328 *fdt_start_offset = spapr_populate_memory_node(fdt, node, addr,
3329 SPAPR_MEMORY_BLOCK_SIZE);
3330 return 0;
3333 static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
3334 bool dedicated_hp_event_source, Error **errp)
3336 SpaprDrc *drc;
3337 uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
3338 int i;
3339 uint64_t addr = addr_start;
3340 bool hotplugged = spapr_drc_hotplugged(dev);
3341 Error *local_err = NULL;
3343 for (i = 0; i < nr_lmbs; i++) {
3344 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3345 addr / SPAPR_MEMORY_BLOCK_SIZE);
3346 g_assert(drc);
3348 spapr_drc_attach(drc, dev, &local_err);
3349 if (local_err) {
3350 while (addr > addr_start) {
3351 addr -= SPAPR_MEMORY_BLOCK_SIZE;
3352 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3353 addr / SPAPR_MEMORY_BLOCK_SIZE);
3354 spapr_drc_detach(drc);
3356 error_propagate(errp, local_err);
3357 return;
3359 if (!hotplugged) {
3360 spapr_drc_reset(drc);
3362 addr += SPAPR_MEMORY_BLOCK_SIZE;
3364 /* send hotplug notification to the
3365 * guest only in case of hotplugged memory
3367 if (hotplugged) {
3368 if (dedicated_hp_event_source) {
3369 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3370 addr_start / SPAPR_MEMORY_BLOCK_SIZE);
3371 spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
3372 nr_lmbs,
3373 spapr_drc_index(drc));
3374 } else {
3375 spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
3376 nr_lmbs);
3381 static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3382 Error **errp)
3384 Error *local_err = NULL;
3385 SpaprMachineState *ms = SPAPR_MACHINE(hotplug_dev);
3386 PCDIMMDevice *dimm = PC_DIMM(dev);
3387 uint64_t size, addr;
3389 size = memory_device_get_region_size(MEMORY_DEVICE(dev), &error_abort);
3391 pc_dimm_plug(dimm, MACHINE(ms), &local_err);
3392 if (local_err) {
3393 goto out;
3396 addr = object_property_get_uint(OBJECT(dimm),
3397 PC_DIMM_ADDR_PROP, &local_err);
3398 if (local_err) {
3399 goto out_unplug;
3402 spapr_add_lmbs(dev, addr, size, spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
3403 &local_err);
3404 if (local_err) {
3405 goto out_unplug;
3408 return;
3410 out_unplug:
3411 pc_dimm_unplug(dimm, MACHINE(ms));
3412 out:
3413 error_propagate(errp, local_err);
3416 static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3417 Error **errp)
3419 const SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(hotplug_dev);
3420 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
3421 PCDIMMDevice *dimm = PC_DIMM(dev);
3422 Error *local_err = NULL;
3423 uint64_t size;
3424 Object *memdev;
3425 hwaddr pagesize;
3427 if (!smc->dr_lmb_enabled) {
3428 error_setg(errp, "Memory hotplug not supported for this machine");
3429 return;
3432 size = memory_device_get_region_size(MEMORY_DEVICE(dimm), &local_err);
3433 if (local_err) {
3434 error_propagate(errp, local_err);
3435 return;
3438 if (size % SPAPR_MEMORY_BLOCK_SIZE) {
3439 error_setg(errp, "Hotplugged memory size must be a multiple of "
3440 "%" PRIu64 " MB", SPAPR_MEMORY_BLOCK_SIZE / MiB);
3441 return;
3444 memdev = object_property_get_link(OBJECT(dimm), PC_DIMM_MEMDEV_PROP,
3445 &error_abort);
3446 pagesize = host_memory_backend_pagesize(MEMORY_BACKEND(memdev));
3447 spapr_check_pagesize(spapr, pagesize, &local_err);
3448 if (local_err) {
3449 error_propagate(errp, local_err);
3450 return;
3453 pc_dimm_pre_plug(dimm, MACHINE(hotplug_dev), NULL, errp);
3456 struct SpaprDimmState {
3457 PCDIMMDevice *dimm;
3458 uint32_t nr_lmbs;
3459 QTAILQ_ENTRY(SpaprDimmState) next;
3462 static SpaprDimmState *spapr_pending_dimm_unplugs_find(SpaprMachineState *s,
3463 PCDIMMDevice *dimm)
3465 SpaprDimmState *dimm_state = NULL;
3467 QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
3468 if (dimm_state->dimm == dimm) {
3469 break;
3472 return dimm_state;
3475 static SpaprDimmState *spapr_pending_dimm_unplugs_add(SpaprMachineState *spapr,
3476 uint32_t nr_lmbs,
3477 PCDIMMDevice *dimm)
3479 SpaprDimmState *ds = NULL;
3482 * If this request is for a DIMM whose removal had failed earlier
3483 * (due to guest's refusal to remove the LMBs), we would have this
3484 * dimm already in the pending_dimm_unplugs list. In that
3485 * case don't add again.
3487 ds = spapr_pending_dimm_unplugs_find(spapr, dimm);
3488 if (!ds) {
3489 ds = g_malloc0(sizeof(SpaprDimmState));
3490 ds->nr_lmbs = nr_lmbs;
3491 ds->dimm = dimm;
3492 QTAILQ_INSERT_HEAD(&spapr->pending_dimm_unplugs, ds, next);
3494 return ds;
3497 static void spapr_pending_dimm_unplugs_remove(SpaprMachineState *spapr,
3498 SpaprDimmState *dimm_state)
3500 QTAILQ_REMOVE(&spapr->pending_dimm_unplugs, dimm_state, next);
3501 g_free(dimm_state);
3504 static SpaprDimmState *spapr_recover_pending_dimm_state(SpaprMachineState *ms,
3505 PCDIMMDevice *dimm)
3507 SpaprDrc *drc;
3508 uint64_t size = memory_device_get_region_size(MEMORY_DEVICE(dimm),
3509 &error_abort);
3510 uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
3511 uint32_t avail_lmbs = 0;
3512 uint64_t addr_start, addr;
3513 int i;
3515 addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
3516 &error_abort);
3518 addr = addr_start;
3519 for (i = 0; i < nr_lmbs; i++) {
3520 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3521 addr / SPAPR_MEMORY_BLOCK_SIZE);
3522 g_assert(drc);
3523 if (drc->dev) {
3524 avail_lmbs++;
3526 addr += SPAPR_MEMORY_BLOCK_SIZE;
3529 return spapr_pending_dimm_unplugs_add(ms, avail_lmbs, dimm);
3532 /* Callback to be called during DRC release. */
3533 void spapr_lmb_release(DeviceState *dev)
3535 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
3536 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_ctrl);
3537 SpaprDimmState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
3539 /* This information will get lost if a migration occurs
3540 * during the unplug process. In this case recover it. */
3541 if (ds == NULL) {
3542 ds = spapr_recover_pending_dimm_state(spapr, PC_DIMM(dev));
3543 g_assert(ds);
3544 /* The DRC being examined by the caller at least must be counted */
3545 g_assert(ds->nr_lmbs);
3548 if (--ds->nr_lmbs) {
3549 return;
3553 * Now that all the LMBs have been removed by the guest, call the
3554 * unplug handler chain. This can never fail.
3556 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
3557 object_unparent(OBJECT(dev));
3560 static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3562 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
3563 SpaprDimmState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
3565 pc_dimm_unplug(PC_DIMM(dev), MACHINE(hotplug_dev));
3566 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
3567 spapr_pending_dimm_unplugs_remove(spapr, ds);
3570 static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
3571 DeviceState *dev, Error **errp)
3573 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
3574 Error *local_err = NULL;
3575 PCDIMMDevice *dimm = PC_DIMM(dev);
3576 uint32_t nr_lmbs;
3577 uint64_t size, addr_start, addr;
3578 int i;
3579 SpaprDrc *drc;
3581 size = memory_device_get_region_size(MEMORY_DEVICE(dimm), &error_abort);
3582 nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
3584 addr_start = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
3585 &local_err);
3586 if (local_err) {
3587 goto out;
3591 * An existing pending dimm state for this DIMM means that there is an
3592 * unplug operation in progress, waiting for the spapr_lmb_release
3593 * callback to complete the job (BQL can't cover that far). In this case,
3594 * bail out to avoid detaching DRCs that were already released.
3596 if (spapr_pending_dimm_unplugs_find(spapr, dimm)) {
3597 error_setg(&local_err,
3598 "Memory unplug already in progress for device %s",
3599 dev->id);
3600 goto out;
3603 spapr_pending_dimm_unplugs_add(spapr, nr_lmbs, dimm);
3605 addr = addr_start;
3606 for (i = 0; i < nr_lmbs; i++) {
3607 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3608 addr / SPAPR_MEMORY_BLOCK_SIZE);
3609 g_assert(drc);
3611 spapr_drc_detach(drc);
3612 addr += SPAPR_MEMORY_BLOCK_SIZE;
3615 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3616 addr_start / SPAPR_MEMORY_BLOCK_SIZE);
3617 spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
3618 nr_lmbs, spapr_drc_index(drc));
3619 out:
3620 error_propagate(errp, local_err);
3623 /* Callback to be called during DRC release. */
3624 void spapr_core_release(DeviceState *dev)
3626 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
3628 /* Call the unplug handler chain. This can never fail. */
3629 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
3630 object_unparent(OBJECT(dev));
3633 static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3635 MachineState *ms = MACHINE(hotplug_dev);
3636 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
3637 CPUCore *cc = CPU_CORE(dev);
3638 CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
3640 if (smc->pre_2_10_has_unused_icps) {
3641 SpaprCpuCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
3642 int i;
3644 for (i = 0; i < cc->nr_threads; i++) {
3645 CPUState *cs = CPU(sc->threads[i]);
3647 pre_2_10_vmstate_register_dummy_icp(cs->cpu_index);
3651 assert(core_slot);
3652 core_slot->cpu = NULL;
3653 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
3656 static
3657 void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
3658 Error **errp)
3660 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3661 int index;
3662 SpaprDrc *drc;
3663 CPUCore *cc = CPU_CORE(dev);
3665 if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
3666 error_setg(errp, "Unable to find CPU core with core-id: %d",
3667 cc->core_id);
3668 return;
3670 if (index == 0) {
3671 error_setg(errp, "Boot CPU core may not be unplugged");
3672 return;
3675 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU,
3676 spapr_vcpu_id(spapr, cc->core_id));
3677 g_assert(drc);
3679 if (!spapr_drc_unplug_requested(drc)) {
3680 spapr_drc_detach(drc);
3681 spapr_hotplug_req_remove_by_index(drc);
3685 int spapr_core_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
3686 void *fdt, int *fdt_start_offset, Error **errp)
3688 SpaprCpuCore *core = SPAPR_CPU_CORE(drc->dev);
3689 CPUState *cs = CPU(core->threads[0]);
3690 PowerPCCPU *cpu = POWERPC_CPU(cs);
3691 DeviceClass *dc = DEVICE_GET_CLASS(cs);
3692 int id = spapr_get_vcpu_id(cpu);
3693 char *nodename;
3694 int offset;
3696 nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
3697 offset = fdt_add_subnode(fdt, 0, nodename);
3698 g_free(nodename);
3700 spapr_populate_cpu_dt(cs, fdt, offset, spapr);
3702 *fdt_start_offset = offset;
3703 return 0;
3706 static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3707 Error **errp)
3709 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3710 MachineClass *mc = MACHINE_GET_CLASS(spapr);
3711 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3712 SpaprCpuCore *core = SPAPR_CPU_CORE(OBJECT(dev));
3713 CPUCore *cc = CPU_CORE(dev);
3714 CPUState *cs;
3715 SpaprDrc *drc;
3716 Error *local_err = NULL;
3717 CPUArchId *core_slot;
3718 int index;
3719 bool hotplugged = spapr_drc_hotplugged(dev);
3720 int i;
3722 core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3723 if (!core_slot) {
3724 error_setg(errp, "Unable to find CPU core with core-id: %d",
3725 cc->core_id);
3726 return;
3728 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU,
3729 spapr_vcpu_id(spapr, cc->core_id));
3731 g_assert(drc || !mc->has_hotpluggable_cpus);
3733 if (drc) {
3734 spapr_drc_attach(drc, dev, &local_err);
3735 if (local_err) {
3736 error_propagate(errp, local_err);
3737 return;
3740 if (hotplugged) {
3742 * Send hotplug notification interrupt to the guest only
3743 * in case of hotplugged CPUs.
3745 spapr_hotplug_req_add_by_index(drc);
3746 } else {
3747 spapr_drc_reset(drc);
3751 core_slot->cpu = OBJECT(dev);
3753 if (smc->pre_2_10_has_unused_icps) {
3754 for (i = 0; i < cc->nr_threads; i++) {
3755 cs = CPU(core->threads[i]);
3756 pre_2_10_vmstate_unregister_dummy_icp(cs->cpu_index);
3761 * Set compatibility mode to match the boot CPU, which was either set
3762 * by the machine reset code or by CAS.
3764 if (hotplugged) {
3765 for (i = 0; i < cc->nr_threads; i++) {
3766 ppc_set_compat(core->threads[i], POWERPC_CPU(first_cpu)->compat_pvr,
3767 &local_err);
3768 if (local_err) {
3769 error_propagate(errp, local_err);
3770 return;
3776 static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3777 Error **errp)
3779 MachineState *machine = MACHINE(OBJECT(hotplug_dev));
3780 MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
3781 Error *local_err = NULL;
3782 CPUCore *cc = CPU_CORE(dev);
3783 const char *base_core_type = spapr_get_cpu_core_type(machine->cpu_type);
3784 const char *type = object_get_typename(OBJECT(dev));
3785 CPUArchId *core_slot;
3786 int index;
3787 unsigned int smp_threads = machine->smp.threads;
3789 if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
3790 error_setg(&local_err, "CPU hotplug not supported for this machine");
3791 goto out;
3794 if (strcmp(base_core_type, type)) {
3795 error_setg(&local_err, "CPU core type should be %s", base_core_type);
3796 goto out;
3799 if (cc->core_id % smp_threads) {
3800 error_setg(&local_err, "invalid core id %d", cc->core_id);
3801 goto out;
3805 * In general we should have homogeneous threads-per-core, but old
3806 * (pre hotplug support) machine types allow the last core to have
3807 * reduced threads as a compatibility hack for when we allowed
3808 * total vcpus not a multiple of threads-per-core.
3810 if (mc->has_hotpluggable_cpus && (cc->nr_threads != smp_threads)) {
3811 error_setg(&local_err, "invalid nr-threads %d, must be %d",
3812 cc->nr_threads, smp_threads);
3813 goto out;
3816 core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3817 if (!core_slot) {
3818 error_setg(&local_err, "core id %d out of range", cc->core_id);
3819 goto out;
3822 if (core_slot->cpu) {
3823 error_setg(&local_err, "core %d already populated", cc->core_id);
3824 goto out;
3827 numa_cpu_pre_plug(core_slot, dev, &local_err);
3829 out:
3830 error_propagate(errp, local_err);
3833 int spapr_phb_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
3834 void *fdt, int *fdt_start_offset, Error **errp)
3836 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(drc->dev);
3837 int intc_phandle;
3839 intc_phandle = spapr_irq_get_phandle(spapr, spapr->fdt_blob, errp);
3840 if (intc_phandle <= 0) {
3841 return -1;
3844 if (spapr_dt_phb(spapr, sphb, intc_phandle, fdt, fdt_start_offset)) {
3845 error_setg(errp, "unable to create FDT node for PHB %d", sphb->index);
3846 return -1;
3849 /* generally SLOF creates these, for hotplug it's up to QEMU */
3850 _FDT(fdt_setprop_string(fdt, *fdt_start_offset, "name", "pci"));
3852 return 0;
3855 static void spapr_phb_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3856 Error **errp)
3858 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3859 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(dev);
3860 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
3861 const unsigned windows_supported = spapr_phb_windows_supported(sphb);
3863 if (dev->hotplugged && !smc->dr_phb_enabled) {
3864 error_setg(errp, "PHB hotplug not supported for this machine");
3865 return;
3868 if (sphb->index == (uint32_t)-1) {
3869 error_setg(errp, "\"index\" for PAPR PHB is mandatory");
3870 return;
3874 * This will check that sphb->index doesn't exceed the maximum number of
3875 * PHBs for the current machine type.
3877 smc->phb_placement(spapr, sphb->index,
3878 &sphb->buid, &sphb->io_win_addr,
3879 &sphb->mem_win_addr, &sphb->mem64_win_addr,
3880 windows_supported, sphb->dma_liobn,
3881 &sphb->nv2_gpa_win_addr, &sphb->nv2_atsd_win_addr,
3882 errp);
3885 static void spapr_phb_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3886 Error **errp)
3888 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3889 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
3890 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(dev);
3891 SpaprDrc *drc;
3892 bool hotplugged = spapr_drc_hotplugged(dev);
3893 Error *local_err = NULL;
3895 if (!smc->dr_phb_enabled) {
3896 return;
3899 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PHB, sphb->index);
3900 /* hotplug hooks should check it's enabled before getting this far */
3901 assert(drc);
3903 spapr_drc_attach(drc, DEVICE(dev), &local_err);
3904 if (local_err) {
3905 error_propagate(errp, local_err);
3906 return;
3909 if (hotplugged) {
3910 spapr_hotplug_req_add_by_index(drc);
3911 } else {
3912 spapr_drc_reset(drc);
3916 void spapr_phb_release(DeviceState *dev)
3918 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
3920 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
3921 object_unparent(OBJECT(dev));
3924 static void spapr_phb_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3926 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
3929 static void spapr_phb_unplug_request(HotplugHandler *hotplug_dev,
3930 DeviceState *dev, Error **errp)
3932 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(dev);
3933 SpaprDrc *drc;
3935 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PHB, sphb->index);
3936 assert(drc);
3938 if (!spapr_drc_unplug_requested(drc)) {
3939 spapr_drc_detach(drc);
3940 spapr_hotplug_req_remove_by_index(drc);
3944 static void spapr_tpm_proxy_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3945 Error **errp)
3947 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3948 SpaprTpmProxy *tpm_proxy = SPAPR_TPM_PROXY(dev);
3950 if (spapr->tpm_proxy != NULL) {
3951 error_setg(errp, "Only one TPM proxy can be specified for this machine");
3952 return;
3955 spapr->tpm_proxy = tpm_proxy;
3958 static void spapr_tpm_proxy_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3960 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3962 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
3963 object_unparent(OBJECT(dev));
3964 spapr->tpm_proxy = NULL;
3967 static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
3968 DeviceState *dev, Error **errp)
3970 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3971 spapr_memory_plug(hotplug_dev, dev, errp);
3972 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3973 spapr_core_plug(hotplug_dev, dev, errp);
3974 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
3975 spapr_phb_plug(hotplug_dev, dev, errp);
3976 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
3977 spapr_tpm_proxy_plug(hotplug_dev, dev, errp);
3981 static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
3982 DeviceState *dev, Error **errp)
3984 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3985 spapr_memory_unplug(hotplug_dev, dev);
3986 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3987 spapr_core_unplug(hotplug_dev, dev);
3988 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
3989 spapr_phb_unplug(hotplug_dev, dev);
3990 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
3991 spapr_tpm_proxy_unplug(hotplug_dev, dev);
3995 static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
3996 DeviceState *dev, Error **errp)
3998 SpaprMachineState *sms = SPAPR_MACHINE(OBJECT(hotplug_dev));
3999 MachineClass *mc = MACHINE_GET_CLASS(sms);
4000 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4002 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
4003 if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
4004 spapr_memory_unplug_request(hotplug_dev, dev, errp);
4005 } else {
4006 /* NOTE: this means there is a window after guest reset, prior to
4007 * CAS negotiation, where unplug requests will fail due to the
4008 * capability not being detected yet. This is a bit different than
4009 * the case with PCI unplug, where the events will be queued and
4010 * eventually handled by the guest after boot
4012 error_setg(errp, "Memory hot unplug not supported for this guest");
4014 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
4015 if (!mc->has_hotpluggable_cpus) {
4016 error_setg(errp, "CPU hot unplug not supported on this machine");
4017 return;
4019 spapr_core_unplug_request(hotplug_dev, dev, errp);
4020 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
4021 if (!smc->dr_phb_enabled) {
4022 error_setg(errp, "PHB hot unplug not supported on this machine");
4023 return;
4025 spapr_phb_unplug_request(hotplug_dev, dev, errp);
4026 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
4027 spapr_tpm_proxy_unplug(hotplug_dev, dev);
4031 static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
4032 DeviceState *dev, Error **errp)
4034 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
4035 spapr_memory_pre_plug(hotplug_dev, dev, errp);
4036 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
4037 spapr_core_pre_plug(hotplug_dev, dev, errp);
4038 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
4039 spapr_phb_pre_plug(hotplug_dev, dev, errp);
4043 static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
4044 DeviceState *dev)
4046 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
4047 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE) ||
4048 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE) ||
4049 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
4050 return HOTPLUG_HANDLER(machine);
4052 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
4053 PCIDevice *pcidev = PCI_DEVICE(dev);
4054 PCIBus *root = pci_device_root_bus(pcidev);
4055 SpaprPhbState *phb =
4056 (SpaprPhbState *)object_dynamic_cast(OBJECT(BUS(root)->parent),
4057 TYPE_SPAPR_PCI_HOST_BRIDGE);
4059 if (phb) {
4060 return HOTPLUG_HANDLER(phb);
4063 return NULL;
4066 static CpuInstanceProperties
4067 spapr_cpu_index_to_props(MachineState *machine, unsigned cpu_index)
4069 CPUArchId *core_slot;
4070 MachineClass *mc = MACHINE_GET_CLASS(machine);
4072 /* make sure possible_cpu are intialized */
4073 mc->possible_cpu_arch_ids(machine);
4074 /* get CPU core slot containing thread that matches cpu_index */
4075 core_slot = spapr_find_cpu_slot(machine, cpu_index, NULL);
4076 assert(core_slot);
4077 return core_slot->props;
4080 static int64_t spapr_get_default_cpu_node_id(const MachineState *ms, int idx)
4082 return idx / ms->smp.cores % ms->numa_state->num_nodes;
4085 static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
4087 int i;
4088 unsigned int smp_threads = machine->smp.threads;
4089 unsigned int smp_cpus = machine->smp.cpus;
4090 const char *core_type;
4091 int spapr_max_cores = machine->smp.max_cpus / smp_threads;
4092 MachineClass *mc = MACHINE_GET_CLASS(machine);
4094 if (!mc->has_hotpluggable_cpus) {
4095 spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
4097 if (machine->possible_cpus) {
4098 assert(machine->possible_cpus->len == spapr_max_cores);
4099 return machine->possible_cpus;
4102 core_type = spapr_get_cpu_core_type(machine->cpu_type);
4103 if (!core_type) {
4104 error_report("Unable to find sPAPR CPU Core definition");
4105 exit(1);
4108 machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
4109 sizeof(CPUArchId) * spapr_max_cores);
4110 machine->possible_cpus->len = spapr_max_cores;
4111 for (i = 0; i < machine->possible_cpus->len; i++) {
4112 int core_id = i * smp_threads;
4114 machine->possible_cpus->cpus[i].type = core_type;
4115 machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
4116 machine->possible_cpus->cpus[i].arch_id = core_id;
4117 machine->possible_cpus->cpus[i].props.has_core_id = true;
4118 machine->possible_cpus->cpus[i].props.core_id = core_id;
4120 return machine->possible_cpus;
4123 static void spapr_phb_placement(SpaprMachineState *spapr, uint32_t index,
4124 uint64_t *buid, hwaddr *pio,
4125 hwaddr *mmio32, hwaddr *mmio64,
4126 unsigned n_dma, uint32_t *liobns,
4127 hwaddr *nv2gpa, hwaddr *nv2atsd, Error **errp)
4130 * New-style PHB window placement.
4132 * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
4133 * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
4134 * windows.
4136 * Some guest kernels can't work with MMIO windows above 1<<46
4137 * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
4139 * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
4140 * PHB stacked together. (32TiB+2GiB)..(32TiB+64GiB) contains the
4141 * 2GiB 32-bit MMIO windows for each PHB. Then 33..64TiB has the
4142 * 1TiB 64-bit MMIO windows for each PHB.
4144 const uint64_t base_buid = 0x800000020000000ULL;
4145 int i;
4147 /* Sanity check natural alignments */
4148 QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
4149 QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
4150 QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
4151 QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
4152 /* Sanity check bounds */
4153 QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
4154 SPAPR_PCI_MEM32_WIN_SIZE);
4155 QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
4156 SPAPR_PCI_MEM64_WIN_SIZE);
4158 if (index >= SPAPR_MAX_PHBS) {
4159 error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
4160 SPAPR_MAX_PHBS - 1);
4161 return;
4164 *buid = base_buid + index;
4165 for (i = 0; i < n_dma; ++i) {
4166 liobns[i] = SPAPR_PCI_LIOBN(index, i);
4169 *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
4170 *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
4171 *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
4173 *nv2gpa = SPAPR_PCI_NV2RAM64_WIN_BASE + index * SPAPR_PCI_NV2RAM64_WIN_SIZE;
4174 *nv2atsd = SPAPR_PCI_NV2ATSD_WIN_BASE + index * SPAPR_PCI_NV2ATSD_WIN_SIZE;
4177 static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
4179 SpaprMachineState *spapr = SPAPR_MACHINE(dev);
4181 return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
4184 static void spapr_ics_resend(XICSFabric *dev)
4186 SpaprMachineState *spapr = SPAPR_MACHINE(dev);
4188 ics_resend(spapr->ics);
4191 static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
4193 PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
4195 return cpu ? spapr_cpu_state(cpu)->icp : NULL;
4198 static void spapr_pic_print_info(InterruptStatsProvider *obj,
4199 Monitor *mon)
4201 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
4203 spapr_irq_print_info(spapr, mon);
4204 monitor_printf(mon, "irqchip: %s\n",
4205 kvm_irqchip_in_kernel() ? "in-kernel" : "emulated");
4208 static int spapr_match_nvt(XiveFabric *xfb, uint8_t format,
4209 uint8_t nvt_blk, uint32_t nvt_idx,
4210 bool cam_ignore, uint8_t priority,
4211 uint32_t logic_serv, XiveTCTXMatch *match)
4213 SpaprMachineState *spapr = SPAPR_MACHINE(xfb);
4214 XivePresenter *xptr = XIVE_PRESENTER(spapr->xive);
4215 XivePresenterClass *xpc = XIVE_PRESENTER_GET_CLASS(xptr);
4216 int count;
4218 /* This is a XIVE only operation */
4219 assert(spapr->active_intc == SPAPR_INTC(spapr->xive));
4221 count = xpc->match_nvt(xptr, format, nvt_blk, nvt_idx, cam_ignore,
4222 priority, logic_serv, match);
4223 if (count < 0) {
4224 return count;
4228 * When we implement the save and restore of the thread interrupt
4229 * contexts in the enter/exit CPU handlers of the machine and the
4230 * escalations in QEMU, we should be able to handle non dispatched
4231 * vCPUs.
4233 * Until this is done, the sPAPR machine should find at least one
4234 * matching context always.
4236 if (count == 0) {
4237 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is not dispatched\n",
4238 nvt_blk, nvt_idx);
4241 return count;
4244 int spapr_get_vcpu_id(PowerPCCPU *cpu)
4246 return cpu->vcpu_id;
4249 void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index, Error **errp)
4251 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
4252 MachineState *ms = MACHINE(spapr);
4253 int vcpu_id;
4255 vcpu_id = spapr_vcpu_id(spapr, cpu_index);
4257 if (kvm_enabled() && !kvm_vcpu_id_is_valid(vcpu_id)) {
4258 error_setg(errp, "Can't create CPU with id %d in KVM", vcpu_id);
4259 error_append_hint(errp, "Adjust the number of cpus to %d "
4260 "or try to raise the number of threads per core\n",
4261 vcpu_id * ms->smp.threads / spapr->vsmt);
4262 return;
4265 cpu->vcpu_id = vcpu_id;
4268 PowerPCCPU *spapr_find_cpu(int vcpu_id)
4270 CPUState *cs;
4272 CPU_FOREACH(cs) {
4273 PowerPCCPU *cpu = POWERPC_CPU(cs);
4275 if (spapr_get_vcpu_id(cpu) == vcpu_id) {
4276 return cpu;
4280 return NULL;
4283 static void spapr_cpu_exec_enter(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
4285 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
4287 /* These are only called by TCG, KVM maintains dispatch state */
4289 spapr_cpu->prod = false;
4290 if (spapr_cpu->vpa_addr) {
4291 CPUState *cs = CPU(cpu);
4292 uint32_t dispatch;
4294 dispatch = ldl_be_phys(cs->as,
4295 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER);
4296 dispatch++;
4297 if ((dispatch & 1) != 0) {
4298 qemu_log_mask(LOG_GUEST_ERROR,
4299 "VPA: incorrect dispatch counter value for "
4300 "dispatched partition %u, correcting.\n", dispatch);
4301 dispatch++;
4303 stl_be_phys(cs->as,
4304 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER, dispatch);
4308 static void spapr_cpu_exec_exit(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
4310 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
4312 if (spapr_cpu->vpa_addr) {
4313 CPUState *cs = CPU(cpu);
4314 uint32_t dispatch;
4316 dispatch = ldl_be_phys(cs->as,
4317 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER);
4318 dispatch++;
4319 if ((dispatch & 1) != 1) {
4320 qemu_log_mask(LOG_GUEST_ERROR,
4321 "VPA: incorrect dispatch counter value for "
4322 "preempted partition %u, correcting.\n", dispatch);
4323 dispatch++;
4325 stl_be_phys(cs->as,
4326 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER, dispatch);
4330 static void spapr_machine_class_init(ObjectClass *oc, void *data)
4332 MachineClass *mc = MACHINE_CLASS(oc);
4333 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
4334 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
4335 NMIClass *nc = NMI_CLASS(oc);
4336 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
4337 PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
4338 XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
4339 InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
4340 XiveFabricClass *xfc = XIVE_FABRIC_CLASS(oc);
4342 mc->desc = "pSeries Logical Partition (PAPR compliant)";
4343 mc->ignore_boot_device_suffixes = true;
4346 * We set up the default / latest behaviour here. The class_init
4347 * functions for the specific versioned machine types can override
4348 * these details for backwards compatibility
4350 mc->init = spapr_machine_init;
4351 mc->reset = spapr_machine_reset;
4352 mc->block_default_type = IF_SCSI;
4353 mc->max_cpus = 1024;
4354 mc->no_parallel = 1;
4355 mc->default_boot_order = "";
4356 mc->default_ram_size = 512 * MiB;
4357 mc->default_display = "std";
4358 mc->kvm_type = spapr_kvm_type;
4359 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_SPAPR_PCI_HOST_BRIDGE);
4360 mc->pci_allow_0_address = true;
4361 assert(!mc->get_hotplug_handler);
4362 mc->get_hotplug_handler = spapr_get_hotplug_handler;
4363 hc->pre_plug = spapr_machine_device_pre_plug;
4364 hc->plug = spapr_machine_device_plug;
4365 mc->cpu_index_to_instance_props = spapr_cpu_index_to_props;
4366 mc->get_default_cpu_node_id = spapr_get_default_cpu_node_id;
4367 mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
4368 hc->unplug_request = spapr_machine_device_unplug_request;
4369 hc->unplug = spapr_machine_device_unplug;
4371 smc->dr_lmb_enabled = true;
4372 smc->update_dt_enabled = true;
4373 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power9_v2.0");
4374 mc->has_hotpluggable_cpus = true;
4375 smc->resize_hpt_default = SPAPR_RESIZE_HPT_ENABLED;
4376 fwc->get_dev_path = spapr_get_fw_dev_path;
4377 nc->nmi_monitor_handler = spapr_nmi;
4378 smc->phb_placement = spapr_phb_placement;
4379 vhc->hypercall = emulate_spapr_hypercall;
4380 vhc->hpt_mask = spapr_hpt_mask;
4381 vhc->map_hptes = spapr_map_hptes;
4382 vhc->unmap_hptes = spapr_unmap_hptes;
4383 vhc->hpte_set_c = spapr_hpte_set_c;
4384 vhc->hpte_set_r = spapr_hpte_set_r;
4385 vhc->get_pate = spapr_get_pate;
4386 vhc->encode_hpt_for_kvm_pr = spapr_encode_hpt_for_kvm_pr;
4387 vhc->cpu_exec_enter = spapr_cpu_exec_enter;
4388 vhc->cpu_exec_exit = spapr_cpu_exec_exit;
4389 xic->ics_get = spapr_ics_get;
4390 xic->ics_resend = spapr_ics_resend;
4391 xic->icp_get = spapr_icp_get;
4392 ispc->print_info = spapr_pic_print_info;
4393 /* Force NUMA node memory size to be a multiple of
4394 * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
4395 * in which LMBs are represented and hot-added
4397 mc->numa_mem_align_shift = 28;
4398 mc->numa_mem_supported = true;
4399 mc->auto_enable_numa = true;
4401 smc->default_caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
4402 smc->default_caps.caps[SPAPR_CAP_VSX] = SPAPR_CAP_ON;
4403 smc->default_caps.caps[SPAPR_CAP_DFP] = SPAPR_CAP_ON;
4404 smc->default_caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_WORKAROUND;
4405 smc->default_caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_WORKAROUND;
4406 smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_WORKAROUND;
4407 smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 16; /* 64kiB */
4408 smc->default_caps.caps[SPAPR_CAP_NESTED_KVM_HV] = SPAPR_CAP_OFF;
4409 smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = SPAPR_CAP_ON;
4410 smc->default_caps.caps[SPAPR_CAP_CCF_ASSIST] = SPAPR_CAP_OFF;
4411 spapr_caps_add_properties(smc, &error_abort);
4412 smc->irq = &spapr_irq_dual;
4413 smc->dr_phb_enabled = true;
4414 smc->linux_pci_probe = true;
4415 smc->smp_threads_vsmt = true;
4416 smc->nr_xirqs = SPAPR_NR_XIRQS;
4417 xfc->match_nvt = spapr_match_nvt;
4420 static const TypeInfo spapr_machine_info = {
4421 .name = TYPE_SPAPR_MACHINE,
4422 .parent = TYPE_MACHINE,
4423 .abstract = true,
4424 .instance_size = sizeof(SpaprMachineState),
4425 .instance_init = spapr_instance_init,
4426 .instance_finalize = spapr_machine_finalizefn,
4427 .class_size = sizeof(SpaprMachineClass),
4428 .class_init = spapr_machine_class_init,
4429 .interfaces = (InterfaceInfo[]) {
4430 { TYPE_FW_PATH_PROVIDER },
4431 { TYPE_NMI },
4432 { TYPE_HOTPLUG_HANDLER },
4433 { TYPE_PPC_VIRTUAL_HYPERVISOR },
4434 { TYPE_XICS_FABRIC },
4435 { TYPE_INTERRUPT_STATS_PROVIDER },
4436 { TYPE_XIVE_FABRIC },
4441 #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest) \
4442 static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
4443 void *data) \
4445 MachineClass *mc = MACHINE_CLASS(oc); \
4446 spapr_machine_##suffix##_class_options(mc); \
4447 if (latest) { \
4448 mc->alias = "pseries"; \
4449 mc->is_default = 1; \
4452 static const TypeInfo spapr_machine_##suffix##_info = { \
4453 .name = MACHINE_TYPE_NAME("pseries-" verstr), \
4454 .parent = TYPE_SPAPR_MACHINE, \
4455 .class_init = spapr_machine_##suffix##_class_init, \
4456 }; \
4457 static void spapr_machine_register_##suffix(void) \
4459 type_register(&spapr_machine_##suffix##_info); \
4461 type_init(spapr_machine_register_##suffix)
4464 * pseries-5.0
4466 static void spapr_machine_5_0_class_options(MachineClass *mc)
4468 /* Defaults for the latest behaviour inherited from the base class */
4471 DEFINE_SPAPR_MACHINE(5_0, "5.0", true);
4474 * pseries-4.2
4476 static void spapr_machine_4_2_class_options(MachineClass *mc)
4478 spapr_machine_5_0_class_options(mc);
4479 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
4482 DEFINE_SPAPR_MACHINE(4_2, "4.2", false);
4485 * pseries-4.1
4487 static void spapr_machine_4_1_class_options(MachineClass *mc)
4489 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4490 static GlobalProperty compat[] = {
4491 /* Only allow 4kiB and 64kiB IOMMU pagesizes */
4492 { TYPE_SPAPR_PCI_HOST_BRIDGE, "pgsz", "0x11000" },
4495 spapr_machine_4_2_class_options(mc);
4496 smc->linux_pci_probe = false;
4497 smc->smp_threads_vsmt = false;
4498 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
4499 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4502 DEFINE_SPAPR_MACHINE(4_1, "4.1", false);
4505 * pseries-4.0
4507 static void phb_placement_4_0(SpaprMachineState *spapr, uint32_t index,
4508 uint64_t *buid, hwaddr *pio,
4509 hwaddr *mmio32, hwaddr *mmio64,
4510 unsigned n_dma, uint32_t *liobns,
4511 hwaddr *nv2gpa, hwaddr *nv2atsd, Error **errp)
4513 spapr_phb_placement(spapr, index, buid, pio, mmio32, mmio64, n_dma, liobns,
4514 nv2gpa, nv2atsd, errp);
4515 *nv2gpa = 0;
4516 *nv2atsd = 0;
4519 static void spapr_machine_4_0_class_options(MachineClass *mc)
4521 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4523 spapr_machine_4_1_class_options(mc);
4524 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
4525 smc->phb_placement = phb_placement_4_0;
4526 smc->irq = &spapr_irq_xics;
4527 smc->pre_4_1_migration = true;
4530 DEFINE_SPAPR_MACHINE(4_0, "4.0", false);
4533 * pseries-3.1
4535 static void spapr_machine_3_1_class_options(MachineClass *mc)
4537 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4539 spapr_machine_4_0_class_options(mc);
4540 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
4542 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power8_v2.0");
4543 smc->update_dt_enabled = false;
4544 smc->dr_phb_enabled = false;
4545 smc->broken_host_serial_model = true;
4546 smc->default_caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_BROKEN;
4547 smc->default_caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_BROKEN;
4548 smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
4549 smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = SPAPR_CAP_OFF;
4552 DEFINE_SPAPR_MACHINE(3_1, "3.1", false);
4555 * pseries-3.0
4558 static void spapr_machine_3_0_class_options(MachineClass *mc)
4560 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4562 spapr_machine_3_1_class_options(mc);
4563 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
4565 smc->legacy_irq_allocation = true;
4566 smc->nr_xirqs = 0x400;
4567 smc->irq = &spapr_irq_xics_legacy;
4570 DEFINE_SPAPR_MACHINE(3_0, "3.0", false);
4573 * pseries-2.12
4575 static void spapr_machine_2_12_class_options(MachineClass *mc)
4577 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4578 static GlobalProperty compat[] = {
4579 { TYPE_POWERPC_CPU, "pre-3.0-migration", "on" },
4580 { TYPE_SPAPR_CPU_CORE, "pre-3.0-migration", "on" },
4583 spapr_machine_3_0_class_options(mc);
4584 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
4585 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4587 /* We depend on kvm_enabled() to choose a default value for the
4588 * hpt-max-page-size capability. Of course we can't do it here
4589 * because this is too early and the HW accelerator isn't initialzed
4590 * yet. Postpone this to machine init (see default_caps_with_cpu()).
4592 smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 0;
4595 DEFINE_SPAPR_MACHINE(2_12, "2.12", false);
4597 static void spapr_machine_2_12_sxxm_class_options(MachineClass *mc)
4599 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4601 spapr_machine_2_12_class_options(mc);
4602 smc->default_caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_WORKAROUND;
4603 smc->default_caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_WORKAROUND;
4604 smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_FIXED_CCD;
4607 DEFINE_SPAPR_MACHINE(2_12_sxxm, "2.12-sxxm", false);
4610 * pseries-2.11
4613 static void spapr_machine_2_11_class_options(MachineClass *mc)
4615 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4617 spapr_machine_2_12_class_options(mc);
4618 smc->default_caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_ON;
4619 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
4622 DEFINE_SPAPR_MACHINE(2_11, "2.11", false);
4625 * pseries-2.10
4628 static void spapr_machine_2_10_class_options(MachineClass *mc)
4630 spapr_machine_2_11_class_options(mc);
4631 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
4634 DEFINE_SPAPR_MACHINE(2_10, "2.10", false);
4637 * pseries-2.9
4640 static void spapr_machine_2_9_class_options(MachineClass *mc)
4642 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4643 static GlobalProperty compat[] = {
4644 { TYPE_POWERPC_CPU, "pre-2.10-migration", "on" },
4647 spapr_machine_2_10_class_options(mc);
4648 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
4649 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4650 mc->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
4651 smc->pre_2_10_has_unused_icps = true;
4652 smc->resize_hpt_default = SPAPR_RESIZE_HPT_DISABLED;
4655 DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
4658 * pseries-2.8
4661 static void spapr_machine_2_8_class_options(MachineClass *mc)
4663 static GlobalProperty compat[] = {
4664 { TYPE_SPAPR_PCI_HOST_BRIDGE, "pcie-extended-configuration-space", "off" },
4667 spapr_machine_2_9_class_options(mc);
4668 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
4669 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4670 mc->numa_mem_align_shift = 23;
4673 DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
4676 * pseries-2.7
4679 static void phb_placement_2_7(SpaprMachineState *spapr, uint32_t index,
4680 uint64_t *buid, hwaddr *pio,
4681 hwaddr *mmio32, hwaddr *mmio64,
4682 unsigned n_dma, uint32_t *liobns,
4683 hwaddr *nv2gpa, hwaddr *nv2atsd, Error **errp)
4685 /* Legacy PHB placement for pseries-2.7 and earlier machine types */
4686 const uint64_t base_buid = 0x800000020000000ULL;
4687 const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
4688 const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
4689 const hwaddr pio_offset = 0x80000000; /* 2 GiB */
4690 const uint32_t max_index = 255;
4691 const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
4693 uint64_t ram_top = MACHINE(spapr)->ram_size;
4694 hwaddr phb0_base, phb_base;
4695 int i;
4697 /* Do we have device memory? */
4698 if (MACHINE(spapr)->maxram_size > ram_top) {
4699 /* Can't just use maxram_size, because there may be an
4700 * alignment gap between normal and device memory regions
4702 ram_top = MACHINE(spapr)->device_memory->base +
4703 memory_region_size(&MACHINE(spapr)->device_memory->mr);
4706 phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
4708 if (index > max_index) {
4709 error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
4710 max_index);
4711 return;
4714 *buid = base_buid + index;
4715 for (i = 0; i < n_dma; ++i) {
4716 liobns[i] = SPAPR_PCI_LIOBN(index, i);
4719 phb_base = phb0_base + index * phb_spacing;
4720 *pio = phb_base + pio_offset;
4721 *mmio32 = phb_base + mmio_offset;
4723 * We don't set the 64-bit MMIO window, relying on the PHB's
4724 * fallback behaviour of automatically splitting a large "32-bit"
4725 * window into contiguous 32-bit and 64-bit windows
4728 *nv2gpa = 0;
4729 *nv2atsd = 0;
4732 static void spapr_machine_2_7_class_options(MachineClass *mc)
4734 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4735 static GlobalProperty compat[] = {
4736 { TYPE_SPAPR_PCI_HOST_BRIDGE, "mem_win_size", "0xf80000000", },
4737 { TYPE_SPAPR_PCI_HOST_BRIDGE, "mem64_win_size", "0", },
4738 { TYPE_POWERPC_CPU, "pre-2.8-migration", "on", },
4739 { TYPE_SPAPR_PCI_HOST_BRIDGE, "pre-2.8-migration", "on", },
4742 spapr_machine_2_8_class_options(mc);
4743 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power7_v2.3");
4744 mc->default_machine_opts = "modern-hotplug-events=off";
4745 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
4746 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4747 smc->phb_placement = phb_placement_2_7;
4750 DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
4753 * pseries-2.6
4756 static void spapr_machine_2_6_class_options(MachineClass *mc)
4758 static GlobalProperty compat[] = {
4759 { TYPE_SPAPR_PCI_HOST_BRIDGE, "ddw", "off" },
4762 spapr_machine_2_7_class_options(mc);
4763 mc->has_hotpluggable_cpus = false;
4764 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
4765 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4768 DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
4771 * pseries-2.5
4774 static void spapr_machine_2_5_class_options(MachineClass *mc)
4776 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4777 static GlobalProperty compat[] = {
4778 { "spapr-vlan", "use-rx-buffer-pools", "off" },
4781 spapr_machine_2_6_class_options(mc);
4782 smc->use_ohci_by_default = true;
4783 compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len);
4784 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4787 DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
4790 * pseries-2.4
4793 static void spapr_machine_2_4_class_options(MachineClass *mc)
4795 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4797 spapr_machine_2_5_class_options(mc);
4798 smc->dr_lmb_enabled = false;
4799 compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len);
4802 DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
4805 * pseries-2.3
4808 static void spapr_machine_2_3_class_options(MachineClass *mc)
4810 static GlobalProperty compat[] = {
4811 { "spapr-pci-host-bridge", "dynamic-reconfiguration", "off" },
4813 spapr_machine_2_4_class_options(mc);
4814 compat_props_add(mc->compat_props, hw_compat_2_3, hw_compat_2_3_len);
4815 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4817 DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
4820 * pseries-2.2
4823 static void spapr_machine_2_2_class_options(MachineClass *mc)
4825 static GlobalProperty compat[] = {
4826 { TYPE_SPAPR_PCI_HOST_BRIDGE, "mem_win_size", "0x20000000" },
4829 spapr_machine_2_3_class_options(mc);
4830 compat_props_add(mc->compat_props, hw_compat_2_2, hw_compat_2_2_len);
4831 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4832 mc->default_machine_opts = "modern-hotplug-events=off,suppress-vmdesc=on";
4834 DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
4837 * pseries-2.1
4840 static void spapr_machine_2_1_class_options(MachineClass *mc)
4842 spapr_machine_2_2_class_options(mc);
4843 compat_props_add(mc->compat_props, hw_compat_2_1, hw_compat_2_1_len);
4845 DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
4847 static void spapr_machine_register_types(void)
4849 type_register_static(&spapr_machine_info);
4852 type_init(spapr_machine_register_types)