openrisc_sim.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * OpenRISC simulator for use as an IIS.
  3. *
  4. * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
  5. * Feng Gao <gf91597@gmail.com>
  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
  10. * version 2.1 of the License, or (at your option) any later version.
  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. #include "qemu/osdep.h"
  21. #include "qemu/error-report.h"
  22. #include "qapi/error.h"
  23. #include "cpu.h"
  24. #include "hw/irq.h"
  25. #include "hw/boards.h"
  26. #include "elf.h"
  27. #include "hw/char/serial.h"
  28. #include "net/net.h"
  29. #include "hw/loader.h"
  30. #include "hw/qdev-properties.h"
  31. #include "exec/address-spaces.h"
  32. #include "sysemu/sysemu.h"
  33. #include "hw/sysbus.h"
  34. #include "sysemu/qtest.h"
  35. #include "sysemu/reset.h"
  36. #define KERNEL_LOAD_ADDR 0x100
  37. static struct openrisc_boot_info {
  38. uint32_t bootstrap_pc;
  39. } boot_info;
  40. static void main_cpu_reset(void *opaque)
  41. {
  42. OpenRISCCPU *cpu = opaque;
  43. CPUState *cs = CPU(cpu);
  44. cpu_reset(CPU(cpu));
  45. cpu_set_pc(cs, boot_info.bootstrap_pc);
  46. }
  47. static void openrisc_sim_net_init(hwaddr base, hwaddr descriptors,
  48. int num_cpus, qemu_irq **cpu_irqs,
  49. int irq_pin, NICInfo *nd)
  50. {
  51. DeviceState *dev;
  52. SysBusDevice *s;
  53. int i;
  54. dev = qdev_create(NULL, "open_eth");
  55. qdev_set_nic_properties(dev, nd);
  56. qdev_init_nofail(dev);
  57. s = SYS_BUS_DEVICE(dev);
  58. for (i = 0; i < num_cpus; i++) {
  59. sysbus_connect_irq(s, 0, cpu_irqs[i][irq_pin]);
  60. }
  61. sysbus_mmio_map(s, 0, base);
  62. sysbus_mmio_map(s, 1, descriptors);
  63. }
  64. static void openrisc_sim_ompic_init(hwaddr base, int num_cpus,
  65. qemu_irq **cpu_irqs, int irq_pin)
  66. {
  67. DeviceState *dev;
  68. SysBusDevice *s;
  69. int i;
  70. dev = qdev_create(NULL, "or1k-ompic");
  71. qdev_prop_set_uint32(dev, "num-cpus", num_cpus);
  72. qdev_init_nofail(dev);
  73. s = SYS_BUS_DEVICE(dev);
  74. for (i = 0; i < num_cpus; i++) {
  75. sysbus_connect_irq(s, i, cpu_irqs[i][irq_pin]);
  76. }
  77. sysbus_mmio_map(s, 0, base);
  78. }
  79. static void openrisc_load_kernel(ram_addr_t ram_size,
  80. const char *kernel_filename)
  81. {
  82. long kernel_size;
  83. uint64_t elf_entry;
  84. hwaddr entry;
  85. if (kernel_filename && !qtest_enabled()) {
  86. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  87. &elf_entry, NULL, NULL, 1, EM_OPENRISC,
  88. 1, 0);
  89. entry = elf_entry;
  90. if (kernel_size < 0) {
  91. kernel_size = load_uimage(kernel_filename,
  92. &entry, NULL, NULL, NULL, NULL);
  93. }
  94. if (kernel_size < 0) {
  95. kernel_size = load_image_targphys(kernel_filename,
  96. KERNEL_LOAD_ADDR,
  97. ram_size - KERNEL_LOAD_ADDR);
  98. }
  99. if (entry <= 0) {
  100. entry = KERNEL_LOAD_ADDR;
  101. }
  102. if (kernel_size < 0) {
  103. error_report("couldn't load the kernel '%s'", kernel_filename);
  104. exit(1);
  105. }
  106. boot_info.bootstrap_pc = entry;
  107. }
  108. }
  109. static void openrisc_sim_init(MachineState *machine)
  110. {
  111. ram_addr_t ram_size = machine->ram_size;
  112. const char *kernel_filename = machine->kernel_filename;
  113. OpenRISCCPU *cpu = NULL;
  114. MemoryRegion *ram;
  115. qemu_irq *cpu_irqs[2];
  116. qemu_irq serial_irq;
  117. int n;
  118. unsigned int smp_cpus = machine->smp.cpus;
  119. for (n = 0; n < smp_cpus; n++) {
  120. cpu = OPENRISC_CPU(cpu_create(machine->cpu_type));
  121. if (cpu == NULL) {
  122. fprintf(stderr, "Unable to find CPU definition!\n");
  123. exit(1);
  124. }
  125. cpu_openrisc_pic_init(cpu);
  126. cpu_irqs[n] = (qemu_irq *) cpu->env.irq;
  127. cpu_openrisc_clock_init(cpu);
  128. qemu_register_reset(main_cpu_reset, cpu);
  129. }
  130. ram = g_malloc(sizeof(*ram));
  131. memory_region_init_ram(ram, NULL, "openrisc.ram", ram_size, &error_fatal);
  132. memory_region_add_subregion(get_system_memory(), 0, ram);
  133. if (nd_table[0].used) {
  134. openrisc_sim_net_init(0x92000000, 0x92000400, smp_cpus,
  135. cpu_irqs, 4, nd_table);
  136. }
  137. if (smp_cpus > 1) {
  138. openrisc_sim_ompic_init(0x98000000, smp_cpus, cpu_irqs, 1);
  139. serial_irq = qemu_irq_split(cpu_irqs[0][2], cpu_irqs[1][2]);
  140. } else {
  141. serial_irq = cpu_irqs[0][2];
  142. }
  143. serial_mm_init(get_system_memory(), 0x90000000, 0, serial_irq,
  144. 115200, serial_hd(0), DEVICE_NATIVE_ENDIAN);
  145. openrisc_load_kernel(ram_size, kernel_filename);
  146. }
  147. static void openrisc_sim_machine_init(MachineClass *mc)
  148. {
  149. mc->desc = "or1k simulation";
  150. mc->init = openrisc_sim_init;
  151. mc->max_cpus = 2;
  152. mc->is_default = 1;
  153. mc->default_cpu_type = OPENRISC_CPU_TYPE_NAME("or1200");
  154. }
  155. DEFINE_MACHINE("or1k-sim", openrisc_sim_machine_init)