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