typhoon.c 27.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * DEC 21272 (TSUNAMI/TYPHOON) chipset emulation.
 *
 * Written by Richard Henderson.
 *
 * This work is licensed under the GNU GPL license version 2 or later.
 */

#include "cpu.h"
10
#include "hw/hw.h"
11
#include "hw/devices.h"
12
#include "sysemu/sysemu.h"
13
#include "alpha_sys.h"
14
#include "exec/address-spaces.h"
15 16


17 18
#define TYPE_TYPHOON_PCI_HOST_BRIDGE "typhoon-pcihost"

19 20 21 22 23 24
typedef struct TyphoonCchip {
    MemoryRegion region;
    uint64_t misc;
    uint64_t drir;
    uint64_t dim[4];
    uint32_t iic[4];
25
    AlphaCPU *cpu[4];
26 27 28
} TyphoonCchip;

typedef struct TyphoonWindow {
29 30 31
    uint64_t wba;
    uint64_t wsm;
    uint64_t tba;
32 33 34 35 36 37 38 39
} TyphoonWindow;
 
typedef struct TyphoonPchip {
    MemoryRegion region;
    MemoryRegion reg_iack;
    MemoryRegion reg_mem;
    MemoryRegion reg_io;
    MemoryRegion reg_conf;
40 41 42 43

    AddressSpace iommu_as;
    MemoryRegion iommu;

44 45 46 47
    uint64_t ctl;
    TyphoonWindow win[4];
} TyphoonPchip;

48 49 50
#define TYPHOON_PCI_HOST_BRIDGE(obj) \
    OBJECT_CHECK(TyphoonState, (obj), TYPE_TYPHOON_PCI_HOST_BRIDGE)

51
typedef struct TyphoonState {
A
Andreas Färber 已提交
52
    PCIHostState parent_obj;
53

54 55 56 57 58 59 60
    TyphoonCchip cchip;
    TyphoonPchip pchip;
    MemoryRegion dchip_region;
    MemoryRegion ram_region;
} TyphoonState;

/* Called when one of DRIR or DIM changes.  */
61
static void cpu_irq_change(AlphaCPU *cpu, uint64_t req)
62 63
{
    /* If there are any non-masked interrupts, tell the cpu.  */
64
    if (cpu != NULL) {
65
        CPUState *cs = CPU(cpu);
66
        if (req) {
67
            cpu_interrupt(cs, CPU_INTERRUPT_HARD);
68
        } else {
69
            cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
70 71 72 73
        }
    }
}

