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"
1151eae1e7SPhilippe Mathieu-Daudé #include "qemu/units.h"
12c0907c9eSPaolo Bonzini #include "hw/sysbus.h"
13d6454270SMarkus Armbruster #include "migration/vmstate.h"
1464552b6bSMarkus Armbruster #include "hw/irq.h"
15edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
160688810bSPeter Maydell #include "hw/pci/pci_bus.h"
17c0907c9eSPaolo Bonzini #include "hw/pci/pci_host.h"
18a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
1903dd024fSPaolo Bonzini #include "qemu/log.h"
200b8fa32fSMarkus Armbruster #include "qemu/module.h"
21db1015e9SEduardo Habkost #include "qom/object.h"
22c0907c9eSPaolo Bonzini
2366a96d70SPeter Maydell /* Old and buggy versions of QEMU used the wrong mapping from
2466a96d70SPeter Maydell * PCI IRQs to system interrupt lines. Unfortunately the Linux
2566a96d70SPeter Maydell * kernel also had the corresponding bug in setting up interrupts
2666a96d70SPeter Maydell * (so older kernels work on QEMU and not on real hardware).
2766a96d70SPeter Maydell * We automatically detect these broken kernels and flip back
2866a96d70SPeter Maydell * to the broken irq mapping by spotting guest writes to the
2966a96d70SPeter Maydell * PCI_INTERRUPT_LINE register to see where the guest thinks
3066a96d70SPeter Maydell * interrupts are going to be routed. So we start in state
3166a96d70SPeter Maydell * ASSUME_OK on reset, and transition to either BROKEN or
3266a96d70SPeter Maydell * FORCE_OK at the first write to an INTERRUPT_LINE register for
3366a96d70SPeter Maydell * a slot where broken and correct interrupt mapping would differ.
3466a96d70SPeter Maydell * Once in either BROKEN or FORCE_OK we never transition again;
3566a96d70SPeter Maydell * this allows a newer kernel to use the INTERRUPT_LINE
3666a96d70SPeter Maydell * registers arbitrarily once it has indicated that it isn't
3766a96d70SPeter Maydell * broken in its init code somewhere.
38bc04d891SPeter Maydell *
39bc04d891SPeter Maydell * Unfortunately we have to cope with multiple different
40bc04d891SPeter Maydell * variants on the broken kernel behaviour:
41bc04d891SPeter Maydell * phase I (before kernel commit 1bc39ac5d) kernels assume old
42bc04d891SPeter Maydell * QEMU behaviour, so they use IRQ 27 for all slots
43bc04d891SPeter Maydell * phase II (1bc39ac5d and later, but before e3e92a7be6) kernels
44bc04d891SPeter Maydell * swizzle IRQs between slots, but do it wrongly, so they
45bc04d891SPeter Maydell * work only for every fourth PCI card, and only if (like old
46bc04d891SPeter Maydell * QEMU) the PCI host device is at slot 0 rather than where
47bc04d891SPeter Maydell * the h/w actually puts it
48bc04d891SPeter Maydell * phase III (e3e92a7be6 and later) kernels still swizzle IRQs between
49bc04d891SPeter Maydell * slots wrongly, but add a fixed offset of 64 to everything
50bc04d891SPeter Maydell * they write to PCI_INTERRUPT_LINE.
51bc04d891SPeter Maydell *
52bc04d891SPeter Maydell * We live in hope of a mythical phase IV kernel which might
53bc04d891SPeter Maydell * actually behave in ways that work on the hardware. Such a
54bc04d891SPeter Maydell * kernel should probably start off by writing some value neither
55bc04d891SPeter Maydell * 27 nor 91 to slot zero's PCI_INTERRUPT_LINE register to
56bc04d891SPeter Maydell * disable the autodetection. After that it can do what it likes.
57bc04d891SPeter Maydell *
58bc04d891SPeter Maydell * Slot % 4 | hw | I | II | III
59bc04d891SPeter Maydell * -------------------------------
60bc04d891SPeter Maydell * 0 | 29 | 27 | 27 | 91
61bc04d891SPeter Maydell * 1 | 30 | 27 | 28 | 92
62bc04d891SPeter Maydell * 2 | 27 | 27 | 29 | 93
63bc04d891SPeter Maydell * 3 | 28 | 27 | 30 | 94
64913b4b6bSPeter Maydell *
65913b4b6bSPeter Maydell * Since our autodetection is not perfect we also provide a
66913b4b6bSPeter Maydell * property so the user can make us start in BROKEN or FORCE_OK
67913b4b6bSPeter Maydell * on reset if they know they have a bad or good kernel.
6866a96d70SPeter Maydell */
6966a96d70SPeter Maydell enum {
7066a96d70SPeter Maydell PCI_VPB_IRQMAP_ASSUME_OK,
7166a96d70SPeter Maydell PCI_VPB_IRQMAP_BROKEN,
7266a96d70SPeter Maydell PCI_VPB_IRQMAP_FORCE_OK,
7366a96d70SPeter Maydell };
7466a96d70SPeter Maydell
75db1015e9SEduardo Habkost struct PCIVPBState {
760688810bSPeter Maydell PCIHostState parent_obj;
770688810bSPeter Maydell
78c0907c9eSPaolo Bonzini qemu_irq irq[4];
797468d73aSPeter Maydell MemoryRegion controlregs;
80c0907c9eSPaolo Bonzini MemoryRegion mem_config;
81c0907c9eSPaolo Bonzini MemoryRegion mem_config2;
8289a32d32SPeter Maydell /* Containers representing the PCI address spaces */
83967c2607SPeter Maydell MemoryRegion pci_io_space;
8489a32d32SPeter Maydell MemoryRegion pci_mem_space;
8589a32d32SPeter Maydell /* Alias regions into PCI address spaces which we expose as sysbus regions.
8689a32d32SPeter Maydell * The offsets into pci_mem_space are controlled by the imap registers.
8789a32d32SPeter Maydell */
88967c2607SPeter Maydell MemoryRegion pci_io_window;
8989a32d32SPeter Maydell MemoryRegion pci_mem_window[3];
900688810bSPeter Maydell PCIBus pci_bus;
910688810bSPeter Maydell PCIDevice pci_dev;
920688810bSPeter Maydell
930688810bSPeter Maydell /* Constant for life of device: */
940688810bSPeter Maydell int realview;
9589a32d32SPeter Maydell uint32_t mem_win_size[3];
96913b4b6bSPeter Maydell uint8_t irq_mapping_prop;
9766a96d70SPeter Maydell
9866a96d70SPeter Maydell /* Variable state: */
997468d73aSPeter Maydell uint32_t imap[3];
1007468d73aSPeter Maydell uint32_t smap[3];
1017468d73aSPeter Maydell uint32_t selfid;
1027468d73aSPeter Maydell uint32_t flags;
10366a96d70SPeter Maydell uint8_t irq_mapping;
104db1015e9SEduardo Habkost };
105db1015e9SEduardo Habkost typedef struct PCIVPBState PCIVPBState;
106c0907c9eSPaolo Bonzini
pci_vpb_update_window(PCIVPBState * s,int i)10789a32d32SPeter Maydell static void pci_vpb_update_window(PCIVPBState *s, int i)
10889a32d32SPeter Maydell {
10989a32d32SPeter Maydell /* Adjust the offset of the alias region we use for
11089a32d32SPeter Maydell * the memory window i to account for a change in the
11189a32d32SPeter Maydell * value of the corresponding IMAP register.
11289a32d32SPeter Maydell * Note that the semantics of the IMAP register differ
11389a32d32SPeter Maydell * for realview and versatile variants of the controller.
11489a32d32SPeter Maydell */
11589a32d32SPeter Maydell hwaddr offset;
11689a32d32SPeter Maydell if (s->realview) {
11789a32d32SPeter Maydell /* Top bits of register (masked according to window size) provide
11889a32d32SPeter Maydell * top bits of PCI address.
11989a32d32SPeter Maydell */
12089a32d32SPeter Maydell offset = s->imap[i] & ~(s->mem_win_size[i] - 1);
12189a32d32SPeter Maydell } else {
12289a32d32SPeter Maydell /* Bottom 4 bits of register provide top 4 bits of PCI address */
12389a32d32SPeter Maydell offset = s->imap[i] << 28;
12489a32d32SPeter Maydell }
12589a32d32SPeter Maydell memory_region_set_alias_offset(&s->pci_mem_window[i], offset);
12689a32d32SPeter Maydell }
12789a32d32SPeter Maydell
pci_vpb_update_all_windows(PCIVPBState * s)12889a32d32SPeter Maydell static void pci_vpb_update_all_windows(PCIVPBState *s)
12989a32d32SPeter Maydell {
13089a32d32SPeter Maydell /* Update all alias windows based on the current register state */
13189a32d32SPeter Maydell int i;
13289a32d32SPeter Maydell
13389a32d32SPeter Maydell for (i = 0; i < 3; i++) {
13489a32d32SPeter Maydell pci_vpb_update_window(s, i);
13589a32d32SPeter Maydell }
13689a32d32SPeter Maydell }
13789a32d32SPeter Maydell
pci_vpb_post_load(void * opaque,int version_id)13889a32d32SPeter Maydell static int pci_vpb_post_load(void *opaque, int version_id)
13989a32d32SPeter Maydell {
14089a32d32SPeter Maydell PCIVPBState *s = opaque;
14189a32d32SPeter Maydell pci_vpb_update_all_windows(s);
14289a32d32SPeter Maydell return 0;
14389a32d32SPeter Maydell }
14489a32d32SPeter Maydell
1457468d73aSPeter Maydell static const VMStateDescription pci_vpb_vmstate = {
1467468d73aSPeter Maydell .name = "versatile-pci",
1477468d73aSPeter Maydell .version_id = 1,
1487468d73aSPeter Maydell .minimum_version_id = 1,
14989a32d32SPeter Maydell .post_load = pci_vpb_post_load,
150e2bd53a3SRichard Henderson .fields = (const VMStateField[]) {
1517468d73aSPeter Maydell VMSTATE_UINT32_ARRAY(imap, PCIVPBState, 3),
1527468d73aSPeter Maydell VMSTATE_UINT32_ARRAY(smap, PCIVPBState, 3),
1537468d73aSPeter Maydell VMSTATE_UINT32(selfid, PCIVPBState),
1547468d73aSPeter Maydell VMSTATE_UINT32(flags, PCIVPBState),
1557468d73aSPeter Maydell VMSTATE_UINT8(irq_mapping, PCIVPBState),
1567468d73aSPeter Maydell VMSTATE_END_OF_LIST()
1577468d73aSPeter Maydell }
1587468d73aSPeter Maydell };
1597468d73aSPeter Maydell
160cd93dbf3SPeter Maydell #define TYPE_VERSATILE_PCI "versatile_pci"
1618110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(PCIVPBState, PCI_VPB,
1628110fa1dSEduardo Habkost TYPE_VERSATILE_PCI)
163cd93dbf3SPeter Maydell
164cd93dbf3SPeter Maydell #define TYPE_VERSATILE_PCI_HOST "versatile_pci_host"
1658110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(PCIDevice, PCI_VPB_HOST,
1668110fa1dSEduardo Habkost TYPE_VERSATILE_PCI_HOST)
167cd93dbf3SPeter Maydell
1687468d73aSPeter Maydell typedef enum {
1697468d73aSPeter Maydell PCI_IMAP0 = 0x0,
1707468d73aSPeter Maydell PCI_IMAP1 = 0x4,
1717468d73aSPeter Maydell PCI_IMAP2 = 0x8,
1727468d73aSPeter Maydell PCI_SELFID = 0xc,
1737468d73aSPeter Maydell PCI_FLAGS = 0x10,
1747468d73aSPeter Maydell PCI_SMAP0 = 0x14,
1757468d73aSPeter Maydell PCI_SMAP1 = 0x18,
1767468d73aSPeter Maydell PCI_SMAP2 = 0x1c,
1777468d73aSPeter Maydell } PCIVPBControlRegs;
1787468d73aSPeter Maydell
pci_vpb_reg_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)1797468d73aSPeter Maydell static void pci_vpb_reg_write(void *opaque, hwaddr addr,
1807468d73aSPeter Maydell uint64_t val, unsigned size)
1817468d73aSPeter Maydell {
1827468d73aSPeter Maydell PCIVPBState *s = opaque;
1837468d73aSPeter Maydell
1847468d73aSPeter Maydell switch (addr) {
1857468d73aSPeter Maydell case PCI_IMAP0:
1867468d73aSPeter Maydell case PCI_IMAP1:
1877468d73aSPeter Maydell case PCI_IMAP2:
1887468d73aSPeter Maydell {
1897468d73aSPeter Maydell int win = (addr - PCI_IMAP0) >> 2;
1907468d73aSPeter Maydell s->imap[win] = val;
19189a32d32SPeter Maydell pci_vpb_update_window(s, win);
1927468d73aSPeter Maydell break;
1937468d73aSPeter Maydell }
1947468d73aSPeter Maydell case PCI_SELFID:
1957468d73aSPeter Maydell s->selfid = val;
1967468d73aSPeter Maydell break;
1977468d73aSPeter Maydell case PCI_FLAGS:
1987468d73aSPeter Maydell s->flags = val;
1997468d73aSPeter Maydell break;
2007468d73aSPeter Maydell case PCI_SMAP0:
2017468d73aSPeter Maydell case PCI_SMAP1:
2027468d73aSPeter Maydell case PCI_SMAP2:
2037468d73aSPeter Maydell {
2047468d73aSPeter Maydell int win = (addr - PCI_SMAP0) >> 2;
2057468d73aSPeter Maydell s->smap[win] = val;
2067468d73aSPeter Maydell break;
2077468d73aSPeter Maydell }
2087468d73aSPeter Maydell default:
2097468d73aSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
2107468d73aSPeter Maydell "pci_vpb_reg_write: Bad offset %x\n", (int)addr);
2117468d73aSPeter Maydell break;
2127468d73aSPeter Maydell }
2137468d73aSPeter Maydell }
2147468d73aSPeter Maydell
pci_vpb_reg_read(void * opaque,hwaddr addr,unsigned size)2157468d73aSPeter Maydell static uint64_t pci_vpb_reg_read(void *opaque, hwaddr addr,
2167468d73aSPeter Maydell unsigned size)
2177468d73aSPeter Maydell {
2187468d73aSPeter Maydell PCIVPBState *s = opaque;
2197468d73aSPeter Maydell
2207468d73aSPeter Maydell switch (addr) {
2217468d73aSPeter Maydell case PCI_IMAP0:
2227468d73aSPeter Maydell case PCI_IMAP1:
2237468d73aSPeter Maydell case PCI_IMAP2:
2247468d73aSPeter Maydell {
2257468d73aSPeter Maydell int win = (addr - PCI_IMAP0) >> 2;
2267468d73aSPeter Maydell return s->imap[win];
2277468d73aSPeter Maydell }
2287468d73aSPeter Maydell case PCI_SELFID:
2297468d73aSPeter Maydell return s->selfid;
2307468d73aSPeter Maydell case PCI_FLAGS:
2317468d73aSPeter Maydell return s->flags;
2327468d73aSPeter Maydell case PCI_SMAP0:
2337468d73aSPeter Maydell case PCI_SMAP1:
2347468d73aSPeter Maydell case PCI_SMAP2:
2357468d73aSPeter Maydell {
2367468d73aSPeter Maydell int win = (addr - PCI_SMAP0) >> 2;
2377468d73aSPeter Maydell return s->smap[win];
2387468d73aSPeter Maydell }
2397468d73aSPeter Maydell default:
2407468d73aSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR,
2417468d73aSPeter Maydell "pci_vpb_reg_read: Bad offset %x\n", (int)addr);
2427468d73aSPeter Maydell return 0;
2437468d73aSPeter Maydell }
2447468d73aSPeter Maydell }
2457468d73aSPeter Maydell
2467468d73aSPeter Maydell static const MemoryRegionOps pci_vpb_reg_ops = {
2477468d73aSPeter Maydell .read = pci_vpb_reg_read,
2487468d73aSPeter Maydell .write = pci_vpb_reg_write,
2497468d73aSPeter Maydell .endianness = DEVICE_NATIVE_ENDIAN,
2507468d73aSPeter Maydell .valid = {
2517468d73aSPeter Maydell .min_access_size = 4,
2527468d73aSPeter Maydell .max_access_size = 4,
2537468d73aSPeter Maydell },
2547468d73aSPeter Maydell };
2557468d73aSPeter Maydell
pci_vpb_broken_irq(int slot,int irq)256bc04d891SPeter Maydell static int pci_vpb_broken_irq(int slot, int irq)
257bc04d891SPeter Maydell {
258bc04d891SPeter Maydell /* Determine whether this IRQ value for this slot represents a
259bc04d891SPeter Maydell * known broken Linux kernel behaviour for this slot.
260bc04d891SPeter Maydell * Return one of the PCI_VPB_IRQMAP_ constants:
261bc04d891SPeter Maydell * BROKEN : if this definitely looks like a broken kernel
262bc04d891SPeter Maydell * FORCE_OK : if this definitely looks good
263bc04d891SPeter Maydell * ASSUME_OK : if we can't tell
264bc04d891SPeter Maydell */
265bc04d891SPeter Maydell slot %= PCI_NUM_PINS;
266bc04d891SPeter Maydell
267bc04d891SPeter Maydell if (irq == 27) {
268bc04d891SPeter Maydell if (slot == 2) {
269bc04d891SPeter Maydell /* Might be a Phase I kernel, or might be a fixed kernel,
270bc04d891SPeter Maydell * since slot 2 is where we expect this IRQ.
271bc04d891SPeter Maydell */
272bc04d891SPeter Maydell return PCI_VPB_IRQMAP_ASSUME_OK;
273bc04d891SPeter Maydell }
274bc04d891SPeter Maydell /* Phase I kernel */
275bc04d891SPeter Maydell return PCI_VPB_IRQMAP_BROKEN;
276bc04d891SPeter Maydell }
277bc04d891SPeter Maydell if (irq == slot + 27) {
278bc04d891SPeter Maydell /* Phase II kernel */
279bc04d891SPeter Maydell return PCI_VPB_IRQMAP_BROKEN;
280bc04d891SPeter Maydell }
281bc04d891SPeter Maydell if (irq == slot + 27 + 64) {
282bc04d891SPeter Maydell /* Phase III kernel */
283bc04d891SPeter Maydell return PCI_VPB_IRQMAP_BROKEN;
284bc04d891SPeter Maydell }
285bc04d891SPeter Maydell /* Anything else must be a fixed kernel, possibly using an
286bc04d891SPeter Maydell * arbitrary irq map.
287bc04d891SPeter Maydell */
288bc04d891SPeter Maydell return PCI_VPB_IRQMAP_FORCE_OK;
289bc04d891SPeter Maydell }
290bc04d891SPeter Maydell
pci_vpb_config_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)291c0907c9eSPaolo Bonzini static void pci_vpb_config_write(void *opaque, hwaddr addr,
292c0907c9eSPaolo Bonzini uint64_t val, unsigned size)
293c0907c9eSPaolo Bonzini {
29466a96d70SPeter Maydell PCIVPBState *s = opaque;
29566a96d70SPeter Maydell if (!s->realview && (addr & 0xff) == PCI_INTERRUPT_LINE
29666a96d70SPeter Maydell && s->irq_mapping == PCI_VPB_IRQMAP_ASSUME_OK) {
29766a96d70SPeter Maydell uint8_t devfn = addr >> 8;
298bc04d891SPeter Maydell s->irq_mapping = pci_vpb_broken_irq(PCI_SLOT(devfn), val);
29966a96d70SPeter Maydell }
300af9277e6SPeter Maydell pci_data_write(&s->pci_bus, addr, val, size);
301c0907c9eSPaolo Bonzini }
302c0907c9eSPaolo Bonzini
pci_vpb_config_read(void * opaque,hwaddr addr,unsigned size)303c0907c9eSPaolo Bonzini static uint64_t pci_vpb_config_read(void *opaque, hwaddr addr,
304c0907c9eSPaolo Bonzini unsigned size)
305c0907c9eSPaolo Bonzini {
30666a96d70SPeter Maydell PCIVPBState *s = opaque;
307c0907c9eSPaolo Bonzini uint32_t val;
308af9277e6SPeter Maydell val = pci_data_read(&s->pci_bus, addr, size);
309c0907c9eSPaolo Bonzini return val;
310c0907c9eSPaolo Bonzini }
311c0907c9eSPaolo Bonzini
312c0907c9eSPaolo Bonzini static const MemoryRegionOps pci_vpb_config_ops = {
313c0907c9eSPaolo Bonzini .read = pci_vpb_config_read,
314c0907c9eSPaolo Bonzini .write = pci_vpb_config_write,
315c0907c9eSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN,
316c0907c9eSPaolo Bonzini };
317c0907c9eSPaolo Bonzini
pci_vpb_map_irq(PCIDevice * d,int irq_num)318c0907c9eSPaolo Bonzini static int pci_vpb_map_irq(PCIDevice *d, int irq_num)
319c0907c9eSPaolo Bonzini {
320fd56e061SDavid Gibson PCIVPBState *s = container_of(pci_get_bus(d), PCIVPBState, pci_bus);
32166a96d70SPeter Maydell
32266a96d70SPeter Maydell if (s->irq_mapping == PCI_VPB_IRQMAP_BROKEN) {
32366a96d70SPeter Maydell /* Legacy broken IRQ mapping for compatibility with old and
32466a96d70SPeter Maydell * buggy Linux guests
32566a96d70SPeter Maydell */
326c0907c9eSPaolo Bonzini return irq_num;
327c0907c9eSPaolo Bonzini }
328c0907c9eSPaolo Bonzini
32966a96d70SPeter Maydell /* Slot to IRQ mapping for RealView Platform Baseboard 926 backplane
33066a96d70SPeter Maydell * name slot IntA IntB IntC IntD
33166a96d70SPeter Maydell * A 31 IRQ28 IRQ29 IRQ30 IRQ27
33266a96d70SPeter Maydell * B 30 IRQ27 IRQ28 IRQ29 IRQ30
33366a96d70SPeter Maydell * C 29 IRQ30 IRQ27 IRQ28 IRQ29
33466a96d70SPeter Maydell * Slot C is for the host bridge; A and B the peripherals.
33566a96d70SPeter Maydell * Our output irqs 0..3 correspond to the baseboard's 27..30.
33666a96d70SPeter Maydell *
33766a96d70SPeter Maydell * This mapping function takes account of an oddity in the PB926
33866a96d70SPeter Maydell * board wiring, where the FPGA's P_nINTA input is connected to
33966a96d70SPeter Maydell * the INTB connection on the board PCI edge connector, P_nINTB
34066a96d70SPeter Maydell * is connected to INTC, and so on, so everything is one number
34166a96d70SPeter Maydell * further round from where you might expect.
34266a96d70SPeter Maydell */
34366a96d70SPeter Maydell return pci_swizzle_map_irq_fn(d, irq_num + 2);
34466a96d70SPeter Maydell }
34566a96d70SPeter Maydell
pci_vpb_rv_map_irq(PCIDevice * d,int irq_num)34666a96d70SPeter Maydell static int pci_vpb_rv_map_irq(PCIDevice *d, int irq_num)
34766a96d70SPeter Maydell {
34866a96d70SPeter Maydell /* Slot to IRQ mapping for RealView EB and PB1176 backplane
34966a96d70SPeter Maydell * name slot IntA IntB IntC IntD
35066a96d70SPeter Maydell * A 31 IRQ50 IRQ51 IRQ48 IRQ49
35166a96d70SPeter Maydell * B 30 IRQ49 IRQ50 IRQ51 IRQ48
35266a96d70SPeter Maydell * C 29 IRQ48 IRQ49 IRQ50 IRQ51
35366a96d70SPeter Maydell * Slot C is for the host bridge; A and B the peripherals.
35466a96d70SPeter Maydell * Our output irqs 0..3 correspond to the baseboard's 48..51.
35566a96d70SPeter Maydell *
35666a96d70SPeter Maydell * The PB1176 and EB boards don't have the PB926 wiring oddity
35766a96d70SPeter Maydell * described above; P_nINTA connects to INTA, P_nINTB to INTB
35866a96d70SPeter Maydell * and so on, which is why this mapping function is different.
35966a96d70SPeter Maydell */
36066a96d70SPeter Maydell return pci_swizzle_map_irq_fn(d, irq_num + 3);
36166a96d70SPeter Maydell }
36266a96d70SPeter Maydell
pci_vpb_set_irq(void * opaque,int irq_num,int level)363c0907c9eSPaolo Bonzini static void pci_vpb_set_irq(void *opaque, int irq_num, int level)
364c0907c9eSPaolo Bonzini {
365c0907c9eSPaolo Bonzini qemu_irq *pic = opaque;
366c0907c9eSPaolo Bonzini
367c0907c9eSPaolo Bonzini qemu_set_irq(pic[irq_num], level);
368c0907c9eSPaolo Bonzini }
369c0907c9eSPaolo Bonzini
pci_vpb_reset(DeviceState * d)37066a96d70SPeter Maydell static void pci_vpb_reset(DeviceState *d)
37166a96d70SPeter Maydell {
37266a96d70SPeter Maydell PCIVPBState *s = PCI_VPB(d);
37366a96d70SPeter Maydell
3747468d73aSPeter Maydell s->imap[0] = 0;
3757468d73aSPeter Maydell s->imap[1] = 0;
3767468d73aSPeter Maydell s->imap[2] = 0;
3777468d73aSPeter Maydell s->smap[0] = 0;
3787468d73aSPeter Maydell s->smap[1] = 0;
3797468d73aSPeter Maydell s->smap[2] = 0;
3807468d73aSPeter Maydell s->selfid = 0;
3817468d73aSPeter Maydell s->flags = 0;
382913b4b6bSPeter Maydell s->irq_mapping = s->irq_mapping_prop;
38389a32d32SPeter Maydell
38489a32d32SPeter Maydell pci_vpb_update_all_windows(s);
38566a96d70SPeter Maydell }
38666a96d70SPeter Maydell
pci_vpb_init(Object * obj)3870688810bSPeter Maydell static void pci_vpb_init(Object *obj)
3880688810bSPeter Maydell {
3890688810bSPeter Maydell PCIVPBState *s = PCI_VPB(obj);
3900688810bSPeter Maydell
39189a32d32SPeter Maydell /* Window sizes for VersatilePB; realview_pci's init will override */
39289a32d32SPeter Maydell s->mem_win_size[0] = 0x0c000000;
39389a32d32SPeter Maydell s->mem_win_size[1] = 0x10000000;
39489a32d32SPeter Maydell s->mem_win_size[2] = 0x10000000;
3950688810bSPeter Maydell }
3960688810bSPeter Maydell
pci_vpb_realize(DeviceState * dev,Error ** errp)397cd93dbf3SPeter Maydell static void pci_vpb_realize(DeviceState *dev, Error **errp)
398c0907c9eSPaolo Bonzini {
399cd93dbf3SPeter Maydell PCIVPBState *s = PCI_VPB(dev);
400d28fca15SLaurent Vivier PCIHostState *h = PCI_HOST_BRIDGE(dev);
401cd93dbf3SPeter Maydell SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
40266a96d70SPeter Maydell pci_map_irq_fn mapfn;
403c0907c9eSPaolo Bonzini int i;
404c0907c9eSPaolo Bonzini
40551eae1e7SPhilippe Mathieu-Daudé memory_region_init(&s->pci_io_space, OBJECT(s), "pci_io", 4 * GiB);
40651eae1e7SPhilippe Mathieu-Daudé memory_region_init(&s->pci_mem_space, OBJECT(s), "pci_mem", 4 * GiB);
407d28fca15SLaurent Vivier
4088d4cdf01SPeter Maydell pci_root_bus_init(&s->pci_bus, sizeof(s->pci_bus), dev, "pci",
409d28fca15SLaurent Vivier &s->pci_mem_space, &s->pci_io_space,
410d28fca15SLaurent Vivier PCI_DEVFN(11, 0), TYPE_PCI_BUS);
411d28fca15SLaurent Vivier h->bus = &s->pci_bus;
412d28fca15SLaurent Vivier
413d28fca15SLaurent Vivier object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_VERSATILE_PCI_HOST);
414d28fca15SLaurent Vivier
415c0907c9eSPaolo Bonzini for (i = 0; i < 4; i++) {
416cd93dbf3SPeter Maydell sysbus_init_irq(sbd, &s->irq[i]);
417c0907c9eSPaolo Bonzini }
4180688810bSPeter Maydell
41966a96d70SPeter Maydell if (s->realview) {
42066a96d70SPeter Maydell mapfn = pci_vpb_rv_map_irq;
42166a96d70SPeter Maydell } else {
42266a96d70SPeter Maydell mapfn = pci_vpb_map_irq;
42366a96d70SPeter Maydell }
42466a96d70SPeter Maydell
425f021f4e9SBernhard Beschow pci_bus_irqs(&s->pci_bus, pci_vpb_set_irq, s->irq, 4);
426f021f4e9SBernhard Beschow pci_bus_map_irqs(&s->pci_bus, mapfn);
427c0907c9eSPaolo Bonzini
428c0907c9eSPaolo Bonzini /* Our memory regions are:
4297468d73aSPeter Maydell * 0 : our control registers
4307468d73aSPeter Maydell * 1 : PCI self config window
4317468d73aSPeter Maydell * 2 : PCI config window
4327468d73aSPeter Maydell * 3 : PCI IO window
43389a32d32SPeter Maydell * 4..6 : PCI memory windows
434c0907c9eSPaolo Bonzini */
43540c5dce9SPaolo Bonzini memory_region_init_io(&s->controlregs, OBJECT(s), &pci_vpb_reg_ops, s,
43640c5dce9SPaolo Bonzini "pci-vpb-regs", 0x1000);
4377468d73aSPeter Maydell sysbus_init_mmio(sbd, &s->controlregs);
43840c5dce9SPaolo Bonzini memory_region_init_io(&s->mem_config, OBJECT(s), &pci_vpb_config_ops, s,
439c0907c9eSPaolo Bonzini "pci-vpb-selfconfig", 0x1000000);
440cd93dbf3SPeter Maydell sysbus_init_mmio(sbd, &s->mem_config);
44140c5dce9SPaolo Bonzini memory_region_init_io(&s->mem_config2, OBJECT(s), &pci_vpb_config_ops, s,
442c0907c9eSPaolo Bonzini "pci-vpb-config", 0x1000000);
443cd93dbf3SPeter Maydell sysbus_init_mmio(sbd, &s->mem_config2);
444967c2607SPeter Maydell
445967c2607SPeter Maydell /* The window into I/O space is always into a fixed base address;
446967c2607SPeter Maydell * its size is the same for both realview and versatile.
447967c2607SPeter Maydell */
44840c5dce9SPaolo Bonzini memory_region_init_alias(&s->pci_io_window, OBJECT(s), "pci-vbp-io-window",
449967c2607SPeter Maydell &s->pci_io_space, 0, 0x100000);
450967c2607SPeter Maydell
451967c2607SPeter Maydell sysbus_init_mmio(sbd, &s->pci_io_space);
452c0907c9eSPaolo Bonzini
45389a32d32SPeter Maydell /* Create the alias regions corresponding to our three windows onto
45489a32d32SPeter Maydell * PCI memory space. The sizes vary from board to board; the base
45589a32d32SPeter Maydell * offsets are guest controllable via the IMAP registers.
45689a32d32SPeter Maydell */
45789a32d32SPeter Maydell for (i = 0; i < 3; i++) {
45840c5dce9SPaolo Bonzini memory_region_init_alias(&s->pci_mem_window[i], OBJECT(s), "pci-vbp-window",
45989a32d32SPeter Maydell &s->pci_mem_space, 0, s->mem_win_size[i]);
46089a32d32SPeter Maydell sysbus_init_mmio(sbd, &s->pci_mem_window[i]);
46189a32d32SPeter Maydell }
46289a32d32SPeter Maydell
4630688810bSPeter Maydell /* TODO Remove once realize propagates to child devices. */
46499ba777eSMarkus Armbruster qdev_realize(DEVICE(&s->pci_dev), BUS(&s->pci_bus), errp);
465c0907c9eSPaolo Bonzini }
466c0907c9eSPaolo Bonzini
versatile_pci_host_realize(PCIDevice * d,Error ** errp)4679af21dbeSMarkus Armbruster static void versatile_pci_host_realize(PCIDevice *d, Error **errp)
468c0907c9eSPaolo Bonzini {
469c0907c9eSPaolo Bonzini pci_set_word(d->config + PCI_STATUS,
470c0907c9eSPaolo Bonzini PCI_STATUS_66MHZ | PCI_STATUS_DEVSEL_MEDIUM);
471c0907c9eSPaolo Bonzini pci_set_byte(d->config + PCI_LATENCY_TIMER, 0x10);
472c0907c9eSPaolo Bonzini }
473c0907c9eSPaolo Bonzini
versatile_pci_host_class_init(ObjectClass * klass,void * data)474c0907c9eSPaolo Bonzini static void versatile_pci_host_class_init(ObjectClass *klass, void *data)
475c0907c9eSPaolo Bonzini {
476c0907c9eSPaolo Bonzini PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
47708c58f92SMarkus Armbruster DeviceClass *dc = DEVICE_CLASS(klass);
478c0907c9eSPaolo Bonzini
4799af21dbeSMarkus Armbruster k->realize = versatile_pci_host_realize;
480c0907c9eSPaolo Bonzini k->vendor_id = PCI_VENDOR_ID_XILINX;
481c0907c9eSPaolo Bonzini k->device_id = PCI_DEVICE_ID_XILINX_XC2VP30;
482c0907c9eSPaolo Bonzini k->class_id = PCI_CLASS_PROCESSOR_CO;
48308c58f92SMarkus Armbruster /*
48408c58f92SMarkus Armbruster * PCI-facing part of the host bridge, not usable without the
48508c58f92SMarkus Armbruster * host-facing part, which can't be device_add'ed, yet.
48608c58f92SMarkus Armbruster */
487e90f2a8cSEduardo Habkost dc->user_creatable = false;
488c0907c9eSPaolo Bonzini }
489c0907c9eSPaolo Bonzini
490c0907c9eSPaolo Bonzini static const TypeInfo versatile_pci_host_info = {
491cd93dbf3SPeter Maydell .name = TYPE_VERSATILE_PCI_HOST,
492c0907c9eSPaolo Bonzini .parent = TYPE_PCI_DEVICE,
493c0907c9eSPaolo Bonzini .instance_size = sizeof(PCIDevice),
494c0907c9eSPaolo Bonzini .class_init = versatile_pci_host_class_init,
495fd3b02c8SEduardo Habkost .interfaces = (InterfaceInfo[]) {
496fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE },
497fd3b02c8SEduardo Habkost { },
498fd3b02c8SEduardo Habkost },
499c0907c9eSPaolo Bonzini };
500c0907c9eSPaolo Bonzini
501913b4b6bSPeter Maydell static Property pci_vpb_properties[] = {
502913b4b6bSPeter Maydell DEFINE_PROP_UINT8("broken-irq-mapping", PCIVPBState, irq_mapping_prop,
503913b4b6bSPeter Maydell PCI_VPB_IRQMAP_ASSUME_OK),
504913b4b6bSPeter Maydell DEFINE_PROP_END_OF_LIST()
505913b4b6bSPeter Maydell };
506913b4b6bSPeter Maydell
pci_vpb_class_init(ObjectClass * klass,void * data)507c0907c9eSPaolo Bonzini static void pci_vpb_class_init(ObjectClass *klass, void *data)
508c0907c9eSPaolo Bonzini {
509cd93dbf3SPeter Maydell DeviceClass *dc = DEVICE_CLASS(klass);
510c0907c9eSPaolo Bonzini
511cd93dbf3SPeter Maydell dc->realize = pci_vpb_realize;
512*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, pci_vpb_reset);
5137468d73aSPeter Maydell dc->vmsd = &pci_vpb_vmstate;
5144f67d30bSMarc-André Lureau device_class_set_props(dc, pci_vpb_properties);
515c0907c9eSPaolo Bonzini }
516c0907c9eSPaolo Bonzini
517c0907c9eSPaolo Bonzini static const TypeInfo pci_vpb_info = {
518cd93dbf3SPeter Maydell .name = TYPE_VERSATILE_PCI,
5190688810bSPeter Maydell .parent = TYPE_PCI_HOST_BRIDGE,
520c0907c9eSPaolo Bonzini .instance_size = sizeof(PCIVPBState),
5210688810bSPeter Maydell .instance_init = pci_vpb_init,
522c0907c9eSPaolo Bonzini .class_init = pci_vpb_class_init,
523c0907c9eSPaolo Bonzini };
524c0907c9eSPaolo Bonzini
pci_realview_init(Object * obj)525cd93dbf3SPeter Maydell static void pci_realview_init(Object *obj)
526c0907c9eSPaolo Bonzini {
527cd93dbf3SPeter Maydell PCIVPBState *s = PCI_VPB(obj);
528c0907c9eSPaolo Bonzini
529cd93dbf3SPeter Maydell s->realview = 1;
53089a32d32SPeter Maydell /* The PCI window sizes are different on Realview boards */
53189a32d32SPeter Maydell s->mem_win_size[0] = 0x01000000;
53289a32d32SPeter Maydell s->mem_win_size[1] = 0x04000000;
53389a32d32SPeter Maydell s->mem_win_size[2] = 0x08000000;
534c0907c9eSPaolo Bonzini }
535c0907c9eSPaolo Bonzini
536c0907c9eSPaolo Bonzini static const TypeInfo pci_realview_info = {
537c0907c9eSPaolo Bonzini .name = "realview_pci",
538cd93dbf3SPeter Maydell .parent = TYPE_VERSATILE_PCI,
539cd93dbf3SPeter Maydell .instance_init = pci_realview_init,
540c0907c9eSPaolo Bonzini };
541c0907c9eSPaolo Bonzini
versatile_pci_register_types(void)542c0907c9eSPaolo Bonzini static void versatile_pci_register_types(void)
543c0907c9eSPaolo Bonzini {
544c0907c9eSPaolo Bonzini type_register_static(&pci_vpb_info);
545c0907c9eSPaolo Bonzini type_register_static(&pci_realview_info);
546c0907c9eSPaolo Bonzini type_register_static(&versatile_pci_host_info);
547c0907c9eSPaolo Bonzini }
548c0907c9eSPaolo Bonzini
549c0907c9eSPaolo Bonzini type_init(versatile_pci_register_types)
550