From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Jones Subject: Re: [kvm-unit-tests PATCH v4 08/12] pci: Add pci_print() Date: Mon, 6 Jun 2016 17:48:09 +0200 Message-ID: <20160606154809.ctby7uon3uwrlq6n@hawk.localdomain> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: kvm@vger.kernel.org, Thomas Huth To: Alexander Gordeev Return-path: Received: from mx1.redhat.com ([209.132.183.28]:46987 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751805AbcFFPsT (ORCPT ); Mon, 6 Jun 2016 11:48:19 -0400 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 676A18F505 for ; Mon, 6 Jun 2016 15:48:13 +0000 (UTC) Content-Disposition: inline In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: On Mon, Jun 06, 2016 at 02:46:37PM +0200, Alexander Gordeev wrote: > Cc: Thomas Huth > Cc: Andrew Jones > Signed-off-by: Alexander Gordeev > --- > lib/pci.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > lib/pci.h | 3 +++ > 2 files changed, 82 insertions(+) > > diff --git a/lib/pci.c b/lib/pci.c > index 5c107e7b4f4e..e715a3e51cc4 100644 > --- a/lib/pci.c > +++ b/lib/pci.c > @@ -118,3 +118,82 @@ bool pci_bar_is64(pcidevaddr_t dev, int bar_num) > return (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == > PCI_BASE_ADDRESS_MEM_TYPE_64; > } > + > +static void pci_dev_print(pcidevaddr_t dev) > +{ > + uint16_t vendor_id = pci_config_readw(dev, PCI_VENDOR_ID); > + uint16_t device_id = pci_config_readw(dev, PCI_DEVICE_ID); > + uint8_t header = pci_config_readb(dev, PCI_HEADER_TYPE); > + uint8_t progif = pci_config_readb(dev, PCI_CLASS_PROG); > + uint8_t subclass = pci_config_readb(dev, PCI_CLASS_DEVICE); > + uint8_t class = pci_config_readb(dev, PCI_CLASS_DEVICE + 1); > + int i; > + > + printf("dev %2d fn %d vendor_id %04x device_id %04x type %02x " > + "progif %02x class %02x subclass %02x\n", > + dev / 8, dev % 8, vendor_id, device_id, header, > + progif, class, subclass); > + > + if ((header & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_NORMAL) > + return; > + > + for (i = 0; i < 6; i++) { > + phys_addr_t start, end; > + uint32_t bar; > + > + if (!pci_bar_is_valid(dev, i)) > + break; > + > + start = pci_bar_get_addr(dev, i); > + end = start + pci_bar_size(dev, i) - 1; > + > + if (pci_bar_is64(dev, i)) { > + printf("\tBAR#%d,%d [%" PRIx64 "-%" PRIx64 " ", > + i, i + 1, start, end); > + i++; > + } else { > + printf("\tBAR#%d [%02x-%02x ", > + i, (uint32_t)start, (uint32_t)end); > + } > + > + bar = pci_bar_get(dev, i); > + > + if (bar & PCI_BASE_ADDRESS_SPACE_IO) { > + printf("PIO]\n"); > + continue; > + } > + > + printf("MEM"); > + > + switch (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) { > + case PCI_BASE_ADDRESS_MEM_TYPE_32: > + printf("32"); > + break; > + case PCI_BASE_ADDRESS_MEM_TYPE_1M: > + printf("1M"); > + break; > + case PCI_BASE_ADDRESS_MEM_TYPE_64: > + printf("64"); > + break; > + default: > + assert(0); > + } > + > + if (bar & PCI_BASE_ADDRESS_MEM_PREFETCH) > + printf("/p"); > + > + printf("]\n"); > + } > +} > + > +void pci_print(void) > +{ > + pcidevaddr_t dev; > + > + for (dev = 0; dev < 256; ++dev) { > + if (pci_config_readw(dev, PCI_VENDOR_ID) != (uint16_t)~0 && > + pci_config_readw(dev, PCI_DEVICE_ID) != (uint16_t)~0) { Should use pci_dev_exists() here. Or, could also just unconditionally call pci_dev_print here with a "if (!pci_dev_exists(dev)) return" in pci_dev_print. > + pci_dev_print(dev); > + } > + } > +} > diff --git a/lib/pci.h b/lib/pci.h > index db8296b2dfa7..e9911db92320 100644 > --- a/lib/pci.h > +++ b/lib/pci.h > @@ -14,6 +14,7 @@ typedef uint16_t pcidevaddr_t; > enum { > PCIDEVADDR_INVALID = 0xffff, > }; > +void pci_print(void); > bool pci_dev_exists(pcidevaddr_t dev); > pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id); > > @@ -55,4 +56,6 @@ struct pci_test_dev_hdr { > uint8_t name[]; > }; > > +#define PCI_HEADER_TYPE_MASK 0x7f > + > #endif /* PCI_H */ > -- > 1.8.3.1 >