153018216SPaolo Bonzini /* 253018216SPaolo Bonzini * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 353018216SPaolo Bonzini * 453018216SPaolo Bonzini * Copyright (c) 2004-2007 Fabrice Bellard 553018216SPaolo Bonzini * Copyright (c) 2007 Jocelyn Mayer 653018216SPaolo Bonzini * Copyright (c) 2010 David Gibson, IBM Corporation. 753018216SPaolo Bonzini * 853018216SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 953018216SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 1053018216SPaolo Bonzini * in the Software without restriction, including without limitation the rights 1153018216SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1253018216SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 1353018216SPaolo Bonzini * furnished to do so, subject to the following conditions: 1453018216SPaolo Bonzini * 1553018216SPaolo Bonzini * The above copyright notice and this permission notice shall be included in 1653018216SPaolo Bonzini * all copies or substantial portions of the Software. 1753018216SPaolo Bonzini * 1853018216SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1953018216SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2053018216SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2153018216SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2253018216SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2353018216SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2453018216SPaolo Bonzini * THE SOFTWARE. 2553018216SPaolo Bonzini * 2653018216SPaolo Bonzini */ 2753018216SPaolo Bonzini #include "sysemu/sysemu.h" 2853018216SPaolo Bonzini #include "hw/hw.h" 2953018216SPaolo Bonzini #include "elf.h" 3053018216SPaolo Bonzini #include "net/net.h" 3153018216SPaolo Bonzini #include "sysemu/blockdev.h" 3253018216SPaolo Bonzini #include "sysemu/cpus.h" 3353018216SPaolo Bonzini #include "sysemu/kvm.h" 3453018216SPaolo Bonzini #include "kvm_ppc.h" 354be21d56SDavid Gibson #include "mmu-hash64.h" 3653018216SPaolo Bonzini 3753018216SPaolo Bonzini #include "hw/boards.h" 380d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h" 3953018216SPaolo Bonzini #include "hw/loader.h" 4053018216SPaolo Bonzini 410d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h" 420d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h" 430d09e41aSPaolo Bonzini #include "hw/pci-host/spapr.h" 440d09e41aSPaolo Bonzini #include "hw/ppc/xics.h" 4553018216SPaolo Bonzini #include "hw/pci/msi.h" 4653018216SPaolo Bonzini 4753018216SPaolo Bonzini #include "hw/pci/pci.h" 4853018216SPaolo Bonzini 4953018216SPaolo Bonzini #include "exec/address-spaces.h" 5053018216SPaolo Bonzini #include "hw/usb.h" 5153018216SPaolo Bonzini #include "qemu/config-file.h" 5253018216SPaolo Bonzini 5353018216SPaolo Bonzini #include <libfdt.h> 5453018216SPaolo Bonzini 5553018216SPaolo Bonzini /* SLOF memory layout: 5653018216SPaolo Bonzini * 5753018216SPaolo Bonzini * SLOF raw image loaded at 0, copies its romfs right below the flat 5853018216SPaolo Bonzini * device-tree, then position SLOF itself 31M below that 5953018216SPaolo Bonzini * 6053018216SPaolo Bonzini * So we set FW_OVERHEAD to 40MB which should account for all of that 6153018216SPaolo Bonzini * and more 6253018216SPaolo Bonzini * 6353018216SPaolo Bonzini * We load our kernel at 4M, leaving space for SLOF initial image 6453018216SPaolo Bonzini */ 6553018216SPaolo Bonzini #define FDT_MAX_SIZE 0x10000 6653018216SPaolo Bonzini #define RTAS_MAX_SIZE 0x10000 6753018216SPaolo Bonzini #define FW_MAX_SIZE 0x400000 6853018216SPaolo Bonzini #define FW_FILE_NAME "slof.bin" 6953018216SPaolo Bonzini #define FW_OVERHEAD 0x2800000 7053018216SPaolo Bonzini #define KERNEL_LOAD_ADDR FW_MAX_SIZE 7153018216SPaolo Bonzini 7253018216SPaolo Bonzini #define MIN_RMA_SLOF 128UL 7353018216SPaolo Bonzini 7453018216SPaolo Bonzini #define TIMEBASE_FREQ 512000000ULL 7553018216SPaolo Bonzini 7653018216SPaolo Bonzini #define MAX_CPUS 256 7753018216SPaolo Bonzini #define XICS_IRQS 1024 7853018216SPaolo Bonzini 7953018216SPaolo Bonzini #define PHANDLE_XICP 0x00001111 8053018216SPaolo Bonzini 8153018216SPaolo Bonzini #define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift)) 8253018216SPaolo Bonzini 8353018216SPaolo Bonzini sPAPREnvironment *spapr; 8453018216SPaolo Bonzini 8553018216SPaolo Bonzini int spapr_allocate_irq(int hint, bool lsi) 8653018216SPaolo Bonzini { 8753018216SPaolo Bonzini int irq; 8853018216SPaolo Bonzini 8953018216SPaolo Bonzini if (hint) { 9053018216SPaolo Bonzini irq = hint; 9153018216SPaolo Bonzini /* FIXME: we should probably check for collisions somehow */ 9253018216SPaolo Bonzini } else { 9353018216SPaolo Bonzini irq = spapr->next_irq++; 9453018216SPaolo Bonzini } 9553018216SPaolo Bonzini 9653018216SPaolo Bonzini /* Configure irq type */ 9753018216SPaolo Bonzini if (!xics_get_qirq(spapr->icp, irq)) { 9853018216SPaolo Bonzini return 0; 9953018216SPaolo Bonzini } 10053018216SPaolo Bonzini 10153018216SPaolo Bonzini xics_set_irq_type(spapr->icp, irq, lsi); 10253018216SPaolo Bonzini 10353018216SPaolo Bonzini return irq; 10453018216SPaolo Bonzini } 10553018216SPaolo Bonzini 10653018216SPaolo Bonzini /* Allocate block of consequtive IRQs, returns a number of the first */ 10753018216SPaolo Bonzini int spapr_allocate_irq_block(int num, bool lsi) 10853018216SPaolo Bonzini { 10953018216SPaolo Bonzini int first = -1; 11053018216SPaolo Bonzini int i; 11153018216SPaolo Bonzini 11253018216SPaolo Bonzini for (i = 0; i < num; ++i) { 11353018216SPaolo Bonzini int irq; 11453018216SPaolo Bonzini 11553018216SPaolo Bonzini irq = spapr_allocate_irq(0, lsi); 11653018216SPaolo Bonzini if (!irq) { 11753018216SPaolo Bonzini return -1; 11853018216SPaolo Bonzini } 11953018216SPaolo Bonzini 12053018216SPaolo Bonzini if (0 == i) { 12153018216SPaolo Bonzini first = irq; 12253018216SPaolo Bonzini } 12353018216SPaolo Bonzini 12453018216SPaolo Bonzini /* If the above doesn't create a consecutive block then that's 12553018216SPaolo Bonzini * an internal bug */ 12653018216SPaolo Bonzini assert(irq == (first + i)); 12753018216SPaolo Bonzini } 12853018216SPaolo Bonzini 12953018216SPaolo Bonzini return first; 13053018216SPaolo Bonzini } 13153018216SPaolo Bonzini 132c04d6cfaSAnthony Liguori static XICSState *try_create_xics(const char *type, int nr_servers, 133c04d6cfaSAnthony Liguori int nr_irqs) 134c04d6cfaSAnthony Liguori { 135c04d6cfaSAnthony Liguori DeviceState *dev; 136c04d6cfaSAnthony Liguori 137c04d6cfaSAnthony Liguori dev = qdev_create(NULL, type); 138c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_servers", nr_servers); 139c04d6cfaSAnthony Liguori qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs); 140c04d6cfaSAnthony Liguori if (qdev_init(dev) < 0) { 141c04d6cfaSAnthony Liguori return NULL; 142c04d6cfaSAnthony Liguori } 143c04d6cfaSAnthony Liguori 144c04d6cfaSAnthony Liguori return XICS(dev); 145c04d6cfaSAnthony Liguori } 146c04d6cfaSAnthony Liguori 147c04d6cfaSAnthony Liguori static XICSState *xics_system_init(int nr_servers, int nr_irqs) 148c04d6cfaSAnthony Liguori { 149c04d6cfaSAnthony Liguori XICSState *icp = NULL; 150c04d6cfaSAnthony Liguori 151c04d6cfaSAnthony Liguori icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs); 152c04d6cfaSAnthony Liguori if (!icp) { 153c04d6cfaSAnthony Liguori perror("Failed to create XICS\n"); 154c04d6cfaSAnthony Liguori abort(); 155c04d6cfaSAnthony Liguori } 156c04d6cfaSAnthony Liguori 157c04d6cfaSAnthony Liguori return icp; 158c04d6cfaSAnthony Liguori } 159c04d6cfaSAnthony Liguori 16053018216SPaolo Bonzini static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr) 16153018216SPaolo Bonzini { 16253018216SPaolo Bonzini int ret = 0, offset; 16353018216SPaolo Bonzini CPUState *cpu; 16453018216SPaolo Bonzini char cpu_model[32]; 16553018216SPaolo Bonzini int smt = kvmppc_smt_threads(); 16653018216SPaolo Bonzini uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)}; 16753018216SPaolo Bonzini 16853018216SPaolo Bonzini assert(spapr->cpu_model); 16953018216SPaolo Bonzini 170182735efSAndreas Färber for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { 17153018216SPaolo Bonzini uint32_t associativity[] = {cpu_to_be32(0x5), 17253018216SPaolo Bonzini cpu_to_be32(0x0), 17353018216SPaolo Bonzini cpu_to_be32(0x0), 17453018216SPaolo Bonzini cpu_to_be32(0x0), 17553018216SPaolo Bonzini cpu_to_be32(cpu->numa_node), 17653018216SPaolo Bonzini cpu_to_be32(cpu->cpu_index)}; 17753018216SPaolo Bonzini 17853018216SPaolo Bonzini if ((cpu->cpu_index % smt) != 0) { 17953018216SPaolo Bonzini continue; 18053018216SPaolo Bonzini } 18153018216SPaolo Bonzini 18253018216SPaolo Bonzini snprintf(cpu_model, 32, "/cpus/%s@%x", spapr->cpu_model, 18353018216SPaolo Bonzini cpu->cpu_index); 18453018216SPaolo Bonzini 18553018216SPaolo Bonzini offset = fdt_path_offset(fdt, cpu_model); 18653018216SPaolo Bonzini if (offset < 0) { 18753018216SPaolo Bonzini return offset; 18853018216SPaolo Bonzini } 18953018216SPaolo Bonzini 19053018216SPaolo Bonzini if (nb_numa_nodes > 1) { 19153018216SPaolo Bonzini ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity, 19253018216SPaolo Bonzini sizeof(associativity)); 19353018216SPaolo Bonzini if (ret < 0) { 19453018216SPaolo Bonzini return ret; 19553018216SPaolo Bonzini } 19653018216SPaolo Bonzini } 19753018216SPaolo Bonzini 19853018216SPaolo Bonzini ret = fdt_setprop(fdt, offset, "ibm,pft-size", 19953018216SPaolo Bonzini pft_size_prop, sizeof(pft_size_prop)); 20053018216SPaolo Bonzini if (ret < 0) { 20153018216SPaolo Bonzini return ret; 20253018216SPaolo Bonzini } 20353018216SPaolo Bonzini } 20453018216SPaolo Bonzini return ret; 20553018216SPaolo Bonzini } 20653018216SPaolo Bonzini 20753018216SPaolo Bonzini 20853018216SPaolo Bonzini static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop, 20953018216SPaolo Bonzini size_t maxsize) 21053018216SPaolo Bonzini { 21153018216SPaolo Bonzini size_t maxcells = maxsize / sizeof(uint32_t); 21253018216SPaolo Bonzini int i, j, count; 21353018216SPaolo Bonzini uint32_t *p = prop; 21453018216SPaolo Bonzini 21553018216SPaolo Bonzini for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 21653018216SPaolo Bonzini struct ppc_one_seg_page_size *sps = &env->sps.sps[i]; 21753018216SPaolo Bonzini 21853018216SPaolo Bonzini if (!sps->page_shift) { 21953018216SPaolo Bonzini break; 22053018216SPaolo Bonzini } 22153018216SPaolo Bonzini for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) { 22253018216SPaolo Bonzini if (sps->enc[count].page_shift == 0) { 22353018216SPaolo Bonzini break; 22453018216SPaolo Bonzini } 22553018216SPaolo Bonzini } 22653018216SPaolo Bonzini if ((p - prop) >= (maxcells - 3 - count * 2)) { 22753018216SPaolo Bonzini break; 22853018216SPaolo Bonzini } 22953018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->page_shift); 23053018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->slb_enc); 23153018216SPaolo Bonzini *(p++) = cpu_to_be32(count); 23253018216SPaolo Bonzini for (j = 0; j < count; j++) { 23353018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->enc[j].page_shift); 23453018216SPaolo Bonzini *(p++) = cpu_to_be32(sps->enc[j].pte_enc); 23553018216SPaolo Bonzini } 23653018216SPaolo Bonzini } 23753018216SPaolo Bonzini 23853018216SPaolo Bonzini return (p - prop) * sizeof(uint32_t); 23953018216SPaolo Bonzini } 24053018216SPaolo Bonzini 24153018216SPaolo Bonzini #define _FDT(exp) \ 24253018216SPaolo Bonzini do { \ 24353018216SPaolo Bonzini int ret = (exp); \ 24453018216SPaolo Bonzini if (ret < 0) { \ 24553018216SPaolo Bonzini fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \ 24653018216SPaolo Bonzini #exp, fdt_strerror(ret)); \ 24753018216SPaolo Bonzini exit(1); \ 24853018216SPaolo Bonzini } \ 24953018216SPaolo Bonzini } while (0) 25053018216SPaolo Bonzini 25153018216SPaolo Bonzini 25253018216SPaolo Bonzini static void *spapr_create_fdt_skel(const char *cpu_model, 25353018216SPaolo Bonzini hwaddr initrd_base, 25453018216SPaolo Bonzini hwaddr initrd_size, 25553018216SPaolo Bonzini hwaddr kernel_size, 25653018216SPaolo Bonzini const char *boot_device, 25753018216SPaolo Bonzini const char *kernel_cmdline, 25853018216SPaolo Bonzini uint32_t epow_irq) 25953018216SPaolo Bonzini { 26053018216SPaolo Bonzini void *fdt; 261182735efSAndreas Färber CPUState *cs; 26253018216SPaolo Bonzini uint32_t start_prop = cpu_to_be32(initrd_base); 26353018216SPaolo Bonzini uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size); 26453018216SPaolo Bonzini char hypertas_prop[] = "hcall-pft\0hcall-term\0hcall-dabr\0hcall-interrupt" 26553018216SPaolo Bonzini "\0hcall-tce\0hcall-vio\0hcall-splpar\0hcall-bulk"; 26653018216SPaolo Bonzini char qemu_hypertas_prop[] = "hcall-memop1"; 26753018216SPaolo Bonzini uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)}; 26853018216SPaolo Bonzini uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)}; 26953018216SPaolo Bonzini char *modelname; 27053018216SPaolo Bonzini int i, smt = kvmppc_smt_threads(); 27153018216SPaolo Bonzini unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80}; 27253018216SPaolo Bonzini 27353018216SPaolo Bonzini fdt = g_malloc0(FDT_MAX_SIZE); 27453018216SPaolo Bonzini _FDT((fdt_create(fdt, FDT_MAX_SIZE))); 27553018216SPaolo Bonzini 27653018216SPaolo Bonzini if (kernel_size) { 27753018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size))); 27853018216SPaolo Bonzini } 27953018216SPaolo Bonzini if (initrd_size) { 28053018216SPaolo Bonzini _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size))); 28153018216SPaolo Bonzini } 28253018216SPaolo Bonzini _FDT((fdt_finish_reservemap(fdt))); 28353018216SPaolo Bonzini 28453018216SPaolo Bonzini /* Root node */ 28553018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, ""))); 28653018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "chrp"))); 28753018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)"))); 288fa388916SAnthony Liguori _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries"))); 28953018216SPaolo Bonzini 29053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x2))); 29153018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x2))); 29253018216SPaolo Bonzini 29353018216SPaolo Bonzini /* /chosen */ 29453018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "chosen"))); 29553018216SPaolo Bonzini 29653018216SPaolo Bonzini /* Set Form1_affinity */ 29753018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5)))); 29853018216SPaolo Bonzini 29953018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline))); 30053018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-start", 30153018216SPaolo Bonzini &start_prop, sizeof(start_prop)))); 30253018216SPaolo Bonzini _FDT((fdt_property(fdt, "linux,initrd-end", 30353018216SPaolo Bonzini &end_prop, sizeof(end_prop)))); 30453018216SPaolo Bonzini if (kernel_size) { 30553018216SPaolo Bonzini uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR), 30653018216SPaolo Bonzini cpu_to_be64(kernel_size) }; 30753018216SPaolo Bonzini 30853018216SPaolo Bonzini _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop)))); 30953018216SPaolo Bonzini } 31053018216SPaolo Bonzini if (boot_device) { 31153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device))); 31253018216SPaolo Bonzini } 31353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width))); 31453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height))); 31553018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth))); 31653018216SPaolo Bonzini 31753018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 31853018216SPaolo Bonzini 31953018216SPaolo Bonzini /* cpus */ 32053018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "cpus"))); 32153018216SPaolo Bonzini 32253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 32353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 32453018216SPaolo Bonzini 32553018216SPaolo Bonzini modelname = g_strdup(cpu_model); 32653018216SPaolo Bonzini 32753018216SPaolo Bonzini for (i = 0; i < strlen(modelname); i++) { 32853018216SPaolo Bonzini modelname[i] = toupper(modelname[i]); 32953018216SPaolo Bonzini } 33053018216SPaolo Bonzini 33153018216SPaolo Bonzini /* This is needed during FDT finalization */ 33253018216SPaolo Bonzini spapr->cpu_model = g_strdup(modelname); 33353018216SPaolo Bonzini 334182735efSAndreas Färber for (cs = first_cpu; cs != NULL; cs = cs->next_cpu) { 335182735efSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 336182735efSAndreas Färber CPUPPCState *env = &cpu->env; 337182735efSAndreas Färber PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs); 338182735efSAndreas Färber int index = cs->cpu_index; 33953018216SPaolo Bonzini uint32_t servers_prop[smp_threads]; 34053018216SPaolo Bonzini uint32_t gservers_prop[smp_threads * 2]; 34153018216SPaolo Bonzini char *nodename; 34253018216SPaolo Bonzini uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40), 34353018216SPaolo Bonzini 0xffffffff, 0xffffffff}; 34453018216SPaolo Bonzini uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ; 34553018216SPaolo Bonzini uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000; 34653018216SPaolo Bonzini uint32_t page_sizes_prop[64]; 34753018216SPaolo Bonzini size_t page_sizes_prop_size; 34853018216SPaolo Bonzini 34953018216SPaolo Bonzini if ((index % smt) != 0) { 35053018216SPaolo Bonzini continue; 35153018216SPaolo Bonzini } 35253018216SPaolo Bonzini 35353018216SPaolo Bonzini nodename = g_strdup_printf("%s@%x", modelname, index); 35453018216SPaolo Bonzini 35553018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, nodename))); 35653018216SPaolo Bonzini 35753018216SPaolo Bonzini g_free(nodename); 35853018216SPaolo Bonzini 35953018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "reg", index))); 36053018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "cpu"))); 36153018216SPaolo Bonzini 36253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR]))); 3630cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-block-size", 36453018216SPaolo Bonzini env->dcache_line_size))); 3650cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-line-size", 3660cbad81fSDavid Gibson env->dcache_line_size))); 3670cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-block-size", 36853018216SPaolo Bonzini env->icache_line_size))); 3690cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-line-size", 3700cbad81fSDavid Gibson env->icache_line_size))); 3710cbad81fSDavid Gibson 3720cbad81fSDavid Gibson if (pcc->l1_dcache_size) { 3730cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size))); 3740cbad81fSDavid Gibson } else { 3750cbad81fSDavid Gibson fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n"); 3760cbad81fSDavid Gibson } 3770cbad81fSDavid Gibson if (pcc->l1_icache_size) { 3780cbad81fSDavid Gibson _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size))); 3790cbad81fSDavid Gibson } else { 3800cbad81fSDavid Gibson fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n"); 3810cbad81fSDavid Gibson } 3820cbad81fSDavid Gibson 38353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq))); 38453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq))); 38553018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr))); 38653018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "status", "okay"))); 38753018216SPaolo Bonzini _FDT((fdt_property(fdt, "64-bit", NULL, 0))); 38853018216SPaolo Bonzini 38953018216SPaolo Bonzini /* Build interrupt servers and gservers properties */ 39053018216SPaolo Bonzini for (i = 0; i < smp_threads; i++) { 39153018216SPaolo Bonzini servers_prop[i] = cpu_to_be32(index + i); 39253018216SPaolo Bonzini /* Hack, direct the group queues back to cpu 0 */ 39353018216SPaolo Bonzini gservers_prop[i*2] = cpu_to_be32(index + i); 39453018216SPaolo Bonzini gservers_prop[i*2 + 1] = 0; 39553018216SPaolo Bonzini } 39653018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s", 39753018216SPaolo Bonzini servers_prop, sizeof(servers_prop)))); 39853018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s", 39953018216SPaolo Bonzini gservers_prop, sizeof(gservers_prop)))); 40053018216SPaolo Bonzini 40153018216SPaolo Bonzini if (env->mmu_model & POWERPC_MMU_1TSEG) { 40253018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,processor-segment-sizes", 40353018216SPaolo Bonzini segs, sizeof(segs)))); 40453018216SPaolo Bonzini } 40553018216SPaolo Bonzini 40653018216SPaolo Bonzini /* Advertise VMX/VSX (vector extensions) if available 40753018216SPaolo Bonzini * 0 / no property == no vector extensions 40853018216SPaolo Bonzini * 1 == VMX / Altivec available 40953018216SPaolo Bonzini * 2 == VSX available */ 41053018216SPaolo Bonzini if (env->insns_flags & PPC_ALTIVEC) { 41153018216SPaolo Bonzini uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1; 41253018216SPaolo Bonzini 41353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx))); 41453018216SPaolo Bonzini } 41553018216SPaolo Bonzini 41653018216SPaolo Bonzini /* Advertise DFP (Decimal Floating Point) if available 41753018216SPaolo Bonzini * 0 / no property == no DFP 41853018216SPaolo Bonzini * 1 == DFP available */ 41953018216SPaolo Bonzini if (env->insns_flags2 & PPC2_DFP) { 42053018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "ibm,dfp", 1))); 42153018216SPaolo Bonzini } 42253018216SPaolo Bonzini 42353018216SPaolo Bonzini page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop, 42453018216SPaolo Bonzini sizeof(page_sizes_prop)); 42553018216SPaolo Bonzini if (page_sizes_prop_size) { 42653018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,segment-page-sizes", 42753018216SPaolo Bonzini page_sizes_prop, page_sizes_prop_size))); 42853018216SPaolo Bonzini } 42953018216SPaolo Bonzini 43053018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 43153018216SPaolo Bonzini } 43253018216SPaolo Bonzini 43353018216SPaolo Bonzini g_free(modelname); 43453018216SPaolo Bonzini 43553018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 43653018216SPaolo Bonzini 43753018216SPaolo Bonzini /* RTAS */ 43853018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "rtas"))); 43953018216SPaolo Bonzini 44053018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop, 44153018216SPaolo Bonzini sizeof(hypertas_prop)))); 44253018216SPaolo Bonzini _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas_prop, 44353018216SPaolo Bonzini sizeof(qemu_hypertas_prop)))); 44453018216SPaolo Bonzini 44553018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,associativity-reference-points", 44653018216SPaolo Bonzini refpoints, sizeof(refpoints)))); 44753018216SPaolo Bonzini 44853018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX))); 44953018216SPaolo Bonzini 45053018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 45153018216SPaolo Bonzini 45253018216SPaolo Bonzini /* interrupt controller */ 45353018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "interrupt-controller"))); 45453018216SPaolo Bonzini 45553018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", 45653018216SPaolo Bonzini "PowerPC-External-Interrupt-Presentation"))); 45753018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp"))); 45853018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 45953018216SPaolo Bonzini _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges", 46053018216SPaolo Bonzini interrupt_server_ranges_prop, 46153018216SPaolo Bonzini sizeof(interrupt_server_ranges_prop)))); 46253018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2))); 46353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP))); 46453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP))); 46553018216SPaolo Bonzini 46653018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 46753018216SPaolo Bonzini 46853018216SPaolo Bonzini /* vdevice */ 46953018216SPaolo Bonzini _FDT((fdt_begin_node(fdt, "vdevice"))); 47053018216SPaolo Bonzini 47153018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "device_type", "vdevice"))); 47253018216SPaolo Bonzini _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice"))); 47353018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#address-cells", 0x1))); 47453018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#size-cells", 0x0))); 47553018216SPaolo Bonzini _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2))); 47653018216SPaolo Bonzini _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0))); 47753018216SPaolo Bonzini 47853018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); 47953018216SPaolo Bonzini 48053018216SPaolo Bonzini /* event-sources */ 48153018216SPaolo Bonzini spapr_events_fdt_skel(fdt, epow_irq); 48253018216SPaolo Bonzini 48353018216SPaolo Bonzini _FDT((fdt_end_node(fdt))); /* close root node */ 48453018216SPaolo Bonzini _FDT((fdt_finish(fdt))); 48553018216SPaolo Bonzini 48653018216SPaolo Bonzini return fdt; 48753018216SPaolo Bonzini } 48853018216SPaolo Bonzini 48953018216SPaolo Bonzini static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt) 49053018216SPaolo Bonzini { 49153018216SPaolo Bonzini uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0), 49253018216SPaolo Bonzini cpu_to_be32(0x0), cpu_to_be32(0x0), 49353018216SPaolo Bonzini cpu_to_be32(0x0)}; 49453018216SPaolo Bonzini char mem_name[32]; 49553018216SPaolo Bonzini hwaddr node0_size, mem_start; 49653018216SPaolo Bonzini uint64_t mem_reg_property[2]; 49753018216SPaolo Bonzini int i, off; 49853018216SPaolo Bonzini 49953018216SPaolo Bonzini /* memory node(s) */ 50053018216SPaolo Bonzini node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size; 50153018216SPaolo Bonzini if (spapr->rma_size > node0_size) { 50253018216SPaolo Bonzini spapr->rma_size = node0_size; 50353018216SPaolo Bonzini } 50453018216SPaolo Bonzini 50553018216SPaolo Bonzini /* RMA */ 50653018216SPaolo Bonzini mem_reg_property[0] = 0; 50753018216SPaolo Bonzini mem_reg_property[1] = cpu_to_be64(spapr->rma_size); 50853018216SPaolo Bonzini off = fdt_add_subnode(fdt, 0, "memory@0"); 50953018216SPaolo Bonzini _FDT(off); 51053018216SPaolo Bonzini _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 51153018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 51253018216SPaolo Bonzini sizeof(mem_reg_property)))); 51353018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 51453018216SPaolo Bonzini sizeof(associativity)))); 51553018216SPaolo Bonzini 51653018216SPaolo Bonzini /* RAM: Node 0 */ 51753018216SPaolo Bonzini if (node0_size > spapr->rma_size) { 51853018216SPaolo Bonzini mem_reg_property[0] = cpu_to_be64(spapr->rma_size); 51953018216SPaolo Bonzini mem_reg_property[1] = cpu_to_be64(node0_size - spapr->rma_size); 52053018216SPaolo Bonzini 52153018216SPaolo Bonzini sprintf(mem_name, "memory@" TARGET_FMT_lx, spapr->rma_size); 52253018216SPaolo Bonzini off = fdt_add_subnode(fdt, 0, mem_name); 52353018216SPaolo Bonzini _FDT(off); 52453018216SPaolo Bonzini _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 52553018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 52653018216SPaolo Bonzini sizeof(mem_reg_property)))); 52753018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 52853018216SPaolo Bonzini sizeof(associativity)))); 52953018216SPaolo Bonzini } 53053018216SPaolo Bonzini 53153018216SPaolo Bonzini /* RAM: Node 1 and beyond */ 53253018216SPaolo Bonzini mem_start = node0_size; 53353018216SPaolo Bonzini for (i = 1; i < nb_numa_nodes; i++) { 53453018216SPaolo Bonzini mem_reg_property[0] = cpu_to_be64(mem_start); 53553018216SPaolo Bonzini mem_reg_property[1] = cpu_to_be64(node_mem[i]); 53653018216SPaolo Bonzini associativity[3] = associativity[4] = cpu_to_be32(i); 53753018216SPaolo Bonzini sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start); 53853018216SPaolo Bonzini off = fdt_add_subnode(fdt, 0, mem_name); 53953018216SPaolo Bonzini _FDT(off); 54053018216SPaolo Bonzini _FDT((fdt_setprop_string(fdt, off, "device_type", "memory"))); 54153018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property, 54253018216SPaolo Bonzini sizeof(mem_reg_property)))); 54353018216SPaolo Bonzini _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity, 54453018216SPaolo Bonzini sizeof(associativity)))); 54553018216SPaolo Bonzini mem_start += node_mem[i]; 54653018216SPaolo Bonzini } 54753018216SPaolo Bonzini 54853018216SPaolo Bonzini return 0; 54953018216SPaolo Bonzini } 55053018216SPaolo Bonzini 55153018216SPaolo Bonzini static void spapr_finalize_fdt(sPAPREnvironment *spapr, 55253018216SPaolo Bonzini hwaddr fdt_addr, 55353018216SPaolo Bonzini hwaddr rtas_addr, 55453018216SPaolo Bonzini hwaddr rtas_size) 55553018216SPaolo Bonzini { 55653018216SPaolo Bonzini int ret; 55753018216SPaolo Bonzini void *fdt; 55853018216SPaolo Bonzini sPAPRPHBState *phb; 55953018216SPaolo Bonzini 56053018216SPaolo Bonzini fdt = g_malloc(FDT_MAX_SIZE); 56153018216SPaolo Bonzini 56253018216SPaolo Bonzini /* open out the base tree into a temp buffer for the final tweaks */ 56353018216SPaolo Bonzini _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE))); 56453018216SPaolo Bonzini 56553018216SPaolo Bonzini ret = spapr_populate_memory(spapr, fdt); 56653018216SPaolo Bonzini if (ret < 0) { 56753018216SPaolo Bonzini fprintf(stderr, "couldn't setup memory nodes in fdt\n"); 56853018216SPaolo Bonzini exit(1); 56953018216SPaolo Bonzini } 57053018216SPaolo Bonzini 57153018216SPaolo Bonzini ret = spapr_populate_vdevice(spapr->vio_bus, fdt); 57253018216SPaolo Bonzini if (ret < 0) { 57353018216SPaolo Bonzini fprintf(stderr, "couldn't setup vio devices in fdt\n"); 57453018216SPaolo Bonzini exit(1); 57553018216SPaolo Bonzini } 57653018216SPaolo Bonzini 57753018216SPaolo Bonzini QLIST_FOREACH(phb, &spapr->phbs, list) { 57853018216SPaolo Bonzini ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt); 57953018216SPaolo Bonzini } 58053018216SPaolo Bonzini 58153018216SPaolo Bonzini if (ret < 0) { 58253018216SPaolo Bonzini fprintf(stderr, "couldn't setup PCI devices in fdt\n"); 58353018216SPaolo Bonzini exit(1); 58453018216SPaolo Bonzini } 58553018216SPaolo Bonzini 58653018216SPaolo Bonzini /* RTAS */ 58753018216SPaolo Bonzini ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size); 58853018216SPaolo Bonzini if (ret < 0) { 58953018216SPaolo Bonzini fprintf(stderr, "Couldn't set up RTAS device tree properties\n"); 59053018216SPaolo Bonzini } 59153018216SPaolo Bonzini 59253018216SPaolo Bonzini /* Advertise NUMA via ibm,associativity */ 59353018216SPaolo Bonzini ret = spapr_fixup_cpu_dt(fdt, spapr); 59453018216SPaolo Bonzini if (ret < 0) { 59553018216SPaolo Bonzini fprintf(stderr, "Couldn't finalize CPU device tree properties\n"); 59653018216SPaolo Bonzini } 59753018216SPaolo Bonzini 59853018216SPaolo Bonzini if (!spapr->has_graphics) { 59953018216SPaolo Bonzini spapr_populate_chosen_stdout(fdt, spapr->vio_bus); 60053018216SPaolo Bonzini } 60153018216SPaolo Bonzini 60253018216SPaolo Bonzini _FDT((fdt_pack(fdt))); 60353018216SPaolo Bonzini 60453018216SPaolo Bonzini if (fdt_totalsize(fdt) > FDT_MAX_SIZE) { 60553018216SPaolo Bonzini hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n", 60653018216SPaolo Bonzini fdt_totalsize(fdt), FDT_MAX_SIZE); 60753018216SPaolo Bonzini exit(1); 60853018216SPaolo Bonzini } 60953018216SPaolo Bonzini 61053018216SPaolo Bonzini cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt)); 61153018216SPaolo Bonzini 61253018216SPaolo Bonzini g_free(fdt); 61353018216SPaolo Bonzini } 61453018216SPaolo Bonzini 61553018216SPaolo Bonzini static uint64_t translate_kernel_address(void *opaque, uint64_t addr) 61653018216SPaolo Bonzini { 61753018216SPaolo Bonzini return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR; 61853018216SPaolo Bonzini } 61953018216SPaolo Bonzini 62053018216SPaolo Bonzini static void emulate_spapr_hypercall(PowerPCCPU *cpu) 62153018216SPaolo Bonzini { 62253018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 62353018216SPaolo Bonzini 62453018216SPaolo Bonzini if (msr_pr) { 62553018216SPaolo Bonzini hcall_dprintf("Hypercall made with MSR[PR]=1\n"); 62653018216SPaolo Bonzini env->gpr[3] = H_PRIVILEGE; 62753018216SPaolo Bonzini } else { 62853018216SPaolo Bonzini env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]); 62953018216SPaolo Bonzini } 63053018216SPaolo Bonzini } 63153018216SPaolo Bonzini 63253018216SPaolo Bonzini static void spapr_reset_htab(sPAPREnvironment *spapr) 63353018216SPaolo Bonzini { 63453018216SPaolo Bonzini long shift; 63553018216SPaolo Bonzini 63653018216SPaolo Bonzini /* allocate hash page table. For now we always make this 16mb, 63753018216SPaolo Bonzini * later we should probably make it scale to the size of guest 63853018216SPaolo Bonzini * RAM */ 63953018216SPaolo Bonzini 64053018216SPaolo Bonzini shift = kvmppc_reset_htab(spapr->htab_shift); 64153018216SPaolo Bonzini 64253018216SPaolo Bonzini if (shift > 0) { 64353018216SPaolo Bonzini /* Kernel handles htab, we don't need to allocate one */ 64453018216SPaolo Bonzini spapr->htab_shift = shift; 64553018216SPaolo Bonzini } else { 64653018216SPaolo Bonzini if (!spapr->htab) { 64753018216SPaolo Bonzini /* Allocate an htab if we don't yet have one */ 64853018216SPaolo Bonzini spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr)); 64953018216SPaolo Bonzini } 65053018216SPaolo Bonzini 65153018216SPaolo Bonzini /* And clear it */ 65253018216SPaolo Bonzini memset(spapr->htab, 0, HTAB_SIZE(spapr)); 65353018216SPaolo Bonzini } 65453018216SPaolo Bonzini 65553018216SPaolo Bonzini /* Update the RMA size if necessary */ 65653018216SPaolo Bonzini if (spapr->vrma_adjust) { 65753018216SPaolo Bonzini spapr->rma_size = kvmppc_rma_size(ram_size, spapr->htab_shift); 65853018216SPaolo Bonzini } 65953018216SPaolo Bonzini } 66053018216SPaolo Bonzini 66153018216SPaolo Bonzini static void ppc_spapr_reset(void) 66253018216SPaolo Bonzini { 663182735efSAndreas Färber PowerPCCPU *first_ppc_cpu; 664259186a7SAndreas Färber 66553018216SPaolo Bonzini /* Reset the hash table & recalc the RMA */ 66653018216SPaolo Bonzini spapr_reset_htab(spapr); 66753018216SPaolo Bonzini 66853018216SPaolo Bonzini qemu_devices_reset(); 66953018216SPaolo Bonzini 67053018216SPaolo Bonzini /* Load the fdt */ 67153018216SPaolo Bonzini spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr, 67253018216SPaolo Bonzini spapr->rtas_size); 67353018216SPaolo Bonzini 67453018216SPaolo Bonzini /* Set up the entry state */ 675182735efSAndreas Färber first_ppc_cpu = POWERPC_CPU(first_cpu); 676182735efSAndreas Färber first_ppc_cpu->env.gpr[3] = spapr->fdt_addr; 677182735efSAndreas Färber first_ppc_cpu->env.gpr[5] = 0; 678182735efSAndreas Färber first_cpu->halted = 0; 679182735efSAndreas Färber first_ppc_cpu->env.nip = spapr->entry_point; 68053018216SPaolo Bonzini 68153018216SPaolo Bonzini } 68253018216SPaolo Bonzini 68353018216SPaolo Bonzini static void spapr_cpu_reset(void *opaque) 68453018216SPaolo Bonzini { 68553018216SPaolo Bonzini PowerPCCPU *cpu = opaque; 686259186a7SAndreas Färber CPUState *cs = CPU(cpu); 68753018216SPaolo Bonzini CPUPPCState *env = &cpu->env; 68853018216SPaolo Bonzini 689259186a7SAndreas Färber cpu_reset(cs); 69053018216SPaolo Bonzini 69153018216SPaolo Bonzini /* All CPUs start halted. CPU0 is unhalted from the machine level 69253018216SPaolo Bonzini * reset code and the rest are explicitly started up by the guest 69353018216SPaolo Bonzini * using an RTAS call */ 694259186a7SAndreas Färber cs->halted = 1; 69553018216SPaolo Bonzini 69653018216SPaolo Bonzini env->spr[SPR_HIOR] = 0; 69753018216SPaolo Bonzini 6984be21d56SDavid Gibson env->external_htab = (uint8_t *)spapr->htab; 69953018216SPaolo Bonzini env->htab_base = -1; 70053018216SPaolo Bonzini env->htab_mask = HTAB_SIZE(spapr) - 1; 701ec4936e1SStefan Weil env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab | 70253018216SPaolo Bonzini (spapr->htab_shift - 18); 70353018216SPaolo Bonzini } 70453018216SPaolo Bonzini 70553018216SPaolo Bonzini static void spapr_create_nvram(sPAPREnvironment *spapr) 70653018216SPaolo Bonzini { 7072ff3de68SMarkus Armbruster DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram"); 7082ff3de68SMarkus Armbruster const char *drivename = qemu_opt_get(qemu_get_machine_opts(), "nvram"); 70953018216SPaolo Bonzini 71053018216SPaolo Bonzini if (drivename) { 71153018216SPaolo Bonzini BlockDriverState *bs; 71253018216SPaolo Bonzini 71353018216SPaolo Bonzini bs = bdrv_find(drivename); 71453018216SPaolo Bonzini if (!bs) { 71553018216SPaolo Bonzini fprintf(stderr, "No such block device \"%s\" for nvram\n", 71653018216SPaolo Bonzini drivename); 71753018216SPaolo Bonzini exit(1); 71853018216SPaolo Bonzini } 71953018216SPaolo Bonzini qdev_prop_set_drive_nofail(dev, "drive", bs); 72053018216SPaolo Bonzini } 72153018216SPaolo Bonzini 72253018216SPaolo Bonzini qdev_init_nofail(dev); 72353018216SPaolo Bonzini 72453018216SPaolo Bonzini spapr->nvram = (struct sPAPRNVRAM *)dev; 72553018216SPaolo Bonzini } 72653018216SPaolo Bonzini 72753018216SPaolo Bonzini /* Returns whether we want to use VGA or not */ 72853018216SPaolo Bonzini static int spapr_vga_init(PCIBus *pci_bus) 72953018216SPaolo Bonzini { 73053018216SPaolo Bonzini switch (vga_interface_type) { 73153018216SPaolo Bonzini case VGA_NONE: 73253018216SPaolo Bonzini case VGA_STD: 73353018216SPaolo Bonzini return pci_vga_init(pci_bus) != NULL; 73453018216SPaolo Bonzini default: 73553018216SPaolo Bonzini fprintf(stderr, "This vga model is not supported," 73653018216SPaolo Bonzini "currently it only supports -vga std\n"); 73753018216SPaolo Bonzini exit(0); 73853018216SPaolo Bonzini break; 73953018216SPaolo Bonzini } 74053018216SPaolo Bonzini } 74153018216SPaolo Bonzini 7424be21d56SDavid Gibson static const VMStateDescription vmstate_spapr = { 7434be21d56SDavid Gibson .name = "spapr", 7444be21d56SDavid Gibson .version_id = 1, 7454be21d56SDavid Gibson .minimum_version_id = 1, 7464be21d56SDavid Gibson .minimum_version_id_old = 1, 7474be21d56SDavid Gibson .fields = (VMStateField []) { 7484be21d56SDavid Gibson VMSTATE_UINT32(next_irq, sPAPREnvironment), 7494be21d56SDavid Gibson 7504be21d56SDavid Gibson /* RTC offset */ 7514be21d56SDavid Gibson VMSTATE_UINT64(rtc_offset, sPAPREnvironment), 7524be21d56SDavid Gibson 7534be21d56SDavid Gibson VMSTATE_END_OF_LIST() 7544be21d56SDavid Gibson }, 7554be21d56SDavid Gibson }; 7564be21d56SDavid Gibson 7574be21d56SDavid Gibson #define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2)) 7584be21d56SDavid Gibson #define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID) 7594be21d56SDavid Gibson #define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY) 7604be21d56SDavid Gibson #define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY)) 7614be21d56SDavid Gibson 7624be21d56SDavid Gibson static int htab_save_setup(QEMUFile *f, void *opaque) 7634be21d56SDavid Gibson { 7644be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 7654be21d56SDavid Gibson 7664be21d56SDavid Gibson /* "Iteration" header */ 7674be21d56SDavid Gibson qemu_put_be32(f, spapr->htab_shift); 7684be21d56SDavid Gibson 769e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 770e68cb8b4SAlexey Kardashevskiy spapr->htab_save_index = 0; 771e68cb8b4SAlexey Kardashevskiy spapr->htab_first_pass = true; 772e68cb8b4SAlexey Kardashevskiy } else { 773e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 774e68cb8b4SAlexey Kardashevskiy 775e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = kvmppc_get_htab_fd(false); 776e68cb8b4SAlexey Kardashevskiy if (spapr->htab_fd < 0) { 777e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n", 778e68cb8b4SAlexey Kardashevskiy strerror(errno)); 779e68cb8b4SAlexey Kardashevskiy return -1; 780e68cb8b4SAlexey Kardashevskiy } 7814be21d56SDavid Gibson } 7824be21d56SDavid Gibson 783e68cb8b4SAlexey Kardashevskiy 784e68cb8b4SAlexey Kardashevskiy return 0; 785e68cb8b4SAlexey Kardashevskiy } 7864be21d56SDavid Gibson 7874be21d56SDavid Gibson static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr, 7884be21d56SDavid Gibson int64_t max_ns) 7894be21d56SDavid Gibson { 7904be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 7914be21d56SDavid Gibson int index = spapr->htab_save_index; 7924be21d56SDavid Gibson int64_t starttime = qemu_get_clock_ns(rt_clock); 7934be21d56SDavid Gibson 7944be21d56SDavid Gibson assert(spapr->htab_first_pass); 7954be21d56SDavid Gibson 7964be21d56SDavid Gibson do { 7974be21d56SDavid Gibson int chunkstart; 7984be21d56SDavid Gibson 7994be21d56SDavid Gibson /* Consume invalid HPTEs */ 8004be21d56SDavid Gibson while ((index < htabslots) 8014be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 8024be21d56SDavid Gibson index++; 8034be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 8044be21d56SDavid Gibson } 8054be21d56SDavid Gibson 8064be21d56SDavid Gibson /* Consume valid HPTEs */ 8074be21d56SDavid Gibson chunkstart = index; 8084be21d56SDavid Gibson while ((index < htabslots) 8094be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 8104be21d56SDavid Gibson index++; 8114be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 8124be21d56SDavid Gibson } 8134be21d56SDavid Gibson 8144be21d56SDavid Gibson if (index > chunkstart) { 8154be21d56SDavid Gibson int n_valid = index - chunkstart; 8164be21d56SDavid Gibson 8174be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 8184be21d56SDavid Gibson qemu_put_be16(f, n_valid); 8194be21d56SDavid Gibson qemu_put_be16(f, 0); 8204be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 8214be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 8224be21d56SDavid Gibson 8234be21d56SDavid Gibson if ((qemu_get_clock_ns(rt_clock) - starttime) > max_ns) { 8244be21d56SDavid Gibson break; 8254be21d56SDavid Gibson } 8264be21d56SDavid Gibson } 8274be21d56SDavid Gibson } while ((index < htabslots) && !qemu_file_rate_limit(f)); 8284be21d56SDavid Gibson 8294be21d56SDavid Gibson if (index >= htabslots) { 8304be21d56SDavid Gibson assert(index == htabslots); 8314be21d56SDavid Gibson index = 0; 8324be21d56SDavid Gibson spapr->htab_first_pass = false; 8334be21d56SDavid Gibson } 8344be21d56SDavid Gibson spapr->htab_save_index = index; 8354be21d56SDavid Gibson } 8364be21d56SDavid Gibson 837e68cb8b4SAlexey Kardashevskiy static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr, 8384be21d56SDavid Gibson int64_t max_ns) 8394be21d56SDavid Gibson { 8404be21d56SDavid Gibson bool final = max_ns < 0; 8414be21d56SDavid Gibson int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64; 8424be21d56SDavid Gibson int examined = 0, sent = 0; 8434be21d56SDavid Gibson int index = spapr->htab_save_index; 8444be21d56SDavid Gibson int64_t starttime = qemu_get_clock_ns(rt_clock); 8454be21d56SDavid Gibson 8464be21d56SDavid Gibson assert(!spapr->htab_first_pass); 8474be21d56SDavid Gibson 8484be21d56SDavid Gibson do { 8494be21d56SDavid Gibson int chunkstart, invalidstart; 8504be21d56SDavid Gibson 8514be21d56SDavid Gibson /* Consume non-dirty HPTEs */ 8524be21d56SDavid Gibson while ((index < htabslots) 8534be21d56SDavid Gibson && !HPTE_DIRTY(HPTE(spapr->htab, index))) { 8544be21d56SDavid Gibson index++; 8554be21d56SDavid Gibson examined++; 8564be21d56SDavid Gibson } 8574be21d56SDavid Gibson 8584be21d56SDavid Gibson chunkstart = index; 8594be21d56SDavid Gibson /* Consume valid dirty HPTEs */ 8604be21d56SDavid Gibson while ((index < htabslots) 8614be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 8624be21d56SDavid Gibson && HPTE_VALID(HPTE(spapr->htab, index))) { 8634be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 8644be21d56SDavid Gibson index++; 8654be21d56SDavid Gibson examined++; 8664be21d56SDavid Gibson } 8674be21d56SDavid Gibson 8684be21d56SDavid Gibson invalidstart = index; 8694be21d56SDavid Gibson /* Consume invalid dirty HPTEs */ 8704be21d56SDavid Gibson while ((index < htabslots) 8714be21d56SDavid Gibson && HPTE_DIRTY(HPTE(spapr->htab, index)) 8724be21d56SDavid Gibson && !HPTE_VALID(HPTE(spapr->htab, index))) { 8734be21d56SDavid Gibson CLEAN_HPTE(HPTE(spapr->htab, index)); 8744be21d56SDavid Gibson index++; 8754be21d56SDavid Gibson examined++; 8764be21d56SDavid Gibson } 8774be21d56SDavid Gibson 8784be21d56SDavid Gibson if (index > chunkstart) { 8794be21d56SDavid Gibson int n_valid = invalidstart - chunkstart; 8804be21d56SDavid Gibson int n_invalid = index - invalidstart; 8814be21d56SDavid Gibson 8824be21d56SDavid Gibson qemu_put_be32(f, chunkstart); 8834be21d56SDavid Gibson qemu_put_be16(f, n_valid); 8844be21d56SDavid Gibson qemu_put_be16(f, n_invalid); 8854be21d56SDavid Gibson qemu_put_buffer(f, HPTE(spapr->htab, chunkstart), 8864be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 8874be21d56SDavid Gibson sent += index - chunkstart; 8884be21d56SDavid Gibson 8894be21d56SDavid Gibson if (!final && (qemu_get_clock_ns(rt_clock) - starttime) > max_ns) { 8904be21d56SDavid Gibson break; 8914be21d56SDavid Gibson } 8924be21d56SDavid Gibson } 8934be21d56SDavid Gibson 8944be21d56SDavid Gibson if (examined >= htabslots) { 8954be21d56SDavid Gibson break; 8964be21d56SDavid Gibson } 8974be21d56SDavid Gibson 8984be21d56SDavid Gibson if (index >= htabslots) { 8994be21d56SDavid Gibson assert(index == htabslots); 9004be21d56SDavid Gibson index = 0; 9014be21d56SDavid Gibson } 9024be21d56SDavid Gibson } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final)); 9034be21d56SDavid Gibson 9044be21d56SDavid Gibson if (index >= htabslots) { 9054be21d56SDavid Gibson assert(index == htabslots); 9064be21d56SDavid Gibson index = 0; 9074be21d56SDavid Gibson } 9084be21d56SDavid Gibson 9094be21d56SDavid Gibson spapr->htab_save_index = index; 9104be21d56SDavid Gibson 911e68cb8b4SAlexey Kardashevskiy return (examined >= htabslots) && (sent == 0) ? 1 : 0; 9124be21d56SDavid Gibson } 9134be21d56SDavid Gibson 914e68cb8b4SAlexey Kardashevskiy #define MAX_ITERATION_NS 5000000 /* 5 ms */ 915e68cb8b4SAlexey Kardashevskiy #define MAX_KVM_BUF_SIZE 2048 916e68cb8b4SAlexey Kardashevskiy 9174be21d56SDavid Gibson static int htab_save_iterate(QEMUFile *f, void *opaque) 9184be21d56SDavid Gibson { 9194be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 920e68cb8b4SAlexey Kardashevskiy int rc = 0; 9214be21d56SDavid Gibson 9224be21d56SDavid Gibson /* Iteration header */ 9234be21d56SDavid Gibson qemu_put_be32(f, 0); 9244be21d56SDavid Gibson 925e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 926e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 927e68cb8b4SAlexey Kardashevskiy 928e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, 929e68cb8b4SAlexey Kardashevskiy MAX_KVM_BUF_SIZE, MAX_ITERATION_NS); 930e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 931e68cb8b4SAlexey Kardashevskiy return rc; 932e68cb8b4SAlexey Kardashevskiy } 933e68cb8b4SAlexey Kardashevskiy } else if (spapr->htab_first_pass) { 9344be21d56SDavid Gibson htab_save_first_pass(f, spapr, MAX_ITERATION_NS); 9354be21d56SDavid Gibson } else { 936e68cb8b4SAlexey Kardashevskiy rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS); 9374be21d56SDavid Gibson } 9384be21d56SDavid Gibson 9394be21d56SDavid Gibson /* End marker */ 9404be21d56SDavid Gibson qemu_put_be32(f, 0); 9414be21d56SDavid Gibson qemu_put_be16(f, 0); 9424be21d56SDavid Gibson qemu_put_be16(f, 0); 9434be21d56SDavid Gibson 944e68cb8b4SAlexey Kardashevskiy return rc; 9454be21d56SDavid Gibson } 9464be21d56SDavid Gibson 9474be21d56SDavid Gibson static int htab_save_complete(QEMUFile *f, void *opaque) 9484be21d56SDavid Gibson { 9494be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 9504be21d56SDavid Gibson 9514be21d56SDavid Gibson /* Iteration header */ 9524be21d56SDavid Gibson qemu_put_be32(f, 0); 9534be21d56SDavid Gibson 954e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 955e68cb8b4SAlexey Kardashevskiy int rc; 956e68cb8b4SAlexey Kardashevskiy 957e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 958e68cb8b4SAlexey Kardashevskiy 959e68cb8b4SAlexey Kardashevskiy rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1); 960e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 961e68cb8b4SAlexey Kardashevskiy return rc; 962e68cb8b4SAlexey Kardashevskiy } 963e68cb8b4SAlexey Kardashevskiy close(spapr->htab_fd); 964e68cb8b4SAlexey Kardashevskiy spapr->htab_fd = -1; 965e68cb8b4SAlexey Kardashevskiy } else { 9664be21d56SDavid Gibson htab_save_later_pass(f, spapr, -1); 967e68cb8b4SAlexey Kardashevskiy } 9684be21d56SDavid Gibson 9694be21d56SDavid Gibson /* End marker */ 9704be21d56SDavid Gibson qemu_put_be32(f, 0); 9714be21d56SDavid Gibson qemu_put_be16(f, 0); 9724be21d56SDavid Gibson qemu_put_be16(f, 0); 9734be21d56SDavid Gibson 9744be21d56SDavid Gibson return 0; 9754be21d56SDavid Gibson } 9764be21d56SDavid Gibson 9774be21d56SDavid Gibson static int htab_load(QEMUFile *f, void *opaque, int version_id) 9784be21d56SDavid Gibson { 9794be21d56SDavid Gibson sPAPREnvironment *spapr = opaque; 9804be21d56SDavid Gibson uint32_t section_hdr; 981e68cb8b4SAlexey Kardashevskiy int fd = -1; 9824be21d56SDavid Gibson 9834be21d56SDavid Gibson if (version_id < 1 || version_id > 1) { 9844be21d56SDavid Gibson fprintf(stderr, "htab_load() bad version\n"); 9854be21d56SDavid Gibson return -EINVAL; 9864be21d56SDavid Gibson } 9874be21d56SDavid Gibson 9884be21d56SDavid Gibson section_hdr = qemu_get_be32(f); 9894be21d56SDavid Gibson 9904be21d56SDavid Gibson if (section_hdr) { 9914be21d56SDavid Gibson /* First section, just the hash shift */ 9924be21d56SDavid Gibson if (spapr->htab_shift != section_hdr) { 9934be21d56SDavid Gibson return -EINVAL; 9944be21d56SDavid Gibson } 9954be21d56SDavid Gibson return 0; 9964be21d56SDavid Gibson } 9974be21d56SDavid Gibson 998e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 999e68cb8b4SAlexey Kardashevskiy assert(kvm_enabled()); 1000e68cb8b4SAlexey Kardashevskiy 1001e68cb8b4SAlexey Kardashevskiy fd = kvmppc_get_htab_fd(true); 1002e68cb8b4SAlexey Kardashevskiy if (fd < 0) { 1003e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n", 1004e68cb8b4SAlexey Kardashevskiy strerror(errno)); 1005e68cb8b4SAlexey Kardashevskiy } 1006e68cb8b4SAlexey Kardashevskiy } 1007e68cb8b4SAlexey Kardashevskiy 10084be21d56SDavid Gibson while (true) { 10094be21d56SDavid Gibson uint32_t index; 10104be21d56SDavid Gibson uint16_t n_valid, n_invalid; 10114be21d56SDavid Gibson 10124be21d56SDavid Gibson index = qemu_get_be32(f); 10134be21d56SDavid Gibson n_valid = qemu_get_be16(f); 10144be21d56SDavid Gibson n_invalid = qemu_get_be16(f); 10154be21d56SDavid Gibson 10164be21d56SDavid Gibson if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) { 10174be21d56SDavid Gibson /* End of Stream */ 10184be21d56SDavid Gibson break; 10194be21d56SDavid Gibson } 10204be21d56SDavid Gibson 1021e68cb8b4SAlexey Kardashevskiy if ((index + n_valid + n_invalid) > 10224be21d56SDavid Gibson (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) { 10234be21d56SDavid Gibson /* Bad index in stream */ 10244be21d56SDavid Gibson fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) " 1025e68cb8b4SAlexey Kardashevskiy "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid, 1026e68cb8b4SAlexey Kardashevskiy spapr->htab_shift); 10274be21d56SDavid Gibson return -EINVAL; 10284be21d56SDavid Gibson } 10294be21d56SDavid Gibson 1030e68cb8b4SAlexey Kardashevskiy if (spapr->htab) { 10314be21d56SDavid Gibson if (n_valid) { 10324be21d56SDavid Gibson qemu_get_buffer(f, HPTE(spapr->htab, index), 10334be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_valid); 10344be21d56SDavid Gibson } 10354be21d56SDavid Gibson if (n_invalid) { 10364be21d56SDavid Gibson memset(HPTE(spapr->htab, index + n_valid), 0, 10374be21d56SDavid Gibson HASH_PTE_SIZE_64 * n_invalid); 10384be21d56SDavid Gibson } 1039e68cb8b4SAlexey Kardashevskiy } else { 1040e68cb8b4SAlexey Kardashevskiy int rc; 1041e68cb8b4SAlexey Kardashevskiy 1042e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1043e68cb8b4SAlexey Kardashevskiy 1044e68cb8b4SAlexey Kardashevskiy rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid); 1045e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 1046e68cb8b4SAlexey Kardashevskiy return rc; 1047e68cb8b4SAlexey Kardashevskiy } 1048e68cb8b4SAlexey Kardashevskiy } 1049e68cb8b4SAlexey Kardashevskiy } 1050e68cb8b4SAlexey Kardashevskiy 1051e68cb8b4SAlexey Kardashevskiy if (!spapr->htab) { 1052e68cb8b4SAlexey Kardashevskiy assert(fd >= 0); 1053e68cb8b4SAlexey Kardashevskiy close(fd); 10544be21d56SDavid Gibson } 10554be21d56SDavid Gibson 10564be21d56SDavid Gibson return 0; 10574be21d56SDavid Gibson } 10584be21d56SDavid Gibson 10594be21d56SDavid Gibson static SaveVMHandlers savevm_htab_handlers = { 10604be21d56SDavid Gibson .save_live_setup = htab_save_setup, 10614be21d56SDavid Gibson .save_live_iterate = htab_save_iterate, 10624be21d56SDavid Gibson .save_live_complete = htab_save_complete, 10634be21d56SDavid Gibson .load_state = htab_load, 10644be21d56SDavid Gibson }; 10654be21d56SDavid Gibson 106653018216SPaolo Bonzini /* pSeries LPAR / sPAPR hardware init */ 106753018216SPaolo Bonzini static void ppc_spapr_init(QEMUMachineInitArgs *args) 106853018216SPaolo Bonzini { 106953018216SPaolo Bonzini ram_addr_t ram_size = args->ram_size; 107053018216SPaolo Bonzini const char *cpu_model = args->cpu_model; 107153018216SPaolo Bonzini const char *kernel_filename = args->kernel_filename; 107253018216SPaolo Bonzini const char *kernel_cmdline = args->kernel_cmdline; 107353018216SPaolo Bonzini const char *initrd_filename = args->initrd_filename; 1074*c1654732SMarkus Armbruster const char *boot_device = args->boot_order; 107553018216SPaolo Bonzini PowerPCCPU *cpu; 107653018216SPaolo Bonzini CPUPPCState *env; 107753018216SPaolo Bonzini PCIHostState *phb; 107853018216SPaolo Bonzini int i; 107953018216SPaolo Bonzini MemoryRegion *sysmem = get_system_memory(); 108053018216SPaolo Bonzini MemoryRegion *ram = g_new(MemoryRegion, 1); 108153018216SPaolo Bonzini hwaddr rma_alloc_size; 108253018216SPaolo Bonzini uint32_t initrd_base = 0; 108353018216SPaolo Bonzini long kernel_size = 0, initrd_size = 0; 108453018216SPaolo Bonzini long load_limit, rtas_limit, fw_size; 108553018216SPaolo Bonzini char *filename; 108653018216SPaolo Bonzini 108753018216SPaolo Bonzini msi_supported = true; 108853018216SPaolo Bonzini 108953018216SPaolo Bonzini spapr = g_malloc0(sizeof(*spapr)); 109053018216SPaolo Bonzini QLIST_INIT(&spapr->phbs); 109153018216SPaolo Bonzini 109253018216SPaolo Bonzini cpu_ppc_hypercall = emulate_spapr_hypercall; 109353018216SPaolo Bonzini 109453018216SPaolo Bonzini /* Allocate RMA if necessary */ 109553018216SPaolo Bonzini rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem); 109653018216SPaolo Bonzini 109753018216SPaolo Bonzini if (rma_alloc_size == -1) { 109853018216SPaolo Bonzini hw_error("qemu: Unable to create RMA\n"); 109953018216SPaolo Bonzini exit(1); 110053018216SPaolo Bonzini } 110153018216SPaolo Bonzini 110253018216SPaolo Bonzini if (rma_alloc_size && (rma_alloc_size < ram_size)) { 110353018216SPaolo Bonzini spapr->rma_size = rma_alloc_size; 110453018216SPaolo Bonzini } else { 110553018216SPaolo Bonzini spapr->rma_size = ram_size; 110653018216SPaolo Bonzini 110753018216SPaolo Bonzini /* With KVM, we don't actually know whether KVM supports an 110853018216SPaolo Bonzini * unbounded RMA (PR KVM) or is limited by the hash table size 110953018216SPaolo Bonzini * (HV KVM using VRMA), so we always assume the latter 111053018216SPaolo Bonzini * 111153018216SPaolo Bonzini * In that case, we also limit the initial allocations for RTAS 111253018216SPaolo Bonzini * etc... to 256M since we have no way to know what the VRMA size 111353018216SPaolo Bonzini * is going to be as it depends on the size of the hash table 111453018216SPaolo Bonzini * isn't determined yet. 111553018216SPaolo Bonzini */ 111653018216SPaolo Bonzini if (kvm_enabled()) { 111753018216SPaolo Bonzini spapr->vrma_adjust = 1; 111853018216SPaolo Bonzini spapr->rma_size = MIN(spapr->rma_size, 0x10000000); 111953018216SPaolo Bonzini } 112053018216SPaolo Bonzini } 112153018216SPaolo Bonzini 112253018216SPaolo Bonzini /* We place the device tree and RTAS just below either the top of the RMA, 112353018216SPaolo Bonzini * or just below 2GB, whichever is lowere, so that it can be 112453018216SPaolo Bonzini * processed with 32-bit real mode code if necessary */ 112553018216SPaolo Bonzini rtas_limit = MIN(spapr->rma_size, 0x80000000); 112653018216SPaolo Bonzini spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE; 112753018216SPaolo Bonzini spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE; 112853018216SPaolo Bonzini load_limit = spapr->fdt_addr - FW_OVERHEAD; 112953018216SPaolo Bonzini 113053018216SPaolo Bonzini /* We aim for a hash table of size 1/128 the size of RAM. The 113153018216SPaolo Bonzini * normal rule of thumb is 1/64 the size of RAM, but that's much 113253018216SPaolo Bonzini * more than needed for the Linux guests we support. */ 113353018216SPaolo Bonzini spapr->htab_shift = 18; /* Minimum architected size */ 113453018216SPaolo Bonzini while (spapr->htab_shift <= 46) { 113553018216SPaolo Bonzini if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) { 113653018216SPaolo Bonzini break; 113753018216SPaolo Bonzini } 113853018216SPaolo Bonzini spapr->htab_shift++; 113953018216SPaolo Bonzini } 114053018216SPaolo Bonzini 11417b565160SDavid Gibson /* Set up Interrupt Controller before we create the VCPUs */ 11427b565160SDavid Gibson spapr->icp = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads, 11437b565160SDavid Gibson XICS_IRQS); 11447b565160SDavid Gibson spapr->next_irq = XICS_IRQ_BASE; 11457b565160SDavid Gibson 114653018216SPaolo Bonzini /* init CPUs */ 114753018216SPaolo Bonzini if (cpu_model == NULL) { 114853018216SPaolo Bonzini cpu_model = kvm_enabled() ? "host" : "POWER7"; 114953018216SPaolo Bonzini } 115053018216SPaolo Bonzini for (i = 0; i < smp_cpus; i++) { 115153018216SPaolo Bonzini cpu = cpu_ppc_init(cpu_model); 115253018216SPaolo Bonzini if (cpu == NULL) { 115353018216SPaolo Bonzini fprintf(stderr, "Unable to find PowerPC CPU definition\n"); 115453018216SPaolo Bonzini exit(1); 115553018216SPaolo Bonzini } 115653018216SPaolo Bonzini env = &cpu->env; 115753018216SPaolo Bonzini 11587b565160SDavid Gibson xics_cpu_setup(spapr->icp, cpu); 11597b565160SDavid Gibson 116053018216SPaolo Bonzini /* Set time-base frequency to 512 MHz */ 116153018216SPaolo Bonzini cpu_ppc_tb_init(env, TIMEBASE_FREQ); 116253018216SPaolo Bonzini 11632cf3eb6dSFabien Chouteau /* PAPR always has exception vectors in RAM not ROM. To ensure this, 11642cf3eb6dSFabien Chouteau * MSR[IP] should never be set. 11652cf3eb6dSFabien Chouteau */ 11662cf3eb6dSFabien Chouteau env->msr_mask &= ~(1 << 6); 116753018216SPaolo Bonzini 116853018216SPaolo Bonzini /* Tell KVM that we're in PAPR mode */ 116953018216SPaolo Bonzini if (kvm_enabled()) { 117053018216SPaolo Bonzini kvmppc_set_papr(cpu); 117153018216SPaolo Bonzini } 117253018216SPaolo Bonzini 117353018216SPaolo Bonzini qemu_register_reset(spapr_cpu_reset, cpu); 117453018216SPaolo Bonzini } 117553018216SPaolo Bonzini 117653018216SPaolo Bonzini /* allocate RAM */ 117753018216SPaolo Bonzini spapr->ram_limit = ram_size; 117853018216SPaolo Bonzini if (spapr->ram_limit > rma_alloc_size) { 117953018216SPaolo Bonzini ram_addr_t nonrma_base = rma_alloc_size; 118053018216SPaolo Bonzini ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size; 118153018216SPaolo Bonzini 11822c9b15caSPaolo Bonzini memory_region_init_ram(ram, NULL, "ppc_spapr.ram", nonrma_size); 118353018216SPaolo Bonzini vmstate_register_ram_global(ram); 118453018216SPaolo Bonzini memory_region_add_subregion(sysmem, nonrma_base, ram); 118553018216SPaolo Bonzini } 118653018216SPaolo Bonzini 118753018216SPaolo Bonzini filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin"); 118853018216SPaolo Bonzini spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr, 118953018216SPaolo Bonzini rtas_limit - spapr->rtas_addr); 119053018216SPaolo Bonzini if (spapr->rtas_size < 0) { 119153018216SPaolo Bonzini hw_error("qemu: could not load LPAR rtas '%s'\n", filename); 119253018216SPaolo Bonzini exit(1); 119353018216SPaolo Bonzini } 119453018216SPaolo Bonzini if (spapr->rtas_size > RTAS_MAX_SIZE) { 119553018216SPaolo Bonzini hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n", 119653018216SPaolo Bonzini spapr->rtas_size, RTAS_MAX_SIZE); 119753018216SPaolo Bonzini exit(1); 119853018216SPaolo Bonzini } 119953018216SPaolo Bonzini g_free(filename); 120053018216SPaolo Bonzini 120153018216SPaolo Bonzini /* Set up EPOW events infrastructure */ 120253018216SPaolo Bonzini spapr_events_init(spapr); 120353018216SPaolo Bonzini 120453018216SPaolo Bonzini /* Set up VIO bus */ 120553018216SPaolo Bonzini spapr->vio_bus = spapr_vio_bus_init(); 120653018216SPaolo Bonzini 120753018216SPaolo Bonzini for (i = 0; i < MAX_SERIAL_PORTS; i++) { 120853018216SPaolo Bonzini if (serial_hds[i]) { 120953018216SPaolo Bonzini spapr_vty_create(spapr->vio_bus, serial_hds[i]); 121053018216SPaolo Bonzini } 121153018216SPaolo Bonzini } 121253018216SPaolo Bonzini 121353018216SPaolo Bonzini /* We always have at least the nvram device on VIO */ 121453018216SPaolo Bonzini spapr_create_nvram(spapr); 121553018216SPaolo Bonzini 121653018216SPaolo Bonzini /* Set up PCI */ 121753018216SPaolo Bonzini spapr_pci_rtas_init(); 121853018216SPaolo Bonzini 121989dfd6e1SDavid Gibson phb = spapr_create_phb(spapr, 0); 122053018216SPaolo Bonzini 122153018216SPaolo Bonzini for (i = 0; i < nb_nics; i++) { 122253018216SPaolo Bonzini NICInfo *nd = &nd_table[i]; 122353018216SPaolo Bonzini 122453018216SPaolo Bonzini if (!nd->model) { 122553018216SPaolo Bonzini nd->model = g_strdup("ibmveth"); 122653018216SPaolo Bonzini } 122753018216SPaolo Bonzini 122853018216SPaolo Bonzini if (strcmp(nd->model, "ibmveth") == 0) { 122953018216SPaolo Bonzini spapr_vlan_create(spapr->vio_bus, nd); 123053018216SPaolo Bonzini } else { 123129b358f9SDavid Gibson pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL); 123253018216SPaolo Bonzini } 123353018216SPaolo Bonzini } 123453018216SPaolo Bonzini 123553018216SPaolo Bonzini for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) { 123653018216SPaolo Bonzini spapr_vscsi_create(spapr->vio_bus); 123753018216SPaolo Bonzini } 123853018216SPaolo Bonzini 123953018216SPaolo Bonzini /* Graphics */ 124053018216SPaolo Bonzini if (spapr_vga_init(phb->bus)) { 124153018216SPaolo Bonzini spapr->has_graphics = true; 124253018216SPaolo Bonzini } 124353018216SPaolo Bonzini 124453018216SPaolo Bonzini if (usb_enabled(spapr->has_graphics)) { 124553018216SPaolo Bonzini pci_create_simple(phb->bus, -1, "pci-ohci"); 124653018216SPaolo Bonzini if (spapr->has_graphics) { 124753018216SPaolo Bonzini usbdevice_create("keyboard"); 124853018216SPaolo Bonzini usbdevice_create("mouse"); 124953018216SPaolo Bonzini } 125053018216SPaolo Bonzini } 125153018216SPaolo Bonzini 125253018216SPaolo Bonzini if (spapr->rma_size < (MIN_RMA_SLOF << 20)) { 125353018216SPaolo Bonzini fprintf(stderr, "qemu: pSeries SLOF firmware requires >= " 125453018216SPaolo Bonzini "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF); 125553018216SPaolo Bonzini exit(1); 125653018216SPaolo Bonzini } 125753018216SPaolo Bonzini 125853018216SPaolo Bonzini if (kernel_filename) { 125953018216SPaolo Bonzini uint64_t lowaddr = 0; 126053018216SPaolo Bonzini 126153018216SPaolo Bonzini kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL, 126253018216SPaolo Bonzini NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0); 126353018216SPaolo Bonzini if (kernel_size < 0) { 126453018216SPaolo Bonzini kernel_size = load_image_targphys(kernel_filename, 126553018216SPaolo Bonzini KERNEL_LOAD_ADDR, 126653018216SPaolo Bonzini load_limit - KERNEL_LOAD_ADDR); 126753018216SPaolo Bonzini } 126853018216SPaolo Bonzini if (kernel_size < 0) { 126953018216SPaolo Bonzini fprintf(stderr, "qemu: could not load kernel '%s'\n", 127053018216SPaolo Bonzini kernel_filename); 127153018216SPaolo Bonzini exit(1); 127253018216SPaolo Bonzini } 127353018216SPaolo Bonzini 127453018216SPaolo Bonzini /* load initrd */ 127553018216SPaolo Bonzini if (initrd_filename) { 127653018216SPaolo Bonzini /* Try to locate the initrd in the gap between the kernel 127753018216SPaolo Bonzini * and the firmware. Add a bit of space just in case 127853018216SPaolo Bonzini */ 127953018216SPaolo Bonzini initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff; 128053018216SPaolo Bonzini initrd_size = load_image_targphys(initrd_filename, initrd_base, 128153018216SPaolo Bonzini load_limit - initrd_base); 128253018216SPaolo Bonzini if (initrd_size < 0) { 128353018216SPaolo Bonzini fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 128453018216SPaolo Bonzini initrd_filename); 128553018216SPaolo Bonzini exit(1); 128653018216SPaolo Bonzini } 128753018216SPaolo Bonzini } else { 128853018216SPaolo Bonzini initrd_base = 0; 128953018216SPaolo Bonzini initrd_size = 0; 129053018216SPaolo Bonzini } 129153018216SPaolo Bonzini } 129253018216SPaolo Bonzini 12938e7ea787SAndreas Färber if (bios_name == NULL) { 12948e7ea787SAndreas Färber bios_name = FW_FILE_NAME; 12958e7ea787SAndreas Färber } 12968e7ea787SAndreas Färber filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); 129753018216SPaolo Bonzini fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE); 129853018216SPaolo Bonzini if (fw_size < 0) { 129953018216SPaolo Bonzini hw_error("qemu: could not load LPAR rtas '%s'\n", filename); 130053018216SPaolo Bonzini exit(1); 130153018216SPaolo Bonzini } 130253018216SPaolo Bonzini g_free(filename); 130353018216SPaolo Bonzini 130453018216SPaolo Bonzini spapr->entry_point = 0x100; 130553018216SPaolo Bonzini 13064be21d56SDavid Gibson vmstate_register(NULL, 0, &vmstate_spapr, spapr); 13074be21d56SDavid Gibson register_savevm_live(NULL, "spapr/htab", -1, 1, 13084be21d56SDavid Gibson &savevm_htab_handlers, spapr); 13094be21d56SDavid Gibson 131053018216SPaolo Bonzini /* Prepare the device tree */ 131153018216SPaolo Bonzini spapr->fdt_skel = spapr_create_fdt_skel(cpu_model, 131253018216SPaolo Bonzini initrd_base, initrd_size, 131353018216SPaolo Bonzini kernel_size, 131453018216SPaolo Bonzini boot_device, kernel_cmdline, 131553018216SPaolo Bonzini spapr->epow_irq); 131653018216SPaolo Bonzini assert(spapr->fdt_skel != NULL); 131753018216SPaolo Bonzini } 131853018216SPaolo Bonzini 131953018216SPaolo Bonzini static QEMUMachine spapr_machine = { 132053018216SPaolo Bonzini .name = "pseries", 132153018216SPaolo Bonzini .desc = "pSeries Logical Partition (PAPR compliant)", 1322159f8286SDavid Gibson .is_default = 1, 132353018216SPaolo Bonzini .init = ppc_spapr_init, 132453018216SPaolo Bonzini .reset = ppc_spapr_reset, 132553018216SPaolo Bonzini .block_default_type = IF_SCSI, 132653018216SPaolo Bonzini .max_cpus = MAX_CPUS, 132753018216SPaolo Bonzini .no_parallel = 1, 1328*c1654732SMarkus Armbruster .default_boot_order = NULL, 132953018216SPaolo Bonzini }; 133053018216SPaolo Bonzini 133153018216SPaolo Bonzini static void spapr_machine_init(void) 133253018216SPaolo Bonzini { 133353018216SPaolo Bonzini qemu_register_machine(&spapr_machine); 133453018216SPaolo Bonzini } 133553018216SPaolo Bonzini 133653018216SPaolo Bonzini machine_init(spapr_machine_init); 1337