blob: 613857b6016d440a1b06c2d10ef9ee465b1a6caf [file] [log] [blame]
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +03001/*
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
Peter Maydell97d54082016-01-26 18:17:15 +000013#include "qemu/osdep.h"
Wei Jiangang86395eb2016-05-17 18:18:46 +080014#include "qapi/error.h"
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030015#include "hw/pci/pci.h"
16#include "hw/pci/pci_bus.h"
17#include "hw/pci/pci_host.h"
Jonathan Cameron154070e2023-02-27 15:31:28 +000018#include "hw/pci/pcie_port.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020019#include "hw/qdev-properties.h"
Laszlo Ersek3cf0ecb2015-06-19 04:40:10 +020020#include "hw/pci/pci_bridge.h"
Jonathan Cameron7bd19002022-06-08 15:54:37 +010021#include "hw/pci-bridge/pci_expander_bridge.h"
Ben Widawsky4f8db872022-04-29 15:40:39 +010022#include "hw/cxl/cxl.h"
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030023#include "qemu/range.h"
24#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020025#include "qemu/module.h"
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +030026#include "sysemu/numa.h"
Tao Xuaa570202019-08-09 14:57:22 +080027#include "hw/boards.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040028#include "qom/object.h"
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030029
Ben Widawsky9dccb122022-04-29 15:40:37 +010030enum BusType { PCI, PCIE, CXL };
Ben Widawsky25a2e522022-04-29 15:40:36 +010031
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030032#define TYPE_PXB_BUS "pxb-bus"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040033typedef struct PXBBus PXBBus;
Eduardo Habkost8110fa12020-08-31 17:07:33 -040034DECLARE_INSTANCE_CHECKER(PXBBus, PXB_BUS,
35 TYPE_PXB_BUS)
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030036
Marcel Apfelbaum02b07432015-11-26 18:00:27 +020037#define TYPE_PXB_PCIE_BUS "pxb-pcie-bus"
Eduardo Habkost8110fa12020-08-31 17:07:33 -040038DECLARE_INSTANCE_CHECKER(PXBBus, PXB_PCIE_BUS,
39 TYPE_PXB_PCIE_BUS)
Marcel Apfelbaum02b07432015-11-26 18:00:27 +020040
Ben Widawsky9dccb122022-04-29 15:40:37 +010041#define TYPE_PXB_CXL_BUS "pxb-cxl-bus"
42DECLARE_INSTANCE_CHECKER(PXBBus, PXB_CXL_BUS,
43 TYPE_PXB_CXL_BUS)
44
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040045struct PXBBus {
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030046 /*< private >*/
47 PCIBus parent_obj;
48 /*< public >*/
49
50 char bus_path[8];
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040051};
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030052
Jonathan Cameronc28db9e2023-04-20 15:27:50 +010053#define TYPE_PXB_PCIE_DEV "pxb-pcie"
54OBJECT_DECLARE_SIMPLE_TYPE(PXBPCIEDev, PXB_PCIE_DEV)
Marcel Apfelbaum02b07432015-11-26 18:00:27 +020055
Laszlo Ersek48ea3de2015-06-19 04:40:17 +020056static GList *pxb_dev_list;
57
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030058#define TYPE_PXB_HOST "pxb-host"
59
Jonathan Cameron0b4aec22022-04-29 15:40:56 +010060CXLComponentState *cxl_get_hb_cstate(PCIHostState *hb)
61{
62 CXLHost *host = PXB_CXL_HOST(hb);
63
64 return &host->cxl_cstate;
65}
66
Jonathan Cameron154070e2023-02-27 15:31:28 +000067bool cxl_get_hb_passthrough(PCIHostState *hb)
68{
69 CXLHost *host = PXB_CXL_HOST(hb);
70
71 return host->passthrough;
72}
73
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030074static int pxb_bus_num(PCIBus *bus)
75{
Jonathan Cameronc28db9e2023-04-20 15:27:50 +010076 PXBDev *pxb = PXB_DEV(bus->parent_dev);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030077
78 return pxb->bus_nr;
79}
80
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +030081static uint16_t pxb_bus_numa_node(PCIBus *bus)
82{
Jonathan Cameronc28db9e2023-04-20 15:27:50 +010083 PXBDev *pxb = PXB_DEV(bus->parent_dev);
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +030084
85 return pxb->numa_node;
86}
87
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030088static void pxb_bus_class_init(ObjectClass *class, void *data)
89{
90 PCIBusClass *pbc = PCI_BUS_CLASS(class);
91
92 pbc->bus_num = pxb_bus_num;
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +030093 pbc->numa_node = pxb_bus_numa_node;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +030094}
95
96static const TypeInfo pxb_bus_info = {
97 .name = TYPE_PXB_BUS,
98 .parent = TYPE_PCI_BUS,
99 .instance_size = sizeof(PXBBus),
100 .class_init = pxb_bus_class_init,
101};
102
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200103static const TypeInfo pxb_pcie_bus_info = {
104 .name = TYPE_PXB_PCIE_BUS,
105 .parent = TYPE_PCIE_BUS,
106 .instance_size = sizeof(PXBBus),
107 .class_init = pxb_bus_class_init,
108};
109
Ben Widawsky4f8db872022-04-29 15:40:39 +0100110static const TypeInfo pxb_cxl_bus_info = {
111 .name = TYPE_PXB_CXL_BUS,
112 .parent = TYPE_CXL_BUS,
113 .instance_size = sizeof(PXBBus),
114 .class_init = pxb_bus_class_init,
115};
116
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300117static const char *pxb_host_root_bus_path(PCIHostState *host_bridge,
118 PCIBus *rootbus)
119{
Ben Widawsky4f8db872022-04-29 15:40:39 +0100120 PXBBus *bus = pci_bus_is_cxl(rootbus) ?
121 PXB_CXL_BUS(rootbus) :
122 pci_bus_is_express(rootbus) ? PXB_PCIE_BUS(rootbus) :
123 PXB_BUS(rootbus);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300124
125 snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus));
126 return bus->bus_path;
127}
128
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200129static char *pxb_host_ofw_unit_address(const SysBusDevice *dev)
130{
131 const PCIHostState *pxb_host;
132 const PCIBus *pxb_bus;
133 const PXBDev *pxb_dev;
134 int position;
135 const DeviceState *pxb_dev_base;
136 const PCIHostState *main_host;
137 const SysBusDevice *main_host_sbd;
138
139 pxb_host = PCI_HOST_BRIDGE(dev);
140 pxb_bus = pxb_host->bus;
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100141 pxb_dev = PXB_DEV(pxb_bus->parent_dev);
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200142 position = g_list_index(pxb_dev_list, pxb_dev);
143 assert(position >= 0);
144
145 pxb_dev_base = DEVICE(pxb_dev);
146 main_host = PCI_HOST_BRIDGE(pxb_dev_base->parent_bus->parent);
147 main_host_sbd = SYS_BUS_DEVICE(main_host);
148
149 if (main_host_sbd->num_mmio > 0) {
Philippe Mathieu-Daudé883f2c52023-01-10 22:29:47 +0100150 return g_strdup_printf(HWADDR_FMT_plx ",%x",
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200151 main_host_sbd->mmio[0].addr, position + 1);
152 }
153 if (main_host_sbd->num_pio > 0) {
154 return g_strdup_printf("i%04x,%x",
155 main_host_sbd->pio[0], position + 1);
156 }
157 return NULL;
158}
159
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300160static void pxb_host_class_init(ObjectClass *class, void *data)
161{
162 DeviceClass *dc = DEVICE_CLASS(class);
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200163 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(class);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300164 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class);
165
166 dc->fw_name = "pci";
Marcel Apfelbaumbf8d4922016-06-27 18:38:33 +0300167 /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300168 dc->user_creatable = false;
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200169 sbc->explicit_ofw_unit_address = pxb_host_ofw_unit_address;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300170 hc->root_bus_path = pxb_host_root_bus_path;
171}
172
173static const TypeInfo pxb_host_info = {
174 .name = TYPE_PXB_HOST,
175 .parent = TYPE_PCI_HOST_BRIDGE,
176 .class_init = pxb_host_class_init,
177};
178
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100179static void pxb_cxl_realize(DeviceState *dev, Error **errp)
180{
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100181 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
182 CXLHost *cxl = PXB_CXL_HOST(dev);
183 CXLComponentState *cxl_cstate = &cxl->cxl_cstate;
184 struct MemoryRegion *mr = &cxl_cstate->crb.component_registers;
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100185
186 cxl_component_register_block_init(OBJECT(dev), cxl_cstate,
187 TYPE_PXB_CXL_HOST);
188 sysbus_init_mmio(sbd, mr);
Jonathan Cameron7bd19002022-06-08 15:54:37 +0100189}
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100190
Jonathan Cameron7bd19002022-06-08 15:54:37 +0100191/*
192 * Host bridge realization has no means of knowning state associated
193 * with a particular machine. As such, it is nececssary to delay
194 * final setup of the host bridge register space until later in the
195 * machine bring up.
196 */
197void pxb_cxl_hook_up_registers(CXLState *cxl_state, PCIBus *bus, Error **errp)
198{
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100199 PXBCXLDev *pxb = PXB_CXL_DEV(pci_bridge_get_device(bus));
200 CXLHost *cxl = pxb->cxl_host_bridge;
Jonathan Cameron7bd19002022-06-08 15:54:37 +0100201 CXLComponentState *cxl_cstate = &cxl->cxl_cstate;
202 struct MemoryRegion *mr = &cxl_cstate->crb.component_registers;
203 hwaddr offset;
204
205 offset = memory_region_size(mr) * cxl_state->next_mr_idx;
206 if (offset > memory_region_size(&cxl_state->host_mr)) {
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100207 error_setg(errp, "Insufficient space for pxb cxl host register space");
208 return;
209 }
210
Jonathan Cameron7bd19002022-06-08 15:54:37 +0100211 memory_region_add_subregion(&cxl_state->host_mr, offset, mr);
212 cxl_state->next_mr_idx++;
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100213}
214
215static void pxb_cxl_host_class_init(ObjectClass *class, void *data)
216{
217 DeviceClass *dc = DEVICE_CLASS(class);
218 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class);
219
220 hc->root_bus_path = pxb_host_root_bus_path;
221 dc->fw_name = "cxl";
222 dc->realize = pxb_cxl_realize;
223 /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */
224 dc->user_creatable = false;
225}
226
227/*
228 * This is a device to handle the MMIO for a CXL host bridge. It does nothing
229 * else.
230 */
231static const TypeInfo cxl_host_info = {
232 .name = TYPE_PXB_CXL_HOST,
233 .parent = TYPE_PCI_HOST_BRIDGE,
234 .instance_size = sizeof(CXLHost),
235 .class_init = pxb_cxl_host_class_init,
236};
237
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300238/*
Wei Jiangang86395eb2016-05-17 18:18:46 +0800239 * Registers the PXB bus as a child of pci host root bus.
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300240 */
Wei Jiangang86395eb2016-05-17 18:18:46 +0800241static void pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus, Error **errp)
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300242{
David Gibsonfd56e062017-11-29 19:46:27 +1100243 PCIBus *bus = pci_get_bus(dev);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300244 int pxb_bus_num = pci_bus_num(pxb_bus);
245
246 if (bus->parent_dev) {
Wei Jiangang86395eb2016-05-17 18:18:46 +0800247 error_setg(errp, "PXB devices can be attached only to root bus");
248 return;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300249 }
250
251 QLIST_FOREACH(bus, &bus->child, sibling) {
252 if (pci_bus_num(bus) == pxb_bus_num) {
Wei Jiangang86395eb2016-05-17 18:18:46 +0800253 error_setg(errp, "Bus %d is already in use", pxb_bus_num);
254 return;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300255 }
256 }
David Gibsonfd56e062017-11-29 19:46:27 +1100257 QLIST_INSERT_HEAD(&pci_get_bus(dev)->child, pxb_bus, sibling);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300258}
259
Marcel Apfelbaum0639b002015-06-02 14:23:08 +0300260static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin)
261{
David Gibsonfd56e062017-11-29 19:46:27 +1100262 PCIDevice *pxb = pci_get_bus(pci_dev)->parent_dev;
Marcel Apfelbaum0639b002015-06-02 14:23:08 +0300263
264 /*
Jonathan Camerone6093012022-01-18 17:48:55 +0000265 * First carry out normal swizzle to handle
266 * multple root ports on a pxb instance.
267 */
268 pin = pci_swizzle_map_irq_fn(pci_dev, pin);
269
270 /*
Marcel Apfelbaum0639b002015-06-02 14:23:08 +0300271 * The bios does not index the pxb slot number when
272 * it computes the IRQ because it resides on bus 0
273 * and not on the current bus.
274 * However QEMU routes the irq through bus 0 and adds
275 * the pxb slot to the IRQ computation of the PXB
276 * device.
277 *
278 * Synchronize between bios and QEMU by canceling
279 * pxb's effect.
280 */
281 return pin - PCI_SLOT(pxb->devfn);
282}
283
Jonathan Cameron154070e2023-02-27 15:31:28 +0000284static void pxb_cxl_dev_reset(DeviceState *dev)
Ben Widawsky4f8db872022-04-29 15:40:39 +0100285{
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100286 CXLHost *cxl = PXB_CXL_DEV(dev)->cxl_host_bridge;
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100287 CXLComponentState *cxl_cstate = &cxl->cxl_cstate;
Jonathan Cameron154070e2023-02-27 15:31:28 +0000288 PCIHostState *hb = PCI_HOST_BRIDGE(cxl);
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100289 uint32_t *reg_state = cxl_cstate->crb.cache_mem_registers;
290 uint32_t *write_msk = cxl_cstate->crb.cache_mem_regs_write_mask;
Jonathan Cameron154070e2023-02-27 15:31:28 +0000291 int dsp_count = 0;
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100292
293 cxl_component_register_init_common(reg_state, write_msk, CXL2_ROOT_PORT);
Jonathan Cameron154070e2023-02-27 15:31:28 +0000294 /*
295 * The CXL specification allows for host bridges with no HDM decoders
296 * if they only have a single root port.
297 */
Jonathan Cameron9136f662023-04-20 15:27:49 +0100298 if (!PXB_CXL_DEV(dev)->hdm_for_passthrough) {
Jonathan Cameron154070e2023-02-27 15:31:28 +0000299 dsp_count = pcie_count_ds_ports(hb->bus);
300 }
301 /* Initial reset will have 0 dsp so wait until > 0 */
302 if (dsp_count == 1) {
303 cxl->passthrough = true;
304 /* Set Capability ID in header to NONE */
305 ARRAY_FIELD_DP32(reg_state, CXL_HDM_CAPABILITY_HEADER, ID, 0);
306 } else {
307 ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, TARGET_COUNT,
308 8);
309 }
Ben Widawsky4f8db872022-04-29 15:40:39 +0100310}
311
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200312static gint pxb_compare(gconstpointer a, gconstpointer b)
313{
314 const PXBDev *pxb_a = a, *pxb_b = b;
315
316 return pxb_a->bus_nr < pxb_b->bus_nr ? -1 :
317 pxb_a->bus_nr > pxb_b->bus_nr ? 1 :
318 0;
319}
320
Ben Widawsky25a2e522022-04-29 15:40:36 +0100321static void pxb_dev_realize_common(PCIDevice *dev, enum BusType type,
322 Error **errp)
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300323{
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100324 PXBDev *pxb = PXB_DEV(dev);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200325 DeviceState *ds, *bds = NULL;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300326 PCIBus *bus;
327 const char *dev_name = NULL;
Wei Jiangang86395eb2016-05-17 18:18:46 +0800328 Error *local_err = NULL;
Tao Xuaa570202019-08-09 14:57:22 +0800329 MachineState *ms = MACHINE(qdev_get_machine());
330
331 if (ms->numa_state == NULL) {
332 error_setg(errp, "NUMA is not supported by this machine-type");
333 return;
334 }
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300335
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +0300336 if (pxb->numa_node != NUMA_NODE_UNASSIGNED &&
Tao Xuaa570202019-08-09 14:57:22 +0800337 pxb->numa_node >= ms->numa_state->num_nodes) {
Wei Jiangang86395eb2016-05-17 18:18:46 +0800338 error_setg(errp, "Illegal numa node %d", pxb->numa_node);
339 return;
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +0300340 }
341
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300342 if (dev->qdev.id && *dev->qdev.id) {
343 dev_name = dev->qdev.id;
344 }
345
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100346 ds = qdev_new(type == CXL ? TYPE_PXB_CXL_HOST : TYPE_PXB_HOST);
Ben Widawsky25a2e522022-04-29 15:40:36 +0100347 if (type == PCIE) {
David Gibson1115ff62017-11-29 19:46:22 +1100348 bus = pci_root_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_PCIE_BUS);
Ben Widawsky9dccb122022-04-29 15:40:37 +0100349 } else if (type == CXL) {
350 bus = pci_root_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_CXL_BUS);
351 bus->flags |= PCI_BUS_CXL;
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100352 PXB_CXL_DEV(dev)->cxl_host_bridge = PXB_CXL_HOST(ds);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200353 } else {
David Gibson1115ff62017-11-29 19:46:22 +1100354 bus = pci_root_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS);
Markus Armbrusterdf707962020-06-10 07:31:59 +0200355 bds = qdev_new("pci-bridge");
Kevin Wolf163f3842021-10-08 15:34:35 +0200356 bds->id = g_strdup(dev_name);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200357 qdev_prop_set_uint8(bds, PCI_BRIDGE_DEV_PROP_CHASSIS_NR, pxb->bus_nr);
358 qdev_prop_set_bit(bds, PCI_BRIDGE_DEV_PROP_SHPC, false);
359 }
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300360
361 bus->parent_dev = dev;
David Gibsonfd56e062017-11-29 19:46:27 +1100362 bus->address_space_mem = pci_get_bus(dev)->address_space_mem;
363 bus->address_space_io = pci_get_bus(dev)->address_space_io;
Marcel Apfelbaum0639b002015-06-02 14:23:08 +0300364 bus->map_irq = pxb_map_irq_fn;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300365
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300366 PCI_HOST_BRIDGE(ds)->bus = bus;
Xingang Wang91528f42021-07-08 12:55:12 +0000367 PCI_HOST_BRIDGE(ds)->bypass_iommu = pxb->bypass_iommu;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300368
Wei Jiangang86395eb2016-05-17 18:18:46 +0800369 pxb_register_bus(dev, bus, &local_err);
370 if (local_err) {
371 error_propagate(errp, local_err);
Wei Jiangang2e4278b2016-03-23 15:26:19 +0800372 goto err_register_bus;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300373 }
374
Markus Armbruster3c6ef472020-06-10 07:32:34 +0200375 sysbus_realize_and_unref(SYS_BUS_DEVICE(ds), &error_fatal);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200376 if (bds) {
Markus Armbrusterdf707962020-06-10 07:31:59 +0200377 qdev_realize_and_unref(bds, &bus->qbus, &error_fatal);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200378 }
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300379
380 pci_word_test_and_set_mask(dev->config + PCI_STATUS,
381 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
382 pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST);
383
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200384 pxb_dev_list = g_list_insert_sorted(pxb_dev_list, pxb, pxb_compare);
Wei Jiangang86395eb2016-05-17 18:18:46 +0800385 return;
Wei Jiangang2e4278b2016-03-23 15:26:19 +0800386
387err_register_bus:
388 object_unref(OBJECT(bds));
389 object_unparent(OBJECT(bus));
390 object_unref(OBJECT(ds));
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300391}
392
Wei Jiangang86395eb2016-05-17 18:18:46 +0800393static void pxb_dev_realize(PCIDevice *dev, Error **errp)
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200394{
David Gibsonfd56e062017-11-29 19:46:27 +1100395 if (pci_bus_is_express(pci_get_bus(dev))) {
Wei Jiangang86395eb2016-05-17 18:18:46 +0800396 error_setg(errp, "pxb devices cannot reside on a PCIe bus");
397 return;
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200398 }
399
Ben Widawsky25a2e522022-04-29 15:40:36 +0100400 pxb_dev_realize_common(dev, PCI, errp);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200401}
402
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200403static void pxb_dev_exitfn(PCIDevice *pci_dev)
404{
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100405 PXBDev *pxb = PXB_DEV(pci_dev);
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200406
407 pxb_dev_list = g_list_remove(pxb_dev_list, pxb);
408}
409
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300410static Property pxb_dev_properties[] = {
Cao jinf9735fd2016-03-01 17:45:24 +0800411 /* Note: 0 is not a legal PXB bus number. */
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300412 DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0),
Marcel Apfelbaum0e79e512015-06-02 14:23:10 +0300413 DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED),
Xingang Wang91528f42021-07-08 12:55:12 +0000414 DEFINE_PROP_BOOL("bypass_iommu", PXBDev, bypass_iommu, false),
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300415 DEFINE_PROP_END_OF_LIST(),
416};
417
418static void pxb_dev_class_init(ObjectClass *klass, void *data)
419{
420 DeviceClass *dc = DEVICE_CLASS(klass);
421 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
422
Wei Jiangang86395eb2016-05-17 18:18:46 +0800423 k->realize = pxb_dev_realize;
Laszlo Ersek48ea3de2015-06-19 04:40:17 +0200424 k->exit = pxb_dev_exitfn;
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300425 k->vendor_id = PCI_VENDOR_ID_REDHAT;
426 k->device_id = PCI_DEVICE_ID_REDHAT_PXB;
427 k->class_id = PCI_CLASS_BRIDGE_HOST;
428
429 dc->desc = "PCI Expander Bridge";
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400430 device_class_set_props(dc, pxb_dev_properties);
Marcel Apfelbaum7b346c72016-07-17 19:53:10 +0300431 dc->hotpluggable = false;
Marcel Apfelbaum13d11b02016-02-03 13:56:10 +0200432 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300433}
434
435static const TypeInfo pxb_dev_info = {
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100436 .name = TYPE_PXB_DEV,
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300437 .parent = TYPE_PCI_DEVICE,
438 .instance_size = sizeof(PXBDev),
439 .class_init = pxb_dev_class_init,
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -0300440 .interfaces = (InterfaceInfo[]) {
441 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
442 { },
443 },
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300444};
445
Wei Jiangang86395eb2016-05-17 18:18:46 +0800446static void pxb_pcie_dev_realize(PCIDevice *dev, Error **errp)
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200447{
David Gibsonfd56e062017-11-29 19:46:27 +1100448 if (!pci_bus_is_express(pci_get_bus(dev))) {
Wei Jiangang86395eb2016-05-17 18:18:46 +0800449 error_setg(errp, "pxb-pcie devices cannot reside on a PCI bus");
450 return;
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200451 }
452
Ben Widawsky25a2e522022-04-29 15:40:36 +0100453 pxb_dev_realize_common(dev, PCIE, errp);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200454}
455
456static void pxb_pcie_dev_class_init(ObjectClass *klass, void *data)
457{
458 DeviceClass *dc = DEVICE_CLASS(klass);
459 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
460
Wei Jiangang86395eb2016-05-17 18:18:46 +0800461 k->realize = pxb_pcie_dev_realize;
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200462 k->exit = pxb_dev_exitfn;
463 k->vendor_id = PCI_VENDOR_ID_REDHAT;
464 k->device_id = PCI_DEVICE_ID_REDHAT_PXB_PCIE;
465 k->class_id = PCI_CLASS_BRIDGE_HOST;
466
467 dc->desc = "PCI Express Expander Bridge";
Marcel Apfelbaum7b346c72016-07-17 19:53:10 +0300468 dc->hotpluggable = false;
Marcel Apfelbaum13d11b02016-02-03 13:56:10 +0200469 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200470}
471
472static const TypeInfo pxb_pcie_dev_info = {
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100473 .name = TYPE_PXB_PCIE_DEV,
474 .parent = TYPE_PXB_DEV,
475 .instance_size = sizeof(PXBPCIEDev),
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200476 .class_init = pxb_pcie_dev_class_init,
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -0300477 .interfaces = (InterfaceInfo[]) {
478 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
479 { },
480 },
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200481};
482
Ben Widawsky4f8db872022-04-29 15:40:39 +0100483static void pxb_cxl_dev_realize(PCIDevice *dev, Error **errp)
484{
Ben Widawsky4f8db872022-04-29 15:40:39 +0100485 /* A CXL PXB's parent bus is still PCIe */
486 if (!pci_bus_is_express(pci_get_bus(dev))) {
487 error_setg(errp, "pxb-cxl devices cannot reside on a PCI bus");
488 return;
489 }
Ben Widawsky4f8db872022-04-29 15:40:39 +0100490
491 pxb_dev_realize_common(dev, CXL, errp);
Jonathan Cameron154070e2023-02-27 15:31:28 +0000492 pxb_cxl_dev_reset(DEVICE(dev));
Ben Widawsky4f8db872022-04-29 15:40:39 +0100493}
494
Jonathan Cameron154070e2023-02-27 15:31:28 +0000495static Property pxb_cxl_dev_properties[] = {
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100496 DEFINE_PROP_BOOL("hdm_for_passthrough", PXBCXLDev, hdm_for_passthrough, false),
Jonathan Cameron154070e2023-02-27 15:31:28 +0000497 DEFINE_PROP_END_OF_LIST(),
498};
499
Ben Widawsky4f8db872022-04-29 15:40:39 +0100500static void pxb_cxl_dev_class_init(ObjectClass *klass, void *data)
501{
502 DeviceClass *dc = DEVICE_CLASS(klass);
503 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
504
505 k->realize = pxb_cxl_dev_realize;
506 k->exit = pxb_dev_exitfn;
507 /*
508 * XXX: These types of bridges don't actually show up in the hierarchy so
509 * vendor, device, class, etc. ids are intentionally left out.
510 */
511
512 dc->desc = "CXL Host Bridge";
Jonathan Cameron154070e2023-02-27 15:31:28 +0000513 device_class_set_props(dc, pxb_cxl_dev_properties);
Ben Widawsky4f8db872022-04-29 15:40:39 +0100514 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
515
516 /* Host bridges aren't hotpluggable. FIXME: spec reference */
517 dc->hotpluggable = false;
Jonathan Cameron154070e2023-02-27 15:31:28 +0000518 dc->reset = pxb_cxl_dev_reset;
Ben Widawsky4f8db872022-04-29 15:40:39 +0100519}
520
521static const TypeInfo pxb_cxl_dev_info = {
Jonathan Cameronc28db9e2023-04-20 15:27:50 +0100522 .name = TYPE_PXB_CXL_DEV,
523 .parent = TYPE_PXB_PCIE_DEV,
524 .instance_size = sizeof(PXBCXLDev),
Ben Widawsky4f8db872022-04-29 15:40:39 +0100525 .class_init = pxb_cxl_dev_class_init,
526 .interfaces =
527 (InterfaceInfo[]){
528 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
529 {},
530 },
531};
532
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300533static void pxb_register_types(void)
534{
535 type_register_static(&pxb_bus_info);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200536 type_register_static(&pxb_pcie_bus_info);
Ben Widawsky4f8db872022-04-29 15:40:39 +0100537 type_register_static(&pxb_cxl_bus_info);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300538 type_register_static(&pxb_host_info);
Ben Widawsky6e4e3ae2022-04-29 15:40:48 +0100539 type_register_static(&cxl_host_info);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300540 type_register_static(&pxb_dev_info);
Marcel Apfelbaum02b07432015-11-26 18:00:27 +0200541 type_register_static(&pxb_pcie_dev_info);
Ben Widawsky4f8db872022-04-29 15:40:39 +0100542 type_register_static(&pxb_cxl_dev_info);
Marcel Apfelbaum40d14be2015-06-02 14:23:06 +0300543}
544
545type_init(pxb_register_types)