Linux 4.16-rc2
[linux-2.6-block.git] / arch / metag / kernel / tcm.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2010 Imagination Technologies Ltd.
4  */
5
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>
16
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;
23 };
24
25 static LIST_HEAD(pool_list);
26
27 static struct tcm_pool *find_pool(unsigned int tag)
28 {
29         struct list_head *lh;
30         struct tcm_pool *pool;
31
32         list_for_each(lh, &pool_list) {
33                 pool = list_entry(lh, struct tcm_pool, list);
34                 if (pool->tag == tag)
35                         return pool;
36         }
37
38         return NULL;
39 }
40
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
45  *
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.
49  */
50 unsigned long tcm_alloc(unsigned int tag, size_t len)
51 {
52         unsigned long vaddr;
53         struct tcm_pool *pool;
54
55         pool = find_pool(tag);
56         if (!pool)
57                 return 0;
58
59         vaddr = gen_pool_alloc(pool->pool, len);
60         if (!vaddr)
61                 return 0;
62
63         return vaddr;
64 }
65
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
71  *
72  * Free the requested number of bytes at a specific address to the
73  * pool matching the specified tag.
74  */
75 void tcm_free(unsigned int tag, unsigned long addr, size_t len)
76 {
77         struct tcm_pool *pool;
78
79         pool = find_pool(tag);
80         if (!pool)
81                 return;
82         gen_pool_free(pool->pool, addr, len);
83 }
84
85 /**
86  * tcm_lookup_tag - find the tag matching an address
87  * @p: memory address to lookup the tag for
88  *
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.
92  */
93 unsigned int tcm_lookup_tag(unsigned long p)
94 {
95         struct list_head *lh;
96         struct tcm_pool *pool;
97         unsigned long addr = (unsigned long) p;
98
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;
103         }
104
105         return TCM_INVALID_TAG;
106 }
107
108 /**
109  * tcm_add_region - add a memory region to TCM pool list
110  * @reg: descriptor of region to be added
111  *
112  * Add a region of memory to the TCM pool list. Returns 0 on success.
113  */
114 int __init tcm_add_region(struct tcm_region *reg)
115 {
116         struct tcm_pool *pool;
117
118         pool = kmalloc(sizeof(*pool), GFP_KERNEL);
119         if (!pool) {
120                 pr_err("Failed to alloc memory for TCM pool!\n");
121                 return -ENOMEM;
122         }
123
124         pool->tag = reg->tag;
125         pool->start = reg->res.start;
126         pool->end = reg->res.end;
127
128         /*
129          * 2^3 = 8 bytes granularity to allow for 64bit access alignment.
130          * -1 = NUMA node specifier.
131          */
132         pool->pool = gen_pool_create(3, -1);
133
134         if (!pool->pool) {
135                 pr_err("Failed to create TCM pool!\n");
136                 kfree(pool);
137                 return -ENOMEM;
138         }
139
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;
144         }
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);
148
149         list_add_tail(&pool->list, &pool_list);
150
151         return 0;
152 }