From patchwork Tue Feb 28 18:08:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Gordeev X-Patchwork-Id: 9596489 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D847A600CB for ; Tue, 28 Feb 2017 18:09:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D06EC28549 for ; Tue, 28 Feb 2017 18:09:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C546F28554; Tue, 28 Feb 2017 18:09:02 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6471728549 for ; Tue, 28 Feb 2017 18:09:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751460AbdB1SJA (ORCPT ); Tue, 28 Feb 2017 13:09:00 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55926 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751352AbdB1SI4 (ORCPT ); Tue, 28 Feb 2017 13:08:56 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 19FB28047C for ; Tue, 28 Feb 2017 18:08:39 +0000 (UTC) Received: from dhcp-27-118.brq.redhat.com (dhcp-27-122.brq.redhat.com [10.34.27.122]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1SI8WNq012601; Tue, 28 Feb 2017 13:08:37 -0500 From: Alexander Gordeev To: kvm@vger.kernel.org Cc: Alexander Gordeev , Thomas Huth , Andrew Jones , Peter Xu Subject: [kvm-unit-tests PATCH v4 3/5] pci: Turn struct pci_dev into device handle for PCI functions Date: Tue, 28 Feb 2017 19:08:28 +0100 Message-Id: <598313d9e84b2ac31d9848c778a86c9245ccb3da.1488304691.git.agordeev@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 28 Feb 2017 18:08:39 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently struct pci_dev is used for caching PCI device info used by some functions. This update turns the struct into device handle that will be used by nearly all existing and future APIs. As result of this change a pci_dev should be initialized with pci_dev_init() and pci_scan_bars() becomes redundant. Cc: Thomas Huth Cc: Andrew Jones Cc: Peter Xu Reviewed-by: Andrew Jones Signed-off-by: Alexander Gordeev --- lib/pci.c | 30 +++++++++++++++++------------- lib/pci.h | 1 - x86/vmexit.c | 1 - 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/pci.c b/lib/pci.c index 9acc9652cb25..cf33b894759d 100644 --- a/lib/pci.c +++ b/lib/pci.c @@ -90,12 +90,6 @@ bool pci_dev_exists(pcidevaddr_t dev) pci_config_readw(dev, PCI_DEVICE_ID) != 0xffff); } -void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf) -{ - memset(dev, 0, sizeof(*dev)); - dev->bdf = bdf; -} - /* Scan bus look for a specific device. Only bus 0 scanned for now. */ pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id) { @@ -122,7 +116,7 @@ uint32_t pci_bar_get(struct pci_dev *dev, int bar_num) bar_num * 4); } -phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num) +static phys_addr_t __pci_bar_get_addr(struct pci_dev *dev, int bar_num) { uint32_t bar = pci_bar_get(dev, bar_num); uint32_t mask = pci_bar_mask(bar); @@ -138,15 +132,23 @@ phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num) return phys_addr; } +phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num) +{ + return dev->resource[bar_num]; +} + void pci_bar_set_addr(struct pci_dev *dev, int bar_num, phys_addr_t addr) { int off = PCI_BASE_ADDRESS_0 + bar_num * 4; pci_config_writel(dev->bdf, off, (uint32_t)addr); + dev->resource[bar_num] = addr; - if (pci_bar_is64(dev, bar_num)) - pci_config_writel(dev->bdf, off + 4, - (uint32_t)(addr >> 32)); + if (pci_bar_is64(dev, bar_num)) { + assert(bar_num + 1 < PCI_BAR_NUM); + pci_config_writel(dev->bdf, off + 4, (uint32_t)(addr >> 32)); + dev->resource[bar_num + 1] = dev->resource[bar_num]; + } } /* @@ -326,13 +328,16 @@ void pci_print(void) } } -void pci_scan_bars(struct pci_dev *dev) +void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf) { int i; + memset(dev, 0, sizeof(*dev)); + dev->bdf = bdf; + for (i = 0; i < PCI_BAR_NUM; i++) { if (pci_bar_size(dev, i)) { - dev->resource[i] = pci_bar_get_addr(dev, i); + dev->resource[i] = __pci_bar_get_addr(dev, i); if (pci_bar_is64(dev, i)) { assert(i + 1 < PCI_BAR_NUM); dev->resource[i + 1] = dev->resource[i]; @@ -360,7 +365,6 @@ static void pci_cap_setup(struct pci_dev *dev, int cap_offset, int cap_id) void pci_enable_defaults(struct pci_dev *dev) { - pci_scan_bars(dev); /* Enable device DMA operations */ pci_cmd_set_clr(dev, PCI_COMMAND_MASTER, 0); pci_cap_walk(dev, pci_cap_setup); diff --git a/lib/pci.h b/lib/pci.h index 3da3ccc8c791..fefd9a84b307 100644 --- a/lib/pci.h +++ b/lib/pci.h @@ -28,7 +28,6 @@ struct pci_dev { }; extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf); -extern void pci_scan_bars(struct pci_dev *dev); extern void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr); typedef void (*pci_cap_handler_t)(struct pci_dev *dev, int cap_offset, int cap_id); extern void pci_cap_walk(struct pci_dev *dev, pci_cap_handler_t handler); diff --git a/x86/vmexit.c b/x86/vmexit.c index 71f4d156b3ee..5b821b5eb125 100644 --- a/x86/vmexit.c +++ b/x86/vmexit.c @@ -519,7 +519,6 @@ int main(int ac, char **av) ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST); if (ret != PCIDEVADDR_INVALID) { pci_dev_init(&pcidev, ret); - pci_scan_bars(&pcidev); assert(pci_bar_is_memory(&pcidev, PCI_TESTDEV_BAR_MEM)); assert(!pci_bar_is_memory(&pcidev, PCI_TESTDEV_BAR_IO)); membar = pcidev.resource[PCI_TESTDEV_BAR_MEM];