machine.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * QEMU HPPA hardware system emulator.
  3. * Copyright 2018 Helge Deller <deller@gmx.de>
  4. */
  5. #include "qemu/osdep.h"
  6. #include "qemu-common.h"
  7. #include "cpu.h"
  8. #include "elf.h"
  9. #include "hw/loader.h"
  10. #include "hw/boards.h"
  11. #include "qemu/error-report.h"
  12. #include "sysemu/reset.h"
  13. #include "sysemu/sysemu.h"
  14. #include "hw/rtc/mc146818rtc.h"
  15. #include "hw/timer/i8254.h"
  16. #include "hw/char/serial.h"
  17. #include "hw/net/lasi_82596.h"
  18. #include "hppa_sys.h"
  19. #include "qemu/units.h"
  20. #include "qapi/error.h"
  21. #include "net/net.h"
  22. #include "qemu/log.h"
  23. #include "net/net.h"
  24. #define MAX_IDE_BUS 2
  25. static ISABus *hppa_isa_bus(void)
  26. {
  27. ISABus *isa_bus;
  28. qemu_irq *isa_irqs;
  29. MemoryRegion *isa_region;
  30. isa_region = g_new(MemoryRegion, 1);
  31. memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
  32. NULL, "isa-io", 0x800);
  33. memory_region_add_subregion(get_system_memory(), IDE_HPA,
  34. isa_region);
  35. isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
  36. &error_abort);
  37. isa_irqs = i8259_init(isa_bus,
  38. /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
  39. NULL);
  40. isa_bus_irqs(isa_bus, isa_irqs);
  41. return isa_bus;
  42. }
  43. static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
  44. {
  45. addr &= (0x10000000 - 1);
  46. return addr;
  47. }
  48. static HPPACPU *cpu[HPPA_MAX_CPUS];
  49. static uint64_t firmware_entry;
  50. static void machine_hppa_init(MachineState *machine)
  51. {
  52. const char *kernel_filename = machine->kernel_filename;
  53. const char *kernel_cmdline = machine->kernel_cmdline;
  54. const char *initrd_filename = machine->initrd_filename;
  55. DeviceState *dev;
  56. PCIBus *pci_bus;
  57. ISABus *isa_bus;
  58. qemu_irq rtc_irq, serial_irq;
  59. char *firmware_filename;
  60. uint64_t firmware_low, firmware_high;
  61. long size;
  62. uint64_t kernel_entry = 0, kernel_low, kernel_high;
  63. MemoryRegion *addr_space = get_system_memory();
  64. MemoryRegion *rom_region;
  65. MemoryRegion *cpu_region;
  66. long i;
  67. unsigned int smp_cpus = machine->smp.cpus;
  68. SysBusDevice *s;
  69. /* Create CPUs. */
  70. for (i = 0; i < smp_cpus; i++) {
  71. char *name = g_strdup_printf("cpu%ld-io-eir", i);
  72. cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
  73. cpu_region = g_new(MemoryRegion, 1);
  74. memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
  75. cpu[i], name, 4);
  76. memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
  77. cpu_region);
  78. g_free(name);
  79. }
  80. /* Main memory region. */
  81. if (machine->ram_size > 3 * GiB) {
  82. error_report("RAM size is currently restricted to 3GB");
  83. exit(EXIT_FAILURE);
  84. }
  85. memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
  86. /* Init Lasi chip */
  87. lasi_init(addr_space);
  88. /* Init Dino (PCI host bus chip). */
  89. pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq);
  90. assert(pci_bus);
  91. /* Create ISA bus. */
  92. isa_bus = hppa_isa_bus();
  93. assert(isa_bus);
  94. /* Realtime clock, used by firmware for PDC_TOD call. */
  95. mc146818_rtc_init(isa_bus, 2000, rtc_irq);
  96. /* Serial code setup. */
  97. if (serial_hd(0)) {
  98. uint32_t addr = DINO_UART_HPA + 0x800;
  99. serial_mm_init(addr_space, addr, 0, serial_irq,
  100. 115200, serial_hd(0), DEVICE_BIG_ENDIAN);
  101. }
  102. /* SCSI disk setup. */
  103. dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
  104. lsi53c8xx_handle_legacy_cmdline(dev);
  105. /* Graphics setup. */
  106. if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
  107. dev = qdev_new("artist");
  108. s = SYS_BUS_DEVICE(dev);
  109. sysbus_realize_and_unref(s, &error_fatal);
  110. sysbus_mmio_map(s, 0, LASI_GFX_HPA);
  111. sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
  112. }
  113. /* Network setup. */
  114. for (i = 0; i < nb_nics; i++) {
  115. if (!enable_lasi_lan()) {
  116. pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
  117. }
  118. }
  119. /* Load firmware. Given that this is not "real" firmware,
  120. but one explicitly written for the emulation, we might as
  121. well load it directly from an ELF image. */
  122. firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
  123. bios_name ? bios_name :
  124. "hppa-firmware.img");
  125. if (firmware_filename == NULL) {
  126. error_report("no firmware provided");
  127. exit(1);
  128. }
  129. size = load_elf(firmware_filename, NULL, NULL, NULL,
  130. &firmware_entry, &firmware_low, &firmware_high, NULL,
  131. true, EM_PARISC, 0, 0);
  132. /* Unfortunately, load_elf sign-extends reading elf32. */
  133. firmware_entry = (target_ureg)firmware_entry;
  134. firmware_low = (target_ureg)firmware_low;
  135. firmware_high = (target_ureg)firmware_high;
  136. if (size < 0) {
  137. error_report("could not load firmware '%s'", firmware_filename);
  138. exit(1);
  139. }
  140. qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
  141. "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
  142. firmware_low, firmware_high, firmware_entry);
  143. if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
  144. error_report("Firmware overlaps with memory or IO space");
  145. exit(1);
  146. }
  147. g_free(firmware_filename);
  148. rom_region = g_new(MemoryRegion, 1);
  149. memory_region_init_ram(rom_region, NULL, "firmware",
  150. (FIRMWARE_END - FIRMWARE_START), &error_fatal);
  151. memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
  152. /* Load kernel */
  153. if (kernel_filename) {
  154. size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
  155. NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
  156. true, EM_PARISC, 0, 0);
  157. /* Unfortunately, load_elf sign-extends reading elf32. */
  158. kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
  159. kernel_low = (target_ureg)kernel_low;
  160. kernel_high = (target_ureg)kernel_high;
  161. if (size < 0) {
  162. error_report("could not load kernel '%s'", kernel_filename);
  163. exit(1);
  164. }
  165. qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
  166. "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
  167. ", size %" PRIu64 " kB\n",
  168. kernel_low, kernel_high, kernel_entry, size / KiB);
  169. if (kernel_cmdline) {
  170. cpu[0]->env.gr[24] = 0x4000;
  171. pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
  172. TARGET_PAGE_SIZE, kernel_cmdline);
  173. }
  174. if (initrd_filename) {
  175. ram_addr_t initrd_base;
  176. int64_t initrd_size;
  177. initrd_size = get_image_size(initrd_filename);
  178. if (initrd_size < 0) {
  179. error_report("could not load initial ram disk '%s'",
  180. initrd_filename);
  181. exit(1);
  182. }
  183. /* Load the initrd image high in memory.
  184. Mirror the algorithm used by palo:
  185. (1) Due to sign-extension problems and PDC,
  186. put the initrd no higher than 1G.
  187. (2) Reserve 64k for stack. */
  188. initrd_base = MIN(ram_size, 1 * GiB);
  189. initrd_base = initrd_base - 64 * KiB;
  190. initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
  191. if (initrd_base < kernel_high) {
  192. error_report("kernel and initial ram disk too large!");
  193. exit(1);
  194. }
  195. load_image_targphys(initrd_filename, initrd_base, initrd_size);
  196. cpu[0]->env.gr[23] = initrd_base;
  197. cpu[0]->env.gr[22] = initrd_base + initrd_size;
  198. }
  199. }
  200. if (!kernel_entry) {
  201. /* When booting via firmware, tell firmware if we want interactive
  202. * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
  203. * or hard disc * (gr[24]='c').
  204. */
  205. kernel_entry = boot_menu ? 1 : 0;
  206. cpu[0]->env.gr[24] = machine->boot_order[0];
  207. }
  208. /* We jump to the firmware entry routine and pass the
  209. * various parameters in registers. After firmware initialization,
  210. * firmware will start the Linux kernel with ramdisk and cmdline.
  211. */
  212. cpu[0]->env.gr[26] = ram_size;
  213. cpu[0]->env.gr[25] = kernel_entry;
  214. /* tell firmware how many SMP CPUs to present in inventory table */
  215. cpu[0]->env.gr[21] = smp_cpus;
  216. }
  217. static void hppa_machine_reset(MachineState *ms)
  218. {
  219. unsigned int smp_cpus = ms->smp.cpus;
  220. int i;
  221. qemu_devices_reset();
  222. /* Start all CPUs at the firmware entry point.
  223. * Monarch CPU will initialize firmware, secondary CPUs
  224. * will enter a small idle look and wait for rendevouz. */
  225. for (i = 0; i < smp_cpus; i++) {
  226. cpu_set_pc(CPU(cpu[i]), firmware_entry);
  227. cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
  228. }
  229. /* already initialized by machine_hppa_init()? */
  230. if (cpu[0]->env.gr[26] == ram_size) {
  231. return;
  232. }
  233. cpu[0]->env.gr[26] = ram_size;
  234. cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
  235. cpu[0]->env.gr[24] = 'c';
  236. /* gr22/gr23 unused, no initrd while reboot. */
  237. cpu[0]->env.gr[21] = smp_cpus;
  238. }
  239. static void machine_hppa_machine_init(MachineClass *mc)
  240. {
  241. mc->desc = "HPPA generic machine";
  242. mc->default_cpu_type = TYPE_HPPA_CPU;
  243. mc->init = machine_hppa_init;
  244. mc->reset = hppa_machine_reset;
  245. mc->block_default_type = IF_SCSI;
  246. mc->max_cpus = HPPA_MAX_CPUS;
  247. mc->default_cpus = 1;
  248. mc->is_default = true;
  249. mc->default_ram_size = 512 * MiB;
  250. mc->default_boot_order = "cd";
  251. mc->default_ram_id = "ram";
  252. }
  253. DEFINE_MACHINE("hppa", machine_hppa_machine_init)