sun4u.c 30.5 KB
Newer Older
bellard's avatar
bellard committed
1
/*
2
 * QEMU Sun4u/Sun4v System Emulator
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2005 Fabrice Bellard
5
 *
bellard's avatar
bellard committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
#include "hw/hw.h"
#include "hw/pci/pci.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
26 27 28 29 30
#include "hw/pci-host/apb.h"
#include "hw/i386/pc.h"
#include "hw/char/serial.h"
#include "hw/timer/m48t59.h"
#include "hw/block/fdc.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
31
#include "net/net.h"
32
#include "qemu/timer.h"
33
#include "sysemu/sysemu.h"
34
#include "hw/boards.h"
35
#include "hw/nvram/openbios_firmware_abi.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
36
#include "hw/nvram/fw_cfg.h"
37 38 39
#include "hw/sysbus.h"
#include "hw/ide.h"
#include "hw/loader.h"
Blue Swirl's avatar
Blue Swirl committed
40
#include "elf.h"
41
#include "sysemu/blockdev.h"
42
#include "exec/address-spaces.h"
bellard's avatar
bellard committed
43

44
//#define DEBUG_IRQ
45
//#define DEBUG_EBUS
46
//#define DEBUG_TIMER
47 48

#ifdef DEBUG_IRQ
49
#define CPUIRQ_DPRINTF(fmt, ...)                                \
50
    do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
51
#else
52 53 54 55 56 57 58 59
#define CPUIRQ_DPRINTF(fmt, ...)
#endif

#ifdef DEBUG_EBUS
#define EBUS_DPRINTF(fmt, ...)                                  \
    do { printf("EBUS: " fmt , ## __VA_ARGS__); } while (0)
#else
#define EBUS_DPRINTF(fmt, ...)
60 61
#endif

62 63 64 65 66 67 68
#ifdef DEBUG_TIMER
#define TIMER_DPRINTF(fmt, ...)                                  \
    do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0)
#else
#define TIMER_DPRINTF(fmt, ...)
#endif

bellard's avatar
bellard committed
69 70
#define KERNEL_LOAD_ADDR     0x00404000
#define CMDLINE_ADDR         0x003ff000
71
#define PROM_SIZE_MAX        (4 * 1024 * 1024)
blueswir1's avatar
blueswir1 committed
72
#define PROM_VADDR           0x000ffd00000ULL
bellard's avatar
bellard committed
73
#define APB_SPECIAL_BASE     0x1fe00000000ULL
blueswir1's avatar
blueswir1 committed
74
#define APB_MEM_BASE         0x1ff00000000ULL
75
#define APB_PCI_IO_BASE      (APB_SPECIAL_BASE + 0x02000000ULL)
blueswir1's avatar
blueswir1 committed
76
#define PROM_FILENAME        "openbios-sparc64"
bellard's avatar
bellard committed
77
#define NVRAM_SIZE           0x2000
ths's avatar
ths committed
78
#define MAX_IDE_BUS          2
79
#define BIOS_CFG_IOPORT      0x510
80 81 82
#define FW_CFG_SPARC64_WIDTH (FW_CFG_ARCH_LOCAL + 0x00)
#define FW_CFG_SPARC64_HEIGHT (FW_CFG_ARCH_LOCAL + 0x01)
#define FW_CFG_SPARC64_DEPTH (FW_CFG_ARCH_LOCAL + 0x02)
bellard's avatar
bellard committed
83

Artyom Tarasenko's avatar
Artyom Tarasenko committed
84
#define IVEC_MAX             0x40
85

86 87
#define TICK_MAX             0x7fffffffffffffffULL

88 89
struct hwdef {
    const char * const default_cpu_model;
90
    uint16_t machine_id;
blueswir1's avatar
blueswir1 committed
91 92
    uint64_t prom_addr;
    uint64_t console_serial_base;
93 94
};

Avi Kivity's avatar
Avi Kivity committed
95 96 97 98 99 100
typedef struct EbusState {
    PCIDevice pci_dev;
    MemoryRegion bar0;
    MemoryRegion bar1;
} EbusState;

bellard's avatar
bellard committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
int DMA_get_channel_mode (int nchan)
{
    return 0;
}
int DMA_read_memory (int nchan, void *buf, int pos, int size)
{
    return 0;
}
int DMA_write_memory (int nchan, void *buf, int pos, int size)
{
    return 0;
}
void DMA_hold_DREQ (int nchan) {}
void DMA_release_DREQ (int nchan) {}
void DMA_schedule(int nchan) {}
Blue Swirl's avatar
Blue Swirl committed
116 117 118 119 120

void DMA_init(int high_page_enable, qemu_irq *cpu_request_exit)
{
}

bellard's avatar
bellard committed
121 122 123 124 125 126
void DMA_register_channel (int nchan,
                           DMA_transfer_handler transfer_handler,
                           void *opaque)
{
}

127
static int fw_cfg_boot_set(void *opaque, const char *boot_device)
blueswir1's avatar
blueswir1 committed
128
{
129
    fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
blueswir1's avatar
blueswir1 committed
130 131 132
    return 0;
}

133 134 135 136 137 138 139 140 141
static int sun4u_NVRAM_set_params(M48t59State *nvram, uint16_t NVRAM_size,
                                  const char *arch, ram_addr_t RAM_size,
                                  const char *boot_devices,
                                  uint32_t kernel_image, uint32_t kernel_size,
                                  const char *cmdline,
                                  uint32_t initrd_image, uint32_t initrd_size,
                                  uint32_t NVRAM_image,
                                  int width, int height, int depth,
                                  const uint8_t *macaddr)
bellard's avatar
bellard committed
142
{
blueswir1's avatar
blueswir1 committed
143 144
    unsigned int i;
    uint32_t start, end;
blueswir1's avatar
blueswir1 committed
145 146 147 148 149
    uint8_t image[0x1ff0];
    struct OpenBIOS_nvpart_v1 *part_header;

    memset(image, '\0', sizeof(image));

150
    start = 0;
bellard's avatar
bellard committed
151

blueswir1's avatar
blueswir1 committed
152 153
    // OpenBIOS nvram variables
    // Variable partition
blueswir1's avatar
blueswir1 committed
154 155
    part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
    part_header->signature = OPENBIOS_PART_SYSTEM;
blueswir1's avatar
blueswir1 committed
156
    pstrcpy(part_header->name, sizeof(part_header->name), "system");
blueswir1's avatar
blueswir1 committed
157

blueswir1's avatar
blueswir1 committed
158
    end = start + sizeof(struct OpenBIOS_nvpart_v1);
blueswir1's avatar
blueswir1 committed
159
    for (i = 0; i < nb_prom_envs; i++)
blueswir1's avatar
blueswir1 committed
160 161 162 163
        end = OpenBIOS_set_var(image, end, prom_envs[i]);

    // End marker
    image[end++] = '\0';
blueswir1's avatar
blueswir1 committed
164 165

    end = start + ((end - start + 15) & ~15);
blueswir1's avatar
blueswir1 committed
166
    OpenBIOS_finish_partition(part_header, end - start);
blueswir1's avatar
blueswir1 committed
167 168 169

    // free partition
    start = end;
blueswir1's avatar
blueswir1 committed
170 171
    part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
    part_header->signature = OPENBIOS_PART_FREE;
blueswir1's avatar
blueswir1 committed
172
    pstrcpy(part_header->name, sizeof(part_header->name), "free");
blueswir1's avatar
blueswir1 committed
173 174

    end = 0x1fd0;
blueswir1's avatar
blueswir1 committed
175 176
    OpenBIOS_finish_partition(part_header, end - start);

blueswir1's avatar
blueswir1 committed
177 178
    Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, 0x80);

blueswir1's avatar
blueswir1 committed
179 180
    for (i = 0; i < sizeof(image); i++)
        m48t59_write(nvram, i, image[i]);
blueswir1's avatar
blueswir1 committed
181

bellard's avatar
bellard committed
182
    return 0;
bellard's avatar
bellard committed
183
}
Blue Swirl's avatar
Blue Swirl committed
184 185 186 187 188 189

static uint64_t sun4u_load_kernel(const char *kernel_filename,
                                  const char *initrd_filename,
                                  ram_addr_t RAM_size, uint64_t *initrd_size,
                                  uint64_t *initrd_addr, uint64_t *kernel_addr,
                                  uint64_t *kernel_entry)
Blue Swirl's avatar
Blue Swirl committed
190 191 192 193
{
    int linux_boot;
    unsigned int i;
    long kernel_size;
Blue Swirl's avatar
Blue Swirl committed
194
    uint8_t *ptr;
Blue Swirl's avatar
Blue Swirl committed
195
    uint64_t kernel_top;
Blue Swirl's avatar
Blue Swirl committed
196 197 198 199 200

    linux_boot = (kernel_filename != NULL);

    kernel_size = 0;
    if (linux_boot) {
Blue Swirl's avatar
Blue Swirl committed
201 202 203 204 205 206 207
        int bswap_needed;

#ifdef BSWAP_NEEDED
        bswap_needed = 1;
#else
        bswap_needed = 0;
#endif
Blue Swirl's avatar
Blue Swirl committed
208 209 210 211 212
        kernel_size = load_elf(kernel_filename, NULL, NULL, kernel_entry,
                               kernel_addr, &kernel_top, 1, ELF_MACHINE, 0);
        if (kernel_size < 0) {
            *kernel_addr = KERNEL_LOAD_ADDR;
            *kernel_entry = KERNEL_LOAD_ADDR;
Blue Swirl's avatar
Blue Swirl committed
213
            kernel_size = load_aout(kernel_filename, KERNEL_LOAD_ADDR,
Blue Swirl's avatar
Blue Swirl committed
214 215
                                    RAM_size - KERNEL_LOAD_ADDR, bswap_needed,
                                    TARGET_PAGE_SIZE);
Blue Swirl's avatar
Blue Swirl committed
216 217
        }
        if (kernel_size < 0) {
Blue Swirl's avatar
Blue Swirl committed
218 219 220
            kernel_size = load_image_targphys(kernel_filename,
                                              KERNEL_LOAD_ADDR,
                                              RAM_size - KERNEL_LOAD_ADDR);
Blue Swirl's avatar
Blue Swirl committed
221
        }
Blue Swirl's avatar
Blue Swirl committed
222 223 224 225 226
        if (kernel_size < 0) {
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
                    kernel_filename);
            exit(1);
        }
Blue Swirl's avatar
Blue Swirl committed
227
        /* load initrd above kernel */
