mips_r4k.c 9.0 KB

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