xref: /openbmc/qemu/hw/xtensa/sim.c (revision 9d81b2d2)
1 /*
2  * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the Open Source and Linux Lab nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "cpu.h"
32 #include "sysemu/sysemu.h"
33 #include "hw/boards.h"
34 #include "hw/loader.h"
35 #include "elf.h"
36 #include "exec/memory.h"
37 #include "exec/address-spaces.h"
38 #include "qemu/error-report.h"
39 
40 static void xtensa_create_memory_regions(const XtensaMemory *memory,
41                                          const char *name)
42 {
43     unsigned i;
44     GString *num_name = g_string_new(NULL);
45 
46     for (i = 0; i < memory->num; ++i) {
47         MemoryRegion *m;
48 
49         g_string_printf(num_name, "%s%u", name, i);
50         m = g_new(MemoryRegion, 1);
51         memory_region_init_ram(m, NULL, num_name->str,
52                                memory->location[i].size, &error_fatal);
53         memory_region_add_subregion(get_system_memory(),
54                                     memory->location[i].addr, m);
55     }
56     g_string_free(num_name, true);
57 }
58 
59 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
60 {
61     XtensaCPU *cpu = opaque;
62 
63     return cpu_get_phys_page_debug(CPU(cpu), addr);
64 }
65 
66 static void sim_reset(void *opaque)
67 {
68     XtensaCPU *cpu = opaque;
69 
70     cpu_reset(CPU(cpu));
71 }
72 
73 static void xtensa_sim_init(MachineState *machine)
74 {
75     XtensaCPU *cpu = NULL;
76     CPUXtensaState *env = NULL;
77     ram_addr_t ram_size = machine->ram_size;
78     const char *cpu_model = machine->cpu_model;
79     const char *kernel_filename = machine->kernel_filename;
80     int n;
81 
82     if (!cpu_model) {
83         cpu_model = XTENSA_DEFAULT_CPU_MODEL;
84     }
85 
86     for (n = 0; n < smp_cpus; n++) {
87         cpu = XTENSA_CPU(cpu_generic_init(TYPE_XTENSA_CPU, cpu_model));
88         if (cpu == NULL) {
89             error_report("unable to find CPU definition '%s'",
90                          cpu_model);
91             exit(EXIT_FAILURE);
92         }
93         env = &cpu->env;
94 
95         env->sregs[PRID] = n;
96         qemu_register_reset(sim_reset, cpu);
97         /* Need MMU initialized prior to ELF loading,
98          * so that ELF gets loaded into virtual addresses
99          */
100         sim_reset(cpu);
101     }
102 
103     if (env) {
104         XtensaMemory sysram = env->config->sysram;
105 
106         sysram.location[0].size = ram_size;
107         xtensa_create_memory_regions(&env->config->instrom, "xtensa.instrom");
108         xtensa_create_memory_regions(&env->config->instram, "xtensa.instram");
109         xtensa_create_memory_regions(&env->config->datarom, "xtensa.datarom");
110         xtensa_create_memory_regions(&env->config->dataram, "xtensa.dataram");
111         xtensa_create_memory_regions(&env->config->sysrom, "xtensa.sysrom");
112         xtensa_create_memory_regions(&sysram, "xtensa.sysram");
113     }
114 
115     if (serial_hds[0]) {
116         xtensa_sim_open_console(serial_hds[0]);
117     }
118     if (kernel_filename) {
119         uint64_t elf_entry;
120         uint64_t elf_lowaddr;
121 #ifdef TARGET_WORDS_BIGENDIAN
122         int success = load_elf(kernel_filename, translate_phys_addr, cpu,
123                 &elf_entry, &elf_lowaddr, NULL, 1, EM_XTENSA, 0, 0);
124 #else
125         int success = load_elf(kernel_filename, translate_phys_addr, cpu,
126                 &elf_entry, &elf_lowaddr, NULL, 0, EM_XTENSA, 0, 0);
127 #endif
128         if (success > 0) {
129             env->pc = elf_entry;
130         }
131     }
132 }
133 
134 static void xtensa_sim_machine_init(MachineClass *mc)
135 {
136     mc->desc = "sim machine (" XTENSA_DEFAULT_CPU_MODEL ")";
137     mc->is_default = true;
138     mc->init = xtensa_sim_init;
139     mc->max_cpus = 4;
140     mc->no_serial = 1;
141 }
142 
143 DEFINE_MACHINE("sim", xtensa_sim_machine_init)
144