]> Git Repo - qemu.git/blame - hw/openrisc/openrisc_sim.c
Include hw/hw.h exactly where needed
[qemu.git] / hw / openrisc / openrisc_sim.c
CommitLineData
ce6e1e9e
JL
1/*
2 * OpenRISC simulator for use as an IIS.
3 *
4 * Copyright (c) 2011-2012 Jia Liu <[email protected]>
5 * Feng Gao <[email protected]>
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
198a2d21 10 * version 2.1 of the License, or (at your option) any later version.
ce6e1e9e
JL
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
ed2decc6 21#include "qemu/osdep.h"
fe2d93c8 22#include "qemu/error-report.h"
da34e65c 23#include "qapi/error.h"
4771d756 24#include "cpu.h"
64552b6b 25#include "hw/irq.h"
83c9f4ca 26#include "hw/boards.h"
ce6e1e9e 27#include "elf.h"
0d09e41a 28#include "hw/char/serial.h"
1422e32d 29#include "net/net.h"
83c9f4ca 30#include "hw/loader.h"
022c62cb 31#include "exec/address-spaces.h"
9c17d615 32#include "sysemu/sysemu.h"
83c9f4ca 33#include "hw/sysbus.h"
9c17d615 34#include "sysemu/qtest.h"
71e8a915 35#include "sysemu/reset.h"
ce6e1e9e
JL
36
37#define KERNEL_LOAD_ADDR 0x100
38
13f1c773
SH
39static struct openrisc_boot_info {
40 uint32_t bootstrap_pc;
41} boot_info;
42
ce6e1e9e
JL
43static void main_cpu_reset(void *opaque)
44{
45 OpenRISCCPU *cpu = opaque;
13f1c773 46 CPUState *cs = CPU(cpu);
ce6e1e9e
JL
47
48 cpu_reset(CPU(cpu));
13f1c773
SH
49
50 cpu_set_pc(cs, boot_info.bootstrap_pc);
ce6e1e9e
JL
51}
52
13f1c773
SH
53static void openrisc_sim_net_init(hwaddr base, hwaddr descriptors,
54 int num_cpus, qemu_irq **cpu_irqs,
55 int irq_pin, NICInfo *nd)
ce6e1e9e
JL
56{
57 DeviceState *dev;
58 SysBusDevice *s;
13f1c773 59 int i;
ce6e1e9e
JL
60
61 dev = qdev_create(NULL, "open_eth");
62 qdev_set_nic_properties(dev, nd);
63 qdev_init_nofail(dev);
64
1356b98d 65 s = SYS_BUS_DEVICE(dev);
13f1c773
SH
66 for (i = 0; i < num_cpus; i++) {
67 sysbus_connect_irq(s, 0, cpu_irqs[i][irq_pin]);
68 }
69 sysbus_mmio_map(s, 0, base);
70 sysbus_mmio_map(s, 1, descriptors);
71}
72
73static void openrisc_sim_ompic_init(hwaddr base, int num_cpus,
74 qemu_irq **cpu_irqs, int irq_pin)
75{
76 DeviceState *dev;
77 SysBusDevice *s;
78 int i;
79
80 dev = qdev_create(NULL, "or1k-ompic");
81 qdev_prop_set_uint32(dev, "num-cpus", num_cpus);
82 qdev_init_nofail(dev);
83
84 s = SYS_BUS_DEVICE(dev);
85 for (i = 0; i < num_cpus; i++) {
86 sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
87 }
88 sysbus_mmio_map(s, 0, base);
ce6e1e9e
JL
89}
90
13f1c773
SH
91static void openrisc_load_kernel(ram_addr_t ram_size,
92 const char *kernel_filename)
ce6e1e9e
JL
93{
94 long kernel_size;
95 uint64_t elf_entry;
a8170e5e 96 hwaddr entry;
ce6e1e9e
JL
97
98 if (kernel_filename && !qtest_enabled()) {
4366e1db 99 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
7ef295ea
PC
100 &elf_entry, NULL, NULL, 1, EM_OPENRISC,
101 1, 0);
ce6e1e9e
JL
102 entry = elf_entry;
103 if (kernel_size < 0) {
104 kernel_size = load_uimage(kernel_filename,
25bda50a 105 &entry, NULL, NULL, NULL, NULL);
ce6e1e9e
JL
106 }
107 if (kernel_size < 0) {
108 kernel_size = load_image_targphys(kernel_filename,
109 KERNEL_LOAD_ADDR,
110 ram_size - KERNEL_LOAD_ADDR);
13f1c773
SH
111 }
112
113 if (entry <= 0) {
ce6e1e9e
JL
114 entry = KERNEL_LOAD_ADDR;
115 }
116
117 if (kernel_size < 0) {
fe2d93c8 118 error_report("couldn't load the kernel '%s'", kernel_filename);
ce6e1e9e
JL
119 exit(1);
120 }
13f1c773 121 boot_info.bootstrap_pc = entry;
ce6e1e9e 122 }
ce6e1e9e
JL
123}
124
3ef96221 125static void openrisc_sim_init(MachineState *machine)
ce6e1e9e 126{
3ef96221 127 ram_addr_t ram_size = machine->ram_size;
3ef96221 128 const char *kernel_filename = machine->kernel_filename;
68f12828 129 OpenRISCCPU *cpu = NULL;
ce6e1e9e 130 MemoryRegion *ram;
13f1c773
SH
131 qemu_irq *cpu_irqs[2];
132 qemu_irq serial_irq;
ce6e1e9e 133 int n;
33decbd2 134 unsigned int smp_cpus = machine->smp.cpus;
ce6e1e9e 135
ce6e1e9e 136 for (n = 0; n < smp_cpus; n++) {
1498e970 137 cpu = OPENRISC_CPU(cpu_create(machine->cpu_type));
13f1c773
SH
138 if (cpu == NULL) {
139 fprintf(stderr, "Unable to find CPU definition!\n");
140 exit(1);
141 }
142 cpu_openrisc_pic_init(cpu);
143 cpu_irqs[n] = (qemu_irq *) cpu->env.irq;
144
145 cpu_openrisc_clock_init(cpu);
146
ce6e1e9e 147 qemu_register_reset(main_cpu_reset, cpu);
ce6e1e9e
JL
148 }
149
150 ram = g_malloc(sizeof(*ram));
98a99ce0 151 memory_region_init_ram(ram, NULL, "openrisc.ram", ram_size, &error_fatal);
ce6e1e9e
JL
152 memory_region_add_subregion(get_system_memory(), 0, ram);
153
13f1c773
SH
154 if (nd_table[0].used) {
155 openrisc_sim_net_init(0x92000000, 0x92000400, smp_cpus,
156 cpu_irqs, 4, nd_table);
157 }
ce6e1e9e 158
13f1c773
SH
159 if (smp_cpus > 1) {
160 openrisc_sim_ompic_init(0x98000000, smp_cpus, cpu_irqs, 1);
ce6e1e9e 161
13f1c773
SH
162 serial_irq = qemu_irq_split(cpu_irqs[0][2], cpu_irqs[1][2]);
163 } else {
164 serial_irq = cpu_irqs[0][2];
ce6e1e9e
JL
165 }
166
13f1c773 167 serial_mm_init(get_system_memory(), 0x90000000, 0, serial_irq,
9bca0edb 168 115200, serial_hd(0), DEVICE_NATIVE_ENDIAN);
13f1c773
SH
169
170 openrisc_load_kernel(ram_size, kernel_filename);
ce6e1e9e
JL
171}
172
e264d29d 173static void openrisc_sim_machine_init(MachineClass *mc)
ce6e1e9e 174{
4a09d0bb 175 mc->desc = "or1k simulation";
e264d29d 176 mc->init = openrisc_sim_init;
13f1c773 177 mc->max_cpus = 2;
e264d29d 178 mc->is_default = 1;
1498e970 179 mc->default_cpu_type = OPENRISC_CPU_TYPE_NAME("or1200");
ce6e1e9e
JL
180}
181
4a09d0bb 182DEFINE_MACHINE("or1k-sim", openrisc_sim_machine_init)
This page took 0.405373 seconds and 4 git commands to generate.