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