Statistics
| Branch: | Revision:

root / hw / sun4m.c @ 77f193da

History | View | Annotate | Download (51 kB)

1
/*
2
 * QEMU Sun4m & Sun4d & Sun4c System Emulator
3
 *
4
 * Copyright (c) 2003-2005 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "hw.h"
25
#include "qemu-timer.h"
26
#include "sun4m.h"
27
#include "nvram.h"
28
#include "sparc32_dma.h"
29
#include "fdc.h"
30
#include "sysemu.h"
31
#include "net.h"
32
#include "boards.h"
33
#include "firmware_abi.h"
34
#include "scsi.h"
35
#include "pc.h"
36
#include "isa.h"
37

    
38
//#define DEBUG_IRQ
39

    
40
/*
41
 * Sun4m architecture was used in the following machines:
42
 *
43
 * SPARCserver 6xxMP/xx
44
 * SPARCclassic (SPARCclassic Server)(SPARCstation LC) (4/15),
45
 * SPARCclassic X (4/10)
46
 * SPARCstation LX/ZX (4/30)
47
 * SPARCstation Voyager
48
 * SPARCstation 10/xx, SPARCserver 10/xx
49
 * SPARCstation 5, SPARCserver 5
50
 * SPARCstation 20/xx, SPARCserver 20
51
 * SPARCstation 4
52
 *
53
 * Sun4d architecture was used in the following machines:
54
 *
55
 * SPARCcenter 2000
56
 * SPARCserver 1000
57
 *
58
 * Sun4c architecture was used in the following machines:
59
 * SPARCstation 1/1+, SPARCserver 1/1+
60
 * SPARCstation SLC
61
 * SPARCstation IPC
62
 * SPARCstation ELC
63
 * SPARCstation IPX
64
 *
65
 * See for example: http://www.sunhelp.org/faq/sunref1.html
66
 */
67

    
68
#ifdef DEBUG_IRQ
69
#define DPRINTF(fmt, args...)                           \
70
    do { printf("CPUIRQ: " fmt , ##args); } while (0)
71
#else
72
#define DPRINTF(fmt, args...)
73
#endif
74

    
75
#define KERNEL_LOAD_ADDR     0x00004000
76
#define CMDLINE_ADDR         0x007ff000
77
#define INITRD_LOAD_ADDR     0x00800000
78
#define PROM_SIZE_MAX        (512 * 1024)
79
#define PROM_VADDR           0xffd00000
80
#define PROM_FILENAME        "openbios-sparc32"
81

    
82
// Control plane, 8-bit and 24-bit planes
83
#define TCX_SIZE             (9 * 1024 * 1024)
84

    
85
#define MAX_CPUS 16
86
#define MAX_PILS 16
87

    
88
struct hwdef {
89
    target_phys_addr_t iommu_base, slavio_base;
90
    target_phys_addr_t intctl_base, counter_base, nvram_base, ms_kb_base;
91
    target_phys_addr_t serial_base, fd_base;
92
    target_phys_addr_t idreg_base, dma_base, esp_base, le_base;
93
    target_phys_addr_t tcx_base, cs_base, apc_base, aux1_base, aux2_base;
94
    target_phys_addr_t ecc_base;
95
    uint32_t ecc_version;
96
    target_phys_addr_t sun4c_intctl_base, sun4c_counter_base;
97
    long vram_size, nvram_size;
98
    // IRQ numbers are not PIL ones, but master interrupt controller
99
    // register bit numbers
100
    int intctl_g_intr, esp_irq, le_irq, clock_irq, clock1_irq;
101
    int ser_irq, ms_kb_irq, fd_irq, me_irq, cs_irq, ecc_irq;
102
    int machine_id; // For NVRAM
103
    uint32_t iommu_version;
104
    uint32_t intbit_to_level[32];
105
    uint64_t max_mem;
106
    const char * const default_cpu_model;
107
};
108

    
109
#define MAX_IOUNITS 5
110

    
111
struct sun4d_hwdef {
112
    target_phys_addr_t iounit_bases[MAX_IOUNITS], slavio_base;
113
    target_phys_addr_t counter_base, nvram_base, ms_kb_base;
114
    target_phys_addr_t serial_base;
115
    target_phys_addr_t espdma_base, esp_base;
116
    target_phys_addr_t ledma_base, le_base;
117
    target_phys_addr_t tcx_base;
118
    target_phys_addr_t sbi_base;
119
    unsigned long vram_size, nvram_size;
120
    // IRQ numbers are not PIL ones, but SBI register bit numbers
121
    int esp_irq, le_irq, clock_irq, clock1_irq;
122
    int ser_irq, ms_kb_irq, me_irq;
123
    int machine_id; // For NVRAM
124
    uint32_t iounit_version;
125
    uint64_t max_mem;
126
    const char * const default_cpu_model;
127
};
128

    
129
int DMA_get_channel_mode (int nchan)
130
{
131
    return 0;
132
}
133
int DMA_read_memory (int nchan, void *buf, int pos, int size)
134
{
135
    return 0;
136
}
137
int DMA_write_memory (int nchan, void *buf, int pos, int size)
138
{
139
    return 0;
140
}
141
void DMA_hold_DREQ (int nchan) {}
142
void DMA_release_DREQ (int nchan) {}
143
void DMA_schedule(int nchan) {}
144
void DMA_run (void) {}
145
void DMA_init (int high_page_enable) {}
146
void DMA_register_channel (int nchan,
147
                           DMA_transfer_handler transfer_handler,
148
                           void *opaque)
149
{
150
}
151

    
152
extern int nographic;
153

    
154
static void nvram_init(m48t59_t *nvram, uint8_t *macaddr, const char *cmdline,
155
                       const char *boot_devices, ram_addr_t RAM_size,
156
                       uint32_t kernel_size,
157
                       int width, int height, int depth,
158
                       int machine_id, const char *arch)
159
{
160
    unsigned int i;
161
    uint32_t start, end;
162
    uint8_t image[0x1ff0];
163
    ohwcfg_v3_t *header = (ohwcfg_v3_t *)ℑ
164
    struct sparc_arch_cfg *sparc_header;
165
    struct OpenBIOS_nvpart_v1 *part_header;
166

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

    
169
    // Try to match PPC NVRAM
170
    strcpy(header->struct_ident, "QEMU_BIOS");
171
    header->struct_version = cpu_to_be32(3); /* structure v3 */
172

    
173
    header->nvram_size = cpu_to_be16(0x2000);
174
    header->nvram_arch_ptr = cpu_to_be16(sizeof(ohwcfg_v3_t));
175
    header->nvram_arch_size = cpu_to_be16(sizeof(struct sparc_arch_cfg));
176
    strcpy(header->arch, arch);
177
    header->nb_cpus = smp_cpus & 0xff;
178
    header->RAM0_base = 0;
179
    header->RAM0_size = cpu_to_be64((uint64_t)RAM_size);
180
    strcpy(header->boot_devices, boot_devices);
181
    header->nboot_devices = strlen(boot_devices) & 0xff;
182
    header->kernel_image = cpu_to_be64((uint64_t)KERNEL_LOAD_ADDR);
183
    header->kernel_size = cpu_to_be64((uint64_t)kernel_size);
184
    if (cmdline) {
185
        strcpy(phys_ram_base + CMDLINE_ADDR, cmdline);
186
        header->cmdline = cpu_to_be64((uint64_t)CMDLINE_ADDR);
187
        header->cmdline_size = cpu_to_be64((uint64_t)strlen(cmdline));
188
    }
189
    // XXX add initrd_image, initrd_size
190
    header->width = cpu_to_be16(width);
191
    header->height = cpu_to_be16(height);
192
    header->depth = cpu_to_be16(depth);
193
    if (nographic)
194
        header->graphic_flags = cpu_to_be16(OHW_GF_NOGRAPHICS);
195

    
196
    header->crc = cpu_to_be16(OHW_compute_crc(header, 0x00, 0xF8));
197

    
198
    // Architecture specific header
199
    start = sizeof(ohwcfg_v3_t);
200
    sparc_header = (struct sparc_arch_cfg *)&image[start];
201
    sparc_header->valid = 0;
202
    start += sizeof(struct sparc_arch_cfg);
203

    
204
    // OpenBIOS nvram variables
205
    // Variable partition
206
    part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
207
    part_header->signature = OPENBIOS_PART_SYSTEM;
208
    strcpy(part_header->name, "system");
209

    
210
    end = start + sizeof(struct OpenBIOS_nvpart_v1);
211
    for (i = 0; i < nb_prom_envs; i++)
212
        end = OpenBIOS_set_var(image, end, prom_envs[i]);
213

    
214
    // End marker
215
    image[end++] = '\0';
216

    
217
    end = start + ((end - start + 15) & ~15);
218
    OpenBIOS_finish_partition(part_header, end - start);
219

    
220
    // free partition
221
    start = end;
222
    part_header = (struct OpenBIOS_nvpart_v1 *)&image[start];
223
    part_header->signature = OPENBIOS_PART_FREE;
224
    strcpy(part_header->name, "free");
225

    
226
    end = 0x1fd0;
227
    OpenBIOS_finish_partition(part_header, end - start);
228

    
229
    Sun_init_header((struct Sun_nvram *)&image[0x1fd8], macaddr, machine_id);
230

    
231
    for (i = 0; i < sizeof(image); i++)
232
        m48t59_write(nvram, i, image[i]);
233
}
234

    
235
static void *slavio_intctl;
236

    
237
void pic_info(void)
238
{
239
    if (slavio_intctl)
240
        slavio_pic_info(slavio_intctl);
241
}
242

    
243
void irq_info(void)
244
{
245
    if (slavio_intctl)
246
        slavio_irq_info(slavio_intctl);
247
}
248

    
249
void cpu_check_irqs(CPUState *env)
250
{
251
    if (env->pil_in && (env->interrupt_index == 0 ||
252
                        (env->interrupt_index & ~15) == TT_EXTINT)) {
253
        unsigned int i;
254

    
255
        for (i = 15; i > 0; i--) {
256
            if (env->pil_in & (1 << i)) {
257
                int old_interrupt = env->interrupt_index;
258

    
259
                env->interrupt_index = TT_EXTINT | i;
260
                if (old_interrupt != env->interrupt_index) {
261
                    DPRINTF("Set CPU IRQ %d\n", i);
262
                    cpu_interrupt(env, CPU_INTERRUPT_HARD);
263
                }
264
                break;
265
            }
266
        }
267
    } else if (!env->pil_in && (env->interrupt_index & ~15) == TT_EXTINT) {
268
        DPRINTF("Reset CPU IRQ %d\n", env->interrupt_index & 15);
269
        env->interrupt_index = 0;
270
        cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
271
    }
272
}
273

    
274
static void cpu_set_irq(void *opaque, int irq, int level)
275
{
276
    CPUState *env = opaque;
277

    
278
    if (level) {
279
        DPRINTF("Raise CPU IRQ %d\n", irq);
280
        env->halted = 0;
281
        env->pil_in |= 1 << irq;
282
        cpu_check_irqs(env);
283
    } else {
284
        DPRINTF("Lower CPU IRQ %d\n", irq);
285
        env->pil_in &= ~(1 << irq);
286
        cpu_check_irqs(env);
287
    }
288
}
289

    
290
static void dummy_cpu_set_irq(void *opaque, int irq, int level)
291
{
292
}
293

    
294
static void *slavio_misc;
295

    
296
void qemu_system_powerdown(void)
297
{
298
    slavio_set_power_fail(slavio_misc, 1);
299
}
300

    
301
static void main_cpu_reset(void *opaque)
302
{
303
    CPUState *env = opaque;
304

    
305
    cpu_reset(env);
306
    env->halted = 0;
307
}
308

    
309
static void secondary_cpu_reset(void *opaque)
310
{
311
    CPUState *env = opaque;
312

    
313
    cpu_reset(env);
314
    env->halted = 1;
315
}
316

    
317
static unsigned long sun4m_load_kernel(const char *kernel_filename,
318
                                       const char *initrd_filename)
319
{
320
    int linux_boot;
321
    unsigned int i;
322
    long initrd_size, kernel_size;
323

    
324
    linux_boot = (kernel_filename != NULL);
325

    
326
    kernel_size = 0;
327
    if (linux_boot) {
328
        kernel_size = load_elf(kernel_filename, -0xf0000000ULL, NULL, NULL,
329
                               NULL);
330
        if (kernel_size < 0)
331
            kernel_size = load_aout(kernel_filename,
332
                                    phys_ram_base + KERNEL_LOAD_ADDR);
333
        if (kernel_size < 0)
334
            kernel_size = load_image(kernel_filename,
335
                                     phys_ram_base + KERNEL_LOAD_ADDR);
336
        if (kernel_size < 0) {
337
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
338
                    kernel_filename);
339
            exit(1);
340
        }
341

    
342
        /* load initrd */
343
        initrd_size = 0;
344
        if (initrd_filename) {
345
            initrd_size = load_image(initrd_filename,
346
                                     phys_ram_base + INITRD_LOAD_ADDR);
347
            if (initrd_size < 0) {
348
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
349
                        initrd_filename);
350
                exit(1);
351
            }
352
        }
353
        if (initrd_size > 0) {
354
            for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
355
                if (ldl_raw(phys_ram_base + KERNEL_LOAD_ADDR + i)
356
                    == 0x48647253) { // HdrS
357
                    stl_raw(phys_ram_base + KERNEL_LOAD_ADDR + i + 16,
358
                            INITRD_LOAD_ADDR);
359
                    stl_raw(phys_ram_base + KERNEL_LOAD_ADDR + i + 20,
360
                            initrd_size);
361
                    break;
362
                }
363
            }
364
        }
365
    }
