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/memblock.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/cpu.h>
27 #include <asm/oplib.h>
35 void * __init prom_early_alloc(unsigned long size)
37 void *ret = memblock_alloc(size, SMP_CACHE_BYTES);
40 prom_printf("prom_early_alloc(%lu) failed\n", size);
44 prom_early_allocated += size;
49 /* The following routines deal with the black magic of fully naming a
52 * Certain well known named nodes are just the simple name string.
54 * Actual devices have an address specifier appended to the base name
55 * string, like this "foo@addr". The "addr" can be in any number of
56 * formats, and the platform plus the type of the node determine the
57 * format and how it is constructed.
59 * For children of the ROOT node, the naming convention is fixed and
60 * determined by whether this is a sun4u or sun4v system.
62 * For children of other nodes, it is bus type specific. So
63 * we walk up the tree until we discover a "device_type" property
64 * we recognize and we go from there.
66 * As an example, the boot device on my workstation has a full path:
68 * /pci@1e,600000/ide@d/disk@0,0:c
70 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
72 const char *name = of_get_property(dp, "name", NULL);
73 struct linux_prom64_registers *regs;
74 struct property *rprop;
75 u32 high_bits, low_bits, type;
77 rprop = of_find_property(dp, "reg", NULL);
82 if (!of_node_is_root(dp->parent)) {
83 sprintf(tmp_buf, "%s@%x,%x",
85 (unsigned int) (regs->phys_addr >> 32UL),
86 (unsigned int) (regs->phys_addr & 0xffffffffUL));
90 type = regs->phys_addr >> 60UL;
91 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
92 low_bits = (regs->phys_addr & 0xffffffffUL);
94 if (type == 0 || type == 8) {
95 const char *prefix = (type == 0) ? "m" : "i";
98 sprintf(tmp_buf, "%s@%s%x,%x",
100 high_bits, low_bits);
102 sprintf(tmp_buf, "%s@%s%x",
106 } else if (type == 12) {
107 sprintf(tmp_buf, "%s@%x",
112 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
114 const char *name = of_get_property(dp, "name", NULL);
115 struct linux_prom64_registers *regs;
116 struct property *prop;
118 prop = of_find_property(dp, "reg", NULL);
123 if (!of_node_is_root(dp->parent)) {
124 sprintf(tmp_buf, "%s@%x,%x",
126 (unsigned int) (regs->phys_addr >> 32UL),
127 (unsigned int) (regs->phys_addr & 0xffffffffUL));
131 prop = of_find_property(dp, "upa-portid", NULL);
133 prop = of_find_property(dp, "portid", NULL);
135 unsigned long mask = 0xffffffffUL;
137 if (tlb_type >= cheetah)
140 sprintf(tmp_buf, "%s@%x,%x",
143 (unsigned int) (regs->phys_addr & mask));
147 /* "name@slot,offset" */
148 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
150 const char *name = of_get_property(dp, "name", NULL);
151 struct linux_prom_registers *regs;
152 struct property *prop;
154 prop = of_find_property(dp, "reg", NULL);
159 sprintf(tmp_buf, "%s@%x,%x",
165 /* "name@devnum[,func]" */
166 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
168 const char *name = of_get_property(dp, "name", NULL);
169 struct linux_prom_pci_registers *regs;
170 struct property *prop;
173 prop = of_find_property(dp, "reg", NULL);
178 devfn = (regs->phys_hi >> 8) & 0xff;
180 sprintf(tmp_buf, "%s@%x,%x",
185 sprintf(tmp_buf, "%s@%x",
191 /* "name@UPA_PORTID,offset" */
192 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
194 const char *name = of_get_property(dp, "name", NULL);
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 const char *name = of_get_property(dp, "name", NULL);
218 struct property *prop;
221 prop = of_find_property(dp, "reg", NULL);
227 sprintf(tmp_buf, "%s@%x", name, *regs);
230 /* "name@addrhi,addrlo" */
231 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
233 const char *name = of_get_property(dp, "name", NULL);
234 struct linux_prom64_registers *regs;
235 struct property *prop;
237 prop = of_find_property(dp, "reg", NULL);
243 sprintf(tmp_buf, "%s@%x,%x",
245 (unsigned int) (regs->phys_addr >> 32UL),
246 (unsigned int) (regs->phys_addr & 0xffffffffUL));
249 /* "name@bus,addr" */
250 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
252 const char *name = of_get_property(dp, "name", NULL);
253 struct property *prop;
256 prop = of_find_property(dp, "reg", NULL);
262 /* This actually isn't right... should look at the #address-cells
263 * property of the i2c bus node etc. etc.
265 sprintf(tmp_buf, "%s@%x,%x",
266 name, regs[0], regs[1]);
269 /* "name@reg0[,reg1]" */
270 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
272 const char *name = of_get_property(dp, "name", NULL);
273 struct property *prop;
276 prop = of_find_property(dp, "reg", NULL);
282 if (prop->length == sizeof(u32) || regs[1] == 1) {
283 sprintf(tmp_buf, "%s@%x",
286 sprintf(tmp_buf, "%s@%x,%x",
287 name, regs[0], regs[1]);
291 /* "name@reg0reg1[,reg2reg3]" */
292 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
294 const char *name = of_get_property(dp, "name", NULL);
295 struct property *prop;
298 prop = of_find_property(dp, "reg", NULL);
304 if (regs[2] || regs[3]) {
305 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
306 name, regs[0], regs[1], regs[2], regs[3]);
308 sprintf(tmp_buf, "%s@%08x%08x",
309 name, regs[0], regs[1]);
313 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
315 struct device_node *parent = dp->parent;
317 if (parent != NULL) {
318 if (of_node_is_type(parent, "pci") ||
319 of_node_is_type(parent, "pciex")) {
320 pci_path_component(dp, tmp_buf);
323 if (of_node_is_type(parent, "sbus")) {
324 sbus_path_component(dp, tmp_buf);
327 if (of_node_is_type(parent, "upa")) {
328 upa_path_component(dp, tmp_buf);
331 if (of_node_is_type(parent, "ebus")) {
332 ebus_path_component(dp, tmp_buf);
335 if (of_node_name_eq(parent, "usb") ||
336 of_node_name_eq(parent, "hub")) {
337 usb_path_component(dp, tmp_buf);
340 if (of_node_is_type(parent, "i2c")) {
341 i2c_path_component(dp, tmp_buf);
344 if (of_node_is_type(parent, "firewire")) {
345 ieee1394_path_component(dp, tmp_buf);
348 if (of_node_is_type(parent, "virtual-devices")) {
349 vdev_path_component(dp, tmp_buf);
352 /* "isa" is handled with platform naming */
355 /* Use platform naming convention. */
356 if (tlb_type == hypervisor) {
357 sun4v_path_component(dp, tmp_buf);
360 sun4u_path_component(dp, tmp_buf);
364 char * __init build_path_component(struct device_node *dp)
366 const char *name = of_get_property(dp, "name", NULL);
367 char tmp_buf[64], *n;
370 __build_path_component(dp, tmp_buf);
371 if (tmp_buf[0] == '\0')
372 strcpy(tmp_buf, name);
374 n = prom_early_alloc(strlen(tmp_buf) + 1);
380 static const char *get_mid_prop(void)
382 return (tlb_type == spitfire ? "upa-portid" : "portid");
385 bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
386 int cpu, unsigned int *thread)
388 const char *mid_prop = get_mid_prop();
391 /* On hypervisor based platforms we interrogate the 'reg'
392 * property. On everything else we look for a 'upa-portid',
393 * 'portid', or 'cpuid' property.
396 if (tlb_type == hypervisor) {
397 struct property *prop = of_find_property(cpun, "reg", NULL);
401 pr_warn("CPU node missing reg property\n");
405 this_cpu_id = regs[0] & 0x0fffffff;
407 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
409 if (this_cpu_id < 0) {
411 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
413 if (this_cpu_id < 0) {
414 pr_warn("CPU node missing cpu ID property\n");
418 if (this_cpu_id == cpu) {
420 int proc_id = cpu_data(cpu).proc_id;
422 /* On sparc64, the cpu thread information is obtained
423 * either from OBP or the machine description. We've
424 * actually probed this information already long before
425 * this interface gets called so instead of interrogating
426 * both the OF node and the MDESC again, just use what
427 * we discovered already.
438 static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
440 struct device_node *dp;
441 const char *mid_prop;
443 mid_prop = get_mid_prop();
444 for_each_node_by_type(dp, "cpu") {
445 int cpuid = of_getintprop_default(dp, mid_prop, -1);
446 const char *this_mid_prop = mid_prop;
450 this_mid_prop = "cpuid";
451 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
454 prom_printf("OF: Serious problem, cpu lacks "
455 "%s property", this_mid_prop);
459 if (cpuid >= NR_CPUS) {
460 printk(KERN_WARNING "Ignoring CPU %d which is "
466 ret = func(dp, cpuid, arg);
473 static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
480 struct device_node *of_find_node_by_cpuid(int cpuid)
482 return of_iterate_over_cpus(check_cpu_node, cpuid);
485 static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
489 set_cpu_present(cpuid, true);
490 set_cpu_possible(cpuid, true);
495 void __init of_populate_present_mask(void)
497 if (tlb_type == hypervisor)
501 of_iterate_over_cpus(record_one_cpu, 0);
504 static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
506 struct device_node *portid_parent = NULL;
509 if (of_find_property(dp, "cpuid", NULL)) {
514 portid_parent = portid_parent->parent;
517 portid = of_getintprop_default(portid_parent,
525 /* On uniprocessor we only want the values for the
526 * real physical cpu the kernel booted onto, however
527 * cpu_data() only has one entry at index 0.
529 if (cpuid != real_hard_smp_processor_id())
534 cpu_data(cpuid).clock_tick =
535 of_getintprop_default(dp, "clock-frequency", 0);
538 cpu_data(cpuid).dcache_size =
539 of_getintprop_default(dp, "l1-dcache-size",
541 cpu_data(cpuid).dcache_line_size =
542 of_getintprop_default(dp, "l1-dcache-line-size",
544 cpu_data(cpuid).icache_size =
545 of_getintprop_default(dp, "l1-icache-size",
547 cpu_data(cpuid).icache_line_size =
548 of_getintprop_default(dp, "l1-icache-line-size",
550 cpu_data(cpuid).ecache_size =
551 of_getintprop_default(dp, "l2-cache-size", 0);
552 cpu_data(cpuid).ecache_line_size =
553 of_getintprop_default(dp, "l2-cache-line-size", 0);
554 if (!cpu_data(cpuid).ecache_size ||
555 !cpu_data(cpuid).ecache_line_size) {
556 cpu_data(cpuid).ecache_size =
557 of_getintprop_default(portid_parent,
560 cpu_data(cpuid).ecache_line_size =
561 of_getintprop_default(portid_parent,
562 "l2-cache-line-size", 64);
565 cpu_data(cpuid).core_id = portid + 1;
566 cpu_data(cpuid).proc_id = portid;
568 cpu_data(cpuid).dcache_size =
569 of_getintprop_default(dp, "dcache-size", 16 * 1024);
570 cpu_data(cpuid).dcache_line_size =
571 of_getintprop_default(dp, "dcache-line-size", 32);
573 cpu_data(cpuid).icache_size =
574 of_getintprop_default(dp, "icache-size", 16 * 1024);
575 cpu_data(cpuid).icache_line_size =
576 of_getintprop_default(dp, "icache-line-size", 32);
578 cpu_data(cpuid).ecache_size =
579 of_getintprop_default(dp, "ecache-size",
581 cpu_data(cpuid).ecache_line_size =
582 of_getintprop_default(dp, "ecache-line-size", 64);
584 cpu_data(cpuid).core_id = 0;
585 cpu_data(cpuid).proc_id = -1;
591 void __init of_fill_in_cpu_data(void)
593 if (tlb_type == hypervisor)
596 of_iterate_over_cpus(fill_in_one_cpu, 0);
598 smp_fill_in_sib_core_maps();
601 void __init of_console_init(void)
603 char *msg = "OF stdout device is: %s\n";
604 struct device_node *dp;
607 of_console_path = prom_early_alloc(256);
608 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
609 prom_printf("Cannot obtain path of stdout.\n");
612 of_console_options = strrchr(of_console_path, ':');
613 if (of_console_options) {
614 of_console_options++;
615 if (*of_console_options == '\0')
616 of_console_options = NULL;
619 node = prom_inst2pkg(prom_stdout);
621 prom_printf("Cannot resolve stdout node from "
622 "instance %08x.\n", prom_stdout);
626 dp = of_find_node_by_phandle(node);
628 if (!of_node_is_type(dp, "display") && !of_node_is_type(dp, "serial")) {
629 prom_printf("Console device_type is neither display "
634 of_console_device = dp;
636 printk(msg, of_console_path);