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