Blue Swirl's avatar
Blue Swirl committed
228 229
        *initrd_size = 0;
        if (initrd_filename) {
Blue Swirl's avatar
Blue Swirl committed
230 231
            *initrd_addr = TARGET_PAGE_ALIGN(kernel_top);

Blue Swirl's avatar
Blue Swirl committed
232
            *initrd_size = load_image_targphys(initrd_filename,
Blue Swirl's avatar
Blue Swirl committed
233 234 235
                                               *initrd_addr,
                                               RAM_size - *initrd_addr);
            if ((int)*initrd_size < 0) {
Blue Swirl's avatar
Blue Swirl committed
236 237 238 239 240 241 242
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
                        initrd_filename);
                exit(1);
            }
        }
        if (*initrd_size > 0) {
            for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
Blue Swirl's avatar
Blue Swirl committed
243
                ptr = rom_ptr(*kernel_addr + i);
Blue Swirl's avatar
Blue Swirl committed
244
                if (ldl_p(ptr + 8) == 0x48647253) { /* HdrS */
Blue Swirl's avatar
Blue Swirl committed
245
                    stl_p(ptr + 24, *initrd_addr + *kernel_addr);
Blue Swirl's avatar
Blue Swirl committed
246
                    stl_p(ptr + 28, *initrd_size);
Blue Swirl's avatar
Blue Swirl committed
247 248 249 250 251 252 253
                    break;
                }
            }
        }
    }
    return kernel_size;
}
bellard's avatar
bellard committed
254

