From patchwork Mon Dec 12 03:08:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 9470011 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 B745360476 for ; Mon, 12 Dec 2016 03:20:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9BC98283F4 for ; Mon, 12 Dec 2016 03:20:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 90E292840E; Mon, 12 Dec 2016 03:20:23 +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=unavailable version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 41FDB283E0 for ; Mon, 12 Dec 2016 03:20:23 +0000 (UTC) Received: from localhost ([::1]:58371 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGH9w-0002qA-AB for patchwork-qemu-devel@patchwork.kernel.org; Sun, 11 Dec 2016 22:20:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55311) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGGz4-0002wE-GR for qemu-devel@nongnu.org; Sun, 11 Dec 2016 22:08:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cGGz2-0003PY-BC for qemu-devel@nongnu.org; Sun, 11 Dec 2016 22:08:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36376) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cGGz2-0003PO-2g for qemu-devel@nongnu.org; Sun, 11 Dec 2016 22:08:56 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (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 4DAB080082; Mon, 12 Dec 2016 03:08:55 +0000 (UTC) Received: from pxdev.xzpeter.org (vpn1-5-149.pek2.redhat.com [10.72.5.149]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uBC38M82005906; Sun, 11 Dec 2016 22:08:52 -0500 From: Peter Xu To: qemu-devel@nongnu.org, kvm@vger.kernel.org Date: Mon, 12 Dec 2016 11:08:15 +0800 Message-Id: <1481512100-10380-10-git-send-email-peterx@redhat.com> In-Reply-To: <1481512100-10380-1-git-send-email-peterx@redhat.com> References: <1481512100-10380-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 12 Dec 2016 03:08:55 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [kvm-unit-tests PATCH v8 09/14] pci: provide pci_scan_bars() 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: drjones@redhat.com, rkrcmar@redhat.com, peterx@redhat.com, agordeev@redhat.com, jan.kiszka@web.de, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Let's provide a more general way to scan PCI bars, rather than read the config registers every time. Then let x86/vmexit.c leverage pci_scan_bars() Reviewed-by: Andrew Jones Signed-off-by: Peter Xu --- lib/pci-host-generic.c | 2 +- lib/pci.c | 17 ++++++++++++++++- lib/pci.h | 5 +++++ x86/vmexit.c | 17 ++++++----------- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/pci-host-generic.c b/lib/pci-host-generic.c index 958ef70..b1d2fc5 100644 --- a/lib/pci-host-generic.c +++ b/lib/pci-host-generic.c @@ -235,7 +235,7 @@ bool pci_probe(void) cmd = PCI_COMMAND_SERR | PCI_COMMAND_PARITY; - for (i = 0; i < 6; i++) { + for (i = 0; i < PCI_BAR_NUM; i++) { u64 addr; if (pci_alloc_resource(&pci_dev, i, &addr)) { diff --git a/lib/pci.c b/lib/pci.c index 8f2356d..d4521be 100644 --- a/lib/pci.c +++ b/lib/pci.c @@ -207,7 +207,7 @@ static void pci_dev_print(pcidevaddr_t dev) if ((header & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_NORMAL) return; - for (i = 0; i < 6; i++) { + for (i = 0; i < PCI_BAR_NUM; i++) { if (pci_bar_size(&pci_dev, i)) { printf("\t"); pci_bar_print(&pci_dev, i); @@ -227,3 +227,18 @@ void pci_print(void) pci_dev_print(dev); } } + +void pci_scan_bars(struct pci_dev *dev) +{ + int i; + + for (i = 0; i < PCI_BAR_NUM; i++) { + if (!pci_bar_is_valid(dev, i)) + continue; + dev->resource[i] = pci_bar_get_addr(dev, i); + if (pci_bar_is64(dev, i)) { + i++; + dev->resource[i] = (phys_addr_t)0; + } + } +} diff --git a/lib/pci.h b/lib/pci.h index 355acd0..8215c9d 100644 --- a/lib/pci.h +++ b/lib/pci.h @@ -15,13 +15,16 @@ enum { PCIDEVADDR_INVALID = 0xffff, }; +#define PCI_BAR_NUM 6 #define PCI_DEVFN_MAX 256 struct pci_dev { uint16_t bdf; + phys_addr_t resource[PCI_BAR_NUM]; }; extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf); +extern void pci_scan_bars(struct pci_dev *dev); extern bool pci_probe(void); extern void pci_print(void); @@ -65,6 +68,8 @@ extern int pci_testdev(void); * pci-testdev supports at least three types of tests (via mmio and * portio BARs): no-eventfd, wildcard-eventfd and datamatch-eventfd */ +#define PCI_TESTDEV_BAR_MEM 0 +#define PCI_TESTDEV_BAR_IO 1 #define PCI_TESTDEV_NUM_BARS 2 #define PCI_TESTDEV_NUM_TESTS 3 diff --git a/x86/vmexit.c b/x86/vmexit.c index 63fa070..f9a22de 100644 --- a/x86/vmexit.c +++ b/x86/vmexit.c @@ -389,17 +389,12 @@ 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); - for (i = 0; i < PCI_TESTDEV_NUM_BARS; i++) { - if (!pci_bar_is_valid(&pcidev, i)) { - continue; - } - if (pci_bar_is_memory(&pcidev, i)) { - membar = pci_bar_get_addr(&pcidev, i); - pci_test.memaddr = ioremap(membar, PAGE_SIZE); - } else { - pci_test.iobar = pci_bar_get_addr(&pcidev, i); - } - } + 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]; + pci_test.memaddr = ioremap(membar, PAGE_SIZE); + pci_test.iobar = pcidev.resource[PCI_TESTDEV_BAR_IO]; printf("pci-testdev at 0x%x membar %lx iobar %x\n", pcidev.bdf, membar, pci_test.iobar); }