A
Avi Kivity 已提交
74
static uint64_t cchip_read(void *opaque, hwaddr addr, unsigned size)
75
{
76
    CPUState *cpu = current_cpu;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    TyphoonState *s = opaque;
    uint64_t ret = 0;

    switch (addr) {
    case 0x0000:
        /* CSC: Cchip System Configuration Register.  */
        /* All sorts of data here; probably the only thing relevant is
           PIP<14> Pchip 1 Present = 0.  */
        break;

    case 0x0040:
        /* MTR: Memory Timing Register.  */
        /* All sorts of stuff related to real DRAM.  */
        break;

    case 0x0080:
        /* MISC: Miscellaneous Register.  */
94
        ret = s->cchip.misc | (cpu->cpu_index & 3);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        break;

    case 0x00c0:
        /* MPD: Memory Presence Detect Register.  */
        break;

    case 0x0100: /* AAR0 */
    case 0x0140: /* AAR1 */
    case 0x0180: /* AAR2 */
    case 0x01c0: /* AAR3 */
        /* AAR: Array Address Register.  */
        /* All sorts of information about DRAM.  */
        break;

    case 0x0200:
        /* DIM0: Device Interrupt Mask Register, CPU0.  */
        ret = s->cchip.dim[0];
        break;
    case 0x0240:
        /* DIM1: Device Interrupt Mask Register, CPU1.  */
        ret = s->cchip.dim[1];
        break;
    case 0x0280:
        /* DIR0: Device Interrupt Request Register, CPU0.  */
        ret = s->cchip.dim[0] & s->cchip.drir;
        break;
    case 0x02c0:
        /* DIR1: Device Interrupt Request Register, CPU1.  */
        ret = s->cchip.dim[1] & s->cchip.drir;
        break;
    case 0x0300:
        /* DRIR: Device Raw Interrupt Request Register.  */
        ret = s->cchip.drir;
        break;

    case 0x0340:
        /* PRBEN: Probe Enable Register.  */
        break;

    case 0x0380:
        /* IIC0: Interval Ignore Count Register, CPU0.  */
        ret = s->cchip.iic[0];
        break;
    case 0x03c0:
        /* IIC1: Interval Ignore Count Register, CPU1.  */
        ret = s->cchip.iic[1];
        break;

    case 0x0400: /* MPR0 */
    case 0x0440: /* MPR1 */
    case 0x0480: /* MPR2 */
    case 0x04c0: /* MPR3 */
        /* MPR: Memory Programming Register.  */
        break;

    case 0x0580:
        /* TTR: TIGbus Timing Register.  */
        /* All sorts of stuff related to interrupt delivery timings.  */
        break;
    case 0x05c0:
        /* TDR: TIGbug Device Timing Register.  */
        break;

    case 0x0600:
        /* DIM2: Device Interrupt Mask Register, CPU2.  */
        ret = s->cchip.dim[2];
        break;
    case 0x0640:
        /* DIM3: Device Interrupt Mask Register, CPU3.  */
        ret = s->cchip.dim[3];
        break;
    case 0x0680:
        /* DIR2: Device Interrupt Request Register, CPU2.  */
        ret = s->cchip.dim[2] & s->cchip.drir;
        break;
    case 0x06c0:
        /* DIR3: Device Interrupt Request Register, CPU3.  */
        ret = s->cchip.dim[3] & s->cchip.drir;
        break;

    case 0x0700:
        /* IIC2: Interval Ignore Count Register, CPU2.  */
        ret = s->cchip.iic[2];
        break;
    case 0x0740:
        /* IIC3: Interval Ignore Count Register, CPU3.  */
        ret = s->cchip.iic[3];
        break;

    case 0x0780:
        /* PWR: Power Management Control.   */
        break;
    
    case 0x0c00: /* CMONCTLA */
    case 0x0c40: /* CMONCTLB */
    case 0x0c80: /* CMONCNT01 */
    case 0x0cc0: /* CMONCNT23 */
        break;

    default:
195
        cpu_unassigned_access(cpu, addr, false, false, 0, size);
196 197 198 199 200 201
        return -1;
    }

    return ret;
}

A
Avi Kivity 已提交
202
static uint64_t dchip_read(void *opaque, hwaddr addr, unsigned size)
203 204 205 206 207
{
    /* Skip this.  It's all related to DRAM timing and setup.  */
    return 0;
}

A
Avi Kivity 已提交
208
static uint64_t pchip_read(void *opaque, hwaddr addr, unsigned size)
209 210 211 212 213 214 215
{
    TyphoonState *s = opaque;
    uint64_t ret = 0;

    switch (addr) {
    case 0x0000:
        /* WSBA0: Window Space Base Address Register.  */
216
        ret = s->pchip.win[0].wba;
217 218 219
        break;
    case 0x0040:
        /* WSBA1 */
220
        ret = s->pchip.win[1].wba;
221 222 223
        break;
    case 0x0080:
        /* WSBA2 */
224
        ret = s->pchip.win[2].wba;
225 226 227
        break;
    case 0x00c0:
        /* WSBA3 */
228
        ret = s->pchip.win[3].wba;
229 230 231 232
        break;

    case 0x0100:
        /* WSM0: Window Space Mask Register.  */
233
        ret = s->pchip.win[0].wsm;
234 235 236
        break;
    case 0x0140:
        /* WSM1 */
237
        ret = s->pchip.win[1].wsm;
238 239 240
        break;
    case 0x0180:
        /* WSM2 */
241
        ret = s->pchip.win[2].wsm;
242 243 244
        break;
    case 0x01c0:
        /* WSM3 */
245
        ret = s->pchip.win[3].wsm;
246 247 248 249
        break;

    case 0x0200:
        /* TBA0: Translated Base Address Register.  */
250
        ret = s->pchip.win[0].tba;
251 252 253
        break;
    case 0x0240:
        /* TBA1 */
254
        ret = s->pchip.win[1].tba;
255 256 257
        break;
    case 0x0280:
        /* TBA2 */
258
        ret = s->pchip.win[2].tba;
259 260 261
        break;
    case 0x02c0:
        /* TBA3 */
262
        ret = s->pchip.win[3].tba;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        break;

    case 0x0300:
        /* PCTL: Pchip Control Register.  */
        ret = s->pchip.ctl;
        break;
    case 0x0340:
        /* PLAT: Pchip Master Latency Register.  */
        break;
    case 0x03c0:
        /* PERROR: Pchip Error Register.  */
        break;
    case 0x0400:
        /* PERRMASK: Pchip Error Mask Register.  */
        break;
    case 0x0440:
        /* PERRSET: Pchip Error Set Register.  */
        break;
    case 0x0480:
        /* TLBIV: Translation Buffer Invalidate Virtual Register (WO).  */
        break;
    case 0x04c0:
        /* TLBIA: Translation Buffer Invalidate All Register (WO).  */
        break;
    case 0x0500: /* PMONCTL */
    case 0x0540: /* PMONCNT */
    case 0x0800: /* SPRST */
        break;

    default:
293
        cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
294 295 296 297 298 299
        return -1;
    }

    return ret;
}

