]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr.c
pseries: Consolidate construction of /rtas device tree node
[mirror_qemu.git] / hw / ppc / spapr.c
CommitLineData
9fdf0c29
DG
1/*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3 *
4 * Copyright (c) 2004-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 * Copyright (c) 2010 David Gibson, IBM Corporation.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 */
0d75590d 27#include "qemu/osdep.h"
da34e65c 28#include "qapi/error.h"
9c17d615 29#include "sysemu/sysemu.h"
e35704ba 30#include "sysemu/numa.h"
83c9f4ca 31#include "hw/hw.h"
03dd024f 32#include "qemu/log.h"
71461b0f 33#include "hw/fw-path-provider.h"
9fdf0c29 34#include "elf.h"
1422e32d 35#include "net/net.h"
ad440b4a 36#include "sysemu/device_tree.h"
fa1d36df 37#include "sysemu/block-backend.h"
9c17d615
PB
38#include "sysemu/cpus.h"
39#include "sysemu/kvm.h"
c20d332a 40#include "sysemu/device_tree.h"
e97c3636 41#include "kvm_ppc.h"
ff14e817 42#include "migration/migration.h"
4be21d56 43#include "mmu-hash64.h"
3794d548 44#include "qom/cpu.h"
9fdf0c29
DG
45
46#include "hw/boards.h"
0d09e41a 47#include "hw/ppc/ppc.h"
9fdf0c29
DG
48#include "hw/loader.h"
49
7804c353 50#include "hw/ppc/fdt.h"
0d09e41a
PB
51#include "hw/ppc/spapr.h"
52#include "hw/ppc/spapr_vio.h"
53#include "hw/pci-host/spapr.h"
54#include "hw/ppc/xics.h"
a2cb15b0 55#include "hw/pci/msi.h"
9fdf0c29 56
83c9f4ca 57#include "hw/pci/pci.h"
71461b0f
AK
58#include "hw/scsi/scsi.h"
59#include "hw/virtio/virtio-scsi.h"
f61b4bed 60
022c62cb 61#include "exec/address-spaces.h"
35139a59 62#include "hw/usb.h"
1de7afc9 63#include "qemu/config-file.h"
135a129a 64#include "qemu/error-report.h"
2a6593cb 65#include "trace.h"
34316482 66#include "hw/nmi.h"
890c2b77 67
68a27b20 68#include "hw/compat.h"
f348b6d1 69#include "qemu/cutils.h"
94a94e4c 70#include "hw/ppc/spapr_cpu_core.h"
2474bfd4 71#include "qmp-commands.h"
68a27b20 72
9fdf0c29
DG
73#include <libfdt.h>
74
4d8d5467
BH
75/* SLOF memory layout:
76 *
77 * SLOF raw image loaded at 0, copies its romfs right below the flat
78 * device-tree, then position SLOF itself 31M below that
79 *
80 * So we set FW_OVERHEAD to 40MB which should account for all of that
81 * and more
82 *
83 * We load our kernel at 4M, leaving space for SLOF initial image
84 */
38b02bd8 85#define FDT_MAX_SIZE 0x100000
39ac8455 86#define RTAS_MAX_SIZE 0x10000
b7d1f77a 87#define RTAS_MAX_ADDR 0x80000000 /* RTAS must stay below that */
a9f8ad8f
DG
88#define FW_MAX_SIZE 0x400000
89#define FW_FILE_NAME "slof.bin"
4d8d5467
BH
90#define FW_OVERHEAD 0x2800000
91#define KERNEL_LOAD_ADDR FW_MAX_SIZE
a9f8ad8f 92
4d8d5467 93#define MIN_RMA_SLOF 128UL
9fdf0c29 94
0c103f8e
DG
95#define PHANDLE_XICP 0x00001111
96
7f763a5d
DG
97#define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift))
98
c04d6cfa 99static XICSState *try_create_xics(const char *type, int nr_servers,
34f2af3d 100 int nr_irqs, Error **errp)
c04d6cfa 101{
34f2af3d 102 Error *err = NULL;
c04d6cfa
AL
103 DeviceState *dev;
104
105 dev = qdev_create(NULL, type);
106 qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
107 qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
34f2af3d
MA
108 object_property_set_bool(OBJECT(dev), true, "realized", &err);
109 if (err) {
110 error_propagate(errp, err);
111 object_unparent(OBJECT(dev));
c04d6cfa
AL
112 return NULL;
113 }
5a3d7b23 114 return XICS_COMMON(dev);
c04d6cfa
AL
115}
116
446f16a6 117static XICSState *xics_system_init(MachineState *machine,
1e49182d 118 int nr_servers, int nr_irqs, Error **errp)
c04d6cfa 119{
27f24582 120 XICSState *xics = NULL;
c04d6cfa 121
11ad93f6 122 if (kvm_enabled()) {
34f2af3d
MA
123 Error *err = NULL;
124
446f16a6 125 if (machine_kernel_irqchip_allowed(machine)) {
27f24582
BH
126 xics = try_create_xics(TYPE_XICS_SPAPR_KVM, nr_servers, nr_irqs,
127 &err);
11ad93f6 128 }
27f24582 129 if (machine_kernel_irqchip_required(machine) && !xics) {
b83baa60
MA
130 error_reportf_err(err,
131 "kernel_irqchip requested but unavailable: ");
132 } else {
133 error_free(err);
11ad93f6
DG
134 }
135 }
136
27f24582
BH
137 if (!xics) {
138 xics = try_create_xics(TYPE_XICS_SPAPR, nr_servers, nr_irqs, errp);
c04d6cfa
AL
139 }
140
27f24582 141 return xics;
c04d6cfa
AL
142}
143
833d4668
AK
144static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
145 int smt_threads)
146{
147 int i, ret = 0;
148 uint32_t servers_prop[smt_threads];
149 uint32_t gservers_prop[smt_threads * 2];
150 int index = ppc_get_vcpu_dt_id(cpu);
151
6d9412ea 152 if (cpu->cpu_version) {
4bce526e 153 ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version);
6d9412ea
AK
154 if (ret < 0) {
155 return ret;
156 }
157 }
158
833d4668
AK
159 /* Build interrupt servers and gservers properties */
160 for (i = 0; i < smt_threads; i++) {
161 servers_prop[i] = cpu_to_be32(index + i);
162 /* Hack, direct the group queues back to cpu 0 */
163 gservers_prop[i*2] = cpu_to_be32(index + i);
164 gservers_prop[i*2 + 1] = 0;
165 }
166 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
167 servers_prop, sizeof(servers_prop));
168 if (ret < 0) {
169 return ret;
170 }
171 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
172 gservers_prop, sizeof(gservers_prop));
173
174 return ret;
175}
176
0da6f3fe
BR
177static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
178{
179 int ret = 0;
180 PowerPCCPU *cpu = POWERPC_CPU(cs);
181 int index = ppc_get_vcpu_dt_id(cpu);
182 uint32_t associativity[] = {cpu_to_be32(0x5),
183 cpu_to_be32(0x0),
184 cpu_to_be32(0x0),
185 cpu_to_be32(0x0),
186 cpu_to_be32(cs->numa_node),
187 cpu_to_be32(index)};
188
189 /* Advertise NUMA via ibm,associativity */
190 if (nb_numa_nodes > 1) {
191 ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
192 sizeof(associativity));
193 }
194
195 return ret;
196}
197
28e02042 198static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
6e806cc3 199{
82677ed2
AK
200 int ret = 0, offset, cpus_offset;
201 CPUState *cs;
6e806cc3
BR
202 char cpu_model[32];
203 int smt = kvmppc_smt_threads();
7f763a5d 204 uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
6e806cc3 205
82677ed2
AK
206 CPU_FOREACH(cs) {
207 PowerPCCPU *cpu = POWERPC_CPU(cs);
208 DeviceClass *dc = DEVICE_GET_CLASS(cs);
209 int index = ppc_get_vcpu_dt_id(cpu);
6e806cc3 210
0f20ba62 211 if ((index % smt) != 0) {
6e806cc3
BR
212 continue;
213 }
214
82677ed2 215 snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
6e806cc3 216
82677ed2
AK
217 cpus_offset = fdt_path_offset(fdt, "/cpus");
218 if (cpus_offset < 0) {
219 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
220 "cpus");
221 if (cpus_offset < 0) {
222 return cpus_offset;
223 }
224 }
225 offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
6e806cc3 226 if (offset < 0) {
82677ed2
AK
227 offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
228 if (offset < 0) {
229 return offset;
230 }
6e806cc3
BR
231 }
232
7f763a5d
DG
233 ret = fdt_setprop(fdt, offset, "ibm,pft-size",
234 pft_size_prop, sizeof(pft_size_prop));
6e806cc3
BR
235 if (ret < 0) {
236 return ret;
237 }
833d4668 238
0da6f3fe
BR
239 ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
240 if (ret < 0) {
241 return ret;
242 }
243
82677ed2 244 ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
2a48d993 245 ppc_get_compat_smt_threads(cpu));
833d4668
AK
246 if (ret < 0) {
247 return ret;
248 }
6e806cc3
BR
249 }
250 return ret;
251}
252
b082d65a
AK
253static hwaddr spapr_node0_size(void)
254{
fb164994
DG
255 MachineState *machine = MACHINE(qdev_get_machine());
256
b082d65a
AK
257 if (nb_numa_nodes) {
258 int i;
259 for (i = 0; i < nb_numa_nodes; ++i) {
260 if (numa_info[i].node_mem) {
fb164994
DG
261 return MIN(pow2floor(numa_info[i].node_mem),
262 machine->ram_size);
b082d65a
AK
263 }
264 }
265 }
fb164994 266 return machine->ram_size;
b082d65a
AK
267}
268
a1d59c0f
AK
269static void add_str(GString *s, const gchar *s1)
270{
271 g_string_append_len(s, s1, strlen(s1) + 1);
272}
7f763a5d 273
a19f7fb0 274static void *spapr_create_fdt_skel(sPAPRMachineState *spapr)
9fdf0c29
DG
275{
276 void *fdt;
ef951443 277 char *buf;
9fdf0c29 278
7267c094 279 fdt = g_malloc0(FDT_MAX_SIZE);
9fdf0c29
DG
280 _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
281
282 _FDT((fdt_finish_reservemap(fdt)));
283
284 /* Root node */
285 _FDT((fdt_begin_node(fdt, "")));
286 _FDT((fdt_property_string(fdt, "device_type", "chrp")));
5d73dd66 287 _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
d63919c9 288 _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
9fdf0c29 289
ef951443
ND
290 /*
291 * Add info to guest to indentify which host is it being run on
292 * and what is the uuid of the guest
293 */
294 if (kvmppc_get_host_model(&buf)) {
295 _FDT((fdt_property_string(fdt, "host-model", buf)));
296 g_free(buf);
297 }
298 if (kvmppc_get_host_serial(&buf)) {
299 _FDT((fdt_property_string(fdt, "host-serial", buf)));
300 g_free(buf);
301 }
302
9c5ce8db 303 buf = qemu_uuid_unparse_strdup(&qemu_uuid);
ef951443
ND
304
305 _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
3dc0a66d
AK
306 if (qemu_uuid_set) {
307 _FDT((fdt_property_string(fdt, "system-id", buf)));
308 }
ef951443
ND
309 g_free(buf);
310
2c1aaa81
SB
311 if (qemu_get_vm_name()) {
312 _FDT((fdt_property_string(fdt, "ibm,partition-name",
313 qemu_get_vm_name())));
314 }
315
9fdf0c29
DG
316 _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
317 _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
318
4040ab72
DG
319 /* vdevice */
320 _FDT((fdt_begin_node(fdt, "vdevice")));
321
322 _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
323 _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
324 _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
325 _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
b5cec4c5
DG
326 _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
327 _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
4040ab72
DG
328
329 _FDT((fdt_end_node(fdt)));
330
74d042e5 331 /* event-sources */
a19f7fb0 332 spapr_events_fdt_skel(fdt, spapr->check_exception_irq);
74d042e5 333
f7d69146
AG
334 /* /hypervisor node */
335 if (kvm_enabled()) {
336 uint8_t hypercall[16];
337
338 /* indicate KVM hypercall interface */
339 _FDT((fdt_begin_node(fdt, "hypervisor")));
340 _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
341 if (kvmppc_has_cap_fixup_hcalls()) {
342 /*
343 * Older KVM versions with older guest kernels were broken with the
344 * magic page, don't allow the guest to map it.
345 */
0ddbd053
AK
346 if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
347 sizeof(hypercall))) {
348 _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
349 sizeof(hypercall))));
350 }
f7d69146
AG
351 }
352 _FDT((fdt_end_node(fdt)));
353 }
354
9fdf0c29
DG
355 _FDT((fdt_end_node(fdt))); /* close root node */
356 _FDT((fdt_finish(fdt)));
357
a3467baa
DG
358 return fdt;
359}
360
03d196b7 361static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
26a8c353
AK
362 hwaddr size)
363{
364 uint32_t associativity[] = {
365 cpu_to_be32(0x4), /* length */
366 cpu_to_be32(0x0), cpu_to_be32(0x0),
c3b4f589 367 cpu_to_be32(0x0), cpu_to_be32(nodeid)
26a8c353
AK
368 };
369 char mem_name[32];
370 uint64_t mem_reg_property[2];
371 int off;
372
373 mem_reg_property[0] = cpu_to_be64(start);
374 mem_reg_property[1] = cpu_to_be64(size);
375
376 sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
377 off = fdt_add_subnode(fdt, 0, mem_name);
378 _FDT(off);
379 _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
380 _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
381 sizeof(mem_reg_property))));
382 _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
383 sizeof(associativity))));
03d196b7 384 return off;
26a8c353
AK
385}
386
28e02042 387static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
7f763a5d 388{
fb164994 389 MachineState *machine = MACHINE(spapr);
7db8a127
AK
390 hwaddr mem_start, node_size;
391 int i, nb_nodes = nb_numa_nodes;
392 NodeInfo *nodes = numa_info;
393 NodeInfo ramnode;
394
395 /* No NUMA nodes, assume there is just one node with whole RAM */
396 if (!nb_numa_nodes) {
397 nb_nodes = 1;
fb164994 398 ramnode.node_mem = machine->ram_size;
7db8a127 399 nodes = &ramnode;
5fe269b1 400 }
7f763a5d 401
7db8a127
AK
402 for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
403 if (!nodes[i].node_mem) {
404 continue;
405 }
fb164994 406 if (mem_start >= machine->ram_size) {
5fe269b1
PM
407 node_size = 0;
408 } else {
7db8a127 409 node_size = nodes[i].node_mem;
fb164994
DG
410 if (node_size > machine->ram_size - mem_start) {
411 node_size = machine->ram_size - mem_start;
5fe269b1
PM
412 }
413 }
7db8a127
AK
414 if (!mem_start) {
415 /* ppc_spapr_init() checks for rma_size <= node0_size already */
e8f986fc 416 spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
7db8a127
AK
417 mem_start += spapr->rma_size;
418 node_size -= spapr->rma_size;
419 }
6010818c
AK
420 for ( ; node_size; ) {
421 hwaddr sizetmp = pow2floor(node_size);
422
423 /* mem_start != 0 here */
424 if (ctzl(mem_start) < ctzl(sizetmp)) {
425 sizetmp = 1ULL << ctzl(mem_start);
426 }
427
428 spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
429 node_size -= sizetmp;
430 mem_start += sizetmp;
431 }
7f763a5d
DG
432 }
433
434 return 0;
435}
436
230bf719
TH
437/* Populate the "ibm,pa-features" property */
438static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset)
439{
440 uint8_t pa_features_206[] = { 6, 0,
441 0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
442 uint8_t pa_features_207[] = { 24, 0,
443 0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
444 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
445 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
bac3bf28 446 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
230bf719
TH
447 uint8_t *pa_features;
448 size_t pa_size;
449
4cbec30d
TH
450 switch (env->mmu_model) {
451 case POWERPC_MMU_2_06:
452 case POWERPC_MMU_2_06a:
230bf719
TH
453 pa_features = pa_features_206;
454 pa_size = sizeof(pa_features_206);
4cbec30d
TH
455 break;
456 case POWERPC_MMU_2_07:
457 case POWERPC_MMU_2_07a:
230bf719
TH
458 pa_features = pa_features_207;
459 pa_size = sizeof(pa_features_207);
4cbec30d
TH
460 break;
461 default:
462 return;
230bf719
TH
463 }
464
465 if (env->ci_large_pages) {
466 /*
467 * Note: we keep CI large pages off by default because a 64K capable
468 * guest provisioned with large pages might otherwise try to map a qemu
469 * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
470 * even if that qemu runs on a 4k host.
471 * We dd this bit back here if we are confident this is not an issue
472 */
473 pa_features[3] |= 0x20;
474 }
bac3bf28
TH
475 if (kvmppc_has_cap_htm() && pa_size > 24) {
476 pa_features[24] |= 0x80; /* Transactional memory support */
477 }
230bf719
TH
478
479 _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
480}
481
0da6f3fe
BR
482static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
483 sPAPRMachineState *spapr)
484{
485 PowerPCCPU *cpu = POWERPC_CPU(cs);
486 CPUPPCState *env = &cpu->env;
487 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
488 int index = ppc_get_vcpu_dt_id(cpu);
489 uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
490 0xffffffff, 0xffffffff};
afd10a0f
BR
491 uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
492 : SPAPR_TIMEBASE_FREQ;
0da6f3fe
BR
493 uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
494 uint32_t page_sizes_prop[64];
495 size_t page_sizes_prop_size;
22419c2a 496 uint32_t vcpus_per_socket = smp_threads * smp_cores;
0da6f3fe 497 uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
af81cf32
BR
498 sPAPRDRConnector *drc;
499 sPAPRDRConnectorClass *drck;
500 int drc_index;
501
502 drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
503 if (drc) {
504 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
505 drc_index = drck->get_index(drc);
506 _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
507 }
0da6f3fe
BR
508
509 _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
510 _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
511
512 _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
513 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
514 env->dcache_line_size)));
515 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
516 env->dcache_line_size)));
517 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
518 env->icache_line_size)));
519 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
520 env->icache_line_size)));
521
522 if (pcc->l1_dcache_size) {
523 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
524 pcc->l1_dcache_size)));
525 } else {
ce9863b7 526 error_report("Warning: Unknown L1 dcache size for cpu");
0da6f3fe
BR
527 }
528 if (pcc->l1_icache_size) {
529 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
530 pcc->l1_icache_size)));
531 } else {
ce9863b7 532 error_report("Warning: Unknown L1 icache size for cpu");
0da6f3fe
BR
533 }
534
535 _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
536 _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
fd5da5c4 537 _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
0da6f3fe
BR
538 _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
539 _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
540 _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
541
542 if (env->spr_cb[SPR_PURR].oea_read) {
543 _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
544 }
545
546 if (env->mmu_model & POWERPC_MMU_1TSEG) {
547 _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
548 segs, sizeof(segs))));
549 }
550
551 /* Advertise VMX/VSX (vector extensions) if available
552 * 0 / no property == no vector extensions
553 * 1 == VMX / Altivec available
554 * 2 == VSX available */
555 if (env->insns_flags & PPC_ALTIVEC) {
556 uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
557
558 _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
559 }
560
561 /* Advertise DFP (Decimal Floating Point) if available
562 * 0 / no property == no DFP
563 * 1 == DFP available */
564 if (env->insns_flags2 & PPC2_DFP) {
565 _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
566 }
567
3654fa95 568 page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
0da6f3fe
BR
569 sizeof(page_sizes_prop));
570 if (page_sizes_prop_size) {
571 _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
572 page_sizes_prop, page_sizes_prop_size)));
573 }
574
230bf719 575 spapr_populate_pa_features(env, fdt, offset);
90da0d5a 576
0da6f3fe 577 _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
22419c2a 578 cs->cpu_index / vcpus_per_socket)));
0da6f3fe
BR
579
580 _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
581 pft_size_prop, sizeof(pft_size_prop))));
582
583 _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
584
585 _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
586 ppc_get_compat_smt_threads(cpu)));
587}
588
589static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
590{
591 CPUState *cs;
592 int cpus_offset;
593 char *nodename;
594 int smt = kvmppc_smt_threads();
595
596 cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
597 _FDT(cpus_offset);
598 _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
599 _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
600
601 /*
602 * We walk the CPUs in reverse order to ensure that CPU DT nodes
603 * created by fdt_add_subnode() end up in the right order in FDT
604 * for the guest kernel the enumerate the CPUs correctly.
605 */
606 CPU_FOREACH_REVERSE(cs) {
607 PowerPCCPU *cpu = POWERPC_CPU(cs);
608 int index = ppc_get_vcpu_dt_id(cpu);
609 DeviceClass *dc = DEVICE_GET_CLASS(cs);
610 int offset;
611
612 if ((index % smt) != 0) {
613 continue;
614 }
615
616 nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
617 offset = fdt_add_subnode(fdt, cpus_offset, nodename);
618 g_free(nodename);
619 _FDT(offset);
620 spapr_populate_cpu_dt(cs, fdt, offset, spapr);
621 }
622
623}
624
03d196b7
BR
625/*
626 * Adds ibm,dynamic-reconfiguration-memory node.
627 * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
628 * of this device tree node.
629 */
630static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
631{
632 MachineState *machine = MACHINE(spapr);
633 int ret, i, offset;
634 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
635 uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
d0e5a8f2
BR
636 uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
637 uint32_t nr_lmbs = (spapr->hotplug_memory.base +
638 memory_region_size(&spapr->hotplug_memory.mr)) /
639 lmb_size;
03d196b7 640 uint32_t *int_buf, *cur_index, buf_len;
6663864e 641 int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
03d196b7 642
16c25aef 643 /*
d0e5a8f2 644 * Don't create the node if there is no hotpluggable memory
16c25aef 645 */
d0e5a8f2 646 if (machine->ram_size == machine->maxram_size) {
16c25aef
BR
647 return 0;
648 }
649
ef001f06
TH
650 /*
651 * Allocate enough buffer size to fit in ibm,dynamic-memory
652 * or ibm,associativity-lookup-arrays
653 */
654 buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
655 * sizeof(uint32_t);
03d196b7
BR
656 cur_index = int_buf = g_malloc0(buf_len);
657
658 offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
659
660 ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
661 sizeof(prop_lmb_size));
662 if (ret < 0) {
663 goto out;
664 }
665
666 ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
667 if (ret < 0) {
668 goto out;
669 }
670
671 ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
672 if (ret < 0) {
673 goto out;
674 }
675
676 /* ibm,dynamic-memory */
677 int_buf[0] = cpu_to_be32(nr_lmbs);
678 cur_index++;
679 for (i = 0; i < nr_lmbs; i++) {
d0e5a8f2 680 uint64_t addr = i * lmb_size;
03d196b7
BR
681 uint32_t *dynamic_memory = cur_index;
682
d0e5a8f2
BR
683 if (i >= hotplug_lmb_start) {
684 sPAPRDRConnector *drc;
685 sPAPRDRConnectorClass *drck;
686
687 drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
688 g_assert(drc);
689 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
690
691 dynamic_memory[0] = cpu_to_be32(addr >> 32);
692 dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
693 dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
694 dynamic_memory[3] = cpu_to_be32(0); /* reserved */
695 dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
696 if (memory_region_present(get_system_memory(), addr)) {
697 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
698 } else {
699 dynamic_memory[5] = cpu_to_be32(0);
700 }
03d196b7 701 } else {
d0e5a8f2
BR
702 /*
703 * LMB information for RMA, boot time RAM and gap b/n RAM and
704 * hotplug memory region -- all these are marked as reserved
705 * and as having no valid DRC.
706 */
707 dynamic_memory[0] = cpu_to_be32(addr >> 32);
708 dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
709 dynamic_memory[2] = cpu_to_be32(0);
710 dynamic_memory[3] = cpu_to_be32(0); /* reserved */
711 dynamic_memory[4] = cpu_to_be32(-1);
712 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
713 SPAPR_LMB_FLAGS_DRC_INVALID);
03d196b7
BR
714 }
715
716 cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
717 }
718 ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
719 if (ret < 0) {
720 goto out;
721 }
722
723 /* ibm,associativity-lookup-arrays */
724 cur_index = int_buf;
6663864e 725 int_buf[0] = cpu_to_be32(nr_nodes);
03d196b7
BR
726 int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
727 cur_index += 2;
6663864e 728 for (i = 0; i < nr_nodes; i++) {
03d196b7
BR
729 uint32_t associativity[] = {
730 cpu_to_be32(0x0),
731 cpu_to_be32(0x0),
732 cpu_to_be32(0x0),
733 cpu_to_be32(i)
734 };
735 memcpy(cur_index, associativity, sizeof(associativity));
736 cur_index += 4;
737 }
738 ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
739 (cur_index - int_buf) * sizeof(uint32_t));
740out:
741 g_free(int_buf);
742 return ret;
743}
744
745int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
746 target_ulong addr, target_ulong size,
747 bool cpu_update, bool memory_update)
748{
749 void *fdt, *fdt_skel;
750 sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
751 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
752
753 size -= sizeof(hdr);
754
755 /* Create sceleton */
756 fdt_skel = g_malloc0(size);
757 _FDT((fdt_create(fdt_skel, size)));
758 _FDT((fdt_begin_node(fdt_skel, "")));
759 _FDT((fdt_end_node(fdt_skel)));
760 _FDT((fdt_finish(fdt_skel)));
761 fdt = g_malloc0(size);
762 _FDT((fdt_open_into(fdt_skel, fdt, size)));
763 g_free(fdt_skel);
764
765 /* Fixup cpu nodes */
766 if (cpu_update) {
767 _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
768 }
769
16c25aef 770 /* Generate ibm,dynamic-reconfiguration-memory node if required */
03d196b7
BR
771 if (memory_update && smc->dr_lmb_enabled) {
772 _FDT((spapr_populate_drconf_memory(spapr, fdt)));
03d196b7
BR
773 }
774
775 /* Pack resulting tree */
776 _FDT((fdt_pack(fdt)));
777
778 if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
779 trace_spapr_cas_failed(size);
780 return -1;
781 }
782
783 cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
784 cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
785 trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
786 g_free(fdt);
787
788 return 0;
789}
790
3f5dabce
DG
791static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
792{
793 int rtas;
794 GString *hypertas = g_string_sized_new(256);
795 GString *qemu_hypertas = g_string_sized_new(256);
796 uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
797 uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
798 memory_region_size(&spapr->hotplug_memory.mr);
799 uint32_t lrdr_capacity[] = {
800 cpu_to_be32(max_hotplug_addr >> 32),
801 cpu_to_be32(max_hotplug_addr & 0xffffffff),
802 0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
803 cpu_to_be32(max_cpus / smp_threads),
804 };
805
806 _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
807
808 /* hypertas */
809 add_str(hypertas, "hcall-pft");
810 add_str(hypertas, "hcall-term");
811 add_str(hypertas, "hcall-dabr");
812 add_str(hypertas, "hcall-interrupt");
813 add_str(hypertas, "hcall-tce");
814 add_str(hypertas, "hcall-vio");
815 add_str(hypertas, "hcall-splpar");
816 add_str(hypertas, "hcall-bulk");
817 add_str(hypertas, "hcall-set-mode");
818 add_str(hypertas, "hcall-sprg0");
819 add_str(hypertas, "hcall-copy");
820 add_str(hypertas, "hcall-debug");
821 add_str(qemu_hypertas, "hcall-memop1");
822
823 if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
824 add_str(hypertas, "hcall-multi-tce");
825 }
826 _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
827 hypertas->str, hypertas->len));
828 g_string_free(hypertas, TRUE);
829 _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
830 qemu_hypertas->str, qemu_hypertas->len));
831 g_string_free(qemu_hypertas, TRUE);
832
833 _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
834 refpoints, sizeof(refpoints)));
835
836 _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
837 RTAS_ERROR_LOG_MAX));
838 _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
839 RTAS_EVENT_SCAN_RATE));
840
841 if (msi_nonbroken) {
842 _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
843 }
844
845 /*
846 * According to PAPR, rtas ibm,os-term does not guarantee a return
847 * back to the guest cpu.
848 *
849 * While an additional ibm,extended-os-term property indicates
850 * that rtas call return will always occur. Set this property.
851 */
852 _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
853
854 _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
855 lrdr_capacity, sizeof(lrdr_capacity)));
856
857 spapr_dt_rtas_tokens(fdt, rtas);
858}
859
7c866c6a
DG
860static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
861{
862 MachineState *machine = MACHINE(spapr);
863 int chosen;
864 const char *boot_device = machine->boot_order;
865 char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
866 size_t cb = 0;
867 char *bootlist = get_boot_devices_list(&cb, true);
868 unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
869
870 _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
871
872 /* Set Form1_affinity */
873 _FDT(fdt_setprop(fdt, chosen, "ibm,architecture-vec-5",
874 vec5, sizeof(vec5)));
875
876 _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
877 _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
878 spapr->initrd_base));
879 _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
880 spapr->initrd_base + spapr->initrd_size));
881
882 if (spapr->kernel_size) {
883 uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
884 cpu_to_be64(spapr->kernel_size) };
885
886 _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
887 &kprop, sizeof(kprop)));
888 if (spapr->kernel_le) {
889 _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
890 }
891 }
892 if (boot_menu) {
893 _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
894 }
895 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
896 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
897 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
898
899 if (cb && bootlist) {
900 int i;
901
902 for (i = 0; i < cb; i++) {
903 if (bootlist[i] == '\n') {
904 bootlist[i] = ' ';
905 }
906 }
907 _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
908 }
909
910 if (boot_device && strlen(boot_device)) {
911 _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
912 }
913
914 if (!spapr->has_graphics && stdout_path) {
915 _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
916 }
917
918 g_free(stdout_path);
919 g_free(bootlist);
920}
921
997b6cfc
DG
922static void *spapr_build_fdt(sPAPRMachineState *spapr,
923 hwaddr rtas_addr,
924 hwaddr rtas_size)
a3467baa 925{
5b2128d2 926 MachineState *machine = MACHINE(qdev_get_machine());
3c0c47e3 927 MachineClass *mc = MACHINE_GET_CLASS(machine);
c20d332a 928 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
7c866c6a 929 int ret;
a3467baa 930 void *fdt;
3384f95c 931 sPAPRPHBState *phb;
a3467baa 932
7267c094 933 fdt = g_malloc(FDT_MAX_SIZE);
a3467baa
DG
934
935 /* open out the base tree into a temp buffer for the final tweaks */
936 _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
4040ab72 937
9b9a1908
DG
938 /* /interrupt controller */
939 spapr_dt_xics(spapr->xics, fdt, PHANDLE_XICP);
940
e8f986fc
BR
941 ret = spapr_populate_memory(spapr, fdt);
942 if (ret < 0) {
ce9863b7 943 error_report("couldn't setup memory nodes in fdt");
e8f986fc 944 exit(1);
7f763a5d
DG
945 }
946
4040ab72
DG
947 ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
948 if (ret < 0) {
ce9863b7 949 error_report("couldn't setup vio devices in fdt");
4040ab72
DG
950 exit(1);
951 }
952
4d9392be
TH
953 if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
954 ret = spapr_rng_populate_dt(fdt);
955 if (ret < 0) {
ce9863b7 956 error_report("could not set up rng device in the fdt");
4d9392be
TH
957 exit(1);
958 }
959 }
960
3384f95c 961 QLIST_FOREACH(phb, &spapr->phbs, list) {
e0fdbd7c 962 ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
da34fed7
TH
963 if (ret < 0) {
964 error_report("couldn't setup PCI devices in fdt");
965 exit(1);
966 }
3384f95c
DG
967 }
968
0da6f3fe
BR
969 /* cpus */
970 spapr_populate_cpus_dt_node(fdt, spapr);
6e806cc3 971
c20d332a
BR
972 if (smc->dr_lmb_enabled) {
973 _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
974 }
975
3c0c47e3 976 if (mc->query_hotpluggable_cpus) {
af81cf32
BR
977 int offset = fdt_path_offset(fdt, "/cpus");
978 ret = spapr_drc_populate_dt(fdt, offset, NULL,
979 SPAPR_DR_CONNECTOR_TYPE_CPU);
980 if (ret < 0) {
981 error_report("Couldn't set up CPU DR device tree properties");
982 exit(1);
983 }
984 }
985
3f5dabce
DG
986 /* /rtas */
987 spapr_dt_rtas(spapr, fdt);
988
7c866c6a
DG
989 /* /chosen */
990 spapr_dt_chosen(spapr, fdt);
cf6e5223
DG
991
992 /* Build memory reserve map */
993 if (spapr->kernel_size) {
994 _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
995 }
996 if (spapr->initrd_size) {
997 _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
998 }
999
997b6cfc 1000 return fdt;
9fdf0c29
DG
1001}
1002
1003static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
1004{
1005 return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
1006}
1007
1b14670a 1008static void emulate_spapr_hypercall(PowerPCCPU *cpu)
9fdf0c29 1009{
1b14670a
AF
1010 CPUPPCState *env = &cpu->env;
1011
efcb9383
DG
1012 if (msr_pr) {
1013 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
1014 env->gpr[3] = H_PRIVILEGE;
1015 } else {
aa100fa4 1016 env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
efcb9383 1017 }
9fdf0c29
DG
1018}
1019
e6b8fd24
SMJ
1020#define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1021#define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1022#define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1023#define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1024#define DIRTY_HPTE(_hpte) ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1025
715c5407
DG
1026/*
1027 * Get the fd to access the kernel htab, re-opening it if necessary
1028 */
1029static int get_htab_fd(sPAPRMachineState *spapr)
1030{
1031 if (spapr->htab_fd >= 0) {
1032 return spapr->htab_fd;
1033 }
1034
1035 spapr->htab_fd = kvmppc_get_htab_fd(false);
1036 if (spapr->htab_fd < 0) {
1037 error_report("Unable to open fd for reading hash table from KVM: %s",
1038 strerror(errno));
1039 }
1040
1041 return spapr->htab_fd;
1042}
1043
1044static void close_htab_fd(sPAPRMachineState *spapr)
1045{
1046 if (spapr->htab_fd >= 0) {
1047 close(spapr->htab_fd);
1048 }
1049 spapr->htab_fd = -1;
1050}
1051
8dfe8e7f
DG
1052static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
1053{
1054 int shift;
1055
1056 /* We aim for a hash table of size 1/128 the size of RAM (rounded
1057 * up). The PAPR recommendation is actually 1/64 of RAM size, but
1058 * that's much more than is needed for Linux guests */
1059 shift = ctz64(pow2ceil(ramsize)) - 7;
1060 shift = MAX(shift, 18); /* Minimum architected size */
1061 shift = MIN(shift, 46); /* Maximum architected size */
1062 return shift;
1063}
1064
c5f54f3e
DG
1065static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1066 Error **errp)
7f763a5d 1067{
c5f54f3e
DG
1068 long rc;
1069
1070 /* Clean up any HPT info from a previous boot */
1071 g_free(spapr->htab);
1072 spapr->htab = NULL;
1073 spapr->htab_shift = 0;
1074 close_htab_fd(spapr);
1075
1076 rc = kvmppc_reset_htab(shift);
1077 if (rc < 0) {
1078 /* kernel-side HPT needed, but couldn't allocate one */
1079 error_setg_errno(errp, errno,
1080 "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1081 shift);
1082 /* This is almost certainly fatal, but if the caller really
1083 * wants to carry on with shift == 0, it's welcome to try */
1084 } else if (rc > 0) {
1085 /* kernel-side HPT allocated */
1086 if (rc != shift) {
1087 error_setg(errp,
1088 "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1089 shift, rc);
7735feda
BR
1090 }
1091
7f763a5d 1092 spapr->htab_shift = shift;
c18ad9a5 1093 spapr->htab = NULL;
b817772a 1094 } else {
c5f54f3e
DG
1095 /* kernel-side HPT not needed, allocate in userspace instead */
1096 size_t size = 1ULL << shift;
1097 int i;
b817772a 1098
c5f54f3e
DG
1099 spapr->htab = qemu_memalign(size, size);
1100 if (!spapr->htab) {
1101 error_setg_errno(errp, errno,
1102 "Could not allocate HPT of order %d", shift);
1103 return;
7735feda
BR
1104 }
1105
c5f54f3e
DG
1106 memset(spapr->htab, 0, size);
1107 spapr->htab_shift = shift;
e6b8fd24 1108
c5f54f3e
DG
1109 for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1110 DIRTY_HPTE(HPTE(spapr->htab, i));
e6b8fd24 1111 }
7f763a5d 1112 }
9fdf0c29
DG
1113}
1114
4f01a637 1115static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
9e3f9733
AG
1116{
1117 bool matched = false;
1118
1119 if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
1120 matched = true;
1121 }
1122
1123 if (!matched) {
1124 error_report("Device %s is not supported by this machine yet.",
1125 qdev_fw_name(DEVICE(sbdev)));
1126 exit(1);
1127 }
9e3f9733
AG
1128}
1129
c8787ad4 1130static void ppc_spapr_reset(void)
a3467baa 1131{
c5f54f3e
DG
1132 MachineState *machine = MACHINE(qdev_get_machine());
1133 sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
182735ef 1134 PowerPCCPU *first_ppc_cpu;
b7d1f77a 1135 uint32_t rtas_limit;
cae172ab 1136 hwaddr rtas_addr, fdt_addr;
997b6cfc
DG
1137 void *fdt;
1138 int rc;
259186a7 1139
9e3f9733
AG
1140 /* Check for unknown sysbus devices */
1141 foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
1142
c5f54f3e
DG
1143 /* Allocate and/or reset the hash page table */
1144 spapr_reallocate_hpt(spapr,
1145 spapr_hpt_shift_for_ramsize(machine->maxram_size),
1146 &error_fatal);
1147
1148 /* Update the RMA size if necessary */
1149 if (spapr->vrma_adjust) {
1150 spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1151 spapr->htab_shift);
1152 }
a3467baa 1153
c8787ad4 1154 qemu_devices_reset();
a3467baa 1155
b7d1f77a
BH
1156 /*
1157 * We place the device tree and RTAS just below either the top of the RMA,
1158 * or just below 2GB, whichever is lowere, so that it can be
1159 * processed with 32-bit real mode code if necessary
1160 */
1161 rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
cae172ab
DG
1162 rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1163 fdt_addr = rtas_addr - FDT_MAX_SIZE;
b7d1f77a 1164
cae172ab 1165 fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
a3467baa 1166
2cac78c1 1167 spapr_load_rtas(spapr, fdt, rtas_addr);
b7d1f77a 1168
997b6cfc
DG
1169 rc = fdt_pack(fdt);
1170
1171 /* Should only fail if we've built a corrupted tree */
1172 assert(rc == 0);
1173
1174 if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1175 error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1176 fdt_totalsize(fdt), FDT_MAX_SIZE);
1177 exit(1);
1178 }
1179
1180 /* Load the fdt */
1181 qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
cae172ab 1182 cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
997b6cfc
DG
1183 g_free(fdt);
1184
a3467baa 1185 /* Set up the entry state */
182735ef 1186 first_ppc_cpu = POWERPC_CPU(first_cpu);
cae172ab 1187 first_ppc_cpu->env.gpr[3] = fdt_addr;
182735ef
AF
1188 first_ppc_cpu->env.gpr[5] = 0;
1189 first_cpu->halted = 0;
1b718907 1190 first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
a3467baa
DG
1191
1192}
1193
28e02042 1194static void spapr_create_nvram(sPAPRMachineState *spapr)
639e8102 1195{
2ff3de68 1196 DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
3978b863 1197 DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
639e8102 1198
3978b863 1199 if (dinfo) {
6231a6da
MA
1200 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
1201 &error_fatal);
639e8102
DG
1202 }
1203
1204 qdev_init_nofail(dev);
1205
1206 spapr->nvram = (struct sPAPRNVRAM *)dev;
1207}
1208
28e02042 1209static void spapr_rtc_create(sPAPRMachineState *spapr)
28df36a1
DG
1210{
1211 DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
1212
1213 qdev_init_nofail(dev);
1214 spapr->rtc = dev;
74e5ae28
DG
1215
1216 object_property_add_alias(qdev_get_machine(), "rtc-time",
1217 OBJECT(spapr->rtc), "date", NULL);
28df36a1
DG
1218}
1219
8c57b867 1220/* Returns whether we want to use VGA or not */
14c6a894 1221static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
f28359d8 1222{
8c57b867 1223 switch (vga_interface_type) {
8c57b867 1224 case VGA_NONE:
7effdaa3
MW
1225 return false;
1226 case VGA_DEVICE:
1227 return true;
1ddcae82 1228 case VGA_STD:
b798c190 1229 case VGA_VIRTIO:
1ddcae82 1230 return pci_vga_init(pci_bus) != NULL;
8c57b867 1231 default:
14c6a894
DG
1232 error_setg(errp,
1233 "Unsupported VGA mode, only -vga std or -vga virtio is supported");
1234 return false;
f28359d8 1235 }
f28359d8
LZ
1236}
1237
880ae7de
DG
1238static int spapr_post_load(void *opaque, int version_id)
1239{
28e02042 1240 sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
880ae7de
DG
1241 int err = 0;
1242
631b22ea 1243 /* In earlier versions, there was no separate qdev for the PAPR
880ae7de
DG
1244 * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1245 * So when migrating from those versions, poke the incoming offset
1246 * value into the RTC device */
1247 if (version_id < 3) {
1248 err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1249 }
1250
1251 return err;
1252}
1253
1254static bool version_before_3(void *opaque, int version_id)
1255{
1256 return version_id < 3;
1257}
1258
4be21d56
DG
1259static const VMStateDescription vmstate_spapr = {
1260 .name = "spapr",
880ae7de 1261 .version_id = 3,
4be21d56 1262 .minimum_version_id = 1,
880ae7de 1263 .post_load = spapr_post_load,
3aff6c2f 1264 .fields = (VMStateField[]) {
880ae7de
DG
1265 /* used to be @next_irq */
1266 VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
4be21d56
DG
1267
1268 /* RTC offset */
28e02042 1269 VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
880ae7de 1270
28e02042 1271 VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
4be21d56
DG
1272 VMSTATE_END_OF_LIST()
1273 },
1274};
1275
4be21d56
DG
1276static int htab_save_setup(QEMUFile *f, void *opaque)
1277{
28e02042 1278 sPAPRMachineState *spapr = opaque;
4be21d56 1279
4be21d56
DG
1280 /* "Iteration" header */
1281 qemu_put_be32(f, spapr->htab_shift);
1282
e68cb8b4
AK
1283 if (spapr->htab) {
1284 spapr->htab_save_index = 0;
1285 spapr->htab_first_pass = true;
1286 } else {
1287 assert(kvm_enabled());
e68cb8b4
AK
1288 }
1289
1290
4be21d56
DG
1291 return 0;
1292}
1293
28e02042 1294static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
4be21d56
DG
1295 int64_t max_ns)
1296{
378bc217 1297 bool has_timeout = max_ns != -1;
4be21d56
DG
1298 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
1299 int index = spapr->htab_save_index;
bc72ad67 1300 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4be21d56
DG
1301
1302 assert(spapr->htab_first_pass);
1303
1304 do {
1305 int chunkstart;
1306
1307 /* Consume invalid HPTEs */
1308 while ((index < htabslots)
1309 && !HPTE_VALID(HPTE(spapr->htab, index))) {
1310 index++;
1311 CLEAN_HPTE(HPTE(spapr->htab, index));
1312 }
1313
1314 /* Consume valid HPTEs */
1315 chunkstart = index;
338c25b6 1316 while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
4be21d56
DG
1317 && HPTE_VALID(HPTE(spapr->htab, index))) {
1318 index++;
1319 CLEAN_HPTE(HPTE(spapr->htab, index));
1320 }
1321
1322 if (index > chunkstart) {
1323 int n_valid = index - chunkstart;
1324
1325 qemu_put_be32(f, chunkstart);
1326 qemu_put_be16(f, n_valid);
1327 qemu_put_be16(f, 0);
1328 qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
1329 HASH_PTE_SIZE_64 * n_valid);
1330
378bc217
DG
1331 if (has_timeout &&
1332 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
4be21d56
DG
1333 break;
1334 }
1335 }
1336 } while ((index < htabslots) && !qemu_file_rate_limit(f));
1337
1338 if (index >= htabslots) {
1339 assert(index == htabslots);
1340 index = 0;
1341 spapr->htab_first_pass = false;
1342 }
1343 spapr->htab_save_index = index;
1344}
1345
28e02042 1346static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
e68cb8b4 1347 int64_t max_ns)
4be21d56
DG
1348{
1349 bool final = max_ns < 0;
1350 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
1351 int examined = 0, sent = 0;
1352 int index = spapr->htab_save_index;
bc72ad67 1353 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4be21d56
DG
1354
1355 assert(!spapr->htab_first_pass);
1356
1357 do {
1358 int chunkstart, invalidstart;
1359
1360 /* Consume non-dirty HPTEs */
1361 while ((index < htabslots)
1362 && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
1363 index++;
1364 examined++;
1365 }
1366
1367 chunkstart = index;
1368 /* Consume valid dirty HPTEs */
338c25b6 1369 while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
4be21d56
DG
1370 && HPTE_DIRTY(HPTE(spapr->htab, index))
1371 && HPTE_VALID(HPTE(spapr->htab, index))) {
1372 CLEAN_HPTE(HPTE(spapr->htab, index));
1373 index++;
1374 examined++;
1375 }
1376
1377 invalidstart = index;
1378 /* Consume invalid dirty HPTEs */
338c25b6 1379 while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
4be21d56
DG
1380 && HPTE_DIRTY(HPTE(spapr->htab, index))
1381 && !HPTE_VALID(HPTE(spapr->htab, index))) {
1382 CLEAN_HPTE(HPTE(spapr->htab, index));
1383 index++;
1384 examined++;
1385 }
1386
1387 if (index > chunkstart) {
1388 int n_valid = invalidstart - chunkstart;
1389 int n_invalid = index - invalidstart;
1390
1391 qemu_put_be32(f, chunkstart);
1392 qemu_put_be16(f, n_valid);
1393 qemu_put_be16(f, n_invalid);
1394 qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
1395 HASH_PTE_SIZE_64 * n_valid);
1396 sent += index - chunkstart;
1397
bc72ad67 1398 if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
4be21d56
DG
1399 break;
1400 }
1401 }
1402
1403 if (examined >= htabslots) {
1404 break;
1405 }
1406
1407 if (index >= htabslots) {
1408 assert(index == htabslots);
1409 index = 0;
1410 }
1411 } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
1412
1413 if (index >= htabslots) {
1414 assert(index == htabslots);
1415 index = 0;
1416 }
1417
1418 spapr->htab_save_index = index;
1419
e68cb8b4 1420 return (examined >= htabslots) && (sent == 0) ? 1 : 0;
4be21d56
DG
1421}
1422
e68cb8b4
AK
1423#define MAX_ITERATION_NS 5000000 /* 5 ms */
1424#define MAX_KVM_BUF_SIZE 2048
1425
4be21d56
DG
1426static int htab_save_iterate(QEMUFile *f, void *opaque)
1427{
28e02042 1428 sPAPRMachineState *spapr = opaque;
715c5407 1429 int fd;
e68cb8b4 1430 int rc = 0;
4be21d56
DG
1431
1432 /* Iteration header */
1433 qemu_put_be32(f, 0);
1434
e68cb8b4
AK
1435 if (!spapr->htab) {
1436 assert(kvm_enabled());
1437
715c5407
DG
1438 fd = get_htab_fd(spapr);
1439 if (fd < 0) {
1440 return fd;
01a57972
SMJ
1441 }
1442
715c5407 1443 rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
e68cb8b4
AK
1444 if (rc < 0) {
1445 return rc;
1446 }
1447 } else if (spapr->htab_first_pass) {
4be21d56
DG
1448 htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
1449 } else {
e68cb8b4 1450 rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
4be21d56
DG
1451 }
1452
1453 /* End marker */
1454 qemu_put_be32(f, 0);
1455 qemu_put_be16(f, 0);
1456 qemu_put_be16(f, 0);
1457
e68cb8b4 1458 return rc;
4be21d56
DG
1459}
1460
1461static int htab_save_complete(QEMUFile *f, void *opaque)
1462{
28e02042 1463 sPAPRMachineState *spapr = opaque;
715c5407 1464 int fd;
4be21d56
DG
1465
1466 /* Iteration header */
1467 qemu_put_be32(f, 0);
1468
e68cb8b4
AK
1469 if (!spapr->htab) {
1470 int rc;
1471
1472 assert(kvm_enabled());
1473
715c5407
DG
1474 fd = get_htab_fd(spapr);
1475 if (fd < 0) {
1476 return fd;
01a57972
SMJ
1477 }
1478
715c5407 1479 rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
e68cb8b4
AK
1480 if (rc < 0) {
1481 return rc;
1482 }
e68cb8b4 1483 } else {
378bc217
DG
1484 if (spapr->htab_first_pass) {
1485 htab_save_first_pass(f, spapr, -1);
1486 }
e68cb8b4
AK
1487 htab_save_later_pass(f, spapr, -1);
1488 }
4be21d56
DG
1489
1490 /* End marker */
1491 qemu_put_be32(f, 0);
1492 qemu_put_be16(f, 0);
1493 qemu_put_be16(f, 0);
1494
1495 return 0;
1496}
1497
1498static int htab_load(QEMUFile *f, void *opaque, int version_id)
1499{
28e02042 1500 sPAPRMachineState *spapr = opaque;
4be21d56 1501 uint32_t section_hdr;
e68cb8b4 1502 int fd = -1;
4be21d56
DG
1503
1504 if (version_id < 1 || version_id > 1) {
98a5d100 1505 error_report("htab_load() bad version");
4be21d56
DG
1506 return -EINVAL;
1507 }
1508
1509 section_hdr = qemu_get_be32(f);
1510
1511 if (section_hdr) {
9897e462 1512 Error *local_err = NULL;
c5f54f3e
DG
1513
1514 /* First section gives the htab size */
1515 spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1516 if (local_err) {
1517 error_report_err(local_err);
4be21d56
DG
1518 return -EINVAL;
1519 }
1520 return 0;
1521 }
1522
e68cb8b4
AK
1523 if (!spapr->htab) {
1524 assert(kvm_enabled());
1525
1526 fd = kvmppc_get_htab_fd(true);
1527 if (fd < 0) {
98a5d100
DG
1528 error_report("Unable to open fd to restore KVM hash table: %s",
1529 strerror(errno));
e68cb8b4
AK
1530 }
1531 }
1532
4be21d56
DG
1533 while (true) {
1534 uint32_t index;
1535 uint16_t n_valid, n_invalid;
1536
1537 index = qemu_get_be32(f);
1538 n_valid = qemu_get_be16(f);
1539 n_invalid = qemu_get_be16(f);
1540
1541 if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
1542 /* End of Stream */
1543 break;
1544 }
1545
e68cb8b4 1546 if ((index + n_valid + n_invalid) >
4be21d56
DG
1547 (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
1548 /* Bad index in stream */
98a5d100
DG
1549 error_report(
1550 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
1551 index, n_valid, n_invalid, spapr->htab_shift);
4be21d56
DG
1552 return -EINVAL;
1553 }
1554
e68cb8b4
AK
1555 if (spapr->htab) {
1556 if (n_valid) {
1557 qemu_get_buffer(f, HPTE(spapr->htab, index),
1558 HASH_PTE_SIZE_64 * n_valid);
1559 }
1560 if (n_invalid) {
1561 memset(HPTE(spapr->htab, index + n_valid), 0,
1562 HASH_PTE_SIZE_64 * n_invalid);
1563 }
1564 } else {
1565 int rc;
1566
1567 assert(fd >= 0);
1568
1569 rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1570 if (rc < 0) {
1571 return rc;
1572 }
4be21d56
DG
1573 }
1574 }
1575
e68cb8b4
AK
1576 if (!spapr->htab) {
1577 assert(fd >= 0);
1578 close(fd);
1579 }
1580
4be21d56
DG
1581 return 0;
1582}
1583
c573fc03
TH
1584static void htab_cleanup(void *opaque)
1585{
1586 sPAPRMachineState *spapr = opaque;
1587
1588 close_htab_fd(spapr);
1589}
1590
4be21d56
DG
1591static SaveVMHandlers savevm_htab_handlers = {
1592 .save_live_setup = htab_save_setup,
1593 .save_live_iterate = htab_save_iterate,
a3e06c3d 1594 .save_live_complete_precopy = htab_save_complete,
c573fc03 1595 .cleanup = htab_cleanup,
4be21d56
DG
1596 .load_state = htab_load,
1597};
1598
5b2128d2
AG
1599static void spapr_boot_set(void *opaque, const char *boot_device,
1600 Error **errp)
1601{
1602 MachineState *machine = MACHINE(qdev_get_machine());
1603 machine->boot_order = g_strdup(boot_device);
1604}
1605
224245bf
DG
1606/*
1607 * Reset routine for LMB DR devices.
1608 *
1609 * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1610 * routine. Reset for PCI DR devices will be handled by PHB reset routine
1611 * when it walks all its children devices. LMB devices reset occurs
1612 * as part of spapr_ppc_reset().
1613 */
1614static void spapr_drc_reset(void *opaque)
1615{
1616 sPAPRDRConnector *drc = opaque;
1617 DeviceState *d = DEVICE(drc);
1618
1619 if (d) {
1620 device_reset(d);
1621 }
1622}
1623
1624static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1625{
1626 MachineState *machine = MACHINE(spapr);
1627 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
e8f986fc 1628 uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
224245bf
DG
1629 int i;
1630
1631 for (i = 0; i < nr_lmbs; i++) {
1632 sPAPRDRConnector *drc;
1633 uint64_t addr;
1634
e8f986fc 1635 addr = i * lmb_size + spapr->hotplug_memory.base;
224245bf
DG
1636 drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1637 addr/lmb_size);
1638 qemu_register_reset(spapr_drc_reset, drc);
1639 }
1640}
1641
1642/*
1643 * If RAM size, maxmem size and individual node mem sizes aren't aligned
1644 * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1645 * since we can't support such unaligned sizes with DRCONF_MEMORY.
1646 */
7c150d6f 1647static void spapr_validate_node_memory(MachineState *machine, Error **errp)
224245bf
DG
1648{
1649 int i;
1650
7c150d6f
DG
1651 if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
1652 error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
1653 " is not aligned to %llu MiB",
1654 machine->ram_size,
1655 SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
1656 return;
1657 }
1658
1659 if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
1660 error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
1661 " is not aligned to %llu MiB",
1662 machine->ram_size,
1663 SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
1664 return;
224245bf
DG
1665 }
1666
1667 for (i = 0; i < nb_numa_nodes; i++) {
1668 if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
7c150d6f
DG
1669 error_setg(errp,
1670 "Node %d memory size 0x%" PRIx64
1671 " is not aligned to %llu MiB",
1672 i, numa_info[i].node_mem,
1673 SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
1674 return;
224245bf
DG
1675 }
1676 }
1677}
1678
9fdf0c29 1679/* pSeries LPAR / sPAPR hardware init */
3ef96221 1680static void ppc_spapr_init(MachineState *machine)
9fdf0c29 1681{
28e02042 1682 sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3c0c47e3 1683 MachineClass *mc = MACHINE_GET_CLASS(machine);
224245bf 1684 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
3ef96221 1685 const char *kernel_filename = machine->kernel_filename;
3ef96221 1686 const char *initrd_filename = machine->initrd_filename;
8c9f64df 1687 PCIHostState *phb;
9fdf0c29 1688 int i;
890c2b77
AK
1689 MemoryRegion *sysmem = get_system_memory();
1690 MemoryRegion *ram = g_new(MemoryRegion, 1);
658fa66b
AK
1691 MemoryRegion *rma_region;
1692 void *rma = NULL;
a8170e5e 1693 hwaddr rma_alloc_size;
b082d65a 1694 hwaddr node0_size = spapr_node0_size();
b7d1f77a 1695 long load_limit, fw_size;
39ac8455 1696 char *filename;
94a94e4c
BR
1697 int smt = kvmppc_smt_threads();
1698 int spapr_cores = smp_cpus / smp_threads;
1699 int spapr_max_cores = max_cpus / smp_threads;
1700
3c0c47e3 1701 if (mc->query_hotpluggable_cpus) {
94a94e4c
BR
1702 if (smp_cpus % smp_threads) {
1703 error_report("smp_cpus (%u) must be multiple of threads (%u)",
1704 smp_cpus, smp_threads);
1705 exit(1);
1706 }
1707 if (max_cpus % smp_threads) {
1708 error_report("max_cpus (%u) must be multiple of threads (%u)",
1709 max_cpus, smp_threads);
1710 exit(1);
1711 }
1712 }
9fdf0c29 1713
226419d6 1714 msi_nonbroken = true;
0ee2c058 1715
d43b45e2
DG
1716 QLIST_INIT(&spapr->phbs);
1717
9fdf0c29
DG
1718 cpu_ppc_hypercall = emulate_spapr_hypercall;
1719
354ac20a 1720 /* Allocate RMA if necessary */
658fa66b 1721 rma_alloc_size = kvmppc_alloc_rma(&rma);
354ac20a
DG
1722
1723 if (rma_alloc_size == -1) {
730fce59 1724 error_report("Unable to create RMA");
354ac20a
DG
1725 exit(1);
1726 }
7f763a5d 1727
c4177479 1728 if (rma_alloc_size && (rma_alloc_size < node0_size)) {
7f763a5d 1729 spapr->rma_size = rma_alloc_size;
354ac20a 1730 } else {
c4177479 1731 spapr->rma_size = node0_size;
7f763a5d
DG
1732
1733 /* With KVM, we don't actually know whether KVM supports an
1734 * unbounded RMA (PR KVM) or is limited by the hash table size
1735 * (HV KVM using VRMA), so we always assume the latter
1736 *
1737 * In that case, we also limit the initial allocations for RTAS
1738 * etc... to 256M since we have no way to know what the VRMA size
1739 * is going to be as it depends on the size of the hash table
1740 * isn't determined yet.
1741 */
1742 if (kvm_enabled()) {
1743 spapr->vrma_adjust = 1;
1744 spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
1745 }
912acdf4
BH
1746
1747 /* Actually we don't support unbounded RMA anymore since we
1748 * added proper emulation of HV mode. The max we can get is
1749 * 16G which also happens to be what we configure for PAPR
1750 * mode so make sure we don't do anything bigger than that
1751 */
1752 spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
354ac20a
DG
1753 }
1754
c4177479 1755 if (spapr->rma_size > node0_size) {
d54e4d76
DG
1756 error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
1757 spapr->rma_size);
c4177479
AK
1758 exit(1);
1759 }
1760
b7d1f77a
BH
1761 /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1762 load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
9fdf0c29 1763
7b565160 1764 /* Set up Interrupt Controller before we create the VCPUs */
27f24582
BH
1765 spapr->xics = xics_system_init(machine,
1766 DIV_ROUND_UP(max_cpus * smt, smp_threads),
1767 XICS_IRQS_SPAPR, &error_fatal);
7b565160 1768
224245bf 1769 if (smc->dr_lmb_enabled) {
7c150d6f 1770 spapr_validate_node_memory(machine, &error_fatal);
224245bf
DG
1771 }
1772
9fdf0c29 1773 /* init CPUs */
19fb2c36 1774 if (machine->cpu_model == NULL) {
3daa4a9f 1775 machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
9fdf0c29 1776 }
94a94e4c 1777
e703d2f7
GK
1778 ppc_cpu_parse_features(machine->cpu_model);
1779
3c0c47e3 1780 if (mc->query_hotpluggable_cpus) {
94a94e4c
BR
1781 char *type = spapr_get_cpu_core_type(machine->cpu_model);
1782
4babfaf0 1783 if (type == NULL) {
caebf378
CLG
1784 error_report("Unable to find sPAPR CPU Core definition");
1785 exit(1);
1786 }
1787
94a94e4c 1788 spapr->cores = g_new0(Object *, spapr_max_cores);
af81cf32 1789 for (i = 0; i < spapr_max_cores; i++) {
12bf2d33 1790 int core_id = i * smp_threads;
af81cf32
BR
1791 sPAPRDRConnector *drc =
1792 spapr_dr_connector_new(OBJECT(spapr),
12bf2d33
GK
1793 SPAPR_DR_CONNECTOR_TYPE_CPU,
1794 (core_id / smp_threads) * smt);
af81cf32
BR
1795
1796 qemu_register_reset(spapr_drc_reset, drc);
1797
1798 if (i < spapr_cores) {
caebf378 1799 Object *core = object_new(type);
af81cf32
BR
1800 object_property_set_int(core, smp_threads, "nr-threads",
1801 &error_fatal);
12bf2d33 1802 object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
af81cf32
BR
1803 &error_fatal);
1804 object_property_set_bool(core, true, "realized", &error_fatal);
94a94e4c 1805 }
9fdf0c29 1806 }
94a94e4c
BR
1807 g_free(type);
1808 } else {
1809 for (i = 0; i < smp_cpus; i++) {
1810 PowerPCCPU *cpu = cpu_ppc_init(machine->cpu_model);
1811 if (cpu == NULL) {
1812 error_report("Unable to find PowerPC CPU definition");
1813 exit(1);
1814 }
1815 spapr_cpu_init(spapr, cpu, &error_fatal);
1816 }
9fdf0c29
DG
1817 }
1818
026bfd89
DG
1819 if (kvm_enabled()) {
1820 /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
1821 kvmppc_enable_logical_ci_hcalls();
ef9971dd 1822 kvmppc_enable_set_mode_hcall();
5145ad4f
NW
1823
1824 /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
1825 kvmppc_enable_clear_ref_mod_hcalls();
026bfd89
DG
1826 }
1827
9fdf0c29 1828 /* allocate RAM */
f92f5da1 1829 memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
fb164994 1830 machine->ram_size);
f92f5da1 1831 memory_region_add_subregion(sysmem, 0, ram);
9fdf0c29 1832
658fa66b
AK
1833 if (rma_alloc_size && rma) {
1834 rma_region = g_new(MemoryRegion, 1);
1835 memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
1836 rma_alloc_size, rma);
1837 vmstate_register_ram_global(rma_region);
1838 memory_region_add_subregion(sysmem, 0, rma_region);
1839 }
1840
4a1c9cf0
BR
1841 /* initialize hotplug memory address space */
1842 if (machine->ram_size < machine->maxram_size) {
1843 ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
71c9a3dd
BR
1844 /*
1845 * Limit the number of hotpluggable memory slots to half the number
1846 * slots that KVM supports, leaving the other half for PCI and other
1847 * devices. However ensure that number of slots doesn't drop below 32.
1848 */
1849 int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
1850 SPAPR_MAX_RAM_SLOTS;
4a1c9cf0 1851
71c9a3dd
BR
1852 if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
1853 max_memslots = SPAPR_MAX_RAM_SLOTS;
1854 }
1855 if (machine->ram_slots > max_memslots) {
d54e4d76
DG
1856 error_report("Specified number of memory slots %"
1857 PRIu64" exceeds max supported %d",
71c9a3dd 1858 machine->ram_slots, max_memslots);
d54e4d76 1859 exit(1);
4a1c9cf0
BR
1860 }
1861
1862 spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
1863 SPAPR_HOTPLUG_MEM_ALIGN);
1864 memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
1865 "hotplug-memory", hotplug_mem_size);
1866 memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
1867 &spapr->hotplug_memory.mr);
1868 }
1869
224245bf
DG
1870 if (smc->dr_lmb_enabled) {
1871 spapr_create_lmb_dr_connectors(spapr);
1872 }
1873
39ac8455 1874 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
4c56440d 1875 if (!filename) {
730fce59 1876 error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
4c56440d
SW
1877 exit(1);
1878 }
b7d1f77a 1879 spapr->rtas_size = get_image_size(filename);
8afc22a2
ZJ
1880 if (spapr->rtas_size < 0) {
1881 error_report("Could not get size of LPAR rtas '%s'", filename);
1882 exit(1);
1883 }
b7d1f77a
BH
1884 spapr->rtas_blob = g_malloc(spapr->rtas_size);
1885 if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
730fce59 1886 error_report("Could not load LPAR rtas '%s'", filename);
39ac8455
DG
1887 exit(1);
1888 }
4d8d5467 1889 if (spapr->rtas_size > RTAS_MAX_SIZE) {
730fce59
TH
1890 error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
1891 (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
4d8d5467
BH
1892 exit(1);
1893 }
7267c094 1894 g_free(filename);
39ac8455 1895
74d042e5
DG
1896 /* Set up EPOW events infrastructure */
1897 spapr_events_init(spapr);
1898
12f42174 1899 /* Set up the RTC RTAS interfaces */
28df36a1 1900 spapr_rtc_create(spapr);
12f42174 1901
b5cec4c5 1902 /* Set up VIO bus */
4040ab72
DG
1903 spapr->vio_bus = spapr_vio_bus_init();
1904
277f9acf 1905 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4040ab72 1906 if (serial_hds[i]) {
d601fac4 1907 spapr_vty_create(spapr->vio_bus, serial_hds[i]);
4040ab72
DG
1908 }
1909 }
9fdf0c29 1910
639e8102
DG
1911 /* We always have at least the nvram device on VIO */
1912 spapr_create_nvram(spapr);
1913
3384f95c 1914 /* Set up PCI */
fa28f71b
AK
1915 spapr_pci_rtas_init();
1916
89dfd6e1 1917 phb = spapr_create_phb(spapr, 0);
3384f95c 1918
277f9acf 1919 for (i = 0; i < nb_nics; i++) {
8d90ad90
DG
1920 NICInfo *nd = &nd_table[i];
1921
1922 if (!nd->model) {
7267c094 1923 nd->model = g_strdup("ibmveth");
8d90ad90
DG
1924 }
1925
1926 if (strcmp(nd->model, "ibmveth") == 0) {
d601fac4 1927 spapr_vlan_create(spapr->vio_bus, nd);
8d90ad90 1928 } else {
29b358f9 1929 pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
8d90ad90
DG
1930 }
1931 }
1932
6e270446 1933 for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
d601fac4 1934 spapr_vscsi_create(spapr->vio_bus);
6e270446
BH
1935 }
1936
f28359d8 1937 /* Graphics */
14c6a894 1938 if (spapr_vga_init(phb->bus, &error_fatal)) {
3fc5acde 1939 spapr->has_graphics = true;
c6e76503 1940 machine->usb |= defaults_enabled() && !machine->usb_disabled;
f28359d8
LZ
1941 }
1942
4ee9ced9 1943 if (machine->usb) {
57040d45
TH
1944 if (smc->use_ohci_by_default) {
1945 pci_create_simple(phb->bus, -1, "pci-ohci");
1946 } else {
1947 pci_create_simple(phb->bus, -1, "nec-usb-xhci");
1948 }
c86580b8 1949
35139a59 1950 if (spapr->has_graphics) {
c86580b8
MA
1951 USBBus *usb_bus = usb_bus_find(-1);
1952
1953 usb_create_simple(usb_bus, "usb-kbd");
1954 usb_create_simple(usb_bus, "usb-mouse");
35139a59
DG
1955 }
1956 }
1957
7f763a5d 1958 if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
d54e4d76
DG
1959 error_report(
1960 "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
1961 MIN_RMA_SLOF);
4d8d5467
BH
1962 exit(1);
1963 }
1964
9fdf0c29
DG
1965 if (kernel_filename) {
1966 uint64_t lowaddr = 0;
1967
a19f7fb0
DG
1968 spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
1969 NULL, NULL, &lowaddr, NULL, 1,
1970 PPC_ELF_MACHINE, 0, 0);
1971 if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
1972 spapr->kernel_size = load_elf(kernel_filename,
1973 translate_kernel_address, NULL, NULL,
1974 &lowaddr, NULL, 0, PPC_ELF_MACHINE,
1975 0, 0);
1976 spapr->kernel_le = spapr->kernel_size > 0;
16457e7f 1977 }
a19f7fb0
DG
1978 if (spapr->kernel_size < 0) {
1979 error_report("error loading %s: %s", kernel_filename,
1980 load_elf_strerror(spapr->kernel_size));
9fdf0c29
DG
1981 exit(1);
1982 }
1983
1984 /* load initrd */
1985 if (initrd_filename) {
4d8d5467
BH
1986 /* Try to locate the initrd in the gap between the kernel
1987 * and the firmware. Add a bit of space just in case
1988 */
a19f7fb0
DG
1989 spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
1990 + 0x1ffff) & ~0xffff;
1991 spapr->initrd_size = load_image_targphys(initrd_filename,
1992 spapr->initrd_base,
1993 load_limit
1994 - spapr->initrd_base);
1995 if (spapr->initrd_size < 0) {
d54e4d76
DG
1996 error_report("could not load initial ram disk '%s'",
1997 initrd_filename);
9fdf0c29
DG
1998 exit(1);
1999 }
9fdf0c29 2000 }
4d8d5467 2001 }
a3467baa 2002
8e7ea787
AF
2003 if (bios_name == NULL) {
2004 bios_name = FW_FILE_NAME;
2005 }
2006 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
4c56440d 2007 if (!filename) {
68fea5a0 2008 error_report("Could not find LPAR firmware '%s'", bios_name);
4c56440d
SW
2009 exit(1);
2010 }
4d8d5467 2011 fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
68fea5a0
TH
2012 if (fw_size <= 0) {
2013 error_report("Could not load LPAR firmware '%s'", filename);
4d8d5467
BH
2014 exit(1);
2015 }
2016 g_free(filename);
4d8d5467 2017
28e02042
DG
2018 /* FIXME: Should register things through the MachineState's qdev
2019 * interface, this is a legacy from the sPAPREnvironment structure
2020 * which predated MachineState but had a similar function */
4be21d56
DG
2021 vmstate_register(NULL, 0, &vmstate_spapr, spapr);
2022 register_savevm_live(NULL, "spapr/htab", -1, 1,
2023 &savevm_htab_handlers, spapr);
2024
9fdf0c29 2025 /* Prepare the device tree */
a19f7fb0 2026 spapr->fdt_skel = spapr_create_fdt_skel(spapr);
a3467baa 2027 assert(spapr->fdt_skel != NULL);
5b2128d2 2028
46503c2b
MR
2029 /* used by RTAS */
2030 QTAILQ_INIT(&spapr->ccs_list);
2031 qemu_register_reset(spapr_ccs_reset_hook, spapr);
2032
5b2128d2 2033 qemu_register_boot_set(spapr_boot_set, spapr);
9fdf0c29
DG
2034}
2035
135a129a
AK
2036static int spapr_kvm_type(const char *vm_type)
2037{
2038 if (!vm_type) {
2039 return 0;
2040 }
2041
2042 if (!strcmp(vm_type, "HV")) {
2043 return 1;
2044 }
2045
2046 if (!strcmp(vm_type, "PR")) {
2047 return 2;
2048 }
2049
2050 error_report("Unknown kvm-type specified '%s'", vm_type);
2051 exit(1);
2052}
2053
71461b0f 2054/*
627b84f4 2055 * Implementation of an interface to adjust firmware path
71461b0f
AK
2056 * for the bootindex property handling.
2057 */
2058static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
2059 DeviceState *dev)
2060{
2061#define CAST(type, obj, name) \
2062 ((type *)object_dynamic_cast(OBJECT(obj), (name)))
2063 SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE);
2064 sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
2065
2066 if (d) {
2067 void *spapr = CAST(void, bus->parent, "spapr-vscsi");
2068 VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
2069 USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
2070
2071 if (spapr) {
2072 /*
2073 * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
2074 * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
2075 * in the top 16 bits of the 64-bit LUN
2076 */
2077 unsigned id = 0x8000 | (d->id << 8) | d->lun;
2078 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
2079 (uint64_t)id << 48);
2080 } else if (virtio) {
2081 /*
2082 * We use SRP luns of the form 01000000 | (target << 8) | lun
2083 * in the top 32 bits of the 64-bit LUN
2084 * Note: the quote above is from SLOF and it is wrong,
2085 * the actual binding is:
2086 * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
2087 */
2088 unsigned id = 0x1000000 | (d->id << 16) | d->lun;
2089 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
2090 (uint64_t)id << 32);
2091 } else if (usb) {
2092 /*
2093 * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
2094 * in the top 32 bits of the 64-bit LUN
2095 */
2096 unsigned usb_port = atoi(usb->port->path);
2097 unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
2098 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
2099 (uint64_t)id << 32);
2100 }
2101 }
2102
2103 if (phb) {
2104 /* Replace "pci" with "pci@800000020000000" */
2105 return g_strdup_printf("pci@%"PRIX64, phb->buid);
2106 }
2107
2108 return NULL;
2109}
2110
23825581
EH
2111static char *spapr_get_kvm_type(Object *obj, Error **errp)
2112{
28e02042 2113 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
23825581 2114
28e02042 2115 return g_strdup(spapr->kvm_type);
23825581
EH
2116}
2117
2118static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
2119{
28e02042 2120 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
23825581 2121
28e02042
DG
2122 g_free(spapr->kvm_type);
2123 spapr->kvm_type = g_strdup(value);
23825581
EH
2124}
2125
2126static void spapr_machine_initfn(Object *obj)
2127{
715c5407
DG
2128 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2129
2130 spapr->htab_fd = -1;
23825581
EH
2131 object_property_add_str(obj, "kvm-type",
2132 spapr_get_kvm_type, spapr_set_kvm_type, NULL);
49d2e648
MA
2133 object_property_set_description(obj, "kvm-type",
2134 "Specifies the KVM virtualization mode (HV, PR)",
2135 NULL);
23825581
EH
2136}
2137
87bbdd9c
DG
2138static void spapr_machine_finalizefn(Object *obj)
2139{
2140 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2141
2142 g_free(spapr->kvm_type);
2143}
2144
e0eeb4a2 2145static void ppc_cpu_do_nmi_on_cpu(CPUState *cs, void *arg)
34316482 2146{
34316482
AK
2147 cpu_synchronize_state(cs);
2148 ppc_cpu_do_system_reset(cs);
2149}
2150
2151static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
2152{
2153 CPUState *cs;
2154
2155 CPU_FOREACH(cs) {
e0eeb4a2 2156 async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, NULL);
34316482
AK
2157 }
2158}
2159
c20d332a
BR
2160static void spapr_add_lmbs(DeviceState *dev, uint64_t addr, uint64_t size,
2161 uint32_t node, Error **errp)
2162{
2163 sPAPRDRConnector *drc;
2164 sPAPRDRConnectorClass *drck;
2165 uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2166 int i, fdt_offset, fdt_size;
2167 void *fdt;
2168
c20d332a
BR
2169 for (i = 0; i < nr_lmbs; i++) {
2170 drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2171 addr/SPAPR_MEMORY_BLOCK_SIZE);
2172 g_assert(drc);
2173
2174 fdt = create_device_tree(&fdt_size);
2175 fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2176 SPAPR_MEMORY_BLOCK_SIZE);
2177
2178 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2179 drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
c20d332a
BR
2180 addr += SPAPR_MEMORY_BLOCK_SIZE;
2181 }
5dd5238c
JD
2182 /* send hotplug notification to the
2183 * guest only in case of hotplugged memory
2184 */
2185 if (dev->hotplugged) {
2186 spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB, nr_lmbs);
2187 }
c20d332a
BR
2188}
2189
2190static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2191 uint32_t node, Error **errp)
2192{
2193 Error *local_err = NULL;
2194 sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2195 PCDIMMDevice *dimm = PC_DIMM(dev);
2196 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2197 MemoryRegion *mr = ddc->get_memory_region(dimm);
2198 uint64_t align = memory_region_get_alignment(mr);
2199 uint64_t size = memory_region_size(mr);
2200 uint64_t addr;
2201
2202 if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2203 error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2204 "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2205 goto out;
2206 }
2207
d6a9b0b8 2208 pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
c20d332a
BR
2209 if (local_err) {
2210 goto out;
2211 }
2212
2213 addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2214 if (local_err) {
2215 pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2216 goto out;
2217 }
2218
2219 spapr_add_lmbs(dev, addr, size, node, &error_abort);
2220
2221out:
2222 error_propagate(errp, local_err);
2223}
2224
af81cf32
BR
2225void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2226 sPAPRMachineState *spapr)
2227{
2228 PowerPCCPU *cpu = POWERPC_CPU(cs);
2229 DeviceClass *dc = DEVICE_GET_CLASS(cs);
2230 int id = ppc_get_vcpu_dt_id(cpu);
2231 void *fdt;
2232 int offset, fdt_size;
2233 char *nodename;
2234
2235 fdt = create_device_tree(&fdt_size);
2236 nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2237 offset = fdt_add_subnode(fdt, 0, nodename);
2238
2239 spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2240 g_free(nodename);
2241
2242 *fdt_offset = offset;
2243 return fdt;
2244}
2245
c20d332a
BR
2246static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2247 DeviceState *dev, Error **errp)
2248{
2249 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2250
2251 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
b556854b 2252 int node;
c20d332a
BR
2253
2254 if (!smc->dr_lmb_enabled) {
2255 error_setg(errp, "Memory hotplug not supported for this machine");
2256 return;
2257 }
2258 node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2259 if (*errp) {
2260 return;
2261 }
1a5512bb
GA
2262 if (node < 0 || node >= MAX_NODES) {
2263 error_setg(errp, "Invaild node %d", node);
2264 return;
2265 }
c20d332a 2266
b556854b
BR
2267 /*
2268 * Currently PowerPC kernel doesn't allow hot-adding memory to
2269 * memory-less node, but instead will silently add the memory
2270 * to the first node that has some memory. This causes two
2271 * unexpected behaviours for the user.
2272 *
2273 * - Memory gets hotplugged to a different node than what the user
2274 * specified.
2275 * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2276 * to memory-less node, a reboot will set things accordingly
2277 * and the previously hotplugged memory now ends in the right node.
2278 * This appears as if some memory moved from one node to another.
2279 *
2280 * So until kernel starts supporting memory hotplug to memory-less
2281 * nodes, just prevent such attempts upfront in QEMU.
2282 */
2283 if (nb_numa_nodes && !numa_info[node].node_mem) {
2284 error_setg(errp, "Can't hotplug memory to memory-less node %d",
2285 node);
2286 return;
2287 }
2288
c20d332a 2289 spapr_memory_plug(hotplug_dev, dev, node, errp);
af81cf32
BR
2290 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2291 spapr_core_plug(hotplug_dev, dev, errp);
c20d332a
BR
2292 }
2293}
2294
2295static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2296 DeviceState *dev, Error **errp)
2297{
3c0c47e3 2298 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
6f4b5c3e 2299
c20d332a
BR
2300 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2301 error_setg(errp, "Memory hot unplug not supported by sPAPR");
6f4b5c3e 2302 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3c0c47e3 2303 if (!mc->query_hotpluggable_cpus) {
6f4b5c3e
BR
2304 error_setg(errp, "CPU hot unplug not supported on this machine");
2305 return;
2306 }
2307 spapr_core_unplug(hotplug_dev, dev, errp);
c20d332a
BR
2308 }
2309}
2310
94a94e4c
BR
2311static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
2312 DeviceState *dev, Error **errp)
2313{
2314 if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2315 spapr_core_pre_plug(hotplug_dev, dev, errp);
2316 }
2317}
2318
7ebaf795
BR
2319static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
2320 DeviceState *dev)
c20d332a 2321{
94a94e4c
BR
2322 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
2323 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
c20d332a
BR
2324 return HOTPLUG_HANDLER(machine);
2325 }
2326 return NULL;
2327}
2328
20bb648d
DG
2329static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
2330{
2331 /* Allocate to NUMA nodes on a "socket" basis (not that concept of
2332 * socket means much for the paravirtualized PAPR platform) */
2333 return cpu_index / smp_threads / smp_cores;
2334}
2335
2474bfd4
IM
2336static HotpluggableCPUList *spapr_query_hotpluggable_cpus(MachineState *machine)
2337{
2338 int i;
2339 HotpluggableCPUList *head = NULL;
2340 sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
2341 int spapr_max_cores = max_cpus / smp_threads;
2474bfd4
IM
2342
2343 for (i = 0; i < spapr_max_cores; i++) {
2344 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
2345 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
2346 CpuInstanceProperties *cpu_props = g_new0(typeof(*cpu_props), 1);
2347
2348 cpu_item->type = spapr_get_cpu_core_type(machine->cpu_model);
2349 cpu_item->vcpus_count = smp_threads;
27393c33 2350 cpu_props->has_core_id = true;
12bf2d33 2351 cpu_props->core_id = i * smp_threads;
2474bfd4
IM
2352 /* TODO: add 'has_node/node' here to describe
2353 to which node core belongs */
2354
2355 cpu_item->props = cpu_props;
2356 if (spapr->cores[i]) {
2357 cpu_item->has_qom_path = true;
2358 cpu_item->qom_path = object_get_canonical_path(spapr->cores[i]);
2359 }
2360 list_item->value = cpu_item;
2361 list_item->next = head;
2362 head = list_item;
2363 }
2364 return head;
2365}
2366
6737d9ad 2367static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
daa23699
DG
2368 uint64_t *buid, hwaddr *pio,
2369 hwaddr *mmio32, hwaddr *mmio64,
6737d9ad
DG
2370 unsigned n_dma, uint32_t *liobns, Error **errp)
2371{
357d1e3b
DG
2372 /*
2373 * New-style PHB window placement.
2374 *
2375 * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
2376 * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
2377 * windows.
2378 *
2379 * Some guest kernels can't work with MMIO windows above 1<<46
2380 * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
2381 *
2382 * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
2383 * PHB stacked together. (32TiB+2GiB)..(32TiB+64GiB) contains the
2384 * 2GiB 32-bit MMIO windows for each PHB. Then 33..64TiB has the
2385 * 1TiB 64-bit MMIO windows for each PHB.
2386 */
6737d9ad 2387 const uint64_t base_buid = 0x800000020000000ULL;
357d1e3b
DG
2388 const int max_phbs =
2389 (SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / SPAPR_PCI_MEM64_WIN_SIZE - 1;
6737d9ad
DG
2390 int i;
2391
357d1e3b
DG
2392 /* Sanity check natural alignments */
2393 QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2394 QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2395 QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
2396 QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
2397 /* Sanity check bounds */
2398 QEMU_BUILD_BUG_ON((max_phbs * SPAPR_PCI_IO_WIN_SIZE) > SPAPR_PCI_MEM32_WIN_SIZE);
2399 QEMU_BUILD_BUG_ON((max_phbs * SPAPR_PCI_MEM32_WIN_SIZE) > SPAPR_PCI_MEM64_WIN_SIZE);
2efff1c0 2400
357d1e3b 2401 if (index >= max_phbs) {
6737d9ad 2402 error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
357d1e3b 2403 max_phbs - 1);
6737d9ad
DG
2404 return;
2405 }
2406
2407 *buid = base_buid + index;
2408 for (i = 0; i < n_dma; ++i) {
2409 liobns[i] = SPAPR_PCI_LIOBN(index, i);
2410 }
2411
357d1e3b
DG
2412 *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
2413 *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
2414 *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
6737d9ad
DG
2415}
2416
29ee3247
AK
2417static void spapr_machine_class_init(ObjectClass *oc, void *data)
2418{
2419 MachineClass *mc = MACHINE_CLASS(oc);
224245bf 2420 sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
71461b0f 2421 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
34316482 2422 NMIClass *nc = NMI_CLASS(oc);
c20d332a 2423 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
958db90c 2424
0eb9054c 2425 mc->desc = "pSeries Logical Partition (PAPR compliant)";
fc9f38c3
DG
2426
2427 /*
2428 * We set up the default / latest behaviour here. The class_init
2429 * functions for the specific versioned machine types can override
2430 * these details for backwards compatibility
2431 */
958db90c
MA
2432 mc->init = ppc_spapr_init;
2433 mc->reset = ppc_spapr_reset;
2434 mc->block_default_type = IF_SCSI;
079019f2 2435 mc->max_cpus = 255;
958db90c 2436 mc->no_parallel = 1;
5b2128d2 2437 mc->default_boot_order = "";
a34944fe 2438 mc->default_ram_size = 512 * M_BYTE;
958db90c 2439 mc->kvm_type = spapr_kvm_type;
9e3f9733 2440 mc->has_dynamic_sysbus = true;
e4024630 2441 mc->pci_allow_0_address = true;
7ebaf795 2442 mc->get_hotplug_handler = spapr_get_hotplug_handler;
94a94e4c 2443 hc->pre_plug = spapr_machine_device_pre_plug;
c20d332a
BR
2444 hc->plug = spapr_machine_device_plug;
2445 hc->unplug = spapr_machine_device_unplug;
20bb648d 2446 mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
00b4fbe2 2447
fc9f38c3 2448 smc->dr_lmb_enabled = true;
3daa4a9f 2449 smc->tcg_default_cpu = "POWER8";
3c0c47e3 2450 mc->query_hotpluggable_cpus = spapr_query_hotpluggable_cpus;
71461b0f 2451 fwc->get_dev_path = spapr_get_fw_dev_path;
34316482 2452 nc->nmi_monitor_handler = spapr_nmi;
6737d9ad 2453 smc->phb_placement = spapr_phb_placement;
29ee3247
AK
2454}
2455
2456static const TypeInfo spapr_machine_info = {
2457 .name = TYPE_SPAPR_MACHINE,
2458 .parent = TYPE_MACHINE,
4aee7362 2459 .abstract = true,
6ca1502e 2460 .instance_size = sizeof(sPAPRMachineState),
23825581 2461 .instance_init = spapr_machine_initfn,
87bbdd9c 2462 .instance_finalize = spapr_machine_finalizefn,
183930c0 2463 .class_size = sizeof(sPAPRMachineClass),
29ee3247 2464 .class_init = spapr_machine_class_init,
71461b0f
AK
2465 .interfaces = (InterfaceInfo[]) {
2466 { TYPE_FW_PATH_PROVIDER },
34316482 2467 { TYPE_NMI },
c20d332a 2468 { TYPE_HOTPLUG_HANDLER },
71461b0f
AK
2469 { }
2470 },
29ee3247
AK
2471};
2472
fccbc785 2473#define DEFINE_SPAPR_MACHINE(suffix, verstr, latest) \
5013c547
DG
2474 static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
2475 void *data) \
2476 { \
2477 MachineClass *mc = MACHINE_CLASS(oc); \
2478 spapr_machine_##suffix##_class_options(mc); \
fccbc785
DG
2479 if (latest) { \
2480 mc->alias = "pseries"; \
2481 mc->is_default = 1; \
2482 } \
5013c547
DG
2483 } \
2484 static void spapr_machine_##suffix##_instance_init(Object *obj) \
2485 { \
2486 MachineState *machine = MACHINE(obj); \
2487 spapr_machine_##suffix##_instance_options(machine); \
2488 } \
2489 static const TypeInfo spapr_machine_##suffix##_info = { \
2490 .name = MACHINE_TYPE_NAME("pseries-" verstr), \
2491 .parent = TYPE_SPAPR_MACHINE, \
2492 .class_init = spapr_machine_##suffix##_class_init, \
2493 .instance_init = spapr_machine_##suffix##_instance_init, \
2494 }; \
2495 static void spapr_machine_register_##suffix(void) \
2496 { \
2497 type_register(&spapr_machine_##suffix##_info); \
2498 } \
0e6aac87 2499 type_init(spapr_machine_register_##suffix)
5013c547 2500
db800b21
DG
2501/*
2502 * pseries-2.8
2503 */
2504static void spapr_machine_2_8_instance_options(MachineState *machine)
2505{
2506}
2507
2508static void spapr_machine_2_8_class_options(MachineClass *mc)
2509{
2510 /* Defaults for the latest behaviour inherited from the base class */
2511}
2512
2513DEFINE_SPAPR_MACHINE(2_8, "2.8", true);
2514
1ea1eefc
BR
2515/*
2516 * pseries-2.7
2517 */
357d1e3b
DG
2518#define SPAPR_COMPAT_2_7 \
2519 HW_COMPAT_2_7 \
2520 { \
2521 .driver = TYPE_SPAPR_PCI_HOST_BRIDGE, \
2522 .property = "mem_win_size", \
2523 .value = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
2524 }, \
2525 { \
2526 .driver = TYPE_SPAPR_PCI_HOST_BRIDGE, \
2527 .property = "mem64_win_size", \
2528 .value = "0", \
2529 },
2530
2531static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
2532 uint64_t *buid, hwaddr *pio,
2533 hwaddr *mmio32, hwaddr *mmio64,
2534 unsigned n_dma, uint32_t *liobns, Error **errp)
2535{
2536 /* Legacy PHB placement for pseries-2.7 and earlier machine types */
2537 const uint64_t base_buid = 0x800000020000000ULL;
2538 const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
2539 const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
2540 const hwaddr pio_offset = 0x80000000; /* 2 GiB */
2541 const uint32_t max_index = 255;
2542 const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
2543
2544 uint64_t ram_top = MACHINE(spapr)->ram_size;
2545 hwaddr phb0_base, phb_base;
2546 int i;
2547
2548 /* Do we have hotpluggable memory? */
2549 if (MACHINE(spapr)->maxram_size > ram_top) {
2550 /* Can't just use maxram_size, because there may be an
2551 * alignment gap between normal and hotpluggable memory
2552 * regions */
2553 ram_top = spapr->hotplug_memory.base +
2554 memory_region_size(&spapr->hotplug_memory.mr);
2555 }
2556
2557 phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
2558
2559 if (index > max_index) {
2560 error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
2561 max_index);
2562 return;
2563 }
2564
2565 *buid = base_buid + index;
2566 for (i = 0; i < n_dma; ++i) {
2567 liobns[i] = SPAPR_PCI_LIOBN(index, i);
2568 }
2569
2570 phb_base = phb0_base + index * phb_spacing;
2571 *pio = phb_base + pio_offset;
2572 *mmio32 = phb_base + mmio_offset;
2573 /*
2574 * We don't set the 64-bit MMIO window, relying on the PHB's
2575 * fallback behaviour of automatically splitting a large "32-bit"
2576 * window into contiguous 32-bit and 64-bit windows
2577 */
2578}
db800b21 2579
1ea1eefc
BR
2580static void spapr_machine_2_7_instance_options(MachineState *machine)
2581{
672de881 2582 spapr_machine_2_8_instance_options(machine);
1ea1eefc
BR
2583}
2584
2585static void spapr_machine_2_7_class_options(MachineClass *mc)
2586{
3daa4a9f
TH
2587 sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
2588
db800b21 2589 spapr_machine_2_8_class_options(mc);
3daa4a9f 2590 smc->tcg_default_cpu = "POWER7";
db800b21 2591 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
357d1e3b 2592 smc->phb_placement = phb_placement_2_7;
1ea1eefc
BR
2593}
2594
db800b21 2595DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
1ea1eefc 2596
4b23699c
DG
2597/*
2598 * pseries-2.6
2599 */
1ea1eefc 2600#define SPAPR_COMPAT_2_6 \
ae4de14c
AK
2601 HW_COMPAT_2_6 \
2602 { \
2603 .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,\
2604 .property = "ddw",\
2605 .value = stringify(off),\
2606 },
1ea1eefc 2607
4b23699c
DG
2608static void spapr_machine_2_6_instance_options(MachineState *machine)
2609{
672de881 2610 spapr_machine_2_7_instance_options(machine);
4b23699c
DG
2611}
2612
2613static void spapr_machine_2_6_class_options(MachineClass *mc)
2614{
1ea1eefc 2615 spapr_machine_2_7_class_options(mc);
3c0c47e3 2616 mc->query_hotpluggable_cpus = NULL;
1ea1eefc 2617 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
4b23699c
DG
2618}
2619
1ea1eefc 2620DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
4b23699c 2621
1c5f29bb
DG
2622/*
2623 * pseries-2.5
2624 */
4b23699c 2625#define SPAPR_COMPAT_2_5 \
57c522f4
TH
2626 HW_COMPAT_2_5 \
2627 { \
2628 .driver = "spapr-vlan", \
2629 .property = "use-rx-buffer-pools", \
2630 .value = "off", \
2631 },
4b23699c 2632
5013c547 2633static void spapr_machine_2_5_instance_options(MachineState *machine)
1c5f29bb 2634{
672de881 2635 spapr_machine_2_6_instance_options(machine);
5013c547
DG
2636}
2637
2638static void spapr_machine_2_5_class_options(MachineClass *mc)
2639{
57040d45
TH
2640 sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
2641
4b23699c 2642 spapr_machine_2_6_class_options(mc);
57040d45 2643 smc->use_ohci_by_default = true;
4b23699c 2644 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
1c5f29bb
DG
2645}
2646
4b23699c 2647DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
1c5f29bb
DG
2648
2649/*
2650 * pseries-2.4
2651 */
80fd50f9
CH
2652#define SPAPR_COMPAT_2_4 \
2653 HW_COMPAT_2_4
2654
5013c547 2655static void spapr_machine_2_4_instance_options(MachineState *machine)
1c5f29bb 2656{
5013c547
DG
2657 spapr_machine_2_5_instance_options(machine);
2658}
1c5f29bb 2659
5013c547
DG
2660static void spapr_machine_2_4_class_options(MachineClass *mc)
2661{
fc9f38c3
DG
2662 sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
2663
2664 spapr_machine_2_5_class_options(mc);
fc9f38c3 2665 smc->dr_lmb_enabled = false;
f949b4e5 2666 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
1c5f29bb
DG
2667}
2668
fccbc785 2669DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
1c5f29bb
DG
2670
2671/*
2672 * pseries-2.3
2673 */
38ff32c6 2674#define SPAPR_COMPAT_2_3 \
7619c7b0
MR
2675 HW_COMPAT_2_3 \
2676 {\
2677 .driver = "spapr-pci-host-bridge",\
2678 .property = "dynamic-reconfiguration",\
2679 .value = "off",\
2680 },
38ff32c6 2681
5013c547 2682static void spapr_machine_2_3_instance_options(MachineState *machine)
d25228e7 2683{
5013c547 2684 spapr_machine_2_4_instance_options(machine);
ff14e817 2685 savevm_skip_section_footers();
13d16814 2686 global_state_set_optional();
09b5e30d 2687 savevm_skip_configuration();
d25228e7
JW
2688}
2689
5013c547 2690static void spapr_machine_2_3_class_options(MachineClass *mc)
6026db45 2691{
fc9f38c3 2692 spapr_machine_2_4_class_options(mc);
f949b4e5 2693 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
6026db45 2694}
fccbc785 2695DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
6026db45 2696
1c5f29bb
DG
2697/*
2698 * pseries-2.2
2699 */
2700
2701#define SPAPR_COMPAT_2_2 \
1c5f29bb
DG
2702 HW_COMPAT_2_2 \
2703 {\
2704 .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,\
2705 .property = "mem_win_size",\
2706 .value = "0x20000000",\
2707 },
2708
5013c547 2709static void spapr_machine_2_2_instance_options(MachineState *machine)
1c5f29bb 2710{
5013c547 2711 spapr_machine_2_3_instance_options(machine);
cba0e779 2712 machine->suppress_vmdesc = true;
1c5f29bb
DG
2713}
2714
5013c547 2715static void spapr_machine_2_2_class_options(MachineClass *mc)
4aee7362 2716{
fc9f38c3 2717 spapr_machine_2_3_class_options(mc);
f949b4e5 2718 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
4aee7362 2719}
fccbc785 2720DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
4aee7362 2721
1c5f29bb
DG
2722/*
2723 * pseries-2.1
2724 */
2725#define SPAPR_COMPAT_2_1 \
1c5f29bb 2726 HW_COMPAT_2_1
3dab0244 2727
5013c547 2728static void spapr_machine_2_1_instance_options(MachineState *machine)
1c5f29bb 2729{
5013c547 2730 spapr_machine_2_2_instance_options(machine);
1c5f29bb 2731}
d25228e7 2732
5013c547 2733static void spapr_machine_2_1_class_options(MachineClass *mc)
d25228e7 2734{
fc9f38c3 2735 spapr_machine_2_2_class_options(mc);
f949b4e5 2736 SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
d25228e7 2737}
fccbc785 2738DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
fb0fc8f6 2739
29ee3247 2740static void spapr_machine_register_types(void)
9fdf0c29 2741{
29ee3247 2742 type_register_static(&spapr_machine_info);
9fdf0c29
DG
2743}
2744
29ee3247 2745type_init(spapr_machine_register_types)