]> git.proxmox.com Git - mirror_qemu.git/blame - hw/sparc64/sun4u.c
target-sparc: implement sun4v RTC
[mirror_qemu.git] / hw / sparc64 / sun4u.c
CommitLineData
3475187d 1/*
c7ba218d 2 * QEMU Sun4u/Sun4v System Emulator
5fafdf24 3 *
3475187d 4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
3475187d
FB
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 */
db5ebe5f 24#include "qemu/osdep.h"
da34e65c 25#include "qapi/error.h"
4771d756
PB
26#include "qemu-common.h"
27#include "cpu.h"
83c9f4ca
PB
28#include "hw/hw.h"
29#include "hw/pci/pci.h"
0d09e41a
PB
30#include "hw/pci-host/apb.h"
31#include "hw/i386/pc.h"
32#include "hw/char/serial.h"
33#include "hw/timer/m48t59.h"
34#include "hw/block/fdc.h"
1422e32d 35#include "net/net.h"
1de7afc9 36#include "qemu/timer.h"
9c17d615 37#include "sysemu/sysemu.h"
83c9f4ca 38#include "hw/boards.h"
c6363bae 39#include "hw/nvram/sun_nvram.h"
2024c014 40#include "hw/nvram/chrp_nvram.h"
0d09e41a 41#include "hw/nvram/fw_cfg.h"
83c9f4ca
PB
42#include "hw/sysbus.h"
43#include "hw/ide.h"
44#include "hw/loader.h"
ca20cf32 45#include "elf.h"
4be74634 46#include "sysemu/block-backend.h"
022c62cb 47#include "exec/address-spaces.h"
f348b6d1 48#include "qemu/cutils.h"
3475187d 49
9d926598 50//#define DEBUG_IRQ
b430a225 51//#define DEBUG_EBUS
8f4efc55 52//#define DEBUG_TIMER
9d926598
BS
53
54#ifdef DEBUG_IRQ
b430a225 55#define CPUIRQ_DPRINTF(fmt, ...) \
001faf32 56 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
9d926598 57#else
b430a225
BS
58#define CPUIRQ_DPRINTF(fmt, ...)
59#endif
60
61#ifdef DEBUG_EBUS
62#define EBUS_DPRINTF(fmt, ...) \
63 do { printf("EBUS: " fmt , ## __VA_ARGS__); } while (0)
64#else
65#define EBUS_DPRINTF(fmt, ...)
9d926598
BS
66#endif
67
8f4efc55
IK
68#ifdef DEBUG_TIMER
69#define TIMER_DPRINTF(fmt, ...) \
70 do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0)
71#else
72#define TIMER_DPRINTF(fmt, ...)
73#endif
74
83469015
FB
75#define KERNEL_LOAD_ADDR 0x00404000
76#define CMDLINE_ADDR 0x003ff000
ac2e9d66 77#define PROM_SIZE_MAX (4 * 1024 * 1024)
f930d07e 78#define PROM_VADDR 0x000ffd00000ULL
83469015 79#define APB_SPECIAL_BASE 0x1fe00000000ULL
f930d07e 80#define APB_MEM_BASE 0x1ff00000000ULL
d63baf92 81#define APB_PCI_IO_BASE (APB_SPECIAL_BASE + 0x02000000ULL)
f930d07e 82#define PROM_FILENAME "openbios-sparc64"
83469015 83#define NVRAM_SIZE 0x2000
e4bcb14c 84#define MAX_IDE_BUS 2
3cce6243 85#define BIOS_CFG_IOPORT 0x510
7589690c
BS
86#define FW_CFG_SPARC64_WIDTH (FW_CFG_ARCH_LOCAL + 0x00)
87#define FW_CFG_SPARC64_HEIGHT (FW_CFG_ARCH_LOCAL + 0x01)
88#define FW_CFG_SPARC64_DEPTH (FW_CFG_ARCH_LOCAL + 0x02)
3475187d 89
852e82f3 90#define IVEC_MAX 0x40
9d926598 91
8fa211e8
BS
92#define TICK_MAX 0x7fffffffffffffffULL
93
c7ba218d
BS
94struct hwdef {
95 const char * const default_cpu_model;
905fdcb5 96 uint16_t machine_id;
e87231d4
BS
97 uint64_t prom_addr;
98 uint64_t console_serial_base;
c7ba218d
BS
99};
100
c5e6fb7e
AK
101typedef struct EbusState {
102 PCIDevice pci_dev;
103 MemoryRegion bar0;
104 MemoryRegion bar1;
105} EbusState;
106
57146941 107void DMA_init(ISABus *bus, int high_page_enable)
4556bd8b
BS
108{
109}
110
ddcd5531
GA
111static void fw_cfg_boot_set(void *opaque, const char *boot_device,
112 Error **errp)
81864572 113{
48779e50 114 fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
81864572
BS
115}
116
31688246 117static int sun4u_NVRAM_set_params(Nvram *nvram, uint16_t NVRAM_size,
43a34704
BS
118 const char *arch, ram_addr_t RAM_size,
119 const char *boot_devices,
120 uint32_t kernel_image, uint32_t kernel_size,
121 const char *cmdline,
122 uint32_t initrd_image, uint32_t initrd_size,
123 uint32_t NVRAM_image,
124 int width, int height, int depth,
125 const uint8_t *macaddr)
83469015 126{
66508601 127 unsigned int i;
2024c014 128 int sysp_end;
d2c63fc1 129 uint8_t image[0x1ff0];
31688246 130 NvramClass *k = NVRAM_GET_CLASS(nvram);
d2c63fc1
BS
131
132 memset(image, '\0', sizeof(image));
133
2024c014
TH
134 /* OpenBIOS nvram variables partition */
135 sysp_end = chrp_nvram_create_system_partition(image, 0);
83469015 136
2024c014
TH
137 /* Free space partition */
138 chrp_nvram_create_free_partition(&image[sysp_end], 0x1fd0 - sysp_end);
d2c63fc1 139
0d31cb99
BS
140 Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, 0x80);
141
31688246
HP
142 for (i = 0; i < sizeof(image); i++) {
143 (k->write)(nvram, i, image[i]);
144 }
66508601 145
83469015 146 return 0;
3475187d 147}
5f2bf0fe
BS
148
149static uint64_t sun4u_load_kernel(const char *kernel_filename,
150 const char *initrd_filename,
151 ram_addr_t RAM_size, uint64_t *initrd_size,
152 uint64_t *initrd_addr, uint64_t *kernel_addr,
153 uint64_t *kernel_entry)
636aa70a
BS
154{
155 int linux_boot;
156 unsigned int i;
157 long kernel_size;
6908d9ce 158 uint8_t *ptr;
5f2bf0fe 159 uint64_t kernel_top;
636aa70a
BS
160
161 linux_boot = (kernel_filename != NULL);
162
163 kernel_size = 0;
164 if (linux_boot) {
ca20cf32
BS
165 int bswap_needed;
166
167#ifdef BSWAP_NEEDED
168 bswap_needed = 1;
169#else
170 bswap_needed = 0;
171#endif
5f2bf0fe 172 kernel_size = load_elf(kernel_filename, NULL, NULL, kernel_entry,
7ef295ea 173 kernel_addr, &kernel_top, 1, EM_SPARCV9, 0, 0);
5f2bf0fe
BS
174 if (kernel_size < 0) {
175 *kernel_addr = KERNEL_LOAD_ADDR;
176 *kernel_entry = KERNEL_LOAD_ADDR;
636aa70a 177 kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
ca20cf32
BS
178 RAM_size - KERNEL_LOAD_ADDR, bswap_needed,
179 TARGET_PAGE_SIZE);
5f2bf0fe
BS
180 }
181 if (kernel_size < 0) {
636aa70a
BS
182 kernel_size = load_image_targphys(kernel_filename,
183 KERNEL_LOAD_ADDR,
184 RAM_size - KERNEL_LOAD_ADDR);
5f2bf0fe 185 }
636aa70a
BS
186 if (kernel_size < 0) {
187 fprintf(stderr, "qemu: could not load kernel '%s'\n",
188 kernel_filename);
189 exit(1);
190 }
5f2bf0fe 191 /* load initrd above kernel */
636aa70a
BS
192 *initrd_size = 0;
193 if (initrd_filename) {
5f2bf0fe
BS
194 *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);
195
636aa70a 196 *initrd_size = load_image_targphys(initrd_filename,
5f2bf0fe
BS
197 *initrd_addr,
198 RAM_size - *initrd_addr);
199 if ((int)*initrd_size < 0) {
636aa70a
BS
200 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
201 initrd_filename);
202 exit(1);
203 }
204 }
205 if (*initrd_size > 0) {
206 for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
5f2bf0fe 207 ptr = rom_ptr(*kernel_addr + i);
6908d9ce 208 if (ldl_p(ptr + 8) == 0x48647253) { /* HdrS */
5f2bf0fe 209 stl_p(ptr + 24, *initrd_addr + *kernel_addr);
6908d9ce 210 stl_p(ptr + 28, *initrd_size);
636aa70a
BS
211 break;
212 }
213 }
214 }
215 }
216 return kernel_size;
217}
3475187d 218
98cec4a2 219void cpu_check_irqs(CPUSPARCState *env)
9d926598 220{
259186a7 221 CPUState *cs;
d532b26c
IK
222 uint32_t pil = env->pil_in |
223 (env->softint & ~(SOFTINT_TIMER | SOFTINT_STIMER));
224
a7be9bad
AT
225 /* TT_IVEC has a higher priority (16) than TT_EXTINT (31..17) */
226 if (env->ivec_status & 0x20) {
227 return;
228 }
259186a7 229 cs = CPU(sparc_env_get_cpu(env));
d532b26c
IK
230 /* check if TM or SM in SOFTINT are set
231 setting these also causes interrupt 14 */
232 if (env->softint & (SOFTINT_TIMER | SOFTINT_STIMER)) {
233 pil |= 1 << 14;
234 }
235
9f94778c
AT
236 /* The bit corresponding to psrpil is (1<< psrpil), the next bit
237 is (2 << psrpil). */
238 if (pil < (2 << env->psrpil)){
259186a7 239 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
d532b26c
IK
240 CPUIRQ_DPRINTF("Reset CPU IRQ (current interrupt %x)\n",
241 env->interrupt_index);
242 env->interrupt_index = 0;
d8ed887b 243 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
d532b26c
IK
244 }
245 return;
246 }
247
248 if (cpu_interrupts_enabled(env)) {
9d926598 249
9d926598
BS
250 unsigned int i;
251
d532b26c 252 for (i = 15; i > env->psrpil; i--) {
9d926598
BS
253 if (pil & (1 << i)) {
254 int old_interrupt = env->interrupt_index;
d532b26c
IK
255 int new_interrupt = TT_EXTINT | i;
256
a7be9bad
AT
257 if (unlikely(env->tl > 0 && cpu_tsptr(env)->tt > new_interrupt
258 && ((cpu_tsptr(env)->tt & 0x1f0) == TT_EXTINT))) {
d532b26c
IK
259 CPUIRQ_DPRINTF("Not setting CPU IRQ: TL=%d "
260 "current %x >= pending %x\n",
261 env->tl, cpu_tsptr(env)->tt, new_interrupt);
262 } else if (old_interrupt != new_interrupt) {
263 env->interrupt_index = new_interrupt;
264 CPUIRQ_DPRINTF("Set CPU IRQ %d old=%x new=%x\n", i,
265 old_interrupt, new_interrupt);
c3affe56 266 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
9d926598
BS
267 }
268 break;
269 }
270 }
259186a7 271 } else if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
d532b26c
IK
272 CPUIRQ_DPRINTF("Interrupts disabled, pil=%08x pil_in=%08x softint=%08x "
273 "current interrupt %x\n",
274 pil, env->pil_in, env->softint, env->interrupt_index);
9f94778c 275 env->interrupt_index = 0;
d8ed887b 276 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
9d926598
BS
277 }
278}
279
ce18c558 280static void cpu_kick_irq(SPARCCPU *cpu)
8f4efc55 281{
259186a7 282 CPUState *cs = CPU(cpu);
ce18c558
AF
283 CPUSPARCState *env = &cpu->env;
284
259186a7 285 cs->halted = 0;
8f4efc55 286 cpu_check_irqs(env);
259186a7 287 qemu_cpu_kick(cs);
8f4efc55
IK
288}
289
361dea40 290static void cpu_set_ivec_irq(void *opaque, int irq, int level)
9d926598 291{
b64ba4b2
AF
292 SPARCCPU *cpu = opaque;
293 CPUSPARCState *env = &cpu->env;
259186a7 294 CPUState *cs;
9d926598
BS
295
296 if (level) {
23cf96e1
AT
297 if (!(env->ivec_status & 0x20)) {
298 CPUIRQ_DPRINTF("Raise IVEC IRQ %d\n", irq);
259186a7
AF
299 cs = CPU(cpu);
300 cs->halted = 0;
23cf96e1
AT
301 env->interrupt_index = TT_IVEC;
302 env->ivec_status |= 0x20;
303 env->ivec_data[0] = (0x1f << 6) | irq;
304 env->ivec_data[1] = 0;
305 env->ivec_data[2] = 0;
c3affe56 306 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
23cf96e1
AT
307 }
308 } else {
309 if (env->ivec_status & 0x20) {
310 CPUIRQ_DPRINTF("Lower IVEC IRQ %d\n", irq);
d8ed887b 311 cs = CPU(cpu);
23cf96e1 312 env->ivec_status &= ~0x20;
d8ed887b 313 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
23cf96e1 314 }
9d926598
BS
315 }
316}
317
e87231d4 318typedef struct ResetData {
403d7a2d 319 SPARCCPU *cpu;
44a99354 320 uint64_t prom_addr;
e87231d4
BS
321} ResetData;
322
6b678e1f 323static CPUTimer *cpu_timer_create(const char *name, SPARCCPU *cpu,
8f4efc55 324 QEMUBHFunc *cb, uint32_t frequency,
e913cac7 325 uint64_t disabled_mask, uint64_t npt_mask)
8f4efc55 326{
7267c094 327 CPUTimer *timer = g_malloc0(sizeof (CPUTimer));
8f4efc55
IK
328
329 timer->name = name;
330 timer->frequency = frequency;
331 timer->disabled_mask = disabled_mask;
e913cac7 332 timer->npt_mask = npt_mask;
8f4efc55
IK
333
334 timer->disabled = 1;
e913cac7 335 timer->npt = 1;
bc72ad67 336 timer->clock_offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8f4efc55 337
bc72ad67 338 timer->qtimer = timer_new_ns(QEMU_CLOCK_VIRTUAL, cb, cpu);
8f4efc55
IK
339
340 return timer;
341}
342
343static void cpu_timer_reset(CPUTimer *timer)
344{
345 timer->disabled = 1;
bc72ad67 346 timer->clock_offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8f4efc55 347
bc72ad67 348 timer_del(timer->qtimer);
8f4efc55
IK
349}
350
c68ea704
FB
351static void main_cpu_reset(void *opaque)
352{
e87231d4 353 ResetData *s = (ResetData *)opaque;
403d7a2d 354 CPUSPARCState *env = &s->cpu->env;
44a99354 355 static unsigned int nr_resets;
20c9f095 356
403d7a2d 357 cpu_reset(CPU(s->cpu));
8f4efc55
IK
358
359 cpu_timer_reset(env->tick);
360 cpu_timer_reset(env->stick);
361 cpu_timer_reset(env->hstick);
362
e87231d4
BS
363 env->gregs[1] = 0; // Memory start
364 env->gregs[2] = ram_size; // Memory size
365 env->gregs[3] = 0; // Machine description XXX
44a99354
BS
366 if (nr_resets++ == 0) {
367 /* Power on reset */
368 env->pc = s->prom_addr + 0x20ULL;
369 } else {
370 env->pc = s->prom_addr + 0x40ULL;
371 }
e87231d4 372 env->npc = env->pc + 4;
20c9f095
BS
373}
374
22548760 375static void tick_irq(void *opaque)
20c9f095 376{
6b678e1f
AF
377 SPARCCPU *cpu = opaque;
378 CPUSPARCState *env = &cpu->env;
20c9f095 379
8f4efc55
IK
380 CPUTimer* timer = env->tick;
381
382 if (timer->disabled) {
383 CPUIRQ_DPRINTF("tick_irq: softint disabled\n");
384 return;
385 } else {
386 CPUIRQ_DPRINTF("tick: fire\n");
8fa211e8 387 }
8f4efc55
IK
388
389 env->softint |= SOFTINT_TIMER;
ce18c558 390 cpu_kick_irq(cpu);
20c9f095
BS
391}
392
22548760 393static void stick_irq(void *opaque)
20c9f095 394{
6b678e1f
AF
395 SPARCCPU *cpu = opaque;
396 CPUSPARCState *env = &cpu->env;
20c9f095 397
8f4efc55
IK
398 CPUTimer* timer = env->stick;
399
400 if (timer->disabled) {
401 CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
402 return;
403 } else {
404 CPUIRQ_DPRINTF("stick: fire\n");
8fa211e8 405 }
8f4efc55
IK
406
407 env->softint |= SOFTINT_STIMER;
ce18c558 408 cpu_kick_irq(cpu);
20c9f095
BS
409}
410
22548760 411static void hstick_irq(void *opaque)
20c9f095 412{
6b678e1f
AF
413 SPARCCPU *cpu = opaque;
414 CPUSPARCState *env = &cpu->env;
20c9f095 415
8f4efc55
IK
416 CPUTimer* timer = env->hstick;
417
418 if (timer->disabled) {
419 CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
420 return;
421 } else {
422 CPUIRQ_DPRINTF("hstick: fire\n");
8fa211e8 423 }
8f4efc55
IK
424
425 env->softint |= SOFTINT_STIMER;
ce18c558 426 cpu_kick_irq(cpu);
8f4efc55
IK
427}
428
429static int64_t cpu_to_timer_ticks(int64_t cpu_ticks, uint32_t frequency)
430{
73bcb24d 431 return muldiv64(cpu_ticks, NANOSECONDS_PER_SECOND, frequency);
8f4efc55
IK
432}
433
434static uint64_t timer_to_cpu_ticks(int64_t timer_ticks, uint32_t frequency)
435{
73bcb24d 436 return muldiv64(timer_ticks, frequency, NANOSECONDS_PER_SECOND);
c68ea704
FB
437}
438
8f4efc55 439void cpu_tick_set_count(CPUTimer *timer, uint64_t count)
f4b1a842 440{
bf43330a
MCA
441 uint64_t real_count = count & ~timer->npt_mask;
442 uint64_t npt_bit = count & timer->npt_mask;
8f4efc55 443
bc72ad67 444 int64_t vm_clock_offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) -
8f4efc55
IK
445 cpu_to_timer_ticks(real_count, timer->frequency);
446
bf43330a 447 TIMER_DPRINTF("%s set_count count=0x%016lx (npt %s) p=%p\n",
8f4efc55 448 timer->name, real_count,
bf43330a 449 timer->npt ? "disabled" : "enabled", timer);
8f4efc55 450
bf43330a 451 timer->npt = npt_bit ? 1 : 0;
8f4efc55 452 timer->clock_offset = vm_clock_offset;
f4b1a842
BS
453}
454
8f4efc55 455uint64_t cpu_tick_get_count(CPUTimer *timer)
f4b1a842 456{
8f4efc55 457 uint64_t real_count = timer_to_cpu_ticks(
bc72ad67 458 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - timer->clock_offset,
8f4efc55
IK
459 timer->frequency);
460
bf43330a 461 TIMER_DPRINTF("%s get_count count=0x%016lx (npt %s) p=%p\n",
8f4efc55 462 timer->name, real_count,
bf43330a 463 timer->npt ? "disabled" : "enabled", timer);
8f4efc55 464
bf43330a
MCA
465 if (timer->npt) {
466 real_count |= timer->npt_mask;
467 }
8f4efc55
IK
468
469 return real_count;
f4b1a842
BS
470}
471
8f4efc55 472void cpu_tick_set_limit(CPUTimer *timer, uint64_t limit)
f4b1a842 473{
bc72ad67 474 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8f4efc55
IK
475
476 uint64_t real_limit = limit & ~timer->disabled_mask;
477 timer->disabled = (limit & timer->disabled_mask) ? 1 : 0;
478
479 int64_t expires = cpu_to_timer_ticks(real_limit, timer->frequency) +
480 timer->clock_offset;
481
482 if (expires < now) {
483 expires = now + 1;
484 }
485
486 TIMER_DPRINTF("%s set_limit limit=0x%016lx (%s) p=%p "
487 "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
488 timer->name, real_limit,
489 timer->disabled?"disabled":"enabled",
490 timer, limit,
491 timer_to_cpu_ticks(now - timer->clock_offset,
492 timer->frequency),
493 timer_to_cpu_ticks(expires - now, timer->frequency));
494
495 if (!real_limit) {
496 TIMER_DPRINTF("%s set_limit limit=ZERO - not starting timer\n",
497 timer->name);
bc72ad67 498 timer_del(timer->qtimer);
8f4efc55 499 } else if (timer->disabled) {
bc72ad67 500 timer_del(timer->qtimer);
8f4efc55 501 } else {
bc72ad67 502 timer_mod(timer->qtimer, expires);
8f4efc55 503 }
f4b1a842
BS
504}
505
361dea40 506static void isa_irq_handler(void *opaque, int n, int level)
1387fe4a 507{
361dea40
BS
508 static const int isa_irq_to_ivec[16] = {
509 [1] = 0x29, /* keyboard */
510 [4] = 0x2b, /* serial */
511 [6] = 0x27, /* floppy */
512 [7] = 0x22, /* parallel */
513 [12] = 0x2a, /* mouse */
514 };
515 qemu_irq *irqs = opaque;
516 int ivec;
517
518 assert(n < 16);
519 ivec = isa_irq_to_ivec[n];
520 EBUS_DPRINTF("Set ISA IRQ %d level %d -> ivec 0x%x\n", n, level, ivec);
521 if (ivec) {
522 qemu_set_irq(irqs[ivec], level);
523 }
1387fe4a
BS
524}
525
c190ea07 526/* EBUS (Eight bit bus) bridge */
48a18b3c 527static ISABus *
361dea40 528pci_ebus_init(PCIBus *bus, int devfn, qemu_irq *irqs)
c190ea07 529{
1387fe4a 530 qemu_irq *isa_irq;
ab953e28 531 PCIDevice *pci_dev;
48a18b3c 532 ISABus *isa_bus;
1387fe4a 533
ab953e28 534 pci_dev = pci_create_simple(bus, devfn, "ebus");
2ae0e48d 535 isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci_dev), "isa.0"));
361dea40 536 isa_irq = qemu_allocate_irqs(isa_irq_handler, irqs, 16);
48a18b3c
HP
537 isa_bus_irqs(isa_bus, isa_irq);
538 return isa_bus;
53e3c4f9 539}
c190ea07 540
3a80cead 541static void pci_ebus_realize(PCIDevice *pci_dev, Error **errp)
53e3c4f9 542{
c5e6fb7e
AK
543 EbusState *s = DO_UPCAST(EbusState, pci_dev, pci_dev);
544
d10e5432
MA
545 if (!isa_bus_new(DEVICE(pci_dev), get_system_memory(),
546 pci_address_space_io(pci_dev), errp)) {
547 return;
548 }
c5e6fb7e
AK
549
550 pci_dev->config[0x04] = 0x06; // command = bus master, pci mem
551 pci_dev->config[0x05] = 0x00;
552 pci_dev->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
553 pci_dev->config[0x07] = 0x03; // status = medium devsel
554 pci_dev->config[0x09] = 0x00; // programming i/f
555 pci_dev->config[0x0D] = 0x0a; // latency_timer
556
0a70e094
PB
557 memory_region_init_alias(&s->bar0, OBJECT(s), "bar0", get_system_io(),
558 0, 0x1000000);
e824b2cc 559 pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0);
0a70e094 560 memory_region_init_alias(&s->bar1, OBJECT(s), "bar1", get_system_io(),
f3b18f35 561 0, 0x4000);
a1cf8be5 562 pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->bar1);
c190ea07
BS
563}
564
40021f08
AL
565static void ebus_class_init(ObjectClass *klass, void *data)
566{
567 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
568
3a80cead 569 k->realize = pci_ebus_realize;
40021f08
AL
570 k->vendor_id = PCI_VENDOR_ID_SUN;
571 k->device_id = PCI_DEVICE_ID_SUN_EBUS;
572 k->revision = 0x01;
573 k->class_id = PCI_CLASS_BRIDGE_OTHER;
574}
575
8c43a6f0 576static const TypeInfo ebus_info = {
39bffca2
AL
577 .name = "ebus",
578 .parent = TYPE_PCI_DEVICE,
579 .instance_size = sizeof(EbusState),
580 .class_init = ebus_class_init,
53e3c4f9
BS
581};
582
13575cf6
AF
583#define TYPE_OPENPROM "openprom"
584#define OPENPROM(obj) OBJECT_CHECK(PROMState, (obj), TYPE_OPENPROM)
585
d4edce38 586typedef struct PROMState {
13575cf6
AF
587 SysBusDevice parent_obj;
588
d4edce38
AK
589 MemoryRegion prom;
590} PROMState;
591
409dbce5
AJ
592static uint64_t translate_prom_address(void *opaque, uint64_t addr)
593{
a8170e5e 594 hwaddr *base_addr = (hwaddr *)opaque;
409dbce5
AJ
595 return addr + *base_addr - PROM_VADDR;
596}
597
1baffa46 598/* Boot PROM (OpenBIOS) */
a8170e5e 599static void prom_init(hwaddr addr, const char *bios_name)
1baffa46
BS
600{
601 DeviceState *dev;
602 SysBusDevice *s;
603 char *filename;
604 int ret;
605
13575cf6 606 dev = qdev_create(NULL, TYPE_OPENPROM);
e23a1b33 607 qdev_init_nofail(dev);
1356b98d 608 s = SYS_BUS_DEVICE(dev);
1baffa46
BS
609
610 sysbus_mmio_map(s, 0, addr);
611
612 /* load boot prom */
613 if (bios_name == NULL) {
614 bios_name = PROM_FILENAME;
615 }
616 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
617 if (filename) {
409dbce5 618 ret = load_elf(filename, translate_prom_address, &addr,
7ef295ea 619 NULL, NULL, NULL, 1, EM_SPARCV9, 0, 0);
1baffa46
BS
620 if (ret < 0 || ret > PROM_SIZE_MAX) {
621 ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
622 }
7267c094 623 g_free(filename);
1baffa46
BS
624 } else {
625 ret = -1;
626 }
627 if (ret < 0 || ret > PROM_SIZE_MAX) {
628 fprintf(stderr, "qemu: could not load prom '%s'\n", bios_name);
629 exit(1);
630 }
631}
632
81a322d4 633static int prom_init1(SysBusDevice *dev)
1baffa46 634{
13575cf6 635 PROMState *s = OPENPROM(dev);
1baffa46 636
49946538 637 memory_region_init_ram(&s->prom, OBJECT(s), "sun4u.prom", PROM_SIZE_MAX,
f8ed85ac 638 &error_fatal);
c5705a77 639 vmstate_register_ram_global(&s->prom);
d4edce38 640 memory_region_set_readonly(&s->prom, true);
750ecd44 641 sysbus_init_mmio(dev, &s->prom);
81a322d4 642 return 0;
1baffa46
BS
643}
644
999e12bb
AL
645static Property prom_properties[] = {
646 {/* end of property list */},
647};
648
649static void prom_class_init(ObjectClass *klass, void *data)
650{
39bffca2 651 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
652 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
653
654 k->init = prom_init1;
39bffca2 655 dc->props = prom_properties;
999e12bb
AL
656}
657
8c43a6f0 658static const TypeInfo prom_info = {
13575cf6 659 .name = TYPE_OPENPROM,
39bffca2
AL
660 .parent = TYPE_SYS_BUS_DEVICE,
661 .instance_size = sizeof(PROMState),
662 .class_init = prom_class_init,
1baffa46
BS
663};
664
bda42033 665
88c034d5
AF
666#define TYPE_SUN4U_MEMORY "memory"
667#define SUN4U_RAM(obj) OBJECT_CHECK(RamDevice, (obj), TYPE_SUN4U_MEMORY)
668
669typedef struct RamDevice {
670 SysBusDevice parent_obj;
671
d4edce38 672 MemoryRegion ram;
04843626 673 uint64_t size;
bda42033
BS
674} RamDevice;
675
676/* System RAM */
81a322d4 677static int ram_init1(SysBusDevice *dev)
bda42033 678{
88c034d5 679 RamDevice *d = SUN4U_RAM(dev);
bda42033 680
49946538 681 memory_region_init_ram(&d->ram, OBJECT(d), "sun4u.ram", d->size,
f8ed85ac 682 &error_fatal);
c5705a77 683 vmstate_register_ram_global(&d->ram);
750ecd44 684 sysbus_init_mmio(dev, &d->ram);
81a322d4 685 return 0;
bda42033
BS
686}
687
a8170e5e 688static void ram_init(hwaddr addr, ram_addr_t RAM_size)
bda42033
BS
689{
690 DeviceState *dev;
691 SysBusDevice *s;
692 RamDevice *d;
693
694 /* allocate RAM */
88c034d5 695 dev = qdev_create(NULL, TYPE_SUN4U_MEMORY);
1356b98d 696 s = SYS_BUS_DEVICE(dev);
bda42033 697
88c034d5 698 d = SUN4U_RAM(dev);
bda42033 699 d->size = RAM_size;
e23a1b33 700 qdev_init_nofail(dev);
bda42033
BS
701
702 sysbus_mmio_map(s, 0, addr);
703}
704
999e12bb
AL
705static Property ram_properties[] = {
706 DEFINE_PROP_UINT64("size", RamDevice, size, 0),
707 DEFINE_PROP_END_OF_LIST(),
708};
709
710static void ram_class_init(ObjectClass *klass, void *data)
711{
39bffca2 712 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
713 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
714
715 k->init = ram_init1;
39bffca2 716 dc->props = ram_properties;
999e12bb
AL
717}
718
8c43a6f0 719static const TypeInfo ram_info = {
88c034d5 720 .name = TYPE_SUN4U_MEMORY,
39bffca2
AL
721 .parent = TYPE_SYS_BUS_DEVICE,
722 .instance_size = sizeof(RamDevice),
723 .class_init = ram_class_init,
bda42033
BS
724};
725
f9d1465f 726static SPARCCPU *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
3475187d 727{
8ebdf9dc 728 SPARCCPU *cpu;
98cec4a2 729 CPUSPARCState *env;
e87231d4 730 ResetData *reset_info;
3475187d 731
8f4efc55
IK
732 uint32_t tick_frequency = 100*1000000;
733 uint32_t stick_frequency = 100*1000000;
734 uint32_t hstick_frequency = 100*1000000;
735
8ebdf9dc 736 if (cpu_model == NULL) {
c7ba218d 737 cpu_model = hwdef->default_cpu_model;
8ebdf9dc
AF
738 }
739 cpu = cpu_sparc_init(cpu_model);
740 if (cpu == NULL) {
62724a37
BS
741 fprintf(stderr, "Unable to find Sparc CPU definition\n");
742 exit(1);
743 }
8ebdf9dc 744 env = &cpu->env;
20c9f095 745
6b678e1f 746 env->tick = cpu_timer_create("tick", cpu, tick_irq,
e913cac7
MCA
747 tick_frequency, TICK_INT_DIS,
748 TICK_NPT_MASK);
8f4efc55 749
6b678e1f 750 env->stick = cpu_timer_create("stick", cpu, stick_irq,
e913cac7
MCA
751 stick_frequency, TICK_INT_DIS,
752 TICK_NPT_MASK);
20c9f095 753
6b678e1f 754 env->hstick = cpu_timer_create("hstick", cpu, hstick_irq,
e913cac7
MCA
755 hstick_frequency, TICK_INT_DIS,
756 TICK_NPT_MASK);
e87231d4 757
7267c094 758 reset_info = g_malloc0(sizeof(ResetData));
403d7a2d 759 reset_info->cpu = cpu;
44a99354 760 reset_info->prom_addr = hwdef->prom_addr;
a08d4367 761 qemu_register_reset(main_cpu_reset, reset_info);
c68ea704 762
f9d1465f 763 return cpu;
7b833f5b
BS
764}
765
38bc50f7 766static void sun4uv_init(MemoryRegion *address_space_mem,
3ef96221 767 MachineState *machine,
7b833f5b
BS
768 const struct hwdef *hwdef)
769{
f9d1465f 770 SPARCCPU *cpu;
31688246 771 Nvram *nvram;
7b833f5b 772 unsigned int i;
5f2bf0fe 773 uint64_t initrd_addr, initrd_size, kernel_addr, kernel_size, kernel_entry;
7b833f5b 774 PCIBus *pci_bus, *pci_bus2, *pci_bus3;
48a18b3c 775 ISABus *isa_bus;
f3b18f35 776 SysBusDevice *s;
361dea40 777 qemu_irq *ivec_irqs, *pbm_irqs;
f455e98c 778 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
fd8014e1 779 DriveInfo *fd[MAX_FD];
c3ae40e1 780 DeviceState *dev;
a88b362c 781 FWCfgState *fw_cfg;
7b833f5b 782
7b833f5b 783 /* init CPUs */
3ef96221 784 cpu = cpu_devinit(machine->cpu_model, hwdef);
7b833f5b 785
bda42033 786 /* set up devices */
3ef96221 787 ram_init(0, machine->ram_size);
3475187d 788
1baffa46 789 prom_init(hwdef->prom_addr, bios_name);
3475187d 790
b64ba4b2 791 ivec_irqs = qemu_allocate_irqs(cpu_set_ivec_irq, cpu, IVEC_MAX);
361dea40
BS
792 pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, ivec_irqs, &pci_bus2,
793 &pci_bus3, &pbm_irqs);
f2898771 794 pci_vga_init(pci_bus);
83469015 795
c190ea07 796 // XXX Should be pci_bus3
361dea40 797 isa_bus = pci_ebus_init(pci_bus, -1, pbm_irqs);
c190ea07 798
e87231d4
BS
799 i = 0;
800 if (hwdef->console_serial_base) {
38bc50f7 801 serial_mm_init(address_space_mem, hwdef->console_serial_base, 0,
39186d8a 802 NULL, 115200, serial_hds[i], DEVICE_BIG_ENDIAN);
e87231d4
BS
803 i++;
804 }
83469015 805
4496dc49 806 serial_hds_isa_init(isa_bus, i, MAX_SERIAL_PORTS);
07dc7880 807 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
83469015 808
cb457d76 809 for(i = 0; i < nb_nics; i++)
29b358f9 810 pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
83469015 811
d8f94e1b 812 ide_drive_get(hd, ARRAY_SIZE(hd));
e4bcb14c 813
3b898dda
BS
814 pci_cmd646_ide_init(pci_bus, hd, 1);
815
48a18b3c 816 isa_create_simple(isa_bus, "i8042");
c3ae40e1
HP
817
818 /* Floppy */
e4bcb14c 819 for(i = 0; i < MAX_FD; i++) {
fd8014e1 820 fd[i] = drive_get(IF_FLOPPY, 0, i);
e4bcb14c 821 }
c3ae40e1
HP
822 dev = DEVICE(isa_create(isa_bus, TYPE_ISA_FDC));
823 if (fd[0]) {
824 qdev_prop_set_drive(dev, "driveA", blk_by_legacy_dinfo(fd[0]),
825 &error_abort);
826 }
827 if (fd[1]) {
828 qdev_prop_set_drive(dev, "driveB", blk_by_legacy_dinfo(fd[1]),
829 &error_abort);
830 }
831 qdev_prop_set_uint32(dev, "dma", -1);
832 qdev_init_nofail(dev);
636aa70a 833
f3b18f35
MCA
834 /* Map NVRAM into I/O (ebus) space */
835 nvram = m48t59_init(NULL, 0, 0, NVRAM_SIZE, 1968, 59);
836 s = SYS_BUS_DEVICE(nvram);
837 memory_region_add_subregion(get_system_io(), 0x2000,
838 sysbus_mmio_get_region(s, 0));
839
636aa70a 840 initrd_size = 0;
5f2bf0fe 841 initrd_addr = 0;
3ef96221
MA
842 kernel_size = sun4u_load_kernel(machine->kernel_filename,
843 machine->initrd_filename,
5f2bf0fe
BS
844 ram_size, &initrd_size, &initrd_addr,
845 &kernel_addr, &kernel_entry);
636aa70a 846
3ef96221
MA
847 sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", machine->ram_size,
848 machine->boot_order,
5f2bf0fe 849 kernel_addr, kernel_size,
3ef96221 850 machine->kernel_cmdline,
5f2bf0fe 851 initrd_addr, initrd_size,
0d31cb99
BS
852 /* XXX: need an option to load a NVRAM image */
853 0,
854 graphic_width, graphic_height, graphic_depth,
855 (uint8_t *)&nd_table[0].macaddr);
83469015 856
66708822 857 fw_cfg = fw_cfg_init_io(BIOS_CFG_IOPORT);
5836d168 858 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
70db9222 859 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
905fdcb5
BS
860 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
861 fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
5f2bf0fe
BS
862 fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_entry);
863 fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
3ef96221 864 if (machine->kernel_cmdline) {
9c9b0512 865 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
3ef96221
MA
866 strlen(machine->kernel_cmdline) + 1);
867 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, machine->kernel_cmdline);
513f789f 868 } else {
9c9b0512 869 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 0);
513f789f 870 }
5f2bf0fe
BS
871 fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
872 fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
3ef96221 873 fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, machine->boot_order[0]);
7589690c
BS
874
875 fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_WIDTH, graphic_width);
876 fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_HEIGHT, graphic_height);
877 fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_DEPTH, graphic_depth);
878
513f789f 879 qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
3475187d
FB
880}
881
905fdcb5
BS
882enum {
883 sun4u_id = 0,
884 sun4v_id = 64,
e87231d4 885 niagara_id,
905fdcb5
BS
886};
887
c7ba218d
BS
888static const struct hwdef hwdefs[] = {
889 /* Sun4u generic PC-like machine */
890 {
5910b047 891 .default_cpu_model = "TI UltraSparc IIi",
905fdcb5 892 .machine_id = sun4u_id,
e87231d4
BS
893 .prom_addr = 0x1fff0000000ULL,
894 .console_serial_base = 0,
c7ba218d
BS
895 },
896 /* Sun4v generic PC-like machine */
897 {
898 .default_cpu_model = "Sun UltraSparc T1",
905fdcb5 899 .machine_id = sun4v_id,
e87231d4
BS
900 .prom_addr = 0x1fff0000000ULL,
901 .console_serial_base = 0,
902 },
903 /* Sun4v generic Niagara machine */
904 {
905 .default_cpu_model = "Sun UltraSparc T1",
906 .machine_id = niagara_id,
907 .prom_addr = 0xfff0000000ULL,
908 .console_serial_base = 0xfff0c2c000ULL,
c7ba218d
BS
909 },
910};
911
912/* Sun4u hardware initialisation */
3ef96221 913static void sun4u_init(MachineState *machine)
5f072e1f 914{
3ef96221 915 sun4uv_init(get_system_memory(), machine, &hwdefs[0]);
c7ba218d
BS
916}
917
918/* Sun4v hardware initialisation */
3ef96221 919static void sun4v_init(MachineState *machine)
5f072e1f 920{
3ef96221 921 sun4uv_init(get_system_memory(), machine, &hwdefs[1]);
c7ba218d
BS
922}
923
e87231d4 924/* Niagara hardware initialisation */
3ef96221 925static void niagara_init(MachineState *machine)
5f072e1f 926{
3ef96221 927 sun4uv_init(get_system_memory(), machine, &hwdefs[2]);
e87231d4
BS
928}
929
8a661aea 930static void sun4u_class_init(ObjectClass *oc, void *data)
e264d29d 931{
8a661aea
AF
932 MachineClass *mc = MACHINE_CLASS(oc);
933
e264d29d
EH
934 mc->desc = "Sun4u platform";
935 mc->init = sun4u_init;
936 mc->max_cpus = 1; /* XXX for now */
937 mc->is_default = 1;
938 mc->default_boot_order = "c";
939}
c7ba218d 940
8a661aea
AF
941static const TypeInfo sun4u_type = {
942 .name = MACHINE_TYPE_NAME("sun4u"),
943 .parent = TYPE_MACHINE,
944 .class_init = sun4u_class_init,
945};
e87231d4 946
8a661aea 947static void sun4v_class_init(ObjectClass *oc, void *data)
e264d29d 948{
8a661aea
AF
949 MachineClass *mc = MACHINE_CLASS(oc);
950
e264d29d
EH
951 mc->desc = "Sun4v platform";
952 mc->init = sun4v_init;
953 mc->max_cpus = 1; /* XXX for now */
954 mc->default_boot_order = "c";
955}
956
8a661aea
AF
957static const TypeInfo sun4v_type = {
958 .name = MACHINE_TYPE_NAME("sun4v"),
959 .parent = TYPE_MACHINE,
960 .class_init = sun4v_class_init,
961};
e264d29d 962
8a661aea 963static void niagara_class_init(ObjectClass *oc, void *data)
e264d29d 964{
8a661aea
AF
965 MachineClass *mc = MACHINE_CLASS(oc);
966
e264d29d
EH
967 mc->desc = "Sun4v platform, Niagara";
968 mc->init = niagara_init;
969 mc->max_cpus = 1; /* XXX for now */
970 mc->default_boot_order = "c";
971}
972
8a661aea
AF
973static const TypeInfo niagara_type = {
974 .name = MACHINE_TYPE_NAME("Niagara"),
975 .parent = TYPE_MACHINE,
976 .class_init = niagara_class_init,
977};
f80f9ec9 978
83f7d43a
AF
979static void sun4u_register_types(void)
980{
981 type_register_static(&ebus_info);
982 type_register_static(&prom_info);
983 type_register_static(&ram_info);
83f7d43a 984
8a661aea
AF
985 type_register_static(&sun4u_type);
986 type_register_static(&sun4v_type);
987 type_register_static(&niagara_type);
988}
989
83f7d43a 990type_init(sun4u_register_types)