From patchwork Thu May 23 05:29:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gibson X-Patchwork-Id: 10956899 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F2D3D112C for ; Thu, 23 May 2019 05:31:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CCD6E27FA8 for ; Thu, 23 May 2019 05:31:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE46627FC0; Thu, 23 May 2019 05:31:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1B0F627FA8 for ; Thu, 23 May 2019 05:31:41 +0000 (UTC) Received: from localhost ([127.0.0.1]:57926 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTgKK-0001EY-9c for patchwork-qemu-devel@patchwork.kernel.org; Thu, 23 May 2019 01:31:40 -0400 Received: from eggs.gnu.org ([209.51.188.92]:41253) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTgIK-00080H-PS for qemu-devel@nongnu.org; Thu, 23 May 2019 01:29:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTgII-0005hH-Mz for qemu-devel@nongnu.org; Thu, 23 May 2019 01:29:36 -0400 Received: from ozlabs.org ([203.11.71.1]:38483) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hTgIF-0005eP-7q; Thu, 23 May 2019 01:29:33 -0400 Received: by ozlabs.org (Postfix, from userid 1007) id 458dNM0wh6z9s9y; Thu, 23 May 2019 15:29:22 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1558589363; bh=fT2wZ0CY2xyh0IA/lNK7JougwtDvFHGZG6nrpkSyQ+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hzdMxLQ+jkCL/8c3CRoSQvq5rj6UAIBkyTVEavuaijHPk7aTiDNXWkg+aPhvm+Vkj zakt9yCvBEIoTaAbzzTj6ZC7oMVVzzC/2GVWvahlaUa8XaoiIiGYCQt3e5A2N7od// N4cUneahgwbGakGJpyDB2sk6cTxmGiXE77d9M7Jk= From: David Gibson To: qemu-ppc@nongnu.org Date: Thu, 23 May 2019 15:29:15 +1000 Message-Id: <20190523052918.1129-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523052918.1129-1-david@gibson.dropbear.id.au> References: <20190523052918.1129-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 203.11.71.1 Subject: [Qemu-devel] [PATCH 5/8] spapr: Clean up DRC index construction X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mst@redhat.com, qemu-devel@nongnu.org, groug@kaod.org, clg@kaod.org, mdroth@linux.ibm.com, David Gibson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP spapr_pci.c currently has several confusingly similarly named functions for various conversions between representations of DRCs. Make things clearer by renaming things in a more consistent XXX_from_YYY() manner and remove some called-only-once variants in favour of open coding. While we're at it, move this code together in the file to avoid some extra forward references, and split out construction and removal of DRCs for the host bridge into helper functions. Signed-off-by: David Gibson --- hw/ppc/spapr_pci.c | 124 +++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 04855d3125..f799f8b423 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -1216,8 +1216,60 @@ static const char *dt_name_from_class(uint8_t class, uint8_t subclass, return name; } -static uint32_t spapr_phb_get_pci_drc_index(SpaprPhbState *phb, - PCIDevice *pdev); +/* + * DRC helper functions + */ + +static uint32_t drc_id_from_devfn(SpaprPhbState *phb, + uint32_t busnr, + int32_t devfn) +{ + return (phb->index << 16) | (busnr << 8) | devfn; +} + +static SpaprDrc *drc_from_devfn(SpaprPhbState *phb, + uint32_t busnr, int32_t devfn) +{ + return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI, + drc_id_from_devfn(phb, busnr, devfn)); +} + +static SpaprDrc *drc_from_dev(SpaprPhbState *phb, PCIDevice *dev) +{ + uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(dev)))); + return drc_from_devfn(phb, busnr, dev->devfn); +} + +static void add_drcs(SpaprPhbState *phb) +{ + int i; + + if (!phb->dr_enabled) { + return; + } + + for (i = 0; i < PCI_SLOT_MAX * PCI_FUNC_MAX; i++) { + spapr_dr_connector_new(OBJECT(phb), TYPE_SPAPR_DRC_PCI, + drc_id_from_devfn(phb, 0, i)); + } +} + +static void remove_drcs(SpaprPhbState *phb) +{ + int i; + + if (!phb->dr_enabled) { + return; + } + + for (i = PCI_SLOT_MAX * PCI_FUNC_MAX - 1; i >= 0; i--) { + SpaprDrc *drc = drc_from_devfn(phb, 0, i); + + if (drc) { + object_unparent(OBJECT(drc)); + } + } +} typedef struct PciWalkFdt { void *fdt; @@ -1284,7 +1336,7 @@ static int spapr_dt_pci_device(SpaprPhbState *sphb, PCIDevice *dev, int func = PCI_FUNC(dev->devfn); PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); ResourceProps rp; - uint32_t drc_index = spapr_phb_get_pci_drc_index(sphb, dev); + SpaprDrc *drc = drc_from_dev(sphb, dev); uint32_t vendor_id = pci_default_read_config(dev, PCI_VENDOR_ID, 2); uint32_t device_id = pci_default_read_config(dev, PCI_DEVICE_ID, 2); uint32_t revision_id = pci_default_read_config(dev, PCI_REVISION_ID, 1); @@ -1351,8 +1403,9 @@ static int spapr_dt_pci_device(SpaprPhbState *sphb, PCIDevice *dev, _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", loc_code)); g_free(loc_code); - if (drc_index) { - _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)); + if (drc) { + _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", + spapr_drc_index(drc))); } if (msi_present(dev)) { @@ -1402,33 +1455,6 @@ void spapr_phb_remove_pci_device_cb(DeviceState *dev) object_unparent(OBJECT(dev)); } -static SpaprDrc *spapr_phb_get_pci_func_drc(SpaprPhbState *phb, - uint32_t busnr, - int32_t devfn) -{ - return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI, - (phb->index << 16) | (busnr << 8) | devfn); -} - -static SpaprDrc *spapr_phb_get_pci_drc(SpaprPhbState *phb, - PCIDevice *pdev) -{ - uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)))); - return spapr_phb_get_pci_func_drc(phb, busnr, pdev->devfn); -} - -static uint32_t spapr_phb_get_pci_drc_index(SpaprPhbState *phb, - PCIDevice *pdev) -{ - SpaprDrc *drc = spapr_phb_get_pci_drc(phb, pdev); - - if (!drc) { - return 0; - } - - return spapr_drc_index(drc); -} - int spapr_pci_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr, void *fdt, int *fdt_start_offset, Error **errp) { @@ -1445,7 +1471,7 @@ static void spapr_pci_plug(HotplugHandler *plug_handler, { SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); PCIDevice *pdev = PCI_DEVICE(plugged_dev); - SpaprDrc *drc = spapr_phb_get_pci_drc(phb, pdev); + SpaprDrc *drc = drc_from_dev(phb, pdev); Error *local_err = NULL; PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))); uint32_t slotnr = PCI_SLOT(pdev->devfn); @@ -1496,8 +1522,8 @@ static void spapr_pci_plug(HotplugHandler *plug_handler, SpaprDrcClass *func_drck; SpaprDREntitySense state; - func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus), - PCI_DEVFN(slotnr, i)); + func_drc = drc_from_devfn(phb, pci_bus_num(bus), + PCI_DEVFN(slotnr, i)); func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); state = func_drck->dr_entity_sense(func_drc); @@ -1533,7 +1559,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler, { SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); PCIDevice *pdev = PCI_DEVICE(plugged_dev); - SpaprDrc *drc = spapr_phb_get_pci_drc(phb, pdev); + SpaprDrc *drc = drc_from_dev(phb, pdev); if (!phb->dr_enabled) { error_setg(errp, QERR_BUS_NO_HOTPLUG, @@ -1555,8 +1581,8 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler, /* ensure any other present functions are pending unplug */ if (PCI_FUNC(pdev->devfn) == 0) { for (i = 1; i < 8; i++) { - func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus), - PCI_DEVFN(slotnr, i)); + func_drc = drc_from_devfn(phb, pci_bus_num(bus), + PCI_DEVFN(slotnr, i)); func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); state = func_drck->dr_entity_sense(func_drc); if (state == SPAPR_DR_ENTITY_SENSE_PRESENT @@ -1577,8 +1603,8 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler, */ if (PCI_FUNC(pdev->devfn) == 0) { for (i = 7; i >= 0; i--) { - func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus), - PCI_DEVFN(slotnr, i)); + func_drc = drc_from_devfn(phb, pci_bus_num(bus), + PCI_DEVFN(slotnr, i)); func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); state = func_drck->dr_entity_sense(func_drc); if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { @@ -1626,16 +1652,7 @@ static void spapr_phb_unrealize(DeviceState *dev, Error **errp) } } - if (sphb->dr_enabled) { - for (i = PCI_SLOT_MAX * 8 - 1; i >= 0; i--) { - SpaprDrc *drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PCI, - (sphb->index << 16) | i); - - if (drc) { - object_unparent(OBJECT(drc)); - } - } - } + remove_drcs(sphb); for (i = PCI_NUM_PINS - 1; i >= 0; i--) { if (sphb->lsi_table[i].irq) { @@ -1861,12 +1878,7 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp) } /* allocate connectors for child PCI devices */ - if (sphb->dr_enabled) { - for (i = 0; i < PCI_SLOT_MAX * 8; i++) { - spapr_dr_connector_new(OBJECT(phb), TYPE_SPAPR_DRC_PCI, - (sphb->index << 16) | i); - } - } + add_drcs(sphb); /* DMA setup */ for (i = 0; i < windows_supported; ++i) {