xref: /openbmc/qemu/hw/ppc/spapr.c (revision 31fe14d1)
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"
33fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3453018216SPaolo Bonzini #include "sysemu/cpus.h"
3553018216SPaolo Bonzini #include "sysemu/kvm.h"
3653018216SPaolo Bonzini #include "kvm_ppc.h"
374be21d56SDavid Gibson #include "mmu-hash64.h"
383794d548SAlexey Kardashevskiy #include "qom/cpu.h"
3953018216SPaolo Bonzini 
4053018216SPaolo Bonzini #include "hw/boards.h"
410d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
4253018216SPaolo Bonzini #include "hw/loader.h"
4353018216SPaolo Bonzini 
440d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
450d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
460d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
470d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
4853018216SPaolo Bonzini #include "hw/pci/msi.h"
4953018216SPaolo Bonzini 
5053018216SPaolo Bonzini #include "hw/pci/pci.h"
5171461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
5271461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
5353018216SPaolo Bonzini 
5453018216SPaolo Bonzini #include "exec/address-spaces.h"
5553018216SPaolo Bonzini #include "hw/usb.h"
5653018216SPaolo Bonzini #include "qemu/config-file.h"
57135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
582a6593cbSAlexey Kardashevskiy #include "trace.h"
5934316482SAlexey Kardashevskiy #include "hw/nmi.h"
6053018216SPaolo Bonzini 
6168a27b20SMichael S. Tsirkin #include "hw/compat.h"
6268a27b20SMichael S. Tsirkin 
6353018216SPaolo Bonzini #include <libfdt.h>
6453018216SPaolo Bonzini 
6553018216SPaolo Bonzini /* SLOF memory layout:
6653018216SPaolo Bonzini  *
6753018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
6853018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
6953018216SPaolo Bonzini  *
7053018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
7153018216SPaolo Bonzini  * and more
7253018216SPaolo Bonzini  *
7353018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
7453018216SPaolo Bonzini  */
753bf6eeddSAlexey Kardashevskiy #define FDT_MAX_SIZE            0x40000
7653018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
77b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
7853018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
7953018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
8053018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
8153018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
8253018216SPaolo Bonzini 
8353018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
8453018216SPaolo Bonzini 
8553018216SPaolo Bonzini #define TIMEBASE_FREQ           512000000ULL
8653018216SPaolo Bonzini 
879674a356SNikunj A Dadhania #define MAX_CPUS                255
8853018216SPaolo Bonzini 
8953018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
9053018216SPaolo Bonzini 
9153018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9253018216SPaolo Bonzini 
936ca1502eSAlexey Kardashevskiy typedef struct sPAPRMachineState sPAPRMachineState;
94748abce9SEduardo Habkost 
9529ee3247SAlexey Kardashevskiy #define TYPE_SPAPR_MACHINE      "spapr-machine"
96748abce9SEduardo Habkost #define SPAPR_MACHINE(obj) \
976ca1502eSAlexey Kardashevskiy     OBJECT_CHECK(sPAPRMachineState, (obj), TYPE_SPAPR_MACHINE)
98748abce9SEduardo Habkost 
99748abce9SEduardo Habkost /**
1006ca1502eSAlexey Kardashevskiy  * sPAPRMachineState:
101748abce9SEduardo Habkost  */
1026ca1502eSAlexey Kardashevskiy struct sPAPRMachineState {
103748abce9SEduardo Habkost     /*< private >*/
104748abce9SEduardo Habkost     MachineState parent_obj;
10523825581SEduardo Habkost 
10623825581SEduardo Habkost     /*< public >*/
10723825581SEduardo Habkost     char *kvm_type;
108748abce9SEduardo Habkost };
109748abce9SEduardo Habkost 
11053018216SPaolo Bonzini sPAPREnvironment *spapr;
11153018216SPaolo Bonzini 
112c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers,
11334f2af3dSMarkus Armbruster                                   int nr_irqs, Error **errp)
114c04d6cfaSAnthony Liguori {
11534f2af3dSMarkus Armbruster     Error *err = NULL;
116c04d6cfaSAnthony Liguori     DeviceState *dev;
117c04d6cfaSAnthony Liguori 
118c04d6cfaSAnthony Liguori     dev = qdev_create(NULL, type);
119c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
120c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
12134f2af3dSMarkus Armbruster     object_property_set_bool(OBJECT(dev), true, "realized", &err);
12234f2af3dSMarkus Armbruster     if (err) {
12334f2af3dSMarkus Armbruster         error_propagate(errp, err);
12434f2af3dSMarkus Armbruster         object_unparent(OBJECT(dev));
125c04d6cfaSAnthony Liguori         return NULL;
126c04d6cfaSAnthony Liguori     }
1275a3d7b23SAlexey Kardashevskiy     return XICS_COMMON(dev);
128c04d6cfaSAnthony Liguori }
129c04d6cfaSAnthony Liguori 
130446f16a6SMarcel Apfelbaum static XICSState *xics_system_init(MachineState *machine,
131446f16a6SMarcel Apfelbaum                                    int nr_servers, int nr_irqs)
132c04d6cfaSAnthony Liguori {
133c04d6cfaSAnthony Liguori     XICSState *icp = NULL;
134c04d6cfaSAnthony Liguori 
13511ad93f6SDavid Gibson     if (kvm_enabled()) {
13634f2af3dSMarkus Armbruster         Error *err = NULL;
13734f2af3dSMarkus Armbruster 
138446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_allowed(machine)) {
13934f2af3dSMarkus Armbruster             icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs, &err);
14011ad93f6SDavid Gibson         }
141446f16a6SMarcel Apfelbaum         if (machine_kernel_irqchip_required(machine) && !icp) {
14234f2af3dSMarkus Armbruster             error_report("kernel_irqchip requested but unavailable: %s",
14334f2af3dSMarkus Armbruster                          error_get_pretty(err));
14411ad93f6SDavid Gibson         }
14511ad93f6SDavid Gibson     }
14611ad93f6SDavid Gibson 
14711ad93f6SDavid Gibson     if (!icp) {
14834f2af3dSMarkus Armbruster         icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs, &error_abort);
149c04d6cfaSAnthony Liguori     }
150c04d6cfaSAnthony Liguori 
151c04d6cfaSAnthony Liguori     return icp;
152c04d6cfaSAnthony Liguori }
153c04d6cfaSAnthony Liguori 
154833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
155833d4668SAlexey Kardashevskiy                                   int smt_threads)
156833d4668SAlexey Kardashevskiy {
157833d4668SAlexey Kardashevskiy     int i, ret = 0;
158833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
159833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
160833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
161833d4668SAlexey Kardashevskiy 
1626d9412eaSAlexey Kardashevskiy     if (cpu->cpu_version) {
1634bce526eSLaurent Dufour         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version);
1646d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1656d9412eaSAlexey Kardashevskiy             return ret;
1666d9412eaSAlexey Kardashevskiy         }
1676d9412eaSAlexey Kardashevskiy     }
1686d9412eaSAlexey Kardashevskiy 
169833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
170833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
171833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
172833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
173833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
174833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
175833d4668SAlexey Kardashevskiy     }
176833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
177833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
178833d4668SAlexey Kardashevskiy     if (ret < 0) {
179833d4668SAlexey Kardashevskiy         return ret;
180833d4668SAlexey Kardashevskiy     }
181833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
182833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
183833d4668SAlexey Kardashevskiy 
184833d4668SAlexey Kardashevskiy     return ret;
185833d4668SAlexey Kardashevskiy }
186833d4668SAlexey Kardashevskiy 
18753018216SPaolo Bonzini static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr)
18853018216SPaolo Bonzini {
18982677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
19082677ed2SAlexey Kardashevskiy     CPUState *cs;
19153018216SPaolo Bonzini     char cpu_model[32];
19253018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
19353018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
19453018216SPaolo Bonzini 
19582677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
19682677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
19782677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
19882677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
19953018216SPaolo Bonzini         uint32_t associativity[] = {cpu_to_be32(0x5),
20053018216SPaolo Bonzini                                     cpu_to_be32(0x0),
20153018216SPaolo Bonzini                                     cpu_to_be32(0x0),
20253018216SPaolo Bonzini                                     cpu_to_be32(0x0),
20382677ed2SAlexey Kardashevskiy                                     cpu_to_be32(cs->numa_node),
2040f20ba62SAlexey Kardashevskiy                                     cpu_to_be32(index)};
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 
22853018216SPaolo Bonzini         if (nb_numa_nodes > 1) {
22953018216SPaolo Bonzini             ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
23053018216SPaolo Bonzini                               sizeof(associativity));
23153018216SPaolo Bonzini             if (ret < 0) {
23253018216SPaolo Bonzini                 return ret;
23353018216SPaolo Bonzini             }
23453018216SPaolo Bonzini         }
23553018216SPaolo Bonzini 
23653018216SPaolo Bonzini         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
23753018216SPaolo Bonzini                           pft_size_prop, sizeof(pft_size_prop));
23853018216SPaolo Bonzini         if (ret < 0) {
23953018216SPaolo Bonzini             return ret;
24053018216SPaolo Bonzini         }
241833d4668SAlexey Kardashevskiy 
24282677ed2SAlexey Kardashevskiy         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
2432a48d993SAlexey Kardashevskiy                                      ppc_get_compat_smt_threads(cpu));
244833d4668SAlexey Kardashevskiy         if (ret < 0) {
245833d4668SAlexey Kardashevskiy             return ret;
246833d4668SAlexey Kardashevskiy         }
24753018216SPaolo Bonzini     }
24853018216SPaolo Bonzini     return ret;
24953018216SPaolo Bonzini }
25053018216SPaolo Bonzini 
25153018216SPaolo Bonzini 
25253018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
25353018216SPaolo Bonzini                                      size_t maxsize)
25453018216SPaolo Bonzini {
25553018216SPaolo Bonzini     size_t maxcells = maxsize / sizeof(uint32_t);
25653018216SPaolo Bonzini     int i, j, count;
25753018216SPaolo Bonzini     uint32_t *p = prop;
25853018216SPaolo Bonzini 
25953018216SPaolo Bonzini     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
26053018216SPaolo Bonzini         struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
26153018216SPaolo Bonzini 
26253018216SPaolo Bonzini         if (!sps->page_shift) {
26353018216SPaolo Bonzini             break;
26453018216SPaolo Bonzini         }
26553018216SPaolo Bonzini         for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
26653018216SPaolo Bonzini             if (sps->enc[count].page_shift == 0) {
26753018216SPaolo Bonzini                 break;
26853018216SPaolo Bonzini             }
26953018216SPaolo Bonzini         }
27053018216SPaolo Bonzini         if ((p - prop) >= (maxcells - 3 - count * 2)) {
27153018216SPaolo Bonzini             break;
27253018216SPaolo Bonzini         }
27353018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->page_shift);
27453018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->slb_enc);
27553018216SPaolo Bonzini         *(p++) = cpu_to_be32(count);
27653018216SPaolo Bonzini         for (j = 0; j < count; j++) {
27753018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].page_shift);
27853018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
27953018216SPaolo Bonzini         }
28053018216SPaolo Bonzini     }
28153018216SPaolo Bonzini 
28253018216SPaolo Bonzini     return (p - prop) * sizeof(uint32_t);
28353018216SPaolo Bonzini }
28453018216SPaolo Bonzini 
285b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
286b082d65aSAlexey Kardashevskiy {
287b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
288b082d65aSAlexey Kardashevskiy         int i;
289b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
290b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
291b082d65aSAlexey Kardashevskiy                 return MIN(pow2floor(numa_info[i].node_mem), ram_size);
292b082d65aSAlexey Kardashevskiy             }
293b082d65aSAlexey Kardashevskiy         }
294b082d65aSAlexey Kardashevskiy     }
295b082d65aSAlexey Kardashevskiy     return 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;
321182735efSAndreas Färber     CPUState *cs;
32253018216SPaolo Bonzini     uint32_t start_prop = cpu_to_be32(initrd_base);
32353018216SPaolo Bonzini     uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
324a1d59c0fSAlexey Kardashevskiy     GString *hypertas = g_string_sized_new(256);
325a1d59c0fSAlexey Kardashevskiy     GString *qemu_hypertas = g_string_sized_new(256);
32653018216SPaolo Bonzini     uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
32753018216SPaolo Bonzini     uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
328833d4668SAlexey Kardashevskiy     int smt = kvmppc_smt_threads();
32953018216SPaolo Bonzini     unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
33010582ff8SAlexey Kardashevskiy     QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
33110582ff8SAlexey Kardashevskiy     unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
33210582ff8SAlexey Kardashevskiy     uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
333ef951443SNikunj A Dadhania     char *buf;
33453018216SPaolo Bonzini 
335a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-pft");
336a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-term");
337a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-dabr");
338a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-interrupt");
339a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-tce");
340a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-vio");
341a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-splpar");
342a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-bulk");
343a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-set-mode");
344a1d59c0fSAlexey Kardashevskiy     add_str(qemu_hypertas, "hcall-memop1");
345a1d59c0fSAlexey Kardashevskiy 
34653018216SPaolo Bonzini     fdt = g_malloc0(FDT_MAX_SIZE);
34753018216SPaolo Bonzini     _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
34853018216SPaolo Bonzini 
34953018216SPaolo Bonzini     if (kernel_size) {
35053018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
35153018216SPaolo Bonzini     }
35253018216SPaolo Bonzini     if (initrd_size) {
35353018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
35453018216SPaolo Bonzini     }
35553018216SPaolo Bonzini     _FDT((fdt_finish_reservemap(fdt)));
35653018216SPaolo Bonzini 
35753018216SPaolo Bonzini     /* Root node */
35853018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "")));
35953018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "chrp")));
36053018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
361fa388916SAnthony Liguori     _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
36253018216SPaolo Bonzini 
363ef951443SNikunj A Dadhania     /*
364ef951443SNikunj A Dadhania      * Add info to guest to indentify which host is it being run on
365ef951443SNikunj A Dadhania      * and what is the uuid of the guest
366ef951443SNikunj A Dadhania      */
367ef951443SNikunj A Dadhania     if (kvmppc_get_host_model(&buf)) {
368ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-model", buf)));
369ef951443SNikunj A Dadhania         g_free(buf);
370ef951443SNikunj A Dadhania     }
371ef951443SNikunj A Dadhania     if (kvmppc_get_host_serial(&buf)) {
372ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-serial", buf)));
373ef951443SNikunj A Dadhania         g_free(buf);
374ef951443SNikunj A Dadhania     }
375ef951443SNikunj A Dadhania 
376ef951443SNikunj A Dadhania     buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1],
377ef951443SNikunj A Dadhania                           qemu_uuid[2], qemu_uuid[3], qemu_uuid[4],
378ef951443SNikunj A Dadhania                           qemu_uuid[5], qemu_uuid[6], qemu_uuid[7],
379ef951443SNikunj A Dadhania                           qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
380ef951443SNikunj A Dadhania                           qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
381ef951443SNikunj A Dadhania                           qemu_uuid[14], qemu_uuid[15]);
382ef951443SNikunj A Dadhania 
383ef951443SNikunj A Dadhania     _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
384ef951443SNikunj A Dadhania     g_free(buf);
385ef951443SNikunj A Dadhania 
38653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
38753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
38853018216SPaolo Bonzini 
38953018216SPaolo Bonzini     /* /chosen */
39053018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "chosen")));
39153018216SPaolo Bonzini 
39253018216SPaolo Bonzini     /* Set Form1_affinity */
39353018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
39453018216SPaolo Bonzini 
39553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
39653018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-start",
39753018216SPaolo Bonzini                        &start_prop, sizeof(start_prop))));
39853018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-end",
39953018216SPaolo Bonzini                        &end_prop, sizeof(end_prop))));
40053018216SPaolo Bonzini     if (kernel_size) {
40153018216SPaolo Bonzini         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
40253018216SPaolo Bonzini                               cpu_to_be64(kernel_size) };
40353018216SPaolo Bonzini 
40453018216SPaolo Bonzini         _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
40516457e7fSBenjamin Herrenschmidt         if (little_endian) {
40616457e7fSBenjamin Herrenschmidt             _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0)));
40716457e7fSBenjamin Herrenschmidt         }
40853018216SPaolo Bonzini     }
409cc84c0f3SAvik Sil     if (boot_menu) {
410cc84c0f3SAvik Sil         _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu)));
411cc84c0f3SAvik Sil     }
41253018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
41353018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
41453018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
41553018216SPaolo Bonzini 
41653018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
41753018216SPaolo Bonzini 
41853018216SPaolo Bonzini     /* cpus */
41953018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "cpus")));
42053018216SPaolo Bonzini 
42153018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
42253018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
42353018216SPaolo Bonzini 
424bdc44640SAndreas Färber     CPU_FOREACH(cs) {
425182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
426182735efSAndreas Färber         CPUPPCState *env = &cpu->env;
4273bbf37f2SAndreas Färber         DeviceClass *dc = DEVICE_GET_CLASS(cs);
428182735efSAndreas Färber         PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4290f20ba62SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
43053018216SPaolo Bonzini         char *nodename;
43153018216SPaolo Bonzini         uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
43253018216SPaolo Bonzini                            0xffffffff, 0xffffffff};
43353018216SPaolo Bonzini         uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
43453018216SPaolo Bonzini         uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
43553018216SPaolo Bonzini         uint32_t page_sizes_prop[64];
43653018216SPaolo Bonzini         size_t page_sizes_prop_size;
43753018216SPaolo Bonzini 
43853018216SPaolo Bonzini         if ((index % smt) != 0) {
43953018216SPaolo Bonzini             continue;
44053018216SPaolo Bonzini         }
44153018216SPaolo Bonzini 
4423bbf37f2SAndreas Färber         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
44353018216SPaolo Bonzini 
44453018216SPaolo Bonzini         _FDT((fdt_begin_node(fdt, nodename)));
44553018216SPaolo Bonzini 
44653018216SPaolo Bonzini         g_free(nodename);
44753018216SPaolo Bonzini 
44853018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "reg", index)));
44953018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "device_type", "cpu")));
45053018216SPaolo Bonzini 
45153018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
4520cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "d-cache-block-size",
45353018216SPaolo Bonzini                                 env->dcache_line_size)));
4540cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "d-cache-line-size",
4550cbad81fSDavid Gibson                                 env->dcache_line_size)));
4560cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "i-cache-block-size",
45753018216SPaolo Bonzini                                 env->icache_line_size)));
4580cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "i-cache-line-size",
4590cbad81fSDavid Gibson                                 env->icache_line_size)));
4600cbad81fSDavid Gibson 
4610cbad81fSDavid Gibson         if (pcc->l1_dcache_size) {
4620cbad81fSDavid Gibson             _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size)));
4630cbad81fSDavid Gibson         } else {
4640cbad81fSDavid Gibson             fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");
4650cbad81fSDavid Gibson         }
4660cbad81fSDavid Gibson         if (pcc->l1_icache_size) {
4670cbad81fSDavid Gibson             _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size)));
4680cbad81fSDavid Gibson         } else {
4690cbad81fSDavid Gibson             fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n");
4700cbad81fSDavid Gibson         }
4710cbad81fSDavid Gibson 
47253018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
47353018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
47453018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
47553018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "status", "okay")));
47653018216SPaolo Bonzini         _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
47753018216SPaolo Bonzini 
478dcb861cbSAlexey Kardashevskiy         if (env->spr_cb[SPR_PURR].oea_read) {
479dcb861cbSAlexey Kardashevskiy             _FDT((fdt_property(fdt, "ibm,purr", NULL, 0)));
480dcb861cbSAlexey Kardashevskiy         }
481dcb861cbSAlexey Kardashevskiy 
48253018216SPaolo Bonzini         if (env->mmu_model & POWERPC_MMU_1TSEG) {
48353018216SPaolo Bonzini             _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
48453018216SPaolo Bonzini                                segs, sizeof(segs))));
48553018216SPaolo Bonzini         }
48653018216SPaolo Bonzini 
48753018216SPaolo Bonzini         /* Advertise VMX/VSX (vector extensions) if available
48853018216SPaolo Bonzini          *   0 / no property == no vector extensions
48953018216SPaolo Bonzini          *   1               == VMX / Altivec available
49053018216SPaolo Bonzini          *   2               == VSX available */
49153018216SPaolo Bonzini         if (env->insns_flags & PPC_ALTIVEC) {
49253018216SPaolo Bonzini             uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
49353018216SPaolo Bonzini 
49453018216SPaolo Bonzini             _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
49553018216SPaolo Bonzini         }
49653018216SPaolo Bonzini 
49753018216SPaolo Bonzini         /* Advertise DFP (Decimal Floating Point) if available
49853018216SPaolo Bonzini          *   0 / no property == no DFP
49953018216SPaolo Bonzini          *   1               == DFP available */
50053018216SPaolo Bonzini         if (env->insns_flags2 & PPC2_DFP) {
50153018216SPaolo Bonzini             _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
50253018216SPaolo Bonzini         }
50353018216SPaolo Bonzini 
50453018216SPaolo Bonzini         page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
50553018216SPaolo Bonzini                                                       sizeof(page_sizes_prop));
50653018216SPaolo Bonzini         if (page_sizes_prop_size) {
50753018216SPaolo Bonzini             _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
50853018216SPaolo Bonzini                                page_sizes_prop, page_sizes_prop_size)));
50953018216SPaolo Bonzini         }
51053018216SPaolo Bonzini 
51110582ff8SAlexey Kardashevskiy         _FDT((fdt_property_cell(fdt, "ibm,chip-id",
51210582ff8SAlexey Kardashevskiy                                 cs->cpu_index / cpus_per_socket)));
51310582ff8SAlexey Kardashevskiy 
51453018216SPaolo Bonzini         _FDT((fdt_end_node(fdt)));
51553018216SPaolo Bonzini     }
51653018216SPaolo Bonzini 
51753018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
51853018216SPaolo Bonzini 
51953018216SPaolo Bonzini     /* RTAS */
52053018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "rtas")));
52153018216SPaolo Bonzini 
522da95324eSAlexey Kardashevskiy     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
523da95324eSAlexey Kardashevskiy         add_str(hypertas, "hcall-multi-tce");
524da95324eSAlexey Kardashevskiy     }
525a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str,
526a1d59c0fSAlexey Kardashevskiy                        hypertas->len)));
527a1d59c0fSAlexey Kardashevskiy     g_string_free(hypertas, TRUE);
528a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str,
529a1d59c0fSAlexey Kardashevskiy                        qemu_hypertas->len)));
530a1d59c0fSAlexey Kardashevskiy     g_string_free(qemu_hypertas, TRUE);
53153018216SPaolo Bonzini 
53253018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
53353018216SPaolo Bonzini         refpoints, sizeof(refpoints))));
53453018216SPaolo Bonzini 
53553018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
53653018216SPaolo Bonzini 
5372e14072fSNikunj A Dadhania     /*
5389d632f5fSzhanghailiang      * According to PAPR, rtas ibm,os-term does not guarantee a return
5392e14072fSNikunj A Dadhania      * back to the guest cpu.
5402e14072fSNikunj A Dadhania      *
5412e14072fSNikunj A Dadhania      * While an additional ibm,extended-os-term property indicates that
5422e14072fSNikunj A Dadhania      * rtas call return will always occur. Set this property.
5432e14072fSNikunj A Dadhania      */
5442e14072fSNikunj A Dadhania     _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0)));
5452e14072fSNikunj A Dadhania 
54653018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
54753018216SPaolo Bonzini 
54853018216SPaolo Bonzini     /* interrupt controller */
54953018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "interrupt-controller")));
55053018216SPaolo Bonzini 
55153018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type",
55253018216SPaolo Bonzini                               "PowerPC-External-Interrupt-Presentation")));
55353018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
55453018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
55553018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
55653018216SPaolo Bonzini                        interrupt_server_ranges_prop,
55753018216SPaolo Bonzini                        sizeof(interrupt_server_ranges_prop))));
55853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
55953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
56053018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
56153018216SPaolo Bonzini 
56253018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
56353018216SPaolo Bonzini 
56453018216SPaolo Bonzini     /* vdevice */
56553018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "vdevice")));
56653018216SPaolo Bonzini 
56753018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
56853018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
56953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
57053018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
57153018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
57253018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
57353018216SPaolo Bonzini 
57453018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
57553018216SPaolo Bonzini 
57653018216SPaolo Bonzini     /* event-sources */
57753018216SPaolo Bonzini     spapr_events_fdt_skel(fdt, epow_irq);
57853018216SPaolo Bonzini 
579f7d69146SAlexander Graf     /* /hypervisor node */
580f7d69146SAlexander Graf     if (kvm_enabled()) {
581f7d69146SAlexander Graf         uint8_t hypercall[16];
582f7d69146SAlexander Graf 
583f7d69146SAlexander Graf         /* indicate KVM hypercall interface */
584f7d69146SAlexander Graf         _FDT((fdt_begin_node(fdt, "hypervisor")));
585f7d69146SAlexander Graf         _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
586f7d69146SAlexander Graf         if (kvmppc_has_cap_fixup_hcalls()) {
587f7d69146SAlexander Graf             /*
588f7d69146SAlexander Graf              * Older KVM versions with older guest kernels were broken with the
589f7d69146SAlexander Graf              * magic page, don't allow the guest to map it.
590f7d69146SAlexander Graf              */
591f7d69146SAlexander Graf             kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
592f7d69146SAlexander Graf                                  sizeof(hypercall));
593f7d69146SAlexander Graf             _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
594f7d69146SAlexander Graf                               sizeof(hypercall))));
595f7d69146SAlexander Graf         }
596f7d69146SAlexander Graf         _FDT((fdt_end_node(fdt)));
597f7d69146SAlexander Graf     }
598f7d69146SAlexander Graf 
59953018216SPaolo Bonzini     _FDT((fdt_end_node(fdt))); /* close root node */
60053018216SPaolo Bonzini     _FDT((fdt_finish(fdt)));
60153018216SPaolo Bonzini 
60253018216SPaolo Bonzini     return fdt;
60353018216SPaolo Bonzini }
60453018216SPaolo Bonzini 
6052a6593cbSAlexey Kardashevskiy int spapr_h_cas_compose_response(target_ulong addr, target_ulong size)
6062a6593cbSAlexey Kardashevskiy {
6072a6593cbSAlexey Kardashevskiy     void *fdt, *fdt_skel;
6082a6593cbSAlexey Kardashevskiy     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
6092a6593cbSAlexey Kardashevskiy 
6102a6593cbSAlexey Kardashevskiy     size -= sizeof(hdr);
6112a6593cbSAlexey Kardashevskiy 
6122a6593cbSAlexey Kardashevskiy     /* Create sceleton */
6132a6593cbSAlexey Kardashevskiy     fdt_skel = g_malloc0(size);
6142a6593cbSAlexey Kardashevskiy     _FDT((fdt_create(fdt_skel, size)));
6152a6593cbSAlexey Kardashevskiy     _FDT((fdt_begin_node(fdt_skel, "")));
6162a6593cbSAlexey Kardashevskiy     _FDT((fdt_end_node(fdt_skel)));
6172a6593cbSAlexey Kardashevskiy     _FDT((fdt_finish(fdt_skel)));
6182a6593cbSAlexey Kardashevskiy     fdt = g_malloc0(size);
6192a6593cbSAlexey Kardashevskiy     _FDT((fdt_open_into(fdt_skel, fdt, size)));
6202a6593cbSAlexey Kardashevskiy     g_free(fdt_skel);
6212a6593cbSAlexey Kardashevskiy 
6223794d548SAlexey Kardashevskiy     /* Fix skeleton up */
6233794d548SAlexey Kardashevskiy     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
6242a6593cbSAlexey Kardashevskiy 
6252a6593cbSAlexey Kardashevskiy     /* Pack resulting tree */
6262a6593cbSAlexey Kardashevskiy     _FDT((fdt_pack(fdt)));
6272a6593cbSAlexey Kardashevskiy 
6282a6593cbSAlexey Kardashevskiy     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
6292a6593cbSAlexey Kardashevskiy         trace_spapr_cas_failed(size);
6302a6593cbSAlexey Kardashevskiy         return -1;
6312a6593cbSAlexey Kardashevskiy     }
6322a6593cbSAlexey Kardashevskiy 
6332a6593cbSAlexey Kardashevskiy     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
6342a6593cbSAlexey Kardashevskiy     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
6352a6593cbSAlexey Kardashevskiy     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
6362a6593cbSAlexey Kardashevskiy     g_free(fdt);
6372a6593cbSAlexey Kardashevskiy 
6382a6593cbSAlexey Kardashevskiy     return 0;
6392a6593cbSAlexey Kardashevskiy }
6402a6593cbSAlexey Kardashevskiy 
64126a8c353SAlexey Kardashevskiy static void spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
64226a8c353SAlexey Kardashevskiy                                        hwaddr size)
64326a8c353SAlexey Kardashevskiy {
64426a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
64526a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
64626a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
647c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
64826a8c353SAlexey Kardashevskiy     };
64926a8c353SAlexey Kardashevskiy     char mem_name[32];
65026a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
65126a8c353SAlexey Kardashevskiy     int off;
65226a8c353SAlexey Kardashevskiy 
65326a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
65426a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
65526a8c353SAlexey Kardashevskiy 
65626a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
65726a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
65826a8c353SAlexey Kardashevskiy     _FDT(off);
65926a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
66026a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
66126a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
66226a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
66326a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
66426a8c353SAlexey Kardashevskiy }
66526a8c353SAlexey Kardashevskiy 
66653018216SPaolo Bonzini static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt)
66753018216SPaolo Bonzini {
6687db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
6697db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
6707db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
6717db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
67253018216SPaolo Bonzini 
6737db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
6747db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
6757db8a127SAlexey Kardashevskiy         nb_nodes = 1;
6767db8a127SAlexey Kardashevskiy         ramnode.node_mem = ram_size;
6777db8a127SAlexey Kardashevskiy         nodes = &ramnode;
6785fe269b1SPaul Mackerras     }
67953018216SPaolo Bonzini 
6807db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
6817db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
6827db8a127SAlexey Kardashevskiy             continue;
68353018216SPaolo Bonzini         }
6845fe269b1SPaul Mackerras         if (mem_start >= ram_size) {
6855fe269b1SPaul Mackerras             node_size = 0;
6865fe269b1SPaul Mackerras         } else {
6877db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
6885fe269b1SPaul Mackerras             if (node_size > ram_size - mem_start) {
6895fe269b1SPaul Mackerras                 node_size = ram_size - mem_start;
6905fe269b1SPaul Mackerras             }
6915fe269b1SPaul Mackerras         }
6927db8a127SAlexey Kardashevskiy         if (!mem_start) {
6937db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
6947db8a127SAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
6957db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
6967db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
6977db8a127SAlexey Kardashevskiy         }
6986010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
6996010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
7006010818cSAlexey Kardashevskiy 
7016010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
7026010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
7036010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
7046010818cSAlexey Kardashevskiy             }
7056010818cSAlexey Kardashevskiy 
7066010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
7076010818cSAlexey Kardashevskiy             node_size -= sizetmp;
7086010818cSAlexey Kardashevskiy             mem_start += sizetmp;
7096010818cSAlexey Kardashevskiy         }
71053018216SPaolo Bonzini     }
71153018216SPaolo Bonzini 
71253018216SPaolo Bonzini     return 0;
71353018216SPaolo Bonzini }
71453018216SPaolo Bonzini 
71553018216SPaolo Bonzini static void spapr_finalize_fdt(sPAPREnvironment *spapr,
71653018216SPaolo Bonzini                                hwaddr fdt_addr,
71753018216SPaolo Bonzini                                hwaddr rtas_addr,
71853018216SPaolo Bonzini                                hwaddr rtas_size)
71953018216SPaolo Bonzini {
7205b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
7215b2128d2SAlexander Graf     const char *boot_device = machine->boot_order;
72271461b0fSAlexey Kardashevskiy     int ret, i;
72371461b0fSAlexey Kardashevskiy     size_t cb = 0;
72471461b0fSAlexey Kardashevskiy     char *bootlist;
72553018216SPaolo Bonzini     void *fdt;
72653018216SPaolo Bonzini     sPAPRPHBState *phb;
72753018216SPaolo Bonzini 
72853018216SPaolo Bonzini     fdt = g_malloc(FDT_MAX_SIZE);
72953018216SPaolo Bonzini 
73053018216SPaolo Bonzini     /* open out the base tree into a temp buffer for the final tweaks */
73153018216SPaolo Bonzini     _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
73253018216SPaolo Bonzini 
73353018216SPaolo Bonzini     ret = spapr_populate_memory(spapr, fdt);
73453018216SPaolo Bonzini     if (ret < 0) {
73553018216SPaolo Bonzini         fprintf(stderr, "couldn't setup memory nodes in fdt\n");
73653018216SPaolo Bonzini         exit(1);
73753018216SPaolo Bonzini     }
73853018216SPaolo Bonzini 
73953018216SPaolo Bonzini     ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
74053018216SPaolo Bonzini     if (ret < 0) {
74153018216SPaolo Bonzini         fprintf(stderr, "couldn't setup vio devices in fdt\n");
74253018216SPaolo Bonzini         exit(1);
74353018216SPaolo Bonzini     }
74453018216SPaolo Bonzini 
74553018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
74653018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
74753018216SPaolo Bonzini     }
74853018216SPaolo Bonzini 
74953018216SPaolo Bonzini     if (ret < 0) {
75053018216SPaolo Bonzini         fprintf(stderr, "couldn't setup PCI devices in fdt\n");
75153018216SPaolo Bonzini         exit(1);
75253018216SPaolo Bonzini     }
75353018216SPaolo Bonzini 
75453018216SPaolo Bonzini     /* RTAS */
75553018216SPaolo Bonzini     ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
75653018216SPaolo Bonzini     if (ret < 0) {
75753018216SPaolo Bonzini         fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
75853018216SPaolo Bonzini     }
75953018216SPaolo Bonzini 
76053018216SPaolo Bonzini     /* Advertise NUMA via ibm,associativity */
76153018216SPaolo Bonzini     ret = spapr_fixup_cpu_dt(fdt, spapr);
76253018216SPaolo Bonzini     if (ret < 0) {
76353018216SPaolo Bonzini         fprintf(stderr, "Couldn't finalize CPU device tree properties\n");
76453018216SPaolo Bonzini     }
76553018216SPaolo Bonzini 
76671461b0fSAlexey Kardashevskiy     bootlist = get_boot_devices_list(&cb, true);
76771461b0fSAlexey Kardashevskiy     if (cb && bootlist) {
76871461b0fSAlexey Kardashevskiy         int offset = fdt_path_offset(fdt, "/chosen");
76971461b0fSAlexey Kardashevskiy         if (offset < 0) {
77071461b0fSAlexey Kardashevskiy             exit(1);
77171461b0fSAlexey Kardashevskiy         }
77271461b0fSAlexey Kardashevskiy         for (i = 0; i < cb; i++) {
77371461b0fSAlexey Kardashevskiy             if (bootlist[i] == '\n') {
77471461b0fSAlexey Kardashevskiy                 bootlist[i] = ' ';
77571461b0fSAlexey Kardashevskiy             }
77671461b0fSAlexey Kardashevskiy 
77771461b0fSAlexey Kardashevskiy         }
77871461b0fSAlexey Kardashevskiy         ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist);
77971461b0fSAlexey Kardashevskiy     }
78071461b0fSAlexey Kardashevskiy 
7815b2128d2SAlexander Graf     if (boot_device && strlen(boot_device)) {
7825b2128d2SAlexander Graf         int offset = fdt_path_offset(fdt, "/chosen");
7835b2128d2SAlexander Graf 
7845b2128d2SAlexander Graf         if (offset < 0) {
7855b2128d2SAlexander Graf             exit(1);
7865b2128d2SAlexander Graf         }
7875b2128d2SAlexander Graf         fdt_setprop_string(fdt, offset, "qemu,boot-device", boot_device);
7885b2128d2SAlexander Graf     }
7895b2128d2SAlexander Graf 
79053018216SPaolo Bonzini     if (!spapr->has_graphics) {
79153018216SPaolo Bonzini         spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
79253018216SPaolo Bonzini     }
79353018216SPaolo Bonzini 
79453018216SPaolo Bonzini     _FDT((fdt_pack(fdt)));
79553018216SPaolo Bonzini 
79653018216SPaolo Bonzini     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
797730fce59SThomas Huth         error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
79853018216SPaolo Bonzini                      fdt_totalsize(fdt), FDT_MAX_SIZE);
79953018216SPaolo Bonzini         exit(1);
80053018216SPaolo Bonzini     }
80153018216SPaolo Bonzini 
80253018216SPaolo Bonzini     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
80353018216SPaolo Bonzini 
804a21a7a70SGonglei     g_free(bootlist);
80553018216SPaolo Bonzini     g_free(fdt);
80653018216SPaolo Bonzini }
80753018216SPaolo Bonzini 
80853018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
80953018216SPaolo Bonzini {
81053018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
81153018216SPaolo Bonzini }
81253018216SPaolo Bonzini 
81353018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu)
81453018216SPaolo Bonzini {
81553018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
81653018216SPaolo Bonzini 
81753018216SPaolo Bonzini     if (msr_pr) {
81853018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
81953018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
82053018216SPaolo Bonzini     } else {
82153018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
82253018216SPaolo Bonzini     }
82353018216SPaolo Bonzini }
82453018216SPaolo Bonzini 
825e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
826e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
827e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
828e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
829e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
830e6b8fd24SSamuel Mendoza-Jonas 
83153018216SPaolo Bonzini static void spapr_reset_htab(sPAPREnvironment *spapr)
83253018216SPaolo Bonzini {
83353018216SPaolo Bonzini     long shift;
834e6b8fd24SSamuel Mendoza-Jonas     int index;
83553018216SPaolo Bonzini 
83653018216SPaolo Bonzini     /* allocate hash page table.  For now we always make this 16mb,
83753018216SPaolo Bonzini      * later we should probably make it scale to the size of guest
83853018216SPaolo Bonzini      * RAM */
83953018216SPaolo Bonzini 
84053018216SPaolo Bonzini     shift = kvmppc_reset_htab(spapr->htab_shift);
84153018216SPaolo Bonzini 
84253018216SPaolo Bonzini     if (shift > 0) {
84353018216SPaolo Bonzini         /* Kernel handles htab, we don't need to allocate one */
84453018216SPaolo Bonzini         spapr->htab_shift = shift;
8457c43bca0SAneesh Kumar K.V         kvmppc_kern_htab = true;
84601a57972SSamuel Mendoza-Jonas 
84701a57972SSamuel Mendoza-Jonas         /* Tell readers to update their file descriptor */
84801a57972SSamuel Mendoza-Jonas         if (spapr->htab_fd >= 0) {
84901a57972SSamuel Mendoza-Jonas             spapr->htab_fd_stale = true;
85001a57972SSamuel Mendoza-Jonas         }
85153018216SPaolo Bonzini     } else {
85253018216SPaolo Bonzini         if (!spapr->htab) {
85353018216SPaolo Bonzini             /* Allocate an htab if we don't yet have one */
85453018216SPaolo Bonzini             spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr));
85553018216SPaolo Bonzini         }
85653018216SPaolo Bonzini 
85753018216SPaolo Bonzini         /* And clear it */
85853018216SPaolo Bonzini         memset(spapr->htab, 0, HTAB_SIZE(spapr));
859e6b8fd24SSamuel Mendoza-Jonas 
860e6b8fd24SSamuel Mendoza-Jonas         for (index = 0; index < HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; index++) {
861e6b8fd24SSamuel Mendoza-Jonas             DIRTY_HPTE(HPTE(spapr->htab, index));
862e6b8fd24SSamuel Mendoza-Jonas         }
86353018216SPaolo Bonzini     }
86453018216SPaolo Bonzini 
86553018216SPaolo Bonzini     /* Update the RMA size if necessary */
86653018216SPaolo Bonzini     if (spapr->vrma_adjust) {
867b082d65aSAlexey Kardashevskiy         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
868b082d65aSAlexey Kardashevskiy                                           spapr->htab_shift);
86953018216SPaolo Bonzini     }
87053018216SPaolo Bonzini }
87153018216SPaolo Bonzini 
8729e3f9733SAlexander Graf static int find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
8739e3f9733SAlexander Graf {
8749e3f9733SAlexander Graf     bool matched = false;
8759e3f9733SAlexander Graf 
8769e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
8779e3f9733SAlexander Graf         matched = true;
8789e3f9733SAlexander Graf     }
8799e3f9733SAlexander Graf 
8809e3f9733SAlexander Graf     if (!matched) {
8819e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
8829e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
8839e3f9733SAlexander Graf         exit(1);
8849e3f9733SAlexander Graf     }
8859e3f9733SAlexander Graf 
8869e3f9733SAlexander Graf     return 0;
8879e3f9733SAlexander Graf }
8889e3f9733SAlexander Graf 
88901a57972SSamuel Mendoza-Jonas /*
89001a57972SSamuel Mendoza-Jonas  * A guest reset will cause spapr->htab_fd to become stale if being used.
89101a57972SSamuel Mendoza-Jonas  * Reopen the file descriptor to make sure the whole HTAB is properly read.
89201a57972SSamuel Mendoza-Jonas  */
89301a57972SSamuel Mendoza-Jonas static int spapr_check_htab_fd(sPAPREnvironment *spapr)
89401a57972SSamuel Mendoza-Jonas {
89501a57972SSamuel Mendoza-Jonas     int rc = 0;
89601a57972SSamuel Mendoza-Jonas 
89701a57972SSamuel Mendoza-Jonas     if (spapr->htab_fd_stale) {
89801a57972SSamuel Mendoza-Jonas         close(spapr->htab_fd);
89901a57972SSamuel Mendoza-Jonas         spapr->htab_fd = kvmppc_get_htab_fd(false);
90001a57972SSamuel Mendoza-Jonas         if (spapr->htab_fd < 0) {
90101a57972SSamuel Mendoza-Jonas             error_report("Unable to open fd for reading hash table from KVM: "
90201a57972SSamuel Mendoza-Jonas                          "%s", strerror(errno));
90301a57972SSamuel Mendoza-Jonas             rc = -1;
90401a57972SSamuel Mendoza-Jonas         }
90501a57972SSamuel Mendoza-Jonas         spapr->htab_fd_stale = false;
90601a57972SSamuel Mendoza-Jonas     }
90701a57972SSamuel Mendoza-Jonas 
90801a57972SSamuel Mendoza-Jonas     return rc;
90901a57972SSamuel Mendoza-Jonas }
91001a57972SSamuel Mendoza-Jonas 
91153018216SPaolo Bonzini static void ppc_spapr_reset(void)
91253018216SPaolo Bonzini {
913182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
914b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
915259186a7SAndreas Färber 
9169e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
9179e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
9189e3f9733SAlexander Graf 
91953018216SPaolo Bonzini     /* Reset the hash table & recalc the RMA */
92053018216SPaolo Bonzini     spapr_reset_htab(spapr);
92153018216SPaolo Bonzini 
92253018216SPaolo Bonzini     qemu_devices_reset();
92353018216SPaolo Bonzini 
924b7d1f77aSBenjamin Herrenschmidt     /*
925b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
926b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
927b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
928b7d1f77aSBenjamin Herrenschmidt      */
929b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
930b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
931b7d1f77aSBenjamin Herrenschmidt     spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
932b7d1f77aSBenjamin Herrenschmidt 
93353018216SPaolo Bonzini     /* Load the fdt */
93453018216SPaolo Bonzini     spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
93553018216SPaolo Bonzini                        spapr->rtas_size);
93653018216SPaolo Bonzini 
937b7d1f77aSBenjamin Herrenschmidt     /* Copy RTAS over */
938b7d1f77aSBenjamin Herrenschmidt     cpu_physical_memory_write(spapr->rtas_addr, spapr->rtas_blob,
939b7d1f77aSBenjamin Herrenschmidt                               spapr->rtas_size);
940b7d1f77aSBenjamin Herrenschmidt 
94153018216SPaolo Bonzini     /* Set up the entry state */
942182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
943182735efSAndreas Färber     first_ppc_cpu->env.gpr[3] = spapr->fdt_addr;
944182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
945182735efSAndreas Färber     first_cpu->halted = 0;
946182735efSAndreas Färber     first_ppc_cpu->env.nip = spapr->entry_point;
94753018216SPaolo Bonzini 
94853018216SPaolo Bonzini }
94953018216SPaolo Bonzini 
95053018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque)
95153018216SPaolo Bonzini {
95253018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
953259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
95453018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
95553018216SPaolo Bonzini 
956259186a7SAndreas Färber     cpu_reset(cs);
95753018216SPaolo Bonzini 
95853018216SPaolo Bonzini     /* All CPUs start halted.  CPU0 is unhalted from the machine level
95953018216SPaolo Bonzini      * reset code and the rest are explicitly started up by the guest
96053018216SPaolo Bonzini      * using an RTAS call */
961259186a7SAndreas Färber     cs->halted = 1;
96253018216SPaolo Bonzini 
96353018216SPaolo Bonzini     env->spr[SPR_HIOR] = 0;
96453018216SPaolo Bonzini 
9654be21d56SDavid Gibson     env->external_htab = (uint8_t *)spapr->htab;
9665736245cSAneesh Kumar K.V     if (kvm_enabled() && !env->external_htab) {
9675736245cSAneesh Kumar K.V         /*
9685736245cSAneesh Kumar K.V          * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte*
9695736245cSAneesh Kumar K.V          * functions do the right thing.
9705736245cSAneesh Kumar K.V          */
9715736245cSAneesh Kumar K.V         env->external_htab = (void *)1;
9725736245cSAneesh Kumar K.V     }
97353018216SPaolo Bonzini     env->htab_base = -1;
974f3c75d42SAneesh Kumar K.V     /*
975f3c75d42SAneesh Kumar K.V      * htab_mask is the mask used to normalize hash value to PTEG index.
976f3c75d42SAneesh Kumar K.V      * htab_shift is log2 of hash table size.
977f3c75d42SAneesh Kumar K.V      * We have 8 hpte per group, and each hpte is 16 bytes.
978f3c75d42SAneesh Kumar K.V      * ie have 128 bytes per hpte entry.
979f3c75d42SAneesh Kumar K.V      */
980f3c75d42SAneesh Kumar K.V     env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1;
981ec4936e1SStefan Weil     env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
98253018216SPaolo Bonzini         (spapr->htab_shift - 18);
98353018216SPaolo Bonzini }
98453018216SPaolo Bonzini 
98553018216SPaolo Bonzini static void spapr_create_nvram(sPAPREnvironment *spapr)
98653018216SPaolo Bonzini {
9872ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
9883978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
98953018216SPaolo Bonzini 
9903978b863SPaolo Bonzini     if (dinfo) {
9914be74634SMarkus Armbruster         qdev_prop_set_drive_nofail(dev, "drive", blk_by_legacy_dinfo(dinfo));
99253018216SPaolo Bonzini     }
99353018216SPaolo Bonzini 
99453018216SPaolo Bonzini     qdev_init_nofail(dev);
99553018216SPaolo Bonzini 
99653018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
99753018216SPaolo Bonzini }
99853018216SPaolo Bonzini 
99928df36a1SDavid Gibson static void spapr_rtc_create(sPAPREnvironment *spapr)
100028df36a1SDavid Gibson {
100128df36a1SDavid Gibson     DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
100228df36a1SDavid Gibson 
100328df36a1SDavid Gibson     qdev_init_nofail(dev);
100428df36a1SDavid Gibson     spapr->rtc = dev;
100574e5ae28SDavid Gibson 
100674e5ae28SDavid Gibson     object_property_add_alias(qdev_get_machine(), "rtc-time",
100774e5ae28SDavid Gibson                               OBJECT(spapr->rtc), "date", NULL);
100828df36a1SDavid Gibson }
100928df36a1SDavid Gibson 
101053018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
101153018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus)
101253018216SPaolo Bonzini {
101353018216SPaolo Bonzini     switch (vga_interface_type) {
101453018216SPaolo Bonzini     case VGA_NONE:
10157effdaa3SMark Wu         return false;
10167effdaa3SMark Wu     case VGA_DEVICE:
10177effdaa3SMark Wu         return true;
101853018216SPaolo Bonzini     case VGA_STD:
101953018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
102053018216SPaolo Bonzini     default:
102153018216SPaolo Bonzini         fprintf(stderr, "This vga model is not supported,"
102253018216SPaolo Bonzini                 "currently it only supports -vga std\n");
102353018216SPaolo Bonzini         exit(0);
102453018216SPaolo Bonzini     }
102553018216SPaolo Bonzini }
102653018216SPaolo Bonzini 
1027880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id)
1028880ae7deSDavid Gibson {
1029880ae7deSDavid Gibson     sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
1030880ae7deSDavid Gibson     int err = 0;
1031880ae7deSDavid Gibson 
1032631b22eaSStefan Weil     /* In earlier versions, there was no separate qdev for the PAPR
1033880ae7deSDavid Gibson      * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1034880ae7deSDavid Gibson      * So when migrating from those versions, poke the incoming offset
1035880ae7deSDavid Gibson      * value into the RTC device */
1036880ae7deSDavid Gibson     if (version_id < 3) {
1037880ae7deSDavid Gibson         err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
1038880ae7deSDavid Gibson     }
1039880ae7deSDavid Gibson 
1040880ae7deSDavid Gibson     return err;
1041880ae7deSDavid Gibson }
1042880ae7deSDavid Gibson 
1043880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id)
1044880ae7deSDavid Gibson {
1045880ae7deSDavid Gibson     return version_id < 3;
1046880ae7deSDavid Gibson }
1047880ae7deSDavid Gibson 
10484be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
10494be21d56SDavid Gibson     .name = "spapr",
1050880ae7deSDavid Gibson     .version_id = 3,
10514be21d56SDavid Gibson     .minimum_version_id = 1,
1052880ae7deSDavid Gibson     .post_load = spapr_post_load,
10534be21d56SDavid Gibson     .fields = (VMStateField[]) {
1054880ae7deSDavid Gibson         /* used to be @next_irq */
1055880ae7deSDavid Gibson         VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
10564be21d56SDavid Gibson 
10574be21d56SDavid Gibson         /* RTC offset */
1058880ae7deSDavid Gibson         VMSTATE_UINT64_TEST(rtc_offset, sPAPREnvironment, version_before_3),
1059880ae7deSDavid Gibson 
106098a8b524SAlexey Kardashevskiy         VMSTATE_PPC_TIMEBASE_V(tb, sPAPREnvironment, 2),
10614be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
10624be21d56SDavid Gibson     },
10634be21d56SDavid Gibson };
10644be21d56SDavid Gibson 
10654be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
10664be21d56SDavid Gibson {
10674be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
10684be21d56SDavid Gibson 
10694be21d56SDavid Gibson     /* "Iteration" header */
10704be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
10714be21d56SDavid Gibson 
1072e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1073e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1074e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1075e68cb8b4SAlexey Kardashevskiy     } else {
1076e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1077e68cb8b4SAlexey Kardashevskiy 
1078e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = kvmppc_get_htab_fd(false);
107901a57972SSamuel Mendoza-Jonas         spapr->htab_fd_stale = false;
1080e68cb8b4SAlexey Kardashevskiy         if (spapr->htab_fd < 0) {
1081e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n",
1082e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1083e68cb8b4SAlexey Kardashevskiy             return -1;
1084e68cb8b4SAlexey Kardashevskiy         }
10854be21d56SDavid Gibson     }
10864be21d56SDavid Gibson 
1087e68cb8b4SAlexey Kardashevskiy 
1088e68cb8b4SAlexey Kardashevskiy     return 0;
1089e68cb8b4SAlexey Kardashevskiy }
10904be21d56SDavid Gibson 
10914be21d56SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr,
10924be21d56SDavid Gibson                                  int64_t max_ns)
10934be21d56SDavid Gibson {
10944be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
10954be21d56SDavid Gibson     int index = spapr->htab_save_index;
1096bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
10974be21d56SDavid Gibson 
10984be21d56SDavid Gibson     assert(spapr->htab_first_pass);
10994be21d56SDavid Gibson 
11004be21d56SDavid Gibson     do {
11014be21d56SDavid Gibson         int chunkstart;
11024be21d56SDavid Gibson 
11034be21d56SDavid Gibson         /* Consume invalid HPTEs */
11044be21d56SDavid Gibson         while ((index < htabslots)
11054be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
11064be21d56SDavid Gibson             index++;
11074be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
11084be21d56SDavid Gibson         }
11094be21d56SDavid Gibson 
11104be21d56SDavid Gibson         /* Consume valid HPTEs */
11114be21d56SDavid Gibson         chunkstart = index;
1112338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
11134be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
11144be21d56SDavid Gibson             index++;
11154be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
11164be21d56SDavid Gibson         }
11174be21d56SDavid Gibson 
11184be21d56SDavid Gibson         if (index > chunkstart) {
11194be21d56SDavid Gibson             int n_valid = index - chunkstart;
11204be21d56SDavid Gibson 
11214be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
11224be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
11234be21d56SDavid Gibson             qemu_put_be16(f, 0);
11244be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
11254be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
11264be21d56SDavid Gibson 
1127bc72ad67SAlex Bligh             if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
11284be21d56SDavid Gibson                 break;
11294be21d56SDavid Gibson             }
11304be21d56SDavid Gibson         }
11314be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
11324be21d56SDavid Gibson 
11334be21d56SDavid Gibson     if (index >= htabslots) {
11344be21d56SDavid Gibson         assert(index == htabslots);
11354be21d56SDavid Gibson         index = 0;
11364be21d56SDavid Gibson         spapr->htab_first_pass = false;
11374be21d56SDavid Gibson     }
11384be21d56SDavid Gibson     spapr->htab_save_index = index;
11394be21d56SDavid Gibson }
11404be21d56SDavid Gibson 
1141e68cb8b4SAlexey Kardashevskiy static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr,
11424be21d56SDavid Gibson                                 int64_t max_ns)
11434be21d56SDavid Gibson {
11444be21d56SDavid Gibson     bool final = max_ns < 0;
11454be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
11464be21d56SDavid Gibson     int examined = 0, sent = 0;
11474be21d56SDavid Gibson     int index = spapr->htab_save_index;
1148bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
11494be21d56SDavid Gibson 
11504be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
11514be21d56SDavid Gibson 
11524be21d56SDavid Gibson     do {
11534be21d56SDavid Gibson         int chunkstart, invalidstart;
11544be21d56SDavid Gibson 
11554be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
11564be21d56SDavid Gibson         while ((index < htabslots)
11574be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
11584be21d56SDavid Gibson             index++;
11594be21d56SDavid Gibson             examined++;
11604be21d56SDavid Gibson         }
11614be21d56SDavid Gibson 
11624be21d56SDavid Gibson         chunkstart = index;
11634be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1164338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
11654be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
11664be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
11674be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
11684be21d56SDavid Gibson             index++;
11694be21d56SDavid Gibson             examined++;
11704be21d56SDavid Gibson         }
11714be21d56SDavid Gibson 
11724be21d56SDavid Gibson         invalidstart = index;
11734be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1174338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
11754be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
11764be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
11774be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
11784be21d56SDavid Gibson             index++;
11794be21d56SDavid Gibson             examined++;
11804be21d56SDavid Gibson         }
11814be21d56SDavid Gibson 
11824be21d56SDavid Gibson         if (index > chunkstart) {
11834be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
11844be21d56SDavid Gibson             int n_invalid = index - invalidstart;
11854be21d56SDavid Gibson 
11864be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
11874be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
11884be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
11894be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
11904be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
11914be21d56SDavid Gibson             sent += index - chunkstart;
11924be21d56SDavid Gibson 
1193bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
11944be21d56SDavid Gibson                 break;
11954be21d56SDavid Gibson             }
11964be21d56SDavid Gibson         }
11974be21d56SDavid Gibson 
11984be21d56SDavid Gibson         if (examined >= htabslots) {
11994be21d56SDavid Gibson             break;
12004be21d56SDavid Gibson         }
12014be21d56SDavid Gibson 
12024be21d56SDavid Gibson         if (index >= htabslots) {
12034be21d56SDavid Gibson             assert(index == htabslots);
12044be21d56SDavid Gibson             index = 0;
12054be21d56SDavid Gibson         }
12064be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
12074be21d56SDavid Gibson 
12084be21d56SDavid Gibson     if (index >= htabslots) {
12094be21d56SDavid Gibson         assert(index == htabslots);
12104be21d56SDavid Gibson         index = 0;
12114be21d56SDavid Gibson     }
12124be21d56SDavid Gibson 
12134be21d56SDavid Gibson     spapr->htab_save_index = index;
12144be21d56SDavid Gibson 
1215e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
12164be21d56SDavid Gibson }
12174be21d56SDavid Gibson 
1218e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1219e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1220e68cb8b4SAlexey Kardashevskiy 
12214be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
12224be21d56SDavid Gibson {
12234be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
1224e68cb8b4SAlexey Kardashevskiy     int rc = 0;
12254be21d56SDavid Gibson 
12264be21d56SDavid Gibson     /* Iteration header */
12274be21d56SDavid Gibson     qemu_put_be32(f, 0);
12284be21d56SDavid Gibson 
1229e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1230e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1231e68cb8b4SAlexey Kardashevskiy 
123201a57972SSamuel Mendoza-Jonas         rc = spapr_check_htab_fd(spapr);
123301a57972SSamuel Mendoza-Jonas         if (rc < 0) {
123401a57972SSamuel Mendoza-Jonas             return rc;
123501a57972SSamuel Mendoza-Jonas         }
123601a57972SSamuel Mendoza-Jonas 
1237e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd,
1238e68cb8b4SAlexey Kardashevskiy                               MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1239e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1240e68cb8b4SAlexey Kardashevskiy             return rc;
1241e68cb8b4SAlexey Kardashevskiy         }
1242e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
12434be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
12444be21d56SDavid Gibson     } else {
1245e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
12464be21d56SDavid Gibson     }
12474be21d56SDavid Gibson 
12484be21d56SDavid Gibson     /* End marker */
12494be21d56SDavid Gibson     qemu_put_be32(f, 0);
12504be21d56SDavid Gibson     qemu_put_be16(f, 0);
12514be21d56SDavid Gibson     qemu_put_be16(f, 0);
12524be21d56SDavid Gibson 
1253e68cb8b4SAlexey Kardashevskiy     return rc;
12544be21d56SDavid Gibson }
12554be21d56SDavid Gibson 
12564be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
12574be21d56SDavid Gibson {
12584be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
12594be21d56SDavid Gibson 
12604be21d56SDavid Gibson     /* Iteration header */
12614be21d56SDavid Gibson     qemu_put_be32(f, 0);
12624be21d56SDavid Gibson 
1263e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1264e68cb8b4SAlexey Kardashevskiy         int rc;
1265e68cb8b4SAlexey Kardashevskiy 
1266e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1267e68cb8b4SAlexey Kardashevskiy 
126801a57972SSamuel Mendoza-Jonas         rc = spapr_check_htab_fd(spapr);
126901a57972SSamuel Mendoza-Jonas         if (rc < 0) {
127001a57972SSamuel Mendoza-Jonas             return rc;
127101a57972SSamuel Mendoza-Jonas         }
127201a57972SSamuel Mendoza-Jonas 
1273e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1);
1274e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1275e68cb8b4SAlexey Kardashevskiy             return rc;
1276e68cb8b4SAlexey Kardashevskiy         }
1277e68cb8b4SAlexey Kardashevskiy         close(spapr->htab_fd);
1278e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = -1;
1279e68cb8b4SAlexey Kardashevskiy     } else {
12804be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1281e68cb8b4SAlexey Kardashevskiy     }
12824be21d56SDavid Gibson 
12834be21d56SDavid Gibson     /* End marker */
12844be21d56SDavid Gibson     qemu_put_be32(f, 0);
12854be21d56SDavid Gibson     qemu_put_be16(f, 0);
12864be21d56SDavid Gibson     qemu_put_be16(f, 0);
12874be21d56SDavid Gibson 
12884be21d56SDavid Gibson     return 0;
12894be21d56SDavid Gibson }
12904be21d56SDavid Gibson 
12914be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
12924be21d56SDavid Gibson {
12934be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
12944be21d56SDavid Gibson     uint32_t section_hdr;
1295e68cb8b4SAlexey Kardashevskiy     int fd = -1;
12964be21d56SDavid Gibson 
12974be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
12984be21d56SDavid Gibson         fprintf(stderr, "htab_load() bad version\n");
12994be21d56SDavid Gibson         return -EINVAL;
13004be21d56SDavid Gibson     }
13014be21d56SDavid Gibson 
13024be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
13034be21d56SDavid Gibson 
13044be21d56SDavid Gibson     if (section_hdr) {
13054be21d56SDavid Gibson         /* First section, just the hash shift */
13064be21d56SDavid Gibson         if (spapr->htab_shift != section_hdr) {
13074be21d56SDavid Gibson             return -EINVAL;
13084be21d56SDavid Gibson         }
13094be21d56SDavid Gibson         return 0;
13104be21d56SDavid Gibson     }
13114be21d56SDavid Gibson 
1312e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1313e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1314e68cb8b4SAlexey Kardashevskiy 
1315e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1316e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
1317e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n",
1318e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1319e68cb8b4SAlexey Kardashevskiy         }
1320e68cb8b4SAlexey Kardashevskiy     }
1321e68cb8b4SAlexey Kardashevskiy 
13224be21d56SDavid Gibson     while (true) {
13234be21d56SDavid Gibson         uint32_t index;
13244be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
13254be21d56SDavid Gibson 
13264be21d56SDavid Gibson         index = qemu_get_be32(f);
13274be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
13284be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
13294be21d56SDavid Gibson 
13304be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
13314be21d56SDavid Gibson             /* End of Stream */
13324be21d56SDavid Gibson             break;
13334be21d56SDavid Gibson         }
13344be21d56SDavid Gibson 
1335e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
13364be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
13374be21d56SDavid Gibson             /* Bad index in stream */
13384be21d56SDavid Gibson             fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) "
1339e68cb8b4SAlexey Kardashevskiy                     "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid,
1340e68cb8b4SAlexey Kardashevskiy                     spapr->htab_shift);
13414be21d56SDavid Gibson             return -EINVAL;
13424be21d56SDavid Gibson         }
13434be21d56SDavid Gibson 
1344e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
13454be21d56SDavid Gibson             if (n_valid) {
13464be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
13474be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
13484be21d56SDavid Gibson             }
13494be21d56SDavid Gibson             if (n_invalid) {
13504be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
13514be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
13524be21d56SDavid Gibson             }
1353e68cb8b4SAlexey Kardashevskiy         } else {
1354e68cb8b4SAlexey Kardashevskiy             int rc;
1355e68cb8b4SAlexey Kardashevskiy 
1356e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1357e68cb8b4SAlexey Kardashevskiy 
1358e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1359e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1360e68cb8b4SAlexey Kardashevskiy                 return rc;
1361e68cb8b4SAlexey Kardashevskiy             }
1362e68cb8b4SAlexey Kardashevskiy         }
1363e68cb8b4SAlexey Kardashevskiy     }
1364e68cb8b4SAlexey Kardashevskiy 
1365e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1366e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1367e68cb8b4SAlexey Kardashevskiy         close(fd);
13684be21d56SDavid Gibson     }
13694be21d56SDavid Gibson 
13704be21d56SDavid Gibson     return 0;
13714be21d56SDavid Gibson }
13724be21d56SDavid Gibson 
13734be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
13744be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
13754be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
13764be21d56SDavid Gibson     .save_live_complete = htab_save_complete,
13774be21d56SDavid Gibson     .load_state = htab_load,
13784be21d56SDavid Gibson };
13794be21d56SDavid Gibson 
13805b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device,
13815b2128d2SAlexander Graf                            Error **errp)
13825b2128d2SAlexander Graf {
13835b2128d2SAlexander Graf     MachineState *machine = MACHINE(qdev_get_machine());
13845b2128d2SAlexander Graf     machine->boot_order = g_strdup(boot_device);
13855b2128d2SAlexander Graf }
13865b2128d2SAlexander Graf 
138753018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
13883ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
138953018216SPaolo Bonzini {
13903ef96221SMarcel Apfelbaum     ram_addr_t ram_size = machine->ram_size;
13913ef96221SMarcel Apfelbaum     const char *cpu_model = machine->cpu_model;
13923ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
13933ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
13943ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
139553018216SPaolo Bonzini     PowerPCCPU *cpu;
139653018216SPaolo Bonzini     CPUPPCState *env;
139753018216SPaolo Bonzini     PCIHostState *phb;
139853018216SPaolo Bonzini     int i;
139953018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
140053018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1401658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1402658fa66bSAlexey Kardashevskiy     void *rma = NULL;
140353018216SPaolo Bonzini     hwaddr rma_alloc_size;
1404b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
140553018216SPaolo Bonzini     uint32_t initrd_base = 0;
140653018216SPaolo Bonzini     long kernel_size = 0, initrd_size = 0;
1407b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
140816457e7fSBenjamin Herrenschmidt     bool kernel_le = false;
140953018216SPaolo Bonzini     char *filename;
141053018216SPaolo Bonzini 
141153018216SPaolo Bonzini     msi_supported = true;
141253018216SPaolo Bonzini 
141353018216SPaolo Bonzini     spapr = g_malloc0(sizeof(*spapr));
141453018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
141553018216SPaolo Bonzini 
141653018216SPaolo Bonzini     cpu_ppc_hypercall = emulate_spapr_hypercall;
141753018216SPaolo Bonzini 
141853018216SPaolo Bonzini     /* Allocate RMA if necessary */
1419658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
142053018216SPaolo Bonzini 
142153018216SPaolo Bonzini     if (rma_alloc_size == -1) {
1422730fce59SThomas Huth         error_report("Unable to create RMA");
142353018216SPaolo Bonzini         exit(1);
142453018216SPaolo Bonzini     }
142553018216SPaolo Bonzini 
1426c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
142753018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
142853018216SPaolo Bonzini     } else {
1429c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
143053018216SPaolo Bonzini 
143153018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
143253018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
143353018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
143453018216SPaolo Bonzini          *
143553018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
143653018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
143753018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
143853018216SPaolo Bonzini          * isn't determined yet.
143953018216SPaolo Bonzini          */
144053018216SPaolo Bonzini         if (kvm_enabled()) {
144153018216SPaolo Bonzini             spapr->vrma_adjust = 1;
144253018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
144353018216SPaolo Bonzini         }
144453018216SPaolo Bonzini     }
144553018216SPaolo Bonzini 
1446c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
1447c4177479SAlexey Kardashevskiy         fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n",
1448c4177479SAlexey Kardashevskiy                 spapr->rma_size);
1449c4177479SAlexey Kardashevskiy         exit(1);
1450c4177479SAlexey Kardashevskiy     }
1451c4177479SAlexey Kardashevskiy 
1452b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1453b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
145453018216SPaolo Bonzini 
145553018216SPaolo Bonzini     /* We aim for a hash table of size 1/128 the size of RAM.  The
145653018216SPaolo Bonzini      * normal rule of thumb is 1/64 the size of RAM, but that's much
145753018216SPaolo Bonzini      * more than needed for the Linux guests we support. */
145853018216SPaolo Bonzini     spapr->htab_shift = 18; /* Minimum architected size */
145953018216SPaolo Bonzini     while (spapr->htab_shift <= 46) {
146053018216SPaolo Bonzini         if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) {
146153018216SPaolo Bonzini             break;
146253018216SPaolo Bonzini         }
146353018216SPaolo Bonzini         spapr->htab_shift++;
146453018216SPaolo Bonzini     }
146553018216SPaolo Bonzini 
14667b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
1467446f16a6SMarcel Apfelbaum     spapr->icp = xics_system_init(machine,
1468446f16a6SMarcel Apfelbaum                                   smp_cpus * kvmppc_smt_threads() / smp_threads,
14697b565160SDavid Gibson                                   XICS_IRQS);
14707b565160SDavid Gibson 
147153018216SPaolo Bonzini     /* init CPUs */
147253018216SPaolo Bonzini     if (cpu_model == NULL) {
147353018216SPaolo Bonzini         cpu_model = kvm_enabled() ? "host" : "POWER7";
147453018216SPaolo Bonzini     }
147553018216SPaolo Bonzini     for (i = 0; i < smp_cpus; i++) {
147653018216SPaolo Bonzini         cpu = cpu_ppc_init(cpu_model);
147753018216SPaolo Bonzini         if (cpu == NULL) {
147853018216SPaolo Bonzini             fprintf(stderr, "Unable to find PowerPC CPU definition\n");
147953018216SPaolo Bonzini             exit(1);
148053018216SPaolo Bonzini         }
148153018216SPaolo Bonzini         env = &cpu->env;
148253018216SPaolo Bonzini 
148353018216SPaolo Bonzini         /* Set time-base frequency to 512 MHz */
148453018216SPaolo Bonzini         cpu_ppc_tb_init(env, TIMEBASE_FREQ);
148553018216SPaolo Bonzini 
14862cf3eb6dSFabien Chouteau         /* PAPR always has exception vectors in RAM not ROM. To ensure this,
14872cf3eb6dSFabien Chouteau          * MSR[IP] should never be set.
14882cf3eb6dSFabien Chouteau          */
14892cf3eb6dSFabien Chouteau         env->msr_mask &= ~(1 << 6);
149053018216SPaolo Bonzini 
149153018216SPaolo Bonzini         /* Tell KVM that we're in PAPR mode */
149253018216SPaolo Bonzini         if (kvm_enabled()) {
149353018216SPaolo Bonzini             kvmppc_set_papr(cpu);
149453018216SPaolo Bonzini         }
149553018216SPaolo Bonzini 
14966d9412eaSAlexey Kardashevskiy         if (cpu->max_compat) {
14976d9412eaSAlexey Kardashevskiy             if (ppc_set_compat(cpu, cpu->max_compat) < 0) {
14986d9412eaSAlexey Kardashevskiy                 exit(1);
14996d9412eaSAlexey Kardashevskiy             }
15006d9412eaSAlexey Kardashevskiy         }
15016d9412eaSAlexey Kardashevskiy 
150224408a7dSAlexey Kardashevskiy         xics_cpu_setup(spapr->icp, cpu);
150324408a7dSAlexey Kardashevskiy 
150453018216SPaolo Bonzini         qemu_register_reset(spapr_cpu_reset, cpu);
150553018216SPaolo Bonzini     }
150653018216SPaolo Bonzini 
150753018216SPaolo Bonzini     /* allocate RAM */
150853018216SPaolo Bonzini     spapr->ram_limit = ram_size;
1509f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
1510f92f5da1SAlexey Kardashevskiy                                          spapr->ram_limit);
1511f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
151253018216SPaolo Bonzini 
1513658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
1514658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
1515658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
1516658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
1517658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
1518658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
1519658fa66bSAlexey Kardashevskiy     }
1520658fa66bSAlexey Kardashevskiy 
152153018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
15224c56440dSStefan Weil     if (!filename) {
1523730fce59SThomas Huth         error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin");
15244c56440dSStefan Weil         exit(1);
15254c56440dSStefan Weil     }
1526b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
1527b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
1528b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
1529730fce59SThomas Huth         error_report("Could not load LPAR rtas '%s'", filename);
153053018216SPaolo Bonzini         exit(1);
153153018216SPaolo Bonzini     }
153253018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
1533730fce59SThomas Huth         error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)",
15342f285bddSPeter Maydell                      (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
153553018216SPaolo Bonzini         exit(1);
153653018216SPaolo Bonzini     }
153753018216SPaolo Bonzini     g_free(filename);
153853018216SPaolo Bonzini 
153953018216SPaolo Bonzini     /* Set up EPOW events infrastructure */
154053018216SPaolo Bonzini     spapr_events_init(spapr);
154153018216SPaolo Bonzini 
154212f42174SDavid Gibson     /* Set up the RTC RTAS interfaces */
154328df36a1SDavid Gibson     spapr_rtc_create(spapr);
154412f42174SDavid Gibson 
154553018216SPaolo Bonzini     /* Set up VIO bus */
154653018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
154753018216SPaolo Bonzini 
154853018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
154953018216SPaolo Bonzini         if (serial_hds[i]) {
155053018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
155153018216SPaolo Bonzini         }
155253018216SPaolo Bonzini     }
155353018216SPaolo Bonzini 
155453018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
155553018216SPaolo Bonzini     spapr_create_nvram(spapr);
155653018216SPaolo Bonzini 
155753018216SPaolo Bonzini     /* Set up PCI */
155853018216SPaolo Bonzini     spapr_pci_rtas_init();
155953018216SPaolo Bonzini 
156089dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
156153018216SPaolo Bonzini 
156253018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
156353018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
156453018216SPaolo Bonzini 
156553018216SPaolo Bonzini         if (!nd->model) {
156653018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
156753018216SPaolo Bonzini         }
156853018216SPaolo Bonzini 
156953018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
157053018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
157153018216SPaolo Bonzini         } else {
157229b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
157353018216SPaolo Bonzini         }
157453018216SPaolo Bonzini     }
157553018216SPaolo Bonzini 
157653018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
157753018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
157853018216SPaolo Bonzini     }
157953018216SPaolo Bonzini 
158053018216SPaolo Bonzini     /* Graphics */
158153018216SPaolo Bonzini     if (spapr_vga_init(phb->bus)) {
158253018216SPaolo Bonzini         spapr->has_graphics = true;
1583c6e76503SPaolo Bonzini         machine->usb |= defaults_enabled() && !machine->usb_disabled;
158453018216SPaolo Bonzini     }
158553018216SPaolo Bonzini 
15864ee9ced9SMarcel Apfelbaum     if (machine->usb) {
158753018216SPaolo Bonzini         pci_create_simple(phb->bus, -1, "pci-ohci");
1588c86580b8SMarkus Armbruster 
158953018216SPaolo Bonzini         if (spapr->has_graphics) {
1590c86580b8SMarkus Armbruster             USBBus *usb_bus = usb_bus_find(-1);
1591c86580b8SMarkus Armbruster 
1592c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-kbd");
1593c86580b8SMarkus Armbruster             usb_create_simple(usb_bus, "usb-mouse");
159453018216SPaolo Bonzini         }
159553018216SPaolo Bonzini     }
159653018216SPaolo Bonzini 
159753018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
159853018216SPaolo Bonzini         fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
159953018216SPaolo Bonzini                 "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
160053018216SPaolo Bonzini         exit(1);
160153018216SPaolo Bonzini     }
160253018216SPaolo Bonzini 
160353018216SPaolo Bonzini     if (kernel_filename) {
160453018216SPaolo Bonzini         uint64_t lowaddr = 0;
160553018216SPaolo Bonzini 
160653018216SPaolo Bonzini         kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
160753018216SPaolo Bonzini                                NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
16083b66da82SAlexey Kardashevskiy         if (kernel_size == ELF_LOAD_WRONG_ENDIAN) {
160916457e7fSBenjamin Herrenschmidt             kernel_size = load_elf(kernel_filename,
161016457e7fSBenjamin Herrenschmidt                                    translate_kernel_address, NULL,
161116457e7fSBenjamin Herrenschmidt                                    NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0);
161216457e7fSBenjamin Herrenschmidt             kernel_le = kernel_size > 0;
161316457e7fSBenjamin Herrenschmidt         }
161416457e7fSBenjamin Herrenschmidt         if (kernel_size < 0) {
16153b66da82SAlexey Kardashevskiy             fprintf(stderr, "qemu: error loading %s: %s\n",
16163b66da82SAlexey Kardashevskiy                     kernel_filename, load_elf_strerror(kernel_size));
161753018216SPaolo Bonzini             exit(1);
161853018216SPaolo Bonzini         }
161953018216SPaolo Bonzini 
162053018216SPaolo Bonzini         /* load initrd */
162153018216SPaolo Bonzini         if (initrd_filename) {
162253018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
162353018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
162453018216SPaolo Bonzini              */
162553018216SPaolo Bonzini             initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
162653018216SPaolo Bonzini             initrd_size = load_image_targphys(initrd_filename, initrd_base,
162753018216SPaolo Bonzini                                               load_limit - initrd_base);
162853018216SPaolo Bonzini             if (initrd_size < 0) {
162953018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
163053018216SPaolo Bonzini                         initrd_filename);
163153018216SPaolo Bonzini                 exit(1);
163253018216SPaolo Bonzini             }
163353018216SPaolo Bonzini         } else {
163453018216SPaolo Bonzini             initrd_base = 0;
163553018216SPaolo Bonzini             initrd_size = 0;
163653018216SPaolo Bonzini         }
163753018216SPaolo Bonzini     }
163853018216SPaolo Bonzini 
16398e7ea787SAndreas Färber     if (bios_name == NULL) {
16408e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
16418e7ea787SAndreas Färber     }
16428e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
16434c56440dSStefan Weil     if (!filename) {
164468fea5a0SThomas Huth         error_report("Could not find LPAR firmware '%s'", bios_name);
16454c56440dSStefan Weil         exit(1);
16464c56440dSStefan Weil     }
164753018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
164868fea5a0SThomas Huth     if (fw_size <= 0) {
164968fea5a0SThomas Huth         error_report("Could not load LPAR firmware '%s'", filename);
165053018216SPaolo Bonzini         exit(1);
165153018216SPaolo Bonzini     }
165253018216SPaolo Bonzini     g_free(filename);
165353018216SPaolo Bonzini 
165453018216SPaolo Bonzini     spapr->entry_point = 0x100;
165553018216SPaolo Bonzini 
16564be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
16574be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
16584be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
16594be21d56SDavid Gibson 
166053018216SPaolo Bonzini     /* Prepare the device tree */
16613bbf37f2SAndreas Färber     spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size,
166216457e7fSBenjamin Herrenschmidt                                             kernel_size, kernel_le,
1663*31fe14d1SNathan Fontenot                                             kernel_cmdline,
1664*31fe14d1SNathan Fontenot                                             spapr->check_exception_irq);
166553018216SPaolo Bonzini     assert(spapr->fdt_skel != NULL);
16665b2128d2SAlexander Graf 
166746503c2bSMichael Roth     /* used by RTAS */
166846503c2bSMichael Roth     QTAILQ_INIT(&spapr->ccs_list);
166946503c2bSMichael Roth     qemu_register_reset(spapr_ccs_reset_hook, spapr);
167046503c2bSMichael Roth 
16715b2128d2SAlexander Graf     qemu_register_boot_set(spapr_boot_set, spapr);
167253018216SPaolo Bonzini }
167353018216SPaolo Bonzini 
1674135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
1675135a129aSAneesh Kumar K.V {
1676135a129aSAneesh Kumar K.V     if (!vm_type) {
1677135a129aSAneesh Kumar K.V         return 0;
1678135a129aSAneesh Kumar K.V     }
1679135a129aSAneesh Kumar K.V 
1680135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
1681135a129aSAneesh Kumar K.V         return 1;
1682135a129aSAneesh Kumar K.V     }
1683135a129aSAneesh Kumar K.V 
1684135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
1685135a129aSAneesh Kumar K.V         return 2;
1686135a129aSAneesh Kumar K.V     }
1687135a129aSAneesh Kumar K.V 
1688135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
1689135a129aSAneesh Kumar K.V     exit(1);
1690135a129aSAneesh Kumar K.V }
1691135a129aSAneesh Kumar K.V 
169271461b0fSAlexey Kardashevskiy /*
1693627b84f4SGonglei  * Implementation of an interface to adjust firmware path
169471461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
169571461b0fSAlexey Kardashevskiy  */
169671461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
169771461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
169871461b0fSAlexey Kardashevskiy {
169971461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
170071461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
170171461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
170271461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
170371461b0fSAlexey Kardashevskiy 
170471461b0fSAlexey Kardashevskiy     if (d) {
170571461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
170671461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
170771461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
170871461b0fSAlexey Kardashevskiy 
170971461b0fSAlexey Kardashevskiy         if (spapr) {
171071461b0fSAlexey Kardashevskiy             /*
171171461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
171271461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
171371461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
171471461b0fSAlexey Kardashevskiy              */
171571461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
171671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
171771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
171871461b0fSAlexey Kardashevskiy         } else if (virtio) {
171971461b0fSAlexey Kardashevskiy             /*
172071461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
172171461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
172271461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
172371461b0fSAlexey Kardashevskiy              * the actual binding is:
172471461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
172571461b0fSAlexey Kardashevskiy              */
172671461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
172771461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
172871461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
172971461b0fSAlexey Kardashevskiy         } else if (usb) {
173071461b0fSAlexey Kardashevskiy             /*
173171461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
173271461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
173371461b0fSAlexey Kardashevskiy              */
173471461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
173571461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
173671461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
173771461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
173871461b0fSAlexey Kardashevskiy         }
173971461b0fSAlexey Kardashevskiy     }
174071461b0fSAlexey Kardashevskiy 
174171461b0fSAlexey Kardashevskiy     if (phb) {
174271461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
174371461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
174471461b0fSAlexey Kardashevskiy     }
174571461b0fSAlexey Kardashevskiy 
174671461b0fSAlexey Kardashevskiy     return NULL;
174771461b0fSAlexey Kardashevskiy }
174871461b0fSAlexey Kardashevskiy 
174923825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
175023825581SEduardo Habkost {
17516ca1502eSAlexey Kardashevskiy     sPAPRMachineState *sm = SPAPR_MACHINE(obj);
175223825581SEduardo Habkost 
175323825581SEduardo Habkost     return g_strdup(sm->kvm_type);
175423825581SEduardo Habkost }
175523825581SEduardo Habkost 
175623825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
175723825581SEduardo Habkost {
17586ca1502eSAlexey Kardashevskiy     sPAPRMachineState *sm = SPAPR_MACHINE(obj);
175923825581SEduardo Habkost 
176023825581SEduardo Habkost     g_free(sm->kvm_type);
176123825581SEduardo Habkost     sm->kvm_type = g_strdup(value);
176223825581SEduardo Habkost }
176323825581SEduardo Habkost 
176423825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
176523825581SEduardo Habkost {
176623825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
176723825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
176849d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
176949d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
177049d2e648SMarcel Apfelbaum                                     NULL);
177123825581SEduardo Habkost }
177223825581SEduardo Habkost 
177334316482SAlexey Kardashevskiy static void ppc_cpu_do_nmi_on_cpu(void *arg)
177434316482SAlexey Kardashevskiy {
177534316482SAlexey Kardashevskiy     CPUState *cs = arg;
177634316482SAlexey Kardashevskiy 
177734316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
177834316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
177934316482SAlexey Kardashevskiy }
178034316482SAlexey Kardashevskiy 
178134316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
178234316482SAlexey Kardashevskiy {
178334316482SAlexey Kardashevskiy     CPUState *cs;
178434316482SAlexey Kardashevskiy 
178534316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
178634316482SAlexey Kardashevskiy         async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs);
178734316482SAlexey Kardashevskiy     }
178834316482SAlexey Kardashevskiy }
178934316482SAlexey Kardashevskiy 
179029ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
179153018216SPaolo Bonzini {
179229ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
179371461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
179434316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
179529ee3247SAlexey Kardashevskiy 
1796958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
1797958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
1798958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
1799958db90cSMarcel Apfelbaum     mc->max_cpus = MAX_CPUS;
1800958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
18015b2128d2SAlexander Graf     mc->default_boot_order = "";
1802958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
18039e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
180400b4fbe2SMarcel Apfelbaum 
180571461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
180634316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
180753018216SPaolo Bonzini }
180853018216SPaolo Bonzini 
180929ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
181029ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
181129ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
18124aee7362SDavid Gibson     .abstract      = true,
18136ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
181423825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
181529ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
181671461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
181771461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
181834316482SAlexey Kardashevskiy         { TYPE_NMI },
181971461b0fSAlexey Kardashevskiy         { }
182071461b0fSAlexey Kardashevskiy     },
182129ee3247SAlexey Kardashevskiy };
182229ee3247SAlexey Kardashevskiy 
182338ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \
182438ff32c6SEduardo Habkost         HW_COMPAT_2_3
182538ff32c6SEduardo Habkost 
1826b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \
182738ff32c6SEduardo Habkost         SPAPR_COMPAT_2_3 \
18284dfd8eaaSEduardo Habkost         HW_COMPAT_2_2 \
1829b194df47SAlexey Kardashevskiy         {\
1830b194df47SAlexey Kardashevskiy             .driver   = TYPE_SPAPR_PCI_HOST_BRIDGE,\
1831b194df47SAlexey Kardashevskiy             .property = "mem_win_size",\
1832b194df47SAlexey Kardashevskiy             .value    = "0x20000000",\
1833dd754bafSEduardo Habkost         },
1834b194df47SAlexey Kardashevskiy 
1835b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_1 \
18364dfd8eaaSEduardo Habkost         SPAPR_COMPAT_2_2 \
18374dfd8eaaSEduardo Habkost         HW_COMPAT_2_1
1838b194df47SAlexey Kardashevskiy 
1839d25228e7SJason Wang static void spapr_compat_2_3(Object *obj)
1840d25228e7SJason Wang {
1841d25228e7SJason Wang }
1842d25228e7SJason Wang 
1843b0e966d0SJason Wang static void spapr_compat_2_2(Object *obj)
1844b0e966d0SJason Wang {
1845d25228e7SJason Wang     spapr_compat_2_3(obj);
1846b0e966d0SJason Wang }
1847b0e966d0SJason Wang 
1848b0e966d0SJason Wang static void spapr_compat_2_1(Object *obj)
1849b0e966d0SJason Wang {
1850b0e966d0SJason Wang     spapr_compat_2_2(obj);
1851b0e966d0SJason Wang }
1852b0e966d0SJason Wang 
1853d25228e7SJason Wang static void spapr_machine_2_3_instance_init(Object *obj)
1854d25228e7SJason Wang {
1855d25228e7SJason Wang     spapr_compat_2_3(obj);
1856d25228e7SJason Wang     spapr_machine_initfn(obj);
1857d25228e7SJason Wang }
1858d25228e7SJason Wang 
1859b0e966d0SJason Wang static void spapr_machine_2_2_instance_init(Object *obj)
1860b0e966d0SJason Wang {
1861b0e966d0SJason Wang     spapr_compat_2_2(obj);
1862b0e966d0SJason Wang     spapr_machine_initfn(obj);
1863b0e966d0SJason Wang }
1864b0e966d0SJason Wang 
1865b0e966d0SJason Wang static void spapr_machine_2_1_instance_init(Object *obj)
1866b0e966d0SJason Wang {
1867b0e966d0SJason Wang     spapr_compat_2_1(obj);
1868b0e966d0SJason Wang     spapr_machine_initfn(obj);
1869b0e966d0SJason Wang }
1870b0e966d0SJason Wang 
18716026db45SAlexey Kardashevskiy static void spapr_machine_2_1_class_init(ObjectClass *oc, void *data)
18726026db45SAlexey Kardashevskiy {
18736026db45SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
187468a27b20SMichael S. Tsirkin     static GlobalProperty compat_props[] = {
1875dd754bafSEduardo Habkost         SPAPR_COMPAT_2_1
187668a27b20SMichael S. Tsirkin         { /* end of list */ }
187768a27b20SMichael S. Tsirkin     };
18786026db45SAlexey Kardashevskiy 
18796026db45SAlexey Kardashevskiy     mc->name = "pseries-2.1";
18806026db45SAlexey Kardashevskiy     mc->desc = "pSeries Logical Partition (PAPR compliant) v2.1";
188168a27b20SMichael S. Tsirkin     mc->compat_props = compat_props;
18826026db45SAlexey Kardashevskiy }
18836026db45SAlexey Kardashevskiy 
18846026db45SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_1_info = {
18856026db45SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE "2.1",
18866026db45SAlexey Kardashevskiy     .parent        = TYPE_SPAPR_MACHINE,
18876026db45SAlexey Kardashevskiy     .class_init    = spapr_machine_2_1_class_init,
1888b0e966d0SJason Wang     .instance_init = spapr_machine_2_1_instance_init,
18896026db45SAlexey Kardashevskiy };
18906026db45SAlexey Kardashevskiy 
18914aee7362SDavid Gibson static void spapr_machine_2_2_class_init(ObjectClass *oc, void *data)
18924aee7362SDavid Gibson {
1893b194df47SAlexey Kardashevskiy     static GlobalProperty compat_props[] = {
1894dd754bafSEduardo Habkost         SPAPR_COMPAT_2_2
1895b194df47SAlexey Kardashevskiy         { /* end of list */ }
1896b194df47SAlexey Kardashevskiy     };
18974aee7362SDavid Gibson     MachineClass *mc = MACHINE_CLASS(oc);
18984aee7362SDavid Gibson 
18994aee7362SDavid Gibson     mc->name = "pseries-2.2";
19004aee7362SDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant) v2.2";
1901b194df47SAlexey Kardashevskiy     mc->compat_props = compat_props;
19024aee7362SDavid Gibson }
19034aee7362SDavid Gibson 
19044aee7362SDavid Gibson static const TypeInfo spapr_machine_2_2_info = {
19054aee7362SDavid Gibson     .name          = TYPE_SPAPR_MACHINE "2.2",
19064aee7362SDavid Gibson     .parent        = TYPE_SPAPR_MACHINE,
19074aee7362SDavid Gibson     .class_init    = spapr_machine_2_2_class_init,
1908b0e966d0SJason Wang     .instance_init = spapr_machine_2_2_instance_init,
19094aee7362SDavid Gibson };
19104aee7362SDavid Gibson 
19113dab0244SAlexey Kardashevskiy static void spapr_machine_2_3_class_init(ObjectClass *oc, void *data)
19123dab0244SAlexey Kardashevskiy {
1913a1a45612SDavid Gibson     static GlobalProperty compat_props[] = {
1914a1a45612SDavid Gibson         /* SPAPR_COMPAT_2_3, */
1915a1a45612SDavid Gibson         { /* end of list */ }
1916a1a45612SDavid Gibson     };
19173dab0244SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
19183dab0244SAlexey Kardashevskiy 
19193dab0244SAlexey Kardashevskiy     mc->name = "pseries-2.3";
19203dab0244SAlexey Kardashevskiy     mc->desc = "pSeries Logical Partition (PAPR compliant) v2.3";
1921a1a45612SDavid Gibson     mc->compat_props = compat_props;
19223dab0244SAlexey Kardashevskiy }
19233dab0244SAlexey Kardashevskiy 
19243dab0244SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_3_info = {
19253dab0244SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE "2.3",
19263dab0244SAlexey Kardashevskiy     .parent        = TYPE_SPAPR_MACHINE,
19273dab0244SAlexey Kardashevskiy     .class_init    = spapr_machine_2_3_class_init,
1928d25228e7SJason Wang     .instance_init = spapr_machine_2_3_instance_init,
1929d25228e7SJason Wang };
1930d25228e7SJason Wang 
1931d25228e7SJason Wang static void spapr_machine_2_4_class_init(ObjectClass *oc, void *data)
1932d25228e7SJason Wang {
1933d25228e7SJason Wang     MachineClass *mc = MACHINE_CLASS(oc);
1934d25228e7SJason Wang 
1935d25228e7SJason Wang     mc->name = "pseries-2.4";
1936d25228e7SJason Wang     mc->desc = "pSeries Logical Partition (PAPR compliant) v2.4";
1937d25228e7SJason Wang     mc->alias = "pseries";
1938d25228e7SJason Wang     mc->is_default = 1;
1939d25228e7SJason Wang }
1940d25228e7SJason Wang 
1941d25228e7SJason Wang static const TypeInfo spapr_machine_2_4_info = {
1942d25228e7SJason Wang     .name          = TYPE_SPAPR_MACHINE "2.4",
1943d25228e7SJason Wang     .parent        = TYPE_SPAPR_MACHINE,
1944d25228e7SJason Wang     .class_init    = spapr_machine_2_4_class_init,
19453dab0244SAlexey Kardashevskiy };
19463dab0244SAlexey Kardashevskiy 
194729ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
194829ee3247SAlexey Kardashevskiy {
194929ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
19506026db45SAlexey Kardashevskiy     type_register_static(&spapr_machine_2_1_info);
19514aee7362SDavid Gibson     type_register_static(&spapr_machine_2_2_info);
19523dab0244SAlexey Kardashevskiy     type_register_static(&spapr_machine_2_3_info);
1953d25228e7SJason Wang     type_register_static(&spapr_machine_2_4_info);
195429ee3247SAlexey Kardashevskiy }
195529ee3247SAlexey Kardashevskiy 
195629ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
1957