xref: /openbmc/qemu/hw/pci-host/versatile.c (revision 64552b6b)
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 
108ef94f0bSPeter Maydell #include "qemu/osdep.h"
11c0907c9eSPaolo Bonzini #include "hw/sysbus.h"
12*64552b6bSMarkus Armbruster #include "hw/irq.h"
13c0907c9eSPaolo Bonzini #include "hw/pci/pci.h"
140688810bSPeter Maydell #include "hw/pci/pci_bus.h"
15c0907c9eSPaolo Bonzini #include "hw/pci/pci_host.h"
1603dd024fSPaolo Bonzini #include "qemu/log.h"
170b8fa32fSMarkus Armbruster #include "qemu/module.h"
18c0907c9eSPaolo Bonzini 
1966a96d70SPeter Maydell /* Old and buggy versions of QEMU used the wrong mapping from
2066a96d70SPeter Maydell  * PCI IRQs to system interrupt lines. Unfortunately the Linux
2166a96d70SPeter Maydell  * kernel also had the corresponding bug in setting up interrupts
2266a96d70SPeter Maydell  * (so older kernels work on QEMU and not on real hardware).
2366a96d70SPeter Maydell  * We automatically detect these broken kernels and flip back
2466a96d70SPeter Maydell  * to the broken irq mapping by spotting guest writes to the
2566a96d70SPeter Maydell  * PCI_INTERRUPT_LINE register to see where the guest thinks
2666a96d70SPeter Maydell  * interrupts are going to be routed. So we start in state
2766a96d70SPeter Maydell  * ASSUME_OK on reset, and transition to either BROKEN or
2866a96d70SPeter Maydell  * FORCE_OK at the first write to an INTERRUPT_LINE register for
2966a96d70SPeter Maydell  * a slot where broken and correct interrupt mapping would differ.
3066a96d70SPeter Maydell  * Once in either BROKEN or FORCE_OK we never transition again;
3166a96d70SPeter Maydell  * this allows a newer kernel to use the INTERRUPT_LINE
3266a96d70SPeter Maydell  * registers arbitrarily once it has indicated that it isn't
3366a96d70SPeter Maydell  * broken in its init code somewhere.
34bc04d891SPeter Maydell  *
35bc04d891SPeter Maydell  * Unfortunately we have to cope with multiple different
36bc04d891SPeter Maydell  * variants on the broken kernel behaviour:
37bc04d891SPeter Maydell  *  phase I (before kernel commit 1bc39ac5d) kernels assume old
38bc04d891SPeter Maydell  *   QEMU behaviour, so they use IRQ 27 for all slots
39bc04d891SPeter Maydell  *  phase II (1bc39ac5d and later, but before e3e92a7be6) kernels
40bc04d891SPeter Maydell  *   swizzle IRQs between slots, but do it wrongly, so they
41bc04d891SPeter Maydell  *   work only for every fourth PCI card, and only if (like old
42bc04d891SPeter Maydell  *   QEMU) the PCI host device is at slot 0 rather than where
43bc04d891SPeter Maydell  *   the h/w actually puts it
44bc04d891SPeter Maydell  *  phase III (e3e92a7be6 and later) kernels still swizzle IRQs between
45bc04d891SPeter Maydell  *   slots wrongly, but add a fixed offset of 64 to everything
46bc04d891SPeter Maydell  *   they write to PCI_INTERRUPT_LINE.
47bc04d891SPeter Maydell  *
48bc04d891SPeter Maydell  * We live in hope of a mythical phase IV kernel which might
49bc04d891SPeter Maydell  * actually behave in ways that work on the hardware. Such a
50bc04d891SPeter Maydell  * kernel should probably start off by writing some value neither
51bc04d891SPeter Maydell  * 27 nor 91 to slot zero's PCI_INTERRUPT_LINE register to
52bc04d891SPeter Maydell  * disable the autodetection. After that it can do what it likes.
53bc04d891SPeter Maydell  *
54bc04d891SPeter Maydell  * Slot % 4 | hw | I  | II | III
55bc04d891SPeter Maydell  * -------------------------------
56bc04d891SPeter Maydell  *   0      | 29 | 27 | 27 | 91
57bc04d891SPeter Maydell  *   1      | 30 | 27 | 28 | 92
58bc04d891SPeter Maydell  *   2      | 27 | 27 | 29 | 93
59bc04d891SPeter Maydell  *   3      | 28 | 27 | 30 | 94
60913b4b6bSPeter Maydell  *
61913b4b6bSPeter Maydell  * Since our autodetection is not perfect we also provide a
62913b4b6bSPeter Maydell  * property so the user can make us start in BROKEN or FORCE_OK
63913b4b6bSPeter Maydell  * on reset if they know they have a bad or good kernel.
6466a96d70SPeter Maydell  */
6566a96d70SPeter Maydell enum {
6666a96d70SPeter Maydell     PCI_VPB_IRQMAP_ASSUME_OK,
6766a96d70SPeter Maydell     PCI_VPB_IRQMAP_BROKEN,
6866a96d70SPeter Maydell     PCI_VPB_IRQMAP_FORCE_OK,
6966a96d70SPeter Maydell };
7066a96d70SPeter Maydell 
71c0907c9eSPaolo Bonzini typedef struct {
720688810bSPeter Maydell     PCIHostState parent_obj;
730688810bSPeter Maydell 
74c0907c9eSPaolo Bonzini     qemu_irq irq[4];
757468d73aSPeter Maydell     MemoryRegion controlregs;
76c0907c9eSPaolo Bonzini     MemoryRegion mem_config;
77c0907c9eSPaolo Bonzini     MemoryRegion mem_config2;
7889a32d32SPeter Maydell     /* Containers representing the PCI address spaces */
79967c2607SPeter Maydell     MemoryRegion pci_io_space;
8089a32d32SPeter Maydell     MemoryRegion pci_mem_space;
8189a32d32SPeter Maydell     /* Alias regions into PCI address spaces which we expose as sysbus regions.
8289a32d32SPeter Maydell      * The offsets into pci_mem_space are controlled by the imap registers.
8389a32d32SPeter Maydell      */
84967c2607SPeter Maydell     MemoryRegion pci_io_window;
8589a32d32SPeter Maydell     MemoryRegion pci_mem_window[3];
860688810bSPeter Maydell     PCIBus pci_bus;
870688810bSPeter Maydell     PCIDevice pci_dev;
880688810bSPeter Maydell 
890688810bSPeter Maydell     /* Constant for life of device: */
900688810bSPeter Maydell     int realview;
9189a32d32SPeter Maydell     uint32_t mem_win_size[3];
92913b4b6bSPeter Maydell     uint8_t irq_mapping_prop;
9366a96d70SPeter Maydell 
9466a96d70SPeter Maydell     /* Variable state: */
957468d73aSPeter Maydell     uint32_t imap[3];
967468d73aSPeter Maydell     uint32_t smap[3];
977468d73aSPeter Maydell     uint32_t selfid;
987468d73aSPeter Maydell     uint32_t flags;
9966a96d70SPeter Maydell     uint8_t irq_mapping;
100c0907c9eSPaolo Bonzini } PCIVPBState;
101c0907c9eSPaolo Bonzini 
10289a32d32SPeter Maydell static void pci_vpb_update_window(PCIVPBState *s, int i)
10389a32d32SPeter Maydell {
10489a32d32SPeter Maydell     /* Adjust the offset of the alias region we use for
10589a32d32SPeter Maydell      * the memory window i to account for a change in the
10689a32d32SPeter Maydell      * value of the corresponding IMAP register.
10789a32d32SPeter Maydell      * Note that the semantics of the IMAP register differ
10889a32d32SPeter Maydell      * for realview and versatile variants of the controller.
10989a32d32SPeter Maydell      */
11089a32d32SPeter Maydell     hwaddr offset;
11189a32d32SPeter Maydell     if (s->realview) {
11289a32d32SPeter Maydell         /* Top bits of register (masked according to window size) provide
11389a32d32SPeter Maydell          * top bits of PCI address.
11489a32d32SPeter Maydell          */
11589a32d32SPeter Maydell         offset = s->imap[i] & ~(s->mem_win_size[i] - 1);
11689a32d32SPeter Maydell     } else {
11789a32d32SPeter Maydell         /* Bottom 4 bits of register provide top 4 bits of PCI address */
11889a32d32SPeter Maydell         offset = s->imap[i] << 28;
11989a32d32SPeter Maydell     }
12089a32d32SPeter Maydell     memory_region_set_alias_offset(&s->pci_mem_window[i], offset);
12189a32d32SPeter Maydell }
12289a32d32SPeter Maydell 
12389a32d32SPeter Maydell static void pci_vpb_update_all_windows(PCIVPBState *s)
12489a32d32SPeter Maydell {
12589a32d32SPeter Maydell     /* Update all alias windows based on the current register state */
12689a32d32SPeter Maydell     int i;
12789a32d32SPeter Maydell 
12889a32d32SPeter Maydell     for (i = 0; i < 3; i++) {
12989a32d32SPeter Maydell         pci_vpb_update_window(s, i);
13089a32d32SPeter Maydell     }
13189a32d32SPeter Maydell }
13289a32d32SPeter Maydell 
13389a32d32SPeter Maydell static int pci_vpb_post_load(void *opaque, int version_id)
13489a32d32SPeter Maydell {
13589a32d32SPeter Maydell     PCIVPBState *s = opaque;
13689a32d32SPeter Maydell     pci_vpb_update_all_windows(s);
13789a32d32SPeter Maydell     return 0;
13889a32d32SPeter Maydell }
13989a32d32SPeter Maydell 
1407468d73aSPeter Maydell static const VMStateDescription pci_vpb_vmstate = {
1417468d73aSPeter Maydell     .name = "versatile-pci",
1427468d73aSPeter Maydell     .version_id = 1,
1437468d73aSPeter Maydell     .minimum_version_id = 1,
14489a32d32SPeter Maydell     .post_load = pci_vpb_post_load,
1457468d73aSPeter Maydell     .fields = (VMStateField[]) {
1467468d73aSPeter Maydell         VMSTATE_UINT32_ARRAY(imap, PCIVPBState, 3),
1477468d73aSPeter Maydell         VMSTATE_UINT32_ARRAY(smap, PCIVPBState, 3),
1487468d73aSPeter Maydell         VMSTATE_UINT32(selfid, PCIVPBState),
1497468d73aSPeter Maydell         VMSTATE_UINT32(flags, PCIVPBState),
1507468d73aSPeter Maydell         VMSTATE_UINT8(irq_mapping, PCIVPBState),
1517468d73aSPeter Maydell         VMSTATE_END_OF_LIST()
1527468d73aSPeter Maydell     }
1537468d73aSPeter Maydell };
1547468d73aSPeter Maydell 
155cd93dbf3SPeter Maydell #define TYPE_VERSATILE_PCI "versatile_pci"
156cd93dbf3SPeter Maydell #define PCI_VPB(obj) \
157cd93dbf3SPeter Maydell     OBJECT_CHECK(PCIVPBState, (obj), TYPE_VERSATILE_PCI)
158cd93dbf3SPeter Maydell 
159cd93dbf3SPeter Maydell #define TYPE_VERSATILE_PCI_HOST "versatile_pci_host"
160cd93dbf3SPeter Maydell #define PCI_VPB_HOST(obj) \
161cd93dbf3SPeter Maydell     OBJECT_CHECK(PCIDevice, (obj), TYPE_VERSATILE_PCIHOST)
162cd93dbf3SPeter Maydell 
1637468d73aSPeter Maydell typedef enum {
1647468d73aSPeter Maydell     PCI_IMAP0 = 0x0,
1657468d73aSPeter Maydell     PCI_IMAP1 = 0x4,
1667468d73aSPeter Maydell     PCI_IMAP2 = 0x8,
1677468d73aSPeter Maydell     PCI_SELFID = 0xc,
1687468d73aSPeter Maydell     PCI_FLAGS = 0x10,
1697468d73aSPeter Maydell     PCI_SMAP0 = 0x14,
1707468d73aSPeter Maydell     PCI_SMAP1 = 0x18,
1717468d73aSPeter Maydell     PCI_SMAP2 = 0x1c,
1727468d73aSPeter Maydell } PCIVPBControlRegs;
1737468d73aSPeter Maydell 
1747468d73aSPeter Maydell static void pci_vpb_reg_write(void *opaque, hwaddr addr,
1757468d73aSPeter Maydell                               uint64_t val, unsigned size)
1767468d73aSPeter Maydell {
1777468d73aSPeter Maydell     PCIVPBState *s = opaque;
1787468d73aSPeter Maydell 
1797468d73aSPeter Maydell     switch (addr) {
1807468d73aSPeter Maydell     case PCI_IMAP0:
1817468d73aSPeter Maydell     case PCI_IMAP1:
1827468d73aSPeter Maydell     case PCI_IMAP2:
1837468d73aSPeter Maydell     {
1847468d73aSPeter Maydell         int win = (addr - PCI_IMAP0) >> 2;
1857468d73aSPeter Maydell         s->imap[win] = val;
18689a32d32SPeter Maydell         pci_vpb_update_window(s, win);
1877468d73aSPeter Maydell         break;
1887468d73aSPeter Maydell     }
1897468d73aSPeter Maydell     case PCI_SELFID:
1907468d73aSPeter Maydell         s->selfid = val;
1917468d73aSPeter Maydell         break;
1927468d73aSPeter Maydell     case PCI_FLAGS:
1937468d73aSPeter Maydell         s->flags = val;
1947468d73aSPeter Maydell         break;
1957468d73aSPeter Maydell     case PCI_SMAP0:
1967468d73aSPeter Maydell     case PCI_SMAP1:
1977468d73aSPeter Maydell     case PCI_SMAP2:
1987468d73aSPeter Maydell     {
1997468d73aSPeter Maydell         int win = (addr - PCI_SMAP0) >> 2;
2007468d73aSPeter Maydell         s->smap[win] = val;
2017468d73aSPeter Maydell         break;
2027468d73aSPeter Maydell     }
2037468d73aSPeter Maydell     default:
2047468d73aSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
2057468d73aSPeter Maydell                       "pci_vpb_reg_write: Bad offset %x\n", (int)addr);
2067468d73aSPeter Maydell         break;
2077468d73aSPeter Maydell     }
2087468d73aSPeter Maydell }
2097468d73aSPeter Maydell 
2107468d73aSPeter Maydell static uint64_t pci_vpb_reg_read(void *opaque, hwaddr addr,
2117468d73aSPeter Maydell                                  unsigned size)
2127468d73aSPeter Maydell {
2137468d73aSPeter Maydell     PCIVPBState *s = opaque;
2147468d73aSPeter Maydell 
2157468d73aSPeter Maydell     switch (addr) {
2167468d73aSPeter Maydell     case PCI_IMAP0:
2177468d73aSPeter Maydell     case PCI_IMAP1:
2187468d73aSPeter Maydell     case PCI_IMAP2:
2197468d73aSPeter Maydell     {
2207468d73aSPeter Maydell         int win = (addr - PCI_IMAP0) >> 2;
2217468d73aSPeter Maydell         return s->imap[win];
2227468d73aSPeter Maydell     }
2237468d73aSPeter Maydell     case PCI_SELFID:
2247468d73aSPeter Maydell         return s->selfid;
2257468d73aSPeter Maydell     case PCI_FLAGS:
2267468d73aSPeter Maydell         return s->flags;
2277468d73aSPeter Maydell     case PCI_SMAP0:
2287468d73aSPeter Maydell     case PCI_SMAP1:
2297468d73aSPeter Maydell     case PCI_SMAP2:
2307468d73aSPeter Maydell     {
2317468d73aSPeter Maydell         int win = (addr - PCI_SMAP0) >> 2;
2327468d73aSPeter Maydell         return s->smap[win];
2337468d73aSPeter Maydell     }
2347468d73aSPeter Maydell     default:
2357468d73aSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
2367468d73aSPeter Maydell                       "pci_vpb_reg_read: Bad offset %x\n", (int)addr);
2377468d73aSPeter Maydell         return 0;
2387468d73aSPeter Maydell     }
2397468d73aSPeter Maydell }
2407468d73aSPeter Maydell 
2417468d73aSPeter Maydell static const MemoryRegionOps pci_vpb_reg_ops = {
2427468d73aSPeter Maydell     .read = pci_vpb_reg_read,
2437468d73aSPeter Maydell     .write = pci_vpb_reg_write,
2447468d73aSPeter Maydell     .endianness = DEVICE_NATIVE_ENDIAN,
2457468d73aSPeter Maydell     .valid = {
2467468d73aSPeter Maydell         .min_access_size = 4,
2477468d73aSPeter Maydell         .max_access_size = 4,
2487468d73aSPeter Maydell     },
2497468d73aSPeter Maydell };
2507468d73aSPeter Maydell 
251bc04d891SPeter Maydell static int pci_vpb_broken_irq(int slot, int irq)
252bc04d891SPeter Maydell {
253bc04d891SPeter Maydell     /* Determine whether this IRQ value for this slot represents a
254bc04d891SPeter Maydell      * known broken Linux kernel behaviour for this slot.
255bc04d891SPeter Maydell      * Return one of the PCI_VPB_IRQMAP_ constants:
256bc04d891SPeter Maydell      *   BROKEN : if this definitely looks like a broken kernel
257bc04d891SPeter Maydell      *   FORCE_OK : if this definitely looks good
258bc04d891SPeter Maydell      *   ASSUME_OK : if we can't tell
259bc04d891SPeter Maydell      */
260bc04d891SPeter Maydell     slot %= PCI_NUM_PINS;
261bc04d891SPeter Maydell 
262bc04d891SPeter Maydell     if (irq == 27) {
263bc04d891SPeter Maydell         if (slot == 2) {
264bc04d891SPeter Maydell             /* Might be a Phase I kernel, or might be a fixed kernel,
265bc04d891SPeter Maydell              * since slot 2 is where we expect this IRQ.
266bc04d891SPeter Maydell              */
267bc04d891SPeter Maydell             return PCI_VPB_IRQMAP_ASSUME_OK;
268bc04d891SPeter Maydell         }
269bc04d891SPeter Maydell         /* Phase I kernel */
270bc04d891SPeter Maydell         return PCI_VPB_IRQMAP_BROKEN;
271bc04d891SPeter Maydell     }
272bc04d891SPeter Maydell     if (irq == slot + 27) {
273bc04d891SPeter Maydell         /* Phase II kernel */
274bc04d891SPeter Maydell         return PCI_VPB_IRQMAP_BROKEN;
275bc04d891SPeter Maydell     }
276bc04d891SPeter Maydell     if (irq == slot + 27 + 64) {
277bc04d891SPeter Maydell         /* Phase III kernel */
278bc04d891SPeter Maydell         return PCI_VPB_IRQMAP_BROKEN;
279bc04d891SPeter Maydell     }
280bc04d891SPeter Maydell     /* Anything else must be a fixed kernel, possibly using an
281bc04d891SPeter Maydell      * arbitrary irq map.
282bc04d891SPeter Maydell      */
283bc04d891SPeter Maydell     return PCI_VPB_IRQMAP_FORCE_OK;
284bc04d891SPeter Maydell }
285bc04d891SPeter Maydell 
286c0907c9eSPaolo Bonzini static void pci_vpb_config_write(void *opaque, hwaddr addr,
287c0907c9eSPaolo Bonzini                                  uint64_t val, unsigned size)
288c0907c9eSPaolo Bonzini {
28966a96d70SPeter Maydell     PCIVPBState *s = opaque;
29066a96d70SPeter Maydell     if (!s->realview && (addr & 0xff) == PCI_INTERRUPT_LINE
29166a96d70SPeter Maydell         && s->irq_mapping == PCI_VPB_IRQMAP_ASSUME_OK) {
29266a96d70SPeter Maydell         uint8_t devfn = addr >> 8;
293bc04d891SPeter Maydell         s->irq_mapping = pci_vpb_broken_irq(PCI_SLOT(devfn), val);
29466a96d70SPeter Maydell     }
295af9277e6SPeter Maydell     pci_data_write(&s->pci_bus, addr, val, size);
296c0907c9eSPaolo Bonzini }
297c0907c9eSPaolo Bonzini 
298c0907c9eSPaolo Bonzini static uint64_t pci_vpb_config_read(void *opaque, hwaddr addr,
299c0907c9eSPaolo Bonzini                                     unsigned size)
300c0907c9eSPaolo Bonzini {
30166a96d70SPeter Maydell     PCIVPBState *s = opaque;
302c0907c9eSPaolo Bonzini     uint32_t val;
303af9277e6SPeter Maydell     val = pci_data_read(&s->pci_bus, addr, size);
304c0907c9eSPaolo Bonzini     return val;
305c0907c9eSPaolo Bonzini }
306c0907c9eSPaolo Bonzini 
307c0907c9eSPaolo Bonzini static const MemoryRegionOps pci_vpb_config_ops = {
308c0907c9eSPaolo Bonzini     .read = pci_vpb_config_read,
309c0907c9eSPaolo Bonzini     .write = pci_vpb_config_write,
310c0907c9eSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
311c0907c9eSPaolo Bonzini };
312c0907c9eSPaolo Bonzini 
313c0907c9eSPaolo Bonzini static int pci_vpb_map_irq(PCIDevice *d, int irq_num)
314c0907c9eSPaolo Bonzini {
315fd56e061SDavid Gibson     PCIVPBState *s = container_of(pci_get_bus(d), PCIVPBState, pci_bus);
31666a96d70SPeter Maydell 
31766a96d70SPeter Maydell     if (s->irq_mapping == PCI_VPB_IRQMAP_BROKEN) {
31866a96d70SPeter Maydell         /* Legacy broken IRQ mapping for compatibility with old and
31966a96d70SPeter Maydell          * buggy Linux guests
32066a96d70SPeter Maydell          */
321c0907c9eSPaolo Bonzini         return irq_num;
322c0907c9eSPaolo Bonzini     }
323c0907c9eSPaolo Bonzini 
32466a96d70SPeter Maydell     /* Slot to IRQ mapping for RealView Platform Baseboard 926 backplane
32566a96d70SPeter Maydell      *      name    slot    IntA    IntB    IntC    IntD
32666a96d70SPeter Maydell      *      A       31      IRQ28   IRQ29   IRQ30   IRQ27
32766a96d70SPeter Maydell      *      B       30      IRQ27   IRQ28   IRQ29   IRQ30
32866a96d70SPeter Maydell      *      C       29      IRQ30   IRQ27   IRQ28   IRQ29
32966a96d70SPeter Maydell      * Slot C is for the host bridge; A and B the peripherals.
33066a96d70SPeter Maydell      * Our output irqs 0..3 correspond to the baseboard's 27..30.
33166a96d70SPeter Maydell      *
33266a96d70SPeter Maydell      * This mapping function takes account of an oddity in the PB926
33366a96d70SPeter Maydell      * board wiring, where the FPGA's P_nINTA input is connected to
33466a96d70SPeter Maydell      * the INTB connection on the board PCI edge connector, P_nINTB
33566a96d70SPeter Maydell      * is connected to INTC, and so on, so everything is one number
33666a96d70SPeter Maydell      * further round from where you might expect.
33766a96d70SPeter Maydell      */
33866a96d70SPeter Maydell     return pci_swizzle_map_irq_fn(d, irq_num + 2);
33966a96d70SPeter Maydell }
34066a96d70SPeter Maydell 
34166a96d70SPeter Maydell static int pci_vpb_rv_map_irq(PCIDevice *d, int irq_num)
34266a96d70SPeter Maydell {
34366a96d70SPeter Maydell     /* Slot to IRQ mapping for RealView EB and PB1176 backplane
34466a96d70SPeter Maydell      *      name    slot    IntA    IntB    IntC    IntD
34566a96d70SPeter Maydell      *      A       31      IRQ50   IRQ51   IRQ48   IRQ49
34666a96d70SPeter Maydell      *      B       30      IRQ49   IRQ50   IRQ51   IRQ48
34766a96d70SPeter Maydell      *      C       29      IRQ48   IRQ49   IRQ50   IRQ51
34866a96d70SPeter Maydell      * Slot C is for the host bridge; A and B the peripherals.
34966a96d70SPeter Maydell      * Our output irqs 0..3 correspond to the baseboard's 48..51.
35066a96d70SPeter Maydell      *
35166a96d70SPeter Maydell      * The PB1176 and EB boards don't have the PB926 wiring oddity
35266a96d70SPeter Maydell      * described above; P_nINTA connects to INTA, P_nINTB to INTB
35366a96d70SPeter Maydell      * and so on, which is why this mapping function is different.
35466a96d70SPeter Maydell      */
35566a96d70SPeter Maydell     return pci_swizzle_map_irq_fn(d, irq_num + 3);
35666a96d70SPeter Maydell }
35766a96d70SPeter Maydell 
358c0907c9eSPaolo Bonzini static void pci_vpb_set_irq(void *opaque, int irq_num, int level)
359c0907c9eSPaolo Bonzini {
360c0907c9eSPaolo Bonzini     qemu_irq *pic = opaque;
361c0907c9eSPaolo Bonzini 
362c0907c9eSPaolo Bonzini     qemu_set_irq(pic[irq_num], level);
363c0907c9eSPaolo Bonzini }
364c0907c9eSPaolo Bonzini 
36566a96d70SPeter Maydell static void pci_vpb_reset(DeviceState *d)
36666a96d70SPeter Maydell {
36766a96d70SPeter Maydell     PCIVPBState *s = PCI_VPB(d);
36866a96d70SPeter Maydell 
3697468d73aSPeter Maydell     s->imap[0] = 0;
3707468d73aSPeter Maydell     s->imap[1] = 0;
3717468d73aSPeter Maydell     s->imap[2] = 0;
3727468d73aSPeter Maydell     s->smap[0] = 0;
3737468d73aSPeter Maydell     s->smap[1] = 0;
3747468d73aSPeter Maydell     s->smap[2] = 0;
3757468d73aSPeter Maydell     s->selfid = 0;
3767468d73aSPeter Maydell     s->flags = 0;
377913b4b6bSPeter Maydell     s->irq_mapping = s->irq_mapping_prop;
37889a32d32SPeter Maydell 
37989a32d32SPeter Maydell     pci_vpb_update_all_windows(s);
38066a96d70SPeter Maydell }
38166a96d70SPeter Maydell 
3820688810bSPeter Maydell static void pci_vpb_init(Object *obj)
3830688810bSPeter Maydell {
3840688810bSPeter Maydell     PCIVPBState *s = PCI_VPB(obj);
3850688810bSPeter Maydell 
38689a32d32SPeter Maydell     /* Window sizes for VersatilePB; realview_pci's init will override */
38789a32d32SPeter Maydell     s->mem_win_size[0] = 0x0c000000;
38889a32d32SPeter Maydell     s->mem_win_size[1] = 0x10000000;
38989a32d32SPeter Maydell     s->mem_win_size[2] = 0x10000000;
3900688810bSPeter Maydell }
3910688810bSPeter Maydell 
392cd93dbf3SPeter Maydell static void pci_vpb_realize(DeviceState *dev, Error **errp)
393c0907c9eSPaolo Bonzini {
394cd93dbf3SPeter Maydell     PCIVPBState *s = PCI_VPB(dev);
395d28fca15SLaurent Vivier     PCIHostState *h = PCI_HOST_BRIDGE(dev);
396cd93dbf3SPeter Maydell     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
39766a96d70SPeter Maydell     pci_map_irq_fn mapfn;
398c0907c9eSPaolo Bonzini     int i;
399c0907c9eSPaolo Bonzini 
400d28fca15SLaurent Vivier     memory_region_init(&s->pci_io_space, OBJECT(s), "pci_io", 1ULL << 32);
401d28fca15SLaurent Vivier     memory_region_init(&s->pci_mem_space, OBJECT(s), "pci_mem", 1ULL << 32);
402d28fca15SLaurent Vivier 
4031115ff6dSDavid Gibson     pci_root_bus_new_inplace(&s->pci_bus, sizeof(s->pci_bus), dev, "pci",
404d28fca15SLaurent Vivier                              &s->pci_mem_space, &s->pci_io_space,
405d28fca15SLaurent Vivier                              PCI_DEVFN(11, 0), TYPE_PCI_BUS);
406d28fca15SLaurent Vivier     h->bus = &s->pci_bus;
407d28fca15SLaurent Vivier 
408d28fca15SLaurent Vivier     object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_VERSATILE_PCI_HOST);
409d28fca15SLaurent Vivier     qdev_set_parent_bus(DEVICE(&s->pci_dev), BUS(&s->pci_bus));
410d28fca15SLaurent Vivier 
411c0907c9eSPaolo Bonzini     for (i = 0; i < 4; i++) {
412cd93dbf3SPeter Maydell         sysbus_init_irq(sbd, &s->irq[i]);
413c0907c9eSPaolo Bonzini     }
4140688810bSPeter Maydell 
41566a96d70SPeter Maydell     if (s->realview) {
41666a96d70SPeter Maydell         mapfn = pci_vpb_rv_map_irq;
41766a96d70SPeter Maydell     } else {
41866a96d70SPeter Maydell         mapfn = pci_vpb_map_irq;
41966a96d70SPeter Maydell     }
42066a96d70SPeter Maydell 
42166a96d70SPeter Maydell     pci_bus_irqs(&s->pci_bus, pci_vpb_set_irq, mapfn, s->irq, 4);
422c0907c9eSPaolo Bonzini 
423c0907c9eSPaolo Bonzini     /* Our memory regions are:
4247468d73aSPeter Maydell      * 0 : our control registers
4257468d73aSPeter Maydell      * 1 : PCI self config window
4267468d73aSPeter Maydell      * 2 : PCI config window
4277468d73aSPeter Maydell      * 3 : PCI IO window
42889a32d32SPeter Maydell      * 4..6 : PCI memory windows
429c0907c9eSPaolo Bonzini      */
43040c5dce9SPaolo Bonzini     memory_region_init_io(&s->controlregs, OBJECT(s), &pci_vpb_reg_ops, s,
43140c5dce9SPaolo Bonzini                           "pci-vpb-regs", 0x1000);
4327468d73aSPeter Maydell     sysbus_init_mmio(sbd, &s->controlregs);
43340c5dce9SPaolo Bonzini     memory_region_init_io(&s->mem_config, OBJECT(s), &pci_vpb_config_ops, s,
434c0907c9eSPaolo Bonzini                           "pci-vpb-selfconfig", 0x1000000);
435cd93dbf3SPeter Maydell     sysbus_init_mmio(sbd, &s->mem_config);
43640c5dce9SPaolo Bonzini     memory_region_init_io(&s->mem_config2, OBJECT(s), &pci_vpb_config_ops, s,
437c0907c9eSPaolo Bonzini                           "pci-vpb-config", 0x1000000);
438cd93dbf3SPeter Maydell     sysbus_init_mmio(sbd, &s->mem_config2);
439967c2607SPeter Maydell 
440967c2607SPeter Maydell     /* The window into I/O space is always into a fixed base address;
441967c2607SPeter Maydell      * its size is the same for both realview and versatile.
442967c2607SPeter Maydell      */
44340c5dce9SPaolo Bonzini     memory_region_init_alias(&s->pci_io_window, OBJECT(s), "pci-vbp-io-window",
444967c2607SPeter Maydell                              &s->pci_io_space, 0, 0x100000);
445967c2607SPeter Maydell 
446967c2607SPeter Maydell     sysbus_init_mmio(sbd, &s->pci_io_space);
447c0907c9eSPaolo Bonzini 
44889a32d32SPeter Maydell     /* Create the alias regions corresponding to our three windows onto
44989a32d32SPeter Maydell      * PCI memory space. The sizes vary from board to board; the base
45089a32d32SPeter Maydell      * offsets are guest controllable via the IMAP registers.
45189a32d32SPeter Maydell      */
45289a32d32SPeter Maydell     for (i = 0; i < 3; i++) {
45340c5dce9SPaolo Bonzini         memory_region_init_alias(&s->pci_mem_window[i], OBJECT(s), "pci-vbp-window",
45489a32d32SPeter Maydell                                  &s->pci_mem_space, 0, s->mem_win_size[i]);
45589a32d32SPeter Maydell         sysbus_init_mmio(sbd, &s->pci_mem_window[i]);
45689a32d32SPeter Maydell     }
45789a32d32SPeter Maydell 
4580688810bSPeter Maydell     /* TODO Remove once realize propagates to child devices. */
459b1af7959SMarcel Apfelbaum     object_property_set_bool(OBJECT(&s->pci_bus), true, "realized", errp);
4600688810bSPeter Maydell     object_property_set_bool(OBJECT(&s->pci_dev), true, "realized", errp);
461c0907c9eSPaolo Bonzini }
462c0907c9eSPaolo Bonzini 
4639af21dbeSMarkus Armbruster static void versatile_pci_host_realize(PCIDevice *d, Error **errp)
464c0907c9eSPaolo Bonzini {
465c0907c9eSPaolo Bonzini     pci_set_word(d->config + PCI_STATUS,
466c0907c9eSPaolo Bonzini                  PCI_STATUS_66MHZ | PCI_STATUS_DEVSEL_MEDIUM);
467c0907c9eSPaolo Bonzini     pci_set_byte(d->config + PCI_LATENCY_TIMER, 0x10);
468c0907c9eSPaolo Bonzini }
469c0907c9eSPaolo Bonzini 
470c0907c9eSPaolo Bonzini static void versatile_pci_host_class_init(ObjectClass *klass, void *data)
471c0907c9eSPaolo Bonzini {
472c0907c9eSPaolo Bonzini     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
47308c58f92SMarkus Armbruster     DeviceClass *dc = DEVICE_CLASS(klass);
474c0907c9eSPaolo Bonzini 
4759af21dbeSMarkus Armbruster     k->realize = versatile_pci_host_realize;
476c0907c9eSPaolo Bonzini     k->vendor_id = PCI_VENDOR_ID_XILINX;
477c0907c9eSPaolo Bonzini     k->device_id = PCI_DEVICE_ID_XILINX_XC2VP30;
478c0907c9eSPaolo Bonzini     k->class_id = PCI_CLASS_PROCESSOR_CO;
47908c58f92SMarkus Armbruster     /*
48008c58f92SMarkus Armbruster      * PCI-facing part of the host bridge, not usable without the
48108c58f92SMarkus Armbruster      * host-facing part, which can't be device_add'ed, yet.
48208c58f92SMarkus Armbruster      */
483e90f2a8cSEduardo Habkost     dc->user_creatable = false;
484c0907c9eSPaolo Bonzini }
485c0907c9eSPaolo Bonzini 
486c0907c9eSPaolo Bonzini static const TypeInfo versatile_pci_host_info = {
487cd93dbf3SPeter Maydell     .name          = TYPE_VERSATILE_PCI_HOST,
488c0907c9eSPaolo Bonzini     .parent        = TYPE_PCI_DEVICE,
489c0907c9eSPaolo Bonzini     .instance_size = sizeof(PCIDevice),
490c0907c9eSPaolo Bonzini     .class_init    = versatile_pci_host_class_init,
491fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
492fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
493fd3b02c8SEduardo Habkost         { },
494fd3b02c8SEduardo Habkost     },
495c0907c9eSPaolo Bonzini };
496c0907c9eSPaolo Bonzini 
497913b4b6bSPeter Maydell static Property pci_vpb_properties[] = {
498913b4b6bSPeter Maydell     DEFINE_PROP_UINT8("broken-irq-mapping", PCIVPBState, irq_mapping_prop,
499913b4b6bSPeter Maydell                       PCI_VPB_IRQMAP_ASSUME_OK),
500913b4b6bSPeter Maydell     DEFINE_PROP_END_OF_LIST()
501913b4b6bSPeter Maydell };
502913b4b6bSPeter Maydell 
503c0907c9eSPaolo Bonzini static void pci_vpb_class_init(ObjectClass *klass, void *data)
504c0907c9eSPaolo Bonzini {
505cd93dbf3SPeter Maydell     DeviceClass *dc = DEVICE_CLASS(klass);
506c0907c9eSPaolo Bonzini 
507cd93dbf3SPeter Maydell     dc->realize = pci_vpb_realize;
50866a96d70SPeter Maydell     dc->reset = pci_vpb_reset;
5097468d73aSPeter Maydell     dc->vmsd = &pci_vpb_vmstate;
510913b4b6bSPeter Maydell     dc->props = pci_vpb_properties;
511c0907c9eSPaolo Bonzini }
512c0907c9eSPaolo Bonzini 
513c0907c9eSPaolo Bonzini static const TypeInfo pci_vpb_info = {
514cd93dbf3SPeter Maydell     .name          = TYPE_VERSATILE_PCI,
5150688810bSPeter Maydell     .parent        = TYPE_PCI_HOST_BRIDGE,
516c0907c9eSPaolo Bonzini     .instance_size = sizeof(PCIVPBState),
5170688810bSPeter Maydell     .instance_init = pci_vpb_init,
518c0907c9eSPaolo Bonzini     .class_init    = pci_vpb_class_init,
519c0907c9eSPaolo Bonzini };
520c0907c9eSPaolo Bonzini 
521cd93dbf3SPeter Maydell static void pci_realview_init(Object *obj)
522c0907c9eSPaolo Bonzini {
523cd93dbf3SPeter Maydell     PCIVPBState *s = PCI_VPB(obj);
524c0907c9eSPaolo Bonzini 
525cd93dbf3SPeter Maydell     s->realview = 1;
52689a32d32SPeter Maydell     /* The PCI window sizes are different on Realview boards */
52789a32d32SPeter Maydell     s->mem_win_size[0] = 0x01000000;
52889a32d32SPeter Maydell     s->mem_win_size[1] = 0x04000000;
52989a32d32SPeter Maydell     s->mem_win_size[2] = 0x08000000;
530c0907c9eSPaolo Bonzini }
531c0907c9eSPaolo Bonzini 
532c0907c9eSPaolo Bonzini static const TypeInfo pci_realview_info = {
533c0907c9eSPaolo Bonzini     .name          = "realview_pci",
534cd93dbf3SPeter Maydell     .parent        = TYPE_VERSATILE_PCI,
535cd93dbf3SPeter Maydell     .instance_init = pci_realview_init,
536c0907c9eSPaolo Bonzini };
537c0907c9eSPaolo Bonzini 
538c0907c9eSPaolo Bonzini static void versatile_pci_register_types(void)
539c0907c9eSPaolo Bonzini {
540c0907c9eSPaolo Bonzini     type_register_static(&pci_vpb_info);
541c0907c9eSPaolo Bonzini     type_register_static(&pci_realview_info);
542c0907c9eSPaolo Bonzini     type_register_static(&versatile_pci_host_info);
543c0907c9eSPaolo Bonzini }
544c0907c9eSPaolo Bonzini 
545c0907c9eSPaolo Bonzini type_init(versatile_pci_register_types)
546