xref: /qemu/hw/ppc/pegasos2.c (revision a6c9808a689764cba980280fc4581e2deb5023a4)
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"
37*a6c9808aSBALATON Zoltan #include "hw/ppc/vof.h"
38ba7e5ac1SBALATON Zoltan 
39*a6c9808aSBALATON Zoltan #include <libfdt.h>
40*a6c9808aSBALATON Zoltan 
41*a6c9808aSBALATON Zoltan #define PROM_FILENAME "vof.bin"
42ba7e5ac1SBALATON Zoltan #define PROM_ADDR     0xfff00000
43ba7e5ac1SBALATON Zoltan #define PROM_SIZE     0x80000
44ba7e5ac1SBALATON Zoltan 
45*a6c9808aSBALATON Zoltan #define KVMPPC_HCALL_BASE    0xf000
46*a6c9808aSBALATON Zoltan #define KVMPPC_H_VOF_CLIENT  (KVMPPC_HCALL_BASE + 0x5)
47*a6c9808aSBALATON Zoltan 
48*a6c9808aSBALATON Zoltan #define H_SUCCESS     0
49*a6c9808aSBALATON Zoltan #define H_PRIVILEGE  -3  /* Caller not privileged */
50*a6c9808aSBALATON Zoltan #define H_PARAMETER  -4  /* Parameter invalid, out-of-range or conflicting */
51*a6c9808aSBALATON Zoltan 
52ba7e5ac1SBALATON Zoltan #define BUS_FREQ_HZ 133333333
53ba7e5ac1SBALATON Zoltan 
54*a6c9808aSBALATON Zoltan #define PCI0_MEM_BASE 0xc0000000
55*a6c9808aSBALATON Zoltan #define PCI0_MEM_SIZE 0x20000000
56*a6c9808aSBALATON Zoltan #define PCI0_IO_BASE  0xf8000000
57*a6c9808aSBALATON Zoltan #define PCI0_IO_SIZE  0x10000
58*a6c9808aSBALATON Zoltan 
59*a6c9808aSBALATON Zoltan #define PCI1_MEM_BASE 0x80000000
60*a6c9808aSBALATON Zoltan #define PCI1_MEM_SIZE 0x40000000
61*a6c9808aSBALATON Zoltan #define PCI1_IO_BASE  0xfe000000
62*a6c9808aSBALATON Zoltan #define PCI1_IO_SIZE  0x10000
63*a6c9808aSBALATON Zoltan 
64a8eda5edSBALATON Zoltan #define TYPE_PEGASOS2_MACHINE  MACHINE_TYPE_NAME("pegasos2")
65a8eda5edSBALATON Zoltan OBJECT_DECLARE_TYPE(Pegasos2MachineState, MachineClass, PEGASOS2_MACHINE)
66a8eda5edSBALATON Zoltan 
67a8eda5edSBALATON Zoltan struct Pegasos2MachineState {
68a8eda5edSBALATON Zoltan     MachineState parent_obj;
69a8eda5edSBALATON Zoltan     PowerPCCPU *cpu;
70a8eda5edSBALATON Zoltan     DeviceState *mv;
71*a6c9808aSBALATON Zoltan     Vof *vof;
72*a6c9808aSBALATON Zoltan     void *fdt_blob;
73*a6c9808aSBALATON Zoltan     uint64_t kernel_addr;
74*a6c9808aSBALATON Zoltan     uint64_t kernel_entry;
75*a6c9808aSBALATON Zoltan     uint64_t kernel_size;
76a8eda5edSBALATON Zoltan };
77a8eda5edSBALATON Zoltan 
78*a6c9808aSBALATON Zoltan static void *build_fdt(MachineState *machine, int *fdt_size);
79*a6c9808aSBALATON Zoltan 
80ba7e5ac1SBALATON Zoltan static void pegasos2_cpu_reset(void *opaque)
81ba7e5ac1SBALATON Zoltan {
82ba7e5ac1SBALATON Zoltan     PowerPCCPU *cpu = opaque;
83*a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(current_machine);
84ba7e5ac1SBALATON Zoltan 
85ba7e5ac1SBALATON Zoltan     cpu_reset(CPU(cpu));
86ba7e5ac1SBALATON Zoltan     cpu->env.spr[SPR_HID1] = 7ULL << 28;
87*a6c9808aSBALATON Zoltan     if (pm->vof) {
88*a6c9808aSBALATON Zoltan         cpu->env.gpr[1] = 2 * VOF_STACK_SIZE - 0x20;
89*a6c9808aSBALATON Zoltan         cpu->env.nip = 0x100;
90*a6c9808aSBALATON Zoltan     }
91ba7e5ac1SBALATON Zoltan }
92ba7e5ac1SBALATON Zoltan 
93ba7e5ac1SBALATON Zoltan static void pegasos2_init(MachineState *machine)
94ba7e5ac1SBALATON Zoltan {
95a8eda5edSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
96a8eda5edSBALATON Zoltan     CPUPPCState *env;
97ba7e5ac1SBALATON Zoltan     MemoryRegion *rom = g_new(MemoryRegion, 1);
98ba7e5ac1SBALATON Zoltan     PCIBus *pci_bus;
99ba7e5ac1SBALATON Zoltan     PCIDevice *dev;
100ba7e5ac1SBALATON Zoltan     I2CBus *i2c_bus;
101ba7e5ac1SBALATON Zoltan     const char *fwname = machine->firmware ?: PROM_FILENAME;
102ba7e5ac1SBALATON Zoltan     char *filename;
103ba7e5ac1SBALATON Zoltan     int sz;
104ba7e5ac1SBALATON Zoltan     uint8_t *spd_data;
105ba7e5ac1SBALATON Zoltan 
106ba7e5ac1SBALATON Zoltan     /* init CPU */
107a8eda5edSBALATON Zoltan     pm->cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
108a8eda5edSBALATON Zoltan     env = &pm->cpu->env;
109a8eda5edSBALATON Zoltan     if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
110ba7e5ac1SBALATON Zoltan         error_report("Incompatible CPU, only 6xx bus supported");
111ba7e5ac1SBALATON Zoltan         exit(1);
112ba7e5ac1SBALATON Zoltan     }
113ba7e5ac1SBALATON Zoltan 
114ba7e5ac1SBALATON Zoltan     /* Set time-base frequency */
115a8eda5edSBALATON Zoltan     cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4);
116a8eda5edSBALATON Zoltan     qemu_register_reset(pegasos2_cpu_reset, pm->cpu);
117ba7e5ac1SBALATON Zoltan 
118ba7e5ac1SBALATON Zoltan     /* RAM */
119ba7e5ac1SBALATON Zoltan     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
120ba7e5ac1SBALATON Zoltan 
121ba7e5ac1SBALATON Zoltan     /* allocate and load firmware */
122ba7e5ac1SBALATON Zoltan     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname);
123ba7e5ac1SBALATON Zoltan     if (!filename) {
124ba7e5ac1SBALATON Zoltan         error_report("Could not find firmware '%s'", fwname);
125ba7e5ac1SBALATON Zoltan         exit(1);
126ba7e5ac1SBALATON Zoltan     }
127*a6c9808aSBALATON Zoltan     if (!machine->firmware && !pm->vof) {
128*a6c9808aSBALATON Zoltan         pm->vof = g_malloc0(sizeof(*pm->vof));
129*a6c9808aSBALATON Zoltan     }
130ba7e5ac1SBALATON Zoltan     memory_region_init_rom(rom, NULL, "pegasos2.rom", PROM_SIZE, &error_fatal);
131ba7e5ac1SBALATON Zoltan     memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
132ba7e5ac1SBALATON Zoltan     sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1,
133ba7e5ac1SBALATON Zoltan                   PPC_ELF_MACHINE, 0, 0);
134ba7e5ac1SBALATON Zoltan     if (sz <= 0) {
135*a6c9808aSBALATON Zoltan         sz = load_image_targphys(filename, pm->vof ? 0 : PROM_ADDR, PROM_SIZE);
136ba7e5ac1SBALATON Zoltan     }
137ba7e5ac1SBALATON Zoltan     if (sz <= 0 || sz > PROM_SIZE) {
138ba7e5ac1SBALATON Zoltan         error_report("Could not load firmware '%s'", filename);
139ba7e5ac1SBALATON Zoltan         exit(1);
140ba7e5ac1SBALATON Zoltan     }
141ba7e5ac1SBALATON Zoltan     g_free(filename);
142*a6c9808aSBALATON Zoltan     if (pm->vof) {
143*a6c9808aSBALATON Zoltan         pm->vof->fw_size = sz;
144*a6c9808aSBALATON Zoltan     }
145ba7e5ac1SBALATON Zoltan 
146ba7e5ac1SBALATON Zoltan     /* Marvell Discovery II system controller */
147a8eda5edSBALATON Zoltan     pm->mv = DEVICE(sysbus_create_simple(TYPE_MV64361, -1,
148a8eda5edSBALATON Zoltan                              ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT]));
149a8eda5edSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 1);
150ba7e5ac1SBALATON Zoltan 
151ba7e5ac1SBALATON Zoltan     /* VIA VT8231 South Bridge (multifunction PCI device) */
152ba7e5ac1SBALATON Zoltan     /* VT8231 function 0: PCI-to-ISA Bridge */
153ba7e5ac1SBALATON Zoltan     dev = pci_create_simple_multifunction(pci_bus, PCI_DEVFN(12, 0), true,
154ba7e5ac1SBALATON Zoltan                                           TYPE_VT8231_ISA);
155ba7e5ac1SBALATON Zoltan     qdev_connect_gpio_out(DEVICE(dev), 0,
156a8eda5edSBALATON Zoltan                           qdev_get_gpio_in_named(pm->mv, "gpp", 31));
157ba7e5ac1SBALATON Zoltan 
158ba7e5ac1SBALATON Zoltan     /* VT8231 function 1: IDE Controller */
159ba7e5ac1SBALATON Zoltan     dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 1), "via-ide");
160ba7e5ac1SBALATON Zoltan     pci_ide_create_devs(dev);
161ba7e5ac1SBALATON Zoltan 
162ba7e5ac1SBALATON Zoltan     /* VT8231 function 2-3: USB Ports */
163ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 2), "vt82c686b-usb-uhci");
164ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 3), "vt82c686b-usb-uhci");
165ba7e5ac1SBALATON Zoltan 
166ba7e5ac1SBALATON Zoltan     /* VT8231 function 4: Power Management Controller */
167ba7e5ac1SBALATON Zoltan     dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 4), TYPE_VT8231_PM);
168ba7e5ac1SBALATON Zoltan     i2c_bus = I2C_BUS(qdev_get_child_bus(DEVICE(dev), "i2c"));
169ba7e5ac1SBALATON Zoltan     spd_data = spd_data_generate(DDR, machine->ram_size);
170ba7e5ac1SBALATON Zoltan     smbus_eeprom_init_one(i2c_bus, 0x57, spd_data);
171ba7e5ac1SBALATON Zoltan 
172ba7e5ac1SBALATON Zoltan     /* VT8231 function 5-6: AC97 Audio & Modem */
173ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 5), TYPE_VIA_AC97);
174ba7e5ac1SBALATON Zoltan     pci_create_simple(pci_bus, PCI_DEVFN(12, 6), TYPE_VIA_MC97);
175ba7e5ac1SBALATON Zoltan 
176ba7e5ac1SBALATON Zoltan     /* other PC hardware */
177ba7e5ac1SBALATON Zoltan     pci_vga_init(pci_bus);
178*a6c9808aSBALATON Zoltan 
179*a6c9808aSBALATON Zoltan     if (machine->kernel_filename) {
180*a6c9808aSBALATON Zoltan         sz = load_elf(machine->kernel_filename, NULL, NULL, NULL,
181*a6c9808aSBALATON Zoltan                       &pm->kernel_entry, &pm->kernel_addr, NULL, NULL, 1,
182*a6c9808aSBALATON Zoltan                       PPC_ELF_MACHINE, 0, 0);
183*a6c9808aSBALATON Zoltan         if (sz <= 0) {
184*a6c9808aSBALATON Zoltan             error_report("Could not load kernel '%s'",
185*a6c9808aSBALATON Zoltan                          machine->kernel_filename);
186*a6c9808aSBALATON Zoltan             exit(1);
187*a6c9808aSBALATON Zoltan         }
188*a6c9808aSBALATON Zoltan         pm->kernel_size = sz;
189*a6c9808aSBALATON Zoltan         if (!pm->vof) {
190*a6c9808aSBALATON Zoltan             warn_report("Option -kernel may be ineffective with -bios.");
191*a6c9808aSBALATON Zoltan         }
192*a6c9808aSBALATON Zoltan     }
193*a6c9808aSBALATON Zoltan     if (machine->kernel_cmdline && !pm->vof) {
194*a6c9808aSBALATON Zoltan         warn_report("Option -append may be ineffective with -bios.");
195*a6c9808aSBALATON Zoltan     }
196*a6c9808aSBALATON Zoltan }
197*a6c9808aSBALATON Zoltan 
198*a6c9808aSBALATON Zoltan static void pegasos2_pci_config_write(AddressSpace *as, int bus, uint32_t addr,
199*a6c9808aSBALATON Zoltan                                       uint32_t len, uint32_t val)
200*a6c9808aSBALATON Zoltan {
201*a6c9808aSBALATON Zoltan     hwaddr pcicfg = (bus ? 0xf1000c78 : 0xf1000cf8);
202*a6c9808aSBALATON Zoltan 
203*a6c9808aSBALATON Zoltan     stl_le_phys(as, pcicfg, addr | BIT(31));
204*a6c9808aSBALATON Zoltan     switch (len) {
205*a6c9808aSBALATON Zoltan     case 4:
206*a6c9808aSBALATON Zoltan         stl_le_phys(as, pcicfg + 4, val);
207*a6c9808aSBALATON Zoltan         break;
208*a6c9808aSBALATON Zoltan     case 2:
209*a6c9808aSBALATON Zoltan         stw_le_phys(as, pcicfg + 4, val);
210*a6c9808aSBALATON Zoltan         break;
211*a6c9808aSBALATON Zoltan     case 1:
212*a6c9808aSBALATON Zoltan         stb_phys(as, pcicfg + 4, val);
213*a6c9808aSBALATON Zoltan         break;
214*a6c9808aSBALATON Zoltan     default:
215*a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid length\n", __func__);
216*a6c9808aSBALATON Zoltan         break;
217*a6c9808aSBALATON Zoltan     }
218*a6c9808aSBALATON Zoltan }
219*a6c9808aSBALATON Zoltan 
220*a6c9808aSBALATON Zoltan static void pegasos2_machine_reset(MachineState *machine)
221*a6c9808aSBALATON Zoltan {
222*a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
223*a6c9808aSBALATON Zoltan     AddressSpace *as = CPU(pm->cpu)->as;
224*a6c9808aSBALATON Zoltan     void *fdt;
225*a6c9808aSBALATON Zoltan     uint64_t d[2];
226*a6c9808aSBALATON Zoltan     int sz;
227*a6c9808aSBALATON Zoltan 
228*a6c9808aSBALATON Zoltan     qemu_devices_reset();
229*a6c9808aSBALATON Zoltan     if (!pm->vof) {
230*a6c9808aSBALATON Zoltan         return; /* Firmware should set up machine so nothing to do */
231*a6c9808aSBALATON Zoltan     }
232*a6c9808aSBALATON Zoltan 
233*a6c9808aSBALATON Zoltan     /* Otherwise, set up devices that board firmware would normally do */
234*a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf1000000, 0x28020ff);
235*a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf1000278, 0xa31fc);
236*a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf100f300, 0x11ff0400);
237*a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf100f10c, 0x80000000);
238*a6c9808aSBALATON Zoltan     stl_le_phys(as, 0xf100001c, 0x8000000);
239*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 0, PCI_COMMAND, 2, PCI_COMMAND_IO |
240*a6c9808aSBALATON Zoltan                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
241*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, PCI_COMMAND, 2, PCI_COMMAND_IO |
242*a6c9808aSBALATON Zoltan                               PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
243*a6c9808aSBALATON Zoltan 
244*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 0) << 8) |
245*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x9);
246*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 0) << 8) |
247*a6c9808aSBALATON Zoltan                               0x50, 1, 0x2);
248*a6c9808aSBALATON Zoltan 
249*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
250*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x109);
251*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
252*a6c9808aSBALATON Zoltan                               PCI_CLASS_PROG, 1, 0xf);
253*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
254*a6c9808aSBALATON Zoltan                               0x40, 1, 0xb);
255*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
256*a6c9808aSBALATON Zoltan                               0x50, 4, 0x17171717);
257*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 1) << 8) |
258*a6c9808aSBALATON Zoltan                               PCI_COMMAND, 2, 0x87);
259*a6c9808aSBALATON Zoltan 
260*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 2) << 8) |
261*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x409);
262*a6c9808aSBALATON Zoltan 
263*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 3) << 8) |
264*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x409);
265*a6c9808aSBALATON Zoltan 
266*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
267*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x9);
268*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
269*a6c9808aSBALATON Zoltan                               0x48, 4, 0xf00);
270*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
271*a6c9808aSBALATON Zoltan                               0x40, 4, 0x558020);
272*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 4) << 8) |
273*a6c9808aSBALATON Zoltan                               0x90, 4, 0xd00);
274*a6c9808aSBALATON Zoltan 
275*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 5) << 8) |
276*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x309);
277*a6c9808aSBALATON Zoltan 
278*a6c9808aSBALATON Zoltan     pegasos2_pci_config_write(as, 1, (PCI_DEVFN(12, 6) << 8) |
279*a6c9808aSBALATON Zoltan                               PCI_INTERRUPT_LINE, 2, 0x309);
280*a6c9808aSBALATON Zoltan 
281*a6c9808aSBALATON Zoltan     /* Device tree and VOF set up */
282*a6c9808aSBALATON Zoltan     vof_init(pm->vof, machine->ram_size, &error_fatal);
283*a6c9808aSBALATON Zoltan     if (vof_claim(pm->vof, 0, VOF_STACK_SIZE, VOF_STACK_SIZE) == -1) {
284*a6c9808aSBALATON Zoltan         error_report("Memory allocation for stack failed");
285*a6c9808aSBALATON Zoltan         exit(1);
286*a6c9808aSBALATON Zoltan     }
287*a6c9808aSBALATON Zoltan     if (pm->kernel_size &&
288*a6c9808aSBALATON Zoltan         vof_claim(pm->vof, pm->kernel_addr, pm->kernel_size, 0) == -1) {
289*a6c9808aSBALATON Zoltan         error_report("Memory for kernel is in use");
290*a6c9808aSBALATON Zoltan         exit(1);
291*a6c9808aSBALATON Zoltan     }
292*a6c9808aSBALATON Zoltan     fdt = build_fdt(machine, &sz);
293*a6c9808aSBALATON Zoltan     /* FIXME: VOF assumes entry is same as load address */
294*a6c9808aSBALATON Zoltan     d[0] = cpu_to_be64(pm->kernel_entry);
295*a6c9808aSBALATON Zoltan     d[1] = cpu_to_be64(pm->kernel_size - (pm->kernel_entry - pm->kernel_addr));
296*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/chosen", "qemu,boot-kernel", d, sizeof(d));
297*a6c9808aSBALATON Zoltan 
298*a6c9808aSBALATON Zoltan     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
299*a6c9808aSBALATON Zoltan     g_free(pm->fdt_blob);
300*a6c9808aSBALATON Zoltan     pm->fdt_blob = fdt;
301*a6c9808aSBALATON Zoltan 
302*a6c9808aSBALATON Zoltan     vof_build_dt(fdt, pm->vof);
303*a6c9808aSBALATON Zoltan     vof_client_open_store(fdt, pm->vof, "/chosen", "stdout", "/failsafe");
304*a6c9808aSBALATON Zoltan     pm->cpu->vhyp = PPC_VIRTUAL_HYPERVISOR(machine);
305*a6c9808aSBALATON Zoltan }
306*a6c9808aSBALATON Zoltan 
307*a6c9808aSBALATON Zoltan static void pegasos2_hypercall(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
308*a6c9808aSBALATON Zoltan {
309*a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(vhyp);
310*a6c9808aSBALATON Zoltan     CPUPPCState *env = &cpu->env;
311*a6c9808aSBALATON Zoltan 
312*a6c9808aSBALATON Zoltan     /* The TCG path should also be holding the BQL at this point */
313*a6c9808aSBALATON Zoltan     g_assert(qemu_mutex_iothread_locked());
314*a6c9808aSBALATON Zoltan 
315*a6c9808aSBALATON Zoltan     if (msr_pr) {
316*a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Hypercall made with MSR[PR]=1\n");
317*a6c9808aSBALATON Zoltan         env->gpr[3] = H_PRIVILEGE;
318*a6c9808aSBALATON Zoltan     } else if (env->gpr[3] == KVMPPC_H_VOF_CLIENT) {
319*a6c9808aSBALATON Zoltan         int ret = vof_client_call(MACHINE(pm), pm->vof, pm->fdt_blob,
320*a6c9808aSBALATON Zoltan                                   env->gpr[4]);
321*a6c9808aSBALATON Zoltan         env->gpr[3] = (ret ? H_PARAMETER : H_SUCCESS);
322*a6c9808aSBALATON Zoltan     } else {
323*a6c9808aSBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Unsupported hypercall " TARGET_FMT_lx
324*a6c9808aSBALATON Zoltan                       "\n", env->gpr[3]);
325*a6c9808aSBALATON Zoltan         env->gpr[3] = -1;
326*a6c9808aSBALATON Zoltan     }
327*a6c9808aSBALATON Zoltan }
328*a6c9808aSBALATON Zoltan 
329*a6c9808aSBALATON Zoltan static void vhyp_nop(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
330*a6c9808aSBALATON Zoltan {
331*a6c9808aSBALATON Zoltan }
332*a6c9808aSBALATON Zoltan 
333*a6c9808aSBALATON Zoltan static target_ulong vhyp_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
334*a6c9808aSBALATON Zoltan {
335*a6c9808aSBALATON Zoltan     return POWERPC_CPU(current_cpu)->env.spr[SPR_SDR1];
336ba7e5ac1SBALATON Zoltan }
337ba7e5ac1SBALATON Zoltan 
338a8eda5edSBALATON Zoltan static void pegasos2_machine_class_init(ObjectClass *oc, void *data)
339ba7e5ac1SBALATON Zoltan {
340a8eda5edSBALATON Zoltan     MachineClass *mc = MACHINE_CLASS(oc);
341*a6c9808aSBALATON Zoltan     PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
342a8eda5edSBALATON Zoltan 
343ba7e5ac1SBALATON Zoltan     mc->desc = "Genesi/bPlan Pegasos II";
344ba7e5ac1SBALATON Zoltan     mc->init = pegasos2_init;
345*a6c9808aSBALATON Zoltan     mc->reset = pegasos2_machine_reset;
346ba7e5ac1SBALATON Zoltan     mc->block_default_type = IF_IDE;
347ba7e5ac1SBALATON Zoltan     mc->default_boot_order = "cd";
348ba7e5ac1SBALATON Zoltan     mc->default_display = "std";
349ba7e5ac1SBALATON Zoltan     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7400_v2.9");
350ba7e5ac1SBALATON Zoltan     mc->default_ram_id = "pegasos2.ram";
351ba7e5ac1SBALATON Zoltan     mc->default_ram_size = 512 * MiB;
352*a6c9808aSBALATON Zoltan 
353*a6c9808aSBALATON Zoltan     vhc->hypercall = pegasos2_hypercall;
354*a6c9808aSBALATON Zoltan     vhc->cpu_exec_enter = vhyp_nop;
355*a6c9808aSBALATON Zoltan     vhc->cpu_exec_exit = vhyp_nop;
356*a6c9808aSBALATON Zoltan     vhc->encode_hpt_for_kvm_pr = vhyp_encode_hpt_for_kvm_pr;
357ba7e5ac1SBALATON Zoltan }
358ba7e5ac1SBALATON Zoltan 
359a8eda5edSBALATON Zoltan static const TypeInfo pegasos2_machine_info = {
360a8eda5edSBALATON Zoltan     .name          = TYPE_PEGASOS2_MACHINE,
361a8eda5edSBALATON Zoltan     .parent        = TYPE_MACHINE,
362a8eda5edSBALATON Zoltan     .class_init    = pegasos2_machine_class_init,
363a8eda5edSBALATON Zoltan     .instance_size = sizeof(Pegasos2MachineState),
364*a6c9808aSBALATON Zoltan     .interfaces = (InterfaceInfo[]) {
365*a6c9808aSBALATON Zoltan         { TYPE_PPC_VIRTUAL_HYPERVISOR },
366*a6c9808aSBALATON Zoltan         { }
367*a6c9808aSBALATON Zoltan     },
368a8eda5edSBALATON Zoltan };
369a8eda5edSBALATON Zoltan 
370a8eda5edSBALATON Zoltan static void pegasos2_machine_register_types(void)
371a8eda5edSBALATON Zoltan {
372a8eda5edSBALATON Zoltan     type_register_static(&pegasos2_machine_info);
373a8eda5edSBALATON Zoltan }
374a8eda5edSBALATON Zoltan 
375a8eda5edSBALATON Zoltan type_init(pegasos2_machine_register_types)
376*a6c9808aSBALATON Zoltan 
377*a6c9808aSBALATON Zoltan /* FDT creation for passing to firmware */
378*a6c9808aSBALATON Zoltan 
379*a6c9808aSBALATON Zoltan typedef struct {
380*a6c9808aSBALATON Zoltan     void *fdt;
381*a6c9808aSBALATON Zoltan     const char *path;
382*a6c9808aSBALATON Zoltan } FDTInfo;
383*a6c9808aSBALATON Zoltan 
384*a6c9808aSBALATON Zoltan /* We do everything in reverse order so it comes out right in the tree */
385*a6c9808aSBALATON Zoltan 
386*a6c9808aSBALATON Zoltan static void dt_ide(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
387*a6c9808aSBALATON Zoltan {
388*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "spi");
389*a6c9808aSBALATON Zoltan }
390*a6c9808aSBALATON Zoltan 
391*a6c9808aSBALATON Zoltan static void dt_usb(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
392*a6c9808aSBALATON Zoltan {
393*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 0);
394*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 1);
395*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "usb");
396*a6c9808aSBALATON Zoltan }
397*a6c9808aSBALATON Zoltan 
398*a6c9808aSBALATON Zoltan static void dt_isa(PCIBus *bus, PCIDevice *d, FDTInfo *fi)
399*a6c9808aSBALATON Zoltan {
400*a6c9808aSBALATON Zoltan     GString *name = g_string_sized_new(64);
401*a6c9808aSBALATON Zoltan     uint32_t cells[3];
402*a6c9808aSBALATON Zoltan 
403*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#size-cells", 1);
404*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, fi->path, "#address-cells", 2);
405*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "device_type", "isa");
406*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, fi->path, "name", "isa");
407*a6c9808aSBALATON Zoltan 
408*a6c9808aSBALATON Zoltan     /* addional devices */
409*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/lpt@i3bc", fi->path);
410*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
411*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
412*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(7);
413*a6c9808aSBALATON Zoltan     cells[1] = 0;
414*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
415*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
416*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
417*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x3bc);
418*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
419*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
420*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "lpt");
421*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "lpt");
422*a6c9808aSBALATON Zoltan 
423*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/fdc@i3f0", fi->path);
424*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
425*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
426*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(6);
427*a6c9808aSBALATON Zoltan     cells[1] = 0;
428*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
429*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
430*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
431*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x3f0);
432*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
433*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
434*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "fdc");
435*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "fdc");
436*a6c9808aSBALATON Zoltan 
437*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/timer@i40", fi->path);
438*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
439*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
440*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
441*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x40);
442*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
443*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
444*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "timer");
445*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "timer");
446*a6c9808aSBALATON Zoltan 
447*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/rtc@i70", fi->path);
448*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
449*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "compatible", "ds1385-rtc");
450*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
451*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(8);
452*a6c9808aSBALATON Zoltan     cells[1] = 0;
453*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
454*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
455*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
456*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x70);
457*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(2);
458*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
459*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "rtc");
460*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "rtc");
461*a6c9808aSBALATON Zoltan 
462*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/keyboard@i60", fi->path);
463*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
464*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
465*a6c9808aSBALATON Zoltan     cells[1] = 0;
466*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
467*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
468*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
469*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x60);
470*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(5);
471*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
472*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "keyboard");
473*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "keyboard");
474*a6c9808aSBALATON Zoltan 
475*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/8042@i60", fi->path);
476*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
477*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#interrupt-cells", 2);
478*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#size-cells", 0);
479*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "#address-cells", 1);
480*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "interrupt-controller", "");
481*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
482*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
483*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x60);
484*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(5);
485*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
486*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "");
487*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "8042");
488*a6c9808aSBALATON Zoltan 
489*a6c9808aSBALATON Zoltan     g_string_printf(name, "%s/serial@i2f8", fi->path);
490*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, name->str);
491*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, name->str, "clock-frequency", 0);
492*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(3);
493*a6c9808aSBALATON Zoltan     cells[1] = 0;
494*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "interrupts",
495*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
496*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(1);
497*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(0x2f8);
498*a6c9808aSBALATON Zoltan     cells[2] = cpu_to_be32(8);
499*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, name->str, "reg", cells, 3 * sizeof(cells[0]));
500*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "device_type", "serial");
501*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, name->str, "name", "serial");
502*a6c9808aSBALATON Zoltan 
503*a6c9808aSBALATON Zoltan     g_string_free(name, TRUE);
504*a6c9808aSBALATON Zoltan }
505*a6c9808aSBALATON Zoltan 
506*a6c9808aSBALATON Zoltan static struct {
507*a6c9808aSBALATON Zoltan     const char *id;
508*a6c9808aSBALATON Zoltan     const char *name;
509*a6c9808aSBALATON Zoltan     void (*dtf)(PCIBus *bus, PCIDevice *d, FDTInfo *fi);
510*a6c9808aSBALATON Zoltan } device_map[] = {
511*a6c9808aSBALATON Zoltan     { "pci11ab,6460", "host", NULL },
512*a6c9808aSBALATON Zoltan     { "pci1106,8231", "isa", dt_isa },
513*a6c9808aSBALATON Zoltan     { "pci1106,571", "ide", dt_ide },
514*a6c9808aSBALATON Zoltan     { "pci1106,3044", "firewire", NULL },
515*a6c9808aSBALATON Zoltan     { "pci1106,3038", "usb", dt_usb },
516*a6c9808aSBALATON Zoltan     { "pci1106,8235", "other", NULL },
517*a6c9808aSBALATON Zoltan     { "pci1106,3058", "sound", NULL },
518*a6c9808aSBALATON Zoltan     { NULL, NULL }
519*a6c9808aSBALATON Zoltan };
520*a6c9808aSBALATON Zoltan 
521*a6c9808aSBALATON Zoltan static void add_pci_device(PCIBus *bus, PCIDevice *d, void *opaque)
522*a6c9808aSBALATON Zoltan {
523*a6c9808aSBALATON Zoltan     FDTInfo *fi = opaque;
524*a6c9808aSBALATON Zoltan     GString *node = g_string_new(NULL);
525*a6c9808aSBALATON Zoltan     uint32_t cells[(PCI_NUM_REGIONS + 1) * 5];
526*a6c9808aSBALATON Zoltan     int i, j;
527*a6c9808aSBALATON Zoltan     const char *name = NULL;
528*a6c9808aSBALATON Zoltan     g_autofree const gchar *pn = g_strdup_printf("pci%x,%x",
529*a6c9808aSBALATON Zoltan                                      pci_get_word(&d->config[PCI_VENDOR_ID]),
530*a6c9808aSBALATON Zoltan                                      pci_get_word(&d->config[PCI_DEVICE_ID]));
531*a6c9808aSBALATON Zoltan 
532*a6c9808aSBALATON Zoltan     for (i = 0; device_map[i].id; i++) {
533*a6c9808aSBALATON Zoltan         if (!strcmp(pn, device_map[i].id)) {
534*a6c9808aSBALATON Zoltan             name = device_map[i].name;
535*a6c9808aSBALATON Zoltan             break;
536*a6c9808aSBALATON Zoltan         }
537*a6c9808aSBALATON Zoltan     }
538*a6c9808aSBALATON Zoltan     g_string_printf(node, "%s/%s@%x", fi->path, (name ?: pn),
539*a6c9808aSBALATON Zoltan                     PCI_SLOT(d->devfn));
540*a6c9808aSBALATON Zoltan     if (PCI_FUNC(d->devfn)) {
541*a6c9808aSBALATON Zoltan         g_string_append_printf(node, ",%x", PCI_FUNC(d->devfn));
542*a6c9808aSBALATON Zoltan     }
543*a6c9808aSBALATON Zoltan 
544*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fi->fdt, node->str);
545*a6c9808aSBALATON Zoltan     if (device_map[i].dtf) {
546*a6c9808aSBALATON Zoltan         FDTInfo cfi = { fi->fdt, node->str };
547*a6c9808aSBALATON Zoltan         device_map[i].dtf(bus, d, &cfi);
548*a6c9808aSBALATON Zoltan     }
549*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(d->devfn << 8);
550*a6c9808aSBALATON Zoltan     cells[1] = 0;
551*a6c9808aSBALATON Zoltan     cells[2] = 0;
552*a6c9808aSBALATON Zoltan     cells[3] = 0;
553*a6c9808aSBALATON Zoltan     cells[4] = 0;
554*a6c9808aSBALATON Zoltan     j = 5;
555*a6c9808aSBALATON Zoltan     for (i = 0; i < PCI_NUM_REGIONS; i++) {
556*a6c9808aSBALATON Zoltan         if (!d->io_regions[i].size) {
557*a6c9808aSBALATON Zoltan             continue;
558*a6c9808aSBALATON Zoltan         }
559*a6c9808aSBALATON Zoltan         cells[j] = cpu_to_be32(d->devfn << 8 | (PCI_BASE_ADDRESS_0 + i * 4));
560*a6c9808aSBALATON Zoltan         if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
561*a6c9808aSBALATON Zoltan             cells[j] |= cpu_to_be32(1 << 24);
562*a6c9808aSBALATON Zoltan         } else {
563*a6c9808aSBALATON Zoltan             cells[j] |= cpu_to_be32(2 << 24);
564*a6c9808aSBALATON Zoltan             if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
565*a6c9808aSBALATON Zoltan                 cells[j] |= cpu_to_be32(4 << 28);
566*a6c9808aSBALATON Zoltan             }
567*a6c9808aSBALATON Zoltan         }
568*a6c9808aSBALATON Zoltan         cells[j + 1] = 0;
569*a6c9808aSBALATON Zoltan         cells[j + 2] = 0;
570*a6c9808aSBALATON Zoltan         cells[j + 3] = cpu_to_be32(d->io_regions[i].size >> 32);
571*a6c9808aSBALATON Zoltan         cells[j + 4] = cpu_to_be32(d->io_regions[i].size);
572*a6c9808aSBALATON Zoltan         j += 5;
573*a6c9808aSBALATON Zoltan     }
574*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fi->fdt, node->str, "reg", cells, j * sizeof(cells[0]));
575*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fi->fdt, node->str, "name", name ?: pn);
576*a6c9808aSBALATON Zoltan     if (pci_get_byte(&d->config[PCI_INTERRUPT_PIN])) {
577*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fi->fdt, node->str, "interrupts",
578*a6c9808aSBALATON Zoltan                               pci_get_byte(&d->config[PCI_INTERRUPT_PIN]));
579*a6c9808aSBALATON Zoltan     }
580*a6c9808aSBALATON Zoltan     /* Pegasos2 firmware has subsystem-id amd subsystem-vendor-id swapped */
581*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-vendor-id",
582*a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_SUBSYSTEM_ID]));
583*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "subsystem-id",
584*a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_SUBSYSTEM_VENDOR_ID]));
585*a6c9808aSBALATON Zoltan     cells[0] = pci_get_long(&d->config[PCI_CLASS_REVISION]);
586*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "class-code", cells[0] >> 8);
587*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "revision-id", cells[0] && 0xff);
588*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "device-id",
589*a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_DEVICE_ID]));
590*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fi->fdt, node->str, "vendor-id",
591*a6c9808aSBALATON Zoltan                           pci_get_word(&d->config[PCI_VENDOR_ID]));
592*a6c9808aSBALATON Zoltan 
593*a6c9808aSBALATON Zoltan     g_string_free(node, TRUE);
594*a6c9808aSBALATON Zoltan }
595*a6c9808aSBALATON Zoltan 
596*a6c9808aSBALATON Zoltan static void *build_fdt(MachineState *machine, int *fdt_size)
597*a6c9808aSBALATON Zoltan {
598*a6c9808aSBALATON Zoltan     Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine);
599*a6c9808aSBALATON Zoltan     PowerPCCPU *cpu = pm->cpu;
600*a6c9808aSBALATON Zoltan     PCIBus *pci_bus;
601*a6c9808aSBALATON Zoltan     FDTInfo fi;
602*a6c9808aSBALATON Zoltan     uint32_t cells[16];
603*a6c9808aSBALATON Zoltan     void *fdt = create_device_tree(fdt_size);
604*a6c9808aSBALATON Zoltan 
605*a6c9808aSBALATON Zoltan     fi.fdt = fdt;
606*a6c9808aSBALATON Zoltan 
607*a6c9808aSBALATON Zoltan     /* root node */
608*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,description",
609*a6c9808aSBALATON Zoltan                             "Pegasos CHRP PowerPC System");
610*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,board", "Pegasos2");
611*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "CODEGEN,vendor", "bplan GmbH");
612*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "revision", "2B");
613*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "model", "Pegasos2");
614*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "device_type", "chrp");
615*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 1);
616*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/", "name", "bplan,Pegasos2");
617*a6c9808aSBALATON Zoltan 
618*a6c9808aSBALATON Zoltan     /* pci@c0000000 */
619*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/pci@c0000000");
620*a6c9808aSBALATON Zoltan     cells[0] = 0;
621*a6c9808aSBALATON Zoltan     cells[1] = 0;
622*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "bus-range",
623*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
624*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "pci-bridge-number", 1);
625*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(PCI0_MEM_BASE);
626*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(PCI0_MEM_SIZE);
627*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "reg", cells, 2 * sizeof(cells[0]));
628*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(0x01000000);
629*a6c9808aSBALATON Zoltan     cells[1] = 0;
630*a6c9808aSBALATON Zoltan     cells[2] = 0;
631*a6c9808aSBALATON Zoltan     cells[3] = cpu_to_be32(PCI0_IO_BASE);
632*a6c9808aSBALATON Zoltan     cells[4] = 0;
633*a6c9808aSBALATON Zoltan     cells[5] = cpu_to_be32(PCI0_IO_SIZE);
634*a6c9808aSBALATON Zoltan     cells[6] = cpu_to_be32(0x02000000);
635*a6c9808aSBALATON Zoltan     cells[7] = 0;
636*a6c9808aSBALATON Zoltan     cells[8] = cpu_to_be32(PCI0_MEM_BASE);
637*a6c9808aSBALATON Zoltan     cells[9] = cpu_to_be32(PCI0_MEM_BASE);
638*a6c9808aSBALATON Zoltan     cells[10] = 0;
639*a6c9808aSBALATON Zoltan     cells[11] = cpu_to_be32(PCI0_MEM_SIZE);
640*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@c0000000", "ranges",
641*a6c9808aSBALATON Zoltan                      cells, 12 * sizeof(cells[0]));
642*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#size-cells", 2);
643*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@c0000000", "#address-cells", 3);
644*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@c0000000", "device_type", "pci");
645*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@c0000000", "name", "pci");
646*a6c9808aSBALATON Zoltan 
647*a6c9808aSBALATON Zoltan     fi.path = "/pci@c0000000";
648*a6c9808aSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 0);
649*a6c9808aSBALATON Zoltan     pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
650*a6c9808aSBALATON Zoltan 
651*a6c9808aSBALATON Zoltan     /* pci@80000000 */
652*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/pci@80000000");
653*a6c9808aSBALATON Zoltan     cells[0] = 0;
654*a6c9808aSBALATON Zoltan     cells[1] = 0;
655*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "bus-range",
656*a6c9808aSBALATON Zoltan                      cells, 2 * sizeof(cells[0]));
657*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "pci-bridge-number", 0);
658*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(PCI1_MEM_BASE);
659*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(PCI1_MEM_SIZE);
660*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "reg", cells, 2 * sizeof(cells[0]));
661*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "8259-interrupt-acknowledge",
662*a6c9808aSBALATON Zoltan                           0xf1000cb4);
663*a6c9808aSBALATON Zoltan     cells[0] = cpu_to_be32(0x01000000);
664*a6c9808aSBALATON Zoltan     cells[1] = 0;
665*a6c9808aSBALATON Zoltan     cells[2] = 0;
666*a6c9808aSBALATON Zoltan     cells[3] = cpu_to_be32(PCI1_IO_BASE);
667*a6c9808aSBALATON Zoltan     cells[4] = 0;
668*a6c9808aSBALATON Zoltan     cells[5] = cpu_to_be32(PCI1_IO_SIZE);
669*a6c9808aSBALATON Zoltan     cells[6] = cpu_to_be32(0x02000000);
670*a6c9808aSBALATON Zoltan     cells[7] = 0;
671*a6c9808aSBALATON Zoltan     cells[8] = cpu_to_be32(PCI1_MEM_BASE);
672*a6c9808aSBALATON Zoltan     cells[9] = cpu_to_be32(PCI1_MEM_BASE);
673*a6c9808aSBALATON Zoltan     cells[10] = 0;
674*a6c9808aSBALATON Zoltan     cells[11] = cpu_to_be32(PCI1_MEM_SIZE);
675*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/pci@80000000", "ranges",
676*a6c9808aSBALATON Zoltan                      cells, 12 * sizeof(cells[0]));
677*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#size-cells", 2);
678*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/pci@80000000", "#address-cells", 3);
679*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@80000000", "device_type", "pci");
680*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/pci@80000000", "name", "pci");
681*a6c9808aSBALATON Zoltan 
682*a6c9808aSBALATON Zoltan     fi.path = "/pci@80000000";
683*a6c9808aSBALATON Zoltan     pci_bus = mv64361_get_pci_bus(pm->mv, 1);
684*a6c9808aSBALATON Zoltan     pci_for_each_device_reverse(pci_bus, 0, add_pci_device, &fi);
685*a6c9808aSBALATON Zoltan 
686*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/failsafe");
687*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/failsafe", "device_type", "serial");
688*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/failsafe", "name", "failsafe");
689*a6c9808aSBALATON Zoltan 
690*a6c9808aSBALATON Zoltan     /* cpus */
691*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/cpus");
692*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#cpus", 1);
693*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
694*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
695*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/cpus", "name", "cpus");
696*a6c9808aSBALATON Zoltan 
697*a6c9808aSBALATON Zoltan     /* FIXME Get CPU name from CPU object */
698*a6c9808aSBALATON Zoltan     const char *cp = "/cpus/PowerPC,G4";
699*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, cp);
700*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "l2cr", 0);
701*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-size", 0x8000);
702*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-block-size",
703*a6c9808aSBALATON Zoltan                           cpu->env.dcache_line_size);
704*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "d-cache-line-size",
705*a6c9808aSBALATON Zoltan                           cpu->env.dcache_line_size);
706*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-size", 0x8000);
707*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-block-size",
708*a6c9808aSBALATON Zoltan                           cpu->env.icache_line_size);
709*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "i-cache-line-size",
710*a6c9808aSBALATON Zoltan                           cpu->env.icache_line_size);
711*a6c9808aSBALATON Zoltan     if (cpu->env.id_tlbs) {
712*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "i-tlb-sets", cpu->env.nb_ways);
713*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "i-tlb-size", cpu->env.tlb_per_way);
714*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "d-tlb-sets", cpu->env.nb_ways);
715*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_cell(fdt, cp, "d-tlb-size", cpu->env.tlb_per_way);
716*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "tlb-split", "");
717*a6c9808aSBALATON Zoltan     }
718*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "tlb-sets", cpu->env.nb_ways);
719*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "tlb-size", cpu->env.nb_tlb);
720*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "state", "running");
721*a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_ALTIVEC) {
722*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "altivec", "");
723*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "data-streams", "");
724*a6c9808aSBALATON Zoltan     }
725*a6c9808aSBALATON Zoltan     /*
726*a6c9808aSBALATON Zoltan      * FIXME What flags do data-streams, external-control and
727*a6c9808aSBALATON Zoltan      * performance-monitor depend on?
728*a6c9808aSBALATON Zoltan      */
729*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "external-control", "");
730*a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_FLOAT_FSQRT) {
731*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "general-purpose", "");
732*a6c9808aSBALATON Zoltan     }
733*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "performance-monitor", "");
734*a6c9808aSBALATON Zoltan     if (cpu->env.insns_flags & PPC_FLOAT_FRES) {
735*a6c9808aSBALATON Zoltan         qemu_fdt_setprop_string(fdt, cp, "graphics", "");
736*a6c9808aSBALATON Zoltan     }
737*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "reservation-granule-size", 4);
738*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "timebase-frequency",
739*a6c9808aSBALATON Zoltan                           cpu->env.tb_env->tb_freq);
740*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "bus-frequency", BUS_FREQ_HZ);
741*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "clock-frequency", BUS_FREQ_HZ * 7.5);
742*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_cell(fdt, cp, "cpu-version", cpu->env.spr[SPR_PVR]);
743*a6c9808aSBALATON Zoltan     cells[0] = 0;
744*a6c9808aSBALATON Zoltan     cells[1] = 0;
745*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, cp, "reg", cells, 2 * sizeof(cells[0]));
746*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "device_type", "cpu");
747*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, cp, "name", strrchr(cp, '/') + 1);
748*a6c9808aSBALATON Zoltan 
749*a6c9808aSBALATON Zoltan     /* memory */
750*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/memory@0");
751*a6c9808aSBALATON Zoltan     cells[0] = 0;
752*a6c9808aSBALATON Zoltan     cells[1] = cpu_to_be32(machine->ram_size);
753*a6c9808aSBALATON Zoltan     qemu_fdt_setprop(fdt, "/memory@0", "reg", cells, 2 * sizeof(cells[0]));
754*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/memory@0", "device_type", "memory");
755*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/memory@0", "name", "memory");
756*a6c9808aSBALATON Zoltan 
757*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/chosen");
758*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
759*a6c9808aSBALATON Zoltan                             machine->kernel_cmdline ?: "");
760*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/chosen", "name", "chosen");
761*a6c9808aSBALATON Zoltan 
762*a6c9808aSBALATON Zoltan     qemu_fdt_add_subnode(fdt, "/openprom");
763*a6c9808aSBALATON Zoltan     qemu_fdt_setprop_string(fdt, "/openprom", "model", "Pegasos2,1.1");
764*a6c9808aSBALATON Zoltan 
765*a6c9808aSBALATON Zoltan     return fdt;
766*a6c9808aSBALATON Zoltan }
767