From patchwork Thu Jun 17 11:03:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 56016 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 49B46B7D9C for ; Thu, 17 Jun 2010 21:18:58 +1000 (EST) Received: from localhost ([127.0.0.1]:51356 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPD7X-0000H5-6S for incoming@patchwork.ozlabs.org; Thu, 17 Jun 2010 07:18:55 -0400 Received: from [140.186.70.92] (port=37979 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPCw4-000353-Ia for qemu-devel@nongnu.org; Thu, 17 Jun 2010 07:07:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OPCw2-0001Ll-G0 for qemu-devel@nongnu.org; Thu, 17 Jun 2010 07:07:04 -0400 Received: from mail.valinux.co.jp ([210.128.90.3]:46454) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OPCw1-0001L8-KI for qemu-devel@nongnu.org; Thu, 17 Jun 2010 07:07:02 -0400 Received: from ps.local.valinux.co.jp (vagw.valinux.co.jp [210.128.90.14]) by mail.valinux.co.jp (Postfix) with SMTP id 6417C1072BF; Thu, 17 Jun 2010 20:06:57 +0900 (JST) Received: (nullmailer pid 8737 invoked by uid 1000); Thu, 17 Jun 2010 11:03:16 -0000 From: Isaku Yamahata To: seabios@seabios.org Date: Thu, 17 Jun 2010 20:03:09 +0900 Message-Id: <0251c9fa80ffb96f219e8fddef954371e84a281d.1276771355.git.yamahata@valinux.co.jp> X-Mailer: git-send-email 1.6.6.1 In-Reply-To: References: In-Reply-To: References: X-Virus-Scanned: clamav-milter 0.95.2 at va-mail.local.valinux.co.jp X-Virus-Status: Clean X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: stefano.stabellini@eu.citrix.com, jan.kiszka@siemens.com, mst@redhat.com, allen.m.kay@intel.com, qemu-devel@nongnu.org, yamahata@valinux.co.jp, jean.guyader@gmail.com Subject: [Qemu-devel] [PATCH 1/8] seabios: pci: introduce foreachpci_in_bus() helper macro. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch introduces foreachpci_in_bus() helper macro for depth first recursion. foreachpci() is for width first recursion. The macro will be used later to initialize pci bridge that requires depth first recursion. Signed-off-by: Isaku Yamahata --- src/pci.c | 30 ++++++++++++++++++++++++++++++ src/pci.h | 11 +++++++++++ 2 files changed, 41 insertions(+), 0 deletions(-) diff --git a/src/pci.c b/src/pci.c index 1ab3c2c..d418b4b 100644 --- a/src/pci.c +++ b/src/pci.c @@ -157,6 +157,36 @@ pci_find_vga(void) } } +// Helper function for foreachpci_in_bus() macro - return next devfn +int +pci_next_in_bus(int bus, int devfn) +{ + int bdf = pci_bus_devfn_to_bdf(bus, devfn); + if (pci_bdf_to_fn(bdf) == 1 + && (pci_config_readb(bdf-1, PCI_HEADER_TYPE) & 0x80) == 0) + // Last found device wasn't a multi-function device - skip to + // the next device. + devfn += 7; + + for (;;) { + if (devfn >= 0x100) + return -1; + + bdf = pci_bus_devfn_to_bdf(bus, devfn); + u16 v = pci_config_readw(bdf, PCI_VENDOR_ID); + if (v != 0x0000 && v != 0xffff) + // Device is present. + break; + + if (pci_bdf_to_fn(bdf) == 0) + devfn += 8; + else + devfn += 1; + } + + return devfn; +} + // Search for a device with the specified vendor and device ids. int pci_find_device(u16 vendid, u16 devid) diff --git a/src/pci.h b/src/pci.h index 8a21c06..26bfd40 100644 --- a/src/pci.h +++ b/src/pci.h @@ -21,6 +21,9 @@ static inline u8 pci_bdf_to_fn(u16 bdf) { static inline u16 pci_to_bdf(int bus, int dev, int fn) { return (bus<<8) | (dev<<3) | fn; } +static inline u16 pci_bus_devfn_to_bdf(int bus, u16 devfn) { + return (bus << 8) | devfn; +} static inline u32 pci_vd(u16 vendor, u16 device) { return (device << 16) | vendor; @@ -50,6 +53,14 @@ int pci_next(int bdf, int *pmax); ; BDF >= 0 \ ; BDF=pci_next(BDF+1, &MAX)) +int pci_next_in_bus(int bus, int devfn); +#define foreachpci_in_bus(BUS, DEVFN, BDF) \ + for (DEVFN = pci_next_in_bus(BUS, 0), \ + BDF = pci_bus_devfn_to_bdf(BUS, DEVFN) \ + ; DEVFN >= 0 \ + ; DEVFN = pci_next_in_bus(BUS, DEVFN + 1), \ + BDF = pci_bus_devfn_to_bdf(BUS, DEVFN)) + // pirtable.c void create_pirtable(void);