linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Philipp Zabel <p.zabel@pengutronix.de>
To: linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	Grant Likely <grant.likely@secretlab.ca>,
	Rob Herring <rob.herring@calxeda.com>,
	Paul Gortmaker <paul.gortmaker@windriver.com>,
	Shawn Guo <shawn.guo@linaro.org>,
	Richard Zhao <richard.zhao@freescale.com>,
	Huang Shijie <shijie8@gmail.com>,
	Dong Aisheng <dong.aisheng@linaro.org>,
	kernel@pengutronix.de, devicetree-discuss@lists.ozlabs.org
Cc: Philipp Zabel <p.zabel@pengutronix.de>
Subject: [PATCH v2 5/9] genalloc: add a global pool list, allow to find pools by phys address
Date: Fri, 31 Aug 2012 11:27:00 +0200	[thread overview]
Message-ID: <1346405224-20399-6-git-send-email-p.zabel@pengutronix.de> (raw)
In-Reply-To: <1346405224-20399-1-git-send-email-p.zabel@pengutronix.de>

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 <p.zabel@pengutronix.de>
---
 include/linux/genalloc.h |   14 +++++++++
 lib/genalloc.c           |   77 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)

diff --git a/include/linux/genalloc.h b/include/linux/genalloc.h
index 5e98eeb..46ff435 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);
+
+#ifdef CONFIG_OF
+struct device_node;
+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..df2d8f9 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -34,6 +34,11 @@
 #include <linux/rculist.h>
 #include <linux/interrupt.h>
 #include <linux/genalloc.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+
+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,69 @@ 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 the physical address in
+ * @np: device node
+ *
+ * Returns the pool that contains the chunk starting at the physical
+ * address of the np device node, or NULL if not found.
+ */
+struct gen_pool *of_get_named_gen_pool(struct device_node *np,
+	const char *propname, int index)
+{
+	struct property *prop;
+	const __be32 *list;
+	int size;
+	phandle phandle;
+	struct device_node *np_pool;
+	const u32 *reg;
+	u64 addr;
+
+	prop = of_find_property(np, propname, &size);
+	if (!prop)
+		return NULL;
+	list = prop->value;
+	size /= sizeof(*list);
+	phandle = be32_to_cpup(list);
+	np_pool = of_find_node_by_phandle(phandle);
+	if (!np_pool)
+		return NULL;
+	reg = of_get_property(np_pool, "reg", NULL);
+	if (!reg)
+		return NULL;
+	addr = of_translate_address(np_pool, reg);
+	if (addr == OF_BAD_ADDR)
+		return NULL;
+	return gen_pool_find_by_phys((phys_addr_t) addr);
+}
+EXPORT_SYMBOL_GPL(of_get_named_gen_pool);
+#endif /* CONFIG_OF */
-- 
1.7.10.4


  parent reply	other threads:[~2012-08-31  9:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-31  9:26 [PATCH v2 0/9] Add device tree support for on-chip SRAM Philipp Zabel
2012-08-31  9:26 ` [PATCH v2 1/9] ARM i.MX: Switch IRAM allocator to device tree initialization Philipp Zabel
2012-08-31  9:26 ` [PATCH v2 2/9] ARM i.MX53: Add OCRAM to device tree Philipp Zabel
2012-08-31  9:26 ` [PATCH v2 3/9] ARM i.MX6: " Philipp Zabel
2012-08-31  9:26 ` [PATCH v2 4/9] iram_alloc: store the virt and phys mem address in gen_pool chunks Philipp Zabel
2012-08-31  9:27 ` Philipp Zabel [this message]
2012-09-03  2:09   ` [PATCH v2 5/9] genalloc: add a global pool list, allow to find pools by phys address Shawn Guo
2012-08-31  9:27 ` [PATCH v2 6/9] misc: Generic on-chip SRAM allocation driver Philipp Zabel
2012-08-31  9:37   ` Jan Lübbe
2012-08-31  9:40     ` Shilimkar, Santosh
2012-09-03  2:19   ` Shawn Guo
2012-08-31  9:27 ` [PATCH v2 7/9] ARM: i.MX53: use generic on-chip SRAM allocator driver for OCRAM Philipp Zabel
2012-08-31  9:27 ` [PATCH v2 8/9] ARM: i.MX6: " Philipp Zabel
2012-08-31  9:27 ` [PATCH v2 9/9] ARM i.MX: remove IRAM_ALLOC facility Philipp Zabel
2012-09-03  1:53 ` [PATCH v2 0/9] Add device tree support for on-chip SRAM Shawn Guo
2012-09-03 15:42   ` Philipp Zabel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1346405224-20399-6-git-send-email-p.zabel@pengutronix.de \
    --to=p.zabel@pengutronix.de \
    --cc=devicetree-discuss@lists.ozlabs.org \
    --cc=dong.aisheng@linaro.org \
    --cc=grant.likely@secretlab.ca \
    --cc=kernel@pengutronix.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paul.gortmaker@windriver.com \
    --cc=richard.zhao@freescale.com \
    --cc=rob.herring@calxeda.com \
    --cc=shawn.guo@linaro.org \
    --cc=shijie8@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).