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_bus.h"
19 #include "hw/pci/pci_bridge.h"
20 #include "hw/i386/pc.h"
21 #include "qemu/range.h"
22 #include "qemu/error-report.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(dev
->bus
) ? PXB_PCIE_DEV(dev
) : PXB_DEV(dev
);
59 static GList
*pxb_dev_list
;
61 #define TYPE_PXB_HOST "pxb-host"
63 static int pxb_bus_num(PCIBus
*bus
)
65 PXBDev
*pxb
= convert_to_pxb(bus
->parent_dev
);
70 static bool pxb_is_root(PCIBus
*bus
)
72 return true; /* by definition */
75 static uint16_t pxb_bus_numa_node(PCIBus
*bus
)
77 PXBDev
*pxb
= convert_to_pxb(bus
->parent_dev
);
79 return pxb
->numa_node
;
82 static void pxb_bus_class_init(ObjectClass
*class, void *data
)
84 PCIBusClass
*pbc
= PCI_BUS_CLASS(class);
86 pbc
->bus_num
= pxb_bus_num
;
87 pbc
->is_root
= pxb_is_root
;
88 pbc
->numa_node
= pxb_bus_numa_node
;
91 static const TypeInfo pxb_bus_info
= {
93 .parent
= TYPE_PCI_BUS
,
94 .instance_size
= sizeof(PXBBus
),
95 .class_init
= pxb_bus_class_init
,
98 static const TypeInfo pxb_pcie_bus_info
= {
99 .name
= TYPE_PXB_PCIE_BUS
,
100 .parent
= TYPE_PCIE_BUS
,
101 .instance_size
= sizeof(PXBBus
),
102 .class_init
= pxb_bus_class_init
,
105 static const char *pxb_host_root_bus_path(PCIHostState
*host_bridge
,
108 PXBBus
*bus
= pci_bus_is_express(rootbus
) ?
109 PXB_PCIE_BUS(rootbus
) : PXB_BUS(rootbus
);
111 snprintf(bus
->bus_path
, 8, "0000:%02x", pxb_bus_num(rootbus
));
112 return bus
->bus_path
;
115 static char *pxb_host_ofw_unit_address(const SysBusDevice
*dev
)
117 const PCIHostState
*pxb_host
;
118 const PCIBus
*pxb_bus
;
119 const PXBDev
*pxb_dev
;
121 const DeviceState
*pxb_dev_base
;
122 const PCIHostState
*main_host
;
123 const SysBusDevice
*main_host_sbd
;
125 pxb_host
= PCI_HOST_BRIDGE(dev
);
126 pxb_bus
= pxb_host
->bus
;
127 pxb_dev
= convert_to_pxb(pxb_bus
->parent_dev
);
128 position
= g_list_index(pxb_dev_list
, pxb_dev
);
129 assert(position
>= 0);
131 pxb_dev_base
= DEVICE(pxb_dev
);
132 main_host
= PCI_HOST_BRIDGE(pxb_dev_base
->parent_bus
->parent
);
133 main_host_sbd
= SYS_BUS_DEVICE(main_host
);
135 if (main_host_sbd
->num_mmio
> 0) {
136 return g_strdup_printf(TARGET_FMT_plx
",%x",
137 main_host_sbd
->mmio
[0].addr
, position
+ 1);
139 if (main_host_sbd
->num_pio
> 0) {
140 return g_strdup_printf("i%04x,%x",
141 main_host_sbd
->pio
[0], position
+ 1);
146 static void pxb_host_class_init(ObjectClass
*class, void *data
)
148 DeviceClass
*dc
= DEVICE_CLASS(class);
149 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(class);
150 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(class);
153 /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */
154 dc
->cannot_instantiate_with_device_add_yet
= true;
155 sbc
->explicit_ofw_unit_address
= pxb_host_ofw_unit_address
;
156 hc
->root_bus_path
= pxb_host_root_bus_path
;
159 static const TypeInfo pxb_host_info
= {
160 .name
= TYPE_PXB_HOST
,
161 .parent
= TYPE_PCI_HOST_BRIDGE
,
162 .class_init
= pxb_host_class_init
,
166 * Registers the PXB bus as a child of pci host root bus.
168 static void pxb_register_bus(PCIDevice
*dev
, PCIBus
*pxb_bus
, Error
**errp
)
170 PCIBus
*bus
= dev
->bus
;
171 int pxb_bus_num
= pci_bus_num(pxb_bus
);
173 if (bus
->parent_dev
) {
174 error_setg(errp
, "PXB devices can be attached only to root bus");
178 QLIST_FOREACH(bus
, &bus
->child
, sibling
) {
179 if (pci_bus_num(bus
) == pxb_bus_num
) {
180 error_setg(errp
, "Bus %d is already in use", pxb_bus_num
);
184 QLIST_INSERT_HEAD(&dev
->bus
->child
, pxb_bus
, sibling
);
187 static int pxb_map_irq_fn(PCIDevice
*pci_dev
, int pin
)
189 PCIDevice
*pxb
= pci_dev
->bus
->parent_dev
;
192 * The bios does not index the pxb slot number when
193 * it computes the IRQ because it resides on bus 0
194 * and not on the current bus.
195 * However QEMU routes the irq through bus 0 and adds
196 * the pxb slot to the IRQ computation of the PXB
199 * Synchronize between bios and QEMU by canceling
202 return pin
- PCI_SLOT(pxb
->devfn
);
205 static gint
pxb_compare(gconstpointer a
, gconstpointer b
)
207 const PXBDev
*pxb_a
= a
, *pxb_b
= b
;
209 return pxb_a
->bus_nr
< pxb_b
->bus_nr
? -1 :
210 pxb_a
->bus_nr
> pxb_b
->bus_nr
? 1 :
214 static void pxb_dev_realize_common(PCIDevice
*dev
, bool pcie
, Error
**errp
)
216 PXBDev
*pxb
= convert_to_pxb(dev
);
217 DeviceState
*ds
, *bds
= NULL
;
219 const char *dev_name
= NULL
;
220 Error
*local_err
= NULL
;
222 if (pxb
->numa_node
!= NUMA_NODE_UNASSIGNED
&&
223 pxb
->numa_node
>= nb_numa_nodes
) {
224 error_setg(errp
, "Illegal numa node %d", pxb
->numa_node
);
228 if (dev
->qdev
.id
&& *dev
->qdev
.id
) {
229 dev_name
= dev
->qdev
.id
;
232 ds
= qdev_create(NULL
, TYPE_PXB_HOST
);
234 bus
= pci_bus_new(ds
, dev_name
, NULL
, NULL
, 0, TYPE_PXB_PCIE_BUS
);
236 bus
= pci_bus_new(ds
, "pxb-internal", NULL
, NULL
, 0, TYPE_PXB_BUS
);
237 bds
= qdev_create(BUS(bus
), "pci-bridge");
239 qdev_prop_set_uint8(bds
, PCI_BRIDGE_DEV_PROP_CHASSIS_NR
, pxb
->bus_nr
);
240 qdev_prop_set_bit(bds
, PCI_BRIDGE_DEV_PROP_SHPC
, false);
243 bus
->parent_dev
= dev
;
244 bus
->address_space_mem
= dev
->bus
->address_space_mem
;
245 bus
->address_space_io
= dev
->bus
->address_space_io
;
246 bus
->map_irq
= pxb_map_irq_fn
;
248 PCI_HOST_BRIDGE(ds
)->bus
= bus
;
250 pxb_register_bus(dev
, bus
, &local_err
);
252 error_propagate(errp
, local_err
);
253 goto err_register_bus
;
256 qdev_init_nofail(ds
);
258 qdev_init_nofail(bds
);
261 pci_word_test_and_set_mask(dev
->config
+ PCI_STATUS
,
262 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
263 pci_config_set_class(dev
->config
, PCI_CLASS_BRIDGE_HOST
);
265 pxb_dev_list
= g_list_insert_sorted(pxb_dev_list
, pxb
, pxb_compare
);
269 object_unref(OBJECT(bds
));
270 object_unparent(OBJECT(bus
));
271 object_unref(OBJECT(ds
));
274 static void pxb_dev_realize(PCIDevice
*dev
, Error
**errp
)
276 if (pci_bus_is_express(dev
->bus
)) {
277 error_setg(errp
, "pxb devices cannot reside on a PCIe bus");
281 pxb_dev_realize_common(dev
, false, errp
);
284 static void pxb_dev_exitfn(PCIDevice
*pci_dev
)
286 PXBDev
*pxb
= convert_to_pxb(pci_dev
);
288 pxb_dev_list
= g_list_remove(pxb_dev_list
, pxb
);
291 static Property pxb_dev_properties
[] = {
292 /* Note: 0 is not a legal PXB bus number. */
293 DEFINE_PROP_UINT8("bus_nr", PXBDev
, bus_nr
, 0),
294 DEFINE_PROP_UINT16("numa_node", PXBDev
, numa_node
, NUMA_NODE_UNASSIGNED
),
295 DEFINE_PROP_END_OF_LIST(),
298 static void pxb_dev_class_init(ObjectClass
*klass
, void *data
)
300 DeviceClass
*dc
= DEVICE_CLASS(klass
);
301 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
303 k
->realize
= pxb_dev_realize
;
304 k
->exit
= pxb_dev_exitfn
;
305 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
306 k
->device_id
= PCI_DEVICE_ID_REDHAT_PXB
;
307 k
->class_id
= PCI_CLASS_BRIDGE_HOST
;
309 dc
->desc
= "PCI Expander Bridge";
310 dc
->props
= pxb_dev_properties
;
311 dc
->hotpluggable
= false;
312 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
315 static const TypeInfo pxb_dev_info
= {
316 .name
= TYPE_PXB_DEVICE
,
317 .parent
= TYPE_PCI_DEVICE
,
318 .instance_size
= sizeof(PXBDev
),
319 .class_init
= pxb_dev_class_init
,
322 static void pxb_pcie_dev_realize(PCIDevice
*dev
, Error
**errp
)
324 if (!pci_bus_is_express(dev
->bus
)) {
325 error_setg(errp
, "pxb-pcie devices cannot reside on a PCI bus");
329 pxb_dev_realize_common(dev
, true, errp
);
332 static void pxb_pcie_dev_class_init(ObjectClass
*klass
, void *data
)
334 DeviceClass
*dc
= DEVICE_CLASS(klass
);
335 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
337 k
->realize
= pxb_pcie_dev_realize
;
338 k
->exit
= pxb_dev_exitfn
;
339 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
340 k
->device_id
= PCI_DEVICE_ID_REDHAT_PXB_PCIE
;
341 k
->class_id
= PCI_CLASS_BRIDGE_HOST
;
343 dc
->desc
= "PCI Express Expander Bridge";
344 dc
->props
= pxb_dev_properties
;
345 dc
->hotpluggable
= false;
346 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
349 static const TypeInfo pxb_pcie_dev_info
= {
350 .name
= TYPE_PXB_PCIE_DEVICE
,
351 .parent
= TYPE_PCI_DEVICE
,
352 .instance_size
= sizeof(PXBDev
),
353 .class_init
= pxb_pcie_dev_class_init
,
356 static void pxb_register_types(void)
358 type_register_static(&pxb_bus_info
);
359 type_register_static(&pxb_pcie_bus_info
);
360 type_register_static(&pxb_host_info
);
361 type_register_static(&pxb_dev_info
);
362 type_register_static(&pxb_pcie_dev_info
);
365 type_init(pxb_register_types
)