From nobody Mon Feb 9 21:21:38 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1599661455; cv=none; d=zohomail.com; s=zohoarc; b=kyEQ8i+wwPZPwdgHSzESoGS1QWe1q8iBBR6Y8mEyiA5xYMJmyrv0JjWSS1qsMpBmvv0CrUg7WnF6kwYh/slL6XeV6Wyva6XcgCHyUeaxaRHWUJVbVFxYe/RnzLcZLvbBpaxzPRNa4aUNVW8tH3zbtYpag98KGjTpUSBHxlL6EBk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599661455; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=PSnNVE3NZlz5gegrFx1Hb/kijSce9zFnCVUjF03h6sQ=; b=I4rg8npmnPPtZkay6c35gKTqNeGbrPr14wUrdMnHse9o4PUaOmzdDF6oFa3EcERX/zqgXxxy+V9GaiQo7eVey08oJR0WERebdejivIXD25xTJophgLQRjdt/p//YMQ8krZts7koynfwLF2rCb7JMhHskabKsAxabCqKVRSWfDT8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1599661455641394.057195903639; Wed, 9 Sep 2020 07:24:15 -0700 (PDT) Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-111-rWjKstrJPdykbSE2lLvOdw-1; Wed, 09 Sep 2020 10:24:12 -0400 Received: by mail-wr1-f71.google.com with SMTP id l9so1016520wrq.20 for ; Wed, 09 Sep 2020 07:24:12 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.redhat.com (65.red-83-57-170.dynamicip.rima-tde.net. [83.57.170.65]) by smtp.gmail.com with ESMTPSA id a85sm4404267wmd.26.2020.09.09.07.24.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2020 07:24:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599661454; 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=PSnNVE3NZlz5gegrFx1Hb/kijSce9zFnCVUjF03h6sQ=; b=LWaN4ZSMmSxnj6CBWkNHsBxSg+zmAGGlVdqU76LPG9w1JU/9WMdW5O4wcrE4NahVN4ec5s mkYRcYlrn1qx6I95IpIhEUbZ8Cq2M2dmTJCFRfjHFyG2/S5mZP5shV15vN9BmlokrmOi70 hhZhsEXc6BT3yc6735Ve+JL0fCLIxhM= X-MC-Unique: rWjKstrJPdykbSE2lLvOdw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PSnNVE3NZlz5gegrFx1Hb/kijSce9zFnCVUjF03h6sQ=; b=SV4uR6s7KzI6Na5ii4Fx1UUDb7MMxRyUFcxPD0l8rDSrjo+cku+VEs2DI4vdEimxJf c1+pajsbmsheVIjsnYwA7YPEf8CFihqKf2715GU9qAu03MND7dxUNULNbPGLa0wo0fZ5 C0LenUg1jmn/uCe+2KCKRfAcsnOAG+Gy9pw0ojI9RNHU6rEHv/WT/d2YiHmFveFjT8Xi aAGSWUVtREyfQ9Xiq7A8Y+ytl7yxJbPR03DiY8nX6aCk9ExCH1stgdRL3bvxu99YLjsv IAWASeHn+iwN3pcsCtN0AL0OixgW5vvZXpPlEi3CXAHeHuDQ9ck6byQDRztQS02Xb+k1 +/cA== X-Gm-Message-State: AOAM531ZCK3xmONqMOEhcvOX67GM5Ilrhfu5e1ITSconn6YzXyEeqJMy 5oqrrdSpn0uecq9eud3ZxpFPAMfsw2cOhyMHOU6ghWTu86ChuOUKuCCkr3X30XUICYNBaoE5mL5 WTp6vHglUSMqX8A== X-Received: by 2002:a1c:2d85:: with SMTP id t127mr3838704wmt.22.1599661451560; Wed, 09 Sep 2020 07:24:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKp0ifME2A9FwhcPA5Sliz011v1Vm6kGX+4Y22yWYAqMvtENk9OK3lhqZ+Thfd3z0oar9OSg== X-Received: by 2002:a1c:2d85:: with SMTP id t127mr3838681wmt.22.1599661451303; Wed, 09 Sep 2020 07:24:11 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Max Reitz , qemu-block@nongnu.org, Kevin Wolf , Fam Zheng , Alex Williamson , Stefan Hajnoczi , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v6 3/4] util/vfio-helpers: Introduce qemu_vfio_pci_init_msix_irqs() Date: Wed, 9 Sep 2020 16:23:53 +0200 Message-Id: <20200909142354.334859-4-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200909142354.334859-1-philmd@redhat.com> References: <20200909142354.334859-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0.003 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8"; text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) qemu_vfio_pci_init_irq() allows us to initialize any type of IRQ, but only one. Introduce qemu_vfio_pci_init_msix_irqs() which is specific to MSIX IRQ type, and allow us to use multiple IRQs (thus passing multiple eventfd notifiers). Reviewed-by: Fam Zheng Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- include/qemu/vfio-helpers.h | 6 +++- util/vfio-helpers.c | 65 ++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/include/qemu/vfio-helpers.h b/include/qemu/vfio-helpers.h index 1f057c2b9e4..092b7b243f9 100644 --- a/include/qemu/vfio-helpers.h +++ b/include/qemu/vfio-helpers.h @@ -1,11 +1,13 @@ /* * QEMU VFIO helpers * - * Copyright 2016 - 2018 Red Hat, Inc. + * Copyright 2016 - 2020 Red Hat, Inc. * * Authors: * Fam Zheng + * Philippe Mathieu-Daud=C3=A9 * + * SPDX-License-Identifier: GPL-2.0-or-later * This work is licensed under the terms of the GNU GPL, version 2 or late= r. * See the COPYING file in the top-level directory. */ @@ -28,5 +30,7 @@ void qemu_vfio_pci_unmap_bar(QEMUVFIOState *s, int index,= void *bar, uint64_t offset, uint64_t size); int qemu_vfio_pci_init_irq(QEMUVFIOState *s, EventNotifier *e, int irq_type, Error **errp); +int qemu_vfio_pci_init_msix_irqs(QEMUVFIOState *s, EventNotifier *e, + unsigned *irq_count, Error **errp); =20 #endif diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 4cc5697dcb2..3b1b81caf8b 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -1,11 +1,13 @@ /* * VFIO utility * - * Copyright 2016 - 2018 Red Hat, Inc. + * Copyright 2016 - 2020 Red Hat, Inc. * * Authors: * Fam Zheng + * Philippe Mathieu-Daud=C3=A9 * + * SPDX-License-Identifier: GPL-2.0-or-later * This work is licensed under the terms of the GNU GPL, version 2 or late= r. * See the COPYING file in the top-level directory. */ @@ -216,6 +218,67 @@ int qemu_vfio_pci_init_irq(QEMUVFIOState *s, EventNoti= fier *e, return 0; } =20 +/** + * Initialize device MSIX IRQs and register event notifiers. + * @irq_count: pointer to number of MSIX IRQs to initialize + * @notifier: Array of @irq_count notifiers (each corresponding to a MSIX = IRQ) + + * If the number of IRQs requested exceeds the available on the device, + * store the number of available IRQs in @irq_count and return -EOVERFLOW. + */ +int qemu_vfio_pci_init_msix_irqs(QEMUVFIOState *s, EventNotifier *notifier, + unsigned *irq_count, Error **errp) +{ + int r; + size_t irq_set_size; + struct vfio_irq_set *irq_set; + struct vfio_irq_info irq_info =3D { + .argsz =3D sizeof(irq_info), + .index =3D VFIO_PCI_MSIX_IRQ_INDEX + }; + + if (ioctl(s->device, VFIO_DEVICE_GET_IRQ_INFO, &irq_info)) { + error_setg_errno(errp, errno, "Failed to get device interrupt info= "); + return -errno; + } + if (irq_info.count < *irq_count) { + error_setg(errp, "Not enough device interrupts available"); + *irq_count =3D irq_info.count; + return -EOVERFLOW; + } + if (!(irq_info.flags & VFIO_IRQ_INFO_EVENTFD)) { + error_setg(errp, "Device interrupt doesn't support eventfd"); + return -EINVAL; + } + + irq_set_size =3D sizeof(*irq_set) + *irq_count * sizeof(int32_t); + irq_set =3D g_malloc0(irq_set_size); + + /* Get to a known IRQ state */ + *irq_set =3D (struct vfio_irq_set) { + .argsz =3D irq_set_size, + .flags =3D VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER, + .index =3D irq_info.index, + .start =3D 0, + .count =3D *irq_count, + }; + + for (unsigned i =3D 0; i < *irq_count; i++) { + ((int32_t *)&irq_set->data)[i] =3D event_notifier_get_fd(¬ifier= [i]); + } + r =3D ioctl(s->device, VFIO_DEVICE_SET_IRQS, irq_set); + g_free(irq_set); + if (r <=3D 0) { + error_setg_errno(errp, errno, "Failed to setup device interrupts"); + return -errno; + } else if (r < *irq_count) { + error_setg(errp, "Not enough device interrupts available"); + *irq_count =3D r; + return -EOVERFLOW; + } + return 0; +} + static int qemu_vfio_pci_read_config(QEMUVFIOState *s, void *buf, int size, int ofs) { --=20 2.26.2