xref: /openbmc/qemu/hw/ppc/spapr.c (revision b8768042)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2004-2007 Fabrice Bellard
553018216SPaolo Bonzini  * Copyright (c) 2007 Jocelyn Mayer
653018216SPaolo Bonzini  * Copyright (c) 2010 David Gibson, IBM Corporation.
753018216SPaolo Bonzini  *
853018216SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
953018216SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
1053018216SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
1153018216SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1253018216SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
1353018216SPaolo Bonzini  * furnished to do so, subject to the following conditions:
1453018216SPaolo Bonzini  *
1553018216SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
1653018216SPaolo Bonzini  * all copies or substantial portions of the Software.
1753018216SPaolo Bonzini  *
1853018216SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1953018216SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2053018216SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2153018216SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2253018216SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2353018216SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2453018216SPaolo Bonzini  * THE SOFTWARE.
2553018216SPaolo Bonzini  *
2653018216SPaolo Bonzini  */
270d75590dSPeter Maydell #include "qemu/osdep.h"
28da34e65cSMarkus Armbruster #include "qapi/error.h"
2953018216SPaolo Bonzini #include "sysemu/sysemu.h"
30e35704baSEduardo Habkost #include "sysemu/numa.h"
3153018216SPaolo Bonzini #include "hw/hw.h"
3203dd024fSPaolo Bonzini #include "qemu/log.h"
3371461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h"
3453018216SPaolo Bonzini #include "elf.h"
3553018216SPaolo Bonzini #include "net/net.h"
36ad440b4aSAndrew Jones #include "sysemu/device_tree.h"
37fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3853018216SPaolo Bonzini #include "sysemu/cpus.h"
39b3946626SVincent Palatin #include "sysemu/hw_accel.h"
4053018216SPaolo Bonzini #include "kvm_ppc.h"
41c4b63b7cSJuan Quintela #include "migration/misc.h"
4284a899deSJuan Quintela #include "migration/global_state.h"
43f2a8f0a6SJuan Quintela #include "migration/register.h"
444be21d56SDavid Gibson #include "mmu-hash64.h"
45b4db5413SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
463794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4753018216SPaolo Bonzini 
4853018216SPaolo Bonzini #include "hw/boards.h"
490d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
5053018216SPaolo Bonzini #include "hw/loader.h"
5153018216SPaolo Bonzini 
527804c353SCédric Le Goater #include "hw/ppc/fdt.h"
530d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
540d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
550d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
560d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5753018216SPaolo Bonzini #include "hw/pci/msi.h"
5853018216SPaolo Bonzini 
5953018216SPaolo Bonzini #include "hw/pci/pci.h"
6071461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
6171461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
62c4e13492SFelipe Franciosi #include "hw/virtio/vhost-scsi-common.h"
6353018216SPaolo Bonzini 
6453018216SPaolo Bonzini #include "exec/address-spaces.h"
6553018216SPaolo Bonzini #include "hw/usb.h"
6653018216SPaolo Bonzini #include "qemu/config-file.h"
67135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
682a6593cbSAlexey Kardashevskiy #include "trace.h"
6934316482SAlexey Kardashevskiy #include "hw/nmi.h"
706449da45SCédric Le Goater #include "hw/intc/intc.h"
7153018216SPaolo Bonzini 
7268a27b20SMichael S. Tsirkin #include "hw/compat.h"
73f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7494a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
752474bfd4SIgor Mammedov #include "qmp-commands.h"
7668a27b20SMichael S. Tsirkin 
7753018216SPaolo Bonzini #include <libfdt.h>
7853018216SPaolo Bonzini 
7953018216SPaolo Bonzini /* SLOF memory layout:
8053018216SPaolo Bonzini  *
8153018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
8253018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
8353018216SPaolo Bonzini  *
8453018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8553018216SPaolo Bonzini  * and more
8653018216SPaolo Bonzini  *
8753018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8853018216SPaolo Bonzini  */
8938b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
9053018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
91b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
9253018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
9353018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9453018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9553018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9653018216SPaolo Bonzini 
9753018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9853018216SPaolo Bonzini 
9953018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
10053018216SPaolo Bonzini 
10153018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
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];
213833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_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 {
2420da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_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 */
256e957f6a9SSam Bobroff static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset,
257e957f6a9SSam Bobroff                                       bool legacy_guest)
25886d5771aSSam Bobroff {
25986d5771aSSam Bobroff     uint8_t pa_features_206[] = { 6, 0,
26086d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
26186d5771aSSam Bobroff     uint8_t pa_features_207[] = { 24, 0,
26286d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
26386d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
26486d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
26586d5771aSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
2669fb4541fSSam Bobroff     uint8_t pa_features_300[] = { 66, 0,
2679fb4541fSSam Bobroff         /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
2689fb4541fSSam Bobroff         /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
26986d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
2709fb4541fSSam Bobroff         /* 6: DS207 */
27186d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
2729fb4541fSSam Bobroff         /* 16: Vector */
27386d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
2749fb4541fSSam Bobroff         /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
2759bf502feSDavid Gibson         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
2769fb4541fSSam Bobroff         /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
2779fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
2789fb4541fSSam Bobroff         /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
2799fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
2809fb4541fSSam Bobroff         /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
2819fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
2829fb4541fSSam Bobroff         /* 42: PM, 44: PC RA, 46: SC vec'd */
2839fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
2849fb4541fSSam Bobroff         /* 48: SIMD, 50: QP BFP, 52: String */
2859fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
2869fb4541fSSam Bobroff         /* 54: DecFP, 56: DecI, 58: SHA */
2879fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
2889fb4541fSSam Bobroff         /* 60: NM atomic, 62: RNG */
2899fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
2909fb4541fSSam Bobroff     };
29186d5771aSSam Bobroff     uint8_t *pa_features;
29286d5771aSSam Bobroff     size_t pa_size;
29386d5771aSSam Bobroff 
29486d5771aSSam Bobroff     switch (POWERPC_MMU_VER(env->mmu_model)) {
29586d5771aSSam Bobroff     case POWERPC_MMU_VER_2_06:
29686d5771aSSam Bobroff         pa_features = pa_features_206;
29786d5771aSSam Bobroff         pa_size = sizeof(pa_features_206);
29886d5771aSSam Bobroff         break;
29986d5771aSSam Bobroff     case POWERPC_MMU_VER_2_07:
30086d5771aSSam Bobroff         pa_features = pa_features_207;
30186d5771aSSam Bobroff         pa_size = sizeof(pa_features_207);
30286d5771aSSam Bobroff         break;
30386d5771aSSam Bobroff     case POWERPC_MMU_VER_3_00:
30486d5771aSSam Bobroff         pa_features = pa_features_300;
30586d5771aSSam Bobroff         pa_size = sizeof(pa_features_300);
30686d5771aSSam Bobroff         break;
30786d5771aSSam Bobroff     default:
30886d5771aSSam Bobroff         return;
30986d5771aSSam Bobroff     }
31086d5771aSSam Bobroff 
31186d5771aSSam Bobroff     if (env->ci_large_pages) {
31286d5771aSSam Bobroff         /*
31386d5771aSSam Bobroff          * Note: we keep CI large pages off by default because a 64K capable
31486d5771aSSam Bobroff          * guest provisioned with large pages might otherwise try to map a qemu
31586d5771aSSam Bobroff          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
31686d5771aSSam Bobroff          * even if that qemu runs on a 4k host.
31786d5771aSSam Bobroff          * We dd this bit back here if we are confident this is not an issue
31886d5771aSSam Bobroff          */
31986d5771aSSam Bobroff         pa_features[3] |= 0x20;
32086d5771aSSam Bobroff     }
32186d5771aSSam Bobroff     if (kvmppc_has_cap_htm() && pa_size > 24) {
32286d5771aSSam Bobroff         pa_features[24] |= 0x80;    /* Transactional memory support */
32386d5771aSSam Bobroff     }
324e957f6a9SSam Bobroff     if (legacy_guest && pa_size > 40) {
325e957f6a9SSam Bobroff         /* Workaround for broken kernels that attempt (guest) radix
326e957f6a9SSam Bobroff          * mode when they can't handle it, if they see the radix bit set
327e957f6a9SSam Bobroff          * in pa-features. So hide it from them. */
328e957f6a9SSam Bobroff         pa_features[40 + 2] &= ~0x80; /* Radix MMU */
329e957f6a9SSam Bobroff     }
33086d5771aSSam Bobroff 
33186d5771aSSam Bobroff     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
33286d5771aSSam Bobroff }
33386d5771aSSam Bobroff 
33428e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
33553018216SPaolo Bonzini {
33682677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
33782677ed2SAlexey Kardashevskiy     CPUState *cs;
33853018216SPaolo Bonzini     char cpu_model[32];
33953018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
34053018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
34153018216SPaolo Bonzini 
34282677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
34382677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
344e957f6a9SSam Bobroff         CPUPPCState *env = &cpu->env;
34582677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
34682677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
34712dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
34853018216SPaolo Bonzini 
3490f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
35053018216SPaolo Bonzini             continue;
35153018216SPaolo Bonzini         }
35253018216SPaolo Bonzini 
35382677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
35453018216SPaolo Bonzini 
35582677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
35682677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
35782677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
35882677ed2SAlexey Kardashevskiy                                           "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 
389e957f6a9SSam Bobroff         spapr_populate_pa_features(env, fdt, offset,
390e957f6a9SSam Bobroff                                          spapr->cas_legacy_guest_workaround);
39153018216SPaolo Bonzini     }
39253018216SPaolo Bonzini     return ret;
39353018216SPaolo Bonzini }
39453018216SPaolo Bonzini 
395b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
396b082d65aSAlexey Kardashevskiy {
397fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
398fb164994SDavid Gibson 
399b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
400b082d65aSAlexey Kardashevskiy         int i;
401b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
402b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
403fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
404fb164994SDavid Gibson                            machine->ram_size);
405b082d65aSAlexey Kardashevskiy             }
406b082d65aSAlexey Kardashevskiy         }
407b082d65aSAlexey Kardashevskiy     }
408fb164994SDavid Gibson     return machine->ram_size;
409b082d65aSAlexey Kardashevskiy }
410b082d65aSAlexey Kardashevskiy 
411a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
412a1d59c0fSAlexey Kardashevskiy {
413a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
414a1d59c0fSAlexey Kardashevskiy }
41553018216SPaolo Bonzini 
41603d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
41726a8c353SAlexey Kardashevskiy                                        hwaddr size)
41826a8c353SAlexey Kardashevskiy {
41926a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
42026a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
42126a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
422c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
42326a8c353SAlexey Kardashevskiy     };
42426a8c353SAlexey Kardashevskiy     char mem_name[32];
42526a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
42626a8c353SAlexey Kardashevskiy     int off;
42726a8c353SAlexey Kardashevskiy 
42826a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
42926a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
43026a8c353SAlexey Kardashevskiy 
43126a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
43226a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
43326a8c353SAlexey Kardashevskiy     _FDT(off);
43426a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
43526a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
43626a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
43726a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
43826a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
43903d196b7SBharata B Rao     return off;
44026a8c353SAlexey Kardashevskiy }
44126a8c353SAlexey Kardashevskiy 
44228e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
44353018216SPaolo Bonzini {
444fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
4457db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
4467db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
4477db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
4487db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
44953018216SPaolo Bonzini 
4507db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
4517db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
4527db8a127SAlexey Kardashevskiy         nb_nodes = 1;
453fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
4547db8a127SAlexey Kardashevskiy         nodes = &ramnode;
4555fe269b1SPaul Mackerras     }
45653018216SPaolo Bonzini 
4577db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
4587db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
4597db8a127SAlexey Kardashevskiy             continue;
46053018216SPaolo Bonzini         }
461fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
4625fe269b1SPaul Mackerras             node_size = 0;
4635fe269b1SPaul Mackerras         } else {
4647db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
465fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
466fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
4675fe269b1SPaul Mackerras             }
4685fe269b1SPaul Mackerras         }
4697db8a127SAlexey Kardashevskiy         if (!mem_start) {
4707db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
471e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
4727db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
4737db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
4747db8a127SAlexey Kardashevskiy         }
4756010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
4766010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
4776010818cSAlexey Kardashevskiy 
4786010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
4796010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
4806010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
4816010818cSAlexey Kardashevskiy             }
4826010818cSAlexey Kardashevskiy 
4836010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
4846010818cSAlexey Kardashevskiy             node_size -= sizetmp;
4856010818cSAlexey Kardashevskiy             mem_start += sizetmp;
4866010818cSAlexey Kardashevskiy         }
48753018216SPaolo Bonzini     }
48853018216SPaolo Bonzini 
48953018216SPaolo Bonzini     return 0;
49053018216SPaolo Bonzini }
49153018216SPaolo Bonzini 
4920da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4930da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4940da6f3feSBharata B Rao {
4950da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4960da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4970da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4980da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
4990da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
5000da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
501afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
502afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
5030da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
5040da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
5050da6f3feSBharata B Rao     size_t page_sizes_prop_size;
50622419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
5070da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
50812dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
509af81cf32SBharata B Rao     sPAPRDRConnector *drc;
510af81cf32SBharata B Rao     int drc_index;
511c64abd1fSSam Bobroff     uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
512c64abd1fSSam Bobroff     int i;
513af81cf32SBharata B Rao 
514fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index);
515af81cf32SBharata B Rao     if (drc) {
5160b55aa91SDavid Gibson         drc_index = spapr_drc_index(drc);
517af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
518af81cf32SBharata B Rao     }
5190da6f3feSBharata B Rao 
5200da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
5210da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
5220da6f3feSBharata B Rao 
5230da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
5240da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
5250da6f3feSBharata B Rao                            env->dcache_line_size)));
5260da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
5270da6f3feSBharata B Rao                            env->dcache_line_size)));
5280da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
5290da6f3feSBharata B Rao                            env->icache_line_size)));
5300da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
5310da6f3feSBharata B Rao                            env->icache_line_size)));
5320da6f3feSBharata B Rao 
5330da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
5340da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
5350da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
5360da6f3feSBharata B Rao     } else {
537ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
5380da6f3feSBharata B Rao     }
5390da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
5400da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
5410da6f3feSBharata B Rao                                pcc->l1_icache_size)));
5420da6f3feSBharata B Rao     } else {
543ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
5440da6f3feSBharata B Rao     }
5450da6f3feSBharata B Rao 
5460da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
5470da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
548fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
5490da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
5500da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
5510da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
5520da6f3feSBharata B Rao 
5530da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
5540da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
5550da6f3feSBharata B Rao     }
5560da6f3feSBharata B Rao 
5570da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
5580da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
5590da6f3feSBharata B Rao                           segs, sizeof(segs))));
5600da6f3feSBharata B Rao     }
5610da6f3feSBharata B Rao 
5620da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
5630da6f3feSBharata B Rao      *   0 / no property == no vector extensions
5640da6f3feSBharata B Rao      *   1               == VMX / Altivec available
5650da6f3feSBharata B Rao      *   2               == VSX available */
5660da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
5670da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
5680da6f3feSBharata B Rao 
5690da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
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 */
5750da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
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 
586e957f6a9SSam Bobroff     spapr_populate_pa_features(env, 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);
6310da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_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 
64803d196b7SBharata B Rao /*
64903d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
65003d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
65103d196b7SBharata B Rao  * of this device tree node.
65203d196b7SBharata B Rao  */
65303d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
65403d196b7SBharata B Rao {
65503d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
65603d196b7SBharata B Rao     int ret, i, offset;
65703d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
65803d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
659d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
660d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
661d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
662d0e5a8f2SBharata B Rao                        lmb_size;
66303d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
6646663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
66503d196b7SBharata B Rao 
666ef001f06SThomas Huth     /*
667d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
66816c25aefSBharata B Rao      */
669d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
67016c25aefSBharata B Rao         return 0;
67116c25aefSBharata B Rao     }
67216c25aefSBharata B Rao 
67316c25aefSBharata B Rao     /*
674ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
675ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
676ef001f06SThomas Huth      */
677ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
678ef001f06SThomas Huth               * sizeof(uint32_t);
67903d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
68003d196b7SBharata B Rao 
68103d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
68203d196b7SBharata B Rao 
68303d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
68403d196b7SBharata B Rao                     sizeof(prop_lmb_size));
68503d196b7SBharata B Rao     if (ret < 0) {
68603d196b7SBharata B Rao         goto out;
68703d196b7SBharata B Rao     }
68803d196b7SBharata B Rao 
68903d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
69003d196b7SBharata B Rao     if (ret < 0) {
69103d196b7SBharata B Rao         goto out;
69203d196b7SBharata B Rao     }
69303d196b7SBharata B Rao 
69403d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
69503d196b7SBharata B Rao     if (ret < 0) {
69603d196b7SBharata B Rao         goto out;
69703d196b7SBharata B Rao     }
69803d196b7SBharata B Rao 
69903d196b7SBharata B Rao     /* ibm,dynamic-memory */
70003d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
70103d196b7SBharata B Rao     cur_index++;
70203d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
703d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
70403d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
70503d196b7SBharata B Rao 
706d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
707d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
708d0e5a8f2SBharata B Rao 
709fbf55397SDavid Gibson             drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, i);
71003d196b7SBharata B Rao             g_assert(drc);
71103d196b7SBharata B Rao 
71203d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
71303d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
7140b55aa91SDavid Gibson             dynamic_memory[2] = cpu_to_be32(spapr_drc_index(drc));
71503d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
71603d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
717d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
71803d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
71903d196b7SBharata B Rao             } else {
72003d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
72103d196b7SBharata B Rao             }
722d0e5a8f2SBharata B Rao         } else {
723d0e5a8f2SBharata B Rao             /*
724d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
725d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
726d0e5a8f2SBharata B Rao              * and as having no valid DRC.
727d0e5a8f2SBharata B Rao              */
728d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
729d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
730d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
731d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
732d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
733d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
734d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
735d0e5a8f2SBharata B Rao         }
73603d196b7SBharata B Rao 
73703d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
73803d196b7SBharata B Rao     }
73903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
74003d196b7SBharata B Rao     if (ret < 0) {
74103d196b7SBharata B Rao         goto out;
74203d196b7SBharata B Rao     }
74303d196b7SBharata B Rao 
74403d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
74503d196b7SBharata B Rao     cur_index = int_buf;
7466663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
74703d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
74803d196b7SBharata B Rao     cur_index += 2;
7496663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
75003d196b7SBharata B Rao         uint32_t associativity[] = {
75103d196b7SBharata B Rao             cpu_to_be32(0x0),
75203d196b7SBharata B Rao             cpu_to_be32(0x0),
75303d196b7SBharata B Rao             cpu_to_be32(0x0),
75403d196b7SBharata B Rao             cpu_to_be32(i)
75503d196b7SBharata B Rao         };
75603d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
75703d196b7SBharata B Rao         cur_index += 4;
75803d196b7SBharata B Rao     }
75903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
76003d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
76103d196b7SBharata B Rao out:
76203d196b7SBharata B Rao     g_free(int_buf);
76303d196b7SBharata B Rao     return ret;
76403d196b7SBharata B Rao }
76503d196b7SBharata B Rao 
7666787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
7676787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
7686787d27bSMichael Roth {
7696787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
770417ece33SMichael Roth     int ret = 0, offset;
7716787d27bSMichael Roth 
7726787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
7736787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
7746787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
7756787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
776417ece33SMichael Roth         if (ret) {
777417ece33SMichael Roth             goto out;
778417ece33SMichael Roth         }
7796787d27bSMichael Roth     }
7806787d27bSMichael Roth 
781*b8768042SCédric Le Goater     /* /interrupt controller */
782*b8768042SCédric Le Goater     if (!spapr_ovec_test(ov5_updates, OV5_XIVE_EXPLOIT)) {
783*b8768042SCédric Le Goater         spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
784*b8768042SCédric Le Goater     }
785*b8768042SCédric Le Goater 
786417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
787417ece33SMichael Roth     if (offset < 0) {
788417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
789417ece33SMichael Roth         if (offset < 0) {
790417ece33SMichael Roth             return offset;
791417ece33SMichael Roth         }
792417ece33SMichael Roth     }
793417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
794417ece33SMichael Roth                                  "ibm,architecture-vec-5");
795417ece33SMichael Roth 
796417ece33SMichael Roth out:
7976787d27bSMichael Roth     return ret;
7986787d27bSMichael Roth }
7996787d27bSMichael Roth 
80003d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
80103d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
8026787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
80303d196b7SBharata B Rao {
80403d196b7SBharata B Rao     void *fdt, *fdt_skel;
80503d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
80603d196b7SBharata B Rao 
80703d196b7SBharata B Rao     size -= sizeof(hdr);
80803d196b7SBharata B Rao 
809*b8768042SCédric Le Goater     /* Create skeleton */
81003d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
81103d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
81203d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
81303d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
81403d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
81503d196b7SBharata B Rao     fdt = g_malloc0(size);
81603d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
81703d196b7SBharata B Rao     g_free(fdt_skel);
81803d196b7SBharata B Rao 
81903d196b7SBharata B Rao     /* Fixup cpu nodes */
82003d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
82103d196b7SBharata B Rao 
8226787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
8236787d27bSMichael Roth         return -1;
82403d196b7SBharata B Rao     }
82503d196b7SBharata B Rao 
82603d196b7SBharata B Rao     /* Pack resulting tree */
82703d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
82803d196b7SBharata B Rao 
82903d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
83003d196b7SBharata B Rao         trace_spapr_cas_failed(size);
83103d196b7SBharata B Rao         return -1;
83203d196b7SBharata B Rao     }
83303d196b7SBharata B Rao 
83403d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
83503d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
83603d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
83703d196b7SBharata B Rao     g_free(fdt);
83803d196b7SBharata B Rao 
83903d196b7SBharata B Rao     return 0;
84003d196b7SBharata B Rao }
84103d196b7SBharata B Rao 
8423f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
8433f5dabceSDavid Gibson {
8443f5dabceSDavid Gibson     int rtas;
8453f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
8463f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
8473f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
8483f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
8493f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
8503f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
8513f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
8523f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
8533f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
8543f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
8553f5dabceSDavid Gibson     };
8563f5dabceSDavid Gibson 
8573f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
8583f5dabceSDavid Gibson 
8593f5dabceSDavid Gibson     /* hypertas */
8603f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
8613f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
8623f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
8633f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
8643f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
8653f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
8663f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
8673f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
8683f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
8693f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
8703f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
8713f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
8723f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
8733f5dabceSDavid Gibson 
8743f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
8753f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
8763f5dabceSDavid Gibson     }
8773f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
8783f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
8793f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
8803f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
8813f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
8823f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
8833f5dabceSDavid Gibson 
8843f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
8853f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
8863f5dabceSDavid Gibson 
8873f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
8883f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
8893f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
8903f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
8913f5dabceSDavid Gibson 
8923f5dabceSDavid Gibson     if (msi_nonbroken) {
8933f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
8943f5dabceSDavid Gibson     }
8953f5dabceSDavid Gibson 
8963f5dabceSDavid Gibson     /*
8973f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
8983f5dabceSDavid Gibson      * back to the guest cpu.
8993f5dabceSDavid Gibson      *
9003f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
9013f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
9023f5dabceSDavid Gibson      */
9033f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
9043f5dabceSDavid Gibson 
9053f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
9063f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
9073f5dabceSDavid Gibson 
9083f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
9093f5dabceSDavid Gibson }
9103f5dabceSDavid Gibson 
9119fb4541fSSam Bobroff /* Prepare ibm,arch-vec-5-platform-support, which indicates the MMU features
9129fb4541fSSam Bobroff  * that the guest may request and thus the valid values for bytes 24..26 of
9139fb4541fSSam Bobroff  * option vector 5: */
9149fb4541fSSam Bobroff static void spapr_dt_ov5_platform_support(void *fdt, int chosen)
9159fb4541fSSam Bobroff {
916545d6e2bSSuraj Jitindar Singh     PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
917545d6e2bSSuraj Jitindar Singh 
918f2b14e3aSCédric Le Goater     char val[2 * 4] = {
919f2b14e3aSCédric Le Goater         23, 0x00, /* Xive mode: 0 = legacy (as in ISA 2.7), 1 = Exploitation */
9209fb4541fSSam Bobroff         24, 0x00, /* Hash/Radix, filled in below. */
9219fb4541fSSam Bobroff         25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
9229fb4541fSSam Bobroff         26, 0x40, /* Radix options: GTSE == yes. */
9239fb4541fSSam Bobroff     };
9249fb4541fSSam Bobroff 
9259fb4541fSSam Bobroff     if (kvm_enabled()) {
9269fb4541fSSam Bobroff         if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
927f2b14e3aSCédric Le Goater             val[3] = 0x80; /* OV5_MMU_BOTH */
9289fb4541fSSam Bobroff         } else if (kvmppc_has_cap_mmu_radix()) {
929f2b14e3aSCédric Le Goater             val[3] = 0x40; /* OV5_MMU_RADIX_300 */
9309fb4541fSSam Bobroff         } else {
931f2b14e3aSCédric Le Goater             val[3] = 0x00; /* Hash */
9329fb4541fSSam Bobroff         }
9339fb4541fSSam Bobroff     } else {
934545d6e2bSSuraj Jitindar Singh         if (first_ppc_cpu->env.mmu_model & POWERPC_MMU_V3) {
935545d6e2bSSuraj Jitindar Singh             /* V3 MMU supports both hash and radix (with dynamic switching) */
936f2b14e3aSCédric Le Goater             val[3] = 0xC0;
937545d6e2bSSuraj Jitindar Singh         } else {
938545d6e2bSSuraj Jitindar Singh             /* Otherwise we can only do hash */
939f2b14e3aSCédric Le Goater             val[3] = 0x00;
9409fb4541fSSam Bobroff         }
941545d6e2bSSuraj Jitindar Singh     }
9429fb4541fSSam Bobroff     _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
9439fb4541fSSam Bobroff                      val, sizeof(val)));
9449fb4541fSSam Bobroff }
9459fb4541fSSam Bobroff 
9467c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
9477c866c6aSDavid Gibson {
9487c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
9497c866c6aSDavid Gibson     int chosen;
9507c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
9517c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
9527c866c6aSDavid Gibson     size_t cb = 0;
9537c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
9547c866c6aSDavid Gibson 
9557c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
9567c866c6aSDavid Gibson 
9577c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
9587c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
9597c866c6aSDavid Gibson                           spapr->initrd_base));
9607c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
9617c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
9627c866c6aSDavid Gibson 
9637c866c6aSDavid Gibson     if (spapr->kernel_size) {
9647c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
9657c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
9667c866c6aSDavid Gibson 
9677c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
9687c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
9697c866c6aSDavid Gibson         if (spapr->kernel_le) {
9707c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
9717c866c6aSDavid Gibson         }
9727c866c6aSDavid Gibson     }
9737c866c6aSDavid Gibson     if (boot_menu) {
9747c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
9757c866c6aSDavid Gibson     }
9767c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
9777c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
9787c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
9797c866c6aSDavid Gibson 
9807c866c6aSDavid Gibson     if (cb && bootlist) {
9817c866c6aSDavid Gibson         int i;
9827c866c6aSDavid Gibson 
9837c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
9847c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
9857c866c6aSDavid Gibson                 bootlist[i] = ' ';
9867c866c6aSDavid Gibson             }
9877c866c6aSDavid Gibson         }
9887c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
9897c866c6aSDavid Gibson     }
9907c866c6aSDavid Gibson 
9917c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
9927c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
9937c866c6aSDavid Gibson     }
9947c866c6aSDavid Gibson 
9957c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
9967c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
9977c866c6aSDavid Gibson     }
9987c866c6aSDavid Gibson 
9999fb4541fSSam Bobroff     spapr_dt_ov5_platform_support(fdt, chosen);
10009fb4541fSSam Bobroff 
10017c866c6aSDavid Gibson     g_free(stdout_path);
10027c866c6aSDavid Gibson     g_free(bootlist);
10037c866c6aSDavid Gibson }
10047c866c6aSDavid Gibson 
1005fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
1006fca5f2dcSDavid Gibson {
1007fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
1008fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
1009fca5f2dcSDavid Gibson     int hypervisor;
1010fca5f2dcSDavid Gibson     uint8_t hypercall[16];
1011fca5f2dcSDavid Gibson 
1012fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
1013fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
1014fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
1015fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
1016fca5f2dcSDavid Gibson         /*
1017fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
1018fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
1019fca5f2dcSDavid Gibson          */
1020fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
1021fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
1022fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
1023fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
1024fca5f2dcSDavid Gibson         }
1025fca5f2dcSDavid Gibson     }
1026fca5f2dcSDavid Gibson }
1027fca5f2dcSDavid Gibson 
1028997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
102953018216SPaolo Bonzini                              hwaddr rtas_addr,
103053018216SPaolo Bonzini                              hwaddr rtas_size)
103153018216SPaolo Bonzini {
10325b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
10333c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
1034c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
10357c866c6aSDavid Gibson     int ret;
103653018216SPaolo Bonzini     void *fdt;
103753018216SPaolo Bonzini     sPAPRPHBState *phb;
1038398a0bd5SDavid Gibson     char *buf;
103953018216SPaolo Bonzini 
1040398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
1041398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
104253018216SPaolo Bonzini 
1043398a0bd5SDavid Gibson     /* Root node */
1044398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
1045398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
1046398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
1047398a0bd5SDavid Gibson 
1048398a0bd5SDavid Gibson     /*
1049398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
1050398a0bd5SDavid Gibson      * and what is the uuid of the guest
1051398a0bd5SDavid Gibson      */
1052398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
1053398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1054398a0bd5SDavid Gibson         g_free(buf);
1055398a0bd5SDavid Gibson     }
1056398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
1057398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1058398a0bd5SDavid Gibson         g_free(buf);
1059398a0bd5SDavid Gibson     }
1060398a0bd5SDavid Gibson 
1061398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
1062398a0bd5SDavid Gibson 
1063398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1064398a0bd5SDavid Gibson     if (qemu_uuid_set) {
1065398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1066398a0bd5SDavid Gibson     }
1067398a0bd5SDavid Gibson     g_free(buf);
1068398a0bd5SDavid Gibson 
1069398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
1070398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1071398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
1072398a0bd5SDavid Gibson     }
1073398a0bd5SDavid Gibson 
1074398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1075398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
107653018216SPaolo Bonzini 
1077e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
1078e8f986fcSBharata B Rao     if (ret < 0) {
1079ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
1080e8f986fcSBharata B Rao         exit(1);
108153018216SPaolo Bonzini     }
108253018216SPaolo Bonzini 
1083bf5a6696SDavid Gibson     /* /vdevice */
1084bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
108553018216SPaolo Bonzini 
10864d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
10874d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
10884d9392beSThomas Huth         if (ret < 0) {
1089ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
10904d9392beSThomas Huth             exit(1);
10914d9392beSThomas Huth         }
10924d9392beSThomas Huth     }
10934d9392beSThomas Huth 
109453018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
109553018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
109653018216SPaolo Bonzini         if (ret < 0) {
1097da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
109853018216SPaolo Bonzini             exit(1);
109953018216SPaolo Bonzini         }
1100da34fed7SThomas Huth     }
110153018216SPaolo Bonzini 
11020da6f3feSBharata B Rao     /* cpus */
11030da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
110453018216SPaolo Bonzini 
1105c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
1106c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1107c20d332aSBharata B Rao     }
1108c20d332aSBharata B Rao 
1109c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
1110af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1111af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1112af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1113af81cf32SBharata B Rao         if (ret < 0) {
1114af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1115af81cf32SBharata B Rao             exit(1);
1116af81cf32SBharata B Rao         }
1117af81cf32SBharata B Rao     }
1118af81cf32SBharata B Rao 
1119ffb1e275SDavid Gibson     /* /event-sources */
1120ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1121ffb1e275SDavid Gibson 
11223f5dabceSDavid Gibson     /* /rtas */
11233f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
11243f5dabceSDavid Gibson 
11257c866c6aSDavid Gibson     /* /chosen */
11267c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1127cf6e5223SDavid Gibson 
1128fca5f2dcSDavid Gibson     /* /hypervisor */
1129fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1130fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1131fca5f2dcSDavid Gibson     }
1132fca5f2dcSDavid Gibson 
1133cf6e5223SDavid Gibson     /* Build memory reserve map */
1134cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1135cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1136cf6e5223SDavid Gibson     }
1137cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1138cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1139cf6e5223SDavid Gibson     }
1140cf6e5223SDavid Gibson 
11416787d27bSMichael Roth     /* ibm,client-architecture-support updates */
11426787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
11436787d27bSMichael Roth     if (ret < 0) {
11446787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
11456787d27bSMichael Roth         exit(1);
11466787d27bSMichael Roth     }
11476787d27bSMichael Roth 
1148997b6cfcSDavid Gibson     return fdt;
114953018216SPaolo Bonzini }
115053018216SPaolo Bonzini 
115153018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
115253018216SPaolo Bonzini {
115353018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
115453018216SPaolo Bonzini }
115553018216SPaolo Bonzini 
11561d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
11571d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
115853018216SPaolo Bonzini {
115953018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
116053018216SPaolo Bonzini 
11618d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
11628d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
11638d04fb55SJan Kiszka 
116453018216SPaolo Bonzini     if (msr_pr) {
116553018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
116653018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
116753018216SPaolo Bonzini     } else {
116853018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
116953018216SPaolo Bonzini     }
117053018216SPaolo Bonzini }
117153018216SPaolo Bonzini 
11729861bb3eSSuraj Jitindar Singh static uint64_t spapr_get_patbe(PPCVirtualHypervisor *vhyp)
11739861bb3eSSuraj Jitindar Singh {
11749861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
11759861bb3eSSuraj Jitindar Singh 
11769861bb3eSSuraj Jitindar Singh     return spapr->patb_entry;
11779861bb3eSSuraj Jitindar Singh }
11789861bb3eSSuraj Jitindar Singh 
1179e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1180e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1181e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1182e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1183e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1184e6b8fd24SSamuel Mendoza-Jonas 
1185715c5407SDavid Gibson /*
1186715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1187715c5407SDavid Gibson  */
1188715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1189715c5407SDavid Gibson {
1190715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1191715c5407SDavid Gibson         return spapr->htab_fd;
1192715c5407SDavid Gibson     }
1193715c5407SDavid Gibson 
1194715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1195715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1196715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1197715c5407SDavid Gibson                      strerror(errno));
1198715c5407SDavid Gibson     }
1199715c5407SDavid Gibson 
1200715c5407SDavid Gibson     return spapr->htab_fd;
1201715c5407SDavid Gibson }
1202715c5407SDavid Gibson 
1203b4db5413SSuraj Jitindar Singh void close_htab_fd(sPAPRMachineState *spapr)
1204715c5407SDavid Gibson {
1205715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1206715c5407SDavid Gibson         close(spapr->htab_fd);
1207715c5407SDavid Gibson     }
1208715c5407SDavid Gibson     spapr->htab_fd = -1;
1209715c5407SDavid Gibson }
1210715c5407SDavid Gibson 
1211e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1212e57ca75cSDavid Gibson {
1213e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1214e57ca75cSDavid Gibson 
1215e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1216e57ca75cSDavid Gibson }
1217e57ca75cSDavid Gibson 
1218e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1219e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1220e57ca75cSDavid Gibson {
1221e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1222e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1223e57ca75cSDavid Gibson 
1224e57ca75cSDavid Gibson     if (!spapr->htab) {
1225e57ca75cSDavid Gibson         /*
1226e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1227e57ca75cSDavid Gibson          */
1228e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1229e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1230e57ca75cSDavid Gibson         return hptes;
1231e57ca75cSDavid Gibson     }
1232e57ca75cSDavid Gibson 
1233e57ca75cSDavid Gibson     /*
1234e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1235e57ca75cSDavid Gibson      * accessible PTEG.
1236e57ca75cSDavid Gibson      */
1237e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1238e57ca75cSDavid Gibson }
1239e57ca75cSDavid Gibson 
1240e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1241e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1242e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1243e57ca75cSDavid Gibson {
1244e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1245e57ca75cSDavid Gibson 
1246e57ca75cSDavid Gibson     if (!spapr->htab) {
1247e57ca75cSDavid Gibson         g_free((void *)hptes);
1248e57ca75cSDavid Gibson     }
1249e57ca75cSDavid Gibson 
1250e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1251e57ca75cSDavid Gibson }
1252e57ca75cSDavid Gibson 
1253e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1254e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1255e57ca75cSDavid Gibson {
1256e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1257e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1258e57ca75cSDavid Gibson 
1259e57ca75cSDavid Gibson     if (!spapr->htab) {
1260e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1261e57ca75cSDavid Gibson     } else {
1262e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1263e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1264e57ca75cSDavid Gibson     }
1265e57ca75cSDavid Gibson }
1266e57ca75cSDavid Gibson 
12678dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
12688dfe8e7fSDavid Gibson {
12698dfe8e7fSDavid Gibson     int shift;
12708dfe8e7fSDavid Gibson 
12718dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
12728dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
12738dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
12748dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
12758dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
12768dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
12778dfe8e7fSDavid Gibson     return shift;
12788dfe8e7fSDavid Gibson }
12798dfe8e7fSDavid Gibson 
128006ec79e8SBharata B Rao void spapr_free_hpt(sPAPRMachineState *spapr)
128106ec79e8SBharata B Rao {
128206ec79e8SBharata B Rao     g_free(spapr->htab);
128306ec79e8SBharata B Rao     spapr->htab = NULL;
128406ec79e8SBharata B Rao     spapr->htab_shift = 0;
128506ec79e8SBharata B Rao     close_htab_fd(spapr);
128606ec79e8SBharata B Rao }
128706ec79e8SBharata B Rao 
1288c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1289c5f54f3eSDavid Gibson                                  Error **errp)
129053018216SPaolo Bonzini {
1291c5f54f3eSDavid Gibson     long rc;
129253018216SPaolo Bonzini 
1293c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
129406ec79e8SBharata B Rao     spapr_free_hpt(spapr);
129553018216SPaolo Bonzini 
1296c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1297c5f54f3eSDavid Gibson     if (rc < 0) {
1298c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1299c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1300c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1301c5f54f3eSDavid Gibson                          shift);
1302c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1303c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1304c5f54f3eSDavid Gibson     } else if (rc > 0) {
1305c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1306c5f54f3eSDavid Gibson         if (rc != shift) {
1307c5f54f3eSDavid Gibson             error_setg(errp,
1308c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1309c5f54f3eSDavid Gibson                        shift, rc);
13107735fedaSBharata B Rao         }
13117735fedaSBharata B Rao 
131253018216SPaolo Bonzini         spapr->htab_shift = shift;
1313c18ad9a5SDavid Gibson         spapr->htab = NULL;
1314b817772aSBharata B Rao     } else {
1315c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1316c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1317c5f54f3eSDavid Gibson         int i;
131801a57972SSamuel Mendoza-Jonas 
1319c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1320c5f54f3eSDavid Gibson         if (!spapr->htab) {
1321c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1322c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1323c5f54f3eSDavid Gibson             return;
1324b817772aSBharata B Rao         }
1325b817772aSBharata B Rao 
1326c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1327c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1328b817772aSBharata B Rao 
1329c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1330c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
13317735fedaSBharata B Rao         }
133253018216SPaolo Bonzini     }
133353018216SPaolo Bonzini }
133453018216SPaolo Bonzini 
1335b4db5413SSuraj Jitindar Singh void spapr_setup_hpt_and_vrma(sPAPRMachineState *spapr)
1336b4db5413SSuraj Jitindar Singh {
1337b4db5413SSuraj Jitindar Singh     spapr_reallocate_hpt(spapr,
1338b4db5413SSuraj Jitindar Singh                      spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size),
1339b4db5413SSuraj Jitindar Singh                      &error_fatal);
1340b4db5413SSuraj Jitindar Singh     if (spapr->vrma_adjust) {
1341b4db5413SSuraj Jitindar Singh         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1342b4db5413SSuraj Jitindar Singh                                           spapr->htab_shift);
1343b4db5413SSuraj Jitindar Singh     }
1344b4db5413SSuraj Jitindar Singh     /* We're setting up a hash table, so that means we're not radix */
1345b4db5413SSuraj Jitindar Singh     spapr->patb_entry = 0;
1346b4db5413SSuraj Jitindar Singh }
1347b4db5413SSuraj Jitindar Singh 
13484f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
13499e3f9733SAlexander Graf {
13509e3f9733SAlexander Graf     bool matched = false;
13519e3f9733SAlexander Graf 
13529e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
13539e3f9733SAlexander Graf         matched = true;
13549e3f9733SAlexander Graf     }
13559e3f9733SAlexander Graf 
13569e3f9733SAlexander Graf     if (!matched) {
13579e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
13589e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
13599e3f9733SAlexander Graf         exit(1);
13609e3f9733SAlexander Graf     }
13619e3f9733SAlexander Graf }
13629e3f9733SAlexander Graf 
136353018216SPaolo Bonzini static void ppc_spapr_reset(void)
136453018216SPaolo Bonzini {
1365c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1366c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1367182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1368b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1369cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1370997b6cfcSDavid Gibson     void *fdt;
1371997b6cfcSDavid Gibson     int rc;
1372259186a7SAndreas Färber 
13739e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
13749e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
13759e3f9733SAlexander Graf 
1376b4db5413SSuraj Jitindar Singh     if (kvm_enabled() && kvmppc_has_cap_mmu_radix()) {
1377b4db5413SSuraj Jitindar Singh         /* If using KVM with radix mode available, VCPUs can be started
1378b4db5413SSuraj Jitindar Singh          * without a HPT because KVM will start them in radix mode.
1379b4db5413SSuraj Jitindar Singh          * Set the GR bit in PATB so that we know there is no HPT. */
1380b4db5413SSuraj Jitindar Singh         spapr->patb_entry = PATBE1_GR;
1381b4db5413SSuraj Jitindar Singh     } else {
1382b4db5413SSuraj Jitindar Singh         spapr_setup_hpt_and_vrma(spapr);
1383c5f54f3eSDavid Gibson     }
138453018216SPaolo Bonzini 
138553018216SPaolo Bonzini     qemu_devices_reset();
138653018216SPaolo Bonzini 
1387b7d1f77aSBenjamin Herrenschmidt     /*
1388b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1389b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1390b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1391b7d1f77aSBenjamin Herrenschmidt      */
1392b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1393cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1394cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1395b7d1f77aSBenjamin Herrenschmidt 
13966787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
13976787d27bSMichael Roth      * negotiated options and start from scratch */
13986787d27bSMichael Roth     if (!spapr->cas_reboot) {
13996787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
14006787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
140166d5c492SDavid Gibson 
140266d5c492SDavid Gibson         ppc_set_compat_all(spapr->max_compat_pvr, &error_fatal);
14036787d27bSMichael Roth     }
14046787d27bSMichael Roth 
1405cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
140653018216SPaolo Bonzini 
14072cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1408b7d1f77aSBenjamin Herrenschmidt 
1409997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1410997b6cfcSDavid Gibson 
1411997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1412997b6cfcSDavid Gibson     assert(rc == 0);
1413997b6cfcSDavid Gibson 
1414997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1415997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1416997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1417997b6cfcSDavid Gibson         exit(1);
1418997b6cfcSDavid Gibson     }
1419997b6cfcSDavid Gibson 
1420997b6cfcSDavid Gibson     /* Load the fdt */
1421997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1422cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1423997b6cfcSDavid Gibson     g_free(fdt);
1424997b6cfcSDavid Gibson 
142553018216SPaolo Bonzini     /* Set up the entry state */
1426182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1427cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1428182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1429182735efSAndreas Färber     first_cpu->halted = 0;
14301b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
143153018216SPaolo Bonzini 
14326787d27bSMichael Roth     spapr->cas_reboot = false;
143353018216SPaolo Bonzini }
143453018216SPaolo Bonzini 
143528e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
143653018216SPaolo Bonzini {
14372ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
14383978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
143953018216SPaolo Bonzini 
14403978b863SPaolo Bonzini     if (dinfo) {
14416231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
14426231a6daSMarkus Armbruster                             &error_fatal);
144353018216SPaolo Bonzini     }
144453018216SPaolo Bonzini 
144553018216SPaolo Bonzini     qdev_init_nofail(dev);
144653018216SPaolo Bonzini 
144753018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
144853018216SPaolo Bonzini }
144953018216SPaolo Bonzini 
145028e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
145128df36a1SDavid Gibson {
1452147ff807SCédric Le Goater     object_initialize(&spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC);
1453147ff807SCédric Le Goater     object_property_add_child(OBJECT(spapr), "rtc", OBJECT(&spapr->rtc),
1454147ff807SCédric Le Goater                               &error_fatal);
1455147ff807SCédric Le Goater     object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1456147ff807SCédric Le Goater                               &error_fatal);
1457147ff807SCédric Le Goater     object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1458147ff807SCédric Le Goater                               "date", &error_fatal);
145928df36a1SDavid Gibson }
146028df36a1SDavid Gibson 
146153018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
146214c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
146353018216SPaolo Bonzini {
146453018216SPaolo Bonzini     switch (vga_interface_type) {
146553018216SPaolo Bonzini     case VGA_NONE:
14667effdaa3SMark Wu         return false;
14677effdaa3SMark Wu     case VGA_DEVICE:
14687effdaa3SMark Wu         return true;
146953018216SPaolo Bonzini     case VGA_STD:
1470b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
147153018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
147253018216SPaolo Bonzini     default:
147314c6a894SDavid Gibson         error_setg(errp,
147414c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
147514c6a894SDavid Gibson         return false;
147653018216SPaolo Bonzini     }
147753018216SPaolo Bonzini }
147853018216SPaolo Bonzini 
1479880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1480880ae7deSDavid Gibson {
148128e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1482880ae7deSDavid Gibson     int err = 0;
1483880ae7deSDavid Gibson 
1484a7ff1212SCédric Le Goater     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
14855bc8d26dSCédric Le Goater         CPUState *cs;
14865bc8d26dSCédric Le Goater         CPU_FOREACH(cs) {
14875bc8d26dSCédric Le Goater             PowerPCCPU *cpu = POWERPC_CPU(cs);
14885bc8d26dSCédric Le Goater             icp_resend(ICP(cpu->intc));
1489a7ff1212SCédric Le Goater         }
1490a7ff1212SCédric Le Goater     }
1491a7ff1212SCédric Le Goater 
1492631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1493880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1494880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1495880ae7deSDavid Gibson      * value into the RTC device */
1496880ae7deSDavid Gibson     if (version_id < 3) {
1497147ff807SCédric Le Goater         err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
1498880ae7deSDavid Gibson     }
1499880ae7deSDavid Gibson 
1500d39c90f5SBharata B Rao     if (spapr->patb_entry) {
1501d39c90f5SBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
1502d39c90f5SBharata B Rao         bool radix = !!(spapr->patb_entry & PATBE1_GR);
1503d39c90f5SBharata B Rao         bool gtse = !!(cpu->env.spr[SPR_LPCR] & LPCR_GTSE);
1504d39c90f5SBharata B Rao 
1505d39c90f5SBharata B Rao         err = kvmppc_configure_v3_mmu(cpu, radix, gtse, spapr->patb_entry);
1506d39c90f5SBharata B Rao         if (err) {
1507d39c90f5SBharata B Rao             error_report("Process table config unsupported by the host");
1508d39c90f5SBharata B Rao             return -EINVAL;
1509d39c90f5SBharata B Rao         }
1510d39c90f5SBharata B Rao     }
1511d39c90f5SBharata B Rao 
1512880ae7deSDavid Gibson     return err;
1513880ae7deSDavid Gibson }
1514880ae7deSDavid Gibson 
1515880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1516880ae7deSDavid Gibson {
1517880ae7deSDavid Gibson     return version_id < 3;
1518880ae7deSDavid Gibson }
1519880ae7deSDavid Gibson 
152062ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
152162ef3760SMichael Roth {
152262ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
152362ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
152462ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
152562ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
152662ef3760SMichael Roth     bool cas_needed;
152762ef3760SMichael Roth 
152862ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
152962ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
153062ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
153162ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
153262ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
153362ef3760SMichael Roth      * negotiatied on the source side.
153462ef3760SMichael Roth      *
153562ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
153662ef3760SMichael Roth      * are the only options available on the current machine/platform.
153762ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
153862ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
153962ef3760SMichael Roth      * compatibility.
154062ef3760SMichael Roth      *
154162ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
154262ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
154362ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
154462ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
154562ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
154662ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
154762ef3760SMichael Roth      *
154862ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
154962ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
155062ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
155162ef3760SMichael Roth      */
155262ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
155362ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
155462ef3760SMichael Roth 
155562ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
155662ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
155762ef3760SMichael Roth      * removed via machine options, which could generate a false positive
155862ef3760SMichael Roth      * that breaks migration.
155962ef3760SMichael Roth      */
156062ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
156162ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
156262ef3760SMichael Roth 
156362ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
156462ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
156562ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
156662ef3760SMichael Roth 
156762ef3760SMichael Roth     return cas_needed;
156862ef3760SMichael Roth }
156962ef3760SMichael Roth 
157062ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
157162ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
157262ef3760SMichael Roth     .version_id = 1,
157362ef3760SMichael Roth     .minimum_version_id = 1,
157462ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
157562ef3760SMichael Roth     .fields = (VMStateField[]) {
157662ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
157762ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
157862ef3760SMichael Roth         VMSTATE_END_OF_LIST()
157962ef3760SMichael Roth     },
158062ef3760SMichael Roth };
158162ef3760SMichael Roth 
15829861bb3eSSuraj Jitindar Singh static bool spapr_patb_entry_needed(void *opaque)
15839861bb3eSSuraj Jitindar Singh {
15849861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = opaque;
15859861bb3eSSuraj Jitindar Singh 
15869861bb3eSSuraj Jitindar Singh     return !!spapr->patb_entry;
15879861bb3eSSuraj Jitindar Singh }
15889861bb3eSSuraj Jitindar Singh 
15899861bb3eSSuraj Jitindar Singh static const VMStateDescription vmstate_spapr_patb_entry = {
15909861bb3eSSuraj Jitindar Singh     .name = "spapr_patb_entry",
15919861bb3eSSuraj Jitindar Singh     .version_id = 1,
15929861bb3eSSuraj Jitindar Singh     .minimum_version_id = 1,
15939861bb3eSSuraj Jitindar Singh     .needed = spapr_patb_entry_needed,
15949861bb3eSSuraj Jitindar Singh     .fields = (VMStateField[]) {
15959861bb3eSSuraj Jitindar Singh         VMSTATE_UINT64(patb_entry, sPAPRMachineState),
15969861bb3eSSuraj Jitindar Singh         VMSTATE_END_OF_LIST()
15979861bb3eSSuraj Jitindar Singh     },
15989861bb3eSSuraj Jitindar Singh };
15999861bb3eSSuraj Jitindar Singh 
16004be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
16014be21d56SDavid Gibson     .name = "spapr",
1602880ae7deSDavid Gibson     .version_id = 3,
16034be21d56SDavid Gibson     .minimum_version_id = 1,
1604880ae7deSDavid Gibson     .post_load = spapr_post_load,
16054be21d56SDavid Gibson     .fields = (VMStateField[]) {
1606880ae7deSDavid Gibson         /* used to be @next_irq */
1607880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
16084be21d56SDavid Gibson 
16094be21d56SDavid Gibson         /* RTC offset */
161028e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1611880ae7deSDavid Gibson 
161228e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
16134be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
16144be21d56SDavid Gibson     },
161562ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
161662ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
16179861bb3eSSuraj Jitindar Singh         &vmstate_spapr_patb_entry,
161862ef3760SMichael Roth         NULL
161962ef3760SMichael Roth     }
16204be21d56SDavid Gibson };
16214be21d56SDavid Gibson 
16224be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
16234be21d56SDavid Gibson {
162428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
16254be21d56SDavid Gibson 
16264be21d56SDavid Gibson     /* "Iteration" header */
16273a384297SBharata B Rao     if (!spapr->htab_shift) {
16283a384297SBharata B Rao         qemu_put_be32(f, -1);
16293a384297SBharata B Rao     } else {
16304be21d56SDavid Gibson         qemu_put_be32(f, spapr->htab_shift);
16313a384297SBharata B Rao     }
16324be21d56SDavid Gibson 
1633e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1634e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1635e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1636e68cb8b4SAlexey Kardashevskiy     } else {
16373a384297SBharata B Rao         if (spapr->htab_shift) {
1638e68cb8b4SAlexey Kardashevskiy             assert(kvm_enabled());
16394be21d56SDavid Gibson         }
16403a384297SBharata B Rao     }
16414be21d56SDavid Gibson 
1642e68cb8b4SAlexey Kardashevskiy 
1643e68cb8b4SAlexey Kardashevskiy     return 0;
1644e68cb8b4SAlexey Kardashevskiy }
16454be21d56SDavid Gibson 
164628e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
16474be21d56SDavid Gibson                                  int64_t max_ns)
16484be21d56SDavid Gibson {
1649378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
16504be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
16514be21d56SDavid Gibson     int index = spapr->htab_save_index;
1652bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
16534be21d56SDavid Gibson 
16544be21d56SDavid Gibson     assert(spapr->htab_first_pass);
16554be21d56SDavid Gibson 
16564be21d56SDavid Gibson     do {
16574be21d56SDavid Gibson         int chunkstart;
16584be21d56SDavid Gibson 
16594be21d56SDavid Gibson         /* Consume invalid HPTEs */
16604be21d56SDavid Gibson         while ((index < htabslots)
16614be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
16624be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
166324ec2863SMarc-André Lureau             index++;
16644be21d56SDavid Gibson         }
16654be21d56SDavid Gibson 
16664be21d56SDavid Gibson         /* Consume valid HPTEs */
16674be21d56SDavid Gibson         chunkstart = index;
1668338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
16694be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
16704be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
167124ec2863SMarc-André Lureau             index++;
16724be21d56SDavid Gibson         }
16734be21d56SDavid Gibson 
16744be21d56SDavid Gibson         if (index > chunkstart) {
16754be21d56SDavid Gibson             int n_valid = index - chunkstart;
16764be21d56SDavid Gibson 
16774be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
16784be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
16794be21d56SDavid Gibson             qemu_put_be16(f, 0);
16804be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
16814be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
16824be21d56SDavid Gibson 
1683378bc217SDavid Gibson             if (has_timeout &&
1684378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
16854be21d56SDavid Gibson                 break;
16864be21d56SDavid Gibson             }
16874be21d56SDavid Gibson         }
16884be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
16894be21d56SDavid Gibson 
16904be21d56SDavid Gibson     if (index >= htabslots) {
16914be21d56SDavid Gibson         assert(index == htabslots);
16924be21d56SDavid Gibson         index = 0;
16934be21d56SDavid Gibson         spapr->htab_first_pass = false;
16944be21d56SDavid Gibson     }
16954be21d56SDavid Gibson     spapr->htab_save_index = index;
16964be21d56SDavid Gibson }
16974be21d56SDavid Gibson 
169828e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
16994be21d56SDavid Gibson                                 int64_t max_ns)
17004be21d56SDavid Gibson {
17014be21d56SDavid Gibson     bool final = max_ns < 0;
17024be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
17034be21d56SDavid Gibson     int examined = 0, sent = 0;
17044be21d56SDavid Gibson     int index = spapr->htab_save_index;
1705bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
17064be21d56SDavid Gibson 
17074be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
17084be21d56SDavid Gibson 
17094be21d56SDavid Gibson     do {
17104be21d56SDavid Gibson         int chunkstart, invalidstart;
17114be21d56SDavid Gibson 
17124be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
17134be21d56SDavid Gibson         while ((index < htabslots)
17144be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
17154be21d56SDavid Gibson             index++;
17164be21d56SDavid Gibson             examined++;
17174be21d56SDavid Gibson         }
17184be21d56SDavid Gibson 
17194be21d56SDavid Gibson         chunkstart = index;
17204be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1721338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
17224be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
17234be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
17244be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
17254be21d56SDavid Gibson             index++;
17264be21d56SDavid Gibson             examined++;
17274be21d56SDavid Gibson         }
17284be21d56SDavid Gibson 
17294be21d56SDavid Gibson         invalidstart = index;
17304be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1731338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
17324be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
17334be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
17344be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
17354be21d56SDavid Gibson             index++;
17364be21d56SDavid Gibson             examined++;
17374be21d56SDavid Gibson         }
17384be21d56SDavid Gibson 
17394be21d56SDavid Gibson         if (index > chunkstart) {
17404be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
17414be21d56SDavid Gibson             int n_invalid = index - invalidstart;
17424be21d56SDavid Gibson 
17434be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
17444be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
17454be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
17464be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
17474be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
17484be21d56SDavid Gibson             sent += index - chunkstart;
17494be21d56SDavid Gibson 
1750bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
17514be21d56SDavid Gibson                 break;
17524be21d56SDavid Gibson             }
17534be21d56SDavid Gibson         }
17544be21d56SDavid Gibson 
17554be21d56SDavid Gibson         if (examined >= htabslots) {
17564be21d56SDavid Gibson             break;
17574be21d56SDavid Gibson         }
17584be21d56SDavid Gibson 
17594be21d56SDavid Gibson         if (index >= htabslots) {
17604be21d56SDavid Gibson             assert(index == htabslots);
17614be21d56SDavid Gibson             index = 0;
17624be21d56SDavid Gibson         }
17634be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
17644be21d56SDavid Gibson 
17654be21d56SDavid Gibson     if (index >= htabslots) {
17664be21d56SDavid Gibson         assert(index == htabslots);
17674be21d56SDavid Gibson         index = 0;
17684be21d56SDavid Gibson     }
17694be21d56SDavid Gibson 
17704be21d56SDavid Gibson     spapr->htab_save_index = index;
17714be21d56SDavid Gibson 
1772e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
17734be21d56SDavid Gibson }
17744be21d56SDavid Gibson 
1775e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1776e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1777e68cb8b4SAlexey Kardashevskiy 
17784be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
17794be21d56SDavid Gibson {
178028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1781715c5407SDavid Gibson     int fd;
1782e68cb8b4SAlexey Kardashevskiy     int rc = 0;
17834be21d56SDavid Gibson 
17844be21d56SDavid Gibson     /* Iteration header */
17853a384297SBharata B Rao     if (!spapr->htab_shift) {
17863a384297SBharata B Rao         qemu_put_be32(f, -1);
17873a384297SBharata B Rao         return 0;
17883a384297SBharata B Rao     } else {
17894be21d56SDavid Gibson         qemu_put_be32(f, 0);
17903a384297SBharata B Rao     }
17914be21d56SDavid Gibson 
1792e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1793e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1794e68cb8b4SAlexey Kardashevskiy 
1795715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1796715c5407SDavid Gibson         if (fd < 0) {
1797715c5407SDavid Gibson             return fd;
179801a57972SSamuel Mendoza-Jonas         }
179901a57972SSamuel Mendoza-Jonas 
1800715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1801e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1802e68cb8b4SAlexey Kardashevskiy             return rc;
1803e68cb8b4SAlexey Kardashevskiy         }
1804e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
18054be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
18064be21d56SDavid Gibson     } else {
1807e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
18084be21d56SDavid Gibson     }
18094be21d56SDavid Gibson 
18104be21d56SDavid Gibson     /* End marker */
18114be21d56SDavid Gibson     qemu_put_be32(f, 0);
18124be21d56SDavid Gibson     qemu_put_be16(f, 0);
18134be21d56SDavid Gibson     qemu_put_be16(f, 0);
18144be21d56SDavid Gibson 
1815e68cb8b4SAlexey Kardashevskiy     return rc;
18164be21d56SDavid Gibson }
18174be21d56SDavid Gibson 
18184be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
18194be21d56SDavid Gibson {
182028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1821715c5407SDavid Gibson     int fd;
18224be21d56SDavid Gibson 
18234be21d56SDavid Gibson     /* Iteration header */
18243a384297SBharata B Rao     if (!spapr->htab_shift) {
18253a384297SBharata B Rao         qemu_put_be32(f, -1);
18263a384297SBharata B Rao         return 0;
18273a384297SBharata B Rao     } else {
18284be21d56SDavid Gibson         qemu_put_be32(f, 0);
18293a384297SBharata B Rao     }
18304be21d56SDavid Gibson 
1831e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1832e68cb8b4SAlexey Kardashevskiy         int rc;
1833e68cb8b4SAlexey Kardashevskiy 
1834e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1835e68cb8b4SAlexey Kardashevskiy 
1836715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1837715c5407SDavid Gibson         if (fd < 0) {
1838715c5407SDavid Gibson             return fd;
183901a57972SSamuel Mendoza-Jonas         }
184001a57972SSamuel Mendoza-Jonas 
1841715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1842e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1843e68cb8b4SAlexey Kardashevskiy             return rc;
1844e68cb8b4SAlexey Kardashevskiy         }
1845e68cb8b4SAlexey Kardashevskiy     } else {
1846378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1847378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1848378bc217SDavid Gibson         }
18494be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1850e68cb8b4SAlexey Kardashevskiy     }
18514be21d56SDavid Gibson 
18524be21d56SDavid Gibson     /* End marker */
18534be21d56SDavid Gibson     qemu_put_be32(f, 0);
18544be21d56SDavid Gibson     qemu_put_be16(f, 0);
18554be21d56SDavid Gibson     qemu_put_be16(f, 0);
18564be21d56SDavid Gibson 
18574be21d56SDavid Gibson     return 0;
18584be21d56SDavid Gibson }
18594be21d56SDavid Gibson 
18604be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
18614be21d56SDavid Gibson {
186228e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
18634be21d56SDavid Gibson     uint32_t section_hdr;
1864e68cb8b4SAlexey Kardashevskiy     int fd = -1;
18654be21d56SDavid Gibson 
18664be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
186798a5d100SDavid Gibson         error_report("htab_load() bad version");
18684be21d56SDavid Gibson         return -EINVAL;
18694be21d56SDavid Gibson     }
18704be21d56SDavid Gibson 
18714be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
18724be21d56SDavid Gibson 
18733a384297SBharata B Rao     if (section_hdr == -1) {
18743a384297SBharata B Rao         spapr_free_hpt(spapr);
18753a384297SBharata B Rao         return 0;
18763a384297SBharata B Rao     }
18773a384297SBharata B Rao 
18784be21d56SDavid Gibson     if (section_hdr) {
18799897e462SGreg Kurz         Error *local_err = NULL;
1880c5f54f3eSDavid Gibson 
1881c5f54f3eSDavid Gibson         /* First section gives the htab size */
1882c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1883c5f54f3eSDavid Gibson         if (local_err) {
1884c5f54f3eSDavid Gibson             error_report_err(local_err);
18854be21d56SDavid Gibson             return -EINVAL;
18864be21d56SDavid Gibson         }
18874be21d56SDavid Gibson         return 0;
18884be21d56SDavid Gibson     }
18894be21d56SDavid Gibson 
1890e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1891e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1892e68cb8b4SAlexey Kardashevskiy 
1893e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1894e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
189598a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1896e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1897e68cb8b4SAlexey Kardashevskiy         }
1898e68cb8b4SAlexey Kardashevskiy     }
1899e68cb8b4SAlexey Kardashevskiy 
19004be21d56SDavid Gibson     while (true) {
19014be21d56SDavid Gibson         uint32_t index;
19024be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
19034be21d56SDavid Gibson 
19044be21d56SDavid Gibson         index = qemu_get_be32(f);
19054be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
19064be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
19074be21d56SDavid Gibson 
19084be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
19094be21d56SDavid Gibson             /* End of Stream */
19104be21d56SDavid Gibson             break;
19114be21d56SDavid Gibson         }
19124be21d56SDavid Gibson 
1913e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
19144be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
19154be21d56SDavid Gibson             /* Bad index in stream */
191698a5d100SDavid Gibson             error_report(
191798a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
191898a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
19194be21d56SDavid Gibson             return -EINVAL;
19204be21d56SDavid Gibson         }
19214be21d56SDavid Gibson 
1922e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
19234be21d56SDavid Gibson             if (n_valid) {
19244be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
19254be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
19264be21d56SDavid Gibson             }
19274be21d56SDavid Gibson             if (n_invalid) {
19284be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
19294be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
19304be21d56SDavid Gibson             }
1931e68cb8b4SAlexey Kardashevskiy         } else {
1932e68cb8b4SAlexey Kardashevskiy             int rc;
1933e68cb8b4SAlexey Kardashevskiy 
1934e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1935e68cb8b4SAlexey Kardashevskiy 
1936e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1937e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1938e68cb8b4SAlexey Kardashevskiy                 return rc;
1939e68cb8b4SAlexey Kardashevskiy             }
1940e68cb8b4SAlexey Kardashevskiy         }
1941e68cb8b4SAlexey Kardashevskiy     }
1942e68cb8b4SAlexey Kardashevskiy 
1943e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1944e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1945e68cb8b4SAlexey Kardashevskiy         close(fd);
19464be21d56SDavid Gibson     }
19474be21d56SDavid Gibson 
19484be21d56SDavid Gibson     return 0;
19494be21d56SDavid Gibson }
19504be21d56SDavid Gibson 
1951c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1952c573fc03SThomas Huth {
1953c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1954c573fc03SThomas Huth 
1955c573fc03SThomas Huth     close_htab_fd(spapr);
1956c573fc03SThomas Huth }
1957c573fc03SThomas Huth 
19584be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
19594be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
19604be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1961a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1962c573fc03SThomas Huth     .cleanup = htab_cleanup,
19634be21d56SDavid Gibson     .load_state = htab_load,
19644be21d56SDavid Gibson };
19654be21d56SDavid Gibson 
19665b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
19675b2128d2SAlexander Graf                            Error **errp)
19685b2128d2SAlexander Graf {
19695b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
19705b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
19715b2128d2SAlexander Graf }
19725b2128d2SAlexander Graf 
1973224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1974224245bfSDavid Gibson {
1975224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1976224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1977e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1978224245bfSDavid Gibson     int i;
1979224245bfSDavid Gibson 
1980224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1981224245bfSDavid Gibson         uint64_t addr;
1982224245bfSDavid Gibson 
1983e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
19846caf3ac6SDavid Gibson         spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_LMB,
1985224245bfSDavid Gibson                                addr / lmb_size);
1986224245bfSDavid Gibson     }
1987224245bfSDavid Gibson }
1988224245bfSDavid Gibson 
1989224245bfSDavid Gibson /*
1990224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1991224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1992224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1993224245bfSDavid Gibson  */
19947c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1995224245bfSDavid Gibson {
1996224245bfSDavid Gibson     int i;
1997224245bfSDavid Gibson 
19987c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
19997c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
20007c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
20017c150d6fSDavid Gibson                    machine->ram_size,
2002224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
20037c150d6fSDavid Gibson         return;
20047c150d6fSDavid Gibson     }
20057c150d6fSDavid Gibson 
20067c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
20077c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
20087c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
20097c150d6fSDavid Gibson                    machine->ram_size,
20107c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
20117c150d6fSDavid Gibson         return;
2012224245bfSDavid Gibson     }
2013224245bfSDavid Gibson 
2014224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
2015224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
20167c150d6fSDavid Gibson             error_setg(errp,
20177c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
20187c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
20197c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
2020224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
20217c150d6fSDavid Gibson             return;
2022224245bfSDavid Gibson         }
2023224245bfSDavid Gibson     }
2024224245bfSDavid Gibson }
2025224245bfSDavid Gibson 
2026535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
2027535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
2028535455fdSIgor Mammedov {
2029535455fdSIgor Mammedov     int index = id / smp_threads;
2030535455fdSIgor Mammedov 
2031535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
2032535455fdSIgor Mammedov         return NULL;
2033535455fdSIgor Mammedov     }
2034535455fdSIgor Mammedov     if (idx) {
2035535455fdSIgor Mammedov         *idx = index;
2036535455fdSIgor Mammedov     }
2037535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
2038535455fdSIgor Mammedov }
2039535455fdSIgor Mammedov 
20400c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
20410c86d0fdSDavid Gibson {
20420c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
20430c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
20440c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
20450c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
2046535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
2047535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
20480c86d0fdSDavid Gibson     int i;
20490c86d0fdSDavid Gibson 
20500c86d0fdSDavid Gibson     if (!type) {
20510c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
20520c86d0fdSDavid Gibson         exit(1);
20530c86d0fdSDavid Gibson     }
20540c86d0fdSDavid Gibson 
2055535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
2056c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
20570c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
20580c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
20590c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
20600c86d0fdSDavid Gibson             exit(1);
20610c86d0fdSDavid Gibson         }
20620c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
20630c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
20640c86d0fdSDavid Gibson                          max_cpus, smp_threads);
20650c86d0fdSDavid Gibson             exit(1);
20660c86d0fdSDavid Gibson         }
20670c86d0fdSDavid Gibson     } else {
20680c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
20690c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
20700c86d0fdSDavid Gibson             exit(1);
20710c86d0fdSDavid Gibson         }
2072535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
20730c86d0fdSDavid Gibson     }
20740c86d0fdSDavid Gibson 
2075535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
20760c86d0fdSDavid Gibson         int core_id = i * smp_threads;
20770c86d0fdSDavid Gibson 
2078c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
20792d335818SDavid Gibson             spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU,
20800c86d0fdSDavid Gibson                                    (core_id / smp_threads) * smt);
20810c86d0fdSDavid Gibson         }
20820c86d0fdSDavid Gibson 
2083535455fdSIgor Mammedov         if (i < boot_cores_nr) {
20840c86d0fdSDavid Gibson             Object *core  = object_new(type);
20850c86d0fdSDavid Gibson             int nr_threads = smp_threads;
20860c86d0fdSDavid Gibson 
20870c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
20880c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
20890c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
20900c86d0fdSDavid Gibson             }
20910c86d0fdSDavid Gibson 
20920c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
20930c86d0fdSDavid Gibson                                     &error_fatal);
20940c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
20950c86d0fdSDavid Gibson                                     &error_fatal);
20960c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
20970c86d0fdSDavid Gibson         }
20980c86d0fdSDavid Gibson     }
20990c86d0fdSDavid Gibson     g_free(type);
21000c86d0fdSDavid Gibson }
21010c86d0fdSDavid Gibson 
210253018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
21033ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
210453018216SPaolo Bonzini {
210528e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
2106224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
21073ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
21083ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
210953018216SPaolo Bonzini     PCIHostState *phb;
211053018216SPaolo Bonzini     int i;
211153018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
211253018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
2113658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
2114658fa66bSAlexey Kardashevskiy     void *rma = NULL;
211553018216SPaolo Bonzini     hwaddr rma_alloc_size;
2116b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
2117b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
211853018216SPaolo Bonzini     char *filename;
211953018216SPaolo Bonzini 
2120226419d6SMichael S. Tsirkin     msi_nonbroken = true;
212153018216SPaolo Bonzini 
212253018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
21230cffce56SDavid Gibson     QTAILQ_INIT(&spapr->pending_dimm_unplugs);
212453018216SPaolo Bonzini 
212553018216SPaolo Bonzini     /* Allocate RMA if necessary */
2126658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
212753018216SPaolo Bonzini 
212853018216SPaolo Bonzini     if (rma_alloc_size == -1) {
2129730fce59SThomas Huth         error_report("Unable to create RMA");
213053018216SPaolo Bonzini         exit(1);
213153018216SPaolo Bonzini     }
213253018216SPaolo Bonzini 
2133c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
213453018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
213553018216SPaolo Bonzini     } else {
2136c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
213753018216SPaolo Bonzini 
213853018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
213953018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
214053018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
214153018216SPaolo Bonzini          *
214253018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
214353018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
214453018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
214553018216SPaolo Bonzini          * isn't determined yet.
214653018216SPaolo Bonzini          */
214753018216SPaolo Bonzini         if (kvm_enabled()) {
214853018216SPaolo Bonzini             spapr->vrma_adjust = 1;
214953018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
215053018216SPaolo Bonzini         }
2151912acdf4SBenjamin Herrenschmidt 
2152912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
2153912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
2154912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
2155912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
2156912acdf4SBenjamin Herrenschmidt          */
2157912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
215853018216SPaolo Bonzini     }
215953018216SPaolo Bonzini 
2160c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
2161d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2162c4177479SAlexey Kardashevskiy                      spapr->rma_size);
2163c4177479SAlexey Kardashevskiy         exit(1);
2164c4177479SAlexey Kardashevskiy     }
2165c4177479SAlexey Kardashevskiy 
2166b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2167b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
216853018216SPaolo Bonzini 
21697b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
217071cd4dacSCédric Le Goater     xics_system_init(machine, XICS_IRQS_SPAPR, &error_fatal);
21717b565160SDavid Gibson 
2172facdb8b6SMichael Roth     /* Set up containers for ibm,client-set-architecture negotiated options */
2173facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2174facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2175facdb8b6SMichael Roth 
2176224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2177facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
21787c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2179224245bfSDavid Gibson     }
2180224245bfSDavid Gibson 
2181417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2182545d6e2bSSuraj Jitindar Singh     if (!kvm_enabled() || kvmppc_has_cap_mmu_radix()) {
2183545d6e2bSSuraj Jitindar Singh         /* KVM and TCG always allow GTSE with radix... */
21849fb4541fSSam Bobroff         spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
21859fb4541fSSam Bobroff     }
21869fb4541fSSam Bobroff     /* ... but not with hash (currently). */
2187417ece33SMichael Roth 
2188ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2189ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2190ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2191ffbb1705SMichael Roth     }
2192ffbb1705SMichael Roth 
219353018216SPaolo Bonzini     /* init CPUs */
219419fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
21953daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
219653018216SPaolo Bonzini     }
219794a94e4cSBharata B Rao 
21987843c0d6SDavid Gibson     spapr_cpu_parse_features(spapr);
2199e703d2f7SGreg Kurz 
22000c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
220153018216SPaolo Bonzini 
2202026bfd89SDavid Gibson     if (kvm_enabled()) {
2203026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2204026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2205ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
22065145ad4fSNathan Whitehorn 
22075145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
22085145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2209026bfd89SDavid Gibson     }
2210026bfd89SDavid Gibson 
221153018216SPaolo Bonzini     /* allocate RAM */
2212f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2213fb164994SDavid Gibson                                          machine->ram_size);
2214f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
221553018216SPaolo Bonzini 
2216658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2217658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2218658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2219658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2220658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2221658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2222658fa66bSAlexey Kardashevskiy     }
2223658fa66bSAlexey Kardashevskiy 
22244a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
22254a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
22264a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
222771c9a3ddSBharata B Rao         /*
222871c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
222971c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
223071c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
223171c9a3ddSBharata B Rao          */
223271c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
223371c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
22344a1c9cf0SBharata B Rao 
223571c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
223671c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
223771c9a3ddSBharata B Rao         }
223871c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2239d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2240d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
224171c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2242d54e4d76SDavid Gibson             exit(1);
22434a1c9cf0SBharata B Rao         }
22444a1c9cf0SBharata B Rao 
22454a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
22464a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
22474a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
22484a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
22494a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
22504a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
22514a1c9cf0SBharata B Rao     }
22524a1c9cf0SBharata B Rao 
2253224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2254224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2255224245bfSDavid Gibson     }
2256224245bfSDavid Gibson 
225753018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
22584c56440dSStefan Weil     if (!filename) {
2259730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
22604c56440dSStefan Weil         exit(1);
22614c56440dSStefan Weil     }
2262b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
22638afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
22648afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
22658afc22a2SZhou Jie         exit(1);
22668afc22a2SZhou Jie     }
2267b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2268b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2269730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
227053018216SPaolo Bonzini         exit(1);
227153018216SPaolo Bonzini     }
227253018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2273730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
22742f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
227553018216SPaolo Bonzini         exit(1);
227653018216SPaolo Bonzini     }
227753018216SPaolo Bonzini     g_free(filename);
227853018216SPaolo Bonzini 
2279ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
228053018216SPaolo Bonzini     spapr_events_init(spapr);
228153018216SPaolo Bonzini 
228212f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
228328df36a1SDavid Gibson     spapr_rtc_create(spapr);
228412f42174SDavid Gibson 
228553018216SPaolo Bonzini     /* Set up VIO bus */
228653018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
228753018216SPaolo Bonzini 
228853018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
228953018216SPaolo Bonzini         if (serial_hds[i]) {
229053018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
229153018216SPaolo Bonzini         }
229253018216SPaolo Bonzini     }
229353018216SPaolo Bonzini 
229453018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
229553018216SPaolo Bonzini     spapr_create_nvram(spapr);
229653018216SPaolo Bonzini 
229753018216SPaolo Bonzini     /* Set up PCI */
229853018216SPaolo Bonzini     spapr_pci_rtas_init();
229953018216SPaolo Bonzini 
230089dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
230153018216SPaolo Bonzini 
230253018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
230353018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
230453018216SPaolo Bonzini 
230553018216SPaolo Bonzini         if (!nd->model) {
230653018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
230753018216SPaolo Bonzini         }
230853018216SPaolo Bonzini 
230953018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
231053018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
231153018216SPaolo Bonzini         } else {
231229b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
231353018216SPaolo Bonzini         }
231453018216SPaolo Bonzini     }
231553018216SPaolo Bonzini 
231653018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
231753018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
231853018216SPaolo Bonzini     }
231953018216SPaolo Bonzini 
232053018216SPaolo Bonzini     /* Graphics */
232114c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
232253018216SPaolo Bonzini         spapr->has_graphics = true;
2323c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
232453018216SPaolo Bonzini     }
232553018216SPaolo Bonzini 
23264ee9ced9SMarcel Apfelbaum     if (machine->usb) {
232757040d45SThomas Huth         if (smc->use_ohci_by_default) {
232853018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
232957040d45SThomas Huth         } else {
233057040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
233157040d45SThomas Huth         }
2332c86580b8SMarkus Armbruster 
233353018216SPaolo Bonzini         if (spapr->has_graphics) {
2334c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2335c86580b8SMarkus Armbruster 
2336c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2337c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
233853018216SPaolo Bonzini         }
233953018216SPaolo Bonzini     }
234053018216SPaolo Bonzini 
234153018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2342d54e4d76SDavid Gibson         error_report(
2343d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2344d54e4d76SDavid Gibson             MIN_RMA_SLOF);
234553018216SPaolo Bonzini         exit(1);
234653018216SPaolo Bonzini     }
234753018216SPaolo Bonzini 
234853018216SPaolo Bonzini     if (kernel_filename) {
234953018216SPaolo Bonzini         uint64_t lowaddr = 0;
235053018216SPaolo Bonzini 
2351a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2352a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2353a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2354a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2355a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2356a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2357a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
23587ef295eaSPeter Crosthwaite                                           0, 0);
2359a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
236016457e7fSBenjamin Herrenschmidt         }
2361a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2362a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2363a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
236453018216SPaolo Bonzini             exit(1);
236553018216SPaolo Bonzini         }
236653018216SPaolo Bonzini 
236753018216SPaolo Bonzini         /* load initrd */
236853018216SPaolo Bonzini         if (initrd_filename) {
236953018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
237053018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
237153018216SPaolo Bonzini              */
2372a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2373a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2374a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2375a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2376a19f7fb0SDavid Gibson                                                      load_limit
2377a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2378a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2379d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
238053018216SPaolo Bonzini                              initrd_filename);
238153018216SPaolo Bonzini                 exit(1);
238253018216SPaolo Bonzini             }
238353018216SPaolo Bonzini         }
238453018216SPaolo Bonzini     }
238553018216SPaolo Bonzini 
23868e7ea787SAndreas Färber     if (bios_name == NULL) {
23878e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
23888e7ea787SAndreas Färber     }
23898e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
23904c56440dSStefan Weil     if (!filename) {
239168fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
23924c56440dSStefan Weil         exit(1);
23934c56440dSStefan Weil     }
239453018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
239568fea5a0SThomas Huth     if (fw_size <= 0) {
239668fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
239753018216SPaolo Bonzini         exit(1);
239853018216SPaolo Bonzini     }
239953018216SPaolo Bonzini     g_free(filename);
240053018216SPaolo Bonzini 
240128e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
240228e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
240328e02042SDavid Gibson      * which predated MachineState but had a similar function */
24044be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
24054be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
24064be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
24074be21d56SDavid Gibson 
24085b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
240942043e4fSLaurent Vivier 
241042043e4fSLaurent Vivier     if (kvm_enabled()) {
24113dc410aeSAlexey Kardashevskiy         /* to stop and start vmclock */
241242043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
241342043e4fSLaurent Vivier                                          &spapr->tb);
24143dc410aeSAlexey Kardashevskiy 
24153dc410aeSAlexey Kardashevskiy         kvmppc_spapr_enable_inkernel_multitce();
241642043e4fSLaurent Vivier     }
241753018216SPaolo Bonzini }
241853018216SPaolo Bonzini 
2419135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2420135a129aSAneesh Kumar K.V {
2421135a129aSAneesh Kumar K.V     if (!vm_type) {
2422135a129aSAneesh Kumar K.V         return 0;
2423135a129aSAneesh Kumar K.V     }
2424135a129aSAneesh Kumar K.V 
2425135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2426135a129aSAneesh Kumar K.V         return 1;
2427135a129aSAneesh Kumar K.V     }
2428135a129aSAneesh Kumar K.V 
2429135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2430135a129aSAneesh Kumar K.V         return 2;
2431135a129aSAneesh Kumar K.V     }
2432135a129aSAneesh Kumar K.V 
2433135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2434135a129aSAneesh Kumar K.V     exit(1);
2435135a129aSAneesh Kumar K.V }
2436135a129aSAneesh Kumar K.V 
243771461b0fSAlexey Kardashevskiy /*
2438627b84f4SGonglei  * Implementation of an interface to adjust firmware path
243971461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
244071461b0fSAlexey Kardashevskiy  */
244171461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
244271461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
244371461b0fSAlexey Kardashevskiy {
244471461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
244571461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
244671461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
244771461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
2448c4e13492SFelipe Franciosi     VHostSCSICommon *vsc = CAST(VHostSCSICommon, dev, TYPE_VHOST_SCSI_COMMON);
244971461b0fSAlexey Kardashevskiy 
245071461b0fSAlexey Kardashevskiy     if (d) {
245171461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
245271461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
245371461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
245471461b0fSAlexey Kardashevskiy 
245571461b0fSAlexey Kardashevskiy         if (spapr) {
245671461b0fSAlexey Kardashevskiy             /*
245771461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
245871461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
245971461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
246071461b0fSAlexey Kardashevskiy              */
246171461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
246271461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
246371461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
246471461b0fSAlexey Kardashevskiy         } else if (virtio) {
246571461b0fSAlexey Kardashevskiy             /*
246671461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
246771461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
246871461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
246971461b0fSAlexey Kardashevskiy              * the actual binding is:
247071461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
247171461b0fSAlexey Kardashevskiy              */
247271461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
247371461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
247471461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
247571461b0fSAlexey Kardashevskiy         } else if (usb) {
247671461b0fSAlexey Kardashevskiy             /*
247771461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
247871461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
247971461b0fSAlexey Kardashevskiy              */
248071461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
248171461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
248271461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
248371461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
248471461b0fSAlexey Kardashevskiy         }
248571461b0fSAlexey Kardashevskiy     }
248671461b0fSAlexey Kardashevskiy 
2487b99260ebSThomas Huth     /*
2488b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2489b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2490b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2491b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2492b99260ebSThomas Huth      */
2493b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2494b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2495b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2496b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2497b99260ebSThomas Huth         }
2498b99260ebSThomas Huth     }
2499b99260ebSThomas Huth 
250071461b0fSAlexey Kardashevskiy     if (phb) {
250171461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
250271461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
250371461b0fSAlexey Kardashevskiy     }
250471461b0fSAlexey Kardashevskiy 
2505c4e13492SFelipe Franciosi     if (vsc) {
2506c4e13492SFelipe Franciosi         /* Same logic as virtio above */
2507c4e13492SFelipe Franciosi         unsigned id = 0x1000000 | (vsc->target << 16) | vsc->lun;
2508c4e13492SFelipe Franciosi         return g_strdup_printf("disk@%"PRIX64, (uint64_t)id << 32);
2509c4e13492SFelipe Franciosi     }
2510c4e13492SFelipe Franciosi 
25114871dd4cSThomas Huth     if (g_str_equal("pci-bridge", qdev_fw_name(dev))) {
25124871dd4cSThomas Huth         /* SLOF uses "pci" instead of "pci-bridge" for PCI bridges */
25134871dd4cSThomas Huth         PCIDevice *pcidev = CAST(PCIDevice, dev, TYPE_PCI_DEVICE);
25144871dd4cSThomas Huth         return g_strdup_printf("pci@%x", PCI_SLOT(pcidev->devfn));
25154871dd4cSThomas Huth     }
25164871dd4cSThomas Huth 
251771461b0fSAlexey Kardashevskiy     return NULL;
251871461b0fSAlexey Kardashevskiy }
251971461b0fSAlexey Kardashevskiy 
252023825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
252123825581SEduardo Habkost {
252228e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
252323825581SEduardo Habkost 
252428e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
252523825581SEduardo Habkost }
252623825581SEduardo Habkost 
252723825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
252823825581SEduardo Habkost {
252928e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
253023825581SEduardo Habkost 
253128e02042SDavid Gibson     g_free(spapr->kvm_type);
253228e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
253323825581SEduardo Habkost }
253423825581SEduardo Habkost 
2535f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2536f6229214SMichael Roth {
2537f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2538f6229214SMichael Roth 
2539f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2540f6229214SMichael Roth }
2541f6229214SMichael Roth 
2542f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2543f6229214SMichael Roth                                             Error **errp)
2544f6229214SMichael Roth {
2545f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2546f6229214SMichael Roth 
2547f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2548f6229214SMichael Roth }
2549f6229214SMichael Roth 
255023825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
255123825581SEduardo Habkost {
2552715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2553715c5407SDavid Gibson 
2554715c5407SDavid Gibson     spapr->htab_fd = -1;
2555f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
255623825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
255723825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
255849d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
255949d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
256049d2e648SMarcel Apfelbaum                                     NULL);
2561f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2562f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2563f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2564f6229214SMichael Roth                             NULL);
2565f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2566f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2567f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2568f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2569f6229214SMichael Roth                                     NULL);
25707843c0d6SDavid Gibson 
25717843c0d6SDavid Gibson     ppc_compat_add_property(obj, "max-cpu-compat", &spapr->max_compat_pvr,
25727843c0d6SDavid Gibson                             "Maximum permitted CPU compatibility mode",
25737843c0d6SDavid Gibson                             &error_fatal);
257423825581SEduardo Habkost }
257523825581SEduardo Habkost 
257687bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
257787bbdd9cSDavid Gibson {
257887bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
257987bbdd9cSDavid Gibson 
258087bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
258187bbdd9cSDavid Gibson }
258287bbdd9cSDavid Gibson 
25831c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
258434316482SAlexey Kardashevskiy {
258534316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
258634316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
258734316482SAlexey Kardashevskiy }
258834316482SAlexey Kardashevskiy 
258934316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
259034316482SAlexey Kardashevskiy {
259134316482SAlexey Kardashevskiy     CPUState *cs;
259234316482SAlexey Kardashevskiy 
259334316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
25941c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
259534316482SAlexey Kardashevskiy     }
259634316482SAlexey Kardashevskiy }
259734316482SAlexey Kardashevskiy 
259879b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
259979b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
260079b78a6bSMichael Roth                            Error **errp)
2601c20d332aSBharata B Rao {
2602c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2603c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2604c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2605c20d332aSBharata B Rao     void *fdt;
260679b78a6bSMichael Roth     uint64_t addr = addr_start;
2607160bb678SGreg Kurz     Error *local_err = NULL;
2608c20d332aSBharata B Rao 
2609c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2610fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
2611c20d332aSBharata B Rao                               addr / SPAPR_MEMORY_BLOCK_SIZE);
2612c20d332aSBharata B Rao         g_assert(drc);
2613c20d332aSBharata B Rao 
2614c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2615c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2616c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2617c20d332aSBharata B Rao 
2618160bb678SGreg Kurz         spapr_drc_attach(drc, dev, fdt, fdt_offset, &local_err);
2619160bb678SGreg Kurz         if (local_err) {
2620160bb678SGreg Kurz             while (addr > addr_start) {
2621160bb678SGreg Kurz                 addr -= SPAPR_MEMORY_BLOCK_SIZE;
2622160bb678SGreg Kurz                 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
2623160bb678SGreg Kurz                                       addr / SPAPR_MEMORY_BLOCK_SIZE);
2624160bb678SGreg Kurz                 spapr_drc_detach(drc, dev, NULL);
2625160bb678SGreg Kurz             }
2626160bb678SGreg Kurz             g_free(fdt);
2627160bb678SGreg Kurz             error_propagate(errp, local_err);
2628160bb678SGreg Kurz             return;
2629160bb678SGreg Kurz         }
2630c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2631c20d332aSBharata B Rao     }
26325dd5238cSJianjun Duan     /* send hotplug notification to the
26335dd5238cSJianjun Duan      * guest only in case of hotplugged memory
26345dd5238cSJianjun Duan      */
26355dd5238cSJianjun Duan     if (dev->hotplugged) {
263679b78a6bSMichael Roth         if (dedicated_hp_event_source) {
2637fbf55397SDavid Gibson             drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
263879b78a6bSMichael Roth                                   addr_start / SPAPR_MEMORY_BLOCK_SIZE);
263979b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
264079b78a6bSMichael Roth                                                    nr_lmbs,
26410b55aa91SDavid Gibson                                                    spapr_drc_index(drc));
264279b78a6bSMichael Roth         } else {
264379b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
264479b78a6bSMichael Roth                                            nr_lmbs);
264579b78a6bSMichael Roth         }
2646c20d332aSBharata B Rao     }
26475dd5238cSJianjun Duan }
2648c20d332aSBharata B Rao 
2649c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2650c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2651c20d332aSBharata B Rao {
2652c20d332aSBharata B Rao     Error *local_err = NULL;
2653c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2654c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2655c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2656c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2657c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2658c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2659c20d332aSBharata B Rao     uint64_t addr;
2660df587133SThomas Huth 
2661d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2662c20d332aSBharata B Rao     if (local_err) {
2663c20d332aSBharata B Rao         goto out;
2664c20d332aSBharata B Rao     }
2665c20d332aSBharata B Rao 
26669ed442b8SMarc-André Lureau     addr = object_property_get_uint(OBJECT(dimm),
26679ed442b8SMarc-André Lureau                                     PC_DIMM_ADDR_PROP, &local_err);
2668c20d332aSBharata B Rao     if (local_err) {
2669160bb678SGreg Kurz         goto out_unplug;
2670c20d332aSBharata B Rao     }
2671c20d332aSBharata B Rao 
267279b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
267379b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
2674160bb678SGreg Kurz                    &local_err);
2675160bb678SGreg Kurz     if (local_err) {
2676160bb678SGreg Kurz         goto out_unplug;
2677160bb678SGreg Kurz     }
2678c20d332aSBharata B Rao 
2679160bb678SGreg Kurz     return;
2680160bb678SGreg Kurz 
2681160bb678SGreg Kurz out_unplug:
2682160bb678SGreg Kurz     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2683c20d332aSBharata B Rao out:
2684c20d332aSBharata B Rao     error_propagate(errp, local_err);
2685c20d332aSBharata B Rao }
2686c20d332aSBharata B Rao 
2687c871bc70SLaurent Vivier static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2688c871bc70SLaurent Vivier                                   Error **errp)
2689c871bc70SLaurent Vivier {
2690c871bc70SLaurent Vivier     PCDIMMDevice *dimm = PC_DIMM(dev);
2691c871bc70SLaurent Vivier     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2692c871bc70SLaurent Vivier     MemoryRegion *mr = ddc->get_memory_region(dimm);
2693c871bc70SLaurent Vivier     uint64_t size = memory_region_size(mr);
2694c871bc70SLaurent Vivier     char *mem_dev;
2695c871bc70SLaurent Vivier 
2696c871bc70SLaurent Vivier     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2697c871bc70SLaurent Vivier         error_setg(errp, "Hotplugged memory size must be a multiple of "
2698c871bc70SLaurent Vivier                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
2699c871bc70SLaurent Vivier         return;
2700c871bc70SLaurent Vivier     }
2701c871bc70SLaurent Vivier 
2702c871bc70SLaurent Vivier     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2703c871bc70SLaurent Vivier     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2704c871bc70SLaurent Vivier         error_setg(errp, "Memory backend has bad page size. "
2705c871bc70SLaurent Vivier                    "Use 'memory-backend-file' with correct mem-path.");
27068a9e0e7bSGreg Kurz         goto out;
2707c871bc70SLaurent Vivier     }
27088a9e0e7bSGreg Kurz 
27098a9e0e7bSGreg Kurz out:
27108a9e0e7bSGreg Kurz     g_free(mem_dev);
2711c871bc70SLaurent Vivier }
2712c871bc70SLaurent Vivier 
27130cffce56SDavid Gibson struct sPAPRDIMMState {
27140cffce56SDavid Gibson     PCDIMMDevice *dimm;
2715cf632463SBharata B Rao     uint32_t nr_lmbs;
27160cffce56SDavid Gibson     QTAILQ_ENTRY(sPAPRDIMMState) next;
27170cffce56SDavid Gibson };
27180cffce56SDavid Gibson 
27190cffce56SDavid Gibson static sPAPRDIMMState *spapr_pending_dimm_unplugs_find(sPAPRMachineState *s,
27200cffce56SDavid Gibson                                                        PCDIMMDevice *dimm)
27210cffce56SDavid Gibson {
27220cffce56SDavid Gibson     sPAPRDIMMState *dimm_state = NULL;
27230cffce56SDavid Gibson 
27240cffce56SDavid Gibson     QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
27250cffce56SDavid Gibson         if (dimm_state->dimm == dimm) {
27260cffce56SDavid Gibson             break;
27270cffce56SDavid Gibson         }
27280cffce56SDavid Gibson     }
27290cffce56SDavid Gibson     return dimm_state;
27300cffce56SDavid Gibson }
27310cffce56SDavid Gibson 
27320cffce56SDavid Gibson static void spapr_pending_dimm_unplugs_add(sPAPRMachineState *spapr,
27330cffce56SDavid Gibson                                            sPAPRDIMMState *dimm_state)
27340cffce56SDavid Gibson {
27350cffce56SDavid Gibson     g_assert(!spapr_pending_dimm_unplugs_find(spapr, dimm_state->dimm));
27360cffce56SDavid Gibson     QTAILQ_INSERT_HEAD(&spapr->pending_dimm_unplugs, dimm_state, next);
27370cffce56SDavid Gibson }
27380cffce56SDavid Gibson 
27390cffce56SDavid Gibson static void spapr_pending_dimm_unplugs_remove(sPAPRMachineState *spapr,
27400cffce56SDavid Gibson                                               sPAPRDIMMState *dimm_state)
27410cffce56SDavid Gibson {
27420cffce56SDavid Gibson     QTAILQ_REMOVE(&spapr->pending_dimm_unplugs, dimm_state, next);
27430cffce56SDavid Gibson     g_free(dimm_state);
27440cffce56SDavid Gibson }
2745cf632463SBharata B Rao 
274616ee9980SDaniel Henrique Barboza static sPAPRDIMMState *spapr_recover_pending_dimm_state(sPAPRMachineState *ms,
274716ee9980SDaniel Henrique Barboza                                                         PCDIMMDevice *dimm)
274816ee9980SDaniel Henrique Barboza {
274916ee9980SDaniel Henrique Barboza     sPAPRDRConnector *drc;
275016ee9980SDaniel Henrique Barboza     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
275116ee9980SDaniel Henrique Barboza     MemoryRegion *mr = ddc->get_memory_region(dimm);
275216ee9980SDaniel Henrique Barboza     uint64_t size = memory_region_size(mr);
275316ee9980SDaniel Henrique Barboza     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
275416ee9980SDaniel Henrique Barboza     uint32_t avail_lmbs = 0;
275516ee9980SDaniel Henrique Barboza     uint64_t addr_start, addr;
275616ee9980SDaniel Henrique Barboza     int i;
275716ee9980SDaniel Henrique Barboza     sPAPRDIMMState *ds;
275816ee9980SDaniel Henrique Barboza 
275916ee9980SDaniel Henrique Barboza     addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
276016ee9980SDaniel Henrique Barboza                                          &error_abort);
276116ee9980SDaniel Henrique Barboza 
276216ee9980SDaniel Henrique Barboza     addr = addr_start;
276316ee9980SDaniel Henrique Barboza     for (i = 0; i < nr_lmbs; i++) {
2764fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
276516ee9980SDaniel Henrique Barboza                               addr / SPAPR_MEMORY_BLOCK_SIZE);
276616ee9980SDaniel Henrique Barboza         g_assert(drc);
2767454b580aSDavid Gibson         if (drc->dev) {
276816ee9980SDaniel Henrique Barboza             avail_lmbs++;
276916ee9980SDaniel Henrique Barboza         }
277016ee9980SDaniel Henrique Barboza         addr += SPAPR_MEMORY_BLOCK_SIZE;
277116ee9980SDaniel Henrique Barboza     }
277216ee9980SDaniel Henrique Barboza 
277316ee9980SDaniel Henrique Barboza     ds = g_malloc0(sizeof(sPAPRDIMMState));
277416ee9980SDaniel Henrique Barboza     ds->nr_lmbs = avail_lmbs;
277516ee9980SDaniel Henrique Barboza     ds->dimm = dimm;
277616ee9980SDaniel Henrique Barboza     spapr_pending_dimm_unplugs_add(ms, ds);
277716ee9980SDaniel Henrique Barboza     return ds;
277816ee9980SDaniel Henrique Barboza }
277916ee9980SDaniel Henrique Barboza 
278031834723SDaniel Henrique Barboza /* Callback to be called during DRC release. */
278131834723SDaniel Henrique Barboza void spapr_lmb_release(DeviceState *dev)
2782cf632463SBharata B Rao {
27830cffce56SDavid Gibson     HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
27840cffce56SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_ctrl);
27850cffce56SDavid Gibson     sPAPRDIMMState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
2786cf632463SBharata B Rao 
278716ee9980SDaniel Henrique Barboza     /* This information will get lost if a migration occurs
278816ee9980SDaniel Henrique Barboza      * during the unplug process. In this case recover it. */
278916ee9980SDaniel Henrique Barboza     if (ds == NULL) {
279016ee9980SDaniel Henrique Barboza         ds = spapr_recover_pending_dimm_state(spapr, PC_DIMM(dev));
2791454b580aSDavid Gibson         /* The DRC being examined by the caller at least must be counted */
2792454b580aSDavid Gibson         g_assert(ds->nr_lmbs);
279316ee9980SDaniel Henrique Barboza     }
2794454b580aSDavid Gibson 
2795454b580aSDavid Gibson     if (--ds->nr_lmbs) {
2796cf632463SBharata B Rao         return;
2797cf632463SBharata B Rao     }
2798cf632463SBharata B Rao 
27990cffce56SDavid Gibson     spapr_pending_dimm_unplugs_remove(spapr, ds);
2800cf632463SBharata B Rao 
2801cf632463SBharata B Rao     /*
2802cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
2803cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
2804cf632463SBharata B Rao      */
2805cf632463SBharata B Rao     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2806cf632463SBharata B Rao }
2807cf632463SBharata B Rao 
2808cf632463SBharata B Rao static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2809cf632463SBharata B Rao                                 Error **errp)
2810cf632463SBharata B Rao {
2811cf632463SBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2812cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2813cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2814cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2815cf632463SBharata B Rao 
2816cf632463SBharata B Rao     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2817cf632463SBharata B Rao     object_unparent(OBJECT(dev));
2818cf632463SBharata B Rao }
2819cf632463SBharata B Rao 
2820cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
2821cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
2822cf632463SBharata B Rao {
28230cffce56SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
2824cf632463SBharata B Rao     Error *local_err = NULL;
2825cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2826cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2827cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2828cf632463SBharata B Rao     uint64_t size = memory_region_size(mr);
28290cffce56SDavid Gibson     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
28300cffce56SDavid Gibson     uint64_t addr_start, addr;
28310cffce56SDavid Gibson     int i;
28320cffce56SDavid Gibson     sPAPRDRConnector *drc;
28330cffce56SDavid Gibson     sPAPRDIMMState *ds;
2834cf632463SBharata B Rao 
28359ed442b8SMarc-André Lureau     addr_start = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
28360cffce56SDavid Gibson                                          &local_err);
2837cf632463SBharata B Rao     if (local_err) {
2838cf632463SBharata B Rao         goto out;
2839cf632463SBharata B Rao     }
2840cf632463SBharata B Rao 
28410cffce56SDavid Gibson     ds = g_malloc0(sizeof(sPAPRDIMMState));
28420cffce56SDavid Gibson     ds->nr_lmbs = nr_lmbs;
28430cffce56SDavid Gibson     ds->dimm = dimm;
28440cffce56SDavid Gibson     spapr_pending_dimm_unplugs_add(spapr, ds);
28450cffce56SDavid Gibson 
28460cffce56SDavid Gibson     addr = addr_start;
28470cffce56SDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
2848fbf55397SDavid Gibson         drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
28490cffce56SDavid Gibson                               addr / SPAPR_MEMORY_BLOCK_SIZE);
28500cffce56SDavid Gibson         g_assert(drc);
28510cffce56SDavid Gibson 
28520be4e886SDavid Gibson         spapr_drc_detach(drc, dev, errp);
28530cffce56SDavid Gibson         addr += SPAPR_MEMORY_BLOCK_SIZE;
28540cffce56SDavid Gibson     }
28550cffce56SDavid Gibson 
2856fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
28570cffce56SDavid Gibson                           addr_start / SPAPR_MEMORY_BLOCK_SIZE);
28580cffce56SDavid Gibson     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
28590b55aa91SDavid Gibson                                               nr_lmbs, spapr_drc_index(drc));
2860cf632463SBharata B Rao out:
2861cf632463SBharata B Rao     error_propagate(errp, local_err);
2862cf632463SBharata B Rao }
2863cf632463SBharata B Rao 
286404d0ffbdSGreg Kurz static void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2865af81cf32SBharata B Rao                                            sPAPRMachineState *spapr)
2866af81cf32SBharata B Rao {
2867af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2868af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2869af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2870af81cf32SBharata B Rao     void *fdt;
2871af81cf32SBharata B Rao     int offset, fdt_size;
2872af81cf32SBharata B Rao     char *nodename;
2873af81cf32SBharata B Rao 
2874af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2875af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2876af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2877af81cf32SBharata B Rao 
2878af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2879af81cf32SBharata B Rao     g_free(nodename);
2880af81cf32SBharata B Rao 
2881af81cf32SBharata B Rao     *fdt_offset = offset;
2882af81cf32SBharata B Rao     return fdt;
2883af81cf32SBharata B Rao }
2884af81cf32SBharata B Rao 
2885115debf2SIgor Mammedov static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2886115debf2SIgor Mammedov                               Error **errp)
2887ff9006ddSIgor Mammedov {
2888535455fdSIgor Mammedov     MachineState *ms = MACHINE(qdev_get_machine());
288946f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
2890ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2891535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
2892ff9006ddSIgor Mammedov 
289346f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
289446f7afa3SGreg Kurz         sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
289546f7afa3SGreg Kurz         sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(cc));
289646f7afa3SGreg Kurz         const char *typename = object_class_get_name(scc->cpu_class);
289746f7afa3SGreg Kurz         size_t size = object_type_get_instance_size(typename);
289846f7afa3SGreg Kurz         int i;
289946f7afa3SGreg Kurz 
290046f7afa3SGreg Kurz         for (i = 0; i < cc->nr_threads; i++) {
290146f7afa3SGreg Kurz             CPUState *cs = CPU(sc->threads + i * size);
290246f7afa3SGreg Kurz 
290346f7afa3SGreg Kurz             pre_2_10_vmstate_register_dummy_icp(cs->cpu_index);
290446f7afa3SGreg Kurz         }
290546f7afa3SGreg Kurz     }
290646f7afa3SGreg Kurz 
290707572c06SGreg Kurz     assert(core_slot);
2908535455fdSIgor Mammedov     core_slot->cpu = NULL;
2909ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
2910ff9006ddSIgor Mammedov }
2911ff9006ddSIgor Mammedov 
291231834723SDaniel Henrique Barboza /* Callback to be called during DRC release. */
291331834723SDaniel Henrique Barboza void spapr_core_release(DeviceState *dev)
2914115debf2SIgor Mammedov {
2915115debf2SIgor Mammedov     HotplugHandler *hotplug_ctrl;
2916115debf2SIgor Mammedov 
2917115debf2SIgor Mammedov     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2918115debf2SIgor Mammedov     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2919115debf2SIgor Mammedov }
2920115debf2SIgor Mammedov 
2921115debf2SIgor Mammedov static
2922115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
2923ff9006ddSIgor Mammedov                                Error **errp)
2924ff9006ddSIgor Mammedov {
2925535455fdSIgor Mammedov     int index;
2926535455fdSIgor Mammedov     sPAPRDRConnector *drc;
2927ff9006ddSIgor Mammedov     Error *local_err = NULL;
2928535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2929535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
2930ff9006ddSIgor Mammedov 
2931535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
2932535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2933535455fdSIgor Mammedov                    cc->core_id);
2934535455fdSIgor Mammedov         return;
2935535455fdSIgor Mammedov     }
2936ff9006ddSIgor Mammedov     if (index == 0) {
2937ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
2938ff9006ddSIgor Mammedov         return;
2939ff9006ddSIgor Mammedov     }
2940ff9006ddSIgor Mammedov 
2941fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt);
2942ff9006ddSIgor Mammedov     g_assert(drc);
2943ff9006ddSIgor Mammedov 
29440be4e886SDavid Gibson     spapr_drc_detach(drc, dev, &local_err);
2945ff9006ddSIgor Mammedov     if (local_err) {
2946ff9006ddSIgor Mammedov         error_propagate(errp, local_err);
2947ff9006ddSIgor Mammedov         return;
2948ff9006ddSIgor Mammedov     }
2949ff9006ddSIgor Mammedov 
2950ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
2951ff9006ddSIgor Mammedov }
2952ff9006ddSIgor Mammedov 
2953ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2954ff9006ddSIgor Mammedov                             Error **errp)
2955ff9006ddSIgor Mammedov {
2956ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
2957ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
295846f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
2959ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
2960ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2961ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
2962ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
2963ff9006ddSIgor Mammedov     Error *local_err = NULL;
2964ff9006ddSIgor Mammedov     void *fdt = NULL;
2965ff9006ddSIgor Mammedov     int fdt_offset = 0;
2966ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
2967535455fdSIgor Mammedov     CPUArchId *core_slot;
2968535455fdSIgor Mammedov     int index;
2969ff9006ddSIgor Mammedov 
2970535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2971535455fdSIgor Mammedov     if (!core_slot) {
2972535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2973535455fdSIgor Mammedov                    cc->core_id);
2974535455fdSIgor Mammedov         return;
2975535455fdSIgor Mammedov     }
2976fbf55397SDavid Gibson     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index * smt);
2977ff9006ddSIgor Mammedov 
2978c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
2979ff9006ddSIgor Mammedov 
2980ff9006ddSIgor Mammedov     fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
2981ff9006ddSIgor Mammedov 
2982ff9006ddSIgor Mammedov     if (drc) {
29835c1da812SDavid Gibson         spapr_drc_attach(drc, dev, fdt, fdt_offset, &local_err);
2984ff9006ddSIgor Mammedov         if (local_err) {
2985ff9006ddSIgor Mammedov             g_free(fdt);
2986ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
2987ff9006ddSIgor Mammedov             return;
2988ff9006ddSIgor Mammedov         }
2989ff9006ddSIgor Mammedov     }
2990ff9006ddSIgor Mammedov 
2991ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2992ff9006ddSIgor Mammedov         /*
2993ff9006ddSIgor Mammedov          * Send hotplug notification interrupt to the guest only in case
2994ff9006ddSIgor Mammedov          * of hotplugged CPUs.
2995ff9006ddSIgor Mammedov          */
2996ff9006ddSIgor Mammedov         spapr_hotplug_req_add_by_index(drc);
2997ff9006ddSIgor Mammedov     }
2998535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
299946f7afa3SGreg Kurz 
300046f7afa3SGreg Kurz     if (smc->pre_2_10_has_unused_icps) {
300146f7afa3SGreg Kurz         sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(cc));
300246f7afa3SGreg Kurz         const char *typename = object_class_get_name(scc->cpu_class);
300346f7afa3SGreg Kurz         size_t size = object_type_get_instance_size(typename);
300446f7afa3SGreg Kurz         int i;
300546f7afa3SGreg Kurz 
300646f7afa3SGreg Kurz         for (i = 0; i < cc->nr_threads; i++) {
300746f7afa3SGreg Kurz             sPAPRCPUCore *sc = SPAPR_CPU_CORE(dev);
300846f7afa3SGreg Kurz             void *obj = sc->threads + i * size;
300946f7afa3SGreg Kurz 
301046f7afa3SGreg Kurz             cs = CPU(obj);
301146f7afa3SGreg Kurz             pre_2_10_vmstate_unregister_dummy_icp(cs->cpu_index);
301246f7afa3SGreg Kurz         }
301346f7afa3SGreg Kurz     }
3014ff9006ddSIgor Mammedov }
3015ff9006ddSIgor Mammedov 
3016ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3017ff9006ddSIgor Mammedov                                 Error **errp)
3018ff9006ddSIgor Mammedov {
3019ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
3020ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
3021ff9006ddSIgor Mammedov     Error *local_err = NULL;
3022ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
3023ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
3024ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
3025535455fdSIgor Mammedov     CPUArchId *core_slot;
3026535455fdSIgor Mammedov     int index;
3027ff9006ddSIgor Mammedov 
3028c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
3029ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
3030ff9006ddSIgor Mammedov         goto out;
3031ff9006ddSIgor Mammedov     }
3032ff9006ddSIgor Mammedov 
3033ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
3034ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
3035ff9006ddSIgor Mammedov         goto out;
3036ff9006ddSIgor Mammedov     }
3037ff9006ddSIgor Mammedov 
3038ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
3039ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
3040ff9006ddSIgor Mammedov         goto out;
3041ff9006ddSIgor Mammedov     }
3042ff9006ddSIgor Mammedov 
3043459264efSDavid Gibson     /*
3044459264efSDavid Gibson      * In general we should have homogeneous threads-per-core, but old
3045459264efSDavid Gibson      * (pre hotplug support) machine types allow the last core to have
3046459264efSDavid Gibson      * reduced threads as a compatibility hack for when we allowed
3047459264efSDavid Gibson      * total vcpus not a multiple of threads-per-core.
3048459264efSDavid Gibson      */
3049459264efSDavid Gibson     if (mc->has_hotpluggable_cpus && (cc->nr_threads != smp_threads)) {
30508149e299SDavid Gibson         error_setg(errp, "invalid nr-threads %d, must be %d",
30518149e299SDavid Gibson                    cc->nr_threads, smp_threads);
30528149e299SDavid Gibson         return;
30538149e299SDavid Gibson     }
30548149e299SDavid Gibson 
3055535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3056535455fdSIgor Mammedov     if (!core_slot) {
3057ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
3058ff9006ddSIgor Mammedov         goto out;
3059ff9006ddSIgor Mammedov     }
3060ff9006ddSIgor Mammedov 
3061535455fdSIgor Mammedov     if (core_slot->cpu) {
3062ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
3063ff9006ddSIgor Mammedov         goto out;
3064ff9006ddSIgor Mammedov     }
3065ff9006ddSIgor Mammedov 
3066a0ceb640SIgor Mammedov     numa_cpu_pre_plug(core_slot, dev, &local_err);
30670b8497f0SIgor Mammedov 
3068ff9006ddSIgor Mammedov out:
3069ff9006ddSIgor Mammedov     g_free(base_core_type);
3070ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
3071ff9006ddSIgor Mammedov }
3072ff9006ddSIgor Mammedov 
3073c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
3074c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
3075c20d332aSBharata B Rao {
3076c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
3077c20d332aSBharata B Rao 
3078c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3079b556854bSBharata B Rao         int node;
3080c20d332aSBharata B Rao 
3081c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
3082c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
3083c20d332aSBharata B Rao             return;
3084c20d332aSBharata B Rao         }
30859ed442b8SMarc-André Lureau         node = object_property_get_uint(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
3086c20d332aSBharata B Rao         if (*errp) {
3087c20d332aSBharata B Rao             return;
3088c20d332aSBharata B Rao         }
30891a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
30901a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
30911a5512bbSGonglei             return;
30921a5512bbSGonglei         }
3093c20d332aSBharata B Rao 
3094b556854bSBharata B Rao         /*
3095b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
3096b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
3097b556854bSBharata B Rao          * to the first node that has some memory. This causes two
3098b556854bSBharata B Rao          * unexpected behaviours for the user.
3099b556854bSBharata B Rao          *
3100b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
3101b556854bSBharata B Rao          *   specified.
3102b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
3103b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
3104b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
3105b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
3106b556854bSBharata B Rao          *
3107b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
3108b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
3109b556854bSBharata B Rao          */
3110b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
3111b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
3112b556854bSBharata B Rao                        node);
3113b556854bSBharata B Rao             return;
3114b556854bSBharata B Rao         }
3115b556854bSBharata B Rao 
3116c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
3117af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3118af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
3119c20d332aSBharata B Rao     }
3120c20d332aSBharata B Rao }
3121c20d332aSBharata B Rao 
3122c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
3123c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
3124c20d332aSBharata B Rao {
3125cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
31263c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
31276f4b5c3eSBharata B Rao 
3128c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3129cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3130cf632463SBharata B Rao             spapr_memory_unplug(hotplug_dev, dev, errp);
3131cf632463SBharata B Rao         } else {
3132cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
3133cf632463SBharata B Rao         }
3134cf632463SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3135c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
3136cf632463SBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
3137cf632463SBharata B Rao             return;
3138cf632463SBharata B Rao         }
3139cf632463SBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
3140cf632463SBharata B Rao     }
3141cf632463SBharata B Rao }
3142cf632463SBharata B Rao 
3143cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
3144cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
3145cf632463SBharata B Rao {
3146cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
3147cf632463SBharata B Rao     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
3148cf632463SBharata B Rao 
3149cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3150cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3151cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
3152cf632463SBharata B Rao         } else {
3153cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
3154cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
3155cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
3156cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
3157cf632463SBharata B Rao              * eventually handled by the guest after boot
3158cf632463SBharata B Rao              */
3159cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
3160cf632463SBharata B Rao         }
31616f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3162c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
31636f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
31646f4b5c3eSBharata B Rao             return;
31656f4b5c3eSBharata B Rao         }
3166115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
3167c20d332aSBharata B Rao     }
3168c20d332aSBharata B Rao }
3169c20d332aSBharata B Rao 
317094a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
317194a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
317294a94e4cSBharata B Rao {
3173c871bc70SLaurent Vivier     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3174c871bc70SLaurent Vivier         spapr_memory_pre_plug(hotplug_dev, dev, errp);
3175c871bc70SLaurent Vivier     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
317694a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
317794a94e4cSBharata B Rao     }
317894a94e4cSBharata B Rao }
317994a94e4cSBharata B Rao 
31807ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
3181c20d332aSBharata B Rao                                                  DeviceState *dev)
3182c20d332aSBharata B Rao {
318394a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
318494a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3185c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
3186c20d332aSBharata B Rao     }
3187c20d332aSBharata B Rao     return NULL;
3188c20d332aSBharata B Rao }
3189c20d332aSBharata B Rao 
3190ea089eebSIgor Mammedov static CpuInstanceProperties
3191ea089eebSIgor Mammedov spapr_cpu_index_to_props(MachineState *machine, unsigned cpu_index)
319220bb648dSDavid Gibson {
3193ea089eebSIgor Mammedov     CPUArchId *core_slot;
3194ea089eebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3195ea089eebSIgor Mammedov 
3196ea089eebSIgor Mammedov     /* make sure possible_cpu are intialized */
3197ea089eebSIgor Mammedov     mc->possible_cpu_arch_ids(machine);
3198ea089eebSIgor Mammedov     /* get CPU core slot containing thread that matches cpu_index */
3199ea089eebSIgor Mammedov     core_slot = spapr_find_cpu_slot(machine, cpu_index, NULL);
3200ea089eebSIgor Mammedov     assert(core_slot);
3201ea089eebSIgor Mammedov     return core_slot->props;
320220bb648dSDavid Gibson }
320320bb648dSDavid Gibson 
3204535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
3205535455fdSIgor Mammedov {
3206535455fdSIgor Mammedov     int i;
3207535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
3208535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3209535455fdSIgor Mammedov 
3210c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
3211535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
3212535455fdSIgor Mammedov     }
3213535455fdSIgor Mammedov     if (machine->possible_cpus) {
3214535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
3215535455fdSIgor Mammedov         return machine->possible_cpus;
3216535455fdSIgor Mammedov     }
3217535455fdSIgor Mammedov 
3218535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
3219535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
3220535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
3221535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
3222535455fdSIgor Mammedov         int core_id = i * smp_threads;
3223535455fdSIgor Mammedov 
3224f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
3225535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
3226535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
3227535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
3228ea089eebSIgor Mammedov 
3229ea089eebSIgor Mammedov         /* default distribution of CPUs over NUMA nodes */
3230ea089eebSIgor Mammedov         if (nb_numa_nodes) {
3231ea089eebSIgor Mammedov             /* preset values but do not enable them i.e. 'has_node_id = false',
3232ea089eebSIgor Mammedov              * numa init code will enable them later if manual mapping wasn't
3233ea089eebSIgor Mammedov              * present on CLI */
3234ea089eebSIgor Mammedov             machine->possible_cpus->cpus[i].props.node_id =
3235ea089eebSIgor Mammedov                 core_id / smp_threads / smp_cores % nb_numa_nodes;
3236ea089eebSIgor Mammedov         }
3237535455fdSIgor Mammedov     }
3238535455fdSIgor Mammedov     return machine->possible_cpus;
3239535455fdSIgor Mammedov }
3240535455fdSIgor Mammedov 
32416737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
3242daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
3243daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
32446737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
32456737d9adSDavid Gibson {
3246357d1e3bSDavid Gibson     /*
3247357d1e3bSDavid Gibson      * New-style PHB window placement.
3248357d1e3bSDavid Gibson      *
3249357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
3250357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
3251357d1e3bSDavid Gibson      * windows.
3252357d1e3bSDavid Gibson      *
3253357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
3254357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
3255357d1e3bSDavid Gibson      *
3256357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
3257357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
3258357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
3259357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
3260357d1e3bSDavid Gibson      */
32616737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
326225e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
326325e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
32646737d9adSDavid Gibson     int i;
32656737d9adSDavid Gibson 
3266357d1e3bSDavid Gibson     /* Sanity check natural alignments */
3267357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3268357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3269357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
3270357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
3271357d1e3bSDavid Gibson     /* Sanity check bounds */
327225e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
327325e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
327425e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
327525e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
32762efff1c0SDavid Gibson 
327725e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
327825e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
327925e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
32806737d9adSDavid Gibson         return;
32816737d9adSDavid Gibson     }
32826737d9adSDavid Gibson 
32836737d9adSDavid Gibson     *buid = base_buid + index;
32846737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
32856737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
32866737d9adSDavid Gibson     }
32876737d9adSDavid Gibson 
3288357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
3289357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
3290357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
32916737d9adSDavid Gibson }
32926737d9adSDavid Gibson 
32937844e12bSCédric Le Goater static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
32947844e12bSCédric Le Goater {
32957844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
32967844e12bSCédric Le Goater 
32977844e12bSCédric Le Goater     return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
32987844e12bSCédric Le Goater }
32997844e12bSCédric Le Goater 
33007844e12bSCédric Le Goater static void spapr_ics_resend(XICSFabric *dev)
33017844e12bSCédric Le Goater {
33027844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
33037844e12bSCédric Le Goater 
33047844e12bSCédric Le Goater     ics_resend(spapr->ics);
33057844e12bSCédric Le Goater }
33067844e12bSCédric Le Goater 
330706747ba6SCédric Le Goater static ICPState *spapr_icp_get(XICSFabric *xi, int cpu_dt_id)
3308b2fc59aaSCédric Le Goater {
33095bc8d26dSCédric Le Goater     PowerPCCPU *cpu = ppc_get_vcpu_by_dt_id(cpu_dt_id);
3310b2fc59aaSCédric Le Goater 
33115bc8d26dSCédric Le Goater     return cpu ? ICP(cpu->intc) : NULL;
3312b2fc59aaSCédric Le Goater }
3313b2fc59aaSCédric Le Goater 
33146449da45SCédric Le Goater static void spapr_pic_print_info(InterruptStatsProvider *obj,
33156449da45SCédric Le Goater                                  Monitor *mon)
33166449da45SCédric Le Goater {
33176449da45SCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
33185bc8d26dSCédric Le Goater     CPUState *cs;
33196449da45SCédric Le Goater 
33205bc8d26dSCédric Le Goater     CPU_FOREACH(cs) {
33215bc8d26dSCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
33225bc8d26dSCédric Le Goater 
33235bc8d26dSCédric Le Goater         icp_pic_print_info(ICP(cpu->intc), mon);
33246449da45SCédric Le Goater     }
33256449da45SCédric Le Goater 
33266449da45SCédric Le Goater     ics_pic_print_info(spapr->ics, mon);
33276449da45SCédric Le Goater }
33286449da45SCédric Le Goater 
332929ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
333053018216SPaolo Bonzini {
333129ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
3332224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
333371461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
333434316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
3335c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
33361d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
33377844e12bSCédric Le Goater     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
33386449da45SCédric Le Goater     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
333929ee3247SAlexey Kardashevskiy 
33400eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
3341fc9f38c3SDavid Gibson 
3342fc9f38c3SDavid Gibson     /*
3343fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
3344fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
3345fc9f38c3SDavid Gibson      * these details for backwards compatibility
3346fc9f38c3SDavid Gibson      */
3347958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
3348958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
3349958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
33506244bb7eSGreg Kurz     mc->max_cpus = 1024;
3351958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
33525b2128d2SAlexander Graf     mc->default_boot_order = "";
3353a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
3354958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
33559e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
3356e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
33577ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
335894a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
3359c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
3360c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
3361ea089eebSIgor Mammedov     mc->cpu_index_to_instance_props = spapr_cpu_index_to_props;
3362535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3363cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
336400b4fbe2SMarcel Apfelbaum 
3365fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
33663daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
3367c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
336871461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
336934316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
33706737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
33711d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3372e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3373e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3374e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3375e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
33769861bb3eSSuraj Jitindar Singh     vhc->get_patbe = spapr_get_patbe;
33777844e12bSCédric Le Goater     xic->ics_get = spapr_ics_get;
33787844e12bSCédric Le Goater     xic->ics_resend = spapr_ics_resend;
3379b2fc59aaSCédric Le Goater     xic->icp_get = spapr_icp_get;
33806449da45SCédric Le Goater     ispc->print_info = spapr_pic_print_info;
338155641213SLaurent Vivier     /* Force NUMA node memory size to be a multiple of
338255641213SLaurent Vivier      * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
338355641213SLaurent Vivier      * in which LMBs are represented and hot-added
338455641213SLaurent Vivier      */
338555641213SLaurent Vivier     mc->numa_mem_align_shift = 28;
338653018216SPaolo Bonzini }
338753018216SPaolo Bonzini 
338829ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
338929ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
339029ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
33914aee7362SDavid Gibson     .abstract      = true,
33926ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
339323825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
339487bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3395183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
339629ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
339771461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
339871461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
339934316482SAlexey Kardashevskiy         { TYPE_NMI },
3400c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
34011d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
34027844e12bSCédric Le Goater         { TYPE_XICS_FABRIC },
34036449da45SCédric Le Goater         { TYPE_INTERRUPT_STATS_PROVIDER },
340471461b0fSAlexey Kardashevskiy         { }
340571461b0fSAlexey Kardashevskiy     },
340629ee3247SAlexey Kardashevskiy };
340729ee3247SAlexey Kardashevskiy 
3408fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
34095013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
34105013c547SDavid Gibson                                                     void *data)      \
34115013c547SDavid Gibson     {                                                                \
34125013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
34135013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3414fccbc785SDavid Gibson         if (latest) {                                                \
3415fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3416fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3417fccbc785SDavid Gibson         }                                                            \
34185013c547SDavid Gibson     }                                                                \
34195013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
34205013c547SDavid Gibson     {                                                                \
34215013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
34225013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
34235013c547SDavid Gibson     }                                                                \
34245013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
34255013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
34265013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
34275013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
34285013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
34295013c547SDavid Gibson     };                                                               \
34305013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
34315013c547SDavid Gibson     {                                                                \
34325013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
34335013c547SDavid Gibson     }                                                                \
34340e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
34355013c547SDavid Gibson 
34361c5f29bbSDavid Gibson /*
34373fa14fbeSDavid Gibson  * pseries-2.10
3438db800b21SDavid Gibson  */
34393fa14fbeSDavid Gibson static void spapr_machine_2_10_instance_options(MachineState *machine)
3440db800b21SDavid Gibson {
3441db800b21SDavid Gibson }
3442db800b21SDavid Gibson 
34433fa14fbeSDavid Gibson static void spapr_machine_2_10_class_options(MachineClass *mc)
3444db800b21SDavid Gibson {
3445db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
3446db800b21SDavid Gibson }
3447db800b21SDavid Gibson 
34483fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_10, "2.10", true);
34493fa14fbeSDavid Gibson 
34503fa14fbeSDavid Gibson /*
34513fa14fbeSDavid Gibson  * pseries-2.9
34523fa14fbeSDavid Gibson  */
34533fa14fbeSDavid Gibson #define SPAPR_COMPAT_2_9                                               \
3454d5fc133eSDavid Gibson     HW_COMPAT_2_9                                                      \
3455d5fc133eSDavid Gibson     {                                                                  \
3456d5fc133eSDavid Gibson         .driver = TYPE_POWERPC_CPU,                                    \
3457d5fc133eSDavid Gibson         .property = "pre-2.10-migration",                              \
3458d5fc133eSDavid Gibson         .value    = "on",                                              \
3459d5fc133eSDavid Gibson     },                                                                 \
34603fa14fbeSDavid Gibson 
34613fa14fbeSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
34623fa14fbeSDavid Gibson {
34633fa14fbeSDavid Gibson     spapr_machine_2_10_instance_options(machine);
34643fa14fbeSDavid Gibson }
34653fa14fbeSDavid Gibson 
34663fa14fbeSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
34673fa14fbeSDavid Gibson {
346846f7afa3SGreg Kurz     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
346946f7afa3SGreg Kurz 
34703fa14fbeSDavid Gibson     spapr_machine_2_10_class_options(mc);
34713fa14fbeSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_9);
34723bfe5716SLaurent Vivier     mc->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
347346f7afa3SGreg Kurz     smc->pre_2_10_has_unused_icps = true;
34743fa14fbeSDavid Gibson }
34753fa14fbeSDavid Gibson 
34763fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
3477fa325e6cSDavid Gibson 
3478fa325e6cSDavid Gibson /*
3479fa325e6cSDavid Gibson  * pseries-2.8
3480fa325e6cSDavid Gibson  */
3481fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                                        \
348282516263SDavid Gibson     HW_COMPAT_2_8                                               \
348382516263SDavid Gibson     {                                                           \
348482516263SDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,                 \
348582516263SDavid Gibson         .property = "pcie-extended-configuration-space",        \
348682516263SDavid Gibson         .value    = "off",                                      \
348782516263SDavid Gibson     },
3488fa325e6cSDavid Gibson 
3489fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
3490fa325e6cSDavid Gibson {
3491fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
3492fa325e6cSDavid Gibson }
3493fa325e6cSDavid Gibson 
3494fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
3495fa325e6cSDavid Gibson {
3496fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
3497fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
349855641213SLaurent Vivier     mc->numa_mem_align_shift = 23;
3499fa325e6cSDavid Gibson }
3500fa325e6cSDavid Gibson 
3501fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
3502db800b21SDavid Gibson 
3503db800b21SDavid Gibson /*
35041ea1eefcSBharata B Rao  * pseries-2.7
35051ea1eefcSBharata B Rao  */
3506db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3507db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3508357d1e3bSDavid Gibson     {                                               \
3509357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3510357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3511357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3512357d1e3bSDavid Gibson     },                                              \
3513357d1e3bSDavid Gibson     {                                               \
3514357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3515357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3516357d1e3bSDavid Gibson         .value    = "0",                            \
3517146c11f1SDavid Gibson     },                                              \
3518146c11f1SDavid Gibson     {                                               \
3519146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3520146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3521146c11f1SDavid Gibson         .value    = "on",                           \
35225c4537bdSDavid Gibson     },                                              \
35235c4537bdSDavid Gibson     {                                               \
35245c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
35255c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
35265c4537bdSDavid Gibson         .value    = "on",                           \
3527357d1e3bSDavid Gibson     },
3528357d1e3bSDavid Gibson 
3529357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3530357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3531357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3532357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3533357d1e3bSDavid Gibson {
3534357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3535357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3536357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3537357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3538357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3539357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3540357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3541357d1e3bSDavid Gibson 
3542357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3543357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3544357d1e3bSDavid Gibson     int i;
3545357d1e3bSDavid Gibson 
3546357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3547357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3548357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3549357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3550357d1e3bSDavid Gibson          * regions */
3551357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3552357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3553357d1e3bSDavid Gibson     }
3554357d1e3bSDavid Gibson 
3555357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3556357d1e3bSDavid Gibson 
3557357d1e3bSDavid Gibson     if (index > max_index) {
3558357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3559357d1e3bSDavid Gibson                    max_index);
3560357d1e3bSDavid Gibson         return;
3561357d1e3bSDavid Gibson     }
3562357d1e3bSDavid Gibson 
3563357d1e3bSDavid Gibson     *buid = base_buid + index;
3564357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3565357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3566357d1e3bSDavid Gibson     }
3567357d1e3bSDavid Gibson 
3568357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3569357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3570357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3571357d1e3bSDavid Gibson     /*
3572357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3573357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3574357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3575357d1e3bSDavid Gibson      */
3576357d1e3bSDavid Gibson }
3577db800b21SDavid Gibson 
35781ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
35791ea1eefcSBharata B Rao {
3580f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3581f6229214SMichael Roth 
3582672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3583f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
35841ea1eefcSBharata B Rao }
35851ea1eefcSBharata B Rao 
35861ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
35871ea1eefcSBharata B Rao {
35883daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
35893daa4a9fSThomas Huth 
3590db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
35913daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3592db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3593357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
35941ea1eefcSBharata B Rao }
35951ea1eefcSBharata B Rao 
3596db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
35971ea1eefcSBharata B Rao 
35981ea1eefcSBharata B Rao /*
35994b23699cSDavid Gibson  * pseries-2.6
36004b23699cSDavid Gibson  */
36011ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3602ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3603ae4de14cSAlexey Kardashevskiy     { \
3604ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3605ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3606ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3607ae4de14cSAlexey Kardashevskiy     },
36081ea1eefcSBharata B Rao 
36094b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
36104b23699cSDavid Gibson {
3611672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
36124b23699cSDavid Gibson }
36134b23699cSDavid Gibson 
36144b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
36154b23699cSDavid Gibson {
36161ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3617c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
36181ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
36194b23699cSDavid Gibson }
36204b23699cSDavid Gibson 
36211ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
36224b23699cSDavid Gibson 
36234b23699cSDavid Gibson /*
36241c5f29bbSDavid Gibson  * pseries-2.5
36251c5f29bbSDavid Gibson  */
36264b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
362757c522f4SThomas Huth     HW_COMPAT_2_5 \
362857c522f4SThomas Huth     { \
362957c522f4SThomas Huth         .driver   = "spapr-vlan", \
363057c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
363157c522f4SThomas Huth         .value    = "off", \
363257c522f4SThomas Huth     },
36334b23699cSDavid Gibson 
36345013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
36351c5f29bbSDavid Gibson {
3636672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
36375013c547SDavid Gibson }
36385013c547SDavid Gibson 
36395013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
36405013c547SDavid Gibson {
364157040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
364257040d45SThomas Huth 
36434b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
364457040d45SThomas Huth     smc->use_ohci_by_default = true;
36454b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
36461c5f29bbSDavid Gibson }
36471c5f29bbSDavid Gibson 
36484b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
36491c5f29bbSDavid Gibson 
36501c5f29bbSDavid Gibson /*
36511c5f29bbSDavid Gibson  * pseries-2.4
36521c5f29bbSDavid Gibson  */
365380fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
365480fd50f9SCornelia Huck         HW_COMPAT_2_4
365580fd50f9SCornelia Huck 
36565013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
36571c5f29bbSDavid Gibson {
36585013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
36595013c547SDavid Gibson }
36601c5f29bbSDavid Gibson 
36615013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
36625013c547SDavid Gibson {
3663fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3664fc9f38c3SDavid Gibson 
3665fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3666fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3667f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
36681c5f29bbSDavid Gibson }
36691c5f29bbSDavid Gibson 
3670fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
36711c5f29bbSDavid Gibson 
36721c5f29bbSDavid Gibson /*
36731c5f29bbSDavid Gibson  * pseries-2.3
36741c5f29bbSDavid Gibson  */
367538ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
36767619c7b0SMichael Roth         HW_COMPAT_2_3 \
36777619c7b0SMichael Roth         {\
36787619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
36797619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
36807619c7b0SMichael Roth             .value    = "off",\
36817619c7b0SMichael Roth         },
368238ff32c6SEduardo Habkost 
36835013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
36841c5f29bbSDavid Gibson {
36855013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
36861c5f29bbSDavid Gibson }
36871c5f29bbSDavid Gibson 
36885013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
36891c5f29bbSDavid Gibson {
3690fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3691f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
36921c5f29bbSDavid Gibson }
3693fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
36941c5f29bbSDavid Gibson 
36951c5f29bbSDavid Gibson /*
36961c5f29bbSDavid Gibson  * pseries-2.2
36971c5f29bbSDavid Gibson  */
36981c5f29bbSDavid Gibson 
3699b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
37004dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3701b194df47SAlexey Kardashevskiy         {\
3702b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3703b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3704b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3705dd754bafSEduardo Habkost         },
3706b194df47SAlexey Kardashevskiy 
37075013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3708b0e966d0SJason Wang {
37095013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3710cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3711b0e966d0SJason Wang }
3712b0e966d0SJason Wang 
37135013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3714b0e966d0SJason Wang {
3715fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3716f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
37171c5f29bbSDavid Gibson }
3718fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
37191c5f29bbSDavid Gibson 
37201c5f29bbSDavid Gibson /*
37211c5f29bbSDavid Gibson  * pseries-2.1
37221c5f29bbSDavid Gibson  */
37231c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
37241c5f29bbSDavid Gibson         HW_COMPAT_2_1
37251c5f29bbSDavid Gibson 
37265013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
37271c5f29bbSDavid Gibson {
37285013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
37291c5f29bbSDavid Gibson }
37301c5f29bbSDavid Gibson 
37315013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
3732b0e966d0SJason Wang {
3733fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
3734f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
37356026db45SAlexey Kardashevskiy }
3736fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
37376026db45SAlexey Kardashevskiy 
373829ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
373929ee3247SAlexey Kardashevskiy {
374029ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
374129ee3247SAlexey Kardashevskiy }
374229ee3247SAlexey Kardashevskiy 
374329ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
3744