From nobody Mon Apr 29 07:55:34 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 ARC-Seal: i=1; a=rsa-sha256; t=1560462494; cv=none; d=zoho.com; s=zohoarc; b=mQH1p/cWM03O10fqSLoupK8XOUpbzU5WmCdmIOIBvhtK5T9x4WK4L4sMJdJz7ik1kmXOdZ1+pJ1dWfoSPy0xB46bgmH4sCu8MtAbm6cc4uAtRdge93OREzIShAbpYVVygCdJxXOYEmibDuFguSzuR4wGGy4GWk6yrXwtaP+OPYo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560462494; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=xq/T4jHNbRLoBQ3HP8Z0nWl1jnIQhSZHVPqsdAYZ2IA=; b=NkprMK4fEkPRSQXLmMKmg+ZiOG1rxN4Kel6pVv+xkcLoiIRd3z6arOKCaSJfeqDG7Uvo/UyfRYtoFOLDsdU2gzYtRPc1jZ2BlAaU5qQeJBA3xpLrZnuqb7TmtDVr/jB9yKeg0Ut0BV22bsnM4vuZA0ORdsPSdjeAcL+bc7ceehI= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1560462494887343.1656248437398; Thu, 13 Jun 2019 14:48:14 -0700 (PDT) Received: from localhost ([::1]:45916 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbXZk-00037q-EQ for importer@patchew.org; Thu, 13 Jun 2019 17:48:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51514) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbXMK-0000Gx-Pd for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hbXMH-00031y-O1 for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37258) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hbXMG-0002tF-Ul for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:09 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 25B1A67F for ; Thu, 13 Jun 2019 21:33:48 +0000 (UTC) Received: from gimli.home (ovpn-116-190.phx2.redhat.com [10.3.116.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id E9EA31001B06; Thu, 13 Jun 2019 21:33:45 +0000 (UTC) From: Alex Williamson To: qemu-devel@nongnu.org Date: Thu, 13 Jun 2019 15:33:45 -0600 Message-ID: <156046162538.26543.2016700244609366109.stgit@gimli.home> In-Reply-To: <156046151566.26543.17274661862206856605.stgit@gimli.home> References: <156046151566.26543.17274661862206856605.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 13 Jun 2019 21:33:48 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 1/3] vfio/pci: Hide Resizable BAR capability 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" The resizable BAR capability is currently exposed read-only from the kernel and we don't yet implement a protocol for virtualizing it to the VM. Exposing it to the guest read-only introduces poor behavior as the guest has no reason to test that a control register write is accepted by the hardware. This can lead to cases where the guest OS assumes the BAR has been resized, but it hasn't. This has been observed when assigning AMD Vega GPUs. Note, this does not preclude future enablement of resizable BARs, but it's currently incorrect to expose this capability as read-only, so better to not expose it at all. Reported-by: James Courtier-Dutton Tested-by: James Courtier-Dutton Signed-off-by: Alex Williamson --- hw/vfio/pci.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 4a04f795162b..48f4e19a02cf 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2119,6 +2119,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev) case 0: /* kernel masked capability */ case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */ case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization= */ + case PCI_EXT_CAP_ID_REBAR: /* Can't expose read-only */ trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, ne= xt); break; default: From nobody Mon Apr 29 07:55:34 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 ARC-Seal: i=1; a=rsa-sha256; t=1560462394; cv=none; d=zoho.com; s=zohoarc; b=ZOzsap8tkfcJ064q94vJ0GDIVnV2Bepnh6UoFYd1suV2TMeaEDRvDp3iWmqCSE61rvTmIDGt3pgJ+hstPSYlb8b3u1ICMWmDd4QacsbFtY9muwiJlGmjd5fl/DKrFdtYb29rXyWooaB/iWP+89TuR/P/Q3xzW7LhY8nOcJOFa6Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560462394; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=BWY0YT1Rw/uPNjsr+C8qYX5QfuAdVvPdPJ34dqTo7dw=; b=XSm5+1dChLb0KfQGyNRMiqwJ1ibkEqcBVgDxLAxdgnUDtOLhEuOK213S0dccoidbG6zvJBP6ZCaXhVqvlrPteg6MEnHf2hWlajSoBAMVjcOZtVQ/iGi4Cy4sayp1E9Z8krV8doaH7Np/HyfxeD+PK3Oyy83GRWwYc9Pc+AOBvLg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1560462394237609.157741043752; Thu, 13 Jun 2019 14:46:34 -0700 (PDT) Received: from localhost ([::1]:45910 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbXYD-00023O-TJ for importer@patchew.org; Thu, 13 Jun 2019 17:46:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51579) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbXMa-0000Ix-Va for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hbXMO-00035N-Ns for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37668) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hbXMM-0002w6-1r for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:14 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D9F133082A24 for ; Thu, 13 Jun 2019 21:33:55 +0000 (UTC) Received: from gimli.home (ovpn-116-190.phx2.redhat.com [10.3.116.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB0C24E6B8; Thu, 13 Jun 2019 21:33:53 +0000 (UTC) From: Alex Williamson To: qemu-devel@nongnu.org Date: Thu, 13 Jun 2019 15:33:53 -0600 Message-ID: <156046163320.26543.15970819423038130751.stgit@gimli.home> In-Reply-To: <156046151566.26543.17274661862206856605.stgit@gimli.home> References: <156046151566.26543.17274661862206856605.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Thu, 13 Jun 2019 21:33:55 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 2/3] vfio/pci: Allow MSI-X relocation to fixup bogus PBA 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" The MSI-X relocation code can sometimes be used to work around bogus MSI-X capabilities, but this test for whether the PBA is outside of the specified BAR causes the device to error before we can apply a relocation. Let it proceed if we intend to relocate MSI-X anyway. Signed-off-by: Alex Williamson --- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 48f4e19a02cf..6520c05dee92 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1533,7 +1533,7 @@ static void vfio_msix_early_setup(VFIOPCIDevice *vdev= , Error **errp) if (vdev->vendor_id =3D=3D PCI_VENDOR_ID_CHELSIO && (vdev->device_id & 0xff00) =3D=3D 0x5800) { msix->pba_offset =3D 0x1000; - } else { + } else if (vdev->msix_relo =3D=3D OFF_AUTOPCIBAR_OFF) { error_setg(errp, "hardware reports invalid configuration, " "MSIX PBA outside of specified BAR"); g_free(msix); From nobody Mon Apr 29 07:55:34 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 ARC-Seal: i=1; a=rsa-sha256; t=1560462692; cv=none; d=zoho.com; s=zohoarc; b=f9KDz5V+uACHCbnRsICcoqE06dMIvDuhg7woIe8Zg9LjtuGJUBRQyvBJvADpdOh9dSLsWNDUy57moOBT9LqTnpY9NEUjUXprfNx8Pk8xFFPND1hPDXO0qJ+rUip0JyeFZOIfFlWjQS9CHlDt1RRKnlmW3r9pRekol2kRCLDx7ZY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560462692; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=JHXFcI35RXeNqb/lluZOnW9SYmCY+cwL+BpRB8Du6NQ=; b=GkXAPtD18ZpSPh9A8KyhjFtNGl0uYOnTfe4CyGk9awuuhoSlGBInPjm5jEq0YbRNZt15ImhT1LGefqwPfomsVhFzQCGi2Wtl5bjWBClMk5edJ5GHR6zFw7wnAElGLrJROd1NGV3H5yeQNM4ZEnJafh0ocEbKVQmc3vhFTcyaBQ4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1560462692729873.1987381935743; Thu, 13 Jun 2019 14:51:32 -0700 (PDT) Received: from localhost ([::1]:45936 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbXd5-0006Tf-Ej for importer@patchew.org; Thu, 13 Jun 2019 17:51:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51591) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbXMh-0000JH-1A for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hbXMO-00035W-TB for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49878) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hbXMM-0002yg-1k for qemu-devel@nongnu.org; Thu, 13 Jun 2019 17:34:15 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A0807C057EC6 for ; Thu, 13 Jun 2019 21:34:01 +0000 (UTC) Received: from gimli.home (ovpn-116-190.phx2.redhat.com [10.3.116.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 573C91001B06; Thu, 13 Jun 2019 21:34:01 +0000 (UTC) From: Alex Williamson To: qemu-devel@nongnu.org Date: Thu, 13 Jun 2019 15:34:01 -0600 Message-ID: <156046164094.26543.10016703921328261988.stgit@gimli.home> In-Reply-To: <156046151566.26543.17274661862206856605.stgit@gimli.home> References: <156046151566.26543.17274661862206856605.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 13 Jun 2019 21:34:01 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 3/3] vfio/common: Introduce vfio_set_irq_signaling helper 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Eric Auger The code used to assign an interrupt index/subindex to an eventfd is duplicated many times. Let's introduce an helper that allows to set/unset the signaling for an ACTION_TRIGGER, ACTION_MASK or ACTION_UNMASK action. In the error message, we now use errno in case of any VFIO_DEVICE_SET_IRQS ioctl failure. Signed-off-by: Eric Auger Reviewed-by: Cornelia Huck Reviewed-by: Li Qiang Signed-off-by: Alex Williamson --- hw/vfio/common.c | 78 +++++++++++++++ hw/vfio/pci.c | 217 ++++++++++---------------------------= ---- hw/vfio/platform.c | 68 ++++--------- include/hw/vfio/vfio-common.h | 2=20 4 files changed, 154 insertions(+), 211 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 4374cc6176a2..a859298fdad9 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -95,6 +95,84 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int= index) ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); } =20 +static inline const char *action_to_str(int action) +{ + switch (action) { + case VFIO_IRQ_SET_ACTION_MASK: + return "MASK"; + case VFIO_IRQ_SET_ACTION_UNMASK: + return "UNMASK"; + case VFIO_IRQ_SET_ACTION_TRIGGER: + return "TRIGGER"; + default: + return "UNKNOWN ACTION"; + } +} + +static const char *index_to_str(VFIODevice *vbasedev, int index) +{ + if (vbasedev->type !=3D VFIO_DEVICE_TYPE_PCI) { + return NULL; + } + + switch (index) { + case VFIO_PCI_INTX_IRQ_INDEX: + return "INTX"; + case VFIO_PCI_MSI_IRQ_INDEX: + return "MSI"; + case VFIO_PCI_MSIX_IRQ_INDEX: + return "MSIX"; + case VFIO_PCI_ERR_IRQ_INDEX: + return "ERR"; + case VFIO_PCI_REQ_IRQ_INDEX: + return "REQ"; + default: + return NULL; + } +} + +int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, + int action, int fd, Error **errp) +{ + struct vfio_irq_set *irq_set; + int argsz, ret =3D 0; + const char *name; + int32_t *pfd; + + argsz =3D sizeof(*irq_set) + sizeof(*pfd); + + irq_set =3D g_malloc0(argsz); + irq_set->argsz =3D argsz; + irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | action; + irq_set->index =3D index; + irq_set->start =3D subindex; + irq_set->count =3D 1; + pfd =3D (int32_t *)&irq_set->data; + *pfd =3D fd; + + if (ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) { + ret =3D -errno; + } + g_free(irq_set); + + if (!ret) { + return 0; + } + + error_setg_errno(errp, -ret, "VFIO_DEVICE_SET_IRQS failure"); + + name =3D index_to_str(vbasedev, index); + if (name) { + error_prepend(errp, "%s-%d: ", name, subindex); + } else { + error_prepend(errp, "index %d-%d: ", index, subindex); + } + error_prepend(errp, + "Failed to %s %s eventfd signaling for interrupt ", + fd < 0 ? "tear down" : "set up", action_to_str(action)); + return ret; +} + /* * IO Port/MMIO - Beware of the endians, VFIO is always little endian */ diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 6520c05dee92..ce3fe96efe2c 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -114,9 +114,7 @@ static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, E= rror **errp) .gsi =3D vdev->intx.route.irq, .flags =3D KVM_IRQFD_FLAG_RESAMPLE, }; - struct vfio_irq_set *irq_set; - int ret, argsz; - int32_t *pfd; + Error *err =3D NULL; =20 if (vdev->no_kvm_intx || !kvm_irqfds_enabled() || vdev->intx.route.mode !=3D PCI_INTX_ENABLED || @@ -144,22 +142,10 @@ static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev,= Error **errp) goto fail_irqfd; } =20 - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_UNM= ASK; - irq_set->index =3D VFIO_PCI_INTX_IRQ_INDEX; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - - *pfd =3D irqfd.resamplefd; - - ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - g_free(irq_set); - if (ret) { - error_setg_errno(errp, -ret, "failed to setup INTx unmask fd"); + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_UNMASK, + irqfd.resamplefd, &err)) { + error_propagate(errp, err); goto fail_vfio; } =20 @@ -263,10 +249,10 @@ static void vfio_intx_update(PCIDevice *pdev) static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp) { uint8_t pin =3D vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1= ); - int ret, argsz, retval =3D 0; - struct vfio_irq_set *irq_set; - int32_t *pfd; Error *err =3D NULL; + int32_t fd; + int ret; + =20 if (!pin) { return 0; @@ -293,27 +279,15 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Erro= r **errp) error_setg_errno(errp, -ret, "event_notifier_init failed"); return ret; } + fd =3D event_notifier_get_fd(&vdev->intx.interrupt); + qemu_set_fd_handler(fd, vfio_intx_interrupt, NULL, vdev); =20 - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRI= GGER; - irq_set->index =3D VFIO_PCI_INTX_IRQ_INDEX; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - - *pfd =3D event_notifier_get_fd(&vdev->intx.interrupt); - qemu_set_fd_handler(*pfd, vfio_intx_interrupt, NULL, vdev); - - ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret) { - error_setg_errno(errp, -ret, "failed to setup INTx fd"); - qemu_set_fd_handler(*pfd, NULL, NULL, vdev); + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { + error_propagate(errp, err); + qemu_set_fd_handler(fd, NULL, NULL, vdev); event_notifier_cleanup(&vdev->intx.interrupt); - retval =3D -errno; - goto cleanup; + return -errno; } =20 vfio_intx_enable_kvm(vdev, &err); @@ -324,11 +298,7 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error= **errp) vdev->interrupt =3D VFIO_INT_INTx; =20 trace_vfio_intx_enable(vdev->vbasedev.name); - -cleanup: - g_free(irq_set); - - return retval; + return 0; } =20 static void vfio_intx_disable(VFIOPCIDevice *vdev) @@ -531,31 +501,19 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, u= nsigned int nr, error_report("vfio: failed to enable vectors, %d", ret); } } else { - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index =3D VFIO_PCI_MSIX_IRQ_INDEX; - irq_set->start =3D nr; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; + Error *err =3D NULL; + int32_t fd; =20 if (vector->virq >=3D 0) { - *pfd =3D event_notifier_get_fd(&vector->kvm_interrupt); + fd =3D event_notifier_get_fd(&vector->kvm_interrupt); } else { - *pfd =3D event_notifier_get_fd(&vector->interrupt); + fd =3D event_notifier_get_fd(&vector->interrupt); } =20 - ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - g_free(irq_set); - if (ret) { - error_report("vfio: failed to modify vector, %d", ret); + if (vfio_set_irq_signaling(&vdev->vbasedev, + VFIO_PCI_MSIX_IRQ_INDEX, nr, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err= )) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); } } =20 @@ -592,26 +550,10 @@ static void vfio_msix_vector_release(PCIDevice *pdev,= unsigned int nr) * be re-asserted on unmask. Nothing to do if already using QEMU mode. */ if (vector->virq >=3D 0) { - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index =3D VFIO_PCI_MSIX_IRQ_INDEX; - irq_set->start =3D nr; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; + int32_t fd =3D event_notifier_get_fd(&vector->interrupt); =20 - *pfd =3D event_notifier_get_fd(&vector->interrupt); - - ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - - g_free(irq_set); + vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, n= r, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, NULL); } } =20 @@ -2638,10 +2580,8 @@ static void vfio_err_notifier_handler(void *opaque) */ static void vfio_register_err_notifier(VFIOPCIDevice *vdev) { - int ret; - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; + Error *err =3D NULL; + int32_t fd; =20 if (!vdev->pci_aer) { return; @@ -2653,58 +2593,30 @@ static void vfio_register_err_notifier(VFIOPCIDevic= e *vdev) return; } =20 - argsz =3D sizeof(*irq_set) + sizeof(*pfd); + fd =3D event_notifier_get_fd(&vdev->err_notifier); + qemu_set_fd_handler(fd, vfio_err_notifier_handler, NULL, vdev); =20 - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index =3D VFIO_PCI_ERR_IRQ_INDEX; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - - *pfd =3D event_notifier_get_fd(&vdev->err_notifier); - qemu_set_fd_handler(*pfd, vfio_err_notifier_handler, NULL, vdev); - - ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret) { - error_report("vfio: Failed to set up error notification"); - qemu_set_fd_handler(*pfd, NULL, NULL, vdev); + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + qemu_set_fd_handler(fd, NULL, NULL, vdev); event_notifier_cleanup(&vdev->err_notifier); vdev->pci_aer =3D false; } - g_free(irq_set); } =20 static void vfio_unregister_err_notifier(VFIOPCIDevice *vdev) { - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - int ret; + Error *err =3D NULL; =20 if (!vdev->pci_aer) { return; } =20 - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index =3D VFIO_PCI_ERR_IRQ_INDEX; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - *pfd =3D -1; - - ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret) { - error_report("vfio: Failed to de-assign error fd: %m"); + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); } - g_free(irq_set); qemu_set_fd_handler(event_notifier_get_fd(&vdev->err_notifier), NULL, NULL, vdev); event_notifier_cleanup(&vdev->err_notifier); @@ -2729,9 +2641,8 @@ static void vfio_register_req_notifier(VFIOPCIDevice = *vdev) { struct vfio_irq_info irq_info =3D { .argsz =3D sizeof(irq_info), .index =3D VFIO_PCI_REQ_IRQ_INDEX }; - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; + Error *err =3D NULL; + int32_t fd; =20 if (!(vdev->features & VFIO_FEATURE_ENABLE_REQ)) { return; @@ -2747,57 +2658,31 @@ static void vfio_register_req_notifier(VFIOPCIDevic= e *vdev) return; } =20 - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index =3D VFIO_PCI_REQ_IRQ_INDEX; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - - *pfd =3D event_notifier_get_fd(&vdev->req_notifier); - qemu_set_fd_handler(*pfd, vfio_req_notifier_handler, NULL, vdev); + fd =3D event_notifier_get_fd(&vdev->req_notifier); + qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev); =20 - if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) { - error_report("vfio: Failed to set up device request notification"); - qemu_set_fd_handler(*pfd, NULL, NULL, vdev); + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + qemu_set_fd_handler(fd, NULL, NULL, vdev); event_notifier_cleanup(&vdev->req_notifier); } else { vdev->req_enabled =3D true; } - - g_free(irq_set); } =20 static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) { - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; + Error *err =3D NULL; =20 if (!vdev->req_enabled) { return; } =20 - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index =3D VFIO_PCI_REQ_IRQ_INDEX; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - *pfd =3D -1; - - if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) { - error_report("vfio: Failed to de-assign device request fd: %m"); + if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); } - g_free(irq_set); qemu_set_fd_handler(event_notifier_get_fd(&vdev->req_notifier), NULL, NULL, vdev); event_notifier_cleanup(&vdev->req_notifier); diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c index ad2972595574..622e609fb425 100644 --- a/hw/vfio/platform.c +++ b/hw/vfio/platform.c @@ -107,26 +107,19 @@ static int vfio_set_trigger_eventfd(VFIOINTp *intp, eventfd_user_side_handler_t handler) { VFIODevice *vbasedev =3D &intp->vdev->vbasedev; - struct vfio_irq_set *irq_set; - int argsz, ret; - int32_t *pfd; - - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRI= GGER; - irq_set->index =3D intp->pin; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - *pfd =3D event_notifier_get_fd(intp->interrupt); - qemu_set_fd_handler(*pfd, (IOHandler *)handler, NULL, intp); - ret =3D ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret < 0) { - error_report("vfio: Failed to set trigger eventfd: %m"); - qemu_set_fd_handler(*pfd, NULL, NULL, NULL); - } - g_free(irq_set); + int32_t fd =3D event_notifier_get_fd(intp->interrupt); + Error *err =3D NULL; + int ret; + + qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp); + + ret =3D vfio_set_irq_signaling(vbasedev, intp->pin, 0, + VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err); + if (ret) { + error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name); + qemu_set_fd_handler(fd, NULL, NULL, NULL); + } + return ret; } =20 @@ -331,7 +324,6 @@ static void vfio_platform_eoi(VFIODevice *vbasedev) =20 static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq) { - int ret; VFIOPlatformDevice *vdev =3D VFIO_PLATFORM_DEVICE(sbdev); VFIOINTp *intp; =20 @@ -342,10 +334,7 @@ static void vfio_start_eventfd_injection(SysBusDevice = *sbdev, qemu_irq irq) } assert(intp); =20 - ret =3D vfio_set_trigger_eventfd(intp, vfio_intp_interrupt); - if (ret) { - error_report("vfio: failed to start eventfd signaling for IRQ %d: = %m", - intp->pin); + if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) { abort(); } } @@ -362,25 +351,16 @@ static void vfio_start_eventfd_injection(SysBusDevice= *sbdev, qemu_irq irq) */ static int vfio_set_resample_eventfd(VFIOINTp *intp) { + int32_t fd =3D event_notifier_get_fd(intp->unmask); VFIODevice *vbasedev =3D &intp->vdev->vbasedev; - struct vfio_irq_set *irq_set; - int argsz, ret; - int32_t *pfd; - - argsz =3D sizeof(*irq_set) + sizeof(*pfd); - irq_set =3D g_malloc0(argsz); - irq_set->argsz =3D argsz; - irq_set->flags =3D VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_UNM= ASK; - irq_set->index =3D intp->pin; - irq_set->start =3D 0; - irq_set->count =3D 1; - pfd =3D (int32_t *)&irq_set->data; - *pfd =3D event_notifier_get_fd(intp->unmask); - qemu_set_fd_handler(*pfd, NULL, NULL, NULL); - ret =3D ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set); - g_free(irq_set); - if (ret < 0) { - error_report("vfio: Failed to set resample eventfd: %m"); + Error *err =3D NULL; + int ret; + + qemu_set_fd_handler(fd, NULL, NULL, NULL); + ret =3D vfio_set_irq_signaling(vbasedev, intp->pin, 0, + VFIO_IRQ_SET_ACTION_UNMASK, fd, &err); + if (ret) { + error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name); } return ret; } @@ -436,8 +416,6 @@ static void vfio_start_irqfd_injection(SysBusDevice *sb= dev, qemu_irq irq) return; fail_vfio: kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq); - error_report("vfio: failed to start eventfd signaling for IRQ %d: %m", - intp->pin); abort(); fail_irqfd: vfio_start_eventfd_injection(sbdev, irq); diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index e05bc09794ab..a88b69b6750e 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -166,6 +166,8 @@ void vfio_put_base_device(VFIODevice *vbasedev); void vfio_disable_irqindex(VFIODevice *vbasedev, int index); void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); +int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, + int action, int fd, Error **errp); void vfio_region_write(void *opaque, hwaddr addr, uint64_t data, unsigned size); uint64_t vfio_region_read(void *opaque,