pegasos2.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. g_free(pm->fdt_blob);
  366. pm->fdt_blob = fdt;
  367. vof_build_dt(fdt, pm->vof);
  368. vof_client_open_store(fdt, pm->vof, "/chosen", "stdout", "/failsafe");
  369. /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
  370. machine->fdt = fdt;
  371. pm->cpu->vhyp = PPC_VIRTUAL_HYPERVISOR(machine);
  372. pm->cpu->vhyp_class = PPC_VIRTUAL_HYPERVISOR_GET_CLASS(pm->cpu->vhyp);
  373. }
  374. enum pegasos2_rtas_tokens {
  375. RTAS_RESTART_RTAS = 0,
  376. RTAS_NVRAM_FETCH = 1,
  377. RTAS_NVRAM_STORE = 2,
  378. RTAS_GET_TIME_OF_DAY = 3,
  379. RTAS_SET_TIME_OF_DAY = 4,
  380. RTAS_EVENT_SCAN = 6,
  381. RTAS_CHECK_EXCEPTION = 7,
  382. RTAS_READ_PCI_CONFIG = 8,
  383. RTAS_WRITE_PCI_CONFIG = 9,
  384. RTAS_DISPLAY_CHARACTER = 10,
  385. RTAS_SET_INDICATOR = 11,
  386. RTAS_POWER_OFF = 17,
  387. RTAS_SUSPEND = 18,
  388. RTAS_HIBERNATE = 19,
  389. RTAS_SYSTEM_REBOOT = 20,
  390. };
  391. static target_ulong pegasos2_rtas(PowerPCCPU *cpu, Pegasos2MachineState *pm,
  392. target_ulong args_real)
  393. {
  394. AddressSpace *as = CPU(cpu)->as;
  395. uint32_t token = ldl_be_phys(as, args_real);
  396. uint32_t nargs = ldl_be_phys(as, args_real + 4);
  397. uint32_t nrets = ldl_be_phys(as, args_real + 8);
  398. uint32_t args = args_real + 12;
  399. uint32_t rets = args_real + 12 + nargs * 4;
  400. if (nrets < 1) {
  401. qemu_log_mask(LOG_GUEST_ERROR, "Too few return values in RTAS call\n");
  402. return H_PARAMETER;
  403. }
  404. switch (token) {
  405. case RTAS_GET_TIME_OF_DAY:
  406. {
  407. QObject *qo = object_property_get_qobject(qdev_get_machine(),
  408. "rtc-time", &error_fatal);
  409. QDict *qd = qobject_to(QDict, qo);
  410. if (nargs != 0 || nrets != 8 || !qd) {
  411. stl_be_phys(as, rets, -1);
  412. qobject_unref(qo);
  413. return H_PARAMETER;
  414. }
  415. stl_be_phys(as, rets, 0);
  416. stl_be_phys(as, rets + 4, qdict_get_int(qd, "tm_year") + 1900);
  417. stl_be_phys(as, rets + 8, qdict_get_int(qd, "tm_mon") + 1);
  418. stl_be_phys(as, rets + 12, qdict_get_int(qd, "tm_mday"));
  419. stl_be_phys(as, rets + 16, qdict_get_int(qd, "tm_hour"));
  420. stl_be_phys(as, rets + 20, qdict_get_int(qd, "tm_min"));
  421. stl_be_phys(as, rets + 24, qdict_get_int(qd, "tm_sec"));
  422. stl_be_phys(as, rets + 28, 0);
  423. qobject_unref(qo);
  424. return H_SUCCESS;
  425. }
  426. case RTAS_READ_PCI_CONFIG:
  427. {
  428. uint32_t addr, len, val;
  429. if (nargs != 2 || nrets != 2) {
  430. stl_be_phys(as, rets, -1);
  431. return H_PARAMETER;
  432. }
  433. addr = ldl_be_phys(as, args);
  434. len = ldl_be_phys(as, args + 4);
  435. val = pegasos2_pci_config_read(pm, !(addr >> 24),
  436. addr & 0x0fffffff, len);
  437. stl_be_phys(as, rets, 0);
  438. stl_be_phys(as, rets + 4, val);
  439. return H_SUCCESS;
  440. }
  441. case RTAS_WRITE_PCI_CONFIG:
  442. {
  443. uint32_t addr, len, val;
  444. if (nargs != 3 || nrets != 1) {
  445. stl_be_phys(as, rets, -1);
  446. return H_PARAMETER;
  447. }
  448. addr = ldl_be_phys(as, args);
  449. len = ldl_be_phys(as, args + 4);
  450. val = ldl_be_phys(as, args + 8);
  451. pegasos2_pci_config_write(pm, !(addr >> 24),
  452. addr & 0x0fffffff, len, val);
  453. stl_be_phys(as, rets, 0);
  454. return H_SUCCESS;
  455. }
  456. case RTAS_DISPLAY_CHARACTER:
  457. if (nargs != 1 || nrets != 1) {
  458. stl_be_phys(as, rets, -1);
  459. return H_PARAMETER;
  460. }
  461. qemu_log_mask(LOG_UNIMP, "%c", ldl_be_phys(as, args));
  462. stl_be_phys(as, rets, 0);
  463. return H_SUCCESS;
  464. case RTAS_POWER_OFF:
  465. {
  466. if (nargs != 2 || nrets != 1) {
  467. stl_be_phys(as, rets, -1);
  468. return H_PARAMETER;
  469. }
  470. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  471. stl_be_phys(as, rets, 0);
  472. return H_SUCCESS;
  473. }
  474. default:
  475. qemu_log_mask(LOG_UNIMP, "Unknown RTAS token %u (args=%u, rets=%u)\n",
  476. token, nargs, nrets);
  477. stl_be_phys(as, rets, 0);
  478. return H_SUCCESS;
  479. }
  480. }
  481. static bool pegasos2_cpu_in_nested(PowerPCCPU *cpu)
  482. {
  483. return false;
  484. }
  485. static void pegasos2_hypercall(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
  486. {
  487. Pegasos2MachineState *pm = PEGASOS2_MACHINE(vhyp);
  488. CPUPPCState *env = &cpu->env;
  489. /* The TCG path should also be holding the BQL at this point */
  490. g_assert(bql_locked());
  491. if (FIELD_EX64(env->msr, MSR, PR)) {
  492. qemu_log_mask(LOG_GUEST_ERROR, "Hypercall made with MSR[PR]=1\n");
  493. env->gpr[3] = H_PRIVILEGE;
  494. } else if (env->gpr[3] == KVMPPC_H_RTAS) {
  495. env->gpr[3] = pegasos2_rtas(cpu, pm, env->gpr[4]);
  496. } else if (env->gpr[3] == KVMPPC_H_VOF_CLIENT) {
  497. int ret = vof_client_call(MACHINE(pm), pm->vof, pm->fdt_blob,
  498. env->gpr[4]);
  499. env->gpr[3] = (ret ? H_PARAMETER : H_SUCCESS);
  500. } else {
  501. qemu_log_mask(LOG_GUEST_ERROR, "Unsupported hypercall " TARGET_FMT_lx
  502. "\n", env->gpr[3]);
  503. env->gpr[3] = -1;
  504. }
  505. }
  506. static void vhyp_nop(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
  507. {
  508. }
  509. static target_ulong vhyp_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
  510. {
  511. return POWERPC_CPU(current_cpu)->env.spr[SPR_SDR1];
  512. }
  513. static bool pegasos2_setprop(MachineState *ms, const char *path,
  514. const char *propname, void *val, int vallen)
  515. {
  516. return true;
  517. }
  518. static void pegasos2_machine_class_init(ObjectClass *oc, void *data)
  519. {
  520. MachineClass *mc = MACHINE_CLASS(oc);
  521. PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
  522. VofMachineIfClass *vmc = VOF_MACHINE_CLASS(oc);
  523. mc->desc = "Genesi/bPlan Pegasos II";
  524. mc->init = pegasos2_init;
  525. mc->reset = pegasos2_machine_reset;
  526. mc->block_default_type = IF_IDE;
  527. mc->default_boot_order = "cd";
  528. mc->default_display = "std";
  529. mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7457_v1.2");
  530. mc->default_ram_id = "pegasos2.ram";
  531. mc->default_ram_size = 512 * MiB;
  532. machine_add_audiodev_property(mc);
  533. vhc->cpu_in_nested = pegasos2_cpu_in_nested;
  534. vhc->hypercall = pegasos2_hypercall;
  535. vhc->cpu_exec_enter = vhyp_nop;
  536. vhc->cpu_exec_exit = vhyp_nop;
  537. vhc->encode_hpt_for_kvm_pr = vhyp_encode_hpt_for_kvm_pr;
  538. vmc->setprop = pegasos2_setprop;
  539. }
  540. static const TypeInfo pegasos2_machine_info = {
  541. .name = TYPE_PEGASOS2_MACHINE,
  542. .parent = TYPE_MACHINE,
  543. .class_init = pegasos2_machine_class_init,
  544. .instance_size = sizeof(Pegasos2MachineState),
  545. .interfaces = (InterfaceInfo[]) {
  546. { TYPE_PPC_VIRTUAL_HYPERVISOR },
  547. { TYPE_VOF_MACHINE_IF },
  548. { }
  549. },
  550. };
  551. static void pegasos2_machine_register_types(void)
  552. {
  553. type_register_static(&pegasos2_machine_info);
  554. }
  555. type_init(pegasos2_machine_register_types)
  556. /* FDT creation for passing to firmware */
  557. typedef struct {
  558. void *fdt;
  559. const char *path;
  560. } FDTInfo;
  561. /* We do everything in reverse order so it comes out right in the tree */
  562. static void dt_ide(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
  563. {
  564. qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "spi");
  565. }
  566. static void dt_usb(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
  567. {
  568. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 0);
  569. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 1);
  570. qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "usb");
  571. }
  572. static void dt_isa(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
  573. {
  574. GString *name = g_string_sized_new(64);
  575. uint32_t cells[3];
  576. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 1);
  577. qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 2);
  578. qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "isa");
  579. qemu_fdt_setprop_string(fi->fdt, fi->path, "name", "isa");
  580. /* additional devices */
  581. g_string_printf(name, "%s/lpt@i3bc", fi->path);
  582. qemu_fdt_add_subnode(fi->fdt, name->str);
  583. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  584. cells[0] = cpu_to_be32(7);
  585. cells[1] = 0;
  586. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  587. cells, 2 * sizeof(cells[0]));
  588. cells[0] = cpu_to_be32(1);
  589. cells[1] = cpu_to_be32(0x3bc);
  590. cells[2] = cpu_to_be32(8);
  591. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  592. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "lpt");
  593. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "lpt");
  594. g_string_printf(name, "%s/fdc@i3f0", fi->path);
  595. qemu_fdt_add_subnode(fi->fdt, name->str);
  596. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  597. cells[0] = cpu_to_be32(6);
  598. cells[1] = 0;
  599. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  600. cells, 2 * sizeof(cells[0]));
  601. cells[0] = cpu_to_be32(1);
  602. cells[1] = cpu_to_be32(0x3f0);
  603. cells[2] = cpu_to_be32(8);
  604. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  605. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "fdc");
  606. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "fdc");
  607. g_string_printf(name, "%s/timer@i40", fi->path);
  608. qemu_fdt_add_subnode(fi->fdt, name->str);
  609. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  610. cells[0] = cpu_to_be32(1);
  611. cells[1] = cpu_to_be32(0x40);
  612. cells[2] = cpu_to_be32(8);
  613. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  614. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "timer");
  615. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "timer");
  616. g_string_printf(name, "%s/rtc@i70", fi->path);
  617. qemu_fdt_add_subnode(fi->fdt, name->str);
  618. qemu_fdt_setprop_string(fi->fdt, name->str, "compatible", "ds1385-rtc");
  619. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  620. cells[0] = cpu_to_be32(8);
  621. cells[1] = 0;
  622. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  623. cells, 2 * sizeof(cells[0]));
  624. cells[0] = cpu_to_be32(1);
  625. cells[1] = cpu_to_be32(0x70);
  626. cells[2] = cpu_to_be32(2);
  627. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  628. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "rtc");
  629. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "rtc");
  630. g_string_printf(name, "%s/keyboard@i60", fi->path);
  631. qemu_fdt_add_subnode(fi->fdt, name->str);
  632. cells[0] = cpu_to_be32(1);
  633. cells[1] = 0;
  634. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  635. cells, 2 * sizeof(cells[0]));
  636. cells[0] = cpu_to_be32(1);
  637. cells[1] = cpu_to_be32(0x60);
  638. cells[2] = cpu_to_be32(5);
  639. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  640. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "keyboard");
  641. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "keyboard");
  642. g_string_printf(name, "%s/8042@i60", fi->path);
  643. qemu_fdt_add_subnode(fi->fdt, name->str);
  644. qemu_fdt_setprop_cell(fi->fdt, name->str, "#interrupt-cells", 2);
  645. qemu_fdt_setprop_cell(fi->fdt, name->str, "#size-cells", 0);
  646. qemu_fdt_setprop_cell(fi->fdt, name->str, "#address-cells", 1);
  647. qemu_fdt_setprop_string(fi->fdt, name->str, "interrupt-controller", "");
  648. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  649. cells[0] = cpu_to_be32(1);
  650. cells[1] = cpu_to_be32(0x60);
  651. cells[2] = cpu_to_be32(5);
  652. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  653. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "");
  654. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "8042");
  655. g_string_printf(name, "%s/serial@i2f8", fi->path);
  656. qemu_fdt_add_subnode(fi->fdt, name->str);
  657. qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
  658. cells[0] = cpu_to_be32(3);
  659. cells[1] = 0;
  660. qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
  661. cells, 2 * sizeof(cells[0]));
  662. cells[0] = cpu_to_be32(1);
  663. cells[1] = cpu_to_be32(0x2f8);
  664. cells[2] = cpu_to_be32(8);
  665. qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
  666. qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "serial");
  667. qemu_fdt_setprop_string(fi->fdt, name->str, "name", "serial");
  668. g_string_free(name, TRUE);
  669. }
  670. static struct {
  671. const char *id;
  672. const char *name;
  673. void (*dtf)(PCIBus *bus, PCIDevice *d, FDTInfo *fi);
  674. } device_map[] = {
  675. { "pci11ab,6460", "host", NULL },
  676. { "pci1106,8231", "isa", dt_isa },
  677. { "pci1106,571", "ide", dt_ide },
  678. { "pci1106,3044", "firewire", NULL },
  679. { "pci1106,3038", "usb", dt_usb },
  680. { "pci1106,8235", "other", NULL },
  681. { "pci1106,3058", "sound", NULL },
  682. { NULL, NULL }
  683. };
  684. static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
  685. {
  686. FDTInfo *fi = opaque;
  687. GString *node = g_string_new(NULL);
  688. uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
  689. int i, j;
  690. const char *name = NULL;
  691. g_autofree const gchar *pn = g_strdup_printf("pci%x,%x",
  692. pci_get_word(&d->config[PCI_VENDOR_ID]),
  693. pci_get_word(&d->config[PCI_DEVICE_ID]));
  694. if (pci_get_word(&d->config[PCI_CLASS_DEVICE]) ==
  695. PCI_CLASS_NETWORK_ETHERNET) {
  696. name = "ethernet";
  697. } else if (pci_get_word(&d->config[PCI_CLASS_DEVICE]) >> 8 ==
  698. PCI_BASE_CLASS_DISPLAY) {
  699. name = "display";
  700. }
  701. for (i = 0; device_map[i].id; i++) {
  702. if (!strcmp(pn, device_map[i].id)) {
  703. name = device_map[i].name;
  704. break;
  705. }
  706. }
  707. g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
  708. PCI_SLOT(d->devfn));
  709. if (PCI_FUNC(d->devfn)) {
  710. g_string_append_printf(node, ",%x", PCI_FUNC(d->devfn));
  711. }
  712. qemu_fdt_add_subnode(fi->fdt, node->str);
  713. if (device_map[i].dtf) {
  714. FDTInfo cfi = { fi->fdt, node->str };
  715. device_map[i].dtf(bus, d, &cfi);
  716. }
  717. cells[0] = cpu_to_be32(d->devfn << 8);
  718. cells[1] = 0;
  719. cells[2] = 0;
  720. cells[3] = 0;
  721. cells[4] = 0;
  722. j = 5;
  723. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  724. if (!d->io_regions[i].size) {
  725. continue;
  726. }
  727. cells[j] = PCI_BASE_ADDRESS_0 + i * 4;
  728. if (cells[j] == 0x28) {
  729. cells[j] = 0x30;
  730. }
  731. cells[j] = cpu_to_be32(d->devfn << 8 | cells[j]);
  732. if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
  733. cells[j] |= cpu_to_be32(1 << 24);
  734. } else {
  735. if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  736. cells[j] |= cpu_to_be32(3 << 24);
  737. } else {
  738. cells[j] |= cpu_to_be32(2 << 24);
  739. }
  740. if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  741. cells[j] |= cpu_to_be32(4 << 28);
  742. }
  743. }
  744. cells[j + 1] = 0;
  745. cells[j + 2] = 0;
  746. cells[j + 3] = cpu_to_be32(d->io_regions[i].size >> 32);
  747. cells[j + 4] = cpu_to_be32(d->io_regions[i].size);
  748. j += 5;
  749. }
  750. qemu_fdt_setprop(fi->fdt, node->str, "reg", cells, j * sizeof(cells[0]));
  751. qemu_fdt_setprop_string(fi->fdt, node->str, "name", name ?: pn);
  752. if (pci_get_byte(&d->config[PCI_INTERRUPT_PIN])) {
  753. qemu_fdt_setprop_cell(fi->fdt, node->str, "interrupts",
  754. pci_get_byte(&d->config[PCI_INTERRUPT_PIN]));
  755. }
  756. /* Pegasos2 firmware has subsystem-id amd subsystem-vendor-id swapped */
  757. qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-vendor-id",
  758. pci_get_word(&d->config[PCI_SUBSYSTEM_ID]));
  759. qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-id",
  760. pci_get_word(&d->config[PCI_SUBSYSTEM_VENDOR_ID]));
  761. cells[0] = pci_get_long(&d->config[PCI_CLASS_REVISION]);
  762. qemu_fdt_setprop_cell(fi->fdt, node->str, "class-code", cells[0] >> 8);
  763. qemu_fdt_setprop_cell(fi->fdt, node->str, "revision-id", cells[0] & 0xff);
  764. qemu_fdt_setprop_cell(fi->fdt, node->str, "device-id",
  765. pci_get_word(&d->config[PCI_DEVICE_ID]));
  766. qemu_fdt_setprop_cell(fi->fdt, node->str, "vendor-id",
  767. pci_get_word(&d->config[PCI_VENDOR_ID]));
  768. g_string_free(node, TRUE);
  769. }
  770. static void *build_fdt(MachineState *machine, int *fdt_size)
  771. {
  772. Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
  773. PowerPCCPU *cpu = pm->cpu;
  774. PCIBus *pci_bus;
  775. FDTInfo fi;
  776. uint32_t cells[16];
  777. void *fdt = create_device_tree(fdt_size);
  778. fi.fdt = fdt;
  779. /* root node */
  780. qemu_fdt_setprop_string(fdt, "/", "CODEGEN,description",
  781. "Pegasos CHRP PowerPC System");
  782. qemu_fdt_setprop_string(fdt, "/", "CODEGEN,board", "Pegasos2");
  783. qemu_fdt_setprop_string(fdt, "/", "CODEGEN,vendor", "bplan GmbH");
  784. qemu_fdt_setprop_string(fdt, "/", "revision", "2B");
  785. qemu_fdt_setprop_string(fdt, "/", "model", "Pegasos2");
  786. qemu_fdt_setprop_string(fdt, "/", "device_type", "chrp");
  787. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 1);
  788. qemu_fdt_setprop_string(fdt, "/", "name", "bplan,Pegasos2");
  789. /* pci@c0000000 */
  790. qemu_fdt_add_subnode(fdt, "/pci@c0000000");
  791. cells[0] = 0;
  792. cells[1] = 0;
  793. qemu_fdt_setprop(fdt, "/pci@c0000000", "bus-range",
  794. cells, 2 * sizeof(cells[0]));
  795. qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "pci-bridge-number", 1);
  796. cells[0] = cpu_to_be32(PCI0_MEM_BASE);
  797. cells[1] = cpu_to_be32(PCI0_MEM_SIZE);
  798. qemu_fdt_setprop(fdt, "/pci@c0000000", "reg", cells, 2 * sizeof(cells[0]));
  799. cells[0] = cpu_to_be32(0x01000000);
  800. cells[1] = 0;
  801. cells[2] = 0;
  802. cells[3] = cpu_to_be32(PCI0_IO_BASE);
  803. cells[4] = 0;
  804. cells[5] = cpu_to_be32(PCI0_IO_SIZE);
  805. cells[6] = cpu_to_be32(0x02000000);
  806. cells[7] = 0;
  807. cells[8] = cpu_to_be32(PCI0_MEM_BASE);
  808. cells[9] = cpu_to_be32(PCI0_MEM_BASE);
  809. cells[10] = 0;
  810. cells[11] = cpu_to_be32(PCI0_MEM_SIZE);
  811. qemu_fdt_setprop(fdt, "/pci@c0000000", "ranges",
  812. cells, 12 * sizeof(cells[0]));
  813. qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#size-cells", 2);
  814. qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#address-cells", 3);
  815. qemu_fdt_setprop_string(fdt, "/pci@c0000000", "device_type", "pci");
  816. qemu_fdt_setprop_string(fdt, "/pci@c0000000", "name", "pci");
  817. fi.path = "/pci@c0000000";
  818. pci_bus = mv64361_get_pci_bus(pm->mv, 0);
  819. pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
  820. /* pci@80000000 */
  821. qemu_fdt_add_subnode(fdt, "/pci@80000000");
  822. cells[0] = 0;
  823. cells[1] = 0;
  824. qemu_fdt_setprop(fdt, "/pci@80000000", "bus-range",
  825. cells, 2 * sizeof(cells[0]));
  826. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "pci-bridge-number", 0);
  827. cells[0] = cpu_to_be32(PCI1_MEM_BASE);
  828. cells[1] = cpu_to_be32(PCI1_MEM_SIZE);
  829. qemu_fdt_setprop(fdt, "/pci@80000000", "reg", cells, 2 * sizeof(cells[0]));
  830. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "8259-interrupt-acknowledge",
  831. 0xf1000cb4);
  832. cells[0] = cpu_to_be32(0x01000000);
  833. cells[1] = 0;
  834. cells[2] = 0;
  835. cells[3] = cpu_to_be32(PCI1_IO_BASE);
  836. cells[4] = 0;
  837. cells[5] = cpu_to_be32(PCI1_IO_SIZE);
  838. cells[6] = cpu_to_be32(0x02000000);
  839. cells[7] = 0;
  840. cells[8] = cpu_to_be32(PCI1_MEM_BASE);
  841. cells[9] = cpu_to_be32(PCI1_MEM_BASE);
  842. cells[10] = 0;
  843. cells[11] = cpu_to_be32(PCI1_MEM_SIZE);
  844. qemu_fdt_setprop(fdt, "/pci@80000000", "ranges",
  845. cells, 12 * sizeof(cells[0]));
  846. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#size-cells", 2);
  847. qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#address-cells", 3);
  848. qemu_fdt_setprop_string(fdt, "/pci@80000000", "device_type", "pci");
  849. qemu_fdt_setprop_string(fdt, "/pci@80000000", "name", "pci");
  850. fi.path = "/pci@80000000";
  851. pci_bus = mv64361_get_pci_bus(pm->mv, 1);
  852. pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
  853. qemu_fdt_add_subnode(fdt, "/failsafe");
  854. qemu_fdt_setprop_string(fdt, "/failsafe", "device_type", "serial");
  855. qemu_fdt_setprop_string(fdt, "/failsafe", "name", "failsafe");
  856. qemu_fdt_add_subnode(fdt, "/rtas");
  857. qemu_fdt_setprop_cell(fdt, "/rtas", "system-reboot", RTAS_SYSTEM_REBOOT);
  858. qemu_fdt_setprop_cell(fdt, "/rtas", "hibernate", RTAS_HIBERNATE);
  859. qemu_fdt_setprop_cell(fdt, "/rtas", "suspend", RTAS_SUSPEND);
  860. qemu_fdt_setprop_cell(fdt, "/rtas", "power-off", RTAS_POWER_OFF);
  861. qemu_fdt_setprop_cell(fdt, "/rtas", "set-indicator", RTAS_SET_INDICATOR);
  862. qemu_fdt_setprop_cell(fdt, "/rtas", "display-character",
  863. RTAS_DISPLAY_CHARACTER);
  864. qemu_fdt_setprop_cell(fdt, "/rtas", "write-pci-config",
  865. RTAS_WRITE_PCI_CONFIG);
  866. qemu_fdt_setprop_cell(fdt, "/rtas", "read-pci-config",
  867. RTAS_READ_PCI_CONFIG);
  868. /* Pegasos2 firmware misspells check-exception and guests use that */
  869. qemu_fdt_setprop_cell(fdt, "/rtas", "check-execption",
  870. RTAS_CHECK_EXCEPTION);
  871. qemu_fdt_setprop_cell(fdt, "/rtas", "event-scan", RTAS_EVENT_SCAN);
  872. qemu_fdt_setprop_cell(fdt, "/rtas", "set-time-of-day",
  873. RTAS_SET_TIME_OF_DAY);
  874. qemu_fdt_setprop_cell(fdt, "/rtas", "get-time-of-day",
  875. RTAS_GET_TIME_OF_DAY);
  876. qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-store", RTAS_NVRAM_STORE);
  877. qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-fetch", RTAS_NVRAM_FETCH);
  878. qemu_fdt_setprop_cell(fdt, "/rtas", "restart-rtas", RTAS_RESTART_RTAS);
  879. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-error-log-max", 0);
  880. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-event-scan-rate", 0);
  881. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-display-device", 0);
  882. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size", 20);
  883. qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-version", 1);
  884. qemu_fdt_setprop_string(fdt, "/rtas", "name", "rtas");
  885. /* cpus */
  886. qemu_fdt_add_subnode(fdt, "/cpus");
  887. qemu_fdt_setprop_cell(fdt, "/cpus", "#cpus", 1);
  888. qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
  889. qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
  890. qemu_fdt_setprop_string(fdt, "/cpus", "name", "cpus");
  891. /* FIXME Get CPU name from CPU object */
  892. const char *cp = "/cpus/PowerPC,G4";
  893. qemu_fdt_add_subnode(fdt, cp);
  894. qemu_fdt_setprop_cell(fdt, cp, "l2cr", 0);
  895. qemu_fdt_setprop_cell(fdt, cp, "d-cache-size", 0x8000);
  896. qemu_fdt_setprop_cell(fdt, cp, "d-cache-block-size",
  897. cpu->env.dcache_line_size);
  898. qemu_fdt_setprop_cell(fdt, cp, "d-cache-line-size",
  899. cpu->env.dcache_line_size);
  900. qemu_fdt_setprop_cell(fdt, cp, "i-cache-size", 0x8000);
  901. qemu_fdt_setprop_cell(fdt, cp, "i-cache-block-size",
  902. cpu->env.icache_line_size);
  903. qemu_fdt_setprop_cell(fdt, cp, "i-cache-line-size",
  904. cpu->env.icache_line_size);
  905. if (ppc_is_split_tlb(cpu)) {
  906. qemu_fdt_setprop_cell(fdt, cp, "i-tlb-sets", cpu->env.nb_ways);
  907. qemu_fdt_setprop_cell(fdt, cp, "i-tlb-size", cpu->env.tlb_per_way);
  908. qemu_fdt_setprop_cell(fdt, cp, "d-tlb-sets", cpu->env.nb_ways);
  909. qemu_fdt_setprop_cell(fdt, cp, "d-tlb-size", cpu->env.tlb_per_way);
  910. qemu_fdt_setprop_string(fdt, cp, "tlb-split", "");
  911. }
  912. qemu_fdt_setprop_cell(fdt, cp, "tlb-sets", cpu->env.nb_ways);
  913. qemu_fdt_setprop_cell(fdt, cp, "tlb-size", cpu->env.nb_tlb);
  914. qemu_fdt_setprop_string(fdt, cp, "state", "running");
  915. if (cpu->env.insns_flags & PPC_ALTIVEC) {
  916. qemu_fdt_setprop_string(fdt, cp, "altivec", "");
  917. qemu_fdt_setprop_string(fdt, cp, "data-streams", "");
  918. }
  919. /*
  920. * FIXME What flags do data-streams, external-control and
  921. * performance-monitor depend on?
  922. */
  923. qemu_fdt_setprop_string(fdt, cp, "external-control", "");
  924. if (cpu->env.insns_flags & PPC_FLOAT_FSQRT) {
  925. qemu_fdt_setprop_string(fdt, cp, "general-purpose", "");
  926. }
  927. qemu_fdt_setprop_string(fdt, cp, "performance-monitor", "");
  928. if (cpu->env.insns_flags & PPC_FLOAT_FRES) {
  929. qemu_fdt_setprop_string(fdt, cp, "graphics", "");
  930. }
  931. qemu_fdt_setprop_cell(fdt, cp, "reservation-granule-size", 4);
  932. qemu_fdt_setprop_cell(fdt, cp, "timebase-frequency",
  933. cpu->env.tb_env->tb_freq);
  934. qemu_fdt_setprop_cell(fdt, cp, "bus-frequency", BUS_FREQ_HZ);
  935. qemu_fdt_setprop_cell(fdt, cp, "clock-frequency", BUS_FREQ_HZ * 7.5);
  936. qemu_fdt_setprop_cell(fdt, cp, "cpu-version", cpu->env.spr[SPR_PVR]);
  937. cells[0] = 0;
  938. cells[1] = 0;
  939. qemu_fdt_setprop(fdt, cp, "reg", cells, 2 * sizeof(cells[0]));
  940. qemu_fdt_setprop_string(fdt, cp, "device_type", "cpu");
  941. qemu_fdt_setprop_string(fdt, cp, "name", strrchr(cp, '/') + 1);
  942. /* memory */
  943. qemu_fdt_add_subnode(fdt, "/memory@0");
  944. cells[0] = 0;
  945. cells[1] = cpu_to_be32(machine->ram_size);
  946. qemu_fdt_setprop(fdt, "/memory@0", "reg", cells, 2 * sizeof(cells[0]));
  947. qemu_fdt_setprop_string(fdt, "/memory@0", "device_type", "memory");
  948. qemu_fdt_setprop_string(fdt, "/memory@0", "name", "memory");
  949. qemu_fdt_add_subnode(fdt, "/chosen");
  950. if (pm->initrd_addr && pm->initrd_size) {
  951. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  952. pm->initrd_addr + pm->initrd_size);
  953. qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  954. pm->initrd_addr);
  955. }
  956. qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  957. machine->kernel_cmdline ?: "");
  958. qemu_fdt_setprop_string(fdt, "/chosen", "name", "chosen");
  959. qemu_fdt_add_subnode(fdt, "/openprom");
  960. qemu_fdt_setprop_string(fdt, "/openprom", "model", "Pegasos2,1.1");
  961. return fdt;
  962. }