153018216SPaolo Bonzini /* 253018216SPaolo Bonzini * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 353018216SPaolo Bonzini * 453018216SPaolo Bonzini * Copyright (c) 2004-2007 Fabrice Bellard 553018216SPaolo Bonzini * Copyright (c) 2007 Jocelyn Mayer 653018216SPaolo Bonzini * Copyright (c) 2010 David Gibson, IBM Corporation. 753018216SPaolo Bonzini * 853018216SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 953018216SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 1053018216SPaolo Bonzini * in the Software without restriction, including without limitation the rights 1153018216SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1253018216SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 1353018216SPaolo Bonzini * furnished to do so, subject to the following conditions: 1453018216SPaolo Bonzini * 1553018216SPaolo Bonzini * The above copyright notice and this permission notice shall be included in 1653018216SPaolo Bonzini * all copies or substantial portions of the Software. 1753018216SPaolo Bonzini * 1853018216SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1953018216SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2053018216SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2153018216SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2253018216SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2353018216SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2453018216SPaolo Bonzini * THE SOFTWARE. 2553018216SPaolo Bonzini * 2653018216SPaolo Bonzini */ 2753018216SPaolo Bonzini #include "sysemu/sysemu.h" 28e35704baSEduardo Habkost #include "sysemu/numa.h" 2953018216SPaolo Bonzini #include "hw/hw.h" 3071461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h" 3153018216SPaolo Bonzini #include "elf.h" 3253018216SPaolo Bonzini #include "net/net.h" 33fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h" 3453018216SPaolo Bonzini #include "sysemu/cpus.h" 3553018216SPaolo Bonzini #include "sysemu/kvm.h" 3653018216SPaolo Bonzini #include "kvm_ppc.h" 37ff14e817SDr. David Alan Gilbert #include "migration/migration.h" 384be21d56SDavid Gibson #include "mmu-hash64.h" 393794d548SAlexey Kardashevskiy #include "qom/cpu.h" 4053018216SPaolo Bonzini 4153018216SPaolo Bonzini #include "hw/boards.h" 420d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h" 4353018216SPaolo Bonzini #include "hw/loader.h" 4453018216SPaolo Bonzini 450d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h" 460d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h" 470d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h" 480d09e41aSPaolo Bonzini #include "hw/ppc/xics.h" 4953018216SPaolo Bonzini #include "hw/pci/msi.h" 5053018216SPaolo Bonzini 5153018216SPaolo Bonzini #include "hw/pci/pci.h" 5271461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h" 5371461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h" 5453018216SPaolo Bonzini 5553018216SPaolo Bonzini #include "exec/address-spaces.h" 5653018216SPaolo Bonzini #include "hw/usb.h" 5753018216SPaolo Bonzini #include "qemu/config-file.h" 58135a129aSAneesh Kumar K.V #include "qemu/error-report.h" 592a6593cbSAlexey Kardashevskiy #include "trace.h" 6034316482SAlexey Kardashevskiy #include "hw/nmi.h" 6153018216SPaolo Bonzini 6268a27b20SMichael S. Tsirkin #include "hw/compat.h" 6368a27b20SMichael S. Tsirkin 6453018216SPaolo Bonzini #include <libfdt.h> 6553018216SPaolo Bonzini 6653018216SPaolo Bonzini /* SLOF memory layout: 6753018216SPaolo Bonzini * 6853018216SPaolo Bonzini * SLOF raw image loaded at 0, copies its romfs right below the flat 6953018216SPaolo Bonzini * device-tree, then position SLOF itself 31M below that 7053018216SPaolo Bonzini * 7153018216SPaolo Bonzini * So we set FW_OVERHEAD to 40MB which should account for all of that 7253018216SPaolo Bonzini * and more 7353018216SPaolo Bonzini * 7453018216SPaolo Bonzini * We load our kernel at 4M, leaving space for SLOF initial image 7553018216SPaolo Bonzini */ 763bf6eeddSAlexey Kardashevskiy #define FDT_MAX_SIZE 0x40000 7753018216SPaolo Bonzini #define RTAS_MAX_SIZE 0x10000 78b7d1f77aSBenjamin Herrenschmidt #define RTAS_MAX_ADDR 0x80000000 /* RTAS must stay below that */ 7953018216SPaolo Bonzini #define FW_MAX_SIZE 0x400000 8053018216SPaolo Bonzini #define FW_FILE_NAME "slof.bin" 8153018216SPaolo Bonzini #define FW_OVERHEAD 0x2800000 8253018216SPaolo Bonzini #define KERNEL_LOAD_ADDR FW_MAX_SIZE 8353018216SPaolo Bonzini 8453018216SPaolo Bonzini #define MIN_RMA_SLOF 128UL 8553018216SPaolo Bonzini 8653018216SPaolo Bonzini #define TIMEBASE_FREQ 512000000ULL 8753018216SPaolo Bonzini 889674a356SNikunj A Dadhania #define MAX_CPUS 255 8953018216SPaolo Bonzini 9053018216SPaolo Bonzini #define PHANDLE_XICP 0x00001111 9153018216SPaolo Bonzini 9253018216SPaolo Bonzini #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift)) 9353018216SPaolo Bonzini 94c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers, 9534f2af3dSMarkus Armbruster int nr_irqs, Error **errp) 96c04d6cfaSAnthony Liguori { 9734f2af3dSMarkus Armbruster Error *err = NULL; 98c04d6cfaSAnthony Liguori DeviceState *dev; 99c04d6cfaSAnthony Liguori 100c04d6cfaSAnthony Liguori dev = qdev_create(NULL, type); 101c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_servers", nr_servers); 102c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs); 10334f2af3dSMarkus Armbruster object_property_set_bool(OBJECT(dev), true, "realized", &err); 10434f2af3dSMarkus Armbruster if (err) { 10534f2af3dSMarkus Armbruster error_propagate(errp, err); 10634f2af3dSMarkus Armbruster object_unparent(OBJECT(dev)); 107c04d6cfaSAnthony Liguori return NULL; 108c04d6cfaSAnthony Liguori } 1095a3d7b23SAlexey Kardashevskiy return XICS_COMMON(dev); 110c04d6cfaSAnthony Liguori } 111c04d6cfaSAnthony Liguori 112446f16a6SMarcel Apfelbaum static XICSState *xics_system_init(MachineState *machine, 113446f16a6SMarcel Apfelbaum int nr_servers, int nr_irqs) 114c04d6cfaSAnthony Liguori { 115c04d6cfaSAnthony Liguori XICSState *icp = NULL; 116c04d6cfaSAnthony Liguori 11711ad93f6SDavid Gibson if (kvm_enabled()) { 11834f2af3dSMarkus Armbruster Error *err = NULL; 11934f2af3dSMarkus Armbruster 120446f16a6SMarcel Apfelbaum if (machine_kernel_irqchip_allowed(machine)) { 12134f2af3dSMarkus Armbruster icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs, &err); 12211ad93f6SDavid Gibson } 123446f16a6SMarcel Apfelbaum if (machine_kernel_irqchip_required(machine) && !icp) { 12434f2af3dSMarkus Armbruster error_report("kernel_irqchip requested but unavailable: %s", 12534f2af3dSMarkus Armbruster error_get_pretty(err)); 12611ad93f6SDavid Gibson } 12711ad93f6SDavid Gibson } 12811ad93f6SDavid Gibson 12911ad93f6SDavid Gibson if (!icp) { 13034f2af3dSMarkus Armbruster icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs, &error_abort); 131c04d6cfaSAnthony Liguori } 132c04d6cfaSAnthony Liguori 133c04d6cfaSAnthony Liguori return icp; 134c04d6cfaSAnthony Liguori } 135c04d6cfaSAnthony Liguori 136833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu, 137833d4668SAlexey Kardashevskiy int smt_threads) 138833d4668SAlexey Kardashevskiy { 139833d4668SAlexey Kardashevskiy int i, ret = 0; 140833d4668SAlexey Kardashevskiy uint32_t servers_prop[smt_threads]; 141833d4668SAlexey Kardashevskiy uint32_t gservers_prop[smt_threads * 2]; 142833d4668SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 143833d4668SAlexey Kardashevskiy 1446d9412eaSAlexey Kardashevskiy if (cpu->cpu_version) { 1454bce526eSLaurent Dufour ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version); 1466d9412eaSAlexey Kardashevskiy if (ret < 0) { 1476d9412eaSAlexey Kardashevskiy return ret; 1486d9412eaSAlexey Kardashevskiy } 1496d9412eaSAlexey Kardashevskiy } 1506d9412eaSAlexey Kardashevskiy 151833d4668SAlexey Kardashevskiy /* Build interrupt servers and gservers properties */ 152833d4668SAlexey Kardashevskiy for (i = 0; i < smt_threads; i++) { 153833d4668SAlexey Kardashevskiy servers_prop[i] = cpu_to_be32(index + i); 154833d4668SAlexey Kardashevskiy /* Hack, direct the group queues back to cpu 0 */ 155833d4668SAlexey Kardashevskiy gservers_prop[i*2] = cpu_to_be32(index + i); 156833d4668SAlexey Kardashevskiy gservers_prop[i*2 + 1] = 0; 157833d4668SAlexey Kardashevskiy } 158833d4668SAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s", 159833d4668SAlexey Kardashevskiy servers_prop, sizeof(servers_prop)); 160833d4668SAlexey Kardashevskiy if (ret < 0) { 161833d4668SAlexey Kardashevskiy return ret; 162833d4668SAlexey Kardashevskiy } 163833d4668SAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s", 164833d4668SAlexey Kardashevskiy gservers_prop, sizeof(gservers_prop)); 165833d4668SAlexey Kardashevskiy 166833d4668SAlexey Kardashevskiy return ret; 167833d4668SAlexey Kardashevskiy } 168833d4668SAlexey Kardashevskiy 1690da6f3feSBharata B Rao static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, CPUState *cs) 1700da6f3feSBharata B Rao { 1710da6f3feSBharata B Rao int ret = 0; 1720da6f3feSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(cs); 1730da6f3feSBharata B Rao int index = ppc_get_vcpu_dt_id(cpu); 1740da6f3feSBharata B Rao uint32_t associativity[] = {cpu_to_be32(0x5), 1750da6f3feSBharata B Rao cpu_to_be32(0x0), 1760da6f3feSBharata B Rao cpu_to_be32(0x0), 1770da6f3feSBharata B Rao cpu_to_be32(0x0), 1780da6f3feSBharata B Rao cpu_to_be32(cs->numa_node), 1790da6f3feSBharata B Rao cpu_to_be32(index)}; 1800da6f3feSBharata B Rao 1810da6f3feSBharata B Rao /* Advertise NUMA via ibm,associativity */ 1820da6f3feSBharata B Rao if (nb_numa_nodes > 1) { 1830da6f3feSBharata B Rao ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity, 1840da6f3feSBharata B Rao sizeof(associativity)); 1850da6f3feSBharata B Rao } 1860da6f3feSBharata B Rao 1870da6f3feSBharata B Rao return ret; 1880da6f3feSBharata B Rao } 1890da6f3feSBharata B Rao 19028e02042SDavid Gibson static int spapr_fixup_cpu_dt(void *fdt, sPAPRMachineState *spapr) 19153018216SPaolo Bonzini { 19282677ed2SAlexey Kardashevskiy int ret = 0, offset, cpus_offset; 19382677ed2SAlexey Kardashevskiy CPUState *cs; 19453018216SPaolo Bonzini char cpu_model[32]; 19553018216SPaolo Bonzini int smt = kvmppc_smt_threads(); 19653018216SPaolo Bonzini uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)}; 19753018216SPaolo Bonzini 19882677ed2SAlexey Kardashevskiy CPU_FOREACH(cs) { 19982677ed2SAlexey Kardashevskiy PowerPCCPU *cpu = POWERPC_CPU(cs); 20082677ed2SAlexey Kardashevskiy DeviceClass *dc = DEVICE_GET_CLASS(cs); 20182677ed2SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 20253018216SPaolo Bonzini 2030f20ba62SAlexey Kardashevskiy if ((index % smt) != 0) { 20453018216SPaolo Bonzini continue; 20553018216SPaolo Bonzini } 20653018216SPaolo Bonzini 20782677ed2SAlexey Kardashevskiy snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index); 20853018216SPaolo Bonzini 20982677ed2SAlexey Kardashevskiy cpus_offset = fdt_path_offset(fdt, "/cpus"); 21082677ed2SAlexey Kardashevskiy if (cpus_offset < 0) { 21182677ed2SAlexey Kardashevskiy cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), 21282677ed2SAlexey Kardashevskiy "cpus"); 21382677ed2SAlexey Kardashevskiy if (cpus_offset < 0) { 21482677ed2SAlexey Kardashevskiy return cpus_offset; 21582677ed2SAlexey Kardashevskiy } 21682677ed2SAlexey Kardashevskiy } 21782677ed2SAlexey Kardashevskiy offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model); 21882677ed2SAlexey Kardashevskiy if (offset < 0) { 21982677ed2SAlexey Kardashevskiy offset = fdt_add_subnode(fdt, cpus_offset, cpu_model); 22053018216SPaolo Bonzini if (offset < 0) { 22153018216SPaolo Bonzini return offset; 22253018216SPaolo Bonzini } 22382677ed2SAlexey Kardashevskiy } 22453018216SPaolo Bonzini 2250da6f3feSBharata B Rao ret = fdt_setprop(fdt, offset, "ibm,pft-size", 2260da6f3feSBharata B Rao pft_size_prop, sizeof(pft_size_prop)); 22753018216SPaolo Bonzini if (ret < 0) { 22853018216SPaolo Bonzini return ret; 22953018216SPaolo Bonzini } 23053018216SPaolo Bonzini 2310da6f3feSBharata B Rao ret = spapr_fixup_cpu_numa_dt(fdt, offset, cs); 23253018216SPaolo Bonzini if (ret < 0) { 23353018216SPaolo Bonzini return ret; 23453018216SPaolo Bonzini } 235833d4668SAlexey Kardashevskiy 23682677ed2SAlexey Kardashevskiy ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, 2372a48d993SAlexey Kardashevskiy ppc_get_compat_smt_threads(cpu)); 238833d4668SAlexey Kardashevskiy if (ret < 0) { 239833d4668SAlexey Kardashevskiy return ret; 240833d4668SAlexey Kardashevskiy } 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 279b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void) 280b082d65aSAlexey Kardashevskiy { 281fb164994SDavid Gibson MachineState *machine = MACHINE(qdev_get_machine()); 282fb164994SDavid Gibson 283b082d65aSAlexey Kardashevskiy if (nb_numa_nodes) { 284b082d65aSAlexey Kardashevskiy int i; 285b082d65aSAlexey Kardashevskiy for (i = 0; i < nb_numa_nodes; ++i) { 286b082d65aSAlexey Kardashevskiy if (numa_info[i].node_mem) { 287fb164994SDavid Gibson return MIN(pow2floor(numa_info[i].node_mem), 288fb164994SDavid Gibson machine->ram_size); 289b082d65aSAlexey Kardashevskiy } 290b082d65aSAlexey Kardashevskiy } 291b082d65aSAlexey Kardashevskiy } 292fb164994SDavid Gibson return machine->ram_size; 293b082d65aSAlexey Kardashevskiy } 294b082d65aSAlexey Kardashevskiy 29553018216SPaolo Bonzini #define _FDT(exp) \ 29653018216SPaolo Bonzini do { \ 29753018216SPaolo Bonzini int ret = (exp); \ 29853018216SPaolo Bonzini if (ret < 0) { \ 29953018216SPaolo Bonzini fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \ 30053018216SPaolo Bonzini #exp, fdt_strerror(ret)); \ 30153018216SPaolo Bonzini exit(1); \ 30253018216SPaolo Bonzini } \ 30353018216SPaolo Bonzini } while (0) 30453018216SPaolo Bonzini 305a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1) 306a1d59c0fSAlexey Kardashevskiy { 307a1d59c0fSAlexey Kardashevskiy g_string_append_len(s, s1, strlen(s1) + 1); 308a1d59c0fSAlexey Kardashevskiy } 30953018216SPaolo Bonzini 3103bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base, 31153018216SPaolo Bonzini hwaddr initrd_size, 31253018216SPaolo Bonzini hwaddr kernel_size, 31316457e7fSBenjamin Herrenschmidt bool little_endian, 31453018216SPaolo Bonzini const char *kernel_cmdline, 31553018216SPaolo Bonzini uint32_t epow_irq) 31653018216SPaolo Bonzini { 31753018216SPaolo Bonzini void *fdt; 31853018216SPaolo Bonzini uint32_t start_prop = cpu_to_be32(initrd_base); 31953018216SPaolo Bonzini uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size); 320a1d59c0fSAlexey Kardashevskiy GString *hypertas = g_string_sized_new(256); 321a1d59c0fSAlexey Kardashevskiy GString *qemu_hypertas = g_string_sized_new(256); 32253018216SPaolo Bonzini uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)}; 3239e734e3dSBharata B Rao uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(max_cpus)}; 32453018216SPaolo Bonzini unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80}; 325ef951443SNikunj A Dadhania char *buf; 32653018216SPaolo Bonzini 327a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-pft"); 328a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-term"); 329a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-dabr"); 330a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-interrupt"); 331a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-tce"); 332a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-vio"); 333a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-splpar"); 334a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-bulk"); 335a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-set-mode"); 336a1d59c0fSAlexey Kardashevskiy add_str(qemu_hypertas, "hcall-memop1"); 337a1d59c0fSAlexey Kardashevskiy 33853018216SPaolo Bonzini fdt = g_malloc0(FDT_MAX_SIZE); 33953018216SPaolo Bonzini _FDT((fdt_create(fdt, FDT_MAX_SIZE))); 34053018216SPaolo Bonzini 34153018216SPaolo Bonzini if (kernel_size) { 34253018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size))); 34353018216SPaolo Bonzini } 34453018216SPaolo Bonzini if (initrd_size) { 34553018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size))); 34653018216SPaolo Bonzini } 34753018216SPaolo Bonzini _FDT((fdt_finish_reservemap(fdt))); 34853018216SPaolo Bonzini 34953018216SPaolo Bonzini /* Root node */ 35053018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, ""))); 35153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "chrp"))); 35253018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)"))); 353fa388916SAnthony Liguori _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries"))); 35453018216SPaolo Bonzini 355ef951443SNikunj A Dadhania /* 356ef951443SNikunj A Dadhania * Add info to guest to indentify which host is it being run on 357ef951443SNikunj A Dadhania * and what is the uuid of the guest 358ef951443SNikunj A Dadhania */ 359ef951443SNikunj A Dadhania if (kvmppc_get_host_model(&buf)) { 360ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "host-model", buf))); 361ef951443SNikunj A Dadhania g_free(buf); 362ef951443SNikunj A Dadhania } 363ef951443SNikunj A Dadhania if (kvmppc_get_host_serial(&buf)) { 364ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "host-serial", buf))); 365ef951443SNikunj A Dadhania g_free(buf); 366ef951443SNikunj A Dadhania } 367ef951443SNikunj A Dadhania 368ef951443SNikunj A Dadhania buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1], 369ef951443SNikunj A Dadhania qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], 370ef951443SNikunj A Dadhania qemu_uuid[5], qemu_uuid[6], qemu_uuid[7], 371ef951443SNikunj A Dadhania qemu_uuid[8], qemu_uuid[9], qemu_uuid[10], 372ef951443SNikunj A Dadhania qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], 373ef951443SNikunj A Dadhania qemu_uuid[14], qemu_uuid[15]); 374ef951443SNikunj A Dadhania 375ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "vm,uuid", buf))); 376ef951443SNikunj A Dadhania g_free(buf); 377ef951443SNikunj A Dadhania 37853018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x2))); 37953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x2))); 38053018216SPaolo Bonzini 38153018216SPaolo Bonzini /* /chosen */ 38253018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "chosen"))); 38353018216SPaolo Bonzini 38453018216SPaolo Bonzini /* Set Form1_affinity */ 38553018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5)))); 38653018216SPaolo Bonzini 38753018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline))); 38853018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-start", 38953018216SPaolo Bonzini &start_prop, sizeof(start_prop)))); 39053018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-end", 39153018216SPaolo Bonzini &end_prop, sizeof(end_prop)))); 39253018216SPaolo Bonzini if (kernel_size) { 39353018216SPaolo Bonzini uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR), 39453018216SPaolo Bonzini cpu_to_be64(kernel_size) }; 39553018216SPaolo Bonzini 39653018216SPaolo Bonzini _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop)))); 39716457e7fSBenjamin Herrenschmidt if (little_endian) { 39816457e7fSBenjamin Herrenschmidt _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0))); 39916457e7fSBenjamin Herrenschmidt } 40053018216SPaolo Bonzini } 401cc84c0f3SAvik Sil if (boot_menu) { 402cc84c0f3SAvik Sil _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu))); 403cc84c0f3SAvik Sil } 40453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width))); 40553018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height))); 40653018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth))); 40753018216SPaolo Bonzini 40853018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 40953018216SPaolo Bonzini 41053018216SPaolo Bonzini /* RTAS */ 41153018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "rtas"))); 41253018216SPaolo Bonzini 413da95324eSAlexey Kardashevskiy if (!kvm_enabled() || kvmppc_spapr_use_multitce()) { 414da95324eSAlexey Kardashevskiy add_str(hypertas, "hcall-multi-tce"); 415da95324eSAlexey Kardashevskiy } 416a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str, 417a1d59c0fSAlexey Kardashevskiy hypertas->len))); 418a1d59c0fSAlexey Kardashevskiy g_string_free(hypertas, TRUE); 419a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str, 420a1d59c0fSAlexey Kardashevskiy qemu_hypertas->len))); 421a1d59c0fSAlexey Kardashevskiy g_string_free(qemu_hypertas, TRUE); 42253018216SPaolo Bonzini 42353018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,associativity-reference-points", 42453018216SPaolo Bonzini refpoints, sizeof(refpoints)))); 42553018216SPaolo Bonzini 42653018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX))); 42779853e18STyrel Datwyler _FDT((fdt_property_cell(fdt, "rtas-event-scan-rate", 42879853e18STyrel Datwyler RTAS_EVENT_SCAN_RATE))); 42953018216SPaolo Bonzini 4302e14072fSNikunj A Dadhania /* 4319d632f5fSzhanghailiang * According to PAPR, rtas ibm,os-term does not guarantee a return 4322e14072fSNikunj A Dadhania * back to the guest cpu. 4332e14072fSNikunj A Dadhania * 4342e14072fSNikunj A Dadhania * While an additional ibm,extended-os-term property indicates that 4352e14072fSNikunj A Dadhania * rtas call return will always occur. Set this property. 4362e14072fSNikunj A Dadhania */ 4372e14072fSNikunj A Dadhania _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0))); 4382e14072fSNikunj A Dadhania 43953018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 44053018216SPaolo Bonzini 44153018216SPaolo Bonzini /* interrupt controller */ 44253018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "interrupt-controller"))); 44353018216SPaolo Bonzini 44453018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", 44553018216SPaolo Bonzini "PowerPC-External-Interrupt-Presentation"))); 44653018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp"))); 44753018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 44853018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges", 44953018216SPaolo Bonzini interrupt_server_ranges_prop, 45053018216SPaolo Bonzini sizeof(interrupt_server_ranges_prop)))); 45153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2))); 45253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP))); 45353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP))); 45453018216SPaolo Bonzini 45553018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 45653018216SPaolo Bonzini 45753018216SPaolo Bonzini /* vdevice */ 45853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "vdevice"))); 45953018216SPaolo Bonzini 46053018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "vdevice"))); 46153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice"))); 46253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 46353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 46453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2))); 46553018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 46653018216SPaolo Bonzini 46753018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 46853018216SPaolo Bonzini 46953018216SPaolo Bonzini /* event-sources */ 47053018216SPaolo Bonzini spapr_events_fdt_skel(fdt, epow_irq); 47153018216SPaolo Bonzini 472f7d69146SAlexander Graf /* /hypervisor node */ 473f7d69146SAlexander Graf if (kvm_enabled()) { 474f7d69146SAlexander Graf uint8_t hypercall[16]; 475f7d69146SAlexander Graf 476f7d69146SAlexander Graf /* indicate KVM hypercall interface */ 477f7d69146SAlexander Graf _FDT((fdt_begin_node(fdt, "hypervisor"))); 478f7d69146SAlexander Graf _FDT((fdt_property_string(fdt, "compatible", "linux,kvm"))); 479f7d69146SAlexander Graf if (kvmppc_has_cap_fixup_hcalls()) { 480f7d69146SAlexander Graf /* 481f7d69146SAlexander Graf * Older KVM versions with older guest kernels were broken with the 482f7d69146SAlexander Graf * magic page, don't allow the guest to map it. 483f7d69146SAlexander Graf */ 484f7d69146SAlexander Graf kvmppc_get_hypercall(first_cpu->env_ptr, hypercall, 485f7d69146SAlexander Graf sizeof(hypercall)); 486f7d69146SAlexander Graf _FDT((fdt_property(fdt, "hcall-instructions", hypercall, 487f7d69146SAlexander Graf sizeof(hypercall)))); 488f7d69146SAlexander Graf } 489f7d69146SAlexander Graf _FDT((fdt_end_node(fdt))); 490f7d69146SAlexander Graf } 491f7d69146SAlexander Graf 49253018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); /* close root node */ 49353018216SPaolo Bonzini _FDT((fdt_finish(fdt))); 49453018216SPaolo Bonzini 49553018216SPaolo Bonzini return fdt; 49653018216SPaolo Bonzini } 49753018216SPaolo Bonzini 49828e02042SDavid Gibson int spapr_h_cas_compose_response(sPAPRMachineState *spapr, 49928e02042SDavid Gibson target_ulong addr, target_ulong size) 5002a6593cbSAlexey Kardashevskiy { 5012a6593cbSAlexey Kardashevskiy void *fdt, *fdt_skel; 5022a6593cbSAlexey Kardashevskiy sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 }; 5032a6593cbSAlexey Kardashevskiy 5042a6593cbSAlexey Kardashevskiy size -= sizeof(hdr); 5052a6593cbSAlexey Kardashevskiy 5062a6593cbSAlexey Kardashevskiy /* Create sceleton */ 5072a6593cbSAlexey Kardashevskiy fdt_skel = g_malloc0(size); 5082a6593cbSAlexey Kardashevskiy _FDT((fdt_create(fdt_skel, size))); 5092a6593cbSAlexey Kardashevskiy _FDT((fdt_begin_node(fdt_skel, ""))); 5102a6593cbSAlexey Kardashevskiy _FDT((fdt_end_node(fdt_skel))); 5112a6593cbSAlexey Kardashevskiy _FDT((fdt_finish(fdt_skel))); 5122a6593cbSAlexey Kardashevskiy fdt = g_malloc0(size); 5132a6593cbSAlexey Kardashevskiy _FDT((fdt_open_into(fdt_skel, fdt, size))); 5142a6593cbSAlexey Kardashevskiy g_free(fdt_skel); 5152a6593cbSAlexey Kardashevskiy 5163794d548SAlexey Kardashevskiy /* Fix skeleton up */ 5173794d548SAlexey Kardashevskiy _FDT((spapr_fixup_cpu_dt(fdt, spapr))); 5182a6593cbSAlexey Kardashevskiy 5192a6593cbSAlexey Kardashevskiy /* Pack resulting tree */ 5202a6593cbSAlexey Kardashevskiy _FDT((fdt_pack(fdt))); 5212a6593cbSAlexey Kardashevskiy 5222a6593cbSAlexey Kardashevskiy if (fdt_totalsize(fdt) + sizeof(hdr) > size) { 5232a6593cbSAlexey Kardashevskiy trace_spapr_cas_failed(size); 5242a6593cbSAlexey Kardashevskiy return -1; 5252a6593cbSAlexey Kardashevskiy } 5262a6593cbSAlexey Kardashevskiy 5272a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr, &hdr, sizeof(hdr)); 5282a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt)); 5292a6593cbSAlexey Kardashevskiy trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr)); 5302a6593cbSAlexey Kardashevskiy g_free(fdt); 5312a6593cbSAlexey Kardashevskiy 5322a6593cbSAlexey Kardashevskiy return 0; 5332a6593cbSAlexey Kardashevskiy } 5342a6593cbSAlexey Kardashevskiy 53526a8c353SAlexey Kardashevskiy static void spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start, 53626a8c353SAlexey Kardashevskiy hwaddr size) 53726a8c353SAlexey Kardashevskiy { 53826a8c353SAlexey Kardashevskiy uint32_t associativity[] = { 53926a8c353SAlexey Kardashevskiy cpu_to_be32(0x4), /* length */ 54026a8c353SAlexey Kardashevskiy cpu_to_be32(0x0), cpu_to_be32(0x0), 541c3b4f589SAlexey Kardashevskiy cpu_to_be32(0x0), cpu_to_be32(nodeid) 54226a8c353SAlexey Kardashevskiy }; 54326a8c353SAlexey Kardashevskiy char mem_name[32]; 54426a8c353SAlexey Kardashevskiy uint64_t mem_reg_property[2]; 54526a8c353SAlexey Kardashevskiy int off; 54626a8c353SAlexey Kardashevskiy 54726a8c353SAlexey Kardashevskiy mem_reg_property[0] = cpu_to_be64(start); 54826a8c353SAlexey Kardashevskiy mem_reg_property[1] = cpu_to_be64(size); 54926a8c353SAlexey Kardashevskiy 55026a8c353SAlexey Kardashevskiy sprintf(mem_name, "memory@" TARGET_FMT_lx, start); 55126a8c353SAlexey Kardashevskiy off = fdt_add_subnode(fdt, 0, mem_name); 55226a8c353SAlexey Kardashevskiy _FDT(off); 55326a8c353SAlexey Kardashevskiy _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 55426a8c353SAlexey Kardashevskiy _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 55526a8c353SAlexey Kardashevskiy sizeof(mem_reg_property)))); 55626a8c353SAlexey Kardashevskiy _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 55726a8c353SAlexey Kardashevskiy sizeof(associativity)))); 55826a8c353SAlexey Kardashevskiy } 55926a8c353SAlexey Kardashevskiy 56028e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt) 56153018216SPaolo Bonzini { 562fb164994SDavid Gibson MachineState *machine = MACHINE(spapr); 5637db8a127SAlexey Kardashevskiy hwaddr mem_start, node_size; 5647db8a127SAlexey Kardashevskiy int i, nb_nodes = nb_numa_nodes; 5657db8a127SAlexey Kardashevskiy NodeInfo *nodes = numa_info; 5667db8a127SAlexey Kardashevskiy NodeInfo ramnode; 56753018216SPaolo Bonzini 5687db8a127SAlexey Kardashevskiy /* No NUMA nodes, assume there is just one node with whole RAM */ 5697db8a127SAlexey Kardashevskiy if (!nb_numa_nodes) { 5707db8a127SAlexey Kardashevskiy nb_nodes = 1; 571fb164994SDavid Gibson ramnode.node_mem = machine->ram_size; 5727db8a127SAlexey Kardashevskiy nodes = &ramnode; 5735fe269b1SPaul Mackerras } 57453018216SPaolo Bonzini 5757db8a127SAlexey Kardashevskiy for (i = 0, mem_start = 0; i < nb_nodes; ++i) { 5767db8a127SAlexey Kardashevskiy if (!nodes[i].node_mem) { 5777db8a127SAlexey Kardashevskiy continue; 57853018216SPaolo Bonzini } 579fb164994SDavid Gibson if (mem_start >= machine->ram_size) { 5805fe269b1SPaul Mackerras node_size = 0; 5815fe269b1SPaul Mackerras } else { 5827db8a127SAlexey Kardashevskiy node_size = nodes[i].node_mem; 583fb164994SDavid Gibson if (node_size > machine->ram_size - mem_start) { 584fb164994SDavid Gibson node_size = machine->ram_size - mem_start; 5855fe269b1SPaul Mackerras } 5865fe269b1SPaul Mackerras } 5877db8a127SAlexey Kardashevskiy if (!mem_start) { 5887db8a127SAlexey Kardashevskiy /* ppc_spapr_init() checks for rma_size <= node0_size already */ 5897db8a127SAlexey Kardashevskiy spapr_populate_memory_node(fdt, i, 0, spapr->rma_size); 5907db8a127SAlexey Kardashevskiy mem_start += spapr->rma_size; 5917db8a127SAlexey Kardashevskiy node_size -= spapr->rma_size; 5927db8a127SAlexey Kardashevskiy } 5936010818cSAlexey Kardashevskiy for ( ; node_size; ) { 5946010818cSAlexey Kardashevskiy hwaddr sizetmp = pow2floor(node_size); 5956010818cSAlexey Kardashevskiy 5966010818cSAlexey Kardashevskiy /* mem_start != 0 here */ 5976010818cSAlexey Kardashevskiy if (ctzl(mem_start) < ctzl(sizetmp)) { 5986010818cSAlexey Kardashevskiy sizetmp = 1ULL << ctzl(mem_start); 5996010818cSAlexey Kardashevskiy } 6006010818cSAlexey Kardashevskiy 6016010818cSAlexey Kardashevskiy spapr_populate_memory_node(fdt, i, mem_start, sizetmp); 6026010818cSAlexey Kardashevskiy node_size -= sizetmp; 6036010818cSAlexey Kardashevskiy mem_start += sizetmp; 6046010818cSAlexey Kardashevskiy } 60553018216SPaolo Bonzini } 60653018216SPaolo Bonzini 60753018216SPaolo Bonzini return 0; 60853018216SPaolo Bonzini } 60953018216SPaolo Bonzini 6100da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset, 6110da6f3feSBharata B Rao sPAPRMachineState *spapr) 6120da6f3feSBharata B Rao { 6130da6f3feSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(cs); 6140da6f3feSBharata B Rao CPUPPCState *env = &cpu->env; 6150da6f3feSBharata B Rao PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs); 6160da6f3feSBharata B Rao int index = ppc_get_vcpu_dt_id(cpu); 6170da6f3feSBharata B Rao uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40), 6180da6f3feSBharata B Rao 0xffffffff, 0xffffffff}; 6190da6f3feSBharata B Rao uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ; 6200da6f3feSBharata B Rao uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000; 6210da6f3feSBharata B Rao uint32_t page_sizes_prop[64]; 6220da6f3feSBharata B Rao size_t page_sizes_prop_size; 6230da6f3feSBharata B Rao QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL); 6240da6f3feSBharata B Rao unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0; 6250da6f3feSBharata B Rao uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1; 6260da6f3feSBharata B Rao uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)}; 6270da6f3feSBharata B Rao 6280da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "reg", index))); 6290da6f3feSBharata B Rao _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu"))); 6300da6f3feSBharata B Rao 6310da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR]))); 6320da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size", 6330da6f3feSBharata B Rao env->dcache_line_size))); 6340da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size", 6350da6f3feSBharata B Rao env->dcache_line_size))); 6360da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size", 6370da6f3feSBharata B Rao env->icache_line_size))); 6380da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size", 6390da6f3feSBharata B Rao env->icache_line_size))); 6400da6f3feSBharata B Rao 6410da6f3feSBharata B Rao if (pcc->l1_dcache_size) { 6420da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size", 6430da6f3feSBharata B Rao pcc->l1_dcache_size))); 6440da6f3feSBharata B Rao } else { 6450da6f3feSBharata B Rao fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n"); 6460da6f3feSBharata B Rao } 6470da6f3feSBharata B Rao if (pcc->l1_icache_size) { 6480da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size", 6490da6f3feSBharata B Rao pcc->l1_icache_size))); 6500da6f3feSBharata B Rao } else { 6510da6f3feSBharata B Rao fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n"); 6520da6f3feSBharata B Rao } 6530da6f3feSBharata B Rao 6540da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq))); 6550da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq))); 6560da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr))); 6570da6f3feSBharata B Rao _FDT((fdt_setprop_string(fdt, offset, "status", "okay"))); 6580da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0))); 6590da6f3feSBharata B Rao 6600da6f3feSBharata B Rao if (env->spr_cb[SPR_PURR].oea_read) { 6610da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0))); 6620da6f3feSBharata B Rao } 6630da6f3feSBharata B Rao 6640da6f3feSBharata B Rao if (env->mmu_model & POWERPC_MMU_1TSEG) { 6650da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes", 6660da6f3feSBharata B Rao segs, sizeof(segs)))); 6670da6f3feSBharata B Rao } 6680da6f3feSBharata B Rao 6690da6f3feSBharata B Rao /* Advertise VMX/VSX (vector extensions) if available 6700da6f3feSBharata B Rao * 0 / no property == no vector extensions 6710da6f3feSBharata B Rao * 1 == VMX / Altivec available 6720da6f3feSBharata B Rao * 2 == VSX available */ 6730da6f3feSBharata B Rao if (env->insns_flags & PPC_ALTIVEC) { 6740da6f3feSBharata B Rao uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1; 6750da6f3feSBharata B Rao 6760da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx))); 6770da6f3feSBharata B Rao } 6780da6f3feSBharata B Rao 6790da6f3feSBharata B Rao /* Advertise DFP (Decimal Floating Point) if available 6800da6f3feSBharata B Rao * 0 / no property == no DFP 6810da6f3feSBharata B Rao * 1 == DFP available */ 6820da6f3feSBharata B Rao if (env->insns_flags2 & PPC2_DFP) { 6830da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1))); 6840da6f3feSBharata B Rao } 6850da6f3feSBharata B Rao 6860da6f3feSBharata B Rao page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop, 6870da6f3feSBharata B Rao sizeof(page_sizes_prop)); 6880da6f3feSBharata B Rao if (page_sizes_prop_size) { 6890da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes", 6900da6f3feSBharata B Rao page_sizes_prop, page_sizes_prop_size))); 6910da6f3feSBharata B Rao } 6920da6f3feSBharata B Rao 6930da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id", 6940da6f3feSBharata B Rao cs->cpu_index / cpus_per_socket))); 6950da6f3feSBharata B Rao 6960da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,pft-size", 6970da6f3feSBharata B Rao pft_size_prop, sizeof(pft_size_prop)))); 6980da6f3feSBharata B Rao 6990da6f3feSBharata B Rao _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs)); 7000da6f3feSBharata B Rao 7010da6f3feSBharata B Rao _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, 7020da6f3feSBharata B Rao ppc_get_compat_smt_threads(cpu))); 7030da6f3feSBharata B Rao } 7040da6f3feSBharata B Rao 7050da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr) 7060da6f3feSBharata B Rao { 7070da6f3feSBharata B Rao CPUState *cs; 7080da6f3feSBharata B Rao int cpus_offset; 7090da6f3feSBharata B Rao char *nodename; 7100da6f3feSBharata B Rao int smt = kvmppc_smt_threads(); 7110da6f3feSBharata B Rao 7120da6f3feSBharata B Rao cpus_offset = fdt_add_subnode(fdt, 0, "cpus"); 7130da6f3feSBharata B Rao _FDT(cpus_offset); 7140da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1))); 7150da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0))); 7160da6f3feSBharata B Rao 7170da6f3feSBharata B Rao /* 7180da6f3feSBharata B Rao * We walk the CPUs in reverse order to ensure that CPU DT nodes 7190da6f3feSBharata B Rao * created by fdt_add_subnode() end up in the right order in FDT 7200da6f3feSBharata B Rao * for the guest kernel the enumerate the CPUs correctly. 7210da6f3feSBharata B Rao */ 7220da6f3feSBharata B Rao CPU_FOREACH_REVERSE(cs) { 7230da6f3feSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(cs); 7240da6f3feSBharata B Rao int index = ppc_get_vcpu_dt_id(cpu); 7250da6f3feSBharata B Rao DeviceClass *dc = DEVICE_GET_CLASS(cs); 7260da6f3feSBharata B Rao int offset; 7270da6f3feSBharata B Rao 7280da6f3feSBharata B Rao if ((index % smt) != 0) { 7290da6f3feSBharata B Rao continue; 7300da6f3feSBharata B Rao } 7310da6f3feSBharata B Rao 7320da6f3feSBharata B Rao nodename = g_strdup_printf("%s@%x", dc->fw_name, index); 7330da6f3feSBharata B Rao offset = fdt_add_subnode(fdt, cpus_offset, nodename); 7340da6f3feSBharata B Rao g_free(nodename); 7350da6f3feSBharata B Rao _FDT(offset); 7360da6f3feSBharata B Rao spapr_populate_cpu_dt(cs, fdt, offset, spapr); 7370da6f3feSBharata B Rao } 7380da6f3feSBharata B Rao 7390da6f3feSBharata B Rao } 7400da6f3feSBharata B Rao 74128e02042SDavid Gibson static void spapr_finalize_fdt(sPAPRMachineState *spapr, 74253018216SPaolo Bonzini hwaddr fdt_addr, 74353018216SPaolo Bonzini hwaddr rtas_addr, 74453018216SPaolo Bonzini hwaddr rtas_size) 74553018216SPaolo Bonzini { 7465b2128d2SAlexander Graf MachineState *machine = MACHINE(qdev_get_machine()); 7475b2128d2SAlexander Graf const char *boot_device = machine->boot_order; 74871461b0fSAlexey Kardashevskiy int ret, i; 74971461b0fSAlexey Kardashevskiy size_t cb = 0; 75071461b0fSAlexey Kardashevskiy char *bootlist; 75153018216SPaolo Bonzini void *fdt; 75253018216SPaolo Bonzini sPAPRPHBState *phb; 75353018216SPaolo Bonzini 75453018216SPaolo Bonzini fdt = g_malloc(FDT_MAX_SIZE); 75553018216SPaolo Bonzini 75653018216SPaolo Bonzini /* open out the base tree into a temp buffer for the final tweaks */ 75753018216SPaolo Bonzini _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE))); 75853018216SPaolo Bonzini 75953018216SPaolo Bonzini ret = spapr_populate_memory(spapr, fdt); 76053018216SPaolo Bonzini if (ret < 0) { 76153018216SPaolo Bonzini fprintf(stderr, "couldn't setup memory nodes in fdt\n"); 76253018216SPaolo Bonzini exit(1); 76353018216SPaolo Bonzini } 76453018216SPaolo Bonzini 76553018216SPaolo Bonzini ret = spapr_populate_vdevice(spapr->vio_bus, fdt); 76653018216SPaolo Bonzini if (ret < 0) { 76753018216SPaolo Bonzini fprintf(stderr, "couldn't setup vio devices in fdt\n"); 76853018216SPaolo Bonzini exit(1); 76953018216SPaolo Bonzini } 77053018216SPaolo Bonzini 77153018216SPaolo Bonzini QLIST_FOREACH(phb, &spapr->phbs, list) { 77253018216SPaolo Bonzini ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt); 77353018216SPaolo Bonzini } 77453018216SPaolo Bonzini 77553018216SPaolo Bonzini if (ret < 0) { 77653018216SPaolo Bonzini fprintf(stderr, "couldn't setup PCI devices in fdt\n"); 77753018216SPaolo Bonzini exit(1); 77853018216SPaolo Bonzini } 77953018216SPaolo Bonzini 78053018216SPaolo Bonzini /* RTAS */ 78153018216SPaolo Bonzini ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size); 78253018216SPaolo Bonzini if (ret < 0) { 78353018216SPaolo Bonzini fprintf(stderr, "Couldn't set up RTAS device tree properties\n"); 78453018216SPaolo Bonzini } 78553018216SPaolo Bonzini 7860da6f3feSBharata B Rao /* cpus */ 7870da6f3feSBharata B Rao spapr_populate_cpus_dt_node(fdt, spapr); 78853018216SPaolo Bonzini 78971461b0fSAlexey Kardashevskiy bootlist = get_boot_devices_list(&cb, true); 79071461b0fSAlexey Kardashevskiy if (cb && bootlist) { 79171461b0fSAlexey Kardashevskiy int offset = fdt_path_offset(fdt, "/chosen"); 79271461b0fSAlexey Kardashevskiy if (offset < 0) { 79371461b0fSAlexey Kardashevskiy exit(1); 79471461b0fSAlexey Kardashevskiy } 79571461b0fSAlexey Kardashevskiy for (i = 0; i < cb; i++) { 79671461b0fSAlexey Kardashevskiy if (bootlist[i] == '\n') { 79771461b0fSAlexey Kardashevskiy bootlist[i] = ' '; 79871461b0fSAlexey Kardashevskiy } 79971461b0fSAlexey Kardashevskiy 80071461b0fSAlexey Kardashevskiy } 80171461b0fSAlexey Kardashevskiy ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist); 80271461b0fSAlexey Kardashevskiy } 80371461b0fSAlexey Kardashevskiy 8045b2128d2SAlexander Graf if (boot_device && strlen(boot_device)) { 8055b2128d2SAlexander Graf int offset = fdt_path_offset(fdt, "/chosen"); 8065b2128d2SAlexander Graf 8075b2128d2SAlexander Graf if (offset < 0) { 8085b2128d2SAlexander Graf exit(1); 8095b2128d2SAlexander Graf } 8105b2128d2SAlexander Graf fdt_setprop_string(fdt, offset, "qemu,boot-device", boot_device); 8115b2128d2SAlexander Graf } 8125b2128d2SAlexander Graf 81353018216SPaolo Bonzini if (!spapr->has_graphics) { 81453018216SPaolo Bonzini spapr_populate_chosen_stdout(fdt, spapr->vio_bus); 81553018216SPaolo Bonzini } 81653018216SPaolo Bonzini 81753018216SPaolo Bonzini _FDT((fdt_pack(fdt))); 81853018216SPaolo Bonzini 81953018216SPaolo Bonzini if (fdt_totalsize(fdt) > FDT_MAX_SIZE) { 820730fce59SThomas Huth error_report("FDT too big ! 0x%x bytes (max is 0x%x)", 82153018216SPaolo Bonzini fdt_totalsize(fdt), FDT_MAX_SIZE); 82253018216SPaolo Bonzini exit(1); 82353018216SPaolo Bonzini } 82453018216SPaolo Bonzini 82553018216SPaolo Bonzini cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt)); 82653018216SPaolo Bonzini 827a21a7a70SGonglei g_free(bootlist); 82853018216SPaolo Bonzini g_free(fdt); 82953018216SPaolo Bonzini } 83053018216SPaolo Bonzini 83153018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 83253018216SPaolo Bonzini { 83353018216SPaolo Bonzini return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR; 83453018216SPaolo Bonzini } 83553018216SPaolo Bonzini 83653018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu) 83753018216SPaolo Bonzini { 83853018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 83953018216SPaolo Bonzini 84053018216SPaolo Bonzini if (msr_pr) { 84153018216SPaolo Bonzini hcall_dprintf("Hypercall made with MSR[PR]=1\n"); 84253018216SPaolo Bonzini env->gpr[3] = H_PRIVILEGE; 84353018216SPaolo Bonzini } else { 84453018216SPaolo Bonzini env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]); 84553018216SPaolo Bonzini } 84653018216SPaolo Bonzini } 84753018216SPaolo Bonzini 848e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2)) 849e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID) 850e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY) 851e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY)) 852e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte) ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY)) 853e6b8fd24SSamuel Mendoza-Jonas 85428e02042SDavid Gibson static void spapr_reset_htab(sPAPRMachineState *spapr) 85553018216SPaolo Bonzini { 85653018216SPaolo Bonzini long shift; 857e6b8fd24SSamuel Mendoza-Jonas int index; 85853018216SPaolo Bonzini 85953018216SPaolo Bonzini /* allocate hash page table. For now we always make this 16mb, 86053018216SPaolo Bonzini * later we should probably make it scale to the size of guest 86153018216SPaolo Bonzini * RAM */ 86253018216SPaolo Bonzini 86353018216SPaolo Bonzini shift = kvmppc_reset_htab(spapr->htab_shift); 86453018216SPaolo Bonzini 86553018216SPaolo Bonzini if (shift > 0) { 86653018216SPaolo Bonzini /* Kernel handles htab, we don't need to allocate one */ 86753018216SPaolo Bonzini spapr->htab_shift = shift; 8687c43bca0SAneesh Kumar K.V kvmppc_kern_htab = true; 86901a57972SSamuel Mendoza-Jonas 87001a57972SSamuel Mendoza-Jonas /* Tell readers to update their file descriptor */ 87101a57972SSamuel Mendoza-Jonas if (spapr->htab_fd >= 0) { 87201a57972SSamuel Mendoza-Jonas spapr->htab_fd_stale = true; 87301a57972SSamuel Mendoza-Jonas } 87453018216SPaolo Bonzini } else { 87553018216SPaolo Bonzini if (!spapr->htab) { 87653018216SPaolo Bonzini /* Allocate an htab if we don't yet have one */ 87753018216SPaolo Bonzini spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr)); 87853018216SPaolo Bonzini } 87953018216SPaolo Bonzini 88053018216SPaolo Bonzini /* And clear it */ 88153018216SPaolo Bonzini memset(spapr->htab, 0, HTAB_SIZE(spapr)); 882e6b8fd24SSamuel Mendoza-Jonas 883e6b8fd24SSamuel Mendoza-Jonas for (index = 0; index < HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; index++) { 884e6b8fd24SSamuel Mendoza-Jonas DIRTY_HPTE(HPTE(spapr->htab, index)); 885e6b8fd24SSamuel Mendoza-Jonas } 88653018216SPaolo Bonzini } 88753018216SPaolo Bonzini 88853018216SPaolo Bonzini /* Update the RMA size if necessary */ 88953018216SPaolo Bonzini if (spapr->vrma_adjust) { 890b082d65aSAlexey Kardashevskiy spapr->rma_size = kvmppc_rma_size(spapr_node0_size(), 891b082d65aSAlexey Kardashevskiy spapr->htab_shift); 89253018216SPaolo Bonzini } 89353018216SPaolo Bonzini } 89453018216SPaolo Bonzini 8959e3f9733SAlexander Graf static int find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque) 8969e3f9733SAlexander Graf { 8979e3f9733SAlexander Graf bool matched = false; 8989e3f9733SAlexander Graf 8999e3f9733SAlexander Graf if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) { 9009e3f9733SAlexander Graf matched = true; 9019e3f9733SAlexander Graf } 9029e3f9733SAlexander Graf 9039e3f9733SAlexander Graf if (!matched) { 9049e3f9733SAlexander Graf error_report("Device %s is not supported by this machine yet.", 9059e3f9733SAlexander Graf qdev_fw_name(DEVICE(sbdev))); 9069e3f9733SAlexander Graf exit(1); 9079e3f9733SAlexander Graf } 9089e3f9733SAlexander Graf 9099e3f9733SAlexander Graf return 0; 9109e3f9733SAlexander Graf } 9119e3f9733SAlexander Graf 91201a57972SSamuel Mendoza-Jonas /* 91301a57972SSamuel Mendoza-Jonas * A guest reset will cause spapr->htab_fd to become stale if being used. 91401a57972SSamuel Mendoza-Jonas * Reopen the file descriptor to make sure the whole HTAB is properly read. 91501a57972SSamuel Mendoza-Jonas */ 91628e02042SDavid Gibson static int spapr_check_htab_fd(sPAPRMachineState *spapr) 91701a57972SSamuel Mendoza-Jonas { 91801a57972SSamuel Mendoza-Jonas int rc = 0; 91901a57972SSamuel Mendoza-Jonas 92001a57972SSamuel Mendoza-Jonas if (spapr->htab_fd_stale) { 92101a57972SSamuel Mendoza-Jonas close(spapr->htab_fd); 92201a57972SSamuel Mendoza-Jonas spapr->htab_fd = kvmppc_get_htab_fd(false); 92301a57972SSamuel Mendoza-Jonas if (spapr->htab_fd < 0) { 92401a57972SSamuel Mendoza-Jonas error_report("Unable to open fd for reading hash table from KVM: " 92501a57972SSamuel Mendoza-Jonas "%s", strerror(errno)); 92601a57972SSamuel Mendoza-Jonas rc = -1; 92701a57972SSamuel Mendoza-Jonas } 92801a57972SSamuel Mendoza-Jonas spapr->htab_fd_stale = false; 92901a57972SSamuel Mendoza-Jonas } 93001a57972SSamuel Mendoza-Jonas 93101a57972SSamuel Mendoza-Jonas return rc; 93201a57972SSamuel Mendoza-Jonas } 93301a57972SSamuel Mendoza-Jonas 93453018216SPaolo Bonzini static void ppc_spapr_reset(void) 93553018216SPaolo Bonzini { 93628e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 937182735efSAndreas Färber PowerPCCPU *first_ppc_cpu; 938b7d1f77aSBenjamin Herrenschmidt uint32_t rtas_limit; 939259186a7SAndreas Färber 9409e3f9733SAlexander Graf /* Check for unknown sysbus devices */ 9419e3f9733SAlexander Graf foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL); 9429e3f9733SAlexander Graf 94353018216SPaolo Bonzini /* Reset the hash table & recalc the RMA */ 94453018216SPaolo Bonzini spapr_reset_htab(spapr); 94553018216SPaolo Bonzini 94653018216SPaolo Bonzini qemu_devices_reset(); 94753018216SPaolo Bonzini 948b7d1f77aSBenjamin Herrenschmidt /* 949b7d1f77aSBenjamin Herrenschmidt * We place the device tree and RTAS just below either the top of the RMA, 950b7d1f77aSBenjamin Herrenschmidt * or just below 2GB, whichever is lowere, so that it can be 951b7d1f77aSBenjamin Herrenschmidt * processed with 32-bit real mode code if necessary 952b7d1f77aSBenjamin Herrenschmidt */ 953b7d1f77aSBenjamin Herrenschmidt rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR); 954b7d1f77aSBenjamin Herrenschmidt spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE; 955b7d1f77aSBenjamin Herrenschmidt spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE; 956b7d1f77aSBenjamin Herrenschmidt 95753018216SPaolo Bonzini /* Load the fdt */ 95853018216SPaolo Bonzini spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr, 95953018216SPaolo Bonzini spapr->rtas_size); 96053018216SPaolo Bonzini 961b7d1f77aSBenjamin Herrenschmidt /* Copy RTAS over */ 962b7d1f77aSBenjamin Herrenschmidt cpu_physical_memory_write(spapr->rtas_addr, spapr->rtas_blob, 963b7d1f77aSBenjamin Herrenschmidt spapr->rtas_size); 964b7d1f77aSBenjamin Herrenschmidt 96553018216SPaolo Bonzini /* Set up the entry state */ 966182735efSAndreas Färber first_ppc_cpu = POWERPC_CPU(first_cpu); 967182735efSAndreas Färber first_ppc_cpu->env.gpr[3] = spapr->fdt_addr; 968182735efSAndreas Färber first_ppc_cpu->env.gpr[5] = 0; 969182735efSAndreas Färber first_cpu->halted = 0; 9701b718907SDavid Gibson first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT; 97153018216SPaolo Bonzini 97253018216SPaolo Bonzini } 97353018216SPaolo Bonzini 97453018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque) 97553018216SPaolo Bonzini { 97628e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 97753018216SPaolo Bonzini PowerPCCPU *cpu = opaque; 978259186a7SAndreas Färber CPUState *cs = CPU(cpu); 97953018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 98053018216SPaolo Bonzini 981259186a7SAndreas Färber cpu_reset(cs); 98253018216SPaolo Bonzini 98353018216SPaolo Bonzini /* All CPUs start halted. CPU0 is unhalted from the machine level 98453018216SPaolo Bonzini * reset code and the rest are explicitly started up by the guest 98553018216SPaolo Bonzini * using an RTAS call */ 986259186a7SAndreas Färber cs->halted = 1; 98753018216SPaolo Bonzini 98853018216SPaolo Bonzini env->spr[SPR_HIOR] = 0; 98953018216SPaolo Bonzini 9904be21d56SDavid Gibson env->external_htab = (uint8_t *)spapr->htab; 9915736245cSAneesh Kumar K.V if (kvm_enabled() && !env->external_htab) { 9925736245cSAneesh Kumar K.V /* 9935736245cSAneesh Kumar K.V * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte* 9945736245cSAneesh Kumar K.V * functions do the right thing. 9955736245cSAneesh Kumar K.V */ 9965736245cSAneesh Kumar K.V env->external_htab = (void *)1; 9975736245cSAneesh Kumar K.V } 99853018216SPaolo Bonzini env->htab_base = -1; 999f3c75d42SAneesh Kumar K.V /* 1000f3c75d42SAneesh Kumar K.V * htab_mask is the mask used to normalize hash value to PTEG index. 1001f3c75d42SAneesh Kumar K.V * htab_shift is log2 of hash table size. 1002f3c75d42SAneesh Kumar K.V * We have 8 hpte per group, and each hpte is 16 bytes. 1003f3c75d42SAneesh Kumar K.V * ie have 128 bytes per hpte entry. 1004f3c75d42SAneesh Kumar K.V */ 100528e02042SDavid Gibson env->htab_mask = (1ULL << (spapr->htab_shift - 7)) - 1; 1006ec4936e1SStefan Weil env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab | 100753018216SPaolo Bonzini (spapr->htab_shift - 18); 100853018216SPaolo Bonzini } 100953018216SPaolo Bonzini 101028e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr) 101153018216SPaolo Bonzini { 10122ff3de68SMarkus Armbruster DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram"); 10133978b863SPaolo Bonzini DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0); 101453018216SPaolo Bonzini 10153978b863SPaolo Bonzini if (dinfo) { 10164be74634SMarkus Armbruster qdev_prop_set_drive_nofail(dev, "drive", blk_by_legacy_dinfo(dinfo)); 101753018216SPaolo Bonzini } 101853018216SPaolo Bonzini 101953018216SPaolo Bonzini qdev_init_nofail(dev); 102053018216SPaolo Bonzini 102153018216SPaolo Bonzini spapr->nvram = (struct sPAPRNVRAM *)dev; 102253018216SPaolo Bonzini } 102353018216SPaolo Bonzini 102428e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr) 102528df36a1SDavid Gibson { 102628df36a1SDavid Gibson DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC); 102728df36a1SDavid Gibson 102828df36a1SDavid Gibson qdev_init_nofail(dev); 102928df36a1SDavid Gibson spapr->rtc = dev; 103074e5ae28SDavid Gibson 103174e5ae28SDavid Gibson object_property_add_alias(qdev_get_machine(), "rtc-time", 103274e5ae28SDavid Gibson OBJECT(spapr->rtc), "date", NULL); 103328df36a1SDavid Gibson } 103428df36a1SDavid Gibson 103553018216SPaolo Bonzini /* Returns whether we want to use VGA or not */ 103653018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus) 103753018216SPaolo Bonzini { 103853018216SPaolo Bonzini switch (vga_interface_type) { 103953018216SPaolo Bonzini case VGA_NONE: 10407effdaa3SMark Wu return false; 10417effdaa3SMark Wu case VGA_DEVICE: 10427effdaa3SMark Wu return true; 104353018216SPaolo Bonzini case VGA_STD: 104453018216SPaolo Bonzini return pci_vga_init(pci_bus) != NULL; 104553018216SPaolo Bonzini default: 104653018216SPaolo Bonzini fprintf(stderr, "This vga model is not supported," 104753018216SPaolo Bonzini "currently it only supports -vga std\n"); 104853018216SPaolo Bonzini exit(0); 104953018216SPaolo Bonzini } 105053018216SPaolo Bonzini } 105153018216SPaolo Bonzini 1052880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id) 1053880ae7deSDavid Gibson { 105428e02042SDavid Gibson sPAPRMachineState *spapr = (sPAPRMachineState *)opaque; 1055880ae7deSDavid Gibson int err = 0; 1056880ae7deSDavid Gibson 1057631b22eaSStefan Weil /* In earlier versions, there was no separate qdev for the PAPR 1058880ae7deSDavid Gibson * RTC, so the RTC offset was stored directly in sPAPREnvironment. 1059880ae7deSDavid Gibson * So when migrating from those versions, poke the incoming offset 1060880ae7deSDavid Gibson * value into the RTC device */ 1061880ae7deSDavid Gibson if (version_id < 3) { 1062880ae7deSDavid Gibson err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset); 1063880ae7deSDavid Gibson } 1064880ae7deSDavid Gibson 1065880ae7deSDavid Gibson return err; 1066880ae7deSDavid Gibson } 1067880ae7deSDavid Gibson 1068880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id) 1069880ae7deSDavid Gibson { 1070880ae7deSDavid Gibson return version_id < 3; 1071880ae7deSDavid Gibson } 1072880ae7deSDavid Gibson 10734be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = { 10744be21d56SDavid Gibson .name = "spapr", 1075880ae7deSDavid Gibson .version_id = 3, 10764be21d56SDavid Gibson .minimum_version_id = 1, 1077880ae7deSDavid Gibson .post_load = spapr_post_load, 10784be21d56SDavid Gibson .fields = (VMStateField[]) { 1079880ae7deSDavid Gibson /* used to be @next_irq */ 1080880ae7deSDavid Gibson VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4), 10814be21d56SDavid Gibson 10824be21d56SDavid Gibson /* RTC offset */ 108328e02042SDavid Gibson VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3), 1084880ae7deSDavid Gibson 108528e02042SDavid Gibson VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2), 10864be21d56SDavid Gibson VMSTATE_END_OF_LIST() 10874be21d56SDavid Gibson }, 10884be21d56SDavid Gibson }; 10894be21d56SDavid Gibson 10904be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque) 10914be21d56SDavid Gibson { 109228e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 10934be21d56SDavid Gibson 10944be21d56SDavid Gibson /* "Iteration" header */ 10954be21d56SDavid Gibson qemu_put_be32(f, spapr->htab_shift); 10964be21d56SDavid Gibson 1097e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 1098e68cb8b4SAlexey Kardashevskiy spapr->htab_save_index = 0; 1099e68cb8b4SAlexey Kardashevskiy spapr->htab_first_pass = true; 1100e68cb8b4SAlexey Kardashevskiy } else { 1101e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1102e68cb8b4SAlexey Kardashevskiy 1103e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = kvmppc_get_htab_fd(false); 110401a57972SSamuel Mendoza-Jonas spapr->htab_fd_stale = false; 1105e68cb8b4SAlexey Kardashevskiy if (spapr->htab_fd < 0) { 1106e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n", 1107e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1108e68cb8b4SAlexey Kardashevskiy return -1; 1109e68cb8b4SAlexey Kardashevskiy } 11104be21d56SDavid Gibson } 11114be21d56SDavid Gibson 1112e68cb8b4SAlexey Kardashevskiy 1113e68cb8b4SAlexey Kardashevskiy return 0; 1114e68cb8b4SAlexey Kardashevskiy } 11154be21d56SDavid Gibson 111628e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr, 11174be21d56SDavid Gibson int64_t max_ns) 11184be21d56SDavid Gibson { 11194be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 11204be21d56SDavid Gibson int index = spapr->htab_save_index; 1121bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 11224be21d56SDavid Gibson 11234be21d56SDavid Gibson assert(spapr->htab_first_pass); 11244be21d56SDavid Gibson 11254be21d56SDavid Gibson do { 11264be21d56SDavid Gibson int chunkstart; 11274be21d56SDavid Gibson 11284be21d56SDavid Gibson /* Consume invalid HPTEs */ 11294be21d56SDavid Gibson while ((index < htabslots) 11304be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 11314be21d56SDavid Gibson index++; 11324be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 11334be21d56SDavid Gibson } 11344be21d56SDavid Gibson 11354be21d56SDavid Gibson /* Consume valid HPTEs */ 11364be21d56SDavid Gibson chunkstart = index; 1137338c25b6SSamuel Mendoza-Jonas while ((index < htabslots) && (index - chunkstart < USHRT_MAX) 11384be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 11394be21d56SDavid Gibson index++; 11404be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 11414be21d56SDavid Gibson } 11424be21d56SDavid Gibson 11434be21d56SDavid Gibson if (index > chunkstart) { 11444be21d56SDavid Gibson int n_valid = index - chunkstart; 11454be21d56SDavid Gibson 11464be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 11474be21d56SDavid Gibson qemu_put_be16(f, n_valid); 11484be21d56SDavid Gibson qemu_put_be16(f, 0); 11494be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 11504be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 11514be21d56SDavid Gibson 1152bc72ad67SAlex Bligh if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 11534be21d56SDavid Gibson break; 11544be21d56SDavid Gibson } 11554be21d56SDavid Gibson } 11564be21d56SDavid Gibson } while ((index < htabslots) && !qemu_file_rate_limit(f)); 11574be21d56SDavid Gibson 11584be21d56SDavid Gibson if (index >= htabslots) { 11594be21d56SDavid Gibson assert(index == htabslots); 11604be21d56SDavid Gibson index = 0; 11614be21d56SDavid Gibson spapr->htab_first_pass = false; 11624be21d56SDavid Gibson } 11634be21d56SDavid Gibson spapr->htab_save_index = index; 11644be21d56SDavid Gibson } 11654be21d56SDavid Gibson 116628e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr, 11674be21d56SDavid Gibson int64_t max_ns) 11684be21d56SDavid Gibson { 11694be21d56SDavid Gibson bool final = max_ns < 0; 11704be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 11714be21d56SDavid Gibson int examined = 0, sent = 0; 11724be21d56SDavid Gibson int index = spapr->htab_save_index; 1173bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 11744be21d56SDavid Gibson 11754be21d56SDavid Gibson assert(!spapr->htab_first_pass); 11764be21d56SDavid Gibson 11774be21d56SDavid Gibson do { 11784be21d56SDavid Gibson int chunkstart, invalidstart; 11794be21d56SDavid Gibson 11804be21d56SDavid Gibson /* Consume non-dirty HPTEs */ 11814be21d56SDavid Gibson while ((index < htabslots) 11824be21d56SDavid Gibson && !HPTE_DIRTY(HPTE(spapr->htab, index))) { 11834be21d56SDavid Gibson index++; 11844be21d56SDavid Gibson examined++; 11854be21d56SDavid Gibson } 11864be21d56SDavid Gibson 11874be21d56SDavid Gibson chunkstart = index; 11884be21d56SDavid Gibson /* Consume valid dirty HPTEs */ 1189338c25b6SSamuel Mendoza-Jonas while ((index < htabslots) && (index - chunkstart < USHRT_MAX) 11904be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 11914be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 11924be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 11934be21d56SDavid Gibson index++; 11944be21d56SDavid Gibson examined++; 11954be21d56SDavid Gibson } 11964be21d56SDavid Gibson 11974be21d56SDavid Gibson invalidstart = index; 11984be21d56SDavid Gibson /* Consume invalid dirty HPTEs */ 1199338c25b6SSamuel Mendoza-Jonas while ((index < htabslots) && (index - invalidstart < USHRT_MAX) 12004be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 12014be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 12024be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 12034be21d56SDavid Gibson index++; 12044be21d56SDavid Gibson examined++; 12054be21d56SDavid Gibson } 12064be21d56SDavid Gibson 12074be21d56SDavid Gibson if (index > chunkstart) { 12084be21d56SDavid Gibson int n_valid = invalidstart - chunkstart; 12094be21d56SDavid Gibson int n_invalid = index - invalidstart; 12104be21d56SDavid Gibson 12114be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 12124be21d56SDavid Gibson qemu_put_be16(f, n_valid); 12134be21d56SDavid Gibson qemu_put_be16(f, n_invalid); 12144be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 12154be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 12164be21d56SDavid Gibson sent += index - chunkstart; 12174be21d56SDavid Gibson 1218bc72ad67SAlex Bligh if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 12194be21d56SDavid Gibson break; 12204be21d56SDavid Gibson } 12214be21d56SDavid Gibson } 12224be21d56SDavid Gibson 12234be21d56SDavid Gibson if (examined >= htabslots) { 12244be21d56SDavid Gibson break; 12254be21d56SDavid Gibson } 12264be21d56SDavid Gibson 12274be21d56SDavid Gibson if (index >= htabslots) { 12284be21d56SDavid Gibson assert(index == htabslots); 12294be21d56SDavid Gibson index = 0; 12304be21d56SDavid Gibson } 12314be21d56SDavid Gibson } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final)); 12324be21d56SDavid Gibson 12334be21d56SDavid Gibson if (index >= htabslots) { 12344be21d56SDavid Gibson assert(index == htabslots); 12354be21d56SDavid Gibson index = 0; 12364be21d56SDavid Gibson } 12374be21d56SDavid Gibson 12384be21d56SDavid Gibson spapr->htab_save_index = index; 12394be21d56SDavid Gibson 1240e68cb8b4SAlexey Kardashevskiy return (examined >= htabslots) && (sent == 0) ? 1 : 0; 12414be21d56SDavid Gibson } 12424be21d56SDavid Gibson 1243e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS 5000000 /* 5 ms */ 1244e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE 2048 1245e68cb8b4SAlexey Kardashevskiy 12464be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque) 12474be21d56SDavid Gibson { 124828e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 1249e68cb8b4SAlexey Kardashevskiy int rc = 0; 12504be21d56SDavid Gibson 12514be21d56SDavid Gibson /* Iteration header */ 12524be21d56SDavid Gibson qemu_put_be32(f, 0); 12534be21d56SDavid Gibson 1254e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1255e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1256e68cb8b4SAlexey Kardashevskiy 125701a57972SSamuel Mendoza-Jonas rc = spapr_check_htab_fd(spapr); 125801a57972SSamuel Mendoza-Jonas if (rc < 0) { 125901a57972SSamuel Mendoza-Jonas return rc; 126001a57972SSamuel Mendoza-Jonas } 126101a57972SSamuel Mendoza-Jonas 1262e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, 1263e68cb8b4SAlexey Kardashevskiy MAX_KVM_BUF_SIZE, MAX_ITERATION_NS); 1264e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1265e68cb8b4SAlexey Kardashevskiy return rc; 1266e68cb8b4SAlexey Kardashevskiy } 1267e68cb8b4SAlexey Kardashevskiy } else if (spapr->htab_first_pass) { 12684be21d56SDavid Gibson htab_save_first_pass(f, spapr, MAX_ITERATION_NS); 12694be21d56SDavid Gibson } else { 1270e68cb8b4SAlexey Kardashevskiy rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS); 12714be21d56SDavid Gibson } 12724be21d56SDavid Gibson 12734be21d56SDavid Gibson /* End marker */ 12744be21d56SDavid Gibson qemu_put_be32(f, 0); 12754be21d56SDavid Gibson qemu_put_be16(f, 0); 12764be21d56SDavid Gibson qemu_put_be16(f, 0); 12774be21d56SDavid Gibson 1278e68cb8b4SAlexey Kardashevskiy return rc; 12794be21d56SDavid Gibson } 12804be21d56SDavid Gibson 12814be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque) 12824be21d56SDavid Gibson { 128328e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 12844be21d56SDavid Gibson 12854be21d56SDavid Gibson /* Iteration header */ 12864be21d56SDavid Gibson qemu_put_be32(f, 0); 12874be21d56SDavid Gibson 1288e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1289e68cb8b4SAlexey Kardashevskiy int rc; 1290e68cb8b4SAlexey Kardashevskiy 1291e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1292e68cb8b4SAlexey Kardashevskiy 129301a57972SSamuel Mendoza-Jonas rc = spapr_check_htab_fd(spapr); 129401a57972SSamuel Mendoza-Jonas if (rc < 0) { 129501a57972SSamuel Mendoza-Jonas return rc; 129601a57972SSamuel Mendoza-Jonas } 129701a57972SSamuel Mendoza-Jonas 1298e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1); 1299e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1300e68cb8b4SAlexey Kardashevskiy return rc; 1301e68cb8b4SAlexey Kardashevskiy } 1302e68cb8b4SAlexey Kardashevskiy close(spapr->htab_fd); 1303e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = -1; 1304e68cb8b4SAlexey Kardashevskiy } else { 13054be21d56SDavid Gibson htab_save_later_pass(f, spapr, -1); 1306e68cb8b4SAlexey Kardashevskiy } 13074be21d56SDavid Gibson 13084be21d56SDavid Gibson /* End marker */ 13094be21d56SDavid Gibson qemu_put_be32(f, 0); 13104be21d56SDavid Gibson qemu_put_be16(f, 0); 13114be21d56SDavid Gibson qemu_put_be16(f, 0); 13124be21d56SDavid Gibson 13134be21d56SDavid Gibson return 0; 13144be21d56SDavid Gibson } 13154be21d56SDavid Gibson 13164be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id) 13174be21d56SDavid Gibson { 131828e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 13194be21d56SDavid Gibson uint32_t section_hdr; 1320e68cb8b4SAlexey Kardashevskiy int fd = -1; 13214be21d56SDavid Gibson 13224be21d56SDavid Gibson if (version_id < 1 || version_id > 1) { 13234be21d56SDavid Gibson fprintf(stderr, "htab_load() bad version\n"); 13244be21d56SDavid Gibson return -EINVAL; 13254be21d56SDavid Gibson } 13264be21d56SDavid Gibson 13274be21d56SDavid Gibson section_hdr = qemu_get_be32(f); 13284be21d56SDavid Gibson 13294be21d56SDavid Gibson if (section_hdr) { 13304be21d56SDavid Gibson /* First section, just the hash shift */ 13314be21d56SDavid Gibson if (spapr->htab_shift != section_hdr) { 13324be21d56SDavid Gibson return -EINVAL; 13334be21d56SDavid Gibson } 13344be21d56SDavid Gibson return 0; 13354be21d56SDavid Gibson } 13364be21d56SDavid Gibson 1337e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1338e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1339e68cb8b4SAlexey Kardashevskiy 1340e68cb8b4SAlexey Kardashevskiy fd = kvmppc_get_htab_fd(true); 1341e68cb8b4SAlexey Kardashevskiy if (fd < 0) { 1342e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n", 1343e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1344e68cb8b4SAlexey Kardashevskiy } 1345e68cb8b4SAlexey Kardashevskiy } 1346e68cb8b4SAlexey Kardashevskiy 13474be21d56SDavid Gibson while (true) { 13484be21d56SDavid Gibson uint32_t index; 13494be21d56SDavid Gibson uint16_t n_valid, n_invalid; 13504be21d56SDavid Gibson 13514be21d56SDavid Gibson index = qemu_get_be32(f); 13524be21d56SDavid Gibson n_valid = qemu_get_be16(f); 13534be21d56SDavid Gibson n_invalid = qemu_get_be16(f); 13544be21d56SDavid Gibson 13554be21d56SDavid Gibson if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) { 13564be21d56SDavid Gibson /* End of Stream */ 13574be21d56SDavid Gibson break; 13584be21d56SDavid Gibson } 13594be21d56SDavid Gibson 1360e68cb8b4SAlexey Kardashevskiy if ((index + n_valid + n_invalid) > 13614be21d56SDavid Gibson (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) { 13624be21d56SDavid Gibson /* Bad index in stream */ 13634be21d56SDavid Gibson fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) " 1364e68cb8b4SAlexey Kardashevskiy "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid, 1365e68cb8b4SAlexey Kardashevskiy spapr->htab_shift); 13664be21d56SDavid Gibson return -EINVAL; 13674be21d56SDavid Gibson } 13684be21d56SDavid Gibson 1369e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 13704be21d56SDavid Gibson if (n_valid) { 13714be21d56SDavid Gibson qemu_get_buffer(f, HPTE(spapr->htab, index), 13724be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 13734be21d56SDavid Gibson } 13744be21d56SDavid Gibson if (n_invalid) { 13754be21d56SDavid Gibson memset(HPTE(spapr->htab, index + n_valid), 0, 13764be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_invalid); 13774be21d56SDavid Gibson } 1378e68cb8b4SAlexey Kardashevskiy } else { 1379e68cb8b4SAlexey Kardashevskiy int rc; 1380e68cb8b4SAlexey Kardashevskiy 1381e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1382e68cb8b4SAlexey Kardashevskiy 1383e68cb8b4SAlexey Kardashevskiy rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid); 1384e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1385e68cb8b4SAlexey Kardashevskiy return rc; 1386e68cb8b4SAlexey Kardashevskiy } 1387e68cb8b4SAlexey Kardashevskiy } 1388e68cb8b4SAlexey Kardashevskiy } 1389e68cb8b4SAlexey Kardashevskiy 1390e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1391e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1392e68cb8b4SAlexey Kardashevskiy close(fd); 13934be21d56SDavid Gibson } 13944be21d56SDavid Gibson 13954be21d56SDavid Gibson return 0; 13964be21d56SDavid Gibson } 13974be21d56SDavid Gibson 13984be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = { 13994be21d56SDavid Gibson .save_live_setup = htab_save_setup, 14004be21d56SDavid Gibson .save_live_iterate = htab_save_iterate, 14014be21d56SDavid Gibson .save_live_complete = htab_save_complete, 14024be21d56SDavid Gibson .load_state = htab_load, 14034be21d56SDavid Gibson }; 14044be21d56SDavid Gibson 14055b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device, 14065b2128d2SAlexander Graf Error **errp) 14075b2128d2SAlexander Graf { 14085b2128d2SAlexander Graf MachineState *machine = MACHINE(qdev_get_machine()); 14095b2128d2SAlexander Graf machine->boot_order = g_strdup(boot_device); 14105b2128d2SAlexander Graf } 14115b2128d2SAlexander Graf 1412bab99ea0SBharata B Rao static void spapr_cpu_init(sPAPRMachineState *spapr, PowerPCCPU *cpu) 1413bab99ea0SBharata B Rao { 1414bab99ea0SBharata B Rao CPUPPCState *env = &cpu->env; 1415bab99ea0SBharata B Rao 1416bab99ea0SBharata B Rao /* Set time-base frequency to 512 MHz */ 1417bab99ea0SBharata B Rao cpu_ppc_tb_init(env, TIMEBASE_FREQ); 1418bab99ea0SBharata B Rao 1419bab99ea0SBharata B Rao /* PAPR always has exception vectors in RAM not ROM. To ensure this, 1420bab99ea0SBharata B Rao * MSR[IP] should never be set. 1421bab99ea0SBharata B Rao */ 1422bab99ea0SBharata B Rao env->msr_mask &= ~(1 << 6); 1423bab99ea0SBharata B Rao 1424bab99ea0SBharata B Rao /* Tell KVM that we're in PAPR mode */ 1425bab99ea0SBharata B Rao if (kvm_enabled()) { 1426bab99ea0SBharata B Rao kvmppc_set_papr(cpu); 1427bab99ea0SBharata B Rao } 1428bab99ea0SBharata B Rao 1429bab99ea0SBharata B Rao if (cpu->max_compat) { 1430bab99ea0SBharata B Rao if (ppc_set_compat(cpu, cpu->max_compat) < 0) { 1431bab99ea0SBharata B Rao exit(1); 1432bab99ea0SBharata B Rao } 1433bab99ea0SBharata B Rao } 1434bab99ea0SBharata B Rao 1435bab99ea0SBharata B Rao xics_cpu_setup(spapr->icp, cpu); 1436bab99ea0SBharata B Rao 1437bab99ea0SBharata B Rao qemu_register_reset(spapr_cpu_reset, cpu); 1438bab99ea0SBharata B Rao } 1439bab99ea0SBharata B Rao 144053018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */ 14413ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine) 144253018216SPaolo Bonzini { 144328e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(machine); 14443ef96221SMarcel Apfelbaum const char *kernel_filename = machine->kernel_filename; 14453ef96221SMarcel Apfelbaum const char *kernel_cmdline = machine->kernel_cmdline; 14463ef96221SMarcel Apfelbaum const char *initrd_filename = machine->initrd_filename; 144753018216SPaolo Bonzini PowerPCCPU *cpu; 144853018216SPaolo Bonzini PCIHostState *phb; 144953018216SPaolo Bonzini int i; 145053018216SPaolo Bonzini MemoryRegion *sysmem = get_system_memory(); 145153018216SPaolo Bonzini MemoryRegion *ram = g_new(MemoryRegion, 1); 1452658fa66bSAlexey Kardashevskiy MemoryRegion *rma_region; 1453658fa66bSAlexey Kardashevskiy void *rma = NULL; 145453018216SPaolo Bonzini hwaddr rma_alloc_size; 1455b082d65aSAlexey Kardashevskiy hwaddr node0_size = spapr_node0_size(); 145653018216SPaolo Bonzini uint32_t initrd_base = 0; 145753018216SPaolo Bonzini long kernel_size = 0, initrd_size = 0; 1458b7d1f77aSBenjamin Herrenschmidt long load_limit, fw_size; 145916457e7fSBenjamin Herrenschmidt bool kernel_le = false; 146053018216SPaolo Bonzini char *filename; 146153018216SPaolo Bonzini 146253018216SPaolo Bonzini msi_supported = true; 146353018216SPaolo Bonzini 146453018216SPaolo Bonzini QLIST_INIT(&spapr->phbs); 146553018216SPaolo Bonzini 146653018216SPaolo Bonzini cpu_ppc_hypercall = emulate_spapr_hypercall; 146753018216SPaolo Bonzini 146853018216SPaolo Bonzini /* Allocate RMA if necessary */ 1469658fa66bSAlexey Kardashevskiy rma_alloc_size = kvmppc_alloc_rma(&rma); 147053018216SPaolo Bonzini 147153018216SPaolo Bonzini if (rma_alloc_size == -1) { 1472730fce59SThomas Huth error_report("Unable to create RMA"); 147353018216SPaolo Bonzini exit(1); 147453018216SPaolo Bonzini } 147553018216SPaolo Bonzini 1476c4177479SAlexey Kardashevskiy if (rma_alloc_size && (rma_alloc_size < node0_size)) { 147753018216SPaolo Bonzini spapr->rma_size = rma_alloc_size; 147853018216SPaolo Bonzini } else { 1479c4177479SAlexey Kardashevskiy spapr->rma_size = node0_size; 148053018216SPaolo Bonzini 148153018216SPaolo Bonzini /* With KVM, we don't actually know whether KVM supports an 148253018216SPaolo Bonzini * unbounded RMA (PR KVM) or is limited by the hash table size 148353018216SPaolo Bonzini * (HV KVM using VRMA), so we always assume the latter 148453018216SPaolo Bonzini * 148553018216SPaolo Bonzini * In that case, we also limit the initial allocations for RTAS 148653018216SPaolo Bonzini * etc... to 256M since we have no way to know what the VRMA size 148753018216SPaolo Bonzini * is going to be as it depends on the size of the hash table 148853018216SPaolo Bonzini * isn't determined yet. 148953018216SPaolo Bonzini */ 149053018216SPaolo Bonzini if (kvm_enabled()) { 149153018216SPaolo Bonzini spapr->vrma_adjust = 1; 149253018216SPaolo Bonzini spapr->rma_size = MIN(spapr->rma_size, 0x10000000); 149353018216SPaolo Bonzini } 149453018216SPaolo Bonzini } 149553018216SPaolo Bonzini 1496c4177479SAlexey Kardashevskiy if (spapr->rma_size > node0_size) { 1497c4177479SAlexey Kardashevskiy fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n", 1498c4177479SAlexey Kardashevskiy spapr->rma_size); 1499c4177479SAlexey Kardashevskiy exit(1); 1500c4177479SAlexey Kardashevskiy } 1501c4177479SAlexey Kardashevskiy 1502b7d1f77aSBenjamin Herrenschmidt /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */ 1503b7d1f77aSBenjamin Herrenschmidt load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD; 150453018216SPaolo Bonzini 150553018216SPaolo Bonzini /* We aim for a hash table of size 1/128 the size of RAM. The 150653018216SPaolo Bonzini * normal rule of thumb is 1/64 the size of RAM, but that's much 150753018216SPaolo Bonzini * more than needed for the Linux guests we support. */ 150853018216SPaolo Bonzini spapr->htab_shift = 18; /* Minimum architected size */ 150953018216SPaolo Bonzini while (spapr->htab_shift <= 46) { 1510fb164994SDavid Gibson if ((1ULL << (spapr->htab_shift + 7)) >= machine->ram_size) { 151153018216SPaolo Bonzini break; 151253018216SPaolo Bonzini } 151353018216SPaolo Bonzini spapr->htab_shift++; 151453018216SPaolo Bonzini } 151553018216SPaolo Bonzini 15167b565160SDavid Gibson /* Set up Interrupt Controller before we create the VCPUs */ 1517446f16a6SMarcel Apfelbaum spapr->icp = xics_system_init(machine, 15189e734e3dSBharata B Rao DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(), 1519f303f117SGreg Kurz smp_threads), 15207b565160SDavid Gibson XICS_IRQS); 15217b565160SDavid Gibson 152253018216SPaolo Bonzini /* init CPUs */ 152319fb2c36SBharata B Rao if (machine->cpu_model == NULL) { 152419fb2c36SBharata B Rao machine->cpu_model = kvm_enabled() ? "host" : "POWER7"; 152553018216SPaolo Bonzini } 152653018216SPaolo Bonzini for (i = 0; i < smp_cpus; i++) { 152719fb2c36SBharata B Rao cpu = cpu_ppc_init(machine->cpu_model); 152853018216SPaolo Bonzini if (cpu == NULL) { 152953018216SPaolo Bonzini fprintf(stderr, "Unable to find PowerPC CPU definition\n"); 153053018216SPaolo Bonzini exit(1); 153153018216SPaolo Bonzini } 1532bab99ea0SBharata B Rao spapr_cpu_init(spapr, cpu); 153353018216SPaolo Bonzini } 153453018216SPaolo Bonzini 1535026bfd89SDavid Gibson if (kvm_enabled()) { 1536026bfd89SDavid Gibson /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */ 1537026bfd89SDavid Gibson kvmppc_enable_logical_ci_hcalls(); 1538026bfd89SDavid Gibson } 1539026bfd89SDavid Gibson 154053018216SPaolo Bonzini /* allocate RAM */ 1541f92f5da1SAlexey Kardashevskiy memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram", 1542fb164994SDavid Gibson machine->ram_size); 1543f92f5da1SAlexey Kardashevskiy memory_region_add_subregion(sysmem, 0, ram); 154453018216SPaolo Bonzini 1545658fa66bSAlexey Kardashevskiy if (rma_alloc_size && rma) { 1546658fa66bSAlexey Kardashevskiy rma_region = g_new(MemoryRegion, 1); 1547658fa66bSAlexey Kardashevskiy memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma", 1548658fa66bSAlexey Kardashevskiy rma_alloc_size, rma); 1549658fa66bSAlexey Kardashevskiy vmstate_register_ram_global(rma_region); 1550658fa66bSAlexey Kardashevskiy memory_region_add_subregion(sysmem, 0, rma_region); 1551658fa66bSAlexey Kardashevskiy } 1552658fa66bSAlexey Kardashevskiy 155353018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin"); 15544c56440dSStefan Weil if (!filename) { 1555730fce59SThomas Huth error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin"); 15564c56440dSStefan Weil exit(1); 15574c56440dSStefan Weil } 1558b7d1f77aSBenjamin Herrenschmidt spapr->rtas_size = get_image_size(filename); 1559b7d1f77aSBenjamin Herrenschmidt spapr->rtas_blob = g_malloc(spapr->rtas_size); 1560b7d1f77aSBenjamin Herrenschmidt if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) { 1561730fce59SThomas Huth error_report("Could not load LPAR rtas '%s'", filename); 156253018216SPaolo Bonzini exit(1); 156353018216SPaolo Bonzini } 156453018216SPaolo Bonzini if (spapr->rtas_size > RTAS_MAX_SIZE) { 1565730fce59SThomas Huth error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)", 15662f285bddSPeter Maydell (size_t)spapr->rtas_size, RTAS_MAX_SIZE); 156753018216SPaolo Bonzini exit(1); 156853018216SPaolo Bonzini } 156953018216SPaolo Bonzini g_free(filename); 157053018216SPaolo Bonzini 157153018216SPaolo Bonzini /* Set up EPOW events infrastructure */ 157253018216SPaolo Bonzini spapr_events_init(spapr); 157353018216SPaolo Bonzini 157412f42174SDavid Gibson /* Set up the RTC RTAS interfaces */ 157528df36a1SDavid Gibson spapr_rtc_create(spapr); 157612f42174SDavid Gibson 157753018216SPaolo Bonzini /* Set up VIO bus */ 157853018216SPaolo Bonzini spapr->vio_bus = spapr_vio_bus_init(); 157953018216SPaolo Bonzini 158053018216SPaolo Bonzini for (i = 0; i < MAX_SERIAL_PORTS; i++) { 158153018216SPaolo Bonzini if (serial_hds[i]) { 158253018216SPaolo Bonzini spapr_vty_create(spapr->vio_bus, serial_hds[i]); 158353018216SPaolo Bonzini } 158453018216SPaolo Bonzini } 158553018216SPaolo Bonzini 158653018216SPaolo Bonzini /* We always have at least the nvram device on VIO */ 158753018216SPaolo Bonzini spapr_create_nvram(spapr); 158853018216SPaolo Bonzini 158953018216SPaolo Bonzini /* Set up PCI */ 159053018216SPaolo Bonzini spapr_pci_rtas_init(); 159153018216SPaolo Bonzini 159289dfd6e1SDavid Gibson phb = spapr_create_phb(spapr, 0); 159353018216SPaolo Bonzini 159453018216SPaolo Bonzini for (i = 0; i < nb_nics; i++) { 159553018216SPaolo Bonzini NICInfo *nd = &nd_table[i]; 159653018216SPaolo Bonzini 159753018216SPaolo Bonzini if (!nd->model) { 159853018216SPaolo Bonzini nd->model = g_strdup("ibmveth"); 159953018216SPaolo Bonzini } 160053018216SPaolo Bonzini 160153018216SPaolo Bonzini if (strcmp(nd->model, "ibmveth") == 0) { 160253018216SPaolo Bonzini spapr_vlan_create(spapr->vio_bus, nd); 160353018216SPaolo Bonzini } else { 160429b358f9SDavid Gibson pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL); 160553018216SPaolo Bonzini } 160653018216SPaolo Bonzini } 160753018216SPaolo Bonzini 160853018216SPaolo Bonzini for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) { 160953018216SPaolo Bonzini spapr_vscsi_create(spapr->vio_bus); 161053018216SPaolo Bonzini } 161153018216SPaolo Bonzini 161253018216SPaolo Bonzini /* Graphics */ 161353018216SPaolo Bonzini if (spapr_vga_init(phb->bus)) { 161453018216SPaolo Bonzini spapr->has_graphics = true; 1615c6e76503SPaolo Bonzini machine->usb |= defaults_enabled() && !machine->usb_disabled; 161653018216SPaolo Bonzini } 161753018216SPaolo Bonzini 16184ee9ced9SMarcel Apfelbaum if (machine->usb) { 161953018216SPaolo Bonzini pci_create_simple(phb->bus, -1, "pci-ohci"); 1620c86580b8SMarkus Armbruster 162153018216SPaolo Bonzini if (spapr->has_graphics) { 1622c86580b8SMarkus Armbruster USBBus *usb_bus = usb_bus_find(-1); 1623c86580b8SMarkus Armbruster 1624c86580b8SMarkus Armbruster usb_create_simple(usb_bus, "usb-kbd"); 1625c86580b8SMarkus Armbruster usb_create_simple(usb_bus, "usb-mouse"); 162653018216SPaolo Bonzini } 162753018216SPaolo Bonzini } 162853018216SPaolo Bonzini 162953018216SPaolo Bonzini if (spapr->rma_size < (MIN_RMA_SLOF << 20)) { 163053018216SPaolo Bonzini fprintf(stderr, "qemu: pSeries SLOF firmware requires >= " 163153018216SPaolo Bonzini "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF); 163253018216SPaolo Bonzini exit(1); 163353018216SPaolo Bonzini } 163453018216SPaolo Bonzini 163553018216SPaolo Bonzini if (kernel_filename) { 163653018216SPaolo Bonzini uint64_t lowaddr = 0; 163753018216SPaolo Bonzini 163853018216SPaolo Bonzini kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL, 163953018216SPaolo Bonzini NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0); 16403b66da82SAlexey Kardashevskiy if (kernel_size == ELF_LOAD_WRONG_ENDIAN) { 164116457e7fSBenjamin Herrenschmidt kernel_size = load_elf(kernel_filename, 164216457e7fSBenjamin Herrenschmidt translate_kernel_address, NULL, 164316457e7fSBenjamin Herrenschmidt NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0); 164416457e7fSBenjamin Herrenschmidt kernel_le = kernel_size > 0; 164516457e7fSBenjamin Herrenschmidt } 164616457e7fSBenjamin Herrenschmidt if (kernel_size < 0) { 16473b66da82SAlexey Kardashevskiy fprintf(stderr, "qemu: error loading %s: %s\n", 16483b66da82SAlexey Kardashevskiy kernel_filename, load_elf_strerror(kernel_size)); 164953018216SPaolo Bonzini exit(1); 165053018216SPaolo Bonzini } 165153018216SPaolo Bonzini 165253018216SPaolo Bonzini /* load initrd */ 165353018216SPaolo Bonzini if (initrd_filename) { 165453018216SPaolo Bonzini /* Try to locate the initrd in the gap between the kernel 165553018216SPaolo Bonzini * and the firmware. Add a bit of space just in case 165653018216SPaolo Bonzini */ 165753018216SPaolo Bonzini initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff; 165853018216SPaolo Bonzini initrd_size = load_image_targphys(initrd_filename, initrd_base, 165953018216SPaolo Bonzini load_limit - initrd_base); 166053018216SPaolo Bonzini if (initrd_size < 0) { 166153018216SPaolo Bonzini fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 166253018216SPaolo Bonzini initrd_filename); 166353018216SPaolo Bonzini exit(1); 166453018216SPaolo Bonzini } 166553018216SPaolo Bonzini } else { 166653018216SPaolo Bonzini initrd_base = 0; 166753018216SPaolo Bonzini initrd_size = 0; 166853018216SPaolo Bonzini } 166953018216SPaolo Bonzini } 167053018216SPaolo Bonzini 16718e7ea787SAndreas Färber if (bios_name == NULL) { 16728e7ea787SAndreas Färber bios_name = FW_FILE_NAME; 16738e7ea787SAndreas Färber } 16748e7ea787SAndreas Färber filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 16754c56440dSStefan Weil if (!filename) { 167668fea5a0SThomas Huth error_report("Could not find LPAR firmware '%s'", bios_name); 16774c56440dSStefan Weil exit(1); 16784c56440dSStefan Weil } 167953018216SPaolo Bonzini fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE); 168068fea5a0SThomas Huth if (fw_size <= 0) { 168168fea5a0SThomas Huth error_report("Could not load LPAR firmware '%s'", filename); 168253018216SPaolo Bonzini exit(1); 168353018216SPaolo Bonzini } 168453018216SPaolo Bonzini g_free(filename); 168553018216SPaolo Bonzini 168628e02042SDavid Gibson /* FIXME: Should register things through the MachineState's qdev 168728e02042SDavid Gibson * interface, this is a legacy from the sPAPREnvironment structure 168828e02042SDavid Gibson * which predated MachineState but had a similar function */ 16894be21d56SDavid Gibson vmstate_register(NULL, 0, &vmstate_spapr, spapr); 16904be21d56SDavid Gibson register_savevm_live(NULL, "spapr/htab", -1, 1, 16914be21d56SDavid Gibson &savevm_htab_handlers, spapr); 16924be21d56SDavid Gibson 169353018216SPaolo Bonzini /* Prepare the device tree */ 16943bbf37f2SAndreas Färber spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size, 169516457e7fSBenjamin Herrenschmidt kernel_size, kernel_le, 169631fe14d1SNathan Fontenot kernel_cmdline, 169731fe14d1SNathan Fontenot spapr->check_exception_irq); 169853018216SPaolo Bonzini assert(spapr->fdt_skel != NULL); 16995b2128d2SAlexander Graf 170046503c2bSMichael Roth /* used by RTAS */ 170146503c2bSMichael Roth QTAILQ_INIT(&spapr->ccs_list); 170246503c2bSMichael Roth qemu_register_reset(spapr_ccs_reset_hook, spapr); 170346503c2bSMichael Roth 17045b2128d2SAlexander Graf qemu_register_boot_set(spapr_boot_set, spapr); 170553018216SPaolo Bonzini } 170653018216SPaolo Bonzini 1707135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type) 1708135a129aSAneesh Kumar K.V { 1709135a129aSAneesh Kumar K.V if (!vm_type) { 1710135a129aSAneesh Kumar K.V return 0; 1711135a129aSAneesh Kumar K.V } 1712135a129aSAneesh Kumar K.V 1713135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "HV")) { 1714135a129aSAneesh Kumar K.V return 1; 1715135a129aSAneesh Kumar K.V } 1716135a129aSAneesh Kumar K.V 1717135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "PR")) { 1718135a129aSAneesh Kumar K.V return 2; 1719135a129aSAneesh Kumar K.V } 1720135a129aSAneesh Kumar K.V 1721135a129aSAneesh Kumar K.V error_report("Unknown kvm-type specified '%s'", vm_type); 1722135a129aSAneesh Kumar K.V exit(1); 1723135a129aSAneesh Kumar K.V } 1724135a129aSAneesh Kumar K.V 172571461b0fSAlexey Kardashevskiy /* 1726627b84f4SGonglei * Implementation of an interface to adjust firmware path 172771461b0fSAlexey Kardashevskiy * for the bootindex property handling. 172871461b0fSAlexey Kardashevskiy */ 172971461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus, 173071461b0fSAlexey Kardashevskiy DeviceState *dev) 173171461b0fSAlexey Kardashevskiy { 173271461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \ 173371461b0fSAlexey Kardashevskiy ((type *)object_dynamic_cast(OBJECT(obj), (name))) 173471461b0fSAlexey Kardashevskiy SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE); 173571461b0fSAlexey Kardashevskiy sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE); 173671461b0fSAlexey Kardashevskiy 173771461b0fSAlexey Kardashevskiy if (d) { 173871461b0fSAlexey Kardashevskiy void *spapr = CAST(void, bus->parent, "spapr-vscsi"); 173971461b0fSAlexey Kardashevskiy VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI); 174071461b0fSAlexey Kardashevskiy USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE); 174171461b0fSAlexey Kardashevskiy 174271461b0fSAlexey Kardashevskiy if (spapr) { 174371461b0fSAlexey Kardashevskiy /* 174471461b0fSAlexey Kardashevskiy * Replace "channel@0/disk@0,0" with "disk@8000000000000000": 174571461b0fSAlexey Kardashevskiy * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun 174671461b0fSAlexey Kardashevskiy * in the top 16 bits of the 64-bit LUN 174771461b0fSAlexey Kardashevskiy */ 174871461b0fSAlexey Kardashevskiy unsigned id = 0x8000 | (d->id << 8) | d->lun; 174971461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 175071461b0fSAlexey Kardashevskiy (uint64_t)id << 48); 175171461b0fSAlexey Kardashevskiy } else if (virtio) { 175271461b0fSAlexey Kardashevskiy /* 175371461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (target << 8) | lun 175471461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 175571461b0fSAlexey Kardashevskiy * Note: the quote above is from SLOF and it is wrong, 175671461b0fSAlexey Kardashevskiy * the actual binding is: 175771461b0fSAlexey Kardashevskiy * swap 0100 or 10 << or 20 << ( target lun-id -- srplun ) 175871461b0fSAlexey Kardashevskiy */ 175971461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (d->id << 16) | d->lun; 176071461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 176171461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 176271461b0fSAlexey Kardashevskiy } else if (usb) { 176371461b0fSAlexey Kardashevskiy /* 176471461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (usb-port << 16) | lun 176571461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 176671461b0fSAlexey Kardashevskiy */ 176771461b0fSAlexey Kardashevskiy unsigned usb_port = atoi(usb->port->path); 176871461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (usb_port << 16) | d->lun; 176971461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 177071461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 177171461b0fSAlexey Kardashevskiy } 177271461b0fSAlexey Kardashevskiy } 177371461b0fSAlexey Kardashevskiy 177471461b0fSAlexey Kardashevskiy if (phb) { 177571461b0fSAlexey Kardashevskiy /* Replace "pci" with "pci@800000020000000" */ 177671461b0fSAlexey Kardashevskiy return g_strdup_printf("pci@%"PRIX64, phb->buid); 177771461b0fSAlexey Kardashevskiy } 177871461b0fSAlexey Kardashevskiy 177971461b0fSAlexey Kardashevskiy return NULL; 178071461b0fSAlexey Kardashevskiy } 178171461b0fSAlexey Kardashevskiy 178223825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp) 178323825581SEduardo Habkost { 178428e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(obj); 178523825581SEduardo Habkost 178628e02042SDavid Gibson return g_strdup(spapr->kvm_type); 178723825581SEduardo Habkost } 178823825581SEduardo Habkost 178923825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp) 179023825581SEduardo Habkost { 179128e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(obj); 179223825581SEduardo Habkost 179328e02042SDavid Gibson g_free(spapr->kvm_type); 179428e02042SDavid Gibson spapr->kvm_type = g_strdup(value); 179523825581SEduardo Habkost } 179623825581SEduardo Habkost 179723825581SEduardo Habkost static void spapr_machine_initfn(Object *obj) 179823825581SEduardo Habkost { 179923825581SEduardo Habkost object_property_add_str(obj, "kvm-type", 180023825581SEduardo Habkost spapr_get_kvm_type, spapr_set_kvm_type, NULL); 180149d2e648SMarcel Apfelbaum object_property_set_description(obj, "kvm-type", 180249d2e648SMarcel Apfelbaum "Specifies the KVM virtualization mode (HV, PR)", 180349d2e648SMarcel Apfelbaum NULL); 180423825581SEduardo Habkost } 180523825581SEduardo Habkost 180634316482SAlexey Kardashevskiy static void ppc_cpu_do_nmi_on_cpu(void *arg) 180734316482SAlexey Kardashevskiy { 180834316482SAlexey Kardashevskiy CPUState *cs = arg; 180934316482SAlexey Kardashevskiy 181034316482SAlexey Kardashevskiy cpu_synchronize_state(cs); 181134316482SAlexey Kardashevskiy ppc_cpu_do_system_reset(cs); 181234316482SAlexey Kardashevskiy } 181334316482SAlexey Kardashevskiy 181434316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp) 181534316482SAlexey Kardashevskiy { 181634316482SAlexey Kardashevskiy CPUState *cs; 181734316482SAlexey Kardashevskiy 181834316482SAlexey Kardashevskiy CPU_FOREACH(cs) { 181934316482SAlexey Kardashevskiy async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs); 182034316482SAlexey Kardashevskiy } 182134316482SAlexey Kardashevskiy } 182234316482SAlexey Kardashevskiy 182329ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data) 182453018216SPaolo Bonzini { 182529ee3247SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 182671461b0fSAlexey Kardashevskiy FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc); 182734316482SAlexey Kardashevskiy NMIClass *nc = NMI_CLASS(oc); 182829ee3247SAlexey Kardashevskiy 1829958db90cSMarcel Apfelbaum mc->init = ppc_spapr_init; 1830958db90cSMarcel Apfelbaum mc->reset = ppc_spapr_reset; 1831958db90cSMarcel Apfelbaum mc->block_default_type = IF_SCSI; 1832958db90cSMarcel Apfelbaum mc->max_cpus = MAX_CPUS; 1833958db90cSMarcel Apfelbaum mc->no_parallel = 1; 18345b2128d2SAlexander Graf mc->default_boot_order = ""; 1835a34944feSNikunj A Dadhania mc->default_ram_size = 512 * M_BYTE; 1836958db90cSMarcel Apfelbaum mc->kvm_type = spapr_kvm_type; 18379e3f9733SAlexander Graf mc->has_dynamic_sysbus = true; 1838e4024630SLaurent Vivier mc->pci_allow_0_address = true; 183900b4fbe2SMarcel Apfelbaum 184071461b0fSAlexey Kardashevskiy fwc->get_dev_path = spapr_get_fw_dev_path; 184134316482SAlexey Kardashevskiy nc->nmi_monitor_handler = spapr_nmi; 184253018216SPaolo Bonzini } 184353018216SPaolo Bonzini 184429ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = { 184529ee3247SAlexey Kardashevskiy .name = TYPE_SPAPR_MACHINE, 184629ee3247SAlexey Kardashevskiy .parent = TYPE_MACHINE, 18474aee7362SDavid Gibson .abstract = true, 18486ca1502eSAlexey Kardashevskiy .instance_size = sizeof(sPAPRMachineState), 184923825581SEduardo Habkost .instance_init = spapr_machine_initfn, 1850183930c0SDavid Gibson .class_size = sizeof(sPAPRMachineClass), 185129ee3247SAlexey Kardashevskiy .class_init = spapr_machine_class_init, 185271461b0fSAlexey Kardashevskiy .interfaces = (InterfaceInfo[]) { 185371461b0fSAlexey Kardashevskiy { TYPE_FW_PATH_PROVIDER }, 185434316482SAlexey Kardashevskiy { TYPE_NMI }, 185571461b0fSAlexey Kardashevskiy { } 185671461b0fSAlexey Kardashevskiy }, 185729ee3247SAlexey Kardashevskiy }; 185829ee3247SAlexey Kardashevskiy 185938ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \ 18607619c7b0SMichael Roth HW_COMPAT_2_3 \ 18617619c7b0SMichael Roth {\ 18627619c7b0SMichael Roth .driver = "spapr-pci-host-bridge",\ 18637619c7b0SMichael Roth .property = "dynamic-reconfiguration",\ 18647619c7b0SMichael Roth .value = "off",\ 18657619c7b0SMichael Roth }, 186638ff32c6SEduardo Habkost 1867b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \ 186838ff32c6SEduardo Habkost SPAPR_COMPAT_2_3 \ 18694dfd8eaaSEduardo Habkost HW_COMPAT_2_2 \ 1870b194df47SAlexey Kardashevskiy {\ 1871b194df47SAlexey Kardashevskiy .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,\ 1872b194df47SAlexey Kardashevskiy .property = "mem_win_size",\ 1873b194df47SAlexey Kardashevskiy .value = "0x20000000",\ 1874dd754bafSEduardo Habkost }, 1875b194df47SAlexey Kardashevskiy 1876b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_1 \ 18774dfd8eaaSEduardo Habkost SPAPR_COMPAT_2_2 \ 18784dfd8eaaSEduardo Habkost HW_COMPAT_2_1 1879b194df47SAlexey Kardashevskiy 1880d25228e7SJason Wang static void spapr_compat_2_3(Object *obj) 1881d25228e7SJason Wang { 1882ff14e817SDr. David Alan Gilbert savevm_skip_section_footers(); 188313d16814SJuan Quintela global_state_set_optional(); 1884d25228e7SJason Wang } 1885d25228e7SJason Wang 1886b0e966d0SJason Wang static void spapr_compat_2_2(Object *obj) 1887b0e966d0SJason Wang { 1888d25228e7SJason Wang spapr_compat_2_3(obj); 1889b0e966d0SJason Wang } 1890b0e966d0SJason Wang 1891b0e966d0SJason Wang static void spapr_compat_2_1(Object *obj) 1892b0e966d0SJason Wang { 1893b0e966d0SJason Wang spapr_compat_2_2(obj); 1894b0e966d0SJason Wang } 1895b0e966d0SJason Wang 1896d25228e7SJason Wang static void spapr_machine_2_3_instance_init(Object *obj) 1897d25228e7SJason Wang { 1898d25228e7SJason Wang spapr_compat_2_3(obj); 1899d25228e7SJason Wang spapr_machine_initfn(obj); 1900d25228e7SJason Wang } 1901d25228e7SJason Wang 1902b0e966d0SJason Wang static void spapr_machine_2_2_instance_init(Object *obj) 1903b0e966d0SJason Wang { 1904b0e966d0SJason Wang spapr_compat_2_2(obj); 1905b0e966d0SJason Wang spapr_machine_initfn(obj); 1906b0e966d0SJason Wang } 1907b0e966d0SJason Wang 1908b0e966d0SJason Wang static void spapr_machine_2_1_instance_init(Object *obj) 1909b0e966d0SJason Wang { 1910b0e966d0SJason Wang spapr_compat_2_1(obj); 1911b0e966d0SJason Wang spapr_machine_initfn(obj); 1912b0e966d0SJason Wang } 1913b0e966d0SJason Wang 19146026db45SAlexey Kardashevskiy static void spapr_machine_2_1_class_init(ObjectClass *oc, void *data) 19156026db45SAlexey Kardashevskiy { 19166026db45SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 191768a27b20SMichael S. Tsirkin static GlobalProperty compat_props[] = { 1918dd754bafSEduardo Habkost SPAPR_COMPAT_2_1 191968a27b20SMichael S. Tsirkin { /* end of list */ } 192068a27b20SMichael S. Tsirkin }; 19216026db45SAlexey Kardashevskiy 19226026db45SAlexey Kardashevskiy mc->name = "pseries-2.1"; 19236026db45SAlexey Kardashevskiy mc->desc = "pSeries Logical Partition (PAPR compliant) v2.1"; 192468a27b20SMichael S. Tsirkin mc->compat_props = compat_props; 19256026db45SAlexey Kardashevskiy } 19266026db45SAlexey Kardashevskiy 19276026db45SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_1_info = { 1928*b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.1"), 19296026db45SAlexey Kardashevskiy .parent = TYPE_SPAPR_MACHINE, 19306026db45SAlexey Kardashevskiy .class_init = spapr_machine_2_1_class_init, 1931b0e966d0SJason Wang .instance_init = spapr_machine_2_1_instance_init, 19326026db45SAlexey Kardashevskiy }; 19336026db45SAlexey Kardashevskiy 19344aee7362SDavid Gibson static void spapr_machine_2_2_class_init(ObjectClass *oc, void *data) 19354aee7362SDavid Gibson { 1936b194df47SAlexey Kardashevskiy static GlobalProperty compat_props[] = { 1937dd754bafSEduardo Habkost SPAPR_COMPAT_2_2 1938b194df47SAlexey Kardashevskiy { /* end of list */ } 1939b194df47SAlexey Kardashevskiy }; 19404aee7362SDavid Gibson MachineClass *mc = MACHINE_CLASS(oc); 19414aee7362SDavid Gibson 19424aee7362SDavid Gibson mc->name = "pseries-2.2"; 19434aee7362SDavid Gibson mc->desc = "pSeries Logical Partition (PAPR compliant) v2.2"; 1944b194df47SAlexey Kardashevskiy mc->compat_props = compat_props; 19454aee7362SDavid Gibson } 19464aee7362SDavid Gibson 19474aee7362SDavid Gibson static const TypeInfo spapr_machine_2_2_info = { 1948*b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.2"), 19494aee7362SDavid Gibson .parent = TYPE_SPAPR_MACHINE, 19504aee7362SDavid Gibson .class_init = spapr_machine_2_2_class_init, 1951b0e966d0SJason Wang .instance_init = spapr_machine_2_2_instance_init, 19524aee7362SDavid Gibson }; 19534aee7362SDavid Gibson 19543dab0244SAlexey Kardashevskiy static void spapr_machine_2_3_class_init(ObjectClass *oc, void *data) 19553dab0244SAlexey Kardashevskiy { 1956a1a45612SDavid Gibson static GlobalProperty compat_props[] = { 19577619c7b0SMichael Roth SPAPR_COMPAT_2_3 1958a1a45612SDavid Gibson { /* end of list */ } 1959a1a45612SDavid Gibson }; 19603dab0244SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 19613dab0244SAlexey Kardashevskiy 19623dab0244SAlexey Kardashevskiy mc->name = "pseries-2.3"; 19633dab0244SAlexey Kardashevskiy mc->desc = "pSeries Logical Partition (PAPR compliant) v2.3"; 1964a1a45612SDavid Gibson mc->compat_props = compat_props; 19653dab0244SAlexey Kardashevskiy } 19663dab0244SAlexey Kardashevskiy 19673dab0244SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_3_info = { 1968*b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.3"), 19693dab0244SAlexey Kardashevskiy .parent = TYPE_SPAPR_MACHINE, 19703dab0244SAlexey Kardashevskiy .class_init = spapr_machine_2_3_class_init, 1971d25228e7SJason Wang .instance_init = spapr_machine_2_3_instance_init, 1972d25228e7SJason Wang }; 1973d25228e7SJason Wang 1974d25228e7SJason Wang static void spapr_machine_2_4_class_init(ObjectClass *oc, void *data) 1975d25228e7SJason Wang { 1976d25228e7SJason Wang MachineClass *mc = MACHINE_CLASS(oc); 1977d25228e7SJason Wang 1978d25228e7SJason Wang mc->name = "pseries-2.4"; 1979d25228e7SJason Wang mc->desc = "pSeries Logical Partition (PAPR compliant) v2.4"; 1980d25228e7SJason Wang mc->alias = "pseries"; 1981d25228e7SJason Wang mc->is_default = 1; 1982d25228e7SJason Wang } 1983d25228e7SJason Wang 1984d25228e7SJason Wang static const TypeInfo spapr_machine_2_4_info = { 1985*b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.4"), 1986d25228e7SJason Wang .parent = TYPE_SPAPR_MACHINE, 1987d25228e7SJason Wang .class_init = spapr_machine_2_4_class_init, 19883dab0244SAlexey Kardashevskiy }; 19893dab0244SAlexey Kardashevskiy 199029ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void) 199129ee3247SAlexey Kardashevskiy { 199229ee3247SAlexey Kardashevskiy type_register_static(&spapr_machine_info); 19936026db45SAlexey Kardashevskiy type_register_static(&spapr_machine_2_1_info); 19944aee7362SDavid Gibson type_register_static(&spapr_machine_2_2_info); 19953dab0244SAlexey Kardashevskiy type_register_static(&spapr_machine_2_3_info); 1996d25228e7SJason Wang type_register_static(&spapr_machine_2_4_info); 199729ee3247SAlexey Kardashevskiy } 199829ee3247SAlexey Kardashevskiy 199929ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types) 2000