Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2700244pxb; Tue, 23 Feb 2021 13:23:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMoln+HCySvJNFLOmYjjWtPA8n/JmVPIA70UXLWBONrU5WVJOkPiahLyUH0Wi2+Mn6+Cdb X-Received: by 2002:a17:907:111b:: with SMTP id qu27mr29117669ejb.453.1614115388636; Tue, 23 Feb 2021 13:23:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614115388; cv=none; d=google.com; s=arc-20160816; b=bY5OLdjakBkJAI12cNjOD1BydZpt5SamVp3dDvb80stymaoexjqwVDCbymjrWf0/5e KS4RZFGuO77TdbbOxsMXhlU7MxxSuTaKlRt1Xsu/Kr8Uw99JBZ6slOIqlhgq2RoNQn2j J6fkkg3oNu+5ShrxKorXQhGxzbeuJlsKje7kVj2R2WOuv7Jdmte3i+E3Zg7z1lfHVljk 5SZbp7bk+1yYl4nmi8eFgeB1DIf+x+7DkFAwd2LXRcia9u7OLeM4SzcnBTMuciu39mx5 gA7lqFBdVPvBmB/zqY69uaDlYwCHdTTz2fjcwghkfXe2kLwUfLpsaj/Z7WpZ+4zsH3I3 imQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MidqRbnoCgHqjW/gvw5lx7fr10QlBFdAtVhbFpPCAK0=; b=vXe4Jq14dSYb257ZPMqB35pHm0MgfmkBGKfz+571ZXzmiXEmY8uKwTFCfOYklH4F0V 2kPZ3AOsYSS/s3Ui9K1e0UY75QqEZsDYq3tvbx+NnqVhL0zVs0pB1o7XgkPfc+oiu+N/ YomlTbJdxf1z1hTAV7FBuWIFJbTLwyX/pOIul/K4/p2bOSkZ248XelaJMa1fb7fAFUdP TDwV8I/ed4mjI/qj3/Myvif4X0BiD65ZAj3c4s8pdHIONQCagqURoICW/KrVsuphNDwV A45idJ+4GcQX+mu0giRJeCY1oydn2Taxm4w3qZdt3wE9CA6/1tByB7eXU7IIXmpHX/rL kebw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jJN91sqp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v15si16384245ede.1.2021.02.23.13.22.31; Tue, 23 Feb 2021 13:23:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jJN91sqp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234324AbhBWVMI (ORCPT + 99 others); Tue, 23 Feb 2021 16:12:08 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:22250 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234567AbhBWVJl (ORCPT ); Tue, 23 Feb 2021 16:09:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614114491; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MidqRbnoCgHqjW/gvw5lx7fr10QlBFdAtVhbFpPCAK0=; b=jJN91sqpehAw8IwKVa6DrHTH9paApeVsd20R9Kvjc2fYjlf2FudjE78UHbj6/ochIMn6YB 5LGxFFBMvPFYATCJMe0XULmq9UhHIrSEWaXw+qtxBpDr/nGRW8V9UVZBwt0d9IgY8TvJ3i X21We+Z+eynMiPNPi7hwgy5bbzBx1Bk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-571-r-l-agpQNzGR7NnqIgxgWQ-1; Tue, 23 Feb 2021 16:08:05 -0500 X-MC-Unique: r-l-agpQNzGR7NnqIgxgWQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BBB1C107ACF3; Tue, 23 Feb 2021 21:08:01 +0000 (UTC) Received: from laptop.redhat.com (ovpn-114-34.ams2.redhat.com [10.36.114.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id 434EA5D9D0; Tue, 23 Feb 2021 21:07:52 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, will@kernel.org, maz@kernel.org, robin.murphy@arm.com, joro@8bytes.org, alex.williamson@redhat.com, tn@semihalf.com, zhukeqian1@huawei.com Cc: jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, wangxingang5@huawei.com, jiangkunkun@huawei.com, jean-philippe@linaro.org, zhangfei.gao@linaro.org, zhangfei.gao@gmail.com, vivek.gautam@arm.com, shameerali.kolothum.thodi@huawei.com, yuzenghui@huawei.com, nicoleotsuka@gmail.com, lushenming@huawei.com, vsethi@nvidia.com Subject: [PATCH v12 11/13] vfio: Document nested stage control Date: Tue, 23 Feb 2021 22:06:23 +0100 Message-Id: <20210223210625.604517-12-eric.auger@redhat.com> In-Reply-To: <20210223210625.604517-1-eric.auger@redhat.com> References: <20210223210625.604517-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The VFIO API was enhanced to support nested stage control: a bunch of new iotcls, one DMA FAULT region and an associated specific IRQ. Let's document the process to follow to set up nested mode. Signed-off-by: Eric Auger --- v11 -> v12: s/VFIO_REGION_INFO_CAP_PRODUCER_FAULT/VFIO_REGION_INFO_CAP_DMA_FAULT v8 -> v9: - new names for SET_MSI_BINDING and SET_PASID_TABLE - new layout for the DMA FAULT memory region and specific IRQ v2 -> v3: - document the new fault API v1 -> v2: - use the new ioctl names - add doc related to fault handling --- Documentation/driver-api/vfio.rst | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/Documentation/driver-api/vfio.rst b/Documentation/driver-api/vfio.rst index f1a4d3c3ba0b..14e41324237d 100644 --- a/Documentation/driver-api/vfio.rst +++ b/Documentation/driver-api/vfio.rst @@ -239,6 +239,83 @@ group and can access them as follows:: /* Gratuitous device reset and go... */ ioctl(device, VFIO_DEVICE_RESET); +IOMMU Dual Stage Control +------------------------ + +Some IOMMUs support 2 stages/levels of translation. "Stage" corresponds to +the ARM terminology while "level" corresponds to Intel's VTD terminology. In +the following text we use either without distinction. + +This is useful when the guest is exposed with a virtual IOMMU and some +devices are assigned to the guest through VFIO. Then the guest OS can use +stage 1 (IOVA -> GPA), while the hypervisor uses stage 2 for VM isolation +(GPA -> HPA). + +The guest gets ownership of the stage 1 page tables and also owns stage 1 +configuration structures. The hypervisor owns the root configuration structure +(for security reason), including stage 2 configuration. This works as long +configuration structures and page table format are compatible between the +virtual IOMMU and the physical IOMMU. + +Assuming the HW supports it, this nested mode is selected by choosing the +VFIO_TYPE1_NESTING_IOMMU type through: + +ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU); + +This forces the hypervisor to use the stage 2, leaving stage 1 available for +guest usage. + +Once groups are attached to the container, the guest stage 1 translation +configuration data can be passed to VFIO by using + +ioctl(container, VFIO_IOMMU_SET_PASID_TABLE, &pasid_table_info); + +This allows to combine the guest stage 1 configuration structure along with +the hypervisor stage 2 configuration structure. Stage 1 configuration +structures are dependent on the IOMMU type. + +As the stage 1 translation is fully delegated to the HW, translation faults +encountered during the translation process need to be propagated up to +the virtualizer and re-injected into the guest. + +The userspace must be prepared to receive faults. The VFIO-PCI device +exposes one dedicated DMA FAULT region: it contains a ring buffer and +its header that allows to manage the head/tail indices. The region is +identified by the following index/subindex: +- VFIO_REGION_TYPE_NESTED/VFIO_REGION_SUBTYPE_NESTED_DMA_FAULT + +The DMA FAULT region exposes a VFIO_REGION_INFO_CAP_DMA_FAULT +region capability that allows the userspace to retrieve the ABI version +of the fault records filled by the host. + +On top of that region, the userspace can be notified whenever a fault +occurs at the physical level. It can use the VFIO_IRQ_TYPE_NESTED/ +VFIO_IRQ_SUBTYPE_DMA_FAULT specific IRQ to attach the eventfd to be +signalled. + +The ring buffer containing the fault records can be mmapped. When +the userspace consumes a fault in the queue, it should increment +the consumer index to allow new fault records to replace the used ones. + +The queue size and the entry size can be retrieved in the header. +The tail index should never overshoot the producer index as in any +other circular buffer scheme. Also it must be less than the queue size +otherwise the change fails. + +When the guest invalidates stage 1 related caches, invalidations must be +forwarded to the host through +ioctl(container, VFIO_IOMMU_CACHE_INVALIDATE, &inv_data); +Those invalidations can happen at various granularity levels, page, context, ... + +The ARM SMMU specification introduces another challenge: MSIs are translated by +both the virtual SMMU and the physical SMMU. To build a nested mapping for the +IOVA programmed into the assigned device, the guest needs to pass its IOVA/MSI +doorbell GPA binding to the host. Then the hypervisor can build a nested stage 2 +binding eventually translating into the physical MSI doorbell. + +This is achieved by calling +ioctl(container, VFIO_IOMMU_SET_MSI_BINDING, &guest_binding); + VFIO User API ------------------------------------------------------------------------------- -- 2.26.2