A
Avi Kivity 已提交
300
static void cchip_write(void *opaque, hwaddr addr,
301
                        uint64_t val, unsigned size)
302 303
{
    TyphoonState *s = opaque;
304
    uint64_t oldval, newval;
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

    switch (addr) {
    case 0x0000:
        /* CSC: Cchip System Configuration Register.  */
        /* All sorts of data here; nothing relevant RW.  */
        break;

    case 0x0040:
        /* MTR: Memory Timing Register.  */
        /* All sorts of stuff related to real DRAM.  */
        break;

    case 0x0080:
        /* MISC: Miscellaneous Register.  */
        newval = oldval = s->cchip.misc;
        newval &= ~(val & 0x10000ff0);     /* W1C fields */
        if (val & 0x100000) {
            newval &= ~0xff0000ull;        /* ACL clears ABT and ABW */
        } else {
            newval |= val & 0x00f00000;    /* ABT field is W1S */
            if ((newval & 0xf0000) == 0) {
                newval |= val & 0xf0000;   /* ABW field is W1S iff zero */
            }
        }
        newval |= (val & 0xf000) >> 4;     /* IPREQ field sets IPINTR.  */

        newval &= ~0xf0000000000ull;       /* WO and RW fields */
        newval |= val & 0xf0000000000ull;
        s->cchip.misc = newval;

        /* Pass on changes to IPI and ITI state.  */
        if ((newval ^ oldval) & 0xff0) {
            int i;
            for (i = 0; i < 4; ++i) {
339 340
                AlphaCPU *cpu = s->cchip.cpu[i];
                if (cpu != NULL) {
341
                    CPUState *cs = CPU(cpu);
342 343
                    /* IPI can be either cleared or set by the write.  */
                    if (newval & (1 << (i + 8))) {
344
                        cpu_interrupt(cs, CPU_INTERRUPT_SMP);
345
                    } else {
346
                        cpu_reset_interrupt(cs, CPU_INTERRUPT_SMP);
347 348 349 350
                    }

                    /* ITI can only be cleared by the write.  */
                    if ((newval & (1 << (i + 4))) == 0) {
351
                        cpu_reset_interrupt(cs, CPU_INTERRUPT_TIMER);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
                    }
                }
            }
        }
        break;

    case 0x00c0:
        /* MPD: Memory Presence Detect Register.  */
        break;

    case 0x0100: /* AAR0 */
    case 0x0140: /* AAR1 */
    case 0x0180: /* AAR2 */
    case 0x01c0: /* AAR3 */
        /* AAR: Array Address Register.  */
        /* All sorts of information about DRAM.  */
        break;

    case 0x0200: /* DIM0 */
        /* DIM: Device Interrupt Mask Register, CPU0.  */
        s->cchip.dim[0] = val;
        cpu_irq_change(s->cchip.cpu[0], val & s->cchip.drir);
        break;
    case 0x0240: /* DIM1 */
        /* DIM: Device Interrupt Mask Register, CPU1.  */
        s->cchip.dim[0] = val;
        cpu_irq_change(s->cchip.cpu[1], val & s->cchip.drir);
        break;

    case 0x0280: /* DIR0 (RO) */
    case 0x02c0: /* DIR1 (RO) */
    case 0x0300: /* DRIR (RO) */
        break;

    case 0x0340:
        /* PRBEN: Probe Enable Register.  */
        break;

    case 0x0380: /* IIC0 */
        s->cchip.iic[0] = val & 0xffffff;
        break;
    case 0x03c0: /* IIC1 */
        s->cchip.iic[1] = val & 0xffffff;
        break;

    case 0x0400: /* MPR0 */
    case 0x0440: /* MPR1 */
    case 0x0480: /* MPR2 */
    case 0x04c0: /* MPR3 */
        /* MPR: Memory Programming Register.  */
        break;

    case 0x0580:
        /* TTR: TIGbus Timing Register.  */
        /* All sorts of stuff related to interrupt delivery timings.  */
        break;
    case 0x05c0:
        /* TDR: TIGbug Device Timing Register.  */
        break;

    case 0x0600:
        /* DIM2: Device Interrupt Mask Register, CPU2.  */
        s->cchip.dim[2] = val;
        cpu_irq_change(s->cchip.cpu[2], val & s->cchip.drir);
        break;
    case 0x0640:
        /* DIM3: Device Interrupt Mask Register, CPU3.  */
        s->cchip.dim[3] = val;
        cpu_irq_change(s->cchip.cpu[3], val & s->cchip.drir);
        break;

    case 0x0680: /* DIR2 (RO) */
    case 0x06c0: /* DIR3 (RO) */
        break;

    case 0x0700: /* IIC2 */
        s->cchip.iic[2] = val & 0xffffff;
        break;
    case 0x0740: /* IIC3 */
        s->cchip.iic[3] = val & 0xffffff;
        break;

    case 0x0780:
        /* PWR: Power Management Control.   */
        break;
    
    case 0x0c00: /* CMONCTLA */
    case 0x0c40: /* CMONCTLB */
    case 0x0c80: /* CMONCNT01 */
    case 0x0cc0: /* CMONCNT23 */
        break;

    default:
445
        cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
446 447 448 449
        return;
    }
}

