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 3782c1aaa81SSam Bobroff if (qemu_get_vm_name()) { 3792c1aaa81SSam Bobroff _FDT((fdt_property_string(fdt, "ibm,partition-name", 3802c1aaa81SSam Bobroff qemu_get_vm_name()))); 3812c1aaa81SSam Bobroff } 3822c1aaa81SSam Bobroff 38353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x2))); 38453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x2))); 38553018216SPaolo Bonzini 38653018216SPaolo Bonzini /* /chosen */ 38753018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "chosen"))); 38853018216SPaolo Bonzini 38953018216SPaolo Bonzini /* Set Form1_affinity */ 39053018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5)))); 39153018216SPaolo Bonzini 39253018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline))); 39353018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-start", 39453018216SPaolo Bonzini &start_prop, sizeof(start_prop)))); 39553018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-end", 39653018216SPaolo Bonzini &end_prop, sizeof(end_prop)))); 39753018216SPaolo Bonzini if (kernel_size) { 39853018216SPaolo Bonzini uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR), 39953018216SPaolo Bonzini cpu_to_be64(kernel_size) }; 40053018216SPaolo Bonzini 40153018216SPaolo Bonzini _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop)))); 40216457e7fSBenjamin Herrenschmidt if (little_endian) { 40316457e7fSBenjamin Herrenschmidt _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0))); 40416457e7fSBenjamin Herrenschmidt } 40553018216SPaolo Bonzini } 406cc84c0f3SAvik Sil if (boot_menu) { 407cc84c0f3SAvik Sil _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu))); 408cc84c0f3SAvik Sil } 40953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width))); 41053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height))); 41153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth))); 41253018216SPaolo Bonzini 41353018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 41453018216SPaolo Bonzini 41553018216SPaolo Bonzini /* RTAS */ 41653018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "rtas"))); 41753018216SPaolo Bonzini 418da95324eSAlexey Kardashevskiy if (!kvm_enabled() || kvmppc_spapr_use_multitce()) { 419da95324eSAlexey Kardashevskiy add_str(hypertas, "hcall-multi-tce"); 420da95324eSAlexey Kardashevskiy } 421a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str, 422a1d59c0fSAlexey Kardashevskiy hypertas->len))); 423a1d59c0fSAlexey Kardashevskiy g_string_free(hypertas, TRUE); 424a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str, 425a1d59c0fSAlexey Kardashevskiy qemu_hypertas->len))); 426a1d59c0fSAlexey Kardashevskiy g_string_free(qemu_hypertas, TRUE); 42753018216SPaolo Bonzini 42853018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,associativity-reference-points", 42953018216SPaolo Bonzini refpoints, sizeof(refpoints)))); 43053018216SPaolo Bonzini 43153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX))); 43279853e18STyrel Datwyler _FDT((fdt_property_cell(fdt, "rtas-event-scan-rate", 43379853e18STyrel Datwyler RTAS_EVENT_SCAN_RATE))); 43453018216SPaolo Bonzini 435*a95f9922SSam Bobroff if (msi_supported) { 436*a95f9922SSam Bobroff _FDT((fdt_property(fdt, "ibm,change-msix-capable", NULL, 0))); 437*a95f9922SSam Bobroff } 438*a95f9922SSam Bobroff 4392e14072fSNikunj A Dadhania /* 4409d632f5fSzhanghailiang * According to PAPR, rtas ibm,os-term does not guarantee a return 4412e14072fSNikunj A Dadhania * back to the guest cpu. 4422e14072fSNikunj A Dadhania * 4432e14072fSNikunj A Dadhania * While an additional ibm,extended-os-term property indicates that 4442e14072fSNikunj A Dadhania * rtas call return will always occur. Set this property. 4452e14072fSNikunj A Dadhania */ 4462e14072fSNikunj A Dadhania _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0))); 4472e14072fSNikunj A Dadhania 44853018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 44953018216SPaolo Bonzini 45053018216SPaolo Bonzini /* interrupt controller */ 45153018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "interrupt-controller"))); 45253018216SPaolo Bonzini 45353018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", 45453018216SPaolo Bonzini "PowerPC-External-Interrupt-Presentation"))); 45553018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp"))); 45653018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 45753018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges", 45853018216SPaolo Bonzini interrupt_server_ranges_prop, 45953018216SPaolo Bonzini sizeof(interrupt_server_ranges_prop)))); 46053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2))); 46153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP))); 46253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP))); 46353018216SPaolo Bonzini 46453018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 46553018216SPaolo Bonzini 46653018216SPaolo Bonzini /* vdevice */ 46753018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "vdevice"))); 46853018216SPaolo Bonzini 46953018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "vdevice"))); 47053018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice"))); 47153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 47253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 47353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2))); 47453018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 47553018216SPaolo Bonzini 47653018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 47753018216SPaolo Bonzini 47853018216SPaolo Bonzini /* event-sources */ 47953018216SPaolo Bonzini spapr_events_fdt_skel(fdt, epow_irq); 48053018216SPaolo Bonzini 481f7d69146SAlexander Graf /* /hypervisor node */ 482f7d69146SAlexander Graf if (kvm_enabled()) { 483f7d69146SAlexander Graf uint8_t hypercall[16]; 484f7d69146SAlexander Graf 485f7d69146SAlexander Graf /* indicate KVM hypercall interface */ 486f7d69146SAlexander Graf _FDT((fdt_begin_node(fdt, "hypervisor"))); 487f7d69146SAlexander Graf _FDT((fdt_property_string(fdt, "compatible", "linux,kvm"))); 488f7d69146SAlexander Graf if (kvmppc_has_cap_fixup_hcalls()) { 489f7d69146SAlexander Graf /* 490f7d69146SAlexander Graf * Older KVM versions with older guest kernels were broken with the 491f7d69146SAlexander Graf * magic page, don't allow the guest to map it. 492f7d69146SAlexander Graf */ 493f7d69146SAlexander Graf kvmppc_get_hypercall(first_cpu->env_ptr, hypercall, 494f7d69146SAlexander Graf sizeof(hypercall)); 495f7d69146SAlexander Graf _FDT((fdt_property(fdt, "hcall-instructions", hypercall, 496f7d69146SAlexander Graf sizeof(hypercall)))); 497f7d69146SAlexander Graf } 498f7d69146SAlexander Graf _FDT((fdt_end_node(fdt))); 499f7d69146SAlexander Graf } 500f7d69146SAlexander Graf 50153018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); /* close root node */ 50253018216SPaolo Bonzini _FDT((fdt_finish(fdt))); 50353018216SPaolo Bonzini 50453018216SPaolo Bonzini return fdt; 50553018216SPaolo Bonzini } 50653018216SPaolo Bonzini 50728e02042SDavid Gibson int spapr_h_cas_compose_response(sPAPRMachineState *spapr, 50828e02042SDavid Gibson target_ulong addr, target_ulong size) 5092a6593cbSAlexey Kardashevskiy { 5102a6593cbSAlexey Kardashevskiy void *fdt, *fdt_skel; 5112a6593cbSAlexey Kardashevskiy sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 }; 5122a6593cbSAlexey Kardashevskiy 5132a6593cbSAlexey Kardashevskiy size -= sizeof(hdr); 5142a6593cbSAlexey Kardashevskiy 5152a6593cbSAlexey Kardashevskiy /* Create sceleton */ 5162a6593cbSAlexey Kardashevskiy fdt_skel = g_malloc0(size); 5172a6593cbSAlexey Kardashevskiy _FDT((fdt_create(fdt_skel, size))); 5182a6593cbSAlexey Kardashevskiy _FDT((fdt_begin_node(fdt_skel, ""))); 5192a6593cbSAlexey Kardashevskiy _FDT((fdt_end_node(fdt_skel))); 5202a6593cbSAlexey Kardashevskiy _FDT((fdt_finish(fdt_skel))); 5212a6593cbSAlexey Kardashevskiy fdt = g_malloc0(size); 5222a6593cbSAlexey Kardashevskiy _FDT((fdt_open_into(fdt_skel, fdt, size))); 5232a6593cbSAlexey Kardashevskiy g_free(fdt_skel); 5242a6593cbSAlexey Kardashevskiy 5253794d548SAlexey Kardashevskiy /* Fix skeleton up */ 5263794d548SAlexey Kardashevskiy _FDT((spapr_fixup_cpu_dt(fdt, spapr))); 5272a6593cbSAlexey Kardashevskiy 5282a6593cbSAlexey Kardashevskiy /* Pack resulting tree */ 5292a6593cbSAlexey Kardashevskiy _FDT((fdt_pack(fdt))); 5302a6593cbSAlexey Kardashevskiy 5312a6593cbSAlexey Kardashevskiy if (fdt_totalsize(fdt) + sizeof(hdr) > size) { 5322a6593cbSAlexey Kardashevskiy trace_spapr_cas_failed(size); 5332a6593cbSAlexey Kardashevskiy return -1; 5342a6593cbSAlexey Kardashevskiy } 5352a6593cbSAlexey Kardashevskiy 5362a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr, &hdr, sizeof(hdr)); 5372a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt)); 5382a6593cbSAlexey Kardashevskiy trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr)); 5392a6593cbSAlexey Kardashevskiy g_free(fdt); 5402a6593cbSAlexey Kardashevskiy 5412a6593cbSAlexey Kardashevskiy return 0; 5422a6593cbSAlexey Kardashevskiy } 5432a6593cbSAlexey Kardashevskiy 54426a8c353SAlexey Kardashevskiy static void spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start, 54526a8c353SAlexey Kardashevskiy hwaddr size) 54626a8c353SAlexey Kardashevskiy { 54726a8c353SAlexey Kardashevskiy uint32_t associativity[] = { 54826a8c353SAlexey Kardashevskiy cpu_to_be32(0x4), /* length */ 54926a8c353SAlexey Kardashevskiy cpu_to_be32(0x0), cpu_to_be32(0x0), 550c3b4f589SAlexey Kardashevskiy cpu_to_be32(0x0), cpu_to_be32(nodeid) 55126a8c353SAlexey Kardashevskiy }; 55226a8c353SAlexey Kardashevskiy char mem_name[32]; 55326a8c353SAlexey Kardashevskiy uint64_t mem_reg_property[2]; 55426a8c353SAlexey Kardashevskiy int off; 55526a8c353SAlexey Kardashevskiy 55626a8c353SAlexey Kardashevskiy mem_reg_property[0] = cpu_to_be64(start); 55726a8c353SAlexey Kardashevskiy mem_reg_property[1] = cpu_to_be64(size); 55826a8c353SAlexey Kardashevskiy 55926a8c353SAlexey Kardashevskiy sprintf(mem_name, "memory@" TARGET_FMT_lx, start); 56026a8c353SAlexey Kardashevskiy off = fdt_add_subnode(fdt, 0, mem_name); 56126a8c353SAlexey Kardashevskiy _FDT(off); 56226a8c353SAlexey Kardashevskiy _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 56326a8c353SAlexey Kardashevskiy _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 56426a8c353SAlexey Kardashevskiy sizeof(mem_reg_property)))); 56526a8c353SAlexey Kardashevskiy _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 56626a8c353SAlexey Kardashevskiy sizeof(associativity)))); 56726a8c353SAlexey Kardashevskiy } 56826a8c353SAlexey Kardashevskiy 56928e02042SDavid Gibson static int spapr_populate_memory(sPAPRMachineState *spapr, void *fdt) 57053018216SPaolo Bonzini { 571fb164994SDavid Gibson MachineState *machine = MACHINE(spapr); 5727db8a127SAlexey Kardashevskiy hwaddr mem_start, node_size; 5737db8a127SAlexey Kardashevskiy int i, nb_nodes = nb_numa_nodes; 5747db8a127SAlexey Kardashevskiy NodeInfo *nodes = numa_info; 5757db8a127SAlexey Kardashevskiy NodeInfo ramnode; 57653018216SPaolo Bonzini 5777db8a127SAlexey Kardashevskiy /* No NUMA nodes, assume there is just one node with whole RAM */ 5787db8a127SAlexey Kardashevskiy if (!nb_numa_nodes) { 5797db8a127SAlexey Kardashevskiy nb_nodes = 1; 580fb164994SDavid Gibson ramnode.node_mem = machine->ram_size; 5817db8a127SAlexey Kardashevskiy nodes = &ramnode; 5825fe269b1SPaul Mackerras } 58353018216SPaolo Bonzini 5847db8a127SAlexey Kardashevskiy for (i = 0, mem_start = 0; i < nb_nodes; ++i) { 5857db8a127SAlexey Kardashevskiy if (!nodes[i].node_mem) { 5867db8a127SAlexey Kardashevskiy continue; 58753018216SPaolo Bonzini } 588fb164994SDavid Gibson if (mem_start >= machine->ram_size) { 5895fe269b1SPaul Mackerras node_size = 0; 5905fe269b1SPaul Mackerras } else { 5917db8a127SAlexey Kardashevskiy node_size = nodes[i].node_mem; 592fb164994SDavid Gibson if (node_size > machine->ram_size - mem_start) { 593fb164994SDavid Gibson node_size = machine->ram_size - mem_start; 5945fe269b1SPaul Mackerras } 5955fe269b1SPaul Mackerras } 5967db8a127SAlexey Kardashevskiy if (!mem_start) { 5977db8a127SAlexey Kardashevskiy /* ppc_spapr_init() checks for rma_size <= node0_size already */ 5987db8a127SAlexey Kardashevskiy spapr_populate_memory_node(fdt, i, 0, spapr->rma_size); 5997db8a127SAlexey Kardashevskiy mem_start += spapr->rma_size; 6007db8a127SAlexey Kardashevskiy node_size -= spapr->rma_size; 6017db8a127SAlexey Kardashevskiy } 6026010818cSAlexey Kardashevskiy for ( ; node_size; ) { 6036010818cSAlexey Kardashevskiy hwaddr sizetmp = pow2floor(node_size); 6046010818cSAlexey Kardashevskiy 6056010818cSAlexey Kardashevskiy /* mem_start != 0 here */ 6066010818cSAlexey Kardashevskiy if (ctzl(mem_start) < ctzl(sizetmp)) { 6076010818cSAlexey Kardashevskiy sizetmp = 1ULL << ctzl(mem_start); 6086010818cSAlexey Kardashevskiy } 6096010818cSAlexey Kardashevskiy 6106010818cSAlexey Kardashevskiy spapr_populate_memory_node(fdt, i, mem_start, sizetmp); 6116010818cSAlexey Kardashevskiy node_size -= sizetmp; 6126010818cSAlexey Kardashevskiy mem_start += sizetmp; 6136010818cSAlexey Kardashevskiy } 61453018216SPaolo Bonzini } 61553018216SPaolo Bonzini 61653018216SPaolo Bonzini return 0; 61753018216SPaolo Bonzini } 61853018216SPaolo Bonzini 6190da6f3feSBharata B Rao static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset, 6200da6f3feSBharata B Rao sPAPRMachineState *spapr) 6210da6f3feSBharata B Rao { 6220da6f3feSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(cs); 6230da6f3feSBharata B Rao CPUPPCState *env = &cpu->env; 6240da6f3feSBharata B Rao PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs); 6250da6f3feSBharata B Rao int index = ppc_get_vcpu_dt_id(cpu); 6260da6f3feSBharata B Rao uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40), 6270da6f3feSBharata B Rao 0xffffffff, 0xffffffff}; 6280da6f3feSBharata B Rao uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ; 6290da6f3feSBharata B Rao uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000; 6300da6f3feSBharata B Rao uint32_t page_sizes_prop[64]; 6310da6f3feSBharata B Rao size_t page_sizes_prop_size; 6320da6f3feSBharata B Rao QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL); 6330da6f3feSBharata B Rao unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0; 6340da6f3feSBharata B Rao uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1; 6350da6f3feSBharata B Rao uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)}; 6360da6f3feSBharata B Rao 6370da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "reg", index))); 6380da6f3feSBharata B Rao _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu"))); 6390da6f3feSBharata B Rao 6400da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR]))); 6410da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size", 6420da6f3feSBharata B Rao env->dcache_line_size))); 6430da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size", 6440da6f3feSBharata B Rao env->dcache_line_size))); 6450da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size", 6460da6f3feSBharata B Rao env->icache_line_size))); 6470da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size", 6480da6f3feSBharata B Rao env->icache_line_size))); 6490da6f3feSBharata B Rao 6500da6f3feSBharata B Rao if (pcc->l1_dcache_size) { 6510da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size", 6520da6f3feSBharata B Rao pcc->l1_dcache_size))); 6530da6f3feSBharata B Rao } else { 6540da6f3feSBharata B Rao fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n"); 6550da6f3feSBharata B Rao } 6560da6f3feSBharata B Rao if (pcc->l1_icache_size) { 6570da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size", 6580da6f3feSBharata B Rao pcc->l1_icache_size))); 6590da6f3feSBharata B Rao } else { 6600da6f3feSBharata B Rao fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n"); 6610da6f3feSBharata B Rao } 6620da6f3feSBharata B Rao 6630da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq))); 6640da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq))); 6650da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", env->slb_nr))); 6660da6f3feSBharata B Rao _FDT((fdt_setprop_string(fdt, offset, "status", "okay"))); 6670da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0))); 6680da6f3feSBharata B Rao 6690da6f3feSBharata B Rao if (env->spr_cb[SPR_PURR].oea_read) { 6700da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0))); 6710da6f3feSBharata B Rao } 6720da6f3feSBharata B Rao 6730da6f3feSBharata B Rao if (env->mmu_model & POWERPC_MMU_1TSEG) { 6740da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes", 6750da6f3feSBharata B Rao segs, sizeof(segs)))); 6760da6f3feSBharata B Rao } 6770da6f3feSBharata B Rao 6780da6f3feSBharata B Rao /* Advertise VMX/VSX (vector extensions) if available 6790da6f3feSBharata B Rao * 0 / no property == no vector extensions 6800da6f3feSBharata B Rao * 1 == VMX / Altivec available 6810da6f3feSBharata B Rao * 2 == VSX available */ 6820da6f3feSBharata B Rao if (env->insns_flags & PPC_ALTIVEC) { 6830da6f3feSBharata B Rao uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1; 6840da6f3feSBharata B Rao 6850da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx))); 6860da6f3feSBharata B Rao } 6870da6f3feSBharata B Rao 6880da6f3feSBharata B Rao /* Advertise DFP (Decimal Floating Point) if available 6890da6f3feSBharata B Rao * 0 / no property == no DFP 6900da6f3feSBharata B Rao * 1 == DFP available */ 6910da6f3feSBharata B Rao if (env->insns_flags2 & PPC2_DFP) { 6920da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1))); 6930da6f3feSBharata B Rao } 6940da6f3feSBharata B Rao 6950da6f3feSBharata B Rao page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop, 6960da6f3feSBharata B Rao sizeof(page_sizes_prop)); 6970da6f3feSBharata B Rao if (page_sizes_prop_size) { 6980da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes", 6990da6f3feSBharata B Rao page_sizes_prop, page_sizes_prop_size))); 7000da6f3feSBharata B Rao } 7010da6f3feSBharata B Rao 7020da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id", 7030da6f3feSBharata B Rao cs->cpu_index / cpus_per_socket))); 7040da6f3feSBharata B Rao 7050da6f3feSBharata B Rao _FDT((fdt_setprop(fdt, offset, "ibm,pft-size", 7060da6f3feSBharata B Rao pft_size_prop, sizeof(pft_size_prop)))); 7070da6f3feSBharata B Rao 7080da6f3feSBharata B Rao _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cs)); 7090da6f3feSBharata B Rao 7100da6f3feSBharata B Rao _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, 7110da6f3feSBharata B Rao ppc_get_compat_smt_threads(cpu))); 7120da6f3feSBharata B Rao } 7130da6f3feSBharata B Rao 7140da6f3feSBharata B Rao static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr) 7150da6f3feSBharata B Rao { 7160da6f3feSBharata B Rao CPUState *cs; 7170da6f3feSBharata B Rao int cpus_offset; 7180da6f3feSBharata B Rao char *nodename; 7190da6f3feSBharata B Rao int smt = kvmppc_smt_threads(); 7200da6f3feSBharata B Rao 7210da6f3feSBharata B Rao cpus_offset = fdt_add_subnode(fdt, 0, "cpus"); 7220da6f3feSBharata B Rao _FDT(cpus_offset); 7230da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1))); 7240da6f3feSBharata B Rao _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0))); 7250da6f3feSBharata B Rao 7260da6f3feSBharata B Rao /* 7270da6f3feSBharata B Rao * We walk the CPUs in reverse order to ensure that CPU DT nodes 7280da6f3feSBharata B Rao * created by fdt_add_subnode() end up in the right order in FDT 7290da6f3feSBharata B Rao * for the guest kernel the enumerate the CPUs correctly. 7300da6f3feSBharata B Rao */ 7310da6f3feSBharata B Rao CPU_FOREACH_REVERSE(cs) { 7320da6f3feSBharata B Rao PowerPCCPU *cpu = POWERPC_CPU(cs); 7330da6f3feSBharata B Rao int index = ppc_get_vcpu_dt_id(cpu); 7340da6f3feSBharata B Rao DeviceClass *dc = DEVICE_GET_CLASS(cs); 7350da6f3feSBharata B Rao int offset; 7360da6f3feSBharata B Rao 7370da6f3feSBharata B Rao if ((index % smt) != 0) { 7380da6f3feSBharata B Rao continue; 7390da6f3feSBharata B Rao } 7400da6f3feSBharata B Rao 7410da6f3feSBharata B Rao nodename = g_strdup_printf("%s@%x", dc->fw_name, index); 7420da6f3feSBharata B Rao offset = fdt_add_subnode(fdt, cpus_offset, nodename); 7430da6f3feSBharata B Rao g_free(nodename); 7440da6f3feSBharata B Rao _FDT(offset); 7450da6f3feSBharata B Rao spapr_populate_cpu_dt(cs, fdt, offset, spapr); 7460da6f3feSBharata B Rao } 7470da6f3feSBharata B Rao 7480da6f3feSBharata B Rao } 7490da6f3feSBharata B Rao 75028e02042SDavid Gibson static void spapr_finalize_fdt(sPAPRMachineState *spapr, 75153018216SPaolo Bonzini hwaddr fdt_addr, 75253018216SPaolo Bonzini hwaddr rtas_addr, 75353018216SPaolo Bonzini hwaddr rtas_size) 75453018216SPaolo Bonzini { 7555b2128d2SAlexander Graf MachineState *machine = MACHINE(qdev_get_machine()); 7565b2128d2SAlexander Graf const char *boot_device = machine->boot_order; 75771461b0fSAlexey Kardashevskiy int ret, i; 75871461b0fSAlexey Kardashevskiy size_t cb = 0; 75971461b0fSAlexey Kardashevskiy char *bootlist; 76053018216SPaolo Bonzini void *fdt; 76153018216SPaolo Bonzini sPAPRPHBState *phb; 76253018216SPaolo Bonzini 76353018216SPaolo Bonzini fdt = g_malloc(FDT_MAX_SIZE); 76453018216SPaolo Bonzini 76553018216SPaolo Bonzini /* open out the base tree into a temp buffer for the final tweaks */ 76653018216SPaolo Bonzini _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE))); 76753018216SPaolo Bonzini 76853018216SPaolo Bonzini ret = spapr_populate_memory(spapr, fdt); 76953018216SPaolo Bonzini if (ret < 0) { 77053018216SPaolo Bonzini fprintf(stderr, "couldn't setup memory nodes in fdt\n"); 77153018216SPaolo Bonzini exit(1); 77253018216SPaolo Bonzini } 77353018216SPaolo Bonzini 77453018216SPaolo Bonzini ret = spapr_populate_vdevice(spapr->vio_bus, fdt); 77553018216SPaolo Bonzini if (ret < 0) { 77653018216SPaolo Bonzini fprintf(stderr, "couldn't setup vio devices in fdt\n"); 77753018216SPaolo Bonzini exit(1); 77853018216SPaolo Bonzini } 77953018216SPaolo Bonzini 78053018216SPaolo Bonzini QLIST_FOREACH(phb, &spapr->phbs, list) { 78153018216SPaolo Bonzini ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt); 78253018216SPaolo Bonzini } 78353018216SPaolo Bonzini 78453018216SPaolo Bonzini if (ret < 0) { 78553018216SPaolo Bonzini fprintf(stderr, "couldn't setup PCI devices in fdt\n"); 78653018216SPaolo Bonzini exit(1); 78753018216SPaolo Bonzini } 78853018216SPaolo Bonzini 78953018216SPaolo Bonzini /* RTAS */ 79053018216SPaolo Bonzini ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size); 79153018216SPaolo Bonzini if (ret < 0) { 79253018216SPaolo Bonzini fprintf(stderr, "Couldn't set up RTAS device tree properties\n"); 79353018216SPaolo Bonzini } 79453018216SPaolo Bonzini 7950da6f3feSBharata B Rao /* cpus */ 7960da6f3feSBharata B Rao spapr_populate_cpus_dt_node(fdt, spapr); 79753018216SPaolo Bonzini 79871461b0fSAlexey Kardashevskiy bootlist = get_boot_devices_list(&cb, true); 79971461b0fSAlexey Kardashevskiy if (cb && bootlist) { 80071461b0fSAlexey Kardashevskiy int offset = fdt_path_offset(fdt, "/chosen"); 80171461b0fSAlexey Kardashevskiy if (offset < 0) { 80271461b0fSAlexey Kardashevskiy exit(1); 80371461b0fSAlexey Kardashevskiy } 80471461b0fSAlexey Kardashevskiy for (i = 0; i < cb; i++) { 80571461b0fSAlexey Kardashevskiy if (bootlist[i] == '\n') { 80671461b0fSAlexey Kardashevskiy bootlist[i] = ' '; 80771461b0fSAlexey Kardashevskiy } 80871461b0fSAlexey Kardashevskiy 80971461b0fSAlexey Kardashevskiy } 81071461b0fSAlexey Kardashevskiy ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist); 81171461b0fSAlexey Kardashevskiy } 81271461b0fSAlexey Kardashevskiy 8135b2128d2SAlexander Graf if (boot_device && strlen(boot_device)) { 8145b2128d2SAlexander Graf int offset = fdt_path_offset(fdt, "/chosen"); 8155b2128d2SAlexander Graf 8165b2128d2SAlexander Graf if (offset < 0) { 8175b2128d2SAlexander Graf exit(1); 8185b2128d2SAlexander Graf } 8195b2128d2SAlexander Graf fdt_setprop_string(fdt, offset, "qemu,boot-device", boot_device); 8205b2128d2SAlexander Graf } 8215b2128d2SAlexander Graf 82253018216SPaolo Bonzini if (!spapr->has_graphics) { 82353018216SPaolo Bonzini spapr_populate_chosen_stdout(fdt, spapr->vio_bus); 82453018216SPaolo Bonzini } 82553018216SPaolo Bonzini 82653018216SPaolo Bonzini _FDT((fdt_pack(fdt))); 82753018216SPaolo Bonzini 82853018216SPaolo Bonzini if (fdt_totalsize(fdt) > FDT_MAX_SIZE) { 829730fce59SThomas Huth error_report("FDT too big ! 0x%x bytes (max is 0x%x)", 83053018216SPaolo Bonzini fdt_totalsize(fdt), FDT_MAX_SIZE); 83153018216SPaolo Bonzini exit(1); 83253018216SPaolo Bonzini } 83353018216SPaolo Bonzini 83453018216SPaolo Bonzini cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt)); 83553018216SPaolo Bonzini 836a21a7a70SGonglei g_free(bootlist); 83753018216SPaolo Bonzini g_free(fdt); 83853018216SPaolo Bonzini } 83953018216SPaolo Bonzini 84053018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 84153018216SPaolo Bonzini { 84253018216SPaolo Bonzini return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR; 84353018216SPaolo Bonzini } 84453018216SPaolo Bonzini 84553018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu) 84653018216SPaolo Bonzini { 84753018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 84853018216SPaolo Bonzini 84953018216SPaolo Bonzini if (msr_pr) { 85053018216SPaolo Bonzini hcall_dprintf("Hypercall made with MSR[PR]=1\n"); 85153018216SPaolo Bonzini env->gpr[3] = H_PRIVILEGE; 85253018216SPaolo Bonzini } else { 85353018216SPaolo Bonzini env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]); 85453018216SPaolo Bonzini } 85553018216SPaolo Bonzini } 85653018216SPaolo Bonzini 857e6b8fd24SSamuel Mendoza-Jonas #define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2)) 858e6b8fd24SSamuel Mendoza-Jonas #define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID) 859e6b8fd24SSamuel Mendoza-Jonas #define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY) 860e6b8fd24SSamuel Mendoza-Jonas #define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY)) 861e6b8fd24SSamuel Mendoza-Jonas #define DIRTY_HPTE(_hpte) ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY)) 862e6b8fd24SSamuel Mendoza-Jonas 86328e02042SDavid Gibson static void spapr_reset_htab(sPAPRMachineState *spapr) 86453018216SPaolo Bonzini { 86553018216SPaolo Bonzini long shift; 866e6b8fd24SSamuel Mendoza-Jonas int index; 86753018216SPaolo Bonzini 86853018216SPaolo Bonzini /* allocate hash page table. For now we always make this 16mb, 86953018216SPaolo Bonzini * later we should probably make it scale to the size of guest 87053018216SPaolo Bonzini * RAM */ 87153018216SPaolo Bonzini 87253018216SPaolo Bonzini shift = kvmppc_reset_htab(spapr->htab_shift); 87353018216SPaolo Bonzini 87453018216SPaolo Bonzini if (shift > 0) { 87553018216SPaolo Bonzini /* Kernel handles htab, we don't need to allocate one */ 87653018216SPaolo Bonzini spapr->htab_shift = shift; 8777c43bca0SAneesh Kumar K.V kvmppc_kern_htab = true; 87801a57972SSamuel Mendoza-Jonas 87901a57972SSamuel Mendoza-Jonas /* Tell readers to update their file descriptor */ 88001a57972SSamuel Mendoza-Jonas if (spapr->htab_fd >= 0) { 88101a57972SSamuel Mendoza-Jonas spapr->htab_fd_stale = true; 88201a57972SSamuel Mendoza-Jonas } 88353018216SPaolo Bonzini } else { 88453018216SPaolo Bonzini if (!spapr->htab) { 88553018216SPaolo Bonzini /* Allocate an htab if we don't yet have one */ 88653018216SPaolo Bonzini spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr)); 88753018216SPaolo Bonzini } 88853018216SPaolo Bonzini 88953018216SPaolo Bonzini /* And clear it */ 89053018216SPaolo Bonzini memset(spapr->htab, 0, HTAB_SIZE(spapr)); 891e6b8fd24SSamuel Mendoza-Jonas 892e6b8fd24SSamuel Mendoza-Jonas for (index = 0; index < HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; index++) { 893e6b8fd24SSamuel Mendoza-Jonas DIRTY_HPTE(HPTE(spapr->htab, index)); 894e6b8fd24SSamuel Mendoza-Jonas } 89553018216SPaolo Bonzini } 89653018216SPaolo Bonzini 89753018216SPaolo Bonzini /* Update the RMA size if necessary */ 89853018216SPaolo Bonzini if (spapr->vrma_adjust) { 899b082d65aSAlexey Kardashevskiy spapr->rma_size = kvmppc_rma_size(spapr_node0_size(), 900b082d65aSAlexey Kardashevskiy spapr->htab_shift); 90153018216SPaolo Bonzini } 90253018216SPaolo Bonzini } 90353018216SPaolo Bonzini 9049e3f9733SAlexander Graf static int find_unknown_sysbus_device(SysBusDevice *sbdev, void *opaque) 9059e3f9733SAlexander Graf { 9069e3f9733SAlexander Graf bool matched = false; 9079e3f9733SAlexander Graf 9089e3f9733SAlexander Graf if (object_dynamic_cast(OBJECT(sbdev), TYPE_SPAPR_PCI_HOST_BRIDGE)) { 9099e3f9733SAlexander Graf matched = true; 9109e3f9733SAlexander Graf } 9119e3f9733SAlexander Graf 9129e3f9733SAlexander Graf if (!matched) { 9139e3f9733SAlexander Graf error_report("Device %s is not supported by this machine yet.", 9149e3f9733SAlexander Graf qdev_fw_name(DEVICE(sbdev))); 9159e3f9733SAlexander Graf exit(1); 9169e3f9733SAlexander Graf } 9179e3f9733SAlexander Graf 9189e3f9733SAlexander Graf return 0; 9199e3f9733SAlexander Graf } 9209e3f9733SAlexander Graf 92101a57972SSamuel Mendoza-Jonas /* 92201a57972SSamuel Mendoza-Jonas * A guest reset will cause spapr->htab_fd to become stale if being used. 92301a57972SSamuel Mendoza-Jonas * Reopen the file descriptor to make sure the whole HTAB is properly read. 92401a57972SSamuel Mendoza-Jonas */ 92528e02042SDavid Gibson static int spapr_check_htab_fd(sPAPRMachineState *spapr) 92601a57972SSamuel Mendoza-Jonas { 92701a57972SSamuel Mendoza-Jonas int rc = 0; 92801a57972SSamuel Mendoza-Jonas 92901a57972SSamuel Mendoza-Jonas if (spapr->htab_fd_stale) { 93001a57972SSamuel Mendoza-Jonas close(spapr->htab_fd); 93101a57972SSamuel Mendoza-Jonas spapr->htab_fd = kvmppc_get_htab_fd(false); 93201a57972SSamuel Mendoza-Jonas if (spapr->htab_fd < 0) { 93301a57972SSamuel Mendoza-Jonas error_report("Unable to open fd for reading hash table from KVM: " 93401a57972SSamuel Mendoza-Jonas "%s", strerror(errno)); 93501a57972SSamuel Mendoza-Jonas rc = -1; 93601a57972SSamuel Mendoza-Jonas } 93701a57972SSamuel Mendoza-Jonas spapr->htab_fd_stale = false; 93801a57972SSamuel Mendoza-Jonas } 93901a57972SSamuel Mendoza-Jonas 94001a57972SSamuel Mendoza-Jonas return rc; 94101a57972SSamuel Mendoza-Jonas } 94201a57972SSamuel Mendoza-Jonas 94353018216SPaolo Bonzini static void ppc_spapr_reset(void) 94453018216SPaolo Bonzini { 94528e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 946182735efSAndreas Färber PowerPCCPU *first_ppc_cpu; 947b7d1f77aSBenjamin Herrenschmidt uint32_t rtas_limit; 948259186a7SAndreas Färber 9499e3f9733SAlexander Graf /* Check for unknown sysbus devices */ 9509e3f9733SAlexander Graf foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL); 9519e3f9733SAlexander Graf 95253018216SPaolo Bonzini /* Reset the hash table & recalc the RMA */ 95353018216SPaolo Bonzini spapr_reset_htab(spapr); 95453018216SPaolo Bonzini 95553018216SPaolo Bonzini qemu_devices_reset(); 95653018216SPaolo Bonzini 957b7d1f77aSBenjamin Herrenschmidt /* 958b7d1f77aSBenjamin Herrenschmidt * We place the device tree and RTAS just below either the top of the RMA, 959b7d1f77aSBenjamin Herrenschmidt * or just below 2GB, whichever is lowere, so that it can be 960b7d1f77aSBenjamin Herrenschmidt * processed with 32-bit real mode code if necessary 961b7d1f77aSBenjamin Herrenschmidt */ 962b7d1f77aSBenjamin Herrenschmidt rtas_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR); 963b7d1f77aSBenjamin Herrenschmidt spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE; 964b7d1f77aSBenjamin Herrenschmidt spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE; 965b7d1f77aSBenjamin Herrenschmidt 96653018216SPaolo Bonzini /* Load the fdt */ 96753018216SPaolo Bonzini spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr, 96853018216SPaolo Bonzini spapr->rtas_size); 96953018216SPaolo Bonzini 970b7d1f77aSBenjamin Herrenschmidt /* Copy RTAS over */ 971b7d1f77aSBenjamin Herrenschmidt cpu_physical_memory_write(spapr->rtas_addr, spapr->rtas_blob, 972b7d1f77aSBenjamin Herrenschmidt spapr->rtas_size); 973b7d1f77aSBenjamin Herrenschmidt 97453018216SPaolo Bonzini /* Set up the entry state */ 975182735efSAndreas Färber first_ppc_cpu = POWERPC_CPU(first_cpu); 976182735efSAndreas Färber first_ppc_cpu->env.gpr[3] = spapr->fdt_addr; 977182735efSAndreas Färber first_ppc_cpu->env.gpr[5] = 0; 978182735efSAndreas Färber first_cpu->halted = 0; 9791b718907SDavid Gibson first_ppc_cpu->env.nip = SPAPR_ENTRY_POINT; 98053018216SPaolo Bonzini 98153018216SPaolo Bonzini } 98253018216SPaolo Bonzini 98353018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque) 98453018216SPaolo Bonzini { 98528e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 98653018216SPaolo Bonzini PowerPCCPU *cpu = opaque; 987259186a7SAndreas Färber CPUState *cs = CPU(cpu); 98853018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 98953018216SPaolo Bonzini 990259186a7SAndreas Färber cpu_reset(cs); 99153018216SPaolo Bonzini 99253018216SPaolo Bonzini /* All CPUs start halted. CPU0 is unhalted from the machine level 99353018216SPaolo Bonzini * reset code and the rest are explicitly started up by the guest 99453018216SPaolo Bonzini * using an RTAS call */ 995259186a7SAndreas Färber cs->halted = 1; 99653018216SPaolo Bonzini 99753018216SPaolo Bonzini env->spr[SPR_HIOR] = 0; 99853018216SPaolo Bonzini 9994be21d56SDavid Gibson env->external_htab = (uint8_t *)spapr->htab; 10005736245cSAneesh Kumar K.V if (kvm_enabled() && !env->external_htab) { 10015736245cSAneesh Kumar K.V /* 10025736245cSAneesh Kumar K.V * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte* 10035736245cSAneesh Kumar K.V * functions do the right thing. 10045736245cSAneesh Kumar K.V */ 10055736245cSAneesh Kumar K.V env->external_htab = (void *)1; 10065736245cSAneesh Kumar K.V } 100753018216SPaolo Bonzini env->htab_base = -1; 1008f3c75d42SAneesh Kumar K.V /* 1009f3c75d42SAneesh Kumar K.V * htab_mask is the mask used to normalize hash value to PTEG index. 1010f3c75d42SAneesh Kumar K.V * htab_shift is log2 of hash table size. 1011f3c75d42SAneesh Kumar K.V * We have 8 hpte per group, and each hpte is 16 bytes. 1012f3c75d42SAneesh Kumar K.V * ie have 128 bytes per hpte entry. 1013f3c75d42SAneesh Kumar K.V */ 101428e02042SDavid Gibson env->htab_mask = (1ULL << (spapr->htab_shift - 7)) - 1; 1015ec4936e1SStefan Weil env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab | 101653018216SPaolo Bonzini (spapr->htab_shift - 18); 101753018216SPaolo Bonzini } 101853018216SPaolo Bonzini 101928e02042SDavid Gibson static void spapr_create_nvram(sPAPRMachineState *spapr) 102053018216SPaolo Bonzini { 10212ff3de68SMarkus Armbruster DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram"); 10223978b863SPaolo Bonzini DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0); 102353018216SPaolo Bonzini 10243978b863SPaolo Bonzini if (dinfo) { 10254be74634SMarkus Armbruster qdev_prop_set_drive_nofail(dev, "drive", blk_by_legacy_dinfo(dinfo)); 102653018216SPaolo Bonzini } 102753018216SPaolo Bonzini 102853018216SPaolo Bonzini qdev_init_nofail(dev); 102953018216SPaolo Bonzini 103053018216SPaolo Bonzini spapr->nvram = (struct sPAPRNVRAM *)dev; 103153018216SPaolo Bonzini } 103253018216SPaolo Bonzini 103328e02042SDavid Gibson static void spapr_rtc_create(sPAPRMachineState *spapr) 103428df36a1SDavid Gibson { 103528df36a1SDavid Gibson DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC); 103628df36a1SDavid Gibson 103728df36a1SDavid Gibson qdev_init_nofail(dev); 103828df36a1SDavid Gibson spapr->rtc = dev; 103974e5ae28SDavid Gibson 104074e5ae28SDavid Gibson object_property_add_alias(qdev_get_machine(), "rtc-time", 104174e5ae28SDavid Gibson OBJECT(spapr->rtc), "date", NULL); 104228df36a1SDavid Gibson } 104328df36a1SDavid Gibson 104453018216SPaolo Bonzini /* Returns whether we want to use VGA or not */ 104553018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus) 104653018216SPaolo Bonzini { 104753018216SPaolo Bonzini switch (vga_interface_type) { 104853018216SPaolo Bonzini case VGA_NONE: 10497effdaa3SMark Wu return false; 10507effdaa3SMark Wu case VGA_DEVICE: 10517effdaa3SMark Wu return true; 105253018216SPaolo Bonzini case VGA_STD: 105353018216SPaolo Bonzini return pci_vga_init(pci_bus) != NULL; 105453018216SPaolo Bonzini default: 105553018216SPaolo Bonzini fprintf(stderr, "This vga model is not supported," 105653018216SPaolo Bonzini "currently it only supports -vga std\n"); 105753018216SPaolo Bonzini exit(0); 105853018216SPaolo Bonzini } 105953018216SPaolo Bonzini } 106053018216SPaolo Bonzini 1061880ae7deSDavid Gibson static int spapr_post_load(void *opaque, int version_id) 1062880ae7deSDavid Gibson { 106328e02042SDavid Gibson sPAPRMachineState *spapr = (sPAPRMachineState *)opaque; 1064880ae7deSDavid Gibson int err = 0; 1065880ae7deSDavid Gibson 1066631b22eaSStefan Weil /* In earlier versions, there was no separate qdev for the PAPR 1067880ae7deSDavid Gibson * RTC, so the RTC offset was stored directly in sPAPREnvironment. 1068880ae7deSDavid Gibson * So when migrating from those versions, poke the incoming offset 1069880ae7deSDavid Gibson * value into the RTC device */ 1070880ae7deSDavid Gibson if (version_id < 3) { 1071880ae7deSDavid Gibson err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset); 1072880ae7deSDavid Gibson } 1073880ae7deSDavid Gibson 1074880ae7deSDavid Gibson return err; 1075880ae7deSDavid Gibson } 1076880ae7deSDavid Gibson 1077880ae7deSDavid Gibson static bool version_before_3(void *opaque, int version_id) 1078880ae7deSDavid Gibson { 1079880ae7deSDavid Gibson return version_id < 3; 1080880ae7deSDavid Gibson } 1081880ae7deSDavid Gibson 10824be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = { 10834be21d56SDavid Gibson .name = "spapr", 1084880ae7deSDavid Gibson .version_id = 3, 10854be21d56SDavid Gibson .minimum_version_id = 1, 1086880ae7deSDavid Gibson .post_load = spapr_post_load, 10874be21d56SDavid Gibson .fields = (VMStateField[]) { 1088880ae7deSDavid Gibson /* used to be @next_irq */ 1089880ae7deSDavid Gibson VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4), 10904be21d56SDavid Gibson 10914be21d56SDavid Gibson /* RTC offset */ 109228e02042SDavid Gibson VMSTATE_UINT64_TEST(rtc_offset, sPAPRMachineState, version_before_3), 1093880ae7deSDavid Gibson 109428e02042SDavid Gibson VMSTATE_PPC_TIMEBASE_V(tb, sPAPRMachineState, 2), 10954be21d56SDavid Gibson VMSTATE_END_OF_LIST() 10964be21d56SDavid Gibson }, 10974be21d56SDavid Gibson }; 10984be21d56SDavid Gibson 10994be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque) 11004be21d56SDavid Gibson { 110128e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 11024be21d56SDavid Gibson 11034be21d56SDavid Gibson /* "Iteration" header */ 11044be21d56SDavid Gibson qemu_put_be32(f, spapr->htab_shift); 11054be21d56SDavid Gibson 1106e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 1107e68cb8b4SAlexey Kardashevskiy spapr->htab_save_index = 0; 1108e68cb8b4SAlexey Kardashevskiy spapr->htab_first_pass = true; 1109e68cb8b4SAlexey Kardashevskiy } else { 1110e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1111e68cb8b4SAlexey Kardashevskiy 1112e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = kvmppc_get_htab_fd(false); 111301a57972SSamuel Mendoza-Jonas spapr->htab_fd_stale = false; 1114e68cb8b4SAlexey Kardashevskiy if (spapr->htab_fd < 0) { 1115e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n", 1116e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1117e68cb8b4SAlexey Kardashevskiy return -1; 1118e68cb8b4SAlexey Kardashevskiy } 11194be21d56SDavid Gibson } 11204be21d56SDavid Gibson 1121e68cb8b4SAlexey Kardashevskiy 1122e68cb8b4SAlexey Kardashevskiy return 0; 1123e68cb8b4SAlexey Kardashevskiy } 11244be21d56SDavid Gibson 112528e02042SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPRMachineState *spapr, 11264be21d56SDavid Gibson int64_t max_ns) 11274be21d56SDavid Gibson { 11284be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 11294be21d56SDavid Gibson int index = spapr->htab_save_index; 1130bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 11314be21d56SDavid Gibson 11324be21d56SDavid Gibson assert(spapr->htab_first_pass); 11334be21d56SDavid Gibson 11344be21d56SDavid Gibson do { 11354be21d56SDavid Gibson int chunkstart; 11364be21d56SDavid Gibson 11374be21d56SDavid Gibson /* Consume invalid HPTEs */ 11384be21d56SDavid Gibson while ((index < htabslots) 11394be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 11404be21d56SDavid Gibson index++; 11414be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 11424be21d56SDavid Gibson } 11434be21d56SDavid Gibson 11444be21d56SDavid Gibson /* Consume valid HPTEs */ 11454be21d56SDavid Gibson chunkstart = index; 1146338c25b6SSamuel Mendoza-Jonas while ((index < htabslots) && (index - chunkstart < USHRT_MAX) 11474be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 11484be21d56SDavid Gibson index++; 11494be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 11504be21d56SDavid Gibson } 11514be21d56SDavid Gibson 11524be21d56SDavid Gibson if (index > chunkstart) { 11534be21d56SDavid Gibson int n_valid = index - chunkstart; 11544be21d56SDavid Gibson 11554be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 11564be21d56SDavid Gibson qemu_put_be16(f, n_valid); 11574be21d56SDavid Gibson qemu_put_be16(f, 0); 11584be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 11594be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 11604be21d56SDavid Gibson 1161bc72ad67SAlex Bligh if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 11624be21d56SDavid Gibson break; 11634be21d56SDavid Gibson } 11644be21d56SDavid Gibson } 11654be21d56SDavid Gibson } while ((index < htabslots) && !qemu_file_rate_limit(f)); 11664be21d56SDavid Gibson 11674be21d56SDavid Gibson if (index >= htabslots) { 11684be21d56SDavid Gibson assert(index == htabslots); 11694be21d56SDavid Gibson index = 0; 11704be21d56SDavid Gibson spapr->htab_first_pass = false; 11714be21d56SDavid Gibson } 11724be21d56SDavid Gibson spapr->htab_save_index = index; 11734be21d56SDavid Gibson } 11744be21d56SDavid Gibson 117528e02042SDavid Gibson static int htab_save_later_pass(QEMUFile *f, sPAPRMachineState *spapr, 11764be21d56SDavid Gibson int64_t max_ns) 11774be21d56SDavid Gibson { 11784be21d56SDavid Gibson bool final = max_ns < 0; 11794be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 11804be21d56SDavid Gibson int examined = 0, sent = 0; 11814be21d56SDavid Gibson int index = spapr->htab_save_index; 1182bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 11834be21d56SDavid Gibson 11844be21d56SDavid Gibson assert(!spapr->htab_first_pass); 11854be21d56SDavid Gibson 11864be21d56SDavid Gibson do { 11874be21d56SDavid Gibson int chunkstart, invalidstart; 11884be21d56SDavid Gibson 11894be21d56SDavid Gibson /* Consume non-dirty HPTEs */ 11904be21d56SDavid Gibson while ((index < htabslots) 11914be21d56SDavid Gibson && !HPTE_DIRTY(HPTE(spapr->htab, index))) { 11924be21d56SDavid Gibson index++; 11934be21d56SDavid Gibson examined++; 11944be21d56SDavid Gibson } 11954be21d56SDavid Gibson 11964be21d56SDavid Gibson chunkstart = index; 11974be21d56SDavid Gibson /* Consume valid dirty HPTEs */ 1198338c25b6SSamuel Mendoza-Jonas while ((index < htabslots) && (index - chunkstart < USHRT_MAX) 11994be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 12004be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 12014be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 12024be21d56SDavid Gibson index++; 12034be21d56SDavid Gibson examined++; 12044be21d56SDavid Gibson } 12054be21d56SDavid Gibson 12064be21d56SDavid Gibson invalidstart = index; 12074be21d56SDavid Gibson /* Consume invalid dirty HPTEs */ 1208338c25b6SSamuel Mendoza-Jonas while ((index < htabslots) && (index - invalidstart < USHRT_MAX) 12094be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 12104be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 12114be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 12124be21d56SDavid Gibson index++; 12134be21d56SDavid Gibson examined++; 12144be21d56SDavid Gibson } 12154be21d56SDavid Gibson 12164be21d56SDavid Gibson if (index > chunkstart) { 12174be21d56SDavid Gibson int n_valid = invalidstart - chunkstart; 12184be21d56SDavid Gibson int n_invalid = index - invalidstart; 12194be21d56SDavid Gibson 12204be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 12214be21d56SDavid Gibson qemu_put_be16(f, n_valid); 12224be21d56SDavid Gibson qemu_put_be16(f, n_invalid); 12234be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 12244be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 12254be21d56SDavid Gibson sent += index - chunkstart; 12264be21d56SDavid Gibson 1227bc72ad67SAlex Bligh if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 12284be21d56SDavid Gibson break; 12294be21d56SDavid Gibson } 12304be21d56SDavid Gibson } 12314be21d56SDavid Gibson 12324be21d56SDavid Gibson if (examined >= htabslots) { 12334be21d56SDavid Gibson break; 12344be21d56SDavid Gibson } 12354be21d56SDavid Gibson 12364be21d56SDavid Gibson if (index >= htabslots) { 12374be21d56SDavid Gibson assert(index == htabslots); 12384be21d56SDavid Gibson index = 0; 12394be21d56SDavid Gibson } 12404be21d56SDavid Gibson } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final)); 12414be21d56SDavid Gibson 12424be21d56SDavid Gibson if (index >= htabslots) { 12434be21d56SDavid Gibson assert(index == htabslots); 12444be21d56SDavid Gibson index = 0; 12454be21d56SDavid Gibson } 12464be21d56SDavid Gibson 12474be21d56SDavid Gibson spapr->htab_save_index = index; 12484be21d56SDavid Gibson 1249e68cb8b4SAlexey Kardashevskiy return (examined >= htabslots) && (sent == 0) ? 1 : 0; 12504be21d56SDavid Gibson } 12514be21d56SDavid Gibson 1252e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS 5000000 /* 5 ms */ 1253e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE 2048 1254e68cb8b4SAlexey Kardashevskiy 12554be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque) 12564be21d56SDavid Gibson { 125728e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 1258e68cb8b4SAlexey Kardashevskiy int rc = 0; 12594be21d56SDavid Gibson 12604be21d56SDavid Gibson /* Iteration header */ 12614be21d56SDavid Gibson qemu_put_be32(f, 0); 12624be21d56SDavid Gibson 1263e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1264e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1265e68cb8b4SAlexey Kardashevskiy 126601a57972SSamuel Mendoza-Jonas rc = spapr_check_htab_fd(spapr); 126701a57972SSamuel Mendoza-Jonas if (rc < 0) { 126801a57972SSamuel Mendoza-Jonas return rc; 126901a57972SSamuel Mendoza-Jonas } 127001a57972SSamuel Mendoza-Jonas 1271e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, 1272e68cb8b4SAlexey Kardashevskiy MAX_KVM_BUF_SIZE, MAX_ITERATION_NS); 1273e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1274e68cb8b4SAlexey Kardashevskiy return rc; 1275e68cb8b4SAlexey Kardashevskiy } 1276e68cb8b4SAlexey Kardashevskiy } else if (spapr->htab_first_pass) { 12774be21d56SDavid Gibson htab_save_first_pass(f, spapr, MAX_ITERATION_NS); 12784be21d56SDavid Gibson } else { 1279e68cb8b4SAlexey Kardashevskiy rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS); 12804be21d56SDavid Gibson } 12814be21d56SDavid Gibson 12824be21d56SDavid Gibson /* End marker */ 12834be21d56SDavid Gibson qemu_put_be32(f, 0); 12844be21d56SDavid Gibson qemu_put_be16(f, 0); 12854be21d56SDavid Gibson qemu_put_be16(f, 0); 12864be21d56SDavid Gibson 1287e68cb8b4SAlexey Kardashevskiy return rc; 12884be21d56SDavid Gibson } 12894be21d56SDavid Gibson 12904be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque) 12914be21d56SDavid Gibson { 129228e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 12934be21d56SDavid Gibson 12944be21d56SDavid Gibson /* Iteration header */ 12954be21d56SDavid Gibson qemu_put_be32(f, 0); 12964be21d56SDavid Gibson 1297e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1298e68cb8b4SAlexey Kardashevskiy int rc; 1299e68cb8b4SAlexey Kardashevskiy 1300e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1301e68cb8b4SAlexey Kardashevskiy 130201a57972SSamuel Mendoza-Jonas rc = spapr_check_htab_fd(spapr); 130301a57972SSamuel Mendoza-Jonas if (rc < 0) { 130401a57972SSamuel Mendoza-Jonas return rc; 130501a57972SSamuel Mendoza-Jonas } 130601a57972SSamuel Mendoza-Jonas 1307e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1); 1308e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1309e68cb8b4SAlexey Kardashevskiy return rc; 1310e68cb8b4SAlexey Kardashevskiy } 1311e68cb8b4SAlexey Kardashevskiy close(spapr->htab_fd); 1312e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = -1; 1313e68cb8b4SAlexey Kardashevskiy } else { 13144be21d56SDavid Gibson htab_save_later_pass(f, spapr, -1); 1315e68cb8b4SAlexey Kardashevskiy } 13164be21d56SDavid Gibson 13174be21d56SDavid Gibson /* End marker */ 13184be21d56SDavid Gibson qemu_put_be32(f, 0); 13194be21d56SDavid Gibson qemu_put_be16(f, 0); 13204be21d56SDavid Gibson qemu_put_be16(f, 0); 13214be21d56SDavid Gibson 13224be21d56SDavid Gibson return 0; 13234be21d56SDavid Gibson } 13244be21d56SDavid Gibson 13254be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id) 13264be21d56SDavid Gibson { 132728e02042SDavid Gibson sPAPRMachineState *spapr = opaque; 13284be21d56SDavid Gibson uint32_t section_hdr; 1329e68cb8b4SAlexey Kardashevskiy int fd = -1; 13304be21d56SDavid Gibson 13314be21d56SDavid Gibson if (version_id < 1 || version_id > 1) { 13324be21d56SDavid Gibson fprintf(stderr, "htab_load() bad version\n"); 13334be21d56SDavid Gibson return -EINVAL; 13344be21d56SDavid Gibson } 13354be21d56SDavid Gibson 13364be21d56SDavid Gibson section_hdr = qemu_get_be32(f); 13374be21d56SDavid Gibson 13384be21d56SDavid Gibson if (section_hdr) { 13394be21d56SDavid Gibson /* First section, just the hash shift */ 13404be21d56SDavid Gibson if (spapr->htab_shift != section_hdr) { 1341613e7a76SBharata B Rao error_report("htab_shift mismatch: source %d target %d", 1342613e7a76SBharata B Rao section_hdr, spapr->htab_shift); 13434be21d56SDavid Gibson return -EINVAL; 13444be21d56SDavid Gibson } 13454be21d56SDavid Gibson return 0; 13464be21d56SDavid Gibson } 13474be21d56SDavid Gibson 1348e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1349e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1350e68cb8b4SAlexey Kardashevskiy 1351e68cb8b4SAlexey Kardashevskiy fd = kvmppc_get_htab_fd(true); 1352e68cb8b4SAlexey Kardashevskiy if (fd < 0) { 1353e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n", 1354e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1355e68cb8b4SAlexey Kardashevskiy } 1356e68cb8b4SAlexey Kardashevskiy } 1357e68cb8b4SAlexey Kardashevskiy 13584be21d56SDavid Gibson while (true) { 13594be21d56SDavid Gibson uint32_t index; 13604be21d56SDavid Gibson uint16_t n_valid, n_invalid; 13614be21d56SDavid Gibson 13624be21d56SDavid Gibson index = qemu_get_be32(f); 13634be21d56SDavid Gibson n_valid = qemu_get_be16(f); 13644be21d56SDavid Gibson n_invalid = qemu_get_be16(f); 13654be21d56SDavid Gibson 13664be21d56SDavid Gibson if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) { 13674be21d56SDavid Gibson /* End of Stream */ 13684be21d56SDavid Gibson break; 13694be21d56SDavid Gibson } 13704be21d56SDavid Gibson 1371e68cb8b4SAlexey Kardashevskiy if ((index + n_valid + n_invalid) > 13724be21d56SDavid Gibson (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) { 13734be21d56SDavid Gibson /* Bad index in stream */ 13744be21d56SDavid Gibson fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) " 1375e68cb8b4SAlexey Kardashevskiy "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid, 1376e68cb8b4SAlexey Kardashevskiy spapr->htab_shift); 13774be21d56SDavid Gibson return -EINVAL; 13784be21d56SDavid Gibson } 13794be21d56SDavid Gibson 1380e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 13814be21d56SDavid Gibson if (n_valid) { 13824be21d56SDavid Gibson qemu_get_buffer(f, HPTE(spapr->htab, index), 13834be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 13844be21d56SDavid Gibson } 13854be21d56SDavid Gibson if (n_invalid) { 13864be21d56SDavid Gibson memset(HPTE(spapr->htab, index + n_valid), 0, 13874be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_invalid); 13884be21d56SDavid Gibson } 1389e68cb8b4SAlexey Kardashevskiy } else { 1390e68cb8b4SAlexey Kardashevskiy int rc; 1391e68cb8b4SAlexey Kardashevskiy 1392e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1393e68cb8b4SAlexey Kardashevskiy 1394e68cb8b4SAlexey Kardashevskiy rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid); 1395e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1396e68cb8b4SAlexey Kardashevskiy return rc; 1397e68cb8b4SAlexey Kardashevskiy } 1398e68cb8b4SAlexey Kardashevskiy } 1399e68cb8b4SAlexey Kardashevskiy } 1400e68cb8b4SAlexey Kardashevskiy 1401e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1402e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1403e68cb8b4SAlexey Kardashevskiy close(fd); 14044be21d56SDavid Gibson } 14054be21d56SDavid Gibson 14064be21d56SDavid Gibson return 0; 14074be21d56SDavid Gibson } 14084be21d56SDavid Gibson 14094be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = { 14104be21d56SDavid Gibson .save_live_setup = htab_save_setup, 14114be21d56SDavid Gibson .save_live_iterate = htab_save_iterate, 14124be21d56SDavid Gibson .save_live_complete = htab_save_complete, 14134be21d56SDavid Gibson .load_state = htab_load, 14144be21d56SDavid Gibson }; 14154be21d56SDavid Gibson 14165b2128d2SAlexander Graf static void spapr_boot_set(void *opaque, const char *boot_device, 14175b2128d2SAlexander Graf Error **errp) 14185b2128d2SAlexander Graf { 14195b2128d2SAlexander Graf MachineState *machine = MACHINE(qdev_get_machine()); 14205b2128d2SAlexander Graf machine->boot_order = g_strdup(boot_device); 14215b2128d2SAlexander Graf } 14225b2128d2SAlexander Graf 1423bab99ea0SBharata B Rao static void spapr_cpu_init(sPAPRMachineState *spapr, PowerPCCPU *cpu) 1424bab99ea0SBharata B Rao { 1425bab99ea0SBharata B Rao CPUPPCState *env = &cpu->env; 1426bab99ea0SBharata B Rao 1427bab99ea0SBharata B Rao /* Set time-base frequency to 512 MHz */ 1428bab99ea0SBharata B Rao cpu_ppc_tb_init(env, TIMEBASE_FREQ); 1429bab99ea0SBharata B Rao 1430bab99ea0SBharata B Rao /* PAPR always has exception vectors in RAM not ROM. To ensure this, 1431bab99ea0SBharata B Rao * MSR[IP] should never be set. 1432bab99ea0SBharata B Rao */ 1433bab99ea0SBharata B Rao env->msr_mask &= ~(1 << 6); 1434bab99ea0SBharata B Rao 1435bab99ea0SBharata B Rao /* Tell KVM that we're in PAPR mode */ 1436bab99ea0SBharata B Rao if (kvm_enabled()) { 1437bab99ea0SBharata B Rao kvmppc_set_papr(cpu); 1438bab99ea0SBharata B Rao } 1439bab99ea0SBharata B Rao 1440bab99ea0SBharata B Rao if (cpu->max_compat) { 1441bab99ea0SBharata B Rao if (ppc_set_compat(cpu, cpu->max_compat) < 0) { 1442bab99ea0SBharata B Rao exit(1); 1443bab99ea0SBharata B Rao } 1444bab99ea0SBharata B Rao } 1445bab99ea0SBharata B Rao 1446bab99ea0SBharata B Rao xics_cpu_setup(spapr->icp, cpu); 1447bab99ea0SBharata B Rao 1448bab99ea0SBharata B Rao qemu_register_reset(spapr_cpu_reset, cpu); 1449bab99ea0SBharata B Rao } 1450bab99ea0SBharata B Rao 145153018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */ 14523ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine) 145353018216SPaolo Bonzini { 145428e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(machine); 14553ef96221SMarcel Apfelbaum const char *kernel_filename = machine->kernel_filename; 14563ef96221SMarcel Apfelbaum const char *kernel_cmdline = machine->kernel_cmdline; 14573ef96221SMarcel Apfelbaum const char *initrd_filename = machine->initrd_filename; 145853018216SPaolo Bonzini PowerPCCPU *cpu; 145953018216SPaolo Bonzini PCIHostState *phb; 146053018216SPaolo Bonzini int i; 146153018216SPaolo Bonzini MemoryRegion *sysmem = get_system_memory(); 146253018216SPaolo Bonzini MemoryRegion *ram = g_new(MemoryRegion, 1); 1463658fa66bSAlexey Kardashevskiy MemoryRegion *rma_region; 1464658fa66bSAlexey Kardashevskiy void *rma = NULL; 146553018216SPaolo Bonzini hwaddr rma_alloc_size; 1466b082d65aSAlexey Kardashevskiy hwaddr node0_size = spapr_node0_size(); 146753018216SPaolo Bonzini uint32_t initrd_base = 0; 146853018216SPaolo Bonzini long kernel_size = 0, initrd_size = 0; 1469b7d1f77aSBenjamin Herrenschmidt long load_limit, fw_size; 147016457e7fSBenjamin Herrenschmidt bool kernel_le = false; 147153018216SPaolo Bonzini char *filename; 147253018216SPaolo Bonzini 147353018216SPaolo Bonzini msi_supported = true; 147453018216SPaolo Bonzini 147553018216SPaolo Bonzini QLIST_INIT(&spapr->phbs); 147653018216SPaolo Bonzini 147753018216SPaolo Bonzini cpu_ppc_hypercall = emulate_spapr_hypercall; 147853018216SPaolo Bonzini 147953018216SPaolo Bonzini /* Allocate RMA if necessary */ 1480658fa66bSAlexey Kardashevskiy rma_alloc_size = kvmppc_alloc_rma(&rma); 148153018216SPaolo Bonzini 148253018216SPaolo Bonzini if (rma_alloc_size == -1) { 1483730fce59SThomas Huth error_report("Unable to create RMA"); 148453018216SPaolo Bonzini exit(1); 148553018216SPaolo Bonzini } 148653018216SPaolo Bonzini 1487c4177479SAlexey Kardashevskiy if (rma_alloc_size && (rma_alloc_size < node0_size)) { 148853018216SPaolo Bonzini spapr->rma_size = rma_alloc_size; 148953018216SPaolo Bonzini } else { 1490c4177479SAlexey Kardashevskiy spapr->rma_size = node0_size; 149153018216SPaolo Bonzini 149253018216SPaolo Bonzini /* With KVM, we don't actually know whether KVM supports an 149353018216SPaolo Bonzini * unbounded RMA (PR KVM) or is limited by the hash table size 149453018216SPaolo Bonzini * (HV KVM using VRMA), so we always assume the latter 149553018216SPaolo Bonzini * 149653018216SPaolo Bonzini * In that case, we also limit the initial allocations for RTAS 149753018216SPaolo Bonzini * etc... to 256M since we have no way to know what the VRMA size 149853018216SPaolo Bonzini * is going to be as it depends on the size of the hash table 149953018216SPaolo Bonzini * isn't determined yet. 150053018216SPaolo Bonzini */ 150153018216SPaolo Bonzini if (kvm_enabled()) { 150253018216SPaolo Bonzini spapr->vrma_adjust = 1; 150353018216SPaolo Bonzini spapr->rma_size = MIN(spapr->rma_size, 0x10000000); 150453018216SPaolo Bonzini } 150553018216SPaolo Bonzini } 150653018216SPaolo Bonzini 1507c4177479SAlexey Kardashevskiy if (spapr->rma_size > node0_size) { 1508c4177479SAlexey Kardashevskiy fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n", 1509c4177479SAlexey Kardashevskiy spapr->rma_size); 1510c4177479SAlexey Kardashevskiy exit(1); 1511c4177479SAlexey Kardashevskiy } 1512c4177479SAlexey Kardashevskiy 1513b7d1f77aSBenjamin Herrenschmidt /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */ 1514b7d1f77aSBenjamin Herrenschmidt load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD; 151553018216SPaolo Bonzini 151653018216SPaolo Bonzini /* We aim for a hash table of size 1/128 the size of RAM. The 151753018216SPaolo Bonzini * normal rule of thumb is 1/64 the size of RAM, but that's much 151853018216SPaolo Bonzini * more than needed for the Linux guests we support. */ 151953018216SPaolo Bonzini spapr->htab_shift = 18; /* Minimum architected size */ 152053018216SPaolo Bonzini while (spapr->htab_shift <= 46) { 1521fb164994SDavid Gibson if ((1ULL << (spapr->htab_shift + 7)) >= machine->ram_size) { 152253018216SPaolo Bonzini break; 152353018216SPaolo Bonzini } 152453018216SPaolo Bonzini spapr->htab_shift++; 152553018216SPaolo Bonzini } 152653018216SPaolo Bonzini 15277b565160SDavid Gibson /* Set up Interrupt Controller before we create the VCPUs */ 1528446f16a6SMarcel Apfelbaum spapr->icp = xics_system_init(machine, 15299e734e3dSBharata B Rao DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(), 1530f303f117SGreg Kurz smp_threads), 15317b565160SDavid Gibson XICS_IRQS); 15327b565160SDavid Gibson 153353018216SPaolo Bonzini /* init CPUs */ 153419fb2c36SBharata B Rao if (machine->cpu_model == NULL) { 153519fb2c36SBharata B Rao machine->cpu_model = kvm_enabled() ? "host" : "POWER7"; 153653018216SPaolo Bonzini } 153753018216SPaolo Bonzini for (i = 0; i < smp_cpus; i++) { 153819fb2c36SBharata B Rao cpu = cpu_ppc_init(machine->cpu_model); 153953018216SPaolo Bonzini if (cpu == NULL) { 154053018216SPaolo Bonzini fprintf(stderr, "Unable to find PowerPC CPU definition\n"); 154153018216SPaolo Bonzini exit(1); 154253018216SPaolo Bonzini } 1543bab99ea0SBharata B Rao spapr_cpu_init(spapr, cpu); 154453018216SPaolo Bonzini } 154553018216SPaolo Bonzini 1546026bfd89SDavid Gibson if (kvm_enabled()) { 1547026bfd89SDavid Gibson /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */ 1548026bfd89SDavid Gibson kvmppc_enable_logical_ci_hcalls(); 1549026bfd89SDavid Gibson } 1550026bfd89SDavid Gibson 155153018216SPaolo Bonzini /* allocate RAM */ 1552f92f5da1SAlexey Kardashevskiy memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram", 1553fb164994SDavid Gibson machine->ram_size); 1554f92f5da1SAlexey Kardashevskiy memory_region_add_subregion(sysmem, 0, ram); 155553018216SPaolo Bonzini 1556658fa66bSAlexey Kardashevskiy if (rma_alloc_size && rma) { 1557658fa66bSAlexey Kardashevskiy rma_region = g_new(MemoryRegion, 1); 1558658fa66bSAlexey Kardashevskiy memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma", 1559658fa66bSAlexey Kardashevskiy rma_alloc_size, rma); 1560658fa66bSAlexey Kardashevskiy vmstate_register_ram_global(rma_region); 1561658fa66bSAlexey Kardashevskiy memory_region_add_subregion(sysmem, 0, rma_region); 1562658fa66bSAlexey Kardashevskiy } 1563658fa66bSAlexey Kardashevskiy 156453018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin"); 15654c56440dSStefan Weil if (!filename) { 1566730fce59SThomas Huth error_report("Could not find LPAR rtas '%s'", "spapr-rtas.bin"); 15674c56440dSStefan Weil exit(1); 15684c56440dSStefan Weil } 1569b7d1f77aSBenjamin Herrenschmidt spapr->rtas_size = get_image_size(filename); 1570b7d1f77aSBenjamin Herrenschmidt spapr->rtas_blob = g_malloc(spapr->rtas_size); 1571b7d1f77aSBenjamin Herrenschmidt if (load_image_size(filename, spapr->rtas_blob, spapr->rtas_size) < 0) { 1572730fce59SThomas Huth error_report("Could not load LPAR rtas '%s'", filename); 157353018216SPaolo Bonzini exit(1); 157453018216SPaolo Bonzini } 157553018216SPaolo Bonzini if (spapr->rtas_size > RTAS_MAX_SIZE) { 1576730fce59SThomas Huth error_report("RTAS too big ! 0x%zx bytes (max is 0x%x)", 15772f285bddSPeter Maydell (size_t)spapr->rtas_size, RTAS_MAX_SIZE); 157853018216SPaolo Bonzini exit(1); 157953018216SPaolo Bonzini } 158053018216SPaolo Bonzini g_free(filename); 158153018216SPaolo Bonzini 158253018216SPaolo Bonzini /* Set up EPOW events infrastructure */ 158353018216SPaolo Bonzini spapr_events_init(spapr); 158453018216SPaolo Bonzini 158512f42174SDavid Gibson /* Set up the RTC RTAS interfaces */ 158628df36a1SDavid Gibson spapr_rtc_create(spapr); 158712f42174SDavid Gibson 158853018216SPaolo Bonzini /* Set up VIO bus */ 158953018216SPaolo Bonzini spapr->vio_bus = spapr_vio_bus_init(); 159053018216SPaolo Bonzini 159153018216SPaolo Bonzini for (i = 0; i < MAX_SERIAL_PORTS; i++) { 159253018216SPaolo Bonzini if (serial_hds[i]) { 159353018216SPaolo Bonzini spapr_vty_create(spapr->vio_bus, serial_hds[i]); 159453018216SPaolo Bonzini } 159553018216SPaolo Bonzini } 159653018216SPaolo Bonzini 159753018216SPaolo Bonzini /* We always have at least the nvram device on VIO */ 159853018216SPaolo Bonzini spapr_create_nvram(spapr); 159953018216SPaolo Bonzini 160053018216SPaolo Bonzini /* Set up PCI */ 160153018216SPaolo Bonzini spapr_pci_rtas_init(); 160253018216SPaolo Bonzini 160389dfd6e1SDavid Gibson phb = spapr_create_phb(spapr, 0); 160453018216SPaolo Bonzini 160553018216SPaolo Bonzini for (i = 0; i < nb_nics; i++) { 160653018216SPaolo Bonzini NICInfo *nd = &nd_table[i]; 160753018216SPaolo Bonzini 160853018216SPaolo Bonzini if (!nd->model) { 160953018216SPaolo Bonzini nd->model = g_strdup("ibmveth"); 161053018216SPaolo Bonzini } 161153018216SPaolo Bonzini 161253018216SPaolo Bonzini if (strcmp(nd->model, "ibmveth") == 0) { 161353018216SPaolo Bonzini spapr_vlan_create(spapr->vio_bus, nd); 161453018216SPaolo Bonzini } else { 161529b358f9SDavid Gibson pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL); 161653018216SPaolo Bonzini } 161753018216SPaolo Bonzini } 161853018216SPaolo Bonzini 161953018216SPaolo Bonzini for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) { 162053018216SPaolo Bonzini spapr_vscsi_create(spapr->vio_bus); 162153018216SPaolo Bonzini } 162253018216SPaolo Bonzini 162353018216SPaolo Bonzini /* Graphics */ 162453018216SPaolo Bonzini if (spapr_vga_init(phb->bus)) { 162553018216SPaolo Bonzini spapr->has_graphics = true; 1626c6e76503SPaolo Bonzini machine->usb |= defaults_enabled() && !machine->usb_disabled; 162753018216SPaolo Bonzini } 162853018216SPaolo Bonzini 16294ee9ced9SMarcel Apfelbaum if (machine->usb) { 163053018216SPaolo Bonzini pci_create_simple(phb->bus, -1, "pci-ohci"); 1631c86580b8SMarkus Armbruster 163253018216SPaolo Bonzini if (spapr->has_graphics) { 1633c86580b8SMarkus Armbruster USBBus *usb_bus = usb_bus_find(-1); 1634c86580b8SMarkus Armbruster 1635c86580b8SMarkus Armbruster usb_create_simple(usb_bus, "usb-kbd"); 1636c86580b8SMarkus Armbruster usb_create_simple(usb_bus, "usb-mouse"); 163753018216SPaolo Bonzini } 163853018216SPaolo Bonzini } 163953018216SPaolo Bonzini 164053018216SPaolo Bonzini if (spapr->rma_size < (MIN_RMA_SLOF << 20)) { 164153018216SPaolo Bonzini fprintf(stderr, "qemu: pSeries SLOF firmware requires >= " 164253018216SPaolo Bonzini "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF); 164353018216SPaolo Bonzini exit(1); 164453018216SPaolo Bonzini } 164553018216SPaolo Bonzini 164653018216SPaolo Bonzini if (kernel_filename) { 164753018216SPaolo Bonzini uint64_t lowaddr = 0; 164853018216SPaolo Bonzini 164953018216SPaolo Bonzini kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL, 165053018216SPaolo Bonzini NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0); 16513b66da82SAlexey Kardashevskiy if (kernel_size == ELF_LOAD_WRONG_ENDIAN) { 165216457e7fSBenjamin Herrenschmidt kernel_size = load_elf(kernel_filename, 165316457e7fSBenjamin Herrenschmidt translate_kernel_address, NULL, 165416457e7fSBenjamin Herrenschmidt NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0); 165516457e7fSBenjamin Herrenschmidt kernel_le = kernel_size > 0; 165616457e7fSBenjamin Herrenschmidt } 165716457e7fSBenjamin Herrenschmidt if (kernel_size < 0) { 16583b66da82SAlexey Kardashevskiy fprintf(stderr, "qemu: error loading %s: %s\n", 16593b66da82SAlexey Kardashevskiy kernel_filename, load_elf_strerror(kernel_size)); 166053018216SPaolo Bonzini exit(1); 166153018216SPaolo Bonzini } 166253018216SPaolo Bonzini 166353018216SPaolo Bonzini /* load initrd */ 166453018216SPaolo Bonzini if (initrd_filename) { 166553018216SPaolo Bonzini /* Try to locate the initrd in the gap between the kernel 166653018216SPaolo Bonzini * and the firmware. Add a bit of space just in case 166753018216SPaolo Bonzini */ 166853018216SPaolo Bonzini initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff; 166953018216SPaolo Bonzini initrd_size = load_image_targphys(initrd_filename, initrd_base, 167053018216SPaolo Bonzini load_limit - initrd_base); 167153018216SPaolo Bonzini if (initrd_size < 0) { 167253018216SPaolo Bonzini fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 167353018216SPaolo Bonzini initrd_filename); 167453018216SPaolo Bonzini exit(1); 167553018216SPaolo Bonzini } 167653018216SPaolo Bonzini } else { 167753018216SPaolo Bonzini initrd_base = 0; 167853018216SPaolo Bonzini initrd_size = 0; 167953018216SPaolo Bonzini } 168053018216SPaolo Bonzini } 168153018216SPaolo Bonzini 16828e7ea787SAndreas Färber if (bios_name == NULL) { 16838e7ea787SAndreas Färber bios_name = FW_FILE_NAME; 16848e7ea787SAndreas Färber } 16858e7ea787SAndreas Färber filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 16864c56440dSStefan Weil if (!filename) { 168768fea5a0SThomas Huth error_report("Could not find LPAR firmware '%s'", bios_name); 16884c56440dSStefan Weil exit(1); 16894c56440dSStefan Weil } 169053018216SPaolo Bonzini fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE); 169168fea5a0SThomas Huth if (fw_size <= 0) { 169268fea5a0SThomas Huth error_report("Could not load LPAR firmware '%s'", filename); 169353018216SPaolo Bonzini exit(1); 169453018216SPaolo Bonzini } 169553018216SPaolo Bonzini g_free(filename); 169653018216SPaolo Bonzini 169728e02042SDavid Gibson /* FIXME: Should register things through the MachineState's qdev 169828e02042SDavid Gibson * interface, this is a legacy from the sPAPREnvironment structure 169928e02042SDavid Gibson * which predated MachineState but had a similar function */ 17004be21d56SDavid Gibson vmstate_register(NULL, 0, &vmstate_spapr, spapr); 17014be21d56SDavid Gibson register_savevm_live(NULL, "spapr/htab", -1, 1, 17024be21d56SDavid Gibson &savevm_htab_handlers, spapr); 17034be21d56SDavid Gibson 170453018216SPaolo Bonzini /* Prepare the device tree */ 17053bbf37f2SAndreas Färber spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size, 170616457e7fSBenjamin Herrenschmidt kernel_size, kernel_le, 170731fe14d1SNathan Fontenot kernel_cmdline, 170831fe14d1SNathan Fontenot spapr->check_exception_irq); 170953018216SPaolo Bonzini assert(spapr->fdt_skel != NULL); 17105b2128d2SAlexander Graf 171146503c2bSMichael Roth /* used by RTAS */ 171246503c2bSMichael Roth QTAILQ_INIT(&spapr->ccs_list); 171346503c2bSMichael Roth qemu_register_reset(spapr_ccs_reset_hook, spapr); 171446503c2bSMichael Roth 17155b2128d2SAlexander Graf qemu_register_boot_set(spapr_boot_set, spapr); 171653018216SPaolo Bonzini } 171753018216SPaolo Bonzini 1718135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type) 1719135a129aSAneesh Kumar K.V { 1720135a129aSAneesh Kumar K.V if (!vm_type) { 1721135a129aSAneesh Kumar K.V return 0; 1722135a129aSAneesh Kumar K.V } 1723135a129aSAneesh Kumar K.V 1724135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "HV")) { 1725135a129aSAneesh Kumar K.V return 1; 1726135a129aSAneesh Kumar K.V } 1727135a129aSAneesh Kumar K.V 1728135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "PR")) { 1729135a129aSAneesh Kumar K.V return 2; 1730135a129aSAneesh Kumar K.V } 1731135a129aSAneesh Kumar K.V 1732135a129aSAneesh Kumar K.V error_report("Unknown kvm-type specified '%s'", vm_type); 1733135a129aSAneesh Kumar K.V exit(1); 1734135a129aSAneesh Kumar K.V } 1735135a129aSAneesh Kumar K.V 173671461b0fSAlexey Kardashevskiy /* 1737627b84f4SGonglei * Implementation of an interface to adjust firmware path 173871461b0fSAlexey Kardashevskiy * for the bootindex property handling. 173971461b0fSAlexey Kardashevskiy */ 174071461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus, 174171461b0fSAlexey Kardashevskiy DeviceState *dev) 174271461b0fSAlexey Kardashevskiy { 174371461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \ 174471461b0fSAlexey Kardashevskiy ((type *)object_dynamic_cast(OBJECT(obj), (name))) 174571461b0fSAlexey Kardashevskiy SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE); 174671461b0fSAlexey Kardashevskiy sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE); 174771461b0fSAlexey Kardashevskiy 174871461b0fSAlexey Kardashevskiy if (d) { 174971461b0fSAlexey Kardashevskiy void *spapr = CAST(void, bus->parent, "spapr-vscsi"); 175071461b0fSAlexey Kardashevskiy VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI); 175171461b0fSAlexey Kardashevskiy USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE); 175271461b0fSAlexey Kardashevskiy 175371461b0fSAlexey Kardashevskiy if (spapr) { 175471461b0fSAlexey Kardashevskiy /* 175571461b0fSAlexey Kardashevskiy * Replace "channel@0/disk@0,0" with "disk@8000000000000000": 175671461b0fSAlexey Kardashevskiy * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun 175771461b0fSAlexey Kardashevskiy * in the top 16 bits of the 64-bit LUN 175871461b0fSAlexey Kardashevskiy */ 175971461b0fSAlexey Kardashevskiy unsigned id = 0x8000 | (d->id << 8) | d->lun; 176071461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 176171461b0fSAlexey Kardashevskiy (uint64_t)id << 48); 176271461b0fSAlexey Kardashevskiy } else if (virtio) { 176371461b0fSAlexey Kardashevskiy /* 176471461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (target << 8) | lun 176571461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 176671461b0fSAlexey Kardashevskiy * Note: the quote above is from SLOF and it is wrong, 176771461b0fSAlexey Kardashevskiy * the actual binding is: 176871461b0fSAlexey Kardashevskiy * swap 0100 or 10 << or 20 << ( target lun-id -- srplun ) 176971461b0fSAlexey Kardashevskiy */ 177071461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (d->id << 16) | d->lun; 177171461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 177271461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 177371461b0fSAlexey Kardashevskiy } else if (usb) { 177471461b0fSAlexey Kardashevskiy /* 177571461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (usb-port << 16) | lun 177671461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 177771461b0fSAlexey Kardashevskiy */ 177871461b0fSAlexey Kardashevskiy unsigned usb_port = atoi(usb->port->path); 177971461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (usb_port << 16) | d->lun; 178071461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 178171461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 178271461b0fSAlexey Kardashevskiy } 178371461b0fSAlexey Kardashevskiy } 178471461b0fSAlexey Kardashevskiy 178571461b0fSAlexey Kardashevskiy if (phb) { 178671461b0fSAlexey Kardashevskiy /* Replace "pci" with "pci@800000020000000" */ 178771461b0fSAlexey Kardashevskiy return g_strdup_printf("pci@%"PRIX64, phb->buid); 178871461b0fSAlexey Kardashevskiy } 178971461b0fSAlexey Kardashevskiy 179071461b0fSAlexey Kardashevskiy return NULL; 179171461b0fSAlexey Kardashevskiy } 179271461b0fSAlexey Kardashevskiy 179323825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp) 179423825581SEduardo Habkost { 179528e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(obj); 179623825581SEduardo Habkost 179728e02042SDavid Gibson return g_strdup(spapr->kvm_type); 179823825581SEduardo Habkost } 179923825581SEduardo Habkost 180023825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp) 180123825581SEduardo Habkost { 180228e02042SDavid Gibson sPAPRMachineState *spapr = SPAPR_MACHINE(obj); 180323825581SEduardo Habkost 180428e02042SDavid Gibson g_free(spapr->kvm_type); 180528e02042SDavid Gibson spapr->kvm_type = g_strdup(value); 180623825581SEduardo Habkost } 180723825581SEduardo Habkost 180823825581SEduardo Habkost static void spapr_machine_initfn(Object *obj) 180923825581SEduardo Habkost { 181023825581SEduardo Habkost object_property_add_str(obj, "kvm-type", 181123825581SEduardo Habkost spapr_get_kvm_type, spapr_set_kvm_type, NULL); 181249d2e648SMarcel Apfelbaum object_property_set_description(obj, "kvm-type", 181349d2e648SMarcel Apfelbaum "Specifies the KVM virtualization mode (HV, PR)", 181449d2e648SMarcel Apfelbaum NULL); 181523825581SEduardo Habkost } 181623825581SEduardo Habkost 181734316482SAlexey Kardashevskiy static void ppc_cpu_do_nmi_on_cpu(void *arg) 181834316482SAlexey Kardashevskiy { 181934316482SAlexey Kardashevskiy CPUState *cs = arg; 182034316482SAlexey Kardashevskiy 182134316482SAlexey Kardashevskiy cpu_synchronize_state(cs); 182234316482SAlexey Kardashevskiy ppc_cpu_do_system_reset(cs); 182334316482SAlexey Kardashevskiy } 182434316482SAlexey Kardashevskiy 182534316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp) 182634316482SAlexey Kardashevskiy { 182734316482SAlexey Kardashevskiy CPUState *cs; 182834316482SAlexey Kardashevskiy 182934316482SAlexey Kardashevskiy CPU_FOREACH(cs) { 183034316482SAlexey Kardashevskiy async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs); 183134316482SAlexey Kardashevskiy } 183234316482SAlexey Kardashevskiy } 183334316482SAlexey Kardashevskiy 183429ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data) 183553018216SPaolo Bonzini { 183629ee3247SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 183771461b0fSAlexey Kardashevskiy FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc); 183834316482SAlexey Kardashevskiy NMIClass *nc = NMI_CLASS(oc); 183929ee3247SAlexey Kardashevskiy 1840958db90cSMarcel Apfelbaum mc->init = ppc_spapr_init; 1841958db90cSMarcel Apfelbaum mc->reset = ppc_spapr_reset; 1842958db90cSMarcel Apfelbaum mc->block_default_type = IF_SCSI; 1843958db90cSMarcel Apfelbaum mc->max_cpus = MAX_CPUS; 1844958db90cSMarcel Apfelbaum mc->no_parallel = 1; 18455b2128d2SAlexander Graf mc->default_boot_order = ""; 1846a34944feSNikunj A Dadhania mc->default_ram_size = 512 * M_BYTE; 1847958db90cSMarcel Apfelbaum mc->kvm_type = spapr_kvm_type; 18489e3f9733SAlexander Graf mc->has_dynamic_sysbus = true; 1849e4024630SLaurent Vivier mc->pci_allow_0_address = true; 185000b4fbe2SMarcel Apfelbaum 185171461b0fSAlexey Kardashevskiy fwc->get_dev_path = spapr_get_fw_dev_path; 185234316482SAlexey Kardashevskiy nc->nmi_monitor_handler = spapr_nmi; 185353018216SPaolo Bonzini } 185453018216SPaolo Bonzini 185529ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = { 185629ee3247SAlexey Kardashevskiy .name = TYPE_SPAPR_MACHINE, 185729ee3247SAlexey Kardashevskiy .parent = TYPE_MACHINE, 18584aee7362SDavid Gibson .abstract = true, 18596ca1502eSAlexey Kardashevskiy .instance_size = sizeof(sPAPRMachineState), 186023825581SEduardo Habkost .instance_init = spapr_machine_initfn, 1861183930c0SDavid Gibson .class_size = sizeof(sPAPRMachineClass), 186229ee3247SAlexey Kardashevskiy .class_init = spapr_machine_class_init, 186371461b0fSAlexey Kardashevskiy .interfaces = (InterfaceInfo[]) { 186471461b0fSAlexey Kardashevskiy { TYPE_FW_PATH_PROVIDER }, 186534316482SAlexey Kardashevskiy { TYPE_NMI }, 186671461b0fSAlexey Kardashevskiy { } 186771461b0fSAlexey Kardashevskiy }, 186829ee3247SAlexey Kardashevskiy }; 186929ee3247SAlexey Kardashevskiy 187038ff32c6SEduardo Habkost #define SPAPR_COMPAT_2_3 \ 18717619c7b0SMichael Roth HW_COMPAT_2_3 \ 18727619c7b0SMichael Roth {\ 18737619c7b0SMichael Roth .driver = "spapr-pci-host-bridge",\ 18747619c7b0SMichael Roth .property = "dynamic-reconfiguration",\ 18757619c7b0SMichael Roth .value = "off",\ 18767619c7b0SMichael Roth }, 187738ff32c6SEduardo Habkost 1878b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_2 \ 187938ff32c6SEduardo Habkost SPAPR_COMPAT_2_3 \ 18804dfd8eaaSEduardo Habkost HW_COMPAT_2_2 \ 1881b194df47SAlexey Kardashevskiy {\ 1882b194df47SAlexey Kardashevskiy .driver = TYPE_SPAPR_PCI_HOST_BRIDGE,\ 1883b194df47SAlexey Kardashevskiy .property = "mem_win_size",\ 1884b194df47SAlexey Kardashevskiy .value = "0x20000000",\ 1885dd754bafSEduardo Habkost }, 1886b194df47SAlexey Kardashevskiy 1887b194df47SAlexey Kardashevskiy #define SPAPR_COMPAT_2_1 \ 18884dfd8eaaSEduardo Habkost SPAPR_COMPAT_2_2 \ 18894dfd8eaaSEduardo Habkost HW_COMPAT_2_1 1890b194df47SAlexey Kardashevskiy 1891d25228e7SJason Wang static void spapr_compat_2_3(Object *obj) 1892d25228e7SJason Wang { 1893ff14e817SDr. David Alan Gilbert savevm_skip_section_footers(); 189413d16814SJuan Quintela global_state_set_optional(); 1895d25228e7SJason Wang } 1896d25228e7SJason Wang 1897b0e966d0SJason Wang static void spapr_compat_2_2(Object *obj) 1898b0e966d0SJason Wang { 1899d25228e7SJason Wang spapr_compat_2_3(obj); 1900b0e966d0SJason Wang } 1901b0e966d0SJason Wang 1902b0e966d0SJason Wang static void spapr_compat_2_1(Object *obj) 1903b0e966d0SJason Wang { 1904b0e966d0SJason Wang spapr_compat_2_2(obj); 1905b0e966d0SJason Wang } 1906b0e966d0SJason Wang 1907d25228e7SJason Wang static void spapr_machine_2_3_instance_init(Object *obj) 1908d25228e7SJason Wang { 1909d25228e7SJason Wang spapr_compat_2_3(obj); 1910d25228e7SJason Wang spapr_machine_initfn(obj); 1911d25228e7SJason Wang } 1912d25228e7SJason Wang 1913b0e966d0SJason Wang static void spapr_machine_2_2_instance_init(Object *obj) 1914b0e966d0SJason Wang { 1915b0e966d0SJason Wang spapr_compat_2_2(obj); 1916b0e966d0SJason Wang spapr_machine_initfn(obj); 1917b0e966d0SJason Wang } 1918b0e966d0SJason Wang 1919b0e966d0SJason Wang static void spapr_machine_2_1_instance_init(Object *obj) 1920b0e966d0SJason Wang { 1921b0e966d0SJason Wang spapr_compat_2_1(obj); 1922b0e966d0SJason Wang spapr_machine_initfn(obj); 1923b0e966d0SJason Wang } 1924b0e966d0SJason Wang 19256026db45SAlexey Kardashevskiy static void spapr_machine_2_1_class_init(ObjectClass *oc, void *data) 19266026db45SAlexey Kardashevskiy { 19276026db45SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 192868a27b20SMichael S. Tsirkin static GlobalProperty compat_props[] = { 1929dd754bafSEduardo Habkost SPAPR_COMPAT_2_1 193068a27b20SMichael S. Tsirkin { /* end of list */ } 193168a27b20SMichael S. Tsirkin }; 19326026db45SAlexey Kardashevskiy 19336026db45SAlexey Kardashevskiy mc->desc = "pSeries Logical Partition (PAPR compliant) v2.1"; 193468a27b20SMichael S. Tsirkin mc->compat_props = compat_props; 19356026db45SAlexey Kardashevskiy } 19366026db45SAlexey Kardashevskiy 19376026db45SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_1_info = { 1938b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.1"), 19396026db45SAlexey Kardashevskiy .parent = TYPE_SPAPR_MACHINE, 19406026db45SAlexey Kardashevskiy .class_init = spapr_machine_2_1_class_init, 1941b0e966d0SJason Wang .instance_init = spapr_machine_2_1_instance_init, 19426026db45SAlexey Kardashevskiy }; 19436026db45SAlexey Kardashevskiy 19444aee7362SDavid Gibson static void spapr_machine_2_2_class_init(ObjectClass *oc, void *data) 19454aee7362SDavid Gibson { 1946b194df47SAlexey Kardashevskiy static GlobalProperty compat_props[] = { 1947dd754bafSEduardo Habkost SPAPR_COMPAT_2_2 1948b194df47SAlexey Kardashevskiy { /* end of list */ } 1949b194df47SAlexey Kardashevskiy }; 19504aee7362SDavid Gibson MachineClass *mc = MACHINE_CLASS(oc); 19514aee7362SDavid Gibson 19524aee7362SDavid Gibson mc->desc = "pSeries Logical Partition (PAPR compliant) v2.2"; 1953b194df47SAlexey Kardashevskiy mc->compat_props = compat_props; 19544aee7362SDavid Gibson } 19554aee7362SDavid Gibson 19564aee7362SDavid Gibson static const TypeInfo spapr_machine_2_2_info = { 1957b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.2"), 19584aee7362SDavid Gibson .parent = TYPE_SPAPR_MACHINE, 19594aee7362SDavid Gibson .class_init = spapr_machine_2_2_class_init, 1960b0e966d0SJason Wang .instance_init = spapr_machine_2_2_instance_init, 19614aee7362SDavid Gibson }; 19624aee7362SDavid Gibson 19633dab0244SAlexey Kardashevskiy static void spapr_machine_2_3_class_init(ObjectClass *oc, void *data) 19643dab0244SAlexey Kardashevskiy { 1965a1a45612SDavid Gibson static GlobalProperty compat_props[] = { 19667619c7b0SMichael Roth SPAPR_COMPAT_2_3 1967a1a45612SDavid Gibson { /* end of list */ } 1968a1a45612SDavid Gibson }; 19693dab0244SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 19703dab0244SAlexey Kardashevskiy 19713dab0244SAlexey Kardashevskiy mc->desc = "pSeries Logical Partition (PAPR compliant) v2.3"; 1972a1a45612SDavid Gibson mc->compat_props = compat_props; 19733dab0244SAlexey Kardashevskiy } 19743dab0244SAlexey Kardashevskiy 19753dab0244SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_3_info = { 1976b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.3"), 19773dab0244SAlexey Kardashevskiy .parent = TYPE_SPAPR_MACHINE, 19783dab0244SAlexey Kardashevskiy .class_init = spapr_machine_2_3_class_init, 1979d25228e7SJason Wang .instance_init = spapr_machine_2_3_instance_init, 1980d25228e7SJason Wang }; 1981d25228e7SJason Wang 1982d25228e7SJason Wang static void spapr_machine_2_4_class_init(ObjectClass *oc, void *data) 1983d25228e7SJason Wang { 1984d25228e7SJason Wang MachineClass *mc = MACHINE_CLASS(oc); 1985d25228e7SJason Wang 1986d25228e7SJason Wang mc->desc = "pSeries Logical Partition (PAPR compliant) v2.4"; 1987d25228e7SJason Wang mc->alias = "pseries"; 1988fb0fc8f6SDavid Gibson mc->is_default = 0; 1989d25228e7SJason Wang } 1990d25228e7SJason Wang 1991d25228e7SJason Wang static const TypeInfo spapr_machine_2_4_info = { 1992b9f072d0SEduardo Habkost .name = MACHINE_TYPE_NAME("pseries-2.4"), 1993d25228e7SJason Wang .parent = TYPE_SPAPR_MACHINE, 1994d25228e7SJason Wang .class_init = spapr_machine_2_4_class_init, 19953dab0244SAlexey Kardashevskiy }; 19963dab0244SAlexey Kardashevskiy 1997fb0fc8f6SDavid Gibson static void spapr_machine_2_5_class_init(ObjectClass *oc, void *data) 1998fb0fc8f6SDavid Gibson { 1999fb0fc8f6SDavid Gibson MachineClass *mc = MACHINE_CLASS(oc); 2000fb0fc8f6SDavid Gibson 2001fb0fc8f6SDavid Gibson mc->name = "pseries-2.5"; 2002fb0fc8f6SDavid Gibson mc->desc = "pSeries Logical Partition (PAPR compliant) v2.5"; 2003fb0fc8f6SDavid Gibson mc->alias = "pseries"; 2004fb0fc8f6SDavid Gibson mc->is_default = 1; 2005fb0fc8f6SDavid Gibson } 2006fb0fc8f6SDavid Gibson 2007fb0fc8f6SDavid Gibson static const TypeInfo spapr_machine_2_5_info = { 2008fb0fc8f6SDavid Gibson .name = MACHINE_TYPE_NAME("pseries-2.5"), 2009fb0fc8f6SDavid Gibson .parent = TYPE_SPAPR_MACHINE, 2010fb0fc8f6SDavid Gibson .class_init = spapr_machine_2_5_class_init, 2011fb0fc8f6SDavid Gibson }; 2012fb0fc8f6SDavid Gibson 201329ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void) 201429ee3247SAlexey Kardashevskiy { 201529ee3247SAlexey Kardashevskiy type_register_static(&spapr_machine_info); 20166026db45SAlexey Kardashevskiy type_register_static(&spapr_machine_2_1_info); 20174aee7362SDavid Gibson type_register_static(&spapr_machine_2_2_info); 20183dab0244SAlexey Kardashevskiy type_register_static(&spapr_machine_2_3_info); 2019d25228e7SJason Wang type_register_static(&spapr_machine_2_4_info); 2020fb0fc8f6SDavid Gibson type_register_static(&spapr_machine_2_5_info); 202129ee3247SAlexey Kardashevskiy } 202229ee3247SAlexey Kardashevskiy 202329ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types) 2024