From patchwork Wed Nov 23 10:57:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 9442943 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 AC7986075F for ; Wed, 23 Nov 2016 10:58:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD0152236A for ; Wed, 23 Nov 2016 10:58:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B182423E64; Wed, 23 Nov 2016 10:58:18 +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 5589B2236A for ; Wed, 23 Nov 2016 10:58:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935199AbcKWK6P (ORCPT ); Wed, 23 Nov 2016 05:58:15 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41182 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935098AbcKWK6K (ORCPT ); Wed, 23 Nov 2016 05:58:10 -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 5DC064E34E; Wed, 23 Nov 2016 10:58:10 +0000 (UTC) Received: from pxdev.xzpeter.org (vpn1-7-82.pek2.redhat.com [10.72.7.82]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uANAva88026812; Wed, 23 Nov 2016 05:58:07 -0500 From: Peter Xu To: kvm@vger.kernel.org Cc: drjones@redhat.com, agordeev@redhat.com, jan.kiszka@web.de, rkrcmar@redhat.com, pbonzini@redhat.com, peterx@redhat.com Subject: [PATCH kvm-unit-tests v6 09/14] pci: provide pci_scan_bars() Date: Wed, 23 Nov 2016 18:57:29 +0800 Message-Id: <1479898654-7707-10-git-send-email-peterx@redhat.com> In-Reply-To: <1479898654-7707-1-git-send-email-peterx@redhat.com> References: <1479898654-7707-1-git-send-email-peterx@redhat.com> 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.38]); Wed, 23 Nov 2016 10:58:10 +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 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 8bad8b1..ad44031 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(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); }