A
Avi Kivity 已提交
450
static void dchip_write(void *opaque, hwaddr addr,
451 452 453 454 455
                        uint64_t val, unsigned size)
{
    /* Skip this.  It's all related to DRAM timing and setup.  */
}

A
Avi Kivity 已提交
456
static void pchip_write(void *opaque, hwaddr addr,
457
                        uint64_t val, unsigned size)
458 459
{
    TyphoonState *s = opaque;
460
    uint64_t oldval;
461 462 463 464

    switch (addr) {
    case 0x0000:
        /* WSBA0: Window Space Base Address Register.  */
465
        s->pchip.win[0].wba = val & 0xfff00003u;
466 467 468
        break;
    case 0x0040:
        /* WSBA1 */
469
        s->pchip.win[1].wba = val & 0xfff00003u;
470 471 472
        break;
    case 0x0080:
        /* WSBA2 */
473
        s->pchip.win[2].wba = val & 0xfff00003u;
474 475 476
        break;
    case 0x00c0:
        /* WSBA3 */
477
        s->pchip.win[3].wba = (val & 0x80fff00001ull) | 2;
478 479 480 481
        break;

    case 0x0100:
        /* WSM0: Window Space Mask Register.  */
482
        s->pchip.win[0].wsm = val & 0xfff00000u;
483 484 485
        break;
    case 0x0140:
        /* WSM1 */
486
        s->pchip.win[1].wsm = val & 0xfff00000u;
487 488 489
        break;
    case 0x0180:
        /* WSM2 */
490
        s->pchip.win[2].wsm = val & 0xfff00000u;
491 492 493
        break;
    case 0x01c0:
        /* WSM3 */
494
        s->pchip.win[3].wsm = val & 0xfff00000u;
495 496 497 498
        break;

    case 0x0200:
        /* TBA0: Translated Base Address Register.  */
499
        s->pchip.win[0].tba = val & 0x7fffffc00ull;
500 501 502
        break;
    case 0x0240:
        /* TBA1 */
503
        s->pchip.win[1].tba = val & 0x7fffffc00ull;
504 505 506
        break;
    case 0x0280:
        /* TBA2 */
507
        s->pchip.win[2].tba = val & 0x7fffffc00ull;
508 509 510
        break;
    case 0x02c0:
        /* TBA3 */
511
        s->pchip.win[3].tba = val & 0x7fffffc00ull;
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        break;

    case 0x0300:
        /* PCTL: Pchip Control Register.  */
        oldval = s->pchip.ctl;
        oldval &= ~0x00001cff0fc7ffull;       /* RW fields */
        oldval |= val & 0x00001cff0fc7ffull;
        s->pchip.ctl = oldval;
        break;

    case 0x0340:
        /* PLAT: Pchip Master Latency Register.  */
        break;
    case 0x03c0:
        /* PERROR: Pchip Error Register.  */
        break;
    case 0x0400:
        /* PERRMASK: Pchip Error Mask Register.  */
        break;
    case 0x0440:
        /* PERRSET: Pchip Error Set Register.  */
        break;

    case 0x0480:
        /* TLBIV: Translation Buffer Invalidate Virtual Register.  */
        break;

    case 0x04c0:
        /* TLBIA: Translation Buffer Invalidate All Register (WO).  */
        break;

    case 0x0500:
        /* PMONCTL */
    case 0x0540:
        /* PMONCNT */
    case 0x0800:
        /* SPRST */
        break;

    default:
552
        cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
553 554 555 556 557 558 559 560 561
        return;
    }
}

