mips_r4k.c 8.9 KB

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