2 * Copyright (C) 2010 Imagination Technologies Ltd.
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/spinlock.h>
8 #include <linux/stddef.h>
9 #include <linux/genalloc.h>
10 #include <linux/string.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
17 struct list_head list;
21 struct gen_pool *pool;
24 static LIST_HEAD(pool_list);
26 static struct tcm_pool *find_pool(unsigned int tag)
29 struct tcm_pool *pool;
31 list_for_each(lh, &pool_list) {
32 pool = list_entry(lh, struct tcm_pool, list);
41 * tcm_alloc - allocate memory from a TCM pool
42 * @tag: tag of the pool to allocate memory from
43 * @len: number of bytes to be allocated
45 * Allocate the requested number of bytes from the pool matching
46 * the specified tag. Returns the address of the allocated memory
49 unsigned long tcm_alloc(unsigned int tag, size_t len)
52 struct tcm_pool *pool;
54 pool = find_pool(tag);
58 vaddr = gen_pool_alloc(pool->pool, len);
66 * tcm_free - free a block of memory to a TCM pool
67 * @tag: tag of the pool to free memory to
68 * @addr: address of the memory to be freed
69 * @len: number of bytes to be freed
71 * Free the requested number of bytes at a specific address to the
72 * pool matching the specified tag.
74 void tcm_free(unsigned int tag, unsigned long addr, size_t len)
76 struct tcm_pool *pool;
78 pool = find_pool(tag);
81 gen_pool_free(pool->pool, addr, len);
85 * tcm_lookup_tag - find the tag matching an address
86 * @p: memory address to lookup the tag for
88 * Find the tag of the tcm memory region that contains the
89 * specified address. Returns %TCM_INVALID_TAG if no such
90 * memory region could be found.
92 unsigned int tcm_lookup_tag(unsigned long p)
95 struct tcm_pool *pool;
96 unsigned long addr = (unsigned long) p;
98 list_for_each(lh, &pool_list) {
99 pool = list_entry(lh, struct tcm_pool, list);
100 if (addr >= pool->start && addr < pool->end)
104 return TCM_INVALID_TAG;
108 * tcm_add_region - add a memory region to TCM pool list
109 * @reg: descriptor of region to be added
111 * Add a region of memory to the TCM pool list. Returns 0 on success.
113 int __init tcm_add_region(struct tcm_region *reg)
115 struct tcm_pool *pool;
117 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
119 pr_err("Failed to alloc memory for TCM pool!\n");
123 pool->tag = reg->tag;
124 pool->start = reg->res.start;
125 pool->end = reg->res.end;
128 * 2^3 = 8 bytes granularity to allow for 64bit access alignment.
129 * -1 = NUMA node specifier.
131 pool->pool = gen_pool_create(3, -1);
134 pr_err("Failed to create TCM pool!\n");
139 if (gen_pool_add(pool->pool, reg->res.start,
140 reg->res.end - reg->res.start + 1, -1)) {
141 pr_err("Failed to add memory to TCM pool!\n");
144 pr_info("Added %s TCM pool (%08x bytes @ %08x)\n",
145 reg->res.name, reg->res.end - reg->res.start + 1,
148 list_add_tail(&pool->list, &pool_list);