2
0

leon3.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  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. */
  24. #include "hw/hw.h"
  25. #include "qemu/timer.h"
  26. #include "hw/ptimer.h"
  27. #include "sysemu/char.h"
  28. #include "sysemu/sysemu.h"
  29. #include "sysemu/qtest.h"
  30. #include "hw/boards.h"
  31. #include "hw/loader.h"
  32. #include "elf.h"
  33. #include "trace.h"
  34. #include "exec/address-spaces.h"
  35. #include "hw/sparc/grlib.h"
  36. /* Default system clock. */
  37. #define CPU_CLK (40 * 1000 * 1000)
  38. #define PROM_FILENAME "u-boot.bin"
  39. #define MAX_PILS 16
  40. typedef struct ResetData {
  41. SPARCCPU *cpu;
  42. uint32_t entry; /* save kernel entry in case of reset */
  43. target_ulong sp; /* initial stack pointer */
  44. } ResetData;
  45. static void main_cpu_reset(void *opaque)
  46. {
  47. ResetData *s = (ResetData *)opaque;
  48. CPUState *cpu = CPU(s->cpu);
  49. CPUSPARCState *env = &s->cpu->env;
  50. cpu_reset(cpu);
  51. cpu->halted = 0;
  52. env->pc = s->entry;
  53. env->npc = s->entry + 4;
  54. env->regbase[6] = s->sp;
  55. }
  56. void leon3_irq_ack(void *irq_manager, int intno)
  57. {
  58. grlib_irqmp_ack((DeviceState *)irq_manager, intno);
  59. }
  60. static void leon3_set_pil_in(void *opaque, uint32_t pil_in)
  61. {
  62. CPUSPARCState *env = (CPUSPARCState *)opaque;
  63. CPUState *cs;
  64. assert(env != NULL);
  65. env->pil_in = pil_in;
  66. if (env->pil_in && (env->interrupt_index == 0 ||
  67. (env->interrupt_index & ~15) == TT_EXTINT)) {
  68. unsigned int i;
  69. for (i = 15; i > 0; i--) {
  70. if (env->pil_in & (1 << i)) {
  71. int old_interrupt = env->interrupt_index;
  72. env->interrupt_index = TT_EXTINT | i;
  73. if (old_interrupt != env->interrupt_index) {
  74. cs = CPU(sparc_env_get_cpu(env));
  75. trace_leon3_set_irq(i);
  76. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  77. }
  78. break;
  79. }
  80. }
  81. } else if (!env->pil_in && (env->interrupt_index & ~15) == TT_EXTINT) {
  82. cs = CPU(sparc_env_get_cpu(env));
  83. trace_leon3_reset_irq(env->interrupt_index & 15);
  84. env->interrupt_index = 0;
  85. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  86. }
  87. }
  88. static void leon3_generic_hw_init(QEMUMachineInitArgs *args)
  89. {
  90. ram_addr_t ram_size = args->ram_size;
  91. const char *cpu_model = args->cpu_model;
  92. const char *kernel_filename = args->kernel_filename;
  93. SPARCCPU *cpu;
  94. CPUSPARCState *env;
  95. MemoryRegion *address_space_mem = get_system_memory();
  96. MemoryRegion *ram = g_new(MemoryRegion, 1);
  97. MemoryRegion *prom = g_new(MemoryRegion, 1);
  98. int ret;
  99. char *filename;
  100. qemu_irq *cpu_irqs = NULL;
  101. int bios_size;
  102. int prom_size;
  103. ResetData *reset_info;
  104. /* Init CPU */
  105. if (!cpu_model) {
  106. cpu_model = "LEON3";
  107. }
  108. cpu = cpu_sparc_init(cpu_model);
  109. if (cpu == NULL) {
  110. fprintf(stderr, "qemu: Unable to find Sparc CPU definition\n");
  111. exit(1);
  112. }
  113. env = &cpu->env;
  114. cpu_sparc_set_id(env, 0);
  115. /* Reset data */
  116. reset_info = g_malloc0(sizeof(ResetData));
  117. reset_info->cpu = cpu;
  118. reset_info->sp = 0x40000000 + ram_size;
  119. qemu_register_reset(main_cpu_reset, reset_info);
  120. /* Allocate IRQ manager */
  121. grlib_irqmp_create(0x80000200, env, &cpu_irqs, MAX_PILS, &leon3_set_pil_in);
  122. env->qemu_irq_ack = leon3_irq_manager;
  123. /* Allocate RAM */
  124. if ((uint64_t)ram_size > (1UL << 30)) {
  125. fprintf(stderr,
  126. "qemu: Too much memory for this machine: %d, maximum 1G\n",
  127. (unsigned int)(ram_size / (1024 * 1024)));
  128. exit(1);
  129. }
  130. memory_region_init_ram(ram, NULL, "leon3.ram", ram_size);
  131. vmstate_register_ram_global(ram);
  132. memory_region_add_subregion(address_space_mem, 0x40000000, ram);
  133. /* Allocate BIOS */
  134. prom_size = 8 * 1024 * 1024; /* 8Mb */
  135. memory_region_init_ram(prom, NULL, "Leon3.bios", prom_size);
  136. vmstate_register_ram_global(prom);
  137. memory_region_set_readonly(prom, true);
  138. memory_region_add_subregion(address_space_mem, 0x00000000, prom);
  139. /* Load boot prom */
  140. if (bios_name == NULL) {
  141. bios_name = PROM_FILENAME;
  142. }
  143. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  144. bios_size = get_image_size(filename);
  145. if (bios_size > prom_size) {
  146. fprintf(stderr, "qemu: could not load prom '%s': file too big\n",
  147. filename);
  148. exit(1);
  149. }
  150. if (bios_size > 0) {
  151. ret = load_image_targphys(filename, 0x00000000, bios_size);
  152. if (ret < 0 || ret > prom_size) {
  153. fprintf(stderr, "qemu: could not load prom '%s'\n", filename);
  154. exit(1);
  155. }
  156. } else if (kernel_filename == NULL && !qtest_enabled()) {
  157. fprintf(stderr, "Can't read bios image %s\n", filename);
  158. exit(1);
  159. }
  160. /* Can directly load an application. */
  161. if (kernel_filename != NULL) {
  162. long kernel_size;
  163. uint64_t entry;
  164. kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
  165. 1 /* big endian */, ELF_MACHINE, 0);
  166. if (kernel_size < 0) {
  167. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  168. kernel_filename);
  169. exit(1);
  170. }
  171. if (bios_size <= 0) {
  172. /* If there is no bios/monitor, start the application. */
  173. env->pc = entry;
  174. env->npc = entry + 4;
  175. reset_info->entry = entry;
  176. }
  177. }
  178. /* Allocate timers */
  179. grlib_gptimer_create(0x80000300, 2, CPU_CLK, cpu_irqs, 6);
  180. /* Allocate uart */
  181. if (serial_hds[0]) {
  182. grlib_apbuart_create(0x80000100, serial_hds[0], cpu_irqs[3]);
  183. }
  184. }
  185. static QEMUMachine leon3_generic_machine = {
  186. .name = "leon3_generic",
  187. .desc = "Leon-3 generic",
  188. .init = leon3_generic_hw_init,
  189. };
  190. static void leon3_machine_init(void)
  191. {
  192. qemu_register_machine(&leon3_generic_machine);
  193. }
  194. machine_init(leon3_machine_init);