qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

hw/xtensa: add virt machine

virt machine is a sim machine with generic PCI host controller.
Make common parts of sim machine initialization reusable.
Add PCI controller at 0xf0000000 with PIO space at its base address,
ECAM space at base address + 1M and MMIO space at base address + 64M.
Connect IRQ lines to consecutive CPU external IRQ pins starting from 0.
Instantiate network interfaces on virt machine.

Xtensa linux kernel configuration virt_defconfig can successfully boot
on this machine.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>

+208 -15
+5
MAINTAINERS
··· 1302 1302 S: Maintained 1303 1303 F: hw/xtensa/sim.c 1304 1304 1305 + virt 1306 + M: Max Filippov <jcmvbkbc@gmail.com> 1307 + S: Maintained 1308 + F: hw/xtensa/virt.c 1309 + 1305 1310 XTFPGA (LX60, LX200, ML605, KC705) 1306 1311 M: Max Filippov <jcmvbkbc@gmail.com> 1307 1312 S: Maintained
+1
default-configs/xtensa-softmmu.mak
··· 5 5 # Boards: 6 6 # 7 7 CONFIG_XTENSA_SIM=y 8 + CONFIG_XTENSA_VIRT=y 8 9 CONFIG_XTENSA_XTFPGA=y
+6
hw/xtensa/Kconfig
··· 1 1 config XTENSA_SIM 2 2 bool 3 3 4 + config XTENSA_VIRT 5 + bool 6 + select XTENSA_SIM 7 + select PCI_EXPRESS_GENERIC_BRIDGE 8 + select PCI_DEVICES 9 + 4 10 config XTENSA_XTFPGA 5 11 bool 6 12 select OPENCORES_ETH
+1
hw/xtensa/Makefile.objs
··· 2 2 obj-y += pic_cpu.o 3 3 obj-y += xtensa_memory.o 4 4 obj-$(CONFIG_XTENSA_SIM) += sim.o 5 + obj-$(CONFIG_XTENSA_VIRT) += virt.o 5 6 obj-$(CONFIG_XTENSA_XTFPGA) += xtfpga.o
+26 -15
hw/xtensa/sim.c
··· 37 37 #include "exec/address-spaces.h" 38 38 #include "qemu/error-report.h" 39 39 #include "xtensa_memory.h" 40 + #include "xtensa_sim.h" 40 41 41 42 static uint64_t translate_phys_addr(void *opaque, uint64_t addr) 42 43 { ··· 52 53 cpu_reset(CPU(cpu)); 53 54 } 54 55 55 - static void xtensa_sim_init(MachineState *machine) 56 + XtensaCPU *xtensa_sim_common_init(MachineState *machine) 56 57 { 57 58 XtensaCPU *cpu = NULL; 58 59 CPUXtensaState *env = NULL; 59 60 ram_addr_t ram_size = machine->ram_size; 60 - const char *kernel_filename = machine->kernel_filename; 61 61 int n; 62 62 63 63 for (n = 0; n < machine->smp.cpus; n++) { ··· 89 89 xtensa_create_memory_regions(&sysram, "xtensa.sysram", 90 90 get_system_memory()); 91 91 } 92 - 93 92 if (serial_hd(0)) { 94 93 xtensa_sim_open_console(serial_hd(0)); 95 94 } 95 + return cpu; 96 + } 97 + 98 + void xtensa_sim_load_kernel(XtensaCPU *cpu, MachineState *machine) 99 + { 100 + const char *kernel_filename = machine->kernel_filename; 101 + #ifdef TARGET_WORDS_BIGENDIAN 102 + int big_endian = true; 103 + #else 104 + int big_endian = false; 105 + #endif 106 + 96 107 if (kernel_filename) { 97 108 uint64_t elf_entry; 98 109 uint64_t elf_lowaddr; 99 - #ifdef TARGET_WORDS_BIGENDIAN 100 - int success = load_elf(kernel_filename, NULL, 101 - translate_phys_addr, cpu, 102 - &elf_entry, &elf_lowaddr, 103 - NULL, 1, EM_XTENSA, 0, 0); 104 - #else 105 - int success = load_elf(kernel_filename, NULL, 106 - translate_phys_addr, cpu, 107 - &elf_entry, &elf_lowaddr, 108 - NULL, 0, EM_XTENSA, 0, 0); 109 - #endif 110 + int success = load_elf(kernel_filename, NULL, translate_phys_addr, cpu, 111 + &elf_entry, &elf_lowaddr, NULL, big_endian, 112 + EM_XTENSA, 0, 0); 113 + 110 114 if (success > 0) { 111 - env->pc = elf_entry; 115 + cpu->env.pc = elf_entry; 112 116 } 113 117 } 118 + } 119 + 120 + static void xtensa_sim_init(MachineState *machine) 121 + { 122 + XtensaCPU *cpu = xtensa_sim_common_init(machine); 123 + 124 + xtensa_sim_load_kernel(cpu, machine); 114 125 } 115 126 116 127 static void xtensa_sim_machine_init(MachineClass *mc)
+135
hw/xtensa/virt.c
··· 1 + /* 2 + * Copyright (c) 2019, Max Filippov, Open Source and Linux Lab. 3 + * All rights reserved. 4 + * 5 + * Redistribution and use in source and binary forms, with or without 6 + * modification, are permitted provided that the following conditions are met: 7 + * * Redistributions of source code must retain the above copyright 8 + * notice, this list of conditions and the following disclaimer. 9 + * * Redistributions in binary form must reproduce the above copyright 10 + * notice, this list of conditions and the following disclaimer in the 11 + * documentation and/or other materials provided with the distribution. 12 + * * Neither the name of the Open Source and Linux Lab nor the 13 + * names of its contributors may be used to endorse or promote products 14 + * derived from this software without specific prior written permission. 15 + * 16 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 + */ 27 + 28 + #include "qemu/osdep.h" 29 + #include "qapi/error.h" 30 + #include "cpu.h" 31 + #include "sysemu/reset.h" 32 + #include "sysemu/sysemu.h" 33 + #include "hw/boards.h" 34 + #include "hw/loader.h" 35 + #include "hw/pci-host/gpex.h" 36 + #include "net/net.h" 37 + #include "elf.h" 38 + #include "exec/memory.h" 39 + #include "exec/address-spaces.h" 40 + #include "qemu/error-report.h" 41 + #include "xtensa_memory.h" 42 + #include "xtensa_sim.h" 43 + 44 + static void create_pcie(CPUXtensaState *env, int irq_base, hwaddr addr_base) 45 + { 46 + hwaddr base_ecam = addr_base + 0x00100000; 47 + hwaddr size_ecam = 0x03f00000; 48 + hwaddr base_pio = addr_base + 0x00000000; 49 + hwaddr size_pio = 0x00010000; 50 + hwaddr base_mmio = addr_base + 0x04000000; 51 + hwaddr size_mmio = 0x08000000; 52 + 53 + MemoryRegion *ecam_alias; 54 + MemoryRegion *ecam_reg; 55 + MemoryRegion *pio_alias; 56 + MemoryRegion *pio_reg; 57 + MemoryRegion *mmio_alias; 58 + MemoryRegion *mmio_reg; 59 + 60 + DeviceState *dev; 61 + PCIHostState *pci; 62 + qemu_irq *extints; 63 + int i; 64 + 65 + dev = qdev_create(NULL, TYPE_GPEX_HOST); 66 + qdev_init_nofail(dev); 67 + 68 + /* Map only the first size_ecam bytes of ECAM space. */ 69 + ecam_alias = g_new0(MemoryRegion, 1); 70 + ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0); 71 + memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam", 72 + ecam_reg, 0, size_ecam); 73 + memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias); 74 + 75 + /* 76 + * Map the MMIO window into system address space so as to expose 77 + * the section of PCI MMIO space which starts at the same base address 78 + * (ie 1:1 mapping for that part of PCI MMIO space visible through 79 + * the window). 80 + */ 81 + mmio_alias = g_new0(MemoryRegion, 1); 82 + mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1); 83 + memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio", 84 + mmio_reg, base_mmio, size_mmio); 85 + memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias); 86 + 87 + /* Map IO port space. */ 88 + pio_alias = g_new0(MemoryRegion, 1); 89 + pio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 2); 90 + memory_region_init_alias(pio_alias, OBJECT(dev), "pcie-pio", 91 + pio_reg, 0, size_pio); 92 + memory_region_add_subregion(get_system_memory(), base_pio, pio_alias); 93 + 94 + /* Connect IRQ lines. */ 95 + extints = xtensa_get_extints(env); 96 + 97 + for (i = 0; i < GPEX_NUM_IRQS; i++) { 98 + void *q = extints[irq_base + i]; 99 + 100 + sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, q); 101 + gpex_set_irq_num(GPEX_HOST(dev), i, irq_base + i); 102 + } 103 + 104 + pci = PCI_HOST_BRIDGE(dev); 105 + if (pci->bus) { 106 + for (i = 0; i < nb_nics; i++) { 107 + NICInfo *nd = &nd_table[i]; 108 + 109 + if (!nd->model) { 110 + nd->model = g_strdup("virtio"); 111 + } 112 + 113 + pci_nic_init_nofail(nd, pci->bus, nd->model, NULL); 114 + } 115 + } 116 + } 117 + 118 + static void xtensa_virt_init(MachineState *machine) 119 + { 120 + XtensaCPU *cpu = xtensa_sim_common_init(machine); 121 + CPUXtensaState *env = &cpu->env; 122 + 123 + create_pcie(env, 0, 0xf0000000); 124 + xtensa_sim_load_kernel(cpu, machine); 125 + } 126 + 127 + static void xtensa_virt_machine_init(MachineClass *mc) 128 + { 129 + mc->desc = "virt machine (" XTENSA_DEFAULT_CPU_MODEL ")"; 130 + mc->init = xtensa_virt_init; 131 + mc->max_cpus = 32; 132 + mc->default_cpu_type = XTENSA_DEFAULT_CPU_TYPE; 133 + } 134 + 135 + DEFINE_MACHINE("virt", xtensa_virt_machine_init)
+34
hw/xtensa/xtensa_sim.h
··· 1 + /* 2 + * Copyright (c) 2019, Max Filippov, Open Source and Linux Lab. 3 + * All rights reserved. 4 + * 5 + * Redistribution and use in source and binary forms, with or without 6 + * modification, are permitted provided that the following conditions are met: 7 + * * Redistributions of source code must retain the above copyright 8 + * notice, this list of conditions and the following disclaimer. 9 + * * Redistributions in binary form must reproduce the above copyright 10 + * notice, this list of conditions and the following disclaimer in the 11 + * documentation and/or other materials provided with the distribution. 12 + * * Neither the name of the Open Source and Linux Lab nor the 13 + * names of its contributors may be used to endorse or promote products 14 + * derived from this software without specific prior written permission. 15 + * 16 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 + */ 27 + 28 + #ifndef XTENSA_SIM_H 29 + #define XTENSA_SIM_H 30 + 31 + XtensaCPU *xtensa_sim_common_init(MachineState *machine); 32 + void xtensa_sim_load_kernel(XtensaCPU *cpu, MachineState *machine); 33 + 34 + #endif