255
void cpu_check_irqs(CPUSPARCState *env)
256
{
257
    CPUState *cs;
258 259 260
    uint32_t pil = env->pil_in |
                  (env->softint & ~(SOFTINT_TIMER | SOFTINT_STIMER));

261 262 263 264
    /* TT_IVEC has a higher priority (16) than TT_EXTINT (31..17) */
    if (env->ivec_status & 0x20) {
        return;
    }
265
    cs = CPU(sparc_env_get_cpu(env));
266 267 268 269 270 271
    /* check if TM or SM in SOFTINT are set
       setting these also causes interrupt 14 */
    if (env->softint & (SOFTINT_TIMER | SOFTINT_STIMER)) {
        pil |= 1 << 14;
    }

272 273 274
    /* The bit corresponding to psrpil is (1<< psrpil), the next bit
       is (2 << psrpil). */
    if (pil < (2 << env->psrpil)){
275
        if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
276 277 278
            CPUIRQ_DPRINTF("Reset CPU IRQ (current interrupt %x)\n",
                           env->interrupt_index);
            env->interrupt_index = 0;
279
            cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
280 281 282 283 284
        }
        return;
    }

    if (cpu_interrupts_enabled(env)) {
285 286 287

        unsigned int i;

288
        for (i = 15; i > env->psrpil; i--) {
289 290
            if (pil & (1 << i)) {
                int old_interrupt = env->interrupt_index;
291 292
                int new_interrupt = TT_EXTINT | i;

293 294
                if (unlikely(env->tl > 0 && cpu_tsptr(env)->tt > new_interrupt
                  && ((cpu_tsptr(env)->tt & 0x1f0) == TT_EXTINT))) {
295 296 297 298 299 300 301
                    CPUIRQ_DPRINTF("Not setting CPU IRQ: TL=%d "
                                   "current %x >= pending %x\n",
                                   env->tl, cpu_tsptr(env)->tt, new_interrupt);
                } else if (old_interrupt != new_interrupt) {
                    env->interrupt_index = new_interrupt;
                    CPUIRQ_DPRINTF("Set CPU IRQ %d old=%x new=%x\n", i,
                                   old_interrupt, new_interrupt);
302
                    cpu_interrupt(cs, CPU_INTERRUPT_HARD);
303 304 305 306
                }
                break;
            }
        }
307
    } else if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
