2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc64 by David S. Miller davem@davemloft.net
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
22 #include <linux/module.h>
23 #include <linux/lmb.h>
24 #include <linux/of_device.h>
27 #include <asm/oplib.h>
35 static unsigned int prom_early_allocated __initdata;
37 void * __init prom_early_alloc(unsigned long size)
39 unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
43 prom_printf("prom_early_alloc(%lu) failed\n");
49 prom_early_allocated += size;
54 static int is_root_node(const struct device_node *dp)
59 return (dp->parent == NULL);
62 /* The following routines deal with the black magic of fully naming a
65 * Certain well known named nodes are just the simple name string.
67 * Actual devices have an address specifier appended to the base name
68 * string, like this "foo@addr". The "addr" can be in any number of
69 * formats, and the platform plus the type of the node determine the
70 * format and how it is constructed.
72 * For children of the ROOT node, the naming convention is fixed and
73 * determined by whether this is a sun4u or sun4v system.
75 * For children of other nodes, it is bus type specific. So
76 * we walk up the tree until we discover a "device_type" property
77 * we recognize and we go from there.
79 * As an example, the boot device on my workstation has a full path:
81 * /pci@1e,600000/ide@d/disk@0,0:c
83 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
85 struct linux_prom64_registers *regs;
86 struct property *rprop;
87 u32 high_bits, low_bits, type;
89 rprop = of_find_property(dp, "reg", NULL);
94 if (!is_root_node(dp->parent)) {
95 sprintf(tmp_buf, "%s@%x,%x",
97 (unsigned int) (regs->phys_addr >> 32UL),
98 (unsigned int) (regs->phys_addr & 0xffffffffUL));
102 type = regs->phys_addr >> 60UL;
103 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
104 low_bits = (regs->phys_addr & 0xffffffffUL);
106 if (type == 0 || type == 8) {
107 const char *prefix = (type == 0) ? "m" : "i";
110 sprintf(tmp_buf, "%s@%s%x,%x",
112 high_bits, low_bits);
114 sprintf(tmp_buf, "%s@%s%x",
118 } else if (type == 12) {
119 sprintf(tmp_buf, "%s@%x",
120 dp->name, high_bits);
124 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
126 struct linux_prom64_registers *regs;
127 struct property *prop;
129 prop = of_find_property(dp, "reg", NULL);
134 if (!is_root_node(dp->parent)) {
135 sprintf(tmp_buf, "%s@%x,%x",
137 (unsigned int) (regs->phys_addr >> 32UL),
138 (unsigned int) (regs->phys_addr & 0xffffffffUL));
142 prop = of_find_property(dp, "upa-portid", NULL);
144 prop = of_find_property(dp, "portid", NULL);
146 unsigned long mask = 0xffffffffUL;
148 if (tlb_type >= cheetah)
151 sprintf(tmp_buf, "%s@%x,%x",
154 (unsigned int) (regs->phys_addr & mask));
158 /* "name@slot,offset" */
159 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
161 struct linux_prom_registers *regs;
162 struct property *prop;
164 prop = of_find_property(dp, "reg", NULL);
169 sprintf(tmp_buf, "%s@%x,%x",
175 /* "name@devnum[,func]" */
176 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
178 struct linux_prom_pci_registers *regs;
179 struct property *prop;
182 prop = of_find_property(dp, "reg", NULL);
187 devfn = (regs->phys_hi >> 8) & 0xff;
189 sprintf(tmp_buf, "%s@%x,%x",
194 sprintf(tmp_buf, "%s@%x",
200 /* "name@UPA_PORTID,offset" */
201 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
203 struct linux_prom64_registers *regs;
204 struct property *prop;
206 prop = of_find_property(dp, "reg", NULL);
212 prop = of_find_property(dp, "upa-portid", NULL);
216 sprintf(tmp_buf, "%s@%x,%x",
218 *(u32 *) prop->value,
219 (unsigned int) (regs->phys_addr & 0xffffffffUL));
223 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
225 struct property *prop;
228 prop = of_find_property(dp, "reg", NULL);
234 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
237 /* "name@addrhi,addrlo" */
238 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
240 struct linux_prom64_registers *regs;
241 struct property *prop;
243 prop = of_find_property(dp, "reg", NULL);
249 sprintf(tmp_buf, "%s@%x,%x",
251 (unsigned int) (regs->phys_addr >> 32UL),
252 (unsigned int) (regs->phys_addr & 0xffffffffUL));
255 /* "name@bus,addr" */
256 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
258 struct property *prop;
261 prop = of_find_property(dp, "reg", NULL);
267 /* This actually isn't right... should look at the #address-cells
268 * property of the i2c bus node etc. etc.
270 sprintf(tmp_buf, "%s@%x,%x",
271 dp->name, regs[0], regs[1]);
274 /* "name@reg0[,reg1]" */
275 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
277 struct property *prop;
280 prop = of_find_property(dp, "reg", NULL);
286 if (prop->length == sizeof(u32) || regs[1] == 1) {
287 sprintf(tmp_buf, "%s@%x",
290 sprintf(tmp_buf, "%s@%x,%x",
291 dp->name, regs[0], regs[1]);
295 /* "name@reg0reg1[,reg2reg3]" */
296 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
298 struct property *prop;
301 prop = of_find_property(dp, "reg", NULL);
307 if (regs[2] || regs[3]) {
308 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
309 dp->name, regs[0], regs[1], regs[2], regs[3]);
311 sprintf(tmp_buf, "%s@%08x%08x",
312 dp->name, regs[0], regs[1]);
316 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
318 struct device_node *parent = dp->parent;
320 if (parent != NULL) {
321 if (!strcmp(parent->type, "pci") ||
322 !strcmp(parent->type, "pciex")) {
323 pci_path_component(dp, tmp_buf);
326 if (!strcmp(parent->type, "sbus")) {
327 sbus_path_component(dp, tmp_buf);
330 if (!strcmp(parent->type, "upa")) {
331 upa_path_component(dp, tmp_buf);
334 if (!strcmp(parent->type, "ebus")) {
335 ebus_path_component(dp, tmp_buf);
338 if (!strcmp(parent->name, "usb") ||
339 !strcmp(parent->name, "hub")) {
340 usb_path_component(dp, tmp_buf);
343 if (!strcmp(parent->type, "i2c")) {
344 i2c_path_component(dp, tmp_buf);
347 if (!strcmp(parent->type, "firewire")) {
348 ieee1394_path_component(dp, tmp_buf);
351 if (!strcmp(parent->type, "virtual-devices")) {
352 vdev_path_component(dp, tmp_buf);
355 /* "isa" is handled with platform naming */
358 /* Use platform naming convention. */
359 if (tlb_type == hypervisor) {
360 sun4v_path_component(dp, tmp_buf);
363 sun4u_path_component(dp, tmp_buf);
367 static char * __init build_path_component(struct device_node *dp)
369 char tmp_buf[64], *n;
372 __build_path_component(dp, tmp_buf);
373 if (tmp_buf[0] == '\0')
374 strcpy(tmp_buf, dp->name);
376 n = prom_early_alloc(strlen(tmp_buf) + 1);
382 static char * __init build_full_name(struct device_node *dp)
384 int len, ourlen, plen;
387 plen = strlen(dp->parent->full_name);
388 ourlen = strlen(dp->path_component_name);
389 len = ourlen + plen + 2;
391 n = prom_early_alloc(len);
392 strcpy(n, dp->parent->full_name);
393 if (!is_root_node(dp->parent)) {
394 strcpy(n + plen, "/");
397 strcpy(n + plen, dp->path_component_name);
402 static char * __init get_one_property(phandle node, const char *name)
404 char *buf = "<NULL>";
407 len = prom_getproplen(node, name);
409 buf = prom_early_alloc(len);
410 len = prom_getproperty(node, name, buf, len);
416 static struct device_node * __init create_node(phandle node, struct device_node *parent)
418 struct device_node *dp;
423 dp = prom_early_alloc(sizeof(*dp));
424 dp->unique_id = prom_unique_id++;
427 kref_init(&dp->kref);
429 dp->name = get_one_property(node, "name");
430 dp->type = get_one_property(node, "device_type");
433 dp->properties = build_prop_list(node);
440 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
442 struct device_node *ret = NULL, *prev_sibling = NULL;
443 struct device_node *dp;
446 dp = create_node(node, parent);
451 prev_sibling->sibling = dp;
458 *nextp = &dp->allnext;
460 dp->path_component_name = build_path_component(dp);
461 dp->full_name = build_full_name(dp);
463 dp->child = build_tree(dp, prom_getchild(node), nextp);
465 node = prom_getsibling(node);
471 static const char *get_mid_prop(void)
473 return (tlb_type == spitfire ? "upa-portid" : "portid");
476 struct device_node *of_find_node_by_cpuid(int cpuid)
478 struct device_node *dp;
479 const char *mid_prop = get_mid_prop();
481 for_each_node_by_type(dp, "cpu") {
482 int id = of_getintprop_default(dp, mid_prop, -1);
483 const char *this_mid_prop = mid_prop;
486 this_mid_prop = "cpuid";
487 id = of_getintprop_default(dp, this_mid_prop, -1);
491 prom_printf("OF: Serious problem, cpu lacks "
492 "%s property", this_mid_prop);
501 static void __init of_fill_in_cpu_data(void)
503 struct device_node *dp;
504 const char *mid_prop = get_mid_prop();
507 for_each_node_by_type(dp, "cpu") {
508 int cpuid = of_getintprop_default(dp, mid_prop, -1);
509 const char *this_mid_prop = mid_prop;
510 struct device_node *portid_parent;
513 portid_parent = NULL;
515 this_mid_prop = "cpuid";
516 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
522 portid_parent = portid_parent->parent;
525 portid = of_getintprop_default(portid_parent,
534 prom_printf("OF: Serious problem, cpu lacks "
535 "%s property", this_mid_prop);
542 if (cpuid >= NR_CPUS) {
543 printk(KERN_WARNING "Ignoring CPU %d which is "
549 /* On uniprocessor we only want the values for the
550 * real physical cpu the kernel booted onto, however
551 * cpu_data() only has one entry at index 0.
553 if (cpuid != real_hard_smp_processor_id())
558 cpu_data(cpuid).clock_tick =
559 of_getintprop_default(dp, "clock-frequency", 0);
562 cpu_data(cpuid).dcache_size =
563 of_getintprop_default(dp, "l1-dcache-size",
565 cpu_data(cpuid).dcache_line_size =
566 of_getintprop_default(dp, "l1-dcache-line-size",
568 cpu_data(cpuid).icache_size =
569 of_getintprop_default(dp, "l1-icache-size",
571 cpu_data(cpuid).icache_line_size =
572 of_getintprop_default(dp, "l1-icache-line-size",
574 cpu_data(cpuid).ecache_size =
575 of_getintprop_default(dp, "l2-cache-size", 0);
576 cpu_data(cpuid).ecache_line_size =
577 of_getintprop_default(dp, "l2-cache-line-size", 0);
578 if (!cpu_data(cpuid).ecache_size ||
579 !cpu_data(cpuid).ecache_line_size) {
580 cpu_data(cpuid).ecache_size =
581 of_getintprop_default(portid_parent,
584 cpu_data(cpuid).ecache_line_size =
585 of_getintprop_default(portid_parent,
586 "l2-cache-line-size", 64);
589 cpu_data(cpuid).core_id = portid + 1;
590 cpu_data(cpuid).proc_id = portid;
592 sparc64_multi_core = 1;
595 cpu_data(cpuid).dcache_size =
596 of_getintprop_default(dp, "dcache-size", 16 * 1024);
597 cpu_data(cpuid).dcache_line_size =
598 of_getintprop_default(dp, "dcache-line-size", 32);
600 cpu_data(cpuid).icache_size =
601 of_getintprop_default(dp, "icache-size", 16 * 1024);
602 cpu_data(cpuid).icache_line_size =
603 of_getintprop_default(dp, "icache-line-size", 32);
605 cpu_data(cpuid).ecache_size =
606 of_getintprop_default(dp, "ecache-size",
608 cpu_data(cpuid).ecache_line_size =
609 of_getintprop_default(dp, "ecache-line-size", 64);
611 cpu_data(cpuid).core_id = 0;
612 cpu_data(cpuid).proc_id = -1;
616 cpu_set(cpuid, cpu_present_map);
617 cpu_set(cpuid, cpu_possible_map);
621 smp_fill_in_sib_core_maps();
624 struct device_node *of_console_device;
625 EXPORT_SYMBOL(of_console_device);
627 char *of_console_path;
628 EXPORT_SYMBOL(of_console_path);
630 char *of_console_options;
631 EXPORT_SYMBOL(of_console_options);
633 static void __init of_console_init(void)
635 char *msg = "OF stdout device is: %s\n";
636 struct device_node *dp;
640 of_console_path = prom_early_alloc(256);
641 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
642 prom_printf("Cannot obtain path of stdout.\n");
645 of_console_options = strrchr(of_console_path, ':');
646 if (of_console_options) {
647 of_console_options++;
648 if (*of_console_options == '\0')
649 of_console_options = NULL;
652 node = prom_inst2pkg(prom_stdout);
654 prom_printf("Cannot resolve stdout node from "
655 "instance %08x.\n", prom_stdout);
659 dp = of_find_node_by_phandle(node);
660 type = of_get_property(dp, "device_type", NULL);
662 prom_printf("Console stdout lacks device_type property.\n");
666 if (strcmp(type, "display") && strcmp(type, "serial")) {
667 prom_printf("Console device_type is neither display "
672 of_console_device = dp;
674 printk(msg, of_console_path);
677 void __init prom_build_devicetree(void)
679 struct device_node **nextp;
681 allnodes = create_node(prom_root_node, NULL);
682 allnodes->path_component_name = "";
683 allnodes->full_name = "/";
685 nextp = &allnodes->allnext;
686 allnodes->child = build_tree(allnodes,
687 prom_getchild(allnodes->node),
691 printk("PROM: Built device tree with %u bytes of memory.\n",
692 prom_early_allocated);
694 if (tlb_type != hypervisor)
695 of_fill_in_cpu_data();