static const MemoryRegionOps cchip_ops = {
    .read = cchip_read,
    .write = cchip_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .valid = {
562
        .min_access_size = 8,
563 564 565
        .max_access_size = 8,
    },
    .impl = {
566 567
        .min_access_size = 8,
        .max_access_size = 8,
568 569 570 571 572 573 574 575
    },
};

static const MemoryRegionOps dchip_ops = {
    .read = dchip_read,
    .write = dchip_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .valid = {
576
        .min_access_size = 8,
577 578 579
        .max_access_size = 8,
    },
    .impl = {
580
        .min_access_size = 8,
581 582 583 584 585 586 587 588 589
        .max_access_size = 8,
    },
};

static const MemoryRegionOps pchip_ops = {
    .read = pchip_read,
    .write = pchip_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .valid = {
590
        .min_access_size = 8,
591 592 593
        .max_access_size = 8,
    },
    .impl = {
594 595
        .min_access_size = 8,
        .max_access_size = 8,
596 597 598
    },
};

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
/* A subroutine of typhoon_translate_iommu that builds an IOMMUTLBEntry
   using the given translated address and mask.  */
static bool make_iommu_tlbe(hwaddr taddr, hwaddr mask, IOMMUTLBEntry *ret)
{
    *ret = (IOMMUTLBEntry) {
        .target_as = &address_space_memory,
        .translated_addr = taddr,
        .addr_mask = mask,
        .perm = IOMMU_RW,
    };
    return true;
}

/* A subroutine of typhoon_translate_iommu that handles scatter-gather
   translation, given the address of the PTE.  */
static bool pte_translate(hwaddr pte_addr, IOMMUTLBEntry *ret)
{
616 617
    uint64_t pte = address_space_ldq(&address_space_memory, pte_addr,
                                     MEMTXATTRS_UNSPECIFIED, NULL);
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

    /* Check valid bit.  */
    if ((pte & 1) == 0) {
        return false;
    }

    return make_iommu_tlbe((pte & 0x3ffffe) << 12, 0x1fff, ret);
}

/* A subroutine of typhoon_translate_iommu that handles one of the
   four single-address-cycle translation windows.  */
static bool window_translate(TyphoonWindow *win, hwaddr addr,
                             IOMMUTLBEntry *ret)
{
    uint32_t wba = win->wba;
    uint64_t wsm = win->wsm;
    uint64_t tba = win->tba;
    uint64_t wsm_ext = wsm | 0xfffff;

    /* Check for window disabled.  */
    if ((wba & 1) == 0) {
        return false;
    }

    /* Check for window hit.  */
    if ((addr & ~wsm_ext) != (wba & 0xfff00000u)) {
        return false;
    }

    if (wba & 2) {
        /* Scatter-gather translation.  */
        hwaddr pte_addr;

        /* See table 10-6, Generating PTE address for PCI DMA Address.  */
        pte_addr  = tba & ~(wsm >> 10);
        pte_addr |= (addr & (wsm | 0xfe000)) >> 10;
        return pte_translate(pte_addr, ret);
    } else {
	/* Direct-mapped translation.  */
	return make_iommu_tlbe(tba & ~wsm_ext, wsm_ext, ret);
    }
}

