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" 3253018216SPaolo Bonzini #include "sysemu/blockdev.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" 5853018216SPaolo Bonzini 5953018216SPaolo Bonzini #include <libfdt.h> 6053018216SPaolo Bonzini 6153018216SPaolo Bonzini /* SLOF memory layout: 6253018216SPaolo Bonzini * 6353018216SPaolo Bonzini * SLOF raw image loaded at 0, copies its romfs right below the flat 6453018216SPaolo Bonzini * device-tree, then position SLOF itself 31M below that 6553018216SPaolo Bonzini * 6653018216SPaolo Bonzini * So we set FW_OVERHEAD to 40MB which should account for all of that 6753018216SPaolo Bonzini * and more 6853018216SPaolo Bonzini * 6953018216SPaolo Bonzini * We load our kernel at 4M, leaving space for SLOF initial image 7053018216SPaolo Bonzini */ 713bf6eeddSAlexey Kardashevskiy #define FDT_MAX_SIZE 0x40000 7253018216SPaolo Bonzini #define RTAS_MAX_SIZE 0x10000 7353018216SPaolo Bonzini #define FW_MAX_SIZE 0x400000 7453018216SPaolo Bonzini #define FW_FILE_NAME "slof.bin" 7553018216SPaolo Bonzini #define FW_OVERHEAD 0x2800000 7653018216SPaolo Bonzini #define KERNEL_LOAD_ADDR FW_MAX_SIZE 7753018216SPaolo Bonzini 7853018216SPaolo Bonzini #define MIN_RMA_SLOF 128UL 7953018216SPaolo Bonzini 8053018216SPaolo Bonzini #define TIMEBASE_FREQ 512000000ULL 8153018216SPaolo Bonzini 8253018216SPaolo Bonzini #define MAX_CPUS 256 8353018216SPaolo Bonzini #define XICS_IRQS 1024 8453018216SPaolo Bonzini 8553018216SPaolo Bonzini #define PHANDLE_XICP 0x00001111 8653018216SPaolo Bonzini 8753018216SPaolo Bonzini #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift)) 8853018216SPaolo Bonzini 8929ee3247SAlexey Kardashevskiy #define TYPE_SPAPR_MACHINE "spapr-machine" 9029ee3247SAlexey Kardashevskiy 9153018216SPaolo Bonzini sPAPREnvironment *spapr; 9253018216SPaolo Bonzini 9353018216SPaolo Bonzini int spapr_allocate_irq(int hint, bool lsi) 9453018216SPaolo Bonzini { 9553018216SPaolo Bonzini int irq; 9653018216SPaolo Bonzini 9753018216SPaolo Bonzini if (hint) { 9853018216SPaolo Bonzini irq = hint; 99f1c2dc7cSAlexey Kardashevskiy if (hint >= spapr->next_irq) { 100f1c2dc7cSAlexey Kardashevskiy spapr->next_irq = hint + 1; 101f1c2dc7cSAlexey Kardashevskiy } 10253018216SPaolo Bonzini /* FIXME: we should probably check for collisions somehow */ 10353018216SPaolo Bonzini } else { 10453018216SPaolo Bonzini irq = spapr->next_irq++; 10553018216SPaolo Bonzini } 10653018216SPaolo Bonzini 10753018216SPaolo Bonzini /* Configure irq type */ 10853018216SPaolo Bonzini if (!xics_get_qirq(spapr->icp, irq)) { 10953018216SPaolo Bonzini return 0; 11053018216SPaolo Bonzini } 11153018216SPaolo Bonzini 11253018216SPaolo Bonzini xics_set_irq_type(spapr->icp, irq, lsi); 11353018216SPaolo Bonzini 11453018216SPaolo Bonzini return irq; 11553018216SPaolo Bonzini } 11653018216SPaolo Bonzini 117f1c2dc7cSAlexey Kardashevskiy /* 118f1c2dc7cSAlexey Kardashevskiy * Allocate block of consequtive IRQs, returns a number of the first. 119f1c2dc7cSAlexey Kardashevskiy * If msi==true, aligns the first IRQ number to num. 120f1c2dc7cSAlexey Kardashevskiy */ 121f1c2dc7cSAlexey Kardashevskiy int spapr_allocate_irq_block(int num, bool lsi, bool msi) 12253018216SPaolo Bonzini { 12353018216SPaolo Bonzini int first = -1; 124f1c2dc7cSAlexey Kardashevskiy int i, hint = 0; 125f1c2dc7cSAlexey Kardashevskiy 126f1c2dc7cSAlexey Kardashevskiy /* 127f1c2dc7cSAlexey Kardashevskiy * MSIMesage::data is used for storing VIRQ so 128f1c2dc7cSAlexey Kardashevskiy * it has to be aligned to num to support multiple 129f1c2dc7cSAlexey Kardashevskiy * MSI vectors. MSI-X is not affected by this. 130f1c2dc7cSAlexey Kardashevskiy * The hint is used for the first IRQ, the rest should 13173f395faSStefan Weil * be allocated continuously. 132f1c2dc7cSAlexey Kardashevskiy */ 133f1c2dc7cSAlexey Kardashevskiy if (msi) { 134f1c2dc7cSAlexey Kardashevskiy assert((num == 1) || (num == 2) || (num == 4) || 135f1c2dc7cSAlexey Kardashevskiy (num == 8) || (num == 16) || (num == 32)); 136f1c2dc7cSAlexey Kardashevskiy hint = (spapr->next_irq + num - 1) & ~(num - 1); 137f1c2dc7cSAlexey Kardashevskiy } 13853018216SPaolo Bonzini 13953018216SPaolo Bonzini for (i = 0; i < num; ++i) { 14053018216SPaolo Bonzini int irq; 14153018216SPaolo Bonzini 142f1c2dc7cSAlexey Kardashevskiy irq = spapr_allocate_irq(hint, lsi); 14353018216SPaolo Bonzini if (!irq) { 14453018216SPaolo Bonzini return -1; 14553018216SPaolo Bonzini } 14653018216SPaolo Bonzini 14753018216SPaolo Bonzini if (0 == i) { 14853018216SPaolo Bonzini first = irq; 149f1c2dc7cSAlexey Kardashevskiy hint = 0; 15053018216SPaolo Bonzini } 15153018216SPaolo Bonzini 15253018216SPaolo Bonzini /* If the above doesn't create a consecutive block then that's 15353018216SPaolo Bonzini * an internal bug */ 15453018216SPaolo Bonzini assert(irq == (first + i)); 15553018216SPaolo Bonzini } 15653018216SPaolo Bonzini 15753018216SPaolo Bonzini return first; 15853018216SPaolo Bonzini } 15953018216SPaolo Bonzini 160c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers, 161c04d6cfaSAnthony Liguori int nr_irqs) 162c04d6cfaSAnthony Liguori { 163c04d6cfaSAnthony Liguori DeviceState *dev; 164c04d6cfaSAnthony Liguori 165c04d6cfaSAnthony Liguori dev = qdev_create(NULL, type); 166c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_servers", nr_servers); 167c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs); 168c04d6cfaSAnthony Liguori if (qdev_init(dev) < 0) { 169c04d6cfaSAnthony Liguori return NULL; 170c04d6cfaSAnthony Liguori } 171c04d6cfaSAnthony Liguori 1725a3d7b23SAlexey Kardashevskiy return XICS_COMMON(dev); 173c04d6cfaSAnthony Liguori } 174c04d6cfaSAnthony Liguori 175c04d6cfaSAnthony Liguori static XICSState *xics_system_init(int nr_servers, int nr_irqs) 176c04d6cfaSAnthony Liguori { 177c04d6cfaSAnthony Liguori XICSState *icp = NULL; 178c04d6cfaSAnthony Liguori 17911ad93f6SDavid Gibson if (kvm_enabled()) { 18011ad93f6SDavid Gibson QemuOpts *machine_opts = qemu_get_machine_opts(); 18111ad93f6SDavid Gibson bool irqchip_allowed = qemu_opt_get_bool(machine_opts, 18211ad93f6SDavid Gibson "kernel_irqchip", true); 18311ad93f6SDavid Gibson bool irqchip_required = qemu_opt_get_bool(machine_opts, 18411ad93f6SDavid Gibson "kernel_irqchip", false); 18511ad93f6SDavid Gibson if (irqchip_allowed) { 18611ad93f6SDavid Gibson icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs); 18711ad93f6SDavid Gibson } 18811ad93f6SDavid Gibson 18911ad93f6SDavid Gibson if (irqchip_required && !icp) { 19011ad93f6SDavid Gibson perror("Failed to create in-kernel XICS\n"); 19111ad93f6SDavid Gibson abort(); 19211ad93f6SDavid Gibson } 19311ad93f6SDavid Gibson } 19411ad93f6SDavid Gibson 19511ad93f6SDavid Gibson if (!icp) { 196c04d6cfaSAnthony Liguori icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs); 19711ad93f6SDavid Gibson } 19811ad93f6SDavid Gibson 199c04d6cfaSAnthony Liguori if (!icp) { 200c04d6cfaSAnthony Liguori perror("Failed to create XICS\n"); 201c04d6cfaSAnthony Liguori abort(); 202c04d6cfaSAnthony Liguori } 203c04d6cfaSAnthony Liguori 204c04d6cfaSAnthony Liguori return icp; 205c04d6cfaSAnthony Liguori } 206c04d6cfaSAnthony Liguori 207833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu, 208833d4668SAlexey Kardashevskiy int smt_threads) 209833d4668SAlexey Kardashevskiy { 210833d4668SAlexey Kardashevskiy int i, ret = 0; 211833d4668SAlexey Kardashevskiy uint32_t servers_prop[smt_threads]; 212833d4668SAlexey Kardashevskiy uint32_t gservers_prop[smt_threads * 2]; 213833d4668SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 214833d4668SAlexey Kardashevskiy 2156d9412eaSAlexey Kardashevskiy if (cpu->cpu_version) { 2166d9412eaSAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "cpu-version", 2176d9412eaSAlexey Kardashevskiy &cpu->cpu_version, sizeof(cpu->cpu_version)); 2186d9412eaSAlexey Kardashevskiy if (ret < 0) { 2196d9412eaSAlexey Kardashevskiy return ret; 2206d9412eaSAlexey Kardashevskiy } 2216d9412eaSAlexey Kardashevskiy } 2226d9412eaSAlexey Kardashevskiy 223833d4668SAlexey Kardashevskiy /* Build interrupt servers and gservers properties */ 224833d4668SAlexey Kardashevskiy for (i = 0; i < smt_threads; i++) { 225833d4668SAlexey Kardashevskiy servers_prop[i] = cpu_to_be32(index + i); 226833d4668SAlexey Kardashevskiy /* Hack, direct the group queues back to cpu 0 */ 227833d4668SAlexey Kardashevskiy gservers_prop[i*2] = cpu_to_be32(index + i); 228833d4668SAlexey Kardashevskiy gservers_prop[i*2 + 1] = 0; 229833d4668SAlexey Kardashevskiy } 230833d4668SAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s", 231833d4668SAlexey Kardashevskiy servers_prop, sizeof(servers_prop)); 232833d4668SAlexey Kardashevskiy if (ret < 0) { 233833d4668SAlexey Kardashevskiy return ret; 234833d4668SAlexey Kardashevskiy } 235833d4668SAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s", 236833d4668SAlexey Kardashevskiy gservers_prop, sizeof(gservers_prop)); 237833d4668SAlexey Kardashevskiy 238833d4668SAlexey Kardashevskiy return ret; 239833d4668SAlexey Kardashevskiy } 240833d4668SAlexey Kardashevskiy 24153018216SPaolo Bonzini static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr) 24253018216SPaolo Bonzini { 24382677ed2SAlexey Kardashevskiy int ret = 0, offset, cpus_offset; 24482677ed2SAlexey Kardashevskiy CPUState *cs; 24553018216SPaolo Bonzini char cpu_model[32]; 24653018216SPaolo Bonzini int smt = kvmppc_smt_threads(); 24753018216SPaolo Bonzini uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)}; 24853018216SPaolo Bonzini 24982677ed2SAlexey Kardashevskiy CPU_FOREACH(cs) { 25082677ed2SAlexey Kardashevskiy PowerPCCPU *cpu = POWERPC_CPU(cs); 25182677ed2SAlexey Kardashevskiy DeviceClass *dc = DEVICE_GET_CLASS(cs); 25282677ed2SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 25353018216SPaolo Bonzini uint32_t associativity[] = {cpu_to_be32(0x5), 25453018216SPaolo Bonzini cpu_to_be32(0x0), 25553018216SPaolo Bonzini cpu_to_be32(0x0), 25653018216SPaolo Bonzini cpu_to_be32(0x0), 25782677ed2SAlexey Kardashevskiy cpu_to_be32(cs->numa_node), 2580f20ba62SAlexey Kardashevskiy cpu_to_be32(index)}; 25953018216SPaolo Bonzini 2600f20ba62SAlexey Kardashevskiy if ((index % smt) != 0) { 26153018216SPaolo Bonzini continue; 26253018216SPaolo Bonzini } 26353018216SPaolo Bonzini 26482677ed2SAlexey Kardashevskiy snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index); 26553018216SPaolo Bonzini 26682677ed2SAlexey Kardashevskiy cpus_offset = fdt_path_offset(fdt, "/cpus"); 26782677ed2SAlexey Kardashevskiy if (cpus_offset < 0) { 26882677ed2SAlexey Kardashevskiy cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), 26982677ed2SAlexey Kardashevskiy "cpus"); 27082677ed2SAlexey Kardashevskiy if (cpus_offset < 0) { 27182677ed2SAlexey Kardashevskiy return cpus_offset; 27282677ed2SAlexey Kardashevskiy } 27382677ed2SAlexey Kardashevskiy } 27482677ed2SAlexey Kardashevskiy offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model); 27582677ed2SAlexey Kardashevskiy if (offset < 0) { 27682677ed2SAlexey Kardashevskiy offset = fdt_add_subnode(fdt, cpus_offset, cpu_model); 27753018216SPaolo Bonzini if (offset < 0) { 27853018216SPaolo Bonzini return offset; 27953018216SPaolo Bonzini } 28082677ed2SAlexey Kardashevskiy } 28153018216SPaolo Bonzini 28253018216SPaolo Bonzini if (nb_numa_nodes > 1) { 28353018216SPaolo Bonzini ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity, 28453018216SPaolo Bonzini sizeof(associativity)); 28553018216SPaolo Bonzini if (ret < 0) { 28653018216SPaolo Bonzini return ret; 28753018216SPaolo Bonzini } 28853018216SPaolo Bonzini } 28953018216SPaolo Bonzini 29053018216SPaolo Bonzini ret = fdt_setprop(fdt, offset, "ibm,pft-size", 29153018216SPaolo Bonzini pft_size_prop, sizeof(pft_size_prop)); 29253018216SPaolo Bonzini if (ret < 0) { 29353018216SPaolo Bonzini return ret; 29453018216SPaolo Bonzini } 295833d4668SAlexey Kardashevskiy 29682677ed2SAlexey Kardashevskiy ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, 2972a48d993SAlexey Kardashevskiy ppc_get_compat_smt_threads(cpu)); 298833d4668SAlexey Kardashevskiy if (ret < 0) { 299833d4668SAlexey Kardashevskiy return ret; 300833d4668SAlexey Kardashevskiy } 30153018216SPaolo Bonzini } 30253018216SPaolo Bonzini return ret; 30353018216SPaolo Bonzini } 30453018216SPaolo Bonzini 30553018216SPaolo Bonzini 30653018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop, 30753018216SPaolo Bonzini size_t maxsize) 30853018216SPaolo Bonzini { 30953018216SPaolo Bonzini size_t maxcells = maxsize / sizeof(uint32_t); 31053018216SPaolo Bonzini int i, j, count; 31153018216SPaolo Bonzini uint32_t *p = prop; 31253018216SPaolo Bonzini 31353018216SPaolo Bonzini for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 31453018216SPaolo Bonzini struct ppc_one_seg_page_size *sps = &env->sps.sps[i]; 31553018216SPaolo Bonzini 31653018216SPaolo Bonzini if (!sps->page_shift) { 31753018216SPaolo Bonzini break; 31853018216SPaolo Bonzini } 31953018216SPaolo Bonzini for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) { 32053018216SPaolo Bonzini if (sps->enc[count].page_shift == 0) { 32153018216SPaolo Bonzini break; 32253018216SPaolo Bonzini } 32353018216SPaolo Bonzini } 32453018216SPaolo Bonzini if ((p - prop) >= (maxcells - 3 - count * 2)) { 32553018216SPaolo Bonzini break; 32653018216SPaolo Bonzini } 32753018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->page_shift); 32853018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->slb_enc); 32953018216SPaolo Bonzini *(p++) = cpu_to_be32(count); 33053018216SPaolo Bonzini for (j = 0; j < count; j++) { 33153018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->enc[j].page_shift); 33253018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->enc[j].pte_enc); 33353018216SPaolo Bonzini } 33453018216SPaolo Bonzini } 33553018216SPaolo Bonzini 33653018216SPaolo Bonzini return (p - prop) * sizeof(uint32_t); 33753018216SPaolo Bonzini } 33853018216SPaolo Bonzini 33953018216SPaolo Bonzini #define _FDT(exp) \ 34053018216SPaolo Bonzini do { \ 34153018216SPaolo Bonzini int ret = (exp); \ 34253018216SPaolo Bonzini if (ret < 0) { \ 34353018216SPaolo Bonzini fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \ 34453018216SPaolo Bonzini #exp, fdt_strerror(ret)); \ 34553018216SPaolo Bonzini exit(1); \ 34653018216SPaolo Bonzini } \ 34753018216SPaolo Bonzini } while (0) 34853018216SPaolo Bonzini 349a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1) 350a1d59c0fSAlexey Kardashevskiy { 351a1d59c0fSAlexey Kardashevskiy g_string_append_len(s, s1, strlen(s1) + 1); 352a1d59c0fSAlexey Kardashevskiy } 35353018216SPaolo Bonzini 3543bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base, 35553018216SPaolo Bonzini hwaddr initrd_size, 35653018216SPaolo Bonzini hwaddr kernel_size, 35716457e7fSBenjamin Herrenschmidt bool little_endian, 35853018216SPaolo Bonzini const char *boot_device, 35953018216SPaolo Bonzini const char *kernel_cmdline, 36053018216SPaolo Bonzini uint32_t epow_irq) 36153018216SPaolo Bonzini { 36253018216SPaolo Bonzini void *fdt; 363182735efSAndreas Färber CPUState *cs; 36453018216SPaolo Bonzini uint32_t start_prop = cpu_to_be32(initrd_base); 36553018216SPaolo Bonzini uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size); 366a1d59c0fSAlexey Kardashevskiy GString *hypertas = g_string_sized_new(256); 367a1d59c0fSAlexey Kardashevskiy GString *qemu_hypertas = g_string_sized_new(256); 36853018216SPaolo Bonzini uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)}; 36953018216SPaolo Bonzini uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)}; 370833d4668SAlexey Kardashevskiy int smt = kvmppc_smt_threads(); 37153018216SPaolo Bonzini unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80}; 37210582ff8SAlexey Kardashevskiy QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL); 37310582ff8SAlexey Kardashevskiy unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0; 37410582ff8SAlexey Kardashevskiy uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1; 37553018216SPaolo Bonzini 376a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-pft"); 377a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-term"); 378a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-dabr"); 379a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-interrupt"); 380a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-tce"); 381a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-vio"); 382a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-splpar"); 383a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-bulk"); 384a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-set-mode"); 385a1d59c0fSAlexey Kardashevskiy add_str(qemu_hypertas, "hcall-memop1"); 386a1d59c0fSAlexey Kardashevskiy 38753018216SPaolo Bonzini fdt = g_malloc0(FDT_MAX_SIZE); 38853018216SPaolo Bonzini _FDT((fdt_create(fdt, FDT_MAX_SIZE))); 38953018216SPaolo Bonzini 39053018216SPaolo Bonzini if (kernel_size) { 39153018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size))); 39253018216SPaolo Bonzini } 39353018216SPaolo Bonzini if (initrd_size) { 39453018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size))); 39553018216SPaolo Bonzini } 39653018216SPaolo Bonzini _FDT((fdt_finish_reservemap(fdt))); 39753018216SPaolo Bonzini 39853018216SPaolo Bonzini /* Root node */ 39953018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, ""))); 40053018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "chrp"))); 40153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)"))); 402fa388916SAnthony Liguori _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries"))); 40353018216SPaolo Bonzini 40453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x2))); 40553018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x2))); 40653018216SPaolo Bonzini 40753018216SPaolo Bonzini /* /chosen */ 40853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "chosen"))); 40953018216SPaolo Bonzini 41053018216SPaolo Bonzini /* Set Form1_affinity */ 41153018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5)))); 41253018216SPaolo Bonzini 41353018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline))); 41453018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-start", 41553018216SPaolo Bonzini &start_prop, sizeof(start_prop)))); 41653018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-end", 41753018216SPaolo Bonzini &end_prop, sizeof(end_prop)))); 41853018216SPaolo Bonzini if (kernel_size) { 41953018216SPaolo Bonzini uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR), 42053018216SPaolo Bonzini cpu_to_be64(kernel_size) }; 42153018216SPaolo Bonzini 42253018216SPaolo Bonzini _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop)))); 42316457e7fSBenjamin Herrenschmidt if (little_endian) { 42416457e7fSBenjamin Herrenschmidt _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0))); 42516457e7fSBenjamin Herrenschmidt } 42653018216SPaolo Bonzini } 42753018216SPaolo Bonzini if (boot_device) { 42853018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device))); 42953018216SPaolo Bonzini } 43053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width))); 43153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height))); 43253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth))); 43353018216SPaolo Bonzini 43453018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 43553018216SPaolo Bonzini 43653018216SPaolo Bonzini /* cpus */ 43753018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "cpus"))); 43853018216SPaolo Bonzini 43953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 44053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 44153018216SPaolo Bonzini 442bdc44640SAndreas Färber CPU_FOREACH(cs) { 443182735efSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 444182735efSAndreas Färber CPUPPCState *env = &cpu->env; 4453bbf37f2SAndreas Färber DeviceClass *dc = DEVICE_GET_CLASS(cs); 446182735efSAndreas Färber PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs); 4470f20ba62SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 44853018216SPaolo Bonzini char *nodename; 44953018216SPaolo Bonzini uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40), 45053018216SPaolo Bonzini 0xffffffff, 0xffffffff}; 45153018216SPaolo Bonzini uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ; 45253018216SPaolo Bonzini uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000; 45353018216SPaolo Bonzini uint32_t page_sizes_prop[64]; 45453018216SPaolo Bonzini size_t page_sizes_prop_size; 45553018216SPaolo Bonzini 45653018216SPaolo Bonzini if ((index % smt) != 0) { 45753018216SPaolo Bonzini continue; 45853018216SPaolo Bonzini } 45953018216SPaolo Bonzini 4603bbf37f2SAndreas Färber nodename = g_strdup_printf("%s@%x", dc->fw_name, index); 46153018216SPaolo Bonzini 46253018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, nodename))); 46353018216SPaolo Bonzini 46453018216SPaolo Bonzini g_free(nodename); 46553018216SPaolo Bonzini 46653018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "reg", index))); 46753018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "cpu"))); 46853018216SPaolo Bonzini 46953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR]))); 4700cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-block-size", 47153018216SPaolo Bonzini env->dcache_line_size))); 4720cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-line-size", 4730cbad81fSDavid Gibson env->dcache_line_size))); 4740cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-block-size", 47553018216SPaolo Bonzini env->icache_line_size))); 4760cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-line-size", 4770cbad81fSDavid Gibson env->icache_line_size))); 4780cbad81fSDavid Gibson 4790cbad81fSDavid Gibson if (pcc->l1_dcache_size) { 4800cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size))); 4810cbad81fSDavid Gibson } else { 4820cbad81fSDavid Gibson fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n"); 4830cbad81fSDavid Gibson } 4840cbad81fSDavid Gibson if (pcc->l1_icache_size) { 4850cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size))); 4860cbad81fSDavid Gibson } else { 4870cbad81fSDavid Gibson fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n"); 4880cbad81fSDavid Gibson } 4890cbad81fSDavid Gibson 49053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq))); 49153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq))); 49253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr))); 49353018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "status", "okay"))); 49453018216SPaolo Bonzini _FDT((fdt_property(fdt, "64-bit", NULL, 0))); 49553018216SPaolo Bonzini 496dcb861cbSAlexey Kardashevskiy if (env->spr_cb[SPR_PURR].oea_read) { 497dcb861cbSAlexey Kardashevskiy _FDT((fdt_property(fdt, "ibm,purr", NULL, 0))); 498dcb861cbSAlexey Kardashevskiy } 499dcb861cbSAlexey Kardashevskiy 50053018216SPaolo Bonzini if (env->mmu_model & POWERPC_MMU_1TSEG) { 50153018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,processor-segment-sizes", 50253018216SPaolo Bonzini segs, sizeof(segs)))); 50353018216SPaolo Bonzini } 50453018216SPaolo Bonzini 50553018216SPaolo Bonzini /* Advertise VMX/VSX (vector extensions) if available 50653018216SPaolo Bonzini * 0 / no property == no vector extensions 50753018216SPaolo Bonzini * 1 == VMX / Altivec available 50853018216SPaolo Bonzini * 2 == VSX available */ 50953018216SPaolo Bonzini if (env->insns_flags & PPC_ALTIVEC) { 51053018216SPaolo Bonzini uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1; 51153018216SPaolo Bonzini 51253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx))); 51353018216SPaolo Bonzini } 51453018216SPaolo Bonzini 51553018216SPaolo Bonzini /* Advertise DFP (Decimal Floating Point) if available 51653018216SPaolo Bonzini * 0 / no property == no DFP 51753018216SPaolo Bonzini * 1 == DFP available */ 51853018216SPaolo Bonzini if (env->insns_flags2 & PPC2_DFP) { 51953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,dfp", 1))); 52053018216SPaolo Bonzini } 52153018216SPaolo Bonzini 52253018216SPaolo Bonzini page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop, 52353018216SPaolo Bonzini sizeof(page_sizes_prop)); 52453018216SPaolo Bonzini if (page_sizes_prop_size) { 52553018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,segment-page-sizes", 52653018216SPaolo Bonzini page_sizes_prop, page_sizes_prop_size))); 52753018216SPaolo Bonzini } 52853018216SPaolo Bonzini 52910582ff8SAlexey Kardashevskiy _FDT((fdt_property_cell(fdt, "ibm,chip-id", 53010582ff8SAlexey Kardashevskiy cs->cpu_index / cpus_per_socket))); 53110582ff8SAlexey Kardashevskiy 53253018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 53353018216SPaolo Bonzini } 53453018216SPaolo Bonzini 53553018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 53653018216SPaolo Bonzini 53753018216SPaolo Bonzini /* RTAS */ 53853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "rtas"))); 53953018216SPaolo Bonzini 540da95324eSAlexey Kardashevskiy if (!kvm_enabled() || kvmppc_spapr_use_multitce()) { 541da95324eSAlexey Kardashevskiy add_str(hypertas, "hcall-multi-tce"); 542da95324eSAlexey Kardashevskiy } 543a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str, 544a1d59c0fSAlexey Kardashevskiy hypertas->len))); 545a1d59c0fSAlexey Kardashevskiy g_string_free(hypertas, TRUE); 546a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str, 547a1d59c0fSAlexey Kardashevskiy qemu_hypertas->len))); 548a1d59c0fSAlexey Kardashevskiy g_string_free(qemu_hypertas, TRUE); 54953018216SPaolo Bonzini 55053018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,associativity-reference-points", 55153018216SPaolo Bonzini refpoints, sizeof(refpoints)))); 55253018216SPaolo Bonzini 55353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX))); 55453018216SPaolo Bonzini 55553018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 55653018216SPaolo Bonzini 55753018216SPaolo Bonzini /* interrupt controller */ 55853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "interrupt-controller"))); 55953018216SPaolo Bonzini 56053018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", 56153018216SPaolo Bonzini "PowerPC-External-Interrupt-Presentation"))); 56253018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp"))); 56353018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 56453018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges", 56553018216SPaolo Bonzini interrupt_server_ranges_prop, 56653018216SPaolo Bonzini sizeof(interrupt_server_ranges_prop)))); 56753018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2))); 56853018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP))); 56953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP))); 57053018216SPaolo Bonzini 57153018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 57253018216SPaolo Bonzini 57353018216SPaolo Bonzini /* vdevice */ 57453018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "vdevice"))); 57553018216SPaolo Bonzini 57653018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "vdevice"))); 57753018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice"))); 57853018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 57953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 58053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2))); 58153018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 58253018216SPaolo Bonzini 58353018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 58453018216SPaolo Bonzini 58553018216SPaolo Bonzini /* event-sources */ 58653018216SPaolo Bonzini spapr_events_fdt_skel(fdt, epow_irq); 58753018216SPaolo Bonzini 588*f7d69146SAlexander Graf /* /hypervisor node */ 589*f7d69146SAlexander Graf if (kvm_enabled()) { 590*f7d69146SAlexander Graf uint8_t hypercall[16]; 591*f7d69146SAlexander Graf 592*f7d69146SAlexander Graf /* indicate KVM hypercall interface */ 593*f7d69146SAlexander Graf _FDT((fdt_begin_node(fdt, "hypervisor"))); 594*f7d69146SAlexander Graf _FDT((fdt_property_string(fdt, "compatible", "linux,kvm"))); 595*f7d69146SAlexander Graf if (kvmppc_has_cap_fixup_hcalls()) { 596*f7d69146SAlexander Graf /* 597*f7d69146SAlexander Graf * Older KVM versions with older guest kernels were broken with the 598*f7d69146SAlexander Graf * magic page, don't allow the guest to map it. 599*f7d69146SAlexander Graf */ 600*f7d69146SAlexander Graf kvmppc_get_hypercall(first_cpu->env_ptr, hypercall, 601*f7d69146SAlexander Graf sizeof(hypercall)); 602*f7d69146SAlexander Graf _FDT((fdt_property(fdt, "hcall-instructions", hypercall, 603*f7d69146SAlexander Graf sizeof(hypercall)))); 604*f7d69146SAlexander Graf } 605*f7d69146SAlexander Graf _FDT((fdt_end_node(fdt))); 606*f7d69146SAlexander Graf } 607*f7d69146SAlexander Graf 60853018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); /* close root node */ 60953018216SPaolo Bonzini _FDT((fdt_finish(fdt))); 61053018216SPaolo Bonzini 61153018216SPaolo Bonzini return fdt; 61253018216SPaolo Bonzini } 61353018216SPaolo Bonzini 6142a6593cbSAlexey Kardashevskiy int spapr_h_cas_compose_response(target_ulong addr, target_ulong size) 6152a6593cbSAlexey Kardashevskiy { 6162a6593cbSAlexey Kardashevskiy void *fdt, *fdt_skel; 6172a6593cbSAlexey Kardashevskiy sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 }; 6182a6593cbSAlexey Kardashevskiy 6192a6593cbSAlexey Kardashevskiy size -= sizeof(hdr); 6202a6593cbSAlexey Kardashevskiy 6212a6593cbSAlexey Kardashevskiy /* Create sceleton */ 6222a6593cbSAlexey Kardashevskiy fdt_skel = g_malloc0(size); 6232a6593cbSAlexey Kardashevskiy _FDT((fdt_create(fdt_skel, size))); 6242a6593cbSAlexey Kardashevskiy _FDT((fdt_begin_node(fdt_skel, ""))); 6252a6593cbSAlexey Kardashevskiy _FDT((fdt_end_node(fdt_skel))); 6262a6593cbSAlexey Kardashevskiy _FDT((fdt_finish(fdt_skel))); 6272a6593cbSAlexey Kardashevskiy fdt = g_malloc0(size); 6282a6593cbSAlexey Kardashevskiy _FDT((fdt_open_into(fdt_skel, fdt, size))); 6292a6593cbSAlexey Kardashevskiy g_free(fdt_skel); 6302a6593cbSAlexey Kardashevskiy 6313794d548SAlexey Kardashevskiy /* Fix skeleton up */ 6323794d548SAlexey Kardashevskiy _FDT((spapr_fixup_cpu_dt(fdt, spapr))); 6332a6593cbSAlexey Kardashevskiy 6342a6593cbSAlexey Kardashevskiy /* Pack resulting tree */ 6352a6593cbSAlexey Kardashevskiy _FDT((fdt_pack(fdt))); 6362a6593cbSAlexey Kardashevskiy 6372a6593cbSAlexey Kardashevskiy if (fdt_totalsize(fdt) + sizeof(hdr) > size) { 6382a6593cbSAlexey Kardashevskiy trace_spapr_cas_failed(size); 6392a6593cbSAlexey Kardashevskiy return -1; 6402a6593cbSAlexey Kardashevskiy } 6412a6593cbSAlexey Kardashevskiy 6422a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr, &hdr, sizeof(hdr)); 6432a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt)); 6442a6593cbSAlexey Kardashevskiy trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr)); 6452a6593cbSAlexey Kardashevskiy g_free(fdt); 6462a6593cbSAlexey Kardashevskiy 6472a6593cbSAlexey Kardashevskiy return 0; 6482a6593cbSAlexey Kardashevskiy } 6492a6593cbSAlexey Kardashevskiy 65053018216SPaolo Bonzini static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt) 65153018216SPaolo Bonzini { 65253018216SPaolo Bonzini uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0), 65353018216SPaolo Bonzini cpu_to_be32(0x0), cpu_to_be32(0x0), 65453018216SPaolo Bonzini cpu_to_be32(0x0)}; 65553018216SPaolo Bonzini char mem_name[32]; 6565fe269b1SPaul Mackerras hwaddr node0_size, mem_start, node_size; 65753018216SPaolo Bonzini uint64_t mem_reg_property[2]; 65853018216SPaolo Bonzini int i, off; 65953018216SPaolo Bonzini 66053018216SPaolo Bonzini /* memory node(s) */ 6615fe269b1SPaul Mackerras if (nb_numa_nodes > 1 && node_mem[0] < ram_size) { 6625fe269b1SPaul Mackerras node0_size = node_mem[0]; 6635fe269b1SPaul Mackerras } else { 6645fe269b1SPaul Mackerras node0_size = ram_size; 6655fe269b1SPaul Mackerras } 66653018216SPaolo Bonzini 66753018216SPaolo Bonzini /* RMA */ 66853018216SPaolo Bonzini mem_reg_property[0] = 0; 66953018216SPaolo Bonzini mem_reg_property[1] = cpu_to_be64(spapr->rma_size); 67053018216SPaolo Bonzini off = fdt_add_subnode(fdt, 0, "memory@0"); 67153018216SPaolo Bonzini _FDT(off); 67253018216SPaolo Bonzini _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 67353018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 67453018216SPaolo Bonzini sizeof(mem_reg_property)))); 67553018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 67653018216SPaolo Bonzini sizeof(associativity)))); 67753018216SPaolo Bonzini 67853018216SPaolo Bonzini /* RAM: Node 0 */ 67953018216SPaolo Bonzini if (node0_size > spapr->rma_size) { 68053018216SPaolo Bonzini mem_reg_property[0] = cpu_to_be64(spapr->rma_size); 68153018216SPaolo Bonzini mem_reg_property[1] = cpu_to_be64(node0_size - spapr->rma_size); 68253018216SPaolo Bonzini 68353018216SPaolo Bonzini sprintf(mem_name, "memory@" TARGET_FMT_lx, spapr->rma_size); 68453018216SPaolo Bonzini off = fdt_add_subnode(fdt, 0, mem_name); 68553018216SPaolo Bonzini _FDT(off); 68653018216SPaolo Bonzini _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 68753018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 68853018216SPaolo Bonzini sizeof(mem_reg_property)))); 68953018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 69053018216SPaolo Bonzini sizeof(associativity)))); 69153018216SPaolo Bonzini } 69253018216SPaolo Bonzini 69353018216SPaolo Bonzini /* RAM: Node 1 and beyond */ 69453018216SPaolo Bonzini mem_start = node0_size; 69553018216SPaolo Bonzini for (i = 1; i < nb_numa_nodes; i++) { 69653018216SPaolo Bonzini mem_reg_property[0] = cpu_to_be64(mem_start); 6975fe269b1SPaul Mackerras if (mem_start >= ram_size) { 6985fe269b1SPaul Mackerras node_size = 0; 6995fe269b1SPaul Mackerras } else { 7005fe269b1SPaul Mackerras node_size = node_mem[i]; 7015fe269b1SPaul Mackerras if (node_size > ram_size - mem_start) { 7025fe269b1SPaul Mackerras node_size = ram_size - mem_start; 7035fe269b1SPaul Mackerras } 7045fe269b1SPaul Mackerras } 7055fe269b1SPaul Mackerras mem_reg_property[1] = cpu_to_be64(node_size); 70653018216SPaolo Bonzini associativity[3] = associativity[4] = cpu_to_be32(i); 70753018216SPaolo Bonzini sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start); 70853018216SPaolo Bonzini off = fdt_add_subnode(fdt, 0, mem_name); 70953018216SPaolo Bonzini _FDT(off); 71053018216SPaolo Bonzini _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 71153018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 71253018216SPaolo Bonzini sizeof(mem_reg_property)))); 71353018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 71453018216SPaolo Bonzini sizeof(associativity)))); 7155fe269b1SPaul Mackerras mem_start += node_size; 71653018216SPaolo Bonzini } 71753018216SPaolo Bonzini 71853018216SPaolo Bonzini return 0; 71953018216SPaolo Bonzini } 72053018216SPaolo Bonzini 72153018216SPaolo Bonzini static void spapr_finalize_fdt(sPAPREnvironment *spapr, 72253018216SPaolo Bonzini hwaddr fdt_addr, 72353018216SPaolo Bonzini hwaddr rtas_addr, 72453018216SPaolo Bonzini hwaddr rtas_size) 72553018216SPaolo Bonzini { 72671461b0fSAlexey Kardashevskiy int ret, i; 72771461b0fSAlexey Kardashevskiy size_t cb = 0; 72871461b0fSAlexey Kardashevskiy char *bootlist; 72953018216SPaolo Bonzini void *fdt; 73053018216SPaolo Bonzini sPAPRPHBState *phb; 73153018216SPaolo Bonzini 73253018216SPaolo Bonzini fdt = g_malloc(FDT_MAX_SIZE); 73353018216SPaolo Bonzini 73453018216SPaolo Bonzini /* open out the base tree into a temp buffer for the final tweaks */ 73553018216SPaolo Bonzini _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE))); 73653018216SPaolo Bonzini 73753018216SPaolo Bonzini ret = spapr_populate_memory(spapr, fdt); 73853018216SPaolo Bonzini if (ret < 0) { 73953018216SPaolo Bonzini fprintf(stderr, "couldn't setup memory nodes in fdt\n"); 74053018216SPaolo Bonzini exit(1); 74153018216SPaolo Bonzini } 74253018216SPaolo Bonzini 74353018216SPaolo Bonzini ret = spapr_populate_vdevice(spapr->vio_bus, fdt); 74453018216SPaolo Bonzini if (ret < 0) { 74553018216SPaolo Bonzini fprintf(stderr, "couldn't setup vio devices in fdt\n"); 74653018216SPaolo Bonzini exit(1); 74753018216SPaolo Bonzini } 74853018216SPaolo Bonzini 74953018216SPaolo Bonzini QLIST_FOREACH(phb, &spapr->phbs, list) { 75053018216SPaolo Bonzini ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt); 75153018216SPaolo Bonzini } 75253018216SPaolo Bonzini 75353018216SPaolo Bonzini if (ret < 0) { 75453018216SPaolo Bonzini fprintf(stderr, "couldn't setup PCI devices in fdt\n"); 75553018216SPaolo Bonzini exit(1); 75653018216SPaolo Bonzini } 75753018216SPaolo Bonzini 75853018216SPaolo Bonzini /* RTAS */ 75953018216SPaolo Bonzini ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size); 76053018216SPaolo Bonzini if (ret < 0) { 76153018216SPaolo Bonzini fprintf(stderr, "Couldn't set up RTAS device tree properties\n"); 76253018216SPaolo Bonzini } 76353018216SPaolo Bonzini 76453018216SPaolo Bonzini /* Advertise NUMA via ibm,associativity */ 76553018216SPaolo Bonzini ret = spapr_fixup_cpu_dt(fdt, spapr); 76653018216SPaolo Bonzini if (ret < 0) { 76753018216SPaolo Bonzini fprintf(stderr, "Couldn't finalize CPU device tree properties\n"); 76853018216SPaolo Bonzini } 76953018216SPaolo Bonzini 77071461b0fSAlexey Kardashevskiy bootlist = get_boot_devices_list(&cb, true); 77171461b0fSAlexey Kardashevskiy if (cb && bootlist) { 77271461b0fSAlexey Kardashevskiy int offset = fdt_path_offset(fdt, "/chosen"); 77371461b0fSAlexey Kardashevskiy if (offset < 0) { 77471461b0fSAlexey Kardashevskiy exit(1); 77571461b0fSAlexey Kardashevskiy } 77671461b0fSAlexey Kardashevskiy for (i = 0; i < cb; i++) { 77771461b0fSAlexey Kardashevskiy if (bootlist[i] == '\n') { 77871461b0fSAlexey Kardashevskiy bootlist[i] = ' '; 77971461b0fSAlexey Kardashevskiy } 78071461b0fSAlexey Kardashevskiy 78171461b0fSAlexey Kardashevskiy } 78271461b0fSAlexey Kardashevskiy ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist); 78371461b0fSAlexey Kardashevskiy } 78471461b0fSAlexey Kardashevskiy 78553018216SPaolo Bonzini if (!spapr->has_graphics) { 78653018216SPaolo Bonzini spapr_populate_chosen_stdout(fdt, spapr->vio_bus); 78753018216SPaolo Bonzini } 78853018216SPaolo Bonzini 78953018216SPaolo Bonzini _FDT((fdt_pack(fdt))); 79053018216SPaolo Bonzini 79153018216SPaolo Bonzini if (fdt_totalsize(fdt) > FDT_MAX_SIZE) { 79253018216SPaolo Bonzini hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n", 79353018216SPaolo Bonzini fdt_totalsize(fdt), FDT_MAX_SIZE); 79453018216SPaolo Bonzini exit(1); 79553018216SPaolo Bonzini } 79653018216SPaolo Bonzini 79753018216SPaolo Bonzini cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt)); 79853018216SPaolo Bonzini 79953018216SPaolo Bonzini g_free(fdt); 80053018216SPaolo Bonzini } 80153018216SPaolo Bonzini 80253018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 80353018216SPaolo Bonzini { 80453018216SPaolo Bonzini return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR; 80553018216SPaolo Bonzini } 80653018216SPaolo Bonzini 80753018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu) 80853018216SPaolo Bonzini { 80953018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 81053018216SPaolo Bonzini 81153018216SPaolo Bonzini if (msr_pr) { 81253018216SPaolo Bonzini hcall_dprintf("Hypercall made with MSR[PR]=1\n"); 81353018216SPaolo Bonzini env->gpr[3] = H_PRIVILEGE; 81453018216SPaolo Bonzini } else { 81553018216SPaolo Bonzini env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]); 81653018216SPaolo Bonzini } 81753018216SPaolo Bonzini } 81853018216SPaolo Bonzini 81953018216SPaolo Bonzini static void spapr_reset_htab(sPAPREnvironment *spapr) 82053018216SPaolo Bonzini { 82153018216SPaolo Bonzini long shift; 82253018216SPaolo Bonzini 82353018216SPaolo Bonzini /* allocate hash page table. For now we always make this 16mb, 82453018216SPaolo Bonzini * later we should probably make it scale to the size of guest 82553018216SPaolo Bonzini * RAM */ 82653018216SPaolo Bonzini 82753018216SPaolo Bonzini shift = kvmppc_reset_htab(spapr->htab_shift); 82853018216SPaolo Bonzini 82953018216SPaolo Bonzini if (shift > 0) { 83053018216SPaolo Bonzini /* Kernel handles htab, we don't need to allocate one */ 83153018216SPaolo Bonzini spapr->htab_shift = shift; 8327c43bca0SAneesh Kumar K.V kvmppc_kern_htab = true; 83353018216SPaolo Bonzini } else { 83453018216SPaolo Bonzini if (!spapr->htab) { 83553018216SPaolo Bonzini /* Allocate an htab if we don't yet have one */ 83653018216SPaolo Bonzini spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr)); 83753018216SPaolo Bonzini } 83853018216SPaolo Bonzini 83953018216SPaolo Bonzini /* And clear it */ 84053018216SPaolo Bonzini memset(spapr->htab, 0, HTAB_SIZE(spapr)); 84153018216SPaolo Bonzini } 84253018216SPaolo Bonzini 84353018216SPaolo Bonzini /* Update the RMA size if necessary */ 84453018216SPaolo Bonzini if (spapr->vrma_adjust) { 845c4177479SAlexey Kardashevskiy hwaddr node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size; 846c4177479SAlexey Kardashevskiy spapr->rma_size = kvmppc_rma_size(node0_size, spapr->htab_shift); 84753018216SPaolo Bonzini } 84853018216SPaolo Bonzini } 84953018216SPaolo Bonzini 85053018216SPaolo Bonzini static void ppc_spapr_reset(void) 85153018216SPaolo Bonzini { 852182735efSAndreas Färber PowerPCCPU *first_ppc_cpu; 853259186a7SAndreas Färber 85453018216SPaolo Bonzini /* Reset the hash table & recalc the RMA */ 85553018216SPaolo Bonzini spapr_reset_htab(spapr); 85653018216SPaolo Bonzini 85753018216SPaolo Bonzini qemu_devices_reset(); 85853018216SPaolo Bonzini 85953018216SPaolo Bonzini /* Load the fdt */ 86053018216SPaolo Bonzini spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr, 86153018216SPaolo Bonzini spapr->rtas_size); 86253018216SPaolo Bonzini 86353018216SPaolo Bonzini /* Set up the entry state */ 864182735efSAndreas Färber first_ppc_cpu = POWERPC_CPU(first_cpu); 865182735efSAndreas Färber first_ppc_cpu->env.gpr[3] = spapr->fdt_addr; 866182735efSAndreas Färber first_ppc_cpu->env.gpr[5] = 0; 867182735efSAndreas Färber first_cpu->halted = 0; 868182735efSAndreas Färber first_ppc_cpu->env.nip = spapr->entry_point; 86953018216SPaolo Bonzini 87053018216SPaolo Bonzini } 87153018216SPaolo Bonzini 87253018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque) 87353018216SPaolo Bonzini { 87453018216SPaolo Bonzini PowerPCCPU *cpu = opaque; 875259186a7SAndreas Färber CPUState *cs = CPU(cpu); 87653018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 87753018216SPaolo Bonzini 878259186a7SAndreas Färber cpu_reset(cs); 87953018216SPaolo Bonzini 88053018216SPaolo Bonzini /* All CPUs start halted. CPU0 is unhalted from the machine level 88153018216SPaolo Bonzini * reset code and the rest are explicitly started up by the guest 88253018216SPaolo Bonzini * using an RTAS call */ 883259186a7SAndreas Färber cs->halted = 1; 88453018216SPaolo Bonzini 88553018216SPaolo Bonzini env->spr[SPR_HIOR] = 0; 88653018216SPaolo Bonzini 8874be21d56SDavid Gibson env->external_htab = (uint8_t *)spapr->htab; 8885736245cSAneesh Kumar K.V if (kvm_enabled() && !env->external_htab) { 8895736245cSAneesh Kumar K.V /* 8905736245cSAneesh Kumar K.V * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte* 8915736245cSAneesh Kumar K.V * functions do the right thing. 8925736245cSAneesh Kumar K.V */ 8935736245cSAneesh Kumar K.V env->external_htab = (void *)1; 8945736245cSAneesh Kumar K.V } 89553018216SPaolo Bonzini env->htab_base = -1; 896f3c75d42SAneesh Kumar K.V /* 897f3c75d42SAneesh Kumar K.V * htab_mask is the mask used to normalize hash value to PTEG index. 898f3c75d42SAneesh Kumar K.V * htab_shift is log2 of hash table size. 899f3c75d42SAneesh Kumar K.V * We have 8 hpte per group, and each hpte is 16 bytes. 900f3c75d42SAneesh Kumar K.V * ie have 128 bytes per hpte entry. 901f3c75d42SAneesh Kumar K.V */ 902f3c75d42SAneesh Kumar K.V env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1; 903ec4936e1SStefan Weil env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab | 90453018216SPaolo Bonzini (spapr->htab_shift - 18); 90553018216SPaolo Bonzini } 90653018216SPaolo Bonzini 90753018216SPaolo Bonzini static void spapr_create_nvram(sPAPREnvironment *spapr) 90853018216SPaolo Bonzini { 9092ff3de68SMarkus Armbruster DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram"); 9103978b863SPaolo Bonzini DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0); 91153018216SPaolo Bonzini 9123978b863SPaolo Bonzini if (dinfo) { 9133978b863SPaolo Bonzini qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv); 91453018216SPaolo Bonzini } 91553018216SPaolo Bonzini 91653018216SPaolo Bonzini qdev_init_nofail(dev); 91753018216SPaolo Bonzini 91853018216SPaolo Bonzini spapr->nvram = (struct sPAPRNVRAM *)dev; 91953018216SPaolo Bonzini } 92053018216SPaolo Bonzini 92153018216SPaolo Bonzini /* Returns whether we want to use VGA or not */ 92253018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus) 92353018216SPaolo Bonzini { 92453018216SPaolo Bonzini switch (vga_interface_type) { 92553018216SPaolo Bonzini case VGA_NONE: 9267effdaa3SMark Wu return false; 9277effdaa3SMark Wu case VGA_DEVICE: 9287effdaa3SMark Wu return true; 92953018216SPaolo Bonzini case VGA_STD: 93053018216SPaolo Bonzini return pci_vga_init(pci_bus) != NULL; 93153018216SPaolo Bonzini default: 93253018216SPaolo Bonzini fprintf(stderr, "This vga model is not supported," 93353018216SPaolo Bonzini "currently it only supports -vga std\n"); 93453018216SPaolo Bonzini exit(0); 93553018216SPaolo Bonzini } 93653018216SPaolo Bonzini } 93753018216SPaolo Bonzini 9384be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = { 9394be21d56SDavid Gibson .name = "spapr", 94098a8b524SAlexey Kardashevskiy .version_id = 2, 9414be21d56SDavid Gibson .minimum_version_id = 1, 9424be21d56SDavid Gibson .fields = (VMStateField[]) { 9434be21d56SDavid Gibson VMSTATE_UINT32(next_irq, sPAPREnvironment), 9444be21d56SDavid Gibson 9454be21d56SDavid Gibson /* RTC offset */ 9464be21d56SDavid Gibson VMSTATE_UINT64(rtc_offset, sPAPREnvironment), 94798a8b524SAlexey Kardashevskiy VMSTATE_PPC_TIMEBASE_V(tb, sPAPREnvironment, 2), 9484be21d56SDavid Gibson VMSTATE_END_OF_LIST() 9494be21d56SDavid Gibson }, 9504be21d56SDavid Gibson }; 9514be21d56SDavid Gibson 9524be21d56SDavid Gibson #define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2)) 9534be21d56SDavid Gibson #define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID) 9544be21d56SDavid Gibson #define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY) 9554be21d56SDavid Gibson #define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY)) 9564be21d56SDavid Gibson 9574be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque) 9584be21d56SDavid Gibson { 9594be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 9604be21d56SDavid Gibson 9614be21d56SDavid Gibson /* "Iteration" header */ 9624be21d56SDavid Gibson qemu_put_be32(f, spapr->htab_shift); 9634be21d56SDavid Gibson 964e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 965e68cb8b4SAlexey Kardashevskiy spapr->htab_save_index = 0; 966e68cb8b4SAlexey Kardashevskiy spapr->htab_first_pass = true; 967e68cb8b4SAlexey Kardashevskiy } else { 968e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 969e68cb8b4SAlexey Kardashevskiy 970e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = kvmppc_get_htab_fd(false); 971e68cb8b4SAlexey Kardashevskiy if (spapr->htab_fd < 0) { 972e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n", 973e68cb8b4SAlexey Kardashevskiy strerror(errno)); 974e68cb8b4SAlexey Kardashevskiy return -1; 975e68cb8b4SAlexey Kardashevskiy } 9764be21d56SDavid Gibson } 9774be21d56SDavid Gibson 978e68cb8b4SAlexey Kardashevskiy 979e68cb8b4SAlexey Kardashevskiy return 0; 980e68cb8b4SAlexey Kardashevskiy } 9814be21d56SDavid Gibson 9824be21d56SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr, 9834be21d56SDavid Gibson int64_t max_ns) 9844be21d56SDavid Gibson { 9854be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 9864be21d56SDavid Gibson int index = spapr->htab_save_index; 987bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 9884be21d56SDavid Gibson 9894be21d56SDavid Gibson assert(spapr->htab_first_pass); 9904be21d56SDavid Gibson 9914be21d56SDavid Gibson do { 9924be21d56SDavid Gibson int chunkstart; 9934be21d56SDavid Gibson 9944be21d56SDavid Gibson /* Consume invalid HPTEs */ 9954be21d56SDavid Gibson while ((index < htabslots) 9964be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 9974be21d56SDavid Gibson index++; 9984be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 9994be21d56SDavid Gibson } 10004be21d56SDavid Gibson 10014be21d56SDavid Gibson /* Consume valid HPTEs */ 10024be21d56SDavid Gibson chunkstart = index; 10034be21d56SDavid Gibson while ((index < htabslots) 10044be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 10054be21d56SDavid Gibson index++; 10064be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10074be21d56SDavid Gibson } 10084be21d56SDavid Gibson 10094be21d56SDavid Gibson if (index > chunkstart) { 10104be21d56SDavid Gibson int n_valid = index - chunkstart; 10114be21d56SDavid Gibson 10124be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 10134be21d56SDavid Gibson qemu_put_be16(f, n_valid); 10144be21d56SDavid Gibson qemu_put_be16(f, 0); 10154be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 10164be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 10174be21d56SDavid Gibson 1018bc72ad67SAlex Bligh if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 10194be21d56SDavid Gibson break; 10204be21d56SDavid Gibson } 10214be21d56SDavid Gibson } 10224be21d56SDavid Gibson } while ((index < htabslots) && !qemu_file_rate_limit(f)); 10234be21d56SDavid Gibson 10244be21d56SDavid Gibson if (index >= htabslots) { 10254be21d56SDavid Gibson assert(index == htabslots); 10264be21d56SDavid Gibson index = 0; 10274be21d56SDavid Gibson spapr->htab_first_pass = false; 10284be21d56SDavid Gibson } 10294be21d56SDavid Gibson spapr->htab_save_index = index; 10304be21d56SDavid Gibson } 10314be21d56SDavid Gibson 1032e68cb8b4SAlexey Kardashevskiy static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr, 10334be21d56SDavid Gibson int64_t max_ns) 10344be21d56SDavid Gibson { 10354be21d56SDavid Gibson bool final = max_ns < 0; 10364be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 10374be21d56SDavid Gibson int examined = 0, sent = 0; 10384be21d56SDavid Gibson int index = spapr->htab_save_index; 1039bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 10404be21d56SDavid Gibson 10414be21d56SDavid Gibson assert(!spapr->htab_first_pass); 10424be21d56SDavid Gibson 10434be21d56SDavid Gibson do { 10444be21d56SDavid Gibson int chunkstart, invalidstart; 10454be21d56SDavid Gibson 10464be21d56SDavid Gibson /* Consume non-dirty HPTEs */ 10474be21d56SDavid Gibson while ((index < htabslots) 10484be21d56SDavid Gibson && !HPTE_DIRTY(HPTE(spapr->htab, index))) { 10494be21d56SDavid Gibson index++; 10504be21d56SDavid Gibson examined++; 10514be21d56SDavid Gibson } 10524be21d56SDavid Gibson 10534be21d56SDavid Gibson chunkstart = index; 10544be21d56SDavid Gibson /* Consume valid dirty HPTEs */ 10554be21d56SDavid Gibson while ((index < htabslots) 10564be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 10574be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 10584be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10594be21d56SDavid Gibson index++; 10604be21d56SDavid Gibson examined++; 10614be21d56SDavid Gibson } 10624be21d56SDavid Gibson 10634be21d56SDavid Gibson invalidstart = index; 10644be21d56SDavid Gibson /* Consume invalid dirty HPTEs */ 10654be21d56SDavid Gibson while ((index < htabslots) 10664be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 10674be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 10684be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10694be21d56SDavid Gibson index++; 10704be21d56SDavid Gibson examined++; 10714be21d56SDavid Gibson } 10724be21d56SDavid Gibson 10734be21d56SDavid Gibson if (index > chunkstart) { 10744be21d56SDavid Gibson int n_valid = invalidstart - chunkstart; 10754be21d56SDavid Gibson int n_invalid = index - invalidstart; 10764be21d56SDavid Gibson 10774be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 10784be21d56SDavid Gibson qemu_put_be16(f, n_valid); 10794be21d56SDavid Gibson qemu_put_be16(f, n_invalid); 10804be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 10814be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 10824be21d56SDavid Gibson sent += index - chunkstart; 10834be21d56SDavid Gibson 1084bc72ad67SAlex Bligh if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 10854be21d56SDavid Gibson break; 10864be21d56SDavid Gibson } 10874be21d56SDavid Gibson } 10884be21d56SDavid Gibson 10894be21d56SDavid Gibson if (examined >= htabslots) { 10904be21d56SDavid Gibson break; 10914be21d56SDavid Gibson } 10924be21d56SDavid Gibson 10934be21d56SDavid Gibson if (index >= htabslots) { 10944be21d56SDavid Gibson assert(index == htabslots); 10954be21d56SDavid Gibson index = 0; 10964be21d56SDavid Gibson } 10974be21d56SDavid Gibson } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final)); 10984be21d56SDavid Gibson 10994be21d56SDavid Gibson if (index >= htabslots) { 11004be21d56SDavid Gibson assert(index == htabslots); 11014be21d56SDavid Gibson index = 0; 11024be21d56SDavid Gibson } 11034be21d56SDavid Gibson 11044be21d56SDavid Gibson spapr->htab_save_index = index; 11054be21d56SDavid Gibson 1106e68cb8b4SAlexey Kardashevskiy return (examined >= htabslots) && (sent == 0) ? 1 : 0; 11074be21d56SDavid Gibson } 11084be21d56SDavid Gibson 1109e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS 5000000 /* 5 ms */ 1110e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE 2048 1111e68cb8b4SAlexey Kardashevskiy 11124be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque) 11134be21d56SDavid Gibson { 11144be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 1115e68cb8b4SAlexey Kardashevskiy int rc = 0; 11164be21d56SDavid Gibson 11174be21d56SDavid Gibson /* Iteration header */ 11184be21d56SDavid Gibson qemu_put_be32(f, 0); 11194be21d56SDavid Gibson 1120e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1121e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1122e68cb8b4SAlexey Kardashevskiy 1123e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, 1124e68cb8b4SAlexey Kardashevskiy MAX_KVM_BUF_SIZE, MAX_ITERATION_NS); 1125e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1126e68cb8b4SAlexey Kardashevskiy return rc; 1127e68cb8b4SAlexey Kardashevskiy } 1128e68cb8b4SAlexey Kardashevskiy } else if (spapr->htab_first_pass) { 11294be21d56SDavid Gibson htab_save_first_pass(f, spapr, MAX_ITERATION_NS); 11304be21d56SDavid Gibson } else { 1131e68cb8b4SAlexey Kardashevskiy rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS); 11324be21d56SDavid Gibson } 11334be21d56SDavid Gibson 11344be21d56SDavid Gibson /* End marker */ 11354be21d56SDavid Gibson qemu_put_be32(f, 0); 11364be21d56SDavid Gibson qemu_put_be16(f, 0); 11374be21d56SDavid Gibson qemu_put_be16(f, 0); 11384be21d56SDavid Gibson 1139e68cb8b4SAlexey Kardashevskiy return rc; 11404be21d56SDavid Gibson } 11414be21d56SDavid Gibson 11424be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque) 11434be21d56SDavid Gibson { 11444be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 11454be21d56SDavid Gibson 11464be21d56SDavid Gibson /* Iteration header */ 11474be21d56SDavid Gibson qemu_put_be32(f, 0); 11484be21d56SDavid Gibson 1149e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1150e68cb8b4SAlexey Kardashevskiy int rc; 1151e68cb8b4SAlexey Kardashevskiy 1152e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1153e68cb8b4SAlexey Kardashevskiy 1154e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1); 1155e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1156e68cb8b4SAlexey Kardashevskiy return rc; 1157e68cb8b4SAlexey Kardashevskiy } 1158e68cb8b4SAlexey Kardashevskiy close(spapr->htab_fd); 1159e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = -1; 1160e68cb8b4SAlexey Kardashevskiy } else { 11614be21d56SDavid Gibson htab_save_later_pass(f, spapr, -1); 1162e68cb8b4SAlexey Kardashevskiy } 11634be21d56SDavid Gibson 11644be21d56SDavid Gibson /* End marker */ 11654be21d56SDavid Gibson qemu_put_be32(f, 0); 11664be21d56SDavid Gibson qemu_put_be16(f, 0); 11674be21d56SDavid Gibson qemu_put_be16(f, 0); 11684be21d56SDavid Gibson 11694be21d56SDavid Gibson return 0; 11704be21d56SDavid Gibson } 11714be21d56SDavid Gibson 11724be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id) 11734be21d56SDavid Gibson { 11744be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 11754be21d56SDavid Gibson uint32_t section_hdr; 1176e68cb8b4SAlexey Kardashevskiy int fd = -1; 11774be21d56SDavid Gibson 11784be21d56SDavid Gibson if (version_id < 1 || version_id > 1) { 11794be21d56SDavid Gibson fprintf(stderr, "htab_load() bad version\n"); 11804be21d56SDavid Gibson return -EINVAL; 11814be21d56SDavid Gibson } 11824be21d56SDavid Gibson 11834be21d56SDavid Gibson section_hdr = qemu_get_be32(f); 11844be21d56SDavid Gibson 11854be21d56SDavid Gibson if (section_hdr) { 11864be21d56SDavid Gibson /* First section, just the hash shift */ 11874be21d56SDavid Gibson if (spapr->htab_shift != section_hdr) { 11884be21d56SDavid Gibson return -EINVAL; 11894be21d56SDavid Gibson } 11904be21d56SDavid Gibson return 0; 11914be21d56SDavid Gibson } 11924be21d56SDavid Gibson 1193e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1194e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1195e68cb8b4SAlexey Kardashevskiy 1196e68cb8b4SAlexey Kardashevskiy fd = kvmppc_get_htab_fd(true); 1197e68cb8b4SAlexey Kardashevskiy if (fd < 0) { 1198e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n", 1199e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1200e68cb8b4SAlexey Kardashevskiy } 1201e68cb8b4SAlexey Kardashevskiy } 1202e68cb8b4SAlexey Kardashevskiy 12034be21d56SDavid Gibson while (true) { 12044be21d56SDavid Gibson uint32_t index; 12054be21d56SDavid Gibson uint16_t n_valid, n_invalid; 12064be21d56SDavid Gibson 12074be21d56SDavid Gibson index = qemu_get_be32(f); 12084be21d56SDavid Gibson n_valid = qemu_get_be16(f); 12094be21d56SDavid Gibson n_invalid = qemu_get_be16(f); 12104be21d56SDavid Gibson 12114be21d56SDavid Gibson if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) { 12124be21d56SDavid Gibson /* End of Stream */ 12134be21d56SDavid Gibson break; 12144be21d56SDavid Gibson } 12154be21d56SDavid Gibson 1216e68cb8b4SAlexey Kardashevskiy if ((index + n_valid + n_invalid) > 12174be21d56SDavid Gibson (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) { 12184be21d56SDavid Gibson /* Bad index in stream */ 12194be21d56SDavid Gibson fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) " 1220e68cb8b4SAlexey Kardashevskiy "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid, 1221e68cb8b4SAlexey Kardashevskiy spapr->htab_shift); 12224be21d56SDavid Gibson return -EINVAL; 12234be21d56SDavid Gibson } 12244be21d56SDavid Gibson 1225e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 12264be21d56SDavid Gibson if (n_valid) { 12274be21d56SDavid Gibson qemu_get_buffer(f, HPTE(spapr->htab, index), 12284be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 12294be21d56SDavid Gibson } 12304be21d56SDavid Gibson if (n_invalid) { 12314be21d56SDavid Gibson memset(HPTE(spapr->htab, index + n_valid), 0, 12324be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_invalid); 12334be21d56SDavid Gibson } 1234e68cb8b4SAlexey Kardashevskiy } else { 1235e68cb8b4SAlexey Kardashevskiy int rc; 1236e68cb8b4SAlexey Kardashevskiy 1237e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1238e68cb8b4SAlexey Kardashevskiy 1239e68cb8b4SAlexey Kardashevskiy rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid); 1240e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1241e68cb8b4SAlexey Kardashevskiy return rc; 1242e68cb8b4SAlexey Kardashevskiy } 1243e68cb8b4SAlexey Kardashevskiy } 1244e68cb8b4SAlexey Kardashevskiy } 1245e68cb8b4SAlexey Kardashevskiy 1246e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1247e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1248e68cb8b4SAlexey Kardashevskiy close(fd); 12494be21d56SDavid Gibson } 12504be21d56SDavid Gibson 12514be21d56SDavid Gibson return 0; 12524be21d56SDavid Gibson } 12534be21d56SDavid Gibson 12544be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = { 12554be21d56SDavid Gibson .save_live_setup = htab_save_setup, 12564be21d56SDavid Gibson .save_live_iterate = htab_save_iterate, 12574be21d56SDavid Gibson .save_live_complete = htab_save_complete, 12584be21d56SDavid Gibson .load_state = htab_load, 12594be21d56SDavid Gibson }; 12604be21d56SDavid Gibson 126153018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */ 12623ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine) 126353018216SPaolo Bonzini { 12643ef96221SMarcel Apfelbaum ram_addr_t ram_size = machine->ram_size; 12653ef96221SMarcel Apfelbaum const char *cpu_model = machine->cpu_model; 12663ef96221SMarcel Apfelbaum const char *kernel_filename = machine->kernel_filename; 12673ef96221SMarcel Apfelbaum const char *kernel_cmdline = machine->kernel_cmdline; 12683ef96221SMarcel Apfelbaum const char *initrd_filename = machine->initrd_filename; 12693ef96221SMarcel Apfelbaum const char *boot_device = machine->boot_order; 127053018216SPaolo Bonzini PowerPCCPU *cpu; 127153018216SPaolo Bonzini CPUPPCState *env; 127253018216SPaolo Bonzini PCIHostState *phb; 127353018216SPaolo Bonzini int i; 127453018216SPaolo Bonzini MemoryRegion *sysmem = get_system_memory(); 127553018216SPaolo Bonzini MemoryRegion *ram = g_new(MemoryRegion, 1); 127653018216SPaolo Bonzini hwaddr rma_alloc_size; 1277c4177479SAlexey Kardashevskiy hwaddr node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size; 127853018216SPaolo Bonzini uint32_t initrd_base = 0; 127953018216SPaolo Bonzini long kernel_size = 0, initrd_size = 0; 128053018216SPaolo Bonzini long load_limit, rtas_limit, fw_size; 128116457e7fSBenjamin Herrenschmidt bool kernel_le = false; 128253018216SPaolo Bonzini char *filename; 128353018216SPaolo Bonzini 128453018216SPaolo Bonzini msi_supported = true; 128553018216SPaolo Bonzini 128653018216SPaolo Bonzini spapr = g_malloc0(sizeof(*spapr)); 128753018216SPaolo Bonzini QLIST_INIT(&spapr->phbs); 128853018216SPaolo Bonzini 128953018216SPaolo Bonzini cpu_ppc_hypercall = emulate_spapr_hypercall; 129053018216SPaolo Bonzini 129153018216SPaolo Bonzini /* Allocate RMA if necessary */ 129253018216SPaolo Bonzini rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem); 129353018216SPaolo Bonzini 129453018216SPaolo Bonzini if (rma_alloc_size == -1) { 129553018216SPaolo Bonzini hw_error("qemu: Unable to create RMA\n"); 129653018216SPaolo Bonzini exit(1); 129753018216SPaolo Bonzini } 129853018216SPaolo Bonzini 1299c4177479SAlexey Kardashevskiy if (rma_alloc_size && (rma_alloc_size < node0_size)) { 130053018216SPaolo Bonzini spapr->rma_size = rma_alloc_size; 130153018216SPaolo Bonzini } else { 1302c4177479SAlexey Kardashevskiy spapr->rma_size = node0_size; 130353018216SPaolo Bonzini 130453018216SPaolo Bonzini /* With KVM, we don't actually know whether KVM supports an 130553018216SPaolo Bonzini * unbounded RMA (PR KVM) or is limited by the hash table size 130653018216SPaolo Bonzini * (HV KVM using VRMA), so we always assume the latter 130753018216SPaolo Bonzini * 130853018216SPaolo Bonzini * In that case, we also limit the initial allocations for RTAS 130953018216SPaolo Bonzini * etc... to 256M since we have no way to know what the VRMA size 131053018216SPaolo Bonzini * is going to be as it depends on the size of the hash table 131153018216SPaolo Bonzini * isn't determined yet. 131253018216SPaolo Bonzini */ 131353018216SPaolo Bonzini if (kvm_enabled()) { 131453018216SPaolo Bonzini spapr->vrma_adjust = 1; 131553018216SPaolo Bonzini spapr->rma_size = MIN(spapr->rma_size, 0x10000000); 131653018216SPaolo Bonzini } 131753018216SPaolo Bonzini } 131853018216SPaolo Bonzini 1319c4177479SAlexey Kardashevskiy if (spapr->rma_size > node0_size) { 1320c4177479SAlexey Kardashevskiy fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n", 1321c4177479SAlexey Kardashevskiy spapr->rma_size); 1322c4177479SAlexey Kardashevskiy exit(1); 1323c4177479SAlexey Kardashevskiy } 1324c4177479SAlexey Kardashevskiy 132553018216SPaolo Bonzini /* We place the device tree and RTAS just below either the top of the RMA, 132653018216SPaolo Bonzini * or just below 2GB, whichever is lowere, so that it can be 132753018216SPaolo Bonzini * processed with 32-bit real mode code if necessary */ 132853018216SPaolo Bonzini rtas_limit = MIN(spapr->rma_size, 0x80000000); 132953018216SPaolo Bonzini spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE; 133053018216SPaolo Bonzini spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE; 133153018216SPaolo Bonzini load_limit = spapr->fdt_addr - FW_OVERHEAD; 133253018216SPaolo Bonzini 133353018216SPaolo Bonzini /* We aim for a hash table of size 1/128 the size of RAM. The 133453018216SPaolo Bonzini * normal rule of thumb is 1/64 the size of RAM, but that's much 133553018216SPaolo Bonzini * more than needed for the Linux guests we support. */ 133653018216SPaolo Bonzini spapr->htab_shift = 18; /* Minimum architected size */ 133753018216SPaolo Bonzini while (spapr->htab_shift <= 46) { 133853018216SPaolo Bonzini if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) { 133953018216SPaolo Bonzini break; 134053018216SPaolo Bonzini } 134153018216SPaolo Bonzini spapr->htab_shift++; 134253018216SPaolo Bonzini } 134353018216SPaolo Bonzini 13447b565160SDavid Gibson /* Set up Interrupt Controller before we create the VCPUs */ 13457b565160SDavid Gibson spapr->icp = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads, 13467b565160SDavid Gibson XICS_IRQS); 13477b565160SDavid Gibson spapr->next_irq = XICS_IRQ_BASE; 13487b565160SDavid Gibson 134953018216SPaolo Bonzini /* init CPUs */ 135053018216SPaolo Bonzini if (cpu_model == NULL) { 135153018216SPaolo Bonzini cpu_model = kvm_enabled() ? "host" : "POWER7"; 135253018216SPaolo Bonzini } 135353018216SPaolo Bonzini for (i = 0; i < smp_cpus; i++) { 135453018216SPaolo Bonzini cpu = cpu_ppc_init(cpu_model); 135553018216SPaolo Bonzini if (cpu == NULL) { 135653018216SPaolo Bonzini fprintf(stderr, "Unable to find PowerPC CPU definition\n"); 135753018216SPaolo Bonzini exit(1); 135853018216SPaolo Bonzini } 135953018216SPaolo Bonzini env = &cpu->env; 136053018216SPaolo Bonzini 136153018216SPaolo Bonzini /* Set time-base frequency to 512 MHz */ 136253018216SPaolo Bonzini cpu_ppc_tb_init(env, TIMEBASE_FREQ); 136353018216SPaolo Bonzini 13642cf3eb6dSFabien Chouteau /* PAPR always has exception vectors in RAM not ROM. To ensure this, 13652cf3eb6dSFabien Chouteau * MSR[IP] should never be set. 13662cf3eb6dSFabien Chouteau */ 13672cf3eb6dSFabien Chouteau env->msr_mask &= ~(1 << 6); 136853018216SPaolo Bonzini 136953018216SPaolo Bonzini /* Tell KVM that we're in PAPR mode */ 137053018216SPaolo Bonzini if (kvm_enabled()) { 137153018216SPaolo Bonzini kvmppc_set_papr(cpu); 137253018216SPaolo Bonzini } 137353018216SPaolo Bonzini 13746d9412eaSAlexey Kardashevskiy if (cpu->max_compat) { 13756d9412eaSAlexey Kardashevskiy if (ppc_set_compat(cpu, cpu->max_compat) < 0) { 13766d9412eaSAlexey Kardashevskiy exit(1); 13776d9412eaSAlexey Kardashevskiy } 13786d9412eaSAlexey Kardashevskiy } 13796d9412eaSAlexey Kardashevskiy 138024408a7dSAlexey Kardashevskiy xics_cpu_setup(spapr->icp, cpu); 138124408a7dSAlexey Kardashevskiy 138253018216SPaolo Bonzini qemu_register_reset(spapr_cpu_reset, cpu); 138353018216SPaolo Bonzini } 138453018216SPaolo Bonzini 138553018216SPaolo Bonzini /* allocate RAM */ 138653018216SPaolo Bonzini spapr->ram_limit = ram_size; 138753018216SPaolo Bonzini if (spapr->ram_limit > rma_alloc_size) { 138853018216SPaolo Bonzini ram_addr_t nonrma_base = rma_alloc_size; 138953018216SPaolo Bonzini ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size; 139053018216SPaolo Bonzini 13912c9b15caSPaolo Bonzini memory_region_init_ram(ram, NULL, "ppc_spapr.ram", nonrma_size); 139253018216SPaolo Bonzini vmstate_register_ram_global(ram); 139353018216SPaolo Bonzini memory_region_add_subregion(sysmem, nonrma_base, ram); 139453018216SPaolo Bonzini } 139553018216SPaolo Bonzini 139653018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin"); 139753018216SPaolo Bonzini spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr, 139853018216SPaolo Bonzini rtas_limit - spapr->rtas_addr); 139953018216SPaolo Bonzini if (spapr->rtas_size < 0) { 140053018216SPaolo Bonzini hw_error("qemu: could not load LPAR rtas '%s'\n", filename); 140153018216SPaolo Bonzini exit(1); 140253018216SPaolo Bonzini } 140353018216SPaolo Bonzini if (spapr->rtas_size > RTAS_MAX_SIZE) { 140453018216SPaolo Bonzini hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n", 140553018216SPaolo Bonzini spapr->rtas_size, RTAS_MAX_SIZE); 140653018216SPaolo Bonzini exit(1); 140753018216SPaolo Bonzini } 140853018216SPaolo Bonzini g_free(filename); 140953018216SPaolo Bonzini 141053018216SPaolo Bonzini /* Set up EPOW events infrastructure */ 141153018216SPaolo Bonzini spapr_events_init(spapr); 141253018216SPaolo Bonzini 141353018216SPaolo Bonzini /* Set up VIO bus */ 141453018216SPaolo Bonzini spapr->vio_bus = spapr_vio_bus_init(); 141553018216SPaolo Bonzini 141653018216SPaolo Bonzini for (i = 0; i < MAX_SERIAL_PORTS; i++) { 141753018216SPaolo Bonzini if (serial_hds[i]) { 141853018216SPaolo Bonzini spapr_vty_create(spapr->vio_bus, serial_hds[i]); 141953018216SPaolo Bonzini } 142053018216SPaolo Bonzini } 142153018216SPaolo Bonzini 142253018216SPaolo Bonzini /* We always have at least the nvram device on VIO */ 142353018216SPaolo Bonzini spapr_create_nvram(spapr); 142453018216SPaolo Bonzini 142553018216SPaolo Bonzini /* Set up PCI */ 1426f1c2dc7cSAlexey Kardashevskiy spapr_pci_msi_init(spapr, SPAPR_PCI_MSI_WINDOW); 142753018216SPaolo Bonzini spapr_pci_rtas_init(); 142853018216SPaolo Bonzini 142989dfd6e1SDavid Gibson phb = spapr_create_phb(spapr, 0); 143053018216SPaolo Bonzini 143153018216SPaolo Bonzini for (i = 0; i < nb_nics; i++) { 143253018216SPaolo Bonzini NICInfo *nd = &nd_table[i]; 143353018216SPaolo Bonzini 143453018216SPaolo Bonzini if (!nd->model) { 143553018216SPaolo Bonzini nd->model = g_strdup("ibmveth"); 143653018216SPaolo Bonzini } 143753018216SPaolo Bonzini 143853018216SPaolo Bonzini if (strcmp(nd->model, "ibmveth") == 0) { 143953018216SPaolo Bonzini spapr_vlan_create(spapr->vio_bus, nd); 144053018216SPaolo Bonzini } else { 144129b358f9SDavid Gibson pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL); 144253018216SPaolo Bonzini } 144353018216SPaolo Bonzini } 144453018216SPaolo Bonzini 144553018216SPaolo Bonzini for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) { 144653018216SPaolo Bonzini spapr_vscsi_create(spapr->vio_bus); 144753018216SPaolo Bonzini } 144853018216SPaolo Bonzini 144953018216SPaolo Bonzini /* Graphics */ 145053018216SPaolo Bonzini if (spapr_vga_init(phb->bus)) { 145153018216SPaolo Bonzini spapr->has_graphics = true; 145253018216SPaolo Bonzini } 145353018216SPaolo Bonzini 145453018216SPaolo Bonzini if (usb_enabled(spapr->has_graphics)) { 145553018216SPaolo Bonzini pci_create_simple(phb->bus, -1, "pci-ohci"); 145653018216SPaolo Bonzini if (spapr->has_graphics) { 145753018216SPaolo Bonzini usbdevice_create("keyboard"); 145853018216SPaolo Bonzini usbdevice_create("mouse"); 145953018216SPaolo Bonzini } 146053018216SPaolo Bonzini } 146153018216SPaolo Bonzini 146253018216SPaolo Bonzini if (spapr->rma_size < (MIN_RMA_SLOF << 20)) { 146353018216SPaolo Bonzini fprintf(stderr, "qemu: pSeries SLOF firmware requires >= " 146453018216SPaolo Bonzini "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF); 146553018216SPaolo Bonzini exit(1); 146653018216SPaolo Bonzini } 146753018216SPaolo Bonzini 146853018216SPaolo Bonzini if (kernel_filename) { 146953018216SPaolo Bonzini uint64_t lowaddr = 0; 147053018216SPaolo Bonzini 147153018216SPaolo Bonzini kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL, 147253018216SPaolo Bonzini NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0); 14733b66da82SAlexey Kardashevskiy if (kernel_size == ELF_LOAD_WRONG_ENDIAN) { 147416457e7fSBenjamin Herrenschmidt kernel_size = load_elf(kernel_filename, 147516457e7fSBenjamin Herrenschmidt translate_kernel_address, NULL, 147616457e7fSBenjamin Herrenschmidt NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0); 147716457e7fSBenjamin Herrenschmidt kernel_le = kernel_size > 0; 147816457e7fSBenjamin Herrenschmidt } 147916457e7fSBenjamin Herrenschmidt if (kernel_size < 0) { 14803b66da82SAlexey Kardashevskiy fprintf(stderr, "qemu: error loading %s: %s\n", 14813b66da82SAlexey Kardashevskiy kernel_filename, load_elf_strerror(kernel_size)); 148253018216SPaolo Bonzini exit(1); 148353018216SPaolo Bonzini } 148453018216SPaolo Bonzini 148553018216SPaolo Bonzini /* load initrd */ 148653018216SPaolo Bonzini if (initrd_filename) { 148753018216SPaolo Bonzini /* Try to locate the initrd in the gap between the kernel 148853018216SPaolo Bonzini * and the firmware. Add a bit of space just in case 148953018216SPaolo Bonzini */ 149053018216SPaolo Bonzini initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff; 149153018216SPaolo Bonzini initrd_size = load_image_targphys(initrd_filename, initrd_base, 149253018216SPaolo Bonzini load_limit - initrd_base); 149353018216SPaolo Bonzini if (initrd_size < 0) { 149453018216SPaolo Bonzini fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 149553018216SPaolo Bonzini initrd_filename); 149653018216SPaolo Bonzini exit(1); 149753018216SPaolo Bonzini } 149853018216SPaolo Bonzini } else { 149953018216SPaolo Bonzini initrd_base = 0; 150053018216SPaolo Bonzini initrd_size = 0; 150153018216SPaolo Bonzini } 150253018216SPaolo Bonzini } 150353018216SPaolo Bonzini 15048e7ea787SAndreas Färber if (bios_name == NULL) { 15058e7ea787SAndreas Färber bios_name = FW_FILE_NAME; 15068e7ea787SAndreas Färber } 15078e7ea787SAndreas Färber filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 150853018216SPaolo Bonzini fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE); 150953018216SPaolo Bonzini if (fw_size < 0) { 151053018216SPaolo Bonzini hw_error("qemu: could not load LPAR rtas '%s'\n", filename); 151153018216SPaolo Bonzini exit(1); 151253018216SPaolo Bonzini } 151353018216SPaolo Bonzini g_free(filename); 151453018216SPaolo Bonzini 151553018216SPaolo Bonzini spapr->entry_point = 0x100; 151653018216SPaolo Bonzini 15174be21d56SDavid Gibson vmstate_register(NULL, 0, &vmstate_spapr, spapr); 15184be21d56SDavid Gibson register_savevm_live(NULL, "spapr/htab", -1, 1, 15194be21d56SDavid Gibson &savevm_htab_handlers, spapr); 15204be21d56SDavid Gibson 152153018216SPaolo Bonzini /* Prepare the device tree */ 15223bbf37f2SAndreas Färber spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size, 152316457e7fSBenjamin Herrenschmidt kernel_size, kernel_le, 152453018216SPaolo Bonzini boot_device, kernel_cmdline, 152553018216SPaolo Bonzini spapr->epow_irq); 152653018216SPaolo Bonzini assert(spapr->fdt_skel != NULL); 152753018216SPaolo Bonzini } 152853018216SPaolo Bonzini 1529135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type) 1530135a129aSAneesh Kumar K.V { 1531135a129aSAneesh Kumar K.V if (!vm_type) { 1532135a129aSAneesh Kumar K.V return 0; 1533135a129aSAneesh Kumar K.V } 1534135a129aSAneesh Kumar K.V 1535135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "HV")) { 1536135a129aSAneesh Kumar K.V return 1; 1537135a129aSAneesh Kumar K.V } 1538135a129aSAneesh Kumar K.V 1539135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "PR")) { 1540135a129aSAneesh Kumar K.V return 2; 1541135a129aSAneesh Kumar K.V } 1542135a129aSAneesh Kumar K.V 1543135a129aSAneesh Kumar K.V error_report("Unknown kvm-type specified '%s'", vm_type); 1544135a129aSAneesh Kumar K.V exit(1); 1545135a129aSAneesh Kumar K.V } 1546135a129aSAneesh Kumar K.V 154771461b0fSAlexey Kardashevskiy /* 154871461b0fSAlexey Kardashevskiy * Implementation of an interface to adjust firmware patch 154971461b0fSAlexey Kardashevskiy * for the bootindex property handling. 155071461b0fSAlexey Kardashevskiy */ 155171461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus, 155271461b0fSAlexey Kardashevskiy DeviceState *dev) 155371461b0fSAlexey Kardashevskiy { 155471461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \ 155571461b0fSAlexey Kardashevskiy ((type *)object_dynamic_cast(OBJECT(obj), (name))) 155671461b0fSAlexey Kardashevskiy SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE); 155771461b0fSAlexey Kardashevskiy sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE); 155871461b0fSAlexey Kardashevskiy 155971461b0fSAlexey Kardashevskiy if (d) { 156071461b0fSAlexey Kardashevskiy void *spapr = CAST(void, bus->parent, "spapr-vscsi"); 156171461b0fSAlexey Kardashevskiy VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI); 156271461b0fSAlexey Kardashevskiy USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE); 156371461b0fSAlexey Kardashevskiy 156471461b0fSAlexey Kardashevskiy if (spapr) { 156571461b0fSAlexey Kardashevskiy /* 156671461b0fSAlexey Kardashevskiy * Replace "channel@0/disk@0,0" with "disk@8000000000000000": 156771461b0fSAlexey Kardashevskiy * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun 156871461b0fSAlexey Kardashevskiy * in the top 16 bits of the 64-bit LUN 156971461b0fSAlexey Kardashevskiy */ 157071461b0fSAlexey Kardashevskiy unsigned id = 0x8000 | (d->id << 8) | d->lun; 157171461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 157271461b0fSAlexey Kardashevskiy (uint64_t)id << 48); 157371461b0fSAlexey Kardashevskiy } else if (virtio) { 157471461b0fSAlexey Kardashevskiy /* 157571461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (target << 8) | lun 157671461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 157771461b0fSAlexey Kardashevskiy * Note: the quote above is from SLOF and it is wrong, 157871461b0fSAlexey Kardashevskiy * the actual binding is: 157971461b0fSAlexey Kardashevskiy * swap 0100 or 10 << or 20 << ( target lun-id -- srplun ) 158071461b0fSAlexey Kardashevskiy */ 158171461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (d->id << 16) | d->lun; 158271461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 158371461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 158471461b0fSAlexey Kardashevskiy } else if (usb) { 158571461b0fSAlexey Kardashevskiy /* 158671461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (usb-port << 16) | lun 158771461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 158871461b0fSAlexey Kardashevskiy */ 158971461b0fSAlexey Kardashevskiy unsigned usb_port = atoi(usb->port->path); 159071461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (usb_port << 16) | d->lun; 159171461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 159271461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 159371461b0fSAlexey Kardashevskiy } 159471461b0fSAlexey Kardashevskiy } 159571461b0fSAlexey Kardashevskiy 159671461b0fSAlexey Kardashevskiy if (phb) { 159771461b0fSAlexey Kardashevskiy /* Replace "pci" with "pci@800000020000000" */ 159871461b0fSAlexey Kardashevskiy return g_strdup_printf("pci@%"PRIX64, phb->buid); 159971461b0fSAlexey Kardashevskiy } 160071461b0fSAlexey Kardashevskiy 160171461b0fSAlexey Kardashevskiy return NULL; 160271461b0fSAlexey Kardashevskiy } 160371461b0fSAlexey Kardashevskiy 160429ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data) 160553018216SPaolo Bonzini { 160629ee3247SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 160771461b0fSAlexey Kardashevskiy FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc); 160829ee3247SAlexey Kardashevskiy 1609958db90cSMarcel Apfelbaum mc->name = "pseries"; 1610958db90cSMarcel Apfelbaum mc->desc = "pSeries Logical Partition (PAPR compliant)"; 1611958db90cSMarcel Apfelbaum mc->is_default = 1; 1612958db90cSMarcel Apfelbaum mc->init = ppc_spapr_init; 1613958db90cSMarcel Apfelbaum mc->reset = ppc_spapr_reset; 1614958db90cSMarcel Apfelbaum mc->block_default_type = IF_SCSI; 1615958db90cSMarcel Apfelbaum mc->max_cpus = MAX_CPUS; 1616958db90cSMarcel Apfelbaum mc->no_parallel = 1; 1617958db90cSMarcel Apfelbaum mc->default_boot_order = NULL; 1618958db90cSMarcel Apfelbaum mc->kvm_type = spapr_kvm_type; 161900b4fbe2SMarcel Apfelbaum 162071461b0fSAlexey Kardashevskiy fwc->get_dev_path = spapr_get_fw_dev_path; 162153018216SPaolo Bonzini } 162253018216SPaolo Bonzini 162329ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = { 162429ee3247SAlexey Kardashevskiy .name = TYPE_SPAPR_MACHINE, 162529ee3247SAlexey Kardashevskiy .parent = TYPE_MACHINE, 162629ee3247SAlexey Kardashevskiy .class_init = spapr_machine_class_init, 162771461b0fSAlexey Kardashevskiy .interfaces = (InterfaceInfo[]) { 162871461b0fSAlexey Kardashevskiy { TYPE_FW_PATH_PROVIDER }, 162971461b0fSAlexey Kardashevskiy { } 163071461b0fSAlexey Kardashevskiy }, 163129ee3247SAlexey Kardashevskiy }; 163229ee3247SAlexey Kardashevskiy 163329ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void) 163429ee3247SAlexey Kardashevskiy { 163529ee3247SAlexey Kardashevskiy type_register_static(&spapr_machine_info); 163629ee3247SAlexey Kardashevskiy } 163729ee3247SAlexey Kardashevskiy 163829ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types) 1639