xref: /openbmc/qemu/hw/ppc/spapr.c (revision 4e5fe368)
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"
477abd43baSSuraj Jitindar Singh #include "cpu-models.h"
483794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4953018216SPaolo Bonzini 
5053018216SPaolo Bonzini #include "hw/boards.h"
510d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
5253018216SPaolo Bonzini #include "hw/loader.h"
5353018216SPaolo Bonzini 
547804c353SCédric Le Goater #include "hw/ppc/fdt.h"
550d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
560d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
570d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
580d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5953018216SPaolo Bonzini #include "hw/pci/msi.h"
6053018216SPaolo Bonzini 
6153018216SPaolo Bonzini #include "hw/pci/pci.h"
6271461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
6371461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
64c4e13492SFelipe Franciosi #include "hw/virtio/vhost-scsi-common.h"
6553018216SPaolo Bonzini 
6653018216SPaolo Bonzini #include "exec/address-spaces.h"
6753018216SPaolo Bonzini #include "hw/usb.h"
6853018216SPaolo Bonzini #include "qemu/config-file.h"
69135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
702a6593cbSAlexey Kardashevskiy #include "trace.h"
7134316482SAlexey Kardashevskiy #include "hw/nmi.h"
726449da45SCédric Le Goater #include "hw/intc/intc.h"
7353018216SPaolo Bonzini 
7468a27b20SMichael S. Tsirkin #include "hw/compat.h"
75f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7694a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
772474bfd4SIgor Mammedov #include "qmp-commands.h"
7868a27b20SMichael S. Tsirkin 
7953018216SPaolo Bonzini #include <libfdt.h>
8053018216SPaolo Bonzini 
8153018216SPaolo Bonzini /* SLOF memory layout:
8253018216SPaolo Bonzini  *
8353018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
8453018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
8553018216SPaolo Bonzini  *
8653018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8753018216SPaolo Bonzini  * and more
8853018216SPaolo Bonzini  *
8953018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
9053018216SPaolo Bonzini  */
9138b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
9253018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
93b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
9453018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
9553018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9653018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9753018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9853018216SPaolo Bonzini 
9953018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
10053018216SPaolo Bonzini 
10153018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
10253018216SPaolo Bonzini 
10371cd4dacSCédric Le Goater static ICSState *spapr_ics_create(sPAPRMachineState *spapr,
10471cd4dacSCédric Le Goater                                   const char *type_ics,
105817bb6a4SCédric Le Goater                                   int nr_irqs, Error **errp)
106c04d6cfaSAnthony Liguori {
107175d2aa0SGreg Kurz     Error *local_err = NULL;
10871cd4dacSCédric Le Goater     Object *obj;
109c04d6cfaSAnthony Liguori 
11071cd4dacSCédric Le Goater     obj = object_new(type_ics);
111175d2aa0SGreg Kurz     object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort);
112ad265631SGreg Kurz     object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
113ad265631SGreg Kurz                                    &error_abort);
114175d2aa0SGreg Kurz     object_property_set_int(obj, nr_irqs, "nr-irqs", &local_err);
115175d2aa0SGreg Kurz     if (local_err) {
116175d2aa0SGreg Kurz         goto error;
117175d2aa0SGreg Kurz     }
11871cd4dacSCédric Le Goater     object_property_set_bool(obj, true, "realized", &local_err);
119175d2aa0SGreg Kurz     if (local_err) {
120175d2aa0SGreg Kurz         goto error;
121c04d6cfaSAnthony Liguori     }
122c04d6cfaSAnthony Liguori 
12371cd4dacSCédric Le Goater     return ICS_SIMPLE(obj);
124175d2aa0SGreg Kurz 
125175d2aa0SGreg Kurz error:
126175d2aa0SGreg Kurz     error_propagate(errp, local_err);
127175d2aa0SGreg Kurz     return NULL;
1285bc8d26dSCédric Le Goater }
1295bc8d26dSCédric Le Goater 
13046f7afa3SGreg Kurz static bool pre_2_10_vmstate_dummy_icp_needed(void *opaque)
13146f7afa3SGreg Kurz {
13246f7afa3SGreg Kurz     /* Dummy entries correspond to unused ICPState objects in older QEMUs,
13346f7afa3SGreg Kurz      * and newer QEMUs don't even have them. In both cases, we don't want
13446f7afa3SGreg Kurz      * to send anything on the wire.
13546f7afa3SGreg Kurz      */
13646f7afa3SGreg Kurz     return false;
13746f7afa3SGreg Kurz }
13846f7afa3SGreg Kurz 
13946f7afa3SGreg Kurz static const VMStateDescription pre_2_10_vmstate_dummy_icp = {
14046f7afa3SGreg Kurz     .name = "icp/server",
14146f7afa3SGreg Kurz     .version_id = 1,
14246f7afa3SGreg Kurz     .minimum_version_id = 1,
14346f7afa3SGreg Kurz     .needed = pre_2_10_vmstate_dummy_icp_needed,
14446f7afa3SGreg Kurz     .fields = (VMStateField[]) {
14546f7afa3SGreg Kurz         VMSTATE_UNUSED(4), /* uint32_t xirr */
14646f7afa3SGreg Kurz         VMSTATE_UNUSED(1), /* uint8_t pending_priority */
14746f7afa3SGreg Kurz         VMSTATE_UNUSED(1), /* uint8_t mfrr */
14846f7afa3SGreg Kurz         VMSTATE_END_OF_LIST()
14946f7afa3SGreg Kurz     },
15046f7afa3SGreg Kurz };
15146f7afa3SGreg Kurz 
15246f7afa3SGreg Kurz static void pre_2_10_vmstate_register_dummy_icp(int i)
15346f7afa3SGreg Kurz {
15446f7afa3SGreg Kurz     vmstate_register(NULL, i, &pre_2_10_vmstate_dummy_icp,
15546f7afa3SGreg Kurz                      (void *)(uintptr_t) i);
15646f7afa3SGreg Kurz }
15746f7afa3SGreg Kurz 
15846f7afa3SGreg Kurz static void pre_2_10_vmstate_unregister_dummy_icp(int i)
15946f7afa3SGreg Kurz {
16046f7afa3SGreg Kurz     vmstate_unregister(NULL, &pre_2_10_vmstate_dummy_icp,
16146f7afa3SGreg Kurz                        (void *)(uintptr_t) i);
16246f7afa3SGreg Kurz }
16346f7afa3SGreg Kurz 
16446f7afa3SGreg Kurz static inline int xics_max_server_number(void)
16546f7afa3SGreg Kurz {
16646f7afa3SGreg Kurz     return DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(), smp_threads);
16746f7afa3SGreg Kurz }
16846f7afa3SGreg Kurz 
16971cd4dacSCédric Le Goater static void xics_system_init(MachineState *machine, int nr_irqs, Error **errp)
170c04d6cfaSAnthony Liguori {
17171cd4dacSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
17246f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
173c04d6cfaSAnthony Liguori 
17411ad93f6SDavid Gibson     if (kvm_enabled()) {
1752192a930SCédric Le Goater         if (machine_kernel_irqchip_allowed(machine) &&
17671cd4dacSCédric Le Goater             !xics_kvm_init(spapr, errp)) {
17771cd4dacSCédric Le Goater             spapr->icp_type = TYPE_KVM_ICP;
1783d85885aSGreg Kurz             spapr->ics = spapr_ics_create(spapr, TYPE_ICS_KVM, nr_irqs, errp);
17911ad93f6SDavid Gibson         }
18071cd4dacSCédric Le Goater         if (machine_kernel_irqchip_required(machine) && !spapr->ics) {
1813d85885aSGreg Kurz             error_prepend(errp, "kernel_irqchip requested but unavailable: ");
1823d85885aSGreg Kurz             return;
18311ad93f6SDavid Gibson         }
184b83baa60SMarkus Armbruster     }
18511ad93f6SDavid Gibson 
18671cd4dacSCédric Le Goater     if (!spapr->ics) {
187f63ebfe0SGreg Kurz         xics_spapr_init(spapr);
18871cd4dacSCédric Le Goater         spapr->icp_type = TYPE_ICP;
18971cd4dacSCédric Le Goater         spapr->ics = spapr_ics_create(spapr, TYPE_ICS_SIMPLE, nr_irqs, errp);
1903d85885aSGreg Kurz         if (!spapr->ics) {
1913d85885aSGreg Kurz             return;
1923d85885aSGreg Kurz         }
193c04d6cfaSAnthony Liguori     }
19446f7afa3SGreg Kurz 
19546f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
19646f7afa3SGreg Kurz         int i;
19746f7afa3SGreg Kurz 
19846f7afa3SGreg Kurz         for (i = 0; i < xics_max_server_number(); i++) {
19946f7afa3SGreg Kurz             /* Dummy entries get deregistered when real ICPState objects
20046f7afa3SGreg Kurz              * are registered during CPU core hotplug.
20146f7afa3SGreg Kurz              */
20246f7afa3SGreg Kurz             pre_2_10_vmstate_register_dummy_icp(i);
20346f7afa3SGreg Kurz         }
20446f7afa3SGreg Kurz     }
205c04d6cfaSAnthony Liguori }
206c04d6cfaSAnthony Liguori 
207833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
208833d4668SAlexey Kardashevskiy                                   int smt_threads)
209833d4668SAlexey Kardashevskiy {
210833d4668SAlexey Kardashevskiy     int i, ret = 0;
211833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
212833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
2132e886fb3SSam Bobroff     int index = spapr_vcpu_id(cpu);
214833d4668SAlexey Kardashevskiy 
215d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
216d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
2176d9412eaSAlexey Kardashevskiy         if (ret < 0) {
2186d9412eaSAlexey Kardashevskiy             return ret;
2196d9412eaSAlexey Kardashevskiy         }
2206d9412eaSAlexey Kardashevskiy     }
2216d9412eaSAlexey Kardashevskiy 
222833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
223833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
224833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
225833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
226833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
227833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
228833d4668SAlexey Kardashevskiy     }
229833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
230833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
231833d4668SAlexey Kardashevskiy     if (ret < 0) {
232833d4668SAlexey Kardashevskiy         return ret;
233833d4668SAlexey Kardashevskiy     }
234833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
235833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
236833d4668SAlexey Kardashevskiy 
237833d4668SAlexey Kardashevskiy     return ret;
238833d4668SAlexey Kardashevskiy }
239833d4668SAlexey Kardashevskiy 
24099861ecbSIgor Mammedov static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu)
2410da6f3feSBharata B Rao {
2422e886fb3SSam Bobroff     int index = spapr_vcpu_id(cpu);
2430da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
2440da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2450da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2460da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
24715f8b142SIgor Mammedov                                 cpu_to_be32(cpu->node_id),
2480da6f3feSBharata B Rao                                 cpu_to_be32(index)};
2490da6f3feSBharata B Rao 
2500da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
25199861ecbSIgor Mammedov     return fdt_setprop(fdt, offset, "ibm,associativity", associativity,
2520da6f3feSBharata B Rao                           sizeof(associativity));
2530da6f3feSBharata B Rao }
2540da6f3feSBharata B Rao 
25586d5771aSSam Bobroff /* Populate the "ibm,pa-features" property */
256ee76a09fSDavid Gibson static void spapr_populate_pa_features(sPAPRMachineState *spapr,
257ee76a09fSDavid Gibson                                        PowerPCCPU *cpu,
258ee76a09fSDavid Gibson                                        void *fdt, int offset,
259e957f6a9SSam Bobroff                                        bool legacy_guest)
26086d5771aSSam Bobroff {
2617abd43baSSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
26286d5771aSSam Bobroff     uint8_t pa_features_206[] = { 6, 0,
26386d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
26486d5771aSSam Bobroff     uint8_t pa_features_207[] = { 24, 0,
26586d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
26686d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
26786d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
26886d5771aSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
2699fb4541fSSam Bobroff     uint8_t pa_features_300[] = { 66, 0,
2709fb4541fSSam Bobroff         /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
2719fb4541fSSam Bobroff         /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
27286d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
2739fb4541fSSam Bobroff         /* 6: DS207 */
27486d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
2759fb4541fSSam Bobroff         /* 16: Vector */
27686d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
2779fb4541fSSam Bobroff         /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
2789bf502feSDavid Gibson         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
2799fb4541fSSam Bobroff         /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
2809fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
2819fb4541fSSam Bobroff         /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
2829fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
2839fb4541fSSam Bobroff         /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
2849fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
2859fb4541fSSam Bobroff         /* 42: PM, 44: PC RA, 46: SC vec'd */
2869fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
2879fb4541fSSam Bobroff         /* 48: SIMD, 50: QP BFP, 52: String */
2889fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
2899fb4541fSSam Bobroff         /* 54: DecFP, 56: DecI, 58: SHA */
2909fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
2919fb4541fSSam Bobroff         /* 60: NM atomic, 62: RNG */
2929fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
2939fb4541fSSam Bobroff     };
2947abd43baSSuraj Jitindar Singh     uint8_t *pa_features = NULL;
29586d5771aSSam Bobroff     size_t pa_size;
29686d5771aSSam Bobroff 
2977abd43baSSuraj Jitindar Singh     if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_06, 0, cpu->compat_pvr)) {
29886d5771aSSam Bobroff         pa_features = pa_features_206;
29986d5771aSSam Bobroff         pa_size = sizeof(pa_features_206);
3007abd43baSSuraj Jitindar Singh     }
3017abd43baSSuraj Jitindar Singh     if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_07, 0, cpu->compat_pvr)) {
30286d5771aSSam Bobroff         pa_features = pa_features_207;
30386d5771aSSam Bobroff         pa_size = sizeof(pa_features_207);
3047abd43baSSuraj Jitindar Singh     }
3057abd43baSSuraj Jitindar Singh     if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0, cpu->compat_pvr)) {
30686d5771aSSam Bobroff         pa_features = pa_features_300;
30786d5771aSSam Bobroff         pa_size = sizeof(pa_features_300);
3087abd43baSSuraj Jitindar Singh     }
3097abd43baSSuraj Jitindar Singh     if (!pa_features) {
31086d5771aSSam Bobroff         return;
31186d5771aSSam Bobroff     }
31286d5771aSSam Bobroff 
31386d5771aSSam Bobroff     if (env->ci_large_pages) {
31486d5771aSSam Bobroff         /*
31586d5771aSSam Bobroff          * Note: we keep CI large pages off by default because a 64K capable
31686d5771aSSam Bobroff          * guest provisioned with large pages might otherwise try to map a qemu
31786d5771aSSam Bobroff          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
31886d5771aSSam Bobroff          * even if that qemu runs on a 4k host.
31986d5771aSSam Bobroff          * We dd this bit back here if we are confident this is not an issue
32086d5771aSSam Bobroff          */
32186d5771aSSam Bobroff         pa_features[3] |= 0x20;
32286d5771aSSam Bobroff     }
323*4e5fe368SSuraj Jitindar Singh     if ((spapr_get_cap(spapr, SPAPR_CAP_HTM) != 0) && pa_size > 24) {
32486d5771aSSam Bobroff         pa_features[24] |= 0x80;    /* Transactional memory support */
32586d5771aSSam Bobroff     }
326e957f6a9SSam Bobroff     if (legacy_guest && pa_size > 40) {
327e957f6a9SSam Bobroff         /* Workaround for broken kernels that attempt (guest) radix
328e957f6a9SSam Bobroff          * mode when they can't handle it, if they see the radix bit set
329e957f6a9SSam Bobroff          * in pa-features. So hide it from them. */
330e957f6a9SSam Bobroff         pa_features[40 + 2] &= ~0x80; /* Radix MMU */
331e957f6a9SSam Bobroff     }
33286d5771aSSam Bobroff 
33386d5771aSSam Bobroff     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
33486d5771aSSam Bobroff }
33586d5771aSSam Bobroff 
33628e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
33753018216SPaolo Bonzini {
33882677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
33982677ed2SAlexey Kardashevskiy     CPUState *cs;
34053018216SPaolo Bonzini     char cpu_model[32];
34153018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
34253018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
34353018216SPaolo Bonzini 
34482677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
34582677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
34682677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
3472e886fb3SSam Bobroff         int index = spapr_vcpu_id(cpu);
34812dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
34953018216SPaolo Bonzini 
3500f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
35153018216SPaolo Bonzini             continue;
35253018216SPaolo Bonzini         }
35353018216SPaolo Bonzini 
35482677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
35553018216SPaolo Bonzini 
35682677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
35782677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
358a4f3885cSGreg Kurz             cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
35982677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
36082677ed2SAlexey Kardashevskiy                 return cpus_offset;
36182677ed2SAlexey Kardashevskiy             }
36282677ed2SAlexey Kardashevskiy         }
36382677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
36482677ed2SAlexey Kardashevskiy         if (offset < 0) {
36582677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
36653018216SPaolo Bonzini             if (offset < 0) {
36753018216SPaolo Bonzini                 return offset;
36853018216SPaolo Bonzini             }
36982677ed2SAlexey Kardashevskiy         }
37053018216SPaolo Bonzini 
3710da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
3720da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
37353018216SPaolo Bonzini         if (ret < 0) {
37453018216SPaolo Bonzini             return ret;
37553018216SPaolo Bonzini         }
37653018216SPaolo Bonzini 
37799861ecbSIgor Mammedov         if (nb_numa_nodes > 1) {
37899861ecbSIgor Mammedov             ret = spapr_fixup_cpu_numa_dt(fdt, offset, cpu);
37953018216SPaolo Bonzini             if (ret < 0) {
38053018216SPaolo Bonzini                 return ret;
38153018216SPaolo Bonzini             }
38299861ecbSIgor Mammedov         }
383833d4668SAlexey Kardashevskiy 
38412dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
385833d4668SAlexey Kardashevskiy         if (ret < 0) {
386833d4668SAlexey Kardashevskiy             return ret;
387833d4668SAlexey Kardashevskiy         }
388e957f6a9SSam Bobroff 
389ee76a09fSDavid Gibson         spapr_populate_pa_features(spapr, cpu, fdt, offset,
390e957f6a9SSam Bobroff                                    spapr->cas_legacy_guest_workaround);
39153018216SPaolo Bonzini     }
39253018216SPaolo Bonzini     return ret;
39353018216SPaolo Bonzini }
39453018216SPaolo Bonzini 
395c86c1affSDaniel Henrique Barboza static hwaddr spapr_node0_size(MachineState *machine)
396b082d65aSAlexey Kardashevskiy {
397b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
398b082d65aSAlexey Kardashevskiy         int i;
399b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
400b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
401fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
402fb164994SDavid Gibson                            machine->ram_size);
403b082d65aSAlexey Kardashevskiy             }
404b082d65aSAlexey Kardashevskiy         }
405b082d65aSAlexey Kardashevskiy     }
406fb164994SDavid Gibson     return machine->ram_size;
407b082d65aSAlexey Kardashevskiy }
408b082d65aSAlexey Kardashevskiy 
409a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
410a1d59c0fSAlexey Kardashevskiy {
411a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
412a1d59c0fSAlexey Kardashevskiy }
41353018216SPaolo Bonzini 
41403d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
41526a8c353SAlexey Kardashevskiy                                        hwaddr size)
41626a8c353SAlexey Kardashevskiy {
41726a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
41826a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
41926a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
420c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
42126a8c353SAlexey Kardashevskiy     };
42226a8c353SAlexey Kardashevskiy     char mem_name[32];
42326a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
42426a8c353SAlexey Kardashevskiy     int off;
42526a8c353SAlexey Kardashevskiy 
42626a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
42726a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
42826a8c353SAlexey Kardashevskiy 
42926a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
43026a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
43126a8c353SAlexey Kardashevskiy     _FDT(off);
43226a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
43326a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
43426a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
43526a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
43626a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
43703d196b7SBharata B Rao     return off;
43826a8c353SAlexey Kardashevskiy }
43926a8c353SAlexey Kardashevskiy 
44028e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
44153018216SPaolo Bonzini {
442fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
4437db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
4447db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
4457db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
4467db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
44753018216SPaolo Bonzini 
4487db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
4497db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
4507db8a127SAlexey Kardashevskiy         nb_nodes = 1;
451fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
4527db8a127SAlexey Kardashevskiy         nodes = &ramnode;
4535fe269b1SPaul Mackerras     }
45453018216SPaolo Bonzini 
4557db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
4567db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
4577db8a127SAlexey Kardashevskiy             continue;
45853018216SPaolo Bonzini         }
459fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
4605fe269b1SPaul Mackerras             node_size = 0;
4615fe269b1SPaul Mackerras         } else {
4627db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
463fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
464fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
4655fe269b1SPaul Mackerras             }
4665fe269b1SPaul Mackerras         }
4677db8a127SAlexey Kardashevskiy         if (!mem_start) {
4687db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
469e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
4707db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
4717db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
4727db8a127SAlexey Kardashevskiy         }
4736010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
4746010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
4756010818cSAlexey Kardashevskiy 
4766010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
4776010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
4786010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
4796010818cSAlexey Kardashevskiy             }
4806010818cSAlexey Kardashevskiy 
4816010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
4826010818cSAlexey Kardashevskiy             node_size -= sizetmp;
4836010818cSAlexey Kardashevskiy             mem_start += sizetmp;
4846010818cSAlexey Kardashevskiy         }
48553018216SPaolo Bonzini     }
48653018216SPaolo Bonzini 
48753018216SPaolo Bonzini     return 0;
48853018216SPaolo Bonzini }
48953018216SPaolo Bonzini 
4900da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4910da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4920da6f3feSBharata B Rao {
4930da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4940da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4950da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4962e886fb3SSam Bobroff     int index = spapr_vcpu_id(cpu);
4970da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4980da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
499afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
500afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
5010da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
5020da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
5030da6f3feSBharata B Rao     size_t page_sizes_prop_size;
50422419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
5050da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
50612dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
507af81cf32SBharata B Rao     sPAPRDRConnector *drc;
508af81cf32SBharata B Rao     int drc_index;
509c64abd1fSSam Bobroff     uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
510c64abd1fSSam Bobroff     int i;
511af81cf32SBharata B Rao 
512fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index);
513af81cf32SBharata B Rao     if (drc) {
5140b55aa91SDavid Gibson         drc_index = spapr_drc_index(drc);
515af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
516af81cf32SBharata B Rao     }
5170da6f3feSBharata B Rao 
5180da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
5190da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
5200da6f3feSBharata B Rao 
5210da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
5220da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
5230da6f3feSBharata B Rao                            env->dcache_line_size)));
5240da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
5250da6f3feSBharata B Rao                            env->dcache_line_size)));
5260da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
5270da6f3feSBharata B Rao                            env->icache_line_size)));
5280da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
5290da6f3feSBharata B Rao                            env->icache_line_size)));
5300da6f3feSBharata B Rao 
5310da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
5320da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
5330da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
5340da6f3feSBharata B Rao     } else {
5353dc6f869SAlistair Francis         warn_report("Unknown L1 dcache size for cpu");
5360da6f3feSBharata B Rao     }
5370da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
5380da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
5390da6f3feSBharata B Rao                                pcc->l1_icache_size)));
5400da6f3feSBharata B Rao     } else {
5413dc6f869SAlistair Francis         warn_report("Unknown L1 icache size for cpu");
5420da6f3feSBharata B Rao     }
5430da6f3feSBharata B Rao 
5440da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
5450da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
546fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
5470da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
5480da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
5490da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
5500da6f3feSBharata B Rao 
5510da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
5520da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
5530da6f3feSBharata B Rao     }
5540da6f3feSBharata B Rao 
5550da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
5560da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
5570da6f3feSBharata B Rao                           segs, sizeof(segs))));
5580da6f3feSBharata B Rao     }
5590da6f3feSBharata B Rao 
56029386642SDavid Gibson     /* Advertise VSX (vector extensions) if available
5610da6f3feSBharata B Rao      *   1               == VMX / Altivec available
56229386642SDavid Gibson      *   2               == VSX available
56329386642SDavid Gibson      *
56429386642SDavid Gibson      * Only CPUs for which we create core types in spapr_cpu_core.c
56529386642SDavid Gibson      * are possible, and all of those have VMX */
566*4e5fe368SSuraj Jitindar Singh     if (spapr_get_cap(spapr, SPAPR_CAP_VSX) != 0) {
56729386642SDavid Gibson         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", 2)));
56829386642SDavid Gibson     } else {
56929386642SDavid Gibson         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", 1)));
5700da6f3feSBharata B Rao     }
5710da6f3feSBharata B Rao 
5720da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
5730da6f3feSBharata B Rao      *   0 / no property == no DFP
5740da6f3feSBharata B Rao      *   1               == DFP available */
575*4e5fe368SSuraj Jitindar Singh     if (spapr_get_cap(spapr, SPAPR_CAP_DFP) != 0) {
5760da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
5770da6f3feSBharata B Rao     }
5780da6f3feSBharata B Rao 
5793654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
5800da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
5810da6f3feSBharata B Rao     if (page_sizes_prop_size) {
5820da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
5830da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
5840da6f3feSBharata B Rao     }
5850da6f3feSBharata B Rao 
586ee76a09fSDavid Gibson     spapr_populate_pa_features(spapr, cpu, fdt, offset, false);
58790da0d5aSBenjamin Herrenschmidt 
5880da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
58922419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
5900da6f3feSBharata B Rao 
5910da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
5920da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
5930da6f3feSBharata B Rao 
59499861ecbSIgor Mammedov     if (nb_numa_nodes > 1) {
59599861ecbSIgor Mammedov         _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cpu));
59699861ecbSIgor Mammedov     }
5970da6f3feSBharata B Rao 
59812dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
599c64abd1fSSam Bobroff 
600c64abd1fSSam Bobroff     if (pcc->radix_page_info) {
601c64abd1fSSam Bobroff         for (i = 0; i < pcc->radix_page_info->count; i++) {
602c64abd1fSSam Bobroff             radix_AP_encodings[i] =
603c64abd1fSSam Bobroff                 cpu_to_be32(pcc->radix_page_info->entries[i]);
604c64abd1fSSam Bobroff         }
605c64abd1fSSam Bobroff         _FDT((fdt_setprop(fdt, offset, "ibm,processor-radix-AP-encodings",
606c64abd1fSSam Bobroff                           radix_AP_encodings,
607c64abd1fSSam Bobroff                           pcc->radix_page_info->count *
608c64abd1fSSam Bobroff                           sizeof(radix_AP_encodings[0]))));
609c64abd1fSSam Bobroff     }
6100da6f3feSBharata B Rao }
6110da6f3feSBharata B Rao 
6120da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
6130da6f3feSBharata B Rao {
6140da6f3feSBharata B Rao     CPUState *cs;
6150da6f3feSBharata B Rao     int cpus_offset;
6160da6f3feSBharata B Rao     char *nodename;
6170da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
6180da6f3feSBharata B Rao 
6190da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
6200da6f3feSBharata B Rao     _FDT(cpus_offset);
6210da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
6220da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
6230da6f3feSBharata B Rao 
6240da6f3feSBharata B Rao     /*
6250da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
6260da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
6270da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
6280da6f3feSBharata B Rao      */
6290da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
6300da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
6312e886fb3SSam Bobroff         int index = spapr_vcpu_id(cpu);
6320da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
6330da6f3feSBharata B Rao         int offset;
6340da6f3feSBharata B Rao 
6350da6f3feSBharata B Rao         if ((index % smt) != 0) {
6360da6f3feSBharata B Rao             continue;
6370da6f3feSBharata B Rao         }
6380da6f3feSBharata B Rao 
6390da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
6400da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
6410da6f3feSBharata B Rao         g_free(nodename);
6420da6f3feSBharata B Rao         _FDT(offset);
6430da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
6440da6f3feSBharata B Rao     }
6450da6f3feSBharata B Rao 
6460da6f3feSBharata B Rao }
6470da6f3feSBharata B Rao 
648f47bd1c8SIgor Mammedov static uint32_t spapr_pc_dimm_node(MemoryDeviceInfoList *list, ram_addr_t addr)
649f47bd1c8SIgor Mammedov {
650f47bd1c8SIgor Mammedov     MemoryDeviceInfoList *info;
651f47bd1c8SIgor Mammedov 
652f47bd1c8SIgor Mammedov     for (info = list; info; info = info->next) {
653f47bd1c8SIgor Mammedov         MemoryDeviceInfo *value = info->value;
654f47bd1c8SIgor Mammedov 
655f47bd1c8SIgor Mammedov         if (value && value->type == MEMORY_DEVICE_INFO_KIND_DIMM) {
656f47bd1c8SIgor Mammedov             PCDIMMDeviceInfo *pcdimm_info = value->u.dimm.data;
657f47bd1c8SIgor Mammedov 
658f47bd1c8SIgor Mammedov             if (pcdimm_info->addr >= addr &&
659f47bd1c8SIgor Mammedov                 addr < (pcdimm_info->addr + pcdimm_info->size)) {
660f47bd1c8SIgor Mammedov                 return pcdimm_info->node;
661f47bd1c8SIgor Mammedov             }
662f47bd1c8SIgor Mammedov         }
663f47bd1c8SIgor Mammedov     }
664f47bd1c8SIgor Mammedov 
665f47bd1c8SIgor Mammedov     return -1;
666f47bd1c8SIgor Mammedov }
667f47bd1c8SIgor Mammedov 
66803d196b7SBharata B Rao /*
66903d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
67003d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
67103d196b7SBharata B Rao  * of this device tree node.
67203d196b7SBharata B Rao  */
67303d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
67403d196b7SBharata B Rao {
67503d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
67603d196b7SBharata B Rao     int ret, i, offset;
67703d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
67803d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
679d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
680d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
681d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
682d0e5a8f2SBharata B Rao                        lmb_size;
68303d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
6846663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
685f47bd1c8SIgor Mammedov     MemoryDeviceInfoList *dimms = NULL;
68603d196b7SBharata B Rao 
687ef001f06SThomas Huth     /*
688d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
68916c25aefSBharata B Rao      */
690d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
69116c25aefSBharata B Rao         return 0;
69216c25aefSBharata B Rao     }
69316c25aefSBharata B Rao 
69416c25aefSBharata B Rao     /*
695ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
696ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
697ef001f06SThomas Huth      */
698ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
699ef001f06SThomas Huth               * sizeof(uint32_t);
70003d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
70103d196b7SBharata B Rao 
70203d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
70303d196b7SBharata B Rao 
70403d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
70503d196b7SBharata B Rao                     sizeof(prop_lmb_size));
70603d196b7SBharata B Rao     if (ret < 0) {
70703d196b7SBharata B Rao         goto out;
70803d196b7SBharata B Rao     }
70903d196b7SBharata B Rao 
71003d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
71103d196b7SBharata B Rao     if (ret < 0) {
71203d196b7SBharata B Rao         goto out;
71303d196b7SBharata B Rao     }
71403d196b7SBharata B Rao 
71503d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
71603d196b7SBharata B Rao     if (ret < 0) {
71703d196b7SBharata B Rao         goto out;
71803d196b7SBharata B Rao     }
71903d196b7SBharata B Rao 
720f47bd1c8SIgor Mammedov     if (hotplug_lmb_start) {
721f47bd1c8SIgor Mammedov         MemoryDeviceInfoList **prev = &dimms;
722f47bd1c8SIgor Mammedov         qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
723f47bd1c8SIgor Mammedov     }
724f47bd1c8SIgor Mammedov 
72503d196b7SBharata B Rao     /* ibm,dynamic-memory */
72603d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
72703d196b7SBharata B Rao     cur_index++;
72803d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
729d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
73003d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
73103d196b7SBharata B Rao 
732d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
733d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
734d0e5a8f2SBharata B Rao 
735fbf55397SDavid Gibson             drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, i);
73603d196b7SBharata B Rao             g_assert(drc);
73703d196b7SBharata B Rao 
73803d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
73903d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
7400b55aa91SDavid Gibson             dynamic_memory[2] = cpu_to_be32(spapr_drc_index(drc));
74103d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
742f47bd1c8SIgor Mammedov             dynamic_memory[4] = cpu_to_be32(spapr_pc_dimm_node(dimms, addr));
743d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
74403d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
74503d196b7SBharata B Rao             } else {
74603d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
74703d196b7SBharata B Rao             }
748d0e5a8f2SBharata B Rao         } else {
749d0e5a8f2SBharata B Rao             /*
750d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
751d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
752d0e5a8f2SBharata B Rao              * and as having no valid DRC.
753d0e5a8f2SBharata B Rao              */
754d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
755d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
756d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
757d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
758d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
759d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
760d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
761d0e5a8f2SBharata B Rao         }
76203d196b7SBharata B Rao 
76303d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
76403d196b7SBharata B Rao     }
765f47bd1c8SIgor Mammedov     qapi_free_MemoryDeviceInfoList(dimms);
76603d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
76703d196b7SBharata B Rao     if (ret < 0) {
76803d196b7SBharata B Rao         goto out;
76903d196b7SBharata B Rao     }
77003d196b7SBharata B Rao 
77103d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
77203d196b7SBharata B Rao     cur_index = int_buf;
7736663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
77403d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
77503d196b7SBharata B Rao     cur_index += 2;
7766663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
77703d196b7SBharata B Rao         uint32_t associativity[] = {
77803d196b7SBharata B Rao             cpu_to_be32(0x0),
77903d196b7SBharata B Rao             cpu_to_be32(0x0),
78003d196b7SBharata B Rao             cpu_to_be32(0x0),
78103d196b7SBharata B Rao             cpu_to_be32(i)
78203d196b7SBharata B Rao         };
78303d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
78403d196b7SBharata B Rao         cur_index += 4;
78503d196b7SBharata B Rao     }
78603d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
78703d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
78803d196b7SBharata B Rao out:
78903d196b7SBharata B Rao     g_free(int_buf);
79003d196b7SBharata B Rao     return ret;
79103d196b7SBharata B Rao }
79203d196b7SBharata B Rao 
7936787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
7946787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
7956787d27bSMichael Roth {
7966787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
797417ece33SMichael Roth     int ret = 0, offset;
7986787d27bSMichael Roth 
7996787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
8006787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
8016787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
8026787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
803417ece33SMichael Roth         if (ret) {
804417ece33SMichael Roth             goto out;
805417ece33SMichael Roth         }
8066787d27bSMichael Roth     }
8076787d27bSMichael Roth 
808417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
809417ece33SMichael Roth     if (offset < 0) {
810417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
811417ece33SMichael Roth         if (offset < 0) {
812417ece33SMichael Roth             return offset;
813417ece33SMichael Roth         }
814417ece33SMichael Roth     }
815417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
816417ece33SMichael Roth                                  "ibm,architecture-vec-5");
817417ece33SMichael Roth 
818417ece33SMichael Roth out:
8196787d27bSMichael Roth     return ret;
8206787d27bSMichael Roth }
8216787d27bSMichael Roth 
82210f12e64SDaniel Henrique Barboza static bool spapr_hotplugged_dev_before_cas(void)
82310f12e64SDaniel Henrique Barboza {
82410f12e64SDaniel Henrique Barboza     Object *drc_container, *obj;
82510f12e64SDaniel Henrique Barboza     ObjectProperty *prop;
82610f12e64SDaniel Henrique Barboza     ObjectPropertyIterator iter;
82710f12e64SDaniel Henrique Barboza 
82810f12e64SDaniel Henrique Barboza     drc_container = container_get(object_get_root(), "/dr-connector");
82910f12e64SDaniel Henrique Barboza     object_property_iter_init(&iter, drc_container);
83010f12e64SDaniel Henrique Barboza     while ((prop = object_property_iter_next(&iter))) {
83110f12e64SDaniel Henrique Barboza         if (!strstart(prop->type, "link<", NULL)) {
83210f12e64SDaniel Henrique Barboza             continue;
83310f12e64SDaniel Henrique Barboza         }
83410f12e64SDaniel Henrique Barboza         obj = object_property_get_link(drc_container, prop->name, NULL);
83510f12e64SDaniel Henrique Barboza         if (spapr_drc_needed(obj)) {
83610f12e64SDaniel Henrique Barboza             return true;
83710f12e64SDaniel Henrique Barboza         }
83810f12e64SDaniel Henrique Barboza     }
83910f12e64SDaniel Henrique Barboza     return false;
84010f12e64SDaniel Henrique Barboza }
84110f12e64SDaniel Henrique Barboza 
84203d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
84303d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
8446787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
84503d196b7SBharata B Rao {
84603d196b7SBharata B Rao     void *fdt, *fdt_skel;
84703d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
84803d196b7SBharata B Rao 
84910f12e64SDaniel Henrique Barboza     if (spapr_hotplugged_dev_before_cas()) {
85010f12e64SDaniel Henrique Barboza         return 1;
85110f12e64SDaniel Henrique Barboza     }
85210f12e64SDaniel Henrique Barboza 
853827b17c4SGreg Kurz     if (size < sizeof(hdr) || size > FW_MAX_SIZE) {
854827b17c4SGreg Kurz         error_report("SLOF provided an unexpected CAS buffer size "
855827b17c4SGreg Kurz                      TARGET_FMT_lu " (min: %zu, max: %u)",
856827b17c4SGreg Kurz                      size, sizeof(hdr), FW_MAX_SIZE);
857827b17c4SGreg Kurz         exit(EXIT_FAILURE);
858827b17c4SGreg Kurz     }
859827b17c4SGreg Kurz 
86003d196b7SBharata B Rao     size -= sizeof(hdr);
86103d196b7SBharata B Rao 
86210f12e64SDaniel Henrique Barboza     /* Create skeleton */
86303d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
86403d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
86503d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
86603d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
86703d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
86803d196b7SBharata B Rao     fdt = g_malloc0(size);
86903d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
87003d196b7SBharata B Rao     g_free(fdt_skel);
87103d196b7SBharata B Rao 
87203d196b7SBharata B Rao     /* Fixup cpu nodes */
87303d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
87403d196b7SBharata B Rao 
8756787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
8766787d27bSMichael Roth         return -1;
87703d196b7SBharata B Rao     }
87803d196b7SBharata B Rao 
87903d196b7SBharata B Rao     /* Pack resulting tree */
88003d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
88103d196b7SBharata B Rao 
88203d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
88303d196b7SBharata B Rao         trace_spapr_cas_failed(size);
88403d196b7SBharata B Rao         return -1;
88503d196b7SBharata B Rao     }
88603d196b7SBharata B Rao 
88703d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
88803d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
88903d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
89003d196b7SBharata B Rao     g_free(fdt);
89103d196b7SBharata B Rao 
89203d196b7SBharata B Rao     return 0;
89303d196b7SBharata B Rao }
89403d196b7SBharata B Rao 
8953f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
8963f5dabceSDavid Gibson {
8973f5dabceSDavid Gibson     int rtas;
8983f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
8993f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
9003f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
9013f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
9023f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
9033f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
9043f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
9053f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
9063f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
9073f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
9083f5dabceSDavid Gibson     };
9093f5dabceSDavid Gibson 
9103f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
9113f5dabceSDavid Gibson 
9123f5dabceSDavid Gibson     /* hypertas */
9133f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
9143f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
9153f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
9163f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
9173f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
9183f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
9193f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
9203f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
9213f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
9223f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
9233f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
9243f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
9253f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
9263f5dabceSDavid Gibson 
9273f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
9283f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
9293f5dabceSDavid Gibson     }
93030f4b05bSDavid Gibson 
93130f4b05bSDavid Gibson     if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
93230f4b05bSDavid Gibson         add_str(hypertas, "hcall-hpt-resize");
93330f4b05bSDavid Gibson     }
93430f4b05bSDavid Gibson 
9353f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
9363f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
9373f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
9383f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
9393f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
9403f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
9413f5dabceSDavid Gibson 
9423f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
9433f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
9443f5dabceSDavid Gibson 
9453f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
9463f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
9473f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
9483f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
9493f5dabceSDavid Gibson 
9504f441474SDavid Gibson     g_assert(msi_nonbroken);
9513f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
9523f5dabceSDavid Gibson 
9533f5dabceSDavid Gibson     /*
9543f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
9553f5dabceSDavid Gibson      * back to the guest cpu.
9563f5dabceSDavid Gibson      *
9573f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
9583f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
9593f5dabceSDavid Gibson      */
9603f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
9613f5dabceSDavid Gibson 
9623f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
9633f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
9643f5dabceSDavid Gibson 
9653f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
9663f5dabceSDavid Gibson }
9673f5dabceSDavid Gibson 
9689fb4541fSSam Bobroff /* Prepare ibm,arch-vec-5-platform-support, which indicates the MMU features
9699fb4541fSSam Bobroff  * that the guest may request and thus the valid values for bytes 24..26 of
9709fb4541fSSam Bobroff  * option vector 5: */
9719fb4541fSSam Bobroff static void spapr_dt_ov5_platform_support(void *fdt, int chosen)
9729fb4541fSSam Bobroff {
973545d6e2bSSuraj Jitindar Singh     PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
974545d6e2bSSuraj Jitindar Singh 
975f2b14e3aSCédric Le Goater     char val[2 * 4] = {
97621f3f8dbSCédric Le Goater         23, 0x00, /* Xive mode, filled in below. */
9779fb4541fSSam Bobroff         24, 0x00, /* Hash/Radix, filled in below. */
9789fb4541fSSam Bobroff         25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
9799fb4541fSSam Bobroff         26, 0x40, /* Radix options: GTSE == yes. */
9809fb4541fSSam Bobroff     };
9819fb4541fSSam Bobroff 
9827abd43baSSuraj Jitindar Singh     if (!ppc_check_compat(first_ppc_cpu, CPU_POWERPC_LOGICAL_3_00, 0,
9837abd43baSSuraj Jitindar Singh                           first_ppc_cpu->compat_pvr)) {
9847abd43baSSuraj Jitindar Singh         /* If we're in a pre POWER9 compat mode then the guest should do hash */
9857abd43baSSuraj Jitindar Singh         val[3] = 0x00; /* Hash */
9867abd43baSSuraj Jitindar Singh     } else if (kvm_enabled()) {
9879fb4541fSSam Bobroff         if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
988f2b14e3aSCédric Le Goater             val[3] = 0x80; /* OV5_MMU_BOTH */
9899fb4541fSSam Bobroff         } else if (kvmppc_has_cap_mmu_radix()) {
990f2b14e3aSCédric Le Goater             val[3] = 0x40; /* OV5_MMU_RADIX_300 */
9919fb4541fSSam Bobroff         } else {
992f2b14e3aSCédric Le Goater             val[3] = 0x00; /* Hash */
9939fb4541fSSam Bobroff         }
9949fb4541fSSam Bobroff     } else {
9957abd43baSSuraj Jitindar Singh         /* V3 MMU supports both hash and radix in tcg (with dynamic switching) */
996f2b14e3aSCédric Le Goater         val[3] = 0xC0;
997545d6e2bSSuraj Jitindar Singh     }
9989fb4541fSSam Bobroff     _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
9999fb4541fSSam Bobroff                      val, sizeof(val)));
10009fb4541fSSam Bobroff }
10019fb4541fSSam Bobroff 
10027c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
10037c866c6aSDavid Gibson {
10047c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
10057c866c6aSDavid Gibson     int chosen;
10067c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
10077c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
10087c866c6aSDavid Gibson     size_t cb = 0;
10097c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
10107c866c6aSDavid Gibson 
10117c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
10127c866c6aSDavid Gibson 
10137c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
10147c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
10157c866c6aSDavid Gibson                           spapr->initrd_base));
10167c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
10177c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
10187c866c6aSDavid Gibson 
10197c866c6aSDavid Gibson     if (spapr->kernel_size) {
10207c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
10217c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
10227c866c6aSDavid Gibson 
10237c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
10247c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
10257c866c6aSDavid Gibson         if (spapr->kernel_le) {
10267c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
10277c866c6aSDavid Gibson         }
10287c866c6aSDavid Gibson     }
10297c866c6aSDavid Gibson     if (boot_menu) {
10307c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
10317c866c6aSDavid Gibson     }
10327c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
10337c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
10347c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
10357c866c6aSDavid Gibson 
10367c866c6aSDavid Gibson     if (cb && bootlist) {
10377c866c6aSDavid Gibson         int i;
10387c866c6aSDavid Gibson 
10397c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
10407c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
10417c866c6aSDavid Gibson                 bootlist[i] = ' ';
10427c866c6aSDavid Gibson             }
10437c866c6aSDavid Gibson         }
10447c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
10457c866c6aSDavid Gibson     }
10467c866c6aSDavid Gibson 
10477c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
10487c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
10497c866c6aSDavid Gibson     }
10507c866c6aSDavid Gibson 
10517c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
10527c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
10537c866c6aSDavid Gibson     }
10547c866c6aSDavid Gibson 
10559fb4541fSSam Bobroff     spapr_dt_ov5_platform_support(fdt, chosen);
10569fb4541fSSam Bobroff 
10577c866c6aSDavid Gibson     g_free(stdout_path);
10587c866c6aSDavid Gibson     g_free(bootlist);
10597c866c6aSDavid Gibson }
10607c866c6aSDavid Gibson 
1061fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
1062fca5f2dcSDavid Gibson {
1063fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
1064fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
1065fca5f2dcSDavid Gibson     int hypervisor;
1066fca5f2dcSDavid Gibson     uint8_t hypercall[16];
1067fca5f2dcSDavid Gibson 
1068fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
1069fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
1070fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
1071fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
1072fca5f2dcSDavid Gibson         /*
1073fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
1074fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
1075fca5f2dcSDavid Gibson          */
1076fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
1077fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
1078fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
1079fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
1080fca5f2dcSDavid Gibson         }
1081fca5f2dcSDavid Gibson     }
1082fca5f2dcSDavid Gibson }
1083fca5f2dcSDavid Gibson 
1084997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
108553018216SPaolo Bonzini                              hwaddr rtas_addr,
108653018216SPaolo Bonzini                              hwaddr rtas_size)
108753018216SPaolo Bonzini {
1088c86c1affSDaniel Henrique Barboza     MachineState *machine = MACHINE(spapr);
10893c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
1090c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
10917c866c6aSDavid Gibson     int ret;
109253018216SPaolo Bonzini     void *fdt;
109353018216SPaolo Bonzini     sPAPRPHBState *phb;
1094398a0bd5SDavid Gibson     char *buf;
109553018216SPaolo Bonzini 
1096398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
1097398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
109853018216SPaolo Bonzini 
1099398a0bd5SDavid Gibson     /* Root node */
1100398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
1101398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
1102398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
1103398a0bd5SDavid Gibson 
1104398a0bd5SDavid Gibson     /*
1105398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
1106398a0bd5SDavid Gibson      * and what is the uuid of the guest
1107398a0bd5SDavid Gibson      */
1108398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
1109398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1110398a0bd5SDavid Gibson         g_free(buf);
1111398a0bd5SDavid Gibson     }
1112398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
1113398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1114398a0bd5SDavid Gibson         g_free(buf);
1115398a0bd5SDavid Gibson     }
1116398a0bd5SDavid Gibson 
1117398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
1118398a0bd5SDavid Gibson 
1119398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1120398a0bd5SDavid Gibson     if (qemu_uuid_set) {
1121398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1122398a0bd5SDavid Gibson     }
1123398a0bd5SDavid Gibson     g_free(buf);
1124398a0bd5SDavid Gibson 
1125398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
1126398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1127398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
1128398a0bd5SDavid Gibson     }
1129398a0bd5SDavid Gibson 
1130398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1131398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
113253018216SPaolo Bonzini 
1133fc7e0765SDavid Gibson     /* /interrupt controller */
1134fc7e0765SDavid Gibson     spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
1135fc7e0765SDavid Gibson 
1136e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
1137e8f986fcSBharata B Rao     if (ret < 0) {
1138ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
1139e8f986fcSBharata B Rao         exit(1);
114053018216SPaolo Bonzini     }
114153018216SPaolo Bonzini 
1142bf5a6696SDavid Gibson     /* /vdevice */
1143bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
114453018216SPaolo Bonzini 
11454d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
11464d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
11474d9392beSThomas Huth         if (ret < 0) {
1148ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
11494d9392beSThomas Huth             exit(1);
11504d9392beSThomas Huth         }
11514d9392beSThomas Huth     }
11524d9392beSThomas Huth 
115353018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
115453018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
115553018216SPaolo Bonzini         if (ret < 0) {
1156da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
115753018216SPaolo Bonzini             exit(1);
115853018216SPaolo Bonzini         }
1159da34fed7SThomas Huth     }
116053018216SPaolo Bonzini 
11610da6f3feSBharata B Rao     /* cpus */
11620da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
116353018216SPaolo Bonzini 
1164c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
1165c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1166c20d332aSBharata B Rao     }
1167c20d332aSBharata B Rao 
1168c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
1169af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1170af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1171af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1172af81cf32SBharata B Rao         if (ret < 0) {
1173af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1174af81cf32SBharata B Rao             exit(1);
1175af81cf32SBharata B Rao         }
1176af81cf32SBharata B Rao     }
1177af81cf32SBharata B Rao 
1178ffb1e275SDavid Gibson     /* /event-sources */
1179ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1180ffb1e275SDavid Gibson 
11813f5dabceSDavid Gibson     /* /rtas */
11823f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
11833f5dabceSDavid Gibson 
11847c866c6aSDavid Gibson     /* /chosen */
11857c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1186cf6e5223SDavid Gibson 
1187fca5f2dcSDavid Gibson     /* /hypervisor */
1188fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1189fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1190fca5f2dcSDavid Gibson     }
1191fca5f2dcSDavid Gibson 
1192cf6e5223SDavid Gibson     /* Build memory reserve map */
1193cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1194cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1195cf6e5223SDavid Gibson     }
1196cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1197cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1198cf6e5223SDavid Gibson     }
1199cf6e5223SDavid Gibson 
12006787d27bSMichael Roth     /* ibm,client-architecture-support updates */
12016787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
12026787d27bSMichael Roth     if (ret < 0) {
12036787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
12046787d27bSMichael Roth         exit(1);
12056787d27bSMichael Roth     }
12066787d27bSMichael Roth 
1207997b6cfcSDavid Gibson     return fdt;
120853018216SPaolo Bonzini }
120953018216SPaolo Bonzini 
121053018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
121153018216SPaolo Bonzini {
121253018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
121353018216SPaolo Bonzini }
121453018216SPaolo Bonzini 
12151d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
12161d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
121753018216SPaolo Bonzini {
121853018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
121953018216SPaolo Bonzini 
12208d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
12218d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
12228d04fb55SJan Kiszka 
122353018216SPaolo Bonzini     if (msr_pr) {
122453018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
122553018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
122653018216SPaolo Bonzini     } else {
122753018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
122853018216SPaolo Bonzini     }
122953018216SPaolo Bonzini }
123053018216SPaolo Bonzini 
12319861bb3eSSuraj Jitindar Singh static uint64_t spapr_get_patbe(PPCVirtualHypervisor *vhyp)
12329861bb3eSSuraj Jitindar Singh {
12339861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
12349861bb3eSSuraj Jitindar Singh 
12359861bb3eSSuraj Jitindar Singh     return spapr->patb_entry;
12369861bb3eSSuraj Jitindar Singh }
12379861bb3eSSuraj Jitindar Singh 
1238e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1239e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1240e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1241e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1242e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1243e6b8fd24SSamuel Mendoza-Jonas 
1244715c5407SDavid Gibson /*
1245715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1246715c5407SDavid Gibson  */
1247715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1248715c5407SDavid Gibson {
124914b0d748SGreg Kurz     Error *local_err = NULL;
125014b0d748SGreg Kurz 
1251715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1252715c5407SDavid Gibson         return spapr->htab_fd;
1253715c5407SDavid Gibson     }
1254715c5407SDavid Gibson 
125514b0d748SGreg Kurz     spapr->htab_fd = kvmppc_get_htab_fd(false, 0, &local_err);
1256715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
125714b0d748SGreg Kurz         error_report_err(local_err);
1258715c5407SDavid Gibson     }
1259715c5407SDavid Gibson 
1260715c5407SDavid Gibson     return spapr->htab_fd;
1261715c5407SDavid Gibson }
1262715c5407SDavid Gibson 
1263b4db5413SSuraj Jitindar Singh void close_htab_fd(sPAPRMachineState *spapr)
1264715c5407SDavid Gibson {
1265715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1266715c5407SDavid Gibson         close(spapr->htab_fd);
1267715c5407SDavid Gibson     }
1268715c5407SDavid Gibson     spapr->htab_fd = -1;
1269715c5407SDavid Gibson }
1270715c5407SDavid Gibson 
1271e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1272e57ca75cSDavid Gibson {
1273e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1274e57ca75cSDavid Gibson 
1275e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1276e57ca75cSDavid Gibson }
1277e57ca75cSDavid Gibson 
12781ec26c75SGreg Kurz static target_ulong spapr_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
12791ec26c75SGreg Kurz {
12801ec26c75SGreg Kurz     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
12811ec26c75SGreg Kurz 
12821ec26c75SGreg Kurz     assert(kvm_enabled());
12831ec26c75SGreg Kurz 
12841ec26c75SGreg Kurz     if (!spapr->htab) {
12851ec26c75SGreg Kurz         return 0;
12861ec26c75SGreg Kurz     }
12871ec26c75SGreg Kurz 
12881ec26c75SGreg Kurz     return (target_ulong)(uintptr_t)spapr->htab | (spapr->htab_shift - 18);
12891ec26c75SGreg Kurz }
12901ec26c75SGreg Kurz 
1291e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1292e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1293e57ca75cSDavid Gibson {
1294e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1295e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1296e57ca75cSDavid Gibson 
1297e57ca75cSDavid Gibson     if (!spapr->htab) {
1298e57ca75cSDavid Gibson         /*
1299e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1300e57ca75cSDavid Gibson          */
1301e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1302e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1303e57ca75cSDavid Gibson         return hptes;
1304e57ca75cSDavid Gibson     }
1305e57ca75cSDavid Gibson 
1306e57ca75cSDavid Gibson     /*
1307e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1308e57ca75cSDavid Gibson      * accessible PTEG.
1309e57ca75cSDavid Gibson      */
1310e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1311e57ca75cSDavid Gibson }
1312e57ca75cSDavid Gibson 
1313e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1314e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1315e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1316e57ca75cSDavid Gibson {
1317e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1318e57ca75cSDavid Gibson 
1319e57ca75cSDavid Gibson     if (!spapr->htab) {
1320e57ca75cSDavid Gibson         g_free((void *)hptes);
1321e57ca75cSDavid Gibson     }
1322e57ca75cSDavid Gibson 
1323e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1324e57ca75cSDavid Gibson }
1325e57ca75cSDavid Gibson 
1326e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1327e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1328e57ca75cSDavid Gibson {
1329e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1330e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1331e57ca75cSDavid Gibson 
1332e57ca75cSDavid Gibson     if (!spapr->htab) {
1333e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1334e57ca75cSDavid Gibson     } else {
1335e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1336e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1337e57ca75cSDavid Gibson     }
1338e57ca75cSDavid Gibson }
1339e57ca75cSDavid Gibson 
13400b0b8310SDavid Gibson int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
13418dfe8e7fSDavid Gibson {
13428dfe8e7fSDavid Gibson     int shift;
13438dfe8e7fSDavid Gibson 
13448dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
13458dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
13468dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
13478dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
13488dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
13498dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
13508dfe8e7fSDavid Gibson     return shift;
13518dfe8e7fSDavid Gibson }
13528dfe8e7fSDavid Gibson 
135306ec79e8SBharata B Rao void spapr_free_hpt(sPAPRMachineState *spapr)
135406ec79e8SBharata B Rao {
135506ec79e8SBharata B Rao     g_free(spapr->htab);
135606ec79e8SBharata B Rao     spapr->htab = NULL;
135706ec79e8SBharata B Rao     spapr->htab_shift = 0;
135806ec79e8SBharata B Rao     close_htab_fd(spapr);
135906ec79e8SBharata B Rao }
136006ec79e8SBharata B Rao 
13612772cf6bSDavid Gibson void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1362c5f54f3eSDavid Gibson                           Error **errp)
136353018216SPaolo Bonzini {
1364c5f54f3eSDavid Gibson     long rc;
136553018216SPaolo Bonzini 
1366c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
136706ec79e8SBharata B Rao     spapr_free_hpt(spapr);
136853018216SPaolo Bonzini 
1369c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1370c5f54f3eSDavid Gibson     if (rc < 0) {
1371c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1372c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1373c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1374c5f54f3eSDavid Gibson                          shift);
1375c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1376c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1377c5f54f3eSDavid Gibson     } else if (rc > 0) {
1378c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1379c5f54f3eSDavid Gibson         if (rc != shift) {
1380c5f54f3eSDavid Gibson             error_setg(errp,
1381c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1382c5f54f3eSDavid Gibson                        shift, rc);
13837735fedaSBharata B Rao         }
13847735fedaSBharata B Rao 
138553018216SPaolo Bonzini         spapr->htab_shift = shift;
1386c18ad9a5SDavid Gibson         spapr->htab = NULL;
1387b817772aSBharata B Rao     } else {
1388c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1389c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1390c5f54f3eSDavid Gibson         int i;
139101a57972SSamuel Mendoza-Jonas 
1392c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1393c5f54f3eSDavid Gibson         if (!spapr->htab) {
1394c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1395c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1396c5f54f3eSDavid Gibson             return;
1397b817772aSBharata B Rao         }
1398b817772aSBharata B Rao 
1399c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1400c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1401b817772aSBharata B Rao 
1402c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1403c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
14047735fedaSBharata B Rao         }
140553018216SPaolo Bonzini     }
1406ee4d9eccSSuraj Jitindar Singh     /* We're setting up a hash table, so that means we're not radix */
1407ee4d9eccSSuraj Jitindar Singh     spapr->patb_entry = 0;
140853018216SPaolo Bonzini }
140953018216SPaolo Bonzini 
1410b4db5413SSuraj Jitindar Singh void spapr_setup_hpt_and_vrma(sPAPRMachineState *spapr)
1411b4db5413SSuraj Jitindar Singh {
14122772cf6bSDavid Gibson     int hpt_shift;
14132772cf6bSDavid Gibson 
14142772cf6bSDavid Gibson     if ((spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED)
14152772cf6bSDavid Gibson         || (spapr->cas_reboot
14162772cf6bSDavid Gibson             && !spapr_ovec_test(spapr->ov5_cas, OV5_HPT_RESIZE))) {
14172772cf6bSDavid Gibson         hpt_shift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size);
14182772cf6bSDavid Gibson     } else {
1419768a20f3SDavid Gibson         uint64_t current_ram_size;
1420768a20f3SDavid Gibson 
1421768a20f3SDavid Gibson         current_ram_size = MACHINE(spapr)->ram_size + get_plugged_memory_size();
1422768a20f3SDavid Gibson         hpt_shift = spapr_hpt_shift_for_ramsize(current_ram_size);
14232772cf6bSDavid Gibson     }
14242772cf6bSDavid Gibson     spapr_reallocate_hpt(spapr, hpt_shift, &error_fatal);
14252772cf6bSDavid Gibson 
1426b4db5413SSuraj Jitindar Singh     if (spapr->vrma_adjust) {
1427c86c1affSDaniel Henrique Barboza         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(MACHINE(spapr)),
1428b4db5413SSuraj Jitindar Singh                                           spapr->htab_shift);
1429b4db5413SSuraj Jitindar Singh     }
1430b4db5413SSuraj Jitindar Singh }
1431b4db5413SSuraj Jitindar Singh 
14324f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
14339e3f9733SAlexander Graf {
14349e3f9733SAlexander Graf     bool matched = false;
14359e3f9733SAlexander Graf 
14369e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
14379e3f9733SAlexander Graf         matched = true;
14389e3f9733SAlexander Graf     }
14399e3f9733SAlexander Graf 
14409e3f9733SAlexander Graf     if (!matched) {
14419e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
14429e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
14439e3f9733SAlexander Graf         exit(1);
14449e3f9733SAlexander Graf     }
14459e3f9733SAlexander Graf }
14469e3f9733SAlexander Graf 
144782512483SGreg Kurz static int spapr_reset_drcs(Object *child, void *opaque)
144882512483SGreg Kurz {
144982512483SGreg Kurz     sPAPRDRConnector *drc =
145082512483SGreg Kurz         (sPAPRDRConnector *) object_dynamic_cast(child,
145182512483SGreg Kurz                                                  TYPE_SPAPR_DR_CONNECTOR);
145282512483SGreg Kurz 
145382512483SGreg Kurz     if (drc) {
145482512483SGreg Kurz         spapr_drc_reset(drc);
145582512483SGreg Kurz     }
145682512483SGreg Kurz 
145782512483SGreg Kurz     return 0;
145882512483SGreg Kurz }
145982512483SGreg Kurz 
1460bcb5ce08SDavid Gibson static void spapr_machine_reset(void)
146153018216SPaolo Bonzini {
1462c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1463c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1464182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1465b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1466cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1467997b6cfcSDavid Gibson     void *fdt;
1468997b6cfcSDavid Gibson     int rc;
1469259186a7SAndreas Färber 
14709e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
14719e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
14729e3f9733SAlexander Graf 
147333face6bSDavid Gibson     spapr_caps_reset(spapr);
147433face6bSDavid Gibson 
14751481fe5fSLaurent Vivier     first_ppc_cpu = POWERPC_CPU(first_cpu);
14761481fe5fSLaurent Vivier     if (kvm_enabled() && kvmppc_has_cap_mmu_radix() &&
14771481fe5fSLaurent Vivier         ppc_check_compat(first_ppc_cpu, CPU_POWERPC_LOGICAL_3_00, 0,
14781481fe5fSLaurent Vivier                          spapr->max_compat_pvr)) {
1479b4db5413SSuraj Jitindar Singh         /* If using KVM with radix mode available, VCPUs can be started
1480b4db5413SSuraj Jitindar Singh          * without a HPT because KVM will start them in radix mode.
1481b4db5413SSuraj Jitindar Singh          * Set the GR bit in PATB so that we know there is no HPT. */
1482b4db5413SSuraj Jitindar Singh         spapr->patb_entry = PATBE1_GR;
1483b4db5413SSuraj Jitindar Singh     } else {
1484b4db5413SSuraj Jitindar Singh         spapr_setup_hpt_and_vrma(spapr);
1485c5f54f3eSDavid Gibson     }
148653018216SPaolo Bonzini 
148753018216SPaolo Bonzini     qemu_devices_reset();
148882512483SGreg Kurz 
148982512483SGreg Kurz     /* DRC reset may cause a device to be unplugged. This will cause troubles
149082512483SGreg Kurz      * if this device is used by another device (eg, a running vhost backend
149182512483SGreg Kurz      * will crash QEMU if the DIMM holding the vring goes away). To avoid such
149282512483SGreg Kurz      * situations, we reset DRCs after all devices have been reset.
149382512483SGreg Kurz      */
149482512483SGreg Kurz     object_child_foreach_recursive(object_get_root(), spapr_reset_drcs, NULL);
149582512483SGreg Kurz 
149656258174SDaniel Henrique Barboza     spapr_clear_pending_events(spapr);
149753018216SPaolo Bonzini 
1498b7d1f77aSBenjamin Herrenschmidt     /*
1499b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1500b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1501b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1502b7d1f77aSBenjamin Herrenschmidt      */
1503b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1504cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1505cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1506b7d1f77aSBenjamin Herrenschmidt 
15076787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
15086787d27bSMichael Roth      * negotiated options and start from scratch */
15096787d27bSMichael Roth     if (!spapr->cas_reboot) {
15106787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
15116787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
151266d5c492SDavid Gibson 
151351f84465SDavid Gibson         ppc_set_compat(first_ppc_cpu, spapr->max_compat_pvr, &error_fatal);
15146787d27bSMichael Roth     }
15156787d27bSMichael Roth 
1516cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
151753018216SPaolo Bonzini 
15182cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1519b7d1f77aSBenjamin Herrenschmidt 
1520997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1521997b6cfcSDavid Gibson 
1522997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1523997b6cfcSDavid Gibson     assert(rc == 0);
1524997b6cfcSDavid Gibson 
1525997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1526997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1527997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1528997b6cfcSDavid Gibson         exit(1);
1529997b6cfcSDavid Gibson     }
1530997b6cfcSDavid Gibson 
1531997b6cfcSDavid Gibson     /* Load the fdt */
1532997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1533cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1534997b6cfcSDavid Gibson     g_free(fdt);
1535997b6cfcSDavid Gibson 
153653018216SPaolo Bonzini     /* Set up the entry state */
1537cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1538182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1539182735efSAndreas Färber     first_cpu->halted = 0;
15401b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
154153018216SPaolo Bonzini 
15426787d27bSMichael Roth     spapr->cas_reboot = false;
154353018216SPaolo Bonzini }
154453018216SPaolo Bonzini 
154528e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
154653018216SPaolo Bonzini {
15472ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
15483978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
154953018216SPaolo Bonzini 
15503978b863SPaolo Bonzini     if (dinfo) {
15516231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
15526231a6daSMarkus Armbruster                             &error_fatal);
155353018216SPaolo Bonzini     }
155453018216SPaolo Bonzini 
155553018216SPaolo Bonzini     qdev_init_nofail(dev);
155653018216SPaolo Bonzini 
155753018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
155853018216SPaolo Bonzini }
155953018216SPaolo Bonzini 
156028e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
156128df36a1SDavid Gibson {
1562147ff807SCédric Le Goater     object_initialize(&spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC);
1563147ff807SCédric Le Goater     object_property_add_child(OBJECT(spapr), "rtc", OBJECT(&spapr->rtc),
1564147ff807SCédric Le Goater                               &error_fatal);
1565147ff807SCédric Le Goater     object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1566147ff807SCédric Le Goater                               &error_fatal);
1567147ff807SCédric Le Goater     object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1568147ff807SCédric Le Goater                               "date", &error_fatal);
156928df36a1SDavid Gibson }
157028df36a1SDavid Gibson 
157153018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
157214c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
157353018216SPaolo Bonzini {
157453018216SPaolo Bonzini     switch (vga_interface_type) {
157553018216SPaolo Bonzini     case VGA_NONE:
15767effdaa3SMark Wu         return false;
15777effdaa3SMark Wu     case VGA_DEVICE:
15787effdaa3SMark Wu         return true;
157953018216SPaolo Bonzini     case VGA_STD:
1580b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
158153018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
158253018216SPaolo Bonzini     default:
158314c6a894SDavid Gibson         error_setg(errp,
158414c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
158514c6a894SDavid Gibson         return false;
158653018216SPaolo Bonzini     }
158753018216SPaolo Bonzini }
158853018216SPaolo Bonzini 
1589*4e5fe368SSuraj Jitindar Singh static int spapr_pre_load(void *opaque)
1590*4e5fe368SSuraj Jitindar Singh {
1591*4e5fe368SSuraj Jitindar Singh     int rc;
1592*4e5fe368SSuraj Jitindar Singh 
1593*4e5fe368SSuraj Jitindar Singh     rc = spapr_caps_pre_load(opaque);
1594*4e5fe368SSuraj Jitindar Singh     if (rc) {
1595*4e5fe368SSuraj Jitindar Singh         return rc;
1596*4e5fe368SSuraj Jitindar Singh     }
1597*4e5fe368SSuraj Jitindar Singh 
1598*4e5fe368SSuraj Jitindar Singh     return 0;
1599*4e5fe368SSuraj Jitindar Singh }
1600*4e5fe368SSuraj Jitindar Singh 
1601880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1602880ae7deSDavid Gibson {
160328e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1604880ae7deSDavid Gibson     int err = 0;
1605880ae7deSDavid Gibson 
1606be85537dSDavid Gibson     err = spapr_caps_post_migration(spapr);
1607be85537dSDavid Gibson     if (err) {
1608be85537dSDavid Gibson         return err;
1609be85537dSDavid Gibson     }
1610be85537dSDavid Gibson 
1611a7ff1212SCédric Le Goater     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
16125bc8d26dSCédric Le Goater         CPUState *cs;
16135bc8d26dSCédric Le Goater         CPU_FOREACH(cs) {
16145bc8d26dSCédric Le Goater             PowerPCCPU *cpu = POWERPC_CPU(cs);
16155bc8d26dSCédric Le Goater             icp_resend(ICP(cpu->intc));
1616a7ff1212SCédric Le Goater         }
1617a7ff1212SCédric Le Goater     }
1618a7ff1212SCédric Le Goater 
1619631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1620880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1621880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1622880ae7deSDavid Gibson      * value into the RTC device */
1623880ae7deSDavid Gibson     if (version_id < 3) {
1624147ff807SCédric Le Goater         err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
1625880ae7deSDavid Gibson     }
1626880ae7deSDavid Gibson 
16270c86b2dfSLaurent Vivier     if (kvm_enabled() && spapr->patb_entry) {
1628d39c90f5SBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
1629d39c90f5SBharata B Rao         bool radix = !!(spapr->patb_entry & PATBE1_GR);
1630d39c90f5SBharata B Rao         bool gtse = !!(cpu->env.spr[SPR_LPCR] & LPCR_GTSE);
1631d39c90f5SBharata B Rao 
1632d39c90f5SBharata B Rao         err = kvmppc_configure_v3_mmu(cpu, radix, gtse, spapr->patb_entry);
1633d39c90f5SBharata B Rao         if (err) {
1634d39c90f5SBharata B Rao             error_report("Process table config unsupported by the host");
1635d39c90f5SBharata B Rao             return -EINVAL;
1636d39c90f5SBharata B Rao         }
1637d39c90f5SBharata B Rao     }
1638d39c90f5SBharata B Rao 
1639880ae7deSDavid Gibson     return err;
1640880ae7deSDavid Gibson }
1641880ae7deSDavid Gibson 
1642*4e5fe368SSuraj Jitindar Singh static int spapr_pre_save(void *opaque)
1643*4e5fe368SSuraj Jitindar Singh {
1644*4e5fe368SSuraj Jitindar Singh     int rc;
1645*4e5fe368SSuraj Jitindar Singh 
1646*4e5fe368SSuraj Jitindar Singh     rc = spapr_caps_pre_save(opaque);
1647*4e5fe368SSuraj Jitindar Singh     if (rc) {
1648*4e5fe368SSuraj Jitindar Singh         return rc;
1649*4e5fe368SSuraj Jitindar Singh     }
1650*4e5fe368SSuraj Jitindar Singh 
1651*4e5fe368SSuraj Jitindar Singh     return 0;
1652*4e5fe368SSuraj Jitindar Singh }
1653*4e5fe368SSuraj Jitindar Singh 
1654880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1655880ae7deSDavid Gibson {
1656880ae7deSDavid Gibson     return version_id < 3;
1657880ae7deSDavid Gibson }
1658880ae7deSDavid Gibson 
1659fd38804bSDaniel Henrique Barboza static bool spapr_pending_events_needed(void *opaque)
1660fd38804bSDaniel Henrique Barboza {
1661fd38804bSDaniel Henrique Barboza     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1662fd38804bSDaniel Henrique Barboza     return !QTAILQ_EMPTY(&spapr->pending_events);
1663fd38804bSDaniel Henrique Barboza }
1664fd38804bSDaniel Henrique Barboza 
1665fd38804bSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_event_entry = {
1666fd38804bSDaniel Henrique Barboza     .name = "spapr_event_log_entry",
1667fd38804bSDaniel Henrique Barboza     .version_id = 1,
1668fd38804bSDaniel Henrique Barboza     .minimum_version_id = 1,
1669fd38804bSDaniel Henrique Barboza     .fields = (VMStateField[]) {
16705341258eSDavid Gibson         VMSTATE_UINT32(summary, sPAPREventLogEntry),
16715341258eSDavid Gibson         VMSTATE_UINT32(extended_length, sPAPREventLogEntry),
1672fd38804bSDaniel Henrique Barboza         VMSTATE_VBUFFER_ALLOC_UINT32(extended_log, sPAPREventLogEntry, 0,
16735341258eSDavid Gibson                                      NULL, extended_length),
1674fd38804bSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
1675fd38804bSDaniel Henrique Barboza     },
1676fd38804bSDaniel Henrique Barboza };
1677fd38804bSDaniel Henrique Barboza 
1678fd38804bSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_pending_events = {
1679fd38804bSDaniel Henrique Barboza     .name = "spapr_pending_events",
1680fd38804bSDaniel Henrique Barboza     .version_id = 1,
1681fd38804bSDaniel Henrique Barboza     .minimum_version_id = 1,
1682fd38804bSDaniel Henrique Barboza     .needed = spapr_pending_events_needed,
1683fd38804bSDaniel Henrique Barboza     .fields = (VMStateField[]) {
1684fd38804bSDaniel Henrique Barboza         VMSTATE_QTAILQ_V(pending_events, sPAPRMachineState, 1,
1685fd38804bSDaniel Henrique Barboza                          vmstate_spapr_event_entry, sPAPREventLogEntry, next),
1686fd38804bSDaniel Henrique Barboza         VMSTATE_END_OF_LIST()
1687fd38804bSDaniel Henrique Barboza     },
1688fd38804bSDaniel Henrique Barboza };
1689fd38804bSDaniel Henrique Barboza 
169062ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
169162ef3760SMichael Roth {
169262ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
169362ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
169462ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
169562ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
169662ef3760SMichael Roth     bool cas_needed;
169762ef3760SMichael Roth 
169862ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
169962ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
170062ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
170162ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
170262ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
170362ef3760SMichael Roth      * negotiatied on the source side.
170462ef3760SMichael Roth      *
170562ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
170662ef3760SMichael Roth      * are the only options available on the current machine/platform.
170762ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
170862ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
170962ef3760SMichael Roth      * compatibility.
171062ef3760SMichael Roth      *
171162ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
171262ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
171362ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
171462ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
171562ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
171662ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
171762ef3760SMichael Roth      *
171862ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
171962ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
172062ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
172162ef3760SMichael Roth      */
172262ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
172362ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
172462ef3760SMichael Roth 
172562ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
172662ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
172762ef3760SMichael Roth      * removed via machine options, which could generate a false positive
172862ef3760SMichael Roth      * that breaks migration.
172962ef3760SMichael Roth      */
173062ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
173162ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
173262ef3760SMichael Roth 
173362ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
173462ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
173562ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
173662ef3760SMichael Roth 
173762ef3760SMichael Roth     return cas_needed;
173862ef3760SMichael Roth }
173962ef3760SMichael Roth 
174062ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
174162ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
174262ef3760SMichael Roth     .version_id = 1,
174362ef3760SMichael Roth     .minimum_version_id = 1,
174462ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
174562ef3760SMichael Roth     .fields = (VMStateField[]) {
174662ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
174762ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
174862ef3760SMichael Roth         VMSTATE_END_OF_LIST()
174962ef3760SMichael Roth     },
175062ef3760SMichael Roth };
175162ef3760SMichael Roth 
17529861bb3eSSuraj Jitindar Singh static bool spapr_patb_entry_needed(void *opaque)
17539861bb3eSSuraj Jitindar Singh {
17549861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = opaque;
17559861bb3eSSuraj Jitindar Singh 
17569861bb3eSSuraj Jitindar Singh     return !!spapr->patb_entry;
17579861bb3eSSuraj Jitindar Singh }
17589861bb3eSSuraj Jitindar Singh 
17599861bb3eSSuraj Jitindar Singh static const VMStateDescription vmstate_spapr_patb_entry = {
17609861bb3eSSuraj Jitindar Singh     .name = "spapr_patb_entry",
17619861bb3eSSuraj Jitindar Singh     .version_id = 1,
17629861bb3eSSuraj Jitindar Singh     .minimum_version_id = 1,
17639861bb3eSSuraj Jitindar Singh     .needed = spapr_patb_entry_needed,
17649861bb3eSSuraj Jitindar Singh     .fields = (VMStateField[]) {
17659861bb3eSSuraj Jitindar Singh         VMSTATE_UINT64(patb_entry, sPAPRMachineState),
17669861bb3eSSuraj Jitindar Singh         VMSTATE_END_OF_LIST()
17679861bb3eSSuraj Jitindar Singh     },
17689861bb3eSSuraj Jitindar Singh };
17699861bb3eSSuraj Jitindar Singh 
17704be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
17714be21d56SDavid Gibson     .name = "spapr",
1772880ae7deSDavid Gibson     .version_id = 3,
17734be21d56SDavid Gibson     .minimum_version_id = 1,
1774*4e5fe368SSuraj Jitindar Singh     .pre_load = spapr_pre_load,
1775880ae7deSDavid Gibson     .post_load = spapr_post_load,
1776*4e5fe368SSuraj Jitindar Singh     .pre_save = spapr_pre_save,
17774be21d56SDavid Gibson     .fields = (VMStateField[]) {
1778880ae7deSDavid Gibson         /* used to be @next_irq */
1779880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
17804be21d56SDavid Gibson 
17814be21d56SDavid Gibson         /* RTC offset */
178228e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1783880ae7deSDavid Gibson 
178428e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
17854be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
17864be21d56SDavid Gibson     },
178762ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
178862ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
17899861bb3eSSuraj Jitindar Singh         &vmstate_spapr_patb_entry,
1790fd38804bSDaniel Henrique Barboza         &vmstate_spapr_pending_events,
1791*4e5fe368SSuraj Jitindar Singh         &vmstate_spapr_cap_htm,
1792*4e5fe368SSuraj Jitindar Singh         &vmstate_spapr_cap_vsx,
1793*4e5fe368SSuraj Jitindar Singh         &vmstate_spapr_cap_dfp,
179462ef3760SMichael Roth         NULL
179562ef3760SMichael Roth     }
17964be21d56SDavid Gibson };
17974be21d56SDavid Gibson 
17984be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
17994be21d56SDavid Gibson {
180028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
18014be21d56SDavid Gibson 
18024be21d56SDavid Gibson     /* "Iteration" header */
18033a384297SBharata B Rao     if (!spapr->htab_shift) {
18043a384297SBharata B Rao         qemu_put_be32(f, -1);
18053a384297SBharata B Rao     } else {
18064be21d56SDavid Gibson         qemu_put_be32(f, spapr->htab_shift);
18073a384297SBharata B Rao     }
18084be21d56SDavid Gibson 
1809e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1810e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1811e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1812e68cb8b4SAlexey Kardashevskiy     } else {
18133a384297SBharata B Rao         if (spapr->htab_shift) {
1814e68cb8b4SAlexey Kardashevskiy             assert(kvm_enabled());
18154be21d56SDavid Gibson         }
18163a384297SBharata B Rao     }
18174be21d56SDavid Gibson 
1818e68cb8b4SAlexey Kardashevskiy 
1819e68cb8b4SAlexey Kardashevskiy     return 0;
1820e68cb8b4SAlexey Kardashevskiy }
18214be21d56SDavid Gibson 
1822332f7721SGreg Kurz static void htab_save_chunk(QEMUFile *f, sPAPRMachineState *spapr,
1823332f7721SGreg Kurz                             int chunkstart, int n_valid, int n_invalid)
1824332f7721SGreg Kurz {
1825332f7721SGreg Kurz     qemu_put_be32(f, chunkstart);
1826332f7721SGreg Kurz     qemu_put_be16(f, n_valid);
1827332f7721SGreg Kurz     qemu_put_be16(f, n_invalid);
1828332f7721SGreg Kurz     qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
1829332f7721SGreg Kurz                     HASH_PTE_SIZE_64 * n_valid);
1830332f7721SGreg Kurz }
1831332f7721SGreg Kurz 
1832332f7721SGreg Kurz static void htab_save_end_marker(QEMUFile *f)
1833332f7721SGreg Kurz {
1834332f7721SGreg Kurz     qemu_put_be32(f, 0);
1835332f7721SGreg Kurz     qemu_put_be16(f, 0);
1836332f7721SGreg Kurz     qemu_put_be16(f, 0);
1837332f7721SGreg Kurz }
1838332f7721SGreg Kurz 
183928e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
18404be21d56SDavid Gibson                                  int64_t max_ns)
18414be21d56SDavid Gibson {
1842378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
18434be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
18444be21d56SDavid Gibson     int index = spapr->htab_save_index;
1845bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
18464be21d56SDavid Gibson 
18474be21d56SDavid Gibson     assert(spapr->htab_first_pass);
18484be21d56SDavid Gibson 
18494be21d56SDavid Gibson     do {
18504be21d56SDavid Gibson         int chunkstart;
18514be21d56SDavid Gibson 
18524be21d56SDavid Gibson         /* Consume invalid HPTEs */
18534be21d56SDavid Gibson         while ((index < htabslots)
18544be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
18554be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
185624ec2863SMarc-André Lureau             index++;
18574be21d56SDavid Gibson         }
18584be21d56SDavid Gibson 
18594be21d56SDavid Gibson         /* Consume valid HPTEs */
18604be21d56SDavid Gibson         chunkstart = index;
1861338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
18624be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
18634be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
186424ec2863SMarc-André Lureau             index++;
18654be21d56SDavid Gibson         }
18664be21d56SDavid Gibson 
18674be21d56SDavid Gibson         if (index > chunkstart) {
18684be21d56SDavid Gibson             int n_valid = index - chunkstart;
18694be21d56SDavid Gibson 
1870332f7721SGreg Kurz             htab_save_chunk(f, spapr, chunkstart, n_valid, 0);
18714be21d56SDavid Gibson 
1872378bc217SDavid Gibson             if (has_timeout &&
1873378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
18744be21d56SDavid Gibson                 break;
18754be21d56SDavid Gibson             }
18764be21d56SDavid Gibson         }
18774be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
18784be21d56SDavid Gibson 
18794be21d56SDavid Gibson     if (index >= htabslots) {
18804be21d56SDavid Gibson         assert(index == htabslots);
18814be21d56SDavid Gibson         index = 0;
18824be21d56SDavid Gibson         spapr->htab_first_pass = false;
18834be21d56SDavid Gibson     }
18844be21d56SDavid Gibson     spapr->htab_save_index = index;
18854be21d56SDavid Gibson }
18864be21d56SDavid Gibson 
188728e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
18884be21d56SDavid Gibson                                 int64_t max_ns)
18894be21d56SDavid Gibson {
18904be21d56SDavid Gibson     bool final = max_ns < 0;
18914be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
18924be21d56SDavid Gibson     int examined = 0, sent = 0;
18934be21d56SDavid Gibson     int index = spapr->htab_save_index;
1894bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
18954be21d56SDavid Gibson 
18964be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
18974be21d56SDavid Gibson 
18984be21d56SDavid Gibson     do {
18994be21d56SDavid Gibson         int chunkstart, invalidstart;
19004be21d56SDavid Gibson 
19014be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
19024be21d56SDavid Gibson         while ((index < htabslots)
19034be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
19044be21d56SDavid Gibson             index++;
19054be21d56SDavid Gibson             examined++;
19064be21d56SDavid Gibson         }
19074be21d56SDavid Gibson 
19084be21d56SDavid Gibson         chunkstart = index;
19094be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1910338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
19114be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
19124be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
19134be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
19144be21d56SDavid Gibson             index++;
19154be21d56SDavid Gibson             examined++;
19164be21d56SDavid Gibson         }
19174be21d56SDavid Gibson 
19184be21d56SDavid Gibson         invalidstart = index;
19194be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1920338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
19214be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
19224be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
19234be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
19244be21d56SDavid Gibson             index++;
19254be21d56SDavid Gibson             examined++;
19264be21d56SDavid Gibson         }
19274be21d56SDavid Gibson 
19284be21d56SDavid Gibson         if (index > chunkstart) {
19294be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
19304be21d56SDavid Gibson             int n_invalid = index - invalidstart;
19314be21d56SDavid Gibson 
1932332f7721SGreg Kurz             htab_save_chunk(f, spapr, chunkstart, n_valid, n_invalid);
19334be21d56SDavid Gibson             sent += index - chunkstart;
19344be21d56SDavid Gibson 
1935bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
19364be21d56SDavid Gibson                 break;
19374be21d56SDavid Gibson             }
19384be21d56SDavid Gibson         }
19394be21d56SDavid Gibson 
19404be21d56SDavid Gibson         if (examined >= htabslots) {
19414be21d56SDavid Gibson             break;
19424be21d56SDavid Gibson         }
19434be21d56SDavid Gibson 
19444be21d56SDavid Gibson         if (index >= htabslots) {
19454be21d56SDavid Gibson             assert(index == htabslots);
19464be21d56SDavid Gibson             index = 0;
19474be21d56SDavid Gibson         }
19484be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
19494be21d56SDavid Gibson 
19504be21d56SDavid Gibson     if (index >= htabslots) {
19514be21d56SDavid Gibson         assert(index == htabslots);
19524be21d56SDavid Gibson         index = 0;
19534be21d56SDavid Gibson     }
19544be21d56SDavid Gibson 
19554be21d56SDavid Gibson     spapr->htab_save_index = index;
19564be21d56SDavid Gibson 
1957e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
19584be21d56SDavid Gibson }
19594be21d56SDavid Gibson 
1960e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1961e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1962e68cb8b4SAlexey Kardashevskiy 
19634be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
19644be21d56SDavid Gibson {
196528e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1966715c5407SDavid Gibson     int fd;
1967e68cb8b4SAlexey Kardashevskiy     int rc = 0;
19684be21d56SDavid Gibson 
19694be21d56SDavid Gibson     /* Iteration header */
19703a384297SBharata B Rao     if (!spapr->htab_shift) {
19713a384297SBharata B Rao         qemu_put_be32(f, -1);
1972e8cd4247SLaurent Vivier         return 1;
19733a384297SBharata B Rao     } else {
19744be21d56SDavid Gibson         qemu_put_be32(f, 0);
19753a384297SBharata B Rao     }
19764be21d56SDavid Gibson 
1977e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1978e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1979e68cb8b4SAlexey Kardashevskiy 
1980715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1981715c5407SDavid Gibson         if (fd < 0) {
1982715c5407SDavid Gibson             return fd;
198301a57972SSamuel Mendoza-Jonas         }
198401a57972SSamuel Mendoza-Jonas 
1985715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1986e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1987e68cb8b4SAlexey Kardashevskiy             return rc;
1988e68cb8b4SAlexey Kardashevskiy         }
1989e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
19904be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
19914be21d56SDavid Gibson     } else {
1992e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
19934be21d56SDavid Gibson     }
19944be21d56SDavid Gibson 
1995332f7721SGreg Kurz     htab_save_end_marker(f);
19964be21d56SDavid Gibson 
1997e68cb8b4SAlexey Kardashevskiy     return rc;
19984be21d56SDavid Gibson }
19994be21d56SDavid Gibson 
20004be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
20014be21d56SDavid Gibson {
200228e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
2003715c5407SDavid Gibson     int fd;
20044be21d56SDavid Gibson 
20054be21d56SDavid Gibson     /* Iteration header */
20063a384297SBharata B Rao     if (!spapr->htab_shift) {
20073a384297SBharata B Rao         qemu_put_be32(f, -1);
20083a384297SBharata B Rao         return 0;
20093a384297SBharata B Rao     } else {
20104be21d56SDavid Gibson         qemu_put_be32(f, 0);
20113a384297SBharata B Rao     }
20124be21d56SDavid Gibson 
2013e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
2014e68cb8b4SAlexey Kardashevskiy         int rc;
2015e68cb8b4SAlexey Kardashevskiy 
2016e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
2017e68cb8b4SAlexey Kardashevskiy 
2018715c5407SDavid Gibson         fd = get_htab_fd(spapr);
2019715c5407SDavid Gibson         if (fd < 0) {
2020715c5407SDavid Gibson             return fd;
202101a57972SSamuel Mendoza-Jonas         }
202201a57972SSamuel Mendoza-Jonas 
2023715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
2024e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
2025e68cb8b4SAlexey Kardashevskiy             return rc;
2026e68cb8b4SAlexey Kardashevskiy         }
2027e68cb8b4SAlexey Kardashevskiy     } else {
2028378bc217SDavid Gibson         if (spapr->htab_first_pass) {
2029378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
2030378bc217SDavid Gibson         }
20314be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
2032e68cb8b4SAlexey Kardashevskiy     }
20334be21d56SDavid Gibson 
20344be21d56SDavid Gibson     /* End marker */
2035332f7721SGreg Kurz     htab_save_end_marker(f);
20364be21d56SDavid Gibson 
20374be21d56SDavid Gibson     return 0;
20384be21d56SDavid Gibson }
20394be21d56SDavid Gibson 
20404be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
20414be21d56SDavid Gibson {
204228e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
20434be21d56SDavid Gibson     uint32_t section_hdr;
2044e68cb8b4SAlexey Kardashevskiy     int fd = -1;
204514b0d748SGreg Kurz     Error *local_err = NULL;
20464be21d56SDavid Gibson 
20474be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
204898a5d100SDavid Gibson         error_report("htab_load() bad version");
20494be21d56SDavid Gibson         return -EINVAL;
20504be21d56SDavid Gibson     }
20514be21d56SDavid Gibson 
20524be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
20534be21d56SDavid Gibson 
20543a384297SBharata B Rao     if (section_hdr == -1) {
20553a384297SBharata B Rao         spapr_free_hpt(spapr);
20563a384297SBharata B Rao         return 0;
20573a384297SBharata B Rao     }
20583a384297SBharata B Rao 
20594be21d56SDavid Gibson     if (section_hdr) {
2060c5f54f3eSDavid Gibson         /* First section gives the htab size */
2061c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
2062c5f54f3eSDavid Gibson         if (local_err) {
2063c5f54f3eSDavid Gibson             error_report_err(local_err);
20644be21d56SDavid Gibson             return -EINVAL;
20654be21d56SDavid Gibson         }
20664be21d56SDavid Gibson         return 0;
20674be21d56SDavid Gibson     }
20684be21d56SDavid Gibson 
2069e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
2070e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
2071e68cb8b4SAlexey Kardashevskiy 
207214b0d748SGreg Kurz         fd = kvmppc_get_htab_fd(true, 0, &local_err);
2073e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
207414b0d748SGreg Kurz             error_report_err(local_err);
207582be8e73SGreg Kurz             return fd;
2076e68cb8b4SAlexey Kardashevskiy         }
2077e68cb8b4SAlexey Kardashevskiy     }
2078e68cb8b4SAlexey Kardashevskiy 
20794be21d56SDavid Gibson     while (true) {
20804be21d56SDavid Gibson         uint32_t index;
20814be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
20824be21d56SDavid Gibson 
20834be21d56SDavid Gibson         index = qemu_get_be32(f);
20844be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
20854be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
20864be21d56SDavid Gibson 
20874be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
20884be21d56SDavid Gibson             /* End of Stream */
20894be21d56SDavid Gibson             break;
20904be21d56SDavid Gibson         }
20914be21d56SDavid Gibson 
2092e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
20934be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
20944be21d56SDavid Gibson             /* Bad index in stream */
209598a5d100SDavid Gibson             error_report(
209698a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
209798a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
20984be21d56SDavid Gibson             return -EINVAL;
20994be21d56SDavid Gibson         }
21004be21d56SDavid Gibson 
2101e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
21024be21d56SDavid Gibson             if (n_valid) {
21034be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
21044be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
21054be21d56SDavid Gibson             }
21064be21d56SDavid Gibson             if (n_invalid) {
21074be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
21084be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
21094be21d56SDavid Gibson             }
2110e68cb8b4SAlexey Kardashevskiy         } else {
2111e68cb8b4SAlexey Kardashevskiy             int rc;
2112e68cb8b4SAlexey Kardashevskiy 
2113e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
2114e68cb8b4SAlexey Kardashevskiy 
2115e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
2116e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
2117e68cb8b4SAlexey Kardashevskiy                 return rc;
2118e68cb8b4SAlexey Kardashevskiy             }
2119e68cb8b4SAlexey Kardashevskiy         }
2120e68cb8b4SAlexey Kardashevskiy     }
2121e68cb8b4SAlexey Kardashevskiy 
2122e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
2123e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
2124e68cb8b4SAlexey Kardashevskiy         close(fd);
21254be21d56SDavid Gibson     }
21264be21d56SDavid Gibson 
21274be21d56SDavid Gibson     return 0;
21284be21d56SDavid Gibson }
21294be21d56SDavid Gibson 
213070f794fcSJuan Quintela static void htab_save_cleanup(void *opaque)
2131c573fc03SThomas Huth {
2132c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
2133c573fc03SThomas Huth 
2134c573fc03SThomas Huth     close_htab_fd(spapr);
2135c573fc03SThomas Huth }
2136c573fc03SThomas Huth 
21374be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
21389907e842SJuan Quintela     .save_setup = htab_save_setup,
21394be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
2140a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
214170f794fcSJuan Quintela     .save_cleanup = htab_save_cleanup,
21424be21d56SDavid Gibson     .load_state = htab_load,
21434be21d56SDavid Gibson };
21444be21d56SDavid Gibson 
21455b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
21465b2128d2SAlexander Graf                            Error **errp)
21475b2128d2SAlexander Graf {
2148c86c1affSDaniel Henrique Barboza     MachineState *machine = MACHINE(opaque);
21495b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
21505b2128d2SAlexander Graf }
21515b2128d2SAlexander Graf 
2152224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
2153224245bfSDavid Gibson {
2154224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
2155224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
2156e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
2157224245bfSDavid Gibson     int i;
2158224245bfSDavid Gibson 
2159224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
2160224245bfSDavid Gibson         uint64_t addr;
2161224245bfSDavid Gibson 
2162e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
21636caf3ac6SDavid Gibson         spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_LMB,
2164224245bfSDavid Gibson                                addr / lmb_size);
2165224245bfSDavid Gibson     }
2166224245bfSDavid Gibson }
2167224245bfSDavid Gibson 
2168224245bfSDavid Gibson /*
2169224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
2170224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
2171224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
2172224245bfSDavid Gibson  */
21737c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
2174224245bfSDavid Gibson {
2175224245bfSDavid Gibson     int i;
2176224245bfSDavid Gibson 
21777c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
21787c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
21797c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
21807c150d6fSDavid Gibson                    machine->ram_size,
2181224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
21827c150d6fSDavid Gibson         return;
21837c150d6fSDavid Gibson     }
21847c150d6fSDavid Gibson 
21857c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
21867c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
21877c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
21887c150d6fSDavid Gibson                    machine->ram_size,
21897c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
21907c150d6fSDavid Gibson         return;
2191224245bfSDavid Gibson     }
2192224245bfSDavid Gibson 
2193224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
2194224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
21957c150d6fSDavid Gibson             error_setg(errp,
21967c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
21977c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
21987c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
2199224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
22007c150d6fSDavid Gibson             return;
2201224245bfSDavid Gibson         }
2202224245bfSDavid Gibson     }
2203224245bfSDavid Gibson }
2204224245bfSDavid Gibson 
2205535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
2206535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
2207535455fdSIgor Mammedov {
2208535455fdSIgor Mammedov     int index = id / smp_threads;
2209535455fdSIgor Mammedov 
2210535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
2211535455fdSIgor Mammedov         return NULL;
2212535455fdSIgor Mammedov     }
2213535455fdSIgor Mammedov     if (idx) {
2214535455fdSIgor Mammedov         *idx = index;
2215535455fdSIgor Mammedov     }
2216535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
2217535455fdSIgor Mammedov }
2218535455fdSIgor Mammedov 
22190c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
22200c86d0fdSDavid Gibson {
22210c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
22220c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
22232e9c10ebSIgor Mammedov     const char *type = spapr_get_cpu_core_type(machine->cpu_type);
22240c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
2225535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
2226535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
22270c86d0fdSDavid Gibson     int i;
22280c86d0fdSDavid Gibson 
22290c86d0fdSDavid Gibson     if (!type) {
22300c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
22310c86d0fdSDavid Gibson         exit(1);
22320c86d0fdSDavid Gibson     }
22330c86d0fdSDavid Gibson 
2234535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
2235c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
22360c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
22370c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
22380c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
22390c86d0fdSDavid Gibson             exit(1);
22400c86d0fdSDavid Gibson         }
22410c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
22420c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
22430c86d0fdSDavid Gibson                          max_cpus, smp_threads);
22440c86d0fdSDavid Gibson             exit(1);
22450c86d0fdSDavid Gibson         }
22460c86d0fdSDavid Gibson     } else {
22470c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
22480c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
22490c86d0fdSDavid Gibson             exit(1);
22500c86d0fdSDavid Gibson         }
2251535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
22520c86d0fdSDavid Gibson     }
22530c86d0fdSDavid Gibson 
2254535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
22550c86d0fdSDavid Gibson         int core_id = i * smp_threads;
22560c86d0fdSDavid Gibson 
2257c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
22582d335818SDavid Gibson             spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU,
22590c86d0fdSDavid Gibson                                    (core_id / smp_threads) * smt);
22600c86d0fdSDavid Gibson         }
22610c86d0fdSDavid Gibson 
2262535455fdSIgor Mammedov         if (i < boot_cores_nr) {
22630c86d0fdSDavid Gibson             Object *core  = object_new(type);
22640c86d0fdSDavid Gibson             int nr_threads = smp_threads;
22650c86d0fdSDavid Gibson 
22660c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
22670c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
22680c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
22690c86d0fdSDavid Gibson             }
22700c86d0fdSDavid Gibson 
22710c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
22720c86d0fdSDavid Gibson                                     &error_fatal);
22730c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
22740c86d0fdSDavid Gibson                                     &error_fatal);
22750c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
22760c86d0fdSDavid Gibson         }
22770c86d0fdSDavid Gibson     }
22780c86d0fdSDavid Gibson }
22790c86d0fdSDavid Gibson 
2280fa98fbfcSSam Bobroff static void spapr_set_vsmt_mode(sPAPRMachineState *spapr, Error **errp)
2281fa98fbfcSSam Bobroff {
2282fa98fbfcSSam Bobroff     Error *local_err = NULL;
2283fa98fbfcSSam Bobroff     bool vsmt_user = !!spapr->vsmt;
2284fa98fbfcSSam Bobroff     int kvm_smt = kvmppc_smt_threads();
2285fa98fbfcSSam Bobroff     int ret;
2286fa98fbfcSSam Bobroff 
2287fa98fbfcSSam Bobroff     if (!kvm_enabled() && (smp_threads > 1)) {
2288fa98fbfcSSam Bobroff         error_setg(&local_err, "TCG cannot support more than 1 thread/core "
2289fa98fbfcSSam Bobroff                      "on a pseries machine");
2290fa98fbfcSSam Bobroff         goto out;
2291fa98fbfcSSam Bobroff     }
2292fa98fbfcSSam Bobroff     if (!is_power_of_2(smp_threads)) {
2293fa98fbfcSSam Bobroff         error_setg(&local_err, "Cannot support %d threads/core on a pseries "
2294fa98fbfcSSam Bobroff                      "machine because it must be a power of 2", smp_threads);
2295fa98fbfcSSam Bobroff         goto out;
2296fa98fbfcSSam Bobroff     }
2297fa98fbfcSSam Bobroff 
2298fa98fbfcSSam Bobroff     /* Detemine the VSMT mode to use: */
2299fa98fbfcSSam Bobroff     if (vsmt_user) {
2300fa98fbfcSSam Bobroff         if (spapr->vsmt < smp_threads) {
2301fa98fbfcSSam Bobroff             error_setg(&local_err, "Cannot support VSMT mode %d"
2302fa98fbfcSSam Bobroff                          " because it must be >= threads/core (%d)",
2303fa98fbfcSSam Bobroff                          spapr->vsmt, smp_threads);
2304fa98fbfcSSam Bobroff             goto out;
2305fa98fbfcSSam Bobroff         }
2306fa98fbfcSSam Bobroff         /* In this case, spapr->vsmt has been set by the command line */
2307fa98fbfcSSam Bobroff     } else {
2308fa98fbfcSSam Bobroff         /* Choose a VSMT mode that may be higher than necessary but is
2309fa98fbfcSSam Bobroff          * likely to be compatible with hosts that don't have VSMT. */
2310fa98fbfcSSam Bobroff         spapr->vsmt = MAX(kvm_smt, smp_threads);
2311fa98fbfcSSam Bobroff     }
2312fa98fbfcSSam Bobroff 
2313fa98fbfcSSam Bobroff     /* KVM: If necessary, set the SMT mode: */
2314fa98fbfcSSam Bobroff     if (kvm_enabled() && (spapr->vsmt != kvm_smt)) {
2315fa98fbfcSSam Bobroff         ret = kvmppc_set_smt_threads(spapr->vsmt);
2316fa98fbfcSSam Bobroff         if (ret) {
2317fa98fbfcSSam Bobroff             error_setg(&local_err,
2318fa98fbfcSSam Bobroff                        "Failed to set KVM's VSMT mode to %d (errno %d)",
2319fa98fbfcSSam Bobroff                        spapr->vsmt, ret);
2320fa98fbfcSSam Bobroff             if (!vsmt_user) {
2321fa98fbfcSSam Bobroff                 error_append_hint(&local_err, "On PPC, a VM with %d threads/"
2322fa98fbfcSSam Bobroff                              "core on a host with %d threads/core requires "
2323fa98fbfcSSam Bobroff                              " the use of VSMT mode %d.\n",
2324fa98fbfcSSam Bobroff                              smp_threads, kvm_smt, spapr->vsmt);
2325fa98fbfcSSam Bobroff             }
2326fa98fbfcSSam Bobroff             kvmppc_hint_smt_possible(&local_err);
2327fa98fbfcSSam Bobroff             goto out;
2328fa98fbfcSSam Bobroff         }
2329fa98fbfcSSam Bobroff     }
2330fa98fbfcSSam Bobroff     /* else TCG: nothing to do currently */
2331fa98fbfcSSam Bobroff out:
2332fa98fbfcSSam Bobroff     error_propagate(errp, local_err);
2333fa98fbfcSSam Bobroff }
2334fa98fbfcSSam Bobroff 
233553018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
2336bcb5ce08SDavid Gibson static void spapr_machine_init(MachineState *machine)
233753018216SPaolo Bonzini {
233828e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
2339224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
23403ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
23413ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
234253018216SPaolo Bonzini     PCIHostState *phb;
234353018216SPaolo Bonzini     int i;
234453018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
234553018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
2346658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
2347658fa66bSAlexey Kardashevskiy     void *rma = NULL;
234853018216SPaolo Bonzini     hwaddr rma_alloc_size;
2349c86c1affSDaniel Henrique Barboza     hwaddr node0_size = spapr_node0_size(machine);
2350b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
235153018216SPaolo Bonzini     char *filename;
235230f4b05bSDavid Gibson     Error *resize_hpt_err = NULL;
235353018216SPaolo Bonzini 
2354226419d6SMichael S. Tsirkin     msi_nonbroken = true;
235553018216SPaolo Bonzini 
235653018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
23570cffce56SDavid Gibson     QTAILQ_INIT(&spapr->pending_dimm_unplugs);
235853018216SPaolo Bonzini 
235930f4b05bSDavid Gibson     /* Check HPT resizing availability */
236030f4b05bSDavid Gibson     kvmppc_check_papr_resize_hpt(&resize_hpt_err);
236130f4b05bSDavid Gibson     if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DEFAULT) {
236230f4b05bSDavid Gibson         /*
236330f4b05bSDavid Gibson          * If the user explicitly requested a mode we should either
236430f4b05bSDavid Gibson          * supply it, or fail completely (which we do below).  But if
236530f4b05bSDavid Gibson          * it's not set explicitly, we reset our mode to something
236630f4b05bSDavid Gibson          * that works
236730f4b05bSDavid Gibson          */
236830f4b05bSDavid Gibson         if (resize_hpt_err) {
236930f4b05bSDavid Gibson             spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
237030f4b05bSDavid Gibson             error_free(resize_hpt_err);
237130f4b05bSDavid Gibson             resize_hpt_err = NULL;
237230f4b05bSDavid Gibson         } else {
237330f4b05bSDavid Gibson             spapr->resize_hpt = smc->resize_hpt_default;
237430f4b05bSDavid Gibson         }
237530f4b05bSDavid Gibson     }
237630f4b05bSDavid Gibson 
237730f4b05bSDavid Gibson     assert(spapr->resize_hpt != SPAPR_RESIZE_HPT_DEFAULT);
237830f4b05bSDavid Gibson 
237930f4b05bSDavid Gibson     if ((spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) && resize_hpt_err) {
238030f4b05bSDavid Gibson         /*
238130f4b05bSDavid Gibson          * User requested HPT resize, but this host can't supply it.  Bail out
238230f4b05bSDavid Gibson          */
238330f4b05bSDavid Gibson         error_report_err(resize_hpt_err);
238430f4b05bSDavid Gibson         exit(1);
238530f4b05bSDavid Gibson     }
238630f4b05bSDavid Gibson 
238753018216SPaolo Bonzini     /* Allocate RMA if necessary */
2388658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
238953018216SPaolo Bonzini 
239053018216SPaolo Bonzini     if (rma_alloc_size == -1) {
2391730fce59SThomas Huth         error_report("Unable to create RMA");
239253018216SPaolo Bonzini         exit(1);
239353018216SPaolo Bonzini     }
239453018216SPaolo Bonzini 
2395c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
239653018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
239753018216SPaolo Bonzini     } else {
2398c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
239953018216SPaolo Bonzini 
240053018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
240153018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
240253018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
240353018216SPaolo Bonzini          *
240453018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
240553018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
240653018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
240753018216SPaolo Bonzini          * isn't determined yet.
240853018216SPaolo Bonzini          */
240953018216SPaolo Bonzini         if (kvm_enabled()) {
241053018216SPaolo Bonzini             spapr->vrma_adjust = 1;
241153018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
241253018216SPaolo Bonzini         }
2413912acdf4SBenjamin Herrenschmidt 
2414912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
2415912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
2416912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
2417912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
2418912acdf4SBenjamin Herrenschmidt          */
2419912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
242053018216SPaolo Bonzini     }
242153018216SPaolo Bonzini 
2422c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
2423d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2424c4177479SAlexey Kardashevskiy                      spapr->rma_size);
2425c4177479SAlexey Kardashevskiy         exit(1);
2426c4177479SAlexey Kardashevskiy     }
2427c4177479SAlexey Kardashevskiy 
2428b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2429b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
243053018216SPaolo Bonzini 
24317b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
243271cd4dacSCédric Le Goater     xics_system_init(machine, XICS_IRQS_SPAPR, &error_fatal);
24337b565160SDavid Gibson 
2434dc1b5eeeSGreg Kurz     /* Set up containers for ibm,client-architecture-support negotiated options
2435dc1b5eeeSGreg Kurz      */
2436facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2437facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2438facdb8b6SMichael Roth 
2439224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2440facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
24417c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2442224245bfSDavid Gibson     }
2443224245bfSDavid Gibson 
2444417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2445545d6e2bSSuraj Jitindar Singh     if (!kvm_enabled() || kvmppc_has_cap_mmu_radix()) {
2446545d6e2bSSuraj Jitindar Singh         /* KVM and TCG always allow GTSE with radix... */
24479fb4541fSSam Bobroff         spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
24489fb4541fSSam Bobroff     }
24499fb4541fSSam Bobroff     /* ... but not with hash (currently). */
2450417ece33SMichael Roth 
2451ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2452ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2453ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2454ffbb1705SMichael Roth     }
2455ffbb1705SMichael Roth 
24562772cf6bSDavid Gibson     /* advertise support for HPT resizing */
24572772cf6bSDavid Gibson     if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
24582772cf6bSDavid Gibson         spapr_ovec_set(spapr->ov5, OV5_HPT_RESIZE);
24592772cf6bSDavid Gibson     }
24602772cf6bSDavid Gibson 
246153018216SPaolo Bonzini     /* init CPUs */
2462fa98fbfcSSam Bobroff     spapr_set_vsmt_mode(spapr, &error_fatal);
2463fa98fbfcSSam Bobroff 
24640c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
246553018216SPaolo Bonzini 
2466026bfd89SDavid Gibson     if (kvm_enabled()) {
2467026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2468026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2469ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
24705145ad4fSNathan Whitehorn 
24715145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
24725145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2473026bfd89SDavid Gibson     }
2474026bfd89SDavid Gibson 
247553018216SPaolo Bonzini     /* allocate RAM */
2476f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2477fb164994SDavid Gibson                                          machine->ram_size);
2478f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
247953018216SPaolo Bonzini 
2480658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2481658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2482658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2483658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2484658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2485658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2486658fa66bSAlexey Kardashevskiy     }
2487658fa66bSAlexey Kardashevskiy 
24884a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
24894a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
24904a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
249171c9a3ddSBharata B Rao         /*
249271c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
249371c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
249471c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
249571c9a3ddSBharata B Rao          */
249671c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
249771c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
24984a1c9cf0SBharata B Rao 
249971c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
250071c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
250171c9a3ddSBharata B Rao         }
250271c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2503d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2504d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
250571c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2506d54e4d76SDavid Gibson             exit(1);
25074a1c9cf0SBharata B Rao         }
25084a1c9cf0SBharata B Rao 
25094a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
25104a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
25114a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
25124a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
25134a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
25144a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
25154a1c9cf0SBharata B Rao     }
25164a1c9cf0SBharata B Rao 
2517224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2518224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2519224245bfSDavid Gibson     }
2520224245bfSDavid Gibson 
252153018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
25224c56440dSStefan Weil     if (!filename) {
2523730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
25244c56440dSStefan Weil         exit(1);
25254c56440dSStefan Weil     }
2526b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
25278afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
25288afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
25298afc22a2SZhou Jie         exit(1);
25308afc22a2SZhou Jie     }
2531b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2532b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2533730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
253453018216SPaolo Bonzini         exit(1);
253553018216SPaolo Bonzini     }
253653018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2537730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
25382f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
253953018216SPaolo Bonzini         exit(1);
254053018216SPaolo Bonzini     }
254153018216SPaolo Bonzini     g_free(filename);
254253018216SPaolo Bonzini 
2543ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
254453018216SPaolo Bonzini     spapr_events_init(spapr);
254553018216SPaolo Bonzini 
254612f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
254728df36a1SDavid Gibson     spapr_rtc_create(spapr);
254812f42174SDavid Gibson 
254953018216SPaolo Bonzini     /* Set up VIO bus */
255053018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
255153018216SPaolo Bonzini 
255253018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
255353018216SPaolo Bonzini         if (serial_hds[i]) {
255453018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
255553018216SPaolo Bonzini         }
255653018216SPaolo Bonzini     }
255753018216SPaolo Bonzini 
255853018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
255953018216SPaolo Bonzini     spapr_create_nvram(spapr);
256053018216SPaolo Bonzini 
256153018216SPaolo Bonzini     /* Set up PCI */
256253018216SPaolo Bonzini     spapr_pci_rtas_init();
256353018216SPaolo Bonzini 
256489dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
256553018216SPaolo Bonzini 
256653018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
256753018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
256853018216SPaolo Bonzini 
256953018216SPaolo Bonzini         if (!nd->model) {
257053018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
257153018216SPaolo Bonzini         }
257253018216SPaolo Bonzini 
257353018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
257453018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
257553018216SPaolo Bonzini         } else {
257629b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
257753018216SPaolo Bonzini         }
257853018216SPaolo Bonzini     }
257953018216SPaolo Bonzini 
258053018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
258153018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
258253018216SPaolo Bonzini     }
258353018216SPaolo Bonzini 
258453018216SPaolo Bonzini     /* Graphics */
258514c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
258653018216SPaolo Bonzini         spapr->has_graphics = true;
2587c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
258853018216SPaolo Bonzini     }
258953018216SPaolo Bonzini 
25904ee9ced9SMarcel Apfelbaum     if (machine->usb) {
259157040d45SThomas Huth         if (smc->use_ohci_by_default) {
259253018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
259357040d45SThomas Huth         } else {
259457040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
259557040d45SThomas Huth         }
2596c86580b8SMarkus Armbruster 
259753018216SPaolo Bonzini         if (spapr->has_graphics) {
2598c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2599c86580b8SMarkus Armbruster 
2600c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2601c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
260253018216SPaolo Bonzini         }
260353018216SPaolo Bonzini     }
260453018216SPaolo Bonzini 
260553018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2606d54e4d76SDavid Gibson         error_report(
2607d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2608d54e4d76SDavid Gibson             MIN_RMA_SLOF);
260953018216SPaolo Bonzini         exit(1);
261053018216SPaolo Bonzini     }
261153018216SPaolo Bonzini 
261253018216SPaolo Bonzini     if (kernel_filename) {
261353018216SPaolo Bonzini         uint64_t lowaddr = 0;
261453018216SPaolo Bonzini 
2615a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2616a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2617a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2618a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2619a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2620a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2621a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
26227ef295eaSPeter Crosthwaite                                           0, 0);
2623a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
262416457e7fSBenjamin Herrenschmidt         }
2625a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2626a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2627a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
262853018216SPaolo Bonzini             exit(1);
262953018216SPaolo Bonzini         }
263053018216SPaolo Bonzini 
263153018216SPaolo Bonzini         /* load initrd */
263253018216SPaolo Bonzini         if (initrd_filename) {
263353018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
263453018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
263553018216SPaolo Bonzini              */
2636a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2637a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2638a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2639a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2640a19f7fb0SDavid Gibson                                                      load_limit
2641a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2642a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2643d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
264453018216SPaolo Bonzini                              initrd_filename);
264553018216SPaolo Bonzini                 exit(1);
264653018216SPaolo Bonzini             }
264753018216SPaolo Bonzini         }
264853018216SPaolo Bonzini     }
264953018216SPaolo Bonzini 
26508e7ea787SAndreas Färber     if (bios_name == NULL) {
26518e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
26528e7ea787SAndreas Färber     }
26538e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
26544c56440dSStefan Weil     if (!filename) {
265568fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
26564c56440dSStefan Weil         exit(1);
26574c56440dSStefan Weil     }
265853018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
265968fea5a0SThomas Huth     if (fw_size <= 0) {
266068fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
266153018216SPaolo Bonzini         exit(1);
266253018216SPaolo Bonzini     }
266353018216SPaolo Bonzini     g_free(filename);
266453018216SPaolo Bonzini 
266528e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
266628e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
266728e02042SDavid Gibson      * which predated MachineState but had a similar function */
26684be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
26694be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
26704be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
26714be21d56SDavid Gibson 
26725b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
267342043e4fSLaurent Vivier 
267442043e4fSLaurent Vivier     if (kvm_enabled()) {
26753dc410aeSAlexey Kardashevskiy         /* to stop and start vmclock */
267642043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
267742043e4fSLaurent Vivier                                          &spapr->tb);
26783dc410aeSAlexey Kardashevskiy 
26793dc410aeSAlexey Kardashevskiy         kvmppc_spapr_enable_inkernel_multitce();
268042043e4fSLaurent Vivier     }
268153018216SPaolo Bonzini }
268253018216SPaolo Bonzini 
2683135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2684135a129aSAneesh Kumar K.V {
2685135a129aSAneesh Kumar K.V     if (!vm_type) {
2686135a129aSAneesh Kumar K.V         return 0;
2687135a129aSAneesh Kumar K.V     }
2688135a129aSAneesh Kumar K.V 
2689135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2690135a129aSAneesh Kumar K.V         return 1;
2691135a129aSAneesh Kumar K.V     }
2692135a129aSAneesh Kumar K.V 
2693135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2694135a129aSAneesh Kumar K.V         return 2;
2695135a129aSAneesh Kumar K.V     }
2696135a129aSAneesh Kumar K.V 
2697135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2698135a129aSAneesh Kumar K.V     exit(1);
2699135a129aSAneesh Kumar K.V }
2700135a129aSAneesh Kumar K.V 
270171461b0fSAlexey Kardashevskiy /*
2702627b84f4SGonglei  * Implementation of an interface to adjust firmware path
270371461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
270471461b0fSAlexey Kardashevskiy  */
270571461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
270671461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
270771461b0fSAlexey Kardashevskiy {
270871461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
270971461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
271071461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
271171461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
2712c4e13492SFelipe Franciosi     VHostSCSICommon *vsc = CAST(VHostSCSICommon, dev, TYPE_VHOST_SCSI_COMMON);
271371461b0fSAlexey Kardashevskiy 
271471461b0fSAlexey Kardashevskiy     if (d) {
271571461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
271671461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
271771461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
271871461b0fSAlexey Kardashevskiy 
271971461b0fSAlexey Kardashevskiy         if (spapr) {
272071461b0fSAlexey Kardashevskiy             /*
272171461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
272271461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
272371461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
272471461b0fSAlexey Kardashevskiy              */
272571461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
272671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
272771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
272871461b0fSAlexey Kardashevskiy         } else if (virtio) {
272971461b0fSAlexey Kardashevskiy             /*
273071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
273171461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
273271461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
273371461b0fSAlexey Kardashevskiy              * the actual binding is:
273471461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
273571461b0fSAlexey Kardashevskiy              */
273671461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
2737bac658d1SThomas Huth             if (d->lun >= 256) {
2738bac658d1SThomas Huth                 /* Use the LUN "flat space addressing method" */
2739bac658d1SThomas Huth                 id |= 0x4000;
2740bac658d1SThomas Huth             }
274171461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
274271461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
274371461b0fSAlexey Kardashevskiy         } else if (usb) {
274471461b0fSAlexey Kardashevskiy             /*
274571461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
274671461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
274771461b0fSAlexey Kardashevskiy              */
274871461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
274971461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
275071461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
275171461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
275271461b0fSAlexey Kardashevskiy         }
275371461b0fSAlexey Kardashevskiy     }
275471461b0fSAlexey Kardashevskiy 
2755b99260ebSThomas Huth     /*
2756b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2757b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2758b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2759b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2760b99260ebSThomas Huth      */
2761b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2762b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2763b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2764b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2765b99260ebSThomas Huth         }
2766b99260ebSThomas Huth     }
2767b99260ebSThomas Huth 
276871461b0fSAlexey Kardashevskiy     if (phb) {
276971461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
277071461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
277171461b0fSAlexey Kardashevskiy     }
277271461b0fSAlexey Kardashevskiy 
2773c4e13492SFelipe Franciosi     if (vsc) {
2774c4e13492SFelipe Franciosi         /* Same logic as virtio above */
2775c4e13492SFelipe Franciosi         unsigned id = 0x1000000 | (vsc->target << 16) | vsc->lun;
2776c4e13492SFelipe Franciosi         return g_strdup_printf("disk@%"PRIX64, (uint64_t)id << 32);
2777c4e13492SFelipe Franciosi     }
2778c4e13492SFelipe Franciosi 
27794871dd4cSThomas Huth     if (g_str_equal("pci-bridge", qdev_fw_name(dev))) {
27804871dd4cSThomas Huth         /* SLOF uses "pci" instead of "pci-bridge" for PCI bridges */
27814871dd4cSThomas Huth         PCIDevice *pcidev = CAST(PCIDevice, dev, TYPE_PCI_DEVICE);
27824871dd4cSThomas Huth         return g_strdup_printf("pci@%x", PCI_SLOT(pcidev->devfn));
27834871dd4cSThomas Huth     }
27844871dd4cSThomas Huth 
278571461b0fSAlexey Kardashevskiy     return NULL;
278671461b0fSAlexey Kardashevskiy }
278771461b0fSAlexey Kardashevskiy 
278823825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
278923825581SEduardo Habkost {
279028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
279123825581SEduardo Habkost 
279228e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
279323825581SEduardo Habkost }
279423825581SEduardo Habkost 
279523825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
279623825581SEduardo Habkost {
279728e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
279823825581SEduardo Habkost 
279928e02042SDavid Gibson     g_free(spapr->kvm_type);
280028e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
280123825581SEduardo Habkost }
280223825581SEduardo Habkost 
2803f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2804f6229214SMichael Roth {
2805f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2806f6229214SMichael Roth 
2807f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2808f6229214SMichael Roth }
2809f6229214SMichael Roth 
2810f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2811f6229214SMichael Roth                                             Error **errp)
2812f6229214SMichael Roth {
2813f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2814f6229214SMichael Roth 
2815f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2816f6229214SMichael Roth }
2817f6229214SMichael Roth 
281830f4b05bSDavid Gibson static char *spapr_get_resize_hpt(Object *obj, Error **errp)
281930f4b05bSDavid Gibson {
282030f4b05bSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
282130f4b05bSDavid Gibson 
282230f4b05bSDavid Gibson     switch (spapr->resize_hpt) {
282330f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_DEFAULT:
282430f4b05bSDavid Gibson         return g_strdup("default");
282530f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_DISABLED:
282630f4b05bSDavid Gibson         return g_strdup("disabled");
282730f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_ENABLED:
282830f4b05bSDavid Gibson         return g_strdup("enabled");
282930f4b05bSDavid Gibson     case SPAPR_RESIZE_HPT_REQUIRED:
283030f4b05bSDavid Gibson         return g_strdup("required");
283130f4b05bSDavid Gibson     }
283230f4b05bSDavid Gibson     g_assert_not_reached();
283330f4b05bSDavid Gibson }
283430f4b05bSDavid Gibson 
283530f4b05bSDavid Gibson static void spapr_set_resize_hpt(Object *obj, const char *value, Error **errp)
283630f4b05bSDavid Gibson {
283730f4b05bSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
283830f4b05bSDavid Gibson 
283930f4b05bSDavid Gibson     if (strcmp(value, "default") == 0) {
284030f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_DEFAULT;
284130f4b05bSDavid Gibson     } else if (strcmp(value, "disabled") == 0) {
284230f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
284330f4b05bSDavid Gibson     } else if (strcmp(value, "enabled") == 0) {
284430f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_ENABLED;
284530f4b05bSDavid Gibson     } else if (strcmp(value, "required") == 0) {
284630f4b05bSDavid Gibson         spapr->resize_hpt = SPAPR_RESIZE_HPT_REQUIRED;
284730f4b05bSDavid Gibson     } else {
284830f4b05bSDavid Gibson         error_setg(errp, "Bad value for \"resize-hpt\" property");
284930f4b05bSDavid Gibson     }
285030f4b05bSDavid Gibson }
285130f4b05bSDavid Gibson 
2852fa98fbfcSSam Bobroff static void spapr_get_vsmt(Object *obj, Visitor *v, const char *name,
2853fa98fbfcSSam Bobroff                                    void *opaque, Error **errp)
2854fa98fbfcSSam Bobroff {
2855fa98fbfcSSam Bobroff     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
2856fa98fbfcSSam Bobroff }
2857fa98fbfcSSam Bobroff 
2858fa98fbfcSSam Bobroff static void spapr_set_vsmt(Object *obj, Visitor *v, const char *name,
2859fa98fbfcSSam Bobroff                                    void *opaque, Error **errp)
2860fa98fbfcSSam Bobroff {
2861fa98fbfcSSam Bobroff     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
2862fa98fbfcSSam Bobroff }
2863fa98fbfcSSam Bobroff 
2864bcb5ce08SDavid Gibson static void spapr_instance_init(Object *obj)
286523825581SEduardo Habkost {
2866715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2867715c5407SDavid Gibson 
2868715c5407SDavid Gibson     spapr->htab_fd = -1;
2869f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
287023825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
287123825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
287249d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
287349d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
287449d2e648SMarcel Apfelbaum                                     NULL);
2875f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2876f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2877f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2878f6229214SMichael Roth                             NULL);
2879f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2880f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2881f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2882f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2883f6229214SMichael Roth                                     NULL);
28847843c0d6SDavid Gibson 
28857843c0d6SDavid Gibson     ppc_compat_add_property(obj, "max-cpu-compat", &spapr->max_compat_pvr,
28867843c0d6SDavid Gibson                             "Maximum permitted CPU compatibility mode",
28877843c0d6SDavid Gibson                             &error_fatal);
288830f4b05bSDavid Gibson 
288930f4b05bSDavid Gibson     object_property_add_str(obj, "resize-hpt",
289030f4b05bSDavid Gibson                             spapr_get_resize_hpt, spapr_set_resize_hpt, NULL);
289130f4b05bSDavid Gibson     object_property_set_description(obj, "resize-hpt",
289230f4b05bSDavid Gibson                                     "Resizing of the Hash Page Table (enabled, disabled, required)",
289330f4b05bSDavid Gibson                                     NULL);
2894fa98fbfcSSam Bobroff     object_property_add(obj, "vsmt", "uint32", spapr_get_vsmt,
2895fa98fbfcSSam Bobroff                         spapr_set_vsmt, NULL, &spapr->vsmt, &error_abort);
2896fa98fbfcSSam Bobroff     object_property_set_description(obj, "vsmt",
2897fa98fbfcSSam Bobroff                                     "Virtual SMT: KVM behaves as if this were"
2898fa98fbfcSSam Bobroff                                     " the host's SMT mode", &error_abort);
289923825581SEduardo Habkost }
290023825581SEduardo Habkost 
290187bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
290287bbdd9cSDavid Gibson {
290387bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
290487bbdd9cSDavid Gibson 
290587bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
290687bbdd9cSDavid Gibson }
290787bbdd9cSDavid Gibson 
29081c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
290934316482SAlexey Kardashevskiy {
291034316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
291134316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
291234316482SAlexey Kardashevskiy }
291334316482SAlexey Kardashevskiy 
291434316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
291534316482SAlexey Kardashevskiy {
291634316482SAlexey Kardashevskiy     CPUState *cs;
291734316482SAlexey Kardashevskiy 
291834316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
29191c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
292034316482SAlexey Kardashevskiy     }
292134316482SAlexey Kardashevskiy }
292234316482SAlexey Kardashevskiy 
292379b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
292479b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
292579b78a6bSMichael Roth                            Error **errp)
2926c20d332aSBharata B Rao {
2927c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2928c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2929c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2930c20d332aSBharata B Rao     void *fdt;
293179b78a6bSMichael Roth     uint64_t addr = addr_start;
293294fd9cbaSLaurent Vivier     bool hotplugged = spapr_drc_hotplugged(dev);
2933160bb678SGreg Kurz     Error *local_err = NULL;
2934c20d332aSBharata B Rao 
2935c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2936fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
2937c20d332aSBharata B Rao                               addr / SPAPR_MEMORY_BLOCK_SIZE);
2938c20d332aSBharata B Rao         g_assert(drc);
2939c20d332aSBharata B Rao 
2940c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2941c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2942c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2943c20d332aSBharata B Rao 
2944160bb678SGreg Kurz         spapr_drc_attach(drc, dev, fdt, fdt_offset, &local_err);
2945160bb678SGreg Kurz         if (local_err) {
2946160bb678SGreg Kurz             while (addr > addr_start) {
2947160bb678SGreg Kurz                 addr -= SPAPR_MEMORY_BLOCK_SIZE;
2948160bb678SGreg Kurz                 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
2949160bb678SGreg Kurz                                       addr / SPAPR_MEMORY_BLOCK_SIZE);
2950a8dc47fdSDavid Gibson                 spapr_drc_detach(drc);
2951160bb678SGreg Kurz             }
2952160bb678SGreg Kurz             g_free(fdt);
2953160bb678SGreg Kurz             error_propagate(errp, local_err);
2954160bb678SGreg Kurz             return;
2955160bb678SGreg Kurz         }
295694fd9cbaSLaurent Vivier         if (!hotplugged) {
295794fd9cbaSLaurent Vivier             spapr_drc_reset(drc);
295894fd9cbaSLaurent Vivier         }
2959c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2960c20d332aSBharata B Rao     }
29615dd5238cSJianjun Duan     /* send hotplug notification to the
29625dd5238cSJianjun Duan      * guest only in case of hotplugged memory
29635dd5238cSJianjun Duan      */
296494fd9cbaSLaurent Vivier     if (hotplugged) {
296579b78a6bSMichael Roth         if (dedicated_hp_event_source) {
2966fbf55397SDavid Gibson             drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
296779b78a6bSMichael Roth                                   addr_start / SPAPR_MEMORY_BLOCK_SIZE);
296879b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
296979b78a6bSMichael Roth                                                    nr_lmbs,
29700b55aa91SDavid Gibson                                                    spapr_drc_index(drc));
297179b78a6bSMichael Roth         } else {
297279b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
297379b78a6bSMichael Roth                                            nr_lmbs);
297479b78a6bSMichael Roth         }
2975c20d332aSBharata B Rao     }
29765dd5238cSJianjun Duan }
2977c20d332aSBharata B Rao 
2978c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2979c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2980c20d332aSBharata B Rao {
2981c20d332aSBharata B Rao     Error *local_err = NULL;
2982c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2983c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2984c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
298504790978SThomas Huth     MemoryRegion *mr;
298604790978SThomas Huth     uint64_t align, size, addr;
298704790978SThomas Huth 
298804790978SThomas Huth     mr = ddc->get_memory_region(dimm, &local_err);
298904790978SThomas Huth     if (local_err) {
299004790978SThomas Huth         goto out;
299104790978SThomas Huth     }
299204790978SThomas Huth     align = memory_region_get_alignment(mr);
299304790978SThomas Huth     size = memory_region_size(mr);
2994df587133SThomas Huth 
2995d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2996c20d332aSBharata B Rao     if (local_err) {
2997c20d332aSBharata B Rao         goto out;
2998c20d332aSBharata B Rao     }
2999c20d332aSBharata B Rao 
30009ed442b8SMarc-André Lureau     addr = object_property_get_uint(OBJECT(dimm),
30019ed442b8SMarc-André Lureau                                     PC_DIMM_ADDR_PROP, &local_err);
3002c20d332aSBharata B Rao     if (local_err) {
3003160bb678SGreg Kurz         goto out_unplug;
3004c20d332aSBharata B Rao     }
3005c20d332aSBharata B Rao 
300679b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
300779b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
3008160bb678SGreg Kurz                    &local_err);
3009160bb678SGreg Kurz     if (local_err) {
3010160bb678SGreg Kurz         goto out_unplug;
3011160bb678SGreg Kurz     }
3012c20d332aSBharata B Rao 
3013160bb678SGreg Kurz     return;
3014160bb678SGreg Kurz 
3015160bb678SGreg Kurz out_unplug:
3016160bb678SGreg Kurz     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
3017c20d332aSBharata B Rao out:
3018c20d332aSBharata B Rao     error_propagate(errp, local_err);
3019c20d332aSBharata B Rao }
3020c20d332aSBharata B Rao 
3021c871bc70SLaurent Vivier static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3022c871bc70SLaurent Vivier                                   Error **errp)
3023c871bc70SLaurent Vivier {
3024c871bc70SLaurent Vivier     PCDIMMDevice *dimm = PC_DIMM(dev);
3025c871bc70SLaurent Vivier     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
302604790978SThomas Huth     MemoryRegion *mr;
302704790978SThomas Huth     uint64_t size;
3028c871bc70SLaurent Vivier     char *mem_dev;
3029c871bc70SLaurent Vivier 
303004790978SThomas Huth     mr = ddc->get_memory_region(dimm, errp);
303104790978SThomas Huth     if (!mr) {
303204790978SThomas Huth         return;
303304790978SThomas Huth     }
303404790978SThomas Huth     size = memory_region_size(mr);
303504790978SThomas Huth 
3036c871bc70SLaurent Vivier     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
3037c871bc70SLaurent Vivier         error_setg(errp, "Hotplugged memory size must be a multiple of "
3038c871bc70SLaurent Vivier                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
3039c871bc70SLaurent Vivier         return;
3040c871bc70SLaurent Vivier     }
3041c871bc70SLaurent Vivier 
3042c871bc70SLaurent Vivier     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
3043c871bc70SLaurent Vivier     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
3044c871bc70SLaurent Vivier         error_setg(errp, "Memory backend has bad page size. "
3045c871bc70SLaurent Vivier                    "Use 'memory-backend-file' with correct mem-path.");
30468a9e0e7bSGreg Kurz         goto out;
3047c871bc70SLaurent Vivier     }
30488a9e0e7bSGreg Kurz 
30498a9e0e7bSGreg Kurz out:
30508a9e0e7bSGreg Kurz     g_free(mem_dev);
3051c871bc70SLaurent Vivier }
3052c871bc70SLaurent Vivier 
30530cffce56SDavid Gibson struct sPAPRDIMMState {
30540cffce56SDavid Gibson     PCDIMMDevice *dimm;
3055cf632463SBharata B Rao     uint32_t nr_lmbs;
30560cffce56SDavid Gibson     QTAILQ_ENTRY(sPAPRDIMMState) next;
30570cffce56SDavid Gibson };
30580cffce56SDavid Gibson 
30590cffce56SDavid Gibson static sPAPRDIMMState *spapr_pending_dimm_unplugs_find(sPAPRMachineState *s,
30600cffce56SDavid Gibson                                                        PCDIMMDevice *dimm)
30610cffce56SDavid Gibson {
30620cffce56SDavid Gibson     sPAPRDIMMState *dimm_state = NULL;
30630cffce56SDavid Gibson 
30640cffce56SDavid Gibson     QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
30650cffce56SDavid Gibson         if (dimm_state->dimm == dimm) {
30660cffce56SDavid Gibson             break;
30670cffce56SDavid Gibson         }
30680cffce56SDavid Gibson     }
30690cffce56SDavid Gibson     return dimm_state;
30700cffce56SDavid Gibson }
30710cffce56SDavid Gibson 
30728d5981c4SBharata B Rao static sPAPRDIMMState *spapr_pending_dimm_unplugs_add(sPAPRMachineState *spapr,
30738d5981c4SBharata B Rao                                                       uint32_t nr_lmbs,
30748d5981c4SBharata B Rao                                                       PCDIMMDevice *dimm)
30750cffce56SDavid Gibson {
30768d5981c4SBharata B Rao     sPAPRDIMMState *ds = NULL;
30778d5981c4SBharata B Rao 
30788d5981c4SBharata B Rao     /*
30798d5981c4SBharata B Rao      * If this request is for a DIMM whose removal had failed earlier
30808d5981c4SBharata B Rao      * (due to guest's refusal to remove the LMBs), we would have this
30818d5981c4SBharata B Rao      * dimm already in the pending_dimm_unplugs list. In that
30828d5981c4SBharata B Rao      * case don't add again.
30838d5981c4SBharata B Rao      */
30848d5981c4SBharata B Rao     ds = spapr_pending_dimm_unplugs_find(spapr, dimm);
30858d5981c4SBharata B Rao     if (!ds) {
30868d5981c4SBharata B Rao         ds = g_malloc0(sizeof(sPAPRDIMMState));
30878d5981c4SBharata B Rao         ds->nr_lmbs = nr_lmbs;
30888d5981c4SBharata B Rao         ds->dimm = dimm;
30898d5981c4SBharata B Rao         QTAILQ_INSERT_HEAD(&spapr->pending_dimm_unplugs, ds, next);
30908d5981c4SBharata B Rao     }
30918d5981c4SBharata B Rao     return ds;
30920cffce56SDavid Gibson }
30930cffce56SDavid Gibson 
30940cffce56SDavid Gibson static void spapr_pending_dimm_unplugs_remove(sPAPRMachineState *spapr,
30950cffce56SDavid Gibson                                               sPAPRDIMMState *dimm_state)
30960cffce56SDavid Gibson {
30970cffce56SDavid Gibson     QTAILQ_REMOVE(&spapr->pending_dimm_unplugs, dimm_state, next);
30980cffce56SDavid Gibson     g_free(dimm_state);
30990cffce56SDavid Gibson }
3100cf632463SBharata B Rao 
310116ee9980SDaniel Henrique Barboza static sPAPRDIMMState *spapr_recover_pending_dimm_state(sPAPRMachineState *ms,
310216ee9980SDaniel Henrique Barboza                                                         PCDIMMDevice *dimm)
310316ee9980SDaniel Henrique Barboza {
310416ee9980SDaniel Henrique Barboza     sPAPRDRConnector *drc;
310516ee9980SDaniel Henrique Barboza     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
310604790978SThomas Huth     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
310716ee9980SDaniel Henrique Barboza     uint64_t size = memory_region_size(mr);
310816ee9980SDaniel Henrique Barboza     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
310916ee9980SDaniel Henrique Barboza     uint32_t avail_lmbs = 0;
311016ee9980SDaniel Henrique Barboza     uint64_t addr_start, addr;
311116ee9980SDaniel Henrique Barboza     int i;
311216ee9980SDaniel Henrique Barboza 
311316ee9980SDaniel Henrique Barboza     addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
311416ee9980SDaniel Henrique Barboza                                          &error_abort);
311516ee9980SDaniel Henrique Barboza 
311616ee9980SDaniel Henrique Barboza     addr = addr_start;
311716ee9980SDaniel Henrique Barboza     for (i = 0; i < nr_lmbs; i++) {
3118fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
311916ee9980SDaniel Henrique Barboza                               addr / SPAPR_MEMORY_BLOCK_SIZE);
312016ee9980SDaniel Henrique Barboza         g_assert(drc);
3121454b580aSDavid Gibson         if (drc->dev) {
312216ee9980SDaniel Henrique Barboza             avail_lmbs++;
312316ee9980SDaniel Henrique Barboza         }
312416ee9980SDaniel Henrique Barboza         addr += SPAPR_MEMORY_BLOCK_SIZE;
312516ee9980SDaniel Henrique Barboza     }
312616ee9980SDaniel Henrique Barboza 
31278d5981c4SBharata B Rao     return spapr_pending_dimm_unplugs_add(ms, avail_lmbs, dimm);
312816ee9980SDaniel Henrique Barboza }
312916ee9980SDaniel Henrique Barboza 
313031834723SDaniel Henrique Barboza /* Callback to be called during DRC release. */
313131834723SDaniel Henrique Barboza void spapr_lmb_release(DeviceState *dev)
3132cf632463SBharata B Rao {
3133765d1bddSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_hotplug_handler(dev));
3134765d1bddSDavid Gibson     PCDIMMDevice *dimm = PC_DIMM(dev);
3135765d1bddSDavid Gibson     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
313604790978SThomas Huth     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
31370cffce56SDavid Gibson     sPAPRDIMMState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
3138cf632463SBharata B Rao 
313916ee9980SDaniel Henrique Barboza     /* This information will get lost if a migration occurs
314016ee9980SDaniel Henrique Barboza      * during the unplug process. In this case recover it. */
314116ee9980SDaniel Henrique Barboza     if (ds == NULL) {
314216ee9980SDaniel Henrique Barboza         ds = spapr_recover_pending_dimm_state(spapr, PC_DIMM(dev));
31438d5981c4SBharata B Rao         g_assert(ds);
3144454b580aSDavid Gibson         /* The DRC being examined by the caller at least must be counted */
3145454b580aSDavid Gibson         g_assert(ds->nr_lmbs);
314616ee9980SDaniel Henrique Barboza     }
3147454b580aSDavid Gibson 
3148454b580aSDavid Gibson     if (--ds->nr_lmbs) {
3149cf632463SBharata B Rao         return;
3150cf632463SBharata B Rao     }
3151cf632463SBharata B Rao 
3152cf632463SBharata B Rao     /*
3153cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
3154cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
3155cf632463SBharata B Rao      */
3156765d1bddSDavid Gibson     pc_dimm_memory_unplug(dev, &spapr->hotplug_memory, mr);
3157cf632463SBharata B Rao     object_unparent(OBJECT(dev));
31582a129767SDaniel Henrique Barboza     spapr_pending_dimm_unplugs_remove(spapr, ds);
3159cf632463SBharata B Rao }
3160cf632463SBharata B Rao 
3161cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
3162cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
3163cf632463SBharata B Rao {
31640cffce56SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
3165cf632463SBharata B Rao     Error *local_err = NULL;
3166cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
3167cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
316804790978SThomas Huth     MemoryRegion *mr;
316904790978SThomas Huth     uint32_t nr_lmbs;
317004790978SThomas Huth     uint64_t size, addr_start, addr;
31710cffce56SDavid Gibson     int i;
31720cffce56SDavid Gibson     sPAPRDRConnector *drc;
317304790978SThomas Huth 
317404790978SThomas Huth     mr = ddc->get_memory_region(dimm, &local_err);
317504790978SThomas Huth     if (local_err) {
317604790978SThomas Huth         goto out;
317704790978SThomas Huth     }
317804790978SThomas Huth     size = memory_region_size(mr);
317904790978SThomas Huth     nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
318004790978SThomas Huth 
31819ed442b8SMarc-André Lureau     addr_start = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
31820cffce56SDavid Gibson                                          &local_err);
3183cf632463SBharata B Rao     if (local_err) {
3184cf632463SBharata B Rao         goto out;
3185cf632463SBharata B Rao     }
3186cf632463SBharata B Rao 
31872a129767SDaniel Henrique Barboza     /*
31882a129767SDaniel Henrique Barboza      * An existing pending dimm state for this DIMM means that there is an
31892a129767SDaniel Henrique Barboza      * unplug operation in progress, waiting for the spapr_lmb_release
31902a129767SDaniel Henrique Barboza      * callback to complete the job (BQL can't cover that far). In this case,
31912a129767SDaniel Henrique Barboza      * bail out to avoid detaching DRCs that were already released.
31922a129767SDaniel Henrique Barboza      */
31932a129767SDaniel Henrique Barboza     if (spapr_pending_dimm_unplugs_find(spapr, dimm)) {
31942a129767SDaniel Henrique Barboza         error_setg(&local_err,
31952a129767SDaniel Henrique Barboza                    "Memory unplug already in progress for device %s",
31962a129767SDaniel Henrique Barboza                    dev->id);
31972a129767SDaniel Henrique Barboza         goto out;
31982a129767SDaniel Henrique Barboza     }
31992a129767SDaniel Henrique Barboza 
32008d5981c4SBharata B Rao     spapr_pending_dimm_unplugs_add(spapr, nr_lmbs, dimm);
32010cffce56SDavid Gibson 
32020cffce56SDavid Gibson     addr = addr_start;
32030cffce56SDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
3204fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
32050cffce56SDavid Gibson                               addr / SPAPR_MEMORY_BLOCK_SIZE);
32060cffce56SDavid Gibson         g_assert(drc);
32070cffce56SDavid Gibson 
3208a8dc47fdSDavid Gibson         spapr_drc_detach(drc);
32090cffce56SDavid Gibson         addr += SPAPR_MEMORY_BLOCK_SIZE;
32100cffce56SDavid Gibson     }
32110cffce56SDavid Gibson 
3212fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
32130cffce56SDavid Gibson                           addr_start / SPAPR_MEMORY_BLOCK_SIZE);
32140cffce56SDavid Gibson     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
32150b55aa91SDavid Gibson                                               nr_lmbs, spapr_drc_index(drc));
3216cf632463SBharata B Rao out:
3217cf632463SBharata B Rao     error_propagate(errp, local_err);
3218cf632463SBharata B Rao }
3219cf632463SBharata B Rao 
322004d0ffbdSGreg Kurz static void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
3221af81cf32SBharata B Rao                                            sPAPRMachineState *spapr)
3222af81cf32SBharata B Rao {
3223af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
3224af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
32252e886fb3SSam Bobroff     int id = spapr_vcpu_id(cpu);
3226af81cf32SBharata B Rao     void *fdt;
3227af81cf32SBharata B Rao     int offset, fdt_size;
3228af81cf32SBharata B Rao     char *nodename;
3229af81cf32SBharata B Rao 
3230af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
3231af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
3232af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
3233af81cf32SBharata B Rao 
3234af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
3235af81cf32SBharata B Rao     g_free(nodename);
3236af81cf32SBharata B Rao 
3237af81cf32SBharata B Rao     *fdt_offset = offset;
3238af81cf32SBharata B Rao     return fdt;
3239af81cf32SBharata B Rao }
3240af81cf32SBharata B Rao 
3241765d1bddSDavid Gibson /* Callback to be called during DRC release. */
3242765d1bddSDavid Gibson void spapr_core_release(DeviceState *dev)
3243ff9006ddSIgor Mammedov {
3244765d1bddSDavid Gibson     MachineState *ms = MACHINE(qdev_get_hotplug_handler(dev));
324546f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
3246ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3247535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
3248ff9006ddSIgor Mammedov 
324946f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
325046f7afa3SGreg Kurz         sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
325146f7afa3SGreg Kurz         int i;
325246f7afa3SGreg Kurz 
325346f7afa3SGreg Kurz         for (i = 0; i < cc->nr_threads; i++) {
325494ad93bdSGreg Kurz             CPUState *cs = CPU(sc->threads[i]);
325546f7afa3SGreg Kurz 
325646f7afa3SGreg Kurz             pre_2_10_vmstate_register_dummy_icp(cs->cpu_index);
325746f7afa3SGreg Kurz         }
325846f7afa3SGreg Kurz     }
325946f7afa3SGreg Kurz 
326007572c06SGreg Kurz     assert(core_slot);
3261535455fdSIgor Mammedov     core_slot->cpu = NULL;
3262ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
3263ff9006ddSIgor Mammedov }
3264ff9006ddSIgor Mammedov 
3265115debf2SIgor Mammedov static
3266115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
3267ff9006ddSIgor Mammedov                                Error **errp)
3268ff9006ddSIgor Mammedov {
3269535455fdSIgor Mammedov     int index;
3270535455fdSIgor Mammedov     sPAPRDRConnector *drc;
3271535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3272535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
3273ff9006ddSIgor Mammedov 
3274535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
3275535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
3276535455fdSIgor Mammedov                    cc->core_id);
3277535455fdSIgor Mammedov         return;
3278535455fdSIgor Mammedov     }
3279ff9006ddSIgor Mammedov     if (index == 0) {
3280ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
3281ff9006ddSIgor Mammedov         return;
3282ff9006ddSIgor Mammedov     }
3283ff9006ddSIgor Mammedov 
3284fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt);
3285ff9006ddSIgor Mammedov     g_assert(drc);
3286ff9006ddSIgor Mammedov 
3287a8dc47fdSDavid Gibson     spapr_drc_detach(drc);
3288ff9006ddSIgor Mammedov 
3289ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
3290ff9006ddSIgor Mammedov }
3291ff9006ddSIgor Mammedov 
3292ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3293ff9006ddSIgor Mammedov                             Error **errp)
3294ff9006ddSIgor Mammedov {
3295ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3296ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
329746f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3298ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
3299ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
330094ad93bdSGreg Kurz     CPUState *cs = CPU(core->threads[0]);
3301ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
3302ff9006ddSIgor Mammedov     Error *local_err = NULL;
3303ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
3304535455fdSIgor Mammedov     CPUArchId *core_slot;
3305535455fdSIgor Mammedov     int index;
330694fd9cbaSLaurent Vivier     bool hotplugged = spapr_drc_hotplugged(dev);
3307ff9006ddSIgor Mammedov 
3308535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3309535455fdSIgor Mammedov     if (!core_slot) {
3310535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
3311535455fdSIgor Mammedov                    cc->core_id);
3312535455fdSIgor Mammedov         return;
3313535455fdSIgor Mammedov     }
3314fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt);
3315ff9006ddSIgor Mammedov 
3316c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
3317ff9006ddSIgor Mammedov 
3318e49c63d5SGreg Kurz     if (drc) {
3319e49c63d5SGreg Kurz         void *fdt;
3320e49c63d5SGreg Kurz         int fdt_offset;
3321e49c63d5SGreg Kurz 
3322ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
3323ff9006ddSIgor Mammedov 
33245c1da812SDavid Gibson         spapr_drc_attach(drc, dev, fdt, fdt_offset, &local_err);
3325ff9006ddSIgor Mammedov         if (local_err) {
3326ff9006ddSIgor Mammedov             g_free(fdt);
3327ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
3328ff9006ddSIgor Mammedov             return;
3329ff9006ddSIgor Mammedov         }
3330ff9006ddSIgor Mammedov 
333194fd9cbaSLaurent Vivier         if (hotplugged) {
3332ff9006ddSIgor Mammedov             /*
333394fd9cbaSLaurent Vivier              * Send hotplug notification interrupt to the guest only
333494fd9cbaSLaurent Vivier              * in case of hotplugged CPUs.
3335ff9006ddSIgor Mammedov              */
3336ff9006ddSIgor Mammedov             spapr_hotplug_req_add_by_index(drc);
333794fd9cbaSLaurent Vivier         } else {
333894fd9cbaSLaurent Vivier             spapr_drc_reset(drc);
3339ff9006ddSIgor Mammedov         }
334094fd9cbaSLaurent Vivier     }
334194fd9cbaSLaurent Vivier 
3342535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
334346f7afa3SGreg Kurz 
334446f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
334546f7afa3SGreg Kurz         int i;
334646f7afa3SGreg Kurz 
334746f7afa3SGreg Kurz         for (i = 0; i < cc->nr_threads; i++) {
334846f7afa3SGreg Kurz             sPAPRCPUCore *sc = SPAPR_CPU_CORE(dev);
334946f7afa3SGreg Kurz 
335094ad93bdSGreg Kurz             cs = CPU(sc->threads[i]);
335146f7afa3SGreg Kurz             pre_2_10_vmstate_unregister_dummy_icp(cs->cpu_index);
335246f7afa3SGreg Kurz         }
335346f7afa3SGreg Kurz     }
3354ff9006ddSIgor Mammedov }
3355ff9006ddSIgor Mammedov 
3356ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3357ff9006ddSIgor Mammedov                                 Error **errp)
3358ff9006ddSIgor Mammedov {
3359ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
3360ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
3361ff9006ddSIgor Mammedov     Error *local_err = NULL;
3362ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
33632e9c10ebSIgor Mammedov     const char *base_core_type = spapr_get_cpu_core_type(machine->cpu_type);
3364ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
3365535455fdSIgor Mammedov     CPUArchId *core_slot;
3366535455fdSIgor Mammedov     int index;
3367ff9006ddSIgor Mammedov 
3368c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
3369ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
3370ff9006ddSIgor Mammedov         goto out;
3371ff9006ddSIgor Mammedov     }
3372ff9006ddSIgor Mammedov 
3373ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
3374ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
3375ff9006ddSIgor Mammedov         goto out;
3376ff9006ddSIgor Mammedov     }
3377ff9006ddSIgor Mammedov 
3378ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
3379ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
3380ff9006ddSIgor Mammedov         goto out;
3381ff9006ddSIgor Mammedov     }
3382ff9006ddSIgor Mammedov 
3383459264efSDavid Gibson     /*
3384459264efSDavid Gibson      * In general we should have homogeneous threads-per-core, but old
3385459264efSDavid Gibson      * (pre hotplug support) machine types allow the last core to have
3386459264efSDavid Gibson      * reduced threads as a compatibility hack for when we allowed
3387459264efSDavid Gibson      * total vcpus not a multiple of threads-per-core.
3388459264efSDavid Gibson      */
3389459264efSDavid Gibson     if (mc->has_hotpluggable_cpus && (cc->nr_threads != smp_threads)) {
3390df8658deSGreg Kurz         error_setg(&local_err, "invalid nr-threads %d, must be %d",
33918149e299SDavid Gibson                    cc->nr_threads, smp_threads);
3392df8658deSGreg Kurz         goto out;
33938149e299SDavid Gibson     }
33948149e299SDavid Gibson 
3395535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3396535455fdSIgor Mammedov     if (!core_slot) {
3397ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
3398ff9006ddSIgor Mammedov         goto out;
3399ff9006ddSIgor Mammedov     }
3400ff9006ddSIgor Mammedov 
3401535455fdSIgor Mammedov     if (core_slot->cpu) {
3402ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
3403ff9006ddSIgor Mammedov         goto out;
3404ff9006ddSIgor Mammedov     }
3405ff9006ddSIgor Mammedov 
3406a0ceb640SIgor Mammedov     numa_cpu_pre_plug(core_slot, dev, &local_err);
34070b8497f0SIgor Mammedov 
3408ff9006ddSIgor Mammedov out:
3409ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
3410ff9006ddSIgor Mammedov }
3411ff9006ddSIgor Mammedov 
3412c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
3413c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
3414c20d332aSBharata B Rao {
3415c86c1affSDaniel Henrique Barboza     MachineState *ms = MACHINE(hotplug_dev);
3416c86c1affSDaniel Henrique Barboza     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
3417c20d332aSBharata B Rao 
3418c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3419b556854bSBharata B Rao         int node;
3420c20d332aSBharata B Rao 
3421c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
3422c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
3423c20d332aSBharata B Rao             return;
3424c20d332aSBharata B Rao         }
34259ed442b8SMarc-André Lureau         node = object_property_get_uint(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
3426c20d332aSBharata B Rao         if (*errp) {
3427c20d332aSBharata B Rao             return;
3428c20d332aSBharata B Rao         }
34291a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
34301a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
34311a5512bbSGonglei             return;
34321a5512bbSGonglei         }
3433c20d332aSBharata B Rao 
3434b556854bSBharata B Rao         /*
3435b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
3436b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
3437b556854bSBharata B Rao          * to the first node that has some memory. This causes two
3438b556854bSBharata B Rao          * unexpected behaviours for the user.
3439b556854bSBharata B Rao          *
3440b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
3441b556854bSBharata B Rao          *   specified.
3442b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
3443b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
3444b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
3445b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
3446b556854bSBharata B Rao          *
3447b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
3448b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
3449b556854bSBharata B Rao          */
3450b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
3451b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
3452b556854bSBharata B Rao                        node);
3453b556854bSBharata B Rao             return;
3454b556854bSBharata B Rao         }
3455b556854bSBharata B Rao 
3456c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
3457af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3458af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
3459c20d332aSBharata B Rao     }
3460c20d332aSBharata B Rao }
3461c20d332aSBharata B Rao 
3462cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
3463cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
3464cf632463SBharata B Rao {
3465c86c1affSDaniel Henrique Barboza     sPAPRMachineState *sms = SPAPR_MACHINE(OBJECT(hotplug_dev));
3466c86c1affSDaniel Henrique Barboza     MachineClass *mc = MACHINE_GET_CLASS(sms);
3467cf632463SBharata B Rao 
3468cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3469cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3470cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
3471cf632463SBharata B Rao         } else {
3472cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
3473cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
3474cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
3475cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
3476cf632463SBharata B Rao              * eventually handled by the guest after boot
3477cf632463SBharata B Rao              */
3478cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
3479cf632463SBharata B Rao         }
34806f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3481c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
34826f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
34836f4b5c3eSBharata B Rao             return;
34846f4b5c3eSBharata B Rao         }
3485115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
3486c20d332aSBharata B Rao     }
3487c20d332aSBharata B Rao }
3488c20d332aSBharata B Rao 
348994a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
349094a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
349194a94e4cSBharata B Rao {
3492c871bc70SLaurent Vivier     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3493c871bc70SLaurent Vivier         spapr_memory_pre_plug(hotplug_dev, dev, errp);
3494c871bc70SLaurent Vivier     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
349594a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
349694a94e4cSBharata B Rao     }
349794a94e4cSBharata B Rao }
349894a94e4cSBharata B Rao 
34997ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
3500c20d332aSBharata B Rao                                                  DeviceState *dev)
3501c20d332aSBharata B Rao {
350294a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
350394a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3504c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
3505c20d332aSBharata B Rao     }
3506c20d332aSBharata B Rao     return NULL;
3507c20d332aSBharata B Rao }
3508c20d332aSBharata B Rao 
3509ea089eebSIgor Mammedov static CpuInstanceProperties
3510ea089eebSIgor Mammedov spapr_cpu_index_to_props(MachineState *machine, unsigned cpu_index)
351120bb648dSDavid Gibson {
3512ea089eebSIgor Mammedov     CPUArchId *core_slot;
3513ea089eebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3514ea089eebSIgor Mammedov 
3515ea089eebSIgor Mammedov     /* make sure possible_cpu are intialized */
3516ea089eebSIgor Mammedov     mc->possible_cpu_arch_ids(machine);
3517ea089eebSIgor Mammedov     /* get CPU core slot containing thread that matches cpu_index */
3518ea089eebSIgor Mammedov     core_slot = spapr_find_cpu_slot(machine, cpu_index, NULL);
3519ea089eebSIgor Mammedov     assert(core_slot);
3520ea089eebSIgor Mammedov     return core_slot->props;
352120bb648dSDavid Gibson }
352220bb648dSDavid Gibson 
352379e07936SIgor Mammedov static int64_t spapr_get_default_cpu_node_id(const MachineState *ms, int idx)
352479e07936SIgor Mammedov {
352579e07936SIgor Mammedov     return idx / smp_cores % nb_numa_nodes;
352679e07936SIgor Mammedov }
352779e07936SIgor Mammedov 
3528535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
3529535455fdSIgor Mammedov {
3530535455fdSIgor Mammedov     int i;
3531535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
3532535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3533535455fdSIgor Mammedov 
3534c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
3535535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
3536535455fdSIgor Mammedov     }
3537535455fdSIgor Mammedov     if (machine->possible_cpus) {
3538535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
3539535455fdSIgor Mammedov         return machine->possible_cpus;
3540535455fdSIgor Mammedov     }
3541535455fdSIgor Mammedov 
3542535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
3543535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
3544535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
3545535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
3546535455fdSIgor Mammedov         int core_id = i * smp_threads;
3547535455fdSIgor Mammedov 
3548f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
3549535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
3550535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
3551535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
3552535455fdSIgor Mammedov     }
3553535455fdSIgor Mammedov     return machine->possible_cpus;
3554535455fdSIgor Mammedov }
3555535455fdSIgor Mammedov 
35566737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
3557daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
3558daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
35596737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
35606737d9adSDavid Gibson {
3561357d1e3bSDavid Gibson     /*
3562357d1e3bSDavid Gibson      * New-style PHB window placement.
3563357d1e3bSDavid Gibson      *
3564357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
3565357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
3566357d1e3bSDavid Gibson      * windows.
3567357d1e3bSDavid Gibson      *
3568357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
3569357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
3570357d1e3bSDavid Gibson      *
3571357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
3572357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
3573357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
3574357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
3575357d1e3bSDavid Gibson      */
35766737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
357725e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
357825e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
35796737d9adSDavid Gibson     int i;
35806737d9adSDavid Gibson 
3581357d1e3bSDavid Gibson     /* Sanity check natural alignments */
3582357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3583357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3584357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
3585357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
3586357d1e3bSDavid Gibson     /* Sanity check bounds */
358725e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
358825e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
358925e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
359025e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
35912efff1c0SDavid Gibson 
359225e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
359325e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
359425e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
35956737d9adSDavid Gibson         return;
35966737d9adSDavid Gibson     }
35976737d9adSDavid Gibson 
35986737d9adSDavid Gibson     *buid = base_buid + index;
35996737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
36006737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
36016737d9adSDavid Gibson     }
36026737d9adSDavid Gibson 
3603357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
3604357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
3605357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
36066737d9adSDavid Gibson }
36076737d9adSDavid Gibson 
36087844e12bSCédric Le Goater static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
36097844e12bSCédric Le Goater {
36107844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
36117844e12bSCédric Le Goater 
36127844e12bSCédric Le Goater     return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
36137844e12bSCédric Le Goater }
36147844e12bSCédric Le Goater 
36157844e12bSCédric Le Goater static void spapr_ics_resend(XICSFabric *dev)
36167844e12bSCédric Le Goater {
36177844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
36187844e12bSCédric Le Goater 
36197844e12bSCédric Le Goater     ics_resend(spapr->ics);
36207844e12bSCédric Le Goater }
36217844e12bSCédric Le Goater 
362281210c20SSam Bobroff static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
3623b2fc59aaSCédric Le Goater {
36242e886fb3SSam Bobroff     PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
3625b2fc59aaSCédric Le Goater 
36265bc8d26dSCédric Le Goater     return cpu ? ICP(cpu->intc) : NULL;
3627b2fc59aaSCédric Le Goater }
3628b2fc59aaSCédric Le Goater 
362960c6823bSCédric Le Goater #define ICS_IRQ_FREE(ics, srcno)   \
363060c6823bSCédric Le Goater     (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
363160c6823bSCédric Le Goater 
363260c6823bSCédric Le Goater static int ics_find_free_block(ICSState *ics, int num, int alignnum)
363360c6823bSCédric Le Goater {
363460c6823bSCédric Le Goater     int first, i;
363560c6823bSCédric Le Goater 
363660c6823bSCédric Le Goater     for (first = 0; first < ics->nr_irqs; first += alignnum) {
363760c6823bSCédric Le Goater         if (num > (ics->nr_irqs - first)) {
363860c6823bSCédric Le Goater             return -1;
363960c6823bSCédric Le Goater         }
364060c6823bSCédric Le Goater         for (i = first; i < first + num; ++i) {
364160c6823bSCédric Le Goater             if (!ICS_IRQ_FREE(ics, i)) {
364260c6823bSCédric Le Goater                 break;
364360c6823bSCédric Le Goater             }
364460c6823bSCédric Le Goater         }
364560c6823bSCédric Le Goater         if (i == (first + num)) {
364660c6823bSCédric Le Goater             return first;
364760c6823bSCédric Le Goater         }
364860c6823bSCédric Le Goater     }
364960c6823bSCédric Le Goater 
365060c6823bSCédric Le Goater     return -1;
365160c6823bSCédric Le Goater }
365260c6823bSCédric Le Goater 
36539e7dc5fcSCédric Le Goater /*
36549e7dc5fcSCédric Le Goater  * Allocate the IRQ number and set the IRQ type, LSI or MSI
36559e7dc5fcSCédric Le Goater  */
36569e7dc5fcSCédric Le Goater static void spapr_irq_set_lsi(sPAPRMachineState *spapr, int irq, bool lsi)
36579e7dc5fcSCédric Le Goater {
36589e7dc5fcSCédric Le Goater     ics_set_irq_type(spapr->ics, irq - spapr->ics->offset, lsi);
36599e7dc5fcSCédric Le Goater }
36609e7dc5fcSCédric Le Goater 
366160c6823bSCédric Le Goater int spapr_irq_alloc(sPAPRMachineState *spapr, int irq_hint, bool lsi,
366260c6823bSCédric Le Goater                     Error **errp)
366360c6823bSCédric Le Goater {
366460c6823bSCédric Le Goater     ICSState *ics = spapr->ics;
366560c6823bSCédric Le Goater     int irq;
366660c6823bSCédric Le Goater 
366760c6823bSCédric Le Goater     if (!ics) {
366860c6823bSCédric Le Goater         return -1;
366960c6823bSCédric Le Goater     }
367060c6823bSCédric Le Goater     if (irq_hint) {
367160c6823bSCédric Le Goater         if (!ICS_IRQ_FREE(ics, irq_hint - ics->offset)) {
367260c6823bSCédric Le Goater             error_setg(errp, "can't allocate IRQ %d: already in use", irq_hint);
367360c6823bSCédric Le Goater             return -1;
367460c6823bSCédric Le Goater         }
367560c6823bSCédric Le Goater         irq = irq_hint;
367660c6823bSCédric Le Goater     } else {
367760c6823bSCédric Le Goater         irq = ics_find_free_block(ics, 1, 1);
367860c6823bSCédric Le Goater         if (irq < 0) {
367960c6823bSCédric Le Goater             error_setg(errp, "can't allocate IRQ: no IRQ left");
368060c6823bSCédric Le Goater             return -1;
368160c6823bSCédric Le Goater         }
368260c6823bSCédric Le Goater         irq += ics->offset;
368360c6823bSCédric Le Goater     }
368460c6823bSCédric Le Goater 
36859e7dc5fcSCédric Le Goater     spapr_irq_set_lsi(spapr, irq, lsi);
368660c6823bSCédric Le Goater     trace_spapr_irq_alloc(irq);
368760c6823bSCédric Le Goater 
368860c6823bSCédric Le Goater     return irq;
368960c6823bSCédric Le Goater }
369060c6823bSCédric Le Goater 
369160c6823bSCédric Le Goater /*
369260c6823bSCédric Le Goater  * Allocate block of consecutive IRQs, and return the number of the first IRQ in
369360c6823bSCédric Le Goater  * the block. If align==true, aligns the first IRQ number to num.
369460c6823bSCédric Le Goater  */
369560c6823bSCédric Le Goater int spapr_irq_alloc_block(sPAPRMachineState *spapr, int num, bool lsi,
369660c6823bSCédric Le Goater                           bool align, Error **errp)
369760c6823bSCédric Le Goater {
369860c6823bSCédric Le Goater     ICSState *ics = spapr->ics;
369960c6823bSCédric Le Goater     int i, first = -1;
370060c6823bSCédric Le Goater 
370160c6823bSCédric Le Goater     if (!ics) {
370260c6823bSCédric Le Goater         return -1;
370360c6823bSCédric Le Goater     }
370460c6823bSCédric Le Goater 
370560c6823bSCédric Le Goater     /*
370660c6823bSCédric Le Goater      * MSIMesage::data is used for storing VIRQ so
370760c6823bSCédric Le Goater      * it has to be aligned to num to support multiple
370860c6823bSCédric Le Goater      * MSI vectors. MSI-X is not affected by this.
370960c6823bSCédric Le Goater      * The hint is used for the first IRQ, the rest should
371060c6823bSCédric Le Goater      * be allocated continuously.
371160c6823bSCédric Le Goater      */
371260c6823bSCédric Le Goater     if (align) {
371360c6823bSCédric Le Goater         assert((num == 1) || (num == 2) || (num == 4) ||
371460c6823bSCédric Le Goater                (num == 8) || (num == 16) || (num == 32));
371560c6823bSCédric Le Goater         first = ics_find_free_block(ics, num, num);
371660c6823bSCédric Le Goater     } else {
371760c6823bSCédric Le Goater         first = ics_find_free_block(ics, num, 1);
371860c6823bSCédric Le Goater     }
371960c6823bSCédric Le Goater     if (first < 0) {
372060c6823bSCédric Le Goater         error_setg(errp, "can't find a free %d-IRQ block", num);
372160c6823bSCédric Le Goater         return -1;
372260c6823bSCédric Le Goater     }
372360c6823bSCédric Le Goater 
372460c6823bSCédric Le Goater     first += ics->offset;
37259e7dc5fcSCédric Le Goater     for (i = first; i < first + num; ++i) {
37269e7dc5fcSCédric Le Goater         spapr_irq_set_lsi(spapr, i, lsi);
37279e7dc5fcSCédric Le Goater     }
372860c6823bSCédric Le Goater 
372960c6823bSCédric Le Goater     trace_spapr_irq_alloc_block(first, num, lsi, align);
373060c6823bSCédric Le Goater 
373160c6823bSCédric Le Goater     return first;
373260c6823bSCédric Le Goater }
373360c6823bSCédric Le Goater 
373460c6823bSCédric Le Goater void spapr_irq_free(sPAPRMachineState *spapr, int irq, int num)
373560c6823bSCédric Le Goater {
373660c6823bSCédric Le Goater     ICSState *ics = spapr->ics;
373760c6823bSCédric Le Goater     int srcno = irq - ics->offset;
373860c6823bSCédric Le Goater     int i;
373960c6823bSCédric Le Goater 
374060c6823bSCédric Le Goater     if (ics_valid_irq(ics, irq)) {
374160c6823bSCédric Le Goater         trace_spapr_irq_free(0, irq, num);
374260c6823bSCédric Le Goater         for (i = srcno; i < srcno + num; ++i) {
374360c6823bSCédric Le Goater             if (ICS_IRQ_FREE(ics, i)) {
374460c6823bSCédric Le Goater                 trace_spapr_irq_free_warn(0, i + ics->offset);
374560c6823bSCédric Le Goater             }
374660c6823bSCédric Le Goater             memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
374760c6823bSCédric Le Goater         }
374860c6823bSCédric Le Goater     }
374960c6823bSCédric Le Goater }
375060c6823bSCédric Le Goater 
375177183755SCédric Le Goater qemu_irq spapr_qirq(sPAPRMachineState *spapr, int irq)
375277183755SCédric Le Goater {
375377183755SCédric Le Goater     ICSState *ics = spapr->ics;
375477183755SCédric Le Goater 
375577183755SCédric Le Goater     if (ics_valid_irq(ics, irq)) {
375677183755SCédric Le Goater         return ics->qirqs[irq - ics->offset];
375777183755SCédric Le Goater     }
375877183755SCédric Le Goater 
375977183755SCédric Le Goater     return NULL;
376077183755SCédric Le Goater }
376177183755SCédric Le Goater 
37626449da45SCédric Le Goater static void spapr_pic_print_info(InterruptStatsProvider *obj,
37636449da45SCédric Le Goater                                  Monitor *mon)
37646449da45SCédric Le Goater {
37656449da45SCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
37665bc8d26dSCédric Le Goater     CPUState *cs;
37676449da45SCédric Le Goater 
37685bc8d26dSCédric Le Goater     CPU_FOREACH(cs) {
37695bc8d26dSCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
37705bc8d26dSCédric Le Goater 
37715bc8d26dSCédric Le Goater         icp_pic_print_info(ICP(cpu->intc), mon);
37726449da45SCédric Le Goater     }
37736449da45SCédric Le Goater 
37746449da45SCédric Le Goater     ics_pic_print_info(spapr->ics, mon);
37756449da45SCédric Le Goater }
37766449da45SCédric Le Goater 
37772e886fb3SSam Bobroff int spapr_vcpu_id(PowerPCCPU *cpu)
37782e886fb3SSam Bobroff {
37792e886fb3SSam Bobroff     CPUState *cs = CPU(cpu);
37802e886fb3SSam Bobroff 
37812e886fb3SSam Bobroff     if (kvm_enabled()) {
37822e886fb3SSam Bobroff         return kvm_arch_vcpu_id(cs);
37832e886fb3SSam Bobroff     } else {
37842e886fb3SSam Bobroff         return cs->cpu_index;
37852e886fb3SSam Bobroff     }
37862e886fb3SSam Bobroff }
37872e886fb3SSam Bobroff 
37882e886fb3SSam Bobroff PowerPCCPU *spapr_find_cpu(int vcpu_id)
37892e886fb3SSam Bobroff {
37902e886fb3SSam Bobroff     CPUState *cs;
37912e886fb3SSam Bobroff 
37922e886fb3SSam Bobroff     CPU_FOREACH(cs) {
37932e886fb3SSam Bobroff         PowerPCCPU *cpu = POWERPC_CPU(cs);
37942e886fb3SSam Bobroff 
37952e886fb3SSam Bobroff         if (spapr_vcpu_id(cpu) == vcpu_id) {
37962e886fb3SSam Bobroff             return cpu;
37972e886fb3SSam Bobroff         }
37982e886fb3SSam Bobroff     }
37992e886fb3SSam Bobroff 
38002e886fb3SSam Bobroff     return NULL;
38012e886fb3SSam Bobroff }
38022e886fb3SSam Bobroff 
380329ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
380453018216SPaolo Bonzini {
380529ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
3806224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
380771461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
380834316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
3809c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
38101d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
38117844e12bSCédric Le Goater     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
38126449da45SCédric Le Goater     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
381329ee3247SAlexey Kardashevskiy 
38140eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
3815fc9f38c3SDavid Gibson 
3816fc9f38c3SDavid Gibson     /*
3817fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
3818fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
3819fc9f38c3SDavid Gibson      * these details for backwards compatibility
3820fc9f38c3SDavid Gibson      */
3821bcb5ce08SDavid Gibson     mc->init = spapr_machine_init;
3822bcb5ce08SDavid Gibson     mc->reset = spapr_machine_reset;
3823958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
38246244bb7eSGreg Kurz     mc->max_cpus = 1024;
3825958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
38265b2128d2SAlexander Graf     mc->default_boot_order = "";
3827a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
3828958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
38299e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
3830e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
38317ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
383294a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
3833c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
3834ea089eebSIgor Mammedov     mc->cpu_index_to_instance_props = spapr_cpu_index_to_props;
383579e07936SIgor Mammedov     mc->get_default_cpu_node_id = spapr_get_default_cpu_node_id;
3836535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3837cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
383800b4fbe2SMarcel Apfelbaum 
3839fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
38402e9c10ebSIgor Mammedov     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power8_v2.0");
3841c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
384252b81ab5SDavid Gibson     smc->resize_hpt_default = SPAPR_RESIZE_HPT_ENABLED;
384371461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
384434316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
38456737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
38461d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3847e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3848e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3849e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3850e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
38519861bb3eSSuraj Jitindar Singh     vhc->get_patbe = spapr_get_patbe;
38521ec26c75SGreg Kurz     vhc->encode_hpt_for_kvm_pr = spapr_encode_hpt_for_kvm_pr;
38537844e12bSCédric Le Goater     xic->ics_get = spapr_ics_get;
38547844e12bSCédric Le Goater     xic->ics_resend = spapr_ics_resend;
3855b2fc59aaSCédric Le Goater     xic->icp_get = spapr_icp_get;
38566449da45SCédric Le Goater     ispc->print_info = spapr_pic_print_info;
385755641213SLaurent Vivier     /* Force NUMA node memory size to be a multiple of
385855641213SLaurent Vivier      * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
385955641213SLaurent Vivier      * in which LMBs are represented and hot-added
386055641213SLaurent Vivier      */
386155641213SLaurent Vivier     mc->numa_mem_align_shift = 28;
386233face6bSDavid Gibson 
3863*4e5fe368SSuraj Jitindar Singh     smc->default_caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
3864*4e5fe368SSuraj Jitindar Singh     smc->default_caps.caps[SPAPR_CAP_VSX] = SPAPR_CAP_ON;
3865*4e5fe368SSuraj Jitindar Singh     smc->default_caps.caps[SPAPR_CAP_DFP] = SPAPR_CAP_ON;
386633face6bSDavid Gibson     spapr_caps_add_properties(smc, &error_abort);
386753018216SPaolo Bonzini }
386853018216SPaolo Bonzini 
386929ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
387029ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
387129ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
38724aee7362SDavid Gibson     .abstract      = true,
38736ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
3874bcb5ce08SDavid Gibson     .instance_init = spapr_instance_init,
387587bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3876183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
387729ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
387871461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
387971461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
388034316482SAlexey Kardashevskiy         { TYPE_NMI },
3881c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
38821d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
38837844e12bSCédric Le Goater         { TYPE_XICS_FABRIC },
38846449da45SCédric Le Goater         { TYPE_INTERRUPT_STATS_PROVIDER },
388571461b0fSAlexey Kardashevskiy         { }
388671461b0fSAlexey Kardashevskiy     },
388729ee3247SAlexey Kardashevskiy };
388829ee3247SAlexey Kardashevskiy 
3889fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
38905013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
38915013c547SDavid Gibson                                                     void *data)      \
38925013c547SDavid Gibson     {                                                                \
38935013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
38945013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3895fccbc785SDavid Gibson         if (latest) {                                                \
3896fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3897fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3898fccbc785SDavid Gibson         }                                                            \
38995013c547SDavid Gibson     }                                                                \
39005013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
39015013c547SDavid Gibson     {                                                                \
39025013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
39035013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
39045013c547SDavid Gibson     }                                                                \
39055013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
39065013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
39075013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
39085013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
39095013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
39105013c547SDavid Gibson     };                                                               \
39115013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
39125013c547SDavid Gibson     {                                                                \
39135013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
39145013c547SDavid Gibson     }                                                                \
39150e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
39165013c547SDavid Gibson 
39171c5f29bbSDavid Gibson /*
39182b615412SDavid Gibson  * pseries-2.12
3919e2676b16SGreg Kurz  */
39202b615412SDavid Gibson static void spapr_machine_2_12_instance_options(MachineState *machine)
3921e2676b16SGreg Kurz {
3922e2676b16SGreg Kurz }
3923e2676b16SGreg Kurz 
39242b615412SDavid Gibson static void spapr_machine_2_12_class_options(MachineClass *mc)
3925e2676b16SGreg Kurz {
3926e2676b16SGreg Kurz     /* Defaults for the latest behaviour inherited from the base class */
3927e2676b16SGreg Kurz }
3928e2676b16SGreg Kurz 
39292b615412SDavid Gibson DEFINE_SPAPR_MACHINE(2_12, "2.12", true);
39302b615412SDavid Gibson 
39312b615412SDavid Gibson /*
39322b615412SDavid Gibson  * pseries-2.11
39332b615412SDavid Gibson  */
39342b615412SDavid Gibson #define SPAPR_COMPAT_2_11                                              \
39352b615412SDavid Gibson     HW_COMPAT_2_11
39362b615412SDavid Gibson 
39372b615412SDavid Gibson static void spapr_machine_2_11_instance_options(MachineState *machine)
39382b615412SDavid Gibson {
39392b615412SDavid Gibson     spapr_machine_2_12_instance_options(machine);
39402b615412SDavid Gibson }
39412b615412SDavid Gibson 
39422b615412SDavid Gibson static void spapr_machine_2_11_class_options(MachineClass *mc)
39432b615412SDavid Gibson {
3944ee76a09fSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3945ee76a09fSDavid Gibson 
39462b615412SDavid Gibson     spapr_machine_2_12_class_options(mc);
3947*4e5fe368SSuraj Jitindar Singh     smc->default_caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_ON;
39482b615412SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_11);
39492b615412SDavid Gibson }
39502b615412SDavid Gibson 
39512b615412SDavid Gibson DEFINE_SPAPR_MACHINE(2_11, "2.11", false);
3952e2676b16SGreg Kurz 
3953e2676b16SGreg Kurz /*
39543fa14fbeSDavid Gibson  * pseries-2.10
3955db800b21SDavid Gibson  */
3956e2676b16SGreg Kurz #define SPAPR_COMPAT_2_10                                              \
39572b615412SDavid Gibson     HW_COMPAT_2_10
3958e2676b16SGreg Kurz 
39593fa14fbeSDavid Gibson static void spapr_machine_2_10_instance_options(MachineState *machine)
3960db800b21SDavid Gibson {
39612b615412SDavid Gibson     spapr_machine_2_11_instance_options(machine);
3962db800b21SDavid Gibson }
3963db800b21SDavid Gibson 
39643fa14fbeSDavid Gibson static void spapr_machine_2_10_class_options(MachineClass *mc)
3965db800b21SDavid Gibson {
3966e2676b16SGreg Kurz     spapr_machine_2_11_class_options(mc);
3967e2676b16SGreg Kurz     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_10);
3968db800b21SDavid Gibson }
3969db800b21SDavid Gibson 
3970e2676b16SGreg Kurz DEFINE_SPAPR_MACHINE(2_10, "2.10", false);
39713fa14fbeSDavid Gibson 
39723fa14fbeSDavid Gibson /*
39733fa14fbeSDavid Gibson  * pseries-2.9
39743fa14fbeSDavid Gibson  */
39753fa14fbeSDavid Gibson #define SPAPR_COMPAT_2_9                                               \
3976d5fc133eSDavid Gibson     HW_COMPAT_2_9                                                      \
3977d5fc133eSDavid Gibson     {                                                                  \
3978d5fc133eSDavid Gibson         .driver = TYPE_POWERPC_CPU,                                    \
3979d5fc133eSDavid Gibson         .property = "pre-2.10-migration",                              \
3980d5fc133eSDavid Gibson         .value    = "on",                                              \
3981d5fc133eSDavid Gibson     },                                                                 \
39823fa14fbeSDavid Gibson 
39833fa14fbeSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
39843fa14fbeSDavid Gibson {
39853fa14fbeSDavid Gibson     spapr_machine_2_10_instance_options(machine);
39863fa14fbeSDavid Gibson }
39873fa14fbeSDavid Gibson 
39883fa14fbeSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
39893fa14fbeSDavid Gibson {
399046f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
399146f7afa3SGreg Kurz 
39923fa14fbeSDavid Gibson     spapr_machine_2_10_class_options(mc);
39933fa14fbeSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_9);
39943bfe5716SLaurent Vivier     mc->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
399546f7afa3SGreg Kurz     smc->pre_2_10_has_unused_icps = true;
399652b81ab5SDavid Gibson     smc->resize_hpt_default = SPAPR_RESIZE_HPT_DISABLED;
39973fa14fbeSDavid Gibson }
39983fa14fbeSDavid Gibson 
39993fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
4000fa325e6cSDavid Gibson 
4001fa325e6cSDavid Gibson /*
4002fa325e6cSDavid Gibson  * pseries-2.8
4003fa325e6cSDavid Gibson  */
4004fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                                        \
400582516263SDavid Gibson     HW_COMPAT_2_8                                               \
400682516263SDavid Gibson     {                                                           \
400782516263SDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,                 \
400882516263SDavid Gibson         .property = "pcie-extended-configuration-space",        \
400982516263SDavid Gibson         .value    = "off",                                      \
401082516263SDavid Gibson     },
4011fa325e6cSDavid Gibson 
4012fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
4013fa325e6cSDavid Gibson {
4014fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
4015fa325e6cSDavid Gibson }
4016fa325e6cSDavid Gibson 
4017fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
4018fa325e6cSDavid Gibson {
4019fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
4020fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
402155641213SLaurent Vivier     mc->numa_mem_align_shift = 23;
4022fa325e6cSDavid Gibson }
4023fa325e6cSDavid Gibson 
4024fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
4025db800b21SDavid Gibson 
4026db800b21SDavid Gibson /*
40271ea1eefcSBharata B Rao  * pseries-2.7
40281ea1eefcSBharata B Rao  */
4029db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
4030db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
4031357d1e3bSDavid Gibson     {                                               \
4032357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
4033357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
4034357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
4035357d1e3bSDavid Gibson     },                                              \
4036357d1e3bSDavid Gibson     {                                               \
4037357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
4038357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
4039357d1e3bSDavid Gibson         .value    = "0",                            \
4040146c11f1SDavid Gibson     },                                              \
4041146c11f1SDavid Gibson     {                                               \
4042146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
4043146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
4044146c11f1SDavid Gibson         .value    = "on",                           \
40455c4537bdSDavid Gibson     },                                              \
40465c4537bdSDavid Gibson     {                                               \
40475c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
40485c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
40495c4537bdSDavid Gibson         .value    = "on",                           \
4050357d1e3bSDavid Gibson     },
4051357d1e3bSDavid Gibson 
4052357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
4053357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
4054357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
4055357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
4056357d1e3bSDavid Gibson {
4057357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
4058357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
4059357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
4060357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
4061357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
4062357d1e3bSDavid Gibson     const uint32_t max_index = 255;
4063357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
4064357d1e3bSDavid Gibson 
4065357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
4066357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
4067357d1e3bSDavid Gibson     int i;
4068357d1e3bSDavid Gibson 
4069357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
4070357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
4071357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
4072357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
4073357d1e3bSDavid Gibson          * regions */
4074357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
4075357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
4076357d1e3bSDavid Gibson     }
4077357d1e3bSDavid Gibson 
4078357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
4079357d1e3bSDavid Gibson 
4080357d1e3bSDavid Gibson     if (index > max_index) {
4081357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
4082357d1e3bSDavid Gibson                    max_index);
4083357d1e3bSDavid Gibson         return;
4084357d1e3bSDavid Gibson     }
4085357d1e3bSDavid Gibson 
4086357d1e3bSDavid Gibson     *buid = base_buid + index;
4087357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
4088357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
4089357d1e3bSDavid Gibson     }
4090357d1e3bSDavid Gibson 
4091357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
4092357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
4093357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
4094357d1e3bSDavid Gibson     /*
4095357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
4096357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
4097357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
4098357d1e3bSDavid Gibson      */
4099357d1e3bSDavid Gibson }
4100db800b21SDavid Gibson 
41011ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
41021ea1eefcSBharata B Rao {
4103f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
4104f6229214SMichael Roth 
4105672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
4106f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
41071ea1eefcSBharata B Rao }
41081ea1eefcSBharata B Rao 
41091ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
41101ea1eefcSBharata B Rao {
41113daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
41123daa4a9fSThomas Huth 
4113db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
41142e9c10ebSIgor Mammedov     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power7_v2.3");
4115db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
4116357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
41171ea1eefcSBharata B Rao }
41181ea1eefcSBharata B Rao 
4119db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
41201ea1eefcSBharata B Rao 
41211ea1eefcSBharata B Rao /*
41224b23699cSDavid Gibson  * pseries-2.6
41234b23699cSDavid Gibson  */
41241ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
4125ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
4126ae4de14cSAlexey Kardashevskiy     { \
4127ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
4128ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
4129ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
4130ae4de14cSAlexey Kardashevskiy     },
41311ea1eefcSBharata B Rao 
41324b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
41334b23699cSDavid Gibson {
4134672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
41354b23699cSDavid Gibson }
41364b23699cSDavid Gibson 
41374b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
41384b23699cSDavid Gibson {
41391ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
4140c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
41411ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
41424b23699cSDavid Gibson }
41434b23699cSDavid Gibson 
41441ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
41454b23699cSDavid Gibson 
41464b23699cSDavid Gibson /*
41471c5f29bbSDavid Gibson  * pseries-2.5
41481c5f29bbSDavid Gibson  */
41494b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
415057c522f4SThomas Huth     HW_COMPAT_2_5 \
415157c522f4SThomas Huth     { \
415257c522f4SThomas Huth         .driver   = "spapr-vlan", \
415357c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
415457c522f4SThomas Huth         .value    = "off", \
415557c522f4SThomas Huth     },
41564b23699cSDavid Gibson 
41575013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
41581c5f29bbSDavid Gibson {
4159672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
41605013c547SDavid Gibson }
41615013c547SDavid Gibson 
41625013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
41635013c547SDavid Gibson {
416457040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
416557040d45SThomas Huth 
41664b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
416757040d45SThomas Huth     smc->use_ohci_by_default = true;
41684b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
41691c5f29bbSDavid Gibson }
41701c5f29bbSDavid Gibson 
41714b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
41721c5f29bbSDavid Gibson 
41731c5f29bbSDavid Gibson /*
41741c5f29bbSDavid Gibson  * pseries-2.4
41751c5f29bbSDavid Gibson  */
417680fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
417780fd50f9SCornelia Huck         HW_COMPAT_2_4
417880fd50f9SCornelia Huck 
41795013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
41801c5f29bbSDavid Gibson {
41815013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
41825013c547SDavid Gibson }
41831c5f29bbSDavid Gibson 
41845013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
41855013c547SDavid Gibson {
4186fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4187fc9f38c3SDavid Gibson 
4188fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
4189fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
4190f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
41911c5f29bbSDavid Gibson }
41921c5f29bbSDavid Gibson 
4193fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
41941c5f29bbSDavid Gibson 
41951c5f29bbSDavid Gibson /*
41961c5f29bbSDavid Gibson  * pseries-2.3
41971c5f29bbSDavid Gibson  */
419838ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
41997619c7b0SMichael Roth         HW_COMPAT_2_3 \
42007619c7b0SMichael Roth         {\
42017619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
42027619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
42037619c7b0SMichael Roth             .value    = "off",\
42047619c7b0SMichael Roth         },
420538ff32c6SEduardo Habkost 
42065013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
42071c5f29bbSDavid Gibson {
42085013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
42091c5f29bbSDavid Gibson }
42101c5f29bbSDavid Gibson 
42115013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
42121c5f29bbSDavid Gibson {
4213fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
4214f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
42151c5f29bbSDavid Gibson }
4216fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
42171c5f29bbSDavid Gibson 
42181c5f29bbSDavid Gibson /*
42191c5f29bbSDavid Gibson  * pseries-2.2
42201c5f29bbSDavid Gibson  */
42211c5f29bbSDavid Gibson 
4222b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
42234dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
4224b194df47SAlexey Kardashevskiy         {\
4225b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
4226b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
4227b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
4228dd754bafSEduardo Habkost         },
4229b194df47SAlexey Kardashevskiy 
42305013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
4231b0e966d0SJason Wang {
42325013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
4233cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
4234b0e966d0SJason Wang }
4235b0e966d0SJason Wang 
42365013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
4237b0e966d0SJason Wang {
4238fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
4239f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
42401c5f29bbSDavid Gibson }
4241fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
42421c5f29bbSDavid Gibson 
42431c5f29bbSDavid Gibson /*
42441c5f29bbSDavid Gibson  * pseries-2.1
42451c5f29bbSDavid Gibson  */
42461c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
42471c5f29bbSDavid Gibson         HW_COMPAT_2_1
42481c5f29bbSDavid Gibson 
42495013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
42501c5f29bbSDavid Gibson {
42515013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
42521c5f29bbSDavid Gibson }
42531c5f29bbSDavid Gibson 
42545013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
4255b0e966d0SJason Wang {
4256fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
4257f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
42586026db45SAlexey Kardashevskiy }
4259fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
42606026db45SAlexey Kardashevskiy 
426129ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
426229ee3247SAlexey Kardashevskiy {
426329ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
426429ee3247SAlexey Kardashevskiy }
426529ee3247SAlexey Kardashevskiy 
426629ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
4267