/* Handle PCI-to-system address translation.  */
/* TODO: A translation failure here ought to set PCI error codes on the
   Pchip and generate a machine check interrupt.  */
664 665
static IOMMUTLBEntry typhoon_translate_iommu(MemoryRegion *iommu, hwaddr addr,
                                             bool is_write)
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
{
    TyphoonPchip *pchip = container_of(iommu, TyphoonPchip, iommu);
    IOMMUTLBEntry ret;
    int i;

    if (addr <= 0xffffffffu) {
        /* Single-address cycle.  */

        /* Check for the Window Hole, inhibiting matching.  */
        if ((pchip->ctl & 0x20)
            && addr >= 0x80000
            && addr <= 0xfffff) {
            goto failure;
        }

        /* Check the first three windows.  */
        for (i = 0; i < 3; ++i) {
            if (window_translate(&pchip->win[i], addr, &ret)) {
                goto success;
            }
        }

        /* Check the fourth window for DAC disable.  */
        if ((pchip->win[3].wba & 0x80000000000ull) == 0
	    && window_translate(&pchip->win[3], addr, &ret)) {
            goto success;
        }
    } else {
        /* Double-address cycle.  */

        if (addr >= 0x10000000000ull && addr < 0x20000000000ull) {
            /* Check for the DMA monster window.  */
            if (pchip->ctl & 0x40) {
                /* See 10.1.4.4; in particular <39:35> is ignored.  */
                make_iommu_tlbe(0, 0x007ffffffffull, &ret);
		goto success;
            }
        }

705
        if (addr >= 0x80000000000ull && addr <= 0xfffffffffffull) {
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
            /* Check the fourth window for DAC enable and window enable.  */
            if ((pchip->win[3].wba & 0x80000000001ull) == 0x80000000001ull) {
                uint64_t pte_addr;

                pte_addr  = pchip->win[3].tba & 0x7ffc00000ull;
                pte_addr |= (addr & 0xffffe000u) >> 10;
                if (pte_translate(pte_addr, &ret)) {
			goto success;
		}
            }
        }
    }

 failure:
    ret = (IOMMUTLBEntry) { .perm = IOMMU_NONE };
 success:
    return ret;
}

static const MemoryRegionIOMMUOps typhoon_iommu_ops = {
    .translate = typhoon_translate_iommu,
};

static AddressSpace *typhoon_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
{
    TyphoonState *s = opaque;
    return &s->pchip.iommu_as;
}

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
static void typhoon_set_irq(void *opaque, int irq, int level)
{
    TyphoonState *s = opaque;
    uint64_t drir;
    int i;

    /* Set/Reset the bit in CCHIP.DRIR based on IRQ+LEVEL.  */
    drir = s->cchip.drir;
    if (level) {
        drir |= 1ull << irq;
    } else {
        drir &= ~(1ull << irq);
    }
    s->cchip.drir = drir;

    for (i = 0; i < 4; ++i) {
        cpu_irq_change(s->cchip.cpu[i], s->cchip.dim[i] & drir);
    }
}

static void typhoon_set_isa_irq(void *opaque, int irq, int level)
{
    typhoon_set_irq(opaque, 55, level);
}

static void typhoon_set_timer_irq(void *opaque, int irq, int level)
{
    TyphoonState *s = opaque;
    int i;

    /* Thankfully, the mc146818rtc code doesn't track the IRQ state,
       and so we don't have to worry about missing interrupts just
       because we never actually ACK the interrupt.  Just ignore any
       case of the interrupt level going low.  */
    if (level == 0) {
        return;
    }

    /* Deliver the interrupt to each CPU, considering each CPU's IIC.  */
    for (i = 0; i < 4; ++i) {
775 776
        AlphaCPU *cpu = s->cchip.cpu[i];
        if (cpu != NULL) {
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
            uint32_t iic = s->cchip.iic[i];

            /* ??? The verbage in Section 10.2.2.10 isn't 100% clear.
               Bit 24 is the OverFlow bit, RO, and set when the count
               decrements past 0.  When is OF cleared?  My guess is that
               OF is actually cleared when the IIC is written, and that
               the ICNT field always decrements.  At least, that's an
               interpretation that makes sense, and "allows the CPU to
               determine exactly how mant interval timer ticks were
               skipped".  At least within the next 4M ticks...  */

            iic = ((iic - 1) & 0x1ffffff) | (iic & 0x1000000);
            s->cchip.iic[i] = iic;

            if (iic & 0x1000000) {
                /* Set the ITI bit for this cpu.  */
                s->cchip.misc |= 1 << (i + 4);
                /* And signal the interrupt.  */
795
                cpu_interrupt(CPU(cpu), CPU_INTERRUPT_TIMER);
796 797 798 799 800
            }
        }
    }
}

