xref: /openbmc/qemu/hw/ppc/spapr.c (revision 6244bb7e)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2004-2007 Fabrice Bellard
553018216SPaolo Bonzini  * Copyright (c) 2007 Jocelyn Mayer
653018216SPaolo Bonzini  * Copyright (c) 2010 David Gibson, IBM Corporation.
753018216SPaolo Bonzini  *
853018216SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
953018216SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
1053018216SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
1153018216SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1253018216SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
1353018216SPaolo Bonzini  * furnished to do so, subject to the following conditions:
1453018216SPaolo Bonzini  *
1553018216SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
1653018216SPaolo Bonzini  * all copies or substantial portions of the Software.
1753018216SPaolo Bonzini  *
1853018216SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1953018216SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2053018216SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2153018216SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2253018216SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2353018216SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2453018216SPaolo Bonzini  * THE SOFTWARE.
2553018216SPaolo Bonzini  *
2653018216SPaolo Bonzini  */
270d75590dSPeter Maydell #include "qemu/osdep.h"
28da34e65cSMarkus Armbruster #include "qapi/error.h"
2953018216SPaolo Bonzini #include "sysemu/sysemu.h"
30e35704baSEduardo Habkost #include "sysemu/numa.h"
3153018216SPaolo Bonzini #include "hw/hw.h"
3203dd024fSPaolo Bonzini #include "qemu/log.h"
3371461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h"
3453018216SPaolo Bonzini #include "elf.h"
3553018216SPaolo Bonzini #include "net/net.h"
36ad440b4aSAndrew Jones #include "sysemu/device_tree.h"
37fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3853018216SPaolo Bonzini #include "sysemu/cpus.h"
39b3946626SVincent Palatin #include "sysemu/hw_accel.h"
4053018216SPaolo Bonzini #include "kvm_ppc.h"
41ff14e817SDr. David Alan Gilbert #include "migration/migration.h"
424be21d56SDavid Gibson #include "mmu-hash64.h"
433794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4453018216SPaolo Bonzini 
4553018216SPaolo Bonzini #include "hw/boards.h"
460d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
4753018216SPaolo Bonzini #include "hw/loader.h"
4853018216SPaolo Bonzini 
497804c353SCédric Le Goater #include "hw/ppc/fdt.h"
500d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
510d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
520d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
530d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5453018216SPaolo Bonzini #include "hw/pci/msi.h"
5553018216SPaolo Bonzini 
5653018216SPaolo Bonzini #include "hw/pci/pci.h"
5771461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
5871461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
5953018216SPaolo Bonzini 
6053018216SPaolo Bonzini #include "exec/address-spaces.h"
6153018216SPaolo Bonzini #include "hw/usb.h"
6253018216SPaolo Bonzini #include "qemu/config-file.h"
63135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
642a6593cbSAlexey Kardashevskiy #include "trace.h"
6534316482SAlexey Kardashevskiy #include "hw/nmi.h"
6653018216SPaolo Bonzini 
6768a27b20SMichael S. Tsirkin #include "hw/compat.h"
68f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
6994a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
702474bfd4SIgor Mammedov #include "qmp-commands.h"
7168a27b20SMichael S. Tsirkin 
7253018216SPaolo Bonzini #include <libfdt.h>
7353018216SPaolo Bonzini 
7453018216SPaolo Bonzini /* SLOF memory layout:
7553018216SPaolo Bonzini  *
7653018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
7753018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
7853018216SPaolo Bonzini  *
7953018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8053018216SPaolo Bonzini  * and more
8153018216SPaolo Bonzini  *
8253018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8353018216SPaolo Bonzini  */
8438b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
8553018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
86b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
8753018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
8853018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
8953018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9053018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9153018216SPaolo Bonzini 
9253018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9353018216SPaolo Bonzini 
9453018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9553018216SPaolo Bonzini 
9653018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9753018216SPaolo Bonzini 
98c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers,
9934f2af3dSMarkus Armbruster                                   int nr_irqs, Error **errp)
100c04d6cfaSAnthony Liguori {
10134f2af3dSMarkus Armbruster     Error *err = NULL;
102c04d6cfaSAnthony Liguori     DeviceState *dev;
103c04d6cfaSAnthony Liguori 
104c04d6cfaSAnthony Liguori     dev = qdev_create(NULL, type);
105c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
106c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
10734f2af3dSMarkus Armbruster     object_property_set_bool(OBJECT(dev), true, "realized", &err);
10834f2af3dSMarkus Armbruster     if (err) {
10934f2af3dSMarkus Armbruster         error_propagate(errp, err);
11034f2af3dSMarkus Armbruster         object_unparent(OBJECT(dev));
111c04d6cfaSAnthony Liguori         return NULL;
112c04d6cfaSAnthony Liguori     }
1135a3d7b23SAlexey Kardashevskiy     return XICS_COMMON(dev);
114c04d6cfaSAnthony Liguori }
115c04d6cfaSAnthony Liguori 
116446f16a6SMarcel Apfelbaum static XICSState *xics_system_init(MachineState *machine,
1171e49182dSDavid Gibson                                    int nr_servers, int nr_irqs, Error **errp)
118c04d6cfaSAnthony Liguori {
11927f24582SBenjamin Herrenschmidt     XICSState *xics = NULL;
120c04d6cfaSAnthony Liguori 
12111ad93f6SDavid Gibson     if (kvm_enabled()) {
12234f2af3dSMarkus Armbruster         Error *err = NULL;
12334f2af3dSMarkus Armbruster 
124446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_allowed(machine)) {
12527f24582SBenjamin Herrenschmidt             xics = try_create_xics(TYPE_XICS_SPAPR_KVM, nr_servers, nr_irqs,
126161deaf2SBenjamin Herrenschmidt                                    &err);
12711ad93f6SDavid Gibson         }
12827f24582SBenjamin Herrenschmidt         if (machine_kernel_irqchip_required(machine) && !xics) {
129b83baa60SMarkus Armbruster             error_reportf_err(err,
130b83baa60SMarkus Armbruster                               "kernel_irqchip requested but unavailable: ");
131b83baa60SMarkus Armbruster         } else {
132903a41d3SStefano Dong (董兴水)             error_free(err);
13311ad93f6SDavid Gibson         }
134b83baa60SMarkus Armbruster     }
13511ad93f6SDavid Gibson 
13627f24582SBenjamin Herrenschmidt     if (!xics) {
13727f24582SBenjamin Herrenschmidt         xics = try_create_xics(TYPE_XICS_SPAPR, nr_servers, nr_irqs, errp);
138c04d6cfaSAnthony Liguori     }
139c04d6cfaSAnthony Liguori 
14027f24582SBenjamin Herrenschmidt     return xics;
141c04d6cfaSAnthony Liguori }
142c04d6cfaSAnthony Liguori 
143833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
144833d4668SAlexey Kardashevskiy                                   int smt_threads)
145833d4668SAlexey Kardashevskiy {
146833d4668SAlexey Kardashevskiy     int i, ret = 0;
147833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
148833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
149833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
150833d4668SAlexey Kardashevskiy 
151d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
152d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
1536d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1546d9412eaSAlexey Kardashevskiy             return ret;
1556d9412eaSAlexey Kardashevskiy         }
1566d9412eaSAlexey Kardashevskiy     }
1576d9412eaSAlexey Kardashevskiy 
158833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
159833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
160833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
161833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
162833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
163833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
164833d4668SAlexey Kardashevskiy     }
165833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
166833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
167833d4668SAlexey Kardashevskiy     if (ret < 0) {
168833d4668SAlexey Kardashevskiy         return ret;
169833d4668SAlexey Kardashevskiy     }
170833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
171833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
172833d4668SAlexey Kardashevskiy 
173833d4668SAlexey Kardashevskiy     return ret;
174833d4668SAlexey Kardashevskiy }
175833d4668SAlexey Kardashevskiy 
1760da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
1770da6f3feSBharata B Rao {
1780da6f3feSBharata B Rao     int ret = 0;
1790da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
1800da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
1810da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
1820da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1830da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1840da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1850da6f3feSBharata B Rao                                 cpu_to_be32(cs->numa_node),
1860da6f3feSBharata B Rao                                 cpu_to_be32(index)};
1870da6f3feSBharata B Rao 
1880da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
1890da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
1900da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
1910da6f3feSBharata B Rao                           sizeof(associativity));
1920da6f3feSBharata B Rao     }
1930da6f3feSBharata B Rao 
1940da6f3feSBharata B Rao     return ret;
1950da6f3feSBharata B Rao }
1960da6f3feSBharata B Rao 
19728e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
19853018216SPaolo Bonzini {
19982677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
20082677ed2SAlexey Kardashevskiy     CPUState *cs;
20153018216SPaolo Bonzini     char cpu_model[32];
20253018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
20353018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
20453018216SPaolo Bonzini 
20582677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
20682677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
20782677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
20882677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
20912dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
21053018216SPaolo Bonzini 
2110f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
21253018216SPaolo Bonzini             continue;
21353018216SPaolo Bonzini         }
21453018216SPaolo Bonzini 
21582677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
21653018216SPaolo Bonzini 
21782677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
21882677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
21982677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
22082677ed2SAlexey Kardashevskiy                                           "cpus");
22182677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
22282677ed2SAlexey Kardashevskiy                 return cpus_offset;
22382677ed2SAlexey Kardashevskiy             }
22482677ed2SAlexey Kardashevskiy         }
22582677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
22682677ed2SAlexey Kardashevskiy         if (offset < 0) {
22782677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
22853018216SPaolo Bonzini             if (offset < 0) {
22953018216SPaolo Bonzini                 return offset;
23053018216SPaolo Bonzini             }
23182677ed2SAlexey Kardashevskiy         }
23253018216SPaolo Bonzini 
2330da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
2340da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
23553018216SPaolo Bonzini         if (ret < 0) {
23653018216SPaolo Bonzini             return ret;
23753018216SPaolo Bonzini         }
23853018216SPaolo Bonzini 
2390da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
24053018216SPaolo Bonzini         if (ret < 0) {
24153018216SPaolo Bonzini             return ret;
24253018216SPaolo Bonzini         }
243833d4668SAlexey Kardashevskiy 
24412dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
245833d4668SAlexey Kardashevskiy         if (ret < 0) {
246833d4668SAlexey Kardashevskiy             return ret;
247833d4668SAlexey Kardashevskiy         }
24853018216SPaolo Bonzini     }
24953018216SPaolo Bonzini     return ret;
25053018216SPaolo Bonzini }
25153018216SPaolo Bonzini 
252b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
253b082d65aSAlexey Kardashevskiy {
254fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
255fb164994SDavid Gibson 
256b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
257b082d65aSAlexey Kardashevskiy         int i;
258b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
259b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
260fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
261fb164994SDavid Gibson                            machine->ram_size);
262b082d65aSAlexey Kardashevskiy             }
263b082d65aSAlexey Kardashevskiy         }
264b082d65aSAlexey Kardashevskiy     }
265fb164994SDavid Gibson     return machine->ram_size;
266b082d65aSAlexey Kardashevskiy }
267b082d65aSAlexey Kardashevskiy 
268a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
269a1d59c0fSAlexey Kardashevskiy {
270a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
271a1d59c0fSAlexey Kardashevskiy }
27253018216SPaolo Bonzini 
27303d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
27426a8c353SAlexey Kardashevskiy                                        hwaddr size)
27526a8c353SAlexey Kardashevskiy {
27626a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
27726a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
27826a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
279c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
28026a8c353SAlexey Kardashevskiy     };
28126a8c353SAlexey Kardashevskiy     char mem_name[32];
28226a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
28326a8c353SAlexey Kardashevskiy     int off;
28426a8c353SAlexey Kardashevskiy 
28526a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
28626a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
28726a8c353SAlexey Kardashevskiy 
28826a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
28926a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
29026a8c353SAlexey Kardashevskiy     _FDT(off);
29126a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
29226a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
29326a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
29426a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
29526a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
29603d196b7SBharata B Rao     return off;
29726a8c353SAlexey Kardashevskiy }
29826a8c353SAlexey Kardashevskiy 
29928e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
30053018216SPaolo Bonzini {
301fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
3027db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
3037db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
3047db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
3057db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
30653018216SPaolo Bonzini 
3077db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
3087db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
3097db8a127SAlexey Kardashevskiy         nb_nodes = 1;
310fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
3117db8a127SAlexey Kardashevskiy         nodes = &ramnode;
3125fe269b1SPaul Mackerras     }
31353018216SPaolo Bonzini 
3147db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
3157db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
3167db8a127SAlexey Kardashevskiy             continue;
31753018216SPaolo Bonzini         }
318fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
3195fe269b1SPaul Mackerras             node_size = 0;
3205fe269b1SPaul Mackerras         } else {
3217db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
322fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
323fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
3245fe269b1SPaul Mackerras             }
3255fe269b1SPaul Mackerras         }
3267db8a127SAlexey Kardashevskiy         if (!mem_start) {
3277db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
328e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
3297db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
3307db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
3317db8a127SAlexey Kardashevskiy         }
3326010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
3336010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
3346010818cSAlexey Kardashevskiy 
3356010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
3366010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
3376010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
3386010818cSAlexey Kardashevskiy             }
3396010818cSAlexey Kardashevskiy 
3406010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
3416010818cSAlexey Kardashevskiy             node_size -= sizetmp;
3426010818cSAlexey Kardashevskiy             mem_start += sizetmp;
3436010818cSAlexey Kardashevskiy         }
34453018216SPaolo Bonzini     }
34553018216SPaolo Bonzini 
34653018216SPaolo Bonzini     return 0;
34753018216SPaolo Bonzini }
34853018216SPaolo Bonzini 
349230bf719SThomas Huth /* Populate the "ibm,pa-features" property */
350230bf719SThomas Huth static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset)
351230bf719SThomas Huth {
352230bf719SThomas Huth     uint8_t pa_features_206[] = { 6, 0,
353230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
354230bf719SThomas Huth     uint8_t pa_features_207[] = { 24, 0,
355230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
356230bf719SThomas Huth         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
357230bf719SThomas Huth         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
358bac3bf28SThomas Huth         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
359230bf719SThomas Huth     uint8_t *pa_features;
360230bf719SThomas Huth     size_t pa_size;
361230bf719SThomas Huth 
3624cbec30dSThomas Huth     switch (env->mmu_model) {
3634cbec30dSThomas Huth     case POWERPC_MMU_2_06:
3644cbec30dSThomas Huth     case POWERPC_MMU_2_06a:
365230bf719SThomas Huth         pa_features = pa_features_206;
366230bf719SThomas Huth         pa_size = sizeof(pa_features_206);
3674cbec30dSThomas Huth         break;
3684cbec30dSThomas Huth     case POWERPC_MMU_2_07:
3694cbec30dSThomas Huth     case POWERPC_MMU_2_07a:
370230bf719SThomas Huth         pa_features = pa_features_207;
371230bf719SThomas Huth         pa_size = sizeof(pa_features_207);
3724cbec30dSThomas Huth         break;
3734cbec30dSThomas Huth     default:
3744cbec30dSThomas Huth         return;
375230bf719SThomas Huth     }
376230bf719SThomas Huth 
377230bf719SThomas Huth     if (env->ci_large_pages) {
378230bf719SThomas Huth         /*
379230bf719SThomas Huth          * Note: we keep CI large pages off by default because a 64K capable
380230bf719SThomas Huth          * guest provisioned with large pages might otherwise try to map a qemu
381230bf719SThomas Huth          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
382230bf719SThomas Huth          * even if that qemu runs on a 4k host.
383230bf719SThomas Huth          * We dd this bit back here if we are confident this is not an issue
384230bf719SThomas Huth          */
385230bf719SThomas Huth         pa_features[3] |= 0x20;
386230bf719SThomas Huth     }
387bac3bf28SThomas Huth     if (kvmppc_has_cap_htm() && pa_size > 24) {
388bac3bf28SThomas Huth         pa_features[24] |= 0x80;    /* Transactional memory support */
389bac3bf28SThomas Huth     }
390230bf719SThomas Huth 
391230bf719SThomas Huth     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
392230bf719SThomas Huth }
393230bf719SThomas Huth 
3940da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
3950da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
3960da6f3feSBharata B Rao {
3970da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
3980da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
3990da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4000da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
4010da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4020da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
403afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
404afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
4050da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
4060da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
4070da6f3feSBharata B Rao     size_t page_sizes_prop_size;
40822419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
4090da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
41012dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
411af81cf32SBharata B Rao     sPAPRDRConnector *drc;
412af81cf32SBharata B Rao     sPAPRDRConnectorClass *drck;
413af81cf32SBharata B Rao     int drc_index;
414af81cf32SBharata B Rao 
415af81cf32SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
416af81cf32SBharata B Rao     if (drc) {
417af81cf32SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
418af81cf32SBharata B Rao         drc_index = drck->get_index(drc);
419af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
420af81cf32SBharata B Rao     }
4210da6f3feSBharata B Rao 
4220da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
4230da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
4240da6f3feSBharata B Rao 
4250da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
4260da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
4270da6f3feSBharata B Rao                            env->dcache_line_size)));
4280da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
4290da6f3feSBharata B Rao                            env->dcache_line_size)));
4300da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
4310da6f3feSBharata B Rao                            env->icache_line_size)));
4320da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
4330da6f3feSBharata B Rao                            env->icache_line_size)));
4340da6f3feSBharata B Rao 
4350da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
4360da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
4370da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
4380da6f3feSBharata B Rao     } else {
439ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
4400da6f3feSBharata B Rao     }
4410da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
4420da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
4430da6f3feSBharata B Rao                                pcc->l1_icache_size)));
4440da6f3feSBharata B Rao     } else {
445ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
4460da6f3feSBharata B Rao     }
4470da6f3feSBharata B Rao 
4480da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
4490da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
450fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
4510da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
4520da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
4530da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
4540da6f3feSBharata B Rao 
4550da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
4560da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
4570da6f3feSBharata B Rao     }
4580da6f3feSBharata B Rao 
4590da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
4600da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
4610da6f3feSBharata B Rao                           segs, sizeof(segs))));
4620da6f3feSBharata B Rao     }
4630da6f3feSBharata B Rao 
4640da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
4650da6f3feSBharata B Rao      *   0 / no property == no vector extensions
4660da6f3feSBharata B Rao      *   1               == VMX / Altivec available
4670da6f3feSBharata B Rao      *   2               == VSX available */
4680da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
4690da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
4700da6f3feSBharata B Rao 
4710da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
4720da6f3feSBharata B Rao     }
4730da6f3feSBharata B Rao 
4740da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
4750da6f3feSBharata B Rao      *   0 / no property == no DFP
4760da6f3feSBharata B Rao      *   1               == DFP available */
4770da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
4780da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
4790da6f3feSBharata B Rao     }
4800da6f3feSBharata B Rao 
4813654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
4820da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
4830da6f3feSBharata B Rao     if (page_sizes_prop_size) {
4840da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
4850da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
4860da6f3feSBharata B Rao     }
4870da6f3feSBharata B Rao 
488230bf719SThomas Huth     spapr_populate_pa_features(env, fdt, offset);
48990da0d5aSBenjamin Herrenschmidt 
4900da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
49122419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
4920da6f3feSBharata B Rao 
4930da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
4940da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
4950da6f3feSBharata B Rao 
4960da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
4970da6f3feSBharata B Rao 
49812dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
4990da6f3feSBharata B Rao }
5000da6f3feSBharata B Rao 
5010da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
5020da6f3feSBharata B Rao {
5030da6f3feSBharata B Rao     CPUState *cs;
5040da6f3feSBharata B Rao     int cpus_offset;
5050da6f3feSBharata B Rao     char *nodename;
5060da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
5070da6f3feSBharata B Rao 
5080da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
5090da6f3feSBharata B Rao     _FDT(cpus_offset);
5100da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
5110da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
5120da6f3feSBharata B Rao 
5130da6f3feSBharata B Rao     /*
5140da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
5150da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
5160da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
5170da6f3feSBharata B Rao      */
5180da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
5190da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
5200da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
5210da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
5220da6f3feSBharata B Rao         int offset;
5230da6f3feSBharata B Rao 
5240da6f3feSBharata B Rao         if ((index % smt) != 0) {
5250da6f3feSBharata B Rao             continue;
5260da6f3feSBharata B Rao         }
5270da6f3feSBharata B Rao 
5280da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
5290da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
5300da6f3feSBharata B Rao         g_free(nodename);
5310da6f3feSBharata B Rao         _FDT(offset);
5320da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
5330da6f3feSBharata B Rao     }
5340da6f3feSBharata B Rao 
5350da6f3feSBharata B Rao }
5360da6f3feSBharata B Rao 
53703d196b7SBharata B Rao /*
53803d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
53903d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
54003d196b7SBharata B Rao  * of this device tree node.
54103d196b7SBharata B Rao  */
54203d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
54303d196b7SBharata B Rao {
54403d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
54503d196b7SBharata B Rao     int ret, i, offset;
54603d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
54703d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
548d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
549d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
550d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
551d0e5a8f2SBharata B Rao                        lmb_size;
55203d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
5536663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
55403d196b7SBharata B Rao 
555ef001f06SThomas Huth     /*
556d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
55716c25aefSBharata B Rao      */
558d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
55916c25aefSBharata B Rao         return 0;
56016c25aefSBharata B Rao     }
56116c25aefSBharata B Rao 
56216c25aefSBharata B Rao     /*
563ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
564ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
565ef001f06SThomas Huth      */
566ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
567ef001f06SThomas Huth               * sizeof(uint32_t);
56803d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
56903d196b7SBharata B Rao 
57003d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
57103d196b7SBharata B Rao 
57203d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
57303d196b7SBharata B Rao                     sizeof(prop_lmb_size));
57403d196b7SBharata B Rao     if (ret < 0) {
57503d196b7SBharata B Rao         goto out;
57603d196b7SBharata B Rao     }
57703d196b7SBharata B Rao 
57803d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
57903d196b7SBharata B Rao     if (ret < 0) {
58003d196b7SBharata B Rao         goto out;
58103d196b7SBharata B Rao     }
58203d196b7SBharata B Rao 
58303d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
58403d196b7SBharata B Rao     if (ret < 0) {
58503d196b7SBharata B Rao         goto out;
58603d196b7SBharata B Rao     }
58703d196b7SBharata B Rao 
58803d196b7SBharata B Rao     /* ibm,dynamic-memory */
58903d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
59003d196b7SBharata B Rao     cur_index++;
59103d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
592d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
59303d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
59403d196b7SBharata B Rao 
595d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
596d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
597d0e5a8f2SBharata B Rao             sPAPRDRConnectorClass *drck;
598d0e5a8f2SBharata B Rao 
599d0e5a8f2SBharata B Rao             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
60003d196b7SBharata B Rao             g_assert(drc);
60103d196b7SBharata B Rao             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
60203d196b7SBharata B Rao 
60303d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
60403d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
60503d196b7SBharata B Rao             dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
60603d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
60703d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
608d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
60903d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
61003d196b7SBharata B Rao             } else {
61103d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
61203d196b7SBharata B Rao             }
613d0e5a8f2SBharata B Rao         } else {
614d0e5a8f2SBharata B Rao             /*
615d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
616d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
617d0e5a8f2SBharata B Rao              * and as having no valid DRC.
618d0e5a8f2SBharata B Rao              */
619d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
620d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
621d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
622d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
623d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
624d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
625d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
626d0e5a8f2SBharata B Rao         }
62703d196b7SBharata B Rao 
62803d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
62903d196b7SBharata B Rao     }
63003d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
63103d196b7SBharata B Rao     if (ret < 0) {
63203d196b7SBharata B Rao         goto out;
63303d196b7SBharata B Rao     }
63403d196b7SBharata B Rao 
63503d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
63603d196b7SBharata B Rao     cur_index = int_buf;
6376663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
63803d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
63903d196b7SBharata B Rao     cur_index += 2;
6406663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
64103d196b7SBharata B Rao         uint32_t associativity[] = {
64203d196b7SBharata B Rao             cpu_to_be32(0x0),
64303d196b7SBharata B Rao             cpu_to_be32(0x0),
64403d196b7SBharata B Rao             cpu_to_be32(0x0),
64503d196b7SBharata B Rao             cpu_to_be32(i)
64603d196b7SBharata B Rao         };
64703d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
64803d196b7SBharata B Rao         cur_index += 4;
64903d196b7SBharata B Rao     }
65003d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
65103d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
65203d196b7SBharata B Rao out:
65303d196b7SBharata B Rao     g_free(int_buf);
65403d196b7SBharata B Rao     return ret;
65503d196b7SBharata B Rao }
65603d196b7SBharata B Rao 
6576787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
6586787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
6596787d27bSMichael Roth {
6606787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
661417ece33SMichael Roth     int ret = 0, offset;
6626787d27bSMichael Roth 
6636787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
6646787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
6656787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
6666787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
667417ece33SMichael Roth         if (ret) {
668417ece33SMichael Roth             goto out;
669417ece33SMichael Roth         }
6706787d27bSMichael Roth     }
6716787d27bSMichael Roth 
672417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
673417ece33SMichael Roth     if (offset < 0) {
674417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
675417ece33SMichael Roth         if (offset < 0) {
676417ece33SMichael Roth             return offset;
677417ece33SMichael Roth         }
678417ece33SMichael Roth     }
679417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
680417ece33SMichael Roth                                  "ibm,architecture-vec-5");
681417ece33SMichael Roth 
682417ece33SMichael Roth out:
6836787d27bSMichael Roth     return ret;
6846787d27bSMichael Roth }
6856787d27bSMichael Roth 
68603d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
68703d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
6886787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
68903d196b7SBharata B Rao {
69003d196b7SBharata B Rao     void *fdt, *fdt_skel;
69103d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
69203d196b7SBharata B Rao 
69303d196b7SBharata B Rao     size -= sizeof(hdr);
69403d196b7SBharata B Rao 
69503d196b7SBharata B Rao     /* Create sceleton */
69603d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
69703d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
69803d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
69903d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
70003d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
70103d196b7SBharata B Rao     fdt = g_malloc0(size);
70203d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
70303d196b7SBharata B Rao     g_free(fdt_skel);
70403d196b7SBharata B Rao 
70503d196b7SBharata B Rao     /* Fixup cpu nodes */
70603d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
70703d196b7SBharata B Rao 
7086787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
7096787d27bSMichael Roth         return -1;
71003d196b7SBharata B Rao     }
71103d196b7SBharata B Rao 
71203d196b7SBharata B Rao     /* Pack resulting tree */
71303d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
71403d196b7SBharata B Rao 
71503d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
71603d196b7SBharata B Rao         trace_spapr_cas_failed(size);
71703d196b7SBharata B Rao         return -1;
71803d196b7SBharata B Rao     }
71903d196b7SBharata B Rao 
72003d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
72103d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
72203d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
72303d196b7SBharata B Rao     g_free(fdt);
72403d196b7SBharata B Rao 
72503d196b7SBharata B Rao     return 0;
72603d196b7SBharata B Rao }
72703d196b7SBharata B Rao 
7283f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
7293f5dabceSDavid Gibson {
7303f5dabceSDavid Gibson     int rtas;
7313f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
7323f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
7333f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
7343f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
7353f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
7363f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
7373f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
7383f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
7393f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
7403f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
7413f5dabceSDavid Gibson     };
7423f5dabceSDavid Gibson 
7433f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
7443f5dabceSDavid Gibson 
7453f5dabceSDavid Gibson     /* hypertas */
7463f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
7473f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
7483f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
7493f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
7503f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
7513f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
7523f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
7533f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
7543f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
7553f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
7563f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
7573f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
7583f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
7593f5dabceSDavid Gibson 
7603f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
7613f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
7623f5dabceSDavid Gibson     }
7633f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
7643f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
7653f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
7663f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
7673f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
7683f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
7693f5dabceSDavid Gibson 
7703f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
7713f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
7723f5dabceSDavid Gibson 
7733f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
7743f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
7753f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
7763f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
7773f5dabceSDavid Gibson 
7783f5dabceSDavid Gibson     if (msi_nonbroken) {
7793f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
7803f5dabceSDavid Gibson     }
7813f5dabceSDavid Gibson 
7823f5dabceSDavid Gibson     /*
7833f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
7843f5dabceSDavid Gibson      * back to the guest cpu.
7853f5dabceSDavid Gibson      *
7863f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
7873f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
7883f5dabceSDavid Gibson      */
7893f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
7903f5dabceSDavid Gibson 
7913f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
7923f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
7933f5dabceSDavid Gibson 
7943f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
7953f5dabceSDavid Gibson }
7963f5dabceSDavid Gibson 
7977c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
7987c866c6aSDavid Gibson {
7997c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
8007c866c6aSDavid Gibson     int chosen;
8017c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
8027c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
8037c866c6aSDavid Gibson     size_t cb = 0;
8047c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
8057c866c6aSDavid Gibson 
8067c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
8077c866c6aSDavid Gibson 
8087c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
8097c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
8107c866c6aSDavid Gibson                           spapr->initrd_base));
8117c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
8127c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
8137c866c6aSDavid Gibson 
8147c866c6aSDavid Gibson     if (spapr->kernel_size) {
8157c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
8167c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
8177c866c6aSDavid Gibson 
8187c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
8197c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
8207c866c6aSDavid Gibson         if (spapr->kernel_le) {
8217c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
8227c866c6aSDavid Gibson         }
8237c866c6aSDavid Gibson     }
8247c866c6aSDavid Gibson     if (boot_menu) {
8257c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
8267c866c6aSDavid Gibson     }
8277c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
8287c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
8297c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
8307c866c6aSDavid Gibson 
8317c866c6aSDavid Gibson     if (cb && bootlist) {
8327c866c6aSDavid Gibson         int i;
8337c866c6aSDavid Gibson 
8347c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
8357c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
8367c866c6aSDavid Gibson                 bootlist[i] = ' ';
8377c866c6aSDavid Gibson             }
8387c866c6aSDavid Gibson         }
8397c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
8407c866c6aSDavid Gibson     }
8417c866c6aSDavid Gibson 
8427c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
8437c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
8447c866c6aSDavid Gibson     }
8457c866c6aSDavid Gibson 
8467c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
8477c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
8487c866c6aSDavid Gibson     }
8497c866c6aSDavid Gibson 
8507c866c6aSDavid Gibson     g_free(stdout_path);
8517c866c6aSDavid Gibson     g_free(bootlist);
8527c866c6aSDavid Gibson }
8537c866c6aSDavid Gibson 
854fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
855fca5f2dcSDavid Gibson {
856fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
857fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
858fca5f2dcSDavid Gibson     int hypervisor;
859fca5f2dcSDavid Gibson     uint8_t hypercall[16];
860fca5f2dcSDavid Gibson 
861fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
862fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
863fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
864fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
865fca5f2dcSDavid Gibson         /*
866fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
867fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
868fca5f2dcSDavid Gibson          */
869fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
870fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
871fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
872fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
873fca5f2dcSDavid Gibson         }
874fca5f2dcSDavid Gibson     }
875fca5f2dcSDavid Gibson }
876fca5f2dcSDavid Gibson 
877997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
87853018216SPaolo Bonzini                              hwaddr rtas_addr,
87953018216SPaolo Bonzini                              hwaddr rtas_size)
88053018216SPaolo Bonzini {
8815b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
8823c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
883c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
8847c866c6aSDavid Gibson     int ret;
88553018216SPaolo Bonzini     void *fdt;
88653018216SPaolo Bonzini     sPAPRPHBState *phb;
887398a0bd5SDavid Gibson     char *buf;
88853018216SPaolo Bonzini 
889398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
890398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
89153018216SPaolo Bonzini 
892398a0bd5SDavid Gibson     /* Root node */
893398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
894398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
895398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
896398a0bd5SDavid Gibson 
897398a0bd5SDavid Gibson     /*
898398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
899398a0bd5SDavid Gibson      * and what is the uuid of the guest
900398a0bd5SDavid Gibson      */
901398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
902398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
903398a0bd5SDavid Gibson         g_free(buf);
904398a0bd5SDavid Gibson     }
905398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
906398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
907398a0bd5SDavid Gibson         g_free(buf);
908398a0bd5SDavid Gibson     }
909398a0bd5SDavid Gibson 
910398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
911398a0bd5SDavid Gibson 
912398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
913398a0bd5SDavid Gibson     if (qemu_uuid_set) {
914398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
915398a0bd5SDavid Gibson     }
916398a0bd5SDavid Gibson     g_free(buf);
917398a0bd5SDavid Gibson 
918398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
919398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
920398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
921398a0bd5SDavid Gibson     }
922398a0bd5SDavid Gibson 
923398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
924398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
92553018216SPaolo Bonzini 
9269b9a1908SDavid Gibson     /* /interrupt controller */
9279b9a1908SDavid Gibson     spapr_dt_xics(spapr->xics, fdt, PHANDLE_XICP);
9289b9a1908SDavid Gibson 
929e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
930e8f986fcSBharata B Rao     if (ret < 0) {
931ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
932e8f986fcSBharata B Rao         exit(1);
93353018216SPaolo Bonzini     }
93453018216SPaolo Bonzini 
935bf5a6696SDavid Gibson     /* /vdevice */
936bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
93753018216SPaolo Bonzini 
9384d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
9394d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
9404d9392beSThomas Huth         if (ret < 0) {
941ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
9424d9392beSThomas Huth             exit(1);
9434d9392beSThomas Huth         }
9444d9392beSThomas Huth     }
9454d9392beSThomas Huth 
94653018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
94753018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
94853018216SPaolo Bonzini         if (ret < 0) {
949da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
95053018216SPaolo Bonzini             exit(1);
95153018216SPaolo Bonzini         }
952da34fed7SThomas Huth     }
95353018216SPaolo Bonzini 
9540da6f3feSBharata B Rao     /* cpus */
9550da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
95653018216SPaolo Bonzini 
957c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
958c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
959c20d332aSBharata B Rao     }
960c20d332aSBharata B Rao 
961c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
962af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
963af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
964af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
965af81cf32SBharata B Rao         if (ret < 0) {
966af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
967af81cf32SBharata B Rao             exit(1);
968af81cf32SBharata B Rao         }
969af81cf32SBharata B Rao     }
970af81cf32SBharata B Rao 
971ffb1e275SDavid Gibson     /* /event-sources */
972ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
973ffb1e275SDavid Gibson 
9743f5dabceSDavid Gibson     /* /rtas */
9753f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
9763f5dabceSDavid Gibson 
9777c866c6aSDavid Gibson     /* /chosen */
9787c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
979cf6e5223SDavid Gibson 
980fca5f2dcSDavid Gibson     /* /hypervisor */
981fca5f2dcSDavid Gibson     if (kvm_enabled()) {
982fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
983fca5f2dcSDavid Gibson     }
984fca5f2dcSDavid Gibson 
985cf6e5223SDavid Gibson     /* Build memory reserve map */
986cf6e5223SDavid Gibson     if (spapr->kernel_size) {
987cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
988cf6e5223SDavid Gibson     }
989cf6e5223SDavid Gibson     if (spapr->initrd_size) {
990cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
991cf6e5223SDavid Gibson     }
992cf6e5223SDavid Gibson 
9936787d27bSMichael Roth     /* ibm,client-architecture-support updates */
9946787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
9956787d27bSMichael Roth     if (ret < 0) {
9966787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
9976787d27bSMichael Roth         exit(1);
9986787d27bSMichael Roth     }
9996787d27bSMichael Roth 
1000997b6cfcSDavid Gibson     return fdt;
100153018216SPaolo Bonzini }
100253018216SPaolo Bonzini 
100353018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
100453018216SPaolo Bonzini {
100553018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
100653018216SPaolo Bonzini }
100753018216SPaolo Bonzini 
10081d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
10091d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
101053018216SPaolo Bonzini {
101153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
101253018216SPaolo Bonzini 
10138d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
10148d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
10158d04fb55SJan Kiszka 
101653018216SPaolo Bonzini     if (msr_pr) {
101753018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
101853018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
101953018216SPaolo Bonzini     } else {
102053018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
102153018216SPaolo Bonzini     }
102253018216SPaolo Bonzini }
102353018216SPaolo Bonzini 
1024e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1025e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1026e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1027e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1028e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1029e6b8fd24SSamuel Mendoza-Jonas 
1030715c5407SDavid Gibson /*
1031715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1032715c5407SDavid Gibson  */
1033715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1034715c5407SDavid Gibson {
1035715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1036715c5407SDavid Gibson         return spapr->htab_fd;
1037715c5407SDavid Gibson     }
1038715c5407SDavid Gibson 
1039715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1040715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1041715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1042715c5407SDavid Gibson                      strerror(errno));
1043715c5407SDavid Gibson     }
1044715c5407SDavid Gibson 
1045715c5407SDavid Gibson     return spapr->htab_fd;
1046715c5407SDavid Gibson }
1047715c5407SDavid Gibson 
1048715c5407SDavid Gibson static void close_htab_fd(sPAPRMachineState *spapr)
1049715c5407SDavid Gibson {
1050715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1051715c5407SDavid Gibson         close(spapr->htab_fd);
1052715c5407SDavid Gibson     }
1053715c5407SDavid Gibson     spapr->htab_fd = -1;
1054715c5407SDavid Gibson }
1055715c5407SDavid Gibson 
10568dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
10578dfe8e7fSDavid Gibson {
10588dfe8e7fSDavid Gibson     int shift;
10598dfe8e7fSDavid Gibson 
10608dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
10618dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
10628dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
10638dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
10648dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
10658dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
10668dfe8e7fSDavid Gibson     return shift;
10678dfe8e7fSDavid Gibson }
10688dfe8e7fSDavid Gibson 
1069c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1070c5f54f3eSDavid Gibson                                  Error **errp)
107153018216SPaolo Bonzini {
1072c5f54f3eSDavid Gibson     long rc;
107353018216SPaolo Bonzini 
1074c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
1075c5f54f3eSDavid Gibson     g_free(spapr->htab);
1076c5f54f3eSDavid Gibson     spapr->htab = NULL;
1077c5f54f3eSDavid Gibson     spapr->htab_shift = 0;
1078c5f54f3eSDavid Gibson     close_htab_fd(spapr);
107953018216SPaolo Bonzini 
1080c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1081c5f54f3eSDavid Gibson     if (rc < 0) {
1082c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1083c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1084c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1085c5f54f3eSDavid Gibson                          shift);
1086c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1087c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1088c5f54f3eSDavid Gibson     } else if (rc > 0) {
1089c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1090c5f54f3eSDavid Gibson         if (rc != shift) {
1091c5f54f3eSDavid Gibson             error_setg(errp,
1092c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1093c5f54f3eSDavid Gibson                        shift, rc);
10947735fedaSBharata B Rao         }
10957735fedaSBharata B Rao 
109653018216SPaolo Bonzini         spapr->htab_shift = shift;
1097c18ad9a5SDavid Gibson         spapr->htab = NULL;
1098b817772aSBharata B Rao     } else {
1099c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1100c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1101c5f54f3eSDavid Gibson         int i;
110201a57972SSamuel Mendoza-Jonas 
1103c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1104c5f54f3eSDavid Gibson         if (!spapr->htab) {
1105c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1106c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1107c5f54f3eSDavid Gibson             return;
1108b817772aSBharata B Rao         }
1109b817772aSBharata B Rao 
1110c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1111c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1112b817772aSBharata B Rao 
1113c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1114c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
11157735fedaSBharata B Rao         }
111653018216SPaolo Bonzini     }
111753018216SPaolo Bonzini }
111853018216SPaolo Bonzini 
11194f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
11209e3f9733SAlexander Graf {
11219e3f9733SAlexander Graf     bool matched = false;
11229e3f9733SAlexander Graf 
11239e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
11249e3f9733SAlexander Graf         matched = true;
11259e3f9733SAlexander Graf     }
11269e3f9733SAlexander Graf 
11279e3f9733SAlexander Graf     if (!matched) {
11289e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
11299e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
11309e3f9733SAlexander Graf         exit(1);
11319e3f9733SAlexander Graf     }
11329e3f9733SAlexander Graf }
11339e3f9733SAlexander Graf 
113453018216SPaolo Bonzini static void ppc_spapr_reset(void)
113553018216SPaolo Bonzini {
1136c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1137c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1138182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1139b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1140cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1141997b6cfcSDavid Gibson     void *fdt;
1142997b6cfcSDavid Gibson     int rc;
1143259186a7SAndreas Färber 
11449e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
11459e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
11469e3f9733SAlexander Graf 
1147c5f54f3eSDavid Gibson     /* Allocate and/or reset the hash page table */
1148c5f54f3eSDavid Gibson     spapr_reallocate_hpt(spapr,
1149c5f54f3eSDavid Gibson                          spapr_hpt_shift_for_ramsize(machine->maxram_size),
1150c5f54f3eSDavid Gibson                          &error_fatal);
1151c5f54f3eSDavid Gibson 
1152c5f54f3eSDavid Gibson     /* Update the RMA size if necessary */
1153c5f54f3eSDavid Gibson     if (spapr->vrma_adjust) {
1154c5f54f3eSDavid Gibson         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1155c5f54f3eSDavid Gibson                                           spapr->htab_shift);
1156c5f54f3eSDavid Gibson     }
115753018216SPaolo Bonzini 
115853018216SPaolo Bonzini     qemu_devices_reset();
115953018216SPaolo Bonzini 
1160b7d1f77aSBenjamin Herrenschmidt     /*
1161b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1162b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1163b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1164b7d1f77aSBenjamin Herrenschmidt      */
1165b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1166cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1167cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1168b7d1f77aSBenjamin Herrenschmidt 
11696787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
11706787d27bSMichael Roth      * negotiated options and start from scratch */
11716787d27bSMichael Roth     if (!spapr->cas_reboot) {
11726787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
11736787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
11746787d27bSMichael Roth     }
11756787d27bSMichael Roth 
1176cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
117753018216SPaolo Bonzini 
11782cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1179b7d1f77aSBenjamin Herrenschmidt 
1180997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1181997b6cfcSDavid Gibson 
1182997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1183997b6cfcSDavid Gibson     assert(rc == 0);
1184997b6cfcSDavid Gibson 
1185997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1186997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1187997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1188997b6cfcSDavid Gibson         exit(1);
1189997b6cfcSDavid Gibson     }
1190997b6cfcSDavid Gibson 
1191997b6cfcSDavid Gibson     /* Load the fdt */
1192997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1193cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1194997b6cfcSDavid Gibson     g_free(fdt);
1195997b6cfcSDavid Gibson 
119653018216SPaolo Bonzini     /* Set up the entry state */
1197182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1198cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1199182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1200182735efSAndreas Färber     first_cpu->halted = 0;
12011b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
120253018216SPaolo Bonzini 
12036787d27bSMichael Roth     spapr->cas_reboot = false;
120453018216SPaolo Bonzini }
120553018216SPaolo Bonzini 
120628e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
120753018216SPaolo Bonzini {
12082ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
12093978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
121053018216SPaolo Bonzini 
12113978b863SPaolo Bonzini     if (dinfo) {
12126231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
12136231a6daSMarkus Armbruster                             &error_fatal);
121453018216SPaolo Bonzini     }
121553018216SPaolo Bonzini 
121653018216SPaolo Bonzini     qdev_init_nofail(dev);
121753018216SPaolo Bonzini 
121853018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
121953018216SPaolo Bonzini }
122053018216SPaolo Bonzini 
122128e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
122228df36a1SDavid Gibson {
122328df36a1SDavid Gibson     DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
122428df36a1SDavid Gibson 
122528df36a1SDavid Gibson     qdev_init_nofail(dev);
122628df36a1SDavid Gibson     spapr->rtc = dev;
122774e5ae28SDavid Gibson 
122874e5ae28SDavid Gibson     object_property_add_alias(qdev_get_machine(), "rtc-time",
122974e5ae28SDavid Gibson                               OBJECT(spapr->rtc), "date", NULL);
123028df36a1SDavid Gibson }
123128df36a1SDavid Gibson 
123253018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
123314c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
123453018216SPaolo Bonzini {
123553018216SPaolo Bonzini     switch (vga_interface_type) {
123653018216SPaolo Bonzini     case VGA_NONE:
12377effdaa3SMark Wu         return false;
12387effdaa3SMark Wu     case VGA_DEVICE:
12397effdaa3SMark Wu         return true;
124053018216SPaolo Bonzini     case VGA_STD:
1241b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
124253018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
124353018216SPaolo Bonzini     default:
124414c6a894SDavid Gibson         error_setg(errp,
124514c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
124614c6a894SDavid Gibson         return false;
124753018216SPaolo Bonzini     }
124853018216SPaolo Bonzini }
124953018216SPaolo Bonzini 
1250880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1251880ae7deSDavid Gibson {
125228e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1253880ae7deSDavid Gibson     int err = 0;
1254880ae7deSDavid Gibson 
1255631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1256880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1257880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1258880ae7deSDavid Gibson      * value into the RTC device */
1259880ae7deSDavid Gibson     if (version_id < 3) {
1260880ae7deSDavid Gibson         err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1261880ae7deSDavid Gibson     }
1262880ae7deSDavid Gibson 
1263880ae7deSDavid Gibson     return err;
1264880ae7deSDavid Gibson }
1265880ae7deSDavid Gibson 
1266880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1267880ae7deSDavid Gibson {
1268880ae7deSDavid Gibson     return version_id < 3;
1269880ae7deSDavid Gibson }
1270880ae7deSDavid Gibson 
127162ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
127262ef3760SMichael Roth {
127362ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
127462ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
127562ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
127662ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
127762ef3760SMichael Roth     bool cas_needed;
127862ef3760SMichael Roth 
127962ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
128062ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
128162ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
128262ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
128362ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
128462ef3760SMichael Roth      * negotiatied on the source side.
128562ef3760SMichael Roth      *
128662ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
128762ef3760SMichael Roth      * are the only options available on the current machine/platform.
128862ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
128962ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
129062ef3760SMichael Roth      * compatibility.
129162ef3760SMichael Roth      *
129262ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
129362ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
129462ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
129562ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
129662ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
129762ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
129862ef3760SMichael Roth      *
129962ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
130062ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
130162ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
130262ef3760SMichael Roth      */
130362ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
130462ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
130562ef3760SMichael Roth 
130662ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
130762ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
130862ef3760SMichael Roth      * removed via machine options, which could generate a false positive
130962ef3760SMichael Roth      * that breaks migration.
131062ef3760SMichael Roth      */
131162ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
131262ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
131362ef3760SMichael Roth 
131462ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
131562ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
131662ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
131762ef3760SMichael Roth 
131862ef3760SMichael Roth     return cas_needed;
131962ef3760SMichael Roth }
132062ef3760SMichael Roth 
132162ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
132262ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
132362ef3760SMichael Roth     .version_id = 1,
132462ef3760SMichael Roth     .minimum_version_id = 1,
132562ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
132662ef3760SMichael Roth     .fields = (VMStateField[]) {
132762ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
132862ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
132962ef3760SMichael Roth         VMSTATE_END_OF_LIST()
133062ef3760SMichael Roth     },
133162ef3760SMichael Roth };
133262ef3760SMichael Roth 
13334be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
13344be21d56SDavid Gibson     .name = "spapr",
1335880ae7deSDavid Gibson     .version_id = 3,
13364be21d56SDavid Gibson     .minimum_version_id = 1,
1337880ae7deSDavid Gibson     .post_load = spapr_post_load,
13384be21d56SDavid Gibson     .fields = (VMStateField[]) {
1339880ae7deSDavid Gibson         /* used to be @next_irq */
1340880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
13414be21d56SDavid Gibson 
13424be21d56SDavid Gibson         /* RTC offset */
134328e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1344880ae7deSDavid Gibson 
134528e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
13464be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
13474be21d56SDavid Gibson     },
134862ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
134962ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
135062ef3760SMichael Roth         NULL
135162ef3760SMichael Roth     }
13524be21d56SDavid Gibson };
13534be21d56SDavid Gibson 
13544be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
13554be21d56SDavid Gibson {
135628e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
13574be21d56SDavid Gibson 
13584be21d56SDavid Gibson     /* "Iteration" header */
13594be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
13604be21d56SDavid Gibson 
1361e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1362e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1363e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1364e68cb8b4SAlexey Kardashevskiy     } else {
1365e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
13664be21d56SDavid Gibson     }
13674be21d56SDavid Gibson 
1368e68cb8b4SAlexey Kardashevskiy 
1369e68cb8b4SAlexey Kardashevskiy     return 0;
1370e68cb8b4SAlexey Kardashevskiy }
13714be21d56SDavid Gibson 
137228e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
13734be21d56SDavid Gibson                                  int64_t max_ns)
13744be21d56SDavid Gibson {
1375378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
13764be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
13774be21d56SDavid Gibson     int index = spapr->htab_save_index;
1378bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
13794be21d56SDavid Gibson 
13804be21d56SDavid Gibson     assert(spapr->htab_first_pass);
13814be21d56SDavid Gibson 
13824be21d56SDavid Gibson     do {
13834be21d56SDavid Gibson         int chunkstart;
13844be21d56SDavid Gibson 
13854be21d56SDavid Gibson         /* Consume invalid HPTEs */
13864be21d56SDavid Gibson         while ((index < htabslots)
13874be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
13884be21d56SDavid Gibson             index++;
13894be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13904be21d56SDavid Gibson         }
13914be21d56SDavid Gibson 
13924be21d56SDavid Gibson         /* Consume valid HPTEs */
13934be21d56SDavid Gibson         chunkstart = index;
1394338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
13954be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
13964be21d56SDavid Gibson             index++;
13974be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13984be21d56SDavid Gibson         }
13994be21d56SDavid Gibson 
14004be21d56SDavid Gibson         if (index > chunkstart) {
14014be21d56SDavid Gibson             int n_valid = index - chunkstart;
14024be21d56SDavid Gibson 
14034be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
14044be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
14054be21d56SDavid Gibson             qemu_put_be16(f, 0);
14064be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
14074be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
14084be21d56SDavid Gibson 
1409378bc217SDavid Gibson             if (has_timeout &&
1410378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
14114be21d56SDavid Gibson                 break;
14124be21d56SDavid Gibson             }
14134be21d56SDavid Gibson         }
14144be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
14154be21d56SDavid Gibson 
14164be21d56SDavid Gibson     if (index >= htabslots) {
14174be21d56SDavid Gibson         assert(index == htabslots);
14184be21d56SDavid Gibson         index = 0;
14194be21d56SDavid Gibson         spapr->htab_first_pass = false;
14204be21d56SDavid Gibson     }
14214be21d56SDavid Gibson     spapr->htab_save_index = index;
14224be21d56SDavid Gibson }
14234be21d56SDavid Gibson 
142428e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
14254be21d56SDavid Gibson                                 int64_t max_ns)
14264be21d56SDavid Gibson {
14274be21d56SDavid Gibson     bool final = max_ns < 0;
14284be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
14294be21d56SDavid Gibson     int examined = 0, sent = 0;
14304be21d56SDavid Gibson     int index = spapr->htab_save_index;
1431bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
14324be21d56SDavid Gibson 
14334be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
14344be21d56SDavid Gibson 
14354be21d56SDavid Gibson     do {
14364be21d56SDavid Gibson         int chunkstart, invalidstart;
14374be21d56SDavid Gibson 
14384be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
14394be21d56SDavid Gibson         while ((index < htabslots)
14404be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
14414be21d56SDavid Gibson             index++;
14424be21d56SDavid Gibson             examined++;
14434be21d56SDavid Gibson         }
14444be21d56SDavid Gibson 
14454be21d56SDavid Gibson         chunkstart = index;
14464be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1447338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
14484be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
14494be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
14504be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
14514be21d56SDavid Gibson             index++;
14524be21d56SDavid Gibson             examined++;
14534be21d56SDavid Gibson         }
14544be21d56SDavid Gibson 
14554be21d56SDavid Gibson         invalidstart = index;
14564be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1457338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
14584be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
14594be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
14604be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
14614be21d56SDavid Gibson             index++;
14624be21d56SDavid Gibson             examined++;
14634be21d56SDavid Gibson         }
14644be21d56SDavid Gibson 
14654be21d56SDavid Gibson         if (index > chunkstart) {
14664be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
14674be21d56SDavid Gibson             int n_invalid = index - invalidstart;
14684be21d56SDavid Gibson 
14694be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
14704be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
14714be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
14724be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
14734be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
14744be21d56SDavid Gibson             sent += index - chunkstart;
14754be21d56SDavid Gibson 
1476bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
14774be21d56SDavid Gibson                 break;
14784be21d56SDavid Gibson             }
14794be21d56SDavid Gibson         }
14804be21d56SDavid Gibson 
14814be21d56SDavid Gibson         if (examined >= htabslots) {
14824be21d56SDavid Gibson             break;
14834be21d56SDavid Gibson         }
14844be21d56SDavid Gibson 
14854be21d56SDavid Gibson         if (index >= htabslots) {
14864be21d56SDavid Gibson             assert(index == htabslots);
14874be21d56SDavid Gibson             index = 0;
14884be21d56SDavid Gibson         }
14894be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
14904be21d56SDavid Gibson 
14914be21d56SDavid Gibson     if (index >= htabslots) {
14924be21d56SDavid Gibson         assert(index == htabslots);
14934be21d56SDavid Gibson         index = 0;
14944be21d56SDavid Gibson     }
14954be21d56SDavid Gibson 
14964be21d56SDavid Gibson     spapr->htab_save_index = index;
14974be21d56SDavid Gibson 
1498e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
14994be21d56SDavid Gibson }
15004be21d56SDavid Gibson 
1501e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1502e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1503e68cb8b4SAlexey Kardashevskiy 
15044be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
15054be21d56SDavid Gibson {
150628e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1507715c5407SDavid Gibson     int fd;
1508e68cb8b4SAlexey Kardashevskiy     int rc = 0;
15094be21d56SDavid Gibson 
15104be21d56SDavid Gibson     /* Iteration header */
15114be21d56SDavid Gibson     qemu_put_be32(f, 0);
15124be21d56SDavid Gibson 
1513e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1514e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1515e68cb8b4SAlexey Kardashevskiy 
1516715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1517715c5407SDavid Gibson         if (fd < 0) {
1518715c5407SDavid Gibson             return fd;
151901a57972SSamuel Mendoza-Jonas         }
152001a57972SSamuel Mendoza-Jonas 
1521715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1522e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1523e68cb8b4SAlexey Kardashevskiy             return rc;
1524e68cb8b4SAlexey Kardashevskiy         }
1525e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
15264be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
15274be21d56SDavid Gibson     } else {
1528e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
15294be21d56SDavid Gibson     }
15304be21d56SDavid Gibson 
15314be21d56SDavid Gibson     /* End marker */
15324be21d56SDavid Gibson     qemu_put_be32(f, 0);
15334be21d56SDavid Gibson     qemu_put_be16(f, 0);
15344be21d56SDavid Gibson     qemu_put_be16(f, 0);
15354be21d56SDavid Gibson 
1536e68cb8b4SAlexey Kardashevskiy     return rc;
15374be21d56SDavid Gibson }
15384be21d56SDavid Gibson 
15394be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
15404be21d56SDavid Gibson {
154128e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1542715c5407SDavid Gibson     int fd;
15434be21d56SDavid Gibson 
15444be21d56SDavid Gibson     /* Iteration header */
15454be21d56SDavid Gibson     qemu_put_be32(f, 0);
15464be21d56SDavid Gibson 
1547e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1548e68cb8b4SAlexey Kardashevskiy         int rc;
1549e68cb8b4SAlexey Kardashevskiy 
1550e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1551e68cb8b4SAlexey Kardashevskiy 
1552715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1553715c5407SDavid Gibson         if (fd < 0) {
1554715c5407SDavid Gibson             return fd;
155501a57972SSamuel Mendoza-Jonas         }
155601a57972SSamuel Mendoza-Jonas 
1557715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1558e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1559e68cb8b4SAlexey Kardashevskiy             return rc;
1560e68cb8b4SAlexey Kardashevskiy         }
1561e68cb8b4SAlexey Kardashevskiy     } else {
1562378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1563378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1564378bc217SDavid Gibson         }
15654be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1566e68cb8b4SAlexey Kardashevskiy     }
15674be21d56SDavid Gibson 
15684be21d56SDavid Gibson     /* End marker */
15694be21d56SDavid Gibson     qemu_put_be32(f, 0);
15704be21d56SDavid Gibson     qemu_put_be16(f, 0);
15714be21d56SDavid Gibson     qemu_put_be16(f, 0);
15724be21d56SDavid Gibson 
15734be21d56SDavid Gibson     return 0;
15744be21d56SDavid Gibson }
15754be21d56SDavid Gibson 
15764be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
15774be21d56SDavid Gibson {
157828e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
15794be21d56SDavid Gibson     uint32_t section_hdr;
1580e68cb8b4SAlexey Kardashevskiy     int fd = -1;
15814be21d56SDavid Gibson 
15824be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
158398a5d100SDavid Gibson         error_report("htab_load() bad version");
15844be21d56SDavid Gibson         return -EINVAL;
15854be21d56SDavid Gibson     }
15864be21d56SDavid Gibson 
15874be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
15884be21d56SDavid Gibson 
15894be21d56SDavid Gibson     if (section_hdr) {
15909897e462SGreg Kurz         Error *local_err = NULL;
1591c5f54f3eSDavid Gibson 
1592c5f54f3eSDavid Gibson         /* First section gives the htab size */
1593c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1594c5f54f3eSDavid Gibson         if (local_err) {
1595c5f54f3eSDavid Gibson             error_report_err(local_err);
15964be21d56SDavid Gibson             return -EINVAL;
15974be21d56SDavid Gibson         }
15984be21d56SDavid Gibson         return 0;
15994be21d56SDavid Gibson     }
16004be21d56SDavid Gibson 
1601e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1602e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1603e68cb8b4SAlexey Kardashevskiy 
1604e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1605e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
160698a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1607e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1608e68cb8b4SAlexey Kardashevskiy         }
1609e68cb8b4SAlexey Kardashevskiy     }
1610e68cb8b4SAlexey Kardashevskiy 
16114be21d56SDavid Gibson     while (true) {
16124be21d56SDavid Gibson         uint32_t index;
16134be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
16144be21d56SDavid Gibson 
16154be21d56SDavid Gibson         index = qemu_get_be32(f);
16164be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
16174be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
16184be21d56SDavid Gibson 
16194be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
16204be21d56SDavid Gibson             /* End of Stream */
16214be21d56SDavid Gibson             break;
16224be21d56SDavid Gibson         }
16234be21d56SDavid Gibson 
1624e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
16254be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
16264be21d56SDavid Gibson             /* Bad index in stream */
162798a5d100SDavid Gibson             error_report(
162898a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
162998a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
16304be21d56SDavid Gibson             return -EINVAL;
16314be21d56SDavid Gibson         }
16324be21d56SDavid Gibson 
1633e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
16344be21d56SDavid Gibson             if (n_valid) {
16354be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
16364be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
16374be21d56SDavid Gibson             }
16384be21d56SDavid Gibson             if (n_invalid) {
16394be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
16404be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
16414be21d56SDavid Gibson             }
1642e68cb8b4SAlexey Kardashevskiy         } else {
1643e68cb8b4SAlexey Kardashevskiy             int rc;
1644e68cb8b4SAlexey Kardashevskiy 
1645e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1646e68cb8b4SAlexey Kardashevskiy 
1647e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1648e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1649e68cb8b4SAlexey Kardashevskiy                 return rc;
1650e68cb8b4SAlexey Kardashevskiy             }
1651e68cb8b4SAlexey Kardashevskiy         }
1652e68cb8b4SAlexey Kardashevskiy     }
1653e68cb8b4SAlexey Kardashevskiy 
1654e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1655e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1656e68cb8b4SAlexey Kardashevskiy         close(fd);
16574be21d56SDavid Gibson     }
16584be21d56SDavid Gibson 
16594be21d56SDavid Gibson     return 0;
16604be21d56SDavid Gibson }
16614be21d56SDavid Gibson 
1662c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1663c573fc03SThomas Huth {
1664c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1665c573fc03SThomas Huth 
1666c573fc03SThomas Huth     close_htab_fd(spapr);
1667c573fc03SThomas Huth }
1668c573fc03SThomas Huth 
16694be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
16704be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
16714be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1672a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1673c573fc03SThomas Huth     .cleanup = htab_cleanup,
16744be21d56SDavid Gibson     .load_state = htab_load,
16754be21d56SDavid Gibson };
16764be21d56SDavid Gibson 
16775b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
16785b2128d2SAlexander Graf                            Error **errp)
16795b2128d2SAlexander Graf {
16805b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
16815b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
16825b2128d2SAlexander Graf }
16835b2128d2SAlexander Graf 
1684224245bfSDavid Gibson /*
1685224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1686224245bfSDavid Gibson  *
1687224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1688224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1689224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1690224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1691224245bfSDavid Gibson  */
1692224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1693224245bfSDavid Gibson {
1694224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1695224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1696224245bfSDavid Gibson 
1697224245bfSDavid Gibson     if (d) {
1698224245bfSDavid Gibson         device_reset(d);
1699224245bfSDavid Gibson     }
1700224245bfSDavid Gibson }
1701224245bfSDavid Gibson 
1702224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1703224245bfSDavid Gibson {
1704224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1705224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1706e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1707224245bfSDavid Gibson     int i;
1708224245bfSDavid Gibson 
1709224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1710224245bfSDavid Gibson         sPAPRDRConnector *drc;
1711224245bfSDavid Gibson         uint64_t addr;
1712224245bfSDavid Gibson 
1713e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1714224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1715224245bfSDavid Gibson                                      addr/lmb_size);
1716224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1717224245bfSDavid Gibson     }
1718224245bfSDavid Gibson }
1719224245bfSDavid Gibson 
1720224245bfSDavid Gibson /*
1721224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1722224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1723224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1724224245bfSDavid Gibson  */
17257c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1726224245bfSDavid Gibson {
1727224245bfSDavid Gibson     int i;
1728224245bfSDavid Gibson 
17297c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
17307c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
17317c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
17327c150d6fSDavid Gibson                    machine->ram_size,
1733224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
17347c150d6fSDavid Gibson         return;
17357c150d6fSDavid Gibson     }
17367c150d6fSDavid Gibson 
17377c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
17387c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
17397c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
17407c150d6fSDavid Gibson                    machine->ram_size,
17417c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
17427c150d6fSDavid Gibson         return;
1743224245bfSDavid Gibson     }
1744224245bfSDavid Gibson 
1745224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1746224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
17477c150d6fSDavid Gibson             error_setg(errp,
17487c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
17497c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
17507c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
1751224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
17527c150d6fSDavid Gibson             return;
1753224245bfSDavid Gibson         }
1754224245bfSDavid Gibson     }
1755224245bfSDavid Gibson }
1756224245bfSDavid Gibson 
1757535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
1758535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1759535455fdSIgor Mammedov {
1760535455fdSIgor Mammedov     int index = id / smp_threads;
1761535455fdSIgor Mammedov 
1762535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
1763535455fdSIgor Mammedov         return NULL;
1764535455fdSIgor Mammedov     }
1765535455fdSIgor Mammedov     if (idx) {
1766535455fdSIgor Mammedov         *idx = index;
1767535455fdSIgor Mammedov     }
1768535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
1769535455fdSIgor Mammedov }
1770535455fdSIgor Mammedov 
17710c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
17720c86d0fdSDavid Gibson {
17730c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
17740c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
17750c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
17760c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
1777535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
1778535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
17790c86d0fdSDavid Gibson     int i;
17800c86d0fdSDavid Gibson 
17810c86d0fdSDavid Gibson     if (!type) {
17820c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
17830c86d0fdSDavid Gibson         exit(1);
17840c86d0fdSDavid Gibson     }
17850c86d0fdSDavid Gibson 
1786535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
1787c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
17880c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
17890c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
17900c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
17910c86d0fdSDavid Gibson             exit(1);
17920c86d0fdSDavid Gibson         }
17930c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
17940c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
17950c86d0fdSDavid Gibson                          max_cpus, smp_threads);
17960c86d0fdSDavid Gibson             exit(1);
17970c86d0fdSDavid Gibson         }
17980c86d0fdSDavid Gibson     } else {
17990c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
18000c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
18010c86d0fdSDavid Gibson             exit(1);
18020c86d0fdSDavid Gibson         }
1803535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
18040c86d0fdSDavid Gibson     }
18050c86d0fdSDavid Gibson 
1806535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
18070c86d0fdSDavid Gibson         int core_id = i * smp_threads;
18080c86d0fdSDavid Gibson 
1809c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
18100c86d0fdSDavid Gibson             sPAPRDRConnector *drc =
18110c86d0fdSDavid Gibson                 spapr_dr_connector_new(OBJECT(spapr),
18120c86d0fdSDavid Gibson                                        SPAPR_DR_CONNECTOR_TYPE_CPU,
18130c86d0fdSDavid Gibson                                        (core_id / smp_threads) * smt);
18140c86d0fdSDavid Gibson 
18150c86d0fdSDavid Gibson             qemu_register_reset(spapr_drc_reset, drc);
18160c86d0fdSDavid Gibson         }
18170c86d0fdSDavid Gibson 
1818535455fdSIgor Mammedov         if (i < boot_cores_nr) {
18190c86d0fdSDavid Gibson             Object *core  = object_new(type);
18200c86d0fdSDavid Gibson             int nr_threads = smp_threads;
18210c86d0fdSDavid Gibson 
18220c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
18230c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
18240c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
18250c86d0fdSDavid Gibson             }
18260c86d0fdSDavid Gibson 
18270c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
18280c86d0fdSDavid Gibson                                     &error_fatal);
18290c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
18300c86d0fdSDavid Gibson                                     &error_fatal);
18310c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
18320c86d0fdSDavid Gibson         }
18330c86d0fdSDavid Gibson     }
18340c86d0fdSDavid Gibson     g_free(type);
18350c86d0fdSDavid Gibson }
18360c86d0fdSDavid Gibson 
183753018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
18383ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
183953018216SPaolo Bonzini {
184028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1841224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
18423ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
18433ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
184453018216SPaolo Bonzini     PCIHostState *phb;
184553018216SPaolo Bonzini     int i;
184653018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
184753018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1848658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1849658fa66bSAlexey Kardashevskiy     void *rma = NULL;
185053018216SPaolo Bonzini     hwaddr rma_alloc_size;
1851b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
1852b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
185353018216SPaolo Bonzini     char *filename;
185494a94e4cSBharata B Rao     int smt = kvmppc_smt_threads();
185553018216SPaolo Bonzini 
1856226419d6SMichael S. Tsirkin     msi_nonbroken = true;
185753018216SPaolo Bonzini 
185853018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
185953018216SPaolo Bonzini 
186053018216SPaolo Bonzini     /* Allocate RMA if necessary */
1861658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
186253018216SPaolo Bonzini 
186353018216SPaolo Bonzini     if (rma_alloc_size == -1) {
1864730fce59SThomas Huth         error_report("Unable to create RMA");
186553018216SPaolo Bonzini         exit(1);
186653018216SPaolo Bonzini     }
186753018216SPaolo Bonzini 
1868c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
186953018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
187053018216SPaolo Bonzini     } else {
1871c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
187253018216SPaolo Bonzini 
187353018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
187453018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
187553018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
187653018216SPaolo Bonzini          *
187753018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
187853018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
187953018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
188053018216SPaolo Bonzini          * isn't determined yet.
188153018216SPaolo Bonzini          */
188253018216SPaolo Bonzini         if (kvm_enabled()) {
188353018216SPaolo Bonzini             spapr->vrma_adjust = 1;
188453018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
188553018216SPaolo Bonzini         }
1886912acdf4SBenjamin Herrenschmidt 
1887912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
1888912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
1889912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
1890912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
1891912acdf4SBenjamin Herrenschmidt          */
1892912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
189353018216SPaolo Bonzini     }
189453018216SPaolo Bonzini 
1895c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
1896d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
1897c4177479SAlexey Kardashevskiy                      spapr->rma_size);
1898c4177479SAlexey Kardashevskiy         exit(1);
1899c4177479SAlexey Kardashevskiy     }
1900c4177479SAlexey Kardashevskiy 
1901b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1902b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
190353018216SPaolo Bonzini 
19047b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
190527f24582SBenjamin Herrenschmidt     spapr->xics = xics_system_init(machine,
190694a94e4cSBharata B Rao                                    DIV_ROUND_UP(max_cpus * smt, smp_threads),
1907161deaf2SBenjamin Herrenschmidt                                    XICS_IRQS_SPAPR, &error_fatal);
19087b565160SDavid Gibson 
1909facdb8b6SMichael Roth     /* Set up containers for ibm,client-set-architecture negotiated options */
1910facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
1911facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
1912facdb8b6SMichael Roth 
1913224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
1914facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
19157c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
1916224245bfSDavid Gibson     }
1917224245bfSDavid Gibson 
1918417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
1919417ece33SMichael Roth 
1920ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
1921ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
1922ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
1923ffbb1705SMichael Roth     }
1924ffbb1705SMichael Roth 
192553018216SPaolo Bonzini     /* init CPUs */
192619fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
19273daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
192853018216SPaolo Bonzini     }
192994a94e4cSBharata B Rao 
1930e703d2f7SGreg Kurz     ppc_cpu_parse_features(machine->cpu_model);
1931e703d2f7SGreg Kurz 
19320c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
193353018216SPaolo Bonzini 
1934026bfd89SDavid Gibson     if (kvm_enabled()) {
1935026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
1936026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
1937ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
19385145ad4fSNathan Whitehorn 
19395145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
19405145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
1941026bfd89SDavid Gibson     }
1942026bfd89SDavid Gibson 
194353018216SPaolo Bonzini     /* allocate RAM */
1944f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
1945fb164994SDavid Gibson                                          machine->ram_size);
1946f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
194753018216SPaolo Bonzini 
1948658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
1949658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
1950658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
1951658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
1952658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
1953658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
1954658fa66bSAlexey Kardashevskiy     }
1955658fa66bSAlexey Kardashevskiy 
19564a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
19574a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
19584a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
195971c9a3ddSBharata B Rao         /*
196071c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
196171c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
196271c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
196371c9a3ddSBharata B Rao          */
196471c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
196571c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
19664a1c9cf0SBharata B Rao 
196771c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
196871c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
196971c9a3ddSBharata B Rao         }
197071c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
1971d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
1972d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
197371c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
1974d54e4d76SDavid Gibson             exit(1);
19754a1c9cf0SBharata B Rao         }
19764a1c9cf0SBharata B Rao 
19774a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
19784a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
19794a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
19804a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
19814a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
19824a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
19834a1c9cf0SBharata B Rao     }
19844a1c9cf0SBharata B Rao 
1985224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
1986224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
1987224245bfSDavid Gibson     }
1988224245bfSDavid Gibson 
198953018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
19904c56440dSStefan Weil     if (!filename) {
1991730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
19924c56440dSStefan Weil         exit(1);
19934c56440dSStefan Weil     }
1994b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
19958afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
19968afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
19978afc22a2SZhou Jie         exit(1);
19988afc22a2SZhou Jie     }
1999b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2000b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2001730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
200253018216SPaolo Bonzini         exit(1);
200353018216SPaolo Bonzini     }
200453018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2005730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
20062f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
200753018216SPaolo Bonzini         exit(1);
200853018216SPaolo Bonzini     }
200953018216SPaolo Bonzini     g_free(filename);
201053018216SPaolo Bonzini 
2011ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
201253018216SPaolo Bonzini     spapr_events_init(spapr);
201353018216SPaolo Bonzini 
201412f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
201528df36a1SDavid Gibson     spapr_rtc_create(spapr);
201612f42174SDavid Gibson 
201753018216SPaolo Bonzini     /* Set up VIO bus */
201853018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
201953018216SPaolo Bonzini 
202053018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
202153018216SPaolo Bonzini         if (serial_hds[i]) {
202253018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
202353018216SPaolo Bonzini         }
202453018216SPaolo Bonzini     }
202553018216SPaolo Bonzini 
202653018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
202753018216SPaolo Bonzini     spapr_create_nvram(spapr);
202853018216SPaolo Bonzini 
202953018216SPaolo Bonzini     /* Set up PCI */
203053018216SPaolo Bonzini     spapr_pci_rtas_init();
203153018216SPaolo Bonzini 
203289dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
203353018216SPaolo Bonzini 
203453018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
203553018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
203653018216SPaolo Bonzini 
203753018216SPaolo Bonzini         if (!nd->model) {
203853018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
203953018216SPaolo Bonzini         }
204053018216SPaolo Bonzini 
204153018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
204253018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
204353018216SPaolo Bonzini         } else {
204429b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
204553018216SPaolo Bonzini         }
204653018216SPaolo Bonzini     }
204753018216SPaolo Bonzini 
204853018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
204953018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
205053018216SPaolo Bonzini     }
205153018216SPaolo Bonzini 
205253018216SPaolo Bonzini     /* Graphics */
205314c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
205453018216SPaolo Bonzini         spapr->has_graphics = true;
2055c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
205653018216SPaolo Bonzini     }
205753018216SPaolo Bonzini 
20584ee9ced9SMarcel Apfelbaum     if (machine->usb) {
205957040d45SThomas Huth         if (smc->use_ohci_by_default) {
206053018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
206157040d45SThomas Huth         } else {
206257040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
206357040d45SThomas Huth         }
2064c86580b8SMarkus Armbruster 
206553018216SPaolo Bonzini         if (spapr->has_graphics) {
2066c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2067c86580b8SMarkus Armbruster 
2068c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2069c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
207053018216SPaolo Bonzini         }
207153018216SPaolo Bonzini     }
207253018216SPaolo Bonzini 
207353018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2074d54e4d76SDavid Gibson         error_report(
2075d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2076d54e4d76SDavid Gibson             MIN_RMA_SLOF);
207753018216SPaolo Bonzini         exit(1);
207853018216SPaolo Bonzini     }
207953018216SPaolo Bonzini 
208053018216SPaolo Bonzini     if (kernel_filename) {
208153018216SPaolo Bonzini         uint64_t lowaddr = 0;
208253018216SPaolo Bonzini 
2083a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2084a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2085a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2086a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2087a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2088a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2089a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
20907ef295eaSPeter Crosthwaite                                           0, 0);
2091a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
209216457e7fSBenjamin Herrenschmidt         }
2093a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2094a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2095a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
209653018216SPaolo Bonzini             exit(1);
209753018216SPaolo Bonzini         }
209853018216SPaolo Bonzini 
209953018216SPaolo Bonzini         /* load initrd */
210053018216SPaolo Bonzini         if (initrd_filename) {
210153018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
210253018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
210353018216SPaolo Bonzini              */
2104a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2105a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2106a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2107a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2108a19f7fb0SDavid Gibson                                                      load_limit
2109a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2110a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2111d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
211253018216SPaolo Bonzini                              initrd_filename);
211353018216SPaolo Bonzini                 exit(1);
211453018216SPaolo Bonzini             }
211553018216SPaolo Bonzini         }
211653018216SPaolo Bonzini     }
211753018216SPaolo Bonzini 
21188e7ea787SAndreas Färber     if (bios_name == NULL) {
21198e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
21208e7ea787SAndreas Färber     }
21218e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
21224c56440dSStefan Weil     if (!filename) {
212368fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
21244c56440dSStefan Weil         exit(1);
21254c56440dSStefan Weil     }
212653018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
212768fea5a0SThomas Huth     if (fw_size <= 0) {
212868fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
212953018216SPaolo Bonzini         exit(1);
213053018216SPaolo Bonzini     }
213153018216SPaolo Bonzini     g_free(filename);
213253018216SPaolo Bonzini 
213328e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
213428e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
213528e02042SDavid Gibson      * which predated MachineState but had a similar function */
21364be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
21374be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
21384be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
21394be21d56SDavid Gibson 
214046503c2bSMichael Roth     /* used by RTAS */
214146503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
214246503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
214346503c2bSMichael Roth 
21445b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
214542043e4fSLaurent Vivier 
214642043e4fSLaurent Vivier     /* to stop and start vmclock */
214742043e4fSLaurent Vivier     if (kvm_enabled()) {
214842043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
214942043e4fSLaurent Vivier                                          &spapr->tb);
215042043e4fSLaurent Vivier     }
215153018216SPaolo Bonzini }
215253018216SPaolo Bonzini 
2153135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2154135a129aSAneesh Kumar K.V {
2155135a129aSAneesh Kumar K.V     if (!vm_type) {
2156135a129aSAneesh Kumar K.V         return 0;
2157135a129aSAneesh Kumar K.V     }
2158135a129aSAneesh Kumar K.V 
2159135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2160135a129aSAneesh Kumar K.V         return 1;
2161135a129aSAneesh Kumar K.V     }
2162135a129aSAneesh Kumar K.V 
2163135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2164135a129aSAneesh Kumar K.V         return 2;
2165135a129aSAneesh Kumar K.V     }
2166135a129aSAneesh Kumar K.V 
2167135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2168135a129aSAneesh Kumar K.V     exit(1);
2169135a129aSAneesh Kumar K.V }
2170135a129aSAneesh Kumar K.V 
217171461b0fSAlexey Kardashevskiy /*
2172627b84f4SGonglei  * Implementation of an interface to adjust firmware path
217371461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
217471461b0fSAlexey Kardashevskiy  */
217571461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
217671461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
217771461b0fSAlexey Kardashevskiy {
217871461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
217971461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
218071461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
218171461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
218271461b0fSAlexey Kardashevskiy 
218371461b0fSAlexey Kardashevskiy     if (d) {
218471461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
218571461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
218671461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
218771461b0fSAlexey Kardashevskiy 
218871461b0fSAlexey Kardashevskiy         if (spapr) {
218971461b0fSAlexey Kardashevskiy             /*
219071461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
219171461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
219271461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
219371461b0fSAlexey Kardashevskiy              */
219471461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
219571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
219671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
219771461b0fSAlexey Kardashevskiy         } else if (virtio) {
219871461b0fSAlexey Kardashevskiy             /*
219971461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
220071461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
220171461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
220271461b0fSAlexey Kardashevskiy              * the actual binding is:
220371461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
220471461b0fSAlexey Kardashevskiy              */
220571461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
220671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
220771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
220871461b0fSAlexey Kardashevskiy         } else if (usb) {
220971461b0fSAlexey Kardashevskiy             /*
221071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
221171461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
221271461b0fSAlexey Kardashevskiy              */
221371461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
221471461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
221571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
221671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
221771461b0fSAlexey Kardashevskiy         }
221871461b0fSAlexey Kardashevskiy     }
221971461b0fSAlexey Kardashevskiy 
2220b99260ebSThomas Huth     /*
2221b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2222b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2223b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2224b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2225b99260ebSThomas Huth      */
2226b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2227b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2228b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2229b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2230b99260ebSThomas Huth         }
2231b99260ebSThomas Huth     }
2232b99260ebSThomas Huth 
223371461b0fSAlexey Kardashevskiy     if (phb) {
223471461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
223571461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
223671461b0fSAlexey Kardashevskiy     }
223771461b0fSAlexey Kardashevskiy 
223871461b0fSAlexey Kardashevskiy     return NULL;
223971461b0fSAlexey Kardashevskiy }
224071461b0fSAlexey Kardashevskiy 
224123825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
224223825581SEduardo Habkost {
224328e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
224423825581SEduardo Habkost 
224528e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
224623825581SEduardo Habkost }
224723825581SEduardo Habkost 
224823825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
224923825581SEduardo Habkost {
225028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
225123825581SEduardo Habkost 
225228e02042SDavid Gibson     g_free(spapr->kvm_type);
225328e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
225423825581SEduardo Habkost }
225523825581SEduardo Habkost 
2256f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2257f6229214SMichael Roth {
2258f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2259f6229214SMichael Roth 
2260f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2261f6229214SMichael Roth }
2262f6229214SMichael Roth 
2263f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2264f6229214SMichael Roth                                             Error **errp)
2265f6229214SMichael Roth {
2266f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2267f6229214SMichael Roth 
2268f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2269f6229214SMichael Roth }
2270f6229214SMichael Roth 
227123825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
227223825581SEduardo Habkost {
2273715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2274715c5407SDavid Gibson 
2275715c5407SDavid Gibson     spapr->htab_fd = -1;
2276f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
227723825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
227823825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
227949d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
228049d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
228149d2e648SMarcel Apfelbaum                                     NULL);
2282f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2283f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2284f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2285f6229214SMichael Roth                             NULL);
2286f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2287f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2288f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2289f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2290f6229214SMichael Roth                                     NULL);
229123825581SEduardo Habkost }
229223825581SEduardo Habkost 
229387bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
229487bbdd9cSDavid Gibson {
229587bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
229687bbdd9cSDavid Gibson 
229787bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
229887bbdd9cSDavid Gibson }
229987bbdd9cSDavid Gibson 
23001c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
230134316482SAlexey Kardashevskiy {
230234316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
230334316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
230434316482SAlexey Kardashevskiy }
230534316482SAlexey Kardashevskiy 
230634316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
230734316482SAlexey Kardashevskiy {
230834316482SAlexey Kardashevskiy     CPUState *cs;
230934316482SAlexey Kardashevskiy 
231034316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
23111c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
231234316482SAlexey Kardashevskiy     }
231334316482SAlexey Kardashevskiy }
231434316482SAlexey Kardashevskiy 
231579b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
231679b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
231779b78a6bSMichael Roth                            Error **errp)
2318c20d332aSBharata B Rao {
2319c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2320c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2321c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2322c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2323c20d332aSBharata B Rao     void *fdt;
232479b78a6bSMichael Roth     uint64_t addr = addr_start;
2325c20d332aSBharata B Rao 
2326c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2327c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2328c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2329c20d332aSBharata B Rao         g_assert(drc);
2330c20d332aSBharata B Rao 
2331c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2332c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2333c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2334c20d332aSBharata B Rao 
2335c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2336c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2337c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
23385c0139a8SMichael Roth         if (!dev->hotplugged) {
23395c0139a8SMichael Roth             /* guests expect coldplugged LMBs to be pre-allocated */
23405c0139a8SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
23415c0139a8SMichael Roth             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
23425c0139a8SMichael Roth         }
2343c20d332aSBharata B Rao     }
23445dd5238cSJianjun Duan     /* send hotplug notification to the
23455dd5238cSJianjun Duan      * guest only in case of hotplugged memory
23465dd5238cSJianjun Duan      */
23475dd5238cSJianjun Duan     if (dev->hotplugged) {
234879b78a6bSMichael Roth         if (dedicated_hp_event_source) {
234979b78a6bSMichael Roth             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
235079b78a6bSMichael Roth                     addr_start / SPAPR_MEMORY_BLOCK_SIZE);
235179b78a6bSMichael Roth             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
235279b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
235379b78a6bSMichael Roth                                                    nr_lmbs,
235479b78a6bSMichael Roth                                                    drck->get_index(drc));
235579b78a6bSMichael Roth         } else {
235679b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
235779b78a6bSMichael Roth                                            nr_lmbs);
235879b78a6bSMichael Roth         }
2359c20d332aSBharata B Rao     }
23605dd5238cSJianjun Duan }
2361c20d332aSBharata B Rao 
2362c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2363c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2364c20d332aSBharata B Rao {
2365c20d332aSBharata B Rao     Error *local_err = NULL;
2366c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2367c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2368c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2369c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2370c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2371c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2372c20d332aSBharata B Rao     uint64_t addr;
2373df587133SThomas Huth     char *mem_dev;
2374c20d332aSBharata B Rao 
2375c20d332aSBharata B Rao     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2376c20d332aSBharata B Rao         error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2377c20d332aSBharata B Rao                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2378c20d332aSBharata B Rao         goto out;
2379c20d332aSBharata B Rao     }
2380c20d332aSBharata B Rao 
2381df587133SThomas Huth     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2382df587133SThomas Huth     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2383df587133SThomas Huth         error_setg(&local_err, "Memory backend has bad page size. "
2384df587133SThomas Huth                    "Use 'memory-backend-file' with correct mem-path.");
2385df587133SThomas Huth         goto out;
2386df587133SThomas Huth     }
2387df587133SThomas Huth 
2388d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2389c20d332aSBharata B Rao     if (local_err) {
2390c20d332aSBharata B Rao         goto out;
2391c20d332aSBharata B Rao     }
2392c20d332aSBharata B Rao 
2393c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2394c20d332aSBharata B Rao     if (local_err) {
2395c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2396c20d332aSBharata B Rao         goto out;
2397c20d332aSBharata B Rao     }
2398c20d332aSBharata B Rao 
239979b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
240079b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
240179b78a6bSMichael Roth                    &error_abort);
2402c20d332aSBharata B Rao 
2403c20d332aSBharata B Rao out:
2404c20d332aSBharata B Rao     error_propagate(errp, local_err);
2405c20d332aSBharata B Rao }
2406c20d332aSBharata B Rao 
2407cf632463SBharata B Rao typedef struct sPAPRDIMMState {
2408cf632463SBharata B Rao     uint32_t nr_lmbs;
2409cf632463SBharata B Rao } sPAPRDIMMState;
2410cf632463SBharata B Rao 
2411cf632463SBharata B Rao static void spapr_lmb_release(DeviceState *dev, void *opaque)
2412cf632463SBharata B Rao {
2413cf632463SBharata B Rao     sPAPRDIMMState *ds = (sPAPRDIMMState *)opaque;
2414cf632463SBharata B Rao     HotplugHandler *hotplug_ctrl;
2415cf632463SBharata B Rao 
2416cf632463SBharata B Rao     if (--ds->nr_lmbs) {
2417cf632463SBharata B Rao         return;
2418cf632463SBharata B Rao     }
2419cf632463SBharata B Rao 
2420cf632463SBharata B Rao     g_free(ds);
2421cf632463SBharata B Rao 
2422cf632463SBharata B Rao     /*
2423cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
2424cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
2425cf632463SBharata B Rao      */
2426cf632463SBharata B Rao     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2427cf632463SBharata B Rao     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2428cf632463SBharata B Rao }
2429cf632463SBharata B Rao 
2430cf632463SBharata B Rao static void spapr_del_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
2431cf632463SBharata B Rao                            Error **errp)
2432cf632463SBharata B Rao {
2433cf632463SBharata B Rao     sPAPRDRConnector *drc;
2434cf632463SBharata B Rao     sPAPRDRConnectorClass *drck;
2435cf632463SBharata B Rao     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
2436cf632463SBharata B Rao     int i;
2437cf632463SBharata B Rao     sPAPRDIMMState *ds = g_malloc0(sizeof(sPAPRDIMMState));
2438cf632463SBharata B Rao     uint64_t addr = addr_start;
2439cf632463SBharata B Rao 
2440cf632463SBharata B Rao     ds->nr_lmbs = nr_lmbs;
2441cf632463SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2442cf632463SBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2443cf632463SBharata B Rao                 addr / SPAPR_MEMORY_BLOCK_SIZE);
2444cf632463SBharata B Rao         g_assert(drc);
2445cf632463SBharata B Rao 
2446cf632463SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2447cf632463SBharata B Rao         drck->detach(drc, dev, spapr_lmb_release, ds, errp);
2448cf632463SBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2449cf632463SBharata B Rao     }
2450cf632463SBharata B Rao 
2451cf632463SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2452cf632463SBharata B Rao                                    addr_start / SPAPR_MEMORY_BLOCK_SIZE);
2453cf632463SBharata B Rao     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2454cf632463SBharata B Rao     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
2455cf632463SBharata B Rao                                               nr_lmbs,
2456cf632463SBharata B Rao                                               drck->get_index(drc));
2457cf632463SBharata B Rao }
2458cf632463SBharata B Rao 
2459cf632463SBharata B Rao static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2460cf632463SBharata B Rao                                 Error **errp)
2461cf632463SBharata B Rao {
2462cf632463SBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2463cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2464cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2465cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2466cf632463SBharata B Rao 
2467cf632463SBharata B Rao     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2468cf632463SBharata B Rao     object_unparent(OBJECT(dev));
2469cf632463SBharata B Rao }
2470cf632463SBharata B Rao 
2471cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
2472cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
2473cf632463SBharata B Rao {
2474cf632463SBharata B Rao     Error *local_err = NULL;
2475cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2476cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2477cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2478cf632463SBharata B Rao     uint64_t size = memory_region_size(mr);
2479cf632463SBharata B Rao     uint64_t addr;
2480cf632463SBharata B Rao 
2481cf632463SBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2482cf632463SBharata B Rao     if (local_err) {
2483cf632463SBharata B Rao         goto out;
2484cf632463SBharata B Rao     }
2485cf632463SBharata B Rao 
2486cf632463SBharata B Rao     spapr_del_lmbs(dev, addr, size, &error_abort);
2487cf632463SBharata B Rao out:
2488cf632463SBharata B Rao     error_propagate(errp, local_err);
2489cf632463SBharata B Rao }
2490cf632463SBharata B Rao 
2491af81cf32SBharata B Rao void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2492af81cf32SBharata B Rao                                     sPAPRMachineState *spapr)
2493af81cf32SBharata B Rao {
2494af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2495af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2496af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2497af81cf32SBharata B Rao     void *fdt;
2498af81cf32SBharata B Rao     int offset, fdt_size;
2499af81cf32SBharata B Rao     char *nodename;
2500af81cf32SBharata B Rao 
2501af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2502af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2503af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2504af81cf32SBharata B Rao 
2505af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2506af81cf32SBharata B Rao     g_free(nodename);
2507af81cf32SBharata B Rao 
2508af81cf32SBharata B Rao     *fdt_offset = offset;
2509af81cf32SBharata B Rao     return fdt;
2510af81cf32SBharata B Rao }
2511af81cf32SBharata B Rao 
2512115debf2SIgor Mammedov static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2513115debf2SIgor Mammedov                               Error **errp)
2514ff9006ddSIgor Mammedov {
2515535455fdSIgor Mammedov     MachineState *ms = MACHINE(qdev_get_machine());
2516ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2517535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
2518ff9006ddSIgor Mammedov 
2519535455fdSIgor Mammedov     core_slot->cpu = NULL;
2520ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
2521ff9006ddSIgor Mammedov }
2522ff9006ddSIgor Mammedov 
2523115debf2SIgor Mammedov static void spapr_core_release(DeviceState *dev, void *opaque)
2524115debf2SIgor Mammedov {
2525115debf2SIgor Mammedov     HotplugHandler *hotplug_ctrl;
2526115debf2SIgor Mammedov 
2527115debf2SIgor Mammedov     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2528115debf2SIgor Mammedov     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2529115debf2SIgor Mammedov }
2530115debf2SIgor Mammedov 
2531115debf2SIgor Mammedov static
2532115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
2533ff9006ddSIgor Mammedov                                Error **errp)
2534ff9006ddSIgor Mammedov {
2535535455fdSIgor Mammedov     int index;
2536535455fdSIgor Mammedov     sPAPRDRConnector *drc;
2537ff9006ddSIgor Mammedov     sPAPRDRConnectorClass *drck;
2538ff9006ddSIgor Mammedov     Error *local_err = NULL;
2539535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2540535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
2541ff9006ddSIgor Mammedov 
2542535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
2543535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2544535455fdSIgor Mammedov                    cc->core_id);
2545535455fdSIgor Mammedov         return;
2546535455fdSIgor Mammedov     }
2547ff9006ddSIgor Mammedov     if (index == 0) {
2548ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
2549ff9006ddSIgor Mammedov         return;
2550ff9006ddSIgor Mammedov     }
2551ff9006ddSIgor Mammedov 
2552535455fdSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2553ff9006ddSIgor Mammedov     g_assert(drc);
2554ff9006ddSIgor Mammedov 
2555ff9006ddSIgor Mammedov     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2556ff9006ddSIgor Mammedov     drck->detach(drc, dev, spapr_core_release, NULL, &local_err);
2557ff9006ddSIgor Mammedov     if (local_err) {
2558ff9006ddSIgor Mammedov         error_propagate(errp, local_err);
2559ff9006ddSIgor Mammedov         return;
2560ff9006ddSIgor Mammedov     }
2561ff9006ddSIgor Mammedov 
2562ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
2563ff9006ddSIgor Mammedov }
2564ff9006ddSIgor Mammedov 
2565ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2566ff9006ddSIgor Mammedov                             Error **errp)
2567ff9006ddSIgor Mammedov {
2568ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
2569ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
2570ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
2571ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2572ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
2573ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
2574ff9006ddSIgor Mammedov     Error *local_err = NULL;
2575ff9006ddSIgor Mammedov     void *fdt = NULL;
2576ff9006ddSIgor Mammedov     int fdt_offset = 0;
2577ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
2578535455fdSIgor Mammedov     CPUArchId *core_slot;
2579535455fdSIgor Mammedov     int index;
2580ff9006ddSIgor Mammedov 
2581535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2582535455fdSIgor Mammedov     if (!core_slot) {
2583535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2584535455fdSIgor Mammedov                    cc->core_id);
2585535455fdSIgor Mammedov         return;
2586535455fdSIgor Mammedov     }
2587ff9006ddSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2588ff9006ddSIgor Mammedov 
2589c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
2590ff9006ddSIgor Mammedov 
2591ff9006ddSIgor Mammedov     /*
2592ff9006ddSIgor Mammedov      * Setup CPU DT entries only for hotplugged CPUs. For boot time or
2593ff9006ddSIgor Mammedov      * coldplugged CPUs DT entries are setup in spapr_build_fdt().
2594ff9006ddSIgor Mammedov      */
2595ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2596ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
2597ff9006ddSIgor Mammedov     }
2598ff9006ddSIgor Mammedov 
2599ff9006ddSIgor Mammedov     if (drc) {
2600ff9006ddSIgor Mammedov         sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2601ff9006ddSIgor Mammedov         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, &local_err);
2602ff9006ddSIgor Mammedov         if (local_err) {
2603ff9006ddSIgor Mammedov             g_free(fdt);
2604ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
2605ff9006ddSIgor Mammedov             return;
2606ff9006ddSIgor Mammedov         }
2607ff9006ddSIgor Mammedov     }
2608ff9006ddSIgor Mammedov 
2609ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2610ff9006ddSIgor Mammedov         /*
2611ff9006ddSIgor Mammedov          * Send hotplug notification interrupt to the guest only in case
2612ff9006ddSIgor Mammedov          * of hotplugged CPUs.
2613ff9006ddSIgor Mammedov          */
2614ff9006ddSIgor Mammedov         spapr_hotplug_req_add_by_index(drc);
2615ff9006ddSIgor Mammedov     } else {
2616ff9006ddSIgor Mammedov         /*
2617ff9006ddSIgor Mammedov          * Set the right DRC states for cold plugged CPU.
2618ff9006ddSIgor Mammedov          */
2619ff9006ddSIgor Mammedov         if (drc) {
2620ff9006ddSIgor Mammedov             sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2621ff9006ddSIgor Mammedov             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
2622ff9006ddSIgor Mammedov             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
2623ff9006ddSIgor Mammedov         }
2624ff9006ddSIgor Mammedov     }
2625535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
2626ff9006ddSIgor Mammedov }
2627ff9006ddSIgor Mammedov 
2628ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2629ff9006ddSIgor Mammedov                                 Error **errp)
2630ff9006ddSIgor Mammedov {
2631ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
2632ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
2633ff9006ddSIgor Mammedov     Error *local_err = NULL;
2634ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2635ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
2636ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
2637535455fdSIgor Mammedov     CPUArchId *core_slot;
2638535455fdSIgor Mammedov     int index;
2639ff9006ddSIgor Mammedov 
2640c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
2641ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
2642ff9006ddSIgor Mammedov         goto out;
2643ff9006ddSIgor Mammedov     }
2644ff9006ddSIgor Mammedov 
2645ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
2646ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
2647ff9006ddSIgor Mammedov         goto out;
2648ff9006ddSIgor Mammedov     }
2649ff9006ddSIgor Mammedov 
2650ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
2651ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
2652ff9006ddSIgor Mammedov         goto out;
2653ff9006ddSIgor Mammedov     }
2654ff9006ddSIgor Mammedov 
2655535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2656535455fdSIgor Mammedov     if (!core_slot) {
2657ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
2658ff9006ddSIgor Mammedov         goto out;
2659ff9006ddSIgor Mammedov     }
2660ff9006ddSIgor Mammedov 
2661535455fdSIgor Mammedov     if (core_slot->cpu) {
2662ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
2663ff9006ddSIgor Mammedov         goto out;
2664ff9006ddSIgor Mammedov     }
2665ff9006ddSIgor Mammedov 
2666ff9006ddSIgor Mammedov out:
2667ff9006ddSIgor Mammedov     g_free(base_core_type);
2668ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
2669ff9006ddSIgor Mammedov }
2670ff9006ddSIgor Mammedov 
2671c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2672c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2673c20d332aSBharata B Rao {
2674c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2675c20d332aSBharata B Rao 
2676c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2677b556854bSBharata B Rao         int node;
2678c20d332aSBharata B Rao 
2679c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2680c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2681c20d332aSBharata B Rao             return;
2682c20d332aSBharata B Rao         }
2683c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2684c20d332aSBharata B Rao         if (*errp) {
2685c20d332aSBharata B Rao             return;
2686c20d332aSBharata B Rao         }
26871a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
26881a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
26891a5512bbSGonglei             return;
26901a5512bbSGonglei         }
2691c20d332aSBharata B Rao 
2692b556854bSBharata B Rao         /*
2693b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2694b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2695b556854bSBharata B Rao          * to the first node that has some memory. This causes two
2696b556854bSBharata B Rao          * unexpected behaviours for the user.
2697b556854bSBharata B Rao          *
2698b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
2699b556854bSBharata B Rao          *   specified.
2700b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2701b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
2702b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
2703b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
2704b556854bSBharata B Rao          *
2705b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
2706b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
2707b556854bSBharata B Rao          */
2708b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
2709b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
2710b556854bSBharata B Rao                        node);
2711b556854bSBharata B Rao             return;
2712b556854bSBharata B Rao         }
2713b556854bSBharata B Rao 
2714c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
2715af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2716af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
2717c20d332aSBharata B Rao     }
2718c20d332aSBharata B Rao }
2719c20d332aSBharata B Rao 
2720c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2721c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2722c20d332aSBharata B Rao {
2723cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
27243c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
27256f4b5c3eSBharata B Rao 
2726c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2727cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2728cf632463SBharata B Rao             spapr_memory_unplug(hotplug_dev, dev, errp);
2729cf632463SBharata B Rao         } else {
2730cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2731cf632463SBharata B Rao         }
2732cf632463SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2733c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
2734cf632463SBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
2735cf632463SBharata B Rao             return;
2736cf632463SBharata B Rao         }
2737cf632463SBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
2738cf632463SBharata B Rao     }
2739cf632463SBharata B Rao }
2740cf632463SBharata B Rao 
2741cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
2742cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
2743cf632463SBharata B Rao {
2744cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
2745cf632463SBharata B Rao     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
2746cf632463SBharata B Rao 
2747cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2748cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2749cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
2750cf632463SBharata B Rao         } else {
2751cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
2752cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
2753cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
2754cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
2755cf632463SBharata B Rao              * eventually handled by the guest after boot
2756cf632463SBharata B Rao              */
2757cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2758cf632463SBharata B Rao         }
27596f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2760c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
27616f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
27626f4b5c3eSBharata B Rao             return;
27636f4b5c3eSBharata B Rao         }
2764115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
2765c20d332aSBharata B Rao     }
2766c20d332aSBharata B Rao }
2767c20d332aSBharata B Rao 
276894a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
276994a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
277094a94e4cSBharata B Rao {
277194a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
277294a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
277394a94e4cSBharata B Rao     }
277494a94e4cSBharata B Rao }
277594a94e4cSBharata B Rao 
27767ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
2777c20d332aSBharata B Rao                                                  DeviceState *dev)
2778c20d332aSBharata B Rao {
277994a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
278094a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2781c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
2782c20d332aSBharata B Rao     }
2783c20d332aSBharata B Rao     return NULL;
2784c20d332aSBharata B Rao }
2785c20d332aSBharata B Rao 
278620bb648dSDavid Gibson static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
278720bb648dSDavid Gibson {
278820bb648dSDavid Gibson     /* Allocate to NUMA nodes on a "socket" basis (not that concept of
278920bb648dSDavid Gibson      * socket means much for the paravirtualized PAPR platform) */
279020bb648dSDavid Gibson     return cpu_index / smp_threads / smp_cores;
279120bb648dSDavid Gibson }
279220bb648dSDavid Gibson 
2793535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
2794535455fdSIgor Mammedov {
2795535455fdSIgor Mammedov     int i;
2796535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
2797535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
2798535455fdSIgor Mammedov 
2799c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
2800535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
2801535455fdSIgor Mammedov     }
2802535455fdSIgor Mammedov     if (machine->possible_cpus) {
2803535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
2804535455fdSIgor Mammedov         return machine->possible_cpus;
2805535455fdSIgor Mammedov     }
2806535455fdSIgor Mammedov 
2807535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2808535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
2809535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
2810535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
2811535455fdSIgor Mammedov         int core_id = i * smp_threads;
2812535455fdSIgor Mammedov 
2813f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
2814535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
2815535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
2816535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
2817535455fdSIgor Mammedov         /* TODO: add 'has_node/node' here to describe
2818535455fdSIgor Mammedov            to which node core belongs */
2819535455fdSIgor Mammedov     }
2820535455fdSIgor Mammedov     return machine->possible_cpus;
2821535455fdSIgor Mammedov }
2822535455fdSIgor Mammedov 
28236737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
2824daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
2825daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
28266737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
28276737d9adSDavid Gibson {
2828357d1e3bSDavid Gibson     /*
2829357d1e3bSDavid Gibson      * New-style PHB window placement.
2830357d1e3bSDavid Gibson      *
2831357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
2832357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
2833357d1e3bSDavid Gibson      * windows.
2834357d1e3bSDavid Gibson      *
2835357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
2836357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
2837357d1e3bSDavid Gibson      *
2838357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
2839357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
2840357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
2841357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
2842357d1e3bSDavid Gibson      */
28436737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
284425e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
284525e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
28466737d9adSDavid Gibson     int i;
28476737d9adSDavid Gibson 
2848357d1e3bSDavid Gibson     /* Sanity check natural alignments */
2849357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2850357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2851357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
2852357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
2853357d1e3bSDavid Gibson     /* Sanity check bounds */
285425e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
285525e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
285625e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
285725e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
28582efff1c0SDavid Gibson 
285925e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
286025e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
286125e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
28626737d9adSDavid Gibson         return;
28636737d9adSDavid Gibson     }
28646737d9adSDavid Gibson 
28656737d9adSDavid Gibson     *buid = base_buid + index;
28666737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
28676737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
28686737d9adSDavid Gibson     }
28696737d9adSDavid Gibson 
2870357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
2871357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
2872357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
28736737d9adSDavid Gibson }
28746737d9adSDavid Gibson 
287529ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
287653018216SPaolo Bonzini {
287729ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
2878224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
287971461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
288034316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
2881c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
28821d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
288329ee3247SAlexey Kardashevskiy 
28840eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
2885fc9f38c3SDavid Gibson 
2886fc9f38c3SDavid Gibson     /*
2887fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
2888fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
2889fc9f38c3SDavid Gibson      * these details for backwards compatibility
2890fc9f38c3SDavid Gibson      */
2891958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
2892958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
2893958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
2894*6244bb7eSGreg Kurz     mc->max_cpus = 1024;
2895958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
28965b2128d2SAlexander Graf     mc->default_boot_order = "";
2897a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
2898958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
28999e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
2900e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
29017ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
290294a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
2903c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
2904c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
290520bb648dSDavid Gibson     mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
2906535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
2907cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
290800b4fbe2SMarcel Apfelbaum 
2909fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
29103daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
2911c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
291271461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
291334316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
29146737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
29151d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
291653018216SPaolo Bonzini }
291753018216SPaolo Bonzini 
291829ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
291929ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
292029ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
29214aee7362SDavid Gibson     .abstract      = true,
29226ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
292323825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
292487bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
2925183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
292629ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
292771461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
292871461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
292934316482SAlexey Kardashevskiy         { TYPE_NMI },
2930c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
29311d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
293271461b0fSAlexey Kardashevskiy         { }
293371461b0fSAlexey Kardashevskiy     },
293429ee3247SAlexey Kardashevskiy };
293529ee3247SAlexey Kardashevskiy 
2936fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
29375013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
29385013c547SDavid Gibson                                                     void *data)      \
29395013c547SDavid Gibson     {                                                                \
29405013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
29415013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
2942fccbc785SDavid Gibson         if (latest) {                                                \
2943fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
2944fccbc785SDavid Gibson             mc->is_default = 1;                                      \
2945fccbc785SDavid Gibson         }                                                            \
29465013c547SDavid Gibson     }                                                                \
29475013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
29485013c547SDavid Gibson     {                                                                \
29495013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
29505013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
29515013c547SDavid Gibson     }                                                                \
29525013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
29535013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
29545013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
29555013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
29565013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
29575013c547SDavid Gibson     };                                                               \
29585013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
29595013c547SDavid Gibson     {                                                                \
29605013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
29615013c547SDavid Gibson     }                                                                \
29620e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
29635013c547SDavid Gibson 
29641c5f29bbSDavid Gibson /*
2965fa325e6cSDavid Gibson  * pseries-2.9
2966db800b21SDavid Gibson  */
2967fa325e6cSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
2968db800b21SDavid Gibson {
2969db800b21SDavid Gibson }
2970db800b21SDavid Gibson 
2971fa325e6cSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
2972db800b21SDavid Gibson {
2973db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
2974db800b21SDavid Gibson }
2975db800b21SDavid Gibson 
2976fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", true);
2977fa325e6cSDavid Gibson 
2978fa325e6cSDavid Gibson /*
2979fa325e6cSDavid Gibson  * pseries-2.8
2980fa325e6cSDavid Gibson  */
2981fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                            \
2982fa325e6cSDavid Gibson     HW_COMPAT_2_8
2983fa325e6cSDavid Gibson 
2984fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
2985fa325e6cSDavid Gibson {
2986fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
2987fa325e6cSDavid Gibson }
2988fa325e6cSDavid Gibson 
2989fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
2990fa325e6cSDavid Gibson {
2991fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
2992fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
2993fa325e6cSDavid Gibson }
2994fa325e6cSDavid Gibson 
2995fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
2996db800b21SDavid Gibson 
2997db800b21SDavid Gibson /*
29981ea1eefcSBharata B Rao  * pseries-2.7
29991ea1eefcSBharata B Rao  */
3000db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3001db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3002357d1e3bSDavid Gibson     {                                               \
3003357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3004357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3005357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3006357d1e3bSDavid Gibson     },                                              \
3007357d1e3bSDavid Gibson     {                                               \
3008357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3009357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3010357d1e3bSDavid Gibson         .value    = "0",                            \
3011146c11f1SDavid Gibson     },                                              \
3012146c11f1SDavid Gibson     {                                               \
3013146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3014146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3015146c11f1SDavid Gibson         .value    = "on",                           \
30165c4537bdSDavid Gibson     },                                              \
30175c4537bdSDavid Gibson     {                                               \
30185c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
30195c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
30205c4537bdSDavid Gibson         .value    = "on",                           \
3021357d1e3bSDavid Gibson     },
3022357d1e3bSDavid Gibson 
3023357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3024357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3025357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3026357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3027357d1e3bSDavid Gibson {
3028357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3029357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3030357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3031357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3032357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3033357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3034357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3035357d1e3bSDavid Gibson 
3036357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3037357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3038357d1e3bSDavid Gibson     int i;
3039357d1e3bSDavid Gibson 
3040357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3041357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3042357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3043357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3044357d1e3bSDavid Gibson          * regions */
3045357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3046357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3047357d1e3bSDavid Gibson     }
3048357d1e3bSDavid Gibson 
3049357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3050357d1e3bSDavid Gibson 
3051357d1e3bSDavid Gibson     if (index > max_index) {
3052357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3053357d1e3bSDavid Gibson                    max_index);
3054357d1e3bSDavid Gibson         return;
3055357d1e3bSDavid Gibson     }
3056357d1e3bSDavid Gibson 
3057357d1e3bSDavid Gibson     *buid = base_buid + index;
3058357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3059357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3060357d1e3bSDavid Gibson     }
3061357d1e3bSDavid Gibson 
3062357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3063357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3064357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3065357d1e3bSDavid Gibson     /*
3066357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3067357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3068357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3069357d1e3bSDavid Gibson      */
3070357d1e3bSDavid Gibson }
3071db800b21SDavid Gibson 
30721ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
30731ea1eefcSBharata B Rao {
3074f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3075f6229214SMichael Roth 
3076672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3077f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
30781ea1eefcSBharata B Rao }
30791ea1eefcSBharata B Rao 
30801ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
30811ea1eefcSBharata B Rao {
30823daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
30833daa4a9fSThomas Huth 
3084db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
30853daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3086db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3087357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
30881ea1eefcSBharata B Rao }
30891ea1eefcSBharata B Rao 
3090db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
30911ea1eefcSBharata B Rao 
30921ea1eefcSBharata B Rao /*
30934b23699cSDavid Gibson  * pseries-2.6
30944b23699cSDavid Gibson  */
30951ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3096ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3097ae4de14cSAlexey Kardashevskiy     { \
3098ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3099ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3100ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3101ae4de14cSAlexey Kardashevskiy     },
31021ea1eefcSBharata B Rao 
31034b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
31044b23699cSDavid Gibson {
3105672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
31064b23699cSDavid Gibson }
31074b23699cSDavid Gibson 
31084b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
31094b23699cSDavid Gibson {
31101ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3111c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
31121ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
31134b23699cSDavid Gibson }
31144b23699cSDavid Gibson 
31151ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
31164b23699cSDavid Gibson 
31174b23699cSDavid Gibson /*
31181c5f29bbSDavid Gibson  * pseries-2.5
31191c5f29bbSDavid Gibson  */
31204b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
312157c522f4SThomas Huth     HW_COMPAT_2_5 \
312257c522f4SThomas Huth     { \
312357c522f4SThomas Huth         .driver   = "spapr-vlan", \
312457c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
312557c522f4SThomas Huth         .value    = "off", \
312657c522f4SThomas Huth     },
31274b23699cSDavid Gibson 
31285013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
31291c5f29bbSDavid Gibson {
3130672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
31315013c547SDavid Gibson }
31325013c547SDavid Gibson 
31335013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
31345013c547SDavid Gibson {
313557040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
313657040d45SThomas Huth 
31374b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
313857040d45SThomas Huth     smc->use_ohci_by_default = true;
31394b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
31401c5f29bbSDavid Gibson }
31411c5f29bbSDavid Gibson 
31424b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
31431c5f29bbSDavid Gibson 
31441c5f29bbSDavid Gibson /*
31451c5f29bbSDavid Gibson  * pseries-2.4
31461c5f29bbSDavid Gibson  */
314780fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
314880fd50f9SCornelia Huck         HW_COMPAT_2_4
314980fd50f9SCornelia Huck 
31505013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
31511c5f29bbSDavid Gibson {
31525013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
31535013c547SDavid Gibson }
31541c5f29bbSDavid Gibson 
31555013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
31565013c547SDavid Gibson {
3157fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3158fc9f38c3SDavid Gibson 
3159fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3160fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3161f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
31621c5f29bbSDavid Gibson }
31631c5f29bbSDavid Gibson 
3164fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
31651c5f29bbSDavid Gibson 
31661c5f29bbSDavid Gibson /*
31671c5f29bbSDavid Gibson  * pseries-2.3
31681c5f29bbSDavid Gibson  */
316938ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
31707619c7b0SMichael Roth         HW_COMPAT_2_3 \
31717619c7b0SMichael Roth         {\
31727619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
31737619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
31747619c7b0SMichael Roth             .value    = "off",\
31757619c7b0SMichael Roth         },
317638ff32c6SEduardo Habkost 
31775013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
31781c5f29bbSDavid Gibson {
31795013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
31801c5f29bbSDavid Gibson     savevm_skip_section_footers();
31811c5f29bbSDavid Gibson     global_state_set_optional();
318209b5e30dSGreg Kurz     savevm_skip_configuration();
31831c5f29bbSDavid Gibson }
31841c5f29bbSDavid Gibson 
31855013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
31861c5f29bbSDavid Gibson {
3187fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3188f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
31891c5f29bbSDavid Gibson }
3190fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
31911c5f29bbSDavid Gibson 
31921c5f29bbSDavid Gibson /*
31931c5f29bbSDavid Gibson  * pseries-2.2
31941c5f29bbSDavid Gibson  */
31951c5f29bbSDavid Gibson 
3196b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
31974dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3198b194df47SAlexey Kardashevskiy         {\
3199b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3200b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3201b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3202dd754bafSEduardo Habkost         },
3203b194df47SAlexey Kardashevskiy 
32045013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3205b0e966d0SJason Wang {
32065013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3207cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3208b0e966d0SJason Wang }
3209b0e966d0SJason Wang 
32105013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3211b0e966d0SJason Wang {
3212fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3213f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
32141c5f29bbSDavid Gibson }
3215fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
32161c5f29bbSDavid Gibson 
32171c5f29bbSDavid Gibson /*
32181c5f29bbSDavid Gibson  * pseries-2.1
32191c5f29bbSDavid Gibson  */
32201c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
32211c5f29bbSDavid Gibson         HW_COMPAT_2_1
32221c5f29bbSDavid Gibson 
32235013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
32241c5f29bbSDavid Gibson {
32255013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
32261c5f29bbSDavid Gibson }
32271c5f29bbSDavid Gibson 
32285013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
3229b0e966d0SJason Wang {
3230fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
3231f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
32326026db45SAlexey Kardashevskiy }
3233fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
32346026db45SAlexey Kardashevskiy 
323529ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
323629ee3247SAlexey Kardashevskiy {
323729ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
323829ee3247SAlexey Kardashevskiy }
323929ee3247SAlexey Kardashevskiy 
324029ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
3241