]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/prep.c
hw: Do not include "sysemu/block-backend.h" if it is not necessary
[mirror_qemu.git] / hw / ppc / prep.c
CommitLineData
9a64fbe4 1/*
a541f297 2 * QEMU PPC PREP hardware System Emulator
5fafdf24 3 *
47103572 4 * Copyright (c) 2003-2007 Jocelyn Mayer
34b9b557 5 * Copyright (c) 2017 Hervé Poussineau
5fafdf24 6 *
a541f297
FB
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
9a64fbe4 24 */
0d75590d 25#include "qemu/osdep.h"
33c11879 26#include "cpu.h"
75610155 27#include "hw/hw.h"
0d09e41a
PB
28#include "hw/timer/m48t59.h"
29#include "hw/i386/pc.h"
30#include "hw/char/serial.h"
31#include "hw/block/fdc.h"
1422e32d 32#include "net/net.h"
9c17d615 33#include "sysemu/sysemu.h"
0d09e41a 34#include "hw/isa/isa.h"
75610155
AF
35#include "hw/pci/pci.h"
36#include "hw/pci/pci_host.h"
0d09e41a 37#include "hw/ppc/ppc.h"
75610155 38#include "hw/boards.h"
c525436e 39#include "qemu/error-report.h"
1de7afc9 40#include "qemu/log.h"
75610155
AF
41#include "hw/ide.h"
42#include "hw/loader.h"
0d09e41a
PB
43#include "hw/timer/mc146818rtc.h"
44#include "hw/isa/pc87312.h"
489983d6 45#include "hw/net/ne2000-isa.h"
9c17d615 46#include "sysemu/arch_init.h"
34b9b557 47#include "sysemu/kvm.h"
97c42c3c 48#include "sysemu/qtest.h"
022c62cb 49#include "exec/address-spaces.h"
659f7f65 50#include "trace.h"
97c42c3c 51#include "elf.h"
f348b6d1 52#include "qemu/cutils.h"
34b9b557 53#include "kvm_ppc.h"
9fddaa0c 54
fe33cc71
JM
55/* SMP is not enabled, for now */
56#define MAX_CPUS 1
57
e4bcb14c
TS
58#define MAX_IDE_BUS 2
59
34b9b557
HP
60#define CFG_ADDR 0xf0000510
61
bba831e8 62#define BIOS_SIZE (1024 * 1024)
b6b8bd18
FB
63#define BIOS_FILENAME "ppc_rom.bin"
64#define KERNEL_LOAD_ADDR 0x01000000
65#define INITRD_LOAD_ADDR 0x01800000
64201201 66
64201201 67/* Constants for devices init */
a541f297
FB
68static const int ide_iobase[2] = { 0x1f0, 0x170 };
69static const int ide_iobase2[2] = { 0x3f6, 0x376 };
70static const int ide_irq[2] = { 13, 13 };
71
72#define NE2000_NB_MAX 6
73
74static uint32_t ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
75static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
9a64fbe4 76
64201201 77/* ISA IO ports bridge */
9a64fbe4
FB
78#define PPC_IO_BASE 0x80000000
79
64201201
FB
80/* PowerPC control and status registers */
81#if 0 // Not used
82static struct {
83 /* IDs */
84 uint32_t veni_devi;
85 uint32_t revi;
86 /* Control and status */
87 uint32_t gcsr;
88 uint32_t xcfr;
89 uint32_t ct32;
90 uint32_t mcsr;
91 /* General purpose registers */
92 uint32_t gprg[6];
93 /* Exceptions */
94 uint32_t feen;
95 uint32_t fest;
96 uint32_t fema;
97 uint32_t fecl;
98 uint32_t eeen;
99 uint32_t eest;
100 uint32_t eecl;
101 uint32_t eeint;
102 uint32_t eemck0;
103 uint32_t eemck1;
104 /* Error diagnostic */
105} XCSR;
64201201 106
36081602 107static void PPC_XCSR_writeb (void *opaque,
a8170e5e 108 hwaddr addr, uint32_t value)
64201201 109{
90e189ec
BS
110 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
111 value);
64201201
FB
112}
113
36081602 114static void PPC_XCSR_writew (void *opaque,
a8170e5e 115 hwaddr addr, uint32_t value)
9a64fbe4 116{
90e189ec
BS
117 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
118 value);
9a64fbe4
FB
119}
120
36081602 121static void PPC_XCSR_writel (void *opaque,
a8170e5e 122 hwaddr addr, uint32_t value)
9a64fbe4 123{
90e189ec
BS
124 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
125 value);
9a64fbe4
FB
126}
127
a8170e5e 128static uint32_t PPC_XCSR_readb (void *opaque, hwaddr addr)
64201201
FB
129{
130 uint32_t retval = 0;
9a64fbe4 131
90e189ec
BS
132 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
133 retval);
9a64fbe4 134
64201201
FB
135 return retval;
136}
137
a8170e5e 138static uint32_t PPC_XCSR_readw (void *opaque, hwaddr addr)
9a64fbe4 139{
64201201
FB
140 uint32_t retval = 0;
141
90e189ec
BS
142 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
143 retval);
64201201
FB
144
145 return retval;
9a64fbe4
FB
146}
147
a8170e5e 148static uint32_t PPC_XCSR_readl (void *opaque, hwaddr addr)
9a64fbe4
FB
149{
150 uint32_t retval = 0;
151
90e189ec
BS
152 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
153 retval);
9a64fbe4
FB
154
155 return retval;
156}
157
0c90c52f
AK
158static const MemoryRegionOps PPC_XCSR_ops = {
159 .old_mmio = {
160 .read = { PPC_XCSR_readb, PPC_XCSR_readw, PPC_XCSR_readl, },
161 .write = { PPC_XCSR_writeb, PPC_XCSR_writew, PPC_XCSR_writel, },
162 },
163 .endianness = DEVICE_LITTLE_ENDIAN,
9a64fbe4
FB
164};
165
b6b8bd18 166#endif
9a64fbe4 167
64201201 168/* Fake super-io ports for PREP platform (Intel 82378ZB) */
c227f099 169typedef struct sysctrl_t {
c4781a51 170 qemu_irq reset_irq;
31688246 171 Nvram *nvram;
64201201
FB
172 uint8_t state;
173 uint8_t syscontrol;
da9b266b 174 int contiguous_map;
9a183916 175 qemu_irq contiguous_map_irq;
fb3444b8 176 int endian;
c227f099 177} sysctrl_t;
9a64fbe4 178
64201201
FB
179enum {
180 STATE_HARDFILE = 0x01,
9a64fbe4 181};
9a64fbe4 182
c227f099 183static sysctrl_t *sysctrl;
9a64fbe4 184
a541f297 185static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
9a64fbe4 186{
c227f099 187 sysctrl_t *sysctrl = opaque;
64201201 188
659f7f65 189 trace_prep_io_800_writeb(addr - PPC_IO_BASE, val);
9a64fbe4
FB
190 switch (addr) {
191 case 0x0092:
192 /* Special port 92 */
193 /* Check soft reset asked */
64201201 194 if (val & 0x01) {
c4781a51
JM
195 qemu_irq_raise(sysctrl->reset_irq);
196 } else {
197 qemu_irq_lower(sysctrl->reset_irq);
9a64fbe4
FB
198 }
199 /* Check LE mode */
64201201 200 if (val & 0x02) {
fb3444b8
FB
201 sysctrl->endian = 1;
202 } else {
203 sysctrl->endian = 0;
9a64fbe4
FB
204 }
205 break;
64201201
FB
206 case 0x0800:
207 /* Motorola CPU configuration register : read-only */
208 break;
209 case 0x0802:
210 /* Motorola base module feature register : read-only */
211 break;
212 case 0x0803:
213 /* Motorola base module status register : read-only */
214 break;
9a64fbe4 215 case 0x0808:
64201201
FB
216 /* Hardfile light register */
217 if (val & 1)
218 sysctrl->state |= STATE_HARDFILE;
219 else
220 sysctrl->state &= ~STATE_HARDFILE;
9a64fbe4
FB
221 break;
222 case 0x0810:
223 /* Password protect 1 register */
31688246
HP
224 if (sysctrl->nvram != NULL) {
225 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
226 (k->toggle_lock)(sysctrl->nvram, 1);
227 }
9a64fbe4
FB
228 break;
229 case 0x0812:
230 /* Password protect 2 register */
31688246
HP
231 if (sysctrl->nvram != NULL) {
232 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
233 (k->toggle_lock)(sysctrl->nvram, 2);
234 }
9a64fbe4
FB
235 break;
236 case 0x0814:
64201201 237 /* L2 invalidate register */
c68ea704 238 // tlb_flush(first_cpu, 1);
9a64fbe4
FB
239 break;
240 case 0x081C:
241 /* system control register */
64201201 242 sysctrl->syscontrol = val & 0x0F;
9a64fbe4
FB
243 break;
244 case 0x0850:
245 /* I/O map type register */
da9b266b 246 sysctrl->contiguous_map = val & 0x01;
9a183916 247 qemu_set_irq(sysctrl->contiguous_map_irq, sysctrl->contiguous_map);
9a64fbe4
FB
248 break;
249 default:
aae9366a
JM
250 printf("ERROR: unaffected IO port write: %04" PRIx32
251 " => %02" PRIx32"\n", addr, val);
9a64fbe4
FB
252 break;
253 }
254}
255
a541f297 256static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
9a64fbe4 257{
c227f099 258 sysctrl_t *sysctrl = opaque;
9a64fbe4
FB
259 uint32_t retval = 0xFF;
260
261 switch (addr) {
262 case 0x0092:
263 /* Special port 92 */
b6f54b31 264 retval = sysctrl->endian << 1;
64201201
FB
265 break;
266 case 0x0800:
267 /* Motorola CPU configuration register */
268 retval = 0xEF; /* MPC750 */
269 break;
270 case 0x0802:
271 /* Motorola Base module feature register */
272 retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
273 break;
274 case 0x0803:
275 /* Motorola base module status register */
276 retval = 0xE0; /* Standard MPC750 */
9a64fbe4
FB
277 break;
278 case 0x080C:
279 /* Equipment present register:
280 * no L2 cache
281 * no upgrade processor
282 * no cards in PCI slots
283 * SCSI fuse is bad
284 */
64201201
FB
285 retval = 0x3C;
286 break;
287 case 0x0810:
288 /* Motorola base module extended feature register */
289 retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
9a64fbe4 290 break;
da9b266b
FB
291 case 0x0814:
292 /* L2 invalidate: don't care */
293 break;
9a64fbe4
FB
294 case 0x0818:
295 /* Keylock */
296 retval = 0x00;
297 break;
298 case 0x081C:
299 /* system control register
300 * 7 - 6 / 1 - 0: L2 cache enable
301 */
64201201 302 retval = sysctrl->syscontrol;
9a64fbe4
FB
303 break;
304 case 0x0823:
305 /* */
306 retval = 0x03; /* no L2 cache */
307 break;
308 case 0x0850:
309 /* I/O map type register */
da9b266b 310 retval = sysctrl->contiguous_map;
9a64fbe4
FB
311 break;
312 default:
aae9366a 313 printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
9a64fbe4
FB
314 break;
315 }
659f7f65 316 trace_prep_io_800_readb(addr - PPC_IO_BASE, retval);
9a64fbe4
FB
317
318 return retval;
319}
320
da9b266b 321
64201201 322#define NVRAM_SIZE 0x2000
a541f297 323
34b9b557
HP
324static void fw_cfg_boot_set(void *opaque, const char *boot_device,
325 Error **errp)
326{
327 fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
328}
329
1bba0dc9
AF
330static void ppc_prep_reset(void *opaque)
331{
5c3e735f 332 PowerPCCPU *cpu = opaque;
1bba0dc9 333
5c3e735f 334 cpu_reset(CPU(cpu));
1bba0dc9
AF
335}
336
fd533eb5
JK
337static const MemoryRegionPortio prep_portio_list[] = {
338 /* System control ports */
339 { 0x0092, 1, 1, .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
340 { 0x0800, 0x52, 1,
341 .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
342 /* Special port to get debug messages from Open-Firmware */
343 { 0x0F00, 4, 1, .write = PPC_debug_write, },
344 PORTIO_END_OF_LIST(),
345};
346
848696bf
KB
347static PortioList prep_port_list;
348
31688246
HP
349/*****************************************************************************/
350/* NVRAM helpers */
351static inline uint32_t nvram_read(Nvram *nvram, uint32_t addr)
352{
5904bca8 353 NvramClass *k = NVRAM_GET_CLASS(nvram);
31688246
HP
354 return (k->read)(nvram, addr);
355}
356
357static inline void nvram_write(Nvram *nvram, uint32_t addr, uint32_t val)
358{
5904bca8 359 NvramClass *k = NVRAM_GET_CLASS(nvram);
31688246
HP
360 (k->write)(nvram, addr, val);
361}
362
363static void NVRAM_set_byte(Nvram *nvram, uint32_t addr, uint8_t value)
364{
365 nvram_write(nvram, addr, value);
366}
367
368static uint8_t NVRAM_get_byte(Nvram *nvram, uint32_t addr)
369{
370 return nvram_read(nvram, addr);
371}
372
373static void NVRAM_set_word(Nvram *nvram, uint32_t addr, uint16_t value)
374{
375 nvram_write(nvram, addr, value >> 8);
376 nvram_write(nvram, addr + 1, value & 0xFF);
377}
378
379static uint16_t NVRAM_get_word(Nvram *nvram, uint32_t addr)
380{
381 uint16_t tmp;
382
383 tmp = nvram_read(nvram, addr) << 8;
384 tmp |= nvram_read(nvram, addr + 1);
385
386 return tmp;
387}
388
389static void NVRAM_set_lword(Nvram *nvram, uint32_t addr, uint32_t value)
390{
391 nvram_write(nvram, addr, value >> 24);
392 nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
393 nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
394 nvram_write(nvram, addr + 3, value & 0xFF);
395}
396
397static void NVRAM_set_string(Nvram *nvram, uint32_t addr, const char *str,
398 uint32_t max)
399{
400 int i;
401
402 for (i = 0; i < max && str[i] != '\0'; i++) {
403 nvram_write(nvram, addr + i, str[i]);
404 }
405 nvram_write(nvram, addr + i, str[i]);
406 nvram_write(nvram, addr + max - 1, '\0');
407}
408
409static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
410{
411 uint16_t tmp;
412 uint16_t pd, pd1, pd2;
413
414 tmp = prev >> 8;
415 pd = prev ^ value;
416 pd1 = pd & 0x000F;
417 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
418 tmp ^= (pd1 << 3) | (pd1 << 8);
419 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
420
421 return tmp;
422}
423
424static uint16_t NVRAM_compute_crc (Nvram *nvram, uint32_t start, uint32_t count)
425{
426 uint32_t i;
427 uint16_t crc = 0xFFFF;
428 int odd;
429
430 odd = count & 1;
431 count &= ~1;
432 for (i = 0; i != count; i++) {
433 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
434 }
435 if (odd) {
436 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
437 }
438
439 return crc;
440}
441
442#define CMDLINE_ADDR 0x017ff000
443
444static int PPC_NVRAM_set_params (Nvram *nvram, uint16_t NVRAM_size,
445 const char *arch,
446 uint32_t RAM_size, int boot_device,
447 uint32_t kernel_image, uint32_t kernel_size,
448 const char *cmdline,
449 uint32_t initrd_image, uint32_t initrd_size,
450 uint32_t NVRAM_image,
451 int width, int height, int depth)
452{
453 uint16_t crc;
454
455 /* Set parameters for Open Hack'Ware BIOS */
456 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
457 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
458 NVRAM_set_word(nvram, 0x14, NVRAM_size);
459 NVRAM_set_string(nvram, 0x20, arch, 16);
460 NVRAM_set_lword(nvram, 0x30, RAM_size);
461 NVRAM_set_byte(nvram, 0x34, boot_device);
462 NVRAM_set_lword(nvram, 0x38, kernel_image);
463 NVRAM_set_lword(nvram, 0x3C, kernel_size);
464 if (cmdline) {
465 /* XXX: put the cmdline in NVRAM too ? */
466 pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR,
467 cmdline);
468 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
469 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
470 } else {
471 NVRAM_set_lword(nvram, 0x40, 0);
472 NVRAM_set_lword(nvram, 0x44, 0);
473 }
474 NVRAM_set_lword(nvram, 0x48, initrd_image);
475 NVRAM_set_lword(nvram, 0x4C, initrd_size);
476 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
477
478 NVRAM_set_word(nvram, 0x54, width);
479 NVRAM_set_word(nvram, 0x56, height);
480 NVRAM_set_word(nvram, 0x58, depth);
481 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
482 NVRAM_set_word(nvram, 0xFC, crc);
483
484 return 0;
485}
486
26aa7d72 487/* PowerPC PREP hardware initialisation */
3ef96221 488static void ppc_prep_init(MachineState *machine)
a541f297 489{
3ef96221 490 ram_addr_t ram_size = machine->ram_size;
3ef96221
MA
491 const char *kernel_filename = machine->kernel_filename;
492 const char *kernel_cmdline = machine->kernel_cmdline;
493 const char *initrd_filename = machine->initrd_filename;
494 const char *boot_device = machine->boot_order;
0c90c52f 495 MemoryRegion *sysmem = get_system_memory();
a9bf3df0 496 PowerPCCPU *cpu = NULL;
e2684c0b 497 CPUPPCState *env = NULL;
31688246 498 Nvram *m48t59;
0c90c52f
AK
499#if 0
500 MemoryRegion *xcsr = g_new(MemoryRegion, 1);
501#endif
d0b25425 502 int linux_boot, i, nb_nics1;
0c90c52f 503 MemoryRegion *ram = g_new(MemoryRegion, 1);
093209cd
BS
504 uint32_t kernel_base, initrd_base;
505 long kernel_size, initrd_size;
8ca8c7bc 506 DeviceState *dev;
8ca8c7bc 507 PCIHostState *pcihost;
46e50e9d 508 PCIBus *pci_bus;
506b7ddf 509 PCIDevice *pci;
48a18b3c 510 ISABus *isa_bus;
52a71bff 511 ISADevice *isa;
28c5af54 512 int ppc_boot_device;
f455e98c 513 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
64201201 514
7267c094 515 sysctrl = g_malloc0(sizeof(sysctrl_t));
a541f297
FB
516
517 linux_boot = (kernel_filename != NULL);
0a032cbe 518
c68ea704 519 /* init CPUs */
fe33cc71 520 for (i = 0; i < smp_cpus; i++) {
23ec69ec 521 cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
a9bf3df0
AF
522 env = &cpu->env;
523
4018bae9
JM
524 if (env->flags & POWERPC_FLAG_RTC_CLK) {
525 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
526 cpu_ppc_tb_init(env, 7812500UL);
527 } else {
528 /* Set time-base frequency to 100 Mhz */
529 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
530 }
5c3e735f 531 qemu_register_reset(ppc_prep_reset, cpu);
fe33cc71 532 }
a541f297
FB
533
534 /* allocate RAM */
e938ba0c 535 memory_region_allocate_system_memory(ram, NULL, "ppc_prep.ram", ram_size);
0c90c52f 536 memory_region_add_subregion(sysmem, 0, ram);
cf9c147c 537
a541f297 538 if (linux_boot) {
64201201 539 kernel_base = KERNEL_LOAD_ADDR;
a541f297 540 /* now we can load the kernel */
dcac9679
PB
541 kernel_size = load_image_targphys(kernel_filename, kernel_base,
542 ram_size - kernel_base);
64201201 543 if (kernel_size < 0) {
c525436e 544 error_report("could not load kernel '%s'", kernel_filename);
a541f297
FB
545 exit(1);
546 }
547 /* load initrd */
a541f297 548 if (initrd_filename) {
64201201 549 initrd_base = INITRD_LOAD_ADDR;
dcac9679
PB
550 initrd_size = load_image_targphys(initrd_filename, initrd_base,
551 ram_size - initrd_base);
a541f297 552 if (initrd_size < 0) {
c525436e
MA
553 error_report("could not load initial ram disk '%s'",
554 initrd_filename);
555 exit(1);
a541f297 556 }
64201201
FB
557 } else {
558 initrd_base = 0;
559 initrd_size = 0;
a541f297 560 }
6ac0e82d 561 ppc_boot_device = 'm';
a541f297 562 } else {
64201201
FB
563 kernel_base = 0;
564 kernel_size = 0;
565 initrd_base = 0;
566 initrd_size = 0;
28c5af54
JM
567 ppc_boot_device = '\0';
568 /* For now, OHW cannot boot from the network. */
0d913fdb
JM
569 for (i = 0; boot_device[i] != '\0'; i++) {
570 if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
571 ppc_boot_device = boot_device[i];
28c5af54 572 break;
0d913fdb 573 }
28c5af54
JM
574 }
575 if (ppc_boot_device == '\0') {
6f76b817 576 error_report("No valid boot device for Mac99 machine");
28c5af54
JM
577 exit(1);
578 }
a541f297
FB
579 }
580
dd37a5e4 581 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
c525436e
MA
582 error_report("Only 6xx bus is supported on PREP machine");
583 exit(1);
dd37a5e4 584 }
8ca8c7bc
AF
585
586 dev = qdev_create(NULL, "raven-pcihost");
d0b25425
HP
587 if (bios_name == NULL) {
588 bios_name = BIOS_FILENAME;
589 }
590 qdev_prop_set_string(dev, "bios-name", bios_name);
4ecd4d16 591 qdev_prop_set_uint32(dev, "elf-machine", PPC_ELF_MACHINE);
8558d942 592 pcihost = PCI_HOST_BRIDGE(dev);
f05f6b4a 593 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
f424d5c4 594 qdev_init_nofail(dev);
8ca8c7bc
AF
595 pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
596 if (pci_bus == NULL) {
6f76b817 597 error_report("Couldn't create PCI host controller");
8ca8c7bc
AF
598 exit(1);
599 }
9a183916 600 sysctrl->contiguous_map_irq = qdev_get_gpio_in(dev, 0);
8ca8c7bc 601
506b7ddf
AF
602 /* PCI -> ISA bridge */
603 pci = pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "i82378");
182735ef 604 cpu = POWERPC_CPU(first_cpu);
506b7ddf 605 qdev_connect_gpio_out(&pci->qdev, 0,
182735ef 606 cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
506b7ddf
AF
607 sysbus_connect_irq(&pcihost->busdev, 0, qdev_get_gpio_in(&pci->qdev, 9));
608 sysbus_connect_irq(&pcihost->busdev, 1, qdev_get_gpio_in(&pci->qdev, 11));
609 sysbus_connect_irq(&pcihost->busdev, 2, qdev_get_gpio_in(&pci->qdev, 9));
610 sysbus_connect_irq(&pcihost->busdev, 3, qdev_get_gpio_in(&pci->qdev, 11));
2ae0e48d 611 isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci), "isa.0"));
506b7ddf 612
52a71bff
HP
613 /* Super I/O (parallel + serial ports) */
614 isa = isa_create(isa_bus, TYPE_PC87312);
4a17cc4f
AF
615 dev = DEVICE(isa);
616 qdev_prop_set_uint8(dev, "config", 13); /* fdc, ser0, ser1, par0 */
617 qdev_init_nofail(dev);
52a71bff 618
a541f297 619 /* init basic PC hardware */
78895427 620 pci_vga_init(pci_bus);
a541f297 621
a541f297
FB
622 nb_nics1 = nb_nics;
623 if (nb_nics1 > NE2000_NB_MAX)
624 nb_nics1 = NE2000_NB_MAX;
625 for(i = 0; i < nb_nics1; i++) {
5652ef78 626 if (nd_table[i].model == NULL) {
7267c094 627 nd_table[i].model = g_strdup("ne2k_isa");
5652ef78
AJ
628 }
629 if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
48a18b3c
HP
630 isa_ne2000_init(isa_bus, ne2000_io[i], ne2000_irq[i],
631 &nd_table[i]);
a41b2ff2 632 } else {
29b358f9 633 pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
a41b2ff2 634 }
a541f297 635 }
a541f297 636
d8f94e1b 637 ide_drive_get(hd, ARRAY_SIZE(hd));
81aa0647 638 for(i = 0; i < MAX_IDE_BUS; i++) {
48a18b3c 639 isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
e4bcb14c
TS
640 hd[2 * i],
641 hd[2 * i + 1]);
a541f297 642 }
48a18b3c 643 isa_create_simple(isa_bus, "i8042");
4556bd8b 644
182735ef
AF
645 cpu = POWERPC_CPU(first_cpu);
646 sysctrl->reset_irq = cpu->env.irq_inputs[PPC6xx_INPUT_HRESET];
fd533eb5 647
848696bf
KB
648 portio_list_init(&prep_port_list, NULL, prep_portio_list, sysctrl, "prep");
649 portio_list_add(&prep_port_list, isa_address_space_io(isa), 0x0);
fd533eb5 650
64201201 651 /* PowerPC control and status register group */
b6b8bd18 652#if 0
2c9b15ca 653 memory_region_init_io(xcsr, NULL, &PPC_XCSR_ops, NULL, "ppc-xcsr", 0x1000);
0c90c52f 654 memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
b6b8bd18 655#endif
a541f297 656
4bcbe0b6 657 if (machine_usb(machine)) {
afb9a60e 658 pci_create_simple(pci_bus, -1, "pci-ohci");
0d92ed30
PB
659 }
660
6de04973 661 m48t59 = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 2000, 59);
3cbee15b 662 if (m48t59 == NULL)
64201201 663 return;
3cbee15b 664 sysctrl->nvram = m48t59;
64201201
FB
665
666 /* Initialise NVRAM */
31688246
HP
667 PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
668 ppc_boot_device,
64201201 669 kernel_base, kernel_size,
b6b8bd18 670 kernel_cmdline,
64201201
FB
671 initrd_base, initrd_size,
672 /* XXX: need an option to load a NVRAM image */
b6b8bd18
FB
673 0,
674 graphic_width, graphic_height, graphic_depth);
a541f297 675}
c0e564d5 676
e264d29d 677static void prep_machine_init(MachineClass *mc)
f80f9ec9 678{
e264d29d
EH
679 mc->desc = "PowerPC PREP platform";
680 mc->init = ppc_prep_init;
2059839b 681 mc->block_default_type = IF_IDE;
e264d29d
EH
682 mc->max_cpus = MAX_CPUS;
683 mc->default_boot_order = "cad";
23ec69ec 684 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("602");
f80f9ec9
AL
685}
686
34b9b557
HP
687static int prep_set_cmos_checksum(DeviceState *dev, void *opaque)
688{
689 uint16_t checksum = *(uint16_t *)opaque;
690 ISADevice *rtc;
691
692 if (object_dynamic_cast(OBJECT(dev), "mc146818rtc")) {
693 rtc = ISA_DEVICE(dev);
694 rtc_set_memory(rtc, 0x2e, checksum & 0xff);
695 rtc_set_memory(rtc, 0x3e, checksum & 0xff);
696 rtc_set_memory(rtc, 0x2f, checksum >> 8);
697 rtc_set_memory(rtc, 0x3f, checksum >> 8);
698 }
699 return 0;
700}
701
702static void ibm_40p_init(MachineState *machine)
703{
704 CPUPPCState *env = NULL;
705 uint16_t cmos_checksum;
706 PowerPCCPU *cpu;
707 DeviceState *dev;
708 SysBusDevice *pcihost;
709 Nvram *m48t59 = NULL;
710 PCIBus *pci_bus;
711 ISABus *isa_bus;
712 void *fw_cfg;
713 int i;
714 uint32_t kernel_base = 0, initrd_base = 0;
715 long kernel_size = 0, initrd_size = 0;
716 char boot_device;
717
718 /* init CPU */
23ec69ec 719 cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
34b9b557
HP
720 env = &cpu->env;
721 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
722 error_report("only 6xx bus is supported on this machine");
723 exit(1);
724 }
725
726 if (env->flags & POWERPC_FLAG_RTC_CLK) {
727 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
728 cpu_ppc_tb_init(env, 7812500UL);
729 } else {
730 /* Set time-base frequency to 100 Mhz */
731 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
732 }
733 qemu_register_reset(ppc_prep_reset, cpu);
734
735 /* PCI host */
736 dev = qdev_create(NULL, "raven-pcihost");
737 if (!bios_name) {
738 bios_name = BIOS_FILENAME;
739 }
740 qdev_prop_set_string(dev, "bios-name", bios_name);
741 qdev_prop_set_uint32(dev, "elf-machine", PPC_ELF_MACHINE);
742 pcihost = SYS_BUS_DEVICE(dev);
743 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
744 qdev_init_nofail(dev);
745 pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci.0"));
746 if (!pci_bus) {
747 error_report("could not create PCI host controller");
748 exit(1);
749 }
750
751 /* PCI -> ISA bridge */
752 dev = DEVICE(pci_create_simple(pci_bus, PCI_DEVFN(11, 0), "i82378"));
753 qdev_connect_gpio_out(dev, 0,
754 cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
755 sysbus_connect_irq(pcihost, 0, qdev_get_gpio_in(dev, 15));
756 sysbus_connect_irq(pcihost, 1, qdev_get_gpio_in(dev, 13));
757 sysbus_connect_irq(pcihost, 2, qdev_get_gpio_in(dev, 15));
758 sysbus_connect_irq(pcihost, 3, qdev_get_gpio_in(dev, 13));
759 isa_bus = ISA_BUS(qdev_get_child_bus(dev, "isa.0"));
760
761 /* Memory controller */
762 dev = DEVICE(isa_create(isa_bus, "rs6000-mc"));
763 qdev_prop_set_uint32(dev, "ram-size", machine->ram_size);
764 qdev_init_nofail(dev);
765
766 /* initialize CMOS checksums */
767 cmos_checksum = 0x6aa9;
768 qbus_walk_children(BUS(isa_bus), prep_set_cmos_checksum, NULL, NULL, NULL,
769 &cmos_checksum);
770
34b9b557
HP
771 /* add some more devices */
772 if (defaults_enabled()) {
773 isa_create_simple(isa_bus, "i8042");
774 m48t59 = NVRAM(isa_create_simple(isa_bus, "isa-m48t59"));
775
776 dev = DEVICE(isa_create(isa_bus, "cs4231a"));
777 qdev_prop_set_uint32(dev, "iobase", 0x830);
778 qdev_prop_set_uint32(dev, "irq", 10);
779 qdev_init_nofail(dev);
780
781 dev = DEVICE(isa_create(isa_bus, "pc87312"));
782 qdev_prop_set_uint32(dev, "config", 12);
783 qdev_init_nofail(dev);
784
785 dev = DEVICE(isa_create(isa_bus, "prep-systemio"));
786 qdev_prop_set_uint32(dev, "ibm-planar-id", 0xfc);
787 qdev_prop_set_uint32(dev, "equipment", 0xc0);
788 qdev_init_nofail(dev);
789
790 pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "lsi53c810");
791
792 /* XXX: s3-trio at PCI_DEVFN(2, 0) */
793 pci_vga_init(pci_bus);
794
795 for (i = 0; i < nb_nics; i++) {
796 pci_nic_init_nofail(&nd_table[i], pci_bus, "pcnet",
797 i == 0 ? "3" : NULL);
798 }
799 }
800
801 /* Prepare firmware configuration for OpenBIOS */
802 fw_cfg = fw_cfg_init_mem(CFG_ADDR, CFG_ADDR + 2);
803
804 if (machine->kernel_filename) {
805 /* load kernel */
806 kernel_base = KERNEL_LOAD_ADDR;
807 kernel_size = load_image_targphys(machine->kernel_filename,
808 kernel_base,
809 machine->ram_size - kernel_base);
810 if (kernel_size < 0) {
811 error_report("could not load kernel '%s'",
812 machine->kernel_filename);
813 exit(1);
814 }
815 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
816 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
817 /* load initrd */
818 if (machine->initrd_filename) {
819 initrd_base = INITRD_LOAD_ADDR;
820 initrd_size = load_image_targphys(machine->initrd_filename,
821 initrd_base,
822 machine->ram_size - initrd_base);
823 if (initrd_size < 0) {
824 error_report("could not load initial ram disk '%s'",
825 machine->initrd_filename);
826 exit(1);
827 }
828 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
829 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
830 }
831 if (machine->kernel_cmdline && *machine->kernel_cmdline) {
832 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, CMDLINE_ADDR);
833 pstrcpy_targphys("cmdline", CMDLINE_ADDR, TARGET_PAGE_SIZE,
834 machine->kernel_cmdline);
835 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
836 machine->kernel_cmdline);
837 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
838 strlen(machine->kernel_cmdline) + 1);
839 }
840 boot_device = 'm';
841 } else {
842 boot_device = machine->boot_order[0];
843 }
844
845 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
846 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size);
847 fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_PREP);
848
849 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_WIDTH, graphic_width);
850 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_HEIGHT, graphic_height);
851 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_DEPTH, graphic_depth);
852
853 fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_IS_KVM, kvm_enabled());
854 if (kvm_enabled()) {
855#ifdef CONFIG_KVM
856 uint8_t *hypercall;
857
858 fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, kvmppc_get_tbfreq());
859 hypercall = g_malloc(16);
860 kvmppc_get_hypercall(env, hypercall, 16);
861 fw_cfg_add_bytes(fw_cfg, FW_CFG_PPC_KVM_HC, hypercall, 16);
862 fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_KVM_PID, getpid());
863#endif
864 } else {
865 fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, NANOSECONDS_PER_SECOND);
866 }
867 fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, boot_device);
868 qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
869
870 /* Prepare firmware configuration for Open Hack'Ware */
871 if (m48t59) {
872 PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
873 boot_device,
874 kernel_base, kernel_size,
875 machine->kernel_cmdline,
876 initrd_base, initrd_size,
877 /* XXX: need an option to load a NVRAM image */
878 0,
879 graphic_width, graphic_height, graphic_depth);
880 }
881}
882
883static void ibm_40p_machine_init(MachineClass *mc)
884{
885 mc->desc = "IBM RS/6000 7020 (40p)",
886 mc->init = ibm_40p_init;
887 mc->max_cpus = 1;
888 mc->pci_allow_0_address = true;
889 mc->default_ram_size = 128 * M_BYTE;
890 mc->block_default_type = IF_SCSI;
891 mc->default_boot_order = "c";
23ec69ec 892 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("604");
34b9b557
HP
893}
894
895DEFINE_MACHINE("40p", ibm_40p_machine_init)
e264d29d 896DEFINE_MACHINE("prep", prep_machine_init)