801 802 803 804 805 806 807
static void typhoon_alarm_timer(void *opaque)
{
    TyphoonState *s = (TyphoonState *)((uintptr_t)opaque & ~3);
    int cpu = (uintptr_t)opaque & 3;

    /* Set the ITI bit for this cpu.  */
    s->cchip.misc |= 1 << (cpu + 4);
808
    cpu_interrupt(CPU(s->cchip.cpu[cpu]), CPU_INTERRUPT_TIMER);
809 810
}

811 812
PCIBus *typhoon_init(ram_addr_t ram_size, ISABus **isa_bus,
                     qemu_irq *p_rtc_irq,
813
                     AlphaCPU *cpus[4], pci_map_irq_fn sys_map_irq)
814 815 816 817 818 819
{
    const uint64_t MB = 1024 * 1024;
    const uint64_t GB = 1024 * MB;
    MemoryRegion *addr_space = get_system_memory();
    DeviceState *dev;
    TyphoonState *s;
820
    PCIHostState *phb;
821
    PCIBus *b;
822
    int i;
823

824
    dev = qdev_create(NULL, TYPE_TYPHOON_PCI_HOST_BRIDGE);
825 826
    qdev_init_nofail(dev);

827
    s = TYPHOON_PCI_HOST_BRIDGE(dev);
828
    phb = PCI_HOST_BRIDGE(dev);
829

830 831 832
    s->cchip.misc = 0x800000000ull; /* Revision: Typhoon.  */
    s->pchip.win[3].wba = 2;        /* Window 3 SG always enabled. */

833
    /* Remember the CPUs so that we can deliver interrupts to them.  */
834
    for (i = 0; i < 4; i++) {
835 836 837
        AlphaCPU *cpu = cpus[i];
        s->cchip.cpu[i] = cpu;
        if (cpu != NULL) {
838
            cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
839 840 841 842
                                                 typhoon_alarm_timer,
                                                 (void *)((uintptr_t)s + i));
        }
    }
843

844
    *p_rtc_irq = qemu_allocate_irq(typhoon_set_timer_irq, s, 0);
845 846 847

    /* Main memory region, 0x00.0000.0000.  Real hardware supports 32GB,
       but the address space hole reserved at this point is 8TB.  */
848 849
    memory_region_allocate_system_memory(&s->ram_region, OBJECT(s), "ram",
                                         ram_size);
850 851 852 853 854 855 856
    memory_region_add_subregion(addr_space, 0, &s->ram_region);

    /* TIGbus, 0x801.0000.0000, 1GB.  */
    /* ??? The TIGbus is used for delivering interrupts, and access to
       the flash ROM.  I'm not sure that we need to implement it at all.  */

    /* Pchip0 CSRs, 0x801.8000.0000, 256MB.  */
857 858
    memory_region_init_io(&s->pchip.region, OBJECT(s), &pchip_ops, s, "pchip0",
                          256*MB);
859 860
    memory_region_add_subregion(addr_space, 0x80180000000ULL,
                                &s->pchip.region);
861 862

    /* Cchip CSRs, 0x801.A000.0000, 256MB.  */
863 864
    memory_region_init_io(&s->cchip.region, OBJECT(s), &cchip_ops, s, "cchip0",
                          256*MB);
865 866
    memory_region_add_subregion(addr_space, 0x801a0000000ULL,
                                &s->cchip.region);
867 868

    /* Dchip CSRs, 0x801.B000.0000, 256MB.  */
869 870
    memory_region_init_io(&s->dchip_region, OBJECT(s), &dchip_ops, s, "dchip0",
                          256*MB);
871 872
    memory_region_add_subregion(addr_space, 0x801b0000000ULL,
                                &s->dchip_region);
