Sparc64: configure screen size from QEMU command line options
[qemu] / hw / sun4u.c
1 /*
2  * QEMU Sun4u/Sun4v System Emulator
3  *
4  * Copyright (c) 2005 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "hw.h"
25 #include "pci.h"
26 #include "pc.h"
27 #include "nvram.h"
28 #include "fdc.h"
29 #include "net.h"
30 #include "qemu-timer.h"
31 #include "sysemu.h"
32 #include "boards.h"
33 #include "firmware_abi.h"
34 #include "fw_cfg.h"
35 #include "sysbus.h"
36
37 //#define DEBUG_IRQ
38
39 #ifdef DEBUG_IRQ
40 #define DPRINTF(fmt, ...)                                       \
41     do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
42 #else
43 #define DPRINTF(fmt, ...)
44 #endif
45
46 #define KERNEL_LOAD_ADDR     0x00404000
47 #define CMDLINE_ADDR         0x003ff000
48 #define INITRD_LOAD_ADDR     0x00300000
49 #define PROM_SIZE_MAX        (4 * 1024 * 1024)
50 #define PROM_VADDR           0x000ffd00000ULL
51 #define APB_SPECIAL_BASE     0x1fe00000000ULL
52 #define APB_MEM_BASE         0x1ff00000000ULL
53 #define VGA_BASE             (APB_MEM_BASE + 0x400000ULL)
54 #define PROM_FILENAME        "openbios-sparc64"
55 #define NVRAM_SIZE           0x2000
56 #define MAX_IDE_BUS          2
57 #define BIOS_CFG_IOPORT      0x510
58 #define FW_CFG_SPARC64_WIDTH (FW_CFG_ARCH_LOCAL + 0x00)
59 #define FW_CFG_SPARC64_HEIGHT (FW_CFG_ARCH_LOCAL + 0x01)
60 #define FW_CFG_SPARC64_DEPTH (FW_CFG_ARCH_LOCAL + 0x02)
61
62 #define MAX_PILS 16
63
64 #define TICK_INT_DIS         0x8000000000000000ULL
65 #define TICK_MAX             0x7fffffffffffffffULL
66
67 struct hwdef {
68     const char * const default_cpu_model;
69     uint16_t machine_id;
70     uint64_t prom_addr;
71     uint64_t console_serial_base;
72 };
73
74 int DMA_get_channel_mode (int nchan)
75 {
76     return 0;
77 }
78 int DMA_read_memory (int nchan, void *buf, int pos, int size)
79 {
80     return 0;
81 }
82 int DMA_write_memory (int nchan, void *buf, int pos, int size)
83 {
84     return 0;
85 }
86 void DMA_hold_DREQ (int nchan) {}
87 void DMA_release_DREQ (int nchan) {}
88 void DMA_schedule(int nchan) {}
89 void DMA_init (int high_page_enable) {}
90 void DMA_register_channel (int nchan,
91                            DMA_transfer_handler transfer_handler,
92                            void *opaque)
93 {
94 }
95
96 static int fw_cfg_boot_set(void *opaque, const char *boot_device)
97 {
98     fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
99     return 0;
100 }
101
102 static int sun4u_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
103                                    const char *arch,
104                                    ram_addr_t RAM_size,
105                                    const char *boot_devices,
106                                    uint32_t kernel_image, uint32_t kernel_size,
107                                    const char *cmdline,
108                                    uint32_t initrd_image, uint32_t initrd_size,
109                                    uint32_t NVRAM_image,
110                                    int width, int height, int depth,
111                                    const uint8_t *macaddr)
112 {
113     unsigned int i;
114     uint32_t start, end;
115     uint8_t image[0x1ff0];
116     struct OpenBIOS_nvpart_v1 *part_header;
117
118     memset(image, '\0', sizeof(image));
119
120     start = 0;
121
122     // OpenBIOS nvram variables
123     // Variable partition
124     part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
125     part_header->signature = OPENBIOS_PART_SYSTEM;
126     pstrcpy(part_header->name, sizeof(part_header->name), "system");
127
128     end = start + sizeof(struct OpenBIOS_nvpart_v1);
129     for (i = 0; i < nb_prom_envs; i++)
130         end = OpenBIOS_set_var(image, end, prom_envs[i]);
131
132     // End marker
133     image[end++] = '\0';
134
135     end = start + ((end - start + 15) & ~15);
136     OpenBIOS_finish_partition(part_header, end - start);
137
138     // free partition
139     start = end;
140     part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
141     part_header->signature = OPENBIOS_PART_FREE;
142     pstrcpy(part_header->name, sizeof(part_header->name), "free");
143
144     end = 0x1fd0;
145     OpenBIOS_finish_partition(part_header, end - start);
146
147     Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, 0x80);
148
149     for (i = 0; i < sizeof(image); i++)
150         m48t59_write(nvram, i, image[i]);
151
152     return 0;
153 }
154 static unsigned long sun4u_load_kernel(const char *kernel_filename,
155                                        const char *initrd_filename,
156                                        ram_addr_t RAM_size, long *initrd_size)
157 {
158     int linux_boot;
159     unsigned int i;
160     long kernel_size;
161
162     linux_boot = (kernel_filename != NULL);
163
164     kernel_size = 0;
165     if (linux_boot) {
166         kernel_size = load_elf(kernel_filename, 0, NULL, NULL, NULL);
167         if (kernel_size < 0)
168             kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
169                                     RAM_size - KERNEL_LOAD_ADDR);
170         if (kernel_size < 0)
171             kernel_size = load_image_targphys(kernel_filename,
172                                               KERNEL_LOAD_ADDR,
173                                               RAM_size - KERNEL_LOAD_ADDR);
174         if (kernel_size < 0) {
175             fprintf(stderr, "qemu: could not load kernel '%s'\n",
176                     kernel_filename);
177             exit(1);
178         }
179
180         /* load initrd */
181         *initrd_size = 0;
182         if (initrd_filename) {
183             *initrd_size = load_image_targphys(initrd_filename,
184                                                INITRD_LOAD_ADDR,
185                                                RAM_size - INITRD_LOAD_ADDR);
186             if (*initrd_size < 0) {
187                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
188                         initrd_filename);
189                 exit(1);
190             }
191         }
192         if (*initrd_size > 0) {
193             for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
194                 if (ldl_phys(KERNEL_LOAD_ADDR + i) == 0x48647253) { // HdrS
195                     stl_phys(KERNEL_LOAD_ADDR + i + 16, INITRD_LOAD_ADDR);
196                     stl_phys(KERNEL_LOAD_ADDR + i + 20, *initrd_size);
197                     break;
198                 }
199             }
200         }
201     }
202     return kernel_size;
203 }
204
205 void pic_info(Monitor *mon)
206 {
207 }
208
209 void irq_info(Monitor *mon)
210 {
211 }
212
213 void cpu_check_irqs(CPUState *env)
214 {
215     uint32_t pil = env->pil_in | (env->softint & ~SOFTINT_TIMER) |
216         ((env->softint & SOFTINT_TIMER) << 14);
217
218     if (pil && (env->interrupt_index == 0 ||
219                 (env->interrupt_index & ~15) == TT_EXTINT)) {
220         unsigned int i;
221
222         for (i = 15; i > 0; i--) {
223             if (pil & (1 << i)) {
224                 int old_interrupt = env->interrupt_index;
225
226                 env->interrupt_index = TT_EXTINT | i;
227                 if (old_interrupt != env->interrupt_index) {
228                     DPRINTF("Set CPU IRQ %d\n", i);
229                     cpu_interrupt(env, CPU_INTERRUPT_HARD);
230                 }
231                 break;
232             }
233         }
234     } else if (!pil && (env->interrupt_index & ~15) == TT_EXTINT) {
235         DPRINTF("Reset CPU IRQ %d\n", env->interrupt_index & 15);
236         env->interrupt_index = 0;
237         cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
238     }
239 }
240
241 static void cpu_set_irq(void *opaque, int irq, int level)
242 {
243     CPUState *env = opaque;
244
245     if (level) {
246         DPRINTF("Raise CPU IRQ %d\n", irq);
247         env->halted = 0;
248         env->pil_in |= 1 << irq;
249         cpu_check_irqs(env);
250     } else {
251         DPRINTF("Lower CPU IRQ %d\n", irq);
252         env->pil_in &= ~(1 << irq);
253         cpu_check_irqs(env);
254     }
255 }
256
257 void qemu_system_powerdown(void)
258 {
259 }
260
261 typedef struct ResetData {
262     CPUState *env;
263     uint64_t reset_addr;
264 } ResetData;
265
266 static void main_cpu_reset(void *opaque)
267 {
268     ResetData *s = (ResetData *)opaque;
269     CPUState *env = s->env;
270
271     cpu_reset(env);
272     env->tick_cmpr = TICK_INT_DIS | 0;
273     ptimer_set_limit(env->tick, TICK_MAX, 1);
274     ptimer_run(env->tick, 1);
275     env->stick_cmpr = TICK_INT_DIS | 0;
276     ptimer_set_limit(env->stick, TICK_MAX, 1);
277     ptimer_run(env->stick, 1);
278     env->hstick_cmpr = TICK_INT_DIS | 0;
279     ptimer_set_limit(env->hstick, TICK_MAX, 1);
280     ptimer_run(env->hstick, 1);
281     env->gregs[1] = 0; // Memory start
282     env->gregs[2] = ram_size; // Memory size
283     env->gregs[3] = 0; // Machine description XXX
284     env->pc = s->reset_addr;
285     env->npc = env->pc + 4;
286 }
287
288 static void tick_irq(void *opaque)
289 {
290     CPUState *env = opaque;
291
292     if (!(env->tick_cmpr & TICK_INT_DIS)) {
293         env->softint |= SOFTINT_TIMER;
294         cpu_interrupt(env, CPU_INTERRUPT_TIMER);
295     }
296 }
297
298 static void stick_irq(void *opaque)
299 {
300     CPUState *env = opaque;
301
302     if (!(env->stick_cmpr & TICK_INT_DIS)) {
303         env->softint |= SOFTINT_STIMER;
304         cpu_interrupt(env, CPU_INTERRUPT_TIMER);
305     }
306 }
307
308 static void hstick_irq(void *opaque)
309 {
310     CPUState *env = opaque;
311
312     if (!(env->hstick_cmpr & TICK_INT_DIS)) {
313         cpu_interrupt(env, CPU_INTERRUPT_TIMER);
314     }
315 }
316
317 void cpu_tick_set_count(void *opaque, uint64_t count)
318 {
319     ptimer_set_count(opaque, -count);
320 }
321
322 uint64_t cpu_tick_get_count(void *opaque)
323 {
324     return -ptimer_get_count(opaque);
325 }
326
327 void cpu_tick_set_limit(void *opaque, uint64_t limit)
328 {
329     ptimer_set_limit(opaque, -limit, 0);
330 }
331
332 static const int ide_iobase[2] = { 0x1f0, 0x170 };
333 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
334 static const int ide_irq[2] = { 14, 15 };
335
336 static const int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
337 static const int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
338
339 static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
340 static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
341
342 static fdctrl_t *floppy_controller;
343
344 static void ebus_mmio_mapfunc(PCIDevice *pci_dev, int region_num,
345                               uint32_t addr, uint32_t size, int type)
346 {
347     DPRINTF("Mapping region %d registers at %08x\n", region_num, addr);
348     switch (region_num) {
349     case 0:
350         isa_mmio_init(addr, 0x1000000);
351         break;
352     case 1:
353         isa_mmio_init(addr, 0x800000);
354         break;
355     }
356 }
357
358 /* EBUS (Eight bit bus) bridge */
359 static void
360 pci_ebus_init(PCIBus *bus, int devfn)
361 {
362     pci_create_simple(bus, devfn, "ebus");
363 }
364
365 static void
366 pci_ebus_init1(PCIDevice *s)
367 {
368     pci_config_set_vendor_id(s->config, PCI_VENDOR_ID_SUN);
369     pci_config_set_device_id(s->config, PCI_DEVICE_ID_SUN_EBUS);
370     s->config[0x04] = 0x06; // command = bus master, pci mem
371     s->config[0x05] = 0x00;
372     s->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
373     s->config[0x07] = 0x03; // status = medium devsel
374     s->config[0x08] = 0x01; // revision
375     s->config[0x09] = 0x00; // programming i/f
376     pci_config_set_class(s->config, PCI_CLASS_BRIDGE_OTHER);
377     s->config[0x0D] = 0x0a; // latency_timer
378     s->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
379
380     pci_register_bar(s, 0, 0x1000000, PCI_ADDRESS_SPACE_MEM,
381                            ebus_mmio_mapfunc);
382     pci_register_bar(s, 1, 0x800000,  PCI_ADDRESS_SPACE_MEM,
383                            ebus_mmio_mapfunc);
384 }
385
386 static PCIDeviceInfo ebus_info = {
387     .qdev.name = "ebus",
388     .qdev.size = sizeof(PCIDevice),
389     .init = pci_ebus_init1,
390 };
391
392 static void pci_ebus_register(void)
393 {
394     pci_qdev_register(&ebus_info);
395 }
396
397 device_init(pci_ebus_register);
398
399 /* Boot PROM (OpenBIOS) */
400 static void prom_init(target_phys_addr_t addr, const char *bios_name)
401 {
402     DeviceState *dev;
403     SysBusDevice *s;
404     char *filename;
405     int ret;
406
407     dev = qdev_create(NULL, "openprom");
408     qdev_init(dev);
409     s = sysbus_from_qdev(dev);
410
411     sysbus_mmio_map(s, 0, addr);
412
413     /* load boot prom */
414     if (bios_name == NULL) {
415         bios_name = PROM_FILENAME;
416     }
417     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
418     if (filename) {
419         ret = load_elf(filename, addr - PROM_VADDR, NULL, NULL, NULL);
420         if (ret < 0 || ret > PROM_SIZE_MAX) {
421             ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
422         }
423         qemu_free(filename);
424     } else {
425         ret = -1;
426     }
427     if (ret < 0 || ret > PROM_SIZE_MAX) {
428         fprintf(stderr, "qemu: could not load prom '%s'\n", bios_name);
429         exit(1);
430     }
431 }
432
433 static void prom_init1(SysBusDevice *dev)
434 {
435     ram_addr_t prom_offset;
436
437     prom_offset = qemu_ram_alloc(PROM_SIZE_MAX);
438     sysbus_init_mmio(dev, PROM_SIZE_MAX, prom_offset | IO_MEM_ROM);
439 }
440
441 static SysBusDeviceInfo prom_info = {
442     .init = prom_init1,
443     .qdev.name  = "openprom",
444     .qdev.size  = sizeof(SysBusDevice),
445     .qdev.props = (Property[]) {
446         {/* end of property list */}
447     }
448 };
449
450 static void prom_register_devices(void)
451 {
452     sysbus_register_withprop(&prom_info);
453 }
454
455 device_init(prom_register_devices);
456
457
458 typedef struct RamDevice
459 {
460     SysBusDevice busdev;
461     uint64_t size;
462 } RamDevice;
463
464 /* System RAM */
465 static void ram_init1(SysBusDevice *dev)
466 {
467     ram_addr_t RAM_size, ram_offset;
468     RamDevice *d = FROM_SYSBUS(RamDevice, dev);
469
470     RAM_size = d->size;
471
472     ram_offset = qemu_ram_alloc(RAM_size);
473     sysbus_init_mmio(dev, RAM_size, ram_offset);
474 }
475
476 static void ram_init(target_phys_addr_t addr, ram_addr_t RAM_size)
477 {
478     DeviceState *dev;
479     SysBusDevice *s;
480     RamDevice *d;
481
482     /* allocate RAM */
483     dev = qdev_create(NULL, "memory");
484     s = sysbus_from_qdev(dev);
485
486     d = FROM_SYSBUS(RamDevice, s);
487     d->size = RAM_size;
488     qdev_init(dev);
489
490     sysbus_mmio_map(s, 0, addr);
491 }
492
493 static SysBusDeviceInfo ram_info = {
494     .init = ram_init1,
495     .qdev.name  = "memory",
496     .qdev.size  = sizeof(RamDevice),
497     .qdev.props = (Property[]) {
498         {
499             .name = "size",
500             .info = &qdev_prop_uint64,
501             .offset = offsetof(RamDevice, size),
502         },
503         {/* end of property list */}
504     }
505 };
506
507 static void ram_register_devices(void)
508 {
509     sysbus_register_withprop(&ram_info);
510 }
511
512 device_init(ram_register_devices);
513
514 static CPUState *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
515 {
516     CPUState *env;
517     QEMUBH *bh;
518     ResetData *reset_info;
519
520     if (!cpu_model)
521         cpu_model = hwdef->default_cpu_model;
522     env = cpu_init(cpu_model);
523     if (!env) {
524         fprintf(stderr, "Unable to find Sparc CPU definition\n");
525         exit(1);
526     }
527     bh = qemu_bh_new(tick_irq, env);
528     env->tick = ptimer_init(bh);
529     ptimer_set_period(env->tick, 1ULL);
530
531     bh = qemu_bh_new(stick_irq, env);
532     env->stick = ptimer_init(bh);
533     ptimer_set_period(env->stick, 1ULL);
534
535     bh = qemu_bh_new(hstick_irq, env);
536     env->hstick = ptimer_init(bh);
537     ptimer_set_period(env->hstick, 1ULL);
538
539     reset_info = qemu_mallocz(sizeof(ResetData));
540     reset_info->env = env;
541     reset_info->reset_addr = hwdef->prom_addr + 0x40ULL;
542     qemu_register_reset(main_cpu_reset, reset_info);
543     main_cpu_reset(reset_info);
544     // Override warm reset address with cold start address
545     env->pc = hwdef->prom_addr + 0x20ULL;
546     env->npc = env->pc + 4;
547
548     return env;
549 }
550
551 static void sun4uv_init(ram_addr_t RAM_size,
552                         const char *boot_devices,
553                         const char *kernel_filename, const char *kernel_cmdline,
554                         const char *initrd_filename, const char *cpu_model,
555                         const struct hwdef *hwdef)
556 {
557     CPUState *env;
558     m48t59_t *nvram;
559     unsigned int i;
560     long initrd_size, kernel_size;
561     PCIBus *pci_bus, *pci_bus2, *pci_bus3;
562     qemu_irq *irq;
563     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
564     BlockDriverState *fd[MAX_FD];
565     void *fw_cfg;
566     DriveInfo *dinfo;
567
568     /* init CPUs */
569     env = cpu_devinit(cpu_model, hwdef);
570
571     /* set up devices */
572     ram_init(0, RAM_size);
573
574     prom_init(hwdef->prom_addr, bios_name);
575
576
577     irq = qemu_allocate_irqs(cpu_set_irq, env, MAX_PILS);
578     pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, irq, &pci_bus2,
579                            &pci_bus3);
580     isa_mem_base = VGA_BASE;
581     pci_vga_init(pci_bus, 0, 0);
582
583     // XXX Should be pci_bus3
584     pci_ebus_init(pci_bus, -1);
585
586     i = 0;
587     if (hwdef->console_serial_base) {
588         serial_mm_init(hwdef->console_serial_base, 0, NULL, 115200,
589                        serial_hds[i], 1);
590         i++;
591     }
592     for(; i < MAX_SERIAL_PORTS; i++) {
593         if (serial_hds[i]) {
594             serial_init(serial_io[i], NULL/*serial_irq[i]*/, 115200,
595                         serial_hds[i]);
596         }
597     }
598
599     for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
600         if (parallel_hds[i]) {
601             parallel_init(parallel_io[i], NULL/*parallel_irq[i]*/,
602                           parallel_hds[i]);
603         }
604     }
605
606     for(i = 0; i < nb_nics; i++)
607         pci_nic_init(&nd_table[i], "ne2k_pci", NULL);
608
609     if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
610         fprintf(stderr, "qemu: too many IDE bus\n");
611         exit(1);
612     }
613     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
614         dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS,
615                           i % MAX_IDE_DEVS);
616         hd[i] = dinfo ? dinfo->bdrv : NULL;
617     }
618
619     pci_cmd646_ide_init(pci_bus, hd, 1);
620
621     /* FIXME: wire up interrupts.  */
622     i8042_init(NULL/*1*/, NULL/*12*/, 0x60);
623     for(i = 0; i < MAX_FD; i++) {
624         dinfo = drive_get(IF_FLOPPY, 0, i);
625         fd[i] = dinfo ? dinfo->bdrv : NULL;
626     }
627     floppy_controller = fdctrl_init(NULL/*6*/, 2, 0, 0x3f0, fd);
628     nvram = m48t59_init(NULL/*8*/, 0, 0x0074, NVRAM_SIZE, 59);
629
630     initrd_size = 0;
631     kernel_size = sun4u_load_kernel(kernel_filename, initrd_filename,
632                                     ram_size, &initrd_size);
633
634     sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", RAM_size, boot_devices,
635                            KERNEL_LOAD_ADDR, kernel_size,
636                            kernel_cmdline,
637                            INITRD_LOAD_ADDR, initrd_size,
638                            /* XXX: need an option to load a NVRAM image */
639                            0,
640                            graphic_width, graphic_height, graphic_depth,
641                            (uint8_t *)&nd_table[0].macaddr);
642
643     fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
644     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
645     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
646     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
647     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, KERNEL_LOAD_ADDR);
648     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
649     if (kernel_cmdline) {
650         fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, CMDLINE_ADDR);
651         pstrcpy_targphys(CMDLINE_ADDR, TARGET_PAGE_SIZE, kernel_cmdline);
652     } else {
653         fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, 0);
654     }
655     fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, INITRD_LOAD_ADDR);
656     fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
657     fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, boot_devices[0]);
658
659     fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_WIDTH, graphic_width);
660     fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_HEIGHT, graphic_height);
661     fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_DEPTH, graphic_depth);
662
663     qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
664 }
665
666 enum {
667     sun4u_id = 0,
668     sun4v_id = 64,
669     niagara_id,
670 };
671
672 static const struct hwdef hwdefs[] = {
673     /* Sun4u generic PC-like machine */
674     {
675         .default_cpu_model = "TI UltraSparc II",
676         .machine_id = sun4u_id,
677         .prom_addr = 0x1fff0000000ULL,
678         .console_serial_base = 0,
679     },
680     /* Sun4v generic PC-like machine */
681     {
682         .default_cpu_model = "Sun UltraSparc T1",
683         .machine_id = sun4v_id,
684         .prom_addr = 0x1fff0000000ULL,
685         .console_serial_base = 0,
686     },
687     /* Sun4v generic Niagara machine */
688     {
689         .default_cpu_model = "Sun UltraSparc T1",
690         .machine_id = niagara_id,
691         .prom_addr = 0xfff0000000ULL,
692         .console_serial_base = 0xfff0c2c000ULL,
693     },
694 };
695
696 /* Sun4u hardware initialisation */
697 static void sun4u_init(ram_addr_t RAM_size,
698                        const char *boot_devices,
699                        const char *kernel_filename, const char *kernel_cmdline,
700                        const char *initrd_filename, const char *cpu_model)
701 {
702     sun4uv_init(RAM_size, boot_devices, kernel_filename,
703                 kernel_cmdline, initrd_filename, cpu_model, &hwdefs[0]);
704 }
705
706 /* Sun4v hardware initialisation */
707 static void sun4v_init(ram_addr_t RAM_size,
708                        const char *boot_devices,
709                        const char *kernel_filename, const char *kernel_cmdline,
710                        const char *initrd_filename, const char *cpu_model)
711 {
712     sun4uv_init(RAM_size, boot_devices, kernel_filename,
713                 kernel_cmdline, initrd_filename, cpu_model, &hwdefs[1]);
714 }
715
716 /* Niagara hardware initialisation */
717 static void niagara_init(ram_addr_t RAM_size,
718                          const char *boot_devices,
719                          const char *kernel_filename, const char *kernel_cmdline,
720                          const char *initrd_filename, const char *cpu_model)
721 {
722     sun4uv_init(RAM_size, boot_devices, kernel_filename,
723                 kernel_cmdline, initrd_filename, cpu_model, &hwdefs[2]);
724 }
725
726 static QEMUMachine sun4u_machine = {
727     .name = "sun4u",
728     .desc = "Sun4u platform",
729     .init = sun4u_init,
730     .max_cpus = 1, // XXX for now
731     .is_default = 1,
732 };
733
734 static QEMUMachine sun4v_machine = {
735     .name = "sun4v",
736     .desc = "Sun4v platform",
737     .init = sun4v_init,
738     .max_cpus = 1, // XXX for now
739 };
740
741 static QEMUMachine niagara_machine = {
742     .name = "Niagara",
743     .desc = "Sun4v platform, Niagara",
744     .init = niagara_init,
745     .max_cpus = 1, // XXX for now
746 };
747
748 static void sun4u_machine_init(void)
749 {
750     qemu_register_machine(&sun4u_machine);
751     qemu_register_machine(&sun4v_machine);
752     qemu_register_machine(&niagara_machine);
753 }
754
755 machine_init(sun4u_machine_init);