153018216SPaolo Bonzini /* 253018216SPaolo Bonzini * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 353018216SPaolo Bonzini * 453018216SPaolo Bonzini * Copyright (c) 2004-2007 Fabrice Bellard 553018216SPaolo Bonzini * Copyright (c) 2007 Jocelyn Mayer 653018216SPaolo Bonzini * Copyright (c) 2010 David Gibson, IBM Corporation. 753018216SPaolo Bonzini * 853018216SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 953018216SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 1053018216SPaolo Bonzini * in the Software without restriction, including without limitation the rights 1153018216SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1253018216SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 1353018216SPaolo Bonzini * furnished to do so, subject to the following conditions: 1453018216SPaolo Bonzini * 1553018216SPaolo Bonzini * The above copyright notice and this permission notice shall be included in 1653018216SPaolo Bonzini * all copies or substantial portions of the Software. 1753018216SPaolo Bonzini * 1853018216SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1953018216SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2053018216SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2153018216SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2253018216SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2353018216SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2453018216SPaolo Bonzini * THE SOFTWARE. 2553018216SPaolo Bonzini * 2653018216SPaolo Bonzini */ 2753018216SPaolo Bonzini #include "sysemu/sysemu.h" 2853018216SPaolo Bonzini #include "hw/hw.h" 2971461b0fSAlexey Kardashevskiy #include "hw/fw-path-provider.h" 3053018216SPaolo Bonzini #include "elf.h" 3153018216SPaolo Bonzini #include "net/net.h" 3253018216SPaolo Bonzini #include "sysemu/blockdev.h" 3353018216SPaolo Bonzini #include "sysemu/cpus.h" 3453018216SPaolo Bonzini #include "sysemu/kvm.h" 3553018216SPaolo Bonzini #include "kvm_ppc.h" 364be21d56SDavid Gibson #include "mmu-hash64.h" 373794d548SAlexey Kardashevskiy #include "qom/cpu.h" 3853018216SPaolo Bonzini 3953018216SPaolo Bonzini #include "hw/boards.h" 400d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h" 4153018216SPaolo Bonzini #include "hw/loader.h" 4253018216SPaolo Bonzini 430d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h" 440d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h" 450d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h" 460d09e41aSPaolo Bonzini #include "hw/ppc/xics.h" 4753018216SPaolo Bonzini #include "hw/pci/msi.h" 4853018216SPaolo Bonzini 4953018216SPaolo Bonzini #include "hw/pci/pci.h" 5071461b0fSAlexey Kardashevskiy #include "hw/scsi/scsi.h" 5171461b0fSAlexey Kardashevskiy #include "hw/virtio/virtio-scsi.h" 5253018216SPaolo Bonzini 5353018216SPaolo Bonzini #include "exec/address-spaces.h" 5453018216SPaolo Bonzini #include "hw/usb.h" 5553018216SPaolo Bonzini #include "qemu/config-file.h" 56135a129aSAneesh Kumar K.V #include "qemu/error-report.h" 572a6593cbSAlexey Kardashevskiy #include "trace.h" 5834316482SAlexey Kardashevskiy #include "hw/nmi.h" 5953018216SPaolo Bonzini 6053018216SPaolo Bonzini #include <libfdt.h> 6153018216SPaolo Bonzini 6253018216SPaolo Bonzini /* SLOF memory layout: 6353018216SPaolo Bonzini * 6453018216SPaolo Bonzini * SLOF raw image loaded at 0, copies its romfs right below the flat 6553018216SPaolo Bonzini * device-tree, then position SLOF itself 31M below that 6653018216SPaolo Bonzini * 6753018216SPaolo Bonzini * So we set FW_OVERHEAD to 40MB which should account for all of that 6853018216SPaolo Bonzini * and more 6953018216SPaolo Bonzini * 7053018216SPaolo Bonzini * We load our kernel at 4M, leaving space for SLOF initial image 7153018216SPaolo Bonzini */ 723bf6eeddSAlexey Kardashevskiy #define FDT_MAX_SIZE 0x40000 7353018216SPaolo Bonzini #define RTAS_MAX_SIZE 0x10000 7453018216SPaolo Bonzini #define FW_MAX_SIZE 0x400000 7553018216SPaolo Bonzini #define FW_FILE_NAME "slof.bin" 7653018216SPaolo Bonzini #define FW_OVERHEAD 0x2800000 7753018216SPaolo Bonzini #define KERNEL_LOAD_ADDR FW_MAX_SIZE 7853018216SPaolo Bonzini 7953018216SPaolo Bonzini #define MIN_RMA_SLOF 128UL 8053018216SPaolo Bonzini 8153018216SPaolo Bonzini #define TIMEBASE_FREQ 512000000ULL 8253018216SPaolo Bonzini 8353018216SPaolo Bonzini #define MAX_CPUS 256 8453018216SPaolo Bonzini 8553018216SPaolo Bonzini #define PHANDLE_XICP 0x00001111 8653018216SPaolo Bonzini 8753018216SPaolo Bonzini #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift)) 8853018216SPaolo Bonzini 896ca1502eSAlexey Kardashevskiy typedef struct sPAPRMachineState sPAPRMachineState; 90748abce9SEduardo Habkost 9129ee3247SAlexey Kardashevskiy #define TYPE_SPAPR_MACHINE "spapr-machine" 92748abce9SEduardo Habkost #define SPAPR_MACHINE(obj) \ 936ca1502eSAlexey Kardashevskiy OBJECT_CHECK(sPAPRMachineState, (obj), TYPE_SPAPR_MACHINE) 94748abce9SEduardo Habkost 95748abce9SEduardo Habkost /** 966ca1502eSAlexey Kardashevskiy * sPAPRMachineState: 97748abce9SEduardo Habkost */ 986ca1502eSAlexey Kardashevskiy struct sPAPRMachineState { 99748abce9SEduardo Habkost /*< private >*/ 100748abce9SEduardo Habkost MachineState parent_obj; 10123825581SEduardo Habkost 10223825581SEduardo Habkost /*< public >*/ 10323825581SEduardo Habkost char *kvm_type; 104748abce9SEduardo Habkost }; 105748abce9SEduardo Habkost 10653018216SPaolo Bonzini sPAPREnvironment *spapr; 10753018216SPaolo Bonzini 108c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers, 109c04d6cfaSAnthony Liguori int nr_irqs) 110c04d6cfaSAnthony Liguori { 111c04d6cfaSAnthony Liguori DeviceState *dev; 112c04d6cfaSAnthony Liguori 113c04d6cfaSAnthony Liguori dev = qdev_create(NULL, type); 114c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_servers", nr_servers); 115c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs); 116c04d6cfaSAnthony Liguori if (qdev_init(dev) < 0) { 117c04d6cfaSAnthony Liguori return NULL; 118c04d6cfaSAnthony Liguori } 119c04d6cfaSAnthony Liguori 1205a3d7b23SAlexey Kardashevskiy return XICS_COMMON(dev); 121c04d6cfaSAnthony Liguori } 122c04d6cfaSAnthony Liguori 123c04d6cfaSAnthony Liguori static XICSState *xics_system_init(int nr_servers, int nr_irqs) 124c04d6cfaSAnthony Liguori { 125c04d6cfaSAnthony Liguori XICSState *icp = NULL; 126c04d6cfaSAnthony Liguori 12711ad93f6SDavid Gibson if (kvm_enabled()) { 12811ad93f6SDavid Gibson QemuOpts *machine_opts = qemu_get_machine_opts(); 12911ad93f6SDavid Gibson bool irqchip_allowed = qemu_opt_get_bool(machine_opts, 13011ad93f6SDavid Gibson "kernel_irqchip", true); 13111ad93f6SDavid Gibson bool irqchip_required = qemu_opt_get_bool(machine_opts, 13211ad93f6SDavid Gibson "kernel_irqchip", false); 13311ad93f6SDavid Gibson if (irqchip_allowed) { 13411ad93f6SDavid Gibson icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs); 13511ad93f6SDavid Gibson } 13611ad93f6SDavid Gibson 13711ad93f6SDavid Gibson if (irqchip_required && !icp) { 13811ad93f6SDavid Gibson perror("Failed to create in-kernel XICS\n"); 13911ad93f6SDavid Gibson abort(); 14011ad93f6SDavid Gibson } 14111ad93f6SDavid Gibson } 14211ad93f6SDavid Gibson 14311ad93f6SDavid Gibson if (!icp) { 144c04d6cfaSAnthony Liguori icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs); 14511ad93f6SDavid Gibson } 14611ad93f6SDavid Gibson 147c04d6cfaSAnthony Liguori if (!icp) { 148c04d6cfaSAnthony Liguori perror("Failed to create XICS\n"); 149c04d6cfaSAnthony Liguori abort(); 150c04d6cfaSAnthony Liguori } 151c04d6cfaSAnthony Liguori 152c04d6cfaSAnthony Liguori return icp; 153c04d6cfaSAnthony Liguori } 154c04d6cfaSAnthony Liguori 155833d4668SAlexey Kardashevskiy static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu, 156833d4668SAlexey Kardashevskiy int smt_threads) 157833d4668SAlexey Kardashevskiy { 158833d4668SAlexey Kardashevskiy int i, ret = 0; 159833d4668SAlexey Kardashevskiy uint32_t servers_prop[smt_threads]; 160833d4668SAlexey Kardashevskiy uint32_t gservers_prop[smt_threads * 2]; 161833d4668SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 162833d4668SAlexey Kardashevskiy 1636d9412eaSAlexey Kardashevskiy if (cpu->cpu_version) { 1644bce526eSLaurent Dufour ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->cpu_version); 1656d9412eaSAlexey Kardashevskiy if (ret < 0) { 1666d9412eaSAlexey Kardashevskiy return ret; 1676d9412eaSAlexey Kardashevskiy } 1686d9412eaSAlexey Kardashevskiy } 1696d9412eaSAlexey Kardashevskiy 170833d4668SAlexey Kardashevskiy /* Build interrupt servers and gservers properties */ 171833d4668SAlexey Kardashevskiy for (i = 0; i < smt_threads; i++) { 172833d4668SAlexey Kardashevskiy servers_prop[i] = cpu_to_be32(index + i); 173833d4668SAlexey Kardashevskiy /* Hack, direct the group queues back to cpu 0 */ 174833d4668SAlexey Kardashevskiy gservers_prop[i*2] = cpu_to_be32(index + i); 175833d4668SAlexey Kardashevskiy gservers_prop[i*2 + 1] = 0; 176833d4668SAlexey Kardashevskiy } 177833d4668SAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s", 178833d4668SAlexey Kardashevskiy servers_prop, sizeof(servers_prop)); 179833d4668SAlexey Kardashevskiy if (ret < 0) { 180833d4668SAlexey Kardashevskiy return ret; 181833d4668SAlexey Kardashevskiy } 182833d4668SAlexey Kardashevskiy ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s", 183833d4668SAlexey Kardashevskiy gservers_prop, sizeof(gservers_prop)); 184833d4668SAlexey Kardashevskiy 185833d4668SAlexey Kardashevskiy return ret; 186833d4668SAlexey Kardashevskiy } 187833d4668SAlexey Kardashevskiy 18853018216SPaolo Bonzini static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr) 18953018216SPaolo Bonzini { 19082677ed2SAlexey Kardashevskiy int ret = 0, offset, cpus_offset; 19182677ed2SAlexey Kardashevskiy CPUState *cs; 19253018216SPaolo Bonzini char cpu_model[32]; 19353018216SPaolo Bonzini int smt = kvmppc_smt_threads(); 19453018216SPaolo Bonzini uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)}; 19553018216SPaolo Bonzini 19682677ed2SAlexey Kardashevskiy CPU_FOREACH(cs) { 19782677ed2SAlexey Kardashevskiy PowerPCCPU *cpu = POWERPC_CPU(cs); 19882677ed2SAlexey Kardashevskiy DeviceClass *dc = DEVICE_GET_CLASS(cs); 19982677ed2SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 20053018216SPaolo Bonzini uint32_t associativity[] = {cpu_to_be32(0x5), 20153018216SPaolo Bonzini cpu_to_be32(0x0), 20253018216SPaolo Bonzini cpu_to_be32(0x0), 20353018216SPaolo Bonzini cpu_to_be32(0x0), 20482677ed2SAlexey Kardashevskiy cpu_to_be32(cs->numa_node), 2050f20ba62SAlexey Kardashevskiy cpu_to_be32(index)}; 20653018216SPaolo Bonzini 2070f20ba62SAlexey Kardashevskiy if ((index % smt) != 0) { 20853018216SPaolo Bonzini continue; 20953018216SPaolo Bonzini } 21053018216SPaolo Bonzini 21182677ed2SAlexey Kardashevskiy snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index); 21253018216SPaolo Bonzini 21382677ed2SAlexey Kardashevskiy cpus_offset = fdt_path_offset(fdt, "/cpus"); 21482677ed2SAlexey Kardashevskiy if (cpus_offset < 0) { 21582677ed2SAlexey Kardashevskiy cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), 21682677ed2SAlexey Kardashevskiy "cpus"); 21782677ed2SAlexey Kardashevskiy if (cpus_offset < 0) { 21882677ed2SAlexey Kardashevskiy return cpus_offset; 21982677ed2SAlexey Kardashevskiy } 22082677ed2SAlexey Kardashevskiy } 22182677ed2SAlexey Kardashevskiy offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model); 22282677ed2SAlexey Kardashevskiy if (offset < 0) { 22382677ed2SAlexey Kardashevskiy offset = fdt_add_subnode(fdt, cpus_offset, cpu_model); 22453018216SPaolo Bonzini if (offset < 0) { 22553018216SPaolo Bonzini return offset; 22653018216SPaolo Bonzini } 22782677ed2SAlexey Kardashevskiy } 22853018216SPaolo Bonzini 22953018216SPaolo Bonzini if (nb_numa_nodes > 1) { 23053018216SPaolo Bonzini ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity, 23153018216SPaolo Bonzini sizeof(associativity)); 23253018216SPaolo Bonzini if (ret < 0) { 23353018216SPaolo Bonzini return ret; 23453018216SPaolo Bonzini } 23553018216SPaolo Bonzini } 23653018216SPaolo Bonzini 23753018216SPaolo Bonzini ret = fdt_setprop(fdt, offset, "ibm,pft-size", 23853018216SPaolo Bonzini pft_size_prop, sizeof(pft_size_prop)); 23953018216SPaolo Bonzini if (ret < 0) { 24053018216SPaolo Bonzini return ret; 24153018216SPaolo Bonzini } 242833d4668SAlexey Kardashevskiy 24382677ed2SAlexey Kardashevskiy ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu, 2442a48d993SAlexey Kardashevskiy ppc_get_compat_smt_threads(cpu)); 245833d4668SAlexey Kardashevskiy if (ret < 0) { 246833d4668SAlexey Kardashevskiy return ret; 247833d4668SAlexey Kardashevskiy } 24853018216SPaolo Bonzini } 24953018216SPaolo Bonzini return ret; 25053018216SPaolo Bonzini } 25153018216SPaolo Bonzini 25253018216SPaolo Bonzini 25353018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop, 25453018216SPaolo Bonzini size_t maxsize) 25553018216SPaolo Bonzini { 25653018216SPaolo Bonzini size_t maxcells = maxsize / sizeof(uint32_t); 25753018216SPaolo Bonzini int i, j, count; 25853018216SPaolo Bonzini uint32_t *p = prop; 25953018216SPaolo Bonzini 26053018216SPaolo Bonzini for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 26153018216SPaolo Bonzini struct ppc_one_seg_page_size *sps = &env->sps.sps[i]; 26253018216SPaolo Bonzini 26353018216SPaolo Bonzini if (!sps->page_shift) { 26453018216SPaolo Bonzini break; 26553018216SPaolo Bonzini } 26653018216SPaolo Bonzini for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) { 26753018216SPaolo Bonzini if (sps->enc[count].page_shift == 0) { 26853018216SPaolo Bonzini break; 26953018216SPaolo Bonzini } 27053018216SPaolo Bonzini } 27153018216SPaolo Bonzini if ((p - prop) >= (maxcells - 3 - count * 2)) { 27253018216SPaolo Bonzini break; 27353018216SPaolo Bonzini } 27453018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->page_shift); 27553018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->slb_enc); 27653018216SPaolo Bonzini *(p++) = cpu_to_be32(count); 27753018216SPaolo Bonzini for (j = 0; j < count; j++) { 27853018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->enc[j].page_shift); 27953018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->enc[j].pte_enc); 28053018216SPaolo Bonzini } 28153018216SPaolo Bonzini } 28253018216SPaolo Bonzini 28353018216SPaolo Bonzini return (p - prop) * sizeof(uint32_t); 28453018216SPaolo Bonzini } 28553018216SPaolo Bonzini 286b082d65aSAlexey Kardashevskiy static hwaddr spapr_node0_size(void) 287b082d65aSAlexey Kardashevskiy { 288b082d65aSAlexey Kardashevskiy if (nb_numa_nodes) { 289b082d65aSAlexey Kardashevskiy int i; 290b082d65aSAlexey Kardashevskiy for (i = 0; i < nb_numa_nodes; ++i) { 291b082d65aSAlexey Kardashevskiy if (numa_info[i].node_mem) { 292b082d65aSAlexey Kardashevskiy return MIN(pow2floor(numa_info[i].node_mem), ram_size); 293b082d65aSAlexey Kardashevskiy } 294b082d65aSAlexey Kardashevskiy } 295b082d65aSAlexey Kardashevskiy } 296b082d65aSAlexey Kardashevskiy return ram_size; 297b082d65aSAlexey Kardashevskiy } 298b082d65aSAlexey Kardashevskiy 29953018216SPaolo Bonzini #define _FDT(exp) \ 30053018216SPaolo Bonzini do { \ 30153018216SPaolo Bonzini int ret = (exp); \ 30253018216SPaolo Bonzini if (ret < 0) { \ 30353018216SPaolo Bonzini fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \ 30453018216SPaolo Bonzini #exp, fdt_strerror(ret)); \ 30553018216SPaolo Bonzini exit(1); \ 30653018216SPaolo Bonzini } \ 30753018216SPaolo Bonzini } while (0) 30853018216SPaolo Bonzini 309a1d59c0fSAlexey Kardashevskiy static void add_str(GString *s, const gchar *s1) 310a1d59c0fSAlexey Kardashevskiy { 311a1d59c0fSAlexey Kardashevskiy g_string_append_len(s, s1, strlen(s1) + 1); 312a1d59c0fSAlexey Kardashevskiy } 31353018216SPaolo Bonzini 3143bbf37f2SAndreas Färber static void *spapr_create_fdt_skel(hwaddr initrd_base, 31553018216SPaolo Bonzini hwaddr initrd_size, 31653018216SPaolo Bonzini hwaddr kernel_size, 31716457e7fSBenjamin Herrenschmidt bool little_endian, 31853018216SPaolo Bonzini const char *boot_device, 31953018216SPaolo Bonzini const char *kernel_cmdline, 32053018216SPaolo Bonzini uint32_t epow_irq) 32153018216SPaolo Bonzini { 32253018216SPaolo Bonzini void *fdt; 323182735efSAndreas Färber CPUState *cs; 32453018216SPaolo Bonzini uint32_t start_prop = cpu_to_be32(initrd_base); 32553018216SPaolo Bonzini uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size); 326a1d59c0fSAlexey Kardashevskiy GString *hypertas = g_string_sized_new(256); 327a1d59c0fSAlexey Kardashevskiy GString *qemu_hypertas = g_string_sized_new(256); 32853018216SPaolo Bonzini uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)}; 32953018216SPaolo Bonzini uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)}; 330833d4668SAlexey Kardashevskiy int smt = kvmppc_smt_threads(); 33153018216SPaolo Bonzini unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80}; 33210582ff8SAlexey Kardashevskiy QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL); 33310582ff8SAlexey Kardashevskiy unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0; 33410582ff8SAlexey Kardashevskiy uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1; 335ef951443SNikunj A Dadhania char *buf; 33653018216SPaolo Bonzini 337a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-pft"); 338a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-term"); 339a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-dabr"); 340a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-interrupt"); 341a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-tce"); 342a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-vio"); 343a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-splpar"); 344a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-bulk"); 345a1d59c0fSAlexey Kardashevskiy add_str(hypertas, "hcall-set-mode"); 346a1d59c0fSAlexey Kardashevskiy add_str(qemu_hypertas, "hcall-memop1"); 347a1d59c0fSAlexey Kardashevskiy 34853018216SPaolo Bonzini fdt = g_malloc0(FDT_MAX_SIZE); 34953018216SPaolo Bonzini _FDT((fdt_create(fdt, FDT_MAX_SIZE))); 35053018216SPaolo Bonzini 35153018216SPaolo Bonzini if (kernel_size) { 35253018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size))); 35353018216SPaolo Bonzini } 35453018216SPaolo Bonzini if (initrd_size) { 35553018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size))); 35653018216SPaolo Bonzini } 35753018216SPaolo Bonzini _FDT((fdt_finish_reservemap(fdt))); 35853018216SPaolo Bonzini 35953018216SPaolo Bonzini /* Root node */ 36053018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, ""))); 36153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "chrp"))); 36253018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)"))); 363fa388916SAnthony Liguori _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries"))); 36453018216SPaolo Bonzini 365ef951443SNikunj A Dadhania if (kvm_enabled()) { 366ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "hypervisor", "kvm"))); 367ef951443SNikunj A Dadhania } 368ef951443SNikunj A Dadhania 369ef951443SNikunj A Dadhania /* 370ef951443SNikunj A Dadhania * Add info to guest to indentify which host is it being run on 371ef951443SNikunj A Dadhania * and what is the uuid of the guest 372ef951443SNikunj A Dadhania */ 373ef951443SNikunj A Dadhania if (kvmppc_get_host_model(&buf)) { 374ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "host-model", buf))); 375ef951443SNikunj A Dadhania g_free(buf); 376ef951443SNikunj A Dadhania } 377ef951443SNikunj A Dadhania if (kvmppc_get_host_serial(&buf)) { 378ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "host-serial", buf))); 379ef951443SNikunj A Dadhania g_free(buf); 380ef951443SNikunj A Dadhania } 381ef951443SNikunj A Dadhania 382ef951443SNikunj A Dadhania buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1], 383ef951443SNikunj A Dadhania qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], 384ef951443SNikunj A Dadhania qemu_uuid[5], qemu_uuid[6], qemu_uuid[7], 385ef951443SNikunj A Dadhania qemu_uuid[8], qemu_uuid[9], qemu_uuid[10], 386ef951443SNikunj A Dadhania qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], 387ef951443SNikunj A Dadhania qemu_uuid[14], qemu_uuid[15]); 388ef951443SNikunj A Dadhania 389ef951443SNikunj A Dadhania _FDT((fdt_property_string(fdt, "vm,uuid", buf))); 390ef951443SNikunj A Dadhania g_free(buf); 391ef951443SNikunj A Dadhania 39253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x2))); 39353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x2))); 39453018216SPaolo Bonzini 39553018216SPaolo Bonzini /* /chosen */ 39653018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "chosen"))); 39753018216SPaolo Bonzini 39853018216SPaolo Bonzini /* Set Form1_affinity */ 39953018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5)))); 40053018216SPaolo Bonzini 40153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline))); 40253018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-start", 40353018216SPaolo Bonzini &start_prop, sizeof(start_prop)))); 40453018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-end", 40553018216SPaolo Bonzini &end_prop, sizeof(end_prop)))); 40653018216SPaolo Bonzini if (kernel_size) { 40753018216SPaolo Bonzini uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR), 40853018216SPaolo Bonzini cpu_to_be64(kernel_size) }; 40953018216SPaolo Bonzini 41053018216SPaolo Bonzini _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop)))); 41116457e7fSBenjamin Herrenschmidt if (little_endian) { 41216457e7fSBenjamin Herrenschmidt _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0))); 41316457e7fSBenjamin Herrenschmidt } 41453018216SPaolo Bonzini } 41553018216SPaolo Bonzini if (boot_device) { 41653018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device))); 41753018216SPaolo Bonzini } 418cc84c0f3SAvik Sil if (boot_menu) { 419cc84c0f3SAvik Sil _FDT((fdt_property_cell(fdt, "qemu,boot-menu", boot_menu))); 420cc84c0f3SAvik Sil } 42153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width))); 42253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height))); 42353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth))); 42453018216SPaolo Bonzini 42553018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 42653018216SPaolo Bonzini 42753018216SPaolo Bonzini /* cpus */ 42853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "cpus"))); 42953018216SPaolo Bonzini 43053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 43153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 43253018216SPaolo Bonzini 433bdc44640SAndreas Färber CPU_FOREACH(cs) { 434182735efSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 435182735efSAndreas Färber CPUPPCState *env = &cpu->env; 4363bbf37f2SAndreas Färber DeviceClass *dc = DEVICE_GET_CLASS(cs); 437182735efSAndreas Färber PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs); 4380f20ba62SAlexey Kardashevskiy int index = ppc_get_vcpu_dt_id(cpu); 43953018216SPaolo Bonzini char *nodename; 44053018216SPaolo Bonzini uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40), 44153018216SPaolo Bonzini 0xffffffff, 0xffffffff}; 44253018216SPaolo Bonzini uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ; 44353018216SPaolo Bonzini uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000; 44453018216SPaolo Bonzini uint32_t page_sizes_prop[64]; 44553018216SPaolo Bonzini size_t page_sizes_prop_size; 44653018216SPaolo Bonzini 44753018216SPaolo Bonzini if ((index % smt) != 0) { 44853018216SPaolo Bonzini continue; 44953018216SPaolo Bonzini } 45053018216SPaolo Bonzini 4513bbf37f2SAndreas Färber nodename = g_strdup_printf("%s@%x", dc->fw_name, index); 45253018216SPaolo Bonzini 45353018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, nodename))); 45453018216SPaolo Bonzini 45553018216SPaolo Bonzini g_free(nodename); 45653018216SPaolo Bonzini 45753018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "reg", index))); 45853018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "cpu"))); 45953018216SPaolo Bonzini 46053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR]))); 4610cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-block-size", 46253018216SPaolo Bonzini env->dcache_line_size))); 4630cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-line-size", 4640cbad81fSDavid Gibson env->dcache_line_size))); 4650cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-block-size", 46653018216SPaolo Bonzini env->icache_line_size))); 4670cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-line-size", 4680cbad81fSDavid Gibson env->icache_line_size))); 4690cbad81fSDavid Gibson 4700cbad81fSDavid Gibson if (pcc->l1_dcache_size) { 4710cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size))); 4720cbad81fSDavid Gibson } else { 4730cbad81fSDavid Gibson fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n"); 4740cbad81fSDavid Gibson } 4750cbad81fSDavid Gibson if (pcc->l1_icache_size) { 4760cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size))); 4770cbad81fSDavid Gibson } else { 4780cbad81fSDavid Gibson fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n"); 4790cbad81fSDavid Gibson } 4800cbad81fSDavid Gibson 48153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq))); 48253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq))); 48353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr))); 48453018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "status", "okay"))); 48553018216SPaolo Bonzini _FDT((fdt_property(fdt, "64-bit", NULL, 0))); 48653018216SPaolo Bonzini 487dcb861cbSAlexey Kardashevskiy if (env->spr_cb[SPR_PURR].oea_read) { 488dcb861cbSAlexey Kardashevskiy _FDT((fdt_property(fdt, "ibm,purr", NULL, 0))); 489dcb861cbSAlexey Kardashevskiy } 490dcb861cbSAlexey Kardashevskiy 49153018216SPaolo Bonzini if (env->mmu_model & POWERPC_MMU_1TSEG) { 49253018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,processor-segment-sizes", 49353018216SPaolo Bonzini segs, sizeof(segs)))); 49453018216SPaolo Bonzini } 49553018216SPaolo Bonzini 49653018216SPaolo Bonzini /* Advertise VMX/VSX (vector extensions) if available 49753018216SPaolo Bonzini * 0 / no property == no vector extensions 49853018216SPaolo Bonzini * 1 == VMX / Altivec available 49953018216SPaolo Bonzini * 2 == VSX available */ 50053018216SPaolo Bonzini if (env->insns_flags & PPC_ALTIVEC) { 50153018216SPaolo Bonzini uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1; 50253018216SPaolo Bonzini 50353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx))); 50453018216SPaolo Bonzini } 50553018216SPaolo Bonzini 50653018216SPaolo Bonzini /* Advertise DFP (Decimal Floating Point) if available 50753018216SPaolo Bonzini * 0 / no property == no DFP 50853018216SPaolo Bonzini * 1 == DFP available */ 50953018216SPaolo Bonzini if (env->insns_flags2 & PPC2_DFP) { 51053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,dfp", 1))); 51153018216SPaolo Bonzini } 51253018216SPaolo Bonzini 51353018216SPaolo Bonzini page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop, 51453018216SPaolo Bonzini sizeof(page_sizes_prop)); 51553018216SPaolo Bonzini if (page_sizes_prop_size) { 51653018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,segment-page-sizes", 51753018216SPaolo Bonzini page_sizes_prop, page_sizes_prop_size))); 51853018216SPaolo Bonzini } 51953018216SPaolo Bonzini 52010582ff8SAlexey Kardashevskiy _FDT((fdt_property_cell(fdt, "ibm,chip-id", 52110582ff8SAlexey Kardashevskiy cs->cpu_index / cpus_per_socket))); 52210582ff8SAlexey Kardashevskiy 52353018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 52453018216SPaolo Bonzini } 52553018216SPaolo Bonzini 52653018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 52753018216SPaolo Bonzini 52853018216SPaolo Bonzini /* RTAS */ 52953018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "rtas"))); 53053018216SPaolo Bonzini 531da95324eSAlexey Kardashevskiy if (!kvm_enabled() || kvmppc_spapr_use_multitce()) { 532da95324eSAlexey Kardashevskiy add_str(hypertas, "hcall-multi-tce"); 533da95324eSAlexey Kardashevskiy } 534a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str, 535a1d59c0fSAlexey Kardashevskiy hypertas->len))); 536a1d59c0fSAlexey Kardashevskiy g_string_free(hypertas, TRUE); 537a1d59c0fSAlexey Kardashevskiy _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str, 538a1d59c0fSAlexey Kardashevskiy qemu_hypertas->len))); 539a1d59c0fSAlexey Kardashevskiy g_string_free(qemu_hypertas, TRUE); 54053018216SPaolo Bonzini 54153018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,associativity-reference-points", 54253018216SPaolo Bonzini refpoints, sizeof(refpoints)))); 54353018216SPaolo Bonzini 54453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX))); 54553018216SPaolo Bonzini 5462e14072fSNikunj A Dadhania /* 5472e14072fSNikunj A Dadhania * According to PAPR, rtas ibm,os-term, does not gaurantee a return 5482e14072fSNikunj A Dadhania * back to the guest cpu. 5492e14072fSNikunj A Dadhania * 5502e14072fSNikunj A Dadhania * While an additional ibm,extended-os-term property indicates that 5512e14072fSNikunj A Dadhania * rtas call return will always occur. Set this property. 5522e14072fSNikunj A Dadhania */ 5532e14072fSNikunj A Dadhania _FDT((fdt_property(fdt, "ibm,extended-os-term", NULL, 0))); 5542e14072fSNikunj A Dadhania 55553018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 55653018216SPaolo Bonzini 55753018216SPaolo Bonzini /* interrupt controller */ 55853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "interrupt-controller"))); 55953018216SPaolo Bonzini 56053018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", 56153018216SPaolo Bonzini "PowerPC-External-Interrupt-Presentation"))); 56253018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp"))); 56353018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 56453018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges", 56553018216SPaolo Bonzini interrupt_server_ranges_prop, 56653018216SPaolo Bonzini sizeof(interrupt_server_ranges_prop)))); 56753018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2))); 56853018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP))); 56953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP))); 57053018216SPaolo Bonzini 57153018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 57253018216SPaolo Bonzini 57353018216SPaolo Bonzini /* vdevice */ 57453018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "vdevice"))); 57553018216SPaolo Bonzini 57653018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "vdevice"))); 57753018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice"))); 57853018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 57953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 58053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2))); 58153018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 58253018216SPaolo Bonzini 58353018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 58453018216SPaolo Bonzini 58553018216SPaolo Bonzini /* event-sources */ 58653018216SPaolo Bonzini spapr_events_fdt_skel(fdt, epow_irq); 58753018216SPaolo Bonzini 588f7d69146SAlexander Graf /* /hypervisor node */ 589f7d69146SAlexander Graf if (kvm_enabled()) { 590f7d69146SAlexander Graf uint8_t hypercall[16]; 591f7d69146SAlexander Graf 592f7d69146SAlexander Graf /* indicate KVM hypercall interface */ 593f7d69146SAlexander Graf _FDT((fdt_begin_node(fdt, "hypervisor"))); 594f7d69146SAlexander Graf _FDT((fdt_property_string(fdt, "compatible", "linux,kvm"))); 595f7d69146SAlexander Graf if (kvmppc_has_cap_fixup_hcalls()) { 596f7d69146SAlexander Graf /* 597f7d69146SAlexander Graf * Older KVM versions with older guest kernels were broken with the 598f7d69146SAlexander Graf * magic page, don't allow the guest to map it. 599f7d69146SAlexander Graf */ 600f7d69146SAlexander Graf kvmppc_get_hypercall(first_cpu->env_ptr, hypercall, 601f7d69146SAlexander Graf sizeof(hypercall)); 602f7d69146SAlexander Graf _FDT((fdt_property(fdt, "hcall-instructions", hypercall, 603f7d69146SAlexander Graf sizeof(hypercall)))); 604f7d69146SAlexander Graf } 605f7d69146SAlexander Graf _FDT((fdt_end_node(fdt))); 606f7d69146SAlexander Graf } 607f7d69146SAlexander Graf 60853018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); /* close root node */ 60953018216SPaolo Bonzini _FDT((fdt_finish(fdt))); 61053018216SPaolo Bonzini 61153018216SPaolo Bonzini return fdt; 61253018216SPaolo Bonzini } 61353018216SPaolo Bonzini 6142a6593cbSAlexey Kardashevskiy int spapr_h_cas_compose_response(target_ulong addr, target_ulong size) 6152a6593cbSAlexey Kardashevskiy { 6162a6593cbSAlexey Kardashevskiy void *fdt, *fdt_skel; 6172a6593cbSAlexey Kardashevskiy sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 }; 6182a6593cbSAlexey Kardashevskiy 6192a6593cbSAlexey Kardashevskiy size -= sizeof(hdr); 6202a6593cbSAlexey Kardashevskiy 6212a6593cbSAlexey Kardashevskiy /* Create sceleton */ 6222a6593cbSAlexey Kardashevskiy fdt_skel = g_malloc0(size); 6232a6593cbSAlexey Kardashevskiy _FDT((fdt_create(fdt_skel, size))); 6242a6593cbSAlexey Kardashevskiy _FDT((fdt_begin_node(fdt_skel, ""))); 6252a6593cbSAlexey Kardashevskiy _FDT((fdt_end_node(fdt_skel))); 6262a6593cbSAlexey Kardashevskiy _FDT((fdt_finish(fdt_skel))); 6272a6593cbSAlexey Kardashevskiy fdt = g_malloc0(size); 6282a6593cbSAlexey Kardashevskiy _FDT((fdt_open_into(fdt_skel, fdt, size))); 6292a6593cbSAlexey Kardashevskiy g_free(fdt_skel); 6302a6593cbSAlexey Kardashevskiy 6313794d548SAlexey Kardashevskiy /* Fix skeleton up */ 6323794d548SAlexey Kardashevskiy _FDT((spapr_fixup_cpu_dt(fdt, spapr))); 6332a6593cbSAlexey Kardashevskiy 6342a6593cbSAlexey Kardashevskiy /* Pack resulting tree */ 6352a6593cbSAlexey Kardashevskiy _FDT((fdt_pack(fdt))); 6362a6593cbSAlexey Kardashevskiy 6372a6593cbSAlexey Kardashevskiy if (fdt_totalsize(fdt) + sizeof(hdr) > size) { 6382a6593cbSAlexey Kardashevskiy trace_spapr_cas_failed(size); 6392a6593cbSAlexey Kardashevskiy return -1; 6402a6593cbSAlexey Kardashevskiy } 6412a6593cbSAlexey Kardashevskiy 6422a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr, &hdr, sizeof(hdr)); 6432a6593cbSAlexey Kardashevskiy cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt)); 6442a6593cbSAlexey Kardashevskiy trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr)); 6452a6593cbSAlexey Kardashevskiy g_free(fdt); 6462a6593cbSAlexey Kardashevskiy 6472a6593cbSAlexey Kardashevskiy return 0; 6482a6593cbSAlexey Kardashevskiy } 6492a6593cbSAlexey Kardashevskiy 65026a8c353SAlexey Kardashevskiy static void spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start, 65126a8c353SAlexey Kardashevskiy hwaddr size) 65226a8c353SAlexey Kardashevskiy { 65326a8c353SAlexey Kardashevskiy uint32_t associativity[] = { 65426a8c353SAlexey Kardashevskiy cpu_to_be32(0x4), /* length */ 65526a8c353SAlexey Kardashevskiy cpu_to_be32(0x0), cpu_to_be32(0x0), 656*c3b4f589SAlexey Kardashevskiy cpu_to_be32(0x0), cpu_to_be32(nodeid) 65726a8c353SAlexey Kardashevskiy }; 65826a8c353SAlexey Kardashevskiy char mem_name[32]; 65926a8c353SAlexey Kardashevskiy uint64_t mem_reg_property[2]; 66026a8c353SAlexey Kardashevskiy int off; 66126a8c353SAlexey Kardashevskiy 66226a8c353SAlexey Kardashevskiy mem_reg_property[0] = cpu_to_be64(start); 66326a8c353SAlexey Kardashevskiy mem_reg_property[1] = cpu_to_be64(size); 66426a8c353SAlexey Kardashevskiy 66526a8c353SAlexey Kardashevskiy sprintf(mem_name, "memory@" TARGET_FMT_lx, start); 66626a8c353SAlexey Kardashevskiy off = fdt_add_subnode(fdt, 0, mem_name); 66726a8c353SAlexey Kardashevskiy _FDT(off); 66826a8c353SAlexey Kardashevskiy _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 66926a8c353SAlexey Kardashevskiy _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 67026a8c353SAlexey Kardashevskiy sizeof(mem_reg_property)))); 67126a8c353SAlexey Kardashevskiy _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 67226a8c353SAlexey Kardashevskiy sizeof(associativity)))); 67326a8c353SAlexey Kardashevskiy } 67426a8c353SAlexey Kardashevskiy 67553018216SPaolo Bonzini static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt) 67653018216SPaolo Bonzini { 6777db8a127SAlexey Kardashevskiy hwaddr mem_start, node_size; 6787db8a127SAlexey Kardashevskiy int i, nb_nodes = nb_numa_nodes; 6797db8a127SAlexey Kardashevskiy NodeInfo *nodes = numa_info; 6807db8a127SAlexey Kardashevskiy NodeInfo ramnode; 68153018216SPaolo Bonzini 6827db8a127SAlexey Kardashevskiy /* No NUMA nodes, assume there is just one node with whole RAM */ 6837db8a127SAlexey Kardashevskiy if (!nb_numa_nodes) { 6847db8a127SAlexey Kardashevskiy nb_nodes = 1; 6857db8a127SAlexey Kardashevskiy ramnode.node_mem = ram_size; 6867db8a127SAlexey Kardashevskiy nodes = &ramnode; 6875fe269b1SPaul Mackerras } 68853018216SPaolo Bonzini 6897db8a127SAlexey Kardashevskiy for (i = 0, mem_start = 0; i < nb_nodes; ++i) { 6907db8a127SAlexey Kardashevskiy if (!nodes[i].node_mem) { 6917db8a127SAlexey Kardashevskiy continue; 69253018216SPaolo Bonzini } 6935fe269b1SPaul Mackerras if (mem_start >= ram_size) { 6945fe269b1SPaul Mackerras node_size = 0; 6955fe269b1SPaul Mackerras } else { 6967db8a127SAlexey Kardashevskiy node_size = nodes[i].node_mem; 6975fe269b1SPaul Mackerras if (node_size > ram_size - mem_start) { 6985fe269b1SPaul Mackerras node_size = ram_size - mem_start; 6995fe269b1SPaul Mackerras } 7005fe269b1SPaul Mackerras } 7017db8a127SAlexey Kardashevskiy if (!mem_start) { 7027db8a127SAlexey Kardashevskiy /* ppc_spapr_init() checks for rma_size <= node0_size already */ 7037db8a127SAlexey Kardashevskiy spapr_populate_memory_node(fdt, i, 0, spapr->rma_size); 7047db8a127SAlexey Kardashevskiy mem_start += spapr->rma_size; 7057db8a127SAlexey Kardashevskiy node_size -= spapr->rma_size; 7067db8a127SAlexey Kardashevskiy } 7076010818cSAlexey Kardashevskiy for ( ; node_size; ) { 7086010818cSAlexey Kardashevskiy hwaddr sizetmp = pow2floor(node_size); 7096010818cSAlexey Kardashevskiy 7106010818cSAlexey Kardashevskiy /* mem_start != 0 here */ 7116010818cSAlexey Kardashevskiy if (ctzl(mem_start) < ctzl(sizetmp)) { 7126010818cSAlexey Kardashevskiy sizetmp = 1ULL << ctzl(mem_start); 7136010818cSAlexey Kardashevskiy } 7146010818cSAlexey Kardashevskiy 7156010818cSAlexey Kardashevskiy spapr_populate_memory_node(fdt, i, mem_start, sizetmp); 7166010818cSAlexey Kardashevskiy node_size -= sizetmp; 7176010818cSAlexey Kardashevskiy mem_start += sizetmp; 7186010818cSAlexey Kardashevskiy } 71953018216SPaolo Bonzini } 72053018216SPaolo Bonzini 72153018216SPaolo Bonzini return 0; 72253018216SPaolo Bonzini } 72353018216SPaolo Bonzini 72453018216SPaolo Bonzini static void spapr_finalize_fdt(sPAPREnvironment *spapr, 72553018216SPaolo Bonzini hwaddr fdt_addr, 72653018216SPaolo Bonzini hwaddr rtas_addr, 72753018216SPaolo Bonzini hwaddr rtas_size) 72853018216SPaolo Bonzini { 72971461b0fSAlexey Kardashevskiy int ret, i; 73071461b0fSAlexey Kardashevskiy size_t cb = 0; 73171461b0fSAlexey Kardashevskiy char *bootlist; 73253018216SPaolo Bonzini void *fdt; 73353018216SPaolo Bonzini sPAPRPHBState *phb; 73453018216SPaolo Bonzini 73553018216SPaolo Bonzini fdt = g_malloc(FDT_MAX_SIZE); 73653018216SPaolo Bonzini 73753018216SPaolo Bonzini /* open out the base tree into a temp buffer for the final tweaks */ 73853018216SPaolo Bonzini _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE))); 73953018216SPaolo Bonzini 74053018216SPaolo Bonzini ret = spapr_populate_memory(spapr, fdt); 74153018216SPaolo Bonzini if (ret < 0) { 74253018216SPaolo Bonzini fprintf(stderr, "couldn't setup memory nodes in fdt\n"); 74353018216SPaolo Bonzini exit(1); 74453018216SPaolo Bonzini } 74553018216SPaolo Bonzini 74653018216SPaolo Bonzini ret = spapr_populate_vdevice(spapr->vio_bus, fdt); 74753018216SPaolo Bonzini if (ret < 0) { 74853018216SPaolo Bonzini fprintf(stderr, "couldn't setup vio devices in fdt\n"); 74953018216SPaolo Bonzini exit(1); 75053018216SPaolo Bonzini } 75153018216SPaolo Bonzini 75253018216SPaolo Bonzini QLIST_FOREACH(phb, &spapr->phbs, list) { 75353018216SPaolo Bonzini ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt); 75453018216SPaolo Bonzini } 75553018216SPaolo Bonzini 75653018216SPaolo Bonzini if (ret < 0) { 75753018216SPaolo Bonzini fprintf(stderr, "couldn't setup PCI devices in fdt\n"); 75853018216SPaolo Bonzini exit(1); 75953018216SPaolo Bonzini } 76053018216SPaolo Bonzini 76153018216SPaolo Bonzini /* RTAS */ 76253018216SPaolo Bonzini ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size); 76353018216SPaolo Bonzini if (ret < 0) { 76453018216SPaolo Bonzini fprintf(stderr, "Couldn't set up RTAS device tree properties\n"); 76553018216SPaolo Bonzini } 76653018216SPaolo Bonzini 76753018216SPaolo Bonzini /* Advertise NUMA via ibm,associativity */ 76853018216SPaolo Bonzini ret = spapr_fixup_cpu_dt(fdt, spapr); 76953018216SPaolo Bonzini if (ret < 0) { 77053018216SPaolo Bonzini fprintf(stderr, "Couldn't finalize CPU device tree properties\n"); 77153018216SPaolo Bonzini } 77253018216SPaolo Bonzini 77371461b0fSAlexey Kardashevskiy bootlist = get_boot_devices_list(&cb, true); 77471461b0fSAlexey Kardashevskiy if (cb && bootlist) { 77571461b0fSAlexey Kardashevskiy int offset = fdt_path_offset(fdt, "/chosen"); 77671461b0fSAlexey Kardashevskiy if (offset < 0) { 77771461b0fSAlexey Kardashevskiy exit(1); 77871461b0fSAlexey Kardashevskiy } 77971461b0fSAlexey Kardashevskiy for (i = 0; i < cb; i++) { 78071461b0fSAlexey Kardashevskiy if (bootlist[i] == '\n') { 78171461b0fSAlexey Kardashevskiy bootlist[i] = ' '; 78271461b0fSAlexey Kardashevskiy } 78371461b0fSAlexey Kardashevskiy 78471461b0fSAlexey Kardashevskiy } 78571461b0fSAlexey Kardashevskiy ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist); 78671461b0fSAlexey Kardashevskiy } 78771461b0fSAlexey Kardashevskiy 78853018216SPaolo Bonzini if (!spapr->has_graphics) { 78953018216SPaolo Bonzini spapr_populate_chosen_stdout(fdt, spapr->vio_bus); 79053018216SPaolo Bonzini } 79153018216SPaolo Bonzini 79253018216SPaolo Bonzini _FDT((fdt_pack(fdt))); 79353018216SPaolo Bonzini 79453018216SPaolo Bonzini if (fdt_totalsize(fdt) > FDT_MAX_SIZE) { 79553018216SPaolo Bonzini hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n", 79653018216SPaolo Bonzini fdt_totalsize(fdt), FDT_MAX_SIZE); 79753018216SPaolo Bonzini exit(1); 79853018216SPaolo Bonzini } 79953018216SPaolo Bonzini 80053018216SPaolo Bonzini cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt)); 80153018216SPaolo Bonzini 802a21a7a70SGonglei g_free(bootlist); 80353018216SPaolo Bonzini g_free(fdt); 80453018216SPaolo Bonzini } 80553018216SPaolo Bonzini 80653018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 80753018216SPaolo Bonzini { 80853018216SPaolo Bonzini return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR; 80953018216SPaolo Bonzini } 81053018216SPaolo Bonzini 81153018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu) 81253018216SPaolo Bonzini { 81353018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 81453018216SPaolo Bonzini 81553018216SPaolo Bonzini if (msr_pr) { 81653018216SPaolo Bonzini hcall_dprintf("Hypercall made with MSR[PR]=1\n"); 81753018216SPaolo Bonzini env->gpr[3] = H_PRIVILEGE; 81853018216SPaolo Bonzini } else { 81953018216SPaolo Bonzini env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]); 82053018216SPaolo Bonzini } 82153018216SPaolo Bonzini } 82253018216SPaolo Bonzini 82353018216SPaolo Bonzini static void spapr_reset_htab(sPAPREnvironment *spapr) 82453018216SPaolo Bonzini { 82553018216SPaolo Bonzini long shift; 82653018216SPaolo Bonzini 82753018216SPaolo Bonzini /* allocate hash page table. For now we always make this 16mb, 82853018216SPaolo Bonzini * later we should probably make it scale to the size of guest 82953018216SPaolo Bonzini * RAM */ 83053018216SPaolo Bonzini 83153018216SPaolo Bonzini shift = kvmppc_reset_htab(spapr->htab_shift); 83253018216SPaolo Bonzini 83353018216SPaolo Bonzini if (shift > 0) { 83453018216SPaolo Bonzini /* Kernel handles htab, we don't need to allocate one */ 83553018216SPaolo Bonzini spapr->htab_shift = shift; 8367c43bca0SAneesh Kumar K.V kvmppc_kern_htab = true; 83753018216SPaolo Bonzini } else { 83853018216SPaolo Bonzini if (!spapr->htab) { 83953018216SPaolo Bonzini /* Allocate an htab if we don't yet have one */ 84053018216SPaolo Bonzini spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr)); 84153018216SPaolo Bonzini } 84253018216SPaolo Bonzini 84353018216SPaolo Bonzini /* And clear it */ 84453018216SPaolo Bonzini memset(spapr->htab, 0, HTAB_SIZE(spapr)); 84553018216SPaolo Bonzini } 84653018216SPaolo Bonzini 84753018216SPaolo Bonzini /* Update the RMA size if necessary */ 84853018216SPaolo Bonzini if (spapr->vrma_adjust) { 849b082d65aSAlexey Kardashevskiy spapr->rma_size = kvmppc_rma_size(spapr_node0_size(), 850b082d65aSAlexey Kardashevskiy spapr->htab_shift); 85153018216SPaolo Bonzini } 85253018216SPaolo Bonzini } 85353018216SPaolo Bonzini 85453018216SPaolo Bonzini static void ppc_spapr_reset(void) 85553018216SPaolo Bonzini { 856182735efSAndreas Färber PowerPCCPU *first_ppc_cpu; 857259186a7SAndreas Färber 85853018216SPaolo Bonzini /* Reset the hash table & recalc the RMA */ 85953018216SPaolo Bonzini spapr_reset_htab(spapr); 86053018216SPaolo Bonzini 86153018216SPaolo Bonzini qemu_devices_reset(); 86253018216SPaolo Bonzini 86353018216SPaolo Bonzini /* Load the fdt */ 86453018216SPaolo Bonzini spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr, 86553018216SPaolo Bonzini spapr->rtas_size); 86653018216SPaolo Bonzini 86753018216SPaolo Bonzini /* Set up the entry state */ 868182735efSAndreas Färber first_ppc_cpu = POWERPC_CPU(first_cpu); 869182735efSAndreas Färber first_ppc_cpu->env.gpr[3] = spapr->fdt_addr; 870182735efSAndreas Färber first_ppc_cpu->env.gpr[5] = 0; 871182735efSAndreas Färber first_cpu->halted = 0; 872182735efSAndreas Färber first_ppc_cpu->env.nip = spapr->entry_point; 87353018216SPaolo Bonzini 87453018216SPaolo Bonzini } 87553018216SPaolo Bonzini 87653018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque) 87753018216SPaolo Bonzini { 87853018216SPaolo Bonzini PowerPCCPU *cpu = opaque; 879259186a7SAndreas Färber CPUState *cs = CPU(cpu); 88053018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 88153018216SPaolo Bonzini 882259186a7SAndreas Färber cpu_reset(cs); 88353018216SPaolo Bonzini 88453018216SPaolo Bonzini /* All CPUs start halted. CPU0 is unhalted from the machine level 88553018216SPaolo Bonzini * reset code and the rest are explicitly started up by the guest 88653018216SPaolo Bonzini * using an RTAS call */ 887259186a7SAndreas Färber cs->halted = 1; 88853018216SPaolo Bonzini 88953018216SPaolo Bonzini env->spr[SPR_HIOR] = 0; 89053018216SPaolo Bonzini 8914be21d56SDavid Gibson env->external_htab = (uint8_t *)spapr->htab; 8925736245cSAneesh Kumar K.V if (kvm_enabled() && !env->external_htab) { 8935736245cSAneesh Kumar K.V /* 8945736245cSAneesh Kumar K.V * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte* 8955736245cSAneesh Kumar K.V * functions do the right thing. 8965736245cSAneesh Kumar K.V */ 8975736245cSAneesh Kumar K.V env->external_htab = (void *)1; 8985736245cSAneesh Kumar K.V } 89953018216SPaolo Bonzini env->htab_base = -1; 900f3c75d42SAneesh Kumar K.V /* 901f3c75d42SAneesh Kumar K.V * htab_mask is the mask used to normalize hash value to PTEG index. 902f3c75d42SAneesh Kumar K.V * htab_shift is log2 of hash table size. 903f3c75d42SAneesh Kumar K.V * We have 8 hpte per group, and each hpte is 16 bytes. 904f3c75d42SAneesh Kumar K.V * ie have 128 bytes per hpte entry. 905f3c75d42SAneesh Kumar K.V */ 906f3c75d42SAneesh Kumar K.V env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1; 907ec4936e1SStefan Weil env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab | 90853018216SPaolo Bonzini (spapr->htab_shift - 18); 90953018216SPaolo Bonzini } 91053018216SPaolo Bonzini 91153018216SPaolo Bonzini static void spapr_create_nvram(sPAPREnvironment *spapr) 91253018216SPaolo Bonzini { 9132ff3de68SMarkus Armbruster DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram"); 9143978b863SPaolo Bonzini DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0); 91553018216SPaolo Bonzini 9163978b863SPaolo Bonzini if (dinfo) { 9173978b863SPaolo Bonzini qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv); 91853018216SPaolo Bonzini } 91953018216SPaolo Bonzini 92053018216SPaolo Bonzini qdev_init_nofail(dev); 92153018216SPaolo Bonzini 92253018216SPaolo Bonzini spapr->nvram = (struct sPAPRNVRAM *)dev; 92353018216SPaolo Bonzini } 92453018216SPaolo Bonzini 92553018216SPaolo Bonzini /* Returns whether we want to use VGA or not */ 92653018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus) 92753018216SPaolo Bonzini { 92853018216SPaolo Bonzini switch (vga_interface_type) { 92953018216SPaolo Bonzini case VGA_NONE: 9307effdaa3SMark Wu return false; 9317effdaa3SMark Wu case VGA_DEVICE: 9327effdaa3SMark Wu return true; 93353018216SPaolo Bonzini case VGA_STD: 93453018216SPaolo Bonzini return pci_vga_init(pci_bus) != NULL; 93553018216SPaolo Bonzini default: 93653018216SPaolo Bonzini fprintf(stderr, "This vga model is not supported," 93753018216SPaolo Bonzini "currently it only supports -vga std\n"); 93853018216SPaolo Bonzini exit(0); 93953018216SPaolo Bonzini } 94053018216SPaolo Bonzini } 94153018216SPaolo Bonzini 9424be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = { 9434be21d56SDavid Gibson .name = "spapr", 94498a8b524SAlexey Kardashevskiy .version_id = 2, 9454be21d56SDavid Gibson .minimum_version_id = 1, 9464be21d56SDavid Gibson .fields = (VMStateField[]) { 947ba0e5bf8SAlexey Kardashevskiy VMSTATE_UNUSED(4), /* used to be @next_irq */ 9484be21d56SDavid Gibson 9494be21d56SDavid Gibson /* RTC offset */ 9504be21d56SDavid Gibson VMSTATE_UINT64(rtc_offset, sPAPREnvironment), 95198a8b524SAlexey Kardashevskiy VMSTATE_PPC_TIMEBASE_V(tb, sPAPREnvironment, 2), 9524be21d56SDavid Gibson VMSTATE_END_OF_LIST() 9534be21d56SDavid Gibson }, 9544be21d56SDavid Gibson }; 9554be21d56SDavid Gibson 9564be21d56SDavid Gibson #define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2)) 9574be21d56SDavid Gibson #define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID) 9584be21d56SDavid Gibson #define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY) 9594be21d56SDavid Gibson #define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY)) 9604be21d56SDavid Gibson 9614be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque) 9624be21d56SDavid Gibson { 9634be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 9644be21d56SDavid Gibson 9654be21d56SDavid Gibson /* "Iteration" header */ 9664be21d56SDavid Gibson qemu_put_be32(f, spapr->htab_shift); 9674be21d56SDavid Gibson 968e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 969e68cb8b4SAlexey Kardashevskiy spapr->htab_save_index = 0; 970e68cb8b4SAlexey Kardashevskiy spapr->htab_first_pass = true; 971e68cb8b4SAlexey Kardashevskiy } else { 972e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 973e68cb8b4SAlexey Kardashevskiy 974e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = kvmppc_get_htab_fd(false); 975e68cb8b4SAlexey Kardashevskiy if (spapr->htab_fd < 0) { 976e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n", 977e68cb8b4SAlexey Kardashevskiy strerror(errno)); 978e68cb8b4SAlexey Kardashevskiy return -1; 979e68cb8b4SAlexey Kardashevskiy } 9804be21d56SDavid Gibson } 9814be21d56SDavid Gibson 982e68cb8b4SAlexey Kardashevskiy 983e68cb8b4SAlexey Kardashevskiy return 0; 984e68cb8b4SAlexey Kardashevskiy } 9854be21d56SDavid Gibson 9864be21d56SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr, 9874be21d56SDavid Gibson int64_t max_ns) 9884be21d56SDavid Gibson { 9894be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 9904be21d56SDavid Gibson int index = spapr->htab_save_index; 991bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 9924be21d56SDavid Gibson 9934be21d56SDavid Gibson assert(spapr->htab_first_pass); 9944be21d56SDavid Gibson 9954be21d56SDavid Gibson do { 9964be21d56SDavid Gibson int chunkstart; 9974be21d56SDavid Gibson 9984be21d56SDavid Gibson /* Consume invalid HPTEs */ 9994be21d56SDavid Gibson while ((index < htabslots) 10004be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 10014be21d56SDavid Gibson index++; 10024be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10034be21d56SDavid Gibson } 10044be21d56SDavid Gibson 10054be21d56SDavid Gibson /* Consume valid HPTEs */ 10064be21d56SDavid Gibson chunkstart = index; 10074be21d56SDavid Gibson while ((index < htabslots) 10084be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 10094be21d56SDavid Gibson index++; 10104be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10114be21d56SDavid Gibson } 10124be21d56SDavid Gibson 10134be21d56SDavid Gibson if (index > chunkstart) { 10144be21d56SDavid Gibson int n_valid = index - chunkstart; 10154be21d56SDavid Gibson 10164be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 10174be21d56SDavid Gibson qemu_put_be16(f, n_valid); 10184be21d56SDavid Gibson qemu_put_be16(f, 0); 10194be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 10204be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 10214be21d56SDavid Gibson 1022bc72ad67SAlex Bligh if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 10234be21d56SDavid Gibson break; 10244be21d56SDavid Gibson } 10254be21d56SDavid Gibson } 10264be21d56SDavid Gibson } while ((index < htabslots) && !qemu_file_rate_limit(f)); 10274be21d56SDavid Gibson 10284be21d56SDavid Gibson if (index >= htabslots) { 10294be21d56SDavid Gibson assert(index == htabslots); 10304be21d56SDavid Gibson index = 0; 10314be21d56SDavid Gibson spapr->htab_first_pass = false; 10324be21d56SDavid Gibson } 10334be21d56SDavid Gibson spapr->htab_save_index = index; 10344be21d56SDavid Gibson } 10354be21d56SDavid Gibson 1036e68cb8b4SAlexey Kardashevskiy static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr, 10374be21d56SDavid Gibson int64_t max_ns) 10384be21d56SDavid Gibson { 10394be21d56SDavid Gibson bool final = max_ns < 0; 10404be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 10414be21d56SDavid Gibson int examined = 0, sent = 0; 10424be21d56SDavid Gibson int index = spapr->htab_save_index; 1043bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 10444be21d56SDavid Gibson 10454be21d56SDavid Gibson assert(!spapr->htab_first_pass); 10464be21d56SDavid Gibson 10474be21d56SDavid Gibson do { 10484be21d56SDavid Gibson int chunkstart, invalidstart; 10494be21d56SDavid Gibson 10504be21d56SDavid Gibson /* Consume non-dirty HPTEs */ 10514be21d56SDavid Gibson while ((index < htabslots) 10524be21d56SDavid Gibson && !HPTE_DIRTY(HPTE(spapr->htab, index))) { 10534be21d56SDavid Gibson index++; 10544be21d56SDavid Gibson examined++; 10554be21d56SDavid Gibson } 10564be21d56SDavid Gibson 10574be21d56SDavid Gibson chunkstart = index; 10584be21d56SDavid Gibson /* Consume valid dirty HPTEs */ 10594be21d56SDavid Gibson while ((index < htabslots) 10604be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 10614be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 10624be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10634be21d56SDavid Gibson index++; 10644be21d56SDavid Gibson examined++; 10654be21d56SDavid Gibson } 10664be21d56SDavid Gibson 10674be21d56SDavid Gibson invalidstart = index; 10684be21d56SDavid Gibson /* Consume invalid dirty HPTEs */ 10694be21d56SDavid Gibson while ((index < htabslots) 10704be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 10714be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 10724be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 10734be21d56SDavid Gibson index++; 10744be21d56SDavid Gibson examined++; 10754be21d56SDavid Gibson } 10764be21d56SDavid Gibson 10774be21d56SDavid Gibson if (index > chunkstart) { 10784be21d56SDavid Gibson int n_valid = invalidstart - chunkstart; 10794be21d56SDavid Gibson int n_invalid = index - invalidstart; 10804be21d56SDavid Gibson 10814be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 10824be21d56SDavid Gibson qemu_put_be16(f, n_valid); 10834be21d56SDavid Gibson qemu_put_be16(f, n_invalid); 10844be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 10854be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 10864be21d56SDavid Gibson sent += index - chunkstart; 10874be21d56SDavid Gibson 1088bc72ad67SAlex Bligh if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) { 10894be21d56SDavid Gibson break; 10904be21d56SDavid Gibson } 10914be21d56SDavid Gibson } 10924be21d56SDavid Gibson 10934be21d56SDavid Gibson if (examined >= htabslots) { 10944be21d56SDavid Gibson break; 10954be21d56SDavid Gibson } 10964be21d56SDavid Gibson 10974be21d56SDavid Gibson if (index >= htabslots) { 10984be21d56SDavid Gibson assert(index == htabslots); 10994be21d56SDavid Gibson index = 0; 11004be21d56SDavid Gibson } 11014be21d56SDavid Gibson } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final)); 11024be21d56SDavid Gibson 11034be21d56SDavid Gibson if (index >= htabslots) { 11044be21d56SDavid Gibson assert(index == htabslots); 11054be21d56SDavid Gibson index = 0; 11064be21d56SDavid Gibson } 11074be21d56SDavid Gibson 11084be21d56SDavid Gibson spapr->htab_save_index = index; 11094be21d56SDavid Gibson 1110e68cb8b4SAlexey Kardashevskiy return (examined >= htabslots) && (sent == 0) ? 1 : 0; 11114be21d56SDavid Gibson } 11124be21d56SDavid Gibson 1113e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS 5000000 /* 5 ms */ 1114e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE 2048 1115e68cb8b4SAlexey Kardashevskiy 11164be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque) 11174be21d56SDavid Gibson { 11184be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 1119e68cb8b4SAlexey Kardashevskiy int rc = 0; 11204be21d56SDavid Gibson 11214be21d56SDavid Gibson /* Iteration header */ 11224be21d56SDavid Gibson qemu_put_be32(f, 0); 11234be21d56SDavid Gibson 1124e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1125e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1126e68cb8b4SAlexey Kardashevskiy 1127e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, 1128e68cb8b4SAlexey Kardashevskiy MAX_KVM_BUF_SIZE, MAX_ITERATION_NS); 1129e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1130e68cb8b4SAlexey Kardashevskiy return rc; 1131e68cb8b4SAlexey Kardashevskiy } 1132e68cb8b4SAlexey Kardashevskiy } else if (spapr->htab_first_pass) { 11334be21d56SDavid Gibson htab_save_first_pass(f, spapr, MAX_ITERATION_NS); 11344be21d56SDavid Gibson } else { 1135e68cb8b4SAlexey Kardashevskiy rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS); 11364be21d56SDavid Gibson } 11374be21d56SDavid Gibson 11384be21d56SDavid Gibson /* End marker */ 11394be21d56SDavid Gibson qemu_put_be32(f, 0); 11404be21d56SDavid Gibson qemu_put_be16(f, 0); 11414be21d56SDavid Gibson qemu_put_be16(f, 0); 11424be21d56SDavid Gibson 1143e68cb8b4SAlexey Kardashevskiy return rc; 11444be21d56SDavid Gibson } 11454be21d56SDavid Gibson 11464be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque) 11474be21d56SDavid Gibson { 11484be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 11494be21d56SDavid Gibson 11504be21d56SDavid Gibson /* Iteration header */ 11514be21d56SDavid Gibson qemu_put_be32(f, 0); 11524be21d56SDavid Gibson 1153e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1154e68cb8b4SAlexey Kardashevskiy int rc; 1155e68cb8b4SAlexey Kardashevskiy 1156e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1157e68cb8b4SAlexey Kardashevskiy 1158e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1); 1159e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1160e68cb8b4SAlexey Kardashevskiy return rc; 1161e68cb8b4SAlexey Kardashevskiy } 1162e68cb8b4SAlexey Kardashevskiy close(spapr->htab_fd); 1163e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = -1; 1164e68cb8b4SAlexey Kardashevskiy } else { 11654be21d56SDavid Gibson htab_save_later_pass(f, spapr, -1); 1166e68cb8b4SAlexey Kardashevskiy } 11674be21d56SDavid Gibson 11684be21d56SDavid Gibson /* End marker */ 11694be21d56SDavid Gibson qemu_put_be32(f, 0); 11704be21d56SDavid Gibson qemu_put_be16(f, 0); 11714be21d56SDavid Gibson qemu_put_be16(f, 0); 11724be21d56SDavid Gibson 11734be21d56SDavid Gibson return 0; 11744be21d56SDavid Gibson } 11754be21d56SDavid Gibson 11764be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id) 11774be21d56SDavid Gibson { 11784be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 11794be21d56SDavid Gibson uint32_t section_hdr; 1180e68cb8b4SAlexey Kardashevskiy int fd = -1; 11814be21d56SDavid Gibson 11824be21d56SDavid Gibson if (version_id < 1 || version_id > 1) { 11834be21d56SDavid Gibson fprintf(stderr, "htab_load() bad version\n"); 11844be21d56SDavid Gibson return -EINVAL; 11854be21d56SDavid Gibson } 11864be21d56SDavid Gibson 11874be21d56SDavid Gibson section_hdr = qemu_get_be32(f); 11884be21d56SDavid Gibson 11894be21d56SDavid Gibson if (section_hdr) { 11904be21d56SDavid Gibson /* First section, just the hash shift */ 11914be21d56SDavid Gibson if (spapr->htab_shift != section_hdr) { 11924be21d56SDavid Gibson return -EINVAL; 11934be21d56SDavid Gibson } 11944be21d56SDavid Gibson return 0; 11954be21d56SDavid Gibson } 11964be21d56SDavid Gibson 1197e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1198e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1199e68cb8b4SAlexey Kardashevskiy 1200e68cb8b4SAlexey Kardashevskiy fd = kvmppc_get_htab_fd(true); 1201e68cb8b4SAlexey Kardashevskiy if (fd < 0) { 1202e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n", 1203e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1204e68cb8b4SAlexey Kardashevskiy } 1205e68cb8b4SAlexey Kardashevskiy } 1206e68cb8b4SAlexey Kardashevskiy 12074be21d56SDavid Gibson while (true) { 12084be21d56SDavid Gibson uint32_t index; 12094be21d56SDavid Gibson uint16_t n_valid, n_invalid; 12104be21d56SDavid Gibson 12114be21d56SDavid Gibson index = qemu_get_be32(f); 12124be21d56SDavid Gibson n_valid = qemu_get_be16(f); 12134be21d56SDavid Gibson n_invalid = qemu_get_be16(f); 12144be21d56SDavid Gibson 12154be21d56SDavid Gibson if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) { 12164be21d56SDavid Gibson /* End of Stream */ 12174be21d56SDavid Gibson break; 12184be21d56SDavid Gibson } 12194be21d56SDavid Gibson 1220e68cb8b4SAlexey Kardashevskiy if ((index + n_valid + n_invalid) > 12214be21d56SDavid Gibson (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) { 12224be21d56SDavid Gibson /* Bad index in stream */ 12234be21d56SDavid Gibson fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) " 1224e68cb8b4SAlexey Kardashevskiy "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid, 1225e68cb8b4SAlexey Kardashevskiy spapr->htab_shift); 12264be21d56SDavid Gibson return -EINVAL; 12274be21d56SDavid Gibson } 12284be21d56SDavid Gibson 1229e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 12304be21d56SDavid Gibson if (n_valid) { 12314be21d56SDavid Gibson qemu_get_buffer(f, HPTE(spapr->htab, index), 12324be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 12334be21d56SDavid Gibson } 12344be21d56SDavid Gibson if (n_invalid) { 12354be21d56SDavid Gibson memset(HPTE(spapr->htab, index + n_valid), 0, 12364be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_invalid); 12374be21d56SDavid Gibson } 1238e68cb8b4SAlexey Kardashevskiy } else { 1239e68cb8b4SAlexey Kardashevskiy int rc; 1240e68cb8b4SAlexey Kardashevskiy 1241e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1242e68cb8b4SAlexey Kardashevskiy 1243e68cb8b4SAlexey Kardashevskiy rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid); 1244e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1245e68cb8b4SAlexey Kardashevskiy return rc; 1246e68cb8b4SAlexey Kardashevskiy } 1247e68cb8b4SAlexey Kardashevskiy } 1248e68cb8b4SAlexey Kardashevskiy } 1249e68cb8b4SAlexey Kardashevskiy 1250e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1251e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1252e68cb8b4SAlexey Kardashevskiy close(fd); 12534be21d56SDavid Gibson } 12544be21d56SDavid Gibson 12554be21d56SDavid Gibson return 0; 12564be21d56SDavid Gibson } 12574be21d56SDavid Gibson 12584be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = { 12594be21d56SDavid Gibson .save_live_setup = htab_save_setup, 12604be21d56SDavid Gibson .save_live_iterate = htab_save_iterate, 12614be21d56SDavid Gibson .save_live_complete = htab_save_complete, 12624be21d56SDavid Gibson .load_state = htab_load, 12634be21d56SDavid Gibson }; 12644be21d56SDavid Gibson 126553018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */ 12663ef96221SMarcel Apfelbaum static void ppc_spapr_init(MachineState *machine) 126753018216SPaolo Bonzini { 12683ef96221SMarcel Apfelbaum ram_addr_t ram_size = machine->ram_size; 12693ef96221SMarcel Apfelbaum const char *cpu_model = machine->cpu_model; 12703ef96221SMarcel Apfelbaum const char *kernel_filename = machine->kernel_filename; 12713ef96221SMarcel Apfelbaum const char *kernel_cmdline = machine->kernel_cmdline; 12723ef96221SMarcel Apfelbaum const char *initrd_filename = machine->initrd_filename; 12733ef96221SMarcel Apfelbaum const char *boot_device = machine->boot_order; 127453018216SPaolo Bonzini PowerPCCPU *cpu; 127553018216SPaolo Bonzini CPUPPCState *env; 127653018216SPaolo Bonzini PCIHostState *phb; 127753018216SPaolo Bonzini int i; 127853018216SPaolo Bonzini MemoryRegion *sysmem = get_system_memory(); 127953018216SPaolo Bonzini MemoryRegion *ram = g_new(MemoryRegion, 1); 1280658fa66bSAlexey Kardashevskiy MemoryRegion *rma_region; 1281658fa66bSAlexey Kardashevskiy void *rma = NULL; 128253018216SPaolo Bonzini hwaddr rma_alloc_size; 1283b082d65aSAlexey Kardashevskiy hwaddr node0_size = spapr_node0_size(); 128453018216SPaolo Bonzini uint32_t initrd_base = 0; 128553018216SPaolo Bonzini long kernel_size = 0, initrd_size = 0; 128653018216SPaolo Bonzini long load_limit, rtas_limit, fw_size; 128716457e7fSBenjamin Herrenschmidt bool kernel_le = false; 128853018216SPaolo Bonzini char *filename; 128953018216SPaolo Bonzini 129053018216SPaolo Bonzini msi_supported = true; 129153018216SPaolo Bonzini 129253018216SPaolo Bonzini spapr = g_malloc0(sizeof(*spapr)); 129353018216SPaolo Bonzini QLIST_INIT(&spapr->phbs); 129453018216SPaolo Bonzini 129553018216SPaolo Bonzini cpu_ppc_hypercall = emulate_spapr_hypercall; 129653018216SPaolo Bonzini 129753018216SPaolo Bonzini /* Allocate RMA if necessary */ 1298658fa66bSAlexey Kardashevskiy rma_alloc_size = kvmppc_alloc_rma(&rma); 129953018216SPaolo Bonzini 130053018216SPaolo Bonzini if (rma_alloc_size == -1) { 130153018216SPaolo Bonzini hw_error("qemu: Unable to create RMA\n"); 130253018216SPaolo Bonzini exit(1); 130353018216SPaolo Bonzini } 130453018216SPaolo Bonzini 1305c4177479SAlexey Kardashevskiy if (rma_alloc_size && (rma_alloc_size < node0_size)) { 130653018216SPaolo Bonzini spapr->rma_size = rma_alloc_size; 130753018216SPaolo Bonzini } else { 1308c4177479SAlexey Kardashevskiy spapr->rma_size = node0_size; 130953018216SPaolo Bonzini 131053018216SPaolo Bonzini /* With KVM, we don't actually know whether KVM supports an 131153018216SPaolo Bonzini * unbounded RMA (PR KVM) or is limited by the hash table size 131253018216SPaolo Bonzini * (HV KVM using VRMA), so we always assume the latter 131353018216SPaolo Bonzini * 131453018216SPaolo Bonzini * In that case, we also limit the initial allocations for RTAS 131553018216SPaolo Bonzini * etc... to 256M since we have no way to know what the VRMA size 131653018216SPaolo Bonzini * is going to be as it depends on the size of the hash table 131753018216SPaolo Bonzini * isn't determined yet. 131853018216SPaolo Bonzini */ 131953018216SPaolo Bonzini if (kvm_enabled()) { 132053018216SPaolo Bonzini spapr->vrma_adjust = 1; 132153018216SPaolo Bonzini spapr->rma_size = MIN(spapr->rma_size, 0x10000000); 132253018216SPaolo Bonzini } 132353018216SPaolo Bonzini } 132453018216SPaolo Bonzini 1325c4177479SAlexey Kardashevskiy if (spapr->rma_size > node0_size) { 1326c4177479SAlexey Kardashevskiy fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n", 1327c4177479SAlexey Kardashevskiy spapr->rma_size); 1328c4177479SAlexey Kardashevskiy exit(1); 1329c4177479SAlexey Kardashevskiy } 1330c4177479SAlexey Kardashevskiy 133153018216SPaolo Bonzini /* We place the device tree and RTAS just below either the top of the RMA, 133253018216SPaolo Bonzini * or just below 2GB, whichever is lowere, so that it can be 133353018216SPaolo Bonzini * processed with 32-bit real mode code if necessary */ 133453018216SPaolo Bonzini rtas_limit = MIN(spapr->rma_size, 0x80000000); 133553018216SPaolo Bonzini spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE; 133653018216SPaolo Bonzini spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE; 133753018216SPaolo Bonzini load_limit = spapr->fdt_addr - FW_OVERHEAD; 133853018216SPaolo Bonzini 133953018216SPaolo Bonzini /* We aim for a hash table of size 1/128 the size of RAM. The 134053018216SPaolo Bonzini * normal rule of thumb is 1/64 the size of RAM, but that's much 134153018216SPaolo Bonzini * more than needed for the Linux guests we support. */ 134253018216SPaolo Bonzini spapr->htab_shift = 18; /* Minimum architected size */ 134353018216SPaolo Bonzini while (spapr->htab_shift <= 46) { 134453018216SPaolo Bonzini if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) { 134553018216SPaolo Bonzini break; 134653018216SPaolo Bonzini } 134753018216SPaolo Bonzini spapr->htab_shift++; 134853018216SPaolo Bonzini } 134953018216SPaolo Bonzini 13507b565160SDavid Gibson /* Set up Interrupt Controller before we create the VCPUs */ 13517b565160SDavid Gibson spapr->icp = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads, 13527b565160SDavid Gibson XICS_IRQS); 13537b565160SDavid Gibson 135453018216SPaolo Bonzini /* init CPUs */ 135553018216SPaolo Bonzini if (cpu_model == NULL) { 135653018216SPaolo Bonzini cpu_model = kvm_enabled() ? "host" : "POWER7"; 135753018216SPaolo Bonzini } 135853018216SPaolo Bonzini for (i = 0; i < smp_cpus; i++) { 135953018216SPaolo Bonzini cpu = cpu_ppc_init(cpu_model); 136053018216SPaolo Bonzini if (cpu == NULL) { 136153018216SPaolo Bonzini fprintf(stderr, "Unable to find PowerPC CPU definition\n"); 136253018216SPaolo Bonzini exit(1); 136353018216SPaolo Bonzini } 136453018216SPaolo Bonzini env = &cpu->env; 136553018216SPaolo Bonzini 136653018216SPaolo Bonzini /* Set time-base frequency to 512 MHz */ 136753018216SPaolo Bonzini cpu_ppc_tb_init(env, TIMEBASE_FREQ); 136853018216SPaolo Bonzini 13692cf3eb6dSFabien Chouteau /* PAPR always has exception vectors in RAM not ROM. To ensure this, 13702cf3eb6dSFabien Chouteau * MSR[IP] should never be set. 13712cf3eb6dSFabien Chouteau */ 13722cf3eb6dSFabien Chouteau env->msr_mask &= ~(1 << 6); 137353018216SPaolo Bonzini 137453018216SPaolo Bonzini /* Tell KVM that we're in PAPR mode */ 137553018216SPaolo Bonzini if (kvm_enabled()) { 137653018216SPaolo Bonzini kvmppc_set_papr(cpu); 137753018216SPaolo Bonzini } 137853018216SPaolo Bonzini 13796d9412eaSAlexey Kardashevskiy if (cpu->max_compat) { 13806d9412eaSAlexey Kardashevskiy if (ppc_set_compat(cpu, cpu->max_compat) < 0) { 13816d9412eaSAlexey Kardashevskiy exit(1); 13826d9412eaSAlexey Kardashevskiy } 13836d9412eaSAlexey Kardashevskiy } 13846d9412eaSAlexey Kardashevskiy 138524408a7dSAlexey Kardashevskiy xics_cpu_setup(spapr->icp, cpu); 138624408a7dSAlexey Kardashevskiy 138753018216SPaolo Bonzini qemu_register_reset(spapr_cpu_reset, cpu); 138853018216SPaolo Bonzini } 138953018216SPaolo Bonzini 139053018216SPaolo Bonzini /* allocate RAM */ 139153018216SPaolo Bonzini spapr->ram_limit = ram_size; 1392f92f5da1SAlexey Kardashevskiy memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram", 1393f92f5da1SAlexey Kardashevskiy spapr->ram_limit); 1394f92f5da1SAlexey Kardashevskiy memory_region_add_subregion(sysmem, 0, ram); 139553018216SPaolo Bonzini 1396658fa66bSAlexey Kardashevskiy if (rma_alloc_size && rma) { 1397658fa66bSAlexey Kardashevskiy rma_region = g_new(MemoryRegion, 1); 1398658fa66bSAlexey Kardashevskiy memory_region_init_ram_ptr(rma_region, NULL, "ppc_spapr.rma", 1399658fa66bSAlexey Kardashevskiy rma_alloc_size, rma); 1400658fa66bSAlexey Kardashevskiy vmstate_register_ram_global(rma_region); 1401658fa66bSAlexey Kardashevskiy memory_region_add_subregion(sysmem, 0, rma_region); 1402658fa66bSAlexey Kardashevskiy } 1403658fa66bSAlexey Kardashevskiy 140453018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin"); 140553018216SPaolo Bonzini spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr, 140653018216SPaolo Bonzini rtas_limit - spapr->rtas_addr); 140753018216SPaolo Bonzini if (spapr->rtas_size < 0) { 140853018216SPaolo Bonzini hw_error("qemu: could not load LPAR rtas '%s'\n", filename); 140953018216SPaolo Bonzini exit(1); 141053018216SPaolo Bonzini } 141153018216SPaolo Bonzini if (spapr->rtas_size > RTAS_MAX_SIZE) { 141253018216SPaolo Bonzini hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n", 141353018216SPaolo Bonzini spapr->rtas_size, RTAS_MAX_SIZE); 141453018216SPaolo Bonzini exit(1); 141553018216SPaolo Bonzini } 141653018216SPaolo Bonzini g_free(filename); 141753018216SPaolo Bonzini 141853018216SPaolo Bonzini /* Set up EPOW events infrastructure */ 141953018216SPaolo Bonzini spapr_events_init(spapr); 142053018216SPaolo Bonzini 142153018216SPaolo Bonzini /* Set up VIO bus */ 142253018216SPaolo Bonzini spapr->vio_bus = spapr_vio_bus_init(); 142353018216SPaolo Bonzini 142453018216SPaolo Bonzini for (i = 0; i < MAX_SERIAL_PORTS; i++) { 142553018216SPaolo Bonzini if (serial_hds[i]) { 142653018216SPaolo Bonzini spapr_vty_create(spapr->vio_bus, serial_hds[i]); 142753018216SPaolo Bonzini } 142853018216SPaolo Bonzini } 142953018216SPaolo Bonzini 143053018216SPaolo Bonzini /* We always have at least the nvram device on VIO */ 143153018216SPaolo Bonzini spapr_create_nvram(spapr); 143253018216SPaolo Bonzini 143353018216SPaolo Bonzini /* Set up PCI */ 1434f1c2dc7cSAlexey Kardashevskiy spapr_pci_msi_init(spapr, SPAPR_PCI_MSI_WINDOW); 143553018216SPaolo Bonzini spapr_pci_rtas_init(); 143653018216SPaolo Bonzini 143789dfd6e1SDavid Gibson phb = spapr_create_phb(spapr, 0); 143853018216SPaolo Bonzini 143953018216SPaolo Bonzini for (i = 0; i < nb_nics; i++) { 144053018216SPaolo Bonzini NICInfo *nd = &nd_table[i]; 144153018216SPaolo Bonzini 144253018216SPaolo Bonzini if (!nd->model) { 144353018216SPaolo Bonzini nd->model = g_strdup("ibmveth"); 144453018216SPaolo Bonzini } 144553018216SPaolo Bonzini 144653018216SPaolo Bonzini if (strcmp(nd->model, "ibmveth") == 0) { 144753018216SPaolo Bonzini spapr_vlan_create(spapr->vio_bus, nd); 144853018216SPaolo Bonzini } else { 144929b358f9SDavid Gibson pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL); 145053018216SPaolo Bonzini } 145153018216SPaolo Bonzini } 145253018216SPaolo Bonzini 145353018216SPaolo Bonzini for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) { 145453018216SPaolo Bonzini spapr_vscsi_create(spapr->vio_bus); 145553018216SPaolo Bonzini } 145653018216SPaolo Bonzini 145753018216SPaolo Bonzini /* Graphics */ 145853018216SPaolo Bonzini if (spapr_vga_init(phb->bus)) { 145953018216SPaolo Bonzini spapr->has_graphics = true; 146053018216SPaolo Bonzini } 146153018216SPaolo Bonzini 146253018216SPaolo Bonzini if (usb_enabled(spapr->has_graphics)) { 146353018216SPaolo Bonzini pci_create_simple(phb->bus, -1, "pci-ohci"); 146453018216SPaolo Bonzini if (spapr->has_graphics) { 146553018216SPaolo Bonzini usbdevice_create("keyboard"); 146653018216SPaolo Bonzini usbdevice_create("mouse"); 146753018216SPaolo Bonzini } 146853018216SPaolo Bonzini } 146953018216SPaolo Bonzini 147053018216SPaolo Bonzini if (spapr->rma_size < (MIN_RMA_SLOF << 20)) { 147153018216SPaolo Bonzini fprintf(stderr, "qemu: pSeries SLOF firmware requires >= " 147253018216SPaolo Bonzini "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF); 147353018216SPaolo Bonzini exit(1); 147453018216SPaolo Bonzini } 147553018216SPaolo Bonzini 147653018216SPaolo Bonzini if (kernel_filename) { 147753018216SPaolo Bonzini uint64_t lowaddr = 0; 147853018216SPaolo Bonzini 147953018216SPaolo Bonzini kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL, 148053018216SPaolo Bonzini NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0); 14813b66da82SAlexey Kardashevskiy if (kernel_size == ELF_LOAD_WRONG_ENDIAN) { 148216457e7fSBenjamin Herrenschmidt kernel_size = load_elf(kernel_filename, 148316457e7fSBenjamin Herrenschmidt translate_kernel_address, NULL, 148416457e7fSBenjamin Herrenschmidt NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0); 148516457e7fSBenjamin Herrenschmidt kernel_le = kernel_size > 0; 148616457e7fSBenjamin Herrenschmidt } 148716457e7fSBenjamin Herrenschmidt if (kernel_size < 0) { 14883b66da82SAlexey Kardashevskiy fprintf(stderr, "qemu: error loading %s: %s\n", 14893b66da82SAlexey Kardashevskiy kernel_filename, load_elf_strerror(kernel_size)); 149053018216SPaolo Bonzini exit(1); 149153018216SPaolo Bonzini } 149253018216SPaolo Bonzini 149353018216SPaolo Bonzini /* load initrd */ 149453018216SPaolo Bonzini if (initrd_filename) { 149553018216SPaolo Bonzini /* Try to locate the initrd in the gap between the kernel 149653018216SPaolo Bonzini * and the firmware. Add a bit of space just in case 149753018216SPaolo Bonzini */ 149853018216SPaolo Bonzini initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff; 149953018216SPaolo Bonzini initrd_size = load_image_targphys(initrd_filename, initrd_base, 150053018216SPaolo Bonzini load_limit - initrd_base); 150153018216SPaolo Bonzini if (initrd_size < 0) { 150253018216SPaolo Bonzini fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 150353018216SPaolo Bonzini initrd_filename); 150453018216SPaolo Bonzini exit(1); 150553018216SPaolo Bonzini } 150653018216SPaolo Bonzini } else { 150753018216SPaolo Bonzini initrd_base = 0; 150853018216SPaolo Bonzini initrd_size = 0; 150953018216SPaolo Bonzini } 151053018216SPaolo Bonzini } 151153018216SPaolo Bonzini 15128e7ea787SAndreas Färber if (bios_name == NULL) { 15138e7ea787SAndreas Färber bios_name = FW_FILE_NAME; 15148e7ea787SAndreas Färber } 15158e7ea787SAndreas Färber filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 151653018216SPaolo Bonzini fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE); 151753018216SPaolo Bonzini if (fw_size < 0) { 151853018216SPaolo Bonzini hw_error("qemu: could not load LPAR rtas '%s'\n", filename); 151953018216SPaolo Bonzini exit(1); 152053018216SPaolo Bonzini } 152153018216SPaolo Bonzini g_free(filename); 152253018216SPaolo Bonzini 152353018216SPaolo Bonzini spapr->entry_point = 0x100; 152453018216SPaolo Bonzini 15254be21d56SDavid Gibson vmstate_register(NULL, 0, &vmstate_spapr, spapr); 15264be21d56SDavid Gibson register_savevm_live(NULL, "spapr/htab", -1, 1, 15274be21d56SDavid Gibson &savevm_htab_handlers, spapr); 15284be21d56SDavid Gibson 152953018216SPaolo Bonzini /* Prepare the device tree */ 15303bbf37f2SAndreas Färber spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size, 153116457e7fSBenjamin Herrenschmidt kernel_size, kernel_le, 153253018216SPaolo Bonzini boot_device, kernel_cmdline, 153353018216SPaolo Bonzini spapr->epow_irq); 153453018216SPaolo Bonzini assert(spapr->fdt_skel != NULL); 153553018216SPaolo Bonzini } 153653018216SPaolo Bonzini 1537135a129aSAneesh Kumar K.V static int spapr_kvm_type(const char *vm_type) 1538135a129aSAneesh Kumar K.V { 1539135a129aSAneesh Kumar K.V if (!vm_type) { 1540135a129aSAneesh Kumar K.V return 0; 1541135a129aSAneesh Kumar K.V } 1542135a129aSAneesh Kumar K.V 1543135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "HV")) { 1544135a129aSAneesh Kumar K.V return 1; 1545135a129aSAneesh Kumar K.V } 1546135a129aSAneesh Kumar K.V 1547135a129aSAneesh Kumar K.V if (!strcmp(vm_type, "PR")) { 1548135a129aSAneesh Kumar K.V return 2; 1549135a129aSAneesh Kumar K.V } 1550135a129aSAneesh Kumar K.V 1551135a129aSAneesh Kumar K.V error_report("Unknown kvm-type specified '%s'", vm_type); 1552135a129aSAneesh Kumar K.V exit(1); 1553135a129aSAneesh Kumar K.V } 1554135a129aSAneesh Kumar K.V 155571461b0fSAlexey Kardashevskiy /* 155671461b0fSAlexey Kardashevskiy * Implementation of an interface to adjust firmware patch 155771461b0fSAlexey Kardashevskiy * for the bootindex property handling. 155871461b0fSAlexey Kardashevskiy */ 155971461b0fSAlexey Kardashevskiy static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus, 156071461b0fSAlexey Kardashevskiy DeviceState *dev) 156171461b0fSAlexey Kardashevskiy { 156271461b0fSAlexey Kardashevskiy #define CAST(type, obj, name) \ 156371461b0fSAlexey Kardashevskiy ((type *)object_dynamic_cast(OBJECT(obj), (name))) 156471461b0fSAlexey Kardashevskiy SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE); 156571461b0fSAlexey Kardashevskiy sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE); 156671461b0fSAlexey Kardashevskiy 156771461b0fSAlexey Kardashevskiy if (d) { 156871461b0fSAlexey Kardashevskiy void *spapr = CAST(void, bus->parent, "spapr-vscsi"); 156971461b0fSAlexey Kardashevskiy VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI); 157071461b0fSAlexey Kardashevskiy USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE); 157171461b0fSAlexey Kardashevskiy 157271461b0fSAlexey Kardashevskiy if (spapr) { 157371461b0fSAlexey Kardashevskiy /* 157471461b0fSAlexey Kardashevskiy * Replace "channel@0/disk@0,0" with "disk@8000000000000000": 157571461b0fSAlexey Kardashevskiy * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun 157671461b0fSAlexey Kardashevskiy * in the top 16 bits of the 64-bit LUN 157771461b0fSAlexey Kardashevskiy */ 157871461b0fSAlexey Kardashevskiy unsigned id = 0x8000 | (d->id << 8) | d->lun; 157971461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 158071461b0fSAlexey Kardashevskiy (uint64_t)id << 48); 158171461b0fSAlexey Kardashevskiy } else if (virtio) { 158271461b0fSAlexey Kardashevskiy /* 158371461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (target << 8) | lun 158471461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 158571461b0fSAlexey Kardashevskiy * Note: the quote above is from SLOF and it is wrong, 158671461b0fSAlexey Kardashevskiy * the actual binding is: 158771461b0fSAlexey Kardashevskiy * swap 0100 or 10 << or 20 << ( target lun-id -- srplun ) 158871461b0fSAlexey Kardashevskiy */ 158971461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (d->id << 16) | d->lun; 159071461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 159171461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 159271461b0fSAlexey Kardashevskiy } else if (usb) { 159371461b0fSAlexey Kardashevskiy /* 159471461b0fSAlexey Kardashevskiy * We use SRP luns of the form 01000000 | (usb-port << 16) | lun 159571461b0fSAlexey Kardashevskiy * in the top 32 bits of the 64-bit LUN 159671461b0fSAlexey Kardashevskiy */ 159771461b0fSAlexey Kardashevskiy unsigned usb_port = atoi(usb->port->path); 159871461b0fSAlexey Kardashevskiy unsigned id = 0x1000000 | (usb_port << 16) | d->lun; 159971461b0fSAlexey Kardashevskiy return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev), 160071461b0fSAlexey Kardashevskiy (uint64_t)id << 32); 160171461b0fSAlexey Kardashevskiy } 160271461b0fSAlexey Kardashevskiy } 160371461b0fSAlexey Kardashevskiy 160471461b0fSAlexey Kardashevskiy if (phb) { 160571461b0fSAlexey Kardashevskiy /* Replace "pci" with "pci@800000020000000" */ 160671461b0fSAlexey Kardashevskiy return g_strdup_printf("pci@%"PRIX64, phb->buid); 160771461b0fSAlexey Kardashevskiy } 160871461b0fSAlexey Kardashevskiy 160971461b0fSAlexey Kardashevskiy return NULL; 161071461b0fSAlexey Kardashevskiy } 161171461b0fSAlexey Kardashevskiy 161223825581SEduardo Habkost static char *spapr_get_kvm_type(Object *obj, Error **errp) 161323825581SEduardo Habkost { 16146ca1502eSAlexey Kardashevskiy sPAPRMachineState *sm = SPAPR_MACHINE(obj); 161523825581SEduardo Habkost 161623825581SEduardo Habkost return g_strdup(sm->kvm_type); 161723825581SEduardo Habkost } 161823825581SEduardo Habkost 161923825581SEduardo Habkost static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp) 162023825581SEduardo Habkost { 16216ca1502eSAlexey Kardashevskiy sPAPRMachineState *sm = SPAPR_MACHINE(obj); 162223825581SEduardo Habkost 162323825581SEduardo Habkost g_free(sm->kvm_type); 162423825581SEduardo Habkost sm->kvm_type = g_strdup(value); 162523825581SEduardo Habkost } 162623825581SEduardo Habkost 162723825581SEduardo Habkost static void spapr_machine_initfn(Object *obj) 162823825581SEduardo Habkost { 162923825581SEduardo Habkost object_property_add_str(obj, "kvm-type", 163023825581SEduardo Habkost spapr_get_kvm_type, spapr_set_kvm_type, NULL); 163123825581SEduardo Habkost } 163223825581SEduardo Habkost 163334316482SAlexey Kardashevskiy static void ppc_cpu_do_nmi_on_cpu(void *arg) 163434316482SAlexey Kardashevskiy { 163534316482SAlexey Kardashevskiy CPUState *cs = arg; 163634316482SAlexey Kardashevskiy 163734316482SAlexey Kardashevskiy cpu_synchronize_state(cs); 163834316482SAlexey Kardashevskiy ppc_cpu_do_system_reset(cs); 163934316482SAlexey Kardashevskiy } 164034316482SAlexey Kardashevskiy 164134316482SAlexey Kardashevskiy static void spapr_nmi(NMIState *n, int cpu_index, Error **errp) 164234316482SAlexey Kardashevskiy { 164334316482SAlexey Kardashevskiy CPUState *cs; 164434316482SAlexey Kardashevskiy 164534316482SAlexey Kardashevskiy CPU_FOREACH(cs) { 164634316482SAlexey Kardashevskiy async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs); 164734316482SAlexey Kardashevskiy } 164834316482SAlexey Kardashevskiy } 164934316482SAlexey Kardashevskiy 165029ee3247SAlexey Kardashevskiy static void spapr_machine_class_init(ObjectClass *oc, void *data) 165153018216SPaolo Bonzini { 165229ee3247SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 165371461b0fSAlexey Kardashevskiy FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc); 165434316482SAlexey Kardashevskiy NMIClass *nc = NMI_CLASS(oc); 165529ee3247SAlexey Kardashevskiy 1656958db90cSMarcel Apfelbaum mc->name = "pseries"; 1657958db90cSMarcel Apfelbaum mc->desc = "pSeries Logical Partition (PAPR compliant)"; 1658958db90cSMarcel Apfelbaum mc->is_default = 1; 1659958db90cSMarcel Apfelbaum mc->init = ppc_spapr_init; 1660958db90cSMarcel Apfelbaum mc->reset = ppc_spapr_reset; 1661958db90cSMarcel Apfelbaum mc->block_default_type = IF_SCSI; 1662958db90cSMarcel Apfelbaum mc->max_cpus = MAX_CPUS; 1663958db90cSMarcel Apfelbaum mc->no_parallel = 1; 1664958db90cSMarcel Apfelbaum mc->default_boot_order = NULL; 1665958db90cSMarcel Apfelbaum mc->kvm_type = spapr_kvm_type; 166600b4fbe2SMarcel Apfelbaum 166771461b0fSAlexey Kardashevskiy fwc->get_dev_path = spapr_get_fw_dev_path; 166834316482SAlexey Kardashevskiy nc->nmi_monitor_handler = spapr_nmi; 166953018216SPaolo Bonzini } 167053018216SPaolo Bonzini 167129ee3247SAlexey Kardashevskiy static const TypeInfo spapr_machine_info = { 167229ee3247SAlexey Kardashevskiy .name = TYPE_SPAPR_MACHINE, 167329ee3247SAlexey Kardashevskiy .parent = TYPE_MACHINE, 16746ca1502eSAlexey Kardashevskiy .instance_size = sizeof(sPAPRMachineState), 167523825581SEduardo Habkost .instance_init = spapr_machine_initfn, 167629ee3247SAlexey Kardashevskiy .class_init = spapr_machine_class_init, 167771461b0fSAlexey Kardashevskiy .interfaces = (InterfaceInfo[]) { 167871461b0fSAlexey Kardashevskiy { TYPE_FW_PATH_PROVIDER }, 167934316482SAlexey Kardashevskiy { TYPE_NMI }, 168071461b0fSAlexey Kardashevskiy { } 168171461b0fSAlexey Kardashevskiy }, 168229ee3247SAlexey Kardashevskiy }; 168329ee3247SAlexey Kardashevskiy 16846026db45SAlexey Kardashevskiy static void spapr_machine_2_1_class_init(ObjectClass *oc, void *data) 16856026db45SAlexey Kardashevskiy { 16866026db45SAlexey Kardashevskiy MachineClass *mc = MACHINE_CLASS(oc); 16876026db45SAlexey Kardashevskiy 16886026db45SAlexey Kardashevskiy mc->name = "pseries-2.1"; 16896026db45SAlexey Kardashevskiy mc->desc = "pSeries Logical Partition (PAPR compliant) v2.1"; 16906026db45SAlexey Kardashevskiy mc->is_default = 0; 16916026db45SAlexey Kardashevskiy } 16926026db45SAlexey Kardashevskiy 16936026db45SAlexey Kardashevskiy static const TypeInfo spapr_machine_2_1_info = { 16946026db45SAlexey Kardashevskiy .name = TYPE_SPAPR_MACHINE "2.1", 16956026db45SAlexey Kardashevskiy .parent = TYPE_SPAPR_MACHINE, 16966026db45SAlexey Kardashevskiy .class_init = spapr_machine_2_1_class_init, 16976026db45SAlexey Kardashevskiy }; 16986026db45SAlexey Kardashevskiy 169929ee3247SAlexey Kardashevskiy static void spapr_machine_register_types(void) 170029ee3247SAlexey Kardashevskiy { 170129ee3247SAlexey Kardashevskiy type_register_static(&spapr_machine_info); 17026026db45SAlexey Kardashevskiy type_register_static(&spapr_machine_2_1_info); 170329ee3247SAlexey Kardashevskiy } 170429ee3247SAlexey Kardashevskiy 170529ee3247SAlexey Kardashevskiy type_init(spapr_machine_register_types) 1706