1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Procedures for creating, accessing and interpreting the device tree.
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
11 * Adapted for sparc64 by David S. Miller davem@davemloft.net
14 #include <linux/memblock.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/cpu.h>
23 #include <asm/oplib.h>
31 void * __init
prom_early_alloc(unsigned long size
)
33 void *ret
= memblock_alloc(size
, SMP_CACHE_BYTES
);
36 prom_printf("prom_early_alloc(%lu) failed\n", size
);
40 prom_early_allocated
+= size
;
45 /* The following routines deal with the black magic of fully naming a
48 * Certain well known named nodes are just the simple name string.
50 * Actual devices have an address specifier appended to the base name
51 * string, like this "foo@addr". The "addr" can be in any number of
52 * formats, and the platform plus the type of the node determine the
53 * format and how it is constructed.
55 * For children of the ROOT node, the naming convention is fixed and
56 * determined by whether this is a sun4u or sun4v system.
58 * For children of other nodes, it is bus type specific. So
59 * we walk up the tree until we discover a "device_type" property
60 * we recognize and we go from there.
62 * As an example, the boot device on my workstation has a full path:
64 * /pci@1e,600000/ide@d/disk@0,0:c
66 static void __init
sun4v_path_component(struct device_node
*dp
, char *tmp_buf
)
68 const char *name
= of_get_property(dp
, "name", NULL
);
69 struct linux_prom64_registers
*regs
;
70 struct property
*rprop
;
71 u32 high_bits
, low_bits
, type
;
73 rprop
= of_find_property(dp
, "reg", NULL
);
78 if (!of_node_is_root(dp
->parent
)) {
79 sprintf(tmp_buf
, "%s@%x,%x",
81 (unsigned int) (regs
->phys_addr
>> 32UL),
82 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
86 type
= regs
->phys_addr
>> 60UL;
87 high_bits
= (regs
->phys_addr
>> 32UL) & 0x0fffffffUL
;
88 low_bits
= (regs
->phys_addr
& 0xffffffffUL
);
90 if (type
== 0 || type
== 8) {
91 const char *prefix
= (type
== 0) ? "m" : "i";
94 sprintf(tmp_buf
, "%s@%s%x,%x",
98 sprintf(tmp_buf
, "%s@%s%x",
102 } else if (type
== 12) {
103 sprintf(tmp_buf
, "%s@%x",
108 static void __init
sun4u_path_component(struct device_node
*dp
, char *tmp_buf
)
110 const char *name
= of_get_property(dp
, "name", NULL
);
111 struct linux_prom64_registers
*regs
;
112 struct property
*prop
;
114 prop
= of_find_property(dp
, "reg", NULL
);
119 if (!of_node_is_root(dp
->parent
)) {
120 sprintf(tmp_buf
, "%s@%x,%x",
122 (unsigned int) (regs
->phys_addr
>> 32UL),
123 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
127 prop
= of_find_property(dp
, "upa-portid", NULL
);
129 prop
= of_find_property(dp
, "portid", NULL
);
131 unsigned long mask
= 0xffffffffUL
;
133 if (tlb_type
>= cheetah
)
136 sprintf(tmp_buf
, "%s@%x,%x",
139 (unsigned int) (regs
->phys_addr
& mask
));
143 /* "name@slot,offset" */
144 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
146 const char *name
= of_get_property(dp
, "name", NULL
);
147 struct linux_prom_registers
*regs
;
148 struct property
*prop
;
150 prop
= of_find_property(dp
, "reg", NULL
);
155 sprintf(tmp_buf
, "%s@%x,%x",
161 /* "name@devnum[,func]" */
162 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
164 const char *name
= of_get_property(dp
, "name", NULL
);
165 struct linux_prom_pci_registers
*regs
;
166 struct property
*prop
;
169 prop
= of_find_property(dp
, "reg", NULL
);
174 devfn
= (regs
->phys_hi
>> 8) & 0xff;
176 sprintf(tmp_buf
, "%s@%x,%x",
181 sprintf(tmp_buf
, "%s@%x",
187 /* "name@UPA_PORTID,offset" */
188 static void __init
upa_path_component(struct device_node
*dp
, char *tmp_buf
)
190 const char *name
= of_get_property(dp
, "name", NULL
);
191 struct linux_prom64_registers
*regs
;
192 struct property
*prop
;
194 prop
= of_find_property(dp
, "reg", NULL
);
200 prop
= of_find_property(dp
, "upa-portid", NULL
);
204 sprintf(tmp_buf
, "%s@%x,%x",
206 *(u32
*) prop
->value
,
207 (unsigned int) (regs
->phys_addr
& 0xffffffffUL
));
211 static void __init
vdev_path_component(struct device_node
*dp
, char *tmp_buf
)
213 const char *name
= of_get_property(dp
, "name", NULL
);
214 struct property
*prop
;
217 prop
= of_find_property(dp
, "reg", NULL
);
223 sprintf(tmp_buf
, "%s@%x", name
, *regs
);
226 /* "name@addrhi,addrlo" */
227 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
229 const char *name
= of_get_property(dp
, "name", NULL
);
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 const char *name
= of_get_property(dp
, "name", NULL
);
249 struct property
*prop
;
252 prop
= of_find_property(dp
, "reg", NULL
);
258 /* This actually isn't right... should look at the #address-cells
259 * property of the i2c bus node etc. etc.
261 sprintf(tmp_buf
, "%s@%x,%x",
262 name
, regs
[0], regs
[1]);
265 /* "name@reg0[,reg1]" */
266 static void __init
usb_path_component(struct device_node
*dp
, char *tmp_buf
)
268 const char *name
= of_get_property(dp
, "name", NULL
);
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 name
, regs
[0], regs
[1]);
287 /* "name@reg0reg1[,reg2reg3]" */
288 static void __init
ieee1394_path_component(struct device_node
*dp
, char *tmp_buf
)
290 const char *name
= of_get_property(dp
, "name", NULL
);
291 struct property
*prop
;
294 prop
= of_find_property(dp
, "reg", NULL
);
300 if (regs
[2] || regs
[3]) {
301 sprintf(tmp_buf
, "%s@%08x%08x,%04x%08x",
302 name
, regs
[0], regs
[1], regs
[2], regs
[3]);
304 sprintf(tmp_buf
, "%s@%08x%08x",
305 name
, regs
[0], regs
[1]);
309 static void __init
__build_path_component(struct device_node
*dp
, char *tmp_buf
)
311 struct device_node
*parent
= dp
->parent
;
313 if (parent
!= NULL
) {
314 if (of_node_is_type(parent
, "pci") ||
315 of_node_is_type(parent
, "pciex")) {
316 pci_path_component(dp
, tmp_buf
);
319 if (of_node_is_type(parent
, "sbus")) {
320 sbus_path_component(dp
, tmp_buf
);
323 if (of_node_is_type(parent
, "upa")) {
324 upa_path_component(dp
, tmp_buf
);
327 if (of_node_is_type(parent
, "ebus")) {
328 ebus_path_component(dp
, tmp_buf
);
331 if (of_node_name_eq(parent
, "usb") ||
332 of_node_name_eq(parent
, "hub")) {
333 usb_path_component(dp
, tmp_buf
);
336 if (of_node_is_type(parent
, "i2c")) {
337 i2c_path_component(dp
, tmp_buf
);
340 if (of_node_is_type(parent
, "firewire")) {
341 ieee1394_path_component(dp
, tmp_buf
);
344 if (of_node_is_type(parent
, "virtual-devices")) {
345 vdev_path_component(dp
, tmp_buf
);
348 /* "isa" is handled with platform naming */
351 /* Use platform naming convention. */
352 if (tlb_type
== hypervisor
) {
353 sun4v_path_component(dp
, tmp_buf
);
356 sun4u_path_component(dp
, tmp_buf
);
360 char * __init
build_path_component(struct device_node
*dp
)
362 const char *name
= of_get_property(dp
, "name", NULL
);
363 char tmp_buf
[64], *n
;
366 __build_path_component(dp
, tmp_buf
);
367 if (tmp_buf
[0] == '\0')
368 strcpy(tmp_buf
, name
);
370 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
376 static const char *get_mid_prop(void)
378 return (tlb_type
== spitfire
? "upa-portid" : "portid");
381 bool arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
382 int cpu
, unsigned int *thread
)
384 const char *mid_prop
= get_mid_prop();
387 /* On hypervisor based platforms we interrogate the 'reg'
388 * property. On everything else we look for a 'upa-portid',
389 * 'portid', or 'cpuid' property.
392 if (tlb_type
== hypervisor
) {
393 struct property
*prop
= of_find_property(cpun
, "reg", NULL
);
397 pr_warn("CPU node missing reg property\n");
401 this_cpu_id
= regs
[0] & 0x0fffffff;
403 this_cpu_id
= of_getintprop_default(cpun
, mid_prop
, -1);
405 if (this_cpu_id
< 0) {
407 this_cpu_id
= of_getintprop_default(cpun
, mid_prop
, -1);
409 if (this_cpu_id
< 0) {
410 pr_warn("CPU node missing cpu ID property\n");
414 if (this_cpu_id
== cpu
) {
416 int proc_id
= cpu_data(cpu
).proc_id
;
418 /* On sparc64, the cpu thread information is obtained
419 * either from OBP or the machine description. We've
420 * actually probed this information already long before
421 * this interface gets called so instead of interrogating
422 * both the OF node and the MDESC again, just use what
423 * we discovered already.
434 static void *of_iterate_over_cpus(void *(*func
)(struct device_node
*, int, int), int arg
)
436 struct device_node
*dp
;
437 const char *mid_prop
;
439 mid_prop
= get_mid_prop();
440 for_each_node_by_type(dp
, "cpu") {
441 int cpuid
= of_getintprop_default(dp
, mid_prop
, -1);
442 const char *this_mid_prop
= mid_prop
;
446 this_mid_prop
= "cpuid";
447 cpuid
= of_getintprop_default(dp
, this_mid_prop
, -1);
450 prom_printf("OF: Serious problem, cpu lacks "
451 "%s property", this_mid_prop
);
455 if (cpuid
>= NR_CPUS
) {
456 printk(KERN_WARNING
"Ignoring CPU %d which is "
462 ret
= func(dp
, cpuid
, arg
);
469 static void *check_cpu_node(struct device_node
*dp
, int cpuid
, int id
)
476 struct device_node
*of_find_node_by_cpuid(int cpuid
)
478 return of_iterate_over_cpus(check_cpu_node
, cpuid
);
481 static void *record_one_cpu(struct device_node
*dp
, int cpuid
, int arg
)
485 set_cpu_present(cpuid
, true);
487 if (num_possible_cpus() < nr_cpu_ids
)
488 set_cpu_possible(cpuid
, true);
493 void __init
of_populate_present_mask(void)
495 if (tlb_type
== hypervisor
)
499 of_iterate_over_cpus(record_one_cpu
, 0);
502 static void *fill_in_one_cpu(struct device_node
*dp
, int cpuid
, int arg
)
504 struct device_node
*portid_parent
= NULL
;
507 if (of_property_present(dp
, "cpuid")) {
512 portid_parent
= portid_parent
->parent
;
515 portid
= of_getintprop_default(portid_parent
,
523 /* On uniprocessor we only want the values for the
524 * real physical cpu the kernel booted onto, however
525 * cpu_data() only has one entry at index 0.
527 if (cpuid
!= real_hard_smp_processor_id())
532 cpu_data(cpuid
).clock_tick
=
533 of_getintprop_default(dp
, "clock-frequency", 0);
536 cpu_data(cpuid
).dcache_size
=
537 of_getintprop_default(dp
, "l1-dcache-size",
539 cpu_data(cpuid
).dcache_line_size
=
540 of_getintprop_default(dp
, "l1-dcache-line-size",
542 cpu_data(cpuid
).icache_size
=
543 of_getintprop_default(dp
, "l1-icache-size",
545 cpu_data(cpuid
).icache_line_size
=
546 of_getintprop_default(dp
, "l1-icache-line-size",
548 cpu_data(cpuid
).ecache_size
=
549 of_getintprop_default(dp
, "l2-cache-size", 0);
550 cpu_data(cpuid
).ecache_line_size
=
551 of_getintprop_default(dp
, "l2-cache-line-size", 0);
552 if (!cpu_data(cpuid
).ecache_size
||
553 !cpu_data(cpuid
).ecache_line_size
) {
554 cpu_data(cpuid
).ecache_size
=
555 of_getintprop_default(portid_parent
,
558 cpu_data(cpuid
).ecache_line_size
=
559 of_getintprop_default(portid_parent
,
560 "l2-cache-line-size", 64);
563 cpu_data(cpuid
).core_id
= portid
+ 1;
564 cpu_data(cpuid
).proc_id
= portid
;
566 cpu_data(cpuid
).dcache_size
=
567 of_getintprop_default(dp
, "dcache-size", 16 * 1024);
568 cpu_data(cpuid
).dcache_line_size
=
569 of_getintprop_default(dp
, "dcache-line-size", 32);
571 cpu_data(cpuid
).icache_size
=
572 of_getintprop_default(dp
, "icache-size", 16 * 1024);
573 cpu_data(cpuid
).icache_line_size
=
574 of_getintprop_default(dp
, "icache-line-size", 32);
576 cpu_data(cpuid
).ecache_size
=
577 of_getintprop_default(dp
, "ecache-size",
579 cpu_data(cpuid
).ecache_line_size
=
580 of_getintprop_default(dp
, "ecache-line-size", 64);
582 cpu_data(cpuid
).core_id
= 0;
583 cpu_data(cpuid
).proc_id
= -1;
589 void __init
of_fill_in_cpu_data(void)
591 if (tlb_type
== hypervisor
)
594 of_iterate_over_cpus(fill_in_one_cpu
, 0);
596 smp_fill_in_sib_core_maps();
599 void __init
of_console_init(void)
601 char *msg
= "OF stdout device is: %s\n";
602 struct device_node
*dp
;
605 of_console_path
= prom_early_alloc(256);
606 if (prom_ihandle2path(prom_stdout
, of_console_path
, 256) < 0) {
607 prom_printf("Cannot obtain path of stdout.\n");
610 of_console_options
= strrchr(of_console_path
, ':');
611 if (of_console_options
) {
612 of_console_options
++;
613 if (*of_console_options
== '\0')
614 of_console_options
= NULL
;
617 node
= prom_inst2pkg(prom_stdout
);
619 prom_printf("Cannot resolve stdout node from "
620 "instance %08x.\n", prom_stdout
);
624 dp
= of_find_node_by_phandle(node
);
626 if (!of_node_is_type(dp
, "display") && !of_node_is_type(dp
, "serial")) {
627 prom_printf("Console device_type is neither display "
632 of_console_device
= dp
;
634 printk(msg
, of_console_path
);