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 unsigned long paddr = memblock_alloc(size, SMP_CACHE_BYTES);
41 prom_printf("prom_early_alloc(%lu) failed\n", size);
47 prom_early_allocated += size;
52 /* The following routines deal with the black magic of fully naming a
55 * Certain well known named nodes are just the simple name string.
57 * Actual devices have an address specifier appended to the base name
58 * string, like this "foo@addr". The "addr" can be in any number of
59 * formats, and the platform plus the type of the node determine the
60 * format and how it is constructed.
62 * For children of the ROOT node, the naming convention is fixed and
63 * determined by whether this is a sun4u or sun4v system.
65 * For children of other nodes, it is bus type specific. So
66 * we walk up the tree until we discover a "device_type" property
67 * we recognize and we go from there.
69 * As an example, the boot device on my workstation has a full path:
71 * /pci@1e,600000/ide@d/disk@0,0:c
73 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
75 struct linux_prom64_registers *regs;
76 struct property *rprop;
77 u32 high_bits, low_bits, type;
79 rprop = of_find_property(dp, "reg", NULL);
84 if (!of_node_is_root(dp->parent)) {
85 sprintf(tmp_buf, "%s@%x,%x",
87 (unsigned int) (regs->phys_addr >> 32UL),
88 (unsigned int) (regs->phys_addr & 0xffffffffUL));
92 type = regs->phys_addr >> 60UL;
93 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
94 low_bits = (regs->phys_addr & 0xffffffffUL);
96 if (type == 0 || type == 8) {
97 const char *prefix = (type == 0) ? "m" : "i";
100 sprintf(tmp_buf, "%s@%s%x,%x",
102 high_bits, low_bits);
104 sprintf(tmp_buf, "%s@%s%x",
108 } else if (type == 12) {
109 sprintf(tmp_buf, "%s@%x",
110 dp->name, high_bits);
114 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
116 struct linux_prom64_registers *regs;
117 struct property *prop;
119 prop = of_find_property(dp, "reg", NULL);
124 if (!of_node_is_root(dp->parent)) {
125 sprintf(tmp_buf, "%s@%x,%x",
127 (unsigned int) (regs->phys_addr >> 32UL),
128 (unsigned int) (regs->phys_addr & 0xffffffffUL));
132 prop = of_find_property(dp, "upa-portid", NULL);
134 prop = of_find_property(dp, "portid", NULL);
136 unsigned long mask = 0xffffffffUL;
138 if (tlb_type >= cheetah)
141 sprintf(tmp_buf, "%s@%x,%x",
144 (unsigned int) (regs->phys_addr & mask));
148 /* "name@slot,offset" */
149 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
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 struct linux_prom_pci_registers *regs;
169 struct property *prop;
172 prop = of_find_property(dp, "reg", NULL);
177 devfn = (regs->phys_hi >> 8) & 0xff;
179 sprintf(tmp_buf, "%s@%x,%x",
184 sprintf(tmp_buf, "%s@%x",
190 /* "name@UPA_PORTID,offset" */
191 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
193 struct linux_prom64_registers *regs;
194 struct property *prop;
196 prop = of_find_property(dp, "reg", NULL);
202 prop = of_find_property(dp, "upa-portid", NULL);
206 sprintf(tmp_buf, "%s@%x,%x",
208 *(u32 *) prop->value,
209 (unsigned int) (regs->phys_addr & 0xffffffffUL));
213 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
215 struct property *prop;
218 prop = of_find_property(dp, "reg", NULL);
224 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
227 /* "name@addrhi,addrlo" */
228 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
230 struct linux_prom64_registers *regs;
231 struct property *prop;
233 prop = of_find_property(dp, "reg", NULL);
239 sprintf(tmp_buf, "%s@%x,%x",
241 (unsigned int) (regs->phys_addr >> 32UL),
242 (unsigned int) (regs->phys_addr & 0xffffffffUL));
245 /* "name@bus,addr" */
246 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
248 struct property *prop;
251 prop = of_find_property(dp, "reg", NULL);
257 /* This actually isn't right... should look at the #address-cells
258 * property of the i2c bus node etc. etc.
260 sprintf(tmp_buf, "%s@%x,%x",
261 dp->name, regs[0], regs[1]);
264 /* "name@reg0[,reg1]" */
265 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
267 struct property *prop;
270 prop = of_find_property(dp, "reg", NULL);
276 if (prop->length == sizeof(u32) || regs[1] == 1) {
277 sprintf(tmp_buf, "%s@%x",
280 sprintf(tmp_buf, "%s@%x,%x",
281 dp->name, regs[0], regs[1]);
285 /* "name@reg0reg1[,reg2reg3]" */
286 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
288 struct property *prop;
291 prop = of_find_property(dp, "reg", NULL);
297 if (regs[2] || regs[3]) {
298 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
299 dp->name, regs[0], regs[1], regs[2], regs[3]);
301 sprintf(tmp_buf, "%s@%08x%08x",
302 dp->name, regs[0], regs[1]);
306 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
308 struct device_node *parent = dp->parent;
310 if (parent != NULL) {
311 if (!strcmp(parent->type, "pci") ||
312 !strcmp(parent->type, "pciex")) {
313 pci_path_component(dp, tmp_buf);
316 if (!strcmp(parent->type, "sbus")) {
317 sbus_path_component(dp, tmp_buf);
320 if (!strcmp(parent->type, "upa")) {
321 upa_path_component(dp, tmp_buf);
324 if (!strcmp(parent->type, "ebus")) {
325 ebus_path_component(dp, tmp_buf);
328 if (!strcmp(parent->name, "usb") ||
329 !strcmp(parent->name, "hub")) {
330 usb_path_component(dp, tmp_buf);
333 if (!strcmp(parent->type, "i2c")) {
334 i2c_path_component(dp, tmp_buf);
337 if (!strcmp(parent->type, "firewire")) {
338 ieee1394_path_component(dp, tmp_buf);
341 if (!strcmp(parent->type, "virtual-devices")) {
342 vdev_path_component(dp, tmp_buf);
345 /* "isa" is handled with platform naming */
348 /* Use platform naming convention. */
349 if (tlb_type == hypervisor) {
350 sun4v_path_component(dp, tmp_buf);
353 sun4u_path_component(dp, tmp_buf);
357 char * __init build_path_component(struct device_node *dp)
359 char tmp_buf[64], *n;
362 __build_path_component(dp, tmp_buf);
363 if (tmp_buf[0] == '\0')
364 strcpy(tmp_buf, dp->name);
366 n = prom_early_alloc(strlen(tmp_buf) + 1);
372 static const char *get_mid_prop(void)
374 return (tlb_type == spitfire ? "upa-portid" : "portid");
377 bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
378 int cpu, unsigned int *thread)
380 const char *mid_prop = get_mid_prop();
383 /* On hypervisor based platforms we interrogate the 'reg'
384 * property. On everything else we look for a 'upa-portis',
385 * 'portid', or 'cpuid' property.
388 if (tlb_type == hypervisor) {
389 struct property *prop = of_find_property(cpun, "reg", NULL);
393 pr_warn("CPU node missing reg property\n");
397 this_cpu_id = regs[0] & 0x0fffffff;
399 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
401 if (this_cpu_id < 0) {
403 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
405 if (this_cpu_id < 0) {
406 pr_warn("CPU node missing cpu ID property\n");
410 if (this_cpu_id == cpu) {
412 int proc_id = cpu_data(cpu).proc_id;
414 /* On sparc64, the cpu thread information is obtained
415 * either from OBP or the machine description. We've
416 * actually probed this information already long before
417 * this interface gets called so instead of interrogating
418 * both the OF node and the MDESC again, just use what
419 * we discovered already.
430 static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
432 struct device_node *dp;
433 const char *mid_prop;
435 mid_prop = get_mid_prop();
436 for_each_node_by_type(dp, "cpu") {
437 int cpuid = of_getintprop_default(dp, mid_prop, -1);
438 const char *this_mid_prop = mid_prop;
442 this_mid_prop = "cpuid";
443 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
446 prom_printf("OF: Serious problem, cpu lacks "
447 "%s property", this_mid_prop);
451 if (cpuid >= NR_CPUS) {
452 printk(KERN_WARNING "Ignoring CPU %d which is "
458 ret = func(dp, cpuid, arg);
465 static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
472 struct device_node *of_find_node_by_cpuid(int cpuid)
474 return of_iterate_over_cpus(check_cpu_node, cpuid);
477 static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
481 set_cpu_present(cpuid, true);
482 set_cpu_possible(cpuid, true);
487 void __init of_populate_present_mask(void)
489 if (tlb_type == hypervisor)
493 of_iterate_over_cpus(record_one_cpu, 0);
496 static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
498 struct device_node *portid_parent = NULL;
501 if (of_find_property(dp, "cpuid", NULL)) {
506 portid_parent = portid_parent->parent;
509 portid = of_getintprop_default(portid_parent,
517 /* On uniprocessor we only want the values for the
518 * real physical cpu the kernel booted onto, however
519 * cpu_data() only has one entry at index 0.
521 if (cpuid != real_hard_smp_processor_id())
526 cpu_data(cpuid).clock_tick =
527 of_getintprop_default(dp, "clock-frequency", 0);
530 cpu_data(cpuid).dcache_size =
531 of_getintprop_default(dp, "l1-dcache-size",
533 cpu_data(cpuid).dcache_line_size =
534 of_getintprop_default(dp, "l1-dcache-line-size",
536 cpu_data(cpuid).icache_size =
537 of_getintprop_default(dp, "l1-icache-size",
539 cpu_data(cpuid).icache_line_size =
540 of_getintprop_default(dp, "l1-icache-line-size",
542 cpu_data(cpuid).ecache_size =
543 of_getintprop_default(dp, "l2-cache-size", 0);
544 cpu_data(cpuid).ecache_line_size =
545 of_getintprop_default(dp, "l2-cache-line-size", 0);
546 if (!cpu_data(cpuid).ecache_size ||
547 !cpu_data(cpuid).ecache_line_size) {
548 cpu_data(cpuid).ecache_size =
549 of_getintprop_default(portid_parent,
552 cpu_data(cpuid).ecache_line_size =
553 of_getintprop_default(portid_parent,
554 "l2-cache-line-size", 64);
557 cpu_data(cpuid).core_id = portid + 1;
558 cpu_data(cpuid).proc_id = portid;
560 cpu_data(cpuid).dcache_size =
561 of_getintprop_default(dp, "dcache-size", 16 * 1024);
562 cpu_data(cpuid).dcache_line_size =
563 of_getintprop_default(dp, "dcache-line-size", 32);
565 cpu_data(cpuid).icache_size =
566 of_getintprop_default(dp, "icache-size", 16 * 1024);
567 cpu_data(cpuid).icache_line_size =
568 of_getintprop_default(dp, "icache-line-size", 32);
570 cpu_data(cpuid).ecache_size =
571 of_getintprop_default(dp, "ecache-size",
573 cpu_data(cpuid).ecache_line_size =
574 of_getintprop_default(dp, "ecache-line-size", 64);
576 cpu_data(cpuid).core_id = 0;
577 cpu_data(cpuid).proc_id = -1;
583 void __init of_fill_in_cpu_data(void)
585 if (tlb_type == hypervisor)
588 of_iterate_over_cpus(fill_in_one_cpu, 0);
590 smp_fill_in_sib_core_maps();
593 void __init of_console_init(void)
595 char *msg = "OF stdout device is: %s\n";
596 struct device_node *dp;
600 of_console_path = prom_early_alloc(256);
601 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
602 prom_printf("Cannot obtain path of stdout.\n");
605 of_console_options = strrchr(of_console_path, ':');
606 if (of_console_options) {
607 of_console_options++;
608 if (*of_console_options == '\0')
609 of_console_options = NULL;
612 node = prom_inst2pkg(prom_stdout);
614 prom_printf("Cannot resolve stdout node from "
615 "instance %08x.\n", prom_stdout);
619 dp = of_find_node_by_phandle(node);
620 type = of_get_property(dp, "device_type", NULL);
622 prom_printf("Console stdout lacks device_type property.\n");
626 if (strcmp(type, "display") && strcmp(type, "serial")) {
627 prom_printf("Console device_type is neither display "
632 of_console_device = dp;
634 printk(msg, of_console_path);