mips_r4k.c 9.3 KB

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