xref: /openbmc/qemu/hw/ppc/spapr.c (revision 15f8b142)
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"
43b4db5413SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
443794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4553018216SPaolo Bonzini 
4653018216SPaolo Bonzini #include "hw/boards.h"
470d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
4853018216SPaolo Bonzini #include "hw/loader.h"
4953018216SPaolo Bonzini 
507804c353SCédric Le Goater #include "hw/ppc/fdt.h"
510d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
520d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
530d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
540d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5553018216SPaolo Bonzini #include "hw/pci/msi.h"
5653018216SPaolo Bonzini 
5753018216SPaolo Bonzini #include "hw/pci/pci.h"
5871461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
5971461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
6053018216SPaolo Bonzini 
6153018216SPaolo Bonzini #include "exec/address-spaces.h"
6253018216SPaolo Bonzini #include "hw/usb.h"
6353018216SPaolo Bonzini #include "qemu/config-file.h"
64135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
652a6593cbSAlexey Kardashevskiy #include "trace.h"
6634316482SAlexey Kardashevskiy #include "hw/nmi.h"
676449da45SCédric Le Goater #include "hw/intc/intc.h"
6853018216SPaolo Bonzini 
6968a27b20SMichael S. Tsirkin #include "hw/compat.h"
70f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7194a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
722474bfd4SIgor Mammedov #include "qmp-commands.h"
7368a27b20SMichael S. Tsirkin 
7453018216SPaolo Bonzini #include <libfdt.h>
7553018216SPaolo Bonzini 
7653018216SPaolo Bonzini /* SLOF memory layout:
7753018216SPaolo Bonzini  *
7853018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
7953018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
8053018216SPaolo Bonzini  *
8153018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8253018216SPaolo Bonzini  * and more
8353018216SPaolo Bonzini  *
8453018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8553018216SPaolo Bonzini  */
8638b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
8753018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
88b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
8953018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
9053018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9153018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9253018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9353018216SPaolo Bonzini 
9453018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9553018216SPaolo Bonzini 
9653018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9753018216SPaolo Bonzini 
9853018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9953018216SPaolo Bonzini 
10071cd4dacSCédric Le Goater static ICSState *spapr_ics_create(sPAPRMachineState *spapr,
10171cd4dacSCédric Le Goater                                   const char *type_ics,
102817bb6a4SCédric Le Goater                                   int nr_irqs, Error **errp)
103c04d6cfaSAnthony Liguori {
104175d2aa0SGreg Kurz     Error *local_err = NULL;
10571cd4dacSCédric Le Goater     Object *obj;
106c04d6cfaSAnthony Liguori 
10771cd4dacSCédric Le Goater     obj = object_new(type_ics);
108175d2aa0SGreg Kurz     object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort);
10971cd4dacSCédric Le Goater     object_property_add_const_link(obj, "xics", OBJECT(spapr), &error_abort);
110175d2aa0SGreg Kurz     object_property_set_int(obj, nr_irqs, "nr-irqs", &local_err);
111175d2aa0SGreg Kurz     if (local_err) {
112175d2aa0SGreg Kurz         goto error;
113175d2aa0SGreg Kurz     }
11471cd4dacSCédric Le Goater     object_property_set_bool(obj, true, "realized", &local_err);
115175d2aa0SGreg Kurz     if (local_err) {
116175d2aa0SGreg Kurz         goto error;
117c04d6cfaSAnthony Liguori     }
118c04d6cfaSAnthony Liguori 
11971cd4dacSCédric Le Goater     return ICS_SIMPLE(obj);
120175d2aa0SGreg Kurz 
121175d2aa0SGreg Kurz error:
122175d2aa0SGreg Kurz     error_propagate(errp, local_err);
123175d2aa0SGreg Kurz     return NULL;
1245bc8d26dSCédric Le Goater }
1255bc8d26dSCédric Le Goater 
12671cd4dacSCédric Le Goater static void xics_system_init(MachineState *machine, int nr_irqs, Error **errp)
127c04d6cfaSAnthony Liguori {
12871cd4dacSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
129c04d6cfaSAnthony Liguori 
13011ad93f6SDavid Gibson     if (kvm_enabled()) {
1312192a930SCédric Le Goater         if (machine_kernel_irqchip_allowed(machine) &&
13271cd4dacSCédric Le Goater             !xics_kvm_init(spapr, errp)) {
13371cd4dacSCédric Le Goater             spapr->icp_type = TYPE_KVM_ICP;
1343d85885aSGreg Kurz             spapr->ics = spapr_ics_create(spapr, TYPE_ICS_KVM, nr_irqs, errp);
13511ad93f6SDavid Gibson         }
13671cd4dacSCédric Le Goater         if (machine_kernel_irqchip_required(machine) && !spapr->ics) {
1373d85885aSGreg Kurz             error_prepend(errp, "kernel_irqchip requested but unavailable: ");
1383d85885aSGreg Kurz             return;
13911ad93f6SDavid Gibson         }
140b83baa60SMarkus Armbruster     }
14111ad93f6SDavid Gibson 
14271cd4dacSCédric Le Goater     if (!spapr->ics) {
143f63ebfe0SGreg Kurz         xics_spapr_init(spapr);
14471cd4dacSCédric Le Goater         spapr->icp_type = TYPE_ICP;
14571cd4dacSCédric Le Goater         spapr->ics = spapr_ics_create(spapr, TYPE_ICS_SIMPLE, nr_irqs, errp);
1463d85885aSGreg Kurz         if (!spapr->ics) {
1473d85885aSGreg Kurz             return;
1483d85885aSGreg Kurz         }
149c04d6cfaSAnthony Liguori     }
150c04d6cfaSAnthony Liguori }
151c04d6cfaSAnthony Liguori 
152833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
153833d4668SAlexey Kardashevskiy                                   int smt_threads)
154833d4668SAlexey Kardashevskiy {
155833d4668SAlexey Kardashevskiy     int i, ret = 0;
156833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
157833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
158833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
159833d4668SAlexey Kardashevskiy 
160d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
161d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
1626d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1636d9412eaSAlexey Kardashevskiy             return ret;
1646d9412eaSAlexey Kardashevskiy         }
1656d9412eaSAlexey Kardashevskiy     }
1666d9412eaSAlexey Kardashevskiy 
167833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
168833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
169833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
170833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
171833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
172833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
173833d4668SAlexey Kardashevskiy     }
174833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
175833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
176833d4668SAlexey Kardashevskiy     if (ret < 0) {
177833d4668SAlexey Kardashevskiy         return ret;
178833d4668SAlexey Kardashevskiy     }
179833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
180833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
181833d4668SAlexey Kardashevskiy 
182833d4668SAlexey Kardashevskiy     return ret;
183833d4668SAlexey Kardashevskiy }
184833d4668SAlexey Kardashevskiy 
1850da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
1860da6f3feSBharata B Rao {
1870da6f3feSBharata B Rao     int ret = 0;
1880da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
1890da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
1900da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
1910da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1920da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1930da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
194*15f8b142SIgor Mammedov                                 cpu_to_be32(cpu->node_id),
1950da6f3feSBharata B Rao                                 cpu_to_be32(index)};
1960da6f3feSBharata B Rao 
1970da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
1980da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
1990da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
2000da6f3feSBharata B Rao                           sizeof(associativity));
2010da6f3feSBharata B Rao     }
2020da6f3feSBharata B Rao 
2030da6f3feSBharata B Rao     return ret;
2040da6f3feSBharata B Rao }
2050da6f3feSBharata B Rao 
20686d5771aSSam Bobroff /* Populate the "ibm,pa-features" property */
207e957f6a9SSam Bobroff static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset,
208e957f6a9SSam Bobroff                                       bool legacy_guest)
20986d5771aSSam Bobroff {
21086d5771aSSam Bobroff     uint8_t pa_features_206[] = { 6, 0,
21186d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
21286d5771aSSam Bobroff     uint8_t pa_features_207[] = { 24, 0,
21386d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
21486d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
21586d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
21686d5771aSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
2179fb4541fSSam Bobroff     uint8_t pa_features_300[] = { 66, 0,
2189fb4541fSSam Bobroff         /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
2199fb4541fSSam Bobroff         /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
22086d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
2219fb4541fSSam Bobroff         /* 6: DS207 */
22286d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
2239fb4541fSSam Bobroff         /* 16: Vector */
22486d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
2259fb4541fSSam Bobroff         /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
2269bf502feSDavid Gibson         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
2279fb4541fSSam Bobroff         /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
2289fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
2299fb4541fSSam Bobroff         /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
2309fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
2319fb4541fSSam Bobroff         /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
2329fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
2339fb4541fSSam Bobroff         /* 42: PM, 44: PC RA, 46: SC vec'd */
2349fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
2359fb4541fSSam Bobroff         /* 48: SIMD, 50: QP BFP, 52: String */
2369fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
2379fb4541fSSam Bobroff         /* 54: DecFP, 56: DecI, 58: SHA */
2389fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
2399fb4541fSSam Bobroff         /* 60: NM atomic, 62: RNG */
2409fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
2419fb4541fSSam Bobroff     };
24286d5771aSSam Bobroff     uint8_t *pa_features;
24386d5771aSSam Bobroff     size_t pa_size;
24486d5771aSSam Bobroff 
24586d5771aSSam Bobroff     switch (POWERPC_MMU_VER(env->mmu_model)) {
24686d5771aSSam Bobroff     case POWERPC_MMU_VER_2_06:
24786d5771aSSam Bobroff         pa_features = pa_features_206;
24886d5771aSSam Bobroff         pa_size = sizeof(pa_features_206);
24986d5771aSSam Bobroff         break;
25086d5771aSSam Bobroff     case POWERPC_MMU_VER_2_07:
25186d5771aSSam Bobroff         pa_features = pa_features_207;
25286d5771aSSam Bobroff         pa_size = sizeof(pa_features_207);
25386d5771aSSam Bobroff         break;
25486d5771aSSam Bobroff     case POWERPC_MMU_VER_3_00:
25586d5771aSSam Bobroff         pa_features = pa_features_300;
25686d5771aSSam Bobroff         pa_size = sizeof(pa_features_300);
25786d5771aSSam Bobroff         break;
25886d5771aSSam Bobroff     default:
25986d5771aSSam Bobroff         return;
26086d5771aSSam Bobroff     }
26186d5771aSSam Bobroff 
26286d5771aSSam Bobroff     if (env->ci_large_pages) {
26386d5771aSSam Bobroff         /*
26486d5771aSSam Bobroff          * Note: we keep CI large pages off by default because a 64K capable
26586d5771aSSam Bobroff          * guest provisioned with large pages might otherwise try to map a qemu
26686d5771aSSam Bobroff          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
26786d5771aSSam Bobroff          * even if that qemu runs on a 4k host.
26886d5771aSSam Bobroff          * We dd this bit back here if we are confident this is not an issue
26986d5771aSSam Bobroff          */
27086d5771aSSam Bobroff         pa_features[3] |= 0x20;
27186d5771aSSam Bobroff     }
27286d5771aSSam Bobroff     if (kvmppc_has_cap_htm() && pa_size > 24) {
27386d5771aSSam Bobroff         pa_features[24] |= 0x80;    /* Transactional memory support */
27486d5771aSSam Bobroff     }
275e957f6a9SSam Bobroff     if (legacy_guest && pa_size > 40) {
276e957f6a9SSam Bobroff         /* Workaround for broken kernels that attempt (guest) radix
277e957f6a9SSam Bobroff          * mode when they can't handle it, if they see the radix bit set
278e957f6a9SSam Bobroff          * in pa-features. So hide it from them. */
279e957f6a9SSam Bobroff         pa_features[40 + 2] &= ~0x80; /* Radix MMU */
280e957f6a9SSam Bobroff     }
28186d5771aSSam Bobroff 
28286d5771aSSam Bobroff     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
28386d5771aSSam Bobroff }
28486d5771aSSam Bobroff 
28528e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
28653018216SPaolo Bonzini {
28782677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
28882677ed2SAlexey Kardashevskiy     CPUState *cs;
28953018216SPaolo Bonzini     char cpu_model[32];
29053018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
29153018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
29253018216SPaolo Bonzini 
29382677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
29482677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
295e957f6a9SSam Bobroff         CPUPPCState *env = &cpu->env;
29682677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
29782677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
29812dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
29953018216SPaolo Bonzini 
3000f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
30153018216SPaolo Bonzini             continue;
30253018216SPaolo Bonzini         }
30353018216SPaolo Bonzini 
30482677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
30553018216SPaolo Bonzini 
30682677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
30782677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
30882677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
30982677ed2SAlexey Kardashevskiy                                           "cpus");
31082677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
31182677ed2SAlexey Kardashevskiy                 return cpus_offset;
31282677ed2SAlexey Kardashevskiy             }
31382677ed2SAlexey Kardashevskiy         }
31482677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
31582677ed2SAlexey Kardashevskiy         if (offset < 0) {
31682677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
31753018216SPaolo Bonzini             if (offset < 0) {
31853018216SPaolo Bonzini                 return offset;
31953018216SPaolo Bonzini             }
32082677ed2SAlexey Kardashevskiy         }
32153018216SPaolo Bonzini 
3220da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
3230da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
32453018216SPaolo Bonzini         if (ret < 0) {
32553018216SPaolo Bonzini             return ret;
32653018216SPaolo Bonzini         }
32753018216SPaolo Bonzini 
3280da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
32953018216SPaolo Bonzini         if (ret < 0) {
33053018216SPaolo Bonzini             return ret;
33153018216SPaolo Bonzini         }
332833d4668SAlexey Kardashevskiy 
33312dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
334833d4668SAlexey Kardashevskiy         if (ret < 0) {
335833d4668SAlexey Kardashevskiy             return ret;
336833d4668SAlexey Kardashevskiy         }
337e957f6a9SSam Bobroff 
338e957f6a9SSam Bobroff         spapr_populate_pa_features(env, fdt, offset,
339e957f6a9SSam Bobroff                                          spapr->cas_legacy_guest_workaround);
34053018216SPaolo Bonzini     }
34153018216SPaolo Bonzini     return ret;
34253018216SPaolo Bonzini }
34353018216SPaolo Bonzini 
344b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
345b082d65aSAlexey Kardashevskiy {
346fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
347fb164994SDavid Gibson 
348b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
349b082d65aSAlexey Kardashevskiy         int i;
350b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
351b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
352fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
353fb164994SDavid Gibson                            machine->ram_size);
354b082d65aSAlexey Kardashevskiy             }
355b082d65aSAlexey Kardashevskiy         }
356b082d65aSAlexey Kardashevskiy     }
357fb164994SDavid Gibson     return machine->ram_size;
358b082d65aSAlexey Kardashevskiy }
359b082d65aSAlexey Kardashevskiy 
360a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
361a1d59c0fSAlexey Kardashevskiy {
362a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
363a1d59c0fSAlexey Kardashevskiy }
36453018216SPaolo Bonzini 
36503d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
36626a8c353SAlexey Kardashevskiy                                        hwaddr size)
36726a8c353SAlexey Kardashevskiy {
36826a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
36926a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
37026a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
371c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
37226a8c353SAlexey Kardashevskiy     };
37326a8c353SAlexey Kardashevskiy     char mem_name[32];
37426a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
37526a8c353SAlexey Kardashevskiy     int off;
37626a8c353SAlexey Kardashevskiy 
37726a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
37826a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
37926a8c353SAlexey Kardashevskiy 
38026a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
38126a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
38226a8c353SAlexey Kardashevskiy     _FDT(off);
38326a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
38426a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
38526a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
38626a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
38726a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
38803d196b7SBharata B Rao     return off;
38926a8c353SAlexey Kardashevskiy }
39026a8c353SAlexey Kardashevskiy 
39128e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
39253018216SPaolo Bonzini {
393fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
3947db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
3957db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
3967db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
3977db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
39853018216SPaolo Bonzini 
3997db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
4007db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
4017db8a127SAlexey Kardashevskiy         nb_nodes = 1;
402fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
4037db8a127SAlexey Kardashevskiy         nodes = &ramnode;
4045fe269b1SPaul Mackerras     }
40553018216SPaolo Bonzini 
4067db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
4077db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
4087db8a127SAlexey Kardashevskiy             continue;
40953018216SPaolo Bonzini         }
410fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
4115fe269b1SPaul Mackerras             node_size = 0;
4125fe269b1SPaul Mackerras         } else {
4137db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
414fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
415fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
4165fe269b1SPaul Mackerras             }
4175fe269b1SPaul Mackerras         }
4187db8a127SAlexey Kardashevskiy         if (!mem_start) {
4197db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
420e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
4217db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
4227db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
4237db8a127SAlexey Kardashevskiy         }
4246010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
4256010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
4266010818cSAlexey Kardashevskiy 
4276010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
4286010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
4296010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
4306010818cSAlexey Kardashevskiy             }
4316010818cSAlexey Kardashevskiy 
4326010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
4336010818cSAlexey Kardashevskiy             node_size -= sizetmp;
4346010818cSAlexey Kardashevskiy             mem_start += sizetmp;
4356010818cSAlexey Kardashevskiy         }
43653018216SPaolo Bonzini     }
43753018216SPaolo Bonzini 
43853018216SPaolo Bonzini     return 0;
43953018216SPaolo Bonzini }
44053018216SPaolo Bonzini 
4410da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4420da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4430da6f3feSBharata B Rao {
4440da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4450da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4460da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4470da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
4480da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4490da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
450afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
451afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
4520da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
4530da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
4540da6f3feSBharata B Rao     size_t page_sizes_prop_size;
45522419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
4560da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
45712dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
458af81cf32SBharata B Rao     sPAPRDRConnector *drc;
459af81cf32SBharata B Rao     sPAPRDRConnectorClass *drck;
460af81cf32SBharata B Rao     int drc_index;
461c64abd1fSSam Bobroff     uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
462c64abd1fSSam Bobroff     int i;
463af81cf32SBharata B Rao 
464af81cf32SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
465af81cf32SBharata B Rao     if (drc) {
466af81cf32SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
467af81cf32SBharata B Rao         drc_index = drck->get_index(drc);
468af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
469af81cf32SBharata B Rao     }
4700da6f3feSBharata B Rao 
4710da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
4720da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
4730da6f3feSBharata B Rao 
4740da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
4750da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
4760da6f3feSBharata B Rao                            env->dcache_line_size)));
4770da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
4780da6f3feSBharata B Rao                            env->dcache_line_size)));
4790da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
4800da6f3feSBharata B Rao                            env->icache_line_size)));
4810da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
4820da6f3feSBharata B Rao                            env->icache_line_size)));
4830da6f3feSBharata B Rao 
4840da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
4850da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
4860da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
4870da6f3feSBharata B Rao     } else {
488ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
4890da6f3feSBharata B Rao     }
4900da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
4910da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
4920da6f3feSBharata B Rao                                pcc->l1_icache_size)));
4930da6f3feSBharata B Rao     } else {
494ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
4950da6f3feSBharata B Rao     }
4960da6f3feSBharata B Rao 
4970da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
4980da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
499fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
5000da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
5010da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
5020da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
5030da6f3feSBharata B Rao 
5040da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
5050da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
5060da6f3feSBharata B Rao     }
5070da6f3feSBharata B Rao 
5080da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
5090da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
5100da6f3feSBharata B Rao                           segs, sizeof(segs))));
5110da6f3feSBharata B Rao     }
5120da6f3feSBharata B Rao 
5130da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
5140da6f3feSBharata B Rao      *   0 / no property == no vector extensions
5150da6f3feSBharata B Rao      *   1               == VMX / Altivec available
5160da6f3feSBharata B Rao      *   2               == VSX available */
5170da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
5180da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
5190da6f3feSBharata B Rao 
5200da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
5210da6f3feSBharata B Rao     }
5220da6f3feSBharata B Rao 
5230da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
5240da6f3feSBharata B Rao      *   0 / no property == no DFP
5250da6f3feSBharata B Rao      *   1               == DFP available */
5260da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
5270da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
5280da6f3feSBharata B Rao     }
5290da6f3feSBharata B Rao 
5303654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
5310da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
5320da6f3feSBharata B Rao     if (page_sizes_prop_size) {
5330da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
5340da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
5350da6f3feSBharata B Rao     }
5360da6f3feSBharata B Rao 
537e957f6a9SSam Bobroff     spapr_populate_pa_features(env, fdt, offset, false);
53890da0d5aSBenjamin Herrenschmidt 
5390da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
54022419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
5410da6f3feSBharata B Rao 
5420da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
5430da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
5440da6f3feSBharata B Rao 
5450da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
5460da6f3feSBharata B Rao 
54712dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
548c64abd1fSSam Bobroff 
549c64abd1fSSam Bobroff     if (pcc->radix_page_info) {
550c64abd1fSSam Bobroff         for (i = 0; i < pcc->radix_page_info->count; i++) {
551c64abd1fSSam Bobroff             radix_AP_encodings[i] =
552c64abd1fSSam Bobroff                 cpu_to_be32(pcc->radix_page_info->entries[i]);
553c64abd1fSSam Bobroff         }
554c64abd1fSSam Bobroff         _FDT((fdt_setprop(fdt, offset, "ibm,processor-radix-AP-encodings",
555c64abd1fSSam Bobroff                           radix_AP_encodings,
556c64abd1fSSam Bobroff                           pcc->radix_page_info->count *
557c64abd1fSSam Bobroff                           sizeof(radix_AP_encodings[0]))));
558c64abd1fSSam Bobroff     }
5590da6f3feSBharata B Rao }
5600da6f3feSBharata B Rao 
5610da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
5620da6f3feSBharata B Rao {
5630da6f3feSBharata B Rao     CPUState *cs;
5640da6f3feSBharata B Rao     int cpus_offset;
5650da6f3feSBharata B Rao     char *nodename;
5660da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
5670da6f3feSBharata B Rao 
5680da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
5690da6f3feSBharata B Rao     _FDT(cpus_offset);
5700da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
5710da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
5720da6f3feSBharata B Rao 
5730da6f3feSBharata B Rao     /*
5740da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
5750da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
5760da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
5770da6f3feSBharata B Rao      */
5780da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
5790da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
5800da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
5810da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
5820da6f3feSBharata B Rao         int offset;
5830da6f3feSBharata B Rao 
5840da6f3feSBharata B Rao         if ((index % smt) != 0) {
5850da6f3feSBharata B Rao             continue;
5860da6f3feSBharata B Rao         }
5870da6f3feSBharata B Rao 
5880da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
5890da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
5900da6f3feSBharata B Rao         g_free(nodename);
5910da6f3feSBharata B Rao         _FDT(offset);
5920da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
5930da6f3feSBharata B Rao     }
5940da6f3feSBharata B Rao 
5950da6f3feSBharata B Rao }
5960da6f3feSBharata B Rao 
59703d196b7SBharata B Rao /*
59803d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
59903d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
60003d196b7SBharata B Rao  * of this device tree node.
60103d196b7SBharata B Rao  */
60203d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
60303d196b7SBharata B Rao {
60403d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
60503d196b7SBharata B Rao     int ret, i, offset;
60603d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
60703d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
608d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
609d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
610d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
611d0e5a8f2SBharata B Rao                        lmb_size;
61203d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
6136663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
61403d196b7SBharata B Rao 
615ef001f06SThomas Huth     /*
616d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
61716c25aefSBharata B Rao      */
618d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
61916c25aefSBharata B Rao         return 0;
62016c25aefSBharata B Rao     }
62116c25aefSBharata B Rao 
62216c25aefSBharata B Rao     /*
623ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
624ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
625ef001f06SThomas Huth      */
626ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
627ef001f06SThomas Huth               * sizeof(uint32_t);
62803d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
62903d196b7SBharata B Rao 
63003d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
63103d196b7SBharata B Rao 
63203d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
63303d196b7SBharata B Rao                     sizeof(prop_lmb_size));
63403d196b7SBharata B Rao     if (ret < 0) {
63503d196b7SBharata B Rao         goto out;
63603d196b7SBharata B Rao     }
63703d196b7SBharata B Rao 
63803d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
63903d196b7SBharata B Rao     if (ret < 0) {
64003d196b7SBharata B Rao         goto out;
64103d196b7SBharata B Rao     }
64203d196b7SBharata B Rao 
64303d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
64403d196b7SBharata B Rao     if (ret < 0) {
64503d196b7SBharata B Rao         goto out;
64603d196b7SBharata B Rao     }
64703d196b7SBharata B Rao 
64803d196b7SBharata B Rao     /* ibm,dynamic-memory */
64903d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
65003d196b7SBharata B Rao     cur_index++;
65103d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
652d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
65303d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
65403d196b7SBharata B Rao 
655d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
656d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
657d0e5a8f2SBharata B Rao             sPAPRDRConnectorClass *drck;
658d0e5a8f2SBharata B Rao 
659d0e5a8f2SBharata B Rao             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
66003d196b7SBharata B Rao             g_assert(drc);
66103d196b7SBharata B Rao             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
66203d196b7SBharata B Rao 
66303d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
66403d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
66503d196b7SBharata B Rao             dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
66603d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
66703d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
668d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
66903d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
67003d196b7SBharata B Rao             } else {
67103d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
67203d196b7SBharata B Rao             }
673d0e5a8f2SBharata B Rao         } else {
674d0e5a8f2SBharata B Rao             /*
675d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
676d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
677d0e5a8f2SBharata B Rao              * and as having no valid DRC.
678d0e5a8f2SBharata B Rao              */
679d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
680d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
681d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
682d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
683d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
684d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
685d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
686d0e5a8f2SBharata B Rao         }
68703d196b7SBharata B Rao 
68803d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
68903d196b7SBharata B Rao     }
69003d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
69103d196b7SBharata B Rao     if (ret < 0) {
69203d196b7SBharata B Rao         goto out;
69303d196b7SBharata B Rao     }
69403d196b7SBharata B Rao 
69503d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
69603d196b7SBharata B Rao     cur_index = int_buf;
6976663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
69803d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
69903d196b7SBharata B Rao     cur_index += 2;
7006663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
70103d196b7SBharata B Rao         uint32_t associativity[] = {
70203d196b7SBharata B Rao             cpu_to_be32(0x0),
70303d196b7SBharata B Rao             cpu_to_be32(0x0),
70403d196b7SBharata B Rao             cpu_to_be32(0x0),
70503d196b7SBharata B Rao             cpu_to_be32(i)
70603d196b7SBharata B Rao         };
70703d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
70803d196b7SBharata B Rao         cur_index += 4;
70903d196b7SBharata B Rao     }
71003d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
71103d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
71203d196b7SBharata B Rao out:
71303d196b7SBharata B Rao     g_free(int_buf);
71403d196b7SBharata B Rao     return ret;
71503d196b7SBharata B Rao }
71603d196b7SBharata B Rao 
7176787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
7186787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
7196787d27bSMichael Roth {
7206787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
721417ece33SMichael Roth     int ret = 0, offset;
7226787d27bSMichael Roth 
7236787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
7246787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
7256787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
7266787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
727417ece33SMichael Roth         if (ret) {
728417ece33SMichael Roth             goto out;
729417ece33SMichael Roth         }
7306787d27bSMichael Roth     }
7316787d27bSMichael Roth 
732417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
733417ece33SMichael Roth     if (offset < 0) {
734417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
735417ece33SMichael Roth         if (offset < 0) {
736417ece33SMichael Roth             return offset;
737417ece33SMichael Roth         }
738417ece33SMichael Roth     }
739417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
740417ece33SMichael Roth                                  "ibm,architecture-vec-5");
741417ece33SMichael Roth 
742417ece33SMichael Roth out:
7436787d27bSMichael Roth     return ret;
7446787d27bSMichael Roth }
7456787d27bSMichael Roth 
74603d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
74703d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
7486787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
74903d196b7SBharata B Rao {
75003d196b7SBharata B Rao     void *fdt, *fdt_skel;
75103d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
75203d196b7SBharata B Rao 
75303d196b7SBharata B Rao     size -= sizeof(hdr);
75403d196b7SBharata B Rao 
75503d196b7SBharata B Rao     /* Create sceleton */
75603d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
75703d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
75803d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
75903d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
76003d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
76103d196b7SBharata B Rao     fdt = g_malloc0(size);
76203d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
76303d196b7SBharata B Rao     g_free(fdt_skel);
76403d196b7SBharata B Rao 
76503d196b7SBharata B Rao     /* Fixup cpu nodes */
76603d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
76703d196b7SBharata B Rao 
7686787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
7696787d27bSMichael Roth         return -1;
77003d196b7SBharata B Rao     }
77103d196b7SBharata B Rao 
77203d196b7SBharata B Rao     /* Pack resulting tree */
77303d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
77403d196b7SBharata B Rao 
77503d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
77603d196b7SBharata B Rao         trace_spapr_cas_failed(size);
77703d196b7SBharata B Rao         return -1;
77803d196b7SBharata B Rao     }
77903d196b7SBharata B Rao 
78003d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
78103d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
78203d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
78303d196b7SBharata B Rao     g_free(fdt);
78403d196b7SBharata B Rao 
78503d196b7SBharata B Rao     return 0;
78603d196b7SBharata B Rao }
78703d196b7SBharata B Rao 
7883f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
7893f5dabceSDavid Gibson {
7903f5dabceSDavid Gibson     int rtas;
7913f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
7923f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
7933f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
7943f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
7953f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
7963f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
7973f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
7983f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
7993f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
8003f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
8013f5dabceSDavid Gibson     };
8023f5dabceSDavid Gibson 
8033f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
8043f5dabceSDavid Gibson 
8053f5dabceSDavid Gibson     /* hypertas */
8063f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
8073f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
8083f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
8093f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
8103f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
8113f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
8123f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
8133f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
8143f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
8153f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
8163f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
8173f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
8183f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
8193f5dabceSDavid Gibson 
8203f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
8213f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
8223f5dabceSDavid Gibson     }
8233f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
8243f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
8253f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
8263f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
8273f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
8283f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
8293f5dabceSDavid Gibson 
8303f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
8313f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
8323f5dabceSDavid Gibson 
8333f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
8343f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
8353f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
8363f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
8373f5dabceSDavid Gibson 
8383f5dabceSDavid Gibson     if (msi_nonbroken) {
8393f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
8403f5dabceSDavid Gibson     }
8413f5dabceSDavid Gibson 
8423f5dabceSDavid Gibson     /*
8433f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
8443f5dabceSDavid Gibson      * back to the guest cpu.
8453f5dabceSDavid Gibson      *
8463f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
8473f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
8483f5dabceSDavid Gibson      */
8493f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
8503f5dabceSDavid Gibson 
8513f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
8523f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
8533f5dabceSDavid Gibson 
8543f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
8553f5dabceSDavid Gibson }
8563f5dabceSDavid Gibson 
8579fb4541fSSam Bobroff /* Prepare ibm,arch-vec-5-platform-support, which indicates the MMU features
8589fb4541fSSam Bobroff  * that the guest may request and thus the valid values for bytes 24..26 of
8599fb4541fSSam Bobroff  * option vector 5: */
8609fb4541fSSam Bobroff static void spapr_dt_ov5_platform_support(void *fdt, int chosen)
8619fb4541fSSam Bobroff {
862545d6e2bSSuraj Jitindar Singh     PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
863545d6e2bSSuraj Jitindar Singh 
8649fb4541fSSam Bobroff     char val[2 * 3] = {
8659fb4541fSSam Bobroff         24, 0x00, /* Hash/Radix, filled in below. */
8669fb4541fSSam Bobroff         25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
8679fb4541fSSam Bobroff         26, 0x40, /* Radix options: GTSE == yes. */
8689fb4541fSSam Bobroff     };
8699fb4541fSSam Bobroff 
8709fb4541fSSam Bobroff     if (kvm_enabled()) {
8719fb4541fSSam Bobroff         if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
8729fb4541fSSam Bobroff             val[1] = 0x80; /* OV5_MMU_BOTH */
8739fb4541fSSam Bobroff         } else if (kvmppc_has_cap_mmu_radix()) {
8749fb4541fSSam Bobroff             val[1] = 0x40; /* OV5_MMU_RADIX_300 */
8759fb4541fSSam Bobroff         } else {
8769fb4541fSSam Bobroff             val[1] = 0x00; /* Hash */
8779fb4541fSSam Bobroff         }
8789fb4541fSSam Bobroff     } else {
879545d6e2bSSuraj Jitindar Singh         if (first_ppc_cpu->env.mmu_model & POWERPC_MMU_V3) {
880545d6e2bSSuraj Jitindar Singh             /* V3 MMU supports both hash and radix (with dynamic switching) */
881545d6e2bSSuraj Jitindar Singh             val[1] = 0xC0;
882545d6e2bSSuraj Jitindar Singh         } else {
883545d6e2bSSuraj Jitindar Singh             /* Otherwise we can only do hash */
8849fb4541fSSam Bobroff             val[1] = 0x00;
8859fb4541fSSam Bobroff         }
886545d6e2bSSuraj Jitindar Singh     }
8879fb4541fSSam Bobroff     _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
8889fb4541fSSam Bobroff                      val, sizeof(val)));
8899fb4541fSSam Bobroff }
8909fb4541fSSam Bobroff 
8917c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
8927c866c6aSDavid Gibson {
8937c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
8947c866c6aSDavid Gibson     int chosen;
8957c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
8967c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
8977c866c6aSDavid Gibson     size_t cb = 0;
8987c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
8997c866c6aSDavid Gibson 
9007c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
9017c866c6aSDavid Gibson 
9027c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
9037c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
9047c866c6aSDavid Gibson                           spapr->initrd_base));
9057c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
9067c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
9077c866c6aSDavid Gibson 
9087c866c6aSDavid Gibson     if (spapr->kernel_size) {
9097c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
9107c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
9117c866c6aSDavid Gibson 
9127c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
9137c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
9147c866c6aSDavid Gibson         if (spapr->kernel_le) {
9157c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
9167c866c6aSDavid Gibson         }
9177c866c6aSDavid Gibson     }
9187c866c6aSDavid Gibson     if (boot_menu) {
9197c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
9207c866c6aSDavid Gibson     }
9217c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
9227c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
9237c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
9247c866c6aSDavid Gibson 
9257c866c6aSDavid Gibson     if (cb && bootlist) {
9267c866c6aSDavid Gibson         int i;
9277c866c6aSDavid Gibson 
9287c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
9297c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
9307c866c6aSDavid Gibson                 bootlist[i] = ' ';
9317c866c6aSDavid Gibson             }
9327c866c6aSDavid Gibson         }
9337c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
9347c866c6aSDavid Gibson     }
9357c866c6aSDavid Gibson 
9367c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
9377c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
9387c866c6aSDavid Gibson     }
9397c866c6aSDavid Gibson 
9407c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
9417c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
9427c866c6aSDavid Gibson     }
9437c866c6aSDavid Gibson 
9449fb4541fSSam Bobroff     spapr_dt_ov5_platform_support(fdt, chosen);
9459fb4541fSSam Bobroff 
9467c866c6aSDavid Gibson     g_free(stdout_path);
9477c866c6aSDavid Gibson     g_free(bootlist);
9487c866c6aSDavid Gibson }
9497c866c6aSDavid Gibson 
950fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
951fca5f2dcSDavid Gibson {
952fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
953fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
954fca5f2dcSDavid Gibson     int hypervisor;
955fca5f2dcSDavid Gibson     uint8_t hypercall[16];
956fca5f2dcSDavid Gibson 
957fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
958fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
959fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
960fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
961fca5f2dcSDavid Gibson         /*
962fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
963fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
964fca5f2dcSDavid Gibson          */
965fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
966fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
967fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
968fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
969fca5f2dcSDavid Gibson         }
970fca5f2dcSDavid Gibson     }
971fca5f2dcSDavid Gibson }
972fca5f2dcSDavid Gibson 
973997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
97453018216SPaolo Bonzini                              hwaddr rtas_addr,
97553018216SPaolo Bonzini                              hwaddr rtas_size)
97653018216SPaolo Bonzini {
9775b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
9783c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
979c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
9807c866c6aSDavid Gibson     int ret;
98153018216SPaolo Bonzini     void *fdt;
98253018216SPaolo Bonzini     sPAPRPHBState *phb;
983398a0bd5SDavid Gibson     char *buf;
98471cd4dacSCédric Le Goater     int smt = kvmppc_smt_threads();
98553018216SPaolo Bonzini 
986398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
987398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
98853018216SPaolo Bonzini 
989398a0bd5SDavid Gibson     /* Root node */
990398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
991398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
992398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
993398a0bd5SDavid Gibson 
994398a0bd5SDavid Gibson     /*
995398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
996398a0bd5SDavid Gibson      * and what is the uuid of the guest
997398a0bd5SDavid Gibson      */
998398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
999398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1000398a0bd5SDavid Gibson         g_free(buf);
1001398a0bd5SDavid Gibson     }
1002398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
1003398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1004398a0bd5SDavid Gibson         g_free(buf);
1005398a0bd5SDavid Gibson     }
1006398a0bd5SDavid Gibson 
1007398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
1008398a0bd5SDavid Gibson 
1009398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1010398a0bd5SDavid Gibson     if (qemu_uuid_set) {
1011398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1012398a0bd5SDavid Gibson     }
1013398a0bd5SDavid Gibson     g_free(buf);
1014398a0bd5SDavid Gibson 
1015398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
1016398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1017398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
1018398a0bd5SDavid Gibson     }
1019398a0bd5SDavid Gibson 
1020398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1021398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
102253018216SPaolo Bonzini 
10239b9a1908SDavid Gibson     /* /interrupt controller */
102471cd4dacSCédric Le Goater     spapr_dt_xics(DIV_ROUND_UP(max_cpus * smt, smp_threads), fdt, PHANDLE_XICP);
10259b9a1908SDavid Gibson 
1026e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
1027e8f986fcSBharata B Rao     if (ret < 0) {
1028ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
1029e8f986fcSBharata B Rao         exit(1);
103053018216SPaolo Bonzini     }
103153018216SPaolo Bonzini 
1032bf5a6696SDavid Gibson     /* /vdevice */
1033bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
103453018216SPaolo Bonzini 
10354d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
10364d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
10374d9392beSThomas Huth         if (ret < 0) {
1038ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
10394d9392beSThomas Huth             exit(1);
10404d9392beSThomas Huth         }
10414d9392beSThomas Huth     }
10424d9392beSThomas Huth 
104353018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
104453018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
104553018216SPaolo Bonzini         if (ret < 0) {
1046da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
104753018216SPaolo Bonzini             exit(1);
104853018216SPaolo Bonzini         }
1049da34fed7SThomas Huth     }
105053018216SPaolo Bonzini 
10510da6f3feSBharata B Rao     /* cpus */
10520da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
105353018216SPaolo Bonzini 
1054c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
1055c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1056c20d332aSBharata B Rao     }
1057c20d332aSBharata B Rao 
1058c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
1059af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1060af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1061af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1062af81cf32SBharata B Rao         if (ret < 0) {
1063af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1064af81cf32SBharata B Rao             exit(1);
1065af81cf32SBharata B Rao         }
1066af81cf32SBharata B Rao     }
1067af81cf32SBharata B Rao 
1068ffb1e275SDavid Gibson     /* /event-sources */
1069ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1070ffb1e275SDavid Gibson 
10713f5dabceSDavid Gibson     /* /rtas */
10723f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
10733f5dabceSDavid Gibson 
10747c866c6aSDavid Gibson     /* /chosen */
10757c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1076cf6e5223SDavid Gibson 
1077fca5f2dcSDavid Gibson     /* /hypervisor */
1078fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1079fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1080fca5f2dcSDavid Gibson     }
1081fca5f2dcSDavid Gibson 
1082cf6e5223SDavid Gibson     /* Build memory reserve map */
1083cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1084cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1085cf6e5223SDavid Gibson     }
1086cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1087cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1088cf6e5223SDavid Gibson     }
1089cf6e5223SDavid Gibson 
10906787d27bSMichael Roth     /* ibm,client-architecture-support updates */
10916787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
10926787d27bSMichael Roth     if (ret < 0) {
10936787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
10946787d27bSMichael Roth         exit(1);
10956787d27bSMichael Roth     }
10966787d27bSMichael Roth 
1097997b6cfcSDavid Gibson     return fdt;
109853018216SPaolo Bonzini }
109953018216SPaolo Bonzini 
110053018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
110153018216SPaolo Bonzini {
110253018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
110353018216SPaolo Bonzini }
110453018216SPaolo Bonzini 
11051d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
11061d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
110753018216SPaolo Bonzini {
110853018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
110953018216SPaolo Bonzini 
11108d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
11118d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
11128d04fb55SJan Kiszka 
111353018216SPaolo Bonzini     if (msr_pr) {
111453018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
111553018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
111653018216SPaolo Bonzini     } else {
111753018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
111853018216SPaolo Bonzini     }
111953018216SPaolo Bonzini }
112053018216SPaolo Bonzini 
11219861bb3eSSuraj Jitindar Singh static uint64_t spapr_get_patbe(PPCVirtualHypervisor *vhyp)
11229861bb3eSSuraj Jitindar Singh {
11239861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
11249861bb3eSSuraj Jitindar Singh 
11259861bb3eSSuraj Jitindar Singh     return spapr->patb_entry;
11269861bb3eSSuraj Jitindar Singh }
11279861bb3eSSuraj Jitindar Singh 
1128e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1129e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1130e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1131e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1132e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1133e6b8fd24SSamuel Mendoza-Jonas 
1134715c5407SDavid Gibson /*
1135715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1136715c5407SDavid Gibson  */
1137715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1138715c5407SDavid Gibson {
1139715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1140715c5407SDavid Gibson         return spapr->htab_fd;
1141715c5407SDavid Gibson     }
1142715c5407SDavid Gibson 
1143715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1144715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1145715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1146715c5407SDavid Gibson                      strerror(errno));
1147715c5407SDavid Gibson     }
1148715c5407SDavid Gibson 
1149715c5407SDavid Gibson     return spapr->htab_fd;
1150715c5407SDavid Gibson }
1151715c5407SDavid Gibson 
1152b4db5413SSuraj Jitindar Singh void close_htab_fd(sPAPRMachineState *spapr)
1153715c5407SDavid Gibson {
1154715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1155715c5407SDavid Gibson         close(spapr->htab_fd);
1156715c5407SDavid Gibson     }
1157715c5407SDavid Gibson     spapr->htab_fd = -1;
1158715c5407SDavid Gibson }
1159715c5407SDavid Gibson 
1160e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1161e57ca75cSDavid Gibson {
1162e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1163e57ca75cSDavid Gibson 
1164e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1165e57ca75cSDavid Gibson }
1166e57ca75cSDavid Gibson 
1167e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1168e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1169e57ca75cSDavid Gibson {
1170e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1171e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1172e57ca75cSDavid Gibson 
1173e57ca75cSDavid Gibson     if (!spapr->htab) {
1174e57ca75cSDavid Gibson         /*
1175e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1176e57ca75cSDavid Gibson          */
1177e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1178e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1179e57ca75cSDavid Gibson         return hptes;
1180e57ca75cSDavid Gibson     }
1181e57ca75cSDavid Gibson 
1182e57ca75cSDavid Gibson     /*
1183e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1184e57ca75cSDavid Gibson      * accessible PTEG.
1185e57ca75cSDavid Gibson      */
1186e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1187e57ca75cSDavid Gibson }
1188e57ca75cSDavid Gibson 
1189e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1190e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1191e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1192e57ca75cSDavid Gibson {
1193e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1194e57ca75cSDavid Gibson 
1195e57ca75cSDavid Gibson     if (!spapr->htab) {
1196e57ca75cSDavid Gibson         g_free((void *)hptes);
1197e57ca75cSDavid Gibson     }
1198e57ca75cSDavid Gibson 
1199e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1200e57ca75cSDavid Gibson }
1201e57ca75cSDavid Gibson 
1202e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1203e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1204e57ca75cSDavid Gibson {
1205e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1206e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1207e57ca75cSDavid Gibson 
1208e57ca75cSDavid Gibson     if (!spapr->htab) {
1209e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1210e57ca75cSDavid Gibson     } else {
1211e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1212e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1213e57ca75cSDavid Gibson     }
1214e57ca75cSDavid Gibson }
1215e57ca75cSDavid Gibson 
12168dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
12178dfe8e7fSDavid Gibson {
12188dfe8e7fSDavid Gibson     int shift;
12198dfe8e7fSDavid Gibson 
12208dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
12218dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
12228dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
12238dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
12248dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
12258dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
12268dfe8e7fSDavid Gibson     return shift;
12278dfe8e7fSDavid Gibson }
12288dfe8e7fSDavid Gibson 
122906ec79e8SBharata B Rao void spapr_free_hpt(sPAPRMachineState *spapr)
123006ec79e8SBharata B Rao {
123106ec79e8SBharata B Rao     g_free(spapr->htab);
123206ec79e8SBharata B Rao     spapr->htab = NULL;
123306ec79e8SBharata B Rao     spapr->htab_shift = 0;
123406ec79e8SBharata B Rao     close_htab_fd(spapr);
123506ec79e8SBharata B Rao }
123606ec79e8SBharata B Rao 
1237c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1238c5f54f3eSDavid Gibson                                  Error **errp)
123953018216SPaolo Bonzini {
1240c5f54f3eSDavid Gibson     long rc;
124153018216SPaolo Bonzini 
1242c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
124306ec79e8SBharata B Rao     spapr_free_hpt(spapr);
124453018216SPaolo Bonzini 
1245c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1246c5f54f3eSDavid Gibson     if (rc < 0) {
1247c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1248c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1249c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1250c5f54f3eSDavid Gibson                          shift);
1251c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1252c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1253c5f54f3eSDavid Gibson     } else if (rc > 0) {
1254c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1255c5f54f3eSDavid Gibson         if (rc != shift) {
1256c5f54f3eSDavid Gibson             error_setg(errp,
1257c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1258c5f54f3eSDavid Gibson                        shift, rc);
12597735fedaSBharata B Rao         }
12607735fedaSBharata B Rao 
126153018216SPaolo Bonzini         spapr->htab_shift = shift;
1262c18ad9a5SDavid Gibson         spapr->htab = NULL;
1263b817772aSBharata B Rao     } else {
1264c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1265c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1266c5f54f3eSDavid Gibson         int i;
126701a57972SSamuel Mendoza-Jonas 
1268c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1269c5f54f3eSDavid Gibson         if (!spapr->htab) {
1270c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1271c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1272c5f54f3eSDavid Gibson             return;
1273b817772aSBharata B Rao         }
1274b817772aSBharata B Rao 
1275c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1276c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1277b817772aSBharata B Rao 
1278c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1279c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
12807735fedaSBharata B Rao         }
128153018216SPaolo Bonzini     }
128253018216SPaolo Bonzini }
128353018216SPaolo Bonzini 
1284b4db5413SSuraj Jitindar Singh void spapr_setup_hpt_and_vrma(sPAPRMachineState *spapr)
1285b4db5413SSuraj Jitindar Singh {
1286b4db5413SSuraj Jitindar Singh     spapr_reallocate_hpt(spapr,
1287b4db5413SSuraj Jitindar Singh                      spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size),
1288b4db5413SSuraj Jitindar Singh                      &error_fatal);
1289b4db5413SSuraj Jitindar Singh     if (spapr->vrma_adjust) {
1290b4db5413SSuraj Jitindar Singh         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1291b4db5413SSuraj Jitindar Singh                                           spapr->htab_shift);
1292b4db5413SSuraj Jitindar Singh     }
1293b4db5413SSuraj Jitindar Singh     /* We're setting up a hash table, so that means we're not radix */
1294b4db5413SSuraj Jitindar Singh     spapr->patb_entry = 0;
1295b4db5413SSuraj Jitindar Singh }
1296b4db5413SSuraj Jitindar Singh 
12974f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
12989e3f9733SAlexander Graf {
12999e3f9733SAlexander Graf     bool matched = false;
13009e3f9733SAlexander Graf 
13019e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
13029e3f9733SAlexander Graf         matched = true;
13039e3f9733SAlexander Graf     }
13049e3f9733SAlexander Graf 
13059e3f9733SAlexander Graf     if (!matched) {
13069e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
13079e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
13089e3f9733SAlexander Graf         exit(1);
13099e3f9733SAlexander Graf     }
13109e3f9733SAlexander Graf }
13119e3f9733SAlexander Graf 
131253018216SPaolo Bonzini static void ppc_spapr_reset(void)
131353018216SPaolo Bonzini {
1314c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1315c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1316182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1317b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1318cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1319997b6cfcSDavid Gibson     void *fdt;
1320997b6cfcSDavid Gibson     int rc;
1321259186a7SAndreas Färber 
13229e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
13239e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
13249e3f9733SAlexander Graf 
1325b4db5413SSuraj Jitindar Singh     if (kvm_enabled() && kvmppc_has_cap_mmu_radix()) {
1326b4db5413SSuraj Jitindar Singh         /* If using KVM with radix mode available, VCPUs can be started
1327b4db5413SSuraj Jitindar Singh          * without a HPT because KVM will start them in radix mode.
1328b4db5413SSuraj Jitindar Singh          * Set the GR bit in PATB so that we know there is no HPT. */
1329b4db5413SSuraj Jitindar Singh         spapr->patb_entry = PATBE1_GR;
1330b4db5413SSuraj Jitindar Singh     } else {
13319861bb3eSSuraj Jitindar Singh         spapr->patb_entry = 0;
1332b4db5413SSuraj Jitindar Singh         spapr_setup_hpt_and_vrma(spapr);
1333c5f54f3eSDavid Gibson     }
133453018216SPaolo Bonzini 
133553018216SPaolo Bonzini     qemu_devices_reset();
133653018216SPaolo Bonzini 
1337b7d1f77aSBenjamin Herrenschmidt     /*
1338b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1339b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1340b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1341b7d1f77aSBenjamin Herrenschmidt      */
1342b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1343cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1344cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1345b7d1f77aSBenjamin Herrenschmidt 
13466787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
13476787d27bSMichael Roth      * negotiated options and start from scratch */
13486787d27bSMichael Roth     if (!spapr->cas_reboot) {
13496787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
13506787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
13516787d27bSMichael Roth     }
13526787d27bSMichael Roth 
1353cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
135453018216SPaolo Bonzini 
13552cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1356b7d1f77aSBenjamin Herrenschmidt 
1357997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1358997b6cfcSDavid Gibson 
1359997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1360997b6cfcSDavid Gibson     assert(rc == 0);
1361997b6cfcSDavid Gibson 
1362997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1363997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1364997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1365997b6cfcSDavid Gibson         exit(1);
1366997b6cfcSDavid Gibson     }
1367997b6cfcSDavid Gibson 
1368997b6cfcSDavid Gibson     /* Load the fdt */
1369997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1370cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1371997b6cfcSDavid Gibson     g_free(fdt);
1372997b6cfcSDavid Gibson 
137353018216SPaolo Bonzini     /* Set up the entry state */
1374182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1375cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1376182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1377182735efSAndreas Färber     first_cpu->halted = 0;
13781b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
137953018216SPaolo Bonzini 
13806787d27bSMichael Roth     spapr->cas_reboot = false;
138153018216SPaolo Bonzini }
138253018216SPaolo Bonzini 
138328e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
138453018216SPaolo Bonzini {
13852ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
13863978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
138753018216SPaolo Bonzini 
13883978b863SPaolo Bonzini     if (dinfo) {
13896231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
13906231a6daSMarkus Armbruster                             &error_fatal);
139153018216SPaolo Bonzini     }
139253018216SPaolo Bonzini 
139353018216SPaolo Bonzini     qdev_init_nofail(dev);
139453018216SPaolo Bonzini 
139553018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
139653018216SPaolo Bonzini }
139753018216SPaolo Bonzini 
139828e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
139928df36a1SDavid Gibson {
1400147ff807SCédric Le Goater     object_initialize(&spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC);
1401147ff807SCédric Le Goater     object_property_add_child(OBJECT(spapr), "rtc", OBJECT(&spapr->rtc),
1402147ff807SCédric Le Goater                               &error_fatal);
1403147ff807SCédric Le Goater     object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1404147ff807SCédric Le Goater                               &error_fatal);
1405147ff807SCédric Le Goater     object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1406147ff807SCédric Le Goater                               "date", &error_fatal);
140728df36a1SDavid Gibson }
140828df36a1SDavid Gibson 
140953018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
141014c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
141153018216SPaolo Bonzini {
141253018216SPaolo Bonzini     switch (vga_interface_type) {
141353018216SPaolo Bonzini     case VGA_NONE:
14147effdaa3SMark Wu         return false;
14157effdaa3SMark Wu     case VGA_DEVICE:
14167effdaa3SMark Wu         return true;
141753018216SPaolo Bonzini     case VGA_STD:
1418b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
141953018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
142053018216SPaolo Bonzini     default:
142114c6a894SDavid Gibson         error_setg(errp,
142214c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
142314c6a894SDavid Gibson         return false;
142453018216SPaolo Bonzini     }
142553018216SPaolo Bonzini }
142653018216SPaolo Bonzini 
1427880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1428880ae7deSDavid Gibson {
142928e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1430880ae7deSDavid Gibson     int err = 0;
1431880ae7deSDavid Gibson 
1432a7ff1212SCédric Le Goater     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
14335bc8d26dSCédric Le Goater         CPUState *cs;
14345bc8d26dSCédric Le Goater         CPU_FOREACH(cs) {
14355bc8d26dSCédric Le Goater             PowerPCCPU *cpu = POWERPC_CPU(cs);
14365bc8d26dSCédric Le Goater             icp_resend(ICP(cpu->intc));
1437a7ff1212SCédric Le Goater         }
1438a7ff1212SCédric Le Goater     }
1439a7ff1212SCédric Le Goater 
1440631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1441880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1442880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1443880ae7deSDavid Gibson      * value into the RTC device */
1444880ae7deSDavid Gibson     if (version_id < 3) {
1445147ff807SCédric Le Goater         err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
1446880ae7deSDavid Gibson     }
1447880ae7deSDavid Gibson 
1448880ae7deSDavid Gibson     return err;
1449880ae7deSDavid Gibson }
1450880ae7deSDavid Gibson 
1451880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1452880ae7deSDavid Gibson {
1453880ae7deSDavid Gibson     return version_id < 3;
1454880ae7deSDavid Gibson }
1455880ae7deSDavid Gibson 
145662ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
145762ef3760SMichael Roth {
145862ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
145962ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
146062ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
146162ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
146262ef3760SMichael Roth     bool cas_needed;
146362ef3760SMichael Roth 
146462ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
146562ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
146662ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
146762ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
146862ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
146962ef3760SMichael Roth      * negotiatied on the source side.
147062ef3760SMichael Roth      *
147162ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
147262ef3760SMichael Roth      * are the only options available on the current machine/platform.
147362ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
147462ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
147562ef3760SMichael Roth      * compatibility.
147662ef3760SMichael Roth      *
147762ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
147862ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
147962ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
148062ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
148162ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
148262ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
148362ef3760SMichael Roth      *
148462ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
148562ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
148662ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
148762ef3760SMichael Roth      */
148862ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
148962ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
149062ef3760SMichael Roth 
149162ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
149262ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
149362ef3760SMichael Roth      * removed via machine options, which could generate a false positive
149462ef3760SMichael Roth      * that breaks migration.
149562ef3760SMichael Roth      */
149662ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
149762ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
149862ef3760SMichael Roth 
149962ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
150062ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
150162ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
150262ef3760SMichael Roth 
150362ef3760SMichael Roth     return cas_needed;
150462ef3760SMichael Roth }
150562ef3760SMichael Roth 
150662ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
150762ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
150862ef3760SMichael Roth     .version_id = 1,
150962ef3760SMichael Roth     .minimum_version_id = 1,
151062ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
151162ef3760SMichael Roth     .fields = (VMStateField[]) {
151262ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
151362ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
151462ef3760SMichael Roth         VMSTATE_END_OF_LIST()
151562ef3760SMichael Roth     },
151662ef3760SMichael Roth };
151762ef3760SMichael Roth 
15189861bb3eSSuraj Jitindar Singh static bool spapr_patb_entry_needed(void *opaque)
15199861bb3eSSuraj Jitindar Singh {
15209861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = opaque;
15219861bb3eSSuraj Jitindar Singh 
15229861bb3eSSuraj Jitindar Singh     return !!spapr->patb_entry;
15239861bb3eSSuraj Jitindar Singh }
15249861bb3eSSuraj Jitindar Singh 
15259861bb3eSSuraj Jitindar Singh static const VMStateDescription vmstate_spapr_patb_entry = {
15269861bb3eSSuraj Jitindar Singh     .name = "spapr_patb_entry",
15279861bb3eSSuraj Jitindar Singh     .version_id = 1,
15289861bb3eSSuraj Jitindar Singh     .minimum_version_id = 1,
15299861bb3eSSuraj Jitindar Singh     .needed = spapr_patb_entry_needed,
15309861bb3eSSuraj Jitindar Singh     .fields = (VMStateField[]) {
15319861bb3eSSuraj Jitindar Singh         VMSTATE_UINT64(patb_entry, sPAPRMachineState),
15329861bb3eSSuraj Jitindar Singh         VMSTATE_END_OF_LIST()
15339861bb3eSSuraj Jitindar Singh     },
15349861bb3eSSuraj Jitindar Singh };
15359861bb3eSSuraj Jitindar Singh 
15364be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
15374be21d56SDavid Gibson     .name = "spapr",
1538880ae7deSDavid Gibson     .version_id = 3,
15394be21d56SDavid Gibson     .minimum_version_id = 1,
1540880ae7deSDavid Gibson     .post_load = spapr_post_load,
15414be21d56SDavid Gibson     .fields = (VMStateField[]) {
1542880ae7deSDavid Gibson         /* used to be @next_irq */
1543880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
15444be21d56SDavid Gibson 
15454be21d56SDavid Gibson         /* RTC offset */
154628e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1547880ae7deSDavid Gibson 
154828e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
15494be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
15504be21d56SDavid Gibson     },
155162ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
155262ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
15539861bb3eSSuraj Jitindar Singh         &vmstate_spapr_patb_entry,
155462ef3760SMichael Roth         NULL
155562ef3760SMichael Roth     }
15564be21d56SDavid Gibson };
15574be21d56SDavid Gibson 
15584be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
15594be21d56SDavid Gibson {
156028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
15614be21d56SDavid Gibson 
15624be21d56SDavid Gibson     /* "Iteration" header */
15634be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
15644be21d56SDavid Gibson 
1565e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1566e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1567e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1568e68cb8b4SAlexey Kardashevskiy     } else {
1569e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
15704be21d56SDavid Gibson     }
15714be21d56SDavid Gibson 
1572e68cb8b4SAlexey Kardashevskiy 
1573e68cb8b4SAlexey Kardashevskiy     return 0;
1574e68cb8b4SAlexey Kardashevskiy }
15754be21d56SDavid Gibson 
157628e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
15774be21d56SDavid Gibson                                  int64_t max_ns)
15784be21d56SDavid Gibson {
1579378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
15804be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
15814be21d56SDavid Gibson     int index = spapr->htab_save_index;
1582bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
15834be21d56SDavid Gibson 
15844be21d56SDavid Gibson     assert(spapr->htab_first_pass);
15854be21d56SDavid Gibson 
15864be21d56SDavid Gibson     do {
15874be21d56SDavid Gibson         int chunkstart;
15884be21d56SDavid Gibson 
15894be21d56SDavid Gibson         /* Consume invalid HPTEs */
15904be21d56SDavid Gibson         while ((index < htabslots)
15914be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
15924be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
159324ec2863SMarc-André Lureau             index++;
15944be21d56SDavid Gibson         }
15954be21d56SDavid Gibson 
15964be21d56SDavid Gibson         /* Consume valid HPTEs */
15974be21d56SDavid Gibson         chunkstart = index;
1598338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
15994be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
16004be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
160124ec2863SMarc-André Lureau             index++;
16024be21d56SDavid Gibson         }
16034be21d56SDavid Gibson 
16044be21d56SDavid Gibson         if (index > chunkstart) {
16054be21d56SDavid Gibson             int n_valid = index - chunkstart;
16064be21d56SDavid Gibson 
16074be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
16084be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
16094be21d56SDavid Gibson             qemu_put_be16(f, 0);
16104be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
16114be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
16124be21d56SDavid Gibson 
1613378bc217SDavid Gibson             if (has_timeout &&
1614378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
16154be21d56SDavid Gibson                 break;
16164be21d56SDavid Gibson             }
16174be21d56SDavid Gibson         }
16184be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
16194be21d56SDavid Gibson 
16204be21d56SDavid Gibson     if (index >= htabslots) {
16214be21d56SDavid Gibson         assert(index == htabslots);
16224be21d56SDavid Gibson         index = 0;
16234be21d56SDavid Gibson         spapr->htab_first_pass = false;
16244be21d56SDavid Gibson     }
16254be21d56SDavid Gibson     spapr->htab_save_index = index;
16264be21d56SDavid Gibson }
16274be21d56SDavid Gibson 
162828e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
16294be21d56SDavid Gibson                                 int64_t max_ns)
16304be21d56SDavid Gibson {
16314be21d56SDavid Gibson     bool final = max_ns < 0;
16324be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
16334be21d56SDavid Gibson     int examined = 0, sent = 0;
16344be21d56SDavid Gibson     int index = spapr->htab_save_index;
1635bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
16364be21d56SDavid Gibson 
16374be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
16384be21d56SDavid Gibson 
16394be21d56SDavid Gibson     do {
16404be21d56SDavid Gibson         int chunkstart, invalidstart;
16414be21d56SDavid Gibson 
16424be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
16434be21d56SDavid Gibson         while ((index < htabslots)
16444be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
16454be21d56SDavid Gibson             index++;
16464be21d56SDavid Gibson             examined++;
16474be21d56SDavid Gibson         }
16484be21d56SDavid Gibson 
16494be21d56SDavid Gibson         chunkstart = index;
16504be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1651338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
16524be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
16534be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
16544be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
16554be21d56SDavid Gibson             index++;
16564be21d56SDavid Gibson             examined++;
16574be21d56SDavid Gibson         }
16584be21d56SDavid Gibson 
16594be21d56SDavid Gibson         invalidstart = index;
16604be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1661338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
16624be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
16634be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
16644be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
16654be21d56SDavid Gibson             index++;
16664be21d56SDavid Gibson             examined++;
16674be21d56SDavid Gibson         }
16684be21d56SDavid Gibson 
16694be21d56SDavid Gibson         if (index > chunkstart) {
16704be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
16714be21d56SDavid Gibson             int n_invalid = index - invalidstart;
16724be21d56SDavid Gibson 
16734be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
16744be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
16754be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
16764be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
16774be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
16784be21d56SDavid Gibson             sent += index - chunkstart;
16794be21d56SDavid Gibson 
1680bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
16814be21d56SDavid Gibson                 break;
16824be21d56SDavid Gibson             }
16834be21d56SDavid Gibson         }
16844be21d56SDavid Gibson 
16854be21d56SDavid Gibson         if (examined >= htabslots) {
16864be21d56SDavid Gibson             break;
16874be21d56SDavid Gibson         }
16884be21d56SDavid Gibson 
16894be21d56SDavid Gibson         if (index >= htabslots) {
16904be21d56SDavid Gibson             assert(index == htabslots);
16914be21d56SDavid Gibson             index = 0;
16924be21d56SDavid Gibson         }
16934be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
16944be21d56SDavid Gibson 
16954be21d56SDavid Gibson     if (index >= htabslots) {
16964be21d56SDavid Gibson         assert(index == htabslots);
16974be21d56SDavid Gibson         index = 0;
16984be21d56SDavid Gibson     }
16994be21d56SDavid Gibson 
17004be21d56SDavid Gibson     spapr->htab_save_index = index;
17014be21d56SDavid Gibson 
1702e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
17034be21d56SDavid Gibson }
17044be21d56SDavid Gibson 
1705e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1706e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1707e68cb8b4SAlexey Kardashevskiy 
17084be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
17094be21d56SDavid Gibson {
171028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1711715c5407SDavid Gibson     int fd;
1712e68cb8b4SAlexey Kardashevskiy     int rc = 0;
17134be21d56SDavid Gibson 
17144be21d56SDavid Gibson     /* Iteration header */
17154be21d56SDavid Gibson     qemu_put_be32(f, 0);
17164be21d56SDavid Gibson 
1717e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1718e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1719e68cb8b4SAlexey Kardashevskiy 
1720715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1721715c5407SDavid Gibson         if (fd < 0) {
1722715c5407SDavid Gibson             return fd;
172301a57972SSamuel Mendoza-Jonas         }
172401a57972SSamuel Mendoza-Jonas 
1725715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1726e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1727e68cb8b4SAlexey Kardashevskiy             return rc;
1728e68cb8b4SAlexey Kardashevskiy         }
1729e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
17304be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
17314be21d56SDavid Gibson     } else {
1732e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
17334be21d56SDavid Gibson     }
17344be21d56SDavid Gibson 
17354be21d56SDavid Gibson     /* End marker */
17364be21d56SDavid Gibson     qemu_put_be32(f, 0);
17374be21d56SDavid Gibson     qemu_put_be16(f, 0);
17384be21d56SDavid Gibson     qemu_put_be16(f, 0);
17394be21d56SDavid Gibson 
1740e68cb8b4SAlexey Kardashevskiy     return rc;
17414be21d56SDavid Gibson }
17424be21d56SDavid Gibson 
17434be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
17444be21d56SDavid Gibson {
174528e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1746715c5407SDavid Gibson     int fd;
17474be21d56SDavid Gibson 
17484be21d56SDavid Gibson     /* Iteration header */
17494be21d56SDavid Gibson     qemu_put_be32(f, 0);
17504be21d56SDavid Gibson 
1751e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1752e68cb8b4SAlexey Kardashevskiy         int rc;
1753e68cb8b4SAlexey Kardashevskiy 
1754e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1755e68cb8b4SAlexey Kardashevskiy 
1756715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1757715c5407SDavid Gibson         if (fd < 0) {
1758715c5407SDavid Gibson             return fd;
175901a57972SSamuel Mendoza-Jonas         }
176001a57972SSamuel Mendoza-Jonas 
1761715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1762e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1763e68cb8b4SAlexey Kardashevskiy             return rc;
1764e68cb8b4SAlexey Kardashevskiy         }
1765e68cb8b4SAlexey Kardashevskiy     } else {
1766378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1767378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1768378bc217SDavid Gibson         }
17694be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1770e68cb8b4SAlexey Kardashevskiy     }
17714be21d56SDavid Gibson 
17724be21d56SDavid Gibson     /* End marker */
17734be21d56SDavid Gibson     qemu_put_be32(f, 0);
17744be21d56SDavid Gibson     qemu_put_be16(f, 0);
17754be21d56SDavid Gibson     qemu_put_be16(f, 0);
17764be21d56SDavid Gibson 
17774be21d56SDavid Gibson     return 0;
17784be21d56SDavid Gibson }
17794be21d56SDavid Gibson 
17804be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
17814be21d56SDavid Gibson {
178228e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
17834be21d56SDavid Gibson     uint32_t section_hdr;
1784e68cb8b4SAlexey Kardashevskiy     int fd = -1;
17854be21d56SDavid Gibson 
17864be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
178798a5d100SDavid Gibson         error_report("htab_load() bad version");
17884be21d56SDavid Gibson         return -EINVAL;
17894be21d56SDavid Gibson     }
17904be21d56SDavid Gibson 
17914be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
17924be21d56SDavid Gibson 
17934be21d56SDavid Gibson     if (section_hdr) {
17949897e462SGreg Kurz         Error *local_err = NULL;
1795c5f54f3eSDavid Gibson 
1796c5f54f3eSDavid Gibson         /* First section gives the htab size */
1797c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1798c5f54f3eSDavid Gibson         if (local_err) {
1799c5f54f3eSDavid Gibson             error_report_err(local_err);
18004be21d56SDavid Gibson             return -EINVAL;
18014be21d56SDavid Gibson         }
18024be21d56SDavid Gibson         return 0;
18034be21d56SDavid Gibson     }
18044be21d56SDavid Gibson 
1805e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1806e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1807e68cb8b4SAlexey Kardashevskiy 
1808e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1809e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
181098a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1811e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1812e68cb8b4SAlexey Kardashevskiy         }
1813e68cb8b4SAlexey Kardashevskiy     }
1814e68cb8b4SAlexey Kardashevskiy 
18154be21d56SDavid Gibson     while (true) {
18164be21d56SDavid Gibson         uint32_t index;
18174be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
18184be21d56SDavid Gibson 
18194be21d56SDavid Gibson         index = qemu_get_be32(f);
18204be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
18214be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
18224be21d56SDavid Gibson 
18234be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
18244be21d56SDavid Gibson             /* End of Stream */
18254be21d56SDavid Gibson             break;
18264be21d56SDavid Gibson         }
18274be21d56SDavid Gibson 
1828e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
18294be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
18304be21d56SDavid Gibson             /* Bad index in stream */
183198a5d100SDavid Gibson             error_report(
183298a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
183398a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
18344be21d56SDavid Gibson             return -EINVAL;
18354be21d56SDavid Gibson         }
18364be21d56SDavid Gibson 
1837e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
18384be21d56SDavid Gibson             if (n_valid) {
18394be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
18404be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
18414be21d56SDavid Gibson             }
18424be21d56SDavid Gibson             if (n_invalid) {
18434be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
18444be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
18454be21d56SDavid Gibson             }
1846e68cb8b4SAlexey Kardashevskiy         } else {
1847e68cb8b4SAlexey Kardashevskiy             int rc;
1848e68cb8b4SAlexey Kardashevskiy 
1849e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1850e68cb8b4SAlexey Kardashevskiy 
1851e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1852e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1853e68cb8b4SAlexey Kardashevskiy                 return rc;
1854e68cb8b4SAlexey Kardashevskiy             }
1855e68cb8b4SAlexey Kardashevskiy         }
1856e68cb8b4SAlexey Kardashevskiy     }
1857e68cb8b4SAlexey Kardashevskiy 
1858e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1859e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1860e68cb8b4SAlexey Kardashevskiy         close(fd);
18614be21d56SDavid Gibson     }
18624be21d56SDavid Gibson 
18634be21d56SDavid Gibson     return 0;
18644be21d56SDavid Gibson }
18654be21d56SDavid Gibson 
1866c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1867c573fc03SThomas Huth {
1868c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1869c573fc03SThomas Huth 
1870c573fc03SThomas Huth     close_htab_fd(spapr);
1871c573fc03SThomas Huth }
1872c573fc03SThomas Huth 
18734be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
18744be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
18754be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1876a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1877c573fc03SThomas Huth     .cleanup = htab_cleanup,
18784be21d56SDavid Gibson     .load_state = htab_load,
18794be21d56SDavid Gibson };
18804be21d56SDavid Gibson 
18815b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
18825b2128d2SAlexander Graf                            Error **errp)
18835b2128d2SAlexander Graf {
18845b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
18855b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
18865b2128d2SAlexander Graf }
18875b2128d2SAlexander Graf 
1888224245bfSDavid Gibson /*
1889224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1890224245bfSDavid Gibson  *
1891224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1892224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1893224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1894224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1895224245bfSDavid Gibson  */
1896224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1897224245bfSDavid Gibson {
1898224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1899224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1900224245bfSDavid Gibson 
1901224245bfSDavid Gibson     if (d) {
1902224245bfSDavid Gibson         device_reset(d);
1903224245bfSDavid Gibson     }
1904224245bfSDavid Gibson }
1905224245bfSDavid Gibson 
1906224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1907224245bfSDavid Gibson {
1908224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1909224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1910e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1911224245bfSDavid Gibson     int i;
1912224245bfSDavid Gibson 
1913224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1914224245bfSDavid Gibson         sPAPRDRConnector *drc;
1915224245bfSDavid Gibson         uint64_t addr;
1916224245bfSDavid Gibson 
1917e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1918224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1919224245bfSDavid Gibson                                      addr/lmb_size);
1920224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1921224245bfSDavid Gibson     }
1922224245bfSDavid Gibson }
1923224245bfSDavid Gibson 
1924224245bfSDavid Gibson /*
1925224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1926224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1927224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1928224245bfSDavid Gibson  */
19297c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1930224245bfSDavid Gibson {
1931224245bfSDavid Gibson     int i;
1932224245bfSDavid Gibson 
19337c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
19347c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
19357c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
19367c150d6fSDavid Gibson                    machine->ram_size,
1937224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
19387c150d6fSDavid Gibson         return;
19397c150d6fSDavid Gibson     }
19407c150d6fSDavid Gibson 
19417c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
19427c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
19437c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
19447c150d6fSDavid Gibson                    machine->ram_size,
19457c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
19467c150d6fSDavid Gibson         return;
1947224245bfSDavid Gibson     }
1948224245bfSDavid Gibson 
1949224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1950224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
19517c150d6fSDavid Gibson             error_setg(errp,
19527c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
19537c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
19547c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
1955224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
19567c150d6fSDavid Gibson             return;
1957224245bfSDavid Gibson         }
1958224245bfSDavid Gibson     }
1959224245bfSDavid Gibson }
1960224245bfSDavid Gibson 
1961535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
1962535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1963535455fdSIgor Mammedov {
1964535455fdSIgor Mammedov     int index = id / smp_threads;
1965535455fdSIgor Mammedov 
1966535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
1967535455fdSIgor Mammedov         return NULL;
1968535455fdSIgor Mammedov     }
1969535455fdSIgor Mammedov     if (idx) {
1970535455fdSIgor Mammedov         *idx = index;
1971535455fdSIgor Mammedov     }
1972535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
1973535455fdSIgor Mammedov }
1974535455fdSIgor Mammedov 
19750c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
19760c86d0fdSDavid Gibson {
19770c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
19780c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
19790c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
19800c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
1981535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
1982535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
19830c86d0fdSDavid Gibson     int i;
19840c86d0fdSDavid Gibson 
19850c86d0fdSDavid Gibson     if (!type) {
19860c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
19870c86d0fdSDavid Gibson         exit(1);
19880c86d0fdSDavid Gibson     }
19890c86d0fdSDavid Gibson 
1990535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
1991c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
19920c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
19930c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
19940c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
19950c86d0fdSDavid Gibson             exit(1);
19960c86d0fdSDavid Gibson         }
19970c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
19980c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
19990c86d0fdSDavid Gibson                          max_cpus, smp_threads);
20000c86d0fdSDavid Gibson             exit(1);
20010c86d0fdSDavid Gibson         }
20020c86d0fdSDavid Gibson     } else {
20030c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
20040c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
20050c86d0fdSDavid Gibson             exit(1);
20060c86d0fdSDavid Gibson         }
2007535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
20080c86d0fdSDavid Gibson     }
20090c86d0fdSDavid Gibson 
2010535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
20110c86d0fdSDavid Gibson         int core_id = i * smp_threads;
20120c86d0fdSDavid Gibson 
2013c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
20140c86d0fdSDavid Gibson             sPAPRDRConnector *drc =
20150c86d0fdSDavid Gibson                 spapr_dr_connector_new(OBJECT(spapr),
20160c86d0fdSDavid Gibson                                        SPAPR_DR_CONNECTOR_TYPE_CPU,
20170c86d0fdSDavid Gibson                                        (core_id / smp_threads) * smt);
20180c86d0fdSDavid Gibson 
20190c86d0fdSDavid Gibson             qemu_register_reset(spapr_drc_reset, drc);
20200c86d0fdSDavid Gibson         }
20210c86d0fdSDavid Gibson 
2022535455fdSIgor Mammedov         if (i < boot_cores_nr) {
20230c86d0fdSDavid Gibson             Object *core  = object_new(type);
20240c86d0fdSDavid Gibson             int nr_threads = smp_threads;
20250c86d0fdSDavid Gibson 
20260c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
20270c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
20280c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
20290c86d0fdSDavid Gibson             }
20300c86d0fdSDavid Gibson 
20310c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
20320c86d0fdSDavid Gibson                                     &error_fatal);
20330c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
20340c86d0fdSDavid Gibson                                     &error_fatal);
20350c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
20360c86d0fdSDavid Gibson         }
20370c86d0fdSDavid Gibson     }
20380c86d0fdSDavid Gibson     g_free(type);
20390c86d0fdSDavid Gibson }
20400c86d0fdSDavid Gibson 
204153018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
20423ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
204353018216SPaolo Bonzini {
204428e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
2045224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
20463ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
20473ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
204853018216SPaolo Bonzini     PCIHostState *phb;
204953018216SPaolo Bonzini     int i;
205053018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
205153018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
2052658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
2053658fa66bSAlexey Kardashevskiy     void *rma = NULL;
205453018216SPaolo Bonzini     hwaddr rma_alloc_size;
2055b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
2056b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
205753018216SPaolo Bonzini     char *filename;
205853018216SPaolo Bonzini 
2059226419d6SMichael S. Tsirkin     msi_nonbroken = true;
206053018216SPaolo Bonzini 
206153018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
20620cffce56SDavid Gibson     QTAILQ_INIT(&spapr->pending_dimm_unplugs);
206353018216SPaolo Bonzini 
206453018216SPaolo Bonzini     /* Allocate RMA if necessary */
2065658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
206653018216SPaolo Bonzini 
206753018216SPaolo Bonzini     if (rma_alloc_size == -1) {
2068730fce59SThomas Huth         error_report("Unable to create RMA");
206953018216SPaolo Bonzini         exit(1);
207053018216SPaolo Bonzini     }
207153018216SPaolo Bonzini 
2072c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
207353018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
207453018216SPaolo Bonzini     } else {
2075c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
207653018216SPaolo Bonzini 
207753018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
207853018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
207953018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
208053018216SPaolo Bonzini          *
208153018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
208253018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
208353018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
208453018216SPaolo Bonzini          * isn't determined yet.
208553018216SPaolo Bonzini          */
208653018216SPaolo Bonzini         if (kvm_enabled()) {
208753018216SPaolo Bonzini             spapr->vrma_adjust = 1;
208853018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
208953018216SPaolo Bonzini         }
2090912acdf4SBenjamin Herrenschmidt 
2091912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
2092912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
2093912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
2094912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
2095912acdf4SBenjamin Herrenschmidt          */
2096912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
209753018216SPaolo Bonzini     }
209853018216SPaolo Bonzini 
2099c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
2100d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2101c4177479SAlexey Kardashevskiy                      spapr->rma_size);
2102c4177479SAlexey Kardashevskiy         exit(1);
2103c4177479SAlexey Kardashevskiy     }
2104c4177479SAlexey Kardashevskiy 
2105b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2106b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
210753018216SPaolo Bonzini 
21087b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
210971cd4dacSCédric Le Goater     xics_system_init(machine, XICS_IRQS_SPAPR, &error_fatal);
21107b565160SDavid Gibson 
2111facdb8b6SMichael Roth     /* Set up containers for ibm,client-set-architecture negotiated options */
2112facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2113facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2114facdb8b6SMichael Roth 
2115224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2116facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
21177c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2118224245bfSDavid Gibson     }
2119224245bfSDavid Gibson 
2120417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2121545d6e2bSSuraj Jitindar Singh     if (!kvm_enabled() || kvmppc_has_cap_mmu_radix()) {
2122545d6e2bSSuraj Jitindar Singh         /* KVM and TCG always allow GTSE with radix... */
21239fb4541fSSam Bobroff         spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
21249fb4541fSSam Bobroff     }
21259fb4541fSSam Bobroff     /* ... but not with hash (currently). */
2126417ece33SMichael Roth 
2127ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2128ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2129ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2130ffbb1705SMichael Roth     }
2131ffbb1705SMichael Roth 
213253018216SPaolo Bonzini     /* init CPUs */
213319fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
21343daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
213553018216SPaolo Bonzini     }
213694a94e4cSBharata B Rao 
2137e703d2f7SGreg Kurz     ppc_cpu_parse_features(machine->cpu_model);
2138e703d2f7SGreg Kurz 
21390c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
214053018216SPaolo Bonzini 
2141026bfd89SDavid Gibson     if (kvm_enabled()) {
2142026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2143026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2144ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
21455145ad4fSNathan Whitehorn 
21465145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
21475145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2148026bfd89SDavid Gibson     }
2149026bfd89SDavid Gibson 
215053018216SPaolo Bonzini     /* allocate RAM */
2151f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2152fb164994SDavid Gibson                                          machine->ram_size);
2153f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
215453018216SPaolo Bonzini 
2155658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2156658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2157658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2158658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2159658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2160658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2161658fa66bSAlexey Kardashevskiy     }
2162658fa66bSAlexey Kardashevskiy 
21634a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
21644a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
21654a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
216671c9a3ddSBharata B Rao         /*
216771c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
216871c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
216971c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
217071c9a3ddSBharata B Rao          */
217171c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
217271c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
21734a1c9cf0SBharata B Rao 
217471c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
217571c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
217671c9a3ddSBharata B Rao         }
217771c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2178d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2179d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
218071c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2181d54e4d76SDavid Gibson             exit(1);
21824a1c9cf0SBharata B Rao         }
21834a1c9cf0SBharata B Rao 
21844a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
21854a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
21864a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
21874a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
21884a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
21894a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
21904a1c9cf0SBharata B Rao     }
21914a1c9cf0SBharata B Rao 
2192224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2193224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2194224245bfSDavid Gibson     }
2195224245bfSDavid Gibson 
219653018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
21974c56440dSStefan Weil     if (!filename) {
2198730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
21994c56440dSStefan Weil         exit(1);
22004c56440dSStefan Weil     }
2201b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
22028afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
22038afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
22048afc22a2SZhou Jie         exit(1);
22058afc22a2SZhou Jie     }
2206b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2207b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2208730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
220953018216SPaolo Bonzini         exit(1);
221053018216SPaolo Bonzini     }
221153018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2212730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
22132f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
221453018216SPaolo Bonzini         exit(1);
221553018216SPaolo Bonzini     }
221653018216SPaolo Bonzini     g_free(filename);
221753018216SPaolo Bonzini 
2218ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
221953018216SPaolo Bonzini     spapr_events_init(spapr);
222053018216SPaolo Bonzini 
222112f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
222228df36a1SDavid Gibson     spapr_rtc_create(spapr);
222312f42174SDavid Gibson 
222453018216SPaolo Bonzini     /* Set up VIO bus */
222553018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
222653018216SPaolo Bonzini 
222753018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
222853018216SPaolo Bonzini         if (serial_hds[i]) {
222953018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
223053018216SPaolo Bonzini         }
223153018216SPaolo Bonzini     }
223253018216SPaolo Bonzini 
223353018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
223453018216SPaolo Bonzini     spapr_create_nvram(spapr);
223553018216SPaolo Bonzini 
223653018216SPaolo Bonzini     /* Set up PCI */
223753018216SPaolo Bonzini     spapr_pci_rtas_init();
223853018216SPaolo Bonzini 
223989dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
224053018216SPaolo Bonzini 
224153018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
224253018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
224353018216SPaolo Bonzini 
224453018216SPaolo Bonzini         if (!nd->model) {
224553018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
224653018216SPaolo Bonzini         }
224753018216SPaolo Bonzini 
224853018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
224953018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
225053018216SPaolo Bonzini         } else {
225129b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
225253018216SPaolo Bonzini         }
225353018216SPaolo Bonzini     }
225453018216SPaolo Bonzini 
225553018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
225653018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
225753018216SPaolo Bonzini     }
225853018216SPaolo Bonzini 
225953018216SPaolo Bonzini     /* Graphics */
226014c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
226153018216SPaolo Bonzini         spapr->has_graphics = true;
2262c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
226353018216SPaolo Bonzini     }
226453018216SPaolo Bonzini 
22654ee9ced9SMarcel Apfelbaum     if (machine->usb) {
226657040d45SThomas Huth         if (smc->use_ohci_by_default) {
226753018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
226857040d45SThomas Huth         } else {
226957040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
227057040d45SThomas Huth         }
2271c86580b8SMarkus Armbruster 
227253018216SPaolo Bonzini         if (spapr->has_graphics) {
2273c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2274c86580b8SMarkus Armbruster 
2275c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2276c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
227753018216SPaolo Bonzini         }
227853018216SPaolo Bonzini     }
227953018216SPaolo Bonzini 
228053018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2281d54e4d76SDavid Gibson         error_report(
2282d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2283d54e4d76SDavid Gibson             MIN_RMA_SLOF);
228453018216SPaolo Bonzini         exit(1);
228553018216SPaolo Bonzini     }
228653018216SPaolo Bonzini 
228753018216SPaolo Bonzini     if (kernel_filename) {
228853018216SPaolo Bonzini         uint64_t lowaddr = 0;
228953018216SPaolo Bonzini 
2290a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2291a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2292a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2293a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2294a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2295a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2296a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
22977ef295eaSPeter Crosthwaite                                           0, 0);
2298a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
229916457e7fSBenjamin Herrenschmidt         }
2300a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2301a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2302a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
230353018216SPaolo Bonzini             exit(1);
230453018216SPaolo Bonzini         }
230553018216SPaolo Bonzini 
230653018216SPaolo Bonzini         /* load initrd */
230753018216SPaolo Bonzini         if (initrd_filename) {
230853018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
230953018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
231053018216SPaolo Bonzini              */
2311a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2312a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2313a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2314a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2315a19f7fb0SDavid Gibson                                                      load_limit
2316a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2317a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2318d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
231953018216SPaolo Bonzini                              initrd_filename);
232053018216SPaolo Bonzini                 exit(1);
232153018216SPaolo Bonzini             }
232253018216SPaolo Bonzini         }
232353018216SPaolo Bonzini     }
232453018216SPaolo Bonzini 
23258e7ea787SAndreas Färber     if (bios_name == NULL) {
23268e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
23278e7ea787SAndreas Färber     }
23288e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
23294c56440dSStefan Weil     if (!filename) {
233068fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
23314c56440dSStefan Weil         exit(1);
23324c56440dSStefan Weil     }
233353018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
233468fea5a0SThomas Huth     if (fw_size <= 0) {
233568fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
233653018216SPaolo Bonzini         exit(1);
233753018216SPaolo Bonzini     }
233853018216SPaolo Bonzini     g_free(filename);
233953018216SPaolo Bonzini 
234028e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
234128e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
234228e02042SDavid Gibson      * which predated MachineState but had a similar function */
23434be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
23444be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
23454be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
23464be21d56SDavid Gibson 
234746503c2bSMichael Roth     /* used by RTAS */
234846503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
234946503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
235046503c2bSMichael Roth 
23515b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
235242043e4fSLaurent Vivier 
235342043e4fSLaurent Vivier     if (kvm_enabled()) {
23543dc410aeSAlexey Kardashevskiy         /* to stop and start vmclock */
235542043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
235642043e4fSLaurent Vivier                                          &spapr->tb);
23573dc410aeSAlexey Kardashevskiy 
23583dc410aeSAlexey Kardashevskiy         kvmppc_spapr_enable_inkernel_multitce();
235942043e4fSLaurent Vivier     }
236053018216SPaolo Bonzini }
236153018216SPaolo Bonzini 
2362135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2363135a129aSAneesh Kumar K.V {
2364135a129aSAneesh Kumar K.V     if (!vm_type) {
2365135a129aSAneesh Kumar K.V         return 0;
2366135a129aSAneesh Kumar K.V     }
2367135a129aSAneesh Kumar K.V 
2368135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2369135a129aSAneesh Kumar K.V         return 1;
2370135a129aSAneesh Kumar K.V     }
2371135a129aSAneesh Kumar K.V 
2372135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2373135a129aSAneesh Kumar K.V         return 2;
2374135a129aSAneesh Kumar K.V     }
2375135a129aSAneesh Kumar K.V 
2376135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2377135a129aSAneesh Kumar K.V     exit(1);
2378135a129aSAneesh Kumar K.V }
2379135a129aSAneesh Kumar K.V 
238071461b0fSAlexey Kardashevskiy /*
2381627b84f4SGonglei  * Implementation of an interface to adjust firmware path
238271461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
238371461b0fSAlexey Kardashevskiy  */
238471461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
238571461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
238671461b0fSAlexey Kardashevskiy {
238771461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
238871461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
238971461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
239071461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
239171461b0fSAlexey Kardashevskiy 
239271461b0fSAlexey Kardashevskiy     if (d) {
239371461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
239471461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
239571461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
239671461b0fSAlexey Kardashevskiy 
239771461b0fSAlexey Kardashevskiy         if (spapr) {
239871461b0fSAlexey Kardashevskiy             /*
239971461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
240071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
240171461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
240271461b0fSAlexey Kardashevskiy              */
240371461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
240471461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
240571461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
240671461b0fSAlexey Kardashevskiy         } else if (virtio) {
240771461b0fSAlexey Kardashevskiy             /*
240871461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
240971461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
241071461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
241171461b0fSAlexey Kardashevskiy              * the actual binding is:
241271461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
241371461b0fSAlexey Kardashevskiy              */
241471461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
241571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
241671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
241771461b0fSAlexey Kardashevskiy         } else if (usb) {
241871461b0fSAlexey Kardashevskiy             /*
241971461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
242071461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
242171461b0fSAlexey Kardashevskiy              */
242271461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
242371461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
242471461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
242571461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
242671461b0fSAlexey Kardashevskiy         }
242771461b0fSAlexey Kardashevskiy     }
242871461b0fSAlexey Kardashevskiy 
2429b99260ebSThomas Huth     /*
2430b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2431b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2432b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2433b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2434b99260ebSThomas Huth      */
2435b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2436b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2437b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2438b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2439b99260ebSThomas Huth         }
2440b99260ebSThomas Huth     }
2441b99260ebSThomas Huth 
244271461b0fSAlexey Kardashevskiy     if (phb) {
244371461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
244471461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
244571461b0fSAlexey Kardashevskiy     }
244671461b0fSAlexey Kardashevskiy 
244771461b0fSAlexey Kardashevskiy     return NULL;
244871461b0fSAlexey Kardashevskiy }
244971461b0fSAlexey Kardashevskiy 
245023825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
245123825581SEduardo Habkost {
245228e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
245323825581SEduardo Habkost 
245428e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
245523825581SEduardo Habkost }
245623825581SEduardo Habkost 
245723825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
245823825581SEduardo Habkost {
245928e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
246023825581SEduardo Habkost 
246128e02042SDavid Gibson     g_free(spapr->kvm_type);
246228e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
246323825581SEduardo Habkost }
246423825581SEduardo Habkost 
2465f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2466f6229214SMichael Roth {
2467f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2468f6229214SMichael Roth 
2469f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2470f6229214SMichael Roth }
2471f6229214SMichael Roth 
2472f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2473f6229214SMichael Roth                                             Error **errp)
2474f6229214SMichael Roth {
2475f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2476f6229214SMichael Roth 
2477f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2478f6229214SMichael Roth }
2479f6229214SMichael Roth 
248023825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
248123825581SEduardo Habkost {
2482715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2483715c5407SDavid Gibson 
2484715c5407SDavid Gibson     spapr->htab_fd = -1;
2485f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
248623825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
248723825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
248849d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
248949d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
249049d2e648SMarcel Apfelbaum                                     NULL);
2491f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2492f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2493f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2494f6229214SMichael Roth                             NULL);
2495f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2496f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2497f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2498f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2499f6229214SMichael Roth                                     NULL);
250023825581SEduardo Habkost }
250123825581SEduardo Habkost 
250287bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
250387bbdd9cSDavid Gibson {
250487bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
250587bbdd9cSDavid Gibson 
250687bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
250787bbdd9cSDavid Gibson }
250887bbdd9cSDavid Gibson 
25091c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
251034316482SAlexey Kardashevskiy {
251134316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
251234316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
251334316482SAlexey Kardashevskiy }
251434316482SAlexey Kardashevskiy 
251534316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
251634316482SAlexey Kardashevskiy {
251734316482SAlexey Kardashevskiy     CPUState *cs;
251834316482SAlexey Kardashevskiy 
251934316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
25201c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
252134316482SAlexey Kardashevskiy     }
252234316482SAlexey Kardashevskiy }
252334316482SAlexey Kardashevskiy 
252479b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
252579b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
252679b78a6bSMichael Roth                            Error **errp)
2527c20d332aSBharata B Rao {
2528c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2529c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2530c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2531c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2532c20d332aSBharata B Rao     void *fdt;
253379b78a6bSMichael Roth     uint64_t addr = addr_start;
2534c20d332aSBharata B Rao 
2535c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2536c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2537c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2538c20d332aSBharata B Rao         g_assert(drc);
2539c20d332aSBharata B Rao 
2540c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2541c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2542c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2543c20d332aSBharata B Rao 
2544c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2545c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2546c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
25475c0139a8SMichael Roth         if (!dev->hotplugged) {
25485c0139a8SMichael Roth             /* guests expect coldplugged LMBs to be pre-allocated */
25495c0139a8SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
25505c0139a8SMichael Roth             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
25515c0139a8SMichael Roth         }
2552c20d332aSBharata B Rao     }
25535dd5238cSJianjun Duan     /* send hotplug notification to the
25545dd5238cSJianjun Duan      * guest only in case of hotplugged memory
25555dd5238cSJianjun Duan      */
25565dd5238cSJianjun Duan     if (dev->hotplugged) {
255779b78a6bSMichael Roth         if (dedicated_hp_event_source) {
255879b78a6bSMichael Roth             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
255979b78a6bSMichael Roth                     addr_start / SPAPR_MEMORY_BLOCK_SIZE);
256079b78a6bSMichael Roth             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
256179b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
256279b78a6bSMichael Roth                                                    nr_lmbs,
256379b78a6bSMichael Roth                                                    drck->get_index(drc));
256479b78a6bSMichael Roth         } else {
256579b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
256679b78a6bSMichael Roth                                            nr_lmbs);
256779b78a6bSMichael Roth         }
2568c20d332aSBharata B Rao     }
25695dd5238cSJianjun Duan }
2570c20d332aSBharata B Rao 
2571c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2572c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2573c20d332aSBharata B Rao {
2574c20d332aSBharata B Rao     Error *local_err = NULL;
2575c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2576c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2577c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2578c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2579c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2580c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2581c20d332aSBharata B Rao     uint64_t addr;
2582df587133SThomas Huth 
2583d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2584c20d332aSBharata B Rao     if (local_err) {
2585c20d332aSBharata B Rao         goto out;
2586c20d332aSBharata B Rao     }
2587c20d332aSBharata B Rao 
2588c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2589c20d332aSBharata B Rao     if (local_err) {
2590c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2591c20d332aSBharata B Rao         goto out;
2592c20d332aSBharata B Rao     }
2593c20d332aSBharata B Rao 
259479b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
259579b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
259679b78a6bSMichael Roth                    &error_abort);
2597c20d332aSBharata B Rao 
2598c20d332aSBharata B Rao out:
2599c20d332aSBharata B Rao     error_propagate(errp, local_err);
2600c20d332aSBharata B Rao }
2601c20d332aSBharata B Rao 
2602c871bc70SLaurent Vivier static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2603c871bc70SLaurent Vivier                                   Error **errp)
2604c871bc70SLaurent Vivier {
2605c871bc70SLaurent Vivier     PCDIMMDevice *dimm = PC_DIMM(dev);
2606c871bc70SLaurent Vivier     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2607c871bc70SLaurent Vivier     MemoryRegion *mr = ddc->get_memory_region(dimm);
2608c871bc70SLaurent Vivier     uint64_t size = memory_region_size(mr);
2609c871bc70SLaurent Vivier     char *mem_dev;
2610c871bc70SLaurent Vivier 
2611c871bc70SLaurent Vivier     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2612c871bc70SLaurent Vivier         error_setg(errp, "Hotplugged memory size must be a multiple of "
2613c871bc70SLaurent Vivier                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
2614c871bc70SLaurent Vivier         return;
2615c871bc70SLaurent Vivier     }
2616c871bc70SLaurent Vivier 
2617c871bc70SLaurent Vivier     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2618c871bc70SLaurent Vivier     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2619c871bc70SLaurent Vivier         error_setg(errp, "Memory backend has bad page size. "
2620c871bc70SLaurent Vivier                    "Use 'memory-backend-file' with correct mem-path.");
2621c871bc70SLaurent Vivier         return;
2622c871bc70SLaurent Vivier     }
2623c871bc70SLaurent Vivier }
2624c871bc70SLaurent Vivier 
26250cffce56SDavid Gibson struct sPAPRDIMMState {
26260cffce56SDavid Gibson     PCDIMMDevice *dimm;
2627cf632463SBharata B Rao     uint32_t nr_lmbs;
26280cffce56SDavid Gibson     QTAILQ_ENTRY(sPAPRDIMMState) next;
26290cffce56SDavid Gibson };
26300cffce56SDavid Gibson 
26310cffce56SDavid Gibson static sPAPRDIMMState *spapr_pending_dimm_unplugs_find(sPAPRMachineState *s,
26320cffce56SDavid Gibson                                                        PCDIMMDevice *dimm)
26330cffce56SDavid Gibson {
26340cffce56SDavid Gibson     sPAPRDIMMState *dimm_state = NULL;
26350cffce56SDavid Gibson 
26360cffce56SDavid Gibson     QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
26370cffce56SDavid Gibson         if (dimm_state->dimm == dimm) {
26380cffce56SDavid Gibson             break;
26390cffce56SDavid Gibson         }
26400cffce56SDavid Gibson     }
26410cffce56SDavid Gibson     return dimm_state;
26420cffce56SDavid Gibson }
26430cffce56SDavid Gibson 
26440cffce56SDavid Gibson static void spapr_pending_dimm_unplugs_add(sPAPRMachineState *spapr,
26450cffce56SDavid Gibson                                            sPAPRDIMMState *dimm_state)
26460cffce56SDavid Gibson {
26470cffce56SDavid Gibson     g_assert(!spapr_pending_dimm_unplugs_find(spapr, dimm_state->dimm));
26480cffce56SDavid Gibson     QTAILQ_INSERT_HEAD(&spapr->pending_dimm_unplugs, dimm_state, next);
26490cffce56SDavid Gibson }
26500cffce56SDavid Gibson 
26510cffce56SDavid Gibson static void spapr_pending_dimm_unplugs_remove(sPAPRMachineState *spapr,
26520cffce56SDavid Gibson                                               sPAPRDIMMState *dimm_state)
26530cffce56SDavid Gibson {
26540cffce56SDavid Gibson     QTAILQ_REMOVE(&spapr->pending_dimm_unplugs, dimm_state, next);
26550cffce56SDavid Gibson     g_free(dimm_state);
26560cffce56SDavid Gibson }
2657cf632463SBharata B Rao 
265816ee9980SDaniel Henrique Barboza static sPAPRDIMMState *spapr_recover_pending_dimm_state(sPAPRMachineState *ms,
265916ee9980SDaniel Henrique Barboza                                                         PCDIMMDevice *dimm)
266016ee9980SDaniel Henrique Barboza {
266116ee9980SDaniel Henrique Barboza     sPAPRDRConnector *drc;
266216ee9980SDaniel Henrique Barboza     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
266316ee9980SDaniel Henrique Barboza     MemoryRegion *mr = ddc->get_memory_region(dimm);
266416ee9980SDaniel Henrique Barboza     uint64_t size = memory_region_size(mr);
266516ee9980SDaniel Henrique Barboza     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
266616ee9980SDaniel Henrique Barboza     uint32_t avail_lmbs = 0;
266716ee9980SDaniel Henrique Barboza     uint64_t addr_start, addr;
266816ee9980SDaniel Henrique Barboza     int i;
266916ee9980SDaniel Henrique Barboza     sPAPRDIMMState *ds;
267016ee9980SDaniel Henrique Barboza 
267116ee9980SDaniel Henrique Barboza     addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
267216ee9980SDaniel Henrique Barboza                                          &error_abort);
267316ee9980SDaniel Henrique Barboza 
267416ee9980SDaniel Henrique Barboza     addr = addr_start;
267516ee9980SDaniel Henrique Barboza     for (i = 0; i < nr_lmbs; i++) {
267616ee9980SDaniel Henrique Barboza         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
267716ee9980SDaniel Henrique Barboza                                        addr / SPAPR_MEMORY_BLOCK_SIZE);
267816ee9980SDaniel Henrique Barboza         g_assert(drc);
267916ee9980SDaniel Henrique Barboza         if (drc->indicator_state != SPAPR_DR_INDICATOR_STATE_INACTIVE) {
268016ee9980SDaniel Henrique Barboza             avail_lmbs++;
268116ee9980SDaniel Henrique Barboza         }
268216ee9980SDaniel Henrique Barboza         addr += SPAPR_MEMORY_BLOCK_SIZE;
268316ee9980SDaniel Henrique Barboza     }
268416ee9980SDaniel Henrique Barboza 
268516ee9980SDaniel Henrique Barboza     ds = g_malloc0(sizeof(sPAPRDIMMState));
268616ee9980SDaniel Henrique Barboza     ds->nr_lmbs = avail_lmbs;
268716ee9980SDaniel Henrique Barboza     ds->dimm = dimm;
268816ee9980SDaniel Henrique Barboza     spapr_pending_dimm_unplugs_add(ms, ds);
268916ee9980SDaniel Henrique Barboza     return ds;
269016ee9980SDaniel Henrique Barboza }
269116ee9980SDaniel Henrique Barboza 
269231834723SDaniel Henrique Barboza /* Callback to be called during DRC release. */
269331834723SDaniel Henrique Barboza void spapr_lmb_release(DeviceState *dev)
2694cf632463SBharata B Rao {
26950cffce56SDavid Gibson     HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
26960cffce56SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_ctrl);
26970cffce56SDavid Gibson     sPAPRDIMMState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
2698cf632463SBharata B Rao 
269916ee9980SDaniel Henrique Barboza     /* This information will get lost if a migration occurs
270016ee9980SDaniel Henrique Barboza      * during the unplug process. In this case recover it. */
270116ee9980SDaniel Henrique Barboza     if (ds == NULL) {
270216ee9980SDaniel Henrique Barboza         ds = spapr_recover_pending_dimm_state(spapr, PC_DIMM(dev));
270316ee9980SDaniel Henrique Barboza         if (ds->nr_lmbs) {
270416ee9980SDaniel Henrique Barboza             return;
270516ee9980SDaniel Henrique Barboza         }
270616ee9980SDaniel Henrique Barboza     } else if (--ds->nr_lmbs) {
2707cf632463SBharata B Rao         return;
2708cf632463SBharata B Rao     }
2709cf632463SBharata B Rao 
27100cffce56SDavid Gibson     spapr_pending_dimm_unplugs_remove(spapr, ds);
2711cf632463SBharata B Rao 
2712cf632463SBharata B Rao     /*
2713cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
2714cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
2715cf632463SBharata B Rao      */
2716cf632463SBharata B Rao     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2717cf632463SBharata B Rao }
2718cf632463SBharata B Rao 
2719cf632463SBharata B Rao static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2720cf632463SBharata B Rao                                 Error **errp)
2721cf632463SBharata B Rao {
2722cf632463SBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2723cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2724cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2725cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2726cf632463SBharata B Rao 
2727cf632463SBharata B Rao     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2728cf632463SBharata B Rao     object_unparent(OBJECT(dev));
2729cf632463SBharata B Rao }
2730cf632463SBharata B Rao 
2731cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
2732cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
2733cf632463SBharata B Rao {
27340cffce56SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
2735cf632463SBharata B Rao     Error *local_err = NULL;
2736cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2737cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2738cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2739cf632463SBharata B Rao     uint64_t size = memory_region_size(mr);
27400cffce56SDavid Gibson     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
27410cffce56SDavid Gibson     uint64_t addr_start, addr;
27420cffce56SDavid Gibson     int i;
27430cffce56SDavid Gibson     sPAPRDRConnector *drc;
27440cffce56SDavid Gibson     sPAPRDRConnectorClass *drck;
27450cffce56SDavid Gibson     sPAPRDIMMState *ds;
2746cf632463SBharata B Rao 
27470cffce56SDavid Gibson     addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
27480cffce56SDavid Gibson                                          &local_err);
2749cf632463SBharata B Rao     if (local_err) {
2750cf632463SBharata B Rao         goto out;
2751cf632463SBharata B Rao     }
2752cf632463SBharata B Rao 
27530cffce56SDavid Gibson     ds = g_malloc0(sizeof(sPAPRDIMMState));
27540cffce56SDavid Gibson     ds->nr_lmbs = nr_lmbs;
27550cffce56SDavid Gibson     ds->dimm = dimm;
27560cffce56SDavid Gibson     spapr_pending_dimm_unplugs_add(spapr, ds);
27570cffce56SDavid Gibson 
27580cffce56SDavid Gibson     addr = addr_start;
27590cffce56SDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
27600cffce56SDavid Gibson         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
27610cffce56SDavid Gibson                 addr / SPAPR_MEMORY_BLOCK_SIZE);
27620cffce56SDavid Gibson         g_assert(drc);
27630cffce56SDavid Gibson 
27640cffce56SDavid Gibson         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
276531834723SDaniel Henrique Barboza         drck->detach(drc, dev, errp);
27660cffce56SDavid Gibson         addr += SPAPR_MEMORY_BLOCK_SIZE;
27670cffce56SDavid Gibson     }
27680cffce56SDavid Gibson 
27690cffce56SDavid Gibson     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
27700cffce56SDavid Gibson                                    addr_start / SPAPR_MEMORY_BLOCK_SIZE);
27710cffce56SDavid Gibson     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
27720cffce56SDavid Gibson     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
27730cffce56SDavid Gibson                                               nr_lmbs,
27740cffce56SDavid Gibson                                               drck->get_index(drc));
2775cf632463SBharata B Rao out:
2776cf632463SBharata B Rao     error_propagate(errp, local_err);
2777cf632463SBharata B Rao }
2778cf632463SBharata B Rao 
2779af81cf32SBharata B Rao void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2780af81cf32SBharata B Rao                                     sPAPRMachineState *spapr)
2781af81cf32SBharata B Rao {
2782af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2783af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2784af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2785af81cf32SBharata B Rao     void *fdt;
2786af81cf32SBharata B Rao     int offset, fdt_size;
2787af81cf32SBharata B Rao     char *nodename;
2788af81cf32SBharata B Rao 
2789af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2790af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2791af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2792af81cf32SBharata B Rao 
2793af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2794af81cf32SBharata B Rao     g_free(nodename);
2795af81cf32SBharata B Rao 
2796af81cf32SBharata B Rao     *fdt_offset = offset;
2797af81cf32SBharata B Rao     return fdt;
2798af81cf32SBharata B Rao }
2799af81cf32SBharata B Rao 
2800115debf2SIgor Mammedov static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2801115debf2SIgor Mammedov                               Error **errp)
2802ff9006ddSIgor Mammedov {
2803535455fdSIgor Mammedov     MachineState *ms = MACHINE(qdev_get_machine());
2804ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2805535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
2806ff9006ddSIgor Mammedov 
280707572c06SGreg Kurz     assert(core_slot);
2808535455fdSIgor Mammedov     core_slot->cpu = NULL;
2809ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
2810ff9006ddSIgor Mammedov }
2811ff9006ddSIgor Mammedov 
281231834723SDaniel Henrique Barboza /* Callback to be called during DRC release. */
281331834723SDaniel Henrique Barboza void spapr_core_release(DeviceState *dev)
2814115debf2SIgor Mammedov {
2815115debf2SIgor Mammedov     HotplugHandler *hotplug_ctrl;
2816115debf2SIgor Mammedov 
2817115debf2SIgor Mammedov     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2818115debf2SIgor Mammedov     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2819115debf2SIgor Mammedov }
2820115debf2SIgor Mammedov 
2821115debf2SIgor Mammedov static
2822115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
2823ff9006ddSIgor Mammedov                                Error **errp)
2824ff9006ddSIgor Mammedov {
2825535455fdSIgor Mammedov     int index;
2826535455fdSIgor Mammedov     sPAPRDRConnector *drc;
2827ff9006ddSIgor Mammedov     sPAPRDRConnectorClass *drck;
2828ff9006ddSIgor Mammedov     Error *local_err = NULL;
2829535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2830535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
2831ff9006ddSIgor Mammedov 
2832535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
2833535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2834535455fdSIgor Mammedov                    cc->core_id);
2835535455fdSIgor Mammedov         return;
2836535455fdSIgor Mammedov     }
2837ff9006ddSIgor Mammedov     if (index == 0) {
2838ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
2839ff9006ddSIgor Mammedov         return;
2840ff9006ddSIgor Mammedov     }
2841ff9006ddSIgor Mammedov 
2842535455fdSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2843ff9006ddSIgor Mammedov     g_assert(drc);
2844ff9006ddSIgor Mammedov 
2845ff9006ddSIgor Mammedov     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
284631834723SDaniel Henrique Barboza     drck->detach(drc, dev, &local_err);
2847ff9006ddSIgor Mammedov     if (local_err) {
2848ff9006ddSIgor Mammedov         error_propagate(errp, local_err);
2849ff9006ddSIgor Mammedov         return;
2850ff9006ddSIgor Mammedov     }
2851ff9006ddSIgor Mammedov 
2852ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
2853ff9006ddSIgor Mammedov }
2854ff9006ddSIgor Mammedov 
2855ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2856ff9006ddSIgor Mammedov                             Error **errp)
2857ff9006ddSIgor Mammedov {
2858ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
2859ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
2860ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
2861ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2862ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
2863ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
2864ff9006ddSIgor Mammedov     Error *local_err = NULL;
2865ff9006ddSIgor Mammedov     void *fdt = NULL;
2866ff9006ddSIgor Mammedov     int fdt_offset = 0;
2867ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
2868535455fdSIgor Mammedov     CPUArchId *core_slot;
2869535455fdSIgor Mammedov     int index;
2870ff9006ddSIgor Mammedov 
2871535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2872535455fdSIgor Mammedov     if (!core_slot) {
2873535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2874535455fdSIgor Mammedov                    cc->core_id);
2875535455fdSIgor Mammedov         return;
2876535455fdSIgor Mammedov     }
2877ff9006ddSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2878ff9006ddSIgor Mammedov 
2879c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
2880ff9006ddSIgor Mammedov 
2881ff9006ddSIgor Mammedov     /*
2882ff9006ddSIgor Mammedov      * Setup CPU DT entries only for hotplugged CPUs. For boot time or
2883ff9006ddSIgor Mammedov      * coldplugged CPUs DT entries are setup in spapr_build_fdt().
2884ff9006ddSIgor Mammedov      */
2885ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2886ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
2887ff9006ddSIgor Mammedov     }
2888ff9006ddSIgor Mammedov 
2889ff9006ddSIgor Mammedov     if (drc) {
2890ff9006ddSIgor Mammedov         sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2891ff9006ddSIgor Mammedov         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, &local_err);
2892ff9006ddSIgor Mammedov         if (local_err) {
2893ff9006ddSIgor Mammedov             g_free(fdt);
2894ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
2895ff9006ddSIgor Mammedov             return;
2896ff9006ddSIgor Mammedov         }
2897ff9006ddSIgor Mammedov     }
2898ff9006ddSIgor Mammedov 
2899ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2900ff9006ddSIgor Mammedov         /*
2901ff9006ddSIgor Mammedov          * Send hotplug notification interrupt to the guest only in case
2902ff9006ddSIgor Mammedov          * of hotplugged CPUs.
2903ff9006ddSIgor Mammedov          */
2904ff9006ddSIgor Mammedov         spapr_hotplug_req_add_by_index(drc);
2905ff9006ddSIgor Mammedov     } else {
2906ff9006ddSIgor Mammedov         /*
2907ff9006ddSIgor Mammedov          * Set the right DRC states for cold plugged CPU.
2908ff9006ddSIgor Mammedov          */
2909ff9006ddSIgor Mammedov         if (drc) {
2910ff9006ddSIgor Mammedov             sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2911ff9006ddSIgor Mammedov             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
2912ff9006ddSIgor Mammedov             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
2913ff9006ddSIgor Mammedov         }
2914ff9006ddSIgor Mammedov     }
2915535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
2916ff9006ddSIgor Mammedov }
2917ff9006ddSIgor Mammedov 
2918ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2919ff9006ddSIgor Mammedov                                 Error **errp)
2920ff9006ddSIgor Mammedov {
2921ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
2922ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
2923ff9006ddSIgor Mammedov     Error *local_err = NULL;
2924ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2925ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
2926ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
2927535455fdSIgor Mammedov     CPUArchId *core_slot;
2928535455fdSIgor Mammedov     int index;
2929ff9006ddSIgor Mammedov 
2930c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
2931ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
2932ff9006ddSIgor Mammedov         goto out;
2933ff9006ddSIgor Mammedov     }
2934ff9006ddSIgor Mammedov 
2935ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
2936ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
2937ff9006ddSIgor Mammedov         goto out;
2938ff9006ddSIgor Mammedov     }
2939ff9006ddSIgor Mammedov 
2940ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
2941ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
2942ff9006ddSIgor Mammedov         goto out;
2943ff9006ddSIgor Mammedov     }
2944ff9006ddSIgor Mammedov 
2945459264efSDavid Gibson     /*
2946459264efSDavid Gibson      * In general we should have homogeneous threads-per-core, but old
2947459264efSDavid Gibson      * (pre hotplug support) machine types allow the last core to have
2948459264efSDavid Gibson      * reduced threads as a compatibility hack for when we allowed
2949459264efSDavid Gibson      * total vcpus not a multiple of threads-per-core.
2950459264efSDavid Gibson      */
2951459264efSDavid Gibson     if (mc->has_hotpluggable_cpus && (cc->nr_threads != smp_threads)) {
29528149e299SDavid Gibson         error_setg(errp, "invalid nr-threads %d, must be %d",
29538149e299SDavid Gibson                    cc->nr_threads, smp_threads);
29548149e299SDavid Gibson         return;
29558149e299SDavid Gibson     }
29568149e299SDavid Gibson 
2957535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2958535455fdSIgor Mammedov     if (!core_slot) {
2959ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
2960ff9006ddSIgor Mammedov         goto out;
2961ff9006ddSIgor Mammedov     }
2962ff9006ddSIgor Mammedov 
2963535455fdSIgor Mammedov     if (core_slot->cpu) {
2964ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
2965ff9006ddSIgor Mammedov         goto out;
2966ff9006ddSIgor Mammedov     }
2967ff9006ddSIgor Mammedov 
2968a0ceb640SIgor Mammedov     numa_cpu_pre_plug(core_slot, dev, &local_err);
29690b8497f0SIgor Mammedov 
2970ff9006ddSIgor Mammedov out:
2971ff9006ddSIgor Mammedov     g_free(base_core_type);
2972ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
2973ff9006ddSIgor Mammedov }
2974ff9006ddSIgor Mammedov 
2975c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2976c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2977c20d332aSBharata B Rao {
2978c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2979c20d332aSBharata B Rao 
2980c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2981b556854bSBharata B Rao         int node;
2982c20d332aSBharata B Rao 
2983c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2984c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2985c20d332aSBharata B Rao             return;
2986c20d332aSBharata B Rao         }
2987c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2988c20d332aSBharata B Rao         if (*errp) {
2989c20d332aSBharata B Rao             return;
2990c20d332aSBharata B Rao         }
29911a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
29921a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
29931a5512bbSGonglei             return;
29941a5512bbSGonglei         }
2995c20d332aSBharata B Rao 
2996b556854bSBharata B Rao         /*
2997b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2998b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2999b556854bSBharata B Rao          * to the first node that has some memory. This causes two
3000b556854bSBharata B Rao          * unexpected behaviours for the user.
3001b556854bSBharata B Rao          *
3002b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
3003b556854bSBharata B Rao          *   specified.
3004b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
3005b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
3006b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
3007b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
3008b556854bSBharata B Rao          *
3009b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
3010b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
3011b556854bSBharata B Rao          */
3012b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
3013b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
3014b556854bSBharata B Rao                        node);
3015b556854bSBharata B Rao             return;
3016b556854bSBharata B Rao         }
3017b556854bSBharata B Rao 
3018c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
3019af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3020af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
3021c20d332aSBharata B Rao     }
3022c20d332aSBharata B Rao }
3023c20d332aSBharata B Rao 
3024c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
3025c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
3026c20d332aSBharata B Rao {
3027cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
30283c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
30296f4b5c3eSBharata B Rao 
3030c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3031cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3032cf632463SBharata B Rao             spapr_memory_unplug(hotplug_dev, dev, errp);
3033cf632463SBharata B Rao         } else {
3034cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
3035cf632463SBharata B Rao         }
3036cf632463SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3037c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
3038cf632463SBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
3039cf632463SBharata B Rao             return;
3040cf632463SBharata B Rao         }
3041cf632463SBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
3042cf632463SBharata B Rao     }
3043cf632463SBharata B Rao }
3044cf632463SBharata B Rao 
3045cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
3046cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
3047cf632463SBharata B Rao {
3048cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
3049cf632463SBharata B Rao     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
3050cf632463SBharata B Rao 
3051cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3052cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3053cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
3054cf632463SBharata B Rao         } else {
3055cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
3056cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
3057cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
3058cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
3059cf632463SBharata B Rao              * eventually handled by the guest after boot
3060cf632463SBharata B Rao              */
3061cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
3062cf632463SBharata B Rao         }
30636f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3064c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
30656f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
30666f4b5c3eSBharata B Rao             return;
30676f4b5c3eSBharata B Rao         }
3068115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
3069c20d332aSBharata B Rao     }
3070c20d332aSBharata B Rao }
3071c20d332aSBharata B Rao 
307294a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
307394a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
307494a94e4cSBharata B Rao {
3075c871bc70SLaurent Vivier     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3076c871bc70SLaurent Vivier         spapr_memory_pre_plug(hotplug_dev, dev, errp);
3077c871bc70SLaurent Vivier     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
307894a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
307994a94e4cSBharata B Rao     }
308094a94e4cSBharata B Rao }
308194a94e4cSBharata B Rao 
30827ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
3083c20d332aSBharata B Rao                                                  DeviceState *dev)
3084c20d332aSBharata B Rao {
308594a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
308694a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3087c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
3088c20d332aSBharata B Rao     }
3089c20d332aSBharata B Rao     return NULL;
3090c20d332aSBharata B Rao }
3091c20d332aSBharata B Rao 
3092ea089eebSIgor Mammedov static CpuInstanceProperties
3093ea089eebSIgor Mammedov spapr_cpu_index_to_props(MachineState *machine, unsigned cpu_index)
309420bb648dSDavid Gibson {
3095ea089eebSIgor Mammedov     CPUArchId *core_slot;
3096ea089eebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3097ea089eebSIgor Mammedov 
3098ea089eebSIgor Mammedov     /* make sure possible_cpu are intialized */
3099ea089eebSIgor Mammedov     mc->possible_cpu_arch_ids(machine);
3100ea089eebSIgor Mammedov     /* get CPU core slot containing thread that matches cpu_index */
3101ea089eebSIgor Mammedov     core_slot = spapr_find_cpu_slot(machine, cpu_index, NULL);
3102ea089eebSIgor Mammedov     assert(core_slot);
3103ea089eebSIgor Mammedov     return core_slot->props;
310420bb648dSDavid Gibson }
310520bb648dSDavid Gibson 
3106535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
3107535455fdSIgor Mammedov {
3108535455fdSIgor Mammedov     int i;
3109535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
3110535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3111535455fdSIgor Mammedov 
3112c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
3113535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
3114535455fdSIgor Mammedov     }
3115535455fdSIgor Mammedov     if (machine->possible_cpus) {
3116535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
3117535455fdSIgor Mammedov         return machine->possible_cpus;
3118535455fdSIgor Mammedov     }
3119535455fdSIgor Mammedov 
3120535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
3121535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
3122535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
3123535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
3124535455fdSIgor Mammedov         int core_id = i * smp_threads;
3125535455fdSIgor Mammedov 
3126f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
3127535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
3128535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
3129535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
3130ea089eebSIgor Mammedov 
3131ea089eebSIgor Mammedov         /* default distribution of CPUs over NUMA nodes */
3132ea089eebSIgor Mammedov         if (nb_numa_nodes) {
3133ea089eebSIgor Mammedov             /* preset values but do not enable them i.e. 'has_node_id = false',
3134ea089eebSIgor Mammedov              * numa init code will enable them later if manual mapping wasn't
3135ea089eebSIgor Mammedov              * present on CLI */
3136ea089eebSIgor Mammedov             machine->possible_cpus->cpus[i].props.node_id =
3137ea089eebSIgor Mammedov                 core_id / smp_threads / smp_cores % nb_numa_nodes;
3138ea089eebSIgor Mammedov         }
3139535455fdSIgor Mammedov     }
3140535455fdSIgor Mammedov     return machine->possible_cpus;
3141535455fdSIgor Mammedov }
3142535455fdSIgor Mammedov 
31436737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
3144daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
3145daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
31466737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
31476737d9adSDavid Gibson {
3148357d1e3bSDavid Gibson     /*
3149357d1e3bSDavid Gibson      * New-style PHB window placement.
3150357d1e3bSDavid Gibson      *
3151357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
3152357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
3153357d1e3bSDavid Gibson      * windows.
3154357d1e3bSDavid Gibson      *
3155357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
3156357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
3157357d1e3bSDavid Gibson      *
3158357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
3159357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
3160357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
3161357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
3162357d1e3bSDavid Gibson      */
31636737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
316425e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
316525e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
31666737d9adSDavid Gibson     int i;
31676737d9adSDavid Gibson 
3168357d1e3bSDavid Gibson     /* Sanity check natural alignments */
3169357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3170357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3171357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
3172357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
3173357d1e3bSDavid Gibson     /* Sanity check bounds */
317425e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
317525e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
317625e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
317725e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
31782efff1c0SDavid Gibson 
317925e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
318025e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
318125e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
31826737d9adSDavid Gibson         return;
31836737d9adSDavid Gibson     }
31846737d9adSDavid Gibson 
31856737d9adSDavid Gibson     *buid = base_buid + index;
31866737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
31876737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
31886737d9adSDavid Gibson     }
31896737d9adSDavid Gibson 
3190357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
3191357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
3192357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
31936737d9adSDavid Gibson }
31946737d9adSDavid Gibson 
31957844e12bSCédric Le Goater static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
31967844e12bSCédric Le Goater {
31977844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
31987844e12bSCédric Le Goater 
31997844e12bSCédric Le Goater     return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
32007844e12bSCédric Le Goater }
32017844e12bSCédric Le Goater 
32027844e12bSCédric Le Goater static void spapr_ics_resend(XICSFabric *dev)
32037844e12bSCédric Le Goater {
32047844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
32057844e12bSCédric Le Goater 
32067844e12bSCédric Le Goater     ics_resend(spapr->ics);
32077844e12bSCédric Le Goater }
32087844e12bSCédric Le Goater 
320906747ba6SCédric Le Goater static ICPState *spapr_icp_get(XICSFabric *xi, int cpu_dt_id)
3210b2fc59aaSCédric Le Goater {
32115bc8d26dSCédric Le Goater     PowerPCCPU *cpu = ppc_get_vcpu_by_dt_id(cpu_dt_id);
3212b2fc59aaSCédric Le Goater 
32135bc8d26dSCédric Le Goater     return cpu ? ICP(cpu->intc) : NULL;
3214b2fc59aaSCédric Le Goater }
3215b2fc59aaSCédric Le Goater 
32166449da45SCédric Le Goater static void spapr_pic_print_info(InterruptStatsProvider *obj,
32176449da45SCédric Le Goater                                  Monitor *mon)
32186449da45SCédric Le Goater {
32196449da45SCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
32205bc8d26dSCédric Le Goater     CPUState *cs;
32216449da45SCédric Le Goater 
32225bc8d26dSCédric Le Goater     CPU_FOREACH(cs) {
32235bc8d26dSCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
32245bc8d26dSCédric Le Goater 
32255bc8d26dSCédric Le Goater         icp_pic_print_info(ICP(cpu->intc), mon);
32266449da45SCédric Le Goater     }
32276449da45SCédric Le Goater 
32286449da45SCédric Le Goater     ics_pic_print_info(spapr->ics, mon);
32296449da45SCédric Le Goater }
32306449da45SCédric Le Goater 
323129ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
323253018216SPaolo Bonzini {
323329ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
3234224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
323571461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
323634316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
3237c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
32381d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
32397844e12bSCédric Le Goater     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
32406449da45SCédric Le Goater     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
324129ee3247SAlexey Kardashevskiy 
32420eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
3243fc9f38c3SDavid Gibson 
3244fc9f38c3SDavid Gibson     /*
3245fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
3246fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
3247fc9f38c3SDavid Gibson      * these details for backwards compatibility
3248fc9f38c3SDavid Gibson      */
3249958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
3250958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
3251958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
32526244bb7eSGreg Kurz     mc->max_cpus = 1024;
3253958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
32545b2128d2SAlexander Graf     mc->default_boot_order = "";
3255a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
3256958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
32579e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
3258e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
32597ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
326094a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
3261c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
3262c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
3263ea089eebSIgor Mammedov     mc->cpu_index_to_instance_props = spapr_cpu_index_to_props;
3264535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3265cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
326600b4fbe2SMarcel Apfelbaum 
3267fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
32683daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
3269c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
327071461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
327134316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
32726737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
32731d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3274e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3275e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3276e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3277e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
32789861bb3eSSuraj Jitindar Singh     vhc->get_patbe = spapr_get_patbe;
32797844e12bSCédric Le Goater     xic->ics_get = spapr_ics_get;
32807844e12bSCédric Le Goater     xic->ics_resend = spapr_ics_resend;
3281b2fc59aaSCédric Le Goater     xic->icp_get = spapr_icp_get;
32826449da45SCédric Le Goater     ispc->print_info = spapr_pic_print_info;
328355641213SLaurent Vivier     /* Force NUMA node memory size to be a multiple of
328455641213SLaurent Vivier      * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
328555641213SLaurent Vivier      * in which LMBs are represented and hot-added
328655641213SLaurent Vivier      */
328755641213SLaurent Vivier     mc->numa_mem_align_shift = 28;
328853018216SPaolo Bonzini }
328953018216SPaolo Bonzini 
329029ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
329129ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
329229ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
32934aee7362SDavid Gibson     .abstract      = true,
32946ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
329523825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
329687bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3297183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
329829ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
329971461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
330071461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
330134316482SAlexey Kardashevskiy         { TYPE_NMI },
3302c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
33031d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
33047844e12bSCédric Le Goater         { TYPE_XICS_FABRIC },
33056449da45SCédric Le Goater         { TYPE_INTERRUPT_STATS_PROVIDER },
330671461b0fSAlexey Kardashevskiy         { }
330771461b0fSAlexey Kardashevskiy     },
330829ee3247SAlexey Kardashevskiy };
330929ee3247SAlexey Kardashevskiy 
3310fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
33115013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
33125013c547SDavid Gibson                                                     void *data)      \
33135013c547SDavid Gibson     {                                                                \
33145013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
33155013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3316fccbc785SDavid Gibson         if (latest) {                                                \
3317fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3318fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3319fccbc785SDavid Gibson         }                                                            \
33205013c547SDavid Gibson     }                                                                \
33215013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
33225013c547SDavid Gibson     {                                                                \
33235013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
33245013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
33255013c547SDavid Gibson     }                                                                \
33265013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
33275013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
33285013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
33295013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
33305013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
33315013c547SDavid Gibson     };                                                               \
33325013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
33335013c547SDavid Gibson     {                                                                \
33345013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
33355013c547SDavid Gibson     }                                                                \
33360e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
33375013c547SDavid Gibson 
33381c5f29bbSDavid Gibson /*
33393fa14fbeSDavid Gibson  * pseries-2.10
3340db800b21SDavid Gibson  */
33413fa14fbeSDavid Gibson static void spapr_machine_2_10_instance_options(MachineState *machine)
3342db800b21SDavid Gibson {
3343db800b21SDavid Gibson }
3344db800b21SDavid Gibson 
33453fa14fbeSDavid Gibson static void spapr_machine_2_10_class_options(MachineClass *mc)
3346db800b21SDavid Gibson {
3347db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
3348db800b21SDavid Gibson }
3349db800b21SDavid Gibson 
33503fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_10, "2.10", true);
33513fa14fbeSDavid Gibson 
33523fa14fbeSDavid Gibson /*
33533fa14fbeSDavid Gibson  * pseries-2.9
33543fa14fbeSDavid Gibson  */
33553fa14fbeSDavid Gibson #define SPAPR_COMPAT_2_9                                               \
33563fa14fbeSDavid Gibson     HW_COMPAT_2_9
33573fa14fbeSDavid Gibson 
33583fa14fbeSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
33593fa14fbeSDavid Gibson {
33603fa14fbeSDavid Gibson     spapr_machine_2_10_instance_options(machine);
33613fa14fbeSDavid Gibson }
33623fa14fbeSDavid Gibson 
33633fa14fbeSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
33643fa14fbeSDavid Gibson {
33653fa14fbeSDavid Gibson     spapr_machine_2_10_class_options(mc);
33663fa14fbeSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_9);
33673bfe5716SLaurent Vivier     mc->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
33683fa14fbeSDavid Gibson }
33693fa14fbeSDavid Gibson 
33703fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
3371fa325e6cSDavid Gibson 
3372fa325e6cSDavid Gibson /*
3373fa325e6cSDavid Gibson  * pseries-2.8
3374fa325e6cSDavid Gibson  */
3375fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                                        \
337682516263SDavid Gibson     HW_COMPAT_2_8                                               \
337782516263SDavid Gibson     {                                                           \
337882516263SDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,                 \
337982516263SDavid Gibson         .property = "pcie-extended-configuration-space",        \
338082516263SDavid Gibson         .value    = "off",                                      \
338182516263SDavid Gibson     },
3382fa325e6cSDavid Gibson 
3383fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
3384fa325e6cSDavid Gibson {
3385fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
3386fa325e6cSDavid Gibson }
3387fa325e6cSDavid Gibson 
3388fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
3389fa325e6cSDavid Gibson {
3390fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
3391fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
339255641213SLaurent Vivier     mc->numa_mem_align_shift = 23;
3393fa325e6cSDavid Gibson }
3394fa325e6cSDavid Gibson 
3395fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
3396db800b21SDavid Gibson 
3397db800b21SDavid Gibson /*
33981ea1eefcSBharata B Rao  * pseries-2.7
33991ea1eefcSBharata B Rao  */
3400db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3401db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3402357d1e3bSDavid Gibson     {                                               \
3403357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3404357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3405357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3406357d1e3bSDavid Gibson     },                                              \
3407357d1e3bSDavid Gibson     {                                               \
3408357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3409357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3410357d1e3bSDavid Gibson         .value    = "0",                            \
3411146c11f1SDavid Gibson     },                                              \
3412146c11f1SDavid Gibson     {                                               \
3413146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3414146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3415146c11f1SDavid Gibson         .value    = "on",                           \
34165c4537bdSDavid Gibson     },                                              \
34175c4537bdSDavid Gibson     {                                               \
34185c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
34195c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
34205c4537bdSDavid Gibson         .value    = "on",                           \
3421357d1e3bSDavid Gibson     },
3422357d1e3bSDavid Gibson 
3423357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3424357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3425357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3426357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3427357d1e3bSDavid Gibson {
3428357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3429357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3430357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3431357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3432357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3433357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3434357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3435357d1e3bSDavid Gibson 
3436357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3437357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3438357d1e3bSDavid Gibson     int i;
3439357d1e3bSDavid Gibson 
3440357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3441357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3442357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3443357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3444357d1e3bSDavid Gibson          * regions */
3445357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3446357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3447357d1e3bSDavid Gibson     }
3448357d1e3bSDavid Gibson 
3449357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3450357d1e3bSDavid Gibson 
3451357d1e3bSDavid Gibson     if (index > max_index) {
3452357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3453357d1e3bSDavid Gibson                    max_index);
3454357d1e3bSDavid Gibson         return;
3455357d1e3bSDavid Gibson     }
3456357d1e3bSDavid Gibson 
3457357d1e3bSDavid Gibson     *buid = base_buid + index;
3458357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3459357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3460357d1e3bSDavid Gibson     }
3461357d1e3bSDavid Gibson 
3462357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3463357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3464357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3465357d1e3bSDavid Gibson     /*
3466357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3467357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3468357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3469357d1e3bSDavid Gibson      */
3470357d1e3bSDavid Gibson }
3471db800b21SDavid Gibson 
34721ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
34731ea1eefcSBharata B Rao {
3474f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3475f6229214SMichael Roth 
3476672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3477f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
34781ea1eefcSBharata B Rao }
34791ea1eefcSBharata B Rao 
34801ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
34811ea1eefcSBharata B Rao {
34823daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
34833daa4a9fSThomas Huth 
3484db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
34853daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3486db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3487357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
34881ea1eefcSBharata B Rao }
34891ea1eefcSBharata B Rao 
3490db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
34911ea1eefcSBharata B Rao 
34921ea1eefcSBharata B Rao /*
34934b23699cSDavid Gibson  * pseries-2.6
34944b23699cSDavid Gibson  */
34951ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3496ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3497ae4de14cSAlexey Kardashevskiy     { \
3498ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3499ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3500ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3501ae4de14cSAlexey Kardashevskiy     },
35021ea1eefcSBharata B Rao 
35034b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
35044b23699cSDavid Gibson {
3505672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
35064b23699cSDavid Gibson }
35074b23699cSDavid Gibson 
35084b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
35094b23699cSDavid Gibson {
35101ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3511c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
35121ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
35134b23699cSDavid Gibson }
35144b23699cSDavid Gibson 
35151ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
35164b23699cSDavid Gibson 
35174b23699cSDavid Gibson /*
35181c5f29bbSDavid Gibson  * pseries-2.5
35191c5f29bbSDavid Gibson  */
35204b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
352157c522f4SThomas Huth     HW_COMPAT_2_5 \
352257c522f4SThomas Huth     { \
352357c522f4SThomas Huth         .driver   = "spapr-vlan", \
352457c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
352557c522f4SThomas Huth         .value    = "off", \
352657c522f4SThomas Huth     },
35274b23699cSDavid Gibson 
35285013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
35291c5f29bbSDavid Gibson {
3530672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
35315013c547SDavid Gibson }
35325013c547SDavid Gibson 
35335013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
35345013c547SDavid Gibson {
353557040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
353657040d45SThomas Huth 
35374b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
353857040d45SThomas Huth     smc->use_ohci_by_default = true;
35394b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
35401c5f29bbSDavid Gibson }
35411c5f29bbSDavid Gibson 
35424b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
35431c5f29bbSDavid Gibson 
35441c5f29bbSDavid Gibson /*
35451c5f29bbSDavid Gibson  * pseries-2.4
35461c5f29bbSDavid Gibson  */
354780fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
354880fd50f9SCornelia Huck         HW_COMPAT_2_4
354980fd50f9SCornelia Huck 
35505013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
35511c5f29bbSDavid Gibson {
35525013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
35535013c547SDavid Gibson }
35541c5f29bbSDavid Gibson 
35555013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
35565013c547SDavid Gibson {
3557fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3558fc9f38c3SDavid Gibson 
3559fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3560fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3561f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
35621c5f29bbSDavid Gibson }
35631c5f29bbSDavid Gibson 
3564fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
35651c5f29bbSDavid Gibson 
35661c5f29bbSDavid Gibson /*
35671c5f29bbSDavid Gibson  * pseries-2.3
35681c5f29bbSDavid Gibson  */
356938ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
35707619c7b0SMichael Roth         HW_COMPAT_2_3 \
35717619c7b0SMichael Roth         {\
35727619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
35737619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
35747619c7b0SMichael Roth             .value    = "off",\
35757619c7b0SMichael Roth         },
357638ff32c6SEduardo Habkost 
35775013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
35781c5f29bbSDavid Gibson {
35795013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
35801c5f29bbSDavid Gibson     savevm_skip_section_footers();
35811c5f29bbSDavid Gibson     global_state_set_optional();
358209b5e30dSGreg Kurz     savevm_skip_configuration();
35831c5f29bbSDavid Gibson }
35841c5f29bbSDavid Gibson 
35855013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
35861c5f29bbSDavid Gibson {
3587fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3588f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
35891c5f29bbSDavid Gibson }
3590fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
35911c5f29bbSDavid Gibson 
35921c5f29bbSDavid Gibson /*
35931c5f29bbSDavid Gibson  * pseries-2.2
35941c5f29bbSDavid Gibson  */
35951c5f29bbSDavid Gibson 
3596b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
35974dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3598b194df47SAlexey Kardashevskiy         {\
3599b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3600b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3601b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3602dd754bafSEduardo Habkost         },
3603b194df47SAlexey Kardashevskiy 
36045013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3605b0e966d0SJason Wang {
36065013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3607cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3608b0e966d0SJason Wang }
3609b0e966d0SJason Wang 
36105013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3611b0e966d0SJason Wang {
3612fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3613f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
36141c5f29bbSDavid Gibson }
3615fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
36161c5f29bbSDavid Gibson 
36171c5f29bbSDavid Gibson /*
36181c5f29bbSDavid Gibson  * pseries-2.1
36191c5f29bbSDavid Gibson  */
36201c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
36211c5f29bbSDavid Gibson         HW_COMPAT_2_1
36221c5f29bbSDavid Gibson 
36235013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
36241c5f29bbSDavid Gibson {
36255013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
36261c5f29bbSDavid Gibson }
36271c5f29bbSDavid Gibson 
36285013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
3629b0e966d0SJason Wang {
3630fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
3631f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
36326026db45SAlexey Kardashevskiy }
3633fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
36346026db45SAlexey Kardashevskiy 
363529ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
363629ee3247SAlexey Kardashevskiy {
363729ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
363829ee3247SAlexey Kardashevskiy }
363929ee3247SAlexey Kardashevskiy 
364029ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
3641