pegasos2.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*
  2. * QEMU PowerPC CHRP (Genesi/bPlan Pegasos II) hardware System Emulator
  3. *
  4. * Copyright (c) 2018-2021 BALATON Zoltan
  5. *
  6. * This work is licensed under the GNU GPL license version 2 or later.
  7. *
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/units.h"
  11. #include "qapi/error.h"
  12. #include "hw/ppc/ppc.h"
  13. #include "hw/sysbus.h"
  14. #include "hw/pci/pci_host.h"
  15. #include "hw/irq.h"
  16. #include "hw/or-irq.h"
  17. #include "hw/pci-host/mv64361.h"
  18. #include "hw/isa/vt82c686.h"
  19. #include "hw/ide/pci.h"
  20. #include "hw/i2c/smbus_eeprom.h"
  21. #include "hw/qdev-properties.h"
  22. #include "system/reset.h"
  23. #include "system/runstate.h"
  24. #include "system/qtest.h"
  25. #include "hw/boards.h"
  26. #include "hw/loader.h"
  27. #include "hw/fw-path-provider.h"
  28. #include "elf.h"
  29. #include "qemu/log.h"
  30. #include "qemu/error-report.h"
  31. #include "system/kvm.h"
  32. #include "kvm_ppc.h"
  33. #include "exec/address-spaces.h"
  34. #include "qom/qom-qobject.h"
  35. #include "qobject/qdict.h"
  36. #include "trace.h"
  37. #include "qemu/datadir.h"
  38. #include "system/device_tree.h"
  39. #include "hw/ppc/vof.h"
  40. #include <libfdt.h>
  41. #define PROM_FILENAME "vof.bin"
  42. #define PROM_ADDR 0xfff00000
  43. #define PROM_SIZE 0x80000
  44. #define INITRD_MIN_ADDR 0x600000
  45. #define KVMPPC_HCALL_BASE 0xf000
  46. #define KVMPPC_H_RTAS (KVMPPC_HCALL_BASE + 0x0)
  47. #define KVMPPC_H_VOF_CLIENT (KVMPPC_HCALL_BASE + 0x5)
  48. #define H_SUCCESS 0
  49. #define H_PRIVILEGE -3 /* Caller not privileged */
  50. #define H_PARAMETER -4 /* Parameter invalid, out-of-range or conflicting */
  51. #define BUS_FREQ_HZ 133333333
  52. #define PCI0_CFG_ADDR 0xcf8
  53. #define PCI0_MEM_BASE 0xc0000000
  54. #define PCI0_MEM_SIZE 0x20000000
  55. #define PCI0_IO_BASE 0xf8000000
  56. #define PCI0_IO_SIZE 0x10000
  57. #define PCI1_CFG_ADDR 0xc78
  58. #define PCI1_MEM_BASE 0x80000000
  59. #define PCI1_MEM_SIZE 0x40000000
  60. #define PCI1_IO_BASE 0xfe000000
  61. #define PCI1_IO_SIZE 0x10000
  62. #define TYPE_PEGASOS2_MACHINE MACHINE_TYPE_NAME("pegasos2")
  63. OBJECT_DECLARE_TYPE(Pegasos2MachineState, MachineClass, PEGASOS2_MACHINE)
  64. struct Pegasos2MachineState {
  65. MachineState parent_obj;
  66. PowerPCCPU *cpu;
  67. DeviceState *mv;
  68. IRQState pci_irqs[PCI_NUM_PINS];
  69. OrIRQState orirq[PCI_NUM_PINS];
  70. qemu_irq mv_pirq[PCI_NUM_PINS];
  71. qemu_irq via_pirq[PCI_NUM_PINS];
  72. Vof *vof;
  73. void *fdt_blob;
  74. uint64_t kernel_addr;
  75. uint64_t kernel_entry;
  76. uint64_t kernel_size;
  77. uint64_t initrd_addr;
  78. uint64_t initrd_size;
  79. };
  80. static void *build_fdt(MachineState *machine, int *fdt_size);
  81. static void pegasos2_cpu_reset(void *opaque)
  82. {
  83. PowerPCCPU *cpu = opaque;
  84. Pegasos2MachineState *pm = PEGASOS2_MACHINE(current_machine);
  85. cpu_reset(CPU(cpu));
  86. cpu->env.spr[SPR_HID1] = 7ULL << 28;
  87. if (pm->vof) {
  88. cpu->env.gpr[1] = 2 * VOF_STACK_SIZE - 0x20;
  89. cpu->env.nip = 0x100;
  90. }
  91. cpu_ppc_tb_reset(&cpu->env);
  92. }
  93. static void pegasos2_pci_irq(void *opaque, int n, int level)
  94. {
  95. Pegasos2MachineState *pm = opaque;
  96. /* PCI interrupt lines are connected to both MV64361 and VT8231 */
  97. qemu_set_irq(pm->mv_pirq[n], level);
  98. qemu_set_irq(pm->via_pirq[n], level);
  99. }
  100. static void pegasos2_init(MachineState *machine)
  101. {
  102. Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
  103. CPUPPCState *env;
  104. MemoryRegion *rom = g_new(MemoryRegion, 1);
  105. PCIBus *pci_bus;
  106. Object *via;
  107. PCIDevice *dev;
  108. I2CBus *i2c_bus;
  109. const char *fwname = machine->firmware ?: PROM_FILENAME;
  110. char *filename;
  111. int i;
  112. ssize_t sz;
  113. uint8_t *spd_data;
  114. /* init CPU */
  115. pm->cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
  116. env = &pm->cpu->env;
  117. if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
  118. error_report("Incompatible CPU, only 6xx bus supported");
  119. exit(1);
  120. }
  121. /* Set time-base frequency */
  122. cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4);
  123. qemu_register_reset(pegasos2_cpu_reset, pm->cpu);
  124. /* RAM */
  125. if (machine->ram_size > 2 * GiB) {
  126. error_report("RAM size more than 2 GiB is not supported");
  127. exit(1);
  128. }
  129. memory_region_add_subregion(get_system_memory(), 0, machine->ram);
  130. /* allocate and load firmware */
  131. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname);
  132. if (!filename) {
  133. error_report("Could not find firmware '%s'", fwname);
  134. exit(1);
  135. }
  136. if (!machine->firmware && !pm->vof) {
  137. pm->vof = g_malloc0(sizeof(*pm->vof));
  138. }
  139. memory_region_init_rom(rom, NULL, "pegasos2.rom", PROM_SIZE, &error_fatal);
  140. memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
  141. sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  142. ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
  143. if (sz <= 0) {
  144. sz = load_image_targphys(filename, pm->vof ? 0 : PROM_ADDR, PROM_SIZE);
  145. }
  146. if (sz <= 0 || sz > PROM_SIZE) {
  147. error_report("Could not load firmware '%s'", filename);
  148. exit(1);
  149. }
  150. g_free(filename);
  151. if (pm->vof) {
  152. pm->vof->fw_size = sz;
  153. }
  154. /* Marvell Discovery II system controller */
  155. pm->mv = DEVICE(sysbus_create_simple(TYPE_MV64361, -1,
  156. qdev_get_gpio_in(DEVICE(pm->cpu), PPC6xx_INPUT_INT)));
  157. for (i = 0; i < PCI_NUM_PINS; i++) {
  158. pm->mv_pirq[i] = qdev_get_gpio_in_named(pm->mv, "gpp", 12 + i);
  159. }
  160. pci_bus = mv64361_get_pci_bus(pm->mv, 1);
  161. /* VIA VT8231 South Bridge (multifunction PCI device) */
  162. via = OBJECT(pci_new_multifunction(PCI_DEVFN(12, 0), TYPE_VT8231_ISA));
  163. /* Set properties on individual devices before realizing the south bridge */
  164. if (machine->audiodev) {
  165. dev = PCI_DEVICE(object_resolve_path_component(via, "ac97"));
  166. qdev_prop_set_string(DEVICE(dev), "audiodev", machine->audiodev);
  167. }
  168. pci_realize_and_unref(PCI_DEVICE(via), pci_bus, &error_abort);
  169. for (i = 0; i < PCI_NUM_PINS; i++) {
  170. pm->via_pirq[i] = qdev_get_gpio_in_named(DEVICE(via), "pirq", i);
  171. }
  172. object_property_add_alias(OBJECT(machine), "rtc-time",
  173. object_resolve_path_component(via, "rtc"),
  174. "date");
  175. qdev_connect_gpio_out_named(DEVICE(via), "intr", 0,
  176. qdev_get_gpio_in_named(pm->mv, "gpp", 31));
  177. dev = PCI_DEVICE(object_resolve_path_component(via, "ide"));
  178. pci_ide_create_devs(dev);
  179. dev = PCI_DEVICE(object_resolve_path_component(via, "pm"));
  180. i2c_bus = I2C_BUS(qdev_get_child_bus(DEVICE(dev), "i2c"));
  181. spd_data = spd_data_generate(DDR, machine->ram_size);
  182. smbus_eeprom_init_one(i2c_bus, 0x57, spd_data);
  183. /* other PC hardware */
  184. pci_vga_init(pci_bus);
  185. /* PCI interrupt routing: lines from pci.0 and pci.1 are ORed */
  186. for (int h = 0; h < 2; h++) {
  187. DeviceState *pd;
  188. g_autofree const char *pn = g_strdup_printf("pcihost%d", h);
  189. pd = DEVICE(object_resolve_path_component(OBJECT(pm->mv), pn));
  190. assert(pd);
  191. for (i = 0; i < PCI_NUM_PINS; i++) {
  192. OrIRQState *ori = &pm->orirq[i];
  193. if (h == 0) {
  194. g_autofree const char *n = g_strdup_printf("pci-orirq[%d]", i);
  195. object_initialize_child_with_props(OBJECT(pm), n,
  196. ori, sizeof(*ori),
  197. TYPE_OR_IRQ, &error_fatal,
  198. "num-lines", "2", NULL);
  199. qdev_realize(DEVICE(ori), NULL, &error_fatal);
  200. qemu_init_irq(&pm->pci_irqs[i], pegasos2_pci_irq, pm, i);
  201. qdev_connect_gpio_out(DEVICE(ori), 0, &pm->pci_irqs[i]);
  202. }
  203. qdev_connect_gpio_out(pd, i, qdev_get_gpio_in(DEVICE(ori), h));
  204. }
  205. }
  206. if (machine->kernel_filename) {
  207. sz = load_elf(machine->kernel_filename, NULL, NULL, NULL,
  208. &pm->kernel_entry, &pm->kernel_addr, NULL, NULL,
  209. ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
  210. if (sz <= 0) {
  211. error_report("Could not load kernel '%s'",
  212. machine->kernel_filename);
  213. exit(1);
  214. }
  215. pm->kernel_size = sz;
  216. if (!pm->vof) {
  217. warn_report("Option -kernel may be ineffective with -bios.");
  218. }
  219. } else if (pm->vof && !qtest_enabled()) {
  220. warn_report("Using Virtual OpenFirmware but no -kernel option.");
  221. }
  222. if (machine->initrd_filename) {
  223. pm->initrd_addr = pm->kernel_addr + pm->kernel_size + 64 * KiB;
  224. pm->initrd_addr = ROUND_UP(pm->initrd_addr, 4);
  225. pm->initrd_addr = MAX(pm->initrd_addr, INITRD_MIN_ADDR);
  226. sz = load_image_targphys(machine->initrd_filename, pm->initrd_addr,
  227. machine->ram_size - pm->initrd_addr);
  228. if (sz <= 0) {
  229. error_report("Could not load initrd '%s'",
  230. machine->initrd_filename);
  231. exit(1);
  232. }
  233. pm->initrd_size = sz;
  234. }
  235. if (!pm->vof && machine->kernel_cmdline && machine->kernel_cmdline[0]) {
  236. warn_report("Option -append may be ineffective with -bios.");
  237. }
  238. }
  239. static uint32_t pegasos2_mv_reg_read(Pegasos2MachineState *pm,
  240. uint32_t addr, uint32_t len)
  241. {
  242. MemoryRegion *r = sysbus_mmio_get_region(SYS_BUS_DEVICE(pm->mv), 0);
  243. uint64_t val = 0xffffffffULL;
  244. memory_region_dispatch_read(r, addr, &val, size_memop(len) | MO_LE,
  245. MEMTXATTRS_UNSPECIFIED);
  246. return val;
  247. }
  248. static void pegasos2_mv_reg_write(Pegasos2MachineState *pm, uint32_t addr,
  249. uint32_t len, uint32_t val)
  250. {
  251. MemoryRegion *r = sysbus_mmio_get_region(SYS_BUS_DEVICE(pm->mv), 0);
  252. memory_region_dispatch_write(r, addr, val, size_memop(len) | MO_LE,
  253. MEMTXATTRS_UNSPECIFIED);
  254. }
  255. static uint32_t pegasos2_pci_config_read(Pegasos2MachineState *pm, int bus,
  256. uint32_t addr, uint32_t len)
  257. {
  258. hwaddr pcicfg = bus ? PCI1_CFG_ADDR : PCI0_CFG_ADDR;
  259. uint64_t val = 0xffffffffULL;
  260. if (len <= 4) {
  261. pegasos2_mv_reg_write(pm, pcicfg, 4, addr | BIT(31));
  262. val = pegasos2_mv_reg_read(pm, pcicfg + 4, len);
  263. }
  264. return val;
  265. }
  266. static void pegasos2_pci_config_write(Pegasos2MachineState *pm, int bus,
  267. uint32_t addr, uint32_t len, uint32_t val)
  268. {
  269. hwaddr pcicfg = bus ? PCI1_CFG_ADDR : PCI0_CFG_ADDR;
  270. pegasos2_mv_reg_write(pm, pcicfg, 4, addr | BIT(31));
  271. pegasos2_mv_reg_write(pm, pcicfg + 4, len, val);
  272. }
  273. static void pegasos2_superio_write(uint8_t addr, uint8_t val)
  274. {
  275. cpu_physical_memory_write(PCI1_IO_BASE + 0x3f0, &addr, 1);
  276. cpu_physical_memory_write(PCI1_IO_BASE + 0x3f1, &val, 1);
  277. }
  278. static void pegasos2_machine_reset(MachineState *machine, ResetType type)
  279. {
  280. Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
  281. void *fdt;
  282. uint64_t d[2];
  283. int sz;
  284. qemu_devices_reset(type);
  285. if (!pm->vof) {
  286. return; /* Firmware should set up machine so nothing to do */
  287. }
  288. /* Otherwise, set up devices that board firmware would normally do */
  289. pegasos2_mv_reg_write(pm, 0, 4, 0x28020ff);
  290. pegasos2_mv_reg_write(pm, 0x278, 4, 0xa31fc);
  291. pegasos2_mv_reg_write(pm, 0xf300, 4, 0x11ff0400);
  292. pegasos2_mv_reg_write(pm, 0xf10c, 4, 0x80000000);
  293. pegasos2_mv_reg_write(pm, 0x1c, 4, 0x8000000);
  294. pegasos2_pci_config_write(pm, 0, PCI_COMMAND, 2, PCI_COMMAND_IO |
  295. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  296. pegasos2_pci_config_write(pm, 1, PCI_COMMAND, 2, PCI_COMMAND_IO |
  297. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  298. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
  299. PCI_INTERRUPT_LINE, 2, 0x9);
  300. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
  301. 0x50, 1, 0x6);
  302. pegasos2_superio_write(0xf4, 0xbe);
  303. pegasos2_superio_write(0xf6, 0xef);
  304. pegasos2_superio_write(0xf7, 0xfc);
  305. pegasos2_superio_write(0xf2, 0x14);
  306. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
  307. 0x50, 1, 0x2);
  308. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
  309. 0x55, 1, 0x90);
  310. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
  311. 0x56, 1, 0x99);
  312. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
  313. 0x57, 1, 0x90);
  314. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
  315. PCI_INTERRUPT_LINE, 2, 0x109);
  316. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
  317. PCI_CLASS_PROG, 1, 0xf);
  318. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
  319. 0x40, 1, 0xb);
  320. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
  321. 0x50, 4, 0x17171717);
  322. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
  323. PCI_COMMAND, 2, 0x87);
  324. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 2) << 8) |
  325. PCI_INTERRUPT_LINE, 2, 0x409);
  326. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 2) << 8) |
  327. PCI_COMMAND, 2, 0x7);
  328. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 3) << 8) |
  329. PCI_INTERRUPT_LINE, 2, 0x409);
  330. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 3) << 8) |
  331. PCI_COMMAND, 2, 0x7);
  332. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
  333. PCI_INTERRUPT_LINE, 2, 0x9);
  334. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
  335. 0x48, 4, 0xf00);
  336. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
  337. 0x40, 4, 0x558020);
  338. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
  339. 0x90, 4, 0xd00);
  340. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 5) << 8) |
  341. PCI_INTERRUPT_LINE, 2, 0x309);
  342. pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 6) << 8) |
  343. PCI_INTERRUPT_LINE, 2, 0x309);
  344. /* Device tree and VOF set up */
  345. vof_init(pm->vof, machine->ram_size, &error_fatal);
  346. if (vof_claim(pm->vof, 0, VOF_STACK_SIZE, VOF_STACK_SIZE) == -1) {
  347. error_report("Memory allocation for stack failed");
  348. exit(1);
  349. }
  350. if (pm->kernel_size &&
  351. vof_claim(pm->vof, pm->kernel_addr, pm->kernel_size, 0) == -1) {
  352. error_report("Memory for kernel is in use");
  353. exit(1);
  354. }
  355. if (pm->initrd_size &&
  356. vof_claim(pm->vof, pm->initrd_addr, pm->initrd_size, 0) == -1) {
  357. error_report("Memory for initrd is in use");
  358. exit(1);
  359. }
  360. fdt = build_fdt(machine, &sz);
  361. /* FIXME: VOF assumes entry is same as load address */
  362. d[0] = cpu_to_be64(pm->kernel_entry);
  363. d[1] = cpu_to_be64(pm->kernel_size - (pm->kernel_entry - pm->kernel_addr));
  364. qemu_fdt_setprop(fdt, "/chosen", "qemu,boot-kernel", d, sizeof(d));
  365. qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
  366. g_free(pm->fdt_blob);
  367. pm->fdt_blob = fdt;
  368. vof_build_dt(fdt, pm->vof);
  369. vof_client_open_store(fdt, pm->vof, "/chosen", "stdout", "/failsafe");
  370. /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
  371. machine->fdt = fdt;
  372. pm->cpu->vhyp = PPC_VIRTUAL_HYPERVISOR(machine);
  373. pm->cpu->vhyp_class = PPC_VIRTUAL_HYPERVISOR_GET_CLASS(pm->cpu->vhyp);
  374. }
  375. enum pegasos2_rtas_tokens {
  376. RTAS_RESTART_RTAS = 0,
  377. RTAS_NVRAM_FETCH = 1,
  378. RTAS_NVRAM_STORE = 2,
  379. RTAS_GET_TIME_OF_DAY = 3,
  380. RTAS_SET_TIME_OF_DAY = 4,
  381. RTAS_EVENT_SCAN = 6,
  382. RTAS_CHECK_EXCEPTION = 7,
  383. RTAS_READ_PCI_CONFIG = 8,
  384. RTAS_WRITE_PCI_CONFIG = 9,
  385. RTAS_DISPLAY_CHARACTER = 10,
  386. RTAS_SET_INDICATOR = 11,
  387. RTAS_POWER_OFF = 17,
  388. RTAS_SUSPEND = 18,
  389. RTAS_HIBERNATE = 19,
  390. RTAS_SYSTEM_REBOOT = 20,
  391. };
  392. static target_ulong pegasos2_rtas(PowerPCCPU *cpu, Pegasos2MachineState *pm,
  393. target_ulong args_real)
  394. {
  395. AddressSpace *as = CPU(cpu)->as;
  396. uint32_t token = ldl_be_phys(as, args_real);
  397. uint32_t nargs = ldl_be_phys(as, args_real + 4);
  398. uint32_t nrets = ldl_be_phys(as, args_real + 8);
  399. uint32_t args = args_real + 12;
  400. uint32_t rets = args_real + 12 + nargs * 4;
  401. if (nrets < 1) {
  402. qemu_log_mask(LOG_GUEST_ERROR, "Too few return values in RTAS call\n");
  403. return H_PARAMETER;
  404. }
  405. switch (token) {
  406. case RTAS_GET_TIME_OF_DAY:
  407. {
  408. QObject *qo = object_property_get_qobject(qdev_get_machine(),
  409. "rtc-time", &error_fatal);
  410. QDict *qd = qobject_to(QDict, qo);
  411. if (nargs != 0 || nrets != 8 || !qd) {
  412. stl_be_phys(as, rets, -1);
  413. qobject_unref(qo);
  414. return H_PARAMETER;
  415. }
  416. stl_be_phys(as, rets, 0);
  417. stl_be_phys(as, rets + 4, qdict_get_int(qd, "tm_year") + 1900);
  418. stl_be_phys(as, rets + 8, qdict_get_int(qd, "tm_mon") + 1);
  419. stl_be_phys(as, rets + 12, qdict_get_int(qd, "tm_mday"));
  420. stl_be_phys(as, rets + 16, qdict_get_int(qd, "tm_hour"));
  421. stl_be_phys(as, rets + 20, qdict_get_int(qd, "tm_min"));
  422. stl_be_phys(as, rets + 24, qdict_get_int(qd, "tm_sec"));
  423. stl_be_phys(as, rets + 28, 0);
  424. qobject_unref(qo);
  425. return H_SUCCESS;
  426. }
  427. case RTAS_READ_PCI_CONFIG:
  428. {
  429. uint32_t addr, len, val;
  430. if (nargs != 2 || nrets != 2) {
  431. stl_be_phys(as, rets, -1);
  432. return H_PARAMETER;
  433. }
  434. addr = ldl_be_phys(as, args);
  435. len = ldl_be_phys(as, args + 4);
  436. val = pegasos2_pci_config_read(pm, !(addr >> 24),
  437. addr & 0x0fffffff, len);
  438. stl_be_phys(as, rets, 0);
  439. stl_be_phys(as, rets + 4, val);
  440. return H_SUCCESS;
  441. }
  442. case RTAS_WRITE_PCI_CONFIG:
  443. {
  444. uint32_t addr, len, val;
  445. if (nargs != 3 || nrets != 1) {
  446. stl_be_phys(as, rets, -1);
  447. return H_PARAMETER;
  448. }
  449. addr = ldl_be_phys(as, args);
  450. len = ldl_be_phys(as, args + 4);
  451. val = ldl_be_phys(as, args + 8);
  452. pegasos2_pci_config_write(pm, !(addr >> 24),
  453. addr & 0x0fffffff, len, val);
  454. stl_be_phys(as, rets, 0);
  455. return H_SUCCESS;
  456. }
  457. case RTAS_DISPLAY_CHARACTER:
  458. if (nargs != 1 || nrets != 1) {
  459. stl_be_phys(as, rets, -1);
  460. return H_PARAMETER;
  461. }
  462. qemu_log_mask(LOG_UNIMP, "%c", ldl_be_phys(as, args));
  463. stl_be_phys(as, rets, 0);
  464. return H_SUCCESS;
  465. case RTAS_POWER_OFF:
  466. {
  467. if (nargs != 2 || nrets != 1) {
  468. stl_be_phys(as, rets, -1);
  469. return H_PARAMETER;
  470. }
  471. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  472. stl_be_phys(as, rets, 0);
  473. return H_SUCCESS;
  474. }
  475. default:
  476. qemu_log_mask(LOG_UNIMP, "Unknown RTAS token %u (args=%u, rets=%u)\n",
  477. token, nargs, nrets);
  478. stl_be_phys(as, rets, 0);
  479. return H_SUCCESS;
  480. }
  481. }
  482. static bool pegasos2_cpu_in_nested(PowerPCCPU *cpu)
  483. {
  484. return false;
  485. }
  486. static void pegasos2_hypercall(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
  487. {
  488. Pegasos2MachineState *pm = PEGASOS2_MACHINE(vhyp);
  489. CPUPPCState *env = &cpu->env;
  490. /* The TCG path should also be holding the BQL at this point */
  491. g_assert(bql_locked());
  492. if (FIELD_EX64(env->msr, MSR, PR)) {
  493. qemu_log_mask(LOG_GUEST_ERROR, "Hypercall made with MSR[PR]=1\n");
  494. env->gpr[3] = H_PRIVILEGE;
  495. } else if (env->gpr[3] == KVMPPC_H_RTAS) {
  496. env->gpr[3] = pegasos2_rtas(cpu, pm, env->gpr[4]);
  497. } else if (env->gpr[3] == KVMPPC_H_VOF_CLIENT) {
  498. int ret = vof_client_call(MACHINE(pm), pm->vof, pm->fdt_blob,
  499. env->gpr[4]);
  500. env->gpr[3] = (ret ? H_PARAMETER : H_SUCCESS);
  501. } else {
  502. qemu_log_mask(LOG_GUEST_ERROR, "Unsupported hypercall " TARGET_FMT_lx
  503. "\n", env->gpr[3]);
  504. env->gpr[3] = -1;
  505. }
  506. }
  507. static void vhyp_nop(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
  508. {
  509. }
  510. static target_ulong vhyp_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
  511. {
  512. return POWERPC_CPU(current_cpu)->env.spr[SPR_SDR1];
  513. }
  514. static bool pegasos2_setprop(MachineState *ms, const char *path,
  515. const char *propname, void *val, int vallen)
  516. {
  517. return true;
  518. }
  519. static void pegasos2_machine_class_init(ObjectClass *oc, void *data)
  520. {
  521. MachineClass *mc = MACHINE_CLASS(oc);
  522. PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
  523. VofMachineIfClass *vmc = VOF_MACHINE_CLASS(oc);
  524. mc->desc = "Genesi/bPlan Pegasos II";
  525. mc->init = pegasos2_init;
  526. mc->reset = pegasos2_machine_reset;
  527. mc->block_default_type = IF_IDE;
  528. mc->default_boot_order = "cd";
  529. mc->default_display = "std";
  530. mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7457_v1.2");
  531. mc->default_ram_id = "pegasos2.ram";
  532. mc->default_ram_size = 512 * MiB;
  533. machine_add_audiodev_property(mc);
  534. vhc->cpu_in_nested = pegasos2_cpu_in_nested;
  535. vhc->hypercall = pegasos2_hypercall;
  536. vhc->cpu_exec_enter = vhyp_nop;
  537. vhc->cpu_exec_exit = vhyp_nop;
  538. vhc->encode_hpt_for_kvm_pr = vhyp_encode_hpt_for_kvm_pr;
  539. vmc->setprop = pegasos2_setprop;
  540. }
  541. static const TypeInfo pegasos2_machine_info = {
  542. .name = TYPE_PEGASOS2_MACHINE,
  543. .parent = TYPE_MACHINE,
  544. .class_init = pegasos2_machine_class_init,
  545. .instance_size = sizeof(Pegasos2MachineState),
  546. .interfaces = (InterfaceInfo[]) {
  547. { TYPE_PPC_VIRTUAL_HYPERVISOR },
  548. { TYPE_VOF_MACHINE_IF },
  549. { }
  550. },
  551. };
  552. static void pegasos2_machine_register_types(void)
  553. {
  554. type_register_static(&pegasos2_machine_info);
  555. }
  556. type_init(pegasos2_machine_register_types)
  557. /* FDT creation for passing to firmware */
  558. typedef struct {
  559. void *fdt;
  560. const char *path;
  561. } FDTInfo;
  562. /* We do everything in reverse order so it comes out right in the tree */
  563. static void dt_ide(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
  564. {
  565. qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "spi");
  566. }
  567. static void dt_usb(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
  568. {
  569. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 0);
  570. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 1);
  571. qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "usb");
  572. }
  573. static void dt_isa(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
  574. {
  575. GString *name = g_string_sized_new(64);
  576. uint32_t cells[3];
  577. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 1);
  578. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 2);
  579. qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "isa");
  580. qemu_fdt_setprop_string(fi->fdt, fi->path, "name", "isa");
  581. /* additional devices */
  582. g_string_printf(name, "%s/lpt@i3bc", fi->path);
  583. qemu_fdt_add_subnode(fi->fdt, name->str);
  584. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  585. cells[0] = cpu_to_be32(7);
  586. cells[1] = 0;
  587. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  588. cells, 2 * sizeof(cells[0]));
  589. cells[0] = cpu_to_be32(1);
  590. cells[1] = cpu_to_be32(0x3bc);
  591. cells[2] = cpu_to_be32(8);
  592. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  593. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "lpt");
  594. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "lpt");
  595. g_string_printf(name, "%s/fdc@i3f0", fi->path);
  596. qemu_fdt_add_subnode(fi->fdt, name->str);
  597. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  598. cells[0] = cpu_to_be32(6);
  599. cells[1] = 0;
  600. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  601. cells, 2 * sizeof(cells[0]));
  602. cells[0] = cpu_to_be32(1);
  603. cells[1] = cpu_to_be32(0x3f0);
  604. cells[2] = cpu_to_be32(8);
  605. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  606. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "fdc");
  607. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "fdc");
  608. g_string_printf(name, "%s/timer@i40", fi->path);
  609. qemu_fdt_add_subnode(fi->fdt, name->str);
  610. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  611. cells[0] = cpu_to_be32(1);
  612. cells[1] = cpu_to_be32(0x40);
  613. cells[2] = cpu_to_be32(8);
  614. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  615. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "timer");
  616. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "timer");
  617. g_string_printf(name, "%s/rtc@i70", fi->path);
  618. qemu_fdt_add_subnode(fi->fdt, name->str);
  619. qemu_fdt_setprop_string(fi->fdt, name->str, "compatible", "ds1385-rtc");
  620. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  621. cells[0] = cpu_to_be32(8);
  622. cells[1] = 0;
  623. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  624. cells, 2 * sizeof(cells[0]));
  625. cells[0] = cpu_to_be32(1);
  626. cells[1] = cpu_to_be32(0x70);
  627. cells[2] = cpu_to_be32(2);
  628. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  629. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "rtc");
  630. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "rtc");
  631. g_string_printf(name, "%s/keyboard@i60", fi->path);
  632. qemu_fdt_add_subnode(fi->fdt, name->str);
  633. cells[0] = cpu_to_be32(1);
  634. cells[1] = 0;
  635. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  636. cells, 2 * sizeof(cells[0]));
  637. cells[0] = cpu_to_be32(1);
  638. cells[1] = cpu_to_be32(0x60);
  639. cells[2] = cpu_to_be32(5);
  640. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  641. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "keyboard");
  642. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "keyboard");
  643. g_string_printf(name, "%s/8042@i60", fi->path);
  644. qemu_fdt_add_subnode(fi->fdt, name->str);
  645. qemu_fdt_setprop_cell(fi->fdt, name->str, "#interrupt-cells", 2);
  646. qemu_fdt_setprop_cell(fi->fdt, name->str, "#size-cells", 0);
  647. qemu_fdt_setprop_cell(fi->fdt, name->str, "#address-cells", 1);
  648. qemu_fdt_setprop_string(fi->fdt, name->str, "interrupt-controller", "");
  649. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  650. cells[0] = cpu_to_be32(1);
  651. cells[1] = cpu_to_be32(0x60);
  652. cells[2] = cpu_to_be32(5);
  653. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  654. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "");
  655. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "8042");
  656. g_string_printf(name, "%s/serial@i2f8", fi->path);
  657. qemu_fdt_add_subnode(fi->fdt, name->str);
  658. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  659. cells[0] = cpu_to_be32(3);
  660. cells[1] = 0;
  661. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  662. cells, 2 * sizeof(cells[0]));
  663. cells[0] = cpu_to_be32(1);
  664. cells[1] = cpu_to_be32(0x2f8);
  665. cells[2] = cpu_to_be32(8);
  666. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  667. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "serial");
  668. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "serial");
  669. g_string_free(name, TRUE);
  670. }
  671. static struct {
  672. const char *id;
  673. const char *name;
  674. void (*dtf)(PCIBus *bus, PCIDevice *d, FDTInfo *fi);
  675. } device_map[] = {
  676. { "pci11ab,6460", "host", NULL },
  677. { "pci1106,8231", "isa", dt_isa },
  678. { "pci1106,571", "ide", dt_ide },
  679. { "pci1106,3044", "firewire", NULL },
  680. { "pci1106,3038", "usb", dt_usb },
  681. { "pci1106,8235", "other", NULL },
  682. { "pci1106,3058", "sound", NULL },
  683. { NULL, NULL }
  684. };
  685. static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
  686. {
  687. FDTInfo *fi = opaque;
  688. GString *node = g_string_new(NULL);
  689. uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
  690. int i, j;
  691. const char *name = NULL;
  692. g_autofree const gchar *pn = g_strdup_printf("pci%x,%x",
  693. pci_get_word(&d->config[PCI_VENDOR_ID]),
  694. pci_get_word(&d->config[PCI_DEVICE_ID]));
  695. if (pci_get_word(&d->config[PCI_CLASS_DEVICE]) ==
  696. PCI_CLASS_NETWORK_ETHERNET) {
  697. name = "ethernet";
  698. } else if (pci_get_word(&d->config[PCI_CLASS_DEVICE]) >> 8 ==
  699. PCI_BASE_CLASS_DISPLAY) {
  700. name = "display";
  701. }
  702. for (i = 0; device_map[i].id; i++) {
  703. if (!strcmp(pn, device_map[i].id)) {
  704. name = device_map[i].name;
  705. break;
  706. }
  707. }
  708. g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
  709. PCI_SLOT(d->devfn));
  710. if (PCI_FUNC(d->devfn)) {
  711. g_string_append_printf(node, ",%x", PCI_FUNC(d->devfn));
  712. }
  713. qemu_fdt_add_subnode(fi->fdt, node->str);
  714. if (device_map[i].dtf) {
  715. FDTInfo cfi = { fi->fdt, node->str };
  716. device_map[i].dtf(bus, d, &cfi);
  717. }
  718. cells[0] = cpu_to_be32(d->devfn << 8);
  719. cells[1] = 0;
  720. cells[2] = 0;
  721. cells[3] = 0;
  722. cells[4] = 0;
  723. j = 5;
  724. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  725. if (!d->io_regions[i].size) {
  726. continue;
  727. }
  728. cells[j] = PCI_BASE_ADDRESS_0 + i * 4;
  729. if (cells[j] == 0x28) {
  730. cells[j] = 0x30;
  731. }
  732. cells[j] = cpu_to_be32(d->devfn << 8 | cells[j]);
  733. if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
  734. cells[j] |= cpu_to_be32(1 << 24);
  735. } else {
  736. if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  737. cells[j] |= cpu_to_be32(3 << 24);
  738. } else {
  739. cells[j] |= cpu_to_be32(2 << 24);
  740. }
  741. if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  742. cells[j] |= cpu_to_be32(4 << 28);
  743. }
  744. }
  745. cells[j + 1] = 0;
  746. cells[j + 2] = 0;
  747. cells[j + 3] = cpu_to_be32(d->io_regions[i].size >> 32);
  748. cells[j + 4] = cpu_to_be32(d->io_regions[i].size);
  749. j += 5;
  750. }
  751. qemu_fdt_setprop(fi->fdt, node->str, "reg", cells, j * sizeof(cells[0]));
  752. qemu_fdt_setprop_string(fi->fdt, node->str, "name", name ?: pn);
  753. if (pci_get_byte(&d->config[PCI_INTERRUPT_PIN])) {
  754. qemu_fdt_setprop_cell(fi->fdt, node->str, "interrupts",
  755. pci_get_byte(&d->config[PCI_INTERRUPT_PIN]));
  756. }
  757. /* Pegasos2 firmware has subsystem-id amd subsystem-vendor-id swapped */
  758. qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-vendor-id",
  759. pci_get_word(&d->config[PCI_SUBSYSTEM_ID]));
  760. qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-id",
  761. pci_get_word(&d->config[PCI_SUBSYSTEM_VENDOR_ID]));
  762. cells[0] = pci_get_long(&d->config[PCI_CLASS_REVISION]);
  763. qemu_fdt_setprop_cell(fi->fdt, node->str, "class-code", cells[0] >> 8);
  764. qemu_fdt_setprop_cell(fi->fdt, node->str, "revision-id", cells[0] & 0xff);
  765. qemu_fdt_setprop_cell(fi->fdt, node->str, "device-id",
  766. pci_get_word(&d->config[PCI_DEVICE_ID]));
  767. qemu_fdt_setprop_cell(fi->fdt, node->str, "vendor-id",
  768. pci_get_word(&d->config[PCI_VENDOR_ID]));
  769. g_string_free(node, TRUE);
  770. }
  771. static void *build_fdt(MachineState *machine, int *fdt_size)
  772. {
  773. Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
  774. PowerPCCPU *cpu = pm->cpu;
  775. PCIBus *pci_bus;
  776. FDTInfo fi;
  777. uint32_t cells[16];
  778. void *fdt = create_device_tree(fdt_size);
  779. fi.fdt = fdt;
  780. /* root node */
  781. qemu_fdt_setprop_string(fdt, "/", "CODEGEN,description",
  782. "Pegasos CHRP PowerPC System");
  783. qemu_fdt_setprop_string(fdt, "/", "CODEGEN,board", "Pegasos2");
  784. qemu_fdt_setprop_string(fdt, "/", "CODEGEN,vendor", "bplan GmbH");
  785. qemu_fdt_setprop_string(fdt, "/", "revision", "2B");
  786. qemu_fdt_setprop_string(fdt, "/", "model", "Pegasos2");
  787. qemu_fdt_setprop_string(fdt, "/", "device_type", "chrp");
  788. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 1);
  789. qemu_fdt_setprop_string(fdt, "/", "name", "bplan,Pegasos2");
  790. /* pci@c0000000 */
  791. qemu_fdt_add_subnode(fdt, "/pci@c0000000");
  792. cells[0] = 0;
  793. cells[1] = 0;
  794. qemu_fdt_setprop(fdt, "/pci@c0000000", "bus-range",
  795. cells, 2 * sizeof(cells[0]));
  796. qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "pci-bridge-number", 1);
  797. cells[0] = cpu_to_be32(PCI0_MEM_BASE);
  798. cells[1] = cpu_to_be32(PCI0_MEM_SIZE);
  799. qemu_fdt_setprop(fdt, "/pci@c0000000", "reg", cells, 2 * sizeof(cells[0]));
  800. cells[0] = cpu_to_be32(0x01000000);
  801. cells[1] = 0;
  802. cells[2] = 0;
  803. cells[3] = cpu_to_be32(PCI0_IO_BASE);
  804. cells[4] = 0;
  805. cells[5] = cpu_to_be32(PCI0_IO_SIZE);
  806. cells[6] = cpu_to_be32(0x02000000);
  807. cells[7] = 0;
  808. cells[8] = cpu_to_be32(PCI0_MEM_BASE);
  809. cells[9] = cpu_to_be32(PCI0_MEM_BASE);
  810. cells[10] = 0;
  811. cells[11] = cpu_to_be32(PCI0_MEM_SIZE);
  812. qemu_fdt_setprop(fdt, "/pci@c0000000", "ranges",
  813. cells, 12 * sizeof(cells[0]));
  814. qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#size-cells", 2);
  815. qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#address-cells", 3);
  816. qemu_fdt_setprop_string(fdt, "/pci@c0000000", "device_type", "pci");
  817. qemu_fdt_setprop_string(fdt, "/pci@c0000000", "name", "pci");
  818. fi.path = "/pci@c0000000";
  819. pci_bus = mv64361_get_pci_bus(pm->mv, 0);
  820. pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
  821. /* pci@80000000 */
  822. qemu_fdt_add_subnode(fdt, "/pci@80000000");
  823. cells[0] = 0;
  824. cells[1] = 0;
  825. qemu_fdt_setprop(fdt, "/pci@80000000", "bus-range",
  826. cells, 2 * sizeof(cells[0]));
  827. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "pci-bridge-number", 0);
  828. cells[0] = cpu_to_be32(PCI1_MEM_BASE);
  829. cells[1] = cpu_to_be32(PCI1_MEM_SIZE);
  830. qemu_fdt_setprop(fdt, "/pci@80000000", "reg", cells, 2 * sizeof(cells[0]));
  831. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "8259-interrupt-acknowledge",
  832. 0xf1000cb4);
  833. cells[0] = cpu_to_be32(0x01000000);
  834. cells[1] = 0;
  835. cells[2] = 0;
  836. cells[3] = cpu_to_be32(PCI1_IO_BASE);
  837. cells[4] = 0;
  838. cells[5] = cpu_to_be32(PCI1_IO_SIZE);
  839. cells[6] = cpu_to_be32(0x02000000);
  840. cells[7] = 0;
  841. cells[8] = cpu_to_be32(PCI1_MEM_BASE);
  842. cells[9] = cpu_to_be32(PCI1_MEM_BASE);
  843. cells[10] = 0;
  844. cells[11] = cpu_to_be32(PCI1_MEM_SIZE);
  845. qemu_fdt_setprop(fdt, "/pci@80000000", "ranges",
  846. cells, 12 * sizeof(cells[0]));
  847. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#size-cells", 2);
  848. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#address-cells", 3);
  849. qemu_fdt_setprop_string(fdt, "/pci@80000000", "device_type", "pci");
  850. qemu_fdt_setprop_string(fdt, "/pci@80000000", "name", "pci");
  851. fi.path = "/pci@80000000";
  852. pci_bus = mv64361_get_pci_bus(pm->mv, 1);
  853. pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
  854. qemu_fdt_add_subnode(fdt, "/failsafe");
  855. qemu_fdt_setprop_string(fdt, "/failsafe", "device_type", "serial");
  856. qemu_fdt_setprop_string(fdt, "/failsafe", "name", "failsafe");
  857. qemu_fdt_add_subnode(fdt, "/rtas");
  858. qemu_fdt_setprop_cell(fdt, "/rtas", "system-reboot", RTAS_SYSTEM_REBOOT);
  859. qemu_fdt_setprop_cell(fdt, "/rtas", "hibernate", RTAS_HIBERNATE);
  860. qemu_fdt_setprop_cell(fdt, "/rtas", "suspend", RTAS_SUSPEND);
  861. qemu_fdt_setprop_cell(fdt, "/rtas", "power-off", RTAS_POWER_OFF);
  862. qemu_fdt_setprop_cell(fdt, "/rtas", "set-indicator", RTAS_SET_INDICATOR);
  863. qemu_fdt_setprop_cell(fdt, "/rtas", "display-character",
  864. RTAS_DISPLAY_CHARACTER);
  865. qemu_fdt_setprop_cell(fdt, "/rtas", "write-pci-config",
  866. RTAS_WRITE_PCI_CONFIG);
  867. qemu_fdt_setprop_cell(fdt, "/rtas", "read-pci-config",
  868. RTAS_READ_PCI_CONFIG);
  869. /* Pegasos2 firmware misspells check-exception and guests use that */
  870. qemu_fdt_setprop_cell(fdt, "/rtas", "check-execption",
  871. RTAS_CHECK_EXCEPTION);
  872. qemu_fdt_setprop_cell(fdt, "/rtas", "event-scan", RTAS_EVENT_SCAN);
  873. qemu_fdt_setprop_cell(fdt, "/rtas", "set-time-of-day",
  874. RTAS_SET_TIME_OF_DAY);
  875. qemu_fdt_setprop_cell(fdt, "/rtas", "get-time-of-day",
  876. RTAS_GET_TIME_OF_DAY);
  877. qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-store", RTAS_NVRAM_STORE);
  878. qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-fetch", RTAS_NVRAM_FETCH);
  879. qemu_fdt_setprop_cell(fdt, "/rtas", "restart-rtas", RTAS_RESTART_RTAS);
  880. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-error-log-max", 0);
  881. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-event-scan-rate", 0);
  882. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-display-device", 0);
  883. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size", 20);
  884. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-version", 1);
  885. qemu_fdt_setprop_string(fdt, "/rtas", "name", "rtas");
  886. /* cpus */
  887. qemu_fdt_add_subnode(fdt, "/cpus");
  888. qemu_fdt_setprop_cell(fdt, "/cpus", "#cpus", 1);
  889. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
  890. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
  891. qemu_fdt_setprop_string(fdt, "/cpus", "name", "cpus");
  892. /* FIXME Get CPU name from CPU object */
  893. const char *cp = "/cpus/PowerPC,G4";
  894. qemu_fdt_add_subnode(fdt, cp);
  895. qemu_fdt_setprop_cell(fdt, cp, "l2cr", 0);
  896. qemu_fdt_setprop_cell(fdt, cp, "d-cache-size", 0x8000);
  897. qemu_fdt_setprop_cell(fdt, cp, "d-cache-block-size",
  898. cpu->env.dcache_line_size);
  899. qemu_fdt_setprop_cell(fdt, cp, "d-cache-line-size",
  900. cpu->env.dcache_line_size);
  901. qemu_fdt_setprop_cell(fdt, cp, "i-cache-size", 0x8000);
  902. qemu_fdt_setprop_cell(fdt, cp, "i-cache-block-size",
  903. cpu->env.icache_line_size);
  904. qemu_fdt_setprop_cell(fdt, cp, "i-cache-line-size",
  905. cpu->env.icache_line_size);
  906. if (ppc_is_split_tlb(cpu)) {
  907. qemu_fdt_setprop_cell(fdt, cp, "i-tlb-sets", cpu->env.nb_ways);
  908. qemu_fdt_setprop_cell(fdt, cp, "i-tlb-size", cpu->env.tlb_per_way);
  909. qemu_fdt_setprop_cell(fdt, cp, "d-tlb-sets", cpu->env.nb_ways);
  910. qemu_fdt_setprop_cell(fdt, cp, "d-tlb-size", cpu->env.tlb_per_way);
  911. qemu_fdt_setprop_string(fdt, cp, "tlb-split", "");
  912. }
  913. qemu_fdt_setprop_cell(fdt, cp, "tlb-sets", cpu->env.nb_ways);
  914. qemu_fdt_setprop_cell(fdt, cp, "tlb-size", cpu->env.nb_tlb);
  915. qemu_fdt_setprop_string(fdt, cp, "state", "running");
  916. if (cpu->env.insns_flags & PPC_ALTIVEC) {
  917. qemu_fdt_setprop_string(fdt, cp, "altivec", "");
  918. qemu_fdt_setprop_string(fdt, cp, "data-streams", "");
  919. }
  920. /*
  921. * FIXME What flags do data-streams, external-control and
  922. * performance-monitor depend on?
  923. */
  924. qemu_fdt_setprop_string(fdt, cp, "external-control", "");
  925. if (cpu->env.insns_flags & PPC_FLOAT_FSQRT) {
  926. qemu_fdt_setprop_string(fdt, cp, "general-purpose", "");
  927. }
  928. qemu_fdt_setprop_string(fdt, cp, "performance-monitor", "");
  929. if (cpu->env.insns_flags & PPC_FLOAT_FRES) {
  930. qemu_fdt_setprop_string(fdt, cp, "graphics", "");
  931. }
  932. qemu_fdt_setprop_cell(fdt, cp, "reservation-granule-size", 4);
  933. qemu_fdt_setprop_cell(fdt, cp, "timebase-frequency",
  934. cpu->env.tb_env->tb_freq);
  935. qemu_fdt_setprop_cell(fdt, cp, "bus-frequency", BUS_FREQ_HZ);
  936. qemu_fdt_setprop_cell(fdt, cp, "clock-frequency", BUS_FREQ_HZ * 7.5);
  937. qemu_fdt_setprop_cell(fdt, cp, "cpu-version", cpu->env.spr[SPR_PVR]);
  938. cells[0] = 0;
  939. cells[1] = 0;
  940. qemu_fdt_setprop(fdt, cp, "reg", cells, 2 * sizeof(cells[0]));
  941. qemu_fdt_setprop_string(fdt, cp, "device_type", "cpu");
  942. qemu_fdt_setprop_string(fdt, cp, "name", strrchr(cp, '/') + 1);
  943. /* memory */
  944. qemu_fdt_add_subnode(fdt, "/memory@0");
  945. cells[0] = 0;
  946. cells[1] = cpu_to_be32(machine->ram_size);
  947. qemu_fdt_setprop(fdt, "/memory@0", "reg", cells, 2 * sizeof(cells[0]));
  948. qemu_fdt_setprop_string(fdt, "/memory@0", "device_type", "memory");
  949. qemu_fdt_setprop_string(fdt, "/memory@0", "name", "memory");
  950. qemu_fdt_add_subnode(fdt, "/chosen");
  951. if (pm->initrd_addr && pm->initrd_size) {
  952. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  953. pm->initrd_addr + pm->initrd_size);
  954. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  955. pm->initrd_addr);
  956. }
  957. qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  958. machine->kernel_cmdline ?: "");
  959. qemu_fdt_setprop_string(fdt, "/chosen", "name", "chosen");
  960. qemu_fdt_add_subnode(fdt, "/openprom");
  961. qemu_fdt_setprop_string(fdt, "/openprom", "model", "Pegasos2,1.1");
  962. return fdt;
  963. }