Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp199948ybl; Tue, 28 Jan 2020 21:58:47 -0800 (PST) X-Google-Smtp-Source: APXvYqzYts4aAtTJulsDF8g1DZag3BNdMHLRgbBH8uJsNS7n+rKVe3PFaq/Y9STS/zqLupQ+2HHJ X-Received: by 2002:aca:503:: with SMTP id 3mr5257687oif.24.1580277527174; Tue, 28 Jan 2020 21:58:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580277527; cv=none; d=google.com; s=arc-20160816; b=DjvIDoHcoj6hJ06yX5FYpvnltB7X/ZKazFBpaei3qw1YoS8aMSgg9gJ/7lt7rbQTAK X39IxcHuDOaE0K2gqtK9v3Buqwo98bxA+Rrts44extC7ex8OzgaqQET2uEkN8K1ND1oa I4nf322/zwMTaU2f2H0MXoGVsa4BlhrvPQ6lhrlM7u/FcZxI1TAO8R87PA2XRV1iSRIS rtS29rkQFALGHXg0lMi5pltny3Wk6Oo9xkzxlb98Lb4PVOltKgOjZwzumNtIIl5kuEza 1IHjOGna/WNCTmQ8oDfvGmNglil9Dp0A82NllWY9XhyBuiDHytszlJJtxzWniMTfTI5t SL5A== 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=oc4hTgf8YTdeTgG69UCZNqfD4V2RQNmVs8cC9DXqwZg=; b=HsNF5P7c0o2PHQ9uHTOZ1mj0FOH4om70cBEOlYYDuoSxqxLO5l6Aw1icxAZwYjJaNv qJZX/GLWLEtgv1mPy6m87r4M/IYnCZjEN4uRODq/xDbJ+6GQFcTYqiRjWugwnlQhdniN ak5fUq7UH9+6Tlz+jjn5zw8NiSKF195VW8rXP7eZrP/v/rG3eMJwncWb2NIR/vI7SHhv fi0vLTX10cv4A75B6lG4+S/+AfGVOdP5MFpmtTbupv9NTE/d+DdU6AlvaK/c0v11+pgs h7qJ5JFfvDj6P3d7JXISUIlYmE6NkM5HjIXF2WBuZ/OCaDFGB3PUTssRqdY5wU6ni4dm ddpA== 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 w15si644633otm.263.2020.01.28.21.58.35; Tue, 28 Jan 2020 21:58:47 -0800 (PST) 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 S1726731AbgA2F5U (ORCPT + 99 others); Wed, 29 Jan 2020 00:57:20 -0500 Received: from mga05.intel.com ([192.55.52.43]:23417 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725934AbgA2F4o (ORCPT ); Wed, 29 Jan 2020 00:56:44 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Jan 2020 21:56:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,376,1574150400"; d="scan'208";a="222346522" Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga008.jf.intel.com with ESMTP; 28 Jan 2020 21:56:41 -0800 From: Jacob Pan To: iommu@lists.linux-foundation.org, LKML , "Lu Baolu" , Joerg Roedel , David Woodhouse Cc: "Yi Liu" , "Tian, Kevin" , Raj Ashok , Alex Williamson , "Christoph Hellwig" , Jean-Philippe Brucker , Jonathan Cameron , Eric Auger , Jacob Pan , Liu@vger.kernel.org, Yi L Subject: [PATCH V9 03/10] iommu/vt-d: Add nested translation helper function Date: Tue, 28 Jan 2020 22:01:46 -0800 Message-Id: <1580277713-66934-4-git-send-email-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1580277713-66934-1-git-send-email-jacob.jun.pan@linux.intel.com> References: <1580277713-66934-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 | 225 ++++++++++++++++++++++++++++++++++++++++++++ drivers/iommu/intel-pasid.h | 12 +++ include/linux/intel-iommu.h | 3 + 3 files changed, 240 insertions(+) diff --git a/drivers/iommu/intel-pasid.c b/drivers/iommu/intel-pasid.c index 22b30f10b396..bd067af4d20b 100644 --- a/drivers/iommu/intel-pasid.c +++ b/drivers/iommu/intel-pasid.c @@ -359,6 +359,76 @@ pasid_set_flpm(struct pasid_entry *pe, u64 value) pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2); } +/* + * Setup the Extended Memory Type(EMT) field (Bits 91-93) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_emt(struct pasid_entry *pe, u64 value) +{ + pasid_set_bits(&pe->val[1], GENMASK_ULL(29, 27), value << 27); +} + +/* + * Setup the Page Attribute Table (PAT) field (Bits 96-127) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_pat(struct pasid_entry *pe, u64 value) +{ + pasid_set_bits(&pe->val[1], GENMASK_ULL(63, 32), value << 32); +} + +/* + * Setup the Cache Disable (CD) field (Bit 89) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_cd(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[1], 1 << 25, 1 << 25); +} + +/* + * Setup the Extended Memory Type Enable (EMTE) field (Bit 90) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_emte(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[1], 1 << 26, 1 << 26); +} + +/* + * Setup the Extended Access Flag Enable (EAFE) field (Bit 135) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_eafe(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7); +} + +/* + * Setup the Page-level Cache Disable (PCD) field (Bit 95) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_pcd(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[1], 1 << 31, 1 << 31); +} + +/* + * Setup the Page-level Write-Through (PWT)) field (Bit 94) + * of a scalable mode PASID entry. + */ +static inline void +pasid_set_pwt(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[1], 1 << 30, 1 << 30); +} + static void pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu, u16 did, int pasid) @@ -596,3 +666,158 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu, return 0; } + +static int intel_pasid_setup_bind_data(struct intel_iommu *iommu, + struct pasid_entry *pte, + struct iommu_gpasid_bind_data_vtd *pasid_data) +{ + /* + * Not all guest PASID table entry fields are passed down during bind, + * here we only set up the ones that are dependent on guest settings. + * Execution related bits such as NXE, SMEP are not meaningful to IOMMU, + * therefore not set. Other fields, such as snoop related, are set based + * on host needs regardless of guest settings. + */ + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_SRE) { + if (!ecap_srs(iommu->ecap)) { + pr_err("No supervisor request support on %s\n", + iommu->name); + return -EINVAL; + } + pasid_set_sre(pte); + } + + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EAFE) { + if (!ecap_eafs(iommu->ecap)) { + pr_err("No extended access flag support on %s\n", + iommu->name); + return -EINVAL; + } + pasid_set_eafe(pte); + } + + /* + * Memory type is only applicable to devices inside processor coherent + * domain. PCIe devices are not included. We can skip the rest of the + * flags if IOMMU does not support MTS. + */ + if (ecap_mts(iommu->ecap)) { + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EMTE) { + pasid_set_emte(pte); + pasid_set_emt(pte, pasid_data->emt); + } + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_PCD) + pasid_set_pcd(pte); + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_PWT) + pasid_set_pwt(pte); + if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_CD) + pasid_set_cd(pte); + pasid_set_pat(pte, pasid_data->pat); + } else if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EMT_MASK) { + pr_warn("No memory type support for bind guest PASID on %s\n", + iommu->name); + return -EINVAL; + } + + return 0; + +} + +/** + * intel_pasid_setup_nested() - Set up PASID entry for nested translation. + * This could be used for guest shared virtual address. In this case, the + * first level page tables are used for GVA-GPA translation in the guest, + * second level page tables are used for GPA-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 + * @pasid_data: Additional PASID info from the guest bind request + * @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, struct iommu_gpasid_bind_data_vtd *pasid_data, + struct dmar_domain *domain, + int addr_width) +{ + struct pasid_entry *pte; + struct dma_pte *pgd; + int ret = 0; + 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; + + /* + * Caller must ensure PASID entry is not in use, i.e. not bind the + * same PASID to the same device twice. + */ + if (pasid_pte_is_present(pte)) + return -EBUSY; + + 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 ADDR_WIDTH_5LEVEL: + pasid_set_flpm(pte, 1); + break; + case ADDR_WIDTH_4LEVEL: + pasid_set_flpm(pte, 0); + break; + default: + dev_err(dev, "Invalid guest address width %d\n", addr_width); + return -EINVAL; + } + + pasid_set_flptr(pte, (u64)gpgd); + + ret = intel_pasid_setup_bind_data(iommu, pte, pasid_data); + if (ret) { + dev_err(dev, "Guest PASID bind data not supported\n"); + return ret; + } + + /* 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)) { + pasid_clear_entry(pte); + 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 ret; +} diff --git a/drivers/iommu/intel-pasid.h b/drivers/iommu/intel-pasid.h index 92de6df24ccb..698015ee3f04 100644 --- a/drivers/iommu/intel-pasid.h +++ b/drivers/iommu/intel-pasid.h @@ -36,6 +36,7 @@ * to vmalloc or even module mappings. */ #define PASID_FLAG_SUPERVISOR_MODE BIT(0) +#define PASID_FLAG_NESTED BIT(1) /* * The PASID_FLAG_FL5LP flag Indicates using 5-level paging for first- @@ -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 */ @@ -99,6 +105,12 @@ 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, + struct iommu_gpasid_bind_data_vtd *pasid_data, + struct dmar_domain *domain, + int addr_width); void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev, int pasid); diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h index 27c6bbb0a333..c8abf051b2d5 100644 --- a/include/linux/intel-iommu.h +++ b/include/linux/intel-iommu.h @@ -42,6 +42,9 @@ #define DMA_FL_PTE_PRESENT BIT_ULL(0) #define DMA_FL_PTE_XD BIT_ULL(63) +#define ADDR_WIDTH_5LEVEL (57) +#define ADDR_WIDTH_4LEVEL (48) + #define CONTEXT_TT_MULTI_LEVEL 0 #define CONTEXT_TT_DEV_IOTLB 1 #define CONTEXT_TT_PASS_THROUGH 2 -- 2.7.4