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