1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Procedures for creating, accessing and interpreting the device tree.
4 *
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
7 *
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
10 *
11 * Adapted for sparc64 by David S. Miller davem@davemloft.net
12 */
13
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>
19 #include <linux/mm.h>
20 #include <linux/of.h>
21
22 #include <asm/prom.h>
23 #include <asm/oplib.h>
24 #include <asm/irq.h>
25 #include <asm/asi.h>
26 #include <asm/upa.h>
27 #include <asm/smp.h>
28
29 #include "prom.h"
30
prom_early_alloc(unsigned long size)31 void * __init prom_early_alloc(unsigned long size)
32 {
33 void *ret = memblock_alloc(size, SMP_CACHE_BYTES);
34
35 if (!ret) {
36 prom_printf("prom_early_alloc(%lu) failed\n", size);
37 prom_halt();
38 }
39
40 prom_early_allocated += size;
41
42 return ret;
43 }
44
45 /* The following routines deal with the black magic of fully naming a
46 * node.
47 *
48 * Certain well known named nodes are just the simple name string.
49 *
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.
54 *
55 * For children of the ROOT node, the naming convention is fixed and
56 * determined by whether this is a sun4u or sun4v system.
57 *
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.
61 *
62 * As an example, the boot device on my workstation has a full path:
63 *
64 * /pci@1e,600000/ide@d/disk@0,0:c
65 */
sun4v_path_component(struct device_node * dp,char * tmp_buf)66 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
67 {
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;
72
73 rprop = of_find_property(dp, "reg", NULL);
74 if (!rprop)
75 return;
76
77 regs = rprop->value;
78 if (!of_node_is_root(dp->parent)) {
79 sprintf(tmp_buf, "%s@%x,%x",
80 name,
81 (unsigned int) (regs->phys_addr >> 32UL),
82 (unsigned int) (regs->phys_addr & 0xffffffffUL));
83 return;
84 }
85
86 type = regs->phys_addr >> 60UL;
87 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
88 low_bits = (regs->phys_addr & 0xffffffffUL);
89
90 if (type == 0 || type == 8) {
91 const char *prefix = (type == 0) ? "m" : "i";
92
93 if (low_bits)
94 sprintf(tmp_buf, "%s@%s%x,%x",
95 name, prefix,
96 high_bits, low_bits);
97 else
98 sprintf(tmp_buf, "%s@%s%x",
99 name,
100 prefix,
101 high_bits);
102 } else if (type == 12) {
103 sprintf(tmp_buf, "%s@%x",
104 name, high_bits);
105 }
106 }
107
sun4u_path_component(struct device_node * dp,char * tmp_buf)108 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
109 {
110 const char *name = of_get_property(dp, "name", NULL);
111 struct linux_prom64_registers *regs;
112 struct property *prop;
113
114 prop = of_find_property(dp, "reg", NULL);
115 if (!prop)
116 return;
117
118 regs = prop->value;
119 if (!of_node_is_root(dp->parent)) {
120 sprintf(tmp_buf, "%s@%x,%x",
121 name,
122 (unsigned int) (regs->phys_addr >> 32UL),
123 (unsigned int) (regs->phys_addr & 0xffffffffUL));
124 return;
125 }
126
127 prop = of_find_property(dp, "upa-portid", NULL);
128 if (!prop)
129 prop = of_find_property(dp, "portid", NULL);
130 if (prop) {
131 unsigned long mask = 0xffffffffUL;
132
133 if (tlb_type >= cheetah)
134 mask = 0x7fffff;
135
136 sprintf(tmp_buf, "%s@%x,%x",
137 name,
138 *(u32 *)prop->value,
139 (unsigned int) (regs->phys_addr & mask));
140 }
141 }
142
143 /* "name@slot,offset" */
sbus_path_component(struct device_node * dp,char * tmp_buf)144 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
145 {
146 const char *name = of_get_property(dp, "name", NULL);
147 struct linux_prom_registers *regs;
148 struct property *prop;
149
150 prop = of_find_property(dp, "reg", NULL);
151 if (!prop)
152 return;
153
154 regs = prop->value;
155 sprintf(tmp_buf, "%s@%x,%x",
156 name,
157 regs->which_io,
158 regs->phys_addr);
159 }
160
161 /* "name@devnum[,func]" */
pci_path_component(struct device_node * dp,char * tmp_buf)162 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
163 {
164 const char *name = of_get_property(dp, "name", NULL);
165 struct linux_prom_pci_registers *regs;
166 struct property *prop;
167 unsigned int devfn;
168
169 prop = of_find_property(dp, "reg", NULL);
170 if (!prop)
171 return;
172
173 regs = prop->value;
174 devfn = (regs->phys_hi >> 8) & 0xff;
175 if (devfn & 0x07) {
176 sprintf(tmp_buf, "%s@%x,%x",
177 name,
178 devfn >> 3,
179 devfn & 0x07);
180 } else {
181 sprintf(tmp_buf, "%s@%x",
182 name,
183 devfn >> 3);
184 }
185 }
186
187 /* "name@UPA_PORTID,offset" */
upa_path_component(struct device_node * dp,char * tmp_buf)188 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
189 {
190 const char *name = of_get_property(dp, "name", NULL);
191 struct linux_prom64_registers *regs;
192 struct property *prop;
193
194 prop = of_find_property(dp, "reg", NULL);
195 if (!prop)
196 return;
197
198 regs = prop->value;
199
200 prop = of_find_property(dp, "upa-portid", NULL);
201 if (!prop)
202 return;
203
204 sprintf(tmp_buf, "%s@%x,%x",
205 name,
206 *(u32 *) prop->value,
207 (unsigned int) (regs->phys_addr & 0xffffffffUL));
208 }
209
210 /* "name@reg" */
vdev_path_component(struct device_node * dp,char * tmp_buf)211 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
212 {
213 const char *name = of_get_property(dp, "name", NULL);
214 struct property *prop;
215 u32 *regs;
216
217 prop = of_find_property(dp, "reg", NULL);
218 if (!prop)
219 return;
220
221 regs = prop->value;
222
223 sprintf(tmp_buf, "%s@%x", name, *regs);
224 }
225
226 /* "name@addrhi,addrlo" */
ebus_path_component(struct device_node * dp,char * tmp_buf)227 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
228 {
229 const char *name = of_get_property(dp, "name", NULL);
230 struct linux_prom64_registers *regs;
231 struct property *prop;
232
233 prop = of_find_property(dp, "reg", NULL);
234 if (!prop)
235 return;
236
237 regs = prop->value;
238
239 sprintf(tmp_buf, "%s@%x,%x",
240 name,
241 (unsigned int) (regs->phys_addr >> 32UL),
242 (unsigned int) (regs->phys_addr & 0xffffffffUL));
243 }
244
245 /* "name@bus,addr" */
i2c_path_component(struct device_node * dp,char * tmp_buf)246 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
247 {
248 const char *name = of_get_property(dp, "name", NULL);
249 struct property *prop;
250 u32 *regs;
251
252 prop = of_find_property(dp, "reg", NULL);
253 if (!prop)
254 return;
255
256 regs = prop->value;
257
258 /* This actually isn't right... should look at the #address-cells
259 * property of the i2c bus node etc. etc.
260 */
261 sprintf(tmp_buf, "%s@%x,%x",
262 name, regs[0], regs[1]);
263 }
264
265 /* "name@reg0[,reg1]" */
usb_path_component(struct device_node * dp,char * tmp_buf)266 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
267 {
268 const char *name = of_get_property(dp, "name", NULL);
269 struct property *prop;
270 u32 *regs;
271
272 prop = of_find_property(dp, "reg", NULL);
273 if (!prop)
274 return;
275
276 regs = prop->value;
277
278 if (prop->length == sizeof(u32) || regs[1] == 1) {
279 sprintf(tmp_buf, "%s@%x",
280 name, regs[0]);
281 } else {
282 sprintf(tmp_buf, "%s@%x,%x",
283 name, regs[0], regs[1]);
284 }
285 }
286
287 /* "name@reg0reg1[,reg2reg3]" */
ieee1394_path_component(struct device_node * dp,char * tmp_buf)288 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
289 {
290 const char *name = of_get_property(dp, "name", NULL);
291 struct property *prop;
292 u32 *regs;
293
294 prop = of_find_property(dp, "reg", NULL);
295 if (!prop)
296 return;
297
298 regs = prop->value;
299
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]);
303 } else {
304 sprintf(tmp_buf, "%s@%08x%08x",
305 name, regs[0], regs[1]);
306 }
307 }
308
__build_path_component(struct device_node * dp,char * tmp_buf)309 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
310 {
311 struct device_node *parent = dp->parent;
312
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);
317 return;
318 }
319 if (of_node_is_type(parent, "sbus")) {
320 sbus_path_component(dp, tmp_buf);
321 return;
322 }
323 if (of_node_is_type(parent, "upa")) {
324 upa_path_component(dp, tmp_buf);
325 return;
326 }
327 if (of_node_is_type(parent, "ebus")) {
328 ebus_path_component(dp, tmp_buf);
329 return;
330 }
331 if (of_node_name_eq(parent, "usb") ||
332 of_node_name_eq(parent, "hub")) {
333 usb_path_component(dp, tmp_buf);
334 return;
335 }
336 if (of_node_is_type(parent, "i2c")) {
337 i2c_path_component(dp, tmp_buf);
338 return;
339 }
340 if (of_node_is_type(parent, "firewire")) {
341 ieee1394_path_component(dp, tmp_buf);
342 return;
343 }
344 if (of_node_is_type(parent, "virtual-devices")) {
345 vdev_path_component(dp, tmp_buf);
346 return;
347 }
348 /* "isa" is handled with platform naming */
349 }
350
351 /* Use platform naming convention. */
352 if (tlb_type == hypervisor) {
353 sun4v_path_component(dp, tmp_buf);
354 return;
355 } else {
356 sun4u_path_component(dp, tmp_buf);
357 }
358 }
359
build_path_component(struct device_node * dp)360 char * __init build_path_component(struct device_node *dp)
361 {
362 const char *name = of_get_property(dp, "name", NULL);
363 char tmp_buf[64], *n;
364 size_t n_sz;
365
366 tmp_buf[0] = '\0';
367 __build_path_component(dp, tmp_buf);
368 if (tmp_buf[0] == '\0')
369 strscpy(tmp_buf, name);
370
371 n_sz = strlen(tmp_buf) + 1;
372 n = prom_early_alloc(n_sz);
373 strscpy(n, tmp_buf, n_sz);
374
375 return n;
376 }
377
get_mid_prop(void)378 static const char *get_mid_prop(void)
379 {
380 return (tlb_type == spitfire ? "upa-portid" : "portid");
381 }
382
arch_find_n_match_cpu_physical_id(struct device_node * cpun,int cpu,unsigned int * thread)383 bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
384 int cpu, unsigned int *thread)
385 {
386 const char *mid_prop = get_mid_prop();
387 int this_cpu_id;
388
389 /* On hypervisor based platforms we interrogate the 'reg'
390 * property. On everything else we look for a 'upa-portid',
391 * 'portid', or 'cpuid' property.
392 */
393
394 if (tlb_type == hypervisor) {
395 struct property *prop = of_find_property(cpun, "reg", NULL);
396 u32 *regs;
397
398 if (!prop) {
399 pr_warn("CPU node missing reg property\n");
400 return false;
401 }
402 regs = prop->value;
403 this_cpu_id = regs[0] & 0x0fffffff;
404 } else {
405 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
406
407 if (this_cpu_id < 0) {
408 mid_prop = "cpuid";
409 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
410 }
411 if (this_cpu_id < 0) {
412 pr_warn("CPU node missing cpu ID property\n");
413 return false;
414 }
415 }
416 if (this_cpu_id == cpu) {
417 if (thread) {
418 int proc_id = cpu_data(cpu).proc_id;
419
420 /* On sparc64, the cpu thread information is obtained
421 * either from OBP or the machine description. We've
422 * actually probed this information already long before
423 * this interface gets called so instead of interrogating
424 * both the OF node and the MDESC again, just use what
425 * we discovered already.
426 */
427 if (proc_id < 0)
428 proc_id = 0;
429 *thread = proc_id;
430 }
431 return true;
432 }
433 return false;
434 }
435
of_iterate_over_cpus(void * (* func)(struct device_node *,int,int),int arg)436 static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
437 {
438 struct device_node *dp;
439 const char *mid_prop;
440
441 mid_prop = get_mid_prop();
442 for_each_node_by_type(dp, "cpu") {
443 int cpuid = of_getintprop_default(dp, mid_prop, -1);
444 const char *this_mid_prop = mid_prop;
445 void *ret;
446
447 if (cpuid < 0) {
448 this_mid_prop = "cpuid";
449 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
450 }
451 if (cpuid < 0) {
452 prom_printf("OF: Serious problem, cpu lacks "
453 "%s property", this_mid_prop);
454 prom_halt();
455 }
456 #ifdef CONFIG_SMP
457 if (cpuid >= NR_CPUS) {
458 printk(KERN_WARNING "Ignoring CPU %d which is "
459 ">= NR_CPUS (%d)\n",
460 cpuid, NR_CPUS);
461 continue;
462 }
463 #endif
464 ret = func(dp, cpuid, arg);
465 if (ret)
466 return ret;
467 }
468 return NULL;
469 }
470
check_cpu_node(struct device_node * dp,int cpuid,int id)471 static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
472 {
473 if (id == cpuid)
474 return dp;
475 return NULL;
476 }
477
of_find_node_by_cpuid(int cpuid)478 struct device_node *of_find_node_by_cpuid(int cpuid)
479 {
480 return of_iterate_over_cpus(check_cpu_node, cpuid);
481 }
482
record_one_cpu(struct device_node * dp,int cpuid,int arg)483 static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
484 {
485 ncpus_probed++;
486 #ifdef CONFIG_SMP
487 set_cpu_present(cpuid, true);
488
489 if (num_possible_cpus() < nr_cpu_ids)
490 set_cpu_possible(cpuid, true);
491 #endif
492 return NULL;
493 }
494
of_populate_present_mask(void)495 void __init of_populate_present_mask(void)
496 {
497 if (tlb_type == hypervisor)
498 return;
499
500 ncpus_probed = 0;
501 of_iterate_over_cpus(record_one_cpu, 0);
502 }
503
fill_in_one_cpu(struct device_node * dp,int cpuid,int arg)504 static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
505 {
506 struct device_node *portid_parent = NULL;
507 int portid = -1;
508
509 if (of_property_present(dp, "cpuid")) {
510 int limit = 2;
511
512 portid_parent = dp;
513 while (limit--) {
514 portid_parent = portid_parent->parent;
515 if (!portid_parent)
516 break;
517 portid = of_getintprop_default(portid_parent,
518 "portid", -1);
519 if (portid >= 0)
520 break;
521 }
522 }
523
524 #ifndef CONFIG_SMP
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.
528 */
529 if (cpuid != real_hard_smp_processor_id())
530 return NULL;
531 cpuid = 0;
532 #endif
533
534 cpu_data(cpuid).clock_tick =
535 of_getintprop_default(dp, "clock-frequency", 0);
536
537 if (portid_parent) {
538 cpu_data(cpuid).dcache_size =
539 of_getintprop_default(dp, "l1-dcache-size",
540 16 * 1024);
541 cpu_data(cpuid).dcache_line_size =
542 of_getintprop_default(dp, "l1-dcache-line-size",
543 32);
544 cpu_data(cpuid).icache_size =
545 of_getintprop_default(dp, "l1-icache-size",
546 8 * 1024);
547 cpu_data(cpuid).icache_line_size =
548 of_getintprop_default(dp, "l1-icache-line-size",
549 32);
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,
558 "l2-cache-size",
559 (4 * 1024 * 1024));
560 cpu_data(cpuid).ecache_line_size =
561 of_getintprop_default(portid_parent,
562 "l2-cache-line-size", 64);
563 }
564
565 cpu_data(cpuid).core_id = portid + 1;
566 cpu_data(cpuid).proc_id = portid;
567 } else {
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);
572
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);
577
578 cpu_data(cpuid).ecache_size =
579 of_getintprop_default(dp, "ecache-size",
580 (4 * 1024 * 1024));
581 cpu_data(cpuid).ecache_line_size =
582 of_getintprop_default(dp, "ecache-line-size", 64);
583
584 cpu_data(cpuid).core_id = 0;
585 cpu_data(cpuid).proc_id = -1;
586 }
587
588 return NULL;
589 }
590
of_fill_in_cpu_data(void)591 void __init of_fill_in_cpu_data(void)
592 {
593 if (tlb_type == hypervisor)
594 return;
595
596 of_iterate_over_cpus(fill_in_one_cpu, 0);
597
598 smp_fill_in_sib_core_maps();
599 }
600
of_console_init(void)601 void __init of_console_init(void)
602 {
603 char *msg = "OF stdout device is: %s\n";
604 struct device_node *dp;
605 phandle node;
606
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");
610 prom_halt();
611 }
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;
617 }
618
619 node = prom_inst2pkg(prom_stdout);
620 if (!node) {
621 prom_printf("Cannot resolve stdout node from "
622 "instance %08x.\n", prom_stdout);
623 prom_halt();
624 }
625
626 dp = of_find_node_by_phandle(node);
627
628 if (!of_node_is_type(dp, "display") && !of_node_is_type(dp, "serial")) {
629 prom_printf("Console device_type is neither display "
630 "nor serial.\n");
631 prom_halt();
632 }
633
634 of_console_device = dp;
635
636 printk(msg, of_console_path);
637 }
638