1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Imagination Technologies Ltd.
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/spinlock.h>
9 #include <linux/stddef.h>
10 #include <linux/genalloc.h>
11 #include <linux/string.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
18 struct list_head list
;
22 struct gen_pool
*pool
;
25 static LIST_HEAD(pool_list
);
27 static struct tcm_pool
*find_pool(unsigned int tag
)
30 struct tcm_pool
*pool
;
32 list_for_each(lh
, &pool_list
) {
33 pool
= list_entry(lh
, struct tcm_pool
, list
);
42 * tcm_alloc - allocate memory from a TCM pool
43 * @tag: tag of the pool to allocate memory from
44 * @len: number of bytes to be allocated
46 * Allocate the requested number of bytes from the pool matching
47 * the specified tag. Returns the address of the allocated memory
50 unsigned long tcm_alloc(unsigned int tag
, size_t len
)
53 struct tcm_pool
*pool
;
55 pool
= find_pool(tag
);
59 vaddr
= gen_pool_alloc(pool
->pool
, len
);
67 * tcm_free - free a block of memory to a TCM pool
68 * @tag: tag of the pool to free memory to
69 * @addr: address of the memory to be freed
70 * @len: number of bytes to be freed
72 * Free the requested number of bytes at a specific address to the
73 * pool matching the specified tag.
75 void tcm_free(unsigned int tag
, unsigned long addr
, size_t len
)
77 struct tcm_pool
*pool
;
79 pool
= find_pool(tag
);
82 gen_pool_free(pool
->pool
, addr
, len
);
86 * tcm_lookup_tag - find the tag matching an address
87 * @p: memory address to lookup the tag for
89 * Find the tag of the tcm memory region that contains the
90 * specified address. Returns %TCM_INVALID_TAG if no such
91 * memory region could be found.
93 unsigned int tcm_lookup_tag(unsigned long p
)
96 struct tcm_pool
*pool
;
97 unsigned long addr
= (unsigned long) p
;
99 list_for_each(lh
, &pool_list
) {
100 pool
= list_entry(lh
, struct tcm_pool
, list
);
101 if (addr
>= pool
->start
&& addr
< pool
->end
)
105 return TCM_INVALID_TAG
;
109 * tcm_add_region - add a memory region to TCM pool list
110 * @reg: descriptor of region to be added
112 * Add a region of memory to the TCM pool list. Returns 0 on success.
114 int __init
tcm_add_region(struct tcm_region
*reg
)
116 struct tcm_pool
*pool
;
118 pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
120 pr_err("Failed to alloc memory for TCM pool!\n");
124 pool
->tag
= reg
->tag
;
125 pool
->start
= reg
->res
.start
;
126 pool
->end
= reg
->res
.end
;
129 * 2^3 = 8 bytes granularity to allow for 64bit access alignment.
130 * -1 = NUMA node specifier.
132 pool
->pool
= gen_pool_create(3, -1);
135 pr_err("Failed to create TCM pool!\n");
140 if (gen_pool_add(pool
->pool
, reg
->res
.start
,
141 reg
->res
.end
- reg
->res
.start
+ 1, -1)) {
142 pr_err("Failed to add memory to TCM pool!\n");
145 pr_info("Added %s TCM pool (%08x bytes @ %08x)\n",
146 reg
->res
.name
, reg
->res
.end
- reg
->res
.start
+ 1,
149 list_add_tail(&pool
->list
, &pool_list
);