hw/pxb: add map_irq func
[qemu/kevin.git] / hw / pci-bridge / pci_expander_bridge.c
blob8660a00219dcfc127a34aee02c8e78893a0767e4
1 /*
2 * PCI Expander Bridge Device Emulation
4 * Copyright (C) 2015 Red Hat Inc
6 * Authors:
7 * Marcel Apfelbaum <marcel@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "hw/pci/pci.h"
14 #include "hw/pci/pci_bus.h"
15 #include "hw/pci/pci_host.h"
16 #include "hw/pci/pci_bus.h"
17 #include "hw/i386/pc.h"
18 #include "qemu/range.h"
19 #include "qemu/error-report.h"
21 #define TYPE_PXB_BUS "pxb-bus"
22 #define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
24 typedef struct PXBBus {
25 /*< private >*/
26 PCIBus parent_obj;
27 /*< public >*/
29 char bus_path[8];
30 } PXBBus;
32 #define TYPE_PXB_DEVICE "pxb"
33 #define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE)
35 typedef struct PXBDev {
36 /*< private >*/
37 PCIDevice parent_obj;
38 /*< public >*/
40 uint8_t bus_nr;
41 } PXBDev;
43 #define TYPE_PXB_HOST "pxb-host"
45 static int pxb_bus_num(PCIBus *bus)
47 PXBDev *pxb = PXB_DEV(bus->parent_dev);
49 return pxb->bus_nr;
52 static bool pxb_is_root(PCIBus *bus)
54 return true; /* by definition */
57 static void pxb_bus_class_init(ObjectClass *class, void *data)
59 PCIBusClass *pbc = PCI_BUS_CLASS(class);
61 pbc->bus_num = pxb_bus_num;
62 pbc->is_root = pxb_is_root;
65 static const TypeInfo pxb_bus_info = {
66 .name = TYPE_PXB_BUS,
67 .parent = TYPE_PCI_BUS,
68 .instance_size = sizeof(PXBBus),
69 .class_init = pxb_bus_class_init,
72 static const char *pxb_host_root_bus_path(PCIHostState *host_bridge,
73 PCIBus *rootbus)
75 PXBBus *bus = PXB_BUS(rootbus);
77 snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus));
78 return bus->bus_path;
81 static void pxb_host_class_init(ObjectClass *class, void *data)
83 DeviceClass *dc = DEVICE_CLASS(class);
84 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class);
86 dc->fw_name = "pci";
87 hc->root_bus_path = pxb_host_root_bus_path;
90 static const TypeInfo pxb_host_info = {
91 .name = TYPE_PXB_HOST,
92 .parent = TYPE_PCI_HOST_BRIDGE,
93 .class_init = pxb_host_class_init,
97 * Registers the PXB bus as a child of the i440fx root bus.
99 * Returns 0 on successs, -1 if i440fx host was not
100 * found or the bus number is already in use.
102 static int pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus)
104 PCIBus *bus = dev->bus;
105 int pxb_bus_num = pci_bus_num(pxb_bus);
107 if (bus->parent_dev) {
108 error_report("PXB devices can be attached only to root bus.");
109 return -1;
112 QLIST_FOREACH(bus, &bus->child, sibling) {
113 if (pci_bus_num(bus) == pxb_bus_num) {
114 error_report("Bus %d is already in use.", pxb_bus_num);
115 return -1;
118 QLIST_INSERT_HEAD(&dev->bus->child, pxb_bus, sibling);
120 return 0;
123 static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin)
125 PCIDevice *pxb = pci_dev->bus->parent_dev;
128 * The bios does not index the pxb slot number when
129 * it computes the IRQ because it resides on bus 0
130 * and not on the current bus.
131 * However QEMU routes the irq through bus 0 and adds
132 * the pxb slot to the IRQ computation of the PXB
133 * device.
135 * Synchronize between bios and QEMU by canceling
136 * pxb's effect.
138 return pin - PCI_SLOT(pxb->devfn);
141 static int pxb_dev_initfn(PCIDevice *dev)
143 PXBDev *pxb = PXB_DEV(dev);
144 DeviceState *ds, *bds;
145 PCIBus *bus;
146 const char *dev_name = NULL;
148 if (dev->qdev.id && *dev->qdev.id) {
149 dev_name = dev->qdev.id;
152 ds = qdev_create(NULL, TYPE_PXB_HOST);
153 bus = pci_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS);
155 bus->parent_dev = dev;
156 bus->address_space_mem = dev->bus->address_space_mem;
157 bus->address_space_io = dev->bus->address_space_io;
158 bus->map_irq = pxb_map_irq_fn;
160 bds = qdev_create(BUS(bus), "pci-bridge");
161 bds->id = dev_name;
162 qdev_prop_set_uint8(bds, "chassis_nr", pxb->bus_nr);
164 PCI_HOST_BRIDGE(ds)->bus = bus;
166 if (pxb_register_bus(dev, bus)) {
167 return -EINVAL;
170 qdev_init_nofail(ds);
171 qdev_init_nofail(bds);
173 pci_word_test_and_set_mask(dev->config + PCI_STATUS,
174 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
175 pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST);
177 return 0;
180 static Property pxb_dev_properties[] = {
181 /* Note: 0 is not a legal a PXB bus number. */
182 DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
183 DEFINE_PROP_END_OF_LIST(),
186 static void pxb_dev_class_init(ObjectClass *klass, void *data)
188 DeviceClass *dc = DEVICE_CLASS(klass);
189 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
191 k->init = pxb_dev_initfn;
192 k->vendor_id = PCI_VENDOR_ID_REDHAT;
193 k->device_id = PCI_DEVICE_ID_REDHAT_PXB;
194 k->class_id = PCI_CLASS_BRIDGE_HOST;
196 dc->desc = "PCI Expander Bridge";
197 dc->props = pxb_dev_properties;
200 static const TypeInfo pxb_dev_info = {
201 .name = TYPE_PXB_DEVICE,
202 .parent = TYPE_PCI_DEVICE,
203 .instance_size = sizeof(PXBDev),
204 .class_init = pxb_dev_class_init,
207 static void pxb_register_types(void)
209 type_register_static(&pxb_bus_info);
210 type_register_static(&pxb_host_info);
211 type_register_static(&pxb_dev_info);
214 type_init(pxb_register_types)