2
0

mips_r4k.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "pc.h"
  13. #include "isa.h"
  14. #include "net.h"
  15. #include "sysemu.h"
  16. #include "boards.h"
  17. #include "flash.h"
  18. #include "qemu-log.h"
  19. #ifdef TARGET_WORDS_BIGENDIAN
  20. #define BIOS_FILENAME "mips_bios.bin"
  21. #else
  22. #define BIOS_FILENAME "mipsel_bios.bin"
  23. #endif
  24. #define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
  25. #define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
  26. #define MAX_IDE_BUS 2
  27. static const int ide_iobase[2] = { 0x1f0, 0x170 };
  28. static const int ide_iobase2[2] = { 0x3f6, 0x376 };
  29. static const int ide_irq[2] = { 14, 15 };
  30. static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
  31. static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
  32. static PITState *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_writel (void *opaque, target_phys_addr_t addr,
  41. uint32_t val)
  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 uint32_t mips_qemu_readl (void *opaque, target_phys_addr_t addr)
  49. {
  50. return 0;
  51. }
  52. static CPUWriteMemoryFunc *mips_qemu_write[] = {
  53. &mips_qemu_writel,
  54. &mips_qemu_writel,
  55. &mips_qemu_writel,
  56. };
  57. static CPUReadMemoryFunc *mips_qemu_read[] = {
  58. &mips_qemu_readl,
  59. &mips_qemu_readl,
  60. &mips_qemu_readl,
  61. };
  62. static int mips_qemu_iomemtype = 0;
  63. static void load_kernel (CPUState *env)
  64. {
  65. int64_t entry, kernel_low, kernel_high;
  66. long kernel_size, initrd_size;
  67. ram_addr_t initrd_offset;
  68. kernel_size = load_elf(loaderparams.kernel_filename, VIRT_TO_PHYS_ADDEND,
  69. (uint64_t *)&entry, (uint64_t *)&kernel_low,
  70. (uint64_t *)&kernel_high);
  71. if (kernel_size >= 0) {
  72. if ((entry & ~0x7fffffffULL) == 0x80000000)
  73. entry = (int32_t)entry;
  74. env->active_tc.PC = entry;
  75. } else {
  76. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  77. loaderparams.kernel_filename);
  78. exit(1);
  79. }
  80. /* load initrd */
  81. initrd_size = 0;
  82. initrd_offset = 0;
  83. if (loaderparams.initrd_filename) {
  84. initrd_size = get_image_size (loaderparams.initrd_filename);
  85. if (initrd_size > 0) {
  86. initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
  87. if (initrd_offset + initrd_size > ram_size) {
  88. fprintf(stderr,
  89. "qemu: memory too small for initial ram disk '%s'\n",
  90. loaderparams.initrd_filename);
  91. exit(1);
  92. }
  93. initrd_size = load_image(loaderparams.initrd_filename,
  94. phys_ram_base + initrd_offset);
  95. }
  96. if (initrd_size == (target_ulong) -1) {
  97. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  98. loaderparams.initrd_filename);
  99. exit(1);
  100. }
  101. }
  102. /* Store command line. */
  103. if (initrd_size > 0) {
  104. int ret;
  105. ret = sprintf((char *)(phys_ram_base + (16 << 20) - 256),
  106. "rd_start=0x" TARGET_FMT_lx " rd_size=%li ",
  107. PHYS_TO_VIRT((uint32_t)initrd_offset),
  108. initrd_size);
  109. strcpy ((char *)(phys_ram_base + (16 << 20) - 256 + ret),
  110. loaderparams.kernel_cmdline);
  111. }
  112. else {
  113. strcpy ((char *)(phys_ram_base + (16 << 20) - 256),
  114. loaderparams.kernel_cmdline);
  115. }
  116. *(int32_t *)(phys_ram_base + (16 << 20) - 260) = tswap32 (0x12345678);
  117. *(int32_t *)(phys_ram_base + (16 << 20) - 264) = tswap32 (ram_size);
  118. }
  119. static void main_cpu_reset(void *opaque)
  120. {
  121. CPUState *env = opaque;
  122. cpu_reset(env);
  123. if (loaderparams.kernel_filename)
  124. load_kernel (env);
  125. }
  126. static const int sector_len = 32 * 1024;
  127. static
  128. void mips_r4k_init (ram_addr_t ram_size, int vga_ram_size,
  129. const char *boot_device,
  130. const char *kernel_filename, const char *kernel_cmdline,
  131. const char *initrd_filename, const char *cpu_model)
  132. {
  133. char buf[1024];
  134. unsigned long bios_offset;
  135. int bios_size;
  136. CPUState *env;
  137. RTCState *rtc_state;
  138. int i;
  139. qemu_irq *i8259;
  140. int index;
  141. BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  142. /* init CPUs */
  143. if (cpu_model == NULL) {
  144. #ifdef TARGET_MIPS64
  145. cpu_model = "R4000";
  146. #else
  147. cpu_model = "24Kf";
  148. #endif
  149. }
  150. env = cpu_init(cpu_model);
  151. if (!env) {
  152. fprintf(stderr, "Unable to find CPU definition\n");
  153. exit(1);
  154. }
  155. qemu_register_reset(main_cpu_reset, env);
  156. /* allocate RAM */
  157. if (ram_size > (256 << 20)) {
  158. fprintf(stderr,
  159. "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
  160. ((unsigned int)ram_size / (1 << 20)));
  161. exit(1);
  162. }
  163. cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
  164. if (!mips_qemu_iomemtype) {
  165. mips_qemu_iomemtype = cpu_register_io_memory(0, mips_qemu_read,
  166. mips_qemu_write, NULL);
  167. }
  168. cpu_register_physical_memory(0x1fbf0000, 0x10000, mips_qemu_iomemtype);
  169. /* Try to load a BIOS image. If this fails, we continue regardless,
  170. but initialize the hardware ourselves. When a kernel gets
  171. preloaded we also initialize the hardware, since the BIOS wasn't
  172. run. */
  173. bios_offset = ram_size + vga_ram_size;
  174. if (bios_name == NULL)
  175. bios_name = BIOS_FILENAME;
  176. snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
  177. bios_size = load_image(buf, phys_ram_base + bios_offset);
  178. if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
  179. cpu_register_physical_memory(0x1fc00000,
  180. BIOS_SIZE, bios_offset | IO_MEM_ROM);
  181. } else if ((index = drive_get_index(IF_PFLASH, 0, 0)) > -1) {
  182. uint32_t mips_rom = 0x00400000;
  183. cpu_register_physical_memory(0x1fc00000, mips_rom,
  184. qemu_ram_alloc(mips_rom) | IO_MEM_ROM);
  185. if (!pflash_cfi01_register(0x1fc00000, qemu_ram_alloc(mips_rom),
  186. drives_table[index].bdrv, sector_len, mips_rom / sector_len,
  187. 4, 0, 0, 0, 0)) {
  188. fprintf(stderr, "qemu: Error registering flash memory.\n");
  189. }
  190. }
  191. else {
  192. /* not fatal */
  193. fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
  194. buf);
  195. }
  196. if (kernel_filename) {
  197. loaderparams.ram_size = ram_size;
  198. loaderparams.kernel_filename = kernel_filename;
  199. loaderparams.kernel_cmdline = kernel_cmdline;
  200. loaderparams.initrd_filename = initrd_filename;
  201. load_kernel (env);
  202. }
  203. /* Init CPU internal devices */
  204. cpu_mips_irq_init_cpu(env);
  205. cpu_mips_clock_init(env);
  206. /* The PIC is attached to the MIPS CPU INT0 pin */
  207. i8259 = i8259_init(env->irq[2]);
  208. rtc_state = rtc_init(0x70, i8259[8], 2000);
  209. /* Register 64 KB of ISA IO space at 0x14000000 */
  210. isa_mmio_init(0x14000000, 0x00010000);
  211. isa_mem_base = 0x10000000;
  212. pit = pit_init(0x40, i8259[0]);
  213. for(i = 0; i < MAX_SERIAL_PORTS; i++) {
  214. if (serial_hds[i]) {
  215. serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
  216. serial_hds[i]);
  217. }
  218. }
  219. isa_vga_init(phys_ram_base + ram_size, ram_size,
  220. vga_ram_size);
  221. if (nd_table[0].vlan)
  222. isa_ne2000_init(0x300, i8259[9], &nd_table[0]);
  223. if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
  224. fprintf(stderr, "qemu: too many IDE bus\n");
  225. exit(1);
  226. }
  227. for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
  228. index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
  229. if (index != -1)
  230. hd[i] = drives_table[index].bdrv;
  231. else
  232. hd[i] = NULL;
  233. }
  234. for(i = 0; i < MAX_IDE_BUS; i++)
  235. isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
  236. hd[MAX_IDE_DEVS * i],
  237. hd[MAX_IDE_DEVS * i + 1]);
  238. i8042_init(i8259[1], i8259[12], 0x60);
  239. }
  240. QEMUMachine mips_machine = {
  241. .name = "mips",
  242. .desc = "mips r4k platform",
  243. .init = mips_r4k_init,
  244. .ram_require = VGA_RAM_SIZE + BIOS_SIZE,
  245. .nodisk_ok = 1,
  246. };