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_phys_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 const char *name = of_get_property(dp, "name", NULL);
76 struct linux_prom64_registers *regs;
77 struct property *rprop;
78 u32 high_bits, low_bits, type;
80 rprop = of_find_property(dp, "reg", NULL);
85 if (!of_node_is_root(dp->parent)) {
86 sprintf(tmp_buf, "%s@%x,%x",
88 (unsigned int) (regs->phys_addr >> 32UL),
89 (unsigned int) (regs->phys_addr & 0xffffffffUL));
93 type = regs->phys_addr >> 60UL;
94 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
95 low_bits = (regs->phys_addr & 0xffffffffUL);
97 if (type == 0 || type == 8) {
98 const char *prefix = (type == 0) ? "m" : "i";
101 sprintf(tmp_buf, "%s@%s%x,%x",
103 high_bits, low_bits);
105 sprintf(tmp_buf, "%s@%s%x",
109 } else if (type == 12) {
110 sprintf(tmp_buf, "%s@%x",
115 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
117 const char *name = of_get_property(dp, "name", NULL);
118 struct linux_prom64_registers *regs;
119 struct property *prop;
121 prop = of_find_property(dp, "reg", NULL);
126 if (!of_node_is_root(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 const char *name = of_get_property(dp, "name", NULL);
154 struct linux_prom_registers *regs;
155 struct property *prop;
157 prop = of_find_property(dp, "reg", NULL);
162 sprintf(tmp_buf, "%s@%x,%x",
168 /* "name@devnum[,func]" */
169 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
171 const char *name = of_get_property(dp, "name", NULL);
172 struct linux_prom_pci_registers *regs;
173 struct property *prop;
176 prop = of_find_property(dp, "reg", NULL);
181 devfn = (regs->phys_hi >> 8) & 0xff;
183 sprintf(tmp_buf, "%s@%x,%x",
188 sprintf(tmp_buf, "%s@%x",
194 /* "name@UPA_PORTID,offset" */
195 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
197 const char *name = of_get_property(dp, "name", NULL);
198 struct linux_prom64_registers *regs;
199 struct property *prop;
201 prop = of_find_property(dp, "reg", NULL);
207 prop = of_find_property(dp, "upa-portid", NULL);
211 sprintf(tmp_buf, "%s@%x,%x",
213 *(u32 *) prop->value,
214 (unsigned int) (regs->phys_addr & 0xffffffffUL));
218 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
220 const char *name = of_get_property(dp, "name", NULL);
221 struct property *prop;
224 prop = of_find_property(dp, "reg", NULL);
230 sprintf(tmp_buf, "%s@%x", name, *regs);
233 /* "name@addrhi,addrlo" */
234 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
236 const char *name = of_get_property(dp, "name", NULL);
237 struct linux_prom64_registers *regs;
238 struct property *prop;
240 prop = of_find_property(dp, "reg", NULL);
246 sprintf(tmp_buf, "%s@%x,%x",
248 (unsigned int) (regs->phys_addr >> 32UL),
249 (unsigned int) (regs->phys_addr & 0xffffffffUL));
252 /* "name@bus,addr" */
253 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
255 const char *name = of_get_property(dp, "name", NULL);
256 struct property *prop;
259 prop = of_find_property(dp, "reg", NULL);
265 /* This actually isn't right... should look at the #address-cells
266 * property of the i2c bus node etc. etc.
268 sprintf(tmp_buf, "%s@%x,%x",
269 name, regs[0], regs[1]);
272 /* "name@reg0[,reg1]" */
273 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
275 const char *name = of_get_property(dp, "name", NULL);
276 struct property *prop;
279 prop = of_find_property(dp, "reg", NULL);
285 if (prop->length == sizeof(u32) || regs[1] == 1) {
286 sprintf(tmp_buf, "%s@%x",
289 sprintf(tmp_buf, "%s@%x,%x",
290 name, regs[0], regs[1]);
294 /* "name@reg0reg1[,reg2reg3]" */
295 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
297 const char *name = of_get_property(dp, "name", NULL);
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 name, regs[0], regs[1], regs[2], regs[3]);
311 sprintf(tmp_buf, "%s@%08x%08x",
312 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 (of_node_name_eq(parent, "usb") ||
339 of_node_name_eq(parent, "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 char * __init build_path_component(struct device_node *dp)
369 const char *name = of_get_property(dp, "name", NULL);
370 char tmp_buf[64], *n;
373 __build_path_component(dp, tmp_buf);
374 if (tmp_buf[0] == '\0')
375 strcpy(tmp_buf, name);
377 n = prom_early_alloc(strlen(tmp_buf) + 1);
383 static const char *get_mid_prop(void)
385 return (tlb_type == spitfire ? "upa-portid" : "portid");
388 bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
389 int cpu, unsigned int *thread)
391 const char *mid_prop = get_mid_prop();
394 /* On hypervisor based platforms we interrogate the 'reg'
395 * property. On everything else we look for a 'upa-portid',
396 * 'portid', or 'cpuid' property.
399 if (tlb_type == hypervisor) {
400 struct property *prop = of_find_property(cpun, "reg", NULL);
404 pr_warn("CPU node missing reg property\n");
408 this_cpu_id = regs[0] & 0x0fffffff;
410 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
412 if (this_cpu_id < 0) {
414 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
416 if (this_cpu_id < 0) {
417 pr_warn("CPU node missing cpu ID property\n");
421 if (this_cpu_id == cpu) {
423 int proc_id = cpu_data(cpu).proc_id;
425 /* On sparc64, the cpu thread information is obtained
426 * either from OBP or the machine description. We've
427 * actually probed this information already long before
428 * this interface gets called so instead of interrogating
429 * both the OF node and the MDESC again, just use what
430 * we discovered already.
441 static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
443 struct device_node *dp;
444 const char *mid_prop;
446 mid_prop = get_mid_prop();
447 for_each_node_by_type(dp, "cpu") {
448 int cpuid = of_getintprop_default(dp, mid_prop, -1);
449 const char *this_mid_prop = mid_prop;
453 this_mid_prop = "cpuid";
454 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
457 prom_printf("OF: Serious problem, cpu lacks "
458 "%s property", this_mid_prop);
462 if (cpuid >= NR_CPUS) {
463 printk(KERN_WARNING "Ignoring CPU %d which is "
469 ret = func(dp, cpuid, arg);
476 static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
483 struct device_node *of_find_node_by_cpuid(int cpuid)
485 return of_iterate_over_cpus(check_cpu_node, cpuid);
488 static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
492 set_cpu_present(cpuid, true);
493 set_cpu_possible(cpuid, true);
498 void __init of_populate_present_mask(void)
500 if (tlb_type == hypervisor)
504 of_iterate_over_cpus(record_one_cpu, 0);
507 static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
509 struct device_node *portid_parent = NULL;
512 if (of_find_property(dp, "cpuid", NULL)) {
517 portid_parent = portid_parent->parent;
520 portid = of_getintprop_default(portid_parent,
528 /* On uniprocessor we only want the values for the
529 * real physical cpu the kernel booted onto, however
530 * cpu_data() only has one entry at index 0.
532 if (cpuid != real_hard_smp_processor_id())
537 cpu_data(cpuid).clock_tick =
538 of_getintprop_default(dp, "clock-frequency", 0);
541 cpu_data(cpuid).dcache_size =
542 of_getintprop_default(dp, "l1-dcache-size",
544 cpu_data(cpuid).dcache_line_size =
545 of_getintprop_default(dp, "l1-dcache-line-size",
547 cpu_data(cpuid).icache_size =
548 of_getintprop_default(dp, "l1-icache-size",
550 cpu_data(cpuid).icache_line_size =
551 of_getintprop_default(dp, "l1-icache-line-size",
553 cpu_data(cpuid).ecache_size =
554 of_getintprop_default(dp, "l2-cache-size", 0);
555 cpu_data(cpuid).ecache_line_size =
556 of_getintprop_default(dp, "l2-cache-line-size", 0);
557 if (!cpu_data(cpuid).ecache_size ||
558 !cpu_data(cpuid).ecache_line_size) {
559 cpu_data(cpuid).ecache_size =
560 of_getintprop_default(portid_parent,
563 cpu_data(cpuid).ecache_line_size =
564 of_getintprop_default(portid_parent,
565 "l2-cache-line-size", 64);
568 cpu_data(cpuid).core_id = portid + 1;
569 cpu_data(cpuid).proc_id = portid;
571 cpu_data(cpuid).dcache_size =
572 of_getintprop_default(dp, "dcache-size", 16 * 1024);
573 cpu_data(cpuid).dcache_line_size =
574 of_getintprop_default(dp, "dcache-line-size", 32);
576 cpu_data(cpuid).icache_size =
577 of_getintprop_default(dp, "icache-size", 16 * 1024);
578 cpu_data(cpuid).icache_line_size =
579 of_getintprop_default(dp, "icache-line-size", 32);
581 cpu_data(cpuid).ecache_size =
582 of_getintprop_default(dp, "ecache-size",
584 cpu_data(cpuid).ecache_line_size =
585 of_getintprop_default(dp, "ecache-line-size", 64);
587 cpu_data(cpuid).core_id = 0;
588 cpu_data(cpuid).proc_id = -1;
594 void __init of_fill_in_cpu_data(void)
596 if (tlb_type == hypervisor)
599 of_iterate_over_cpus(fill_in_one_cpu, 0);
601 smp_fill_in_sib_core_maps();
604 void __init of_console_init(void)
606 char *msg = "OF stdout device is: %s\n";
607 struct device_node *dp;
611 of_console_path = prom_early_alloc(256);
612 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
613 prom_printf("Cannot obtain path of stdout.\n");
616 of_console_options = strrchr(of_console_path, ':');
617 if (of_console_options) {
618 of_console_options++;
619 if (*of_console_options == '\0')
620 of_console_options = NULL;
623 node = prom_inst2pkg(prom_stdout);
625 prom_printf("Cannot resolve stdout node from "
626 "instance %08x.\n", prom_stdout);
630 dp = of_find_node_by_phandle(node);
631 type = of_get_property(dp, "device_type", NULL);
633 prom_printf("Console stdout lacks device_type property.\n");
637 if (strcmp(type, "display") && strcmp(type, "serial")) {
638 prom_printf("Console device_type is neither display "
643 of_console_device = dp;
645 printk(msg, of_console_path);