From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexander Gordeev 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> References: Cc: Alexander Gordeev , Thomas Huth , Andrew Jones , Peter Xu To: kvm@vger.kernel.org Return-path: 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) In-Reply-To: In-Reply-To: References: Sender: kvm-owner@vger.kernel.org List-ID: 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]; -- 1.8.3.1