From patchwork Mon May 19 16:24:00 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 30392 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ie0-f200.google.com (mail-ie0-f200.google.com [209.85.223.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7B33E203AB for ; Mon, 19 May 2014 16:25:55 +0000 (UTC) Received: by mail-ie0-f200.google.com with SMTP id y20sf18360248ier.3 for ; Mon, 19 May 2014 09:25:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe:list-post :list-help:list-subscribe:mime-version:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list :list-archive:content-type:content-transfer-encoding; bh=jIunMj76r1nuK/D15OIxLBvLDOlwCTUdWAJielIkhfw=; b=Nkf5vqPZEu8/V5gBsIMl42cElnZC5Oe2w4PUJYS6pGi0J6OYGZPWUtUSrY6KP1R3CC q5sUzeJjkpLU4dOh9YPNO0AXP3LVWvsJzyvGScGs/B6UPY2KdZbPJL3fKrS3CibAckag SJnOBJ9I27Q2OLhaOy83cn4EIb8Iul4BW+bUr8ycW6b0PfIB5GNgBEtpa0IeWBUTg7rm iIiB80i8WgycMl4HwgzxaQzdMA8L/EDPnNp/6hK6szfp2wB3cubx/5VZUAmI8o7Jc/QE mMTomWsJl2G3fGXly9865+YIpe3G8c6tAVQ7tUh+y/EBqlf4M0/fWiraBlWhuq3IzHWS 3Uuw== X-Gm-Message-State: ALoCoQlndz6OulKj272D/uIabKaTsHWcWTUCMW7A4makfqWebUgdUKzxMlQPY/DYN8U+fJuFYFW8 X-Received: by 10.182.153.167 with SMTP id vh7mr1365859obb.5.1400516755009; Mon, 19 May 2014 09:25:55 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.80.81 with SMTP id b75ls1834219qgd.34.gmail; Mon, 19 May 2014 09:25:54 -0700 (PDT) X-Received: by 10.58.185.165 with SMTP id fd5mr1493920vec.41.1400516754806; Mon, 19 May 2014 09:25:54 -0700 (PDT) Received: from mail-ve0-f177.google.com (mail-ve0-f177.google.com [209.85.128.177]) by mx.google.com with ESMTPS id af10si4064135vec.23.2014.05.19.09.25.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 19 May 2014 09:25:54 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.177 as permitted sender) client-ip=209.85.128.177; Received: by mail-ve0-f177.google.com with SMTP id db11so6589782veb.8 for ; Mon, 19 May 2014 09:25:54 -0700 (PDT) X-Received: by 10.58.216.163 with SMTP id or3mr120419vec.80.1400516754712; Mon, 19 May 2014 09:25:54 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.221.72 with SMTP id ib8csp247567vcb; Mon, 19 May 2014 09:25:54 -0700 (PDT) X-Received: by 10.140.42.165 with SMTP id c34mr48952834qga.40.1400516753829; Mon, 19 May 2014 09:25:53 -0700 (PDT) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id e88si9018530qgf.97.2014.05.19.09.25.53 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 19 May 2014 09:25:53 -0700 (PDT) Received-SPF: none (google.com: xen-devel-bounces@lists.xen.org does not designate permitted sender hosts) client-ip=50.57.142.19; Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WmQMI-0000or-Iv; Mon, 19 May 2014 16:24:14 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WmQMG-0000nk-JH for xen-devel@lists.xenproject.org; Mon, 19 May 2014 16:24:12 +0000 Received: from [85.158.139.211:12384] by server-5.bemta-5.messagelabs.com id A9/80-06049-B203A735; Mon, 19 May 2014 16:24:11 +0000 X-Env-Sender: julien.grall@linaro.org X-Msg-Ref: server-16.tower-206.messagelabs.com!1400516650!2277217!1 X-Originating-IP: [74.125.83.48] X-SpamReason: No, hits=0.5 required=7.0 tests=BODY_RANDOM_LONG X-StarScan-Received: X-StarScan-Version: 6.11.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 12643 invoked from network); 19 May 2014 16:24:11 -0000 Received: from mail-ee0-f48.google.com (HELO mail-ee0-f48.google.com) (74.125.83.48) by server-16.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 19 May 2014 16:24:11 -0000 Received: by mail-ee0-f48.google.com with SMTP id e49so3758403eek.7 for ; Mon, 19 May 2014 09:24:10 -0700 (PDT) X-Received: by 10.14.212.9 with SMTP id x9mr48940957eeo.46.1400516650861; Mon, 19 May 2014 09:24:10 -0700 (PDT) Received: from belegaer.uk.xensource.com ([185.25.64.249]) by mx.google.com with ESMTPSA id p9sm42954653eeg.32.2014.05.19.09.24.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 May 2014 09:24:09 -0700 (PDT) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Mon, 19 May 2014 17:24:00 +0100 Message-Id: <1400516640-7175-5-git-send-email-julien.grall@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1400516640-7175-1-git-send-email-julien.grall@linaro.org> References: <1400516640-7175-1-git-send-email-julien.grall@linaro.org> Cc: stefano.stabellini@citrix.com, Julien Grall , tim@xen.org, ian.campbell@citrix.com, Jan Beulich Subject: [Xen-devel] [PATCH v8 4/4] xen/arm: grant: Add another entry to map MFN 1:1 in dom0 p2m X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: julien.grall@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.177 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Archive: Grant mapping can be used for DMA request. The dev_bus_addr returned by the hypercall is the MFN (not the IPA). Currently Linux is using this address (via swiotlb) to program the DMA. When the device is protected by IOMMU the request will fail. We have to add 1:1 mapping in the domain p2m to allow DMA request working. This is valid because DOM0 has its memory mapped 1:1 and therefore we know that RAM and devices cannot clash. The grant mapping code already handle this case for x86 PV guests. Reuse the same code path for ARM guest. Signed-off-by: Julien Grall Cc: Jan Beulich --- The patch has been heavily rework to use iommu_{,un}map_page. I dropped all the acks. Changes in v8: - Rework differently the 1:1 mapping by using iommu_{,un}map_page helpers. Changes in v5: - Update commit message Changes in v4: - Patch added --- xen/arch/arm/p2m.c | 2 ++ xen/common/grant_table.c | 4 ++-- xen/drivers/passthrough/arm/smmu.c | 44 ++++++++++++++++++++++++++++++++++++ xen/include/asm-arm/grant_table.h | 2 ++ xen/include/asm-arm/p2m.h | 2 ++ xen/include/asm-x86/grant_table.h | 2 ++ 6 files changed, 54 insertions(+), 2 deletions(-) diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c index 96bc0ef..810459a 100644 --- a/xen/arch/arm/p2m.c +++ b/xen/arch/arm/p2m.c @@ -227,6 +227,7 @@ static lpae_t mfn_to_p2m_entry(unsigned long mfn, unsigned int mattr, e.p2m.write = 0; break; + case p2m_iommu_map_rw: case p2m_map_foreign: case p2m_grant_map_rw: case p2m_mmio_direct: @@ -234,6 +235,7 @@ static lpae_t mfn_to_p2m_entry(unsigned long mfn, unsigned int mattr, e.p2m.write = 1; break; + case p2m_iommu_map_ro: case p2m_grant_map_ro: case p2m_invalid: e.p2m.xn = 1; diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 2c93d9c..7e549f2 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -727,7 +727,7 @@ __gnttab_map_grant_ref( double_gt_lock(lgt, rgt); - if ( !paging_mode_translate(ld) && need_iommu(ld) ) + if ( gnttab_need_iommu_mapping(ld) && need_iommu(ld) ) { unsigned int wrc, rdc; int err = 0; @@ -935,7 +935,7 @@ __gnttab_unmap_common( act->pin -= GNTPIN_hstw_inc; } - if ( !paging_mode_translate(ld) && need_iommu(ld) ) + if ( gnttab_need_iommu_mapping(ld) && need_iommu(ld) ) { unsigned int wrc, rdc; int err = 0; diff --git a/xen/drivers/passthrough/arm/smmu.c b/xen/drivers/passthrough/arm/smmu.c index 21b4572..9f85800 100644 --- a/xen/drivers/passthrough/arm/smmu.c +++ b/xen/drivers/passthrough/arm/smmu.c @@ -1536,6 +1536,48 @@ static void arm_smmu_iommu_domain_teardown(struct domain *d) xfree(smmu_domain); } +static int arm_smmu_map_page(struct domain *d, unsigned long gfn, + unsigned long mfn, unsigned int flags) +{ + p2m_type_t t; + + /* This function should only be used by gnttab code when the domain + * is direct mapped and gfn == mfn. + */ + if ( !is_domain_direct_mapped(d) || gfn != mfn ) + return -EINVAL; + + /* We only support readable and writable flags */ + if ( !(flags & (IOMMUF_readable | IOMMUF_writable)) ) + return -EINVAL; + + /* The function guest_physmap_add_entry replace the current mapping + * if there is already one... + */ + t = (flags & IOMMUF_writable)? p2m_iommu_map_rw : p2m_iommu_map_ro; + + /* Grant mapping can be used for DMA request. The dev_bus_addr returned by + * the hypercall is the MFN (not the IPA). For device protected by + * an IOMMU, Xen needs to add a 1:1 mapping in the domain p2m to + * allow DMA request working. + * This is only valid when the domain is directed mapped + */ + return guest_physmap_add_entry(d, gfn, mfn, 0, t); +} + +static int arm_smmu_unmap_page(struct domain *d, unsigned long gfn) +{ + /* This function should only be used by gnttab code when the domain + * is direct mapped + */ + if ( !is_domain_direct_mapped(d) ) + return -EINVAL; + + guest_physmap_remove_page(d, gfn, gfn, 0); + + return 0; +} + static const struct iommu_ops arm_smmu_iommu_ops = { .init = arm_smmu_iommu_domain_init, .hwdom_init = arm_smmu_iommu_hwdom_init, @@ -1544,6 +1586,8 @@ static const struct iommu_ops arm_smmu_iommu_ops = { .iotlb_flush_all = arm_smmu_iotlb_flush_all, .assign_dt_device = arm_smmu_attach_dev, .reassign_dt_device = arm_smmu_reassign_dt_dev, + .map_page = arm_smmu_map_page, + .unmap_page = arm_smmu_unmap_page, }; static int __init smmu_init(struct dt_device_node *dev, diff --git a/xen/include/asm-arm/grant_table.h b/xen/include/asm-arm/grant_table.h index 6e0cc59..673bcdd 100644 --- a/xen/include/asm-arm/grant_table.h +++ b/xen/include/asm-arm/grant_table.h @@ -33,6 +33,8 @@ static inline int replace_grant_supported(void) ( ((i >= nr_grant_frames(d->grant_table)) && \ (i < max_nr_grant_frames)) ? 0 : (d->arch.grant_table_gpfn[i])) +#define gnttab_need_iommu_mapping(d) is_domain_direct_mapped(d) + #endif /* __ASM_GRANT_TABLE_H__ */ /* * Local variables: diff --git a/xen/include/asm-arm/p2m.h b/xen/include/asm-arm/p2m.h index bd71abe..b68d5b8 100644 --- a/xen/include/asm-arm/p2m.h +++ b/xen/include/asm-arm/p2m.h @@ -45,6 +45,8 @@ typedef enum { p2m_map_foreign, /* Ram pages from foreign domain */ p2m_grant_map_rw, /* Read/write grant mapping */ p2m_grant_map_ro, /* Read-only grant mapping */ + p2m_iommu_map_rw, /* Read/write iommu mapping */ + p2m_iommu_map_ro, /* Read-only iommu mapping */ p2m_max_real_type, /* Types after this won't be store in the p2m */ } p2m_type_t; diff --git a/xen/include/asm-x86/grant_table.h b/xen/include/asm-x86/grant_table.h index 3013869..e5ccf2b 100644 --- a/xen/include/asm-x86/grant_table.h +++ b/xen/include/asm-x86/grant_table.h @@ -65,6 +65,8 @@ static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st) /* Done implicitly when page tables are destroyed. */ #define gnttab_release_host_mappings(domain) ( paging_mode_external(domain) ) +#define gnttab_need_iommu_mapping(d) !paging_mode_translate(d) + static inline int replace_grant_supported(void) { return 1;