mac_oldworld.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * QEMU OldWorld PowerMac (currently ~G3 Beige) hardware System Emulator
  3. *
  4. * Copyright (c) 2004-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/datadir.h"
  27. #include "qemu/units.h"
  28. #include "qapi/error.h"
  29. #include "hw/ppc/ppc.h"
  30. #include "hw/qdev-properties.h"
  31. #include "hw/boards.h"
  32. #include "hw/input/adb.h"
  33. #include "system/system.h"
  34. #include "net/net.h"
  35. #include "hw/isa/isa.h"
  36. #include "hw/pci/pci.h"
  37. #include "hw/pci/pci_host.h"
  38. #include "hw/pci-host/grackle.h"
  39. #include "hw/nvram/fw_cfg.h"
  40. #include "hw/char/escc.h"
  41. #include "hw/misc/macio/macio.h"
  42. #include "hw/loader.h"
  43. #include "hw/fw-path-provider.h"
  44. #include "elf.h"
  45. #include "qemu/error-report.h"
  46. #include "system/kvm.h"
  47. #include "system/reset.h"
  48. #include "kvm_ppc.h"
  49. #define MAX_IDE_BUS 2
  50. #define CFG_ADDR 0xf0000510
  51. #define TBFREQ 16600000UL
  52. #define CLOCKFREQ 266000000UL
  53. #define BUSFREQ 66000000UL
  54. #define NDRV_VGA_FILENAME "qemu_vga.ndrv"
  55. #define PROM_FILENAME "openbios-ppc"
  56. #define PROM_BASE 0xffc00000
  57. #define PROM_SIZE (4 * MiB)
  58. #define KERNEL_LOAD_ADDR 0x01000000
  59. #define KERNEL_GAP 0x00100000
  60. #define GRACKLE_BASE 0xfec00000
  61. static void fw_cfg_boot_set(void *opaque, const char *boot_device,
  62. Error **errp)
  63. {
  64. fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
  65. }
  66. static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
  67. {
  68. return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
  69. }
  70. static void ppc_heathrow_reset(void *opaque)
  71. {
  72. PowerPCCPU *cpu = opaque;
  73. cpu_ppc_tb_reset(&cpu->env);
  74. cpu_reset(CPU(cpu));
  75. }
  76. static void ppc_heathrow_init(MachineState *machine)
  77. {
  78. const char *bios_name = machine->firmware ?: PROM_FILENAME;
  79. MachineClass *mc = MACHINE_GET_CLASS(machine);
  80. PowerPCCPU *cpu = NULL;
  81. CPUPPCState *env = NULL;
  82. char *filename;
  83. int i, bios_size = -1;
  84. MemoryRegion *bios = g_new(MemoryRegion, 1);
  85. uint64_t bios_addr;
  86. uint32_t kernel_base = 0, initrd_base = 0, cmdline_base = 0;
  87. int32_t kernel_size = 0, initrd_size = 0;
  88. PCIBus *pci_bus;
  89. Object *macio;
  90. MACIOIDEState *macio_ide;
  91. SysBusDevice *s;
  92. DeviceState *dev, *pic_dev, *grackle_dev;
  93. BusState *adb_bus;
  94. uint16_t ppc_boot_device;
  95. DriveInfo *dinfo, *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  96. void *fw_cfg;
  97. uint64_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TBFREQ;
  98. /* init CPUs */
  99. for (i = 0; i < machine->smp.cpus; i++) {
  100. cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
  101. env = &cpu->env;
  102. /* Set time-base frequency to 16.6 Mhz */
  103. cpu_ppc_tb_init(env, TBFREQ);
  104. qemu_register_reset(ppc_heathrow_reset, cpu);
  105. }
  106. /* allocate RAM */
  107. if (machine->ram_size > 2047 * MiB) {
  108. error_report("Too much memory for this machine: %" PRId64 " MB, "
  109. "maximum 2047 MB", machine->ram_size / MiB);
  110. exit(1);
  111. }
  112. memory_region_add_subregion(get_system_memory(), 0, machine->ram);
  113. /* allocate and load firmware ROM */
  114. memory_region_init_rom(bios, NULL, "ppc_heathrow.bios", PROM_SIZE,
  115. &error_fatal);
  116. memory_region_add_subregion(get_system_memory(), PROM_BASE, bios);
  117. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  118. if (filename) {
  119. /* Load OpenBIOS (ELF) */
  120. bios_size = load_elf(filename, NULL, NULL, NULL, NULL, &bios_addr,
  121. NULL, NULL, ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
  122. /* Unfortunately, load_elf sign-extends reading elf32 */
  123. bios_addr = (uint32_t)bios_addr;
  124. if (bios_size <= 0) {
  125. /* or if could not load ELF try loading a binary ROM image */
  126. bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE);
  127. bios_addr = PROM_BASE;
  128. }
  129. g_free(filename);
  130. }
  131. if (bios_size < 0 || bios_addr - PROM_BASE + bios_size > PROM_SIZE) {
  132. error_report("could not load PowerPC bios '%s'", bios_name);
  133. exit(1);
  134. }
  135. if (machine->kernel_filename) {
  136. kernel_base = KERNEL_LOAD_ADDR;
  137. kernel_size = load_elf(machine->kernel_filename, NULL,
  138. translate_kernel_address, NULL, NULL, NULL,
  139. NULL, NULL, ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
  140. if (kernel_size < 0) {
  141. kernel_size = load_aout(machine->kernel_filename, kernel_base,
  142. machine->ram_size - kernel_base,
  143. true, TARGET_PAGE_SIZE);
  144. }
  145. if (kernel_size < 0) {
  146. kernel_size = load_image_targphys(machine->kernel_filename,
  147. kernel_base,
  148. machine->ram_size - kernel_base);
  149. }
  150. if (kernel_size < 0) {
  151. error_report("could not load kernel '%s'",
  152. machine->kernel_filename);
  153. exit(1);
  154. }
  155. /* load initrd */
  156. if (machine->initrd_filename) {
  157. initrd_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size +
  158. KERNEL_GAP);
  159. initrd_size = load_image_targphys(machine->initrd_filename,
  160. initrd_base,
  161. machine->ram_size - initrd_base);
  162. if (initrd_size < 0) {
  163. error_report("could not load initial ram disk '%s'",
  164. machine->initrd_filename);
  165. exit(1);
  166. }
  167. cmdline_base = TARGET_PAGE_ALIGN(initrd_base + initrd_size);
  168. } else {
  169. cmdline_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + KERNEL_GAP);
  170. }
  171. ppc_boot_device = 'm';
  172. } else {
  173. ppc_boot_device = '\0';
  174. for (i = 0; machine->boot_config.order[i] != '\0'; i++) {
  175. /*
  176. * TOFIX: for now, the second IDE channel is not properly
  177. * used by OHW. The Mac floppy disk are not emulated.
  178. * For now, OHW cannot boot from the network.
  179. */
  180. #if 0
  181. if (machine->boot_config.order[i] >= 'a' &&
  182. machine->boot_config.order[i] <= 'f') {
  183. ppc_boot_device = machine->boot_config.order[i];
  184. break;
  185. }
  186. #else
  187. if (machine->boot_config.order[i] >= 'c' &&
  188. machine->boot_config.order[i] <= 'd') {
  189. ppc_boot_device = machine->boot_config.order[i];
  190. break;
  191. }
  192. #endif
  193. }
  194. if (ppc_boot_device == '\0') {
  195. error_report("No valid boot device for G3 Beige machine");
  196. exit(1);
  197. }
  198. }
  199. /* Grackle PCI host bridge */
  200. grackle_dev = qdev_new(TYPE_GRACKLE_PCI_HOST_BRIDGE);
  201. qdev_prop_set_uint32(grackle_dev, "ofw-addr", 0x80000000);
  202. s = SYS_BUS_DEVICE(grackle_dev);
  203. sysbus_realize_and_unref(s, &error_fatal);
  204. sysbus_mmio_map(s, 0, GRACKLE_BASE);
  205. sysbus_mmio_map(s, 1, GRACKLE_BASE + 0x200000);
  206. /* PCI hole */
  207. memory_region_add_subregion(get_system_memory(), 0x80000000ULL,
  208. sysbus_mmio_get_region(s, 2));
  209. /* Register 2 MB of ISA IO space */
  210. memory_region_add_subregion(get_system_memory(), 0xfe000000,
  211. sysbus_mmio_get_region(s, 3));
  212. pci_bus = PCI_HOST_BRIDGE(grackle_dev)->bus;
  213. /* MacIO */
  214. macio = OBJECT(pci_new(PCI_DEVFN(16, 0), TYPE_OLDWORLD_MACIO));
  215. qdev_prop_set_uint64(DEVICE(macio), "frequency", tbfreq);
  216. dev = DEVICE(object_resolve_path_component(macio, "escc"));
  217. qdev_prop_set_chr(dev, "chrA", serial_hd(0));
  218. qdev_prop_set_chr(dev, "chrB", serial_hd(1));
  219. dinfo = drive_get(IF_MTD, 0, 0);
  220. if (dinfo) {
  221. dev = DEVICE(object_resolve_path_component(macio, "nvram"));
  222. qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo));
  223. }
  224. pci_realize_and_unref(PCI_DEVICE(macio), pci_bus, &error_fatal);
  225. pic_dev = DEVICE(object_resolve_path_component(macio, "pic"));
  226. for (i = 0; i < 4; i++) {
  227. qdev_connect_gpio_out(grackle_dev, i,
  228. qdev_get_gpio_in(pic_dev, 0x15 + i));
  229. }
  230. /* Connect the heathrow PIC outputs to the 6xx bus */
  231. for (i = 0; i < machine->smp.cpus; i++) {
  232. switch (PPC_INPUT(env)) {
  233. case PPC_FLAGS_INPUT_6xx:
  234. /* XXX: we register only 1 output pin for heathrow PIC */
  235. qdev_connect_gpio_out(pic_dev, 0,
  236. qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_INT));
  237. break;
  238. default:
  239. error_report("Bus model not supported on OldWorld Mac machine");
  240. exit(1);
  241. }
  242. }
  243. pci_vga_init(pci_bus);
  244. pci_init_nic_devices(pci_bus, mc->default_nic);
  245. /* MacIO IDE */
  246. ide_drive_get(hd, ARRAY_SIZE(hd));
  247. macio_ide = MACIO_IDE(object_resolve_path_component(macio, "ide[0]"));
  248. macio_ide_init_drives(macio_ide, hd);
  249. macio_ide = MACIO_IDE(object_resolve_path_component(macio, "ide[1]"));
  250. macio_ide_init_drives(macio_ide, &hd[MAX_IDE_DEVS]);
  251. /* MacIO CUDA/ADB */
  252. dev = DEVICE(object_resolve_path_component(macio, "cuda"));
  253. adb_bus = qdev_get_child_bus(dev, "adb.0");
  254. dev = qdev_new(TYPE_ADB_KEYBOARD);
  255. qdev_realize_and_unref(dev, adb_bus, &error_fatal);
  256. dev = qdev_new(TYPE_ADB_MOUSE);
  257. qdev_realize_and_unref(dev, adb_bus, &error_fatal);
  258. if (machine_usb(machine)) {
  259. pci_create_simple(pci_bus, -1, "pci-ohci");
  260. }
  261. if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8) {
  262. graphic_depth = 15;
  263. }
  264. /* No PCI init: the BIOS will do it */
  265. dev = qdev_new(TYPE_FW_CFG_MEM);
  266. fw_cfg = FW_CFG(dev);
  267. qdev_prop_set_uint32(dev, "data_width", 1);
  268. qdev_prop_set_bit(dev, "dma_enabled", false);
  269. object_property_add_child(OBJECT(machine), TYPE_FW_CFG, OBJECT(fw_cfg));
  270. s = SYS_BUS_DEVICE(dev);
  271. sysbus_realize_and_unref(s, &error_fatal);
  272. sysbus_mmio_map(s, 0, CFG_ADDR);
  273. sysbus_mmio_map(s, 1, CFG_ADDR + 2);
  274. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)machine->smp.cpus);
  275. fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)machine->smp.max_cpus);
  276. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size);
  277. fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_HEATHROW);
  278. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
  279. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  280. if (machine->kernel_cmdline) {
  281. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, cmdline_base);
  282. pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
  283. machine->kernel_cmdline);
  284. } else {
  285. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, 0);
  286. }
  287. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
  288. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  289. fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ppc_boot_device);
  290. fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_WIDTH, graphic_width);
  291. fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_HEIGHT, graphic_height);
  292. fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_DEPTH, graphic_depth);
  293. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_IS_KVM, kvm_enabled());
  294. if (kvm_enabled()) {
  295. uint8_t *hypercall;
  296. hypercall = g_malloc(16);
  297. kvmppc_get_hypercall(env, hypercall, 16);
  298. fw_cfg_add_bytes(fw_cfg, FW_CFG_PPC_KVM_HC, hypercall, 16);
  299. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_KVM_PID, getpid());
  300. }
  301. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, tbfreq);
  302. /* Mac OS X requires a "known good" clock-frequency value; pass it one. */
  303. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_CLOCKFREQ, CLOCKFREQ);
  304. fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_BUSFREQ, BUSFREQ);
  305. /* MacOS NDRV VGA driver */
  306. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, NDRV_VGA_FILENAME);
  307. if (filename) {
  308. gchar *ndrv_file;
  309. gsize ndrv_size;
  310. if (g_file_get_contents(filename, &ndrv_file, &ndrv_size, NULL)) {
  311. fw_cfg_add_file(fw_cfg, "ndrv/qemu_vga.ndrv", ndrv_file, ndrv_size);
  312. }
  313. g_free(filename);
  314. }
  315. qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
  316. }
  317. /*
  318. * Implementation of an interface to adjust firmware path
  319. * for the bootindex property handling.
  320. */
  321. static char *heathrow_fw_dev_path(FWPathProvider *p, BusState *bus,
  322. DeviceState *dev)
  323. {
  324. PCIDevice *pci;
  325. MACIOIDEState *macio_ide;
  326. if (!strcmp(object_get_typename(OBJECT(dev)), "macio-oldworld")) {
  327. pci = PCI_DEVICE(dev);
  328. return g_strdup_printf("mac-io@%x", PCI_SLOT(pci->devfn));
  329. }
  330. if (!strcmp(object_get_typename(OBJECT(dev)), "macio-ide")) {
  331. macio_ide = MACIO_IDE(dev);
  332. return g_strdup_printf("ata-3@%x", macio_ide->addr);
  333. }
  334. if (!strcmp(object_get_typename(OBJECT(dev)), "ide-hd")) {
  335. return g_strdup("disk");
  336. }
  337. if (!strcmp(object_get_typename(OBJECT(dev)), "ide-cd")) {
  338. return g_strdup("cdrom");
  339. }
  340. if (!strcmp(object_get_typename(OBJECT(dev)), "virtio-blk-device")) {
  341. return g_strdup("disk");
  342. }
  343. return NULL;
  344. }
  345. static int heathrow_kvm_type(MachineState *machine, const char *arg)
  346. {
  347. /* Always force PR KVM */
  348. return 2;
  349. }
  350. static void heathrow_class_init(ObjectClass *oc, void *data)
  351. {
  352. MachineClass *mc = MACHINE_CLASS(oc);
  353. FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
  354. mc->desc = "Heathrow based PowerMac";
  355. mc->init = ppc_heathrow_init;
  356. mc->block_default_type = IF_IDE;
  357. /* SMP is not supported currently */
  358. mc->max_cpus = 1;
  359. #ifndef TARGET_PPC64
  360. mc->is_default = true;
  361. #endif
  362. /* TOFIX "cad" when Mac floppy is implemented */
  363. mc->default_boot_order = "cd";
  364. mc->kvm_type = heathrow_kvm_type;
  365. mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("750_v3.1");
  366. mc->default_display = "std";
  367. mc->default_nic = "ne2k_pci";
  368. mc->ignore_boot_device_suffixes = true;
  369. mc->default_ram_id = "ppc_heathrow.ram";
  370. fwc->get_dev_path = heathrow_fw_dev_path;
  371. }
  372. static const TypeInfo ppc_heathrow_machine_info = {
  373. .name = MACHINE_TYPE_NAME("g3beige"),
  374. .parent = TYPE_MACHINE,
  375. .class_init = heathrow_class_init,
  376. .interfaces = (InterfaceInfo[]) {
  377. { TYPE_FW_PATH_PROVIDER },
  378. { }
  379. },
  380. };
  381. static void ppc_heathrow_register_types(void)
  382. {
  383. type_register_static(&ppc_heathrow_machine_info);
  384. }
  385. type_init(ppc_heathrow_register_types);