]> Pileus Git - ~andy/linux/blob - drivers/of/fdt.c
of/flattree: Make the kernel accept ePAPR style phandle information
[~andy/linux] / drivers / of / fdt.c
1 /*
2  * Functions for working with the Flattened Device Tree data format
3  *
4  * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5  * benh@kernel.crashing.org
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/lmb.h>
14 #include <linux/initrd.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17
18
19 #ifdef CONFIG_PPC
20 #include <asm/machdep.h>
21 #endif /* CONFIG_PPC */
22
23 int __initdata dt_root_addr_cells;
24 int __initdata dt_root_size_cells;
25
26 struct boot_param_header *initial_boot_params;
27
28 char *find_flat_dt_string(u32 offset)
29 {
30         return ((char *)initial_boot_params) +
31                 be32_to_cpu(initial_boot_params->off_dt_strings) + offset;
32 }
33
34 /**
35  * of_scan_flat_dt - scan flattened tree blob and call callback on each.
36  * @it: callback function
37  * @data: context data pointer
38  *
39  * This function is used to scan the flattened device-tree, it is
40  * used to extract the memory information at boot before we can
41  * unflatten the tree
42  */
43 int __init of_scan_flat_dt(int (*it)(unsigned long node,
44                                      const char *uname, int depth,
45                                      void *data),
46                            void *data)
47 {
48         unsigned long p = ((unsigned long)initial_boot_params) +
49                 be32_to_cpu(initial_boot_params->off_dt_struct);
50         int rc = 0;
51         int depth = -1;
52
53         do {
54                 u32 tag = be32_to_cpup((__be32 *)p);
55                 char *pathp;
56
57                 p += 4;
58                 if (tag == OF_DT_END_NODE) {
59                         depth--;
60                         continue;
61                 }
62                 if (tag == OF_DT_NOP)
63                         continue;
64                 if (tag == OF_DT_END)
65                         break;
66                 if (tag == OF_DT_PROP) {
67                         u32 sz = be32_to_cpup((__be32 *)p);
68                         p += 8;
69                         if (be32_to_cpu(initial_boot_params->version) < 0x10)
70                                 p = _ALIGN(p, sz >= 8 ? 8 : 4);
71                         p += sz;
72                         p = _ALIGN(p, 4);
73                         continue;
74                 }
75                 if (tag != OF_DT_BEGIN_NODE) {
76                         pr_err("Invalid tag %x in flat device tree!\n", tag);
77                         return -EINVAL;
78                 }
79                 depth++;
80                 pathp = (char *)p;
81                 p = _ALIGN(p + strlen(pathp) + 1, 4);
82                 if ((*pathp) == '/') {
83                         char *lp, *np;
84                         for (lp = NULL, np = pathp; *np; np++)
85                                 if ((*np) == '/')
86                                         lp = np+1;
87                         if (lp != NULL)
88                                 pathp = lp;
89                 }
90                 rc = it(p, pathp, depth, data);
91                 if (rc != 0)
92                         break;
93         } while (1);
94
95         return rc;
96 }
97
98 /**
99  * of_get_flat_dt_root - find the root node in the flat blob
100  */
101 unsigned long __init of_get_flat_dt_root(void)
102 {
103         unsigned long p = ((unsigned long)initial_boot_params) +
104                 be32_to_cpu(initial_boot_params->off_dt_struct);
105
106         while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
107                 p += 4;
108         BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
109         p += 4;
110         return _ALIGN(p + strlen((char *)p) + 1, 4);
111 }
112
113 /**
114  * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
115  *
116  * This function can be used within scan_flattened_dt callback to get
117  * access to properties
118  */
119 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
120                                  unsigned long *size)
121 {
122         unsigned long p = node;
123
124         do {
125                 u32 tag = be32_to_cpup((__be32 *)p);
126                 u32 sz, noff;
127                 const char *nstr;
128
129                 p += 4;
130                 if (tag == OF_DT_NOP)
131                         continue;
132                 if (tag != OF_DT_PROP)
133                         return NULL;
134
135                 sz = be32_to_cpup((__be32 *)p);
136                 noff = be32_to_cpup((__be32 *)(p + 4));
137                 p += 8;
138                 if (be32_to_cpu(initial_boot_params->version) < 0x10)
139                         p = _ALIGN(p, sz >= 8 ? 8 : 4);
140
141                 nstr = find_flat_dt_string(noff);
142                 if (nstr == NULL) {
143                         pr_warning("Can't find property index name !\n");
144                         return NULL;
145                 }
146                 if (strcmp(name, nstr) == 0) {
147                         if (size)
148                                 *size = sz;
149                         return (void *)p;
150                 }
151                 p += sz;
152                 p = _ALIGN(p, 4);
153         } while (1);
154 }
155
156 /**
157  * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
158  * @node: node to test
159  * @compat: compatible string to compare with compatible list.
160  */
161 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
162 {
163         const char *cp;
164         unsigned long cplen, l;
165
166         cp = of_get_flat_dt_prop(node, "compatible", &cplen);
167         if (cp == NULL)
168                 return 0;
169         while (cplen > 0) {
170                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
171                         return 1;
172                 l = strlen(cp) + 1;
173                 cp += l;
174                 cplen -= l;
175         }
176
177         return 0;
178 }
179
180 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
181                                        unsigned long align)
182 {
183         void *res;
184
185         *mem = _ALIGN(*mem, align);
186         res = (void *)*mem;
187         *mem += size;
188
189         return res;
190 }
191
192 /**
193  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
194  * @p: pointer to node in flat tree
195  * @dad: Parent struct device_node
196  * @allnextpp: pointer to ->allnext from last allocated device_node
197  * @fpsize: Size of the node path up at the current depth.
198  */
199 unsigned long __init unflatten_dt_node(unsigned long mem,
200                                         unsigned long *p,
201                                         struct device_node *dad,
202                                         struct device_node ***allnextpp,
203                                         unsigned long fpsize)
204 {
205         struct device_node *np;
206         struct property *pp, **prev_pp = NULL;
207         char *pathp;
208         u32 tag;
209         unsigned int l, allocl;
210         int has_name = 0;
211         int new_format = 0;
212
213         tag = be32_to_cpup((__be32 *)(*p));
214         if (tag != OF_DT_BEGIN_NODE) {
215                 pr_err("Weird tag at start of node: %x\n", tag);
216                 return mem;
217         }
218         *p += 4;
219         pathp = (char *)*p;
220         l = allocl = strlen(pathp) + 1;
221         *p = _ALIGN(*p + l, 4);
222
223         /* version 0x10 has a more compact unit name here instead of the full
224          * path. we accumulate the full path size using "fpsize", we'll rebuild
225          * it later. We detect this because the first character of the name is
226          * not '/'.
227          */
228         if ((*pathp) != '/') {
229                 new_format = 1;
230                 if (fpsize == 0) {
231                         /* root node: special case. fpsize accounts for path
232                          * plus terminating zero. root node only has '/', so
233                          * fpsize should be 2, but we want to avoid the first
234                          * level nodes to have two '/' so we use fpsize 1 here
235                          */
236                         fpsize = 1;
237                         allocl = 2;
238                 } else {
239                         /* account for '/' and path size minus terminal 0
240                          * already in 'l'
241                          */
242                         fpsize += l;
243                         allocl = fpsize;
244                 }
245         }
246
247         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
248                                 __alignof__(struct device_node));
249         if (allnextpp) {
250                 memset(np, 0, sizeof(*np));
251                 np->full_name = ((char *)np) + sizeof(struct device_node);
252                 if (new_format) {
253                         char *fn = np->full_name;
254                         /* rebuild full path for new format */
255                         if (dad && dad->parent) {
256                                 strcpy(fn, dad->full_name);
257 #ifdef DEBUG
258                                 if ((strlen(fn) + l + 1) != allocl) {
259                                         pr_debug("%s: p: %d, l: %d, a: %d\n",
260                                                 pathp, (int)strlen(fn),
261                                                 l, allocl);
262                                 }
263 #endif
264                                 fn += strlen(fn);
265                         }
266                         *(fn++) = '/';
267                         memcpy(fn, pathp, l);
268                 } else
269                         memcpy(np->full_name, pathp, l);
270                 prev_pp = &np->properties;
271                 **allnextpp = np;
272                 *allnextpp = &np->allnext;
273                 if (dad != NULL) {
274                         np->parent = dad;
275                         /* we temporarily use the next field as `last_child'*/
276                         if (dad->next == NULL)
277                                 dad->child = np;
278                         else
279                                 dad->next->sibling = np;
280                         dad->next = np;
281                 }
282                 kref_init(&np->kref);
283         }
284         while (1) {
285                 u32 sz, noff;
286                 char *pname;
287
288                 tag = be32_to_cpup((__be32 *)(*p));
289                 if (tag == OF_DT_NOP) {
290                         *p += 4;
291                         continue;
292                 }
293                 if (tag != OF_DT_PROP)
294                         break;
295                 *p += 4;
296                 sz = be32_to_cpup((__be32 *)(*p));
297                 noff = be32_to_cpup((__be32 *)((*p) + 4));
298                 *p += 8;
299                 if (be32_to_cpu(initial_boot_params->version) < 0x10)
300                         *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
301
302                 pname = find_flat_dt_string(noff);
303                 if (pname == NULL) {
304                         pr_info("Can't find property name in list !\n");
305                         break;
306                 }
307                 if (strcmp(pname, "name") == 0)
308                         has_name = 1;
309                 l = strlen(pname) + 1;
310                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
311                                         __alignof__(struct property));
312                 if (allnextpp) {
313                         /* We accept flattened tree phandles either in
314                          * ePAPR-style "phandle" properties, or the
315                          * legacy "linux,phandle" properties.  If both
316                          * appear and have different values, things
317                          * will get weird.  Don't do that. */
318                         if ((strcmp(pname, "phandle") == 0) ||
319                             (strcmp(pname, "linux,phandle") == 0)) {
320                                 if (np->phandle == 0)
321                                         np->phandle = *((u32 *)*p);
322                         }
323                         /* And we process the "ibm,phandle" property
324                          * used in pSeries dynamic device tree
325                          * stuff */
326                         if (strcmp(pname, "ibm,phandle") == 0)
327                                 np->phandle = *((u32 *)*p);
328                         pp->name = pname;
329                         pp->length = sz;
330                         pp->value = (void *)*p;
331                         *prev_pp = pp;
332                         prev_pp = &pp->next;
333                 }
334                 *p = _ALIGN((*p) + sz, 4);
335         }
336         /* with version 0x10 we may not have the name property, recreate
337          * it here from the unit name if absent
338          */
339         if (!has_name) {
340                 char *p1 = pathp, *ps = pathp, *pa = NULL;
341                 int sz;
342
343                 while (*p1) {
344                         if ((*p1) == '@')
345                                 pa = p1;
346                         if ((*p1) == '/')
347                                 ps = p1 + 1;
348                         p1++;
349                 }
350                 if (pa < ps)
351                         pa = p1;
352                 sz = (pa - ps) + 1;
353                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
354                                         __alignof__(struct property));
355                 if (allnextpp) {
356                         pp->name = "name";
357                         pp->length = sz;
358                         pp->value = pp + 1;
359                         *prev_pp = pp;
360                         prev_pp = &pp->next;
361                         memcpy(pp->value, ps, sz - 1);
362                         ((char *)pp->value)[sz - 1] = 0;
363                         pr_debug("fixed up name for %s -> %s\n", pathp,
364                                 (char *)pp->value);
365                 }
366         }
367         if (allnextpp) {
368                 *prev_pp = NULL;
369                 np->name = of_get_property(np, "name", NULL);
370                 np->type = of_get_property(np, "device_type", NULL);
371
372                 if (!np->name)
373                         np->name = "<NULL>";
374                 if (!np->type)
375                         np->type = "<NULL>";
376         }
377         while (tag == OF_DT_BEGIN_NODE) {
378                 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
379                 tag = be32_to_cpup((__be32 *)(*p));
380         }
381         if (tag != OF_DT_END_NODE) {
382                 pr_err("Weird tag at end of node: %x\n", tag);
383                 return mem;
384         }
385         *p += 4;
386         return mem;
387 }
388
389 #ifdef CONFIG_BLK_DEV_INITRD
390 /**
391  * early_init_dt_check_for_initrd - Decode initrd location from flat tree
392  * @node: reference to node containing initrd location ('chosen')
393  */
394 void __init early_init_dt_check_for_initrd(unsigned long node)
395 {
396         unsigned long start, end, len;
397         __be32 *prop;
398
399         pr_debug("Looking for initrd properties... ");
400
401         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
402         if (!prop)
403                 return;
404         start = of_read_ulong(prop, len/4);
405
406         prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
407         if (!prop)
408                 return;
409         end = of_read_ulong(prop, len/4);
410
411         early_init_dt_setup_initrd_arch(start, end);
412         pr_debug("initrd_start=0x%lx  initrd_end=0x%lx\n", start, end);
413 }
414 #else
415 inline void early_init_dt_check_for_initrd(unsigned long node)
416 {
417 }
418 #endif /* CONFIG_BLK_DEV_INITRD */
419
420 /**
421  * early_init_dt_scan_root - fetch the top level address and size cells
422  */
423 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
424                                    int depth, void *data)
425 {
426         __be32 *prop;
427
428         if (depth != 0)
429                 return 0;
430
431         dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
432         dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
433
434         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
435         if (prop)
436                 dt_root_size_cells = be32_to_cpup(prop);
437         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
438
439         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
440         if (prop)
441                 dt_root_addr_cells = be32_to_cpup(prop);
442         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
443
444         /* break now */
445         return 1;
446 }
447
448 u64 __init dt_mem_next_cell(int s, __be32 **cellp)
449 {
450         __be32 *p = *cellp;
451
452         *cellp = p + s;
453         return of_read_number(p, s);
454 }
455
456 /**
457  * early_init_dt_scan_memory - Look for an parse memory nodes
458  */
459 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
460                                      int depth, void *data)
461 {
462         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
463         __be32 *reg, *endp;
464         unsigned long l;
465
466         /* We are scanning "memory" nodes only */
467         if (type == NULL) {
468                 /*
469                  * The longtrail doesn't have a device_type on the
470                  * /memory node, so look for the node called /memory@0.
471                  */
472                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
473                         return 0;
474         } else if (strcmp(type, "memory") != 0)
475                 return 0;
476
477         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
478         if (reg == NULL)
479                 reg = of_get_flat_dt_prop(node, "reg", &l);
480         if (reg == NULL)
481                 return 0;
482
483         endp = reg + (l / sizeof(__be32));
484
485         pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
486             uname, l, reg[0], reg[1], reg[2], reg[3]);
487
488         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
489                 u64 base, size;
490
491                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
492                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
493
494                 if (size == 0)
495                         continue;
496                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
497                     (unsigned long long)size);
498
499                 early_init_dt_add_memory_arch(base, size);
500         }
501
502         return 0;
503 }
504
505 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
506                                      int depth, void *data)
507 {
508         unsigned long l;
509         char *p;
510
511         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
512
513         if (depth != 1 ||
514             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
515                 return 0;
516
517         early_init_dt_check_for_initrd(node);
518
519         /* Retreive command line */
520         p = of_get_flat_dt_prop(node, "bootargs", &l);
521         if (p != NULL && l > 0)
522                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
523
524 #ifdef CONFIG_CMDLINE
525 #ifndef CONFIG_CMDLINE_FORCE
526         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
527 #endif
528                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
529 #endif /* CONFIG_CMDLINE */
530
531         early_init_dt_scan_chosen_arch(node);
532
533         pr_debug("Command line is: %s\n", cmd_line);
534
535         /* break now */
536         return 1;
537 }
538
539 /**
540  * unflatten_device_tree - create tree of device_nodes from flat blob
541  *
542  * unflattens the device-tree passed by the firmware, creating the
543  * tree of struct device_node. It also fills the "name" and "type"
544  * pointers of the nodes so the normal device-tree walking functions
545  * can be used.
546  */
547 void __init unflatten_device_tree(void)
548 {
549         unsigned long start, mem, size;
550         struct device_node **allnextp = &allnodes;
551
552         pr_debug(" -> unflatten_device_tree()\n");
553
554         /* First pass, scan for size */
555         start = ((unsigned long)initial_boot_params) +
556                 be32_to_cpu(initial_boot_params->off_dt_struct);
557         size = unflatten_dt_node(0, &start, NULL, NULL, 0);
558         size = (size | 3) + 1;
559
560         pr_debug("  size is %lx, allocating...\n", size);
561
562         /* Allocate memory for the expanded device tree */
563         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
564         mem = (unsigned long) __va(mem);
565
566         ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
567
568         pr_debug("  unflattening %lx...\n", mem);
569
570         /* Second pass, do actual unflattening */
571         start = ((unsigned long)initial_boot_params) +
572                 be32_to_cpu(initial_boot_params->off_dt_struct);
573         unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
574         if (be32_to_cpup((__be32 *)start) != OF_DT_END)
575                 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
576         if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
577                 pr_warning("End of tree marker overwritten: %08x\n",
578                            be32_to_cpu(((__be32 *)mem)[size / 4]));
579         *allnextp = NULL;
580
581         /* Get pointer to OF "/chosen" node for use everywhere */
582         of_chosen = of_find_node_by_path("/chosen");
583         if (of_chosen == NULL)
584                 of_chosen = of_find_node_by_path("/chosen@0");
585
586         pr_debug(" <- unflatten_device_tree()\n");
587 }