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.
14 #include "alpha_sys.h"
15 #include "exec-memory.h"
18 #define TYPE_TYPHOON_PCI_HOST_BRIDGE "typhoon-pcihost"
20 typedef struct TyphoonCchip
{
26 CPUAlphaState
*cpu
[4];
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(CPUAlphaState
*env
, uint64_t req
)
63 /* If there are any non-masked interrupts, tell the cpu. */
66 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
68 cpu_reset_interrupt(env
, 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
;
85 /* CSC: Cchip System Configuration Register. */
86 /* All sorts of data here; probably the only thing relevant is
87 PIP<14> Pchip 1 Present = 0. */
91 /* MTR: Memory Timing Register. */
92 /* All sorts of stuff related to real DRAM. */
96 /* MISC: Miscellaneous Register. */
97 ret
= s
->cchip
.misc
| (env
->cpu_index
& 3);
101 /* MPD: Memory Presence Detect Register. */
104 case 0x0100: /* AAR0 */
105 case 0x0140: /* AAR1 */
106 case 0x0180: /* AAR2 */
107 case 0x01c0: /* AAR3 */
108 /* AAR: Array Address Register. */
109 /* All sorts of information about DRAM. */
113 /* DIM0: Device Interrupt Mask Register, CPU0. */
114 ret
= s
->cchip
.dim
[0];
117 /* DIM1: Device Interrupt Mask Register, CPU1. */
118 ret
= s
->cchip
.dim
[1];
121 /* DIR0: Device Interrupt Request Register, CPU0. */
122 ret
= s
->cchip
.dim
[0] & s
->cchip
.drir
;
125 /* DIR1: Device Interrupt Request Register, CPU1. */
126 ret
= s
->cchip
.dim
[1] & s
->cchip
.drir
;
129 /* DRIR: Device Raw Interrupt Request Register. */
134 /* PRBEN: Probe Enable Register. */
138 /* IIC0: Interval Ignore Count Register, CPU0. */
139 ret
= s
->cchip
.iic
[0];
142 /* IIC1: Interval Ignore Count Register, CPU1. */
143 ret
= s
->cchip
.iic
[1];
146 case 0x0400: /* MPR0 */
147 case 0x0440: /* MPR1 */
148 case 0x0480: /* MPR2 */
149 case 0x04c0: /* MPR3 */
150 /* MPR: Memory Programming Register. */
154 /* TTR: TIGbus Timing Register. */
155 /* All sorts of stuff related to interrupt delivery timings. */
158 /* TDR: TIGbug Device Timing Register. */
162 /* DIM2: Device Interrupt Mask Register, CPU2. */
163 ret
= s
->cchip
.dim
[2];
166 /* DIM3: Device Interrupt Mask Register, CPU3. */
167 ret
= s
->cchip
.dim
[3];
170 /* DIR2: Device Interrupt Request Register, CPU2. */
171 ret
= s
->cchip
.dim
[2] & s
->cchip
.drir
;
174 /* DIR3: Device Interrupt Request Register, CPU3. */
175 ret
= s
->cchip
.dim
[3] & s
->cchip
.drir
;
179 /* IIC2: Interval Ignore Count Register, CPU2. */
180 ret
= s
->cchip
.iic
[2];
183 /* IIC3: Interval Ignore Count Register, CPU3. */
184 ret
= s
->cchip
.iic
[3];
188 /* PWR: Power Management Control. */
191 case 0x0c00: /* CMONCTLA */
192 case 0x0c40: /* CMONCTLB */
193 case 0x0c80: /* CMONCNT01 */
194 case 0x0cc0: /* CMONCNT23 */
198 cpu_unassigned_access(cpu_single_env
, addr
, 0, 0, 0, size
);
202 s
->latch_tmp
= ret
>> 32;
206 static uint64_t dchip_read(void *opaque
, hwaddr addr
, unsigned size
)
208 /* Skip this. It's all related to DRAM timing and setup. */
212 static uint64_t pchip_read(void *opaque
, hwaddr addr
, unsigned size
)
214 TyphoonState
*s
= opaque
;
223 /* WSBA0: Window Space Base Address Register. */
224 ret
= s
->pchip
.win
[0].base_addr
;
228 ret
= s
->pchip
.win
[1].base_addr
;
232 ret
= s
->pchip
.win
[2].base_addr
;
236 ret
= s
->pchip
.win
[3].base_addr
;
240 /* WSM0: Window Space Mask Register. */
241 ret
= s
->pchip
.win
[0].mask
;
245 ret
= s
->pchip
.win
[1].mask
;
249 ret
= s
->pchip
.win
[2].mask
;
253 ret
= s
->pchip
.win
[3].mask
;
257 /* TBA0: Translated Base Address Register. */
258 ret
= (uint64_t)s
->pchip
.win
[0].translated_base_pfn
<< 10;
262 ret
= (uint64_t)s
->pchip
.win
[1].translated_base_pfn
<< 10;
266 ret
= (uint64_t)s
->pchip
.win
[2].translated_base_pfn
<< 10;
270 ret
= (uint64_t)s
->pchip
.win
[3].translated_base_pfn
<< 10;
274 /* PCTL: Pchip Control Register. */
278 /* PLAT: Pchip Master Latency Register. */
281 /* PERROR: Pchip Error Register. */
284 /* PERRMASK: Pchip Error Mask Register. */
287 /* PERRSET: Pchip Error Set Register. */
290 /* TLBIV: Translation Buffer Invalidate Virtual Register (WO). */
293 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
295 case 0x0500: /* PMONCTL */
296 case 0x0540: /* PMONCNT */
297 case 0x0800: /* SPRST */
301 cpu_unassigned_access(cpu_single_env
, addr
, 0, 0, 0, size
);
305 s
->latch_tmp
= ret
>> 32;
309 static void cchip_write(void *opaque
, hwaddr addr
,
310 uint64_t v32
, unsigned size
)
312 TyphoonState
*s
= opaque
;
313 uint64_t val
, oldval
, newval
;
316 val
= v32
<< 32 | s
->latch_tmp
;
325 /* CSC: Cchip System Configuration Register. */
326 /* All sorts of data here; nothing relevant RW. */
330 /* MTR: Memory Timing Register. */
331 /* All sorts of stuff related to real DRAM. */
335 /* MISC: Miscellaneous Register. */
336 newval
= oldval
= s
->cchip
.misc
;
337 newval
&= ~(val
& 0x10000ff0); /* W1C fields */
338 if (val
& 0x100000) {
339 newval
&= ~0xff0000ull
; /* ACL clears ABT and ABW */
341 newval
|= val
& 0x00f00000; /* ABT field is W1S */
342 if ((newval
& 0xf0000) == 0) {
343 newval
|= val
& 0xf0000; /* ABW field is W1S iff zero */
346 newval
|= (val
& 0xf000) >> 4; /* IPREQ field sets IPINTR. */
348 newval
&= ~0xf0000000000ull
; /* WO and RW fields */
349 newval
|= val
& 0xf0000000000ull
;
350 s
->cchip
.misc
= newval
;
352 /* Pass on changes to IPI and ITI state. */
353 if ((newval
^ oldval
) & 0xff0) {
355 for (i
= 0; i
< 4; ++i
) {
356 CPUAlphaState
*env
= s
->cchip
.cpu
[i
];
358 /* IPI can be either cleared or set by the write. */
359 if (newval
& (1 << (i
+ 8))) {
360 cpu_interrupt(env
, CPU_INTERRUPT_SMP
);
362 cpu_reset_interrupt(env
, CPU_INTERRUPT_SMP
);
365 /* ITI can only be cleared by the write. */
366 if ((newval
& (1 << (i
+ 4))) == 0) {
367 cpu_reset_interrupt(env
, CPU_INTERRUPT_TIMER
);
375 /* MPD: Memory Presence Detect Register. */
378 case 0x0100: /* AAR0 */
379 case 0x0140: /* AAR1 */
380 case 0x0180: /* AAR2 */
381 case 0x01c0: /* AAR3 */
382 /* AAR: Array Address Register. */
383 /* All sorts of information about DRAM. */
386 case 0x0200: /* DIM0 */
387 /* DIM: Device Interrupt Mask Register, CPU0. */
388 s
->cchip
.dim
[0] = val
;
389 cpu_irq_change(s
->cchip
.cpu
[0], val
& s
->cchip
.drir
);
391 case 0x0240: /* DIM1 */
392 /* DIM: Device Interrupt Mask Register, CPU1. */
393 s
->cchip
.dim
[0] = val
;
394 cpu_irq_change(s
->cchip
.cpu
[1], val
& s
->cchip
.drir
);
397 case 0x0280: /* DIR0 (RO) */
398 case 0x02c0: /* DIR1 (RO) */
399 case 0x0300: /* DRIR (RO) */
403 /* PRBEN: Probe Enable Register. */
406 case 0x0380: /* IIC0 */
407 s
->cchip
.iic
[0] = val
& 0xffffff;
409 case 0x03c0: /* IIC1 */
410 s
->cchip
.iic
[1] = val
& 0xffffff;
413 case 0x0400: /* MPR0 */
414 case 0x0440: /* MPR1 */
415 case 0x0480: /* MPR2 */
416 case 0x04c0: /* MPR3 */
417 /* MPR: Memory Programming Register. */
421 /* TTR: TIGbus Timing Register. */
422 /* All sorts of stuff related to interrupt delivery timings. */
425 /* TDR: TIGbug Device Timing Register. */
429 /* DIM2: Device Interrupt Mask Register, CPU2. */
430 s
->cchip
.dim
[2] = val
;
431 cpu_irq_change(s
->cchip
.cpu
[2], val
& s
->cchip
.drir
);
434 /* DIM3: Device Interrupt Mask Register, CPU3. */
435 s
->cchip
.dim
[3] = val
;
436 cpu_irq_change(s
->cchip
.cpu
[3], val
& s
->cchip
.drir
);
439 case 0x0680: /* DIR2 (RO) */
440 case 0x06c0: /* DIR3 (RO) */
443 case 0x0700: /* IIC2 */
444 s
->cchip
.iic
[2] = val
& 0xffffff;
446 case 0x0740: /* IIC3 */
447 s
->cchip
.iic
[3] = val
& 0xffffff;
451 /* PWR: Power Management Control. */
454 case 0x0c00: /* CMONCTLA */
455 case 0x0c40: /* CMONCTLB */
456 case 0x0c80: /* CMONCNT01 */
457 case 0x0cc0: /* CMONCNT23 */
461 cpu_unassigned_access(cpu_single_env
, addr
, 1, 0, 0, size
);
466 static void dchip_write(void *opaque
, hwaddr addr
,
467 uint64_t val
, unsigned size
)
469 /* Skip this. It's all related to DRAM timing and setup. */
472 static void pchip_write(void *opaque
, hwaddr addr
,
473 uint64_t v32
, unsigned size
)
475 TyphoonState
*s
= opaque
;
476 uint64_t val
, oldval
;
479 val
= v32
<< 32 | s
->latch_tmp
;
488 /* WSBA0: Window Space Base Address Register. */
489 s
->pchip
.win
[0].base_addr
= val
;
493 s
->pchip
.win
[1].base_addr
= val
;
497 s
->pchip
.win
[2].base_addr
= val
;
501 s
->pchip
.win
[3].base_addr
= val
;
505 /* WSM0: Window Space Mask Register. */
506 s
->pchip
.win
[0].mask
= val
;
510 s
->pchip
.win
[1].mask
= val
;
514 s
->pchip
.win
[2].mask
= val
;
518 s
->pchip
.win
[3].mask
= val
;
522 /* TBA0: Translated Base Address Register. */
523 s
->pchip
.win
[0].translated_base_pfn
= val
>> 10;
527 s
->pchip
.win
[1].translated_base_pfn
= val
>> 10;
531 s
->pchip
.win
[2].translated_base_pfn
= val
>> 10;
535 s
->pchip
.win
[3].translated_base_pfn
= val
>> 10;
539 /* PCTL: Pchip Control Register. */
540 oldval
= s
->pchip
.ctl
;
541 oldval
&= ~0x00001cff0fc7ffull
; /* RW fields */
542 oldval
|= val
& 0x00001cff0fc7ffull
;
544 s
->pchip
.ctl
= oldval
;
548 /* PLAT: Pchip Master Latency Register. */
551 /* PERROR: Pchip Error Register. */
554 /* PERRMASK: Pchip Error Mask Register. */
557 /* PERRSET: Pchip Error Set Register. */
561 /* TLBIV: Translation Buffer Invalidate Virtual Register. */
565 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
577 cpu_unassigned_access(cpu_single_env
, addr
, 1, 0, 0, size
);
582 static const MemoryRegionOps cchip_ops
= {
584 .write
= cchip_write
,
585 .endianness
= DEVICE_LITTLE_ENDIAN
,
587 .min_access_size
= 4, /* ??? Should be 8. */
588 .max_access_size
= 8,
591 .min_access_size
= 4,
592 .max_access_size
= 4,
596 static const MemoryRegionOps dchip_ops
= {
598 .write
= dchip_write
,
599 .endianness
= DEVICE_LITTLE_ENDIAN
,
601 .min_access_size
= 4, /* ??? Should be 8. */
602 .max_access_size
= 8,
605 .min_access_size
= 4,
606 .max_access_size
= 8,
610 static const MemoryRegionOps pchip_ops
= {
612 .write
= pchip_write
,
613 .endianness
= DEVICE_LITTLE_ENDIAN
,
615 .min_access_size
= 4, /* ??? Should be 8. */
616 .max_access_size
= 8,
619 .min_access_size
= 4,
620 .max_access_size
= 4,
624 static void typhoon_set_irq(void *opaque
, int irq
, int level
)
626 TyphoonState
*s
= opaque
;
630 /* Set/Reset the bit in CCHIP.DRIR based on IRQ+LEVEL. */
631 drir
= s
->cchip
.drir
;
635 drir
&= ~(1ull << irq
);
637 s
->cchip
.drir
= drir
;
639 for (i
= 0; i
< 4; ++i
) {
640 cpu_irq_change(s
->cchip
.cpu
[i
], s
->cchip
.dim
[i
] & drir
);
644 static void typhoon_set_isa_irq(void *opaque
, int irq
, int level
)
646 typhoon_set_irq(opaque
, 55, level
);
649 static void typhoon_set_timer_irq(void *opaque
, int irq
, int level
)
651 TyphoonState
*s
= opaque
;
654 /* Thankfully, the mc146818rtc code doesn't track the IRQ state,
655 and so we don't have to worry about missing interrupts just
656 because we never actually ACK the interrupt. Just ignore any
657 case of the interrupt level going low. */
662 /* Deliver the interrupt to each CPU, considering each CPU's IIC. */
663 for (i
= 0; i
< 4; ++i
) {
664 CPUAlphaState
*env
= s
->cchip
.cpu
[i
];
666 uint32_t iic
= s
->cchip
.iic
[i
];
668 /* ??? The verbage in Section 10.2.2.10 isn't 100% clear.
669 Bit 24 is the OverFlow bit, RO, and set when the count
670 decrements past 0. When is OF cleared? My guess is that
671 OF is actually cleared when the IIC is written, and that
672 the ICNT field always decrements. At least, that's an
673 interpretation that makes sense, and "allows the CPU to
674 determine exactly how mant interval timer ticks were
675 skipped". At least within the next 4M ticks... */
677 iic
= ((iic
- 1) & 0x1ffffff) | (iic
& 0x1000000);
678 s
->cchip
.iic
[i
] = iic
;
680 if (iic
& 0x1000000) {
681 /* Set the ITI bit for this cpu. */
682 s
->cchip
.misc
|= 1 << (i
+ 4);
683 /* And signal the interrupt. */
684 cpu_interrupt(env
, CPU_INTERRUPT_TIMER
);
690 static void typhoon_alarm_timer(void *opaque
)
692 TyphoonState
*s
= (TyphoonState
*)((uintptr_t)opaque
& ~3);
693 int cpu
= (uintptr_t)opaque
& 3;
695 /* Set the ITI bit for this cpu. */
696 s
->cchip
.misc
|= 1 << (cpu
+ 4);
697 cpu_interrupt(s
->cchip
.cpu
[cpu
], CPU_INTERRUPT_TIMER
);
700 PCIBus
*typhoon_init(ram_addr_t ram_size
, ISABus
**isa_bus
,
702 CPUAlphaState
*cpus
[4], pci_map_irq_fn sys_map_irq
)
704 const uint64_t MB
= 1024 * 1024;
705 const uint64_t GB
= 1024 * MB
;
706 MemoryRegion
*addr_space
= get_system_memory();
707 MemoryRegion
*addr_space_io
= get_system_io();
714 dev
= qdev_create(NULL
, TYPE_TYPHOON_PCI_HOST_BRIDGE
);
715 qdev_init_nofail(dev
);
717 s
= TYPHOON_PCI_HOST_BRIDGE(dev
);
718 phb
= PCI_HOST_BRIDGE(dev
);
720 /* Remember the CPUs so that we can deliver interrupts to them. */
721 for (i
= 0; i
< 4; i
++) {
722 CPUAlphaState
*env
= cpus
[i
];
723 s
->cchip
.cpu
[i
] = env
;
725 env
->alarm_timer
= qemu_new_timer_ns(rtc_clock
,
727 (void *)((uintptr_t)s
+ i
));
731 *p_rtc_irq
= *qemu_allocate_irqs(typhoon_set_timer_irq
, s
, 1);
733 /* Main memory region, 0x00.0000.0000. Real hardware supports 32GB,
734 but the address space hole reserved at this point is 8TB. */
735 memory_region_init_ram(&s
->ram_region
, "ram", ram_size
);
736 vmstate_register_ram_global(&s
->ram_region
);
737 memory_region_add_subregion(addr_space
, 0, &s
->ram_region
);
739 /* TIGbus, 0x801.0000.0000, 1GB. */
740 /* ??? The TIGbus is used for delivering interrupts, and access to
741 the flash ROM. I'm not sure that we need to implement it at all. */
743 /* Pchip0 CSRs, 0x801.8000.0000, 256MB. */
744 memory_region_init_io(&s
->pchip
.region
, &pchip_ops
, s
, "pchip0", 256*MB
);
745 memory_region_add_subregion(addr_space
, 0x80180000000ULL
,
748 /* Cchip CSRs, 0x801.A000.0000, 256MB. */
749 memory_region_init_io(&s
->cchip
.region
, &cchip_ops
, s
, "cchip0", 256*MB
);
750 memory_region_add_subregion(addr_space
, 0x801a0000000ULL
,
753 /* Dchip CSRs, 0x801.B000.0000, 256MB. */
754 memory_region_init_io(&s
->dchip_region
, &dchip_ops
, s
, "dchip0", 256*MB
);
755 memory_region_add_subregion(addr_space
, 0x801b0000000ULL
,
758 /* Pchip0 PCI memory, 0x800.0000.0000, 4GB. */
759 memory_region_init(&s
->pchip
.reg_mem
, "pci0-mem", 4*GB
);
760 memory_region_add_subregion(addr_space
, 0x80000000000ULL
,
763 /* Pchip0 PCI I/O, 0x801.FC00.0000, 32MB. */
764 /* ??? Ideally we drop the "system" i/o space on the floor and give the
765 PCI subsystem the full address space reserved by the chipset.
766 We can't do that until the MEM and IO paths in memory.c are unified. */
767 memory_region_init_io(&s
->pchip
.reg_io
, &alpha_pci_bw_io_ops
, NULL
,
769 memory_region_add_subregion(addr_space
, 0x801fc000000ULL
,
772 b
= pci_register_bus(dev
, "pci",
773 typhoon_set_irq
, sys_map_irq
, s
,
774 &s
->pchip
.reg_mem
, addr_space_io
, 0, 64);
777 /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB. */
778 memory_region_init_io(&s
->pchip
.reg_iack
, &alpha_pci_iack_ops
, b
,
780 memory_region_add_subregion(addr_space
, 0x801f8000000ULL
,
783 /* Pchip0 PCI configuration, 0x801.FE00.0000, 16MB. */
784 memory_region_init_io(&s
->pchip
.reg_conf
, &alpha_pci_conf1_ops
, b
,
786 memory_region_add_subregion(addr_space
, 0x801fe000000ULL
,
789 /* For the record, these are the mappings for the second PCI bus.
790 We can get away with not implementing them because we indicate
791 via the Cchip.CSC<PIP> bit that Pchip1 is not present. */
792 /* Pchip1 PCI memory, 0x802.0000.0000, 4GB. */
793 /* Pchip1 CSRs, 0x802.8000.0000, 256MB. */
794 /* Pchip1 PCI special/interrupt acknowledge, 0x802.F800.0000, 64MB. */
795 /* Pchip1 PCI I/O, 0x802.FC00.0000, 32MB. */
796 /* Pchip1 PCI configuration, 0x802.FE00.0000, 16MB. */
798 /* Init the ISA bus. */
799 /* ??? Technically there should be a cy82c693ub pci-isa bridge. */
801 qemu_irq isa_pci_irq
, *isa_irqs
;
803 *isa_bus
= isa_bus_new(NULL
, addr_space_io
);
804 isa_pci_irq
= *qemu_allocate_irqs(typhoon_set_isa_irq
, s
, 1);
805 isa_irqs
= i8259_init(*isa_bus
, isa_pci_irq
);
806 isa_bus_irqs(*isa_bus
, isa_irqs
);
812 static int typhoon_pcihost_init(SysBusDevice
*dev
)
817 static void typhoon_pcihost_class_init(ObjectClass
*klass
, void *data
)
819 DeviceClass
*dc
= DEVICE_CLASS(klass
);
820 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
822 k
->init
= typhoon_pcihost_init
;
826 static const TypeInfo typhoon_pcihost_info
= {
827 .name
= TYPE_TYPHOON_PCI_HOST_BRIDGE
,
828 .parent
= TYPE_PCI_HOST_BRIDGE
,
829 .instance_size
= sizeof(TyphoonState
),
830 .class_init
= typhoon_pcihost_class_init
,
833 static void typhoon_register_types(void)
835 type_register_static(&typhoon_pcihost_info
);
838 type_init(typhoon_register_types
)