xref: /qemu/hw/ppc/pegasos2.c (revision d105377264a87573ba4207d05c1641bb3708710a) !
1ba7e5ac1SBALATON Zoltan /*
2ba7e5ac1SBALATON Zoltan  * QEMU PowerPC CHRP (Genesi/bPlan Pegasos II) hardware System Emulator
3ba7e5ac1SBALATON Zoltan  *
4a8eda5edSBALATON Zoltan  * Copyright (c) 2018-2021 BALATON Zoltan
5ba7e5ac1SBALATON Zoltan  *
6ba7e5ac1SBALATON Zoltan  * This work is licensed under the GNU GPL license version 2 or later.
7ba7e5ac1SBALATON Zoltan  *
8ba7e5ac1SBALATON Zoltan  */
9ba7e5ac1SBALATON Zoltan 
10ba7e5ac1SBALATON Zoltan #include "qemu/osdep.h"
11ba7e5ac1SBALATON Zoltan #include "qemu/units.h"
12ba7e5ac1SBALATON Zoltan #include "qapi/error.h"
13ba7e5ac1SBALATON Zoltan #include "hw/hw.h"
14ba7e5ac1SBALATON Zoltan #include "hw/ppc/ppc.h"
15ba7e5ac1SBALATON Zoltan #include "hw/sysbus.h"
16ba7e5ac1SBALATON Zoltan #include "hw/pci/pci_host.h"
17ba7e5ac1SBALATON Zoltan #include "hw/irq.h"
18ba7e5ac1SBALATON Zoltan #include "hw/pci-host/mv64361.h"
19ba7e5ac1SBALATON Zoltan #include "hw/isa/vt82c686.h"
20ba7e5ac1SBALATON Zoltan #include "hw/ide/pci.h"
21ba7e5ac1SBALATON Zoltan #include "hw/i2c/smbus_eeprom.h"
22ba7e5ac1SBALATON Zoltan #include "hw/qdev-properties.h"
23ba7e5ac1SBALATON Zoltan #include "sysemu/reset.h"
24284c0486SBALATON Zoltan #include "sysemu/runstate.h"
2588adcbf2SBALATON Zoltan #include "sysemu/qtest.h"
26ba7e5ac1SBALATON Zoltan #include "hw/boards.h"
27ba7e5ac1SBALATON Zoltan #include "hw/loader.h"
28ba7e5ac1SBALATON Zoltan #include "hw/fw-path-provider.h"
29ba7e5ac1SBALATON Zoltan #include "elf.h"
30ba7e5ac1SBALATON Zoltan #include "qemu/log.h"
31ba7e5ac1SBALATON Zoltan #include "qemu/error-report.h"
32ba7e5ac1SBALATON Zoltan #include "sysemu/kvm.h"
33ba7e5ac1SBALATON Zoltan #include "kvm_ppc.h"
34ba7e5ac1SBALATON Zoltan #include "exec/address-spaces.h"
3594cd1ffbSBALATON Zoltan #include "qom/qom-qobject.h"
3694cd1ffbSBALATON Zoltan #include "qapi/qmp/qdict.h"
37ba7e5ac1SBALATON Zoltan #include "trace.h"
38ba7e5ac1SBALATON Zoltan #include "qemu/datadir.h"
39ba7e5ac1SBALATON Zoltan #include "sysemu/device_tree.h"
40a6c9808aSBALATON Zoltan #include "hw/ppc/vof.h"
41ba7e5ac1SBALATON Zoltan 
42a6c9808aSBALATON Zoltan #include <libfdt.h>
43a6c9808aSBALATON Zoltan 
44a6c9808aSBALATON Zoltan #define PROM_FILENAME "vof.bin"
45ba7e5ac1SBALATON Zoltan #define PROM_ADDR     0xfff00000
46ba7e5ac1SBALATON Zoltan #define PROM_SIZE     0x80000
47ba7e5ac1SBALATON Zoltan 
48a6c9808aSBALATON Zoltan #define KVMPPC_HCALL_BASE    0xf000
495f2eb049SBALATON Zoltan #define KVMPPC_H_RTAS        (KVMPPC_HCALL_BASE + 0x0)
50a6c9808aSBALATON Zoltan #define KVMPPC_H_VOF_CLIENT  (KVMPPC_HCALL_BASE + 0x5)
51a6c9808aSBALATON Zoltan 
52a6c9808aSBALATON Zoltan #define H_SUCCESS     0
53a6c9808aSBALATON Zoltan #define H_PRIVILEGE  -3  /* Caller not privileged */
54a6c9808aSBALATON Zoltan #define H_PARAMETER  -4  /* Parameter invalid, out-of-range or conflicting */
55a6c9808aSBALATON Zoltan 
56ba7e5ac1SBALATON Zoltan #define BUS_FREQ_HZ 133333333
57ba7e5ac1SBALATON Zoltan 
58d200ea14SBALATON Zoltan #define PCI0_CFG_ADDR 0xcf8
59a6c9808aSBALATON Zoltan #define PCI0_MEM_BASE 0xc0000000
60a6c9808aSBALATON Zoltan #define PCI0_MEM_SIZE 0x20000000
61a6c9808aSBALATON Zoltan #define PCI0_IO_BASE  0xf8000000
62a6c9808aSBALATON Zoltan #define PCI0_IO_SIZE  0x10000
63a6c9808aSBALATON Zoltan 
64d200ea14SBALATON Zoltan #define PCI1_CFG_ADDR 0xc78
65a6c9808aSBALATON Zoltan #define PCI1_MEM_BASE 0x80000000
66a6c9808aSBALATON Zoltan #define PCI1_MEM_SIZE 0x40000000
67a6c9808aSBALATON Zoltan #define PCI1_IO_BASE  0xfe000000
68a6c9808aSBALATON Zoltan #define PCI1_IO_SIZE  0x10000
69a6c9808aSBALATON Zoltan 
70a8eda5edSBALATON Zoltan #define TYPE_PEGASOS2_MACHINE  MACHINE_TYPE_NAME("pegasos2")
71a8eda5edSBALATON Zoltan OBJECT_DECLARE_TYPE(Pegasos2MachineState, MachineClass, PEGASOS2_MACHINE)
72a8eda5edSBALATON Zoltan 
73a8eda5edSBALATON Zoltan struct Pegasos2MachineState {
74a8eda5edSBALATON Zoltan     MachineState parent_obj;
75a8eda5edSBALATON Zoltan     PowerPCCPU *cpu;
76a8eda5edSBALATON Zoltan     DeviceState *mv;
77a6c9808aSBALATON Zoltan     Vof *vof;
78a6c9808aSBALATON Zoltan     void *fdt_blob;
79a6c9808aSBALATON Zoltan     uint64_t kernel_addr;
80a6c9808aSBALATON Zoltan     uint64_t kernel_entry;
81a6c9808aSBALATON Zoltan     uint64_t kernel_size;
82a8eda5edSBALATON Zoltan };
83a8eda5edSBALATON Zoltan 
84a6c9808aSBALATON Zoltan static void *build_fdt(MachineState *machine, int *fdt_size);
85a6c9808aSBALATON Zoltan 
86ba7e5ac1SBALATON Zoltan static void pegasos2_cpu_reset(void *opaque)
87ba7e5ac1SBALATON Zoltan {
88ba7e5ac1SBALATON Zoltan     PowerPCCPU *cpu = opaque;
89a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(current_machine);
90ba7e5ac1SBALATON Zoltan 
91ba7e5ac1SBALATON Zoltan     cpu_reset(CPU(cpu));
92ba7e5ac1SBALATON Zoltan     cpu->env.spr[SPR_HID1] = 7ULL << 28;
93a6c9808aSBALATON Zoltan     if (pm->vof) {
94a6c9808aSBALATON Zoltan         cpu->env.gpr[1] = 2 * VOF_STACK_SIZE - 0x20;
95a6c9808aSBALATON Zoltan         cpu->env.nip = 0x100;
96a6c9808aSBALATON Zoltan     }
97ba7e5ac1SBALATON Zoltan }
98ba7e5ac1SBALATON Zoltan 
99ba7e5ac1SBALATON Zoltan static void pegasos2_init(MachineState *machine)
100ba7e5ac1SBALATON Zoltan {
101a8eda5edSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
102a8eda5edSBALATON Zoltan     CPUPPCState *env;
103ba7e5ac1SBALATON Zoltan     MemoryRegion *rom = g_new(MemoryRegion, 1);
104ba7e5ac1SBALATON Zoltan     PCIBus *pci_bus;
1059eb6abbfSBernhard Beschow     PCIDevice *dev, *via;
106ba7e5ac1SBALATON Zoltan     I2CBus *i2c_bus;
107ba7e5ac1SBALATON Zoltan     const char *fwname = machine->firmware ?: PROM_FILENAME;
108ba7e5ac1SBALATON Zoltan     char *filename;
109ba7e5ac1SBALATON Zoltan     int sz;
110ba7e5ac1SBALATON Zoltan     uint8_t *spd_data;
111ba7e5ac1SBALATON Zoltan 
112ba7e5ac1SBALATON Zoltan     /* init CPU */
113a8eda5edSBALATON Zoltan     pm->cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
114a8eda5edSBALATON Zoltan     env = &pm->cpu->env;
115a8eda5edSBALATON Zoltan     if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
116ba7e5ac1SBALATON Zoltan         error_report("Incompatible CPU, only 6xx bus supported");
117ba7e5ac1SBALATON Zoltan         exit(1);
118ba7e5ac1SBALATON Zoltan     }
119ba7e5ac1SBALATON Zoltan 
120ba7e5ac1SBALATON Zoltan     /* Set time-base frequency */
121a8eda5edSBALATON Zoltan     cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4);
122a8eda5edSBALATON Zoltan     qemu_register_reset(pegasos2_cpu_reset, pm->cpu);
123ba7e5ac1SBALATON Zoltan 
124ba7e5ac1SBALATON Zoltan     /* RAM */
125239fec24SBALATON Zoltan     if (machine->ram_size > 2 * GiB) {
126239fec24SBALATON Zoltan         error_report("RAM size more than 2 GiB is not supported");
127239fec24SBALATON Zoltan         exit(1);
128239fec24SBALATON Zoltan     }
129ba7e5ac1SBALATON Zoltan     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
130ba7e5ac1SBALATON Zoltan 
131ba7e5ac1SBALATON Zoltan     /* allocate and load firmware */
132ba7e5ac1SBALATON Zoltan     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname);
133ba7e5ac1SBALATON Zoltan     if (!filename) {
134ba7e5ac1SBALATON Zoltan         error_report("Could not find firmware '%s'", fwname);
135ba7e5ac1SBALATON Zoltan         exit(1);
136ba7e5ac1SBALATON Zoltan     }
137a6c9808aSBALATON Zoltan     if (!machine->firmware && !pm->vof) {
138a6c9808aSBALATON Zoltan         pm->vof = g_malloc0(sizeof(*pm->vof));
139a6c9808aSBALATON Zoltan     }
140ba7e5ac1SBALATON Zoltan     memory_region_init_rom(rom, NULL, "pegasos2.rom", PROM_SIZE, &error_fatal);
141ba7e5ac1SBALATON Zoltan     memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
142ba7e5ac1SBALATON Zoltan     sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1,
143ba7e5ac1SBALATON Zoltan                   PPC_ELF_MACHINE, 0, 0);
144ba7e5ac1SBALATON Zoltan     if (sz <= 0) {
145a6c9808aSBALATON Zoltan         sz = load_image_targphys(filename, pm->vof ? 0 : PROM_ADDR, PROM_SIZE);
146ba7e5ac1SBALATON Zoltan     }
147ba7e5ac1SBALATON Zoltan     if (sz <= 0 || sz > PROM_SIZE) {
148ba7e5ac1SBALATON Zoltan         error_report("Could not load firmware '%s'", filename);
149ba7e5ac1SBALATON Zoltan         exit(1);
150ba7e5ac1SBALATON Zoltan     }
151ba7e5ac1SBALATON Zoltan     g_free(filename);
152a6c9808aSBALATON Zoltan     if (pm->vof) {
153a6c9808aSBALATON Zoltan         pm->vof->fw_size = sz;
154a6c9808aSBALATON Zoltan     }
155ba7e5ac1SBALATON Zoltan 
156ba7e5ac1SBALATON Zoltan     /* Marvell Discovery II system controller */
157a8eda5edSBALATON Zoltan     pm->mv = DEVICE(sysbus_create_simple(TYPE_MV64361, -1,
1580f3e0c6fSCédric Le Goater                           qdev_get_gpio_in(DEVICE(pm->cpu), PPC6xx_INPUT_INT)));
159a8eda5edSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 1);
160ba7e5ac1SBALATON Zoltan 
161ba7e5ac1SBALATON Zoltan     /* VIA VT8231 South Bridge (multifunction PCI device) */
162ba7e5ac1SBALATON Zoltan     /* VT8231 function 0: PCI-to-ISA Bridge */
1639eb6abbfSBernhard Beschow     via = pci_create_simple_multifunction(pci_bus, PCI_DEVFN(12, 0), true,
164ba7e5ac1SBALATON Zoltan                                           TYPE_VT8231_ISA);
1659eb6abbfSBernhard Beschow     qdev_connect_gpio_out(DEVICE(via), 0,
166a8eda5edSBALATON Zoltan                           qdev_get_gpio_in_named(pm->mv, "gpp", 31));
167ba7e5ac1SBALATON Zoltan 
1689eb6abbfSBernhard Beschow     dev = PCI_DEVICE(object_resolve_path_component(OBJECT(via), "ide"));
169ba7e5ac1SBALATON Zoltan     pci_ide_create_devs(dev);
170ba7e5ac1SBALATON Zoltan 
171*d1053772SBernhard Beschow     dev = PCI_DEVICE(object_resolve_path_component(OBJECT(via), "pm"));
172ba7e5ac1SBALATON Zoltan     i2c_bus = I2C_BUS(qdev_get_child_bus(DEVICE(dev), "i2c"));
173ba7e5ac1SBALATON Zoltan     spd_data = spd_data_generate(DDR, machine->ram_size);
174ba7e5ac1SBALATON Zoltan     smbus_eeprom_init_one(i2c_bus, 0x57, spd_data);
175ba7e5ac1SBALATON Zoltan 
176ba7e5ac1SBALATON Zoltan     /* VT8231 function 5-6: AC97 Audio & Modem */
177ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 5), TYPE_VIA_AC97);
178ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 6), TYPE_VIA_MC97);
179ba7e5ac1SBALATON Zoltan 
180ba7e5ac1SBALATON Zoltan     /* other PC hardware */
181ba7e5ac1SBALATON Zoltan     pci_vga_init(pci_bus);
182a6c9808aSBALATON Zoltan 
183a6c9808aSBALATON Zoltan     if (machine->kernel_filename) {
184a6c9808aSBALATON Zoltan         sz = load_elf(machine->kernel_filename, NULL, NULL, NULL,
185a6c9808aSBALATON Zoltan                       &pm->kernel_entry, &pm->kernel_addr, NULL, NULL, 1,
186a6c9808aSBALATON Zoltan                       PPC_ELF_MACHINE, 0, 0);
187a6c9808aSBALATON Zoltan         if (sz <= 0) {
188a6c9808aSBALATON Zoltan             error_report("Could not load kernel '%s'",
189a6c9808aSBALATON Zoltan                          machine->kernel_filename);
190a6c9808aSBALATON Zoltan             exit(1);
191a6c9808aSBALATON Zoltan         }
192a6c9808aSBALATON Zoltan         pm->kernel_size = sz;
193a6c9808aSBALATON Zoltan         if (!pm->vof) {
194a6c9808aSBALATON Zoltan             warn_report("Option -kernel may be ineffective with -bios.");
195a6c9808aSBALATON Zoltan         }
19688adcbf2SBALATON Zoltan     } else if (pm->vof && !qtest_enabled()) {
19799173b67SBALATON Zoltan         warn_report("Using Virtual OpenFirmware but no -kernel option.");
198a6c9808aSBALATON Zoltan     }
19999173b67SBALATON Zoltan 
2006ebc0048SBALATON Zoltan     if (!pm->vof && machine->kernel_cmdline && machine->kernel_cmdline[0]) {
201a6c9808aSBALATON Zoltan         warn_report("Option -append may be ineffective with -bios.");
202a6c9808aSBALATON Zoltan     }
203a6c9808aSBALATON Zoltan }
204a6c9808aSBALATON Zoltan 
205bd20cde5SBALATON Zoltan static uint32_t pegasos2_mv_reg_read(Pegasos2MachineState *pm,
2065f2eb049SBALATON Zoltan                                      uint32_t addr, uint32_t len)
2075f2eb049SBALATON Zoltan {
208bd20cde5SBALATON Zoltan     MemoryRegion *r = sysbus_mmio_get_region(SYS_BUS_DEVICE(pm->mv), 0);
209bd20cde5SBALATON Zoltan     uint64_t val = 0xffffffffULL;
210bd20cde5SBALATON Zoltan     memory_region_dispatch_read(r, addr, &val, size_memop(len) | MO_LE,
211bd20cde5SBALATON Zoltan                                 MEMTXATTRS_UNSPECIFIED);
212bd20cde5SBALATON Zoltan     return val;
213bd20cde5SBALATON Zoltan }
2145f2eb049SBALATON Zoltan 
215bd20cde5SBALATON Zoltan static void pegasos2_mv_reg_write(Pegasos2MachineState *pm, uint32_t addr,
216bd20cde5SBALATON Zoltan                                   uint32_t len, uint32_t val)
217bd20cde5SBALATON Zoltan {
218bd20cde5SBALATON Zoltan     MemoryRegion *r = sysbus_mmio_get_region(SYS_BUS_DEVICE(pm->mv), 0);
219bd20cde5SBALATON Zoltan     memory_region_dispatch_write(r, addr, val, size_memop(len) | MO_LE,
220bd20cde5SBALATON Zoltan                                  MEMTXATTRS_UNSPECIFIED);
221bd20cde5SBALATON Zoltan }
222bd20cde5SBALATON Zoltan 
223bd20cde5SBALATON Zoltan static uint32_t pegasos2_pci_config_read(Pegasos2MachineState *pm, int bus,
224bd20cde5SBALATON Zoltan                                          uint32_t addr, uint32_t len)
225bd20cde5SBALATON Zoltan {
226d200ea14SBALATON Zoltan     hwaddr pcicfg = bus ? PCI1_CFG_ADDR : PCI0_CFG_ADDR;
227bd20cde5SBALATON Zoltan     uint64_t val = 0xffffffffULL;
228bd20cde5SBALATON Zoltan 
229bd20cde5SBALATON Zoltan     if (len <= 4) {
230bd20cde5SBALATON Zoltan         pegasos2_mv_reg_write(pm, pcicfg, 4, addr | BIT(31));
231bd20cde5SBALATON Zoltan         val = pegasos2_mv_reg_read(pm, pcicfg + 4, len);
2325f2eb049SBALATON Zoltan     }
2335f2eb049SBALATON Zoltan     return val;
2345f2eb049SBALATON Zoltan }
2355f2eb049SBALATON Zoltan 
236bd20cde5SBALATON Zoltan static void pegasos2_pci_config_write(Pegasos2MachineState *pm, int bus,
237bd20cde5SBALATON Zoltan                                       uint32_t addr, uint32_t len, uint32_t val)
238a6c9808aSBALATON Zoltan {
239d200ea14SBALATON Zoltan     hwaddr pcicfg = bus ? PCI1_CFG_ADDR : PCI0_CFG_ADDR;
240a6c9808aSBALATON Zoltan 
241bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, pcicfg, 4, addr | BIT(31));
242bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, pcicfg + 4, len, val);
243a6c9808aSBALATON Zoltan }
244a6c9808aSBALATON Zoltan 
2457966d70fSJason A. Donenfeld static void pegasos2_machine_reset(MachineState *machine, ShutdownCause reason)
246a6c9808aSBALATON Zoltan {
247a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
248a6c9808aSBALATON Zoltan     void *fdt;
249a6c9808aSBALATON Zoltan     uint64_t d[2];
250a6c9808aSBALATON Zoltan     int sz;
251a6c9808aSBALATON Zoltan 
2527966d70fSJason A. Donenfeld     qemu_devices_reset(reason);
253a6c9808aSBALATON Zoltan     if (!pm->vof) {
254a6c9808aSBALATON Zoltan         return; /* Firmware should set up machine so nothing to do */
255a6c9808aSBALATON Zoltan     }
256a6c9808aSBALATON Zoltan 
257a6c9808aSBALATON Zoltan     /* Otherwise, set up devices that board firmware would normally do */
258bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, 0, 4, 0x28020ff);
259bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, 0x278, 4, 0xa31fc);
260bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, 0xf300, 4, 0x11ff0400);
261bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, 0xf10c, 4, 0x80000000);
262bd20cde5SBALATON Zoltan     pegasos2_mv_reg_write(pm, 0x1c, 4, 0x8000000);
263bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 0, PCI_COMMAND, 2, PCI_COMMAND_IO |
264a6c9808aSBALATON Zoltan                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
265bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, PCI_COMMAND, 2, PCI_COMMAND_IO |
266a6c9808aSBALATON Zoltan                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
267a6c9808aSBALATON Zoltan 
268bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
269a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x9);
270bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 0) << 8) |
271a6c9808aSBALATON Zoltan                               0x50, 1, 0x2);
272a6c9808aSBALATON Zoltan 
273bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
274a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x109);
275bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
276a6c9808aSBALATON Zoltan                               PCI_CLASS_PROG, 1, 0xf);
277bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
278a6c9808aSBALATON Zoltan                               0x40, 1, 0xb);
279bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
280a6c9808aSBALATON Zoltan                               0x50, 4, 0x17171717);
281bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 1) << 8) |
282a6c9808aSBALATON Zoltan                               PCI_COMMAND, 2, 0x87);
283a6c9808aSBALATON Zoltan 
284bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 2) << 8) |
285a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x409);
286a6c9808aSBALATON Zoltan 
287bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 3) << 8) |
288a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x409);
289a6c9808aSBALATON Zoltan 
290bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
291a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x9);
292bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
293a6c9808aSBALATON Zoltan                               0x48, 4, 0xf00);
294bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
295a6c9808aSBALATON Zoltan                               0x40, 4, 0x558020);
296bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 4) << 8) |
297a6c9808aSBALATON Zoltan                               0x90, 4, 0xd00);
298a6c9808aSBALATON Zoltan 
299bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 5) << 8) |
300a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x309);
301a6c9808aSBALATON Zoltan 
302bd20cde5SBALATON Zoltan     pegasos2_pci_config_write(pm, 1, (PCI_DEVFN(12, 6) << 8) |
303a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x309);
304a6c9808aSBALATON Zoltan 
305a6c9808aSBALATON Zoltan     /* Device tree and VOF set up */
306a6c9808aSBALATON Zoltan     vof_init(pm->vof, machine->ram_size, &error_fatal);
307a6c9808aSBALATON Zoltan     if (vof_claim(pm->vof, 0, VOF_STACK_SIZE, VOF_STACK_SIZE) == -1) {
308a6c9808aSBALATON Zoltan         error_report("Memory allocation for stack failed");
309a6c9808aSBALATON Zoltan         exit(1);
310a6c9808aSBALATON Zoltan     }
311a6c9808aSBALATON Zoltan     if (pm->kernel_size &&
312a6c9808aSBALATON Zoltan         vof_claim(pm->vof, pm->kernel_addr, pm->kernel_size, 0) == -1) {
313a6c9808aSBALATON Zoltan         error_report("Memory for kernel is in use");
314a6c9808aSBALATON Zoltan         exit(1);
315a6c9808aSBALATON Zoltan     }
316a6c9808aSBALATON Zoltan     fdt = build_fdt(machine, &sz);
317a6c9808aSBALATON Zoltan     /* FIXME: VOF assumes entry is same as load address */
318a6c9808aSBALATON Zoltan     d[0] = cpu_to_be64(pm->kernel_entry);
319a6c9808aSBALATON Zoltan     d[1] = cpu_to_be64(pm->kernel_size - (pm->kernel_entry - pm->kernel_addr));
320a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/chosen", "qemu,boot-kernel", d, sizeof(d));
321a6c9808aSBALATON Zoltan 
322a6c9808aSBALATON Zoltan     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
323a6c9808aSBALATON Zoltan     g_free(pm->fdt_blob);
324a6c9808aSBALATON Zoltan     pm->fdt_blob = fdt;
325a6c9808aSBALATON Zoltan 
326a6c9808aSBALATON Zoltan     vof_build_dt(fdt, pm->vof);
327a6c9808aSBALATON Zoltan     vof_client_open_store(fdt, pm->vof, "/chosen", "stdout", "/failsafe");
3288d55f876SDaniel Henrique Barboza 
3298d55f876SDaniel Henrique Barboza     /* Set machine->fdt for 'dumpdtb' QMP/HMP command */
3308d55f876SDaniel Henrique Barboza     machine->fdt = fdt;
3318d55f876SDaniel Henrique Barboza 
332a6c9808aSBALATON Zoltan     pm->cpu->vhyp = PPC_VIRTUAL_HYPERVISOR(machine);
333a6c9808aSBALATON Zoltan }
334a6c9808aSBALATON Zoltan 
3355f2eb049SBALATON Zoltan enum pegasos2_rtas_tokens {
3365f2eb049SBALATON Zoltan     RTAS_RESTART_RTAS = 0,
3375f2eb049SBALATON Zoltan     RTAS_NVRAM_FETCH = 1,
3385f2eb049SBALATON Zoltan     RTAS_NVRAM_STORE = 2,
3395f2eb049SBALATON Zoltan     RTAS_GET_TIME_OF_DAY = 3,
3405f2eb049SBALATON Zoltan     RTAS_SET_TIME_OF_DAY = 4,
3415f2eb049SBALATON Zoltan     RTAS_EVENT_SCAN = 6,
3425f2eb049SBALATON Zoltan     RTAS_CHECK_EXCEPTION = 7,
3435f2eb049SBALATON Zoltan     RTAS_READ_PCI_CONFIG = 8,
3445f2eb049SBALATON Zoltan     RTAS_WRITE_PCI_CONFIG = 9,
3455f2eb049SBALATON Zoltan     RTAS_DISPLAY_CHARACTER = 10,
3465f2eb049SBALATON Zoltan     RTAS_SET_INDICATOR = 11,
3475f2eb049SBALATON Zoltan     RTAS_POWER_OFF = 17,
3485f2eb049SBALATON Zoltan     RTAS_SUSPEND = 18,
3495f2eb049SBALATON Zoltan     RTAS_HIBERNATE = 19,
3505f2eb049SBALATON Zoltan     RTAS_SYSTEM_REBOOT = 20,
3515f2eb049SBALATON Zoltan };
3525f2eb049SBALATON Zoltan 
3535f2eb049SBALATON Zoltan static target_ulong pegasos2_rtas(PowerPCCPU *cpu, Pegasos2MachineState *pm,
3545f2eb049SBALATON Zoltan                                   target_ulong args_real)
3555f2eb049SBALATON Zoltan {
3565f2eb049SBALATON Zoltan     AddressSpace *as = CPU(cpu)->as;
3575f2eb049SBALATON Zoltan     uint32_t token = ldl_be_phys(as, args_real);
3585f2eb049SBALATON Zoltan     uint32_t nargs = ldl_be_phys(as, args_real + 4);
3595f2eb049SBALATON Zoltan     uint32_t nrets = ldl_be_phys(as, args_real + 8);
3605f2eb049SBALATON Zoltan     uint32_t args = args_real + 12;
3615f2eb049SBALATON Zoltan     uint32_t rets = args_real + 12 + nargs * 4;
3625f2eb049SBALATON Zoltan 
3635f2eb049SBALATON Zoltan     if (nrets < 1) {
3645f2eb049SBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Too few return values in RTAS call\n");
3655f2eb049SBALATON Zoltan         return H_PARAMETER;
3665f2eb049SBALATON Zoltan     }
3675f2eb049SBALATON Zoltan     switch (token) {
36894cd1ffbSBALATON Zoltan     case RTAS_GET_TIME_OF_DAY:
36994cd1ffbSBALATON Zoltan     {
37094cd1ffbSBALATON Zoltan         QObject *qo = object_property_get_qobject(qdev_get_machine(),
37194cd1ffbSBALATON Zoltan                                                   "rtc-time", &error_fatal);
37294cd1ffbSBALATON Zoltan         QDict *qd = qobject_to(QDict, qo);
37394cd1ffbSBALATON Zoltan 
37494cd1ffbSBALATON Zoltan         if (nargs != 0 || nrets != 8 || !qd) {
37594cd1ffbSBALATON Zoltan             stl_be_phys(as, rets, -1);
37694cd1ffbSBALATON Zoltan             qobject_unref(qo);
37794cd1ffbSBALATON Zoltan             return H_PARAMETER;
37894cd1ffbSBALATON Zoltan         }
37994cd1ffbSBALATON Zoltan 
38094cd1ffbSBALATON Zoltan         stl_be_phys(as, rets, 0);
38194cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 4, qdict_get_int(qd, "tm_year") + 1900);
38294cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 8, qdict_get_int(qd, "tm_mon") + 1);
38394cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 12, qdict_get_int(qd, "tm_mday"));
38494cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 16, qdict_get_int(qd, "tm_hour"));
38594cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 20, qdict_get_int(qd, "tm_min"));
38694cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 24, qdict_get_int(qd, "tm_sec"));
38794cd1ffbSBALATON Zoltan         stl_be_phys(as, rets + 28, 0);
38894cd1ffbSBALATON Zoltan         qobject_unref(qo);
38994cd1ffbSBALATON Zoltan         return H_SUCCESS;
39094cd1ffbSBALATON Zoltan     }
3915f2eb049SBALATON Zoltan     case RTAS_READ_PCI_CONFIG:
3925f2eb049SBALATON Zoltan     {
3935f2eb049SBALATON Zoltan         uint32_t addr, len, val;
3945f2eb049SBALATON Zoltan 
3955f2eb049SBALATON Zoltan         if (nargs != 2 || nrets != 2) {
3965f2eb049SBALATON Zoltan             stl_be_phys(as, rets, -1);
3975f2eb049SBALATON Zoltan             return H_PARAMETER;
3985f2eb049SBALATON Zoltan         }
3995f2eb049SBALATON Zoltan         addr = ldl_be_phys(as, args);
4005f2eb049SBALATON Zoltan         len = ldl_be_phys(as, args + 4);
401bd20cde5SBALATON Zoltan         val = pegasos2_pci_config_read(pm, !(addr >> 24),
4025f2eb049SBALATON Zoltan                                        addr & 0x0fffffff, len);
4035f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
4045f2eb049SBALATON Zoltan         stl_be_phys(as, rets + 4, val);
4055f2eb049SBALATON Zoltan         return H_SUCCESS;
4065f2eb049SBALATON Zoltan     }
4075f2eb049SBALATON Zoltan     case RTAS_WRITE_PCI_CONFIG:
4085f2eb049SBALATON Zoltan     {
4095f2eb049SBALATON Zoltan         uint32_t addr, len, val;
4105f2eb049SBALATON Zoltan 
4115f2eb049SBALATON Zoltan         if (nargs != 3 || nrets != 1) {
4125f2eb049SBALATON Zoltan             stl_be_phys(as, rets, -1);
4135f2eb049SBALATON Zoltan             return H_PARAMETER;
4145f2eb049SBALATON Zoltan         }
4155f2eb049SBALATON Zoltan         addr = ldl_be_phys(as, args);
4165f2eb049SBALATON Zoltan         len = ldl_be_phys(as, args + 4);
4175f2eb049SBALATON Zoltan         val = ldl_be_phys(as, args + 8);
418bd20cde5SBALATON Zoltan         pegasos2_pci_config_write(pm, !(addr >> 24),
4195f2eb049SBALATON Zoltan                                   addr & 0x0fffffff, len, val);
4205f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
4215f2eb049SBALATON Zoltan         return H_SUCCESS;
4225f2eb049SBALATON Zoltan     }
4235f2eb049SBALATON Zoltan     case RTAS_DISPLAY_CHARACTER:
4245f2eb049SBALATON Zoltan         if (nargs != 1 || nrets != 1) {
4255f2eb049SBALATON Zoltan             stl_be_phys(as, rets, -1);
4265f2eb049SBALATON Zoltan             return H_PARAMETER;
4275f2eb049SBALATON Zoltan         }
4285f2eb049SBALATON Zoltan         qemu_log_mask(LOG_UNIMP, "%c", ldl_be_phys(as, args));
4295f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
4305f2eb049SBALATON Zoltan         return H_SUCCESS;
431284c0486SBALATON Zoltan     case RTAS_POWER_OFF:
432284c0486SBALATON Zoltan     {
433284c0486SBALATON Zoltan         if (nargs != 2 || nrets != 1) {
434284c0486SBALATON Zoltan             stl_be_phys(as, rets, -1);
435284c0486SBALATON Zoltan             return H_PARAMETER;
436284c0486SBALATON Zoltan         }
437284c0486SBALATON Zoltan         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
438284c0486SBALATON Zoltan         stl_be_phys(as, rets, 0);
439284c0486SBALATON Zoltan         return H_SUCCESS;
440284c0486SBALATON Zoltan     }
4415f2eb049SBALATON Zoltan     default:
4425f2eb049SBALATON Zoltan         qemu_log_mask(LOG_UNIMP, "Unknown RTAS token %u (args=%u, rets=%u)\n",
4435f2eb049SBALATON Zoltan                       token, nargs, nrets);
4445f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
4455f2eb049SBALATON Zoltan         return H_SUCCESS;
4465f2eb049SBALATON Zoltan     }
4475f2eb049SBALATON Zoltan }
4485f2eb049SBALATON Zoltan 
4497cebc5dbSNicholas Piggin static bool pegasos2_cpu_in_nested(PowerPCCPU *cpu)
4507cebc5dbSNicholas Piggin {
4517cebc5dbSNicholas Piggin     return false;
4527cebc5dbSNicholas Piggin }
4537cebc5dbSNicholas Piggin 
454a6c9808aSBALATON Zoltan static void pegasos2_hypercall(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
455a6c9808aSBALATON Zoltan {
456a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(vhyp);
457a6c9808aSBALATON Zoltan     CPUPPCState *env = &cpu->env;
458a6c9808aSBALATON Zoltan 
459a6c9808aSBALATON Zoltan     /* The TCG path should also be holding the BQL at this point */
460a6c9808aSBALATON Zoltan     g_assert(qemu_mutex_iothread_locked());
461a6c9808aSBALATON Zoltan 
462d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
463a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Hypercall made with MSR[PR]=1\n");
464a6c9808aSBALATON Zoltan         env->gpr[3] = H_PRIVILEGE;
4655f2eb049SBALATON Zoltan     } else if (env->gpr[3] == KVMPPC_H_RTAS) {
4665f2eb049SBALATON Zoltan         env->gpr[3] = pegasos2_rtas(cpu, pm, env->gpr[4]);
467a6c9808aSBALATON Zoltan     } else if (env->gpr[3] == KVMPPC_H_VOF_CLIENT) {
468a6c9808aSBALATON Zoltan         int ret = vof_client_call(MACHINE(pm), pm->vof, pm->fdt_blob,
469a6c9808aSBALATON Zoltan                                   env->gpr[4]);
470a6c9808aSBALATON Zoltan         env->gpr[3] = (ret ? H_PARAMETER : H_SUCCESS);
471a6c9808aSBALATON Zoltan     } else {
472a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Unsupported hypercall " TARGET_FMT_lx
473a6c9808aSBALATON Zoltan                       "\n", env->gpr[3]);
474a6c9808aSBALATON Zoltan         env->gpr[3] = -1;
475a6c9808aSBALATON Zoltan     }
476a6c9808aSBALATON Zoltan }
477a6c9808aSBALATON Zoltan 
478a6c9808aSBALATON Zoltan static void vhyp_nop(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
479a6c9808aSBALATON Zoltan {
480a6c9808aSBALATON Zoltan }
481a6c9808aSBALATON Zoltan 
482a6c9808aSBALATON Zoltan static target_ulong vhyp_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
483a6c9808aSBALATON Zoltan {
484a6c9808aSBALATON Zoltan     return POWERPC_CPU(current_cpu)->env.spr[SPR_SDR1];
485ba7e5ac1SBALATON Zoltan }
486ba7e5ac1SBALATON Zoltan 
487a312aaebSBALATON Zoltan static bool pegasos2_setprop(MachineState *ms, const char *path,
488a312aaebSBALATON Zoltan                              const char *propname, void *val, int vallen)
489a312aaebSBALATON Zoltan {
490a312aaebSBALATON Zoltan     return true;
491a312aaebSBALATON Zoltan }
492a312aaebSBALATON Zoltan 
493a8eda5edSBALATON Zoltan static void pegasos2_machine_class_init(ObjectClass *oc, void *data)
494ba7e5ac1SBALATON Zoltan {
495a8eda5edSBALATON Zoltan     MachineClass *mc = MACHINE_CLASS(oc);
496a6c9808aSBALATON Zoltan     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
497a312aaebSBALATON Zoltan     VofMachineIfClass *vmc = VOF_MACHINE_CLASS(oc);
498a8eda5edSBALATON Zoltan 
499ba7e5ac1SBALATON Zoltan     mc->desc = "Genesi/bPlan Pegasos II";
500ba7e5ac1SBALATON Zoltan     mc->init = pegasos2_init;
501a6c9808aSBALATON Zoltan     mc->reset = pegasos2_machine_reset;
502ba7e5ac1SBALATON Zoltan     mc->block_default_type = IF_IDE;
503ba7e5ac1SBALATON Zoltan     mc->default_boot_order = "cd";
504ba7e5ac1SBALATON Zoltan     mc->default_display = "std";
505ba7e5ac1SBALATON Zoltan     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7400_v2.9");
506ba7e5ac1SBALATON Zoltan     mc->default_ram_id = "pegasos2.ram";
507ba7e5ac1SBALATON Zoltan     mc->default_ram_size = 512 * MiB;
508a6c9808aSBALATON Zoltan 
5097cebc5dbSNicholas Piggin     vhc->cpu_in_nested = pegasos2_cpu_in_nested;
510a6c9808aSBALATON Zoltan     vhc->hypercall = pegasos2_hypercall;
511a6c9808aSBALATON Zoltan     vhc->cpu_exec_enter = vhyp_nop;
512a6c9808aSBALATON Zoltan     vhc->cpu_exec_exit = vhyp_nop;
513a6c9808aSBALATON Zoltan     vhc->encode_hpt_for_kvm_pr = vhyp_encode_hpt_for_kvm_pr;
514a312aaebSBALATON Zoltan 
515a312aaebSBALATON Zoltan     vmc->setprop = pegasos2_setprop;
516ba7e5ac1SBALATON Zoltan }
517ba7e5ac1SBALATON Zoltan 
518a8eda5edSBALATON Zoltan static const TypeInfo pegasos2_machine_info = {
519a8eda5edSBALATON Zoltan     .name          = TYPE_PEGASOS2_MACHINE,
520a8eda5edSBALATON Zoltan     .parent        = TYPE_MACHINE,
521a8eda5edSBALATON Zoltan     .class_init    = pegasos2_machine_class_init,
522a8eda5edSBALATON Zoltan     .instance_size = sizeof(Pegasos2MachineState),
523a6c9808aSBALATON Zoltan     .interfaces = (InterfaceInfo[]) {
524a6c9808aSBALATON Zoltan         { TYPE_PPC_VIRTUAL_HYPERVISOR },
525a312aaebSBALATON Zoltan         { TYPE_VOF_MACHINE_IF },
526a6c9808aSBALATON Zoltan         { }
527a6c9808aSBALATON Zoltan     },
528a8eda5edSBALATON Zoltan };
529a8eda5edSBALATON Zoltan 
530a8eda5edSBALATON Zoltan static void pegasos2_machine_register_types(void)
531a8eda5edSBALATON Zoltan {
532a8eda5edSBALATON Zoltan     type_register_static(&pegasos2_machine_info);
533a8eda5edSBALATON Zoltan }
534a8eda5edSBALATON Zoltan 
535a8eda5edSBALATON Zoltan type_init(pegasos2_machine_register_types)
536a6c9808aSBALATON Zoltan 
537a6c9808aSBALATON Zoltan /* FDT creation for passing to firmware */
538a6c9808aSBALATON Zoltan 
539a6c9808aSBALATON Zoltan typedef struct {
540a6c9808aSBALATON Zoltan     void *fdt;
541a6c9808aSBALATON Zoltan     const char *path;
542a6c9808aSBALATON Zoltan } FDTInfo;
543a6c9808aSBALATON Zoltan 
544a6c9808aSBALATON Zoltan /* We do everything in reverse order so it comes out right in the tree */
545a6c9808aSBALATON Zoltan 
546a6c9808aSBALATON Zoltan static void dt_ide(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
547a6c9808aSBALATON Zoltan {
548a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "spi");
549a6c9808aSBALATON Zoltan }
550a6c9808aSBALATON Zoltan 
551a6c9808aSBALATON Zoltan static void dt_usb(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
552a6c9808aSBALATON Zoltan {
553a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 0);
554a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 1);
555a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "usb");
556a6c9808aSBALATON Zoltan }
557a6c9808aSBALATON Zoltan 
558a6c9808aSBALATON Zoltan static void dt_isa(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
559a6c9808aSBALATON Zoltan {
560a6c9808aSBALATON Zoltan     GString *name = g_string_sized_new(64);
561a6c9808aSBALATON Zoltan     uint32_t cells[3];
562a6c9808aSBALATON Zoltan 
563a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 1);
564a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 2);
565a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "isa");
566a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "name", "isa");
567a6c9808aSBALATON Zoltan 
568a6c9808aSBALATON Zoltan     /* addional devices */
569a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/lpt@i3bc", fi->path);
570a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
571a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
572a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(7);
573a6c9808aSBALATON Zoltan     cells[1] = 0;
574a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
575a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
576a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
577a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x3bc);
578a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
579a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
580a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "lpt");
581a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "lpt");
582a6c9808aSBALATON Zoltan 
583a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/fdc@i3f0", fi->path);
584a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
585a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
586a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(6);
587a6c9808aSBALATON Zoltan     cells[1] = 0;
588a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
589a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
590a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
591a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x3f0);
592a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
593a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
594a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "fdc");
595a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "fdc");
596a6c9808aSBALATON Zoltan 
597a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/timer@i40", fi->path);
598a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
599a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
600a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
601a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x40);
602a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
603a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
604a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "timer");
605a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "timer");
606a6c9808aSBALATON Zoltan 
607a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/rtc@i70", fi->path);
608a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
609a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "compatible", "ds1385-rtc");
610a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
611a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(8);
612a6c9808aSBALATON Zoltan     cells[1] = 0;
613a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
614a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
615a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
616a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x70);
617a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(2);
618a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
619a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "rtc");
620a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "rtc");
621a6c9808aSBALATON Zoltan 
622a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/keyboard@i60", fi->path);
623a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
624a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
625a6c9808aSBALATON Zoltan     cells[1] = 0;
626a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
627a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
628a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
629a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x60);
630a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(5);
631a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
632a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "keyboard");
633a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "keyboard");
634a6c9808aSBALATON Zoltan 
635a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/8042@i60", fi->path);
636a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
637a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#interrupt-cells", 2);
638a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#size-cells", 0);
639a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#address-cells", 1);
640a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "interrupt-controller", "");
641a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
642a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
643a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x60);
644a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(5);
645a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
646a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "");
647a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "8042");
648a6c9808aSBALATON Zoltan 
649a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/serial@i2f8", fi->path);
650a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
651a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
652a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(3);
653a6c9808aSBALATON Zoltan     cells[1] = 0;
654a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
655a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
656a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
657a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x2f8);
658a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
659a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
660a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "serial");
661a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "serial");
662a6c9808aSBALATON Zoltan 
663a6c9808aSBALATON Zoltan     g_string_free(name, TRUE);
664a6c9808aSBALATON Zoltan }
665a6c9808aSBALATON Zoltan 
666a6c9808aSBALATON Zoltan static struct {
667a6c9808aSBALATON Zoltan     const char *id;
668a6c9808aSBALATON Zoltan     const char *name;
669a6c9808aSBALATON Zoltan     void (*dtf)(PCIBus *bus, PCIDevice *d, FDTInfo *fi);
670a6c9808aSBALATON Zoltan } device_map[] = {
671a6c9808aSBALATON Zoltan     { "pci11ab,6460", "host", NULL },
672a6c9808aSBALATON Zoltan     { "pci1106,8231", "isa", dt_isa },
673a6c9808aSBALATON Zoltan     { "pci1106,571", "ide", dt_ide },
674a6c9808aSBALATON Zoltan     { "pci1106,3044", "firewire", NULL },
675a6c9808aSBALATON Zoltan     { "pci1106,3038", "usb", dt_usb },
676a6c9808aSBALATON Zoltan     { "pci1106,8235", "other", NULL },
677a6c9808aSBALATON Zoltan     { "pci1106,3058", "sound", NULL },
678a6c9808aSBALATON Zoltan     { NULL, NULL }
679a6c9808aSBALATON Zoltan };
680a6c9808aSBALATON Zoltan 
681a6c9808aSBALATON Zoltan static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
682a6c9808aSBALATON Zoltan {
683a6c9808aSBALATON Zoltan     FDTInfo *fi = opaque;
684a6c9808aSBALATON Zoltan     GString *node = g_string_new(NULL);
685a6c9808aSBALATON Zoltan     uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
686a6c9808aSBALATON Zoltan     int i, j;
687a6c9808aSBALATON Zoltan     const char *name = NULL;
688a6c9808aSBALATON Zoltan     g_autofree const gchar *pn = g_strdup_printf("pci%x,%x",
689a6c9808aSBALATON Zoltan                                      pci_get_word(&d->config[PCI_VENDOR_ID]),
690a6c9808aSBALATON Zoltan                                      pci_get_word(&d->config[PCI_DEVICE_ID]));
691a6c9808aSBALATON Zoltan 
692a6c9808aSBALATON Zoltan     for (i = 0; device_map[i].id; i++) {
693a6c9808aSBALATON Zoltan         if (!strcmp(pn, device_map[i].id)) {
694a6c9808aSBALATON Zoltan             name = device_map[i].name;
695a6c9808aSBALATON Zoltan             break;
696a6c9808aSBALATON Zoltan         }
697a6c9808aSBALATON Zoltan     }
698a6c9808aSBALATON Zoltan     g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
699a6c9808aSBALATON Zoltan                     PCI_SLOT(d->devfn));
700a6c9808aSBALATON Zoltan     if (PCI_FUNC(d->devfn)) {
701a6c9808aSBALATON Zoltan         g_string_append_printf(node, ",%x", PCI_FUNC(d->devfn));
702a6c9808aSBALATON Zoltan     }
703a6c9808aSBALATON Zoltan 
704a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, node->str);
705a6c9808aSBALATON Zoltan     if (device_map[i].dtf) {
706a6c9808aSBALATON Zoltan         FDTInfo cfi = { fi->fdt, node->str };
707a6c9808aSBALATON Zoltan         device_map[i].dtf(bus, d, &cfi);
708a6c9808aSBALATON Zoltan     }
709a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(d->devfn << 8);
710a6c9808aSBALATON Zoltan     cells[1] = 0;
711a6c9808aSBALATON Zoltan     cells[2] = 0;
712a6c9808aSBALATON Zoltan     cells[3] = 0;
713a6c9808aSBALATON Zoltan     cells[4] = 0;
714a6c9808aSBALATON Zoltan     j = 5;
715a6c9808aSBALATON Zoltan     for (i = 0; i < PCI_NUM_REGIONS; i++) {
716a6c9808aSBALATON Zoltan         if (!d->io_regions[i].size) {
717a6c9808aSBALATON Zoltan             continue;
718a6c9808aSBALATON Zoltan         }
719a6c9808aSBALATON Zoltan         cells[j] = cpu_to_be32(d->devfn << 8 | (PCI_BASE_ADDRESS_0 + i * 4));
720a6c9808aSBALATON Zoltan         if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
721a6c9808aSBALATON Zoltan             cells[j] |= cpu_to_be32(1 << 24);
722a6c9808aSBALATON Zoltan         } else {
723a6c9808aSBALATON Zoltan             cells[j] |= cpu_to_be32(2 << 24);
724a6c9808aSBALATON Zoltan             if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
725a6c9808aSBALATON Zoltan                 cells[j] |= cpu_to_be32(4 << 28);
726a6c9808aSBALATON Zoltan             }
727a6c9808aSBALATON Zoltan         }
728a6c9808aSBALATON Zoltan         cells[j + 1] = 0;
729a6c9808aSBALATON Zoltan         cells[j + 2] = 0;
730a6c9808aSBALATON Zoltan         cells[j + 3] = cpu_to_be32(d->io_regions[i].size >> 32);
731a6c9808aSBALATON Zoltan         cells[j + 4] = cpu_to_be32(d->io_regions[i].size);
732a6c9808aSBALATON Zoltan         j += 5;
733a6c9808aSBALATON Zoltan     }
734a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, node->str, "reg", cells, j * sizeof(cells[0]));
735a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, node->str, "name", name ?: pn);
736a6c9808aSBALATON Zoltan     if (pci_get_byte(&d->config[PCI_INTERRUPT_PIN])) {
737a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fi->fdt, node->str, "interrupts",
738a6c9808aSBALATON Zoltan                               pci_get_byte(&d->config[PCI_INTERRUPT_PIN]));
739a6c9808aSBALATON Zoltan     }
740a6c9808aSBALATON Zoltan     /* Pegasos2 firmware has subsystem-id amd subsystem-vendor-id swapped */
741a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-vendor-id",
742a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_SUBSYSTEM_ID]));
743a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-id",
744a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_SUBSYSTEM_VENDOR_ID]));
745a6c9808aSBALATON Zoltan     cells[0] = pci_get_long(&d->config[PCI_CLASS_REVISION]);
746a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "class-code", cells[0] >> 8);
747e7dfb29eSDavid Gibson     qemu_fdt_setprop_cell(fi->fdt, node->str, "revision-id", cells[0] & 0xff);
748a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "device-id",
749a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_DEVICE_ID]));
750a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "vendor-id",
751a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_VENDOR_ID]));
752a6c9808aSBALATON Zoltan 
753a6c9808aSBALATON Zoltan     g_string_free(node, TRUE);
754a6c9808aSBALATON Zoltan }
755a6c9808aSBALATON Zoltan 
756a6c9808aSBALATON Zoltan static void *build_fdt(MachineState *machine, int *fdt_size)
757a6c9808aSBALATON Zoltan {
758a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
759a6c9808aSBALATON Zoltan     PowerPCCPU *cpu = pm->cpu;
760a6c9808aSBALATON Zoltan     PCIBus *pci_bus;
761a6c9808aSBALATON Zoltan     FDTInfo fi;
762a6c9808aSBALATON Zoltan     uint32_t cells[16];
763a6c9808aSBALATON Zoltan     void *fdt = create_device_tree(fdt_size);
764a6c9808aSBALATON Zoltan 
765a6c9808aSBALATON Zoltan     fi.fdt = fdt;
766a6c9808aSBALATON Zoltan 
767a6c9808aSBALATON Zoltan     /* root node */
768a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,description",
769a6c9808aSBALATON Zoltan                             "Pegasos CHRP PowerPC System");
770a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,board", "Pegasos2");
771a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,vendor", "bplan GmbH");
772a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "revision", "2B");
773a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "model", "Pegasos2");
774a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "device_type", "chrp");
775a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 1);
776a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "name", "bplan,Pegasos2");
777a6c9808aSBALATON Zoltan 
778a6c9808aSBALATON Zoltan     /* pci@c0000000 */
779a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/pci@c0000000");
780a6c9808aSBALATON Zoltan     cells[0] = 0;
781a6c9808aSBALATON Zoltan     cells[1] = 0;
782a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "bus-range",
783a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
784a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "pci-bridge-number", 1);
785a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(PCI0_MEM_BASE);
786a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(PCI0_MEM_SIZE);
787a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "reg", cells, 2 * sizeof(cells[0]));
788a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(0x01000000);
789a6c9808aSBALATON Zoltan     cells[1] = 0;
790a6c9808aSBALATON Zoltan     cells[2] = 0;
791a6c9808aSBALATON Zoltan     cells[3] = cpu_to_be32(PCI0_IO_BASE);
792a6c9808aSBALATON Zoltan     cells[4] = 0;
793a6c9808aSBALATON Zoltan     cells[5] = cpu_to_be32(PCI0_IO_SIZE);
794a6c9808aSBALATON Zoltan     cells[6] = cpu_to_be32(0x02000000);
795a6c9808aSBALATON Zoltan     cells[7] = 0;
796a6c9808aSBALATON Zoltan     cells[8] = cpu_to_be32(PCI0_MEM_BASE);
797a6c9808aSBALATON Zoltan     cells[9] = cpu_to_be32(PCI0_MEM_BASE);
798a6c9808aSBALATON Zoltan     cells[10] = 0;
799a6c9808aSBALATON Zoltan     cells[11] = cpu_to_be32(PCI0_MEM_SIZE);
800a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "ranges",
801a6c9808aSBALATON Zoltan                      cells, 12 * sizeof(cells[0]));
802a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#size-cells", 2);
803a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#address-cells", 3);
804a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@c0000000", "device_type", "pci");
805a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@c0000000", "name", "pci");
806a6c9808aSBALATON Zoltan 
807a6c9808aSBALATON Zoltan     fi.path = "/pci@c0000000";
808a6c9808aSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 0);
809a6c9808aSBALATON Zoltan     pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
810a6c9808aSBALATON Zoltan 
811a6c9808aSBALATON Zoltan     /* pci@80000000 */
812a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/pci@80000000");
813a6c9808aSBALATON Zoltan     cells[0] = 0;
814a6c9808aSBALATON Zoltan     cells[1] = 0;
815a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "bus-range",
816a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
817a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "pci-bridge-number", 0);
818a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(PCI1_MEM_BASE);
819a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(PCI1_MEM_SIZE);
820a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "reg", cells, 2 * sizeof(cells[0]));
821a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "8259-interrupt-acknowledge",
822a6c9808aSBALATON Zoltan                           0xf1000cb4);
823a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(0x01000000);
824a6c9808aSBALATON Zoltan     cells[1] = 0;
825a6c9808aSBALATON Zoltan     cells[2] = 0;
826a6c9808aSBALATON Zoltan     cells[3] = cpu_to_be32(PCI1_IO_BASE);
827a6c9808aSBALATON Zoltan     cells[4] = 0;
828a6c9808aSBALATON Zoltan     cells[5] = cpu_to_be32(PCI1_IO_SIZE);
829a6c9808aSBALATON Zoltan     cells[6] = cpu_to_be32(0x02000000);
830a6c9808aSBALATON Zoltan     cells[7] = 0;
831a6c9808aSBALATON Zoltan     cells[8] = cpu_to_be32(PCI1_MEM_BASE);
832a6c9808aSBALATON Zoltan     cells[9] = cpu_to_be32(PCI1_MEM_BASE);
833a6c9808aSBALATON Zoltan     cells[10] = 0;
834a6c9808aSBALATON Zoltan     cells[11] = cpu_to_be32(PCI1_MEM_SIZE);
835a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "ranges",
836a6c9808aSBALATON Zoltan                      cells, 12 * sizeof(cells[0]));
837a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#size-cells", 2);
838a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#address-cells", 3);
839a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@80000000", "device_type", "pci");
840a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@80000000", "name", "pci");
841a6c9808aSBALATON Zoltan 
842a6c9808aSBALATON Zoltan     fi.path = "/pci@80000000";
843a6c9808aSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 1);
844a6c9808aSBALATON Zoltan     pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
845a6c9808aSBALATON Zoltan 
846a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/failsafe");
847a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/failsafe", "device_type", "serial");
848a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/failsafe", "name", "failsafe");
849a6c9808aSBALATON Zoltan 
8505f2eb049SBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/rtas");
8515f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "system-reboot", RTAS_SYSTEM_REBOOT);
8525f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "hibernate", RTAS_HIBERNATE);
8535f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "suspend", RTAS_SUSPEND);
8545f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "power-off", RTAS_POWER_OFF);
8555f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "set-indicator", RTAS_SET_INDICATOR);
8565f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "display-character",
8575f2eb049SBALATON Zoltan                           RTAS_DISPLAY_CHARACTER);
8585f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "write-pci-config",
8595f2eb049SBALATON Zoltan                           RTAS_WRITE_PCI_CONFIG);
8605f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "read-pci-config",
8615f2eb049SBALATON Zoltan                           RTAS_READ_PCI_CONFIG);
8625f2eb049SBALATON Zoltan     /* Pegasos2 firmware misspells check-exception and guests use that */
8635f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "check-execption",
8645f2eb049SBALATON Zoltan                           RTAS_CHECK_EXCEPTION);
8655f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "event-scan", RTAS_EVENT_SCAN);
8665f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "set-time-of-day",
8675f2eb049SBALATON Zoltan                           RTAS_SET_TIME_OF_DAY);
8685f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "get-time-of-day",
8695f2eb049SBALATON Zoltan                           RTAS_GET_TIME_OF_DAY);
8705f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-store", RTAS_NVRAM_STORE);
8715f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-fetch", RTAS_NVRAM_FETCH);
8725f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "restart-rtas", RTAS_RESTART_RTAS);
8735f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-error-log-max", 0);
8745f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-event-scan-rate", 0);
8755f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-display-device", 0);
8765f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size", 20);
8775f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-version", 1);
8785f2eb049SBALATON Zoltan 
879a6c9808aSBALATON Zoltan     /* cpus */
880a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/cpus");
881a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#cpus", 1);
882a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
883a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
884a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/cpus", "name", "cpus");
885a6c9808aSBALATON Zoltan 
886a6c9808aSBALATON Zoltan     /* FIXME Get CPU name from CPU object */
887a6c9808aSBALATON Zoltan     const char *cp = "/cpus/PowerPC,G4";
888a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, cp);
889a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "l2cr", 0);
890a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-size", 0x8000);
891a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-block-size",
892a6c9808aSBALATON Zoltan                           cpu->env.dcache_line_size);
893a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-line-size",
894a6c9808aSBALATON Zoltan                           cpu->env.dcache_line_size);
895a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-size", 0x8000);
896a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-block-size",
897a6c9808aSBALATON Zoltan                           cpu->env.icache_line_size);
898a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-line-size",
899a6c9808aSBALATON Zoltan                           cpu->env.icache_line_size);
900a6c9808aSBALATON Zoltan     if (cpu->env.id_tlbs) {
901a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "i-tlb-sets", cpu->env.nb_ways);
902a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "i-tlb-size", cpu->env.tlb_per_way);
903a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "d-tlb-sets", cpu->env.nb_ways);
904a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "d-tlb-size", cpu->env.tlb_per_way);
905a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "tlb-split", "");
906a6c9808aSBALATON Zoltan     }
907a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "tlb-sets", cpu->env.nb_ways);
908a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "tlb-size", cpu->env.nb_tlb);
909a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "state", "running");
910a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_ALTIVEC) {
911a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "altivec", "");
912a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "data-streams", "");
913a6c9808aSBALATON Zoltan     }
914a6c9808aSBALATON Zoltan     /*
915a6c9808aSBALATON Zoltan      * FIXME What flags do data-streams, external-control and
916a6c9808aSBALATON Zoltan      * performance-monitor depend on?
917a6c9808aSBALATON Zoltan      */
918a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "external-control", "");
919a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_FLOAT_FSQRT) {
920a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "general-purpose", "");
921a6c9808aSBALATON Zoltan     }
922a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "performance-monitor", "");
923a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_FLOAT_FRES) {
924a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "graphics", "");
925a6c9808aSBALATON Zoltan     }
926a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "reservation-granule-size", 4);
927a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "timebase-frequency",
928a6c9808aSBALATON Zoltan                           cpu->env.tb_env->tb_freq);
929a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "bus-frequency", BUS_FREQ_HZ);
930a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "clock-frequency", BUS_FREQ_HZ * 7.5);
931a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "cpu-version", cpu->env.spr[SPR_PVR]);
932a6c9808aSBALATON Zoltan     cells[0] = 0;
933a6c9808aSBALATON Zoltan     cells[1] = 0;
934a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, cp, "reg", cells, 2 * sizeof(cells[0]));
935a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "device_type", "cpu");
936a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "name", strrchr(cp, '/') + 1);
937a6c9808aSBALATON Zoltan 
938a6c9808aSBALATON Zoltan     /* memory */
939a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/memory@0");
940a6c9808aSBALATON Zoltan     cells[0] = 0;
941a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(machine->ram_size);
942a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/memory@0", "reg", cells, 2 * sizeof(cells[0]));
943a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/memory@0", "device_type", "memory");
944a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/memory@0", "name", "memory");
945a6c9808aSBALATON Zoltan 
946a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/chosen");
947a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
948a6c9808aSBALATON Zoltan                             machine->kernel_cmdline ?: "");
949a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/chosen", "name", "chosen");
950a6c9808aSBALATON Zoltan 
951a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/openprom");
952a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/openprom", "model", "Pegasos2,1.1");
953a6c9808aSBALATON Zoltan 
954a6c9808aSBALATON Zoltan     return fdt;
955a6c9808aSBALATON Zoltan }
956