]> git.ipfire.org Git - thirdparty/linux.git/blob - arch/powerpc/platforms/4xx/ocm.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156
[thirdparty/linux.git] / arch / powerpc / platforms / 4xx / ocm.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PowerPC 4xx OCM memory allocation support
4 *
5 * (C) Copyright 2009, Applied Micro Circuits Corporation
6 * Victor Gallardo (vgallardo@amcc.com)
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <asm/rheap.h>
17 #include <asm/ppc4xx_ocm.h>
18 #include <linux/slab.h>
19 #include <linux/debugfs.h>
20
21 #define OCM_DISABLED 0
22 #define OCM_ENABLED 1
23
24 struct ocm_block {
25 struct list_head list;
26 void __iomem *addr;
27 int size;
28 const char *owner;
29 };
30
31 /* non-cached or cached region */
32 struct ocm_region {
33 phys_addr_t phys;
34 void __iomem *virt;
35
36 int memtotal;
37 int memfree;
38
39 rh_info_t *rh;
40 struct list_head list;
41 };
42
43 struct ocm_info {
44 int index;
45 int status;
46 int ready;
47
48 phys_addr_t phys;
49
50 int alignment;
51 int memtotal;
52 int cache_size;
53
54 struct ocm_region nc; /* non-cached region */
55 struct ocm_region c; /* cached region */
56 };
57
58 static struct ocm_info *ocm_nodes;
59 static int ocm_count;
60
61 static struct ocm_info *ocm_get_node(unsigned int index)
62 {
63 if (index >= ocm_count) {
64 printk(KERN_ERR "PPC4XX OCM: invalid index");
65 return NULL;
66 }
67
68 return &ocm_nodes[index];
69 }
70
71 static int ocm_free_region(struct ocm_region *ocm_reg, const void *addr)
72 {
73 struct ocm_block *blk, *tmp;
74 unsigned long offset;
75
76 if (!ocm_reg->virt)
77 return 0;
78
79 list_for_each_entry_safe(blk, tmp, &ocm_reg->list, list) {
80 if (blk->addr == addr) {
81 offset = addr - ocm_reg->virt;
82 ocm_reg->memfree += blk->size;
83 rh_free(ocm_reg->rh, offset);
84 list_del(&blk->list);
85 kfree(blk);
86 return 1;
87 }
88 }
89
90 return 0;
91 }
92
93 static void __init ocm_init_node(int count, struct device_node *node)
94 {
95 struct ocm_info *ocm;
96
97 const unsigned int *cell_index;
98 const unsigned int *cache_size;
99 int len;
100
101 struct resource rsrc;
102
103 ocm = ocm_get_node(count);
104
105 cell_index = of_get_property(node, "cell-index", &len);
106 if (!cell_index) {
107 printk(KERN_ERR "PPC4XX OCM: missing cell-index property");
108 return;
109 }
110 ocm->index = *cell_index;
111
112 if (of_device_is_available(node))
113 ocm->status = OCM_ENABLED;
114
115 cache_size = of_get_property(node, "cached-region-size", &len);
116 if (cache_size)
117 ocm->cache_size = *cache_size;
118
119 if (of_address_to_resource(node, 0, &rsrc)) {
120 printk(KERN_ERR "PPC4XX OCM%d: could not get resource address\n",
121 ocm->index);
122 return;
123 }
124
125 ocm->phys = rsrc.start;
126 ocm->memtotal = (rsrc.end - rsrc.start + 1);
127
128 printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (%s)\n",
129 ocm->index, ocm->memtotal,
130 (ocm->status == OCM_DISABLED) ? "disabled" : "enabled");
131
132 if (ocm->status == OCM_DISABLED)
133 return;
134
135 /* request region */
136
137 if (!request_mem_region(ocm->phys, ocm->memtotal, "ppc4xx_ocm")) {
138 printk(KERN_ERR "PPC4XX OCM%d: could not request region\n",
139 ocm->index);
140 return;
141 }
142
143 /* Configure non-cached and cached regions */
144
145 ocm->nc.phys = ocm->phys;
146 ocm->nc.memtotal = ocm->memtotal - ocm->cache_size;
147 ocm->nc.memfree = ocm->nc.memtotal;
148
149 ocm->c.phys = ocm->phys + ocm->nc.memtotal;
150 ocm->c.memtotal = ocm->cache_size;
151 ocm->c.memfree = ocm->c.memtotal;
152
153 if (ocm->nc.memtotal == 0)
154 ocm->nc.phys = 0;
155
156 if (ocm->c.memtotal == 0)
157 ocm->c.phys = 0;
158
159 printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (non-cached)\n",
160 ocm->index, ocm->nc.memtotal);
161
162 printk(KERN_INFO "PPC4XX OCM%d: %d Bytes (cached)\n",
163 ocm->index, ocm->c.memtotal);
164
165 /* ioremap the non-cached region */
166 if (ocm->nc.memtotal) {
167 ocm->nc.virt = __ioremap(ocm->nc.phys, ocm->nc.memtotal,
168 _PAGE_EXEC | pgprot_val(PAGE_KERNEL_NCG));
169
170 if (!ocm->nc.virt) {
171 printk(KERN_ERR
172 "PPC4XX OCM%d: failed to ioremap non-cached memory\n",
173 ocm->index);
174 ocm->nc.memfree = 0;
175 return;
176 }
177 }
178
179 /* ioremap the cached region */
180
181 if (ocm->c.memtotal) {
182 ocm->c.virt = __ioremap(ocm->c.phys, ocm->c.memtotal,
183 _PAGE_EXEC | pgprot_val(PAGE_KERNEL));
184
185 if (!ocm->c.virt) {
186 printk(KERN_ERR
187 "PPC4XX OCM%d: failed to ioremap cached memory\n",
188 ocm->index);
189 ocm->c.memfree = 0;
190 return;
191 }
192 }
193
194 /* Create Remote Heaps */
195
196 ocm->alignment = 4; /* default 4 byte alignment */
197
198 if (ocm->nc.virt) {
199 ocm->nc.rh = rh_create(ocm->alignment);
200 rh_attach_region(ocm->nc.rh, 0, ocm->nc.memtotal);
201 }
202
203 if (ocm->c.virt) {
204 ocm->c.rh = rh_create(ocm->alignment);
205 rh_attach_region(ocm->c.rh, 0, ocm->c.memtotal);
206 }
207
208 INIT_LIST_HEAD(&ocm->nc.list);
209 INIT_LIST_HEAD(&ocm->c.list);
210
211 ocm->ready = 1;
212 }
213
214 static int ocm_debugfs_show(struct seq_file *m, void *v)
215 {
216 struct ocm_block *blk, *tmp;
217 unsigned int i;
218
219 for (i = 0; i < ocm_count; i++) {
220 struct ocm_info *ocm = ocm_get_node(i);
221
222 if (!ocm || !ocm->ready)
223 continue;
224
225 seq_printf(m, "PPC4XX OCM : %d\n", ocm->index);
226 seq_printf(m, "PhysAddr : %pa\n", &(ocm->phys));
227 seq_printf(m, "MemTotal : %d Bytes\n", ocm->memtotal);
228 seq_printf(m, "MemTotal(NC) : %d Bytes\n", ocm->nc.memtotal);
229 seq_printf(m, "MemTotal(C) : %d Bytes\n\n", ocm->c.memtotal);
230
231 seq_printf(m, "NC.PhysAddr : %pa\n", &(ocm->nc.phys));
232 seq_printf(m, "NC.VirtAddr : 0x%p\n", ocm->nc.virt);
233 seq_printf(m, "NC.MemTotal : %d Bytes\n", ocm->nc.memtotal);
234 seq_printf(m, "NC.MemFree : %d Bytes\n", ocm->nc.memfree);
235
236 list_for_each_entry_safe(blk, tmp, &ocm->nc.list, list) {
237 seq_printf(m, "NC.MemUsed : %d Bytes (%s)\n",
238 blk->size, blk->owner);
239 }
240
241 seq_printf(m, "\nC.PhysAddr : %pa\n", &(ocm->c.phys));
242 seq_printf(m, "C.VirtAddr : 0x%p\n", ocm->c.virt);
243 seq_printf(m, "C.MemTotal : %d Bytes\n", ocm->c.memtotal);
244 seq_printf(m, "C.MemFree : %d Bytes\n", ocm->c.memfree);
245
246 list_for_each_entry_safe(blk, tmp, &ocm->c.list, list) {
247 seq_printf(m, "C.MemUsed : %d Bytes (%s)\n",
248 blk->size, blk->owner);
249 }
250
251 seq_putc(m, '\n');
252 }
253
254 return 0;
255 }
256
257 static int ocm_debugfs_open(struct inode *inode, struct file *file)
258 {
259 return single_open(file, ocm_debugfs_show, NULL);
260 }
261
262 static const struct file_operations ocm_debugfs_fops = {
263 .open = ocm_debugfs_open,
264 .read = seq_read,
265 .llseek = seq_lseek,
266 .release = single_release,
267 };
268
269 static int ocm_debugfs_init(void)
270 {
271 struct dentry *junk;
272
273 junk = debugfs_create_dir("ppc4xx_ocm", 0);
274 if (!junk) {
275 printk(KERN_ALERT "debugfs ppc4xx ocm: failed to create dir\n");
276 return -1;
277 }
278
279 if (debugfs_create_file("info", 0644, junk, NULL, &ocm_debugfs_fops)) {
280 printk(KERN_ALERT "debugfs ppc4xx ocm: failed to create file\n");
281 return -1;
282 }
283
284 return 0;
285 }
286
287 void *ppc4xx_ocm_alloc(phys_addr_t *phys, int size, int align,
288 int flags, const char *owner)
289 {
290 void __iomem *addr = NULL;
291 unsigned long offset;
292 struct ocm_info *ocm;
293 struct ocm_region *ocm_reg;
294 struct ocm_block *ocm_blk;
295 int i;
296
297 for (i = 0; i < ocm_count; i++) {
298 ocm = ocm_get_node(i);
299
300 if (!ocm || !ocm->ready)
301 continue;
302
303 if (flags == PPC4XX_OCM_NON_CACHED)
304 ocm_reg = &ocm->nc;
305 else
306 ocm_reg = &ocm->c;
307
308 if (!ocm_reg->virt)
309 continue;
310
311 if (align < ocm->alignment)
312 align = ocm->alignment;
313
314 offset = rh_alloc_align(ocm_reg->rh, size, align, NULL);
315
316 if (IS_ERR_VALUE(offset))
317 continue;
318
319 ocm_blk = kzalloc(sizeof(*ocm_blk), GFP_KERNEL);
320 if (!ocm_blk) {
321 rh_free(ocm_reg->rh, offset);
322 break;
323 }
324
325 *phys = ocm_reg->phys + offset;
326 addr = ocm_reg->virt + offset;
327 size = ALIGN(size, align);
328
329 ocm_blk->addr = addr;
330 ocm_blk->size = size;
331 ocm_blk->owner = owner;
332 list_add_tail(&ocm_blk->list, &ocm_reg->list);
333
334 ocm_reg->memfree -= size;
335
336 break;
337 }
338
339 return addr;
340 }
341
342 void ppc4xx_ocm_free(const void *addr)
343 {
344 int i;
345
346 if (!addr)
347 return;
348
349 for (i = 0; i < ocm_count; i++) {
350 struct ocm_info *ocm = ocm_get_node(i);
351
352 if (!ocm || !ocm->ready)
353 continue;
354
355 if (ocm_free_region(&ocm->nc, addr) ||
356 ocm_free_region(&ocm->c, addr))
357 return;
358 }
359 }
360
361 static int __init ppc4xx_ocm_init(void)
362 {
363 struct device_node *np;
364 int count;
365
366 count = 0;
367 for_each_compatible_node(np, NULL, "ibm,ocm")
368 count++;
369
370 if (!count)
371 return 0;
372
373 ocm_nodes = kzalloc((count * sizeof(struct ocm_info)), GFP_KERNEL);
374 if (!ocm_nodes)
375 return -ENOMEM;
376
377 ocm_count = count;
378 count = 0;
379
380 for_each_compatible_node(np, NULL, "ibm,ocm") {
381 ocm_init_node(count, np);
382 count++;
383 }
384
385 ocm_debugfs_init();
386
387 return 0;
388 }
389
390 arch_initcall(ppc4xx_ocm_init);