2 * DEC 21272 (TSUNAMI/TYPHOON) chipset emulation.
4 * Written by Richard Henderson.
6 * This work is licensed under the GNU GPL license version 2 or later.
10 #include "exec/exec-all.h"
12 #include "hw/devices.h"
13 #include "sysemu/sysemu.h"
14 #include "hw/alpha_sys.h"
15 #include "exec/address-spaces.h"
18 #define TYPE_TYPHOON_PCI_HOST_BRIDGE "typhoon-pcihost"
20 typedef struct TyphoonCchip {
29 typedef struct TyphoonWindow {
32 uint32_t translated_base_pfn;
35 typedef struct TyphoonPchip {
37 MemoryRegion reg_iack;
40 MemoryRegion reg_conf;
45 #define TYPHOON_PCI_HOST_BRIDGE(obj) \
46 OBJECT_CHECK(TyphoonState, (obj), TYPE_TYPHOON_PCI_HOST_BRIDGE)
48 typedef struct TyphoonState {
49 PCIHostState parent_obj;
53 MemoryRegion dchip_region;
54 MemoryRegion ram_region;
56 /* QEMU emulation state. */
60 /* Called when one of DRIR or DIM changes. */
61 static void cpu_irq_change(AlphaCPU *cpu, uint64_t req)
63 /* If there are any non-masked interrupts, tell the cpu. */
65 CPUAlphaState *env = &cpu->env;
66 CPUState *cs = CPU(cpu);
68 cpu_interrupt(env, CPU_INTERRUPT_HARD);
70 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
75 static uint64_t cchip_read(void *opaque, hwaddr addr, unsigned size)
77 CPUAlphaState *env = cpu_single_env;
78 TyphoonState *s = opaque;
88 /* CSC: Cchip System Configuration Register. */
89 /* All sorts of data here; probably the only thing relevant is
90 PIP<14> Pchip 1 Present = 0. */
94 /* MTR: Memory Timing Register. */
95 /* All sorts of stuff related to real DRAM. */
99 /* MISC: Miscellaneous Register. */
100 cpu = ENV_GET_CPU(env);
101 ret = s->cchip.misc | (cpu->cpu_index & 3);
105 /* MPD: Memory Presence Detect Register. */
108 case 0x0100: /* AAR0 */
109 case 0x0140: /* AAR1 */
110 case 0x0180: /* AAR2 */
111 case 0x01c0: /* AAR3 */
112 /* AAR: Array Address Register. */
113 /* All sorts of information about DRAM. */
117 /* DIM0: Device Interrupt Mask Register, CPU0. */
118 ret = s->cchip.dim[0];
121 /* DIM1: Device Interrupt Mask Register, CPU1. */
122 ret = s->cchip.dim[1];
125 /* DIR0: Device Interrupt Request Register, CPU0. */
126 ret = s->cchip.dim[0] & s->cchip.drir;
129 /* DIR1: Device Interrupt Request Register, CPU1. */
130 ret = s->cchip.dim[1] & s->cchip.drir;
133 /* DRIR: Device Raw Interrupt Request Register. */
138 /* PRBEN: Probe Enable Register. */
142 /* IIC0: Interval Ignore Count Register, CPU0. */
143 ret = s->cchip.iic[0];
146 /* IIC1: Interval Ignore Count Register, CPU1. */
147 ret = s->cchip.iic[1];
150 case 0x0400: /* MPR0 */
151 case 0x0440: /* MPR1 */
152 case 0x0480: /* MPR2 */
153 case 0x04c0: /* MPR3 */
154 /* MPR: Memory Programming Register. */
158 /* TTR: TIGbus Timing Register. */
159 /* All sorts of stuff related to interrupt delivery timings. */
162 /* TDR: TIGbug Device Timing Register. */
166 /* DIM2: Device Interrupt Mask Register, CPU2. */
167 ret = s->cchip.dim[2];
170 /* DIM3: Device Interrupt Mask Register, CPU3. */
171 ret = s->cchip.dim[3];
174 /* DIR2: Device Interrupt Request Register, CPU2. */
175 ret = s->cchip.dim[2] & s->cchip.drir;
178 /* DIR3: Device Interrupt Request Register, CPU3. */
179 ret = s->cchip.dim[3] & s->cchip.drir;
183 /* IIC2: Interval Ignore Count Register, CPU2. */
184 ret = s->cchip.iic[2];
187 /* IIC3: Interval Ignore Count Register, CPU3. */
188 ret = s->cchip.iic[3];
192 /* PWR: Power Management Control. */
195 case 0x0c00: /* CMONCTLA */
196 case 0x0c40: /* CMONCTLB */
197 case 0x0c80: /* CMONCNT01 */
198 case 0x0cc0: /* CMONCNT23 */
202 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, size);
206 s->latch_tmp = ret >> 32;
210 static uint64_t dchip_read(void *opaque, hwaddr addr, unsigned size)
212 /* Skip this. It's all related to DRAM timing and setup. */
216 static uint64_t pchip_read(void *opaque, hwaddr addr, unsigned size)
218 TyphoonState *s = opaque;
227 /* WSBA0: Window Space Base Address Register. */
228 ret = s->pchip.win[0].base_addr;
232 ret = s->pchip.win[1].base_addr;
236 ret = s->pchip.win[2].base_addr;
240 ret = s->pchip.win[3].base_addr;
244 /* WSM0: Window Space Mask Register. */
245 ret = s->pchip.win[0].mask;
249 ret = s->pchip.win[1].mask;
253 ret = s->pchip.win[2].mask;
257 ret = s->pchip.win[3].mask;
261 /* TBA0: Translated Base Address Register. */
262 ret = (uint64_t)s->pchip.win[0].translated_base_pfn << 10;
266 ret = (uint64_t)s->pchip.win[1].translated_base_pfn << 10;
270 ret = (uint64_t)s->pchip.win[2].translated_base_pfn << 10;
274 ret = (uint64_t)s->pchip.win[3].translated_base_pfn << 10;
278 /* PCTL: Pchip Control Register. */
282 /* PLAT: Pchip Master Latency Register. */
285 /* PERROR: Pchip Error Register. */
288 /* PERRMASK: Pchip Error Mask Register. */
291 /* PERRSET: Pchip Error Set Register. */
294 /* TLBIV: Translation Buffer Invalidate Virtual Register (WO). */
297 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
299 case 0x0500: /* PMONCTL */
300 case 0x0540: /* PMONCNT */
301 case 0x0800: /* SPRST */
305 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, size);
309 s->latch_tmp = ret >> 32;
313 static void cchip_write(void *opaque, hwaddr addr,
314 uint64_t v32, unsigned size)
316 TyphoonState *s = opaque;
317 uint64_t val, oldval, newval;
320 val = v32 << 32 | s->latch_tmp;
329 /* CSC: Cchip System Configuration Register. */
330 /* All sorts of data here; nothing relevant RW. */
334 /* MTR: Memory Timing Register. */
335 /* All sorts of stuff related to real DRAM. */
339 /* MISC: Miscellaneous Register. */
340 newval = oldval = s->cchip.misc;
341 newval &= ~(val & 0x10000ff0); /* W1C fields */
342 if (val & 0x100000) {
343 newval &= ~0xff0000ull; /* ACL clears ABT and ABW */
345 newval |= val & 0x00f00000; /* ABT field is W1S */
346 if ((newval & 0xf0000) == 0) {
347 newval |= val & 0xf0000; /* ABW field is W1S iff zero */
350 newval |= (val & 0xf000) >> 4; /* IPREQ field sets IPINTR. */
352 newval &= ~0xf0000000000ull; /* WO and RW fields */
353 newval |= val & 0xf0000000000ull;
354 s->cchip.misc = newval;
356 /* Pass on changes to IPI and ITI state. */
357 if ((newval ^ oldval) & 0xff0) {
359 for (i = 0; i < 4; ++i) {
360 AlphaCPU *cpu = s->cchip.cpu[i];
362 CPUAlphaState *env = &cpu->env;
363 CPUState *cs = CPU(cpu);
364 /* IPI can be either cleared or set by the write. */
365 if (newval & (1 << (i + 8))) {
366 cpu_interrupt(env, CPU_INTERRUPT_SMP);
368 cpu_reset_interrupt(cs, CPU_INTERRUPT_SMP);
371 /* ITI can only be cleared by the write. */
372 if ((newval & (1 << (i + 4))) == 0) {
373 cpu_reset_interrupt(cs, CPU_INTERRUPT_TIMER);
381 /* MPD: Memory Presence Detect Register. */
384 case 0x0100: /* AAR0 */
385 case 0x0140: /* AAR1 */
386 case 0x0180: /* AAR2 */
387 case 0x01c0: /* AAR3 */
388 /* AAR: Array Address Register. */
389 /* All sorts of information about DRAM. */
392 case 0x0200: /* DIM0 */
393 /* DIM: Device Interrupt Mask Register, CPU0. */
394 s->cchip.dim[0] = val;
395 cpu_irq_change(s->cchip.cpu[0], val & s->cchip.drir);
397 case 0x0240: /* DIM1 */
398 /* DIM: Device Interrupt Mask Register, CPU1. */
399 s->cchip.dim[0] = val;
400 cpu_irq_change(s->cchip.cpu[1], val & s->cchip.drir);
403 case 0x0280: /* DIR0 (RO) */
404 case 0x02c0: /* DIR1 (RO) */
405 case 0x0300: /* DRIR (RO) */
409 /* PRBEN: Probe Enable Register. */
412 case 0x0380: /* IIC0 */
413 s->cchip.iic[0] = val & 0xffffff;
415 case 0x03c0: /* IIC1 */
416 s->cchip.iic[1] = val & 0xffffff;
419 case 0x0400: /* MPR0 */
420 case 0x0440: /* MPR1 */
421 case 0x0480: /* MPR2 */
422 case 0x04c0: /* MPR3 */
423 /* MPR: Memory Programming Register. */
427 /* TTR: TIGbus Timing Register. */
428 /* All sorts of stuff related to interrupt delivery timings. */
431 /* TDR: TIGbug Device Timing Register. */
435 /* DIM2: Device Interrupt Mask Register, CPU2. */
436 s->cchip.dim[2] = val;
437 cpu_irq_change(s->cchip.cpu[2], val & s->cchip.drir);
440 /* DIM3: Device Interrupt Mask Register, CPU3. */
441 s->cchip.dim[3] = val;
442 cpu_irq_change(s->cchip.cpu[3], val & s->cchip.drir);
445 case 0x0680: /* DIR2 (RO) */
446 case 0x06c0: /* DIR3 (RO) */
449 case 0x0700: /* IIC2 */
450 s->cchip.iic[2] = val & 0xffffff;
452 case 0x0740: /* IIC3 */
453 s->cchip.iic[3] = val & 0xffffff;
457 /* PWR: Power Management Control. */
460 case 0x0c00: /* CMONCTLA */
461 case 0x0c40: /* CMONCTLB */
462 case 0x0c80: /* CMONCNT01 */
463 case 0x0cc0: /* CMONCNT23 */
467 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, size);
472 static void dchip_write(void *opaque, hwaddr addr,
473 uint64_t val, unsigned size)
475 /* Skip this. It's all related to DRAM timing and setup. */
478 static void pchip_write(void *opaque, hwaddr addr,
479 uint64_t v32, unsigned size)
481 TyphoonState *s = opaque;
482 uint64_t val, oldval;
485 val = v32 << 32 | s->latch_tmp;
494 /* WSBA0: Window Space Base Address Register. */
495 s->pchip.win[0].base_addr = val;
499 s->pchip.win[1].base_addr = val;
503 s->pchip.win[2].base_addr = val;
507 s->pchip.win[3].base_addr = val;
511 /* WSM0: Window Space Mask Register. */
512 s->pchip.win[0].mask = val;
516 s->pchip.win[1].mask = val;
520 s->pchip.win[2].mask = val;
524 s->pchip.win[3].mask = val;
528 /* TBA0: Translated Base Address Register. */
529 s->pchip.win[0].translated_base_pfn = val >> 10;
533 s->pchip.win[1].translated_base_pfn = val >> 10;
537 s->pchip.win[2].translated_base_pfn = val >> 10;
541 s->pchip.win[3].translated_base_pfn = val >> 10;
545 /* PCTL: Pchip Control Register. */
546 oldval = s->pchip.ctl;
547 oldval &= ~0x00001cff0fc7ffull; /* RW fields */
548 oldval |= val & 0x00001cff0fc7ffull;
550 s->pchip.ctl = oldval;
554 /* PLAT: Pchip Master Latency Register. */
557 /* PERROR: Pchip Error Register. */
560 /* PERRMASK: Pchip Error Mask Register. */
563 /* PERRSET: Pchip Error Set Register. */
567 /* TLBIV: Translation Buffer Invalidate Virtual Register. */
571 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
583 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, size);
588 static const MemoryRegionOps cchip_ops = {
590 .write = cchip_write,
591 .endianness = DEVICE_LITTLE_ENDIAN,
593 .min_access_size = 4, /* ??? Should be 8. */
594 .max_access_size = 8,
597 .min_access_size = 4,
598 .max_access_size = 4,
602 static const MemoryRegionOps dchip_ops = {
604 .write = dchip_write,
605 .endianness = DEVICE_LITTLE_ENDIAN,
607 .min_access_size = 4, /* ??? Should be 8. */
608 .max_access_size = 8,
611 .min_access_size = 4,
612 .max_access_size = 8,
616 static const MemoryRegionOps pchip_ops = {
618 .write = pchip_write,
619 .endianness = DEVICE_LITTLE_ENDIAN,
621 .min_access_size = 4, /* ??? Should be 8. */
622 .max_access_size = 8,
625 .min_access_size = 4,
626 .max_access_size = 4,
630 static void typhoon_set_irq(void *opaque, int irq, int level)
632 TyphoonState *s = opaque;
636 /* Set/Reset the bit in CCHIP.DRIR based on IRQ+LEVEL. */
637 drir = s->cchip.drir;
641 drir &= ~(1ull << irq);
643 s->cchip.drir = drir;
645 for (i = 0; i < 4; ++i) {
646 cpu_irq_change(s->cchip.cpu[i], s->cchip.dim[i] & drir);
650 static void typhoon_set_isa_irq(void *opaque, int irq, int level)
652 typhoon_set_irq(opaque, 55, level);
655 static void typhoon_set_timer_irq(void *opaque, int irq, int level)
657 TyphoonState *s = opaque;
660 /* Thankfully, the mc146818rtc code doesn't track the IRQ state,
661 and so we don't have to worry about missing interrupts just
662 because we never actually ACK the interrupt. Just ignore any
663 case of the interrupt level going low. */
668 /* Deliver the interrupt to each CPU, considering each CPU's IIC. */
669 for (i = 0; i < 4; ++i) {
670 AlphaCPU *cpu = s->cchip.cpu[i];
672 uint32_t iic = s->cchip.iic[i];
674 /* ??? The verbage in Section 10.2.2.10 isn't 100% clear.
675 Bit 24 is the OverFlow bit, RO, and set when the count
676 decrements past 0. When is OF cleared? My guess is that
677 OF is actually cleared when the IIC is written, and that
678 the ICNT field always decrements. At least, that's an
679 interpretation that makes sense, and "allows the CPU to
680 determine exactly how mant interval timer ticks were
681 skipped". At least within the next 4M ticks... */
683 iic = ((iic - 1) & 0x1ffffff) | (iic & 0x1000000);
684 s->cchip.iic[i] = iic;
686 if (iic & 0x1000000) {
687 /* Set the ITI bit for this cpu. */
688 s->cchip.misc |= 1 << (i + 4);
689 /* And signal the interrupt. */
690 cpu_interrupt(&cpu->env, CPU_INTERRUPT_TIMER);
696 static void typhoon_alarm_timer(void *opaque)
698 TyphoonState *s = (TyphoonState *)((uintptr_t)opaque & ~3);
699 int cpu = (uintptr_t)opaque & 3;
701 /* Set the ITI bit for this cpu. */
702 s->cchip.misc |= 1 << (cpu + 4);
703 cpu_interrupt(&s->cchip.cpu[cpu]->env, CPU_INTERRUPT_TIMER);
706 PCIBus *typhoon_init(ram_addr_t ram_size, ISABus **isa_bus,
708 AlphaCPU *cpus[4], pci_map_irq_fn sys_map_irq)
710 const uint64_t MB = 1024 * 1024;
711 const uint64_t GB = 1024 * MB;
712 MemoryRegion *addr_space = get_system_memory();
713 MemoryRegion *addr_space_io = get_system_io();
720 dev = qdev_create(NULL, TYPE_TYPHOON_PCI_HOST_BRIDGE);
721 qdev_init_nofail(dev);
723 s = TYPHOON_PCI_HOST_BRIDGE(dev);
724 phb = PCI_HOST_BRIDGE(dev);
726 /* Remember the CPUs so that we can deliver interrupts to them. */
727 for (i = 0; i < 4; i++) {
728 AlphaCPU *cpu = cpus[i];
729 s->cchip.cpu[i] = cpu;
731 cpu->alarm_timer = qemu_new_timer_ns(rtc_clock,
733 (void *)((uintptr_t)s + i));
737 *p_rtc_irq = *qemu_allocate_irqs(typhoon_set_timer_irq, s, 1);
739 /* Main memory region, 0x00.0000.0000. Real hardware supports 32GB,
740 but the address space hole reserved at this point is 8TB. */
741 memory_region_init_ram(&s->ram_region, "ram", ram_size);
742 vmstate_register_ram_global(&s->ram_region);
743 memory_region_add_subregion(addr_space, 0, &s->ram_region);
745 /* TIGbus, 0x801.0000.0000, 1GB. */
746 /* ??? The TIGbus is used for delivering interrupts, and access to
747 the flash ROM. I'm not sure that we need to implement it at all. */
749 /* Pchip0 CSRs, 0x801.8000.0000, 256MB. */
750 memory_region_init_io(&s->pchip.region, &pchip_ops, s, "pchip0", 256*MB);
751 memory_region_add_subregion(addr_space, 0x80180000000ULL,
754 /* Cchip CSRs, 0x801.A000.0000, 256MB. */
755 memory_region_init_io(&s->cchip.region, &cchip_ops, s, "cchip0", 256*MB);
756 memory_region_add_subregion(addr_space, 0x801a0000000ULL,
759 /* Dchip CSRs, 0x801.B000.0000, 256MB. */
760 memory_region_init_io(&s->dchip_region, &dchip_ops, s, "dchip0", 256*MB);
761 memory_region_add_subregion(addr_space, 0x801b0000000ULL,
764 /* Pchip0 PCI memory, 0x800.0000.0000, 4GB. */
765 memory_region_init(&s->pchip.reg_mem, "pci0-mem", 4*GB);
766 memory_region_add_subregion(addr_space, 0x80000000000ULL,
769 /* Pchip0 PCI I/O, 0x801.FC00.0000, 32MB. */
770 /* ??? Ideally we drop the "system" i/o space on the floor and give the
771 PCI subsystem the full address space reserved by the chipset.
772 We can't do that until the MEM and IO paths in memory.c are unified. */
773 memory_region_init_io(&s->pchip.reg_io, &alpha_pci_bw_io_ops, NULL,
775 memory_region_add_subregion(addr_space, 0x801fc000000ULL,
778 b = pci_register_bus(dev, "pci",
779 typhoon_set_irq, sys_map_irq, s,
780 &s->pchip.reg_mem, addr_space_io, 0, 64);
783 /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB. */
784 memory_region_init_io(&s->pchip.reg_iack, &alpha_pci_iack_ops, b,
786 memory_region_add_subregion(addr_space, 0x801f8000000ULL,
789 /* Pchip0 PCI configuration, 0x801.FE00.0000, 16MB. */
790 memory_region_init_io(&s->pchip.reg_conf, &alpha_pci_conf1_ops, b,
792 memory_region_add_subregion(addr_space, 0x801fe000000ULL,
795 /* For the record, these are the mappings for the second PCI bus.
796 We can get away with not implementing them because we indicate
797 via the Cchip.CSC<PIP> bit that Pchip1 is not present. */
798 /* Pchip1 PCI memory, 0x802.0000.0000, 4GB. */
799 /* Pchip1 CSRs, 0x802.8000.0000, 256MB. */
800 /* Pchip1 PCI special/interrupt acknowledge, 0x802.F800.0000, 64MB. */
801 /* Pchip1 PCI I/O, 0x802.FC00.0000, 32MB. */
802 /* Pchip1 PCI configuration, 0x802.FE00.0000, 16MB. */
804 /* Init the ISA bus. */
805 /* ??? Technically there should be a cy82c693ub pci-isa bridge. */
807 qemu_irq isa_pci_irq, *isa_irqs;
809 *isa_bus = isa_bus_new(NULL, addr_space_io);
810 isa_pci_irq = *qemu_allocate_irqs(typhoon_set_isa_irq, s, 1);
811 isa_irqs = i8259_init(*isa_bus, isa_pci_irq);
812 isa_bus_irqs(*isa_bus, isa_irqs);
818 static int typhoon_pcihost_init(SysBusDevice *dev)
823 static void typhoon_pcihost_class_init(ObjectClass *klass, void *data)
825 DeviceClass *dc = DEVICE_CLASS(klass);
826 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
828 k->init = typhoon_pcihost_init;
832 static const TypeInfo typhoon_pcihost_info = {
833 .name = TYPE_TYPHOON_PCI_HOST_BRIDGE,
834 .parent = TYPE_PCI_HOST_BRIDGE,
835 .instance_size = sizeof(TyphoonState),
836 .class_init = typhoon_pcihost_class_init,
839 static void typhoon_register_types(void)
841 type_register_static(&typhoon_pcihost_info);
844 type_init(typhoon_register_types)