of/flattree: merge dt_mem_next_cell
[pandora-kernel.git] / arch / microblaze / kernel / prom.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
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
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <stdarg.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/threads.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/stringify.h>
25 #include <linux/delay.h>
26 #include <linux/initrd.h>
27 #include <linux/bitops.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/debugfs.h>
31 #include <linux/irq.h>
32 #include <linux/lmb.h>
33
34 #include <asm/prom.h>
35 #include <asm/page.h>
36 #include <asm/processor.h>
37 #include <asm/irq.h>
38 #include <linux/io.h>
39 #include <asm/system.h>
40 #include <asm/mmu.h>
41 #include <asm/pgtable.h>
42 #include <asm/sections.h>
43 #include <asm/pci-bridge.h>
44
45 typedef u32 cell_t;
46
47 /* export that to outside world */
48 struct device_node *of_chosen;
49
50 #define early_init_dt_scan_drconf_memory(node) 0
51
52 static int __init early_init_dt_scan_cpus(unsigned long node,
53                                           const char *uname, int depth,
54                                           void *data)
55 {
56         static int logical_cpuid;
57         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
58         const u32 *intserv;
59         int i, nthreads;
60         int found = 0;
61
62         /* We are scanning "cpu" nodes only */
63         if (type == NULL || strcmp(type, "cpu") != 0)
64                 return 0;
65
66         /* Get physical cpuid */
67         intserv = of_get_flat_dt_prop(node, "reg", NULL);
68         nthreads = 1;
69
70         /*
71          * Now see if any of these threads match our boot cpu.
72          * NOTE: This must match the parsing done in smp_setup_cpu_maps.
73          */
74         for (i = 0; i < nthreads; i++) {
75                 /*
76                  * version 2 of the kexec param format adds the phys cpuid of
77                  * booted proc.
78                  */
79                 if (initial_boot_params && initial_boot_params->version >= 2) {
80                         if (intserv[i] ==
81                                         initial_boot_params->boot_cpuid_phys) {
82                                 found = 1;
83                                 break;
84                         }
85                 } else {
86                         /*
87                          * Check if it's the boot-cpu, set it's hw index now,
88                          * unfortunately this format did not support booting
89                          * off secondary threads.
90                          */
91                         if (of_get_flat_dt_prop(node,
92                                         "linux,boot-cpu", NULL) != NULL) {
93                                 found = 1;
94                                 break;
95                         }
96                 }
97
98 #ifdef CONFIG_SMP
99                 /* logical cpu id is always 0 on UP kernels */
100                 logical_cpuid++;
101 #endif
102         }
103
104         if (found) {
105                 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
106                         intserv[i]);
107                 boot_cpuid = logical_cpuid;
108         }
109
110         return 0;
111 }
112
113 static int __init early_init_dt_scan_chosen(unsigned long node,
114                                 const char *uname, int depth, void *data)
115 {
116         unsigned long l;
117         char *p;
118
119         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
120
121         if (depth != 1 ||
122                 (strcmp(uname, "chosen") != 0 &&
123                                 strcmp(uname, "chosen@0") != 0))
124                 return 0;
125
126 #ifdef CONFIG_KEXEC
127         lprop = (u64 *)of_get_flat_dt_prop(node,
128                                 "linux,crashkernel-base", NULL);
129         if (lprop)
130                 crashk_res.start = *lprop;
131
132         lprop = (u64 *)of_get_flat_dt_prop(node,
133                                 "linux,crashkernel-size", NULL);
134         if (lprop)
135                 crashk_res.end = crashk_res.start + *lprop - 1;
136 #endif
137
138         early_init_dt_check_for_initrd(node);
139
140         /* Retreive command line */
141         p = of_get_flat_dt_prop(node, "bootargs", &l);
142         if (p != NULL && l > 0)
143                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
144
145 #ifdef CONFIG_CMDLINE
146 #ifndef CONFIG_CMDLINE_FORCE
147         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
148 #endif
149                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
150 #endif /* CONFIG_CMDLINE */
151
152         pr_debug("Command line is: %s\n", cmd_line);
153
154         /* break now */
155         return 1;
156 }
157
158 static int __init early_init_dt_scan_memory(unsigned long node,
159                                 const char *uname, int depth, void *data)
160 {
161         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
162         cell_t *reg, *endp;
163         unsigned long l;
164
165         /* Look for the ibm,dynamic-reconfiguration-memory node */
166 /*      if (depth == 1 &&
167                 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
168                 return early_init_dt_scan_drconf_memory(node);
169 */
170         /* We are scanning "memory" nodes only */
171         if (type == NULL) {
172                 /*
173                  * The longtrail doesn't have a device_type on the
174                  * /memory node, so look for the node called /memory@0.
175                  */
176                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
177                         return 0;
178         } else if (strcmp(type, "memory") != 0)
179                 return 0;
180
181         reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
182         if (reg == NULL)
183                 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
184         if (reg == NULL)
185                 return 0;
186
187         endp = reg + (l / sizeof(cell_t));
188
189         pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
190                 uname, l, reg[0], reg[1], reg[2], reg[3]);
191
192         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
193                 u64 base, size;
194
195                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
196                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
197
198                 if (size == 0)
199                         continue;
200                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
201                         (unsigned long long)size);
202
203                 lmb_add(base, size);
204         }
205         return 0;
206 }
207
208 #ifdef CONFIG_PHYP_DUMP
209 /**
210  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
211  *
212  * Function to find the largest size we need to reserve
213  * during early boot process.
214  *
215  * It either looks for boot param and returns that OR
216  * returns larger of 256 or 5% rounded down to multiples of 256MB.
217  *
218  */
219 static inline unsigned long phyp_dump_calculate_reserve_size(void)
220 {
221         unsigned long tmp;
222
223         if (phyp_dump_info->reserve_bootvar)
224                 return phyp_dump_info->reserve_bootvar;
225
226         /* divide by 20 to get 5% of value */
227         tmp = lmb_end_of_DRAM();
228         do_div(tmp, 20);
229
230         /* round it down in multiples of 256 */
231         tmp = tmp & ~0x0FFFFFFFUL;
232
233         return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
234 }
235
236 /**
237  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
238  *
239  * This routine may reserve memory regions in the kernel only
240  * if the system is supported and a dump was taken in last
241  * boot instance or if the hardware is supported and the
242  * scratch area needs to be setup. In other instances it returns
243  * without reserving anything. The memory in case of dump being
244  * active is freed when the dump is collected (by userland tools).
245  */
246 static void __init phyp_dump_reserve_mem(void)
247 {
248         unsigned long base, size;
249         unsigned long variable_reserve_size;
250
251         if (!phyp_dump_info->phyp_dump_configured) {
252                 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
253                 return;
254         }
255
256         if (!phyp_dump_info->phyp_dump_at_boot) {
257                 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
258                 return;
259         }
260
261         variable_reserve_size = phyp_dump_calculate_reserve_size();
262
263         if (phyp_dump_info->phyp_dump_is_active) {
264                 /* Reserve *everything* above RMR.Area freed by userland tools*/
265                 base = variable_reserve_size;
266                 size = lmb_end_of_DRAM() - base;
267
268                 /* XXX crashed_ram_end is wrong, since it may be beyond
269                  * the memory_limit, it will need to be adjusted. */
270                 lmb_reserve(base, size);
271
272                 phyp_dump_info->init_reserve_start = base;
273                 phyp_dump_info->init_reserve_size = size;
274         } else {
275                 size = phyp_dump_info->cpu_state_size +
276                         phyp_dump_info->hpte_region_size +
277                         variable_reserve_size;
278                 base = lmb_end_of_DRAM() - size;
279                 lmb_reserve(base, size);
280                 phyp_dump_info->init_reserve_start = base;
281                 phyp_dump_info->init_reserve_size = size;
282         }
283 }
284 #else
285 static inline void __init phyp_dump_reserve_mem(void) {}
286 #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
287
288 #ifdef CONFIG_EARLY_PRINTK
289 /* MS this is Microblaze specifig function */
290 static int __init early_init_dt_scan_serial(unsigned long node,
291                                 const char *uname, int depth, void *data)
292 {
293         unsigned long l;
294         char *p;
295         int *addr;
296
297         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
298
299 /* find all serial nodes */
300         if (strncmp(uname, "serial", 6) != 0)
301                 return 0;
302
303         early_init_dt_check_for_initrd(node);
304
305 /* find compatible node with uartlite */
306         p = of_get_flat_dt_prop(node, "compatible", &l);
307         if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
308                         (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
309                 return 0;
310
311         addr = of_get_flat_dt_prop(node, "reg", &l);
312         return *addr; /* return address */
313 }
314
315 /* this function is looking for early uartlite console - Microblaze specific */
316 int __init early_uartlite_console(void)
317 {
318         return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
319 }
320 #endif
321
322 void __init early_init_devtree(void *params)
323 {
324         pr_debug(" -> early_init_devtree(%p)\n", params);
325
326         /* Setup flat device-tree pointer */
327         initial_boot_params = params;
328
329 #ifdef CONFIG_PHYP_DUMP
330         /* scan tree to see if dump occured during last boot */
331         of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
332 #endif
333
334         /* Retrieve various informations from the /chosen node of the
335          * device-tree, including the platform type, initrd location and
336          * size, TCE reserve, and more ...
337          */
338         of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
339
340         /* Scan memory nodes and rebuild LMBs */
341         lmb_init();
342         of_scan_flat_dt(early_init_dt_scan_root, NULL);
343         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
344
345         /* Save command line for /proc/cmdline and then parse parameters */
346         strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
347         parse_early_param();
348
349         lmb_analyze();
350
351         pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
352
353         pr_debug("Scanning CPUs ...\n");
354
355         /* Retreive CPU related informations from the flat tree
356          * (altivec support, boot CPU ID, ...)
357          */
358         of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
359
360         pr_debug(" <- early_init_devtree()\n");
361 }
362
363 /**
364  * Indicates whether the root node has a given value in its
365  * compatible property.
366  */
367 int machine_is_compatible(const char *compat)
368 {
369         struct device_node *root;
370         int rc = 0;
371
372         root = of_find_node_by_path("/");
373         if (root) {
374                 rc = of_device_is_compatible(root, compat);
375                 of_node_put(root);
376         }
377         return rc;
378 }
379 EXPORT_SYMBOL(machine_is_compatible);
380
381 /*******
382  *
383  * New implementation of the OF "find" APIs, return a refcounted
384  * object, call of_node_put() when done.  The device tree and list
385  * are protected by a rw_lock.
386  *
387  * Note that property management will need some locking as well,
388  * this isn't dealt with yet.
389  *
390  *******/
391
392 /**
393  *      of_find_node_by_phandle - Find a node given a phandle
394  *      @handle:        phandle of the node to find
395  *
396  *      Returns a node pointer with refcount incremented, use
397  *      of_node_put() on it when done.
398  */
399 struct device_node *of_find_node_by_phandle(phandle handle)
400 {
401         struct device_node *np;
402
403         read_lock(&devtree_lock);
404         for (np = allnodes; np != NULL; np = np->allnext)
405                 if (np->linux_phandle == handle)
406                         break;
407         of_node_get(np);
408         read_unlock(&devtree_lock);
409         return np;
410 }
411 EXPORT_SYMBOL(of_find_node_by_phandle);
412
413 /**
414  *      of_node_get - Increment refcount of a node
415  *      @node:  Node to inc refcount, NULL is supported to
416  *              simplify writing of callers
417  *
418  *      Returns node.
419  */
420 struct device_node *of_node_get(struct device_node *node)
421 {
422         if (node)
423                 kref_get(&node->kref);
424         return node;
425 }
426 EXPORT_SYMBOL(of_node_get);
427
428 static inline struct device_node *kref_to_device_node(struct kref *kref)
429 {
430         return container_of(kref, struct device_node, kref);
431 }
432
433 /**
434  *      of_node_release - release a dynamically allocated node
435  *      @kref:  kref element of the node to be released
436  *
437  *      In of_node_put() this function is passed to kref_put()
438  *      as the destructor.
439  */
440 static void of_node_release(struct kref *kref)
441 {
442         struct device_node *node = kref_to_device_node(kref);
443         struct property *prop = node->properties;
444
445         /* We should never be releasing nodes that haven't been detached. */
446         if (!of_node_check_flag(node, OF_DETACHED)) {
447                 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
448                         node->full_name);
449                 dump_stack();
450                 kref_init(&node->kref);
451                 return;
452         }
453
454         if (!of_node_check_flag(node, OF_DYNAMIC))
455                 return;
456
457         while (prop) {
458                 struct property *next = prop->next;
459                 kfree(prop->name);
460                 kfree(prop->value);
461                 kfree(prop);
462                 prop = next;
463
464                 if (!prop) {
465                         prop = node->deadprops;
466                         node->deadprops = NULL;
467                 }
468         }
469         kfree(node->full_name);
470         kfree(node->data);
471         kfree(node);
472 }
473
474 /**
475  *      of_node_put - Decrement refcount of a node
476  *      @node:  Node to dec refcount, NULL is supported to
477  *              simplify writing of callers
478  *
479  */
480 void of_node_put(struct device_node *node)
481 {
482         if (node)
483                 kref_put(&node->kref, of_node_release);
484 }
485 EXPORT_SYMBOL(of_node_put);
486
487 /*
488  * Plug a device node into the tree and global list.
489  */
490 void of_attach_node(struct device_node *np)
491 {
492         unsigned long flags;
493
494         write_lock_irqsave(&devtree_lock, flags);
495         np->sibling = np->parent->child;
496         np->allnext = allnodes;
497         np->parent->child = np;
498         allnodes = np;
499         write_unlock_irqrestore(&devtree_lock, flags);
500 }
501
502 /*
503  * "Unplug" a node from the device tree.  The caller must hold
504  * a reference to the node.  The memory associated with the node
505  * is not freed until its refcount goes to zero.
506  */
507 void of_detach_node(struct device_node *np)
508 {
509         struct device_node *parent;
510         unsigned long flags;
511
512         write_lock_irqsave(&devtree_lock, flags);
513
514         parent = np->parent;
515         if (!parent)
516                 goto out_unlock;
517
518         if (allnodes == np)
519                 allnodes = np->allnext;
520         else {
521                 struct device_node *prev;
522                 for (prev = allnodes;
523                      prev->allnext != np;
524                      prev = prev->allnext)
525                         ;
526                 prev->allnext = np->allnext;
527         }
528
529         if (parent->child == np)
530                 parent->child = np->sibling;
531         else {
532                 struct device_node *prevsib;
533                 for (prevsib = np->parent->child;
534                      prevsib->sibling != np;
535                      prevsib = prevsib->sibling)
536                         ;
537                 prevsib->sibling = np->sibling;
538         }
539
540         of_node_set_flag(np, OF_DETACHED);
541
542 out_unlock:
543         write_unlock_irqrestore(&devtree_lock, flags);
544 }
545
546 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
547 static struct debugfs_blob_wrapper flat_dt_blob;
548
549 static int __init export_flat_device_tree(void)
550 {
551         struct dentry *d;
552
553         flat_dt_blob.data = initial_boot_params;
554         flat_dt_blob.size = initial_boot_params->totalsize;
555
556         d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
557                                 of_debugfs_root, &flat_dt_blob);
558         if (!d)
559                 return 1;
560
561         return 0;
562 }
563 device_initcall(export_flat_device_tree);
564 #endif