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/memblock.h>
26 #include <asm/oplib.h>
34 void * __init
prom_early_alloc(unsigned long size
)
36 unsigned long paddr
= memblock_alloc(size
, SMP_CACHE_BYTES
);
40 prom_printf("prom_early_alloc(%lu) failed\n");
46 prom_early_allocated
+= size
;
51 /* The following routines deal with the black magic of fully naming a
54 * Certain well known named nodes are just the simple name string.
56 * Actual devices have an address specifier appended to the base name
57 * string, like this "foo@addr". The "addr" can be in any number of
58 * formats, and the platform plus the type of the node determine the
59 * format and how it is constructed.
61 * For children of the ROOT node, the naming convention is fixed and
62 * determined by whether this is a sun4u or sun4v system.
64 * For children of other nodes, it is bus type specific. So
65 * we walk up the tree until we discover a "device_type" property
66 * we recognize and we go from there.
68 * As an example, the boot device on my workstation has a full path:
70 * /pci@1e,600000/ide@d/disk@0,0:c
72 static void __init
sun4v_path_component(struct device_node
*dp
, char *tmp_buf
)
74 struct linux_prom64_registers
*regs
;
75 struct property
*rprop
;
76 u32 high_bits
, low_bits
, type
;
78 rprop
= of_find_property(dp
, "reg", NULL
);
83 if (!of_node_is_root(dp
->parent
)) {
84 sprintf(tmp_buf
, "%s@%x,%x",
86 (unsigned int) (regs
->phys_addr
>> 32UL),
87 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
91 type
= regs
->phys_addr
>> 60UL;
92 high_bits
= (regs
->phys_addr
>> 32UL) & 0x0fffffffUL
;
93 low_bits
= (regs
->phys_addr
& 0xffffffffUL
);
95 if (type
== 0 || type
== 8) {
96 const char *prefix
= (type
== 0) ? "m" : "i";
99 sprintf(tmp_buf
, "%s@%s%x,%x",
101 high_bits
, low_bits
);
103 sprintf(tmp_buf
, "%s@%s%x",
107 } else if (type
== 12) {
108 sprintf(tmp_buf
, "%s@%x",
109 dp
->name
, high_bits
);
113 static void __init
sun4u_path_component(struct device_node
*dp
, char *tmp_buf
)
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 struct linux_prom_registers
*regs
;
151 struct property
*prop
;
153 prop
= of_find_property(dp
, "reg", NULL
);
158 sprintf(tmp_buf
, "%s@%x,%x",
164 /* "name@devnum[,func]" */
165 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
167 struct linux_prom_pci_registers
*regs
;
168 struct property
*prop
;
171 prop
= of_find_property(dp
, "reg", NULL
);
176 devfn
= (regs
->phys_hi
>> 8) & 0xff;
178 sprintf(tmp_buf
, "%s@%x,%x",
183 sprintf(tmp_buf
, "%s@%x",
189 /* "name@UPA_PORTID,offset" */
190 static void __init
upa_path_component(struct device_node
*dp
, char *tmp_buf
)
192 struct linux_prom64_registers
*regs
;
193 struct property
*prop
;
195 prop
= of_find_property(dp
, "reg", NULL
);
201 prop
= of_find_property(dp
, "upa-portid", NULL
);
205 sprintf(tmp_buf
, "%s@%x,%x",
207 *(u32
*) prop
->value
,
208 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
212 static void __init
vdev_path_component(struct device_node
*dp
, char *tmp_buf
)
214 struct property
*prop
;
217 prop
= of_find_property(dp
, "reg", NULL
);
223 sprintf(tmp_buf
, "%s@%x", dp
->name
, *regs
);
226 /* "name@addrhi,addrlo" */
227 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
229 struct linux_prom64_registers
*regs
;
230 struct property
*prop
;
232 prop
= of_find_property(dp
, "reg", NULL
);
238 sprintf(tmp_buf
, "%s@%x,%x",
240 (unsigned int) (regs
->phys_addr
>> 32UL),
241 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
244 /* "name@bus,addr" */
245 static void __init
i2c_path_component(struct device_node
*dp
, char *tmp_buf
)
247 struct property
*prop
;
250 prop
= of_find_property(dp
, "reg", NULL
);
256 /* This actually isn't right... should look at the #address-cells
257 * property of the i2c bus node etc. etc.
259 sprintf(tmp_buf
, "%s@%x,%x",
260 dp
->name
, regs
[0], regs
[1]);
263 /* "name@reg0[,reg1]" */
264 static void __init
usb_path_component(struct device_node
*dp
, char *tmp_buf
)
266 struct property
*prop
;
269 prop
= of_find_property(dp
, "reg", NULL
);
275 if (prop
->length
== sizeof(u32
) || regs
[1] == 1) {
276 sprintf(tmp_buf
, "%s@%x",
279 sprintf(tmp_buf
, "%s@%x,%x",
280 dp
->name
, regs
[0], regs
[1]);
284 /* "name@reg0reg1[,reg2reg3]" */
285 static void __init
ieee1394_path_component(struct device_node
*dp
, char *tmp_buf
)
287 struct property
*prop
;
290 prop
= of_find_property(dp
, "reg", NULL
);
296 if (regs
[2] || regs
[3]) {
297 sprintf(tmp_buf
, "%s@%08x%08x,%04x%08x",
298 dp
->name
, regs
[0], regs
[1], regs
[2], regs
[3]);
300 sprintf(tmp_buf
, "%s@%08x%08x",
301 dp
->name
, regs
[0], regs
[1]);
305 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
307 struct device_node
*parent
= dp
->parent
;
309 if (parent
!= NULL
) {
310 if (!strcmp(parent
->type
, "pci") ||
311 !strcmp(parent
->type
, "pciex")) {
312 pci_path_component(dp
, tmp_buf
);
315 if (!strcmp(parent
->type
, "sbus")) {
316 sbus_path_component(dp
, tmp_buf
);
319 if (!strcmp(parent
->type
, "upa")) {
320 upa_path_component(dp
, tmp_buf
);
323 if (!strcmp(parent
->type
, "ebus")) {
324 ebus_path_component(dp
, tmp_buf
);
327 if (!strcmp(parent
->name
, "usb") ||
328 !strcmp(parent
->name
, "hub")) {
329 usb_path_component(dp
, tmp_buf
);
332 if (!strcmp(parent
->type
, "i2c")) {
333 i2c_path_component(dp
, tmp_buf
);
336 if (!strcmp(parent
->type
, "firewire")) {
337 ieee1394_path_component(dp
, tmp_buf
);
340 if (!strcmp(parent
->type
, "virtual-devices")) {
341 vdev_path_component(dp
, tmp_buf
);
344 /* "isa" is handled with platform naming */
347 /* Use platform naming convention. */
348 if (tlb_type
== hypervisor
) {
349 sun4v_path_component(dp
, tmp_buf
);
352 sun4u_path_component(dp
, tmp_buf
);
356 char * __init
build_path_component(struct device_node
*dp
)
358 char tmp_buf
[64], *n
;
361 __build_path_component(dp
, tmp_buf
);
362 if (tmp_buf
[0] == '\0')
363 strcpy(tmp_buf
, dp
->name
);
365 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
371 static const char *get_mid_prop(void)
373 return (tlb_type
== spitfire
? "upa-portid" : "portid");
376 static void *of_iterate_over_cpus(void *(*func
)(struct device_node
*, int, int), int arg
)
378 struct device_node
*dp
;
379 const char *mid_prop
;
381 mid_prop
= get_mid_prop();
382 for_each_node_by_type(dp
, "cpu") {
383 int cpuid
= of_getintprop_default(dp
, mid_prop
, -1);
384 const char *this_mid_prop
= mid_prop
;
388 this_mid_prop
= "cpuid";
389 cpuid
= of_getintprop_default(dp
, this_mid_prop
, -1);
392 prom_printf("OF: Serious problem, cpu lacks "
393 "%s property", this_mid_prop
);
397 if (cpuid
>= NR_CPUS
) {
398 printk(KERN_WARNING
"Ignoring CPU %d which is "
404 ret
= func(dp
, cpuid
, arg
);
411 static void *check_cpu_node(struct device_node
*dp
, int cpuid
, int id
)
418 struct device_node
*of_find_node_by_cpuid(int cpuid
)
420 return of_iterate_over_cpus(check_cpu_node
, cpuid
);
423 static void *record_one_cpu(struct device_node
*dp
, int cpuid
, int arg
)
427 set_cpu_present(cpuid
, true);
428 set_cpu_possible(cpuid
, true);
433 void __init
of_populate_present_mask(void)
435 if (tlb_type
== hypervisor
)
439 of_iterate_over_cpus(record_one_cpu
, 0);
442 static void *fill_in_one_cpu(struct device_node
*dp
, int cpuid
, int arg
)
444 struct device_node
*portid_parent
= NULL
;
447 if (of_find_property(dp
, "cpuid", NULL
)) {
452 portid_parent
= portid_parent
->parent
;
455 portid
= of_getintprop_default(portid_parent
,
463 /* On uniprocessor we only want the values for the
464 * real physical cpu the kernel booted onto, however
465 * cpu_data() only has one entry at index 0.
467 if (cpuid
!= real_hard_smp_processor_id())
472 cpu_data(cpuid
).clock_tick
=
473 of_getintprop_default(dp
, "clock-frequency", 0);
476 cpu_data(cpuid
).dcache_size
=
477 of_getintprop_default(dp
, "l1-dcache-size",
479 cpu_data(cpuid
).dcache_line_size
=
480 of_getintprop_default(dp
, "l1-dcache-line-size",
482 cpu_data(cpuid
).icache_size
=
483 of_getintprop_default(dp
, "l1-icache-size",
485 cpu_data(cpuid
).icache_line_size
=
486 of_getintprop_default(dp
, "l1-icache-line-size",
488 cpu_data(cpuid
).ecache_size
=
489 of_getintprop_default(dp
, "l2-cache-size", 0);
490 cpu_data(cpuid
).ecache_line_size
=
491 of_getintprop_default(dp
, "l2-cache-line-size", 0);
492 if (!cpu_data(cpuid
).ecache_size
||
493 !cpu_data(cpuid
).ecache_line_size
) {
494 cpu_data(cpuid
).ecache_size
=
495 of_getintprop_default(portid_parent
,
498 cpu_data(cpuid
).ecache_line_size
=
499 of_getintprop_default(portid_parent
,
500 "l2-cache-line-size", 64);
503 cpu_data(cpuid
).core_id
= portid
+ 1;
504 cpu_data(cpuid
).proc_id
= portid
;
506 sparc64_multi_core
= 1;
509 cpu_data(cpuid
).dcache_size
=
510 of_getintprop_default(dp
, "dcache-size", 16 * 1024);
511 cpu_data(cpuid
).dcache_line_size
=
512 of_getintprop_default(dp
, "dcache-line-size", 32);
514 cpu_data(cpuid
).icache_size
=
515 of_getintprop_default(dp
, "icache-size", 16 * 1024);
516 cpu_data(cpuid
).icache_line_size
=
517 of_getintprop_default(dp
, "icache-line-size", 32);
519 cpu_data(cpuid
).ecache_size
=
520 of_getintprop_default(dp
, "ecache-size",
522 cpu_data(cpuid
).ecache_line_size
=
523 of_getintprop_default(dp
, "ecache-line-size", 64);
525 cpu_data(cpuid
).core_id
= 0;
526 cpu_data(cpuid
).proc_id
= -1;
532 void __init
of_fill_in_cpu_data(void)
534 if (tlb_type
== hypervisor
)
537 of_iterate_over_cpus(fill_in_one_cpu
, 0);
539 smp_fill_in_sib_core_maps();
542 void __init
of_console_init(void)
544 char *msg
= "OF stdout device is: %s\n";
545 struct device_node
*dp
;
549 of_console_path
= prom_early_alloc(256);
550 if (prom_ihandle2path(prom_stdout
, of_console_path
, 256) < 0) {
551 prom_printf("Cannot obtain path of stdout.\n");
554 of_console_options
= strrchr(of_console_path
, ':');
555 if (of_console_options
) {
556 of_console_options
++;
557 if (*of_console_options
== '\0')
558 of_console_options
= NULL
;
561 node
= prom_inst2pkg(prom_stdout
);
563 prom_printf("Cannot resolve stdout node from "
564 "instance %08x.\n", prom_stdout
);
568 dp
= of_find_node_by_phandle(node
);
569 type
= of_get_property(dp
, "device_type", NULL
);
571 prom_printf("Console stdout lacks device_type property.\n");
575 if (strcmp(type
, "display") && strcmp(type
, "serial")) {
576 prom_printf("Console device_type is neither display "
581 of_console_device
= dp
;
583 printk(msg
, of_console_path
);