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 /* The following routines deal with the black magic of fully naming a
57 * Certain well known named nodes are just the simple name string.
59 * Actual devices have an address specifier appended to the base name
60 * string, like this "foo@addr". The "addr" can be in any number of
61 * formats, and the platform plus the type of the node determine the
62 * format and how it is constructed.
64 * For children of the ROOT node, the naming convention is fixed and
65 * determined by whether this is a sun4u or sun4v system.
67 * For children of other nodes, it is bus type specific. So
68 * we walk up the tree until we discover a "device_type" property
69 * we recognize and we go from there.
71 * As an example, the boot device on my workstation has a full path:
73 * /pci@1e,600000/ide@d/disk@0,0:c
75 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
77 struct linux_prom64_registers *regs;
78 struct property *rprop;
79 u32 high_bits, low_bits, type;
81 rprop = of_find_property(dp, "reg", NULL);
86 if (!is_root_node(dp->parent)) {
87 sprintf(tmp_buf, "%s@%x,%x",
89 (unsigned int) (regs->phys_addr >> 32UL),
90 (unsigned int) (regs->phys_addr & 0xffffffffUL));
94 type = regs->phys_addr >> 60UL;
95 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
96 low_bits = (regs->phys_addr & 0xffffffffUL);
98 if (type == 0 || type == 8) {
99 const char *prefix = (type == 0) ? "m" : "i";
102 sprintf(tmp_buf, "%s@%s%x,%x",
104 high_bits, low_bits);
106 sprintf(tmp_buf, "%s@%s%x",
110 } else if (type == 12) {
111 sprintf(tmp_buf, "%s@%x",
112 dp->name, high_bits);
116 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
118 struct linux_prom64_registers *regs;
119 struct property *prop;
121 prop = of_find_property(dp, "reg", NULL);
126 if (!is_root_node(dp->parent)) {
127 sprintf(tmp_buf, "%s@%x,%x",
129 (unsigned int) (regs->phys_addr >> 32UL),
130 (unsigned int) (regs->phys_addr & 0xffffffffUL));
134 prop = of_find_property(dp, "upa-portid", NULL);
136 prop = of_find_property(dp, "portid", NULL);
138 unsigned long mask = 0xffffffffUL;
140 if (tlb_type >= cheetah)
143 sprintf(tmp_buf, "%s@%x,%x",
146 (unsigned int) (regs->phys_addr & mask));
150 /* "name@slot,offset" */
151 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
153 struct linux_prom_registers *regs;
154 struct property *prop;
156 prop = of_find_property(dp, "reg", NULL);
161 sprintf(tmp_buf, "%s@%x,%x",
167 /* "name@devnum[,func]" */
168 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
170 struct linux_prom_pci_registers *regs;
171 struct property *prop;
174 prop = of_find_property(dp, "reg", NULL);
179 devfn = (regs->phys_hi >> 8) & 0xff;
181 sprintf(tmp_buf, "%s@%x,%x",
186 sprintf(tmp_buf, "%s@%x",
192 /* "name@UPA_PORTID,offset" */
193 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
195 struct linux_prom64_registers *regs;
196 struct property *prop;
198 prop = of_find_property(dp, "reg", NULL);
204 prop = of_find_property(dp, "upa-portid", NULL);
208 sprintf(tmp_buf, "%s@%x,%x",
210 *(u32 *) prop->value,
211 (unsigned int) (regs->phys_addr & 0xffffffffUL));
215 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
217 struct property *prop;
220 prop = of_find_property(dp, "reg", NULL);
226 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
229 /* "name@addrhi,addrlo" */
230 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
232 struct linux_prom64_registers *regs;
233 struct property *prop;
235 prop = of_find_property(dp, "reg", NULL);
241 sprintf(tmp_buf, "%s@%x,%x",
243 (unsigned int) (regs->phys_addr >> 32UL),
244 (unsigned int) (regs->phys_addr & 0xffffffffUL));
247 /* "name@bus,addr" */
248 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
250 struct property *prop;
253 prop = of_find_property(dp, "reg", NULL);
259 /* This actually isn't right... should look at the #address-cells
260 * property of the i2c bus node etc. etc.
262 sprintf(tmp_buf, "%s@%x,%x",
263 dp->name, regs[0], regs[1]);
266 /* "name@reg0[,reg1]" */
267 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
269 struct property *prop;
272 prop = of_find_property(dp, "reg", NULL);
278 if (prop->length == sizeof(u32) || regs[1] == 1) {
279 sprintf(tmp_buf, "%s@%x",
282 sprintf(tmp_buf, "%s@%x,%x",
283 dp->name, regs[0], regs[1]);
287 /* "name@reg0reg1[,reg2reg3]" */
288 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
290 struct property *prop;
293 prop = of_find_property(dp, "reg", NULL);
299 if (regs[2] || regs[3]) {
300 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
301 dp->name, regs[0], regs[1], regs[2], regs[3]);
303 sprintf(tmp_buf, "%s@%08x%08x",
304 dp->name, regs[0], regs[1]);
308 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
310 struct device_node *parent = dp->parent;
312 if (parent != NULL) {
313 if (!strcmp(parent->type, "pci") ||
314 !strcmp(parent->type, "pciex")) {
315 pci_path_component(dp, tmp_buf);
318 if (!strcmp(parent->type, "sbus")) {
319 sbus_path_component(dp, tmp_buf);
322 if (!strcmp(parent->type, "upa")) {
323 upa_path_component(dp, tmp_buf);
326 if (!strcmp(parent->type, "ebus")) {
327 ebus_path_component(dp, tmp_buf);
330 if (!strcmp(parent->name, "usb") ||
331 !strcmp(parent->name, "hub")) {
332 usb_path_component(dp, tmp_buf);
335 if (!strcmp(parent->type, "i2c")) {
336 i2c_path_component(dp, tmp_buf);
339 if (!strcmp(parent->type, "firewire")) {
340 ieee1394_path_component(dp, tmp_buf);
343 if (!strcmp(parent->type, "virtual-devices")) {
344 vdev_path_component(dp, tmp_buf);
347 /* "isa" is handled with platform naming */
350 /* Use platform naming convention. */
351 if (tlb_type == hypervisor) {
352 sun4v_path_component(dp, tmp_buf);
355 sun4u_path_component(dp, tmp_buf);
359 char * __init build_path_component(struct device_node *dp)
361 char tmp_buf[64], *n;
364 __build_path_component(dp, tmp_buf);
365 if (tmp_buf[0] == '\0')
366 strcpy(tmp_buf, dp->name);
368 n = prom_early_alloc(strlen(tmp_buf) + 1);
374 static const char *get_mid_prop(void)
376 return (tlb_type == spitfire ? "upa-portid" : "portid");
379 struct device_node *of_find_node_by_cpuid(int cpuid)
381 struct device_node *dp;
382 const char *mid_prop = get_mid_prop();
384 for_each_node_by_type(dp, "cpu") {
385 int id = of_getintprop_default(dp, mid_prop, -1);
386 const char *this_mid_prop = mid_prop;
389 this_mid_prop = "cpuid";
390 id = of_getintprop_default(dp, this_mid_prop, -1);
394 prom_printf("OF: Serious problem, cpu lacks "
395 "%s property", this_mid_prop);
404 static void __init of_fill_in_cpu_data(void)
406 struct device_node *dp;
407 const char *mid_prop = get_mid_prop();
410 for_each_node_by_type(dp, "cpu") {
411 int cpuid = of_getintprop_default(dp, mid_prop, -1);
412 const char *this_mid_prop = mid_prop;
413 struct device_node *portid_parent;
416 portid_parent = NULL;
418 this_mid_prop = "cpuid";
419 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
425 portid_parent = portid_parent->parent;
428 portid = of_getintprop_default(portid_parent,
437 prom_printf("OF: Serious problem, cpu lacks "
438 "%s property", this_mid_prop);
445 if (cpuid >= NR_CPUS) {
446 printk(KERN_WARNING "Ignoring CPU %d which is "
452 /* On uniprocessor we only want the values for the
453 * real physical cpu the kernel booted onto, however
454 * cpu_data() only has one entry at index 0.
456 if (cpuid != real_hard_smp_processor_id())
461 cpu_data(cpuid).clock_tick =
462 of_getintprop_default(dp, "clock-frequency", 0);
465 cpu_data(cpuid).dcache_size =
466 of_getintprop_default(dp, "l1-dcache-size",
468 cpu_data(cpuid).dcache_line_size =
469 of_getintprop_default(dp, "l1-dcache-line-size",
471 cpu_data(cpuid).icache_size =
472 of_getintprop_default(dp, "l1-icache-size",
474 cpu_data(cpuid).icache_line_size =
475 of_getintprop_default(dp, "l1-icache-line-size",
477 cpu_data(cpuid).ecache_size =
478 of_getintprop_default(dp, "l2-cache-size", 0);
479 cpu_data(cpuid).ecache_line_size =
480 of_getintprop_default(dp, "l2-cache-line-size", 0);
481 if (!cpu_data(cpuid).ecache_size ||
482 !cpu_data(cpuid).ecache_line_size) {
483 cpu_data(cpuid).ecache_size =
484 of_getintprop_default(portid_parent,
487 cpu_data(cpuid).ecache_line_size =
488 of_getintprop_default(portid_parent,
489 "l2-cache-line-size", 64);
492 cpu_data(cpuid).core_id = portid + 1;
493 cpu_data(cpuid).proc_id = portid;
495 sparc64_multi_core = 1;
498 cpu_data(cpuid).dcache_size =
499 of_getintprop_default(dp, "dcache-size", 16 * 1024);
500 cpu_data(cpuid).dcache_line_size =
501 of_getintprop_default(dp, "dcache-line-size", 32);
503 cpu_data(cpuid).icache_size =
504 of_getintprop_default(dp, "icache-size", 16 * 1024);
505 cpu_data(cpuid).icache_line_size =
506 of_getintprop_default(dp, "icache-line-size", 32);
508 cpu_data(cpuid).ecache_size =
509 of_getintprop_default(dp, "ecache-size",
511 cpu_data(cpuid).ecache_line_size =
512 of_getintprop_default(dp, "ecache-line-size", 64);
514 cpu_data(cpuid).core_id = 0;
515 cpu_data(cpuid).proc_id = -1;
519 cpu_set(cpuid, cpu_present_map);
520 cpu_set(cpuid, cpu_possible_map);
524 smp_fill_in_sib_core_maps();
527 struct device_node *of_console_device;
528 EXPORT_SYMBOL(of_console_device);
530 char *of_console_path;
531 EXPORT_SYMBOL(of_console_path);
533 char *of_console_options;
534 EXPORT_SYMBOL(of_console_options);
536 static void __init of_console_init(void)
538 char *msg = "OF stdout device is: %s\n";
539 struct device_node *dp;
543 of_console_path = prom_early_alloc(256);
544 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
545 prom_printf("Cannot obtain path of stdout.\n");
548 of_console_options = strrchr(of_console_path, ':');
549 if (of_console_options) {
550 of_console_options++;
551 if (*of_console_options == '\0')
552 of_console_options = NULL;
555 node = prom_inst2pkg(prom_stdout);
557 prom_printf("Cannot resolve stdout node from "
558 "instance %08x.\n", prom_stdout);
562 dp = of_find_node_by_phandle(node);
563 type = of_get_property(dp, "device_type", NULL);
565 prom_printf("Console stdout lacks device_type property.\n");
569 if (strcmp(type, "display") && strcmp(type, "serial")) {
570 prom_printf("Console device_type is neither display "
575 of_console_device = dp;
577 printk(msg, of_console_path);
580 void __init prom_build_devicetree(void)
582 struct device_node **nextp;
584 allnodes = prom_create_node(prom_root_node, NULL);
585 allnodes->path_component_name = "";
586 allnodes->full_name = "/";
588 nextp = &allnodes->allnext;
589 allnodes->child = prom_build_tree(allnodes,
590 prom_getchild(allnodes->node),
594 printk("PROM: Built device tree with %u bytes of memory.\n",
595 prom_early_allocated);
597 if (tlb_type != hypervisor)
598 of_fill_in_cpu_data();