From patchwork Tue Dec 15 13:06:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magnus Damm X-Patchwork-Id: 7854601 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 80D559F349 for ; Tue, 15 Dec 2015 13:06:29 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6829120304 for ; Tue, 15 Dec 2015 13:06:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 44B9E2038E for ; Tue, 15 Dec 2015 13:06:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933340AbbLONEg (ORCPT ); Tue, 15 Dec 2015 08:04:36 -0500 Received: from mail-pf0-f170.google.com ([209.85.192.170]:35683 "EHLO mail-pf0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932176AbbLONEf (ORCPT ); Tue, 15 Dec 2015 08:04:35 -0500 Received: by mail-pf0-f170.google.com with SMTP id 63so4736251pff.2; Tue, 15 Dec 2015 05:04:34 -0800 (PST) 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=Clnobs/A3nOZv13uprOVuzRBLivWiZV3AwUSVuWXItU=; b=zf3PJWvpP4yNimo7S7tUhiPZ7fzl1CdqxUeZh8SNDWiOOz2mkSW3HAo6tOjD4h8bt4 UOrouscqpCvptgutTbK+3cHzrnuCntRtSTQ6INoic5LCoMDWv5SFB2kv1oiI5iJVyKzx Zg3MtzJ6EyJjxek9+icZcFfK+SJ4d9Ok7LVgpf54jjP5d65ZRxu9cEZiOg9qg/wuINVp ym3OTGKjRNga3nYHciM5nEwo5761Q+h6vQV5T6di9qrgj4tGWHbuDm1ik+3rOWjonrhD ooHbiAiWonU+JRUbrL5bWjvBDMxQyxriOknHmr9QWjZWy7C//ZKh3qPjWjP2NJkd4/L4 TFzQ== X-Received: by 10.98.13.200 with SMTP id 69mr44505484pfn.165.1450184674499; Tue, 15 Dec 2015 05:04:34 -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 f63sm2350016pfj.35.2015.12.15.05.04.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Dec 2015 05:04:33 -0800 (PST) From: Magnus Damm To: iommu@lists.linux-foundation.org Cc: laurent.pinchart+renesas@ideasonboard.com, geert+renesas@glider.be, linux-sh@vger.kernel.org, joro@8bytes.org, linux-kernel@vger.kernel.org, horms+renesas@verge.net.au, Magnus Damm Date: Tue, 15 Dec 2015 22:06:14 +0900 Message-Id: <20151215130614.13535.5206.sendpatchset@little-apple> In-Reply-To: <20151215130508.13535.26186.sendpatchset@little-apple> References: <20151215130508.13535.26186.sendpatchset@little-apple> Subject: [PATCH/RFC 07/10] iommu/ipmmu-vmsa: Break out utlb parsing code Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Magnus Damm Break out the utlb parsing code and dev_data allocation into a separate function. This is preparation for future code sharing. Signed-off-by: Magnus Damm --- drivers/iommu/ipmmu-vmsa.c | 126 ++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 56 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-sh" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- 0012/drivers/iommu/ipmmu-vmsa.c +++ work/drivers/iommu/ipmmu-vmsa.c 2015-12-14 18:56:47.020513000 +0900 @@ -607,6 +607,69 @@ static int ipmmu_find_utlbs(struct ipmmu return 0; } +static int ipmmu_init_platform_device(struct device *dev, + struct iommu_group *group) +{ + struct ipmmu_vmsa_dev_data *dev_data; + struct ipmmu_vmsa_device *mmu; + unsigned int *utlbs; + unsigned int i; + int num_utlbs; + int ret = -ENODEV; + + /* Find the master corresponding to the device. */ + + num_utlbs = of_count_phandle_with_args(dev->of_node, "iommus", + "#iommu-cells"); + if (num_utlbs < 0) + return -ENODEV; + + utlbs = kcalloc(num_utlbs, sizeof(*utlbs), GFP_KERNEL); + if (!utlbs) + return -ENOMEM; + + spin_lock(&ipmmu_devices_lock); + + list_for_each_entry(mmu, &ipmmu_devices, list) { + ret = ipmmu_find_utlbs(mmu, dev, utlbs, num_utlbs); + if (!ret) { + /* + * TODO Take a reference to the MMU to protect + * against device removal. + */ + break; + } + } + + spin_unlock(&ipmmu_devices_lock); + + if (ret < 0) + goto error; + + for (i = 0; i < num_utlbs; ++i) { + if (utlbs[i] >= mmu->num_utlbs) { + ret = -EINVAL; + goto error; + } + } + + dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); + if (!dev_data) { + ret = -ENOMEM; + goto error; + } + + dev_data->mmu = mmu; + dev_data->utlbs = utlbs; + dev_data->num_utlbs = num_utlbs; + set_dev_data(dev, dev_data); + return 0; + +error: + kfree(utlbs); + return ret; +} + #ifdef CONFIG_ARM static int ipmmu_map_attach(struct device *dev, struct ipmmu_vmsa_device *mmu) { @@ -663,56 +726,15 @@ static inline void ipmmu_release_mapping static int ipmmu_add_device(struct device *dev) { - struct ipmmu_vmsa_dev_data *dev_data = get_dev_data(dev); - struct ipmmu_vmsa_device *mmu; - struct iommu_group *group = NULL; - unsigned int *utlbs; - unsigned int i; - int num_utlbs; - int ret = -ENODEV; + struct iommu_group *group; + int ret; - if (dev_data) { + if (get_dev_data(dev)) { dev_warn(dev, "IOMMU driver already assigned to device %s\n", dev_name(dev)); return -EINVAL; } - /* Find the master corresponding to the device. */ - - num_utlbs = of_count_phandle_with_args(dev->of_node, "iommus", - "#iommu-cells"); - if (num_utlbs < 0) - return -ENODEV; - - utlbs = kcalloc(num_utlbs, sizeof(*utlbs), GFP_KERNEL); - if (!utlbs) - return -ENOMEM; - - spin_lock(&ipmmu_devices_lock); - - list_for_each_entry(mmu, &ipmmu_devices, list) { - ret = ipmmu_find_utlbs(mmu, dev, utlbs, num_utlbs); - if (!ret) { - /* - * TODO Take a reference to the MMU to protect - * against device removal. - */ - break; - } - } - - spin_unlock(&ipmmu_devices_lock); - - if (ret < 0) - return -ENODEV; - - for (i = 0; i < num_utlbs; ++i) { - if (utlbs[i] >= mmu->num_utlbs) { - ret = -EINVAL; - goto error; - } - } - /* Create a device group and add the device to it. */ group = iommu_group_alloc(); if (IS_ERR(group)) { @@ -730,27 +752,19 @@ static int ipmmu_add_device(struct devic goto error; } - dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); - if (!dev_data) { - ret = -ENOMEM; + ret = ipmmu_init_platform_device(dev, group); + if (ret < 0) { + dev_err(dev, "Failed to init platform device\n"); goto error; } - dev_data->mmu = mmu; - dev_data->utlbs = utlbs; - dev_data->num_utlbs = num_utlbs; - set_dev_data(dev, dev_data); - - ret = ipmmu_map_attach(dev, mmu); + ret = ipmmu_map_attach(dev, get_dev_data(dev)->mmu); if (ret < 0) goto error; return 0; error: - kfree(dev_data); - kfree(utlbs); - set_dev_data(dev, NULL); if (!IS_ERR_OR_NULL(group))