Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp868350ybb; Fri, 20 Mar 2020 09:22:19 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtD1rkGK8bjLxFtpVG05gPfHr4RcYhNvzG2sFMfwPHM5jud70z1qx0TGPN+pXrZdiqpu+y3 X-Received: by 2002:aca:c0d4:: with SMTP id q203mr2127982oif.27.1584721338930; Fri, 20 Mar 2020 09:22:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584721338; cv=none; d=google.com; s=arc-20160816; b=GfEUFkdRSfw9ZAOmxHNrBlQCuCBm8OVhIk6Aq9WGB9pjME8lHxeRyk496nhYWmokPz +LXajNZXJMaEDEGA21C7AoLUgwbtJPoH8Fg9csLkJwcMMUOh4dI9xoV/L8u5koFUuEX9 uW5KXJX1LJ48LXhb1jXWeGLCin704tnVvSS+8ixKVkvm1eValR3UpHHv4eWsv/M5E3CP H9aGlKNlZ6hHAIMogJcLx9PhnZb3Fj3prDt0hB60B+FAiYraUVp+dFqbwcvszKzwUEiC VAPNwLIJ7w4nC8iae8gdo+NEm5rjYUBvp8NdtgHWusoN9TH2bighZ0eucu0wmyMvgwj6 meTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bXG84dHL0m4cU/Qn0Lqwx5Wj56OC04pQ0TdubjShMr8=; b=aMZxA7QstoKTJN2eMFWetxY4TXi7DHoXTrJ8DcOWeLeNEwc+lNZw7cA/Olt8SUnYlU ccLF7wxFyPQOkkCaglKLVZRLrcpMzS7JC+ntDzSXbVki59rzCz8qxdnaMY6qgEvVMCfg z5B0KEiWPt/ET95m31t3PqwOkjqys+W7axh5D08eJA12X91W/vDLz+4VnBl1F5EfR/wJ e6HQFUBRMFsZwu0Nm7h+mqIvM3xGKxvhoTBp1KoJG3JhUpSQPvnEfqG3cQXRnM3eCzv4 4MKiZrJJNtsRiJ5I8+lrg8Pc1TFWrCx7eNoaZ0O3HuoLPq+EtUyGtoztuTvrV7HlIoCK DLHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ee34v3Kl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id 127si3001252oig.130.2020.03.20.09.22.01; Fri, 20 Mar 2020 09:22:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ee34v3Kl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727537AbgCTQVR (ORCPT + 99 others); Fri, 20 Mar 2020 12:21:17 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:46040 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727337AbgCTQVQ (ORCPT ); Fri, 20 Mar 2020 12:21:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584721274; 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=bXG84dHL0m4cU/Qn0Lqwx5Wj56OC04pQ0TdubjShMr8=; b=ee34v3Kl/8HQycJIqOLr67n0aPU4Uz9UXAT4udJtTvFQrlyUyMG0sBGmesq200CByDdr9q Kk0/+fGgyi8mQBXvuup2ugqx6Khlhn8Mx9ny/tQ0o/UMHtMxgK/R6bVSwAg3lVRgCqDi8a 2MdVQR1p286cQzKCeVmF2FP8kDNN12U= 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-415-Pi0HcnQSPJ6M84wPvl51YQ-1; Fri, 20 Mar 2020 12:21:13 -0400 X-MC-Unique: Pi0HcnQSPJ6M84wPvl51YQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F1B381005510; Fri, 20 Mar 2020 16:21:09 +0000 (UTC) Received: from laptop.redhat.com (ovpn-113-142.ams2.redhat.com [10.36.113.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 85DEC60BFB; Fri, 20 Mar 2020 16:20:59 +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, joro@8bytes.org, alex.williamson@redhat.com, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, jean-philippe.brucker@arm.com, will.deacon@arm.com, robin.murphy@arm.com Cc: marc.zyngier@arm.com, peter.maydell@linaro.org, zhangfei.gao@gmail.com Subject: [PATCH v10 11/11] vfio: Document nested stage control Date: Fri, 20 Mar 2020 17:19:11 +0100 Message-Id: <20200320161911.27494-12-eric.auger@redhat.com> In-Reply-To: <20200320161911.27494-1-eric.auger@redhat.com> References: <20200320161911.27494-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org 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 --- 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..563ebcec9224 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); =20 +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 us= e +stage 1 (IOVA -> GPA), while the hypervisor uses stage 2 for VM isolatio= n +(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 str= ucture +(for security reason), including stage 2 configuration. This works as lo= ng +configuration structures and page table format are compatible between th= e +virtual IOMMU and the physical IOMMU. + +Assuming the HW supports it, this nested mode is selected by choosing th= e +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 w= ith +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 fau= lts +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_PRODUCER_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, cont= ext, ... + +The ARM SMMU specification introduces another challenge: MSIs are transl= ated by +both the virtual SMMU and the physical SMMU. To build a nested mapping f= or the +IOVA programmed into the assigned device, the guest needs to pass its IO= VA/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 ------------------------------------------------------------------------= ------- =20 --=20 2.20.1