xref: /openbmc/qemu/hw/ppc/spapr.c (revision ec975e83)
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"
666449da45SCédric Le Goater #include "hw/intc/intc.h"
6753018216SPaolo Bonzini 
6868a27b20SMichael S. Tsirkin #include "hw/compat.h"
69f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7094a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
712474bfd4SIgor Mammedov #include "qmp-commands.h"
7268a27b20SMichael S. Tsirkin 
7353018216SPaolo Bonzini #include <libfdt.h>
7453018216SPaolo Bonzini 
7553018216SPaolo Bonzini /* SLOF memory layout:
7653018216SPaolo Bonzini  *
7753018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
7853018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
7953018216SPaolo Bonzini  *
8053018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8153018216SPaolo Bonzini  * and more
8253018216SPaolo Bonzini  *
8353018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8453018216SPaolo Bonzini  */
8538b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
8653018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
87b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
8853018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
8953018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9053018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9153018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9253018216SPaolo Bonzini 
9353018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9453018216SPaolo Bonzini 
9553018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9653018216SPaolo Bonzini 
9753018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9853018216SPaolo Bonzini 
99e6f7e110SCédric Le Goater static int try_create_xics(sPAPRMachineState *spapr, const char *type_ics,
100817bb6a4SCédric Le Goater                            const char *type_icp, int nr_servers,
101817bb6a4SCédric Le Goater                            int nr_irqs, Error **errp)
102c04d6cfaSAnthony Liguori {
1032cd908d0SCédric Le Goater     XICSFabric *xi = XICS_FABRIC(spapr);
1044e4169f7SCédric Le Goater     Error *err = NULL, *local_err = NULL;
1054e4169f7SCédric Le Goater     ICSState *ics = NULL;
106817bb6a4SCédric Le Goater     int i;
107c04d6cfaSAnthony Liguori 
1084e4169f7SCédric Le Goater     ics = ICS_SIMPLE(object_new(type_ics));
109c79b2fddSCédric Le Goater     qdev_set_parent_bus(DEVICE(ics), sysbus_get_default());
110681bfadeSCédric Le Goater     object_property_add_child(OBJECT(spapr), "ics", OBJECT(ics), NULL);
1114e4169f7SCédric Le Goater     object_property_set_int(OBJECT(ics), nr_irqs, "nr-irqs", &err);
112b4f27d71SCédric Le Goater     object_property_add_const_link(OBJECT(ics), "xics", OBJECT(xi), NULL);
1134e4169f7SCédric Le Goater     object_property_set_bool(OBJECT(ics), true, "realized", &local_err);
1144e4169f7SCédric Le Goater     error_propagate(&err, local_err);
1154e4169f7SCédric Le Goater     if (err) {
1164e4169f7SCédric Le Goater         goto error;
1174e4169f7SCédric Le Goater     }
1184e4169f7SCédric Le Goater 
119852ad27eSCédric Le Goater     spapr->icps = g_malloc0(nr_servers * sizeof(ICPState));
120852ad27eSCédric Le Goater     spapr->nr_servers = nr_servers;
121817bb6a4SCédric Le Goater 
122817bb6a4SCédric Le Goater     for (i = 0; i < nr_servers; i++) {
123852ad27eSCédric Le Goater         ICPState *icp = &spapr->icps[i];
124817bb6a4SCédric Le Goater 
125817bb6a4SCédric Le Goater         object_initialize(icp, sizeof(*icp), type_icp);
12620147f2fSCédric Le Goater         qdev_set_parent_bus(DEVICE(icp), sysbus_get_default());
127852ad27eSCédric Le Goater         object_property_add_child(OBJECT(spapr), "icp[*]", OBJECT(icp), NULL);
1282cd908d0SCédric Le Goater         object_property_add_const_link(OBJECT(icp), "xics", OBJECT(xi), NULL);
129817bb6a4SCédric Le Goater         object_property_set_bool(OBJECT(icp), true, "realized", &err);
130817bb6a4SCédric Le Goater         if (err) {
131817bb6a4SCédric Le Goater             goto error;
132817bb6a4SCédric Le Goater         }
133817bb6a4SCédric Le Goater         object_unref(OBJECT(icp));
134817bb6a4SCédric Le Goater     }
135817bb6a4SCédric Le Goater 
136681bfadeSCédric Le Goater     spapr->ics = ics;
137e6f7e110SCédric Le Goater     return 0;
1384e4169f7SCédric Le Goater 
1394e4169f7SCédric Le Goater error:
1404e4169f7SCédric Le Goater     error_propagate(errp, err);
1414e4169f7SCédric Le Goater     if (ics) {
1424e4169f7SCédric Le Goater         object_unparent(OBJECT(ics));
1434e4169f7SCédric Le Goater     }
144e6f7e110SCédric Le Goater     return -1;
145c04d6cfaSAnthony Liguori }
146c04d6cfaSAnthony Liguori 
147e6f7e110SCédric Le Goater static int xics_system_init(MachineState *machine,
1481e49182dSDavid Gibson                             int nr_servers, int nr_irqs, Error **errp)
149c04d6cfaSAnthony Liguori {
150e6f7e110SCédric Le Goater     int rc = -1;
151c04d6cfaSAnthony Liguori 
15211ad93f6SDavid Gibson     if (kvm_enabled()) {
15334f2af3dSMarkus Armbruster         Error *err = NULL;
15434f2af3dSMarkus Armbruster 
1552192a930SCédric Le Goater         if (machine_kernel_irqchip_allowed(machine) &&
1562192a930SCédric Le Goater             !xics_kvm_init(SPAPR_MACHINE(machine), errp)) {
157e6f7e110SCédric Le Goater             rc = try_create_xics(SPAPR_MACHINE(machine), TYPE_ICS_KVM,
158817bb6a4SCédric Le Goater                                  TYPE_KVM_ICP, nr_servers, nr_irqs, &err);
15911ad93f6SDavid Gibson         }
160e6f7e110SCédric Le Goater         if (machine_kernel_irqchip_required(machine) && rc < 0) {
161b83baa60SMarkus Armbruster             error_reportf_err(err,
162b83baa60SMarkus Armbruster                               "kernel_irqchip requested but unavailable: ");
163b83baa60SMarkus Armbruster         } else {
164903a41d3SStefano Dong (董兴水)             error_free(err);
16511ad93f6SDavid Gibson         }
166b83baa60SMarkus Armbruster     }
16711ad93f6SDavid Gibson 
168e6f7e110SCédric Le Goater     if (rc < 0) {
1692192a930SCédric Le Goater         xics_spapr_init(SPAPR_MACHINE(machine), errp);
170e6f7e110SCédric Le Goater         rc = try_create_xics(SPAPR_MACHINE(machine), TYPE_ICS_SIMPLE,
171681bfadeSCédric Le Goater                                TYPE_ICP, nr_servers, nr_irqs, errp);
172c04d6cfaSAnthony Liguori     }
173c04d6cfaSAnthony Liguori 
174e6f7e110SCédric Le Goater     return rc;
175c04d6cfaSAnthony Liguori }
176c04d6cfaSAnthony Liguori 
177833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
178833d4668SAlexey Kardashevskiy                                   int smt_threads)
179833d4668SAlexey Kardashevskiy {
180833d4668SAlexey Kardashevskiy     int i, ret = 0;
181833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
182833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
183833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
184833d4668SAlexey Kardashevskiy 
185d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
186d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
1876d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1886d9412eaSAlexey Kardashevskiy             return ret;
1896d9412eaSAlexey Kardashevskiy         }
1906d9412eaSAlexey Kardashevskiy     }
1916d9412eaSAlexey Kardashevskiy 
192833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
193833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
194833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
195833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
196833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
197833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
198833d4668SAlexey Kardashevskiy     }
199833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
200833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
201833d4668SAlexey Kardashevskiy     if (ret < 0) {
202833d4668SAlexey Kardashevskiy         return ret;
203833d4668SAlexey Kardashevskiy     }
204833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
205833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
206833d4668SAlexey Kardashevskiy 
207833d4668SAlexey Kardashevskiy     return ret;
208833d4668SAlexey Kardashevskiy }
209833d4668SAlexey Kardashevskiy 
2100da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
2110da6f3feSBharata B Rao {
2120da6f3feSBharata B Rao     int ret = 0;
2130da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2140da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
2150da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
2160da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2170da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2180da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2190da6f3feSBharata B Rao                                 cpu_to_be32(cs->numa_node),
2200da6f3feSBharata B Rao                                 cpu_to_be32(index)};
2210da6f3feSBharata B Rao 
2220da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
2230da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
2240da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
2250da6f3feSBharata B Rao                           sizeof(associativity));
2260da6f3feSBharata B Rao     }
2270da6f3feSBharata B Rao 
2280da6f3feSBharata B Rao     return ret;
2290da6f3feSBharata B Rao }
2300da6f3feSBharata B Rao 
23128e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
23253018216SPaolo Bonzini {
23382677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
23482677ed2SAlexey Kardashevskiy     CPUState *cs;
23553018216SPaolo Bonzini     char cpu_model[32];
23653018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
23753018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
23853018216SPaolo Bonzini 
23982677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
24082677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
24182677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
24282677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
24312dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
24453018216SPaolo Bonzini 
2450f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
24653018216SPaolo Bonzini             continue;
24753018216SPaolo Bonzini         }
24853018216SPaolo Bonzini 
24982677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
25053018216SPaolo Bonzini 
25182677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
25282677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
25382677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
25482677ed2SAlexey Kardashevskiy                                           "cpus");
25582677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
25682677ed2SAlexey Kardashevskiy                 return cpus_offset;
25782677ed2SAlexey Kardashevskiy             }
25882677ed2SAlexey Kardashevskiy         }
25982677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
26082677ed2SAlexey Kardashevskiy         if (offset < 0) {
26182677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
26253018216SPaolo Bonzini             if (offset < 0) {
26353018216SPaolo Bonzini                 return offset;
26453018216SPaolo Bonzini             }
26582677ed2SAlexey Kardashevskiy         }
26653018216SPaolo Bonzini 
2670da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
2680da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
26953018216SPaolo Bonzini         if (ret < 0) {
27053018216SPaolo Bonzini             return ret;
27153018216SPaolo Bonzini         }
27253018216SPaolo Bonzini 
2730da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
27453018216SPaolo Bonzini         if (ret < 0) {
27553018216SPaolo Bonzini             return ret;
27653018216SPaolo Bonzini         }
277833d4668SAlexey Kardashevskiy 
27812dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
279833d4668SAlexey Kardashevskiy         if (ret < 0) {
280833d4668SAlexey Kardashevskiy             return ret;
281833d4668SAlexey Kardashevskiy         }
28253018216SPaolo Bonzini     }
28353018216SPaolo Bonzini     return ret;
28453018216SPaolo Bonzini }
28553018216SPaolo Bonzini 
286b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
287b082d65aSAlexey Kardashevskiy {
288fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
289fb164994SDavid Gibson 
290b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
291b082d65aSAlexey Kardashevskiy         int i;
292b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
293b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
294fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
295fb164994SDavid Gibson                            machine->ram_size);
296b082d65aSAlexey Kardashevskiy             }
297b082d65aSAlexey Kardashevskiy         }
298b082d65aSAlexey Kardashevskiy     }
299fb164994SDavid Gibson     return machine->ram_size;
300b082d65aSAlexey Kardashevskiy }
301b082d65aSAlexey Kardashevskiy 
302a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
303a1d59c0fSAlexey Kardashevskiy {
304a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
305a1d59c0fSAlexey Kardashevskiy }
30653018216SPaolo Bonzini 
30703d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
30826a8c353SAlexey Kardashevskiy                                        hwaddr size)
30926a8c353SAlexey Kardashevskiy {
31026a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
31126a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
31226a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
313c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
31426a8c353SAlexey Kardashevskiy     };
31526a8c353SAlexey Kardashevskiy     char mem_name[32];
31626a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
31726a8c353SAlexey Kardashevskiy     int off;
31826a8c353SAlexey Kardashevskiy 
31926a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
32026a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
32126a8c353SAlexey Kardashevskiy 
32226a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
32326a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
32426a8c353SAlexey Kardashevskiy     _FDT(off);
32526a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
32626a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
32726a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
32826a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
32926a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
33003d196b7SBharata B Rao     return off;
33126a8c353SAlexey Kardashevskiy }
33226a8c353SAlexey Kardashevskiy 
33328e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
33453018216SPaolo Bonzini {
335fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
3367db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
3377db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
3387db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
3397db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
34053018216SPaolo Bonzini 
3417db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
3427db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
3437db8a127SAlexey Kardashevskiy         nb_nodes = 1;
344fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
3457db8a127SAlexey Kardashevskiy         nodes = &ramnode;
3465fe269b1SPaul Mackerras     }
34753018216SPaolo Bonzini 
3487db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
3497db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
3507db8a127SAlexey Kardashevskiy             continue;
35153018216SPaolo Bonzini         }
352fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
3535fe269b1SPaul Mackerras             node_size = 0;
3545fe269b1SPaul Mackerras         } else {
3557db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
356fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
357fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
3585fe269b1SPaul Mackerras             }
3595fe269b1SPaul Mackerras         }
3607db8a127SAlexey Kardashevskiy         if (!mem_start) {
3617db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
362e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
3637db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
3647db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
3657db8a127SAlexey Kardashevskiy         }
3666010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
3676010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
3686010818cSAlexey Kardashevskiy 
3696010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
3706010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
3716010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
3726010818cSAlexey Kardashevskiy             }
3736010818cSAlexey Kardashevskiy 
3746010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
3756010818cSAlexey Kardashevskiy             node_size -= sizetmp;
3766010818cSAlexey Kardashevskiy             mem_start += sizetmp;
3776010818cSAlexey Kardashevskiy         }
37853018216SPaolo Bonzini     }
37953018216SPaolo Bonzini 
38053018216SPaolo Bonzini     return 0;
38153018216SPaolo Bonzini }
38253018216SPaolo Bonzini 
383230bf719SThomas Huth /* Populate the "ibm,pa-features" property */
384230bf719SThomas Huth static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset)
385230bf719SThomas Huth {
386230bf719SThomas Huth     uint8_t pa_features_206[] = { 6, 0,
387230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
388230bf719SThomas Huth     uint8_t pa_features_207[] = { 24, 0,
389230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
390230bf719SThomas Huth         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
391230bf719SThomas Huth         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
392bac3bf28SThomas Huth         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
3934975c098SSuraj Jitindar Singh     /* Currently we don't advertise any of the "new" ISAv3.00 functionality */
3944975c098SSuraj Jitindar Singh     uint8_t pa_features_300[] = { 64, 0,
3954975c098SSuraj Jitindar Singh         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /*  0 -  5 */
3964975c098SSuraj Jitindar Singh         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /*  6 - 11 */
3974975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
3984975c098SSuraj Jitindar Singh         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
3994975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 24 - 29 */
4004975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 - 35 */
4014975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 36 - 41 */
4024975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 42 - 47 */
4034975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 48 - 53 */
4044975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 54 - 59 */
4054975c098SSuraj Jitindar Singh         0x00, 0x00, 0x00, 0x00           }; /* 60 - 63 */
4064975c098SSuraj Jitindar Singh 
407230bf719SThomas Huth     uint8_t *pa_features;
408230bf719SThomas Huth     size_t pa_size;
409230bf719SThomas Huth 
410*ec975e83SSam Bobroff     switch (POWERPC_MMU_VER(env->mmu_model)) {
411*ec975e83SSam Bobroff     case POWERPC_MMU_VER_2_06:
412230bf719SThomas Huth         pa_features = pa_features_206;
413230bf719SThomas Huth         pa_size = sizeof(pa_features_206);
4144cbec30dSThomas Huth         break;
415*ec975e83SSam Bobroff     case POWERPC_MMU_VER_2_07:
416230bf719SThomas Huth         pa_features = pa_features_207;
417230bf719SThomas Huth         pa_size = sizeof(pa_features_207);
4184cbec30dSThomas Huth         break;
419*ec975e83SSam Bobroff     case POWERPC_MMU_VER_3_00:
4204975c098SSuraj Jitindar Singh         pa_features = pa_features_300;
4214975c098SSuraj Jitindar Singh         pa_size = sizeof(pa_features_300);
4224975c098SSuraj Jitindar Singh         break;
4234cbec30dSThomas Huth     default:
4244cbec30dSThomas Huth         return;
425230bf719SThomas Huth     }
426230bf719SThomas Huth 
427230bf719SThomas Huth     if (env->ci_large_pages) {
428230bf719SThomas Huth         /*
429230bf719SThomas Huth          * Note: we keep CI large pages off by default because a 64K capable
430230bf719SThomas Huth          * guest provisioned with large pages might otherwise try to map a qemu
431230bf719SThomas Huth          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
432230bf719SThomas Huth          * even if that qemu runs on a 4k host.
433230bf719SThomas Huth          * We dd this bit back here if we are confident this is not an issue
434230bf719SThomas Huth          */
435230bf719SThomas Huth         pa_features[3] |= 0x20;
436230bf719SThomas Huth     }
437bac3bf28SThomas Huth     if (kvmppc_has_cap_htm() && pa_size > 24) {
438bac3bf28SThomas Huth         pa_features[24] |= 0x80;    /* Transactional memory support */
439bac3bf28SThomas Huth     }
440230bf719SThomas Huth 
441230bf719SThomas Huth     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
442230bf719SThomas Huth }
443230bf719SThomas Huth 
4440da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4450da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4460da6f3feSBharata B Rao {
4470da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4480da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4490da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4500da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
4510da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4520da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
453afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
454afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
4550da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
4560da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
4570da6f3feSBharata B Rao     size_t page_sizes_prop_size;
45822419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
4590da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
46012dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
461af81cf32SBharata B Rao     sPAPRDRConnector *drc;
462af81cf32SBharata B Rao     sPAPRDRConnectorClass *drck;
463af81cf32SBharata B Rao     int drc_index;
464af81cf32SBharata B Rao 
465af81cf32SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
466af81cf32SBharata B Rao     if (drc) {
467af81cf32SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
468af81cf32SBharata B Rao         drc_index = drck->get_index(drc);
469af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
470af81cf32SBharata B Rao     }
4710da6f3feSBharata B Rao 
4720da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
4730da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
4740da6f3feSBharata B Rao 
4750da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
4760da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
4770da6f3feSBharata B Rao                            env->dcache_line_size)));
4780da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
4790da6f3feSBharata B Rao                            env->dcache_line_size)));
4800da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
4810da6f3feSBharata B Rao                            env->icache_line_size)));
4820da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
4830da6f3feSBharata B Rao                            env->icache_line_size)));
4840da6f3feSBharata B Rao 
4850da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
4860da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
4870da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
4880da6f3feSBharata B Rao     } else {
489ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
4900da6f3feSBharata B Rao     }
4910da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
4920da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
4930da6f3feSBharata B Rao                                pcc->l1_icache_size)));
4940da6f3feSBharata B Rao     } else {
495ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
4960da6f3feSBharata B Rao     }
4970da6f3feSBharata B Rao 
4980da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
4990da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
500fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
5010da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
5020da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
5030da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
5040da6f3feSBharata B Rao 
5050da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
5060da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
5070da6f3feSBharata B Rao     }
5080da6f3feSBharata B Rao 
5090da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
5100da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
5110da6f3feSBharata B Rao                           segs, sizeof(segs))));
5120da6f3feSBharata B Rao     }
5130da6f3feSBharata B Rao 
5140da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
5150da6f3feSBharata B Rao      *   0 / no property == no vector extensions
5160da6f3feSBharata B Rao      *   1               == VMX / Altivec available
5170da6f3feSBharata B Rao      *   2               == VSX available */
5180da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
5190da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
5200da6f3feSBharata B Rao 
5210da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
5220da6f3feSBharata B Rao     }
5230da6f3feSBharata B Rao 
5240da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
5250da6f3feSBharata B Rao      *   0 / no property == no DFP
5260da6f3feSBharata B Rao      *   1               == DFP available */
5270da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
5280da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
5290da6f3feSBharata B Rao     }
5300da6f3feSBharata B Rao 
5313654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
5320da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
5330da6f3feSBharata B Rao     if (page_sizes_prop_size) {
5340da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
5350da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
5360da6f3feSBharata B Rao     }
5370da6f3feSBharata B Rao 
538230bf719SThomas Huth     spapr_populate_pa_features(env, fdt, offset);
53990da0d5aSBenjamin Herrenschmidt 
5400da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
54122419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
5420da6f3feSBharata B Rao 
5430da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
5440da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
5450da6f3feSBharata B Rao 
5460da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
5470da6f3feSBharata B Rao 
54812dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
5490da6f3feSBharata B Rao }
5500da6f3feSBharata B Rao 
5510da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
5520da6f3feSBharata B Rao {
5530da6f3feSBharata B Rao     CPUState *cs;
5540da6f3feSBharata B Rao     int cpus_offset;
5550da6f3feSBharata B Rao     char *nodename;
5560da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
5570da6f3feSBharata B Rao 
5580da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
5590da6f3feSBharata B Rao     _FDT(cpus_offset);
5600da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
5610da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
5620da6f3feSBharata B Rao 
5630da6f3feSBharata B Rao     /*
5640da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
5650da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
5660da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
5670da6f3feSBharata B Rao      */
5680da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
5690da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
5700da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
5710da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
5720da6f3feSBharata B Rao         int offset;
5730da6f3feSBharata B Rao 
5740da6f3feSBharata B Rao         if ((index % smt) != 0) {
5750da6f3feSBharata B Rao             continue;
5760da6f3feSBharata B Rao         }
5770da6f3feSBharata B Rao 
5780da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
5790da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
5800da6f3feSBharata B Rao         g_free(nodename);
5810da6f3feSBharata B Rao         _FDT(offset);
5820da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
5830da6f3feSBharata B Rao     }
5840da6f3feSBharata B Rao 
5850da6f3feSBharata B Rao }
5860da6f3feSBharata B Rao 
58703d196b7SBharata B Rao /*
58803d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
58903d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
59003d196b7SBharata B Rao  * of this device tree node.
59103d196b7SBharata B Rao  */
59203d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
59303d196b7SBharata B Rao {
59403d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
59503d196b7SBharata B Rao     int ret, i, offset;
59603d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
59703d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
598d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
599d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
600d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
601d0e5a8f2SBharata B Rao                        lmb_size;
60203d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
6036663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
60403d196b7SBharata B Rao 
605ef001f06SThomas Huth     /*
606d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
60716c25aefSBharata B Rao      */
608d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
60916c25aefSBharata B Rao         return 0;
61016c25aefSBharata B Rao     }
61116c25aefSBharata B Rao 
61216c25aefSBharata B Rao     /*
613ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
614ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
615ef001f06SThomas Huth      */
616ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
617ef001f06SThomas Huth               * sizeof(uint32_t);
61803d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
61903d196b7SBharata B Rao 
62003d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
62103d196b7SBharata B Rao 
62203d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
62303d196b7SBharata B Rao                     sizeof(prop_lmb_size));
62403d196b7SBharata B Rao     if (ret < 0) {
62503d196b7SBharata B Rao         goto out;
62603d196b7SBharata B Rao     }
62703d196b7SBharata B Rao 
62803d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
62903d196b7SBharata B Rao     if (ret < 0) {
63003d196b7SBharata B Rao         goto out;
63103d196b7SBharata B Rao     }
63203d196b7SBharata B Rao 
63303d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
63403d196b7SBharata B Rao     if (ret < 0) {
63503d196b7SBharata B Rao         goto out;
63603d196b7SBharata B Rao     }
63703d196b7SBharata B Rao 
63803d196b7SBharata B Rao     /* ibm,dynamic-memory */
63903d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
64003d196b7SBharata B Rao     cur_index++;
64103d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
642d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
64303d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
64403d196b7SBharata B Rao 
645d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
646d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
647d0e5a8f2SBharata B Rao             sPAPRDRConnectorClass *drck;
648d0e5a8f2SBharata B Rao 
649d0e5a8f2SBharata B Rao             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
65003d196b7SBharata B Rao             g_assert(drc);
65103d196b7SBharata B Rao             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
65203d196b7SBharata B Rao 
65303d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
65403d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
65503d196b7SBharata B Rao             dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
65603d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
65703d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
658d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
65903d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
66003d196b7SBharata B Rao             } else {
66103d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
66203d196b7SBharata B Rao             }
663d0e5a8f2SBharata B Rao         } else {
664d0e5a8f2SBharata B Rao             /*
665d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
666d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
667d0e5a8f2SBharata B Rao              * and as having no valid DRC.
668d0e5a8f2SBharata B Rao              */
669d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
670d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
671d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
672d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
673d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
674d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
675d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
676d0e5a8f2SBharata B Rao         }
67703d196b7SBharata B Rao 
67803d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
67903d196b7SBharata B Rao     }
68003d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
68103d196b7SBharata B Rao     if (ret < 0) {
68203d196b7SBharata B Rao         goto out;
68303d196b7SBharata B Rao     }
68403d196b7SBharata B Rao 
68503d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
68603d196b7SBharata B Rao     cur_index = int_buf;
6876663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
68803d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
68903d196b7SBharata B Rao     cur_index += 2;
6906663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
69103d196b7SBharata B Rao         uint32_t associativity[] = {
69203d196b7SBharata B Rao             cpu_to_be32(0x0),
69303d196b7SBharata B Rao             cpu_to_be32(0x0),
69403d196b7SBharata B Rao             cpu_to_be32(0x0),
69503d196b7SBharata B Rao             cpu_to_be32(i)
69603d196b7SBharata B Rao         };
69703d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
69803d196b7SBharata B Rao         cur_index += 4;
69903d196b7SBharata B Rao     }
70003d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
70103d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
70203d196b7SBharata B Rao out:
70303d196b7SBharata B Rao     g_free(int_buf);
70403d196b7SBharata B Rao     return ret;
70503d196b7SBharata B Rao }
70603d196b7SBharata B Rao 
7076787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
7086787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
7096787d27bSMichael Roth {
7106787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
711417ece33SMichael Roth     int ret = 0, offset;
7126787d27bSMichael Roth 
7136787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
7146787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
7156787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
7166787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
717417ece33SMichael Roth         if (ret) {
718417ece33SMichael Roth             goto out;
719417ece33SMichael Roth         }
7206787d27bSMichael Roth     }
7216787d27bSMichael Roth 
722417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
723417ece33SMichael Roth     if (offset < 0) {
724417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
725417ece33SMichael Roth         if (offset < 0) {
726417ece33SMichael Roth             return offset;
727417ece33SMichael Roth         }
728417ece33SMichael Roth     }
729417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
730417ece33SMichael Roth                                  "ibm,architecture-vec-5");
731417ece33SMichael Roth 
732417ece33SMichael Roth out:
7336787d27bSMichael Roth     return ret;
7346787d27bSMichael Roth }
7356787d27bSMichael Roth 
73603d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
73703d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
7386787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
73903d196b7SBharata B Rao {
74003d196b7SBharata B Rao     void *fdt, *fdt_skel;
74103d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
74203d196b7SBharata B Rao 
74303d196b7SBharata B Rao     size -= sizeof(hdr);
74403d196b7SBharata B Rao 
74503d196b7SBharata B Rao     /* Create sceleton */
74603d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
74703d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
74803d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
74903d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
75003d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
75103d196b7SBharata B Rao     fdt = g_malloc0(size);
75203d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
75303d196b7SBharata B Rao     g_free(fdt_skel);
75403d196b7SBharata B Rao 
75503d196b7SBharata B Rao     /* Fixup cpu nodes */
75603d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
75703d196b7SBharata B Rao 
7586787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
7596787d27bSMichael Roth         return -1;
76003d196b7SBharata B Rao     }
76103d196b7SBharata B Rao 
76203d196b7SBharata B Rao     /* Pack resulting tree */
76303d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
76403d196b7SBharata B Rao 
76503d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
76603d196b7SBharata B Rao         trace_spapr_cas_failed(size);
76703d196b7SBharata B Rao         return -1;
76803d196b7SBharata B Rao     }
76903d196b7SBharata B Rao 
77003d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
77103d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
77203d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
77303d196b7SBharata B Rao     g_free(fdt);
77403d196b7SBharata B Rao 
77503d196b7SBharata B Rao     return 0;
77603d196b7SBharata B Rao }
77703d196b7SBharata B Rao 
7783f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
7793f5dabceSDavid Gibson {
7803f5dabceSDavid Gibson     int rtas;
7813f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
7823f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
7833f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
7843f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
7853f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
7863f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
7873f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
7883f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
7893f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
7903f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
7913f5dabceSDavid Gibson     };
7923f5dabceSDavid Gibson 
7933f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
7943f5dabceSDavid Gibson 
7953f5dabceSDavid Gibson     /* hypertas */
7963f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
7973f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
7983f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
7993f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
8003f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
8013f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
8023f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
8033f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
8043f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
8053f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
8063f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
8073f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
8083f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
8093f5dabceSDavid Gibson 
8103f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
8113f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
8123f5dabceSDavid Gibson     }
8133f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
8143f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
8153f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
8163f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
8173f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
8183f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
8193f5dabceSDavid Gibson 
8203f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
8213f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
8223f5dabceSDavid Gibson 
8233f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
8243f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
8253f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
8263f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
8273f5dabceSDavid Gibson 
8283f5dabceSDavid Gibson     if (msi_nonbroken) {
8293f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
8303f5dabceSDavid Gibson     }
8313f5dabceSDavid Gibson 
8323f5dabceSDavid Gibson     /*
8333f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
8343f5dabceSDavid Gibson      * back to the guest cpu.
8353f5dabceSDavid Gibson      *
8363f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
8373f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
8383f5dabceSDavid Gibson      */
8393f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
8403f5dabceSDavid Gibson 
8413f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
8423f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
8433f5dabceSDavid Gibson 
8443f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
8453f5dabceSDavid Gibson }
8463f5dabceSDavid Gibson 
8477c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
8487c866c6aSDavid Gibson {
8497c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
8507c866c6aSDavid Gibson     int chosen;
8517c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
8527c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
8537c866c6aSDavid Gibson     size_t cb = 0;
8547c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
8557c866c6aSDavid Gibson 
8567c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
8577c866c6aSDavid Gibson 
8587c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
8597c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
8607c866c6aSDavid Gibson                           spapr->initrd_base));
8617c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
8627c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
8637c866c6aSDavid Gibson 
8647c866c6aSDavid Gibson     if (spapr->kernel_size) {
8657c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
8667c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
8677c866c6aSDavid Gibson 
8687c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
8697c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
8707c866c6aSDavid Gibson         if (spapr->kernel_le) {
8717c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
8727c866c6aSDavid Gibson         }
8737c866c6aSDavid Gibson     }
8747c866c6aSDavid Gibson     if (boot_menu) {
8757c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
8767c866c6aSDavid Gibson     }
8777c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
8787c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
8797c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
8807c866c6aSDavid Gibson 
8817c866c6aSDavid Gibson     if (cb && bootlist) {
8827c866c6aSDavid Gibson         int i;
8837c866c6aSDavid Gibson 
8847c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
8857c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
8867c866c6aSDavid Gibson                 bootlist[i] = ' ';
8877c866c6aSDavid Gibson             }
8887c866c6aSDavid Gibson         }
8897c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
8907c866c6aSDavid Gibson     }
8917c866c6aSDavid Gibson 
8927c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
8937c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
8947c866c6aSDavid Gibson     }
8957c866c6aSDavid Gibson 
8967c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
8977c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
8987c866c6aSDavid Gibson     }
8997c866c6aSDavid Gibson 
9007c866c6aSDavid Gibson     g_free(stdout_path);
9017c866c6aSDavid Gibson     g_free(bootlist);
9027c866c6aSDavid Gibson }
9037c866c6aSDavid Gibson 
904fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
905fca5f2dcSDavid Gibson {
906fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
907fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
908fca5f2dcSDavid Gibson     int hypervisor;
909fca5f2dcSDavid Gibson     uint8_t hypercall[16];
910fca5f2dcSDavid Gibson 
911fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
912fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
913fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
914fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
915fca5f2dcSDavid Gibson         /*
916fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
917fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
918fca5f2dcSDavid Gibson          */
919fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
920fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
921fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
922fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
923fca5f2dcSDavid Gibson         }
924fca5f2dcSDavid Gibson     }
925fca5f2dcSDavid Gibson }
926fca5f2dcSDavid Gibson 
927997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
92853018216SPaolo Bonzini                              hwaddr rtas_addr,
92953018216SPaolo Bonzini                              hwaddr rtas_size)
93053018216SPaolo Bonzini {
9315b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
9323c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
933c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
9347c866c6aSDavid Gibson     int ret;
93553018216SPaolo Bonzini     void *fdt;
93653018216SPaolo Bonzini     sPAPRPHBState *phb;
937398a0bd5SDavid Gibson     char *buf;
93853018216SPaolo Bonzini 
939398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
940398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
94153018216SPaolo Bonzini 
942398a0bd5SDavid Gibson     /* Root node */
943398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
944398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
945398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
946398a0bd5SDavid Gibson 
947398a0bd5SDavid Gibson     /*
948398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
949398a0bd5SDavid Gibson      * and what is the uuid of the guest
950398a0bd5SDavid Gibson      */
951398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
952398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
953398a0bd5SDavid Gibson         g_free(buf);
954398a0bd5SDavid Gibson     }
955398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
956398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
957398a0bd5SDavid Gibson         g_free(buf);
958398a0bd5SDavid Gibson     }
959398a0bd5SDavid Gibson 
960398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
961398a0bd5SDavid Gibson 
962398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
963398a0bd5SDavid Gibson     if (qemu_uuid_set) {
964398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
965398a0bd5SDavid Gibson     }
966398a0bd5SDavid Gibson     g_free(buf);
967398a0bd5SDavid Gibson 
968398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
969398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
970398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
971398a0bd5SDavid Gibson     }
972398a0bd5SDavid Gibson 
973398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
974398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
97553018216SPaolo Bonzini 
9769b9a1908SDavid Gibson     /* /interrupt controller */
977852ad27eSCédric Le Goater     spapr_dt_xics(spapr->nr_servers, fdt, PHANDLE_XICP);
9789b9a1908SDavid Gibson 
979e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
980e8f986fcSBharata B Rao     if (ret < 0) {
981ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
982e8f986fcSBharata B Rao         exit(1);
98353018216SPaolo Bonzini     }
98453018216SPaolo Bonzini 
985bf5a6696SDavid Gibson     /* /vdevice */
986bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
98753018216SPaolo Bonzini 
9884d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
9894d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
9904d9392beSThomas Huth         if (ret < 0) {
991ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
9924d9392beSThomas Huth             exit(1);
9934d9392beSThomas Huth         }
9944d9392beSThomas Huth     }
9954d9392beSThomas Huth 
99653018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
99753018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
99853018216SPaolo Bonzini         if (ret < 0) {
999da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
100053018216SPaolo Bonzini             exit(1);
100153018216SPaolo Bonzini         }
1002da34fed7SThomas Huth     }
100353018216SPaolo Bonzini 
10040da6f3feSBharata B Rao     /* cpus */
10050da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
100653018216SPaolo Bonzini 
1007c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
1008c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1009c20d332aSBharata B Rao     }
1010c20d332aSBharata B Rao 
1011c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
1012af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1013af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1014af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1015af81cf32SBharata B Rao         if (ret < 0) {
1016af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1017af81cf32SBharata B Rao             exit(1);
1018af81cf32SBharata B Rao         }
1019af81cf32SBharata B Rao     }
1020af81cf32SBharata B Rao 
1021ffb1e275SDavid Gibson     /* /event-sources */
1022ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1023ffb1e275SDavid Gibson 
10243f5dabceSDavid Gibson     /* /rtas */
10253f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
10263f5dabceSDavid Gibson 
10277c866c6aSDavid Gibson     /* /chosen */
10287c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1029cf6e5223SDavid Gibson 
1030fca5f2dcSDavid Gibson     /* /hypervisor */
1031fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1032fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1033fca5f2dcSDavid Gibson     }
1034fca5f2dcSDavid Gibson 
1035cf6e5223SDavid Gibson     /* Build memory reserve map */
1036cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1037cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1038cf6e5223SDavid Gibson     }
1039cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1040cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1041cf6e5223SDavid Gibson     }
1042cf6e5223SDavid Gibson 
10436787d27bSMichael Roth     /* ibm,client-architecture-support updates */
10446787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
10456787d27bSMichael Roth     if (ret < 0) {
10466787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
10476787d27bSMichael Roth         exit(1);
10486787d27bSMichael Roth     }
10496787d27bSMichael Roth 
1050997b6cfcSDavid Gibson     return fdt;
105153018216SPaolo Bonzini }
105253018216SPaolo Bonzini 
105353018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
105453018216SPaolo Bonzini {
105553018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
105653018216SPaolo Bonzini }
105753018216SPaolo Bonzini 
10581d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
10591d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
106053018216SPaolo Bonzini {
106153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
106253018216SPaolo Bonzini 
10638d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
10648d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
10658d04fb55SJan Kiszka 
106653018216SPaolo Bonzini     if (msr_pr) {
106753018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
106853018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
106953018216SPaolo Bonzini     } else {
107053018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
107153018216SPaolo Bonzini     }
107253018216SPaolo Bonzini }
107353018216SPaolo Bonzini 
10749861bb3eSSuraj Jitindar Singh static uint64_t spapr_get_patbe(PPCVirtualHypervisor *vhyp)
10759861bb3eSSuraj Jitindar Singh {
10769861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
10779861bb3eSSuraj Jitindar Singh 
10789861bb3eSSuraj Jitindar Singh     return spapr->patb_entry;
10799861bb3eSSuraj Jitindar Singh }
10809861bb3eSSuraj Jitindar Singh 
1081e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1082e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1083e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1084e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1085e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1086e6b8fd24SSamuel Mendoza-Jonas 
1087715c5407SDavid Gibson /*
1088715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1089715c5407SDavid Gibson  */
1090715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1091715c5407SDavid Gibson {
1092715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1093715c5407SDavid Gibson         return spapr->htab_fd;
1094715c5407SDavid Gibson     }
1095715c5407SDavid Gibson 
1096715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1097715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1098715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1099715c5407SDavid Gibson                      strerror(errno));
1100715c5407SDavid Gibson     }
1101715c5407SDavid Gibson 
1102715c5407SDavid Gibson     return spapr->htab_fd;
1103715c5407SDavid Gibson }
1104715c5407SDavid Gibson 
1105715c5407SDavid Gibson static void close_htab_fd(sPAPRMachineState *spapr)
1106715c5407SDavid Gibson {
1107715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1108715c5407SDavid Gibson         close(spapr->htab_fd);
1109715c5407SDavid Gibson     }
1110715c5407SDavid Gibson     spapr->htab_fd = -1;
1111715c5407SDavid Gibson }
1112715c5407SDavid Gibson 
1113e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1114e57ca75cSDavid Gibson {
1115e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1116e57ca75cSDavid Gibson 
1117e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1118e57ca75cSDavid Gibson }
1119e57ca75cSDavid Gibson 
1120e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1121e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1122e57ca75cSDavid Gibson {
1123e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1124e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1125e57ca75cSDavid Gibson 
1126e57ca75cSDavid Gibson     if (!spapr->htab) {
1127e57ca75cSDavid Gibson         /*
1128e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1129e57ca75cSDavid Gibson          */
1130e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1131e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1132e57ca75cSDavid Gibson         return hptes;
1133e57ca75cSDavid Gibson     }
1134e57ca75cSDavid Gibson 
1135e57ca75cSDavid Gibson     /*
1136e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1137e57ca75cSDavid Gibson      * accessible PTEG.
1138e57ca75cSDavid Gibson      */
1139e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1140e57ca75cSDavid Gibson }
1141e57ca75cSDavid Gibson 
1142e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1143e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1144e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1145e57ca75cSDavid Gibson {
1146e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1147e57ca75cSDavid Gibson 
1148e57ca75cSDavid Gibson     if (!spapr->htab) {
1149e57ca75cSDavid Gibson         g_free((void *)hptes);
1150e57ca75cSDavid Gibson     }
1151e57ca75cSDavid Gibson 
1152e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1153e57ca75cSDavid Gibson }
1154e57ca75cSDavid Gibson 
1155e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1156e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1157e57ca75cSDavid Gibson {
1158e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1159e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1160e57ca75cSDavid Gibson 
1161e57ca75cSDavid Gibson     if (!spapr->htab) {
1162e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1163e57ca75cSDavid Gibson     } else {
1164e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1165e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1166e57ca75cSDavid Gibson     }
1167e57ca75cSDavid Gibson }
1168e57ca75cSDavid Gibson 
11698dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
11708dfe8e7fSDavid Gibson {
11718dfe8e7fSDavid Gibson     int shift;
11728dfe8e7fSDavid Gibson 
11738dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
11748dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
11758dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
11768dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
11778dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
11788dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
11798dfe8e7fSDavid Gibson     return shift;
11808dfe8e7fSDavid Gibson }
11818dfe8e7fSDavid Gibson 
1182c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1183c5f54f3eSDavid Gibson                                  Error **errp)
118453018216SPaolo Bonzini {
1185c5f54f3eSDavid Gibson     long rc;
118653018216SPaolo Bonzini 
1187c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
1188c5f54f3eSDavid Gibson     g_free(spapr->htab);
1189c5f54f3eSDavid Gibson     spapr->htab = NULL;
1190c5f54f3eSDavid Gibson     spapr->htab_shift = 0;
1191c5f54f3eSDavid Gibson     close_htab_fd(spapr);
119253018216SPaolo Bonzini 
1193c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1194c5f54f3eSDavid Gibson     if (rc < 0) {
1195c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1196c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1197c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1198c5f54f3eSDavid Gibson                          shift);
1199c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1200c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1201c5f54f3eSDavid Gibson     } else if (rc > 0) {
1202c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1203c5f54f3eSDavid Gibson         if (rc != shift) {
1204c5f54f3eSDavid Gibson             error_setg(errp,
1205c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1206c5f54f3eSDavid Gibson                        shift, rc);
12077735fedaSBharata B Rao         }
12087735fedaSBharata B Rao 
120953018216SPaolo Bonzini         spapr->htab_shift = shift;
1210c18ad9a5SDavid Gibson         spapr->htab = NULL;
1211b817772aSBharata B Rao     } else {
1212c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1213c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1214c5f54f3eSDavid Gibson         int i;
121501a57972SSamuel Mendoza-Jonas 
1216c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1217c5f54f3eSDavid Gibson         if (!spapr->htab) {
1218c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1219c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1220c5f54f3eSDavid Gibson             return;
1221b817772aSBharata B Rao         }
1222b817772aSBharata B Rao 
1223c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1224c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1225b817772aSBharata B Rao 
1226c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1227c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
12287735fedaSBharata B Rao         }
122953018216SPaolo Bonzini     }
123053018216SPaolo Bonzini }
123153018216SPaolo Bonzini 
12324f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
12339e3f9733SAlexander Graf {
12349e3f9733SAlexander Graf     bool matched = false;
12359e3f9733SAlexander Graf 
12369e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
12379e3f9733SAlexander Graf         matched = true;
12389e3f9733SAlexander Graf     }
12399e3f9733SAlexander Graf 
12409e3f9733SAlexander Graf     if (!matched) {
12419e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
12429e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
12439e3f9733SAlexander Graf         exit(1);
12449e3f9733SAlexander Graf     }
12459e3f9733SAlexander Graf }
12469e3f9733SAlexander Graf 
124753018216SPaolo Bonzini static void ppc_spapr_reset(void)
124853018216SPaolo Bonzini {
1249c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1250c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1251182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1252b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1253cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1254997b6cfcSDavid Gibson     void *fdt;
1255997b6cfcSDavid Gibson     int rc;
1256259186a7SAndreas Färber 
12579e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
12589e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
12599e3f9733SAlexander Graf 
12609861bb3eSSuraj Jitindar Singh     spapr->patb_entry = 0;
12619861bb3eSSuraj Jitindar Singh 
1262c5f54f3eSDavid Gibson     /* Allocate and/or reset the hash page table */
1263c5f54f3eSDavid Gibson     spapr_reallocate_hpt(spapr,
1264c5f54f3eSDavid Gibson                          spapr_hpt_shift_for_ramsize(machine->maxram_size),
1265c5f54f3eSDavid Gibson                          &error_fatal);
1266c5f54f3eSDavid Gibson 
1267c5f54f3eSDavid Gibson     /* Update the RMA size if necessary */
1268c5f54f3eSDavid Gibson     if (spapr->vrma_adjust) {
1269c5f54f3eSDavid Gibson         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1270c5f54f3eSDavid Gibson                                           spapr->htab_shift);
1271c5f54f3eSDavid Gibson     }
127253018216SPaolo Bonzini 
127353018216SPaolo Bonzini     qemu_devices_reset();
127453018216SPaolo Bonzini 
1275b7d1f77aSBenjamin Herrenschmidt     /*
1276b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1277b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1278b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1279b7d1f77aSBenjamin Herrenschmidt      */
1280b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1281cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1282cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1283b7d1f77aSBenjamin Herrenschmidt 
12846787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
12856787d27bSMichael Roth      * negotiated options and start from scratch */
12866787d27bSMichael Roth     if (!spapr->cas_reboot) {
12876787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
12886787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
12896787d27bSMichael Roth     }
12906787d27bSMichael Roth 
1291cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
129253018216SPaolo Bonzini 
12932cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1294b7d1f77aSBenjamin Herrenschmidt 
1295997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1296997b6cfcSDavid Gibson 
1297997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1298997b6cfcSDavid Gibson     assert(rc == 0);
1299997b6cfcSDavid Gibson 
1300997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1301997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1302997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1303997b6cfcSDavid Gibson         exit(1);
1304997b6cfcSDavid Gibson     }
1305997b6cfcSDavid Gibson 
1306997b6cfcSDavid Gibson     /* Load the fdt */
1307997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1308cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1309997b6cfcSDavid Gibson     g_free(fdt);
1310997b6cfcSDavid Gibson 
131153018216SPaolo Bonzini     /* Set up the entry state */
1312182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1313cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1314182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1315182735efSAndreas Färber     first_cpu->halted = 0;
13161b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
131753018216SPaolo Bonzini 
13186787d27bSMichael Roth     spapr->cas_reboot = false;
131953018216SPaolo Bonzini }
132053018216SPaolo Bonzini 
132128e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
132253018216SPaolo Bonzini {
13232ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
13243978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
132553018216SPaolo Bonzini 
13263978b863SPaolo Bonzini     if (dinfo) {
13276231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
13286231a6daSMarkus Armbruster                             &error_fatal);
132953018216SPaolo Bonzini     }
133053018216SPaolo Bonzini 
133153018216SPaolo Bonzini     qdev_init_nofail(dev);
133253018216SPaolo Bonzini 
133353018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
133453018216SPaolo Bonzini }
133553018216SPaolo Bonzini 
133628e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
133728df36a1SDavid Gibson {
133828df36a1SDavid Gibson     DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
133928df36a1SDavid Gibson 
134028df36a1SDavid Gibson     qdev_init_nofail(dev);
134128df36a1SDavid Gibson     spapr->rtc = dev;
134274e5ae28SDavid Gibson 
134374e5ae28SDavid Gibson     object_property_add_alias(qdev_get_machine(), "rtc-time",
134474e5ae28SDavid Gibson                               OBJECT(spapr->rtc), "date", NULL);
134528df36a1SDavid Gibson }
134628df36a1SDavid Gibson 
134753018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
134814c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
134953018216SPaolo Bonzini {
135053018216SPaolo Bonzini     switch (vga_interface_type) {
135153018216SPaolo Bonzini     case VGA_NONE:
13527effdaa3SMark Wu         return false;
13537effdaa3SMark Wu     case VGA_DEVICE:
13547effdaa3SMark Wu         return true;
135553018216SPaolo Bonzini     case VGA_STD:
1356b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
135753018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
135853018216SPaolo Bonzini     default:
135914c6a894SDavid Gibson         error_setg(errp,
136014c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
136114c6a894SDavid Gibson         return false;
136253018216SPaolo Bonzini     }
136353018216SPaolo Bonzini }
136453018216SPaolo Bonzini 
1365880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1366880ae7deSDavid Gibson {
136728e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1368880ae7deSDavid Gibson     int err = 0;
1369880ae7deSDavid Gibson 
1370a7ff1212SCédric Le Goater     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
1371a7ff1212SCédric Le Goater         int i;
1372a7ff1212SCédric Le Goater         for (i = 0; i < spapr->nr_servers; i++) {
1373a7ff1212SCédric Le Goater             icp_resend(&spapr->icps[i]);
1374a7ff1212SCédric Le Goater         }
1375a7ff1212SCédric Le Goater     }
1376a7ff1212SCédric Le Goater 
1377631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1378880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1379880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1380880ae7deSDavid Gibson      * value into the RTC device */
1381880ae7deSDavid Gibson     if (version_id < 3) {
1382880ae7deSDavid Gibson         err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1383880ae7deSDavid Gibson     }
1384880ae7deSDavid Gibson 
1385880ae7deSDavid Gibson     return err;
1386880ae7deSDavid Gibson }
1387880ae7deSDavid Gibson 
1388880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1389880ae7deSDavid Gibson {
1390880ae7deSDavid Gibson     return version_id < 3;
1391880ae7deSDavid Gibson }
1392880ae7deSDavid Gibson 
139362ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
139462ef3760SMichael Roth {
139562ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
139662ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
139762ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
139862ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
139962ef3760SMichael Roth     bool cas_needed;
140062ef3760SMichael Roth 
140162ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
140262ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
140362ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
140462ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
140562ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
140662ef3760SMichael Roth      * negotiatied on the source side.
140762ef3760SMichael Roth      *
140862ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
140962ef3760SMichael Roth      * are the only options available on the current machine/platform.
141062ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
141162ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
141262ef3760SMichael Roth      * compatibility.
141362ef3760SMichael Roth      *
141462ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
141562ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
141662ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
141762ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
141862ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
141962ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
142062ef3760SMichael Roth      *
142162ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
142262ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
142362ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
142462ef3760SMichael Roth      */
142562ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
142662ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
142762ef3760SMichael Roth 
142862ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
142962ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
143062ef3760SMichael Roth      * removed via machine options, which could generate a false positive
143162ef3760SMichael Roth      * that breaks migration.
143262ef3760SMichael Roth      */
143362ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
143462ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
143562ef3760SMichael Roth 
143662ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
143762ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
143862ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
143962ef3760SMichael Roth 
144062ef3760SMichael Roth     return cas_needed;
144162ef3760SMichael Roth }
144262ef3760SMichael Roth 
144362ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
144462ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
144562ef3760SMichael Roth     .version_id = 1,
144662ef3760SMichael Roth     .minimum_version_id = 1,
144762ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
144862ef3760SMichael Roth     .fields = (VMStateField[]) {
144962ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
145062ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
145162ef3760SMichael Roth         VMSTATE_END_OF_LIST()
145262ef3760SMichael Roth     },
145362ef3760SMichael Roth };
145462ef3760SMichael Roth 
14559861bb3eSSuraj Jitindar Singh static bool spapr_patb_entry_needed(void *opaque)
14569861bb3eSSuraj Jitindar Singh {
14579861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = opaque;
14589861bb3eSSuraj Jitindar Singh 
14599861bb3eSSuraj Jitindar Singh     return !!spapr->patb_entry;
14609861bb3eSSuraj Jitindar Singh }
14619861bb3eSSuraj Jitindar Singh 
14629861bb3eSSuraj Jitindar Singh static const VMStateDescription vmstate_spapr_patb_entry = {
14639861bb3eSSuraj Jitindar Singh     .name = "spapr_patb_entry",
14649861bb3eSSuraj Jitindar Singh     .version_id = 1,
14659861bb3eSSuraj Jitindar Singh     .minimum_version_id = 1,
14669861bb3eSSuraj Jitindar Singh     .needed = spapr_patb_entry_needed,
14679861bb3eSSuraj Jitindar Singh     .fields = (VMStateField[]) {
14689861bb3eSSuraj Jitindar Singh         VMSTATE_UINT64(patb_entry, sPAPRMachineState),
14699861bb3eSSuraj Jitindar Singh         VMSTATE_END_OF_LIST()
14709861bb3eSSuraj Jitindar Singh     },
14719861bb3eSSuraj Jitindar Singh };
14729861bb3eSSuraj Jitindar Singh 
14734be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
14744be21d56SDavid Gibson     .name = "spapr",
1475880ae7deSDavid Gibson     .version_id = 3,
14764be21d56SDavid Gibson     .minimum_version_id = 1,
1477880ae7deSDavid Gibson     .post_load = spapr_post_load,
14784be21d56SDavid Gibson     .fields = (VMStateField[]) {
1479880ae7deSDavid Gibson         /* used to be @next_irq */
1480880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
14814be21d56SDavid Gibson 
14824be21d56SDavid Gibson         /* RTC offset */
148328e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1484880ae7deSDavid Gibson 
148528e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
14864be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
14874be21d56SDavid Gibson     },
148862ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
148962ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
14909861bb3eSSuraj Jitindar Singh         &vmstate_spapr_patb_entry,
149162ef3760SMichael Roth         NULL
149262ef3760SMichael Roth     }
14934be21d56SDavid Gibson };
14944be21d56SDavid Gibson 
14954be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
14964be21d56SDavid Gibson {
149728e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
14984be21d56SDavid Gibson 
14994be21d56SDavid Gibson     /* "Iteration" header */
15004be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
15014be21d56SDavid Gibson 
1502e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1503e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1504e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1505e68cb8b4SAlexey Kardashevskiy     } else {
1506e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
15074be21d56SDavid Gibson     }
15084be21d56SDavid Gibson 
1509e68cb8b4SAlexey Kardashevskiy 
1510e68cb8b4SAlexey Kardashevskiy     return 0;
1511e68cb8b4SAlexey Kardashevskiy }
15124be21d56SDavid Gibson 
151328e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
15144be21d56SDavid Gibson                                  int64_t max_ns)
15154be21d56SDavid Gibson {
1516378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
15174be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
15184be21d56SDavid Gibson     int index = spapr->htab_save_index;
1519bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
15204be21d56SDavid Gibson 
15214be21d56SDavid Gibson     assert(spapr->htab_first_pass);
15224be21d56SDavid Gibson 
15234be21d56SDavid Gibson     do {
15244be21d56SDavid Gibson         int chunkstart;
15254be21d56SDavid Gibson 
15264be21d56SDavid Gibson         /* Consume invalid HPTEs */
15274be21d56SDavid Gibson         while ((index < htabslots)
15284be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
15294be21d56SDavid Gibson             index++;
15304be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
15314be21d56SDavid Gibson         }
15324be21d56SDavid Gibson 
15334be21d56SDavid Gibson         /* Consume valid HPTEs */
15344be21d56SDavid Gibson         chunkstart = index;
1535338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
15364be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
15374be21d56SDavid Gibson             index++;
15384be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
15394be21d56SDavid Gibson         }
15404be21d56SDavid Gibson 
15414be21d56SDavid Gibson         if (index > chunkstart) {
15424be21d56SDavid Gibson             int n_valid = index - chunkstart;
15434be21d56SDavid Gibson 
15444be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
15454be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
15464be21d56SDavid Gibson             qemu_put_be16(f, 0);
15474be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
15484be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
15494be21d56SDavid Gibson 
1550378bc217SDavid Gibson             if (has_timeout &&
1551378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
15524be21d56SDavid Gibson                 break;
15534be21d56SDavid Gibson             }
15544be21d56SDavid Gibson         }
15554be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
15564be21d56SDavid Gibson 
15574be21d56SDavid Gibson     if (index >= htabslots) {
15584be21d56SDavid Gibson         assert(index == htabslots);
15594be21d56SDavid Gibson         index = 0;
15604be21d56SDavid Gibson         spapr->htab_first_pass = false;
15614be21d56SDavid Gibson     }
15624be21d56SDavid Gibson     spapr->htab_save_index = index;
15634be21d56SDavid Gibson }
15644be21d56SDavid Gibson 
156528e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
15664be21d56SDavid Gibson                                 int64_t max_ns)
15674be21d56SDavid Gibson {
15684be21d56SDavid Gibson     bool final = max_ns < 0;
15694be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
15704be21d56SDavid Gibson     int examined = 0, sent = 0;
15714be21d56SDavid Gibson     int index = spapr->htab_save_index;
1572bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
15734be21d56SDavid Gibson 
15744be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
15754be21d56SDavid Gibson 
15764be21d56SDavid Gibson     do {
15774be21d56SDavid Gibson         int chunkstart, invalidstart;
15784be21d56SDavid Gibson 
15794be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
15804be21d56SDavid Gibson         while ((index < htabslots)
15814be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
15824be21d56SDavid Gibson             index++;
15834be21d56SDavid Gibson             examined++;
15844be21d56SDavid Gibson         }
15854be21d56SDavid Gibson 
15864be21d56SDavid Gibson         chunkstart = index;
15874be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1588338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
15894be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
15904be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
15914be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
15924be21d56SDavid Gibson             index++;
15934be21d56SDavid Gibson             examined++;
15944be21d56SDavid Gibson         }
15954be21d56SDavid Gibson 
15964be21d56SDavid Gibson         invalidstart = index;
15974be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1598338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
15994be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
16004be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
16014be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
16024be21d56SDavid Gibson             index++;
16034be21d56SDavid Gibson             examined++;
16044be21d56SDavid Gibson         }
16054be21d56SDavid Gibson 
16064be21d56SDavid Gibson         if (index > chunkstart) {
16074be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
16084be21d56SDavid Gibson             int n_invalid = index - invalidstart;
16094be21d56SDavid Gibson 
16104be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
16114be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
16124be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
16134be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
16144be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
16154be21d56SDavid Gibson             sent += index - chunkstart;
16164be21d56SDavid Gibson 
1617bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
16184be21d56SDavid Gibson                 break;
16194be21d56SDavid Gibson             }
16204be21d56SDavid Gibson         }
16214be21d56SDavid Gibson 
16224be21d56SDavid Gibson         if (examined >= htabslots) {
16234be21d56SDavid Gibson             break;
16244be21d56SDavid Gibson         }
16254be21d56SDavid Gibson 
16264be21d56SDavid Gibson         if (index >= htabslots) {
16274be21d56SDavid Gibson             assert(index == htabslots);
16284be21d56SDavid Gibson             index = 0;
16294be21d56SDavid Gibson         }
16304be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
16314be21d56SDavid Gibson 
16324be21d56SDavid Gibson     if (index >= htabslots) {
16334be21d56SDavid Gibson         assert(index == htabslots);
16344be21d56SDavid Gibson         index = 0;
16354be21d56SDavid Gibson     }
16364be21d56SDavid Gibson 
16374be21d56SDavid Gibson     spapr->htab_save_index = index;
16384be21d56SDavid Gibson 
1639e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
16404be21d56SDavid Gibson }
16414be21d56SDavid Gibson 
1642e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1643e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1644e68cb8b4SAlexey Kardashevskiy 
16454be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
16464be21d56SDavid Gibson {
164728e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1648715c5407SDavid Gibson     int fd;
1649e68cb8b4SAlexey Kardashevskiy     int rc = 0;
16504be21d56SDavid Gibson 
16514be21d56SDavid Gibson     /* Iteration header */
16524be21d56SDavid Gibson     qemu_put_be32(f, 0);
16534be21d56SDavid Gibson 
1654e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1655e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1656e68cb8b4SAlexey Kardashevskiy 
1657715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1658715c5407SDavid Gibson         if (fd < 0) {
1659715c5407SDavid Gibson             return fd;
166001a57972SSamuel Mendoza-Jonas         }
166101a57972SSamuel Mendoza-Jonas 
1662715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1663e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1664e68cb8b4SAlexey Kardashevskiy             return rc;
1665e68cb8b4SAlexey Kardashevskiy         }
1666e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
16674be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
16684be21d56SDavid Gibson     } else {
1669e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
16704be21d56SDavid Gibson     }
16714be21d56SDavid Gibson 
16724be21d56SDavid Gibson     /* End marker */
16734be21d56SDavid Gibson     qemu_put_be32(f, 0);
16744be21d56SDavid Gibson     qemu_put_be16(f, 0);
16754be21d56SDavid Gibson     qemu_put_be16(f, 0);
16764be21d56SDavid Gibson 
1677e68cb8b4SAlexey Kardashevskiy     return rc;
16784be21d56SDavid Gibson }
16794be21d56SDavid Gibson 
16804be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
16814be21d56SDavid Gibson {
168228e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1683715c5407SDavid Gibson     int fd;
16844be21d56SDavid Gibson 
16854be21d56SDavid Gibson     /* Iteration header */
16864be21d56SDavid Gibson     qemu_put_be32(f, 0);
16874be21d56SDavid Gibson 
1688e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1689e68cb8b4SAlexey Kardashevskiy         int rc;
1690e68cb8b4SAlexey Kardashevskiy 
1691e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1692e68cb8b4SAlexey Kardashevskiy 
1693715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1694715c5407SDavid Gibson         if (fd < 0) {
1695715c5407SDavid Gibson             return fd;
169601a57972SSamuel Mendoza-Jonas         }
169701a57972SSamuel Mendoza-Jonas 
1698715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1699e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1700e68cb8b4SAlexey Kardashevskiy             return rc;
1701e68cb8b4SAlexey Kardashevskiy         }
1702e68cb8b4SAlexey Kardashevskiy     } else {
1703378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1704378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1705378bc217SDavid Gibson         }
17064be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1707e68cb8b4SAlexey Kardashevskiy     }
17084be21d56SDavid Gibson 
17094be21d56SDavid Gibson     /* End marker */
17104be21d56SDavid Gibson     qemu_put_be32(f, 0);
17114be21d56SDavid Gibson     qemu_put_be16(f, 0);
17124be21d56SDavid Gibson     qemu_put_be16(f, 0);
17134be21d56SDavid Gibson 
17144be21d56SDavid Gibson     return 0;
17154be21d56SDavid Gibson }
17164be21d56SDavid Gibson 
17174be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
17184be21d56SDavid Gibson {
171928e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
17204be21d56SDavid Gibson     uint32_t section_hdr;
1721e68cb8b4SAlexey Kardashevskiy     int fd = -1;
17224be21d56SDavid Gibson 
17234be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
172498a5d100SDavid Gibson         error_report("htab_load() bad version");
17254be21d56SDavid Gibson         return -EINVAL;
17264be21d56SDavid Gibson     }
17274be21d56SDavid Gibson 
17284be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
17294be21d56SDavid Gibson 
17304be21d56SDavid Gibson     if (section_hdr) {
17319897e462SGreg Kurz         Error *local_err = NULL;
1732c5f54f3eSDavid Gibson 
1733c5f54f3eSDavid Gibson         /* First section gives the htab size */
1734c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1735c5f54f3eSDavid Gibson         if (local_err) {
1736c5f54f3eSDavid Gibson             error_report_err(local_err);
17374be21d56SDavid Gibson             return -EINVAL;
17384be21d56SDavid Gibson         }
17394be21d56SDavid Gibson         return 0;
17404be21d56SDavid Gibson     }
17414be21d56SDavid Gibson 
1742e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1743e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1744e68cb8b4SAlexey Kardashevskiy 
1745e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1746e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
174798a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1748e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1749e68cb8b4SAlexey Kardashevskiy         }
1750e68cb8b4SAlexey Kardashevskiy     }
1751e68cb8b4SAlexey Kardashevskiy 
17524be21d56SDavid Gibson     while (true) {
17534be21d56SDavid Gibson         uint32_t index;
17544be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
17554be21d56SDavid Gibson 
17564be21d56SDavid Gibson         index = qemu_get_be32(f);
17574be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
17584be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
17594be21d56SDavid Gibson 
17604be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
17614be21d56SDavid Gibson             /* End of Stream */
17624be21d56SDavid Gibson             break;
17634be21d56SDavid Gibson         }
17644be21d56SDavid Gibson 
1765e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
17664be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
17674be21d56SDavid Gibson             /* Bad index in stream */
176898a5d100SDavid Gibson             error_report(
176998a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
177098a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
17714be21d56SDavid Gibson             return -EINVAL;
17724be21d56SDavid Gibson         }
17734be21d56SDavid Gibson 
1774e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
17754be21d56SDavid Gibson             if (n_valid) {
17764be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
17774be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
17784be21d56SDavid Gibson             }
17794be21d56SDavid Gibson             if (n_invalid) {
17804be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
17814be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
17824be21d56SDavid Gibson             }
1783e68cb8b4SAlexey Kardashevskiy         } else {
1784e68cb8b4SAlexey Kardashevskiy             int rc;
1785e68cb8b4SAlexey Kardashevskiy 
1786e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1787e68cb8b4SAlexey Kardashevskiy 
1788e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1789e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1790e68cb8b4SAlexey Kardashevskiy                 return rc;
1791e68cb8b4SAlexey Kardashevskiy             }
1792e68cb8b4SAlexey Kardashevskiy         }
1793e68cb8b4SAlexey Kardashevskiy     }
1794e68cb8b4SAlexey Kardashevskiy 
1795e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1796e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1797e68cb8b4SAlexey Kardashevskiy         close(fd);
17984be21d56SDavid Gibson     }
17994be21d56SDavid Gibson 
18004be21d56SDavid Gibson     return 0;
18014be21d56SDavid Gibson }
18024be21d56SDavid Gibson 
1803c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1804c573fc03SThomas Huth {
1805c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1806c573fc03SThomas Huth 
1807c573fc03SThomas Huth     close_htab_fd(spapr);
1808c573fc03SThomas Huth }
1809c573fc03SThomas Huth 
18104be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
18114be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
18124be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1813a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1814c573fc03SThomas Huth     .cleanup = htab_cleanup,
18154be21d56SDavid Gibson     .load_state = htab_load,
18164be21d56SDavid Gibson };
18174be21d56SDavid Gibson 
18185b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
18195b2128d2SAlexander Graf                            Error **errp)
18205b2128d2SAlexander Graf {
18215b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
18225b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
18235b2128d2SAlexander Graf }
18245b2128d2SAlexander Graf 
1825224245bfSDavid Gibson /*
1826224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1827224245bfSDavid Gibson  *
1828224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1829224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1830224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1831224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1832224245bfSDavid Gibson  */
1833224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1834224245bfSDavid Gibson {
1835224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1836224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1837224245bfSDavid Gibson 
1838224245bfSDavid Gibson     if (d) {
1839224245bfSDavid Gibson         device_reset(d);
1840224245bfSDavid Gibson     }
1841224245bfSDavid Gibson }
1842224245bfSDavid Gibson 
1843224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1844224245bfSDavid Gibson {
1845224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1846224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1847e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1848224245bfSDavid Gibson     int i;
1849224245bfSDavid Gibson 
1850224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1851224245bfSDavid Gibson         sPAPRDRConnector *drc;
1852224245bfSDavid Gibson         uint64_t addr;
1853224245bfSDavid Gibson 
1854e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1855224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1856224245bfSDavid Gibson                                      addr/lmb_size);
1857224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1858224245bfSDavid Gibson     }
1859224245bfSDavid Gibson }
1860224245bfSDavid Gibson 
1861224245bfSDavid Gibson /*
1862224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1863224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1864224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1865224245bfSDavid Gibson  */
18667c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1867224245bfSDavid Gibson {
1868224245bfSDavid Gibson     int i;
1869224245bfSDavid Gibson 
18707c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
18717c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
18727c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
18737c150d6fSDavid Gibson                    machine->ram_size,
1874224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
18757c150d6fSDavid Gibson         return;
18767c150d6fSDavid Gibson     }
18777c150d6fSDavid Gibson 
18787c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
18797c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
18807c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
18817c150d6fSDavid Gibson                    machine->ram_size,
18827c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
18837c150d6fSDavid Gibson         return;
1884224245bfSDavid Gibson     }
1885224245bfSDavid Gibson 
1886224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1887224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
18887c150d6fSDavid Gibson             error_setg(errp,
18897c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
18907c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
18917c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
1892224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
18937c150d6fSDavid Gibson             return;
1894224245bfSDavid Gibson         }
1895224245bfSDavid Gibson     }
1896224245bfSDavid Gibson }
1897224245bfSDavid Gibson 
1898535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
1899535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1900535455fdSIgor Mammedov {
1901535455fdSIgor Mammedov     int index = id / smp_threads;
1902535455fdSIgor Mammedov 
1903535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
1904535455fdSIgor Mammedov         return NULL;
1905535455fdSIgor Mammedov     }
1906535455fdSIgor Mammedov     if (idx) {
1907535455fdSIgor Mammedov         *idx = index;
1908535455fdSIgor Mammedov     }
1909535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
1910535455fdSIgor Mammedov }
1911535455fdSIgor Mammedov 
19120c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
19130c86d0fdSDavid Gibson {
19140c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
19150c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
19160c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
19170c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
1918535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
1919535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
19200c86d0fdSDavid Gibson     int i;
19210c86d0fdSDavid Gibson 
19220c86d0fdSDavid Gibson     if (!type) {
19230c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
19240c86d0fdSDavid Gibson         exit(1);
19250c86d0fdSDavid Gibson     }
19260c86d0fdSDavid Gibson 
1927535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
1928c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
19290c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
19300c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
19310c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
19320c86d0fdSDavid Gibson             exit(1);
19330c86d0fdSDavid Gibson         }
19340c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
19350c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
19360c86d0fdSDavid Gibson                          max_cpus, smp_threads);
19370c86d0fdSDavid Gibson             exit(1);
19380c86d0fdSDavid Gibson         }
19390c86d0fdSDavid Gibson     } else {
19400c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
19410c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
19420c86d0fdSDavid Gibson             exit(1);
19430c86d0fdSDavid Gibson         }
1944535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
19450c86d0fdSDavid Gibson     }
19460c86d0fdSDavid Gibson 
1947535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
19480c86d0fdSDavid Gibson         int core_id = i * smp_threads;
19490c86d0fdSDavid Gibson 
1950c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
19510c86d0fdSDavid Gibson             sPAPRDRConnector *drc =
19520c86d0fdSDavid Gibson                 spapr_dr_connector_new(OBJECT(spapr),
19530c86d0fdSDavid Gibson                                        SPAPR_DR_CONNECTOR_TYPE_CPU,
19540c86d0fdSDavid Gibson                                        (core_id / smp_threads) * smt);
19550c86d0fdSDavid Gibson 
19560c86d0fdSDavid Gibson             qemu_register_reset(spapr_drc_reset, drc);
19570c86d0fdSDavid Gibson         }
19580c86d0fdSDavid Gibson 
1959535455fdSIgor Mammedov         if (i < boot_cores_nr) {
19600c86d0fdSDavid Gibson             Object *core  = object_new(type);
19610c86d0fdSDavid Gibson             int nr_threads = smp_threads;
19620c86d0fdSDavid Gibson 
19630c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
19640c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
19650c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
19660c86d0fdSDavid Gibson             }
19670c86d0fdSDavid Gibson 
19680c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
19690c86d0fdSDavid Gibson                                     &error_fatal);
19700c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
19710c86d0fdSDavid Gibson                                     &error_fatal);
19720c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
19730c86d0fdSDavid Gibson         }
19740c86d0fdSDavid Gibson     }
19750c86d0fdSDavid Gibson     g_free(type);
19760c86d0fdSDavid Gibson }
19770c86d0fdSDavid Gibson 
197853018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
19793ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
198053018216SPaolo Bonzini {
198128e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1982224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
19833ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
19843ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
198553018216SPaolo Bonzini     PCIHostState *phb;
198653018216SPaolo Bonzini     int i;
198753018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
198853018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1989658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1990658fa66bSAlexey Kardashevskiy     void *rma = NULL;
199153018216SPaolo Bonzini     hwaddr rma_alloc_size;
1992b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
1993b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
199453018216SPaolo Bonzini     char *filename;
199594a94e4cSBharata B Rao     int smt = kvmppc_smt_threads();
199653018216SPaolo Bonzini 
1997226419d6SMichael S. Tsirkin     msi_nonbroken = true;
199853018216SPaolo Bonzini 
199953018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
200053018216SPaolo Bonzini 
200153018216SPaolo Bonzini     /* Allocate RMA if necessary */
2002658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
200353018216SPaolo Bonzini 
200453018216SPaolo Bonzini     if (rma_alloc_size == -1) {
2005730fce59SThomas Huth         error_report("Unable to create RMA");
200653018216SPaolo Bonzini         exit(1);
200753018216SPaolo Bonzini     }
200853018216SPaolo Bonzini 
2009c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
201053018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
201153018216SPaolo Bonzini     } else {
2012c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
201353018216SPaolo Bonzini 
201453018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
201553018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
201653018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
201753018216SPaolo Bonzini          *
201853018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
201953018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
202053018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
202153018216SPaolo Bonzini          * isn't determined yet.
202253018216SPaolo Bonzini          */
202353018216SPaolo Bonzini         if (kvm_enabled()) {
202453018216SPaolo Bonzini             spapr->vrma_adjust = 1;
202553018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
202653018216SPaolo Bonzini         }
2027912acdf4SBenjamin Herrenschmidt 
2028912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
2029912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
2030912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
2031912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
2032912acdf4SBenjamin Herrenschmidt          */
2033912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
203453018216SPaolo Bonzini     }
203553018216SPaolo Bonzini 
2036c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
2037d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2038c4177479SAlexey Kardashevskiy                      spapr->rma_size);
2039c4177479SAlexey Kardashevskiy         exit(1);
2040c4177479SAlexey Kardashevskiy     }
2041c4177479SAlexey Kardashevskiy 
2042b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2043b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
204453018216SPaolo Bonzini 
20457b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
2046e6f7e110SCédric Le Goater     xics_system_init(machine, DIV_ROUND_UP(max_cpus * smt, smp_threads),
2047161deaf2SBenjamin Herrenschmidt                      XICS_IRQS_SPAPR, &error_fatal);
20487b565160SDavid Gibson 
2049facdb8b6SMichael Roth     /* Set up containers for ibm,client-set-architecture negotiated options */
2050facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2051facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2052facdb8b6SMichael Roth 
2053224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2054facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
20557c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2056224245bfSDavid Gibson     }
2057224245bfSDavid Gibson 
2058417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2059417ece33SMichael Roth 
2060ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2061ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2062ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2063ffbb1705SMichael Roth     }
2064ffbb1705SMichael Roth 
206553018216SPaolo Bonzini     /* init CPUs */
206619fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
20673daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
206853018216SPaolo Bonzini     }
206994a94e4cSBharata B Rao 
2070e703d2f7SGreg Kurz     ppc_cpu_parse_features(machine->cpu_model);
2071e703d2f7SGreg Kurz 
20720c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
207353018216SPaolo Bonzini 
2074026bfd89SDavid Gibson     if (kvm_enabled()) {
2075026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2076026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2077ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
20785145ad4fSNathan Whitehorn 
20795145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
20805145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2081026bfd89SDavid Gibson     }
2082026bfd89SDavid Gibson 
208353018216SPaolo Bonzini     /* allocate RAM */
2084f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2085fb164994SDavid Gibson                                          machine->ram_size);
2086f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
208753018216SPaolo Bonzini 
2088658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2089658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2090658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2091658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2092658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2093658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2094658fa66bSAlexey Kardashevskiy     }
2095658fa66bSAlexey Kardashevskiy 
20964a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
20974a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
20984a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
209971c9a3ddSBharata B Rao         /*
210071c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
210171c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
210271c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
210371c9a3ddSBharata B Rao          */
210471c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
210571c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
21064a1c9cf0SBharata B Rao 
210771c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
210871c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
210971c9a3ddSBharata B Rao         }
211071c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2111d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2112d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
211371c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2114d54e4d76SDavid Gibson             exit(1);
21154a1c9cf0SBharata B Rao         }
21164a1c9cf0SBharata B Rao 
21174a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
21184a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
21194a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
21204a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
21214a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
21224a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
21234a1c9cf0SBharata B Rao     }
21244a1c9cf0SBharata B Rao 
2125224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2126224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2127224245bfSDavid Gibson     }
2128224245bfSDavid Gibson 
212953018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
21304c56440dSStefan Weil     if (!filename) {
2131730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
21324c56440dSStefan Weil         exit(1);
21334c56440dSStefan Weil     }
2134b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
21358afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
21368afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
21378afc22a2SZhou Jie         exit(1);
21388afc22a2SZhou Jie     }
2139b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2140b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2141730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
214253018216SPaolo Bonzini         exit(1);
214353018216SPaolo Bonzini     }
214453018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2145730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
21462f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
214753018216SPaolo Bonzini         exit(1);
214853018216SPaolo Bonzini     }
214953018216SPaolo Bonzini     g_free(filename);
215053018216SPaolo Bonzini 
2151ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
215253018216SPaolo Bonzini     spapr_events_init(spapr);
215353018216SPaolo Bonzini 
215412f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
215528df36a1SDavid Gibson     spapr_rtc_create(spapr);
215612f42174SDavid Gibson 
215753018216SPaolo Bonzini     /* Set up VIO bus */
215853018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
215953018216SPaolo Bonzini 
216053018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
216153018216SPaolo Bonzini         if (serial_hds[i]) {
216253018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
216353018216SPaolo Bonzini         }
216453018216SPaolo Bonzini     }
216553018216SPaolo Bonzini 
216653018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
216753018216SPaolo Bonzini     spapr_create_nvram(spapr);
216853018216SPaolo Bonzini 
216953018216SPaolo Bonzini     /* Set up PCI */
217053018216SPaolo Bonzini     spapr_pci_rtas_init();
217153018216SPaolo Bonzini 
217289dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
217353018216SPaolo Bonzini 
217453018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
217553018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
217653018216SPaolo Bonzini 
217753018216SPaolo Bonzini         if (!nd->model) {
217853018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
217953018216SPaolo Bonzini         }
218053018216SPaolo Bonzini 
218153018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
218253018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
218353018216SPaolo Bonzini         } else {
218429b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
218553018216SPaolo Bonzini         }
218653018216SPaolo Bonzini     }
218753018216SPaolo Bonzini 
218853018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
218953018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
219053018216SPaolo Bonzini     }
219153018216SPaolo Bonzini 
219253018216SPaolo Bonzini     /* Graphics */
219314c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
219453018216SPaolo Bonzini         spapr->has_graphics = true;
2195c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
219653018216SPaolo Bonzini     }
219753018216SPaolo Bonzini 
21984ee9ced9SMarcel Apfelbaum     if (machine->usb) {
219957040d45SThomas Huth         if (smc->use_ohci_by_default) {
220053018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
220157040d45SThomas Huth         } else {
220257040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
220357040d45SThomas Huth         }
2204c86580b8SMarkus Armbruster 
220553018216SPaolo Bonzini         if (spapr->has_graphics) {
2206c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2207c86580b8SMarkus Armbruster 
2208c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2209c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
221053018216SPaolo Bonzini         }
221153018216SPaolo Bonzini     }
221253018216SPaolo Bonzini 
221353018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2214d54e4d76SDavid Gibson         error_report(
2215d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2216d54e4d76SDavid Gibson             MIN_RMA_SLOF);
221753018216SPaolo Bonzini         exit(1);
221853018216SPaolo Bonzini     }
221953018216SPaolo Bonzini 
222053018216SPaolo Bonzini     if (kernel_filename) {
222153018216SPaolo Bonzini         uint64_t lowaddr = 0;
222253018216SPaolo Bonzini 
2223a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2224a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2225a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2226a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2227a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2228a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2229a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
22307ef295eaSPeter Crosthwaite                                           0, 0);
2231a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
223216457e7fSBenjamin Herrenschmidt         }
2233a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2234a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2235a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
223653018216SPaolo Bonzini             exit(1);
223753018216SPaolo Bonzini         }
223853018216SPaolo Bonzini 
223953018216SPaolo Bonzini         /* load initrd */
224053018216SPaolo Bonzini         if (initrd_filename) {
224153018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
224253018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
224353018216SPaolo Bonzini              */
2244a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2245a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2246a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2247a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2248a19f7fb0SDavid Gibson                                                      load_limit
2249a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2250a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2251d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
225253018216SPaolo Bonzini                              initrd_filename);
225353018216SPaolo Bonzini                 exit(1);
225453018216SPaolo Bonzini             }
225553018216SPaolo Bonzini         }
225653018216SPaolo Bonzini     }
225753018216SPaolo Bonzini 
22588e7ea787SAndreas Färber     if (bios_name == NULL) {
22598e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
22608e7ea787SAndreas Färber     }
22618e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
22624c56440dSStefan Weil     if (!filename) {
226368fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
22644c56440dSStefan Weil         exit(1);
22654c56440dSStefan Weil     }
226653018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
226768fea5a0SThomas Huth     if (fw_size <= 0) {
226868fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
226953018216SPaolo Bonzini         exit(1);
227053018216SPaolo Bonzini     }
227153018216SPaolo Bonzini     g_free(filename);
227253018216SPaolo Bonzini 
227328e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
227428e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
227528e02042SDavid Gibson      * which predated MachineState but had a similar function */
22764be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
22774be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
22784be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
22794be21d56SDavid Gibson 
228046503c2bSMichael Roth     /* used by RTAS */
228146503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
228246503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
228346503c2bSMichael Roth 
22845b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
228542043e4fSLaurent Vivier 
228642043e4fSLaurent Vivier     /* to stop and start vmclock */
228742043e4fSLaurent Vivier     if (kvm_enabled()) {
228842043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
228942043e4fSLaurent Vivier                                          &spapr->tb);
229042043e4fSLaurent Vivier     }
229153018216SPaolo Bonzini }
229253018216SPaolo Bonzini 
2293135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2294135a129aSAneesh Kumar K.V {
2295135a129aSAneesh Kumar K.V     if (!vm_type) {
2296135a129aSAneesh Kumar K.V         return 0;
2297135a129aSAneesh Kumar K.V     }
2298135a129aSAneesh Kumar K.V 
2299135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2300135a129aSAneesh Kumar K.V         return 1;
2301135a129aSAneesh Kumar K.V     }
2302135a129aSAneesh Kumar K.V 
2303135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2304135a129aSAneesh Kumar K.V         return 2;
2305135a129aSAneesh Kumar K.V     }
2306135a129aSAneesh Kumar K.V 
2307135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2308135a129aSAneesh Kumar K.V     exit(1);
2309135a129aSAneesh Kumar K.V }
2310135a129aSAneesh Kumar K.V 
231171461b0fSAlexey Kardashevskiy /*
2312627b84f4SGonglei  * Implementation of an interface to adjust firmware path
231371461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
231471461b0fSAlexey Kardashevskiy  */
231571461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
231671461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
231771461b0fSAlexey Kardashevskiy {
231871461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
231971461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
232071461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
232171461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
232271461b0fSAlexey Kardashevskiy 
232371461b0fSAlexey Kardashevskiy     if (d) {
232471461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
232571461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
232671461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
232771461b0fSAlexey Kardashevskiy 
232871461b0fSAlexey Kardashevskiy         if (spapr) {
232971461b0fSAlexey Kardashevskiy             /*
233071461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
233171461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
233271461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
233371461b0fSAlexey Kardashevskiy              */
233471461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
233571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
233671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
233771461b0fSAlexey Kardashevskiy         } else if (virtio) {
233871461b0fSAlexey Kardashevskiy             /*
233971461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
234071461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
234171461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
234271461b0fSAlexey Kardashevskiy              * the actual binding is:
234371461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
234471461b0fSAlexey Kardashevskiy              */
234571461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
234671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
234771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
234871461b0fSAlexey Kardashevskiy         } else if (usb) {
234971461b0fSAlexey Kardashevskiy             /*
235071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
235171461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
235271461b0fSAlexey Kardashevskiy              */
235371461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
235471461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
235571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
235671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
235771461b0fSAlexey Kardashevskiy         }
235871461b0fSAlexey Kardashevskiy     }
235971461b0fSAlexey Kardashevskiy 
2360b99260ebSThomas Huth     /*
2361b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2362b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2363b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2364b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2365b99260ebSThomas Huth      */
2366b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2367b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2368b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2369b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2370b99260ebSThomas Huth         }
2371b99260ebSThomas Huth     }
2372b99260ebSThomas Huth 
237371461b0fSAlexey Kardashevskiy     if (phb) {
237471461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
237571461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
237671461b0fSAlexey Kardashevskiy     }
237771461b0fSAlexey Kardashevskiy 
237871461b0fSAlexey Kardashevskiy     return NULL;
237971461b0fSAlexey Kardashevskiy }
238071461b0fSAlexey Kardashevskiy 
238123825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
238223825581SEduardo Habkost {
238328e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
238423825581SEduardo Habkost 
238528e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
238623825581SEduardo Habkost }
238723825581SEduardo Habkost 
238823825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
238923825581SEduardo Habkost {
239028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
239123825581SEduardo Habkost 
239228e02042SDavid Gibson     g_free(spapr->kvm_type);
239328e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
239423825581SEduardo Habkost }
239523825581SEduardo Habkost 
2396f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2397f6229214SMichael Roth {
2398f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2399f6229214SMichael Roth 
2400f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2401f6229214SMichael Roth }
2402f6229214SMichael Roth 
2403f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2404f6229214SMichael Roth                                             Error **errp)
2405f6229214SMichael Roth {
2406f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2407f6229214SMichael Roth 
2408f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2409f6229214SMichael Roth }
2410f6229214SMichael Roth 
241123825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
241223825581SEduardo Habkost {
2413715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2414715c5407SDavid Gibson 
2415715c5407SDavid Gibson     spapr->htab_fd = -1;
2416f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
241723825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
241823825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
241949d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
242049d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
242149d2e648SMarcel Apfelbaum                                     NULL);
2422f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2423f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2424f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2425f6229214SMichael Roth                             NULL);
2426f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2427f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2428f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2429f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2430f6229214SMichael Roth                                     NULL);
243123825581SEduardo Habkost }
243223825581SEduardo Habkost 
243387bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
243487bbdd9cSDavid Gibson {
243587bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
243687bbdd9cSDavid Gibson 
243787bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
243887bbdd9cSDavid Gibson }
243987bbdd9cSDavid Gibson 
24401c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
244134316482SAlexey Kardashevskiy {
244234316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
244334316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
244434316482SAlexey Kardashevskiy }
244534316482SAlexey Kardashevskiy 
244634316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
244734316482SAlexey Kardashevskiy {
244834316482SAlexey Kardashevskiy     CPUState *cs;
244934316482SAlexey Kardashevskiy 
245034316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
24511c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
245234316482SAlexey Kardashevskiy     }
245334316482SAlexey Kardashevskiy }
245434316482SAlexey Kardashevskiy 
245579b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
245679b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
245779b78a6bSMichael Roth                            Error **errp)
2458c20d332aSBharata B Rao {
2459c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2460c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2461c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2462c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2463c20d332aSBharata B Rao     void *fdt;
246479b78a6bSMichael Roth     uint64_t addr = addr_start;
2465c20d332aSBharata B Rao 
2466c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2467c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2468c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2469c20d332aSBharata B Rao         g_assert(drc);
2470c20d332aSBharata B Rao 
2471c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2472c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2473c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2474c20d332aSBharata B Rao 
2475c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2476c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2477c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
24785c0139a8SMichael Roth         if (!dev->hotplugged) {
24795c0139a8SMichael Roth             /* guests expect coldplugged LMBs to be pre-allocated */
24805c0139a8SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
24815c0139a8SMichael Roth             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
24825c0139a8SMichael Roth         }
2483c20d332aSBharata B Rao     }
24845dd5238cSJianjun Duan     /* send hotplug notification to the
24855dd5238cSJianjun Duan      * guest only in case of hotplugged memory
24865dd5238cSJianjun Duan      */
24875dd5238cSJianjun Duan     if (dev->hotplugged) {
248879b78a6bSMichael Roth         if (dedicated_hp_event_source) {
248979b78a6bSMichael Roth             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
249079b78a6bSMichael Roth                     addr_start / SPAPR_MEMORY_BLOCK_SIZE);
249179b78a6bSMichael Roth             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
249279b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
249379b78a6bSMichael Roth                                                    nr_lmbs,
249479b78a6bSMichael Roth                                                    drck->get_index(drc));
249579b78a6bSMichael Roth         } else {
249679b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
249779b78a6bSMichael Roth                                            nr_lmbs);
249879b78a6bSMichael Roth         }
2499c20d332aSBharata B Rao     }
25005dd5238cSJianjun Duan }
2501c20d332aSBharata B Rao 
2502c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2503c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2504c20d332aSBharata B Rao {
2505c20d332aSBharata B Rao     Error *local_err = NULL;
2506c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2507c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2508c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2509c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2510c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2511c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2512c20d332aSBharata B Rao     uint64_t addr;
2513df587133SThomas Huth     char *mem_dev;
2514c20d332aSBharata B Rao 
2515c20d332aSBharata B Rao     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2516c20d332aSBharata B Rao         error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2517c20d332aSBharata B Rao                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2518c20d332aSBharata B Rao         goto out;
2519c20d332aSBharata B Rao     }
2520c20d332aSBharata B Rao 
2521df587133SThomas Huth     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2522df587133SThomas Huth     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2523df587133SThomas Huth         error_setg(&local_err, "Memory backend has bad page size. "
2524df587133SThomas Huth                    "Use 'memory-backend-file' with correct mem-path.");
2525df587133SThomas Huth         goto out;
2526df587133SThomas Huth     }
2527df587133SThomas Huth 
2528d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2529c20d332aSBharata B Rao     if (local_err) {
2530c20d332aSBharata B Rao         goto out;
2531c20d332aSBharata B Rao     }
2532c20d332aSBharata B Rao 
2533c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2534c20d332aSBharata B Rao     if (local_err) {
2535c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2536c20d332aSBharata B Rao         goto out;
2537c20d332aSBharata B Rao     }
2538c20d332aSBharata B Rao 
253979b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
254079b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
254179b78a6bSMichael Roth                    &error_abort);
2542c20d332aSBharata B Rao 
2543c20d332aSBharata B Rao out:
2544c20d332aSBharata B Rao     error_propagate(errp, local_err);
2545c20d332aSBharata B Rao }
2546c20d332aSBharata B Rao 
2547cf632463SBharata B Rao typedef struct sPAPRDIMMState {
2548cf632463SBharata B Rao     uint32_t nr_lmbs;
2549cf632463SBharata B Rao } sPAPRDIMMState;
2550cf632463SBharata B Rao 
2551cf632463SBharata B Rao static void spapr_lmb_release(DeviceState *dev, void *opaque)
2552cf632463SBharata B Rao {
2553cf632463SBharata B Rao     sPAPRDIMMState *ds = (sPAPRDIMMState *)opaque;
2554cf632463SBharata B Rao     HotplugHandler *hotplug_ctrl;
2555cf632463SBharata B Rao 
2556cf632463SBharata B Rao     if (--ds->nr_lmbs) {
2557cf632463SBharata B Rao         return;
2558cf632463SBharata B Rao     }
2559cf632463SBharata B Rao 
2560cf632463SBharata B Rao     g_free(ds);
2561cf632463SBharata B Rao 
2562cf632463SBharata B Rao     /*
2563cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
2564cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
2565cf632463SBharata B Rao      */
2566cf632463SBharata B Rao     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2567cf632463SBharata B Rao     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2568cf632463SBharata B Rao }
2569cf632463SBharata B Rao 
2570cf632463SBharata B Rao static void spapr_del_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
2571cf632463SBharata B Rao                            Error **errp)
2572cf632463SBharata B Rao {
2573cf632463SBharata B Rao     sPAPRDRConnector *drc;
2574cf632463SBharata B Rao     sPAPRDRConnectorClass *drck;
2575cf632463SBharata B Rao     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
2576cf632463SBharata B Rao     int i;
2577cf632463SBharata B Rao     sPAPRDIMMState *ds = g_malloc0(sizeof(sPAPRDIMMState));
2578cf632463SBharata B Rao     uint64_t addr = addr_start;
2579cf632463SBharata B Rao 
2580cf632463SBharata B Rao     ds->nr_lmbs = nr_lmbs;
2581cf632463SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2582cf632463SBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2583cf632463SBharata B Rao                 addr / SPAPR_MEMORY_BLOCK_SIZE);
2584cf632463SBharata B Rao         g_assert(drc);
2585cf632463SBharata B Rao 
2586cf632463SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2587cf632463SBharata B Rao         drck->detach(drc, dev, spapr_lmb_release, ds, errp);
2588cf632463SBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2589cf632463SBharata B Rao     }
2590cf632463SBharata B Rao 
2591cf632463SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2592cf632463SBharata B Rao                                    addr_start / SPAPR_MEMORY_BLOCK_SIZE);
2593cf632463SBharata B Rao     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2594cf632463SBharata B Rao     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
2595cf632463SBharata B Rao                                               nr_lmbs,
2596cf632463SBharata B Rao                                               drck->get_index(drc));
2597cf632463SBharata B Rao }
2598cf632463SBharata B Rao 
2599cf632463SBharata B Rao static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2600cf632463SBharata B Rao                                 Error **errp)
2601cf632463SBharata B Rao {
2602cf632463SBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2603cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2604cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2605cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2606cf632463SBharata B Rao 
2607cf632463SBharata B Rao     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2608cf632463SBharata B Rao     object_unparent(OBJECT(dev));
2609cf632463SBharata B Rao }
2610cf632463SBharata B Rao 
2611cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
2612cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
2613cf632463SBharata B Rao {
2614cf632463SBharata B Rao     Error *local_err = NULL;
2615cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2616cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2617cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2618cf632463SBharata B Rao     uint64_t size = memory_region_size(mr);
2619cf632463SBharata B Rao     uint64_t addr;
2620cf632463SBharata B Rao 
2621cf632463SBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2622cf632463SBharata B Rao     if (local_err) {
2623cf632463SBharata B Rao         goto out;
2624cf632463SBharata B Rao     }
2625cf632463SBharata B Rao 
2626cf632463SBharata B Rao     spapr_del_lmbs(dev, addr, size, &error_abort);
2627cf632463SBharata B Rao out:
2628cf632463SBharata B Rao     error_propagate(errp, local_err);
2629cf632463SBharata B Rao }
2630cf632463SBharata B Rao 
2631af81cf32SBharata B Rao void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2632af81cf32SBharata B Rao                                     sPAPRMachineState *spapr)
2633af81cf32SBharata B Rao {
2634af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2635af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2636af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2637af81cf32SBharata B Rao     void *fdt;
2638af81cf32SBharata B Rao     int offset, fdt_size;
2639af81cf32SBharata B Rao     char *nodename;
2640af81cf32SBharata B Rao 
2641af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2642af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2643af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2644af81cf32SBharata B Rao 
2645af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2646af81cf32SBharata B Rao     g_free(nodename);
2647af81cf32SBharata B Rao 
2648af81cf32SBharata B Rao     *fdt_offset = offset;
2649af81cf32SBharata B Rao     return fdt;
2650af81cf32SBharata B Rao }
2651af81cf32SBharata B Rao 
2652115debf2SIgor Mammedov static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2653115debf2SIgor Mammedov                               Error **errp)
2654ff9006ddSIgor Mammedov {
2655535455fdSIgor Mammedov     MachineState *ms = MACHINE(qdev_get_machine());
2656ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2657535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
2658ff9006ddSIgor Mammedov 
2659535455fdSIgor Mammedov     core_slot->cpu = NULL;
2660ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
2661ff9006ddSIgor Mammedov }
2662ff9006ddSIgor Mammedov 
2663115debf2SIgor Mammedov static void spapr_core_release(DeviceState *dev, void *opaque)
2664115debf2SIgor Mammedov {
2665115debf2SIgor Mammedov     HotplugHandler *hotplug_ctrl;
2666115debf2SIgor Mammedov 
2667115debf2SIgor Mammedov     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2668115debf2SIgor Mammedov     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2669115debf2SIgor Mammedov }
2670115debf2SIgor Mammedov 
2671115debf2SIgor Mammedov static
2672115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
2673ff9006ddSIgor Mammedov                                Error **errp)
2674ff9006ddSIgor Mammedov {
2675535455fdSIgor Mammedov     int index;
2676535455fdSIgor Mammedov     sPAPRDRConnector *drc;
2677ff9006ddSIgor Mammedov     sPAPRDRConnectorClass *drck;
2678ff9006ddSIgor Mammedov     Error *local_err = NULL;
2679535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2680535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
2681ff9006ddSIgor Mammedov 
2682535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
2683535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2684535455fdSIgor Mammedov                    cc->core_id);
2685535455fdSIgor Mammedov         return;
2686535455fdSIgor Mammedov     }
2687ff9006ddSIgor Mammedov     if (index == 0) {
2688ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
2689ff9006ddSIgor Mammedov         return;
2690ff9006ddSIgor Mammedov     }
2691ff9006ddSIgor Mammedov 
2692535455fdSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2693ff9006ddSIgor Mammedov     g_assert(drc);
2694ff9006ddSIgor Mammedov 
2695ff9006ddSIgor Mammedov     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2696ff9006ddSIgor Mammedov     drck->detach(drc, dev, spapr_core_release, NULL, &local_err);
2697ff9006ddSIgor Mammedov     if (local_err) {
2698ff9006ddSIgor Mammedov         error_propagate(errp, local_err);
2699ff9006ddSIgor Mammedov         return;
2700ff9006ddSIgor Mammedov     }
2701ff9006ddSIgor Mammedov 
2702ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
2703ff9006ddSIgor Mammedov }
2704ff9006ddSIgor Mammedov 
2705ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2706ff9006ddSIgor Mammedov                             Error **errp)
2707ff9006ddSIgor Mammedov {
2708ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
2709ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
2710ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
2711ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2712ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
2713ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
2714ff9006ddSIgor Mammedov     Error *local_err = NULL;
2715ff9006ddSIgor Mammedov     void *fdt = NULL;
2716ff9006ddSIgor Mammedov     int fdt_offset = 0;
2717ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
2718535455fdSIgor Mammedov     CPUArchId *core_slot;
2719535455fdSIgor Mammedov     int index;
2720ff9006ddSIgor Mammedov 
2721535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2722535455fdSIgor Mammedov     if (!core_slot) {
2723535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2724535455fdSIgor Mammedov                    cc->core_id);
2725535455fdSIgor Mammedov         return;
2726535455fdSIgor Mammedov     }
2727ff9006ddSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2728ff9006ddSIgor Mammedov 
2729c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
2730ff9006ddSIgor Mammedov 
2731ff9006ddSIgor Mammedov     /*
2732ff9006ddSIgor Mammedov      * Setup CPU DT entries only for hotplugged CPUs. For boot time or
2733ff9006ddSIgor Mammedov      * coldplugged CPUs DT entries are setup in spapr_build_fdt().
2734ff9006ddSIgor Mammedov      */
2735ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2736ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
2737ff9006ddSIgor Mammedov     }
2738ff9006ddSIgor Mammedov 
2739ff9006ddSIgor Mammedov     if (drc) {
2740ff9006ddSIgor Mammedov         sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2741ff9006ddSIgor Mammedov         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, &local_err);
2742ff9006ddSIgor Mammedov         if (local_err) {
2743ff9006ddSIgor Mammedov             g_free(fdt);
2744ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
2745ff9006ddSIgor Mammedov             return;
2746ff9006ddSIgor Mammedov         }
2747ff9006ddSIgor Mammedov     }
2748ff9006ddSIgor Mammedov 
2749ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2750ff9006ddSIgor Mammedov         /*
2751ff9006ddSIgor Mammedov          * Send hotplug notification interrupt to the guest only in case
2752ff9006ddSIgor Mammedov          * of hotplugged CPUs.
2753ff9006ddSIgor Mammedov          */
2754ff9006ddSIgor Mammedov         spapr_hotplug_req_add_by_index(drc);
2755ff9006ddSIgor Mammedov     } else {
2756ff9006ddSIgor Mammedov         /*
2757ff9006ddSIgor Mammedov          * Set the right DRC states for cold plugged CPU.
2758ff9006ddSIgor Mammedov          */
2759ff9006ddSIgor Mammedov         if (drc) {
2760ff9006ddSIgor Mammedov             sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2761ff9006ddSIgor Mammedov             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
2762ff9006ddSIgor Mammedov             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
2763ff9006ddSIgor Mammedov         }
2764ff9006ddSIgor Mammedov     }
2765535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
2766ff9006ddSIgor Mammedov }
2767ff9006ddSIgor Mammedov 
2768ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2769ff9006ddSIgor Mammedov                                 Error **errp)
2770ff9006ddSIgor Mammedov {
2771ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
2772ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
2773ff9006ddSIgor Mammedov     Error *local_err = NULL;
2774ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2775ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
2776ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
2777535455fdSIgor Mammedov     CPUArchId *core_slot;
2778535455fdSIgor Mammedov     int index;
2779ff9006ddSIgor Mammedov 
2780c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
2781ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
2782ff9006ddSIgor Mammedov         goto out;
2783ff9006ddSIgor Mammedov     }
2784ff9006ddSIgor Mammedov 
2785ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
2786ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
2787ff9006ddSIgor Mammedov         goto out;
2788ff9006ddSIgor Mammedov     }
2789ff9006ddSIgor Mammedov 
2790ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
2791ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
2792ff9006ddSIgor Mammedov         goto out;
2793ff9006ddSIgor Mammedov     }
2794ff9006ddSIgor Mammedov 
2795535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2796535455fdSIgor Mammedov     if (!core_slot) {
2797ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
2798ff9006ddSIgor Mammedov         goto out;
2799ff9006ddSIgor Mammedov     }
2800ff9006ddSIgor Mammedov 
2801535455fdSIgor Mammedov     if (core_slot->cpu) {
2802ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
2803ff9006ddSIgor Mammedov         goto out;
2804ff9006ddSIgor Mammedov     }
2805ff9006ddSIgor Mammedov 
2806ff9006ddSIgor Mammedov out:
2807ff9006ddSIgor Mammedov     g_free(base_core_type);
2808ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
2809ff9006ddSIgor Mammedov }
2810ff9006ddSIgor Mammedov 
2811c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2812c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2813c20d332aSBharata B Rao {
2814c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2815c20d332aSBharata B Rao 
2816c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2817b556854bSBharata B Rao         int node;
2818c20d332aSBharata B Rao 
2819c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2820c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2821c20d332aSBharata B Rao             return;
2822c20d332aSBharata B Rao         }
2823c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2824c20d332aSBharata B Rao         if (*errp) {
2825c20d332aSBharata B Rao             return;
2826c20d332aSBharata B Rao         }
28271a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
28281a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
28291a5512bbSGonglei             return;
28301a5512bbSGonglei         }
2831c20d332aSBharata B Rao 
2832b556854bSBharata B Rao         /*
2833b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2834b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2835b556854bSBharata B Rao          * to the first node that has some memory. This causes two
2836b556854bSBharata B Rao          * unexpected behaviours for the user.
2837b556854bSBharata B Rao          *
2838b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
2839b556854bSBharata B Rao          *   specified.
2840b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2841b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
2842b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
2843b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
2844b556854bSBharata B Rao          *
2845b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
2846b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
2847b556854bSBharata B Rao          */
2848b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
2849b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
2850b556854bSBharata B Rao                        node);
2851b556854bSBharata B Rao             return;
2852b556854bSBharata B Rao         }
2853b556854bSBharata B Rao 
2854c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
2855af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2856af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
2857c20d332aSBharata B Rao     }
2858c20d332aSBharata B Rao }
2859c20d332aSBharata B Rao 
2860c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2861c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2862c20d332aSBharata B Rao {
2863cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
28643c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
28656f4b5c3eSBharata B Rao 
2866c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2867cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2868cf632463SBharata B Rao             spapr_memory_unplug(hotplug_dev, dev, errp);
2869cf632463SBharata B Rao         } else {
2870cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2871cf632463SBharata B Rao         }
2872cf632463SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2873c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
2874cf632463SBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
2875cf632463SBharata B Rao             return;
2876cf632463SBharata B Rao         }
2877cf632463SBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
2878cf632463SBharata B Rao     }
2879cf632463SBharata B Rao }
2880cf632463SBharata B Rao 
2881cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
2882cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
2883cf632463SBharata B Rao {
2884cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
2885cf632463SBharata B Rao     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
2886cf632463SBharata B Rao 
2887cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2888cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2889cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
2890cf632463SBharata B Rao         } else {
2891cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
2892cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
2893cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
2894cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
2895cf632463SBharata B Rao              * eventually handled by the guest after boot
2896cf632463SBharata B Rao              */
2897cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2898cf632463SBharata B Rao         }
28996f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2900c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
29016f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
29026f4b5c3eSBharata B Rao             return;
29036f4b5c3eSBharata B Rao         }
2904115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
2905c20d332aSBharata B Rao     }
2906c20d332aSBharata B Rao }
2907c20d332aSBharata B Rao 
290894a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
290994a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
291094a94e4cSBharata B Rao {
291194a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
291294a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
291394a94e4cSBharata B Rao     }
291494a94e4cSBharata B Rao }
291594a94e4cSBharata B Rao 
29167ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
2917c20d332aSBharata B Rao                                                  DeviceState *dev)
2918c20d332aSBharata B Rao {
291994a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
292094a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2921c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
2922c20d332aSBharata B Rao     }
2923c20d332aSBharata B Rao     return NULL;
2924c20d332aSBharata B Rao }
2925c20d332aSBharata B Rao 
292620bb648dSDavid Gibson static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
292720bb648dSDavid Gibson {
292820bb648dSDavid Gibson     /* Allocate to NUMA nodes on a "socket" basis (not that concept of
292920bb648dSDavid Gibson      * socket means much for the paravirtualized PAPR platform) */
293020bb648dSDavid Gibson     return cpu_index / smp_threads / smp_cores;
293120bb648dSDavid Gibson }
293220bb648dSDavid Gibson 
2933535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
2934535455fdSIgor Mammedov {
2935535455fdSIgor Mammedov     int i;
2936535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
2937535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
2938535455fdSIgor Mammedov 
2939c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
2940535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
2941535455fdSIgor Mammedov     }
2942535455fdSIgor Mammedov     if (machine->possible_cpus) {
2943535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
2944535455fdSIgor Mammedov         return machine->possible_cpus;
2945535455fdSIgor Mammedov     }
2946535455fdSIgor Mammedov 
2947535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2948535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
2949535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
2950535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
2951535455fdSIgor Mammedov         int core_id = i * smp_threads;
2952535455fdSIgor Mammedov 
2953f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
2954535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
2955535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
2956535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
2957535455fdSIgor Mammedov         /* TODO: add 'has_node/node' here to describe
2958535455fdSIgor Mammedov            to which node core belongs */
2959535455fdSIgor Mammedov     }
2960535455fdSIgor Mammedov     return machine->possible_cpus;
2961535455fdSIgor Mammedov }
2962535455fdSIgor Mammedov 
29636737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
2964daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
2965daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
29666737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
29676737d9adSDavid Gibson {
2968357d1e3bSDavid Gibson     /*
2969357d1e3bSDavid Gibson      * New-style PHB window placement.
2970357d1e3bSDavid Gibson      *
2971357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
2972357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
2973357d1e3bSDavid Gibson      * windows.
2974357d1e3bSDavid Gibson      *
2975357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
2976357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
2977357d1e3bSDavid Gibson      *
2978357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
2979357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
2980357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
2981357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
2982357d1e3bSDavid Gibson      */
29836737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
298425e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
298525e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
29866737d9adSDavid Gibson     int i;
29876737d9adSDavid Gibson 
2988357d1e3bSDavid Gibson     /* Sanity check natural alignments */
2989357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2990357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
2991357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
2992357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
2993357d1e3bSDavid Gibson     /* Sanity check bounds */
299425e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
299525e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
299625e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
299725e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
29982efff1c0SDavid Gibson 
299925e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
300025e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
300125e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
30026737d9adSDavid Gibson         return;
30036737d9adSDavid Gibson     }
30046737d9adSDavid Gibson 
30056737d9adSDavid Gibson     *buid = base_buid + index;
30066737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
30076737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
30086737d9adSDavid Gibson     }
30096737d9adSDavid Gibson 
3010357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
3011357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
3012357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
30136737d9adSDavid Gibson }
30146737d9adSDavid Gibson 
30157844e12bSCédric Le Goater static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
30167844e12bSCédric Le Goater {
30177844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
30187844e12bSCédric Le Goater 
30197844e12bSCédric Le Goater     return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
30207844e12bSCédric Le Goater }
30217844e12bSCédric Le Goater 
30227844e12bSCédric Le Goater static void spapr_ics_resend(XICSFabric *dev)
30237844e12bSCédric Le Goater {
30247844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
30257844e12bSCédric Le Goater 
30267844e12bSCédric Le Goater     ics_resend(spapr->ics);
30277844e12bSCédric Le Goater }
30287844e12bSCédric Le Goater 
3029b2fc59aaSCédric Le Goater static ICPState *spapr_icp_get(XICSFabric *xi, int server)
3030b2fc59aaSCédric Le Goater {
3031b2fc59aaSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
3032b2fc59aaSCédric Le Goater 
3033852ad27eSCédric Le Goater     return (server < spapr->nr_servers) ? &spapr->icps[server] : NULL;
3034b2fc59aaSCédric Le Goater }
3035b2fc59aaSCédric Le Goater 
30366449da45SCédric Le Goater static void spapr_pic_print_info(InterruptStatsProvider *obj,
30376449da45SCédric Le Goater                                  Monitor *mon)
30386449da45SCédric Le Goater {
30396449da45SCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
30406449da45SCédric Le Goater     int i;
30416449da45SCédric Le Goater 
30426449da45SCédric Le Goater     for (i = 0; i < spapr->nr_servers; i++) {
30436449da45SCédric Le Goater         icp_pic_print_info(&spapr->icps[i], mon);
30446449da45SCédric Le Goater     }
30456449da45SCédric Le Goater 
30466449da45SCédric Le Goater     ics_pic_print_info(spapr->ics, mon);
30476449da45SCédric Le Goater }
30486449da45SCédric Le Goater 
304929ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
305053018216SPaolo Bonzini {
305129ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
3052224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
305371461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
305434316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
3055c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
30561d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
30577844e12bSCédric Le Goater     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
30586449da45SCédric Le Goater     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
305929ee3247SAlexey Kardashevskiy 
30600eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
3061fc9f38c3SDavid Gibson 
3062fc9f38c3SDavid Gibson     /*
3063fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
3064fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
3065fc9f38c3SDavid Gibson      * these details for backwards compatibility
3066fc9f38c3SDavid Gibson      */
3067958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
3068958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
3069958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
30706244bb7eSGreg Kurz     mc->max_cpus = 1024;
3071958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
30725b2128d2SAlexander Graf     mc->default_boot_order = "";
3073a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
3074958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
30759e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
3076e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
30777ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
307894a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
3079c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
3080c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
308120bb648dSDavid Gibson     mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
3082535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3083cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
308400b4fbe2SMarcel Apfelbaum 
3085fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
30863daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
3087c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
308871461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
308934316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
30906737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
30911d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3092e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3093e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3094e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3095e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
30969861bb3eSSuraj Jitindar Singh     vhc->get_patbe = spapr_get_patbe;
30977844e12bSCédric Le Goater     xic->ics_get = spapr_ics_get;
30987844e12bSCédric Le Goater     xic->ics_resend = spapr_ics_resend;
3099b2fc59aaSCédric Le Goater     xic->icp_get = spapr_icp_get;
31006449da45SCédric Le Goater     ispc->print_info = spapr_pic_print_info;
310153018216SPaolo Bonzini }
310253018216SPaolo Bonzini 
310329ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
310429ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
310529ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
31064aee7362SDavid Gibson     .abstract      = true,
31076ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
310823825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
310987bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3110183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
311129ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
311271461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
311371461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
311434316482SAlexey Kardashevskiy         { TYPE_NMI },
3115c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
31161d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
31177844e12bSCédric Le Goater         { TYPE_XICS_FABRIC },
31186449da45SCédric Le Goater         { TYPE_INTERRUPT_STATS_PROVIDER },
311971461b0fSAlexey Kardashevskiy         { }
312071461b0fSAlexey Kardashevskiy     },
312129ee3247SAlexey Kardashevskiy };
312229ee3247SAlexey Kardashevskiy 
3123fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
31245013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
31255013c547SDavid Gibson                                                     void *data)      \
31265013c547SDavid Gibson     {                                                                \
31275013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
31285013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3129fccbc785SDavid Gibson         if (latest) {                                                \
3130fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3131fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3132fccbc785SDavid Gibson         }                                                            \
31335013c547SDavid Gibson     }                                                                \
31345013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
31355013c547SDavid Gibson     {                                                                \
31365013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
31375013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
31385013c547SDavid Gibson     }                                                                \
31395013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
31405013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
31415013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
31425013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
31435013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
31445013c547SDavid Gibson     };                                                               \
31455013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
31465013c547SDavid Gibson     {                                                                \
31475013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
31485013c547SDavid Gibson     }                                                                \
31490e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
31505013c547SDavid Gibson 
31511c5f29bbSDavid Gibson /*
3152fa325e6cSDavid Gibson  * pseries-2.9
3153db800b21SDavid Gibson  */
3154fa325e6cSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
3155db800b21SDavid Gibson {
3156db800b21SDavid Gibson }
3157db800b21SDavid Gibson 
3158fa325e6cSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
3159db800b21SDavid Gibson {
3160db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
3161db800b21SDavid Gibson }
3162db800b21SDavid Gibson 
3163fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", true);
3164fa325e6cSDavid Gibson 
3165fa325e6cSDavid Gibson /*
3166fa325e6cSDavid Gibson  * pseries-2.8
3167fa325e6cSDavid Gibson  */
3168fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                            \
3169fa325e6cSDavid Gibson     HW_COMPAT_2_8
3170fa325e6cSDavid Gibson 
3171fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
3172fa325e6cSDavid Gibson {
3173fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
3174fa325e6cSDavid Gibson }
3175fa325e6cSDavid Gibson 
3176fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
3177fa325e6cSDavid Gibson {
3178fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
3179fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
3180fa325e6cSDavid Gibson }
3181fa325e6cSDavid Gibson 
3182fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
3183db800b21SDavid Gibson 
3184db800b21SDavid Gibson /*
31851ea1eefcSBharata B Rao  * pseries-2.7
31861ea1eefcSBharata B Rao  */
3187db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3188db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3189357d1e3bSDavid Gibson     {                                               \
3190357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3191357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3192357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3193357d1e3bSDavid Gibson     },                                              \
3194357d1e3bSDavid Gibson     {                                               \
3195357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3196357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3197357d1e3bSDavid Gibson         .value    = "0",                            \
3198146c11f1SDavid Gibson     },                                              \
3199146c11f1SDavid Gibson     {                                               \
3200146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3201146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3202146c11f1SDavid Gibson         .value    = "on",                           \
32035c4537bdSDavid Gibson     },                                              \
32045c4537bdSDavid Gibson     {                                               \
32055c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
32065c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
32075c4537bdSDavid Gibson         .value    = "on",                           \
3208357d1e3bSDavid Gibson     },
3209357d1e3bSDavid Gibson 
3210357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3211357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3212357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3213357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3214357d1e3bSDavid Gibson {
3215357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3216357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3217357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3218357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3219357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3220357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3221357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3222357d1e3bSDavid Gibson 
3223357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3224357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3225357d1e3bSDavid Gibson     int i;
3226357d1e3bSDavid Gibson 
3227357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3228357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3229357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3230357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3231357d1e3bSDavid Gibson          * regions */
3232357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3233357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3234357d1e3bSDavid Gibson     }
3235357d1e3bSDavid Gibson 
3236357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3237357d1e3bSDavid Gibson 
3238357d1e3bSDavid Gibson     if (index > max_index) {
3239357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3240357d1e3bSDavid Gibson                    max_index);
3241357d1e3bSDavid Gibson         return;
3242357d1e3bSDavid Gibson     }
3243357d1e3bSDavid Gibson 
3244357d1e3bSDavid Gibson     *buid = base_buid + index;
3245357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3246357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3247357d1e3bSDavid Gibson     }
3248357d1e3bSDavid Gibson 
3249357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3250357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3251357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3252357d1e3bSDavid Gibson     /*
3253357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3254357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3255357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3256357d1e3bSDavid Gibson      */
3257357d1e3bSDavid Gibson }
3258db800b21SDavid Gibson 
32591ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
32601ea1eefcSBharata B Rao {
3261f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3262f6229214SMichael Roth 
3263672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3264f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
32651ea1eefcSBharata B Rao }
32661ea1eefcSBharata B Rao 
32671ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
32681ea1eefcSBharata B Rao {
32693daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
32703daa4a9fSThomas Huth 
3271db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
32723daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3273db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3274357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
32751ea1eefcSBharata B Rao }
32761ea1eefcSBharata B Rao 
3277db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
32781ea1eefcSBharata B Rao 
32791ea1eefcSBharata B Rao /*
32804b23699cSDavid Gibson  * pseries-2.6
32814b23699cSDavid Gibson  */
32821ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3283ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3284ae4de14cSAlexey Kardashevskiy     { \
3285ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3286ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3287ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3288ae4de14cSAlexey Kardashevskiy     },
32891ea1eefcSBharata B Rao 
32904b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
32914b23699cSDavid Gibson {
3292672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
32934b23699cSDavid Gibson }
32944b23699cSDavid Gibson 
32954b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
32964b23699cSDavid Gibson {
32971ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3298c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
32991ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
33004b23699cSDavid Gibson }
33014b23699cSDavid Gibson 
33021ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
33034b23699cSDavid Gibson 
33044b23699cSDavid Gibson /*
33051c5f29bbSDavid Gibson  * pseries-2.5
33061c5f29bbSDavid Gibson  */
33074b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
330857c522f4SThomas Huth     HW_COMPAT_2_5 \
330957c522f4SThomas Huth     { \
331057c522f4SThomas Huth         .driver   = "spapr-vlan", \
331157c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
331257c522f4SThomas Huth         .value    = "off", \
331357c522f4SThomas Huth     },
33144b23699cSDavid Gibson 
33155013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
33161c5f29bbSDavid Gibson {
3317672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
33185013c547SDavid Gibson }
33195013c547SDavid Gibson 
33205013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
33215013c547SDavid Gibson {
332257040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
332357040d45SThomas Huth 
33244b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
332557040d45SThomas Huth     smc->use_ohci_by_default = true;
33264b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
33271c5f29bbSDavid Gibson }
33281c5f29bbSDavid Gibson 
33294b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
33301c5f29bbSDavid Gibson 
33311c5f29bbSDavid Gibson /*
33321c5f29bbSDavid Gibson  * pseries-2.4
33331c5f29bbSDavid Gibson  */
333480fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
333580fd50f9SCornelia Huck         HW_COMPAT_2_4
333680fd50f9SCornelia Huck 
33375013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
33381c5f29bbSDavid Gibson {
33395013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
33405013c547SDavid Gibson }
33411c5f29bbSDavid Gibson 
33425013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
33435013c547SDavid Gibson {
3344fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3345fc9f38c3SDavid Gibson 
3346fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3347fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3348f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
33491c5f29bbSDavid Gibson }
33501c5f29bbSDavid Gibson 
3351fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
33521c5f29bbSDavid Gibson 
33531c5f29bbSDavid Gibson /*
33541c5f29bbSDavid Gibson  * pseries-2.3
33551c5f29bbSDavid Gibson  */
335638ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
33577619c7b0SMichael Roth         HW_COMPAT_2_3 \
33587619c7b0SMichael Roth         {\
33597619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
33607619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
33617619c7b0SMichael Roth             .value    = "off",\
33627619c7b0SMichael Roth         },
336338ff32c6SEduardo Habkost 
33645013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
33651c5f29bbSDavid Gibson {
33665013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
33671c5f29bbSDavid Gibson     savevm_skip_section_footers();
33681c5f29bbSDavid Gibson     global_state_set_optional();
336909b5e30dSGreg Kurz     savevm_skip_configuration();
33701c5f29bbSDavid Gibson }
33711c5f29bbSDavid Gibson 
33725013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
33731c5f29bbSDavid Gibson {
3374fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3375f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
33761c5f29bbSDavid Gibson }
3377fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
33781c5f29bbSDavid Gibson 
33791c5f29bbSDavid Gibson /*
33801c5f29bbSDavid Gibson  * pseries-2.2
33811c5f29bbSDavid Gibson  */
33821c5f29bbSDavid Gibson 
3383b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
33844dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3385b194df47SAlexey Kardashevskiy         {\
3386b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3387b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3388b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3389dd754bafSEduardo Habkost         },
3390b194df47SAlexey Kardashevskiy 
33915013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3392b0e966d0SJason Wang {
33935013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3394cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3395b0e966d0SJason Wang }
3396b0e966d0SJason Wang 
33975013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3398b0e966d0SJason Wang {
3399fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3400f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
34011c5f29bbSDavid Gibson }
3402fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
34031c5f29bbSDavid Gibson 
34041c5f29bbSDavid Gibson /*
34051c5f29bbSDavid Gibson  * pseries-2.1
34061c5f29bbSDavid Gibson  */
34071c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
34081c5f29bbSDavid Gibson         HW_COMPAT_2_1
34091c5f29bbSDavid Gibson 
34105013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
34111c5f29bbSDavid Gibson {
34125013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
34131c5f29bbSDavid Gibson }
34141c5f29bbSDavid Gibson 
34155013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
3416b0e966d0SJason Wang {
3417fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
3418f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
34196026db45SAlexey Kardashevskiy }
3420fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
34216026db45SAlexey Kardashevskiy 
342229ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
342329ee3247SAlexey Kardashevskiy {
342429ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
342529ee3247SAlexey Kardashevskiy }
342629ee3247SAlexey Kardashevskiy 
342729ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
3428