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