machine.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * QEMU HPPA hardware system emulator.
  3. * Copyright 2018 Helge Deller <deller@gmx.de>
  4. */
  5. #include "qemu/osdep.h"
  6. #include "qemu/datadir.h"
  7. #include "cpu.h"
  8. #include "elf.h"
  9. #include "hw/loader.h"
  10. #include "qemu/error-report.h"
  11. #include "sysemu/reset.h"
  12. #include "sysemu/sysemu.h"
  13. #include "sysemu/runstate.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 "hw/nmi.h"
  19. #include "hppa_sys.h"
  20. #include "qemu/units.h"
  21. #include "qapi/error.h"
  22. #include "net/net.h"
  23. #include "qemu/log.h"
  24. #include "net/net.h"
  25. #define MAX_IDE_BUS 2
  26. #define MIN_SEABIOS_HPPA_VERSION 1 /* require at least this fw version */
  27. #define HPA_POWER_BUTTON (FIRMWARE_END - 0x10)
  28. static void hppa_powerdown_req(Notifier *n, void *opaque)
  29. {
  30. hwaddr soft_power_reg = HPA_POWER_BUTTON;
  31. uint32_t val;
  32. val = ldl_be_phys(&address_space_memory, soft_power_reg);
  33. if ((val >> 8) == 0) {
  34. /* immediately shut down when under hardware control */
  35. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  36. return;
  37. }
  38. /* clear bit 31 to indicate that the power switch was pressed. */
  39. val &= ~1;
  40. stl_be_phys(&address_space_memory, soft_power_reg, val);
  41. }
  42. static Notifier hppa_system_powerdown_notifier = {
  43. .notify = hppa_powerdown_req
  44. };
  45. static ISABus *hppa_isa_bus(void)
  46. {
  47. ISABus *isa_bus;
  48. qemu_irq *isa_irqs;
  49. MemoryRegion *isa_region;
  50. isa_region = g_new(MemoryRegion, 1);
  51. memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
  52. NULL, "isa-io", 0x800);
  53. memory_region_add_subregion(get_system_memory(), IDE_HPA,
  54. isa_region);
  55. isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
  56. &error_abort);
  57. isa_irqs = i8259_init(isa_bus,
  58. /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
  59. NULL);
  60. isa_bus_irqs(isa_bus, isa_irqs);
  61. return isa_bus;
  62. }
  63. static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
  64. {
  65. addr &= (0x10000000 - 1);
  66. return addr;
  67. }
  68. static HPPACPU *cpu[HPPA_MAX_CPUS];
  69. static uint64_t firmware_entry;
  70. static void fw_cfg_boot_set(void *opaque, const char *boot_device,
  71. Error **errp)
  72. {
  73. fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
  74. }
  75. static FWCfgState *create_fw_cfg(MachineState *ms)
  76. {
  77. FWCfgState *fw_cfg;
  78. uint64_t val;
  79. fw_cfg = fw_cfg_init_mem(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4);
  80. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, ms->smp.cpus);
  81. fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, HPPA_MAX_CPUS);
  82. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, ms->ram_size);
  83. val = cpu_to_le64(MIN_SEABIOS_HPPA_VERSION);
  84. fw_cfg_add_file(fw_cfg, "/etc/firmware-min-version",
  85. g_memdup(&val, sizeof(val)), sizeof(val));
  86. val = cpu_to_le64(HPPA_TLB_ENTRIES);
  87. fw_cfg_add_file(fw_cfg, "/etc/cpu/tlb_entries",
  88. g_memdup(&val, sizeof(val)), sizeof(val));
  89. val = cpu_to_le64(HPPA_BTLB_ENTRIES);
  90. fw_cfg_add_file(fw_cfg, "/etc/cpu/btlb_entries",
  91. g_memdup(&val, sizeof(val)), sizeof(val));
  92. val = cpu_to_le64(HPA_POWER_BUTTON);
  93. fw_cfg_add_file(fw_cfg, "/etc/power-button-addr",
  94. g_memdup(&val, sizeof(val)), sizeof(val));
  95. fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ms->boot_order[0]);
  96. qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
  97. return fw_cfg;
  98. }
  99. static void machine_hppa_init(MachineState *machine)
  100. {
  101. const char *kernel_filename = machine->kernel_filename;
  102. const char *kernel_cmdline = machine->kernel_cmdline;
  103. const char *initrd_filename = machine->initrd_filename;
  104. DeviceState *dev;
  105. PCIBus *pci_bus;
  106. ISABus *isa_bus;
  107. qemu_irq rtc_irq, serial_irq;
  108. char *firmware_filename;
  109. uint64_t firmware_low, firmware_high;
  110. long size;
  111. uint64_t kernel_entry = 0, kernel_low, kernel_high;
  112. MemoryRegion *addr_space = get_system_memory();
  113. MemoryRegion *rom_region;
  114. MemoryRegion *cpu_region;
  115. long i;
  116. unsigned int smp_cpus = machine->smp.cpus;
  117. SysBusDevice *s;
  118. /* Create CPUs. */
  119. for (i = 0; i < smp_cpus; i++) {
  120. char *name = g_strdup_printf("cpu%ld-io-eir", i);
  121. cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
  122. cpu_region = g_new(MemoryRegion, 1);
  123. memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
  124. cpu[i], name, 4);
  125. memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
  126. cpu_region);
  127. g_free(name);
  128. }
  129. /* Main memory region. */
  130. if (machine->ram_size > 3 * GiB) {
  131. error_report("RAM size is currently restricted to 3GB");
  132. exit(EXIT_FAILURE);
  133. }
  134. memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
  135. /* Init Lasi chip */
  136. lasi_init(addr_space);
  137. /* Init Dino (PCI host bus chip). */
  138. pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq);
  139. assert(pci_bus);
  140. /* Create ISA bus. */
  141. isa_bus = hppa_isa_bus();
  142. assert(isa_bus);
  143. /* Realtime clock, used by firmware for PDC_TOD call. */
  144. mc146818_rtc_init(isa_bus, 2000, rtc_irq);
  145. /* Serial code setup. */
  146. if (serial_hd(0)) {
  147. uint32_t addr = DINO_UART_HPA + 0x800;
  148. serial_mm_init(addr_space, addr, 0, serial_irq,
  149. 115200, serial_hd(0), DEVICE_BIG_ENDIAN);
  150. }
  151. /* fw_cfg configuration interface */
  152. create_fw_cfg(machine);
  153. /* SCSI disk setup. */
  154. dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
  155. lsi53c8xx_handle_legacy_cmdline(dev);
  156. /* Graphics setup. */
  157. if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
  158. dev = qdev_new("artist");
  159. s = SYS_BUS_DEVICE(dev);
  160. sysbus_realize_and_unref(s, &error_fatal);
  161. sysbus_mmio_map(s, 0, LASI_GFX_HPA);
  162. sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
  163. }
  164. /* Network setup. */
  165. for (i = 0; i < nb_nics; i++) {
  166. if (!enable_lasi_lan()) {
  167. pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
  168. }
  169. }
  170. /* register power switch emulation */
  171. qemu_register_powerdown_notifier(&hppa_system_powerdown_notifier);
  172. /* Load firmware. Given that this is not "real" firmware,
  173. but one explicitly written for the emulation, we might as
  174. well load it directly from an ELF image. */
  175. firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
  176. machine->firmware ?: "hppa-firmware.img");
  177. if (firmware_filename == NULL) {
  178. error_report("no firmware provided");
  179. exit(1);
  180. }
  181. size = load_elf(firmware_filename, NULL, NULL, NULL,
  182. &firmware_entry, &firmware_low, &firmware_high, NULL,
  183. true, EM_PARISC, 0, 0);
  184. /* Unfortunately, load_elf sign-extends reading elf32. */
  185. firmware_entry = (target_ureg)firmware_entry;
  186. firmware_low = (target_ureg)firmware_low;
  187. firmware_high = (target_ureg)firmware_high;
  188. if (size < 0) {
  189. error_report("could not load firmware '%s'", firmware_filename);
  190. exit(1);
  191. }
  192. qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
  193. "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
  194. firmware_low, firmware_high, firmware_entry);
  195. if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
  196. error_report("Firmware overlaps with memory or IO space");
  197. exit(1);
  198. }
  199. g_free(firmware_filename);
  200. rom_region = g_new(MemoryRegion, 1);
  201. memory_region_init_ram(rom_region, NULL, "firmware",
  202. (FIRMWARE_END - FIRMWARE_START), &error_fatal);
  203. memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
  204. /* Load kernel */
  205. if (kernel_filename) {
  206. size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
  207. NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
  208. true, EM_PARISC, 0, 0);
  209. /* Unfortunately, load_elf sign-extends reading elf32. */
  210. kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
  211. kernel_low = (target_ureg)kernel_low;
  212. kernel_high = (target_ureg)kernel_high;
  213. if (size < 0) {
  214. error_report("could not load kernel '%s'", kernel_filename);
  215. exit(1);
  216. }
  217. qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
  218. "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
  219. ", size %" PRIu64 " kB\n",
  220. kernel_low, kernel_high, kernel_entry, size / KiB);
  221. if (kernel_cmdline) {
  222. cpu[0]->env.gr[24] = 0x4000;
  223. pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
  224. TARGET_PAGE_SIZE, kernel_cmdline);
  225. }
  226. if (initrd_filename) {
  227. ram_addr_t initrd_base;
  228. int64_t initrd_size;
  229. initrd_size = get_image_size(initrd_filename);
  230. if (initrd_size < 0) {
  231. error_report("could not load initial ram disk '%s'",
  232. initrd_filename);
  233. exit(1);
  234. }
  235. /* Load the initrd image high in memory.
  236. Mirror the algorithm used by palo:
  237. (1) Due to sign-extension problems and PDC,
  238. put the initrd no higher than 1G.
  239. (2) Reserve 64k for stack. */
  240. initrd_base = MIN(machine->ram_size, 1 * GiB);
  241. initrd_base = initrd_base - 64 * KiB;
  242. initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
  243. if (initrd_base < kernel_high) {
  244. error_report("kernel and initial ram disk too large!");
  245. exit(1);
  246. }
  247. load_image_targphys(initrd_filename, initrd_base, initrd_size);
  248. cpu[0]->env.gr[23] = initrd_base;
  249. cpu[0]->env.gr[22] = initrd_base + initrd_size;
  250. }
  251. }
  252. if (!kernel_entry) {
  253. /* When booting via firmware, tell firmware if we want interactive
  254. * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
  255. * or hard disc * (gr[24]='c').
  256. */
  257. kernel_entry = boot_menu ? 1 : 0;
  258. cpu[0]->env.gr[24] = machine->boot_order[0];
  259. }
  260. /* We jump to the firmware entry routine and pass the
  261. * various parameters in registers. After firmware initialization,
  262. * firmware will start the Linux kernel with ramdisk and cmdline.
  263. */
  264. cpu[0]->env.gr[26] = machine->ram_size;
  265. cpu[0]->env.gr[25] = kernel_entry;
  266. /* tell firmware how many SMP CPUs to present in inventory table */
  267. cpu[0]->env.gr[21] = smp_cpus;
  268. /* tell firmware fw_cfg port */
  269. cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
  270. }
  271. static void hppa_machine_reset(MachineState *ms)
  272. {
  273. unsigned int smp_cpus = ms->smp.cpus;
  274. int i;
  275. qemu_devices_reset();
  276. /* Start all CPUs at the firmware entry point.
  277. * Monarch CPU will initialize firmware, secondary CPUs
  278. * will enter a small idle look and wait for rendevouz. */
  279. for (i = 0; i < smp_cpus; i++) {
  280. cpu_set_pc(CPU(cpu[i]), firmware_entry);
  281. cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
  282. }
  283. /* already initialized by machine_hppa_init()? */
  284. if (cpu[0]->env.gr[26] == ms->ram_size) {
  285. return;
  286. }
  287. cpu[0]->env.gr[26] = ms->ram_size;
  288. cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
  289. cpu[0]->env.gr[24] = 'c';
  290. /* gr22/gr23 unused, no initrd while reboot. */
  291. cpu[0]->env.gr[21] = smp_cpus;
  292. /* tell firmware fw_cfg port */
  293. cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
  294. }
  295. static void hppa_nmi(NMIState *n, int cpu_index, Error **errp)
  296. {
  297. CPUState *cs;
  298. CPU_FOREACH(cs) {
  299. cpu_interrupt(cs, CPU_INTERRUPT_NMI);
  300. }
  301. }
  302. static void machine_hppa_machine_init(MachineClass *mc)
  303. {
  304. mc->desc = "HPPA generic machine";
  305. mc->default_cpu_type = TYPE_HPPA_CPU;
  306. mc->init = machine_hppa_init;
  307. mc->reset = hppa_machine_reset;
  308. mc->block_default_type = IF_SCSI;
  309. mc->max_cpus = HPPA_MAX_CPUS;
  310. mc->default_cpus = 1;
  311. mc->is_default = true;
  312. mc->default_ram_size = 512 * MiB;
  313. mc->default_boot_order = "cd";
  314. mc->default_ram_id = "ram";
  315. }
  316. static void machine_hppa_machine_init_class_init(ObjectClass *oc, void *data)
  317. {
  318. MachineClass *mc = MACHINE_CLASS(oc);
  319. machine_hppa_machine_init(mc);
  320. NMIClass *nc = NMI_CLASS(oc);
  321. nc->nmi_monitor_handler = hppa_nmi;
  322. }
  323. static const TypeInfo machine_hppa_machine_init_typeinfo = {
  324. .name = ("hppa" "-machine"),
  325. .parent = "machine",
  326. .class_init = machine_hppa_machine_init_class_init,
  327. .interfaces = (InterfaceInfo[]) {
  328. { TYPE_NMI },
  329. { }
  330. },
  331. };
  332. static void machine_hppa_machine_init_register_types(void)
  333. {
  334. type_register_static(&machine_hppa_machine_init_typeinfo);
  335. }
  336. type_init(machine_hppa_machine_init_register_types)