xref: /openbmc/qemu/hw/sparc/leon3.c (revision 3d54cbf269d63ff1d500b35b2bcf4565ff8ad485)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * QEMU Leon3 System Emulator
353018216SPaolo Bonzini  *
4f432962eSClément Chigot  * SPDX-License-Identifier: MIT
5f432962eSClément Chigot  *
6f432962eSClément Chigot  * Copyright (c) 2010-2024 AdaCore
753018216SPaolo Bonzini  *
853018216SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
953018216SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
1053018216SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
1153018216SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1253018216SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
1353018216SPaolo Bonzini  * furnished to do so, subject to the following conditions:
1453018216SPaolo Bonzini  *
1553018216SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
1653018216SPaolo Bonzini  * all copies or substantial portions of the Software.
1753018216SPaolo Bonzini  *
1853018216SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1953018216SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2053018216SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2153018216SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2253018216SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2353018216SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2453018216SPaolo Bonzini  * THE SOFTWARE.
2553018216SPaolo Bonzini  */
2671e8a915SMarkus Armbruster 
27db5ebe5fSPeter Maydell #include "qemu/osdep.h"
280a2e467bSPhilippe Mathieu-Daudé #include "qemu/units.h"
2929bd7231SAlistair Francis #include "qemu/error-report.h"
30da34e65cSMarkus Armbruster #include "qapi/error.h"
312c65db5eSPaolo Bonzini #include "qemu/datadir.h"
324771d756SPaolo Bonzini #include "cpu.h"
3364552b6bSMarkus Armbruster #include "hw/irq.h"
3453018216SPaolo Bonzini #include "qemu/timer.h"
3553018216SPaolo Bonzini #include "hw/ptimer.h"
36a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
3753018216SPaolo Bonzini #include "sysemu/sysemu.h"
3877612541SAndreas Färber #include "sysemu/qtest.h"
3971e8a915SMarkus Armbruster #include "sysemu/reset.h"
4053018216SPaolo Bonzini #include "hw/boards.h"
4153018216SPaolo Bonzini #include "hw/loader.h"
4253018216SPaolo Bonzini #include "elf.h"
4353018216SPaolo Bonzini #include "trace.h"
4453018216SPaolo Bonzini 
45f432962eSClément Chigot #include "hw/timer/grlib_gptimer.h"
46f432962eSClément Chigot #include "hw/char/grlib_uart.h"
47f432962eSClément Chigot #include "hw/intc/grlib_irqmp.h"
48162abf1aSKONRAD Frederic #include "hw/misc/grlib_ahb_apb_pnp.h"
4953018216SPaolo Bonzini 
5053018216SPaolo Bonzini /* Default system clock.  */
5153018216SPaolo Bonzini #define CPU_CLK (40 * 1000 * 1000)
5253018216SPaolo Bonzini 
53bd30132cSKONRAD Frederic #define LEON3_PROM_FILENAME "u-boot.bin"
54dbed0d2dSKONRAD Frederic #define LEON3_PROM_OFFSET    (0x00000000)
55dbed0d2dSKONRAD Frederic #define LEON3_RAM_OFFSET     (0x40000000)
5653018216SPaolo Bonzini 
57d65aba82SClément Chigot #define MAX_CPUS  4
58d65aba82SClément Chigot 
59b70447aaSKONRAD Frederic #define LEON3_UART_OFFSET  (0x80000100)
60b70447aaSKONRAD Frederic #define LEON3_UART_IRQ     (3)
61b70447aaSKONRAD Frederic 
62ea005daeSKONRAD Frederic #define LEON3_IRQMP_OFFSET (0x80000200)
63ea005daeSKONRAD Frederic 
64948caec8SKONRAD Frederic #define LEON3_TIMER_OFFSET (0x80000300)
65948caec8SKONRAD Frederic #define LEON3_TIMER_IRQ    (6)
66948caec8SKONRAD Frederic #define LEON3_TIMER_COUNT  (2)
67948caec8SKONRAD Frederic 
68162abf1aSKONRAD Frederic #define LEON3_APB_PNP_OFFSET (0x800FF000)
69162abf1aSKONRAD Frederic #define LEON3_AHB_PNP_OFFSET (0xFFFFF000)
70162abf1aSKONRAD Frederic 
7153018216SPaolo Bonzini typedef struct ResetData {
72d65aba82SClément Chigot     struct CPUResetData {
73d65aba82SClément Chigot         int id;
7453018216SPaolo Bonzini         SPARCCPU *cpu;
75d65aba82SClément Chigot     } info[MAX_CPUS];
7653018216SPaolo Bonzini     uint32_t entry;             /* save kernel entry in case of reset */
7753018216SPaolo Bonzini } ResetData;
7853018216SPaolo Bonzini 
gen_store_u32(uint32_t * code,hwaddr addr,uint32_t val)79dbed0d2dSKONRAD Frederic static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
80dbed0d2dSKONRAD Frederic {
81dbed0d2dSKONRAD Frederic     stl_p(code++, 0x82100000); /* mov %g0, %g1                */
82dbed0d2dSKONRAD Frederic     stl_p(code++, 0x84100000); /* mov %g0, %g2                */
83dbed0d2dSKONRAD Frederic     stl_p(code++, 0x03000000 +
84dbed0d2dSKONRAD Frederic       extract32(addr, 10, 22));
85dbed0d2dSKONRAD Frederic                                /* sethi %hi(addr), %g1        */
86dbed0d2dSKONRAD Frederic     stl_p(code++, 0x82106000 +
87dbed0d2dSKONRAD Frederic       extract32(addr, 0, 10));
88dbed0d2dSKONRAD Frederic                                /* or %g1, addr, %g1           */
89dbed0d2dSKONRAD Frederic     stl_p(code++, 0x05000000 +
90dbed0d2dSKONRAD Frederic       extract32(val, 10, 22));
91dbed0d2dSKONRAD Frederic                                /* sethi %hi(val), %g2         */
92dbed0d2dSKONRAD Frederic     stl_p(code++, 0x8410a000 +
93dbed0d2dSKONRAD Frederic       extract32(val, 0, 10));
94dbed0d2dSKONRAD Frederic                                /* or %g2, val, %g2            */
95dbed0d2dSKONRAD Frederic     stl_p(code++, 0xc4204000); /* st %g2, [ %g1 ]             */
96dbed0d2dSKONRAD Frederic 
97dbed0d2dSKONRAD Frederic     return code;
98dbed0d2dSKONRAD Frederic }
99dbed0d2dSKONRAD Frederic 
100dbed0d2dSKONRAD Frederic /*
101dbed0d2dSKONRAD Frederic  * When loading a kernel in RAM the machine is expected to be in a different
102dbed0d2dSKONRAD Frederic  * state (eg: initialized by the bootloader).  This little code reproduces
10356bd9678SClément Chigot  * this behavior.  Also this code can be executed by the secondary cpus as
10456bd9678SClément Chigot  * well since it looks at the %asr17 register before doing any
10556bd9678SClément Chigot  * initialization, it allows to use the same reset address for all the
10656bd9678SClément Chigot  * cpus.
107dbed0d2dSKONRAD Frederic  */
write_bootloader(void * ptr,hwaddr kernel_addr)10804752105SPhilippe Mathieu-Daudé static void write_bootloader(void *ptr, hwaddr kernel_addr)
109dbed0d2dSKONRAD Frederic {
11004752105SPhilippe Mathieu-Daudé     uint32_t *p = ptr;
11156bd9678SClément Chigot     uint32_t *sec_cpu_branch_p = NULL;
11256bd9678SClément Chigot 
11356bd9678SClément Chigot     /* If we are running on a secondary CPU, jump directly to the kernel.  */
11456bd9678SClément Chigot 
11556bd9678SClément Chigot     stl_p(p++, 0x85444000); /* rd %asr17, %g2      */
11656bd9678SClément Chigot     stl_p(p++, 0x8530a01c); /* srl  %g2, 0x1c, %g2 */
11756bd9678SClément Chigot     stl_p(p++, 0x80908000); /* tst  %g2            */
11856bd9678SClément Chigot     /* Filled below.  */
11956bd9678SClément Chigot     sec_cpu_branch_p = p;
12056bd9678SClément Chigot     stl_p(p++, 0x0BADC0DE); /* bne xxx             */
12156bd9678SClément Chigot     stl_p(p++, 0x01000000); /* nop */
122dbed0d2dSKONRAD Frederic 
123dbed0d2dSKONRAD Frederic     /* Initialize the UARTs                                        */
124dbed0d2dSKONRAD Frederic     /* *UART_CONTROL = UART_RECEIVE_ENABLE | UART_TRANSMIT_ENABLE; */
125dbed0d2dSKONRAD Frederic     p = gen_store_u32(p, 0x80000108, 3);
126dbed0d2dSKONRAD Frederic 
127dbed0d2dSKONRAD Frederic     /* Initialize the TIMER 0                                      */
128dbed0d2dSKONRAD Frederic     /* *GPTIMER_SCALER_RELOAD = 40 - 1;                            */
129dbed0d2dSKONRAD Frederic     p = gen_store_u32(p, 0x80000304, 39);
130dbed0d2dSKONRAD Frederic     /* *GPTIMER0_COUNTER_RELOAD = 0xFFFE;                          */
131dbed0d2dSKONRAD Frederic     p = gen_store_u32(p, 0x80000314, 0xFFFFFFFE);
132dbed0d2dSKONRAD Frederic     /* *GPTIMER0_CONFIG = GPTIMER_ENABLE | GPTIMER_RESTART;        */
133dbed0d2dSKONRAD Frederic     p = gen_store_u32(p, 0x80000318, 3);
134dbed0d2dSKONRAD Frederic 
13556bd9678SClément Chigot     /* Now, the relative branch above can be computed.  */
13656bd9678SClément Chigot     stl_p(sec_cpu_branch_p, 0x12800000
13756bd9678SClément Chigot           + (p - sec_cpu_branch_p));
13856bd9678SClément Chigot 
139dbed0d2dSKONRAD Frederic     /* JUMP to the entry point                                     */
140dbed0d2dSKONRAD Frederic     stl_p(p++, 0x82100000); /* mov %g0, %g1 */
141dbed0d2dSKONRAD Frederic     stl_p(p++, 0x03000000 + extract32(kernel_addr, 10, 22));
142dbed0d2dSKONRAD Frederic                             /* sethi %hi(kernel_addr), %g1 */
143dbed0d2dSKONRAD Frederic     stl_p(p++, 0x82106000 + extract32(kernel_addr, 0, 10));
144dbed0d2dSKONRAD Frederic                             /* or kernel_addr, %g1 */
145dbed0d2dSKONRAD Frederic     stl_p(p++, 0x81c04000); /* jmp  %g1 */
146dbed0d2dSKONRAD Frederic     stl_p(p++, 0x01000000); /* nop */
147dbed0d2dSKONRAD Frederic }
148dbed0d2dSKONRAD Frederic 
leon3_cpu_reset(void * opaque)149d65aba82SClément Chigot static void leon3_cpu_reset(void *opaque)
15053018216SPaolo Bonzini {
151d65aba82SClément Chigot     struct CPUResetData *info = (struct CPUResetData *) opaque;
152d65aba82SClément Chigot     int id = info->id;
153*7164f7e4SThomas Huth     ResetData *s = container_of(info, ResetData, info[id]);
154d65aba82SClément Chigot     CPUState *cpu = CPU(s->info[id].cpu);
155d65aba82SClément Chigot     CPUSPARCState *env = cpu_env(cpu);
15653018216SPaolo Bonzini 
157259186a7SAndreas Färber     cpu_reset(cpu);
15853018216SPaolo Bonzini 
159d65aba82SClément Chigot     cpu->halted = cpu->cpu_index != 0;
16053018216SPaolo Bonzini     env->pc = s->entry;
16153018216SPaolo Bonzini     env->npc = s->entry + 4;
16253018216SPaolo Bonzini }
16353018216SPaolo Bonzini 
leon3_cache_control_int(CPUSPARCState * env)16412841199SPhilippe Mathieu-Daudé static void leon3_cache_control_int(CPUSPARCState *env)
16512841199SPhilippe Mathieu-Daudé {
16612841199SPhilippe Mathieu-Daudé     uint32_t state = 0;
16712841199SPhilippe Mathieu-Daudé 
16812841199SPhilippe Mathieu-Daudé     if (env->cache_control & CACHE_CTRL_IF) {
16912841199SPhilippe Mathieu-Daudé         /* Instruction cache state */
17012841199SPhilippe Mathieu-Daudé         state = env->cache_control & CACHE_STATE_MASK;
17112841199SPhilippe Mathieu-Daudé         if (state == CACHE_ENABLED) {
17212841199SPhilippe Mathieu-Daudé             state = CACHE_FROZEN;
17312841199SPhilippe Mathieu-Daudé             trace_int_helper_icache_freeze();
17412841199SPhilippe Mathieu-Daudé         }
17512841199SPhilippe Mathieu-Daudé 
17612841199SPhilippe Mathieu-Daudé         env->cache_control &= ~CACHE_STATE_MASK;
17712841199SPhilippe Mathieu-Daudé         env->cache_control |= state;
17812841199SPhilippe Mathieu-Daudé     }
17912841199SPhilippe Mathieu-Daudé 
18012841199SPhilippe Mathieu-Daudé     if (env->cache_control & CACHE_CTRL_DF) {
18112841199SPhilippe Mathieu-Daudé         /* Data cache state */
18212841199SPhilippe Mathieu-Daudé         state = (env->cache_control >> 2) & CACHE_STATE_MASK;
18312841199SPhilippe Mathieu-Daudé         if (state == CACHE_ENABLED) {
18412841199SPhilippe Mathieu-Daudé             state = CACHE_FROZEN;
18512841199SPhilippe Mathieu-Daudé             trace_int_helper_dcache_freeze();
18612841199SPhilippe Mathieu-Daudé         }
18712841199SPhilippe Mathieu-Daudé 
18812841199SPhilippe Mathieu-Daudé         env->cache_control &= ~(CACHE_STATE_MASK << 2);
18912841199SPhilippe Mathieu-Daudé         env->cache_control |= (state << 2);
19012841199SPhilippe Mathieu-Daudé     }
19112841199SPhilippe Mathieu-Daudé }
19212841199SPhilippe Mathieu-Daudé 
leon3_irq_ack(CPUSPARCState * env,int intno)193a318da6bSClément Chigot static void leon3_irq_ack(CPUSPARCState *env, int intno)
19453018216SPaolo Bonzini {
195d65aba82SClément Chigot     CPUState *cpu = CPU(env_cpu(env));
196d65aba82SClément Chigot     grlib_irqmp_ack(env->irq_manager, cpu->cpu_index, intno);
19753018216SPaolo Bonzini }
19853018216SPaolo Bonzini 
199ab4c072dSMarc-André Lureau /*
200ab4c072dSMarc-André Lureau  * This device assumes that the incoming 'level' value on the
201ab4c072dSMarc-André Lureau  * qemu_irq is the interrupt number, not just a simple 0/1 level.
202ab4c072dSMarc-André Lureau  */
leon3_set_pil_in(void * opaque,int n,int level)203ab4c072dSMarc-André Lureau static void leon3_set_pil_in(void *opaque, int n, int level)
20453018216SPaolo Bonzini {
2055c7127eeSPhilippe Mathieu-Daudé     DeviceState *cpu = opaque;
2065c7127eeSPhilippe Mathieu-Daudé     CPUState *cs = CPU(cpu);
2075c7127eeSPhilippe Mathieu-Daudé     CPUSPARCState *env = cpu_env(cs);
208ab4c072dSMarc-André Lureau     uint32_t pil_in = level;
20953018216SPaolo Bonzini 
21053018216SPaolo Bonzini     assert(env != NULL);
21153018216SPaolo Bonzini 
21253018216SPaolo Bonzini     env->pil_in = pil_in;
21353018216SPaolo Bonzini 
21453018216SPaolo Bonzini     if (env->pil_in && (env->interrupt_index == 0 ||
21553018216SPaolo Bonzini                         (env->interrupt_index & ~15) == TT_EXTINT)) {
21653018216SPaolo Bonzini         unsigned int i;
21753018216SPaolo Bonzini 
21853018216SPaolo Bonzini         for (i = 15; i > 0; i--) {
21953018216SPaolo Bonzini             if (env->pil_in & (1 << i)) {
22053018216SPaolo Bonzini                 int old_interrupt = env->interrupt_index;
22153018216SPaolo Bonzini 
22253018216SPaolo Bonzini                 env->interrupt_index = TT_EXTINT | i;
22353018216SPaolo Bonzini                 if (old_interrupt != env->interrupt_index) {
22453018216SPaolo Bonzini                     trace_leon3_set_irq(i);
225c3affe56SAndreas Färber                     cpu_interrupt(cs, CPU_INTERRUPT_HARD);
22653018216SPaolo Bonzini                 }
22753018216SPaolo Bonzini                 break;
22853018216SPaolo Bonzini             }
22953018216SPaolo Bonzini         }
23053018216SPaolo Bonzini     } else if (!env->pil_in && (env->interrupt_index & ~15) == TT_EXTINT) {
23153018216SPaolo Bonzini         trace_leon3_reset_irq(env->interrupt_index & 15);
23253018216SPaolo Bonzini         env->interrupt_index = 0;
233d8ed887bSAndreas Färber         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
23453018216SPaolo Bonzini     }
23553018216SPaolo Bonzini }
23653018216SPaolo Bonzini 
leon3_start_cpu_async_work(CPUState * cpu,run_on_cpu_data data)237d65aba82SClément Chigot static void leon3_start_cpu_async_work(CPUState *cpu, run_on_cpu_data data)
238d65aba82SClément Chigot {
239d65aba82SClément Chigot     cpu->halted = 0;
240d65aba82SClément Chigot }
241d65aba82SClément Chigot 
leon3_start_cpu(void * opaque,int n,int level)242d65aba82SClément Chigot static void leon3_start_cpu(void *opaque, int n, int level)
243d65aba82SClément Chigot {
24408507399SPhilippe Mathieu-Daudé     DeviceState *cpu = opaque;
24508507399SPhilippe Mathieu-Daudé     CPUState *cs = CPU(cpu);
246d65aba82SClément Chigot 
247d65aba82SClément Chigot     assert(level == 1);
248d65aba82SClément Chigot     async_run_on_cpu(cs, leon3_start_cpu_async_work, RUN_ON_CPU_NULL);
249d65aba82SClément Chigot }
250d65aba82SClément Chigot 
leon3_irq_manager(CPUSPARCState * env,int intno)251a318da6bSClément Chigot static void leon3_irq_manager(CPUSPARCState *env, int intno)
25212841199SPhilippe Mathieu-Daudé {
253a318da6bSClément Chigot     leon3_irq_ack(env, intno);
25412841199SPhilippe Mathieu-Daudé     leon3_cache_control_int(env);
25512841199SPhilippe Mathieu-Daudé }
25612841199SPhilippe Mathieu-Daudé 
leon3_generic_hw_init(MachineState * machine)2573ef96221SMarcel Apfelbaum static void leon3_generic_hw_init(MachineState *machine)
25853018216SPaolo Bonzini {
2593ef96221SMarcel Apfelbaum     ram_addr_t ram_size = machine->ram_size;
260377ce9cbSPaolo Bonzini     const char *bios_name = machine->firmware ?: LEON3_PROM_FILENAME;
2613ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
26253018216SPaolo Bonzini     SPARCCPU *cpu;
26353018216SPaolo Bonzini     CPUSPARCState   *env;
26453018216SPaolo Bonzini     MemoryRegion *address_space_mem = get_system_memory();
26553018216SPaolo Bonzini     MemoryRegion *prom = g_new(MemoryRegion, 1);
26653018216SPaolo Bonzini     int         ret;
26753018216SPaolo Bonzini     char       *filename;
26853018216SPaolo Bonzini     int         bios_size;
26953018216SPaolo Bonzini     int         prom_size;
27053018216SPaolo Bonzini     ResetData  *reset_info;
27133919536SPeter Maydell     DeviceState *dev, *irqmpdev;
272948caec8SKONRAD Frederic     int i;
273162abf1aSKONRAD Frederic     AHBPnp *ahb_pnp;
274162abf1aSKONRAD Frederic     APBPnp *apb_pnp;
27553018216SPaolo Bonzini 
276d65aba82SClément Chigot     reset_info = g_malloc0(sizeof(ResetData));
277d65aba82SClément Chigot 
278d65aba82SClément Chigot     for (i = 0; i < machine->smp.cpus; i++) {
27953018216SPaolo Bonzini         /* Init CPU */
2800f0554c6SPhilippe Mathieu-Daudé         cpu = SPARC_CPU(object_new(machine->cpu_type));
2810f0554c6SPhilippe Mathieu-Daudé         qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu, "start_cpu", 1);
2820f0554c6SPhilippe Mathieu-Daudé         qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1);
2830f0554c6SPhilippe Mathieu-Daudé         qdev_realize(DEVICE(cpu), NULL, &error_fatal);
28453018216SPaolo Bonzini         env = &cpu->env;
28553018216SPaolo Bonzini 
286d65aba82SClément Chigot         cpu_sparc_set_id(env, i);
28753018216SPaolo Bonzini 
28853018216SPaolo Bonzini         /* Reset data */
289d65aba82SClément Chigot         reset_info->info[i].id = i;
290d65aba82SClément Chigot         reset_info->info[i].cpu = cpu;
291d65aba82SClément Chigot         qemu_register_reset(leon3_cpu_reset, &reset_info->info[i]);
292d65aba82SClément Chigot     }
29353018216SPaolo Bonzini 
294df707969SMarkus Armbruster     ahb_pnp = GRLIB_AHB_PNP(qdev_new(TYPE_GRLIB_AHB_PNP));
2953c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(ahb_pnp), &error_fatal);
296162abf1aSKONRAD Frederic     sysbus_mmio_map(SYS_BUS_DEVICE(ahb_pnp), 0, LEON3_AHB_PNP_OFFSET);
297162abf1aSKONRAD Frederic     grlib_ahb_pnp_add_entry(ahb_pnp, 0, 0, GRLIB_VENDOR_GAISLER,
298162abf1aSKONRAD Frederic                             GRLIB_LEON3_DEV, GRLIB_AHB_MASTER,
299162abf1aSKONRAD Frederic                             GRLIB_CPU_AREA);
300162abf1aSKONRAD Frederic 
301df707969SMarkus Armbruster     apb_pnp = GRLIB_APB_PNP(qdev_new(TYPE_GRLIB_APB_PNP));
3023c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(apb_pnp), &error_fatal);
303162abf1aSKONRAD Frederic     sysbus_mmio_map(SYS_BUS_DEVICE(apb_pnp), 0, LEON3_APB_PNP_OFFSET);
304162abf1aSKONRAD Frederic     grlib_ahb_pnp_add_entry(ahb_pnp, LEON3_APB_PNP_OFFSET, 0xFFF,
305162abf1aSKONRAD Frederic                             GRLIB_VENDOR_GAISLER, GRLIB_APBMST_DEV,
306162abf1aSKONRAD Frederic                             GRLIB_AHB_SLAVE, GRLIB_AHBMEM_AREA);
307162abf1aSKONRAD Frederic 
30853018216SPaolo Bonzini     /* Allocate IRQ manager */
30933919536SPeter Maydell     irqmpdev = qdev_new(TYPE_GRLIB_IRQMP);
310d65aba82SClément Chigot     object_property_set_int(OBJECT(irqmpdev), "ncpus", machine->smp.cpus,
311d65aba82SClément Chigot                             &error_fatal);
3126bf14785SClément Chigot     sysbus_realize_and_unref(SYS_BUS_DEVICE(irqmpdev), &error_fatal);
313d65aba82SClément Chigot 
314d65aba82SClément Chigot     for (i = 0; i < machine->smp.cpus; i++) {
315d65aba82SClément Chigot         cpu = reset_info->info[i].cpu;
316d65aba82SClément Chigot         env = &cpu->env;
317d65aba82SClément Chigot         qdev_connect_gpio_out_named(irqmpdev, "grlib-start-cpu", i,
318d65aba82SClément Chigot                                     qdev_get_gpio_in_named(DEVICE(cpu),
319d65aba82SClément Chigot                                                            "start_cpu", 0));
320d65aba82SClément Chigot         qdev_connect_gpio_out_named(irqmpdev, "grlib-irq", i,
321d65aba82SClément Chigot                                     qdev_get_gpio_in_named(DEVICE(cpu),
322d65aba82SClément Chigot                                                            "pil", 0));
32333919536SPeter Maydell         env->irq_manager = irqmpdev;
32453018216SPaolo Bonzini         env->qemu_irq_ack = leon3_irq_manager;
325d65aba82SClément Chigot     }
326d65aba82SClément Chigot 
327d65aba82SClément Chigot     sysbus_mmio_map(SYS_BUS_DEVICE(irqmpdev), 0, LEON3_IRQMP_OFFSET);
328162abf1aSKONRAD Frederic     grlib_apb_pnp_add_entry(apb_pnp, LEON3_IRQMP_OFFSET, 0xFFF,
329162abf1aSKONRAD Frederic                             GRLIB_VENDOR_GAISLER, GRLIB_IRQMP_DEV,
330162abf1aSKONRAD Frederic                             2, 0, GRLIB_APBIO_AREA);
33153018216SPaolo Bonzini 
33253018216SPaolo Bonzini     /* Allocate RAM */
3330a2e467bSPhilippe Mathieu-Daudé     if (ram_size > 1 * GiB) {
3340a2e467bSPhilippe Mathieu-Daudé         error_report("Too much memory for this machine: %" PRId64 "MB,"
3350a2e467bSPhilippe Mathieu-Daudé                      " maximum 1G",
3360a2e467bSPhilippe Mathieu-Daudé                      ram_size / MiB);
33753018216SPaolo Bonzini         exit(1);
33853018216SPaolo Bonzini     }
33953018216SPaolo Bonzini 
340fe3e7b71SIgor Mammedov     memory_region_add_subregion(address_space_mem, LEON3_RAM_OFFSET,
341fe3e7b71SIgor Mammedov                                 machine->ram);
34253018216SPaolo Bonzini 
34353018216SPaolo Bonzini     /* Allocate BIOS */
3440a2e467bSPhilippe Mathieu-Daudé     prom_size = 8 * MiB;
345ec7b2175SPhilippe Mathieu-Daudé     memory_region_init_rom(prom, NULL, "Leon3.bios", prom_size, &error_fatal);
346dbed0d2dSKONRAD Frederic     memory_region_add_subregion(address_space_mem, LEON3_PROM_OFFSET, prom);
34753018216SPaolo Bonzini 
34853018216SPaolo Bonzini     /* Load boot prom */
34953018216SPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
35053018216SPaolo Bonzini 
35147dc0ec5SPeter Maydell     if (filename) {
35253018216SPaolo Bonzini         bios_size = get_image_size(filename);
35347dc0ec5SPeter Maydell     } else {
35447dc0ec5SPeter Maydell         bios_size = -1;
35547dc0ec5SPeter Maydell     }
35653018216SPaolo Bonzini 
35753018216SPaolo Bonzini     if (bios_size > prom_size) {
35829bd7231SAlistair Francis         error_report("could not load prom '%s': file too big", filename);
35953018216SPaolo Bonzini         exit(1);
36053018216SPaolo Bonzini     }
36153018216SPaolo Bonzini 
36253018216SPaolo Bonzini     if (bios_size > 0) {
363dbed0d2dSKONRAD Frederic         ret = load_image_targphys(filename, LEON3_PROM_OFFSET, bios_size);
36453018216SPaolo Bonzini         if (ret < 0 || ret > prom_size) {
36529bd7231SAlistair Francis             error_report("could not load prom '%s'", filename);
36653018216SPaolo Bonzini             exit(1);
36753018216SPaolo Bonzini         }
36877612541SAndreas Färber     } else if (kernel_filename == NULL && !qtest_enabled()) {
369bd30132cSKONRAD Frederic         error_report("Can't read bios image '%s'", filename
370bd30132cSKONRAD Frederic                                                    ? filename
371bd30132cSKONRAD Frederic                                                    : LEON3_PROM_FILENAME);
37253018216SPaolo Bonzini         exit(1);
37353018216SPaolo Bonzini     }
374d71cdbfdSGonglei     g_free(filename);
37553018216SPaolo Bonzini 
37653018216SPaolo Bonzini     /* Can directly load an application. */
37753018216SPaolo Bonzini     if (kernel_filename != NULL) {
37853018216SPaolo Bonzini         long     kernel_size;
37953018216SPaolo Bonzini         uint64_t entry;
38053018216SPaolo Bonzini 
3814366e1dbSLiam Merwick         kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
3826cdda0ffSAleksandar Markovic                                &entry, NULL, NULL, NULL,
3837ef295eaSPeter Crosthwaite                                1 /* big endian */, EM_SPARC, 0, 0);
38453018216SPaolo Bonzini         if (kernel_size < 0) {
3859176a580SPhilippe Mathieu-Daudé             kernel_size = load_uimage(kernel_filename, NULL, &entry,
3869176a580SPhilippe Mathieu-Daudé                                       NULL, NULL, NULL);
3879176a580SPhilippe Mathieu-Daudé         }
3889176a580SPhilippe Mathieu-Daudé         if (kernel_size < 0) {
38929bd7231SAlistair Francis             error_report("could not load kernel '%s'", kernel_filename);
39053018216SPaolo Bonzini             exit(1);
39153018216SPaolo Bonzini         }
39253018216SPaolo Bonzini         if (bios_size <= 0) {
393dbed0d2dSKONRAD Frederic             /*
394dbed0d2dSKONRAD Frederic              * If there is no bios/monitor just start the application but put
395dbed0d2dSKONRAD Frederic              * the machine in an initialized state through a little
396dbed0d2dSKONRAD Frederic              * bootloader.
397dbed0d2dSKONRAD Frederic              */
39804752105SPhilippe Mathieu-Daudé             write_bootloader(memory_region_get_ram_ptr(prom), entry);
399dbed0d2dSKONRAD Frederic             reset_info->entry = LEON3_PROM_OFFSET;
400d65aba82SClément Chigot             for (i = 0; i < machine->smp.cpus; i++) {
401d65aba82SClément Chigot                 reset_info->info[i].cpu->env.pc = LEON3_PROM_OFFSET;
402d65aba82SClément Chigot                 reset_info->info[i].cpu->env.npc = LEON3_PROM_OFFSET + 4;
403d65aba82SClément Chigot             }
40453018216SPaolo Bonzini         }
40553018216SPaolo Bonzini     }
40653018216SPaolo Bonzini 
40753018216SPaolo Bonzini     /* Allocate timers */
4083e80f690SMarkus Armbruster     dev = qdev_new(TYPE_GRLIB_GPTIMER);
409948caec8SKONRAD Frederic     qdev_prop_set_uint32(dev, "nr-timers", LEON3_TIMER_COUNT);
410948caec8SKONRAD Frederic     qdev_prop_set_uint32(dev, "frequency", CPU_CLK);
411948caec8SKONRAD Frederic     qdev_prop_set_uint32(dev, "irq-line", LEON3_TIMER_IRQ);
4123c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
413948caec8SKONRAD Frederic 
414948caec8SKONRAD Frederic     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_TIMER_OFFSET);
415948caec8SKONRAD Frederic     for (i = 0; i < LEON3_TIMER_COUNT; i++) {
416948caec8SKONRAD Frederic         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
41733919536SPeter Maydell                            qdev_get_gpio_in(irqmpdev, LEON3_TIMER_IRQ + i));
418948caec8SKONRAD Frederic     }
41953018216SPaolo Bonzini 
420162abf1aSKONRAD Frederic     grlib_apb_pnp_add_entry(apb_pnp, LEON3_TIMER_OFFSET, 0xFFF,
421162abf1aSKONRAD Frederic                             GRLIB_VENDOR_GAISLER, GRLIB_GPTIMER_DEV,
422162abf1aSKONRAD Frederic                             0, LEON3_TIMER_IRQ, GRLIB_APBIO_AREA);
423162abf1aSKONRAD Frederic 
42453018216SPaolo Bonzini     /* Allocate uart */
4253e80f690SMarkus Armbruster     dev = qdev_new(TYPE_GRLIB_APB_UART);
426b70447aaSKONRAD Frederic     qdev_prop_set_chr(dev, "chrdev", serial_hd(0));
4273c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
428b70447aaSKONRAD Frederic     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, LEON3_UART_OFFSET);
42933919536SPeter Maydell     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
43033919536SPeter Maydell                        qdev_get_gpio_in(irqmpdev, LEON3_UART_IRQ));
431162abf1aSKONRAD Frederic     grlib_apb_pnp_add_entry(apb_pnp, LEON3_UART_OFFSET, 0xFFF,
432162abf1aSKONRAD Frederic                             GRLIB_VENDOR_GAISLER, GRLIB_APBUART_DEV, 1,
433162abf1aSKONRAD Frederic                             LEON3_UART_IRQ, GRLIB_APBIO_AREA);
43453018216SPaolo Bonzini }
43553018216SPaolo Bonzini 
leon3_generic_machine_init(MachineClass * mc)436e264d29dSEduardo Habkost static void leon3_generic_machine_init(MachineClass *mc)
43753018216SPaolo Bonzini {
438e264d29dSEduardo Habkost     mc->desc = "Leon-3 generic";
439e264d29dSEduardo Habkost     mc->init = leon3_generic_hw_init;
440e9135ab3SIgor Mammedov     mc->default_cpu_type = SPARC_CPU_TYPE_NAME("LEON3");
441fe3e7b71SIgor Mammedov     mc->default_ram_id = "leon3.ram";
442d65aba82SClément Chigot     mc->max_cpus = MAX_CPUS;
44353018216SPaolo Bonzini }
44453018216SPaolo Bonzini 
445e264d29dSEduardo Habkost DEFINE_MACHINE("leon3_generic", leon3_generic_machine_init)
446