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 35 void * __init prom_early_alloc(unsigned long size) 36 { 37 unsigned long paddr = memblock_phys_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 */ 73 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf) 74 { 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; 79 80 rprop = of_find_property(dp, "reg", NULL); 81 if (!rprop) 82 return; 83 84 regs = rprop->value; 85 if (!of_node_is_root(dp->parent)) { 86 sprintf(tmp_buf, "%s@%x,%x", 87 name, 88 (unsigned int) (regs->phys_addr >> 32UL), 89 (unsigned int) (regs->phys_addr & 0xffffffffUL)); 90 return; 91 } 92 93 type = regs->phys_addr >> 60UL; 94 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL; 95 low_bits = (regs->phys_addr & 0xffffffffUL); 96 97 if (type == 0 || type == 8) { 98 const char *prefix = (type == 0) ? "m" : "i"; 99 100 if (low_bits) 101 sprintf(tmp_buf, "%s@%s%x,%x", 102 name, prefix, 103 high_bits, low_bits); 104 else 105 sprintf(tmp_buf, "%s@%s%x", 106 name, 107 prefix, 108 high_bits); 109 } else if (type == 12) { 110 sprintf(tmp_buf, "%s@%x", 111 name, high_bits); 112 } 113 } 114 115 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf) 116 { 117 const char *name = of_get_property(dp, "name", NULL); 118 struct linux_prom64_registers *regs; 119 struct property *prop; 120 121 prop = of_find_property(dp, "reg", NULL); 122 if (!prop) 123 return; 124 125 regs = prop->value; 126 if (!of_node_is_root(dp->parent)) { 127 sprintf(tmp_buf, "%s@%x,%x", 128 name, 129 (unsigned int) (regs->phys_addr >> 32UL), 130 (unsigned int) (regs->phys_addr & 0xffffffffUL)); 131 return; 132 } 133 134 prop = of_find_property(dp, "upa-portid", NULL); 135 if (!prop) 136 prop = of_find_property(dp, "portid", NULL); 137 if (prop) { 138 unsigned long mask = 0xffffffffUL; 139 140 if (tlb_type >= cheetah) 141 mask = 0x7fffff; 142 143 sprintf(tmp_buf, "%s@%x,%x", 144 name, 145 *(u32 *)prop->value, 146 (unsigned int) (regs->phys_addr & mask)); 147 } 148 } 149 150 /* "name@slot,offset" */ 151 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf) 152 { 153 const char *name = of_get_property(dp, "name", NULL); 154 struct linux_prom_registers *regs; 155 struct property *prop; 156 157 prop = of_find_property(dp, "reg", NULL); 158 if (!prop) 159 return; 160 161 regs = prop->value; 162 sprintf(tmp_buf, "%s@%x,%x", 163 name, 164 regs->which_io, 165 regs->phys_addr); 166 } 167 168 /* "name@devnum[,func]" */ 169 static void __init pci_path_component(struct device_node *dp, char *tmp_buf) 170 { 171 const char *name = of_get_property(dp, "name", NULL); 172 struct linux_prom_pci_registers *regs; 173 struct property *prop; 174 unsigned int devfn; 175 176 prop = of_find_property(dp, "reg", NULL); 177 if (!prop) 178 return; 179 180 regs = prop->value; 181 devfn = (regs->phys_hi >> 8) & 0xff; 182 if (devfn & 0x07) { 183 sprintf(tmp_buf, "%s@%x,%x", 184 name, 185 devfn >> 3, 186 devfn & 0x07); 187 } else { 188 sprintf(tmp_buf, "%s@%x", 189 name, 190 devfn >> 3); 191 } 192 } 193 194 /* "name@UPA_PORTID,offset" */ 195 static void __init upa_path_component(struct device_node *dp, char *tmp_buf) 196 { 197 const char *name = of_get_property(dp, "name", NULL); 198 struct linux_prom64_registers *regs; 199 struct property *prop; 200 201 prop = of_find_property(dp, "reg", NULL); 202 if (!prop) 203 return; 204 205 regs = prop->value; 206 207 prop = of_find_property(dp, "upa-portid", NULL); 208 if (!prop) 209 return; 210 211 sprintf(tmp_buf, "%s@%x,%x", 212 name, 213 *(u32 *) prop->value, 214 (unsigned int) (regs->phys_addr & 0xffffffffUL)); 215 } 216 217 /* "name@reg" */ 218 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf) 219 { 220 const char *name = of_get_property(dp, "name", NULL); 221 struct property *prop; 222 u32 *regs; 223 224 prop = of_find_property(dp, "reg", NULL); 225 if (!prop) 226 return; 227 228 regs = prop->value; 229 230 sprintf(tmp_buf, "%s@%x", name, *regs); 231 } 232 233 /* "name@addrhi,addrlo" */ 234 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf) 235 { 236 const char *name = of_get_property(dp, "name", NULL); 237 struct linux_prom64_registers *regs; 238 struct property *prop; 239 240 prop = of_find_property(dp, "reg", NULL); 241 if (!prop) 242 return; 243 244 regs = prop->value; 245 246 sprintf(tmp_buf, "%s@%x,%x", 247 name, 248 (unsigned int) (regs->phys_addr >> 32UL), 249 (unsigned int) (regs->phys_addr & 0xffffffffUL)); 250 } 251 252 /* "name@bus,addr" */ 253 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf) 254 { 255 const char *name = of_get_property(dp, "name", NULL); 256 struct property *prop; 257 u32 *regs; 258 259 prop = of_find_property(dp, "reg", NULL); 260 if (!prop) 261 return; 262 263 regs = prop->value; 264 265 /* This actually isn't right... should look at the #address-cells 266 * property of the i2c bus node etc. etc. 267 */ 268 sprintf(tmp_buf, "%s@%x,%x", 269 name, regs[0], regs[1]); 270 } 271 272 /* "name@reg0[,reg1]" */ 273 static void __init usb_path_component(struct device_node *dp, char *tmp_buf) 274 { 275 const char *name = of_get_property(dp, "name", NULL); 276 struct property *prop; 277 u32 *regs; 278 279 prop = of_find_property(dp, "reg", NULL); 280 if (!prop) 281 return; 282 283 regs = prop->value; 284 285 if (prop->length == sizeof(u32) || regs[1] == 1) { 286 sprintf(tmp_buf, "%s@%x", 287 name, regs[0]); 288 } else { 289 sprintf(tmp_buf, "%s@%x,%x", 290 name, regs[0], regs[1]); 291 } 292 } 293 294 /* "name@reg0reg1[,reg2reg3]" */ 295 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf) 296 { 297 const char *name = of_get_property(dp, "name", NULL); 298 struct property *prop; 299 u32 *regs; 300 301 prop = of_find_property(dp, "reg", NULL); 302 if (!prop) 303 return; 304 305 regs = prop->value; 306 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]); 310 } else { 311 sprintf(tmp_buf, "%s@%08x%08x", 312 name, regs[0], regs[1]); 313 } 314 } 315 316 static void __init __build_path_component(struct device_node *dp, char *tmp_buf) 317 { 318 struct device_node *parent = dp->parent; 319 320 if (parent != NULL) { 321 if (of_node_is_type(parent, "pci") || 322 of_node_is_type(parent, "pciex")) { 323 pci_path_component(dp, tmp_buf); 324 return; 325 } 326 if (of_node_is_type(parent, "sbus")) { 327 sbus_path_component(dp, tmp_buf); 328 return; 329 } 330 if (of_node_is_type(parent, "upa")) { 331 upa_path_component(dp, tmp_buf); 332 return; 333 } 334 if (of_node_is_type(parent, "ebus")) { 335 ebus_path_component(dp, tmp_buf); 336 return; 337 } 338 if (of_node_name_eq(parent, "usb") || 339 of_node_name_eq(parent, "hub")) { 340 usb_path_component(dp, tmp_buf); 341 return; 342 } 343 if (of_node_is_type(parent, "i2c")) { 344 i2c_path_component(dp, tmp_buf); 345 return; 346 } 347 if (of_node_is_type(parent, "firewire")) { 348 ieee1394_path_component(dp, tmp_buf); 349 return; 350 } 351 if (of_node_is_type(parent, "virtual-devices")) { 352 vdev_path_component(dp, tmp_buf); 353 return; 354 } 355 /* "isa" is handled with platform naming */ 356 } 357 358 /* Use platform naming convention. */ 359 if (tlb_type == hypervisor) { 360 sun4v_path_component(dp, tmp_buf); 361 return; 362 } else { 363 sun4u_path_component(dp, tmp_buf); 364 } 365 } 366 367 char * __init build_path_component(struct device_node *dp) 368 { 369 const char *name = of_get_property(dp, "name", NULL); 370 char tmp_buf[64], *n; 371 372 tmp_buf[0] = '\0'; 373 __build_path_component(dp, tmp_buf); 374 if (tmp_buf[0] == '\0') 375 strcpy(tmp_buf, name); 376 377 n = prom_early_alloc(strlen(tmp_buf) + 1); 378 strcpy(n, tmp_buf); 379 380 return n; 381 } 382 383 static const char *get_mid_prop(void) 384 { 385 return (tlb_type == spitfire ? "upa-portid" : "portid"); 386 } 387 388 bool arch_find_n_match_cpu_physical_id(struct device_node *cpun, 389 int cpu, unsigned int *thread) 390 { 391 const char *mid_prop = get_mid_prop(); 392 int this_cpu_id; 393 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. 397 */ 398 399 if (tlb_type == hypervisor) { 400 struct property *prop = of_find_property(cpun, "reg", NULL); 401 u32 *regs; 402 403 if (!prop) { 404 pr_warn("CPU node missing reg property\n"); 405 return false; 406 } 407 regs = prop->value; 408 this_cpu_id = regs[0] & 0x0fffffff; 409 } else { 410 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1); 411 412 if (this_cpu_id < 0) { 413 mid_prop = "cpuid"; 414 this_cpu_id = of_getintprop_default(cpun, mid_prop, -1); 415 } 416 if (this_cpu_id < 0) { 417 pr_warn("CPU node missing cpu ID property\n"); 418 return false; 419 } 420 } 421 if (this_cpu_id == cpu) { 422 if (thread) { 423 int proc_id = cpu_data(cpu).proc_id; 424 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. 431 */ 432 if (proc_id < 0) 433 proc_id = 0; 434 *thread = proc_id; 435 } 436 return true; 437 } 438 return false; 439 } 440 441 static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg) 442 { 443 struct device_node *dp; 444 const char *mid_prop; 445 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; 450 void *ret; 451 452 if (cpuid < 0) { 453 this_mid_prop = "cpuid"; 454 cpuid = of_getintprop_default(dp, this_mid_prop, -1); 455 } 456 if (cpuid < 0) { 457 prom_printf("OF: Serious problem, cpu lacks " 458 "%s property", this_mid_prop); 459 prom_halt(); 460 } 461 #ifdef CONFIG_SMP 462 if (cpuid >= NR_CPUS) { 463 printk(KERN_WARNING "Ignoring CPU %d which is " 464 ">= NR_CPUS (%d)\n", 465 cpuid, NR_CPUS); 466 continue; 467 } 468 #endif 469 ret = func(dp, cpuid, arg); 470 if (ret) 471 return ret; 472 } 473 return NULL; 474 } 475 476 static void *check_cpu_node(struct device_node *dp, int cpuid, int id) 477 { 478 if (id == cpuid) 479 return dp; 480 return NULL; 481 } 482 483 struct device_node *of_find_node_by_cpuid(int cpuid) 484 { 485 return of_iterate_over_cpus(check_cpu_node, cpuid); 486 } 487 488 static void *record_one_cpu(struct device_node *dp, int cpuid, int arg) 489 { 490 ncpus_probed++; 491 #ifdef CONFIG_SMP 492 set_cpu_present(cpuid, true); 493 set_cpu_possible(cpuid, true); 494 #endif 495 return NULL; 496 } 497 498 void __init of_populate_present_mask(void) 499 { 500 if (tlb_type == hypervisor) 501 return; 502 503 ncpus_probed = 0; 504 of_iterate_over_cpus(record_one_cpu, 0); 505 } 506 507 static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg) 508 { 509 struct device_node *portid_parent = NULL; 510 int portid = -1; 511 512 if (of_find_property(dp, "cpuid", NULL)) { 513 int limit = 2; 514 515 portid_parent = dp; 516 while (limit--) { 517 portid_parent = portid_parent->parent; 518 if (!portid_parent) 519 break; 520 portid = of_getintprop_default(portid_parent, 521 "portid", -1); 522 if (portid >= 0) 523 break; 524 } 525 } 526 527 #ifndef CONFIG_SMP 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. 531 */ 532 if (cpuid != real_hard_smp_processor_id()) 533 return NULL; 534 cpuid = 0; 535 #endif 536 537 cpu_data(cpuid).clock_tick = 538 of_getintprop_default(dp, "clock-frequency", 0); 539 540 if (portid_parent) { 541 cpu_data(cpuid).dcache_size = 542 of_getintprop_default(dp, "l1-dcache-size", 543 16 * 1024); 544 cpu_data(cpuid).dcache_line_size = 545 of_getintprop_default(dp, "l1-dcache-line-size", 546 32); 547 cpu_data(cpuid).icache_size = 548 of_getintprop_default(dp, "l1-icache-size", 549 8 * 1024); 550 cpu_data(cpuid).icache_line_size = 551 of_getintprop_default(dp, "l1-icache-line-size", 552 32); 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, 561 "l2-cache-size", 562 (4 * 1024 * 1024)); 563 cpu_data(cpuid).ecache_line_size = 564 of_getintprop_default(portid_parent, 565 "l2-cache-line-size", 64); 566 } 567 568 cpu_data(cpuid).core_id = portid + 1; 569 cpu_data(cpuid).proc_id = portid; 570 } else { 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); 575 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); 580 581 cpu_data(cpuid).ecache_size = 582 of_getintprop_default(dp, "ecache-size", 583 (4 * 1024 * 1024)); 584 cpu_data(cpuid).ecache_line_size = 585 of_getintprop_default(dp, "ecache-line-size", 64); 586 587 cpu_data(cpuid).core_id = 0; 588 cpu_data(cpuid).proc_id = -1; 589 } 590 591 return NULL; 592 } 593 594 void __init of_fill_in_cpu_data(void) 595 { 596 if (tlb_type == hypervisor) 597 return; 598 599 of_iterate_over_cpus(fill_in_one_cpu, 0); 600 601 smp_fill_in_sib_core_maps(); 602 } 603 604 void __init of_console_init(void) 605 { 606 char *msg = "OF stdout device is: %s\n"; 607 struct device_node *dp; 608 phandle node; 609 610 of_console_path = prom_early_alloc(256); 611 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) { 612 prom_printf("Cannot obtain path of stdout.\n"); 613 prom_halt(); 614 } 615 of_console_options = strrchr(of_console_path, ':'); 616 if (of_console_options) { 617 of_console_options++; 618 if (*of_console_options == '\0') 619 of_console_options = NULL; 620 } 621 622 node = prom_inst2pkg(prom_stdout); 623 if (!node) { 624 prom_printf("Cannot resolve stdout node from " 625 "instance %08x.\n", prom_stdout); 626 prom_halt(); 627 } 628 629 dp = of_find_node_by_phandle(node); 630 631 if (!of_node_is_type(dp, "display") && !of_node_is_type(dp, "serial")) { 632 prom_printf("Console device_type is neither display " 633 "nor serial.\n"); 634 prom_halt(); 635 } 636 637 of_console_device = dp; 638 639 printk(msg, of_console_path); 640 } 641