From patchwork Fri Sep 7 12:43:43 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Zabel X-Patchwork-Id: 1422021 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork2.kernel.org (Postfix) with ESMTP id 9BD17DF283 for ; Fri, 7 Sep 2012 12:48:40 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1T9xw6-0005sX-TO; Fri, 07 Sep 2012 12:45:27 +0000 Received: from metis.ext.pengutronix.de ([2001:6f8:1178:4:290:27ff:fe1d:cc33]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1T9xuo-0005IY-Kr for linux-arm-kernel@lists.infradead.org; Fri, 07 Sep 2012 12:44:08 +0000 Received: from dude.hi.pengutronix.de ([10.1.0.7] helo=dude.pengutronix.de) by metis.ext.pengutronix.de with esmtp (Exim 4.72) (envelope-from ) id 1T9xuc-0001ec-VE; Fri, 07 Sep 2012 14:43:54 +0200 From: Philipp Zabel To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Grant Likely , Rob Herring , Paul Gortmaker , Shawn Guo , Richard Zhao , Huang Shijie , Dong Aisheng , kernel@pengutronix.de, devicetree-discuss@lists.ozlabs.org Subject: [PATCH v4 1/6] genalloc: add a global pool list, allow to find pools by phys address Date: Fri, 7 Sep 2012 14:43:43 +0200 Message-Id: <1347021828-23034-2-git-send-email-p.zabel@pengutronix.de> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1347021828-23034-1-git-send-email-p.zabel@pengutronix.de> References: <1347021828-23034-1-git-send-email-p.zabel@pengutronix.de> X-SA-Exim-Connect-IP: 10.1.0.7 X-SA-Exim-Mail-From: p.zabel@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-arm-kernel@lists.infradead.org X-Spam-Note: CRM114 invocation failed X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Philipp Zabel X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org This patch keeps all created pools in a global list and adds two functions that allow to retrieve the gen_pool pointer from a known physical address and from a device tree node. Signed-off-by: Philipp Zabel Reviewed-by: Shawn Guo --- include/linux/genalloc.h | 14 ++++++++++ lib/genalloc.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/include/linux/genalloc.h b/include/linux/genalloc.h index 5e98eeb..d498c43 100644 --- a/include/linux/genalloc.h +++ b/include/linux/genalloc.h @@ -33,6 +33,7 @@ * General purpose special memory pool descriptor. */ struct gen_pool { + struct list_head next_pool; /* pool in global list */ spinlock_t lock; struct list_head chunks; /* list of chunks in this pool */ int min_alloc_order; /* minimum allocation order */ @@ -78,4 +79,17 @@ extern void gen_pool_for_each_chunk(struct gen_pool *, void (*)(struct gen_pool *, struct gen_pool_chunk *, void *), void *); extern size_t gen_pool_avail(struct gen_pool *); extern size_t gen_pool_size(struct gen_pool *); +extern struct gen_pool *gen_pool_find_by_phys(phys_addr_t phys); + +struct device_node; +#ifdef CONFIG_OF +extern struct gen_pool *of_get_named_gen_pool(struct device_node *np, + const char *propname, int index); +#else +inline struct gen_pool *of_get_named_gen_pool(struct device_node *np, + const char *propname, int index) +{ + return NULL; +} +#endif #endif /* __GENALLOC_H__ */ diff --git a/lib/genalloc.c b/lib/genalloc.c index 6bc04aa..90699ac 100644 --- a/lib/genalloc.c +++ b/lib/genalloc.c @@ -34,6 +34,11 @@ #include #include #include +#include +#include + +static LIST_HEAD(pools); +static DEFINE_SPINLOCK(list_lock); static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set) { @@ -152,6 +157,9 @@ struct gen_pool *gen_pool_create(int min_alloc_order, int nid) spin_lock_init(&pool->lock); INIT_LIST_HEAD(&pool->chunks); pool->min_alloc_order = min_alloc_order; + spin_lock(&list_lock); + list_add_rcu(&pool->next_pool, &pools); + spin_unlock(&list_lock); } return pool; } @@ -234,6 +242,9 @@ void gen_pool_destroy(struct gen_pool *pool) int order = pool->min_alloc_order; int bit, end_bit; + spin_lock(&list_lock); + list_del_rcu(&pool->next_pool); + spin_unlock(&list_lock); list_for_each_safe(_chunk, _next_chunk, &pool->chunks) { chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk); list_del(&chunk->next_chunk); @@ -400,3 +411,59 @@ size_t gen_pool_size(struct gen_pool *pool) return size; } EXPORT_SYMBOL_GPL(gen_pool_size); + +/** + * gen_pool_find_by_phys - find a pool by physical start address + * @phys: physical address as added with gen_pool_add_virt + * + * Returns the pool that contains the chunk starting at phys, + * or NULL if not found. + */ +struct gen_pool *gen_pool_find_by_phys(phys_addr_t phys) +{ + struct gen_pool *pool, *found = NULL; + struct gen_pool_chunk *chunk; + + rcu_read_lock(); + list_for_each_entry_rcu(pool, &pools, next_pool) { + list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { + if (phys == chunk->phys_addr) { + found = pool; + break; + } + } + } + rcu_read_unlock(); + + return found; +} +EXPORT_SYMBOL_GPL(gen_pool_find_by_phys); + +#ifdef CONFIG_OF +/** + * of_get_named_gen_pool - find a pool by phandle property + * @np: device node + * @propname: property name containing phandle(s) + * @index: index into the phandle array + * + * Returns the pool that contains the chunk starting at the physical + * address of the device tree node pointed at by the phandle property, + * or NULL if not found. + */ +struct gen_pool *of_get_named_gen_pool(struct device_node *np, + const char *propname, int index) +{ + struct device_node *np_pool; + struct resource res; + int ret; + + np_pool = of_parse_phandle(np, propname, index); + if (!np_pool) + return NULL; + ret = of_address_to_resource(np_pool, 0, &res); + if (ret < 0) + return NULL; + return gen_pool_find_by_phys((phys_addr_t) res.start); +} +EXPORT_SYMBOL_GPL(of_get_named_gen_pool); +#endif /* CONFIG_OF */