From patchwork Mon Jun 19 09:14:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 9795549 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 297E460381 for ; Mon, 19 Jun 2017 09:18:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 28131283C0 for ; Mon, 19 Jun 2017 09:18:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1CE8C283C6; Mon, 19 Jun 2017 09:18:13 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 9488327C05 for ; Mon, 19 Jun 2017 09:18:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753873AbdFSJR5 (ORCPT ); Mon, 19 Jun 2017 05:17:57 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:33162 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753872AbdFSJRz (ORCPT ); Mon, 19 Jun 2017 05:17:55 -0400 Received: by mail-pf0-f194.google.com with SMTP id w12so15534921pfk.0; Mon, 19 Jun 2017 02:17:54 -0700 (PDT) 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=kEMsbl9YLsAGynMbBBfxDbSEy8tCaOfj87bY0iRAC6o=; b=SRQ/1WN6Zja3oG7EiVp9BNOwi2q4nckgFDzLA0YcFbif0VDKlsxLVvRTMXCe4Jn5n+ 0V+x6r4ufeSTBo/Q5dtQzu4LrybR9wpLq9xYhx+ZLB+WoFmXfnCHO9j6sJYbxmGbwTCP bU3isAtISIc4bizq6eCd4juHdqwNAX3dM45vbnOy67nU+5cshiVToV8clDKgzsO3e8nP izD7gXDxdO80eT1QUMGUM0VF8FCdMFP0Rf4BrqryKIE0MnwgU6FMuxkg4FFT25HNjkob wLV9S7c/0TQwZ89xjW25jZsZ6yqWDWhHgdG22ZfXZWrMyMoHtEBavkkHyrkwxu3zHC1J +9XQ== 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=kEMsbl9YLsAGynMbBBfxDbSEy8tCaOfj87bY0iRAC6o=; b=pqJCB2blK20UBsc9BhxVrhArdtxBDaRK/ELA/mwPcD0+t1XQpdzWMKXoVa4MOuhuV3 KCCN9gwbTHFTrWuqr/NNq+PaMk0vZjwqthuPQUooMX4e5Vk/q7486WKMAuNVf9eAqg8c P5uFH1yi7/NjUDG+MvNJuU8JGtc0R8lY7CAiN989zWJ3APBkH2lR63iFQME1HMXlmxYM SE6HodGLHVz/BajxAQAbaMwJutE8tAN5tlNZMw7cxjcUb/o1SWHbOB6PtEeECcWKOoC/ EN7mfGahUZw5ItCj5RvH+2Pe+dOcGcAgjsj/SXNNHvKCTqBTBCqOCj6s93qYYiHZHaN4 XqEA== X-Gm-Message-State: AKS2vOwHuBnakTRgjh0XcRU1euTVxNxJeH795QMrc9HCV+0N8O1BZa/E zRUCrsKXgd6BjA== X-Received: by 10.84.224.71 with SMTP id a7mr15324695plt.67.1497863873954; Mon, 19 Jun 2017 02:17:53 -0700 (PDT) Received: from [127.0.0.1] (s214090.ppp.asahi-net.or.jp. [220.157.214.90]) by smtp.gmail.com with ESMTPSA id i14sm19645379pgn.14.2017.06.19.02.17.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Jun 2017 02:17:52 -0700 (PDT) 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: Mon, 19 Jun 2017 18:14:06 +0900 Message-Id: <149786364660.14868.13384595731879361399.sendpatchset@little-apple> In-Reply-To: <149786362527.14868.2143461703972595839.sendpatchset@little-apple> References: <149786362527.14868.2143461703972595839.sendpatchset@little-apple> Subject: [PATCH v4 02/09] iommu/ipmmu-vmsa: Add optional root device feature 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 root device handling to the IPMMU driver by allowing certain DT compat strings to enable has_cache_leaf_nodes that in turn will support both root devices with interrupts and leaf devices that face the actual IPMMU consumer devices. Signed-off-by: Magnus Damm --- Changes since V3: - Reworked root finding code to make it easier to follow, thanks Geert! Changes since V2: - Fixed a bug in ipmmu_find_root() when only leaf devices are present - Broke out __ipmmu_find_root() to allow ->xlate() check for root devices Changes since V1: - Moved patch to earlier in the series - Updated code to work with recent changes in: [PATCH v3 00/06] iommu/ipmmu-vmsa: IPMMU multi-arch update V3 drivers/iommu/ipmmu-vmsa.c | 95 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 17 deletions(-) --- 0015/drivers/iommu/ipmmu-vmsa.c +++ work/drivers/iommu/ipmmu-vmsa.c 2017-06-19 13:59:41.050607110 +0900 @@ -36,6 +36,7 @@ struct ipmmu_features { bool use_ns_alias_offset; + bool has_cache_leaf_nodes; }; struct ipmmu_vmsa_device { @@ -44,6 +45,7 @@ struct ipmmu_vmsa_device { struct iommu_device iommu; struct list_head list; const struct ipmmu_features *features; + bool is_leaf; unsigned int num_utlbs; spinlock_t lock; /* Protects ctx and domains[] */ DECLARE_BITMAP(ctx, IPMMU_CTX_MAX); @@ -54,6 +56,7 @@ struct ipmmu_vmsa_device { struct ipmmu_vmsa_domain { struct ipmmu_vmsa_device *mmu; + struct ipmmu_vmsa_device *root; struct iommu_domain io_domain; struct io_pgtable_cfg cfg; @@ -203,6 +206,44 @@ static struct ipmmu_vmsa_iommu_priv *to_ #define IMUASID_ASID0_SHIFT 0 /* ----------------------------------------------------------------------------- + * Root device handling + */ + +static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu) +{ + if (mmu->features->has_cache_leaf_nodes) + return mmu->is_leaf ? false : true; + else + return true; /* older IPMMU hardware treated as single root */ +} + +static struct ipmmu_vmsa_device *__ipmmu_find_root(void) +{ + struct ipmmu_vmsa_device *mmu; + struct ipmmu_vmsa_device *root = NULL; + + spin_lock(&ipmmu_devices_lock); + + list_for_each_entry(mmu, &ipmmu_devices, list) { + if (ipmmu_is_root(mmu)) { + root = mmu; + break; + } + } + + spin_unlock(&ipmmu_devices_lock); + return root; +} + +static struct ipmmu_vmsa_device *ipmmu_find_root(struct ipmmu_vmsa_device *leaf) +{ + if (ipmmu_is_root(leaf)) + return leaf; + else + return __ipmmu_find_root(); +} + +/* ----------------------------------------------------------------------------- * Read/Write Access */ @@ -219,13 +260,13 @@ static void ipmmu_write(struct ipmmu_vms static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg) { - return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg); + return ipmmu_read(domain->root, domain->context_id * IM_CTX_SIZE + reg); } static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg, u32 data) { - ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data); + ipmmu_write(domain->root, domain->context_id * IM_CTX_SIZE + reg, data); } /* ----------------------------------------------------------------------------- @@ -360,7 +401,7 @@ static int ipmmu_domain_init_context(str * TODO: Add support for coherent walk through CCI with DVM and remove * cache handling. For now, delegate it to the io-pgtable code. */ - domain->cfg.iommu_dev = domain->mmu->dev; + domain->cfg.iommu_dev = domain->root->dev; domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, domain); @@ -370,7 +411,7 @@ static int ipmmu_domain_init_context(str /* * Find an unused context. */ - ret = ipmmu_domain_allocate_context(domain->mmu, domain); + ret = ipmmu_domain_allocate_context(domain->root, domain); if (ret == IPMMU_CTX_MAX) { free_io_pgtable_ops(domain->iop); return -EBUSY; @@ -441,7 +482,7 @@ static void ipmmu_domain_destroy_context */ ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH); ipmmu_tlb_sync(domain); - ipmmu_domain_free_context(domain->mmu, domain->context_id); + ipmmu_domain_free_context(domain->root, domain->context_id); } /* ----------------------------------------------------------------------------- @@ -555,7 +596,7 @@ static int ipmmu_attach_device(struct io { struct ipmmu_vmsa_iommu_priv *priv = to_priv(dev); struct iommu_fwspec *fwspec = dev->iommu_fwspec; - struct ipmmu_vmsa_device *mmu = priv->mmu; + struct ipmmu_vmsa_device *root, *mmu = priv->mmu; struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); unsigned long flags; unsigned int i; @@ -566,11 +607,18 @@ static int ipmmu_attach_device(struct io return -ENXIO; } + root = ipmmu_find_root(priv->mmu); + if (!root) { + dev_err(dev, "Unable to locate root IPMMU\n"); + return -EAGAIN; + } + spin_lock_irqsave(&domain->lock, flags); if (!domain->mmu) { /* The domain hasn't been used yet, initialize it. */ domain->mmu = mmu; + domain->root = root; ret = ipmmu_domain_init_context(domain); } else if (domain->mmu != mmu) { /* @@ -911,6 +959,7 @@ static void ipmmu_device_reset(struct ip static const struct ipmmu_features ipmmu_features_default = { .use_ns_alias_offset = true, + .has_cache_leaf_nodes = false, }; static const struct of_device_id ipmmu_of_ids[] = { @@ -965,19 +1014,31 @@ static int ipmmu_probe(struct platform_d mmu->base += IM_NS_ALIAS_OFFSET; irq = platform_get_irq(pdev, 0); - if (irq < 0) { - dev_err(&pdev->dev, "no IRQ found\n"); - return irq; - } - ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0, - dev_name(&pdev->dev), mmu); - if (ret < 0) { - dev_err(&pdev->dev, "failed to request IRQ %d\n", irq); - return ret; - } + /* + * Determine if this IPMMU instance is a leaf device by checking + * if the renesas,ipmmu-main property exists or not. + */ + if (mmu->features->has_cache_leaf_nodes && + of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL)) + mmu->is_leaf = true; + + /* Root devices have mandatory IRQs */ + if (ipmmu_is_root(mmu)) { + if (irq < 0) { + dev_err(&pdev->dev, "no IRQ found\n"); + return irq; + } - ipmmu_device_reset(mmu); + ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0, + dev_name(&pdev->dev), mmu); + if (ret < 0) { + dev_err(&pdev->dev, "failed to request IRQ %d\n", irq); + return ret; + } + + ipmmu_device_reset(mmu); + } ret = iommu_device_register(&mmu->iommu); if (ret)