308 309 310
        CPUIRQ_DPRINTF("Interrupts disabled, pil=%08x pil_in=%08x softint=%08x "
                       "current interrupt %x\n",
                       pil, env->pil_in, env->softint, env->interrupt_index);
311
        env->interrupt_index = 0;
312
        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
313 314 315
    }
}

316
static void cpu_kick_irq(SPARCCPU *cpu)
317
{
318
    CPUState *cs = CPU(cpu);
319 320
    CPUSPARCState *env = &cpu->env;

321
    cs->halted = 0;
322
    cpu_check_irqs(env);
323
    qemu_cpu_kick(cs);
324 325
}

326
static void cpu_set_ivec_irq(void *opaque, int irq, int level)
327
{
328 329
    SPARCCPU *cpu = opaque;
    CPUSPARCState *env = &cpu->env;
330
    CPUState *cs;
331 332

    if (level) {
333 334
        if (!(env->ivec_status & 0x20)) {
            CPUIRQ_DPRINTF("Raise IVEC IRQ %d\n", irq);
335 336
            cs = CPU(cpu);
            cs->halted = 0;
337 338 339 340 341
            env->interrupt_index = TT_IVEC;
            env->ivec_status |= 0x20;
            env->ivec_data[0] = (0x1f << 6) | irq;
            env->ivec_data[1] = 0;
            env->ivec_data[2] = 0;
342
            cpu_interrupt(cs, CPU_INTERRUPT_HARD);
343 344 345 346
        }
    } else {
        if (env->ivec_status & 0x20) {
            CPUIRQ_DPRINTF("Lower IVEC IRQ %d\n", irq);
347
            cs = CPU(cpu);
348
            env->ivec_status &= ~0x20;
349
            cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
350
        }
351 352 353
    }
}

blueswir1's avatar
blueswir1 committed
354
typedef struct ResetData {
355
    SPARCCPU *cpu;
356
    uint64_t prom_addr;
blueswir1's avatar
blueswir1 committed
357 358
} ResetData;

359 360 361 362 363 364 365
void cpu_put_timer(QEMUFile *f, CPUTimer *s)
{
    qemu_put_be32s(f, &s->frequency);
    qemu_put_be32s(f, &s->disabled);
    qemu_put_be64s(f, &s->disabled_mask);
    qemu_put_sbe64s(f, &s->clock_offset);

366
    timer_put(f, s->qtimer);
367 368 369 370 371 372 373 374 375
}

void cpu_get_timer(QEMUFile *f, CPUTimer *s)
{
    qemu_get_be32s(f, &s->frequency);
    qemu_get_be32s(f, &s->disabled);
    qemu_get_be64s(f, &s->disabled_mask);
    qemu_get_sbe64s(f, &s->clock_offset);

376
    timer_get(f, s->qtimer);
377 378
}

379
static CPUTimer *cpu_timer_create(const char *name, SPARCCPU *cpu,
380 381 382
                                  QEMUBHFunc *cb, uint32_t frequency,
                                  uint64_t disabled_mask)
{
383
    CPUTimer *timer = g_malloc0(sizeof (CPUTimer));
384 385 386 387 388 389

    timer->name = name;
    timer->frequency = frequency;
    timer->disabled_mask = disabled_mask;

    timer->disabled = 1;
390
    timer->clock_offset = qemu_get_clock_ns(vm_clock);
391

392
    timer->qtimer = qemu_new_timer_ns(vm_clock, cb, cpu);
393 394 395 396 397 398 399

    return timer;
}

static void cpu_timer_reset(CPUTimer *timer)
{
    timer->disabled = 1;
400
    timer->clock_offset = qemu_get_clock_ns(vm_clock);
401 402 403 404

    qemu_del_timer(timer->qtimer);
}

bellard's avatar
bellard committed
405 406
static void main_cpu_reset(void *opaque)
{
blueswir1's avatar
blueswir1 committed
407
    ResetData *s = (ResetData *)opaque;
408
    CPUSPARCState *env = &s->cpu->env;
409
    static unsigned int nr_resets;
410

411
    cpu_reset(CPU(s->cpu));
412 413 414 415 416

    cpu_timer_reset(env->tick);
    cpu_timer_reset(env->stick);
    cpu_timer_reset(env->hstick);

blueswir1's avatar
blueswir1 committed
417 418 419
    env->gregs[1] = 0; // Memory start
    env->gregs[2] = ram_size; // Memory size
    env->gregs[3] = 0; // Machine description XXX
420 421 422 423 424 425
    if (nr_resets++ == 0) {
        /* Power on reset */
        env->pc = s->prom_addr + 0x20ULL;
    } else {
        env->pc = s->prom_addr + 0x40ULL;
    }
blueswir1's avatar
blueswir1 committed
426
    env->npc = env->pc + 4;
427 428
}

