From patchwork Wed Mar 8 11:01:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 9610847 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 456776046A for ; Wed, 8 Mar 2017 11:10:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 405462857D for ; Wed, 8 Mar 2017 11:10:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3306428586; Wed, 8 Mar 2017 11:10:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 755DA2857B for ; Wed, 8 Mar 2017 11:10:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751755AbdCHLKC (ORCPT ); Wed, 8 Mar 2017 06:10:02 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:33294 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750728AbdCHLKB (ORCPT ); Wed, 8 Mar 2017 06:10:01 -0500 Received: by mail-pf0-f196.google.com with SMTP id v190so3466848pfb.0; Wed, 08 Mar 2017 03:07:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:date:message-id:in-reply-to:references:subject; bh=t2UK8ad06ExEm6i3970i0zhNBgiLV2jaGscC39xwVtc=; b=lrq47kdu5hZEfJpR9wHogH7Ne/0vdaiEH20OVcNTiY4RvgxPyw99WdKam+GwAq/52N hHZLhlg16XROKlHBYbDVrE8Kks5V9bQ7Diz89rD/btjt1WUFBpANo9sJLGRxpkymLyaS lO8NU9SI/5RH9sCTIftxE+Mk9RBZGTbeNHgnvToTM8WY7CSmIxR7SL9bTXItitk668qw Q+ypdgxj3O/bOu+ii8nctXQV5HcjzhfPY2PsCNm9PDdajMWXifdRPVtP1gDOZfdXWic+ SsBb6JMEG/BwW0QTy4BIXJE2OEj1gdAxcbVoT1U/ni8JV/4ECSvR47q+UNPrIyeVH2Fv uNWg== 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:date:message-id:in-reply-to :references:subject; bh=t2UK8ad06ExEm6i3970i0zhNBgiLV2jaGscC39xwVtc=; b=tWzWgB7jkjyxFGlEn+dZRaRFVXBbxoogis/dzp+lTIGQEwrOk9UmnI3Aef6o80N8H9 O78Blzph1cMKPp93sv9h8/cbDD54U7aEEt8SSsSn+q0d/cEbAlV2L7mBrzg+HZl1EHCz dgm3/KjrC/s+E9u1baKOQv2XT2IYgR3/TrIVSdIUyFfeSNKagYkdrqb0eDnMg8yDmK36 Sc+xQRUFLgeTeUSQZmSi+ta4fpp3f3/vF8nwEGoikC+zpy4d/ilMEjoA6RCxSc9G5HCK xwnARJXEPEQ5xsHNpthALR4D6ymJadpki3ugOIrDsZFs7/wZZKp235qZEnaOrLPHEG83 9QIQ== X-Gm-Message-State: AMke39k+Hv5A48uw6nmBZ8cd8AYkL3Q5QN6RKh8eqhVD8WkrBde6pjpgR0Os3JqU0NSSUA== X-Received: by 10.84.236.4 with SMTP id q4mr7771634plk.1.1488971255256; Wed, 08 Mar 2017 03:07:35 -0800 (PST) Received: from [127.0.0.1] (s214090.ppp.asahi-net.or.jp. [220.157.214.90]) by smtp.gmail.com with ESMTPSA id g78sm5574404pfe.5.2017.03.08.03.07.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Mar 2017 03:07:33 -0800 (PST) From: Magnus Damm To: joro@8bytes.org Cc: laurent.pinchart+renesas@ideasonboard.com, geert+renesas@glider.be, linux-kernel@vger.kernel.org, linux-renesas-soc@vger.kernel.org, iommu@lists.linux-foundation.org, horms+renesas@verge.net.au, Magnus Damm , robin.murphy@arm.com, m.szyprowski@samsung.com Date: Wed, 08 Mar 2017 20:01:53 +0900 Message-Id: <148897091345.16106.2935423334300744039.sendpatchset@little-apple> In-Reply-To: <148897088333.16106.13237004440297956899.sendpatchset@little-apple> References: <148897088333.16106.13237004440297956899.sendpatchset@little-apple> Subject: [PATCH v3 03/09] iommu/ipmmu-vmsa: Enable multi context support Sender: linux-renesas-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-renesas-soc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Magnus Damm Add support for up to 8 contexts. Each context is mapped to one domain. One domain is assigned one or more slave devices. Contexts are allocated dynamically and slave devices are grouped together based on which IPMMU device they are connected to. This makes slave devices tied to the same IPMMU device share the same IOVA space. Signed-off-by: Magnus Damm --- Changes since V2: - Updated patch description to reflect code included in: [PATCH v7 00/07] iommu/ipmmu-vmsa: IPMMU multi-arch update V7 Changes since V1: - Support up to 8 contexts instead of 4 - Use feature flag and runtime handling - Default to single context drivers/iommu/ipmmu-vmsa.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) --- 0012/drivers/iommu/ipmmu-vmsa.c +++ work/drivers/iommu/ipmmu-vmsa.c 2017-03-08 17:59:19.900607110 +0900 @@ -30,11 +30,12 @@ #include "io-pgtable.h" -#define IPMMU_CTX_MAX 1 +#define IPMMU_CTX_MAX 8 struct ipmmu_features { bool use_ns_alias_offset; bool has_cache_leaf_nodes; + bool has_eight_ctx; }; struct ipmmu_vmsa_device { @@ -44,6 +45,7 @@ struct ipmmu_vmsa_device { const struct ipmmu_features *features; bool is_leaf; unsigned int num_utlbs; + unsigned int num_ctx; spinlock_t lock; /* Protects ctx and domains[] */ DECLARE_BITMAP(ctx, IPMMU_CTX_MAX); struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX]; @@ -376,11 +378,12 @@ static int ipmmu_domain_allocate_context spin_lock_irqsave(&mmu->lock, flags); - ret = find_first_zero_bit(mmu->ctx, IPMMU_CTX_MAX); - if (ret != IPMMU_CTX_MAX) { + ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx); + if (ret != mmu->num_ctx) { mmu->domains[ret] = domain; set_bit(ret, mmu->ctx); - } + } else + ret = -EBUSY; spin_unlock_irqrestore(&mmu->lock, flags); @@ -425,9 +428,9 @@ static int ipmmu_domain_init_context(str * Find an unused context. */ ret = ipmmu_domain_allocate_context(domain->root, domain); - if (ret == IPMMU_CTX_MAX) { + if (ret < 0) { free_io_pgtable_ops(domain->iop); - return -EBUSY; + return ret; } domain->context_id = ret; @@ -562,7 +565,7 @@ static irqreturn_t ipmmu_irq(int irq, vo /* * Check interrupts for all active contexts. */ - for (i = 0; i < IPMMU_CTX_MAX; i++) { + for (i = 0; i < mmu->num_ctx; i++) { if (!mmu->domains[i]) continue; if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED) @@ -632,6 +635,13 @@ static int ipmmu_attach_device(struct io domain->mmu = mmu; domain->root = root; ret = ipmmu_domain_init_context(domain); + if (ret < 0) { + dev_err(dev, "Unable to initialize IPMMU context\n"); + domain->mmu = NULL; + } else { + dev_info(dev, "Using IPMMU context %u\n", + domain->context_id); + } } else if (domain->mmu != mmu) { /* * Something is wrong, we can't attach two devices using @@ -1047,13 +1057,14 @@ static void ipmmu_device_reset(struct ip unsigned int i; /* Disable all contexts. */ - for (i = 0; i < 4; ++i) + for (i = 0; i < mmu->num_ctx; ++i) ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0); } static const struct ipmmu_features ipmmu_features_default = { .use_ns_alias_offset = true, .has_cache_leaf_nodes = false, + .has_eight_ctx = false, }; static const struct of_device_id ipmmu_of_ids[] = { @@ -1112,6 +1123,17 @@ static int ipmmu_probe(struct platform_d if (mmu->features->use_ns_alias_offset) mmu->base += IM_NS_ALIAS_OFFSET; + /* + * The number of contexts varies with generation and instance. + * Newer SoCs get a total of 8 contexts enabled, older ones just one. + */ + if (mmu->features->has_eight_ctx) + mmu->num_ctx = 8; + else + mmu->num_ctx = 1; + + WARN_ON(mmu->num_ctx > IPMMU_CTX_MAX); + irq = platform_get_irq(pdev, 0); /*