xref: /qemu/hw/ppc/pegasos2.c (revision 6ebc0048dd8ff93c2847739599afa630f3d9dabd)
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-common.h"
12ba7e5ac1SBALATON Zoltan #include "qemu/units.h"
13ba7e5ac1SBALATON Zoltan #include "qapi/error.h"
14ba7e5ac1SBALATON Zoltan #include "hw/hw.h"
15ba7e5ac1SBALATON Zoltan #include "hw/ppc/ppc.h"
16ba7e5ac1SBALATON Zoltan #include "hw/sysbus.h"
17ba7e5ac1SBALATON Zoltan #include "hw/pci/pci_host.h"
18ba7e5ac1SBALATON Zoltan #include "hw/irq.h"
19ba7e5ac1SBALATON Zoltan #include "hw/pci-host/mv64361.h"
20ba7e5ac1SBALATON Zoltan #include "hw/isa/vt82c686.h"
21ba7e5ac1SBALATON Zoltan #include "hw/ide/pci.h"
22ba7e5ac1SBALATON Zoltan #include "hw/i2c/smbus_eeprom.h"
23ba7e5ac1SBALATON Zoltan #include "hw/qdev-properties.h"
24ba7e5ac1SBALATON Zoltan #include "sysemu/reset.h"
25ba7e5ac1SBALATON Zoltan #include "hw/boards.h"
26ba7e5ac1SBALATON Zoltan #include "hw/loader.h"
27ba7e5ac1SBALATON Zoltan #include "hw/fw-path-provider.h"
28ba7e5ac1SBALATON Zoltan #include "elf.h"
29ba7e5ac1SBALATON Zoltan #include "qemu/log.h"
30ba7e5ac1SBALATON Zoltan #include "qemu/error-report.h"
31ba7e5ac1SBALATON Zoltan #include "sysemu/kvm.h"
32ba7e5ac1SBALATON Zoltan #include "kvm_ppc.h"
33ba7e5ac1SBALATON Zoltan #include "exec/address-spaces.h"
34ba7e5ac1SBALATON Zoltan #include "trace.h"
35ba7e5ac1SBALATON Zoltan #include "qemu/datadir.h"
36ba7e5ac1SBALATON Zoltan #include "sysemu/device_tree.h"
37a6c9808aSBALATON Zoltan #include "hw/ppc/vof.h"
38ba7e5ac1SBALATON Zoltan 
39a6c9808aSBALATON Zoltan #include <libfdt.h>
40a6c9808aSBALATON Zoltan 
41a6c9808aSBALATON Zoltan #define PROM_FILENAME "vof.bin"
42ba7e5ac1SBALATON Zoltan #define PROM_ADDR     0xfff00000
43ba7e5ac1SBALATON Zoltan #define PROM_SIZE     0x80000
44ba7e5ac1SBALATON Zoltan 
45a6c9808aSBALATON Zoltan #define KVMPPC_HCALL_BASE    0xf000
465f2eb049SBALATON Zoltan #define KVMPPC_H_RTAS        (KVMPPC_HCALL_BASE + 0x0)
47a6c9808aSBALATON Zoltan #define KVMPPC_H_VOF_CLIENT  (KVMPPC_HCALL_BASE + 0x5)
48a6c9808aSBALATON Zoltan 
49a6c9808aSBALATON Zoltan #define H_SUCCESS     0
50a6c9808aSBALATON Zoltan #define H_PRIVILEGE  -3  /* Caller not privileged */
51a6c9808aSBALATON Zoltan #define H_PARAMETER  -4  /* Parameter invalid, out-of-range or conflicting */
52a6c9808aSBALATON Zoltan 
53ba7e5ac1SBALATON Zoltan #define BUS_FREQ_HZ 133333333
54ba7e5ac1SBALATON Zoltan 
55a6c9808aSBALATON Zoltan #define PCI0_MEM_BASE 0xc0000000
56a6c9808aSBALATON Zoltan #define PCI0_MEM_SIZE 0x20000000
57a6c9808aSBALATON Zoltan #define PCI0_IO_BASE  0xf8000000
58a6c9808aSBALATON Zoltan #define PCI0_IO_SIZE  0x10000
59a6c9808aSBALATON Zoltan 
60a6c9808aSBALATON Zoltan #define PCI1_MEM_BASE 0x80000000
61a6c9808aSBALATON Zoltan #define PCI1_MEM_SIZE 0x40000000
62a6c9808aSBALATON Zoltan #define PCI1_IO_BASE  0xfe000000
63a6c9808aSBALATON Zoltan #define PCI1_IO_SIZE  0x10000
64a6c9808aSBALATON Zoltan 
65a8eda5edSBALATON Zoltan #define TYPE_PEGASOS2_MACHINE  MACHINE_TYPE_NAME("pegasos2")
66a8eda5edSBALATON Zoltan OBJECT_DECLARE_TYPE(Pegasos2MachineState, MachineClass, PEGASOS2_MACHINE)
67a8eda5edSBALATON Zoltan 
68a8eda5edSBALATON Zoltan struct Pegasos2MachineState {
69a8eda5edSBALATON Zoltan     MachineState parent_obj;
70a8eda5edSBALATON Zoltan     PowerPCCPU *cpu;
71a8eda5edSBALATON Zoltan     DeviceState *mv;
72a6c9808aSBALATON Zoltan     Vof *vof;
73a6c9808aSBALATON Zoltan     void *fdt_blob;
74a6c9808aSBALATON Zoltan     uint64_t kernel_addr;
75a6c9808aSBALATON Zoltan     uint64_t kernel_entry;
76a6c9808aSBALATON Zoltan     uint64_t kernel_size;
77a8eda5edSBALATON Zoltan };
78a8eda5edSBALATON Zoltan 
79a6c9808aSBALATON Zoltan static void *build_fdt(MachineState *machine, int *fdt_size);
80a6c9808aSBALATON Zoltan 
81ba7e5ac1SBALATON Zoltan static void pegasos2_cpu_reset(void *opaque)
82ba7e5ac1SBALATON Zoltan {
83ba7e5ac1SBALATON Zoltan     PowerPCCPU *cpu = opaque;
84a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(current_machine);
85ba7e5ac1SBALATON Zoltan 
86ba7e5ac1SBALATON Zoltan     cpu_reset(CPU(cpu));
87ba7e5ac1SBALATON Zoltan     cpu->env.spr[SPR_HID1] = 7ULL << 28;
88a6c9808aSBALATON Zoltan     if (pm->vof) {
89a6c9808aSBALATON Zoltan         cpu->env.gpr[1] = 2 * VOF_STACK_SIZE - 0x20;
90a6c9808aSBALATON Zoltan         cpu->env.nip = 0x100;
91a6c9808aSBALATON Zoltan     }
92ba7e5ac1SBALATON Zoltan }
93ba7e5ac1SBALATON Zoltan 
94ba7e5ac1SBALATON Zoltan static void pegasos2_init(MachineState *machine)
95ba7e5ac1SBALATON Zoltan {
96a8eda5edSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
97a8eda5edSBALATON Zoltan     CPUPPCState *env;
98ba7e5ac1SBALATON Zoltan     MemoryRegion *rom = g_new(MemoryRegion, 1);
99ba7e5ac1SBALATON Zoltan     PCIBus *pci_bus;
100ba7e5ac1SBALATON Zoltan     PCIDevice *dev;
101ba7e5ac1SBALATON Zoltan     I2CBus *i2c_bus;
102ba7e5ac1SBALATON Zoltan     const char *fwname = machine->firmware ?: PROM_FILENAME;
103ba7e5ac1SBALATON Zoltan     char *filename;
104ba7e5ac1SBALATON Zoltan     int sz;
105ba7e5ac1SBALATON Zoltan     uint8_t *spd_data;
106ba7e5ac1SBALATON Zoltan 
107ba7e5ac1SBALATON Zoltan     /* init CPU */
108a8eda5edSBALATON Zoltan     pm->cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
109a8eda5edSBALATON Zoltan     env = &pm->cpu->env;
110a8eda5edSBALATON Zoltan     if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
111ba7e5ac1SBALATON Zoltan         error_report("Incompatible CPU, only 6xx bus supported");
112ba7e5ac1SBALATON Zoltan         exit(1);
113ba7e5ac1SBALATON Zoltan     }
114ba7e5ac1SBALATON Zoltan 
115ba7e5ac1SBALATON Zoltan     /* Set time-base frequency */
116a8eda5edSBALATON Zoltan     cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4);
117a8eda5edSBALATON Zoltan     qemu_register_reset(pegasos2_cpu_reset, pm->cpu);
118ba7e5ac1SBALATON Zoltan 
119ba7e5ac1SBALATON Zoltan     /* RAM */
120ba7e5ac1SBALATON Zoltan     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
121ba7e5ac1SBALATON Zoltan 
122ba7e5ac1SBALATON Zoltan     /* allocate and load firmware */
123ba7e5ac1SBALATON Zoltan     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname);
124ba7e5ac1SBALATON Zoltan     if (!filename) {
125ba7e5ac1SBALATON Zoltan         error_report("Could not find firmware '%s'", fwname);
126ba7e5ac1SBALATON Zoltan         exit(1);
127ba7e5ac1SBALATON Zoltan     }
128a6c9808aSBALATON Zoltan     if (!machine->firmware && !pm->vof) {
129a6c9808aSBALATON Zoltan         pm->vof = g_malloc0(sizeof(*pm->vof));
130a6c9808aSBALATON Zoltan     }
131ba7e5ac1SBALATON Zoltan     memory_region_init_rom(rom, NULL, "pegasos2.rom", PROM_SIZE, &error_fatal);
132ba7e5ac1SBALATON Zoltan     memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
133ba7e5ac1SBALATON Zoltan     sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1,
134ba7e5ac1SBALATON Zoltan                   PPC_ELF_MACHINE, 0, 0);
135ba7e5ac1SBALATON Zoltan     if (sz <= 0) {
136a6c9808aSBALATON Zoltan         sz = load_image_targphys(filename, pm->vof ? 0 : PROM_ADDR, PROM_SIZE);
137ba7e5ac1SBALATON Zoltan     }
138ba7e5ac1SBALATON Zoltan     if (sz <= 0 || sz > PROM_SIZE) {
139ba7e5ac1SBALATON Zoltan         error_report("Could not load firmware '%s'", filename);
140ba7e5ac1SBALATON Zoltan         exit(1);
141ba7e5ac1SBALATON Zoltan     }
142ba7e5ac1SBALATON Zoltan     g_free(filename);
143a6c9808aSBALATON Zoltan     if (pm->vof) {
144a6c9808aSBALATON Zoltan         pm->vof->fw_size = sz;
145a6c9808aSBALATON Zoltan     }
146ba7e5ac1SBALATON Zoltan 
147ba7e5ac1SBALATON Zoltan     /* Marvell Discovery II system controller */
148a8eda5edSBALATON Zoltan     pm->mv = DEVICE(sysbus_create_simple(TYPE_MV64361, -1,
149a8eda5edSBALATON Zoltan                              ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT]));
150a8eda5edSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 1);
151ba7e5ac1SBALATON Zoltan 
152ba7e5ac1SBALATON Zoltan     /* VIA VT8231 South Bridge (multifunction PCI device) */
153ba7e5ac1SBALATON Zoltan     /* VT8231 function 0: PCI-to-ISA Bridge */
154ba7e5ac1SBALATON Zoltan     dev = pci_create_simple_multifunction(pci_bus, PCI_DEVFN(12, 0), true,
155ba7e5ac1SBALATON Zoltan                                           TYPE_VT8231_ISA);
156ba7e5ac1SBALATON Zoltan     qdev_connect_gpio_out(DEVICE(dev), 0,
157a8eda5edSBALATON Zoltan                           qdev_get_gpio_in_named(pm->mv, "gpp", 31));
158ba7e5ac1SBALATON Zoltan 
159ba7e5ac1SBALATON Zoltan     /* VT8231 function 1: IDE Controller */
160ba7e5ac1SBALATON Zoltan     dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 1), "via-ide");
161ba7e5ac1SBALATON Zoltan     pci_ide_create_devs(dev);
162ba7e5ac1SBALATON Zoltan 
163ba7e5ac1SBALATON Zoltan     /* VT8231 function 2-3: USB Ports */
164ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 2), "vt82c686b-usb-uhci");
165ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 3), "vt82c686b-usb-uhci");
166ba7e5ac1SBALATON Zoltan 
167ba7e5ac1SBALATON Zoltan     /* VT8231 function 4: Power Management Controller */
168ba7e5ac1SBALATON Zoltan     dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 4), TYPE_VT8231_PM);
169ba7e5ac1SBALATON Zoltan     i2c_bus = I2C_BUS(qdev_get_child_bus(DEVICE(dev), "i2c"));
170ba7e5ac1SBALATON Zoltan     spd_data = spd_data_generate(DDR, machine->ram_size);
171ba7e5ac1SBALATON Zoltan     smbus_eeprom_init_one(i2c_bus, 0x57, spd_data);
172ba7e5ac1SBALATON Zoltan 
173ba7e5ac1SBALATON Zoltan     /* VT8231 function 5-6: AC97 Audio & Modem */
174ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 5), TYPE_VIA_AC97);
175ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 6), TYPE_VIA_MC97);
176ba7e5ac1SBALATON Zoltan 
177ba7e5ac1SBALATON Zoltan     /* other PC hardware */
178ba7e5ac1SBALATON Zoltan     pci_vga_init(pci_bus);
179a6c9808aSBALATON Zoltan 
180a6c9808aSBALATON Zoltan     if (machine->kernel_filename) {
181a6c9808aSBALATON Zoltan         sz = load_elf(machine->kernel_filename, NULL, NULL, NULL,
182a6c9808aSBALATON Zoltan                       &pm->kernel_entry, &pm->kernel_addr, NULL, NULL, 1,
183a6c9808aSBALATON Zoltan                       PPC_ELF_MACHINE, 0, 0);
184a6c9808aSBALATON Zoltan         if (sz <= 0) {
185a6c9808aSBALATON Zoltan             error_report("Could not load kernel '%s'",
186a6c9808aSBALATON Zoltan                          machine->kernel_filename);
187a6c9808aSBALATON Zoltan             exit(1);
188a6c9808aSBALATON Zoltan         }
189a6c9808aSBALATON Zoltan         pm->kernel_size = sz;
190a6c9808aSBALATON Zoltan         if (!pm->vof) {
191a6c9808aSBALATON Zoltan             warn_report("Option -kernel may be ineffective with -bios.");
192a6c9808aSBALATON Zoltan         }
193a6c9808aSBALATON Zoltan     }
194*6ebc0048SBALATON Zoltan     if (!pm->vof && machine->kernel_cmdline && machine->kernel_cmdline[0]) {
195a6c9808aSBALATON Zoltan         warn_report("Option -append may be ineffective with -bios.");
196a6c9808aSBALATON Zoltan     }
197a6c9808aSBALATON Zoltan }
198a6c9808aSBALATON Zoltan 
1995f2eb049SBALATON Zoltan static uint32_t pegasos2_pci_config_read(AddressSpace *as, int bus,
2005f2eb049SBALATON Zoltan                                          uint32_t addr, uint32_t len)
2015f2eb049SBALATON Zoltan {
2025f2eb049SBALATON Zoltan     hwaddr pcicfg = (bus ? 0xf1000c78 : 0xf1000cf8);
2035f2eb049SBALATON Zoltan     uint32_t val = 0xffffffff;
2045f2eb049SBALATON Zoltan 
2055f2eb049SBALATON Zoltan     stl_le_phys(as, pcicfg, addr | BIT(31));
2065f2eb049SBALATON Zoltan     switch (len) {
2075f2eb049SBALATON Zoltan     case 4:
2085f2eb049SBALATON Zoltan         val = ldl_le_phys(as, pcicfg + 4);
2095f2eb049SBALATON Zoltan         break;
2105f2eb049SBALATON Zoltan     case 2:
2115f2eb049SBALATON Zoltan         val = lduw_le_phys(as, pcicfg + 4);
2125f2eb049SBALATON Zoltan         break;
2135f2eb049SBALATON Zoltan     case 1:
2145f2eb049SBALATON Zoltan         val = ldub_phys(as, pcicfg + 4);
2155f2eb049SBALATON Zoltan         break;
2165f2eb049SBALATON Zoltan     default:
2175f2eb049SBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid length\n", __func__);
2185f2eb049SBALATON Zoltan         break;
2195f2eb049SBALATON Zoltan     }
2205f2eb049SBALATON Zoltan     return val;
2215f2eb049SBALATON Zoltan }
2225f2eb049SBALATON Zoltan 
223a6c9808aSBALATON Zoltan static void pegasos2_pci_config_write(AddressSpace *as, int bus, uint32_t addr,
224a6c9808aSBALATON Zoltan                                       uint32_t len, uint32_t val)
225a6c9808aSBALATON Zoltan {
226a6c9808aSBALATON Zoltan     hwaddr pcicfg = (bus ? 0xf1000c78 : 0xf1000cf8);
227a6c9808aSBALATON Zoltan 
228a6c9808aSBALATON Zoltan     stl_le_phys(as, pcicfg, addr | BIT(31));
229a6c9808aSBALATON Zoltan     switch (len) {
230a6c9808aSBALATON Zoltan     case 4:
231a6c9808aSBALATON Zoltan         stl_le_phys(as, pcicfg + 4, val);
232a6c9808aSBALATON Zoltan         break;
233a6c9808aSBALATON Zoltan     case 2:
234a6c9808aSBALATON Zoltan         stw_le_phys(as, pcicfg + 4, val);
235a6c9808aSBALATON Zoltan         break;
236a6c9808aSBALATON Zoltan     case 1:
237a6c9808aSBALATON Zoltan         stb_phys(as, pcicfg + 4, val);
238a6c9808aSBALATON Zoltan         break;
239a6c9808aSBALATON Zoltan     default:
240a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid length\n", __func__);
241a6c9808aSBALATON Zoltan         break;
242a6c9808aSBALATON Zoltan     }
243a6c9808aSBALATON Zoltan }
244a6c9808aSBALATON Zoltan 
245a6c9808aSBALATON Zoltan static void pegasos2_machine_reset(MachineState *machine)
246a6c9808aSBALATON Zoltan {
247a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
248a6c9808aSBALATON Zoltan     AddressSpace *as = CPU(pm->cpu)->as;
249a6c9808aSBALATON Zoltan     void *fdt;
250a6c9808aSBALATON Zoltan     uint64_t d[2];
251a6c9808aSBALATON Zoltan     int sz;
252a6c9808aSBALATON Zoltan 
253a6c9808aSBALATON Zoltan     qemu_devices_reset();
254a6c9808aSBALATON Zoltan     if (!pm->vof) {
255a6c9808aSBALATON Zoltan         return; /* Firmware should set up machine so nothing to do */
256a6c9808aSBALATON Zoltan     }
257a6c9808aSBALATON Zoltan 
258a6c9808aSBALATON Zoltan     /* Otherwise, set up devices that board firmware would normally do */
259a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf1000000, 0x28020ff);
260a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf1000278, 0xa31fc);
261a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf100f300, 0x11ff0400);
262a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf100f10c, 0x80000000);
263a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf100001c, 0x8000000);
264a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 0, PCI_COMMAND, 2, PCI_COMMAND_IO |
265a6c9808aSBALATON Zoltan                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
266a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, PCI_COMMAND, 2, PCI_COMMAND_IO |
267a6c9808aSBALATON Zoltan                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
268a6c9808aSBALATON Zoltan 
269a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 0) << 8) |
270a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x9);
271a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 0) << 8) |
272a6c9808aSBALATON Zoltan                               0x50, 1, 0x2);
273a6c9808aSBALATON Zoltan 
274a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
275a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x109);
276a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
277a6c9808aSBALATON Zoltan                               PCI_CLASS_PROG, 1, 0xf);
278a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
279a6c9808aSBALATON Zoltan                               0x40, 1, 0xb);
280a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
281a6c9808aSBALATON Zoltan                               0x50, 4, 0x17171717);
282a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
283a6c9808aSBALATON Zoltan                               PCI_COMMAND, 2, 0x87);
284a6c9808aSBALATON Zoltan 
285a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 2) << 8) |
286a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x409);
287a6c9808aSBALATON Zoltan 
288a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 3) << 8) |
289a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x409);
290a6c9808aSBALATON Zoltan 
291a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
292a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x9);
293a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
294a6c9808aSBALATON Zoltan                               0x48, 4, 0xf00);
295a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
296a6c9808aSBALATON Zoltan                               0x40, 4, 0x558020);
297a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
298a6c9808aSBALATON Zoltan                               0x90, 4, 0xd00);
299a6c9808aSBALATON Zoltan 
300a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 5) << 8) |
301a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x309);
302a6c9808aSBALATON Zoltan 
303a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 6) << 8) |
304a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x309);
305a6c9808aSBALATON Zoltan 
306a6c9808aSBALATON Zoltan     /* Device tree and VOF set up */
307a6c9808aSBALATON Zoltan     vof_init(pm->vof, machine->ram_size, &error_fatal);
308a6c9808aSBALATON Zoltan     if (vof_claim(pm->vof, 0, VOF_STACK_SIZE, VOF_STACK_SIZE) == -1) {
309a6c9808aSBALATON Zoltan         error_report("Memory allocation for stack failed");
310a6c9808aSBALATON Zoltan         exit(1);
311a6c9808aSBALATON Zoltan     }
312a6c9808aSBALATON Zoltan     if (pm->kernel_size &&
313a6c9808aSBALATON Zoltan         vof_claim(pm->vof, pm->kernel_addr, pm->kernel_size, 0) == -1) {
314a6c9808aSBALATON Zoltan         error_report("Memory for kernel is in use");
315a6c9808aSBALATON Zoltan         exit(1);
316a6c9808aSBALATON Zoltan     }
317a6c9808aSBALATON Zoltan     fdt = build_fdt(machine, &sz);
318a6c9808aSBALATON Zoltan     /* FIXME: VOF assumes entry is same as load address */
319a6c9808aSBALATON Zoltan     d[0] = cpu_to_be64(pm->kernel_entry);
320a6c9808aSBALATON Zoltan     d[1] = cpu_to_be64(pm->kernel_size - (pm->kernel_entry - pm->kernel_addr));
321a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/chosen", "qemu,boot-kernel", d, sizeof(d));
322a6c9808aSBALATON Zoltan 
323a6c9808aSBALATON Zoltan     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
324a6c9808aSBALATON Zoltan     g_free(pm->fdt_blob);
325a6c9808aSBALATON Zoltan     pm->fdt_blob = fdt;
326a6c9808aSBALATON Zoltan 
327a6c9808aSBALATON Zoltan     vof_build_dt(fdt, pm->vof);
328a6c9808aSBALATON Zoltan     vof_client_open_store(fdt, pm->vof, "/chosen", "stdout", "/failsafe");
329a6c9808aSBALATON Zoltan     pm->cpu->vhyp = PPC_VIRTUAL_HYPERVISOR(machine);
330a6c9808aSBALATON Zoltan }
331a6c9808aSBALATON Zoltan 
3325f2eb049SBALATON Zoltan enum pegasos2_rtas_tokens {
3335f2eb049SBALATON Zoltan     RTAS_RESTART_RTAS = 0,
3345f2eb049SBALATON Zoltan     RTAS_NVRAM_FETCH = 1,
3355f2eb049SBALATON Zoltan     RTAS_NVRAM_STORE = 2,
3365f2eb049SBALATON Zoltan     RTAS_GET_TIME_OF_DAY = 3,
3375f2eb049SBALATON Zoltan     RTAS_SET_TIME_OF_DAY = 4,
3385f2eb049SBALATON Zoltan     RTAS_EVENT_SCAN = 6,
3395f2eb049SBALATON Zoltan     RTAS_CHECK_EXCEPTION = 7,
3405f2eb049SBALATON Zoltan     RTAS_READ_PCI_CONFIG = 8,
3415f2eb049SBALATON Zoltan     RTAS_WRITE_PCI_CONFIG = 9,
3425f2eb049SBALATON Zoltan     RTAS_DISPLAY_CHARACTER = 10,
3435f2eb049SBALATON Zoltan     RTAS_SET_INDICATOR = 11,
3445f2eb049SBALATON Zoltan     RTAS_POWER_OFF = 17,
3455f2eb049SBALATON Zoltan     RTAS_SUSPEND = 18,
3465f2eb049SBALATON Zoltan     RTAS_HIBERNATE = 19,
3475f2eb049SBALATON Zoltan     RTAS_SYSTEM_REBOOT = 20,
3485f2eb049SBALATON Zoltan };
3495f2eb049SBALATON Zoltan 
3505f2eb049SBALATON Zoltan static target_ulong pegasos2_rtas(PowerPCCPU *cpu, Pegasos2MachineState *pm,
3515f2eb049SBALATON Zoltan                                   target_ulong args_real)
3525f2eb049SBALATON Zoltan {
3535f2eb049SBALATON Zoltan     AddressSpace *as = CPU(cpu)->as;
3545f2eb049SBALATON Zoltan     uint32_t token = ldl_be_phys(as, args_real);
3555f2eb049SBALATON Zoltan     uint32_t nargs = ldl_be_phys(as, args_real + 4);
3565f2eb049SBALATON Zoltan     uint32_t nrets = ldl_be_phys(as, args_real + 8);
3575f2eb049SBALATON Zoltan     uint32_t args = args_real + 12;
3585f2eb049SBALATON Zoltan     uint32_t rets = args_real + 12 + nargs * 4;
3595f2eb049SBALATON Zoltan 
3605f2eb049SBALATON Zoltan     if (nrets < 1) {
3615f2eb049SBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Too few return values in RTAS call\n");
3625f2eb049SBALATON Zoltan         return H_PARAMETER;
3635f2eb049SBALATON Zoltan     }
3645f2eb049SBALATON Zoltan     switch (token) {
3655f2eb049SBALATON Zoltan     case RTAS_READ_PCI_CONFIG:
3665f2eb049SBALATON Zoltan     {
3675f2eb049SBALATON Zoltan         uint32_t addr, len, val;
3685f2eb049SBALATON Zoltan 
3695f2eb049SBALATON Zoltan         if (nargs != 2 || nrets != 2) {
3705f2eb049SBALATON Zoltan             stl_be_phys(as, rets, -1);
3715f2eb049SBALATON Zoltan             return H_PARAMETER;
3725f2eb049SBALATON Zoltan         }
3735f2eb049SBALATON Zoltan         addr = ldl_be_phys(as, args);
3745f2eb049SBALATON Zoltan         len = ldl_be_phys(as, args + 4);
3755f2eb049SBALATON Zoltan         val = pegasos2_pci_config_read(as, !(addr >> 24),
3765f2eb049SBALATON Zoltan                                        addr & 0x0fffffff, len);
3775f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
3785f2eb049SBALATON Zoltan         stl_be_phys(as, rets + 4, val);
3795f2eb049SBALATON Zoltan         return H_SUCCESS;
3805f2eb049SBALATON Zoltan     }
3815f2eb049SBALATON Zoltan     case RTAS_WRITE_PCI_CONFIG:
3825f2eb049SBALATON Zoltan     {
3835f2eb049SBALATON Zoltan         uint32_t addr, len, val;
3845f2eb049SBALATON Zoltan 
3855f2eb049SBALATON Zoltan         if (nargs != 3 || nrets != 1) {
3865f2eb049SBALATON Zoltan             stl_be_phys(as, rets, -1);
3875f2eb049SBALATON Zoltan             return H_PARAMETER;
3885f2eb049SBALATON Zoltan         }
3895f2eb049SBALATON Zoltan         addr = ldl_be_phys(as, args);
3905f2eb049SBALATON Zoltan         len = ldl_be_phys(as, args + 4);
3915f2eb049SBALATON Zoltan         val = ldl_be_phys(as, args + 8);
3925f2eb049SBALATON Zoltan         pegasos2_pci_config_write(as, !(addr >> 24),
3935f2eb049SBALATON Zoltan                                   addr & 0x0fffffff, len, val);
3945f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
3955f2eb049SBALATON Zoltan         return H_SUCCESS;
3965f2eb049SBALATON Zoltan     }
3975f2eb049SBALATON Zoltan     case RTAS_DISPLAY_CHARACTER:
3985f2eb049SBALATON Zoltan         if (nargs != 1 || nrets != 1) {
3995f2eb049SBALATON Zoltan             stl_be_phys(as, rets, -1);
4005f2eb049SBALATON Zoltan             return H_PARAMETER;
4015f2eb049SBALATON Zoltan         }
4025f2eb049SBALATON Zoltan         qemu_log_mask(LOG_UNIMP, "%c", ldl_be_phys(as, args));
4035f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
4045f2eb049SBALATON Zoltan         return H_SUCCESS;
4055f2eb049SBALATON Zoltan     default:
4065f2eb049SBALATON Zoltan         qemu_log_mask(LOG_UNIMP, "Unknown RTAS token %u (args=%u, rets=%u)\n",
4075f2eb049SBALATON Zoltan                       token, nargs, nrets);
4085f2eb049SBALATON Zoltan         stl_be_phys(as, rets, 0);
4095f2eb049SBALATON Zoltan         return H_SUCCESS;
4105f2eb049SBALATON Zoltan     }
4115f2eb049SBALATON Zoltan }
4125f2eb049SBALATON Zoltan 
413a6c9808aSBALATON Zoltan static void pegasos2_hypercall(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
414a6c9808aSBALATON Zoltan {
415a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(vhyp);
416a6c9808aSBALATON Zoltan     CPUPPCState *env = &cpu->env;
417a6c9808aSBALATON Zoltan 
418a6c9808aSBALATON Zoltan     /* The TCG path should also be holding the BQL at this point */
419a6c9808aSBALATON Zoltan     g_assert(qemu_mutex_iothread_locked());
420a6c9808aSBALATON Zoltan 
421a6c9808aSBALATON Zoltan     if (msr_pr) {
422a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Hypercall made with MSR[PR]=1\n");
423a6c9808aSBALATON Zoltan         env->gpr[3] = H_PRIVILEGE;
4245f2eb049SBALATON Zoltan     } else if (env->gpr[3] == KVMPPC_H_RTAS) {
4255f2eb049SBALATON Zoltan         env->gpr[3] = pegasos2_rtas(cpu, pm, env->gpr[4]);
426a6c9808aSBALATON Zoltan     } else if (env->gpr[3] == KVMPPC_H_VOF_CLIENT) {
427a6c9808aSBALATON Zoltan         int ret = vof_client_call(MACHINE(pm), pm->vof, pm->fdt_blob,
428a6c9808aSBALATON Zoltan                                   env->gpr[4]);
429a6c9808aSBALATON Zoltan         env->gpr[3] = (ret ? H_PARAMETER : H_SUCCESS);
430a6c9808aSBALATON Zoltan     } else {
431a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Unsupported hypercall " TARGET_FMT_lx
432a6c9808aSBALATON Zoltan                       "\n", env->gpr[3]);
433a6c9808aSBALATON Zoltan         env->gpr[3] = -1;
434a6c9808aSBALATON Zoltan     }
435a6c9808aSBALATON Zoltan }
436a6c9808aSBALATON Zoltan 
437a6c9808aSBALATON Zoltan static void vhyp_nop(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
438a6c9808aSBALATON Zoltan {
439a6c9808aSBALATON Zoltan }
440a6c9808aSBALATON Zoltan 
441a6c9808aSBALATON Zoltan static target_ulong vhyp_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
442a6c9808aSBALATON Zoltan {
443a6c9808aSBALATON Zoltan     return POWERPC_CPU(current_cpu)->env.spr[SPR_SDR1];
444ba7e5ac1SBALATON Zoltan }
445ba7e5ac1SBALATON Zoltan 
446a312aaebSBALATON Zoltan static bool pegasos2_setprop(MachineState *ms, const char *path,
447a312aaebSBALATON Zoltan                              const char *propname, void *val, int vallen)
448a312aaebSBALATON Zoltan {
449a312aaebSBALATON Zoltan     return true;
450a312aaebSBALATON Zoltan }
451a312aaebSBALATON Zoltan 
452a8eda5edSBALATON Zoltan static void pegasos2_machine_class_init(ObjectClass *oc, void *data)
453ba7e5ac1SBALATON Zoltan {
454a8eda5edSBALATON Zoltan     MachineClass *mc = MACHINE_CLASS(oc);
455a6c9808aSBALATON Zoltan     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
456a312aaebSBALATON Zoltan     VofMachineIfClass *vmc = VOF_MACHINE_CLASS(oc);
457a8eda5edSBALATON Zoltan 
458ba7e5ac1SBALATON Zoltan     mc->desc = "Genesi/bPlan Pegasos II";
459ba7e5ac1SBALATON Zoltan     mc->init = pegasos2_init;
460a6c9808aSBALATON Zoltan     mc->reset = pegasos2_machine_reset;
461ba7e5ac1SBALATON Zoltan     mc->block_default_type = IF_IDE;
462ba7e5ac1SBALATON Zoltan     mc->default_boot_order = "cd";
463ba7e5ac1SBALATON Zoltan     mc->default_display = "std";
464ba7e5ac1SBALATON Zoltan     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7400_v2.9");
465ba7e5ac1SBALATON Zoltan     mc->default_ram_id = "pegasos2.ram";
466ba7e5ac1SBALATON Zoltan     mc->default_ram_size = 512 * MiB;
467a6c9808aSBALATON Zoltan 
468a6c9808aSBALATON Zoltan     vhc->hypercall = pegasos2_hypercall;
469a6c9808aSBALATON Zoltan     vhc->cpu_exec_enter = vhyp_nop;
470a6c9808aSBALATON Zoltan     vhc->cpu_exec_exit = vhyp_nop;
471a6c9808aSBALATON Zoltan     vhc->encode_hpt_for_kvm_pr = vhyp_encode_hpt_for_kvm_pr;
472a312aaebSBALATON Zoltan 
473a312aaebSBALATON Zoltan     vmc->setprop = pegasos2_setprop;
474ba7e5ac1SBALATON Zoltan }
475ba7e5ac1SBALATON Zoltan 
476a8eda5edSBALATON Zoltan static const TypeInfo pegasos2_machine_info = {
477a8eda5edSBALATON Zoltan     .name          = TYPE_PEGASOS2_MACHINE,
478a8eda5edSBALATON Zoltan     .parent        = TYPE_MACHINE,
479a8eda5edSBALATON Zoltan     .class_init    = pegasos2_machine_class_init,
480a8eda5edSBALATON Zoltan     .instance_size = sizeof(Pegasos2MachineState),
481a6c9808aSBALATON Zoltan     .interfaces = (InterfaceInfo[]) {
482a6c9808aSBALATON Zoltan         { TYPE_PPC_VIRTUAL_HYPERVISOR },
483a312aaebSBALATON Zoltan         { TYPE_VOF_MACHINE_IF },
484a6c9808aSBALATON Zoltan         { }
485a6c9808aSBALATON Zoltan     },
486a8eda5edSBALATON Zoltan };
487a8eda5edSBALATON Zoltan 
488a8eda5edSBALATON Zoltan static void pegasos2_machine_register_types(void)
489a8eda5edSBALATON Zoltan {
490a8eda5edSBALATON Zoltan     type_register_static(&pegasos2_machine_info);
491a8eda5edSBALATON Zoltan }
492a8eda5edSBALATON Zoltan 
493a8eda5edSBALATON Zoltan type_init(pegasos2_machine_register_types)
494a6c9808aSBALATON Zoltan 
495a6c9808aSBALATON Zoltan /* FDT creation for passing to firmware */
496a6c9808aSBALATON Zoltan 
497a6c9808aSBALATON Zoltan typedef struct {
498a6c9808aSBALATON Zoltan     void *fdt;
499a6c9808aSBALATON Zoltan     const char *path;
500a6c9808aSBALATON Zoltan } FDTInfo;
501a6c9808aSBALATON Zoltan 
502a6c9808aSBALATON Zoltan /* We do everything in reverse order so it comes out right in the tree */
503a6c9808aSBALATON Zoltan 
504a6c9808aSBALATON Zoltan static void dt_ide(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
505a6c9808aSBALATON Zoltan {
506a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "spi");
507a6c9808aSBALATON Zoltan }
508a6c9808aSBALATON Zoltan 
509a6c9808aSBALATON Zoltan static void dt_usb(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
510a6c9808aSBALATON Zoltan {
511a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 0);
512a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 1);
513a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "usb");
514a6c9808aSBALATON Zoltan }
515a6c9808aSBALATON Zoltan 
516a6c9808aSBALATON Zoltan static void dt_isa(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
517a6c9808aSBALATON Zoltan {
518a6c9808aSBALATON Zoltan     GString *name = g_string_sized_new(64);
519a6c9808aSBALATON Zoltan     uint32_t cells[3];
520a6c9808aSBALATON Zoltan 
521a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 1);
522a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 2);
523a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "isa");
524a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "name", "isa");
525a6c9808aSBALATON Zoltan 
526a6c9808aSBALATON Zoltan     /* addional devices */
527a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/lpt@i3bc", fi->path);
528a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
529a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
530a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(7);
531a6c9808aSBALATON Zoltan     cells[1] = 0;
532a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
533a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
534a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
535a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x3bc);
536a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
537a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
538a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "lpt");
539a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "lpt");
540a6c9808aSBALATON Zoltan 
541a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/fdc@i3f0", fi->path);
542a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
543a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
544a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(6);
545a6c9808aSBALATON Zoltan     cells[1] = 0;
546a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
547a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
548a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
549a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x3f0);
550a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
551a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
552a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "fdc");
553a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "fdc");
554a6c9808aSBALATON Zoltan 
555a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/timer@i40", fi->path);
556a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
557a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
558a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
559a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x40);
560a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
561a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
562a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "timer");
563a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "timer");
564a6c9808aSBALATON Zoltan 
565a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/rtc@i70", fi->path);
566a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
567a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "compatible", "ds1385-rtc");
568a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
569a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(8);
570a6c9808aSBALATON Zoltan     cells[1] = 0;
571a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
572a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
573a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
574a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x70);
575a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(2);
576a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
577a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "rtc");
578a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "rtc");
579a6c9808aSBALATON Zoltan 
580a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/keyboard@i60", fi->path);
581a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
582a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
583a6c9808aSBALATON Zoltan     cells[1] = 0;
584a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
585a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
586a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
587a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x60);
588a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(5);
589a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
590a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "keyboard");
591a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "keyboard");
592a6c9808aSBALATON Zoltan 
593a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/8042@i60", fi->path);
594a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
595a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#interrupt-cells", 2);
596a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#size-cells", 0);
597a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#address-cells", 1);
598a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "interrupt-controller", "");
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(0x60);
602a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(5);
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", "");
605a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "8042");
606a6c9808aSBALATON Zoltan 
607a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/serial@i2f8", fi->path);
608a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
609a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
610a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(3);
611a6c9808aSBALATON Zoltan     cells[1] = 0;
612a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
613a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
614a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
615a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x2f8);
616a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
617a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
618a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "serial");
619a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "serial");
620a6c9808aSBALATON Zoltan 
621a6c9808aSBALATON Zoltan     g_string_free(name, TRUE);
622a6c9808aSBALATON Zoltan }
623a6c9808aSBALATON Zoltan 
624a6c9808aSBALATON Zoltan static struct {
625a6c9808aSBALATON Zoltan     const char *id;
626a6c9808aSBALATON Zoltan     const char *name;
627a6c9808aSBALATON Zoltan     void (*dtf)(PCIBus *bus, PCIDevice *d, FDTInfo *fi);
628a6c9808aSBALATON Zoltan } device_map[] = {
629a6c9808aSBALATON Zoltan     { "pci11ab,6460", "host", NULL },
630a6c9808aSBALATON Zoltan     { "pci1106,8231", "isa", dt_isa },
631a6c9808aSBALATON Zoltan     { "pci1106,571", "ide", dt_ide },
632a6c9808aSBALATON Zoltan     { "pci1106,3044", "firewire", NULL },
633a6c9808aSBALATON Zoltan     { "pci1106,3038", "usb", dt_usb },
634a6c9808aSBALATON Zoltan     { "pci1106,8235", "other", NULL },
635a6c9808aSBALATON Zoltan     { "pci1106,3058", "sound", NULL },
636a6c9808aSBALATON Zoltan     { NULL, NULL }
637a6c9808aSBALATON Zoltan };
638a6c9808aSBALATON Zoltan 
639a6c9808aSBALATON Zoltan static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
640a6c9808aSBALATON Zoltan {
641a6c9808aSBALATON Zoltan     FDTInfo *fi = opaque;
642a6c9808aSBALATON Zoltan     GString *node = g_string_new(NULL);
643a6c9808aSBALATON Zoltan     uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
644a6c9808aSBALATON Zoltan     int i, j;
645a6c9808aSBALATON Zoltan     const char *name = NULL;
646a6c9808aSBALATON Zoltan     g_autofree const gchar *pn = g_strdup_printf("pci%x,%x",
647a6c9808aSBALATON Zoltan                                      pci_get_word(&d->config[PCI_VENDOR_ID]),
648a6c9808aSBALATON Zoltan                                      pci_get_word(&d->config[PCI_DEVICE_ID]));
649a6c9808aSBALATON Zoltan 
650a6c9808aSBALATON Zoltan     for (i = 0; device_map[i].id; i++) {
651a6c9808aSBALATON Zoltan         if (!strcmp(pn, device_map[i].id)) {
652a6c9808aSBALATON Zoltan             name = device_map[i].name;
653a6c9808aSBALATON Zoltan             break;
654a6c9808aSBALATON Zoltan         }
655a6c9808aSBALATON Zoltan     }
656a6c9808aSBALATON Zoltan     g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
657a6c9808aSBALATON Zoltan                     PCI_SLOT(d->devfn));
658a6c9808aSBALATON Zoltan     if (PCI_FUNC(d->devfn)) {
659a6c9808aSBALATON Zoltan         g_string_append_printf(node, ",%x", PCI_FUNC(d->devfn));
660a6c9808aSBALATON Zoltan     }
661a6c9808aSBALATON Zoltan 
662a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, node->str);
663a6c9808aSBALATON Zoltan     if (device_map[i].dtf) {
664a6c9808aSBALATON Zoltan         FDTInfo cfi = { fi->fdt, node->str };
665a6c9808aSBALATON Zoltan         device_map[i].dtf(bus, d, &cfi);
666a6c9808aSBALATON Zoltan     }
667a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(d->devfn << 8);
668a6c9808aSBALATON Zoltan     cells[1] = 0;
669a6c9808aSBALATON Zoltan     cells[2] = 0;
670a6c9808aSBALATON Zoltan     cells[3] = 0;
671a6c9808aSBALATON Zoltan     cells[4] = 0;
672a6c9808aSBALATON Zoltan     j = 5;
673a6c9808aSBALATON Zoltan     for (i = 0; i < PCI_NUM_REGIONS; i++) {
674a6c9808aSBALATON Zoltan         if (!d->io_regions[i].size) {
675a6c9808aSBALATON Zoltan             continue;
676a6c9808aSBALATON Zoltan         }
677a6c9808aSBALATON Zoltan         cells[j] = cpu_to_be32(d->devfn << 8 | (PCI_BASE_ADDRESS_0 + i * 4));
678a6c9808aSBALATON Zoltan         if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
679a6c9808aSBALATON Zoltan             cells[j] |= cpu_to_be32(1 << 24);
680a6c9808aSBALATON Zoltan         } else {
681a6c9808aSBALATON Zoltan             cells[j] |= cpu_to_be32(2 << 24);
682a6c9808aSBALATON Zoltan             if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
683a6c9808aSBALATON Zoltan                 cells[j] |= cpu_to_be32(4 << 28);
684a6c9808aSBALATON Zoltan             }
685a6c9808aSBALATON Zoltan         }
686a6c9808aSBALATON Zoltan         cells[j + 1] = 0;
687a6c9808aSBALATON Zoltan         cells[j + 2] = 0;
688a6c9808aSBALATON Zoltan         cells[j + 3] = cpu_to_be32(d->io_regions[i].size >> 32);
689a6c9808aSBALATON Zoltan         cells[j + 4] = cpu_to_be32(d->io_regions[i].size);
690a6c9808aSBALATON Zoltan         j += 5;
691a6c9808aSBALATON Zoltan     }
692a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, node->str, "reg", cells, j * sizeof(cells[0]));
693a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, node->str, "name", name ?: pn);
694a6c9808aSBALATON Zoltan     if (pci_get_byte(&d->config[PCI_INTERRUPT_PIN])) {
695a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fi->fdt, node->str, "interrupts",
696a6c9808aSBALATON Zoltan                               pci_get_byte(&d->config[PCI_INTERRUPT_PIN]));
697a6c9808aSBALATON Zoltan     }
698a6c9808aSBALATON Zoltan     /* Pegasos2 firmware has subsystem-id amd subsystem-vendor-id swapped */
699a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-vendor-id",
700a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_SUBSYSTEM_ID]));
701a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-id",
702a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_SUBSYSTEM_VENDOR_ID]));
703a6c9808aSBALATON Zoltan     cells[0] = pci_get_long(&d->config[PCI_CLASS_REVISION]);
704a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "class-code", cells[0] >> 8);
705e7dfb29eSDavid Gibson     qemu_fdt_setprop_cell(fi->fdt, node->str, "revision-id", cells[0] & 0xff);
706a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "device-id",
707a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_DEVICE_ID]));
708a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "vendor-id",
709a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_VENDOR_ID]));
710a6c9808aSBALATON Zoltan 
711a6c9808aSBALATON Zoltan     g_string_free(node, TRUE);
712a6c9808aSBALATON Zoltan }
713a6c9808aSBALATON Zoltan 
714a6c9808aSBALATON Zoltan static void *build_fdt(MachineState *machine, int *fdt_size)
715a6c9808aSBALATON Zoltan {
716a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
717a6c9808aSBALATON Zoltan     PowerPCCPU *cpu = pm->cpu;
718a6c9808aSBALATON Zoltan     PCIBus *pci_bus;
719a6c9808aSBALATON Zoltan     FDTInfo fi;
720a6c9808aSBALATON Zoltan     uint32_t cells[16];
721a6c9808aSBALATON Zoltan     void *fdt = create_device_tree(fdt_size);
722a6c9808aSBALATON Zoltan 
723a6c9808aSBALATON Zoltan     fi.fdt = fdt;
724a6c9808aSBALATON Zoltan 
725a6c9808aSBALATON Zoltan     /* root node */
726a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,description",
727a6c9808aSBALATON Zoltan                             "Pegasos CHRP PowerPC System");
728a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,board", "Pegasos2");
729a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,vendor", "bplan GmbH");
730a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "revision", "2B");
731a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "model", "Pegasos2");
732a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "device_type", "chrp");
733a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 1);
734a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "name", "bplan,Pegasos2");
735a6c9808aSBALATON Zoltan 
736a6c9808aSBALATON Zoltan     /* pci@c0000000 */
737a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/pci@c0000000");
738a6c9808aSBALATON Zoltan     cells[0] = 0;
739a6c9808aSBALATON Zoltan     cells[1] = 0;
740a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "bus-range",
741a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
742a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "pci-bridge-number", 1);
743a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(PCI0_MEM_BASE);
744a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(PCI0_MEM_SIZE);
745a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "reg", cells, 2 * sizeof(cells[0]));
746a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(0x01000000);
747a6c9808aSBALATON Zoltan     cells[1] = 0;
748a6c9808aSBALATON Zoltan     cells[2] = 0;
749a6c9808aSBALATON Zoltan     cells[3] = cpu_to_be32(PCI0_IO_BASE);
750a6c9808aSBALATON Zoltan     cells[4] = 0;
751a6c9808aSBALATON Zoltan     cells[5] = cpu_to_be32(PCI0_IO_SIZE);
752a6c9808aSBALATON Zoltan     cells[6] = cpu_to_be32(0x02000000);
753a6c9808aSBALATON Zoltan     cells[7] = 0;
754a6c9808aSBALATON Zoltan     cells[8] = cpu_to_be32(PCI0_MEM_BASE);
755a6c9808aSBALATON Zoltan     cells[9] = cpu_to_be32(PCI0_MEM_BASE);
756a6c9808aSBALATON Zoltan     cells[10] = 0;
757a6c9808aSBALATON Zoltan     cells[11] = cpu_to_be32(PCI0_MEM_SIZE);
758a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "ranges",
759a6c9808aSBALATON Zoltan                      cells, 12 * sizeof(cells[0]));
760a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#size-cells", 2);
761a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#address-cells", 3);
762a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@c0000000", "device_type", "pci");
763a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@c0000000", "name", "pci");
764a6c9808aSBALATON Zoltan 
765a6c9808aSBALATON Zoltan     fi.path = "/pci@c0000000";
766a6c9808aSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 0);
767a6c9808aSBALATON Zoltan     pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
768a6c9808aSBALATON Zoltan 
769a6c9808aSBALATON Zoltan     /* pci@80000000 */
770a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/pci@80000000");
771a6c9808aSBALATON Zoltan     cells[0] = 0;
772a6c9808aSBALATON Zoltan     cells[1] = 0;
773a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "bus-range",
774a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
775a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "pci-bridge-number", 0);
776a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(PCI1_MEM_BASE);
777a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(PCI1_MEM_SIZE);
778a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "reg", cells, 2 * sizeof(cells[0]));
779a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "8259-interrupt-acknowledge",
780a6c9808aSBALATON Zoltan                           0xf1000cb4);
781a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(0x01000000);
782a6c9808aSBALATON Zoltan     cells[1] = 0;
783a6c9808aSBALATON Zoltan     cells[2] = 0;
784a6c9808aSBALATON Zoltan     cells[3] = cpu_to_be32(PCI1_IO_BASE);
785a6c9808aSBALATON Zoltan     cells[4] = 0;
786a6c9808aSBALATON Zoltan     cells[5] = cpu_to_be32(PCI1_IO_SIZE);
787a6c9808aSBALATON Zoltan     cells[6] = cpu_to_be32(0x02000000);
788a6c9808aSBALATON Zoltan     cells[7] = 0;
789a6c9808aSBALATON Zoltan     cells[8] = cpu_to_be32(PCI1_MEM_BASE);
790a6c9808aSBALATON Zoltan     cells[9] = cpu_to_be32(PCI1_MEM_BASE);
791a6c9808aSBALATON Zoltan     cells[10] = 0;
792a6c9808aSBALATON Zoltan     cells[11] = cpu_to_be32(PCI1_MEM_SIZE);
793a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "ranges",
794a6c9808aSBALATON Zoltan                      cells, 12 * sizeof(cells[0]));
795a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#size-cells", 2);
796a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#address-cells", 3);
797a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@80000000", "device_type", "pci");
798a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@80000000", "name", "pci");
799a6c9808aSBALATON Zoltan 
800a6c9808aSBALATON Zoltan     fi.path = "/pci@80000000";
801a6c9808aSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 1);
802a6c9808aSBALATON Zoltan     pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
803a6c9808aSBALATON Zoltan 
804a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/failsafe");
805a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/failsafe", "device_type", "serial");
806a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/failsafe", "name", "failsafe");
807a6c9808aSBALATON Zoltan 
8085f2eb049SBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/rtas");
8095f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "system-reboot", RTAS_SYSTEM_REBOOT);
8105f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "hibernate", RTAS_HIBERNATE);
8115f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "suspend", RTAS_SUSPEND);
8125f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "power-off", RTAS_POWER_OFF);
8135f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "set-indicator", RTAS_SET_INDICATOR);
8145f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "display-character",
8155f2eb049SBALATON Zoltan                           RTAS_DISPLAY_CHARACTER);
8165f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "write-pci-config",
8175f2eb049SBALATON Zoltan                           RTAS_WRITE_PCI_CONFIG);
8185f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "read-pci-config",
8195f2eb049SBALATON Zoltan                           RTAS_READ_PCI_CONFIG);
8205f2eb049SBALATON Zoltan     /* Pegasos2 firmware misspells check-exception and guests use that */
8215f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "check-execption",
8225f2eb049SBALATON Zoltan                           RTAS_CHECK_EXCEPTION);
8235f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "event-scan", RTAS_EVENT_SCAN);
8245f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "set-time-of-day",
8255f2eb049SBALATON Zoltan                           RTAS_SET_TIME_OF_DAY);
8265f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "get-time-of-day",
8275f2eb049SBALATON Zoltan                           RTAS_GET_TIME_OF_DAY);
8285f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-store", RTAS_NVRAM_STORE);
8295f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "nvram-fetch", RTAS_NVRAM_FETCH);
8305f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "restart-rtas", RTAS_RESTART_RTAS);
8315f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-error-log-max", 0);
8325f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-event-scan-rate", 0);
8335f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-display-device", 0);
8345f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size", 20);
8355f2eb049SBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-version", 1);
8365f2eb049SBALATON Zoltan 
837a6c9808aSBALATON Zoltan     /* cpus */
838a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/cpus");
839a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#cpus", 1);
840a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
841a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
842a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/cpus", "name", "cpus");
843a6c9808aSBALATON Zoltan 
844a6c9808aSBALATON Zoltan     /* FIXME Get CPU name from CPU object */
845a6c9808aSBALATON Zoltan     const char *cp = "/cpus/PowerPC,G4";
846a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, cp);
847a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "l2cr", 0);
848a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-size", 0x8000);
849a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-block-size",
850a6c9808aSBALATON Zoltan                           cpu->env.dcache_line_size);
851a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-line-size",
852a6c9808aSBALATON Zoltan                           cpu->env.dcache_line_size);
853a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-size", 0x8000);
854a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-block-size",
855a6c9808aSBALATON Zoltan                           cpu->env.icache_line_size);
856a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-line-size",
857a6c9808aSBALATON Zoltan                           cpu->env.icache_line_size);
858a6c9808aSBALATON Zoltan     if (cpu->env.id_tlbs) {
859a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "i-tlb-sets", cpu->env.nb_ways);
860a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "i-tlb-size", cpu->env.tlb_per_way);
861a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "d-tlb-sets", cpu->env.nb_ways);
862a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "d-tlb-size", cpu->env.tlb_per_way);
863a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "tlb-split", "");
864a6c9808aSBALATON Zoltan     }
865a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "tlb-sets", cpu->env.nb_ways);
866a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "tlb-size", cpu->env.nb_tlb);
867a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "state", "running");
868a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_ALTIVEC) {
869a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "altivec", "");
870a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "data-streams", "");
871a6c9808aSBALATON Zoltan     }
872a6c9808aSBALATON Zoltan     /*
873a6c9808aSBALATON Zoltan      * FIXME What flags do data-streams, external-control and
874a6c9808aSBALATON Zoltan      * performance-monitor depend on?
875a6c9808aSBALATON Zoltan      */
876a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "external-control", "");
877a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_FLOAT_FSQRT) {
878a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "general-purpose", "");
879a6c9808aSBALATON Zoltan     }
880a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "performance-monitor", "");
881a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_FLOAT_FRES) {
882a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "graphics", "");
883a6c9808aSBALATON Zoltan     }
884a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "reservation-granule-size", 4);
885a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "timebase-frequency",
886a6c9808aSBALATON Zoltan                           cpu->env.tb_env->tb_freq);
887a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "bus-frequency", BUS_FREQ_HZ);
888a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "clock-frequency", BUS_FREQ_HZ * 7.5);
889a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "cpu-version", cpu->env.spr[SPR_PVR]);
890a6c9808aSBALATON Zoltan     cells[0] = 0;
891a6c9808aSBALATON Zoltan     cells[1] = 0;
892a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, cp, "reg", cells, 2 * sizeof(cells[0]));
893a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "device_type", "cpu");
894a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "name", strrchr(cp, '/') + 1);
895a6c9808aSBALATON Zoltan 
896a6c9808aSBALATON Zoltan     /* memory */
897a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/memory@0");
898a6c9808aSBALATON Zoltan     cells[0] = 0;
899a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(machine->ram_size);
900a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/memory@0", "reg", cells, 2 * sizeof(cells[0]));
901a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/memory@0", "device_type", "memory");
902a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/memory@0", "name", "memory");
903a6c9808aSBALATON Zoltan 
904a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/chosen");
905a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
906a6c9808aSBALATON Zoltan                             machine->kernel_cmdline ?: "");
907a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/chosen", "name", "chosen");
908a6c9808aSBALATON Zoltan 
909a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/openprom");
910a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/openprom", "model", "Pegasos2,1.1");
911a6c9808aSBALATON Zoltan 
912a6c9808aSBALATON Zoltan     return fdt;
913a6c9808aSBALATON Zoltan }
914