blueswir1's avatar
blueswir1 committed
429
static void tick_irq(void *opaque)
430
{
431 432
    SPARCCPU *cpu = opaque;
    CPUSPARCState *env = &cpu->env;
433

434 435 436 437 438 439 440
    CPUTimer* timer = env->tick;

    if (timer->disabled) {
        CPUIRQ_DPRINTF("tick_irq: softint disabled\n");
        return;
    } else {
        CPUIRQ_DPRINTF("tick: fire\n");
441
    }
442 443

    env->softint |= SOFTINT_TIMER;
444
    cpu_kick_irq(cpu);
445 446
}

blueswir1's avatar
blueswir1 committed
447
static void stick_irq(void *opaque)
448
{
449 450
    SPARCCPU *cpu = opaque;
    CPUSPARCState *env = &cpu->env;
451

452 453 454 455 456 457 458
    CPUTimer* timer = env->stick;

    if (timer->disabled) {
        CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
        return;
    } else {
        CPUIRQ_DPRINTF("stick: fire\n");
459
    }
460 461

    env->softint |= SOFTINT_STIMER;
462
    cpu_kick_irq(cpu);
463 464
}

blueswir1's avatar
blueswir1 committed
465
static void hstick_irq(void *opaque)
466
{
467 468
    SPARCCPU *cpu = opaque;
    CPUSPARCState *env = &cpu->env;
469

470 471 472 473 474 475 476
    CPUTimer* timer = env->hstick;

    if (timer->disabled) {
        CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
        return;
    } else {
        CPUIRQ_DPRINTF("hstick: fire\n");
477
    }
478 479

    env->softint |= SOFTINT_STIMER;
480
    cpu_kick_irq(cpu);
481 482 483 484 485 486 487 488 489 490
}

static int64_t cpu_to_timer_ticks(int64_t cpu_ticks, uint32_t frequency)
{
    return muldiv64(cpu_ticks, get_ticks_per_sec(), frequency);
}

static uint64_t timer_to_cpu_ticks(int64_t timer_ticks, uint32_t frequency)
{
    return muldiv64(timer_ticks, frequency, get_ticks_per_sec());
bellard's avatar
bellard committed
491 492
}

493
void cpu_tick_set_count(CPUTimer *timer, uint64_t count)
blueswir1's avatar
blueswir1 committed
494
{
495 496 497
    uint64_t real_count = count & ~timer->disabled_mask;
    uint64_t disabled_bit = count & timer->disabled_mask;

498
    int64_t vm_clock_offset = qemu_get_clock_ns(vm_clock) -
499 500 501 502 503 504 505 506
                    cpu_to_timer_ticks(real_count, timer->frequency);

    TIMER_DPRINTF("%s set_count count=0x%016lx (%s) p=%p\n",
                  timer->name, real_count,
                  timer->disabled?"disabled":"enabled", timer);

    timer->disabled = disabled_bit ? 1 : 0;
    timer->clock_offset = vm_clock_offset;
blueswir1's avatar
blueswir1 committed
507 508
}

509
uint64_t cpu_tick_get_count(CPUTimer *timer)
blueswir1's avatar
blueswir1 committed
510
{
511
    uint64_t real_count = timer_to_cpu_ticks(
512
                    qemu_get_clock_ns(vm_clock) - timer->clock_offset,
513 514 515 516 517 518 519 520 521 522
                    timer->frequency);

    TIMER_DPRINTF("%s get_count count=0x%016lx (%s) p=%p\n",
           timer->name, real_count,
           timer->disabled?"disabled":"enabled", timer);

    if (timer->disabled)
        real_count |= timer->disabled_mask;

    return real_count;
blueswir1's avatar
blueswir1 committed
523 524
}

525
void cpu_tick_set_limit(CPUTimer *timer, uint64_t limit)
blueswir1's avatar
blueswir1 committed
526
{
527
    int64_t now = qemu_get_clock_ns(vm_clock);
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

    uint64_t real_limit = limit & ~timer->disabled_mask;
    timer->disabled = (limit & timer->disabled_mask) ? 1 : 0;

    int64_t expires = cpu_to_timer_ticks(real_limit, timer->frequency) +
                    timer->clock_offset;

    if (expires < now) {
        expires = now + 1;
    }

    TIMER_DPRINTF("%s set_limit limit=0x%016lx (%s) p=%p "
                  "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
                  timer->name, real_limit,
                  timer->disabled?"disabled":"enabled",
                  timer, limit,
                  timer_to_cpu_ticks(now - timer->clock_offset,
                                     timer->frequency),
                  timer_to_cpu_ticks(expires - now, timer->frequency));

    if (!real_limit) {
        TIMER_DPRINTF("%s set_limit limit=ZERO - not starting timer\n",
                timer->name);
        qemu_del_timer(timer->qtimer);
    } else if (timer->disabled) {
        qemu_del_timer(timer->qtimer);
    } else {
        qemu_mod_timer(timer->qtimer, expires);
    }
blueswir1's avatar
blueswir1 committed
557 558
}

