xref: /qemu/hw/pci-bridge/pci_expander_bridge.c (revision 86395eb3)
1 /*
2  * PCI Expander Bridge Device Emulation
3  *
4  * Copyright (C) 2015 Red Hat Inc
5  *
6  * Authors:
7  *   Marcel Apfelbaum <marcel@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "hw/pci/pci.h"
16 #include "hw/pci/pci_bus.h"
17 #include "hw/pci/pci_host.h"
18 #include "hw/pci/pci_bus.h"
19 #include "hw/pci/pci_bridge.h"
20 #include "hw/i386/pc.h"
21 #include "qemu/range.h"
22 #include "qemu/error-report.h"
23 #include "sysemu/numa.h"
24 
25 #define TYPE_PXB_BUS "pxb-bus"
26 #define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
27 
28 #define TYPE_PXB_PCIE_BUS "pxb-pcie-bus"
29 #define PXB_PCIE_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_PCIE_BUS)
30 
31 typedef struct PXBBus {
32     /*< private >*/
33     PCIBus parent_obj;
34     /*< public >*/
35 
36     char bus_path[8];
37 } PXBBus;
38 
39 #define TYPE_PXB_DEVICE "pxb"
40 #define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE)
41 
42 #define TYPE_PXB_PCIE_DEVICE "pxb-pcie"
43 #define PXB_PCIE_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_PCIE_DEVICE)
44 
45 typedef struct PXBDev {
46     /*< private >*/
47     PCIDevice parent_obj;
48     /*< public >*/
49 
50     uint8_t bus_nr;
51     uint16_t numa_node;
52 } PXBDev;
53 
54 static PXBDev *convert_to_pxb(PCIDevice *dev)
55 {
56     return pci_bus_is_express(dev->bus) ? PXB_PCIE_DEV(dev) : PXB_DEV(dev);
57 }
58 
59 static GList *pxb_dev_list;
60 
61 #define TYPE_PXB_HOST "pxb-host"
62 
63 static int pxb_bus_num(PCIBus *bus)
64 {
65     PXBDev *pxb = convert_to_pxb(bus->parent_dev);
66 
67     return pxb->bus_nr;
68 }
69 
70 static bool pxb_is_root(PCIBus *bus)
71 {
72     return true; /* by definition */
73 }
74 
75 static uint16_t pxb_bus_numa_node(PCIBus *bus)
76 {
77     PXBDev *pxb = convert_to_pxb(bus->parent_dev);
78 
79     return pxb->numa_node;
80 }
81 
82 static void pxb_bus_class_init(ObjectClass *class, void *data)
83 {
84     PCIBusClass *pbc = PCI_BUS_CLASS(class);
85 
86     pbc->bus_num = pxb_bus_num;
87     pbc->is_root = pxb_is_root;
88     pbc->numa_node = pxb_bus_numa_node;
89 }
90 
91 static const TypeInfo pxb_bus_info = {
92     .name          = TYPE_PXB_BUS,
93     .parent        = TYPE_PCI_BUS,
94     .instance_size = sizeof(PXBBus),
95     .class_init    = pxb_bus_class_init,
96 };
97 
98 static const TypeInfo pxb_pcie_bus_info = {
99     .name          = TYPE_PXB_PCIE_BUS,
100     .parent        = TYPE_PCIE_BUS,
101     .instance_size = sizeof(PXBBus),
102     .class_init    = pxb_bus_class_init,
103 };
104 
105 static const char *pxb_host_root_bus_path(PCIHostState *host_bridge,
106                                           PCIBus *rootbus)
107 {
108     PXBBus *bus = pci_bus_is_express(rootbus) ?
109                   PXB_PCIE_BUS(rootbus) : PXB_BUS(rootbus);
110 
111     snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus));
112     return bus->bus_path;
113 }
114 
115 static char *pxb_host_ofw_unit_address(const SysBusDevice *dev)
116 {
117     const PCIHostState *pxb_host;
118     const PCIBus *pxb_bus;
119     const PXBDev *pxb_dev;
120     int position;
121     const DeviceState *pxb_dev_base;
122     const PCIHostState *main_host;
123     const SysBusDevice *main_host_sbd;
124 
125     pxb_host = PCI_HOST_BRIDGE(dev);
126     pxb_bus = pxb_host->bus;
127     pxb_dev = convert_to_pxb(pxb_bus->parent_dev);
128     position = g_list_index(pxb_dev_list, pxb_dev);
129     assert(position >= 0);
130 
131     pxb_dev_base = DEVICE(pxb_dev);
132     main_host = PCI_HOST_BRIDGE(pxb_dev_base->parent_bus->parent);
133     main_host_sbd = SYS_BUS_DEVICE(main_host);
134 
135     if (main_host_sbd->num_mmio > 0) {
136         return g_strdup_printf(TARGET_FMT_plx ",%x",
137                                main_host_sbd->mmio[0].addr, position + 1);
138     }
139     if (main_host_sbd->num_pio > 0) {
140         return g_strdup_printf("i%04x,%x",
141                                main_host_sbd->pio[0], position + 1);
142     }
143     return NULL;
144 }
145 
146 static void pxb_host_class_init(ObjectClass *class, void *data)
147 {
148     DeviceClass *dc = DEVICE_CLASS(class);
149     SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(class);
150     PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class);
151 
152     dc->fw_name = "pci";
153     /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */
154     dc->cannot_instantiate_with_device_add_yet = true;
155     sbc->explicit_ofw_unit_address = pxb_host_ofw_unit_address;
156     hc->root_bus_path = pxb_host_root_bus_path;
157 }
158 
159 static const TypeInfo pxb_host_info = {
160     .name          = TYPE_PXB_HOST,
161     .parent        = TYPE_PCI_HOST_BRIDGE,
162     .class_init    = pxb_host_class_init,
163 };
164 
165 /*
166  * Registers the PXB bus as a child of pci host root bus.
167  */
168 static void pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus, Error **errp)
169 {
170     PCIBus *bus = dev->bus;
171     int pxb_bus_num = pci_bus_num(pxb_bus);
172 
173     if (bus->parent_dev) {
174         error_setg(errp, "PXB devices can be attached only to root bus");
175         return;
176     }
177 
178     QLIST_FOREACH(bus, &bus->child, sibling) {
179         if (pci_bus_num(bus) == pxb_bus_num) {
180             error_setg(errp, "Bus %d is already in use", pxb_bus_num);
181             return;
182         }
183     }
184     QLIST_INSERT_HEAD(&dev->bus->child, pxb_bus, sibling);
185 }
186 
187 static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin)
188 {
189     PCIDevice *pxb = pci_dev->bus->parent_dev;
190 
191     /*
192      * The bios does not index the pxb slot number when
193      * it computes the IRQ because it resides on bus 0
194      * and not on the current bus.
195      * However QEMU routes the irq through bus 0 and adds
196      * the pxb slot to the IRQ computation of the PXB
197      * device.
198      *
199      * Synchronize between bios and QEMU by canceling
200      * pxb's effect.
201      */
202     return pin - PCI_SLOT(pxb->devfn);
203 }
204 
205 static gint pxb_compare(gconstpointer a, gconstpointer b)
206 {
207     const PXBDev *pxb_a = a, *pxb_b = b;
208 
209     return pxb_a->bus_nr < pxb_b->bus_nr ? -1 :
210            pxb_a->bus_nr > pxb_b->bus_nr ?  1 :
211            0;
212 }
213 
214 static void pxb_dev_realize_common(PCIDevice *dev, bool pcie, Error **errp)
215 {
216     PXBDev *pxb = convert_to_pxb(dev);
217     DeviceState *ds, *bds = NULL;
218     PCIBus *bus;
219     const char *dev_name = NULL;
220     Error *local_err = NULL;
221 
222     if (pxb->numa_node != NUMA_NODE_UNASSIGNED &&
223         pxb->numa_node >= nb_numa_nodes) {
224         error_setg(errp, "Illegal numa node %d", pxb->numa_node);
225         return;
226     }
227 
228     if (dev->qdev.id && *dev->qdev.id) {
229         dev_name = dev->qdev.id;
230     }
231 
232     ds = qdev_create(NULL, TYPE_PXB_HOST);
233     if (pcie) {
234         bus = pci_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_PCIE_BUS);
235     } else {
236         bus = pci_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS);
237         bds = qdev_create(BUS(bus), "pci-bridge");
238         bds->id = dev_name;
239         qdev_prop_set_uint8(bds, PCI_BRIDGE_DEV_PROP_CHASSIS_NR, pxb->bus_nr);
240         qdev_prop_set_bit(bds, PCI_BRIDGE_DEV_PROP_SHPC, false);
241     }
242 
243     bus->parent_dev = dev;
244     bus->address_space_mem = dev->bus->address_space_mem;
245     bus->address_space_io = dev->bus->address_space_io;
246     bus->map_irq = pxb_map_irq_fn;
247 
248     PCI_HOST_BRIDGE(ds)->bus = bus;
249 
250     pxb_register_bus(dev, bus, &local_err);
251     if (local_err) {
252         error_propagate(errp, local_err);
253         goto err_register_bus;
254     }
255 
256     qdev_init_nofail(ds);
257     if (bds) {
258         qdev_init_nofail(bds);
259     }
260 
261     pci_word_test_and_set_mask(dev->config + PCI_STATUS,
262                                PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
263     pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST);
264 
265     pxb_dev_list = g_list_insert_sorted(pxb_dev_list, pxb, pxb_compare);
266     return;
267 
268 err_register_bus:
269     object_unref(OBJECT(bds));
270     object_unparent(OBJECT(bus));
271     object_unref(OBJECT(ds));
272 }
273 
274 static void pxb_dev_realize(PCIDevice *dev, Error **errp)
275 {
276     if (pci_bus_is_express(dev->bus)) {
277         error_setg(errp, "pxb devices cannot reside on a PCIe bus");
278         return;
279     }
280 
281     pxb_dev_realize_common(dev, false, errp);
282 }
283 
284 static void pxb_dev_exitfn(PCIDevice *pci_dev)
285 {
286     PXBDev *pxb = convert_to_pxb(pci_dev);
287 
288     pxb_dev_list = g_list_remove(pxb_dev_list, pxb);
289 }
290 
291 static Property pxb_dev_properties[] = {
292     /* Note: 0 is not a legal PXB bus number. */
293     DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
294     DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED),
295     DEFINE_PROP_END_OF_LIST(),
296 };
297 
298 static void pxb_dev_class_init(ObjectClass *klass, void *data)
299 {
300     DeviceClass *dc = DEVICE_CLASS(klass);
301     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
302 
303     k->realize = pxb_dev_realize;
304     k->exit = pxb_dev_exitfn;
305     k->vendor_id = PCI_VENDOR_ID_REDHAT;
306     k->device_id = PCI_DEVICE_ID_REDHAT_PXB;
307     k->class_id = PCI_CLASS_BRIDGE_HOST;
308 
309     dc->desc = "PCI Expander Bridge";
310     dc->props = pxb_dev_properties;
311     dc->hotpluggable = false;
312     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
313 }
314 
315 static const TypeInfo pxb_dev_info = {
316     .name          = TYPE_PXB_DEVICE,
317     .parent        = TYPE_PCI_DEVICE,
318     .instance_size = sizeof(PXBDev),
319     .class_init    = pxb_dev_class_init,
320 };
321 
322 static void pxb_pcie_dev_realize(PCIDevice *dev, Error **errp)
323 {
324     if (!pci_bus_is_express(dev->bus)) {
325         error_setg(errp, "pxb-pcie devices cannot reside on a PCI bus");
326         return;
327     }
328 
329     pxb_dev_realize_common(dev, true, errp);
330 }
331 
332 static void pxb_pcie_dev_class_init(ObjectClass *klass, void *data)
333 {
334     DeviceClass *dc = DEVICE_CLASS(klass);
335     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
336 
337     k->realize = pxb_pcie_dev_realize;
338     k->exit = pxb_dev_exitfn;
339     k->vendor_id = PCI_VENDOR_ID_REDHAT;
340     k->device_id = PCI_DEVICE_ID_REDHAT_PXB_PCIE;
341     k->class_id = PCI_CLASS_BRIDGE_HOST;
342 
343     dc->desc = "PCI Express Expander Bridge";
344     dc->props = pxb_dev_properties;
345     dc->hotpluggable = false;
346     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
347 }
348 
349 static const TypeInfo pxb_pcie_dev_info = {
350     .name          = TYPE_PXB_PCIE_DEVICE,
351     .parent        = TYPE_PCI_DEVICE,
352     .instance_size = sizeof(PXBDev),
353     .class_init    = pxb_pcie_dev_class_init,
354 };
355 
356 static void pxb_register_types(void)
357 {
358     type_register_static(&pxb_bus_info);
359     type_register_static(&pxb_pcie_bus_info);
360     type_register_static(&pxb_host_info);
361     type_register_static(&pxb_dev_info);
362     type_register_static(&pxb_pcie_dev_info);
363 }
364 
365 type_init(pxb_register_types)
366