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