From patchwork Wed Dec 9 13:58:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 7808901 Return-Path: X-Original-To: patchwork-linux-media@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 966A89F39B for ; Wed, 9 Dec 2015 13:58:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CDF3D20462 for ; Wed, 9 Dec 2015 13:58:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B23C220457 for ; Wed, 9 Dec 2015 13:58:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754865AbbLIN6q (ORCPT ); Wed, 9 Dec 2015 08:58:46 -0500 Received: from mailout1.w1.samsung.com ([210.118.77.11]:63168 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754438AbbLIN6i (ORCPT ); Wed, 9 Dec 2015 08:58:38 -0500 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NZ300LOPG5N7280@mailout1.w1.samsung.com>; Wed, 09 Dec 2015 13:58:35 +0000 (GMT) X-AuditID: cbfec7f4-f79026d00000418a-5e-5668338b8019 Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 91.26.16778.B8338665; Wed, 9 Dec 2015 13:58:35 +0000 (GMT) Received: from amdc1339.digital.local ([106.116.147.30]) by eusync2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0NZ300CXZG5IXP20@eusync2.samsung.com>; Wed, 09 Dec 2015 13:58:35 +0000 (GMT) From: Marek Szyprowski To: linux-media@vger.kernel.org, linux-samsung-soc@vger.kernel.org Cc: Marek Szyprowski , devicetree@vger.kernel.org, Sylwester Nawrocki , Kamil Debski , Laurent Pinchart , Andrzej Hajda , Kukjin Kim , Krzysztof Kozlowski , Bartlomiej Zolnierkiewicz , Rob Herring , Frank Rowand , Grant Likely Subject: [PATCH v2 3/7] of: reserved_mem: add support for named reserved mem nodes Date: Wed, 09 Dec 2015 14:58:18 +0100 Message-id: <1449669502-24601-4-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.9.2 In-reply-to: <1449669502-24601-1-git-send-email-m.szyprowski@samsung.com> References: <1449669502-24601-1-git-send-email-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrFLMWRmVeSWpSXmKPExsVy+t/xK7rdxhlhBq9uqljcWneO1WLjjPWs FvOPAFkz3/xnszjwZwejxY/XF9gsXr8wtOh//JrZonPiEnaLng1bWS1mnN/HZLH2yF12i9a9 R9gtDr9pZ3Xg89g56y67x+yOmawem1Z1snncubaHzaNvyypGj8+b5ALYorhsUlJzMstSi/Tt Ergy3p6ex1Yw06Li82KhBsa1el2MnBwSAiYS3SuvMEHYYhIX7q1n62Lk4hASWMooMevgIlYI p4lJYvanS8wgVWwChhJdb7vYQGwRASeJhbP+soMUMQv8ZZZYuPUZWJGwQIjErxOvWUBsFgFV iZ8b7oGt4BXwkFjz9x0LxDo5if8vVwDFOTg4BTwlXh0GaxUCKnn/ezLzBEbeBYwMqxhFU0uT C4qT0nMN9YoTc4tL89L1kvNzNzFCQvTLDsbFx6wOMQpwMCrx8F5wSQ8TYk0sK67MPcQowcGs JMJ7Qj8jTIg3JbGyKrUoP76oNCe1+BCjNAeLkjjv3F3vQ4QE0hNLUrNTUwtSi2CyTBycUg2M aj2zQi/P6jTnEvwgZ3nm8ptTXC/uTn283Pjgyd6A7Rba61Y4zvu9ff/3OYfM0/f28/AJX+l+ Z/Hdn6e8y1pKI3v687yG9T+0vzC7pPm/X+X5j09z1+7qG74W11y7j1i0NStGKXeVz2vXV1J/ nqzD5+0ZK5Wlbd3B2OZidb1o3ob0ae3fN7IpsRRnJBpqMRcVJwIA+eyCT00CAAA= Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 This patch allows device drivers to initialize more than one reserved memory region assigned to given device. When driver needs to use more than one reserved memory region, it should allocate child devices and initialize regions by index or name for each of its child devices. Signed-off-by: Marek Szyprowski --- drivers/of/of_reserved_mem.c | 104 ++++++++++++++++++++++++++++++++-------- include/linux/of_reserved_mem.h | 31 ++++++++++-- 2 files changed, 112 insertions(+), 23 deletions(-) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index be77e75..a583480 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -21,6 +21,7 @@ #include #include #include +#include #define MAX_RESERVED_REGIONS 16 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; @@ -281,53 +282,116 @@ static inline struct reserved_mem *__find_rmem(struct device_node *node) return NULL; } +struct rmem_assigned_device { + struct device *dev; + struct reserved_mem *rmem; + struct list_head list; +}; + +static LIST_HEAD(of_rmem_assigned_device_list); +static DEFINE_MUTEX(of_rmem_assigned_device_mutex); + /** * of_reserved_mem_device_init() - assign reserved memory region to given device + * @dev: Pointer to the device to configure + * @np: Pointer to the device_node with 'reserved-memory' property + * @idx: Index of selected region + * + * This function assigns respective DMA-mapping operations based on reserved + * memory region specified by 'memory-region' property in @np node to the @dev + * device. When driver needs to use more than one reserved memory region, it + * should allocate child devices and initialize regions by name for each of + * child device. * - * This function assign memory region pointed by "memory-region" device tree - * property to the given device. + * Returns error code or zero on success. */ -int of_reserved_mem_device_init(struct device *dev) +int of_reserved_mem_init(struct device *dev, struct device_node *np, int idx) { + struct rmem_assigned_device *rd; + struct device_node *target; struct reserved_mem *rmem; - struct device_node *np; int ret; - np = of_parse_phandle(dev->of_node, "memory-region", 0); - if (!np) - return -ENODEV; + if (!np || !dev) + return -EINVAL; + + target = of_parse_phandle(np, "memory-region", idx); + if (!target) + return -EINVAL; - rmem = __find_rmem(np); - of_node_put(np); + rmem = __find_rmem(target); + of_node_put(target); if (!rmem || !rmem->ops || !rmem->ops->device_init) return -EINVAL; + rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL); + if (!rd) + return -ENOMEM; + ret = rmem->ops->device_init(rmem, dev); - if (ret == 0) + if (ret == 0) { + rd->dev = dev; + rd->rmem = rmem; + + mutex_lock(&of_rmem_assigned_device_mutex); + list_add(&rd->list, &of_rmem_assigned_device_list); + mutex_unlock(&of_rmem_assigned_device_mutex); + dev_info(dev, "assigned reserved memory node %s\n", rmem->name); + } else { + kfree(rd); + } return ret; } -EXPORT_SYMBOL_GPL(of_reserved_mem_device_init); +EXPORT_SYMBOL_GPL(of_reserved_mem_init); + +/** + * of_reserved_mem_device_init() - assign reserved memory region to given device + * @dev: Pointer to the device to configure + * @np: Pointer to the device_node with 'reserved-memory' property + * @name: Name of the selected region + * + * This function assigns respective DMA-mapping operations based on reserved + * memory region specified by 'memory-region' property in @np node, named @name + * to the @dev device. + * + * Returns error code or zero on success. + */ +int of_reserved_mem_init_by_name(struct device *dev, struct device_node *np, + const char *name) +{ + int idx = of_property_match_string(np, "memory-region-names", name); + + if (idx < 0) + return -EINVAL; + return of_reserved_mem_init(dev, np, idx); +} +EXPORT_SYMBOL_GPL(of_reserved_mem_init_by_name); /** * of_reserved_mem_device_release() - release reserved memory device structures + * @dev: Pointer to the device to deconfigure * * This function releases structures allocated for memory region handling for * the given device. */ void of_reserved_mem_device_release(struct device *dev) { - struct reserved_mem *rmem; - struct device_node *np; - - np = of_parse_phandle(dev->of_node, "memory-region", 0); - if (!np) - return; - - rmem = __find_rmem(np); - of_node_put(np); + struct rmem_assigned_device *rd; + struct reserved_mem *rmem = NULL; + + mutex_lock(&of_rmem_assigned_device_mutex); + list_for_each_entry(rd, &of_rmem_assigned_device_list, list) { + if (rd->dev == dev) { + rmem = rd->rmem; + list_del(&rd->list); + kfree(rd); + break; + } + } + mutex_unlock(&of_rmem_assigned_device_mutex); if (!rmem || !rmem->ops || !rmem->ops->device_release) return; diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h index ad2f670..0b928ad 100644 --- a/include/linux/of_reserved_mem.h +++ b/include/linux/of_reserved_mem.h @@ -1,7 +1,8 @@ #ifndef __OF_RESERVED_MEM_H #define __OF_RESERVED_MEM_H -struct device; +#include + struct of_phandle_args; struct reserved_mem_ops; @@ -28,14 +29,23 @@ typedef int (*reservedmem_of_init_fn)(struct reserved_mem *rmem); _OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn) #ifdef CONFIG_OF_RESERVED_MEM -int of_reserved_mem_device_init(struct device *dev); + +int of_reserved_mem_init(struct device *dev, struct device_node *np, int idx); +int of_reserved_mem_init_by_name(struct device *dev, struct device_node *np, + const char *name); void of_reserved_mem_device_release(struct device *dev); void fdt_init_reserved_mem(void); void fdt_reserved_mem_save_node(unsigned long node, const char *uname, phys_addr_t base, phys_addr_t size); #else -static inline int of_reserved_mem_device_init(struct device *dev) +static inline int of_reserved_mem_init(struct device *dev, + struct device_node *np, int idx) +{ + return -ENOSYS; +} +static inline int of_reserved_mem_init_by_name(struct device *dev, + struct device_node *np, const char *name) { return -ENOSYS; } @@ -46,4 +56,19 @@ static inline void fdt_reserved_mem_save_node(unsigned long node, const char *uname, phys_addr_t base, phys_addr_t size) { } #endif +/** + * of_reserved_mem_device_init() - assign reserved memory region to given device + * @dev: Pointer to the device to configure + * + * This function assigns respective DMA-mapping operations based on the first + * reserved memory region specified by 'memory-region' property in device tree + * node of the given device. + * + * Returns error code or zero on success. + */ +static inline int of_reserved_mem_device_init(struct device *dev) +{ + return of_reserved_mem_init(dev, dev->of_node, 0); +} + #endif /* __OF_RESERVED_MEM_H */