xref: /openbmc/qemu/hw/ppc/spapr.c (revision 06747ba6)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2004-2007 Fabrice Bellard
553018216SPaolo Bonzini  * Copyright (c) 2007 Jocelyn Mayer
653018216SPaolo Bonzini  * Copyright (c) 2010 David Gibson, IBM Corporation.
753018216SPaolo Bonzini  *
853018216SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
953018216SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
1053018216SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
1153018216SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1253018216SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
1353018216SPaolo Bonzini  * furnished to do so, subject to the following conditions:
1453018216SPaolo Bonzini  *
1553018216SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
1653018216SPaolo Bonzini  * all copies or substantial portions of the Software.
1753018216SPaolo Bonzini  *
1853018216SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1953018216SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2053018216SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2153018216SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2253018216SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2353018216SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2453018216SPaolo Bonzini  * THE SOFTWARE.
2553018216SPaolo Bonzini  *
2653018216SPaolo Bonzini  */
270d75590dSPeter Maydell #include "qemu/osdep.h"
28da34e65cSMarkus Armbruster #include "qapi/error.h"
2953018216SPaolo Bonzini #include "sysemu/sysemu.h"
30e35704baSEduardo Habkost #include "sysemu/numa.h"
3153018216SPaolo Bonzini #include "hw/hw.h"
3203dd024fSPaolo Bonzini #include "qemu/log.h"
3371461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h"
3453018216SPaolo Bonzini #include "elf.h"
3553018216SPaolo Bonzini #include "net/net.h"
36ad440b4aSAndrew Jones #include "sysemu/device_tree.h"
37fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3853018216SPaolo Bonzini #include "sysemu/cpus.h"
39b3946626SVincent Palatin #include "sysemu/hw_accel.h"
4053018216SPaolo Bonzini #include "kvm_ppc.h"
41ff14e817SDr. David Alan Gilbert #include "migration/migration.h"
424be21d56SDavid Gibson #include "mmu-hash64.h"
43b4db5413SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
443794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4553018216SPaolo Bonzini 
4653018216SPaolo Bonzini #include "hw/boards.h"
470d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
4853018216SPaolo Bonzini #include "hw/loader.h"
4953018216SPaolo Bonzini 
507804c353SCédric Le Goater #include "hw/ppc/fdt.h"
510d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
520d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
530d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
540d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5553018216SPaolo Bonzini #include "hw/pci/msi.h"
5653018216SPaolo Bonzini 
5753018216SPaolo Bonzini #include "hw/pci/pci.h"
5871461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
5971461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
6053018216SPaolo Bonzini 
6153018216SPaolo Bonzini #include "exec/address-spaces.h"
6253018216SPaolo Bonzini #include "hw/usb.h"
6353018216SPaolo Bonzini #include "qemu/config-file.h"
64135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
652a6593cbSAlexey Kardashevskiy #include "trace.h"
6634316482SAlexey Kardashevskiy #include "hw/nmi.h"
676449da45SCédric Le Goater #include "hw/intc/intc.h"
6853018216SPaolo Bonzini 
6968a27b20SMichael S. Tsirkin #include "hw/compat.h"
70f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7194a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
722474bfd4SIgor Mammedov #include "qmp-commands.h"
7368a27b20SMichael S. Tsirkin 
7453018216SPaolo Bonzini #include <libfdt.h>
7553018216SPaolo Bonzini 
7653018216SPaolo Bonzini /* SLOF memory layout:
7753018216SPaolo Bonzini  *
7853018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
7953018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
8053018216SPaolo Bonzini  *
8153018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8253018216SPaolo Bonzini  * and more
8353018216SPaolo Bonzini  *
8453018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8553018216SPaolo Bonzini  */
8638b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
8753018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
88b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
8953018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
9053018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9153018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9253018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9353018216SPaolo Bonzini 
9453018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9553018216SPaolo Bonzini 
9653018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9753018216SPaolo Bonzini 
9853018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9953018216SPaolo Bonzini 
100e6f7e110SCédric Le Goater static int try_create_xics(sPAPRMachineState *spapr, const char *type_ics,
101817bb6a4SCédric Le Goater                            const char *type_icp, int nr_servers,
102817bb6a4SCédric Le Goater                            int nr_irqs, Error **errp)
103c04d6cfaSAnthony Liguori {
1042cd908d0SCédric Le Goater     XICSFabric *xi = XICS_FABRIC(spapr);
1054e4169f7SCédric Le Goater     Error *err = NULL, *local_err = NULL;
1064e4169f7SCédric Le Goater     ICSState *ics = NULL;
107817bb6a4SCédric Le Goater     int i;
108c04d6cfaSAnthony Liguori 
1094e4169f7SCédric Le Goater     ics = ICS_SIMPLE(object_new(type_ics));
110681bfadeSCédric Le Goater     object_property_add_child(OBJECT(spapr), "ics", OBJECT(ics), NULL);
1114e4169f7SCédric Le Goater     object_property_set_int(OBJECT(ics), nr_irqs, "nr-irqs", &err);
112b4f27d71SCédric Le Goater     object_property_add_const_link(OBJECT(ics), "xics", OBJECT(xi), NULL);
1134e4169f7SCédric Le Goater     object_property_set_bool(OBJECT(ics), true, "realized", &local_err);
1144e4169f7SCédric Le Goater     error_propagate(&err, local_err);
1154e4169f7SCédric Le Goater     if (err) {
1164e4169f7SCédric Le Goater         goto error;
1174e4169f7SCédric Le Goater     }
1184e4169f7SCédric Le Goater 
119852ad27eSCédric Le Goater     spapr->icps = g_malloc0(nr_servers * sizeof(ICPState));
120852ad27eSCédric Le Goater     spapr->nr_servers = nr_servers;
121817bb6a4SCédric Le Goater 
122817bb6a4SCédric Le Goater     for (i = 0; i < nr_servers; i++) {
123852ad27eSCédric Le Goater         ICPState *icp = &spapr->icps[i];
124817bb6a4SCédric Le Goater 
125817bb6a4SCédric Le Goater         object_initialize(icp, sizeof(*icp), type_icp);
126852ad27eSCédric Le Goater         object_property_add_child(OBJECT(spapr), "icp[*]", OBJECT(icp), NULL);
1272cd908d0SCédric Le Goater         object_property_add_const_link(OBJECT(icp), "xics", OBJECT(xi), NULL);
128817bb6a4SCédric Le Goater         object_property_set_bool(OBJECT(icp), true, "realized", &err);
129817bb6a4SCédric Le Goater         if (err) {
130817bb6a4SCédric Le Goater             goto error;
131817bb6a4SCédric Le Goater         }
132817bb6a4SCédric Le Goater         object_unref(OBJECT(icp));
133817bb6a4SCédric Le Goater     }
134817bb6a4SCédric Le Goater 
135681bfadeSCédric Le Goater     spapr->ics = ics;
136e6f7e110SCédric Le Goater     return 0;
1374e4169f7SCédric Le Goater 
1384e4169f7SCédric Le Goater error:
1394e4169f7SCédric Le Goater     error_propagate(errp, err);
1404e4169f7SCédric Le Goater     if (ics) {
1414e4169f7SCédric Le Goater         object_unparent(OBJECT(ics));
1424e4169f7SCédric Le Goater     }
143e6f7e110SCédric Le Goater     return -1;
144c04d6cfaSAnthony Liguori }
145c04d6cfaSAnthony Liguori 
146e6f7e110SCédric Le Goater static int xics_system_init(MachineState *machine,
1471e49182dSDavid Gibson                             int nr_servers, int nr_irqs, Error **errp)
148c04d6cfaSAnthony Liguori {
149e6f7e110SCédric Le Goater     int rc = -1;
150c04d6cfaSAnthony Liguori 
15111ad93f6SDavid Gibson     if (kvm_enabled()) {
15234f2af3dSMarkus Armbruster         Error *err = NULL;
15334f2af3dSMarkus Armbruster 
1542192a930SCédric Le Goater         if (machine_kernel_irqchip_allowed(machine) &&
1552192a930SCédric Le Goater             !xics_kvm_init(SPAPR_MACHINE(machine), errp)) {
156e6f7e110SCédric Le Goater             rc = try_create_xics(SPAPR_MACHINE(machine), TYPE_ICS_KVM,
157817bb6a4SCédric Le Goater                                  TYPE_KVM_ICP, nr_servers, nr_irqs, &err);
15811ad93f6SDavid Gibson         }
159e6f7e110SCédric Le Goater         if (machine_kernel_irqchip_required(machine) && rc < 0) {
160b83baa60SMarkus Armbruster             error_reportf_err(err,
161b83baa60SMarkus Armbruster                               "kernel_irqchip requested but unavailable: ");
162b83baa60SMarkus Armbruster         } else {
163903a41d3SStefano Dong (董兴水)             error_free(err);
16411ad93f6SDavid Gibson         }
165b83baa60SMarkus Armbruster     }
16611ad93f6SDavid Gibson 
167e6f7e110SCédric Le Goater     if (rc < 0) {
1682192a930SCédric Le Goater         xics_spapr_init(SPAPR_MACHINE(machine), errp);
169e6f7e110SCédric Le Goater         rc = try_create_xics(SPAPR_MACHINE(machine), TYPE_ICS_SIMPLE,
170681bfadeSCédric Le Goater                                TYPE_ICP, nr_servers, nr_irqs, errp);
171c04d6cfaSAnthony Liguori     }
172c04d6cfaSAnthony Liguori 
173e6f7e110SCédric Le Goater     return rc;
174c04d6cfaSAnthony Liguori }
175c04d6cfaSAnthony Liguori 
176833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
177833d4668SAlexey Kardashevskiy                                   int smt_threads)
178833d4668SAlexey Kardashevskiy {
179833d4668SAlexey Kardashevskiy     int i, ret = 0;
180833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
181833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
182833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
183833d4668SAlexey Kardashevskiy 
184d6e166c0SDavid Gibson     if (cpu->compat_pvr) {
185d6e166c0SDavid Gibson         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
1866d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1876d9412eaSAlexey Kardashevskiy             return ret;
1886d9412eaSAlexey Kardashevskiy         }
1896d9412eaSAlexey Kardashevskiy     }
1906d9412eaSAlexey Kardashevskiy 
191833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
192833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
193833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
194833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
195833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
196833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
197833d4668SAlexey Kardashevskiy     }
198833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
199833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
200833d4668SAlexey Kardashevskiy     if (ret < 0) {
201833d4668SAlexey Kardashevskiy         return ret;
202833d4668SAlexey Kardashevskiy     }
203833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
204833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
205833d4668SAlexey Kardashevskiy 
206833d4668SAlexey Kardashevskiy     return ret;
207833d4668SAlexey Kardashevskiy }
208833d4668SAlexey Kardashevskiy 
2090da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
2100da6f3feSBharata B Rao {
2110da6f3feSBharata B Rao     int ret = 0;
2120da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2130da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
2140da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
2150da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2160da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2170da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
2180da6f3feSBharata B Rao                                 cpu_to_be32(cs->numa_node),
2190da6f3feSBharata B Rao                                 cpu_to_be32(index)};
2200da6f3feSBharata B Rao 
2210da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
2220da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
2230da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
2240da6f3feSBharata B Rao                           sizeof(associativity));
2250da6f3feSBharata B Rao     }
2260da6f3feSBharata B Rao 
2270da6f3feSBharata B Rao     return ret;
2280da6f3feSBharata B Rao }
2290da6f3feSBharata B Rao 
23086d5771aSSam Bobroff /* Populate the "ibm,pa-features" property */
231e957f6a9SSam Bobroff static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset,
232e957f6a9SSam Bobroff                                       bool legacy_guest)
23386d5771aSSam Bobroff {
23486d5771aSSam Bobroff     uint8_t pa_features_206[] = { 6, 0,
23586d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
23686d5771aSSam Bobroff     uint8_t pa_features_207[] = { 24, 0,
23786d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
23886d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
23986d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
24086d5771aSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
2419fb4541fSSam Bobroff     uint8_t pa_features_300[] = { 66, 0,
2429fb4541fSSam Bobroff         /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
2439fb4541fSSam Bobroff         /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
24486d5771aSSam Bobroff         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
2459fb4541fSSam Bobroff         /* 6: DS207 */
24686d5771aSSam Bobroff         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
2479fb4541fSSam Bobroff         /* 16: Vector */
24886d5771aSSam Bobroff         0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
2499fb4541fSSam Bobroff         /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
2509fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 18 - 23 */
2519fb4541fSSam Bobroff         /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
2529fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
2539fb4541fSSam Bobroff         /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
2549fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
2559fb4541fSSam Bobroff         /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
2569fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
2579fb4541fSSam Bobroff         /* 42: PM, 44: PC RA, 46: SC vec'd */
2589fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
2599fb4541fSSam Bobroff         /* 48: SIMD, 50: QP BFP, 52: String */
2609fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
2619fb4541fSSam Bobroff         /* 54: DecFP, 56: DecI, 58: SHA */
2629fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
2639fb4541fSSam Bobroff         /* 60: NM atomic, 62: RNG */
2649fb4541fSSam Bobroff         0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
2659fb4541fSSam Bobroff     };
26686d5771aSSam Bobroff     uint8_t *pa_features;
26786d5771aSSam Bobroff     size_t pa_size;
26886d5771aSSam Bobroff 
26986d5771aSSam Bobroff     switch (POWERPC_MMU_VER(env->mmu_model)) {
27086d5771aSSam Bobroff     case POWERPC_MMU_VER_2_06:
27186d5771aSSam Bobroff         pa_features = pa_features_206;
27286d5771aSSam Bobroff         pa_size = sizeof(pa_features_206);
27386d5771aSSam Bobroff         break;
27486d5771aSSam Bobroff     case POWERPC_MMU_VER_2_07:
27586d5771aSSam Bobroff         pa_features = pa_features_207;
27686d5771aSSam Bobroff         pa_size = sizeof(pa_features_207);
27786d5771aSSam Bobroff         break;
27886d5771aSSam Bobroff     case POWERPC_MMU_VER_3_00:
27986d5771aSSam Bobroff         pa_features = pa_features_300;
28086d5771aSSam Bobroff         pa_size = sizeof(pa_features_300);
28186d5771aSSam Bobroff         break;
28286d5771aSSam Bobroff     default:
28386d5771aSSam Bobroff         return;
28486d5771aSSam Bobroff     }
28586d5771aSSam Bobroff 
28686d5771aSSam Bobroff     if (env->ci_large_pages) {
28786d5771aSSam Bobroff         /*
28886d5771aSSam Bobroff          * Note: we keep CI large pages off by default because a 64K capable
28986d5771aSSam Bobroff          * guest provisioned with large pages might otherwise try to map a qemu
29086d5771aSSam Bobroff          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
29186d5771aSSam Bobroff          * even if that qemu runs on a 4k host.
29286d5771aSSam Bobroff          * We dd this bit back here if we are confident this is not an issue
29386d5771aSSam Bobroff          */
29486d5771aSSam Bobroff         pa_features[3] |= 0x20;
29586d5771aSSam Bobroff     }
29686d5771aSSam Bobroff     if (kvmppc_has_cap_htm() && pa_size > 24) {
29786d5771aSSam Bobroff         pa_features[24] |= 0x80;    /* Transactional memory support */
29886d5771aSSam Bobroff     }
299e957f6a9SSam Bobroff     if (legacy_guest && pa_size > 40) {
300e957f6a9SSam Bobroff         /* Workaround for broken kernels that attempt (guest) radix
301e957f6a9SSam Bobroff          * mode when they can't handle it, if they see the radix bit set
302e957f6a9SSam Bobroff          * in pa-features. So hide it from them. */
303e957f6a9SSam Bobroff         pa_features[40 + 2] &= ~0x80; /* Radix MMU */
304e957f6a9SSam Bobroff     }
30586d5771aSSam Bobroff 
30686d5771aSSam Bobroff     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
30786d5771aSSam Bobroff }
30886d5771aSSam Bobroff 
30928e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
31053018216SPaolo Bonzini {
31182677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
31282677ed2SAlexey Kardashevskiy     CPUState *cs;
31353018216SPaolo Bonzini     char cpu_model[32];
31453018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
31553018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
31653018216SPaolo Bonzini 
31782677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
31882677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
319e957f6a9SSam Bobroff         CPUPPCState *env = &cpu->env;
32082677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
32182677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
32212dbeb16SDavid Gibson         int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
32353018216SPaolo Bonzini 
3240f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
32553018216SPaolo Bonzini             continue;
32653018216SPaolo Bonzini         }
32753018216SPaolo Bonzini 
32882677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
32953018216SPaolo Bonzini 
33082677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
33182677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
33282677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
33382677ed2SAlexey Kardashevskiy                                           "cpus");
33482677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
33582677ed2SAlexey Kardashevskiy                 return cpus_offset;
33682677ed2SAlexey Kardashevskiy             }
33782677ed2SAlexey Kardashevskiy         }
33882677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
33982677ed2SAlexey Kardashevskiy         if (offset < 0) {
34082677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
34153018216SPaolo Bonzini             if (offset < 0) {
34253018216SPaolo Bonzini                 return offset;
34353018216SPaolo Bonzini             }
34482677ed2SAlexey Kardashevskiy         }
34553018216SPaolo Bonzini 
3460da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
3470da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
34853018216SPaolo Bonzini         if (ret < 0) {
34953018216SPaolo Bonzini             return ret;
35053018216SPaolo Bonzini         }
35153018216SPaolo Bonzini 
3520da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
35353018216SPaolo Bonzini         if (ret < 0) {
35453018216SPaolo Bonzini             return ret;
35553018216SPaolo Bonzini         }
356833d4668SAlexey Kardashevskiy 
35712dbeb16SDavid Gibson         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt);
358833d4668SAlexey Kardashevskiy         if (ret < 0) {
359833d4668SAlexey Kardashevskiy             return ret;
360833d4668SAlexey Kardashevskiy         }
361e957f6a9SSam Bobroff 
362e957f6a9SSam Bobroff         spapr_populate_pa_features(env, fdt, offset,
363e957f6a9SSam Bobroff                                          spapr->cas_legacy_guest_workaround);
36453018216SPaolo Bonzini     }
36553018216SPaolo Bonzini     return ret;
36653018216SPaolo Bonzini }
36753018216SPaolo Bonzini 
368b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
369b082d65aSAlexey Kardashevskiy {
370fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
371fb164994SDavid Gibson 
372b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
373b082d65aSAlexey Kardashevskiy         int i;
374b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
375b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
376fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
377fb164994SDavid Gibson                            machine->ram_size);
378b082d65aSAlexey Kardashevskiy             }
379b082d65aSAlexey Kardashevskiy         }
380b082d65aSAlexey Kardashevskiy     }
381fb164994SDavid Gibson     return machine->ram_size;
382b082d65aSAlexey Kardashevskiy }
383b082d65aSAlexey Kardashevskiy 
384a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
385a1d59c0fSAlexey Kardashevskiy {
386a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
387a1d59c0fSAlexey Kardashevskiy }
38853018216SPaolo Bonzini 
38903d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
39026a8c353SAlexey Kardashevskiy                                        hwaddr size)
39126a8c353SAlexey Kardashevskiy {
39226a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
39326a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
39426a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
395c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
39626a8c353SAlexey Kardashevskiy     };
39726a8c353SAlexey Kardashevskiy     char mem_name[32];
39826a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
39926a8c353SAlexey Kardashevskiy     int off;
40026a8c353SAlexey Kardashevskiy 
40126a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
40226a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
40326a8c353SAlexey Kardashevskiy 
40426a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
40526a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
40626a8c353SAlexey Kardashevskiy     _FDT(off);
40726a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
40826a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
40926a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
41026a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
41126a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
41203d196b7SBharata B Rao     return off;
41326a8c353SAlexey Kardashevskiy }
41426a8c353SAlexey Kardashevskiy 
41528e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
41653018216SPaolo Bonzini {
417fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
4187db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
4197db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
4207db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
4217db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
42253018216SPaolo Bonzini 
4237db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
4247db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
4257db8a127SAlexey Kardashevskiy         nb_nodes = 1;
426fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
4277db8a127SAlexey Kardashevskiy         nodes = &ramnode;
4285fe269b1SPaul Mackerras     }
42953018216SPaolo Bonzini 
4307db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
4317db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
4327db8a127SAlexey Kardashevskiy             continue;
43353018216SPaolo Bonzini         }
434fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
4355fe269b1SPaul Mackerras             node_size = 0;
4365fe269b1SPaul Mackerras         } else {
4377db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
438fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
439fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
4405fe269b1SPaul Mackerras             }
4415fe269b1SPaul Mackerras         }
4427db8a127SAlexey Kardashevskiy         if (!mem_start) {
4437db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
444e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
4457db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
4467db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
4477db8a127SAlexey Kardashevskiy         }
4486010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
4496010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
4506010818cSAlexey Kardashevskiy 
4516010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
4526010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
4536010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
4546010818cSAlexey Kardashevskiy             }
4556010818cSAlexey Kardashevskiy 
4566010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
4576010818cSAlexey Kardashevskiy             node_size -= sizetmp;
4586010818cSAlexey Kardashevskiy             mem_start += sizetmp;
4596010818cSAlexey Kardashevskiy         }
46053018216SPaolo Bonzini     }
46153018216SPaolo Bonzini 
46253018216SPaolo Bonzini     return 0;
46353018216SPaolo Bonzini }
46453018216SPaolo Bonzini 
4650da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
4660da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
4670da6f3feSBharata B Rao {
4680da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
4690da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
4700da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4710da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
4720da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
4730da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
474afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
475afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
4760da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
4770da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
4780da6f3feSBharata B Rao     size_t page_sizes_prop_size;
47922419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
4800da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
48112dbeb16SDavid Gibson     int compat_smt = MIN(smp_threads, ppc_compat_max_threads(cpu));
482af81cf32SBharata B Rao     sPAPRDRConnector *drc;
483af81cf32SBharata B Rao     sPAPRDRConnectorClass *drck;
484af81cf32SBharata B Rao     int drc_index;
485c64abd1fSSam Bobroff     uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
486c64abd1fSSam Bobroff     int i;
487af81cf32SBharata B Rao 
488af81cf32SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
489af81cf32SBharata B Rao     if (drc) {
490af81cf32SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
491af81cf32SBharata B Rao         drc_index = drck->get_index(drc);
492af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
493af81cf32SBharata B Rao     }
4940da6f3feSBharata B Rao 
4950da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
4960da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
4970da6f3feSBharata B Rao 
4980da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
4990da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
5000da6f3feSBharata B Rao                            env->dcache_line_size)));
5010da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
5020da6f3feSBharata B Rao                            env->dcache_line_size)));
5030da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
5040da6f3feSBharata B Rao                            env->icache_line_size)));
5050da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
5060da6f3feSBharata B Rao                            env->icache_line_size)));
5070da6f3feSBharata B Rao 
5080da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
5090da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
5100da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
5110da6f3feSBharata B Rao     } else {
512ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
5130da6f3feSBharata B Rao     }
5140da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
5150da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
5160da6f3feSBharata B Rao                                pcc->l1_icache_size)));
5170da6f3feSBharata B Rao     } else {
518ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
5190da6f3feSBharata B Rao     }
5200da6f3feSBharata B Rao 
5210da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
5220da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
523fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
5240da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
5250da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
5260da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
5270da6f3feSBharata B Rao 
5280da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
5290da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
5300da6f3feSBharata B Rao     }
5310da6f3feSBharata B Rao 
5320da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
5330da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
5340da6f3feSBharata B Rao                           segs, sizeof(segs))));
5350da6f3feSBharata B Rao     }
5360da6f3feSBharata B Rao 
5370da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
5380da6f3feSBharata B Rao      *   0 / no property == no vector extensions
5390da6f3feSBharata B Rao      *   1               == VMX / Altivec available
5400da6f3feSBharata B Rao      *   2               == VSX available */
5410da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
5420da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
5430da6f3feSBharata B Rao 
5440da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
5450da6f3feSBharata B Rao     }
5460da6f3feSBharata B Rao 
5470da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
5480da6f3feSBharata B Rao      *   0 / no property == no DFP
5490da6f3feSBharata B Rao      *   1               == DFP available */
5500da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
5510da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
5520da6f3feSBharata B Rao     }
5530da6f3feSBharata B Rao 
5543654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
5550da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
5560da6f3feSBharata B Rao     if (page_sizes_prop_size) {
5570da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
5580da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
5590da6f3feSBharata B Rao     }
5600da6f3feSBharata B Rao 
561e957f6a9SSam Bobroff     spapr_populate_pa_features(env, fdt, offset, false);
56290da0d5aSBenjamin Herrenschmidt 
5630da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
56422419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
5650da6f3feSBharata B Rao 
5660da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
5670da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
5680da6f3feSBharata B Rao 
5690da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
5700da6f3feSBharata B Rao 
57112dbeb16SDavid Gibson     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
572c64abd1fSSam Bobroff 
573c64abd1fSSam Bobroff     if (pcc->radix_page_info) {
574c64abd1fSSam Bobroff         for (i = 0; i < pcc->radix_page_info->count; i++) {
575c64abd1fSSam Bobroff             radix_AP_encodings[i] =
576c64abd1fSSam Bobroff                 cpu_to_be32(pcc->radix_page_info->entries[i]);
577c64abd1fSSam Bobroff         }
578c64abd1fSSam Bobroff         _FDT((fdt_setprop(fdt, offset, "ibm,processor-radix-AP-encodings",
579c64abd1fSSam Bobroff                           radix_AP_encodings,
580c64abd1fSSam Bobroff                           pcc->radix_page_info->count *
581c64abd1fSSam Bobroff                           sizeof(radix_AP_encodings[0]))));
582c64abd1fSSam Bobroff     }
5830da6f3feSBharata B Rao }
5840da6f3feSBharata B Rao 
5850da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
5860da6f3feSBharata B Rao {
5870da6f3feSBharata B Rao     CPUState *cs;
5880da6f3feSBharata B Rao     int cpus_offset;
5890da6f3feSBharata B Rao     char *nodename;
5900da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
5910da6f3feSBharata B Rao 
5920da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
5930da6f3feSBharata B Rao     _FDT(cpus_offset);
5940da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
5950da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
5960da6f3feSBharata B Rao 
5970da6f3feSBharata B Rao     /*
5980da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
5990da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
6000da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
6010da6f3feSBharata B Rao      */
6020da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
6030da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
6040da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
6050da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
6060da6f3feSBharata B Rao         int offset;
6070da6f3feSBharata B Rao 
6080da6f3feSBharata B Rao         if ((index % smt) != 0) {
6090da6f3feSBharata B Rao             continue;
6100da6f3feSBharata B Rao         }
6110da6f3feSBharata B Rao 
6120da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
6130da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
6140da6f3feSBharata B Rao         g_free(nodename);
6150da6f3feSBharata B Rao         _FDT(offset);
6160da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
6170da6f3feSBharata B Rao     }
6180da6f3feSBharata B Rao 
6190da6f3feSBharata B Rao }
6200da6f3feSBharata B Rao 
62103d196b7SBharata B Rao /*
62203d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
62303d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
62403d196b7SBharata B Rao  * of this device tree node.
62503d196b7SBharata B Rao  */
62603d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
62703d196b7SBharata B Rao {
62803d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
62903d196b7SBharata B Rao     int ret, i, offset;
63003d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
63103d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
632d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
633d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
634d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
635d0e5a8f2SBharata B Rao                        lmb_size;
63603d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
6376663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
63803d196b7SBharata B Rao 
639ef001f06SThomas Huth     /*
640d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
64116c25aefSBharata B Rao      */
642d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
64316c25aefSBharata B Rao         return 0;
64416c25aefSBharata B Rao     }
64516c25aefSBharata B Rao 
64616c25aefSBharata B Rao     /*
647ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
648ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
649ef001f06SThomas Huth      */
650ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
651ef001f06SThomas Huth               * sizeof(uint32_t);
65203d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
65303d196b7SBharata B Rao 
65403d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
65503d196b7SBharata B Rao 
65603d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
65703d196b7SBharata B Rao                     sizeof(prop_lmb_size));
65803d196b7SBharata B Rao     if (ret < 0) {
65903d196b7SBharata B Rao         goto out;
66003d196b7SBharata B Rao     }
66103d196b7SBharata B Rao 
66203d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
66303d196b7SBharata B Rao     if (ret < 0) {
66403d196b7SBharata B Rao         goto out;
66503d196b7SBharata B Rao     }
66603d196b7SBharata B Rao 
66703d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
66803d196b7SBharata B Rao     if (ret < 0) {
66903d196b7SBharata B Rao         goto out;
67003d196b7SBharata B Rao     }
67103d196b7SBharata B Rao 
67203d196b7SBharata B Rao     /* ibm,dynamic-memory */
67303d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
67403d196b7SBharata B Rao     cur_index++;
67503d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
676d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
67703d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
67803d196b7SBharata B Rao 
679d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
680d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
681d0e5a8f2SBharata B Rao             sPAPRDRConnectorClass *drck;
682d0e5a8f2SBharata B Rao 
683d0e5a8f2SBharata B Rao             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
68403d196b7SBharata B Rao             g_assert(drc);
68503d196b7SBharata B Rao             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
68603d196b7SBharata B Rao 
68703d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
68803d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
68903d196b7SBharata B Rao             dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
69003d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
69103d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
692d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
69303d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
69403d196b7SBharata B Rao             } else {
69503d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
69603d196b7SBharata B Rao             }
697d0e5a8f2SBharata B Rao         } else {
698d0e5a8f2SBharata B Rao             /*
699d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
700d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
701d0e5a8f2SBharata B Rao              * and as having no valid DRC.
702d0e5a8f2SBharata B Rao              */
703d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
704d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
705d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
706d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
707d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
708d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
709d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
710d0e5a8f2SBharata B Rao         }
71103d196b7SBharata B Rao 
71203d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
71303d196b7SBharata B Rao     }
71403d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
71503d196b7SBharata B Rao     if (ret < 0) {
71603d196b7SBharata B Rao         goto out;
71703d196b7SBharata B Rao     }
71803d196b7SBharata B Rao 
71903d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
72003d196b7SBharata B Rao     cur_index = int_buf;
7216663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
72203d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
72303d196b7SBharata B Rao     cur_index += 2;
7246663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
72503d196b7SBharata B Rao         uint32_t associativity[] = {
72603d196b7SBharata B Rao             cpu_to_be32(0x0),
72703d196b7SBharata B Rao             cpu_to_be32(0x0),
72803d196b7SBharata B Rao             cpu_to_be32(0x0),
72903d196b7SBharata B Rao             cpu_to_be32(i)
73003d196b7SBharata B Rao         };
73103d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
73203d196b7SBharata B Rao         cur_index += 4;
73303d196b7SBharata B Rao     }
73403d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
73503d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
73603d196b7SBharata B Rao out:
73703d196b7SBharata B Rao     g_free(int_buf);
73803d196b7SBharata B Rao     return ret;
73903d196b7SBharata B Rao }
74003d196b7SBharata B Rao 
7416787d27bSMichael Roth static int spapr_dt_cas_updates(sPAPRMachineState *spapr, void *fdt,
7426787d27bSMichael Roth                                 sPAPROptionVector *ov5_updates)
7436787d27bSMichael Roth {
7446787d27bSMichael Roth     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
745417ece33SMichael Roth     int ret = 0, offset;
7466787d27bSMichael Roth 
7476787d27bSMichael Roth     /* Generate ibm,dynamic-reconfiguration-memory node if required */
7486787d27bSMichael Roth     if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
7496787d27bSMichael Roth         g_assert(smc->dr_lmb_enabled);
7506787d27bSMichael Roth         ret = spapr_populate_drconf_memory(spapr, fdt);
751417ece33SMichael Roth         if (ret) {
752417ece33SMichael Roth             goto out;
753417ece33SMichael Roth         }
7546787d27bSMichael Roth     }
7556787d27bSMichael Roth 
756417ece33SMichael Roth     offset = fdt_path_offset(fdt, "/chosen");
757417ece33SMichael Roth     if (offset < 0) {
758417ece33SMichael Roth         offset = fdt_add_subnode(fdt, 0, "chosen");
759417ece33SMichael Roth         if (offset < 0) {
760417ece33SMichael Roth             return offset;
761417ece33SMichael Roth         }
762417ece33SMichael Roth     }
763417ece33SMichael Roth     ret = spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
764417ece33SMichael Roth                                  "ibm,architecture-vec-5");
765417ece33SMichael Roth 
766417ece33SMichael Roth out:
7676787d27bSMichael Roth     return ret;
7686787d27bSMichael Roth }
7696787d27bSMichael Roth 
77003d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
77103d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
7726787d27bSMichael Roth                                  sPAPROptionVector *ov5_updates)
77303d196b7SBharata B Rao {
77403d196b7SBharata B Rao     void *fdt, *fdt_skel;
77503d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
77603d196b7SBharata B Rao 
77703d196b7SBharata B Rao     size -= sizeof(hdr);
77803d196b7SBharata B Rao 
77903d196b7SBharata B Rao     /* Create sceleton */
78003d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
78103d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
78203d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
78303d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
78403d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
78503d196b7SBharata B Rao     fdt = g_malloc0(size);
78603d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
78703d196b7SBharata B Rao     g_free(fdt_skel);
78803d196b7SBharata B Rao 
78903d196b7SBharata B Rao     /* Fixup cpu nodes */
79003d196b7SBharata B Rao     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
79103d196b7SBharata B Rao 
7926787d27bSMichael Roth     if (spapr_dt_cas_updates(spapr, fdt, ov5_updates)) {
7936787d27bSMichael Roth         return -1;
79403d196b7SBharata B Rao     }
79503d196b7SBharata B Rao 
79603d196b7SBharata B Rao     /* Pack resulting tree */
79703d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
79803d196b7SBharata B Rao 
79903d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
80003d196b7SBharata B Rao         trace_spapr_cas_failed(size);
80103d196b7SBharata B Rao         return -1;
80203d196b7SBharata B Rao     }
80303d196b7SBharata B Rao 
80403d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
80503d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
80603d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
80703d196b7SBharata B Rao     g_free(fdt);
80803d196b7SBharata B Rao 
80903d196b7SBharata B Rao     return 0;
81003d196b7SBharata B Rao }
81103d196b7SBharata B Rao 
8123f5dabceSDavid Gibson static void spapr_dt_rtas(sPAPRMachineState *spapr, void *fdt)
8133f5dabceSDavid Gibson {
8143f5dabceSDavid Gibson     int rtas;
8153f5dabceSDavid Gibson     GString *hypertas = g_string_sized_new(256);
8163f5dabceSDavid Gibson     GString *qemu_hypertas = g_string_sized_new(256);
8173f5dabceSDavid Gibson     uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
8183f5dabceSDavid Gibson     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
8193f5dabceSDavid Gibson         memory_region_size(&spapr->hotplug_memory.mr);
8203f5dabceSDavid Gibson     uint32_t lrdr_capacity[] = {
8213f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr >> 32),
8223f5dabceSDavid Gibson         cpu_to_be32(max_hotplug_addr & 0xffffffff),
8233f5dabceSDavid Gibson         0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
8243f5dabceSDavid Gibson         cpu_to_be32(max_cpus / smp_threads),
8253f5dabceSDavid Gibson     };
8263f5dabceSDavid Gibson 
8273f5dabceSDavid Gibson     _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
8283f5dabceSDavid Gibson 
8293f5dabceSDavid Gibson     /* hypertas */
8303f5dabceSDavid Gibson     add_str(hypertas, "hcall-pft");
8313f5dabceSDavid Gibson     add_str(hypertas, "hcall-term");
8323f5dabceSDavid Gibson     add_str(hypertas, "hcall-dabr");
8333f5dabceSDavid Gibson     add_str(hypertas, "hcall-interrupt");
8343f5dabceSDavid Gibson     add_str(hypertas, "hcall-tce");
8353f5dabceSDavid Gibson     add_str(hypertas, "hcall-vio");
8363f5dabceSDavid Gibson     add_str(hypertas, "hcall-splpar");
8373f5dabceSDavid Gibson     add_str(hypertas, "hcall-bulk");
8383f5dabceSDavid Gibson     add_str(hypertas, "hcall-set-mode");
8393f5dabceSDavid Gibson     add_str(hypertas, "hcall-sprg0");
8403f5dabceSDavid Gibson     add_str(hypertas, "hcall-copy");
8413f5dabceSDavid Gibson     add_str(hypertas, "hcall-debug");
8423f5dabceSDavid Gibson     add_str(qemu_hypertas, "hcall-memop1");
8433f5dabceSDavid Gibson 
8443f5dabceSDavid Gibson     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
8453f5dabceSDavid Gibson         add_str(hypertas, "hcall-multi-tce");
8463f5dabceSDavid Gibson     }
8473f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
8483f5dabceSDavid Gibson                      hypertas->str, hypertas->len));
8493f5dabceSDavid Gibson     g_string_free(hypertas, TRUE);
8503f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
8513f5dabceSDavid Gibson                      qemu_hypertas->str, qemu_hypertas->len));
8523f5dabceSDavid Gibson     g_string_free(qemu_hypertas, TRUE);
8533f5dabceSDavid Gibson 
8543f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
8553f5dabceSDavid Gibson                      refpoints, sizeof(refpoints)));
8563f5dabceSDavid Gibson 
8573f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
8583f5dabceSDavid Gibson                           RTAS_ERROR_LOG_MAX));
8593f5dabceSDavid Gibson     _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
8603f5dabceSDavid Gibson                           RTAS_EVENT_SCAN_RATE));
8613f5dabceSDavid Gibson 
8623f5dabceSDavid Gibson     if (msi_nonbroken) {
8633f5dabceSDavid Gibson         _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
8643f5dabceSDavid Gibson     }
8653f5dabceSDavid Gibson 
8663f5dabceSDavid Gibson     /*
8673f5dabceSDavid Gibson      * According to PAPR, rtas ibm,os-term does not guarantee a return
8683f5dabceSDavid Gibson      * back to the guest cpu.
8693f5dabceSDavid Gibson      *
8703f5dabceSDavid Gibson      * While an additional ibm,extended-os-term property indicates
8713f5dabceSDavid Gibson      * that rtas call return will always occur. Set this property.
8723f5dabceSDavid Gibson      */
8733f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
8743f5dabceSDavid Gibson 
8753f5dabceSDavid Gibson     _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
8763f5dabceSDavid Gibson                      lrdr_capacity, sizeof(lrdr_capacity)));
8773f5dabceSDavid Gibson 
8783f5dabceSDavid Gibson     spapr_dt_rtas_tokens(fdt, rtas);
8793f5dabceSDavid Gibson }
8803f5dabceSDavid Gibson 
8819fb4541fSSam Bobroff /* Prepare ibm,arch-vec-5-platform-support, which indicates the MMU features
8829fb4541fSSam Bobroff  * that the guest may request and thus the valid values for bytes 24..26 of
8839fb4541fSSam Bobroff  * option vector 5: */
8849fb4541fSSam Bobroff static void spapr_dt_ov5_platform_support(void *fdt, int chosen)
8859fb4541fSSam Bobroff {
8869fb4541fSSam Bobroff     char val[2 * 3] = {
8879fb4541fSSam Bobroff         24, 0x00, /* Hash/Radix, filled in below. */
8889fb4541fSSam Bobroff         25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
8899fb4541fSSam Bobroff         26, 0x40, /* Radix options: GTSE == yes. */
8909fb4541fSSam Bobroff     };
8919fb4541fSSam Bobroff 
8929fb4541fSSam Bobroff     if (kvm_enabled()) {
8939fb4541fSSam Bobroff         if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
8949fb4541fSSam Bobroff             val[1] = 0x80; /* OV5_MMU_BOTH */
8959fb4541fSSam Bobroff         } else if (kvmppc_has_cap_mmu_radix()) {
8969fb4541fSSam Bobroff             val[1] = 0x40; /* OV5_MMU_RADIX_300 */
8979fb4541fSSam Bobroff         } else {
8989fb4541fSSam Bobroff             val[1] = 0x00; /* Hash */
8999fb4541fSSam Bobroff         }
9009fb4541fSSam Bobroff     } else {
9019fb4541fSSam Bobroff         /* TODO: TCG case, hash */
9029fb4541fSSam Bobroff         val[1] = 0x00;
9039fb4541fSSam Bobroff     }
9049fb4541fSSam Bobroff     _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
9059fb4541fSSam Bobroff                      val, sizeof(val)));
9069fb4541fSSam Bobroff }
9079fb4541fSSam Bobroff 
9087c866c6aSDavid Gibson static void spapr_dt_chosen(sPAPRMachineState *spapr, void *fdt)
9097c866c6aSDavid Gibson {
9107c866c6aSDavid Gibson     MachineState *machine = MACHINE(spapr);
9117c866c6aSDavid Gibson     int chosen;
9127c866c6aSDavid Gibson     const char *boot_device = machine->boot_order;
9137c866c6aSDavid Gibson     char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
9147c866c6aSDavid Gibson     size_t cb = 0;
9157c866c6aSDavid Gibson     char *bootlist = get_boot_devices_list(&cb, true);
9167c866c6aSDavid Gibson 
9177c866c6aSDavid Gibson     _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
9187c866c6aSDavid Gibson 
9197c866c6aSDavid Gibson     _FDT(fdt_setprop_string(fdt, chosen, "bootargs", machine->kernel_cmdline));
9207c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
9217c866c6aSDavid Gibson                           spapr->initrd_base));
9227c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
9237c866c6aSDavid Gibson                           spapr->initrd_base + spapr->initrd_size));
9247c866c6aSDavid Gibson 
9257c866c6aSDavid Gibson     if (spapr->kernel_size) {
9267c866c6aSDavid Gibson         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
9277c866c6aSDavid Gibson                               cpu_to_be64(spapr->kernel_size) };
9287c866c6aSDavid Gibson 
9297c866c6aSDavid Gibson         _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
9307c866c6aSDavid Gibson                          &kprop, sizeof(kprop)));
9317c866c6aSDavid Gibson         if (spapr->kernel_le) {
9327c866c6aSDavid Gibson             _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
9337c866c6aSDavid Gibson         }
9347c866c6aSDavid Gibson     }
9357c866c6aSDavid Gibson     if (boot_menu) {
9367c866c6aSDavid Gibson         _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
9377c866c6aSDavid Gibson     }
9387c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
9397c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
9407c866c6aSDavid Gibson     _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
9417c866c6aSDavid Gibson 
9427c866c6aSDavid Gibson     if (cb && bootlist) {
9437c866c6aSDavid Gibson         int i;
9447c866c6aSDavid Gibson 
9457c866c6aSDavid Gibson         for (i = 0; i < cb; i++) {
9467c866c6aSDavid Gibson             if (bootlist[i] == '\n') {
9477c866c6aSDavid Gibson                 bootlist[i] = ' ';
9487c866c6aSDavid Gibson             }
9497c866c6aSDavid Gibson         }
9507c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
9517c866c6aSDavid Gibson     }
9527c866c6aSDavid Gibson 
9537c866c6aSDavid Gibson     if (boot_device && strlen(boot_device)) {
9547c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
9557c866c6aSDavid Gibson     }
9567c866c6aSDavid Gibson 
9577c866c6aSDavid Gibson     if (!spapr->has_graphics && stdout_path) {
9587c866c6aSDavid Gibson         _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
9597c866c6aSDavid Gibson     }
9607c866c6aSDavid Gibson 
9619fb4541fSSam Bobroff     spapr_dt_ov5_platform_support(fdt, chosen);
9629fb4541fSSam Bobroff 
9637c866c6aSDavid Gibson     g_free(stdout_path);
9647c866c6aSDavid Gibson     g_free(bootlist);
9657c866c6aSDavid Gibson }
9667c866c6aSDavid Gibson 
967fca5f2dcSDavid Gibson static void spapr_dt_hypervisor(sPAPRMachineState *spapr, void *fdt)
968fca5f2dcSDavid Gibson {
969fca5f2dcSDavid Gibson     /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
970fca5f2dcSDavid Gibson      * KVM to work under pHyp with some guest co-operation */
971fca5f2dcSDavid Gibson     int hypervisor;
972fca5f2dcSDavid Gibson     uint8_t hypercall[16];
973fca5f2dcSDavid Gibson 
974fca5f2dcSDavid Gibson     _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
975fca5f2dcSDavid Gibson     /* indicate KVM hypercall interface */
976fca5f2dcSDavid Gibson     _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
977fca5f2dcSDavid Gibson     if (kvmppc_has_cap_fixup_hcalls()) {
978fca5f2dcSDavid Gibson         /*
979fca5f2dcSDavid Gibson          * Older KVM versions with older guest kernels were broken
980fca5f2dcSDavid Gibson          * with the magic page, don't allow the guest to map it.
981fca5f2dcSDavid Gibson          */
982fca5f2dcSDavid Gibson         if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
983fca5f2dcSDavid Gibson                                   sizeof(hypercall))) {
984fca5f2dcSDavid Gibson             _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
985fca5f2dcSDavid Gibson                              hypercall, sizeof(hypercall)));
986fca5f2dcSDavid Gibson         }
987fca5f2dcSDavid Gibson     }
988fca5f2dcSDavid Gibson }
989fca5f2dcSDavid Gibson 
990997b6cfcSDavid Gibson static void *spapr_build_fdt(sPAPRMachineState *spapr,
99153018216SPaolo Bonzini                              hwaddr rtas_addr,
99253018216SPaolo Bonzini                              hwaddr rtas_size)
99353018216SPaolo Bonzini {
9945b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
9953c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
996c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
9977c866c6aSDavid Gibson     int ret;
99853018216SPaolo Bonzini     void *fdt;
99953018216SPaolo Bonzini     sPAPRPHBState *phb;
1000398a0bd5SDavid Gibson     char *buf;
100153018216SPaolo Bonzini 
1002398a0bd5SDavid Gibson     fdt = g_malloc0(FDT_MAX_SIZE);
1003398a0bd5SDavid Gibson     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
100453018216SPaolo Bonzini 
1005398a0bd5SDavid Gibson     /* Root node */
1006398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
1007398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
1008398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
1009398a0bd5SDavid Gibson 
1010398a0bd5SDavid Gibson     /*
1011398a0bd5SDavid Gibson      * Add info to guest to indentify which host is it being run on
1012398a0bd5SDavid Gibson      * and what is the uuid of the guest
1013398a0bd5SDavid Gibson      */
1014398a0bd5SDavid Gibson     if (kvmppc_get_host_model(&buf)) {
1015398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1016398a0bd5SDavid Gibson         g_free(buf);
1017398a0bd5SDavid Gibson     }
1018398a0bd5SDavid Gibson     if (kvmppc_get_host_serial(&buf)) {
1019398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1020398a0bd5SDavid Gibson         g_free(buf);
1021398a0bd5SDavid Gibson     }
1022398a0bd5SDavid Gibson 
1023398a0bd5SDavid Gibson     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
1024398a0bd5SDavid Gibson 
1025398a0bd5SDavid Gibson     _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1026398a0bd5SDavid Gibson     if (qemu_uuid_set) {
1027398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1028398a0bd5SDavid Gibson     }
1029398a0bd5SDavid Gibson     g_free(buf);
1030398a0bd5SDavid Gibson 
1031398a0bd5SDavid Gibson     if (qemu_get_vm_name()) {
1032398a0bd5SDavid Gibson         _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1033398a0bd5SDavid Gibson                                 qemu_get_vm_name()));
1034398a0bd5SDavid Gibson     }
1035398a0bd5SDavid Gibson 
1036398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1037398a0bd5SDavid Gibson     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
103853018216SPaolo Bonzini 
10399b9a1908SDavid Gibson     /* /interrupt controller */
1040852ad27eSCédric Le Goater     spapr_dt_xics(spapr->nr_servers, fdt, PHANDLE_XICP);
10419b9a1908SDavid Gibson 
1042e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
1043e8f986fcSBharata B Rao     if (ret < 0) {
1044ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
1045e8f986fcSBharata B Rao         exit(1);
104653018216SPaolo Bonzini     }
104753018216SPaolo Bonzini 
1048bf5a6696SDavid Gibson     /* /vdevice */
1049bf5a6696SDavid Gibson     spapr_dt_vdevice(spapr->vio_bus, fdt);
105053018216SPaolo Bonzini 
10514d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
10524d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
10534d9392beSThomas Huth         if (ret < 0) {
1054ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
10554d9392beSThomas Huth             exit(1);
10564d9392beSThomas Huth         }
10574d9392beSThomas Huth     }
10584d9392beSThomas Huth 
105953018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
106053018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
106153018216SPaolo Bonzini         if (ret < 0) {
1062da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
106353018216SPaolo Bonzini             exit(1);
106453018216SPaolo Bonzini         }
1065da34fed7SThomas Huth     }
106653018216SPaolo Bonzini 
10670da6f3feSBharata B Rao     /* cpus */
10680da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
106953018216SPaolo Bonzini 
1070c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
1071c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
1072c20d332aSBharata B Rao     }
1073c20d332aSBharata B Rao 
1074c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
1075af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
1076af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
1077af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
1078af81cf32SBharata B Rao         if (ret < 0) {
1079af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
1080af81cf32SBharata B Rao             exit(1);
1081af81cf32SBharata B Rao         }
1082af81cf32SBharata B Rao     }
1083af81cf32SBharata B Rao 
1084ffb1e275SDavid Gibson     /* /event-sources */
1085ffbb1705SMichael Roth     spapr_dt_events(spapr, fdt);
1086ffb1e275SDavid Gibson 
10873f5dabceSDavid Gibson     /* /rtas */
10883f5dabceSDavid Gibson     spapr_dt_rtas(spapr, fdt);
10893f5dabceSDavid Gibson 
10907c866c6aSDavid Gibson     /* /chosen */
10917c866c6aSDavid Gibson     spapr_dt_chosen(spapr, fdt);
1092cf6e5223SDavid Gibson 
1093fca5f2dcSDavid Gibson     /* /hypervisor */
1094fca5f2dcSDavid Gibson     if (kvm_enabled()) {
1095fca5f2dcSDavid Gibson         spapr_dt_hypervisor(spapr, fdt);
1096fca5f2dcSDavid Gibson     }
1097fca5f2dcSDavid Gibson 
1098cf6e5223SDavid Gibson     /* Build memory reserve map */
1099cf6e5223SDavid Gibson     if (spapr->kernel_size) {
1100cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1101cf6e5223SDavid Gibson     }
1102cf6e5223SDavid Gibson     if (spapr->initrd_size) {
1103cf6e5223SDavid Gibson         _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base, spapr->initrd_size)));
1104cf6e5223SDavid Gibson     }
1105cf6e5223SDavid Gibson 
11066787d27bSMichael Roth     /* ibm,client-architecture-support updates */
11076787d27bSMichael Roth     ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
11086787d27bSMichael Roth     if (ret < 0) {
11096787d27bSMichael Roth         error_report("couldn't setup CAS properties fdt");
11106787d27bSMichael Roth         exit(1);
11116787d27bSMichael Roth     }
11126787d27bSMichael Roth 
1113997b6cfcSDavid Gibson     return fdt;
111453018216SPaolo Bonzini }
111553018216SPaolo Bonzini 
111653018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
111753018216SPaolo Bonzini {
111853018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
111953018216SPaolo Bonzini }
112053018216SPaolo Bonzini 
11211d1be34dSDavid Gibson static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
11221d1be34dSDavid Gibson                                     PowerPCCPU *cpu)
112353018216SPaolo Bonzini {
112453018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
112553018216SPaolo Bonzini 
11268d04fb55SJan Kiszka     /* The TCG path should also be holding the BQL at this point */
11278d04fb55SJan Kiszka     g_assert(qemu_mutex_iothread_locked());
11288d04fb55SJan Kiszka 
112953018216SPaolo Bonzini     if (msr_pr) {
113053018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
113153018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
113253018216SPaolo Bonzini     } else {
113353018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
113453018216SPaolo Bonzini     }
113553018216SPaolo Bonzini }
113653018216SPaolo Bonzini 
11379861bb3eSSuraj Jitindar Singh static uint64_t spapr_get_patbe(PPCVirtualHypervisor *vhyp)
11389861bb3eSSuraj Jitindar Singh {
11399861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
11409861bb3eSSuraj Jitindar Singh 
11419861bb3eSSuraj Jitindar Singh     return spapr->patb_entry;
11429861bb3eSSuraj Jitindar Singh }
11439861bb3eSSuraj Jitindar Singh 
1144e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1145e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1146e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1147e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1148e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1149e6b8fd24SSamuel Mendoza-Jonas 
1150715c5407SDavid Gibson /*
1151715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1152715c5407SDavid Gibson  */
1153715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1154715c5407SDavid Gibson {
1155715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1156715c5407SDavid Gibson         return spapr->htab_fd;
1157715c5407SDavid Gibson     }
1158715c5407SDavid Gibson 
1159715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1160715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1161715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1162715c5407SDavid Gibson                      strerror(errno));
1163715c5407SDavid Gibson     }
1164715c5407SDavid Gibson 
1165715c5407SDavid Gibson     return spapr->htab_fd;
1166715c5407SDavid Gibson }
1167715c5407SDavid Gibson 
1168b4db5413SSuraj Jitindar Singh void close_htab_fd(sPAPRMachineState *spapr)
1169715c5407SDavid Gibson {
1170715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1171715c5407SDavid Gibson         close(spapr->htab_fd);
1172715c5407SDavid Gibson     }
1173715c5407SDavid Gibson     spapr->htab_fd = -1;
1174715c5407SDavid Gibson }
1175715c5407SDavid Gibson 
1176e57ca75cSDavid Gibson static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1177e57ca75cSDavid Gibson {
1178e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1179e57ca75cSDavid Gibson 
1180e57ca75cSDavid Gibson     return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1181e57ca75cSDavid Gibson }
1182e57ca75cSDavid Gibson 
1183e57ca75cSDavid Gibson static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1184e57ca75cSDavid Gibson                                                 hwaddr ptex, int n)
1185e57ca75cSDavid Gibson {
1186e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1187e57ca75cSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1188e57ca75cSDavid Gibson 
1189e57ca75cSDavid Gibson     if (!spapr->htab) {
1190e57ca75cSDavid Gibson         /*
1191e57ca75cSDavid Gibson          * HTAB is controlled by KVM. Fetch into temporary buffer
1192e57ca75cSDavid Gibson          */
1193e57ca75cSDavid Gibson         ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1194e57ca75cSDavid Gibson         kvmppc_read_hptes(hptes, ptex, n);
1195e57ca75cSDavid Gibson         return hptes;
1196e57ca75cSDavid Gibson     }
1197e57ca75cSDavid Gibson 
1198e57ca75cSDavid Gibson     /*
1199e57ca75cSDavid Gibson      * HTAB is controlled by QEMU. Just point to the internally
1200e57ca75cSDavid Gibson      * accessible PTEG.
1201e57ca75cSDavid Gibson      */
1202e57ca75cSDavid Gibson     return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1203e57ca75cSDavid Gibson }
1204e57ca75cSDavid Gibson 
1205e57ca75cSDavid Gibson static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1206e57ca75cSDavid Gibson                               const ppc_hash_pte64_t *hptes,
1207e57ca75cSDavid Gibson                               hwaddr ptex, int n)
1208e57ca75cSDavid Gibson {
1209e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1210e57ca75cSDavid Gibson 
1211e57ca75cSDavid Gibson     if (!spapr->htab) {
1212e57ca75cSDavid Gibson         g_free((void *)hptes);
1213e57ca75cSDavid Gibson     }
1214e57ca75cSDavid Gibson 
1215e57ca75cSDavid Gibson     /* Nothing to do for qemu managed HPT */
1216e57ca75cSDavid Gibson }
1217e57ca75cSDavid Gibson 
1218e57ca75cSDavid Gibson static void spapr_store_hpte(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1219e57ca75cSDavid Gibson                              uint64_t pte0, uint64_t pte1)
1220e57ca75cSDavid Gibson {
1221e57ca75cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(vhyp);
1222e57ca75cSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
1223e57ca75cSDavid Gibson 
1224e57ca75cSDavid Gibson     if (!spapr->htab) {
1225e57ca75cSDavid Gibson         kvmppc_write_hpte(ptex, pte0, pte1);
1226e57ca75cSDavid Gibson     } else {
1227e57ca75cSDavid Gibson         stq_p(spapr->htab + offset, pte0);
1228e57ca75cSDavid Gibson         stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1229e57ca75cSDavid Gibson     }
1230e57ca75cSDavid Gibson }
1231e57ca75cSDavid Gibson 
12328dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
12338dfe8e7fSDavid Gibson {
12348dfe8e7fSDavid Gibson     int shift;
12358dfe8e7fSDavid Gibson 
12368dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
12378dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
12388dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
12398dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
12408dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
12418dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
12428dfe8e7fSDavid Gibson     return shift;
12438dfe8e7fSDavid Gibson }
12448dfe8e7fSDavid Gibson 
1245c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1246c5f54f3eSDavid Gibson                                  Error **errp)
124753018216SPaolo Bonzini {
1248c5f54f3eSDavid Gibson     long rc;
124953018216SPaolo Bonzini 
1250c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
1251c5f54f3eSDavid Gibson     g_free(spapr->htab);
1252c5f54f3eSDavid Gibson     spapr->htab = NULL;
1253c5f54f3eSDavid Gibson     spapr->htab_shift = 0;
1254c5f54f3eSDavid Gibson     close_htab_fd(spapr);
125553018216SPaolo Bonzini 
1256c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1257c5f54f3eSDavid Gibson     if (rc < 0) {
1258c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1259c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1260c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1261c5f54f3eSDavid Gibson                          shift);
1262c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1263c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1264c5f54f3eSDavid Gibson     } else if (rc > 0) {
1265c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1266c5f54f3eSDavid Gibson         if (rc != shift) {
1267c5f54f3eSDavid Gibson             error_setg(errp,
1268c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1269c5f54f3eSDavid Gibson                        shift, rc);
12707735fedaSBharata B Rao         }
12717735fedaSBharata B Rao 
127253018216SPaolo Bonzini         spapr->htab_shift = shift;
1273c18ad9a5SDavid Gibson         spapr->htab = NULL;
1274b817772aSBharata B Rao     } else {
1275c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1276c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1277c5f54f3eSDavid Gibson         int i;
127801a57972SSamuel Mendoza-Jonas 
1279c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1280c5f54f3eSDavid Gibson         if (!spapr->htab) {
1281c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1282c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1283c5f54f3eSDavid Gibson             return;
1284b817772aSBharata B Rao         }
1285b817772aSBharata B Rao 
1286c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1287c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1288b817772aSBharata B Rao 
1289c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1290c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
12917735fedaSBharata B Rao         }
129253018216SPaolo Bonzini     }
129353018216SPaolo Bonzini }
129453018216SPaolo Bonzini 
1295b4db5413SSuraj Jitindar Singh void spapr_setup_hpt_and_vrma(sPAPRMachineState *spapr)
1296b4db5413SSuraj Jitindar Singh {
1297b4db5413SSuraj Jitindar Singh     spapr_reallocate_hpt(spapr,
1298b4db5413SSuraj Jitindar Singh                      spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size),
1299b4db5413SSuraj Jitindar Singh                      &error_fatal);
1300b4db5413SSuraj Jitindar Singh     if (spapr->vrma_adjust) {
1301b4db5413SSuraj Jitindar Singh         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1302b4db5413SSuraj Jitindar Singh                                           spapr->htab_shift);
1303b4db5413SSuraj Jitindar Singh     }
1304b4db5413SSuraj Jitindar Singh     /* We're setting up a hash table, so that means we're not radix */
1305b4db5413SSuraj Jitindar Singh     spapr->patb_entry = 0;
1306b4db5413SSuraj Jitindar Singh }
1307b4db5413SSuraj Jitindar Singh 
13084f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
13099e3f9733SAlexander Graf {
13109e3f9733SAlexander Graf     bool matched = false;
13119e3f9733SAlexander Graf 
13129e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
13139e3f9733SAlexander Graf         matched = true;
13149e3f9733SAlexander Graf     }
13159e3f9733SAlexander Graf 
13169e3f9733SAlexander Graf     if (!matched) {
13179e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
13189e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
13199e3f9733SAlexander Graf         exit(1);
13209e3f9733SAlexander Graf     }
13219e3f9733SAlexander Graf }
13229e3f9733SAlexander Graf 
132353018216SPaolo Bonzini static void ppc_spapr_reset(void)
132453018216SPaolo Bonzini {
1325c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1326c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1327182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1328b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1329cae172abSDavid Gibson     hwaddr rtas_addr, fdt_addr;
1330997b6cfcSDavid Gibson     void *fdt;
1331997b6cfcSDavid Gibson     int rc;
1332259186a7SAndreas Färber 
13339e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
13349e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
13359e3f9733SAlexander Graf 
1336b4db5413SSuraj Jitindar Singh     if (kvm_enabled() && kvmppc_has_cap_mmu_radix()) {
1337b4db5413SSuraj Jitindar Singh         /* If using KVM with radix mode available, VCPUs can be started
1338b4db5413SSuraj Jitindar Singh          * without a HPT because KVM will start them in radix mode.
1339b4db5413SSuraj Jitindar Singh          * Set the GR bit in PATB so that we know there is no HPT. */
1340b4db5413SSuraj Jitindar Singh         spapr->patb_entry = PATBE1_GR;
1341b4db5413SSuraj Jitindar Singh     } else {
13429861bb3eSSuraj Jitindar Singh         spapr->patb_entry = 0;
1343b4db5413SSuraj Jitindar Singh         spapr_setup_hpt_and_vrma(spapr);
1344c5f54f3eSDavid Gibson     }
134553018216SPaolo Bonzini 
134653018216SPaolo Bonzini     qemu_devices_reset();
134753018216SPaolo Bonzini 
1348b7d1f77aSBenjamin Herrenschmidt     /*
1349b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1350b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1351b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1352b7d1f77aSBenjamin Herrenschmidt      */
1353b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1354cae172abSDavid Gibson     rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1355cae172abSDavid Gibson     fdt_addr = rtas_addr - FDT_MAX_SIZE;
1356b7d1f77aSBenjamin Herrenschmidt 
13576787d27bSMichael Roth     /* if this reset wasn't generated by CAS, we should reset our
13586787d27bSMichael Roth      * negotiated options and start from scratch */
13596787d27bSMichael Roth     if (!spapr->cas_reboot) {
13606787d27bSMichael Roth         spapr_ovec_cleanup(spapr->ov5_cas);
13616787d27bSMichael Roth         spapr->ov5_cas = spapr_ovec_new();
13626787d27bSMichael Roth     }
13636787d27bSMichael Roth 
1364cae172abSDavid Gibson     fdt = spapr_build_fdt(spapr, rtas_addr, spapr->rtas_size);
136553018216SPaolo Bonzini 
13662cac78c1SDavid Gibson     spapr_load_rtas(spapr, fdt, rtas_addr);
1367b7d1f77aSBenjamin Herrenschmidt 
1368997b6cfcSDavid Gibson     rc = fdt_pack(fdt);
1369997b6cfcSDavid Gibson 
1370997b6cfcSDavid Gibson     /* Should only fail if we've built a corrupted tree */
1371997b6cfcSDavid Gibson     assert(rc == 0);
1372997b6cfcSDavid Gibson 
1373997b6cfcSDavid Gibson     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1374997b6cfcSDavid Gibson         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
1375997b6cfcSDavid Gibson                      fdt_totalsize(fdt), FDT_MAX_SIZE);
1376997b6cfcSDavid Gibson         exit(1);
1377997b6cfcSDavid Gibson     }
1378997b6cfcSDavid Gibson 
1379997b6cfcSDavid Gibson     /* Load the fdt */
1380997b6cfcSDavid Gibson     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
1381cae172abSDavid Gibson     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
1382997b6cfcSDavid Gibson     g_free(fdt);
1383997b6cfcSDavid Gibson 
138453018216SPaolo Bonzini     /* Set up the entry state */
1385182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1386cae172abSDavid Gibson     first_ppc_cpu->env.gpr[3] = fdt_addr;
1387182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1388182735efSAndreas Färber     first_cpu->halted = 0;
13891b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
139053018216SPaolo Bonzini 
13916787d27bSMichael Roth     spapr->cas_reboot = false;
139253018216SPaolo Bonzini }
139353018216SPaolo Bonzini 
139428e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
139553018216SPaolo Bonzini {
13962ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
13973978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
139853018216SPaolo Bonzini 
13993978b863SPaolo Bonzini     if (dinfo) {
14006231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
14016231a6daSMarkus Armbruster                             &error_fatal);
140253018216SPaolo Bonzini     }
140353018216SPaolo Bonzini 
140453018216SPaolo Bonzini     qdev_init_nofail(dev);
140553018216SPaolo Bonzini 
140653018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
140753018216SPaolo Bonzini }
140853018216SPaolo Bonzini 
140928e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
141028df36a1SDavid Gibson {
1411147ff807SCédric Le Goater     object_initialize(&spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC);
1412147ff807SCédric Le Goater     object_property_add_child(OBJECT(spapr), "rtc", OBJECT(&spapr->rtc),
1413147ff807SCédric Le Goater                               &error_fatal);
1414147ff807SCédric Le Goater     object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1415147ff807SCédric Le Goater                               &error_fatal);
1416147ff807SCédric Le Goater     object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1417147ff807SCédric Le Goater                               "date", &error_fatal);
141828df36a1SDavid Gibson }
141928df36a1SDavid Gibson 
142053018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
142114c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
142253018216SPaolo Bonzini {
142353018216SPaolo Bonzini     switch (vga_interface_type) {
142453018216SPaolo Bonzini     case VGA_NONE:
14257effdaa3SMark Wu         return false;
14267effdaa3SMark Wu     case VGA_DEVICE:
14277effdaa3SMark Wu         return true;
142853018216SPaolo Bonzini     case VGA_STD:
1429b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
143053018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
143153018216SPaolo Bonzini     default:
143214c6a894SDavid Gibson         error_setg(errp,
143314c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
143414c6a894SDavid Gibson         return false;
143553018216SPaolo Bonzini     }
143653018216SPaolo Bonzini }
143753018216SPaolo Bonzini 
1438880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1439880ae7deSDavid Gibson {
144028e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1441880ae7deSDavid Gibson     int err = 0;
1442880ae7deSDavid Gibson 
1443a7ff1212SCédric Le Goater     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
1444a7ff1212SCédric Le Goater         int i;
1445a7ff1212SCédric Le Goater         for (i = 0; i < spapr->nr_servers; i++) {
1446a7ff1212SCédric Le Goater             icp_resend(&spapr->icps[i]);
1447a7ff1212SCédric Le Goater         }
1448a7ff1212SCédric Le Goater     }
1449a7ff1212SCédric Le Goater 
1450631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1451880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1452880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1453880ae7deSDavid Gibson      * value into the RTC device */
1454880ae7deSDavid Gibson     if (version_id < 3) {
1455147ff807SCédric Le Goater         err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
1456880ae7deSDavid Gibson     }
1457880ae7deSDavid Gibson 
1458880ae7deSDavid Gibson     return err;
1459880ae7deSDavid Gibson }
1460880ae7deSDavid Gibson 
1461880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1462880ae7deSDavid Gibson {
1463880ae7deSDavid Gibson     return version_id < 3;
1464880ae7deSDavid Gibson }
1465880ae7deSDavid Gibson 
146662ef3760SMichael Roth static bool spapr_ov5_cas_needed(void *opaque)
146762ef3760SMichael Roth {
146862ef3760SMichael Roth     sPAPRMachineState *spapr = opaque;
146962ef3760SMichael Roth     sPAPROptionVector *ov5_mask = spapr_ovec_new();
147062ef3760SMichael Roth     sPAPROptionVector *ov5_legacy = spapr_ovec_new();
147162ef3760SMichael Roth     sPAPROptionVector *ov5_removed = spapr_ovec_new();
147262ef3760SMichael Roth     bool cas_needed;
147362ef3760SMichael Roth 
147462ef3760SMichael Roth     /* Prior to the introduction of sPAPROptionVector, we had two option
147562ef3760SMichael Roth      * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
147662ef3760SMichael Roth      * Both of these options encode machine topology into the device-tree
147762ef3760SMichael Roth      * in such a way that the now-booted OS should still be able to interact
147862ef3760SMichael Roth      * appropriately with QEMU regardless of what options were actually
147962ef3760SMichael Roth      * negotiatied on the source side.
148062ef3760SMichael Roth      *
148162ef3760SMichael Roth      * As such, we can avoid migrating the CAS-negotiated options if these
148262ef3760SMichael Roth      * are the only options available on the current machine/platform.
148362ef3760SMichael Roth      * Since these are the only options available for pseries-2.7 and
148462ef3760SMichael Roth      * earlier, this allows us to maintain old->new/new->old migration
148562ef3760SMichael Roth      * compatibility.
148662ef3760SMichael Roth      *
148762ef3760SMichael Roth      * For QEMU 2.8+, there are additional CAS-negotiatable options available
148862ef3760SMichael Roth      * via default pseries-2.8 machines and explicit command-line parameters.
148962ef3760SMichael Roth      * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
149062ef3760SMichael Roth      * of the actual CAS-negotiated values to continue working properly. For
149162ef3760SMichael Roth      * example, availability of memory unplug depends on knowing whether
149262ef3760SMichael Roth      * OV5_HP_EVT was negotiated via CAS.
149362ef3760SMichael Roth      *
149462ef3760SMichael Roth      * Thus, for any cases where the set of available CAS-negotiatable
149562ef3760SMichael Roth      * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
149662ef3760SMichael Roth      * include the CAS-negotiated options in the migration stream.
149762ef3760SMichael Roth      */
149862ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
149962ef3760SMichael Roth     spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
150062ef3760SMichael Roth 
150162ef3760SMichael Roth     /* spapr_ovec_diff returns true if bits were removed. we avoid using
150262ef3760SMichael Roth      * the mask itself since in the future it's possible "legacy" bits may be
150362ef3760SMichael Roth      * removed via machine options, which could generate a false positive
150462ef3760SMichael Roth      * that breaks migration.
150562ef3760SMichael Roth      */
150662ef3760SMichael Roth     spapr_ovec_intersect(ov5_legacy, spapr->ov5, ov5_mask);
150762ef3760SMichael Roth     cas_needed = spapr_ovec_diff(ov5_removed, spapr->ov5, ov5_legacy);
150862ef3760SMichael Roth 
150962ef3760SMichael Roth     spapr_ovec_cleanup(ov5_mask);
151062ef3760SMichael Roth     spapr_ovec_cleanup(ov5_legacy);
151162ef3760SMichael Roth     spapr_ovec_cleanup(ov5_removed);
151262ef3760SMichael Roth 
151362ef3760SMichael Roth     return cas_needed;
151462ef3760SMichael Roth }
151562ef3760SMichael Roth 
151662ef3760SMichael Roth static const VMStateDescription vmstate_spapr_ov5_cas = {
151762ef3760SMichael Roth     .name = "spapr_option_vector_ov5_cas",
151862ef3760SMichael Roth     .version_id = 1,
151962ef3760SMichael Roth     .minimum_version_id = 1,
152062ef3760SMichael Roth     .needed = spapr_ov5_cas_needed,
152162ef3760SMichael Roth     .fields = (VMStateField[]) {
152262ef3760SMichael Roth         VMSTATE_STRUCT_POINTER_V(ov5_cas, sPAPRMachineState, 1,
152362ef3760SMichael Roth                                  vmstate_spapr_ovec, sPAPROptionVector),
152462ef3760SMichael Roth         VMSTATE_END_OF_LIST()
152562ef3760SMichael Roth     },
152662ef3760SMichael Roth };
152762ef3760SMichael Roth 
15289861bb3eSSuraj Jitindar Singh static bool spapr_patb_entry_needed(void *opaque)
15299861bb3eSSuraj Jitindar Singh {
15309861bb3eSSuraj Jitindar Singh     sPAPRMachineState *spapr = opaque;
15319861bb3eSSuraj Jitindar Singh 
15329861bb3eSSuraj Jitindar Singh     return !!spapr->patb_entry;
15339861bb3eSSuraj Jitindar Singh }
15349861bb3eSSuraj Jitindar Singh 
15359861bb3eSSuraj Jitindar Singh static const VMStateDescription vmstate_spapr_patb_entry = {
15369861bb3eSSuraj Jitindar Singh     .name = "spapr_patb_entry",
15379861bb3eSSuraj Jitindar Singh     .version_id = 1,
15389861bb3eSSuraj Jitindar Singh     .minimum_version_id = 1,
15399861bb3eSSuraj Jitindar Singh     .needed = spapr_patb_entry_needed,
15409861bb3eSSuraj Jitindar Singh     .fields = (VMStateField[]) {
15419861bb3eSSuraj Jitindar Singh         VMSTATE_UINT64(patb_entry, sPAPRMachineState),
15429861bb3eSSuraj Jitindar Singh         VMSTATE_END_OF_LIST()
15439861bb3eSSuraj Jitindar Singh     },
15449861bb3eSSuraj Jitindar Singh };
15459861bb3eSSuraj Jitindar Singh 
15464be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
15474be21d56SDavid Gibson     .name = "spapr",
1548880ae7deSDavid Gibson     .version_id = 3,
15494be21d56SDavid Gibson     .minimum_version_id = 1,
1550880ae7deSDavid Gibson     .post_load = spapr_post_load,
15514be21d56SDavid Gibson     .fields = (VMStateField[]) {
1552880ae7deSDavid Gibson         /* used to be @next_irq */
1553880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
15544be21d56SDavid Gibson 
15554be21d56SDavid Gibson         /* RTC offset */
155628e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1557880ae7deSDavid Gibson 
155828e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
15594be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
15604be21d56SDavid Gibson     },
156162ef3760SMichael Roth     .subsections = (const VMStateDescription*[]) {
156262ef3760SMichael Roth         &vmstate_spapr_ov5_cas,
15639861bb3eSSuraj Jitindar Singh         &vmstate_spapr_patb_entry,
156462ef3760SMichael Roth         NULL
156562ef3760SMichael Roth     }
15664be21d56SDavid Gibson };
15674be21d56SDavid Gibson 
15684be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
15694be21d56SDavid Gibson {
157028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
15714be21d56SDavid Gibson 
15724be21d56SDavid Gibson     /* "Iteration" header */
15734be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
15744be21d56SDavid Gibson 
1575e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1576e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1577e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1578e68cb8b4SAlexey Kardashevskiy     } else {
1579e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
15804be21d56SDavid Gibson     }
15814be21d56SDavid Gibson 
1582e68cb8b4SAlexey Kardashevskiy 
1583e68cb8b4SAlexey Kardashevskiy     return 0;
1584e68cb8b4SAlexey Kardashevskiy }
15854be21d56SDavid Gibson 
158628e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
15874be21d56SDavid Gibson                                  int64_t max_ns)
15884be21d56SDavid Gibson {
1589378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
15904be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
15914be21d56SDavid Gibson     int index = spapr->htab_save_index;
1592bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
15934be21d56SDavid Gibson 
15944be21d56SDavid Gibson     assert(spapr->htab_first_pass);
15954be21d56SDavid Gibson 
15964be21d56SDavid Gibson     do {
15974be21d56SDavid Gibson         int chunkstart;
15984be21d56SDavid Gibson 
15994be21d56SDavid Gibson         /* Consume invalid HPTEs */
16004be21d56SDavid Gibson         while ((index < htabslots)
16014be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
16024be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
160324ec2863SMarc-André Lureau             index++;
16044be21d56SDavid Gibson         }
16054be21d56SDavid Gibson 
16064be21d56SDavid Gibson         /* Consume valid HPTEs */
16074be21d56SDavid Gibson         chunkstart = index;
1608338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
16094be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
16104be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
161124ec2863SMarc-André Lureau             index++;
16124be21d56SDavid Gibson         }
16134be21d56SDavid Gibson 
16144be21d56SDavid Gibson         if (index > chunkstart) {
16154be21d56SDavid Gibson             int n_valid = index - chunkstart;
16164be21d56SDavid Gibson 
16174be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
16184be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
16194be21d56SDavid Gibson             qemu_put_be16(f, 0);
16204be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
16214be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
16224be21d56SDavid Gibson 
1623378bc217SDavid Gibson             if (has_timeout &&
1624378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
16254be21d56SDavid Gibson                 break;
16264be21d56SDavid Gibson             }
16274be21d56SDavid Gibson         }
16284be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
16294be21d56SDavid Gibson 
16304be21d56SDavid Gibson     if (index >= htabslots) {
16314be21d56SDavid Gibson         assert(index == htabslots);
16324be21d56SDavid Gibson         index = 0;
16334be21d56SDavid Gibson         spapr->htab_first_pass = false;
16344be21d56SDavid Gibson     }
16354be21d56SDavid Gibson     spapr->htab_save_index = index;
16364be21d56SDavid Gibson }
16374be21d56SDavid Gibson 
163828e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
16394be21d56SDavid Gibson                                 int64_t max_ns)
16404be21d56SDavid Gibson {
16414be21d56SDavid Gibson     bool final = max_ns < 0;
16424be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
16434be21d56SDavid Gibson     int examined = 0, sent = 0;
16444be21d56SDavid Gibson     int index = spapr->htab_save_index;
1645bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
16464be21d56SDavid Gibson 
16474be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
16484be21d56SDavid Gibson 
16494be21d56SDavid Gibson     do {
16504be21d56SDavid Gibson         int chunkstart, invalidstart;
16514be21d56SDavid Gibson 
16524be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
16534be21d56SDavid Gibson         while ((index < htabslots)
16544be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
16554be21d56SDavid Gibson             index++;
16564be21d56SDavid Gibson             examined++;
16574be21d56SDavid Gibson         }
16584be21d56SDavid Gibson 
16594be21d56SDavid Gibson         chunkstart = index;
16604be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1661338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
16624be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
16634be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
16644be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
16654be21d56SDavid Gibson             index++;
16664be21d56SDavid Gibson             examined++;
16674be21d56SDavid Gibson         }
16684be21d56SDavid Gibson 
16694be21d56SDavid Gibson         invalidstart = index;
16704be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1671338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
16724be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
16734be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
16744be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
16754be21d56SDavid Gibson             index++;
16764be21d56SDavid Gibson             examined++;
16774be21d56SDavid Gibson         }
16784be21d56SDavid Gibson 
16794be21d56SDavid Gibson         if (index > chunkstart) {
16804be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
16814be21d56SDavid Gibson             int n_invalid = index - invalidstart;
16824be21d56SDavid Gibson 
16834be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
16844be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
16854be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
16864be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
16874be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
16884be21d56SDavid Gibson             sent += index - chunkstart;
16894be21d56SDavid Gibson 
1690bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
16914be21d56SDavid Gibson                 break;
16924be21d56SDavid Gibson             }
16934be21d56SDavid Gibson         }
16944be21d56SDavid Gibson 
16954be21d56SDavid Gibson         if (examined >= htabslots) {
16964be21d56SDavid Gibson             break;
16974be21d56SDavid Gibson         }
16984be21d56SDavid Gibson 
16994be21d56SDavid Gibson         if (index >= htabslots) {
17004be21d56SDavid Gibson             assert(index == htabslots);
17014be21d56SDavid Gibson             index = 0;
17024be21d56SDavid Gibson         }
17034be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
17044be21d56SDavid Gibson 
17054be21d56SDavid Gibson     if (index >= htabslots) {
17064be21d56SDavid Gibson         assert(index == htabslots);
17074be21d56SDavid Gibson         index = 0;
17084be21d56SDavid Gibson     }
17094be21d56SDavid Gibson 
17104be21d56SDavid Gibson     spapr->htab_save_index = index;
17114be21d56SDavid Gibson 
1712e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
17134be21d56SDavid Gibson }
17144be21d56SDavid Gibson 
1715e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1716e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1717e68cb8b4SAlexey Kardashevskiy 
17184be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
17194be21d56SDavid Gibson {
172028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1721715c5407SDavid Gibson     int fd;
1722e68cb8b4SAlexey Kardashevskiy     int rc = 0;
17234be21d56SDavid Gibson 
17244be21d56SDavid Gibson     /* Iteration header */
17254be21d56SDavid Gibson     qemu_put_be32(f, 0);
17264be21d56SDavid Gibson 
1727e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1728e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1729e68cb8b4SAlexey Kardashevskiy 
1730715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1731715c5407SDavid Gibson         if (fd < 0) {
1732715c5407SDavid Gibson             return fd;
173301a57972SSamuel Mendoza-Jonas         }
173401a57972SSamuel Mendoza-Jonas 
1735715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1736e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1737e68cb8b4SAlexey Kardashevskiy             return rc;
1738e68cb8b4SAlexey Kardashevskiy         }
1739e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
17404be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
17414be21d56SDavid Gibson     } else {
1742e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
17434be21d56SDavid Gibson     }
17444be21d56SDavid Gibson 
17454be21d56SDavid Gibson     /* End marker */
17464be21d56SDavid Gibson     qemu_put_be32(f, 0);
17474be21d56SDavid Gibson     qemu_put_be16(f, 0);
17484be21d56SDavid Gibson     qemu_put_be16(f, 0);
17494be21d56SDavid Gibson 
1750e68cb8b4SAlexey Kardashevskiy     return rc;
17514be21d56SDavid Gibson }
17524be21d56SDavid Gibson 
17534be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
17544be21d56SDavid Gibson {
175528e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1756715c5407SDavid Gibson     int fd;
17574be21d56SDavid Gibson 
17584be21d56SDavid Gibson     /* Iteration header */
17594be21d56SDavid Gibson     qemu_put_be32(f, 0);
17604be21d56SDavid Gibson 
1761e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1762e68cb8b4SAlexey Kardashevskiy         int rc;
1763e68cb8b4SAlexey Kardashevskiy 
1764e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1765e68cb8b4SAlexey Kardashevskiy 
1766715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1767715c5407SDavid Gibson         if (fd < 0) {
1768715c5407SDavid Gibson             return fd;
176901a57972SSamuel Mendoza-Jonas         }
177001a57972SSamuel Mendoza-Jonas 
1771715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1772e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1773e68cb8b4SAlexey Kardashevskiy             return rc;
1774e68cb8b4SAlexey Kardashevskiy         }
1775e68cb8b4SAlexey Kardashevskiy     } else {
1776378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1777378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1778378bc217SDavid Gibson         }
17794be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1780e68cb8b4SAlexey Kardashevskiy     }
17814be21d56SDavid Gibson 
17824be21d56SDavid Gibson     /* End marker */
17834be21d56SDavid Gibson     qemu_put_be32(f, 0);
17844be21d56SDavid Gibson     qemu_put_be16(f, 0);
17854be21d56SDavid Gibson     qemu_put_be16(f, 0);
17864be21d56SDavid Gibson 
17874be21d56SDavid Gibson     return 0;
17884be21d56SDavid Gibson }
17894be21d56SDavid Gibson 
17904be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
17914be21d56SDavid Gibson {
179228e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
17934be21d56SDavid Gibson     uint32_t section_hdr;
1794e68cb8b4SAlexey Kardashevskiy     int fd = -1;
17954be21d56SDavid Gibson 
17964be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
179798a5d100SDavid Gibson         error_report("htab_load() bad version");
17984be21d56SDavid Gibson         return -EINVAL;
17994be21d56SDavid Gibson     }
18004be21d56SDavid Gibson 
18014be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
18024be21d56SDavid Gibson 
18034be21d56SDavid Gibson     if (section_hdr) {
18049897e462SGreg Kurz         Error *local_err = NULL;
1805c5f54f3eSDavid Gibson 
1806c5f54f3eSDavid Gibson         /* First section gives the htab size */
1807c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1808c5f54f3eSDavid Gibson         if (local_err) {
1809c5f54f3eSDavid Gibson             error_report_err(local_err);
18104be21d56SDavid Gibson             return -EINVAL;
18114be21d56SDavid Gibson         }
18124be21d56SDavid Gibson         return 0;
18134be21d56SDavid Gibson     }
18144be21d56SDavid Gibson 
1815e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1816e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1817e68cb8b4SAlexey Kardashevskiy 
1818e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1819e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
182098a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1821e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1822e68cb8b4SAlexey Kardashevskiy         }
1823e68cb8b4SAlexey Kardashevskiy     }
1824e68cb8b4SAlexey Kardashevskiy 
18254be21d56SDavid Gibson     while (true) {
18264be21d56SDavid Gibson         uint32_t index;
18274be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
18284be21d56SDavid Gibson 
18294be21d56SDavid Gibson         index = qemu_get_be32(f);
18304be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
18314be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
18324be21d56SDavid Gibson 
18334be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
18344be21d56SDavid Gibson             /* End of Stream */
18354be21d56SDavid Gibson             break;
18364be21d56SDavid Gibson         }
18374be21d56SDavid Gibson 
1838e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
18394be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
18404be21d56SDavid Gibson             /* Bad index in stream */
184198a5d100SDavid Gibson             error_report(
184298a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
184398a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
18444be21d56SDavid Gibson             return -EINVAL;
18454be21d56SDavid Gibson         }
18464be21d56SDavid Gibson 
1847e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
18484be21d56SDavid Gibson             if (n_valid) {
18494be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
18504be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
18514be21d56SDavid Gibson             }
18524be21d56SDavid Gibson             if (n_invalid) {
18534be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
18544be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
18554be21d56SDavid Gibson             }
1856e68cb8b4SAlexey Kardashevskiy         } else {
1857e68cb8b4SAlexey Kardashevskiy             int rc;
1858e68cb8b4SAlexey Kardashevskiy 
1859e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1860e68cb8b4SAlexey Kardashevskiy 
1861e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1862e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1863e68cb8b4SAlexey Kardashevskiy                 return rc;
1864e68cb8b4SAlexey Kardashevskiy             }
1865e68cb8b4SAlexey Kardashevskiy         }
1866e68cb8b4SAlexey Kardashevskiy     }
1867e68cb8b4SAlexey Kardashevskiy 
1868e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1869e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1870e68cb8b4SAlexey Kardashevskiy         close(fd);
18714be21d56SDavid Gibson     }
18724be21d56SDavid Gibson 
18734be21d56SDavid Gibson     return 0;
18744be21d56SDavid Gibson }
18754be21d56SDavid Gibson 
1876c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1877c573fc03SThomas Huth {
1878c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1879c573fc03SThomas Huth 
1880c573fc03SThomas Huth     close_htab_fd(spapr);
1881c573fc03SThomas Huth }
1882c573fc03SThomas Huth 
18834be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
18844be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
18854be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1886a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1887c573fc03SThomas Huth     .cleanup = htab_cleanup,
18884be21d56SDavid Gibson     .load_state = htab_load,
18894be21d56SDavid Gibson };
18904be21d56SDavid Gibson 
18915b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
18925b2128d2SAlexander Graf                            Error **errp)
18935b2128d2SAlexander Graf {
18945b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
18955b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
18965b2128d2SAlexander Graf }
18975b2128d2SAlexander Graf 
1898224245bfSDavid Gibson /*
1899224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1900224245bfSDavid Gibson  *
1901224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1902224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1903224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1904224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1905224245bfSDavid Gibson  */
1906224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1907224245bfSDavid Gibson {
1908224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1909224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1910224245bfSDavid Gibson 
1911224245bfSDavid Gibson     if (d) {
1912224245bfSDavid Gibson         device_reset(d);
1913224245bfSDavid Gibson     }
1914224245bfSDavid Gibson }
1915224245bfSDavid Gibson 
1916224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1917224245bfSDavid Gibson {
1918224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1919224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1920e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1921224245bfSDavid Gibson     int i;
1922224245bfSDavid Gibson 
1923224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1924224245bfSDavid Gibson         sPAPRDRConnector *drc;
1925224245bfSDavid Gibson         uint64_t addr;
1926224245bfSDavid Gibson 
1927e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1928224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1929224245bfSDavid Gibson                                      addr/lmb_size);
1930224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1931224245bfSDavid Gibson     }
1932224245bfSDavid Gibson }
1933224245bfSDavid Gibson 
1934224245bfSDavid Gibson /*
1935224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1936224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1937224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1938224245bfSDavid Gibson  */
19397c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1940224245bfSDavid Gibson {
1941224245bfSDavid Gibson     int i;
1942224245bfSDavid Gibson 
19437c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
19447c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
19457c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
19467c150d6fSDavid Gibson                    machine->ram_size,
1947224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
19487c150d6fSDavid Gibson         return;
19497c150d6fSDavid Gibson     }
19507c150d6fSDavid Gibson 
19517c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
19527c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
19537c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
19547c150d6fSDavid Gibson                    machine->ram_size,
19557c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
19567c150d6fSDavid Gibson         return;
1957224245bfSDavid Gibson     }
1958224245bfSDavid Gibson 
1959224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1960224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
19617c150d6fSDavid Gibson             error_setg(errp,
19627c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
19637c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
19647c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
1965224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
19667c150d6fSDavid Gibson             return;
1967224245bfSDavid Gibson         }
1968224245bfSDavid Gibson     }
1969224245bfSDavid Gibson }
1970224245bfSDavid Gibson 
1971535455fdSIgor Mammedov /* find cpu slot in machine->possible_cpus by core_id */
1972535455fdSIgor Mammedov static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1973535455fdSIgor Mammedov {
1974535455fdSIgor Mammedov     int index = id / smp_threads;
1975535455fdSIgor Mammedov 
1976535455fdSIgor Mammedov     if (index >= ms->possible_cpus->len) {
1977535455fdSIgor Mammedov         return NULL;
1978535455fdSIgor Mammedov     }
1979535455fdSIgor Mammedov     if (idx) {
1980535455fdSIgor Mammedov         *idx = index;
1981535455fdSIgor Mammedov     }
1982535455fdSIgor Mammedov     return &ms->possible_cpus->cpus[index];
1983535455fdSIgor Mammedov }
1984535455fdSIgor Mammedov 
19850c86d0fdSDavid Gibson static void spapr_init_cpus(sPAPRMachineState *spapr)
19860c86d0fdSDavid Gibson {
19870c86d0fdSDavid Gibson     MachineState *machine = MACHINE(spapr);
19880c86d0fdSDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
19890c86d0fdSDavid Gibson     char *type = spapr_get_cpu_core_type(machine->cpu_model);
19900c86d0fdSDavid Gibson     int smt = kvmppc_smt_threads();
1991535455fdSIgor Mammedov     const CPUArchIdList *possible_cpus;
1992535455fdSIgor Mammedov     int boot_cores_nr = smp_cpus / smp_threads;
19930c86d0fdSDavid Gibson     int i;
19940c86d0fdSDavid Gibson 
19950c86d0fdSDavid Gibson     if (!type) {
19960c86d0fdSDavid Gibson         error_report("Unable to find sPAPR CPU Core definition");
19970c86d0fdSDavid Gibson         exit(1);
19980c86d0fdSDavid Gibson     }
19990c86d0fdSDavid Gibson 
2000535455fdSIgor Mammedov     possible_cpus = mc->possible_cpu_arch_ids(machine);
2001c5514d0eSIgor Mammedov     if (mc->has_hotpluggable_cpus) {
20020c86d0fdSDavid Gibson         if (smp_cpus % smp_threads) {
20030c86d0fdSDavid Gibson             error_report("smp_cpus (%u) must be multiple of threads (%u)",
20040c86d0fdSDavid Gibson                          smp_cpus, smp_threads);
20050c86d0fdSDavid Gibson             exit(1);
20060c86d0fdSDavid Gibson         }
20070c86d0fdSDavid Gibson         if (max_cpus % smp_threads) {
20080c86d0fdSDavid Gibson             error_report("max_cpus (%u) must be multiple of threads (%u)",
20090c86d0fdSDavid Gibson                          max_cpus, smp_threads);
20100c86d0fdSDavid Gibson             exit(1);
20110c86d0fdSDavid Gibson         }
20120c86d0fdSDavid Gibson     } else {
20130c86d0fdSDavid Gibson         if (max_cpus != smp_cpus) {
20140c86d0fdSDavid Gibson             error_report("This machine version does not support CPU hotplug");
20150c86d0fdSDavid Gibson             exit(1);
20160c86d0fdSDavid Gibson         }
2017535455fdSIgor Mammedov         boot_cores_nr = possible_cpus->len;
20180c86d0fdSDavid Gibson     }
20190c86d0fdSDavid Gibson 
2020535455fdSIgor Mammedov     for (i = 0; i < possible_cpus->len; i++) {
20210c86d0fdSDavid Gibson         int core_id = i * smp_threads;
20220c86d0fdSDavid Gibson 
2023c5514d0eSIgor Mammedov         if (mc->has_hotpluggable_cpus) {
20240c86d0fdSDavid Gibson             sPAPRDRConnector *drc =
20250c86d0fdSDavid Gibson                 spapr_dr_connector_new(OBJECT(spapr),
20260c86d0fdSDavid Gibson                                        SPAPR_DR_CONNECTOR_TYPE_CPU,
20270c86d0fdSDavid Gibson                                        (core_id / smp_threads) * smt);
20280c86d0fdSDavid Gibson 
20290c86d0fdSDavid Gibson             qemu_register_reset(spapr_drc_reset, drc);
20300c86d0fdSDavid Gibson         }
20310c86d0fdSDavid Gibson 
2032535455fdSIgor Mammedov         if (i < boot_cores_nr) {
20330c86d0fdSDavid Gibson             Object *core  = object_new(type);
20340c86d0fdSDavid Gibson             int nr_threads = smp_threads;
20350c86d0fdSDavid Gibson 
20360c86d0fdSDavid Gibson             /* Handle the partially filled core for older machine types */
20370c86d0fdSDavid Gibson             if ((i + 1) * smp_threads >= smp_cpus) {
20380c86d0fdSDavid Gibson                 nr_threads = smp_cpus - i * smp_threads;
20390c86d0fdSDavid Gibson             }
20400c86d0fdSDavid Gibson 
20410c86d0fdSDavid Gibson             object_property_set_int(core, nr_threads, "nr-threads",
20420c86d0fdSDavid Gibson                                     &error_fatal);
20430c86d0fdSDavid Gibson             object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
20440c86d0fdSDavid Gibson                                     &error_fatal);
20450c86d0fdSDavid Gibson             object_property_set_bool(core, true, "realized", &error_fatal);
20460c86d0fdSDavid Gibson         }
20470c86d0fdSDavid Gibson     }
20480c86d0fdSDavid Gibson     g_free(type);
20490c86d0fdSDavid Gibson }
20500c86d0fdSDavid Gibson 
205153018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
20523ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
205353018216SPaolo Bonzini {
205428e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
2055224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
20563ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
20573ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
205853018216SPaolo Bonzini     PCIHostState *phb;
205953018216SPaolo Bonzini     int i;
206053018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
206153018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
2062658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
2063658fa66bSAlexey Kardashevskiy     void *rma = NULL;
206453018216SPaolo Bonzini     hwaddr rma_alloc_size;
2065b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
2066b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
206753018216SPaolo Bonzini     char *filename;
206894a94e4cSBharata B Rao     int smt = kvmppc_smt_threads();
206953018216SPaolo Bonzini 
2070226419d6SMichael S. Tsirkin     msi_nonbroken = true;
207153018216SPaolo Bonzini 
207253018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
207353018216SPaolo Bonzini 
207453018216SPaolo Bonzini     /* Allocate RMA if necessary */
2075658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
207653018216SPaolo Bonzini 
207753018216SPaolo Bonzini     if (rma_alloc_size == -1) {
2078730fce59SThomas Huth         error_report("Unable to create RMA");
207953018216SPaolo Bonzini         exit(1);
208053018216SPaolo Bonzini     }
208153018216SPaolo Bonzini 
2082c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
208353018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
208453018216SPaolo Bonzini     } else {
2085c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
208653018216SPaolo Bonzini 
208753018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
208853018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
208953018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
209053018216SPaolo Bonzini          *
209153018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
209253018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
209353018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
209453018216SPaolo Bonzini          * isn't determined yet.
209553018216SPaolo Bonzini          */
209653018216SPaolo Bonzini         if (kvm_enabled()) {
209753018216SPaolo Bonzini             spapr->vrma_adjust = 1;
209853018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
209953018216SPaolo Bonzini         }
2100912acdf4SBenjamin Herrenschmidt 
2101912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
2102912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
2103912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
2104912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
2105912acdf4SBenjamin Herrenschmidt          */
2106912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
210753018216SPaolo Bonzini     }
210853018216SPaolo Bonzini 
2109c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
2110d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2111c4177479SAlexey Kardashevskiy                      spapr->rma_size);
2112c4177479SAlexey Kardashevskiy         exit(1);
2113c4177479SAlexey Kardashevskiy     }
2114c4177479SAlexey Kardashevskiy 
2115b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2116b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
211753018216SPaolo Bonzini 
21187b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
2119e6f7e110SCédric Le Goater     xics_system_init(machine, DIV_ROUND_UP(max_cpus * smt, smp_threads),
2120161deaf2SBenjamin Herrenschmidt                      XICS_IRQS_SPAPR, &error_fatal);
21217b565160SDavid Gibson 
2122facdb8b6SMichael Roth     /* Set up containers for ibm,client-set-architecture negotiated options */
2123facdb8b6SMichael Roth     spapr->ov5 = spapr_ovec_new();
2124facdb8b6SMichael Roth     spapr->ov5_cas = spapr_ovec_new();
2125facdb8b6SMichael Roth 
2126224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2127facdb8b6SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
21287c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
2129224245bfSDavid Gibson     }
2130224245bfSDavid Gibson 
2131417ece33SMichael Roth     spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
21329fb4541fSSam Bobroff     if (kvmppc_has_cap_mmu_radix()) {
21339fb4541fSSam Bobroff         /* KVM always allows GTSE with radix... */
21349fb4541fSSam Bobroff         spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
21359fb4541fSSam Bobroff     }
21369fb4541fSSam Bobroff     /* ... but not with hash (currently). */
2137417ece33SMichael Roth 
2138ffbb1705SMichael Roth     /* advertise support for dedicated HP event source to guests */
2139ffbb1705SMichael Roth     if (spapr->use_hotplug_event_source) {
2140ffbb1705SMichael Roth         spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2141ffbb1705SMichael Roth     }
2142ffbb1705SMichael Roth 
214353018216SPaolo Bonzini     /* init CPUs */
214419fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
21453daa4a9fSThomas Huth         machine->cpu_model = kvm_enabled() ? "host" : smc->tcg_default_cpu;
214653018216SPaolo Bonzini     }
214794a94e4cSBharata B Rao 
2148e703d2f7SGreg Kurz     ppc_cpu_parse_features(machine->cpu_model);
2149e703d2f7SGreg Kurz 
21500c86d0fdSDavid Gibson     spapr_init_cpus(spapr);
215153018216SPaolo Bonzini 
2152026bfd89SDavid Gibson     if (kvm_enabled()) {
2153026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2154026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
2155ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
21565145ad4fSNathan Whitehorn 
21575145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
21585145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
2159026bfd89SDavid Gibson     }
2160026bfd89SDavid Gibson 
216153018216SPaolo Bonzini     /* allocate RAM */
2162f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
2163fb164994SDavid Gibson                                          machine->ram_size);
2164f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
216553018216SPaolo Bonzini 
2166658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
2167658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
2168658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
2169658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
2170658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
2171658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
2172658fa66bSAlexey Kardashevskiy     }
2173658fa66bSAlexey Kardashevskiy 
21744a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
21754a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
21764a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
217771c9a3ddSBharata B Rao         /*
217871c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
217971c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
218071c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
218171c9a3ddSBharata B Rao          */
218271c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
218371c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
21844a1c9cf0SBharata B Rao 
218571c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
218671c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
218771c9a3ddSBharata B Rao         }
218871c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
2189d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
2190d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
219171c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
2192d54e4d76SDavid Gibson             exit(1);
21934a1c9cf0SBharata B Rao         }
21944a1c9cf0SBharata B Rao 
21954a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
21964a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
21974a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
21984a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
21994a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
22004a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
22014a1c9cf0SBharata B Rao     }
22024a1c9cf0SBharata B Rao 
2203224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
2204224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
2205224245bfSDavid Gibson     }
2206224245bfSDavid Gibson 
220753018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
22084c56440dSStefan Weil     if (!filename) {
2209730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
22104c56440dSStefan Weil         exit(1);
22114c56440dSStefan Weil     }
2212b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
22138afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
22148afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
22158afc22a2SZhou Jie         exit(1);
22168afc22a2SZhou Jie     }
2217b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
2218b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
2219730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
222053018216SPaolo Bonzini         exit(1);
222153018216SPaolo Bonzini     }
222253018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
2223730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
22242f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
222553018216SPaolo Bonzini         exit(1);
222653018216SPaolo Bonzini     }
222753018216SPaolo Bonzini     g_free(filename);
222853018216SPaolo Bonzini 
2229ffbb1705SMichael Roth     /* Set up RTAS event infrastructure */
223053018216SPaolo Bonzini     spapr_events_init(spapr);
223153018216SPaolo Bonzini 
223212f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
223328df36a1SDavid Gibson     spapr_rtc_create(spapr);
223412f42174SDavid Gibson 
223553018216SPaolo Bonzini     /* Set up VIO bus */
223653018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
223753018216SPaolo Bonzini 
223853018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
223953018216SPaolo Bonzini         if (serial_hds[i]) {
224053018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
224153018216SPaolo Bonzini         }
224253018216SPaolo Bonzini     }
224353018216SPaolo Bonzini 
224453018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
224553018216SPaolo Bonzini     spapr_create_nvram(spapr);
224653018216SPaolo Bonzini 
224753018216SPaolo Bonzini     /* Set up PCI */
224853018216SPaolo Bonzini     spapr_pci_rtas_init();
224953018216SPaolo Bonzini 
225089dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
225153018216SPaolo Bonzini 
225253018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
225353018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
225453018216SPaolo Bonzini 
225553018216SPaolo Bonzini         if (!nd->model) {
225653018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
225753018216SPaolo Bonzini         }
225853018216SPaolo Bonzini 
225953018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
226053018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
226153018216SPaolo Bonzini         } else {
226229b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
226353018216SPaolo Bonzini         }
226453018216SPaolo Bonzini     }
226553018216SPaolo Bonzini 
226653018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
226753018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
226853018216SPaolo Bonzini     }
226953018216SPaolo Bonzini 
227053018216SPaolo Bonzini     /* Graphics */
227114c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
227253018216SPaolo Bonzini         spapr->has_graphics = true;
2273c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
227453018216SPaolo Bonzini     }
227553018216SPaolo Bonzini 
22764ee9ced9SMarcel Apfelbaum     if (machine->usb) {
227757040d45SThomas Huth         if (smc->use_ohci_by_default) {
227853018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
227957040d45SThomas Huth         } else {
228057040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
228157040d45SThomas Huth         }
2282c86580b8SMarkus Armbruster 
228353018216SPaolo Bonzini         if (spapr->has_graphics) {
2284c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
2285c86580b8SMarkus Armbruster 
2286c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
2287c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
228853018216SPaolo Bonzini         }
228953018216SPaolo Bonzini     }
229053018216SPaolo Bonzini 
229153018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
2292d54e4d76SDavid Gibson         error_report(
2293d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2294d54e4d76SDavid Gibson             MIN_RMA_SLOF);
229553018216SPaolo Bonzini         exit(1);
229653018216SPaolo Bonzini     }
229753018216SPaolo Bonzini 
229853018216SPaolo Bonzini     if (kernel_filename) {
229953018216SPaolo Bonzini         uint64_t lowaddr = 0;
230053018216SPaolo Bonzini 
2301a19f7fb0SDavid Gibson         spapr->kernel_size = load_elf(kernel_filename, translate_kernel_address,
2302a19f7fb0SDavid Gibson                                       NULL, NULL, &lowaddr, NULL, 1,
2303a19f7fb0SDavid Gibson                                       PPC_ELF_MACHINE, 0, 0);
2304a19f7fb0SDavid Gibson         if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
2305a19f7fb0SDavid Gibson             spapr->kernel_size = load_elf(kernel_filename,
2306a19f7fb0SDavid Gibson                                           translate_kernel_address, NULL, NULL,
2307a19f7fb0SDavid Gibson                                           &lowaddr, NULL, 0, PPC_ELF_MACHINE,
23087ef295eaSPeter Crosthwaite                                           0, 0);
2309a19f7fb0SDavid Gibson             spapr->kernel_le = spapr->kernel_size > 0;
231016457e7fSBenjamin Herrenschmidt         }
2311a19f7fb0SDavid Gibson         if (spapr->kernel_size < 0) {
2312a19f7fb0SDavid Gibson             error_report("error loading %s: %s", kernel_filename,
2313a19f7fb0SDavid Gibson                          load_elf_strerror(spapr->kernel_size));
231453018216SPaolo Bonzini             exit(1);
231553018216SPaolo Bonzini         }
231653018216SPaolo Bonzini 
231753018216SPaolo Bonzini         /* load initrd */
231853018216SPaolo Bonzini         if (initrd_filename) {
231953018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
232053018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
232153018216SPaolo Bonzini              */
2322a19f7fb0SDavid Gibson             spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2323a19f7fb0SDavid Gibson                                   + 0x1ffff) & ~0xffff;
2324a19f7fb0SDavid Gibson             spapr->initrd_size = load_image_targphys(initrd_filename,
2325a19f7fb0SDavid Gibson                                                      spapr->initrd_base,
2326a19f7fb0SDavid Gibson                                                      load_limit
2327a19f7fb0SDavid Gibson                                                      - spapr->initrd_base);
2328a19f7fb0SDavid Gibson             if (spapr->initrd_size < 0) {
2329d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
233053018216SPaolo Bonzini                              initrd_filename);
233153018216SPaolo Bonzini                 exit(1);
233253018216SPaolo Bonzini             }
233353018216SPaolo Bonzini         }
233453018216SPaolo Bonzini     }
233553018216SPaolo Bonzini 
23368e7ea787SAndreas Färber     if (bios_name == NULL) {
23378e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
23388e7ea787SAndreas Färber     }
23398e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
23404c56440dSStefan Weil     if (!filename) {
234168fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
23424c56440dSStefan Weil         exit(1);
23434c56440dSStefan Weil     }
234453018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
234568fea5a0SThomas Huth     if (fw_size <= 0) {
234668fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
234753018216SPaolo Bonzini         exit(1);
234853018216SPaolo Bonzini     }
234953018216SPaolo Bonzini     g_free(filename);
235053018216SPaolo Bonzini 
235128e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
235228e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
235328e02042SDavid Gibson      * which predated MachineState but had a similar function */
23544be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
23554be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
23564be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
23574be21d56SDavid Gibson 
235846503c2bSMichael Roth     /* used by RTAS */
235946503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
236046503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
236146503c2bSMichael Roth 
23625b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
236342043e4fSLaurent Vivier 
236442043e4fSLaurent Vivier     if (kvm_enabled()) {
23653dc410aeSAlexey Kardashevskiy         /* to stop and start vmclock */
236642043e4fSLaurent Vivier         qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
236742043e4fSLaurent Vivier                                          &spapr->tb);
23683dc410aeSAlexey Kardashevskiy 
23693dc410aeSAlexey Kardashevskiy         kvmppc_spapr_enable_inkernel_multitce();
237042043e4fSLaurent Vivier     }
237153018216SPaolo Bonzini }
237253018216SPaolo Bonzini 
2373135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2374135a129aSAneesh Kumar K.V {
2375135a129aSAneesh Kumar K.V     if (!vm_type) {
2376135a129aSAneesh Kumar K.V         return 0;
2377135a129aSAneesh Kumar K.V     }
2378135a129aSAneesh Kumar K.V 
2379135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2380135a129aSAneesh Kumar K.V         return 1;
2381135a129aSAneesh Kumar K.V     }
2382135a129aSAneesh Kumar K.V 
2383135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2384135a129aSAneesh Kumar K.V         return 2;
2385135a129aSAneesh Kumar K.V     }
2386135a129aSAneesh Kumar K.V 
2387135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2388135a129aSAneesh Kumar K.V     exit(1);
2389135a129aSAneesh Kumar K.V }
2390135a129aSAneesh Kumar K.V 
239171461b0fSAlexey Kardashevskiy /*
2392627b84f4SGonglei  * Implementation of an interface to adjust firmware path
239371461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
239471461b0fSAlexey Kardashevskiy  */
239571461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
239671461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
239771461b0fSAlexey Kardashevskiy {
239871461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
239971461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
240071461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
240171461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
240271461b0fSAlexey Kardashevskiy 
240371461b0fSAlexey Kardashevskiy     if (d) {
240471461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
240571461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
240671461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
240771461b0fSAlexey Kardashevskiy 
240871461b0fSAlexey Kardashevskiy         if (spapr) {
240971461b0fSAlexey Kardashevskiy             /*
241071461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
241171461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
241271461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
241371461b0fSAlexey Kardashevskiy              */
241471461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
241571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
241671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
241771461b0fSAlexey Kardashevskiy         } else if (virtio) {
241871461b0fSAlexey Kardashevskiy             /*
241971461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
242071461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
242171461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
242271461b0fSAlexey Kardashevskiy              * the actual binding is:
242371461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
242471461b0fSAlexey Kardashevskiy              */
242571461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
242671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
242771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
242871461b0fSAlexey Kardashevskiy         } else if (usb) {
242971461b0fSAlexey Kardashevskiy             /*
243071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
243171461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
243271461b0fSAlexey Kardashevskiy              */
243371461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
243471461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
243571461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
243671461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
243771461b0fSAlexey Kardashevskiy         }
243871461b0fSAlexey Kardashevskiy     }
243971461b0fSAlexey Kardashevskiy 
2440b99260ebSThomas Huth     /*
2441b99260ebSThomas Huth      * SLOF probes the USB devices, and if it recognizes that the device is a
2442b99260ebSThomas Huth      * storage device, it changes its name to "storage" instead of "usb-host",
2443b99260ebSThomas Huth      * and additionally adds a child node for the SCSI LUN, so the correct
2444b99260ebSThomas Huth      * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
2445b99260ebSThomas Huth      */
2446b99260ebSThomas Huth     if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
2447b99260ebSThomas Huth         USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
2448b99260ebSThomas Huth         if (usb_host_dev_is_scsi_storage(usbdev)) {
2449b99260ebSThomas Huth             return g_strdup_printf("storage@%s/disk", usbdev->port->path);
2450b99260ebSThomas Huth         }
2451b99260ebSThomas Huth     }
2452b99260ebSThomas Huth 
245371461b0fSAlexey Kardashevskiy     if (phb) {
245471461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
245571461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
245671461b0fSAlexey Kardashevskiy     }
245771461b0fSAlexey Kardashevskiy 
245871461b0fSAlexey Kardashevskiy     return NULL;
245971461b0fSAlexey Kardashevskiy }
246071461b0fSAlexey Kardashevskiy 
246123825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
246223825581SEduardo Habkost {
246328e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
246423825581SEduardo Habkost 
246528e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
246623825581SEduardo Habkost }
246723825581SEduardo Habkost 
246823825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
246923825581SEduardo Habkost {
247028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
247123825581SEduardo Habkost 
247228e02042SDavid Gibson     g_free(spapr->kvm_type);
247328e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
247423825581SEduardo Habkost }
247523825581SEduardo Habkost 
2476f6229214SMichael Roth static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
2477f6229214SMichael Roth {
2478f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2479f6229214SMichael Roth 
2480f6229214SMichael Roth     return spapr->use_hotplug_event_source;
2481f6229214SMichael Roth }
2482f6229214SMichael Roth 
2483f6229214SMichael Roth static void spapr_set_modern_hotplug_events(Object *obj, bool value,
2484f6229214SMichael Roth                                             Error **errp)
2485f6229214SMichael Roth {
2486f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2487f6229214SMichael Roth 
2488f6229214SMichael Roth     spapr->use_hotplug_event_source = value;
2489f6229214SMichael Roth }
2490f6229214SMichael Roth 
249123825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
249223825581SEduardo Habkost {
2493715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2494715c5407SDavid Gibson 
2495715c5407SDavid Gibson     spapr->htab_fd = -1;
2496f6229214SMichael Roth     spapr->use_hotplug_event_source = true;
249723825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
249823825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
249949d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
250049d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
250149d2e648SMarcel Apfelbaum                                     NULL);
2502f6229214SMichael Roth     object_property_add_bool(obj, "modern-hotplug-events",
2503f6229214SMichael Roth                             spapr_get_modern_hotplug_events,
2504f6229214SMichael Roth                             spapr_set_modern_hotplug_events,
2505f6229214SMichael Roth                             NULL);
2506f6229214SMichael Roth     object_property_set_description(obj, "modern-hotplug-events",
2507f6229214SMichael Roth                                     "Use dedicated hotplug event mechanism in"
2508f6229214SMichael Roth                                     " place of standard EPOW events when possible"
2509f6229214SMichael Roth                                     " (required for memory hot-unplug support)",
2510f6229214SMichael Roth                                     NULL);
251123825581SEduardo Habkost }
251223825581SEduardo Habkost 
251387bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
251487bbdd9cSDavid Gibson {
251587bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
251687bbdd9cSDavid Gibson 
251787bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
251887bbdd9cSDavid Gibson }
251987bbdd9cSDavid Gibson 
25201c7ad77eSNicholas Piggin void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
252134316482SAlexey Kardashevskiy {
252234316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
252334316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
252434316482SAlexey Kardashevskiy }
252534316482SAlexey Kardashevskiy 
252634316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
252734316482SAlexey Kardashevskiy {
252834316482SAlexey Kardashevskiy     CPUState *cs;
252934316482SAlexey Kardashevskiy 
253034316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
25311c7ad77eSNicholas Piggin         async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
253234316482SAlexey Kardashevskiy     }
253334316482SAlexey Kardashevskiy }
253434316482SAlexey Kardashevskiy 
253579b78a6bSMichael Roth static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
253679b78a6bSMichael Roth                            uint32_t node, bool dedicated_hp_event_source,
253779b78a6bSMichael Roth                            Error **errp)
2538c20d332aSBharata B Rao {
2539c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2540c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2541c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2542c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2543c20d332aSBharata B Rao     void *fdt;
254479b78a6bSMichael Roth     uint64_t addr = addr_start;
2545c20d332aSBharata B Rao 
2546c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2547c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2548c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2549c20d332aSBharata B Rao         g_assert(drc);
2550c20d332aSBharata B Rao 
2551c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2552c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2553c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2554c20d332aSBharata B Rao 
2555c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2556c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2557c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
25585c0139a8SMichael Roth         if (!dev->hotplugged) {
25595c0139a8SMichael Roth             /* guests expect coldplugged LMBs to be pre-allocated */
25605c0139a8SMichael Roth             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
25615c0139a8SMichael Roth             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
25625c0139a8SMichael Roth         }
2563c20d332aSBharata B Rao     }
25645dd5238cSJianjun Duan     /* send hotplug notification to the
25655dd5238cSJianjun Duan      * guest only in case of hotplugged memory
25665dd5238cSJianjun Duan      */
25675dd5238cSJianjun Duan     if (dev->hotplugged) {
256879b78a6bSMichael Roth         if (dedicated_hp_event_source) {
256979b78a6bSMichael Roth             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
257079b78a6bSMichael Roth                     addr_start / SPAPR_MEMORY_BLOCK_SIZE);
257179b78a6bSMichael Roth             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
257279b78a6bSMichael Roth             spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
257379b78a6bSMichael Roth                                                    nr_lmbs,
257479b78a6bSMichael Roth                                                    drck->get_index(drc));
257579b78a6bSMichael Roth         } else {
257679b78a6bSMichael Roth             spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
257779b78a6bSMichael Roth                                            nr_lmbs);
257879b78a6bSMichael Roth         }
2579c20d332aSBharata B Rao     }
25805dd5238cSJianjun Duan }
2581c20d332aSBharata B Rao 
2582c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2583c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2584c20d332aSBharata B Rao {
2585c20d332aSBharata B Rao     Error *local_err = NULL;
2586c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2587c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2588c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2589c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2590c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2591c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2592c20d332aSBharata B Rao     uint64_t addr;
2593df587133SThomas Huth     char *mem_dev;
2594c20d332aSBharata B Rao 
2595c20d332aSBharata B Rao     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2596c20d332aSBharata B Rao         error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2597c20d332aSBharata B Rao                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2598c20d332aSBharata B Rao         goto out;
2599c20d332aSBharata B Rao     }
2600c20d332aSBharata B Rao 
2601df587133SThomas Huth     mem_dev = object_property_get_str(OBJECT(dimm), PC_DIMM_MEMDEV_PROP, NULL);
2602df587133SThomas Huth     if (mem_dev && !kvmppc_is_mem_backend_page_size_ok(mem_dev)) {
2603df587133SThomas Huth         error_setg(&local_err, "Memory backend has bad page size. "
2604df587133SThomas Huth                    "Use 'memory-backend-file' with correct mem-path.");
2605df587133SThomas Huth         goto out;
2606df587133SThomas Huth     }
2607df587133SThomas Huth 
2608d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2609c20d332aSBharata B Rao     if (local_err) {
2610c20d332aSBharata B Rao         goto out;
2611c20d332aSBharata B Rao     }
2612c20d332aSBharata B Rao 
2613c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2614c20d332aSBharata B Rao     if (local_err) {
2615c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2616c20d332aSBharata B Rao         goto out;
2617c20d332aSBharata B Rao     }
2618c20d332aSBharata B Rao 
261979b78a6bSMichael Roth     spapr_add_lmbs(dev, addr, size, node,
262079b78a6bSMichael Roth                    spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
262179b78a6bSMichael Roth                    &error_abort);
2622c20d332aSBharata B Rao 
2623c20d332aSBharata B Rao out:
2624c20d332aSBharata B Rao     error_propagate(errp, local_err);
2625c20d332aSBharata B Rao }
2626c20d332aSBharata B Rao 
2627cf632463SBharata B Rao typedef struct sPAPRDIMMState {
2628cf632463SBharata B Rao     uint32_t nr_lmbs;
2629cf632463SBharata B Rao } sPAPRDIMMState;
2630cf632463SBharata B Rao 
2631cf632463SBharata B Rao static void spapr_lmb_release(DeviceState *dev, void *opaque)
2632cf632463SBharata B Rao {
2633cf632463SBharata B Rao     sPAPRDIMMState *ds = (sPAPRDIMMState *)opaque;
2634cf632463SBharata B Rao     HotplugHandler *hotplug_ctrl;
2635cf632463SBharata B Rao 
2636cf632463SBharata B Rao     if (--ds->nr_lmbs) {
2637cf632463SBharata B Rao         return;
2638cf632463SBharata B Rao     }
2639cf632463SBharata B Rao 
2640cf632463SBharata B Rao     g_free(ds);
2641cf632463SBharata B Rao 
2642cf632463SBharata B Rao     /*
2643cf632463SBharata B Rao      * Now that all the LMBs have been removed by the guest, call the
2644cf632463SBharata B Rao      * pc-dimm unplug handler to cleanup up the pc-dimm device.
2645cf632463SBharata B Rao      */
2646cf632463SBharata B Rao     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2647cf632463SBharata B Rao     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2648cf632463SBharata B Rao }
2649cf632463SBharata B Rao 
2650cf632463SBharata B Rao static void spapr_del_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
2651cf632463SBharata B Rao                            Error **errp)
2652cf632463SBharata B Rao {
2653cf632463SBharata B Rao     sPAPRDRConnector *drc;
2654cf632463SBharata B Rao     sPAPRDRConnectorClass *drck;
2655cf632463SBharata B Rao     uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
2656cf632463SBharata B Rao     int i;
2657cf632463SBharata B Rao     sPAPRDIMMState *ds = g_malloc0(sizeof(sPAPRDIMMState));
2658cf632463SBharata B Rao     uint64_t addr = addr_start;
2659cf632463SBharata B Rao 
2660cf632463SBharata B Rao     ds->nr_lmbs = nr_lmbs;
2661cf632463SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2662cf632463SBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2663cf632463SBharata B Rao                 addr / SPAPR_MEMORY_BLOCK_SIZE);
2664cf632463SBharata B Rao         g_assert(drc);
2665cf632463SBharata B Rao 
2666cf632463SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2667cf632463SBharata B Rao         drck->detach(drc, dev, spapr_lmb_release, ds, errp);
2668cf632463SBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2669cf632463SBharata B Rao     }
2670cf632463SBharata B Rao 
2671cf632463SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2672cf632463SBharata B Rao                                    addr_start / SPAPR_MEMORY_BLOCK_SIZE);
2673cf632463SBharata B Rao     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2674cf632463SBharata B Rao     spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
2675cf632463SBharata B Rao                                               nr_lmbs,
2676cf632463SBharata B Rao                                               drck->get_index(drc));
2677cf632463SBharata B Rao }
2678cf632463SBharata B Rao 
2679cf632463SBharata B Rao static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2680cf632463SBharata B Rao                                 Error **errp)
2681cf632463SBharata B Rao {
2682cf632463SBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2683cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2684cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2685cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2686cf632463SBharata B Rao 
2687cf632463SBharata B Rao     pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2688cf632463SBharata B Rao     object_unparent(OBJECT(dev));
2689cf632463SBharata B Rao }
2690cf632463SBharata B Rao 
2691cf632463SBharata B Rao static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
2692cf632463SBharata B Rao                                         DeviceState *dev, Error **errp)
2693cf632463SBharata B Rao {
2694cf632463SBharata B Rao     Error *local_err = NULL;
2695cf632463SBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2696cf632463SBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2697cf632463SBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2698cf632463SBharata B Rao     uint64_t size = memory_region_size(mr);
2699cf632463SBharata B Rao     uint64_t addr;
2700cf632463SBharata B Rao 
2701cf632463SBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2702cf632463SBharata B Rao     if (local_err) {
2703cf632463SBharata B Rao         goto out;
2704cf632463SBharata B Rao     }
2705cf632463SBharata B Rao 
2706cf632463SBharata B Rao     spapr_del_lmbs(dev, addr, size, &error_abort);
2707cf632463SBharata B Rao out:
2708cf632463SBharata B Rao     error_propagate(errp, local_err);
2709cf632463SBharata B Rao }
2710cf632463SBharata B Rao 
2711af81cf32SBharata B Rao void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2712af81cf32SBharata B Rao                                     sPAPRMachineState *spapr)
2713af81cf32SBharata B Rao {
2714af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2715af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2716af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2717af81cf32SBharata B Rao     void *fdt;
2718af81cf32SBharata B Rao     int offset, fdt_size;
2719af81cf32SBharata B Rao     char *nodename;
2720af81cf32SBharata B Rao 
2721af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2722af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2723af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2724af81cf32SBharata B Rao 
2725af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2726af81cf32SBharata B Rao     g_free(nodename);
2727af81cf32SBharata B Rao 
2728af81cf32SBharata B Rao     *fdt_offset = offset;
2729af81cf32SBharata B Rao     return fdt;
2730af81cf32SBharata B Rao }
2731af81cf32SBharata B Rao 
2732115debf2SIgor Mammedov static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
2733115debf2SIgor Mammedov                               Error **errp)
2734ff9006ddSIgor Mammedov {
2735535455fdSIgor Mammedov     MachineState *ms = MACHINE(qdev_get_machine());
2736ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2737535455fdSIgor Mammedov     CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
2738ff9006ddSIgor Mammedov 
2739535455fdSIgor Mammedov     core_slot->cpu = NULL;
2740ff9006ddSIgor Mammedov     object_unparent(OBJECT(dev));
2741ff9006ddSIgor Mammedov }
2742ff9006ddSIgor Mammedov 
2743115debf2SIgor Mammedov static void spapr_core_release(DeviceState *dev, void *opaque)
2744115debf2SIgor Mammedov {
2745115debf2SIgor Mammedov     HotplugHandler *hotplug_ctrl;
2746115debf2SIgor Mammedov 
2747115debf2SIgor Mammedov     hotplug_ctrl = qdev_get_hotplug_handler(dev);
2748115debf2SIgor Mammedov     hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
2749115debf2SIgor Mammedov }
2750115debf2SIgor Mammedov 
2751115debf2SIgor Mammedov static
2752115debf2SIgor Mammedov void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
2753ff9006ddSIgor Mammedov                                Error **errp)
2754ff9006ddSIgor Mammedov {
2755535455fdSIgor Mammedov     int index;
2756535455fdSIgor Mammedov     sPAPRDRConnector *drc;
2757ff9006ddSIgor Mammedov     sPAPRDRConnectorClass *drck;
2758ff9006ddSIgor Mammedov     Error *local_err = NULL;
2759535455fdSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2760535455fdSIgor Mammedov     int smt = kvmppc_smt_threads();
2761ff9006ddSIgor Mammedov 
2762535455fdSIgor Mammedov     if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
2763535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2764535455fdSIgor Mammedov                    cc->core_id);
2765535455fdSIgor Mammedov         return;
2766535455fdSIgor Mammedov     }
2767ff9006ddSIgor Mammedov     if (index == 0) {
2768ff9006ddSIgor Mammedov         error_setg(errp, "Boot CPU core may not be unplugged");
2769ff9006ddSIgor Mammedov         return;
2770ff9006ddSIgor Mammedov     }
2771ff9006ddSIgor Mammedov 
2772535455fdSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2773ff9006ddSIgor Mammedov     g_assert(drc);
2774ff9006ddSIgor Mammedov 
2775ff9006ddSIgor Mammedov     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2776ff9006ddSIgor Mammedov     drck->detach(drc, dev, spapr_core_release, NULL, &local_err);
2777ff9006ddSIgor Mammedov     if (local_err) {
2778ff9006ddSIgor Mammedov         error_propagate(errp, local_err);
2779ff9006ddSIgor Mammedov         return;
2780ff9006ddSIgor Mammedov     }
2781ff9006ddSIgor Mammedov 
2782ff9006ddSIgor Mammedov     spapr_hotplug_req_remove_by_index(drc);
2783ff9006ddSIgor Mammedov }
2784ff9006ddSIgor Mammedov 
2785ff9006ddSIgor Mammedov static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2786ff9006ddSIgor Mammedov                             Error **errp)
2787ff9006ddSIgor Mammedov {
2788ff9006ddSIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
2789ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(spapr);
2790ff9006ddSIgor Mammedov     sPAPRCPUCore *core = SPAPR_CPU_CORE(OBJECT(dev));
2791ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2792ff9006ddSIgor Mammedov     CPUState *cs = CPU(core->threads);
2793ff9006ddSIgor Mammedov     sPAPRDRConnector *drc;
2794ff9006ddSIgor Mammedov     Error *local_err = NULL;
2795ff9006ddSIgor Mammedov     void *fdt = NULL;
2796ff9006ddSIgor Mammedov     int fdt_offset = 0;
2797ff9006ddSIgor Mammedov     int smt = kvmppc_smt_threads();
2798535455fdSIgor Mammedov     CPUArchId *core_slot;
2799535455fdSIgor Mammedov     int index;
2800ff9006ddSIgor Mammedov 
2801535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2802535455fdSIgor Mammedov     if (!core_slot) {
2803535455fdSIgor Mammedov         error_setg(errp, "Unable to find CPU core with core-id: %d",
2804535455fdSIgor Mammedov                    cc->core_id);
2805535455fdSIgor Mammedov         return;
2806535455fdSIgor Mammedov     }
2807ff9006ddSIgor Mammedov     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index * smt);
2808ff9006ddSIgor Mammedov 
2809c5514d0eSIgor Mammedov     g_assert(drc || !mc->has_hotpluggable_cpus);
2810ff9006ddSIgor Mammedov 
2811ff9006ddSIgor Mammedov     /*
2812ff9006ddSIgor Mammedov      * Setup CPU DT entries only for hotplugged CPUs. For boot time or
2813ff9006ddSIgor Mammedov      * coldplugged CPUs DT entries are setup in spapr_build_fdt().
2814ff9006ddSIgor Mammedov      */
2815ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2816ff9006ddSIgor Mammedov         fdt = spapr_populate_hotplug_cpu_dt(cs, &fdt_offset, spapr);
2817ff9006ddSIgor Mammedov     }
2818ff9006ddSIgor Mammedov 
2819ff9006ddSIgor Mammedov     if (drc) {
2820ff9006ddSIgor Mammedov         sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2821ff9006ddSIgor Mammedov         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, &local_err);
2822ff9006ddSIgor Mammedov         if (local_err) {
2823ff9006ddSIgor Mammedov             g_free(fdt);
2824ff9006ddSIgor Mammedov             error_propagate(errp, local_err);
2825ff9006ddSIgor Mammedov             return;
2826ff9006ddSIgor Mammedov         }
2827ff9006ddSIgor Mammedov     }
2828ff9006ddSIgor Mammedov 
2829ff9006ddSIgor Mammedov     if (dev->hotplugged) {
2830ff9006ddSIgor Mammedov         /*
2831ff9006ddSIgor Mammedov          * Send hotplug notification interrupt to the guest only in case
2832ff9006ddSIgor Mammedov          * of hotplugged CPUs.
2833ff9006ddSIgor Mammedov          */
2834ff9006ddSIgor Mammedov         spapr_hotplug_req_add_by_index(drc);
2835ff9006ddSIgor Mammedov     } else {
2836ff9006ddSIgor Mammedov         /*
2837ff9006ddSIgor Mammedov          * Set the right DRC states for cold plugged CPU.
2838ff9006ddSIgor Mammedov          */
2839ff9006ddSIgor Mammedov         if (drc) {
2840ff9006ddSIgor Mammedov             sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2841ff9006ddSIgor Mammedov             drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_USABLE);
2842ff9006ddSIgor Mammedov             drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_UNISOLATED);
2843ff9006ddSIgor Mammedov         }
2844ff9006ddSIgor Mammedov     }
2845535455fdSIgor Mammedov     core_slot->cpu = OBJECT(dev);
2846ff9006ddSIgor Mammedov }
2847ff9006ddSIgor Mammedov 
2848ff9006ddSIgor Mammedov static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2849ff9006ddSIgor Mammedov                                 Error **errp)
2850ff9006ddSIgor Mammedov {
2851ff9006ddSIgor Mammedov     MachineState *machine = MACHINE(OBJECT(hotplug_dev));
2852ff9006ddSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
2853ff9006ddSIgor Mammedov     Error *local_err = NULL;
2854ff9006ddSIgor Mammedov     CPUCore *cc = CPU_CORE(dev);
2855ff9006ddSIgor Mammedov     char *base_core_type = spapr_get_cpu_core_type(machine->cpu_model);
2856ff9006ddSIgor Mammedov     const char *type = object_get_typename(OBJECT(dev));
2857535455fdSIgor Mammedov     CPUArchId *core_slot;
2858535455fdSIgor Mammedov     int index;
2859ff9006ddSIgor Mammedov 
2860c5514d0eSIgor Mammedov     if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
2861ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU hotplug not supported for this machine");
2862ff9006ddSIgor Mammedov         goto out;
2863ff9006ddSIgor Mammedov     }
2864ff9006ddSIgor Mammedov 
2865ff9006ddSIgor Mammedov     if (strcmp(base_core_type, type)) {
2866ff9006ddSIgor Mammedov         error_setg(&local_err, "CPU core type should be %s", base_core_type);
2867ff9006ddSIgor Mammedov         goto out;
2868ff9006ddSIgor Mammedov     }
2869ff9006ddSIgor Mammedov 
2870ff9006ddSIgor Mammedov     if (cc->core_id % smp_threads) {
2871ff9006ddSIgor Mammedov         error_setg(&local_err, "invalid core id %d", cc->core_id);
2872ff9006ddSIgor Mammedov         goto out;
2873ff9006ddSIgor Mammedov     }
2874ff9006ddSIgor Mammedov 
28758149e299SDavid Gibson     if (cc->nr_threads != smp_threads) {
28768149e299SDavid Gibson         error_setg(errp, "invalid nr-threads %d, must be %d",
28778149e299SDavid Gibson                    cc->nr_threads, smp_threads);
28788149e299SDavid Gibson         return;
28798149e299SDavid Gibson     }
28808149e299SDavid Gibson 
2881535455fdSIgor Mammedov     core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
2882535455fdSIgor Mammedov     if (!core_slot) {
2883ff9006ddSIgor Mammedov         error_setg(&local_err, "core id %d out of range", cc->core_id);
2884ff9006ddSIgor Mammedov         goto out;
2885ff9006ddSIgor Mammedov     }
2886ff9006ddSIgor Mammedov 
2887535455fdSIgor Mammedov     if (core_slot->cpu) {
2888ff9006ddSIgor Mammedov         error_setg(&local_err, "core %d already populated", cc->core_id);
2889ff9006ddSIgor Mammedov         goto out;
2890ff9006ddSIgor Mammedov     }
2891ff9006ddSIgor Mammedov 
2892ff9006ddSIgor Mammedov out:
2893ff9006ddSIgor Mammedov     g_free(base_core_type);
2894ff9006ddSIgor Mammedov     error_propagate(errp, local_err);
2895ff9006ddSIgor Mammedov }
2896ff9006ddSIgor Mammedov 
2897c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2898c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2899c20d332aSBharata B Rao {
2900c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2901c20d332aSBharata B Rao 
2902c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2903b556854bSBharata B Rao         int node;
2904c20d332aSBharata B Rao 
2905c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2906c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2907c20d332aSBharata B Rao             return;
2908c20d332aSBharata B Rao         }
2909c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2910c20d332aSBharata B Rao         if (*errp) {
2911c20d332aSBharata B Rao             return;
2912c20d332aSBharata B Rao         }
29131a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
29141a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
29151a5512bbSGonglei             return;
29161a5512bbSGonglei         }
2917c20d332aSBharata B Rao 
2918b556854bSBharata B Rao         /*
2919b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2920b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2921b556854bSBharata B Rao          * to the first node that has some memory. This causes two
2922b556854bSBharata B Rao          * unexpected behaviours for the user.
2923b556854bSBharata B Rao          *
2924b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
2925b556854bSBharata B Rao          *   specified.
2926b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2927b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
2928b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
2929b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
2930b556854bSBharata B Rao          *
2931b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
2932b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
2933b556854bSBharata B Rao          */
2934b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
2935b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
2936b556854bSBharata B Rao                        node);
2937b556854bSBharata B Rao             return;
2938b556854bSBharata B Rao         }
2939b556854bSBharata B Rao 
2940c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
2941af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2942af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
2943c20d332aSBharata B Rao     }
2944c20d332aSBharata B Rao }
2945c20d332aSBharata B Rao 
2946c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2947c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2948c20d332aSBharata B Rao {
2949cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
29503c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
29516f4b5c3eSBharata B Rao 
2952c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2953cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2954cf632463SBharata B Rao             spapr_memory_unplug(hotplug_dev, dev, errp);
2955cf632463SBharata B Rao         } else {
2956cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2957cf632463SBharata B Rao         }
2958cf632463SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2959c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
2960cf632463SBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
2961cf632463SBharata B Rao             return;
2962cf632463SBharata B Rao         }
2963cf632463SBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
2964cf632463SBharata B Rao     }
2965cf632463SBharata B Rao }
2966cf632463SBharata B Rao 
2967cf632463SBharata B Rao static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
2968cf632463SBharata B Rao                                                 DeviceState *dev, Error **errp)
2969cf632463SBharata B Rao {
2970cf632463SBharata B Rao     sPAPRMachineState *sms = SPAPR_MACHINE(qdev_get_machine());
2971cf632463SBharata B Rao     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
2972cf632463SBharata B Rao 
2973cf632463SBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2974cf632463SBharata B Rao         if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
2975cf632463SBharata B Rao             spapr_memory_unplug_request(hotplug_dev, dev, errp);
2976cf632463SBharata B Rao         } else {
2977cf632463SBharata B Rao             /* NOTE: this means there is a window after guest reset, prior to
2978cf632463SBharata B Rao              * CAS negotiation, where unplug requests will fail due to the
2979cf632463SBharata B Rao              * capability not being detected yet. This is a bit different than
2980cf632463SBharata B Rao              * the case with PCI unplug, where the events will be queued and
2981cf632463SBharata B Rao              * eventually handled by the guest after boot
2982cf632463SBharata B Rao              */
2983cf632463SBharata B Rao             error_setg(errp, "Memory hot unplug not supported for this guest");
2984cf632463SBharata B Rao         }
29856f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2986c5514d0eSIgor Mammedov         if (!mc->has_hotpluggable_cpus) {
29876f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
29886f4b5c3eSBharata B Rao             return;
29896f4b5c3eSBharata B Rao         }
2990115debf2SIgor Mammedov         spapr_core_unplug_request(hotplug_dev, dev, errp);
2991c20d332aSBharata B Rao     }
2992c20d332aSBharata B Rao }
2993c20d332aSBharata B Rao 
299494a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
299594a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
299694a94e4cSBharata B Rao {
299794a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
299894a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
299994a94e4cSBharata B Rao     }
300094a94e4cSBharata B Rao }
300194a94e4cSBharata B Rao 
30027ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
3003c20d332aSBharata B Rao                                                  DeviceState *dev)
3004c20d332aSBharata B Rao {
300594a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
300694a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3007c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
3008c20d332aSBharata B Rao     }
3009c20d332aSBharata B Rao     return NULL;
3010c20d332aSBharata B Rao }
3011c20d332aSBharata B Rao 
301220bb648dSDavid Gibson static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
301320bb648dSDavid Gibson {
301420bb648dSDavid Gibson     /* Allocate to NUMA nodes on a "socket" basis (not that concept of
301520bb648dSDavid Gibson      * socket means much for the paravirtualized PAPR platform) */
301620bb648dSDavid Gibson     return cpu_index / smp_threads / smp_cores;
301720bb648dSDavid Gibson }
301820bb648dSDavid Gibson 
3019535455fdSIgor Mammedov static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
3020535455fdSIgor Mammedov {
3021535455fdSIgor Mammedov     int i;
3022535455fdSIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
3023535455fdSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(machine);
3024535455fdSIgor Mammedov 
3025c5514d0eSIgor Mammedov     if (!mc->has_hotpluggable_cpus) {
3026535455fdSIgor Mammedov         spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
3027535455fdSIgor Mammedov     }
3028535455fdSIgor Mammedov     if (machine->possible_cpus) {
3029535455fdSIgor Mammedov         assert(machine->possible_cpus->len == spapr_max_cores);
3030535455fdSIgor Mammedov         return machine->possible_cpus;
3031535455fdSIgor Mammedov     }
3032535455fdSIgor Mammedov 
3033535455fdSIgor Mammedov     machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
3034535455fdSIgor Mammedov                              sizeof(CPUArchId) * spapr_max_cores);
3035535455fdSIgor Mammedov     machine->possible_cpus->len = spapr_max_cores;
3036535455fdSIgor Mammedov     for (i = 0; i < machine->possible_cpus->len; i++) {
3037535455fdSIgor Mammedov         int core_id = i * smp_threads;
3038535455fdSIgor Mammedov 
3039f2d672c2SIgor Mammedov         machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
3040535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].arch_id = core_id;
3041535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.has_core_id = true;
3042535455fdSIgor Mammedov         machine->possible_cpus->cpus[i].props.core_id = core_id;
3043535455fdSIgor Mammedov         /* TODO: add 'has_node/node' here to describe
3044535455fdSIgor Mammedov            to which node core belongs */
3045535455fdSIgor Mammedov     }
3046535455fdSIgor Mammedov     return machine->possible_cpus;
3047535455fdSIgor Mammedov }
3048535455fdSIgor Mammedov 
30496737d9adSDavid Gibson static void spapr_phb_placement(sPAPRMachineState *spapr, uint32_t index,
3050daa23699SDavid Gibson                                 uint64_t *buid, hwaddr *pio,
3051daa23699SDavid Gibson                                 hwaddr *mmio32, hwaddr *mmio64,
30526737d9adSDavid Gibson                                 unsigned n_dma, uint32_t *liobns, Error **errp)
30536737d9adSDavid Gibson {
3054357d1e3bSDavid Gibson     /*
3055357d1e3bSDavid Gibson      * New-style PHB window placement.
3056357d1e3bSDavid Gibson      *
3057357d1e3bSDavid Gibson      * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
3058357d1e3bSDavid Gibson      * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
3059357d1e3bSDavid Gibson      * windows.
3060357d1e3bSDavid Gibson      *
3061357d1e3bSDavid Gibson      * Some guest kernels can't work with MMIO windows above 1<<46
3062357d1e3bSDavid Gibson      * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
3063357d1e3bSDavid Gibson      *
3064357d1e3bSDavid Gibson      * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
3065357d1e3bSDavid Gibson      * PHB stacked together.  (32TiB+2GiB)..(32TiB+64GiB) contains the
3066357d1e3bSDavid Gibson      * 2GiB 32-bit MMIO windows for each PHB.  Then 33..64TiB has the
3067357d1e3bSDavid Gibson      * 1TiB 64-bit MMIO windows for each PHB.
3068357d1e3bSDavid Gibson      */
30696737d9adSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
307025e6a118SMichael S. Tsirkin #define SPAPR_MAX_PHBS ((SPAPR_PCI_LIMIT - SPAPR_PCI_BASE) / \
307125e6a118SMichael S. Tsirkin                         SPAPR_PCI_MEM64_WIN_SIZE - 1)
30726737d9adSDavid Gibson     int i;
30736737d9adSDavid Gibson 
3074357d1e3bSDavid Gibson     /* Sanity check natural alignments */
3075357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3076357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
3077357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
3078357d1e3bSDavid Gibson     QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
3079357d1e3bSDavid Gibson     /* Sanity check bounds */
308025e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
308125e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM32_WIN_SIZE);
308225e6a118SMichael S. Tsirkin     QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
308325e6a118SMichael S. Tsirkin                       SPAPR_PCI_MEM64_WIN_SIZE);
30842efff1c0SDavid Gibson 
308525e6a118SMichael S. Tsirkin     if (index >= SPAPR_MAX_PHBS) {
308625e6a118SMichael S. Tsirkin         error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
308725e6a118SMichael S. Tsirkin                    SPAPR_MAX_PHBS - 1);
30886737d9adSDavid Gibson         return;
30896737d9adSDavid Gibson     }
30906737d9adSDavid Gibson 
30916737d9adSDavid Gibson     *buid = base_buid + index;
30926737d9adSDavid Gibson     for (i = 0; i < n_dma; ++i) {
30936737d9adSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
30946737d9adSDavid Gibson     }
30956737d9adSDavid Gibson 
3096357d1e3bSDavid Gibson     *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
3097357d1e3bSDavid Gibson     *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
3098357d1e3bSDavid Gibson     *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
30996737d9adSDavid Gibson }
31006737d9adSDavid Gibson 
31017844e12bSCédric Le Goater static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
31027844e12bSCédric Le Goater {
31037844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
31047844e12bSCédric Le Goater 
31057844e12bSCédric Le Goater     return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
31067844e12bSCédric Le Goater }
31077844e12bSCédric Le Goater 
31087844e12bSCédric Le Goater static void spapr_ics_resend(XICSFabric *dev)
31097844e12bSCédric Le Goater {
31107844e12bSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(dev);
31117844e12bSCédric Le Goater 
31127844e12bSCédric Le Goater     ics_resend(spapr->ics);
31137844e12bSCédric Le Goater }
31147844e12bSCédric Le Goater 
3115*06747ba6SCédric Le Goater static ICPState *spapr_icp_get(XICSFabric *xi, int cpu_dt_id)
3116b2fc59aaSCédric Le Goater {
3117b2fc59aaSCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
3118*06747ba6SCédric Le Goater     int server = xics_get_cpu_index_by_dt_id(cpu_dt_id);
3119b2fc59aaSCédric Le Goater 
3120852ad27eSCédric Le Goater     return (server < spapr->nr_servers) ? &spapr->icps[server] : NULL;
3121b2fc59aaSCédric Le Goater }
3122b2fc59aaSCédric Le Goater 
31236449da45SCédric Le Goater static void spapr_pic_print_info(InterruptStatsProvider *obj,
31246449da45SCédric Le Goater                                  Monitor *mon)
31256449da45SCédric Le Goater {
31266449da45SCédric Le Goater     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
31276449da45SCédric Le Goater     int i;
31286449da45SCédric Le Goater 
31296449da45SCédric Le Goater     for (i = 0; i < spapr->nr_servers; i++) {
31306449da45SCédric Le Goater         icp_pic_print_info(&spapr->icps[i], mon);
31316449da45SCédric Le Goater     }
31326449da45SCédric Le Goater 
31336449da45SCédric Le Goater     ics_pic_print_info(spapr->ics, mon);
31346449da45SCédric Le Goater }
31356449da45SCédric Le Goater 
313629ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
313753018216SPaolo Bonzini {
313829ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
3139224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
314071461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
314134316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
3142c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
31431d1be34dSDavid Gibson     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
31447844e12bSCédric Le Goater     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
31456449da45SCédric Le Goater     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
314629ee3247SAlexey Kardashevskiy 
31470eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
3148fc9f38c3SDavid Gibson 
3149fc9f38c3SDavid Gibson     /*
3150fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
3151fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
3152fc9f38c3SDavid Gibson      * these details for backwards compatibility
3153fc9f38c3SDavid Gibson      */
3154958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
3155958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
3156958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
31576244bb7eSGreg Kurz     mc->max_cpus = 1024;
3158958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
31595b2128d2SAlexander Graf     mc->default_boot_order = "";
3160a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
3161958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
31629e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
3163e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
31647ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
316594a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
3166c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
3167c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
316820bb648dSDavid Gibson     mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
3169535455fdSIgor Mammedov     mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
3170cf632463SBharata B Rao     hc->unplug_request = spapr_machine_device_unplug_request;
317100b4fbe2SMarcel Apfelbaum 
3172fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
31733daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER8";
3174c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = true;
317571461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
317634316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
31776737d9adSDavid Gibson     smc->phb_placement = spapr_phb_placement;
31781d1be34dSDavid Gibson     vhc->hypercall = emulate_spapr_hypercall;
3179e57ca75cSDavid Gibson     vhc->hpt_mask = spapr_hpt_mask;
3180e57ca75cSDavid Gibson     vhc->map_hptes = spapr_map_hptes;
3181e57ca75cSDavid Gibson     vhc->unmap_hptes = spapr_unmap_hptes;
3182e57ca75cSDavid Gibson     vhc->store_hpte = spapr_store_hpte;
31839861bb3eSSuraj Jitindar Singh     vhc->get_patbe = spapr_get_patbe;
31847844e12bSCédric Le Goater     xic->ics_get = spapr_ics_get;
31857844e12bSCédric Le Goater     xic->ics_resend = spapr_ics_resend;
3186b2fc59aaSCédric Le Goater     xic->icp_get = spapr_icp_get;
31876449da45SCédric Le Goater     ispc->print_info = spapr_pic_print_info;
318855641213SLaurent Vivier     /* Force NUMA node memory size to be a multiple of
318955641213SLaurent Vivier      * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
319055641213SLaurent Vivier      * in which LMBs are represented and hot-added
319155641213SLaurent Vivier      */
319255641213SLaurent Vivier     mc->numa_mem_align_shift = 28;
319353018216SPaolo Bonzini }
319453018216SPaolo Bonzini 
319529ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
319629ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
319729ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
31984aee7362SDavid Gibson     .abstract      = true,
31996ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
320023825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
320187bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
3202183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
320329ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
320471461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
320571461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
320634316482SAlexey Kardashevskiy         { TYPE_NMI },
3207c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
32081d1be34dSDavid Gibson         { TYPE_PPC_VIRTUAL_HYPERVISOR },
32097844e12bSCédric Le Goater         { TYPE_XICS_FABRIC },
32106449da45SCédric Le Goater         { TYPE_INTERRUPT_STATS_PROVIDER },
321171461b0fSAlexey Kardashevskiy         { }
321271461b0fSAlexey Kardashevskiy     },
321329ee3247SAlexey Kardashevskiy };
321429ee3247SAlexey Kardashevskiy 
3215fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
32165013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
32175013c547SDavid Gibson                                                     void *data)      \
32185013c547SDavid Gibson     {                                                                \
32195013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
32205013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
3221fccbc785SDavid Gibson         if (latest) {                                                \
3222fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
3223fccbc785SDavid Gibson             mc->is_default = 1;                                      \
3224fccbc785SDavid Gibson         }                                                            \
32255013c547SDavid Gibson     }                                                                \
32265013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
32275013c547SDavid Gibson     {                                                                \
32285013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
32295013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
32305013c547SDavid Gibson     }                                                                \
32315013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
32325013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
32335013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
32345013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
32355013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
32365013c547SDavid Gibson     };                                                               \
32375013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
32385013c547SDavid Gibson     {                                                                \
32395013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
32405013c547SDavid Gibson     }                                                                \
32410e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
32425013c547SDavid Gibson 
32431c5f29bbSDavid Gibson /*
32443fa14fbeSDavid Gibson  * pseries-2.10
3245db800b21SDavid Gibson  */
32463fa14fbeSDavid Gibson static void spapr_machine_2_10_instance_options(MachineState *machine)
3247db800b21SDavid Gibson {
3248db800b21SDavid Gibson }
3249db800b21SDavid Gibson 
32503fa14fbeSDavid Gibson static void spapr_machine_2_10_class_options(MachineClass *mc)
3251db800b21SDavid Gibson {
3252db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
3253db800b21SDavid Gibson }
3254db800b21SDavid Gibson 
32553fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_10, "2.10", true);
32563fa14fbeSDavid Gibson 
32573fa14fbeSDavid Gibson /*
32583fa14fbeSDavid Gibson  * pseries-2.9
32593fa14fbeSDavid Gibson  */
32603fa14fbeSDavid Gibson #define SPAPR_COMPAT_2_9                                               \
32613fa14fbeSDavid Gibson     HW_COMPAT_2_9
32623fa14fbeSDavid Gibson 
32633fa14fbeSDavid Gibson static void spapr_machine_2_9_instance_options(MachineState *machine)
32643fa14fbeSDavid Gibson {
32653fa14fbeSDavid Gibson     spapr_machine_2_10_instance_options(machine);
32663fa14fbeSDavid Gibson }
32673fa14fbeSDavid Gibson 
32683fa14fbeSDavid Gibson static void spapr_machine_2_9_class_options(MachineClass *mc)
32693fa14fbeSDavid Gibson {
32703fa14fbeSDavid Gibson     spapr_machine_2_10_class_options(mc);
32713fa14fbeSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_9);
32723fa14fbeSDavid Gibson }
32733fa14fbeSDavid Gibson 
32743fa14fbeSDavid Gibson DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
3275fa325e6cSDavid Gibson 
3276fa325e6cSDavid Gibson /*
3277fa325e6cSDavid Gibson  * pseries-2.8
3278fa325e6cSDavid Gibson  */
3279fa325e6cSDavid Gibson #define SPAPR_COMPAT_2_8                                        \
328082516263SDavid Gibson     HW_COMPAT_2_8                                               \
328182516263SDavid Gibson     {                                                           \
328282516263SDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,                 \
328382516263SDavid Gibson         .property = "pcie-extended-configuration-space",        \
328482516263SDavid Gibson         .value    = "off",                                      \
328582516263SDavid Gibson     },
3286fa325e6cSDavid Gibson 
3287fa325e6cSDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
3288fa325e6cSDavid Gibson {
3289fa325e6cSDavid Gibson     spapr_machine_2_9_instance_options(machine);
3290fa325e6cSDavid Gibson }
3291fa325e6cSDavid Gibson 
3292fa325e6cSDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
3293fa325e6cSDavid Gibson {
3294fa325e6cSDavid Gibson     spapr_machine_2_9_class_options(mc);
3295fa325e6cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_8);
329655641213SLaurent Vivier     mc->numa_mem_align_shift = 23;
3297fa325e6cSDavid Gibson }
3298fa325e6cSDavid Gibson 
3299fa325e6cSDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
3300db800b21SDavid Gibson 
3301db800b21SDavid Gibson /*
33021ea1eefcSBharata B Rao  * pseries-2.7
33031ea1eefcSBharata B Rao  */
3304db800b21SDavid Gibson #define SPAPR_COMPAT_2_7                            \
3305db800b21SDavid Gibson     HW_COMPAT_2_7                                   \
3306357d1e3bSDavid Gibson     {                                               \
3307357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3308357d1e3bSDavid Gibson         .property = "mem_win_size",                 \
3309357d1e3bSDavid Gibson         .value    = stringify(SPAPR_PCI_2_7_MMIO_WIN_SIZE),\
3310357d1e3bSDavid Gibson     },                                              \
3311357d1e3bSDavid Gibson     {                                               \
3312357d1e3bSDavid Gibson         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,     \
3313357d1e3bSDavid Gibson         .property = "mem64_win_size",               \
3314357d1e3bSDavid Gibson         .value    = "0",                            \
3315146c11f1SDavid Gibson     },                                              \
3316146c11f1SDavid Gibson     {                                               \
3317146c11f1SDavid Gibson         .driver = TYPE_POWERPC_CPU,                 \
3318146c11f1SDavid Gibson         .property = "pre-2.8-migration",            \
3319146c11f1SDavid Gibson         .value    = "on",                           \
33205c4537bdSDavid Gibson     },                                              \
33215c4537bdSDavid Gibson     {                                               \
33225c4537bdSDavid Gibson         .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,       \
33235c4537bdSDavid Gibson         .property = "pre-2.8-migration",            \
33245c4537bdSDavid Gibson         .value    = "on",                           \
3325357d1e3bSDavid Gibson     },
3326357d1e3bSDavid Gibson 
3327357d1e3bSDavid Gibson static void phb_placement_2_7(sPAPRMachineState *spapr, uint32_t index,
3328357d1e3bSDavid Gibson                               uint64_t *buid, hwaddr *pio,
3329357d1e3bSDavid Gibson                               hwaddr *mmio32, hwaddr *mmio64,
3330357d1e3bSDavid Gibson                               unsigned n_dma, uint32_t *liobns, Error **errp)
3331357d1e3bSDavid Gibson {
3332357d1e3bSDavid Gibson     /* Legacy PHB placement for pseries-2.7 and earlier machine types */
3333357d1e3bSDavid Gibson     const uint64_t base_buid = 0x800000020000000ULL;
3334357d1e3bSDavid Gibson     const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
3335357d1e3bSDavid Gibson     const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
3336357d1e3bSDavid Gibson     const hwaddr pio_offset = 0x80000000; /* 2 GiB */
3337357d1e3bSDavid Gibson     const uint32_t max_index = 255;
3338357d1e3bSDavid Gibson     const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
3339357d1e3bSDavid Gibson 
3340357d1e3bSDavid Gibson     uint64_t ram_top = MACHINE(spapr)->ram_size;
3341357d1e3bSDavid Gibson     hwaddr phb0_base, phb_base;
3342357d1e3bSDavid Gibson     int i;
3343357d1e3bSDavid Gibson 
3344357d1e3bSDavid Gibson     /* Do we have hotpluggable memory? */
3345357d1e3bSDavid Gibson     if (MACHINE(spapr)->maxram_size > ram_top) {
3346357d1e3bSDavid Gibson         /* Can't just use maxram_size, because there may be an
3347357d1e3bSDavid Gibson          * alignment gap between normal and hotpluggable memory
3348357d1e3bSDavid Gibson          * regions */
3349357d1e3bSDavid Gibson         ram_top = spapr->hotplug_memory.base +
3350357d1e3bSDavid Gibson             memory_region_size(&spapr->hotplug_memory.mr);
3351357d1e3bSDavid Gibson     }
3352357d1e3bSDavid Gibson 
3353357d1e3bSDavid Gibson     phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
3354357d1e3bSDavid Gibson 
3355357d1e3bSDavid Gibson     if (index > max_index) {
3356357d1e3bSDavid Gibson         error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
3357357d1e3bSDavid Gibson                    max_index);
3358357d1e3bSDavid Gibson         return;
3359357d1e3bSDavid Gibson     }
3360357d1e3bSDavid Gibson 
3361357d1e3bSDavid Gibson     *buid = base_buid + index;
3362357d1e3bSDavid Gibson     for (i = 0; i < n_dma; ++i) {
3363357d1e3bSDavid Gibson         liobns[i] = SPAPR_PCI_LIOBN(index, i);
3364357d1e3bSDavid Gibson     }
3365357d1e3bSDavid Gibson 
3366357d1e3bSDavid Gibson     phb_base = phb0_base + index * phb_spacing;
3367357d1e3bSDavid Gibson     *pio = phb_base + pio_offset;
3368357d1e3bSDavid Gibson     *mmio32 = phb_base + mmio_offset;
3369357d1e3bSDavid Gibson     /*
3370357d1e3bSDavid Gibson      * We don't set the 64-bit MMIO window, relying on the PHB's
3371357d1e3bSDavid Gibson      * fallback behaviour of automatically splitting a large "32-bit"
3372357d1e3bSDavid Gibson      * window into contiguous 32-bit and 64-bit windows
3373357d1e3bSDavid Gibson      */
3374357d1e3bSDavid Gibson }
3375db800b21SDavid Gibson 
33761ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
33771ea1eefcSBharata B Rao {
3378f6229214SMichael Roth     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
3379f6229214SMichael Roth 
3380672de881SMichael Roth     spapr_machine_2_8_instance_options(machine);
3381f6229214SMichael Roth     spapr->use_hotplug_event_source = false;
33821ea1eefcSBharata B Rao }
33831ea1eefcSBharata B Rao 
33841ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
33851ea1eefcSBharata B Rao {
33863daa4a9fSThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
33873daa4a9fSThomas Huth 
3388db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
33893daa4a9fSThomas Huth     smc->tcg_default_cpu = "POWER7";
3390db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
3391357d1e3bSDavid Gibson     smc->phb_placement = phb_placement_2_7;
33921ea1eefcSBharata B Rao }
33931ea1eefcSBharata B Rao 
3394db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
33951ea1eefcSBharata B Rao 
33961ea1eefcSBharata B Rao /*
33974b23699cSDavid Gibson  * pseries-2.6
33984b23699cSDavid Gibson  */
33991ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
3400ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
3401ae4de14cSAlexey Kardashevskiy     { \
3402ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3403ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
3404ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
3405ae4de14cSAlexey Kardashevskiy     },
34061ea1eefcSBharata B Rao 
34074b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
34084b23699cSDavid Gibson {
3409672de881SMichael Roth     spapr_machine_2_7_instance_options(machine);
34104b23699cSDavid Gibson }
34114b23699cSDavid Gibson 
34124b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
34134b23699cSDavid Gibson {
34141ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
3415c5514d0eSIgor Mammedov     mc->has_hotpluggable_cpus = false;
34161ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
34174b23699cSDavid Gibson }
34184b23699cSDavid Gibson 
34191ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
34204b23699cSDavid Gibson 
34214b23699cSDavid Gibson /*
34221c5f29bbSDavid Gibson  * pseries-2.5
34231c5f29bbSDavid Gibson  */
34244b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
342557c522f4SThomas Huth     HW_COMPAT_2_5 \
342657c522f4SThomas Huth     { \
342757c522f4SThomas Huth         .driver   = "spapr-vlan", \
342857c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
342957c522f4SThomas Huth         .value    = "off", \
343057c522f4SThomas Huth     },
34314b23699cSDavid Gibson 
34325013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
34331c5f29bbSDavid Gibson {
3434672de881SMichael Roth     spapr_machine_2_6_instance_options(machine);
34355013c547SDavid Gibson }
34365013c547SDavid Gibson 
34375013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
34385013c547SDavid Gibson {
343957040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
344057040d45SThomas Huth 
34414b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
344257040d45SThomas Huth     smc->use_ohci_by_default = true;
34434b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
34441c5f29bbSDavid Gibson }
34451c5f29bbSDavid Gibson 
34464b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
34471c5f29bbSDavid Gibson 
34481c5f29bbSDavid Gibson /*
34491c5f29bbSDavid Gibson  * pseries-2.4
34501c5f29bbSDavid Gibson  */
345180fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
345280fd50f9SCornelia Huck         HW_COMPAT_2_4
345380fd50f9SCornelia Huck 
34545013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
34551c5f29bbSDavid Gibson {
34565013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
34575013c547SDavid Gibson }
34581c5f29bbSDavid Gibson 
34595013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
34605013c547SDavid Gibson {
3461fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3462fc9f38c3SDavid Gibson 
3463fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
3464fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
3465f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
34661c5f29bbSDavid Gibson }
34671c5f29bbSDavid Gibson 
3468fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
34691c5f29bbSDavid Gibson 
34701c5f29bbSDavid Gibson /*
34711c5f29bbSDavid Gibson  * pseries-2.3
34721c5f29bbSDavid Gibson  */
347338ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
34747619c7b0SMichael Roth         HW_COMPAT_2_3 \
34757619c7b0SMichael Roth         {\
34767619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
34777619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
34787619c7b0SMichael Roth             .value    = "off",\
34797619c7b0SMichael Roth         },
348038ff32c6SEduardo Habkost 
34815013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
34821c5f29bbSDavid Gibson {
34835013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
34841c5f29bbSDavid Gibson     savevm_skip_section_footers();
34851c5f29bbSDavid Gibson     global_state_set_optional();
348609b5e30dSGreg Kurz     savevm_skip_configuration();
34871c5f29bbSDavid Gibson }
34881c5f29bbSDavid Gibson 
34895013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
34901c5f29bbSDavid Gibson {
3491fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
3492f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
34931c5f29bbSDavid Gibson }
3494fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
34951c5f29bbSDavid Gibson 
34961c5f29bbSDavid Gibson /*
34971c5f29bbSDavid Gibson  * pseries-2.2
34981c5f29bbSDavid Gibson  */
34991c5f29bbSDavid Gibson 
3500b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
35014dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
3502b194df47SAlexey Kardashevskiy         {\
3503b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
3504b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
3505b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
3506dd754bafSEduardo Habkost         },
3507b194df47SAlexey Kardashevskiy 
35085013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
3509b0e966d0SJason Wang {
35105013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
3511cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
3512b0e966d0SJason Wang }
3513b0e966d0SJason Wang 
35145013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
3515b0e966d0SJason Wang {
3516fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
3517f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
35181c5f29bbSDavid Gibson }
3519fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
35201c5f29bbSDavid Gibson 
35211c5f29bbSDavid Gibson /*
35221c5f29bbSDavid Gibson  * pseries-2.1
35231c5f29bbSDavid Gibson  */
35241c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
35251c5f29bbSDavid Gibson         HW_COMPAT_2_1
35261c5f29bbSDavid Gibson 
35275013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
35281c5f29bbSDavid Gibson {
35295013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
35301c5f29bbSDavid Gibson }
35311c5f29bbSDavid Gibson 
35325013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
3533b0e966d0SJason Wang {
3534fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
3535f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
35366026db45SAlexey Kardashevskiy }
3537fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
35386026db45SAlexey Kardashevskiy 
353929ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
354029ee3247SAlexey Kardashevskiy {
354129ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
354229ee3247SAlexey Kardashevskiy }
354329ee3247SAlexey Kardashevskiy 
354429ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
3545