xref: /openbmc/qemu/hw/ppc/spapr.c (revision 4cbec30d)
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"
3953018216SPaolo Bonzini #include "sysemu/kvm.h"
40c20d332aSBharata B Rao #include "sysemu/device_tree.h"
4153018216SPaolo Bonzini #include "kvm_ppc.h"
42ff14e817SDr. David Alan Gilbert #include "migration/migration.h"
434be21d56SDavid Gibson #include "mmu-hash64.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"
6753018216SPaolo Bonzini 
6868a27b20SMichael S. Tsirkin #include "hw/compat.h"
69f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
7094a94e4cSBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
712474bfd4SIgor Mammedov #include "qmp-commands.h"
7268a27b20SMichael S. Tsirkin 
7353018216SPaolo Bonzini #include <libfdt.h>
7453018216SPaolo Bonzini 
7553018216SPaolo Bonzini /* SLOF memory layout:
7653018216SPaolo Bonzini  *
7753018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
7853018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
7953018216SPaolo Bonzini  *
8053018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
8153018216SPaolo Bonzini  * and more
8253018216SPaolo Bonzini  *
8353018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
8453018216SPaolo Bonzini  */
8538b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
8653018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
87b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
8853018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
8953018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
9053018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
9153018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
9253018216SPaolo Bonzini 
9353018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
9453018216SPaolo Bonzini 
9553018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9653018216SPaolo Bonzini 
9753018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9853018216SPaolo Bonzini 
99c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers,
10034f2af3dSMarkus Armbruster                                   int nr_irqs, Error **errp)
101c04d6cfaSAnthony Liguori {
10234f2af3dSMarkus Armbruster     Error *err = NULL;
103c04d6cfaSAnthony Liguori     DeviceState *dev;
104c04d6cfaSAnthony Liguori 
105c04d6cfaSAnthony Liguori     dev = qdev_create(NULL, type);
106c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
107c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
10834f2af3dSMarkus Armbruster     object_property_set_bool(OBJECT(dev), true, "realized", &err);
10934f2af3dSMarkus Armbruster     if (err) {
11034f2af3dSMarkus Armbruster         error_propagate(errp, err);
11134f2af3dSMarkus Armbruster         object_unparent(OBJECT(dev));
112c04d6cfaSAnthony Liguori         return NULL;
113c04d6cfaSAnthony Liguori     }
1145a3d7b23SAlexey Kardashevskiy     return XICS_COMMON(dev);
115c04d6cfaSAnthony Liguori }
116c04d6cfaSAnthony Liguori 
117446f16a6SMarcel Apfelbaum static XICSState *xics_system_init(MachineState *machine,
1181e49182dSDavid Gibson                                    int nr_servers, int nr_irqs, Error **errp)
119c04d6cfaSAnthony Liguori {
12027f24582SBenjamin Herrenschmidt     XICSState *xics = NULL;
121c04d6cfaSAnthony Liguori 
12211ad93f6SDavid Gibson     if (kvm_enabled()) {
12334f2af3dSMarkus Armbruster         Error *err = NULL;
12434f2af3dSMarkus Armbruster 
125446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_allowed(machine)) {
12627f24582SBenjamin Herrenschmidt             xics = try_create_xics(TYPE_XICS_SPAPR_KVM, nr_servers, nr_irqs,
127161deaf2SBenjamin Herrenschmidt                                    &err);
12811ad93f6SDavid Gibson         }
12927f24582SBenjamin Herrenschmidt         if (machine_kernel_irqchip_required(machine) && !xics) {
130b83baa60SMarkus Armbruster             error_reportf_err(err,
131b83baa60SMarkus Armbruster                               "kernel_irqchip requested but unavailable: ");
132b83baa60SMarkus Armbruster         } else {
133903a41d3SStefano Dong (董兴水)             error_free(err);
13411ad93f6SDavid Gibson         }
135b83baa60SMarkus Armbruster     }
13611ad93f6SDavid Gibson 
13727f24582SBenjamin Herrenschmidt     if (!xics) {
13827f24582SBenjamin Herrenschmidt         xics = try_create_xics(TYPE_XICS_SPAPR, nr_servers, nr_irqs, errp);
139c04d6cfaSAnthony Liguori     }
140c04d6cfaSAnthony Liguori 
14127f24582SBenjamin Herrenschmidt     return xics;
142c04d6cfaSAnthony Liguori }
143c04d6cfaSAnthony Liguori 
144833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
145833d4668SAlexey Kardashevskiy                                   int smt_threads)
146833d4668SAlexey Kardashevskiy {
147833d4668SAlexey Kardashevskiy     int i, ret = 0;
148833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
149833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
150833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
151833d4668SAlexey Kardashevskiy 
1526d9412eaSAlexey Kardashevskiy     if (cpu->cpu_version) {
1534bce526eSLaurent Dufour         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version);
1546d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1556d9412eaSAlexey Kardashevskiy             return ret;
1566d9412eaSAlexey Kardashevskiy         }
1576d9412eaSAlexey Kardashevskiy     }
1586d9412eaSAlexey Kardashevskiy 
159833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
160833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
161833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
162833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
163833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
164833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
165833d4668SAlexey Kardashevskiy     }
166833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
167833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
168833d4668SAlexey Kardashevskiy     if (ret < 0) {
169833d4668SAlexey Kardashevskiy         return ret;
170833d4668SAlexey Kardashevskiy     }
171833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
172833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
173833d4668SAlexey Kardashevskiy 
174833d4668SAlexey Kardashevskiy     return ret;
175833d4668SAlexey Kardashevskiy }
176833d4668SAlexey Kardashevskiy 
1770da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
1780da6f3feSBharata B Rao {
1790da6f3feSBharata B Rao     int ret = 0;
1800da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
1810da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
1820da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
1830da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1840da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1850da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1860da6f3feSBharata B Rao                                 cpu_to_be32(cs->numa_node),
1870da6f3feSBharata B Rao                                 cpu_to_be32(index)};
1880da6f3feSBharata B Rao 
1890da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
1900da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
1910da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
1920da6f3feSBharata B Rao                           sizeof(associativity));
1930da6f3feSBharata B Rao     }
1940da6f3feSBharata B Rao 
1950da6f3feSBharata B Rao     return ret;
1960da6f3feSBharata B Rao }
1970da6f3feSBharata B Rao 
19828e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
19953018216SPaolo Bonzini {
20082677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
20182677ed2SAlexey Kardashevskiy     CPUState *cs;
20253018216SPaolo Bonzini     char cpu_model[32];
20353018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
20453018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
20553018216SPaolo Bonzini 
20682677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
20782677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
20882677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
20982677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
21053018216SPaolo Bonzini 
2110f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
21253018216SPaolo Bonzini             continue;
21353018216SPaolo Bonzini         }
21453018216SPaolo Bonzini 
21582677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
21653018216SPaolo Bonzini 
21782677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
21882677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
21982677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
22082677ed2SAlexey Kardashevskiy                                           "cpus");
22182677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
22282677ed2SAlexey Kardashevskiy                 return cpus_offset;
22382677ed2SAlexey Kardashevskiy             }
22482677ed2SAlexey Kardashevskiy         }
22582677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
22682677ed2SAlexey Kardashevskiy         if (offset < 0) {
22782677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
22853018216SPaolo Bonzini             if (offset < 0) {
22953018216SPaolo Bonzini                 return offset;
23053018216SPaolo Bonzini             }
23182677ed2SAlexey Kardashevskiy         }
23253018216SPaolo Bonzini 
2330da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
2340da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
23553018216SPaolo Bonzini         if (ret < 0) {
23653018216SPaolo Bonzini             return ret;
23753018216SPaolo Bonzini         }
23853018216SPaolo Bonzini 
2390da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
24053018216SPaolo Bonzini         if (ret < 0) {
24153018216SPaolo Bonzini             return ret;
24253018216SPaolo Bonzini         }
243833d4668SAlexey Kardashevskiy 
24482677ed2SAlexey Kardashevskiy         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
2452a48d993SAlexey Kardashevskiy                                      ppc_get_compat_smt_threads(cpu));
246833d4668SAlexey Kardashevskiy         if (ret < 0) {
247833d4668SAlexey Kardashevskiy             return ret;
248833d4668SAlexey Kardashevskiy         }
24953018216SPaolo Bonzini     }
25053018216SPaolo Bonzini     return ret;
25153018216SPaolo Bonzini }
25253018216SPaolo Bonzini 
253b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
254b082d65aSAlexey Kardashevskiy {
255fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
256fb164994SDavid Gibson 
257b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
258b082d65aSAlexey Kardashevskiy         int i;
259b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
260b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
261fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
262fb164994SDavid Gibson                            machine->ram_size);
263b082d65aSAlexey Kardashevskiy             }
264b082d65aSAlexey Kardashevskiy         }
265b082d65aSAlexey Kardashevskiy     }
266fb164994SDavid Gibson     return machine->ram_size;
267b082d65aSAlexey Kardashevskiy }
268b082d65aSAlexey Kardashevskiy 
269a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
270a1d59c0fSAlexey Kardashevskiy {
271a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
272a1d59c0fSAlexey Kardashevskiy }
27353018216SPaolo Bonzini 
2743bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base,
27553018216SPaolo Bonzini                                    hwaddr initrd_size,
27653018216SPaolo Bonzini                                    hwaddr kernel_size,
27716457e7fSBenjamin Herrenschmidt                                    bool little_endian,
27853018216SPaolo Bonzini                                    const char *kernel_cmdline,
27953018216SPaolo Bonzini                                    uint32_t epow_irq)
28053018216SPaolo Bonzini {
28153018216SPaolo Bonzini     void *fdt;
28253018216SPaolo Bonzini     uint32_t start_prop = cpu_to_be32(initrd_base);
28353018216SPaolo Bonzini     uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
284a1d59c0fSAlexey Kardashevskiy     GString *hypertas = g_string_sized_new(256);
285a1d59c0fSAlexey Kardashevskiy     GString *qemu_hypertas = g_string_sized_new(256);
28653018216SPaolo Bonzini     uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
2879e734e3dSBharata B Rao     uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(max_cpus)};
28853018216SPaolo Bonzini     unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
289ef951443SNikunj A Dadhania     char *buf;
29053018216SPaolo Bonzini 
291a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-pft");
292a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-term");
293a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-dabr");
294a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-interrupt");
295a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-tce");
296a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-vio");
297a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-splpar");
298a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-bulk");
299a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-set-mode");
3006cc09e26SThomas Huth     add_str(hypertas, "hcall-sprg0");
3016cc09e26SThomas Huth     add_str(hypertas, "hcall-copy");
3026cc09e26SThomas Huth     add_str(hypertas, "hcall-debug");
303a1d59c0fSAlexey Kardashevskiy     add_str(qemu_hypertas, "hcall-memop1");
304a1d59c0fSAlexey Kardashevskiy 
30553018216SPaolo Bonzini     fdt = g_malloc0(FDT_MAX_SIZE);
30653018216SPaolo Bonzini     _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
30753018216SPaolo Bonzini 
30853018216SPaolo Bonzini     if (kernel_size) {
30953018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
31053018216SPaolo Bonzini     }
31153018216SPaolo Bonzini     if (initrd_size) {
31253018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
31353018216SPaolo Bonzini     }
31453018216SPaolo Bonzini     _FDT((fdt_finish_reservemap(fdt)));
31553018216SPaolo Bonzini 
31653018216SPaolo Bonzini     /* Root node */
31753018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "")));
31853018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "chrp")));
31953018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
320fa388916SAnthony Liguori     _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
32153018216SPaolo Bonzini 
322ef951443SNikunj A Dadhania     /*
323ef951443SNikunj A Dadhania      * Add info to guest to indentify which host is it being run on
324ef951443SNikunj A Dadhania      * and what is the uuid of the guest
325ef951443SNikunj A Dadhania      */
326ef951443SNikunj A Dadhania     if (kvmppc_get_host_model(&buf)) {
327ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-model", buf)));
328ef951443SNikunj A Dadhania         g_free(buf);
329ef951443SNikunj A Dadhania     }
330ef951443SNikunj A Dadhania     if (kvmppc_get_host_serial(&buf)) {
331ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-serial", buf)));
332ef951443SNikunj A Dadhania         g_free(buf);
333ef951443SNikunj A Dadhania     }
334ef951443SNikunj A Dadhania 
3359c5ce8dbSFam Zheng     buf = qemu_uuid_unparse_strdup(&qemu_uuid);
336ef951443SNikunj A Dadhania 
337ef951443SNikunj A Dadhania     _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
3383dc0a66dSAlexey Kardashevskiy     if (qemu_uuid_set) {
3393dc0a66dSAlexey Kardashevskiy         _FDT((fdt_property_string(fdt, "system-id", buf)));
3403dc0a66dSAlexey Kardashevskiy     }
341ef951443SNikunj A Dadhania     g_free(buf);
342ef951443SNikunj A Dadhania 
3432c1aaa81SSam Bobroff     if (qemu_get_vm_name()) {
3442c1aaa81SSam Bobroff         _FDT((fdt_property_string(fdt, "ibm,partition-name",
3452c1aaa81SSam Bobroff                                   qemu_get_vm_name())));
3462c1aaa81SSam Bobroff     }
3472c1aaa81SSam Bobroff 
34853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
34953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
35053018216SPaolo Bonzini 
35153018216SPaolo Bonzini     /* /chosen */
35253018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "chosen")));
35353018216SPaolo Bonzini 
35453018216SPaolo Bonzini     /* Set Form1_affinity */
35553018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
35653018216SPaolo Bonzini 
35753018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
35853018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-start",
35953018216SPaolo Bonzini                        &start_prop, sizeof(start_prop))));
36053018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-end",
36153018216SPaolo Bonzini                        &end_prop, sizeof(end_prop))));
36253018216SPaolo Bonzini     if (kernel_size) {
36353018216SPaolo Bonzini         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
36453018216SPaolo Bonzini                               cpu_to_be64(kernel_size) };
36553018216SPaolo Bonzini 
36653018216SPaolo Bonzini         _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
36716457e7fSBenjamin Herrenschmidt         if (little_endian) {
36816457e7fSBenjamin Herrenschmidt             _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0)));
36916457e7fSBenjamin Herrenschmidt         }
37053018216SPaolo Bonzini     }
371cc84c0f3SAvik Sil     if (boot_menu) {
372cc84c0f3SAvik Sil         _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu)));
373cc84c0f3SAvik Sil     }
37453018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
37553018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
37653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
37753018216SPaolo Bonzini 
37853018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
37953018216SPaolo Bonzini 
38053018216SPaolo Bonzini     /* RTAS */
38153018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "rtas")));
38253018216SPaolo Bonzini 
383da95324eSAlexey Kardashevskiy     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
384da95324eSAlexey Kardashevskiy         add_str(hypertas, "hcall-multi-tce");
385da95324eSAlexey Kardashevskiy     }
386a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str,
387a1d59c0fSAlexey Kardashevskiy                        hypertas->len)));
388a1d59c0fSAlexey Kardashevskiy     g_string_free(hypertas, TRUE);
389a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str,
390a1d59c0fSAlexey Kardashevskiy                        qemu_hypertas->len)));
391a1d59c0fSAlexey Kardashevskiy     g_string_free(qemu_hypertas, TRUE);
39253018216SPaolo Bonzini 
39353018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
39453018216SPaolo Bonzini         refpoints, sizeof(refpoints))));
39553018216SPaolo Bonzini 
39653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
39779853e18STyrel Datwyler     _FDT((fdt_property_cell(fdt, "rtas-event-scan-rate",
39879853e18STyrel Datwyler                             RTAS_EVENT_SCAN_RATE)));
39953018216SPaolo Bonzini 
400226419d6SMichael S. Tsirkin     if (msi_nonbroken) {
401a95f9922SSam Bobroff         _FDT((fdt_property(fdt, "ibm,change-msix-capable", NULL, 0)));
402a95f9922SSam Bobroff     }
403a95f9922SSam Bobroff 
4042e14072fSNikunj A Dadhania     /*
4059d632f5fSzhanghailiang      * According to PAPR, rtas ibm,os-term does not guarantee a return
4062e14072fSNikunj A Dadhania      * back to the guest cpu.
4072e14072fSNikunj A Dadhania      *
4082e14072fSNikunj A Dadhania      * While an additional ibm,extended-os-term property indicates that
4092e14072fSNikunj A Dadhania      * rtas call return will always occur. Set this property.
4102e14072fSNikunj A Dadhania      */
4112e14072fSNikunj A Dadhania     _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0)));
4122e14072fSNikunj A Dadhania 
41353018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
41453018216SPaolo Bonzini 
41553018216SPaolo Bonzini     /* interrupt controller */
41653018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "interrupt-controller")));
41753018216SPaolo Bonzini 
41853018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type",
41953018216SPaolo Bonzini                               "PowerPC-External-Interrupt-Presentation")));
42053018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
42153018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
42253018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
42353018216SPaolo Bonzini                        interrupt_server_ranges_prop,
42453018216SPaolo Bonzini                        sizeof(interrupt_server_ranges_prop))));
42553018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
42653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
42753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
42853018216SPaolo Bonzini 
42953018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
43053018216SPaolo Bonzini 
43153018216SPaolo Bonzini     /* vdevice */
43253018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "vdevice")));
43353018216SPaolo Bonzini 
43453018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
43553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
43653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
43753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
43853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
43953018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
44053018216SPaolo Bonzini 
44153018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
44253018216SPaolo Bonzini 
44353018216SPaolo Bonzini     /* event-sources */
44453018216SPaolo Bonzini     spapr_events_fdt_skel(fdt, epow_irq);
44553018216SPaolo Bonzini 
446f7d69146SAlexander Graf     /* /hypervisor node */
447f7d69146SAlexander Graf     if (kvm_enabled()) {
448f7d69146SAlexander Graf         uint8_t hypercall[16];
449f7d69146SAlexander Graf 
450f7d69146SAlexander Graf         /* indicate KVM hypercall interface */
451f7d69146SAlexander Graf         _FDT((fdt_begin_node(fdt, "hypervisor")));
452f7d69146SAlexander Graf         _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
453f7d69146SAlexander Graf         if (kvmppc_has_cap_fixup_hcalls()) {
454f7d69146SAlexander Graf             /*
455f7d69146SAlexander Graf              * Older KVM versions with older guest kernels were broken with the
456f7d69146SAlexander Graf              * magic page, don't allow the guest to map it.
457f7d69146SAlexander Graf              */
4580ddbd053SAlexey Kardashevskiy             if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
4590ddbd053SAlexey Kardashevskiy                                       sizeof(hypercall))) {
460f7d69146SAlexander Graf                 _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
461f7d69146SAlexander Graf                                    sizeof(hypercall))));
462f7d69146SAlexander Graf             }
4630ddbd053SAlexey Kardashevskiy         }
464f7d69146SAlexander Graf         _FDT((fdt_end_node(fdt)));
465f7d69146SAlexander Graf     }
466f7d69146SAlexander Graf 
46753018216SPaolo Bonzini     _FDT((fdt_end_node(fdt))); /* close root node */
46853018216SPaolo Bonzini     _FDT((fdt_finish(fdt)));
46953018216SPaolo Bonzini 
47053018216SPaolo Bonzini     return fdt;
47153018216SPaolo Bonzini }
47253018216SPaolo Bonzini 
47303d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
47426a8c353SAlexey Kardashevskiy                                        hwaddr size)
47526a8c353SAlexey Kardashevskiy {
47626a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
47726a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
47826a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
479c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
48026a8c353SAlexey Kardashevskiy     };
48126a8c353SAlexey Kardashevskiy     char mem_name[32];
48226a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
48326a8c353SAlexey Kardashevskiy     int off;
48426a8c353SAlexey Kardashevskiy 
48526a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
48626a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
48726a8c353SAlexey Kardashevskiy 
48826a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
48926a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
49026a8c353SAlexey Kardashevskiy     _FDT(off);
49126a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
49226a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
49326a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
49426a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
49526a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
49603d196b7SBharata B Rao     return off;
49726a8c353SAlexey Kardashevskiy }
49826a8c353SAlexey Kardashevskiy 
49928e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
50053018216SPaolo Bonzini {
501fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
5027db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
5037db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
5047db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
5057db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
50653018216SPaolo Bonzini 
5077db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
5087db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
5097db8a127SAlexey Kardashevskiy         nb_nodes = 1;
510fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
5117db8a127SAlexey Kardashevskiy         nodes = &ramnode;
5125fe269b1SPaul Mackerras     }
51353018216SPaolo Bonzini 
5147db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
5157db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
5167db8a127SAlexey Kardashevskiy             continue;
51753018216SPaolo Bonzini         }
518fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
5195fe269b1SPaul Mackerras             node_size = 0;
5205fe269b1SPaul Mackerras         } else {
5217db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
522fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
523fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
5245fe269b1SPaul Mackerras             }
5255fe269b1SPaul Mackerras         }
5267db8a127SAlexey Kardashevskiy         if (!mem_start) {
5277db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
528e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
5297db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
5307db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
5317db8a127SAlexey Kardashevskiy         }
5326010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
5336010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
5346010818cSAlexey Kardashevskiy 
5356010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
5366010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
5376010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
5386010818cSAlexey Kardashevskiy             }
5396010818cSAlexey Kardashevskiy 
5406010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
5416010818cSAlexey Kardashevskiy             node_size -= sizetmp;
5426010818cSAlexey Kardashevskiy             mem_start += sizetmp;
5436010818cSAlexey Kardashevskiy         }
54453018216SPaolo Bonzini     }
54553018216SPaolo Bonzini 
54653018216SPaolo Bonzini     return 0;
54753018216SPaolo Bonzini }
54853018216SPaolo Bonzini 
549230bf719SThomas Huth /* Populate the "ibm,pa-features" property */
550230bf719SThomas Huth static void spapr_populate_pa_features(CPUPPCState *env, void *fdt, int offset)
551230bf719SThomas Huth {
552230bf719SThomas Huth     uint8_t pa_features_206[] = { 6, 0,
553230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
554230bf719SThomas Huth     uint8_t pa_features_207[] = { 24, 0,
555230bf719SThomas Huth         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
556230bf719SThomas Huth         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
557230bf719SThomas Huth         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
558230bf719SThomas Huth         0x80, 0x00, 0x80, 0x00, 0x80, 0x00 };
559230bf719SThomas Huth     uint8_t *pa_features;
560230bf719SThomas Huth     size_t pa_size;
561230bf719SThomas Huth 
562*4cbec30dSThomas Huth     switch (env->mmu_model) {
563*4cbec30dSThomas Huth     case POWERPC_MMU_2_06:
564*4cbec30dSThomas Huth     case POWERPC_MMU_2_06a:
565230bf719SThomas Huth         pa_features = pa_features_206;
566230bf719SThomas Huth         pa_size = sizeof(pa_features_206);
567*4cbec30dSThomas Huth         break;
568*4cbec30dSThomas Huth     case POWERPC_MMU_2_07:
569*4cbec30dSThomas Huth     case POWERPC_MMU_2_07a:
570230bf719SThomas Huth         pa_features = pa_features_207;
571230bf719SThomas Huth         pa_size = sizeof(pa_features_207);
572*4cbec30dSThomas Huth         break;
573*4cbec30dSThomas Huth     default:
574*4cbec30dSThomas Huth         return;
575230bf719SThomas Huth     }
576230bf719SThomas Huth 
577230bf719SThomas Huth     if (env->ci_large_pages) {
578230bf719SThomas Huth         /*
579230bf719SThomas Huth          * Note: we keep CI large pages off by default because a 64K capable
580230bf719SThomas Huth          * guest provisioned with large pages might otherwise try to map a qemu
581230bf719SThomas Huth          * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
582230bf719SThomas Huth          * even if that qemu runs on a 4k host.
583230bf719SThomas Huth          * We dd this bit back here if we are confident this is not an issue
584230bf719SThomas Huth          */
585230bf719SThomas Huth         pa_features[3] |= 0x20;
586230bf719SThomas Huth     }
587230bf719SThomas Huth 
588230bf719SThomas Huth     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
589230bf719SThomas Huth }
590230bf719SThomas Huth 
5910da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
5920da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
5930da6f3feSBharata B Rao {
5940da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
5950da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
5960da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
5970da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
5980da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
5990da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
600afd10a0fSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
601afd10a0fSBharata B Rao         : SPAPR_TIMEBASE_FREQ;
6020da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
6030da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
6040da6f3feSBharata B Rao     size_t page_sizes_prop_size;
60522419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
6060da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
607af81cf32SBharata B Rao     sPAPRDRConnector *drc;
608af81cf32SBharata B Rao     sPAPRDRConnectorClass *drck;
609af81cf32SBharata B Rao     int drc_index;
610af81cf32SBharata B Rao 
611af81cf32SBharata B Rao     drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_CPU, index);
612af81cf32SBharata B Rao     if (drc) {
613af81cf32SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
614af81cf32SBharata B Rao         drc_index = drck->get_index(drc);
615af81cf32SBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
616af81cf32SBharata B Rao     }
6170da6f3feSBharata B Rao 
6180da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
6190da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
6200da6f3feSBharata B Rao 
6210da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
6220da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
6230da6f3feSBharata B Rao                            env->dcache_line_size)));
6240da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
6250da6f3feSBharata B Rao                            env->dcache_line_size)));
6260da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
6270da6f3feSBharata B Rao                            env->icache_line_size)));
6280da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
6290da6f3feSBharata B Rao                            env->icache_line_size)));
6300da6f3feSBharata B Rao 
6310da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
6320da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
6330da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
6340da6f3feSBharata B Rao     } else {
635ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 dcache size for cpu");
6360da6f3feSBharata B Rao     }
6370da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
6380da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
6390da6f3feSBharata B Rao                                pcc->l1_icache_size)));
6400da6f3feSBharata B Rao     } else {
641ce9863b7SCédric Le Goater         error_report("Warning: Unknown L1 icache size for cpu");
6420da6f3feSBharata B Rao     }
6430da6f3feSBharata B Rao 
6440da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
6450da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
646fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
6470da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
6480da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
6490da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
6500da6f3feSBharata B Rao 
6510da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
6520da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
6530da6f3feSBharata B Rao     }
6540da6f3feSBharata B Rao 
6550da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
6560da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
6570da6f3feSBharata B Rao                           segs, sizeof(segs))));
6580da6f3feSBharata B Rao     }
6590da6f3feSBharata B Rao 
6600da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
6610da6f3feSBharata B Rao      *   0 / no property == no vector extensions
6620da6f3feSBharata B Rao      *   1               == VMX / Altivec available
6630da6f3feSBharata B Rao      *   2               == VSX available */
6640da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
6650da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
6660da6f3feSBharata B Rao 
6670da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
6680da6f3feSBharata B Rao     }
6690da6f3feSBharata B Rao 
6700da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
6710da6f3feSBharata B Rao      *   0 / no property == no DFP
6720da6f3feSBharata B Rao      *   1               == DFP available */
6730da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
6740da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
6750da6f3feSBharata B Rao     }
6760da6f3feSBharata B Rao 
6773654fa95SCédric Le Goater     page_sizes_prop_size = ppc_create_page_sizes_prop(env, page_sizes_prop,
6780da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
6790da6f3feSBharata B Rao     if (page_sizes_prop_size) {
6800da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
6810da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
6820da6f3feSBharata B Rao     }
6830da6f3feSBharata B Rao 
684230bf719SThomas Huth     spapr_populate_pa_features(env, fdt, offset);
68590da0d5aSBenjamin Herrenschmidt 
6860da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
68722419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
6880da6f3feSBharata B Rao 
6890da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
6900da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
6910da6f3feSBharata B Rao 
6920da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
6930da6f3feSBharata B Rao 
6940da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
6950da6f3feSBharata B Rao                                 ppc_get_compat_smt_threads(cpu)));
6960da6f3feSBharata B Rao }
6970da6f3feSBharata B Rao 
6980da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
6990da6f3feSBharata B Rao {
7000da6f3feSBharata B Rao     CPUState *cs;
7010da6f3feSBharata B Rao     int cpus_offset;
7020da6f3feSBharata B Rao     char *nodename;
7030da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
7040da6f3feSBharata B Rao 
7050da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
7060da6f3feSBharata B Rao     _FDT(cpus_offset);
7070da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
7080da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
7090da6f3feSBharata B Rao 
7100da6f3feSBharata B Rao     /*
7110da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
7120da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
7130da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
7140da6f3feSBharata B Rao      */
7150da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
7160da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
7170da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
7180da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
7190da6f3feSBharata B Rao         int offset;
7200da6f3feSBharata B Rao 
7210da6f3feSBharata B Rao         if ((index % smt) != 0) {
7220da6f3feSBharata B Rao             continue;
7230da6f3feSBharata B Rao         }
7240da6f3feSBharata B Rao 
7250da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
7260da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
7270da6f3feSBharata B Rao         g_free(nodename);
7280da6f3feSBharata B Rao         _FDT(offset);
7290da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
7300da6f3feSBharata B Rao     }
7310da6f3feSBharata B Rao 
7320da6f3feSBharata B Rao }
7330da6f3feSBharata B Rao 
73403d196b7SBharata B Rao /*
73503d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
73603d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
73703d196b7SBharata B Rao  * of this device tree node.
73803d196b7SBharata B Rao  */
73903d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
74003d196b7SBharata B Rao {
74103d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
74203d196b7SBharata B Rao     int ret, i, offset;
74303d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
74403d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
745d0e5a8f2SBharata B Rao     uint32_t hotplug_lmb_start = spapr->hotplug_memory.base / lmb_size;
746d0e5a8f2SBharata B Rao     uint32_t nr_lmbs = (spapr->hotplug_memory.base +
747d0e5a8f2SBharata B Rao                        memory_region_size(&spapr->hotplug_memory.mr)) /
748d0e5a8f2SBharata B Rao                        lmb_size;
74903d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
7506663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
75103d196b7SBharata B Rao 
752ef001f06SThomas Huth     /*
753d0e5a8f2SBharata B Rao      * Don't create the node if there is no hotpluggable memory
75416c25aefSBharata B Rao      */
755d0e5a8f2SBharata B Rao     if (machine->ram_size == machine->maxram_size) {
75616c25aefSBharata B Rao         return 0;
75716c25aefSBharata B Rao     }
75816c25aefSBharata B Rao 
75916c25aefSBharata B Rao     /*
760ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
761ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
762ef001f06SThomas Huth      */
763ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
764ef001f06SThomas Huth               * sizeof(uint32_t);
76503d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
76603d196b7SBharata B Rao 
76703d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
76803d196b7SBharata B Rao 
76903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
77003d196b7SBharata B Rao                     sizeof(prop_lmb_size));
77103d196b7SBharata B Rao     if (ret < 0) {
77203d196b7SBharata B Rao         goto out;
77303d196b7SBharata B Rao     }
77403d196b7SBharata B Rao 
77503d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
77603d196b7SBharata B Rao     if (ret < 0) {
77703d196b7SBharata B Rao         goto out;
77803d196b7SBharata B Rao     }
77903d196b7SBharata B Rao 
78003d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
78103d196b7SBharata B Rao     if (ret < 0) {
78203d196b7SBharata B Rao         goto out;
78303d196b7SBharata B Rao     }
78403d196b7SBharata B Rao 
78503d196b7SBharata B Rao     /* ibm,dynamic-memory */
78603d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
78703d196b7SBharata B Rao     cur_index++;
78803d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
789d0e5a8f2SBharata B Rao         uint64_t addr = i * lmb_size;
79003d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
79103d196b7SBharata B Rao 
792d0e5a8f2SBharata B Rao         if (i >= hotplug_lmb_start) {
793d0e5a8f2SBharata B Rao             sPAPRDRConnector *drc;
794d0e5a8f2SBharata B Rao             sPAPRDRConnectorClass *drck;
795d0e5a8f2SBharata B Rao 
796d0e5a8f2SBharata B Rao             drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB, i);
79703d196b7SBharata B Rao             g_assert(drc);
79803d196b7SBharata B Rao             drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
79903d196b7SBharata B Rao 
80003d196b7SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
80103d196b7SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
80203d196b7SBharata B Rao             dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
80303d196b7SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
80403d196b7SBharata B Rao             dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
805d0e5a8f2SBharata B Rao             if (memory_region_present(get_system_memory(), addr)) {
80603d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
80703d196b7SBharata B Rao             } else {
80803d196b7SBharata B Rao                 dynamic_memory[5] = cpu_to_be32(0);
80903d196b7SBharata B Rao             }
810d0e5a8f2SBharata B Rao         } else {
811d0e5a8f2SBharata B Rao             /*
812d0e5a8f2SBharata B Rao              * LMB information for RMA, boot time RAM and gap b/n RAM and
813d0e5a8f2SBharata B Rao              * hotplug memory region -- all these are marked as reserved
814d0e5a8f2SBharata B Rao              * and as having no valid DRC.
815d0e5a8f2SBharata B Rao              */
816d0e5a8f2SBharata B Rao             dynamic_memory[0] = cpu_to_be32(addr >> 32);
817d0e5a8f2SBharata B Rao             dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
818d0e5a8f2SBharata B Rao             dynamic_memory[2] = cpu_to_be32(0);
819d0e5a8f2SBharata B Rao             dynamic_memory[3] = cpu_to_be32(0); /* reserved */
820d0e5a8f2SBharata B Rao             dynamic_memory[4] = cpu_to_be32(-1);
821d0e5a8f2SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
822d0e5a8f2SBharata B Rao                                             SPAPR_LMB_FLAGS_DRC_INVALID);
823d0e5a8f2SBharata B Rao         }
82403d196b7SBharata B Rao 
82503d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
82603d196b7SBharata B Rao     }
82703d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
82803d196b7SBharata B Rao     if (ret < 0) {
82903d196b7SBharata B Rao         goto out;
83003d196b7SBharata B Rao     }
83103d196b7SBharata B Rao 
83203d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
83303d196b7SBharata B Rao     cur_index = int_buf;
8346663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
83503d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
83603d196b7SBharata B Rao     cur_index += 2;
8376663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
83803d196b7SBharata B Rao         uint32_t associativity[] = {
83903d196b7SBharata B Rao             cpu_to_be32(0x0),
84003d196b7SBharata B Rao             cpu_to_be32(0x0),
84103d196b7SBharata B Rao             cpu_to_be32(0x0),
84203d196b7SBharata B Rao             cpu_to_be32(i)
84303d196b7SBharata B Rao         };
84403d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
84503d196b7SBharata B Rao         cur_index += 4;
84603d196b7SBharata B Rao     }
84703d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
84803d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
84903d196b7SBharata B Rao out:
85003d196b7SBharata B Rao     g_free(int_buf);
85103d196b7SBharata B Rao     return ret;
85203d196b7SBharata B Rao }
85303d196b7SBharata B Rao 
85403d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
85503d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
85603d196b7SBharata B Rao                                  bool cpu_update, bool memory_update)
85703d196b7SBharata B Rao {
85803d196b7SBharata B Rao     void *fdt, *fdt_skel;
85903d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
86003d196b7SBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
86103d196b7SBharata B Rao 
86203d196b7SBharata B Rao     size -= sizeof(hdr);
86303d196b7SBharata B Rao 
86403d196b7SBharata B Rao     /* Create sceleton */
86503d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
86603d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
86703d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
86803d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
86903d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
87003d196b7SBharata B Rao     fdt = g_malloc0(size);
87103d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
87203d196b7SBharata B Rao     g_free(fdt_skel);
87303d196b7SBharata B Rao 
87403d196b7SBharata B Rao     /* Fixup cpu nodes */
87503d196b7SBharata B Rao     if (cpu_update) {
87603d196b7SBharata B Rao         _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
87703d196b7SBharata B Rao     }
87803d196b7SBharata B Rao 
87916c25aefSBharata B Rao     /* Generate ibm,dynamic-reconfiguration-memory node if required */
88003d196b7SBharata B Rao     if (memory_update && smc->dr_lmb_enabled) {
88103d196b7SBharata B Rao         _FDT((spapr_populate_drconf_memory(spapr, fdt)));
88203d196b7SBharata B Rao     }
88303d196b7SBharata B Rao 
88403d196b7SBharata B Rao     /* Pack resulting tree */
88503d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
88603d196b7SBharata B Rao 
88703d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
88803d196b7SBharata B Rao         trace_spapr_cas_failed(size);
88903d196b7SBharata B Rao         return -1;
89003d196b7SBharata B Rao     }
89103d196b7SBharata B Rao 
89203d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
89303d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
89403d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
89503d196b7SBharata B Rao     g_free(fdt);
89603d196b7SBharata B Rao 
89703d196b7SBharata B Rao     return 0;
89803d196b7SBharata B Rao }
89903d196b7SBharata B Rao 
90028e02042SDavid Gibson static void spapr_finalize_fdt(sPAPRMachineState *spapr,
90153018216SPaolo Bonzini                                hwaddr fdt_addr,
90253018216SPaolo Bonzini                                hwaddr rtas_addr,
90353018216SPaolo Bonzini                                hwaddr rtas_size)
90453018216SPaolo Bonzini {
9055b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
9063c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
907c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
9085b2128d2SAlexander Graf     const char *boot_device = machine->boot_order;
90971461b0fSAlexey Kardashevskiy     int ret, i;
91071461b0fSAlexey Kardashevskiy     size_t cb = 0;
91171461b0fSAlexey Kardashevskiy     char *bootlist;
91253018216SPaolo Bonzini     void *fdt;
91353018216SPaolo Bonzini     sPAPRPHBState *phb;
91453018216SPaolo Bonzini 
91553018216SPaolo Bonzini     fdt = g_malloc(FDT_MAX_SIZE);
91653018216SPaolo Bonzini 
91753018216SPaolo Bonzini     /* open out the base tree into a temp buffer for the final tweaks */
91853018216SPaolo Bonzini     _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
91953018216SPaolo Bonzini 
920e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
921e8f986fcSBharata B Rao     if (ret < 0) {
922ce9863b7SCédric Le Goater         error_report("couldn't setup memory nodes in fdt");
923e8f986fcSBharata B Rao         exit(1);
92453018216SPaolo Bonzini     }
92553018216SPaolo Bonzini 
92653018216SPaolo Bonzini     ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
92753018216SPaolo Bonzini     if (ret < 0) {
928ce9863b7SCédric Le Goater         error_report("couldn't setup vio devices in fdt");
92953018216SPaolo Bonzini         exit(1);
93053018216SPaolo Bonzini     }
93153018216SPaolo Bonzini 
9324d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
9334d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
9344d9392beSThomas Huth         if (ret < 0) {
935ce9863b7SCédric Le Goater             error_report("could not set up rng device in the fdt");
9364d9392beSThomas Huth             exit(1);
9374d9392beSThomas Huth         }
9384d9392beSThomas Huth     }
9394d9392beSThomas Huth 
94053018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
94153018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
94253018216SPaolo Bonzini         if (ret < 0) {
943da34fed7SThomas Huth             error_report("couldn't setup PCI devices in fdt");
94453018216SPaolo Bonzini             exit(1);
94553018216SPaolo Bonzini         }
946da34fed7SThomas Huth     }
94753018216SPaolo Bonzini 
94853018216SPaolo Bonzini     /* RTAS */
94953018216SPaolo Bonzini     ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
95053018216SPaolo Bonzini     if (ret < 0) {
951ce9863b7SCédric Le Goater         error_report("Couldn't set up RTAS device tree properties");
95253018216SPaolo Bonzini     }
95353018216SPaolo Bonzini 
9540da6f3feSBharata B Rao     /* cpus */
9550da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
95653018216SPaolo Bonzini 
95771461b0fSAlexey Kardashevskiy     bootlist = get_boot_devices_list(&cb, true);
95871461b0fSAlexey Kardashevskiy     if (cb && bootlist) {
95971461b0fSAlexey Kardashevskiy         int offset = fdt_path_offset(fdt, "/chosen");
96071461b0fSAlexey Kardashevskiy         if (offset < 0) {
96171461b0fSAlexey Kardashevskiy             exit(1);
96271461b0fSAlexey Kardashevskiy         }
96371461b0fSAlexey Kardashevskiy         for (i = 0; i < cb; i++) {
96471461b0fSAlexey Kardashevskiy             if (bootlist[i] == '\n') {
96571461b0fSAlexey Kardashevskiy                 bootlist[i] = ' ';
96671461b0fSAlexey Kardashevskiy             }
96771461b0fSAlexey Kardashevskiy 
96871461b0fSAlexey Kardashevskiy         }
96971461b0fSAlexey Kardashevskiy         ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist);
97071461b0fSAlexey Kardashevskiy     }
97171461b0fSAlexey Kardashevskiy 
9725b2128d2SAlexander Graf     if (boot_device && strlen(boot_device)) {
9735b2128d2SAlexander Graf         int offset = fdt_path_offset(fdt, "/chosen");
9745b2128d2SAlexander Graf 
9755b2128d2SAlexander Graf         if (offset < 0) {
9765b2128d2SAlexander Graf             exit(1);
9775b2128d2SAlexander Graf         }
9785b2128d2SAlexander Graf         fdt_setprop_string(fdt, offset, "qemu,boot-device", boot_device);
9795b2128d2SAlexander Graf     }
9805b2128d2SAlexander Graf 
98153018216SPaolo Bonzini     if (!spapr->has_graphics) {
98253018216SPaolo Bonzini         spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
98353018216SPaolo Bonzini     }
98453018216SPaolo Bonzini 
985c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
986c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
987c20d332aSBharata B Rao     }
988c20d332aSBharata B Rao 
9893c0c47e3SDavid Gibson     if (mc->query_hotpluggable_cpus) {
990af81cf32SBharata B Rao         int offset = fdt_path_offset(fdt, "/cpus");
991af81cf32SBharata B Rao         ret = spapr_drc_populate_dt(fdt, offset, NULL,
992af81cf32SBharata B Rao                                     SPAPR_DR_CONNECTOR_TYPE_CPU);
993af81cf32SBharata B Rao         if (ret < 0) {
994af81cf32SBharata B Rao             error_report("Couldn't set up CPU DR device tree properties");
995af81cf32SBharata B Rao             exit(1);
996af81cf32SBharata B Rao         }
997af81cf32SBharata B Rao     }
998af81cf32SBharata B Rao 
99953018216SPaolo Bonzini     _FDT((fdt_pack(fdt)));
100053018216SPaolo Bonzini 
100153018216SPaolo Bonzini     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
1002730fce59SThomas Huth         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
100353018216SPaolo Bonzini                      fdt_totalsize(fdt), FDT_MAX_SIZE);
100453018216SPaolo Bonzini         exit(1);
100553018216SPaolo Bonzini     }
100653018216SPaolo Bonzini 
1007ad440b4aSAndrew Jones     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
100853018216SPaolo Bonzini     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
100953018216SPaolo Bonzini 
1010a21a7a70SGonglei     g_free(bootlist);
101153018216SPaolo Bonzini     g_free(fdt);
101253018216SPaolo Bonzini }
101353018216SPaolo Bonzini 
101453018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
101553018216SPaolo Bonzini {
101653018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
101753018216SPaolo Bonzini }
101853018216SPaolo Bonzini 
101953018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu)
102053018216SPaolo Bonzini {
102153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
102253018216SPaolo Bonzini 
102353018216SPaolo Bonzini     if (msr_pr) {
102453018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
102553018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
102653018216SPaolo Bonzini     } else {
102753018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
102853018216SPaolo Bonzini     }
102953018216SPaolo Bonzini }
103053018216SPaolo Bonzini 
1031e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1032e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1033e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1034e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1035e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1036e6b8fd24SSamuel Mendoza-Jonas 
1037715c5407SDavid Gibson /*
1038715c5407SDavid Gibson  * Get the fd to access the kernel htab, re-opening it if necessary
1039715c5407SDavid Gibson  */
1040715c5407SDavid Gibson static int get_htab_fd(sPAPRMachineState *spapr)
1041715c5407SDavid Gibson {
1042715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1043715c5407SDavid Gibson         return spapr->htab_fd;
1044715c5407SDavid Gibson     }
1045715c5407SDavid Gibson 
1046715c5407SDavid Gibson     spapr->htab_fd = kvmppc_get_htab_fd(false);
1047715c5407SDavid Gibson     if (spapr->htab_fd < 0) {
1048715c5407SDavid Gibson         error_report("Unable to open fd for reading hash table from KVM: %s",
1049715c5407SDavid Gibson                      strerror(errno));
1050715c5407SDavid Gibson     }
1051715c5407SDavid Gibson 
1052715c5407SDavid Gibson     return spapr->htab_fd;
1053715c5407SDavid Gibson }
1054715c5407SDavid Gibson 
1055715c5407SDavid Gibson static void close_htab_fd(sPAPRMachineState *spapr)
1056715c5407SDavid Gibson {
1057715c5407SDavid Gibson     if (spapr->htab_fd >= 0) {
1058715c5407SDavid Gibson         close(spapr->htab_fd);
1059715c5407SDavid Gibson     }
1060715c5407SDavid Gibson     spapr->htab_fd = -1;
1061715c5407SDavid Gibson }
1062715c5407SDavid Gibson 
10638dfe8e7fSDavid Gibson static int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
10648dfe8e7fSDavid Gibson {
10658dfe8e7fSDavid Gibson     int shift;
10668dfe8e7fSDavid Gibson 
10678dfe8e7fSDavid Gibson     /* We aim for a hash table of size 1/128 the size of RAM (rounded
10688dfe8e7fSDavid Gibson      * up).  The PAPR recommendation is actually 1/64 of RAM size, but
10698dfe8e7fSDavid Gibson      * that's much more than is needed for Linux guests */
10708dfe8e7fSDavid Gibson     shift = ctz64(pow2ceil(ramsize)) - 7;
10718dfe8e7fSDavid Gibson     shift = MAX(shift, 18); /* Minimum architected size */
10728dfe8e7fSDavid Gibson     shift = MIN(shift, 46); /* Maximum architected size */
10738dfe8e7fSDavid Gibson     return shift;
10748dfe8e7fSDavid Gibson }
10758dfe8e7fSDavid Gibson 
1076c5f54f3eSDavid Gibson static void spapr_reallocate_hpt(sPAPRMachineState *spapr, int shift,
1077c5f54f3eSDavid Gibson                                  Error **errp)
107853018216SPaolo Bonzini {
1079c5f54f3eSDavid Gibson     long rc;
108053018216SPaolo Bonzini 
1081c5f54f3eSDavid Gibson     /* Clean up any HPT info from a previous boot */
1082c5f54f3eSDavid Gibson     g_free(spapr->htab);
1083c5f54f3eSDavid Gibson     spapr->htab = NULL;
1084c5f54f3eSDavid Gibson     spapr->htab_shift = 0;
1085c5f54f3eSDavid Gibson     close_htab_fd(spapr);
108653018216SPaolo Bonzini 
1087c5f54f3eSDavid Gibson     rc = kvmppc_reset_htab(shift);
1088c5f54f3eSDavid Gibson     if (rc < 0) {
1089c5f54f3eSDavid Gibson         /* kernel-side HPT needed, but couldn't allocate one */
1090c5f54f3eSDavid Gibson         error_setg_errno(errp, errno,
1091c5f54f3eSDavid Gibson                          "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1092c5f54f3eSDavid Gibson                          shift);
1093c5f54f3eSDavid Gibson         /* This is almost certainly fatal, but if the caller really
1094c5f54f3eSDavid Gibson          * wants to carry on with shift == 0, it's welcome to try */
1095c5f54f3eSDavid Gibson     } else if (rc > 0) {
1096c5f54f3eSDavid Gibson         /* kernel-side HPT allocated */
1097c5f54f3eSDavid Gibson         if (rc != shift) {
1098c5f54f3eSDavid Gibson             error_setg(errp,
1099c5f54f3eSDavid Gibson                        "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1100c5f54f3eSDavid Gibson                        shift, rc);
11017735fedaSBharata B Rao         }
11027735fedaSBharata B Rao 
110353018216SPaolo Bonzini         spapr->htab_shift = shift;
1104c18ad9a5SDavid Gibson         spapr->htab = NULL;
1105b817772aSBharata B Rao     } else {
1106c5f54f3eSDavid Gibson         /* kernel-side HPT not needed, allocate in userspace instead */
1107c5f54f3eSDavid Gibson         size_t size = 1ULL << shift;
1108c5f54f3eSDavid Gibson         int i;
110901a57972SSamuel Mendoza-Jonas 
1110c5f54f3eSDavid Gibson         spapr->htab = qemu_memalign(size, size);
1111c5f54f3eSDavid Gibson         if (!spapr->htab) {
1112c5f54f3eSDavid Gibson             error_setg_errno(errp, errno,
1113c5f54f3eSDavid Gibson                              "Could not allocate HPT of order %d", shift);
1114c5f54f3eSDavid Gibson             return;
1115b817772aSBharata B Rao         }
1116b817772aSBharata B Rao 
1117c5f54f3eSDavid Gibson         memset(spapr->htab, 0, size);
1118c5f54f3eSDavid Gibson         spapr->htab_shift = shift;
1119b817772aSBharata B Rao 
1120c5f54f3eSDavid Gibson         for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1121c5f54f3eSDavid Gibson             DIRTY_HPTE(HPTE(spapr->htab, i));
11227735fedaSBharata B Rao         }
112353018216SPaolo Bonzini     }
112453018216SPaolo Bonzini }
112553018216SPaolo Bonzini 
11264f01a637SDavid Gibson static void find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
11279e3f9733SAlexander Graf {
11289e3f9733SAlexander Graf     bool matched = false;
11299e3f9733SAlexander Graf 
11309e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
11319e3f9733SAlexander Graf         matched = true;
11329e3f9733SAlexander Graf     }
11339e3f9733SAlexander Graf 
11349e3f9733SAlexander Graf     if (!matched) {
11359e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
11369e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
11379e3f9733SAlexander Graf         exit(1);
11389e3f9733SAlexander Graf     }
11399e3f9733SAlexander Graf }
11409e3f9733SAlexander Graf 
114153018216SPaolo Bonzini static void ppc_spapr_reset(void)
114253018216SPaolo Bonzini {
1143c5f54f3eSDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
1144c5f54f3eSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1145182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1146b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1147259186a7SAndreas Färber 
11489e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
11499e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
11509e3f9733SAlexander Graf 
1151c5f54f3eSDavid Gibson     /* Allocate and/or reset the hash page table */
1152c5f54f3eSDavid Gibson     spapr_reallocate_hpt(spapr,
1153c5f54f3eSDavid Gibson                          spapr_hpt_shift_for_ramsize(machine->maxram_size),
1154c5f54f3eSDavid Gibson                          &error_fatal);
1155c5f54f3eSDavid Gibson 
1156c5f54f3eSDavid Gibson     /* Update the RMA size if necessary */
1157c5f54f3eSDavid Gibson     if (spapr->vrma_adjust) {
1158c5f54f3eSDavid Gibson         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1159c5f54f3eSDavid Gibson                                           spapr->htab_shift);
1160c5f54f3eSDavid Gibson     }
116153018216SPaolo Bonzini 
116253018216SPaolo Bonzini     qemu_devices_reset();
116353018216SPaolo Bonzini 
1164b7d1f77aSBenjamin Herrenschmidt     /*
1165b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1166b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1167b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1168b7d1f77aSBenjamin Herrenschmidt      */
1169b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1170b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1171b7d1f77aSBenjamin Herrenschmidt     spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
1172b7d1f77aSBenjamin Herrenschmidt 
117353018216SPaolo Bonzini     /* Load the fdt */
117453018216SPaolo Bonzini     spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
117553018216SPaolo Bonzini                        spapr->rtas_size);
117653018216SPaolo Bonzini 
1177b7d1f77aSBenjamin Herrenschmidt     /* Copy RTAS over */
1178b7d1f77aSBenjamin Herrenschmidt     cpu_physical_memory_write(spapr->rtas_addr, spapr->rtas_blob,
1179b7d1f77aSBenjamin Herrenschmidt                               spapr->rtas_size);
1180b7d1f77aSBenjamin Herrenschmidt 
118153018216SPaolo Bonzini     /* Set up the entry state */
1182182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1183182735efSAndreas Färber     first_ppc_cpu->env.gpr[3] = spapr->fdt_addr;
1184182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1185182735efSAndreas Färber     first_cpu->halted = 0;
11861b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
118753018216SPaolo Bonzini 
118853018216SPaolo Bonzini }
118953018216SPaolo Bonzini 
119028e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
119153018216SPaolo Bonzini {
11922ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
11933978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
119453018216SPaolo Bonzini 
11953978b863SPaolo Bonzini     if (dinfo) {
11966231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
11976231a6daSMarkus Armbruster                             &error_fatal);
119853018216SPaolo Bonzini     }
119953018216SPaolo Bonzini 
120053018216SPaolo Bonzini     qdev_init_nofail(dev);
120153018216SPaolo Bonzini 
120253018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
120353018216SPaolo Bonzini }
120453018216SPaolo Bonzini 
120528e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
120628df36a1SDavid Gibson {
120728df36a1SDavid Gibson     DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
120828df36a1SDavid Gibson 
120928df36a1SDavid Gibson     qdev_init_nofail(dev);
121028df36a1SDavid Gibson     spapr->rtc = dev;
121174e5ae28SDavid Gibson 
121274e5ae28SDavid Gibson     object_property_add_alias(qdev_get_machine(), "rtc-time",
121374e5ae28SDavid Gibson                               OBJECT(spapr->rtc), "date", NULL);
121428df36a1SDavid Gibson }
121528df36a1SDavid Gibson 
121653018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
121714c6a894SDavid Gibson static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
121853018216SPaolo Bonzini {
121953018216SPaolo Bonzini     switch (vga_interface_type) {
122053018216SPaolo Bonzini     case VGA_NONE:
12217effdaa3SMark Wu         return false;
12227effdaa3SMark Wu     case VGA_DEVICE:
12237effdaa3SMark Wu         return true;
122453018216SPaolo Bonzini     case VGA_STD:
1225b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
122653018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
122753018216SPaolo Bonzini     default:
122814c6a894SDavid Gibson         error_setg(errp,
122914c6a894SDavid Gibson                    "Unsupported VGA mode, only -vga std or -vga virtio is supported");
123014c6a894SDavid Gibson         return false;
123153018216SPaolo Bonzini     }
123253018216SPaolo Bonzini }
123353018216SPaolo Bonzini 
1234880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1235880ae7deSDavid Gibson {
123628e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1237880ae7deSDavid Gibson     int err = 0;
1238880ae7deSDavid Gibson 
1239631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1240880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1241880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1242880ae7deSDavid Gibson      * value into the RTC device */
1243880ae7deSDavid Gibson     if (version_id < 3) {
1244880ae7deSDavid Gibson         err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1245880ae7deSDavid Gibson     }
1246880ae7deSDavid Gibson 
1247880ae7deSDavid Gibson     return err;
1248880ae7deSDavid Gibson }
1249880ae7deSDavid Gibson 
1250880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1251880ae7deSDavid Gibson {
1252880ae7deSDavid Gibson     return version_id < 3;
1253880ae7deSDavid Gibson }
1254880ae7deSDavid Gibson 
12554be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
12564be21d56SDavid Gibson     .name = "spapr",
1257880ae7deSDavid Gibson     .version_id = 3,
12584be21d56SDavid Gibson     .minimum_version_id = 1,
1259880ae7deSDavid Gibson     .post_load = spapr_post_load,
12604be21d56SDavid Gibson     .fields = (VMStateField[]) {
1261880ae7deSDavid Gibson         /* used to be @next_irq */
1262880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
12634be21d56SDavid Gibson 
12644be21d56SDavid Gibson         /* RTC offset */
126528e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1266880ae7deSDavid Gibson 
126728e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
12684be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
12694be21d56SDavid Gibson     },
12704be21d56SDavid Gibson };
12714be21d56SDavid Gibson 
12724be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
12734be21d56SDavid Gibson {
127428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
12754be21d56SDavid Gibson 
12764be21d56SDavid Gibson     /* "Iteration" header */
12774be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
12784be21d56SDavid Gibson 
1279e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1280e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1281e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1282e68cb8b4SAlexey Kardashevskiy     } else {
1283e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
12844be21d56SDavid Gibson     }
12854be21d56SDavid Gibson 
1286e68cb8b4SAlexey Kardashevskiy 
1287e68cb8b4SAlexey Kardashevskiy     return 0;
1288e68cb8b4SAlexey Kardashevskiy }
12894be21d56SDavid Gibson 
129028e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
12914be21d56SDavid Gibson                                  int64_t max_ns)
12924be21d56SDavid Gibson {
1293378bc217SDavid Gibson     bool has_timeout = max_ns != -1;
12944be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
12954be21d56SDavid Gibson     int index = spapr->htab_save_index;
1296bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
12974be21d56SDavid Gibson 
12984be21d56SDavid Gibson     assert(spapr->htab_first_pass);
12994be21d56SDavid Gibson 
13004be21d56SDavid Gibson     do {
13014be21d56SDavid Gibson         int chunkstart;
13024be21d56SDavid Gibson 
13034be21d56SDavid Gibson         /* Consume invalid HPTEs */
13044be21d56SDavid Gibson         while ((index < htabslots)
13054be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
13064be21d56SDavid Gibson             index++;
13074be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13084be21d56SDavid Gibson         }
13094be21d56SDavid Gibson 
13104be21d56SDavid Gibson         /* Consume valid HPTEs */
13114be21d56SDavid Gibson         chunkstart = index;
1312338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
13134be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
13144be21d56SDavid Gibson             index++;
13154be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13164be21d56SDavid Gibson         }
13174be21d56SDavid Gibson 
13184be21d56SDavid Gibson         if (index > chunkstart) {
13194be21d56SDavid Gibson             int n_valid = index - chunkstart;
13204be21d56SDavid Gibson 
13214be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
13224be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
13234be21d56SDavid Gibson             qemu_put_be16(f, 0);
13244be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
13254be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
13264be21d56SDavid Gibson 
1327378bc217SDavid Gibson             if (has_timeout &&
1328378bc217SDavid Gibson                 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
13294be21d56SDavid Gibson                 break;
13304be21d56SDavid Gibson             }
13314be21d56SDavid Gibson         }
13324be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
13334be21d56SDavid Gibson 
13344be21d56SDavid Gibson     if (index >= htabslots) {
13354be21d56SDavid Gibson         assert(index == htabslots);
13364be21d56SDavid Gibson         index = 0;
13374be21d56SDavid Gibson         spapr->htab_first_pass = false;
13384be21d56SDavid Gibson     }
13394be21d56SDavid Gibson     spapr->htab_save_index = index;
13404be21d56SDavid Gibson }
13414be21d56SDavid Gibson 
134228e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
13434be21d56SDavid Gibson                                 int64_t max_ns)
13444be21d56SDavid Gibson {
13454be21d56SDavid Gibson     bool final = max_ns < 0;
13464be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
13474be21d56SDavid Gibson     int examined = 0, sent = 0;
13484be21d56SDavid Gibson     int index = spapr->htab_save_index;
1349bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
13504be21d56SDavid Gibson 
13514be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
13524be21d56SDavid Gibson 
13534be21d56SDavid Gibson     do {
13544be21d56SDavid Gibson         int chunkstart, invalidstart;
13554be21d56SDavid Gibson 
13564be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
13574be21d56SDavid Gibson         while ((index < htabslots)
13584be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
13594be21d56SDavid Gibson             index++;
13604be21d56SDavid Gibson             examined++;
13614be21d56SDavid Gibson         }
13624be21d56SDavid Gibson 
13634be21d56SDavid Gibson         chunkstart = index;
13644be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1365338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
13664be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
13674be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
13684be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13694be21d56SDavid Gibson             index++;
13704be21d56SDavid Gibson             examined++;
13714be21d56SDavid Gibson         }
13724be21d56SDavid Gibson 
13734be21d56SDavid Gibson         invalidstart = index;
13744be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1375338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
13764be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
13774be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
13784be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13794be21d56SDavid Gibson             index++;
13804be21d56SDavid Gibson             examined++;
13814be21d56SDavid Gibson         }
13824be21d56SDavid Gibson 
13834be21d56SDavid Gibson         if (index > chunkstart) {
13844be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
13854be21d56SDavid Gibson             int n_invalid = index - invalidstart;
13864be21d56SDavid Gibson 
13874be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
13884be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
13894be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
13904be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
13914be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
13924be21d56SDavid Gibson             sent += index - chunkstart;
13934be21d56SDavid Gibson 
1394bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
13954be21d56SDavid Gibson                 break;
13964be21d56SDavid Gibson             }
13974be21d56SDavid Gibson         }
13984be21d56SDavid Gibson 
13994be21d56SDavid Gibson         if (examined >= htabslots) {
14004be21d56SDavid Gibson             break;
14014be21d56SDavid Gibson         }
14024be21d56SDavid Gibson 
14034be21d56SDavid Gibson         if (index >= htabslots) {
14044be21d56SDavid Gibson             assert(index == htabslots);
14054be21d56SDavid Gibson             index = 0;
14064be21d56SDavid Gibson         }
14074be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
14084be21d56SDavid Gibson 
14094be21d56SDavid Gibson     if (index >= htabslots) {
14104be21d56SDavid Gibson         assert(index == htabslots);
14114be21d56SDavid Gibson         index = 0;
14124be21d56SDavid Gibson     }
14134be21d56SDavid Gibson 
14144be21d56SDavid Gibson     spapr->htab_save_index = index;
14154be21d56SDavid Gibson 
1416e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
14174be21d56SDavid Gibson }
14184be21d56SDavid Gibson 
1419e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1420e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1421e68cb8b4SAlexey Kardashevskiy 
14224be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
14234be21d56SDavid Gibson {
142428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1425715c5407SDavid Gibson     int fd;
1426e68cb8b4SAlexey Kardashevskiy     int rc = 0;
14274be21d56SDavid Gibson 
14284be21d56SDavid Gibson     /* Iteration header */
14294be21d56SDavid Gibson     qemu_put_be32(f, 0);
14304be21d56SDavid Gibson 
1431e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1432e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1433e68cb8b4SAlexey Kardashevskiy 
1434715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1435715c5407SDavid Gibson         if (fd < 0) {
1436715c5407SDavid Gibson             return fd;
143701a57972SSamuel Mendoza-Jonas         }
143801a57972SSamuel Mendoza-Jonas 
1439715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1440e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1441e68cb8b4SAlexey Kardashevskiy             return rc;
1442e68cb8b4SAlexey Kardashevskiy         }
1443e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
14444be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
14454be21d56SDavid Gibson     } else {
1446e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
14474be21d56SDavid Gibson     }
14484be21d56SDavid Gibson 
14494be21d56SDavid Gibson     /* End marker */
14504be21d56SDavid Gibson     qemu_put_be32(f, 0);
14514be21d56SDavid Gibson     qemu_put_be16(f, 0);
14524be21d56SDavid Gibson     qemu_put_be16(f, 0);
14534be21d56SDavid Gibson 
1454e68cb8b4SAlexey Kardashevskiy     return rc;
14554be21d56SDavid Gibson }
14564be21d56SDavid Gibson 
14574be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
14584be21d56SDavid Gibson {
145928e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1460715c5407SDavid Gibson     int fd;
14614be21d56SDavid Gibson 
14624be21d56SDavid Gibson     /* Iteration header */
14634be21d56SDavid Gibson     qemu_put_be32(f, 0);
14644be21d56SDavid Gibson 
1465e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1466e68cb8b4SAlexey Kardashevskiy         int rc;
1467e68cb8b4SAlexey Kardashevskiy 
1468e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1469e68cb8b4SAlexey Kardashevskiy 
1470715c5407SDavid Gibson         fd = get_htab_fd(spapr);
1471715c5407SDavid Gibson         if (fd < 0) {
1472715c5407SDavid Gibson             return fd;
147301a57972SSamuel Mendoza-Jonas         }
147401a57972SSamuel Mendoza-Jonas 
1475715c5407SDavid Gibson         rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
1476e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1477e68cb8b4SAlexey Kardashevskiy             return rc;
1478e68cb8b4SAlexey Kardashevskiy         }
1479e68cb8b4SAlexey Kardashevskiy     } else {
1480378bc217SDavid Gibson         if (spapr->htab_first_pass) {
1481378bc217SDavid Gibson             htab_save_first_pass(f, spapr, -1);
1482378bc217SDavid Gibson         }
14834be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1484e68cb8b4SAlexey Kardashevskiy     }
14854be21d56SDavid Gibson 
14864be21d56SDavid Gibson     /* End marker */
14874be21d56SDavid Gibson     qemu_put_be32(f, 0);
14884be21d56SDavid Gibson     qemu_put_be16(f, 0);
14894be21d56SDavid Gibson     qemu_put_be16(f, 0);
14904be21d56SDavid Gibson 
14914be21d56SDavid Gibson     return 0;
14924be21d56SDavid Gibson }
14934be21d56SDavid Gibson 
14944be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
14954be21d56SDavid Gibson {
149628e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
14974be21d56SDavid Gibson     uint32_t section_hdr;
1498e68cb8b4SAlexey Kardashevskiy     int fd = -1;
14994be21d56SDavid Gibson 
15004be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
150198a5d100SDavid Gibson         error_report("htab_load() bad version");
15024be21d56SDavid Gibson         return -EINVAL;
15034be21d56SDavid Gibson     }
15044be21d56SDavid Gibson 
15054be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
15064be21d56SDavid Gibson 
15074be21d56SDavid Gibson     if (section_hdr) {
15089897e462SGreg Kurz         Error *local_err = NULL;
1509c5f54f3eSDavid Gibson 
1510c5f54f3eSDavid Gibson         /* First section gives the htab size */
1511c5f54f3eSDavid Gibson         spapr_reallocate_hpt(spapr, section_hdr, &local_err);
1512c5f54f3eSDavid Gibson         if (local_err) {
1513c5f54f3eSDavid Gibson             error_report_err(local_err);
15144be21d56SDavid Gibson             return -EINVAL;
15154be21d56SDavid Gibson         }
15164be21d56SDavid Gibson         return 0;
15174be21d56SDavid Gibson     }
15184be21d56SDavid Gibson 
1519e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1520e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1521e68cb8b4SAlexey Kardashevskiy 
1522e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1523e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
152498a5d100SDavid Gibson             error_report("Unable to open fd to restore KVM hash table: %s",
1525e68cb8b4SAlexey Kardashevskiy                          strerror(errno));
1526e68cb8b4SAlexey Kardashevskiy         }
1527e68cb8b4SAlexey Kardashevskiy     }
1528e68cb8b4SAlexey Kardashevskiy 
15294be21d56SDavid Gibson     while (true) {
15304be21d56SDavid Gibson         uint32_t index;
15314be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
15324be21d56SDavid Gibson 
15334be21d56SDavid Gibson         index = qemu_get_be32(f);
15344be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
15354be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
15364be21d56SDavid Gibson 
15374be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
15384be21d56SDavid Gibson             /* End of Stream */
15394be21d56SDavid Gibson             break;
15404be21d56SDavid Gibson         }
15414be21d56SDavid Gibson 
1542e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
15434be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
15444be21d56SDavid Gibson             /* Bad index in stream */
154598a5d100SDavid Gibson             error_report(
154698a5d100SDavid Gibson                 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
154798a5d100SDavid Gibson                 index, n_valid, n_invalid, spapr->htab_shift);
15484be21d56SDavid Gibson             return -EINVAL;
15494be21d56SDavid Gibson         }
15504be21d56SDavid Gibson 
1551e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
15524be21d56SDavid Gibson             if (n_valid) {
15534be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
15544be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
15554be21d56SDavid Gibson             }
15564be21d56SDavid Gibson             if (n_invalid) {
15574be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
15584be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
15594be21d56SDavid Gibson             }
1560e68cb8b4SAlexey Kardashevskiy         } else {
1561e68cb8b4SAlexey Kardashevskiy             int rc;
1562e68cb8b4SAlexey Kardashevskiy 
1563e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1564e68cb8b4SAlexey Kardashevskiy 
1565e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1566e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1567e68cb8b4SAlexey Kardashevskiy                 return rc;
1568e68cb8b4SAlexey Kardashevskiy             }
1569e68cb8b4SAlexey Kardashevskiy         }
1570e68cb8b4SAlexey Kardashevskiy     }
1571e68cb8b4SAlexey Kardashevskiy 
1572e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1573e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1574e68cb8b4SAlexey Kardashevskiy         close(fd);
15754be21d56SDavid Gibson     }
15764be21d56SDavid Gibson 
15774be21d56SDavid Gibson     return 0;
15784be21d56SDavid Gibson }
15794be21d56SDavid Gibson 
1580c573fc03SThomas Huth static void htab_cleanup(void *opaque)
1581c573fc03SThomas Huth {
1582c573fc03SThomas Huth     sPAPRMachineState *spapr = opaque;
1583c573fc03SThomas Huth 
1584c573fc03SThomas Huth     close_htab_fd(spapr);
1585c573fc03SThomas Huth }
1586c573fc03SThomas Huth 
15874be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
15884be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
15894be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1590a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
1591c573fc03SThomas Huth     .cleanup = htab_cleanup,
15924be21d56SDavid Gibson     .load_state = htab_load,
15934be21d56SDavid Gibson };
15944be21d56SDavid Gibson 
15955b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
15965b2128d2SAlexander Graf                            Error **errp)
15975b2128d2SAlexander Graf {
15985b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
15995b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
16005b2128d2SAlexander Graf }
16015b2128d2SAlexander Graf 
1602224245bfSDavid Gibson /*
1603224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1604224245bfSDavid Gibson  *
1605224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1606224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1607224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1608224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1609224245bfSDavid Gibson  */
1610224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1611224245bfSDavid Gibson {
1612224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1613224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1614224245bfSDavid Gibson 
1615224245bfSDavid Gibson     if (d) {
1616224245bfSDavid Gibson         device_reset(d);
1617224245bfSDavid Gibson     }
1618224245bfSDavid Gibson }
1619224245bfSDavid Gibson 
1620224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1621224245bfSDavid Gibson {
1622224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1623224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1624e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1625224245bfSDavid Gibson     int i;
1626224245bfSDavid Gibson 
1627224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1628224245bfSDavid Gibson         sPAPRDRConnector *drc;
1629224245bfSDavid Gibson         uint64_t addr;
1630224245bfSDavid Gibson 
1631e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1632224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1633224245bfSDavid Gibson                                      addr/lmb_size);
1634224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1635224245bfSDavid Gibson     }
1636224245bfSDavid Gibson }
1637224245bfSDavid Gibson 
1638224245bfSDavid Gibson /*
1639224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1640224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1641224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1642224245bfSDavid Gibson  */
16437c150d6fSDavid Gibson static void spapr_validate_node_memory(MachineState *machine, Error **errp)
1644224245bfSDavid Gibson {
1645224245bfSDavid Gibson     int i;
1646224245bfSDavid Gibson 
16477c150d6fSDavid Gibson     if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
16487c150d6fSDavid Gibson         error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
16497c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
16507c150d6fSDavid Gibson                    machine->ram_size,
1651224245bfSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
16527c150d6fSDavid Gibson         return;
16537c150d6fSDavid Gibson     }
16547c150d6fSDavid Gibson 
16557c150d6fSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
16567c150d6fSDavid Gibson         error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
16577c150d6fSDavid Gibson                    " is not aligned to %llu MiB",
16587c150d6fSDavid Gibson                    machine->ram_size,
16597c150d6fSDavid Gibson                    SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
16607c150d6fSDavid Gibson         return;
1661224245bfSDavid Gibson     }
1662224245bfSDavid Gibson 
1663224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1664224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
16657c150d6fSDavid Gibson             error_setg(errp,
16667c150d6fSDavid Gibson                        "Node %d memory size 0x%" PRIx64
16677c150d6fSDavid Gibson                        " is not aligned to %llu MiB",
16687c150d6fSDavid Gibson                        i, numa_info[i].node_mem,
1669224245bfSDavid Gibson                        SPAPR_MEMORY_BLOCK_SIZE / M_BYTE);
16707c150d6fSDavid Gibson             return;
1671224245bfSDavid Gibson         }
1672224245bfSDavid Gibson     }
1673224245bfSDavid Gibson }
1674224245bfSDavid Gibson 
167553018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
16763ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
167753018216SPaolo Bonzini {
167828e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
16793c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(machine);
1680224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
16813ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
16823ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
16833ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
168453018216SPaolo Bonzini     PCIHostState *phb;
168553018216SPaolo Bonzini     int i;
168653018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
168753018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1688658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1689658fa66bSAlexey Kardashevskiy     void *rma = NULL;
169053018216SPaolo Bonzini     hwaddr rma_alloc_size;
1691b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
169253018216SPaolo Bonzini     uint32_t initrd_base = 0;
169353018216SPaolo Bonzini     long kernel_size = 0, initrd_size = 0;
1694b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
169516457e7fSBenjamin Herrenschmidt     bool kernel_le = false;
169653018216SPaolo Bonzini     char *filename;
169794a94e4cSBharata B Rao     int smt = kvmppc_smt_threads();
169894a94e4cSBharata B Rao     int spapr_cores = smp_cpus / smp_threads;
169994a94e4cSBharata B Rao     int spapr_max_cores = max_cpus / smp_threads;
170094a94e4cSBharata B Rao 
17013c0c47e3SDavid Gibson     if (mc->query_hotpluggable_cpus) {
170294a94e4cSBharata B Rao         if (smp_cpus % smp_threads) {
170394a94e4cSBharata B Rao             error_report("smp_cpus (%u) must be multiple of threads (%u)",
170494a94e4cSBharata B Rao                          smp_cpus, smp_threads);
170594a94e4cSBharata B Rao             exit(1);
170694a94e4cSBharata B Rao         }
170794a94e4cSBharata B Rao         if (max_cpus % smp_threads) {
170894a94e4cSBharata B Rao             error_report("max_cpus (%u) must be multiple of threads (%u)",
170994a94e4cSBharata B Rao                          max_cpus, smp_threads);
171094a94e4cSBharata B Rao             exit(1);
171194a94e4cSBharata B Rao         }
171294a94e4cSBharata B Rao     }
171353018216SPaolo Bonzini 
1714226419d6SMichael S. Tsirkin     msi_nonbroken = true;
171553018216SPaolo Bonzini 
171653018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
171753018216SPaolo Bonzini 
171853018216SPaolo Bonzini     cpu_ppc_hypercall = emulate_spapr_hypercall;
171953018216SPaolo Bonzini 
172053018216SPaolo Bonzini     /* Allocate RMA if necessary */
1721658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
172253018216SPaolo Bonzini 
172353018216SPaolo Bonzini     if (rma_alloc_size == -1) {
1724730fce59SThomas Huth         error_report("Unable to create RMA");
172553018216SPaolo Bonzini         exit(1);
172653018216SPaolo Bonzini     }
172753018216SPaolo Bonzini 
1728c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
172953018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
173053018216SPaolo Bonzini     } else {
1731c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
173253018216SPaolo Bonzini 
173353018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
173453018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
173553018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
173653018216SPaolo Bonzini          *
173753018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
173853018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
173953018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
174053018216SPaolo Bonzini          * isn't determined yet.
174153018216SPaolo Bonzini          */
174253018216SPaolo Bonzini         if (kvm_enabled()) {
174353018216SPaolo Bonzini             spapr->vrma_adjust = 1;
174453018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
174553018216SPaolo Bonzini         }
1746912acdf4SBenjamin Herrenschmidt 
1747912acdf4SBenjamin Herrenschmidt         /* Actually we don't support unbounded RMA anymore since we
1748912acdf4SBenjamin Herrenschmidt          * added proper emulation of HV mode. The max we can get is
1749912acdf4SBenjamin Herrenschmidt          * 16G which also happens to be what we configure for PAPR
1750912acdf4SBenjamin Herrenschmidt          * mode so make sure we don't do anything bigger than that
1751912acdf4SBenjamin Herrenschmidt          */
1752912acdf4SBenjamin Herrenschmidt         spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
175353018216SPaolo Bonzini     }
175453018216SPaolo Bonzini 
1755c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
1756d54e4d76SDavid Gibson         error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
1757c4177479SAlexey Kardashevskiy                      spapr->rma_size);
1758c4177479SAlexey Kardashevskiy         exit(1);
1759c4177479SAlexey Kardashevskiy     }
1760c4177479SAlexey Kardashevskiy 
1761b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1762b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
176353018216SPaolo Bonzini 
17647b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
176527f24582SBenjamin Herrenschmidt     spapr->xics = xics_system_init(machine,
176694a94e4cSBharata B Rao                                    DIV_ROUND_UP(max_cpus * smt, smp_threads),
1767161deaf2SBenjamin Herrenschmidt                                    XICS_IRQS_SPAPR, &error_fatal);
17687b565160SDavid Gibson 
1769224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
17707c150d6fSDavid Gibson         spapr_validate_node_memory(machine, &error_fatal);
1771224245bfSDavid Gibson     }
1772224245bfSDavid Gibson 
177353018216SPaolo Bonzini     /* init CPUs */
177419fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
177519fb2c36SBharata B Rao         machine->cpu_model = kvm_enabled() ? "host" : "POWER7";
177653018216SPaolo Bonzini     }
177794a94e4cSBharata B Rao 
1778e703d2f7SGreg Kurz     ppc_cpu_parse_features(machine->cpu_model);
1779e703d2f7SGreg Kurz 
17803c0c47e3SDavid Gibson     if (mc->query_hotpluggable_cpus) {
178194a94e4cSBharata B Rao         char *type = spapr_get_cpu_core_type(machine->cpu_model);
178294a94e4cSBharata B Rao 
17834babfaf0SThomas Huth         if (type == NULL) {
1784caebf378SCédric Le Goater             error_report("Unable to find sPAPR CPU Core definition");
1785caebf378SCédric Le Goater             exit(1);
1786caebf378SCédric Le Goater         }
1787caebf378SCédric Le Goater 
178894a94e4cSBharata B Rao         spapr->cores = g_new0(Object *, spapr_max_cores);
1789af81cf32SBharata B Rao         for (i = 0; i < spapr_max_cores; i++) {
179012bf2d33SGreg Kurz             int core_id = i * smp_threads;
1791af81cf32SBharata B Rao             sPAPRDRConnector *drc =
1792af81cf32SBharata B Rao                 spapr_dr_connector_new(OBJECT(spapr),
179312bf2d33SGreg Kurz                                        SPAPR_DR_CONNECTOR_TYPE_CPU,
179412bf2d33SGreg Kurz                                        (core_id / smp_threads) * smt);
1795af81cf32SBharata B Rao 
1796af81cf32SBharata B Rao             qemu_register_reset(spapr_drc_reset, drc);
1797af81cf32SBharata B Rao 
1798af81cf32SBharata B Rao             if (i < spapr_cores) {
1799caebf378SCédric Le Goater                 Object *core  = object_new(type);
180094a94e4cSBharata B Rao                 object_property_set_int(core, smp_threads, "nr-threads",
180194a94e4cSBharata B Rao                                         &error_fatal);
180212bf2d33SGreg Kurz                 object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
180394a94e4cSBharata B Rao                                         &error_fatal);
180494a94e4cSBharata B Rao                 object_property_set_bool(core, true, "realized", &error_fatal);
180594a94e4cSBharata B Rao             }
1806af81cf32SBharata B Rao         }
180794a94e4cSBharata B Rao         g_free(type);
180894a94e4cSBharata B Rao     } else {
180953018216SPaolo Bonzini         for (i = 0; i < smp_cpus; i++) {
181094a94e4cSBharata B Rao             PowerPCCPU *cpu = cpu_ppc_init(machine->cpu_model);
181153018216SPaolo Bonzini             if (cpu == NULL) {
1812569f4967SDavid Gibson                 error_report("Unable to find PowerPC CPU definition");
181353018216SPaolo Bonzini                 exit(1);
181453018216SPaolo Bonzini             }
1815569f4967SDavid Gibson             spapr_cpu_init(spapr, cpu, &error_fatal);
181653018216SPaolo Bonzini        }
181794a94e4cSBharata B Rao     }
181853018216SPaolo Bonzini 
1819026bfd89SDavid Gibson     if (kvm_enabled()) {
1820026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
1821026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
1822ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
18235145ad4fSNathan Whitehorn 
18245145ad4fSNathan Whitehorn         /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
18255145ad4fSNathan Whitehorn         kvmppc_enable_clear_ref_mod_hcalls();
1826026bfd89SDavid Gibson     }
1827026bfd89SDavid Gibson 
182853018216SPaolo Bonzini     /* allocate RAM */
1829f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
1830fb164994SDavid Gibson                                          machine->ram_size);
1831f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
183253018216SPaolo Bonzini 
1833658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
1834658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
1835658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
1836658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
1837658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
1838658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
1839658fa66bSAlexey Kardashevskiy     }
1840658fa66bSAlexey Kardashevskiy 
18414a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
18424a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
18434a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
184471c9a3ddSBharata B Rao         /*
184571c9a3ddSBharata B Rao          * Limit the number of hotpluggable memory slots to half the number
184671c9a3ddSBharata B Rao          * slots that KVM supports, leaving the other half for PCI and other
184771c9a3ddSBharata B Rao          * devices. However ensure that number of slots doesn't drop below 32.
184871c9a3ddSBharata B Rao          */
184971c9a3ddSBharata B Rao         int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
185071c9a3ddSBharata B Rao                            SPAPR_MAX_RAM_SLOTS;
18514a1c9cf0SBharata B Rao 
185271c9a3ddSBharata B Rao         if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
185371c9a3ddSBharata B Rao             max_memslots = SPAPR_MAX_RAM_SLOTS;
185471c9a3ddSBharata B Rao         }
185571c9a3ddSBharata B Rao         if (machine->ram_slots > max_memslots) {
1856d54e4d76SDavid Gibson             error_report("Specified number of memory slots %"
1857d54e4d76SDavid Gibson                          PRIu64" exceeds max supported %d",
185871c9a3ddSBharata B Rao                          machine->ram_slots, max_memslots);
1859d54e4d76SDavid Gibson             exit(1);
18604a1c9cf0SBharata B Rao         }
18614a1c9cf0SBharata B Rao 
18624a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
18634a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
18644a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
18654a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
18664a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
18674a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
18684a1c9cf0SBharata B Rao     }
18694a1c9cf0SBharata B Rao 
1870224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
1871224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
1872224245bfSDavid Gibson     }
1873224245bfSDavid Gibson 
187453018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
18754c56440dSStefan Weil     if (!filename) {
1876730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
18774c56440dSStefan Weil         exit(1);
18784c56440dSStefan Weil     }
1879b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
18808afc22a2SZhou Jie     if (spapr->rtas_size < 0) {
18818afc22a2SZhou Jie         error_report("Could not get size of LPAR rtas '%s'", filename);
18828afc22a2SZhou Jie         exit(1);
18838afc22a2SZhou Jie     }
1884b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
1885b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
1886730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
188753018216SPaolo Bonzini         exit(1);
188853018216SPaolo Bonzini     }
188953018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
1890730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
18912f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
189253018216SPaolo Bonzini         exit(1);
189353018216SPaolo Bonzini     }
189453018216SPaolo Bonzini     g_free(filename);
189553018216SPaolo Bonzini 
189653018216SPaolo Bonzini     /* Set up EPOW events infrastructure */
189753018216SPaolo Bonzini     spapr_events_init(spapr);
189853018216SPaolo Bonzini 
189912f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
190028df36a1SDavid Gibson     spapr_rtc_create(spapr);
190112f42174SDavid Gibson 
190253018216SPaolo Bonzini     /* Set up VIO bus */
190353018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
190453018216SPaolo Bonzini 
190553018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
190653018216SPaolo Bonzini         if (serial_hds[i]) {
190753018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
190853018216SPaolo Bonzini         }
190953018216SPaolo Bonzini     }
191053018216SPaolo Bonzini 
191153018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
191253018216SPaolo Bonzini     spapr_create_nvram(spapr);
191353018216SPaolo Bonzini 
191453018216SPaolo Bonzini     /* Set up PCI */
191553018216SPaolo Bonzini     spapr_pci_rtas_init();
191653018216SPaolo Bonzini 
191789dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
191853018216SPaolo Bonzini 
191953018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
192053018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
192153018216SPaolo Bonzini 
192253018216SPaolo Bonzini         if (!nd->model) {
192353018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
192453018216SPaolo Bonzini         }
192553018216SPaolo Bonzini 
192653018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
192753018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
192853018216SPaolo Bonzini         } else {
192929b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
193053018216SPaolo Bonzini         }
193153018216SPaolo Bonzini     }
193253018216SPaolo Bonzini 
193353018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
193453018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
193553018216SPaolo Bonzini     }
193653018216SPaolo Bonzini 
193753018216SPaolo Bonzini     /* Graphics */
193814c6a894SDavid Gibson     if (spapr_vga_init(phb->bus, &error_fatal)) {
193953018216SPaolo Bonzini         spapr->has_graphics = true;
1940c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
194153018216SPaolo Bonzini     }
194253018216SPaolo Bonzini 
19434ee9ced9SMarcel Apfelbaum     if (machine->usb) {
194457040d45SThomas Huth         if (smc->use_ohci_by_default) {
194553018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
194657040d45SThomas Huth         } else {
194757040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
194857040d45SThomas Huth         }
1949c86580b8SMarkus Armbruster 
195053018216SPaolo Bonzini         if (spapr->has_graphics) {
1951c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
1952c86580b8SMarkus Armbruster 
1953c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
1954c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
195553018216SPaolo Bonzini         }
195653018216SPaolo Bonzini     }
195753018216SPaolo Bonzini 
195853018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
1959d54e4d76SDavid Gibson         error_report(
1960d54e4d76SDavid Gibson             "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
1961d54e4d76SDavid Gibson             MIN_RMA_SLOF);
196253018216SPaolo Bonzini         exit(1);
196353018216SPaolo Bonzini     }
196453018216SPaolo Bonzini 
196553018216SPaolo Bonzini     if (kernel_filename) {
196653018216SPaolo Bonzini         uint64_t lowaddr = 0;
196753018216SPaolo Bonzini 
196853018216SPaolo Bonzini         kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
19697ef295eaSPeter Crosthwaite                                NULL, &lowaddr, NULL, 1, PPC_ELF_MACHINE,
19707ef295eaSPeter Crosthwaite                                0, 0);
19713b66da82SAlexey Kardashevskiy         if (kernel_size == ELF_LOAD_WRONG_ENDIAN) {
197216457e7fSBenjamin Herrenschmidt             kernel_size = load_elf(kernel_filename,
197316457e7fSBenjamin Herrenschmidt                                    translate_kernel_address, NULL,
19747ef295eaSPeter Crosthwaite                                    NULL, &lowaddr, NULL, 0, PPC_ELF_MACHINE,
19757ef295eaSPeter Crosthwaite                                    0, 0);
197616457e7fSBenjamin Herrenschmidt             kernel_le = kernel_size > 0;
197716457e7fSBenjamin Herrenschmidt         }
197816457e7fSBenjamin Herrenschmidt         if (kernel_size < 0) {
1979d54e4d76SDavid Gibson             error_report("error loading %s: %s",
19803b66da82SAlexey Kardashevskiy                          kernel_filename, load_elf_strerror(kernel_size));
198153018216SPaolo Bonzini             exit(1);
198253018216SPaolo Bonzini         }
198353018216SPaolo Bonzini 
198453018216SPaolo Bonzini         /* load initrd */
198553018216SPaolo Bonzini         if (initrd_filename) {
198653018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
198753018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
198853018216SPaolo Bonzini              */
198953018216SPaolo Bonzini             initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
199053018216SPaolo Bonzini             initrd_size = load_image_targphys(initrd_filename, initrd_base,
199153018216SPaolo Bonzini                                               load_limit - initrd_base);
199253018216SPaolo Bonzini             if (initrd_size < 0) {
1993d54e4d76SDavid Gibson                 error_report("could not load initial ram disk '%s'",
199453018216SPaolo Bonzini                              initrd_filename);
199553018216SPaolo Bonzini                 exit(1);
199653018216SPaolo Bonzini             }
199753018216SPaolo Bonzini         } else {
199853018216SPaolo Bonzini             initrd_base = 0;
199953018216SPaolo Bonzini             initrd_size = 0;
200053018216SPaolo Bonzini         }
200153018216SPaolo Bonzini     }
200253018216SPaolo Bonzini 
20038e7ea787SAndreas Färber     if (bios_name == NULL) {
20048e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
20058e7ea787SAndreas Färber     }
20068e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
20074c56440dSStefan Weil     if (!filename) {
200868fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
20094c56440dSStefan Weil         exit(1);
20104c56440dSStefan Weil     }
201153018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
201268fea5a0SThomas Huth     if (fw_size <= 0) {
201368fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
201453018216SPaolo Bonzini         exit(1);
201553018216SPaolo Bonzini     }
201653018216SPaolo Bonzini     g_free(filename);
201753018216SPaolo Bonzini 
201828e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
201928e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
202028e02042SDavid Gibson      * which predated MachineState but had a similar function */
20214be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
20224be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
20234be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
20244be21d56SDavid Gibson 
202553018216SPaolo Bonzini     /* Prepare the device tree */
20263bbf37f2SAndreas Färber     spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size,
202716457e7fSBenjamin Herrenschmidt                                             kernel_size, kernel_le,
202831fe14d1SNathan Fontenot                                             kernel_cmdline,
202931fe14d1SNathan Fontenot                                             spapr->check_exception_irq);
203053018216SPaolo Bonzini     assert(spapr->fdt_skel != NULL);
20315b2128d2SAlexander Graf 
203246503c2bSMichael Roth     /* used by RTAS */
203346503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
203446503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
203546503c2bSMichael Roth 
20365b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
203753018216SPaolo Bonzini }
203853018216SPaolo Bonzini 
2039135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2040135a129aSAneesh Kumar K.V {
2041135a129aSAneesh Kumar K.V     if (!vm_type) {
2042135a129aSAneesh Kumar K.V         return 0;
2043135a129aSAneesh Kumar K.V     }
2044135a129aSAneesh Kumar K.V 
2045135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2046135a129aSAneesh Kumar K.V         return 1;
2047135a129aSAneesh Kumar K.V     }
2048135a129aSAneesh Kumar K.V 
2049135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2050135a129aSAneesh Kumar K.V         return 2;
2051135a129aSAneesh Kumar K.V     }
2052135a129aSAneesh Kumar K.V 
2053135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2054135a129aSAneesh Kumar K.V     exit(1);
2055135a129aSAneesh Kumar K.V }
2056135a129aSAneesh Kumar K.V 
205771461b0fSAlexey Kardashevskiy /*
2058627b84f4SGonglei  * Implementation of an interface to adjust firmware path
205971461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
206071461b0fSAlexey Kardashevskiy  */
206171461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
206271461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
206371461b0fSAlexey Kardashevskiy {
206471461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
206571461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
206671461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
206771461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
206871461b0fSAlexey Kardashevskiy 
206971461b0fSAlexey Kardashevskiy     if (d) {
207071461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
207171461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
207271461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
207371461b0fSAlexey Kardashevskiy 
207471461b0fSAlexey Kardashevskiy         if (spapr) {
207571461b0fSAlexey Kardashevskiy             /*
207671461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
207771461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
207871461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
207971461b0fSAlexey Kardashevskiy              */
208071461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
208171461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
208271461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
208371461b0fSAlexey Kardashevskiy         } else if (virtio) {
208471461b0fSAlexey Kardashevskiy             /*
208571461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
208671461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
208771461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
208871461b0fSAlexey Kardashevskiy              * the actual binding is:
208971461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
209071461b0fSAlexey Kardashevskiy              */
209171461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
209271461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
209371461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
209471461b0fSAlexey Kardashevskiy         } else if (usb) {
209571461b0fSAlexey Kardashevskiy             /*
209671461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
209771461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
209871461b0fSAlexey Kardashevskiy              */
209971461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
210071461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
210171461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
210271461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
210371461b0fSAlexey Kardashevskiy         }
210471461b0fSAlexey Kardashevskiy     }
210571461b0fSAlexey Kardashevskiy 
210671461b0fSAlexey Kardashevskiy     if (phb) {
210771461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
210871461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
210971461b0fSAlexey Kardashevskiy     }
211071461b0fSAlexey Kardashevskiy 
211171461b0fSAlexey Kardashevskiy     return NULL;
211271461b0fSAlexey Kardashevskiy }
211371461b0fSAlexey Kardashevskiy 
211423825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
211523825581SEduardo Habkost {
211628e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
211723825581SEduardo Habkost 
211828e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
211923825581SEduardo Habkost }
212023825581SEduardo Habkost 
212123825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
212223825581SEduardo Habkost {
212328e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
212423825581SEduardo Habkost 
212528e02042SDavid Gibson     g_free(spapr->kvm_type);
212628e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
212723825581SEduardo Habkost }
212823825581SEduardo Habkost 
212923825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
213023825581SEduardo Habkost {
2131715c5407SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
2132715c5407SDavid Gibson 
2133715c5407SDavid Gibson     spapr->htab_fd = -1;
213423825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
213523825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
213649d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
213749d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
213849d2e648SMarcel Apfelbaum                                     NULL);
213923825581SEduardo Habkost }
214023825581SEduardo Habkost 
214187bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
214287bbdd9cSDavid Gibson {
214387bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
214487bbdd9cSDavid Gibson 
214587bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
214687bbdd9cSDavid Gibson }
214787bbdd9cSDavid Gibson 
2148e0eeb4a2SAlex Bennée static void ppc_cpu_do_nmi_on_cpu(CPUState *cs, void *arg)
214934316482SAlexey Kardashevskiy {
215034316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
215134316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
215234316482SAlexey Kardashevskiy }
215334316482SAlexey Kardashevskiy 
215434316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
215534316482SAlexey Kardashevskiy {
215634316482SAlexey Kardashevskiy     CPUState *cs;
215734316482SAlexey Kardashevskiy 
215834316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
2159e0eeb4a2SAlex Bennée         async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, NULL);
216034316482SAlexey Kardashevskiy     }
216134316482SAlexey Kardashevskiy }
216234316482SAlexey Kardashevskiy 
2163c20d332aSBharata B Rao static void spapr_add_lmbs(DeviceState *dev, uint64_t addr, uint64_t size,
2164c20d332aSBharata B Rao                            uint32_t node, Error **errp)
2165c20d332aSBharata B Rao {
2166c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2167c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2168c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2169c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2170c20d332aSBharata B Rao     void *fdt;
2171c20d332aSBharata B Rao 
2172c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2173c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2174c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2175c20d332aSBharata B Rao         g_assert(drc);
2176c20d332aSBharata B Rao 
2177c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2178c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2179c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2180c20d332aSBharata B Rao 
2181c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2182c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2183c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2184c20d332aSBharata B Rao     }
21855dd5238cSJianjun Duan     /* send hotplug notification to the
21865dd5238cSJianjun Duan      * guest only in case of hotplugged memory
21875dd5238cSJianjun Duan      */
21885dd5238cSJianjun Duan     if (dev->hotplugged) {
21890a417869SBharata B Rao        spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB, nr_lmbs);
2190c20d332aSBharata B Rao     }
21915dd5238cSJianjun Duan }
2192c20d332aSBharata B Rao 
2193c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2194c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2195c20d332aSBharata B Rao {
2196c20d332aSBharata B Rao     Error *local_err = NULL;
2197c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2198c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2199c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2200c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2201c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2202c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2203c20d332aSBharata B Rao     uint64_t addr;
2204c20d332aSBharata B Rao 
2205c20d332aSBharata B Rao     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2206c20d332aSBharata B Rao         error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2207c20d332aSBharata B Rao                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2208c20d332aSBharata B Rao         goto out;
2209c20d332aSBharata B Rao     }
2210c20d332aSBharata B Rao 
2211d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2212c20d332aSBharata B Rao     if (local_err) {
2213c20d332aSBharata B Rao         goto out;
2214c20d332aSBharata B Rao     }
2215c20d332aSBharata B Rao 
2216c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2217c20d332aSBharata B Rao     if (local_err) {
2218c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2219c20d332aSBharata B Rao         goto out;
2220c20d332aSBharata B Rao     }
2221c20d332aSBharata B Rao 
2222c20d332aSBharata B Rao     spapr_add_lmbs(dev, addr, size, node, &error_abort);
2223c20d332aSBharata B Rao 
2224c20d332aSBharata B Rao out:
2225c20d332aSBharata B Rao     error_propagate(errp, local_err);
2226c20d332aSBharata B Rao }
2227c20d332aSBharata B Rao 
2228af81cf32SBharata B Rao void *spapr_populate_hotplug_cpu_dt(CPUState *cs, int *fdt_offset,
2229af81cf32SBharata B Rao                                     sPAPRMachineState *spapr)
2230af81cf32SBharata B Rao {
2231af81cf32SBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
2232af81cf32SBharata B Rao     DeviceClass *dc = DEVICE_GET_CLASS(cs);
2233af81cf32SBharata B Rao     int id = ppc_get_vcpu_dt_id(cpu);
2234af81cf32SBharata B Rao     void *fdt;
2235af81cf32SBharata B Rao     int offset, fdt_size;
2236af81cf32SBharata B Rao     char *nodename;
2237af81cf32SBharata B Rao 
2238af81cf32SBharata B Rao     fdt = create_device_tree(&fdt_size);
2239af81cf32SBharata B Rao     nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
2240af81cf32SBharata B Rao     offset = fdt_add_subnode(fdt, 0, nodename);
2241af81cf32SBharata B Rao 
2242af81cf32SBharata B Rao     spapr_populate_cpu_dt(cs, fdt, offset, spapr);
2243af81cf32SBharata B Rao     g_free(nodename);
2244af81cf32SBharata B Rao 
2245af81cf32SBharata B Rao     *fdt_offset = offset;
2246af81cf32SBharata B Rao     return fdt;
2247af81cf32SBharata B Rao }
2248af81cf32SBharata B Rao 
2249c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2250c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2251c20d332aSBharata B Rao {
2252c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2253c20d332aSBharata B Rao 
2254c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2255b556854bSBharata B Rao         int node;
2256c20d332aSBharata B Rao 
2257c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2258c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2259c20d332aSBharata B Rao             return;
2260c20d332aSBharata B Rao         }
2261c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2262c20d332aSBharata B Rao         if (*errp) {
2263c20d332aSBharata B Rao             return;
2264c20d332aSBharata B Rao         }
22651a5512bbSGonglei         if (node < 0 || node >= MAX_NODES) {
22661a5512bbSGonglei             error_setg(errp, "Invaild node %d", node);
22671a5512bbSGonglei             return;
22681a5512bbSGonglei         }
2269c20d332aSBharata B Rao 
2270b556854bSBharata B Rao         /*
2271b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2272b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2273b556854bSBharata B Rao          * to the first node that has some memory. This causes two
2274b556854bSBharata B Rao          * unexpected behaviours for the user.
2275b556854bSBharata B Rao          *
2276b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
2277b556854bSBharata B Rao          *   specified.
2278b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2279b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
2280b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
2281b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
2282b556854bSBharata B Rao          *
2283b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
2284b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
2285b556854bSBharata B Rao          */
2286b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
2287b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
2288b556854bSBharata B Rao                        node);
2289b556854bSBharata B Rao             return;
2290b556854bSBharata B Rao         }
2291b556854bSBharata B Rao 
2292c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
2293af81cf32SBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2294af81cf32SBharata B Rao         spapr_core_plug(hotplug_dev, dev, errp);
2295c20d332aSBharata B Rao     }
2296c20d332aSBharata B Rao }
2297c20d332aSBharata B Rao 
2298c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2299c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2300c20d332aSBharata B Rao {
23013c0c47e3SDavid Gibson     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
23026f4b5c3eSBharata B Rao 
2303c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2304c20d332aSBharata B Rao         error_setg(errp, "Memory hot unplug not supported by sPAPR");
23056f4b5c3eSBharata B Rao     } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
23063c0c47e3SDavid Gibson         if (!mc->query_hotpluggable_cpus) {
23076f4b5c3eSBharata B Rao             error_setg(errp, "CPU hot unplug not supported on this machine");
23086f4b5c3eSBharata B Rao             return;
23096f4b5c3eSBharata B Rao         }
23106f4b5c3eSBharata B Rao         spapr_core_unplug(hotplug_dev, dev, errp);
2311c20d332aSBharata B Rao     }
2312c20d332aSBharata B Rao }
2313c20d332aSBharata B Rao 
231494a94e4cSBharata B Rao static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
231594a94e4cSBharata B Rao                                           DeviceState *dev, Error **errp)
231694a94e4cSBharata B Rao {
231794a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
231894a94e4cSBharata B Rao         spapr_core_pre_plug(hotplug_dev, dev, errp);
231994a94e4cSBharata B Rao     }
232094a94e4cSBharata B Rao }
232194a94e4cSBharata B Rao 
23227ebaf795SBharata B Rao static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
2323c20d332aSBharata B Rao                                                  DeviceState *dev)
2324c20d332aSBharata B Rao {
232594a94e4cSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
232694a94e4cSBharata B Rao         object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
2327c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
2328c20d332aSBharata B Rao     }
2329c20d332aSBharata B Rao     return NULL;
2330c20d332aSBharata B Rao }
2331c20d332aSBharata B Rao 
233220bb648dSDavid Gibson static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
233320bb648dSDavid Gibson {
233420bb648dSDavid Gibson     /* Allocate to NUMA nodes on a "socket" basis (not that concept of
233520bb648dSDavid Gibson      * socket means much for the paravirtualized PAPR platform) */
233620bb648dSDavid Gibson     return cpu_index / smp_threads / smp_cores;
233720bb648dSDavid Gibson }
233820bb648dSDavid Gibson 
23392474bfd4SIgor Mammedov static HotpluggableCPUList *spapr_query_hotpluggable_cpus(MachineState *machine)
23402474bfd4SIgor Mammedov {
23412474bfd4SIgor Mammedov     int i;
23422474bfd4SIgor Mammedov     HotpluggableCPUList *head = NULL;
23432474bfd4SIgor Mammedov     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
23442474bfd4SIgor Mammedov     int spapr_max_cores = max_cpus / smp_threads;
23452474bfd4SIgor Mammedov 
23462474bfd4SIgor Mammedov     for (i = 0; i < spapr_max_cores; i++) {
23472474bfd4SIgor Mammedov         HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
23482474bfd4SIgor Mammedov         HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
23492474bfd4SIgor Mammedov         CpuInstanceProperties *cpu_props = g_new0(typeof(*cpu_props), 1);
23502474bfd4SIgor Mammedov 
23512474bfd4SIgor Mammedov         cpu_item->type = spapr_get_cpu_core_type(machine->cpu_model);
23522474bfd4SIgor Mammedov         cpu_item->vcpus_count = smp_threads;
235327393c33SPeter Krempa         cpu_props->has_core_id = true;
235412bf2d33SGreg Kurz         cpu_props->core_id = i * smp_threads;
23552474bfd4SIgor Mammedov         /* TODO: add 'has_node/node' here to describe
23562474bfd4SIgor Mammedov            to which node core belongs */
23572474bfd4SIgor Mammedov 
23582474bfd4SIgor Mammedov         cpu_item->props = cpu_props;
23592474bfd4SIgor Mammedov         if (spapr->cores[i]) {
23602474bfd4SIgor Mammedov             cpu_item->has_qom_path = true;
23612474bfd4SIgor Mammedov             cpu_item->qom_path = object_get_canonical_path(spapr->cores[i]);
23622474bfd4SIgor Mammedov         }
23632474bfd4SIgor Mammedov         list_item->value = cpu_item;
23642474bfd4SIgor Mammedov         list_item->next = head;
23652474bfd4SIgor Mammedov         head = list_item;
23662474bfd4SIgor Mammedov     }
23672474bfd4SIgor Mammedov     return head;
23682474bfd4SIgor Mammedov }
23692474bfd4SIgor Mammedov 
237029ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
237153018216SPaolo Bonzini {
237229ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
2373224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
237471461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
237534316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
2376c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
237729ee3247SAlexey Kardashevskiy 
23780eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
2379fc9f38c3SDavid Gibson 
2380fc9f38c3SDavid Gibson     /*
2381fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
2382fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
2383fc9f38c3SDavid Gibson      * these details for backwards compatibility
2384fc9f38c3SDavid Gibson      */
2385958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
2386958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
2387958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
238838b02bd8SAlexey Kardashevskiy     mc->max_cpus = MAX_CPUMASK_BITS;
2389958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
23905b2128d2SAlexander Graf     mc->default_boot_order = "";
2391a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
2392958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
23939e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
2394e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
23957ebaf795SBharata B Rao     mc->get_hotplug_handler = spapr_get_hotplug_handler;
239694a94e4cSBharata B Rao     hc->pre_plug = spapr_machine_device_pre_plug;
2397c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
2398c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
239920bb648dSDavid Gibson     mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
240000b4fbe2SMarcel Apfelbaum 
2401fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
24023c0c47e3SDavid Gibson     mc->query_hotpluggable_cpus = spapr_query_hotpluggable_cpus;
240371461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
240434316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
240553018216SPaolo Bonzini }
240653018216SPaolo Bonzini 
240729ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
240829ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
240929ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
24104aee7362SDavid Gibson     .abstract      = true,
24116ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
241223825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
241387bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
2414183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
241529ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
241671461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
241771461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
241834316482SAlexey Kardashevskiy         { TYPE_NMI },
2419c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
242071461b0fSAlexey Kardashevskiy         { }
242171461b0fSAlexey Kardashevskiy     },
242229ee3247SAlexey Kardashevskiy };
242329ee3247SAlexey Kardashevskiy 
2424fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
24255013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
24265013c547SDavid Gibson                                                     void *data)      \
24275013c547SDavid Gibson     {                                                                \
24285013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
24295013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
2430fccbc785SDavid Gibson         if (latest) {                                                \
2431fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
2432fccbc785SDavid Gibson             mc->is_default = 1;                                      \
2433fccbc785SDavid Gibson         }                                                            \
24345013c547SDavid Gibson     }                                                                \
24355013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
24365013c547SDavid Gibson     {                                                                \
24375013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
24385013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
24395013c547SDavid Gibson     }                                                                \
24405013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
24415013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
24425013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
24435013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
24445013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
24455013c547SDavid Gibson     };                                                               \
24465013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
24475013c547SDavid Gibson     {                                                                \
24485013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
24495013c547SDavid Gibson     }                                                                \
24500e6aac87SEduardo Habkost     type_init(spapr_machine_register_##suffix)
24515013c547SDavid Gibson 
24521c5f29bbSDavid Gibson /*
2453db800b21SDavid Gibson  * pseries-2.8
2454db800b21SDavid Gibson  */
2455db800b21SDavid Gibson static void spapr_machine_2_8_instance_options(MachineState *machine)
2456db800b21SDavid Gibson {
2457db800b21SDavid Gibson }
2458db800b21SDavid Gibson 
2459db800b21SDavid Gibson static void spapr_machine_2_8_class_options(MachineClass *mc)
2460db800b21SDavid Gibson {
2461db800b21SDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
2462db800b21SDavid Gibson }
2463db800b21SDavid Gibson 
2464db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_8, "2.8", true);
2465db800b21SDavid Gibson 
2466db800b21SDavid Gibson /*
24671ea1eefcSBharata B Rao  * pseries-2.7
24681ea1eefcSBharata B Rao  */
2469db800b21SDavid Gibson #define SPAPR_COMPAT_2_7 \
2470db800b21SDavid Gibson     HW_COMPAT_2_7 \
2471db800b21SDavid Gibson 
24721ea1eefcSBharata B Rao static void spapr_machine_2_7_instance_options(MachineState *machine)
24731ea1eefcSBharata B Rao {
24741ea1eefcSBharata B Rao }
24751ea1eefcSBharata B Rao 
24761ea1eefcSBharata B Rao static void spapr_machine_2_7_class_options(MachineClass *mc)
24771ea1eefcSBharata B Rao {
2478db800b21SDavid Gibson     spapr_machine_2_8_class_options(mc);
2479db800b21SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_7);
24801ea1eefcSBharata B Rao }
24811ea1eefcSBharata B Rao 
2482db800b21SDavid Gibson DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
24831ea1eefcSBharata B Rao 
24841ea1eefcSBharata B Rao /*
24854b23699cSDavid Gibson  * pseries-2.6
24864b23699cSDavid Gibson  */
24871ea1eefcSBharata B Rao #define SPAPR_COMPAT_2_6 \
2488ae4de14cSAlexey Kardashevskiy     HW_COMPAT_2_6 \
2489ae4de14cSAlexey Kardashevskiy     { \
2490ae4de14cSAlexey Kardashevskiy         .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
2491ae4de14cSAlexey Kardashevskiy         .property = "ddw",\
2492ae4de14cSAlexey Kardashevskiy         .value    = stringify(off),\
2493ae4de14cSAlexey Kardashevskiy     },
24941ea1eefcSBharata B Rao 
24954b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
24964b23699cSDavid Gibson {
24974b23699cSDavid Gibson }
24984b23699cSDavid Gibson 
24994b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
25004b23699cSDavid Gibson {
25011ea1eefcSBharata B Rao     spapr_machine_2_7_class_options(mc);
25023c0c47e3SDavid Gibson     mc->query_hotpluggable_cpus = NULL;
25031ea1eefcSBharata B Rao     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);
25044b23699cSDavid Gibson }
25054b23699cSDavid Gibson 
25061ea1eefcSBharata B Rao DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
25074b23699cSDavid Gibson 
25084b23699cSDavid Gibson /*
25091c5f29bbSDavid Gibson  * pseries-2.5
25101c5f29bbSDavid Gibson  */
25114b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
251257c522f4SThomas Huth     HW_COMPAT_2_5 \
251357c522f4SThomas Huth     { \
251457c522f4SThomas Huth         .driver   = "spapr-vlan", \
251557c522f4SThomas Huth         .property = "use-rx-buffer-pools", \
251657c522f4SThomas Huth         .value    = "off", \
251757c522f4SThomas Huth     },
25184b23699cSDavid Gibson 
25195013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
25201c5f29bbSDavid Gibson {
25215013c547SDavid Gibson }
25225013c547SDavid Gibson 
25235013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
25245013c547SDavid Gibson {
252557040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
252657040d45SThomas Huth 
25274b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
252857040d45SThomas Huth     smc->use_ohci_by_default = true;
25294b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
25301c5f29bbSDavid Gibson }
25311c5f29bbSDavid Gibson 
25324b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
25331c5f29bbSDavid Gibson 
25341c5f29bbSDavid Gibson /*
25351c5f29bbSDavid Gibson  * pseries-2.4
25361c5f29bbSDavid Gibson  */
253780fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
253880fd50f9SCornelia Huck         HW_COMPAT_2_4
253980fd50f9SCornelia Huck 
25405013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
25411c5f29bbSDavid Gibson {
25425013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
25435013c547SDavid Gibson }
25441c5f29bbSDavid Gibson 
25455013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
25465013c547SDavid Gibson {
2547fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
2548fc9f38c3SDavid Gibson 
2549fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
2550fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
2551f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
25521c5f29bbSDavid Gibson }
25531c5f29bbSDavid Gibson 
2554fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
25551c5f29bbSDavid Gibson 
25561c5f29bbSDavid Gibson /*
25571c5f29bbSDavid Gibson  * pseries-2.3
25581c5f29bbSDavid Gibson  */
255938ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
25607619c7b0SMichael Roth         HW_COMPAT_2_3 \
25617619c7b0SMichael Roth         {\
25627619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
25637619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
25647619c7b0SMichael Roth             .value    = "off",\
25657619c7b0SMichael Roth         },
256638ff32c6SEduardo Habkost 
25675013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
25681c5f29bbSDavid Gibson {
25695013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
25701c5f29bbSDavid Gibson     savevm_skip_section_footers();
25711c5f29bbSDavid Gibson     global_state_set_optional();
257209b5e30dSGreg Kurz     savevm_skip_configuration();
25731c5f29bbSDavid Gibson }
25741c5f29bbSDavid Gibson 
25755013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
25761c5f29bbSDavid Gibson {
2577fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
2578f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
25791c5f29bbSDavid Gibson }
2580fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
25811c5f29bbSDavid Gibson 
25821c5f29bbSDavid Gibson /*
25831c5f29bbSDavid Gibson  * pseries-2.2
25841c5f29bbSDavid Gibson  */
25851c5f29bbSDavid Gibson 
2586b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
25874dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
2588b194df47SAlexey Kardashevskiy         {\
2589b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
2590b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
2591b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
2592dd754bafSEduardo Habkost         },
2593b194df47SAlexey Kardashevskiy 
25945013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
2595b0e966d0SJason Wang {
25965013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
2597cba0e779SGreg Kurz     machine->suppress_vmdesc = true;
2598b0e966d0SJason Wang }
2599b0e966d0SJason Wang 
26005013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
2601b0e966d0SJason Wang {
2602fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
2603f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
26041c5f29bbSDavid Gibson }
2605fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
26061c5f29bbSDavid Gibson 
26071c5f29bbSDavid Gibson /*
26081c5f29bbSDavid Gibson  * pseries-2.1
26091c5f29bbSDavid Gibson  */
26101c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
26111c5f29bbSDavid Gibson         HW_COMPAT_2_1
26121c5f29bbSDavid Gibson 
26135013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
26141c5f29bbSDavid Gibson {
26155013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
26161c5f29bbSDavid Gibson }
26171c5f29bbSDavid Gibson 
26185013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
2619b0e966d0SJason Wang {
2620fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
2621f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
26226026db45SAlexey Kardashevskiy }
2623fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
26246026db45SAlexey Kardashevskiy 
262529ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
262629ee3247SAlexey Kardashevskiy {
262729ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
262829ee3247SAlexey Kardashevskiy }
262929ee3247SAlexey Kardashevskiy 
263029ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
2631