xref: /openbmc/qemu/hw/ppc/spapr.c (revision b83baa60)
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  */
2753018216SPaolo Bonzini #include "sysemu/sysemu.h"
28e35704baSEduardo Habkost #include "sysemu/numa.h"
2953018216SPaolo Bonzini #include "hw/hw.h"
3071461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h"
3153018216SPaolo Bonzini #include "elf.h"
3253018216SPaolo Bonzini #include "net/net.h"
33ad440b4aSAndrew Jones #include "sysemu/device_tree.h"
34fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3553018216SPaolo Bonzini #include "sysemu/cpus.h"
3653018216SPaolo Bonzini #include "sysemu/kvm.h"
37c20d332aSBharata B Rao #include "sysemu/device_tree.h"
3853018216SPaolo Bonzini #include "kvm_ppc.h"
39ff14e817SDr. David Alan Gilbert #include "migration/migration.h"
404be21d56SDavid Gibson #include "mmu-hash64.h"
413794d548SAlexey Kardashevskiy #include "qom/cpu.h"
4253018216SPaolo Bonzini 
4353018216SPaolo Bonzini #include "hw/boards.h"
440d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
4553018216SPaolo Bonzini #include "hw/loader.h"
4653018216SPaolo Bonzini 
470d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
480d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
490d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
500d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
5153018216SPaolo Bonzini #include "hw/pci/msi.h"
5253018216SPaolo Bonzini 
5353018216SPaolo Bonzini #include "hw/pci/pci.h"
5471461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
5571461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
5653018216SPaolo Bonzini 
5753018216SPaolo Bonzini #include "exec/address-spaces.h"
5853018216SPaolo Bonzini #include "hw/usb.h"
5953018216SPaolo Bonzini #include "qemu/config-file.h"
60135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
612a6593cbSAlexey Kardashevskiy #include "trace.h"
6234316482SAlexey Kardashevskiy #include "hw/nmi.h"
6353018216SPaolo Bonzini 
6468a27b20SMichael S. Tsirkin #include "hw/compat.h"
65224245bfSDavid Gibson #include "qemu-common.h"
6668a27b20SMichael S. Tsirkin 
6753018216SPaolo Bonzini #include <libfdt.h>
6853018216SPaolo Bonzini 
6953018216SPaolo Bonzini /* SLOF memory layout:
7053018216SPaolo Bonzini  *
7153018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
7253018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
7353018216SPaolo Bonzini  *
7453018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
7553018216SPaolo Bonzini  * and more
7653018216SPaolo Bonzini  *
7753018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
7853018216SPaolo Bonzini  */
7938b02bd8SAlexey Kardashevskiy #define FDT_MAX_SIZE            0x100000
8053018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
81b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
8253018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
8353018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
8453018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
8553018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
8653018216SPaolo Bonzini 
8753018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
8853018216SPaolo Bonzini 
8953018216SPaolo Bonzini #define TIMEBASE_FREQ           512000000ULL
9053018216SPaolo Bonzini 
9153018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9253018216SPaolo Bonzini 
9353018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9453018216SPaolo Bonzini 
95c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers,
9634f2af3dSMarkus Armbruster                                   int nr_irqs, Error **errp)
97c04d6cfaSAnthony Liguori {
9834f2af3dSMarkus Armbruster     Error *err = NULL;
99c04d6cfaSAnthony Liguori     DeviceState *dev;
100c04d6cfaSAnthony Liguori 
101c04d6cfaSAnthony Liguori     dev = qdev_create(NULL, type);
102c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
103c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
10434f2af3dSMarkus Armbruster     object_property_set_bool(OBJECT(dev), true, "realized", &err);
10534f2af3dSMarkus Armbruster     if (err) {
10634f2af3dSMarkus Armbruster         error_propagate(errp, err);
10734f2af3dSMarkus Armbruster         object_unparent(OBJECT(dev));
108c04d6cfaSAnthony Liguori         return NULL;
109c04d6cfaSAnthony Liguori     }
1105a3d7b23SAlexey Kardashevskiy     return XICS_COMMON(dev);
111c04d6cfaSAnthony Liguori }
112c04d6cfaSAnthony Liguori 
113446f16a6SMarcel Apfelbaum static XICSState *xics_system_init(MachineState *machine,
114446f16a6SMarcel Apfelbaum                                    int nr_servers, int nr_irqs)
115c04d6cfaSAnthony Liguori {
116c04d6cfaSAnthony Liguori     XICSState *icp = NULL;
117c04d6cfaSAnthony Liguori 
11811ad93f6SDavid Gibson     if (kvm_enabled()) {
11934f2af3dSMarkus Armbruster         Error *err = NULL;
12034f2af3dSMarkus Armbruster 
121446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_allowed(machine)) {
12234f2af3dSMarkus Armbruster             icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs, &err);
12311ad93f6SDavid Gibson         }
124446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_required(machine) && !icp) {
125*b83baa60SMarkus Armbruster             error_reportf_err(err,
126*b83baa60SMarkus Armbruster                               "kernel_irqchip requested but unavailable: ");
127*b83baa60SMarkus Armbruster         } else {
128903a41d3SStefano Dong (董兴水)             error_free(err);
12911ad93f6SDavid Gibson         }
130*b83baa60SMarkus Armbruster     }
13111ad93f6SDavid Gibson 
13211ad93f6SDavid Gibson     if (!icp) {
13334f2af3dSMarkus Armbruster         icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs, &error_abort);
134c04d6cfaSAnthony Liguori     }
135c04d6cfaSAnthony Liguori 
136c04d6cfaSAnthony Liguori     return icp;
137c04d6cfaSAnthony Liguori }
138c04d6cfaSAnthony Liguori 
139833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
140833d4668SAlexey Kardashevskiy                                   int smt_threads)
141833d4668SAlexey Kardashevskiy {
142833d4668SAlexey Kardashevskiy     int i, ret = 0;
143833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
144833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
145833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
146833d4668SAlexey Kardashevskiy 
1476d9412eaSAlexey Kardashevskiy     if (cpu->cpu_version) {
1484bce526eSLaurent Dufour         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version);
1496d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1506d9412eaSAlexey Kardashevskiy             return ret;
1516d9412eaSAlexey Kardashevskiy         }
1526d9412eaSAlexey Kardashevskiy     }
1536d9412eaSAlexey Kardashevskiy 
154833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
155833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
156833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
157833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
158833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
159833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
160833d4668SAlexey Kardashevskiy     }
161833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
162833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
163833d4668SAlexey Kardashevskiy     if (ret < 0) {
164833d4668SAlexey Kardashevskiy         return ret;
165833d4668SAlexey Kardashevskiy     }
166833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
167833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
168833d4668SAlexey Kardashevskiy 
169833d4668SAlexey Kardashevskiy     return ret;
170833d4668SAlexey Kardashevskiy }
171833d4668SAlexey Kardashevskiy 
1720da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs)
1730da6f3feSBharata B Rao {
1740da6f3feSBharata B Rao     int ret = 0;
1750da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
1760da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
1770da6f3feSBharata B Rao     uint32_t associativity[] = {cpu_to_be32(0x5),
1780da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1790da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1800da6f3feSBharata B Rao                                 cpu_to_be32(0x0),
1810da6f3feSBharata B Rao                                 cpu_to_be32(cs->numa_node),
1820da6f3feSBharata B Rao                                 cpu_to_be32(index)};
1830da6f3feSBharata B Rao 
1840da6f3feSBharata B Rao     /* Advertise NUMA via ibm,associativity */
1850da6f3feSBharata B Rao     if (nb_numa_nodes > 1) {
1860da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
1870da6f3feSBharata B Rao                           sizeof(associativity));
1880da6f3feSBharata B Rao     }
1890da6f3feSBharata B Rao 
1900da6f3feSBharata B Rao     return ret;
1910da6f3feSBharata B Rao }
1920da6f3feSBharata B Rao 
19328e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr)
19453018216SPaolo Bonzini {
19582677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
19682677ed2SAlexey Kardashevskiy     CPUState *cs;
19753018216SPaolo Bonzini     char cpu_model[32];
19853018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
19953018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
20053018216SPaolo Bonzini 
20182677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
20282677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
20382677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
20482677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
20553018216SPaolo Bonzini 
2060f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
20753018216SPaolo Bonzini             continue;
20853018216SPaolo Bonzini         }
20953018216SPaolo Bonzini 
21082677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
21153018216SPaolo Bonzini 
21282677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
21382677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
21482677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
21582677ed2SAlexey Kardashevskiy                                           "cpus");
21682677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
21782677ed2SAlexey Kardashevskiy                 return cpus_offset;
21882677ed2SAlexey Kardashevskiy             }
21982677ed2SAlexey Kardashevskiy         }
22082677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
22182677ed2SAlexey Kardashevskiy         if (offset < 0) {
22282677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
22353018216SPaolo Bonzini             if (offset < 0) {
22453018216SPaolo Bonzini                 return offset;
22553018216SPaolo Bonzini             }
22682677ed2SAlexey Kardashevskiy         }
22753018216SPaolo Bonzini 
2280da6f3feSBharata B Rao         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
2290da6f3feSBharata B Rao                           pft_size_prop, sizeof(pft_size_prop));
23053018216SPaolo Bonzini         if (ret < 0) {
23153018216SPaolo Bonzini             return ret;
23253018216SPaolo Bonzini         }
23353018216SPaolo Bonzini 
2340da6f3feSBharata B Rao         ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs);
23553018216SPaolo Bonzini         if (ret < 0) {
23653018216SPaolo Bonzini             return ret;
23753018216SPaolo Bonzini         }
238833d4668SAlexey Kardashevskiy 
23982677ed2SAlexey Kardashevskiy         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
2402a48d993SAlexey Kardashevskiy                                      ppc_get_compat_smt_threads(cpu));
241833d4668SAlexey Kardashevskiy         if (ret < 0) {
242833d4668SAlexey Kardashevskiy             return ret;
243833d4668SAlexey Kardashevskiy         }
24453018216SPaolo Bonzini     }
24553018216SPaolo Bonzini     return ret;
24653018216SPaolo Bonzini }
24753018216SPaolo Bonzini 
24853018216SPaolo Bonzini 
24953018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
25053018216SPaolo Bonzini                                      size_t maxsize)
25153018216SPaolo Bonzini {
25253018216SPaolo Bonzini     size_t maxcells = maxsize / sizeof(uint32_t);
25353018216SPaolo Bonzini     int i, j, count;
25453018216SPaolo Bonzini     uint32_t *p = prop;
25553018216SPaolo Bonzini 
25653018216SPaolo Bonzini     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
25753018216SPaolo Bonzini         struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
25853018216SPaolo Bonzini 
25953018216SPaolo Bonzini         if (!sps->page_shift) {
26053018216SPaolo Bonzini             break;
26153018216SPaolo Bonzini         }
26253018216SPaolo Bonzini         for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
26353018216SPaolo Bonzini             if (sps->enc[count].page_shift == 0) {
26453018216SPaolo Bonzini                 break;
26553018216SPaolo Bonzini             }
26653018216SPaolo Bonzini         }
26753018216SPaolo Bonzini         if ((p - prop) >= (maxcells - 3 - count * 2)) {
26853018216SPaolo Bonzini             break;
26953018216SPaolo Bonzini         }
27053018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->page_shift);
27153018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->slb_enc);
27253018216SPaolo Bonzini         *(p++) = cpu_to_be32(count);
27353018216SPaolo Bonzini         for (j = 0; j < count; j++) {
27453018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].page_shift);
27553018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
27653018216SPaolo Bonzini         }
27753018216SPaolo Bonzini     }
27853018216SPaolo Bonzini 
27953018216SPaolo Bonzini     return (p - prop) * sizeof(uint32_t);
28053018216SPaolo Bonzini }
28153018216SPaolo Bonzini 
282b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
283b082d65aSAlexey Kardashevskiy {
284fb164994SDavid Gibson     MachineState *machine = MACHINE(qdev_get_machine());
285fb164994SDavid Gibson 
286b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
287b082d65aSAlexey Kardashevskiy         int i;
288b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
289b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
290fb164994SDavid Gibson                 return MIN(pow2floor(numa_info[i].node_mem),
291fb164994SDavid Gibson                            machine->ram_size);
292b082d65aSAlexey Kardashevskiy             }
293b082d65aSAlexey Kardashevskiy         }
294b082d65aSAlexey Kardashevskiy     }
295fb164994SDavid Gibson     return machine->ram_size;
296b082d65aSAlexey Kardashevskiy }
297b082d65aSAlexey Kardashevskiy 
29853018216SPaolo Bonzini #define _FDT(exp) \
29953018216SPaolo Bonzini     do { \
30053018216SPaolo Bonzini         int ret = (exp);                                           \
30153018216SPaolo Bonzini         if (ret < 0) {                                             \
30253018216SPaolo Bonzini             fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
30353018216SPaolo Bonzini                     #exp, fdt_strerror(ret));                      \
30453018216SPaolo Bonzini             exit(1);                                               \
30553018216SPaolo Bonzini         }                                                          \
30653018216SPaolo Bonzini     } while (0)
30753018216SPaolo Bonzini 
308a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
309a1d59c0fSAlexey Kardashevskiy {
310a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
311a1d59c0fSAlexey Kardashevskiy }
31253018216SPaolo Bonzini 
3133bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base,
31453018216SPaolo Bonzini                                    hwaddr initrd_size,
31553018216SPaolo Bonzini                                    hwaddr kernel_size,
31616457e7fSBenjamin Herrenschmidt                                    bool little_endian,
31753018216SPaolo Bonzini                                    const char *kernel_cmdline,
31853018216SPaolo Bonzini                                    uint32_t epow_irq)
31953018216SPaolo Bonzini {
32053018216SPaolo Bonzini     void *fdt;
32153018216SPaolo Bonzini     uint32_t start_prop = cpu_to_be32(initrd_base);
32253018216SPaolo Bonzini     uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
323a1d59c0fSAlexey Kardashevskiy     GString *hypertas = g_string_sized_new(256);
324a1d59c0fSAlexey Kardashevskiy     GString *qemu_hypertas = g_string_sized_new(256);
32553018216SPaolo Bonzini     uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
3269e734e3dSBharata B Rao     uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(max_cpus)};
32753018216SPaolo Bonzini     unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
328ef951443SNikunj A Dadhania     char *buf;
32953018216SPaolo Bonzini 
330a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-pft");
331a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-term");
332a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-dabr");
333a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-interrupt");
334a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-tce");
335a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-vio");
336a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-splpar");
337a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-bulk");
338a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-set-mode");
339a1d59c0fSAlexey Kardashevskiy     add_str(qemu_hypertas, "hcall-memop1");
340a1d59c0fSAlexey Kardashevskiy 
34153018216SPaolo Bonzini     fdt = g_malloc0(FDT_MAX_SIZE);
34253018216SPaolo Bonzini     _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
34353018216SPaolo Bonzini 
34453018216SPaolo Bonzini     if (kernel_size) {
34553018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
34653018216SPaolo Bonzini     }
34753018216SPaolo Bonzini     if (initrd_size) {
34853018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
34953018216SPaolo Bonzini     }
35053018216SPaolo Bonzini     _FDT((fdt_finish_reservemap(fdt)));
35153018216SPaolo Bonzini 
35253018216SPaolo Bonzini     /* Root node */
35353018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "")));
35453018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "chrp")));
35553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
356fa388916SAnthony Liguori     _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
35753018216SPaolo Bonzini 
358ef951443SNikunj A Dadhania     /*
359ef951443SNikunj A Dadhania      * Add info to guest to indentify which host is it being run on
360ef951443SNikunj A Dadhania      * and what is the uuid of the guest
361ef951443SNikunj A Dadhania      */
362ef951443SNikunj A Dadhania     if (kvmppc_get_host_model(&buf)) {
363ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-model", buf)));
364ef951443SNikunj A Dadhania         g_free(buf);
365ef951443SNikunj A Dadhania     }
366ef951443SNikunj A Dadhania     if (kvmppc_get_host_serial(&buf)) {
367ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-serial", buf)));
368ef951443SNikunj A Dadhania         g_free(buf);
369ef951443SNikunj A Dadhania     }
370ef951443SNikunj A Dadhania 
371ef951443SNikunj A Dadhania     buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1],
372ef951443SNikunj A Dadhania                           qemu_uuid[2], qemu_uuid[3], qemu_uuid[4],
373ef951443SNikunj A Dadhania                           qemu_uuid[5], qemu_uuid[6], qemu_uuid[7],
374ef951443SNikunj A Dadhania                           qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
375ef951443SNikunj A Dadhania                           qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
376ef951443SNikunj A Dadhania                           qemu_uuid[14], qemu_uuid[15]);
377ef951443SNikunj A Dadhania 
378ef951443SNikunj A Dadhania     _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
3793dc0a66dSAlexey Kardashevskiy     if (qemu_uuid_set) {
3803dc0a66dSAlexey Kardashevskiy         _FDT((fdt_property_string(fdt, "system-id", buf)));
3813dc0a66dSAlexey Kardashevskiy     }
382ef951443SNikunj A Dadhania     g_free(buf);
383ef951443SNikunj A Dadhania 
3842c1aaa81SSam Bobroff     if (qemu_get_vm_name()) {
3852c1aaa81SSam Bobroff         _FDT((fdt_property_string(fdt, "ibm,partition-name",
3862c1aaa81SSam Bobroff                                   qemu_get_vm_name())));
3872c1aaa81SSam Bobroff     }
3882c1aaa81SSam Bobroff 
38953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
39053018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
39153018216SPaolo Bonzini 
39253018216SPaolo Bonzini     /* /chosen */
39353018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "chosen")));
39453018216SPaolo Bonzini 
39553018216SPaolo Bonzini     /* Set Form1_affinity */
39653018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
39753018216SPaolo Bonzini 
39853018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
39953018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-start",
40053018216SPaolo Bonzini                        &start_prop, sizeof(start_prop))));
40153018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-end",
40253018216SPaolo Bonzini                        &end_prop, sizeof(end_prop))));
40353018216SPaolo Bonzini     if (kernel_size) {
40453018216SPaolo Bonzini         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
40553018216SPaolo Bonzini                               cpu_to_be64(kernel_size) };
40653018216SPaolo Bonzini 
40753018216SPaolo Bonzini         _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
40816457e7fSBenjamin Herrenschmidt         if (little_endian) {
40916457e7fSBenjamin Herrenschmidt             _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0)));
41016457e7fSBenjamin Herrenschmidt         }
41153018216SPaolo Bonzini     }
412cc84c0f3SAvik Sil     if (boot_menu) {
413cc84c0f3SAvik Sil         _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu)));
414cc84c0f3SAvik Sil     }
41553018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
41653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
41753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
41853018216SPaolo Bonzini 
41953018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
42053018216SPaolo Bonzini 
42153018216SPaolo Bonzini     /* RTAS */
42253018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "rtas")));
42353018216SPaolo Bonzini 
424da95324eSAlexey Kardashevskiy     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
425da95324eSAlexey Kardashevskiy         add_str(hypertas, "hcall-multi-tce");
426da95324eSAlexey Kardashevskiy     }
427a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str,
428a1d59c0fSAlexey Kardashevskiy                        hypertas->len)));
429a1d59c0fSAlexey Kardashevskiy     g_string_free(hypertas, TRUE);
430a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str,
431a1d59c0fSAlexey Kardashevskiy                        qemu_hypertas->len)));
432a1d59c0fSAlexey Kardashevskiy     g_string_free(qemu_hypertas, TRUE);
43353018216SPaolo Bonzini 
43453018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
43553018216SPaolo Bonzini         refpoints, sizeof(refpoints))));
43653018216SPaolo Bonzini 
43753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
43879853e18STyrel Datwyler     _FDT((fdt_property_cell(fdt, "rtas-event-scan-rate",
43979853e18STyrel Datwyler                             RTAS_EVENT_SCAN_RATE)));
44053018216SPaolo Bonzini 
441a95f9922SSam Bobroff     if (msi_supported) {
442a95f9922SSam Bobroff         _FDT((fdt_property(fdt, "ibm,change-msix-capable", NULL, 0)));
443a95f9922SSam Bobroff     }
444a95f9922SSam Bobroff 
4452e14072fSNikunj A Dadhania     /*
4469d632f5fSzhanghailiang      * According to PAPR, rtas ibm,os-term does not guarantee a return
4472e14072fSNikunj A Dadhania      * back to the guest cpu.
4482e14072fSNikunj A Dadhania      *
4492e14072fSNikunj A Dadhania      * While an additional ibm,extended-os-term property indicates that
4502e14072fSNikunj A Dadhania      * rtas call return will always occur. Set this property.
4512e14072fSNikunj A Dadhania      */
4522e14072fSNikunj A Dadhania     _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0)));
4532e14072fSNikunj A Dadhania 
45453018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
45553018216SPaolo Bonzini 
45653018216SPaolo Bonzini     /* interrupt controller */
45753018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "interrupt-controller")));
45853018216SPaolo Bonzini 
45953018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type",
46053018216SPaolo Bonzini                               "PowerPC-External-Interrupt-Presentation")));
46153018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
46253018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
46353018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
46453018216SPaolo Bonzini                        interrupt_server_ranges_prop,
46553018216SPaolo Bonzini                        sizeof(interrupt_server_ranges_prop))));
46653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
46753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
46853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
46953018216SPaolo Bonzini 
47053018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
47153018216SPaolo Bonzini 
47253018216SPaolo Bonzini     /* vdevice */
47353018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "vdevice")));
47453018216SPaolo Bonzini 
47553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
47653018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
47753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
47853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
47953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
48053018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
48153018216SPaolo Bonzini 
48253018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
48353018216SPaolo Bonzini 
48453018216SPaolo Bonzini     /* event-sources */
48553018216SPaolo Bonzini     spapr_events_fdt_skel(fdt, epow_irq);
48653018216SPaolo Bonzini 
487f7d69146SAlexander Graf     /* /hypervisor node */
488f7d69146SAlexander Graf     if (kvm_enabled()) {
489f7d69146SAlexander Graf         uint8_t hypercall[16];
490f7d69146SAlexander Graf 
491f7d69146SAlexander Graf         /* indicate KVM hypercall interface */
492f7d69146SAlexander Graf         _FDT((fdt_begin_node(fdt, "hypervisor")));
493f7d69146SAlexander Graf         _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
494f7d69146SAlexander Graf         if (kvmppc_has_cap_fixup_hcalls()) {
495f7d69146SAlexander Graf             /*
496f7d69146SAlexander Graf              * Older KVM versions with older guest kernels were broken with the
497f7d69146SAlexander Graf              * magic page, don't allow the guest to map it.
498f7d69146SAlexander Graf              */
499f7d69146SAlexander Graf             kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
500f7d69146SAlexander Graf                                  sizeof(hypercall));
501f7d69146SAlexander Graf             _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
502f7d69146SAlexander Graf                               sizeof(hypercall))));
503f7d69146SAlexander Graf         }
504f7d69146SAlexander Graf         _FDT((fdt_end_node(fdt)));
505f7d69146SAlexander Graf     }
506f7d69146SAlexander Graf 
50753018216SPaolo Bonzini     _FDT((fdt_end_node(fdt))); /* close root node */
50853018216SPaolo Bonzini     _FDT((fdt_finish(fdt)));
50953018216SPaolo Bonzini 
51053018216SPaolo Bonzini     return fdt;
51153018216SPaolo Bonzini }
51253018216SPaolo Bonzini 
51303d196b7SBharata B Rao static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
51426a8c353SAlexey Kardashevskiy                                        hwaddr size)
51526a8c353SAlexey Kardashevskiy {
51626a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
51726a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
51826a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
519c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
52026a8c353SAlexey Kardashevskiy     };
52126a8c353SAlexey Kardashevskiy     char mem_name[32];
52226a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
52326a8c353SAlexey Kardashevskiy     int off;
52426a8c353SAlexey Kardashevskiy 
52526a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
52626a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
52726a8c353SAlexey Kardashevskiy 
52826a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
52926a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
53026a8c353SAlexey Kardashevskiy     _FDT(off);
53126a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
53226a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
53326a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
53426a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
53526a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
53603d196b7SBharata B Rao     return off;
53726a8c353SAlexey Kardashevskiy }
53826a8c353SAlexey Kardashevskiy 
53928e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt)
54053018216SPaolo Bonzini {
541fb164994SDavid Gibson     MachineState *machine = MACHINE(spapr);
5427db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
5437db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
5447db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
5457db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
54653018216SPaolo Bonzini 
5477db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
5487db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
5497db8a127SAlexey Kardashevskiy         nb_nodes = 1;
550fb164994SDavid Gibson         ramnode.node_mem = machine->ram_size;
5517db8a127SAlexey Kardashevskiy         nodes = &ramnode;
5525fe269b1SPaul Mackerras     }
55353018216SPaolo Bonzini 
5547db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
5557db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
5567db8a127SAlexey Kardashevskiy             continue;
55753018216SPaolo Bonzini         }
558fb164994SDavid Gibson         if (mem_start >= machine->ram_size) {
5595fe269b1SPaul Mackerras             node_size = 0;
5605fe269b1SPaul Mackerras         } else {
5617db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
562fb164994SDavid Gibson             if (node_size > machine->ram_size - mem_start) {
563fb164994SDavid Gibson                 node_size = machine->ram_size - mem_start;
5645fe269b1SPaul Mackerras             }
5655fe269b1SPaul Mackerras         }
5667db8a127SAlexey Kardashevskiy         if (!mem_start) {
5677db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
568e8f986fcSBharata B Rao             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
5697db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
5707db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
5717db8a127SAlexey Kardashevskiy         }
5726010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
5736010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
5746010818cSAlexey Kardashevskiy 
5756010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
5766010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
5776010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
5786010818cSAlexey Kardashevskiy             }
5796010818cSAlexey Kardashevskiy 
5806010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
5816010818cSAlexey Kardashevskiy             node_size -= sizetmp;
5826010818cSAlexey Kardashevskiy             mem_start += sizetmp;
5836010818cSAlexey Kardashevskiy         }
58453018216SPaolo Bonzini     }
58553018216SPaolo Bonzini 
58653018216SPaolo Bonzini     return 0;
58753018216SPaolo Bonzini }
58853018216SPaolo Bonzini 
5890da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
5900da6f3feSBharata B Rao                                   sPAPRMachineState *spapr)
5910da6f3feSBharata B Rao {
5920da6f3feSBharata B Rao     PowerPCCPU *cpu = POWERPC_CPU(cs);
5930da6f3feSBharata B Rao     CPUPPCState *env = &cpu->env;
5940da6f3feSBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
5950da6f3feSBharata B Rao     int index = ppc_get_vcpu_dt_id(cpu);
5960da6f3feSBharata B Rao     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
5970da6f3feSBharata B Rao                        0xffffffff, 0xffffffff};
5980da6f3feSBharata B Rao     uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
5990da6f3feSBharata B Rao     uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
6000da6f3feSBharata B Rao     uint32_t page_sizes_prop[64];
6010da6f3feSBharata B Rao     size_t page_sizes_prop_size;
60222419c2aSDavid Gibson     uint32_t vcpus_per_socket = smp_threads * smp_cores;
6030da6f3feSBharata B Rao     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
6040da6f3feSBharata B Rao 
60590da0d5aSBenjamin Herrenschmidt     /* Note: we keep CI large pages off for now because a 64K capable guest
60690da0d5aSBenjamin Herrenschmidt      * provisioned with large pages might otherwise try to map a qemu
60790da0d5aSBenjamin Herrenschmidt      * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
60890da0d5aSBenjamin Herrenschmidt      * even if that qemu runs on a 4k host.
60990da0d5aSBenjamin Herrenschmidt      *
61090da0d5aSBenjamin Herrenschmidt      * We can later add this bit back when we are confident this is not
61190da0d5aSBenjamin Herrenschmidt      * an issue (!HV KVM or 64K host)
61290da0d5aSBenjamin Herrenschmidt      */
61390da0d5aSBenjamin Herrenschmidt     uint8_t pa_features_206[] = { 6, 0,
61490da0d5aSBenjamin Herrenschmidt         0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
61590da0d5aSBenjamin Herrenschmidt     uint8_t pa_features_207[] = { 24, 0,
61690da0d5aSBenjamin Herrenschmidt         0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
61790da0d5aSBenjamin Herrenschmidt         0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
61890da0d5aSBenjamin Herrenschmidt         0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
61990da0d5aSBenjamin Herrenschmidt         0x80, 0x00, 0x80, 0x00, 0x80, 0x00 };
62090da0d5aSBenjamin Herrenschmidt     uint8_t *pa_features;
62190da0d5aSBenjamin Herrenschmidt     size_t pa_size;
62290da0d5aSBenjamin Herrenschmidt 
6230da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
6240da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
6250da6f3feSBharata B Rao 
6260da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
6270da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
6280da6f3feSBharata B Rao                            env->dcache_line_size)));
6290da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
6300da6f3feSBharata B Rao                            env->dcache_line_size)));
6310da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
6320da6f3feSBharata B Rao                            env->icache_line_size)));
6330da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
6340da6f3feSBharata B Rao                            env->icache_line_size)));
6350da6f3feSBharata B Rao 
6360da6f3feSBharata B Rao     if (pcc->l1_dcache_size) {
6370da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
6380da6f3feSBharata B Rao                                pcc->l1_dcache_size)));
6390da6f3feSBharata B Rao     } else {
6400da6f3feSBharata B Rao         fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");
6410da6f3feSBharata B Rao     }
6420da6f3feSBharata B Rao     if (pcc->l1_icache_size) {
6430da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
6440da6f3feSBharata B Rao                                pcc->l1_icache_size)));
6450da6f3feSBharata B Rao     } else {
6460da6f3feSBharata B Rao         fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n");
6470da6f3feSBharata B Rao     }
6480da6f3feSBharata B Rao 
6490da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
6500da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
651fd5da5c4SThomas Huth     _FDT((fdt_setprop_cell(fdt, offset, "slb-size", env->slb_nr)));
6520da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr)));
6530da6f3feSBharata B Rao     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
6540da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
6550da6f3feSBharata B Rao 
6560da6f3feSBharata B Rao     if (env->spr_cb[SPR_PURR].oea_read) {
6570da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
6580da6f3feSBharata B Rao     }
6590da6f3feSBharata B Rao 
6600da6f3feSBharata B Rao     if (env->mmu_model & POWERPC_MMU_1TSEG) {
6610da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
6620da6f3feSBharata B Rao                           segs, sizeof(segs))));
6630da6f3feSBharata B Rao     }
6640da6f3feSBharata B Rao 
6650da6f3feSBharata B Rao     /* Advertise VMX/VSX (vector extensions) if available
6660da6f3feSBharata B Rao      *   0 / no property == no vector extensions
6670da6f3feSBharata B Rao      *   1               == VMX / Altivec available
6680da6f3feSBharata B Rao      *   2               == VSX available */
6690da6f3feSBharata B Rao     if (env->insns_flags & PPC_ALTIVEC) {
6700da6f3feSBharata B Rao         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
6710da6f3feSBharata B Rao 
6720da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
6730da6f3feSBharata B Rao     }
6740da6f3feSBharata B Rao 
6750da6f3feSBharata B Rao     /* Advertise DFP (Decimal Floating Point) if available
6760da6f3feSBharata B Rao      *   0 / no property == no DFP
6770da6f3feSBharata B Rao      *   1               == DFP available */
6780da6f3feSBharata B Rao     if (env->insns_flags2 & PPC2_DFP) {
6790da6f3feSBharata B Rao         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
6800da6f3feSBharata B Rao     }
6810da6f3feSBharata B Rao 
6820da6f3feSBharata B Rao     page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
6830da6f3feSBharata B Rao                                                   sizeof(page_sizes_prop));
6840da6f3feSBharata B Rao     if (page_sizes_prop_size) {
6850da6f3feSBharata B Rao         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
6860da6f3feSBharata B Rao                           page_sizes_prop, page_sizes_prop_size)));
6870da6f3feSBharata B Rao     }
6880da6f3feSBharata B Rao 
68990da0d5aSBenjamin Herrenschmidt     /* Do the ibm,pa-features property, adjust it for ci-large-pages */
69090da0d5aSBenjamin Herrenschmidt     if (env->mmu_model == POWERPC_MMU_2_06) {
69190da0d5aSBenjamin Herrenschmidt         pa_features = pa_features_206;
69290da0d5aSBenjamin Herrenschmidt         pa_size = sizeof(pa_features_206);
69390da0d5aSBenjamin Herrenschmidt     } else /* env->mmu_model == POWERPC_MMU_2_07 */ {
69490da0d5aSBenjamin Herrenschmidt         pa_features = pa_features_207;
69590da0d5aSBenjamin Herrenschmidt         pa_size = sizeof(pa_features_207);
69690da0d5aSBenjamin Herrenschmidt     }
69790da0d5aSBenjamin Herrenschmidt     if (env->ci_large_pages) {
69890da0d5aSBenjamin Herrenschmidt         pa_features[3] |= 0x20;
69990da0d5aSBenjamin Herrenschmidt     }
70090da0d5aSBenjamin Herrenschmidt     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
70190da0d5aSBenjamin Herrenschmidt 
7020da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
70322419c2aSDavid Gibson                            cs->cpu_index / vcpus_per_socket)));
7040da6f3feSBharata B Rao 
7050da6f3feSBharata B Rao     _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
7060da6f3feSBharata B Rao                       pft_size_prop, sizeof(pft_size_prop))));
7070da6f3feSBharata B Rao 
7080da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs));
7090da6f3feSBharata B Rao 
7100da6f3feSBharata B Rao     _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
7110da6f3feSBharata B Rao                                 ppc_get_compat_smt_threads(cpu)));
7120da6f3feSBharata B Rao }
7130da6f3feSBharata B Rao 
7140da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
7150da6f3feSBharata B Rao {
7160da6f3feSBharata B Rao     CPUState *cs;
7170da6f3feSBharata B Rao     int cpus_offset;
7180da6f3feSBharata B Rao     char *nodename;
7190da6f3feSBharata B Rao     int smt = kvmppc_smt_threads();
7200da6f3feSBharata B Rao 
7210da6f3feSBharata B Rao     cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
7220da6f3feSBharata B Rao     _FDT(cpus_offset);
7230da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
7240da6f3feSBharata B Rao     _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
7250da6f3feSBharata B Rao 
7260da6f3feSBharata B Rao     /*
7270da6f3feSBharata B Rao      * We walk the CPUs in reverse order to ensure that CPU DT nodes
7280da6f3feSBharata B Rao      * created by fdt_add_subnode() end up in the right order in FDT
7290da6f3feSBharata B Rao      * for the guest kernel the enumerate the CPUs correctly.
7300da6f3feSBharata B Rao      */
7310da6f3feSBharata B Rao     CPU_FOREACH_REVERSE(cs) {
7320da6f3feSBharata B Rao         PowerPCCPU *cpu = POWERPC_CPU(cs);
7330da6f3feSBharata B Rao         int index = ppc_get_vcpu_dt_id(cpu);
7340da6f3feSBharata B Rao         DeviceClass *dc = DEVICE_GET_CLASS(cs);
7350da6f3feSBharata B Rao         int offset;
7360da6f3feSBharata B Rao 
7370da6f3feSBharata B Rao         if ((index % smt) != 0) {
7380da6f3feSBharata B Rao             continue;
7390da6f3feSBharata B Rao         }
7400da6f3feSBharata B Rao 
7410da6f3feSBharata B Rao         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
7420da6f3feSBharata B Rao         offset = fdt_add_subnode(fdt, cpus_offset, nodename);
7430da6f3feSBharata B Rao         g_free(nodename);
7440da6f3feSBharata B Rao         _FDT(offset);
7450da6f3feSBharata B Rao         spapr_populate_cpu_dt(cs, fdt, offset, spapr);
7460da6f3feSBharata B Rao     }
7470da6f3feSBharata B Rao 
7480da6f3feSBharata B Rao }
7490da6f3feSBharata B Rao 
75003d196b7SBharata B Rao /*
75103d196b7SBharata B Rao  * Adds ibm,dynamic-reconfiguration-memory node.
75203d196b7SBharata B Rao  * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
75303d196b7SBharata B Rao  * of this device tree node.
75403d196b7SBharata B Rao  */
75503d196b7SBharata B Rao static int spapr_populate_drconf_memory(sPAPRMachineState *spapr, void *fdt)
75603d196b7SBharata B Rao {
75703d196b7SBharata B Rao     MachineState *machine = MACHINE(spapr);
75803d196b7SBharata B Rao     int ret, i, offset;
75903d196b7SBharata B Rao     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
76003d196b7SBharata B Rao     uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
761e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
76203d196b7SBharata B Rao     uint32_t *int_buf, *cur_index, buf_len;
7636663864eSBharata B Rao     int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
76403d196b7SBharata B Rao 
765ef001f06SThomas Huth     /*
766ef001f06SThomas Huth      * Allocate enough buffer size to fit in ibm,dynamic-memory
767ef001f06SThomas Huth      * or ibm,associativity-lookup-arrays
768ef001f06SThomas Huth      */
769ef001f06SThomas Huth     buf_len = MAX(nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1, nr_nodes * 4 + 2)
770ef001f06SThomas Huth               * sizeof(uint32_t);
77103d196b7SBharata B Rao     cur_index = int_buf = g_malloc0(buf_len);
77203d196b7SBharata B Rao 
77303d196b7SBharata B Rao     offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
77403d196b7SBharata B Rao 
77503d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
77603d196b7SBharata B Rao                     sizeof(prop_lmb_size));
77703d196b7SBharata B Rao     if (ret < 0) {
77803d196b7SBharata B Rao         goto out;
77903d196b7SBharata B Rao     }
78003d196b7SBharata B Rao 
78103d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
78203d196b7SBharata B Rao     if (ret < 0) {
78303d196b7SBharata B Rao         goto out;
78403d196b7SBharata B Rao     }
78503d196b7SBharata B Rao 
78603d196b7SBharata B Rao     ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
78703d196b7SBharata B Rao     if (ret < 0) {
78803d196b7SBharata B Rao         goto out;
78903d196b7SBharata B Rao     }
79003d196b7SBharata B Rao 
79103d196b7SBharata B Rao     /* ibm,dynamic-memory */
79203d196b7SBharata B Rao     int_buf[0] = cpu_to_be32(nr_lmbs);
79303d196b7SBharata B Rao     cur_index++;
79403d196b7SBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
79503d196b7SBharata B Rao         sPAPRDRConnector *drc;
79603d196b7SBharata B Rao         sPAPRDRConnectorClass *drck;
797e8f986fcSBharata B Rao         uint64_t addr = i * lmb_size + spapr->hotplug_memory.base;;
79803d196b7SBharata B Rao         uint32_t *dynamic_memory = cur_index;
79903d196b7SBharata B Rao 
80003d196b7SBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
80103d196b7SBharata B Rao                                        addr/lmb_size);
80203d196b7SBharata B Rao         g_assert(drc);
80303d196b7SBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
80403d196b7SBharata B Rao 
80503d196b7SBharata B Rao         dynamic_memory[0] = cpu_to_be32(addr >> 32);
80603d196b7SBharata B Rao         dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
80703d196b7SBharata B Rao         dynamic_memory[2] = cpu_to_be32(drck->get_index(drc));
80803d196b7SBharata B Rao         dynamic_memory[3] = cpu_to_be32(0); /* reserved */
80903d196b7SBharata B Rao         dynamic_memory[4] = cpu_to_be32(numa_get_node(addr, NULL));
81003d196b7SBharata B Rao         if (addr < machine->ram_size ||
81103d196b7SBharata B Rao                     memory_region_present(get_system_memory(), addr)) {
81203d196b7SBharata B Rao             dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
81303d196b7SBharata B Rao         } else {
81403d196b7SBharata B Rao             dynamic_memory[5] = cpu_to_be32(0);
81503d196b7SBharata B Rao         }
81603d196b7SBharata B Rao 
81703d196b7SBharata B Rao         cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
81803d196b7SBharata B Rao     }
81903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
82003d196b7SBharata B Rao     if (ret < 0) {
82103d196b7SBharata B Rao         goto out;
82203d196b7SBharata B Rao     }
82303d196b7SBharata B Rao 
82403d196b7SBharata B Rao     /* ibm,associativity-lookup-arrays */
82503d196b7SBharata B Rao     cur_index = int_buf;
8266663864eSBharata B Rao     int_buf[0] = cpu_to_be32(nr_nodes);
82703d196b7SBharata B Rao     int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
82803d196b7SBharata B Rao     cur_index += 2;
8296663864eSBharata B Rao     for (i = 0; i < nr_nodes; i++) {
83003d196b7SBharata B Rao         uint32_t associativity[] = {
83103d196b7SBharata B Rao             cpu_to_be32(0x0),
83203d196b7SBharata B Rao             cpu_to_be32(0x0),
83303d196b7SBharata B Rao             cpu_to_be32(0x0),
83403d196b7SBharata B Rao             cpu_to_be32(i)
83503d196b7SBharata B Rao         };
83603d196b7SBharata B Rao         memcpy(cur_index, associativity, sizeof(associativity));
83703d196b7SBharata B Rao         cur_index += 4;
83803d196b7SBharata B Rao     }
83903d196b7SBharata B Rao     ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
84003d196b7SBharata B Rao             (cur_index - int_buf) * sizeof(uint32_t));
84103d196b7SBharata B Rao out:
84203d196b7SBharata B Rao     g_free(int_buf);
84303d196b7SBharata B Rao     return ret;
84403d196b7SBharata B Rao }
84503d196b7SBharata B Rao 
84603d196b7SBharata B Rao int spapr_h_cas_compose_response(sPAPRMachineState *spapr,
84703d196b7SBharata B Rao                                  target_ulong addr, target_ulong size,
84803d196b7SBharata B Rao                                  bool cpu_update, bool memory_update)
84903d196b7SBharata B Rao {
85003d196b7SBharata B Rao     void *fdt, *fdt_skel;
85103d196b7SBharata B Rao     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
85203d196b7SBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
85303d196b7SBharata B Rao 
85403d196b7SBharata B Rao     size -= sizeof(hdr);
85503d196b7SBharata B Rao 
85603d196b7SBharata B Rao     /* Create sceleton */
85703d196b7SBharata B Rao     fdt_skel = g_malloc0(size);
85803d196b7SBharata B Rao     _FDT((fdt_create(fdt_skel, size)));
85903d196b7SBharata B Rao     _FDT((fdt_begin_node(fdt_skel, "")));
86003d196b7SBharata B Rao     _FDT((fdt_end_node(fdt_skel)));
86103d196b7SBharata B Rao     _FDT((fdt_finish(fdt_skel)));
86203d196b7SBharata B Rao     fdt = g_malloc0(size);
86303d196b7SBharata B Rao     _FDT((fdt_open_into(fdt_skel, fdt, size)));
86403d196b7SBharata B Rao     g_free(fdt_skel);
86503d196b7SBharata B Rao 
86603d196b7SBharata B Rao     /* Fixup cpu nodes */
86703d196b7SBharata B Rao     if (cpu_update) {
86803d196b7SBharata B Rao         _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
86903d196b7SBharata B Rao     }
87003d196b7SBharata B Rao 
87103d196b7SBharata B Rao     /* Generate memory nodes or ibm,dynamic-reconfiguration-memory node */
87203d196b7SBharata B Rao     if (memory_update && smc->dr_lmb_enabled) {
87303d196b7SBharata B Rao         _FDT((spapr_populate_drconf_memory(spapr, fdt)));
87403d196b7SBharata B Rao     }
87503d196b7SBharata B Rao 
87603d196b7SBharata B Rao     /* Pack resulting tree */
87703d196b7SBharata B Rao     _FDT((fdt_pack(fdt)));
87803d196b7SBharata B Rao 
87903d196b7SBharata B Rao     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
88003d196b7SBharata B Rao         trace_spapr_cas_failed(size);
88103d196b7SBharata B Rao         return -1;
88203d196b7SBharata B Rao     }
88303d196b7SBharata B Rao 
88403d196b7SBharata B Rao     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
88503d196b7SBharata B Rao     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
88603d196b7SBharata B Rao     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
88703d196b7SBharata B Rao     g_free(fdt);
88803d196b7SBharata B Rao 
88903d196b7SBharata B Rao     return 0;
89003d196b7SBharata B Rao }
89103d196b7SBharata B Rao 
89228e02042SDavid Gibson static void spapr_finalize_fdt(sPAPRMachineState *spapr,
89353018216SPaolo Bonzini                                hwaddr fdt_addr,
89453018216SPaolo Bonzini                                hwaddr rtas_addr,
89553018216SPaolo Bonzini                                hwaddr rtas_size)
89653018216SPaolo Bonzini {
8975b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
898c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
8995b2128d2SAlexander Graf     const char *boot_device = machine->boot_order;
90071461b0fSAlexey Kardashevskiy     int ret, i;
90171461b0fSAlexey Kardashevskiy     size_t cb = 0;
90271461b0fSAlexey Kardashevskiy     char *bootlist;
90353018216SPaolo Bonzini     void *fdt;
90453018216SPaolo Bonzini     sPAPRPHBState *phb;
90553018216SPaolo Bonzini 
90653018216SPaolo Bonzini     fdt = g_malloc(FDT_MAX_SIZE);
90753018216SPaolo Bonzini 
90853018216SPaolo Bonzini     /* open out the base tree into a temp buffer for the final tweaks */
90953018216SPaolo Bonzini     _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
91053018216SPaolo Bonzini 
911e8f986fcSBharata B Rao     ret = spapr_populate_memory(spapr, fdt);
912e8f986fcSBharata B Rao     if (ret < 0) {
913e8f986fcSBharata B Rao         fprintf(stderr, "couldn't setup memory nodes in fdt\n");
914e8f986fcSBharata B Rao         exit(1);
91553018216SPaolo Bonzini     }
91653018216SPaolo Bonzini 
91753018216SPaolo Bonzini     ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
91853018216SPaolo Bonzini     if (ret < 0) {
91953018216SPaolo Bonzini         fprintf(stderr, "couldn't setup vio devices in fdt\n");
92053018216SPaolo Bonzini         exit(1);
92153018216SPaolo Bonzini     }
92253018216SPaolo Bonzini 
9234d9392beSThomas Huth     if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
9244d9392beSThomas Huth         ret = spapr_rng_populate_dt(fdt);
9254d9392beSThomas Huth         if (ret < 0) {
9264d9392beSThomas Huth             fprintf(stderr, "could not set up rng device in the fdt\n");
9274d9392beSThomas Huth             exit(1);
9284d9392beSThomas Huth         }
9294d9392beSThomas Huth     }
9304d9392beSThomas Huth 
93153018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
93253018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
93353018216SPaolo Bonzini     }
93453018216SPaolo Bonzini 
93553018216SPaolo Bonzini     if (ret < 0) {
93653018216SPaolo Bonzini         fprintf(stderr, "couldn't setup PCI devices in fdt\n");
93753018216SPaolo Bonzini         exit(1);
93853018216SPaolo Bonzini     }
93953018216SPaolo Bonzini 
94053018216SPaolo Bonzini     /* RTAS */
94153018216SPaolo Bonzini     ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
94253018216SPaolo Bonzini     if (ret < 0) {
94353018216SPaolo Bonzini         fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
94453018216SPaolo Bonzini     }
94553018216SPaolo Bonzini 
9460da6f3feSBharata B Rao     /* cpus */
9470da6f3feSBharata B Rao     spapr_populate_cpus_dt_node(fdt, spapr);
94853018216SPaolo Bonzini 
94971461b0fSAlexey Kardashevskiy     bootlist = get_boot_devices_list(&cb, true);
95071461b0fSAlexey Kardashevskiy     if (cb && bootlist) {
95171461b0fSAlexey Kardashevskiy         int offset = fdt_path_offset(fdt, "/chosen");
95271461b0fSAlexey Kardashevskiy         if (offset < 0) {
95371461b0fSAlexey Kardashevskiy             exit(1);
95471461b0fSAlexey Kardashevskiy         }
95571461b0fSAlexey Kardashevskiy         for (i = 0; i < cb; i++) {
95671461b0fSAlexey Kardashevskiy             if (bootlist[i] == '\n') {
95771461b0fSAlexey Kardashevskiy                 bootlist[i] = ' ';
95871461b0fSAlexey Kardashevskiy             }
95971461b0fSAlexey Kardashevskiy 
96071461b0fSAlexey Kardashevskiy         }
96171461b0fSAlexey Kardashevskiy         ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist);
96271461b0fSAlexey Kardashevskiy     }
96371461b0fSAlexey Kardashevskiy 
9645b2128d2SAlexander Graf     if (boot_device && strlen(boot_device)) {
9655b2128d2SAlexander Graf         int offset = fdt_path_offset(fdt, "/chosen");
9665b2128d2SAlexander Graf 
9675b2128d2SAlexander Graf         if (offset < 0) {
9685b2128d2SAlexander Graf             exit(1);
9695b2128d2SAlexander Graf         }
9705b2128d2SAlexander Graf         fdt_setprop_string(fdt, offset, "qemu,boot-device", boot_device);
9715b2128d2SAlexander Graf     }
9725b2128d2SAlexander Graf 
97353018216SPaolo Bonzini     if (!spapr->has_graphics) {
97453018216SPaolo Bonzini         spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
97553018216SPaolo Bonzini     }
97653018216SPaolo Bonzini 
977c20d332aSBharata B Rao     if (smc->dr_lmb_enabled) {
978c20d332aSBharata B Rao         _FDT(spapr_drc_populate_dt(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
979c20d332aSBharata B Rao     }
980c20d332aSBharata B Rao 
98153018216SPaolo Bonzini     _FDT((fdt_pack(fdt)));
98253018216SPaolo Bonzini 
98353018216SPaolo Bonzini     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
984730fce59SThomas Huth         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
98553018216SPaolo Bonzini                      fdt_totalsize(fdt), FDT_MAX_SIZE);
98653018216SPaolo Bonzini         exit(1);
98753018216SPaolo Bonzini     }
98853018216SPaolo Bonzini 
989ad440b4aSAndrew Jones     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
99053018216SPaolo Bonzini     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
99153018216SPaolo Bonzini 
992a21a7a70SGonglei     g_free(bootlist);
99353018216SPaolo Bonzini     g_free(fdt);
99453018216SPaolo Bonzini }
99553018216SPaolo Bonzini 
99653018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
99753018216SPaolo Bonzini {
99853018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
99953018216SPaolo Bonzini }
100053018216SPaolo Bonzini 
100153018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu)
100253018216SPaolo Bonzini {
100353018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
100453018216SPaolo Bonzini 
100553018216SPaolo Bonzini     if (msr_pr) {
100653018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
100753018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
100853018216SPaolo Bonzini     } else {
100953018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
101053018216SPaolo Bonzini     }
101153018216SPaolo Bonzini }
101253018216SPaolo Bonzini 
1013e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1014e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1015e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1016e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1017e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1018e6b8fd24SSamuel Mendoza-Jonas 
1019b817772aSBharata B Rao static void spapr_alloc_htab(sPAPRMachineState *spapr)
102053018216SPaolo Bonzini {
102153018216SPaolo Bonzini     long shift;
1022e6b8fd24SSamuel Mendoza-Jonas     int index;
102353018216SPaolo Bonzini 
102453018216SPaolo Bonzini     /* allocate hash page table.  For now we always make this 16mb,
102553018216SPaolo Bonzini      * later we should probably make it scale to the size of guest
102653018216SPaolo Bonzini      * RAM */
102753018216SPaolo Bonzini 
102853018216SPaolo Bonzini     shift = kvmppc_reset_htab(spapr->htab_shift);
1029b41d320fSBharata B Rao     if (shift < 0) {
1030b41d320fSBharata B Rao         /*
1031b41d320fSBharata B Rao          * For HV KVM, host kernel will return -ENOMEM when requested
1032b41d320fSBharata B Rao          * HTAB size can't be allocated.
1033b41d320fSBharata B Rao          */
1034b41d320fSBharata B Rao         error_setg(&error_abort, "Failed to allocate HTAB of requested size, try with smaller maxmem");
1035b41d320fSBharata B Rao     } else if (shift > 0) {
1036b41d320fSBharata B Rao         /*
1037b41d320fSBharata B Rao          * Kernel handles htab, we don't need to allocate one
1038b41d320fSBharata B Rao          *
1039b41d320fSBharata B Rao          * Older kernels can fall back to lower HTAB shift values,
1040b41d320fSBharata B Rao          * but we don't allow booting of such guests.
1041b41d320fSBharata B Rao          */
10427735fedaSBharata B Rao         if (shift != spapr->htab_shift) {
10437735fedaSBharata B Rao             error_setg(&error_abort, "Failed to allocate HTAB of requested size, try with smaller maxmem");
10447735fedaSBharata B Rao         }
10457735fedaSBharata B Rao 
104653018216SPaolo Bonzini         spapr->htab_shift = shift;
10477c43bca0SAneesh Kumar K.V         kvmppc_kern_htab = true;
1048b817772aSBharata B Rao     } else {
1049b817772aSBharata B Rao         /* Allocate htab */
1050b817772aSBharata B Rao         spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr));
105101a57972SSamuel Mendoza-Jonas 
1052b817772aSBharata B Rao         /* And clear it */
1053b817772aSBharata B Rao         memset(spapr->htab, 0, HTAB_SIZE(spapr));
1054b817772aSBharata B Rao 
1055b817772aSBharata B Rao         for (index = 0; index < HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; index++) {
1056b817772aSBharata B Rao             DIRTY_HPTE(HPTE(spapr->htab, index));
1057b817772aSBharata B Rao         }
1058b817772aSBharata B Rao     }
1059b817772aSBharata B Rao }
1060b817772aSBharata B Rao 
1061b817772aSBharata B Rao /*
1062b817772aSBharata B Rao  * Clear HTAB entries during reset.
1063b817772aSBharata B Rao  *
1064b817772aSBharata B Rao  * If host kernel has allocated HTAB, KVM_PPC_ALLOCATE_HTAB ioctl is
1065b817772aSBharata B Rao  * used to clear HTAB. Otherwise QEMU-allocated HTAB is cleared manually.
1066b817772aSBharata B Rao  */
1067b817772aSBharata B Rao static void spapr_reset_htab(sPAPRMachineState *spapr)
1068b817772aSBharata B Rao {
1069b817772aSBharata B Rao     long shift;
1070b817772aSBharata B Rao     int index;
1071b817772aSBharata B Rao 
1072b817772aSBharata B Rao     shift = kvmppc_reset_htab(spapr->htab_shift);
1073b41d320fSBharata B Rao     if (shift < 0) {
1074b41d320fSBharata B Rao         error_setg(&error_abort, "Failed to reset HTAB");
1075b41d320fSBharata B Rao     } else if (shift > 0) {
10767735fedaSBharata B Rao         if (shift != spapr->htab_shift) {
10777735fedaSBharata B Rao             error_setg(&error_abort, "Requested HTAB allocation failed during reset");
10787735fedaSBharata B Rao         }
10797735fedaSBharata B Rao 
108001a57972SSamuel Mendoza-Jonas         /* Tell readers to update their file descriptor */
108101a57972SSamuel Mendoza-Jonas         if (spapr->htab_fd >= 0) {
108201a57972SSamuel Mendoza-Jonas             spapr->htab_fd_stale = true;
108301a57972SSamuel Mendoza-Jonas         }
108453018216SPaolo Bonzini     } else {
108553018216SPaolo Bonzini         memset(spapr->htab, 0, HTAB_SIZE(spapr));
1086e6b8fd24SSamuel Mendoza-Jonas 
1087e6b8fd24SSamuel Mendoza-Jonas         for (index = 0; index < HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; index++) {
1088e6b8fd24SSamuel Mendoza-Jonas             DIRTY_HPTE(HPTE(spapr->htab, index));
1089e6b8fd24SSamuel Mendoza-Jonas         }
109053018216SPaolo Bonzini     }
109153018216SPaolo Bonzini 
109253018216SPaolo Bonzini     /* Update the RMA size if necessary */
109353018216SPaolo Bonzini     if (spapr->vrma_adjust) {
1094b082d65aSAlexey Kardashevskiy         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
1095b082d65aSAlexey Kardashevskiy                                           spapr->htab_shift);
109653018216SPaolo Bonzini     }
109753018216SPaolo Bonzini }
109853018216SPaolo Bonzini 
10999e3f9733SAlexander Graf static int find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
11009e3f9733SAlexander Graf {
11019e3f9733SAlexander Graf     bool matched = false;
11029e3f9733SAlexander Graf 
11039e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
11049e3f9733SAlexander Graf         matched = true;
11059e3f9733SAlexander Graf     }
11069e3f9733SAlexander Graf 
11079e3f9733SAlexander Graf     if (!matched) {
11089e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
11099e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
11109e3f9733SAlexander Graf         exit(1);
11119e3f9733SAlexander Graf     }
11129e3f9733SAlexander Graf 
11139e3f9733SAlexander Graf     return 0;
11149e3f9733SAlexander Graf }
11159e3f9733SAlexander Graf 
111601a57972SSamuel Mendoza-Jonas /*
111701a57972SSamuel Mendoza-Jonas  * A guest reset will cause spapr->htab_fd to become stale if being used.
111801a57972SSamuel Mendoza-Jonas  * Reopen the file descriptor to make sure the whole HTAB is properly read.
111901a57972SSamuel Mendoza-Jonas  */
112028e02042SDavid Gibson static int spapr_check_htab_fd(sPAPRMachineState *spapr)
112101a57972SSamuel Mendoza-Jonas {
112201a57972SSamuel Mendoza-Jonas     int rc = 0;
112301a57972SSamuel Mendoza-Jonas 
112401a57972SSamuel Mendoza-Jonas     if (spapr->htab_fd_stale) {
112501a57972SSamuel Mendoza-Jonas         close(spapr->htab_fd);
112601a57972SSamuel Mendoza-Jonas         spapr->htab_fd = kvmppc_get_htab_fd(false);
112701a57972SSamuel Mendoza-Jonas         if (spapr->htab_fd < 0) {
112801a57972SSamuel Mendoza-Jonas             error_report("Unable to open fd for reading hash table from KVM: "
112901a57972SSamuel Mendoza-Jonas                          "%s", strerror(errno));
113001a57972SSamuel Mendoza-Jonas             rc = -1;
113101a57972SSamuel Mendoza-Jonas         }
113201a57972SSamuel Mendoza-Jonas         spapr->htab_fd_stale = false;
113301a57972SSamuel Mendoza-Jonas     }
113401a57972SSamuel Mendoza-Jonas 
113501a57972SSamuel Mendoza-Jonas     return rc;
113601a57972SSamuel Mendoza-Jonas }
113701a57972SSamuel Mendoza-Jonas 
113853018216SPaolo Bonzini static void ppc_spapr_reset(void)
113953018216SPaolo Bonzini {
114028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
1141182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
1142b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
1143259186a7SAndreas Färber 
11449e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
11459e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
11469e3f9733SAlexander Graf 
114753018216SPaolo Bonzini     /* Reset the hash table & recalc the RMA */
114853018216SPaolo Bonzini     spapr_reset_htab(spapr);
114953018216SPaolo Bonzini 
115053018216SPaolo Bonzini     qemu_devices_reset();
115153018216SPaolo Bonzini 
1152b7d1f77aSBenjamin Herrenschmidt     /*
1153b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
1154b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
1155b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
1156b7d1f77aSBenjamin Herrenschmidt      */
1157b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
1158b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1159b7d1f77aSBenjamin Herrenschmidt     spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
1160b7d1f77aSBenjamin Herrenschmidt 
116153018216SPaolo Bonzini     /* Load the fdt */
116253018216SPaolo Bonzini     spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
116353018216SPaolo Bonzini                        spapr->rtas_size);
116453018216SPaolo Bonzini 
1165b7d1f77aSBenjamin Herrenschmidt     /* Copy RTAS over */
1166b7d1f77aSBenjamin Herrenschmidt     cpu_physical_memory_write(spapr->rtas_addr, spapr->rtas_blob,
1167b7d1f77aSBenjamin Herrenschmidt                               spapr->rtas_size);
1168b7d1f77aSBenjamin Herrenschmidt 
116953018216SPaolo Bonzini     /* Set up the entry state */
1170182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
1171182735efSAndreas Färber     first_ppc_cpu->env.gpr[3] = spapr->fdt_addr;
1172182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
1173182735efSAndreas Färber     first_cpu->halted = 0;
11741b718907SDavid Gibson     first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT;
117553018216SPaolo Bonzini 
117653018216SPaolo Bonzini }
117753018216SPaolo Bonzini 
117853018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque)
117953018216SPaolo Bonzini {
118028e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
118153018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
1182259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
118353018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
118453018216SPaolo Bonzini 
1185259186a7SAndreas Färber     cpu_reset(cs);
118653018216SPaolo Bonzini 
118753018216SPaolo Bonzini     /* All CPUs start halted.  CPU0 is unhalted from the machine level
118853018216SPaolo Bonzini      * reset code and the rest are explicitly started up by the guest
118953018216SPaolo Bonzini      * using an RTAS call */
1190259186a7SAndreas Färber     cs->halted = 1;
119153018216SPaolo Bonzini 
119253018216SPaolo Bonzini     env->spr[SPR_HIOR] = 0;
119353018216SPaolo Bonzini 
11944be21d56SDavid Gibson     env->external_htab = (uint8_t *)spapr->htab;
11955736245cSAneesh Kumar K.V     if (kvm_enabled() && !env->external_htab) {
11965736245cSAneesh Kumar K.V         /*
11975736245cSAneesh Kumar K.V          * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte*
11985736245cSAneesh Kumar K.V          * functions do the right thing.
11995736245cSAneesh Kumar K.V          */
12005736245cSAneesh Kumar K.V         env->external_htab = (void *)1;
12015736245cSAneesh Kumar K.V     }
120253018216SPaolo Bonzini     env->htab_base = -1;
1203f3c75d42SAneesh Kumar K.V     /*
1204f3c75d42SAneesh Kumar K.V      * htab_mask is the mask used to normalize hash value to PTEG index.
1205f3c75d42SAneesh Kumar K.V      * htab_shift is log2 of hash table size.
1206f3c75d42SAneesh Kumar K.V      * We have 8 hpte per group, and each hpte is 16 bytes.
1207f3c75d42SAneesh Kumar K.V      * ie have 128 bytes per hpte entry.
1208f3c75d42SAneesh Kumar K.V      */
120928e02042SDavid Gibson     env->htab_mask = (1ULL << (spapr->htab_shift - 7)) - 1;
1210ec4936e1SStefan Weil     env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
121153018216SPaolo Bonzini         (spapr->htab_shift - 18);
121253018216SPaolo Bonzini }
121353018216SPaolo Bonzini 
121428e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr)
121553018216SPaolo Bonzini {
12162ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
12173978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
121853018216SPaolo Bonzini 
12193978b863SPaolo Bonzini     if (dinfo) {
12206231a6daSMarkus Armbruster         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
12216231a6daSMarkus Armbruster                             &error_fatal);
122253018216SPaolo Bonzini     }
122353018216SPaolo Bonzini 
122453018216SPaolo Bonzini     qdev_init_nofail(dev);
122553018216SPaolo Bonzini 
122653018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
122753018216SPaolo Bonzini }
122853018216SPaolo Bonzini 
122928e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr)
123028df36a1SDavid Gibson {
123128df36a1SDavid Gibson     DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
123228df36a1SDavid Gibson 
123328df36a1SDavid Gibson     qdev_init_nofail(dev);
123428df36a1SDavid Gibson     spapr->rtc = dev;
123574e5ae28SDavid Gibson 
123674e5ae28SDavid Gibson     object_property_add_alias(qdev_get_machine(), "rtc-time",
123774e5ae28SDavid Gibson                               OBJECT(spapr->rtc), "date", NULL);
123828df36a1SDavid Gibson }
123928df36a1SDavid Gibson 
124053018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
124153018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus)
124253018216SPaolo Bonzini {
124353018216SPaolo Bonzini     switch (vga_interface_type) {
124453018216SPaolo Bonzini     case VGA_NONE:
12457effdaa3SMark Wu         return false;
12467effdaa3SMark Wu     case VGA_DEVICE:
12477effdaa3SMark Wu         return true;
124853018216SPaolo Bonzini     case VGA_STD:
1249b798c190SBenjamin Herrenschmidt     case VGA_VIRTIO:
125053018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
125153018216SPaolo Bonzini     default:
125253018216SPaolo Bonzini         fprintf(stderr, "This vga model is not supported,"
125353018216SPaolo Bonzini                 "currently it only supports -vga std\n");
125453018216SPaolo Bonzini         exit(0);
125553018216SPaolo Bonzini     }
125653018216SPaolo Bonzini }
125753018216SPaolo Bonzini 
1258880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1259880ae7deSDavid Gibson {
126028e02042SDavid Gibson     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
1261880ae7deSDavid Gibson     int err = 0;
1262880ae7deSDavid Gibson 
1263631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1264880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1265880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1266880ae7deSDavid Gibson      * value into the RTC device */
1267880ae7deSDavid Gibson     if (version_id < 3) {
1268880ae7deSDavid Gibson         err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1269880ae7deSDavid Gibson     }
1270880ae7deSDavid Gibson 
1271880ae7deSDavid Gibson     return err;
1272880ae7deSDavid Gibson }
1273880ae7deSDavid Gibson 
1274880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1275880ae7deSDavid Gibson {
1276880ae7deSDavid Gibson     return version_id < 3;
1277880ae7deSDavid Gibson }
1278880ae7deSDavid Gibson 
12794be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
12804be21d56SDavid Gibson     .name = "spapr",
1281880ae7deSDavid Gibson     .version_id = 3,
12824be21d56SDavid Gibson     .minimum_version_id = 1,
1283880ae7deSDavid Gibson     .post_load = spapr_post_load,
12844be21d56SDavid Gibson     .fields = (VMStateField[]) {
1285880ae7deSDavid Gibson         /* used to be @next_irq */
1286880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
12874be21d56SDavid Gibson 
12884be21d56SDavid Gibson         /* RTC offset */
128928e02042SDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3),
1290880ae7deSDavid Gibson 
129128e02042SDavid Gibson         VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2),
12924be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
12934be21d56SDavid Gibson     },
12944be21d56SDavid Gibson };
12954be21d56SDavid Gibson 
12964be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
12974be21d56SDavid Gibson {
129828e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
12994be21d56SDavid Gibson 
13004be21d56SDavid Gibson     /* "Iteration" header */
13014be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
13024be21d56SDavid Gibson 
1303e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1304e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1305e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1306e68cb8b4SAlexey Kardashevskiy     } else {
1307e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1308e68cb8b4SAlexey Kardashevskiy 
1309e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = kvmppc_get_htab_fd(false);
131001a57972SSamuel Mendoza-Jonas         spapr->htab_fd_stale = false;
1311e68cb8b4SAlexey Kardashevskiy         if (spapr->htab_fd < 0) {
1312e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n",
1313e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1314e68cb8b4SAlexey Kardashevskiy             return -1;
1315e68cb8b4SAlexey Kardashevskiy         }
13164be21d56SDavid Gibson     }
13174be21d56SDavid Gibson 
1318e68cb8b4SAlexey Kardashevskiy 
1319e68cb8b4SAlexey Kardashevskiy     return 0;
1320e68cb8b4SAlexey Kardashevskiy }
13214be21d56SDavid Gibson 
132228e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr,
13234be21d56SDavid Gibson                                  int64_t max_ns)
13244be21d56SDavid Gibson {
13254be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
13264be21d56SDavid Gibson     int index = spapr->htab_save_index;
1327bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
13284be21d56SDavid Gibson 
13294be21d56SDavid Gibson     assert(spapr->htab_first_pass);
13304be21d56SDavid Gibson 
13314be21d56SDavid Gibson     do {
13324be21d56SDavid Gibson         int chunkstart;
13334be21d56SDavid Gibson 
13344be21d56SDavid Gibson         /* Consume invalid HPTEs */
13354be21d56SDavid Gibson         while ((index < htabslots)
13364be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
13374be21d56SDavid Gibson             index++;
13384be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13394be21d56SDavid Gibson         }
13404be21d56SDavid Gibson 
13414be21d56SDavid Gibson         /* Consume valid HPTEs */
13424be21d56SDavid Gibson         chunkstart = index;
1343338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
13444be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
13454be21d56SDavid Gibson             index++;
13464be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13474be21d56SDavid Gibson         }
13484be21d56SDavid Gibson 
13494be21d56SDavid Gibson         if (index > chunkstart) {
13504be21d56SDavid Gibson             int n_valid = index - chunkstart;
13514be21d56SDavid Gibson 
13524be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
13534be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
13544be21d56SDavid Gibson             qemu_put_be16(f, 0);
13554be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
13564be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
13574be21d56SDavid Gibson 
1358bc72ad67SAlex Bligh             if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
13594be21d56SDavid Gibson                 break;
13604be21d56SDavid Gibson             }
13614be21d56SDavid Gibson         }
13624be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
13634be21d56SDavid Gibson 
13644be21d56SDavid Gibson     if (index >= htabslots) {
13654be21d56SDavid Gibson         assert(index == htabslots);
13664be21d56SDavid Gibson         index = 0;
13674be21d56SDavid Gibson         spapr->htab_first_pass = false;
13684be21d56SDavid Gibson     }
13694be21d56SDavid Gibson     spapr->htab_save_index = index;
13704be21d56SDavid Gibson }
13714be21d56SDavid Gibson 
137228e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr,
13734be21d56SDavid Gibson                                 int64_t max_ns)
13744be21d56SDavid Gibson {
13754be21d56SDavid Gibson     bool final = max_ns < 0;
13764be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
13774be21d56SDavid Gibson     int examined = 0, sent = 0;
13784be21d56SDavid Gibson     int index = spapr->htab_save_index;
1379bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
13804be21d56SDavid Gibson 
13814be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
13824be21d56SDavid Gibson 
13834be21d56SDavid Gibson     do {
13844be21d56SDavid Gibson         int chunkstart, invalidstart;
13854be21d56SDavid Gibson 
13864be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
13874be21d56SDavid Gibson         while ((index < htabslots)
13884be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
13894be21d56SDavid Gibson             index++;
13904be21d56SDavid Gibson             examined++;
13914be21d56SDavid Gibson         }
13924be21d56SDavid Gibson 
13934be21d56SDavid Gibson         chunkstart = index;
13944be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1395338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
13964be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
13974be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
13984be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
13994be21d56SDavid Gibson             index++;
14004be21d56SDavid Gibson             examined++;
14014be21d56SDavid Gibson         }
14024be21d56SDavid Gibson 
14034be21d56SDavid Gibson         invalidstart = index;
14044be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1405338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
14064be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
14074be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
14084be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
14094be21d56SDavid Gibson             index++;
14104be21d56SDavid Gibson             examined++;
14114be21d56SDavid Gibson         }
14124be21d56SDavid Gibson 
14134be21d56SDavid Gibson         if (index > chunkstart) {
14144be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
14154be21d56SDavid Gibson             int n_invalid = index - invalidstart;
14164be21d56SDavid Gibson 
14174be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
14184be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
14194be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
14204be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
14214be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
14224be21d56SDavid Gibson             sent += index - chunkstart;
14234be21d56SDavid Gibson 
1424bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
14254be21d56SDavid Gibson                 break;
14264be21d56SDavid Gibson             }
14274be21d56SDavid Gibson         }
14284be21d56SDavid Gibson 
14294be21d56SDavid Gibson         if (examined >= htabslots) {
14304be21d56SDavid Gibson             break;
14314be21d56SDavid Gibson         }
14324be21d56SDavid Gibson 
14334be21d56SDavid Gibson         if (index >= htabslots) {
14344be21d56SDavid Gibson             assert(index == htabslots);
14354be21d56SDavid Gibson             index = 0;
14364be21d56SDavid Gibson         }
14374be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
14384be21d56SDavid Gibson 
14394be21d56SDavid Gibson     if (index >= htabslots) {
14404be21d56SDavid Gibson         assert(index == htabslots);
14414be21d56SDavid Gibson         index = 0;
14424be21d56SDavid Gibson     }
14434be21d56SDavid Gibson 
14444be21d56SDavid Gibson     spapr->htab_save_index = index;
14454be21d56SDavid Gibson 
1446e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
14474be21d56SDavid Gibson }
14484be21d56SDavid Gibson 
1449e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1450e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1451e68cb8b4SAlexey Kardashevskiy 
14524be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
14534be21d56SDavid Gibson {
145428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
1455e68cb8b4SAlexey Kardashevskiy     int rc = 0;
14564be21d56SDavid Gibson 
14574be21d56SDavid Gibson     /* Iteration header */
14584be21d56SDavid Gibson     qemu_put_be32(f, 0);
14594be21d56SDavid Gibson 
1460e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1461e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1462e68cb8b4SAlexey Kardashevskiy 
146301a57972SSamuel Mendoza-Jonas         rc = spapr_check_htab_fd(spapr);
146401a57972SSamuel Mendoza-Jonas         if (rc < 0) {
146501a57972SSamuel Mendoza-Jonas             return rc;
146601a57972SSamuel Mendoza-Jonas         }
146701a57972SSamuel Mendoza-Jonas 
1468e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd,
1469e68cb8b4SAlexey Kardashevskiy                               MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1470e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1471e68cb8b4SAlexey Kardashevskiy             return rc;
1472e68cb8b4SAlexey Kardashevskiy         }
1473e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
14744be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
14754be21d56SDavid Gibson     } else {
1476e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
14774be21d56SDavid Gibson     }
14784be21d56SDavid Gibson 
14794be21d56SDavid Gibson     /* End marker */
14804be21d56SDavid Gibson     qemu_put_be32(f, 0);
14814be21d56SDavid Gibson     qemu_put_be16(f, 0);
14824be21d56SDavid Gibson     qemu_put_be16(f, 0);
14834be21d56SDavid Gibson 
1484e68cb8b4SAlexey Kardashevskiy     return rc;
14854be21d56SDavid Gibson }
14864be21d56SDavid Gibson 
14874be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
14884be21d56SDavid Gibson {
148928e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
14904be21d56SDavid Gibson 
14914be21d56SDavid Gibson     /* Iteration header */
14924be21d56SDavid Gibson     qemu_put_be32(f, 0);
14934be21d56SDavid Gibson 
1494e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1495e68cb8b4SAlexey Kardashevskiy         int rc;
1496e68cb8b4SAlexey Kardashevskiy 
1497e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1498e68cb8b4SAlexey Kardashevskiy 
149901a57972SSamuel Mendoza-Jonas         rc = spapr_check_htab_fd(spapr);
150001a57972SSamuel Mendoza-Jonas         if (rc < 0) {
150101a57972SSamuel Mendoza-Jonas             return rc;
150201a57972SSamuel Mendoza-Jonas         }
150301a57972SSamuel Mendoza-Jonas 
1504e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1);
1505e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1506e68cb8b4SAlexey Kardashevskiy             return rc;
1507e68cb8b4SAlexey Kardashevskiy         }
1508e68cb8b4SAlexey Kardashevskiy         close(spapr->htab_fd);
1509e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = -1;
1510e68cb8b4SAlexey Kardashevskiy     } else {
15114be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1512e68cb8b4SAlexey Kardashevskiy     }
15134be21d56SDavid Gibson 
15144be21d56SDavid Gibson     /* End marker */
15154be21d56SDavid Gibson     qemu_put_be32(f, 0);
15164be21d56SDavid Gibson     qemu_put_be16(f, 0);
15174be21d56SDavid Gibson     qemu_put_be16(f, 0);
15184be21d56SDavid Gibson 
15194be21d56SDavid Gibson     return 0;
15204be21d56SDavid Gibson }
15214be21d56SDavid Gibson 
15224be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
15234be21d56SDavid Gibson {
152428e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
15254be21d56SDavid Gibson     uint32_t section_hdr;
1526e68cb8b4SAlexey Kardashevskiy     int fd = -1;
15274be21d56SDavid Gibson 
15284be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
15294be21d56SDavid Gibson         fprintf(stderr, "htab_load() bad version\n");
15304be21d56SDavid Gibson         return -EINVAL;
15314be21d56SDavid Gibson     }
15324be21d56SDavid Gibson 
15334be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
15344be21d56SDavid Gibson 
15354be21d56SDavid Gibson     if (section_hdr) {
15364be21d56SDavid Gibson         /* First section, just the hash shift */
15374be21d56SDavid Gibson         if (spapr->htab_shift != section_hdr) {
1538613e7a76SBharata B Rao             error_report("htab_shift mismatch: source %d target %d",
1539613e7a76SBharata B Rao                          section_hdr, spapr->htab_shift);
15404be21d56SDavid Gibson             return -EINVAL;
15414be21d56SDavid Gibson         }
15424be21d56SDavid Gibson         return 0;
15434be21d56SDavid Gibson     }
15444be21d56SDavid Gibson 
1545e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1546e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1547e68cb8b4SAlexey Kardashevskiy 
1548e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1549e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
1550e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n",
1551e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1552e68cb8b4SAlexey Kardashevskiy         }
1553e68cb8b4SAlexey Kardashevskiy     }
1554e68cb8b4SAlexey Kardashevskiy 
15554be21d56SDavid Gibson     while (true) {
15564be21d56SDavid Gibson         uint32_t index;
15574be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
15584be21d56SDavid Gibson 
15594be21d56SDavid Gibson         index = qemu_get_be32(f);
15604be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
15614be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
15624be21d56SDavid Gibson 
15634be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
15644be21d56SDavid Gibson             /* End of Stream */
15654be21d56SDavid Gibson             break;
15664be21d56SDavid Gibson         }
15674be21d56SDavid Gibson 
1568e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
15694be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
15704be21d56SDavid Gibson             /* Bad index in stream */
15714be21d56SDavid Gibson             fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) "
1572e68cb8b4SAlexey Kardashevskiy                     "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid,
1573e68cb8b4SAlexey Kardashevskiy                     spapr->htab_shift);
15744be21d56SDavid Gibson             return -EINVAL;
15754be21d56SDavid Gibson         }
15764be21d56SDavid Gibson 
1577e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
15784be21d56SDavid Gibson             if (n_valid) {
15794be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
15804be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
15814be21d56SDavid Gibson             }
15824be21d56SDavid Gibson             if (n_invalid) {
15834be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
15844be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
15854be21d56SDavid Gibson             }
1586e68cb8b4SAlexey Kardashevskiy         } else {
1587e68cb8b4SAlexey Kardashevskiy             int rc;
1588e68cb8b4SAlexey Kardashevskiy 
1589e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1590e68cb8b4SAlexey Kardashevskiy 
1591e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1592e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1593e68cb8b4SAlexey Kardashevskiy                 return rc;
1594e68cb8b4SAlexey Kardashevskiy             }
1595e68cb8b4SAlexey Kardashevskiy         }
1596e68cb8b4SAlexey Kardashevskiy     }
1597e68cb8b4SAlexey Kardashevskiy 
1598e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1599e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1600e68cb8b4SAlexey Kardashevskiy         close(fd);
16014be21d56SDavid Gibson     }
16024be21d56SDavid Gibson 
16034be21d56SDavid Gibson     return 0;
16044be21d56SDavid Gibson }
16054be21d56SDavid Gibson 
16064be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
16074be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
16084be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
1609a3e06c3dSDr. David Alan Gilbert     .save_live_complete_precopy = htab_save_complete,
16104be21d56SDavid Gibson     .load_state = htab_load,
16114be21d56SDavid Gibson };
16124be21d56SDavid Gibson 
16135b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
16145b2128d2SAlexander Graf                            Error **errp)
16155b2128d2SAlexander Graf {
16165b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
16175b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
16185b2128d2SAlexander Graf }
16195b2128d2SAlexander Graf 
1620bab99ea0SBharata B Rao static void spapr_cpu_init(sPAPRMachineState *spapr, PowerPCCPU *cpu)
1621bab99ea0SBharata B Rao {
1622bab99ea0SBharata B Rao     CPUPPCState *env = &cpu->env;
1623bab99ea0SBharata B Rao 
1624bab99ea0SBharata B Rao     /* Set time-base frequency to 512 MHz */
1625bab99ea0SBharata B Rao     cpu_ppc_tb_init(env, TIMEBASE_FREQ);
1626bab99ea0SBharata B Rao 
1627bab99ea0SBharata B Rao     /* PAPR always has exception vectors in RAM not ROM. To ensure this,
1628bab99ea0SBharata B Rao      * MSR[IP] should never be set.
1629bab99ea0SBharata B Rao      */
1630bab99ea0SBharata B Rao     env->msr_mask &= ~(1 << 6);
1631bab99ea0SBharata B Rao 
1632bab99ea0SBharata B Rao     /* Tell KVM that we're in PAPR mode */
1633bab99ea0SBharata B Rao     if (kvm_enabled()) {
1634bab99ea0SBharata B Rao         kvmppc_set_papr(cpu);
1635bab99ea0SBharata B Rao     }
1636bab99ea0SBharata B Rao 
1637bab99ea0SBharata B Rao     if (cpu->max_compat) {
1638bab99ea0SBharata B Rao         if (ppc_set_compat(cpu, cpu->max_compat) < 0) {
1639bab99ea0SBharata B Rao             exit(1);
1640bab99ea0SBharata B Rao         }
1641bab99ea0SBharata B Rao     }
1642bab99ea0SBharata B Rao 
1643bab99ea0SBharata B Rao     xics_cpu_setup(spapr->icp, cpu);
1644bab99ea0SBharata B Rao 
1645bab99ea0SBharata B Rao     qemu_register_reset(spapr_cpu_reset, cpu);
1646bab99ea0SBharata B Rao }
1647bab99ea0SBharata B Rao 
1648224245bfSDavid Gibson /*
1649224245bfSDavid Gibson  * Reset routine for LMB DR devices.
1650224245bfSDavid Gibson  *
1651224245bfSDavid Gibson  * Unlike PCI DR devices, LMB DR devices explicitly register this reset
1652224245bfSDavid Gibson  * routine. Reset for PCI DR devices will be handled by PHB reset routine
1653224245bfSDavid Gibson  * when it walks all its children devices. LMB devices reset occurs
1654224245bfSDavid Gibson  * as part of spapr_ppc_reset().
1655224245bfSDavid Gibson  */
1656224245bfSDavid Gibson static void spapr_drc_reset(void *opaque)
1657224245bfSDavid Gibson {
1658224245bfSDavid Gibson     sPAPRDRConnector *drc = opaque;
1659224245bfSDavid Gibson     DeviceState *d = DEVICE(drc);
1660224245bfSDavid Gibson 
1661224245bfSDavid Gibson     if (d) {
1662224245bfSDavid Gibson         device_reset(d);
1663224245bfSDavid Gibson     }
1664224245bfSDavid Gibson }
1665224245bfSDavid Gibson 
1666224245bfSDavid Gibson static void spapr_create_lmb_dr_connectors(sPAPRMachineState *spapr)
1667224245bfSDavid Gibson {
1668224245bfSDavid Gibson     MachineState *machine = MACHINE(spapr);
1669224245bfSDavid Gibson     uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
1670e8f986fcSBharata B Rao     uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
1671224245bfSDavid Gibson     int i;
1672224245bfSDavid Gibson 
1673224245bfSDavid Gibson     for (i = 0; i < nr_lmbs; i++) {
1674224245bfSDavid Gibson         sPAPRDRConnector *drc;
1675224245bfSDavid Gibson         uint64_t addr;
1676224245bfSDavid Gibson 
1677e8f986fcSBharata B Rao         addr = i * lmb_size + spapr->hotplug_memory.base;
1678224245bfSDavid Gibson         drc = spapr_dr_connector_new(OBJECT(spapr), SPAPR_DR_CONNECTOR_TYPE_LMB,
1679224245bfSDavid Gibson                                      addr/lmb_size);
1680224245bfSDavid Gibson         qemu_register_reset(spapr_drc_reset, drc);
1681224245bfSDavid Gibson     }
1682224245bfSDavid Gibson }
1683224245bfSDavid Gibson 
1684224245bfSDavid Gibson /*
1685224245bfSDavid Gibson  * If RAM size, maxmem size and individual node mem sizes aren't aligned
1686224245bfSDavid Gibson  * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
1687224245bfSDavid Gibson  * since we can't support such unaligned sizes with DRCONF_MEMORY.
1688224245bfSDavid Gibson  */
1689224245bfSDavid Gibson static void spapr_validate_node_memory(MachineState *machine)
1690224245bfSDavid Gibson {
1691224245bfSDavid Gibson     int i;
1692224245bfSDavid Gibson 
1693224245bfSDavid Gibson     if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE ||
1694224245bfSDavid Gibson         machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
1695224245bfSDavid Gibson         error_report("Can't support memory configuration where RAM size "
1696224245bfSDavid Gibson                      "0x" RAM_ADDR_FMT " or maxmem size "
1697224245bfSDavid Gibson                      "0x" RAM_ADDR_FMT " isn't aligned to %llu MB",
1698224245bfSDavid Gibson                      machine->ram_size, machine->maxram_size,
1699224245bfSDavid Gibson                      SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
1700224245bfSDavid Gibson         exit(EXIT_FAILURE);
1701224245bfSDavid Gibson     }
1702224245bfSDavid Gibson 
1703224245bfSDavid Gibson     for (i = 0; i < nb_numa_nodes; i++) {
1704224245bfSDavid Gibson         if (numa_info[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
1705224245bfSDavid Gibson             error_report("Can't support memory configuration where memory size"
1706224245bfSDavid Gibson                          " %" PRIx64 " of node %d isn't aligned to %llu MB",
1707224245bfSDavid Gibson                          numa_info[i].node_mem, i,
1708224245bfSDavid Gibson                          SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
1709224245bfSDavid Gibson             exit(EXIT_FAILURE);
1710224245bfSDavid Gibson         }
1711224245bfSDavid Gibson     }
1712224245bfSDavid Gibson }
1713224245bfSDavid Gibson 
171453018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
17153ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
171653018216SPaolo Bonzini {
171728e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
1718224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
17193ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
17203ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
17213ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
172253018216SPaolo Bonzini     PowerPCCPU *cpu;
172353018216SPaolo Bonzini     PCIHostState *phb;
172453018216SPaolo Bonzini     int i;
172553018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
172653018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1727658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1728658fa66bSAlexey Kardashevskiy     void *rma = NULL;
172953018216SPaolo Bonzini     hwaddr rma_alloc_size;
1730b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
173153018216SPaolo Bonzini     uint32_t initrd_base = 0;
173253018216SPaolo Bonzini     long kernel_size = 0, initrd_size = 0;
1733b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
173416457e7fSBenjamin Herrenschmidt     bool kernel_le = false;
173553018216SPaolo Bonzini     char *filename;
173653018216SPaolo Bonzini 
173753018216SPaolo Bonzini     msi_supported = true;
173853018216SPaolo Bonzini 
173953018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
174053018216SPaolo Bonzini 
174153018216SPaolo Bonzini     cpu_ppc_hypercall = emulate_spapr_hypercall;
174253018216SPaolo Bonzini 
174353018216SPaolo Bonzini     /* Allocate RMA if necessary */
1744658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
174553018216SPaolo Bonzini 
174653018216SPaolo Bonzini     if (rma_alloc_size == -1) {
1747730fce59SThomas Huth         error_report("Unable to create RMA");
174853018216SPaolo Bonzini         exit(1);
174953018216SPaolo Bonzini     }
175053018216SPaolo Bonzini 
1751c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
175253018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
175353018216SPaolo Bonzini     } else {
1754c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
175553018216SPaolo Bonzini 
175653018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
175753018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
175853018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
175953018216SPaolo Bonzini          *
176053018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
176153018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
176253018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
176353018216SPaolo Bonzini          * isn't determined yet.
176453018216SPaolo Bonzini          */
176553018216SPaolo Bonzini         if (kvm_enabled()) {
176653018216SPaolo Bonzini             spapr->vrma_adjust = 1;
176753018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
176853018216SPaolo Bonzini         }
176953018216SPaolo Bonzini     }
177053018216SPaolo Bonzini 
1771c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
1772c4177479SAlexey Kardashevskiy         fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n",
1773c4177479SAlexey Kardashevskiy                 spapr->rma_size);
1774c4177479SAlexey Kardashevskiy         exit(1);
1775c4177479SAlexey Kardashevskiy     }
1776c4177479SAlexey Kardashevskiy 
1777b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1778b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
177953018216SPaolo Bonzini 
178053018216SPaolo Bonzini     /* We aim for a hash table of size 1/128 the size of RAM.  The
178153018216SPaolo Bonzini      * normal rule of thumb is 1/64 the size of RAM, but that's much
178253018216SPaolo Bonzini      * more than needed for the Linux guests we support. */
178353018216SPaolo Bonzini     spapr->htab_shift = 18; /* Minimum architected size */
178453018216SPaolo Bonzini     while (spapr->htab_shift <= 46) {
1785ce881f77SBharata B Rao         if ((1ULL << (spapr->htab_shift + 7)) >= machine->maxram_size) {
178653018216SPaolo Bonzini             break;
178753018216SPaolo Bonzini         }
178853018216SPaolo Bonzini         spapr->htab_shift++;
178953018216SPaolo Bonzini     }
1790b817772aSBharata B Rao     spapr_alloc_htab(spapr);
179153018216SPaolo Bonzini 
17927b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
1793446f16a6SMarcel Apfelbaum     spapr->icp = xics_system_init(machine,
17949e734e3dSBharata B Rao                                   DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(),
1795f303f117SGreg Kurz                                                smp_threads),
17967b565160SDavid Gibson                                   XICS_IRQS);
17977b565160SDavid Gibson 
1798224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
1799224245bfSDavid Gibson         spapr_validate_node_memory(machine);
1800224245bfSDavid Gibson     }
1801224245bfSDavid Gibson 
180253018216SPaolo Bonzini     /* init CPUs */
180319fb2c36SBharata B Rao     if (machine->cpu_model == NULL) {
180419fb2c36SBharata B Rao         machine->cpu_model = kvm_enabled() ? "host" : "POWER7";
180553018216SPaolo Bonzini     }
180653018216SPaolo Bonzini     for (i = 0; i < smp_cpus; i++) {
180719fb2c36SBharata B Rao         cpu = cpu_ppc_init(machine->cpu_model);
180853018216SPaolo Bonzini         if (cpu == NULL) {
180953018216SPaolo Bonzini             fprintf(stderr, "Unable to find PowerPC CPU definition\n");
181053018216SPaolo Bonzini             exit(1);
181153018216SPaolo Bonzini         }
1812bab99ea0SBharata B Rao         spapr_cpu_init(spapr, cpu);
181353018216SPaolo Bonzini     }
181453018216SPaolo Bonzini 
1815026bfd89SDavid Gibson     if (kvm_enabled()) {
1816026bfd89SDavid Gibson         /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
1817026bfd89SDavid Gibson         kvmppc_enable_logical_ci_hcalls();
1818ef9971ddSAlexey Kardashevskiy         kvmppc_enable_set_mode_hcall();
1819026bfd89SDavid Gibson     }
1820026bfd89SDavid Gibson 
182153018216SPaolo Bonzini     /* allocate RAM */
1822f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
1823fb164994SDavid Gibson                                          machine->ram_size);
1824f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
182553018216SPaolo Bonzini 
1826658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
1827658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
1828658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
1829658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
1830658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
1831658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
1832658fa66bSAlexey Kardashevskiy     }
1833658fa66bSAlexey Kardashevskiy 
18344a1c9cf0SBharata B Rao     /* initialize hotplug memory address space */
18354a1c9cf0SBharata B Rao     if (machine->ram_size < machine->maxram_size) {
18364a1c9cf0SBharata B Rao         ram_addr_t hotplug_mem_size = machine->maxram_size - machine->ram_size;
18374a1c9cf0SBharata B Rao 
18384a1c9cf0SBharata B Rao         if (machine->ram_slots > SPAPR_MAX_RAM_SLOTS) {
183919a35c9eSBharata B Rao             error_report("Specified number of memory slots %"PRIu64" exceeds max supported %d\n",
184019a35c9eSBharata B Rao                          machine->ram_slots, SPAPR_MAX_RAM_SLOTS);
18414a1c9cf0SBharata B Rao             exit(EXIT_FAILURE);
18424a1c9cf0SBharata B Rao         }
18434a1c9cf0SBharata B Rao 
18444a1c9cf0SBharata B Rao         spapr->hotplug_memory.base = ROUND_UP(machine->ram_size,
18454a1c9cf0SBharata B Rao                                               SPAPR_HOTPLUG_MEM_ALIGN);
18464a1c9cf0SBharata B Rao         memory_region_init(&spapr->hotplug_memory.mr, OBJECT(spapr),
18474a1c9cf0SBharata B Rao                            "hotplug-memory", hotplug_mem_size);
18484a1c9cf0SBharata B Rao         memory_region_add_subregion(sysmem, spapr->hotplug_memory.base,
18494a1c9cf0SBharata B Rao                                     &spapr->hotplug_memory.mr);
18504a1c9cf0SBharata B Rao     }
18514a1c9cf0SBharata B Rao 
1852224245bfSDavid Gibson     if (smc->dr_lmb_enabled) {
1853224245bfSDavid Gibson         spapr_create_lmb_dr_connectors(spapr);
1854224245bfSDavid Gibson     }
1855224245bfSDavid Gibson 
185653018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
18574c56440dSStefan Weil     if (!filename) {
1858730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
18594c56440dSStefan Weil         exit(1);
18604c56440dSStefan Weil     }
1861b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
1862b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
1863b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
1864730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
186553018216SPaolo Bonzini         exit(1);
186653018216SPaolo Bonzini     }
186753018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
1868730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
18692f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
187053018216SPaolo Bonzini         exit(1);
187153018216SPaolo Bonzini     }
187253018216SPaolo Bonzini     g_free(filename);
187353018216SPaolo Bonzini 
187453018216SPaolo Bonzini     /* Set up EPOW events infrastructure */
187553018216SPaolo Bonzini     spapr_events_init(spapr);
187653018216SPaolo Bonzini 
187712f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
187828df36a1SDavid Gibson     spapr_rtc_create(spapr);
187912f42174SDavid Gibson 
188053018216SPaolo Bonzini     /* Set up VIO bus */
188153018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
188253018216SPaolo Bonzini 
188353018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
188453018216SPaolo Bonzini         if (serial_hds[i]) {
188553018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
188653018216SPaolo Bonzini         }
188753018216SPaolo Bonzini     }
188853018216SPaolo Bonzini 
188953018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
189053018216SPaolo Bonzini     spapr_create_nvram(spapr);
189153018216SPaolo Bonzini 
189253018216SPaolo Bonzini     /* Set up PCI */
189353018216SPaolo Bonzini     spapr_pci_rtas_init();
189453018216SPaolo Bonzini 
189589dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
189653018216SPaolo Bonzini 
189753018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
189853018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
189953018216SPaolo Bonzini 
190053018216SPaolo Bonzini         if (!nd->model) {
190153018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
190253018216SPaolo Bonzini         }
190353018216SPaolo Bonzini 
190453018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
190553018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
190653018216SPaolo Bonzini         } else {
190729b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
190853018216SPaolo Bonzini         }
190953018216SPaolo Bonzini     }
191053018216SPaolo Bonzini 
191153018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
191253018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
191353018216SPaolo Bonzini     }
191453018216SPaolo Bonzini 
191553018216SPaolo Bonzini     /* Graphics */
191653018216SPaolo Bonzini     if (spapr_vga_init(phb->bus)) {
191753018216SPaolo Bonzini         spapr->has_graphics = true;
1918c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
191953018216SPaolo Bonzini     }
192053018216SPaolo Bonzini 
19214ee9ced9SMarcel Apfelbaum     if (machine->usb) {
192257040d45SThomas Huth         if (smc->use_ohci_by_default) {
192353018216SPaolo Bonzini             pci_create_simple(phb->bus, -1, "pci-ohci");
192457040d45SThomas Huth         } else {
192557040d45SThomas Huth             pci_create_simple(phb->bus, -1, "nec-usb-xhci");
192657040d45SThomas Huth         }
1927c86580b8SMarkus Armbruster 
192853018216SPaolo Bonzini         if (spapr->has_graphics) {
1929c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
1930c86580b8SMarkus Armbruster 
1931c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
1932c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
193353018216SPaolo Bonzini         }
193453018216SPaolo Bonzini     }
193553018216SPaolo Bonzini 
193653018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
193753018216SPaolo Bonzini         fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
193853018216SPaolo Bonzini                 "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
193953018216SPaolo Bonzini         exit(1);
194053018216SPaolo Bonzini     }
194153018216SPaolo Bonzini 
194253018216SPaolo Bonzini     if (kernel_filename) {
194353018216SPaolo Bonzini         uint64_t lowaddr = 0;
194453018216SPaolo Bonzini 
194553018216SPaolo Bonzini         kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
19464ecd4d16SPeter Crosthwaite                                NULL, &lowaddr, NULL, 1, PPC_ELF_MACHINE, 0);
19473b66da82SAlexey Kardashevskiy         if (kernel_size == ELF_LOAD_WRONG_ENDIAN) {
194816457e7fSBenjamin Herrenschmidt             kernel_size = load_elf(kernel_filename,
194916457e7fSBenjamin Herrenschmidt                                    translate_kernel_address, NULL,
19504ecd4d16SPeter Crosthwaite                                    NULL, &lowaddr, NULL, 0, PPC_ELF_MACHINE, 0);
195116457e7fSBenjamin Herrenschmidt             kernel_le = kernel_size > 0;
195216457e7fSBenjamin Herrenschmidt         }
195316457e7fSBenjamin Herrenschmidt         if (kernel_size < 0) {
19543b66da82SAlexey Kardashevskiy             fprintf(stderr, "qemu: error loading %s: %s\n",
19553b66da82SAlexey Kardashevskiy                     kernel_filename, load_elf_strerror(kernel_size));
195653018216SPaolo Bonzini             exit(1);
195753018216SPaolo Bonzini         }
195853018216SPaolo Bonzini 
195953018216SPaolo Bonzini         /* load initrd */
196053018216SPaolo Bonzini         if (initrd_filename) {
196153018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
196253018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
196353018216SPaolo Bonzini              */
196453018216SPaolo Bonzini             initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
196553018216SPaolo Bonzini             initrd_size = load_image_targphys(initrd_filename, initrd_base,
196653018216SPaolo Bonzini                                               load_limit - initrd_base);
196753018216SPaolo Bonzini             if (initrd_size < 0) {
196853018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
196953018216SPaolo Bonzini                         initrd_filename);
197053018216SPaolo Bonzini                 exit(1);
197153018216SPaolo Bonzini             }
197253018216SPaolo Bonzini         } else {
197353018216SPaolo Bonzini             initrd_base = 0;
197453018216SPaolo Bonzini             initrd_size = 0;
197553018216SPaolo Bonzini         }
197653018216SPaolo Bonzini     }
197753018216SPaolo Bonzini 
19788e7ea787SAndreas Färber     if (bios_name == NULL) {
19798e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
19808e7ea787SAndreas Färber     }
19818e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
19824c56440dSStefan Weil     if (!filename) {
198368fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
19844c56440dSStefan Weil         exit(1);
19854c56440dSStefan Weil     }
198653018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
198768fea5a0SThomas Huth     if (fw_size <= 0) {
198868fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
198953018216SPaolo Bonzini         exit(1);
199053018216SPaolo Bonzini     }
199153018216SPaolo Bonzini     g_free(filename);
199253018216SPaolo Bonzini 
199328e02042SDavid Gibson     /* FIXME: Should register things through the MachineState's qdev
199428e02042SDavid Gibson      * interface, this is a legacy from the sPAPREnvironment structure
199528e02042SDavid Gibson      * which predated MachineState but had a similar function */
19964be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
19974be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
19984be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
19994be21d56SDavid Gibson 
200053018216SPaolo Bonzini     /* Prepare the device tree */
20013bbf37f2SAndreas Färber     spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size,
200216457e7fSBenjamin Herrenschmidt                                             kernel_size, kernel_le,
200331fe14d1SNathan Fontenot                                             kernel_cmdline,
200431fe14d1SNathan Fontenot                                             spapr->check_exception_irq);
200553018216SPaolo Bonzini     assert(spapr->fdt_skel != NULL);
20065b2128d2SAlexander Graf 
200746503c2bSMichael Roth     /* used by RTAS */
200846503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
200946503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
201046503c2bSMichael Roth 
20115b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
201253018216SPaolo Bonzini }
201353018216SPaolo Bonzini 
2014135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
2015135a129aSAneesh Kumar K.V {
2016135a129aSAneesh Kumar K.V     if (!vm_type) {
2017135a129aSAneesh Kumar K.V         return 0;
2018135a129aSAneesh Kumar K.V     }
2019135a129aSAneesh Kumar K.V 
2020135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
2021135a129aSAneesh Kumar K.V         return 1;
2022135a129aSAneesh Kumar K.V     }
2023135a129aSAneesh Kumar K.V 
2024135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
2025135a129aSAneesh Kumar K.V         return 2;
2026135a129aSAneesh Kumar K.V     }
2027135a129aSAneesh Kumar K.V 
2028135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
2029135a129aSAneesh Kumar K.V     exit(1);
2030135a129aSAneesh Kumar K.V }
2031135a129aSAneesh Kumar K.V 
203271461b0fSAlexey Kardashevskiy /*
2033627b84f4SGonglei  * Implementation of an interface to adjust firmware path
203471461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
203571461b0fSAlexey Kardashevskiy  */
203671461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
203771461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
203871461b0fSAlexey Kardashevskiy {
203971461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
204071461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
204171461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
204271461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
204371461b0fSAlexey Kardashevskiy 
204471461b0fSAlexey Kardashevskiy     if (d) {
204571461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
204671461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
204771461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
204871461b0fSAlexey Kardashevskiy 
204971461b0fSAlexey Kardashevskiy         if (spapr) {
205071461b0fSAlexey Kardashevskiy             /*
205171461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
205271461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
205371461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
205471461b0fSAlexey Kardashevskiy              */
205571461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
205671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
205771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
205871461b0fSAlexey Kardashevskiy         } else if (virtio) {
205971461b0fSAlexey Kardashevskiy             /*
206071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
206171461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
206271461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
206371461b0fSAlexey Kardashevskiy              * the actual binding is:
206471461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
206571461b0fSAlexey Kardashevskiy              */
206671461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
206771461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
206871461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
206971461b0fSAlexey Kardashevskiy         } else if (usb) {
207071461b0fSAlexey Kardashevskiy             /*
207171461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
207271461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
207371461b0fSAlexey Kardashevskiy              */
207471461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
207571461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
207671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
207771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
207871461b0fSAlexey Kardashevskiy         }
207971461b0fSAlexey Kardashevskiy     }
208071461b0fSAlexey Kardashevskiy 
208171461b0fSAlexey Kardashevskiy     if (phb) {
208271461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
208371461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
208471461b0fSAlexey Kardashevskiy     }
208571461b0fSAlexey Kardashevskiy 
208671461b0fSAlexey Kardashevskiy     return NULL;
208771461b0fSAlexey Kardashevskiy }
208871461b0fSAlexey Kardashevskiy 
208923825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
209023825581SEduardo Habkost {
209128e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
209223825581SEduardo Habkost 
209328e02042SDavid Gibson     return g_strdup(spapr->kvm_type);
209423825581SEduardo Habkost }
209523825581SEduardo Habkost 
209623825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
209723825581SEduardo Habkost {
209828e02042SDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
209923825581SEduardo Habkost 
210028e02042SDavid Gibson     g_free(spapr->kvm_type);
210128e02042SDavid Gibson     spapr->kvm_type = g_strdup(value);
210223825581SEduardo Habkost }
210323825581SEduardo Habkost 
210423825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
210523825581SEduardo Habkost {
210623825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
210723825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
210849d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
210949d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
211049d2e648SMarcel Apfelbaum                                     NULL);
211123825581SEduardo Habkost }
211223825581SEduardo Habkost 
211387bbdd9cSDavid Gibson static void spapr_machine_finalizefn(Object *obj)
211487bbdd9cSDavid Gibson {
211587bbdd9cSDavid Gibson     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
211687bbdd9cSDavid Gibson 
211787bbdd9cSDavid Gibson     g_free(spapr->kvm_type);
211887bbdd9cSDavid Gibson }
211987bbdd9cSDavid Gibson 
212034316482SAlexey Kardashevskiy static void ppc_cpu_do_nmi_on_cpu(void *arg)
212134316482SAlexey Kardashevskiy {
212234316482SAlexey Kardashevskiy     CPUState *cs = arg;
212334316482SAlexey Kardashevskiy 
212434316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
212534316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
212634316482SAlexey Kardashevskiy }
212734316482SAlexey Kardashevskiy 
212834316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
212934316482SAlexey Kardashevskiy {
213034316482SAlexey Kardashevskiy     CPUState *cs;
213134316482SAlexey Kardashevskiy 
213234316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
213334316482SAlexey Kardashevskiy         async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs);
213434316482SAlexey Kardashevskiy     }
213534316482SAlexey Kardashevskiy }
213634316482SAlexey Kardashevskiy 
2137c20d332aSBharata B Rao static void spapr_add_lmbs(DeviceState *dev, uint64_t addr, uint64_t size,
2138c20d332aSBharata B Rao                            uint32_t node, Error **errp)
2139c20d332aSBharata B Rao {
2140c20d332aSBharata B Rao     sPAPRDRConnector *drc;
2141c20d332aSBharata B Rao     sPAPRDRConnectorClass *drck;
2142c20d332aSBharata B Rao     uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
2143c20d332aSBharata B Rao     int i, fdt_offset, fdt_size;
2144c20d332aSBharata B Rao     void *fdt;
2145c20d332aSBharata B Rao 
2146c20d332aSBharata B Rao     /*
2147c20d332aSBharata B Rao      * Check for DRC connectors and send hotplug notification to the
2148c20d332aSBharata B Rao      * guest only in case of hotplugged memory. This allows cold plugged
2149c20d332aSBharata B Rao      * memory to be specified at boot time.
2150c20d332aSBharata B Rao      */
2151c20d332aSBharata B Rao     if (!dev->hotplugged) {
2152c20d332aSBharata B Rao         return;
2153c20d332aSBharata B Rao     }
2154c20d332aSBharata B Rao 
2155c20d332aSBharata B Rao     for (i = 0; i < nr_lmbs; i++) {
2156c20d332aSBharata B Rao         drc = spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_LMB,
2157c20d332aSBharata B Rao                 addr/SPAPR_MEMORY_BLOCK_SIZE);
2158c20d332aSBharata B Rao         g_assert(drc);
2159c20d332aSBharata B Rao 
2160c20d332aSBharata B Rao         fdt = create_device_tree(&fdt_size);
2161c20d332aSBharata B Rao         fdt_offset = spapr_populate_memory_node(fdt, node, addr,
2162c20d332aSBharata B Rao                                                 SPAPR_MEMORY_BLOCK_SIZE);
2163c20d332aSBharata B Rao 
2164c20d332aSBharata B Rao         drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
2165c20d332aSBharata B Rao         drck->attach(drc, dev, fdt, fdt_offset, !dev->hotplugged, errp);
2166c20d332aSBharata B Rao         addr += SPAPR_MEMORY_BLOCK_SIZE;
2167c20d332aSBharata B Rao     }
21680a417869SBharata B Rao     spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB, nr_lmbs);
2169c20d332aSBharata B Rao }
2170c20d332aSBharata B Rao 
2171c20d332aSBharata B Rao static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2172c20d332aSBharata B Rao                               uint32_t node, Error **errp)
2173c20d332aSBharata B Rao {
2174c20d332aSBharata B Rao     Error *local_err = NULL;
2175c20d332aSBharata B Rao     sPAPRMachineState *ms = SPAPR_MACHINE(hotplug_dev);
2176c20d332aSBharata B Rao     PCDIMMDevice *dimm = PC_DIMM(dev);
2177c20d332aSBharata B Rao     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
2178c20d332aSBharata B Rao     MemoryRegion *mr = ddc->get_memory_region(dimm);
2179c20d332aSBharata B Rao     uint64_t align = memory_region_get_alignment(mr);
2180c20d332aSBharata B Rao     uint64_t size = memory_region_size(mr);
2181c20d332aSBharata B Rao     uint64_t addr;
2182c20d332aSBharata B Rao 
2183c20d332aSBharata B Rao     if (size % SPAPR_MEMORY_BLOCK_SIZE) {
2184c20d332aSBharata B Rao         error_setg(&local_err, "Hotplugged memory size must be a multiple of "
2185c20d332aSBharata B Rao                       "%lld MB", SPAPR_MEMORY_BLOCK_SIZE/M_BYTE);
2186c20d332aSBharata B Rao         goto out;
2187c20d332aSBharata B Rao     }
2188c20d332aSBharata B Rao 
2189d6a9b0b8SMichael S. Tsirkin     pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err);
2190c20d332aSBharata B Rao     if (local_err) {
2191c20d332aSBharata B Rao         goto out;
2192c20d332aSBharata B Rao     }
2193c20d332aSBharata B Rao 
2194c20d332aSBharata B Rao     addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err);
2195c20d332aSBharata B Rao     if (local_err) {
2196c20d332aSBharata B Rao         pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr);
2197c20d332aSBharata B Rao         goto out;
2198c20d332aSBharata B Rao     }
2199c20d332aSBharata B Rao 
2200c20d332aSBharata B Rao     spapr_add_lmbs(dev, addr, size, node, &error_abort);
2201c20d332aSBharata B Rao 
2202c20d332aSBharata B Rao out:
2203c20d332aSBharata B Rao     error_propagate(errp, local_err);
2204c20d332aSBharata B Rao }
2205c20d332aSBharata B Rao 
2206c20d332aSBharata B Rao static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
2207c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2208c20d332aSBharata B Rao {
2209c20d332aSBharata B Rao     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(qdev_get_machine());
2210c20d332aSBharata B Rao 
2211c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2212b556854bSBharata B Rao         int node;
2213c20d332aSBharata B Rao 
2214c20d332aSBharata B Rao         if (!smc->dr_lmb_enabled) {
2215c20d332aSBharata B Rao             error_setg(errp, "Memory hotplug not supported for this machine");
2216c20d332aSBharata B Rao             return;
2217c20d332aSBharata B Rao         }
2218c20d332aSBharata B Rao         node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, errp);
2219c20d332aSBharata B Rao         if (*errp) {
2220c20d332aSBharata B Rao             return;
2221c20d332aSBharata B Rao         }
2222c20d332aSBharata B Rao 
2223b556854bSBharata B Rao         /*
2224b556854bSBharata B Rao          * Currently PowerPC kernel doesn't allow hot-adding memory to
2225b556854bSBharata B Rao          * memory-less node, but instead will silently add the memory
2226b556854bSBharata B Rao          * to the first node that has some memory. This causes two
2227b556854bSBharata B Rao          * unexpected behaviours for the user.
2228b556854bSBharata B Rao          *
2229b556854bSBharata B Rao          * - Memory gets hotplugged to a different node than what the user
2230b556854bSBharata B Rao          *   specified.
2231b556854bSBharata B Rao          * - Since pc-dimm subsystem in QEMU still thinks that memory belongs
2232b556854bSBharata B Rao          *   to memory-less node, a reboot will set things accordingly
2233b556854bSBharata B Rao          *   and the previously hotplugged memory now ends in the right node.
2234b556854bSBharata B Rao          *   This appears as if some memory moved from one node to another.
2235b556854bSBharata B Rao          *
2236b556854bSBharata B Rao          * So until kernel starts supporting memory hotplug to memory-less
2237b556854bSBharata B Rao          * nodes, just prevent such attempts upfront in QEMU.
2238b556854bSBharata B Rao          */
2239b556854bSBharata B Rao         if (nb_numa_nodes && !numa_info[node].node_mem) {
2240b556854bSBharata B Rao             error_setg(errp, "Can't hotplug memory to memory-less node %d",
2241b556854bSBharata B Rao                        node);
2242b556854bSBharata B Rao             return;
2243b556854bSBharata B Rao         }
2244b556854bSBharata B Rao 
2245c20d332aSBharata B Rao         spapr_memory_plug(hotplug_dev, dev, node, errp);
2246c20d332aSBharata B Rao     }
2247c20d332aSBharata B Rao }
2248c20d332aSBharata B Rao 
2249c20d332aSBharata B Rao static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
2250c20d332aSBharata B Rao                                       DeviceState *dev, Error **errp)
2251c20d332aSBharata B Rao {
2252c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2253c20d332aSBharata B Rao         error_setg(errp, "Memory hot unplug not supported by sPAPR");
2254c20d332aSBharata B Rao     }
2255c20d332aSBharata B Rao }
2256c20d332aSBharata B Rao 
2257c20d332aSBharata B Rao static HotplugHandler *spapr_get_hotpug_handler(MachineState *machine,
2258c20d332aSBharata B Rao                                              DeviceState *dev)
2259c20d332aSBharata B Rao {
2260c20d332aSBharata B Rao     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2261c20d332aSBharata B Rao         return HOTPLUG_HANDLER(machine);
2262c20d332aSBharata B Rao     }
2263c20d332aSBharata B Rao     return NULL;
2264c20d332aSBharata B Rao }
2265c20d332aSBharata B Rao 
226620bb648dSDavid Gibson static unsigned spapr_cpu_index_to_socket_id(unsigned cpu_index)
226720bb648dSDavid Gibson {
226820bb648dSDavid Gibson     /* Allocate to NUMA nodes on a "socket" basis (not that concept of
226920bb648dSDavid Gibson      * socket means much for the paravirtualized PAPR platform) */
227020bb648dSDavid Gibson     return cpu_index / smp_threads / smp_cores;
227120bb648dSDavid Gibson }
227220bb648dSDavid Gibson 
227329ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
227453018216SPaolo Bonzini {
227529ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
2276224245bfSDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
227771461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
227834316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
2279c20d332aSBharata B Rao     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
228029ee3247SAlexey Kardashevskiy 
22810eb9054cSDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant)";
2282fc9f38c3SDavid Gibson 
2283fc9f38c3SDavid Gibson     /*
2284fc9f38c3SDavid Gibson      * We set up the default / latest behaviour here.  The class_init
2285fc9f38c3SDavid Gibson      * functions for the specific versioned machine types can override
2286fc9f38c3SDavid Gibson      * these details for backwards compatibility
2287fc9f38c3SDavid Gibson      */
2288958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
2289958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
2290958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
229138b02bd8SAlexey Kardashevskiy     mc->max_cpus = MAX_CPUMASK_BITS;
2292958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
22935b2128d2SAlexander Graf     mc->default_boot_order = "";
2294a34944feSNikunj A Dadhania     mc->default_ram_size = 512 * M_BYTE;
2295958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
22969e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
2297e4024630SLaurent Vivier     mc->pci_allow_0_address = true;
2298c20d332aSBharata B Rao     mc->get_hotplug_handler = spapr_get_hotpug_handler;
2299c20d332aSBharata B Rao     hc->plug = spapr_machine_device_plug;
2300c20d332aSBharata B Rao     hc->unplug = spapr_machine_device_unplug;
230120bb648dSDavid Gibson     mc->cpu_index_to_socket_id = spapr_cpu_index_to_socket_id;
230200b4fbe2SMarcel Apfelbaum 
2303fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = true;
230471461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
230534316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
230653018216SPaolo Bonzini }
230753018216SPaolo Bonzini 
230829ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
230929ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
231029ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
23114aee7362SDavid Gibson     .abstract      = true,
23126ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
231323825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
231487bbdd9cSDavid Gibson     .instance_finalize = spapr_machine_finalizefn,
2315183930c0SDavid Gibson     .class_size    = sizeof(sPAPRMachineClass),
231629ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
231771461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
231871461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
231934316482SAlexey Kardashevskiy         { TYPE_NMI },
2320c20d332aSBharata B Rao         { TYPE_HOTPLUG_HANDLER },
232171461b0fSAlexey Kardashevskiy         { }
232271461b0fSAlexey Kardashevskiy     },
232329ee3247SAlexey Kardashevskiy };
232429ee3247SAlexey Kardashevskiy 
2325fccbc785SDavid Gibson #define DEFINE_SPAPR_MACHINE(suffix, verstr, latest)                 \
23265013c547SDavid Gibson     static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
23275013c547SDavid Gibson                                                     void *data)      \
23285013c547SDavid Gibson     {                                                                \
23295013c547SDavid Gibson         MachineClass *mc = MACHINE_CLASS(oc);                        \
23305013c547SDavid Gibson         spapr_machine_##suffix##_class_options(mc);                  \
2331fccbc785SDavid Gibson         if (latest) {                                                \
2332fccbc785SDavid Gibson             mc->alias = "pseries";                                   \
2333fccbc785SDavid Gibson             mc->is_default = 1;                                      \
2334fccbc785SDavid Gibson         }                                                            \
23355013c547SDavid Gibson     }                                                                \
23365013c547SDavid Gibson     static void spapr_machine_##suffix##_instance_init(Object *obj)  \
23375013c547SDavid Gibson     {                                                                \
23385013c547SDavid Gibson         MachineState *machine = MACHINE(obj);                        \
23395013c547SDavid Gibson         spapr_machine_##suffix##_instance_options(machine);          \
23405013c547SDavid Gibson     }                                                                \
23415013c547SDavid Gibson     static const TypeInfo spapr_machine_##suffix##_info = {          \
23425013c547SDavid Gibson         .name = MACHINE_TYPE_NAME("pseries-" verstr),                \
23435013c547SDavid Gibson         .parent = TYPE_SPAPR_MACHINE,                                \
23445013c547SDavid Gibson         .class_init = spapr_machine_##suffix##_class_init,           \
23455013c547SDavid Gibson         .instance_init = spapr_machine_##suffix##_instance_init,     \
23465013c547SDavid Gibson     };                                                               \
23475013c547SDavid Gibson     static void spapr_machine_register_##suffix(void)                \
23485013c547SDavid Gibson     {                                                                \
23495013c547SDavid Gibson         type_register(&spapr_machine_##suffix##_info);               \
23505013c547SDavid Gibson     }                                                                \
23515013c547SDavid Gibson     machine_init(spapr_machine_register_##suffix)
23525013c547SDavid Gibson 
23531c5f29bbSDavid Gibson /*
23544b23699cSDavid Gibson  * pseries-2.6
23554b23699cSDavid Gibson  */
23564b23699cSDavid Gibson static void spapr_machine_2_6_instance_options(MachineState *machine)
23574b23699cSDavid Gibson {
23584b23699cSDavid Gibson }
23594b23699cSDavid Gibson 
23604b23699cSDavid Gibson static void spapr_machine_2_6_class_options(MachineClass *mc)
23614b23699cSDavid Gibson {
23624b23699cSDavid Gibson     /* Defaults for the latest behaviour inherited from the base class */
23634b23699cSDavid Gibson }
23644b23699cSDavid Gibson 
23654b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_6, "2.6", true);
23664b23699cSDavid Gibson 
23674b23699cSDavid Gibson /*
23681c5f29bbSDavid Gibson  * pseries-2.5
23691c5f29bbSDavid Gibson  */
23704b23699cSDavid Gibson #define SPAPR_COMPAT_2_5 \
23714b23699cSDavid Gibson         HW_COMPAT_2_5
23724b23699cSDavid Gibson 
23735013c547SDavid Gibson static void spapr_machine_2_5_instance_options(MachineState *machine)
23741c5f29bbSDavid Gibson {
23755013c547SDavid Gibson }
23765013c547SDavid Gibson 
23775013c547SDavid Gibson static void spapr_machine_2_5_class_options(MachineClass *mc)
23785013c547SDavid Gibson {
237957040d45SThomas Huth     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
238057040d45SThomas Huth 
23814b23699cSDavid Gibson     spapr_machine_2_6_class_options(mc);
238257040d45SThomas Huth     smc->use_ohci_by_default = true;
23834b23699cSDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_5);
23841c5f29bbSDavid Gibson }
23851c5f29bbSDavid Gibson 
23864b23699cSDavid Gibson DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
23871c5f29bbSDavid Gibson 
23881c5f29bbSDavid Gibson /*
23891c5f29bbSDavid Gibson  * pseries-2.4
23901c5f29bbSDavid Gibson  */
239180fd50f9SCornelia Huck #define SPAPR_COMPAT_2_4 \
239280fd50f9SCornelia Huck         HW_COMPAT_2_4
239380fd50f9SCornelia Huck 
23945013c547SDavid Gibson static void spapr_machine_2_4_instance_options(MachineState *machine)
23951c5f29bbSDavid Gibson {
23965013c547SDavid Gibson     spapr_machine_2_5_instance_options(machine);
23975013c547SDavid Gibson }
23981c5f29bbSDavid Gibson 
23995013c547SDavid Gibson static void spapr_machine_2_4_class_options(MachineClass *mc)
24005013c547SDavid Gibson {
2401fc9f38c3SDavid Gibson     sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
2402fc9f38c3SDavid Gibson 
2403fc9f38c3SDavid Gibson     spapr_machine_2_5_class_options(mc);
2404fc9f38c3SDavid Gibson     smc->dr_lmb_enabled = false;
2405f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_4);
24061c5f29bbSDavid Gibson }
24071c5f29bbSDavid Gibson 
2408fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
24091c5f29bbSDavid Gibson 
24101c5f29bbSDavid Gibson /*
24111c5f29bbSDavid Gibson  * pseries-2.3
24121c5f29bbSDavid Gibson  */
241338ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
241480fd50f9SCornelia Huck         SPAPR_COMPAT_2_4 \
24157619c7b0SMichael Roth         HW_COMPAT_2_3 \
24167619c7b0SMichael Roth         {\
24177619c7b0SMichael Roth             .driver   = "spapr-pci-host-bridge",\
24187619c7b0SMichael Roth             .property = "dynamic-reconfiguration",\
24197619c7b0SMichael Roth             .value    = "off",\
24207619c7b0SMichael Roth         },
242138ff32c6SEduardo Habkost 
24225013c547SDavid Gibson static void spapr_machine_2_3_instance_options(MachineState *machine)
24231c5f29bbSDavid Gibson {
24245013c547SDavid Gibson     spapr_machine_2_4_instance_options(machine);
24251c5f29bbSDavid Gibson     savevm_skip_section_footers();
24261c5f29bbSDavid Gibson     global_state_set_optional();
24271c5f29bbSDavid Gibson }
24281c5f29bbSDavid Gibson 
24295013c547SDavid Gibson static void spapr_machine_2_3_class_options(MachineClass *mc)
24301c5f29bbSDavid Gibson {
2431fc9f38c3SDavid Gibson     spapr_machine_2_4_class_options(mc);
2432f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_3);
24331c5f29bbSDavid Gibson }
2434fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
24351c5f29bbSDavid Gibson 
24361c5f29bbSDavid Gibson /*
24371c5f29bbSDavid Gibson  * pseries-2.2
24381c5f29bbSDavid Gibson  */
24391c5f29bbSDavid Gibson 
2440b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
244138ff32c6SEduardo Habkost         SPAPR_COMPAT_2_3 \
24424dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
2443b194df47SAlexey Kardashevskiy         {\
2444b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
2445b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
2446b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
2447dd754bafSEduardo Habkost         },
2448b194df47SAlexey Kardashevskiy 
24495013c547SDavid Gibson static void spapr_machine_2_2_instance_options(MachineState *machine)
2450b0e966d0SJason Wang {
24515013c547SDavid Gibson     spapr_machine_2_3_instance_options(machine);
2452b0e966d0SJason Wang }
2453b0e966d0SJason Wang 
24545013c547SDavid Gibson static void spapr_machine_2_2_class_options(MachineClass *mc)
2455b0e966d0SJason Wang {
2456fc9f38c3SDavid Gibson     spapr_machine_2_3_class_options(mc);
2457f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_2);
24581c5f29bbSDavid Gibson }
2459fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
24601c5f29bbSDavid Gibson 
24611c5f29bbSDavid Gibson /*
24621c5f29bbSDavid Gibson  * pseries-2.1
24631c5f29bbSDavid Gibson  */
24641c5f29bbSDavid Gibson #define SPAPR_COMPAT_2_1 \
24651c5f29bbSDavid Gibson         SPAPR_COMPAT_2_2 \
24661c5f29bbSDavid Gibson         HW_COMPAT_2_1
24671c5f29bbSDavid Gibson 
24685013c547SDavid Gibson static void spapr_machine_2_1_instance_options(MachineState *machine)
24691c5f29bbSDavid Gibson {
24705013c547SDavid Gibson     spapr_machine_2_2_instance_options(machine);
24711c5f29bbSDavid Gibson }
24721c5f29bbSDavid Gibson 
24735013c547SDavid Gibson static void spapr_machine_2_1_class_options(MachineClass *mc)
2474b0e966d0SJason Wang {
2475fc9f38c3SDavid Gibson     spapr_machine_2_2_class_options(mc);
2476f949b4e5SDavid Gibson     SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_1);
24776026db45SAlexey Kardashevskiy }
2478fccbc785SDavid Gibson DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
24796026db45SAlexey Kardashevskiy 
248029ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
248129ee3247SAlexey Kardashevskiy {
248229ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
248329ee3247SAlexey Kardashevskiy }
248429ee3247SAlexey Kardashevskiy 
248529ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
2486