559
static void isa_irq_handler(void *opaque, int n, int level)
560
{
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    static const int isa_irq_to_ivec[16] = {
        [1] = 0x29, /* keyboard */
        [4] = 0x2b, /* serial */
        [6] = 0x27, /* floppy */
        [7] = 0x22, /* parallel */
        [12] = 0x2a, /* mouse */
    };
    qemu_irq *irqs = opaque;
    int ivec;

    assert(n < 16);
    ivec = isa_irq_to_ivec[n];
    EBUS_DPRINTF("Set ISA IRQ %d level %d -> ivec 0x%x\n", n, level, ivec);
    if (ivec) {
        qemu_set_irq(irqs[ivec], level);
    }
577 578
}

blueswir1's avatar
blueswir1 committed
579
/* EBUS (Eight bit bus) bridge */
580
static ISABus *
581
pci_ebus_init(PCIBus *bus, int devfn, qemu_irq *irqs)
blueswir1's avatar
blueswir1 committed
582
{
583
    qemu_irq *isa_irq;
584
    PCIDevice *pci_dev;
585
    ISABus *isa_bus;
586

587
    pci_dev = pci_create_simple(bus, devfn, "ebus");
Andreas Färber's avatar
Andreas Färber committed
588
    isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci_dev), "isa.0"));
589
    isa_irq = qemu_allocate_irqs(isa_irq_handler, irqs, 16);
590 591
    isa_bus_irqs(isa_bus, isa_irq);
    return isa_bus;
Blue Swirl's avatar
Blue Swirl committed
592
}
blueswir1's avatar
blueswir1 committed
593

594
static int
Avi Kivity's avatar
Avi Kivity committed
595
pci_ebus_init1(PCIDevice *pci_dev)
Blue Swirl's avatar
Blue Swirl committed
596
{
Avi Kivity's avatar
Avi Kivity committed
597 598
    EbusState *s = DO_UPCAST(EbusState, pci_dev, pci_dev);

599
    isa_bus_new(&pci_dev->qdev, pci_address_space_io(pci_dev));
Avi Kivity's avatar
Avi Kivity committed
600 601 602 603 604 605 606 607

    pci_dev->config[0x04] = 0x06; // command = bus master, pci mem
    pci_dev->config[0x05] = 0x00;
    pci_dev->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
    pci_dev->config[0x07] = 0x03; // status = medium devsel
    pci_dev->config[0x09] = 0x00; // programming i/f
    pci_dev->config[0x0D] = 0x0a; // latency_timer

Paolo Bonzini's avatar
Paolo Bonzini committed
608 609
    memory_region_init_alias(&s->bar0, OBJECT(s), "bar0", get_system_io(),
                             0, 0x1000000);
610
    pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar0);
Paolo Bonzini's avatar
Paolo Bonzini committed
611 612
    memory_region_init_alias(&s->bar1, OBJECT(s), "bar1", get_system_io(),
                             0, 0x800000);
613
    pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar1);
614
    return 0;
blueswir1's avatar
blueswir1 committed
615 616
}

617 618 619 620 621 622 623 624 625 626 627
static void ebus_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = pci_ebus_init1;
    k->vendor_id = PCI_VENDOR_ID_SUN;
    k->device_id = PCI_DEVICE_ID_SUN_EBUS;
    k->revision = 0x01;
    k->class_id = PCI_CLASS_BRIDGE_OTHER;
}

628
static const TypeInfo ebus_info = {
629 630 631 632
    .name          = "ebus",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(EbusState),
    .class_init    = ebus_class_init,
Blue Swirl's avatar
Blue Swirl committed
633 634
};

635 636 637
#define TYPE_OPENPROM "openprom"
#define OPENPROM(obj) OBJECT_CHECK(PROMState, (obj), TYPE_OPENPROM)

Avi Kivity's avatar
Avi Kivity committed
638
typedef struct PROMState {
639 640
    SysBusDevice parent_obj;

Avi Kivity's avatar
Avi Kivity committed
641 642 643
    MemoryRegion prom;
} PROMState;

644 645
static uint64_t translate_prom_address(void *opaque, uint64_t addr)
{
646
    hwaddr *base_addr = (hwaddr *)opaque;
647 648 649
    return addr + *base_addr - PROM_VADDR;
}

