Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [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 | #undef DEBUG |
| 17 | |
| 18 | #include <stdarg.h> |
| 19 | #include <linux/config.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/threads.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/pci.h> |
| 27 | #include <linux/stringify.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/initrd.h> |
| 30 | #include <linux/bitops.h> |
| 31 | #include <linux/module.h> |
Michael Ellerman | dcee303 | 2005-12-04 18:39:48 +1100 | [diff] [blame] | 32 | #include <linux/kexec.h> |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 33 | |
| 34 | #include <asm/prom.h> |
| 35 | #include <asm/rtas.h> |
| 36 | #include <asm/lmb.h> |
| 37 | #include <asm/page.h> |
| 38 | #include <asm/processor.h> |
| 39 | #include <asm/irq.h> |
| 40 | #include <asm/io.h> |
Michael Ellerman | 0cc4746 | 2005-12-04 18:39:37 +1100 | [diff] [blame] | 41 | #include <asm/kdump.h> |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 42 | #include <asm/smp.h> |
| 43 | #include <asm/system.h> |
| 44 | #include <asm/mmu.h> |
| 45 | #include <asm/pgtable.h> |
| 46 | #include <asm/pci.h> |
| 47 | #include <asm/iommu.h> |
| 48 | #include <asm/btext.h> |
| 49 | #include <asm/sections.h> |
| 50 | #include <asm/machdep.h> |
| 51 | #include <asm/pSeries_reconfig.h> |
Paul Mackerras | 40ef8cb | 2005-10-10 22:50:37 +1000 | [diff] [blame] | 52 | #include <asm/pci-bridge.h> |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 53 | |
| 54 | #ifdef DEBUG |
| 55 | #define DBG(fmt...) printk(KERN_ERR fmt) |
| 56 | #else |
| 57 | #define DBG(fmt...) |
| 58 | #endif |
| 59 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 60 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 61 | static int __initdata dt_root_addr_cells; |
| 62 | static int __initdata dt_root_size_cells; |
| 63 | |
| 64 | #ifdef CONFIG_PPC64 |
| 65 | static int __initdata iommu_is_off; |
| 66 | int __initdata iommu_force_on; |
Paul Mackerras | cf00a8d | 2005-10-31 13:07:02 +1100 | [diff] [blame] | 67 | unsigned long tce_alloc_start, tce_alloc_end; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | typedef u32 cell_t; |
| 71 | |
| 72 | #if 0 |
| 73 | static struct boot_param_header *initial_boot_params __initdata; |
| 74 | #else |
| 75 | struct boot_param_header *initial_boot_params; |
| 76 | #endif |
| 77 | |
| 78 | static struct device_node *allnodes = NULL; |
| 79 | |
| 80 | /* use when traversing tree through the allnext, child, sibling, |
| 81 | * or parent members of struct device_node. |
| 82 | */ |
| 83 | static DEFINE_RWLOCK(devtree_lock); |
| 84 | |
| 85 | /* export that to outside world */ |
| 86 | struct device_node *of_chosen; |
| 87 | |
| 88 | struct device_node *dflt_interrupt_controller; |
| 89 | int num_interrupt_controllers; |
| 90 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 91 | /* |
| 92 | * Wrapper for allocating memory for various data that needs to be |
| 93 | * attached to device nodes as they are processed at boot or when |
| 94 | * added to the device tree later (e.g. DLPAR). At boot there is |
| 95 | * already a region reserved so we just increment *mem_start by size; |
| 96 | * otherwise we call kmalloc. |
| 97 | */ |
| 98 | static void * prom_alloc(unsigned long size, unsigned long *mem_start) |
| 99 | { |
| 100 | unsigned long tmp; |
| 101 | |
| 102 | if (!mem_start) |
| 103 | return kmalloc(size, GFP_KERNEL); |
| 104 | |
| 105 | tmp = *mem_start; |
| 106 | *mem_start += size; |
| 107 | return (void *)tmp; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Find the device_node with a given phandle. |
| 112 | */ |
| 113 | static struct device_node * find_phandle(phandle ph) |
| 114 | { |
| 115 | struct device_node *np; |
| 116 | |
| 117 | for (np = allnodes; np != 0; np = np->allnext) |
| 118 | if (np->linux_phandle == ph) |
| 119 | return np; |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Find the interrupt parent of a node. |
| 125 | */ |
| 126 | static struct device_node * __devinit intr_parent(struct device_node *p) |
| 127 | { |
| 128 | phandle *parp; |
| 129 | |
| 130 | parp = (phandle *) get_property(p, "interrupt-parent", NULL); |
| 131 | if (parp == NULL) |
| 132 | return p->parent; |
| 133 | p = find_phandle(*parp); |
| 134 | if (p != NULL) |
| 135 | return p; |
| 136 | /* |
| 137 | * On a powermac booted with BootX, we don't get to know the |
| 138 | * phandles for any nodes, so find_phandle will return NULL. |
| 139 | * Fortunately these machines only have one interrupt controller |
| 140 | * so there isn't in fact any ambiguity. -- paulus |
| 141 | */ |
| 142 | if (num_interrupt_controllers == 1) |
| 143 | p = dflt_interrupt_controller; |
| 144 | return p; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Find out the size of each entry of the interrupts property |
| 149 | * for a node. |
| 150 | */ |
| 151 | int __devinit prom_n_intr_cells(struct device_node *np) |
| 152 | { |
| 153 | struct device_node *p; |
| 154 | unsigned int *icp; |
| 155 | |
| 156 | for (p = np; (p = intr_parent(p)) != NULL; ) { |
| 157 | icp = (unsigned int *) |
| 158 | get_property(p, "#interrupt-cells", NULL); |
| 159 | if (icp != NULL) |
| 160 | return *icp; |
| 161 | if (get_property(p, "interrupt-controller", NULL) != NULL |
| 162 | || get_property(p, "interrupt-map", NULL) != NULL) { |
| 163 | printk("oops, node %s doesn't have #interrupt-cells\n", |
| 164 | p->full_name); |
| 165 | return 1; |
| 166 | } |
| 167 | } |
| 168 | #ifdef DEBUG_IRQ |
| 169 | printk("prom_n_intr_cells failed for %s\n", np->full_name); |
| 170 | #endif |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Map an interrupt from a device up to the platform interrupt |
| 176 | * descriptor. |
| 177 | */ |
| 178 | static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler, |
| 179 | struct device_node *np, unsigned int *ints, |
| 180 | int nintrc) |
| 181 | { |
| 182 | struct device_node *p, *ipar; |
| 183 | unsigned int *imap, *imask, *ip; |
| 184 | int i, imaplen, match; |
| 185 | int newintrc = 0, newaddrc = 0; |
| 186 | unsigned int *reg; |
| 187 | int naddrc; |
| 188 | |
| 189 | reg = (unsigned int *) get_property(np, "reg", NULL); |
| 190 | naddrc = prom_n_addr_cells(np); |
| 191 | p = intr_parent(np); |
| 192 | while (p != NULL) { |
| 193 | if (get_property(p, "interrupt-controller", NULL) != NULL) |
| 194 | /* this node is an interrupt controller, stop here */ |
| 195 | break; |
| 196 | imap = (unsigned int *) |
| 197 | get_property(p, "interrupt-map", &imaplen); |
| 198 | if (imap == NULL) { |
| 199 | p = intr_parent(p); |
| 200 | continue; |
| 201 | } |
| 202 | imask = (unsigned int *) |
| 203 | get_property(p, "interrupt-map-mask", NULL); |
| 204 | if (imask == NULL) { |
| 205 | printk("oops, %s has interrupt-map but no mask\n", |
| 206 | p->full_name); |
| 207 | return 0; |
| 208 | } |
| 209 | imaplen /= sizeof(unsigned int); |
| 210 | match = 0; |
| 211 | ipar = NULL; |
| 212 | while (imaplen > 0 && !match) { |
| 213 | /* check the child-interrupt field */ |
| 214 | match = 1; |
| 215 | for (i = 0; i < naddrc && match; ++i) |
| 216 | match = ((reg[i] ^ imap[i]) & imask[i]) == 0; |
| 217 | for (; i < naddrc + nintrc && match; ++i) |
| 218 | match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0; |
| 219 | imap += naddrc + nintrc; |
| 220 | imaplen -= naddrc + nintrc; |
| 221 | /* grab the interrupt parent */ |
| 222 | ipar = find_phandle((phandle) *imap++); |
| 223 | --imaplen; |
| 224 | if (ipar == NULL && num_interrupt_controllers == 1) |
| 225 | /* cope with BootX not giving us phandles */ |
| 226 | ipar = dflt_interrupt_controller; |
| 227 | if (ipar == NULL) { |
| 228 | printk("oops, no int parent %x in map of %s\n", |
| 229 | imap[-1], p->full_name); |
| 230 | return 0; |
| 231 | } |
| 232 | /* find the parent's # addr and intr cells */ |
| 233 | ip = (unsigned int *) |
| 234 | get_property(ipar, "#interrupt-cells", NULL); |
| 235 | if (ip == NULL) { |
| 236 | printk("oops, no #interrupt-cells on %s\n", |
| 237 | ipar->full_name); |
| 238 | return 0; |
| 239 | } |
| 240 | newintrc = *ip; |
| 241 | ip = (unsigned int *) |
| 242 | get_property(ipar, "#address-cells", NULL); |
| 243 | newaddrc = (ip == NULL)? 0: *ip; |
| 244 | imap += newaddrc + newintrc; |
| 245 | imaplen -= newaddrc + newintrc; |
| 246 | } |
| 247 | if (imaplen < 0) { |
| 248 | printk("oops, error decoding int-map on %s, len=%d\n", |
| 249 | p->full_name, imaplen); |
| 250 | return 0; |
| 251 | } |
| 252 | if (!match) { |
| 253 | #ifdef DEBUG_IRQ |
| 254 | printk("oops, no match in %s int-map for %s\n", |
| 255 | p->full_name, np->full_name); |
| 256 | #endif |
| 257 | return 0; |
| 258 | } |
| 259 | p = ipar; |
| 260 | naddrc = newaddrc; |
| 261 | nintrc = newintrc; |
| 262 | ints = imap - nintrc; |
| 263 | reg = ints - naddrc; |
| 264 | } |
| 265 | if (p == NULL) { |
| 266 | #ifdef DEBUG_IRQ |
| 267 | printk("hmmm, int tree for %s doesn't have ctrler\n", |
| 268 | np->full_name); |
| 269 | #endif |
| 270 | return 0; |
| 271 | } |
| 272 | *irq = ints; |
| 273 | *ictrler = p; |
| 274 | return nintrc; |
| 275 | } |
| 276 | |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 277 | static unsigned char map_isa_senses[4] = { |
| 278 | IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, |
| 279 | IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE, |
| 280 | IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE, |
| 281 | IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE |
| 282 | }; |
| 283 | |
| 284 | static unsigned char map_mpic_senses[4] = { |
| 285 | IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE, |
| 286 | IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, |
| 287 | /* 2 seems to be used for the 8259 cascade... */ |
| 288 | IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE, |
| 289 | IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE, |
| 290 | }; |
| 291 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 292 | static int __devinit finish_node_interrupts(struct device_node *np, |
| 293 | unsigned long *mem_start, |
| 294 | int measure_only) |
| 295 | { |
| 296 | unsigned int *ints; |
| 297 | int intlen, intrcells, intrcount; |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 298 | int i, j, n, sense; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 299 | unsigned int *irq, virq; |
| 300 | struct device_node *ic; |
| 301 | |
Paul Mackerras | a575b80 | 2005-10-23 17:23:21 +1000 | [diff] [blame] | 302 | if (num_interrupt_controllers == 0) { |
| 303 | /* |
| 304 | * Old machines just have a list of interrupt numbers |
| 305 | * and no interrupt-controller nodes. |
| 306 | */ |
| 307 | ints = (unsigned int *) get_property(np, "AAPL,interrupts", |
| 308 | &intlen); |
| 309 | /* XXX old interpret_pci_props looked in parent too */ |
| 310 | /* XXX old interpret_macio_props looked for interrupts |
| 311 | before AAPL,interrupts */ |
| 312 | if (ints == NULL) |
| 313 | ints = (unsigned int *) get_property(np, "interrupts", |
| 314 | &intlen); |
| 315 | if (ints == NULL) |
| 316 | return 0; |
| 317 | |
| 318 | np->n_intrs = intlen / sizeof(unsigned int); |
| 319 | np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]), |
| 320 | mem_start); |
| 321 | if (!np->intrs) |
| 322 | return -ENOMEM; |
| 323 | if (measure_only) |
| 324 | return 0; |
| 325 | |
| 326 | for (i = 0; i < np->n_intrs; ++i) { |
| 327 | np->intrs[i].line = *ints++; |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 328 | np->intrs[i].sense = IRQ_SENSE_LEVEL |
| 329 | | IRQ_POLARITY_NEGATIVE; |
Paul Mackerras | a575b80 | 2005-10-23 17:23:21 +1000 | [diff] [blame] | 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 334 | ints = (unsigned int *) get_property(np, "interrupts", &intlen); |
| 335 | if (ints == NULL) |
| 336 | return 0; |
| 337 | intrcells = prom_n_intr_cells(np); |
| 338 | intlen /= intrcells * sizeof(unsigned int); |
| 339 | |
| 340 | np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start); |
| 341 | if (!np->intrs) |
| 342 | return -ENOMEM; |
| 343 | |
| 344 | if (measure_only) |
| 345 | return 0; |
| 346 | |
| 347 | intrcount = 0; |
| 348 | for (i = 0; i < intlen; ++i, ints += intrcells) { |
| 349 | n = map_interrupt(&irq, &ic, np, ints, intrcells); |
| 350 | if (n <= 0) |
| 351 | continue; |
| 352 | |
| 353 | /* don't map IRQ numbers under a cascaded 8259 controller */ |
| 354 | if (ic && device_is_compatible(ic, "chrp,iic")) { |
| 355 | np->intrs[intrcount].line = irq[0]; |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 356 | sense = (n > 1)? (irq[1] & 3): 3; |
| 357 | np->intrs[intrcount].sense = map_isa_senses[sense]; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 358 | } else { |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 359 | virq = virt_irq_create_mapping(irq[0]); |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 360 | #ifdef CONFIG_PPC64 |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 361 | if (virq == NO_IRQ) { |
| 362 | printk(KERN_CRIT "Could not allocate interrupt" |
| 363 | " number for %s\n", np->full_name); |
| 364 | continue; |
| 365 | } |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 366 | #endif |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 367 | np->intrs[intrcount].line = irq_offset_up(virq); |
| 368 | sense = (n > 1)? (irq[1] & 3): 1; |
| 369 | np->intrs[intrcount].sense = map_mpic_senses[sense]; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | #ifdef CONFIG_PPC64 |
| 373 | /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */ |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 374 | if (_machine == PLATFORM_POWERMAC && ic && ic->parent) { |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 375 | char *name = get_property(ic->parent, "name", NULL); |
| 376 | if (name && !strcmp(name, "u3")) |
| 377 | np->intrs[intrcount].line += 128; |
| 378 | else if (!(name && !strcmp(name, "mac-io"))) |
| 379 | /* ignore other cascaded controllers, such as |
| 380 | the k2-sata-root */ |
| 381 | break; |
| 382 | } |
| 383 | #endif |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 384 | if (n > 2) { |
| 385 | printk("hmmm, got %d intr cells for %s:", n, |
| 386 | np->full_name); |
| 387 | for (j = 0; j < n; ++j) |
| 388 | printk(" %d", irq[j]); |
| 389 | printk("\n"); |
| 390 | } |
| 391 | ++intrcount; |
| 392 | } |
| 393 | np->n_intrs = intrcount; |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 398 | static int __devinit finish_node(struct device_node *np, |
| 399 | unsigned long *mem_start, |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 400 | int measure_only) |
| 401 | { |
| 402 | struct device_node *child; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 403 | int rc = 0; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 404 | |
| 405 | rc = finish_node_interrupts(np, mem_start, measure_only); |
| 406 | if (rc) |
| 407 | goto out; |
| 408 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 409 | for (child = np->child; child != NULL; child = child->sibling) { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 410 | rc = finish_node(child, mem_start, measure_only); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 411 | if (rc) |
| 412 | goto out; |
| 413 | } |
| 414 | out: |
| 415 | return rc; |
| 416 | } |
| 417 | |
| 418 | static void __init scan_interrupt_controllers(void) |
| 419 | { |
| 420 | struct device_node *np; |
| 421 | int n = 0; |
| 422 | char *name, *ic; |
| 423 | int iclen; |
| 424 | |
| 425 | for (np = allnodes; np != NULL; np = np->allnext) { |
| 426 | ic = get_property(np, "interrupt-controller", &iclen); |
| 427 | name = get_property(np, "name", NULL); |
| 428 | /* checking iclen makes sure we don't get a false |
| 429 | match on /chosen.interrupt_controller */ |
| 430 | if ((name != NULL |
| 431 | && strcmp(name, "interrupt-controller") == 0) |
| 432 | || (ic != NULL && iclen == 0 |
| 433 | && strcmp(name, "AppleKiwi"))) { |
| 434 | if (n == 0) |
| 435 | dflt_interrupt_controller = np; |
| 436 | ++n; |
| 437 | } |
| 438 | } |
| 439 | num_interrupt_controllers = n; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * finish_device_tree is called once things are running normally |
| 444 | * (i.e. with text and data mapped to the address they were linked at). |
| 445 | * It traverses the device tree and fills in some of the additional, |
| 446 | * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt |
| 447 | * mapping is also initialized at this point. |
| 448 | */ |
| 449 | void __init finish_device_tree(void) |
| 450 | { |
| 451 | unsigned long start, end, size = 0; |
| 452 | |
| 453 | DBG(" -> finish_device_tree\n"); |
| 454 | |
| 455 | #ifdef CONFIG_PPC64 |
| 456 | /* Initialize virtual IRQ map */ |
| 457 | virt_irq_init(); |
| 458 | #endif |
| 459 | scan_interrupt_controllers(); |
| 460 | |
| 461 | /* |
| 462 | * Finish device-tree (pre-parsing some properties etc...) |
| 463 | * We do this in 2 passes. One with "measure_only" set, which |
| 464 | * will only measure the amount of memory needed, then we can |
| 465 | * allocate that memory, and call finish_node again. However, |
| 466 | * we must be careful as most routines will fail nowadays when |
| 467 | * prom_alloc() returns 0, so we must make sure our first pass |
| 468 | * doesn't start at 0. We pre-initialize size to 16 for that |
| 469 | * reason and then remove those additional 16 bytes |
| 470 | */ |
| 471 | size = 16; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 472 | finish_node(allnodes, &size, 1); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 473 | size -= 16; |
| 474 | end = start = (unsigned long) __va(lmb_alloc(size, 128)); |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 475 | finish_node(allnodes, &end, 0); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 476 | BUG_ON(end != start + size); |
| 477 | |
| 478 | DBG(" <- finish_device_tree\n"); |
| 479 | } |
| 480 | |
| 481 | static inline char *find_flat_dt_string(u32 offset) |
| 482 | { |
| 483 | return ((char *)initial_boot_params) + |
| 484 | initial_boot_params->off_dt_strings + offset; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * This function is used to scan the flattened device-tree, it is |
| 489 | * used to extract the memory informations at boot before we can |
| 490 | * unflatten the tree |
| 491 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 492 | int __init of_scan_flat_dt(int (*it)(unsigned long node, |
| 493 | const char *uname, int depth, |
| 494 | void *data), |
| 495 | void *data) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 496 | { |
| 497 | unsigned long p = ((unsigned long)initial_boot_params) + |
| 498 | initial_boot_params->off_dt_struct; |
| 499 | int rc = 0; |
| 500 | int depth = -1; |
| 501 | |
| 502 | do { |
| 503 | u32 tag = *((u32 *)p); |
| 504 | char *pathp; |
| 505 | |
| 506 | p += 4; |
| 507 | if (tag == OF_DT_END_NODE) { |
| 508 | depth --; |
| 509 | continue; |
| 510 | } |
| 511 | if (tag == OF_DT_NOP) |
| 512 | continue; |
| 513 | if (tag == OF_DT_END) |
| 514 | break; |
| 515 | if (tag == OF_DT_PROP) { |
| 516 | u32 sz = *((u32 *)p); |
| 517 | p += 8; |
| 518 | if (initial_boot_params->version < 0x10) |
| 519 | p = _ALIGN(p, sz >= 8 ? 8 : 4); |
| 520 | p += sz; |
| 521 | p = _ALIGN(p, 4); |
| 522 | continue; |
| 523 | } |
| 524 | if (tag != OF_DT_BEGIN_NODE) { |
| 525 | printk(KERN_WARNING "Invalid tag %x scanning flattened" |
| 526 | " device tree !\n", tag); |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | depth++; |
| 530 | pathp = (char *)p; |
| 531 | p = _ALIGN(p + strlen(pathp) + 1, 4); |
| 532 | if ((*pathp) == '/') { |
| 533 | char *lp, *np; |
| 534 | for (lp = NULL, np = pathp; *np; np++) |
| 535 | if ((*np) == '/') |
| 536 | lp = np+1; |
| 537 | if (lp != NULL) |
| 538 | pathp = lp; |
| 539 | } |
| 540 | rc = it(p, pathp, depth, data); |
| 541 | if (rc != 0) |
| 542 | break; |
| 543 | } while(1); |
| 544 | |
| 545 | return rc; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * This function can be used within scan_flattened_dt callback to get |
| 550 | * access to properties |
| 551 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 552 | void* __init of_get_flat_dt_prop(unsigned long node, const char *name, |
| 553 | unsigned long *size) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 554 | { |
| 555 | unsigned long p = node; |
| 556 | |
| 557 | do { |
| 558 | u32 tag = *((u32 *)p); |
| 559 | u32 sz, noff; |
| 560 | const char *nstr; |
| 561 | |
| 562 | p += 4; |
| 563 | if (tag == OF_DT_NOP) |
| 564 | continue; |
| 565 | if (tag != OF_DT_PROP) |
| 566 | return NULL; |
| 567 | |
| 568 | sz = *((u32 *)p); |
| 569 | noff = *((u32 *)(p + 4)); |
| 570 | p += 8; |
| 571 | if (initial_boot_params->version < 0x10) |
| 572 | p = _ALIGN(p, sz >= 8 ? 8 : 4); |
| 573 | |
| 574 | nstr = find_flat_dt_string(noff); |
| 575 | if (nstr == NULL) { |
| 576 | printk(KERN_WARNING "Can't find property index" |
| 577 | " name !\n"); |
| 578 | return NULL; |
| 579 | } |
| 580 | if (strcmp(name, nstr) == 0) { |
| 581 | if (size) |
| 582 | *size = sz; |
| 583 | return (void *)p; |
| 584 | } |
| 585 | p += sz; |
| 586 | p = _ALIGN(p, 4); |
| 587 | } while(1); |
| 588 | } |
| 589 | |
| 590 | static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size, |
| 591 | unsigned long align) |
| 592 | { |
| 593 | void *res; |
| 594 | |
| 595 | *mem = _ALIGN(*mem, align); |
| 596 | res = (void *)*mem; |
| 597 | *mem += size; |
| 598 | |
| 599 | return res; |
| 600 | } |
| 601 | |
| 602 | static unsigned long __init unflatten_dt_node(unsigned long mem, |
| 603 | unsigned long *p, |
| 604 | struct device_node *dad, |
| 605 | struct device_node ***allnextpp, |
| 606 | unsigned long fpsize) |
| 607 | { |
| 608 | struct device_node *np; |
| 609 | struct property *pp, **prev_pp = NULL; |
| 610 | char *pathp; |
| 611 | u32 tag; |
| 612 | unsigned int l, allocl; |
| 613 | int has_name = 0; |
| 614 | int new_format = 0; |
| 615 | |
| 616 | tag = *((u32 *)(*p)); |
| 617 | if (tag != OF_DT_BEGIN_NODE) { |
| 618 | printk("Weird tag at start of node: %x\n", tag); |
| 619 | return mem; |
| 620 | } |
| 621 | *p += 4; |
| 622 | pathp = (char *)*p; |
| 623 | l = allocl = strlen(pathp) + 1; |
| 624 | *p = _ALIGN(*p + l, 4); |
| 625 | |
| 626 | /* version 0x10 has a more compact unit name here instead of the full |
| 627 | * path. we accumulate the full path size using "fpsize", we'll rebuild |
| 628 | * it later. We detect this because the first character of the name is |
| 629 | * not '/'. |
| 630 | */ |
| 631 | if ((*pathp) != '/') { |
| 632 | new_format = 1; |
| 633 | if (fpsize == 0) { |
| 634 | /* root node: special case. fpsize accounts for path |
| 635 | * plus terminating zero. root node only has '/', so |
| 636 | * fpsize should be 2, but we want to avoid the first |
| 637 | * level nodes to have two '/' so we use fpsize 1 here |
| 638 | */ |
| 639 | fpsize = 1; |
| 640 | allocl = 2; |
| 641 | } else { |
| 642 | /* account for '/' and path size minus terminal 0 |
| 643 | * already in 'l' |
| 644 | */ |
| 645 | fpsize += l; |
| 646 | allocl = fpsize; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
| 651 | np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl, |
| 652 | __alignof__(struct device_node)); |
| 653 | if (allnextpp) { |
| 654 | memset(np, 0, sizeof(*np)); |
| 655 | np->full_name = ((char*)np) + sizeof(struct device_node); |
| 656 | if (new_format) { |
| 657 | char *p = np->full_name; |
| 658 | /* rebuild full path for new format */ |
| 659 | if (dad && dad->parent) { |
| 660 | strcpy(p, dad->full_name); |
| 661 | #ifdef DEBUG |
| 662 | if ((strlen(p) + l + 1) != allocl) { |
| 663 | DBG("%s: p: %d, l: %d, a: %d\n", |
| 664 | pathp, strlen(p), l, allocl); |
| 665 | } |
| 666 | #endif |
| 667 | p += strlen(p); |
| 668 | } |
| 669 | *(p++) = '/'; |
| 670 | memcpy(p, pathp, l); |
| 671 | } else |
| 672 | memcpy(np->full_name, pathp, l); |
| 673 | prev_pp = &np->properties; |
| 674 | **allnextpp = np; |
| 675 | *allnextpp = &np->allnext; |
| 676 | if (dad != NULL) { |
| 677 | np->parent = dad; |
| 678 | /* we temporarily use the next field as `last_child'*/ |
| 679 | if (dad->next == 0) |
| 680 | dad->child = np; |
| 681 | else |
| 682 | dad->next->sibling = np; |
| 683 | dad->next = np; |
| 684 | } |
| 685 | kref_init(&np->kref); |
| 686 | } |
| 687 | while(1) { |
| 688 | u32 sz, noff; |
| 689 | char *pname; |
| 690 | |
| 691 | tag = *((u32 *)(*p)); |
| 692 | if (tag == OF_DT_NOP) { |
| 693 | *p += 4; |
| 694 | continue; |
| 695 | } |
| 696 | if (tag != OF_DT_PROP) |
| 697 | break; |
| 698 | *p += 4; |
| 699 | sz = *((u32 *)(*p)); |
| 700 | noff = *((u32 *)((*p) + 4)); |
| 701 | *p += 8; |
| 702 | if (initial_boot_params->version < 0x10) |
| 703 | *p = _ALIGN(*p, sz >= 8 ? 8 : 4); |
| 704 | |
| 705 | pname = find_flat_dt_string(noff); |
| 706 | if (pname == NULL) { |
| 707 | printk("Can't find property name in list !\n"); |
| 708 | break; |
| 709 | } |
| 710 | if (strcmp(pname, "name") == 0) |
| 711 | has_name = 1; |
| 712 | l = strlen(pname) + 1; |
| 713 | pp = unflatten_dt_alloc(&mem, sizeof(struct property), |
| 714 | __alignof__(struct property)); |
| 715 | if (allnextpp) { |
| 716 | if (strcmp(pname, "linux,phandle") == 0) { |
| 717 | np->node = *((u32 *)*p); |
| 718 | if (np->linux_phandle == 0) |
| 719 | np->linux_phandle = np->node; |
| 720 | } |
| 721 | if (strcmp(pname, "ibm,phandle") == 0) |
| 722 | np->linux_phandle = *((u32 *)*p); |
| 723 | pp->name = pname; |
| 724 | pp->length = sz; |
| 725 | pp->value = (void *)*p; |
| 726 | *prev_pp = pp; |
| 727 | prev_pp = &pp->next; |
| 728 | } |
| 729 | *p = _ALIGN((*p) + sz, 4); |
| 730 | } |
| 731 | /* with version 0x10 we may not have the name property, recreate |
| 732 | * it here from the unit name if absent |
| 733 | */ |
| 734 | if (!has_name) { |
| 735 | char *p = pathp, *ps = pathp, *pa = NULL; |
| 736 | int sz; |
| 737 | |
| 738 | while (*p) { |
| 739 | if ((*p) == '@') |
| 740 | pa = p; |
| 741 | if ((*p) == '/') |
| 742 | ps = p + 1; |
| 743 | p++; |
| 744 | } |
| 745 | if (pa < ps) |
| 746 | pa = p; |
| 747 | sz = (pa - ps) + 1; |
| 748 | pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz, |
| 749 | __alignof__(struct property)); |
| 750 | if (allnextpp) { |
| 751 | pp->name = "name"; |
| 752 | pp->length = sz; |
| 753 | pp->value = (unsigned char *)(pp + 1); |
| 754 | *prev_pp = pp; |
| 755 | prev_pp = &pp->next; |
| 756 | memcpy(pp->value, ps, sz - 1); |
| 757 | ((char *)pp->value)[sz - 1] = 0; |
| 758 | DBG("fixed up name for %s -> %s\n", pathp, pp->value); |
| 759 | } |
| 760 | } |
| 761 | if (allnextpp) { |
| 762 | *prev_pp = NULL; |
| 763 | np->name = get_property(np, "name", NULL); |
| 764 | np->type = get_property(np, "device_type", NULL); |
| 765 | |
| 766 | if (!np->name) |
| 767 | np->name = "<NULL>"; |
| 768 | if (!np->type) |
| 769 | np->type = "<NULL>"; |
| 770 | } |
| 771 | while (tag == OF_DT_BEGIN_NODE) { |
| 772 | mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize); |
| 773 | tag = *((u32 *)(*p)); |
| 774 | } |
| 775 | if (tag != OF_DT_END_NODE) { |
| 776 | printk("Weird tag at end of node: %x\n", tag); |
| 777 | return mem; |
| 778 | } |
| 779 | *p += 4; |
| 780 | return mem; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | /** |
| 785 | * unflattens the device-tree passed by the firmware, creating the |
| 786 | * tree of struct device_node. It also fills the "name" and "type" |
| 787 | * pointers of the nodes so the normal device-tree walking functions |
| 788 | * can be used (this used to be done by finish_device_tree) |
| 789 | */ |
| 790 | void __init unflatten_device_tree(void) |
| 791 | { |
| 792 | unsigned long start, mem, size; |
| 793 | struct device_node **allnextp = &allnodes; |
| 794 | char *p = NULL; |
| 795 | int l = 0; |
| 796 | |
| 797 | DBG(" -> unflatten_device_tree()\n"); |
| 798 | |
| 799 | /* First pass, scan for size */ |
| 800 | start = ((unsigned long)initial_boot_params) + |
| 801 | initial_boot_params->off_dt_struct; |
| 802 | size = unflatten_dt_node(0, &start, NULL, NULL, 0); |
| 803 | size = (size | 3) + 1; |
| 804 | |
| 805 | DBG(" size is %lx, allocating...\n", size); |
| 806 | |
| 807 | /* Allocate memory for the expanded device tree */ |
| 808 | mem = lmb_alloc(size + 4, __alignof__(struct device_node)); |
| 809 | if (!mem) { |
| 810 | DBG("Couldn't allocate memory with lmb_alloc()!\n"); |
| 811 | panic("Couldn't allocate memory with lmb_alloc()!\n"); |
| 812 | } |
| 813 | mem = (unsigned long) __va(mem); |
| 814 | |
| 815 | ((u32 *)mem)[size / 4] = 0xdeadbeef; |
| 816 | |
| 817 | DBG(" unflattening %lx...\n", mem); |
| 818 | |
| 819 | /* Second pass, do actual unflattening */ |
| 820 | start = ((unsigned long)initial_boot_params) + |
| 821 | initial_boot_params->off_dt_struct; |
| 822 | unflatten_dt_node(mem, &start, NULL, &allnextp, 0); |
| 823 | if (*((u32 *)start) != OF_DT_END) |
| 824 | printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start)); |
| 825 | if (((u32 *)mem)[size / 4] != 0xdeadbeef) |
| 826 | printk(KERN_WARNING "End of tree marker overwritten: %08x\n", |
| 827 | ((u32 *)mem)[size / 4] ); |
| 828 | *allnextp = NULL; |
| 829 | |
| 830 | /* Get pointer to OF "/chosen" node for use everywhere */ |
| 831 | of_chosen = of_find_node_by_path("/chosen"); |
Paul Mackerras | a575b80 | 2005-10-23 17:23:21 +1000 | [diff] [blame] | 832 | if (of_chosen == NULL) |
| 833 | of_chosen = of_find_node_by_path("/chosen@0"); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 834 | |
| 835 | /* Retreive command line */ |
| 836 | if (of_chosen != NULL) { |
| 837 | p = (char *)get_property(of_chosen, "bootargs", &l); |
| 838 | if (p != NULL && l > 0) |
| 839 | strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE)); |
| 840 | } |
| 841 | #ifdef CONFIG_CMDLINE |
| 842 | if (l == 0 || (l == 1 && (*p) == 0)) |
| 843 | strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
| 844 | #endif /* CONFIG_CMDLINE */ |
| 845 | |
| 846 | DBG("Command line is: %s\n", cmd_line); |
| 847 | |
| 848 | DBG(" <- unflatten_device_tree()\n"); |
| 849 | } |
| 850 | |
| 851 | |
| 852 | static int __init early_init_dt_scan_cpus(unsigned long node, |
| 853 | const char *uname, int depth, void *data) |
| 854 | { |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 855 | u32 *prop; |
Stephen Rothwell | 676e249 | 2005-11-10 14:16:21 +1100 | [diff] [blame] | 856 | unsigned long size; |
| 857 | char *type = of_get_flat_dt_prop(node, "device_type", &size); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 858 | |
| 859 | /* We are scanning "cpu" nodes only */ |
| 860 | if (type == NULL || strcmp(type, "cpu") != 0) |
| 861 | return 0; |
| 862 | |
Paul Mackerras | 80579e1 | 2005-10-27 22:42:04 +1000 | [diff] [blame] | 863 | boot_cpuid = 0; |
| 864 | boot_cpuid_phys = 0; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 865 | if (initial_boot_params && initial_boot_params->version >= 2) { |
| 866 | /* version 2 of the kexec param format adds the phys cpuid |
| 867 | * of booted proc. |
| 868 | */ |
| 869 | boot_cpuid_phys = initial_boot_params->boot_cpuid_phys; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 870 | } else { |
Paul Mackerras | 80579e1 | 2005-10-27 22:42:04 +1000 | [diff] [blame] | 871 | /* Check if it's the boot-cpu, set it's hw index now */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 872 | if (of_get_flat_dt_prop(node, |
| 873 | "linux,boot-cpu", NULL) != NULL) { |
| 874 | prop = of_get_flat_dt_prop(node, "reg", NULL); |
Paul Mackerras | 80579e1 | 2005-10-27 22:42:04 +1000 | [diff] [blame] | 875 | if (prop != NULL) |
| 876 | boot_cpuid_phys = *prop; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 877 | } |
| 878 | } |
Paul Mackerras | 80579e1 | 2005-10-27 22:42:04 +1000 | [diff] [blame] | 879 | set_hard_smp_processor_id(0, boot_cpuid_phys); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 880 | |
| 881 | #ifdef CONFIG_ALTIVEC |
| 882 | /* Check if we have a VMX and eventually update CPU features */ |
Stephen Rothwell | 676e249 | 2005-11-10 14:16:21 +1100 | [diff] [blame] | 883 | prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 884 | if (prop && (*prop) > 0) { |
| 885 | cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; |
| 886 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; |
| 887 | } |
| 888 | |
| 889 | /* Same goes for Apple's "altivec" property */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 890 | prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 891 | if (prop) { |
| 892 | cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; |
| 893 | cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; |
| 894 | } |
| 895 | #endif /* CONFIG_ALTIVEC */ |
| 896 | |
| 897 | #ifdef CONFIG_PPC_PSERIES |
| 898 | /* |
| 899 | * Check for an SMT capable CPU and set the CPU feature. We do |
| 900 | * this by looking at the size of the ibm,ppc-interrupt-server#s |
| 901 | * property |
| 902 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 903 | prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 904 | &size); |
| 905 | cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT; |
| 906 | if (prop && ((size / sizeof(u32)) > 1)) |
| 907 | cur_cpu_spec->cpu_features |= CPU_FTR_SMT; |
| 908 | #endif |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | static int __init early_init_dt_scan_chosen(unsigned long node, |
| 914 | const char *uname, int depth, void *data) |
| 915 | { |
| 916 | u32 *prop; |
| 917 | unsigned long *lprop; |
| 918 | |
| 919 | DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname); |
| 920 | |
Paul Mackerras | a575b80 | 2005-10-23 17:23:21 +1000 | [diff] [blame] | 921 | if (depth != 1 || |
| 922 | (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 923 | return 0; |
| 924 | |
| 925 | /* get platform type */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 926 | prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 927 | if (prop == NULL) |
| 928 | return 0; |
Kumar Gala | 60dda25 | 2005-10-20 11:44:03 -0500 | [diff] [blame] | 929 | #ifdef CONFIG_PPC_MULTIPLATFORM |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 930 | _machine = *prop; |
| 931 | #endif |
| 932 | |
| 933 | #ifdef CONFIG_PPC64 |
| 934 | /* check if iommu is forced on or off */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 935 | if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 936 | iommu_is_off = 1; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 937 | if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 938 | iommu_force_on = 1; |
| 939 | #endif |
| 940 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 941 | lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 942 | if (lprop) |
| 943 | memory_limit = *lprop; |
| 944 | |
| 945 | #ifdef CONFIG_PPC64 |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 946 | lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 947 | if (lprop) |
| 948 | tce_alloc_start = *lprop; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 949 | lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 950 | if (lprop) |
| 951 | tce_alloc_end = *lprop; |
| 952 | #endif |
| 953 | |
| 954 | #ifdef CONFIG_PPC_RTAS |
| 955 | /* To help early debugging via the front panel, we retreive a minimal |
| 956 | * set of RTAS infos now if available |
| 957 | */ |
| 958 | { |
| 959 | u64 *basep, *entryp; |
| 960 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 961 | basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL); |
| 962 | entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL); |
| 963 | prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 964 | if (basep && entryp && prop) { |
| 965 | rtas.base = *basep; |
| 966 | rtas.entry = *entryp; |
| 967 | rtas.size = *prop; |
| 968 | } |
| 969 | } |
| 970 | #endif /* CONFIG_PPC_RTAS */ |
| 971 | |
Michael Ellerman | dcee303 | 2005-12-04 18:39:48 +1100 | [diff] [blame] | 972 | #ifdef CONFIG_KEXEC |
| 973 | lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL); |
| 974 | if (lprop) |
| 975 | crashk_res.start = *lprop; |
| 976 | |
| 977 | lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL); |
| 978 | if (lprop) |
| 979 | crashk_res.end = crashk_res.start + *lprop - 1; |
| 980 | #endif |
| 981 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 982 | /* break now */ |
| 983 | return 1; |
| 984 | } |
| 985 | |
| 986 | static int __init early_init_dt_scan_root(unsigned long node, |
| 987 | const char *uname, int depth, void *data) |
| 988 | { |
| 989 | u32 *prop; |
| 990 | |
| 991 | if (depth != 0) |
| 992 | return 0; |
| 993 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 994 | prop = of_get_flat_dt_prop(node, "#size-cells", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 995 | dt_root_size_cells = (prop == NULL) ? 1 : *prop; |
| 996 | DBG("dt_root_size_cells = %x\n", dt_root_size_cells); |
| 997 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 998 | prop = of_get_flat_dt_prop(node, "#address-cells", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 999 | dt_root_addr_cells = (prop == NULL) ? 2 : *prop; |
| 1000 | DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells); |
| 1001 | |
| 1002 | /* break now */ |
| 1003 | return 1; |
| 1004 | } |
| 1005 | |
| 1006 | static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp) |
| 1007 | { |
| 1008 | cell_t *p = *cellp; |
| 1009 | unsigned long r; |
| 1010 | |
| 1011 | /* Ignore more than 2 cells */ |
| 1012 | while (s > sizeof(unsigned long) / 4) { |
| 1013 | p++; |
| 1014 | s--; |
| 1015 | } |
| 1016 | r = *p++; |
| 1017 | #ifdef CONFIG_PPC64 |
| 1018 | if (s > 1) { |
| 1019 | r <<= 32; |
| 1020 | r |= *(p++); |
| 1021 | s--; |
| 1022 | } |
| 1023 | #endif |
| 1024 | |
| 1025 | *cellp = p; |
| 1026 | return r; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | static int __init early_init_dt_scan_memory(unsigned long node, |
| 1031 | const char *uname, int depth, void *data) |
| 1032 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1033 | char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1034 | cell_t *reg, *endp; |
| 1035 | unsigned long l; |
| 1036 | |
| 1037 | /* We are scanning "memory" nodes only */ |
Paul Mackerras | a23414b | 2005-11-10 12:00:55 +1100 | [diff] [blame] | 1038 | if (type == NULL) { |
| 1039 | /* |
| 1040 | * The longtrail doesn't have a device_type on the |
| 1041 | * /memory node, so look for the node called /memory@0. |
| 1042 | */ |
| 1043 | if (depth != 1 || strcmp(uname, "memory@0") != 0) |
| 1044 | return 0; |
| 1045 | } else if (strcmp(type, "memory") != 0) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1046 | return 0; |
| 1047 | |
Michael Ellerman | ba75948 | 2005-12-04 18:39:55 +1100 | [diff] [blame] | 1048 | reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l); |
| 1049 | if (reg == NULL) |
| 1050 | reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1051 | if (reg == NULL) |
| 1052 | return 0; |
| 1053 | |
| 1054 | endp = reg + (l / sizeof(cell_t)); |
| 1055 | |
Michael Ellerman | 358c86f | 2005-11-03 15:39:09 +1100 | [diff] [blame] | 1056 | DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n", |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1057 | uname, l, reg[0], reg[1], reg[2], reg[3]); |
| 1058 | |
| 1059 | while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { |
| 1060 | unsigned long base, size; |
| 1061 | |
| 1062 | base = dt_mem_next_cell(dt_root_addr_cells, ®); |
| 1063 | size = dt_mem_next_cell(dt_root_size_cells, ®); |
| 1064 | |
| 1065 | if (size == 0) |
| 1066 | continue; |
| 1067 | DBG(" - %lx , %lx\n", base, size); |
| 1068 | #ifdef CONFIG_PPC64 |
| 1069 | if (iommu_is_off) { |
| 1070 | if (base >= 0x80000000ul) |
| 1071 | continue; |
| 1072 | if ((base + size) > 0x80000000ul) |
| 1073 | size = 0x80000000ul - base; |
| 1074 | } |
| 1075 | #endif |
| 1076 | lmb_add(base, size); |
| 1077 | } |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | static void __init early_reserve_mem(void) |
| 1082 | { |
| 1083 | unsigned long base, size; |
| 1084 | unsigned long *reserve_map; |
| 1085 | |
| 1086 | reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) + |
| 1087 | initial_boot_params->off_mem_rsvmap); |
| 1088 | while (1) { |
| 1089 | base = *(reserve_map++); |
| 1090 | size = *(reserve_map++); |
| 1091 | if (size == 0) |
| 1092 | break; |
| 1093 | DBG("reserving: %lx -> %lx\n", base, size); |
| 1094 | lmb_reserve(base, size); |
| 1095 | } |
| 1096 | |
| 1097 | #if 0 |
| 1098 | DBG("memory reserved, lmbs :\n"); |
| 1099 | lmb_dump_all(); |
| 1100 | #endif |
| 1101 | } |
| 1102 | |
| 1103 | void __init early_init_devtree(void *params) |
| 1104 | { |
| 1105 | DBG(" -> early_init_devtree()\n"); |
| 1106 | |
| 1107 | /* Setup flat device-tree pointer */ |
| 1108 | initial_boot_params = params; |
| 1109 | |
| 1110 | /* Retrieve various informations from the /chosen node of the |
| 1111 | * device-tree, including the platform type, initrd location and |
| 1112 | * size, TCE reserve, and more ... |
| 1113 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1114 | of_scan_flat_dt(early_init_dt_scan_chosen, NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1115 | |
| 1116 | /* Scan memory nodes and rebuild LMBs */ |
| 1117 | lmb_init(); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1118 | of_scan_flat_dt(early_init_dt_scan_root, NULL); |
| 1119 | of_scan_flat_dt(early_init_dt_scan_memory, NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1120 | lmb_enforce_memory_limit(memory_limit); |
| 1121 | lmb_analyze(); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1122 | |
| 1123 | DBG("Phys. mem: %lx\n", lmb_phys_mem_size()); |
| 1124 | |
| 1125 | /* Reserve LMB regions used by kernel, initrd, dt, etc... */ |
Michael Ellerman | 0cc4746 | 2005-12-04 18:39:37 +1100 | [diff] [blame] | 1126 | lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START); |
| 1127 | #ifdef CONFIG_CRASH_DUMP |
| 1128 | lmb_reserve(0, KDUMP_RESERVE_LIMIT); |
| 1129 | #endif |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1130 | early_reserve_mem(); |
| 1131 | |
| 1132 | DBG("Scanning CPUs ...\n"); |
| 1133 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1134 | /* Retreive CPU related informations from the flat tree |
| 1135 | * (altivec support, boot CPU ID, ...) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1136 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 1137 | of_scan_flat_dt(early_init_dt_scan_cpus, NULL); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1138 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1139 | DBG(" <- early_init_devtree()\n"); |
| 1140 | } |
| 1141 | |
| 1142 | #undef printk |
| 1143 | |
| 1144 | int |
| 1145 | prom_n_addr_cells(struct device_node* np) |
| 1146 | { |
| 1147 | int* ip; |
| 1148 | do { |
| 1149 | if (np->parent) |
| 1150 | np = np->parent; |
| 1151 | ip = (int *) get_property(np, "#address-cells", NULL); |
| 1152 | if (ip != NULL) |
| 1153 | return *ip; |
| 1154 | } while (np->parent); |
| 1155 | /* No #address-cells property for the root node, default to 1 */ |
| 1156 | return 1; |
| 1157 | } |
Paul Mackerras | 1dfc677 | 2005-11-14 17:30:40 +1100 | [diff] [blame] | 1158 | EXPORT_SYMBOL(prom_n_addr_cells); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1159 | |
| 1160 | int |
| 1161 | prom_n_size_cells(struct device_node* np) |
| 1162 | { |
| 1163 | int* ip; |
| 1164 | do { |
| 1165 | if (np->parent) |
| 1166 | np = np->parent; |
| 1167 | ip = (int *) get_property(np, "#size-cells", NULL); |
| 1168 | if (ip != NULL) |
| 1169 | return *ip; |
| 1170 | } while (np->parent); |
| 1171 | /* No #size-cells property for the root node, default to 1 */ |
| 1172 | return 1; |
| 1173 | } |
Paul Mackerras | 1dfc677 | 2005-11-14 17:30:40 +1100 | [diff] [blame] | 1174 | EXPORT_SYMBOL(prom_n_size_cells); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1175 | |
| 1176 | /** |
| 1177 | * Work out the sense (active-low level / active-high edge) |
| 1178 | * of each interrupt from the device tree. |
| 1179 | */ |
| 1180 | void __init prom_get_irq_senses(unsigned char *senses, int off, int max) |
| 1181 | { |
| 1182 | struct device_node *np; |
| 1183 | int i, j; |
| 1184 | |
| 1185 | /* default to level-triggered */ |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 1186 | memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1187 | |
| 1188 | for (np = allnodes; np != 0; np = np->allnext) { |
| 1189 | for (j = 0; j < np->n_intrs; j++) { |
| 1190 | i = np->intrs[j].line; |
| 1191 | if (i >= off && i < max) |
Paul Mackerras | 6d0124f | 2005-10-26 17:19:06 +1000 | [diff] [blame] | 1192 | senses[i-off] = np->intrs[j].sense; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | /** |
| 1198 | * Construct and return a list of the device_nodes with a given name. |
| 1199 | */ |
| 1200 | struct device_node *find_devices(const char *name) |
| 1201 | { |
| 1202 | struct device_node *head, **prevp, *np; |
| 1203 | |
| 1204 | prevp = &head; |
| 1205 | for (np = allnodes; np != 0; np = np->allnext) { |
| 1206 | if (np->name != 0 && strcasecmp(np->name, name) == 0) { |
| 1207 | *prevp = np; |
| 1208 | prevp = &np->next; |
| 1209 | } |
| 1210 | } |
| 1211 | *prevp = NULL; |
| 1212 | return head; |
| 1213 | } |
| 1214 | EXPORT_SYMBOL(find_devices); |
| 1215 | |
| 1216 | /** |
| 1217 | * Construct and return a list of the device_nodes with a given type. |
| 1218 | */ |
| 1219 | struct device_node *find_type_devices(const char *type) |
| 1220 | { |
| 1221 | struct device_node *head, **prevp, *np; |
| 1222 | |
| 1223 | prevp = &head; |
| 1224 | for (np = allnodes; np != 0; np = np->allnext) { |
| 1225 | if (np->type != 0 && strcasecmp(np->type, type) == 0) { |
| 1226 | *prevp = np; |
| 1227 | prevp = &np->next; |
| 1228 | } |
| 1229 | } |
| 1230 | *prevp = NULL; |
| 1231 | return head; |
| 1232 | } |
| 1233 | EXPORT_SYMBOL(find_type_devices); |
| 1234 | |
| 1235 | /** |
| 1236 | * Returns all nodes linked together |
| 1237 | */ |
| 1238 | struct device_node *find_all_nodes(void) |
| 1239 | { |
| 1240 | struct device_node *head, **prevp, *np; |
| 1241 | |
| 1242 | prevp = &head; |
| 1243 | for (np = allnodes; np != 0; np = np->allnext) { |
| 1244 | *prevp = np; |
| 1245 | prevp = &np->next; |
| 1246 | } |
| 1247 | *prevp = NULL; |
| 1248 | return head; |
| 1249 | } |
| 1250 | EXPORT_SYMBOL(find_all_nodes); |
| 1251 | |
| 1252 | /** Checks if the given "compat" string matches one of the strings in |
| 1253 | * the device's "compatible" property |
| 1254 | */ |
| 1255 | int device_is_compatible(struct device_node *device, const char *compat) |
| 1256 | { |
| 1257 | const char* cp; |
| 1258 | int cplen, l; |
| 1259 | |
| 1260 | cp = (char *) get_property(device, "compatible", &cplen); |
| 1261 | if (cp == NULL) |
| 1262 | return 0; |
| 1263 | while (cplen > 0) { |
| 1264 | if (strncasecmp(cp, compat, strlen(compat)) == 0) |
| 1265 | return 1; |
| 1266 | l = strlen(cp) + 1; |
| 1267 | cp += l; |
| 1268 | cplen -= l; |
| 1269 | } |
| 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | EXPORT_SYMBOL(device_is_compatible); |
| 1274 | |
| 1275 | |
| 1276 | /** |
| 1277 | * Indicates whether the root node has a given value in its |
| 1278 | * compatible property. |
| 1279 | */ |
| 1280 | int machine_is_compatible(const char *compat) |
| 1281 | { |
| 1282 | struct device_node *root; |
| 1283 | int rc = 0; |
| 1284 | |
| 1285 | root = of_find_node_by_path("/"); |
| 1286 | if (root) { |
| 1287 | rc = device_is_compatible(root, compat); |
| 1288 | of_node_put(root); |
| 1289 | } |
| 1290 | return rc; |
| 1291 | } |
| 1292 | EXPORT_SYMBOL(machine_is_compatible); |
| 1293 | |
| 1294 | /** |
| 1295 | * Construct and return a list of the device_nodes with a given type |
| 1296 | * and compatible property. |
| 1297 | */ |
| 1298 | struct device_node *find_compatible_devices(const char *type, |
| 1299 | const char *compat) |
| 1300 | { |
| 1301 | struct device_node *head, **prevp, *np; |
| 1302 | |
| 1303 | prevp = &head; |
| 1304 | for (np = allnodes; np != 0; np = np->allnext) { |
| 1305 | if (type != NULL |
| 1306 | && !(np->type != 0 && strcasecmp(np->type, type) == 0)) |
| 1307 | continue; |
| 1308 | if (device_is_compatible(np, compat)) { |
| 1309 | *prevp = np; |
| 1310 | prevp = &np->next; |
| 1311 | } |
| 1312 | } |
| 1313 | *prevp = NULL; |
| 1314 | return head; |
| 1315 | } |
| 1316 | EXPORT_SYMBOL(find_compatible_devices); |
| 1317 | |
| 1318 | /** |
| 1319 | * Find the device_node with a given full_name. |
| 1320 | */ |
| 1321 | struct device_node *find_path_device(const char *path) |
| 1322 | { |
| 1323 | struct device_node *np; |
| 1324 | |
| 1325 | for (np = allnodes; np != 0; np = np->allnext) |
| 1326 | if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) |
| 1327 | return np; |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | EXPORT_SYMBOL(find_path_device); |
| 1331 | |
| 1332 | /******* |
| 1333 | * |
| 1334 | * New implementation of the OF "find" APIs, return a refcounted |
| 1335 | * object, call of_node_put() when done. The device tree and list |
| 1336 | * are protected by a rw_lock. |
| 1337 | * |
| 1338 | * Note that property management will need some locking as well, |
| 1339 | * this isn't dealt with yet. |
| 1340 | * |
| 1341 | *******/ |
| 1342 | |
| 1343 | /** |
| 1344 | * of_find_node_by_name - Find a node by its "name" property |
| 1345 | * @from: The node to start searching from or NULL, the node |
| 1346 | * you pass will not be searched, only the next one |
| 1347 | * will; typically, you pass what the previous call |
| 1348 | * returned. of_node_put() will be called on it |
| 1349 | * @name: The name string to match against |
| 1350 | * |
| 1351 | * Returns a node pointer with refcount incremented, use |
| 1352 | * of_node_put() on it when done. |
| 1353 | */ |
| 1354 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 1355 | const char *name) |
| 1356 | { |
| 1357 | struct device_node *np; |
| 1358 | |
| 1359 | read_lock(&devtree_lock); |
| 1360 | np = from ? from->allnext : allnodes; |
| 1361 | for (; np != 0; np = np->allnext) |
| 1362 | if (np->name != 0 && strcasecmp(np->name, name) == 0 |
| 1363 | && of_node_get(np)) |
| 1364 | break; |
| 1365 | if (from) |
| 1366 | of_node_put(from); |
| 1367 | read_unlock(&devtree_lock); |
| 1368 | return np; |
| 1369 | } |
| 1370 | EXPORT_SYMBOL(of_find_node_by_name); |
| 1371 | |
| 1372 | /** |
| 1373 | * of_find_node_by_type - Find a node by its "device_type" property |
| 1374 | * @from: The node to start searching from or NULL, the node |
| 1375 | * you pass will not be searched, only the next one |
| 1376 | * will; typically, you pass what the previous call |
| 1377 | * returned. of_node_put() will be called on it |
| 1378 | * @name: The type string to match against |
| 1379 | * |
| 1380 | * Returns a node pointer with refcount incremented, use |
| 1381 | * of_node_put() on it when done. |
| 1382 | */ |
| 1383 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 1384 | const char *type) |
| 1385 | { |
| 1386 | struct device_node *np; |
| 1387 | |
| 1388 | read_lock(&devtree_lock); |
| 1389 | np = from ? from->allnext : allnodes; |
| 1390 | for (; np != 0; np = np->allnext) |
| 1391 | if (np->type != 0 && strcasecmp(np->type, type) == 0 |
| 1392 | && of_node_get(np)) |
| 1393 | break; |
| 1394 | if (from) |
| 1395 | of_node_put(from); |
| 1396 | read_unlock(&devtree_lock); |
| 1397 | return np; |
| 1398 | } |
| 1399 | EXPORT_SYMBOL(of_find_node_by_type); |
| 1400 | |
| 1401 | /** |
| 1402 | * of_find_compatible_node - Find a node based on type and one of the |
| 1403 | * tokens in its "compatible" property |
| 1404 | * @from: The node to start searching from or NULL, the node |
| 1405 | * you pass will not be searched, only the next one |
| 1406 | * will; typically, you pass what the previous call |
| 1407 | * returned. of_node_put() will be called on it |
| 1408 | * @type: The type string to match "device_type" or NULL to ignore |
| 1409 | * @compatible: The string to match to one of the tokens in the device |
| 1410 | * "compatible" list. |
| 1411 | * |
| 1412 | * Returns a node pointer with refcount incremented, use |
| 1413 | * of_node_put() on it when done. |
| 1414 | */ |
| 1415 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 1416 | const char *type, const char *compatible) |
| 1417 | { |
| 1418 | struct device_node *np; |
| 1419 | |
| 1420 | read_lock(&devtree_lock); |
| 1421 | np = from ? from->allnext : allnodes; |
| 1422 | for (; np != 0; np = np->allnext) { |
| 1423 | if (type != NULL |
| 1424 | && !(np->type != 0 && strcasecmp(np->type, type) == 0)) |
| 1425 | continue; |
| 1426 | if (device_is_compatible(np, compatible) && of_node_get(np)) |
| 1427 | break; |
| 1428 | } |
| 1429 | if (from) |
| 1430 | of_node_put(from); |
| 1431 | read_unlock(&devtree_lock); |
| 1432 | return np; |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(of_find_compatible_node); |
| 1435 | |
| 1436 | /** |
| 1437 | * of_find_node_by_path - Find a node matching a full OF path |
| 1438 | * @path: The full path to match |
| 1439 | * |
| 1440 | * Returns a node pointer with refcount incremented, use |
| 1441 | * of_node_put() on it when done. |
| 1442 | */ |
| 1443 | struct device_node *of_find_node_by_path(const char *path) |
| 1444 | { |
| 1445 | struct device_node *np = allnodes; |
| 1446 | |
| 1447 | read_lock(&devtree_lock); |
| 1448 | for (; np != 0; np = np->allnext) { |
| 1449 | if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0 |
| 1450 | && of_node_get(np)) |
| 1451 | break; |
| 1452 | } |
| 1453 | read_unlock(&devtree_lock); |
| 1454 | return np; |
| 1455 | } |
| 1456 | EXPORT_SYMBOL(of_find_node_by_path); |
| 1457 | |
| 1458 | /** |
| 1459 | * of_find_node_by_phandle - Find a node given a phandle |
| 1460 | * @handle: phandle of the node to find |
| 1461 | * |
| 1462 | * Returns a node pointer with refcount incremented, use |
| 1463 | * of_node_put() on it when done. |
| 1464 | */ |
| 1465 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 1466 | { |
| 1467 | struct device_node *np; |
| 1468 | |
| 1469 | read_lock(&devtree_lock); |
| 1470 | for (np = allnodes; np != 0; np = np->allnext) |
| 1471 | if (np->linux_phandle == handle) |
| 1472 | break; |
| 1473 | if (np) |
| 1474 | of_node_get(np); |
| 1475 | read_unlock(&devtree_lock); |
| 1476 | return np; |
| 1477 | } |
| 1478 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 1479 | |
| 1480 | /** |
| 1481 | * of_find_all_nodes - Get next node in global list |
| 1482 | * @prev: Previous node or NULL to start iteration |
| 1483 | * of_node_put() will be called on it |
| 1484 | * |
| 1485 | * Returns a node pointer with refcount incremented, use |
| 1486 | * of_node_put() on it when done. |
| 1487 | */ |
| 1488 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 1489 | { |
| 1490 | struct device_node *np; |
| 1491 | |
| 1492 | read_lock(&devtree_lock); |
| 1493 | np = prev ? prev->allnext : allnodes; |
| 1494 | for (; np != 0; np = np->allnext) |
| 1495 | if (of_node_get(np)) |
| 1496 | break; |
| 1497 | if (prev) |
| 1498 | of_node_put(prev); |
| 1499 | read_unlock(&devtree_lock); |
| 1500 | return np; |
| 1501 | } |
| 1502 | EXPORT_SYMBOL(of_find_all_nodes); |
| 1503 | |
| 1504 | /** |
| 1505 | * of_get_parent - Get a node's parent if any |
| 1506 | * @node: Node to get parent |
| 1507 | * |
| 1508 | * Returns a node pointer with refcount incremented, use |
| 1509 | * of_node_put() on it when done. |
| 1510 | */ |
| 1511 | struct device_node *of_get_parent(const struct device_node *node) |
| 1512 | { |
| 1513 | struct device_node *np; |
| 1514 | |
| 1515 | if (!node) |
| 1516 | return NULL; |
| 1517 | |
| 1518 | read_lock(&devtree_lock); |
| 1519 | np = of_node_get(node->parent); |
| 1520 | read_unlock(&devtree_lock); |
| 1521 | return np; |
| 1522 | } |
| 1523 | EXPORT_SYMBOL(of_get_parent); |
| 1524 | |
| 1525 | /** |
| 1526 | * of_get_next_child - Iterate a node childs |
| 1527 | * @node: parent node |
| 1528 | * @prev: previous child of the parent node, or NULL to get first |
| 1529 | * |
| 1530 | * Returns a node pointer with refcount incremented, use |
| 1531 | * of_node_put() on it when done. |
| 1532 | */ |
| 1533 | struct device_node *of_get_next_child(const struct device_node *node, |
| 1534 | struct device_node *prev) |
| 1535 | { |
| 1536 | struct device_node *next; |
| 1537 | |
| 1538 | read_lock(&devtree_lock); |
| 1539 | next = prev ? prev->sibling : node->child; |
| 1540 | for (; next != 0; next = next->sibling) |
| 1541 | if (of_node_get(next)) |
| 1542 | break; |
| 1543 | if (prev) |
| 1544 | of_node_put(prev); |
| 1545 | read_unlock(&devtree_lock); |
| 1546 | return next; |
| 1547 | } |
| 1548 | EXPORT_SYMBOL(of_get_next_child); |
| 1549 | |
| 1550 | /** |
| 1551 | * of_node_get - Increment refcount of a node |
| 1552 | * @node: Node to inc refcount, NULL is supported to |
| 1553 | * simplify writing of callers |
| 1554 | * |
| 1555 | * Returns node. |
| 1556 | */ |
| 1557 | struct device_node *of_node_get(struct device_node *node) |
| 1558 | { |
| 1559 | if (node) |
| 1560 | kref_get(&node->kref); |
| 1561 | return node; |
| 1562 | } |
| 1563 | EXPORT_SYMBOL(of_node_get); |
| 1564 | |
| 1565 | static inline struct device_node * kref_to_device_node(struct kref *kref) |
| 1566 | { |
| 1567 | return container_of(kref, struct device_node, kref); |
| 1568 | } |
| 1569 | |
| 1570 | /** |
| 1571 | * of_node_release - release a dynamically allocated node |
| 1572 | * @kref: kref element of the node to be released |
| 1573 | * |
| 1574 | * In of_node_put() this function is passed to kref_put() |
| 1575 | * as the destructor. |
| 1576 | */ |
| 1577 | static void of_node_release(struct kref *kref) |
| 1578 | { |
| 1579 | struct device_node *node = kref_to_device_node(kref); |
| 1580 | struct property *prop = node->properties; |
| 1581 | |
| 1582 | if (!OF_IS_DYNAMIC(node)) |
| 1583 | return; |
| 1584 | while (prop) { |
| 1585 | struct property *next = prop->next; |
| 1586 | kfree(prop->name); |
| 1587 | kfree(prop->value); |
| 1588 | kfree(prop); |
| 1589 | prop = next; |
| 1590 | } |
| 1591 | kfree(node->intrs); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1592 | kfree(node->full_name); |
| 1593 | kfree(node->data); |
| 1594 | kfree(node); |
| 1595 | } |
| 1596 | |
| 1597 | /** |
| 1598 | * of_node_put - Decrement refcount of a node |
| 1599 | * @node: Node to dec refcount, NULL is supported to |
| 1600 | * simplify writing of callers |
| 1601 | * |
| 1602 | */ |
| 1603 | void of_node_put(struct device_node *node) |
| 1604 | { |
| 1605 | if (node) |
| 1606 | kref_put(&node->kref, of_node_release); |
| 1607 | } |
| 1608 | EXPORT_SYMBOL(of_node_put); |
| 1609 | |
| 1610 | /* |
| 1611 | * Plug a device node into the tree and global list. |
| 1612 | */ |
| 1613 | void of_attach_node(struct device_node *np) |
| 1614 | { |
| 1615 | write_lock(&devtree_lock); |
| 1616 | np->sibling = np->parent->child; |
| 1617 | np->allnext = allnodes; |
| 1618 | np->parent->child = np; |
| 1619 | allnodes = np; |
| 1620 | write_unlock(&devtree_lock); |
| 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * "Unplug" a node from the device tree. The caller must hold |
| 1625 | * a reference to the node. The memory associated with the node |
| 1626 | * is not freed until its refcount goes to zero. |
| 1627 | */ |
| 1628 | void of_detach_node(const struct device_node *np) |
| 1629 | { |
| 1630 | struct device_node *parent; |
| 1631 | |
| 1632 | write_lock(&devtree_lock); |
| 1633 | |
| 1634 | parent = np->parent; |
| 1635 | |
| 1636 | if (allnodes == np) |
| 1637 | allnodes = np->allnext; |
| 1638 | else { |
| 1639 | struct device_node *prev; |
| 1640 | for (prev = allnodes; |
| 1641 | prev->allnext != np; |
| 1642 | prev = prev->allnext) |
| 1643 | ; |
| 1644 | prev->allnext = np->allnext; |
| 1645 | } |
| 1646 | |
| 1647 | if (parent->child == np) |
| 1648 | parent->child = np->sibling; |
| 1649 | else { |
| 1650 | struct device_node *prevsib; |
| 1651 | for (prevsib = np->parent->child; |
| 1652 | prevsib->sibling != np; |
| 1653 | prevsib = prevsib->sibling) |
| 1654 | ; |
| 1655 | prevsib->sibling = np->sibling; |
| 1656 | } |
| 1657 | |
| 1658 | write_unlock(&devtree_lock); |
| 1659 | } |
| 1660 | |
| 1661 | #ifdef CONFIG_PPC_PSERIES |
| 1662 | /* |
| 1663 | * Fix up the uninitialized fields in a new device node: |
| 1664 | * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields |
| 1665 | * |
| 1666 | * A lot of boot-time code is duplicated here, because functions such |
| 1667 | * as finish_node_interrupts, interpret_pci_props, etc. cannot use the |
| 1668 | * slab allocator. |
| 1669 | * |
| 1670 | * This should probably be split up into smaller chunks. |
| 1671 | */ |
| 1672 | |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 1673 | static int of_finish_dynamic_node(struct device_node *node) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1674 | { |
| 1675 | struct device_node *parent = of_get_parent(node); |
| 1676 | int err = 0; |
| 1677 | phandle *ibm_phandle; |
| 1678 | |
| 1679 | node->name = get_property(node, "name", NULL); |
| 1680 | node->type = get_property(node, "device_type", NULL); |
| 1681 | |
| 1682 | if (!parent) { |
| 1683 | err = -ENODEV; |
| 1684 | goto out; |
| 1685 | } |
| 1686 | |
| 1687 | /* We don't support that function on PowerMac, at least |
| 1688 | * not yet |
| 1689 | */ |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 1690 | if (_machine == PLATFORM_POWERMAC) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1691 | return -ENODEV; |
| 1692 | |
| 1693 | /* fix up new node's linux_phandle field */ |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 1694 | if ((ibm_phandle = (unsigned int *)get_property(node, |
| 1695 | "ibm,phandle", NULL))) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1696 | node->linux_phandle = *ibm_phandle; |
| 1697 | |
| 1698 | out: |
| 1699 | of_node_put(parent); |
| 1700 | return err; |
| 1701 | } |
| 1702 | |
| 1703 | static int prom_reconfig_notifier(struct notifier_block *nb, |
| 1704 | unsigned long action, void *node) |
| 1705 | { |
| 1706 | int err; |
| 1707 | |
| 1708 | switch (action) { |
| 1709 | case PSERIES_RECONFIG_ADD: |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame^] | 1710 | err = of_finish_dynamic_node(node); |
| 1711 | if (!err) |
| 1712 | finish_node(node, NULL, 0); |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1713 | if (err < 0) { |
| 1714 | printk(KERN_ERR "finish_node returned %d\n", err); |
| 1715 | err = NOTIFY_BAD; |
| 1716 | } |
| 1717 | break; |
| 1718 | default: |
| 1719 | err = NOTIFY_DONE; |
| 1720 | break; |
| 1721 | } |
| 1722 | return err; |
| 1723 | } |
| 1724 | |
| 1725 | static struct notifier_block prom_reconfig_nb = { |
| 1726 | .notifier_call = prom_reconfig_notifier, |
| 1727 | .priority = 10, /* This one needs to run first */ |
| 1728 | }; |
| 1729 | |
| 1730 | static int __init prom_reconfig_setup(void) |
| 1731 | { |
| 1732 | return pSeries_reconfig_notifier_register(&prom_reconfig_nb); |
| 1733 | } |
| 1734 | __initcall(prom_reconfig_setup); |
| 1735 | #endif |
| 1736 | |
| 1737 | /* |
| 1738 | * Find a property with a given name for a given node |
| 1739 | * and return the value. |
| 1740 | */ |
| 1741 | unsigned char *get_property(struct device_node *np, const char *name, |
| 1742 | int *lenp) |
| 1743 | { |
| 1744 | struct property *pp; |
| 1745 | |
| 1746 | for (pp = np->properties; pp != 0; pp = pp->next) |
| 1747 | if (strcmp(pp->name, name) == 0) { |
| 1748 | if (lenp != 0) |
| 1749 | *lenp = pp->length; |
| 1750 | return pp->value; |
| 1751 | } |
| 1752 | return NULL; |
| 1753 | } |
| 1754 | EXPORT_SYMBOL(get_property); |
| 1755 | |
| 1756 | /* |
| 1757 | * Add a property to a node |
| 1758 | */ |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1759 | int prom_add_property(struct device_node* np, struct property* prop) |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1760 | { |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1761 | struct property **next; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1762 | |
| 1763 | prop->next = NULL; |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1764 | write_lock(&devtree_lock); |
| 1765 | next = &np->properties; |
| 1766 | while (*next) { |
| 1767 | if (strcmp(prop->name, (*next)->name) == 0) { |
| 1768 | /* duplicate ! don't insert it */ |
| 1769 | write_unlock(&devtree_lock); |
| 1770 | return -1; |
| 1771 | } |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1772 | next = &(*next)->next; |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1773 | } |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1774 | *next = prop; |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1775 | write_unlock(&devtree_lock); |
| 1776 | |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 1777 | #ifdef CONFIG_PROC_DEVICETREE |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1778 | /* try to add to proc as well if it was initialized */ |
| 1779 | if (np->pde) |
| 1780 | proc_device_tree_add_prop(np->pde, prop); |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 1781 | #endif /* CONFIG_PROC_DEVICETREE */ |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1782 | |
| 1783 | return 0; |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1784 | } |
| 1785 | |
Paul Mackerras | 9b6b563 | 2005-10-06 12:06:20 +1000 | [diff] [blame] | 1786 | |