spapr_pci: PCI DT node creation in QEMU
v8 cover spapr_pci: PCI DT node creation in QEMU
The patch series creates PCI device tree(DT) nodes in QEMU. The new hotplug code needs the device node creation in QEMU. While during boot, nodes were created in SLOF. It makes more sense to consolidate the code to one place for better maintainability.
New slof.bin is already there in spapr-next
Changelog V7:
* Rebased to spapr-next
* Dropped three patches that have been picked upstream
* Limited usage of sPAPRFDT to pci enumeration code (David Gibson)
* Reworked all the patches and moved cleanup in last patch.
Changelog V6:
* Open coded fdt_setprop_string as the return would leak memory (Thomas Huth)
Changelog V5:
* Reworked enumerate bridges to reduce scopes (Alexey)
* Fixed unit address which should be hex
* Added FDT_NAME_MAX and use snprintf for composing name (Alexey)
* Added patch to drop redundant args and change prototype (Alexey)
* Added bus number in the ibm,loc-code for qemu emulated or failed vfio device.
* Now qemu_<name>:<phb-index>:<bus>:<slot>.<fn> is having hex encoding (Alexey)
Changelog V4:
* Refactored and simplified the "ibm,loc-code" patch
Changelog V3:
* Dropped duplicate macro patches
* Squashed Michael's drc_index changes to enumeration patch
* Use common create routine for boottime and hotplug case
* Proper error handling not depending on g_assert
* Encode vfio loc-code if getting loc-code from host fails
Changelog V2:
* Fix device tree for 64-bit encoding
* Fix the class code, was failing xhci
* Remove macro duplication
* Fix DT fields generation for boot time device (Michael Roth)
Changelog v1:
* Correct indent problems reported by checkpatch(David Gibson)
* Declare sPAPRFDT structure as local (David Gibson)
* Re-arrange code to avoid multiple indentation (Alexey Kardashevskiy)
Nikunj A Dadhania (3):
spapr_pci: enumerate and add PCI device tree
spapr_pci: populate ibm,loc-code
spapr_pci: drop redundant args in spapr_[populate,create]_pci_child_dt
hw/ppc/spapr_pci.c | 228 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 202 insertions(+), 26 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v8 1/3 spapr_pci: enumerate and add PCI device tree
All the PCI enumeration and device node creation was off-loaded to SLOF. With PCI hotplug support, code needed to be added to add device node. This creates multiple copy of the code one in SLOF and other in hotplug code. To unify this, the patch adds the pci device node creation in Qemu. For backward compatibility, a flag "qemu,phb-enumerated" is added to the phb, suggesting to SLOF to not do device node creation.
--- hw/ppc/spapr_pci.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 133 insertions(+), 17 deletions(-)
hw/ppc/spapr_pci.c +133 -17
@@ -23,6 +23,7 @@* THE SOFTWARE. */ #include "hw/hw.h" +#include "hw/sysbus.h" #include "hw/pci/pci.h" #include "hw/pci/msi.h" #include "hw/pci/msix.h"
@@ -35,6 +36,7 @@#include "qemu/error-report.h" #include "qapi/qmp/qerror.h" +#include "hw/pci/pci_bridge.h" #include "hw/pci/pci_bus.h" #include "hw/ppc/spapr_drc.h" #include "sysemu/device_tree.h"
@@ -966,30 +968,32 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,return 0 ; } +static uint32_t spapr_phb_get_pci_drc_index (sPAPRPHBState *phb, +PCIDevice *pdev); +/* create OF node for pci device and required OF DT properties */ -static void *spapr_create_pci_child_dt (sPAPRPHBState *phb,PCIDevice *dev, -int drc_index,const char *drc_name, -int *dt_offset) +static int spapr_create_pci_child_dt (sPAPRPHBState *phb,PCIDevice *dev, +int drc_index,const char *drc_name, +void *fdt,int node_offset) { -void *fdt; -int offset, ret, fdt_size; +int offset, ret;int slot =PCI_SLOT (dev->devfn);int func =PCI_FUNC (dev->devfn);char nodename[FDT_NAME_MAX ]; - fdt =create_device_tree (&fdt_size);if (func !=0 ) {snprintf (nodename,FDT_NAME_MAX ,"pci@%x,%x" , slot, func); }else {snprintf (nodename,FDT_NAME_MAX ,"pci@%x" , slot); } - offset =fdt_add_subnode (fdt,0 , nodename); + offset =fdt_add_subnode (fdt, node_offset, nodename); ret =spapr_populate_pci_child_dt (dev, fdt, offset, phb->index, drc_index, drc_name);g_assert (!ret); - - *dt_offset = offset; -return fdt; +if (ret) { +return 0 ; + } +return offset; } static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,
@@ -1002,19 +1006,22 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,int drc_index = drck->get_index (drc);const char *drc_name = drck->get_name (drc);void *fdt =NULL ; -int fdt_start_offset =0 ; +int fdt_start_offset =0 , fdt_size; -/* boot-time devices get their device tree node created by SLOF, but for -* hotplugged devices we need QEMU to generate it so the guest can fetch -* it via RTAS -*/ if (dev->hotplugged) { - fdt =spapr_create_pci_child_dt (phb, pdev, drc_index, drc_name, - &fdt_start_offset); + fdt =create_device_tree (&fdt_size); + fdt_start_offset =spapr_create_pci_child_dt (phb, pdev, + drc_index, drc_name, + fdt,0 ); +if (!fdt_start_offset) { +error_setg (errp,"Failed to create pci child device tree node" ); + goto out; + } } drck->attach (drc,DEVICE (pdev), fdt, fdt_start_offset, !dev->hotplugged, errp); +out:if (*errp) {g_free (fdt); }
@@ -1056,6 +1063,20 @@ static sPAPRDRConnector *spapr_phb_get_pci_drc(sPAPRPHBState *phb, pdev->devfn); } +static uint32_t spapr_phb_get_pci_drc_index (sPAPRPHBState *phb, +PCIDevice *pdev) +{ +sPAPRDRConnector *drc =spapr_phb_get_pci_drc (phb, pdev); +sPAPRDRConnectorClass *drck; + +if (!drc) { +return 0 ; + } + + drck =SPAPR_DR_CONNECTOR_GET_CLASS (drc); +return drck->get_index (drc); +} +static void spapr_phb_hot_plug_child (HotplugHandler *plug_handler,DeviceState *plugged_dev,Error **errp) {
@@ -1480,6 +1501,87 @@ PCIHostState *spapr_create_phb(sPAPRMachineState *spapr, int index)return PCI_HOST_BRIDGE (dev); } +typedef struct sPAPRFDT { +void *fdt; +int node_off; +sPAPRPHBState *sphb; +}sPAPRFDT ; + +static void spapr_populate_pci_devices_dt (PCIBus *bus,PCIDevice *pdev, +void *opaque) +{ +PCIBus *sec_bus; +sPAPRFDT *p = opaque; +int offset; +sPAPRFDT s_fdt; +uint32_t drc_index =spapr_phb_get_pci_drc_index (p->sphb, pdev); + + offset =spapr_create_pci_child_dt (p->sphb, pdev, + drc_index,NULL , + p->fdt, p->node_off); +if (!offset) { +error_report ("Failed to create pci child device tree node" ); +return ; + } + +if ((pci_default_read_config (pdev,PCI_HEADER_TYPE ,1 ) != +PCI_HEADER_TYPE_BRIDGE )) { +return ; + } + + sec_bus =pci_bridge_get_sec_bus (PCI_BRIDGE (pdev)); +if (!sec_bus) { +return ; + } + + s_fdt.fdt = p->fdt; + s_fdt.node_off = offset; + s_fdt.sphb = p->sphb; +pci_for_each_device (sec_bus,pci_bus_num (sec_bus), + spapr_populate_pci_devices_dt, + &s_fdt); +} + +static void spapr_phb_pci_enumerate_bridge (PCIBus *bus,PCIDevice *pdev, +void *opaque) +{ +unsigned int *bus_no = opaque; +unsigned int primary = *bus_no; +unsigned int subordinate =0xff ; +PCIBus *sec_bus =NULL ; + +if ((pci_default_read_config (pdev,PCI_HEADER_TYPE ,1 ) != +PCI_HEADER_TYPE_BRIDGE )) { +return ; + } + + (*bus_no)++; +pci_default_write_config (pdev,PCI_PRIMARY_BUS , primary,1 ); +pci_default_write_config (pdev,PCI_SECONDARY_BUS , *bus_no,1 ); +pci_default_write_config (pdev,PCI_SUBORDINATE_BUS , *bus_no,1 ); + + sec_bus =pci_bridge_get_sec_bus (PCI_BRIDGE (pdev)); +if (!sec_bus) { +return ; + } + +pci_default_write_config (pdev,PCI_SUBORDINATE_BUS , subordinate,1 ); +pci_for_each_device (sec_bus,pci_bus_num (sec_bus), + spapr_phb_pci_enumerate_bridge, bus_no); +pci_default_write_config (pdev,PCI_SUBORDINATE_BUS , *bus_no,1 ); +} + +static void spapr_phb_pci_enumerate (sPAPRPHBState *phb) +{ +PCIBus *bus =PCI_HOST_BRIDGE (phb)->bus; +unsigned int bus_no =0 ; + +pci_for_each_device (bus,pci_bus_num (bus), + spapr_phb_pci_enumerate_bridge, + &bus_no); + +} +int spapr_populate_pci_dt (sPAPRPHBState *phb,uint32_t xics_phandle,void *fdt)
@@ -1519,6 +1621,8 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb, cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};uint32_t interrupt_map[PCI_SLOT_MAX *PCI_NUM_PINS ][7 ];sPAPRTCETable *tcet; +PCIBus *bus =PCI_HOST_BRIDGE (phb)->bus; +sPAPRFDT s_fdt;/* Start populating the FDT */ snprintf (nodename,FDT_NAME_MAX ,"pci@%" PRIx64, phb->buid);
@@ -1568,6 +1672,18 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb, tcet->liobn, tcet->bus_offset, tcet->nb_table << tcet->page_shift); +/* Walk the bridges and program the bus numbers*/ +spapr_phb_pci_enumerate (phb); +_FDT (fdt_setprop_cell (fdt, bus_off,"qemu,phb-enumerated" ,0x1 )); + +/* Populate tree nodes with PCI devices attached */ + s_fdt.fdt = fdt; + s_fdt.node_off = bus_off; + s_fdt.sphb = phb; +pci_for_each_device (bus,pci_bus_num (bus), + spapr_populate_pci_devices_dt, + &s_fdt); + ret =spapr_drc_populate_dt (fdt, bus_off,OBJECT (phb),SPAPR_DR_CONNECTOR_TYPE_PCI );if (ret) {
Signature · 1 line
1.8.3.1
PATCH v8 3/3 spapr_pci: drop redundant args in spapr_[populate, create]_pci_child_dt
In reply to Nikunj A Dadhania* phb_index is not being used and if required can be obtained from sphb
* use helper to get drc_index in spapr_populate_pci_child_dt()
* Check if drc_index is zero
--- hw/ppc/spapr_pci.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-)
hw/ppc/spapr_pci.c +11 -16
@@ -937,14 +937,17 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp) rp->assigned_len = assigned_idx *sizeof (ResourceFields); } +static uint32_t spapr_phb_get_pci_drc_index (sPAPRPHBState *phb, +PCIDevice *pdev); +static int spapr_populate_pci_child_dt (PCIDevice *dev,void *fdt,int offset, -int phb_index,int drc_index,sPAPRPHBState *sphb) {ResourceProps rp;bool is_bridge = false;int pci_status, err;char *buf =NULL ; +uint32_t drc_index =spapr_phb_get_pci_drc_index (sphb, dev);if (pci_default_read_config (dev,PCI_HEADER_TYPE ,1 ) ==PCI_HEADER_TYPE_BRIDGE ) {
@@ -1017,7 +1020,9 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,return err; } -_FDT (fdt_setprop_cell (fdt, offset,"ibm,my-drc-index" , drc_index)); +if (drc_index) { +_FDT (fdt_setprop_cell (fdt, offset,"ibm,my-drc-index" , drc_index)); + }_FDT (fdt_setprop_cell (fdt, offset,"#address-cells" ,RESOURCE_CELLS_ADDRESS ));
@@ -1034,12 +1039,8 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,return 0 ; } -static uint32_t spapr_phb_get_pci_drc_index (sPAPRPHBState *phb, -PCIDevice *pdev); -/* create OF node for pci device and required OF DT properties */ static int spapr_create_pci_child_dt (sPAPRPHBState *phb,PCIDevice *dev, -int drc_index,const char *drc_name,void *fdt,int node_offset) {int offset, ret;
@@ -1053,8 +1054,8 @@ static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev,snprintf (nodename,FDT_NAME_MAX ,"pci@%x" , slot); } offset =fdt_add_subnode (fdt, node_offset, nodename); - ret =spapr_populate_pci_child_dt (dev, fdt, offset, phb->index, drc_index, - phb); + ret =spapr_populate_pci_child_dt (dev, fdt, offset, phb); +g_assert (!ret);if (ret) {return 0 ;
@@ -1069,15 +1070,12 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc, {sPAPRDRConnectorClass *drck =SPAPR_DR_CONNECTOR_GET_CLASS (drc);DeviceState *dev =DEVICE (pdev); -int drc_index = drck->get_index (drc);void *fdt =NULL ;int fdt_start_offset =0 , fdt_size;if (dev->hotplugged) { fdt =create_device_tree (&fdt_size); - fdt_start_offset =spapr_create_pci_child_dt (phb, pdev, - drc_index,NULL , - fdt,0 ); + fdt_start_offset =spapr_create_pci_child_dt (phb, pdev, fdt,0 );if (!fdt_start_offset) {error_setg (errp,"Failed to create pci child device tree node" ); goto out;
@@ -1579,11 +1577,8 @@ static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev,sPAPRFDT *p = opaque;int offset;sPAPRFDT s_fdt; -uint32_t drc_index =spapr_phb_get_pci_drc_index (p->sphb, pdev); - offset =spapr_create_pci_child_dt (p->sphb, pdev, - drc_index,NULL , - p->fdt, p->node_off); + offset =spapr_create_pci_child_dt (p->sphb, pdev, p->fdt, p->node_off);if (!offset) {error_report ("Failed to create pci child device tree node" );return ;
Signature · 1 line
1.8.3.1
PATCH v8 2/3 spapr_pci: populate ibm,loc-code
In reply to Nikunj A DadhaniaEach hardware instance has a platform unique location code. The OF device tree that describes a part of a hardware entity must include the “ibm,loc-code” property with a value that represents the location code for that hardware entity.
Populate ibm,loc-code.
1) PCI passthru devices need to identify with its own ibm,loc-code available on the host. In failure cases use:
vfio_<name>:<phb-index>:<bus>:<slot>.<fn>
2) Emulated devices encode as following:
qemu_<name>:<phb-index>:<bus>:<slot>.<fn>
--- hw/ppc/spapr_pci.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 6 deletions(-)
hw/ppc/spapr_pci.c +71 -6
@@ -747,6 +747,60 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)return &phb->iommu_as; } +static char *spapr_phb_vfio_get_loc_code (sPAPRPHBState *sphb,PCIDevice *pdev) +{ +char *path =NULL , *buf =NULL , *host =NULL ; + +/* Get the PCI VFIO host id */ + host =object_property_get_str (OBJECT (pdev),"host" ,NULL ); +if (!host) { + goto err_out; + } + +/* Construct the path of the file that will give us the DT location */ + path =g_strdup_printf ("/sys/bus/pci/devices/%s/devspec" , host); +g_free (host); +if (!path || !g_file_get_contents (path, &buf,NULL ,NULL )) { + goto err_out; + } +g_free (path); + +/* Construct and read from host device tree the loc-code */ + path =g_strdup_printf ("/proc/device-tree%s/ibm,loc-code" , buf); +g_free (buf); +if (!path || !g_file_get_contents (path, &buf,NULL ,NULL )) { + goto err_out; + } +return buf; + +err_out: +g_free (path); +return NULL ; +} + +static char *spapr_phb_get_loc_code (sPAPRPHBState *sphb,PCIDevice *pdev) +{ +char *buf; +const char *devtype ="qemu" ; +uint32_t busnr =pci_bus_num (PCI_BUS (qdev_get_parent_bus (DEVICE (pdev)))); + +if (object_dynamic_cast (OBJECT (pdev),"vfio-pci" )) { + buf =spapr_phb_vfio_get_loc_code (sphb, pdev); +if (buf) { +return buf; + } + devtype ="vfio" ; + } +/* +* For emulated devices and VFIO-failure case, make up +* the loc-code. +*/ + buf =g_strdup_printf ("%s_%s:%04x:%02x:%02x.%x" , + devtype, pdev->name, sphb->index, busnr, +PCI_SLOT (pdev->devfn),PCI_FUNC (pdev->devfn)); +return buf; +} +/* Macros to operate with address in OF binding to PCI */ #define b_x (x, p, l) (((x) & ((1<<(l))-1)) << (p))#define b_n (x) b_x((x), 31, 1)/* 0 if relocatable */
@@ -885,11 +939,12 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)static int spapr_populate_pci_child_dt (PCIDevice *dev,void *fdt,int offset,int phb_index,int drc_index, -const char *drc_name) +sPAPRPHBState *sphb) {ResourceProps rp;bool is_bridge = false; -int pci_status; +int pci_status, err; +char *buf =NULL ;if (pci_default_read_config (dev,PCI_HEADER_TYPE ,1 ) ==PCI_HEADER_TYPE_BRIDGE ) {
@@ -950,7 +1005,18 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,* processed by OF beforehand */ _FDT (fdt_setprop_string (fdt, offset,"name" ,"pci" )); -_FDT (fdt_setprop (fdt, offset,"ibm,loc-code" , drc_name,strlen (drc_name))); + buf =spapr_phb_get_loc_code (sphb, dev); +if (!buf) { +error_report ("Failed setting the ibm,loc-code" ); +return -1 ; + } + + err =fdt_setprop_string (fdt, offset,"ibm,loc-code" , buf); +g_free (buf); +if (err <0 ) { +return err; + } +_FDT (fdt_setprop_cell (fdt, offset,"ibm,my-drc-index" , drc_index)); _FDT(fdt_setprop_cell(fdt, offset, "#address-cells",
@@ -988,7 +1054,7 @@ static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev, } offset =fdt_add_subnode (fdt, node_offset, nodename); ret =spapr_populate_pci_child_dt (dev, fdt, offset, phb->index, drc_index, - drc_name); + phb);g_assert (!ret);if (ret) {return 0 ;
@@ -1004,14 +1070,13 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,sPAPRDRConnectorClass *drck =SPAPR_DR_CONNECTOR_GET_CLASS (drc);DeviceState *dev =DEVICE (pdev);int drc_index = drck->get_index (drc); -const char *drc_name = drck->get_name (drc);void *fdt =NULL ;int fdt_start_offset =0 , fdt_size;if (dev->hotplugged) { fdt =create_device_tree (&fdt_size); fdt_start_offset =spapr_create_pci_child_dt (phb, pdev, - drc_index, drc_name, + drc_index,NULL , fdt,0 );if (!fdt_start_offset) {error_setg (errp,"Failed to create pci child device tree node" );
Signature · 1 line
1.8.3.1
David Gibson
AppliedQuoted 6 lines · Nikunj A Dadhania
The patch series creates PCI device tree(DT) nodes in QEMU. The new hotplug code needs the device node creation in QEMU. While during boot, nodes were created in SLOF. It makes more sense to consolidate the code to one place for better maintainability.
New slof.bin is already there in spapr-next
Merged into spapr-next, thanks.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson