2 * PCI Expander Bridge Device Emulation
4 * Copyright (C) 2015 Red Hat Inc
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 "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"
25 #define TYPE_PXB_BUS "pxb-bus"
26 #define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
28 #define TYPE_PXB_PCIE_BUS "pxb-pcie-bus"
29 #define PXB_PCIE_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_PCIE_BUS)
31 typedef struct PXBBus
{
39 #define TYPE_PXB_DEVICE "pxb"
40 #define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE)
42 #define TYPE_PXB_PCIE_DEVICE "pxb-pcie"
43 #define PXB_PCIE_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_PCIE_DEVICE)
45 typedef struct PXBDev
{
54 static PXBDev
*convert_to_pxb(PCIDevice
*dev
)
56 return pci_bus_is_express(pci_get_bus(dev
))
57 ? PXB_PCIE_DEV(dev
) : PXB_DEV(dev
);
60 static GList
*pxb_dev_list
;
62 #define TYPE_PXB_HOST "pxb-host"
64 static int pxb_bus_num(PCIBus
*bus
)
66 PXBDev
*pxb
= convert_to_pxb(bus
->parent_dev
);
71 static uint16_t pxb_bus_numa_node(PCIBus
*bus
)
73 PXBDev
*pxb
= convert_to_pxb(bus
->parent_dev
);
75 return pxb
->numa_node
;
78 static void pxb_bus_class_init(ObjectClass
*class, void *data
)
80 PCIBusClass
*pbc
= PCI_BUS_CLASS(class);
82 pbc
->bus_num
= pxb_bus_num
;
83 pbc
->numa_node
= pxb_bus_numa_node
;
86 static const TypeInfo pxb_bus_info
= {
88 .parent
= TYPE_PCI_BUS
,
89 .instance_size
= sizeof(PXBBus
),
90 .class_init
= pxb_bus_class_init
,
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
,
100 static const char *pxb_host_root_bus_path(PCIHostState
*host_bridge
,
103 PXBBus
*bus
= pci_bus_is_express(rootbus
) ?
104 PXB_PCIE_BUS(rootbus
) : PXB_BUS(rootbus
);
106 snprintf(bus
->bus_path
, 8, "0000:%02x", pxb_bus_num(rootbus
));
107 return bus
->bus_path
;
110 static char *pxb_host_ofw_unit_address(const SysBusDevice
*dev
)
112 const PCIHostState
*pxb_host
;
113 const PCIBus
*pxb_bus
;
114 const PXBDev
*pxb_dev
;
116 const DeviceState
*pxb_dev_base
;
117 const PCIHostState
*main_host
;
118 const SysBusDevice
*main_host_sbd
;
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);
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
);
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);
134 if (main_host_sbd
->num_pio
> 0) {
135 return g_strdup_printf("i%04x,%x",
136 main_host_sbd
->pio
[0], position
+ 1);
141 static void pxb_host_class_init(ObjectClass
*class, void *data
)
143 DeviceClass
*dc
= DEVICE_CLASS(class);
144 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(class);
145 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(class);
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
;
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
,
161 * Registers the PXB bus as a child of pci host root bus.
163 static void pxb_register_bus(PCIDevice
*dev
, PCIBus
*pxb_bus
, Error
**errp
)
165 PCIBus
*bus
= pci_get_bus(dev
);
166 int pxb_bus_num
= pci_bus_num(pxb_bus
);
168 if (bus
->parent_dev
) {
169 error_setg(errp
, "PXB devices can be attached only to root bus");
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
);
179 QLIST_INSERT_HEAD(&pci_get_bus(dev
)->child
, pxb_bus
, sibling
);
182 static int pxb_map_irq_fn(PCIDevice
*pci_dev
, int pin
)
184 PCIDevice
*pxb
= pci_get_bus(pci_dev
)->parent_dev
;
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
194 * Synchronize between bios and QEMU by canceling
197 return pin
- PCI_SLOT(pxb
->devfn
);
200 static gint
pxb_compare(gconstpointer a
, gconstpointer b
)
202 const PXBDev
*pxb_a
= a
, *pxb_b
= b
;
204 return pxb_a
->bus_nr
< pxb_b
->bus_nr
? -1 :
205 pxb_a
->bus_nr
> pxb_b
->bus_nr
? 1 :
209 static void pxb_dev_realize_common(PCIDevice
*dev
, bool pcie
, Error
**errp
)
211 PXBDev
*pxb
= convert_to_pxb(dev
);
212 DeviceState
*ds
, *bds
= NULL
;
214 const char *dev_name
= NULL
;
215 Error
*local_err
= NULL
;
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
);
223 if (dev
->qdev
.id
&& *dev
->qdev
.id
) {
224 dev_name
= dev
->qdev
.id
;
227 ds
= qdev_create(NULL
, TYPE_PXB_HOST
);
229 bus
= pci_root_bus_new(ds
, dev_name
, NULL
, NULL
, 0, TYPE_PXB_PCIE_BUS
);
231 bus
= pci_root_bus_new(ds
, "pxb-internal", NULL
, NULL
, 0, TYPE_PXB_BUS
);
232 bds
= qdev_create(BUS(bus
), "pci-bridge");
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);
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
;
243 PCI_HOST_BRIDGE(ds
)->bus
= bus
;
245 pxb_register_bus(dev
, bus
, &local_err
);
247 error_propagate(errp
, local_err
);
248 goto err_register_bus
;
251 qdev_init_nofail(ds
);
253 qdev_init_nofail(bds
);
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
);
260 pxb_dev_list
= g_list_insert_sorted(pxb_dev_list
, pxb
, pxb_compare
);
264 object_unref(OBJECT(bds
));
265 object_unparent(OBJECT(bus
));
266 object_unref(OBJECT(ds
));
269 static void pxb_dev_realize(PCIDevice
*dev
, Error
**errp
)
271 if (pci_bus_is_express(pci_get_bus(dev
))) {
272 error_setg(errp
, "pxb devices cannot reside on a PCIe bus");
276 pxb_dev_realize_common(dev
, false, errp
);
279 static void pxb_dev_exitfn(PCIDevice
*pci_dev
)
281 PXBDev
*pxb
= convert_to_pxb(pci_dev
);
283 pxb_dev_list
= g_list_remove(pxb_dev_list
, pxb
);
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(),
293 static void pxb_dev_class_init(ObjectClass
*klass
, void *data
)
295 DeviceClass
*dc
= DEVICE_CLASS(klass
);
296 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
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
;
304 dc
->desc
= "PCI Expander Bridge";
305 dc
->props
= pxb_dev_properties
;
306 dc
->hotpluggable
= false;
307 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
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
},
321 static void pxb_pcie_dev_realize(PCIDevice
*dev
, Error
**errp
)
323 if (!pci_bus_is_express(pci_get_bus(dev
))) {
324 error_setg(errp
, "pxb-pcie devices cannot reside on a PCI bus");
328 pxb_dev_realize_common(dev
, true, errp
);
331 static void pxb_pcie_dev_class_init(ObjectClass
*klass
, void *data
)
333 DeviceClass
*dc
= DEVICE_CLASS(klass
);
334 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
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
;
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
);
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
},
359 static void pxb_register_types(void)
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
);
368 type_init(pxb_register_types
)