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.
11 #include "hw/devices.h"
12 #include "sysemu/sysemu.h"
13 #include "alpha_sys.h"
14 #include "exec/address-spaces.h"
17 #define TYPE_TYPHOON_PCI_HOST_BRIDGE "typhoon-pcihost"
19 typedef struct TyphoonCchip
{
28 typedef struct TyphoonWindow
{
31 uint32_t translated_base_pfn
;
34 typedef struct TyphoonPchip
{
36 MemoryRegion reg_iack
;
39 MemoryRegion reg_conf
;
44 #define TYPHOON_PCI_HOST_BRIDGE(obj) \
45 OBJECT_CHECK(TyphoonState, (obj), TYPE_TYPHOON_PCI_HOST_BRIDGE)
47 typedef struct TyphoonState
{
48 PCIHostState parent_obj
;
52 MemoryRegion dchip_region
;
53 MemoryRegion ram_region
;
55 /* QEMU emulation state. */
59 /* Called when one of DRIR or DIM changes. */
60 static void cpu_irq_change(AlphaCPU
*cpu
, uint64_t req
)
62 /* If there are any non-masked interrupts, tell the cpu. */
64 CPUState
*cs
= CPU(cpu
);
66 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
68 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
73 static uint64_t cchip_read(void *opaque
, hwaddr addr
, unsigned size
)
75 CPUAlphaState
*env
= cpu_single_env
;
76 TyphoonState
*s
= opaque
;
86 /* CSC: Cchip System Configuration Register. */
87 /* All sorts of data here; probably the only thing relevant is
88 PIP<14> Pchip 1 Present = 0. */
92 /* MTR: Memory Timing Register. */
93 /* All sorts of stuff related to real DRAM. */
97 /* MISC: Miscellaneous Register. */
98 cpu
= ENV_GET_CPU(env
);
99 ret
= s
->cchip
.misc
| (cpu
->cpu_index
& 3);
103 /* MPD: Memory Presence Detect Register. */
106 case 0x0100: /* AAR0 */
107 case 0x0140: /* AAR1 */
108 case 0x0180: /* AAR2 */
109 case 0x01c0: /* AAR3 */
110 /* AAR: Array Address Register. */
111 /* All sorts of information about DRAM. */
115 /* DIM0: Device Interrupt Mask Register, CPU0. */
116 ret
= s
->cchip
.dim
[0];
119 /* DIM1: Device Interrupt Mask Register, CPU1. */
120 ret
= s
->cchip
.dim
[1];
123 /* DIR0: Device Interrupt Request Register, CPU0. */
124 ret
= s
->cchip
.dim
[0] & s
->cchip
.drir
;
127 /* DIR1: Device Interrupt Request Register, CPU1. */
128 ret
= s
->cchip
.dim
[1] & s
->cchip
.drir
;
131 /* DRIR: Device Raw Interrupt Request Register. */
136 /* PRBEN: Probe Enable Register. */
140 /* IIC0: Interval Ignore Count Register, CPU0. */
141 ret
= s
->cchip
.iic
[0];
144 /* IIC1: Interval Ignore Count Register, CPU1. */
145 ret
= s
->cchip
.iic
[1];
148 case 0x0400: /* MPR0 */
149 case 0x0440: /* MPR1 */
150 case 0x0480: /* MPR2 */
151 case 0x04c0: /* MPR3 */
152 /* MPR: Memory Programming Register. */
156 /* TTR: TIGbus Timing Register. */
157 /* All sorts of stuff related to interrupt delivery timings. */
160 /* TDR: TIGbug Device Timing Register. */
164 /* DIM2: Device Interrupt Mask Register, CPU2. */
165 ret
= s
->cchip
.dim
[2];
168 /* DIM3: Device Interrupt Mask Register, CPU3. */
169 ret
= s
->cchip
.dim
[3];
172 /* DIR2: Device Interrupt Request Register, CPU2. */
173 ret
= s
->cchip
.dim
[2] & s
->cchip
.drir
;
176 /* DIR3: Device Interrupt Request Register, CPU3. */
177 ret
= s
->cchip
.dim
[3] & s
->cchip
.drir
;
181 /* IIC2: Interval Ignore Count Register, CPU2. */
182 ret
= s
->cchip
.iic
[2];
185 /* IIC3: Interval Ignore Count Register, CPU3. */
186 ret
= s
->cchip
.iic
[3];
190 /* PWR: Power Management Control. */
193 case 0x0c00: /* CMONCTLA */
194 case 0x0c40: /* CMONCTLB */
195 case 0x0c80: /* CMONCNT01 */
196 case 0x0cc0: /* CMONCNT23 */
200 cpu
= CPU(alpha_env_get_cpu(cpu_single_env
));
201 cpu_unassigned_access(cpu
, addr
, false, false, 0, size
);
205 s
->latch_tmp
= ret
>> 32;
209 static uint64_t dchip_read(void *opaque
, hwaddr addr
, unsigned size
)
211 /* Skip this. It's all related to DRAM timing and setup. */
215 static uint64_t pchip_read(void *opaque
, hwaddr addr
, unsigned size
)
217 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 cs
= CPU(alpha_env_get_cpu(cpu_single_env
));
306 cpu_unassigned_access(cs
, addr
, false, false, 0, size
);
310 s
->latch_tmp
= ret
>> 32;
314 static void cchip_write(void *opaque
, hwaddr addr
,
315 uint64_t v32
, unsigned size
)
317 TyphoonState
*s
= opaque
;
318 CPUState
*cpu_single_cpu
= CPU(alpha_env_get_cpu(cpu_single_env
));
319 uint64_t val
, oldval
, newval
;
322 val
= v32
<< 32 | s
->latch_tmp
;
331 /* CSC: Cchip System Configuration Register. */
332 /* All sorts of data here; nothing relevant RW. */
336 /* MTR: Memory Timing Register. */
337 /* All sorts of stuff related to real DRAM. */
341 /* MISC: Miscellaneous Register. */
342 newval
= oldval
= s
->cchip
.misc
;
343 newval
&= ~(val
& 0x10000ff0); /* W1C fields */
344 if (val
& 0x100000) {
345 newval
&= ~0xff0000ull
; /* ACL clears ABT and ABW */
347 newval
|= val
& 0x00f00000; /* ABT field is W1S */
348 if ((newval
& 0xf0000) == 0) {
349 newval
|= val
& 0xf0000; /* ABW field is W1S iff zero */
352 newval
|= (val
& 0xf000) >> 4; /* IPREQ field sets IPINTR. */
354 newval
&= ~0xf0000000000ull
; /* WO and RW fields */
355 newval
|= val
& 0xf0000000000ull
;
356 s
->cchip
.misc
= newval
;
358 /* Pass on changes to IPI and ITI state. */
359 if ((newval
^ oldval
) & 0xff0) {
361 for (i
= 0; i
< 4; ++i
) {
362 AlphaCPU
*cpu
= s
->cchip
.cpu
[i
];
364 CPUState
*cs
= CPU(cpu
);
365 /* IPI can be either cleared or set by the write. */
366 if (newval
& (1 << (i
+ 8))) {
367 cpu_interrupt(cs
, CPU_INTERRUPT_SMP
);
369 cpu_reset_interrupt(cs
, CPU_INTERRUPT_SMP
);
372 /* ITI can only be cleared by the write. */
373 if ((newval
& (1 << (i
+ 4))) == 0) {
374 cpu_reset_interrupt(cs
, CPU_INTERRUPT_TIMER
);
382 /* MPD: Memory Presence Detect Register. */
385 case 0x0100: /* AAR0 */
386 case 0x0140: /* AAR1 */
387 case 0x0180: /* AAR2 */
388 case 0x01c0: /* AAR3 */
389 /* AAR: Array Address Register. */
390 /* All sorts of information about DRAM. */
393 case 0x0200: /* DIM0 */
394 /* DIM: Device Interrupt Mask Register, CPU0. */
395 s
->cchip
.dim
[0] = val
;
396 cpu_irq_change(s
->cchip
.cpu
[0], val
& s
->cchip
.drir
);
398 case 0x0240: /* DIM1 */
399 /* DIM: Device Interrupt Mask Register, CPU1. */
400 s
->cchip
.dim
[0] = val
;
401 cpu_irq_change(s
->cchip
.cpu
[1], val
& s
->cchip
.drir
);
404 case 0x0280: /* DIR0 (RO) */
405 case 0x02c0: /* DIR1 (RO) */
406 case 0x0300: /* DRIR (RO) */
410 /* PRBEN: Probe Enable Register. */
413 case 0x0380: /* IIC0 */
414 s
->cchip
.iic
[0] = val
& 0xffffff;
416 case 0x03c0: /* IIC1 */
417 s
->cchip
.iic
[1] = val
& 0xffffff;
420 case 0x0400: /* MPR0 */
421 case 0x0440: /* MPR1 */
422 case 0x0480: /* MPR2 */
423 case 0x04c0: /* MPR3 */
424 /* MPR: Memory Programming Register. */
428 /* TTR: TIGbus Timing Register. */
429 /* All sorts of stuff related to interrupt delivery timings. */
432 /* TDR: TIGbug Device Timing Register. */
436 /* DIM2: Device Interrupt Mask Register, CPU2. */
437 s
->cchip
.dim
[2] = val
;
438 cpu_irq_change(s
->cchip
.cpu
[2], val
& s
->cchip
.drir
);
441 /* DIM3: Device Interrupt Mask Register, CPU3. */
442 s
->cchip
.dim
[3] = val
;
443 cpu_irq_change(s
->cchip
.cpu
[3], val
& s
->cchip
.drir
);
446 case 0x0680: /* DIR2 (RO) */
447 case 0x06c0: /* DIR3 (RO) */
450 case 0x0700: /* IIC2 */
451 s
->cchip
.iic
[2] = val
& 0xffffff;
453 case 0x0740: /* IIC3 */
454 s
->cchip
.iic
[3] = val
& 0xffffff;
458 /* PWR: Power Management Control. */
461 case 0x0c00: /* CMONCTLA */
462 case 0x0c40: /* CMONCTLB */
463 case 0x0c80: /* CMONCNT01 */
464 case 0x0cc0: /* CMONCNT23 */
468 cpu_unassigned_access(cpu_single_cpu
, addr
, true, false, 0, size
);
473 static void dchip_write(void *opaque
, hwaddr addr
,
474 uint64_t val
, unsigned size
)
476 /* Skip this. It's all related to DRAM timing and setup. */
479 static void pchip_write(void *opaque
, hwaddr addr
,
480 uint64_t v32
, unsigned size
)
482 TyphoonState
*s
= opaque
;
484 uint64_t val
, oldval
;
487 val
= v32
<< 32 | s
->latch_tmp
;
496 /* WSBA0: Window Space Base Address Register. */
497 s
->pchip
.win
[0].base_addr
= val
;
501 s
->pchip
.win
[1].base_addr
= val
;
505 s
->pchip
.win
[2].base_addr
= val
;
509 s
->pchip
.win
[3].base_addr
= val
;
513 /* WSM0: Window Space Mask Register. */
514 s
->pchip
.win
[0].mask
= val
;
518 s
->pchip
.win
[1].mask
= val
;
522 s
->pchip
.win
[2].mask
= val
;
526 s
->pchip
.win
[3].mask
= val
;
530 /* TBA0: Translated Base Address Register. */
531 s
->pchip
.win
[0].translated_base_pfn
= val
>> 10;
535 s
->pchip
.win
[1].translated_base_pfn
= val
>> 10;
539 s
->pchip
.win
[2].translated_base_pfn
= val
>> 10;
543 s
->pchip
.win
[3].translated_base_pfn
= val
>> 10;
547 /* PCTL: Pchip Control Register. */
548 oldval
= s
->pchip
.ctl
;
549 oldval
&= ~0x00001cff0fc7ffull
; /* RW fields */
550 oldval
|= val
& 0x00001cff0fc7ffull
;
552 s
->pchip
.ctl
= oldval
;
556 /* PLAT: Pchip Master Latency Register. */
559 /* PERROR: Pchip Error Register. */
562 /* PERRMASK: Pchip Error Mask Register. */
565 /* PERRSET: Pchip Error Set Register. */
569 /* TLBIV: Translation Buffer Invalidate Virtual Register. */
573 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
585 cs
= CPU(alpha_env_get_cpu(cpu_single_env
));
586 cpu_unassigned_access(cs
, addr
, true, false, 0, size
);
591 static const MemoryRegionOps cchip_ops
= {
593 .write
= cchip_write
,
594 .endianness
= DEVICE_LITTLE_ENDIAN
,
596 .min_access_size
= 4, /* ??? Should be 8. */
597 .max_access_size
= 8,
600 .min_access_size
= 4,
601 .max_access_size
= 4,
605 static const MemoryRegionOps dchip_ops
= {
607 .write
= dchip_write
,
608 .endianness
= DEVICE_LITTLE_ENDIAN
,
610 .min_access_size
= 4, /* ??? Should be 8. */
611 .max_access_size
= 8,
614 .min_access_size
= 4,
615 .max_access_size
= 8,
619 static const MemoryRegionOps pchip_ops
= {
621 .write
= pchip_write
,
622 .endianness
= DEVICE_LITTLE_ENDIAN
,
624 .min_access_size
= 4, /* ??? Should be 8. */
625 .max_access_size
= 8,
628 .min_access_size
= 4,
629 .max_access_size
= 4,
633 static void typhoon_set_irq(void *opaque
, int irq
, int level
)
635 TyphoonState
*s
= opaque
;
639 /* Set/Reset the bit in CCHIP.DRIR based on IRQ+LEVEL. */
640 drir
= s
->cchip
.drir
;
644 drir
&= ~(1ull << irq
);
646 s
->cchip
.drir
= drir
;
648 for (i
= 0; i
< 4; ++i
) {
649 cpu_irq_change(s
->cchip
.cpu
[i
], s
->cchip
.dim
[i
] & drir
);
653 static void typhoon_set_isa_irq(void *opaque
, int irq
, int level
)
655 typhoon_set_irq(opaque
, 55, level
);
658 static void typhoon_set_timer_irq(void *opaque
, int irq
, int level
)
660 TyphoonState
*s
= opaque
;
663 /* Thankfully, the mc146818rtc code doesn't track the IRQ state,
664 and so we don't have to worry about missing interrupts just
665 because we never actually ACK the interrupt. Just ignore any
666 case of the interrupt level going low. */
671 /* Deliver the interrupt to each CPU, considering each CPU's IIC. */
672 for (i
= 0; i
< 4; ++i
) {
673 AlphaCPU
*cpu
= s
->cchip
.cpu
[i
];
675 uint32_t iic
= s
->cchip
.iic
[i
];
677 /* ??? The verbage in Section 10.2.2.10 isn't 100% clear.
678 Bit 24 is the OverFlow bit, RO, and set when the count
679 decrements past 0. When is OF cleared? My guess is that
680 OF is actually cleared when the IIC is written, and that
681 the ICNT field always decrements. At least, that's an
682 interpretation that makes sense, and "allows the CPU to
683 determine exactly how mant interval timer ticks were
684 skipped". At least within the next 4M ticks... */
686 iic
= ((iic
- 1) & 0x1ffffff) | (iic
& 0x1000000);
687 s
->cchip
.iic
[i
] = iic
;
689 if (iic
& 0x1000000) {
690 /* Set the ITI bit for this cpu. */
691 s
->cchip
.misc
|= 1 << (i
+ 4);
692 /* And signal the interrupt. */
693 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_TIMER
);
699 static void typhoon_alarm_timer(void *opaque
)
701 TyphoonState
*s
= (TyphoonState
*)((uintptr_t)opaque
& ~3);
702 int cpu
= (uintptr_t)opaque
& 3;
704 /* Set the ITI bit for this cpu. */
705 s
->cchip
.misc
|= 1 << (cpu
+ 4);
706 cpu_interrupt(CPU(s
->cchip
.cpu
[cpu
]), CPU_INTERRUPT_TIMER
);
709 PCIBus
*typhoon_init(ram_addr_t ram_size
, ISABus
**isa_bus
,
711 AlphaCPU
*cpus
[4], pci_map_irq_fn sys_map_irq
)
713 const uint64_t MB
= 1024 * 1024;
714 const uint64_t GB
= 1024 * MB
;
715 MemoryRegion
*addr_space
= get_system_memory();
716 MemoryRegion
*addr_space_io
= get_system_io();
723 dev
= qdev_create(NULL
, TYPE_TYPHOON_PCI_HOST_BRIDGE
);
724 qdev_init_nofail(dev
);
726 s
= TYPHOON_PCI_HOST_BRIDGE(dev
);
727 phb
= PCI_HOST_BRIDGE(dev
);
729 /* Remember the CPUs so that we can deliver interrupts to them. */
730 for (i
= 0; i
< 4; i
++) {
731 AlphaCPU
*cpu
= cpus
[i
];
732 s
->cchip
.cpu
[i
] = cpu
;
734 cpu
->alarm_timer
= qemu_new_timer_ns(rtc_clock
,
736 (void *)((uintptr_t)s
+ i
));
740 *p_rtc_irq
= *qemu_allocate_irqs(typhoon_set_timer_irq
, s
, 1);
742 /* Main memory region, 0x00.0000.0000. Real hardware supports 32GB,
743 but the address space hole reserved at this point is 8TB. */
744 memory_region_init_ram(&s
->ram_region
, NULL
, "ram", ram_size
);
745 vmstate_register_ram_global(&s
->ram_region
);
746 memory_region_add_subregion(addr_space
, 0, &s
->ram_region
);
748 /* TIGbus, 0x801.0000.0000, 1GB. */
749 /* ??? The TIGbus is used for delivering interrupts, and access to
750 the flash ROM. I'm not sure that we need to implement it at all. */
752 /* Pchip0 CSRs, 0x801.8000.0000, 256MB. */
753 memory_region_init_io(&s
->pchip
.region
, NULL
, &pchip_ops
, s
, "pchip0", 256*MB
);
754 memory_region_add_subregion(addr_space
, 0x80180000000ULL
,
757 /* Cchip CSRs, 0x801.A000.0000, 256MB. */
758 memory_region_init_io(&s
->cchip
.region
, NULL
, &cchip_ops
, s
, "cchip0", 256*MB
);
759 memory_region_add_subregion(addr_space
, 0x801a0000000ULL
,
762 /* Dchip CSRs, 0x801.B000.0000, 256MB. */
763 memory_region_init_io(&s
->dchip_region
, NULL
, &dchip_ops
, s
, "dchip0", 256*MB
);
764 memory_region_add_subregion(addr_space
, 0x801b0000000ULL
,
767 /* Pchip0 PCI memory, 0x800.0000.0000, 4GB. */
768 memory_region_init(&s
->pchip
.reg_mem
, NULL
, "pci0-mem", 4*GB
);
769 memory_region_add_subregion(addr_space
, 0x80000000000ULL
,
772 /* Pchip0 PCI I/O, 0x801.FC00.0000, 32MB. */
773 /* ??? Ideally we drop the "system" i/o space on the floor and give the
774 PCI subsystem the full address space reserved by the chipset.
775 We can't do that until the MEM and IO paths in memory.c are unified. */
776 memory_region_init_io(&s
->pchip
.reg_io
, NULL
, &alpha_pci_bw_io_ops
, NULL
,
778 memory_region_add_subregion(addr_space
, 0x801fc000000ULL
,
781 b
= pci_register_bus(dev
, "pci",
782 typhoon_set_irq
, sys_map_irq
, s
,
783 &s
->pchip
.reg_mem
, addr_space_io
, 0, 64, TYPE_PCI_BUS
);
786 /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB. */
787 memory_region_init_io(&s
->pchip
.reg_iack
, NULL
, &alpha_pci_iack_ops
, b
,
789 memory_region_add_subregion(addr_space
, 0x801f8000000ULL
,
792 /* Pchip0 PCI configuration, 0x801.FE00.0000, 16MB. */
793 memory_region_init_io(&s
->pchip
.reg_conf
, NULL
, &alpha_pci_conf1_ops
, b
,
795 memory_region_add_subregion(addr_space
, 0x801fe000000ULL
,
798 /* For the record, these are the mappings for the second PCI bus.
799 We can get away with not implementing them because we indicate
800 via the Cchip.CSC<PIP> bit that Pchip1 is not present. */
801 /* Pchip1 PCI memory, 0x802.0000.0000, 4GB. */
802 /* Pchip1 CSRs, 0x802.8000.0000, 256MB. */
803 /* Pchip1 PCI special/interrupt acknowledge, 0x802.F800.0000, 64MB. */
804 /* Pchip1 PCI I/O, 0x802.FC00.0000, 32MB. */
805 /* Pchip1 PCI configuration, 0x802.FE00.0000, 16MB. */
807 /* Init the ISA bus. */
808 /* ??? Technically there should be a cy82c693ub pci-isa bridge. */
810 qemu_irq isa_pci_irq
, *isa_irqs
;
812 *isa_bus
= isa_bus_new(NULL
, addr_space_io
);
813 isa_pci_irq
= *qemu_allocate_irqs(typhoon_set_isa_irq
, s
, 1);
814 isa_irqs
= i8259_init(*isa_bus
, isa_pci_irq
);
815 isa_bus_irqs(*isa_bus
, isa_irqs
);
821 static int typhoon_pcihost_init(SysBusDevice
*dev
)
826 static void typhoon_pcihost_class_init(ObjectClass
*klass
, void *data
)
828 DeviceClass
*dc
= DEVICE_CLASS(klass
);
829 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
831 k
->init
= typhoon_pcihost_init
;
835 static const TypeInfo typhoon_pcihost_info
= {
836 .name
= TYPE_TYPHOON_PCI_HOST_BRIDGE
,
837 .parent
= TYPE_PCI_HOST_BRIDGE
,
838 .instance_size
= sizeof(TyphoonState
),
839 .class_init
= typhoon_pcihost_class_init
,
842 static void typhoon_register_types(void)
844 type_register_static(&typhoon_pcihost_info
);
847 type_init(typhoon_register_types
)