From patchwork Mon Jun 6 03:58:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 9157173 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 9F60960467 for ; Mon, 6 Jun 2016 04:09:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 957BE281DB for ; Mon, 6 Jun 2016 04:09:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 87F782821D; Mon, 6 Jun 2016 04:09:02 +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 1421927B13 for ; Mon, 6 Jun 2016 04:09:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753159AbcFFEIf (ORCPT ); Mon, 6 Jun 2016 00:08:35 -0400 Received: from mail-pa0-f67.google.com ([209.85.220.67]:33095 "EHLO mail-pa0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753138AbcFFEIc (ORCPT ); Mon, 6 Jun 2016 00:08:32 -0400 Received: by mail-pa0-f67.google.com with SMTP id di3so10837106pab.0; Sun, 05 Jun 2016 21:08:27 -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=aMAXEvRG3dzOCK1eIG6t25JRYvMEzkGfLaaTLSzoJqc=; b=NYTiy9YC3bAqwAgw2as5+viJXQ8WxG6KlRJB4oKhAkxcB6RFEdIoMiJYHdHmqihc0p qmFB/hncAjpQesrHPv9UrwnJ+O6iNTnlfgZGwoRYu6m9qIWuMuYrekw1Ad3kTH7CEyvc xUJUfIrwYmFz2Ytbe6h4Wg8Gh1Kd26193yesVaJT1xIR675EQjyMRv/Kpejx/B35WQ9O ESgpLHSMwaohmwBqtvQFglTe4cIbejdK7Oe8agmrJUFPmJKTUV/FyD/G5bSD5aEkJ+T6 x/dh8LJMpBWy7MhXFQl6WZkH+D7HinGzr0XKvrQyLQnmz2oBWmKrEaDEgDYPhjUwHYi6 0fAg== 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=aMAXEvRG3dzOCK1eIG6t25JRYvMEzkGfLaaTLSzoJqc=; b=hF3pK6bK2q1jJ7ocBmHxLEopj0245iXNgonbKXdNkOTzuBVhW8wrnvTX8r+rhBsUmt swE3OdS/9hbFG4jhaqkaOziKsIRT1JwWoZlofgvkwgyGZcwvl/XHF5jgdzIIPMtWrxqH J3tPVIXsJcHj5XVQHfVYvJC92/tXVisjs394VnKBcRCNzg+fJgSWVad98tw6ryjBIK1O QKrG1dJ+uhsfA9DP2ZC/ycG3COPbzSvaJ23L+2uYxsD7G72n41MFlVpllFCJ9nGKy0Kb hSS+xaQzPq6smYxUr9/cD6NcLXeo3fGvGqiu7Vh4nCPx2yoh+ZYNinI7ZkI3/KydCaXG EMzA== X-Gm-Message-State: ALyK8tJIe/yFPD4XNm7j0QXYuz24j3HBPpiZqbyIwqkcSxvKk+R+zJgi+xIV6e4rXjVNqg== X-Received: by 10.66.222.98 with SMTP id ql2mr22161345pac.137.1465186101662; Sun, 05 Jun 2016 21:08:21 -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 l67sm23448095pfi.10.2016.06.05.21.08.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Jun 2016 21:08:20 -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:58:01 +0900 Message-Id: <20160606035801.31763.40763.sendpatchset@little-apple> In-Reply-To: <20160606035723.31763.66500.sendpatchset@little-apple> References: <20160606035723.31763.66500.sendpatchset@little-apple> Subject: [PATCH v2 04/11] iommu/ipmmu-vmsa: Reuse iommu groups 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 Extend the IPMMU driver to group devices together based on the IPMMU device they are connected to. The slave devices are kept on a list which is used to determine if existing groups (and contexts) exist. With this patch the two SYS-DMAC devices using IPMMU-DS0 on r8a7795 will be grouped together. Signed-off-by: Magnus Damm --- Changes since V1: - New patch drivers/iommu/ipmmu-vmsa.c | 68 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 8 deletions(-) --- 0013/drivers/iommu/ipmmu-vmsa.c +++ work/drivers/iommu/ipmmu-vmsa.c 2016-06-06 10:55:32.340607110 +0900 @@ -69,11 +69,16 @@ struct ipmmu_vmsa_archdata { struct ipmmu_vmsa_device *mmu; unsigned int *utlbs; unsigned int num_utlbs; + struct device *dev; + struct list_head list; }; static DEFINE_SPINLOCK(ipmmu_devices_lock); static LIST_HEAD(ipmmu_devices); +static DEFINE_SPINLOCK(ipmmu_slave_devices_lock); +static LIST_HEAD(ipmmu_slave_devices); + static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom) { return container_of(dom, struct ipmmu_vmsa_domain, io_domain); @@ -619,6 +624,9 @@ static int ipmmu_attach_device(struct io dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n", dev_name(mmu->dev), dev_name(domain->mmu->dev)); ret = -EINVAL; + } else { + dev_info(dev, "Reusing IPMMU context %u\n", + domain->context_id); } spin_unlock_irqrestore(&domain->lock, flags); @@ -676,6 +684,42 @@ static phys_addr_t ipmmu_iova_to_phys(st return domain->iop->iova_to_phys(domain->iop, iova); } +static struct device *ipmmu_find_sibling_device(struct device *dev) +{ + struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu; + struct ipmmu_vmsa_archdata *sibling_archdata = NULL; + bool found = false; + + spin_lock(&ipmmu_slave_devices_lock); + + list_for_each_entry(sibling_archdata, &ipmmu_slave_devices, list) { + if (archdata == sibling_archdata) + continue; + if (sibling_archdata->mmu == archdata->mmu) { + found = true; + break; + } + } + + spin_unlock(&ipmmu_slave_devices_lock); + + return found ? sibling_archdata->dev : NULL; +} + +static struct iommu_group *ipmmu_find_group(struct device *dev) +{ + struct iommu_group *group; + struct device *sibling; + + sibling = ipmmu_find_sibling_device(dev); + if (sibling) + group = iommu_group_get(sibling); + if (!sibling || IS_ERR(group)) + group = generic_device_group(dev); + + return group; +} + static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev, unsigned int *utlbs, unsigned int num_utlbs) { @@ -756,6 +800,7 @@ static int ipmmu_init_platform_device(st archdata->mmu = mmu; archdata->utlbs = utlbs; archdata->num_utlbs = num_utlbs; + archdata->dev = dev; dev->archdata.iommu = archdata; return 0; @@ -908,6 +953,7 @@ static void ipmmu_domain_free_dma(struct static int ipmmu_add_device_dma(struct device *dev) { + struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu; struct iommu_group *group; /* only accept devices with iommus property */ @@ -919,11 +965,21 @@ static int ipmmu_add_device_dma(struct d if (IS_ERR(group)) return PTR_ERR(group); + archdata = dev->archdata.iommu; + spin_lock(&ipmmu_slave_devices_lock); + list_add(&archdata->list, &ipmmu_slave_devices); + spin_unlock(&ipmmu_slave_devices_lock); return 0; } static void ipmmu_remove_device_dma(struct device *dev) { + struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu; + + spin_lock(&ipmmu_slave_devices_lock); + list_del(&archdata->list); + spin_unlock(&ipmmu_slave_devices_lock); + iommu_group_remove_device(dev); } @@ -932,15 +988,11 @@ static struct iommu_group *ipmmu_device_ struct iommu_group *group; int ret; - group = generic_device_group(dev); - if (IS_ERR(group)) - return group; - - ret = ipmmu_init_platform_device(dev, group); - if (ret) { - iommu_group_put(group); + ret = ipmmu_init_platform_device(dev, NULL); + if (!ret) + group = ipmmu_find_group(dev); + else group = ERR_PTR(ret); - } return group; }