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_unassigned_access(cpu_single_env
, addr
, 0, 0, 0, size
);
204 s
->latch_tmp
= ret
>> 32;
208 static uint64_t dchip_read(void *opaque
, hwaddr addr
, unsigned size
)
210 /* Skip this. It's all related to DRAM timing and setup. */
214 static uint64_t pchip_read(void *opaque
, hwaddr addr
, unsigned size
)
216 TyphoonState
*s
= opaque
;
225 /* WSBA0: Window Space Base Address Register. */
226 ret
= s
->pchip
.win
[0].base_addr
;
230 ret
= s
->pchip
.win
[1].base_addr
;
234 ret
= s
->pchip
.win
[2].base_addr
;
238 ret
= s
->pchip
.win
[3].base_addr
;
242 /* WSM0: Window Space Mask Register. */
243 ret
= s
->pchip
.win
[0].mask
;
247 ret
= s
->pchip
.win
[1].mask
;
251 ret
= s
->pchip
.win
[2].mask
;
255 ret
= s
->pchip
.win
[3].mask
;
259 /* TBA0: Translated Base Address Register. */
260 ret
= (uint64_t)s
->pchip
.win
[0].translated_base_pfn
<< 10;
264 ret
= (uint64_t)s
->pchip
.win
[1].translated_base_pfn
<< 10;
268 ret
= (uint64_t)s
->pchip
.win
[2].translated_base_pfn
<< 10;
272 ret
= (uint64_t)s
->pchip
.win
[3].translated_base_pfn
<< 10;
276 /* PCTL: Pchip Control Register. */
280 /* PLAT: Pchip Master Latency Register. */
283 /* PERROR: Pchip Error Register. */
286 /* PERRMASK: Pchip Error Mask Register. */
289 /* PERRSET: Pchip Error Set Register. */
292 /* TLBIV: Translation Buffer Invalidate Virtual Register (WO). */
295 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
297 case 0x0500: /* PMONCTL */
298 case 0x0540: /* PMONCNT */
299 case 0x0800: /* SPRST */
303 cpu_unassigned_access(cpu_single_env
, addr
, 0, 0, 0, size
);
307 s
->latch_tmp
= ret
>> 32;
311 static void cchip_write(void *opaque
, hwaddr addr
,
312 uint64_t v32
, unsigned size
)
314 TyphoonState
*s
= opaque
;
315 uint64_t val
, oldval
, newval
;
318 val
= v32
<< 32 | s
->latch_tmp
;
327 /* CSC: Cchip System Configuration Register. */
328 /* All sorts of data here; nothing relevant RW. */
332 /* MTR: Memory Timing Register. */
333 /* All sorts of stuff related to real DRAM. */
337 /* MISC: Miscellaneous Register. */
338 newval
= oldval
= s
->cchip
.misc
;
339 newval
&= ~(val
& 0x10000ff0); /* W1C fields */
340 if (val
& 0x100000) {
341 newval
&= ~0xff0000ull
; /* ACL clears ABT and ABW */
343 newval
|= val
& 0x00f00000; /* ABT field is W1S */
344 if ((newval
& 0xf0000) == 0) {
345 newval
|= val
& 0xf0000; /* ABW field is W1S iff zero */
348 newval
|= (val
& 0xf000) >> 4; /* IPREQ field sets IPINTR. */
350 newval
&= ~0xf0000000000ull
; /* WO and RW fields */
351 newval
|= val
& 0xf0000000000ull
;
352 s
->cchip
.misc
= newval
;
354 /* Pass on changes to IPI and ITI state. */
355 if ((newval
^ oldval
) & 0xff0) {
357 for (i
= 0; i
< 4; ++i
) {
358 AlphaCPU
*cpu
= s
->cchip
.cpu
[i
];
360 CPUState
*cs
= CPU(cpu
);
361 /* IPI can be either cleared or set by the write. */
362 if (newval
& (1 << (i
+ 8))) {
363 cpu_interrupt(cs
, CPU_INTERRUPT_SMP
);
365 cpu_reset_interrupt(cs
, CPU_INTERRUPT_SMP
);
368 /* ITI can only be cleared by the write. */
369 if ((newval
& (1 << (i
+ 4))) == 0) {
370 cpu_reset_interrupt(cs
, CPU_INTERRUPT_TIMER
);
378 /* MPD: Memory Presence Detect Register. */
381 case 0x0100: /* AAR0 */
382 case 0x0140: /* AAR1 */
383 case 0x0180: /* AAR2 */
384 case 0x01c0: /* AAR3 */
385 /* AAR: Array Address Register. */
386 /* All sorts of information about DRAM. */
389 case 0x0200: /* DIM0 */
390 /* DIM: Device Interrupt Mask Register, CPU0. */
391 s
->cchip
.dim
[0] = val
;
392 cpu_irq_change(s
->cchip
.cpu
[0], val
& s
->cchip
.drir
);
394 case 0x0240: /* DIM1 */
395 /* DIM: Device Interrupt Mask Register, CPU1. */
396 s
->cchip
.dim
[0] = val
;
397 cpu_irq_change(s
->cchip
.cpu
[1], val
& s
->cchip
.drir
);
400 case 0x0280: /* DIR0 (RO) */
401 case 0x02c0: /* DIR1 (RO) */
402 case 0x0300: /* DRIR (RO) */
406 /* PRBEN: Probe Enable Register. */
409 case 0x0380: /* IIC0 */
410 s
->cchip
.iic
[0] = val
& 0xffffff;
412 case 0x03c0: /* IIC1 */
413 s
->cchip
.iic
[1] = val
& 0xffffff;
416 case 0x0400: /* MPR0 */
417 case 0x0440: /* MPR1 */
418 case 0x0480: /* MPR2 */
419 case 0x04c0: /* MPR3 */
420 /* MPR: Memory Programming Register. */
424 /* TTR: TIGbus Timing Register. */
425 /* All sorts of stuff related to interrupt delivery timings. */
428 /* TDR: TIGbug Device Timing Register. */
432 /* DIM2: Device Interrupt Mask Register, CPU2. */
433 s
->cchip
.dim
[2] = val
;
434 cpu_irq_change(s
->cchip
.cpu
[2], val
& s
->cchip
.drir
);
437 /* DIM3: Device Interrupt Mask Register, CPU3. */
438 s
->cchip
.dim
[3] = val
;
439 cpu_irq_change(s
->cchip
.cpu
[3], val
& s
->cchip
.drir
);
442 case 0x0680: /* DIR2 (RO) */
443 case 0x06c0: /* DIR3 (RO) */
446 case 0x0700: /* IIC2 */
447 s
->cchip
.iic
[2] = val
& 0xffffff;
449 case 0x0740: /* IIC3 */
450 s
->cchip
.iic
[3] = val
& 0xffffff;
454 /* PWR: Power Management Control. */
457 case 0x0c00: /* CMONCTLA */
458 case 0x0c40: /* CMONCTLB */
459 case 0x0c80: /* CMONCNT01 */
460 case 0x0cc0: /* CMONCNT23 */
464 cpu_unassigned_access(cpu_single_env
, addr
, 1, 0, 0, size
);
469 static void dchip_write(void *opaque
, hwaddr addr
,
470 uint64_t val
, unsigned size
)
472 /* Skip this. It's all related to DRAM timing and setup. */
475 static void pchip_write(void *opaque
, hwaddr addr
,
476 uint64_t v32
, unsigned size
)
478 TyphoonState
*s
= opaque
;
479 uint64_t val
, oldval
;
482 val
= v32
<< 32 | s
->latch_tmp
;
491 /* WSBA0: Window Space Base Address Register. */
492 s
->pchip
.win
[0].base_addr
= val
;
496 s
->pchip
.win
[1].base_addr
= val
;
500 s
->pchip
.win
[2].base_addr
= val
;
504 s
->pchip
.win
[3].base_addr
= val
;
508 /* WSM0: Window Space Mask Register. */
509 s
->pchip
.win
[0].mask
= val
;
513 s
->pchip
.win
[1].mask
= val
;
517 s
->pchip
.win
[2].mask
= val
;
521 s
->pchip
.win
[3].mask
= val
;
525 /* TBA0: Translated Base Address Register. */
526 s
->pchip
.win
[0].translated_base_pfn
= val
>> 10;
530 s
->pchip
.win
[1].translated_base_pfn
= val
>> 10;
534 s
->pchip
.win
[2].translated_base_pfn
= val
>> 10;
538 s
->pchip
.win
[3].translated_base_pfn
= val
>> 10;
542 /* PCTL: Pchip Control Register. */
543 oldval
= s
->pchip
.ctl
;
544 oldval
&= ~0x00001cff0fc7ffull
; /* RW fields */
545 oldval
|= val
& 0x00001cff0fc7ffull
;
547 s
->pchip
.ctl
= oldval
;
551 /* PLAT: Pchip Master Latency Register. */
554 /* PERROR: Pchip Error Register. */
557 /* PERRMASK: Pchip Error Mask Register. */
560 /* PERRSET: Pchip Error Set Register. */
564 /* TLBIV: Translation Buffer Invalidate Virtual Register. */
568 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
580 cpu_unassigned_access(cpu_single_env
, addr
, 1, 0, 0, size
);
585 static const MemoryRegionOps cchip_ops
= {
587 .write
= cchip_write
,
588 .endianness
= DEVICE_LITTLE_ENDIAN
,
590 .min_access_size
= 4, /* ??? Should be 8. */
591 .max_access_size
= 8,
594 .min_access_size
= 4,
595 .max_access_size
= 4,
599 static const MemoryRegionOps dchip_ops
= {
601 .write
= dchip_write
,
602 .endianness
= DEVICE_LITTLE_ENDIAN
,
604 .min_access_size
= 4, /* ??? Should be 8. */
605 .max_access_size
= 8,
608 .min_access_size
= 4,
609 .max_access_size
= 8,
613 static const MemoryRegionOps pchip_ops
= {
615 .write
= pchip_write
,
616 .endianness
= DEVICE_LITTLE_ENDIAN
,
618 .min_access_size
= 4, /* ??? Should be 8. */
619 .max_access_size
= 8,
622 .min_access_size
= 4,
623 .max_access_size
= 4,
627 static void typhoon_set_irq(void *opaque
, int irq
, int level
)
629 TyphoonState
*s
= opaque
;
633 /* Set/Reset the bit in CCHIP.DRIR based on IRQ+LEVEL. */
634 drir
= s
->cchip
.drir
;
638 drir
&= ~(1ull << irq
);
640 s
->cchip
.drir
= drir
;
642 for (i
= 0; i
< 4; ++i
) {
643 cpu_irq_change(s
->cchip
.cpu
[i
], s
->cchip
.dim
[i
] & drir
);
647 static void typhoon_set_isa_irq(void *opaque
, int irq
, int level
)
649 typhoon_set_irq(opaque
, 55, level
);
652 static void typhoon_set_timer_irq(void *opaque
, int irq
, int level
)
654 TyphoonState
*s
= opaque
;
657 /* Thankfully, the mc146818rtc code doesn't track the IRQ state,
658 and so we don't have to worry about missing interrupts just
659 because we never actually ACK the interrupt. Just ignore any
660 case of the interrupt level going low. */
665 /* Deliver the interrupt to each CPU, considering each CPU's IIC. */
666 for (i
= 0; i
< 4; ++i
) {
667 AlphaCPU
*cpu
= s
->cchip
.cpu
[i
];
669 uint32_t iic
= s
->cchip
.iic
[i
];
671 /* ??? The verbage in Section 10.2.2.10 isn't 100% clear.
672 Bit 24 is the OverFlow bit, RO, and set when the count
673 decrements past 0. When is OF cleared? My guess is that
674 OF is actually cleared when the IIC is written, and that
675 the ICNT field always decrements. At least, that's an
676 interpretation that makes sense, and "allows the CPU to
677 determine exactly how mant interval timer ticks were
678 skipped". At least within the next 4M ticks... */
680 iic
= ((iic
- 1) & 0x1ffffff) | (iic
& 0x1000000);
681 s
->cchip
.iic
[i
] = iic
;
683 if (iic
& 0x1000000) {
684 /* Set the ITI bit for this cpu. */
685 s
->cchip
.misc
|= 1 << (i
+ 4);
686 /* And signal the interrupt. */
687 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_TIMER
);
693 static void typhoon_alarm_timer(void *opaque
)
695 TyphoonState
*s
= (TyphoonState
*)((uintptr_t)opaque
& ~3);
696 int cpu
= (uintptr_t)opaque
& 3;
698 /* Set the ITI bit for this cpu. */
699 s
->cchip
.misc
|= 1 << (cpu
+ 4);
700 cpu_interrupt(CPU(s
->cchip
.cpu
[cpu
]), CPU_INTERRUPT_TIMER
);
703 PCIBus
*typhoon_init(ram_addr_t ram_size
, ISABus
**isa_bus
,
705 AlphaCPU
*cpus
[4], pci_map_irq_fn sys_map_irq
)
707 const uint64_t MB
= 1024 * 1024;
708 const uint64_t GB
= 1024 * MB
;
709 MemoryRegion
*addr_space
= get_system_memory();
710 MemoryRegion
*addr_space_io
= get_system_io();
717 dev
= qdev_create(NULL
, TYPE_TYPHOON_PCI_HOST_BRIDGE
);
718 qdev_init_nofail(dev
);
720 s
= TYPHOON_PCI_HOST_BRIDGE(dev
);
721 phb
= PCI_HOST_BRIDGE(dev
);
723 /* Remember the CPUs so that we can deliver interrupts to them. */
724 for (i
= 0; i
< 4; i
++) {
725 AlphaCPU
*cpu
= cpus
[i
];
726 s
->cchip
.cpu
[i
] = cpu
;
728 cpu
->alarm_timer
= qemu_new_timer_ns(rtc_clock
,
730 (void *)((uintptr_t)s
+ i
));
734 *p_rtc_irq
= *qemu_allocate_irqs(typhoon_set_timer_irq
, s
, 1);
736 /* Main memory region, 0x00.0000.0000. Real hardware supports 32GB,
737 but the address space hole reserved at this point is 8TB. */
738 memory_region_init_ram(&s
->ram_region
, "ram", ram_size
);
739 vmstate_register_ram_global(&s
->ram_region
);
740 memory_region_add_subregion(addr_space
, 0, &s
->ram_region
);
742 /* TIGbus, 0x801.0000.0000, 1GB. */
743 /* ??? The TIGbus is used for delivering interrupts, and access to
744 the flash ROM. I'm not sure that we need to implement it at all. */
746 /* Pchip0 CSRs, 0x801.8000.0000, 256MB. */
747 memory_region_init_io(&s
->pchip
.region
, &pchip_ops
, s
, "pchip0", 256*MB
);
748 memory_region_add_subregion(addr_space
, 0x80180000000ULL
,
751 /* Cchip CSRs, 0x801.A000.0000, 256MB. */
752 memory_region_init_io(&s
->cchip
.region
, &cchip_ops
, s
, "cchip0", 256*MB
);
753 memory_region_add_subregion(addr_space
, 0x801a0000000ULL
,
756 /* Dchip CSRs, 0x801.B000.0000, 256MB. */
757 memory_region_init_io(&s
->dchip_region
, &dchip_ops
, s
, "dchip0", 256*MB
);
758 memory_region_add_subregion(addr_space
, 0x801b0000000ULL
,
761 /* Pchip0 PCI memory, 0x800.0000.0000, 4GB. */
762 memory_region_init(&s
->pchip
.reg_mem
, "pci0-mem", 4*GB
);
763 memory_region_add_subregion(addr_space
, 0x80000000000ULL
,
766 /* Pchip0 PCI I/O, 0x801.FC00.0000, 32MB. */
767 /* ??? Ideally we drop the "system" i/o space on the floor and give the
768 PCI subsystem the full address space reserved by the chipset.
769 We can't do that until the MEM and IO paths in memory.c are unified. */
770 memory_region_init_io(&s
->pchip
.reg_io
, &alpha_pci_bw_io_ops
, NULL
,
772 memory_region_add_subregion(addr_space
, 0x801fc000000ULL
,
775 b
= pci_register_bus(dev
, "pci",
776 typhoon_set_irq
, sys_map_irq
, s
,
777 &s
->pchip
.reg_mem
, addr_space_io
, 0, 64, TYPE_PCI_BUS
);
780 /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB. */
781 memory_region_init_io(&s
->pchip
.reg_iack
, &alpha_pci_iack_ops
, b
,
783 memory_region_add_subregion(addr_space
, 0x801f8000000ULL
,
786 /* Pchip0 PCI configuration, 0x801.FE00.0000, 16MB. */
787 memory_region_init_io(&s
->pchip
.reg_conf
, &alpha_pci_conf1_ops
, b
,
789 memory_region_add_subregion(addr_space
, 0x801fe000000ULL
,
792 /* For the record, these are the mappings for the second PCI bus.
793 We can get away with not implementing them because we indicate
794 via the Cchip.CSC<PIP> bit that Pchip1 is not present. */
795 /* Pchip1 PCI memory, 0x802.0000.0000, 4GB. */
796 /* Pchip1 CSRs, 0x802.8000.0000, 256MB. */
797 /* Pchip1 PCI special/interrupt acknowledge, 0x802.F800.0000, 64MB. */
798 /* Pchip1 PCI I/O, 0x802.FC00.0000, 32MB. */
799 /* Pchip1 PCI configuration, 0x802.FE00.0000, 16MB. */
801 /* Init the ISA bus. */
802 /* ??? Technically there should be a cy82c693ub pci-isa bridge. */
804 qemu_irq isa_pci_irq
, *isa_irqs
;
806 *isa_bus
= isa_bus_new(NULL
, addr_space_io
);
807 isa_pci_irq
= *qemu_allocate_irqs(typhoon_set_isa_irq
, s
, 1);
808 isa_irqs
= i8259_init(*isa_bus
, isa_pci_irq
);
809 isa_bus_irqs(*isa_bus
, isa_irqs
);
815 static int typhoon_pcihost_init(SysBusDevice
*dev
)
820 static void typhoon_pcihost_class_init(ObjectClass
*klass
, void *data
)
822 DeviceClass
*dc
= DEVICE_CLASS(klass
);
823 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
825 k
->init
= typhoon_pcihost_init
;
829 static const TypeInfo typhoon_pcihost_info
= {
830 .name
= TYPE_TYPHOON_PCI_HOST_BRIDGE
,
831 .parent
= TYPE_PCI_HOST_BRIDGE
,
832 .instance_size
= sizeof(TyphoonState
),
833 .class_init
= typhoon_pcihost_class_init
,
836 static void typhoon_register_types(void)
838 type_register_static(&typhoon_pcihost_info
);
841 type_init(typhoon_register_types
)