xref: /openbmc/qemu/hw/ppc/spapr.c (revision 2f285bdd)
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"
2853018216SPaolo Bonzini #include "hw/hw.h"
2971461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h"
3053018216SPaolo Bonzini #include "elf.h"
3153018216SPaolo Bonzini #include "net/net.h"
32fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3353018216SPaolo Bonzini #include "sysemu/cpus.h"
3453018216SPaolo Bonzini #include "sysemu/kvm.h"
3553018216SPaolo Bonzini #include "kvm_ppc.h"
364be21d56SDavid Gibson #include "mmu-hash64.h"
373794d548SAlexey Kardashevskiy #include "qom/cpu.h"
3853018216SPaolo Bonzini 
3953018216SPaolo Bonzini #include "hw/boards.h"
400d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
4153018216SPaolo Bonzini #include "hw/loader.h"
4253018216SPaolo Bonzini 
430d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
440d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
450d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
460d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
4753018216SPaolo Bonzini #include "hw/pci/msi.h"
4853018216SPaolo Bonzini 
4953018216SPaolo Bonzini #include "hw/pci/pci.h"
5071461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h"
5171461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h"
5253018216SPaolo Bonzini 
5353018216SPaolo Bonzini #include "exec/address-spaces.h"
5453018216SPaolo Bonzini #include "hw/usb.h"
5553018216SPaolo Bonzini #include "qemu/config-file.h"
56135a129aSAneesh Kumar K.V #include "qemu/error-report.h"
572a6593cbSAlexey Kardashevskiy #include "trace.h"
5834316482SAlexey Kardashevskiy #include "hw/nmi.h"
5953018216SPaolo Bonzini 
6068a27b20SMichael S. Tsirkin #include "hw/compat.h"
6168a27b20SMichael S. Tsirkin 
6253018216SPaolo Bonzini #include <libfdt.h>
6353018216SPaolo Bonzini 
6453018216SPaolo Bonzini /* SLOF memory layout:
6553018216SPaolo Bonzini  *
6653018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
6753018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
6853018216SPaolo Bonzini  *
6953018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
7053018216SPaolo Bonzini  * and more
7153018216SPaolo Bonzini  *
7253018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
7353018216SPaolo Bonzini  */
743bf6eeddSAlexey Kardashevskiy #define FDT_MAX_SIZE            0x40000
7553018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
76b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR           0x80000000 /* RTAS must stay below that */
7753018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
7853018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
7953018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
8053018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
8153018216SPaolo Bonzini 
8253018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
8353018216SPaolo Bonzini 
8453018216SPaolo Bonzini #define TIMEBASE_FREQ           512000000ULL
8553018216SPaolo Bonzini 
869674a356SNikunj A Dadhania #define MAX_CPUS                255
8753018216SPaolo Bonzini 
8853018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
8953018216SPaolo Bonzini 
9053018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
9153018216SPaolo Bonzini 
926ca1502eSAlexey Kardashevskiy typedef struct sPAPRMachineState sPAPRMachineState;
93748abce9SEduardo Habkost 
9429ee3247SAlexey Kardashevskiy #define TYPE_SPAPR_MACHINE      "spapr-machine"
95748abce9SEduardo Habkost #define SPAPR_MACHINE(obj) \
966ca1502eSAlexey Kardashevskiy     OBJECT_CHECK(sPAPRMachineState, (obj), TYPE_SPAPR_MACHINE)
97748abce9SEduardo Habkost 
98748abce9SEduardo Habkost /**
996ca1502eSAlexey Kardashevskiy  * sPAPRMachineState:
100748abce9SEduardo Habkost  */
1016ca1502eSAlexey Kardashevskiy struct sPAPRMachineState {
102748abce9SEduardo Habkost     /*< private >*/
103748abce9SEduardo Habkost     MachineState parent_obj;
10423825581SEduardo Habkost 
10523825581SEduardo Habkost     /*< public >*/
10623825581SEduardo Habkost     char *kvm_type;
107748abce9SEduardo Habkost };
108748abce9SEduardo Habkost 
10953018216SPaolo Bonzini sPAPREnvironment *spapr;
11053018216SPaolo Bonzini 
111c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers,
112c04d6cfaSAnthony Liguori                                   int nr_irqs)
113c04d6cfaSAnthony Liguori {
114c04d6cfaSAnthony Liguori     DeviceState *dev;
115c04d6cfaSAnthony Liguori 
116c04d6cfaSAnthony Liguori     dev = qdev_create(NULL, type);
117c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
118c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
119c04d6cfaSAnthony Liguori     if (qdev_init(dev) < 0) {
120c04d6cfaSAnthony Liguori         return NULL;
121c04d6cfaSAnthony Liguori     }
122c04d6cfaSAnthony Liguori 
1235a3d7b23SAlexey Kardashevskiy     return XICS_COMMON(dev);
124c04d6cfaSAnthony Liguori }
125c04d6cfaSAnthony Liguori 
126c04d6cfaSAnthony Liguori static XICSState *xics_system_init(int nr_servers, int nr_irqs)
127c04d6cfaSAnthony Liguori {
128c04d6cfaSAnthony Liguori     XICSState *icp = NULL;
129c04d6cfaSAnthony Liguori 
13011ad93f6SDavid Gibson     if (kvm_enabled()) {
13111ad93f6SDavid Gibson         QemuOpts *machine_opts = qemu_get_machine_opts();
13211ad93f6SDavid Gibson         bool irqchip_allowed = qemu_opt_get_bool(machine_opts,
13311ad93f6SDavid Gibson                                                 "kernel_irqchip", true);
13411ad93f6SDavid Gibson         bool irqchip_required = qemu_opt_get_bool(machine_opts,
13511ad93f6SDavid Gibson                                                   "kernel_irqchip", false);
13611ad93f6SDavid Gibson         if (irqchip_allowed) {
13711ad93f6SDavid Gibson             icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs);
13811ad93f6SDavid Gibson         }
13911ad93f6SDavid Gibson 
14011ad93f6SDavid Gibson         if (irqchip_required && !icp) {
14111ad93f6SDavid Gibson             perror("Failed to create in-kernel XICS\n");
14211ad93f6SDavid Gibson             abort();
14311ad93f6SDavid Gibson         }
14411ad93f6SDavid Gibson     }
14511ad93f6SDavid Gibson 
14611ad93f6SDavid Gibson     if (!icp) {
147c04d6cfaSAnthony Liguori         icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs);
14811ad93f6SDavid Gibson     }
14911ad93f6SDavid Gibson 
150c04d6cfaSAnthony Liguori     if (!icp) {
151c04d6cfaSAnthony Liguori         perror("Failed to create XICS\n");
152c04d6cfaSAnthony Liguori         abort();
153c04d6cfaSAnthony Liguori     }
154c04d6cfaSAnthony Liguori 
155c04d6cfaSAnthony Liguori     return icp;
156c04d6cfaSAnthony Liguori }
157c04d6cfaSAnthony Liguori 
158833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
159833d4668SAlexey Kardashevskiy                                   int smt_threads)
160833d4668SAlexey Kardashevskiy {
161833d4668SAlexey Kardashevskiy     int i, ret = 0;
162833d4668SAlexey Kardashevskiy     uint32_t servers_prop[smt_threads];
163833d4668SAlexey Kardashevskiy     uint32_t gservers_prop[smt_threads * 2];
164833d4668SAlexey Kardashevskiy     int index = ppc_get_vcpu_dt_id(cpu);
165833d4668SAlexey Kardashevskiy 
1666d9412eaSAlexey Kardashevskiy     if (cpu->cpu_version) {
1674bce526eSLaurent Dufour         ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version);
1686d9412eaSAlexey Kardashevskiy         if (ret < 0) {
1696d9412eaSAlexey Kardashevskiy             return ret;
1706d9412eaSAlexey Kardashevskiy         }
1716d9412eaSAlexey Kardashevskiy     }
1726d9412eaSAlexey Kardashevskiy 
173833d4668SAlexey Kardashevskiy     /* Build interrupt servers and gservers properties */
174833d4668SAlexey Kardashevskiy     for (i = 0; i < smt_threads; i++) {
175833d4668SAlexey Kardashevskiy         servers_prop[i] = cpu_to_be32(index + i);
176833d4668SAlexey Kardashevskiy         /* Hack, direct the group queues back to cpu 0 */
177833d4668SAlexey Kardashevskiy         gservers_prop[i*2] = cpu_to_be32(index + i);
178833d4668SAlexey Kardashevskiy         gservers_prop[i*2 + 1] = 0;
179833d4668SAlexey Kardashevskiy     }
180833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
181833d4668SAlexey Kardashevskiy                       servers_prop, sizeof(servers_prop));
182833d4668SAlexey Kardashevskiy     if (ret < 0) {
183833d4668SAlexey Kardashevskiy         return ret;
184833d4668SAlexey Kardashevskiy     }
185833d4668SAlexey Kardashevskiy     ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
186833d4668SAlexey Kardashevskiy                       gservers_prop, sizeof(gservers_prop));
187833d4668SAlexey Kardashevskiy 
188833d4668SAlexey Kardashevskiy     return ret;
189833d4668SAlexey Kardashevskiy }
190833d4668SAlexey Kardashevskiy 
19153018216SPaolo Bonzini static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr)
19253018216SPaolo Bonzini {
19382677ed2SAlexey Kardashevskiy     int ret = 0, offset, cpus_offset;
19482677ed2SAlexey Kardashevskiy     CPUState *cs;
19553018216SPaolo Bonzini     char cpu_model[32];
19653018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
19753018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
19853018216SPaolo Bonzini 
19982677ed2SAlexey Kardashevskiy     CPU_FOREACH(cs) {
20082677ed2SAlexey Kardashevskiy         PowerPCCPU *cpu = POWERPC_CPU(cs);
20182677ed2SAlexey Kardashevskiy         DeviceClass *dc = DEVICE_GET_CLASS(cs);
20282677ed2SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
20353018216SPaolo Bonzini         uint32_t associativity[] = {cpu_to_be32(0x5),
20453018216SPaolo Bonzini                                     cpu_to_be32(0x0),
20553018216SPaolo Bonzini                                     cpu_to_be32(0x0),
20653018216SPaolo Bonzini                                     cpu_to_be32(0x0),
20782677ed2SAlexey Kardashevskiy                                     cpu_to_be32(cs->numa_node),
2080f20ba62SAlexey Kardashevskiy                                     cpu_to_be32(index)};
20953018216SPaolo Bonzini 
2100f20ba62SAlexey Kardashevskiy         if ((index % smt) != 0) {
21153018216SPaolo Bonzini             continue;
21253018216SPaolo Bonzini         }
21353018216SPaolo Bonzini 
21482677ed2SAlexey Kardashevskiy         snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
21553018216SPaolo Bonzini 
21682677ed2SAlexey Kardashevskiy         cpus_offset = fdt_path_offset(fdt, "/cpus");
21782677ed2SAlexey Kardashevskiy         if (cpus_offset < 0) {
21882677ed2SAlexey Kardashevskiy             cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
21982677ed2SAlexey Kardashevskiy                                           "cpus");
22082677ed2SAlexey Kardashevskiy             if (cpus_offset < 0) {
22182677ed2SAlexey Kardashevskiy                 return cpus_offset;
22282677ed2SAlexey Kardashevskiy             }
22382677ed2SAlexey Kardashevskiy         }
22482677ed2SAlexey Kardashevskiy         offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
22582677ed2SAlexey Kardashevskiy         if (offset < 0) {
22682677ed2SAlexey Kardashevskiy             offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
22753018216SPaolo Bonzini             if (offset < 0) {
22853018216SPaolo Bonzini                 return offset;
22953018216SPaolo Bonzini             }
23082677ed2SAlexey Kardashevskiy         }
23153018216SPaolo Bonzini 
23253018216SPaolo Bonzini         if (nb_numa_nodes > 1) {
23353018216SPaolo Bonzini             ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
23453018216SPaolo Bonzini                               sizeof(associativity));
23553018216SPaolo Bonzini             if (ret < 0) {
23653018216SPaolo Bonzini                 return ret;
23753018216SPaolo Bonzini             }
23853018216SPaolo Bonzini         }
23953018216SPaolo Bonzini 
24053018216SPaolo Bonzini         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
24153018216SPaolo Bonzini                           pft_size_prop, sizeof(pft_size_prop));
24253018216SPaolo Bonzini         if (ret < 0) {
24353018216SPaolo Bonzini             return ret;
24453018216SPaolo Bonzini         }
245833d4668SAlexey Kardashevskiy 
24682677ed2SAlexey Kardashevskiy         ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
2472a48d993SAlexey Kardashevskiy                                      ppc_get_compat_smt_threads(cpu));
248833d4668SAlexey Kardashevskiy         if (ret < 0) {
249833d4668SAlexey Kardashevskiy             return ret;
250833d4668SAlexey Kardashevskiy         }
25153018216SPaolo Bonzini     }
25253018216SPaolo Bonzini     return ret;
25353018216SPaolo Bonzini }
25453018216SPaolo Bonzini 
25553018216SPaolo Bonzini 
25653018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
25753018216SPaolo Bonzini                                      size_t maxsize)
25853018216SPaolo Bonzini {
25953018216SPaolo Bonzini     size_t maxcells = maxsize / sizeof(uint32_t);
26053018216SPaolo Bonzini     int i, j, count;
26153018216SPaolo Bonzini     uint32_t *p = prop;
26253018216SPaolo Bonzini 
26353018216SPaolo Bonzini     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
26453018216SPaolo Bonzini         struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
26553018216SPaolo Bonzini 
26653018216SPaolo Bonzini         if (!sps->page_shift) {
26753018216SPaolo Bonzini             break;
26853018216SPaolo Bonzini         }
26953018216SPaolo Bonzini         for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
27053018216SPaolo Bonzini             if (sps->enc[count].page_shift == 0) {
27153018216SPaolo Bonzini                 break;
27253018216SPaolo Bonzini             }
27353018216SPaolo Bonzini         }
27453018216SPaolo Bonzini         if ((p - prop) >= (maxcells - 3 - count * 2)) {
27553018216SPaolo Bonzini             break;
27653018216SPaolo Bonzini         }
27753018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->page_shift);
27853018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->slb_enc);
27953018216SPaolo Bonzini         *(p++) = cpu_to_be32(count);
28053018216SPaolo Bonzini         for (j = 0; j < count; j++) {
28153018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].page_shift);
28253018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
28353018216SPaolo Bonzini         }
28453018216SPaolo Bonzini     }
28553018216SPaolo Bonzini 
28653018216SPaolo Bonzini     return (p - prop) * sizeof(uint32_t);
28753018216SPaolo Bonzini }
28853018216SPaolo Bonzini 
289b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void)
290b082d65aSAlexey Kardashevskiy {
291b082d65aSAlexey Kardashevskiy     if (nb_numa_nodes) {
292b082d65aSAlexey Kardashevskiy         int i;
293b082d65aSAlexey Kardashevskiy         for (i = 0; i < nb_numa_nodes; ++i) {
294b082d65aSAlexey Kardashevskiy             if (numa_info[i].node_mem) {
295b082d65aSAlexey Kardashevskiy                 return MIN(pow2floor(numa_info[i].node_mem), ram_size);
296b082d65aSAlexey Kardashevskiy             }
297b082d65aSAlexey Kardashevskiy         }
298b082d65aSAlexey Kardashevskiy     }
299b082d65aSAlexey Kardashevskiy     return ram_size;
300b082d65aSAlexey Kardashevskiy }
301b082d65aSAlexey Kardashevskiy 
30253018216SPaolo Bonzini #define _FDT(exp) \
30353018216SPaolo Bonzini     do { \
30453018216SPaolo Bonzini         int ret = (exp);                                           \
30553018216SPaolo Bonzini         if (ret < 0) {                                             \
30653018216SPaolo Bonzini             fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
30753018216SPaolo Bonzini                     #exp, fdt_strerror(ret));                      \
30853018216SPaolo Bonzini             exit(1);                                               \
30953018216SPaolo Bonzini         }                                                          \
31053018216SPaolo Bonzini     } while (0)
31153018216SPaolo Bonzini 
312a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1)
313a1d59c0fSAlexey Kardashevskiy {
314a1d59c0fSAlexey Kardashevskiy     g_string_append_len(s, s1, strlen(s1) + 1);
315a1d59c0fSAlexey Kardashevskiy }
31653018216SPaolo Bonzini 
3173bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base,
31853018216SPaolo Bonzini                                    hwaddr initrd_size,
31953018216SPaolo Bonzini                                    hwaddr kernel_size,
32016457e7fSBenjamin Herrenschmidt                                    bool little_endian,
32153018216SPaolo Bonzini                                    const char *boot_device,
32253018216SPaolo Bonzini                                    const char *kernel_cmdline,
32353018216SPaolo Bonzini                                    uint32_t epow_irq)
32453018216SPaolo Bonzini {
32553018216SPaolo Bonzini     void *fdt;
326182735efSAndreas Färber     CPUState *cs;
32753018216SPaolo Bonzini     uint32_t start_prop = cpu_to_be32(initrd_base);
32853018216SPaolo Bonzini     uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
329a1d59c0fSAlexey Kardashevskiy     GString *hypertas = g_string_sized_new(256);
330a1d59c0fSAlexey Kardashevskiy     GString *qemu_hypertas = g_string_sized_new(256);
33153018216SPaolo Bonzini     uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
33253018216SPaolo Bonzini     uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
333833d4668SAlexey Kardashevskiy     int smt = kvmppc_smt_threads();
33453018216SPaolo Bonzini     unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
33510582ff8SAlexey Kardashevskiy     QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
33610582ff8SAlexey Kardashevskiy     unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
33710582ff8SAlexey Kardashevskiy     uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
338ef951443SNikunj A Dadhania     char *buf;
33953018216SPaolo Bonzini 
340a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-pft");
341a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-term");
342a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-dabr");
343a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-interrupt");
344a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-tce");
345a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-vio");
346a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-splpar");
347a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-bulk");
348a1d59c0fSAlexey Kardashevskiy     add_str(hypertas, "hcall-set-mode");
349a1d59c0fSAlexey Kardashevskiy     add_str(qemu_hypertas, "hcall-memop1");
350a1d59c0fSAlexey Kardashevskiy 
35153018216SPaolo Bonzini     fdt = g_malloc0(FDT_MAX_SIZE);
35253018216SPaolo Bonzini     _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
35353018216SPaolo Bonzini 
35453018216SPaolo Bonzini     if (kernel_size) {
35553018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
35653018216SPaolo Bonzini     }
35753018216SPaolo Bonzini     if (initrd_size) {
35853018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
35953018216SPaolo Bonzini     }
36053018216SPaolo Bonzini     _FDT((fdt_finish_reservemap(fdt)));
36153018216SPaolo Bonzini 
36253018216SPaolo Bonzini     /* Root node */
36353018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "")));
36453018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "chrp")));
36553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
366fa388916SAnthony Liguori     _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
36753018216SPaolo Bonzini 
368ef951443SNikunj A Dadhania     /*
369ef951443SNikunj A Dadhania      * Add info to guest to indentify which host is it being run on
370ef951443SNikunj A Dadhania      * and what is the uuid of the guest
371ef951443SNikunj A Dadhania      */
372ef951443SNikunj A Dadhania     if (kvmppc_get_host_model(&buf)) {
373ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-model", buf)));
374ef951443SNikunj A Dadhania         g_free(buf);
375ef951443SNikunj A Dadhania     }
376ef951443SNikunj A Dadhania     if (kvmppc_get_host_serial(&buf)) {
377ef951443SNikunj A Dadhania         _FDT((fdt_property_string(fdt, "host-serial", buf)));
378ef951443SNikunj A Dadhania         g_free(buf);
379ef951443SNikunj A Dadhania     }
380ef951443SNikunj A Dadhania 
381ef951443SNikunj A Dadhania     buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1],
382ef951443SNikunj A Dadhania                           qemu_uuid[2], qemu_uuid[3], qemu_uuid[4],
383ef951443SNikunj A Dadhania                           qemu_uuid[5], qemu_uuid[6], qemu_uuid[7],
384ef951443SNikunj A Dadhania                           qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
385ef951443SNikunj A Dadhania                           qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
386ef951443SNikunj A Dadhania                           qemu_uuid[14], qemu_uuid[15]);
387ef951443SNikunj A Dadhania 
388ef951443SNikunj A Dadhania     _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
389ef951443SNikunj A Dadhania     g_free(buf);
390ef951443SNikunj A Dadhania 
39153018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
39253018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
39353018216SPaolo Bonzini 
39453018216SPaolo Bonzini     /* /chosen */
39553018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "chosen")));
39653018216SPaolo Bonzini 
39753018216SPaolo Bonzini     /* Set Form1_affinity */
39853018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
39953018216SPaolo Bonzini 
40053018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
40153018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-start",
40253018216SPaolo Bonzini                        &start_prop, sizeof(start_prop))));
40353018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-end",
40453018216SPaolo Bonzini                        &end_prop, sizeof(end_prop))));
40553018216SPaolo Bonzini     if (kernel_size) {
40653018216SPaolo Bonzini         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
40753018216SPaolo Bonzini                               cpu_to_be64(kernel_size) };
40853018216SPaolo Bonzini 
40953018216SPaolo Bonzini         _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
41016457e7fSBenjamin Herrenschmidt         if (little_endian) {
41116457e7fSBenjamin Herrenschmidt             _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0)));
41216457e7fSBenjamin Herrenschmidt         }
41353018216SPaolo Bonzini     }
41453018216SPaolo Bonzini     if (boot_device) {
41553018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));
41653018216SPaolo Bonzini     }
417cc84c0f3SAvik Sil     if (boot_menu) {
418cc84c0f3SAvik Sil         _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu)));
419cc84c0f3SAvik Sil     }
42053018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
42153018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
42253018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
42353018216SPaolo Bonzini 
42453018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
42553018216SPaolo Bonzini 
42653018216SPaolo Bonzini     /* cpus */
42753018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "cpus")));
42853018216SPaolo Bonzini 
42953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
43053018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
43153018216SPaolo Bonzini 
432bdc44640SAndreas Färber     CPU_FOREACH(cs) {
433182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
434182735efSAndreas Färber         CPUPPCState *env = &cpu->env;
4353bbf37f2SAndreas Färber         DeviceClass *dc = DEVICE_GET_CLASS(cs);
436182735efSAndreas Färber         PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
4370f20ba62SAlexey Kardashevskiy         int index = ppc_get_vcpu_dt_id(cpu);
43853018216SPaolo Bonzini         char *nodename;
43953018216SPaolo Bonzini         uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
44053018216SPaolo Bonzini                            0xffffffff, 0xffffffff};
44153018216SPaolo Bonzini         uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
44253018216SPaolo Bonzini         uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
44353018216SPaolo Bonzini         uint32_t page_sizes_prop[64];
44453018216SPaolo Bonzini         size_t page_sizes_prop_size;
44553018216SPaolo Bonzini 
44653018216SPaolo Bonzini         if ((index % smt) != 0) {
44753018216SPaolo Bonzini             continue;
44853018216SPaolo Bonzini         }
44953018216SPaolo Bonzini 
4503bbf37f2SAndreas Färber         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
45153018216SPaolo Bonzini 
45253018216SPaolo Bonzini         _FDT((fdt_begin_node(fdt, nodename)));
45353018216SPaolo Bonzini 
45453018216SPaolo Bonzini         g_free(nodename);
45553018216SPaolo Bonzini 
45653018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "reg", index)));
45753018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "device_type", "cpu")));
45853018216SPaolo Bonzini 
45953018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
4600cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "d-cache-block-size",
46153018216SPaolo Bonzini                                 env->dcache_line_size)));
4620cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "d-cache-line-size",
4630cbad81fSDavid Gibson                                 env->dcache_line_size)));
4640cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "i-cache-block-size",
46553018216SPaolo Bonzini                                 env->icache_line_size)));
4660cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "i-cache-line-size",
4670cbad81fSDavid Gibson                                 env->icache_line_size)));
4680cbad81fSDavid Gibson 
4690cbad81fSDavid Gibson         if (pcc->l1_dcache_size) {
4700cbad81fSDavid Gibson             _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size)));
4710cbad81fSDavid Gibson         } else {
4720cbad81fSDavid Gibson             fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");
4730cbad81fSDavid Gibson         }
4740cbad81fSDavid Gibson         if (pcc->l1_icache_size) {
4750cbad81fSDavid Gibson             _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size)));
4760cbad81fSDavid Gibson         } else {
4770cbad81fSDavid Gibson             fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n");
4780cbad81fSDavid Gibson         }
4790cbad81fSDavid Gibson 
48053018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
48153018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
48253018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
48353018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "status", "okay")));
48453018216SPaolo Bonzini         _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
48553018216SPaolo Bonzini 
486dcb861cbSAlexey Kardashevskiy         if (env->spr_cb[SPR_PURR].oea_read) {
487dcb861cbSAlexey Kardashevskiy             _FDT((fdt_property(fdt, "ibm,purr", NULL, 0)));
488dcb861cbSAlexey Kardashevskiy         }
489dcb861cbSAlexey Kardashevskiy 
49053018216SPaolo Bonzini         if (env->mmu_model & POWERPC_MMU_1TSEG) {
49153018216SPaolo Bonzini             _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
49253018216SPaolo Bonzini                                segs, sizeof(segs))));
49353018216SPaolo Bonzini         }
49453018216SPaolo Bonzini 
49553018216SPaolo Bonzini         /* Advertise VMX/VSX (vector extensions) if available
49653018216SPaolo Bonzini          *   0 / no property == no vector extensions
49753018216SPaolo Bonzini          *   1               == VMX / Altivec available
49853018216SPaolo Bonzini          *   2               == VSX available */
49953018216SPaolo Bonzini         if (env->insns_flags & PPC_ALTIVEC) {
50053018216SPaolo Bonzini             uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
50153018216SPaolo Bonzini 
50253018216SPaolo Bonzini             _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
50353018216SPaolo Bonzini         }
50453018216SPaolo Bonzini 
50553018216SPaolo Bonzini         /* Advertise DFP (Decimal Floating Point) if available
50653018216SPaolo Bonzini          *   0 / no property == no DFP
50753018216SPaolo Bonzini          *   1               == DFP available */
50853018216SPaolo Bonzini         if (env->insns_flags2 & PPC2_DFP) {
50953018216SPaolo Bonzini             _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
51053018216SPaolo Bonzini         }
51153018216SPaolo Bonzini 
51253018216SPaolo Bonzini         page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
51353018216SPaolo Bonzini                                                       sizeof(page_sizes_prop));
51453018216SPaolo Bonzini         if (page_sizes_prop_size) {
51553018216SPaolo Bonzini             _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
51653018216SPaolo Bonzini                                page_sizes_prop, page_sizes_prop_size)));
51753018216SPaolo Bonzini         }
51853018216SPaolo Bonzini 
51910582ff8SAlexey Kardashevskiy         _FDT((fdt_property_cell(fdt, "ibm,chip-id",
52010582ff8SAlexey Kardashevskiy                                 cs->cpu_index / cpus_per_socket)));
52110582ff8SAlexey Kardashevskiy 
52253018216SPaolo Bonzini         _FDT((fdt_end_node(fdt)));
52353018216SPaolo Bonzini     }
52453018216SPaolo Bonzini 
52553018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
52653018216SPaolo Bonzini 
52753018216SPaolo Bonzini     /* RTAS */
52853018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "rtas")));
52953018216SPaolo Bonzini 
530da95324eSAlexey Kardashevskiy     if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
531da95324eSAlexey Kardashevskiy         add_str(hypertas, "hcall-multi-tce");
532da95324eSAlexey Kardashevskiy     }
533a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str,
534a1d59c0fSAlexey Kardashevskiy                        hypertas->len)));
535a1d59c0fSAlexey Kardashevskiy     g_string_free(hypertas, TRUE);
536a1d59c0fSAlexey Kardashevskiy     _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str,
537a1d59c0fSAlexey Kardashevskiy                        qemu_hypertas->len)));
538a1d59c0fSAlexey Kardashevskiy     g_string_free(qemu_hypertas, TRUE);
53953018216SPaolo Bonzini 
54053018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
54153018216SPaolo Bonzini         refpoints, sizeof(refpoints))));
54253018216SPaolo Bonzini 
54353018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
54453018216SPaolo Bonzini 
5452e14072fSNikunj A Dadhania     /*
5469d632f5fSzhanghailiang      * According to PAPR, rtas ibm,os-term does not guarantee a return
5472e14072fSNikunj A Dadhania      * back to the guest cpu.
5482e14072fSNikunj A Dadhania      *
5492e14072fSNikunj A Dadhania      * While an additional ibm,extended-os-term property indicates that
5502e14072fSNikunj A Dadhania      * rtas call return will always occur. Set this property.
5512e14072fSNikunj A Dadhania      */
5522e14072fSNikunj A Dadhania     _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0)));
5532e14072fSNikunj A Dadhania 
55453018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
55553018216SPaolo Bonzini 
55653018216SPaolo Bonzini     /* interrupt controller */
55753018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "interrupt-controller")));
55853018216SPaolo Bonzini 
55953018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type",
56053018216SPaolo Bonzini                               "PowerPC-External-Interrupt-Presentation")));
56153018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
56253018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
56353018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
56453018216SPaolo Bonzini                        interrupt_server_ranges_prop,
56553018216SPaolo Bonzini                        sizeof(interrupt_server_ranges_prop))));
56653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
56753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
56853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
56953018216SPaolo Bonzini 
57053018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
57153018216SPaolo Bonzini 
57253018216SPaolo Bonzini     /* vdevice */
57353018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "vdevice")));
57453018216SPaolo Bonzini 
57553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
57653018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
57753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
57853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
57953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
58053018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
58153018216SPaolo Bonzini 
58253018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
58353018216SPaolo Bonzini 
58453018216SPaolo Bonzini     /* event-sources */
58553018216SPaolo Bonzini     spapr_events_fdt_skel(fdt, epow_irq);
58653018216SPaolo Bonzini 
587f7d69146SAlexander Graf     /* /hypervisor node */
588f7d69146SAlexander Graf     if (kvm_enabled()) {
589f7d69146SAlexander Graf         uint8_t hypercall[16];
590f7d69146SAlexander Graf 
591f7d69146SAlexander Graf         /* indicate KVM hypercall interface */
592f7d69146SAlexander Graf         _FDT((fdt_begin_node(fdt, "hypervisor")));
593f7d69146SAlexander Graf         _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
594f7d69146SAlexander Graf         if (kvmppc_has_cap_fixup_hcalls()) {
595f7d69146SAlexander Graf             /*
596f7d69146SAlexander Graf              * Older KVM versions with older guest kernels were broken with the
597f7d69146SAlexander Graf              * magic page, don't allow the guest to map it.
598f7d69146SAlexander Graf              */
599f7d69146SAlexander Graf             kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
600f7d69146SAlexander Graf                                  sizeof(hypercall));
601f7d69146SAlexander Graf             _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
602f7d69146SAlexander Graf                               sizeof(hypercall))));
603f7d69146SAlexander Graf         }
604f7d69146SAlexander Graf         _FDT((fdt_end_node(fdt)));
605f7d69146SAlexander Graf     }
606f7d69146SAlexander Graf 
60753018216SPaolo Bonzini     _FDT((fdt_end_node(fdt))); /* close root node */
60853018216SPaolo Bonzini     _FDT((fdt_finish(fdt)));
60953018216SPaolo Bonzini 
61053018216SPaolo Bonzini     return fdt;
61153018216SPaolo Bonzini }
61253018216SPaolo Bonzini 
6132a6593cbSAlexey Kardashevskiy int spapr_h_cas_compose_response(target_ulong addr, target_ulong size)
6142a6593cbSAlexey Kardashevskiy {
6152a6593cbSAlexey Kardashevskiy     void *fdt, *fdt_skel;
6162a6593cbSAlexey Kardashevskiy     sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
6172a6593cbSAlexey Kardashevskiy 
6182a6593cbSAlexey Kardashevskiy     size -= sizeof(hdr);
6192a6593cbSAlexey Kardashevskiy 
6202a6593cbSAlexey Kardashevskiy     /* Create sceleton */
6212a6593cbSAlexey Kardashevskiy     fdt_skel = g_malloc0(size);
6222a6593cbSAlexey Kardashevskiy     _FDT((fdt_create(fdt_skel, size)));
6232a6593cbSAlexey Kardashevskiy     _FDT((fdt_begin_node(fdt_skel, "")));
6242a6593cbSAlexey Kardashevskiy     _FDT((fdt_end_node(fdt_skel)));
6252a6593cbSAlexey Kardashevskiy     _FDT((fdt_finish(fdt_skel)));
6262a6593cbSAlexey Kardashevskiy     fdt = g_malloc0(size);
6272a6593cbSAlexey Kardashevskiy     _FDT((fdt_open_into(fdt_skel, fdt, size)));
6282a6593cbSAlexey Kardashevskiy     g_free(fdt_skel);
6292a6593cbSAlexey Kardashevskiy 
6303794d548SAlexey Kardashevskiy     /* Fix skeleton up */
6313794d548SAlexey Kardashevskiy     _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
6322a6593cbSAlexey Kardashevskiy 
6332a6593cbSAlexey Kardashevskiy     /* Pack resulting tree */
6342a6593cbSAlexey Kardashevskiy     _FDT((fdt_pack(fdt)));
6352a6593cbSAlexey Kardashevskiy 
6362a6593cbSAlexey Kardashevskiy     if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
6372a6593cbSAlexey Kardashevskiy         trace_spapr_cas_failed(size);
6382a6593cbSAlexey Kardashevskiy         return -1;
6392a6593cbSAlexey Kardashevskiy     }
6402a6593cbSAlexey Kardashevskiy 
6412a6593cbSAlexey Kardashevskiy     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
6422a6593cbSAlexey Kardashevskiy     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
6432a6593cbSAlexey Kardashevskiy     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
6442a6593cbSAlexey Kardashevskiy     g_free(fdt);
6452a6593cbSAlexey Kardashevskiy 
6462a6593cbSAlexey Kardashevskiy     return 0;
6472a6593cbSAlexey Kardashevskiy }
6482a6593cbSAlexey Kardashevskiy 
64926a8c353SAlexey Kardashevskiy static void spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
65026a8c353SAlexey Kardashevskiy                                        hwaddr size)
65126a8c353SAlexey Kardashevskiy {
65226a8c353SAlexey Kardashevskiy     uint32_t associativity[] = {
65326a8c353SAlexey Kardashevskiy         cpu_to_be32(0x4), /* length */
65426a8c353SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(0x0),
655c3b4f589SAlexey Kardashevskiy         cpu_to_be32(0x0), cpu_to_be32(nodeid)
65626a8c353SAlexey Kardashevskiy     };
65726a8c353SAlexey Kardashevskiy     char mem_name[32];
65826a8c353SAlexey Kardashevskiy     uint64_t mem_reg_property[2];
65926a8c353SAlexey Kardashevskiy     int off;
66026a8c353SAlexey Kardashevskiy 
66126a8c353SAlexey Kardashevskiy     mem_reg_property[0] = cpu_to_be64(start);
66226a8c353SAlexey Kardashevskiy     mem_reg_property[1] = cpu_to_be64(size);
66326a8c353SAlexey Kardashevskiy 
66426a8c353SAlexey Kardashevskiy     sprintf(mem_name, "memory@" TARGET_FMT_lx, start);
66526a8c353SAlexey Kardashevskiy     off = fdt_add_subnode(fdt, 0, mem_name);
66626a8c353SAlexey Kardashevskiy     _FDT(off);
66726a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
66826a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
66926a8c353SAlexey Kardashevskiy                       sizeof(mem_reg_property))));
67026a8c353SAlexey Kardashevskiy     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
67126a8c353SAlexey Kardashevskiy                       sizeof(associativity))));
67226a8c353SAlexey Kardashevskiy }
67326a8c353SAlexey Kardashevskiy 
67453018216SPaolo Bonzini static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt)
67553018216SPaolo Bonzini {
6767db8a127SAlexey Kardashevskiy     hwaddr mem_start, node_size;
6777db8a127SAlexey Kardashevskiy     int i, nb_nodes = nb_numa_nodes;
6787db8a127SAlexey Kardashevskiy     NodeInfo *nodes = numa_info;
6797db8a127SAlexey Kardashevskiy     NodeInfo ramnode;
68053018216SPaolo Bonzini 
6817db8a127SAlexey Kardashevskiy     /* No NUMA nodes, assume there is just one node with whole RAM */
6827db8a127SAlexey Kardashevskiy     if (!nb_numa_nodes) {
6837db8a127SAlexey Kardashevskiy         nb_nodes = 1;
6847db8a127SAlexey Kardashevskiy         ramnode.node_mem = ram_size;
6857db8a127SAlexey Kardashevskiy         nodes = &ramnode;
6865fe269b1SPaul Mackerras     }
68753018216SPaolo Bonzini 
6887db8a127SAlexey Kardashevskiy     for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
6897db8a127SAlexey Kardashevskiy         if (!nodes[i].node_mem) {
6907db8a127SAlexey Kardashevskiy             continue;
69153018216SPaolo Bonzini         }
6925fe269b1SPaul Mackerras         if (mem_start >= ram_size) {
6935fe269b1SPaul Mackerras             node_size = 0;
6945fe269b1SPaul Mackerras         } else {
6957db8a127SAlexey Kardashevskiy             node_size = nodes[i].node_mem;
6965fe269b1SPaul Mackerras             if (node_size > ram_size - mem_start) {
6975fe269b1SPaul Mackerras                 node_size = ram_size - mem_start;
6985fe269b1SPaul Mackerras             }
6995fe269b1SPaul Mackerras         }
7007db8a127SAlexey Kardashevskiy         if (!mem_start) {
7017db8a127SAlexey Kardashevskiy             /* ppc_spapr_init() checks for rma_size <= node0_size already */
7027db8a127SAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
7037db8a127SAlexey Kardashevskiy             mem_start += spapr->rma_size;
7047db8a127SAlexey Kardashevskiy             node_size -= spapr->rma_size;
7057db8a127SAlexey Kardashevskiy         }
7066010818cSAlexey Kardashevskiy         for ( ; node_size; ) {
7076010818cSAlexey Kardashevskiy             hwaddr sizetmp = pow2floor(node_size);
7086010818cSAlexey Kardashevskiy 
7096010818cSAlexey Kardashevskiy             /* mem_start != 0 here */
7106010818cSAlexey Kardashevskiy             if (ctzl(mem_start) < ctzl(sizetmp)) {
7116010818cSAlexey Kardashevskiy                 sizetmp = 1ULL << ctzl(mem_start);
7126010818cSAlexey Kardashevskiy             }
7136010818cSAlexey Kardashevskiy 
7146010818cSAlexey Kardashevskiy             spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
7156010818cSAlexey Kardashevskiy             node_size -= sizetmp;
7166010818cSAlexey Kardashevskiy             mem_start += sizetmp;
7176010818cSAlexey Kardashevskiy         }
71853018216SPaolo Bonzini     }
71953018216SPaolo Bonzini 
72053018216SPaolo Bonzini     return 0;
72153018216SPaolo Bonzini }
72253018216SPaolo Bonzini 
72353018216SPaolo Bonzini static void spapr_finalize_fdt(sPAPREnvironment *spapr,
72453018216SPaolo Bonzini                                hwaddr fdt_addr,
72553018216SPaolo Bonzini                                hwaddr rtas_addr,
72653018216SPaolo Bonzini                                hwaddr rtas_size)
72753018216SPaolo Bonzini {
72871461b0fSAlexey Kardashevskiy     int ret, i;
72971461b0fSAlexey Kardashevskiy     size_t cb = 0;
73071461b0fSAlexey Kardashevskiy     char *bootlist;
73153018216SPaolo Bonzini     void *fdt;
73253018216SPaolo Bonzini     sPAPRPHBState *phb;
73353018216SPaolo Bonzini 
73453018216SPaolo Bonzini     fdt = g_malloc(FDT_MAX_SIZE);
73553018216SPaolo Bonzini 
73653018216SPaolo Bonzini     /* open out the base tree into a temp buffer for the final tweaks */
73753018216SPaolo Bonzini     _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
73853018216SPaolo Bonzini 
73953018216SPaolo Bonzini     ret = spapr_populate_memory(spapr, fdt);
74053018216SPaolo Bonzini     if (ret < 0) {
74153018216SPaolo Bonzini         fprintf(stderr, "couldn't setup memory nodes in fdt\n");
74253018216SPaolo Bonzini         exit(1);
74353018216SPaolo Bonzini     }
74453018216SPaolo Bonzini 
74553018216SPaolo Bonzini     ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
74653018216SPaolo Bonzini     if (ret < 0) {
74753018216SPaolo Bonzini         fprintf(stderr, "couldn't setup vio devices in fdt\n");
74853018216SPaolo Bonzini         exit(1);
74953018216SPaolo Bonzini     }
75053018216SPaolo Bonzini 
75153018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
75253018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
75353018216SPaolo Bonzini     }
75453018216SPaolo Bonzini 
75553018216SPaolo Bonzini     if (ret < 0) {
75653018216SPaolo Bonzini         fprintf(stderr, "couldn't setup PCI devices in fdt\n");
75753018216SPaolo Bonzini         exit(1);
75853018216SPaolo Bonzini     }
75953018216SPaolo Bonzini 
76053018216SPaolo Bonzini     /* RTAS */
76153018216SPaolo Bonzini     ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
76253018216SPaolo Bonzini     if (ret < 0) {
76353018216SPaolo Bonzini         fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
76453018216SPaolo Bonzini     }
76553018216SPaolo Bonzini 
76653018216SPaolo Bonzini     /* Advertise NUMA via ibm,associativity */
76753018216SPaolo Bonzini     ret = spapr_fixup_cpu_dt(fdt, spapr);
76853018216SPaolo Bonzini     if (ret < 0) {
76953018216SPaolo Bonzini         fprintf(stderr, "Couldn't finalize CPU device tree properties\n");
77053018216SPaolo Bonzini     }
77153018216SPaolo Bonzini 
77271461b0fSAlexey Kardashevskiy     bootlist = get_boot_devices_list(&cb, true);
77371461b0fSAlexey Kardashevskiy     if (cb && bootlist) {
77471461b0fSAlexey Kardashevskiy         int offset = fdt_path_offset(fdt, "/chosen");
77571461b0fSAlexey Kardashevskiy         if (offset < 0) {
77671461b0fSAlexey Kardashevskiy             exit(1);
77771461b0fSAlexey Kardashevskiy         }
77871461b0fSAlexey Kardashevskiy         for (i = 0; i < cb; i++) {
77971461b0fSAlexey Kardashevskiy             if (bootlist[i] == '\n') {
78071461b0fSAlexey Kardashevskiy                 bootlist[i] = ' ';
78171461b0fSAlexey Kardashevskiy             }
78271461b0fSAlexey Kardashevskiy 
78371461b0fSAlexey Kardashevskiy         }
78471461b0fSAlexey Kardashevskiy         ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist);
78571461b0fSAlexey Kardashevskiy     }
78671461b0fSAlexey Kardashevskiy 
78753018216SPaolo Bonzini     if (!spapr->has_graphics) {
78853018216SPaolo Bonzini         spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
78953018216SPaolo Bonzini     }
79053018216SPaolo Bonzini 
79153018216SPaolo Bonzini     _FDT((fdt_pack(fdt)));
79253018216SPaolo Bonzini 
79353018216SPaolo Bonzini     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
79453018216SPaolo Bonzini         hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n",
79553018216SPaolo Bonzini                  fdt_totalsize(fdt), FDT_MAX_SIZE);
79653018216SPaolo Bonzini         exit(1);
79753018216SPaolo Bonzini     }
79853018216SPaolo Bonzini 
79953018216SPaolo Bonzini     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
80053018216SPaolo Bonzini 
801a21a7a70SGonglei     g_free(bootlist);
80253018216SPaolo Bonzini     g_free(fdt);
80353018216SPaolo Bonzini }
80453018216SPaolo Bonzini 
80553018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
80653018216SPaolo Bonzini {
80753018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
80853018216SPaolo Bonzini }
80953018216SPaolo Bonzini 
81053018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu)
81153018216SPaolo Bonzini {
81253018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
81353018216SPaolo Bonzini 
81453018216SPaolo Bonzini     if (msr_pr) {
81553018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
81653018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
81753018216SPaolo Bonzini     } else {
81853018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
81953018216SPaolo Bonzini     }
82053018216SPaolo Bonzini }
82153018216SPaolo Bonzini 
822e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
823e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
824e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
825e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
826e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
827e6b8fd24SSamuel Mendoza-Jonas 
82853018216SPaolo Bonzini static void spapr_reset_htab(sPAPREnvironment *spapr)
82953018216SPaolo Bonzini {
83053018216SPaolo Bonzini     long shift;
831e6b8fd24SSamuel Mendoza-Jonas     int index;
83253018216SPaolo Bonzini 
83353018216SPaolo Bonzini     /* allocate hash page table.  For now we always make this 16mb,
83453018216SPaolo Bonzini      * later we should probably make it scale to the size of guest
83553018216SPaolo Bonzini      * RAM */
83653018216SPaolo Bonzini 
83753018216SPaolo Bonzini     shift = kvmppc_reset_htab(spapr->htab_shift);
83853018216SPaolo Bonzini 
83953018216SPaolo Bonzini     if (shift > 0) {
84053018216SPaolo Bonzini         /* Kernel handles htab, we don't need to allocate one */
84153018216SPaolo Bonzini         spapr->htab_shift = shift;
8427c43bca0SAneesh Kumar K.V         kvmppc_kern_htab = true;
84301a57972SSamuel Mendoza-Jonas 
84401a57972SSamuel Mendoza-Jonas         /* Tell readers to update their file descriptor */
84501a57972SSamuel Mendoza-Jonas         if (spapr->htab_fd >= 0) {
84601a57972SSamuel Mendoza-Jonas             spapr->htab_fd_stale = true;
84701a57972SSamuel Mendoza-Jonas         }
84853018216SPaolo Bonzini     } else {
84953018216SPaolo Bonzini         if (!spapr->htab) {
85053018216SPaolo Bonzini             /* Allocate an htab if we don't yet have one */
85153018216SPaolo Bonzini             spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr));
85253018216SPaolo Bonzini         }
85353018216SPaolo Bonzini 
85453018216SPaolo Bonzini         /* And clear it */
85553018216SPaolo Bonzini         memset(spapr->htab, 0, HTAB_SIZE(spapr));
856e6b8fd24SSamuel Mendoza-Jonas 
857e6b8fd24SSamuel Mendoza-Jonas         for (index = 0; index < HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; index++) {
858e6b8fd24SSamuel Mendoza-Jonas             DIRTY_HPTE(HPTE(spapr->htab, index));
859e6b8fd24SSamuel Mendoza-Jonas         }
86053018216SPaolo Bonzini     }
86153018216SPaolo Bonzini 
86253018216SPaolo Bonzini     /* Update the RMA size if necessary */
86353018216SPaolo Bonzini     if (spapr->vrma_adjust) {
864b082d65aSAlexey Kardashevskiy         spapr->rma_size = kvmppc_rma_size(spapr_node0_size(),
865b082d65aSAlexey Kardashevskiy                                           spapr->htab_shift);
86653018216SPaolo Bonzini     }
86753018216SPaolo Bonzini }
86853018216SPaolo Bonzini 
8699e3f9733SAlexander Graf static int find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque)
8709e3f9733SAlexander Graf {
8719e3f9733SAlexander Graf     bool matched = false;
8729e3f9733SAlexander Graf 
8739e3f9733SAlexander Graf     if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
8749e3f9733SAlexander Graf         matched = true;
8759e3f9733SAlexander Graf     }
8769e3f9733SAlexander Graf 
8779e3f9733SAlexander Graf     if (!matched) {
8789e3f9733SAlexander Graf         error_report("Device %s is not supported by this machine yet.",
8799e3f9733SAlexander Graf                      qdev_fw_name(DEVICE(sbdev)));
8809e3f9733SAlexander Graf         exit(1);
8819e3f9733SAlexander Graf     }
8829e3f9733SAlexander Graf 
8839e3f9733SAlexander Graf     return 0;
8849e3f9733SAlexander Graf }
8859e3f9733SAlexander Graf 
88601a57972SSamuel Mendoza-Jonas /*
88701a57972SSamuel Mendoza-Jonas  * A guest reset will cause spapr->htab_fd to become stale if being used.
88801a57972SSamuel Mendoza-Jonas  * Reopen the file descriptor to make sure the whole HTAB is properly read.
88901a57972SSamuel Mendoza-Jonas  */
89001a57972SSamuel Mendoza-Jonas static int spapr_check_htab_fd(sPAPREnvironment *spapr)
89101a57972SSamuel Mendoza-Jonas {
89201a57972SSamuel Mendoza-Jonas     int rc = 0;
89301a57972SSamuel Mendoza-Jonas 
89401a57972SSamuel Mendoza-Jonas     if (spapr->htab_fd_stale) {
89501a57972SSamuel Mendoza-Jonas         close(spapr->htab_fd);
89601a57972SSamuel Mendoza-Jonas         spapr->htab_fd = kvmppc_get_htab_fd(false);
89701a57972SSamuel Mendoza-Jonas         if (spapr->htab_fd < 0) {
89801a57972SSamuel Mendoza-Jonas             error_report("Unable to open fd for reading hash table from KVM: "
89901a57972SSamuel Mendoza-Jonas                     "%s", strerror(errno));
90001a57972SSamuel Mendoza-Jonas             rc = -1;
90101a57972SSamuel Mendoza-Jonas         }
90201a57972SSamuel Mendoza-Jonas         spapr->htab_fd_stale = false;
90301a57972SSamuel Mendoza-Jonas     }
90401a57972SSamuel Mendoza-Jonas 
90501a57972SSamuel Mendoza-Jonas     return rc;
90601a57972SSamuel Mendoza-Jonas }
90701a57972SSamuel Mendoza-Jonas 
90853018216SPaolo Bonzini static void ppc_spapr_reset(void)
90953018216SPaolo Bonzini {
910182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
911b7d1f77aSBenjamin Herrenschmidt     uint32_t rtas_limit;
912259186a7SAndreas Färber 
9139e3f9733SAlexander Graf     /* Check for unknown sysbus devices */
9149e3f9733SAlexander Graf     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
9159e3f9733SAlexander Graf 
91653018216SPaolo Bonzini     /* Reset the hash table & recalc the RMA */
91753018216SPaolo Bonzini     spapr_reset_htab(spapr);
91853018216SPaolo Bonzini 
91953018216SPaolo Bonzini     qemu_devices_reset();
92053018216SPaolo Bonzini 
921b7d1f77aSBenjamin Herrenschmidt     /*
922b7d1f77aSBenjamin Herrenschmidt      * We place the device tree and RTAS just below either the top of the RMA,
923b7d1f77aSBenjamin Herrenschmidt      * or just below 2GB, whichever is lowere, so that it can be
924b7d1f77aSBenjamin Herrenschmidt      * processed with 32-bit real mode code if necessary
925b7d1f77aSBenjamin Herrenschmidt      */
926b7d1f77aSBenjamin Herrenschmidt     rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR);
927b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
928b7d1f77aSBenjamin Herrenschmidt     spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
929b7d1f77aSBenjamin Herrenschmidt 
93053018216SPaolo Bonzini     /* Load the fdt */
93153018216SPaolo Bonzini     spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
93253018216SPaolo Bonzini                        spapr->rtas_size);
93353018216SPaolo Bonzini 
934b7d1f77aSBenjamin Herrenschmidt     /* Copy RTAS over */
935b7d1f77aSBenjamin Herrenschmidt     cpu_physical_memory_write(spapr->rtas_addr, spapr->rtas_blob,
936b7d1f77aSBenjamin Herrenschmidt                               spapr->rtas_size);
937b7d1f77aSBenjamin Herrenschmidt 
93853018216SPaolo Bonzini     /* Set up the entry state */
939182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
940182735efSAndreas Färber     first_ppc_cpu->env.gpr[3] = spapr->fdt_addr;
941182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
942182735efSAndreas Färber     first_cpu->halted = 0;
943182735efSAndreas Färber     first_ppc_cpu->env.nip = spapr->entry_point;
94453018216SPaolo Bonzini 
94553018216SPaolo Bonzini }
94653018216SPaolo Bonzini 
94753018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque)
94853018216SPaolo Bonzini {
94953018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
950259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
95153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
95253018216SPaolo Bonzini 
953259186a7SAndreas Färber     cpu_reset(cs);
95453018216SPaolo Bonzini 
95553018216SPaolo Bonzini     /* All CPUs start halted.  CPU0 is unhalted from the machine level
95653018216SPaolo Bonzini      * reset code and the rest are explicitly started up by the guest
95753018216SPaolo Bonzini      * using an RTAS call */
958259186a7SAndreas Färber     cs->halted = 1;
95953018216SPaolo Bonzini 
96053018216SPaolo Bonzini     env->spr[SPR_HIOR] = 0;
96153018216SPaolo Bonzini 
9624be21d56SDavid Gibson     env->external_htab = (uint8_t *)spapr->htab;
9635736245cSAneesh Kumar K.V     if (kvm_enabled() && !env->external_htab) {
9645736245cSAneesh Kumar K.V         /*
9655736245cSAneesh Kumar K.V          * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte*
9665736245cSAneesh Kumar K.V          * functions do the right thing.
9675736245cSAneesh Kumar K.V          */
9685736245cSAneesh Kumar K.V         env->external_htab = (void *)1;
9695736245cSAneesh Kumar K.V     }
97053018216SPaolo Bonzini     env->htab_base = -1;
971f3c75d42SAneesh Kumar K.V     /*
972f3c75d42SAneesh Kumar K.V      * htab_mask is the mask used to normalize hash value to PTEG index.
973f3c75d42SAneesh Kumar K.V      * htab_shift is log2 of hash table size.
974f3c75d42SAneesh Kumar K.V      * We have 8 hpte per group, and each hpte is 16 bytes.
975f3c75d42SAneesh Kumar K.V      * ie have 128 bytes per hpte entry.
976f3c75d42SAneesh Kumar K.V      */
977f3c75d42SAneesh Kumar K.V     env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1;
978ec4936e1SStefan Weil     env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
97953018216SPaolo Bonzini         (spapr->htab_shift - 18);
98053018216SPaolo Bonzini }
98153018216SPaolo Bonzini 
98253018216SPaolo Bonzini static void spapr_create_nvram(sPAPREnvironment *spapr)
98353018216SPaolo Bonzini {
9842ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
9853978b863SPaolo Bonzini     DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
98653018216SPaolo Bonzini 
9873978b863SPaolo Bonzini     if (dinfo) {
9884be74634SMarkus Armbruster         qdev_prop_set_drive_nofail(dev, "drive", blk_by_legacy_dinfo(dinfo));
98953018216SPaolo Bonzini     }
99053018216SPaolo Bonzini 
99153018216SPaolo Bonzini     qdev_init_nofail(dev);
99253018216SPaolo Bonzini 
99353018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
99453018216SPaolo Bonzini }
99553018216SPaolo Bonzini 
99653018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
99753018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus)
99853018216SPaolo Bonzini {
99953018216SPaolo Bonzini     switch (vga_interface_type) {
100053018216SPaolo Bonzini     case VGA_NONE:
10017effdaa3SMark Wu         return false;
10027effdaa3SMark Wu     case VGA_DEVICE:
10037effdaa3SMark Wu         return true;
100453018216SPaolo Bonzini     case VGA_STD:
100553018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
100653018216SPaolo Bonzini     default:
100753018216SPaolo Bonzini         fprintf(stderr, "This vga model is not supported,"
100853018216SPaolo Bonzini                 "currently it only supports -vga std\n");
100953018216SPaolo Bonzini         exit(0);
101053018216SPaolo Bonzini     }
101153018216SPaolo Bonzini }
101253018216SPaolo Bonzini 
10134be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
10144be21d56SDavid Gibson     .name = "spapr",
101598a8b524SAlexey Kardashevskiy     .version_id = 2,
10164be21d56SDavid Gibson     .minimum_version_id = 1,
10174be21d56SDavid Gibson     .fields = (VMStateField[]) {
1018ba0e5bf8SAlexey Kardashevskiy         VMSTATE_UNUSED(4), /* used to be @next_irq */
10194be21d56SDavid Gibson 
10204be21d56SDavid Gibson         /* RTC offset */
10214be21d56SDavid Gibson         VMSTATE_UINT64(rtc_offset, sPAPREnvironment),
102298a8b524SAlexey Kardashevskiy         VMSTATE_PPC_TIMEBASE_V(tb, sPAPREnvironment, 2),
10234be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
10244be21d56SDavid Gibson     },
10254be21d56SDavid Gibson };
10264be21d56SDavid Gibson 
10274be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
10284be21d56SDavid Gibson {
10294be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
10304be21d56SDavid Gibson 
10314be21d56SDavid Gibson     /* "Iteration" header */
10324be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
10334be21d56SDavid Gibson 
1034e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
1035e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
1036e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
1037e68cb8b4SAlexey Kardashevskiy     } else {
1038e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1039e68cb8b4SAlexey Kardashevskiy 
1040e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = kvmppc_get_htab_fd(false);
104101a57972SSamuel Mendoza-Jonas         spapr->htab_fd_stale = false;
1042e68cb8b4SAlexey Kardashevskiy         if (spapr->htab_fd < 0) {
1043e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n",
1044e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1045e68cb8b4SAlexey Kardashevskiy             return -1;
1046e68cb8b4SAlexey Kardashevskiy         }
10474be21d56SDavid Gibson     }
10484be21d56SDavid Gibson 
1049e68cb8b4SAlexey Kardashevskiy 
1050e68cb8b4SAlexey Kardashevskiy     return 0;
1051e68cb8b4SAlexey Kardashevskiy }
10524be21d56SDavid Gibson 
10534be21d56SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr,
10544be21d56SDavid Gibson                                  int64_t max_ns)
10554be21d56SDavid Gibson {
10564be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
10574be21d56SDavid Gibson     int index = spapr->htab_save_index;
1058bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
10594be21d56SDavid Gibson 
10604be21d56SDavid Gibson     assert(spapr->htab_first_pass);
10614be21d56SDavid Gibson 
10624be21d56SDavid Gibson     do {
10634be21d56SDavid Gibson         int chunkstart;
10644be21d56SDavid Gibson 
10654be21d56SDavid Gibson         /* Consume invalid HPTEs */
10664be21d56SDavid Gibson         while ((index < htabslots)
10674be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
10684be21d56SDavid Gibson             index++;
10694be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
10704be21d56SDavid Gibson         }
10714be21d56SDavid Gibson 
10724be21d56SDavid Gibson         /* Consume valid HPTEs */
10734be21d56SDavid Gibson         chunkstart = index;
1074338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
10754be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
10764be21d56SDavid Gibson             index++;
10774be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
10784be21d56SDavid Gibson         }
10794be21d56SDavid Gibson 
10804be21d56SDavid Gibson         if (index > chunkstart) {
10814be21d56SDavid Gibson             int n_valid = index - chunkstart;
10824be21d56SDavid Gibson 
10834be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
10844be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
10854be21d56SDavid Gibson             qemu_put_be16(f, 0);
10864be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
10874be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
10884be21d56SDavid Gibson 
1089bc72ad67SAlex Bligh             if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
10904be21d56SDavid Gibson                 break;
10914be21d56SDavid Gibson             }
10924be21d56SDavid Gibson         }
10934be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
10944be21d56SDavid Gibson 
10954be21d56SDavid Gibson     if (index >= htabslots) {
10964be21d56SDavid Gibson         assert(index == htabslots);
10974be21d56SDavid Gibson         index = 0;
10984be21d56SDavid Gibson         spapr->htab_first_pass = false;
10994be21d56SDavid Gibson     }
11004be21d56SDavid Gibson     spapr->htab_save_index = index;
11014be21d56SDavid Gibson }
11024be21d56SDavid Gibson 
1103e68cb8b4SAlexey Kardashevskiy static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr,
11044be21d56SDavid Gibson                                 int64_t max_ns)
11054be21d56SDavid Gibson {
11064be21d56SDavid Gibson     bool final = max_ns < 0;
11074be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
11084be21d56SDavid Gibson     int examined = 0, sent = 0;
11094be21d56SDavid Gibson     int index = spapr->htab_save_index;
1110bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
11114be21d56SDavid Gibson 
11124be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
11134be21d56SDavid Gibson 
11144be21d56SDavid Gibson     do {
11154be21d56SDavid Gibson         int chunkstart, invalidstart;
11164be21d56SDavid Gibson 
11174be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
11184be21d56SDavid Gibson         while ((index < htabslots)
11194be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
11204be21d56SDavid Gibson             index++;
11214be21d56SDavid Gibson             examined++;
11224be21d56SDavid Gibson         }
11234be21d56SDavid Gibson 
11244be21d56SDavid Gibson         chunkstart = index;
11254be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
1126338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
11274be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
11284be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
11294be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
11304be21d56SDavid Gibson             index++;
11314be21d56SDavid Gibson             examined++;
11324be21d56SDavid Gibson         }
11334be21d56SDavid Gibson 
11344be21d56SDavid Gibson         invalidstart = index;
11354be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
1136338c25b6SSamuel Mendoza-Jonas         while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
11374be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
11384be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
11394be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
11404be21d56SDavid Gibson             index++;
11414be21d56SDavid Gibson             examined++;
11424be21d56SDavid Gibson         }
11434be21d56SDavid Gibson 
11444be21d56SDavid Gibson         if (index > chunkstart) {
11454be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
11464be21d56SDavid Gibson             int n_invalid = index - invalidstart;
11474be21d56SDavid Gibson 
11484be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
11494be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
11504be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
11514be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
11524be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
11534be21d56SDavid Gibson             sent += index - chunkstart;
11544be21d56SDavid Gibson 
1155bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
11564be21d56SDavid Gibson                 break;
11574be21d56SDavid Gibson             }
11584be21d56SDavid Gibson         }
11594be21d56SDavid Gibson 
11604be21d56SDavid Gibson         if (examined >= htabslots) {
11614be21d56SDavid Gibson             break;
11624be21d56SDavid Gibson         }
11634be21d56SDavid Gibson 
11644be21d56SDavid Gibson         if (index >= htabslots) {
11654be21d56SDavid Gibson             assert(index == htabslots);
11664be21d56SDavid Gibson             index = 0;
11674be21d56SDavid Gibson         }
11684be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
11694be21d56SDavid Gibson 
11704be21d56SDavid Gibson     if (index >= htabslots) {
11714be21d56SDavid Gibson         assert(index == htabslots);
11724be21d56SDavid Gibson         index = 0;
11734be21d56SDavid Gibson     }
11744be21d56SDavid Gibson 
11754be21d56SDavid Gibson     spapr->htab_save_index = index;
11764be21d56SDavid Gibson 
1177e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
11784be21d56SDavid Gibson }
11794be21d56SDavid Gibson 
1180e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
1181e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
1182e68cb8b4SAlexey Kardashevskiy 
11834be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
11844be21d56SDavid Gibson {
11854be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
1186e68cb8b4SAlexey Kardashevskiy     int rc = 0;
11874be21d56SDavid Gibson 
11884be21d56SDavid Gibson     /* Iteration header */
11894be21d56SDavid Gibson     qemu_put_be32(f, 0);
11904be21d56SDavid Gibson 
1191e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1192e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1193e68cb8b4SAlexey Kardashevskiy 
119401a57972SSamuel Mendoza-Jonas         rc = spapr_check_htab_fd(spapr);
119501a57972SSamuel Mendoza-Jonas         if (rc < 0) {
119601a57972SSamuel Mendoza-Jonas             return rc;
119701a57972SSamuel Mendoza-Jonas         }
119801a57972SSamuel Mendoza-Jonas 
1199e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd,
1200e68cb8b4SAlexey Kardashevskiy                               MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1201e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1202e68cb8b4SAlexey Kardashevskiy             return rc;
1203e68cb8b4SAlexey Kardashevskiy         }
1204e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
12054be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
12064be21d56SDavid Gibson     } else {
1207e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
12084be21d56SDavid Gibson     }
12094be21d56SDavid Gibson 
12104be21d56SDavid Gibson     /* End marker */
12114be21d56SDavid Gibson     qemu_put_be32(f, 0);
12124be21d56SDavid Gibson     qemu_put_be16(f, 0);
12134be21d56SDavid Gibson     qemu_put_be16(f, 0);
12144be21d56SDavid Gibson 
1215e68cb8b4SAlexey Kardashevskiy     return rc;
12164be21d56SDavid Gibson }
12174be21d56SDavid Gibson 
12184be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
12194be21d56SDavid Gibson {
12204be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
12214be21d56SDavid Gibson 
12224be21d56SDavid Gibson     /* Iteration header */
12234be21d56SDavid Gibson     qemu_put_be32(f, 0);
12244be21d56SDavid Gibson 
1225e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1226e68cb8b4SAlexey Kardashevskiy         int rc;
1227e68cb8b4SAlexey Kardashevskiy 
1228e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1229e68cb8b4SAlexey Kardashevskiy 
123001a57972SSamuel Mendoza-Jonas         rc = spapr_check_htab_fd(spapr);
123101a57972SSamuel Mendoza-Jonas         if (rc < 0) {
123201a57972SSamuel Mendoza-Jonas             return rc;
123301a57972SSamuel Mendoza-Jonas         }
123401a57972SSamuel Mendoza-Jonas 
1235e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1);
1236e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
1237e68cb8b4SAlexey Kardashevskiy             return rc;
1238e68cb8b4SAlexey Kardashevskiy         }
1239e68cb8b4SAlexey Kardashevskiy         close(spapr->htab_fd);
1240e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = -1;
1241e68cb8b4SAlexey Kardashevskiy     } else {
12424be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1243e68cb8b4SAlexey Kardashevskiy     }
12444be21d56SDavid Gibson 
12454be21d56SDavid Gibson     /* End marker */
12464be21d56SDavid Gibson     qemu_put_be32(f, 0);
12474be21d56SDavid Gibson     qemu_put_be16(f, 0);
12484be21d56SDavid Gibson     qemu_put_be16(f, 0);
12494be21d56SDavid Gibson 
12504be21d56SDavid Gibson     return 0;
12514be21d56SDavid Gibson }
12524be21d56SDavid Gibson 
12534be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
12544be21d56SDavid Gibson {
12554be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
12564be21d56SDavid Gibson     uint32_t section_hdr;
1257e68cb8b4SAlexey Kardashevskiy     int fd = -1;
12584be21d56SDavid Gibson 
12594be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
12604be21d56SDavid Gibson         fprintf(stderr, "htab_load() bad version\n");
12614be21d56SDavid Gibson         return -EINVAL;
12624be21d56SDavid Gibson     }
12634be21d56SDavid Gibson 
12644be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
12654be21d56SDavid Gibson 
12664be21d56SDavid Gibson     if (section_hdr) {
12674be21d56SDavid Gibson         /* First section, just the hash shift */
12684be21d56SDavid Gibson         if (spapr->htab_shift != section_hdr) {
12694be21d56SDavid Gibson             return -EINVAL;
12704be21d56SDavid Gibson         }
12714be21d56SDavid Gibson         return 0;
12724be21d56SDavid Gibson     }
12734be21d56SDavid Gibson 
1274e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1275e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1276e68cb8b4SAlexey Kardashevskiy 
1277e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1278e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
1279e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n",
1280e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1281e68cb8b4SAlexey Kardashevskiy         }
1282e68cb8b4SAlexey Kardashevskiy     }
1283e68cb8b4SAlexey Kardashevskiy 
12844be21d56SDavid Gibson     while (true) {
12854be21d56SDavid Gibson         uint32_t index;
12864be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
12874be21d56SDavid Gibson 
12884be21d56SDavid Gibson         index = qemu_get_be32(f);
12894be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
12904be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
12914be21d56SDavid Gibson 
12924be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
12934be21d56SDavid Gibson             /* End of Stream */
12944be21d56SDavid Gibson             break;
12954be21d56SDavid Gibson         }
12964be21d56SDavid Gibson 
1297e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
12984be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
12994be21d56SDavid Gibson             /* Bad index in stream */
13004be21d56SDavid Gibson             fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) "
1301e68cb8b4SAlexey Kardashevskiy                     "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid,
1302e68cb8b4SAlexey Kardashevskiy                     spapr->htab_shift);
13034be21d56SDavid Gibson             return -EINVAL;
13044be21d56SDavid Gibson         }
13054be21d56SDavid Gibson 
1306e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
13074be21d56SDavid Gibson             if (n_valid) {
13084be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
13094be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
13104be21d56SDavid Gibson             }
13114be21d56SDavid Gibson             if (n_invalid) {
13124be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
13134be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
13144be21d56SDavid Gibson             }
1315e68cb8b4SAlexey Kardashevskiy         } else {
1316e68cb8b4SAlexey Kardashevskiy             int rc;
1317e68cb8b4SAlexey Kardashevskiy 
1318e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1319e68cb8b4SAlexey Kardashevskiy 
1320e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1321e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1322e68cb8b4SAlexey Kardashevskiy                 return rc;
1323e68cb8b4SAlexey Kardashevskiy             }
1324e68cb8b4SAlexey Kardashevskiy         }
1325e68cb8b4SAlexey Kardashevskiy     }
1326e68cb8b4SAlexey Kardashevskiy 
1327e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1328e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1329e68cb8b4SAlexey Kardashevskiy         close(fd);
13304be21d56SDavid Gibson     }
13314be21d56SDavid Gibson 
13324be21d56SDavid Gibson     return 0;
13334be21d56SDavid Gibson }
13344be21d56SDavid Gibson 
13354be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
13364be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
13374be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
13384be21d56SDavid Gibson     .save_live_complete = htab_save_complete,
13394be21d56SDavid Gibson     .load_state = htab_load,
13404be21d56SDavid Gibson };
13414be21d56SDavid Gibson 
134253018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
13433ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine)
134453018216SPaolo Bonzini {
13453ef96221SMarcel Apfelbaum     ram_addr_t ram_size = machine->ram_size;
13463ef96221SMarcel Apfelbaum     const char *cpu_model = machine->cpu_model;
13473ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
13483ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
13493ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
13503ef96221SMarcel Apfelbaum     const char *boot_device = machine->boot_order;
135153018216SPaolo Bonzini     PowerPCCPU *cpu;
135253018216SPaolo Bonzini     CPUPPCState *env;
135353018216SPaolo Bonzini     PCIHostState *phb;
135453018216SPaolo Bonzini     int i;
135553018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
135653018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
1357658fa66bSAlexey Kardashevskiy     MemoryRegion *rma_region;
1358658fa66bSAlexey Kardashevskiy     void *rma = NULL;
135953018216SPaolo Bonzini     hwaddr rma_alloc_size;
1360b082d65aSAlexey Kardashevskiy     hwaddr node0_size = spapr_node0_size();
136153018216SPaolo Bonzini     uint32_t initrd_base = 0;
136253018216SPaolo Bonzini     long kernel_size = 0, initrd_size = 0;
1363b7d1f77aSBenjamin Herrenschmidt     long load_limit, fw_size;
136416457e7fSBenjamin Herrenschmidt     bool kernel_le = false;
136553018216SPaolo Bonzini     char *filename;
136653018216SPaolo Bonzini 
136753018216SPaolo Bonzini     msi_supported = true;
136853018216SPaolo Bonzini 
136953018216SPaolo Bonzini     spapr = g_malloc0(sizeof(*spapr));
137053018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
137153018216SPaolo Bonzini 
137253018216SPaolo Bonzini     cpu_ppc_hypercall = emulate_spapr_hypercall;
137353018216SPaolo Bonzini 
137453018216SPaolo Bonzini     /* Allocate RMA if necessary */
1375658fa66bSAlexey Kardashevskiy     rma_alloc_size = kvmppc_alloc_rma(&rma);
137653018216SPaolo Bonzini 
137753018216SPaolo Bonzini     if (rma_alloc_size == -1) {
137853018216SPaolo Bonzini         hw_error("qemu: Unable to create RMA\n");
137953018216SPaolo Bonzini         exit(1);
138053018216SPaolo Bonzini     }
138153018216SPaolo Bonzini 
1382c4177479SAlexey Kardashevskiy     if (rma_alloc_size && (rma_alloc_size < node0_size)) {
138353018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
138453018216SPaolo Bonzini     } else {
1385c4177479SAlexey Kardashevskiy         spapr->rma_size = node0_size;
138653018216SPaolo Bonzini 
138753018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
138853018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
138953018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
139053018216SPaolo Bonzini          *
139153018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
139253018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
139353018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
139453018216SPaolo Bonzini          * isn't determined yet.
139553018216SPaolo Bonzini          */
139653018216SPaolo Bonzini         if (kvm_enabled()) {
139753018216SPaolo Bonzini             spapr->vrma_adjust = 1;
139853018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
139953018216SPaolo Bonzini         }
140053018216SPaolo Bonzini     }
140153018216SPaolo Bonzini 
1402c4177479SAlexey Kardashevskiy     if (spapr->rma_size > node0_size) {
1403c4177479SAlexey Kardashevskiy         fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n",
1404c4177479SAlexey Kardashevskiy                 spapr->rma_size);
1405c4177479SAlexey Kardashevskiy         exit(1);
1406c4177479SAlexey Kardashevskiy     }
1407c4177479SAlexey Kardashevskiy 
1408b7d1f77aSBenjamin Herrenschmidt     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
1409b7d1f77aSBenjamin Herrenschmidt     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
141053018216SPaolo Bonzini 
141153018216SPaolo Bonzini     /* We aim for a hash table of size 1/128 the size of RAM.  The
141253018216SPaolo Bonzini      * normal rule of thumb is 1/64 the size of RAM, but that's much
141353018216SPaolo Bonzini      * more than needed for the Linux guests we support. */
141453018216SPaolo Bonzini     spapr->htab_shift = 18; /* Minimum architected size */
141553018216SPaolo Bonzini     while (spapr->htab_shift <= 46) {
141653018216SPaolo Bonzini         if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) {
141753018216SPaolo Bonzini             break;
141853018216SPaolo Bonzini         }
141953018216SPaolo Bonzini         spapr->htab_shift++;
142053018216SPaolo Bonzini     }
142153018216SPaolo Bonzini 
14227b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
14237b565160SDavid Gibson     spapr->icp = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads,
14247b565160SDavid Gibson                                   XICS_IRQS);
14257b565160SDavid Gibson 
142653018216SPaolo Bonzini     /* init CPUs */
142753018216SPaolo Bonzini     if (cpu_model == NULL) {
142853018216SPaolo Bonzini         cpu_model = kvm_enabled() ? "host" : "POWER7";
142953018216SPaolo Bonzini     }
143053018216SPaolo Bonzini     for (i = 0; i < smp_cpus; i++) {
143153018216SPaolo Bonzini         cpu = cpu_ppc_init(cpu_model);
143253018216SPaolo Bonzini         if (cpu == NULL) {
143353018216SPaolo Bonzini             fprintf(stderr, "Unable to find PowerPC CPU definition\n");
143453018216SPaolo Bonzini             exit(1);
143553018216SPaolo Bonzini         }
143653018216SPaolo Bonzini         env = &cpu->env;
143753018216SPaolo Bonzini 
143853018216SPaolo Bonzini         /* Set time-base frequency to 512 MHz */
143953018216SPaolo Bonzini         cpu_ppc_tb_init(env, TIMEBASE_FREQ);
144053018216SPaolo Bonzini 
14412cf3eb6dSFabien Chouteau         /* PAPR always has exception vectors in RAM not ROM. To ensure this,
14422cf3eb6dSFabien Chouteau          * MSR[IP] should never be set.
14432cf3eb6dSFabien Chouteau          */
14442cf3eb6dSFabien Chouteau         env->msr_mask &= ~(1 << 6);
144553018216SPaolo Bonzini 
144653018216SPaolo Bonzini         /* Tell KVM that we're in PAPR mode */
144753018216SPaolo Bonzini         if (kvm_enabled()) {
144853018216SPaolo Bonzini             kvmppc_set_papr(cpu);
144953018216SPaolo Bonzini         }
145053018216SPaolo Bonzini 
14516d9412eaSAlexey Kardashevskiy         if (cpu->max_compat) {
14526d9412eaSAlexey Kardashevskiy             if (ppc_set_compat(cpu, cpu->max_compat) < 0) {
14536d9412eaSAlexey Kardashevskiy                 exit(1);
14546d9412eaSAlexey Kardashevskiy             }
14556d9412eaSAlexey Kardashevskiy         }
14566d9412eaSAlexey Kardashevskiy 
145724408a7dSAlexey Kardashevskiy         xics_cpu_setup(spapr->icp, cpu);
145824408a7dSAlexey Kardashevskiy 
145953018216SPaolo Bonzini         qemu_register_reset(spapr_cpu_reset, cpu);
146053018216SPaolo Bonzini     }
146153018216SPaolo Bonzini 
146253018216SPaolo Bonzini     /* allocate RAM */
146353018216SPaolo Bonzini     spapr->ram_limit = ram_size;
1464f92f5da1SAlexey Kardashevskiy     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
1465f92f5da1SAlexey Kardashevskiy                                          spapr->ram_limit);
1466f92f5da1SAlexey Kardashevskiy     memory_region_add_subregion(sysmem, 0, ram);
146753018216SPaolo Bonzini 
1468658fa66bSAlexey Kardashevskiy     if (rma_alloc_size && rma) {
1469658fa66bSAlexey Kardashevskiy         rma_region = g_new(MemoryRegion, 1);
1470658fa66bSAlexey Kardashevskiy         memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma",
1471658fa66bSAlexey Kardashevskiy                                    rma_alloc_size, rma);
1472658fa66bSAlexey Kardashevskiy         vmstate_register_ram_global(rma_region);
1473658fa66bSAlexey Kardashevskiy         memory_region_add_subregion(sysmem, 0, rma_region);
1474658fa66bSAlexey Kardashevskiy     }
1475658fa66bSAlexey Kardashevskiy 
147653018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
1477b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_size = get_image_size(filename);
1478b7d1f77aSBenjamin Herrenschmidt     spapr->rtas_blob = g_malloc(spapr->rtas_size);
1479b7d1f77aSBenjamin Herrenschmidt     if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) {
148053018216SPaolo Bonzini         hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
148153018216SPaolo Bonzini         exit(1);
148253018216SPaolo Bonzini     }
148353018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
1484b7d1f77aSBenjamin Herrenschmidt         hw_error("RTAS too big ! 0x%zx bytes (max is 0x%x)\n",
1485*2f285bddSPeter Maydell                  (size_t)spapr->rtas_size, RTAS_MAX_SIZE);
148653018216SPaolo Bonzini         exit(1);
148753018216SPaolo Bonzini     }
148853018216SPaolo Bonzini     g_free(filename);
148953018216SPaolo Bonzini 
149053018216SPaolo Bonzini     /* Set up EPOW events infrastructure */
149153018216SPaolo Bonzini     spapr_events_init(spapr);
149253018216SPaolo Bonzini 
149353018216SPaolo Bonzini     /* Set up VIO bus */
149453018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
149553018216SPaolo Bonzini 
149653018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
149753018216SPaolo Bonzini         if (serial_hds[i]) {
149853018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
149953018216SPaolo Bonzini         }
150053018216SPaolo Bonzini     }
150153018216SPaolo Bonzini 
150253018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
150353018216SPaolo Bonzini     spapr_create_nvram(spapr);
150453018216SPaolo Bonzini 
150553018216SPaolo Bonzini     /* Set up PCI */
150653018216SPaolo Bonzini     spapr_pci_rtas_init();
150753018216SPaolo Bonzini 
150889dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
150953018216SPaolo Bonzini 
151053018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
151153018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
151253018216SPaolo Bonzini 
151353018216SPaolo Bonzini         if (!nd->model) {
151453018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
151553018216SPaolo Bonzini         }
151653018216SPaolo Bonzini 
151753018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
151853018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
151953018216SPaolo Bonzini         } else {
152029b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
152153018216SPaolo Bonzini         }
152253018216SPaolo Bonzini     }
152353018216SPaolo Bonzini 
152453018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
152553018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
152653018216SPaolo Bonzini     }
152753018216SPaolo Bonzini 
152853018216SPaolo Bonzini     /* Graphics */
152953018216SPaolo Bonzini     if (spapr_vga_init(phb->bus)) {
153053018216SPaolo Bonzini         spapr->has_graphics = true;
153153018216SPaolo Bonzini     }
153253018216SPaolo Bonzini 
153353018216SPaolo Bonzini     if (usb_enabled(spapr->has_graphics)) {
153453018216SPaolo Bonzini         pci_create_simple(phb->bus, -1, "pci-ohci");
153553018216SPaolo Bonzini         if (spapr->has_graphics) {
153653018216SPaolo Bonzini             usbdevice_create("keyboard");
153753018216SPaolo Bonzini             usbdevice_create("mouse");
153853018216SPaolo Bonzini         }
153953018216SPaolo Bonzini     }
154053018216SPaolo Bonzini 
154153018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
154253018216SPaolo Bonzini         fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
154353018216SPaolo Bonzini                 "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
154453018216SPaolo Bonzini         exit(1);
154553018216SPaolo Bonzini     }
154653018216SPaolo Bonzini 
154753018216SPaolo Bonzini     if (kernel_filename) {
154853018216SPaolo Bonzini         uint64_t lowaddr = 0;
154953018216SPaolo Bonzini 
155053018216SPaolo Bonzini         kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
155153018216SPaolo Bonzini                                NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
15523b66da82SAlexey Kardashevskiy         if (kernel_size == ELF_LOAD_WRONG_ENDIAN) {
155316457e7fSBenjamin Herrenschmidt             kernel_size = load_elf(kernel_filename,
155416457e7fSBenjamin Herrenschmidt                                    translate_kernel_address, NULL,
155516457e7fSBenjamin Herrenschmidt                                    NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0);
155616457e7fSBenjamin Herrenschmidt             kernel_le = kernel_size > 0;
155716457e7fSBenjamin Herrenschmidt         }
155816457e7fSBenjamin Herrenschmidt         if (kernel_size < 0) {
15593b66da82SAlexey Kardashevskiy             fprintf(stderr, "qemu: error loading %s: %s\n",
15603b66da82SAlexey Kardashevskiy                     kernel_filename, load_elf_strerror(kernel_size));
156153018216SPaolo Bonzini             exit(1);
156253018216SPaolo Bonzini         }
156353018216SPaolo Bonzini 
156453018216SPaolo Bonzini         /* load initrd */
156553018216SPaolo Bonzini         if (initrd_filename) {
156653018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
156753018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
156853018216SPaolo Bonzini              */
156953018216SPaolo Bonzini             initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
157053018216SPaolo Bonzini             initrd_size = load_image_targphys(initrd_filename, initrd_base,
157153018216SPaolo Bonzini                                               load_limit - initrd_base);
157253018216SPaolo Bonzini             if (initrd_size < 0) {
157353018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
157453018216SPaolo Bonzini                         initrd_filename);
157553018216SPaolo Bonzini                 exit(1);
157653018216SPaolo Bonzini             }
157753018216SPaolo Bonzini         } else {
157853018216SPaolo Bonzini             initrd_base = 0;
157953018216SPaolo Bonzini             initrd_size = 0;
158053018216SPaolo Bonzini         }
158153018216SPaolo Bonzini     }
158253018216SPaolo Bonzini 
15838e7ea787SAndreas Färber     if (bios_name == NULL) {
15848e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
15858e7ea787SAndreas Färber     }
15868e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
158753018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
158853018216SPaolo Bonzini     if (fw_size < 0) {
158953018216SPaolo Bonzini         hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
159053018216SPaolo Bonzini         exit(1);
159153018216SPaolo Bonzini     }
159253018216SPaolo Bonzini     g_free(filename);
159353018216SPaolo Bonzini 
159453018216SPaolo Bonzini     spapr->entry_point = 0x100;
159553018216SPaolo Bonzini 
15964be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
15974be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
15984be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
15994be21d56SDavid Gibson 
160053018216SPaolo Bonzini     /* Prepare the device tree */
16013bbf37f2SAndreas Färber     spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size,
160216457e7fSBenjamin Herrenschmidt                                             kernel_size, kernel_le,
160353018216SPaolo Bonzini                                             boot_device, kernel_cmdline,
160453018216SPaolo Bonzini                                             spapr->epow_irq);
160553018216SPaolo Bonzini     assert(spapr->fdt_skel != NULL);
160653018216SPaolo Bonzini }
160753018216SPaolo Bonzini 
1608135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type)
1609135a129aSAneesh Kumar K.V {
1610135a129aSAneesh Kumar K.V     if (!vm_type) {
1611135a129aSAneesh Kumar K.V         return 0;
1612135a129aSAneesh Kumar K.V     }
1613135a129aSAneesh Kumar K.V 
1614135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "HV")) {
1615135a129aSAneesh Kumar K.V         return 1;
1616135a129aSAneesh Kumar K.V     }
1617135a129aSAneesh Kumar K.V 
1618135a129aSAneesh Kumar K.V     if (!strcmp(vm_type, "PR")) {
1619135a129aSAneesh Kumar K.V         return 2;
1620135a129aSAneesh Kumar K.V     }
1621135a129aSAneesh Kumar K.V 
1622135a129aSAneesh Kumar K.V     error_report("Unknown kvm-type specified '%s'", vm_type);
1623135a129aSAneesh Kumar K.V     exit(1);
1624135a129aSAneesh Kumar K.V }
1625135a129aSAneesh Kumar K.V 
162671461b0fSAlexey Kardashevskiy /*
162771461b0fSAlexey Kardashevskiy  * Implementation of an interface to adjust firmware patch
162871461b0fSAlexey Kardashevskiy  * for the bootindex property handling.
162971461b0fSAlexey Kardashevskiy  */
163071461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
163171461b0fSAlexey Kardashevskiy                                    DeviceState *dev)
163271461b0fSAlexey Kardashevskiy {
163371461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \
163471461b0fSAlexey Kardashevskiy     ((type *)object_dynamic_cast(OBJECT(obj), (name)))
163571461b0fSAlexey Kardashevskiy     SCSIDevice *d = CAST(SCSIDevice,  dev, TYPE_SCSI_DEVICE);
163671461b0fSAlexey Kardashevskiy     sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
163771461b0fSAlexey Kardashevskiy 
163871461b0fSAlexey Kardashevskiy     if (d) {
163971461b0fSAlexey Kardashevskiy         void *spapr = CAST(void, bus->parent, "spapr-vscsi");
164071461b0fSAlexey Kardashevskiy         VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
164171461b0fSAlexey Kardashevskiy         USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
164271461b0fSAlexey Kardashevskiy 
164371461b0fSAlexey Kardashevskiy         if (spapr) {
164471461b0fSAlexey Kardashevskiy             /*
164571461b0fSAlexey Kardashevskiy              * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
164671461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
164771461b0fSAlexey Kardashevskiy              * in the top 16 bits of the 64-bit LUN
164871461b0fSAlexey Kardashevskiy              */
164971461b0fSAlexey Kardashevskiy             unsigned id = 0x8000 | (d->id << 8) | d->lun;
165071461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
165171461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 48);
165271461b0fSAlexey Kardashevskiy         } else if (virtio) {
165371461b0fSAlexey Kardashevskiy             /*
165471461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (target << 8) | lun
165571461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
165671461b0fSAlexey Kardashevskiy              * Note: the quote above is from SLOF and it is wrong,
165771461b0fSAlexey Kardashevskiy              * the actual binding is:
165871461b0fSAlexey Kardashevskiy              * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
165971461b0fSAlexey Kardashevskiy              */
166071461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (d->id << 16) | d->lun;
166171461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
166271461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
166371461b0fSAlexey Kardashevskiy         } else if (usb) {
166471461b0fSAlexey Kardashevskiy             /*
166571461b0fSAlexey Kardashevskiy              * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
166671461b0fSAlexey Kardashevskiy              * in the top 32 bits of the 64-bit LUN
166771461b0fSAlexey Kardashevskiy              */
166871461b0fSAlexey Kardashevskiy             unsigned usb_port = atoi(usb->port->path);
166971461b0fSAlexey Kardashevskiy             unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
167071461b0fSAlexey Kardashevskiy             return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
167171461b0fSAlexey Kardashevskiy                                    (uint64_t)id << 32);
167271461b0fSAlexey Kardashevskiy         }
167371461b0fSAlexey Kardashevskiy     }
167471461b0fSAlexey Kardashevskiy 
167571461b0fSAlexey Kardashevskiy     if (phb) {
167671461b0fSAlexey Kardashevskiy         /* Replace "pci" with "pci@800000020000000" */
167771461b0fSAlexey Kardashevskiy         return g_strdup_printf("pci@%"PRIX64, phb->buid);
167871461b0fSAlexey Kardashevskiy     }
167971461b0fSAlexey Kardashevskiy 
168071461b0fSAlexey Kardashevskiy     return NULL;
168171461b0fSAlexey Kardashevskiy }
168271461b0fSAlexey Kardashevskiy 
168323825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp)
168423825581SEduardo Habkost {
16856ca1502eSAlexey Kardashevskiy     sPAPRMachineState *sm = SPAPR_MACHINE(obj);
168623825581SEduardo Habkost 
168723825581SEduardo Habkost     return g_strdup(sm->kvm_type);
168823825581SEduardo Habkost }
168923825581SEduardo Habkost 
169023825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
169123825581SEduardo Habkost {
16926ca1502eSAlexey Kardashevskiy     sPAPRMachineState *sm = SPAPR_MACHINE(obj);
169323825581SEduardo Habkost 
169423825581SEduardo Habkost     g_free(sm->kvm_type);
169523825581SEduardo Habkost     sm->kvm_type = g_strdup(value);
169623825581SEduardo Habkost }
169723825581SEduardo Habkost 
169823825581SEduardo Habkost static void spapr_machine_initfn(Object *obj)
169923825581SEduardo Habkost {
170023825581SEduardo Habkost     object_property_add_str(obj, "kvm-type",
170123825581SEduardo Habkost                             spapr_get_kvm_type, spapr_set_kvm_type, NULL);
170249d2e648SMarcel Apfelbaum     object_property_set_description(obj, "kvm-type",
170349d2e648SMarcel Apfelbaum                                     "Specifies the KVM virtualization mode (HV, PR)",
170449d2e648SMarcel Apfelbaum                                     NULL);
170523825581SEduardo Habkost }
170623825581SEduardo Habkost 
170734316482SAlexey Kardashevskiy static void ppc_cpu_do_nmi_on_cpu(void *arg)
170834316482SAlexey Kardashevskiy {
170934316482SAlexey Kardashevskiy     CPUState *cs = arg;
171034316482SAlexey Kardashevskiy 
171134316482SAlexey Kardashevskiy     cpu_synchronize_state(cs);
171234316482SAlexey Kardashevskiy     ppc_cpu_do_system_reset(cs);
171334316482SAlexey Kardashevskiy }
171434316482SAlexey Kardashevskiy 
171534316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
171634316482SAlexey Kardashevskiy {
171734316482SAlexey Kardashevskiy     CPUState *cs;
171834316482SAlexey Kardashevskiy 
171934316482SAlexey Kardashevskiy     CPU_FOREACH(cs) {
172034316482SAlexey Kardashevskiy         async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs);
172134316482SAlexey Kardashevskiy     }
172234316482SAlexey Kardashevskiy }
172334316482SAlexey Kardashevskiy 
172429ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data)
172553018216SPaolo Bonzini {
172629ee3247SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
172771461b0fSAlexey Kardashevskiy     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
172834316482SAlexey Kardashevskiy     NMIClass *nc = NMI_CLASS(oc);
172929ee3247SAlexey Kardashevskiy 
1730958db90cSMarcel Apfelbaum     mc->init = ppc_spapr_init;
1731958db90cSMarcel Apfelbaum     mc->reset = ppc_spapr_reset;
1732958db90cSMarcel Apfelbaum     mc->block_default_type = IF_SCSI;
1733958db90cSMarcel Apfelbaum     mc->max_cpus = MAX_CPUS;
1734958db90cSMarcel Apfelbaum     mc->no_parallel = 1;
1735958db90cSMarcel Apfelbaum     mc->default_boot_order = NULL;
1736958db90cSMarcel Apfelbaum     mc->kvm_type = spapr_kvm_type;
17379e3f9733SAlexander Graf     mc->has_dynamic_sysbus = true;
173800b4fbe2SMarcel Apfelbaum 
173971461b0fSAlexey Kardashevskiy     fwc->get_dev_path = spapr_get_fw_dev_path;
174034316482SAlexey Kardashevskiy     nc->nmi_monitor_handler = spapr_nmi;
174153018216SPaolo Bonzini }
174253018216SPaolo Bonzini 
174329ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = {
174429ee3247SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE,
174529ee3247SAlexey Kardashevskiy     .parent        = TYPE_MACHINE,
17464aee7362SDavid Gibson     .abstract      = true,
17476ca1502eSAlexey Kardashevskiy     .instance_size = sizeof(sPAPRMachineState),
174823825581SEduardo Habkost     .instance_init = spapr_machine_initfn,
174929ee3247SAlexey Kardashevskiy     .class_init    = spapr_machine_class_init,
175071461b0fSAlexey Kardashevskiy     .interfaces = (InterfaceInfo[]) {
175171461b0fSAlexey Kardashevskiy         { TYPE_FW_PATH_PROVIDER },
175234316482SAlexey Kardashevskiy         { TYPE_NMI },
175371461b0fSAlexey Kardashevskiy         { }
175471461b0fSAlexey Kardashevskiy     },
175529ee3247SAlexey Kardashevskiy };
175629ee3247SAlexey Kardashevskiy 
17576026db45SAlexey Kardashevskiy static void spapr_machine_2_1_class_init(ObjectClass *oc, void *data)
17586026db45SAlexey Kardashevskiy {
17596026db45SAlexey Kardashevskiy     MachineClass *mc = MACHINE_CLASS(oc);
176068a27b20SMichael S. Tsirkin     static GlobalProperty compat_props[] = {
176168a27b20SMichael S. Tsirkin         HW_COMPAT_2_1,
176268a27b20SMichael S. Tsirkin         { /* end of list */ }
176368a27b20SMichael S. Tsirkin     };
17646026db45SAlexey Kardashevskiy 
17656026db45SAlexey Kardashevskiy     mc->name = "pseries-2.1";
17666026db45SAlexey Kardashevskiy     mc->desc = "pSeries Logical Partition (PAPR compliant) v2.1";
176768a27b20SMichael S. Tsirkin     mc->compat_props = compat_props;
17686026db45SAlexey Kardashevskiy }
17696026db45SAlexey Kardashevskiy 
17706026db45SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_1_info = {
17716026db45SAlexey Kardashevskiy     .name          = TYPE_SPAPR_MACHINE "2.1",
17726026db45SAlexey Kardashevskiy     .parent        = TYPE_SPAPR_MACHINE,
17736026db45SAlexey Kardashevskiy     .class_init    = spapr_machine_2_1_class_init,
17746026db45SAlexey Kardashevskiy };
17756026db45SAlexey Kardashevskiy 
17764aee7362SDavid Gibson static void spapr_machine_2_2_class_init(ObjectClass *oc, void *data)
17774aee7362SDavid Gibson {
17784aee7362SDavid Gibson     MachineClass *mc = MACHINE_CLASS(oc);
17794aee7362SDavid Gibson 
17804aee7362SDavid Gibson     mc->name = "pseries-2.2";
17814aee7362SDavid Gibson     mc->desc = "pSeries Logical Partition (PAPR compliant) v2.2";
17824aee7362SDavid Gibson     mc->alias = "pseries";
17834aee7362SDavid Gibson     mc->is_default = 1;
17844aee7362SDavid Gibson }
17854aee7362SDavid Gibson 
17864aee7362SDavid Gibson static const TypeInfo spapr_machine_2_2_info = {
17874aee7362SDavid Gibson     .name          = TYPE_SPAPR_MACHINE "2.2",
17884aee7362SDavid Gibson     .parent        = TYPE_SPAPR_MACHINE,
17894aee7362SDavid Gibson     .class_init    = spapr_machine_2_2_class_init,
17904aee7362SDavid Gibson };
17914aee7362SDavid Gibson 
179229ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void)
179329ee3247SAlexey Kardashevskiy {
179429ee3247SAlexey Kardashevskiy     type_register_static(&spapr_machine_info);
17956026db45SAlexey Kardashevskiy     type_register_static(&spapr_machine_2_1_info);
17964aee7362SDavid Gibson     type_register_static(&spapr_machine_2_2_info);
179729ee3247SAlexey Kardashevskiy }
179829ee3247SAlexey Kardashevskiy 
179929ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types)
1800