From nobody Wed Nov 27 15:33:13 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; 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=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1698317443; cv=none; d=zohomail.com; s=zohoarc; b=Z3P8Iz/ZXBUXPJR9oh5Do0KKqyRe1YuPc5OmXUd4cHXLS9zenfaAUe1CzQb3qByP3W6hTfPz8/4fRQws9BRX7uuAlfKl9VkOmiMs9ngnz2tDoRUI8eCLplhwhAL7wqUm+H8ADVzGJX6jqQ5XBDgHFZJyhBgIIKklEtlKbGloBO8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1698317443; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gCY7pFiMd89WWOems/XmOuECBdmBgsyuhQd2MD5ekE4=; b=ZjuPKvfaDwwPRaLuDWMfgsMDxze83WhA44JnXthNb9FJoRG78+fST+TAqymnlkiVzbGtNbeZxAbToRQs+MytQtxI5vJo66qZbidVzUyZ0Mtw1gwsH+cCMiadlbLjOtxPa7YnRTMbr4Rhq2IfdzNFOzHps8d3bOeB+Ug0ds+UHDQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1698317443066786.6719577432088; Thu, 26 Oct 2023 03:50:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qvxvB-0003v7-Jq; Thu, 26 Oct 2023 06:49:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qvxv4-0003ib-MB for qemu-devel@nongnu.org; Thu, 26 Oct 2023 06:48:57 -0400 Received: from mgamail.intel.com ([134.134.136.126]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qvxv1-0001PR-4x for qemu-devel@nongnu.org; Thu, 26 Oct 2023 06:48:54 -0400 Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 03:48:03 -0700 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.147]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 03:47:49 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698317331; x=1729853331; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4fpM8djcG6yzw2/d0EJVHfGLIQl+ddsysHN+ZtJ/98U=; b=mD4S6y2LbuKu/JO8az3vixfI4aX+gol3MdhFbIsKhmq9UOMTtha6Qf2F WcZoO7Ry9feod91+TKVXzB5PUsrtT8vjeZCV+Xqt8c5Tqahk1Dh5DDnwA sW3nfxnaBadbVIfyk/641jV8mdMxV7KPFLwfKHx7NrETCBdmVX9LZ5z3P uKIAy5hppI9PrH4aRia1w4MtOYmpo47q7sDOLJPRiq+wdsz68yyPI5OPT nC2XZFzd7gSy6KKyDEj8WTAA0g2acIKw+WpOh+YOfZ61vZHqCRDqQ4R2q Zq4ImzWJTDGwDs43u3Xfj+H5WGQB/qt+YB3SAnrjASO5/YiU+4zj40iaY w==; X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="372563726" X-IronPort-AV: E=Sophos;i="6.03,253,1694761200"; d="scan'208";a="372563726" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,253,1694761200"; d="scan'208";a="463817" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, kevin.tian@intel.com, yi.l.liu@intel.com, yi.y.sun@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v3 28/37] vfio/iommufd: Add support for iova_ranges Date: Thu, 26 Oct 2023 18:30:55 +0800 Message-Id: <20231026103104.1686921-29-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231026103104.1686921-1-zhenzhong.duan@intel.com> References: <20231026103104.1686921-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 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=134.134.136.126; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, 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.29 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1698317445279100003 Content-Type: text/plain; charset="utf-8" Some vIOMMU such as virtio-iommu use iova ranges from host side to setup reserved ranges for passthrough device, so that guest will not use an iova range beyond host support. Use an uAPI of IOMMUFD to get iova ranges of host side and pass to vIOMMU just like the legacy backend. Signed-off-by: Zhenzhong Duan --- hw/vfio/iommufd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index c1daaf1c39..18a09d7f5a 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -348,6 +348,52 @@ static int vfio_ram_block_discard_disable(bool state) return ram_block_uncoordinated_discard_disable(state); } =20 +static int vfio_get_info_iova_range(VFIOIOMMUFDContainer *container, + uint32_t ioas_id) +{ + VFIOContainerBase *bcontainer =3D &container->bcontainer; + struct iommu_ioas_iova_ranges *info; + struct iommu_iova_range *iova_ranges; + int ret, sz, fd =3D container->be->fd; + + info =3D g_malloc0(sizeof(*info)); + info->size =3D sizeof(*info); + info->ioas_id =3D ioas_id; + + ret =3D ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info); + if (ret && errno !=3D EMSGSIZE) { + goto error; + } + + sz =3D info->num_iovas * sizeof(struct iommu_iova_range); + info =3D g_realloc(info, sizeof(*info) + sz); + info->allowed_iovas =3D (uint64_t)(info + 1); + + ret =3D ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info); + if (ret) { + goto error; + } + + iova_ranges =3D (struct iommu_iova_range *)info->allowed_iovas; + + for (int i =3D 0; i < info->num_iovas; i++) { + Range *range =3D g_new(Range, 1); + + range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); + bcontainer->iova_ranges =3D + range_list_insert(bcontainer->iova_ranges, range); + } + + g_free(info); + return 0; + +error: + ret =3D -errno; + g_free(info); + error_report("vfio/iommufd: Cannot get iova ranges: %m"); + return ret; +} + static int iommufd_attach_device(char *name, VFIODevice *vbasedev, AddressSpace *as, Error **errp) { @@ -425,6 +471,7 @@ static int iommufd_attach_device(char *name, VFIODevice= *vbasedev, } =20 bcontainer->pgsizes =3D qemu_real_host_page_size(); + vfio_get_info_iova_range(container, ioas_id); =20 bcontainer->listener =3D vfio_memory_listener; memory_listener_register(&bcontainer->listener, bcontainer->space->as); --=20 2.34.1