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