xref: /openbmc/qemu/hw/pci-host/versatile.c (revision 40c5dce9)
1c0907c9eSPaolo Bonzini /*
2c0907c9eSPaolo Bonzini  * ARM Versatile/PB PCI host controller
3c0907c9eSPaolo Bonzini  *
4c0907c9eSPaolo Bonzini  * Copyright (c) 2006-2009 CodeSourcery.
5c0907c9eSPaolo Bonzini  * Written by Paul Brook
6c0907c9eSPaolo Bonzini  *
7c0907c9eSPaolo Bonzini  * This code is licensed under the LGPL.
8c0907c9eSPaolo Bonzini  */
9c0907c9eSPaolo Bonzini 
10c0907c9eSPaolo Bonzini #include "hw/sysbus.h"
11c0907c9eSPaolo Bonzini #include "hw/pci/pci.h"
120688810bSPeter Maydell #include "hw/pci/pci_bus.h"
13c0907c9eSPaolo Bonzini #include "hw/pci/pci_host.h"
14c0907c9eSPaolo Bonzini #include "exec/address-spaces.h"
15c0907c9eSPaolo Bonzini 
1666a96d70SPeter Maydell /* Old and buggy versions of QEMU used the wrong mapping from
1766a96d70SPeter Maydell  * PCI IRQs to system interrupt lines. Unfortunately the Linux
1866a96d70SPeter Maydell  * kernel also had the corresponding bug in setting up interrupts
1966a96d70SPeter Maydell  * (so older kernels work on QEMU and not on real hardware).
2066a96d70SPeter Maydell  * We automatically detect these broken kernels and flip back
2166a96d70SPeter Maydell  * to the broken irq mapping by spotting guest writes to the
2266a96d70SPeter Maydell  * PCI_INTERRUPT_LINE register to see where the guest thinks
2366a96d70SPeter Maydell  * interrupts are going to be routed. So we start in state
2466a96d70SPeter Maydell  * ASSUME_OK on reset, and transition to either BROKEN or
2566a96d70SPeter Maydell  * FORCE_OK at the first write to an INTERRUPT_LINE register for
2666a96d70SPeter Maydell  * a slot where broken and correct interrupt mapping would differ.
2766a96d70SPeter Maydell  * Once in either BROKEN or FORCE_OK we never transition again;
2866a96d70SPeter Maydell  * this allows a newer kernel to use the INTERRUPT_LINE
2966a96d70SPeter Maydell  * registers arbitrarily once it has indicated that it isn't
3066a96d70SPeter Maydell  * broken in its init code somewhere.
31bc04d891SPeter Maydell  *
32bc04d891SPeter Maydell  * Unfortunately we have to cope with multiple different
33bc04d891SPeter Maydell  * variants on the broken kernel behaviour:
34bc04d891SPeter Maydell  *  phase I (before kernel commit 1bc39ac5d) kernels assume old
35bc04d891SPeter Maydell  *   QEMU behaviour, so they use IRQ 27 for all slots
36bc04d891SPeter Maydell  *  phase II (1bc39ac5d and later, but before e3e92a7be6) kernels
37bc04d891SPeter Maydell  *   swizzle IRQs between slots, but do it wrongly, so they
38bc04d891SPeter Maydell  *   work only for every fourth PCI card, and only if (like old
39bc04d891SPeter Maydell  *   QEMU) the PCI host device is at slot 0 rather than where
40bc04d891SPeter Maydell  *   the h/w actually puts it
41bc04d891SPeter Maydell  *  phase III (e3e92a7be6 and later) kernels still swizzle IRQs between
42bc04d891SPeter Maydell  *   slots wrongly, but add a fixed offset of 64 to everything
43bc04d891SPeter Maydell  *   they write to PCI_INTERRUPT_LINE.
44bc04d891SPeter Maydell  *
45bc04d891SPeter Maydell  * We live in hope of a mythical phase IV kernel which might
46bc04d891SPeter Maydell  * actually behave in ways that work on the hardware. Such a
47bc04d891SPeter Maydell  * kernel should probably start off by writing some value neither
48bc04d891SPeter Maydell  * 27 nor 91 to slot zero's PCI_INTERRUPT_LINE register to
49bc04d891SPeter Maydell  * disable the autodetection. After that it can do what it likes.
50bc04d891SPeter Maydell  *
51bc04d891SPeter Maydell  * Slot % 4 | hw | I  | II | III
52bc04d891SPeter Maydell  * -------------------------------
53bc04d891SPeter Maydell  *   0      | 29 | 27 | 27 | 91
54bc04d891SPeter Maydell  *   1      | 30 | 27 | 28 | 92
55bc04d891SPeter Maydell  *   2      | 27 | 27 | 29 | 93
56bc04d891SPeter Maydell  *   3      | 28 | 27 | 30 | 94
57913b4b6bSPeter Maydell  *
58913b4b6bSPeter Maydell  * Since our autodetection is not perfect we also provide a
59913b4b6bSPeter Maydell  * property so the user can make us start in BROKEN or FORCE_OK
60913b4b6bSPeter Maydell  * on reset if they know they have a bad or good kernel.
6166a96d70SPeter Maydell  */
6266a96d70SPeter Maydell enum {
6366a96d70SPeter Maydell     PCI_VPB_IRQMAP_ASSUME_OK,
6466a96d70SPeter Maydell     PCI_VPB_IRQMAP_BROKEN,
6566a96d70SPeter Maydell     PCI_VPB_IRQMAP_FORCE_OK,
6666a96d70SPeter Maydell };
6766a96d70SPeter Maydell 
68c0907c9eSPaolo Bonzini typedef struct {
690688810bSPeter Maydell     PCIHostState parent_obj;
700688810bSPeter Maydell 
71c0907c9eSPaolo Bonzini     qemu_irq irq[4];
727468d73aSPeter Maydell     MemoryRegion controlregs;
73c0907c9eSPaolo Bonzini     MemoryRegion mem_config;
74c0907c9eSPaolo Bonzini     MemoryRegion mem_config2;
7589a32d32SPeter Maydell     /* Containers representing the PCI address spaces */
76967c2607SPeter Maydell     MemoryRegion pci_io_space;
7789a32d32SPeter Maydell     MemoryRegion pci_mem_space;
7889a32d32SPeter Maydell     /* Alias regions into PCI address spaces which we expose as sysbus regions.
7989a32d32SPeter Maydell      * The offsets into pci_mem_space are controlled by the imap registers.
8089a32d32SPeter Maydell      */
81967c2607SPeter Maydell     MemoryRegion pci_io_window;
8289a32d32SPeter Maydell     MemoryRegion pci_mem_window[3];
830688810bSPeter Maydell     PCIBus pci_bus;
840688810bSPeter Maydell     PCIDevice pci_dev;
850688810bSPeter Maydell 
860688810bSPeter Maydell     /* Constant for life of device: */
870688810bSPeter Maydell     int realview;
8889a32d32SPeter Maydell     uint32_t mem_win_size[3];
89913b4b6bSPeter Maydell     uint8_t irq_mapping_prop;
9066a96d70SPeter Maydell 
9166a96d70SPeter Maydell     /* Variable state: */
927468d73aSPeter Maydell     uint32_t imap[3];
937468d73aSPeter Maydell     uint32_t smap[3];
947468d73aSPeter Maydell     uint32_t selfid;
957468d73aSPeter Maydell     uint32_t flags;
9666a96d70SPeter Maydell     uint8_t irq_mapping;
97c0907c9eSPaolo Bonzini } PCIVPBState;
98c0907c9eSPaolo Bonzini 
9989a32d32SPeter Maydell static void pci_vpb_update_window(PCIVPBState *s, int i)
10089a32d32SPeter Maydell {
10189a32d32SPeter Maydell     /* Adjust the offset of the alias region we use for
10289a32d32SPeter Maydell      * the memory window i to account for a change in the
10389a32d32SPeter Maydell      * value of the corresponding IMAP register.
10489a32d32SPeter Maydell      * Note that the semantics of the IMAP register differ
10589a32d32SPeter Maydell      * for realview and versatile variants of the controller.
10689a32d32SPeter Maydell      */
10789a32d32SPeter Maydell     hwaddr offset;
10889a32d32SPeter Maydell     if (s->realview) {
10989a32d32SPeter Maydell         /* Top bits of register (masked according to window size) provide
11089a32d32SPeter Maydell          * top bits of PCI address.
11189a32d32SPeter Maydell          */
11289a32d32SPeter Maydell         offset = s->imap[i] & ~(s->mem_win_size[i] - 1);
11389a32d32SPeter Maydell     } else {
11489a32d32SPeter Maydell         /* Bottom 4 bits of register provide top 4 bits of PCI address */
11589a32d32SPeter Maydell         offset = s->imap[i] << 28;
11689a32d32SPeter Maydell     }
11789a32d32SPeter Maydell     memory_region_set_alias_offset(&s->pci_mem_window[i], offset);
11889a32d32SPeter Maydell }
11989a32d32SPeter Maydell 
12089a32d32SPeter Maydell static void pci_vpb_update_all_windows(PCIVPBState *s)
12189a32d32SPeter Maydell {
12289a32d32SPeter Maydell     /* Update all alias windows based on the current register state */
12389a32d32SPeter Maydell     int i;
12489a32d32SPeter Maydell 
12589a32d32SPeter Maydell     for (i = 0; i < 3; i++) {
12689a32d32SPeter Maydell         pci_vpb_update_window(s, i);
12789a32d32SPeter Maydell     }
12889a32d32SPeter Maydell }
12989a32d32SPeter Maydell 
13089a32d32SPeter Maydell static int pci_vpb_post_load(void *opaque, int version_id)
13189a32d32SPeter Maydell {
13289a32d32SPeter Maydell     PCIVPBState *s = opaque;
13389a32d32SPeter Maydell     pci_vpb_update_all_windows(s);
13489a32d32SPeter Maydell     return 0;
13589a32d32SPeter Maydell }
13689a32d32SPeter Maydell 
1377468d73aSPeter Maydell static const VMStateDescription pci_vpb_vmstate = {
1387468d73aSPeter Maydell     .name = "versatile-pci",
1397468d73aSPeter Maydell     .version_id = 1,
1407468d73aSPeter Maydell     .minimum_version_id = 1,
14189a32d32SPeter Maydell     .post_load = pci_vpb_post_load,
1427468d73aSPeter Maydell     .fields = (VMStateField[]) {
1437468d73aSPeter Maydell         VMSTATE_UINT32_ARRAY(imap, PCIVPBState, 3),
1447468d73aSPeter Maydell         VMSTATE_UINT32_ARRAY(smap, PCIVPBState, 3),
1457468d73aSPeter Maydell         VMSTATE_UINT32(selfid, PCIVPBState),
1467468d73aSPeter Maydell         VMSTATE_UINT32(flags, PCIVPBState),
1477468d73aSPeter Maydell         VMSTATE_UINT8(irq_mapping, PCIVPBState),
1487468d73aSPeter Maydell         VMSTATE_END_OF_LIST()
1497468d73aSPeter Maydell     }
1507468d73aSPeter Maydell };
1517468d73aSPeter Maydell 
152cd93dbf3SPeter Maydell #define TYPE_VERSATILE_PCI "versatile_pci"
153cd93dbf3SPeter Maydell #define PCI_VPB(obj) \
154cd93dbf3SPeter Maydell     OBJECT_CHECK(PCIVPBState, (obj), TYPE_VERSATILE_PCI)
155cd93dbf3SPeter Maydell 
156cd93dbf3SPeter Maydell #define TYPE_VERSATILE_PCI_HOST "versatile_pci_host"
157cd93dbf3SPeter Maydell #define PCI_VPB_HOST(obj) \
158cd93dbf3SPeter Maydell     OBJECT_CHECK(PCIDevice, (obj), TYPE_VERSATILE_PCIHOST)
159cd93dbf3SPeter Maydell 
1607468d73aSPeter Maydell typedef enum {
1617468d73aSPeter Maydell     PCI_IMAP0 = 0x0,
1627468d73aSPeter Maydell     PCI_IMAP1 = 0x4,
1637468d73aSPeter Maydell     PCI_IMAP2 = 0x8,
1647468d73aSPeter Maydell     PCI_SELFID = 0xc,
1657468d73aSPeter Maydell     PCI_FLAGS = 0x10,
1667468d73aSPeter Maydell     PCI_SMAP0 = 0x14,
1677468d73aSPeter Maydell     PCI_SMAP1 = 0x18,
1687468d73aSPeter Maydell     PCI_SMAP2 = 0x1c,
1697468d73aSPeter Maydell } PCIVPBControlRegs;
1707468d73aSPeter Maydell 
1717468d73aSPeter Maydell static void pci_vpb_reg_write(void *opaque, hwaddr addr,
1727468d73aSPeter Maydell                               uint64_t val, unsigned size)
1737468d73aSPeter Maydell {
1747468d73aSPeter Maydell     PCIVPBState *s = opaque;
1757468d73aSPeter Maydell 
1767468d73aSPeter Maydell     switch (addr) {
1777468d73aSPeter Maydell     case PCI_IMAP0:
1787468d73aSPeter Maydell     case PCI_IMAP1:
1797468d73aSPeter Maydell     case PCI_IMAP2:
1807468d73aSPeter Maydell     {
1817468d73aSPeter Maydell         int win = (addr - PCI_IMAP0) >> 2;
1827468d73aSPeter Maydell         s->imap[win] = val;
18389a32d32SPeter Maydell         pci_vpb_update_window(s, win);
1847468d73aSPeter Maydell         break;
1857468d73aSPeter Maydell     }
1867468d73aSPeter Maydell     case PCI_SELFID:
1877468d73aSPeter Maydell         s->selfid = val;
1887468d73aSPeter Maydell         break;
1897468d73aSPeter Maydell     case PCI_FLAGS:
1907468d73aSPeter Maydell         s->flags = val;
1917468d73aSPeter Maydell         break;
1927468d73aSPeter Maydell     case PCI_SMAP0:
1937468d73aSPeter Maydell     case PCI_SMAP1:
1947468d73aSPeter Maydell     case PCI_SMAP2:
1957468d73aSPeter Maydell     {
1967468d73aSPeter Maydell         int win = (addr - PCI_SMAP0) >> 2;
1977468d73aSPeter Maydell         s->smap[win] = val;
1987468d73aSPeter Maydell         break;
1997468d73aSPeter Maydell     }
2007468d73aSPeter Maydell     default:
2017468d73aSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
2027468d73aSPeter Maydell                       "pci_vpb_reg_write: Bad offset %x\n", (int)addr);
2037468d73aSPeter Maydell         break;
2047468d73aSPeter Maydell     }
2057468d73aSPeter Maydell }
2067468d73aSPeter Maydell 
2077468d73aSPeter Maydell static uint64_t pci_vpb_reg_read(void *opaque, hwaddr addr,
2087468d73aSPeter Maydell                                  unsigned size)
2097468d73aSPeter Maydell {
2107468d73aSPeter Maydell     PCIVPBState *s = opaque;
2117468d73aSPeter Maydell 
2127468d73aSPeter Maydell     switch (addr) {
2137468d73aSPeter Maydell     case PCI_IMAP0:
2147468d73aSPeter Maydell     case PCI_IMAP1:
2157468d73aSPeter Maydell     case PCI_IMAP2:
2167468d73aSPeter Maydell     {
2177468d73aSPeter Maydell         int win = (addr - PCI_IMAP0) >> 2;
2187468d73aSPeter Maydell         return s->imap[win];
2197468d73aSPeter Maydell     }
2207468d73aSPeter Maydell     case PCI_SELFID:
2217468d73aSPeter Maydell         return s->selfid;
2227468d73aSPeter Maydell     case PCI_FLAGS:
2237468d73aSPeter Maydell         return s->flags;
2247468d73aSPeter Maydell     case PCI_SMAP0:
2257468d73aSPeter Maydell     case PCI_SMAP1:
2267468d73aSPeter Maydell     case PCI_SMAP2:
2277468d73aSPeter Maydell     {
2287468d73aSPeter Maydell         int win = (addr - PCI_SMAP0) >> 2;
2297468d73aSPeter Maydell         return s->smap[win];
2307468d73aSPeter Maydell     }
2317468d73aSPeter Maydell     default:
2327468d73aSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
2337468d73aSPeter Maydell                       "pci_vpb_reg_read: Bad offset %x\n", (int)addr);
2347468d73aSPeter Maydell         return 0;
2357468d73aSPeter Maydell     }
2367468d73aSPeter Maydell }
2377468d73aSPeter Maydell 
2387468d73aSPeter Maydell static const MemoryRegionOps pci_vpb_reg_ops = {
2397468d73aSPeter Maydell     .read = pci_vpb_reg_read,
2407468d73aSPeter Maydell     .write = pci_vpb_reg_write,
2417468d73aSPeter Maydell     .endianness = DEVICE_NATIVE_ENDIAN,
2427468d73aSPeter Maydell     .valid = {
2437468d73aSPeter Maydell         .min_access_size = 4,
2447468d73aSPeter Maydell         .max_access_size = 4,
2457468d73aSPeter Maydell     },
2467468d73aSPeter Maydell };
2477468d73aSPeter Maydell 
248bc04d891SPeter Maydell static int pci_vpb_broken_irq(int slot, int irq)
249bc04d891SPeter Maydell {
250bc04d891SPeter Maydell     /* Determine whether this IRQ value for this slot represents a
251bc04d891SPeter Maydell      * known broken Linux kernel behaviour for this slot.
252bc04d891SPeter Maydell      * Return one of the PCI_VPB_IRQMAP_ constants:
253bc04d891SPeter Maydell      *   BROKEN : if this definitely looks like a broken kernel
254bc04d891SPeter Maydell      *   FORCE_OK : if this definitely looks good
255bc04d891SPeter Maydell      *   ASSUME_OK : if we can't tell
256bc04d891SPeter Maydell      */
257bc04d891SPeter Maydell     slot %= PCI_NUM_PINS;
258bc04d891SPeter Maydell 
259bc04d891SPeter Maydell     if (irq == 27) {
260bc04d891SPeter Maydell         if (slot == 2) {
261bc04d891SPeter Maydell             /* Might be a Phase I kernel, or might be a fixed kernel,
262bc04d891SPeter Maydell              * since slot 2 is where we expect this IRQ.
263bc04d891SPeter Maydell              */
264bc04d891SPeter Maydell             return PCI_VPB_IRQMAP_ASSUME_OK;
265bc04d891SPeter Maydell         }
266bc04d891SPeter Maydell         /* Phase I kernel */
267bc04d891SPeter Maydell         return PCI_VPB_IRQMAP_BROKEN;
268bc04d891SPeter Maydell     }
269bc04d891SPeter Maydell     if (irq == slot + 27) {
270bc04d891SPeter Maydell         /* Phase II kernel */
271bc04d891SPeter Maydell         return PCI_VPB_IRQMAP_BROKEN;
272bc04d891SPeter Maydell     }
273bc04d891SPeter Maydell     if (irq == slot + 27 + 64) {
274bc04d891SPeter Maydell         /* Phase III kernel */
275bc04d891SPeter Maydell         return PCI_VPB_IRQMAP_BROKEN;
276bc04d891SPeter Maydell     }
277bc04d891SPeter Maydell     /* Anything else must be a fixed kernel, possibly using an
278bc04d891SPeter Maydell      * arbitrary irq map.
279bc04d891SPeter Maydell      */
280bc04d891SPeter Maydell     return PCI_VPB_IRQMAP_FORCE_OK;
281bc04d891SPeter Maydell }
282bc04d891SPeter Maydell 
283c0907c9eSPaolo Bonzini static void pci_vpb_config_write(void *opaque, hwaddr addr,
284c0907c9eSPaolo Bonzini                                  uint64_t val, unsigned size)
285c0907c9eSPaolo Bonzini {
28666a96d70SPeter Maydell     PCIVPBState *s = opaque;
28766a96d70SPeter Maydell     if (!s->realview && (addr & 0xff) == PCI_INTERRUPT_LINE
28866a96d70SPeter Maydell         && s->irq_mapping == PCI_VPB_IRQMAP_ASSUME_OK) {
28966a96d70SPeter Maydell         uint8_t devfn = addr >> 8;
290bc04d891SPeter Maydell         s->irq_mapping = pci_vpb_broken_irq(PCI_SLOT(devfn), val);
29166a96d70SPeter Maydell     }
292af9277e6SPeter Maydell     pci_data_write(&s->pci_bus, addr, val, size);
293c0907c9eSPaolo Bonzini }
294c0907c9eSPaolo Bonzini 
295c0907c9eSPaolo Bonzini static uint64_t pci_vpb_config_read(void *opaque, hwaddr addr,
296c0907c9eSPaolo Bonzini                                     unsigned size)
297c0907c9eSPaolo Bonzini {
29866a96d70SPeter Maydell     PCIVPBState *s = opaque;
299c0907c9eSPaolo Bonzini     uint32_t val;
300af9277e6SPeter Maydell     val = pci_data_read(&s->pci_bus, addr, size);
301c0907c9eSPaolo Bonzini     return val;
302c0907c9eSPaolo Bonzini }
303c0907c9eSPaolo Bonzini 
304c0907c9eSPaolo Bonzini static const MemoryRegionOps pci_vpb_config_ops = {
305c0907c9eSPaolo Bonzini     .read = pci_vpb_config_read,
306c0907c9eSPaolo Bonzini     .write = pci_vpb_config_write,
307c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
308c0907c9eSPaolo Bonzini };
309c0907c9eSPaolo Bonzini 
310c0907c9eSPaolo Bonzini static int pci_vpb_map_irq(PCIDevice *d, int irq_num)
311c0907c9eSPaolo Bonzini {
31266a96d70SPeter Maydell     PCIVPBState *s = container_of(d->bus, PCIVPBState, pci_bus);
31366a96d70SPeter Maydell 
31466a96d70SPeter Maydell     if (s->irq_mapping == PCI_VPB_IRQMAP_BROKEN) {
31566a96d70SPeter Maydell         /* Legacy broken IRQ mapping for compatibility with old and
31666a96d70SPeter Maydell          * buggy Linux guests
31766a96d70SPeter Maydell          */
318c0907c9eSPaolo Bonzini         return irq_num;
319c0907c9eSPaolo Bonzini     }
320c0907c9eSPaolo Bonzini 
32166a96d70SPeter Maydell     /* Slot to IRQ mapping for RealView Platform Baseboard 926 backplane
32266a96d70SPeter Maydell      *      name    slot    IntA    IntB    IntC    IntD
32366a96d70SPeter Maydell      *      A       31      IRQ28   IRQ29   IRQ30   IRQ27
32466a96d70SPeter Maydell      *      B       30      IRQ27   IRQ28   IRQ29   IRQ30
32566a96d70SPeter Maydell      *      C       29      IRQ30   IRQ27   IRQ28   IRQ29
32666a96d70SPeter Maydell      * Slot C is for the host bridge; A and B the peripherals.
32766a96d70SPeter Maydell      * Our output irqs 0..3 correspond to the baseboard's 27..30.
32866a96d70SPeter Maydell      *
32966a96d70SPeter Maydell      * This mapping function takes account of an oddity in the PB926
33066a96d70SPeter Maydell      * board wiring, where the FPGA's P_nINTA input is connected to
33166a96d70SPeter Maydell      * the INTB connection on the board PCI edge connector, P_nINTB
33266a96d70SPeter Maydell      * is connected to INTC, and so on, so everything is one number
33366a96d70SPeter Maydell      * further round from where you might expect.
33466a96d70SPeter Maydell      */
33566a96d70SPeter Maydell     return pci_swizzle_map_irq_fn(d, irq_num + 2);
33666a96d70SPeter Maydell }
33766a96d70SPeter Maydell 
33866a96d70SPeter Maydell static int pci_vpb_rv_map_irq(PCIDevice *d, int irq_num)
33966a96d70SPeter Maydell {
34066a96d70SPeter Maydell     /* Slot to IRQ mapping for RealView EB and PB1176 backplane
34166a96d70SPeter Maydell      *      name    slot    IntA    IntB    IntC    IntD
34266a96d70SPeter Maydell      *      A       31      IRQ50   IRQ51   IRQ48   IRQ49
34366a96d70SPeter Maydell      *      B       30      IRQ49   IRQ50   IRQ51   IRQ48
34466a96d70SPeter Maydell      *      C       29      IRQ48   IRQ49   IRQ50   IRQ51
34566a96d70SPeter Maydell      * Slot C is for the host bridge; A and B the peripherals.
34666a96d70SPeter Maydell      * Our output irqs 0..3 correspond to the baseboard's 48..51.
34766a96d70SPeter Maydell      *
34866a96d70SPeter Maydell      * The PB1176 and EB boards don't have the PB926 wiring oddity
34966a96d70SPeter Maydell      * described above; P_nINTA connects to INTA, P_nINTB to INTB
35066a96d70SPeter Maydell      * and so on, which is why this mapping function is different.
35166a96d70SPeter Maydell      */
35266a96d70SPeter Maydell     return pci_swizzle_map_irq_fn(d, irq_num + 3);
35366a96d70SPeter Maydell }
35466a96d70SPeter Maydell 
355c0907c9eSPaolo Bonzini static void pci_vpb_set_irq(void *opaque, int irq_num, int level)
356c0907c9eSPaolo Bonzini {
357c0907c9eSPaolo Bonzini     qemu_irq *pic = opaque;
358c0907c9eSPaolo Bonzini 
359c0907c9eSPaolo Bonzini     qemu_set_irq(pic[irq_num], level);
360c0907c9eSPaolo Bonzini }
361c0907c9eSPaolo Bonzini 
36266a96d70SPeter Maydell static void pci_vpb_reset(DeviceState *d)
36366a96d70SPeter Maydell {
36466a96d70SPeter Maydell     PCIVPBState *s = PCI_VPB(d);
36566a96d70SPeter Maydell 
3667468d73aSPeter Maydell     s->imap[0] = 0;
3677468d73aSPeter Maydell     s->imap[1] = 0;
3687468d73aSPeter Maydell     s->imap[2] = 0;
3697468d73aSPeter Maydell     s->smap[0] = 0;
3707468d73aSPeter Maydell     s->smap[1] = 0;
3717468d73aSPeter Maydell     s->smap[2] = 0;
3727468d73aSPeter Maydell     s->selfid = 0;
3737468d73aSPeter Maydell     s->flags = 0;
374913b4b6bSPeter Maydell     s->irq_mapping = s->irq_mapping_prop;
37589a32d32SPeter Maydell 
37689a32d32SPeter Maydell     pci_vpb_update_all_windows(s);
37766a96d70SPeter Maydell }
37866a96d70SPeter Maydell 
3790688810bSPeter Maydell static void pci_vpb_init(Object *obj)
3800688810bSPeter Maydell {
3810688810bSPeter Maydell     PCIHostState *h = PCI_HOST_BRIDGE(obj);
3820688810bSPeter Maydell     PCIVPBState *s = PCI_VPB(obj);
3830688810bSPeter Maydell 
384*40c5dce9SPaolo Bonzini     memory_region_init(&s->pci_io_space, OBJECT(s), "pci_io", 1ULL << 32);
385*40c5dce9SPaolo Bonzini     memory_region_init(&s->pci_mem_space, OBJECT(s), "pci_mem", 1ULL << 32);
386967c2607SPeter Maydell 
3870688810bSPeter Maydell     pci_bus_new_inplace(&s->pci_bus, DEVICE(obj), "pci",
38889a32d32SPeter Maydell                         &s->pci_mem_space, &s->pci_io_space,
3890688810bSPeter Maydell                         PCI_DEVFN(11, 0), TYPE_PCI_BUS);
3900688810bSPeter Maydell     h->bus = &s->pci_bus;
3910688810bSPeter Maydell 
3920688810bSPeter Maydell     object_initialize(&s->pci_dev, TYPE_VERSATILE_PCI_HOST);
3930688810bSPeter Maydell     qdev_set_parent_bus(DEVICE(&s->pci_dev), BUS(&s->pci_bus));
39489a32d32SPeter Maydell 
39589a32d32SPeter Maydell     /* Window sizes for VersatilePB; realview_pci's init will override */
39689a32d32SPeter Maydell     s->mem_win_size[0] = 0x0c000000;
39789a32d32SPeter Maydell     s->mem_win_size[1] = 0x10000000;
39889a32d32SPeter Maydell     s->mem_win_size[2] = 0x10000000;
3990688810bSPeter Maydell }
4000688810bSPeter Maydell 
401cd93dbf3SPeter Maydell static void pci_vpb_realize(DeviceState *dev, Error **errp)
402c0907c9eSPaolo Bonzini {
403cd93dbf3SPeter Maydell     PCIVPBState *s = PCI_VPB(dev);
404cd93dbf3SPeter Maydell     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
40566a96d70SPeter Maydell     pci_map_irq_fn mapfn;
406c0907c9eSPaolo Bonzini     int i;
407c0907c9eSPaolo Bonzini 
408c0907c9eSPaolo Bonzini     for (i = 0; i < 4; i++) {
409cd93dbf3SPeter Maydell         sysbus_init_irq(sbd, &s->irq[i]);
410c0907c9eSPaolo Bonzini     }
4110688810bSPeter Maydell 
41266a96d70SPeter Maydell     if (s->realview) {
41366a96d70SPeter Maydell         mapfn = pci_vpb_rv_map_irq;
41466a96d70SPeter Maydell     } else {
41566a96d70SPeter Maydell         mapfn = pci_vpb_map_irq;
41666a96d70SPeter Maydell     }
41766a96d70SPeter Maydell 
41866a96d70SPeter Maydell     pci_bus_irqs(&s->pci_bus, pci_vpb_set_irq, mapfn, s->irq, 4);
419c0907c9eSPaolo Bonzini 
420c0907c9eSPaolo Bonzini     /* Our memory regions are:
4217468d73aSPeter Maydell      * 0 : our control registers
4227468d73aSPeter Maydell      * 1 : PCI self config window
4237468d73aSPeter Maydell      * 2 : PCI config window
4247468d73aSPeter Maydell      * 3 : PCI IO window
42589a32d32SPeter Maydell      * 4..6 : PCI memory windows
426c0907c9eSPaolo Bonzini      */
427*40c5dce9SPaolo Bonzini     memory_region_init_io(&s->controlregs, OBJECT(s), &pci_vpb_reg_ops, s,
428*40c5dce9SPaolo Bonzini                           "pci-vpb-regs", 0x1000);
4297468d73aSPeter Maydell     sysbus_init_mmio(sbd, &s->controlregs);
430*40c5dce9SPaolo Bonzini     memory_region_init_io(&s->mem_config, OBJECT(s), &pci_vpb_config_ops, s,
431c0907c9eSPaolo Bonzini                           "pci-vpb-selfconfig", 0x1000000);
432cd93dbf3SPeter Maydell     sysbus_init_mmio(sbd, &s->mem_config);
433*40c5dce9SPaolo Bonzini     memory_region_init_io(&s->mem_config2, OBJECT(s), &pci_vpb_config_ops, s,
434c0907c9eSPaolo Bonzini                           "pci-vpb-config", 0x1000000);
435cd93dbf3SPeter Maydell     sysbus_init_mmio(sbd, &s->mem_config2);
436967c2607SPeter Maydell 
437967c2607SPeter Maydell     /* The window into I/O space is always into a fixed base address;
438967c2607SPeter Maydell      * its size is the same for both realview and versatile.
439967c2607SPeter Maydell      */
440*40c5dce9SPaolo Bonzini     memory_region_init_alias(&s->pci_io_window, OBJECT(s), "pci-vbp-io-window",
441967c2607SPeter Maydell                              &s->pci_io_space, 0, 0x100000);
442967c2607SPeter Maydell 
443967c2607SPeter Maydell     sysbus_init_mmio(sbd, &s->pci_io_space);
444c0907c9eSPaolo Bonzini 
44589a32d32SPeter Maydell     /* Create the alias regions corresponding to our three windows onto
44689a32d32SPeter Maydell      * PCI memory space. The sizes vary from board to board; the base
44789a32d32SPeter Maydell      * offsets are guest controllable via the IMAP registers.
44889a32d32SPeter Maydell      */
44989a32d32SPeter Maydell     for (i = 0; i < 3; i++) {
450*40c5dce9SPaolo Bonzini         memory_region_init_alias(&s->pci_mem_window[i], OBJECT(s), "pci-vbp-window",
45189a32d32SPeter Maydell                                  &s->pci_mem_space, 0, s->mem_win_size[i]);
45289a32d32SPeter Maydell         sysbus_init_mmio(sbd, &s->pci_mem_window[i]);
45389a32d32SPeter Maydell     }
45489a32d32SPeter Maydell 
4550688810bSPeter Maydell     /* TODO Remove once realize propagates to child devices. */
4560688810bSPeter Maydell     object_property_set_bool(OBJECT(&s->pci_dev), true, "realized", errp);
457c0907c9eSPaolo Bonzini }
458c0907c9eSPaolo Bonzini 
459c0907c9eSPaolo Bonzini static int versatile_pci_host_init(PCIDevice *d)
460c0907c9eSPaolo Bonzini {
461c0907c9eSPaolo Bonzini     pci_set_word(d->config + PCI_STATUS,
462c0907c9eSPaolo Bonzini                  PCI_STATUS_66MHZ | PCI_STATUS_DEVSEL_MEDIUM);
463c0907c9eSPaolo Bonzini     pci_set_byte(d->config + PCI_LATENCY_TIMER, 0x10);
464c0907c9eSPaolo Bonzini     return 0;
465c0907c9eSPaolo Bonzini }
466c0907c9eSPaolo Bonzini 
467c0907c9eSPaolo Bonzini static void versatile_pci_host_class_init(ObjectClass *klass, void *data)
468c0907c9eSPaolo Bonzini {
469c0907c9eSPaolo Bonzini     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
470c0907c9eSPaolo Bonzini 
471c0907c9eSPaolo Bonzini     k->init = versatile_pci_host_init;
472c0907c9eSPaolo Bonzini     k->vendor_id = PCI_VENDOR_ID_XILINX;
473c0907c9eSPaolo Bonzini     k->device_id = PCI_DEVICE_ID_XILINX_XC2VP30;
474c0907c9eSPaolo Bonzini     k->class_id = PCI_CLASS_PROCESSOR_CO;
475c0907c9eSPaolo Bonzini }
476c0907c9eSPaolo Bonzini 
477c0907c9eSPaolo Bonzini static const TypeInfo versatile_pci_host_info = {
478cd93dbf3SPeter Maydell     .name          = TYPE_VERSATILE_PCI_HOST,
479c0907c9eSPaolo Bonzini     .parent        = TYPE_PCI_DEVICE,
480c0907c9eSPaolo Bonzini     .instance_size = sizeof(PCIDevice),
481c0907c9eSPaolo Bonzini     .class_init    = versatile_pci_host_class_init,
482c0907c9eSPaolo Bonzini };
483c0907c9eSPaolo Bonzini 
484913b4b6bSPeter Maydell static Property pci_vpb_properties[] = {
485913b4b6bSPeter Maydell     DEFINE_PROP_UINT8("broken-irq-mapping", PCIVPBState, irq_mapping_prop,
486913b4b6bSPeter Maydell                       PCI_VPB_IRQMAP_ASSUME_OK),
487913b4b6bSPeter Maydell     DEFINE_PROP_END_OF_LIST()
488913b4b6bSPeter Maydell };
489913b4b6bSPeter Maydell 
490c0907c9eSPaolo Bonzini static void pci_vpb_class_init(ObjectClass *klass, void *data)
491c0907c9eSPaolo Bonzini {
492cd93dbf3SPeter Maydell     DeviceClass *dc = DEVICE_CLASS(klass);
493c0907c9eSPaolo Bonzini 
494cd93dbf3SPeter Maydell     dc->realize = pci_vpb_realize;
49566a96d70SPeter Maydell     dc->reset = pci_vpb_reset;
4967468d73aSPeter Maydell     dc->vmsd = &pci_vpb_vmstate;
497913b4b6bSPeter Maydell     dc->props = pci_vpb_properties;
498c0907c9eSPaolo Bonzini }
499c0907c9eSPaolo Bonzini 
500c0907c9eSPaolo Bonzini static const TypeInfo pci_vpb_info = {
501cd93dbf3SPeter Maydell     .name          = TYPE_VERSATILE_PCI,
5020688810bSPeter Maydell     .parent        = TYPE_PCI_HOST_BRIDGE,
503c0907c9eSPaolo Bonzini     .instance_size = sizeof(PCIVPBState),
5040688810bSPeter Maydell     .instance_init = pci_vpb_init,
505c0907c9eSPaolo Bonzini     .class_init    = pci_vpb_class_init,
506c0907c9eSPaolo Bonzini };
507c0907c9eSPaolo Bonzini 
508cd93dbf3SPeter Maydell static void pci_realview_init(Object *obj)
509c0907c9eSPaolo Bonzini {
510cd93dbf3SPeter Maydell     PCIVPBState *s = PCI_VPB(obj);
511c0907c9eSPaolo Bonzini 
512cd93dbf3SPeter Maydell     s->realview = 1;
51389a32d32SPeter Maydell     /* The PCI window sizes are different on Realview boards */
51489a32d32SPeter Maydell     s->mem_win_size[0] = 0x01000000;
51589a32d32SPeter Maydell     s->mem_win_size[1] = 0x04000000;
51689a32d32SPeter Maydell     s->mem_win_size[2] = 0x08000000;
517c0907c9eSPaolo Bonzini }
518c0907c9eSPaolo Bonzini 
519c0907c9eSPaolo Bonzini static const TypeInfo pci_realview_info = {
520c0907c9eSPaolo Bonzini     .name          = "realview_pci",
521cd93dbf3SPeter Maydell     .parent        = TYPE_VERSATILE_PCI,
522cd93dbf3SPeter Maydell     .instance_init = pci_realview_init,
523c0907c9eSPaolo Bonzini };
524c0907c9eSPaolo Bonzini 
525c0907c9eSPaolo Bonzini static void versatile_pci_register_types(void)
526c0907c9eSPaolo Bonzini {
527c0907c9eSPaolo Bonzini     type_register_static(&pci_vpb_info);
528c0907c9eSPaolo Bonzini     type_register_static(&pci_realview_info);
529c0907c9eSPaolo Bonzini     type_register_static(&versatile_pci_host_info);
530c0907c9eSPaolo Bonzini }
531c0907c9eSPaolo Bonzini 
532c0907c9eSPaolo Bonzini type_init(versatile_pci_register_types)
533