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/pci/pci_bridge.h"
19 #include "qemu/range.h"
20 #include "qemu/error-report.h"
21 #include "sysemu/numa.h"
23 #define TYPE_PXB_BUS "pxb-bus"
24 #define PXB_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_BUS)
26 #define TYPE_PXB_PCIE_BUS "pxb-pcie-bus"
27 #define PXB_PCIE_BUS(obj) OBJECT_CHECK(PXBBus, (obj), TYPE_PXB_PCIE_BUS)
29 typedef struct PXBBus
{
37 #define TYPE_PXB_DEVICE "pxb"
38 #define PXB_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_DEVICE)
40 #define TYPE_PXB_PCIE_DEVICE "pxb-pcie"
41 #define PXB_PCIE_DEV(obj) OBJECT_CHECK(PXBDev, (obj), TYPE_PXB_PCIE_DEVICE)
43 typedef struct PXBDev
{
52 static PXBDev
*convert_to_pxb(PCIDevice
*dev
)
54 return pci_bus_is_express(pci_get_bus(dev
))
55 ? PXB_PCIE_DEV(dev
) : PXB_DEV(dev
);
58 static GList
*pxb_dev_list
;
60 #define TYPE_PXB_HOST "pxb-host"
62 static int pxb_bus_num(PCIBus
*bus
)
64 PXBDev
*pxb
= convert_to_pxb(bus
->parent_dev
);
69 static uint16_t pxb_bus_numa_node(PCIBus
*bus
)
71 PXBDev
*pxb
= convert_to_pxb(bus
->parent_dev
);
73 return pxb
->numa_node
;
76 static void pxb_bus_class_init(ObjectClass
*class, void *data
)
78 PCIBusClass
*pbc
= PCI_BUS_CLASS(class);
80 pbc
->bus_num
= pxb_bus_num
;
81 pbc
->numa_node
= pxb_bus_numa_node
;
84 static const TypeInfo pxb_bus_info
= {
86 .parent
= TYPE_PCI_BUS
,
87 .instance_size
= sizeof(PXBBus
),
88 .class_init
= pxb_bus_class_init
,
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
,
98 static const char *pxb_host_root_bus_path(PCIHostState
*host_bridge
,
101 PXBBus
*bus
= pci_bus_is_express(rootbus
) ?
102 PXB_PCIE_BUS(rootbus
) : PXB_BUS(rootbus
);
104 snprintf(bus
->bus_path
, 8, "0000:%02x", pxb_bus_num(rootbus
));
105 return bus
->bus_path
;
108 static char *pxb_host_ofw_unit_address(const SysBusDevice
*dev
)
110 const PCIHostState
*pxb_host
;
111 const PCIBus
*pxb_bus
;
112 const PXBDev
*pxb_dev
;
114 const DeviceState
*pxb_dev_base
;
115 const PCIHostState
*main_host
;
116 const SysBusDevice
*main_host_sbd
;
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);
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
);
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);
132 if (main_host_sbd
->num_pio
> 0) {
133 return g_strdup_printf("i%04x,%x",
134 main_host_sbd
->pio
[0], position
+ 1);
139 static void pxb_host_class_init(ObjectClass
*class, void *data
)
141 DeviceClass
*dc
= DEVICE_CLASS(class);
142 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(class);
143 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(class);
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
;
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
,
159 * Registers the PXB bus as a child of pci host root bus.
161 static void pxb_register_bus(PCIDevice
*dev
, PCIBus
*pxb_bus
, Error
**errp
)
163 PCIBus
*bus
= pci_get_bus(dev
);
164 int pxb_bus_num
= pci_bus_num(pxb_bus
);
166 if (bus
->parent_dev
) {
167 error_setg(errp
, "PXB devices can be attached only to root bus");
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
);
177 QLIST_INSERT_HEAD(&pci_get_bus(dev
)->child
, pxb_bus
, sibling
);
180 static int pxb_map_irq_fn(PCIDevice
*pci_dev
, int pin
)
182 PCIDevice
*pxb
= pci_get_bus(pci_dev
)->parent_dev
;
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
192 * Synchronize between bios and QEMU by canceling
195 return pin
- PCI_SLOT(pxb
->devfn
);
198 static gint
pxb_compare(gconstpointer a
, gconstpointer b
)
200 const PXBDev
*pxb_a
= a
, *pxb_b
= b
;
202 return pxb_a
->bus_nr
< pxb_b
->bus_nr
? -1 :
203 pxb_a
->bus_nr
> pxb_b
->bus_nr
? 1 :
207 static void pxb_dev_realize_common(PCIDevice
*dev
, bool pcie
, Error
**errp
)
209 PXBDev
*pxb
= convert_to_pxb(dev
);
210 DeviceState
*ds
, *bds
= NULL
;
212 const char *dev_name
= NULL
;
213 Error
*local_err
= NULL
;
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
);
221 if (dev
->qdev
.id
&& *dev
->qdev
.id
) {
222 dev_name
= dev
->qdev
.id
;
225 ds
= qdev_create(NULL
, TYPE_PXB_HOST
);
227 bus
= pci_root_bus_new(ds
, dev_name
, NULL
, NULL
, 0, TYPE_PXB_PCIE_BUS
);
229 bus
= pci_root_bus_new(ds
, "pxb-internal", NULL
, NULL
, 0, TYPE_PXB_BUS
);
230 bds
= qdev_create(BUS(bus
), "pci-bridge");
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);
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
;
241 PCI_HOST_BRIDGE(ds
)->bus
= bus
;
243 pxb_register_bus(dev
, bus
, &local_err
);
245 error_propagate(errp
, local_err
);
246 goto err_register_bus
;
249 qdev_init_nofail(ds
);
251 qdev_init_nofail(bds
);
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
);
258 pxb_dev_list
= g_list_insert_sorted(pxb_dev_list
, pxb
, pxb_compare
);
262 object_unref(OBJECT(bds
));
263 object_unparent(OBJECT(bus
));
264 object_unref(OBJECT(ds
));
267 static void pxb_dev_realize(PCIDevice
*dev
, Error
**errp
)
269 if (pci_bus_is_express(pci_get_bus(dev
))) {
270 error_setg(errp
, "pxb devices cannot reside on a PCIe bus");
274 pxb_dev_realize_common(dev
, false, errp
);
277 static void pxb_dev_exitfn(PCIDevice
*pci_dev
)
279 PXBDev
*pxb
= convert_to_pxb(pci_dev
);
281 pxb_dev_list
= g_list_remove(pxb_dev_list
, pxb
);
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(),
291 static void pxb_dev_class_init(ObjectClass
*klass
, void *data
)
293 DeviceClass
*dc
= DEVICE_CLASS(klass
);
294 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
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
;
302 dc
->desc
= "PCI Expander Bridge";
303 dc
->props
= pxb_dev_properties
;
304 dc
->hotpluggable
= false;
305 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
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
},
319 static void pxb_pcie_dev_realize(PCIDevice
*dev
, Error
**errp
)
321 if (!pci_bus_is_express(pci_get_bus(dev
))) {
322 error_setg(errp
, "pxb-pcie devices cannot reside on a PCI bus");
326 pxb_dev_realize_common(dev
, true, errp
);
329 static void pxb_pcie_dev_class_init(ObjectClass
*klass
, void *data
)
331 DeviceClass
*dc
= DEVICE_CLASS(klass
);
332 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
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
;
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
);
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
},
357 static void pxb_register_types(void)
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
);
366 type_init(pxb_register_types
)