Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * OpenRISC simulator for use as an IIS. |
| 3 | * |
| 4 | * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com> |
| 5 | * Feng Gao <gf91597@gmail.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 21 | #include "hw/hw.h" |
| 22 | #include "hw/boards.h" |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 23 | #include "elf.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 24 | #include "hw/char/serial.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 25 | #include "net/net.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 26 | #include "hw/loader.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 27 | #include "exec/address-spaces.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 28 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 29 | #include "hw/sysbus.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 30 | #include "sysemu/qtest.h" |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 31 | |
| 32 | #define KERNEL_LOAD_ADDR 0x100 |
| 33 | |
| 34 | static void main_cpu_reset(void *opaque) |
| 35 | { |
| 36 | OpenRISCCPU *cpu = opaque; |
| 37 | |
| 38 | cpu_reset(CPU(cpu)); |
| 39 | } |
| 40 | |
| 41 | static void openrisc_sim_net_init(MemoryRegion *address_space, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 42 | hwaddr base, |
| 43 | hwaddr descriptors, |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 44 | qemu_irq irq, NICInfo *nd) |
| 45 | { |
| 46 | DeviceState *dev; |
| 47 | SysBusDevice *s; |
| 48 | |
| 49 | dev = qdev_create(NULL, "open_eth"); |
| 50 | qdev_set_nic_properties(dev, nd); |
| 51 | qdev_init_nofail(dev); |
| 52 | |
Andreas Färber | 1356b98 | 2013-01-20 02:47:33 +0100 | [diff] [blame] | 53 | s = SYS_BUS_DEVICE(dev); |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 54 | sysbus_connect_irq(s, 0, irq); |
| 55 | memory_region_add_subregion(address_space, base, |
| 56 | sysbus_mmio_get_region(s, 0)); |
| 57 | memory_region_add_subregion(address_space, descriptors, |
| 58 | sysbus_mmio_get_region(s, 1)); |
| 59 | } |
| 60 | |
| 61 | static void cpu_openrisc_load_kernel(ram_addr_t ram_size, |
| 62 | const char *kernel_filename, |
| 63 | OpenRISCCPU *cpu) |
| 64 | { |
| 65 | long kernel_size; |
| 66 | uint64_t elf_entry; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 67 | hwaddr entry; |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 68 | |
| 69 | if (kernel_filename && !qtest_enabled()) { |
| 70 | kernel_size = load_elf(kernel_filename, NULL, NULL, |
Peter Crosthwaite | ed03ecf | 2015-05-10 23:29:10 -0700 | [diff] [blame] | 71 | &elf_entry, NULL, NULL, 1, EM_OPENRISC, 1); |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 72 | entry = elf_entry; |
| 73 | if (kernel_size < 0) { |
| 74 | kernel_size = load_uimage(kernel_filename, |
Max Filippov | 25bda50 | 2014-10-19 07:42:22 +0400 | [diff] [blame] | 75 | &entry, NULL, NULL, NULL, NULL); |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 76 | } |
| 77 | if (kernel_size < 0) { |
| 78 | kernel_size = load_image_targphys(kernel_filename, |
| 79 | KERNEL_LOAD_ADDR, |
| 80 | ram_size - KERNEL_LOAD_ADDR); |
| 81 | entry = KERNEL_LOAD_ADDR; |
| 82 | } |
| 83 | |
| 84 | if (kernel_size < 0) { |
Jia Liu | 4284c05 | 2013-07-23 18:31:24 +0800 | [diff] [blame] | 85 | fprintf(stderr, "QEMU: couldn't load the kernel '%s'\n", |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 86 | kernel_filename); |
| 87 | exit(1); |
| 88 | } |
Jia Liu | b6d9766 | 2013-08-21 08:54:29 +0800 | [diff] [blame] | 89 | cpu->env.pc = entry; |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 90 | } |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Marcel Apfelbaum | 3ef9622 | 2014-05-07 17:42:57 +0300 | [diff] [blame] | 93 | static void openrisc_sim_init(MachineState *machine) |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 94 | { |
Marcel Apfelbaum | 3ef9622 | 2014-05-07 17:42:57 +0300 | [diff] [blame] | 95 | ram_addr_t ram_size = machine->ram_size; |
| 96 | const char *cpu_model = machine->cpu_model; |
| 97 | const char *kernel_filename = machine->kernel_filename; |
Jia Liu | 68f1282 | 2013-07-23 18:30:09 +0800 | [diff] [blame] | 98 | OpenRISCCPU *cpu = NULL; |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 99 | MemoryRegion *ram; |
| 100 | int n; |
| 101 | |
| 102 | if (!cpu_model) { |
| 103 | cpu_model = "or1200"; |
| 104 | } |
| 105 | |
| 106 | for (n = 0; n < smp_cpus; n++) { |
| 107 | cpu = cpu_openrisc_init(cpu_model); |
| 108 | if (cpu == NULL) { |
Jia Liu | 4284c05 | 2013-07-23 18:31:24 +0800 | [diff] [blame] | 109 | fprintf(stderr, "Unable to find CPU definition!\n"); |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 110 | exit(1); |
| 111 | } |
| 112 | qemu_register_reset(main_cpu_reset, cpu); |
| 113 | main_cpu_reset(cpu); |
| 114 | } |
| 115 | |
| 116 | ram = g_malloc(sizeof(*ram)); |
Markus Armbruster | f8ed85a | 2015-09-11 16:51:43 +0200 | [diff] [blame] | 117 | memory_region_init_ram(ram, NULL, "openrisc.ram", ram_size, &error_fatal); |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 118 | vmstate_register_ram_global(ram); |
| 119 | memory_region_add_subregion(get_system_memory(), 0, ram); |
| 120 | |
| 121 | cpu_openrisc_pic_init(cpu); |
| 122 | cpu_openrisc_clock_init(cpu); |
| 123 | |
| 124 | serial_mm_init(get_system_memory(), 0x90000000, 0, cpu->env.irq[2], |
| 125 | 115200, serial_hds[0], DEVICE_NATIVE_ENDIAN); |
| 126 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 127 | if (nd_table[0].used) { |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 128 | openrisc_sim_net_init(get_system_memory(), 0x92000000, |
| 129 | 0x92000400, cpu->env.irq[4], nd_table); |
| 130 | } |
| 131 | |
| 132 | cpu_openrisc_load_kernel(ram_size, kernel_filename, cpu); |
| 133 | } |
| 134 | |
Eduardo Habkost | e264d29 | 2015-09-04 15:37:08 -0300 | [diff] [blame] | 135 | static void openrisc_sim_machine_init(MachineClass *mc) |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 136 | { |
Eduardo Habkost | e264d29 | 2015-09-04 15:37:08 -0300 | [diff] [blame] | 137 | mc->desc = "or32 simulation"; |
| 138 | mc->init = openrisc_sim_init; |
| 139 | mc->max_cpus = 1; |
| 140 | mc->is_default = 1; |
Jia Liu | ce6e1e9 | 2012-07-20 15:50:48 +0800 | [diff] [blame] | 141 | } |
| 142 | |
Eduardo Habkost | e264d29 | 2015-09-04 15:37:08 -0300 | [diff] [blame] | 143 | DEFINE_MACHINE("or32-sim", openrisc_sim_machine_init) |