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