diff mbox

[kvm-unit-tests,v2,09/17] x86/vmexit: leverage pci_scan_bars()

Message ID 1478704224-20472-10-git-send-email-peterx@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Peter Xu Nov. 9, 2016, 3:10 p.m. UTC
Since pci-testdev is a very specific device for QEMU, let's use the new
pci_scan_bars() helper, and selectively choose the bars we want.

Reviewed-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 lib/pci.h    |  2 ++
 x86/vmexit.c | 17 ++++++-----------
 2 files changed, 8 insertions(+), 11 deletions(-)

Comments

Andrew Jones Nov. 10, 2016, 7:27 p.m. UTC | #1
On Wed, Nov 09, 2016 at 10:10:16AM -0500, Peter Xu wrote:
> Since pci-testdev is a very specific device for QEMU, let's use the new
> pci_scan_bars() helper, and selectively choose the bars we want.
> 
> Reviewed-by: Andrew Jones <drjones@redhat.com>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  lib/pci.h    |  2 ++
>  x86/vmexit.c | 17 ++++++-----------
>  2 files changed, 8 insertions(+), 11 deletions(-)
> 
> diff --git a/lib/pci.h b/lib/pci.h
> index 5ed4e11..e452819 100644
> --- a/lib/pci.h
> +++ b/lib/pci.h
> @@ -68,6 +68,8 @@ 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..a22f43f 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.bar[PCI_TESTDEV_BAR_MEM];

nit: I'd drop 'membar' and just pass pcidev.bar to ioremap

> +		pci_test.memaddr = ioremap(membar, PAGE_SIZE);
> +		pci_test.iobar = pcidev.bar[PCI_TESTDEV_BAR_IO];
>  		printf("pci-testdev at 0x%x membar %lx iobar %x\n",
>  		       pcidev.bdf, membar, pci_test.iobar);
>  	}
> -- 
> 2.7.4
>
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Peter Xu Nov. 14, 2016, 8:35 p.m. UTC | #2
On Thu, Nov 10, 2016 at 08:27:58PM +0100, Andrew Jones wrote:

[...]

> > @@ -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.bar[PCI_TESTDEV_BAR_MEM];
> 
> nit: I'd drop 'membar' and just pass pcidev.bar to ioremap

Below printf() is using it as well. I kept it in case anyone is using
the below printf for any reason.

> 
> > +		pci_test.memaddr = ioremap(membar, PAGE_SIZE);
> > +		pci_test.iobar = pcidev.bar[PCI_TESTDEV_BAR_IO];
> >  		printf("pci-testdev at 0x%x membar %lx iobar %x\n",
> >  		       pcidev.bdf, membar, pci_test.iobar);
> >  	}
> > -- 
> > 2.7.4
> >

Thanks,

-- peterx
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/lib/pci.h b/lib/pci.h
index 5ed4e11..e452819 100644
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -68,6 +68,8 @@  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..a22f43f 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.bar[PCI_TESTDEV_BAR_MEM];
+		pci_test.memaddr = ioremap(membar, PAGE_SIZE);
+		pci_test.iobar = pcidev.bar[PCI_TESTDEV_BAR_IO];
 		printf("pci-testdev at 0x%x membar %lx iobar %x\n",
 		       pcidev.bdf, membar, pci_test.iobar);
 	}