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