| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * PCI Expander Bridge Device Emulation | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2015 Red Hat Inc | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Authors: | 
					
						
							|  |  |  |  *   Marcel Apfelbaum <marcel@redhat.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL, version 2 or later. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:15 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | #include "hw/pci/pci.h"
 | 
					
						
							|  |  |  | #include "hw/pci/pci_bus.h"
 | 
					
						
							|  |  |  | #include "hw/pci/pci_host.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:51 +02:00
										 |  |  | #include "hw/qdev-properties.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:10 +02:00
										 |  |  | #include "hw/pci/pci_bridge.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | #include "qemu/range.h"
 | 
					
						
							|  |  |  | #include "qemu/error-report.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  | #include "sysemu/numa.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-09 14:57:22 +08:00
										 |  |  | #include "hw/boards.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | #include "qom/object.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define TYPE_PXB_BUS "pxb-bus"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | typedef struct PXBBus PXBBus; | 
					
						
							| 
									
										
										
										
											2020-08-31 17:07:33 -04:00
										 |  |  | DECLARE_INSTANCE_CHECKER(PXBBus, PXB_BUS, | 
					
						
							|  |  |  |                          TYPE_PXB_BUS) | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | #define TYPE_PXB_PCIE_BUS "pxb-pcie-bus"
 | 
					
						
							| 
									
										
										
										
											2020-08-31 17:07:33 -04:00
										 |  |  | DECLARE_INSTANCE_CHECKER(PXBBus, PXB_PCIE_BUS, | 
					
						
							|  |  |  |                          TYPE_PXB_PCIE_BUS) | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | struct PXBBus { | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     /*< private >*/ | 
					
						
							|  |  |  |     PCIBus parent_obj; | 
					
						
							|  |  |  |     /*< public >*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     char bus_path[8]; | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define TYPE_PXB_DEVICE "pxb"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | typedef struct PXBDev PXBDev; | 
					
						
							| 
									
										
										
										
											2020-08-31 17:07:33 -04:00
										 |  |  | DECLARE_INSTANCE_CHECKER(PXBDev, PXB_DEV, | 
					
						
							|  |  |  |                          TYPE_PXB_DEVICE) | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | #define TYPE_PXB_PCIE_DEVICE "pxb-pcie"
 | 
					
						
							| 
									
										
										
										
											2020-08-31 17:07:33 -04:00
										 |  |  | DECLARE_INSTANCE_CHECKER(PXBDev, PXB_PCIE_DEV, | 
					
						
							|  |  |  |                          TYPE_PXB_PCIE_DEVICE) | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | struct PXBDev { | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     /*< private >*/ | 
					
						
							|  |  |  |     PCIDevice parent_obj; | 
					
						
							|  |  |  |     /*< public >*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint8_t bus_nr; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  |     uint16_t numa_node; | 
					
						
							| 
									
										
										
										
											2021-07-08 12:55:12 +00:00
										 |  |  |     bool bypass_iommu; | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | static PXBDev *convert_to_pxb(PCIDevice *dev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     return pci_bus_is_express(pci_get_bus(dev)) | 
					
						
							|  |  |  |         ? PXB_PCIE_DEV(dev) : PXB_DEV(dev); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  | static GList *pxb_dev_list; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | #define TYPE_PXB_HOST "pxb-host"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int pxb_bus_num(PCIBus *bus) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     PXBDev *pxb = convert_to_pxb(bus->parent_dev); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return pxb->bus_nr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  | static uint16_t pxb_bus_numa_node(PCIBus *bus) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     PXBDev *pxb = convert_to_pxb(bus->parent_dev); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return pxb->numa_node; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | static void pxb_bus_class_init(ObjectClass *class, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PCIBusClass *pbc = PCI_BUS_CLASS(class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pbc->bus_num = pxb_bus_num; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  |     pbc->numa_node = pxb_bus_numa_node; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo pxb_bus_info = { | 
					
						
							|  |  |  |     .name          = TYPE_PXB_BUS, | 
					
						
							|  |  |  |     .parent        = TYPE_PCI_BUS, | 
					
						
							|  |  |  |     .instance_size = sizeof(PXBBus), | 
					
						
							|  |  |  |     .class_init    = pxb_bus_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | static const TypeInfo pxb_pcie_bus_info = { | 
					
						
							|  |  |  |     .name          = TYPE_PXB_PCIE_BUS, | 
					
						
							|  |  |  |     .parent        = TYPE_PCIE_BUS, | 
					
						
							|  |  |  |     .instance_size = sizeof(PXBBus), | 
					
						
							|  |  |  |     .class_init    = pxb_bus_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | static const char *pxb_host_root_bus_path(PCIHostState *host_bridge, | 
					
						
							|  |  |  |                                           PCIBus *rootbus) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     PXBBus *bus = pci_bus_is_express(rootbus) ? | 
					
						
							|  |  |  |                   PXB_PCIE_BUS(rootbus) : PXB_BUS(rootbus); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     snprintf(bus->bus_path, 8, "0000:%02x", pxb_bus_num(rootbus)); | 
					
						
							|  |  |  |     return bus->bus_path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  | static char *pxb_host_ofw_unit_address(const SysBusDevice *dev) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const PCIHostState *pxb_host; | 
					
						
							|  |  |  |     const PCIBus *pxb_bus; | 
					
						
							|  |  |  |     const PXBDev *pxb_dev; | 
					
						
							|  |  |  |     int position; | 
					
						
							|  |  |  |     const DeviceState *pxb_dev_base; | 
					
						
							|  |  |  |     const PCIHostState *main_host; | 
					
						
							|  |  |  |     const SysBusDevice *main_host_sbd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pxb_host = PCI_HOST_BRIDGE(dev); | 
					
						
							|  |  |  |     pxb_bus = pxb_host->bus; | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     pxb_dev = convert_to_pxb(pxb_bus->parent_dev); | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  |     position = g_list_index(pxb_dev_list, pxb_dev); | 
					
						
							|  |  |  |     assert(position >= 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pxb_dev_base = DEVICE(pxb_dev); | 
					
						
							|  |  |  |     main_host = PCI_HOST_BRIDGE(pxb_dev_base->parent_bus->parent); | 
					
						
							|  |  |  |     main_host_sbd = SYS_BUS_DEVICE(main_host); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (main_host_sbd->num_mmio > 0) { | 
					
						
							|  |  |  |         return g_strdup_printf(TARGET_FMT_plx ",%x", | 
					
						
							|  |  |  |                                main_host_sbd->mmio[0].addr, position + 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (main_host_sbd->num_pio > 0) { | 
					
						
							|  |  |  |         return g_strdup_printf("i%04x,%x", | 
					
						
							|  |  |  |                                main_host_sbd->pio[0], position + 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | static void pxb_host_class_init(ObjectClass *class, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *dc = DEVICE_CLASS(class); | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  |     SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(class); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dc->fw_name = "pci"; | 
					
						
							| 
									
										
										
										
											2016-06-27 18:38:33 +03:00
										 |  |  |     /* Reason: Internal part of the pxb/pxb-pcie device, not usable by itself */ | 
					
						
							| 
									
										
										
										
											2017-05-03 17:35:44 -03:00
										 |  |  |     dc->user_creatable = false; | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  |     sbc->explicit_ofw_unit_address = pxb_host_ofw_unit_address; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     hc->root_bus_path = pxb_host_root_bus_path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo pxb_host_info = { | 
					
						
							|  |  |  |     .name          = TYPE_PXB_HOST, | 
					
						
							|  |  |  |     .parent        = TYPE_PCI_HOST_BRIDGE, | 
					
						
							|  |  |  |     .class_init    = pxb_host_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |  * Registers the PXB bus as a child of pci host root bus. | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  | static void pxb_register_bus(PCIDevice *dev, PCIBus *pxb_bus, Error **errp) | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     PCIBus *bus = pci_get_bus(dev); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     int pxb_bus_num = pci_bus_num(pxb_bus); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bus->parent_dev) { | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |         error_setg(errp, "PXB devices can be attached only to root bus"); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QLIST_FOREACH(bus, &bus->child, sibling) { | 
					
						
							|  |  |  |         if (pci_bus_num(bus) == pxb_bus_num) { | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |             error_setg(errp, "Bus %d is already in use", pxb_bus_num); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     QLIST_INSERT_HEAD(&pci_get_bus(dev)->child, pxb_bus, sibling); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:08 +03:00
										 |  |  | static int pxb_map_irq_fn(PCIDevice *pci_dev, int pin) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     PCIDevice *pxb = pci_get_bus(pci_dev)->parent_dev; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * The bios does not index the pxb slot number when | 
					
						
							|  |  |  |      * it computes the IRQ because it resides on bus 0 | 
					
						
							|  |  |  |      * and not on the current bus. | 
					
						
							|  |  |  |      * However QEMU routes the irq through bus 0 and adds | 
					
						
							|  |  |  |      * the pxb slot to the IRQ computation of the PXB | 
					
						
							|  |  |  |      * device. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Synchronize between bios and QEMU by canceling | 
					
						
							|  |  |  |      * pxb's effect. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     return pin - PCI_SLOT(pxb->devfn); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  | static gint pxb_compare(gconstpointer a, gconstpointer b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const PXBDev *pxb_a = a, *pxb_b = b; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return pxb_a->bus_nr < pxb_b->bus_nr ? -1 : | 
					
						
							|  |  |  |            pxb_a->bus_nr > pxb_b->bus_nr ?  1 : | 
					
						
							|  |  |  |            0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  | static void pxb_dev_realize_common(PCIDevice *dev, bool pcie, Error **errp) | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     PXBDev *pxb = convert_to_pxb(dev); | 
					
						
							|  |  |  |     DeviceState *ds, *bds = NULL; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     PCIBus *bus; | 
					
						
							|  |  |  |     const char *dev_name = NULL; | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     Error *local_err = NULL; | 
					
						
							| 
									
										
										
										
											2019-08-09 14:57:22 +08:00
										 |  |  |     MachineState *ms = MACHINE(qdev_get_machine()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ms->numa_state == NULL) { | 
					
						
							|  |  |  |         error_setg(errp, "NUMA is not supported by this machine-type"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  |     if (pxb->numa_node != NUMA_NODE_UNASSIGNED && | 
					
						
							| 
									
										
										
										
											2019-08-09 14:57:22 +08:00
										 |  |  |         pxb->numa_node >= ms->numa_state->num_nodes) { | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |         error_setg(errp, "Illegal numa node %d", pxb->numa_node); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     if (dev->qdev.id && *dev->qdev.id) { | 
					
						
							|  |  |  |         dev_name = dev->qdev.id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												qdev: Convert uses of qdev_create() with Coccinelle
This is the transformation explained in the commit before previous.
Takes care of just one pattern that needs conversion.  More to come in
this series.
Coccinelle script:
    @ depends on !(file in "hw/arm/highbank.c")@
    expression bus, type_name, dev, expr;
    @@
    -    dev = qdev_create(bus, type_name);
    +    dev = qdev_new(type_name);
         ... when != dev = expr
    -    qdev_init_nofail(dev);
    +    qdev_realize_and_unref(dev, bus, &error_fatal);
    @@
    expression bus, type_name, dev, expr;
    identifier DOWN;
    @@
    -    dev = DOWN(qdev_create(bus, type_name));
    +    dev = DOWN(qdev_new(type_name));
         ... when != dev = expr
    -    qdev_init_nofail(DEVICE(dev));
    +    qdev_realize_and_unref(DEVICE(dev), bus, &error_fatal);
    @@
    expression bus, type_name, expr;
    identifier dev;
    @@
    -    DeviceState *dev = qdev_create(bus, type_name);
    +    DeviceState *dev = qdev_new(type_name);
         ... when != dev = expr
    -    qdev_init_nofail(dev);
    +    qdev_realize_and_unref(dev, bus, &error_fatal);
    @@
    expression bus, type_name, dev, expr, errp;
    symbol true;
    @@
    -    dev = qdev_create(bus, type_name);
    +    dev = qdev_new(type_name);
         ... when != dev = expr
    -    object_property_set_bool(OBJECT(dev), true, "realized", errp);
    +    qdev_realize_and_unref(dev, bus, errp);
    @@
    expression bus, type_name, expr, errp;
    identifier dev;
    symbol true;
    @@
    -    DeviceState *dev = qdev_create(bus, type_name);
    +    DeviceState *dev = qdev_new(type_name);
         ... when != dev = expr
    -    object_property_set_bool(OBJECT(dev), true, "realized", errp);
    +    qdev_realize_and_unref(dev, bus, errp);
The first rule exempts hw/arm/highbank.c, because it matches along two
control flow paths there, with different @type_name.  Covered by the
next commit's manual conversions.
Missing #include "qapi/error.h" added manually.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-10-armbru@redhat.com>
[Conflicts in hw/misc/empty_slot.c and hw/sparc/leon3.c resolved]
											
										 
											2020-06-10 07:31:58 +02:00
										 |  |  |     ds = qdev_new(TYPE_PXB_HOST); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     if (pcie) { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:22 +11:00
										 |  |  |         bus = pci_root_bus_new(ds, dev_name, NULL, NULL, 0, TYPE_PXB_PCIE_BUS); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:22 +11:00
										 |  |  |         bus = pci_root_bus_new(ds, "pxb-internal", NULL, NULL, 0, TYPE_PXB_BUS); | 
					
						
							| 
									
										
										
										
											2020-06-10 07:31:59 +02:00
										 |  |  |         bds = qdev_new("pci-bridge"); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |         bds->id = dev_name; | 
					
						
							|  |  |  |         qdev_prop_set_uint8(bds, PCI_BRIDGE_DEV_PROP_CHASSIS_NR, pxb->bus_nr); | 
					
						
							|  |  |  |         qdev_prop_set_bit(bds, PCI_BRIDGE_DEV_PROP_SHPC, false); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bus->parent_dev = dev; | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     bus->address_space_mem = pci_get_bus(dev)->address_space_mem; | 
					
						
							|  |  |  |     bus->address_space_io = pci_get_bus(dev)->address_space_io; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:08 +03:00
										 |  |  |     bus->map_irq = pxb_map_irq_fn; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PCI_HOST_BRIDGE(ds)->bus = bus; | 
					
						
							| 
									
										
										
										
											2021-07-08 12:55:12 +00:00
										 |  |  |     PCI_HOST_BRIDGE(ds)->bypass_iommu = pxb->bypass_iommu; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     pxb_register_bus(dev, bus, &local_err); | 
					
						
							|  |  |  |     if (local_err) { | 
					
						
							|  |  |  |         error_propagate(errp, local_err); | 
					
						
							| 
									
										
										
										
											2016-03-23 15:26:19 +08:00
										 |  |  |         goto err_register_bus; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												sysbus: Convert to sysbus_realize() etc. with Coccinelle
Convert from qdev_realize(), qdev_realize_and_unref() with null @bus
argument to sysbus_realize(), sysbus_realize_and_unref().
Coccinelle script:
    @@
    expression dev, errp;
    @@
    -    qdev_realize(DEVICE(dev), NULL, errp);
    +    sysbus_realize(SYS_BUS_DEVICE(dev), errp);
    @@
    expression sysbus_dev, dev, errp;
    @@
    +    sysbus_dev = SYS_BUS_DEVICE(dev);
    -    qdev_realize_and_unref(dev, NULL, errp);
    +    sysbus_realize_and_unref(sysbus_dev, errp);
    -    sysbus_dev = SYS_BUS_DEVICE(dev);
    @@
    expression sysbus_dev, dev, errp;
    expression expr;
    @@
         sysbus_dev = SYS_BUS_DEVICE(dev);
         ... when != dev = expr;
    -    qdev_realize_and_unref(dev, NULL, errp);
    +    sysbus_realize_and_unref(sysbus_dev, errp);
    @@
    expression dev, errp;
    @@
    -    qdev_realize_and_unref(DEVICE(dev), NULL, errp);
    +    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), errp);
    @@
    expression dev, errp;
    @@
    -    qdev_realize_and_unref(dev, NULL, errp);
    +    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), errp);
Whitespace changes minimized manually.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-46-armbru@redhat.com>
[Conflicts in hw/misc/empty_slot.c and hw/sparc/leon3.c resolved]
											
										 
											2020-06-10 07:32:34 +02:00
										 |  |  |     sysbus_realize_and_unref(SYS_BUS_DEVICE(ds), &error_fatal); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     if (bds) { | 
					
						
							| 
									
										
										
										
											2020-06-10 07:31:59 +02:00
										 |  |  |         qdev_realize_and_unref(bds, &bus->qbus, &error_fatal); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pci_word_test_and_set_mask(dev->config + PCI_STATUS, | 
					
						
							|  |  |  |                                PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); | 
					
						
							|  |  |  |     pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_HOST); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  |     pxb_dev_list = g_list_insert_sorted(pxb_dev_list, pxb, pxb_compare); | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     return; | 
					
						
							| 
									
										
										
										
											2016-03-23 15:26:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | err_register_bus: | 
					
						
							|  |  |  |     object_unref(OBJECT(bds)); | 
					
						
							|  |  |  |     object_unparent(OBJECT(bus)); | 
					
						
							|  |  |  |     object_unref(OBJECT(ds)); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  | static void pxb_dev_realize(PCIDevice *dev, Error **errp) | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     if (pci_bus_is_express(pci_get_bus(dev))) { | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |         error_setg(errp, "pxb devices cannot reside on a PCIe bus"); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     pxb_dev_realize_common(dev, false, errp); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  | static void pxb_dev_exitfn(PCIDevice *pci_dev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     PXBDev *pxb = convert_to_pxb(pci_dev); | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pxb_dev_list = g_list_remove(pxb_dev_list, pxb); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | static Property pxb_dev_properties[] = { | 
					
						
							| 
									
										
										
										
											2016-03-01 17:45:24 +08:00
										 |  |  |     /* Note: 0 is not a legal PXB bus number. */ | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     DEFINE_PROP_UINT8("bus_nr", PXBDev, bus_nr, 0), | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:10 +03:00
										 |  |  |     DEFINE_PROP_UINT16("numa_node", PXBDev, numa_node, NUMA_NODE_UNASSIGNED), | 
					
						
							| 
									
										
										
										
											2021-07-08 12:55:12 +00:00
										 |  |  |     DEFINE_PROP_BOOL("bypass_iommu", PXBDev, bypass_iommu, false), | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     DEFINE_PROP_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void pxb_dev_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							|  |  |  |     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     k->realize = pxb_dev_realize; | 
					
						
							| 
									
										
										
										
											2015-06-19 04:40:17 +02:00
										 |  |  |     k->exit = pxb_dev_exitfn; | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     k->vendor_id = PCI_VENDOR_ID_REDHAT; | 
					
						
							|  |  |  |     k->device_id = PCI_DEVICE_ID_REDHAT_PXB; | 
					
						
							|  |  |  |     k->class_id = PCI_CLASS_BRIDGE_HOST; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dc->desc = "PCI Expander Bridge"; | 
					
						
							| 
									
										
										
										
											2020-01-10 19:30:32 +04:00
										 |  |  |     device_class_set_props(dc, pxb_dev_properties); | 
					
						
							| 
									
										
										
										
											2016-07-17 19:53:10 +03:00
										 |  |  |     dc->hotpluggable = false; | 
					
						
							| 
									
										
										
										
											2016-02-03 13:56:10 +02:00
										 |  |  |     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo pxb_dev_info = { | 
					
						
							|  |  |  |     .name          = TYPE_PXB_DEVICE, | 
					
						
							|  |  |  |     .parent        = TYPE_PCI_DEVICE, | 
					
						
							|  |  |  |     .instance_size = sizeof(PXBDev), | 
					
						
							|  |  |  |     .class_init    = pxb_dev_class_init, | 
					
						
							| 
									
										
										
										
											2017-09-27 16:56:34 -03:00
										 |  |  |     .interfaces = (InterfaceInfo[]) { | 
					
						
							|  |  |  |         { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | 
					
						
							|  |  |  |         { }, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  | static void pxb_pcie_dev_realize(PCIDevice *dev, Error **errp) | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-29 19:46:27 +11:00
										 |  |  |     if (!pci_bus_is_express(pci_get_bus(dev))) { | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |         error_setg(errp, "pxb-pcie devices cannot reside on a PCI bus"); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     pxb_dev_realize_common(dev, true, errp); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void pxb_pcie_dev_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							|  |  |  |     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-17 18:18:46 +08:00
										 |  |  |     k->realize = pxb_pcie_dev_realize; | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     k->exit = pxb_dev_exitfn; | 
					
						
							|  |  |  |     k->vendor_id = PCI_VENDOR_ID_REDHAT; | 
					
						
							|  |  |  |     k->device_id = PCI_DEVICE_ID_REDHAT_PXB_PCIE; | 
					
						
							|  |  |  |     k->class_id = PCI_CLASS_BRIDGE_HOST; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dc->desc = "PCI Express Expander Bridge"; | 
					
						
							| 
									
										
										
										
											2020-01-10 19:30:32 +04:00
										 |  |  |     device_class_set_props(dc, pxb_dev_properties); | 
					
						
							| 
									
										
										
										
											2016-07-17 19:53:10 +03:00
										 |  |  |     dc->hotpluggable = false; | 
					
						
							| 
									
										
										
										
											2016-02-03 13:56:10 +02:00
										 |  |  |     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo pxb_pcie_dev_info = { | 
					
						
							|  |  |  |     .name          = TYPE_PXB_PCIE_DEVICE, | 
					
						
							|  |  |  |     .parent        = TYPE_PCI_DEVICE, | 
					
						
							|  |  |  |     .instance_size = sizeof(PXBDev), | 
					
						
							|  |  |  |     .class_init    = pxb_pcie_dev_class_init, | 
					
						
							| 
									
										
										
										
											2017-09-27 16:56:34 -03:00
										 |  |  |     .interfaces = (InterfaceInfo[]) { | 
					
						
							|  |  |  |         { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | 
					
						
							|  |  |  |         { }, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | static void pxb_register_types(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     type_register_static(&pxb_bus_info); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     type_register_static(&pxb_pcie_bus_info); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  |     type_register_static(&pxb_host_info); | 
					
						
							|  |  |  |     type_register_static(&pxb_dev_info); | 
					
						
							| 
									
										
										
										
											2015-11-26 18:00:27 +02:00
										 |  |  |     type_register_static(&pxb_pcie_dev_info); | 
					
						
							| 
									
										
										
										
											2015-06-02 14:23:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type_init(pxb_register_types) |