xref: /openbmc/qemu/hw/ppc/spapr.c (revision 3bbf37f2)
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"
2953018216SPaolo Bonzini #include "elf.h"
3053018216SPaolo Bonzini #include "net/net.h"
3153018216SPaolo Bonzini #include "sysemu/blockdev.h"
3253018216SPaolo Bonzini #include "sysemu/cpus.h"
3353018216SPaolo Bonzini #include "sysemu/kvm.h"
3453018216SPaolo Bonzini #include "kvm_ppc.h"
354be21d56SDavid Gibson #include "mmu-hash64.h"
3653018216SPaolo Bonzini 
3753018216SPaolo Bonzini #include "hw/boards.h"
380d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
3953018216SPaolo Bonzini #include "hw/loader.h"
4053018216SPaolo Bonzini 
410d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
420d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
430d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h"
440d09e41aSPaolo Bonzini #include "hw/ppc/xics.h"
4553018216SPaolo Bonzini #include "hw/pci/msi.h"
4653018216SPaolo Bonzini 
4753018216SPaolo Bonzini #include "hw/pci/pci.h"
4853018216SPaolo Bonzini 
4953018216SPaolo Bonzini #include "exec/address-spaces.h"
5053018216SPaolo Bonzini #include "hw/usb.h"
5153018216SPaolo Bonzini #include "qemu/config-file.h"
5253018216SPaolo Bonzini 
5353018216SPaolo Bonzini #include <libfdt.h>
5453018216SPaolo Bonzini 
5553018216SPaolo Bonzini /* SLOF memory layout:
5653018216SPaolo Bonzini  *
5753018216SPaolo Bonzini  * SLOF raw image loaded at 0, copies its romfs right below the flat
5853018216SPaolo Bonzini  * device-tree, then position SLOF itself 31M below that
5953018216SPaolo Bonzini  *
6053018216SPaolo Bonzini  * So we set FW_OVERHEAD to 40MB which should account for all of that
6153018216SPaolo Bonzini  * and more
6253018216SPaolo Bonzini  *
6353018216SPaolo Bonzini  * We load our kernel at 4M, leaving space for SLOF initial image
6453018216SPaolo Bonzini  */
653bf6eeddSAlexey Kardashevskiy #define FDT_MAX_SIZE            0x40000
6653018216SPaolo Bonzini #define RTAS_MAX_SIZE           0x10000
6753018216SPaolo Bonzini #define FW_MAX_SIZE             0x400000
6853018216SPaolo Bonzini #define FW_FILE_NAME            "slof.bin"
6953018216SPaolo Bonzini #define FW_OVERHEAD             0x2800000
7053018216SPaolo Bonzini #define KERNEL_LOAD_ADDR        FW_MAX_SIZE
7153018216SPaolo Bonzini 
7253018216SPaolo Bonzini #define MIN_RMA_SLOF            128UL
7353018216SPaolo Bonzini 
7453018216SPaolo Bonzini #define TIMEBASE_FREQ           512000000ULL
7553018216SPaolo Bonzini 
7653018216SPaolo Bonzini #define MAX_CPUS                256
7753018216SPaolo Bonzini #define XICS_IRQS               1024
7853018216SPaolo Bonzini 
7953018216SPaolo Bonzini #define PHANDLE_XICP            0x00001111
8053018216SPaolo Bonzini 
8153018216SPaolo Bonzini #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
8253018216SPaolo Bonzini 
8353018216SPaolo Bonzini sPAPREnvironment *spapr;
8453018216SPaolo Bonzini 
8553018216SPaolo Bonzini int spapr_allocate_irq(int hint, bool lsi)
8653018216SPaolo Bonzini {
8753018216SPaolo Bonzini     int irq;
8853018216SPaolo Bonzini 
8953018216SPaolo Bonzini     if (hint) {
9053018216SPaolo Bonzini         irq = hint;
91f1c2dc7cSAlexey Kardashevskiy         if (hint >= spapr->next_irq) {
92f1c2dc7cSAlexey Kardashevskiy             spapr->next_irq = hint + 1;
93f1c2dc7cSAlexey Kardashevskiy         }
9453018216SPaolo Bonzini         /* FIXME: we should probably check for collisions somehow */
9553018216SPaolo Bonzini     } else {
9653018216SPaolo Bonzini         irq = spapr->next_irq++;
9753018216SPaolo Bonzini     }
9853018216SPaolo Bonzini 
9953018216SPaolo Bonzini     /* Configure irq type */
10053018216SPaolo Bonzini     if (!xics_get_qirq(spapr->icp, irq)) {
10153018216SPaolo Bonzini         return 0;
10253018216SPaolo Bonzini     }
10353018216SPaolo Bonzini 
10453018216SPaolo Bonzini     xics_set_irq_type(spapr->icp, irq, lsi);
10553018216SPaolo Bonzini 
10653018216SPaolo Bonzini     return irq;
10753018216SPaolo Bonzini }
10853018216SPaolo Bonzini 
109f1c2dc7cSAlexey Kardashevskiy /*
110f1c2dc7cSAlexey Kardashevskiy  * Allocate block of consequtive IRQs, returns a number of the first.
111f1c2dc7cSAlexey Kardashevskiy  * If msi==true, aligns the first IRQ number to num.
112f1c2dc7cSAlexey Kardashevskiy  */
113f1c2dc7cSAlexey Kardashevskiy int spapr_allocate_irq_block(int num, bool lsi, bool msi)
11453018216SPaolo Bonzini {
11553018216SPaolo Bonzini     int first = -1;
116f1c2dc7cSAlexey Kardashevskiy     int i, hint = 0;
117f1c2dc7cSAlexey Kardashevskiy 
118f1c2dc7cSAlexey Kardashevskiy     /*
119f1c2dc7cSAlexey Kardashevskiy      * MSIMesage::data is used for storing VIRQ so
120f1c2dc7cSAlexey Kardashevskiy      * it has to be aligned to num to support multiple
121f1c2dc7cSAlexey Kardashevskiy      * MSI vectors. MSI-X is not affected by this.
122f1c2dc7cSAlexey Kardashevskiy      * The hint is used for the first IRQ, the rest should
123f1c2dc7cSAlexey Kardashevskiy      * be allocated continously.
124f1c2dc7cSAlexey Kardashevskiy      */
125f1c2dc7cSAlexey Kardashevskiy     if (msi) {
126f1c2dc7cSAlexey Kardashevskiy         assert((num == 1) || (num == 2) || (num == 4) ||
127f1c2dc7cSAlexey Kardashevskiy                (num == 8) || (num == 16) || (num == 32));
128f1c2dc7cSAlexey Kardashevskiy         hint = (spapr->next_irq + num - 1) & ~(num - 1);
129f1c2dc7cSAlexey Kardashevskiy     }
13053018216SPaolo Bonzini 
13153018216SPaolo Bonzini     for (i = 0; i < num; ++i) {
13253018216SPaolo Bonzini         int irq;
13353018216SPaolo Bonzini 
134f1c2dc7cSAlexey Kardashevskiy         irq = spapr_allocate_irq(hint, lsi);
13553018216SPaolo Bonzini         if (!irq) {
13653018216SPaolo Bonzini             return -1;
13753018216SPaolo Bonzini         }
13853018216SPaolo Bonzini 
13953018216SPaolo Bonzini         if (0 == i) {
14053018216SPaolo Bonzini             first = irq;
141f1c2dc7cSAlexey Kardashevskiy             hint = 0;
14253018216SPaolo Bonzini         }
14353018216SPaolo Bonzini 
14453018216SPaolo Bonzini         /* If the above doesn't create a consecutive block then that's
14553018216SPaolo Bonzini          * an internal bug */
14653018216SPaolo Bonzini         assert(irq == (first + i));
14753018216SPaolo Bonzini     }
14853018216SPaolo Bonzini 
14953018216SPaolo Bonzini     return first;
15053018216SPaolo Bonzini }
15153018216SPaolo Bonzini 
152c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers,
153c04d6cfaSAnthony Liguori                                   int nr_irqs)
154c04d6cfaSAnthony Liguori {
155c04d6cfaSAnthony Liguori     DeviceState *dev;
156c04d6cfaSAnthony Liguori 
157c04d6cfaSAnthony Liguori     dev = qdev_create(NULL, type);
158c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
159c04d6cfaSAnthony Liguori     qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
160c04d6cfaSAnthony Liguori     if (qdev_init(dev) < 0) {
161c04d6cfaSAnthony Liguori         return NULL;
162c04d6cfaSAnthony Liguori     }
163c04d6cfaSAnthony Liguori 
1645a3d7b23SAlexey Kardashevskiy     return XICS_COMMON(dev);
165c04d6cfaSAnthony Liguori }
166c04d6cfaSAnthony Liguori 
167c04d6cfaSAnthony Liguori static XICSState *xics_system_init(int nr_servers, int nr_irqs)
168c04d6cfaSAnthony Liguori {
169c04d6cfaSAnthony Liguori     XICSState *icp = NULL;
170c04d6cfaSAnthony Liguori 
17111ad93f6SDavid Gibson     if (kvm_enabled()) {
17211ad93f6SDavid Gibson         QemuOpts *machine_opts = qemu_get_machine_opts();
17311ad93f6SDavid Gibson         bool irqchip_allowed = qemu_opt_get_bool(machine_opts,
17411ad93f6SDavid Gibson                                                 "kernel_irqchip", true);
17511ad93f6SDavid Gibson         bool irqchip_required = qemu_opt_get_bool(machine_opts,
17611ad93f6SDavid Gibson                                                   "kernel_irqchip", false);
17711ad93f6SDavid Gibson         if (irqchip_allowed) {
17811ad93f6SDavid Gibson             icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs);
17911ad93f6SDavid Gibson         }
18011ad93f6SDavid Gibson 
18111ad93f6SDavid Gibson         if (irqchip_required && !icp) {
18211ad93f6SDavid Gibson             perror("Failed to create in-kernel XICS\n");
18311ad93f6SDavid Gibson             abort();
18411ad93f6SDavid Gibson         }
18511ad93f6SDavid Gibson     }
18611ad93f6SDavid Gibson 
18711ad93f6SDavid Gibson     if (!icp) {
188c04d6cfaSAnthony Liguori         icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs);
18911ad93f6SDavid Gibson     }
19011ad93f6SDavid Gibson 
191c04d6cfaSAnthony Liguori     if (!icp) {
192c04d6cfaSAnthony Liguori         perror("Failed to create XICS\n");
193c04d6cfaSAnthony Liguori         abort();
194c04d6cfaSAnthony Liguori     }
195c04d6cfaSAnthony Liguori 
196c04d6cfaSAnthony Liguori     return icp;
197c04d6cfaSAnthony Liguori }
198c04d6cfaSAnthony Liguori 
19953018216SPaolo Bonzini static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr)
20053018216SPaolo Bonzini {
20153018216SPaolo Bonzini     int ret = 0, offset;
20253018216SPaolo Bonzini     CPUState *cpu;
20353018216SPaolo Bonzini     char cpu_model[32];
20453018216SPaolo Bonzini     int smt = kvmppc_smt_threads();
20553018216SPaolo Bonzini     uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
20653018216SPaolo Bonzini 
207bdc44640SAndreas Färber     CPU_FOREACH(cpu) {
208*3bbf37f2SAndreas Färber         DeviceClass *dc = DEVICE_GET_CLASS(cpu);
20953018216SPaolo Bonzini         uint32_t associativity[] = {cpu_to_be32(0x5),
21053018216SPaolo Bonzini                                     cpu_to_be32(0x0),
21153018216SPaolo Bonzini                                     cpu_to_be32(0x0),
21253018216SPaolo Bonzini                                     cpu_to_be32(0x0),
21353018216SPaolo Bonzini                                     cpu_to_be32(cpu->numa_node),
21453018216SPaolo Bonzini                                     cpu_to_be32(cpu->cpu_index)};
21553018216SPaolo Bonzini 
21653018216SPaolo Bonzini         if ((cpu->cpu_index % smt) != 0) {
21753018216SPaolo Bonzini             continue;
21853018216SPaolo Bonzini         }
21953018216SPaolo Bonzini 
220*3bbf37f2SAndreas Färber         snprintf(cpu_model, 32, "/cpus/%s@%x", dc->fw_name,
22153018216SPaolo Bonzini                  cpu->cpu_index);
22253018216SPaolo Bonzini 
22353018216SPaolo Bonzini         offset = fdt_path_offset(fdt, cpu_model);
22453018216SPaolo Bonzini         if (offset < 0) {
22553018216SPaolo Bonzini             return offset;
22653018216SPaolo Bonzini         }
22753018216SPaolo Bonzini 
22853018216SPaolo Bonzini         if (nb_numa_nodes > 1) {
22953018216SPaolo Bonzini             ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
23053018216SPaolo Bonzini                               sizeof(associativity));
23153018216SPaolo Bonzini             if (ret < 0) {
23253018216SPaolo Bonzini                 return ret;
23353018216SPaolo Bonzini             }
23453018216SPaolo Bonzini         }
23553018216SPaolo Bonzini 
23653018216SPaolo Bonzini         ret = fdt_setprop(fdt, offset, "ibm,pft-size",
23753018216SPaolo Bonzini                           pft_size_prop, sizeof(pft_size_prop));
23853018216SPaolo Bonzini         if (ret < 0) {
23953018216SPaolo Bonzini             return ret;
24053018216SPaolo Bonzini         }
24153018216SPaolo Bonzini     }
24253018216SPaolo Bonzini     return ret;
24353018216SPaolo Bonzini }
24453018216SPaolo Bonzini 
24553018216SPaolo Bonzini 
24653018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
24753018216SPaolo Bonzini                                      size_t maxsize)
24853018216SPaolo Bonzini {
24953018216SPaolo Bonzini     size_t maxcells = maxsize / sizeof(uint32_t);
25053018216SPaolo Bonzini     int i, j, count;
25153018216SPaolo Bonzini     uint32_t *p = prop;
25253018216SPaolo Bonzini 
25353018216SPaolo Bonzini     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
25453018216SPaolo Bonzini         struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
25553018216SPaolo Bonzini 
25653018216SPaolo Bonzini         if (!sps->page_shift) {
25753018216SPaolo Bonzini             break;
25853018216SPaolo Bonzini         }
25953018216SPaolo Bonzini         for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
26053018216SPaolo Bonzini             if (sps->enc[count].page_shift == 0) {
26153018216SPaolo Bonzini                 break;
26253018216SPaolo Bonzini             }
26353018216SPaolo Bonzini         }
26453018216SPaolo Bonzini         if ((p - prop) >= (maxcells - 3 - count * 2)) {
26553018216SPaolo Bonzini             break;
26653018216SPaolo Bonzini         }
26753018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->page_shift);
26853018216SPaolo Bonzini         *(p++) = cpu_to_be32(sps->slb_enc);
26953018216SPaolo Bonzini         *(p++) = cpu_to_be32(count);
27053018216SPaolo Bonzini         for (j = 0; j < count; j++) {
27153018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].page_shift);
27253018216SPaolo Bonzini             *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
27353018216SPaolo Bonzini         }
27453018216SPaolo Bonzini     }
27553018216SPaolo Bonzini 
27653018216SPaolo Bonzini     return (p - prop) * sizeof(uint32_t);
27753018216SPaolo Bonzini }
27853018216SPaolo Bonzini 
27953018216SPaolo Bonzini #define _FDT(exp) \
28053018216SPaolo Bonzini     do { \
28153018216SPaolo Bonzini         int ret = (exp);                                           \
28253018216SPaolo Bonzini         if (ret < 0) {                                             \
28353018216SPaolo Bonzini             fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
28453018216SPaolo Bonzini                     #exp, fdt_strerror(ret));                      \
28553018216SPaolo Bonzini             exit(1);                                               \
28653018216SPaolo Bonzini         }                                                          \
28753018216SPaolo Bonzini     } while (0)
28853018216SPaolo Bonzini 
28953018216SPaolo Bonzini 
290*3bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base,
29153018216SPaolo Bonzini                                    hwaddr initrd_size,
29253018216SPaolo Bonzini                                    hwaddr kernel_size,
29316457e7fSBenjamin Herrenschmidt                                    bool little_endian,
29453018216SPaolo Bonzini                                    const char *boot_device,
29553018216SPaolo Bonzini                                    const char *kernel_cmdline,
29653018216SPaolo Bonzini                                    uint32_t epow_irq)
29753018216SPaolo Bonzini {
29853018216SPaolo Bonzini     void *fdt;
299182735efSAndreas Färber     CPUState *cs;
30053018216SPaolo Bonzini     uint32_t start_prop = cpu_to_be32(initrd_base);
30153018216SPaolo Bonzini     uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
30253018216SPaolo Bonzini     char hypertas_prop[] = "hcall-pft\0hcall-term\0hcall-dabr\0hcall-interrupt"
30342561bf2SAnton Blanchard         "\0hcall-tce\0hcall-vio\0hcall-splpar\0hcall-bulk\0hcall-set-mode";
30453018216SPaolo Bonzini     char qemu_hypertas_prop[] = "hcall-memop1";
30553018216SPaolo Bonzini     uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
30653018216SPaolo Bonzini     uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
30753018216SPaolo Bonzini     int i, smt = kvmppc_smt_threads();
30853018216SPaolo Bonzini     unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
30953018216SPaolo Bonzini 
31053018216SPaolo Bonzini     fdt = g_malloc0(FDT_MAX_SIZE);
31153018216SPaolo Bonzini     _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
31253018216SPaolo Bonzini 
31353018216SPaolo Bonzini     if (kernel_size) {
31453018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
31553018216SPaolo Bonzini     }
31653018216SPaolo Bonzini     if (initrd_size) {
31753018216SPaolo Bonzini         _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
31853018216SPaolo Bonzini     }
31953018216SPaolo Bonzini     _FDT((fdt_finish_reservemap(fdt)));
32053018216SPaolo Bonzini 
32153018216SPaolo Bonzini     /* Root node */
32253018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "")));
32353018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "chrp")));
32453018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
325fa388916SAnthony Liguori     _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
32653018216SPaolo Bonzini 
32753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
32853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
32953018216SPaolo Bonzini 
33053018216SPaolo Bonzini     /* /chosen */
33153018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "chosen")));
33253018216SPaolo Bonzini 
33353018216SPaolo Bonzini     /* Set Form1_affinity */
33453018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
33553018216SPaolo Bonzini 
33653018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
33753018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-start",
33853018216SPaolo Bonzini                        &start_prop, sizeof(start_prop))));
33953018216SPaolo Bonzini     _FDT((fdt_property(fdt, "linux,initrd-end",
34053018216SPaolo Bonzini                        &end_prop, sizeof(end_prop))));
34153018216SPaolo Bonzini     if (kernel_size) {
34253018216SPaolo Bonzini         uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
34353018216SPaolo Bonzini                               cpu_to_be64(kernel_size) };
34453018216SPaolo Bonzini 
34553018216SPaolo Bonzini         _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
34616457e7fSBenjamin Herrenschmidt         if (little_endian) {
34716457e7fSBenjamin Herrenschmidt             _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0)));
34816457e7fSBenjamin Herrenschmidt         }
34953018216SPaolo Bonzini     }
35053018216SPaolo Bonzini     if (boot_device) {
35153018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));
35253018216SPaolo Bonzini     }
35353018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
35453018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
35553018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
35653018216SPaolo Bonzini 
35753018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
35853018216SPaolo Bonzini 
35953018216SPaolo Bonzini     /* cpus */
36053018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "cpus")));
36153018216SPaolo Bonzini 
36253018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
36353018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
36453018216SPaolo Bonzini 
365bdc44640SAndreas Färber     CPU_FOREACH(cs) {
366182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
367182735efSAndreas Färber         CPUPPCState *env = &cpu->env;
368*3bbf37f2SAndreas Färber         DeviceClass *dc = DEVICE_GET_CLASS(cs);
369182735efSAndreas Färber         PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
370182735efSAndreas Färber         int index = cs->cpu_index;
37153018216SPaolo Bonzini         uint32_t servers_prop[smp_threads];
37253018216SPaolo Bonzini         uint32_t gservers_prop[smp_threads * 2];
37353018216SPaolo Bonzini         char *nodename;
37453018216SPaolo Bonzini         uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
37553018216SPaolo Bonzini                            0xffffffff, 0xffffffff};
37653018216SPaolo Bonzini         uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
37753018216SPaolo Bonzini         uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
37853018216SPaolo Bonzini         uint32_t page_sizes_prop[64];
37953018216SPaolo Bonzini         size_t page_sizes_prop_size;
38053018216SPaolo Bonzini 
38153018216SPaolo Bonzini         if ((index % smt) != 0) {
38253018216SPaolo Bonzini             continue;
38353018216SPaolo Bonzini         }
38453018216SPaolo Bonzini 
385*3bbf37f2SAndreas Färber         nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
38653018216SPaolo Bonzini 
38753018216SPaolo Bonzini         _FDT((fdt_begin_node(fdt, nodename)));
38853018216SPaolo Bonzini 
38953018216SPaolo Bonzini         g_free(nodename);
39053018216SPaolo Bonzini 
39153018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "reg", index)));
39253018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "device_type", "cpu")));
39353018216SPaolo Bonzini 
39453018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
3950cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "d-cache-block-size",
39653018216SPaolo Bonzini                                 env->dcache_line_size)));
3970cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "d-cache-line-size",
3980cbad81fSDavid Gibson                                 env->dcache_line_size)));
3990cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "i-cache-block-size",
40053018216SPaolo Bonzini                                 env->icache_line_size)));
4010cbad81fSDavid Gibson         _FDT((fdt_property_cell(fdt, "i-cache-line-size",
4020cbad81fSDavid Gibson                                 env->icache_line_size)));
4030cbad81fSDavid Gibson 
4040cbad81fSDavid Gibson         if (pcc->l1_dcache_size) {
4050cbad81fSDavid Gibson             _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size)));
4060cbad81fSDavid Gibson         } else {
4070cbad81fSDavid Gibson             fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");
4080cbad81fSDavid Gibson         }
4090cbad81fSDavid Gibson         if (pcc->l1_icache_size) {
4100cbad81fSDavid Gibson             _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size)));
4110cbad81fSDavid Gibson         } else {
4120cbad81fSDavid Gibson             fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n");
4130cbad81fSDavid Gibson         }
4140cbad81fSDavid Gibson 
41553018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
41653018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
41753018216SPaolo Bonzini         _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
41853018216SPaolo Bonzini         _FDT((fdt_property_string(fdt, "status", "okay")));
41953018216SPaolo Bonzini         _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
42053018216SPaolo Bonzini 
42153018216SPaolo Bonzini         /* Build interrupt servers and gservers properties */
42253018216SPaolo Bonzini         for (i = 0; i < smp_threads; i++) {
42353018216SPaolo Bonzini             servers_prop[i] = cpu_to_be32(index + i);
42453018216SPaolo Bonzini             /* Hack, direct the group queues back to cpu 0 */
42553018216SPaolo Bonzini             gservers_prop[i*2] = cpu_to_be32(index + i);
42653018216SPaolo Bonzini             gservers_prop[i*2 + 1] = 0;
42753018216SPaolo Bonzini         }
42853018216SPaolo Bonzini         _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s",
42953018216SPaolo Bonzini                            servers_prop, sizeof(servers_prop))));
43053018216SPaolo Bonzini         _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",
43153018216SPaolo Bonzini                            gservers_prop, sizeof(gservers_prop))));
43253018216SPaolo Bonzini 
433dcb861cbSAlexey Kardashevskiy         if (env->spr_cb[SPR_PURR].oea_read) {
434dcb861cbSAlexey Kardashevskiy             _FDT((fdt_property(fdt, "ibm,purr", NULL, 0)));
435dcb861cbSAlexey Kardashevskiy         }
436dcb861cbSAlexey Kardashevskiy 
43753018216SPaolo Bonzini         if (env->mmu_model & POWERPC_MMU_1TSEG) {
43853018216SPaolo Bonzini             _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
43953018216SPaolo Bonzini                                segs, sizeof(segs))));
44053018216SPaolo Bonzini         }
44153018216SPaolo Bonzini 
44253018216SPaolo Bonzini         /* Advertise VMX/VSX (vector extensions) if available
44353018216SPaolo Bonzini          *   0 / no property == no vector extensions
44453018216SPaolo Bonzini          *   1               == VMX / Altivec available
44553018216SPaolo Bonzini          *   2               == VSX available */
44653018216SPaolo Bonzini         if (env->insns_flags & PPC_ALTIVEC) {
44753018216SPaolo Bonzini             uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
44853018216SPaolo Bonzini 
44953018216SPaolo Bonzini             _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
45053018216SPaolo Bonzini         }
45153018216SPaolo Bonzini 
45253018216SPaolo Bonzini         /* Advertise DFP (Decimal Floating Point) if available
45353018216SPaolo Bonzini          *   0 / no property == no DFP
45453018216SPaolo Bonzini          *   1               == DFP available */
45553018216SPaolo Bonzini         if (env->insns_flags2 & PPC2_DFP) {
45653018216SPaolo Bonzini             _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
45753018216SPaolo Bonzini         }
45853018216SPaolo Bonzini 
45953018216SPaolo Bonzini         page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
46053018216SPaolo Bonzini                                                       sizeof(page_sizes_prop));
46153018216SPaolo Bonzini         if (page_sizes_prop_size) {
46253018216SPaolo Bonzini             _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
46353018216SPaolo Bonzini                                page_sizes_prop, page_sizes_prop_size)));
46453018216SPaolo Bonzini         }
46553018216SPaolo Bonzini 
46653018216SPaolo Bonzini         _FDT((fdt_end_node(fdt)));
46753018216SPaolo Bonzini     }
46853018216SPaolo Bonzini 
46953018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
47053018216SPaolo Bonzini 
47153018216SPaolo Bonzini     /* RTAS */
47253018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "rtas")));
47353018216SPaolo Bonzini 
47453018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop,
47553018216SPaolo Bonzini                        sizeof(hypertas_prop))));
47653018216SPaolo Bonzini     _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas_prop,
47753018216SPaolo Bonzini                        sizeof(qemu_hypertas_prop))));
47853018216SPaolo Bonzini 
47953018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
48053018216SPaolo Bonzini         refpoints, sizeof(refpoints))));
48153018216SPaolo Bonzini 
48253018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
48353018216SPaolo Bonzini 
48453018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
48553018216SPaolo Bonzini 
48653018216SPaolo Bonzini     /* interrupt controller */
48753018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "interrupt-controller")));
48853018216SPaolo Bonzini 
48953018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type",
49053018216SPaolo Bonzini                               "PowerPC-External-Interrupt-Presentation")));
49153018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
49253018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
49353018216SPaolo Bonzini     _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
49453018216SPaolo Bonzini                        interrupt_server_ranges_prop,
49553018216SPaolo Bonzini                        sizeof(interrupt_server_ranges_prop))));
49653018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
49753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
49853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
49953018216SPaolo Bonzini 
50053018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
50153018216SPaolo Bonzini 
50253018216SPaolo Bonzini     /* vdevice */
50353018216SPaolo Bonzini     _FDT((fdt_begin_node(fdt, "vdevice")));
50453018216SPaolo Bonzini 
50553018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
50653018216SPaolo Bonzini     _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
50753018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
50853018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
50953018216SPaolo Bonzini     _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
51053018216SPaolo Bonzini     _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
51153018216SPaolo Bonzini 
51253018216SPaolo Bonzini     _FDT((fdt_end_node(fdt)));
51353018216SPaolo Bonzini 
51453018216SPaolo Bonzini     /* event-sources */
51553018216SPaolo Bonzini     spapr_events_fdt_skel(fdt, epow_irq);
51653018216SPaolo Bonzini 
51753018216SPaolo Bonzini     _FDT((fdt_end_node(fdt))); /* close root node */
51853018216SPaolo Bonzini     _FDT((fdt_finish(fdt)));
51953018216SPaolo Bonzini 
52053018216SPaolo Bonzini     return fdt;
52153018216SPaolo Bonzini }
52253018216SPaolo Bonzini 
52353018216SPaolo Bonzini static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt)
52453018216SPaolo Bonzini {
52553018216SPaolo Bonzini     uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0),
52653018216SPaolo Bonzini                                 cpu_to_be32(0x0), cpu_to_be32(0x0),
52753018216SPaolo Bonzini                                 cpu_to_be32(0x0)};
52853018216SPaolo Bonzini     char mem_name[32];
52953018216SPaolo Bonzini     hwaddr node0_size, mem_start;
53053018216SPaolo Bonzini     uint64_t mem_reg_property[2];
53153018216SPaolo Bonzini     int i, off;
53253018216SPaolo Bonzini 
53353018216SPaolo Bonzini     /* memory node(s) */
53453018216SPaolo Bonzini     node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size;
53553018216SPaolo Bonzini     if (spapr->rma_size > node0_size) {
53653018216SPaolo Bonzini         spapr->rma_size = node0_size;
53753018216SPaolo Bonzini     }
53853018216SPaolo Bonzini 
53953018216SPaolo Bonzini     /* RMA */
54053018216SPaolo Bonzini     mem_reg_property[0] = 0;
54153018216SPaolo Bonzini     mem_reg_property[1] = cpu_to_be64(spapr->rma_size);
54253018216SPaolo Bonzini     off = fdt_add_subnode(fdt, 0, "memory@0");
54353018216SPaolo Bonzini     _FDT(off);
54453018216SPaolo Bonzini     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
54553018216SPaolo Bonzini     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
54653018216SPaolo Bonzini                       sizeof(mem_reg_property))));
54753018216SPaolo Bonzini     _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
54853018216SPaolo Bonzini                       sizeof(associativity))));
54953018216SPaolo Bonzini 
55053018216SPaolo Bonzini     /* RAM: Node 0 */
55153018216SPaolo Bonzini     if (node0_size > spapr->rma_size) {
55253018216SPaolo Bonzini         mem_reg_property[0] = cpu_to_be64(spapr->rma_size);
55353018216SPaolo Bonzini         mem_reg_property[1] = cpu_to_be64(node0_size - spapr->rma_size);
55453018216SPaolo Bonzini 
55553018216SPaolo Bonzini         sprintf(mem_name, "memory@" TARGET_FMT_lx, spapr->rma_size);
55653018216SPaolo Bonzini         off = fdt_add_subnode(fdt, 0, mem_name);
55753018216SPaolo Bonzini         _FDT(off);
55853018216SPaolo Bonzini         _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
55953018216SPaolo Bonzini         _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
56053018216SPaolo Bonzini                           sizeof(mem_reg_property))));
56153018216SPaolo Bonzini         _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
56253018216SPaolo Bonzini                           sizeof(associativity))));
56353018216SPaolo Bonzini     }
56453018216SPaolo Bonzini 
56553018216SPaolo Bonzini     /* RAM: Node 1 and beyond */
56653018216SPaolo Bonzini     mem_start = node0_size;
56753018216SPaolo Bonzini     for (i = 1; i < nb_numa_nodes; i++) {
56853018216SPaolo Bonzini         mem_reg_property[0] = cpu_to_be64(mem_start);
56953018216SPaolo Bonzini         mem_reg_property[1] = cpu_to_be64(node_mem[i]);
57053018216SPaolo Bonzini         associativity[3] = associativity[4] = cpu_to_be32(i);
57153018216SPaolo Bonzini         sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start);
57253018216SPaolo Bonzini         off = fdt_add_subnode(fdt, 0, mem_name);
57353018216SPaolo Bonzini         _FDT(off);
57453018216SPaolo Bonzini         _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
57553018216SPaolo Bonzini         _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
57653018216SPaolo Bonzini                           sizeof(mem_reg_property))));
57753018216SPaolo Bonzini         _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
57853018216SPaolo Bonzini                           sizeof(associativity))));
57953018216SPaolo Bonzini         mem_start += node_mem[i];
58053018216SPaolo Bonzini     }
58153018216SPaolo Bonzini 
58253018216SPaolo Bonzini     return 0;
58353018216SPaolo Bonzini }
58453018216SPaolo Bonzini 
58553018216SPaolo Bonzini static void spapr_finalize_fdt(sPAPREnvironment *spapr,
58653018216SPaolo Bonzini                                hwaddr fdt_addr,
58753018216SPaolo Bonzini                                hwaddr rtas_addr,
58853018216SPaolo Bonzini                                hwaddr rtas_size)
58953018216SPaolo Bonzini {
59053018216SPaolo Bonzini     int ret;
59153018216SPaolo Bonzini     void *fdt;
59253018216SPaolo Bonzini     sPAPRPHBState *phb;
59353018216SPaolo Bonzini 
59453018216SPaolo Bonzini     fdt = g_malloc(FDT_MAX_SIZE);
59553018216SPaolo Bonzini 
59653018216SPaolo Bonzini     /* open out the base tree into a temp buffer for the final tweaks */
59753018216SPaolo Bonzini     _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
59853018216SPaolo Bonzini 
59953018216SPaolo Bonzini     ret = spapr_populate_memory(spapr, fdt);
60053018216SPaolo Bonzini     if (ret < 0) {
60153018216SPaolo Bonzini         fprintf(stderr, "couldn't setup memory nodes in fdt\n");
60253018216SPaolo Bonzini         exit(1);
60353018216SPaolo Bonzini     }
60453018216SPaolo Bonzini 
60553018216SPaolo Bonzini     ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
60653018216SPaolo Bonzini     if (ret < 0) {
60753018216SPaolo Bonzini         fprintf(stderr, "couldn't setup vio devices in fdt\n");
60853018216SPaolo Bonzini         exit(1);
60953018216SPaolo Bonzini     }
61053018216SPaolo Bonzini 
61153018216SPaolo Bonzini     QLIST_FOREACH(phb, &spapr->phbs, list) {
61253018216SPaolo Bonzini         ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
61353018216SPaolo Bonzini     }
61453018216SPaolo Bonzini 
61553018216SPaolo Bonzini     if (ret < 0) {
61653018216SPaolo Bonzini         fprintf(stderr, "couldn't setup PCI devices in fdt\n");
61753018216SPaolo Bonzini         exit(1);
61853018216SPaolo Bonzini     }
61953018216SPaolo Bonzini 
62053018216SPaolo Bonzini     /* RTAS */
62153018216SPaolo Bonzini     ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
62253018216SPaolo Bonzini     if (ret < 0) {
62353018216SPaolo Bonzini         fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
62453018216SPaolo Bonzini     }
62553018216SPaolo Bonzini 
62653018216SPaolo Bonzini     /* Advertise NUMA via ibm,associativity */
62753018216SPaolo Bonzini     ret = spapr_fixup_cpu_dt(fdt, spapr);
62853018216SPaolo Bonzini     if (ret < 0) {
62953018216SPaolo Bonzini         fprintf(stderr, "Couldn't finalize CPU device tree properties\n");
63053018216SPaolo Bonzini     }
63153018216SPaolo Bonzini 
63253018216SPaolo Bonzini     if (!spapr->has_graphics) {
63353018216SPaolo Bonzini         spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
63453018216SPaolo Bonzini     }
63553018216SPaolo Bonzini 
63653018216SPaolo Bonzini     _FDT((fdt_pack(fdt)));
63753018216SPaolo Bonzini 
63853018216SPaolo Bonzini     if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
63953018216SPaolo Bonzini         hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n",
64053018216SPaolo Bonzini                  fdt_totalsize(fdt), FDT_MAX_SIZE);
64153018216SPaolo Bonzini         exit(1);
64253018216SPaolo Bonzini     }
64353018216SPaolo Bonzini 
64453018216SPaolo Bonzini     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
64553018216SPaolo Bonzini 
64653018216SPaolo Bonzini     g_free(fdt);
64753018216SPaolo Bonzini }
64853018216SPaolo Bonzini 
64953018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
65053018216SPaolo Bonzini {
65153018216SPaolo Bonzini     return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
65253018216SPaolo Bonzini }
65353018216SPaolo Bonzini 
65453018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu)
65553018216SPaolo Bonzini {
65653018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
65753018216SPaolo Bonzini 
65853018216SPaolo Bonzini     if (msr_pr) {
65953018216SPaolo Bonzini         hcall_dprintf("Hypercall made with MSR[PR]=1\n");
66053018216SPaolo Bonzini         env->gpr[3] = H_PRIVILEGE;
66153018216SPaolo Bonzini     } else {
66253018216SPaolo Bonzini         env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
66353018216SPaolo Bonzini     }
66453018216SPaolo Bonzini }
66553018216SPaolo Bonzini 
66653018216SPaolo Bonzini static void spapr_reset_htab(sPAPREnvironment *spapr)
66753018216SPaolo Bonzini {
66853018216SPaolo Bonzini     long shift;
66953018216SPaolo Bonzini 
67053018216SPaolo Bonzini     /* allocate hash page table.  For now we always make this 16mb,
67153018216SPaolo Bonzini      * later we should probably make it scale to the size of guest
67253018216SPaolo Bonzini      * RAM */
67353018216SPaolo Bonzini 
67453018216SPaolo Bonzini     shift = kvmppc_reset_htab(spapr->htab_shift);
67553018216SPaolo Bonzini 
67653018216SPaolo Bonzini     if (shift > 0) {
67753018216SPaolo Bonzini         /* Kernel handles htab, we don't need to allocate one */
67853018216SPaolo Bonzini         spapr->htab_shift = shift;
67953018216SPaolo Bonzini     } else {
68053018216SPaolo Bonzini         if (!spapr->htab) {
68153018216SPaolo Bonzini             /* Allocate an htab if we don't yet have one */
68253018216SPaolo Bonzini             spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr));
68353018216SPaolo Bonzini         }
68453018216SPaolo Bonzini 
68553018216SPaolo Bonzini         /* And clear it */
68653018216SPaolo Bonzini         memset(spapr->htab, 0, HTAB_SIZE(spapr));
68753018216SPaolo Bonzini     }
68853018216SPaolo Bonzini 
68953018216SPaolo Bonzini     /* Update the RMA size if necessary */
69053018216SPaolo Bonzini     if (spapr->vrma_adjust) {
69153018216SPaolo Bonzini         spapr->rma_size = kvmppc_rma_size(ram_size, spapr->htab_shift);
69253018216SPaolo Bonzini     }
69353018216SPaolo Bonzini }
69453018216SPaolo Bonzini 
69553018216SPaolo Bonzini static void ppc_spapr_reset(void)
69653018216SPaolo Bonzini {
697182735efSAndreas Färber     PowerPCCPU *first_ppc_cpu;
698259186a7SAndreas Färber 
69953018216SPaolo Bonzini     /* Reset the hash table & recalc the RMA */
70053018216SPaolo Bonzini     spapr_reset_htab(spapr);
70153018216SPaolo Bonzini 
70253018216SPaolo Bonzini     qemu_devices_reset();
70353018216SPaolo Bonzini 
70453018216SPaolo Bonzini     /* Load the fdt */
70553018216SPaolo Bonzini     spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
70653018216SPaolo Bonzini                        spapr->rtas_size);
70753018216SPaolo Bonzini 
70853018216SPaolo Bonzini     /* Set up the entry state */
709182735efSAndreas Färber     first_ppc_cpu = POWERPC_CPU(first_cpu);
710182735efSAndreas Färber     first_ppc_cpu->env.gpr[3] = spapr->fdt_addr;
711182735efSAndreas Färber     first_ppc_cpu->env.gpr[5] = 0;
712182735efSAndreas Färber     first_cpu->halted = 0;
713182735efSAndreas Färber     first_ppc_cpu->env.nip = spapr->entry_point;
71453018216SPaolo Bonzini 
71553018216SPaolo Bonzini }
71653018216SPaolo Bonzini 
71753018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque)
71853018216SPaolo Bonzini {
71953018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
720259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
72153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
72253018216SPaolo Bonzini 
723259186a7SAndreas Färber     cpu_reset(cs);
72453018216SPaolo Bonzini 
72553018216SPaolo Bonzini     /* All CPUs start halted.  CPU0 is unhalted from the machine level
72653018216SPaolo Bonzini      * reset code and the rest are explicitly started up by the guest
72753018216SPaolo Bonzini      * using an RTAS call */
728259186a7SAndreas Färber     cs->halted = 1;
72953018216SPaolo Bonzini 
73053018216SPaolo Bonzini     env->spr[SPR_HIOR] = 0;
73153018216SPaolo Bonzini 
7324be21d56SDavid Gibson     env->external_htab = (uint8_t *)spapr->htab;
73353018216SPaolo Bonzini     env->htab_base = -1;
73453018216SPaolo Bonzini     env->htab_mask = HTAB_SIZE(spapr) - 1;
735ec4936e1SStefan Weil     env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
73653018216SPaolo Bonzini         (spapr->htab_shift - 18);
73753018216SPaolo Bonzini }
73853018216SPaolo Bonzini 
73953018216SPaolo Bonzini static void spapr_create_nvram(sPAPREnvironment *spapr)
74053018216SPaolo Bonzini {
7412ff3de68SMarkus Armbruster     DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
7422ff3de68SMarkus Armbruster     const char *drivename = qemu_opt_get(qemu_get_machine_opts(), "nvram");
74353018216SPaolo Bonzini 
74453018216SPaolo Bonzini     if (drivename) {
74553018216SPaolo Bonzini         BlockDriverState *bs;
74653018216SPaolo Bonzini 
74753018216SPaolo Bonzini         bs = bdrv_find(drivename);
74853018216SPaolo Bonzini         if (!bs) {
74953018216SPaolo Bonzini             fprintf(stderr, "No such block device \"%s\" for nvram\n",
75053018216SPaolo Bonzini                     drivename);
75153018216SPaolo Bonzini             exit(1);
75253018216SPaolo Bonzini         }
75353018216SPaolo Bonzini         qdev_prop_set_drive_nofail(dev, "drive", bs);
75453018216SPaolo Bonzini     }
75553018216SPaolo Bonzini 
75653018216SPaolo Bonzini     qdev_init_nofail(dev);
75753018216SPaolo Bonzini 
75853018216SPaolo Bonzini     spapr->nvram = (struct sPAPRNVRAM *)dev;
75953018216SPaolo Bonzini }
76053018216SPaolo Bonzini 
76153018216SPaolo Bonzini /* Returns whether we want to use VGA or not */
76253018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus)
76353018216SPaolo Bonzini {
76453018216SPaolo Bonzini     switch (vga_interface_type) {
76553018216SPaolo Bonzini     case VGA_NONE:
76653018216SPaolo Bonzini     case VGA_STD:
76753018216SPaolo Bonzini         return pci_vga_init(pci_bus) != NULL;
76853018216SPaolo Bonzini     default:
76953018216SPaolo Bonzini         fprintf(stderr, "This vga model is not supported,"
77053018216SPaolo Bonzini                 "currently it only supports -vga std\n");
77153018216SPaolo Bonzini         exit(0);
77253018216SPaolo Bonzini         break;
77353018216SPaolo Bonzini     }
77453018216SPaolo Bonzini }
77553018216SPaolo Bonzini 
7764be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = {
7774be21d56SDavid Gibson     .name = "spapr",
7784be21d56SDavid Gibson     .version_id = 1,
7794be21d56SDavid Gibson     .minimum_version_id = 1,
7804be21d56SDavid Gibson     .minimum_version_id_old = 1,
7814be21d56SDavid Gibson     .fields      = (VMStateField []) {
7824be21d56SDavid Gibson         VMSTATE_UINT32(next_irq, sPAPREnvironment),
7834be21d56SDavid Gibson 
7844be21d56SDavid Gibson         /* RTC offset */
7854be21d56SDavid Gibson         VMSTATE_UINT64(rtc_offset, sPAPREnvironment),
7864be21d56SDavid Gibson 
7874be21d56SDavid Gibson         VMSTATE_END_OF_LIST()
7884be21d56SDavid Gibson     },
7894be21d56SDavid Gibson };
7904be21d56SDavid Gibson 
7914be21d56SDavid Gibson #define HPTE(_table, _i)   (void *)(((uint64_t *)(_table)) + ((_i) * 2))
7924be21d56SDavid Gibson #define HPTE_VALID(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
7934be21d56SDavid Gibson #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
7944be21d56SDavid Gibson #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
7954be21d56SDavid Gibson 
7964be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque)
7974be21d56SDavid Gibson {
7984be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
7994be21d56SDavid Gibson 
8004be21d56SDavid Gibson     /* "Iteration" header */
8014be21d56SDavid Gibson     qemu_put_be32(f, spapr->htab_shift);
8024be21d56SDavid Gibson 
803e68cb8b4SAlexey Kardashevskiy     if (spapr->htab) {
804e68cb8b4SAlexey Kardashevskiy         spapr->htab_save_index = 0;
805e68cb8b4SAlexey Kardashevskiy         spapr->htab_first_pass = true;
806e68cb8b4SAlexey Kardashevskiy     } else {
807e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
808e68cb8b4SAlexey Kardashevskiy 
809e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = kvmppc_get_htab_fd(false);
810e68cb8b4SAlexey Kardashevskiy         if (spapr->htab_fd < 0) {
811e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n",
812e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
813e68cb8b4SAlexey Kardashevskiy             return -1;
814e68cb8b4SAlexey Kardashevskiy         }
8154be21d56SDavid Gibson     }
8164be21d56SDavid Gibson 
817e68cb8b4SAlexey Kardashevskiy 
818e68cb8b4SAlexey Kardashevskiy     return 0;
819e68cb8b4SAlexey Kardashevskiy }
8204be21d56SDavid Gibson 
8214be21d56SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr,
8224be21d56SDavid Gibson                                  int64_t max_ns)
8234be21d56SDavid Gibson {
8244be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
8254be21d56SDavid Gibson     int index = spapr->htab_save_index;
826bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
8274be21d56SDavid Gibson 
8284be21d56SDavid Gibson     assert(spapr->htab_first_pass);
8294be21d56SDavid Gibson 
8304be21d56SDavid Gibson     do {
8314be21d56SDavid Gibson         int chunkstart;
8324be21d56SDavid Gibson 
8334be21d56SDavid Gibson         /* Consume invalid HPTEs */
8344be21d56SDavid Gibson         while ((index < htabslots)
8354be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
8364be21d56SDavid Gibson             index++;
8374be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
8384be21d56SDavid Gibson         }
8394be21d56SDavid Gibson 
8404be21d56SDavid Gibson         /* Consume valid HPTEs */
8414be21d56SDavid Gibson         chunkstart = index;
8424be21d56SDavid Gibson         while ((index < htabslots)
8434be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
8444be21d56SDavid Gibson             index++;
8454be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
8464be21d56SDavid Gibson         }
8474be21d56SDavid Gibson 
8484be21d56SDavid Gibson         if (index > chunkstart) {
8494be21d56SDavid Gibson             int n_valid = index - chunkstart;
8504be21d56SDavid Gibson 
8514be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
8524be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
8534be21d56SDavid Gibson             qemu_put_be16(f, 0);
8544be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
8554be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
8564be21d56SDavid Gibson 
857bc72ad67SAlex Bligh             if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
8584be21d56SDavid Gibson                 break;
8594be21d56SDavid Gibson             }
8604be21d56SDavid Gibson         }
8614be21d56SDavid Gibson     } while ((index < htabslots) && !qemu_file_rate_limit(f));
8624be21d56SDavid Gibson 
8634be21d56SDavid Gibson     if (index >= htabslots) {
8644be21d56SDavid Gibson         assert(index == htabslots);
8654be21d56SDavid Gibson         index = 0;
8664be21d56SDavid Gibson         spapr->htab_first_pass = false;
8674be21d56SDavid Gibson     }
8684be21d56SDavid Gibson     spapr->htab_save_index = index;
8694be21d56SDavid Gibson }
8704be21d56SDavid Gibson 
871e68cb8b4SAlexey Kardashevskiy static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr,
8724be21d56SDavid Gibson                                 int64_t max_ns)
8734be21d56SDavid Gibson {
8744be21d56SDavid Gibson     bool final = max_ns < 0;
8754be21d56SDavid Gibson     int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
8764be21d56SDavid Gibson     int examined = 0, sent = 0;
8774be21d56SDavid Gibson     int index = spapr->htab_save_index;
878bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
8794be21d56SDavid Gibson 
8804be21d56SDavid Gibson     assert(!spapr->htab_first_pass);
8814be21d56SDavid Gibson 
8824be21d56SDavid Gibson     do {
8834be21d56SDavid Gibson         int chunkstart, invalidstart;
8844be21d56SDavid Gibson 
8854be21d56SDavid Gibson         /* Consume non-dirty HPTEs */
8864be21d56SDavid Gibson         while ((index < htabslots)
8874be21d56SDavid Gibson                && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
8884be21d56SDavid Gibson             index++;
8894be21d56SDavid Gibson             examined++;
8904be21d56SDavid Gibson         }
8914be21d56SDavid Gibson 
8924be21d56SDavid Gibson         chunkstart = index;
8934be21d56SDavid Gibson         /* Consume valid dirty HPTEs */
8944be21d56SDavid Gibson         while ((index < htabslots)
8954be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
8964be21d56SDavid Gibson                && HPTE_VALID(HPTE(spapr->htab, index))) {
8974be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
8984be21d56SDavid Gibson             index++;
8994be21d56SDavid Gibson             examined++;
9004be21d56SDavid Gibson         }
9014be21d56SDavid Gibson 
9024be21d56SDavid Gibson         invalidstart = index;
9034be21d56SDavid Gibson         /* Consume invalid dirty HPTEs */
9044be21d56SDavid Gibson         while ((index < htabslots)
9054be21d56SDavid Gibson                && HPTE_DIRTY(HPTE(spapr->htab, index))
9064be21d56SDavid Gibson                && !HPTE_VALID(HPTE(spapr->htab, index))) {
9074be21d56SDavid Gibson             CLEAN_HPTE(HPTE(spapr->htab, index));
9084be21d56SDavid Gibson             index++;
9094be21d56SDavid Gibson             examined++;
9104be21d56SDavid Gibson         }
9114be21d56SDavid Gibson 
9124be21d56SDavid Gibson         if (index > chunkstart) {
9134be21d56SDavid Gibson             int n_valid = invalidstart - chunkstart;
9144be21d56SDavid Gibson             int n_invalid = index - invalidstart;
9154be21d56SDavid Gibson 
9164be21d56SDavid Gibson             qemu_put_be32(f, chunkstart);
9174be21d56SDavid Gibson             qemu_put_be16(f, n_valid);
9184be21d56SDavid Gibson             qemu_put_be16(f, n_invalid);
9194be21d56SDavid Gibson             qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
9204be21d56SDavid Gibson                             HASH_PTE_SIZE_64 * n_valid);
9214be21d56SDavid Gibson             sent += index - chunkstart;
9224be21d56SDavid Gibson 
923bc72ad67SAlex Bligh             if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
9244be21d56SDavid Gibson                 break;
9254be21d56SDavid Gibson             }
9264be21d56SDavid Gibson         }
9274be21d56SDavid Gibson 
9284be21d56SDavid Gibson         if (examined >= htabslots) {
9294be21d56SDavid Gibson             break;
9304be21d56SDavid Gibson         }
9314be21d56SDavid Gibson 
9324be21d56SDavid Gibson         if (index >= htabslots) {
9334be21d56SDavid Gibson             assert(index == htabslots);
9344be21d56SDavid Gibson             index = 0;
9354be21d56SDavid Gibson         }
9364be21d56SDavid Gibson     } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
9374be21d56SDavid Gibson 
9384be21d56SDavid Gibson     if (index >= htabslots) {
9394be21d56SDavid Gibson         assert(index == htabslots);
9404be21d56SDavid Gibson         index = 0;
9414be21d56SDavid Gibson     }
9424be21d56SDavid Gibson 
9434be21d56SDavid Gibson     spapr->htab_save_index = index;
9444be21d56SDavid Gibson 
945e68cb8b4SAlexey Kardashevskiy     return (examined >= htabslots) && (sent == 0) ? 1 : 0;
9464be21d56SDavid Gibson }
9474be21d56SDavid Gibson 
948e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS    5000000 /* 5 ms */
949e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE    2048
950e68cb8b4SAlexey Kardashevskiy 
9514be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque)
9524be21d56SDavid Gibson {
9534be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
954e68cb8b4SAlexey Kardashevskiy     int rc = 0;
9554be21d56SDavid Gibson 
9564be21d56SDavid Gibson     /* Iteration header */
9574be21d56SDavid Gibson     qemu_put_be32(f, 0);
9584be21d56SDavid Gibson 
959e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
960e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
961e68cb8b4SAlexey Kardashevskiy 
962e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd,
963e68cb8b4SAlexey Kardashevskiy                               MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
964e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
965e68cb8b4SAlexey Kardashevskiy             return rc;
966e68cb8b4SAlexey Kardashevskiy         }
967e68cb8b4SAlexey Kardashevskiy     } else  if (spapr->htab_first_pass) {
9684be21d56SDavid Gibson         htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
9694be21d56SDavid Gibson     } else {
970e68cb8b4SAlexey Kardashevskiy         rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
9714be21d56SDavid Gibson     }
9724be21d56SDavid Gibson 
9734be21d56SDavid Gibson     /* End marker */
9744be21d56SDavid Gibson     qemu_put_be32(f, 0);
9754be21d56SDavid Gibson     qemu_put_be16(f, 0);
9764be21d56SDavid Gibson     qemu_put_be16(f, 0);
9774be21d56SDavid Gibson 
978e68cb8b4SAlexey Kardashevskiy     return rc;
9794be21d56SDavid Gibson }
9804be21d56SDavid Gibson 
9814be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque)
9824be21d56SDavid Gibson {
9834be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
9844be21d56SDavid Gibson 
9854be21d56SDavid Gibson     /* Iteration header */
9864be21d56SDavid Gibson     qemu_put_be32(f, 0);
9874be21d56SDavid Gibson 
988e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
989e68cb8b4SAlexey Kardashevskiy         int rc;
990e68cb8b4SAlexey Kardashevskiy 
991e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
992e68cb8b4SAlexey Kardashevskiy 
993e68cb8b4SAlexey Kardashevskiy         rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1);
994e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
995e68cb8b4SAlexey Kardashevskiy             return rc;
996e68cb8b4SAlexey Kardashevskiy         }
997e68cb8b4SAlexey Kardashevskiy         close(spapr->htab_fd);
998e68cb8b4SAlexey Kardashevskiy         spapr->htab_fd = -1;
999e68cb8b4SAlexey Kardashevskiy     } else {
10004be21d56SDavid Gibson         htab_save_later_pass(f, spapr, -1);
1001e68cb8b4SAlexey Kardashevskiy     }
10024be21d56SDavid Gibson 
10034be21d56SDavid Gibson     /* End marker */
10044be21d56SDavid Gibson     qemu_put_be32(f, 0);
10054be21d56SDavid Gibson     qemu_put_be16(f, 0);
10064be21d56SDavid Gibson     qemu_put_be16(f, 0);
10074be21d56SDavid Gibson 
10084be21d56SDavid Gibson     return 0;
10094be21d56SDavid Gibson }
10104be21d56SDavid Gibson 
10114be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id)
10124be21d56SDavid Gibson {
10134be21d56SDavid Gibson     sPAPREnvironment *spapr = opaque;
10144be21d56SDavid Gibson     uint32_t section_hdr;
1015e68cb8b4SAlexey Kardashevskiy     int fd = -1;
10164be21d56SDavid Gibson 
10174be21d56SDavid Gibson     if (version_id < 1 || version_id > 1) {
10184be21d56SDavid Gibson         fprintf(stderr, "htab_load() bad version\n");
10194be21d56SDavid Gibson         return -EINVAL;
10204be21d56SDavid Gibson     }
10214be21d56SDavid Gibson 
10224be21d56SDavid Gibson     section_hdr = qemu_get_be32(f);
10234be21d56SDavid Gibson 
10244be21d56SDavid Gibson     if (section_hdr) {
10254be21d56SDavid Gibson         /* First section, just the hash shift */
10264be21d56SDavid Gibson         if (spapr->htab_shift != section_hdr) {
10274be21d56SDavid Gibson             return -EINVAL;
10284be21d56SDavid Gibson         }
10294be21d56SDavid Gibson         return 0;
10304be21d56SDavid Gibson     }
10314be21d56SDavid Gibson 
1032e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1033e68cb8b4SAlexey Kardashevskiy         assert(kvm_enabled());
1034e68cb8b4SAlexey Kardashevskiy 
1035e68cb8b4SAlexey Kardashevskiy         fd = kvmppc_get_htab_fd(true);
1036e68cb8b4SAlexey Kardashevskiy         if (fd < 0) {
1037e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n",
1038e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
1039e68cb8b4SAlexey Kardashevskiy         }
1040e68cb8b4SAlexey Kardashevskiy     }
1041e68cb8b4SAlexey Kardashevskiy 
10424be21d56SDavid Gibson     while (true) {
10434be21d56SDavid Gibson         uint32_t index;
10444be21d56SDavid Gibson         uint16_t n_valid, n_invalid;
10454be21d56SDavid Gibson 
10464be21d56SDavid Gibson         index = qemu_get_be32(f);
10474be21d56SDavid Gibson         n_valid = qemu_get_be16(f);
10484be21d56SDavid Gibson         n_invalid = qemu_get_be16(f);
10494be21d56SDavid Gibson 
10504be21d56SDavid Gibson         if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
10514be21d56SDavid Gibson             /* End of Stream */
10524be21d56SDavid Gibson             break;
10534be21d56SDavid Gibson         }
10544be21d56SDavid Gibson 
1055e68cb8b4SAlexey Kardashevskiy         if ((index + n_valid + n_invalid) >
10564be21d56SDavid Gibson             (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
10574be21d56SDavid Gibson             /* Bad index in stream */
10584be21d56SDavid Gibson             fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) "
1059e68cb8b4SAlexey Kardashevskiy                     "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid,
1060e68cb8b4SAlexey Kardashevskiy                     spapr->htab_shift);
10614be21d56SDavid Gibson             return -EINVAL;
10624be21d56SDavid Gibson         }
10634be21d56SDavid Gibson 
1064e68cb8b4SAlexey Kardashevskiy         if (spapr->htab) {
10654be21d56SDavid Gibson             if (n_valid) {
10664be21d56SDavid Gibson                 qemu_get_buffer(f, HPTE(spapr->htab, index),
10674be21d56SDavid Gibson                                 HASH_PTE_SIZE_64 * n_valid);
10684be21d56SDavid Gibson             }
10694be21d56SDavid Gibson             if (n_invalid) {
10704be21d56SDavid Gibson                 memset(HPTE(spapr->htab, index + n_valid), 0,
10714be21d56SDavid Gibson                        HASH_PTE_SIZE_64 * n_invalid);
10724be21d56SDavid Gibson             }
1073e68cb8b4SAlexey Kardashevskiy         } else {
1074e68cb8b4SAlexey Kardashevskiy             int rc;
1075e68cb8b4SAlexey Kardashevskiy 
1076e68cb8b4SAlexey Kardashevskiy             assert(fd >= 0);
1077e68cb8b4SAlexey Kardashevskiy 
1078e68cb8b4SAlexey Kardashevskiy             rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1079e68cb8b4SAlexey Kardashevskiy             if (rc < 0) {
1080e68cb8b4SAlexey Kardashevskiy                 return rc;
1081e68cb8b4SAlexey Kardashevskiy             }
1082e68cb8b4SAlexey Kardashevskiy         }
1083e68cb8b4SAlexey Kardashevskiy     }
1084e68cb8b4SAlexey Kardashevskiy 
1085e68cb8b4SAlexey Kardashevskiy     if (!spapr->htab) {
1086e68cb8b4SAlexey Kardashevskiy         assert(fd >= 0);
1087e68cb8b4SAlexey Kardashevskiy         close(fd);
10884be21d56SDavid Gibson     }
10894be21d56SDavid Gibson 
10904be21d56SDavid Gibson     return 0;
10914be21d56SDavid Gibson }
10924be21d56SDavid Gibson 
10934be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = {
10944be21d56SDavid Gibson     .save_live_setup = htab_save_setup,
10954be21d56SDavid Gibson     .save_live_iterate = htab_save_iterate,
10964be21d56SDavid Gibson     .save_live_complete = htab_save_complete,
10974be21d56SDavid Gibson     .load_state = htab_load,
10984be21d56SDavid Gibson };
10994be21d56SDavid Gibson 
110053018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */
110153018216SPaolo Bonzini static void ppc_spapr_init(QEMUMachineInitArgs *args)
110253018216SPaolo Bonzini {
110353018216SPaolo Bonzini     ram_addr_t ram_size = args->ram_size;
110453018216SPaolo Bonzini     const char *cpu_model = args->cpu_model;
110553018216SPaolo Bonzini     const char *kernel_filename = args->kernel_filename;
110653018216SPaolo Bonzini     const char *kernel_cmdline = args->kernel_cmdline;
110753018216SPaolo Bonzini     const char *initrd_filename = args->initrd_filename;
1108c1654732SMarkus Armbruster     const char *boot_device = args->boot_order;
110953018216SPaolo Bonzini     PowerPCCPU *cpu;
111053018216SPaolo Bonzini     CPUPPCState *env;
111153018216SPaolo Bonzini     PCIHostState *phb;
111253018216SPaolo Bonzini     int i;
111353018216SPaolo Bonzini     MemoryRegion *sysmem = get_system_memory();
111453018216SPaolo Bonzini     MemoryRegion *ram = g_new(MemoryRegion, 1);
111553018216SPaolo Bonzini     hwaddr rma_alloc_size;
111653018216SPaolo Bonzini     uint32_t initrd_base = 0;
111753018216SPaolo Bonzini     long kernel_size = 0, initrd_size = 0;
111853018216SPaolo Bonzini     long load_limit, rtas_limit, fw_size;
111916457e7fSBenjamin Herrenschmidt     bool kernel_le = false;
112053018216SPaolo Bonzini     char *filename;
112153018216SPaolo Bonzini 
112253018216SPaolo Bonzini     msi_supported = true;
112353018216SPaolo Bonzini 
112453018216SPaolo Bonzini     spapr = g_malloc0(sizeof(*spapr));
112553018216SPaolo Bonzini     QLIST_INIT(&spapr->phbs);
112653018216SPaolo Bonzini 
112753018216SPaolo Bonzini     cpu_ppc_hypercall = emulate_spapr_hypercall;
112853018216SPaolo Bonzini 
112953018216SPaolo Bonzini     /* Allocate RMA if necessary */
113053018216SPaolo Bonzini     rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem);
113153018216SPaolo Bonzini 
113253018216SPaolo Bonzini     if (rma_alloc_size == -1) {
113353018216SPaolo Bonzini         hw_error("qemu: Unable to create RMA\n");
113453018216SPaolo Bonzini         exit(1);
113553018216SPaolo Bonzini     }
113653018216SPaolo Bonzini 
113753018216SPaolo Bonzini     if (rma_alloc_size && (rma_alloc_size < ram_size)) {
113853018216SPaolo Bonzini         spapr->rma_size = rma_alloc_size;
113953018216SPaolo Bonzini     } else {
114053018216SPaolo Bonzini         spapr->rma_size = ram_size;
114153018216SPaolo Bonzini 
114253018216SPaolo Bonzini         /* With KVM, we don't actually know whether KVM supports an
114353018216SPaolo Bonzini          * unbounded RMA (PR KVM) or is limited by the hash table size
114453018216SPaolo Bonzini          * (HV KVM using VRMA), so we always assume the latter
114553018216SPaolo Bonzini          *
114653018216SPaolo Bonzini          * In that case, we also limit the initial allocations for RTAS
114753018216SPaolo Bonzini          * etc... to 256M since we have no way to know what the VRMA size
114853018216SPaolo Bonzini          * is going to be as it depends on the size of the hash table
114953018216SPaolo Bonzini          * isn't determined yet.
115053018216SPaolo Bonzini          */
115153018216SPaolo Bonzini         if (kvm_enabled()) {
115253018216SPaolo Bonzini             spapr->vrma_adjust = 1;
115353018216SPaolo Bonzini             spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
115453018216SPaolo Bonzini         }
115553018216SPaolo Bonzini     }
115653018216SPaolo Bonzini 
115753018216SPaolo Bonzini     /* We place the device tree and RTAS just below either the top of the RMA,
115853018216SPaolo Bonzini      * or just below 2GB, whichever is lowere, so that it can be
115953018216SPaolo Bonzini      * processed with 32-bit real mode code if necessary */
116053018216SPaolo Bonzini     rtas_limit = MIN(spapr->rma_size, 0x80000000);
116153018216SPaolo Bonzini     spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
116253018216SPaolo Bonzini     spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
116353018216SPaolo Bonzini     load_limit = spapr->fdt_addr - FW_OVERHEAD;
116453018216SPaolo Bonzini 
116553018216SPaolo Bonzini     /* We aim for a hash table of size 1/128 the size of RAM.  The
116653018216SPaolo Bonzini      * normal rule of thumb is 1/64 the size of RAM, but that's much
116753018216SPaolo Bonzini      * more than needed for the Linux guests we support. */
116853018216SPaolo Bonzini     spapr->htab_shift = 18; /* Minimum architected size */
116953018216SPaolo Bonzini     while (spapr->htab_shift <= 46) {
117053018216SPaolo Bonzini         if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) {
117153018216SPaolo Bonzini             break;
117253018216SPaolo Bonzini         }
117353018216SPaolo Bonzini         spapr->htab_shift++;
117453018216SPaolo Bonzini     }
117553018216SPaolo Bonzini 
11767b565160SDavid Gibson     /* Set up Interrupt Controller before we create the VCPUs */
11777b565160SDavid Gibson     spapr->icp = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads,
11787b565160SDavid Gibson                                   XICS_IRQS);
11797b565160SDavid Gibson     spapr->next_irq = XICS_IRQ_BASE;
11807b565160SDavid Gibson 
118153018216SPaolo Bonzini     /* init CPUs */
118253018216SPaolo Bonzini     if (cpu_model == NULL) {
118353018216SPaolo Bonzini         cpu_model = kvm_enabled() ? "host" : "POWER7";
118453018216SPaolo Bonzini     }
118553018216SPaolo Bonzini     for (i = 0; i < smp_cpus; i++) {
118653018216SPaolo Bonzini         cpu = cpu_ppc_init(cpu_model);
118753018216SPaolo Bonzini         if (cpu == NULL) {
118853018216SPaolo Bonzini             fprintf(stderr, "Unable to find PowerPC CPU definition\n");
118953018216SPaolo Bonzini             exit(1);
119053018216SPaolo Bonzini         }
119153018216SPaolo Bonzini         env = &cpu->env;
119253018216SPaolo Bonzini 
119353018216SPaolo Bonzini         /* Set time-base frequency to 512 MHz */
119453018216SPaolo Bonzini         cpu_ppc_tb_init(env, TIMEBASE_FREQ);
119553018216SPaolo Bonzini 
11962cf3eb6dSFabien Chouteau         /* PAPR always has exception vectors in RAM not ROM. To ensure this,
11972cf3eb6dSFabien Chouteau          * MSR[IP] should never be set.
11982cf3eb6dSFabien Chouteau          */
11992cf3eb6dSFabien Chouteau         env->msr_mask &= ~(1 << 6);
120053018216SPaolo Bonzini 
120153018216SPaolo Bonzini         /* Tell KVM that we're in PAPR mode */
120253018216SPaolo Bonzini         if (kvm_enabled()) {
120353018216SPaolo Bonzini             kvmppc_set_papr(cpu);
120453018216SPaolo Bonzini         }
120553018216SPaolo Bonzini 
120624408a7dSAlexey Kardashevskiy         xics_cpu_setup(spapr->icp, cpu);
120724408a7dSAlexey Kardashevskiy 
120853018216SPaolo Bonzini         qemu_register_reset(spapr_cpu_reset, cpu);
120953018216SPaolo Bonzini     }
121053018216SPaolo Bonzini 
121153018216SPaolo Bonzini     /* allocate RAM */
121253018216SPaolo Bonzini     spapr->ram_limit = ram_size;
121353018216SPaolo Bonzini     if (spapr->ram_limit > rma_alloc_size) {
121453018216SPaolo Bonzini         ram_addr_t nonrma_base = rma_alloc_size;
121553018216SPaolo Bonzini         ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size;
121653018216SPaolo Bonzini 
12172c9b15caSPaolo Bonzini         memory_region_init_ram(ram, NULL, "ppc_spapr.ram", nonrma_size);
121853018216SPaolo Bonzini         vmstate_register_ram_global(ram);
121953018216SPaolo Bonzini         memory_region_add_subregion(sysmem, nonrma_base, ram);
122053018216SPaolo Bonzini     }
122153018216SPaolo Bonzini 
122253018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
122353018216SPaolo Bonzini     spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr,
122453018216SPaolo Bonzini                                            rtas_limit - spapr->rtas_addr);
122553018216SPaolo Bonzini     if (spapr->rtas_size < 0) {
122653018216SPaolo Bonzini         hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
122753018216SPaolo Bonzini         exit(1);
122853018216SPaolo Bonzini     }
122953018216SPaolo Bonzini     if (spapr->rtas_size > RTAS_MAX_SIZE) {
123053018216SPaolo Bonzini         hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n",
123153018216SPaolo Bonzini                  spapr->rtas_size, RTAS_MAX_SIZE);
123253018216SPaolo Bonzini         exit(1);
123353018216SPaolo Bonzini     }
123453018216SPaolo Bonzini     g_free(filename);
123553018216SPaolo Bonzini 
123653018216SPaolo Bonzini     /* Set up EPOW events infrastructure */
123753018216SPaolo Bonzini     spapr_events_init(spapr);
123853018216SPaolo Bonzini 
123953018216SPaolo Bonzini     /* Set up VIO bus */
124053018216SPaolo Bonzini     spapr->vio_bus = spapr_vio_bus_init();
124153018216SPaolo Bonzini 
124253018216SPaolo Bonzini     for (i = 0; i < MAX_SERIAL_PORTS; i++) {
124353018216SPaolo Bonzini         if (serial_hds[i]) {
124453018216SPaolo Bonzini             spapr_vty_create(spapr->vio_bus, serial_hds[i]);
124553018216SPaolo Bonzini         }
124653018216SPaolo Bonzini     }
124753018216SPaolo Bonzini 
124853018216SPaolo Bonzini     /* We always have at least the nvram device on VIO */
124953018216SPaolo Bonzini     spapr_create_nvram(spapr);
125053018216SPaolo Bonzini 
125153018216SPaolo Bonzini     /* Set up PCI */
1252f1c2dc7cSAlexey Kardashevskiy     spapr_pci_msi_init(spapr, SPAPR_PCI_MSI_WINDOW);
125353018216SPaolo Bonzini     spapr_pci_rtas_init();
125453018216SPaolo Bonzini 
125589dfd6e1SDavid Gibson     phb = spapr_create_phb(spapr, 0);
125653018216SPaolo Bonzini 
125753018216SPaolo Bonzini     for (i = 0; i < nb_nics; i++) {
125853018216SPaolo Bonzini         NICInfo *nd = &nd_table[i];
125953018216SPaolo Bonzini 
126053018216SPaolo Bonzini         if (!nd->model) {
126153018216SPaolo Bonzini             nd->model = g_strdup("ibmveth");
126253018216SPaolo Bonzini         }
126353018216SPaolo Bonzini 
126453018216SPaolo Bonzini         if (strcmp(nd->model, "ibmveth") == 0) {
126553018216SPaolo Bonzini             spapr_vlan_create(spapr->vio_bus, nd);
126653018216SPaolo Bonzini         } else {
126729b358f9SDavid Gibson             pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
126853018216SPaolo Bonzini         }
126953018216SPaolo Bonzini     }
127053018216SPaolo Bonzini 
127153018216SPaolo Bonzini     for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
127253018216SPaolo Bonzini         spapr_vscsi_create(spapr->vio_bus);
127353018216SPaolo Bonzini     }
127453018216SPaolo Bonzini 
127553018216SPaolo Bonzini     /* Graphics */
127653018216SPaolo Bonzini     if (spapr_vga_init(phb->bus)) {
127753018216SPaolo Bonzini         spapr->has_graphics = true;
127853018216SPaolo Bonzini     }
127953018216SPaolo Bonzini 
128053018216SPaolo Bonzini     if (usb_enabled(spapr->has_graphics)) {
128153018216SPaolo Bonzini         pci_create_simple(phb->bus, -1, "pci-ohci");
128253018216SPaolo Bonzini         if (spapr->has_graphics) {
128353018216SPaolo Bonzini             usbdevice_create("keyboard");
128453018216SPaolo Bonzini             usbdevice_create("mouse");
128553018216SPaolo Bonzini         }
128653018216SPaolo Bonzini     }
128753018216SPaolo Bonzini 
128853018216SPaolo Bonzini     if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
128953018216SPaolo Bonzini         fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
129053018216SPaolo Bonzini                 "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
129153018216SPaolo Bonzini         exit(1);
129253018216SPaolo Bonzini     }
129353018216SPaolo Bonzini 
129453018216SPaolo Bonzini     if (kernel_filename) {
129553018216SPaolo Bonzini         uint64_t lowaddr = 0;
129653018216SPaolo Bonzini 
129753018216SPaolo Bonzini         kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
129853018216SPaolo Bonzini                                NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
129953018216SPaolo Bonzini         if (kernel_size < 0) {
130016457e7fSBenjamin Herrenschmidt             kernel_size = load_elf(kernel_filename,
130116457e7fSBenjamin Herrenschmidt                                    translate_kernel_address, NULL,
130216457e7fSBenjamin Herrenschmidt                                    NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0);
130316457e7fSBenjamin Herrenschmidt             kernel_le = kernel_size > 0;
130416457e7fSBenjamin Herrenschmidt         }
130516457e7fSBenjamin Herrenschmidt         if (kernel_size < 0) {
130653018216SPaolo Bonzini             kernel_size = load_image_targphys(kernel_filename,
130753018216SPaolo Bonzini                                               KERNEL_LOAD_ADDR,
130853018216SPaolo Bonzini                                               load_limit - KERNEL_LOAD_ADDR);
130953018216SPaolo Bonzini         }
131053018216SPaolo Bonzini         if (kernel_size < 0) {
131153018216SPaolo Bonzini             fprintf(stderr, "qemu: could not load kernel '%s'\n",
131253018216SPaolo Bonzini                     kernel_filename);
131353018216SPaolo Bonzini             exit(1);
131453018216SPaolo Bonzini         }
131553018216SPaolo Bonzini 
131653018216SPaolo Bonzini         /* load initrd */
131753018216SPaolo Bonzini         if (initrd_filename) {
131853018216SPaolo Bonzini             /* Try to locate the initrd in the gap between the kernel
131953018216SPaolo Bonzini              * and the firmware. Add a bit of space just in case
132053018216SPaolo Bonzini              */
132153018216SPaolo Bonzini             initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
132253018216SPaolo Bonzini             initrd_size = load_image_targphys(initrd_filename, initrd_base,
132353018216SPaolo Bonzini                                               load_limit - initrd_base);
132453018216SPaolo Bonzini             if (initrd_size < 0) {
132553018216SPaolo Bonzini                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
132653018216SPaolo Bonzini                         initrd_filename);
132753018216SPaolo Bonzini                 exit(1);
132853018216SPaolo Bonzini             }
132953018216SPaolo Bonzini         } else {
133053018216SPaolo Bonzini             initrd_base = 0;
133153018216SPaolo Bonzini             initrd_size = 0;
133253018216SPaolo Bonzini         }
133353018216SPaolo Bonzini     }
133453018216SPaolo Bonzini 
13358e7ea787SAndreas Färber     if (bios_name == NULL) {
13368e7ea787SAndreas Färber         bios_name = FW_FILE_NAME;
13378e7ea787SAndreas Färber     }
13388e7ea787SAndreas Färber     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
133953018216SPaolo Bonzini     fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
134053018216SPaolo Bonzini     if (fw_size < 0) {
134153018216SPaolo Bonzini         hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
134253018216SPaolo Bonzini         exit(1);
134353018216SPaolo Bonzini     }
134453018216SPaolo Bonzini     g_free(filename);
134553018216SPaolo Bonzini 
134653018216SPaolo Bonzini     spapr->entry_point = 0x100;
134753018216SPaolo Bonzini 
13484be21d56SDavid Gibson     vmstate_register(NULL, 0, &vmstate_spapr, spapr);
13494be21d56SDavid Gibson     register_savevm_live(NULL, "spapr/htab", -1, 1,
13504be21d56SDavid Gibson                          &savevm_htab_handlers, spapr);
13514be21d56SDavid Gibson 
135253018216SPaolo Bonzini     /* Prepare the device tree */
1353*3bbf37f2SAndreas Färber     spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size,
135416457e7fSBenjamin Herrenschmidt                                             kernel_size, kernel_le,
135553018216SPaolo Bonzini                                             boot_device, kernel_cmdline,
135653018216SPaolo Bonzini                                             spapr->epow_irq);
135753018216SPaolo Bonzini     assert(spapr->fdt_skel != NULL);
135853018216SPaolo Bonzini }
135953018216SPaolo Bonzini 
136053018216SPaolo Bonzini static QEMUMachine spapr_machine = {
136153018216SPaolo Bonzini     .name = "pseries",
136253018216SPaolo Bonzini     .desc = "pSeries Logical Partition (PAPR compliant)",
1363159f8286SDavid Gibson     .is_default = 1,
136453018216SPaolo Bonzini     .init = ppc_spapr_init,
136553018216SPaolo Bonzini     .reset = ppc_spapr_reset,
136653018216SPaolo Bonzini     .block_default_type = IF_SCSI,
136753018216SPaolo Bonzini     .max_cpus = MAX_CPUS,
136853018216SPaolo Bonzini     .no_parallel = 1,
1369c1654732SMarkus Armbruster     .default_boot_order = NULL,
137053018216SPaolo Bonzini };
137153018216SPaolo Bonzini 
137253018216SPaolo Bonzini static void spapr_machine_init(void)
137353018216SPaolo Bonzini {
137453018216SPaolo Bonzini     qemu_register_machine(&spapr_machine);
137553018216SPaolo Bonzini }
137653018216SPaolo Bonzini 
137753018216SPaolo Bonzini machine_init(spapr_machine_init);
1378