366
    return kernel_size;
367
}
368

    
369
static void sun4m_hw_init(const struct hwdef *hwdef, ram_addr_t RAM_size,
370
                          const char *boot_device,
371
                          DisplayState *ds, const char *kernel_filename,
372
                          const char *kernel_cmdline,
373
                          const char *initrd_filename, const char *cpu_model)
374

    
375
{
376
    CPUState *env, *envs[MAX_CPUS];
377
    unsigned int i;
378
    void *iommu, *espdma, *ledma, *main_esp, *nvram;
379
    qemu_irq *cpu_irqs[MAX_CPUS], *slavio_irq, *slavio_cpu_irq,
380
        *espdma_irq, *ledma_irq;
381
    qemu_irq *esp_reset, *le_reset;
382
    qemu_irq *fdc_tc;
383
    unsigned long prom_offset, kernel_size;
384
    int ret;
385
    char buf[1024];
386
    BlockDriverState *fd[MAX_FD];
387
    int drive_index;
388

    
389
    /* init CPUs */
390
    if (!cpu_model)
391
        cpu_model = hwdef->default_cpu_model;
392

    
393
    for(i = 0; i < smp_cpus; i++) {
394
        env = cpu_init(cpu_model);
395
        if (!env) {
396
            fprintf(stderr, "qemu: Unable to find Sparc CPU definition\n");
397
            exit(1);
398
        }
399
        cpu_sparc_set_id(env, i);
400
        envs[i] = env;
401
        if (i == 0) {
402
            qemu_register_reset(main_cpu_reset, env);
403
        } else {
404
            qemu_register_reset(secondary_cpu_reset, env);
405
            env->halted = 1;
406
        }
407
        register_savevm("cpu", i, 3, cpu_save, cpu_load, env);
408
        cpu_irqs[i] = qemu_allocate_irqs(cpu_set_irq, envs[i], MAX_PILS);
409
        env->prom_addr = hwdef->slavio_base;
410
    }
411

    
412
    for (i = smp_cpus; i < MAX_CPUS; i++)
413
        cpu_irqs[i] = qemu_allocate_irqs(dummy_cpu_set_irq, NULL, MAX_PILS);
414

    
415

    
416
    /* allocate RAM */
417
    if ((uint64_t)RAM_size > hwdef->max_mem) {
418
        fprintf(stderr,
419
                "qemu: Too much memory for this machine: %d, maximum %d\n",
420
                (unsigned int)(RAM_size / (1024 * 1024)),
421
                (unsigned int)(hwdef->max_mem / (1024 * 1024)));
422
        exit(1);
423
    }
424
    cpu_register_physical_memory(0, RAM_size, 0);
425

    
426
    /* load boot prom */
427
    prom_offset = RAM_size + hwdef->vram_size;
428
    cpu_register_physical_memory(hwdef->slavio_base,
429
                                 (PROM_SIZE_MAX + TARGET_PAGE_SIZE - 1) &
430
                                 TARGET_PAGE_MASK,
431
                                 prom_offset | IO_MEM_ROM);
432

    
433
    if (bios_name == NULL)
434
        bios_name = PROM_FILENAME;
435
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
436
    ret = load_elf(buf, hwdef->slavio_base - PROM_VADDR, NULL, NULL, NULL);
437
    if (ret < 0 || ret > PROM_SIZE_MAX)
438
        ret = load_image(buf, phys_ram_base + prom_offset);
439
    if (ret < 0 || ret > PROM_SIZE_MAX) {
440
        fprintf(stderr, "qemu: could not load prom '%s'\n",
441
                buf);
442
        exit(1);
443
    }
444
    prom_offset += (ret + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
445

    
446
    /* set up devices */
447
    slavio_intctl = slavio_intctl_init(hwdef->intctl_base,
448
                                       hwdef->intctl_base + 0x10000ULL,
449
                                       &hwdef->intbit_to_level[0],
450
                                       &slavio_irq, &slavio_cpu_irq,
451
                                       cpu_irqs,
452
                                       hwdef->clock_irq);
453

    
454
    if (hwdef->idreg_base != (target_phys_addr_t)-1) {
455
        stl_raw(phys_ram_base + prom_offset, 0xfe810103);
456

    
457
        cpu_register_physical_memory(hwdef->idreg_base, sizeof(uint32_t),
458
                                     prom_offset | IO_MEM_ROM);
459
    }
460

    
461
    iommu = iommu_init(hwdef->iommu_base, hwdef->iommu_version,
462
                       slavio_irq[hwdef->me_irq]);
463

    
464
    espdma = sparc32_dma_init(hwdef->dma_base, slavio_irq[hwdef->esp_irq],
465
                              iommu, &espdma_irq, &esp_reset);
466

    
467
    ledma = sparc32_dma_init(hwdef->dma_base + 16ULL,
468
                             slavio_irq[hwdef->le_irq], iommu, &ledma_irq,
469
                             &le_reset);
470

    
471
    if (graphic_depth != 8 && graphic_depth != 24) {
472
        fprintf(stderr, "qemu: Unsupported depth: %d\n", graphic_depth);
473
        exit (1);
474
    }
475
    tcx_init(ds, hwdef->tcx_base, phys_ram_base + RAM_size, RAM_size,
476
             hwdef->vram_size, graphic_width, graphic_height, graphic_depth);
477

    
478
    if (nd_table[0].model == NULL
479
        || strcmp(nd_table[0].model, "lance") == 0) {
480
        lance_init(&nd_table[0], hwdef->le_base, ledma, *ledma_irq, le_reset);
481
    } else if (strcmp(nd_table[0].model, "?") == 0) {
482
        fprintf(stderr, "qemu: Supported NICs: lance\n");
483
        exit (1);
484
    } else {
485
        fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
486
        exit (1);
487
    }
488

    
489
    nvram = m48t59_init(slavio_irq[0], hwdef->nvram_base, 0,
490
                        hwdef->nvram_size, 8);
491

    
492
    slavio_timer_init_all(hwdef->counter_base, slavio_irq[hwdef->clock1_irq],
493
                          slavio_cpu_irq, smp_cpus);
494

    
495
    slavio_serial_ms_kbd_init(hwdef->ms_kb_base, slavio_irq[hwdef->ms_kb_irq],
496
                              nographic);
497
    // Slavio TTYA (base+4, Linux ttyS0) is the first Qemu serial device
498
    // Slavio TTYB (base+0, Linux ttyS1) is the second Qemu serial device
499
    slavio_serial_init(hwdef->serial_base, slavio_irq[hwdef->ser_irq],
500
                       serial_hds[1], serial_hds[0]);
501

    
502
    slavio_misc = slavio_misc_init(hwdef->slavio_base, hwdef->apc_base,
503
                                   hwdef->aux1_base, hwdef->aux2_base,
504
                                   slavio_irq[hwdef->me_irq], envs[0],
505
                                   &fdc_tc);
506

    
507
    if (hwdef->fd_base != (target_phys_addr_t)-1) {
508
        /* there is zero or one floppy drive */
509
        memset(fd, 0, sizeof(fd));
510
        drive_index = drive_get_index(IF_FLOPPY, 0, 0);
511
        if (drive_index != -1)
512
            fd[0] = drives_table[drive_index].bdrv;
513

    
514
        sun4m_fdctrl_init(slavio_irq[hwdef->fd_irq], hwdef->fd_base, fd,
515
                          fdc_tc);
516
    }
517

    
518
    if (drive_get_max_bus(IF_SCSI) > 0) {
519
        fprintf(stderr, "qemu: too many SCSI bus\n");
520
        exit(1);
521
    }
522

    
523
    main_esp = esp_init(hwdef->esp_base, 2,
524
                        espdma_memory_read, espdma_memory_write,
525
                        espdma, *espdma_irq, esp_reset);
526

    
527
    for (i = 0; i < ESP_MAX_DEVS; i++) {
528
        drive_index = drive_get_index(IF_SCSI, 0, i);
529
        if (drive_index == -1)
530
            continue;
531
        esp_scsi_attach(main_esp, drives_table[drive_index].bdrv, i);
532
    }
533

    
534
    if (hwdef->cs_base != (target_phys_addr_t)-1)
535
        cs_init(hwdef->cs_base, hwdef->cs_irq, slavio_intctl);
536

    
537
    kernel_size = sun4m_load_kernel(kernel_filename, initrd_filename);
538

    
539
    nvram_init(nvram, (uint8_t *)&nd_table[0].macaddr, kernel_cmdline,
540
               boot_device, RAM_size, kernel_size, graphic_width,
541
               graphic_height, graphic_depth, hwdef->machine_id, "Sun4m");
542

    
543
    if (hwdef->ecc_base != (target_phys_addr_t)-1)
544
        ecc_init(hwdef->ecc_base, slavio_irq[hwdef->ecc_irq],
545
                 hwdef->ecc_version);
546
}
547

    
548
static void sun4c_hw_init(const struct hwdef *hwdef, ram_addr_t RAM_size,
549
                          const char *boot_device,
550
                          DisplayState *ds, const char *kernel_filename,
551
                          const char *kernel_cmdline,
552
                          const char *initrd_filename, const char *cpu_model)
553
{
554
    CPUState *env;
555
    unsigned int i;
556
    void *iommu, *espdma, *ledma, *main_esp, *nvram;
557
    qemu_irq *cpu_irqs, *slavio_irq, *espdma_irq, *ledma_irq;
558
    qemu_irq *esp_reset, *le_reset;
559
    qemu_irq *fdc_tc;
560
    unsigned long prom_offset, kernel_size;
561
    int ret;
562
    char buf[1024];
563
    BlockDriverState *fd[MAX_FD];
564
    int drive_index;
565

    
566
    /* init CPU */
567
    if (!cpu_model)
568
        cpu_model = hwdef->default_cpu_model;
569

    
570
    env = cpu_init(cpu_model);
571
    if (!env) {
572
        fprintf(stderr, "qemu: Unable to find Sparc CPU definition\n");
573
        exit(1);
574
    }
575

    
576
    cpu_sparc_set_id(env, 0);
577

    
578
    qemu_register_reset(main_cpu_reset, env);
579
    register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
580
    cpu_irqs = qemu_allocate_irqs(cpu_set_irq, env, MAX_PILS);
581
    env->prom_addr = hwdef->slavio_base;
582

    
583
    /* allocate RAM */
584
    if ((uint64_t)RAM_size > hwdef->max_mem) {
585
        fprintf(stderr,
586
                "qemu: Too much memory for this machine: %d, maximum %d\n",
587
                (unsigned int)(RAM_size / (1024 * 1024)),
588
                (unsigned int)(hwdef->max_mem / (1024 * 1024)));
589
        exit(1);
590
    }
591
    cpu_register_physical_memory(0, RAM_size, 0);
592

    
593
    /* load boot prom */
594
    prom_offset = RAM_size + hwdef->vram_size;
595
    cpu_register_physical_memory(hwdef->slavio_base,
596
                                 (PROM_SIZE_MAX + TARGET_PAGE_SIZE - 1) &
597
                                 TARGET_PAGE_MASK,
598
                                 prom_offset | IO_MEM_ROM);
599

    
600
    if (bios_name == NULL)
601
        bios_name = PROM_FILENAME;
602
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
603
    ret = load_elf(buf, hwdef->slavio_base - PROM_VADDR, NULL, NULL, NULL);
604
    if (ret < 0 || ret > PROM_SIZE_MAX)
605
        ret = load_image(buf, phys_ram_base + prom_offset);
606
    if (ret < 0 || ret > PROM_SIZE_MAX) {
607
        fprintf(stderr, "qemu: could not load prom '%s'\n",
608
                buf);
609
        exit(1);
610
    }
611
    prom_offset += (ret + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
612

    
613
    /* set up devices */
614
    slavio_intctl = sun4c_intctl_init(hwdef->sun4c_intctl_base,
615
                                      &slavio_irq, cpu_irqs);
616

    
617
    iommu = iommu_init(hwdef->iommu_base, hwdef->iommu_version,
618
                       slavio_irq[hwdef->me_irq]);
619

    
620
    espdma = sparc32_dma_init(hwdef->dma_base, slavio_irq[hwdef->esp_irq],
621
                              iommu, &espdma_irq, &esp_reset);
622

    
623
    ledma = sparc32_dma_init(hwdef->dma_base + 16ULL,
624
                             slavio_irq[hwdef->le_irq], iommu, &ledma_irq,
625
                             &le_reset);
626

    
627
    if (graphic_depth != 8 && graphic_depth != 24) {
628
        fprintf(stderr, "qemu: Unsupported depth: %d\n", graphic_depth);
629
        exit (1);
630
    }
631
    tcx_init(ds, hwdef->tcx_base, phys_ram_base + RAM_size, RAM_size,
632
             hwdef->vram_size, graphic_width, graphic_height, graphic_depth);
633

    
634
    if (nd_table[0].model == NULL
635
        || strcmp(nd_table[0].model, "lance") == 0) {
636
        lance_init(&nd_table[0], hwdef->le_base, ledma, *ledma_irq, le_reset);
637
    } else if (strcmp(nd_table[0].model, "?") == 0) {
638
        fprintf(stderr, "qemu: Supported NICs: lance\n");
639
        exit (1);
640
    } else {
641
        fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
642
        exit (1);
643
    }
644

    
645
    nvram = m48t59_init(slavio_irq[0], hwdef->nvram_base, 0,
646
                        hwdef->nvram_size, 2);
647

    
648
    slavio_serial_ms_kbd_init(hwdef->ms_kb_base, slavio_irq[hwdef->ms_kb_irq],
649
                              nographic);
650
    // Slavio TTYA (base+4, Linux ttyS0) is the first Qemu serial device
651
    // Slavio TTYB (base+0, Linux ttyS1) is the second Qemu serial device
652
    slavio_serial_init(hwdef->serial_base, slavio_irq[hwdef->ser_irq],
653
                       serial_hds[1], serial_hds[0]);
654

    
655
    slavio_misc = slavio_misc_init(-1, hwdef->apc_base,
656
                                   hwdef->aux1_base, hwdef->aux2_base,
657
                                   slavio_irq[hwdef->me_irq], env, &fdc_tc);
658

    
659
    if (hwdef->fd_base != (target_phys_addr_t)-1) {
660
        /* there is zero or one floppy drive */
661
        fd[1] = fd[0] = NULL;
662
        drive_index = drive_get_index(IF_FLOPPY, 0, 0);
663
        if (drive_index != -1)
664
            fd[0] = drives_table[drive_index].bdrv;
665

    
666
        sun4m_fdctrl_init(slavio_irq[hwdef->fd_irq], hwdef->fd_base, fd,
667
                          fdc_tc);
668
    }
669

    
670
    if (drive_get_max_bus(IF_SCSI) > 0) {
671
        fprintf(stderr, "qemu: too many SCSI bus\n");
672
        exit(1);
673
    }
674

    
675
    main_esp = esp_init(hwdef->esp_base, 2,
676
                        espdma_memory_read, espdma_memory_write,
677
                        espdma, *espdma_irq, esp_reset);
678

    
679
    for (i = 0; i < ESP_MAX_DEVS; i++) {
680
        drive_index = drive_get_index(IF_SCSI, 0, i);
681
        if (drive_index == -1)
682
            continue;
683
        esp_scsi_attach(main_esp, drives_table[drive_index].bdrv, i);
684
    }
685

    
686
    kernel_size = sun4m_load_kernel(kernel_filename, initrd_filename);
687

    
688
    nvram_init(nvram, (uint8_t *)&nd_table[0].macaddr, kernel_cmdline,
689
               boot_device, RAM_size, kernel_size, graphic_width,
690
               graphic_height, graphic_depth, hwdef->machine_id, "Sun4c");
691
}
692

    
693
static const struct hwdef hwdefs[] = {
694
    /* SS-5 */
695
    {
696
        .iommu_base   = 0x10000000,
697
        .tcx_base     = 0x50000000,
698
        .cs_base      = 0x6c000000,
699
        .slavio_base  = 0x70000000,
700
        .ms_kb_base   = 0x71000000,
701
        .serial_base  = 0x71100000,
702
        .nvram_base   = 0x71200000,
703
        .fd_base      = 0x71400000,
704
        .counter_base = 0x71d00000,
705
        .intctl_base  = 0x71e00000,
706
        .idreg_base   = 0x78000000,
707
        .dma_base     = 0x78400000,
708
        .esp_base     = 0x78800000,
709
        .le_base      = 0x78c00000,
710
        .apc_base     = 0x6a000000,
711
        .aux1_base    = 0x71900000,
712
        .aux2_base    = 0x71910000,
713
        .ecc_base     = -1,
714
        .sun4c_intctl_base  = -1,
715
        .sun4c_counter_base = -1,
716
        .vram_size    = 0x00100000,
717
        .nvram_size   = 0x2000,
718
        .esp_irq = 18,
719
        .le_irq = 16,
720
        .clock_irq = 7,
721
        .clock1_irq = 19,
722
        .ms_kb_irq = 14,
723
        .ser_irq = 15,
724
        .fd_irq = 22,
725
        .me_irq = 30,
726
        .cs_irq = 5,
727
        .machine_id = 0x80,
728
        .iommu_version = 0x05000000,
729
        .intbit_to_level = {
730
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
731
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
732
        },
733
        .max_mem = 0x10000000,
734
        .default_cpu_model = "Fujitsu MB86904",
735
    },
736
    /* SS-10 */
737
    {
738
        .iommu_base   = 0xfe0000000ULL,
739
        .tcx_base     = 0xe20000000ULL,
740
        .cs_base      = -1,
741
        .slavio_base  = 0xff0000000ULL,
742
        .ms_kb_base   = 0xff1000000ULL,
743
        .serial_base  = 0xff1100000ULL,
744
        .nvram_base   = 0xff1200000ULL,
745
        .fd_base      = 0xff1700000ULL,
746
        .counter_base = 0xff1300000ULL,
747
        .intctl_base  = 0xff1400000ULL,
748
        .idreg_base   = 0xef0000000ULL,
749
        .dma_base     = 0xef0400000ULL,
750
        .esp_base     = 0xef0800000ULL,
751
        .le_base      = 0xef0c00000ULL,
752
        .apc_base     = 0xefa000000ULL, // XXX should not exist
753
        .aux1_base    = 0xff1800000ULL,
754
        .aux2_base    = 0xff1a01000ULL,
755
        .ecc_base     = 0xf00000000ULL,
756
        .ecc_version  = 0x10000000, // version 0, implementation 1
757
        .sun4c_intctl_base  = -1,
758
        .sun4c_counter_base = -1,
759
        .vram_size    = 0x00100000,
760
        .nvram_size   = 0x2000,
761
        .esp_irq = 18,
762
        .le_irq = 16,
763
        .clock_irq = 7,
764
        .clock1_irq = 19,
765
        .ms_kb_irq = 14,
766
        .ser_irq = 15,
767
        .fd_irq = 22,
768
        .me_irq = 30,
769
        .cs_irq = -1,
770
        .ecc_irq = 28,
771
        .machine_id = 0x72,
772
        .iommu_version = 0x03000000,
773
        .intbit_to_level = {
774
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
775
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
776
        },
777
        .max_mem = 0xf00000000ULL,
778
        .default_cpu_model = "TI SuperSparc II",
779
    },
780
    /* SS-600MP */
781
    {
782
        .iommu_base   = 0xfe0000000ULL,
783
        .tcx_base     = 0xe20000000ULL,
784
        .cs_base      = -1,
785
        .slavio_base  = 0xff0000000ULL,
786
        .ms_kb_base   = 0xff1000000ULL,
787
        .serial_base  = 0xff1100000ULL,
788
        .nvram_base   = 0xff1200000ULL,
789
        .fd_base      = -1,
790
        .counter_base = 0xff1300000ULL,
791
        .intctl_base  = 0xff1400000ULL,
792
        .idreg_base   = -1,
793
        .dma_base     = 0xef0081000ULL,
794
        .esp_base     = 0xef0080000ULL,
795
        .le_base      = 0xef0060000ULL,
796
        .apc_base     = 0xefa000000ULL, // XXX should not exist
797
        .aux1_base    = 0xff1800000ULL,
798
        .aux2_base    = 0xff1a01000ULL, // XXX should not exist
799
        .ecc_base     = 0xf00000000ULL,
800
        .ecc_version  = 0x00000000, // version 0, implementation 0
801
        .sun4c_intctl_base  = -1,
802
        .sun4c_counter_base = -1,
803
        .vram_size    = 0x00100000,
804
        .nvram_size   = 0x2000,
805
        .esp_irq = 18,
806
        .le_irq = 16,
807
        .clock_irq = 7,
808
        .clock1_irq = 19,
809
        .ms_kb_irq = 14,
810
        .ser_irq = 15,
811
        .fd_irq = 22,
812
        .me_irq = 30,
813
        .cs_irq = -1,
814
        .ecc_irq = 28,
815
        .machine_id = 0x71,
816
        .iommu_version = 0x01000000,
817
        .intbit_to_level = {
818
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
819
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
820
        },
821
        .max_mem = 0xf00000000ULL,
822
        .default_cpu_model = "TI SuperSparc II",
823
    },
824
    /* SS-20 */
825
    {
826
        .iommu_base   = 0xfe0000000ULL,
827
        .tcx_base     = 0xe20000000ULL,
828
        .cs_base      = -1,
829
        .slavio_base  = 0xff0000000ULL,
830
        .ms_kb_base   = 0xff1000000ULL,
831
        .serial_base  = 0xff1100000ULL,
832
        .nvram_base   = 0xff1200000ULL,
833
        .fd_base      = 0xff1700000ULL,
834
        .counter_base = 0xff1300000ULL,
835
        .intctl_base  = 0xff1400000ULL,
836
        .idreg_base   = 0xef0000000ULL,
837
        .dma_base     = 0xef0400000ULL,
838
        .esp_base     = 0xef0800000ULL,
839
        .le_base      = 0xef0c00000ULL,
840
        .apc_base     = 0xefa000000ULL, // XXX should not exist
841
        .aux1_base    = 0xff1800000ULL,
842
        .aux2_base    = 0xff1a01000ULL,
843
        .ecc_base     = 0xf00000000ULL,
844
        .ecc_version  = 0x20000000, // version 0, implementation 2
845
        .sun4c_intctl_base  = -1,
846
        .sun4c_counter_base = -1,
847
        .vram_size    = 0x00100000,
848
        .nvram_size   = 0x2000,
849
        .esp_irq = 18,
850
        .le_irq = 16,
851
        .clock_irq = 7,
852
        .clock1_irq = 19,
853
        .ms_kb_irq = 14,
854
        .ser_irq = 15,
855
        .fd_irq = 22,
856
        .me_irq = 30,
857
        .cs_irq = -1,
858
        .ecc_irq = 28,
859
        .machine_id = 0x72,
860
        .iommu_version = 0x13000000,
861
        .intbit_to_level = {
862
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
863
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
864
        },
865
        .max_mem = 0xf00000000ULL,
866
        .default_cpu_model = "TI SuperSparc II",
867
    },
868
    /* SS-2 */
869
    {
870
        .iommu_base   = 0xf8000000,
871
        .tcx_base     = 0xfe000000,
872
        .cs_base      = -1,
873
        .slavio_base  = 0xf6000000,
874
        .ms_kb_base   = 0xf0000000,
875
        .serial_base  = 0xf1000000,
876
        .nvram_base   = 0xf2000000,
877
        .fd_base      = 0xf7200000,
878
        .counter_base = -1,
879
        .intctl_base  = -1,
880
        .dma_base     = 0xf8400000,
881
        .esp_base     = 0xf8800000,
882
        .le_base      = 0xf8c00000,
883
        .apc_base     = -1,
884
        .aux1_base    = 0xf7400003,
885
        .aux2_base    = -1,
886
        .sun4c_intctl_base  = 0xf5000000,
887
        .sun4c_counter_base = 0xf3000000,
888
        .vram_size    = 0x00100000,
889
        .nvram_size   = 0x800,
890
        .esp_irq = 2,
891
        .le_irq = 3,
892
        .clock_irq = 5,
893
        .clock1_irq = 7,
894
        .ms_kb_irq = 1,
895
        .ser_irq = 1,
896
        .fd_irq = 1,
897
        .me_irq = 1,
898
        .cs_irq = -1,
899
        .machine_id = 0x55,
900
        .max_mem = 0x10000000,
901
        .default_cpu_model = "Cypress CY7C601",
902
    },
903
    /* Voyager */
904
    {
905
        .iommu_base   = 0x10000000,
906
        .tcx_base     = 0x50000000,
907
        .cs_base      = -1,
908
        .slavio_base  = 0x70000000,
909
        .ms_kb_base   = 0x71000000,
910
        .serial_base  = 0x71100000,
911
        .nvram_base   = 0x71200000,
912
        .fd_base      = 0x71400000,
913
        .counter_base = 0x71d00000,
914
        .intctl_base  = 0x71e00000,
915
        .idreg_base   = 0x78000000,
916
        .dma_base     = 0x78400000,
917
        .esp_base     = 0x78800000,
918
        .le_base      = 0x78c00000,
919
        .apc_base     = 0x71300000, // pmc
920
        .aux1_base    = 0x71900000,
921
        .aux2_base    = 0x71910000,
922
        .ecc_base     = -1,
923
        .sun4c_intctl_base  = -1,
924
        .sun4c_counter_base = -1,
925
        .vram_size    = 0x00100000,
926
        .nvram_size   = 0x2000,
927
        .esp_irq = 18,
928
        .le_irq = 16,
929
        .clock_irq = 7,
930
        .clock1_irq = 19,
931
        .ms_kb_irq = 14,
932
        .ser_irq = 15,
933
        .fd_irq = 22,
934
        .me_irq = 30,
935
        .cs_irq = -1,
936
        .machine_id = 0x80,
937
        .iommu_version = 0x05000000,
938
        .intbit_to_level = {
939
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
940
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
941
        },
942
        .max_mem = 0x10000000,
943
        .default_cpu_model = "Fujitsu MB86904",
944
    },
945
    /* LX */
946
    {
947
        .iommu_base   = 0x10000000,
948
        .tcx_base     = 0x50000000,
949
        .cs_base      = -1,
950
        .slavio_base  = 0x70000000,
951
        .ms_kb_base   = 0x71000000,
952
        .serial_base  = 0x71100000,
953
        .nvram_base   = 0x71200000,
954
        .fd_base      = 0x71400000,
955
        .counter_base = 0x71d00000,
956
        .intctl_base  = 0x71e00000,
957
        .idreg_base   = 0x78000000,
958
        .dma_base     = 0x78400000,
959
        .esp_base     = 0x78800000,
960
        .le_base      = 0x78c00000,
961
        .apc_base     = -1,
962
        .aux1_base    = 0x71900000,
963
        .aux2_base    = 0x71910000,
964
        .ecc_base     = -1,
965
        .sun4c_intctl_base  = -1,
966
        .sun4c_counter_base = -1,
967
        .vram_size    = 0x00100000,
968
        .nvram_size   = 0x2000,
969
        .esp_irq = 18,
970
        .le_irq = 16,
971
        .clock_irq = 7,
972
        .clock1_irq = 19,
973
        .ms_kb_irq = 14,
974
        .ser_irq = 15,
975
        .fd_irq = 22,
976
        .me_irq = 30,
977
        .cs_irq = -1,
978
        .machine_id = 0x80,
979
        .iommu_version = 0x04000000,
980
        .intbit_to_level = {
981
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
982
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
983
        },
984
        .max_mem = 0x10000000,
985
        .default_cpu_model = "TI MicroSparc I",
986
    },
987
    /* SS-4 */
988
    {
989
        .iommu_base   = 0x10000000,
990
        .tcx_base     = 0x50000000,
991
        .cs_base      = 0x6c000000,
992
        .slavio_base  = 0x70000000,
993
        .ms_kb_base   = 0x71000000,
994
        .serial_base  = 0x71100000,
995
        .nvram_base   = 0x71200000,
996
        .fd_base      = 0x71400000,
997
        .counter_base = 0x71d00000,
998
        .intctl_base  = 0x71e00000,
999
        .idreg_base   = 0x78000000,
1000
        .dma_base     = 0x78400000,
1001
        .esp_base     = 0x78800000,
1002
        .le_base      = 0x78c00000,
1003
        .apc_base     = 0x6a000000,
1004
        .aux1_base    = 0x71900000,
1005
        .aux2_base    = 0x71910000,
1006
        .ecc_base     = -1,
1007
        .sun4c_intctl_base  = -1,
1008
        .sun4c_counter_base = -1,
1009
        .vram_size    = 0x00100000,
1010
        .nvram_size   = 0x2000,
1011
        .esp_irq = 18,
1012
        .le_irq = 16,
1013
        .clock_irq = 7,
1014
        .clock1_irq = 19,
1015
        .ms_kb_irq = 14,
1016
        .ser_irq = 15,
1017
        .fd_irq = 22,
1018
        .me_irq = 30,
1019
        .cs_irq = 5,
1020
        .machine_id = 0x80,
1021
        .iommu_version = 0x05000000,
1022
        .intbit_to_level = {
1023
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
1024
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
1025
        },
1026
        .max_mem = 0x10000000,
1027
        .default_cpu_model = "Fujitsu MB86904",
1028
    },
1029
    /* SPARCClassic */
1030
    {
1031
        .iommu_base   = 0x10000000,
1032
        .tcx_base     = 0x50000000,
1033
        .cs_base      = -1,
1034
        .slavio_base  = 0x70000000,
1035
        .ms_kb_base   = 0x71000000,
1036
        .serial_base  = 0x71100000,
1037
        .nvram_base   = 0x71200000,
1038
        .fd_base      = 0x71400000,
1039
        .counter_base = 0x71d00000,
1040
        .intctl_base  = 0x71e00000,
1041
        .idreg_base   = 0x78000000,
1042
        .dma_base     = 0x78400000,
1043
        .esp_base     = 0x78800000,
1044
        .le_base      = 0x78c00000,
1045
        .apc_base     = 0x6a000000,
1046
        .aux1_base    = 0x71900000,
1047
        .aux2_base    = 0x71910000,
1048
        .ecc_base     = -1,
1049
        .sun4c_intctl_base  = -1,
1050
        .sun4c_counter_base = -1,
1051
        .vram_size    = 0x00100000,
1052
        .nvram_size   = 0x2000,
1053
        .esp_irq = 18,
1054
        .le_irq = 16,
1055
        .clock_irq = 7,
1056
        .clock1_irq = 19,
1057
        .ms_kb_irq = 14,
1058
        .ser_irq = 15,
1059
        .fd_irq = 22,
1060
        .me_irq = 30,
1061
        .cs_irq = -1,
1062
        .machine_id = 0x80,
1063
        .iommu_version = 0x05000000,
1064
        .intbit_to_level = {
1065
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
1066
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
1067
        },
1068
        .max_mem = 0x10000000,
1069
        .default_cpu_model = "TI MicroSparc I",
1070
    },
1071
    /* SPARCbook */
1072
    {
1073
        .iommu_base   = 0x10000000,
1074
        .tcx_base     = 0x50000000, // XXX
1075
        .cs_base      = -1,
1076
        .slavio_base  = 0x70000000,
1077
        .ms_kb_base   = 0x71000000,
1078
        .serial_base  = 0x71100000,
1079
        .nvram_base   = 0x71200000,
1080
        .fd_base      = 0x71400000,
1081
        .counter_base = 0x71d00000,
1082
        .intctl_base  = 0x71e00000,
1083
        .idreg_base   = 0x78000000,
1084
        .dma_base     = 0x78400000,
1085
        .esp_base     = 0x78800000,
1086
        .le_base      = 0x78c00000,
1087
        .apc_base     = 0x6a000000,
1088
        .aux1_base    = 0x71900000,
1089
        .aux2_base    = 0x71910000,
1090
        .ecc_base     = -1,
1091
        .sun4c_intctl_base  = -1,
1092
        .sun4c_counter_base = -1,
1093
        .vram_size    = 0x00100000,
1094
        .nvram_size   = 0x2000,
1095
        .esp_irq = 18,
1096
        .le_irq = 16,
1097
        .clock_irq = 7,
1098
        .clock1_irq = 19,
1099
        .ms_kb_irq = 14,
1100
        .ser_irq = 15,
1101
        .fd_irq = 22,
1102
        .me_irq = 30,
1103
        .cs_irq = -1,
1104
        .machine_id = 0x80,
1105
        .iommu_version = 0x05000000,
1106
        .intbit_to_level = {
1107
            2, 3, 5, 7, 9, 11, 0, 14,   3, 5, 7, 9, 11, 13, 12, 12,
1108
            6, 0, 4, 10, 8, 0, 11, 0,   0, 0, 0, 0, 15, 0, 15, 0,
1109
        },
1110
        .max_mem = 0x10000000,
1111
        .default_cpu_model = "TI MicroSparc I",
1112
    },
1113
};
1114

    
1115
/* SPARCstation 5 hardware initialisation */
1116
static void ss5_init(ram_addr_t RAM_size, int vga_ram_size,
1117
                     const char *boot_device, DisplayState *ds,
1118
                     const char *kernel_filename, const char *kernel_cmdline,
1119
                     const char *initrd_filename, const char *cpu_model)
1120
{
1121
    sun4m_hw_init(&hwdefs[0], RAM_size, boot_device, ds, kernel_filename,
1122
                  kernel_cmdline, initrd_filename, cpu_model);
1123
}
1124

    
1125
/* SPARCstation 10 hardware initialisation */
1126
static void ss10_init(ram_addr_t RAM_size, int vga_ram_size,
1127
                      const char *boot_device, DisplayState *ds,
1128
                      const char *kernel_filename, const char *kernel_cmdline,
1129
                      const char *initrd_filename, const char *cpu_model)
1130
{
1131
    sun4m_hw_init(&hwdefs[1], RAM_size, boot_device, ds, kernel_filename,
1132
                  kernel_cmdline, initrd_filename, cpu_model);
1133
}
1134

    
1135
/* SPARCserver 600MP hardware initialisation */
1136
static void ss600mp_init(ram_addr_t RAM_size, int vga_ram_size,
1137
                         const char *boot_device, DisplayState *ds,
1138
                         const char *kernel_filename,
1139
                         const char *kernel_cmdline,
1140
                         const char *initrd_filename, const char *cpu_model)
1141
{
1142
    sun4m_hw_init(&hwdefs[2], RAM_size, boot_device, ds, kernel_filename,
1143
                  kernel_cmdline, initrd_filename, cpu_model);
1144
}
1145

    
1146
/* SPARCstation 20 hardware initialisation */
1147
static void ss20_init(ram_addr_t RAM_size, int vga_ram_size,
1148
                      const char *boot_device, DisplayState *ds,
1149
                      const char *kernel_filename, const char *kernel_cmdline,
1150
                      const char *initrd_filename, const char *cpu_model)
1151
{
1152
    sun4m_hw_init(&hwdefs[3], RAM_size, boot_device, ds, kernel_filename,
1153
                  kernel_cmdline, initrd_filename, cpu_model);
1154
}
1155

    
1156
/* SPARCstation 2 hardware initialisation */
1157
static void ss2_init(ram_addr_t RAM_size, int vga_ram_size,
1158
                     const char *boot_device, DisplayState *ds,
1159
                     const char *kernel_filename, const char *kernel_cmdline,
1160
                     const char *initrd_filename, const char *cpu_model)
1161
{
1162
    sun4c_hw_init(&hwdefs[4], RAM_size, boot_device, ds, kernel_filename,
1163
                  kernel_cmdline, initrd_filename, cpu_model);
1164
}
1165

    
1166
/* SPARCstation Voyager hardware initialisation */
1167
static void vger_init(ram_addr_t RAM_size, int vga_ram_size,
1168
                      const char *boot_device, DisplayState *ds,
1169
                      const char *kernel_filename, const char *kernel_cmdline,
1170
                      const char *initrd_filename, const char *cpu_model)
1171
{
1172
    sun4m_hw_init(&hwdefs[5], RAM_size, boot_device, ds, kernel_filename,
1173
                  kernel_cmdline, initrd_filename, cpu_model);
1174
}
1175

    
1176
/* SPARCstation LX hardware initialisation */
1177
static void ss_lx_init(ram_addr_t RAM_size, int vga_ram_size,
1178
                       const char *boot_device, DisplayState *ds,
1179
                       const char *kernel_filename, const char *kernel_cmdline,
1180
                       const char *initrd_filename, const char *cpu_model)
1181
{
1182
    sun4m_hw_init(&hwdefs[6], RAM_size, boot_device, ds, kernel_filename,
1183
                  kernel_cmdline, initrd_filename, cpu_model);
1184
}
1185

    
1186
/* SPARCstation 4 hardware initialisation */
1187
static void ss4_init(ram_addr_t RAM_size, int vga_ram_size,
1188
                     const char *boot_device, DisplayState *ds,
1189
                     const char *kernel_filename, const char *kernel_cmdline,
1190
                     const char *initrd_filename, const char *cpu_model)
1191
{
1192
    sun4m_hw_init(&hwdefs[7], RAM_size, boot_device, ds, kernel_filename,
1193
                  kernel_cmdline, initrd_filename, cpu_model);
1194
}
1195

    
1196
/* SPARCClassic hardware initialisation */
1197
static void scls_init(ram_addr_t RAM_size, int vga_ram_size,
1198
                      const char *boot_device, DisplayState *ds,
1199
                      const char *kernel_filename, const char *kernel_cmdline,
1200
                      const char *initrd_filename, const char *cpu_model)
1201
{
1202
    sun4m_hw_init(&hwdefs[8], RAM_size, boot_device, ds, kernel_filename,
1203
                  kernel_cmdline, initrd_filename, cpu_model);
1204
}
1205

    
1206
/* SPARCbook hardware initialisation */
1207
static void sbook_init(ram_addr_t RAM_size, int vga_ram_size,
1208
                       const char *boot_device, DisplayState *ds,
1209
                       const char *kernel_filename, const char *kernel_cmdline,
1210
                       const char *initrd_filename, const char *cpu_model)
1211
{
1212
    sun4m_hw_init(&hwdefs[9], RAM_size, boot_device, ds, kernel_filename,
1213
                  kernel_cmdline, initrd_filename, cpu_model);
1214
}
1215

    
1216
QEMUMachine ss5_machine = {
1217
    "SS-5",
1218
    "Sun4m platform, SPARCstation 5",
1219
    ss5_init,
1220
    PROM_SIZE_MAX + TCX_SIZE,
1221
};
1222

    
1223
QEMUMachine ss10_machine = {
1224
    "SS-10",
1225
    "Sun4m platform, SPARCstation 10",
1226
    ss10_init,
1227
    PROM_SIZE_MAX + TCX_SIZE,
1228
};
1229

    
1230
QEMUMachine ss600mp_machine = {
1231
    "SS-600MP",
1232
    "Sun4m platform, SPARCserver 600MP",
1233
    ss600mp_init,
1234
    PROM_SIZE_MAX + TCX_SIZE,
1235
};
1236

    
1237
QEMUMachine ss20_machine = {
1238
    "SS-20",
1239
    "Sun4m platform, SPARCstation 20",
1240
    ss20_init,
1241
    PROM_SIZE_MAX + TCX_SIZE,
1242
};
1243

    
1244
QEMUMachine ss2_machine = {
1245
    "SS-2",
1246
    "Sun4c platform, SPARCstation 2",
1247
    ss2_init,
1248
    PROM_SIZE_MAX + TCX_SIZE,
1249
};
1250

    
1251
QEMUMachine voyager_machine = {
1252
    "Voyager",
1253
    "Sun4m platform, SPARCstation Voyager",
1254
    vger_init,
1255
    PROM_SIZE_MAX + TCX_SIZE,
1256
};
1257

    
1258
QEMUMachine ss_lx_machine = {
1259
    "LX",
1260
    "Sun4m platform, SPARCstation LX",
1261
    ss_lx_init,
1262
    PROM_SIZE_MAX + TCX_SIZE,
1263
};
1264

    
1265
QEMUMachine ss4_machine = {
1266
    "SS-4",
1267
    "Sun4m platform, SPARCstation 4",
1268
    ss4_init,
1269
    PROM_SIZE_MAX + TCX_SIZE,
1270
};
1271

    
1272
QEMUMachine scls_machine = {
1273
    "SPARCClassic",
1274
    "Sun4m platform, SPARCClassic",
1275
    scls_init,
1276
    PROM_SIZE_MAX + TCX_SIZE,
1277
};
1278

    
1279
QEMUMachine sbook_machine = {
1280
    "SPARCbook",
1281
    "Sun4m platform, SPARCbook",
1282
    sbook_init,
1283
    PROM_SIZE_MAX + TCX_SIZE,
1284
};
1285

    
1286
static const struct sun4d_hwdef sun4d_hwdefs[] = {
1287
    /* SS-1000 */
1288
    {
1289
        .iounit_bases   = {
1290
            0xfe0200000ULL,
1291
            0xfe1200000ULL,
1292
            0xfe2200000ULL,
1293
            0xfe3200000ULL,
1294
            -1,
1295
        },
1296
        .tcx_base     = 0x820000000ULL,
1297
        .slavio_base  = 0xf00000000ULL,
1298
        .ms_kb_base   = 0xf00240000ULL,
1299
        .serial_base  = 0xf00200000ULL,
1300
        .nvram_base   = 0xf00280000ULL,
1301
        .counter_base = 0xf00300000ULL,
1302
        .espdma_base  = 0x800081000ULL,
1303
        .esp_base     = 0x800080000ULL,
1304
        .ledma_base   = 0x800040000ULL,
1305
        .le_base      = 0x800060000ULL,
1306
        .sbi_base     = 0xf02800000ULL,
1307
        .vram_size    = 0x00100000,
1308
        .nvram_size   = 0x2000,
1309
        .esp_irq = 3,
1310
        .le_irq = 4,
1311
        .clock_irq = 14,
1312
        .clock1_irq = 10,
1313
        .ms_kb_irq = 12,
1314
        .ser_irq = 12,
1315
        .machine_id = 0x80,
1316
        .iounit_version = 0x03000000,
1317
        .max_mem = 0xf00000000ULL,
1318
        .default_cpu_model = "TI SuperSparc II",
1319
    },
1320
    /* SS-2000 */
1321
    {
1322
        .iounit_bases   = {
1323
            0xfe0200000ULL,
1324
            0xfe1200000ULL,
1325
            0xfe2200000ULL,
1326
            0xfe3200000ULL,
1327
            0xfe4200000ULL,
1328
        },
1329
        .tcx_base     = 0x820000000ULL,
1330
        .slavio_base  = 0xf00000000ULL,
1331
        .ms_kb_base   = 0xf00240000ULL,
1332
        .serial_base  = 0xf00200000ULL,
1333
        .nvram_base   = 0xf00280000ULL,
1334
        .counter_base = 0xf00300000ULL,
1335
        .espdma_base  = 0x800081000ULL,
1336
        .esp_base     = 0x800080000ULL,
1337
        .ledma_base   = 0x800040000ULL,
1338
        .le_base      = 0x800060000ULL,
1339
        .sbi_base     = 0xf02800000ULL,
1340
        .vram_size    = 0x00100000,
1341
        .nvram_size   = 0x2000,
1342
        .esp_irq = 3,
1343
        .le_irq = 4,
1344
        .clock_irq = 14,
1345
        .clock1_irq = 10,
1346
        .ms_kb_irq = 12,
1347
        .ser_irq = 12,
1348
        .machine_id = 0x80,
1349
        .iounit_version = 0x03000000,
1350
        .max_mem = 0xf00000000ULL,
1351
        .default_cpu_model = "TI SuperSparc II",
1352
    },
1353
};
1354

    
1355
static void sun4d_hw_init(const struct sun4d_hwdef *hwdef, ram_addr_t RAM_size,
1356
                          const char *boot_device,
1357
                          DisplayState *ds, const char *kernel_filename,
1358
                          const char *kernel_cmdline,
1359
                          const char *initrd_filename, const char *cpu_model)
1360
{
1361
    CPUState *env, *envs[MAX_CPUS];
1362
    unsigned int i;
1363
    void *iounits[MAX_IOUNITS], *espdma, *ledma, *main_esp, *nvram, *sbi;
1364
    qemu_irq *cpu_irqs[MAX_CPUS], *sbi_irq, *sbi_cpu_irq,
1365
        *espdma_irq, *ledma_irq;
1366
    qemu_irq *esp_reset, *le_reset;
1367
    unsigned long prom_offset, kernel_size;
1368
    int ret;
1369
    char buf[1024];
1370
    int drive_index;
1371

    
1372
    /* init CPUs */
1373
    if (!cpu_model)
1374
        cpu_model = hwdef->default_cpu_model;
1375

    
1376
    for (i = 0; i < smp_cpus; i++) {
1377
        env = cpu_init(cpu_model);
1378
        if (!env) {
1379
            fprintf(stderr, "qemu: Unable to find Sparc CPU definition\n");
1380
            exit(1);
1381
        }
1382
        cpu_sparc_set_id(env, i);
1383
        envs[i] = env;
1384
        if (i == 0) {
1385
            qemu_register_reset(main_cpu_reset, env);
1386
        } else {
1387
            qemu_register_reset(secondary_cpu_reset, env);
1388
            env->halted = 1;
1389
        }
1390
        register_savevm("cpu", i, 3, cpu_save, cpu_load, env);
1391
        cpu_irqs[i] = qemu_allocate_irqs(cpu_set_irq, envs[i], MAX_PILS);
1392
        env->prom_addr = hwdef->slavio_base;
1393
    }
1394

    
1395
    for (i = smp_cpus; i < MAX_CPUS; i++)
1396
        cpu_irqs[i] = qemu_allocate_irqs(dummy_cpu_set_irq, NULL, MAX_PILS);
1397

    
1398
    /* allocate RAM */
1399
    if ((uint64_t)RAM_size > hwdef->max_mem) {
1400
        fprintf(stderr,
1401
                "qemu: Too much memory for this machine: %d, maximum %d\n",
1402
                (unsigned int)(RAM_size / (1024 * 1024)),
1403
                (unsigned int)(hwdef->max_mem / (1024 * 1024)));
1404
        exit(1);
1405
    }
1406
    cpu_register_physical_memory(0, RAM_size, 0);
1407

    
1408
    /* load boot prom */
1409
    prom_offset = RAM_size + hwdef->vram_size;
1410
    cpu_register_physical_memory(hwdef->slavio_base,
1411
                                 (PROM_SIZE_MAX + TARGET_PAGE_SIZE - 1) &
1412
                                 TARGET_PAGE_MASK,
1413
                                 prom_offset | IO_MEM_ROM);
1414

    
1415
    if (bios_name == NULL)
1416
        bios_name = PROM_FILENAME;
1417
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
1418
    ret = load_elf(buf, hwdef->slavio_base - PROM_VADDR, NULL, NULL, NULL);
1419
    if (ret < 0 || ret > PROM_SIZE_MAX)
1420
        ret = load_image(buf, phys_ram_base + prom_offset);
1421
    if (ret < 0 || ret > PROM_SIZE_MAX) {
1422
        fprintf(stderr, "qemu: could not load prom '%s'\n",
1423
                buf);
1424
        exit(1);
1425
    }
1426

    
1427
    /* set up devices */
1428
    sbi = sbi_init(hwdef->sbi_base, &sbi_irq, &sbi_cpu_irq, cpu_irqs);
1429

    
1430
    for (i = 0; i < MAX_IOUNITS; i++)
1431
        if (hwdef->iounit_bases[i] != (target_phys_addr_t)-1)
1432
            iounits[i] = iommu_init(hwdef->iounit_bases[i],
1433
                                    hwdef->iounit_version,
1434
                                    sbi_irq[hwdef->me_irq]);
1435

    
1436
    espdma = sparc32_dma_init(hwdef->espdma_base, sbi_irq[hwdef->esp_irq],
1437
                              iounits[0], &espdma_irq, &esp_reset);
1438

    
1439
    ledma = sparc32_dma_init(hwdef->ledma_base, sbi_irq[hwdef->le_irq],
1440
                             iounits[0], &ledma_irq, &le_reset);
1441

    
1442
    if (graphic_depth != 8 && graphic_depth != 24) {
1443
        fprintf(stderr, "qemu: Unsupported depth: %d\n", graphic_depth);
1444
        exit (1);
1445
    }
1446
    tcx_init(ds, hwdef->tcx_base, phys_ram_base + RAM_size, RAM_size,
1447
             hwdef->vram_size, graphic_width, graphic_height, graphic_depth);
1448

    
1449
    if (nd_table[0].model == NULL
1450
        || strcmp(nd_table[0].model, "lance") == 0) {
1451
        lance_init(&nd_table[0], hwdef->le_base, ledma, *ledma_irq, le_reset);
1452
    } else if (strcmp(nd_table[0].model, "?") == 0) {
1453
        fprintf(stderr, "qemu: Supported NICs: lance\n");
1454
        exit (1);
1455
    } else {
1456
        fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
1457
        exit (1);
1458
    }
1459

    
1460
    nvram = m48t59_init(sbi_irq[0], hwdef->nvram_base, 0,
1461
                        hwdef->nvram_size, 8);
1462

    
1463
    slavio_timer_init_all(hwdef->counter_base, sbi_irq[hwdef->clock1_irq],
1464
                          sbi_cpu_irq, smp_cpus);
1465

    
1466
    slavio_serial_ms_kbd_init(hwdef->ms_kb_base, sbi_irq[hwdef->ms_kb_irq],
1467
                              nographic);
1468
    // Slavio TTYA (base+4, Linux ttyS0) is the first Qemu serial device
1469
    // Slavio TTYB (base+0, Linux ttyS1) is the second Qemu serial device
1470
    slavio_serial_init(hwdef->serial_base, sbi_irq[hwdef->ser_irq],
1471
                       serial_hds[1], serial_hds[0]);
1472

    
1473
    if (drive_get_max_bus(IF_SCSI) > 0) {
1474
        fprintf(stderr, "qemu: too many SCSI bus\n");
1475
        exit(1);
1476
    }
1477

    
1478
    main_esp = esp_init(hwdef->esp_base, 2,
1479
                        espdma_memory_read, espdma_memory_write,
1480
                        espdma, *espdma_irq, esp_reset);
1481

    
1482
    for (i = 0; i < ESP_MAX_DEVS; i++) {
1483
        drive_index = drive_get_index(IF_SCSI, 0, i);
1484
        if (drive_index == -1)
1485
            continue;
1486
        esp_scsi_attach(main_esp, drives_table[drive_index].bdrv, i);
1487
    }
1488

    
1489
    kernel_size = sun4m_load_kernel(kernel_filename, initrd_filename);
1490

    
1491
    nvram_init(nvram, (uint8_t *)&nd_table[0].macaddr, kernel_cmdline,
1492
               boot_device, RAM_size, kernel_size, graphic_width,
1493
               graphic_height, graphic_depth, hwdef->machine_id, "Sun4d");
1494
}
1495

    
1496
/* SPARCserver 1000 hardware initialisation */
1497
static void ss1000_init(ram_addr_t RAM_size, int vga_ram_size,
1498
                        const char *boot_device, DisplayState *ds,
1499
                        const char *kernel_filename, const char *kernel_cmdline,
1500
                        const char *initrd_filename, const char *cpu_model)
1501
{
1502
    sun4d_hw_init(&sun4d_hwdefs[0], RAM_size, boot_device, ds, kernel_filename,
1503
                  kernel_cmdline, initrd_filename, cpu_model);
1504
}
1505

    
1506
/* SPARCcenter 2000 hardware initialisation */
1507
static void ss2000_init(ram_addr_t RAM_size, int vga_ram_size,
1508
                        const char *boot_device, DisplayState *ds,
1509
                        const char *kernel_filename, const char *kernel_cmdline,
1510
                        const char *initrd_filename, const char *cpu_model)
1511
{
1512
    sun4d_hw_init(&sun4d_hwdefs[1], RAM_size, boot_device, ds, kernel_filename,
1513
                  kernel_cmdline, initrd_filename, cpu_model);
1514
}
1515

    
1516
QEMUMachine ss1000_machine = {
1517
    "SS-1000",
1518
    "Sun4d platform, SPARCserver 1000",
1519
    ss1000_init,
1520
    PROM_SIZE_MAX + TCX_SIZE,
1521
};
1522

    
1523
QEMUMachine ss2000_machine = {
1524
    "SS-2000",
1525
    "Sun4d platform, SPARCcenter 2000",
1526
    ss2000_init,
1527
    PROM_SIZE_MAX + TCX_SIZE,
1528
};