Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1070538yba; Fri, 3 May 2019 15:33:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxPCACfrBRnAUmixQZuQETEykV1AbkIMAfLz76PAvrvr88ZhfhbhHVHW1fijoiHHYgXhBBV X-Received: by 2002:a17:902:854a:: with SMTP id d10mr14221923plo.8.1556922812926; Fri, 03 May 2019 15:33:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556922812; cv=none; d=google.com; s=arc-20160816; b=UpjIbt0eim0joG0odvID2GhBABg9TMwuYqTqZw43z9EUVlmzDX3EgabL2rf/8GZCNv B+oc1x+i8ryuXRKKg8y3xEHat7sRad5QXkkQTgZuOYl+cda1vyLZgy+P1mjV2iQ9N8mJ qP5GLFouqbbBzleMXeLjQwHqoQNKHP/WdEB2QnY9mmuo4+6ZVeKJn8uSqbzuTpIwMQhQ nfqcH5W0VfCQuhFbYRMaLIUg6k+qs6rhz0gEgH5L89xN3ZDt/XfyuM3wBdFPvjQvEHK+ PjzCj3OEyEBmQ7QaVXwpqFW+Rnem+/CUEeHFTyitOX3PCfBpnSNQywYpaLxHGV7K0W+a UPjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=qHBxpsFR2hTZWVazs5K697GHBxvsgNISw3HPSU/FSlE=; b=PYkk79gHs4sa2JpmUF3aPwQcRzBZqS0dRe52cUp60olPL7wcFX5oSrCLrb4srJcxBn BEmRqMW07kyZOq+rtruRo2QqFqa5jU2ViqETCZMmTAtXSdYepD8qcM8n9IJGsvOxV0kK mT368nDGL0RHi4qu0KV1wd8un6bPEX0vDGkk1ybb6Sj3TGZ7OvVx7vbW8b+UROo5V9VK eWu4oKK7l/c+kis65F36muVM9Q1kQXqsIidiTxZ9UIPlRYcWM4DnATbSapaRlaQIMy75 tmSqipgsxV6CrI+QVn2HaA3XK3UxYbRNmutGQ8syJLqRF7pgx2kXDL9cBXxYhKBncM00 MbPA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a11si3950377pgw.384.2019.05.03.15.33.17; Fri, 03 May 2019 15:33:32 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727198AbfECWaT (ORCPT + 99 others); Fri, 3 May 2019 18:30:19 -0400 Received: from mga02.intel.com ([134.134.136.20]:7007 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726797AbfECW3g (ORCPT ); Fri, 3 May 2019 18:29:36 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 May 2019 15:29:33 -0700 X-ExtLoop1: 1 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga002.jf.intel.com with ESMTP; 03 May 2019 15:29:33 -0700 From: Jacob Pan To: iommu@lists.linux-foundation.org, LKML , Joerg Roedel , David Woodhouse , Eric Auger , Alex Williamson , Jean-Philippe Brucker Cc: "Yi Liu" , "Tian, Kevin" , Raj Ashok , "Christoph Hellwig" , "Lu Baolu" , Andriy Shevchenko , Jacob Pan , Liu@vger.kernel.org, Yi L Subject: [PATCH v3 12/16] iommu/vt-d: Add nested translation helper function Date: Fri, 3 May 2019 15:32:13 -0700 Message-Id: <1556922737-76313-13-git-send-email-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1556922737-76313-1-git-send-email-jacob.jun.pan@linux.intel.com> References: <1556922737-76313-1-git-send-email-jacob.jun.pan@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Nested translation mode is supported in VT-d 3.0 Spec.CH 3.8. With PASID granular translation type set to 0x11b, translation result from the first level(FL) also subject to a second level(SL) page table translation. This mode is used for SVA virtualization, where FL performs guest virtual to guest physical translation and SL performs guest physical to host physical translation. Signed-off-by: Jacob Pan Signed-off-by: Liu, Yi L --- drivers/iommu/intel-pasid.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ drivers/iommu/intel-pasid.h | 11 ++++++ 2 files changed, 104 insertions(+) diff --git a/drivers/iommu/intel-pasid.c b/drivers/iommu/intel-pasid.c index dde05b5..d8421f7 100644 --- a/drivers/iommu/intel-pasid.c +++ b/drivers/iommu/intel-pasid.c @@ -682,3 +682,96 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu, return 0; } + +/** + * intel_pasid_setup_nested() - Set up PASID entry for nested translation + * which is used for vSVA. The first level page tables are used for + * GVA-GPA translation in the guest, second level page tables are used + * for GPA to HPA translation. + * + * @iommu: Iommu which the device belong to + * @dev: Device to be set up for translation + * @gpgd: FLPTPTR: First Level Page translation pointer in GPA + * @pasid: PASID to be programmed in the device PASID table + * @flags: Additional info such as supervisor PASID + * @domain: Domain info for setting up second level page tables + * @addr_width: Address width of the first level (guest) + */ +int intel_pasid_setup_nested(struct intel_iommu *iommu, + struct device *dev, pgd_t *gpgd, + int pasid, int flags, + struct dmar_domain *domain, + int addr_width) +{ + struct pasid_entry *pte; + struct dma_pte *pgd; + u64 pgd_val; + int agaw; + u16 did; + + if (!ecap_nest(iommu->ecap)) { + pr_err("IOMMU: %s: No nested translation support\n", + iommu->name); + return -EINVAL; + } + + pte = intel_pasid_get_entry(dev, pasid); + if (WARN_ON(!pte)) + return -EINVAL; + + pasid_clear_entry(pte); + + /* Sanity checking performed by caller to make sure address + * width matching in two dimensions: + * 1. CPU vs. IOMMU + * 2. Guest vs. Host. + */ + switch (addr_width) { + case 57: + pasid_set_flpm(pte, 1); + break; + case 48: + pasid_set_flpm(pte, 0); + break; + default: + dev_err(dev, "Invalid paging mode %d\n", addr_width); + return -EINVAL; + } + + /* Setup the first level page table pointer in GPA */ + pasid_set_flptr(pte, (u64)gpgd); + if (flags & PASID_FLAG_SUPERVISOR_MODE) { + if (!ecap_srs(iommu->ecap)) { + pr_err("No supervisor request support on %s\n", + iommu->name); + return -EINVAL; + } + pasid_set_sre(pte); + } + + /* Setup the second level based on the given domain */ + pgd = domain->pgd; + + for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) { + pgd = phys_to_virt(dma_pte_addr(pgd)); + if (!dma_pte_present(pgd)) { + dev_err(dev, "Invalid domain page table\n"); + return -EINVAL; + } + } + pgd_val = virt_to_phys(pgd); + pasid_set_slptr(pte, pgd_val); + pasid_set_fault_enable(pte); + + did = domain->iommu_did[iommu->seq_id]; + pasid_set_domain_id(pte, did); + + pasid_set_address_width(pte, agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); + pasid_set_present(pte); + pasid_flush_caches(iommu, pte, pasid, did); + + return 0; +} diff --git a/drivers/iommu/intel-pasid.h b/drivers/iommu/intel-pasid.h index 4b26ab5..2234fd5 100644 --- a/drivers/iommu/intel-pasid.h +++ b/drivers/iommu/intel-pasid.h @@ -42,6 +42,7 @@ * to vmalloc or even module mappings. */ #define PASID_FLAG_SUPERVISOR_MODE BIT(0) +#define PASID_FLAG_NESTED BIT(1) struct pasid_dir_entry { u64 val; @@ -51,6 +52,11 @@ struct pasid_entry { u64 val[8]; }; +#define PASID_ENTRY_PGTT_FL_ONLY (1) +#define PASID_ENTRY_PGTT_SL_ONLY (2) +#define PASID_ENTRY_PGTT_NESTED (3) +#define PASID_ENTRY_PGTT_PT (4) + /* The representative of a PASID table */ struct pasid_table { void *table; /* pasid table pointer */ @@ -77,6 +83,11 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu, int intel_pasid_setup_pass_through(struct intel_iommu *iommu, struct dmar_domain *domain, struct device *dev, int pasid); +int intel_pasid_setup_nested(struct intel_iommu *iommu, + struct device *dev, pgd_t *pgd, + int pasid, int flags, + struct dmar_domain *domain, + int addr_width); void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev, int pasid); int vcmd_alloc_pasid(struct intel_iommu *iommu, unsigned int *pasid); -- 2.7.4