]> Git Repo - qemu.git/blame - hw/openrisc/openrisc_sim.c
works with less than base ISA qemu-system-riscv32 -M virt -bios none -kernel output...
[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"
0d09e41a 27#include "hw/char/serial.h"
1422e32d 28#include "net/net.h"
7025114b 29#include "hw/openrisc/boot.h"
a27bd6c7 30#include "hw/qdev-properties.h"
5852c1f8
SH
31#include "exec/address-spaces.h"
32#include "sysemu/device_tree.h"
9c17d615 33#include "sysemu/sysemu.h"
83c9f4ca 34#include "hw/sysbus.h"
9c17d615 35#include "sysemu/qtest.h"
71e8a915 36#include "sysemu/reset.h"
1eeffbeb 37#include "hw/core/split-irq.h"
ce6e1e9e 38
5852c1f8
SH
39#include <libfdt.h>
40
ce6e1e9e
JL
41#define KERNEL_LOAD_ADDR 0x100
42
f42e09e6 43#define OR1KSIM_CPUS_MAX 4
5852c1f8 44#define OR1KSIM_CLK_MHZ 20000000
f42e09e6 45
f85ad231
SH
46#define TYPE_OR1KSIM_MACHINE MACHINE_TYPE_NAME("or1k-sim")
47#define OR1KSIM_MACHINE(obj) \
48 OBJECT_CHECK(Or1ksimState, (obj), TYPE_OR1KSIM_MACHINE)
49
50typedef struct Or1ksimState {
51 /*< private >*/
52 MachineState parent_obj;
53
54 /*< public >*/
5852c1f8
SH
55 void *fdt;
56 int fdt_size;
f85ad231
SH
57
58} Or1ksimState;
59
76f36985
SH
60enum {
61 OR1KSIM_DRAM,
62 OR1KSIM_UART,
63 OR1KSIM_ETHOC,
64 OR1KSIM_OMPIC,
65};
66
67enum {
68 OR1KSIM_OMPIC_IRQ = 1,
69 OR1KSIM_UART_IRQ = 2,
70 OR1KSIM_ETHOC_IRQ = 4,
71};
72
777784bd
JD
73enum {
74 OR1KSIM_UART_COUNT = 4
75};
76
76f36985
SH
77static const struct MemmapEntry {
78 hwaddr base;
79 hwaddr size;
80} or1ksim_memmap[] = {
81 [OR1KSIM_DRAM] = { 0x00000000, 0 },
82 [OR1KSIM_UART] = { 0x90000000, 0x100 },
83 [OR1KSIM_ETHOC] = { 0x92000000, 0x800 },
a92162f4 84 [OR1KSIM_OMPIC] = { 0x98000000, OR1KSIM_CPUS_MAX * 8 },
76f36985
SH
85};
86
13f1c773
SH
87static struct openrisc_boot_info {
88 uint32_t bootstrap_pc;
5852c1f8 89 uint32_t fdt_addr;
13f1c773
SH
90} boot_info;
91
ce6e1e9e
JL
92static void main_cpu_reset(void *opaque)
93{
94 OpenRISCCPU *cpu = opaque;
13f1c773 95 CPUState *cs = CPU(cpu);
ce6e1e9e
JL
96
97 cpu_reset(CPU(cpu));
13f1c773
SH
98
99 cpu_set_pc(cs, boot_info.bootstrap_pc);
5852c1f8 100 cpu_set_gpr(&cpu->env, 3, boot_info.fdt_addr);
ce6e1e9e
JL
101}
102
eaca43a0
PM
103static qemu_irq get_cpu_irq(OpenRISCCPU *cpus[], int cpunum, int irq_pin)
104{
71b3254d 105 return qdev_get_gpio_in_named(DEVICE(cpus[cpunum]), "IRQ", irq_pin);
eaca43a0
PM
106}
107
5852c1f8
SH
108static void openrisc_create_fdt(Or1ksimState *state,
109 const struct MemmapEntry *memmap,
110 int num_cpus, uint64_t mem_size,
111 const char *cmdline)
112{
113 void *fdt;
114 int cpu;
115 char *nodename;
116 int pic_ph;
117
118 fdt = state->fdt = create_device_tree(&state->fdt_size);
119 if (!fdt) {
120 error_report("create_device_tree() failed");
121 exit(1);
122 }
123
124 qemu_fdt_setprop_string(fdt, "/", "compatible", "opencores,or1ksim");
125 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x1);
126 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x1);
127
128 nodename = g_strdup_printf("/memory@%" HWADDR_PRIx,
129 memmap[OR1KSIM_DRAM].base);
130 qemu_fdt_add_subnode(fdt, nodename);
131 qemu_fdt_setprop_cells(fdt, nodename, "reg",
132 memmap[OR1KSIM_DRAM].base, mem_size);
133 qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
134 g_free(nodename);
135
136 qemu_fdt_add_subnode(fdt, "/cpus");
137 qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
138 qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
139
140 for (cpu = 0; cpu < num_cpus; cpu++) {
141 nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
142 qemu_fdt_add_subnode(fdt, nodename);
143 qemu_fdt_setprop_string(fdt, nodename, "compatible",
144 "opencores,or1200-rtlsvn481");
145 qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
146 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
147 OR1KSIM_CLK_MHZ);
148 g_free(nodename);
149 }
150
151 nodename = (char *)"/pic";
152 qemu_fdt_add_subnode(fdt, nodename);
153 pic_ph = qemu_fdt_alloc_phandle(fdt);
154 qemu_fdt_setprop_string(fdt, nodename, "compatible",
155 "opencores,or1k-pic-level");
156 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
157 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
158 qemu_fdt_setprop_cell(fdt, nodename, "phandle", pic_ph);
159
160 qemu_fdt_setprop_cell(fdt, "/", "interrupt-parent", pic_ph);
161
162 qemu_fdt_add_subnode(fdt, "/chosen");
163 if (cmdline) {
164 qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
165 }
166
167 /* Create aliases node for use by devices. */
168 qemu_fdt_add_subnode(fdt, "/aliases");
169}
170
171static void openrisc_sim_net_init(Or1ksimState *state, hwaddr base, hwaddr size,
eaca43a0 172 int num_cpus, OpenRISCCPU *cpus[],
13f1c773 173 int irq_pin, NICInfo *nd)
ce6e1e9e 174{
5852c1f8 175 void *fdt = state->fdt;
ce6e1e9e
JL
176 DeviceState *dev;
177 SysBusDevice *s;
5852c1f8 178 char *nodename;
13f1c773 179 int i;
ce6e1e9e 180
3e80f690 181 dev = qdev_new("open_eth");
ce6e1e9e 182 qdev_set_nic_properties(dev, nd);
ce6e1e9e 183
1356b98d 184 s = SYS_BUS_DEVICE(dev);
3c6ef471 185 sysbus_realize_and_unref(s, &error_fatal);
1eeffbeb
PM
186 if (num_cpus > 1) {
187 DeviceState *splitter = qdev_new(TYPE_SPLIT_IRQ);
188 qdev_prop_set_uint32(splitter, "num-lines", num_cpus);
189 qdev_realize_and_unref(splitter, NULL, &error_fatal);
190 for (i = 0; i < num_cpus; i++) {
eaca43a0 191 qdev_connect_gpio_out(splitter, i, get_cpu_irq(cpus, i, irq_pin));
1eeffbeb
PM
192 }
193 sysbus_connect_irq(s, 0, qdev_get_gpio_in(splitter, 0));
194 } else {
eaca43a0 195 sysbus_connect_irq(s, 0, get_cpu_irq(cpus, 0, irq_pin));
13f1c773
SH
196 }
197 sysbus_mmio_map(s, 0, base);
5852c1f8
SH
198 sysbus_mmio_map(s, 1, base + 0x400);
199
200 /* Init device tree node for ethoc. */
201 nodename = g_strdup_printf("/ethoc@%" HWADDR_PRIx, base);
202 qemu_fdt_add_subnode(fdt, nodename);
203 qemu_fdt_setprop_string(fdt, nodename, "compatible", "opencores,ethoc");
204 qemu_fdt_setprop_cells(fdt, nodename, "reg", base, size);
205 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", irq_pin);
206 qemu_fdt_setprop(fdt, nodename, "big-endian", NULL, 0);
207
208 qemu_fdt_setprop_string(fdt, "/aliases", "enet0", nodename);
209 g_free(nodename);
13f1c773
SH
210}
211
5852c1f8
SH
212static void openrisc_sim_ompic_init(Or1ksimState *state, hwaddr base,
213 hwaddr size, int num_cpus,
eaca43a0 214 OpenRISCCPU *cpus[], int irq_pin)
13f1c773 215{
5852c1f8 216 void *fdt = state->fdt;
13f1c773
SH
217 DeviceState *dev;
218 SysBusDevice *s;
5852c1f8 219 char *nodename;
13f1c773
SH
220 int i;
221
3e80f690 222 dev = qdev_new("or1k-ompic");
13f1c773 223 qdev_prop_set_uint32(dev, "num-cpus", num_cpus);
13f1c773
SH
224
225 s = SYS_BUS_DEVICE(dev);
3c6ef471 226 sysbus_realize_and_unref(s, &error_fatal);
13f1c773 227 for (i = 0; i < num_cpus; i++) {
eaca43a0 228 sysbus_connect_irq(s, i, get_cpu_irq(cpus, i, irq_pin));
13f1c773
SH
229 }
230 sysbus_mmio_map(s, 0, base);
5852c1f8
SH
231
232 /* Add device tree node for ompic. */
233 nodename = g_strdup_printf("/ompic@%" HWADDR_PRIx, base);
234 qemu_fdt_add_subnode(fdt, nodename);
235 qemu_fdt_setprop_string(fdt, nodename, "compatible", "openrisc,ompic");
236 qemu_fdt_setprop_cells(fdt, nodename, "reg", base, size);
237 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
238 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 0);
239 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", irq_pin);
240 g_free(nodename);
ce6e1e9e
JL
241}
242
5852c1f8
SH
243static void openrisc_sim_serial_init(Or1ksimState *state, hwaddr base,
244 hwaddr size, int num_cpus,
777784bd
JD
245 OpenRISCCPU *cpus[], int irq_pin,
246 int uart_idx)
22991cfb 247{
5852c1f8
SH
248 void *fdt = state->fdt;
249 char *nodename;
22991cfb 250 qemu_irq serial_irq;
777784bd 251 char alias[sizeof("uart0")];
22991cfb
SH
252 int i;
253
254 if (num_cpus > 1) {
255 DeviceState *splitter = qdev_new(TYPE_SPLIT_IRQ);
256 qdev_prop_set_uint32(splitter, "num-lines", num_cpus);
257 qdev_realize_and_unref(splitter, NULL, &error_fatal);
258 for (i = 0; i < num_cpus; i++) {
259 qdev_connect_gpio_out(splitter, i, get_cpu_irq(cpus, i, irq_pin));
260 }
261 serial_irq = qdev_get_gpio_in(splitter, 0);
262 } else {
263 serial_irq = get_cpu_irq(cpus, 0, irq_pin);
264 }
265 serial_mm_init(get_system_memory(), base, 0, serial_irq, 115200,
777784bd
JD
266 serial_hd(OR1KSIM_UART_COUNT - uart_idx - 1),
267 DEVICE_NATIVE_ENDIAN);
22991cfb 268
5852c1f8
SH
269 /* Add device tree node for serial. */
270 nodename = g_strdup_printf("/serial@%" HWADDR_PRIx, base);
271 qemu_fdt_add_subnode(fdt, nodename);
272 qemu_fdt_setprop_string(fdt, nodename, "compatible", "ns16550a");
273 qemu_fdt_setprop_cells(fdt, nodename, "reg", base, size);
274 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", irq_pin);
275 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", OR1KSIM_CLK_MHZ);
276 qemu_fdt_setprop(fdt, nodename, "big-endian", NULL, 0);
277
278 /* The /chosen node is created during fdt creation. */
279 qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
777784bd
JD
280 snprintf(alias, sizeof(alias), "uart%d", uart_idx);
281 qemu_fdt_setprop_string(fdt, "/aliases", alias, nodename);
5852c1f8
SH
282 g_free(nodename);
283}
22991cfb 284
3ef96221 285static void openrisc_sim_init(MachineState *machine)
ce6e1e9e 286{
3ef96221 287 ram_addr_t ram_size = machine->ram_size;
3ef96221 288 const char *kernel_filename = machine->kernel_filename;
f42e09e6 289 OpenRISCCPU *cpus[OR1KSIM_CPUS_MAX] = {};
5852c1f8 290 Or1ksimState *state = OR1KSIM_MACHINE(machine);
ce6e1e9e 291 MemoryRegion *ram;
5852c1f8 292 hwaddr load_addr;
ce6e1e9e 293 int n;
33decbd2 294 unsigned int smp_cpus = machine->smp.cpus;
ce6e1e9e 295
f42e09e6 296 assert(smp_cpus >= 1 && smp_cpus <= OR1KSIM_CPUS_MAX);
ce6e1e9e 297 for (n = 0; n < smp_cpus; n++) {
eaca43a0
PM
298 cpus[n] = OPENRISC_CPU(cpu_create(machine->cpu_type));
299 if (cpus[n] == NULL) {
13f1c773
SH
300 fprintf(stderr, "Unable to find CPU definition!\n");
301 exit(1);
302 }
13f1c773 303
eaca43a0 304 cpu_openrisc_clock_init(cpus[n]);
13f1c773 305
eaca43a0 306 qemu_register_reset(main_cpu_reset, cpus[n]);
ce6e1e9e
JL
307 }
308
309 ram = g_malloc(sizeof(*ram));
98a99ce0 310 memory_region_init_ram(ram, NULL, "openrisc.ram", ram_size, &error_fatal);
ce6e1e9e
JL
311 memory_region_add_subregion(get_system_memory(), 0, ram);
312
5852c1f8
SH
313 openrisc_create_fdt(state, or1ksim_memmap, smp_cpus, machine->ram_size,
314 machine->kernel_cmdline);
315
13f1c773 316 if (nd_table[0].used) {
5852c1f8
SH
317 openrisc_sim_net_init(state, or1ksim_memmap[OR1KSIM_ETHOC].base,
318 or1ksim_memmap[OR1KSIM_ETHOC].size,
76f36985
SH
319 smp_cpus, cpus,
320 OR1KSIM_ETHOC_IRQ, nd_table);
13f1c773 321 }
ce6e1e9e 322
13f1c773 323 if (smp_cpus > 1) {
5852c1f8 324 openrisc_sim_ompic_init(state, or1ksim_memmap[OR1KSIM_OMPIC].base,
a92162f4 325 or1ksim_memmap[OR1KSIM_OMPIC].size,
5852c1f8 326 smp_cpus, cpus, OR1KSIM_OMPIC_IRQ);
ce6e1e9e
JL
327 }
328
777784bd
JD
329 for (n = 0; n < OR1KSIM_UART_COUNT; ++n)
330 openrisc_sim_serial_init(state, or1ksim_memmap[OR1KSIM_UART].base +
331 or1ksim_memmap[OR1KSIM_UART].size * n,
332 or1ksim_memmap[OR1KSIM_UART].size,
333 smp_cpus, cpus, OR1KSIM_UART_IRQ, n);
13f1c773 334
7025114b
SH
335 load_addr = openrisc_load_kernel(ram_size, kernel_filename,
336 &boot_info.bootstrap_pc);
5852c1f8 337 if (load_addr > 0) {
9576abf2 338 if (machine->initrd_filename) {
7025114b
SH
339 load_addr = openrisc_load_initrd(state->fdt,
340 machine->initrd_filename,
9576abf2
SH
341 load_addr, machine->ram_size);
342 }
7025114b 343 boot_info.fdt_addr = openrisc_load_fdt(state->fdt, load_addr,
5852c1f8
SH
344 machine->ram_size);
345 }
ce6e1e9e
JL
346}
347
f85ad231 348static void openrisc_sim_machine_init(ObjectClass *oc, void *data)
ce6e1e9e 349{
f85ad231
SH
350 MachineClass *mc = MACHINE_CLASS(oc);
351
4a09d0bb 352 mc->desc = "or1k simulation";
e264d29d 353 mc->init = openrisc_sim_init;
f42e09e6 354 mc->max_cpus = OR1KSIM_CPUS_MAX;
ea0ac7f6 355 mc->is_default = true;
1498e970 356 mc->default_cpu_type = OPENRISC_CPU_TYPE_NAME("or1200");
ce6e1e9e
JL
357}
358
f85ad231
SH
359static const TypeInfo or1ksim_machine_typeinfo = {
360 .name = TYPE_OR1KSIM_MACHINE,
361 .parent = TYPE_MACHINE,
362 .class_init = openrisc_sim_machine_init,
363 .instance_size = sizeof(Or1ksimState),
364};
365
366static void or1ksim_machine_init_register_types(void)
367{
368 type_register_static(&or1ksim_machine_typeinfo);
369}
370
371type_init(or1ksim_machine_init_register_types)
This page took 0.5403 seconds and 5 git commands to generate.