From patchwork Wed Jan 29 14:55:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11356303 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 64699159A for ; Wed, 29 Jan 2020 14:56:52 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 401162070E for ; Wed, 29 Jan 2020 14:56:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VWqkjMAq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 401162070E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iwokZ-0000LQ-HO; Wed, 29 Jan 2020 14:55:27 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iwokY-0000Kt-Fo for xen-devel@lists.xenproject.org; Wed, 29 Jan 2020 14:55:26 +0000 X-Inumbo-ID: 6164dd7c-42a7-11ea-b211-bc764e2007e4 Received: from mail-lf1-x143.google.com (unknown [2a00:1450:4864:20::143]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6164dd7c-42a7-11ea-b211-bc764e2007e4; Wed, 29 Jan 2020 14:55:25 +0000 (UTC) Received: by mail-lf1-x143.google.com with SMTP id t23so12055630lfk.6 for ; Wed, 29 Jan 2020 06:55:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ZW9+Wg3q/4l9pYMVcV8YdGyoyYfomvr0275Ia3YZK6E=; b=VWqkjMAqKqsPzyQZmtXOTKbHWqAfFlPjz3aEQ0PmSczqCrwVYLSpCXm3OTe7KvLrAG qSEYpqoMpvkPxUlkgQQFXgbU+HVtOcm53pq37UhmRKSzWsAC2Uy2GGja2oGz4t0zSq2R fUCShSiRkGcinaeOfcahl4UZRjGk3YyDigVudV3d3CrltoxmvS18oPopZAtPR9IufI4k eHqlzzL+2at8UVRf68VwHKmKvgW2+8yCWFp0kSkqCGtKUfZt//O9svvmM/3dRn5hOx1Q kyUuxmniWdqCVOgURTeo9K1wa04EdDOw7eVlGqpB9n+RFzToVyOE032spzL4ZJLVhlxc lqBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ZW9+Wg3q/4l9pYMVcV8YdGyoyYfomvr0275Ia3YZK6E=; b=azri75AUQO6EVk/2EK1oAS6RHRnVg3KQ/t+a+vlIah4F54aLZ40KTLo0M3yWCaFzzI aZBkJGLPvJx7rU7Ma9dfTTPR98B8BNKc8meL6JZeP2uXd28+Z9W2J63K3UdtlQ3ucGs9 wkBGkem66KTxMcDOELyyY9TPJHebRliUH/iJ7/NPAK9ts24XchEF7EeB72/5oDkTTU1z HzEsuTD/PiGm0sj5ws1qsLs+UqrXE0Vj4gR0WwPMgHFSyI7jY23Pw+e7ZFts87TybZ2w S3d6LaQ2M/XlxM0Vsi3aHYsXk62kwUqHvn5w0PhXx5ScmsS20yfonjJuy+/nPUIIrLUW gN/w== X-Gm-Message-State: APjAAAW+sYRfUI5JNXABHQw1hlBq/P/pgm1E2o5V7kuWkn5Ly9USxVSU waIUxKaoU02CiKjiDhTXDmcImWgMYJw= X-Google-Smtp-Source: APXvYqzSmmZbz/o2JGngqjGECL0eKGvEb/MeDavdj+NRTZ6D4QpyzxFTBV2dU0FxZRcLVgWxs2AJdQ== X-Received: by 2002:a19:c210:: with SMTP id l16mr5846701lfc.35.1580309724283; Wed, 29 Jan 2020 06:55:24 -0800 (PST) Received: from otyshchenko.kyiv.epam.com (ll-22.209.223.85.sovam.net.ua. [85.223.209.22]) by smtp.gmail.com with ESMTPSA id k24sm1358605ljj.27.2020.01.29.06.55.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 29 Jan 2020 06:55:23 -0800 (PST) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Wed, 29 Jan 2020 16:55:14 +0200 Message-Id: <1580309714-21912-1-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [Xen-devel] [PATCH] iommu/arm: Don't allow the same micro-TLB to be shared between domains X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Oleksandr Tyshchenko , Yoshihiro Shimoda , Stefano Stabellini , Julien Grall , Volodymyr Babchuk MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Oleksandr Tyshchenko For the IPMMU-VMSA we need to prevent the use cases where devices which use the same micro-TLB are assigned to different Xen domains (micro-TLB cannot be shared between multiple Xen domains, since it points to the context bank to use for the page walk). As each Xen domain uses individual context bank pointed by context_id, we can potentially recognize that use case by comparing current and new context_id for the already enabled micro-TLB and prevent different context bank from being set. Signed-off-by: Oleksandr Tyshchenko --- CC: Julien Grall CC: Stefano Stabellini CC: Volodymyr Babchuk CC: Yoshihiro Shimoda --- xen/drivers/passthrough/arm/ipmmu-vmsa.c | 44 +++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/xen/drivers/passthrough/arm/ipmmu-vmsa.c b/xen/drivers/passthrough/arm/ipmmu-vmsa.c index 9cfae7e..c21d2d7 100644 --- a/xen/drivers/passthrough/arm/ipmmu-vmsa.c +++ b/xen/drivers/passthrough/arm/ipmmu-vmsa.c @@ -257,6 +257,7 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock); #define IMUCTR_TTSEL_MMU(n) ((n) << 4) #define IMUCTR_TTSEL_PMB (8 << 4) #define IMUCTR_TTSEL_MASK (15 << 4) +#define IMUCTR_TTSEL_SHIFT 4 #define IMUCTR_FLUSH (1 << 1) #define IMUCTR_MMUEN (1 << 0) @@ -434,19 +435,45 @@ static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) } /* Enable MMU translation for the micro-TLB. */ -static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain, - unsigned int utlb) +static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain, + unsigned int utlb) { struct ipmmu_vmsa_device *mmu = domain->mmu; + uint32_t data; + + /* + * We need to prevent the use cases where devices which use the same + * micro-TLB are assigned to different Xen domains (micro-TLB cannot be + * shared between multiple Xen domains, since it points to the context bank + * to use for the page walk). + * As each Xen domain uses individual context bank pointed by context_id, + * we can potentially recognize that use case by comparing current and new + * context_id for already enabled micro-TLB and prevent different context + * bank from being set. + */ + data = ipmmu_read(mmu, IMUCTR(utlb)); + if ( data & IMUCTR_MMUEN ) + { + unsigned int context_id; + + context_id = (data & IMUCTR_TTSEL_MASK) >> IMUCTR_TTSEL_SHIFT; + if ( domain->context_id != context_id ) + { + dev_err(mmu->dev, "Micro-TLB %u already assigned to IPMMU context %u\n", + utlb, context_id); + return -EINVAL; + } + } /* * TODO: Reference-count the micro-TLB as several bus masters can be - * connected to the same micro-TLB. Prevent the use cases where - * the same micro-TLB could be shared between multiple Xen domains. + * connected to the same micro-TLB. */ ipmmu_write(mmu, IMUASID(utlb), 0); - ipmmu_write(mmu, IMUCTR(utlb), ipmmu_read(mmu, IMUCTR(utlb)) | + ipmmu_write(mmu, IMUCTR(utlb), data | IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN); + + return 0; } /* Disable MMU translation for the micro-TLB. */ @@ -671,7 +698,12 @@ static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain, dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id); for ( i = 0; i < fwspec->num_ids; ++i ) - ipmmu_utlb_enable(domain, fwspec->ids[i]); + { + int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]); + + if ( ret ) + return ret; + } return 0; }