873 874

    /* Pchip0 PCI memory, 0x800.0000.0000, 4GB.  */
875
    memory_region_init(&s->pchip.reg_mem, OBJECT(s), "pci0-mem", 4*GB);
876 877
    memory_region_add_subregion(addr_space, 0x80000000000ULL,
                                &s->pchip.reg_mem);
878 879

    /* Pchip0 PCI I/O, 0x801.FC00.0000, 32MB.  */
880 881
    memory_region_init_io(&s->pchip.reg_io, OBJECT(s), &alpha_pci_ignore_ops,
                          NULL, "pci0-io", 32*MB);
882 883
    memory_region_add_subregion(addr_space, 0x801fc000000ULL,
                                &s->pchip.reg_io);
884

885
    b = pci_register_bus(dev, "pci",
886
                         typhoon_set_irq, sys_map_irq, s,
887 888
                         &s->pchip.reg_mem, &s->pchip.reg_io,
                         0, 64, TYPE_PCI_BUS);
889
    phb->bus = b;
890

891 892 893 894 895 896
    /* Host memory as seen from the PCI side, via the IOMMU.  */
    memory_region_init_iommu(&s->pchip.iommu, OBJECT(s), &typhoon_iommu_ops,
                             "iommu-typhoon", UINT64_MAX);
    address_space_init(&s->pchip.iommu_as, &s->pchip.iommu, "pchip0-pci");
    pci_setup_iommu(b, typhoon_pci_dma_iommu, s);

897
    /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB.  */
898 899
    memory_region_init_io(&s->pchip.reg_iack, OBJECT(s), &alpha_pci_iack_ops,
                          b, "pci0-iack", 64*MB);
900 901
    memory_region_add_subregion(addr_space, 0x801f8000000ULL,
                                &s->pchip.reg_iack);
902 903

    /* Pchip0 PCI configuration, 0x801.FE00.0000, 16MB.  */
904 905
    memory_region_init_io(&s->pchip.reg_conf, OBJECT(s), &alpha_pci_conf1_ops,
                          b, "pci0-conf", 16*MB);
906 907
    memory_region_add_subregion(addr_space, 0x801fe000000ULL,
                                &s->pchip.reg_conf);
908 909 910 911 912 913 914 915 916 917 918 919 920

    /* For the record, these are the mappings for the second PCI bus.
       We can get away with not implementing them because we indicate
       via the Cchip.CSC<PIP> bit that Pchip1 is not present.  */
    /* Pchip1 PCI memory, 0x802.0000.0000, 4GB.  */
    /* Pchip1 CSRs, 0x802.8000.0000, 256MB.  */
    /* Pchip1 PCI special/interrupt acknowledge, 0x802.F800.0000, 64MB.  */
    /* Pchip1 PCI I/O, 0x802.FC00.0000, 32MB.  */
    /* Pchip1 PCI configuration, 0x802.FE00.0000, 16MB.  */

    /* Init the ISA bus.  */
    /* ??? Technically there should be a cy82c693ub pci-isa bridge.  */
    {
921
        qemu_irq *isa_irqs;
922

923
        *isa_bus = isa_bus_new(NULL, get_system_memory(), &s->pchip.reg_io);
924 925
        isa_irqs = i8259_init(*isa_bus,
                              qemu_allocate_irq(typhoon_set_isa_irq, s, 0));
926
        isa_bus_irqs(*isa_bus, isa_irqs);
927 928 929 930 931 932 933 934 935 936
    }

    return b;
}

static int typhoon_pcihost_init(SysBusDevice *dev)
{
    return 0;
}

937 938 939 940 941 942 943
static void typhoon_pcihost_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = typhoon_pcihost_init;
}

944
static const TypeInfo typhoon_pcihost_info = {
945
    .name          = TYPE_TYPHOON_PCI_HOST_BRIDGE,
946
    .parent        = TYPE_PCI_HOST_BRIDGE,
947 948
    .instance_size = sizeof(TyphoonState),
    .class_init    = typhoon_pcihost_class_init,
949 950
};

A
Andreas Färber 已提交
951
static void typhoon_register_types(void)
952
{
953
    type_register_static(&typhoon_pcihost_info);
954
}
A
Andreas Färber 已提交
955 956

type_init(typhoon_register_types)