From patchwork Mon Oct 7 09:21:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 281011 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 85FF42C009C for ; Mon, 7 Oct 2013 20:20:03 +1100 (EST) Received: from localhost ([::1]:58217 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VT6yu-0000GR-S4 for incoming@patchwork.ozlabs.org; Mon, 07 Oct 2013 05:20:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39892) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VT6yF-00007v-Mk for qemu-devel@nongnu.org; Mon, 07 Oct 2013 05:19:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VT6y7-0004QN-BM for qemu-devel@nongnu.org; Mon, 07 Oct 2013 05:19:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41667) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VT6y7-0004Q9-25 for qemu-devel@nongnu.org; Mon, 07 Oct 2013 05:19:11 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r979J7GG010669 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 7 Oct 2013 05:19:07 -0400 Received: from redhat.com (vpn1-7-45.ams2.redhat.com [10.36.7.45]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id r979J3H5005573; Mon, 7 Oct 2013 05:19:04 -0400 Date: Mon, 7 Oct 2013 12:21:31 +0300 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1381137467-6678-2-git-send-email-mst@redhat.com> References: <1381137467-6678-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1381137467-6678-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell , Gerd Hoffmann , Anthony Liguori , Igor Mammedov , Laszlo Ersek , afaerber@suse.de Subject: [Qemu-devel] [PATCH v3 1/4] 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 Add a method to scan pci buses in a predictable order. Useful for ACPI hotplug. Document that order is not guaranteed for pci_for_each_device, and re-implement using that. 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 4b90e5d..6259e23 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -391,6 +391,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 c3fdff4..728f7b9 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -1682,6 +1682,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);