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