From nobody Sat May 18 11:46:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1635395613291928.1465098050247; Wed, 27 Oct 2021 21:33:33 -0700 (PDT) Received: from localhost ([::1]:59996 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfx6a-0005xo-2N for importer@patchew.org; Thu, 28 Oct 2021 00:33:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49062) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx4x-0003uW-Tx for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:31:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:36423) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx4v-0000Pq-W6 for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:31:51 -0400 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-285-zv3TowQaP_a7PhSvTlnWXQ-1; Thu, 28 Oct 2021 00:31:48 -0400 Received: by mail-pg1-f197.google.com with SMTP id t75-20020a63784e000000b002993a9284b0so2678111pgc.11 for ; Wed, 27 Oct 2021 21:31:48 -0700 (PDT) Received: from localhost.localdomain ([191.101.132.60]) by smtp.gmail.com with ESMTPSA id k22sm1483074pfi.149.2021.10.27.21.31.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Oct 2021 21:31:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635395509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kMuvGj1yvNCOYzU3xwimoAnfIFfyj/RCjotL/X8hM4E=; b=goUQR8X+3qSeAV42PfYjIJS51B7ASRUhAbK5RvyGLYH6itLsERfF1Uxz+JjbUoB/6CE1fz lxMXeEbnWEr1QQ3BfYspqslCFxvfiP8QoldedRZ/w2OxTN+pz4QPCENOjXOF1W4wcsAzzZ 838XQxCqN0xHFyS4Vm8Jd+VtCgYSYU8= X-MC-Unique: zv3TowQaP_a7PhSvTlnWXQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kMuvGj1yvNCOYzU3xwimoAnfIFfyj/RCjotL/X8hM4E=; b=5VainmLWNiS0ZXnphOzr0N1nokCWBSD+HaCTWqN8qX/UL+Bdu6PBIQ9I2Ib33rkrmA h6+eMUVHOQh3kUgdL5JRKC7vACDi9gXq0KyWaII7IRGZytqFtdEpsq814aD2QzmehZ3/ 2gggvCHTV2toPytGvlo56u94hsmwRTCMzGQyrKjDbS/TNQhyhyDPgqIGA0dAIKhW0pNj Nm4FBL6+3i8pFAzu/iWVmaapTXbYVn5VHNs4iH962fHqyAh1kQbuSgs4quChDnVl2BqH /kBWR4Rg57AhSNY8j+gkgbd9jqRJZ57U0b3R924Mkx7oUmv5YOv0DsuH5rHOuTZz/9Hs xuKQ== X-Gm-Message-State: AOAM532udAZNW2I8fwX0/CLgv09xCJ5dQcfygEsqY40Z2ONX81H/3zQR NH6XB8gLKy4TeqVCsT8Y095MR7LhQFHxWWr/YMN4/pk/UocWNGtItSgzY4+pPl4sfYgmFhsL/44 hOjICop50SN/IwWo5MiOEefucIoaQGI0LRefNFFyXaIBT6tEDKzgLB8ex0alsjvmx X-Received: by 2002:a05:6a00:238d:b0:47c:2232:80d8 with SMTP id f13-20020a056a00238d00b0047c223280d8mr1792701pfc.12.1635395506954; Wed, 27 Oct 2021 21:31:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwlcA0pqqNn9r0J2KR0THnplcH62KPtET0KGVJ3AQTZoImNgw7y9fr+Ky2ncdcZJpNzjeqZDg== X-Received: by 2002:a05:6a00:238d:b0:47c:2232:80d8 with SMTP id f13-20020a056a00238d00b0047c223280d8mr1792655pfc.12.1635395506583; Wed, 27 Oct 2021 21:31:46 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v2 1/5] pci: Define pci_bus_dev_fn/pci_bus_fn/pci_bus_ret_fn Date: Thu, 28 Oct 2021 12:31:25 +0800 Message-Id: <20211028043129.38871-2-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211028043129.38871-1-peterx@redhat.com> References: <20211028043129.38871-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Daniel P . Berrange" , Eduardo Habkost , David Hildenbrand , Jason Wang , "Michael S . Tsirkin" , Markus Armbruster , peterx@redhat.com, Eric Auger , Alex Williamson , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1635395613868100001 Content-Type: text/plain; charset="utf-8" They're used in quite a few places of pci.[ch] and also in the rest of the = code base. Define them so that it doesn't need to be defined all over the place= s. The pci_bus_fn is similar to pci_bus_dev_fn that only takes a PCIBus* and an opaque. The pci_bus_ret_fn is similar to pci_bus_fn but it allows to retur= n a void* pointer. Reviewed-by: David Hildenbrand Reviewed-by: Eric Auger Signed-off-by: Peter Xu --- hw/pci/pci.c | 20 ++++++-------------- include/hw/pci/pci.h | 19 +++++++++---------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 186758ee11..17e59cb3a3 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -1655,9 +1655,7 @@ static const pci_class_desc pci_class_descriptions[] = =3D }; =20 static void pci_for_each_device_under_bus_reverse(PCIBus *bus, - void (*fn)(PCIBus *b, - PCIDevice *d, - void *opaque), + pci_bus_dev_fn fn, void *opaque) { PCIDevice *d; @@ -1672,8 +1670,7 @@ static void pci_for_each_device_under_bus_reverse(PCI= Bus *bus, } =20 void pci_for_each_device_reverse(PCIBus *bus, int bus_num, - void (*fn)(PCIBus *b, PCIDevice *d, void *opaque), - void *opaque) + pci_bus_dev_fn fn, void *opaque) { bus =3D pci_find_bus_nr(bus, bus_num); =20 @@ -1683,9 +1680,7 @@ void pci_for_each_device_reverse(PCIBus *bus, int bus= _num, } =20 static void pci_for_each_device_under_bus(PCIBus *bus, - void (*fn)(PCIBus *b, PCIDevice = *d, - void *opaque), - void *opaque) + pci_bus_dev_fn fn, void *opaque) { PCIDevice *d; int devfn; @@ -1699,8 +1694,7 @@ static void pci_for_each_device_under_bus(PCIBus *bus, } =20 void pci_for_each_device(PCIBus *bus, int bus_num, - void (*fn)(PCIBus *b, PCIDevice *d, void *opaque), - void *opaque) + pci_bus_dev_fn fn, void *opaque) { bus =3D pci_find_bus_nr(bus, bus_num); =20 @@ -2078,10 +2072,8 @@ static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_= num) return NULL; } =20 -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) +void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, + pci_bus_fn end, void *parent_state) { PCIBus *sec; void *state; diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index 7fc90132cf..4a8740b76b 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -401,6 +401,10 @@ typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque,= int pin); OBJECT_DECLARE_TYPE(PCIBus, PCIBusClass, PCI_BUS) #define TYPE_PCIE_BUS "PCIE" =20 +typedef void (*pci_bus_dev_fn)(PCIBus *b, PCIDevice *d, void *opaque); +typedef void (*pci_bus_fn)(PCIBus *b, void *opaque); +typedef void *(*pci_bus_ret_fn)(PCIBus *b, void *opaque); + bool pci_bus_is_express(PCIBus *bus); =20 void pci_root_bus_init(PCIBus *bus, size_t bus_size, DeviceState *parent, @@ -458,23 +462,18 @@ static inline int pci_dev_bus_num(const PCIDevice *de= v) =20 int pci_bus_numa_node(PCIBus *bus); void pci_for_each_device(PCIBus *bus, int bus_num, - void (*fn)(PCIBus *bus, PCIDevice *d, void *opaqu= e), + pci_bus_dev_fn fn, void *opaque); void pci_for_each_device_reverse(PCIBus *bus, int bus_num, - void (*fn)(PCIBus *bus, PCIDevice *d, - void *opaque), + pci_bus_dev_fn fn, 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); +void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, + pci_bus_fn end, void *parent_state); PCIDevice *pci_get_function_0(PCIDevice *pci_dev); =20 /* 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) +void pci_for_each_bus(PCIBus *bus, pci_bus_fn fn, void *opaque) { pci_for_each_bus_depth_first(bus, NULL, fn, opaque); } --=20 2.32.0 From nobody Sat May 18 11:46:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1635395630199331.99387498463045; Wed, 27 Oct 2021 21:33:50 -0700 (PDT) Received: from localhost ([::1]:60424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfx6q-0006Ec-WB for importer@patchew.org; Thu, 28 Oct 2021 00:33:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49078) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx55-00047P-T1 for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:31:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35842) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx54-0000TJ-0C for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:31:59 -0400 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-458-jqJvFVp6MXu585TT4_-Diw-1; Thu, 28 Oct 2021 00:31:56 -0400 Received: by mail-pj1-f69.google.com with SMTP id y22-20020a17090a6c9600b001a38db472c0so2522911pjj.7 for ; Wed, 27 Oct 2021 21:31:56 -0700 (PDT) Received: from localhost.localdomain ([191.101.132.60]) by smtp.gmail.com with ESMTPSA id k22sm1483074pfi.149.2021.10.27.21.31.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Oct 2021 21:31:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635395517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n8lDU7FEmiXvrdQWCCVGE+omVdmuTswIs2+DgDzt7Rc=; b=F/Gkbhoco4UjmleJhZHURI9fPwP3OQfdX6Q4iJ28KE6cUGZgCbGM6WG/3gAEhMulj9YFPm cY3oXXt3UmOQgqFAsGu2AHNAYfNclnBExGoE2kx2v8jG0mVE+VciO49svYqH+uH6qNE31U rZxvn8YKY8EMLhx7VFECn4mACkvflX8= X-MC-Unique: jqJvFVp6MXu585TT4_-Diw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=n8lDU7FEmiXvrdQWCCVGE+omVdmuTswIs2+DgDzt7Rc=; b=sgr5OY1ZONP/vQo8wF/dX0GXzg8epwUrlwmUlLcMAOr0pyDn2QMRa6ZaB1O7ZYcNB3 V0835aW2PM6kVW1ExiLHtvpkyolSCQ+6/36qwz9t6MZInhOVivpPj0x0qWFcQGqCJlGj p1PV1rY7qmpeFxvKG9ynkuAoc1pCZ5e1ijEHkW3uML7i0UkuUR+ZXT6vHAz1nEDA2sP1 Y2SA3bXwT4NCQS0xCYPQ6gXsvTDP5i/oRbWDNi6DJmd5o6c/HY6JkLW5sdXduvFjOlZn wutTprqRnfKmnwHh8zqZL60AZa+AOxNnbjnj6ZlQzrgBJY8gnVlrLe043IoMI2YVN4kR /H4w== X-Gm-Message-State: AOAM53090Z8k3fNPELl14Aon4/0ilWh/FOm7HKQJVBjATWpsi1VC1Mgo kbpq1szXUXZyMAR+3WDMbJd/5edl1q6Cy5C25vnCYNPHgwBgXVB4SDH3kMeuLj7lhJbyPOLSXlM Py0oy35UzCAAiOwYvv0q2eAq7fr2sgyIpif9xXPzSp/ndr7AnnLeMpFPU2FPs03IM X-Received: by 2002:aa7:8111:0:b0:47b:e311:cdae with SMTP id b17-20020aa78111000000b0047be311cdaemr1768656pfi.71.1635395514725; Wed, 27 Oct 2021 21:31:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzoZgeokAYahQc2nDjtEblSwziyI4PDuav4FpOHqbeBBic085ptrU93G/JiKkaQj9M7x4NDA== X-Received: by 2002:aa7:8111:0:b0:47b:e311:cdae with SMTP id b17-20020aa78111000000b0047be311cdaemr1768611pfi.71.1635395514280; Wed, 27 Oct 2021 21:31:54 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v2 2/5] pci: Export pci_for_each_device_under_bus*() Date: Thu, 28 Oct 2021 12:31:26 +0800 Message-Id: <20211028043129.38871-3-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211028043129.38871-1-peterx@redhat.com> References: <20211028043129.38871-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Daniel P . Berrange" , Eduardo Habkost , David Hildenbrand , Jason Wang , "Michael S . Tsirkin" , Markus Armbruster , peterx@redhat.com, Eric Auger , Alex Williamson , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1635395632350100001 Content-Type: text/plain; charset="utf-8" They're actually more commonly used than the helper without _under_bus, bec= ause most callers do have the pci bus on hand. After exporting we can switch a = lot of the call sites to use these two helpers. Reviewed-by: David Hildenbrand Reviewed-by: Eric Auger Signed-off-by: Peter Xu --- hw/i386/acpi-build.c | 5 ++--- hw/pci/pci.c | 10 +++++----- hw/pci/pcie.c | 4 +--- hw/ppc/spapr_pci.c | 12 +++++------- hw/ppc/spapr_pci_nvlink2.c | 7 +++---- hw/ppc/spapr_pci_vfio.c | 4 ++-- hw/s390x/s390-pci-bus.c | 5 ++--- hw/xen/xen_pt.c | 4 ++-- include/hw/pci/pci.h | 5 +++++ 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index 81418b7911..a76b17ed92 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -2132,8 +2132,7 @@ dmar_host_bridges(Object *obj, void *opaque) PCIBus *bus =3D PCI_HOST_BRIDGE(obj)->bus; =20 if (bus && !pci_bus_bypass_iommu(bus)) { - pci_for_each_device(bus, pci_bus_num(bus), insert_scope, - scope_blob); + pci_for_each_device_under_bus(bus, insert_scope, scope_blob); } } =20 @@ -2339,7 +2338,7 @@ ivrs_host_bridges(Object *obj, void *opaque) PCIBus *bus =3D PCI_HOST_BRIDGE(obj)->bus; =20 if (bus && !pci_bus_bypass_iommu(bus)) { - pci_for_each_device(bus, pci_bus_num(bus), insert_ivhd, ivhd_b= lob); + pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob); } } =20 diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 17e59cb3a3..4a84e478ce 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -1654,9 +1654,9 @@ static const pci_class_desc pci_class_descriptions[] = =3D { 0, NULL} }; =20 -static void pci_for_each_device_under_bus_reverse(PCIBus *bus, - pci_bus_dev_fn fn, - void *opaque) +void pci_for_each_device_under_bus_reverse(PCIBus *bus, + pci_bus_dev_fn fn, + void *opaque) { PCIDevice *d; int devfn; @@ -1679,8 +1679,8 @@ void pci_for_each_device_reverse(PCIBus *bus, int bus= _num, } } =20 -static void pci_for_each_device_under_bus(PCIBus *bus, - pci_bus_dev_fn fn, void *opaque) +void pci_for_each_device_under_bus(PCIBus *bus, + pci_bus_dev_fn fn, void *opaque) { PCIDevice *d; int devfn; diff --git a/hw/pci/pcie.c b/hw/pci/pcie.c index 6e95d82903..914a9bf3d1 100644 --- a/hw/pci/pcie.c +++ b/hw/pci/pcie.c @@ -694,9 +694,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, (!(old_slt_ctl & PCI_EXP_SLTCTL_PCC) || (old_slt_ctl & PCI_EXP_SLTCTL_PIC_OFF) !=3D PCI_EXP_SLTCTL_PIC_OFF= )) { PCIBus *sec_bus =3D pci_bridge_get_sec_bus(PCI_BRIDGE(dev)); - pci_for_each_device(sec_bus, pci_bus_num(sec_bus), - pcie_unplug_device, NULL); - + pci_for_each_device_under_bus(sec_bus, pcie_unplug_device, NULL); pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA, PCI_EXP_SLTSTA_PDS); if (dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA || diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 7430bd6314..5bfd4aa9e5 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -1317,8 +1317,7 @@ static int spapr_dt_pci_bus(SpaprPhbState *sphb, PCIB= us *bus, RESOURCE_CELLS_SIZE)); =20 assert(bus); - pci_for_each_device_reverse(bus, pci_bus_num(bus), - spapr_dt_pci_device_cb, &cbinfo); + pci_for_each_device_under_bus_reverse(bus, spapr_dt_pci_device_cb, &cb= info); if (cbinfo.err) { return cbinfo.err; } @@ -2306,8 +2305,8 @@ static void spapr_phb_pci_enumerate_bridge(PCIBus *bu= s, PCIDevice *pdev, return; } =20 - pci_for_each_device(sec_bus, pci_bus_num(sec_bus), - spapr_phb_pci_enumerate_bridge, bus_no); + pci_for_each_device_under_bus(sec_bus, spapr_phb_pci_enumerate_bridge, + bus_no); pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1); } =20 @@ -2316,9 +2315,8 @@ static void spapr_phb_pci_enumerate(SpaprPhbState *ph= b) PCIBus *bus =3D PCI_HOST_BRIDGE(phb)->bus; unsigned int bus_no =3D 0; =20 - pci_for_each_device(bus, pci_bus_num(bus), - spapr_phb_pci_enumerate_bridge, - &bus_no); + pci_for_each_device_under_bus(bus, spapr_phb_pci_enumerate_bridge, + &bus_no); =20 } =20 diff --git a/hw/ppc/spapr_pci_nvlink2.c b/hw/ppc/spapr_pci_nvlink2.c index 8ef9b40a18..7fb0cf4d04 100644 --- a/hw/ppc/spapr_pci_nvlink2.c +++ b/hw/ppc/spapr_pci_nvlink2.c @@ -164,8 +164,7 @@ static void spapr_phb_pci_collect_nvgpu(PCIBus *bus, PC= IDevice *pdev, return; } =20 - pci_for_each_device(sec_bus, pci_bus_num(sec_bus), - spapr_phb_pci_collect_nvgpu, opaque); + pci_for_each_device_under_bus(sec_bus, spapr_phb_pci_collect_nvgpu, op= aque); } =20 void spapr_phb_nvgpu_setup(SpaprPhbState *sphb, Error **errp) @@ -183,8 +182,8 @@ void spapr_phb_nvgpu_setup(SpaprPhbState *sphb, Error *= *errp) sphb->nvgpus->nv2_atsd_current =3D sphb->nv2_atsd_win_addr; =20 bus =3D PCI_HOST_BRIDGE(sphb)->bus; - pci_for_each_device(bus, pci_bus_num(bus), - spapr_phb_pci_collect_nvgpu, sphb->nvgpus); + pci_for_each_device_under_bus(bus, spapr_phb_pci_collect_nvgpu, + sphb->nvgpus); =20 if (sphb->nvgpus->err) { error_propagate(errp, sphb->nvgpus->err); diff --git a/hw/ppc/spapr_pci_vfio.c b/hw/ppc/spapr_pci_vfio.c index f3b37df8ea..2a76b4e0b5 100644 --- a/hw/ppc/spapr_pci_vfio.c +++ b/hw/ppc/spapr_pci_vfio.c @@ -164,8 +164,8 @@ static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus *b= us, =20 static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus *bus, void *opaque) { - pci_for_each_device(bus, pci_bus_num(bus), - spapr_phb_vfio_eeh_clear_dev_msix, NULL); + pci_for_each_device_under_bus(bus, spapr_phb_vfio_eeh_clear_dev_msi= x, + NULL); } =20 static void spapr_phb_vfio_eeh_pre_reset(SpaprPhbState *sphb) diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c index 6fafffb029..1b51a72838 100644 --- a/hw/s390x/s390-pci-bus.c +++ b/hw/s390x/s390-pci-bus.c @@ -1163,8 +1163,7 @@ static void s390_pci_enumerate_bridge(PCIBus *bus, PC= IDevice *pdev, } =20 /* Assign numbers to all child bridges. The last is the highest number= . */ - pci_for_each_device(sec_bus, pci_bus_num(sec_bus), - s390_pci_enumerate_bridge, s); + pci_for_each_device_under_bus(sec_bus, s390_pci_enumerate_bridge, s); pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, s->bus_no, 1); } =20 @@ -1193,7 +1192,7 @@ static void s390_pcihost_reset(DeviceState *dev) * on every system reset, we also have to reassign numbers. */ s->bus_no =3D 0; - pci_for_each_device(bus, pci_bus_num(bus), s390_pci_enumerate_bridge, = s); + pci_for_each_device_under_bus(bus, s390_pci_enumerate_bridge, s); } =20 static void s390_pcihost_class_init(ObjectClass *klass, void *data) diff --git a/hw/xen/xen_pt.c b/hw/xen/xen_pt.c index ca0a98187e..027190fa44 100644 --- a/hw/xen/xen_pt.c +++ b/hw/xen/xen_pt.c @@ -615,8 +615,8 @@ static void xen_pt_region_update(XenPCIPassthroughState= *s, } =20 args.type =3D d->io_regions[bar].type; - pci_for_each_device(pci_get_bus(d), pci_dev_bus_num(d), - xen_pt_check_bar_overlap, &args); + pci_for_each_device_under_bus(pci_get_bus(d), + xen_pt_check_bar_overlap, &args); if (args.rc) { XEN_PT_WARN(d, "Region: %d (addr: 0x%"FMT_PCIBUS ", len: 0x%"FMT_PCIBUS") is overlapped.\n", diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index 4a8740b76b..5c4016b995 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -467,6 +467,11 @@ void pci_for_each_device(PCIBus *bus, int bus_num, void pci_for_each_device_reverse(PCIBus *bus, int bus_num, pci_bus_dev_fn fn, void *opaque); +void pci_for_each_device_under_bus(PCIBus *bus, + pci_bus_dev_fn fn, void *opaque); +void pci_for_each_device_under_bus_reverse(PCIBus *bus, + pci_bus_dev_fn fn, + void *opaque); void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, pci_bus_fn end, void *parent_state); PCIDevice *pci_get_function_0(PCIDevice *pci_dev); --=20 2.32.0 From nobody Sat May 18 11:46:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1635395877637748.304071260718; Wed, 27 Oct 2021 21:37:57 -0700 (PDT) Received: from localhost ([::1]:39204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfxAq-0002fj-LU for importer@patchew.org; Thu, 28 Oct 2021 00:37:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49100) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx5C-0004Nd-Jb for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:32:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:28977) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx5A-0000TU-Ke for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:32:06 -0400 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-455-Zf3P0FPtNECbl7ORgjqlSA-1; Thu, 28 Oct 2021 00:32:02 -0400 Received: by mail-pg1-f198.google.com with SMTP id p28-20020a637f5c000000b002a3c58b5917so2646264pgn.23 for ; Wed, 27 Oct 2021 21:32:02 -0700 (PDT) Received: from localhost.localdomain ([191.101.132.60]) by smtp.gmail.com with ESMTPSA id k22sm1483074pfi.149.2021.10.27.21.31.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Oct 2021 21:32:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635395524; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pgTHIAPHXkkbULyCuKmi+XPo0/hjZBL6MtokIFprPvs=; b=ILBa41sv3eW1c+ZkP5ONgbslte7p3Rwe35r28+Di10nh2WK0lB+tGH2R43PjqCskYNxBRZ 6A1vokyphwoDihfYOBjtYYg+TbwItXzm5oDXronPgg6XNX54zVFC40kZpPVSNj+Ec+3scE fwYP4h9jHpUaj/w0g4pILGBqHbsEm+I= X-MC-Unique: Zf3P0FPtNECbl7ORgjqlSA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pgTHIAPHXkkbULyCuKmi+XPo0/hjZBL6MtokIFprPvs=; b=0iLPonlqe2VAhYWzRiysLa4encL9eAsOzeX/3mUyfgq2PrRtbR/RoC3ALIhMXglFV6 S9XPbuy1hURU1DKROEGM3FviMsZGQ8wD0QbxXqDEmG/i4hoK6jkDoyQfYeE0AF1LOdNt Pd8Dl+Fg3amMf3sAkD0MbVF8O3z3HmEfSHEYvCPXjx8gMEGQfr8nEY/4v2FKj97ncxrh hznxzx2odHyWUau2cvNF1TueDYahdm9Oqe+DDbXzlJT+czxpZIsVUhvBpXIk1alLrudy pVt7bIAlIbbiQx5s8x5dxcdiKBK04xqw6z+qSeOzgLhWutCQZUmgzxT5F9xntQcta2hC J+pQ== X-Gm-Message-State: AOAM531MyqsqaNb8Th7N0GTtxbjwgykQui5jr4N1rEnjUNdnO7r7JlJG 9YHjXxfKFwaHsiTtjG1E8P7rPP0x8JhJd3Vd6thrTglw7Pg/McNEaPGhTA1vpMVHr36XhEwejPi eHZbalc4KRWwJaB3eTi8MmsaVy6Bo3SYXMFEoxEwxi5vTFjBnyTXH3h2XeX7nui2D X-Received: by 2002:a17:90a:644d:: with SMTP id y13mr2057980pjm.10.1635395521299; Wed, 27 Oct 2021 21:32:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxi2balo4iS5FNHItyKoQlOCvZ7NFqP/tawuSh5V7bmrhkfP9tEpS7se0TnwisRipUc2HduSg== X-Received: by 2002:a17:90a:644d:: with SMTP id y13mr2057939pjm.10.1635395520974; Wed, 27 Oct 2021 21:32:00 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v2 3/5] qom: object_child_foreach_recursive_type() Date: Thu, 28 Oct 2021 12:31:27 +0800 Message-Id: <20211028043129.38871-4-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211028043129.38871-1-peterx@redhat.com> References: <20211028043129.38871-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Daniel P . Berrange" , Eduardo Habkost , David Hildenbrand , Jason Wang , "Michael S . Tsirkin" , Markus Armbruster , peterx@redhat.com, Eric Auger , Alex Williamson , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1635395878330100001 Content-Type: text/plain; charset="utf-8" Add this sister helper besides object_child_foreach_recursive() to loop over child objects only if the object can be casted to a specific type. Suggested-by: Michael S. Tsirkin Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- include/qom/object.h | 20 ++++++++++++++++++++ qom/object.c | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index faae0d841f..355277db40 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -1926,6 +1926,26 @@ int object_child_foreach(Object *obj, int (*fn)(Obje= ct *child, void *opaque), int object_child_foreach_recursive(Object *obj, int (*fn)(Object *child, void *opaque), void *opaque); + +/** + * object_child_foreach_recursive_type: + * @obj: the object whose children will be navigated + * @type: the typename string to scan + * @fn: the iterator function to be called + * @opaque: an opaque value that will be passed to the iterator + * + * This is a special version of object_child_foreach_recursive() so that we + * only call the fn() if the child can be casted to the @typename specifie= d. + * Please refer to the comments above object_child_foreach_recursive() for + * more details. + * + * Returns: The last value returned by @fn, or 0 if there is no child. + */ +int object_child_foreach_recursive_type(Object *obj, + const char *typename, + int (*fn)(Object *child, void *opa= que), + void *opaque); + /** * container_get: * @root: root of the #path, e.g., object_get_root() diff --git a/qom/object.c b/qom/object.c index 6be710bc40..d25ca09b1d 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1134,6 +1134,33 @@ int object_child_foreach_recursive(Object *obj, return do_object_child_foreach(obj, fn, opaque, true); } =20 +typedef struct { + const char *typename; + int (*fn)(Object *child, void *opaque); + void *opaque; +} ObjectTypeArgs; + +static int object_child_hook(Object *child, void *opaque) +{ + ObjectTypeArgs *args =3D opaque; + + if (object_dynamic_cast(child, args->typename)) { + return args->fn(child, args->opaque); + } + + return 0; +} + +int object_child_foreach_recursive_type(Object *obj, + const char *typename, + int (*fn)(Object *child, void *opa= que), + void *opaque) +{ + ObjectTypeArgs args =3D { .typename =3D typename, .fn =3D fn, .opaque = =3D opaque }; + + return object_child_foreach_recursive(obj, object_child_hook, &args); +} + static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) { GSList **list =3D opaque; --=20 2.32.0 From nobody Sat May 18 11:46:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16353959732325.321495703496339; Wed, 27 Oct 2021 21:39:33 -0700 (PDT) Received: from localhost ([::1]:43500 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfxCO-0005Xb-59 for importer@patchew.org; Thu, 28 Oct 2021 00:39:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49122) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx5H-0004Xg-9R for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:32:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29652) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx5F-0000UB-Dp for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:32:11 -0400 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-352-D4MxLVtRPoiAeyswa8_qNA-1; Thu, 28 Oct 2021 00:32:07 -0400 Received: by mail-pj1-f71.google.com with SMTP id r13-20020a17090a1bcd00b001a1b1747cd2so2807077pjr.9 for ; Wed, 27 Oct 2021 21:32:07 -0700 (PDT) Received: from localhost.localdomain ([191.101.132.60]) by smtp.gmail.com with ESMTPSA id k22sm1483074pfi.149.2021.10.27.21.32.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Oct 2021 21:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635395528; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=07LHrb6kZNzZ4/fu3Eq9r55HxFAxKolf5VDQaKGM4hU=; b=KJn7NR0TrZC+YJSxkbBSAM0DRiFUopWeeuEi59DmOprQtP6ZnpCjI8AhZw8o+SeK4v/3FN V6BaGLwG6fDnm2HhEEHjOGttLhLC9a/fixZe5jHh7TOYXgthaIxt23vOl7H0vz4qvARhwW Vrlm1J9Ln2ew4RVdzwuz14w6l7Moq3I= X-MC-Unique: D4MxLVtRPoiAeyswa8_qNA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=07LHrb6kZNzZ4/fu3Eq9r55HxFAxKolf5VDQaKGM4hU=; b=GNQ0H/umB079U/EI89Ips029yqcu9vY9SiN3ul5F+qGqMNrl+/66/6TT0aIdcu9TmR Qq/YupuGqXExchVmiYYt9UincWCoJkDfOWWVxZnF41TevD5bLk+n9G2P3RD2Pa9sS6vb RBVwCMRlJzMow8Pm48+d25Z7wil7HH49zWuiEUOvcuUMTe/WEKAJk8dPvPenh/RCA8tR g93xgE0TzJlZl6nPC1zHIydb+Cn6oHbHt6DK8YltEVGKVEqSDsNUMA2jC3NMsXNYG9Tq btywB97MA/Oe7K27+M0yQzjW0zg2LD6JOBLcOXK2EAhUdH1uImmbxhnUyA+5tKCdvUXE Gwjg== X-Gm-Message-State: AOAM533qrHg8vWLuT3LDzuTtBhLM1ktkGIByLbxD4whiLR4+Jn2QsLE7 H4D9Dbh7wrbRCbo284sXi9/lm/S0rSONBDpzgP9Omc7ufVrUei8M1rM8oNaYIk9U3N49RJexc/U iYDe0uCWz0XIInXiTtVWJhQOu1bTHaupRi9+oPeYpS5vmX8nh5CFNpeD0/L1cnU4b X-Received: by 2002:a05:6a00:2ab:b0:47b:ee2c:62fb with SMTP id q11-20020a056a0002ab00b0047bee2c62fbmr1953569pfs.82.1635395526183; Wed, 27 Oct 2021 21:32:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxg1Wr3goSq54D+NmwpIw1kzEVekW2zuwTplXT2875a32II6O+BqTyp1pIeEBEoU60E6nSSMg== X-Received: by 2002:a05:6a00:2ab:b0:47b:ee2c:62fb with SMTP id q11-20020a056a0002ab00b0047bee2c62fbmr1953523pfs.82.1635395525669; Wed, 27 Oct 2021 21:32:05 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v2 4/5] pci: Add pci_for_each_root_bus() Date: Thu, 28 Oct 2021 12:31:28 +0800 Message-Id: <20211028043129.38871-5-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211028043129.38871-1-peterx@redhat.com> References: <20211028043129.38871-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Daniel P . Berrange" , Eduardo Habkost , David Hildenbrand , Jason Wang , "Michael S . Tsirkin" , Markus Armbruster , peterx@redhat.com, Eric Auger , Alex Williamson , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1635395975140100001 Content-Type: text/plain; charset="utf-8" Add a helper to loop over each root bus of the system, either the default r= oot bus or extended buses like pxb-pcie. There're three places that can be rewritten with the pci_for_each_root_bus() helper that we just introduced. De-dup the code. Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- hw/arm/virt-acpi-build.c | 31 +++++++++++-------------------- hw/i386/acpi-build.c | 38 ++++++++++---------------------------- hw/pci/pci.c | 26 ++++++++++++++++++++++++++ include/hw/pci/pci.h | 2 ++ 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c index 674f902652..adba51f35a 100644 --- a/hw/arm/virt-acpi-build.c +++ b/hw/arm/virt-acpi-build.c @@ -264,28 +264,20 @@ struct AcpiIortIdMapping { typedef struct AcpiIortIdMapping AcpiIortIdMapping; =20 /* Build the iort ID mapping to SMMUv3 for a given PCI host bridge */ -static int -iort_host_bridges(Object *obj, void *opaque) +static void +iort_host_bridges(PCIBus *bus, void *opaque) { - GArray *idmap_blob =3D opaque; - - if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { - PCIBus *bus =3D PCI_HOST_BRIDGE(obj)->bus; - - if (bus && !pci_bus_bypass_iommu(bus)) { - int min_bus, max_bus; + if (!pci_bus_bypass_iommu(bus)) { + int min_bus, max_bus; =20 - pci_bus_range(bus, &min_bus, &max_bus); + pci_bus_range(bus, &min_bus, &max_bus); =20 - AcpiIortIdMapping idmap =3D { - .input_base =3D min_bus << 8, - .id_count =3D (max_bus - min_bus + 1) << 8, - }; - g_array_append_val(idmap_blob, idmap); - } + AcpiIortIdMapping idmap =3D { + .input_base =3D min_bus << 8, + .id_count =3D (max_bus - min_bus + 1) << 8, + }; + g_array_append_val((GArray *)opaque, idmap); } - - return 0; } =20 static int iort_idmap_compare(gconstpointer a, gconstpointer b) @@ -320,8 +312,7 @@ build_iort(GArray *table_data, BIOSLinker *linker, Virt= MachineState *vms) if (vms->iommu =3D=3D VIRT_IOMMU_SMMUV3) { AcpiIortIdMapping next_range =3D {0}; =20 - object_child_foreach_recursive(object_get_root(), - iort_host_bridges, smmu_idmaps); + pci_for_each_root_bus(iort_host_bridges, smmu_idmaps); =20 /* Sort the smmu idmap by input_base */ g_array_sort(smmu_idmaps, iort_idmap_compare); diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index a76b17ed92..3e50acfe35 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -2123,20 +2123,12 @@ insert_scope(PCIBus *bus, PCIDevice *dev, void *opa= que) } =20 /* For a given PCI host bridge, walk and insert DMAR scope */ -static int -dmar_host_bridges(Object *obj, void *opaque) +static void +dmar_host_bridges(PCIBus *bus, void *opaque) { - GArray *scope_blob =3D opaque; - - if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { - PCIBus *bus =3D PCI_HOST_BRIDGE(obj)->bus; - - if (bus && !pci_bus_bypass_iommu(bus)) { - pci_for_each_device_under_bus(bus, insert_scope, scope_blob); - } + if (!pci_bus_bypass_iommu(bus)) { + pci_for_each_device_under_bus(bus, insert_scope, opaque); } - - return 0; } =20 /* @@ -2165,8 +2157,7 @@ build_dmar_q35(GArray *table_data, BIOSLinker *linker= , const char *oem_id, * Insert scope for each PCI bridge and endpoint device which * is attached to a bus with iommu enabled. */ - object_child_foreach_recursive(object_get_root(), - dmar_host_bridges, scope_blob); + pci_for_each_root_bus(dmar_host_bridges, scope_blob); =20 assert(iommu); if (x86_iommu_ir_supported(iommu)) { @@ -2329,20 +2320,12 @@ insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaq= ue) } =20 /* For all PCI host bridges, walk and insert IVHD entries */ -static int -ivrs_host_bridges(Object *obj, void *opaque) +static void +ivrs_host_bridges(PCIBus *bus, void *opaque) { - GArray *ivhd_blob =3D opaque; - - if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { - PCIBus *bus =3D PCI_HOST_BRIDGE(obj)->bus; - - if (bus && !pci_bus_bypass_iommu(bus)) { - pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob); - } + if (!pci_bus_bypass_iommu(bus)) { + pci_for_each_device_under_bus(bus, insert_ivhd, opaque); } - - return 0; } =20 static void @@ -2380,8 +2363,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linke= r, const char *oem_id, * blob further below. Fall back to an entry covering all devices, wh= ich * is sufficient when no aliases are present. */ - object_child_foreach_recursive(object_get_root(), - ivrs_host_bridges, ivhd_blob); + pci_for_each_root_bus(ivrs_host_bridges, ivhd_blob); =20 if (!ivhd_blob->len) { /* diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 4a84e478ce..258290f4eb 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2097,6 +2097,32 @@ void pci_for_each_bus_depth_first(PCIBus *bus, pci_b= us_ret_fn begin, } } =20 +typedef struct { + pci_bus_fn fn; + void *opaque; +} PCIRootBusArgs; + +static int pci_find_root_bus(Object *obj, void *opaque) +{ + PCIRootBusArgs *args =3D opaque; + PCIBus *bus =3D PCI_HOST_BRIDGE(obj)->bus; + + if (bus) { + args->fn(bus, args->opaque); + } + + return 0; +} + +void pci_for_each_root_bus(pci_bus_fn fn, void *opaque) +{ + PCIRootBusArgs args =3D { .fn =3D fn, .opaque =3D opaque }; + + object_child_foreach_recursive_type(object_get_root(), + TYPE_PCI_HOST_BRIDGE, + pci_find_root_bus, + &args); +} =20 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn) { diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index 5c4016b995..6813f128e0 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -474,6 +474,8 @@ void pci_for_each_device_under_bus_reverse(PCIBus *bus, void *opaque); void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, pci_bus_fn end, void *parent_state); +/* Call `fn' for each pci root bus on the system */ +void pci_for_each_root_bus(pci_bus_fn fn, void *opaque); PCIDevice *pci_get_function_0(PCIDevice *pci_dev); =20 /* Use this wrapper when specific scan order is not required. */ --=20 2.32.0 From nobody Sat May 18 11:46:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1635395837698218.27227440946126; Wed, 27 Oct 2021 21:37:17 -0700 (PDT) Received: from localhost ([::1]:37518 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mfxAC-0001UF-MD for importer@patchew.org; Thu, 28 Oct 2021 00:37:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49134) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx5O-0004ee-6b for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:32:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:42131) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mfx5M-0000UR-Dg for qemu-devel@nongnu.org; Thu, 28 Oct 2021 00:32:17 -0400 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-11-Q-ysD1aCPVCoJ64oD8NUvQ-1; Thu, 28 Oct 2021 00:32:14 -0400 Received: by mail-pf1-f197.google.com with SMTP id z2-20020aa79482000000b0047c0239f1e3so2624008pfk.22 for ; Wed, 27 Oct 2021 21:32:14 -0700 (PDT) Received: from localhost.localdomain ([191.101.132.60]) by smtp.gmail.com with ESMTPSA id k22sm1483074pfi.149.2021.10.27.21.32.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Oct 2021 21:32:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635395535; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YH8yplBAnObRfPP9y1l2ATyeHY/CS+/4q2iAtlXiHIM=; b=jCU/HqMsoVh28rcRqghrst9oOENaYqJ/VqsOZOa8on1kQY9UiXn5lz3rdNvyjsWSfBpulW Kk+QLsPLK1iDAUPV0PpYMvdgoc2fKyxNpxNDCSqt8xmAL8r/bYT6NrBj1KY4SWtwYrfYul DtlfunPmv3uVnwdQlnVt1K0bEyBs9ok= X-MC-Unique: Q-ysD1aCPVCoJ64oD8NUvQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YH8yplBAnObRfPP9y1l2ATyeHY/CS+/4q2iAtlXiHIM=; b=ydDUMNJ0i4qa11PCCwyZPjroJCqdD3M0GMd9Ajsz810x+9qXi81QGw4uIqqA8nu/EN cplV42cuuyUqGrOWq5HbGSg3kWNzsqHQ0yJMlKII9jvRkja0etONkV3nuAv5pZKNtHaV yRNGjQAouva83TgoVe49EKtHt4vmvJaXON0mggfA4j6XJB1Rr6Wzh/zoKr+iaV8/yIFJ xtVZRTtfIqYkeJ62yi3kDctZjc8OZWwM80t72ytcV7kwh7wJiq6kf9qYm9MFvY1zSG2j TFADO2Fk8kw3Rrpuhs15ciVmFsTY2xFnLj+uvSe6mxVxA1csdCI7LpVEbhF2LA3Z0e0l vgAQ== X-Gm-Message-State: AOAM532SDrXiP2ZKmXXkE6ieR4sU70kDkFYXk2OksvLkpcIrWw9C36HJ T5QAvbOqkMwYUiWpQJqQm1k3rDi+lLDCntHDmoZomzdSsgW/MNmqa3FWCWcsgqgyJdcp+bhqtPK +pOSqIvmbLJEV9/6G70WexYrcddUvgzF2F2+qEkE1w1M6NF4wPhMf4jlZdBmVQr5G X-Received: by 2002:a17:902:6a86:b0:13f:f048:9778 with SMTP id n6-20020a1709026a8600b0013ff0489778mr1586854plk.27.1635395533165; Wed, 27 Oct 2021 21:32:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCwRBEyPBgLMHB7eYTygG8lgMQQvMuJn4SiomcgiSq4JKwz0xnaoLsqqRZzQhJxENLA+wlIQ== X-Received: by 2002:a17:902:6a86:b0:13f:f048:9778 with SMTP id n6-20020a1709026a8600b0013ff0489778mr1586807plk.27.1635395532696; Wed, 27 Oct 2021 21:32:12 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v2 5/5] pc/q35: Add pre-plug hook for x86-iommu Date: Thu, 28 Oct 2021 12:31:29 +0800 Message-Id: <20211028043129.38871-6-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211028043129.38871-1-peterx@redhat.com> References: <20211028043129.38871-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Daniel P . Berrange" , Eduardo Habkost , David Hildenbrand , Jason Wang , "Michael S . Tsirkin" , Markus Armbruster , peterx@redhat.com, Eric Auger , Alex Williamson , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1635395838231100001 Content-Type: text/plain; charset="utf-8" Add a pre-plug hook for x86-iommu, so that we can detect vfio-pci devices before realizing the vIOMMU device. When the guest contains both the x86 vIOMMU and vfio-pci devices, the user needs to specify the x86 vIOMMU before the vfio-pci devices. The reason is, vfio_realize() calls pci_device_iommu_address_space() to fetch the correct = dma address space for the device, while that API can only work right after the vIOMMU device initialized first. For example, the iommu_fn() that is used in pci_device_iommu_address_space(= ) is only setup in realize() of the vIOMMU devices. For a long time we have had libvirt making sure that the ordering is correc= t, however from qemu side we never fail a guest from booting even if the order= ing is specified wrongly. When the order is wrong, the guest will encounter misterious error when operating on the vfio-pci device because in QEMU we'll still assume the vfio-pci devices are put into the default DMA domain (whic= h is normally the direct GPA mapping), so e.g. the DMAs will never go right. This patch fails the guest from booting when we detected such errornous cmd= line specified, then the guest at least won't encounter weird device behavior af= ter booted. The error message will also help the user to know how to fix the i= ssue. Cc: Alex Williamson Suggested-by: Igor Mammedov Signed-off-by: Peter Xu Acked-by: David Hildenbrand --- hw/i386/pc.c | 4 ++++ hw/i386/x86-iommu.c | 14 ++++++++++++++ include/hw/i386/x86-iommu.h | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 86223acfd3..b70a04011e 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -81,6 +81,7 @@ #include "hw/core/cpu.h" #include "hw/usb.h" #include "hw/i386/intel_iommu.h" +#include "hw/i386/x86-iommu.h" #include "hw/net/ne2000-isa.h" #include "standard-headers/asm-x86/bootparam.h" #include "hw/virtio/virtio-pmem-pci.h" @@ -1327,6 +1328,8 @@ static void pc_machine_device_pre_plug_cb(HotplugHand= ler *hotplug_dev, pc_memory_pre_plug(hotplug_dev, dev, errp); } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { x86_cpu_pre_plug(hotplug_dev, dev, errp); + } else if (object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE)) { + x86_iommu_pre_plug(X86_IOMMU_DEVICE(dev), errp); } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) || object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { pc_virtio_md_pci_pre_plug(hotplug_dev, dev, errp); @@ -1383,6 +1386,7 @@ static HotplugHandler *pc_get_hotplug_handler(Machine= State *machine, { if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) || object_dynamic_cast(OBJECT(dev), TYPE_CPU) || + object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) || object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) || object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { return HOTPLUG_HANDLER(machine); diff --git a/hw/i386/x86-iommu.c b/hw/i386/x86-iommu.c index 86ad03972e..c9ee9041a3 100644 --- a/hw/i386/x86-iommu.c +++ b/hw/i386/x86-iommu.c @@ -22,6 +22,7 @@ #include "hw/i386/x86-iommu.h" #include "hw/qdev-properties.h" #include "hw/i386/pc.h" +#include "hw/vfio/pci.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "trace.h" @@ -103,6 +104,19 @@ IommuType x86_iommu_get_type(void) return x86_iommu_default->type; } =20 +void x86_iommu_pre_plug(X86IOMMUState *iommu, Error **errp) +{ + bool ambiguous =3D false; + Object *object; + + object =3D object_resolve_path_type("", TYPE_VFIO_PCI, &ambiguous); + if (object || ambiguous) { + /* There're one or more vfio-pci devices detected */ + error_setg(errp, "Please specify all the vfio-pci devices to be af= ter " + "the vIOMMU device"); + } +} + static void x86_iommu_realize(DeviceState *dev, Error **errp) { X86IOMMUState *x86_iommu =3D X86_IOMMU_DEVICE(dev); diff --git a/include/hw/i386/x86-iommu.h b/include/hw/i386/x86-iommu.h index 9de92d33a1..e8b6c293e0 100644 --- a/include/hw/i386/x86-iommu.h +++ b/include/hw/i386/x86-iommu.h @@ -172,4 +172,12 @@ void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bo= ol global, * @out: Output MSI message */ void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *out); + +/** + * x86_iommu_pre_plug: called before plugging the iommu device + * @X86IOMMUState: the pointer to x86 iommu state + * @errp: the double pointer to Error, set if we want to fail the plug + */ +void x86_iommu_pre_plug(X86IOMMUState *iommu, Error **errp); + #endif --=20 2.32.0