650
/* Boot PROM (OpenBIOS) */
651
static void prom_init(hwaddr addr, const char *bios_name)
652 653 654 655 656 657
{
    DeviceState *dev;
    SysBusDevice *s;
    char *filename;
    int ret;

658
    dev = qdev_create(NULL, TYPE_OPENPROM);
Markus Armbruster's avatar
Markus Armbruster committed
659
    qdev_init_nofail(dev);
660
    s = SYS_BUS_DEVICE(dev);
661 662 663 664 665 666 667 668 669

    sysbus_mmio_map(s, 0, addr);

    /* load boot prom */
    if (bios_name == NULL) {
        bios_name = PROM_FILENAME;
    }
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
    if (filename) {
670 671
        ret = load_elf(filename, translate_prom_address, &addr,
                       NULL, NULL, NULL, 1, ELF_MACHINE, 0);
672 673 674
        if (ret < 0 || ret > PROM_SIZE_MAX) {
            ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
        }
675
        g_free(filename);
676 677 678 679 680 681 682 683 684
    } else {
        ret = -1;
    }
    if (ret < 0 || ret > PROM_SIZE_MAX) {
        fprintf(stderr, "qemu: could not load prom '%s'\n", bios_name);
        exit(1);
    }
}

685
static int prom_init1(SysBusDevice *dev)
686
{
687
    PROMState *s = OPENPROM(dev);
688

689
    memory_region_init_ram(&s->prom, OBJECT(s), "sun4u.prom", PROM_SIZE_MAX);
690
    vmstate_register_ram_global(&s->prom);
Avi Kivity's avatar
Avi Kivity committed
691
    memory_region_set_readonly(&s->prom, true);
692
    sysbus_init_mmio(dev, &s->prom);
693
    return 0;
694 695
}

696 697 698 699 700 701
static Property prom_properties[] = {
    {/* end of property list */},
};

static void prom_class_init(ObjectClass *klass, void *data)
{
702
    DeviceClass *dc = DEVICE_CLASS(klass);
703 704 705
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = prom_init1;
706
    dc->props = prom_properties;
707 708
}

709
static const TypeInfo prom_info = {
710
    .name          = TYPE_OPENPROM,
711 712 713
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(PROMState),
    .class_init    = prom_class_init,
714 715
};

Blue Swirl's avatar
Blue Swirl committed
716

717 718 719 720 721 722
#define TYPE_SUN4U_MEMORY "memory"
#define SUN4U_RAM(obj) OBJECT_CHECK(RamDevice, (obj), TYPE_SUN4U_MEMORY)

typedef struct RamDevice {
    SysBusDevice parent_obj;

Avi Kivity's avatar
Avi Kivity committed
723
    MemoryRegion ram;
724
    uint64_t size;
Blue Swirl's avatar
Blue Swirl committed
725 726 727
} RamDevice;

/* System RAM */
728
static int ram_init1(SysBusDevice *dev)
Blue Swirl's avatar
Blue Swirl committed
729
{
730
    RamDevice *d = SUN4U_RAM(dev);
Blue Swirl's avatar
Blue Swirl committed
731

732
    memory_region_init_ram(&d->ram, OBJECT(d), "sun4u.ram", d->size);
733
    vmstate_register_ram_global(&d->ram);
734
    sysbus_init_mmio(dev, &d->ram);
735
    return 0;
Blue Swirl's avatar
Blue Swirl committed
736 737
}

738
static void ram_init(hwaddr addr, ram_addr_t RAM_size)
Blue Swirl's avatar
Blue Swirl committed
739 740 741 742 743 744
{
    DeviceState *dev;
    SysBusDevice *s;
    RamDevice *d;

    /* allocate RAM */
745
    dev = qdev_create(NULL, TYPE_SUN4U_MEMORY);
746
    s = SYS_BUS_DEVICE(dev);
Blue Swirl's avatar
Blue Swirl committed
747

748
    d = SUN4U_RAM(dev);
Blue Swirl's avatar
Blue Swirl committed
749
    d->size = RAM_size;
Markus Armbruster's avatar
Markus Armbruster committed
750
    qdev_init_nofail(dev);
Blue Swirl's avatar
Blue Swirl committed
751 752 753 754

    sysbus_mmio_map(s, 0, addr);
}

755 756 757 758 759 760 761
static Property ram_properties[] = {
    DEFINE_PROP_UINT64("size", RamDevice, size, 0),
    DEFINE_PROP_END_OF_LIST(),
};

static void ram_class_init(ObjectClass *klass, void *data)
{
762
    DeviceClass *dc = DEVICE_CLASS(klass);
763 764 765
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = ram_init1;
766
    dc->props = ram_properties;
767 768
}

769
static const TypeInfo ram_info = {
770
    .name          = TYPE_SUN4U_MEMORY,
771 772 773
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(RamDevice),
    .class_init    = ram_class_init,
Blue Swirl's avatar
Blue Swirl committed
774 775
};

