arch: simplify several early memory allocations
[linux-2.6-block.git] / arch / sparc / kernel / prom_64.c
CommitLineData
372b07bb
DM
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
cfbddd0d 18#include <linux/memblock.h>
372b07bb 19#include <linux/kernel.h>
372b07bb 20#include <linux/string.h>
cfbddd0d
SR
21#include <linux/types.h>
22#include <linux/cpu.h>
372b07bb 23#include <linux/mm.h>
035ebefc 24#include <linux/of.h>
372b07bb
DM
25
26#include <asm/prom.h>
27#include <asm/oplib.h>
2b1e5978
DM
28#include <asm/irq.h>
29#include <asm/asi.h>
30#include <asm/upa.h>
5cbc3073 31#include <asm/smp.h>
372b07bb 32
657f201d 33#include "prom.h"
fb7cd9d9 34
efeac2f8 35void * __init prom_early_alloc(unsigned long size)
372b07bb 36{
b63a07d6 37 void *ret = memblock_alloc(size, SMP_CACHE_BYTES);
372b07bb 38
b63a07d6 39 if (!ret) {
5da444aa 40 prom_printf("prom_early_alloc(%lu) failed\n", size);
ad072004
DM
41 prom_halt();
42 }
372b07bb
DM
43
44 prom_early_allocated += size;
45
46 return ret;
47}
48
372b07bb
DM
49/* The following routines deal with the black magic of fully naming a
50 * node.
51 *
52 * Certain well known named nodes are just the simple name string.
53 *
54 * Actual devices have an address specifier appended to the base name
55 * string, like this "foo@addr". The "addr" can be in any number of
56 * formats, and the platform plus the type of the node determine the
57 * format and how it is constructed.
58 *
59 * For children of the ROOT node, the naming convention is fixed and
60 * determined by whether this is a sun4u or sun4v system.
61 *
62 * For children of other nodes, it is bus type specific. So
63 * we walk up the tree until we discover a "device_type" property
64 * we recognize and we go from there.
65 *
66 * As an example, the boot device on my workstation has a full path:
67 *
68 * /pci@1e,600000/ide@d/disk@0,0:c
69 */
70static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
71{
bb31f9eb 72 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
73 struct linux_prom64_registers *regs;
74 struct property *rprop;
75 u32 high_bits, low_bits, type;
76
77 rprop = of_find_property(dp, "reg", NULL);
78 if (!rprop)
79 return;
80
81 regs = rprop->value;
035ebefc 82 if (!of_node_is_root(dp->parent)) {
a06ecbfe 83 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 84 name,
372b07bb
DM
85 (unsigned int) (regs->phys_addr >> 32UL),
86 (unsigned int) (regs->phys_addr & 0xffffffffUL));
87 return;
88 }
89
90 type = regs->phys_addr >> 60UL;
91 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
92 low_bits = (regs->phys_addr & 0xffffffffUL);
93
94 if (type == 0 || type == 8) {
95 const char *prefix = (type == 0) ? "m" : "i";
96
97 if (low_bits)
a06ecbfe 98 sprintf(tmp_buf, "%s@%s%x,%x",
bb31f9eb 99 name, prefix,
372b07bb
DM
100 high_bits, low_bits);
101 else
a06ecbfe 102 sprintf(tmp_buf, "%s@%s%x",
bb31f9eb 103 name,
372b07bb
DM
104 prefix,
105 high_bits);
106 } else if (type == 12) {
a06ecbfe 107 sprintf(tmp_buf, "%s@%x",
bb31f9eb 108 name, high_bits);
372b07bb
DM
109 }
110}
111
112static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
113{
bb31f9eb 114 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
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;
035ebefc 123 if (!of_node_is_root(dp->parent)) {
a06ecbfe 124 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 125 name,
372b07bb
DM
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
a06ecbfe 140 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 141 name,
372b07bb
DM
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{
bb31f9eb 150 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
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;
a06ecbfe 159 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 160 name,
372b07bb
DM
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{
bb31f9eb 168 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
169 struct linux_prom_pci_registers *regs;
170 struct property *prop;
171 unsigned int devfn;
172
173 prop = of_find_property(dp, "reg", NULL);
174 if (!prop)
175 return;
176
177 regs = prop->value;
178 devfn = (regs->phys_hi >> 8) & 0xff;
179 if (devfn & 0x07) {
a06ecbfe 180 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 181 name,
372b07bb
DM
182 devfn >> 3,
183 devfn & 0x07);
184 } else {
a06ecbfe 185 sprintf(tmp_buf, "%s@%x",
bb31f9eb 186 name,
372b07bb
DM
187 devfn >> 3);
188 }
189}
190
191/* "name@UPA_PORTID,offset" */
192static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
193{
bb31f9eb 194 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
195 struct linux_prom64_registers *regs;
196 struct property *prop;
197
198 prop = of_find_property(dp, "reg", NULL);
199 if (!prop)
200 return;
201
202 regs = prop->value;
203
204 prop = of_find_property(dp, "upa-portid", NULL);
205 if (!prop)
206 return;
207
a06ecbfe 208 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 209 name,
372b07bb
DM
210 *(u32 *) prop->value,
211 (unsigned int) (regs->phys_addr & 0xffffffffUL));
212}
213
214/* "name@reg" */
215static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
216{
bb31f9eb 217 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
218 struct property *prop;
219 u32 *regs;
220
221 prop = of_find_property(dp, "reg", NULL);
222 if (!prop)
223 return;
224
225 regs = prop->value;
226
bb31f9eb 227 sprintf(tmp_buf, "%s@%x", name, *regs);
372b07bb
DM
228}
229
230/* "name@addrhi,addrlo" */
231static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
232{
bb31f9eb 233 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
234 struct linux_prom64_registers *regs;
235 struct property *prop;
236
237 prop = of_find_property(dp, "reg", NULL);
238 if (!prop)
239 return;
240
241 regs = prop->value;
242
a06ecbfe 243 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 244 name,
372b07bb
DM
245 (unsigned int) (regs->phys_addr >> 32UL),
246 (unsigned int) (regs->phys_addr & 0xffffffffUL));
247}
248
249/* "name@bus,addr" */
250static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
251{
bb31f9eb 252 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
253 struct property *prop;
254 u32 *regs;
255
256 prop = of_find_property(dp, "reg", NULL);
257 if (!prop)
258 return;
259
260 regs = prop->value;
261
262 /* This actually isn't right... should look at the #address-cells
263 * property of the i2c bus node etc. etc.
264 */
a06ecbfe 265 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 266 name, regs[0], regs[1]);
372b07bb
DM
267}
268
269/* "name@reg0[,reg1]" */
270static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
271{
bb31f9eb 272 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
273 struct property *prop;
274 u32 *regs;
275
276 prop = of_find_property(dp, "reg", NULL);
277 if (!prop)
278 return;
279
280 regs = prop->value;
281
282 if (prop->length == sizeof(u32) || regs[1] == 1) {
a06ecbfe 283 sprintf(tmp_buf, "%s@%x",
bb31f9eb 284 name, regs[0]);
372b07bb 285 } else {
a06ecbfe 286 sprintf(tmp_buf, "%s@%x,%x",
bb31f9eb 287 name, regs[0], regs[1]);
372b07bb
DM
288 }
289}
290
291/* "name@reg0reg1[,reg2reg3]" */
292static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
293{
bb31f9eb 294 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
295 struct property *prop;
296 u32 *regs;
297
298 prop = of_find_property(dp, "reg", NULL);
299 if (!prop)
300 return;
301
302 regs = prop->value;
303
304 if (regs[2] || regs[3]) {
a06ecbfe 305 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
bb31f9eb 306 name, regs[0], regs[1], regs[2], regs[3]);
372b07bb 307 } else {
a06ecbfe 308 sprintf(tmp_buf, "%s@%08x%08x",
bb31f9eb 309 name, regs[0], regs[1]);
372b07bb
DM
310 }
311}
312
313static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
314{
315 struct device_node *parent = dp->parent;
316
317 if (parent != NULL) {
88ca0557
RH
318 if (of_node_is_type(parent, "pci") ||
319 of_node_is_type(parent, "pciex")) {
c91e2eca
DM
320 pci_path_component(dp, tmp_buf);
321 return;
322 }
88ca0557 323 if (of_node_is_type(parent, "sbus")) {
c91e2eca
DM
324 sbus_path_component(dp, tmp_buf);
325 return;
326 }
88ca0557 327 if (of_node_is_type(parent, "upa")) {
c91e2eca
DM
328 upa_path_component(dp, tmp_buf);
329 return;
330 }
88ca0557 331 if (of_node_is_type(parent, "ebus")) {
c91e2eca
DM
332 ebus_path_component(dp, tmp_buf);
333 return;
334 }
29c990df
RH
335 if (of_node_name_eq(parent, "usb") ||
336 of_node_name_eq(parent, "hub")) {
c91e2eca
DM
337 usb_path_component(dp, tmp_buf);
338 return;
339 }
88ca0557 340 if (of_node_is_type(parent, "i2c")) {
c91e2eca
DM
341 i2c_path_component(dp, tmp_buf);
342 return;
343 }
88ca0557 344 if (of_node_is_type(parent, "firewire")) {
c91e2eca
DM
345 ieee1394_path_component(dp, tmp_buf);
346 return;
347 }
88ca0557 348 if (of_node_is_type(parent, "virtual-devices")) {
c91e2eca
DM
349 vdev_path_component(dp, tmp_buf);
350 return;
351 }
372b07bb
DM
352 /* "isa" is handled with platform naming */
353 }
354
355 /* Use platform naming convention. */
c91e2eca
DM
356 if (tlb_type == hypervisor) {
357 sun4v_path_component(dp, tmp_buf);
358 return;
359 } else {
360 sun4u_path_component(dp, tmp_buf);
361 }
372b07bb
DM
362}
363
6524036a 364char * __init build_path_component(struct device_node *dp)
372b07bb 365{
bb31f9eb 366 const char *name = of_get_property(dp, "name", NULL);
372b07bb
DM
367 char tmp_buf[64], *n;
368
369 tmp_buf[0] = '\0';
370 __build_path_component(dp, tmp_buf);
371 if (tmp_buf[0] == '\0')
bb31f9eb 372 strcpy(tmp_buf, name);
372b07bb
DM
373
374 n = prom_early_alloc(strlen(tmp_buf) + 1);
375 strcpy(n, tmp_buf);
376
377 return n;
378}
379
5cbc3073
DM
380static const char *get_mid_prop(void)
381{
382 return (tlb_type == spitfire ? "upa-portid" : "portid");
383}
384
d1cb9d1a
DM
385bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
386 int cpu, unsigned int *thread)
387{
388 const char *mid_prop = get_mid_prop();
389 int this_cpu_id;
390
391 /* On hypervisor based platforms we interrogate the 'reg'
a718d139 392 * property. On everything else we look for a 'upa-portid',
d1cb9d1a
DM
393 * 'portid', or 'cpuid' property.
394 */
395
396 if (tlb_type == hypervisor) {
397 struct property *prop = of_find_property(cpun, "reg", NULL);
398 u32 *regs;
399
400 if (!prop) {
401 pr_warn("CPU node missing reg property\n");
402 return false;
403 }
404 regs = prop->value;
405 this_cpu_id = regs[0] & 0x0fffffff;
406 } else {
407 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
408
409 if (this_cpu_id < 0) {
410 mid_prop = "cpuid";
411 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
412 }
413 if (this_cpu_id < 0) {
414 pr_warn("CPU node missing cpu ID property\n");
415 return false;
416 }
417 }
418 if (this_cpu_id == cpu) {
419 if (thread) {
420 int proc_id = cpu_data(cpu).proc_id;
421
422 /* On sparc64, the cpu thread information is obtained
423 * either from OBP or the machine description. We've
424 * actually probed this information already long before
425 * this interface gets called so instead of interrogating
426 * both the OF node and the MDESC again, just use what
427 * we discovered already.
428 */
429 if (proc_id < 0)
430 proc_id = 0;
431 *thread = proc_id;
432 }
433 return true;
434 }
435 return false;
436}
437
9bab5414 438static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
5cbc3073
DM
439{
440 struct device_node *dp;
23dc758e
DM
441 const char *mid_prop;
442
23dc758e 443 mid_prop = get_mid_prop();
5cbc3073
DM
444 for_each_node_by_type(dp, "cpu") {
445 int cpuid = of_getintprop_default(dp, mid_prop, -1);
446 const char *this_mid_prop = mid_prop;
9bab5414 447 void *ret;
5cbc3073 448
5cbc3073
DM
449 if (cpuid < 0) {
450 this_mid_prop = "cpuid";
451 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
5cbc3073 452 }
5cbc3073
DM
453 if (cpuid < 0) {
454 prom_printf("OF: Serious problem, cpu lacks "
455 "%s property", this_mid_prop);
456 prom_halt();
457 }
5cbc3073 458#ifdef CONFIG_SMP
8a177c4f
DM
459 if (cpuid >= NR_CPUS) {
460 printk(KERN_WARNING "Ignoring CPU %d which is "
461 ">= NR_CPUS (%d)\n",
462 cpuid, NR_CPUS);
5cbc3073 463 continue;
8a177c4f 464 }
5cbc3073 465#endif
9bab5414
DM
466 ret = func(dp, cpuid, arg);
467 if (ret)
468 return ret;
469 }
470 return NULL;
471}
5cbc3073 472
9bab5414
DM
473static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
474{
475 if (id == cpuid)
476 return dp;
477 return NULL;
478}
479
480struct device_node *of_find_node_by_cpuid(int cpuid)
481{
482 return of_iterate_over_cpus(check_cpu_node, cpuid);
483}
484
485static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
486{
487 ncpus_probed++;
a2f9f6bb 488#ifdef CONFIG_SMP
9bab5414
DM
489 set_cpu_present(cpuid, true);
490 set_cpu_possible(cpuid, true);
a2f9f6bb 491#endif
9bab5414
DM
492 return NULL;
493}
5cbc3073 494
9bab5414
DM
495void __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}
5cbc3073 503
9bab5414
DM
504static 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_find_property(dp, "cpuid", NULL)) {
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) {
5cbc3073 556 cpu_data(cpuid).ecache_size =
9bab5414
DM
557 of_getintprop_default(portid_parent,
558 "l2-cache-size",
5cbc3073
DM
559 (4 * 1024 * 1024));
560 cpu_data(cpuid).ecache_line_size =
9bab5414
DM
561 of_getintprop_default(portid_parent,
562 "l2-cache-line-size", 64);
5cbc3073
DM
563 }
564
9bab5414
DM
565 cpu_data(cpuid).core_id = portid + 1;
566 cpu_data(cpuid).proc_id = portid;
9bab5414
DM
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;
5cbc3073
DM
586 }
587
9bab5414
DM
588 return NULL;
589}
590
591void __init of_fill_in_cpu_data(void)
592{
593 if (tlb_type == hypervisor)
594 return;
595
9bab5414
DM
596 of_iterate_over_cpus(fill_in_one_cpu, 0);
597
5cbc3073
DM
598 smp_fill_in_sib_core_maps();
599}
600
23dc758e 601void __init of_console_init(void)
c73fcc84
DM
602{
603 char *msg = "OF stdout device is: %s\n";
604 struct device_node *dp;
c73fcc84
DM
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);
c73fcc84 627
88ca0557 628 if (!of_node_is_type(dp, "display") && !of_node_is_type(dp, "serial")) {
c73fcc84
DM
629 prom_printf("Console device_type is neither display "
630 "nor serial.\n");
631 prom_halt();
632 }
633
634 of_console_device = dp;
635
c73fcc84
DM
636 printk(msg, of_console_path);
637}