xref: /openbmc/qemu/hw/ppc/spapr.c (revision 817bb6a4)
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 
984e4169f7SCédric Le Goater static XICSState *try_create_xics(const char *type, const char *type_ics,
99*817bb6a4SCédric Le Goater                                   const char *type_icp, int nr_servers,
100*817bb6a4SCédric Le Goater                                   int nr_irqs, Error **errp)
101c04d6cfaSAnthony Liguori {
1024e4169f7SCédric Le Goater     Error *err = NULL, *local_err = NULL;
1034e4169f7SCédric Le Goater     XICSState *xics;
1044e4169f7SCédric Le Goater     ICSState *ics = NULL;
105*817bb6a4SCédric Le Goater     int i;
106c04d6cfaSAnthony Liguori 
1074e4169f7SCédric Le Goater     xics = XICS_COMMON(object_new(type));
1084e4169f7SCédric Le Goater     qdev_set_parent_bus(DEVICE(xics), sysbus_get_default());
109*817bb6a4SCédric Le Goater     object_property_set_bool(OBJECT(xics), true, "realized", &err);
11034f2af3dSMarkus Armbruster     if (err) {
1114e4169f7SCédric Le Goater         goto error;
112c04d6cfaSAnthony Liguori     }
1134e4169f7SCédric Le Goater 
1144e4169f7SCédric Le Goater     ics = ICS_SIMPLE(object_new(type_ics));
1154e4169f7SCédric Le Goater     object_property_add_child(OBJECT(xics), "ics", OBJECT(ics), NULL);
1164e4169f7SCédric Le Goater     object_property_set_int(OBJECT(ics), nr_irqs, "nr-irqs", &err);
1174e4169f7SCédric Le Goater     object_property_add_const_link(OBJECT(ics), "xics", OBJECT(xics), NULL);
1184e4169f7SCédric Le Goater     object_property_set_bool(OBJECT(ics), true, "realized", &local_err);
1194e4169f7SCédric Le Goater     error_propagate(&err, local_err);
1204e4169f7SCédric Le Goater     if (err) {
1214e4169f7SCédric Le Goater         goto error;
1224e4169f7SCédric Le Goater     }
1234e4169f7SCédric Le Goater     QLIST_INSERT_HEAD(&xics->ics, ics, list);
1244e4169f7SCédric Le Goater 
125*817bb6a4SCédric Le Goater     xics->ss = g_malloc0(nr_servers * sizeof(ICPState));
126*817bb6a4SCédric Le Goater     xics->nr_servers = nr_servers;
127*817bb6a4SCédric Le Goater 
128*817bb6a4SCédric Le Goater     for (i = 0; i < nr_servers; i++) {
129*817bb6a4SCédric Le Goater         ICPState *icp = &xics->ss[i];
130*817bb6a4SCédric Le Goater 
131*817bb6a4SCédric Le Goater         object_initialize(icp, sizeof(*icp), type_icp);
132*817bb6a4SCédric Le Goater         object_property_add_child(OBJECT(xics), "icp[*]", OBJECT(icp), NULL);
133*817bb6a4SCédric Le Goater         object_property_add_const_link(OBJECT(icp), "xics", OBJECT(xics), NULL);
134*817bb6a4SCédric Le Goater         object_property_set_bool(OBJECT(icp), true, "realized", &err);
135*817bb6a4SCédric Le Goater         if (err) {
136*817bb6a4SCédric Le Goater             goto error;
137*817bb6a4SCédric Le Goater         }
138*817bb6a4SCédric Le Goater         object_unref(OBJECT(icp));
139*817bb6a4SCédric Le Goater     }
140*817bb6a4SCédric Le Goater 
1414e4169f7SCédric Le Goater     return xics;
1424e4169f7SCédric Le Goater 
1434e4169f7SCédric Le Goater error:
1444e4169f7SCédric Le Goater     error_propagate(errp, err);
1454e4169f7SCédric Le Goater     if (ics) {
1464e4169f7SCédric Le Goater         object_unparent(OBJECT(ics));
1474e4169f7SCédric Le Goater     }
1484e4169f7SCédric Le Goater     object_unparent(OBJECT(xics));
1494e4169f7SCédric Le Goater     return NULL;
150c04d6cfaSAnthony Liguori }
151c04d6cfaSAnthony Liguori 
152446f16a6SMarcel Apfelbaum static XICSState *xics_system_init(MachineState *machine,
1531e49182dSDavid Gibson                                    int nr_servers, int nr_irqs, Error **errp)
154c04d6cfaSAnthony Liguori {
15527f24582SBenjamin Herrenschmidt     XICSState *xics = NULL;
156c04d6cfaSAnthony Liguori 
15711ad93f6SDavid Gibson     if (kvm_enabled()) {
15834f2af3dSMarkus Armbruster         Error *err = NULL;
15934f2af3dSMarkus Armbruster 
160446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_allowed(machine)) {
1614e4169f7SCédric Le Goater             xics = try_create_xics(TYPE_XICS_SPAPR_KVM, TYPE_ICS_KVM,
162*817bb6a4SCédric Le Goater                                    TYPE_KVM_ICP, nr_servers, nr_irqs, &err);
16311ad93f6SDavid Gibson         }
16427f24582SBenjamin Herrenschmidt         if (machine_kernel_irqchip_required(machine) && !xics) {
165b83baa60SMarkus Armbruster             error_reportf_err(err,
166b83baa60SMarkus Armbruster                               "kernel_irqchip requested but unavailable: ");
167b83baa60SMarkus Armbruster         } else {
168903a41d3SStefano Dong (董兴水)             error_free(err);
16911ad93f6SDavid Gibson         }
170b83baa60SMarkus Armbruster     }
17111ad93f6SDavid Gibson 
17227f24582SBenjamin Herrenschmidt     if (!xics) {
173*817bb6a4SCédric Le Goater         xics = try_create_xics(TYPE_XICS_SPAPR, TYPE_ICS_SIMPLE, TYPE_ICP,
174*817bb6a4SCédric Le Goater                                nr_servers, nr_irqs, errp);
175c04d6cfaSAnthony Liguori     }
176c04d6cfaSAnthony Liguori 
17727f24582SBenjamin Herrenschmidt     return xics;
178c04d6cfaSAnthony Liguori }
179c04d6cfaSAnthony Liguori 
180833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
181833d4668SAlexey Kardashevskiy                                   int smt_threads)
182833d4668SAlexey Kardashevskiy {
183833d4668SAlexey Kardashevskiy     int i, ret = 0;
184833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
185833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
186833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
187833d4668SAlexey Kardashevskiy 
188d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
189d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
1906d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1916d9412eaSAlexey Kardashevskiy             return ret;
1926d9412eaSAlexey Kardashevskiy         }
1936d9412eaSAlexey Kardashevskiy     }
1946d9412eaSAlexey Kardashevskiy 
195833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
196833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
197833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
198833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
199833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
200833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
201833d4668SAlexey Kardashevskiy     }
202833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
203833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
204833d4668SAlexey Kardashevskiy     if (ret < 0) {
205833d4668SAlexey Kardashevskiy         return ret;
206833d4668SAlexey Kardashevskiy     }
207833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
208833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
209833d4668SAlexey Kardashevskiy 
210833d4668SAlexey Kardashevskiy     return ret;
211833d4668SAlexey Kardashevskiy }
212833d4668SAlexey Kardashevskiy 
2130da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
2140da6f3feSBharata B Rao {
2150da6f3feSBharata B Rao     int ret = 0;
2160da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2170da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
2180da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
2190da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2200da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2210da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2220da6f3feSBharata B Rao                                 cpu_to_be32(cs->numa_node),
2230da6f3feSBharata B Rao                                 cpu_to_be32(index)};
2240da6f3feSBharata B Rao 
2250da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
2260da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
2270da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
2280da6f3feSBharata B Rao                           sizeof(associativity));
2290da6f3feSBharata B Rao     }
2300da6f3feSBharata B Rao 
2310da6f3feSBharata B Rao     return ret;
2320da6f3feSBharata B Rao }
2330da6f3feSBharata B Rao 
23428e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
23553018216SPaolo Bonzini {
23682677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
23782677ed2SAlexey Kardashevskiy     CPUState *cs;
23853018216SPaolo Bonzini     char cpu_model[32];
23953018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
24053018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
24153018216SPaolo Bonzini 
24282677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
24382677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
24482677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
24582677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
24612dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
24753018216SPaolo Bonzini 
2480f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
24953018216SPaolo Bonzini             continue;
25053018216SPaolo Bonzini         }
25153018216SPaolo Bonzini 
25282677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
25353018216SPaolo Bonzini 
25482677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
25582677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
25682677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
25782677ed2SAlexey Kardashevskiy                                           "cpus");
25882677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
25982677ed2SAlexey Kardashevskiy                 return cpus_offset;
26082677ed2SAlexey Kardashevskiy             }
26182677ed2SAlexey Kardashevskiy         }
26282677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
26382677ed2SAlexey Kardashevskiy         if (offset < 0) {
26482677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
26553018216SPaolo Bonzini             if (offset < 0) {
26653018216SPaolo Bonzini                 return offset;
26753018216SPaolo Bonzini             }
26882677ed2SAlexey Kardashevskiy         }
26953018216SPaolo Bonzini 
2700da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
2710da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
27253018216SPaolo Bonzini         if (ret < 0) {
27353018216SPaolo Bonzini             return ret;
27453018216SPaolo Bonzini         }
27553018216SPaolo Bonzini 
2760da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
27753018216SPaolo Bonzini         if (ret < 0) {
27853018216SPaolo Bonzini             return ret;
27953018216SPaolo Bonzini         }
280833d4668SAlexey Kardashevskiy 
28112dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
282833d4668SAlexey Kardashevskiy         if (ret < 0) {
283833d4668SAlexey Kardashevskiy             return ret;
284833d4668SAlexey Kardashevskiy         }
28553018216SPaolo Bonzini     }
28653018216SPaolo Bonzini     return ret;
28753018216SPaolo Bonzini }
28853018216SPaolo Bonzini 
289b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
290b082d65aSAlexey Kardashevskiy {
291fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
292fb164994SDavid Gibson 
293b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
294b082d65aSAlexey Kardashevskiy         int i;
295b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
296b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
297fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
298fb164994SDavid Gibson                            machine->ram_size);
299b082d65aSAlexey Kardashevskiy             }
300b082d65aSAlexey Kardashevskiy         }
301b082d65aSAlexey Kardashevskiy     }
302fb164994SDavid Gibson     return machine->ram_size;
303b082d65aSAlexey Kardashevskiy }
304b082d65aSAlexey Kardashevskiy 
305a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
306a1d59c0fSAlexey Kardashevskiy {
307a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
308a1d59c0fSAlexey Kardashevskiy }
30953018216SPaolo Bonzini 
31003d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
31126a8c353SAlexey Kardashevskiy                                        hwaddr size)
31226a8c353SAlexey Kardashevskiy {
31326a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
31426a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
31526a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
316c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
31726a8c353SAlexey Kardashevskiy     };
31826a8c353SAlexey Kardashevskiy     char mem_name[32];
31926a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
32026a8c353SAlexey Kardashevskiy     int off;
32126a8c353SAlexey Kardashevskiy 
32226a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
32326a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
32426a8c353SAlexey Kardashevskiy 
32526a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
32626a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
32726a8c353SAlexey Kardashevskiy     _FDT(off);
32826a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
32926a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
33026a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
33126a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
33226a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
33303d196b7SBharata B Rao     return off;
33426a8c353SAlexey Kardashevskiy }
33526a8c353SAlexey Kardashevskiy 
33628e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
33753018216SPaolo Bonzini {
338fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
3397db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
3407db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
3417db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
3427db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
34353018216SPaolo Bonzini 
3447db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
3457db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
3467db8a127SAlexey Kardashevskiy         nb_nodes = 1;
347fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
3487db8a127SAlexey Kardashevskiy         nodes = &ramnode;
3495fe269b1SPaul Mackerras     }
35053018216SPaolo Bonzini 
3517db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
3527db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
3537db8a127SAlexey Kardashevskiy             continue;
35453018216SPaolo Bonzini         }
355fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
3565fe269b1SPaul Mackerras             node_size = 0;
3575fe269b1SPaul Mackerras         } else {
3587db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
359fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
360fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
3615fe269b1SPaul Mackerras             }
3625fe269b1SPaul Mackerras         }
3637db8a127SAlexey Kardashevskiy         if (!mem_start) {
3647db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
365e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
3667db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
3677db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
3687db8a127SAlexey Kardashevskiy         }
3696010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
3706010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
3716010818cSAlexey Kardashevskiy 
3726010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
3736010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
3746010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
3756010818cSAlexey Kardashevskiy             }
3766010818cSAlexey Kardashevskiy 
3776010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
3786010818cSAlexey Kardashevskiy             node_size -= sizetmp;
3796010818cSAlexey Kardashevskiy             mem_start += sizetmp;
3806010818cSAlexey Kardashevskiy         }
38153018216SPaolo Bonzini     }
38253018216SPaolo Bonzini 
38353018216SPaolo Bonzini     return 0;
38453018216SPaolo Bonzini }
38553018216SPaolo Bonzini 
386230bf719SThomas Huth /* Populate the "ibm,pa-features" property */
387230bf719SThomas Huth static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset)
388230bf719SThomas Huth {
389230bf719SThomas Huth     uint8_t pa_features_206[] = { 6, 0,
390230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
391230bf719SThomas Huth     uint8_t pa_features_207[] = { 24, 0,
392230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
393230bf719SThomas Huth         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
394230bf719SThomas Huth         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
395bac3bf28SThomas Huth         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
396230bf719SThomas Huth     uint8_t *pa_features;
397230bf719SThomas Huth     size_t pa_size;
398230bf719SThomas Huth 
3994cbec30dSThomas Huth     switch (env->mmu_model) {
4004cbec30dSThomas Huth     case POWERPC_MMU_2_06:
4014cbec30dSThomas Huth     case POWERPC_MMU_2_06a:
402230bf719SThomas Huth         pa_features = pa_features_206;
403230bf719SThomas Huth         pa_size = sizeof(pa_features_206);
4044cbec30dSThomas Huth         break;
4054cbec30dSThomas Huth     case POWERPC_MMU_2_07:
4064cbec30dSThomas Huth     case POWERPC_MMU_2_07a:
407230bf719SThomas Huth         pa_features = pa_features_207;
408230bf719SThomas Huth         pa_size = sizeof(pa_features_207);
4094cbec30dSThomas Huth         break;
4104cbec30dSThomas Huth     default:
4114cbec30dSThomas Huth         return;
412230bf719SThomas Huth     }
413230bf719SThomas Huth 
414230bf719SThomas Huth     if (env->ci_large_pages) {
415230bf719SThomas Huth         /*
416230bf719SThomas Huth          * Note: we keep CI large pages off by default because a 64K capable
417230bf719SThomas Huth          * guest provisioned with large pages might otherwise try to map a qemu
418230bf719SThomas Huth          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
419230bf719SThomas Huth          * even if that qemu runs on a 4k host.
420230bf719SThomas Huth          * We dd this bit back here if we are confident this is not an issue
421230bf719SThomas Huth          */
422230bf719SThomas Huth         pa_features[3] |= 0x20;
423230bf719SThomas Huth     }
424bac3bf28SThomas Huth     if (kvmppc_has_cap_htm() && pa_size > 24) {
425bac3bf28SThomas Huth         pa_features[24] |= 0x80;    /* Transactional memory support */
426bac3bf28SThomas Huth     }
427230bf719SThomas Huth 
428230bf719SThomas Huth     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
429230bf719SThomas Huth }
430230bf719SThomas Huth 
4310da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4320da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4330da6f3feSBharata B Rao {
4340da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4350da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4360da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4370da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
4380da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4390da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
440afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
441afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
4420da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
4430da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
4440da6f3feSBharata B Rao     size_t page_sizes_prop_size;
44522419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
4460da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
44712dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
448af81cf32SBharata B Rao     sPAPRDRConnector *drc;
449af81cf32SBharata B Rao     sPAPRDRConnectorClass *drck;
450af81cf32SBharata B Rao     int drc_index;
451af81cf32SBharata B Rao 
452af81cf32SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
453af81cf32SBharata B Rao     if (drc) {
454af81cf32SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
455af81cf32SBharata B Rao         drc_index = drck->get_index(drc);
456af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
457af81cf32SBharata B Rao     }
4580da6f3feSBharata B Rao 
4590da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
4600da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
4610da6f3feSBharata B Rao 
4620da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
4630da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
4640da6f3feSBharata B Rao                            env->dcache_line_size)));
4650da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
4660da6f3feSBharata B Rao                            env->dcache_line_size)));
4670da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
4680da6f3feSBharata B Rao                            env->icache_line_size)));
4690da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
4700da6f3feSBharata B Rao                            env->icache_line_size)));
4710da6f3feSBharata B Rao 
4720da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
4730da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
4740da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
4750da6f3feSBharata B Rao     } else {
476ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
4770da6f3feSBharata B Rao     }
4780da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
4790da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
4800da6f3feSBharata B Rao                                pcc->l1_icache_size)));
4810da6f3feSBharata B Rao     } else {
482ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
4830da6f3feSBharata B Rao     }
4840da6f3feSBharata B Rao 
4850da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
4860da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
487fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
4880da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
4890da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
4900da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
4910da6f3feSBharata B Rao 
4920da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
4930da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
4940da6f3feSBharata B Rao     }
4950da6f3feSBharata B Rao 
4960da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
4970da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
4980da6f3feSBharata B Rao                           segs, sizeof(segs))));
4990da6f3feSBharata B Rao     }
5000da6f3feSBharata B Rao 
5010da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
5020da6f3feSBharata B Rao      *   0 / no property == no vector extensions
5030da6f3feSBharata B Rao      *   1               == VMX / Altivec available
5040da6f3feSBharata B Rao      *   2               == VSX available */
5050da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
5060da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
5070da6f3feSBharata B Rao 
5080da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
5090da6f3feSBharata B Rao     }
5100da6f3feSBharata B Rao 
5110da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
5120da6f3feSBharata B Rao      *   0 / no property == no DFP
5130da6f3feSBharata B Rao      *   1               == DFP available */
5140da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
5150da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
5160da6f3feSBharata B Rao     }
5170da6f3feSBharata B Rao 
5183654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
5190da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
5200da6f3feSBharata B Rao     if (page_sizes_prop_size) {
5210da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
5220da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
5230da6f3feSBharata B Rao     }
5240da6f3feSBharata B Rao 
525230bf719SThomas Huth     spapr_populate_pa_features(env, fdt, offset);
52690da0d5aSBenjamin Herrenschmidt 
5270da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
52822419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
5290da6f3feSBharata B Rao 
5300da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
5310da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
5320da6f3feSBharata B Rao 
5330da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
5340da6f3feSBharata B Rao 
53512dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
5360da6f3feSBharata B Rao }
5370da6f3feSBharata B Rao 
5380da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
5390da6f3feSBharata B Rao {
5400da6f3feSBharata B Rao     CPUState *cs;
5410da6f3feSBharata B Rao     int cpus_offset;
5420da6f3feSBharata B Rao     char *nodename;
5430da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
5440da6f3feSBharata B Rao 
5450da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
5460da6f3feSBharata B Rao     _FDT(cpus_offset);
5470da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
5480da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
5490da6f3feSBharata B Rao 
5500da6f3feSBharata B Rao     /*
5510da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
5520da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
5530da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
5540da6f3feSBharata B Rao      */
5550da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
5560da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
5570da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
5580da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
5590da6f3feSBharata B Rao         int offset;
5600da6f3feSBharata B Rao 
5610da6f3feSBharata B Rao         if ((index % smt) != 0) {
5620da6f3feSBharata B Rao             continue;
5630da6f3feSBharata B Rao         }
5640da6f3feSBharata B Rao 
5650da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
5660da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
5670da6f3feSBharata B Rao         g_free(nodename);
5680da6f3feSBharata B Rao         _FDT(offset);
5690da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
5700da6f3feSBharata B Rao     }
5710da6f3feSBharata B Rao 
5720da6f3feSBharata B Rao }
5730da6f3feSBharata B Rao 
57403d196b7SBharata B Rao /*
57503d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
57603d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
57703d196b7SBharata B Rao  * of this device tree node.
57803d196b7SBharata B Rao  */
57903d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
58003d196b7SBharata B Rao {
58103d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
58203d196b7SBharata B Rao     int ret, i, offset;
58303d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
58403d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
585d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
586d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
587d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
588d0e5a8f2SBharata B Rao                        lmb_size;
58903d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
5906663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
59103d196b7SBharata B Rao 
592ef001f06SThomas Huth     /*
593d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
59416c25aefSBharata B Rao      */
595d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
59616c25aefSBharata B Rao         return 0;
59716c25aefSBharata B Rao     }
59816c25aefSBharata B Rao 
59916c25aefSBharata B Rao     /*
600ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
601ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
602ef001f06SThomas Huth      */
603ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
604ef001f06SThomas Huth               * sizeof(uint32_t);
60503d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
60603d196b7SBharata B Rao 
60703d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
60803d196b7SBharata B Rao 
60903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
61003d196b7SBharata B Rao                     sizeof(prop_lmb_size));
61103d196b7SBharata B Rao     if (ret < 0) {
61203d196b7SBharata B Rao         goto out;
61303d196b7SBharata B Rao     }
61403d196b7SBharata B Rao 
61503d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
61603d196b7SBharata B Rao     if (ret < 0) {
61703d196b7SBharata B Rao         goto out;
61803d196b7SBharata B Rao     }
61903d196b7SBharata B Rao 
62003d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
62103d196b7SBharata B Rao     if (ret < 0) {
62203d196b7SBharata B Rao         goto out;
62303d196b7SBharata B Rao     }
62403d196b7SBharata B Rao 
62503d196b7SBharata B Rao     /* ibm,dynamic-memory */
62603d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
62703d196b7SBharata B Rao     cur_index++;
62803d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
629d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
63003d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
63103d196b7SBharata B Rao 
632d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
633d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
634d0e5a8f2SBharata B Rao             sPAPRDRConnectorClass *drck;
635d0e5a8f2SBharata B Rao 
636d0e5a8f2SBharata B Rao             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
63703d196b7SBharata B Rao             g_assert(drc);
63803d196b7SBharata B Rao             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
63903d196b7SBharata B Rao 
64003d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
64103d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
64203d196b7SBharata B Rao             dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
64303d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
64403d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
645d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
64603d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
64703d196b7SBharata B Rao             } else {
64803d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
64903d196b7SBharata B Rao             }
650d0e5a8f2SBharata B Rao         } else {
651d0e5a8f2SBharata B Rao             /*
652d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
653d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
654d0e5a8f2SBharata B Rao              * and as having no valid DRC.
655d0e5a8f2SBharata B Rao              */
656d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
657d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
658d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
659d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
660d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
661d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
662d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
663d0e5a8f2SBharata B Rao         }
66403d196b7SBharata B Rao 
66503d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
66603d196b7SBharata B Rao     }
66703d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
66803d196b7SBharata B Rao     if (ret < 0) {
66903d196b7SBharata B Rao         goto out;
67003d196b7SBharata B Rao     }
67103d196b7SBharata B Rao 
67203d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
67303d196b7SBharata B Rao     cur_index = int_buf;
6746663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
67503d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
67603d196b7SBharata B Rao     cur_index += 2;
6776663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
67803d196b7SBharata B Rao         uint32_t associativity[] = {
67903d196b7SBharata B Rao             cpu_to_be32(0x0),
68003d196b7SBharata B Rao             cpu_to_be32(0x0),
68103d196b7SBharata B Rao             cpu_to_be32(0x0),
68203d196b7SBharata B Rao             cpu_to_be32(i)
68303d196b7SBharata B Rao         };
68403d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
68503d196b7SBharata B Rao         cur_index += 4;
68603d196b7SBharata B Rao     }
68703d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
68803d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
68903d196b7SBharata B Rao out:
69003d196b7SBharata B Rao     g_free(int_buf);
69103d196b7SBharata B Rao     return ret;
69203d196b7SBharata B Rao }
69303d196b7SBharata B Rao 
6946787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
6956787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
6966787d27bSMichael Roth {
6976787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
698417ece33SMichael Roth     int ret = 0, offset;
6996787d27bSMichael Roth 
7006787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
7016787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
7026787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
7036787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
704417ece33SMichael Roth         if (ret) {
705417ece33SMichael Roth             goto out;
706417ece33SMichael Roth         }
7076787d27bSMichael Roth     }
7086787d27bSMichael Roth 
709417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
710417ece33SMichael Roth     if (offset < 0) {
711417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
712417ece33SMichael Roth         if (offset < 0) {
713417ece33SMichael Roth             return offset;
714417ece33SMichael Roth         }
715417ece33SMichael Roth     }
716417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
717417ece33SMichael Roth                                  "ibm,architecture-vec-5");
718417ece33SMichael Roth 
719417ece33SMichael Roth out:
7206787d27bSMichael Roth     return ret;
7216787d27bSMichael Roth }
7226787d27bSMichael Roth 
72303d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
72403d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
7256787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
72603d196b7SBharata B Rao {
72703d196b7SBharata B Rao     void *fdt, *fdt_skel;
72803d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
72903d196b7SBharata B Rao 
73003d196b7SBharata B Rao     size -= sizeof(hdr);
73103d196b7SBharata B Rao 
73203d196b7SBharata B Rao     /* Create sceleton */
73303d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
73403d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
73503d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
73603d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
73703d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
73803d196b7SBharata B Rao     fdt = g_malloc0(size);
73903d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
74003d196b7SBharata B Rao     g_free(fdt_skel);
74103d196b7SBharata B Rao 
74203d196b7SBharata B Rao     /* Fixup cpu nodes */
74303d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
74403d196b7SBharata B Rao 
7456787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
7466787d27bSMichael Roth         return -1;
74703d196b7SBharata B Rao     }
74803d196b7SBharata B Rao 
74903d196b7SBharata B Rao     /* Pack resulting tree */
75003d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
75103d196b7SBharata B Rao 
75203d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
75303d196b7SBharata B Rao         trace_spapr_cas_failed(size);
75403d196b7SBharata B Rao         return -1;
75503d196b7SBharata B Rao     }
75603d196b7SBharata B Rao 
75703d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
75803d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
75903d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
76003d196b7SBharata B Rao     g_free(fdt);
76103d196b7SBharata B Rao 
76203d196b7SBharata B Rao     return 0;
76303d196b7SBharata B Rao }
76403d196b7SBharata B Rao 
7653f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
7663f5dabceSDavid Gibson {
7673f5dabceSDavid Gibson     int rtas;
7683f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
7693f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
7703f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
7713f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
7723f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
7733f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
7743f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
7753f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
7763f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
7773f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
7783f5dabceSDavid Gibson     };
7793f5dabceSDavid Gibson 
7803f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
7813f5dabceSDavid Gibson 
7823f5dabceSDavid Gibson     /* hypertas */
7833f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
7843f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
7853f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
7863f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
7873f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
7883f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
7893f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
7903f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
7913f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
7923f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
7933f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
7943f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
7953f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
7963f5dabceSDavid Gibson 
7973f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
7983f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
7993f5dabceSDavid Gibson     }
8003f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
8013f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
8023f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
8033f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
8043f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
8053f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
8063f5dabceSDavid Gibson 
8073f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
8083f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
8093f5dabceSDavid Gibson 
8103f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
8113f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
8123f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
8133f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
8143f5dabceSDavid Gibson 
8153f5dabceSDavid Gibson     if (msi_nonbroken) {
8163f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
8173f5dabceSDavid Gibson     }
8183f5dabceSDavid Gibson 
8193f5dabceSDavid Gibson     /*
8203f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
8213f5dabceSDavid Gibson      * back to the guest cpu.
8223f5dabceSDavid Gibson      *
8233f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
8243f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
8253f5dabceSDavid Gibson      */
8263f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
8273f5dabceSDavid Gibson 
8283f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
8293f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
8303f5dabceSDavid Gibson 
8313f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
8323f5dabceSDavid Gibson }
8333f5dabceSDavid Gibson 
8347c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
8357c866c6aSDavid Gibson {
8367c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
8377c866c6aSDavid Gibson     int chosen;
8387c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
8397c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
8407c866c6aSDavid Gibson     size_t cb = 0;
8417c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
8427c866c6aSDavid Gibson 
8437c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
8447c866c6aSDavid Gibson 
8457c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
8467c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
8477c866c6aSDavid Gibson                           spapr->initrd_base));
8487c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
8497c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
8507c866c6aSDavid Gibson 
8517c866c6aSDavid Gibson     if (spapr->kernel_size) {
8527c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
8537c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
8547c866c6aSDavid Gibson 
8557c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
8567c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
8577c866c6aSDavid Gibson         if (spapr->kernel_le) {
8587c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
8597c866c6aSDavid Gibson         }
8607c866c6aSDavid Gibson     }
8617c866c6aSDavid Gibson     if (boot_menu) {
8627c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
8637c866c6aSDavid Gibson     }
8647c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
8657c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
8667c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
8677c866c6aSDavid Gibson 
8687c866c6aSDavid Gibson     if (cb && bootlist) {
8697c866c6aSDavid Gibson         int i;
8707c866c6aSDavid Gibson 
8717c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
8727c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
8737c866c6aSDavid Gibson                 bootlist[i] = ' ';
8747c866c6aSDavid Gibson             }
8757c866c6aSDavid Gibson         }
8767c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
8777c866c6aSDavid Gibson     }
8787c866c6aSDavid Gibson 
8797c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
8807c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
8817c866c6aSDavid Gibson     }
8827c866c6aSDavid Gibson 
8837c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
8847c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
8857c866c6aSDavid Gibson     }
8867c866c6aSDavid Gibson 
8877c866c6aSDavid Gibson     g_free(stdout_path);
8887c866c6aSDavid Gibson     g_free(bootlist);
8897c866c6aSDavid Gibson }
8907c866c6aSDavid Gibson 
891fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
892fca5f2dcSDavid Gibson {
893fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
894fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
895fca5f2dcSDavid Gibson     int hypervisor;
896fca5f2dcSDavid Gibson     uint8_t hypercall[16];
897fca5f2dcSDavid Gibson 
898fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
899fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
900fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
901fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
902fca5f2dcSDavid Gibson         /*
903fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
904fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
905fca5f2dcSDavid Gibson          */
906fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
907fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
908fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
909fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
910fca5f2dcSDavid Gibson         }
911fca5f2dcSDavid Gibson     }
912fca5f2dcSDavid Gibson }
913fca5f2dcSDavid Gibson 
914997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
91553018216SPaolo Bonzini                              hwaddr rtas_addr,
91653018216SPaolo Bonzini                              hwaddr rtas_size)
91753018216SPaolo Bonzini {
9185b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
9193c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
920c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
9217c866c6aSDavid Gibson     int ret;
92253018216SPaolo Bonzini     void *fdt;
92353018216SPaolo Bonzini     sPAPRPHBState *phb;
924398a0bd5SDavid Gibson     char *buf;
92553018216SPaolo Bonzini 
926398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
927398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
92853018216SPaolo Bonzini 
929398a0bd5SDavid Gibson     /* Root node */
930398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
931398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
932398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
933398a0bd5SDavid Gibson 
934398a0bd5SDavid Gibson     /*
935398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
936398a0bd5SDavid Gibson      * and what is the uuid of the guest
937398a0bd5SDavid Gibson      */
938398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
939398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
940398a0bd5SDavid Gibson         g_free(buf);
941398a0bd5SDavid Gibson     }
942398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
943398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
944398a0bd5SDavid Gibson         g_free(buf);
945398a0bd5SDavid Gibson     }
946398a0bd5SDavid Gibson 
947398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
948398a0bd5SDavid Gibson 
949398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
950398a0bd5SDavid Gibson     if (qemu_uuid_set) {
951398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
952398a0bd5SDavid Gibson     }
953398a0bd5SDavid Gibson     g_free(buf);
954398a0bd5SDavid Gibson 
955398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
956398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
957398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
958398a0bd5SDavid Gibson     }
959398a0bd5SDavid Gibson 
960398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
961398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
96253018216SPaolo Bonzini 
9639b9a1908SDavid Gibson     /* /interrupt controller */
9649b9a1908SDavid Gibson     spapr_dt_xics(spapr->xics, fdt, PHANDLE_XICP);
9659b9a1908SDavid Gibson 
966e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
967e8f986fcSBharata B Rao     if (ret < 0) {
968ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
969e8f986fcSBharata B Rao         exit(1);
97053018216SPaolo Bonzini     }
97153018216SPaolo Bonzini 
972bf5a6696SDavid Gibson     /* /vdevice */
973bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
97453018216SPaolo Bonzini 
9754d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
9764d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
9774d9392beSThomas Huth         if (ret < 0) {
978ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
9794d9392beSThomas Huth             exit(1);
9804d9392beSThomas Huth         }
9814d9392beSThomas Huth     }
9824d9392beSThomas Huth 
98353018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
98453018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
98553018216SPaolo Bonzini         if (ret < 0) {
986da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
98753018216SPaolo Bonzini             exit(1);
98853018216SPaolo Bonzini         }
989da34fed7SThomas Huth     }
99053018216SPaolo Bonzini 
9910da6f3feSBharata B Rao     /* cpus */
9920da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
99353018216SPaolo Bonzini 
994c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
995c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
996c20d332aSBharata B Rao     }
997c20d332aSBharata B Rao 
998c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
999af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1000af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1001af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1002af81cf32SBharata B Rao         if (ret < 0) {
1003af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1004af81cf32SBharata B Rao             exit(1);
1005af81cf32SBharata B Rao         }
1006af81cf32SBharata B Rao     }
1007af81cf32SBharata B Rao 
1008ffb1e275SDavid Gibson     /* /event-sources */
1009ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1010ffb1e275SDavid Gibson 
10113f5dabceSDavid Gibson     /* /rtas */
10123f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
10133f5dabceSDavid Gibson 
10147c866c6aSDavid Gibson     /* /chosen */
10157c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1016cf6e5223SDavid Gibson 
1017fca5f2dcSDavid Gibson     /* /hypervisor */
1018fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1019fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1020fca5f2dcSDavid Gibson     }
1021fca5f2dcSDavid Gibson 
1022cf6e5223SDavid Gibson     /* Build memory reserve map */
1023cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1024cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1025cf6e5223SDavid Gibson     }
1026cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1027cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1028cf6e5223SDavid Gibson     }
1029cf6e5223SDavid Gibson 
10306787d27bSMichael Roth     /* ibm,client-architecture-support updates */
10316787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
10326787d27bSMichael Roth     if (ret < 0) {
10336787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
10346787d27bSMichael Roth         exit(1);
10356787d27bSMichael Roth     }
10366787d27bSMichael Roth 
1037997b6cfcSDavid Gibson     return fdt;
103853018216SPaolo Bonzini }
103953018216SPaolo Bonzini 
104053018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
104153018216SPaolo Bonzini {
104253018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
104353018216SPaolo Bonzini }
104453018216SPaolo Bonzini 
10451d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
10461d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
104753018216SPaolo Bonzini {
104853018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
104953018216SPaolo Bonzini 
10508d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
10518d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
10528d04fb55SJan Kiszka 
105353018216SPaolo Bonzini     if (msr_pr) {
105453018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
105553018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
105653018216SPaolo Bonzini     } else {
105753018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
105853018216SPaolo Bonzini     }
105953018216SPaolo Bonzini }
106053018216SPaolo Bonzini 
1061e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1062e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1063e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1064e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1065e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1066e6b8fd24SSamuel Mendoza-Jonas 
1067715c5407SDavid Gibson /*
1068715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1069715c5407SDavid Gibson  */
1070715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1071715c5407SDavid Gibson {
1072715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1073715c5407SDavid Gibson         return spapr->htab_fd;
1074715c5407SDavid Gibson     }
1075715c5407SDavid Gibson 
1076715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1077715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1078715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1079715c5407SDavid Gibson                      strerror(errno));
1080715c5407SDavid Gibson     }
1081715c5407SDavid Gibson 
1082715c5407SDavid Gibson     return spapr->htab_fd;
1083715c5407SDavid Gibson }
1084715c5407SDavid Gibson 
1085715c5407SDavid Gibson static void close_htab_fd(sPAPRMachineState *spapr)
1086715c5407SDavid Gibson {
1087715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1088715c5407SDavid Gibson         close(spapr->htab_fd);
1089715c5407SDavid Gibson     }
1090715c5407SDavid Gibson     spapr->htab_fd = -1;
1091715c5407SDavid Gibson }
1092715c5407SDavid Gibson 
1093e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1094e57ca75cSDavid Gibson {
1095e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1096e57ca75cSDavid Gibson 
1097e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1098e57ca75cSDavid Gibson }
1099e57ca75cSDavid Gibson 
1100e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1101e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1102e57ca75cSDavid Gibson {
1103e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1104e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1105e57ca75cSDavid Gibson 
1106e57ca75cSDavid Gibson     if (!spapr->htab) {
1107e57ca75cSDavid Gibson         /*
1108e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1109e57ca75cSDavid Gibson          */
1110e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1111e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1112e57ca75cSDavid Gibson         return hptes;
1113e57ca75cSDavid Gibson     }
1114e57ca75cSDavid Gibson 
1115e57ca75cSDavid Gibson     /*
1116e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1117e57ca75cSDavid Gibson      * accessible PTEG.
1118e57ca75cSDavid Gibson      */
1119e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1120e57ca75cSDavid Gibson }
1121e57ca75cSDavid Gibson 
1122e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1123e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1124e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1125e57ca75cSDavid Gibson {
1126e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1127e57ca75cSDavid Gibson 
1128e57ca75cSDavid Gibson     if (!spapr->htab) {
1129e57ca75cSDavid Gibson         g_free((void *)hptes);
1130e57ca75cSDavid Gibson     }
1131e57ca75cSDavid Gibson 
1132e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1133e57ca75cSDavid Gibson }
1134e57ca75cSDavid Gibson 
1135e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1136e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1137e57ca75cSDavid Gibson {
1138e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1139e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1140e57ca75cSDavid Gibson 
1141e57ca75cSDavid Gibson     if (!spapr->htab) {
1142e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1143e57ca75cSDavid Gibson     } else {
1144e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1145e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1146e57ca75cSDavid Gibson     }
1147e57ca75cSDavid Gibson }
1148e57ca75cSDavid Gibson 
11498dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
11508dfe8e7fSDavid Gibson {
11518dfe8e7fSDavid Gibson     int shift;
11528dfe8e7fSDavid Gibson 
11538dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
11548dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
11558dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
11568dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
11578dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
11588dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
11598dfe8e7fSDavid Gibson     return shift;
11608dfe8e7fSDavid Gibson }
11618dfe8e7fSDavid Gibson 
1162c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1163c5f54f3eSDavid Gibson                                  Error **errp)
116453018216SPaolo Bonzini {
1165c5f54f3eSDavid Gibson     long rc;
116653018216SPaolo Bonzini 
1167c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
1168c5f54f3eSDavid Gibson     g_free(spapr->htab);
1169c5f54f3eSDavid Gibson     spapr->htab = NULL;
1170c5f54f3eSDavid Gibson     spapr->htab_shift = 0;
1171c5f54f3eSDavid Gibson     close_htab_fd(spapr);
117253018216SPaolo Bonzini 
1173c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1174c5f54f3eSDavid Gibson     if (rc < 0) {
1175c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1176c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1177c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1178c5f54f3eSDavid Gibson                          shift);
1179c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1180c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1181c5f54f3eSDavid Gibson     } else if (rc > 0) {
1182c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1183c5f54f3eSDavid Gibson         if (rc != shift) {
1184c5f54f3eSDavid Gibson             error_setg(errp,
1185c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1186c5f54f3eSDavid Gibson                        shift, rc);
11877735fedaSBharata B Rao         }
11887735fedaSBharata B Rao 
118953018216SPaolo Bonzini         spapr->htab_shift = shift;
1190c18ad9a5SDavid Gibson         spapr->htab = NULL;
1191b817772aSBharata B Rao     } else {
1192c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1193c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1194c5f54f3eSDavid Gibson         int i;
119501a57972SSamuel Mendoza-Jonas 
1196c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1197c5f54f3eSDavid Gibson         if (!spapr->htab) {
1198c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1199c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1200c5f54f3eSDavid Gibson             return;
1201b817772aSBharata B Rao         }
1202b817772aSBharata B Rao 
1203c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1204c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1205b817772aSBharata B Rao 
1206c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1207c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
12087735fedaSBharata B Rao         }
120953018216SPaolo Bonzini     }
121053018216SPaolo Bonzini }
121153018216SPaolo Bonzini 
12124f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
12139e3f9733SAlexander Graf {
12149e3f9733SAlexander Graf     bool matched = false;
12159e3f9733SAlexander Graf 
12169e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
12179e3f9733SAlexander Graf         matched = true;
12189e3f9733SAlexander Graf     }
12199e3f9733SAlexander Graf 
12209e3f9733SAlexander Graf     if (!matched) {
12219e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
12229e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
12239e3f9733SAlexander Graf         exit(1);
12249e3f9733SAlexander Graf     }
12259e3f9733SAlexander Graf }
12269e3f9733SAlexander Graf 
122753018216SPaolo Bonzini static void ppc_spapr_reset(void)
122853018216SPaolo Bonzini {
1229c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1230c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1231182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1232b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1233cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1234997b6cfcSDavid Gibson     void *fdt;
1235997b6cfcSDavid Gibson     int rc;
1236259186a7SAndreas Färber 
12379e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
12389e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
12399e3f9733SAlexander Graf 
1240c5f54f3eSDavid Gibson     /* Allocate and/or reset the hash page table */
1241c5f54f3eSDavid Gibson     spapr_reallocate_hpt(spapr,
1242c5f54f3eSDavid Gibson                          spapr_hpt_shift_for_ramsize(machine->maxram_size),
1243c5f54f3eSDavid Gibson                          &error_fatal);
1244c5f54f3eSDavid Gibson 
1245c5f54f3eSDavid Gibson     /* Update the RMA size if necessary */
1246c5f54f3eSDavid Gibson     if (spapr->vrma_adjust) {
1247c5f54f3eSDavid Gibson         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1248c5f54f3eSDavid Gibson                                           spapr->htab_shift);
1249c5f54f3eSDavid Gibson     }
125053018216SPaolo Bonzini 
125153018216SPaolo Bonzini     qemu_devices_reset();
125253018216SPaolo Bonzini 
1253b7d1f77aSBenjamin Herrenschmidt     /*
1254b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1255b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1256b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1257b7d1f77aSBenjamin Herrenschmidt      */
1258b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1259cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1260cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1261b7d1f77aSBenjamin Herrenschmidt 
12626787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
12636787d27bSMichael Roth      * negotiated options and start from scratch */
12646787d27bSMichael Roth     if (!spapr->cas_reboot) {
12656787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
12666787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
12676787d27bSMichael Roth     }
12686787d27bSMichael Roth 
1269cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
127053018216SPaolo Bonzini 
12712cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1272b7d1f77aSBenjamin Herrenschmidt 
1273997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1274997b6cfcSDavid Gibson 
1275997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1276997b6cfcSDavid Gibson     assert(rc == 0);
1277997b6cfcSDavid Gibson 
1278997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1279997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1280997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1281997b6cfcSDavid Gibson         exit(1);
1282997b6cfcSDavid Gibson     }
1283997b6cfcSDavid Gibson 
1284997b6cfcSDavid Gibson     /* Load the fdt */
1285997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1286cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1287997b6cfcSDavid Gibson     g_free(fdt);
1288997b6cfcSDavid Gibson 
128953018216SPaolo Bonzini     /* Set up the entry state */
1290182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1291cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1292182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1293182735efSAndreas Färber     first_cpu->halted = 0;
12941b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
129553018216SPaolo Bonzini 
12966787d27bSMichael Roth     spapr->cas_reboot = false;
129753018216SPaolo Bonzini }
129853018216SPaolo Bonzini 
129928e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
130053018216SPaolo Bonzini {
13012ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
13023978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
130353018216SPaolo Bonzini 
13043978b863SPaolo Bonzini     if (dinfo) {
13056231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
13066231a6daSMarkus Armbruster                             &error_fatal);
130753018216SPaolo Bonzini     }
130853018216SPaolo Bonzini 
130953018216SPaolo Bonzini     qdev_init_nofail(dev);
131053018216SPaolo Bonzini 
131153018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
131253018216SPaolo Bonzini }
131353018216SPaolo Bonzini 
131428e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
131528df36a1SDavid Gibson {
131628df36a1SDavid Gibson     DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
131728df36a1SDavid Gibson 
131828df36a1SDavid Gibson     qdev_init_nofail(dev);
131928df36a1SDavid Gibson     spapr->rtc = dev;
132074e5ae28SDavid Gibson 
132174e5ae28SDavid Gibson     object_property_add_alias(qdev_get_machine(), "rtc-time",
132274e5ae28SDavid Gibson                               OBJECT(spapr->rtc), "date", NULL);
132328df36a1SDavid Gibson }
132428df36a1SDavid Gibson 
132553018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
132614c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
132753018216SPaolo Bonzini {
132853018216SPaolo Bonzini     switch (vga_interface_type) {
132953018216SPaolo Bonzini     case VGA_NONE:
13307effdaa3SMark Wu         return false;
13317effdaa3SMark Wu     case VGA_DEVICE:
13327effdaa3SMark Wu         return true;
133353018216SPaolo Bonzini     case VGA_STD:
1334b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
133553018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
133653018216SPaolo Bonzini     default:
133714c6a894SDavid Gibson         error_setg(errp,
133814c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
133914c6a894SDavid Gibson         return false;
134053018216SPaolo Bonzini     }
134153018216SPaolo Bonzini }
134253018216SPaolo Bonzini 
1343880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1344880ae7deSDavid Gibson {
134528e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1346880ae7deSDavid Gibson     int err = 0;
1347880ae7deSDavid Gibson 
1348631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1349880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1350880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1351880ae7deSDavid Gibson      * value into the RTC device */
1352880ae7deSDavid Gibson     if (version_id < 3) {
1353880ae7deSDavid Gibson         err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1354880ae7deSDavid Gibson     }
1355880ae7deSDavid Gibson 
1356880ae7deSDavid Gibson     return err;
1357880ae7deSDavid Gibson }
1358880ae7deSDavid Gibson 
1359880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1360880ae7deSDavid Gibson {
1361880ae7deSDavid Gibson     return version_id < 3;
1362880ae7deSDavid Gibson }
1363880ae7deSDavid Gibson 
136462ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
136562ef3760SMichael Roth {
136662ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
136762ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
136862ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
136962ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
137062ef3760SMichael Roth     bool cas_needed;
137162ef3760SMichael Roth 
137262ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
137362ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
137462ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
137562ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
137662ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
137762ef3760SMichael Roth      * negotiatied on the source side.
137862ef3760SMichael Roth      *
137962ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
138062ef3760SMichael Roth      * are the only options available on the current machine/platform.
138162ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
138262ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
138362ef3760SMichael Roth      * compatibility.
138462ef3760SMichael Roth      *
138562ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
138662ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
138762ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
138862ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
138962ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
139062ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
139162ef3760SMichael Roth      *
139262ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
139362ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
139462ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
139562ef3760SMichael Roth      */
139662ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
139762ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
139862ef3760SMichael Roth 
139962ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
140062ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
140162ef3760SMichael Roth      * removed via machine options, which could generate a false positive
140262ef3760SMichael Roth      * that breaks migration.
140362ef3760SMichael Roth      */
140462ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
140562ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
140662ef3760SMichael Roth 
140762ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
140862ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
140962ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
141062ef3760SMichael Roth 
141162ef3760SMichael Roth     return cas_needed;
141262ef3760SMichael Roth }
141362ef3760SMichael Roth 
141462ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
141562ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
141662ef3760SMichael Roth     .version_id = 1,
141762ef3760SMichael Roth     .minimum_version_id = 1,
141862ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
141962ef3760SMichael Roth     .fields = (VMStateField[]) {
142062ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
142162ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
142262ef3760SMichael Roth         VMSTATE_END_OF_LIST()
142362ef3760SMichael Roth     },
142462ef3760SMichael Roth };
142562ef3760SMichael Roth 
14264be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
14274be21d56SDavid Gibson     .name = "spapr",
1428880ae7deSDavid Gibson     .version_id = 3,
14294be21d56SDavid Gibson     .minimum_version_id = 1,
1430880ae7deSDavid Gibson     .post_load = spapr_post_load,
14314be21d56SDavid Gibson     .fields = (VMStateField[]) {
1432880ae7deSDavid Gibson         /* used to be @next_irq */
1433880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
14344be21d56SDavid Gibson 
14354be21d56SDavid Gibson         /* RTC offset */
143628e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1437880ae7deSDavid Gibson 
143828e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
14394be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
14404be21d56SDavid Gibson     },
144162ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
144262ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
144362ef3760SMichael Roth         NULL
144462ef3760SMichael Roth     }
14454be21d56SDavid Gibson };
14464be21d56SDavid Gibson 
14474be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
14484be21d56SDavid Gibson {
144928e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
14504be21d56SDavid Gibson 
14514be21d56SDavid Gibson     /* "Iteration" header */
14524be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
14534be21d56SDavid Gibson 
1454e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1455e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1456e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1457e68cb8b4SAlexey Kardashevskiy     } else {
1458e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
14594be21d56SDavid Gibson     }
14604be21d56SDavid Gibson 
1461e68cb8b4SAlexey Kardashevskiy 
1462e68cb8b4SAlexey Kardashevskiy     return 0;
1463e68cb8b4SAlexey Kardashevskiy }
14644be21d56SDavid Gibson 
146528e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
14664be21d56SDavid Gibson                                  int64_t max_ns)
14674be21d56SDavid Gibson {
1468378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
14694be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
14704be21d56SDavid Gibson     int index = spapr->htab_save_index;
1471bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
14724be21d56SDavid Gibson 
14734be21d56SDavid Gibson     assert(spapr->htab_first_pass);
14744be21d56SDavid Gibson 
14754be21d56SDavid Gibson     do {
14764be21d56SDavid Gibson         int chunkstart;
14774be21d56SDavid Gibson 
14784be21d56SDavid Gibson         /* Consume invalid HPTEs */
14794be21d56SDavid Gibson         while ((index < htabslots)
14804be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
14814be21d56SDavid Gibson             index++;
14824be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
14834be21d56SDavid Gibson         }
14844be21d56SDavid Gibson 
14854be21d56SDavid Gibson         /* Consume valid HPTEs */
14864be21d56SDavid Gibson         chunkstart = index;
1487338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
14884be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
14894be21d56SDavid Gibson             index++;
14904be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
14914be21d56SDavid Gibson         }
14924be21d56SDavid Gibson 
14934be21d56SDavid Gibson         if (index > chunkstart) {
14944be21d56SDavid Gibson             int n_valid = index - chunkstart;
14954be21d56SDavid Gibson 
14964be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
14974be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
14984be21d56SDavid Gibson             qemu_put_be16(f, 0);
14994be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
15004be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
15014be21d56SDavid Gibson 
1502378bc217SDavid Gibson             if (has_timeout &&
1503378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
15044be21d56SDavid Gibson                 break;
15054be21d56SDavid Gibson             }
15064be21d56SDavid Gibson         }
15074be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
15084be21d56SDavid Gibson 
15094be21d56SDavid Gibson     if (index >= htabslots) {
15104be21d56SDavid Gibson         assert(index == htabslots);
15114be21d56SDavid Gibson         index = 0;
15124be21d56SDavid Gibson         spapr->htab_first_pass = false;
15134be21d56SDavid Gibson     }
15144be21d56SDavid Gibson     spapr->htab_save_index = index;
15154be21d56SDavid Gibson }
15164be21d56SDavid Gibson 
151728e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
15184be21d56SDavid Gibson                                 int64_t max_ns)
15194be21d56SDavid Gibson {
15204be21d56SDavid Gibson     bool final = max_ns < 0;
15214be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
15224be21d56SDavid Gibson     int examined = 0, sent = 0;
15234be21d56SDavid Gibson     int index = spapr->htab_save_index;
1524bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
15254be21d56SDavid Gibson 
15264be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
15274be21d56SDavid Gibson 
15284be21d56SDavid Gibson     do {
15294be21d56SDavid Gibson         int chunkstart, invalidstart;
15304be21d56SDavid Gibson 
15314be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
15324be21d56SDavid Gibson         while ((index < htabslots)
15334be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
15344be21d56SDavid Gibson             index++;
15354be21d56SDavid Gibson             examined++;
15364be21d56SDavid Gibson         }
15374be21d56SDavid Gibson 
15384be21d56SDavid Gibson         chunkstart = index;
15394be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1540338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
15414be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
15424be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
15434be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
15444be21d56SDavid Gibson             index++;
15454be21d56SDavid Gibson             examined++;
15464be21d56SDavid Gibson         }
15474be21d56SDavid Gibson 
15484be21d56SDavid Gibson         invalidstart = index;
15494be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1550338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
15514be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
15524be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
15534be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
15544be21d56SDavid Gibson             index++;
15554be21d56SDavid Gibson             examined++;
15564be21d56SDavid Gibson         }
15574be21d56SDavid Gibson 
15584be21d56SDavid Gibson         if (index > chunkstart) {
15594be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
15604be21d56SDavid Gibson             int n_invalid = index - invalidstart;
15614be21d56SDavid Gibson 
15624be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
15634be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
15644be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
15654be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
15664be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
15674be21d56SDavid Gibson             sent += index - chunkstart;
15684be21d56SDavid Gibson 
1569bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
15704be21d56SDavid Gibson                 break;
15714be21d56SDavid Gibson             }
15724be21d56SDavid Gibson         }
15734be21d56SDavid Gibson 
15744be21d56SDavid Gibson         if (examined >= htabslots) {
15754be21d56SDavid Gibson             break;
15764be21d56SDavid Gibson         }
15774be21d56SDavid Gibson 
15784be21d56SDavid Gibson         if (index >= htabslots) {
15794be21d56SDavid Gibson             assert(index == htabslots);
15804be21d56SDavid Gibson             index = 0;
15814be21d56SDavid Gibson         }
15824be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
15834be21d56SDavid Gibson 
15844be21d56SDavid Gibson     if (index >= htabslots) {
15854be21d56SDavid Gibson         assert(index == htabslots);
15864be21d56SDavid Gibson         index = 0;
15874be21d56SDavid Gibson     }
15884be21d56SDavid Gibson 
15894be21d56SDavid Gibson     spapr->htab_save_index = index;
15904be21d56SDavid Gibson 
1591e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
15924be21d56SDavid Gibson }
15934be21d56SDavid Gibson 
1594e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1595e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1596e68cb8b4SAlexey Kardashevskiy 
15974be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
15984be21d56SDavid Gibson {
159928e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1600715c5407SDavid Gibson     int fd;
1601e68cb8b4SAlexey Kardashevskiy     int rc = 0;
16024be21d56SDavid Gibson 
16034be21d56SDavid Gibson     /* Iteration header */
16044be21d56SDavid Gibson     qemu_put_be32(f, 0);
16054be21d56SDavid Gibson 
1606e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1607e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1608e68cb8b4SAlexey Kardashevskiy 
1609715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1610715c5407SDavid Gibson         if (fd < 0) {
1611715c5407SDavid Gibson             return fd;
161201a57972SSamuel Mendoza-Jonas         }
161301a57972SSamuel Mendoza-Jonas 
1614715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1615e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1616e68cb8b4SAlexey Kardashevskiy             return rc;
1617e68cb8b4SAlexey Kardashevskiy         }
1618e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
16194be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
16204be21d56SDavid Gibson     } else {
1621e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
16224be21d56SDavid Gibson     }
16234be21d56SDavid Gibson 
16244be21d56SDavid Gibson     /* End marker */
16254be21d56SDavid Gibson     qemu_put_be32(f, 0);
16264be21d56SDavid Gibson     qemu_put_be16(f, 0);
16274be21d56SDavid Gibson     qemu_put_be16(f, 0);
16284be21d56SDavid Gibson 
1629e68cb8b4SAlexey Kardashevskiy     return rc;
16304be21d56SDavid Gibson }
16314be21d56SDavid Gibson 
16324be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
16334be21d56SDavid Gibson {
163428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1635715c5407SDavid Gibson     int fd;
16364be21d56SDavid Gibson 
16374be21d56SDavid Gibson     /* Iteration header */
16384be21d56SDavid Gibson     qemu_put_be32(f, 0);
16394be21d56SDavid Gibson 
1640e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1641e68cb8b4SAlexey Kardashevskiy         int rc;
1642e68cb8b4SAlexey Kardashevskiy 
1643e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1644e68cb8b4SAlexey Kardashevskiy 
1645715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1646715c5407SDavid Gibson         if (fd < 0) {
1647715c5407SDavid Gibson             return fd;
164801a57972SSamuel Mendoza-Jonas         }
164901a57972SSamuel Mendoza-Jonas 
1650715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1651e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1652e68cb8b4SAlexey Kardashevskiy             return rc;
1653e68cb8b4SAlexey Kardashevskiy         }
1654e68cb8b4SAlexey Kardashevskiy     } else {
1655378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1656378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1657378bc217SDavid Gibson         }
16584be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1659e68cb8b4SAlexey Kardashevskiy     }
16604be21d56SDavid Gibson 
16614be21d56SDavid Gibson     /* End marker */
16624be21d56SDavid Gibson     qemu_put_be32(f, 0);
16634be21d56SDavid Gibson     qemu_put_be16(f, 0);
16644be21d56SDavid Gibson     qemu_put_be16(f, 0);
16654be21d56SDavid Gibson 
16664be21d56SDavid Gibson     return 0;
16674be21d56SDavid Gibson }
16684be21d56SDavid Gibson 
16694be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
16704be21d56SDavid Gibson {
167128e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
16724be21d56SDavid Gibson     uint32_t section_hdr;
1673e68cb8b4SAlexey Kardashevskiy     int fd = -1;
16744be21d56SDavid Gibson 
16754be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
167698a5d100SDavid Gibson         error_report("htab_load() bad version");
16774be21d56SDavid Gibson         return -EINVAL;
16784be21d56SDavid Gibson     }
16794be21d56SDavid Gibson 
16804be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
16814be21d56SDavid Gibson 
16824be21d56SDavid Gibson     if (section_hdr) {
16839897e462SGreg Kurz         Error *local_err = NULL;
1684c5f54f3eSDavid Gibson 
1685c5f54f3eSDavid Gibson         /* First section gives the htab size */
1686c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1687c5f54f3eSDavid Gibson         if (local_err) {
1688c5f54f3eSDavid Gibson             error_report_err(local_err);
16894be21d56SDavid Gibson             return -EINVAL;
16904be21d56SDavid Gibson         }
16914be21d56SDavid Gibson         return 0;
16924be21d56SDavid Gibson     }
16934be21d56SDavid Gibson 
1694e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1695e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1696e68cb8b4SAlexey Kardashevskiy 
1697e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1698e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
169998a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1700e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1701e68cb8b4SAlexey Kardashevskiy         }
1702e68cb8b4SAlexey Kardashevskiy     }
1703e68cb8b4SAlexey Kardashevskiy 
17044be21d56SDavid Gibson     while (true) {
17054be21d56SDavid Gibson         uint32_t index;
17064be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
17074be21d56SDavid Gibson 
17084be21d56SDavid Gibson         index = qemu_get_be32(f);
17094be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
17104be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
17114be21d56SDavid Gibson 
17124be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
17134be21d56SDavid Gibson             /* End of Stream */
17144be21d56SDavid Gibson             break;
17154be21d56SDavid Gibson         }
17164be21d56SDavid Gibson 
1717e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
17184be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
17194be21d56SDavid Gibson             /* Bad index in stream */
172098a5d100SDavid Gibson             error_report(
172198a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
172298a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
17234be21d56SDavid Gibson             return -EINVAL;
17244be21d56SDavid Gibson         }
17254be21d56SDavid Gibson 
1726e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
17274be21d56SDavid Gibson             if (n_valid) {
17284be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
17294be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
17304be21d56SDavid Gibson             }
17314be21d56SDavid Gibson             if (n_invalid) {
17324be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
17334be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
17344be21d56SDavid Gibson             }
1735e68cb8b4SAlexey Kardashevskiy         } else {
1736e68cb8b4SAlexey Kardashevskiy             int rc;
1737e68cb8b4SAlexey Kardashevskiy 
1738e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1739e68cb8b4SAlexey Kardashevskiy 
1740e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1741e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1742e68cb8b4SAlexey Kardashevskiy                 return rc;
1743e68cb8b4SAlexey Kardashevskiy             }
1744e68cb8b4SAlexey Kardashevskiy         }
1745e68cb8b4SAlexey Kardashevskiy     }
1746e68cb8b4SAlexey Kardashevskiy 
1747e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1748e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1749e68cb8b4SAlexey Kardashevskiy         close(fd);
17504be21d56SDavid Gibson     }
17514be21d56SDavid Gibson 
17524be21d56SDavid Gibson     return 0;
17534be21d56SDavid Gibson }
17544be21d56SDavid Gibson 
1755c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1756c573fc03SThomas Huth {
1757c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1758c573fc03SThomas Huth 
1759c573fc03SThomas Huth     close_htab_fd(spapr);
1760c573fc03SThomas Huth }
1761c573fc03SThomas Huth 
17624be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
17634be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
17644be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1765a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1766c573fc03SThomas Huth     .cleanup = htab_cleanup,
17674be21d56SDavid Gibson     .load_state = htab_load,
17684be21d56SDavid Gibson };
17694be21d56SDavid Gibson 
17705b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
17715b2128d2SAlexander Graf                            Error **errp)
17725b2128d2SAlexander Graf {
17735b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
17745b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
17755b2128d2SAlexander Graf }
17765b2128d2SAlexander Graf 
1777224245bfSDavid Gibson /*
1778224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1779224245bfSDavid Gibson  *
1780224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1781224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1782224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1783224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1784224245bfSDavid Gibson  */
1785224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1786224245bfSDavid Gibson {
1787224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1788224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1789224245bfSDavid Gibson 
1790224245bfSDavid Gibson     if (d) {
1791224245bfSDavid Gibson         device_reset(d);
1792224245bfSDavid Gibson     }
1793224245bfSDavid Gibson }
1794224245bfSDavid Gibson 
1795224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1796224245bfSDavid Gibson {
1797224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1798224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1799e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1800224245bfSDavid Gibson     int i;
1801224245bfSDavid Gibson 
1802224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1803224245bfSDavid Gibson         sPAPRDRConnector *drc;
1804224245bfSDavid Gibson         uint64_t addr;
1805224245bfSDavid Gibson 
1806e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1807224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1808224245bfSDavid Gibson                                      addr/lmb_size);
1809224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1810224245bfSDavid Gibson     }
1811224245bfSDavid Gibson }
1812224245bfSDavid Gibson 
1813224245bfSDavid Gibson /*
1814224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1815224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1816224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1817224245bfSDavid Gibson  */
18187c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1819224245bfSDavid Gibson {
1820224245bfSDavid Gibson     int i;
1821224245bfSDavid Gibson 
18227c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
18237c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
18247c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
18257c150d6fSDavid Gibson                    machine->ram_size,
1826224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
18277c150d6fSDavid Gibson         return;
18287c150d6fSDavid Gibson     }
18297c150d6fSDavid Gibson 
18307c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
18317c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
18327c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
18337c150d6fSDavid Gibson                    machine->ram_size,
18347c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
18357c150d6fSDavid Gibson         return;
1836224245bfSDavid Gibson     }
1837224245bfSDavid Gibson 
1838224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1839224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
18407c150d6fSDavid Gibson             error_setg(errp,
18417c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
18427c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
18437c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
1844224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
18457c150d6fSDavid Gibson             return;
1846224245bfSDavid Gibson         }
1847224245bfSDavid Gibson     }
1848224245bfSDavid Gibson }
1849224245bfSDavid Gibson 
1850535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
1851535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1852535455fdSIgor Mammedov {
1853535455fdSIgor Mammedov     int index = id / smp_threads;
1854535455fdSIgor Mammedov 
1855535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
1856535455fdSIgor Mammedov         return NULL;
1857535455fdSIgor Mammedov     }
1858535455fdSIgor Mammedov     if (idx) {
1859535455fdSIgor Mammedov         *idx = index;
1860535455fdSIgor Mammedov     }
1861535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
1862535455fdSIgor Mammedov }
1863535455fdSIgor Mammedov 
18640c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
18650c86d0fdSDavid Gibson {
18660c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
18670c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
18680c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
18690c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
1870535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
1871535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
18720c86d0fdSDavid Gibson     int i;
18730c86d0fdSDavid Gibson 
18740c86d0fdSDavid Gibson     if (!type) {
18750c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
18760c86d0fdSDavid Gibson         exit(1);
18770c86d0fdSDavid Gibson     }
18780c86d0fdSDavid Gibson 
1879535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
1880c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
18810c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
18820c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
18830c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
18840c86d0fdSDavid Gibson             exit(1);
18850c86d0fdSDavid Gibson         }
18860c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
18870c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
18880c86d0fdSDavid Gibson                          max_cpus, smp_threads);
18890c86d0fdSDavid Gibson             exit(1);
18900c86d0fdSDavid Gibson         }
18910c86d0fdSDavid Gibson     } else {
18920c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
18930c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
18940c86d0fdSDavid Gibson             exit(1);
18950c86d0fdSDavid Gibson         }
1896535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
18970c86d0fdSDavid Gibson     }
18980c86d0fdSDavid Gibson 
1899535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
19000c86d0fdSDavid Gibson         int core_id = i * smp_threads;
19010c86d0fdSDavid Gibson 
1902c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
19030c86d0fdSDavid Gibson             sPAPRDRConnector *drc =
19040c86d0fdSDavid Gibson                 spapr_dr_connector_new(OBJECT(spapr),
19050c86d0fdSDavid Gibson                                        SPAPR_DR_CONNECTOR_TYPE_CPU,
19060c86d0fdSDavid Gibson                                        (core_id / smp_threads) * smt);
19070c86d0fdSDavid Gibson 
19080c86d0fdSDavid Gibson             qemu_register_reset(spapr_drc_reset, drc);
19090c86d0fdSDavid Gibson         }
19100c86d0fdSDavid Gibson 
1911535455fdSIgor Mammedov         if (i < boot_cores_nr) {
19120c86d0fdSDavid Gibson             Object *core  = object_new(type);
19130c86d0fdSDavid Gibson             int nr_threads = smp_threads;
19140c86d0fdSDavid Gibson 
19150c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
19160c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
19170c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
19180c86d0fdSDavid Gibson             }
19190c86d0fdSDavid Gibson 
19200c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
19210c86d0fdSDavid Gibson                                     &error_fatal);
19220c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
19230c86d0fdSDavid Gibson                                     &error_fatal);
19240c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
19250c86d0fdSDavid Gibson         }
19260c86d0fdSDavid Gibson     }
19270c86d0fdSDavid Gibson     g_free(type);
19280c86d0fdSDavid Gibson }
19290c86d0fdSDavid Gibson 
193053018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
19313ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
193253018216SPaolo Bonzini {
193328e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1934224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
19353ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
19363ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
193753018216SPaolo Bonzini     PCIHostState *phb;
193853018216SPaolo Bonzini     int i;
193953018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
194053018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1941658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1942658fa66bSAlexey Kardashevskiy     void *rma = NULL;
194353018216SPaolo Bonzini     hwaddr rma_alloc_size;
1944b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
1945b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
194653018216SPaolo Bonzini     char *filename;
194794a94e4cSBharata B Rao     int smt = kvmppc_smt_threads();
194853018216SPaolo Bonzini 
1949226419d6SMichael S. Tsirkin     msi_nonbroken = true;
195053018216SPaolo Bonzini 
195153018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
195253018216SPaolo Bonzini 
195353018216SPaolo Bonzini     /* Allocate RMA if necessary */
1954658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
195553018216SPaolo Bonzini 
195653018216SPaolo Bonzini     if (rma_alloc_size == -1) {
1957730fce59SThomas Huth         error_report("Unable to create RMA");
195853018216SPaolo Bonzini         exit(1);
195953018216SPaolo Bonzini     }
196053018216SPaolo Bonzini 
1961c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
196253018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
196353018216SPaolo Bonzini     } else {
1964c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
196553018216SPaolo Bonzini 
196653018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
196753018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
196853018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
196953018216SPaolo Bonzini          *
197053018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
197153018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
197253018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
197353018216SPaolo Bonzini          * isn't determined yet.
197453018216SPaolo Bonzini          */
197553018216SPaolo Bonzini         if (kvm_enabled()) {
197653018216SPaolo Bonzini             spapr->vrma_adjust = 1;
197753018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
197853018216SPaolo Bonzini         }
1979912acdf4SBenjamin Herrenschmidt 
1980912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
1981912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
1982912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
1983912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
1984912acdf4SBenjamin Herrenschmidt          */
1985912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
198653018216SPaolo Bonzini     }
198753018216SPaolo Bonzini 
1988c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
1989d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
1990c4177479SAlexey Kardashevskiy                      spapr->rma_size);
1991c4177479SAlexey Kardashevskiy         exit(1);
1992c4177479SAlexey Kardashevskiy     }
1993c4177479SAlexey Kardashevskiy 
1994b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1995b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
199653018216SPaolo Bonzini 
19977b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
199827f24582SBenjamin Herrenschmidt     spapr->xics = xics_system_init(machine,
199994a94e4cSBharata B Rao                                    DIV_ROUND_UP(max_cpus * smt, smp_threads),
2000161deaf2SBenjamin Herrenschmidt                                    XICS_IRQS_SPAPR, &error_fatal);
20017b565160SDavid Gibson 
2002facdb8b6SMichael Roth     /* Set up containers for ibm,client-set-architecture negotiated options */
2003facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2004facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2005facdb8b6SMichael Roth 
2006224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2007facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
20087c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2009224245bfSDavid Gibson     }
2010224245bfSDavid Gibson 
2011417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2012417ece33SMichael Roth 
2013ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2014ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2015ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2016ffbb1705SMichael Roth     }
2017ffbb1705SMichael Roth 
201853018216SPaolo Bonzini     /* init CPUs */
201919fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
20203daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
202153018216SPaolo Bonzini     }
202294a94e4cSBharata B Rao 
2023e703d2f7SGreg Kurz     ppc_cpu_parse_features(machine->cpu_model);
2024e703d2f7SGreg Kurz 
20250c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
202653018216SPaolo Bonzini 
2027026bfd89SDavid Gibson     if (kvm_enabled()) {
2028026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2029026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2030ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
20315145ad4fSNathan Whitehorn 
20325145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
20335145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2034026bfd89SDavid Gibson     }
2035026bfd89SDavid Gibson 
203653018216SPaolo Bonzini     /* allocate RAM */
2037f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2038fb164994SDavid Gibson                                          machine->ram_size);
2039f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
204053018216SPaolo Bonzini 
2041658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2042658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2043658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2044658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2045658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2046658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2047658fa66bSAlexey Kardashevskiy     }
2048658fa66bSAlexey Kardashevskiy 
20494a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
20504a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
20514a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
205271c9a3ddSBharata B Rao         /*
205371c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
205471c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
205571c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
205671c9a3ddSBharata B Rao          */
205771c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
205871c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
20594a1c9cf0SBharata B Rao 
206071c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
206171c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
206271c9a3ddSBharata B Rao         }
206371c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2064d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2065d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
206671c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2067d54e4d76SDavid Gibson             exit(1);
20684a1c9cf0SBharata B Rao         }
20694a1c9cf0SBharata B Rao 
20704a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
20714a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
20724a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
20734a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
20744a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
20754a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
20764a1c9cf0SBharata B Rao     }
20774a1c9cf0SBharata B Rao 
2078224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2079224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2080224245bfSDavid Gibson     }
2081224245bfSDavid Gibson 
208253018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
20834c56440dSStefan Weil     if (!filename) {
2084730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
20854c56440dSStefan Weil         exit(1);
20864c56440dSStefan Weil     }
2087b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
20888afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
20898afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
20908afc22a2SZhou Jie         exit(1);
20918afc22a2SZhou Jie     }
2092b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2093b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2094730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
209553018216SPaolo Bonzini         exit(1);
209653018216SPaolo Bonzini     }
209753018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2098730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
20992f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
210053018216SPaolo Bonzini         exit(1);
210153018216SPaolo Bonzini     }
210253018216SPaolo Bonzini     g_free(filename);
210353018216SPaolo Bonzini 
2104ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
210553018216SPaolo Bonzini     spapr_events_init(spapr);
210653018216SPaolo Bonzini 
210712f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
210828df36a1SDavid Gibson     spapr_rtc_create(spapr);
210912f42174SDavid Gibson 
211053018216SPaolo Bonzini     /* Set up VIO bus */
211153018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
211253018216SPaolo Bonzini 
211353018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
211453018216SPaolo Bonzini         if (serial_hds[i]) {
211553018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
211653018216SPaolo Bonzini         }
211753018216SPaolo Bonzini     }
211853018216SPaolo Bonzini 
211953018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
212053018216SPaolo Bonzini     spapr_create_nvram(spapr);
212153018216SPaolo Bonzini 
212253018216SPaolo Bonzini     /* Set up PCI */
212353018216SPaolo Bonzini     spapr_pci_rtas_init();
212453018216SPaolo Bonzini 
212589dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
212653018216SPaolo Bonzini 
212753018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
212853018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
212953018216SPaolo Bonzini 
213053018216SPaolo Bonzini         if (!nd->model) {
213153018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
213253018216SPaolo Bonzini         }
213353018216SPaolo Bonzini 
213453018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
213553018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
213653018216SPaolo Bonzini         } else {
213729b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
213853018216SPaolo Bonzini         }
213953018216SPaolo Bonzini     }
214053018216SPaolo Bonzini 
214153018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
214253018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
214353018216SPaolo Bonzini     }
214453018216SPaolo Bonzini 
214553018216SPaolo Bonzini     /* Graphics */
214614c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
214753018216SPaolo Bonzini         spapr->has_graphics = true;
2148c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
214953018216SPaolo Bonzini     }
215053018216SPaolo Bonzini 
21514ee9ced9SMarcel Apfelbaum     if (machine->usb) {
215257040d45SThomas Huth         if (smc->use_ohci_by_default) {
215353018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
215457040d45SThomas Huth         } else {
215557040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
215657040d45SThomas Huth         }
2157c86580b8SMarkus Armbruster 
215853018216SPaolo Bonzini         if (spapr->has_graphics) {
2159c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2160c86580b8SMarkus Armbruster 
2161c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2162c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
216353018216SPaolo Bonzini         }
216453018216SPaolo Bonzini     }
216553018216SPaolo Bonzini 
216653018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2167d54e4d76SDavid Gibson         error_report(
2168d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2169d54e4d76SDavid Gibson             MIN_RMA_SLOF);
217053018216SPaolo Bonzini         exit(1);
217153018216SPaolo Bonzini     }
217253018216SPaolo Bonzini 
217353018216SPaolo Bonzini     if (kernel_filename) {
217453018216SPaolo Bonzini         uint64_t lowaddr = 0;
217553018216SPaolo Bonzini 
2176a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2177a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2178a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2179a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2180a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2181a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2182a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
21837ef295eaSPeter Crosthwaite                                           0, 0);
2184a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
218516457e7fSBenjamin Herrenschmidt         }
2186a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2187a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2188a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
218953018216SPaolo Bonzini             exit(1);
219053018216SPaolo Bonzini         }
219153018216SPaolo Bonzini 
219253018216SPaolo Bonzini         /* load initrd */
219353018216SPaolo Bonzini         if (initrd_filename) {
219453018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
219553018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
219653018216SPaolo Bonzini              */
2197a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2198a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2199a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2200a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2201a19f7fb0SDavid Gibson                                                      load_limit
2202a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2203a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2204d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
220553018216SPaolo Bonzini                              initrd_filename);
220653018216SPaolo Bonzini                 exit(1);
220753018216SPaolo Bonzini             }
220853018216SPaolo Bonzini         }
220953018216SPaolo Bonzini     }
221053018216SPaolo Bonzini 
22118e7ea787SAndreas Färber     if (bios_name == NULL) {
22128e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
22138e7ea787SAndreas Färber     }
22148e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
22154c56440dSStefan Weil     if (!filename) {
221668fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
22174c56440dSStefan Weil         exit(1);
22184c56440dSStefan Weil     }
221953018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
222068fea5a0SThomas Huth     if (fw_size <= 0) {
222168fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
222253018216SPaolo Bonzini         exit(1);
222353018216SPaolo Bonzini     }
222453018216SPaolo Bonzini     g_free(filename);
222553018216SPaolo Bonzini 
222628e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
222728e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
222828e02042SDavid Gibson      * which predated MachineState but had a similar function */
22294be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
22304be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
22314be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
22324be21d56SDavid Gibson 
223346503c2bSMichael Roth     /* used by RTAS */
223446503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
223546503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
223646503c2bSMichael Roth 
22375b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
223842043e4fSLaurent Vivier 
223942043e4fSLaurent Vivier     /* to stop and start vmclock */
224042043e4fSLaurent Vivier     if (kvm_enabled()) {
224142043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
224242043e4fSLaurent Vivier                                          &spapr->tb);
224342043e4fSLaurent Vivier     }
224453018216SPaolo Bonzini }
224553018216SPaolo Bonzini 
2246135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2247135a129aSAneesh Kumar K.V {
2248135a129aSAneesh Kumar K.V     if (!vm_type) {
2249135a129aSAneesh Kumar K.V         return 0;
2250135a129aSAneesh Kumar K.V     }
2251135a129aSAneesh Kumar K.V 
2252135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2253135a129aSAneesh Kumar K.V         return 1;
2254135a129aSAneesh Kumar K.V     }
2255135a129aSAneesh Kumar K.V 
2256135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2257135a129aSAneesh Kumar K.V         return 2;
2258135a129aSAneesh Kumar K.V     }
2259135a129aSAneesh Kumar K.V 
2260135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2261135a129aSAneesh Kumar K.V     exit(1);
2262135a129aSAneesh Kumar K.V }
2263135a129aSAneesh Kumar K.V 
226471461b0fSAlexey Kardashevskiy /*
2265627b84f4SGonglei  * Implementation of an interface to adjust firmware path
226671461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
226771461b0fSAlexey Kardashevskiy  */
226871461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
226971461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
227071461b0fSAlexey Kardashevskiy {
227171461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
227271461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
227371461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
227471461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
227571461b0fSAlexey Kardashevskiy 
227671461b0fSAlexey Kardashevskiy     if (d) {
227771461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
227871461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
227971461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
228071461b0fSAlexey Kardashevskiy 
228171461b0fSAlexey Kardashevskiy         if (spapr) {
228271461b0fSAlexey Kardashevskiy             /*
228371461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
228471461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
228571461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
228671461b0fSAlexey Kardashevskiy              */
228771461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
228871461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
228971461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
229071461b0fSAlexey Kardashevskiy         } else if (virtio) {
229171461b0fSAlexey Kardashevskiy             /*
229271461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
229371461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
229471461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
229571461b0fSAlexey Kardashevskiy              * the actual binding is:
229671461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
229771461b0fSAlexey Kardashevskiy              */
229871461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
229971461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
230071461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
230171461b0fSAlexey Kardashevskiy         } else if (usb) {
230271461b0fSAlexey Kardashevskiy             /*
230371461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
230471461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
230571461b0fSAlexey Kardashevskiy              */
230671461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
230771461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
230871461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
230971461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
231071461b0fSAlexey Kardashevskiy         }
231171461b0fSAlexey Kardashevskiy     }
231271461b0fSAlexey Kardashevskiy 
2313b99260ebSThomas Huth     /*
2314b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2315b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2316b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2317b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2318b99260ebSThomas Huth      */
2319b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2320b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2321b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2322b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2323b99260ebSThomas Huth         }
2324b99260ebSThomas Huth     }
2325b99260ebSThomas Huth 
232671461b0fSAlexey Kardashevskiy     if (phb) {
232771461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
232871461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
232971461b0fSAlexey Kardashevskiy     }
233071461b0fSAlexey Kardashevskiy 
233171461b0fSAlexey Kardashevskiy     return NULL;
233271461b0fSAlexey Kardashevskiy }
233371461b0fSAlexey Kardashevskiy 
233423825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
233523825581SEduardo Habkost {
233628e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
233723825581SEduardo Habkost 
233828e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
233923825581SEduardo Habkost }
234023825581SEduardo Habkost 
234123825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
234223825581SEduardo Habkost {
234328e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
234423825581SEduardo Habkost 
234528e02042SDavid Gibson     g_free(spapr->kvm_type);
234628e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
234723825581SEduardo Habkost }
234823825581SEduardo Habkost 
2349f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2350f6229214SMichael Roth {
2351f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2352f6229214SMichael Roth 
2353f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2354f6229214SMichael Roth }
2355f6229214SMichael Roth 
2356f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2357f6229214SMichael Roth                                             Error **errp)
2358f6229214SMichael Roth {
2359f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2360f6229214SMichael Roth 
2361f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2362f6229214SMichael Roth }
2363f6229214SMichael Roth 
236423825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
236523825581SEduardo Habkost {
2366715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2367715c5407SDavid Gibson 
2368715c5407SDavid Gibson     spapr->htab_fd = -1;
2369f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
237023825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
237123825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
237249d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
237349d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
237449d2e648SMarcel Apfelbaum                                     NULL);
2375f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2376f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2377f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2378f6229214SMichael Roth                             NULL);
2379f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2380f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2381f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2382f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2383f6229214SMichael Roth                                     NULL);
238423825581SEduardo Habkost }
238523825581SEduardo Habkost 
238687bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
238787bbdd9cSDavid Gibson {
238887bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
238987bbdd9cSDavid Gibson 
239087bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
239187bbdd9cSDavid Gibson }
239287bbdd9cSDavid Gibson 
23931c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
239434316482SAlexey Kardashevskiy {
239534316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
239634316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
239734316482SAlexey Kardashevskiy }
239834316482SAlexey Kardashevskiy 
239934316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
240034316482SAlexey Kardashevskiy {
240134316482SAlexey Kardashevskiy     CPUState *cs;
240234316482SAlexey Kardashevskiy 
240334316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
24041c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
240534316482SAlexey Kardashevskiy     }
240634316482SAlexey Kardashevskiy }
240734316482SAlexey Kardashevskiy 
240879b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
240979b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
241079b78a6bSMichael Roth                            Error **errp)
2411c20d332aSBharata B Rao {
2412c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2413c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2414c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2415c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2416c20d332aSBharata B Rao     void *fdt;
241779b78a6bSMichael Roth     uint64_t addr = addr_start;
2418c20d332aSBharata B Rao 
2419c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2420c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2421c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2422c20d332aSBharata B Rao         g_assert(drc);
2423c20d332aSBharata B Rao 
2424c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2425c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2426c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2427c20d332aSBharata B Rao 
2428c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2429c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2430c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
24315c0139a8SMichael Roth         if (!dev->hotplugged) {
24325c0139a8SMichael Roth             /* guests expect coldplugged LMBs to be pre-allocated */
24335c0139a8SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
24345c0139a8SMichael Roth             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
24355c0139a8SMichael Roth         }
2436c20d332aSBharata B Rao     }
24375dd5238cSJianjun Duan     /* send hotplug notification to the
24385dd5238cSJianjun Duan      * guest only in case of hotplugged memory
24395dd5238cSJianjun Duan      */
24405dd5238cSJianjun Duan     if (dev->hotplugged) {
244179b78a6bSMichael Roth         if (dedicated_hp_event_source) {
244279b78a6bSMichael Roth             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
244379b78a6bSMichael Roth                     addr_start / SPAPR_MEMORY_BLOCK_SIZE);
244479b78a6bSMichael Roth             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
244579b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
244679b78a6bSMichael Roth                                                    nr_lmbs,
244779b78a6bSMichael Roth                                                    drck->get_index(drc));
244879b78a6bSMichael Roth         } else {
244979b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
245079b78a6bSMichael Roth                                            nr_lmbs);
245179b78a6bSMichael Roth         }
2452c20d332aSBharata B Rao     }
24535dd5238cSJianjun Duan }
2454c20d332aSBharata B Rao 
2455c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2456c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2457c20d332aSBharata B Rao {
2458c20d332aSBharata B Rao     Error *local_err = NULL;
2459c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2460c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2461c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2462c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2463c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2464c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2465c20d332aSBharata B Rao     uint64_t addr;
2466df587133SThomas Huth     char *mem_dev;
2467c20d332aSBharata B Rao 
2468c20d332aSBharata B Rao     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2469c20d332aSBharata B Rao         error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2470c20d332aSBharata B Rao                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2471c20d332aSBharata B Rao         goto out;
2472c20d332aSBharata B Rao     }
2473c20d332aSBharata B Rao 
2474df587133SThomas Huth     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2475df587133SThomas Huth     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2476df587133SThomas Huth         error_setg(&local_err, "Memory backend has bad page size. "
2477df587133SThomas Huth                    "Use 'memory-backend-file' with correct mem-path.");
2478df587133SThomas Huth         goto out;
2479df587133SThomas Huth     }
2480df587133SThomas Huth 
2481d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2482c20d332aSBharata B Rao     if (local_err) {
2483c20d332aSBharata B Rao         goto out;
2484c20d332aSBharata B Rao     }
2485c20d332aSBharata B Rao 
2486c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2487c20d332aSBharata B Rao     if (local_err) {
2488c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2489c20d332aSBharata B Rao         goto out;
2490c20d332aSBharata B Rao     }
2491c20d332aSBharata B Rao 
249279b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
249379b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
249479b78a6bSMichael Roth                    &error_abort);
2495c20d332aSBharata B Rao 
2496c20d332aSBharata B Rao out:
2497c20d332aSBharata B Rao     error_propagate(errp, local_err);
2498c20d332aSBharata B Rao }
2499c20d332aSBharata B Rao 
2500cf632463SBharata B Rao typedef struct sPAPRDIMMState {
2501cf632463SBharata B Rao     uint32_t nr_lmbs;
2502cf632463SBharata B Rao } sPAPRDIMMState;
2503cf632463SBharata B Rao 
2504cf632463SBharata B Rao static void spapr_lmb_release(DeviceState *dev, void *opaque)
2505cf632463SBharata B Rao {
2506cf632463SBharata B Rao     sPAPRDIMMState *ds = (sPAPRDIMMState *)opaque;
2507cf632463SBharata B Rao     HotplugHandler *hotplug_ctrl;
2508cf632463SBharata B Rao 
2509cf632463SBharata B Rao     if (--ds->nr_lmbs) {
2510cf632463SBharata B Rao         return;
2511cf632463SBharata B Rao     }
2512cf632463SBharata B Rao 
2513cf632463SBharata B Rao     g_free(ds);
2514cf632463SBharata B Rao 
2515cf632463SBharata B Rao     /*
2516cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
2517cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
2518cf632463SBharata B Rao      */
2519cf632463SBharata B Rao     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2520cf632463SBharata B Rao     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2521cf632463SBharata B Rao }
2522cf632463SBharata B Rao 
2523cf632463SBharata B Rao static void spapr_del_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
2524cf632463SBharata B Rao                            Error **errp)
2525cf632463SBharata B Rao {
2526cf632463SBharata B Rao     sPAPRDRConnector *drc;
2527cf632463SBharata B Rao     sPAPRDRConnectorClass *drck;
2528cf632463SBharata B Rao     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
2529cf632463SBharata B Rao     int i;
2530cf632463SBharata B Rao     sPAPRDIMMState *ds = g_malloc0(sizeof(sPAPRDIMMState));
2531cf632463SBharata B Rao     uint64_t addr = addr_start;
2532cf632463SBharata B Rao 
2533cf632463SBharata B Rao     ds->nr_lmbs = nr_lmbs;
2534cf632463SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2535cf632463SBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2536cf632463SBharata B Rao                 addr / SPAPR_MEMORY_BLOCK_SIZE);
2537cf632463SBharata B Rao         g_assert(drc);
2538cf632463SBharata B Rao 
2539cf632463SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2540cf632463SBharata B Rao         drck->detach(drc, dev, spapr_lmb_release, ds, errp);
2541cf632463SBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2542cf632463SBharata B Rao     }
2543cf632463SBharata B Rao 
2544cf632463SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2545cf632463SBharata B Rao                                    addr_start / SPAPR_MEMORY_BLOCK_SIZE);
2546cf632463SBharata B Rao     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2547cf632463SBharata B Rao     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
2548cf632463SBharata B Rao                                               nr_lmbs,
2549cf632463SBharata B Rao                                               drck->get_index(drc));
2550cf632463SBharata B Rao }
2551cf632463SBharata B Rao 
2552cf632463SBharata B Rao static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2553cf632463SBharata B Rao                                 Error **errp)
2554cf632463SBharata B Rao {
2555cf632463SBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2556cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2557cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2558cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2559cf632463SBharata B Rao 
2560cf632463SBharata B Rao     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2561cf632463SBharata B Rao     object_unparent(OBJECT(dev));
2562cf632463SBharata B Rao }
2563cf632463SBharata B Rao 
2564cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
2565cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
2566cf632463SBharata B Rao {
2567cf632463SBharata B Rao     Error *local_err = NULL;
2568cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2569cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2570cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2571cf632463SBharata B Rao     uint64_t size = memory_region_size(mr);
2572cf632463SBharata B Rao     uint64_t addr;
2573cf632463SBharata B Rao 
2574cf632463SBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2575cf632463SBharata B Rao     if (local_err) {
2576cf632463SBharata B Rao         goto out;
2577cf632463SBharata B Rao     }
2578cf632463SBharata B Rao 
2579cf632463SBharata B Rao     spapr_del_lmbs(dev, addr, size, &error_abort);
2580cf632463SBharata B Rao out:
2581cf632463SBharata B Rao     error_propagate(errp, local_err);
2582cf632463SBharata B Rao }
2583cf632463SBharata B Rao 
2584af81cf32SBharata B Rao void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2585af81cf32SBharata B Rao                                     sPAPRMachineState *spapr)
2586af81cf32SBharata B Rao {
2587af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2588af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2589af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2590af81cf32SBharata B Rao     void *fdt;
2591af81cf32SBharata B Rao     int offset, fdt_size;
2592af81cf32SBharata B Rao     char *nodename;
2593af81cf32SBharata B Rao 
2594af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2595af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2596af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2597af81cf32SBharata B Rao 
2598af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2599af81cf32SBharata B Rao     g_free(nodename);
2600af81cf32SBharata B Rao 
2601af81cf32SBharata B Rao     *fdt_offset = offset;
2602af81cf32SBharata B Rao     return fdt;
2603af81cf32SBharata B Rao }
2604af81cf32SBharata B Rao 
2605115debf2SIgor Mammedov static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2606115debf2SIgor Mammedov                               Error **errp)
2607ff9006ddSIgor Mammedov {
2608535455fdSIgor Mammedov     MachineState *ms = MACHINE(qdev_get_machine());
2609ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2610535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
2611ff9006ddSIgor Mammedov 
2612535455fdSIgor Mammedov     core_slot->cpu = NULL;
2613ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
2614ff9006ddSIgor Mammedov }
2615ff9006ddSIgor Mammedov 
2616115debf2SIgor Mammedov static void spapr_core_release(DeviceState *dev, void *opaque)
2617115debf2SIgor Mammedov {
2618115debf2SIgor Mammedov     HotplugHandler *hotplug_ctrl;
2619115debf2SIgor Mammedov 
2620115debf2SIgor Mammedov     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2621115debf2SIgor Mammedov     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2622115debf2SIgor Mammedov }
2623115debf2SIgor Mammedov 
2624115debf2SIgor Mammedov static
2625115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
2626ff9006ddSIgor Mammedov                                Error **errp)
2627ff9006ddSIgor Mammedov {
2628535455fdSIgor Mammedov     int index;
2629535455fdSIgor Mammedov     sPAPRDRConnector *drc;
2630ff9006ddSIgor Mammedov     sPAPRDRConnectorClass *drck;
2631ff9006ddSIgor Mammedov     Error *local_err = NULL;
2632535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2633535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
2634ff9006ddSIgor Mammedov 
2635535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
2636535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2637535455fdSIgor Mammedov                    cc->core_id);
2638535455fdSIgor Mammedov         return;
2639535455fdSIgor Mammedov     }
2640ff9006ddSIgor Mammedov     if (index == 0) {
2641ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
2642ff9006ddSIgor Mammedov         return;
2643ff9006ddSIgor Mammedov     }
2644ff9006ddSIgor Mammedov 
2645535455fdSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2646ff9006ddSIgor Mammedov     g_assert(drc);
2647ff9006ddSIgor Mammedov 
2648ff9006ddSIgor Mammedov     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2649ff9006ddSIgor Mammedov     drck->detach(drc, dev, spapr_core_release, NULL, &local_err);
2650ff9006ddSIgor Mammedov     if (local_err) {
2651ff9006ddSIgor Mammedov         error_propagate(errp, local_err);
2652ff9006ddSIgor Mammedov         return;
2653ff9006ddSIgor Mammedov     }
2654ff9006ddSIgor Mammedov 
2655ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
2656ff9006ddSIgor Mammedov }
2657ff9006ddSIgor Mammedov 
2658ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2659ff9006ddSIgor Mammedov                             Error **errp)
2660ff9006ddSIgor Mammedov {
2661ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
2662ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
2663ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
2664ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2665ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
2666ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
2667ff9006ddSIgor Mammedov     Error *local_err = NULL;
2668ff9006ddSIgor Mammedov     void *fdt = NULL;
2669ff9006ddSIgor Mammedov     int fdt_offset = 0;
2670ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
2671535455fdSIgor Mammedov     CPUArchId *core_slot;
2672535455fdSIgor Mammedov     int index;
2673ff9006ddSIgor Mammedov 
2674535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2675535455fdSIgor Mammedov     if (!core_slot) {
2676535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2677535455fdSIgor Mammedov                    cc->core_id);
2678535455fdSIgor Mammedov         return;
2679535455fdSIgor Mammedov     }
2680ff9006ddSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2681ff9006ddSIgor Mammedov 
2682c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
2683ff9006ddSIgor Mammedov 
2684ff9006ddSIgor Mammedov     /*
2685ff9006ddSIgor Mammedov      * Setup CPU DT entries only for hotplugged CPUs. For boot time or
2686ff9006ddSIgor Mammedov      * coldplugged CPUs DT entries are setup in spapr_build_fdt().
2687ff9006ddSIgor Mammedov      */
2688ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2689ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
2690ff9006ddSIgor Mammedov     }
2691ff9006ddSIgor Mammedov 
2692ff9006ddSIgor Mammedov     if (drc) {
2693ff9006ddSIgor Mammedov         sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2694ff9006ddSIgor Mammedov         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, &local_err);
2695ff9006ddSIgor Mammedov         if (local_err) {
2696ff9006ddSIgor Mammedov             g_free(fdt);
2697ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
2698ff9006ddSIgor Mammedov             return;
2699ff9006ddSIgor Mammedov         }
2700ff9006ddSIgor Mammedov     }
2701ff9006ddSIgor Mammedov 
2702ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2703ff9006ddSIgor Mammedov         /*
2704ff9006ddSIgor Mammedov          * Send hotplug notification interrupt to the guest only in case
2705ff9006ddSIgor Mammedov          * of hotplugged CPUs.
2706ff9006ddSIgor Mammedov          */
2707ff9006ddSIgor Mammedov         spapr_hotplug_req_add_by_index(drc);
2708ff9006ddSIgor Mammedov     } else {
2709ff9006ddSIgor Mammedov         /*
2710ff9006ddSIgor Mammedov          * Set the right DRC states for cold plugged CPU.
2711ff9006ddSIgor Mammedov          */
2712ff9006ddSIgor Mammedov         if (drc) {
2713ff9006ddSIgor Mammedov             sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2714ff9006ddSIgor Mammedov             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
2715ff9006ddSIgor Mammedov             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
2716ff9006ddSIgor Mammedov         }
2717ff9006ddSIgor Mammedov     }
2718535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
2719ff9006ddSIgor Mammedov }
2720ff9006ddSIgor Mammedov 
2721ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2722ff9006ddSIgor Mammedov                                 Error **errp)
2723ff9006ddSIgor Mammedov {
2724ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
2725ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
2726ff9006ddSIgor Mammedov     Error *local_err = NULL;
2727ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2728ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
2729ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
2730535455fdSIgor Mammedov     CPUArchId *core_slot;
2731535455fdSIgor Mammedov     int index;
2732ff9006ddSIgor Mammedov 
2733c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
2734ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
2735ff9006ddSIgor Mammedov         goto out;
2736ff9006ddSIgor Mammedov     }
2737ff9006ddSIgor Mammedov 
2738ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
2739ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
2740ff9006ddSIgor Mammedov         goto out;
2741ff9006ddSIgor Mammedov     }
2742ff9006ddSIgor Mammedov 
2743ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
2744ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
2745ff9006ddSIgor Mammedov         goto out;
2746ff9006ddSIgor Mammedov     }
2747ff9006ddSIgor Mammedov 
2748535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2749535455fdSIgor Mammedov     if (!core_slot) {
2750ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
2751ff9006ddSIgor Mammedov         goto out;
2752ff9006ddSIgor Mammedov     }
2753ff9006ddSIgor Mammedov 
2754535455fdSIgor Mammedov     if (core_slot->cpu) {
2755ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
2756ff9006ddSIgor Mammedov         goto out;
2757ff9006ddSIgor Mammedov     }
2758ff9006ddSIgor Mammedov 
2759ff9006ddSIgor Mammedov out:
2760ff9006ddSIgor Mammedov     g_free(base_core_type);
2761ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
2762ff9006ddSIgor Mammedov }
2763ff9006ddSIgor Mammedov 
2764c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2765c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2766c20d332aSBharata B Rao {
2767c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2768c20d332aSBharata B Rao 
2769c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2770b556854bSBharata B Rao         int node;
2771c20d332aSBharata B Rao 
2772c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2773c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2774c20d332aSBharata B Rao             return;
2775c20d332aSBharata B Rao         }
2776c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2777c20d332aSBharata B Rao         if (*errp) {
2778c20d332aSBharata B Rao             return;
2779c20d332aSBharata B Rao         }
27801a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
27811a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
27821a5512bbSGonglei             return;
27831a5512bbSGonglei         }
2784c20d332aSBharata B Rao 
2785b556854bSBharata B Rao         /*
2786b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2787b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2788b556854bSBharata B Rao          * to the first node that has some memory. This causes two
2789b556854bSBharata B Rao          * unexpected behaviours for the user.
2790b556854bSBharata B Rao          *
2791b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
2792b556854bSBharata B Rao          *   specified.
2793b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2794b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
2795b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
2796b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
2797b556854bSBharata B Rao          *
2798b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
2799b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
2800b556854bSBharata B Rao          */
2801b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
2802b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
2803b556854bSBharata B Rao                        node);
2804b556854bSBharata B Rao             return;
2805b556854bSBharata B Rao         }
2806b556854bSBharata B Rao 
2807c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
2808af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2809af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
2810c20d332aSBharata B Rao     }
2811c20d332aSBharata B Rao }
2812c20d332aSBharata B Rao 
2813c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2814c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2815c20d332aSBharata B Rao {
2816cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
28173c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
28186f4b5c3eSBharata B Rao 
2819c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2820cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2821cf632463SBharata B Rao             spapr_memory_unplug(hotplug_dev, dev, errp);
2822cf632463SBharata B Rao         } else {
2823cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2824cf632463SBharata B Rao         }
2825cf632463SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2826c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
2827cf632463SBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
2828cf632463SBharata B Rao             return;
2829cf632463SBharata B Rao         }
2830cf632463SBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
2831cf632463SBharata B Rao     }
2832cf632463SBharata B Rao }
2833cf632463SBharata B Rao 
2834cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
2835cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
2836cf632463SBharata B Rao {
2837cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
2838cf632463SBharata B Rao     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
2839cf632463SBharata B Rao 
2840cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2841cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2842cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
2843cf632463SBharata B Rao         } else {
2844cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
2845cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
2846cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
2847cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
2848cf632463SBharata B Rao              * eventually handled by the guest after boot
2849cf632463SBharata B Rao              */
2850cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2851cf632463SBharata B Rao         }
28526f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2853c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
28546f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
28556f4b5c3eSBharata B Rao             return;
28566f4b5c3eSBharata B Rao         }
2857115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
2858c20d332aSBharata B Rao     }
2859c20d332aSBharata B Rao }
2860c20d332aSBharata B Rao 
286194a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
286294a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
286394a94e4cSBharata B Rao {
286494a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
286594a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
286694a94e4cSBharata B Rao     }
286794a94e4cSBharata B Rao }
286894a94e4cSBharata B Rao 
28697ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
2870c20d332aSBharata B Rao                                                  DeviceState *dev)
2871c20d332aSBharata B Rao {
287294a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
287394a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2874c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
2875c20d332aSBharata B Rao     }
2876c20d332aSBharata B Rao     return NULL;
2877c20d332aSBharata B Rao }
2878c20d332aSBharata B Rao 
287920bb648dSDavid Gibson static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
288020bb648dSDavid Gibson {
288120bb648dSDavid Gibson     /* Allocate to NUMA nodes on a "socket" basis (not that concept of
288220bb648dSDavid Gibson      * socket means much for the paravirtualized PAPR platform) */
288320bb648dSDavid Gibson     return cpu_index / smp_threads / smp_cores;
288420bb648dSDavid Gibson }
288520bb648dSDavid Gibson 
2886535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
2887535455fdSIgor Mammedov {
2888535455fdSIgor Mammedov     int i;
2889535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
2890535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
2891535455fdSIgor Mammedov 
2892c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
2893535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
2894535455fdSIgor Mammedov     }
2895535455fdSIgor Mammedov     if (machine->possible_cpus) {
2896535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
2897535455fdSIgor Mammedov         return machine->possible_cpus;
2898535455fdSIgor Mammedov     }
2899535455fdSIgor Mammedov 
2900535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2901535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
2902535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
2903535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
2904535455fdSIgor Mammedov         int core_id = i * smp_threads;
2905535455fdSIgor Mammedov 
2906f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
2907535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
2908535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
2909535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
2910535455fdSIgor Mammedov         /* TODO: add 'has_node/node' here to describe
2911535455fdSIgor Mammedov            to which node core belongs */
2912535455fdSIgor Mammedov     }
2913535455fdSIgor Mammedov     return machine->possible_cpus;
2914535455fdSIgor Mammedov }
2915535455fdSIgor Mammedov 
29166737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
2917daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
2918daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
29196737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
29206737d9adSDavid Gibson {
2921357d1e3bSDavid Gibson     /*
2922357d1e3bSDavid Gibson      * New-style PHB window placement.
2923357d1e3bSDavid Gibson      *
2924357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
2925357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
2926357d1e3bSDavid Gibson      * windows.
2927357d1e3bSDavid Gibson      *
2928357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
2929357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
2930357d1e3bSDavid Gibson      *
2931357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
2932357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
2933357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
2934357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
2935357d1e3bSDavid Gibson      */
29366737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
293725e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
293825e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
29396737d9adSDavid Gibson     int i;
29406737d9adSDavid Gibson 
2941357d1e3bSDavid Gibson     /* Sanity check natural alignments */
2942357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2943357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2944357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
2945357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
2946357d1e3bSDavid Gibson     /* Sanity check bounds */
294725e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
294825e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
294925e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
295025e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
29512efff1c0SDavid Gibson 
295225e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
295325e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
295425e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
29556737d9adSDavid Gibson         return;
29566737d9adSDavid Gibson     }
29576737d9adSDavid Gibson 
29586737d9adSDavid Gibson     *buid = base_buid + index;
29596737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
29606737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
29616737d9adSDavid Gibson     }
29626737d9adSDavid Gibson 
2963357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
2964357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
2965357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
29666737d9adSDavid Gibson }
29676737d9adSDavid Gibson 
296829ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
296953018216SPaolo Bonzini {
297029ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
2971224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
297271461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
297334316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
2974c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
29751d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
297629ee3247SAlexey Kardashevskiy 
29770eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
2978fc9f38c3SDavid Gibson 
2979fc9f38c3SDavid Gibson     /*
2980fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
2981fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
2982fc9f38c3SDavid Gibson      * these details for backwards compatibility
2983fc9f38c3SDavid Gibson      */
2984958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
2985958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
2986958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
29876244bb7eSGreg Kurz     mc->max_cpus = 1024;
2988958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
29895b2128d2SAlexander Graf     mc->default_boot_order = "";
2990a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
2991958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
29929e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
2993e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
29947ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
299594a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
2996c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
2997c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
299820bb648dSDavid Gibson     mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
2999535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3000cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
300100b4fbe2SMarcel Apfelbaum 
3002fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
30033daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
3004c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
300571461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
300634316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
30076737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
30081d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3009e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3010e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3011e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3012e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
301353018216SPaolo Bonzini }
301453018216SPaolo Bonzini 
301529ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
301629ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
301729ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
30184aee7362SDavid Gibson     .abstract      = true,
30196ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
302023825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
302187bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3022183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
302329ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
302471461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
302571461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
302634316482SAlexey Kardashevskiy         { TYPE_NMI },
3027c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
30281d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
302971461b0fSAlexey Kardashevskiy         { }
303071461b0fSAlexey Kardashevskiy     },
303129ee3247SAlexey Kardashevskiy };
303229ee3247SAlexey Kardashevskiy 
3033fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
30345013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
30355013c547SDavid Gibson                                                     void *data)      \
30365013c547SDavid Gibson     {                                                                \
30375013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
30385013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3039fccbc785SDavid Gibson         if (latest) {                                                \
3040fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3041fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3042fccbc785SDavid Gibson         }                                                            \
30435013c547SDavid Gibson     }                                                                \
30445013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
30455013c547SDavid Gibson     {                                                                \
30465013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
30475013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
30485013c547SDavid Gibson     }                                                                \
30495013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
30505013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
30515013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
30525013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
30535013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
30545013c547SDavid Gibson     };                                                               \
30555013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
30565013c547SDavid Gibson     {                                                                \
30575013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
30585013c547SDavid Gibson     }                                                                \
30590e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
30605013c547SDavid Gibson 
30611c5f29bbSDavid Gibson /*
3062fa325e6cSDavid Gibson  * pseries-2.9
3063db800b21SDavid Gibson  */
3064fa325e6cSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
3065db800b21SDavid Gibson {
3066db800b21SDavid Gibson }
3067db800b21SDavid Gibson 
3068fa325e6cSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
3069db800b21SDavid Gibson {
3070db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
3071db800b21SDavid Gibson }
3072db800b21SDavid Gibson 
3073fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", true);
3074fa325e6cSDavid Gibson 
3075fa325e6cSDavid Gibson /*
3076fa325e6cSDavid Gibson  * pseries-2.8
3077fa325e6cSDavid Gibson  */
3078fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                            \
3079fa325e6cSDavid Gibson     HW_COMPAT_2_8
3080fa325e6cSDavid Gibson 
3081fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
3082fa325e6cSDavid Gibson {
3083fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
3084fa325e6cSDavid Gibson }
3085fa325e6cSDavid Gibson 
3086fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
3087fa325e6cSDavid Gibson {
3088fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
3089fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
3090fa325e6cSDavid Gibson }
3091fa325e6cSDavid Gibson 
3092fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
3093db800b21SDavid Gibson 
3094db800b21SDavid Gibson /*
30951ea1eefcSBharata B Rao  * pseries-2.7
30961ea1eefcSBharata B Rao  */
3097db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3098db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3099357d1e3bSDavid Gibson     {                                               \
3100357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3101357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3102357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3103357d1e3bSDavid Gibson     },                                              \
3104357d1e3bSDavid Gibson     {                                               \
3105357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3106357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3107357d1e3bSDavid Gibson         .value    = "0",                            \
3108146c11f1SDavid Gibson     },                                              \
3109146c11f1SDavid Gibson     {                                               \
3110146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3111146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3112146c11f1SDavid Gibson         .value    = "on",                           \
31135c4537bdSDavid Gibson     },                                              \
31145c4537bdSDavid Gibson     {                                               \
31155c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
31165c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
31175c4537bdSDavid Gibson         .value    = "on",                           \
3118357d1e3bSDavid Gibson     },
3119357d1e3bSDavid Gibson 
3120357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3121357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3122357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3123357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3124357d1e3bSDavid Gibson {
3125357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3126357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3127357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3128357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3129357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3130357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3131357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3132357d1e3bSDavid Gibson 
3133357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3134357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3135357d1e3bSDavid Gibson     int i;
3136357d1e3bSDavid Gibson 
3137357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3138357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3139357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3140357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3141357d1e3bSDavid Gibson          * regions */
3142357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3143357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3144357d1e3bSDavid Gibson     }
3145357d1e3bSDavid Gibson 
3146357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3147357d1e3bSDavid Gibson 
3148357d1e3bSDavid Gibson     if (index > max_index) {
3149357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3150357d1e3bSDavid Gibson                    max_index);
3151357d1e3bSDavid Gibson         return;
3152357d1e3bSDavid Gibson     }
3153357d1e3bSDavid Gibson 
3154357d1e3bSDavid Gibson     *buid = base_buid + index;
3155357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3156357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3157357d1e3bSDavid Gibson     }
3158357d1e3bSDavid Gibson 
3159357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3160357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3161357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3162357d1e3bSDavid Gibson     /*
3163357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3164357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3165357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3166357d1e3bSDavid Gibson      */
3167357d1e3bSDavid Gibson }
3168db800b21SDavid Gibson 
31691ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
31701ea1eefcSBharata B Rao {
3171f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3172f6229214SMichael Roth 
3173672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3174f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
31751ea1eefcSBharata B Rao }
31761ea1eefcSBharata B Rao 
31771ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
31781ea1eefcSBharata B Rao {
31793daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
31803daa4a9fSThomas Huth 
3181db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
31823daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3183db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3184357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
31851ea1eefcSBharata B Rao }
31861ea1eefcSBharata B Rao 
3187db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
31881ea1eefcSBharata B Rao 
31891ea1eefcSBharata B Rao /*
31904b23699cSDavid Gibson  * pseries-2.6
31914b23699cSDavid Gibson  */
31921ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3193ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3194ae4de14cSAlexey Kardashevskiy     { \
3195ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3196ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3197ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3198ae4de14cSAlexey Kardashevskiy     },
31991ea1eefcSBharata B Rao 
32004b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
32014b23699cSDavid Gibson {
3202672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
32034b23699cSDavid Gibson }
32044b23699cSDavid Gibson 
32054b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
32064b23699cSDavid Gibson {
32071ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3208c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
32091ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
32104b23699cSDavid Gibson }
32114b23699cSDavid Gibson 
32121ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
32134b23699cSDavid Gibson 
32144b23699cSDavid Gibson /*
32151c5f29bbSDavid Gibson  * pseries-2.5
32161c5f29bbSDavid Gibson  */
32174b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
321857c522f4SThomas Huth     HW_COMPAT_2_5 \
321957c522f4SThomas Huth     { \
322057c522f4SThomas Huth         .driver   = "spapr-vlan", \
322157c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
322257c522f4SThomas Huth         .value    = "off", \
322357c522f4SThomas Huth     },
32244b23699cSDavid Gibson 
32255013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
32261c5f29bbSDavid Gibson {
3227672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
32285013c547SDavid Gibson }
32295013c547SDavid Gibson 
32305013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
32315013c547SDavid Gibson {
323257040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
323357040d45SThomas Huth 
32344b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
323557040d45SThomas Huth     smc->use_ohci_by_default = true;
32364b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
32371c5f29bbSDavid Gibson }
32381c5f29bbSDavid Gibson 
32394b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
32401c5f29bbSDavid Gibson 
32411c5f29bbSDavid Gibson /*
32421c5f29bbSDavid Gibson  * pseries-2.4
32431c5f29bbSDavid Gibson  */
324480fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
324580fd50f9SCornelia Huck         HW_COMPAT_2_4
324680fd50f9SCornelia Huck 
32475013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
32481c5f29bbSDavid Gibson {
32495013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
32505013c547SDavid Gibson }
32511c5f29bbSDavid Gibson 
32525013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
32535013c547SDavid Gibson {
3254fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3255fc9f38c3SDavid Gibson 
3256fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3257fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3258f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
32591c5f29bbSDavid Gibson }
32601c5f29bbSDavid Gibson 
3261fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
32621c5f29bbSDavid Gibson 
32631c5f29bbSDavid Gibson /*
32641c5f29bbSDavid Gibson  * pseries-2.3
32651c5f29bbSDavid Gibson  */
326638ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
32677619c7b0SMichael Roth         HW_COMPAT_2_3 \
32687619c7b0SMichael Roth         {\
32697619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
32707619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
32717619c7b0SMichael Roth             .value    = "off",\
32727619c7b0SMichael Roth         },
327338ff32c6SEduardo Habkost 
32745013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
32751c5f29bbSDavid Gibson {
32765013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
32771c5f29bbSDavid Gibson     savevm_skip_section_footers();
32781c5f29bbSDavid Gibson     global_state_set_optional();
327909b5e30dSGreg Kurz     savevm_skip_configuration();
32801c5f29bbSDavid Gibson }
32811c5f29bbSDavid Gibson 
32825013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
32831c5f29bbSDavid Gibson {
3284fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3285f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
32861c5f29bbSDavid Gibson }
3287fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
32881c5f29bbSDavid Gibson 
32891c5f29bbSDavid Gibson /*
32901c5f29bbSDavid Gibson  * pseries-2.2
32911c5f29bbSDavid Gibson  */
32921c5f29bbSDavid Gibson 
3293b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
32944dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3295b194df47SAlexey Kardashevskiy         {\
3296b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3297b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3298b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3299dd754bafSEduardo Habkost         },
3300b194df47SAlexey Kardashevskiy 
33015013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3302b0e966d0SJason Wang {
33035013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3304cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3305b0e966d0SJason Wang }
3306b0e966d0SJason Wang 
33075013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3308b0e966d0SJason Wang {
3309fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3310f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
33111c5f29bbSDavid Gibson }
3312fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
33131c5f29bbSDavid Gibson 
33141c5f29bbSDavid Gibson /*
33151c5f29bbSDavid Gibson  * pseries-2.1
33161c5f29bbSDavid Gibson  */
33171c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
33181c5f29bbSDavid Gibson         HW_COMPAT_2_1
33191c5f29bbSDavid Gibson 
33205013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
33211c5f29bbSDavid Gibson {
33225013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
33231c5f29bbSDavid Gibson }
33241c5f29bbSDavid Gibson 
33255013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
3326b0e966d0SJason Wang {
3327fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
3328f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
33296026db45SAlexey Kardashevskiy }
3330fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
33316026db45SAlexey Kardashevskiy 
333229ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
333329ee3247SAlexey Kardashevskiy {
333429ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
333529ee3247SAlexey Kardashevskiy }
333629ee3247SAlexey Kardashevskiy 
333729ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
3338