machine.c 15 KB

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