r4k.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * QEMU/MIPS pseudo-board
  3. *
  4. * emulates a simple machine with ISA-like bus.
  5. * ISA IO space mapped to the 0x14000000 (PHYS) and
  6. * ISA memory at the 0x10000000 (PHYS, 16Mb in size).
  7. * All peripherial devices are attached to this "bus" with
  8. * the standard PC ISA addresses.
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/units.h"
  12. #include "qapi/error.h"
  13. #include "qemu-common.h"
  14. #include "cpu.h"
  15. #include "hw/mips/mips.h"
  16. #include "hw/mips/cpudevs.h"
  17. #include "hw/intc/i8259.h"
  18. #include "hw/char/serial.h"
  19. #include "hw/isa/isa.h"
  20. #include "net/net.h"
  21. #include "hw/net/ne2000-isa.h"
  22. #include "sysemu/sysemu.h"
  23. #include "hw/boards.h"
  24. #include "hw/block/flash.h"
  25. #include "qemu/log.h"
  26. #include "hw/mips/bios.h"
  27. #include "hw/ide.h"
  28. #include "hw/ide/internal.h"
  29. #include "hw/loader.h"
  30. #include "elf.h"
  31. #include "hw/rtc/mc146818rtc.h"
  32. #include "hw/input/i8042.h"
  33. #include "hw/timer/i8254.h"
  34. #include "exec/address-spaces.h"
  35. #include "sysemu/qtest.h"
  36. #include "sysemu/reset.h"
  37. #include "sysemu/runstate.h"
  38. #include "qemu/error-report.h"
  39. #define MAX_IDE_BUS 2
  40. static const int ide_iobase[2] = { 0x1f0, 0x170 };
  41. static const int ide_iobase2[2] = { 0x3f6, 0x376 };
  42. static const int ide_irq[2] = { 14, 15 };
  43. static ISADevice *pit; /* PIT i8254 */
  44. /* i8254 PIT is attached to the IRQ0 at PIC i8259 */
  45. static struct _loaderparams {
  46. int ram_size;
  47. const char *kernel_filename;
  48. const char *kernel_cmdline;
  49. const char *initrd_filename;
  50. } loaderparams;
  51. static void mips_qemu_write(void *opaque, hwaddr addr,
  52. uint64_t val, unsigned size)
  53. {
  54. if ((addr & 0xffff) == 0 && val == 42) {
  55. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  56. } else if ((addr & 0xffff) == 4 && val == 42) {
  57. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  58. }
  59. }
  60. static uint64_t mips_qemu_read(void *opaque, hwaddr addr,
  61. unsigned size)
  62. {
  63. return 0;
  64. }
  65. static const MemoryRegionOps mips_qemu_ops = {
  66. .read = mips_qemu_read,
  67. .write = mips_qemu_write,
  68. .endianness = DEVICE_NATIVE_ENDIAN,
  69. };
  70. typedef struct ResetData {
  71. MIPSCPU *cpu;
  72. uint64_t vector;
  73. } ResetData;
  74. static int64_t load_kernel(void)
  75. {
  76. const size_t params_size = 264;
  77. int64_t entry, kernel_high, initrd_size;
  78. long kernel_size;
  79. ram_addr_t initrd_offset;
  80. uint32_t *params_buf;
  81. int big_endian;
  82. #ifdef TARGET_WORDS_BIGENDIAN
  83. big_endian = 1;
  84. #else
  85. big_endian = 0;
  86. #endif
  87. kernel_size = load_elf(loaderparams.kernel_filename, NULL,
  88. cpu_mips_kseg0_to_phys, NULL,
  89. (uint64_t *)&entry, NULL,
  90. (uint64_t *)&kernel_high, NULL, big_endian,
  91. EM_MIPS, 1, 0);
  92. if (kernel_size >= 0) {
  93. if ((entry & ~0x7fffffffULL) == 0x80000000) {
  94. entry = (int32_t)entry;
  95. }
  96. } else {
  97. error_report("could not load kernel '%s': %s",
  98. loaderparams.kernel_filename,
  99. load_elf_strerror(kernel_size));
  100. exit(1);
  101. }
  102. /* load initrd */
  103. initrd_size = 0;
  104. initrd_offset = 0;
  105. if (loaderparams.initrd_filename) {
  106. initrd_size = get_image_size(loaderparams.initrd_filename);
  107. if (initrd_size > 0) {
  108. initrd_offset = (kernel_high + ~INITRD_PAGE_MASK) &
  109. INITRD_PAGE_MASK;
  110. if (initrd_offset + initrd_size > ram_size) {
  111. error_report("memory too small for initial ram disk '%s'",
  112. loaderparams.initrd_filename);
  113. exit(1);
  114. }
  115. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  116. initrd_offset,
  117. ram_size - initrd_offset);
  118. }
  119. if (initrd_size == (target_ulong) -1) {
  120. error_report("could not load initial ram disk '%s'",
  121. loaderparams.initrd_filename);
  122. exit(1);
  123. }
  124. }
  125. /* Store command line. */
  126. params_buf = g_malloc(params_size);
  127. params_buf[0] = tswap32(ram_size);
  128. params_buf[1] = tswap32(0x12345678);
  129. if (initrd_size > 0) {
  130. snprintf((char *)params_buf + 8, 256,
  131. "rd_start=0x%" PRIx64 " rd_size=%" PRId64 " %s",
  132. cpu_mips_phys_to_kseg0(NULL, initrd_offset),
  133. initrd_size, loaderparams.kernel_cmdline);
  134. } else {
  135. snprintf((char *)params_buf + 8, 256,
  136. "%s", loaderparams.kernel_cmdline);
  137. }
  138. rom_add_blob_fixed("params", params_buf, params_size,
  139. 16 * MiB - params_size);
  140. g_free(params_buf);
  141. return entry;
  142. }
  143. static void main_cpu_reset(void *opaque)
  144. {
  145. ResetData *s = (ResetData *)opaque;
  146. CPUMIPSState *env = &s->cpu->env;
  147. cpu_reset(CPU(s->cpu));
  148. env->active_tc.PC = s->vector;
  149. }
  150. static const int sector_len = 32 * KiB;
  151. static
  152. void mips_r4k_init(MachineState *machine)
  153. {
  154. const char *kernel_filename = machine->kernel_filename;
  155. const char *kernel_cmdline = machine->kernel_cmdline;
  156. const char *initrd_filename = machine->initrd_filename;
  157. char *filename;
  158. MemoryRegion *address_space_mem = get_system_memory();
  159. MemoryRegion *bios;
  160. MemoryRegion *iomem = g_new(MemoryRegion, 1);
  161. MemoryRegion *isa_io = g_new(MemoryRegion, 1);
  162. MemoryRegion *isa_mem = g_new(MemoryRegion, 1);
  163. int bios_size;
  164. MIPSCPU *cpu;
  165. CPUMIPSState *env;
  166. ResetData *reset_info;
  167. int i;
  168. qemu_irq *i8259;
  169. ISABus *isa_bus;
  170. DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  171. DriveInfo *dinfo;
  172. int be;
  173. /* init CPUs */
  174. cpu = MIPS_CPU(cpu_create(machine->cpu_type));
  175. env = &cpu->env;
  176. reset_info = g_malloc0(sizeof(ResetData));
  177. reset_info->cpu = cpu;
  178. reset_info->vector = env->active_tc.PC;
  179. qemu_register_reset(main_cpu_reset, reset_info);
  180. /* allocate RAM */
  181. if (machine->ram_size > 256 * MiB) {
  182. error_report("Too much memory for this machine: %" PRId64 "MB,"
  183. " maximum 256MB", ram_size / MiB);
  184. exit(1);
  185. }
  186. memory_region_add_subregion(address_space_mem, 0, machine->ram);
  187. memory_region_init_io(iomem, NULL, &mips_qemu_ops,
  188. NULL, "mips-qemu", 0x10000);
  189. memory_region_add_subregion(address_space_mem, 0x1fbf0000, iomem);
  190. /*
  191. * Try to load a BIOS image. If this fails, we continue regardless,
  192. * but initialize the hardware ourselves. When a kernel gets
  193. * preloaded we also initialize the hardware, since the BIOS wasn't
  194. * run.
  195. */
  196. if (bios_name == NULL) {
  197. bios_name = BIOS_FILENAME;
  198. }
  199. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  200. if (filename) {
  201. bios_size = get_image_size(filename);
  202. } else {
  203. bios_size = -1;
  204. }
  205. #ifdef TARGET_WORDS_BIGENDIAN
  206. be = 1;
  207. #else
  208. be = 0;
  209. #endif
  210. dinfo = drive_get(IF_PFLASH, 0, 0);
  211. if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
  212. bios = g_new(MemoryRegion, 1);
  213. memory_region_init_rom(bios, NULL, "mips_r4k.bios", BIOS_SIZE,
  214. &error_fatal);
  215. memory_region_add_subregion(get_system_memory(), 0x1fc00000, bios);
  216. load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
  217. } else if (dinfo != NULL) {
  218. uint32_t mips_rom = 0x00400000;
  219. if (!pflash_cfi01_register(0x1fc00000, "mips_r4k.bios", mips_rom,
  220. blk_by_legacy_dinfo(dinfo),
  221. sector_len, 4, 0, 0, 0, 0, be)) {
  222. fprintf(stderr, "qemu: Error registering flash memory.\n");
  223. }
  224. } else if (!qtest_enabled()) {
  225. /* not fatal */
  226. warn_report("could not load MIPS bios '%s'", bios_name);
  227. }
  228. g_free(filename);
  229. if (kernel_filename) {
  230. loaderparams.ram_size = machine->ram_size;
  231. loaderparams.kernel_filename = kernel_filename;
  232. loaderparams.kernel_cmdline = kernel_cmdline;
  233. loaderparams.initrd_filename = initrd_filename;
  234. reset_info->vector = load_kernel();
  235. }
  236. /* Init CPU internal devices */
  237. cpu_mips_irq_init_cpu(cpu);
  238. cpu_mips_clock_init(cpu);
  239. /* ISA bus: IO space at 0x14000000, mem space at 0x10000000 */
  240. memory_region_init_alias(isa_io, NULL, "isa-io",
  241. get_system_io(), 0, 0x00010000);
  242. memory_region_init(isa_mem, NULL, "isa-mem", 0x01000000);
  243. memory_region_add_subregion(get_system_memory(), 0x14000000, isa_io);
  244. memory_region_add_subregion(get_system_memory(), 0x10000000, isa_mem);
  245. isa_bus = isa_bus_new(NULL, isa_mem, get_system_io(), &error_abort);
  246. /* The PIC is attached to the MIPS CPU INT0 pin */
  247. i8259 = i8259_init(isa_bus, env->irq[2]);
  248. isa_bus_irqs(isa_bus, i8259);
  249. mc146818_rtc_init(isa_bus, 2000, NULL);
  250. pit = i8254_pit_init(isa_bus, 0x40, 0, NULL);
  251. serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
  252. isa_vga_init(isa_bus);
  253. if (nd_table[0].used) {
  254. isa_ne2000_init(isa_bus, 0x300, 9, &nd_table[0]);
  255. }
  256. ide_drive_get(hd, ARRAY_SIZE(hd));
  257. for (i = 0; i < MAX_IDE_BUS; i++)
  258. isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
  259. hd[MAX_IDE_DEVS * i],
  260. hd[MAX_IDE_DEVS * i + 1]);
  261. isa_create_simple(isa_bus, TYPE_I8042);
  262. }
  263. static void mips_machine_init(MachineClass *mc)
  264. {
  265. mc->deprecation_reason = "use malta machine type instead";
  266. mc->desc = "mips r4k platform";
  267. mc->init = mips_r4k_init;
  268. mc->block_default_type = IF_IDE;
  269. #ifdef TARGET_MIPS64
  270. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("R4000");
  271. #else
  272. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
  273. #endif
  274. mc->default_ram_id = "mips_r4k.ram";
  275. }
  276. DEFINE_MACHINE("mips", mips_machine_init)