From patchwork Mon Jun 6 03:57:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 9157203 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 559ED60467 for ; Mon, 6 Jun 2016 04:10:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4C5D527B13 for ; Mon, 6 Jun 2016 04:10:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 40E46281D2; Mon, 6 Jun 2016 04:10:45 +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 C085727B13 for ; Mon, 6 Jun 2016 04:10:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753013AbcFFEIM (ORCPT ); Mon, 6 Jun 2016 00:08:12 -0400 Received: from mail-pa0-f65.google.com ([209.85.220.65]:34822 "EHLO mail-pa0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752991AbcFFEIJ (ORCPT ); Mon, 6 Jun 2016 00:08:09 -0400 Received: by mail-pa0-f65.google.com with SMTP id gp3so10818136pac.2; Sun, 05 Jun 2016 21:08:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:date:message-id:in-reply-to:references:subject; bh=OVLRjh3jQIPjFLqRbVBQDCtwTBORsTzTTpFYoNteRl0=; b=wCK79O7uxnvQTG41F7LSdXw6F46KXc4YccVKUM0rPAGxHWIeKY9N+4cwTR2Xx+JglX NDlhowtRh9A5UTaZxG1Yld++rrTdyOcAJ+eeX/f8Q+HpJb4fgisXBJ892feJau9xIWJg TkDKmAb62sQSGbBVj0k9sc/+s81l6kqr2D7cmH6p2ZyNF/Zm+OmVZHqPxPsCmzVEUfQ0 GeyF/YQ2l2zZ6KUpAzcXWGnvojZVJ/8ypeoiNLQOiZ2+PG51/YOM9wAi9hgdHbaupSY5 SHfy0Yp5EVppELymxLa8cKmu3ethy7TFL8JTwIT9F/b0+ef814JCb223Wusq5AT2s7rK B8iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:date:message-id:in-reply-to :references:subject; bh=OVLRjh3jQIPjFLqRbVBQDCtwTBORsTzTTpFYoNteRl0=; b=jioWLSPznjQqkt5+stE2lA/EdzY2K5CU1RH8P78az/0QE++iyIpxVN9APJoJDtM6qz vY0PAKfPLLjh3aBdnhF/cd59vlTHqyXCVRut8Iv8QIC9kxbjkiPu8gUPq50/pXkoGRB6 8pBxOSWpPhfeXdC7HZZkXej0z4A6J9izAfM6DTKxPiumCXPXm+9uCjMttqSo18uizU4A I3YHtb/oVnXol20K5NPf48r30iebBGlNVt+22yR8RMlwn9aTOABHkR71ekCcpRWnlcT1 nxUe6cVxeniOY8R+HzHYrZwXHqDbh2mLk663DCKeKTfXA0KsKF2+aiFl0bivpdQwiYsf LbQQ== X-Gm-Message-State: ALyK8tLAbIHtKad5Vpjh90OaWdKhe/ZkEN0rtDiZl3Vv0EXbspw0IjuWpmYjVJKOcPL4Pw== X-Received: by 10.66.65.133 with SMTP id x5mr22356662pas.108.1465186083738; Sun, 05 Jun 2016 21:08:03 -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 ap3sm20979868pad.33.2016.06.05.21.08.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Jun 2016 21:08:02 -0700 (PDT) From: Magnus Damm To: iommu@lists.linux-foundation.org Cc: laurent.pinchart+renesas@ideasonboard.com, geert+renesas@glider.be, joro@8bytes.org, linux-kernel@vger.kernel.org, linux-renesas-soc@vger.kernel.org, horms+renesas@verge.net.au, Magnus Damm Date: Mon, 06 Jun 2016 12:57:43 +0900 Message-Id: <20160606035743.31763.22662.sendpatchset@little-apple> In-Reply-To: <20160606035723.31763.66500.sendpatchset@little-apple> References: <20160606035723.31763.66500.sendpatchset@little-apple> Subject: [PATCH v2 02/11] 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 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 | 87 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 17 deletions(-) --- 0009/drivers/iommu/ipmmu-vmsa.c +++ work/drivers/iommu/ipmmu-vmsa.c 2016-06-06 10:11:32.130607110 +0900 @@ -34,6 +34,7 @@ struct ipmmu_features { bool use_ns_alias_offset; + bool has_cache_leaf_nodes; }; struct ipmmu_vmsa_device { @@ -41,6 +42,7 @@ struct ipmmu_vmsa_device { void __iomem *base; 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); @@ -51,6 +53,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; @@ -195,6 +198,36 @@ static struct ipmmu_vmsa_domain *to_vmsa #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(struct ipmmu_vmsa_device *leaf) +{ + struct ipmmu_vmsa_device *mmu = NULL; + + if (ipmmu_is_root(leaf)) + return leaf; + + spin_lock(&ipmmu_devices_lock); + + list_for_each_entry(mmu, &ipmmu_devices, list) { + if (ipmmu_is_root(mmu)) + break; + } + + spin_unlock(&ipmmu_devices_lock); + return mmu; +} + +/* ----------------------------------------------------------------------------- * Read/Write Access */ @@ -211,13 +244,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); } /* ----------------------------------------------------------------------------- @@ -350,7 +383,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); @@ -360,7 +393,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; @@ -431,7 +464,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); } /* ----------------------------------------------------------------------------- @@ -544,7 +577,7 @@ static int ipmmu_attach_device(struct io struct device *dev) { struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu; - struct ipmmu_vmsa_device *mmu = archdata->mmu; + struct ipmmu_vmsa_device *root, *mmu = archdata->mmu; struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); unsigned long flags; unsigned int i; @@ -555,11 +588,18 @@ static int ipmmu_attach_device(struct io return -ENXIO; } + root = ipmmu_find_root(archdata->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) { /* @@ -935,6 +975,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[] = { @@ -994,19 +1035,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); + } /* * We can't create the ARM mapping here as it requires the bus to have