of: Merge of_node_get() and of_node_put()
[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  *
323  * New implementation of the OF "find" APIs, return a refcounted
324  * object, call of_node_put() when done.  The device tree and list
325  * are protected by a rw_lock.
326  *
327  * Note that property management will need some locking as well,
328  * this isn't dealt with yet.
329  *
330  *******/
331
332 /**
333  *      of_find_node_by_phandle - Find a node given a phandle
334  *      @handle:        phandle of the node to find
335  *
336  *      Returns a node pointer with refcount incremented, use
337  *      of_node_put() on it when done.
338  */
339 struct device_node *of_find_node_by_phandle(phandle handle)
340 {
341         struct device_node *np;
342
343         read_lock(&devtree_lock);
344         for (np = allnodes; np != NULL; np = np->allnext)
345                 if (np->linux_phandle == handle)
346                         break;
347         of_node_get(np);
348         read_unlock(&devtree_lock);
349         return np;
350 }
351 EXPORT_SYMBOL(of_find_node_by_phandle);
352
353 /*
354  * Plug a device node into the tree and global list.
355  */
356 void of_attach_node(struct device_node *np)
357 {
358         unsigned long flags;
359
360         write_lock_irqsave(&devtree_lock, flags);
361         np->sibling = np->parent->child;
362         np->allnext = allnodes;
363         np->parent->child = np;
364         allnodes = np;
365         write_unlock_irqrestore(&devtree_lock, flags);
366 }
367
368 /*
369  * "Unplug" a node from the device tree.  The caller must hold
370  * a reference to the node.  The memory associated with the node
371  * is not freed until its refcount goes to zero.
372  */
373 void of_detach_node(struct device_node *np)
374 {
375         struct device_node *parent;
376         unsigned long flags;
377
378         write_lock_irqsave(&devtree_lock, flags);
379
380         parent = np->parent;
381         if (!parent)
382                 goto out_unlock;
383
384         if (allnodes == np)
385                 allnodes = np->allnext;
386         else {
387                 struct device_node *prev;
388                 for (prev = allnodes;
389                      prev->allnext != np;
390                      prev = prev->allnext)
391                         ;
392                 prev->allnext = np->allnext;
393         }
394
395         if (parent->child == np)
396                 parent->child = np->sibling;
397         else {
398                 struct device_node *prevsib;
399                 for (prevsib = np->parent->child;
400                      prevsib->sibling != np;
401                      prevsib = prevsib->sibling)
402                         ;
403                 prevsib->sibling = np->sibling;
404         }
405
406         of_node_set_flag(np, OF_DETACHED);
407
408 out_unlock:
409         write_unlock_irqrestore(&devtree_lock, flags);
410 }
411
412 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
413 static struct debugfs_blob_wrapper flat_dt_blob;
414
415 static int __init export_flat_device_tree(void)
416 {
417         struct dentry *d;
418
419         flat_dt_blob.data = initial_boot_params;
420         flat_dt_blob.size = initial_boot_params->totalsize;
421
422         d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
423                                 of_debugfs_root, &flat_dt_blob);
424         if (!d)
425                 return 1;
426
427         return 0;
428 }
429 device_initcall(export_flat_device_tree);
430 #endif