From patchwork Mon Jan 20 14:10:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 312553 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 1AE4C2C007C for ; Tue, 21 Jan 2014 01:14:23 +1100 (EST) Received: from localhost ([::1]:52237 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5FcK-0000ku-L7 for incoming@patchwork.ozlabs.org; Mon, 20 Jan 2014 09:14:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43863) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5FTz-0006Nf-5z for qemu-devel@nongnu.org; Mon, 20 Jan 2014 09:05:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W5FTs-0001Tl-Tk for qemu-devel@nongnu.org; Mon, 20 Jan 2014 09:05:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:10096) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5FTs-0001TX-C3 for qemu-devel@nongnu.org; Mon, 20 Jan 2014 09:05:36 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s0KE5ZAu022632 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 20 Jan 2014 09:05:35 -0500 Received: from redhat.com (vpn1-4-138.ams2.redhat.com [10.36.4.138]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id s0KE5YGK024974 for ; Mon, 20 Jan 2014 09:05:35 -0500 Date: Mon, 20 Jan 2014 16:10:20 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1390226872-781-18-git-send-email-mst@redhat.com> References: <1390226872-781-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1390226872-781-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 17/29] pci: add pci_for_each_bus_depth_first X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Useful for ACPI hotplug. Signed-off-by: Michael S. Tsirkin --- include/hw/pci/pci.h | 14 ++++++++++++++ hw/pci/pci.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index 754b82d..5252346 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -387,6 +387,20 @@ int pci_bus_num(PCIBus *s); void pci_for_each_device(PCIBus *bus, int bus_num, void (*fn)(PCIBus *bus, PCIDevice *d, void *opaque), void *opaque); +void pci_for_each_bus_depth_first(PCIBus *bus, + void *(*begin)(PCIBus *bus, void *parent_state), + void (*end)(PCIBus *bus, void *state), + void *parent_state); + +/* Use this wrapper when specific scan order is not required. */ +static inline +void pci_for_each_bus(PCIBus *bus, + void (*fn)(PCIBus *bus, void *opaque), + void *opaque) +{ + pci_for_each_bus_depth_first(bus, NULL, fn, opaque); +} + PCIBus *pci_find_primary_bus(void); PCIBus *pci_device_root_bus(const PCIDevice *d); const char *pci_root_bus_path(PCIDevice *dev); diff --git a/hw/pci/pci.c b/hw/pci/pci.c index aa2a395..2aca8a4 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -1704,6 +1704,34 @@ static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num) return NULL; } +void pci_for_each_bus_depth_first(PCIBus *bus, + void *(*begin)(PCIBus *bus, void *parent_state), + void (*end)(PCIBus *bus, void *state), + void *parent_state) +{ + PCIBus *sec; + void *state; + + if (!bus) { + return; + } + + if (begin) { + state = begin(bus, parent_state); + } else { + state = parent_state; + } + + QLIST_FOREACH(sec, &bus->child, sibling) { + pci_for_each_bus_depth_first(sec, begin, end, state); + } + + if (end) { + end(bus, state); + } +} + + PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn) { bus = pci_find_bus_nr(bus, bus_num);