blob: 7f5dcd6d85ff52e4d08f6ee54db1d5db53b39905 [file] [log] [blame]
Fabien Chouteaub04d9892011-01-24 12:56:55 +01001/*
2 * QEMU Leon3 System Emulator
3 *
4 * Copyright (c) 2010-2011 AdaCore
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010024#include "hw/hw.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/timer.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010026#include "hw/ptimer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020027#include "sysemu/char.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/sysemu.h"
Andreas Färber77612542013-08-04 17:58:58 +020029#include "sysemu/qtest.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010030#include "hw/boards.h"
31#include "hw/loader.h"
Fabien Chouteaub04d9892011-01-24 12:56:55 +010032#include "elf.h"
33#include "trace.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/address-spaces.h"
Fabien Chouteaub04d9892011-01-24 12:56:55 +010035
Paolo Bonzini0d09e412013-02-05 17:06:20 +010036#include "hw/sparc/grlib.h"
Fabien Chouteaub04d9892011-01-24 12:56:55 +010037
38/* Default system clock. */
39#define CPU_CLK (40 * 1000 * 1000)
40
41#define PROM_FILENAME "u-boot.bin"
42
43#define MAX_PILS 16
44
45typedef struct ResetData {
Andreas Färberc537d792012-05-04 02:36:04 +020046 SPARCCPU *cpu;
Fabien Chouteaub04d9892011-01-24 12:56:55 +010047 uint32_t entry; /* save kernel entry in case of reset */
Sebastian Huberc1570e22014-02-03 10:18:11 +010048 target_ulong sp; /* initial stack pointer */
Fabien Chouteaub04d9892011-01-24 12:56:55 +010049} ResetData;
50
51static void main_cpu_reset(void *opaque)
52{
53 ResetData *s = (ResetData *)opaque;
Andreas Färber259186a2013-01-17 18:51:17 +010054 CPUState *cpu = CPU(s->cpu);
Andreas Färberc537d792012-05-04 02:36:04 +020055 CPUSPARCState *env = &s->cpu->env;
Fabien Chouteaub04d9892011-01-24 12:56:55 +010056
Andreas Färber259186a2013-01-17 18:51:17 +010057 cpu_reset(cpu);
Fabien Chouteaub04d9892011-01-24 12:56:55 +010058
Andreas Färber259186a2013-01-17 18:51:17 +010059 cpu->halted = 0;
Fabien Chouteaub04d9892011-01-24 12:56:55 +010060 env->pc = s->entry;
61 env->npc = s->entry + 4;
Sebastian Huberc1570e22014-02-03 10:18:11 +010062 env->regbase[6] = s->sp;
Fabien Chouteaub04d9892011-01-24 12:56:55 +010063}
64
Fabien Chouteau60f356e2011-01-31 11:36:54 +010065void leon3_irq_ack(void *irq_manager, int intno)
Fabien Chouteaub04d9892011-01-24 12:56:55 +010066{
67 grlib_irqmp_ack((DeviceState *)irq_manager, intno);
Fabien Chouteaub04d9892011-01-24 12:56:55 +010068}
69
70static void leon3_set_pil_in(void *opaque, uint32_t pil_in)
71{
Andreas Färber98cec4a2012-03-14 01:38:24 +010072 CPUSPARCState *env = (CPUSPARCState *)opaque;
Andreas Färberd8ed8872013-01-17 22:30:20 +010073 CPUState *cs;
Fabien Chouteaub04d9892011-01-24 12:56:55 +010074
75 assert(env != NULL);
76
77 env->pil_in = pil_in;
78
79 if (env->pil_in && (env->interrupt_index == 0 ||
80 (env->interrupt_index & ~15) == TT_EXTINT)) {
81 unsigned int i;
82
83 for (i = 15; i > 0; i--) {
84 if (env->pil_in & (1 << i)) {
85 int old_interrupt = env->interrupt_index;
86
87 env->interrupt_index = TT_EXTINT | i;
88 if (old_interrupt != env->interrupt_index) {
Andreas Färberc3affe52013-01-18 15:03:43 +010089 cs = CPU(sparc_env_get_cpu(env));
Fabien Chouteaub04d9892011-01-24 12:56:55 +010090 trace_leon3_set_irq(i);
Andreas Färberc3affe52013-01-18 15:03:43 +010091 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
Fabien Chouteaub04d9892011-01-24 12:56:55 +010092 }
93 break;
94 }
95 }
96 } else if (!env->pil_in && (env->interrupt_index & ~15) == TT_EXTINT) {
Andreas Färberd8ed8872013-01-17 22:30:20 +010097 cs = CPU(sparc_env_get_cpu(env));
Fabien Chouteaub04d9892011-01-24 12:56:55 +010098 trace_leon3_reset_irq(env->interrupt_index & 15);
99 env->interrupt_index = 0;
Andreas Färberd8ed8872013-01-17 22:30:20 +0100100 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100101 }
102}
103
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300104static void leon3_generic_hw_init(MachineState *machine)
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100105{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300106 ram_addr_t ram_size = machine->ram_size;
107 const char *cpu_model = machine->cpu_model;
108 const char *kernel_filename = machine->kernel_filename;
Andreas Färber60ad0732012-05-04 02:33:34 +0200109 SPARCCPU *cpu;
Andreas Färber98cec4a2012-03-14 01:38:24 +0100110 CPUSPARCState *env;
Avi Kivitya4911d62011-07-25 15:07:22 +0300111 MemoryRegion *address_space_mem = get_system_memory();
112 MemoryRegion *ram = g_new(MemoryRegion, 1);
113 MemoryRegion *prom = g_new(MemoryRegion, 1);
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100114 int ret;
115 char *filename;
116 qemu_irq *cpu_irqs = NULL;
117 int bios_size;
118 int prom_size;
119 ResetData *reset_info;
120
121 /* Init CPU */
122 if (!cpu_model) {
123 cpu_model = "LEON3";
124 }
125
Andreas Färber60ad0732012-05-04 02:33:34 +0200126 cpu = cpu_sparc_init(cpu_model);
127 if (cpu == NULL) {
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100128 fprintf(stderr, "qemu: Unable to find Sparc CPU definition\n");
129 exit(1);
130 }
Andreas Färber60ad0732012-05-04 02:33:34 +0200131 env = &cpu->env;
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100132
133 cpu_sparc_set_id(env, 0);
134
135 /* Reset data */
Anthony Liguori7267c092011-08-20 22:09:37 -0500136 reset_info = g_malloc0(sizeof(ResetData));
Andreas Färberc537d792012-05-04 02:36:04 +0200137 reset_info->cpu = cpu;
Sebastian Huberc1570e22014-02-03 10:18:11 +0100138 reset_info->sp = 0x40000000 + ram_size;
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100139 qemu_register_reset(main_cpu_reset, reset_info);
140
141 /* Allocate IRQ manager */
142 grlib_irqmp_create(0x80000200, env, &cpu_irqs, MAX_PILS, &leon3_set_pil_in);
143
Fabien Chouteau60f356e2011-01-31 11:36:54 +0100144 env->qemu_irq_ack = leon3_irq_manager;
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100145
146 /* Allocate RAM */
147 if ((uint64_t)ram_size > (1UL << 30)) {
148 fprintf(stderr,
149 "qemu: Too much memory for this machine: %d, maximum 1G\n",
150 (unsigned int)(ram_size / (1024 * 1024)));
151 exit(1);
152 }
153
Dirk Müller8e7ba4e2015-03-24 22:30:13 +0100154 memory_region_allocate_system_memory(ram, NULL, "leon3.ram", ram_size);
Avi Kivitya4911d62011-07-25 15:07:22 +0300155 memory_region_add_subregion(address_space_mem, 0x40000000, ram);
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100156
157 /* Allocate BIOS */
158 prom_size = 8 * 1024 * 1024; /* 8Mb */
Hu Tao49946532014-09-09 13:27:55 +0800159 memory_region_init_ram(prom, NULL, "Leon3.bios", prom_size, &error_abort);
Avi Kivityc5705a72011-12-20 15:59:12 +0200160 vmstate_register_ram_global(prom);
Avi Kivitya4911d62011-07-25 15:07:22 +0300161 memory_region_set_readonly(prom, true);
162 memory_region_add_subregion(address_space_mem, 0x00000000, prom);
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100163
164 /* Load boot prom */
165 if (bios_name == NULL) {
166 bios_name = PROM_FILENAME;
167 }
168 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
169
170 bios_size = get_image_size(filename);
171
172 if (bios_size > prom_size) {
173 fprintf(stderr, "qemu: could not load prom '%s': file too big\n",
174 filename);
175 exit(1);
176 }
177
178 if (bios_size > 0) {
179 ret = load_image_targphys(filename, 0x00000000, bios_size);
180 if (ret < 0 || ret > prom_size) {
181 fprintf(stderr, "qemu: could not load prom '%s'\n", filename);
182 exit(1);
183 }
Andreas Färber77612542013-08-04 17:58:58 +0200184 } else if (kernel_filename == NULL && !qtest_enabled()) {
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100185 fprintf(stderr, "Can't read bios image %s\n", filename);
186 exit(1);
187 }
Gongleid71cdbf2015-02-27 15:50:13 +0800188 g_free(filename);
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100189
190 /* Can directly load an application. */
191 if (kernel_filename != NULL) {
192 long kernel_size;
193 uint64_t entry;
194
195 kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
196 1 /* big endian */, ELF_MACHINE, 0);
197 if (kernel_size < 0) {
198 fprintf(stderr, "qemu: could not load kernel '%s'\n",
199 kernel_filename);
200 exit(1);
201 }
202 if (bios_size <= 0) {
203 /* If there is no bios/monitor, start the application. */
204 env->pc = entry;
205 env->npc = entry + 4;
206 reset_info->entry = entry;
207 }
208 }
209
210 /* Allocate timers */
211 grlib_gptimer_create(0x80000300, 2, CPU_CLK, cpu_irqs, 6);
212
213 /* Allocate uart */
214 if (serial_hds[0]) {
215 grlib_apbuart_create(0x80000100, serial_hds[0], cpu_irqs[3]);
216 }
217}
218
Stefan Weilda665c92012-10-03 11:19:39 +0200219static QEMUMachine leon3_generic_machine = {
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100220 .name = "leon3_generic",
221 .desc = "Leon-3 generic",
222 .init = leon3_generic_hw_init,
Fabien Chouteaub04d9892011-01-24 12:56:55 +0100223};
224
225static void leon3_machine_init(void)
226{
227 qemu_register_machine(&leon3_generic_machine);
228}
229
230machine_init(leon3_machine_init);