machine.c 9.4 KB

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