xref: /openbmc/qemu/hw/ppc/spapr.c (revision 827b17c4)
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"
29fa98fbfcSSam Bobroff #include "qapi/visitor.h"
3053018216SPaolo Bonzini #include "sysemu/sysemu.h"
31e35704baSEduardo Habkost #include "sysemu/numa.h"
3253018216SPaolo Bonzini #include "hw/hw.h"
3303dd024fSPaolo Bonzini #include "qemu/log.h"
3471461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h"
3553018216SPaolo Bonzini #include "elf.h"
3653018216SPaolo Bonzini #include "net/net.h"
37ad440b4aSAndrew Jones #include "sysemu/device_tree.h"
38fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3953018216SPaolo Bonzini #include "sysemu/cpus.h"
40b3946626SVincent Palatin #include "sysemu/hw_accel.h"
4153018216SPaolo Bonzini #include "kvm_ppc.h"
42c4b63b7cSJuan Quintela #include "migration/misc.h"
4384a899deSJuan Quintela #include "migration/global_state.h"
44f2a8f0a6SJuan Quintela #include "migration/register.h"
454be21d56SDavid Gibson #include "mmu-hash64.h"
46b4db5413SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
473794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4853018216SPaolo Bonzini 
4953018216SPaolo Bonzini #include "hw/boards.h"
500d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
5153018216SPaolo Bonzini #include "hw/loader.h"
5253018216SPaolo Bonzini 
537804c353SCédric Le Goater #include "hw/ppc/fdt.h"
540d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
550d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
560d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
570d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5853018216SPaolo Bonzini #include "hw/pci/msi.h"
5953018216SPaolo Bonzini 
6053018216SPaolo Bonzini #include "hw/pci/pci.h"
6171461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
6271461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
63c4e13492SFelipe Franciosi #include "hw/virtio/vhost-scsi-common.h"
6453018216SPaolo Bonzini 
6553018216SPaolo Bonzini #include "exec/address-spaces.h"
6653018216SPaolo Bonzini #include "hw/usb.h"
6753018216SPaolo Bonzini #include "qemu/config-file.h"
68135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
692a6593cbSAlexey Kardashevskiy #include "trace.h"
7034316482SAlexey Kardashevskiy #include "hw/nmi.h"
716449da45SCédric Le Goater #include "hw/intc/intc.h"
7253018216SPaolo Bonzini 
7368a27b20SMichael S. Tsirkin #include "hw/compat.h"
74f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7594a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
762474bfd4SIgor Mammedov #include "qmp-commands.h"
7768a27b20SMichael S. Tsirkin 
7853018216SPaolo Bonzini #include <libfdt.h>
7953018216SPaolo Bonzini 
8053018216SPaolo Bonzini /* SLOF memory layout:
8153018216SPaolo Bonzini  *
8253018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
8353018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
8453018216SPaolo Bonzini  *
8553018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8653018216SPaolo Bonzini  * and more
8753018216SPaolo Bonzini  *
8853018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8953018216SPaolo Bonzini  */
9038b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
9153018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
92b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
9353018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
9453018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9553018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9653018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9753018216SPaolo Bonzini 
9853018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9953018216SPaolo Bonzini 
10053018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
10153018216SPaolo Bonzini 
10271cd4dacSCédric Le Goater static ICSState *spapr_ics_create(sPAPRMachineState *spapr,
10371cd4dacSCédric Le Goater                                   const char *type_ics,
104817bb6a4SCédric Le Goater                                   int nr_irqs, Error **errp)
105c04d6cfaSAnthony Liguori {
106175d2aa0SGreg Kurz     Error *local_err = NULL;
10771cd4dacSCédric Le Goater     Object *obj;
108c04d6cfaSAnthony Liguori 
10971cd4dacSCédric Le Goater     obj = object_new(type_ics);
110175d2aa0SGreg Kurz     object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort);
111ad265631SGreg Kurz     object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
112ad265631SGreg Kurz                                    &error_abort);
113175d2aa0SGreg Kurz     object_property_set_int(obj, nr_irqs, "nr-irqs", &local_err);
114175d2aa0SGreg Kurz     if (local_err) {
115175d2aa0SGreg Kurz         goto error;
116175d2aa0SGreg Kurz     }
11771cd4dacSCédric Le Goater     object_property_set_bool(obj, true, "realized", &local_err);
118175d2aa0SGreg Kurz     if (local_err) {
119175d2aa0SGreg Kurz         goto error;
120c04d6cfaSAnthony Liguori     }
121c04d6cfaSAnthony Liguori 
12271cd4dacSCédric Le Goater     return ICS_SIMPLE(obj);
123175d2aa0SGreg Kurz 
124175d2aa0SGreg Kurz error:
125175d2aa0SGreg Kurz     error_propagate(errp, local_err);
126175d2aa0SGreg Kurz     return NULL;
1275bc8d26dSCédric Le Goater }
1285bc8d26dSCédric Le Goater 
12946f7afa3SGreg Kurz static bool pre_2_10_vmstate_dummy_icp_needed(void *opaque)
13046f7afa3SGreg Kurz {
13146f7afa3SGreg Kurz     /* Dummy entries correspond to unused ICPState objects in older QEMUs,
13246f7afa3SGreg Kurz      * and newer QEMUs don't even have them. In both cases, we don't want
13346f7afa3SGreg Kurz      * to send anything on the wire.
13446f7afa3SGreg Kurz      */
13546f7afa3SGreg Kurz     return false;
13646f7afa3SGreg Kurz }
13746f7afa3SGreg Kurz 
13846f7afa3SGreg Kurz static const VMStateDescription pre_2_10_vmstate_dummy_icp = {
13946f7afa3SGreg Kurz     .name = "icp/server",
14046f7afa3SGreg Kurz     .version_id = 1,
14146f7afa3SGreg Kurz     .minimum_version_id = 1,
14246f7afa3SGreg Kurz     .needed = pre_2_10_vmstate_dummy_icp_needed,
14346f7afa3SGreg Kurz     .fields = (VMStateField[]) {
14446f7afa3SGreg Kurz         VMSTATE_UNUSED(4), /* uint32_t xirr */
14546f7afa3SGreg Kurz         VMSTATE_UNUSED(1), /* uint8_t pending_priority */
14646f7afa3SGreg Kurz         VMSTATE_UNUSED(1), /* uint8_t mfrr */
14746f7afa3SGreg Kurz         VMSTATE_END_OF_LIST()
14846f7afa3SGreg Kurz     },
14946f7afa3SGreg Kurz };
15046f7afa3SGreg Kurz 
15146f7afa3SGreg Kurz static void pre_2_10_vmstate_register_dummy_icp(int i)
15246f7afa3SGreg Kurz {
15346f7afa3SGreg Kurz     vmstate_register(NULL, i, &pre_2_10_vmstate_dummy_icp,
15446f7afa3SGreg Kurz                      (void *)(uintptr_t) i);
15546f7afa3SGreg Kurz }
15646f7afa3SGreg Kurz 
15746f7afa3SGreg Kurz static void pre_2_10_vmstate_unregister_dummy_icp(int i)
15846f7afa3SGreg Kurz {
15946f7afa3SGreg Kurz     vmstate_unregister(NULL, &pre_2_10_vmstate_dummy_icp,
16046f7afa3SGreg Kurz                        (void *)(uintptr_t) i);
16146f7afa3SGreg Kurz }
16246f7afa3SGreg Kurz 
16346f7afa3SGreg Kurz static inline int xics_max_server_number(void)
16446f7afa3SGreg Kurz {
16546f7afa3SGreg Kurz     return DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(), smp_threads);
16646f7afa3SGreg Kurz }
16746f7afa3SGreg Kurz 
16871cd4dacSCédric Le Goater static void xics_system_init(MachineState *machine, int nr_irqs, Error **errp)
169c04d6cfaSAnthony Liguori {
17071cd4dacSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
17146f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
172c04d6cfaSAnthony Liguori 
17311ad93f6SDavid Gibson     if (kvm_enabled()) {
1742192a930SCédric Le Goater         if (machine_kernel_irqchip_allowed(machine) &&
17571cd4dacSCédric Le Goater             !xics_kvm_init(spapr, errp)) {
17671cd4dacSCédric Le Goater             spapr->icp_type = TYPE_KVM_ICP;
1773d85885aSGreg Kurz             spapr->ics = spapr_ics_create(spapr, TYPE_ICS_KVM, nr_irqs, errp);
17811ad93f6SDavid Gibson         }
17971cd4dacSCédric Le Goater         if (machine_kernel_irqchip_required(machine) && !spapr->ics) {
1803d85885aSGreg Kurz             error_prepend(errp, "kernel_irqchip requested but unavailable: ");
1813d85885aSGreg Kurz             return;
18211ad93f6SDavid Gibson         }
183b83baa60SMarkus Armbruster     }
18411ad93f6SDavid Gibson 
18571cd4dacSCédric Le Goater     if (!spapr->ics) {
186f63ebfe0SGreg Kurz         xics_spapr_init(spapr);
18771cd4dacSCédric Le Goater         spapr->icp_type = TYPE_ICP;
18871cd4dacSCédric Le Goater         spapr->ics = spapr_ics_create(spapr, TYPE_ICS_SIMPLE, nr_irqs, errp);
1893d85885aSGreg Kurz         if (!spapr->ics) {
1903d85885aSGreg Kurz             return;
1913d85885aSGreg Kurz         }
192c04d6cfaSAnthony Liguori     }
19346f7afa3SGreg Kurz 
19446f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
19546f7afa3SGreg Kurz         int i;
19646f7afa3SGreg Kurz 
19746f7afa3SGreg Kurz         for (i = 0; i < xics_max_server_number(); i++) {
19846f7afa3SGreg Kurz             /* Dummy entries get deregistered when real ICPState objects
19946f7afa3SGreg Kurz              * are registered during CPU core hotplug.
20046f7afa3SGreg Kurz              */
20146f7afa3SGreg Kurz             pre_2_10_vmstate_register_dummy_icp(i);
20246f7afa3SGreg Kurz         }
20346f7afa3SGreg Kurz     }
204c04d6cfaSAnthony Liguori }
205c04d6cfaSAnthony Liguori 
206833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
207833d4668SAlexey Kardashevskiy                                   int smt_threads)
208833d4668SAlexey Kardashevskiy {
209833d4668SAlexey Kardashevskiy     int i, ret = 0;
210833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
211833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
2122e886fb3SSam Bobroff     int index = spapr_vcpu_id(cpu);
213833d4668SAlexey Kardashevskiy 
214d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
215d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
2166d9412eaSAlexey Kardashevskiy         if (ret < 0) {
2176d9412eaSAlexey Kardashevskiy             return ret;
2186d9412eaSAlexey Kardashevskiy         }
2196d9412eaSAlexey Kardashevskiy     }
2206d9412eaSAlexey Kardashevskiy 
221833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
222833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
223833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
224833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
225833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
226833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
227833d4668SAlexey Kardashevskiy     }
228833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
229833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
230833d4668SAlexey Kardashevskiy     if (ret < 0) {
231833d4668SAlexey Kardashevskiy         return ret;
232833d4668SAlexey Kardashevskiy     }
233833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
234833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
235833d4668SAlexey Kardashevskiy 
236833d4668SAlexey Kardashevskiy     return ret;
237833d4668SAlexey Kardashevskiy }
238833d4668SAlexey Kardashevskiy 
23999861ecbSIgor Mammedov static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu)
2400da6f3feSBharata B Rao {
2412e886fb3SSam Bobroff     int index = spapr_vcpu_id(cpu);
2420da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
2430da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2440da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2450da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
24615f8b142SIgor Mammedov                                 cpu_to_be32(cpu->node_id),
2470da6f3feSBharata B Rao                                 cpu_to_be32(index)};
2480da6f3feSBharata B Rao 
2490da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
25099861ecbSIgor Mammedov     return fdt_setprop(fdt, offset, "ibm,associativity", associativity,
2510da6f3feSBharata B Rao                           sizeof(associativity));
2520da6f3feSBharata B Rao }
2530da6f3feSBharata B Rao 
25486d5771aSSam Bobroff /* Populate the "ibm,pa-features" property */
255e957f6a9SSam Bobroff static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset,
256e957f6a9SSam Bobroff                                       bool legacy_guest)
25786d5771aSSam Bobroff {
25886d5771aSSam Bobroff     uint8_t pa_features_206[] = { 6, 0,
25986d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
26086d5771aSSam Bobroff     uint8_t pa_features_207[] = { 24, 0,
26186d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
26286d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
26386d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
26486d5771aSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
2659fb4541fSSam Bobroff     uint8_t pa_features_300[] = { 66, 0,
2669fb4541fSSam Bobroff         /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
2679fb4541fSSam Bobroff         /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
26886d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
2699fb4541fSSam Bobroff         /* 6: DS207 */
27086d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
2719fb4541fSSam Bobroff         /* 16: Vector */
27286d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
2739fb4541fSSam Bobroff         /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
2749bf502feSDavid Gibson         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
2759fb4541fSSam Bobroff         /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
2769fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
2779fb4541fSSam Bobroff         /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
2789fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
2799fb4541fSSam Bobroff         /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
2809fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
2819fb4541fSSam Bobroff         /* 42: PM, 44: PC RA, 46: SC vec'd */
2829fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
2839fb4541fSSam Bobroff         /* 48: SIMD, 50: QP BFP, 52: String */
2849fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
2859fb4541fSSam Bobroff         /* 54: DecFP, 56: DecI, 58: SHA */
2869fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
2879fb4541fSSam Bobroff         /* 60: NM atomic, 62: RNG */
2889fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
2899fb4541fSSam Bobroff     };
29086d5771aSSam Bobroff     uint8_t *pa_features;
29186d5771aSSam Bobroff     size_t pa_size;
29286d5771aSSam Bobroff 
29386d5771aSSam Bobroff     switch (POWERPC_MMU_VER(env->mmu_model)) {
29486d5771aSSam Bobroff     case POWERPC_MMU_VER_2_06:
29586d5771aSSam Bobroff         pa_features = pa_features_206;
29686d5771aSSam Bobroff         pa_size = sizeof(pa_features_206);
29786d5771aSSam Bobroff         break;
29886d5771aSSam Bobroff     case POWERPC_MMU_VER_2_07:
29986d5771aSSam Bobroff         pa_features = pa_features_207;
30086d5771aSSam Bobroff         pa_size = sizeof(pa_features_207);
30186d5771aSSam Bobroff         break;
30286d5771aSSam Bobroff     case POWERPC_MMU_VER_3_00:
30386d5771aSSam Bobroff         pa_features = pa_features_300;
30486d5771aSSam Bobroff         pa_size = sizeof(pa_features_300);
30586d5771aSSam Bobroff         break;
30686d5771aSSam Bobroff     default:
30786d5771aSSam Bobroff         return;
30886d5771aSSam Bobroff     }
30986d5771aSSam Bobroff 
31086d5771aSSam Bobroff     if (env->ci_large_pages) {
31186d5771aSSam Bobroff         /*
31286d5771aSSam Bobroff          * Note: we keep CI large pages off by default because a 64K capable
31386d5771aSSam Bobroff          * guest provisioned with large pages might otherwise try to map a qemu
31486d5771aSSam Bobroff          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
31586d5771aSSam Bobroff          * even if that qemu runs on a 4k host.
31686d5771aSSam Bobroff          * We dd this bit back here if we are confident this is not an issue
31786d5771aSSam Bobroff          */
31886d5771aSSam Bobroff         pa_features[3] |= 0x20;
31986d5771aSSam Bobroff     }
32086d5771aSSam Bobroff     if (kvmppc_has_cap_htm() && pa_size > 24) {
32186d5771aSSam Bobroff         pa_features[24] |= 0x80;    /* Transactional memory support */
32286d5771aSSam Bobroff     }
323e957f6a9SSam Bobroff     if (legacy_guest && pa_size > 40) {
324e957f6a9SSam Bobroff         /* Workaround for broken kernels that attempt (guest) radix
325e957f6a9SSam Bobroff          * mode when they can't handle it, if they see the radix bit set
326e957f6a9SSam Bobroff          * in pa-features. So hide it from them. */
327e957f6a9SSam Bobroff         pa_features[40 + 2] &= ~0x80; /* Radix MMU */
328e957f6a9SSam Bobroff     }
32986d5771aSSam Bobroff 
33086d5771aSSam Bobroff     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
33186d5771aSSam Bobroff }
33286d5771aSSam Bobroff 
33328e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
33453018216SPaolo Bonzini {
33582677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
33682677ed2SAlexey Kardashevskiy     CPUState *cs;
33753018216SPaolo Bonzini     char cpu_model[32];
33853018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
33953018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
34053018216SPaolo Bonzini 
34182677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
34282677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
343e957f6a9SSam Bobroff         CPUPPCState *env = &cpu->env;
34482677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
3452e886fb3SSam Bobroff         int index = spapr_vcpu_id(cpu);
34612dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
34753018216SPaolo Bonzini 
3480f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
34953018216SPaolo Bonzini             continue;
35053018216SPaolo Bonzini         }
35153018216SPaolo Bonzini 
35282677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
35353018216SPaolo Bonzini 
35482677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
35582677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
356a4f3885cSGreg Kurz             cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
35782677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
35882677ed2SAlexey Kardashevskiy                 return cpus_offset;
35982677ed2SAlexey Kardashevskiy             }
36082677ed2SAlexey Kardashevskiy         }
36182677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
36282677ed2SAlexey Kardashevskiy         if (offset < 0) {
36382677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
36453018216SPaolo Bonzini             if (offset < 0) {
36553018216SPaolo Bonzini                 return offset;
36653018216SPaolo Bonzini             }
36782677ed2SAlexey Kardashevskiy         }
36853018216SPaolo Bonzini 
3690da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
3700da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
37153018216SPaolo Bonzini         if (ret < 0) {
37253018216SPaolo Bonzini             return ret;
37353018216SPaolo Bonzini         }
37453018216SPaolo Bonzini 
37599861ecbSIgor Mammedov         if (nb_numa_nodes > 1) {
37699861ecbSIgor Mammedov             ret = spapr_fixup_cpu_numa_dt(fdt, offset, cpu);
37753018216SPaolo Bonzini             if (ret < 0) {
37853018216SPaolo Bonzini                 return ret;
37953018216SPaolo Bonzini             }
38099861ecbSIgor Mammedov         }
381833d4668SAlexey Kardashevskiy 
38212dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
383833d4668SAlexey Kardashevskiy         if (ret < 0) {
384833d4668SAlexey Kardashevskiy             return ret;
385833d4668SAlexey Kardashevskiy         }
386e957f6a9SSam Bobroff 
387e957f6a9SSam Bobroff         spapr_populate_pa_features(env, fdt, offset,
388e957f6a9SSam Bobroff                                          spapr->cas_legacy_guest_workaround);
38953018216SPaolo Bonzini     }
39053018216SPaolo Bonzini     return ret;
39153018216SPaolo Bonzini }
39253018216SPaolo Bonzini 
393c86c1affSDaniel Henrique Barboza static hwaddr spapr_node0_size(MachineState *machine)
394b082d65aSAlexey Kardashevskiy {
395b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
396b082d65aSAlexey Kardashevskiy         int i;
397b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
398b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
399fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
400fb164994SDavid Gibson                            machine->ram_size);
401b082d65aSAlexey Kardashevskiy             }
402b082d65aSAlexey Kardashevskiy         }
403b082d65aSAlexey Kardashevskiy     }
404fb164994SDavid Gibson     return machine->ram_size;
405b082d65aSAlexey Kardashevskiy }
406b082d65aSAlexey Kardashevskiy 
407a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
408a1d59c0fSAlexey Kardashevskiy {
409a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
410a1d59c0fSAlexey Kardashevskiy }
41153018216SPaolo Bonzini 
41203d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
41326a8c353SAlexey Kardashevskiy                                        hwaddr size)
41426a8c353SAlexey Kardashevskiy {
41526a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
41626a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
41726a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
418c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
41926a8c353SAlexey Kardashevskiy     };
42026a8c353SAlexey Kardashevskiy     char mem_name[32];
42126a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
42226a8c353SAlexey Kardashevskiy     int off;
42326a8c353SAlexey Kardashevskiy 
42426a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
42526a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
42626a8c353SAlexey Kardashevskiy 
42726a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
42826a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
42926a8c353SAlexey Kardashevskiy     _FDT(off);
43026a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
43126a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
43226a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
43326a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
43426a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
43503d196b7SBharata B Rao     return off;
43626a8c353SAlexey Kardashevskiy }
43726a8c353SAlexey Kardashevskiy 
43828e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
43953018216SPaolo Bonzini {
440fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
4417db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
4427db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
4437db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
4447db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
44553018216SPaolo Bonzini 
4467db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
4477db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
4487db8a127SAlexey Kardashevskiy         nb_nodes = 1;
449fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
4507db8a127SAlexey Kardashevskiy         nodes = &ramnode;
4515fe269b1SPaul Mackerras     }
45253018216SPaolo Bonzini 
4537db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
4547db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
4557db8a127SAlexey Kardashevskiy             continue;
45653018216SPaolo Bonzini         }
457fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
4585fe269b1SPaul Mackerras             node_size = 0;
4595fe269b1SPaul Mackerras         } else {
4607db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
461fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
462fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
4635fe269b1SPaul Mackerras             }
4645fe269b1SPaul Mackerras         }
4657db8a127SAlexey Kardashevskiy         if (!mem_start) {
4667db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
467e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
4687db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
4697db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
4707db8a127SAlexey Kardashevskiy         }
4716010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
4726010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
4736010818cSAlexey Kardashevskiy 
4746010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
4756010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
4766010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
4776010818cSAlexey Kardashevskiy             }
4786010818cSAlexey Kardashevskiy 
4796010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
4806010818cSAlexey Kardashevskiy             node_size -= sizetmp;
4816010818cSAlexey Kardashevskiy             mem_start += sizetmp;
4826010818cSAlexey Kardashevskiy         }
48353018216SPaolo Bonzini     }
48453018216SPaolo Bonzini 
48553018216SPaolo Bonzini     return 0;
48653018216SPaolo Bonzini }
48753018216SPaolo Bonzini 
4880da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4890da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4900da6f3feSBharata B Rao {
4910da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4920da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4930da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4942e886fb3SSam Bobroff     int index = spapr_vcpu_id(cpu);
4950da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4960da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
497afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
498afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
4990da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
5000da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
5010da6f3feSBharata B Rao     size_t page_sizes_prop_size;
50222419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
5030da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
50412dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
505af81cf32SBharata B Rao     sPAPRDRConnector *drc;
506af81cf32SBharata B Rao     int drc_index;
507c64abd1fSSam Bobroff     uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
508c64abd1fSSam Bobroff     int i;
509af81cf32SBharata B Rao 
510fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index);
511af81cf32SBharata B Rao     if (drc) {
5120b55aa91SDavid Gibson         drc_index = spapr_drc_index(drc);
513af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
514af81cf32SBharata B Rao     }
5150da6f3feSBharata B Rao 
5160da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
5170da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
5180da6f3feSBharata B Rao 
5190da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
5200da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
5210da6f3feSBharata B Rao                            env->dcache_line_size)));
5220da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
5230da6f3feSBharata B Rao                            env->dcache_line_size)));
5240da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
5250da6f3feSBharata B Rao                            env->icache_line_size)));
5260da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
5270da6f3feSBharata B Rao                            env->icache_line_size)));
5280da6f3feSBharata B Rao 
5290da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
5300da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
5310da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
5320da6f3feSBharata B Rao     } else {
5333dc6f869SAlistair Francis         warn_report("Unknown L1 dcache size for cpu");
5340da6f3feSBharata B Rao     }
5350da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
5360da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
5370da6f3feSBharata B Rao                                pcc->l1_icache_size)));
5380da6f3feSBharata B Rao     } else {
5393dc6f869SAlistair Francis         warn_report("Unknown L1 icache size for cpu");
5400da6f3feSBharata B Rao     }
5410da6f3feSBharata B Rao 
5420da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
5430da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
544fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
5450da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
5460da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
5470da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
5480da6f3feSBharata B Rao 
5490da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
5500da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
5510da6f3feSBharata B Rao     }
5520da6f3feSBharata B Rao 
5530da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
5540da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
5550da6f3feSBharata B Rao                           segs, sizeof(segs))));
5560da6f3feSBharata B Rao     }
5570da6f3feSBharata B Rao 
5580da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
5590da6f3feSBharata B Rao      *   0 / no property == no vector extensions
5600da6f3feSBharata B Rao      *   1               == VMX / Altivec available
5610da6f3feSBharata B Rao      *   2               == VSX available */
5620da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
5630da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
5640da6f3feSBharata B Rao 
5650da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
5660da6f3feSBharata B Rao     }
5670da6f3feSBharata B Rao 
5680da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
5690da6f3feSBharata B Rao      *   0 / no property == no DFP
5700da6f3feSBharata B Rao      *   1               == DFP available */
5710da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
5720da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
5730da6f3feSBharata B Rao     }
5740da6f3feSBharata B Rao 
5753654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
5760da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
5770da6f3feSBharata B Rao     if (page_sizes_prop_size) {
5780da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
5790da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
5800da6f3feSBharata B Rao     }
5810da6f3feSBharata B Rao 
582e957f6a9SSam Bobroff     spapr_populate_pa_features(env, fdt, offset, false);
58390da0d5aSBenjamin Herrenschmidt 
5840da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
58522419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
5860da6f3feSBharata B Rao 
5870da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
5880da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
5890da6f3feSBharata B Rao 
59099861ecbSIgor Mammedov     if (nb_numa_nodes > 1) {
59199861ecbSIgor Mammedov         _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cpu));
59299861ecbSIgor Mammedov     }
5930da6f3feSBharata B Rao 
59412dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
595c64abd1fSSam Bobroff 
596c64abd1fSSam Bobroff     if (pcc->radix_page_info) {
597c64abd1fSSam Bobroff         for (i = 0; i < pcc->radix_page_info->count; i++) {
598c64abd1fSSam Bobroff             radix_AP_encodings[i] =
599c64abd1fSSam Bobroff                 cpu_to_be32(pcc->radix_page_info->entries[i]);
600c64abd1fSSam Bobroff         }
601c64abd1fSSam Bobroff         _FDT((fdt_setprop(fdt, offset, "ibm,processor-radix-AP-encodings",
602c64abd1fSSam Bobroff                           radix_AP_encodings,
603c64abd1fSSam Bobroff                           pcc->radix_page_info->count *
604c64abd1fSSam Bobroff                           sizeof(radix_AP_encodings[0]))));
605c64abd1fSSam Bobroff     }
6060da6f3feSBharata B Rao }
6070da6f3feSBharata B Rao 
6080da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
6090da6f3feSBharata B Rao {
6100da6f3feSBharata B Rao     CPUState *cs;
6110da6f3feSBharata B Rao     int cpus_offset;
6120da6f3feSBharata B Rao     char *nodename;
6130da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
6140da6f3feSBharata B Rao 
6150da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
6160da6f3feSBharata B Rao     _FDT(cpus_offset);
6170da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
6180da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
6190da6f3feSBharata B Rao 
6200da6f3feSBharata B Rao     /*
6210da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
6220da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
6230da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
6240da6f3feSBharata B Rao      */
6250da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
6260da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
6272e886fb3SSam Bobroff         int index = spapr_vcpu_id(cpu);
6280da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
6290da6f3feSBharata B Rao         int offset;
6300da6f3feSBharata B Rao 
6310da6f3feSBharata B Rao         if ((index % smt) != 0) {
6320da6f3feSBharata B Rao             continue;
6330da6f3feSBharata B Rao         }
6340da6f3feSBharata B Rao 
6350da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
6360da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
6370da6f3feSBharata B Rao         g_free(nodename);
6380da6f3feSBharata B Rao         _FDT(offset);
6390da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
6400da6f3feSBharata B Rao     }
6410da6f3feSBharata B Rao 
6420da6f3feSBharata B Rao }
6430da6f3feSBharata B Rao 
64403d196b7SBharata B Rao /*
64503d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
64603d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
64703d196b7SBharata B Rao  * of this device tree node.
64803d196b7SBharata B Rao  */
64903d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
65003d196b7SBharata B Rao {
65103d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
65203d196b7SBharata B Rao     int ret, i, offset;
65303d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
65403d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
655d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
656d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
657d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
658d0e5a8f2SBharata B Rao                        lmb_size;
65903d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
6606663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
66103d196b7SBharata B Rao 
662ef001f06SThomas Huth     /*
663d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
66416c25aefSBharata B Rao      */
665d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
66616c25aefSBharata B Rao         return 0;
66716c25aefSBharata B Rao     }
66816c25aefSBharata B Rao 
66916c25aefSBharata B Rao     /*
670ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
671ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
672ef001f06SThomas Huth      */
673ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
674ef001f06SThomas Huth               * sizeof(uint32_t);
67503d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
67603d196b7SBharata B Rao 
67703d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
67803d196b7SBharata B Rao 
67903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
68003d196b7SBharata B Rao                     sizeof(prop_lmb_size));
68103d196b7SBharata B Rao     if (ret < 0) {
68203d196b7SBharata B Rao         goto out;
68303d196b7SBharata B Rao     }
68403d196b7SBharata B Rao 
68503d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
68603d196b7SBharata B Rao     if (ret < 0) {
68703d196b7SBharata B Rao         goto out;
68803d196b7SBharata B Rao     }
68903d196b7SBharata B Rao 
69003d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
69103d196b7SBharata B Rao     if (ret < 0) {
69203d196b7SBharata B Rao         goto out;
69303d196b7SBharata B Rao     }
69403d196b7SBharata B Rao 
69503d196b7SBharata B Rao     /* ibm,dynamic-memory */
69603d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
69703d196b7SBharata B Rao     cur_index++;
69803d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
699d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
70003d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
70103d196b7SBharata B Rao 
702d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
703d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
704d0e5a8f2SBharata B Rao 
705fbf55397SDavid Gibson             drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, i);
70603d196b7SBharata B Rao             g_assert(drc);
70703d196b7SBharata B Rao 
70803d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
70903d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
7100b55aa91SDavid Gibson             dynamic_memory[2] = cpu_to_be32(spapr_drc_index(drc));
71103d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
71203d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
713d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
71403d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
71503d196b7SBharata B Rao             } else {
71603d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
71703d196b7SBharata B Rao             }
718d0e5a8f2SBharata B Rao         } else {
719d0e5a8f2SBharata B Rao             /*
720d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
721d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
722d0e5a8f2SBharata B Rao              * and as having no valid DRC.
723d0e5a8f2SBharata B Rao              */
724d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
725d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
726d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
727d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
728d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
729d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
730d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
731d0e5a8f2SBharata B Rao         }
73203d196b7SBharata B Rao 
73303d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
73403d196b7SBharata B Rao     }
73503d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
73603d196b7SBharata B Rao     if (ret < 0) {
73703d196b7SBharata B Rao         goto out;
73803d196b7SBharata B Rao     }
73903d196b7SBharata B Rao 
74003d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
74103d196b7SBharata B Rao     cur_index = int_buf;
7426663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
74303d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
74403d196b7SBharata B Rao     cur_index += 2;
7456663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
74603d196b7SBharata B Rao         uint32_t associativity[] = {
74703d196b7SBharata B Rao             cpu_to_be32(0x0),
74803d196b7SBharata B Rao             cpu_to_be32(0x0),
74903d196b7SBharata B Rao             cpu_to_be32(0x0),
75003d196b7SBharata B Rao             cpu_to_be32(i)
75103d196b7SBharata B Rao         };
75203d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
75303d196b7SBharata B Rao         cur_index += 4;
75403d196b7SBharata B Rao     }
75503d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
75603d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
75703d196b7SBharata B Rao out:
75803d196b7SBharata B Rao     g_free(int_buf);
75903d196b7SBharata B Rao     return ret;
76003d196b7SBharata B Rao }
76103d196b7SBharata B Rao 
7626787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
7636787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
7646787d27bSMichael Roth {
7656787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
766417ece33SMichael Roth     int ret = 0, offset;
7676787d27bSMichael Roth 
7686787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
7696787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
7706787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
7716787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
772417ece33SMichael Roth         if (ret) {
773417ece33SMichael Roth             goto out;
774417ece33SMichael Roth         }
7756787d27bSMichael Roth     }
7766787d27bSMichael Roth 
777417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
778417ece33SMichael Roth     if (offset < 0) {
779417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
780417ece33SMichael Roth         if (offset < 0) {
781417ece33SMichael Roth             return offset;
782417ece33SMichael Roth         }
783417ece33SMichael Roth     }
784417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
785417ece33SMichael Roth                                  "ibm,architecture-vec-5");
786417ece33SMichael Roth 
787417ece33SMichael Roth out:
7886787d27bSMichael Roth     return ret;
7896787d27bSMichael Roth }
7906787d27bSMichael Roth 
79110f12e64SDaniel Henrique Barboza static bool spapr_hotplugged_dev_before_cas(void)
79210f12e64SDaniel Henrique Barboza {
79310f12e64SDaniel Henrique Barboza     Object *drc_container, *obj;
79410f12e64SDaniel Henrique Barboza     ObjectProperty *prop;
79510f12e64SDaniel Henrique Barboza     ObjectPropertyIterator iter;
79610f12e64SDaniel Henrique Barboza 
79710f12e64SDaniel Henrique Barboza     drc_container = container_get(object_get_root(), "/dr-connector");
79810f12e64SDaniel Henrique Barboza     object_property_iter_init(&iter, drc_container);
79910f12e64SDaniel Henrique Barboza     while ((prop = object_property_iter_next(&iter))) {
80010f12e64SDaniel Henrique Barboza         if (!strstart(prop->type, "link<", NULL)) {
80110f12e64SDaniel Henrique Barboza             continue;
80210f12e64SDaniel Henrique Barboza         }
80310f12e64SDaniel Henrique Barboza         obj = object_property_get_link(drc_container, prop->name, NULL);
80410f12e64SDaniel Henrique Barboza         if (spapr_drc_needed(obj)) {
80510f12e64SDaniel Henrique Barboza             return true;
80610f12e64SDaniel Henrique Barboza         }
80710f12e64SDaniel Henrique Barboza     }
80810f12e64SDaniel Henrique Barboza     return false;
80910f12e64SDaniel Henrique Barboza }
81010f12e64SDaniel Henrique Barboza 
81103d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
81203d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
8136787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
81403d196b7SBharata B Rao {
81503d196b7SBharata B Rao     void *fdt, *fdt_skel;
81603d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
81703d196b7SBharata B Rao 
81810f12e64SDaniel Henrique Barboza     if (spapr_hotplugged_dev_before_cas()) {
81910f12e64SDaniel Henrique Barboza         return 1;
82010f12e64SDaniel Henrique Barboza     }
82110f12e64SDaniel Henrique Barboza 
822*827b17c4SGreg Kurz     if (size < sizeof(hdr) || size > FW_MAX_SIZE) {
823*827b17c4SGreg Kurz         error_report("SLOF provided an unexpected CAS buffer size "
824*827b17c4SGreg Kurz                      TARGET_FMT_lu " (min: %zu, max: %u)",
825*827b17c4SGreg Kurz                      size, sizeof(hdr), FW_MAX_SIZE);
826*827b17c4SGreg Kurz         exit(EXIT_FAILURE);
827*827b17c4SGreg Kurz     }
828*827b17c4SGreg Kurz 
82903d196b7SBharata B Rao     size -= sizeof(hdr);
83003d196b7SBharata B Rao 
83110f12e64SDaniel Henrique Barboza     /* Create skeleton */
83203d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
83303d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
83403d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
83503d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
83603d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
83703d196b7SBharata B Rao     fdt = g_malloc0(size);
83803d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
83903d196b7SBharata B Rao     g_free(fdt_skel);
84003d196b7SBharata B Rao 
84103d196b7SBharata B Rao     /* Fixup cpu nodes */
84203d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
84303d196b7SBharata B Rao 
8446787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
8456787d27bSMichael Roth         return -1;
84603d196b7SBharata B Rao     }
84703d196b7SBharata B Rao 
84803d196b7SBharata B Rao     /* Pack resulting tree */
84903d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
85003d196b7SBharata B Rao 
85103d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
85203d196b7SBharata B Rao         trace_spapr_cas_failed(size);
85303d196b7SBharata B Rao         return -1;
85403d196b7SBharata B Rao     }
85503d196b7SBharata B Rao 
85603d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
85703d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
85803d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
85903d196b7SBharata B Rao     g_free(fdt);
86003d196b7SBharata B Rao 
86103d196b7SBharata B Rao     return 0;
86203d196b7SBharata B Rao }
86303d196b7SBharata B Rao 
8643f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
8653f5dabceSDavid Gibson {
8663f5dabceSDavid Gibson     int rtas;
8673f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
8683f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
8693f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
8703f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
8713f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
8723f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
8733f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
8743f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
8753f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
8763f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
8773f5dabceSDavid Gibson     };
8783f5dabceSDavid Gibson 
8793f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
8803f5dabceSDavid Gibson 
8813f5dabceSDavid Gibson     /* hypertas */
8823f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
8833f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
8843f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
8853f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
8863f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
8873f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
8883f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
8893f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
8903f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
8913f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
8923f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
8933f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
8943f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
8953f5dabceSDavid Gibson 
8963f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
8973f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
8983f5dabceSDavid Gibson     }
89930f4b05bSDavid Gibson 
90030f4b05bSDavid Gibson     if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
90130f4b05bSDavid Gibson         add_str(hypertas, "hcall-hpt-resize");
90230f4b05bSDavid Gibson     }
90330f4b05bSDavid Gibson 
9043f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
9053f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
9063f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
9073f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
9083f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
9093f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
9103f5dabceSDavid Gibson 
9113f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
9123f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
9133f5dabceSDavid Gibson 
9143f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
9153f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
9163f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
9173f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
9183f5dabceSDavid Gibson 
9193f5dabceSDavid Gibson     if (msi_nonbroken) {
9203f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
9213f5dabceSDavid Gibson     }
9223f5dabceSDavid Gibson 
9233f5dabceSDavid Gibson     /*
9243f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
9253f5dabceSDavid Gibson      * back to the guest cpu.
9263f5dabceSDavid Gibson      *
9273f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
9283f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
9293f5dabceSDavid Gibson      */
9303f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
9313f5dabceSDavid Gibson 
9323f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
9333f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
9343f5dabceSDavid Gibson 
9353f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
9363f5dabceSDavid Gibson }
9373f5dabceSDavid Gibson 
9389fb4541fSSam Bobroff /* Prepare ibm,arch-vec-5-platform-support, which indicates the MMU features
9399fb4541fSSam Bobroff  * that the guest may request and thus the valid values for bytes 24..26 of
9409fb4541fSSam Bobroff  * option vector 5: */
9419fb4541fSSam Bobroff static void spapr_dt_ov5_platform_support(void *fdt, int chosen)
9429fb4541fSSam Bobroff {
943545d6e2bSSuraj Jitindar Singh     PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
944545d6e2bSSuraj Jitindar Singh 
945f2b14e3aSCédric Le Goater     char val[2 * 4] = {
94621f3f8dbSCédric Le Goater         23, 0x00, /* Xive mode, filled in below. */
9479fb4541fSSam Bobroff         24, 0x00, /* Hash/Radix, filled in below. */
9489fb4541fSSam Bobroff         25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
9499fb4541fSSam Bobroff         26, 0x40, /* Radix options: GTSE == yes. */
9509fb4541fSSam Bobroff     };
9519fb4541fSSam Bobroff 
9529fb4541fSSam Bobroff     if (kvm_enabled()) {
9539fb4541fSSam Bobroff         if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
954f2b14e3aSCédric Le Goater             val[3] = 0x80; /* OV5_MMU_BOTH */
9559fb4541fSSam Bobroff         } else if (kvmppc_has_cap_mmu_radix()) {
956f2b14e3aSCédric Le Goater             val[3] = 0x40; /* OV5_MMU_RADIX_300 */
9579fb4541fSSam Bobroff         } else {
958f2b14e3aSCédric Le Goater             val[3] = 0x00; /* Hash */
9599fb4541fSSam Bobroff         }
9609fb4541fSSam Bobroff     } else {
961545d6e2bSSuraj Jitindar Singh         if (first_ppc_cpu->env.mmu_model & POWERPC_MMU_V3) {
962545d6e2bSSuraj Jitindar Singh             /* V3 MMU supports both hash and radix (with dynamic switching) */
963f2b14e3aSCédric Le Goater             val[3] = 0xC0;
964545d6e2bSSuraj Jitindar Singh         } else {
965545d6e2bSSuraj Jitindar Singh             /* Otherwise we can only do hash */
966f2b14e3aSCédric Le Goater             val[3] = 0x00;
9679fb4541fSSam Bobroff         }
968545d6e2bSSuraj Jitindar Singh     }
9699fb4541fSSam Bobroff     _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
9709fb4541fSSam Bobroff                      val, sizeof(val)));
9719fb4541fSSam Bobroff }
9729fb4541fSSam Bobroff 
9737c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
9747c866c6aSDavid Gibson {
9757c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
9767c866c6aSDavid Gibson     int chosen;
9777c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
9787c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
9797c866c6aSDavid Gibson     size_t cb = 0;
9807c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
9817c866c6aSDavid Gibson 
9827c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
9837c866c6aSDavid Gibson 
9847c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
9857c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
9867c866c6aSDavid Gibson                           spapr->initrd_base));
9877c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
9887c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
9897c866c6aSDavid Gibson 
9907c866c6aSDavid Gibson     if (spapr->kernel_size) {
9917c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
9927c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
9937c866c6aSDavid Gibson 
9947c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
9957c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
9967c866c6aSDavid Gibson         if (spapr->kernel_le) {
9977c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
9987c866c6aSDavid Gibson         }
9997c866c6aSDavid Gibson     }
10007c866c6aSDavid Gibson     if (boot_menu) {
10017c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
10027c866c6aSDavid Gibson     }
10037c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
10047c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
10057c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
10067c866c6aSDavid Gibson 
10077c866c6aSDavid Gibson     if (cb && bootlist) {
10087c866c6aSDavid Gibson         int i;
10097c866c6aSDavid Gibson 
10107c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
10117c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
10127c866c6aSDavid Gibson                 bootlist[i] = ' ';
10137c866c6aSDavid Gibson             }
10147c866c6aSDavid Gibson         }
10157c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
10167c866c6aSDavid Gibson     }
10177c866c6aSDavid Gibson 
10187c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
10197c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
10207c866c6aSDavid Gibson     }
10217c866c6aSDavid Gibson 
10227c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
10237c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
10247c866c6aSDavid Gibson     }
10257c866c6aSDavid Gibson 
10269fb4541fSSam Bobroff     spapr_dt_ov5_platform_support(fdt, chosen);
10279fb4541fSSam Bobroff 
10287c866c6aSDavid Gibson     g_free(stdout_path);
10297c866c6aSDavid Gibson     g_free(bootlist);
10307c866c6aSDavid Gibson }
10317c866c6aSDavid Gibson 
1032fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
1033fca5f2dcSDavid Gibson {
1034fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
1035fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
1036fca5f2dcSDavid Gibson     int hypervisor;
1037fca5f2dcSDavid Gibson     uint8_t hypercall[16];
1038fca5f2dcSDavid Gibson 
1039fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
1040fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
1041fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
1042fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
1043fca5f2dcSDavid Gibson         /*
1044fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
1045fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
1046fca5f2dcSDavid Gibson          */
1047fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
1048fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
1049fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
1050fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
1051fca5f2dcSDavid Gibson         }
1052fca5f2dcSDavid Gibson     }
1053fca5f2dcSDavid Gibson }
1054fca5f2dcSDavid Gibson 
1055997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
105653018216SPaolo Bonzini                              hwaddr rtas_addr,
105753018216SPaolo Bonzini                              hwaddr rtas_size)
105853018216SPaolo Bonzini {
1059c86c1affSDaniel Henrique Barboza     MachineState *machine = MACHINE(spapr);
10603c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
1061c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
10627c866c6aSDavid Gibson     int ret;
106353018216SPaolo Bonzini     void *fdt;
106453018216SPaolo Bonzini     sPAPRPHBState *phb;
1065398a0bd5SDavid Gibson     char *buf;
106653018216SPaolo Bonzini 
1067398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
1068398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
106953018216SPaolo Bonzini 
1070398a0bd5SDavid Gibson     /* Root node */
1071398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
1072398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
1073398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
1074398a0bd5SDavid Gibson 
1075398a0bd5SDavid Gibson     /*
1076398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
1077398a0bd5SDavid Gibson      * and what is the uuid of the guest
1078398a0bd5SDavid Gibson      */
1079398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
1080398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1081398a0bd5SDavid Gibson         g_free(buf);
1082398a0bd5SDavid Gibson     }
1083398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
1084398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1085398a0bd5SDavid Gibson         g_free(buf);
1086398a0bd5SDavid Gibson     }
1087398a0bd5SDavid Gibson 
1088398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
1089398a0bd5SDavid Gibson 
1090398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1091398a0bd5SDavid Gibson     if (qemu_uuid_set) {
1092398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1093398a0bd5SDavid Gibson     }
1094398a0bd5SDavid Gibson     g_free(buf);
1095398a0bd5SDavid Gibson 
1096398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
1097398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1098398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
1099398a0bd5SDavid Gibson     }
1100398a0bd5SDavid Gibson 
1101398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1102398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
110353018216SPaolo Bonzini 
1104fc7e0765SDavid Gibson     /* /interrupt controller */
1105fc7e0765SDavid Gibson     spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
1106fc7e0765SDavid Gibson 
1107e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
1108e8f986fcSBharata B Rao     if (ret < 0) {
1109ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
1110e8f986fcSBharata B Rao         exit(1);
111153018216SPaolo Bonzini     }
111253018216SPaolo Bonzini 
1113bf5a6696SDavid Gibson     /* /vdevice */
1114bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
111553018216SPaolo Bonzini 
11164d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
11174d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
11184d9392beSThomas Huth         if (ret < 0) {
1119ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
11204d9392beSThomas Huth             exit(1);
11214d9392beSThomas Huth         }
11224d9392beSThomas Huth     }
11234d9392beSThomas Huth 
112453018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
112553018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
112653018216SPaolo Bonzini         if (ret < 0) {
1127da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
112853018216SPaolo Bonzini             exit(1);
112953018216SPaolo Bonzini         }
1130da34fed7SThomas Huth     }
113153018216SPaolo Bonzini 
11320da6f3feSBharata B Rao     /* cpus */
11330da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
113453018216SPaolo Bonzini 
1135c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
1136c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1137c20d332aSBharata B Rao     }
1138c20d332aSBharata B Rao 
1139c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
1140af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1141af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1142af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1143af81cf32SBharata B Rao         if (ret < 0) {
1144af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1145af81cf32SBharata B Rao             exit(1);
1146af81cf32SBharata B Rao         }
1147af81cf32SBharata B Rao     }
1148af81cf32SBharata B Rao 
1149ffb1e275SDavid Gibson     /* /event-sources */
1150ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1151ffb1e275SDavid Gibson 
11523f5dabceSDavid Gibson     /* /rtas */
11533f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
11543f5dabceSDavid Gibson 
11557c866c6aSDavid Gibson     /* /chosen */
11567c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1157cf6e5223SDavid Gibson 
1158fca5f2dcSDavid Gibson     /* /hypervisor */
1159fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1160fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1161fca5f2dcSDavid Gibson     }
1162fca5f2dcSDavid Gibson 
1163cf6e5223SDavid Gibson     /* Build memory reserve map */
1164cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1165cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1166cf6e5223SDavid Gibson     }
1167cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1168cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1169cf6e5223SDavid Gibson     }
1170cf6e5223SDavid Gibson 
11716787d27bSMichael Roth     /* ibm,client-architecture-support updates */
11726787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
11736787d27bSMichael Roth     if (ret < 0) {
11746787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
11756787d27bSMichael Roth         exit(1);
11766787d27bSMichael Roth     }
11776787d27bSMichael Roth 
1178997b6cfcSDavid Gibson     return fdt;
117953018216SPaolo Bonzini }
118053018216SPaolo Bonzini 
118153018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
118253018216SPaolo Bonzini {
118353018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
118453018216SPaolo Bonzini }
118553018216SPaolo Bonzini 
11861d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
11871d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
118853018216SPaolo Bonzini {
118953018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
119053018216SPaolo Bonzini 
11918d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
11928d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
11938d04fb55SJan Kiszka 
119453018216SPaolo Bonzini     if (msr_pr) {
119553018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
119653018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
119753018216SPaolo Bonzini     } else {
119853018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
119953018216SPaolo Bonzini     }
120053018216SPaolo Bonzini }
120153018216SPaolo Bonzini 
12029861bb3eSSuraj Jitindar Singh static uint64_t spapr_get_patbe(PPCVirtualHypervisor *vhyp)
12039861bb3eSSuraj Jitindar Singh {
12049861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
12059861bb3eSSuraj Jitindar Singh 
12069861bb3eSSuraj Jitindar Singh     return spapr->patb_entry;
12079861bb3eSSuraj Jitindar Singh }
12089861bb3eSSuraj Jitindar Singh 
1209e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1210e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1211e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1212e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1213e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1214e6b8fd24SSamuel Mendoza-Jonas 
1215715c5407SDavid Gibson /*
1216715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1217715c5407SDavid Gibson  */
1218715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1219715c5407SDavid Gibson {
122014b0d748SGreg Kurz     Error *local_err = NULL;
122114b0d748SGreg Kurz 
1222715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1223715c5407SDavid Gibson         return spapr->htab_fd;
1224715c5407SDavid Gibson     }
1225715c5407SDavid Gibson 
122614b0d748SGreg Kurz     spapr->htab_fd = kvmppc_get_htab_fd(false, 0, &local_err);
1227715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
122814b0d748SGreg Kurz         error_report_err(local_err);
1229715c5407SDavid Gibson     }
1230715c5407SDavid Gibson 
1231715c5407SDavid Gibson     return spapr->htab_fd;
1232715c5407SDavid Gibson }
1233715c5407SDavid Gibson 
1234b4db5413SSuraj Jitindar Singh void close_htab_fd(sPAPRMachineState *spapr)
1235715c5407SDavid Gibson {
1236715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1237715c5407SDavid Gibson         close(spapr->htab_fd);
1238715c5407SDavid Gibson     }
1239715c5407SDavid Gibson     spapr->htab_fd = -1;
1240715c5407SDavid Gibson }
1241715c5407SDavid Gibson 
1242e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1243e57ca75cSDavid Gibson {
1244e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1245e57ca75cSDavid Gibson 
1246e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1247e57ca75cSDavid Gibson }
1248e57ca75cSDavid Gibson 
12491ec26c75SGreg Kurz static target_ulong spapr_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
12501ec26c75SGreg Kurz {
12511ec26c75SGreg Kurz     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
12521ec26c75SGreg Kurz 
12531ec26c75SGreg Kurz     assert(kvm_enabled());
12541ec26c75SGreg Kurz 
12551ec26c75SGreg Kurz     if (!spapr->htab) {
12561ec26c75SGreg Kurz         return 0;
12571ec26c75SGreg Kurz     }
12581ec26c75SGreg Kurz 
12591ec26c75SGreg Kurz     return (target_ulong)(uintptr_t)spapr->htab | (spapr->htab_shift - 18);
12601ec26c75SGreg Kurz }
12611ec26c75SGreg Kurz 
1262e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1263e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1264e57ca75cSDavid Gibson {
1265e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1266e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1267e57ca75cSDavid Gibson 
1268e57ca75cSDavid Gibson     if (!spapr->htab) {
1269e57ca75cSDavid Gibson         /*
1270e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1271e57ca75cSDavid Gibson          */
1272e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1273e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1274e57ca75cSDavid Gibson         return hptes;
1275e57ca75cSDavid Gibson     }
1276e57ca75cSDavid Gibson 
1277e57ca75cSDavid Gibson     /*
1278e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1279e57ca75cSDavid Gibson      * accessible PTEG.
1280e57ca75cSDavid Gibson      */
1281e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1282e57ca75cSDavid Gibson }
1283e57ca75cSDavid Gibson 
1284e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1285e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1286e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1287e57ca75cSDavid Gibson {
1288e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1289e57ca75cSDavid Gibson 
1290e57ca75cSDavid Gibson     if (!spapr->htab) {
1291e57ca75cSDavid Gibson         g_free((void *)hptes);
1292e57ca75cSDavid Gibson     }
1293e57ca75cSDavid Gibson 
1294e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1295e57ca75cSDavid Gibson }
1296e57ca75cSDavid Gibson 
1297e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1298e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1299e57ca75cSDavid Gibson {
1300e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1301e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1302e57ca75cSDavid Gibson 
1303e57ca75cSDavid Gibson     if (!spapr->htab) {
1304e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1305e57ca75cSDavid Gibson     } else {
1306e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1307e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1308e57ca75cSDavid Gibson     }
1309e57ca75cSDavid Gibson }
1310e57ca75cSDavid Gibson 
13110b0b8310SDavid Gibson int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
13128dfe8e7fSDavid Gibson {
13138dfe8e7fSDavid Gibson     int shift;
13148dfe8e7fSDavid Gibson 
13158dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
13168dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
13178dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
13188dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
13198dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
13208dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
13218dfe8e7fSDavid Gibson     return shift;
13228dfe8e7fSDavid Gibson }
13238dfe8e7fSDavid Gibson 
132406ec79e8SBharata B Rao void spapr_free_hpt(sPAPRMachineState *spapr)
132506ec79e8SBharata B Rao {
132606ec79e8SBharata B Rao     g_free(spapr->htab);
132706ec79e8SBharata B Rao     spapr->htab = NULL;
132806ec79e8SBharata B Rao     spapr->htab_shift = 0;
132906ec79e8SBharata B Rao     close_htab_fd(spapr);
133006ec79e8SBharata B Rao }
133106ec79e8SBharata B Rao 
13322772cf6bSDavid Gibson void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1333c5f54f3eSDavid Gibson                           Error **errp)
133453018216SPaolo Bonzini {
1335c5f54f3eSDavid Gibson     long rc;
133653018216SPaolo Bonzini 
1337c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
133806ec79e8SBharata B Rao     spapr_free_hpt(spapr);
133953018216SPaolo Bonzini 
1340c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1341c5f54f3eSDavid Gibson     if (rc < 0) {
1342c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1343c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1344c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1345c5f54f3eSDavid Gibson                          shift);
1346c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1347c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1348c5f54f3eSDavid Gibson     } else if (rc > 0) {
1349c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1350c5f54f3eSDavid Gibson         if (rc != shift) {
1351c5f54f3eSDavid Gibson             error_setg(errp,
1352c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1353c5f54f3eSDavid Gibson                        shift, rc);
13547735fedaSBharata B Rao         }
13557735fedaSBharata B Rao 
135653018216SPaolo Bonzini         spapr->htab_shift = shift;
1357c18ad9a5SDavid Gibson         spapr->htab = NULL;
1358b817772aSBharata B Rao     } else {
1359c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1360c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1361c5f54f3eSDavid Gibson         int i;
136201a57972SSamuel Mendoza-Jonas 
1363c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1364c5f54f3eSDavid Gibson         if (!spapr->htab) {
1365c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1366c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1367c5f54f3eSDavid Gibson             return;
1368b817772aSBharata B Rao         }
1369b817772aSBharata B Rao 
1370c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1371c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1372b817772aSBharata B Rao 
1373c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1374c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
13757735fedaSBharata B Rao         }
137653018216SPaolo Bonzini     }
137753018216SPaolo Bonzini }
137853018216SPaolo Bonzini 
1379b4db5413SSuraj Jitindar Singh void spapr_setup_hpt_and_vrma(sPAPRMachineState *spapr)
1380b4db5413SSuraj Jitindar Singh {
13812772cf6bSDavid Gibson     int hpt_shift;
13822772cf6bSDavid Gibson 
13832772cf6bSDavid Gibson     if ((spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED)
13842772cf6bSDavid Gibson         || (spapr->cas_reboot
13852772cf6bSDavid Gibson             && !spapr_ovec_test(spapr->ov5_cas, OV5_HPT_RESIZE))) {
13862772cf6bSDavid Gibson         hpt_shift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size);
13872772cf6bSDavid Gibson     } else {
13882772cf6bSDavid Gibson         hpt_shift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->ram_size);
13892772cf6bSDavid Gibson     }
13902772cf6bSDavid Gibson     spapr_reallocate_hpt(spapr, hpt_shift, &error_fatal);
13912772cf6bSDavid Gibson 
1392b4db5413SSuraj Jitindar Singh     if (spapr->vrma_adjust) {
1393c86c1affSDaniel Henrique Barboza         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(MACHINE(spapr)),
1394b4db5413SSuraj Jitindar Singh                                           spapr->htab_shift);
1395b4db5413SSuraj Jitindar Singh     }
1396b4db5413SSuraj Jitindar Singh     /* We're setting up a hash table, so that means we're not radix */
1397b4db5413SSuraj Jitindar Singh     spapr->patb_entry = 0;
1398b4db5413SSuraj Jitindar Singh }
1399b4db5413SSuraj Jitindar Singh 
14004f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
14019e3f9733SAlexander Graf {
14029e3f9733SAlexander Graf     bool matched = false;
14039e3f9733SAlexander Graf 
14049e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
14059e3f9733SAlexander Graf         matched = true;
14069e3f9733SAlexander Graf     }
14079e3f9733SAlexander Graf 
14089e3f9733SAlexander Graf     if (!matched) {
14099e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
14109e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
14119e3f9733SAlexander Graf         exit(1);
14129e3f9733SAlexander Graf     }
14139e3f9733SAlexander Graf }
14149e3f9733SAlexander Graf 
141553018216SPaolo Bonzini static void ppc_spapr_reset(void)
141653018216SPaolo Bonzini {
1417c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1418c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1419182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1420b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1421cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1422997b6cfcSDavid Gibson     void *fdt;
1423997b6cfcSDavid Gibson     int rc;
1424259186a7SAndreas Färber 
14259e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
14269e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
14279e3f9733SAlexander Graf 
1428b4db5413SSuraj Jitindar Singh     if (kvm_enabled() && kvmppc_has_cap_mmu_radix()) {
1429b4db5413SSuraj Jitindar Singh         /* If using KVM with radix mode available, VCPUs can be started
1430b4db5413SSuraj Jitindar Singh          * without a HPT because KVM will start them in radix mode.
1431b4db5413SSuraj Jitindar Singh          * Set the GR bit in PATB so that we know there is no HPT. */
1432b4db5413SSuraj Jitindar Singh         spapr->patb_entry = PATBE1_GR;
1433b4db5413SSuraj Jitindar Singh     } else {
1434b4db5413SSuraj Jitindar Singh         spapr_setup_hpt_and_vrma(spapr);
1435c5f54f3eSDavid Gibson     }
143653018216SPaolo Bonzini 
143753018216SPaolo Bonzini     qemu_devices_reset();
143856258174SDaniel Henrique Barboza     spapr_clear_pending_events(spapr);
143953018216SPaolo Bonzini 
1440b7d1f77aSBenjamin Herrenschmidt     /*
1441b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1442b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1443b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1444b7d1f77aSBenjamin Herrenschmidt      */
1445b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1446cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1447cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1448b7d1f77aSBenjamin Herrenschmidt 
14496787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
14506787d27bSMichael Roth      * negotiated options and start from scratch */
14516787d27bSMichael Roth     if (!spapr->cas_reboot) {
14526787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
14536787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
145466d5c492SDavid Gibson 
145566d5c492SDavid Gibson         ppc_set_compat_all(spapr->max_compat_pvr, &error_fatal);
14566787d27bSMichael Roth     }
14576787d27bSMichael Roth 
1458cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
145953018216SPaolo Bonzini 
14602cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1461b7d1f77aSBenjamin Herrenschmidt 
1462997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1463997b6cfcSDavid Gibson 
1464997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1465997b6cfcSDavid Gibson     assert(rc == 0);
1466997b6cfcSDavid Gibson 
1467997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1468997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1469997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1470997b6cfcSDavid Gibson         exit(1);
1471997b6cfcSDavid Gibson     }
1472997b6cfcSDavid Gibson 
1473997b6cfcSDavid Gibson     /* Load the fdt */
1474997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1475cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1476997b6cfcSDavid Gibson     g_free(fdt);
1477997b6cfcSDavid Gibson 
147853018216SPaolo Bonzini     /* Set up the entry state */
1479182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1480cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1481182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1482182735efSAndreas Färber     first_cpu->halted = 0;
14831b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
148453018216SPaolo Bonzini 
14856787d27bSMichael Roth     spapr->cas_reboot = false;
148653018216SPaolo Bonzini }
148753018216SPaolo Bonzini 
148828e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
148953018216SPaolo Bonzini {
14902ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
14913978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
149253018216SPaolo Bonzini 
14933978b863SPaolo Bonzini     if (dinfo) {
14946231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
14956231a6daSMarkus Armbruster                             &error_fatal);
149653018216SPaolo Bonzini     }
149753018216SPaolo Bonzini 
149853018216SPaolo Bonzini     qdev_init_nofail(dev);
149953018216SPaolo Bonzini 
150053018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
150153018216SPaolo Bonzini }
150253018216SPaolo Bonzini 
150328e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
150428df36a1SDavid Gibson {
1505147ff807SCédric Le Goater     object_initialize(&spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC);
1506147ff807SCédric Le Goater     object_property_add_child(OBJECT(spapr), "rtc", OBJECT(&spapr->rtc),
1507147ff807SCédric Le Goater                               &error_fatal);
1508147ff807SCédric Le Goater     object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1509147ff807SCédric Le Goater                               &error_fatal);
1510147ff807SCédric Le Goater     object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1511147ff807SCédric Le Goater                               "date", &error_fatal);
151228df36a1SDavid Gibson }
151328df36a1SDavid Gibson 
151453018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
151514c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
151653018216SPaolo Bonzini {
151753018216SPaolo Bonzini     switch (vga_interface_type) {
151853018216SPaolo Bonzini     case VGA_NONE:
15197effdaa3SMark Wu         return false;
15207effdaa3SMark Wu     case VGA_DEVICE:
15217effdaa3SMark Wu         return true;
152253018216SPaolo Bonzini     case VGA_STD:
1523b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
152453018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
152553018216SPaolo Bonzini     default:
152614c6a894SDavid Gibson         error_setg(errp,
152714c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
152814c6a894SDavid Gibson         return false;
152953018216SPaolo Bonzini     }
153053018216SPaolo Bonzini }
153153018216SPaolo Bonzini 
1532880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1533880ae7deSDavid Gibson {
153428e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1535880ae7deSDavid Gibson     int err = 0;
1536880ae7deSDavid Gibson 
1537a7ff1212SCédric Le Goater     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
15385bc8d26dSCédric Le Goater         CPUState *cs;
15395bc8d26dSCédric Le Goater         CPU_FOREACH(cs) {
15405bc8d26dSCédric Le Goater             PowerPCCPU *cpu = POWERPC_CPU(cs);
15415bc8d26dSCédric Le Goater             icp_resend(ICP(cpu->intc));
1542a7ff1212SCédric Le Goater         }
1543a7ff1212SCédric Le Goater     }
1544a7ff1212SCédric Le Goater 
1545631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1546880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1547880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1548880ae7deSDavid Gibson      * value into the RTC device */
1549880ae7deSDavid Gibson     if (version_id < 3) {
1550147ff807SCédric Le Goater         err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
1551880ae7deSDavid Gibson     }
1552880ae7deSDavid Gibson 
1553d39c90f5SBharata B Rao     if (spapr->patb_entry) {
1554d39c90f5SBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
1555d39c90f5SBharata B Rao         bool radix = !!(spapr->patb_entry & PATBE1_GR);
1556d39c90f5SBharata B Rao         bool gtse = !!(cpu->env.spr[SPR_LPCR] & LPCR_GTSE);
1557d39c90f5SBharata B Rao 
1558d39c90f5SBharata B Rao         err = kvmppc_configure_v3_mmu(cpu, radix, gtse, spapr->patb_entry);
1559d39c90f5SBharata B Rao         if (err) {
1560d39c90f5SBharata B Rao             error_report("Process table config unsupported by the host");
1561d39c90f5SBharata B Rao             return -EINVAL;
1562d39c90f5SBharata B Rao         }
1563d39c90f5SBharata B Rao     }
1564d39c90f5SBharata B Rao 
1565880ae7deSDavid Gibson     return err;
1566880ae7deSDavid Gibson }
1567880ae7deSDavid Gibson 
1568880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1569880ae7deSDavid Gibson {
1570880ae7deSDavid Gibson     return version_id < 3;
1571880ae7deSDavid Gibson }
1572880ae7deSDavid Gibson 
1573fd38804bSDaniel Henrique Barboza static bool spapr_pending_events_needed(void *opaque)
1574fd38804bSDaniel Henrique Barboza {
1575fd38804bSDaniel Henrique Barboza     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1576fd38804bSDaniel Henrique Barboza     return !QTAILQ_EMPTY(&spapr->pending_events);
1577fd38804bSDaniel Henrique Barboza }
1578fd38804bSDaniel Henrique Barboza 
1579fd38804bSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_event_entry = {
1580fd38804bSDaniel Henrique Barboza     .name = "spapr_event_log_entry",
1581fd38804bSDaniel Henrique Barboza     .version_id = 1,
1582fd38804bSDaniel Henrique Barboza     .minimum_version_id = 1,
1583fd38804bSDaniel Henrique Barboza     .fields = (VMStateField[]) {
15845341258eSDavid Gibson         VMSTATE_UINT32(summary, sPAPREventLogEntry),
15855341258eSDavid Gibson         VMSTATE_UINT32(extended_length, sPAPREventLogEntry),
1586fd38804bSDaniel Henrique Barboza         VMSTATE_VBUFFER_ALLOC_UINT32(extended_log, sPAPREventLogEntry, 0,
15875341258eSDavid Gibson                                      NULL, extended_length),
1588fd38804bSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
1589fd38804bSDaniel Henrique Barboza     },
1590fd38804bSDaniel Henrique Barboza };
1591fd38804bSDaniel Henrique Barboza 
1592fd38804bSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_pending_events = {
1593fd38804bSDaniel Henrique Barboza     .name = "spapr_pending_events",
1594fd38804bSDaniel Henrique Barboza     .version_id = 1,
1595fd38804bSDaniel Henrique Barboza     .minimum_version_id = 1,
1596fd38804bSDaniel Henrique Barboza     .needed = spapr_pending_events_needed,
1597fd38804bSDaniel Henrique Barboza     .fields = (VMStateField[]) {
1598fd38804bSDaniel Henrique Barboza         VMSTATE_QTAILQ_V(pending_events, sPAPRMachineState, 1,
1599fd38804bSDaniel Henrique Barboza                          vmstate_spapr_event_entry, sPAPREventLogEntry, next),
1600fd38804bSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
1601fd38804bSDaniel Henrique Barboza     },
1602fd38804bSDaniel Henrique Barboza };
1603fd38804bSDaniel Henrique Barboza 
160462ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
160562ef3760SMichael Roth {
160662ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
160762ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
160862ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
160962ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
161062ef3760SMichael Roth     bool cas_needed;
161162ef3760SMichael Roth 
161262ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
161362ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
161462ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
161562ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
161662ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
161762ef3760SMichael Roth      * negotiatied on the source side.
161862ef3760SMichael Roth      *
161962ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
162062ef3760SMichael Roth      * are the only options available on the current machine/platform.
162162ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
162262ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
162362ef3760SMichael Roth      * compatibility.
162462ef3760SMichael Roth      *
162562ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
162662ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
162762ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
162862ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
162962ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
163062ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
163162ef3760SMichael Roth      *
163262ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
163362ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
163462ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
163562ef3760SMichael Roth      */
163662ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
163762ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
163862ef3760SMichael Roth 
163962ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
164062ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
164162ef3760SMichael Roth      * removed via machine options, which could generate a false positive
164262ef3760SMichael Roth      * that breaks migration.
164362ef3760SMichael Roth      */
164462ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
164562ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
164662ef3760SMichael Roth 
164762ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
164862ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
164962ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
165062ef3760SMichael Roth 
165162ef3760SMichael Roth     return cas_needed;
165262ef3760SMichael Roth }
165362ef3760SMichael Roth 
165462ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
165562ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
165662ef3760SMichael Roth     .version_id = 1,
165762ef3760SMichael Roth     .minimum_version_id = 1,
165862ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
165962ef3760SMichael Roth     .fields = (VMStateField[]) {
166062ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
166162ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
166262ef3760SMichael Roth         VMSTATE_END_OF_LIST()
166362ef3760SMichael Roth     },
166462ef3760SMichael Roth };
166562ef3760SMichael Roth 
16669861bb3eSSuraj Jitindar Singh static bool spapr_patb_entry_needed(void *opaque)
16679861bb3eSSuraj Jitindar Singh {
16689861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = opaque;
16699861bb3eSSuraj Jitindar Singh 
16709861bb3eSSuraj Jitindar Singh     return !!spapr->patb_entry;
16719861bb3eSSuraj Jitindar Singh }
16729861bb3eSSuraj Jitindar Singh 
16739861bb3eSSuraj Jitindar Singh static const VMStateDescription vmstate_spapr_patb_entry = {
16749861bb3eSSuraj Jitindar Singh     .name = "spapr_patb_entry",
16759861bb3eSSuraj Jitindar Singh     .version_id = 1,
16769861bb3eSSuraj Jitindar Singh     .minimum_version_id = 1,
16779861bb3eSSuraj Jitindar Singh     .needed = spapr_patb_entry_needed,
16789861bb3eSSuraj Jitindar Singh     .fields = (VMStateField[]) {
16799861bb3eSSuraj Jitindar Singh         VMSTATE_UINT64(patb_entry, sPAPRMachineState),
16809861bb3eSSuraj Jitindar Singh         VMSTATE_END_OF_LIST()
16819861bb3eSSuraj Jitindar Singh     },
16829861bb3eSSuraj Jitindar Singh };
16839861bb3eSSuraj Jitindar Singh 
16844be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
16854be21d56SDavid Gibson     .name = "spapr",
1686880ae7deSDavid Gibson     .version_id = 3,
16874be21d56SDavid Gibson     .minimum_version_id = 1,
1688880ae7deSDavid Gibson     .post_load = spapr_post_load,
16894be21d56SDavid Gibson     .fields = (VMStateField[]) {
1690880ae7deSDavid Gibson         /* used to be @next_irq */
1691880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
16924be21d56SDavid Gibson 
16934be21d56SDavid Gibson         /* RTC offset */
169428e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1695880ae7deSDavid Gibson 
169628e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
16974be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
16984be21d56SDavid Gibson     },
169962ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
170062ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
17019861bb3eSSuraj Jitindar Singh         &vmstate_spapr_patb_entry,
1702fd38804bSDaniel Henrique Barboza         &vmstate_spapr_pending_events,
170362ef3760SMichael Roth         NULL
170462ef3760SMichael Roth     }
17054be21d56SDavid Gibson };
17064be21d56SDavid Gibson 
17074be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
17084be21d56SDavid Gibson {
170928e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
17104be21d56SDavid Gibson 
17114be21d56SDavid Gibson     /* "Iteration" header */
17123a384297SBharata B Rao     if (!spapr->htab_shift) {
17133a384297SBharata B Rao         qemu_put_be32(f, -1);
17143a384297SBharata B Rao     } else {
17154be21d56SDavid Gibson         qemu_put_be32(f, spapr->htab_shift);
17163a384297SBharata B Rao     }
17174be21d56SDavid Gibson 
1718e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1719e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1720e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1721e68cb8b4SAlexey Kardashevskiy     } else {
17223a384297SBharata B Rao         if (spapr->htab_shift) {
1723e68cb8b4SAlexey Kardashevskiy             assert(kvm_enabled());
17244be21d56SDavid Gibson         }
17253a384297SBharata B Rao     }
17264be21d56SDavid Gibson 
1727e68cb8b4SAlexey Kardashevskiy 
1728e68cb8b4SAlexey Kardashevskiy     return 0;
1729e68cb8b4SAlexey Kardashevskiy }
17304be21d56SDavid Gibson 
1731332f7721SGreg Kurz static void htab_save_chunk(QEMUFile *f, sPAPRMachineState *spapr,
1732332f7721SGreg Kurz                             int chunkstart, int n_valid, int n_invalid)
1733332f7721SGreg Kurz {
1734332f7721SGreg Kurz     qemu_put_be32(f, chunkstart);
1735332f7721SGreg Kurz     qemu_put_be16(f, n_valid);
1736332f7721SGreg Kurz     qemu_put_be16(f, n_invalid);
1737332f7721SGreg Kurz     qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
1738332f7721SGreg Kurz                     HASH_PTE_SIZE_64 * n_valid);
1739332f7721SGreg Kurz }
1740332f7721SGreg Kurz 
1741332f7721SGreg Kurz static void htab_save_end_marker(QEMUFile *f)
1742332f7721SGreg Kurz {
1743332f7721SGreg Kurz     qemu_put_be32(f, 0);
1744332f7721SGreg Kurz     qemu_put_be16(f, 0);
1745332f7721SGreg Kurz     qemu_put_be16(f, 0);
1746332f7721SGreg Kurz }
1747332f7721SGreg Kurz 
174828e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
17494be21d56SDavid Gibson                                  int64_t max_ns)
17504be21d56SDavid Gibson {
1751378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
17524be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
17534be21d56SDavid Gibson     int index = spapr->htab_save_index;
1754bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
17554be21d56SDavid Gibson 
17564be21d56SDavid Gibson     assert(spapr->htab_first_pass);
17574be21d56SDavid Gibson 
17584be21d56SDavid Gibson     do {
17594be21d56SDavid Gibson         int chunkstart;
17604be21d56SDavid Gibson 
17614be21d56SDavid Gibson         /* Consume invalid HPTEs */
17624be21d56SDavid Gibson         while ((index < htabslots)
17634be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
17644be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
176524ec2863SMarc-André Lureau             index++;
17664be21d56SDavid Gibson         }
17674be21d56SDavid Gibson 
17684be21d56SDavid Gibson         /* Consume valid HPTEs */
17694be21d56SDavid Gibson         chunkstart = index;
1770338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
17714be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
17724be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
177324ec2863SMarc-André Lureau             index++;
17744be21d56SDavid Gibson         }
17754be21d56SDavid Gibson 
17764be21d56SDavid Gibson         if (index > chunkstart) {
17774be21d56SDavid Gibson             int n_valid = index - chunkstart;
17784be21d56SDavid Gibson 
1779332f7721SGreg Kurz             htab_save_chunk(f, spapr, chunkstart, n_valid, 0);
17804be21d56SDavid Gibson 
1781378bc217SDavid Gibson             if (has_timeout &&
1782378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
17834be21d56SDavid Gibson                 break;
17844be21d56SDavid Gibson             }
17854be21d56SDavid Gibson         }
17864be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
17874be21d56SDavid Gibson 
17884be21d56SDavid Gibson     if (index >= htabslots) {
17894be21d56SDavid Gibson         assert(index == htabslots);
17904be21d56SDavid Gibson         index = 0;
17914be21d56SDavid Gibson         spapr->htab_first_pass = false;
17924be21d56SDavid Gibson     }
17934be21d56SDavid Gibson     spapr->htab_save_index = index;
17944be21d56SDavid Gibson }
17954be21d56SDavid Gibson 
179628e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
17974be21d56SDavid Gibson                                 int64_t max_ns)
17984be21d56SDavid Gibson {
17994be21d56SDavid Gibson     bool final = max_ns < 0;
18004be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
18014be21d56SDavid Gibson     int examined = 0, sent = 0;
18024be21d56SDavid Gibson     int index = spapr->htab_save_index;
1803bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
18044be21d56SDavid Gibson 
18054be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
18064be21d56SDavid Gibson 
18074be21d56SDavid Gibson     do {
18084be21d56SDavid Gibson         int chunkstart, invalidstart;
18094be21d56SDavid Gibson 
18104be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
18114be21d56SDavid Gibson         while ((index < htabslots)
18124be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
18134be21d56SDavid Gibson             index++;
18144be21d56SDavid Gibson             examined++;
18154be21d56SDavid Gibson         }
18164be21d56SDavid Gibson 
18174be21d56SDavid Gibson         chunkstart = index;
18184be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1819338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
18204be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
18214be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
18224be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
18234be21d56SDavid Gibson             index++;
18244be21d56SDavid Gibson             examined++;
18254be21d56SDavid Gibson         }
18264be21d56SDavid Gibson 
18274be21d56SDavid Gibson         invalidstart = index;
18284be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1829338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
18304be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
18314be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
18324be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
18334be21d56SDavid Gibson             index++;
18344be21d56SDavid Gibson             examined++;
18354be21d56SDavid Gibson         }
18364be21d56SDavid Gibson 
18374be21d56SDavid Gibson         if (index > chunkstart) {
18384be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
18394be21d56SDavid Gibson             int n_invalid = index - invalidstart;
18404be21d56SDavid Gibson 
1841332f7721SGreg Kurz             htab_save_chunk(f, spapr, chunkstart, n_valid, n_invalid);
18424be21d56SDavid Gibson             sent += index - chunkstart;
18434be21d56SDavid Gibson 
1844bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
18454be21d56SDavid Gibson                 break;
18464be21d56SDavid Gibson             }
18474be21d56SDavid Gibson         }
18484be21d56SDavid Gibson 
18494be21d56SDavid Gibson         if (examined >= htabslots) {
18504be21d56SDavid Gibson             break;
18514be21d56SDavid Gibson         }
18524be21d56SDavid Gibson 
18534be21d56SDavid Gibson         if (index >= htabslots) {
18544be21d56SDavid Gibson             assert(index == htabslots);
18554be21d56SDavid Gibson             index = 0;
18564be21d56SDavid Gibson         }
18574be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
18584be21d56SDavid Gibson 
18594be21d56SDavid Gibson     if (index >= htabslots) {
18604be21d56SDavid Gibson         assert(index == htabslots);
18614be21d56SDavid Gibson         index = 0;
18624be21d56SDavid Gibson     }
18634be21d56SDavid Gibson 
18644be21d56SDavid Gibson     spapr->htab_save_index = index;
18654be21d56SDavid Gibson 
1866e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
18674be21d56SDavid Gibson }
18684be21d56SDavid Gibson 
1869e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1870e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1871e68cb8b4SAlexey Kardashevskiy 
18724be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
18734be21d56SDavid Gibson {
187428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1875715c5407SDavid Gibson     int fd;
1876e68cb8b4SAlexey Kardashevskiy     int rc = 0;
18774be21d56SDavid Gibson 
18784be21d56SDavid Gibson     /* Iteration header */
18793a384297SBharata B Rao     if (!spapr->htab_shift) {
18803a384297SBharata B Rao         qemu_put_be32(f, -1);
1881e8cd4247SLaurent Vivier         return 1;
18823a384297SBharata B Rao     } else {
18834be21d56SDavid Gibson         qemu_put_be32(f, 0);
18843a384297SBharata B Rao     }
18854be21d56SDavid Gibson 
1886e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1887e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1888e68cb8b4SAlexey Kardashevskiy 
1889715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1890715c5407SDavid Gibson         if (fd < 0) {
1891715c5407SDavid Gibson             return fd;
189201a57972SSamuel Mendoza-Jonas         }
189301a57972SSamuel Mendoza-Jonas 
1894715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1895e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1896e68cb8b4SAlexey Kardashevskiy             return rc;
1897e68cb8b4SAlexey Kardashevskiy         }
1898e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
18994be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
19004be21d56SDavid Gibson     } else {
1901e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
19024be21d56SDavid Gibson     }
19034be21d56SDavid Gibson 
1904332f7721SGreg Kurz     htab_save_end_marker(f);
19054be21d56SDavid Gibson 
1906e68cb8b4SAlexey Kardashevskiy     return rc;
19074be21d56SDavid Gibson }
19084be21d56SDavid Gibson 
19094be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
19104be21d56SDavid Gibson {
191128e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1912715c5407SDavid Gibson     int fd;
19134be21d56SDavid Gibson 
19144be21d56SDavid Gibson     /* Iteration header */
19153a384297SBharata B Rao     if (!spapr->htab_shift) {
19163a384297SBharata B Rao         qemu_put_be32(f, -1);
19173a384297SBharata B Rao         return 0;
19183a384297SBharata B Rao     } else {
19194be21d56SDavid Gibson         qemu_put_be32(f, 0);
19203a384297SBharata B Rao     }
19214be21d56SDavid Gibson 
1922e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1923e68cb8b4SAlexey Kardashevskiy         int rc;
1924e68cb8b4SAlexey Kardashevskiy 
1925e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1926e68cb8b4SAlexey Kardashevskiy 
1927715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1928715c5407SDavid Gibson         if (fd < 0) {
1929715c5407SDavid Gibson             return fd;
193001a57972SSamuel Mendoza-Jonas         }
193101a57972SSamuel Mendoza-Jonas 
1932715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1933e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1934e68cb8b4SAlexey Kardashevskiy             return rc;
1935e68cb8b4SAlexey Kardashevskiy         }
1936e68cb8b4SAlexey Kardashevskiy     } else {
1937378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1938378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1939378bc217SDavid Gibson         }
19404be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1941e68cb8b4SAlexey Kardashevskiy     }
19424be21d56SDavid Gibson 
19434be21d56SDavid Gibson     /* End marker */
1944332f7721SGreg Kurz     htab_save_end_marker(f);
19454be21d56SDavid Gibson 
19464be21d56SDavid Gibson     return 0;
19474be21d56SDavid Gibson }
19484be21d56SDavid Gibson 
19494be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
19504be21d56SDavid Gibson {
195128e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
19524be21d56SDavid Gibson     uint32_t section_hdr;
1953e68cb8b4SAlexey Kardashevskiy     int fd = -1;
195414b0d748SGreg Kurz     Error *local_err = NULL;
19554be21d56SDavid Gibson 
19564be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
195798a5d100SDavid Gibson         error_report("htab_load() bad version");
19584be21d56SDavid Gibson         return -EINVAL;
19594be21d56SDavid Gibson     }
19604be21d56SDavid Gibson 
19614be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
19624be21d56SDavid Gibson 
19633a384297SBharata B Rao     if (section_hdr == -1) {
19643a384297SBharata B Rao         spapr_free_hpt(spapr);
19653a384297SBharata B Rao         return 0;
19663a384297SBharata B Rao     }
19673a384297SBharata B Rao 
19684be21d56SDavid Gibson     if (section_hdr) {
1969c5f54f3eSDavid Gibson         /* First section gives the htab size */
1970c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1971c5f54f3eSDavid Gibson         if (local_err) {
1972c5f54f3eSDavid Gibson             error_report_err(local_err);
19734be21d56SDavid Gibson             return -EINVAL;
19744be21d56SDavid Gibson         }
19754be21d56SDavid Gibson         return 0;
19764be21d56SDavid Gibson     }
19774be21d56SDavid Gibson 
1978e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1979e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1980e68cb8b4SAlexey Kardashevskiy 
198114b0d748SGreg Kurz         fd = kvmppc_get_htab_fd(true, 0, &local_err);
1982e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
198314b0d748SGreg Kurz             error_report_err(local_err);
198482be8e73SGreg Kurz             return fd;
1985e68cb8b4SAlexey Kardashevskiy         }
1986e68cb8b4SAlexey Kardashevskiy     }
1987e68cb8b4SAlexey Kardashevskiy 
19884be21d56SDavid Gibson     while (true) {
19894be21d56SDavid Gibson         uint32_t index;
19904be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
19914be21d56SDavid Gibson 
19924be21d56SDavid Gibson         index = qemu_get_be32(f);
19934be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
19944be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
19954be21d56SDavid Gibson 
19964be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
19974be21d56SDavid Gibson             /* End of Stream */
19984be21d56SDavid Gibson             break;
19994be21d56SDavid Gibson         }
20004be21d56SDavid Gibson 
2001e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
20024be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
20034be21d56SDavid Gibson             /* Bad index in stream */
200498a5d100SDavid Gibson             error_report(
200598a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
200698a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
20074be21d56SDavid Gibson             return -EINVAL;
20084be21d56SDavid Gibson         }
20094be21d56SDavid Gibson 
2010e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
20114be21d56SDavid Gibson             if (n_valid) {
20124be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
20134be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
20144be21d56SDavid Gibson             }
20154be21d56SDavid Gibson             if (n_invalid) {
20164be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
20174be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
20184be21d56SDavid Gibson             }
2019e68cb8b4SAlexey Kardashevskiy         } else {
2020e68cb8b4SAlexey Kardashevskiy             int rc;
2021e68cb8b4SAlexey Kardashevskiy 
2022e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
2023e68cb8b4SAlexey Kardashevskiy 
2024e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
2025e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
2026e68cb8b4SAlexey Kardashevskiy                 return rc;
2027e68cb8b4SAlexey Kardashevskiy             }
2028e68cb8b4SAlexey Kardashevskiy         }
2029e68cb8b4SAlexey Kardashevskiy     }
2030e68cb8b4SAlexey Kardashevskiy 
2031e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
2032e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
2033e68cb8b4SAlexey Kardashevskiy         close(fd);
20344be21d56SDavid Gibson     }
20354be21d56SDavid Gibson 
20364be21d56SDavid Gibson     return 0;
20374be21d56SDavid Gibson }
20384be21d56SDavid Gibson 
203970f794fcSJuan Quintela static void htab_save_cleanup(void *opaque)
2040c573fc03SThomas Huth {
2041c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
2042c573fc03SThomas Huth 
2043c573fc03SThomas Huth     close_htab_fd(spapr);
2044c573fc03SThomas Huth }
2045c573fc03SThomas Huth 
20464be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
20479907e842SJuan Quintela     .save_setup = htab_save_setup,
20484be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
2049a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
205070f794fcSJuan Quintela     .save_cleanup = htab_save_cleanup,
20514be21d56SDavid Gibson     .load_state = htab_load,
20524be21d56SDavid Gibson };
20534be21d56SDavid Gibson 
20545b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
20555b2128d2SAlexander Graf                            Error **errp)
20565b2128d2SAlexander Graf {
2057c86c1affSDaniel Henrique Barboza     MachineState *machine = MACHINE(opaque);
20585b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
20595b2128d2SAlexander Graf }
20605b2128d2SAlexander Graf 
2061224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
2062224245bfSDavid Gibson {
2063224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
2064224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
2065e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
2066224245bfSDavid Gibson     int i;
2067224245bfSDavid Gibson 
2068224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
2069224245bfSDavid Gibson         uint64_t addr;
2070224245bfSDavid Gibson 
2071e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
20726caf3ac6SDavid Gibson         spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_LMB,
2073224245bfSDavid Gibson                                addr / lmb_size);
2074224245bfSDavid Gibson     }
2075224245bfSDavid Gibson }
2076224245bfSDavid Gibson 
2077224245bfSDavid Gibson /*
2078224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
2079224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
2080224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
2081224245bfSDavid Gibson  */
20827c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
2083224245bfSDavid Gibson {
2084224245bfSDavid Gibson     int i;
2085224245bfSDavid Gibson 
20867c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
20877c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
20887c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
20897c150d6fSDavid Gibson                    machine->ram_size,
2090224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
20917c150d6fSDavid Gibson         return;
20927c150d6fSDavid Gibson     }
20937c150d6fSDavid Gibson 
20947c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
20957c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
20967c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
20977c150d6fSDavid Gibson                    machine->ram_size,
20987c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
20997c150d6fSDavid Gibson         return;
2100224245bfSDavid Gibson     }
2101224245bfSDavid Gibson 
2102224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
2103224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
21047c150d6fSDavid Gibson             error_setg(errp,
21057c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
21067c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
21077c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
2108224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
21097c150d6fSDavid Gibson             return;
2110224245bfSDavid Gibson         }
2111224245bfSDavid Gibson     }
2112224245bfSDavid Gibson }
2113224245bfSDavid Gibson 
2114535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
2115535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
2116535455fdSIgor Mammedov {
2117535455fdSIgor Mammedov     int index = id / smp_threads;
2118535455fdSIgor Mammedov 
2119535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
2120535455fdSIgor Mammedov         return NULL;
2121535455fdSIgor Mammedov     }
2122535455fdSIgor Mammedov     if (idx) {
2123535455fdSIgor Mammedov         *idx = index;
2124535455fdSIgor Mammedov     }
2125535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
2126535455fdSIgor Mammedov }
2127535455fdSIgor Mammedov 
21280c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
21290c86d0fdSDavid Gibson {
21300c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
21310c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
21320c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
21330c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
2134535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
2135535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
21360c86d0fdSDavid Gibson     int i;
21370c86d0fdSDavid Gibson 
21380c86d0fdSDavid Gibson     if (!type) {
21390c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
21400c86d0fdSDavid Gibson         exit(1);
21410c86d0fdSDavid Gibson     }
21420c86d0fdSDavid Gibson 
2143535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
2144c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
21450c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
21460c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
21470c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
21480c86d0fdSDavid Gibson             exit(1);
21490c86d0fdSDavid Gibson         }
21500c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
21510c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
21520c86d0fdSDavid Gibson                          max_cpus, smp_threads);
21530c86d0fdSDavid Gibson             exit(1);
21540c86d0fdSDavid Gibson         }
21550c86d0fdSDavid Gibson     } else {
21560c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
21570c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
21580c86d0fdSDavid Gibson             exit(1);
21590c86d0fdSDavid Gibson         }
2160535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
21610c86d0fdSDavid Gibson     }
21620c86d0fdSDavid Gibson 
2163535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
21640c86d0fdSDavid Gibson         int core_id = i * smp_threads;
21650c86d0fdSDavid Gibson 
2166c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
21672d335818SDavid Gibson             spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU,
21680c86d0fdSDavid Gibson                                    (core_id / smp_threads) * smt);
21690c86d0fdSDavid Gibson         }
21700c86d0fdSDavid Gibson 
2171535455fdSIgor Mammedov         if (i < boot_cores_nr) {
21720c86d0fdSDavid Gibson             Object *core  = object_new(type);
21730c86d0fdSDavid Gibson             int nr_threads = smp_threads;
21740c86d0fdSDavid Gibson 
21750c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
21760c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
21770c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
21780c86d0fdSDavid Gibson             }
21790c86d0fdSDavid Gibson 
21800c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
21810c86d0fdSDavid Gibson                                     &error_fatal);
21820c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
21830c86d0fdSDavid Gibson                                     &error_fatal);
21840c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
21850c86d0fdSDavid Gibson         }
21860c86d0fdSDavid Gibson     }
21870c86d0fdSDavid Gibson     g_free(type);
21880c86d0fdSDavid Gibson }
21890c86d0fdSDavid Gibson 
2190fa98fbfcSSam Bobroff static void spapr_set_vsmt_mode(sPAPRMachineState *spapr, Error **errp)
2191fa98fbfcSSam Bobroff {
2192fa98fbfcSSam Bobroff     Error *local_err = NULL;
2193fa98fbfcSSam Bobroff     bool vsmt_user = !!spapr->vsmt;
2194fa98fbfcSSam Bobroff     int kvm_smt = kvmppc_smt_threads();
2195fa98fbfcSSam Bobroff     int ret;
2196fa98fbfcSSam Bobroff 
2197fa98fbfcSSam Bobroff     if (!kvm_enabled() && (smp_threads > 1)) {
2198fa98fbfcSSam Bobroff         error_setg(&local_err, "TCG cannot support more than 1 thread/core "
2199fa98fbfcSSam Bobroff                      "on a pseries machine");
2200fa98fbfcSSam Bobroff         goto out;
2201fa98fbfcSSam Bobroff     }
2202fa98fbfcSSam Bobroff     if (!is_power_of_2(smp_threads)) {
2203fa98fbfcSSam Bobroff         error_setg(&local_err, "Cannot support %d threads/core on a pseries "
2204fa98fbfcSSam Bobroff                      "machine because it must be a power of 2", smp_threads);
2205fa98fbfcSSam Bobroff         goto out;
2206fa98fbfcSSam Bobroff     }
2207fa98fbfcSSam Bobroff 
2208fa98fbfcSSam Bobroff     /* Detemine the VSMT mode to use: */
2209fa98fbfcSSam Bobroff     if (vsmt_user) {
2210fa98fbfcSSam Bobroff         if (spapr->vsmt < smp_threads) {
2211fa98fbfcSSam Bobroff             error_setg(&local_err, "Cannot support VSMT mode %d"
2212fa98fbfcSSam Bobroff                          " because it must be >= threads/core (%d)",
2213fa98fbfcSSam Bobroff                          spapr->vsmt, smp_threads);
2214fa98fbfcSSam Bobroff             goto out;
2215fa98fbfcSSam Bobroff         }
2216fa98fbfcSSam Bobroff         /* In this case, spapr->vsmt has been set by the command line */
2217fa98fbfcSSam Bobroff     } else {
2218fa98fbfcSSam Bobroff         /* Choose a VSMT mode that may be higher than necessary but is
2219fa98fbfcSSam Bobroff          * likely to be compatible with hosts that don't have VSMT. */
2220fa98fbfcSSam Bobroff         spapr->vsmt = MAX(kvm_smt, smp_threads);
2221fa98fbfcSSam Bobroff     }
2222fa98fbfcSSam Bobroff 
2223fa98fbfcSSam Bobroff     /* KVM: If necessary, set the SMT mode: */
2224fa98fbfcSSam Bobroff     if (kvm_enabled() && (spapr->vsmt != kvm_smt)) {
2225fa98fbfcSSam Bobroff         ret = kvmppc_set_smt_threads(spapr->vsmt);
2226fa98fbfcSSam Bobroff         if (ret) {
2227fa98fbfcSSam Bobroff             error_setg(&local_err,
2228fa98fbfcSSam Bobroff                        "Failed to set KVM's VSMT mode to %d (errno %d)",
2229fa98fbfcSSam Bobroff                        spapr->vsmt, ret);
2230fa98fbfcSSam Bobroff             if (!vsmt_user) {
2231fa98fbfcSSam Bobroff                 error_append_hint(&local_err, "On PPC, a VM with %d threads/"
2232fa98fbfcSSam Bobroff                              "core on a host with %d threads/core requires "
2233fa98fbfcSSam Bobroff                              " the use of VSMT mode %d.\n",
2234fa98fbfcSSam Bobroff                              smp_threads, kvm_smt, spapr->vsmt);
2235fa98fbfcSSam Bobroff             }
2236fa98fbfcSSam Bobroff             kvmppc_hint_smt_possible(&local_err);
2237fa98fbfcSSam Bobroff             goto out;
2238fa98fbfcSSam Bobroff         }
2239fa98fbfcSSam Bobroff     }
2240fa98fbfcSSam Bobroff     /* else TCG: nothing to do currently */
2241fa98fbfcSSam Bobroff out:
2242fa98fbfcSSam Bobroff     error_propagate(errp, local_err);
2243fa98fbfcSSam Bobroff }
2244fa98fbfcSSam Bobroff 
224553018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
22463ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
224753018216SPaolo Bonzini {
224828e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
2249224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
22503ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
22513ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
225253018216SPaolo Bonzini     PCIHostState *phb;
225353018216SPaolo Bonzini     int i;
225453018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
225553018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
2256658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
2257658fa66bSAlexey Kardashevskiy     void *rma = NULL;
225853018216SPaolo Bonzini     hwaddr rma_alloc_size;
2259c86c1affSDaniel Henrique Barboza     hwaddr node0_size = spapr_node0_size(machine);
2260b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
226153018216SPaolo Bonzini     char *filename;
226230f4b05bSDavid Gibson     Error *resize_hpt_err = NULL;
226353018216SPaolo Bonzini 
2264226419d6SMichael S. Tsirkin     msi_nonbroken = true;
226553018216SPaolo Bonzini 
226653018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
22670cffce56SDavid Gibson     QTAILQ_INIT(&spapr->pending_dimm_unplugs);
226853018216SPaolo Bonzini 
226930f4b05bSDavid Gibson     /* Check HPT resizing availability */
227030f4b05bSDavid Gibson     kvmppc_check_papr_resize_hpt(&resize_hpt_err);
227130f4b05bSDavid Gibson     if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DEFAULT) {
227230f4b05bSDavid Gibson         /*
227330f4b05bSDavid Gibson          * If the user explicitly requested a mode we should either
227430f4b05bSDavid Gibson          * supply it, or fail completely (which we do below).  But if
227530f4b05bSDavid Gibson          * it's not set explicitly, we reset our mode to something
227630f4b05bSDavid Gibson          * that works
227730f4b05bSDavid Gibson          */
227830f4b05bSDavid Gibson         if (resize_hpt_err) {
227930f4b05bSDavid Gibson             spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
228030f4b05bSDavid Gibson             error_free(resize_hpt_err);
228130f4b05bSDavid Gibson             resize_hpt_err = NULL;
228230f4b05bSDavid Gibson         } else {
228330f4b05bSDavid Gibson             spapr->resize_hpt = smc->resize_hpt_default;
228430f4b05bSDavid Gibson         }
228530f4b05bSDavid Gibson     }
228630f4b05bSDavid Gibson 
228730f4b05bSDavid Gibson     assert(spapr->resize_hpt != SPAPR_RESIZE_HPT_DEFAULT);
228830f4b05bSDavid Gibson 
228930f4b05bSDavid Gibson     if ((spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) && resize_hpt_err) {
229030f4b05bSDavid Gibson         /*
229130f4b05bSDavid Gibson          * User requested HPT resize, but this host can't supply it.  Bail out
229230f4b05bSDavid Gibson          */
229330f4b05bSDavid Gibson         error_report_err(resize_hpt_err);
229430f4b05bSDavid Gibson         exit(1);
229530f4b05bSDavid Gibson     }
229630f4b05bSDavid Gibson 
229753018216SPaolo Bonzini     /* Allocate RMA if necessary */
2298658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
229953018216SPaolo Bonzini 
230053018216SPaolo Bonzini     if (rma_alloc_size == -1) {
2301730fce59SThomas Huth         error_report("Unable to create RMA");
230253018216SPaolo Bonzini         exit(1);
230353018216SPaolo Bonzini     }
230453018216SPaolo Bonzini 
2305c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
230653018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
230753018216SPaolo Bonzini     } else {
2308c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
230953018216SPaolo Bonzini 
231053018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
231153018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
231253018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
231353018216SPaolo Bonzini          *
231453018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
231553018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
231653018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
231753018216SPaolo Bonzini          * isn't determined yet.
231853018216SPaolo Bonzini          */
231953018216SPaolo Bonzini         if (kvm_enabled()) {
232053018216SPaolo Bonzini             spapr->vrma_adjust = 1;
232153018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
232253018216SPaolo Bonzini         }
2323912acdf4SBenjamin Herrenschmidt 
2324912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
2325912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
2326912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
2327912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
2328912acdf4SBenjamin Herrenschmidt          */
2329912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
233053018216SPaolo Bonzini     }
233153018216SPaolo Bonzini 
2332c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
2333d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2334c4177479SAlexey Kardashevskiy                      spapr->rma_size);
2335c4177479SAlexey Kardashevskiy         exit(1);
2336c4177479SAlexey Kardashevskiy     }
2337c4177479SAlexey Kardashevskiy 
2338b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2339b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
234053018216SPaolo Bonzini 
23417b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
234271cd4dacSCédric Le Goater     xics_system_init(machine, XICS_IRQS_SPAPR, &error_fatal);
23437b565160SDavid Gibson 
2344dc1b5eeeSGreg Kurz     /* Set up containers for ibm,client-architecture-support negotiated options
2345dc1b5eeeSGreg Kurz      */
2346facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2347facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2348facdb8b6SMichael Roth 
2349224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2350facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
23517c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2352224245bfSDavid Gibson     }
2353224245bfSDavid Gibson 
2354417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2355545d6e2bSSuraj Jitindar Singh     if (!kvm_enabled() || kvmppc_has_cap_mmu_radix()) {
2356545d6e2bSSuraj Jitindar Singh         /* KVM and TCG always allow GTSE with radix... */
23579fb4541fSSam Bobroff         spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
23589fb4541fSSam Bobroff     }
23599fb4541fSSam Bobroff     /* ... but not with hash (currently). */
2360417ece33SMichael Roth 
2361ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2362ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2363ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2364ffbb1705SMichael Roth     }
2365ffbb1705SMichael Roth 
23662772cf6bSDavid Gibson     /* advertise support for HPT resizing */
23672772cf6bSDavid Gibson     if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
23682772cf6bSDavid Gibson         spapr_ovec_set(spapr->ov5, OV5_HPT_RESIZE);
23692772cf6bSDavid Gibson     }
23702772cf6bSDavid Gibson 
237153018216SPaolo Bonzini     /* init CPUs */
237219fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
23733daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
237453018216SPaolo Bonzini     }
237594a94e4cSBharata B Rao 
23767843c0d6SDavid Gibson     spapr_cpu_parse_features(spapr);
2377e703d2f7SGreg Kurz 
2378fa98fbfcSSam Bobroff     spapr_set_vsmt_mode(spapr, &error_fatal);
2379fa98fbfcSSam Bobroff 
23800c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
238153018216SPaolo Bonzini 
2382026bfd89SDavid Gibson     if (kvm_enabled()) {
2383026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2384026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2385ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
23865145ad4fSNathan Whitehorn 
23875145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
23885145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2389026bfd89SDavid Gibson     }
2390026bfd89SDavid Gibson 
239153018216SPaolo Bonzini     /* allocate RAM */
2392f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2393fb164994SDavid Gibson                                          machine->ram_size);
2394f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
239553018216SPaolo Bonzini 
2396658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2397658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2398658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2399658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2400658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2401658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2402658fa66bSAlexey Kardashevskiy     }
2403658fa66bSAlexey Kardashevskiy 
24044a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
24054a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
24064a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
240771c9a3ddSBharata B Rao         /*
240871c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
240971c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
241071c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
241171c9a3ddSBharata B Rao          */
241271c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
241371c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
24144a1c9cf0SBharata B Rao 
241571c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
241671c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
241771c9a3ddSBharata B Rao         }
241871c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2419d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2420d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
242171c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2422d54e4d76SDavid Gibson             exit(1);
24234a1c9cf0SBharata B Rao         }
24244a1c9cf0SBharata B Rao 
24254a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
24264a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
24274a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
24284a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
24294a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
24304a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
24314a1c9cf0SBharata B Rao     }
24324a1c9cf0SBharata B Rao 
2433224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2434224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2435224245bfSDavid Gibson     }
2436224245bfSDavid Gibson 
243753018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
24384c56440dSStefan Weil     if (!filename) {
2439730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
24404c56440dSStefan Weil         exit(1);
24414c56440dSStefan Weil     }
2442b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
24438afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
24448afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
24458afc22a2SZhou Jie         exit(1);
24468afc22a2SZhou Jie     }
2447b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2448b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2449730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
245053018216SPaolo Bonzini         exit(1);
245153018216SPaolo Bonzini     }
245253018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2453730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
24542f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
245553018216SPaolo Bonzini         exit(1);
245653018216SPaolo Bonzini     }
245753018216SPaolo Bonzini     g_free(filename);
245853018216SPaolo Bonzini 
2459ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
246053018216SPaolo Bonzini     spapr_events_init(spapr);
246153018216SPaolo Bonzini 
246212f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
246328df36a1SDavid Gibson     spapr_rtc_create(spapr);
246412f42174SDavid Gibson 
246553018216SPaolo Bonzini     /* Set up VIO bus */
246653018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
246753018216SPaolo Bonzini 
246853018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
246953018216SPaolo Bonzini         if (serial_hds[i]) {
247053018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
247153018216SPaolo Bonzini         }
247253018216SPaolo Bonzini     }
247353018216SPaolo Bonzini 
247453018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
247553018216SPaolo Bonzini     spapr_create_nvram(spapr);
247653018216SPaolo Bonzini 
247753018216SPaolo Bonzini     /* Set up PCI */
247853018216SPaolo Bonzini     spapr_pci_rtas_init();
247953018216SPaolo Bonzini 
248089dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
248153018216SPaolo Bonzini 
248253018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
248353018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
248453018216SPaolo Bonzini 
248553018216SPaolo Bonzini         if (!nd->model) {
248653018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
248753018216SPaolo Bonzini         }
248853018216SPaolo Bonzini 
248953018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
249053018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
249153018216SPaolo Bonzini         } else {
249229b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
249353018216SPaolo Bonzini         }
249453018216SPaolo Bonzini     }
249553018216SPaolo Bonzini 
249653018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
249753018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
249853018216SPaolo Bonzini     }
249953018216SPaolo Bonzini 
250053018216SPaolo Bonzini     /* Graphics */
250114c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
250253018216SPaolo Bonzini         spapr->has_graphics = true;
2503c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
250453018216SPaolo Bonzini     }
250553018216SPaolo Bonzini 
25064ee9ced9SMarcel Apfelbaum     if (machine->usb) {
250757040d45SThomas Huth         if (smc->use_ohci_by_default) {
250853018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
250957040d45SThomas Huth         } else {
251057040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
251157040d45SThomas Huth         }
2512c86580b8SMarkus Armbruster 
251353018216SPaolo Bonzini         if (spapr->has_graphics) {
2514c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2515c86580b8SMarkus Armbruster 
2516c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2517c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
251853018216SPaolo Bonzini         }
251953018216SPaolo Bonzini     }
252053018216SPaolo Bonzini 
252153018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2522d54e4d76SDavid Gibson         error_report(
2523d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2524d54e4d76SDavid Gibson             MIN_RMA_SLOF);
252553018216SPaolo Bonzini         exit(1);
252653018216SPaolo Bonzini     }
252753018216SPaolo Bonzini 
252853018216SPaolo Bonzini     if (kernel_filename) {
252953018216SPaolo Bonzini         uint64_t lowaddr = 0;
253053018216SPaolo Bonzini 
2531a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2532a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2533a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2534a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2535a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2536a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2537a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
25387ef295eaSPeter Crosthwaite                                           0, 0);
2539a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
254016457e7fSBenjamin Herrenschmidt         }
2541a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2542a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2543a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
254453018216SPaolo Bonzini             exit(1);
254553018216SPaolo Bonzini         }
254653018216SPaolo Bonzini 
254753018216SPaolo Bonzini         /* load initrd */
254853018216SPaolo Bonzini         if (initrd_filename) {
254953018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
255053018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
255153018216SPaolo Bonzini              */
2552a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2553a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2554a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2555a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2556a19f7fb0SDavid Gibson                                                      load_limit
2557a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2558a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2559d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
256053018216SPaolo Bonzini                              initrd_filename);
256153018216SPaolo Bonzini                 exit(1);
256253018216SPaolo Bonzini             }
256353018216SPaolo Bonzini         }
256453018216SPaolo Bonzini     }
256553018216SPaolo Bonzini 
25668e7ea787SAndreas Färber     if (bios_name == NULL) {
25678e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
25688e7ea787SAndreas Färber     }
25698e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
25704c56440dSStefan Weil     if (!filename) {
257168fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
25724c56440dSStefan Weil         exit(1);
25734c56440dSStefan Weil     }
257453018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
257568fea5a0SThomas Huth     if (fw_size <= 0) {
257668fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
257753018216SPaolo Bonzini         exit(1);
257853018216SPaolo Bonzini     }
257953018216SPaolo Bonzini     g_free(filename);
258053018216SPaolo Bonzini 
258128e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
258228e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
258328e02042SDavid Gibson      * which predated MachineState but had a similar function */
25844be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
25854be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
25864be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
25874be21d56SDavid Gibson 
25885b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
258942043e4fSLaurent Vivier 
259042043e4fSLaurent Vivier     if (kvm_enabled()) {
25913dc410aeSAlexey Kardashevskiy         /* to stop and start vmclock */
259242043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
259342043e4fSLaurent Vivier                                          &spapr->tb);
25943dc410aeSAlexey Kardashevskiy 
25953dc410aeSAlexey Kardashevskiy         kvmppc_spapr_enable_inkernel_multitce();
259642043e4fSLaurent Vivier     }
259753018216SPaolo Bonzini }
259853018216SPaolo Bonzini 
2599135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2600135a129aSAneesh Kumar K.V {
2601135a129aSAneesh Kumar K.V     if (!vm_type) {
2602135a129aSAneesh Kumar K.V         return 0;
2603135a129aSAneesh Kumar K.V     }
2604135a129aSAneesh Kumar K.V 
2605135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2606135a129aSAneesh Kumar K.V         return 1;
2607135a129aSAneesh Kumar K.V     }
2608135a129aSAneesh Kumar K.V 
2609135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2610135a129aSAneesh Kumar K.V         return 2;
2611135a129aSAneesh Kumar K.V     }
2612135a129aSAneesh Kumar K.V 
2613135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2614135a129aSAneesh Kumar K.V     exit(1);
2615135a129aSAneesh Kumar K.V }
2616135a129aSAneesh Kumar K.V 
261771461b0fSAlexey Kardashevskiy /*
2618627b84f4SGonglei  * Implementation of an interface to adjust firmware path
261971461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
262071461b0fSAlexey Kardashevskiy  */
262171461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
262271461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
262371461b0fSAlexey Kardashevskiy {
262471461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
262571461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
262671461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
262771461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
2628c4e13492SFelipe Franciosi     VHostSCSICommon *vsc = CAST(VHostSCSICommon, dev, TYPE_VHOST_SCSI_COMMON);
262971461b0fSAlexey Kardashevskiy 
263071461b0fSAlexey Kardashevskiy     if (d) {
263171461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
263271461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
263371461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
263471461b0fSAlexey Kardashevskiy 
263571461b0fSAlexey Kardashevskiy         if (spapr) {
263671461b0fSAlexey Kardashevskiy             /*
263771461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
263871461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
263971461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
264071461b0fSAlexey Kardashevskiy              */
264171461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
264271461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
264371461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
264471461b0fSAlexey Kardashevskiy         } else if (virtio) {
264571461b0fSAlexey Kardashevskiy             /*
264671461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
264771461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
264871461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
264971461b0fSAlexey Kardashevskiy              * the actual binding is:
265071461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
265171461b0fSAlexey Kardashevskiy              */
265271461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
265371461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
265471461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
265571461b0fSAlexey Kardashevskiy         } else if (usb) {
265671461b0fSAlexey Kardashevskiy             /*
265771461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
265871461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
265971461b0fSAlexey Kardashevskiy              */
266071461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
266171461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
266271461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
266371461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
266471461b0fSAlexey Kardashevskiy         }
266571461b0fSAlexey Kardashevskiy     }
266671461b0fSAlexey Kardashevskiy 
2667b99260ebSThomas Huth     /*
2668b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2669b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2670b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2671b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2672b99260ebSThomas Huth      */
2673b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2674b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2675b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2676b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2677b99260ebSThomas Huth         }
2678b99260ebSThomas Huth     }
2679b99260ebSThomas Huth 
268071461b0fSAlexey Kardashevskiy     if (phb) {
268171461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
268271461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
268371461b0fSAlexey Kardashevskiy     }
268471461b0fSAlexey Kardashevskiy 
2685c4e13492SFelipe Franciosi     if (vsc) {
2686c4e13492SFelipe Franciosi         /* Same logic as virtio above */
2687c4e13492SFelipe Franciosi         unsigned id = 0x1000000 | (vsc->target << 16) | vsc->lun;
2688c4e13492SFelipe Franciosi         return g_strdup_printf("disk@%"PRIX64, (uint64_t)id << 32);
2689c4e13492SFelipe Franciosi     }
2690c4e13492SFelipe Franciosi 
26914871dd4cSThomas Huth     if (g_str_equal("pci-bridge", qdev_fw_name(dev))) {
26924871dd4cSThomas Huth         /* SLOF uses "pci" instead of "pci-bridge" for PCI bridges */
26934871dd4cSThomas Huth         PCIDevice *pcidev = CAST(PCIDevice, dev, TYPE_PCI_DEVICE);
26944871dd4cSThomas Huth         return g_strdup_printf("pci@%x", PCI_SLOT(pcidev->devfn));
26954871dd4cSThomas Huth     }
26964871dd4cSThomas Huth 
269771461b0fSAlexey Kardashevskiy     return NULL;
269871461b0fSAlexey Kardashevskiy }
269971461b0fSAlexey Kardashevskiy 
270023825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
270123825581SEduardo Habkost {
270228e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
270323825581SEduardo Habkost 
270428e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
270523825581SEduardo Habkost }
270623825581SEduardo Habkost 
270723825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
270823825581SEduardo Habkost {
270928e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
271023825581SEduardo Habkost 
271128e02042SDavid Gibson     g_free(spapr->kvm_type);
271228e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
271323825581SEduardo Habkost }
271423825581SEduardo Habkost 
2715f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2716f6229214SMichael Roth {
2717f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2718f6229214SMichael Roth 
2719f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2720f6229214SMichael Roth }
2721f6229214SMichael Roth 
2722f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2723f6229214SMichael Roth                                             Error **errp)
2724f6229214SMichael Roth {
2725f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2726f6229214SMichael Roth 
2727f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2728f6229214SMichael Roth }
2729f6229214SMichael Roth 
273030f4b05bSDavid Gibson static char *spapr_get_resize_hpt(Object *obj, Error **errp)
273130f4b05bSDavid Gibson {
273230f4b05bSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
273330f4b05bSDavid Gibson 
273430f4b05bSDavid Gibson     switch (spapr->resize_hpt) {
273530f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_DEFAULT:
273630f4b05bSDavid Gibson         return g_strdup("default");
273730f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_DISABLED:
273830f4b05bSDavid Gibson         return g_strdup("disabled");
273930f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_ENABLED:
274030f4b05bSDavid Gibson         return g_strdup("enabled");
274130f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_REQUIRED:
274230f4b05bSDavid Gibson         return g_strdup("required");
274330f4b05bSDavid Gibson     }
274430f4b05bSDavid Gibson     g_assert_not_reached();
274530f4b05bSDavid Gibson }
274630f4b05bSDavid Gibson 
274730f4b05bSDavid Gibson static void spapr_set_resize_hpt(Object *obj, const char *value, Error **errp)
274830f4b05bSDavid Gibson {
274930f4b05bSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
275030f4b05bSDavid Gibson 
275130f4b05bSDavid Gibson     if (strcmp(value, "default") == 0) {
275230f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_DEFAULT;
275330f4b05bSDavid Gibson     } else if (strcmp(value, "disabled") == 0) {
275430f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
275530f4b05bSDavid Gibson     } else if (strcmp(value, "enabled") == 0) {
275630f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_ENABLED;
275730f4b05bSDavid Gibson     } else if (strcmp(value, "required") == 0) {
275830f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_REQUIRED;
275930f4b05bSDavid Gibson     } else {
276030f4b05bSDavid Gibson         error_setg(errp, "Bad value for \"resize-hpt\" property");
276130f4b05bSDavid Gibson     }
276230f4b05bSDavid Gibson }
276330f4b05bSDavid Gibson 
2764fa98fbfcSSam Bobroff static void spapr_get_vsmt(Object *obj, Visitor *v, const char *name,
2765fa98fbfcSSam Bobroff                                    void *opaque, Error **errp)
2766fa98fbfcSSam Bobroff {
2767fa98fbfcSSam Bobroff     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
2768fa98fbfcSSam Bobroff }
2769fa98fbfcSSam Bobroff 
2770fa98fbfcSSam Bobroff static void spapr_set_vsmt(Object *obj, Visitor *v, const char *name,
2771fa98fbfcSSam Bobroff                                    void *opaque, Error **errp)
2772fa98fbfcSSam Bobroff {
2773fa98fbfcSSam Bobroff     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
2774fa98fbfcSSam Bobroff }
2775fa98fbfcSSam Bobroff 
277623825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
277723825581SEduardo Habkost {
2778715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2779715c5407SDavid Gibson 
2780715c5407SDavid Gibson     spapr->htab_fd = -1;
2781f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
278223825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
278323825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
278449d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
278549d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
278649d2e648SMarcel Apfelbaum                                     NULL);
2787f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2788f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2789f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2790f6229214SMichael Roth                             NULL);
2791f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2792f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2793f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2794f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2795f6229214SMichael Roth                                     NULL);
27967843c0d6SDavid Gibson 
27977843c0d6SDavid Gibson     ppc_compat_add_property(obj, "max-cpu-compat", &spapr->max_compat_pvr,
27987843c0d6SDavid Gibson                             "Maximum permitted CPU compatibility mode",
27997843c0d6SDavid Gibson                             &error_fatal);
280030f4b05bSDavid Gibson 
280130f4b05bSDavid Gibson     object_property_add_str(obj, "resize-hpt",
280230f4b05bSDavid Gibson                             spapr_get_resize_hpt, spapr_set_resize_hpt, NULL);
280330f4b05bSDavid Gibson     object_property_set_description(obj, "resize-hpt",
280430f4b05bSDavid Gibson                                     "Resizing of the Hash Page Table (enabled, disabled, required)",
280530f4b05bSDavid Gibson                                     NULL);
2806fa98fbfcSSam Bobroff     object_property_add(obj, "vsmt", "uint32", spapr_get_vsmt,
2807fa98fbfcSSam Bobroff                         spapr_set_vsmt, NULL, &spapr->vsmt, &error_abort);
2808fa98fbfcSSam Bobroff     object_property_set_description(obj, "vsmt",
2809fa98fbfcSSam Bobroff                                     "Virtual SMT: KVM behaves as if this were"
2810fa98fbfcSSam Bobroff                                     " the host's SMT mode", &error_abort);
281123825581SEduardo Habkost }
281223825581SEduardo Habkost 
281387bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
281487bbdd9cSDavid Gibson {
281587bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
281687bbdd9cSDavid Gibson 
281787bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
281887bbdd9cSDavid Gibson }
281987bbdd9cSDavid Gibson 
28201c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
282134316482SAlexey Kardashevskiy {
282234316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
282334316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
282434316482SAlexey Kardashevskiy }
282534316482SAlexey Kardashevskiy 
282634316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
282734316482SAlexey Kardashevskiy {
282834316482SAlexey Kardashevskiy     CPUState *cs;
282934316482SAlexey Kardashevskiy 
283034316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
28311c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
283234316482SAlexey Kardashevskiy     }
283334316482SAlexey Kardashevskiy }
283434316482SAlexey Kardashevskiy 
283579b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
283679b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
283779b78a6bSMichael Roth                            Error **errp)
2838c20d332aSBharata B Rao {
2839c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2840c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2841c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2842c20d332aSBharata B Rao     void *fdt;
284379b78a6bSMichael Roth     uint64_t addr = addr_start;
284494fd9cbaSLaurent Vivier     bool hotplugged = spapr_drc_hotplugged(dev);
2845160bb678SGreg Kurz     Error *local_err = NULL;
2846c20d332aSBharata B Rao 
2847c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2848fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
2849c20d332aSBharata B Rao                               addr / SPAPR_MEMORY_BLOCK_SIZE);
2850c20d332aSBharata B Rao         g_assert(drc);
2851c20d332aSBharata B Rao 
2852c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2853c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2854c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2855c20d332aSBharata B Rao 
2856160bb678SGreg Kurz         spapr_drc_attach(drc, dev, fdt, fdt_offset, &local_err);
2857160bb678SGreg Kurz         if (local_err) {
2858160bb678SGreg Kurz             while (addr > addr_start) {
2859160bb678SGreg Kurz                 addr -= SPAPR_MEMORY_BLOCK_SIZE;
2860160bb678SGreg Kurz                 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
2861160bb678SGreg Kurz                                       addr / SPAPR_MEMORY_BLOCK_SIZE);
2862a8dc47fdSDavid Gibson                 spapr_drc_detach(drc);
2863160bb678SGreg Kurz             }
2864160bb678SGreg Kurz             g_free(fdt);
2865160bb678SGreg Kurz             error_propagate(errp, local_err);
2866160bb678SGreg Kurz             return;
2867160bb678SGreg Kurz         }
286894fd9cbaSLaurent Vivier         if (!hotplugged) {
286994fd9cbaSLaurent Vivier             spapr_drc_reset(drc);
287094fd9cbaSLaurent Vivier         }
2871c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2872c20d332aSBharata B Rao     }
28735dd5238cSJianjun Duan     /* send hotplug notification to the
28745dd5238cSJianjun Duan      * guest only in case of hotplugged memory
28755dd5238cSJianjun Duan      */
287694fd9cbaSLaurent Vivier     if (hotplugged) {
287779b78a6bSMichael Roth         if (dedicated_hp_event_source) {
2878fbf55397SDavid Gibson             drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
287979b78a6bSMichael Roth                                   addr_start / SPAPR_MEMORY_BLOCK_SIZE);
288079b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
288179b78a6bSMichael Roth                                                    nr_lmbs,
28820b55aa91SDavid Gibson                                                    spapr_drc_index(drc));
288379b78a6bSMichael Roth         } else {
288479b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
288579b78a6bSMichael Roth                                            nr_lmbs);
288679b78a6bSMichael Roth         }
2887c20d332aSBharata B Rao     }
28885dd5238cSJianjun Duan }
2889c20d332aSBharata B Rao 
2890c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2891c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2892c20d332aSBharata B Rao {
2893c20d332aSBharata B Rao     Error *local_err = NULL;
2894c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2895c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2896c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
289704790978SThomas Huth     MemoryRegion *mr;
289804790978SThomas Huth     uint64_t align, size, addr;
289904790978SThomas Huth 
290004790978SThomas Huth     mr = ddc->get_memory_region(dimm, &local_err);
290104790978SThomas Huth     if (local_err) {
290204790978SThomas Huth         goto out;
290304790978SThomas Huth     }
290404790978SThomas Huth     align = memory_region_get_alignment(mr);
290504790978SThomas Huth     size = memory_region_size(mr);
2906df587133SThomas Huth 
2907d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2908c20d332aSBharata B Rao     if (local_err) {
2909c20d332aSBharata B Rao         goto out;
2910c20d332aSBharata B Rao     }
2911c20d332aSBharata B Rao 
29129ed442b8SMarc-André Lureau     addr = object_property_get_uint(OBJECT(dimm),
29139ed442b8SMarc-André Lureau                                     PC_DIMM_ADDR_PROP, &local_err);
2914c20d332aSBharata B Rao     if (local_err) {
2915160bb678SGreg Kurz         goto out_unplug;
2916c20d332aSBharata B Rao     }
2917c20d332aSBharata B Rao 
291879b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
291979b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
2920160bb678SGreg Kurz                    &local_err);
2921160bb678SGreg Kurz     if (local_err) {
2922160bb678SGreg Kurz         goto out_unplug;
2923160bb678SGreg Kurz     }
2924c20d332aSBharata B Rao 
2925160bb678SGreg Kurz     return;
2926160bb678SGreg Kurz 
2927160bb678SGreg Kurz out_unplug:
2928160bb678SGreg Kurz     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2929c20d332aSBharata B Rao out:
2930c20d332aSBharata B Rao     error_propagate(errp, local_err);
2931c20d332aSBharata B Rao }
2932c20d332aSBharata B Rao 
2933c871bc70SLaurent Vivier static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2934c871bc70SLaurent Vivier                                   Error **errp)
2935c871bc70SLaurent Vivier {
2936c871bc70SLaurent Vivier     PCDIMMDevice *dimm = PC_DIMM(dev);
2937c871bc70SLaurent Vivier     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
293804790978SThomas Huth     MemoryRegion *mr;
293904790978SThomas Huth     uint64_t size;
2940c871bc70SLaurent Vivier     char *mem_dev;
2941c871bc70SLaurent Vivier 
294204790978SThomas Huth     mr = ddc->get_memory_region(dimm, errp);
294304790978SThomas Huth     if (!mr) {
294404790978SThomas Huth         return;
294504790978SThomas Huth     }
294604790978SThomas Huth     size = memory_region_size(mr);
294704790978SThomas Huth 
2948c871bc70SLaurent Vivier     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2949c871bc70SLaurent Vivier         error_setg(errp, "Hotplugged memory size must be a multiple of "
2950c871bc70SLaurent Vivier                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
2951c871bc70SLaurent Vivier         return;
2952c871bc70SLaurent Vivier     }
2953c871bc70SLaurent Vivier 
2954c871bc70SLaurent Vivier     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2955c871bc70SLaurent Vivier     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2956c871bc70SLaurent Vivier         error_setg(errp, "Memory backend has bad page size. "
2957c871bc70SLaurent Vivier                    "Use 'memory-backend-file' with correct mem-path.");
29588a9e0e7bSGreg Kurz         goto out;
2959c871bc70SLaurent Vivier     }
29608a9e0e7bSGreg Kurz 
29618a9e0e7bSGreg Kurz out:
29628a9e0e7bSGreg Kurz     g_free(mem_dev);
2963c871bc70SLaurent Vivier }
2964c871bc70SLaurent Vivier 
29650cffce56SDavid Gibson struct sPAPRDIMMState {
29660cffce56SDavid Gibson     PCDIMMDevice *dimm;
2967cf632463SBharata B Rao     uint32_t nr_lmbs;
29680cffce56SDavid Gibson     QTAILQ_ENTRY(sPAPRDIMMState) next;
29690cffce56SDavid Gibson };
29700cffce56SDavid Gibson 
29710cffce56SDavid Gibson static sPAPRDIMMState *spapr_pending_dimm_unplugs_find(sPAPRMachineState *s,
29720cffce56SDavid Gibson                                                        PCDIMMDevice *dimm)
29730cffce56SDavid Gibson {
29740cffce56SDavid Gibson     sPAPRDIMMState *dimm_state = NULL;
29750cffce56SDavid Gibson 
29760cffce56SDavid Gibson     QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
29770cffce56SDavid Gibson         if (dimm_state->dimm == dimm) {
29780cffce56SDavid Gibson             break;
29790cffce56SDavid Gibson         }
29800cffce56SDavid Gibson     }
29810cffce56SDavid Gibson     return dimm_state;
29820cffce56SDavid Gibson }
29830cffce56SDavid Gibson 
29848d5981c4SBharata B Rao static sPAPRDIMMState *spapr_pending_dimm_unplugs_add(sPAPRMachineState *spapr,
29858d5981c4SBharata B Rao                                                       uint32_t nr_lmbs,
29868d5981c4SBharata B Rao                                                       PCDIMMDevice *dimm)
29870cffce56SDavid Gibson {
29888d5981c4SBharata B Rao     sPAPRDIMMState *ds = NULL;
29898d5981c4SBharata B Rao 
29908d5981c4SBharata B Rao     /*
29918d5981c4SBharata B Rao      * If this request is for a DIMM whose removal had failed earlier
29928d5981c4SBharata B Rao      * (due to guest's refusal to remove the LMBs), we would have this
29938d5981c4SBharata B Rao      * dimm already in the pending_dimm_unplugs list. In that
29948d5981c4SBharata B Rao      * case don't add again.
29958d5981c4SBharata B Rao      */
29968d5981c4SBharata B Rao     ds = spapr_pending_dimm_unplugs_find(spapr, dimm);
29978d5981c4SBharata B Rao     if (!ds) {
29988d5981c4SBharata B Rao         ds = g_malloc0(sizeof(sPAPRDIMMState));
29998d5981c4SBharata B Rao         ds->nr_lmbs = nr_lmbs;
30008d5981c4SBharata B Rao         ds->dimm = dimm;
30018d5981c4SBharata B Rao         QTAILQ_INSERT_HEAD(&spapr->pending_dimm_unplugs, ds, next);
30028d5981c4SBharata B Rao     }
30038d5981c4SBharata B Rao     return ds;
30040cffce56SDavid Gibson }
30050cffce56SDavid Gibson 
30060cffce56SDavid Gibson static void spapr_pending_dimm_unplugs_remove(sPAPRMachineState *spapr,
30070cffce56SDavid Gibson                                               sPAPRDIMMState *dimm_state)
30080cffce56SDavid Gibson {
30090cffce56SDavid Gibson     QTAILQ_REMOVE(&spapr->pending_dimm_unplugs, dimm_state, next);
30100cffce56SDavid Gibson     g_free(dimm_state);
30110cffce56SDavid Gibson }
3012cf632463SBharata B Rao 
301316ee9980SDaniel Henrique Barboza static sPAPRDIMMState *spapr_recover_pending_dimm_state(sPAPRMachineState *ms,
301416ee9980SDaniel Henrique Barboza                                                         PCDIMMDevice *dimm)
301516ee9980SDaniel Henrique Barboza {
301616ee9980SDaniel Henrique Barboza     sPAPRDRConnector *drc;
301716ee9980SDaniel Henrique Barboza     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
301804790978SThomas Huth     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
301916ee9980SDaniel Henrique Barboza     uint64_t size = memory_region_size(mr);
302016ee9980SDaniel Henrique Barboza     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
302116ee9980SDaniel Henrique Barboza     uint32_t avail_lmbs = 0;
302216ee9980SDaniel Henrique Barboza     uint64_t addr_start, addr;
302316ee9980SDaniel Henrique Barboza     int i;
302416ee9980SDaniel Henrique Barboza 
302516ee9980SDaniel Henrique Barboza     addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
302616ee9980SDaniel Henrique Barboza                                          &error_abort);
302716ee9980SDaniel Henrique Barboza 
302816ee9980SDaniel Henrique Barboza     addr = addr_start;
302916ee9980SDaniel Henrique Barboza     for (i = 0; i < nr_lmbs; i++) {
3030fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
303116ee9980SDaniel Henrique Barboza                               addr / SPAPR_MEMORY_BLOCK_SIZE);
303216ee9980SDaniel Henrique Barboza         g_assert(drc);
3033454b580aSDavid Gibson         if (drc->dev) {
303416ee9980SDaniel Henrique Barboza             avail_lmbs++;
303516ee9980SDaniel Henrique Barboza         }
303616ee9980SDaniel Henrique Barboza         addr += SPAPR_MEMORY_BLOCK_SIZE;
303716ee9980SDaniel Henrique Barboza     }
303816ee9980SDaniel Henrique Barboza 
30398d5981c4SBharata B Rao     return spapr_pending_dimm_unplugs_add(ms, avail_lmbs, dimm);
304016ee9980SDaniel Henrique Barboza }
304116ee9980SDaniel Henrique Barboza 
304231834723SDaniel Henrique Barboza /* Callback to be called during DRC release. */
304331834723SDaniel Henrique Barboza void spapr_lmb_release(DeviceState *dev)
3044cf632463SBharata B Rao {
3045765d1bddSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_hotplug_handler(dev));
3046765d1bddSDavid Gibson     PCDIMMDevice *dimm = PC_DIMM(dev);
3047765d1bddSDavid Gibson     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
304804790978SThomas Huth     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
30490cffce56SDavid Gibson     sPAPRDIMMState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
3050cf632463SBharata B Rao 
305116ee9980SDaniel Henrique Barboza     /* This information will get lost if a migration occurs
305216ee9980SDaniel Henrique Barboza      * during the unplug process. In this case recover it. */
305316ee9980SDaniel Henrique Barboza     if (ds == NULL) {
305416ee9980SDaniel Henrique Barboza         ds = spapr_recover_pending_dimm_state(spapr, PC_DIMM(dev));
30558d5981c4SBharata B Rao         g_assert(ds);
3056454b580aSDavid Gibson         /* The DRC being examined by the caller at least must be counted */
3057454b580aSDavid Gibson         g_assert(ds->nr_lmbs);
305816ee9980SDaniel Henrique Barboza     }
3059454b580aSDavid Gibson 
3060454b580aSDavid Gibson     if (--ds->nr_lmbs) {
3061cf632463SBharata B Rao         return;
3062cf632463SBharata B Rao     }
3063cf632463SBharata B Rao 
30640cffce56SDavid Gibson     spapr_pending_dimm_unplugs_remove(spapr, ds);
3065cf632463SBharata B Rao 
3066cf632463SBharata B Rao     /*
3067cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
3068cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
3069cf632463SBharata B Rao      */
3070765d1bddSDavid Gibson     pc_dimm_memory_unplug(dev, &spapr->hotplug_memory, mr);
3071cf632463SBharata B Rao     object_unparent(OBJECT(dev));
3072cf632463SBharata B Rao }
3073cf632463SBharata B Rao 
3074cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
3075cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
3076cf632463SBharata B Rao {
30770cffce56SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
3078cf632463SBharata B Rao     Error *local_err = NULL;
3079cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
3080cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
308104790978SThomas Huth     MemoryRegion *mr;
308204790978SThomas Huth     uint32_t nr_lmbs;
308304790978SThomas Huth     uint64_t size, addr_start, addr;
30840cffce56SDavid Gibson     int i;
30850cffce56SDavid Gibson     sPAPRDRConnector *drc;
308604790978SThomas Huth 
308704790978SThomas Huth     mr = ddc->get_memory_region(dimm, &local_err);
308804790978SThomas Huth     if (local_err) {
308904790978SThomas Huth         goto out;
309004790978SThomas Huth     }
309104790978SThomas Huth     size = memory_region_size(mr);
309204790978SThomas Huth     nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
309304790978SThomas Huth 
30949ed442b8SMarc-André Lureau     addr_start = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
30950cffce56SDavid Gibson                                          &local_err);
3096cf632463SBharata B Rao     if (local_err) {
3097cf632463SBharata B Rao         goto out;
3098cf632463SBharata B Rao     }
3099cf632463SBharata B Rao 
31008d5981c4SBharata B Rao     spapr_pending_dimm_unplugs_add(spapr, nr_lmbs, dimm);
31010cffce56SDavid Gibson 
31020cffce56SDavid Gibson     addr = addr_start;
31030cffce56SDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
3104fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
31050cffce56SDavid Gibson                               addr / SPAPR_MEMORY_BLOCK_SIZE);
31060cffce56SDavid Gibson         g_assert(drc);
31070cffce56SDavid Gibson 
3108a8dc47fdSDavid Gibson         spapr_drc_detach(drc);
31090cffce56SDavid Gibson         addr += SPAPR_MEMORY_BLOCK_SIZE;
31100cffce56SDavid Gibson     }
31110cffce56SDavid Gibson 
3112fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
31130cffce56SDavid Gibson                           addr_start / SPAPR_MEMORY_BLOCK_SIZE);
31140cffce56SDavid Gibson     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
31150b55aa91SDavid Gibson                                               nr_lmbs, spapr_drc_index(drc));
3116cf632463SBharata B Rao out:
3117cf632463SBharata B Rao     error_propagate(errp, local_err);
3118cf632463SBharata B Rao }
3119cf632463SBharata B Rao 
312004d0ffbdSGreg Kurz static void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
3121af81cf32SBharata B Rao                                            sPAPRMachineState *spapr)
3122af81cf32SBharata B Rao {
3123af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
3124af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
31252e886fb3SSam Bobroff     int id = spapr_vcpu_id(cpu);
3126af81cf32SBharata B Rao     void *fdt;
3127af81cf32SBharata B Rao     int offset, fdt_size;
3128af81cf32SBharata B Rao     char *nodename;
3129af81cf32SBharata B Rao 
3130af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
3131af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
3132af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
3133af81cf32SBharata B Rao 
3134af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
3135af81cf32SBharata B Rao     g_free(nodename);
3136af81cf32SBharata B Rao 
3137af81cf32SBharata B Rao     *fdt_offset = offset;
3138af81cf32SBharata B Rao     return fdt;
3139af81cf32SBharata B Rao }
3140af81cf32SBharata B Rao 
3141765d1bddSDavid Gibson /* Callback to be called during DRC release. */
3142765d1bddSDavid Gibson void spapr_core_release(DeviceState *dev)
3143ff9006ddSIgor Mammedov {
3144765d1bddSDavid Gibson     MachineState *ms = MACHINE(qdev_get_hotplug_handler(dev));
314546f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
3146ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3147535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
3148ff9006ddSIgor Mammedov 
314946f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
315046f7afa3SGreg Kurz         sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
315146f7afa3SGreg Kurz         sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(cc));
315246f7afa3SGreg Kurz         const char *typename = object_class_get_name(scc->cpu_class);
315346f7afa3SGreg Kurz         size_t size = object_type_get_instance_size(typename);
315446f7afa3SGreg Kurz         int i;
315546f7afa3SGreg Kurz 
315646f7afa3SGreg Kurz         for (i = 0; i < cc->nr_threads; i++) {
315746f7afa3SGreg Kurz             CPUState *cs = CPU(sc->threads + i * size);
315846f7afa3SGreg Kurz 
315946f7afa3SGreg Kurz             pre_2_10_vmstate_register_dummy_icp(cs->cpu_index);
316046f7afa3SGreg Kurz         }
316146f7afa3SGreg Kurz     }
316246f7afa3SGreg Kurz 
316307572c06SGreg Kurz     assert(core_slot);
3164535455fdSIgor Mammedov     core_slot->cpu = NULL;
3165ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
3166ff9006ddSIgor Mammedov }
3167ff9006ddSIgor Mammedov 
3168115debf2SIgor Mammedov static
3169115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
3170ff9006ddSIgor Mammedov                                Error **errp)
3171ff9006ddSIgor Mammedov {
3172535455fdSIgor Mammedov     int index;
3173535455fdSIgor Mammedov     sPAPRDRConnector *drc;
3174535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3175535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
3176ff9006ddSIgor Mammedov 
3177535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
3178535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
3179535455fdSIgor Mammedov                    cc->core_id);
3180535455fdSIgor Mammedov         return;
3181535455fdSIgor Mammedov     }
3182ff9006ddSIgor Mammedov     if (index == 0) {
3183ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
3184ff9006ddSIgor Mammedov         return;
3185ff9006ddSIgor Mammedov     }
3186ff9006ddSIgor Mammedov 
3187fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt);
3188ff9006ddSIgor Mammedov     g_assert(drc);
3189ff9006ddSIgor Mammedov 
3190a8dc47fdSDavid Gibson     spapr_drc_detach(drc);
3191ff9006ddSIgor Mammedov 
3192ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
3193ff9006ddSIgor Mammedov }
3194ff9006ddSIgor Mammedov 
3195ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3196ff9006ddSIgor Mammedov                             Error **errp)
3197ff9006ddSIgor Mammedov {
3198ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3199ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
320046f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3201ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
3202ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3203ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
3204ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
3205ff9006ddSIgor Mammedov     Error *local_err = NULL;
3206ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
3207535455fdSIgor Mammedov     CPUArchId *core_slot;
3208535455fdSIgor Mammedov     int index;
320994fd9cbaSLaurent Vivier     bool hotplugged = spapr_drc_hotplugged(dev);
3210ff9006ddSIgor Mammedov 
3211535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3212535455fdSIgor Mammedov     if (!core_slot) {
3213535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
3214535455fdSIgor Mammedov                    cc->core_id);
3215535455fdSIgor Mammedov         return;
3216535455fdSIgor Mammedov     }
3217fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt);
3218ff9006ddSIgor Mammedov 
3219c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
3220ff9006ddSIgor Mammedov 
3221e49c63d5SGreg Kurz     if (drc) {
3222e49c63d5SGreg Kurz         void *fdt;
3223e49c63d5SGreg Kurz         int fdt_offset;
3224e49c63d5SGreg Kurz 
3225ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
3226ff9006ddSIgor Mammedov 
32275c1da812SDavid Gibson         spapr_drc_attach(drc, dev, fdt, fdt_offset, &local_err);
3228ff9006ddSIgor Mammedov         if (local_err) {
3229ff9006ddSIgor Mammedov             g_free(fdt);
3230ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
3231ff9006ddSIgor Mammedov             return;
3232ff9006ddSIgor Mammedov         }
3233ff9006ddSIgor Mammedov 
323494fd9cbaSLaurent Vivier         if (hotplugged) {
3235ff9006ddSIgor Mammedov             /*
323694fd9cbaSLaurent Vivier              * Send hotplug notification interrupt to the guest only
323794fd9cbaSLaurent Vivier              * in case of hotplugged CPUs.
3238ff9006ddSIgor Mammedov              */
3239ff9006ddSIgor Mammedov             spapr_hotplug_req_add_by_index(drc);
324094fd9cbaSLaurent Vivier         } else {
324194fd9cbaSLaurent Vivier             spapr_drc_reset(drc);
3242ff9006ddSIgor Mammedov         }
324394fd9cbaSLaurent Vivier     }
324494fd9cbaSLaurent Vivier 
3245535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
324646f7afa3SGreg Kurz 
324746f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
324846f7afa3SGreg Kurz         sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(cc));
324946f7afa3SGreg Kurz         const char *typename = object_class_get_name(scc->cpu_class);
325046f7afa3SGreg Kurz         size_t size = object_type_get_instance_size(typename);
325146f7afa3SGreg Kurz         int i;
325246f7afa3SGreg Kurz 
325346f7afa3SGreg Kurz         for (i = 0; i < cc->nr_threads; i++) {
325446f7afa3SGreg Kurz             sPAPRCPUCore *sc = SPAPR_CPU_CORE(dev);
325546f7afa3SGreg Kurz             void *obj = sc->threads + i * size;
325646f7afa3SGreg Kurz 
325746f7afa3SGreg Kurz             cs = CPU(obj);
325846f7afa3SGreg Kurz             pre_2_10_vmstate_unregister_dummy_icp(cs->cpu_index);
325946f7afa3SGreg Kurz         }
326046f7afa3SGreg Kurz     }
3261ff9006ddSIgor Mammedov }
3262ff9006ddSIgor Mammedov 
3263ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3264ff9006ddSIgor Mammedov                                 Error **errp)
3265ff9006ddSIgor Mammedov {
3266ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
3267ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
3268ff9006ddSIgor Mammedov     Error *local_err = NULL;
3269ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3270ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
3271ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
3272535455fdSIgor Mammedov     CPUArchId *core_slot;
3273535455fdSIgor Mammedov     int index;
3274ff9006ddSIgor Mammedov 
3275c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
3276ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
3277ff9006ddSIgor Mammedov         goto out;
3278ff9006ddSIgor Mammedov     }
3279ff9006ddSIgor Mammedov 
3280ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
3281ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
3282ff9006ddSIgor Mammedov         goto out;
3283ff9006ddSIgor Mammedov     }
3284ff9006ddSIgor Mammedov 
3285ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
3286ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
3287ff9006ddSIgor Mammedov         goto out;
3288ff9006ddSIgor Mammedov     }
3289ff9006ddSIgor Mammedov 
3290459264efSDavid Gibson     /*
3291459264efSDavid Gibson      * In general we should have homogeneous threads-per-core, but old
3292459264efSDavid Gibson      * (pre hotplug support) machine types allow the last core to have
3293459264efSDavid Gibson      * reduced threads as a compatibility hack for when we allowed
3294459264efSDavid Gibson      * total vcpus not a multiple of threads-per-core.
3295459264efSDavid Gibson      */
3296459264efSDavid Gibson     if (mc->has_hotpluggable_cpus && (cc->nr_threads != smp_threads)) {
3297df8658deSGreg Kurz         error_setg(&local_err, "invalid nr-threads %d, must be %d",
32988149e299SDavid Gibson                    cc->nr_threads, smp_threads);
3299df8658deSGreg Kurz         goto out;
33008149e299SDavid Gibson     }
33018149e299SDavid Gibson 
3302535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3303535455fdSIgor Mammedov     if (!core_slot) {
3304ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
3305ff9006ddSIgor Mammedov         goto out;
3306ff9006ddSIgor Mammedov     }
3307ff9006ddSIgor Mammedov 
3308535455fdSIgor Mammedov     if (core_slot->cpu) {
3309ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
3310ff9006ddSIgor Mammedov         goto out;
3311ff9006ddSIgor Mammedov     }
3312ff9006ddSIgor Mammedov 
3313a0ceb640SIgor Mammedov     numa_cpu_pre_plug(core_slot, dev, &local_err);
33140b8497f0SIgor Mammedov 
3315ff9006ddSIgor Mammedov out:
3316ff9006ddSIgor Mammedov     g_free(base_core_type);
3317ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
3318ff9006ddSIgor Mammedov }
3319ff9006ddSIgor Mammedov 
3320c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
3321c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
3322c20d332aSBharata B Rao {
3323c86c1affSDaniel Henrique Barboza     MachineState *ms = MACHINE(hotplug_dev);
3324c86c1affSDaniel Henrique Barboza     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
3325c20d332aSBharata B Rao 
3326c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3327b556854bSBharata B Rao         int node;
3328c20d332aSBharata B Rao 
3329c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
3330c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
3331c20d332aSBharata B Rao             return;
3332c20d332aSBharata B Rao         }
33339ed442b8SMarc-André Lureau         node = object_property_get_uint(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
3334c20d332aSBharata B Rao         if (*errp) {
3335c20d332aSBharata B Rao             return;
3336c20d332aSBharata B Rao         }
33371a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
33381a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
33391a5512bbSGonglei             return;
33401a5512bbSGonglei         }
3341c20d332aSBharata B Rao 
3342b556854bSBharata B Rao         /*
3343b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
3344b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
3345b556854bSBharata B Rao          * to the first node that has some memory. This causes two
3346b556854bSBharata B Rao          * unexpected behaviours for the user.
3347b556854bSBharata B Rao          *
3348b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
3349b556854bSBharata B Rao          *   specified.
3350b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
3351b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
3352b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
3353b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
3354b556854bSBharata B Rao          *
3355b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
3356b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
3357b556854bSBharata B Rao          */
3358b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
3359b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
3360b556854bSBharata B Rao                        node);
3361b556854bSBharata B Rao             return;
3362b556854bSBharata B Rao         }
3363b556854bSBharata B Rao 
3364c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
3365af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3366af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
3367c20d332aSBharata B Rao     }
3368c20d332aSBharata B Rao }
3369c20d332aSBharata B Rao 
3370cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
3371cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
3372cf632463SBharata B Rao {
3373c86c1affSDaniel Henrique Barboza     sPAPRMachineState *sms = SPAPR_MACHINE(OBJECT(hotplug_dev));
3374c86c1affSDaniel Henrique Barboza     MachineClass *mc = MACHINE_GET_CLASS(sms);
3375cf632463SBharata B Rao 
3376cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3377cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3378cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
3379cf632463SBharata B Rao         } else {
3380cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
3381cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
3382cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
3383cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
3384cf632463SBharata B Rao              * eventually handled by the guest after boot
3385cf632463SBharata B Rao              */
3386cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
3387cf632463SBharata B Rao         }
33886f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3389c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
33906f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
33916f4b5c3eSBharata B Rao             return;
33926f4b5c3eSBharata B Rao         }
3393115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
3394c20d332aSBharata B Rao     }
3395c20d332aSBharata B Rao }
3396c20d332aSBharata B Rao 
339794a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
339894a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
339994a94e4cSBharata B Rao {
3400c871bc70SLaurent Vivier     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3401c871bc70SLaurent Vivier         spapr_memory_pre_plug(hotplug_dev, dev, errp);
3402c871bc70SLaurent Vivier     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
340394a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
340494a94e4cSBharata B Rao     }
340594a94e4cSBharata B Rao }
340694a94e4cSBharata B Rao 
34077ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
3408c20d332aSBharata B Rao                                                  DeviceState *dev)
3409c20d332aSBharata B Rao {
341094a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
341194a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3412c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
3413c20d332aSBharata B Rao     }
3414c20d332aSBharata B Rao     return NULL;
3415c20d332aSBharata B Rao }
3416c20d332aSBharata B Rao 
3417ea089eebSIgor Mammedov static CpuInstanceProperties
3418ea089eebSIgor Mammedov spapr_cpu_index_to_props(MachineState *machine, unsigned cpu_index)
341920bb648dSDavid Gibson {
3420ea089eebSIgor Mammedov     CPUArchId *core_slot;
3421ea089eebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3422ea089eebSIgor Mammedov 
3423ea089eebSIgor Mammedov     /* make sure possible_cpu are intialized */
3424ea089eebSIgor Mammedov     mc->possible_cpu_arch_ids(machine);
3425ea089eebSIgor Mammedov     /* get CPU core slot containing thread that matches cpu_index */
3426ea089eebSIgor Mammedov     core_slot = spapr_find_cpu_slot(machine, cpu_index, NULL);
3427ea089eebSIgor Mammedov     assert(core_slot);
3428ea089eebSIgor Mammedov     return core_slot->props;
342920bb648dSDavid Gibson }
343020bb648dSDavid Gibson 
343179e07936SIgor Mammedov static int64_t spapr_get_default_cpu_node_id(const MachineState *ms, int idx)
343279e07936SIgor Mammedov {
343379e07936SIgor Mammedov     return idx / smp_cores % nb_numa_nodes;
343479e07936SIgor Mammedov }
343579e07936SIgor Mammedov 
3436535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
3437535455fdSIgor Mammedov {
3438535455fdSIgor Mammedov     int i;
3439535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
3440535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3441535455fdSIgor Mammedov 
3442c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
3443535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
3444535455fdSIgor Mammedov     }
3445535455fdSIgor Mammedov     if (machine->possible_cpus) {
3446535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
3447535455fdSIgor Mammedov         return machine->possible_cpus;
3448535455fdSIgor Mammedov     }
3449535455fdSIgor Mammedov 
3450535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
3451535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
3452535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
3453535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
3454535455fdSIgor Mammedov         int core_id = i * smp_threads;
3455535455fdSIgor Mammedov 
3456f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
3457535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
3458535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
3459535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
3460535455fdSIgor Mammedov     }
3461535455fdSIgor Mammedov     return machine->possible_cpus;
3462535455fdSIgor Mammedov }
3463535455fdSIgor Mammedov 
34646737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
3465daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
3466daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
34676737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
34686737d9adSDavid Gibson {
3469357d1e3bSDavid Gibson     /*
3470357d1e3bSDavid Gibson      * New-style PHB window placement.
3471357d1e3bSDavid Gibson      *
3472357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
3473357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
3474357d1e3bSDavid Gibson      * windows.
3475357d1e3bSDavid Gibson      *
3476357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
3477357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
3478357d1e3bSDavid Gibson      *
3479357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
3480357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
3481357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
3482357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
3483357d1e3bSDavid Gibson      */
34846737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
348525e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
348625e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
34876737d9adSDavid Gibson     int i;
34886737d9adSDavid Gibson 
3489357d1e3bSDavid Gibson     /* Sanity check natural alignments */
3490357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3491357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3492357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
3493357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
3494357d1e3bSDavid Gibson     /* Sanity check bounds */
349525e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
349625e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
349725e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
349825e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
34992efff1c0SDavid Gibson 
350025e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
350125e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
350225e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
35036737d9adSDavid Gibson         return;
35046737d9adSDavid Gibson     }
35056737d9adSDavid Gibson 
35066737d9adSDavid Gibson     *buid = base_buid + index;
35076737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
35086737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
35096737d9adSDavid Gibson     }
35106737d9adSDavid Gibson 
3511357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
3512357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
3513357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
35146737d9adSDavid Gibson }
35156737d9adSDavid Gibson 
35167844e12bSCédric Le Goater static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
35177844e12bSCédric Le Goater {
35187844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
35197844e12bSCédric Le Goater 
35207844e12bSCédric Le Goater     return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
35217844e12bSCédric Le Goater }
35227844e12bSCédric Le Goater 
35237844e12bSCédric Le Goater static void spapr_ics_resend(XICSFabric *dev)
35247844e12bSCédric Le Goater {
35257844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
35267844e12bSCédric Le Goater 
35277844e12bSCédric Le Goater     ics_resend(spapr->ics);
35287844e12bSCédric Le Goater }
35297844e12bSCédric Le Goater 
353081210c20SSam Bobroff static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
3531b2fc59aaSCédric Le Goater {
35322e886fb3SSam Bobroff     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
3533b2fc59aaSCédric Le Goater 
35345bc8d26dSCédric Le Goater     return cpu ? ICP(cpu->intc) : NULL;
3535b2fc59aaSCédric Le Goater }
3536b2fc59aaSCédric Le Goater 
35376449da45SCédric Le Goater static void spapr_pic_print_info(InterruptStatsProvider *obj,
35386449da45SCédric Le Goater                                  Monitor *mon)
35396449da45SCédric Le Goater {
35406449da45SCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
35415bc8d26dSCédric Le Goater     CPUState *cs;
35426449da45SCédric Le Goater 
35435bc8d26dSCédric Le Goater     CPU_FOREACH(cs) {
35445bc8d26dSCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
35455bc8d26dSCédric Le Goater 
35465bc8d26dSCédric Le Goater         icp_pic_print_info(ICP(cpu->intc), mon);
35476449da45SCédric Le Goater     }
35486449da45SCédric Le Goater 
35496449da45SCédric Le Goater     ics_pic_print_info(spapr->ics, mon);
35506449da45SCédric Le Goater }
35516449da45SCédric Le Goater 
35522e886fb3SSam Bobroff int spapr_vcpu_id(PowerPCCPU *cpu)
35532e886fb3SSam Bobroff {
35542e886fb3SSam Bobroff     CPUState *cs = CPU(cpu);
35552e886fb3SSam Bobroff 
35562e886fb3SSam Bobroff     if (kvm_enabled()) {
35572e886fb3SSam Bobroff         return kvm_arch_vcpu_id(cs);
35582e886fb3SSam Bobroff     } else {
35592e886fb3SSam Bobroff         return cs->cpu_index;
35602e886fb3SSam Bobroff     }
35612e886fb3SSam Bobroff }
35622e886fb3SSam Bobroff 
35632e886fb3SSam Bobroff PowerPCCPU *spapr_find_cpu(int vcpu_id)
35642e886fb3SSam Bobroff {
35652e886fb3SSam Bobroff     CPUState *cs;
35662e886fb3SSam Bobroff 
35672e886fb3SSam Bobroff     CPU_FOREACH(cs) {
35682e886fb3SSam Bobroff         PowerPCCPU *cpu = POWERPC_CPU(cs);
35692e886fb3SSam Bobroff 
35702e886fb3SSam Bobroff         if (spapr_vcpu_id(cpu) == vcpu_id) {
35712e886fb3SSam Bobroff             return cpu;
35722e886fb3SSam Bobroff         }
35732e886fb3SSam Bobroff     }
35742e886fb3SSam Bobroff 
35752e886fb3SSam Bobroff     return NULL;
35762e886fb3SSam Bobroff }
35772e886fb3SSam Bobroff 
357829ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
357953018216SPaolo Bonzini {
358029ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
3581224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
358271461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
358334316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
3584c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
35851d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
35867844e12bSCédric Le Goater     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
35876449da45SCédric Le Goater     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
358829ee3247SAlexey Kardashevskiy 
35890eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
3590fc9f38c3SDavid Gibson 
3591fc9f38c3SDavid Gibson     /*
3592fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
3593fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
3594fc9f38c3SDavid Gibson      * these details for backwards compatibility
3595fc9f38c3SDavid Gibson      */
3596958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
3597958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
3598958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
35996244bb7eSGreg Kurz     mc->max_cpus = 1024;
3600958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
36015b2128d2SAlexander Graf     mc->default_boot_order = "";
3602a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
3603958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
36049e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
3605e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
36067ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
360794a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
3608c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
3609ea089eebSIgor Mammedov     mc->cpu_index_to_instance_props = spapr_cpu_index_to_props;
361079e07936SIgor Mammedov     mc->get_default_cpu_node_id = spapr_get_default_cpu_node_id;
3611535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3612cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
361300b4fbe2SMarcel Apfelbaum 
3614fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
36153daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
3616c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
361752b81ab5SDavid Gibson     smc->resize_hpt_default = SPAPR_RESIZE_HPT_ENABLED;
361871461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
361934316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
36206737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
36211d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3622e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3623e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3624e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3625e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
36269861bb3eSSuraj Jitindar Singh     vhc->get_patbe = spapr_get_patbe;
36271ec26c75SGreg Kurz     vhc->encode_hpt_for_kvm_pr = spapr_encode_hpt_for_kvm_pr;
36287844e12bSCédric Le Goater     xic->ics_get = spapr_ics_get;
36297844e12bSCédric Le Goater     xic->ics_resend = spapr_ics_resend;
3630b2fc59aaSCédric Le Goater     xic->icp_get = spapr_icp_get;
36316449da45SCédric Le Goater     ispc->print_info = spapr_pic_print_info;
363255641213SLaurent Vivier     /* Force NUMA node memory size to be a multiple of
363355641213SLaurent Vivier      * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
363455641213SLaurent Vivier      * in which LMBs are represented and hot-added
363555641213SLaurent Vivier      */
363655641213SLaurent Vivier     mc->numa_mem_align_shift = 28;
363753018216SPaolo Bonzini }
363853018216SPaolo Bonzini 
363929ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
364029ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
364129ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
36424aee7362SDavid Gibson     .abstract      = true,
36436ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
364423825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
364587bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3646183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
364729ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
364871461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
364971461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
365034316482SAlexey Kardashevskiy         { TYPE_NMI },
3651c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
36521d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
36537844e12bSCédric Le Goater         { TYPE_XICS_FABRIC },
36546449da45SCédric Le Goater         { TYPE_INTERRUPT_STATS_PROVIDER },
365571461b0fSAlexey Kardashevskiy         { }
365671461b0fSAlexey Kardashevskiy     },
365729ee3247SAlexey Kardashevskiy };
365829ee3247SAlexey Kardashevskiy 
3659fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
36605013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
36615013c547SDavid Gibson                                                     void *data)      \
36625013c547SDavid Gibson     {                                                                \
36635013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
36645013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3665fccbc785SDavid Gibson         if (latest) {                                                \
3666fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3667fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3668fccbc785SDavid Gibson         }                                                            \
36695013c547SDavid Gibson     }                                                                \
36705013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
36715013c547SDavid Gibson     {                                                                \
36725013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
36735013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
36745013c547SDavid Gibson     }                                                                \
36755013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
36765013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
36775013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
36785013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
36795013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
36805013c547SDavid Gibson     };                                                               \
36815013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
36825013c547SDavid Gibson     {                                                                \
36835013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
36845013c547SDavid Gibson     }                                                                \
36850e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
36865013c547SDavid Gibson 
36871c5f29bbSDavid Gibson /*
3688e2676b16SGreg Kurz  * pseries-2.11
3689e2676b16SGreg Kurz  */
3690e2676b16SGreg Kurz static void spapr_machine_2_11_instance_options(MachineState *machine)
3691e2676b16SGreg Kurz {
3692e2676b16SGreg Kurz }
3693e2676b16SGreg Kurz 
3694e2676b16SGreg Kurz static void spapr_machine_2_11_class_options(MachineClass *mc)
3695e2676b16SGreg Kurz {
3696e2676b16SGreg Kurz     /* Defaults for the latest behaviour inherited from the base class */
3697e2676b16SGreg Kurz }
3698e2676b16SGreg Kurz 
3699e2676b16SGreg Kurz DEFINE_SPAPR_MACHINE(2_11, "2.11", true);
3700e2676b16SGreg Kurz 
3701e2676b16SGreg Kurz /*
37023fa14fbeSDavid Gibson  * pseries-2.10
3703db800b21SDavid Gibson  */
3704e2676b16SGreg Kurz #define SPAPR_COMPAT_2_10                                              \
3705e2676b16SGreg Kurz     HW_COMPAT_2_10                                                     \
3706e2676b16SGreg Kurz 
37073fa14fbeSDavid Gibson static void spapr_machine_2_10_instance_options(MachineState *machine)
3708db800b21SDavid Gibson {
3709db800b21SDavid Gibson }
3710db800b21SDavid Gibson 
37113fa14fbeSDavid Gibson static void spapr_machine_2_10_class_options(MachineClass *mc)
3712db800b21SDavid Gibson {
3713e2676b16SGreg Kurz     spapr_machine_2_11_class_options(mc);
3714e2676b16SGreg Kurz     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_10);
3715db800b21SDavid Gibson }
3716db800b21SDavid Gibson 
3717e2676b16SGreg Kurz DEFINE_SPAPR_MACHINE(2_10, "2.10", false);
37183fa14fbeSDavid Gibson 
37193fa14fbeSDavid Gibson /*
37203fa14fbeSDavid Gibson  * pseries-2.9
37213fa14fbeSDavid Gibson  */
37223fa14fbeSDavid Gibson #define SPAPR_COMPAT_2_9                                               \
3723d5fc133eSDavid Gibson     HW_COMPAT_2_9                                                      \
3724d5fc133eSDavid Gibson     {                                                                  \
3725d5fc133eSDavid Gibson         .driver = TYPE_POWERPC_CPU,                                    \
3726d5fc133eSDavid Gibson         .property = "pre-2.10-migration",                              \
3727d5fc133eSDavid Gibson         .value    = "on",                                              \
3728d5fc133eSDavid Gibson     },                                                                 \
37293fa14fbeSDavid Gibson 
37303fa14fbeSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
37313fa14fbeSDavid Gibson {
37323fa14fbeSDavid Gibson     spapr_machine_2_10_instance_options(machine);
37333fa14fbeSDavid Gibson }
37343fa14fbeSDavid Gibson 
37353fa14fbeSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
37363fa14fbeSDavid Gibson {
373746f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
373846f7afa3SGreg Kurz 
37393fa14fbeSDavid Gibson     spapr_machine_2_10_class_options(mc);
37403fa14fbeSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_9);
37413bfe5716SLaurent Vivier     mc->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
374246f7afa3SGreg Kurz     smc->pre_2_10_has_unused_icps = true;
374352b81ab5SDavid Gibson     smc->resize_hpt_default = SPAPR_RESIZE_HPT_DISABLED;
37443fa14fbeSDavid Gibson }
37453fa14fbeSDavid Gibson 
37463fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
3747fa325e6cSDavid Gibson 
3748fa325e6cSDavid Gibson /*
3749fa325e6cSDavid Gibson  * pseries-2.8
3750fa325e6cSDavid Gibson  */
3751fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                                        \
375282516263SDavid Gibson     HW_COMPAT_2_8                                               \
375382516263SDavid Gibson     {                                                           \
375482516263SDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,                 \
375582516263SDavid Gibson         .property = "pcie-extended-configuration-space",        \
375682516263SDavid Gibson         .value    = "off",                                      \
375782516263SDavid Gibson     },
3758fa325e6cSDavid Gibson 
3759fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
3760fa325e6cSDavid Gibson {
3761fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
3762fa325e6cSDavid Gibson }
3763fa325e6cSDavid Gibson 
3764fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
3765fa325e6cSDavid Gibson {
3766fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
3767fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
376855641213SLaurent Vivier     mc->numa_mem_align_shift = 23;
3769fa325e6cSDavid Gibson }
3770fa325e6cSDavid Gibson 
3771fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
3772db800b21SDavid Gibson 
3773db800b21SDavid Gibson /*
37741ea1eefcSBharata B Rao  * pseries-2.7
37751ea1eefcSBharata B Rao  */
3776db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3777db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3778357d1e3bSDavid Gibson     {                                               \
3779357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3780357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3781357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3782357d1e3bSDavid Gibson     },                                              \
3783357d1e3bSDavid Gibson     {                                               \
3784357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3785357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3786357d1e3bSDavid Gibson         .value    = "0",                            \
3787146c11f1SDavid Gibson     },                                              \
3788146c11f1SDavid Gibson     {                                               \
3789146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3790146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3791146c11f1SDavid Gibson         .value    = "on",                           \
37925c4537bdSDavid Gibson     },                                              \
37935c4537bdSDavid Gibson     {                                               \
37945c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
37955c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
37965c4537bdSDavid Gibson         .value    = "on",                           \
3797357d1e3bSDavid Gibson     },
3798357d1e3bSDavid Gibson 
3799357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3800357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3801357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3802357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3803357d1e3bSDavid Gibson {
3804357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3805357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3806357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3807357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3808357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3809357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3810357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3811357d1e3bSDavid Gibson 
3812357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3813357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3814357d1e3bSDavid Gibson     int i;
3815357d1e3bSDavid Gibson 
3816357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3817357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3818357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3819357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3820357d1e3bSDavid Gibson          * regions */
3821357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3822357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3823357d1e3bSDavid Gibson     }
3824357d1e3bSDavid Gibson 
3825357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3826357d1e3bSDavid Gibson 
3827357d1e3bSDavid Gibson     if (index > max_index) {
3828357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3829357d1e3bSDavid Gibson                    max_index);
3830357d1e3bSDavid Gibson         return;
3831357d1e3bSDavid Gibson     }
3832357d1e3bSDavid Gibson 
3833357d1e3bSDavid Gibson     *buid = base_buid + index;
3834357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3835357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3836357d1e3bSDavid Gibson     }
3837357d1e3bSDavid Gibson 
3838357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3839357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3840357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3841357d1e3bSDavid Gibson     /*
3842357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3843357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3844357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3845357d1e3bSDavid Gibson      */
3846357d1e3bSDavid Gibson }
3847db800b21SDavid Gibson 
38481ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
38491ea1eefcSBharata B Rao {
3850f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3851f6229214SMichael Roth 
3852672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3853f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
38541ea1eefcSBharata B Rao }
38551ea1eefcSBharata B Rao 
38561ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
38571ea1eefcSBharata B Rao {
38583daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
38593daa4a9fSThomas Huth 
3860db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
38613daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3862db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3863357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
38641ea1eefcSBharata B Rao }
38651ea1eefcSBharata B Rao 
3866db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
38671ea1eefcSBharata B Rao 
38681ea1eefcSBharata B Rao /*
38694b23699cSDavid Gibson  * pseries-2.6
38704b23699cSDavid Gibson  */
38711ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3872ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3873ae4de14cSAlexey Kardashevskiy     { \
3874ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3875ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3876ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3877ae4de14cSAlexey Kardashevskiy     },
38781ea1eefcSBharata B Rao 
38794b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
38804b23699cSDavid Gibson {
3881672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
38824b23699cSDavid Gibson }
38834b23699cSDavid Gibson 
38844b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
38854b23699cSDavid Gibson {
38861ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3887c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
38881ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
38894b23699cSDavid Gibson }
38904b23699cSDavid Gibson 
38911ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
38924b23699cSDavid Gibson 
38934b23699cSDavid Gibson /*
38941c5f29bbSDavid Gibson  * pseries-2.5
38951c5f29bbSDavid Gibson  */
38964b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
389757c522f4SThomas Huth     HW_COMPAT_2_5 \
389857c522f4SThomas Huth     { \
389957c522f4SThomas Huth         .driver   = "spapr-vlan", \
390057c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
390157c522f4SThomas Huth         .value    = "off", \
390257c522f4SThomas Huth     },
39034b23699cSDavid Gibson 
39045013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
39051c5f29bbSDavid Gibson {
3906672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
39075013c547SDavid Gibson }
39085013c547SDavid Gibson 
39095013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
39105013c547SDavid Gibson {
391157040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
391257040d45SThomas Huth 
39134b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
391457040d45SThomas Huth     smc->use_ohci_by_default = true;
39154b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
39161c5f29bbSDavid Gibson }
39171c5f29bbSDavid Gibson 
39184b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
39191c5f29bbSDavid Gibson 
39201c5f29bbSDavid Gibson /*
39211c5f29bbSDavid Gibson  * pseries-2.4
39221c5f29bbSDavid Gibson  */
392380fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
392480fd50f9SCornelia Huck         HW_COMPAT_2_4
392580fd50f9SCornelia Huck 
39265013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
39271c5f29bbSDavid Gibson {
39285013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
39295013c547SDavid Gibson }
39301c5f29bbSDavid Gibson 
39315013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
39325013c547SDavid Gibson {
3933fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3934fc9f38c3SDavid Gibson 
3935fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3936fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3937f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
39381c5f29bbSDavid Gibson }
39391c5f29bbSDavid Gibson 
3940fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
39411c5f29bbSDavid Gibson 
39421c5f29bbSDavid Gibson /*
39431c5f29bbSDavid Gibson  * pseries-2.3
39441c5f29bbSDavid Gibson  */
394538ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
39467619c7b0SMichael Roth         HW_COMPAT_2_3 \
39477619c7b0SMichael Roth         {\
39487619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
39497619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
39507619c7b0SMichael Roth             .value    = "off",\
39517619c7b0SMichael Roth         },
395238ff32c6SEduardo Habkost 
39535013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
39541c5f29bbSDavid Gibson {
39555013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
39561c5f29bbSDavid Gibson }
39571c5f29bbSDavid Gibson 
39585013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
39591c5f29bbSDavid Gibson {
3960fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3961f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
39621c5f29bbSDavid Gibson }
3963fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
39641c5f29bbSDavid Gibson 
39651c5f29bbSDavid Gibson /*
39661c5f29bbSDavid Gibson  * pseries-2.2
39671c5f29bbSDavid Gibson  */
39681c5f29bbSDavid Gibson 
3969b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
39704dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3971b194df47SAlexey Kardashevskiy         {\
3972b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3973b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3974b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3975dd754bafSEduardo Habkost         },
3976b194df47SAlexey Kardashevskiy 
39775013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3978b0e966d0SJason Wang {
39795013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3980cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3981b0e966d0SJason Wang }
3982b0e966d0SJason Wang 
39835013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3984b0e966d0SJason Wang {
3985fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3986f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
39871c5f29bbSDavid Gibson }
3988fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
39891c5f29bbSDavid Gibson 
39901c5f29bbSDavid Gibson /*
39911c5f29bbSDavid Gibson  * pseries-2.1
39921c5f29bbSDavid Gibson  */
39931c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
39941c5f29bbSDavid Gibson         HW_COMPAT_2_1
39951c5f29bbSDavid Gibson 
39965013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
39971c5f29bbSDavid Gibson {
39985013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
39991c5f29bbSDavid Gibson }
40001c5f29bbSDavid Gibson 
40015013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
4002b0e966d0SJason Wang {
4003fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
4004f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
40056026db45SAlexey Kardashevskiy }
4006fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
40076026db45SAlexey Kardashevskiy 
400829ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
400929ee3247SAlexey Kardashevskiy {
401029ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
401129ee3247SAlexey Kardashevskiy }
401229ee3247SAlexey Kardashevskiy 
401329ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
4014