776
static SPARCCPU *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
bellard's avatar
bellard committed
777
{
778
    SPARCCPU *cpu;
779
    CPUSPARCState *env;
blueswir1's avatar
blueswir1 committed
780
    ResetData *reset_info;
bellard's avatar
bellard committed
781

782 783 784 785
    uint32_t   tick_frequency = 100*1000000;
    uint32_t  stick_frequency = 100*1000000;
    uint32_t hstick_frequency = 100*1000000;

786
    if (cpu_model == NULL) {
787
        cpu_model = hwdef->default_cpu_model;
788 789 790
    }
    cpu = cpu_sparc_init(cpu_model);
    if (cpu == NULL) {
blueswir1's avatar
blueswir1 committed
791 792 793
        fprintf(stderr, "Unable to find Sparc CPU definition\n");
        exit(1);
    }
794
    env = &cpu->env;
795

796
    env->tick = cpu_timer_create("tick", cpu, tick_irq,
797 798
                                  tick_frequency, TICK_NPT_MASK);

799
    env->stick = cpu_timer_create("stick", cpu, stick_irq,
800
                                   stick_frequency, TICK_INT_DIS);
801

802
    env->hstick = cpu_timer_create("hstick", cpu, hstick_irq,
803
                                    hstick_frequency, TICK_INT_DIS);
blueswir1's avatar
blueswir1 committed
804

805
    reset_info = g_malloc0(sizeof(ResetData));
806
    reset_info->cpu = cpu;
807
    reset_info->prom_addr = hwdef->prom_addr;
808
    qemu_register_reset(main_cpu_reset, reset_info);
bellard's avatar
bellard committed
809

810
    return cpu;
Blue Swirl's avatar
Blue Swirl committed
811 812
}

813 814
static void sun4uv_init(MemoryRegion *address_space_mem,
                        ram_addr_t RAM_size,
Blue Swirl's avatar
Blue Swirl committed
815 816 817 818 819
                        const char *boot_devices,
                        const char *kernel_filename, const char *kernel_cmdline,
                        const char *initrd_filename, const char *cpu_model,
                        const struct hwdef *hwdef)
{
820
    SPARCCPU *cpu;
821
    M48t59State *nvram;
Blue Swirl's avatar
Blue Swirl committed
822
    unsigned int i;
Blue Swirl's avatar
Blue Swirl committed
823
    uint64_t initrd_addr, initrd_size, kernel_addr, kernel_size, kernel_entry;
Blue Swirl's avatar
Blue Swirl committed
824
    PCIBus *pci_bus, *pci_bus2, *pci_bus3;
825
    ISABus *isa_bus;
826
    qemu_irq *ivec_irqs, *pbm_irqs;
827
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
Gerd Hoffmann's avatar
Gerd Hoffmann committed
828
    DriveInfo *fd[MAX_FD];
Laszlo Ersek's avatar
Laszlo Ersek committed
829
    FWCfgState *fw_cfg;
Blue Swirl's avatar
Blue Swirl committed
830 831

    /* init CPUs */
832
    cpu = cpu_devinit(cpu_model, hwdef);
Blue Swirl's avatar
Blue Swirl committed
833

Blue Swirl's avatar
Blue Swirl committed
834 835
    /* set up devices */
    ram_init(0, RAM_size);
bellard's avatar
bellard committed
836

837
    prom_init(hwdef->prom_addr, bios_name);
bellard's avatar
bellard committed
838

839
    ivec_irqs = qemu_allocate_irqs(cpu_set_ivec_irq, cpu, IVEC_MAX);
840 841
    pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, ivec_irqs, &pci_bus2,
                           &pci_bus3, &pbm_irqs);
842
    pci_vga_init(pci_bus);
bellard's avatar
bellard committed
843

blueswir1's avatar
blueswir1 committed
844
    // XXX Should be pci_bus3
845
    isa_bus = pci_ebus_init(pci_bus, -1, pbm_irqs);
blueswir1's avatar
blueswir1 committed
846

blueswir1's avatar
blueswir1 committed
847 848
    i = 0;
    if (hwdef->console_serial_base) {
849
        serial_mm_init(address_space_mem, hwdef->console_serial_base, 0,
850
                       NULL, 115200, serial_hds[i], DEVICE_BIG_ENDIAN);
blueswir1's avatar
blueswir1 committed
851 852 853
        i++;
    }
    for(; i < MAX_SERIAL_PORTS; i++) {
bellard's avatar
bellard committed
854
        if (serial_hds[i]) {
855
            serial_isa_init(isa_bus, i, serial_hds[i]);
bellard's avatar
bellard committed
856 857 858 859 860
        }
    }

    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
        if (parallel_hds[i]) {
Hervé Poussineau's avatar