Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 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 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <stdarg.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/threads.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/stringify.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/initrd.h> |
| 27 | #include <linux/bitops.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/kexec.h> |
| 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/irq.h> |
| 32 | #include <linux/lmb.h> |
| 33 | |
| 34 | #include <asm/prom.h> |
| 35 | #include <asm/page.h> |
| 36 | #include <asm/processor.h> |
| 37 | #include <asm/irq.h> |
| 38 | #include <linux/io.h> |
| 39 | #include <asm/system.h> |
| 40 | #include <asm/mmu.h> |
| 41 | #include <asm/pgtable.h> |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 42 | #include <asm/sections.h> |
| 43 | #include <asm/pci-bridge.h> |
| 44 | |
| 45 | static int __initdata dt_root_addr_cells; |
| 46 | static int __initdata dt_root_size_cells; |
| 47 | |
| 48 | typedef u32 cell_t; |
| 49 | |
| 50 | static struct boot_param_header *initial_boot_params; |
| 51 | |
| 52 | /* export that to outside world */ |
| 53 | struct device_node *of_chosen; |
| 54 | |
| 55 | static inline char *find_flat_dt_string(u32 offset) |
| 56 | { |
| 57 | return ((char *)initial_boot_params) + |
| 58 | initial_boot_params->off_dt_strings + offset; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * This function is used to scan the flattened device-tree, it is |
| 63 | * used to extract the memory informations at boot before we can |
| 64 | * unflatten the tree |
| 65 | */ |
| 66 | int __init of_scan_flat_dt(int (*it)(unsigned long node, |
| 67 | const char *uname, int depth, |
| 68 | void *data), |
| 69 | void *data) |
| 70 | { |
| 71 | unsigned long p = ((unsigned long)initial_boot_params) + |
| 72 | initial_boot_params->off_dt_struct; |
| 73 | int rc = 0; |
| 74 | int depth = -1; |
| 75 | |
| 76 | do { |
| 77 | u32 tag = *((u32 *)p); |
| 78 | char *pathp; |
| 79 | |
| 80 | p += 4; |
| 81 | if (tag == OF_DT_END_NODE) { |
| 82 | depth--; |
| 83 | continue; |
| 84 | } |
| 85 | if (tag == OF_DT_NOP) |
| 86 | continue; |
| 87 | if (tag == OF_DT_END) |
| 88 | break; |
| 89 | if (tag == OF_DT_PROP) { |
| 90 | u32 sz = *((u32 *)p); |
| 91 | p += 8; |
| 92 | if (initial_boot_params->version < 0x10) |
| 93 | p = _ALIGN(p, sz >= 8 ? 8 : 4); |
| 94 | p += sz; |
| 95 | p = _ALIGN(p, 4); |
| 96 | continue; |
| 97 | } |
| 98 | if (tag != OF_DT_BEGIN_NODE) { |
| 99 | printk(KERN_WARNING "Invalid tag %x scanning flattened" |
| 100 | " device tree !\n", tag); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | depth++; |
| 104 | pathp = (char *)p; |
| 105 | p = _ALIGN(p + strlen(pathp) + 1, 4); |
| 106 | if ((*pathp) == '/') { |
| 107 | char *lp, *np; |
| 108 | for (lp = NULL, np = pathp; *np; np++) |
| 109 | if ((*np) == '/') |
| 110 | lp = np+1; |
| 111 | if (lp != NULL) |
| 112 | pathp = lp; |
| 113 | } |
| 114 | rc = it(p, pathp, depth, data); |
| 115 | if (rc != 0) |
| 116 | break; |
| 117 | } while (1); |
| 118 | |
| 119 | return rc; |
| 120 | } |
| 121 | |
| 122 | unsigned long __init of_get_flat_dt_root(void) |
| 123 | { |
| 124 | unsigned long p = ((unsigned long)initial_boot_params) + |
| 125 | initial_boot_params->off_dt_struct; |
| 126 | |
| 127 | while (*((u32 *)p) == OF_DT_NOP) |
| 128 | p += 4; |
| 129 | BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE); |
| 130 | p += 4; |
| 131 | return _ALIGN(p + strlen((char *)p) + 1, 4); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * This function can be used within scan_flattened_dt callback to get |
| 136 | * access to properties |
| 137 | */ |
| 138 | void *__init of_get_flat_dt_prop(unsigned long node, const char *name, |
| 139 | unsigned long *size) |
| 140 | { |
| 141 | unsigned long p = node; |
| 142 | |
| 143 | do { |
| 144 | u32 tag = *((u32 *)p); |
| 145 | u32 sz, noff; |
| 146 | const char *nstr; |
| 147 | |
| 148 | p += 4; |
| 149 | if (tag == OF_DT_NOP) |
| 150 | continue; |
| 151 | if (tag != OF_DT_PROP) |
| 152 | return NULL; |
| 153 | |
| 154 | sz = *((u32 *)p); |
| 155 | noff = *((u32 *)(p + 4)); |
| 156 | p += 8; |
| 157 | if (initial_boot_params->version < 0x10) |
| 158 | p = _ALIGN(p, sz >= 8 ? 8 : 4); |
| 159 | |
| 160 | nstr = find_flat_dt_string(noff); |
| 161 | if (nstr == NULL) { |
| 162 | printk(KERN_WARNING "Can't find property index" |
| 163 | " name !\n"); |
| 164 | return NULL; |
| 165 | } |
| 166 | if (strcmp(name, nstr) == 0) { |
| 167 | if (size) |
| 168 | *size = sz; |
| 169 | return (void *)p; |
| 170 | } |
| 171 | p += sz; |
| 172 | p = _ALIGN(p, 4); |
| 173 | } while (1); |
| 174 | } |
| 175 | |
| 176 | int __init of_flat_dt_is_compatible(unsigned long node, const char *compat) |
| 177 | { |
| 178 | const char *cp; |
| 179 | unsigned long cplen, l; |
| 180 | |
| 181 | cp = of_get_flat_dt_prop(node, "compatible", &cplen); |
| 182 | if (cp == NULL) |
| 183 | return 0; |
| 184 | while (cplen > 0) { |
| 185 | if (strncasecmp(cp, compat, strlen(compat)) == 0) |
| 186 | return 1; |
| 187 | l = strlen(cp) + 1; |
| 188 | cp += l; |
| 189 | cplen -= l; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size, |
| 196 | unsigned long align) |
| 197 | { |
| 198 | void *res; |
| 199 | |
| 200 | *mem = _ALIGN(*mem, align); |
| 201 | res = (void *)*mem; |
| 202 | *mem += size; |
| 203 | |
| 204 | return res; |
| 205 | } |
| 206 | |
| 207 | static unsigned long __init unflatten_dt_node(unsigned long mem, |
| 208 | unsigned long *p, |
| 209 | struct device_node *dad, |
| 210 | struct device_node ***allnextpp, |
| 211 | unsigned long fpsize) |
| 212 | { |
| 213 | struct device_node *np; |
| 214 | struct property *pp, **prev_pp = NULL; |
| 215 | char *pathp; |
| 216 | u32 tag; |
| 217 | unsigned int l, allocl; |
| 218 | int has_name = 0; |
| 219 | int new_format = 0; |
| 220 | |
| 221 | tag = *((u32 *)(*p)); |
| 222 | if (tag != OF_DT_BEGIN_NODE) { |
| 223 | printk("Weird tag at start of node: %x\n", tag); |
| 224 | return mem; |
| 225 | } |
| 226 | *p += 4; |
| 227 | pathp = (char *)*p; |
| 228 | l = allocl = strlen(pathp) + 1; |
| 229 | *p = _ALIGN(*p + l, 4); |
| 230 | |
| 231 | /* version 0x10 has a more compact unit name here instead of the full |
| 232 | * path. we accumulate the full path size using "fpsize", we'll rebuild |
| 233 | * it later. We detect this because the first character of the name is |
| 234 | * not '/'. |
| 235 | */ |
| 236 | if ((*pathp) != '/') { |
| 237 | new_format = 1; |
| 238 | if (fpsize == 0) { |
| 239 | /* root node: special case. fpsize accounts for path |
| 240 | * plus terminating zero. root node only has '/', so |
| 241 | * fpsize should be 2, but we want to avoid the first |
| 242 | * level nodes to have two '/' so we use fpsize 1 here |
| 243 | */ |
| 244 | fpsize = 1; |
| 245 | allocl = 2; |
| 246 | } else { |
| 247 | /* account for '/' and path size minus terminal 0 |
| 248 | * already in 'l' |
| 249 | */ |
| 250 | fpsize += l; |
| 251 | allocl = fpsize; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl, |
| 256 | __alignof__(struct device_node)); |
| 257 | if (allnextpp) { |
| 258 | memset(np, 0, sizeof(*np)); |
| 259 | np->full_name = ((char *)np) + sizeof(struct device_node); |
| 260 | if (new_format) { |
| 261 | char *p2 = np->full_name; |
| 262 | /* rebuild full path for new format */ |
| 263 | if (dad && dad->parent) { |
| 264 | strcpy(p2, dad->full_name); |
| 265 | #ifdef DEBUG |
| 266 | if ((strlen(p2) + l + 1) != allocl) { |
| 267 | pr_debug("%s: p: %d, l: %d, a: %d\n", |
| 268 | pathp, (int)strlen(p2), |
| 269 | l, allocl); |
| 270 | } |
| 271 | #endif |
| 272 | p2 += strlen(p2); |
| 273 | } |
| 274 | *(p2++) = '/'; |
| 275 | memcpy(p2, pathp, l); |
| 276 | } else |
| 277 | memcpy(np->full_name, pathp, l); |
| 278 | prev_pp = &np->properties; |
| 279 | **allnextpp = np; |
| 280 | *allnextpp = &np->allnext; |
| 281 | if (dad != NULL) { |
| 282 | np->parent = dad; |
| 283 | /* we temporarily use the next field as `last_child'*/ |
| 284 | if (dad->next == NULL) |
| 285 | dad->child = np; |
| 286 | else |
| 287 | dad->next->sibling = np; |
| 288 | dad->next = np; |
| 289 | } |
| 290 | kref_init(&np->kref); |
| 291 | } |
| 292 | while (1) { |
| 293 | u32 sz, noff; |
| 294 | char *pname; |
| 295 | |
| 296 | tag = *((u32 *)(*p)); |
| 297 | if (tag == OF_DT_NOP) { |
| 298 | *p += 4; |
| 299 | continue; |
| 300 | } |
| 301 | if (tag != OF_DT_PROP) |
| 302 | break; |
| 303 | *p += 4; |
| 304 | sz = *((u32 *)(*p)); |
| 305 | noff = *((u32 *)((*p) + 4)); |
| 306 | *p += 8; |
| 307 | if (initial_boot_params->version < 0x10) |
| 308 | *p = _ALIGN(*p, sz >= 8 ? 8 : 4); |
| 309 | |
| 310 | pname = find_flat_dt_string(noff); |
| 311 | if (pname == NULL) { |
| 312 | printk(KERN_INFO |
| 313 | "Can't find property name in list !\n"); |
| 314 | break; |
| 315 | } |
| 316 | if (strcmp(pname, "name") == 0) |
| 317 | has_name = 1; |
| 318 | l = strlen(pname) + 1; |
| 319 | pp = unflatten_dt_alloc(&mem, sizeof(struct property), |
| 320 | __alignof__(struct property)); |
| 321 | if (allnextpp) { |
| 322 | if (strcmp(pname, "linux,phandle") == 0) { |
| 323 | np->node = *((u32 *)*p); |
| 324 | if (np->linux_phandle == 0) |
| 325 | np->linux_phandle = np->node; |
| 326 | } |
| 327 | if (strcmp(pname, "ibm,phandle") == 0) |
| 328 | np->linux_phandle = *((u32 *)*p); |
| 329 | pp->name = pname; |
| 330 | pp->length = sz; |
| 331 | pp->value = (void *)*p; |
| 332 | *prev_pp = pp; |
| 333 | prev_pp = &pp->next; |
| 334 | } |
| 335 | *p = _ALIGN((*p) + sz, 4); |
| 336 | } |
| 337 | /* with version 0x10 we may not have the name property, recreate |
| 338 | * it here from the unit name if absent |
| 339 | */ |
| 340 | if (!has_name) { |
| 341 | char *p1 = pathp, *ps = pathp, *pa = NULL; |
| 342 | int sz; |
| 343 | |
| 344 | while (*p1) { |
| 345 | if ((*p1) == '@') |
| 346 | pa = p1; |
| 347 | if ((*p1) == '/') |
| 348 | ps = p1 + 1; |
| 349 | p1++; |
| 350 | } |
| 351 | if (pa < ps) |
| 352 | pa = p1; |
| 353 | sz = (pa - ps) + 1; |
| 354 | pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz, |
| 355 | __alignof__(struct property)); |
| 356 | if (allnextpp) { |
| 357 | pp->name = "name"; |
| 358 | pp->length = sz; |
| 359 | pp->value = pp + 1; |
| 360 | *prev_pp = pp; |
| 361 | prev_pp = &pp->next; |
| 362 | memcpy(pp->value, ps, sz - 1); |
| 363 | ((char *)pp->value)[sz - 1] = 0; |
| 364 | pr_debug("fixed up name for %s -> %s\n", pathp, |
| 365 | (char *)pp->value); |
| 366 | } |
| 367 | } |
| 368 | if (allnextpp) { |
| 369 | *prev_pp = NULL; |
| 370 | np->name = of_get_property(np, "name", NULL); |
| 371 | np->type = of_get_property(np, "device_type", NULL); |
| 372 | |
| 373 | if (!np->name) |
| 374 | np->name = "<NULL>"; |
| 375 | if (!np->type) |
| 376 | np->type = "<NULL>"; |
| 377 | } |
| 378 | while (tag == OF_DT_BEGIN_NODE) { |
| 379 | mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize); |
| 380 | tag = *((u32 *)(*p)); |
| 381 | } |
| 382 | if (tag != OF_DT_END_NODE) { |
| 383 | printk(KERN_INFO "Weird tag at end of node: %x\n", tag); |
| 384 | return mem; |
| 385 | } |
| 386 | *p += 4; |
| 387 | return mem; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * unflattens the device-tree passed by the firmware, creating the |
| 392 | * tree of struct device_node. It also fills the "name" and "type" |
| 393 | * pointers of the nodes so the normal device-tree walking functions |
| 394 | * can be used (this used to be done by finish_device_tree) |
| 395 | */ |
| 396 | void __init unflatten_device_tree(void) |
| 397 | { |
| 398 | unsigned long start, mem, size; |
| 399 | struct device_node **allnextp = &allnodes; |
| 400 | |
| 401 | pr_debug(" -> unflatten_device_tree()\n"); |
| 402 | |
| 403 | /* First pass, scan for size */ |
| 404 | start = ((unsigned long)initial_boot_params) + |
| 405 | initial_boot_params->off_dt_struct; |
| 406 | size = unflatten_dt_node(0, &start, NULL, NULL, 0); |
| 407 | size = (size | 3) + 1; |
| 408 | |
| 409 | pr_debug(" size is %lx, allocating...\n", size); |
| 410 | |
| 411 | /* Allocate memory for the expanded device tree */ |
| 412 | mem = lmb_alloc(size + 4, __alignof__(struct device_node)); |
| 413 | mem = (unsigned long) __va(mem); |
| 414 | |
| 415 | ((u32 *)mem)[size / 4] = 0xdeadbeef; |
| 416 | |
| 417 | pr_debug(" unflattening %lx...\n", mem); |
| 418 | |
| 419 | /* Second pass, do actual unflattening */ |
| 420 | start = ((unsigned long)initial_boot_params) + |
| 421 | initial_boot_params->off_dt_struct; |
| 422 | unflatten_dt_node(mem, &start, NULL, &allnextp, 0); |
| 423 | if (*((u32 *)start) != OF_DT_END) |
| 424 | printk(KERN_WARNING "Weird tag at end of tree: %08x\n", |
| 425 | *((u32 *)start)); |
| 426 | if (((u32 *)mem)[size / 4] != 0xdeadbeef) |
| 427 | printk(KERN_WARNING "End of tree marker overwritten: %08x\n", |
| 428 | ((u32 *)mem)[size / 4]); |
| 429 | *allnextp = NULL; |
| 430 | |
| 431 | /* Get pointer to OF "/chosen" node for use everywhere */ |
| 432 | of_chosen = of_find_node_by_path("/chosen"); |
| 433 | if (of_chosen == NULL) |
| 434 | of_chosen = of_find_node_by_path("/chosen@0"); |
| 435 | |
| 436 | pr_debug(" <- unflatten_device_tree()\n"); |
| 437 | } |
| 438 | |
| 439 | #define early_init_dt_scan_drconf_memory(node) 0 |
| 440 | |
| 441 | static int __init early_init_dt_scan_cpus(unsigned long node, |
| 442 | const char *uname, int depth, |
| 443 | void *data) |
| 444 | { |
| 445 | static int logical_cpuid; |
| 446 | char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 447 | const u32 *intserv; |
| 448 | int i, nthreads; |
| 449 | int found = 0; |
| 450 | |
| 451 | /* We are scanning "cpu" nodes only */ |
| 452 | if (type == NULL || strcmp(type, "cpu") != 0) |
| 453 | return 0; |
| 454 | |
| 455 | /* Get physical cpuid */ |
| 456 | intserv = of_get_flat_dt_prop(node, "reg", NULL); |
| 457 | nthreads = 1; |
| 458 | |
| 459 | /* |
| 460 | * Now see if any of these threads match our boot cpu. |
| 461 | * NOTE: This must match the parsing done in smp_setup_cpu_maps. |
| 462 | */ |
| 463 | for (i = 0; i < nthreads; i++) { |
| 464 | /* |
| 465 | * version 2 of the kexec param format adds the phys cpuid of |
| 466 | * booted proc. |
| 467 | */ |
| 468 | if (initial_boot_params && initial_boot_params->version >= 2) { |
| 469 | if (intserv[i] == |
| 470 | initial_boot_params->boot_cpuid_phys) { |
| 471 | found = 1; |
| 472 | break; |
| 473 | } |
| 474 | } else { |
| 475 | /* |
| 476 | * Check if it's the boot-cpu, set it's hw index now, |
| 477 | * unfortunately this format did not support booting |
| 478 | * off secondary threads. |
| 479 | */ |
| 480 | if (of_get_flat_dt_prop(node, |
| 481 | "linux,boot-cpu", NULL) != NULL) { |
| 482 | found = 1; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | #ifdef CONFIG_SMP |
| 488 | /* logical cpu id is always 0 on UP kernels */ |
| 489 | logical_cpuid++; |
| 490 | #endif |
| 491 | } |
| 492 | |
| 493 | if (found) { |
| 494 | pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid, |
| 495 | intserv[i]); |
| 496 | boot_cpuid = logical_cpuid; |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | #ifdef CONFIG_BLK_DEV_INITRD |
| 503 | static void __init early_init_dt_check_for_initrd(unsigned long node) |
| 504 | { |
| 505 | unsigned long l; |
| 506 | u32 *prop; |
| 507 | |
| 508 | pr_debug("Looking for initrd properties... "); |
| 509 | |
| 510 | prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l); |
| 511 | if (prop) { |
| 512 | initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4)); |
| 513 | |
| 514 | prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l); |
| 515 | if (prop) { |
| 516 | initrd_end = (unsigned long) |
| 517 | __va(of_read_ulong(prop, l/4)); |
| 518 | initrd_below_start_ok = 1; |
| 519 | } else { |
| 520 | initrd_start = 0; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", |
| 525 | initrd_start, initrd_end); |
| 526 | } |
| 527 | #else |
| 528 | static inline void early_init_dt_check_for_initrd(unsigned long node) |
| 529 | { |
| 530 | } |
| 531 | #endif /* CONFIG_BLK_DEV_INITRD */ |
| 532 | |
| 533 | static int __init early_init_dt_scan_chosen(unsigned long node, |
| 534 | const char *uname, int depth, void *data) |
| 535 | { |
| 536 | unsigned long l; |
| 537 | char *p; |
| 538 | |
| 539 | pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
| 540 | |
| 541 | if (depth != 1 || |
| 542 | (strcmp(uname, "chosen") != 0 && |
| 543 | strcmp(uname, "chosen@0") != 0)) |
| 544 | return 0; |
| 545 | |
| 546 | #ifdef CONFIG_KEXEC |
| 547 | lprop = (u64 *)of_get_flat_dt_prop(node, |
| 548 | "linux,crashkernel-base", NULL); |
| 549 | if (lprop) |
| 550 | crashk_res.start = *lprop; |
| 551 | |
| 552 | lprop = (u64 *)of_get_flat_dt_prop(node, |
| 553 | "linux,crashkernel-size", NULL); |
| 554 | if (lprop) |
| 555 | crashk_res.end = crashk_res.start + *lprop - 1; |
| 556 | #endif |
| 557 | |
| 558 | early_init_dt_check_for_initrd(node); |
| 559 | |
| 560 | /* Retreive command line */ |
| 561 | p = of_get_flat_dt_prop(node, "bootargs", &l); |
| 562 | if (p != NULL && l > 0) |
| 563 | strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE)); |
| 564 | |
| 565 | #ifdef CONFIG_CMDLINE |
Michal Simek | 1dff89a | 2009-05-21 08:20:30 +0200 | [diff] [blame^] | 566 | #ifndef CONFIG_CMDLINE_FORCE |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 567 | if (p == NULL || l == 0 || (l == 1 && (*p) == 0)) |
Michal Simek | 1dff89a | 2009-05-21 08:20:30 +0200 | [diff] [blame^] | 568 | #endif |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 569 | strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
| 570 | #endif /* CONFIG_CMDLINE */ |
| 571 | |
| 572 | pr_debug("Command line is: %s\n", cmd_line); |
| 573 | |
| 574 | /* break now */ |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | static int __init early_init_dt_scan_root(unsigned long node, |
| 579 | const char *uname, int depth, void *data) |
| 580 | { |
| 581 | u32 *prop; |
| 582 | |
| 583 | if (depth != 0) |
| 584 | return 0; |
| 585 | |
| 586 | prop = of_get_flat_dt_prop(node, "#size-cells", NULL); |
| 587 | dt_root_size_cells = (prop == NULL) ? 1 : *prop; |
| 588 | pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells); |
| 589 | |
| 590 | prop = of_get_flat_dt_prop(node, "#address-cells", NULL); |
| 591 | dt_root_addr_cells = (prop == NULL) ? 2 : *prop; |
| 592 | pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells); |
| 593 | |
| 594 | /* break now */ |
| 595 | return 1; |
| 596 | } |
| 597 | |
| 598 | static u64 __init dt_mem_next_cell(int s, cell_t **cellp) |
| 599 | { |
| 600 | cell_t *p = *cellp; |
| 601 | |
| 602 | *cellp = p + s; |
| 603 | return of_read_number(p, s); |
| 604 | } |
| 605 | |
| 606 | static int __init early_init_dt_scan_memory(unsigned long node, |
| 607 | const char *uname, int depth, void *data) |
| 608 | { |
| 609 | char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 610 | cell_t *reg, *endp; |
| 611 | unsigned long l; |
| 612 | |
| 613 | /* Look for the ibm,dynamic-reconfiguration-memory node */ |
| 614 | /* if (depth == 1 && |
| 615 | strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0) |
| 616 | return early_init_dt_scan_drconf_memory(node); |
| 617 | */ |
| 618 | /* We are scanning "memory" nodes only */ |
| 619 | if (type == NULL) { |
| 620 | /* |
| 621 | * The longtrail doesn't have a device_type on the |
| 622 | * /memory node, so look for the node called /memory@0. |
| 623 | */ |
| 624 | if (depth != 1 || strcmp(uname, "memory@0") != 0) |
| 625 | return 0; |
| 626 | } else if (strcmp(type, "memory") != 0) |
| 627 | return 0; |
| 628 | |
| 629 | reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l); |
| 630 | if (reg == NULL) |
| 631 | reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l); |
| 632 | if (reg == NULL) |
| 633 | return 0; |
| 634 | |
| 635 | endp = reg + (l / sizeof(cell_t)); |
| 636 | |
| 637 | pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n", |
| 638 | uname, l, reg[0], reg[1], reg[2], reg[3]); |
| 639 | |
| 640 | while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { |
| 641 | u64 base, size; |
| 642 | |
| 643 | base = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 644 | size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 645 | |
| 646 | if (size == 0) |
| 647 | continue; |
| 648 | pr_debug(" - %llx , %llx\n", (unsigned long long)base, |
| 649 | (unsigned long long)size); |
| 650 | |
| 651 | lmb_add(base, size); |
| 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | #ifdef CONFIG_PHYP_DUMP |
| 657 | /** |
| 658 | * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg |
| 659 | * |
| 660 | * Function to find the largest size we need to reserve |
| 661 | * during early boot process. |
| 662 | * |
| 663 | * It either looks for boot param and returns that OR |
| 664 | * returns larger of 256 or 5% rounded down to multiples of 256MB. |
| 665 | * |
| 666 | */ |
| 667 | static inline unsigned long phyp_dump_calculate_reserve_size(void) |
| 668 | { |
| 669 | unsigned long tmp; |
| 670 | |
| 671 | if (phyp_dump_info->reserve_bootvar) |
| 672 | return phyp_dump_info->reserve_bootvar; |
| 673 | |
| 674 | /* divide by 20 to get 5% of value */ |
| 675 | tmp = lmb_end_of_DRAM(); |
| 676 | do_div(tmp, 20); |
| 677 | |
| 678 | /* round it down in multiples of 256 */ |
| 679 | tmp = tmp & ~0x0FFFFFFFUL; |
| 680 | |
| 681 | return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END); |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory |
| 686 | * |
| 687 | * This routine may reserve memory regions in the kernel only |
| 688 | * if the system is supported and a dump was taken in last |
| 689 | * boot instance or if the hardware is supported and the |
| 690 | * scratch area needs to be setup. In other instances it returns |
| 691 | * without reserving anything. The memory in case of dump being |
| 692 | * active is freed when the dump is collected (by userland tools). |
| 693 | */ |
| 694 | static void __init phyp_dump_reserve_mem(void) |
| 695 | { |
| 696 | unsigned long base, size; |
| 697 | unsigned long variable_reserve_size; |
| 698 | |
| 699 | if (!phyp_dump_info->phyp_dump_configured) { |
| 700 | printk(KERN_ERR "Phyp-dump not supported on this hardware\n"); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | if (!phyp_dump_info->phyp_dump_at_boot) { |
| 705 | printk(KERN_INFO "Phyp-dump disabled at boot time\n"); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | variable_reserve_size = phyp_dump_calculate_reserve_size(); |
| 710 | |
| 711 | if (phyp_dump_info->phyp_dump_is_active) { |
| 712 | /* Reserve *everything* above RMR.Area freed by userland tools*/ |
| 713 | base = variable_reserve_size; |
| 714 | size = lmb_end_of_DRAM() - base; |
| 715 | |
| 716 | /* XXX crashed_ram_end is wrong, since it may be beyond |
| 717 | * the memory_limit, it will need to be adjusted. */ |
| 718 | lmb_reserve(base, size); |
| 719 | |
| 720 | phyp_dump_info->init_reserve_start = base; |
| 721 | phyp_dump_info->init_reserve_size = size; |
| 722 | } else { |
| 723 | size = phyp_dump_info->cpu_state_size + |
| 724 | phyp_dump_info->hpte_region_size + |
| 725 | variable_reserve_size; |
| 726 | base = lmb_end_of_DRAM() - size; |
| 727 | lmb_reserve(base, size); |
| 728 | phyp_dump_info->init_reserve_start = base; |
| 729 | phyp_dump_info->init_reserve_size = size; |
| 730 | } |
| 731 | } |
| 732 | #else |
| 733 | static inline void __init phyp_dump_reserve_mem(void) {} |
| 734 | #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */ |
| 735 | |
| 736 | #ifdef CONFIG_EARLY_PRINTK |
| 737 | /* MS this is Microblaze specifig function */ |
| 738 | static int __init early_init_dt_scan_serial(unsigned long node, |
| 739 | const char *uname, int depth, void *data) |
| 740 | { |
| 741 | unsigned long l; |
| 742 | char *p; |
| 743 | int *addr; |
| 744 | |
| 745 | pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
| 746 | |
| 747 | /* find all serial nodes */ |
| 748 | if (strncmp(uname, "serial", 6) != 0) |
| 749 | return 0; |
| 750 | |
| 751 | early_init_dt_check_for_initrd(node); |
| 752 | |
| 753 | /* find compatible node with uartlite */ |
| 754 | p = of_get_flat_dt_prop(node, "compatible", &l); |
| 755 | if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) && |
| 756 | (strncmp(p, "xlnx,opb-uartlite", 17) != 0)) |
| 757 | return 0; |
| 758 | |
| 759 | addr = of_get_flat_dt_prop(node, "reg", &l); |
| 760 | return *addr; /* return address */ |
| 761 | } |
| 762 | |
| 763 | /* this function is looking for early uartlite console - Microblaze specific */ |
| 764 | int __init early_uartlite_console(void) |
| 765 | { |
| 766 | return of_scan_flat_dt(early_init_dt_scan_serial, NULL); |
| 767 | } |
| 768 | #endif |
| 769 | |
| 770 | void __init early_init_devtree(void *params) |
| 771 | { |
| 772 | pr_debug(" -> early_init_devtree(%p)\n", params); |
| 773 | |
| 774 | /* Setup flat device-tree pointer */ |
| 775 | initial_boot_params = params; |
| 776 | |
| 777 | #ifdef CONFIG_PHYP_DUMP |
| 778 | /* scan tree to see if dump occured during last boot */ |
| 779 | of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL); |
| 780 | #endif |
| 781 | |
| 782 | /* Retrieve various informations from the /chosen node of the |
| 783 | * device-tree, including the platform type, initrd location and |
| 784 | * size, TCE reserve, and more ... |
| 785 | */ |
| 786 | of_scan_flat_dt(early_init_dt_scan_chosen, NULL); |
| 787 | |
| 788 | /* Scan memory nodes and rebuild LMBs */ |
| 789 | lmb_init(); |
| 790 | of_scan_flat_dt(early_init_dt_scan_root, NULL); |
| 791 | of_scan_flat_dt(early_init_dt_scan_memory, NULL); |
| 792 | |
| 793 | /* Save command line for /proc/cmdline and then parse parameters */ |
| 794 | strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE); |
| 795 | parse_early_param(); |
| 796 | |
| 797 | lmb_analyze(); |
| 798 | |
| 799 | pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size()); |
| 800 | |
| 801 | pr_debug("Scanning CPUs ...\n"); |
| 802 | |
| 803 | /* Retreive CPU related informations from the flat tree |
| 804 | * (altivec support, boot CPU ID, ...) |
| 805 | */ |
| 806 | of_scan_flat_dt(early_init_dt_scan_cpus, NULL); |
| 807 | |
| 808 | pr_debug(" <- early_init_devtree()\n"); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Indicates whether the root node has a given value in its |
| 813 | * compatible property. |
| 814 | */ |
| 815 | int machine_is_compatible(const char *compat) |
| 816 | { |
| 817 | struct device_node *root; |
| 818 | int rc = 0; |
| 819 | |
| 820 | root = of_find_node_by_path("/"); |
| 821 | if (root) { |
| 822 | rc = of_device_is_compatible(root, compat); |
| 823 | of_node_put(root); |
| 824 | } |
| 825 | return rc; |
| 826 | } |
| 827 | EXPORT_SYMBOL(machine_is_compatible); |
| 828 | |
| 829 | /******* |
| 830 | * |
| 831 | * New implementation of the OF "find" APIs, return a refcounted |
| 832 | * object, call of_node_put() when done. The device tree and list |
| 833 | * are protected by a rw_lock. |
| 834 | * |
| 835 | * Note that property management will need some locking as well, |
| 836 | * this isn't dealt with yet. |
| 837 | * |
| 838 | *******/ |
| 839 | |
| 840 | /** |
| 841 | * of_find_node_by_phandle - Find a node given a phandle |
| 842 | * @handle: phandle of the node to find |
| 843 | * |
| 844 | * Returns a node pointer with refcount incremented, use |
| 845 | * of_node_put() on it when done. |
| 846 | */ |
| 847 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 848 | { |
| 849 | struct device_node *np; |
| 850 | |
| 851 | read_lock(&devtree_lock); |
| 852 | for (np = allnodes; np != NULL; np = np->allnext) |
| 853 | if (np->linux_phandle == handle) |
| 854 | break; |
| 855 | of_node_get(np); |
| 856 | read_unlock(&devtree_lock); |
| 857 | return np; |
| 858 | } |
| 859 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 860 | |
| 861 | /** |
| 862 | * of_find_all_nodes - Get next node in global list |
| 863 | * @prev: Previous node or NULL to start iteration |
| 864 | * of_node_put() will be called on it |
| 865 | * |
| 866 | * Returns a node pointer with refcount incremented, use |
| 867 | * of_node_put() on it when done. |
| 868 | */ |
| 869 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 870 | { |
| 871 | struct device_node *np; |
| 872 | |
| 873 | read_lock(&devtree_lock); |
| 874 | np = prev ? prev->allnext : allnodes; |
| 875 | for (; np != NULL; np = np->allnext) |
| 876 | if (of_node_get(np)) |
| 877 | break; |
| 878 | of_node_put(prev); |
| 879 | read_unlock(&devtree_lock); |
| 880 | return np; |
| 881 | } |
| 882 | EXPORT_SYMBOL(of_find_all_nodes); |
| 883 | |
| 884 | /** |
| 885 | * of_node_get - Increment refcount of a node |
| 886 | * @node: Node to inc refcount, NULL is supported to |
| 887 | * simplify writing of callers |
| 888 | * |
| 889 | * Returns node. |
| 890 | */ |
| 891 | struct device_node *of_node_get(struct device_node *node) |
| 892 | { |
| 893 | if (node) |
| 894 | kref_get(&node->kref); |
| 895 | return node; |
| 896 | } |
| 897 | EXPORT_SYMBOL(of_node_get); |
| 898 | |
| 899 | static inline struct device_node *kref_to_device_node(struct kref *kref) |
| 900 | { |
| 901 | return container_of(kref, struct device_node, kref); |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * of_node_release - release a dynamically allocated node |
| 906 | * @kref: kref element of the node to be released |
| 907 | * |
| 908 | * In of_node_put() this function is passed to kref_put() |
| 909 | * as the destructor. |
| 910 | */ |
| 911 | static void of_node_release(struct kref *kref) |
| 912 | { |
| 913 | struct device_node *node = kref_to_device_node(kref); |
| 914 | struct property *prop = node->properties; |
| 915 | |
| 916 | /* We should never be releasing nodes that haven't been detached. */ |
| 917 | if (!of_node_check_flag(node, OF_DETACHED)) { |
| 918 | printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n", |
| 919 | node->full_name); |
| 920 | dump_stack(); |
| 921 | kref_init(&node->kref); |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | if (!of_node_check_flag(node, OF_DYNAMIC)) |
| 926 | return; |
| 927 | |
| 928 | while (prop) { |
| 929 | struct property *next = prop->next; |
| 930 | kfree(prop->name); |
| 931 | kfree(prop->value); |
| 932 | kfree(prop); |
| 933 | prop = next; |
| 934 | |
| 935 | if (!prop) { |
| 936 | prop = node->deadprops; |
| 937 | node->deadprops = NULL; |
| 938 | } |
| 939 | } |
| 940 | kfree(node->full_name); |
| 941 | kfree(node->data); |
| 942 | kfree(node); |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * of_node_put - Decrement refcount of a node |
| 947 | * @node: Node to dec refcount, NULL is supported to |
| 948 | * simplify writing of callers |
| 949 | * |
| 950 | */ |
| 951 | void of_node_put(struct device_node *node) |
| 952 | { |
| 953 | if (node) |
| 954 | kref_put(&node->kref, of_node_release); |
| 955 | } |
| 956 | EXPORT_SYMBOL(of_node_put); |
| 957 | |
| 958 | /* |
| 959 | * Plug a device node into the tree and global list. |
| 960 | */ |
| 961 | void of_attach_node(struct device_node *np) |
| 962 | { |
| 963 | unsigned long flags; |
| 964 | |
| 965 | write_lock_irqsave(&devtree_lock, flags); |
| 966 | np->sibling = np->parent->child; |
| 967 | np->allnext = allnodes; |
| 968 | np->parent->child = np; |
| 969 | allnodes = np; |
| 970 | write_unlock_irqrestore(&devtree_lock, flags); |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * "Unplug" a node from the device tree. The caller must hold |
| 975 | * a reference to the node. The memory associated with the node |
| 976 | * is not freed until its refcount goes to zero. |
| 977 | */ |
| 978 | void of_detach_node(struct device_node *np) |
| 979 | { |
| 980 | struct device_node *parent; |
| 981 | unsigned long flags; |
| 982 | |
| 983 | write_lock_irqsave(&devtree_lock, flags); |
| 984 | |
| 985 | parent = np->parent; |
| 986 | if (!parent) |
| 987 | goto out_unlock; |
| 988 | |
| 989 | if (allnodes == np) |
| 990 | allnodes = np->allnext; |
| 991 | else { |
| 992 | struct device_node *prev; |
| 993 | for (prev = allnodes; |
| 994 | prev->allnext != np; |
| 995 | prev = prev->allnext) |
| 996 | ; |
| 997 | prev->allnext = np->allnext; |
| 998 | } |
| 999 | |
| 1000 | if (parent->child == np) |
| 1001 | parent->child = np->sibling; |
| 1002 | else { |
| 1003 | struct device_node *prevsib; |
| 1004 | for (prevsib = np->parent->child; |
| 1005 | prevsib->sibling != np; |
| 1006 | prevsib = prevsib->sibling) |
| 1007 | ; |
| 1008 | prevsib->sibling = np->sibling; |
| 1009 | } |
| 1010 | |
| 1011 | of_node_set_flag(np, OF_DETACHED); |
| 1012 | |
| 1013 | out_unlock: |
| 1014 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Add a property to a node |
| 1019 | */ |
| 1020 | int prom_add_property(struct device_node *np, struct property *prop) |
| 1021 | { |
| 1022 | struct property **next; |
| 1023 | unsigned long flags; |
| 1024 | |
| 1025 | prop->next = NULL; |
| 1026 | write_lock_irqsave(&devtree_lock, flags); |
| 1027 | next = &np->properties; |
| 1028 | while (*next) { |
| 1029 | if (strcmp(prop->name, (*next)->name) == 0) { |
| 1030 | /* duplicate ! don't insert it */ |
| 1031 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1032 | return -1; |
| 1033 | } |
| 1034 | next = &(*next)->next; |
| 1035 | } |
| 1036 | *next = prop; |
| 1037 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1038 | |
| 1039 | #ifdef CONFIG_PROC_DEVICETREE |
| 1040 | /* try to add to proc as well if it was initialized */ |
| 1041 | if (np->pde) |
| 1042 | proc_device_tree_add_prop(np->pde, prop); |
| 1043 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1044 | |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * Remove a property from a node. Note that we don't actually |
| 1050 | * remove it, since we have given out who-knows-how-many pointers |
| 1051 | * to the data using get-property. Instead we just move the property |
| 1052 | * to the "dead properties" list, so it won't be found any more. |
| 1053 | */ |
| 1054 | int prom_remove_property(struct device_node *np, struct property *prop) |
| 1055 | { |
| 1056 | struct property **next; |
| 1057 | unsigned long flags; |
| 1058 | int found = 0; |
| 1059 | |
| 1060 | write_lock_irqsave(&devtree_lock, flags); |
| 1061 | next = &np->properties; |
| 1062 | while (*next) { |
| 1063 | if (*next == prop) { |
| 1064 | /* found the node */ |
| 1065 | *next = prop->next; |
| 1066 | prop->next = np->deadprops; |
| 1067 | np->deadprops = prop; |
| 1068 | found = 1; |
| 1069 | break; |
| 1070 | } |
| 1071 | next = &(*next)->next; |
| 1072 | } |
| 1073 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1074 | |
| 1075 | if (!found) |
| 1076 | return -ENODEV; |
| 1077 | |
| 1078 | #ifdef CONFIG_PROC_DEVICETREE |
| 1079 | /* try to remove the proc node as well */ |
| 1080 | if (np->pde) |
| 1081 | proc_device_tree_remove_prop(np->pde, prop); |
| 1082 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Update a property in a node. Note that we don't actually |
| 1089 | * remove it, since we have given out who-knows-how-many pointers |
| 1090 | * to the data using get-property. Instead we just move the property |
| 1091 | * to the "dead properties" list, and add the new property to the |
| 1092 | * property list |
| 1093 | */ |
| 1094 | int prom_update_property(struct device_node *np, |
| 1095 | struct property *newprop, |
| 1096 | struct property *oldprop) |
| 1097 | { |
| 1098 | struct property **next; |
| 1099 | unsigned long flags; |
| 1100 | int found = 0; |
| 1101 | |
| 1102 | write_lock_irqsave(&devtree_lock, flags); |
| 1103 | next = &np->properties; |
| 1104 | while (*next) { |
| 1105 | if (*next == oldprop) { |
| 1106 | /* found the node */ |
| 1107 | newprop->next = oldprop->next; |
| 1108 | *next = newprop; |
| 1109 | oldprop->next = np->deadprops; |
| 1110 | np->deadprops = oldprop; |
| 1111 | found = 1; |
| 1112 | break; |
| 1113 | } |
| 1114 | next = &(*next)->next; |
| 1115 | } |
| 1116 | write_unlock_irqrestore(&devtree_lock, flags); |
| 1117 | |
| 1118 | if (!found) |
| 1119 | return -ENODEV; |
| 1120 | |
| 1121 | #ifdef CONFIG_PROC_DEVICETREE |
| 1122 | /* try to add to proc as well if it was initialized */ |
| 1123 | if (np->pde) |
| 1124 | proc_device_tree_update_prop(np->pde, newprop, oldprop); |
| 1125 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | #if defined(CONFIG_DEBUG_FS) && defined(DEBUG) |
| 1131 | static struct debugfs_blob_wrapper flat_dt_blob; |
| 1132 | |
| 1133 | static int __init export_flat_device_tree(void) |
| 1134 | { |
| 1135 | struct dentry *d; |
| 1136 | |
| 1137 | flat_dt_blob.data = initial_boot_params; |
| 1138 | flat_dt_blob.size = initial_boot_params->totalsize; |
| 1139 | |
| 1140 | d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR, |
| 1141 | of_debugfs_root, &flat_dt_blob); |
| 1142 | if (!d) |
| 1143 | return 1; |
| 1144 | |
| 1145 | return 0; |
| 1146 | } |
| 1147 | device_initcall(export_flat_device_tree); |
| 1148 | #endif |