Linux 4.15.10
[linux/fpc-iii.git] / arch / metag / kernel / tcm.c
blob1d7b4e33b114785388020ddc7c953f905497bf18
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2010 Imagination Technologies Ltd.
4 */
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>
14 #include <asm/page.h>
15 #include <asm/tcm.h>
17 struct tcm_pool {
18 struct list_head list;
19 unsigned int tag;
20 unsigned long start;
21 unsigned long end;
22 struct gen_pool *pool;
25 static LIST_HEAD(pool_list);
27 static struct tcm_pool *find_pool(unsigned int tag)
29 struct list_head *lh;
30 struct tcm_pool *pool;
32 list_for_each(lh, &pool_list) {
33 pool = list_entry(lh, struct tcm_pool, list);
34 if (pool->tag == tag)
35 return pool;
38 return NULL;
41 /**
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
48 * or zero on failure.
50 unsigned long tcm_alloc(unsigned int tag, size_t len)
52 unsigned long vaddr;
53 struct tcm_pool *pool;
55 pool = find_pool(tag);
56 if (!pool)
57 return 0;
59 vaddr = gen_pool_alloc(pool->pool, len);
60 if (!vaddr)
61 return 0;
63 return vaddr;
66 /**
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);
80 if (!pool)
81 return;
82 gen_pool_free(pool->pool, addr, len);
85 /**
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)
95 struct list_head *lh;
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)
102 return pool->tag;
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);
119 if (!pool) {
120 pr_err("Failed to alloc memory for TCM pool!\n");
121 return -ENOMEM;
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);
134 if (!pool->pool) {
135 pr_err("Failed to create TCM pool!\n");
136 kfree(pool);
137 return -ENOMEM;
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");
143 return -ENOMEM;
145 pr_info("Added %s TCM pool (%08x bytes @ %08x)\n",
146 reg->res.name, reg->res.end - reg->res.start + 1,
147 reg->res.start);
149 list_add_tail(&pool->list, &pool_list);
151 return 0;