xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision 473104134b35ce1c3ca77b738c561d6c215adc1b)
19b6b563cSPaul Mackerras /*
29b6b563cSPaul Mackerras  * Procedures for creating, accessing and interpreting the device tree.
39b6b563cSPaul Mackerras  *
49b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
59b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
69b6b563cSPaul Mackerras  *
79b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
89b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
99b6b563cSPaul Mackerras  *
109b6b563cSPaul Mackerras  *      This program is free software; you can redistribute it and/or
119b6b563cSPaul Mackerras  *      modify it under the terms of the GNU General Public License
129b6b563cSPaul Mackerras  *      as published by the Free Software Foundation; either version
139b6b563cSPaul Mackerras  *      2 of the License, or (at your option) any later version.
149b6b563cSPaul Mackerras  */
159b6b563cSPaul Mackerras 
169b6b563cSPaul Mackerras #undef DEBUG
179b6b563cSPaul Mackerras 
189b6b563cSPaul Mackerras #include <stdarg.h>
199b6b563cSPaul Mackerras #include <linux/config.h>
209b6b563cSPaul Mackerras #include <linux/kernel.h>
219b6b563cSPaul Mackerras #include <linux/string.h>
229b6b563cSPaul Mackerras #include <linux/init.h>
239b6b563cSPaul Mackerras #include <linux/threads.h>
249b6b563cSPaul Mackerras #include <linux/spinlock.h>
259b6b563cSPaul Mackerras #include <linux/types.h>
269b6b563cSPaul Mackerras #include <linux/pci.h>
279b6b563cSPaul Mackerras #include <linux/stringify.h>
289b6b563cSPaul Mackerras #include <linux/delay.h>
299b6b563cSPaul Mackerras #include <linux/initrd.h>
309b6b563cSPaul Mackerras #include <linux/bitops.h>
319b6b563cSPaul Mackerras #include <linux/module.h>
32dcee3036SMichael Ellerman #include <linux/kexec.h>
339b6b563cSPaul Mackerras 
349b6b563cSPaul Mackerras #include <asm/prom.h>
359b6b563cSPaul Mackerras #include <asm/rtas.h>
369b6b563cSPaul Mackerras #include <asm/lmb.h>
379b6b563cSPaul Mackerras #include <asm/page.h>
389b6b563cSPaul Mackerras #include <asm/processor.h>
399b6b563cSPaul Mackerras #include <asm/irq.h>
409b6b563cSPaul Mackerras #include <asm/io.h>
410cc4746cSMichael Ellerman #include <asm/kdump.h>
429b6b563cSPaul Mackerras #include <asm/smp.h>
439b6b563cSPaul Mackerras #include <asm/system.h>
449b6b563cSPaul Mackerras #include <asm/mmu.h>
459b6b563cSPaul Mackerras #include <asm/pgtable.h>
469b6b563cSPaul Mackerras #include <asm/pci.h>
479b6b563cSPaul Mackerras #include <asm/iommu.h>
489b6b563cSPaul Mackerras #include <asm/btext.h>
499b6b563cSPaul Mackerras #include <asm/sections.h>
509b6b563cSPaul Mackerras #include <asm/machdep.h>
519b6b563cSPaul Mackerras #include <asm/pSeries_reconfig.h>
5240ef8cbcSPaul Mackerras #include <asm/pci-bridge.h>
532babf5c2SMichael Ellerman #include <asm/kexec.h>
549b6b563cSPaul Mackerras 
559b6b563cSPaul Mackerras #ifdef DEBUG
569b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt)
579b6b563cSPaul Mackerras #else
589b6b563cSPaul Mackerras #define DBG(fmt...)
599b6b563cSPaul Mackerras #endif
609b6b563cSPaul Mackerras 
619b6b563cSPaul Mackerras 
629b6b563cSPaul Mackerras static int __initdata dt_root_addr_cells;
639b6b563cSPaul Mackerras static int __initdata dt_root_size_cells;
649b6b563cSPaul Mackerras 
659b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6628897731SOlof Johansson int __initdata iommu_is_off;
679b6b563cSPaul Mackerras int __initdata iommu_force_on;
68cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
699b6b563cSPaul Mackerras #endif
709b6b563cSPaul Mackerras 
719b6b563cSPaul Mackerras typedef u32 cell_t;
729b6b563cSPaul Mackerras 
739b6b563cSPaul Mackerras #if 0
749b6b563cSPaul Mackerras static struct boot_param_header *initial_boot_params __initdata;
759b6b563cSPaul Mackerras #else
769b6b563cSPaul Mackerras struct boot_param_header *initial_boot_params;
779b6b563cSPaul Mackerras #endif
789b6b563cSPaul Mackerras 
799b6b563cSPaul Mackerras static struct device_node *allnodes = NULL;
809b6b563cSPaul Mackerras 
819b6b563cSPaul Mackerras /* use when traversing tree through the allnext, child, sibling,
829b6b563cSPaul Mackerras  * or parent members of struct device_node.
839b6b563cSPaul Mackerras  */
849b6b563cSPaul Mackerras static DEFINE_RWLOCK(devtree_lock);
859b6b563cSPaul Mackerras 
869b6b563cSPaul Mackerras /* export that to outside world */
879b6b563cSPaul Mackerras struct device_node *of_chosen;
889b6b563cSPaul Mackerras 
899b6b563cSPaul Mackerras struct device_node *dflt_interrupt_controller;
909b6b563cSPaul Mackerras int num_interrupt_controllers;
919b6b563cSPaul Mackerras 
929b6b563cSPaul Mackerras /*
939b6b563cSPaul Mackerras  * Wrapper for allocating memory for various data that needs to be
949b6b563cSPaul Mackerras  * attached to device nodes as they are processed at boot or when
959b6b563cSPaul Mackerras  * added to the device tree later (e.g. DLPAR).  At boot there is
969b6b563cSPaul Mackerras  * already a region reserved so we just increment *mem_start by size;
979b6b563cSPaul Mackerras  * otherwise we call kmalloc.
989b6b563cSPaul Mackerras  */
999b6b563cSPaul Mackerras static void * prom_alloc(unsigned long size, unsigned long *mem_start)
1009b6b563cSPaul Mackerras {
1019b6b563cSPaul Mackerras 	unsigned long tmp;
1029b6b563cSPaul Mackerras 
1039b6b563cSPaul Mackerras 	if (!mem_start)
1049b6b563cSPaul Mackerras 		return kmalloc(size, GFP_KERNEL);
1059b6b563cSPaul Mackerras 
1069b6b563cSPaul Mackerras 	tmp = *mem_start;
1079b6b563cSPaul Mackerras 	*mem_start += size;
1089b6b563cSPaul Mackerras 	return (void *)tmp;
1099b6b563cSPaul Mackerras }
1109b6b563cSPaul Mackerras 
1119b6b563cSPaul Mackerras /*
1129b6b563cSPaul Mackerras  * Find the device_node with a given phandle.
1139b6b563cSPaul Mackerras  */
1149b6b563cSPaul Mackerras static struct device_node * find_phandle(phandle ph)
1159b6b563cSPaul Mackerras {
1169b6b563cSPaul Mackerras 	struct device_node *np;
1179b6b563cSPaul Mackerras 
1189b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext)
1199b6b563cSPaul Mackerras 		if (np->linux_phandle == ph)
1209b6b563cSPaul Mackerras 			return np;
1219b6b563cSPaul Mackerras 	return NULL;
1229b6b563cSPaul Mackerras }
1239b6b563cSPaul Mackerras 
1249b6b563cSPaul Mackerras /*
1259b6b563cSPaul Mackerras  * Find the interrupt parent of a node.
1269b6b563cSPaul Mackerras  */
1279b6b563cSPaul Mackerras static struct device_node * __devinit intr_parent(struct device_node *p)
1289b6b563cSPaul Mackerras {
1299b6b563cSPaul Mackerras 	phandle *parp;
1309b6b563cSPaul Mackerras 
1319b6b563cSPaul Mackerras 	parp = (phandle *) get_property(p, "interrupt-parent", NULL);
1329b6b563cSPaul Mackerras 	if (parp == NULL)
1339b6b563cSPaul Mackerras 		return p->parent;
1349b6b563cSPaul Mackerras 	p = find_phandle(*parp);
1359b6b563cSPaul Mackerras 	if (p != NULL)
1369b6b563cSPaul Mackerras 		return p;
1379b6b563cSPaul Mackerras 	/*
1389b6b563cSPaul Mackerras 	 * On a powermac booted with BootX, we don't get to know the
1399b6b563cSPaul Mackerras 	 * phandles for any nodes, so find_phandle will return NULL.
1409b6b563cSPaul Mackerras 	 * Fortunately these machines only have one interrupt controller
1419b6b563cSPaul Mackerras 	 * so there isn't in fact any ambiguity.  -- paulus
1429b6b563cSPaul Mackerras 	 */
1439b6b563cSPaul Mackerras 	if (num_interrupt_controllers == 1)
1449b6b563cSPaul Mackerras 		p = dflt_interrupt_controller;
1459b6b563cSPaul Mackerras 	return p;
1469b6b563cSPaul Mackerras }
1479b6b563cSPaul Mackerras 
1489b6b563cSPaul Mackerras /*
1499b6b563cSPaul Mackerras  * Find out the size of each entry of the interrupts property
1509b6b563cSPaul Mackerras  * for a node.
1519b6b563cSPaul Mackerras  */
1529b6b563cSPaul Mackerras int __devinit prom_n_intr_cells(struct device_node *np)
1539b6b563cSPaul Mackerras {
1549b6b563cSPaul Mackerras 	struct device_node *p;
1559b6b563cSPaul Mackerras 	unsigned int *icp;
1569b6b563cSPaul Mackerras 
1579b6b563cSPaul Mackerras 	for (p = np; (p = intr_parent(p)) != NULL; ) {
1589b6b563cSPaul Mackerras 		icp = (unsigned int *)
1599b6b563cSPaul Mackerras 			get_property(p, "#interrupt-cells", NULL);
1609b6b563cSPaul Mackerras 		if (icp != NULL)
1619b6b563cSPaul Mackerras 			return *icp;
1629b6b563cSPaul Mackerras 		if (get_property(p, "interrupt-controller", NULL) != NULL
1639b6b563cSPaul Mackerras 		    || get_property(p, "interrupt-map", NULL) != NULL) {
1649b6b563cSPaul Mackerras 			printk("oops, node %s doesn't have #interrupt-cells\n",
1659b6b563cSPaul Mackerras 			       p->full_name);
1669b6b563cSPaul Mackerras 			return 1;
1679b6b563cSPaul Mackerras 		}
1689b6b563cSPaul Mackerras 	}
1699b6b563cSPaul Mackerras #ifdef DEBUG_IRQ
1709b6b563cSPaul Mackerras 	printk("prom_n_intr_cells failed for %s\n", np->full_name);
1719b6b563cSPaul Mackerras #endif
1729b6b563cSPaul Mackerras 	return 1;
1739b6b563cSPaul Mackerras }
1749b6b563cSPaul Mackerras 
1759b6b563cSPaul Mackerras /*
1769b6b563cSPaul Mackerras  * Map an interrupt from a device up to the platform interrupt
1779b6b563cSPaul Mackerras  * descriptor.
1789b6b563cSPaul Mackerras  */
1799b6b563cSPaul Mackerras static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
1809b6b563cSPaul Mackerras 				   struct device_node *np, unsigned int *ints,
1819b6b563cSPaul Mackerras 				   int nintrc)
1829b6b563cSPaul Mackerras {
1839b6b563cSPaul Mackerras 	struct device_node *p, *ipar;
1849b6b563cSPaul Mackerras 	unsigned int *imap, *imask, *ip;
1859b6b563cSPaul Mackerras 	int i, imaplen, match;
1869b6b563cSPaul Mackerras 	int newintrc = 0, newaddrc = 0;
1879b6b563cSPaul Mackerras 	unsigned int *reg;
1889b6b563cSPaul Mackerras 	int naddrc;
1899b6b563cSPaul Mackerras 
1909b6b563cSPaul Mackerras 	reg = (unsigned int *) get_property(np, "reg", NULL);
1919b6b563cSPaul Mackerras 	naddrc = prom_n_addr_cells(np);
1929b6b563cSPaul Mackerras 	p = intr_parent(np);
1939b6b563cSPaul Mackerras 	while (p != NULL) {
1949b6b563cSPaul Mackerras 		if (get_property(p, "interrupt-controller", NULL) != NULL)
1959b6b563cSPaul Mackerras 			/* this node is an interrupt controller, stop here */
1969b6b563cSPaul Mackerras 			break;
1979b6b563cSPaul Mackerras 		imap = (unsigned int *)
1989b6b563cSPaul Mackerras 			get_property(p, "interrupt-map", &imaplen);
1999b6b563cSPaul Mackerras 		if (imap == NULL) {
2009b6b563cSPaul Mackerras 			p = intr_parent(p);
2019b6b563cSPaul Mackerras 			continue;
2029b6b563cSPaul Mackerras 		}
2039b6b563cSPaul Mackerras 		imask = (unsigned int *)
2049b6b563cSPaul Mackerras 			get_property(p, "interrupt-map-mask", NULL);
2059b6b563cSPaul Mackerras 		if (imask == NULL) {
2069b6b563cSPaul Mackerras 			printk("oops, %s has interrupt-map but no mask\n",
2079b6b563cSPaul Mackerras 			       p->full_name);
2089b6b563cSPaul Mackerras 			return 0;
2099b6b563cSPaul Mackerras 		}
2109b6b563cSPaul Mackerras 		imaplen /= sizeof(unsigned int);
2119b6b563cSPaul Mackerras 		match = 0;
2129b6b563cSPaul Mackerras 		ipar = NULL;
2139b6b563cSPaul Mackerras 		while (imaplen > 0 && !match) {
2149b6b563cSPaul Mackerras 			/* check the child-interrupt field */
2159b6b563cSPaul Mackerras 			match = 1;
2169b6b563cSPaul Mackerras 			for (i = 0; i < naddrc && match; ++i)
2179b6b563cSPaul Mackerras 				match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
2189b6b563cSPaul Mackerras 			for (; i < naddrc + nintrc && match; ++i)
2199b6b563cSPaul Mackerras 				match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
2209b6b563cSPaul Mackerras 			imap += naddrc + nintrc;
2219b6b563cSPaul Mackerras 			imaplen -= naddrc + nintrc;
2229b6b563cSPaul Mackerras 			/* grab the interrupt parent */
2239b6b563cSPaul Mackerras 			ipar = find_phandle((phandle) *imap++);
2249b6b563cSPaul Mackerras 			--imaplen;
2259b6b563cSPaul Mackerras 			if (ipar == NULL && num_interrupt_controllers == 1)
2269b6b563cSPaul Mackerras 				/* cope with BootX not giving us phandles */
2279b6b563cSPaul Mackerras 				ipar = dflt_interrupt_controller;
2289b6b563cSPaul Mackerras 			if (ipar == NULL) {
2299b6b563cSPaul Mackerras 				printk("oops, no int parent %x in map of %s\n",
2309b6b563cSPaul Mackerras 				       imap[-1], p->full_name);
2319b6b563cSPaul Mackerras 				return 0;
2329b6b563cSPaul Mackerras 			}
2339b6b563cSPaul Mackerras 			/* find the parent's # addr and intr cells */
2349b6b563cSPaul Mackerras 			ip = (unsigned int *)
2359b6b563cSPaul Mackerras 				get_property(ipar, "#interrupt-cells", NULL);
2369b6b563cSPaul Mackerras 			if (ip == NULL) {
2379b6b563cSPaul Mackerras 				printk("oops, no #interrupt-cells on %s\n",
2389b6b563cSPaul Mackerras 				       ipar->full_name);
2399b6b563cSPaul Mackerras 				return 0;
2409b6b563cSPaul Mackerras 			}
2419b6b563cSPaul Mackerras 			newintrc = *ip;
2429b6b563cSPaul Mackerras 			ip = (unsigned int *)
2439b6b563cSPaul Mackerras 				get_property(ipar, "#address-cells", NULL);
2449b6b563cSPaul Mackerras 			newaddrc = (ip == NULL)? 0: *ip;
2459b6b563cSPaul Mackerras 			imap += newaddrc + newintrc;
2469b6b563cSPaul Mackerras 			imaplen -= newaddrc + newintrc;
2479b6b563cSPaul Mackerras 		}
2489b6b563cSPaul Mackerras 		if (imaplen < 0) {
2499b6b563cSPaul Mackerras 			printk("oops, error decoding int-map on %s, len=%d\n",
2509b6b563cSPaul Mackerras 			       p->full_name, imaplen);
2519b6b563cSPaul Mackerras 			return 0;
2529b6b563cSPaul Mackerras 		}
2539b6b563cSPaul Mackerras 		if (!match) {
2549b6b563cSPaul Mackerras #ifdef DEBUG_IRQ
2559b6b563cSPaul Mackerras 			printk("oops, no match in %s int-map for %s\n",
2569b6b563cSPaul Mackerras 			       p->full_name, np->full_name);
2579b6b563cSPaul Mackerras #endif
2589b6b563cSPaul Mackerras 			return 0;
2599b6b563cSPaul Mackerras 		}
2609b6b563cSPaul Mackerras 		p = ipar;
2619b6b563cSPaul Mackerras 		naddrc = newaddrc;
2629b6b563cSPaul Mackerras 		nintrc = newintrc;
2639b6b563cSPaul Mackerras 		ints = imap - nintrc;
2649b6b563cSPaul Mackerras 		reg = ints - naddrc;
2659b6b563cSPaul Mackerras 	}
2669b6b563cSPaul Mackerras 	if (p == NULL) {
2679b6b563cSPaul Mackerras #ifdef DEBUG_IRQ
2689b6b563cSPaul Mackerras 		printk("hmmm, int tree for %s doesn't have ctrler\n",
2699b6b563cSPaul Mackerras 		       np->full_name);
2709b6b563cSPaul Mackerras #endif
2719b6b563cSPaul Mackerras 		return 0;
2729b6b563cSPaul Mackerras 	}
2739b6b563cSPaul Mackerras 	*irq = ints;
2749b6b563cSPaul Mackerras 	*ictrler = p;
2759b6b563cSPaul Mackerras 	return nintrc;
2769b6b563cSPaul Mackerras }
2779b6b563cSPaul Mackerras 
2786d0124fcSPaul Mackerras static unsigned char map_isa_senses[4] = {
2796d0124fcSPaul Mackerras 	IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
2806d0124fcSPaul Mackerras 	IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
2816d0124fcSPaul Mackerras 	IRQ_SENSE_EDGE  | IRQ_POLARITY_NEGATIVE,
2826d0124fcSPaul Mackerras 	IRQ_SENSE_EDGE  | IRQ_POLARITY_POSITIVE
2836d0124fcSPaul Mackerras };
2846d0124fcSPaul Mackerras 
2856d0124fcSPaul Mackerras static unsigned char map_mpic_senses[4] = {
2866d0124fcSPaul Mackerras 	IRQ_SENSE_EDGE  | IRQ_POLARITY_POSITIVE,
2876d0124fcSPaul Mackerras 	IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
2886d0124fcSPaul Mackerras 	/* 2 seems to be used for the 8259 cascade... */
2896d0124fcSPaul Mackerras 	IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
2906d0124fcSPaul Mackerras 	IRQ_SENSE_EDGE  | IRQ_POLARITY_NEGATIVE,
2916d0124fcSPaul Mackerras };
2926d0124fcSPaul Mackerras 
2939b6b563cSPaul Mackerras static int __devinit finish_node_interrupts(struct device_node *np,
2949b6b563cSPaul Mackerras 					    unsigned long *mem_start,
2959b6b563cSPaul Mackerras 					    int measure_only)
2969b6b563cSPaul Mackerras {
2979b6b563cSPaul Mackerras 	unsigned int *ints;
2989b6b563cSPaul Mackerras 	int intlen, intrcells, intrcount;
2996d0124fcSPaul Mackerras 	int i, j, n, sense;
3009b6b563cSPaul Mackerras 	unsigned int *irq, virq;
3019b6b563cSPaul Mackerras 	struct device_node *ic;
3021beb6a7dSBenjamin Herrenschmidt 	int trace = 0;
3031beb6a7dSBenjamin Herrenschmidt 
3041beb6a7dSBenjamin Herrenschmidt 	//#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
3051beb6a7dSBenjamin Herrenschmidt #define TRACE(fmt...)
3061beb6a7dSBenjamin Herrenschmidt 
3071beb6a7dSBenjamin Herrenschmidt 	if (!strcmp(np->name, "smu-doorbell"))
3081beb6a7dSBenjamin Herrenschmidt 		trace = 1;
3091beb6a7dSBenjamin Herrenschmidt 
3101beb6a7dSBenjamin Herrenschmidt 	TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
3111beb6a7dSBenjamin Herrenschmidt 	      num_interrupt_controllers);
3129b6b563cSPaul Mackerras 
313a575b807SPaul Mackerras 	if (num_interrupt_controllers == 0) {
314a575b807SPaul Mackerras 		/*
315a575b807SPaul Mackerras 		 * Old machines just have a list of interrupt numbers
316a575b807SPaul Mackerras 		 * and no interrupt-controller nodes.
317a575b807SPaul Mackerras 		 */
318a575b807SPaul Mackerras 		ints = (unsigned int *) get_property(np, "AAPL,interrupts",
319a575b807SPaul Mackerras 						     &intlen);
320a575b807SPaul Mackerras 		/* XXX old interpret_pci_props looked in parent too */
321a575b807SPaul Mackerras 		/* XXX old interpret_macio_props looked for interrupts
322a575b807SPaul Mackerras 		   before AAPL,interrupts */
323a575b807SPaul Mackerras 		if (ints == NULL)
324a575b807SPaul Mackerras 			ints = (unsigned int *) get_property(np, "interrupts",
325a575b807SPaul Mackerras 							     &intlen);
326a575b807SPaul Mackerras 		if (ints == NULL)
327a575b807SPaul Mackerras 			return 0;
328a575b807SPaul Mackerras 
329a575b807SPaul Mackerras 		np->n_intrs = intlen / sizeof(unsigned int);
330a575b807SPaul Mackerras 		np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
331a575b807SPaul Mackerras 				       mem_start);
332a575b807SPaul Mackerras 		if (!np->intrs)
333a575b807SPaul Mackerras 			return -ENOMEM;
334a575b807SPaul Mackerras 		if (measure_only)
335a575b807SPaul Mackerras 			return 0;
336a575b807SPaul Mackerras 
337a575b807SPaul Mackerras 		for (i = 0; i < np->n_intrs; ++i) {
338a575b807SPaul Mackerras 			np->intrs[i].line = *ints++;
3396d0124fcSPaul Mackerras 			np->intrs[i].sense = IRQ_SENSE_LEVEL
3406d0124fcSPaul Mackerras 				| IRQ_POLARITY_NEGATIVE;
341a575b807SPaul Mackerras 		}
342a575b807SPaul Mackerras 		return 0;
343a575b807SPaul Mackerras 	}
344a575b807SPaul Mackerras 
3459b6b563cSPaul Mackerras 	ints = (unsigned int *) get_property(np, "interrupts", &intlen);
3461beb6a7dSBenjamin Herrenschmidt 	TRACE("ints=%p, intlen=%d\n", ints, intlen);
3479b6b563cSPaul Mackerras 	if (ints == NULL)
3489b6b563cSPaul Mackerras 		return 0;
3499b6b563cSPaul Mackerras 	intrcells = prom_n_intr_cells(np);
3509b6b563cSPaul Mackerras 	intlen /= intrcells * sizeof(unsigned int);
3511beb6a7dSBenjamin Herrenschmidt 	TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
3529b6b563cSPaul Mackerras 	np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
3539b6b563cSPaul Mackerras 	if (!np->intrs)
3549b6b563cSPaul Mackerras 		return -ENOMEM;
3559b6b563cSPaul Mackerras 
3569b6b563cSPaul Mackerras 	if (measure_only)
3579b6b563cSPaul Mackerras 		return 0;
3589b6b563cSPaul Mackerras 
3599b6b563cSPaul Mackerras 	intrcount = 0;
3609b6b563cSPaul Mackerras 	for (i = 0; i < intlen; ++i, ints += intrcells) {
3619b6b563cSPaul Mackerras 		n = map_interrupt(&irq, &ic, np, ints, intrcells);
3621beb6a7dSBenjamin Herrenschmidt 		TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
3639b6b563cSPaul Mackerras 		if (n <= 0)
3649b6b563cSPaul Mackerras 			continue;
3659b6b563cSPaul Mackerras 
3669b6b563cSPaul Mackerras 		/* don't map IRQ numbers under a cascaded 8259 controller */
3679b6b563cSPaul Mackerras 		if (ic && device_is_compatible(ic, "chrp,iic")) {
3689b6b563cSPaul Mackerras 			np->intrs[intrcount].line = irq[0];
3696d0124fcSPaul Mackerras 			sense = (n > 1)? (irq[1] & 3): 3;
3706d0124fcSPaul Mackerras 			np->intrs[intrcount].sense = map_isa_senses[sense];
3719b6b563cSPaul Mackerras 		} else {
3729b6b563cSPaul Mackerras 			virq = virt_irq_create_mapping(irq[0]);
3731beb6a7dSBenjamin Herrenschmidt 			TRACE("virq=%d\n", virq);
3746d0124fcSPaul Mackerras #ifdef CONFIG_PPC64
3759b6b563cSPaul Mackerras 			if (virq == NO_IRQ) {
3769b6b563cSPaul Mackerras 				printk(KERN_CRIT "Could not allocate interrupt"
3779b6b563cSPaul Mackerras 				       " number for %s\n", np->full_name);
3789b6b563cSPaul Mackerras 				continue;
3799b6b563cSPaul Mackerras 			}
3809b6b563cSPaul Mackerras #endif
3816d0124fcSPaul Mackerras 			np->intrs[intrcount].line = irq_offset_up(virq);
3826d0124fcSPaul Mackerras 			sense = (n > 1)? (irq[1] & 3): 1;
3831beb6a7dSBenjamin Herrenschmidt 
3841beb6a7dSBenjamin Herrenschmidt 			/* Apple uses bits in there in a different way, let's
3851beb6a7dSBenjamin Herrenschmidt 			 * only keep the real sense bit on macs
3861beb6a7dSBenjamin Herrenschmidt 			 */
387e8222502SBenjamin Herrenschmidt 			if (machine_is(powermac))
3881beb6a7dSBenjamin Herrenschmidt 				sense &= 0x1;
3896d0124fcSPaul Mackerras 			np->intrs[intrcount].sense = map_mpic_senses[sense];
3909b6b563cSPaul Mackerras 		}
3919b6b563cSPaul Mackerras 
3929b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3939b6b563cSPaul Mackerras 		/* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
394e8222502SBenjamin Herrenschmidt 		if (machine_is(powermac) && ic && ic->parent) {
3959b6b563cSPaul Mackerras 			char *name = get_property(ic->parent, "name", NULL);
3969b6b563cSPaul Mackerras 			if (name && !strcmp(name, "u3"))
3979b6b563cSPaul Mackerras 				np->intrs[intrcount].line += 128;
3981beb6a7dSBenjamin Herrenschmidt 			else if (!(name && (!strcmp(name, "mac-io") ||
3991beb6a7dSBenjamin Herrenschmidt 					    !strcmp(name, "u4"))))
4009b6b563cSPaul Mackerras 				/* ignore other cascaded controllers, such as
4019b6b563cSPaul Mackerras 				   the k2-sata-root */
4029b6b563cSPaul Mackerras 				break;
4039b6b563cSPaul Mackerras 		}
4041beb6a7dSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
4059b6b563cSPaul Mackerras 		if (n > 2) {
4069b6b563cSPaul Mackerras 			printk("hmmm, got %d intr cells for %s:", n,
4079b6b563cSPaul Mackerras 			       np->full_name);
4089b6b563cSPaul Mackerras 			for (j = 0; j < n; ++j)
4099b6b563cSPaul Mackerras 				printk(" %d", irq[j]);
4109b6b563cSPaul Mackerras 			printk("\n");
4119b6b563cSPaul Mackerras 		}
4129b6b563cSPaul Mackerras 		++intrcount;
4139b6b563cSPaul Mackerras 	}
4149b6b563cSPaul Mackerras 	np->n_intrs = intrcount;
4159b6b563cSPaul Mackerras 
4169b6b563cSPaul Mackerras 	return 0;
4179b6b563cSPaul Mackerras }
4189b6b563cSPaul Mackerras 
4199b6b563cSPaul Mackerras static int __devinit finish_node(struct device_node *np,
4209b6b563cSPaul Mackerras 				 unsigned long *mem_start,
4219b6b563cSPaul Mackerras 				 int measure_only)
4229b6b563cSPaul Mackerras {
4239b6b563cSPaul Mackerras 	struct device_node *child;
424cc5d0189SBenjamin Herrenschmidt 	int rc = 0;
4259b6b563cSPaul Mackerras 
4269b6b563cSPaul Mackerras 	rc = finish_node_interrupts(np, mem_start, measure_only);
4279b6b563cSPaul Mackerras 	if (rc)
4289b6b563cSPaul Mackerras 		goto out;
4299b6b563cSPaul Mackerras 
4309b6b563cSPaul Mackerras 	for (child = np->child; child != NULL; child = child->sibling) {
431cc5d0189SBenjamin Herrenschmidt 		rc = finish_node(child, mem_start, measure_only);
4329b6b563cSPaul Mackerras 		if (rc)
4339b6b563cSPaul Mackerras 			goto out;
4349b6b563cSPaul Mackerras 	}
4359b6b563cSPaul Mackerras out:
4369b6b563cSPaul Mackerras 	return rc;
4379b6b563cSPaul Mackerras }
4389b6b563cSPaul Mackerras 
4399b6b563cSPaul Mackerras static void __init scan_interrupt_controllers(void)
4409b6b563cSPaul Mackerras {
4419b6b563cSPaul Mackerras 	struct device_node *np;
4429b6b563cSPaul Mackerras 	int n = 0;
4439b6b563cSPaul Mackerras 	char *name, *ic;
4449b6b563cSPaul Mackerras 	int iclen;
4459b6b563cSPaul Mackerras 
4469b6b563cSPaul Mackerras 	for (np = allnodes; np != NULL; np = np->allnext) {
4479b6b563cSPaul Mackerras 		ic = get_property(np, "interrupt-controller", &iclen);
4489b6b563cSPaul Mackerras 		name = get_property(np, "name", NULL);
4499b6b563cSPaul Mackerras 		/* checking iclen makes sure we don't get a false
4509b6b563cSPaul Mackerras 		   match on /chosen.interrupt_controller */
4519b6b563cSPaul Mackerras 		if ((name != NULL
4529b6b563cSPaul Mackerras 		     && strcmp(name, "interrupt-controller") == 0)
4539b6b563cSPaul Mackerras 		    || (ic != NULL && iclen == 0
4549b6b563cSPaul Mackerras 			&& strcmp(name, "AppleKiwi"))) {
4559b6b563cSPaul Mackerras 			if (n == 0)
4569b6b563cSPaul Mackerras 				dflt_interrupt_controller = np;
4579b6b563cSPaul Mackerras 			++n;
4589b6b563cSPaul Mackerras 		}
4599b6b563cSPaul Mackerras 	}
4609b6b563cSPaul Mackerras 	num_interrupt_controllers = n;
4619b6b563cSPaul Mackerras }
4629b6b563cSPaul Mackerras 
4639b6b563cSPaul Mackerras /**
4649b6b563cSPaul Mackerras  * finish_device_tree is called once things are running normally
4659b6b563cSPaul Mackerras  * (i.e. with text and data mapped to the address they were linked at).
4669b6b563cSPaul Mackerras  * It traverses the device tree and fills in some of the additional,
4679b6b563cSPaul Mackerras  * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
4689b6b563cSPaul Mackerras  * mapping is also initialized at this point.
4699b6b563cSPaul Mackerras  */
4709b6b563cSPaul Mackerras void __init finish_device_tree(void)
4719b6b563cSPaul Mackerras {
4729b6b563cSPaul Mackerras 	unsigned long start, end, size = 0;
4739b6b563cSPaul Mackerras 
4749b6b563cSPaul Mackerras 	DBG(" -> finish_device_tree\n");
4759b6b563cSPaul Mackerras 
4769b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4779b6b563cSPaul Mackerras 	/* Initialize virtual IRQ map */
4789b6b563cSPaul Mackerras 	virt_irq_init();
4799b6b563cSPaul Mackerras #endif
4809b6b563cSPaul Mackerras 	scan_interrupt_controllers();
4819b6b563cSPaul Mackerras 
4829b6b563cSPaul Mackerras 	/*
4839b6b563cSPaul Mackerras 	 * Finish device-tree (pre-parsing some properties etc...)
4849b6b563cSPaul Mackerras 	 * We do this in 2 passes. One with "measure_only" set, which
4859b6b563cSPaul Mackerras 	 * will only measure the amount of memory needed, then we can
4869b6b563cSPaul Mackerras 	 * allocate that memory, and call finish_node again. However,
4879b6b563cSPaul Mackerras 	 * we must be careful as most routines will fail nowadays when
4889b6b563cSPaul Mackerras 	 * prom_alloc() returns 0, so we must make sure our first pass
4899b6b563cSPaul Mackerras 	 * doesn't start at 0. We pre-initialize size to 16 for that
4909b6b563cSPaul Mackerras 	 * reason and then remove those additional 16 bytes
4919b6b563cSPaul Mackerras 	 */
4929b6b563cSPaul Mackerras 	size = 16;
493cc5d0189SBenjamin Herrenschmidt 	finish_node(allnodes, &size, 1);
4949b6b563cSPaul Mackerras 	size -= 16;
495fa938953SMichael Ellerman 
496fa938953SMichael Ellerman 	if (0 == size)
497fa938953SMichael Ellerman 		end = start = 0;
498fa938953SMichael Ellerman 	else
4999b6b563cSPaul Mackerras 		end = start = (unsigned long)__va(lmb_alloc(size, 128));
500fa938953SMichael Ellerman 
501cc5d0189SBenjamin Herrenschmidt 	finish_node(allnodes, &end, 0);
5029b6b563cSPaul Mackerras 	BUG_ON(end != start + size);
5039b6b563cSPaul Mackerras 
5049b6b563cSPaul Mackerras 	DBG(" <- finish_device_tree\n");
5059b6b563cSPaul Mackerras }
5069b6b563cSPaul Mackerras 
5079b6b563cSPaul Mackerras static inline char *find_flat_dt_string(u32 offset)
5089b6b563cSPaul Mackerras {
5099b6b563cSPaul Mackerras 	return ((char *)initial_boot_params) +
5109b6b563cSPaul Mackerras 		initial_boot_params->off_dt_strings + offset;
5119b6b563cSPaul Mackerras }
5129b6b563cSPaul Mackerras 
5139b6b563cSPaul Mackerras /**
5149b6b563cSPaul Mackerras  * This function is used to scan the flattened device-tree, it is
5159b6b563cSPaul Mackerras  * used to extract the memory informations at boot before we can
5169b6b563cSPaul Mackerras  * unflatten the tree
5179b6b563cSPaul Mackerras  */
5183c726f8dSBenjamin Herrenschmidt int __init of_scan_flat_dt(int (*it)(unsigned long node,
5199b6b563cSPaul Mackerras 				     const char *uname, int depth,
5209b6b563cSPaul Mackerras 				     void *data),
5219b6b563cSPaul Mackerras 			   void *data)
5229b6b563cSPaul Mackerras {
5239b6b563cSPaul Mackerras 	unsigned long p = ((unsigned long)initial_boot_params) +
5249b6b563cSPaul Mackerras 		initial_boot_params->off_dt_struct;
5259b6b563cSPaul Mackerras 	int rc = 0;
5269b6b563cSPaul Mackerras 	int depth = -1;
5279b6b563cSPaul Mackerras 
5289b6b563cSPaul Mackerras 	do {
5299b6b563cSPaul Mackerras 		u32 tag = *((u32 *)p);
5309b6b563cSPaul Mackerras 		char *pathp;
5319b6b563cSPaul Mackerras 
5329b6b563cSPaul Mackerras 		p += 4;
5339b6b563cSPaul Mackerras 		if (tag == OF_DT_END_NODE) {
5349b6b563cSPaul Mackerras 			depth --;
5359b6b563cSPaul Mackerras 			continue;
5369b6b563cSPaul Mackerras 		}
5379b6b563cSPaul Mackerras 		if (tag == OF_DT_NOP)
5389b6b563cSPaul Mackerras 			continue;
5399b6b563cSPaul Mackerras 		if (tag == OF_DT_END)
5409b6b563cSPaul Mackerras 			break;
5419b6b563cSPaul Mackerras 		if (tag == OF_DT_PROP) {
5429b6b563cSPaul Mackerras 			u32 sz = *((u32 *)p);
5439b6b563cSPaul Mackerras 			p += 8;
5449b6b563cSPaul Mackerras 			if (initial_boot_params->version < 0x10)
5459b6b563cSPaul Mackerras 				p = _ALIGN(p, sz >= 8 ? 8 : 4);
5469b6b563cSPaul Mackerras 			p += sz;
5479b6b563cSPaul Mackerras 			p = _ALIGN(p, 4);
5489b6b563cSPaul Mackerras 			continue;
5499b6b563cSPaul Mackerras 		}
5509b6b563cSPaul Mackerras 		if (tag != OF_DT_BEGIN_NODE) {
5519b6b563cSPaul Mackerras 			printk(KERN_WARNING "Invalid tag %x scanning flattened"
5529b6b563cSPaul Mackerras 			       " device tree !\n", tag);
5539b6b563cSPaul Mackerras 			return -EINVAL;
5549b6b563cSPaul Mackerras 		}
5559b6b563cSPaul Mackerras 		depth++;
5569b6b563cSPaul Mackerras 		pathp = (char *)p;
5579b6b563cSPaul Mackerras 		p = _ALIGN(p + strlen(pathp) + 1, 4);
5589b6b563cSPaul Mackerras 		if ((*pathp) == '/') {
5599b6b563cSPaul Mackerras 			char *lp, *np;
5609b6b563cSPaul Mackerras 			for (lp = NULL, np = pathp; *np; np++)
5619b6b563cSPaul Mackerras 				if ((*np) == '/')
5629b6b563cSPaul Mackerras 					lp = np+1;
5639b6b563cSPaul Mackerras 			if (lp != NULL)
5649b6b563cSPaul Mackerras 				pathp = lp;
5659b6b563cSPaul Mackerras 		}
5669b6b563cSPaul Mackerras 		rc = it(p, pathp, depth, data);
5679b6b563cSPaul Mackerras 		if (rc != 0)
5689b6b563cSPaul Mackerras 			break;
5699b6b563cSPaul Mackerras 	} while(1);
5709b6b563cSPaul Mackerras 
5719b6b563cSPaul Mackerras 	return rc;
5729b6b563cSPaul Mackerras }
5739b6b563cSPaul Mackerras 
574e8222502SBenjamin Herrenschmidt unsigned long __init of_get_flat_dt_root(void)
575e8222502SBenjamin Herrenschmidt {
576e8222502SBenjamin Herrenschmidt 	unsigned long p = ((unsigned long)initial_boot_params) +
577e8222502SBenjamin Herrenschmidt 		initial_boot_params->off_dt_struct;
578e8222502SBenjamin Herrenschmidt 
579e8222502SBenjamin Herrenschmidt 	while(*((u32 *)p) == OF_DT_NOP)
580e8222502SBenjamin Herrenschmidt 		p += 4;
581e8222502SBenjamin Herrenschmidt 	BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
582e8222502SBenjamin Herrenschmidt 	p += 4;
583e8222502SBenjamin Herrenschmidt 	return _ALIGN(p + strlen((char *)p) + 1, 4);
584e8222502SBenjamin Herrenschmidt }
585e8222502SBenjamin Herrenschmidt 
5869b6b563cSPaul Mackerras /**
5879b6b563cSPaul Mackerras  * This  function can be used within scan_flattened_dt callback to get
5889b6b563cSPaul Mackerras  * access to properties
5899b6b563cSPaul Mackerras  */
5903c726f8dSBenjamin Herrenschmidt void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
5919b6b563cSPaul Mackerras 				 unsigned long *size)
5929b6b563cSPaul Mackerras {
5939b6b563cSPaul Mackerras 	unsigned long p = node;
5949b6b563cSPaul Mackerras 
5959b6b563cSPaul Mackerras 	do {
5969b6b563cSPaul Mackerras 		u32 tag = *((u32 *)p);
5979b6b563cSPaul Mackerras 		u32 sz, noff;
5989b6b563cSPaul Mackerras 		const char *nstr;
5999b6b563cSPaul Mackerras 
6009b6b563cSPaul Mackerras 		p += 4;
6019b6b563cSPaul Mackerras 		if (tag == OF_DT_NOP)
6029b6b563cSPaul Mackerras 			continue;
6039b6b563cSPaul Mackerras 		if (tag != OF_DT_PROP)
6049b6b563cSPaul Mackerras 			return NULL;
6059b6b563cSPaul Mackerras 
6069b6b563cSPaul Mackerras 		sz = *((u32 *)p);
6079b6b563cSPaul Mackerras 		noff = *((u32 *)(p + 4));
6089b6b563cSPaul Mackerras 		p += 8;
6099b6b563cSPaul Mackerras 		if (initial_boot_params->version < 0x10)
6109b6b563cSPaul Mackerras 			p = _ALIGN(p, sz >= 8 ? 8 : 4);
6119b6b563cSPaul Mackerras 
6129b6b563cSPaul Mackerras 		nstr = find_flat_dt_string(noff);
6139b6b563cSPaul Mackerras 		if (nstr == NULL) {
6149b6b563cSPaul Mackerras 			printk(KERN_WARNING "Can't find property index"
6159b6b563cSPaul Mackerras 			       " name !\n");
6169b6b563cSPaul Mackerras 			return NULL;
6179b6b563cSPaul Mackerras 		}
6189b6b563cSPaul Mackerras 		if (strcmp(name, nstr) == 0) {
6199b6b563cSPaul Mackerras 			if (size)
6209b6b563cSPaul Mackerras 				*size = sz;
6219b6b563cSPaul Mackerras 			return (void *)p;
6229b6b563cSPaul Mackerras 		}
6239b6b563cSPaul Mackerras 		p += sz;
6249b6b563cSPaul Mackerras 		p = _ALIGN(p, 4);
6259b6b563cSPaul Mackerras 	} while(1);
6269b6b563cSPaul Mackerras }
6279b6b563cSPaul Mackerras 
628e8222502SBenjamin Herrenschmidt int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
629e8222502SBenjamin Herrenschmidt {
630e8222502SBenjamin Herrenschmidt 	const char* cp;
631e8222502SBenjamin Herrenschmidt 	unsigned long cplen, l;
632e8222502SBenjamin Herrenschmidt 
633e8222502SBenjamin Herrenschmidt 	cp = of_get_flat_dt_prop(node, "compatible", &cplen);
634e8222502SBenjamin Herrenschmidt 	if (cp == NULL)
635e8222502SBenjamin Herrenschmidt 		return 0;
636e8222502SBenjamin Herrenschmidt 	while (cplen > 0) {
637e8222502SBenjamin Herrenschmidt 		if (strncasecmp(cp, compat, strlen(compat)) == 0)
638e8222502SBenjamin Herrenschmidt 			return 1;
639e8222502SBenjamin Herrenschmidt 		l = strlen(cp) + 1;
640e8222502SBenjamin Herrenschmidt 		cp += l;
641e8222502SBenjamin Herrenschmidt 		cplen -= l;
642e8222502SBenjamin Herrenschmidt 	}
643e8222502SBenjamin Herrenschmidt 
644e8222502SBenjamin Herrenschmidt 	return 0;
645e8222502SBenjamin Herrenschmidt }
646e8222502SBenjamin Herrenschmidt 
6479b6b563cSPaul Mackerras static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
6489b6b563cSPaul Mackerras 				       unsigned long align)
6499b6b563cSPaul Mackerras {
6509b6b563cSPaul Mackerras 	void *res;
6519b6b563cSPaul Mackerras 
6529b6b563cSPaul Mackerras 	*mem = _ALIGN(*mem, align);
6539b6b563cSPaul Mackerras 	res = (void *)*mem;
6549b6b563cSPaul Mackerras 	*mem += size;
6559b6b563cSPaul Mackerras 
6569b6b563cSPaul Mackerras 	return res;
6579b6b563cSPaul Mackerras }
6589b6b563cSPaul Mackerras 
6599b6b563cSPaul Mackerras static unsigned long __init unflatten_dt_node(unsigned long mem,
6609b6b563cSPaul Mackerras 					      unsigned long *p,
6619b6b563cSPaul Mackerras 					      struct device_node *dad,
6629b6b563cSPaul Mackerras 					      struct device_node ***allnextpp,
6639b6b563cSPaul Mackerras 					      unsigned long fpsize)
6649b6b563cSPaul Mackerras {
6659b6b563cSPaul Mackerras 	struct device_node *np;
6669b6b563cSPaul Mackerras 	struct property *pp, **prev_pp = NULL;
6679b6b563cSPaul Mackerras 	char *pathp;
6689b6b563cSPaul Mackerras 	u32 tag;
6699b6b563cSPaul Mackerras 	unsigned int l, allocl;
6709b6b563cSPaul Mackerras 	int has_name = 0;
6719b6b563cSPaul Mackerras 	int new_format = 0;
6729b6b563cSPaul Mackerras 
6739b6b563cSPaul Mackerras 	tag = *((u32 *)(*p));
6749b6b563cSPaul Mackerras 	if (tag != OF_DT_BEGIN_NODE) {
6759b6b563cSPaul Mackerras 		printk("Weird tag at start of node: %x\n", tag);
6769b6b563cSPaul Mackerras 		return mem;
6779b6b563cSPaul Mackerras 	}
6789b6b563cSPaul Mackerras 	*p += 4;
6799b6b563cSPaul Mackerras 	pathp = (char *)*p;
6809b6b563cSPaul Mackerras 	l = allocl = strlen(pathp) + 1;
6819b6b563cSPaul Mackerras 	*p = _ALIGN(*p + l, 4);
6829b6b563cSPaul Mackerras 
6839b6b563cSPaul Mackerras 	/* version 0x10 has a more compact unit name here instead of the full
6849b6b563cSPaul Mackerras 	 * path. we accumulate the full path size using "fpsize", we'll rebuild
6859b6b563cSPaul Mackerras 	 * it later. We detect this because the first character of the name is
6869b6b563cSPaul Mackerras 	 * not '/'.
6879b6b563cSPaul Mackerras 	 */
6889b6b563cSPaul Mackerras 	if ((*pathp) != '/') {
6899b6b563cSPaul Mackerras 		new_format = 1;
6909b6b563cSPaul Mackerras 		if (fpsize == 0) {
6919b6b563cSPaul Mackerras 			/* root node: special case. fpsize accounts for path
6929b6b563cSPaul Mackerras 			 * plus terminating zero. root node only has '/', so
6939b6b563cSPaul Mackerras 			 * fpsize should be 2, but we want to avoid the first
6949b6b563cSPaul Mackerras 			 * level nodes to have two '/' so we use fpsize 1 here
6959b6b563cSPaul Mackerras 			 */
6969b6b563cSPaul Mackerras 			fpsize = 1;
6979b6b563cSPaul Mackerras 			allocl = 2;
6989b6b563cSPaul Mackerras 		} else {
6999b6b563cSPaul Mackerras 			/* account for '/' and path size minus terminal 0
7009b6b563cSPaul Mackerras 			 * already in 'l'
7019b6b563cSPaul Mackerras 			 */
7029b6b563cSPaul Mackerras 			fpsize += l;
7039b6b563cSPaul Mackerras 			allocl = fpsize;
7049b6b563cSPaul Mackerras 		}
7059b6b563cSPaul Mackerras 	}
7069b6b563cSPaul Mackerras 
7079b6b563cSPaul Mackerras 
7089b6b563cSPaul Mackerras 	np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
7099b6b563cSPaul Mackerras 				__alignof__(struct device_node));
7109b6b563cSPaul Mackerras 	if (allnextpp) {
7119b6b563cSPaul Mackerras 		memset(np, 0, sizeof(*np));
7129b6b563cSPaul Mackerras 		np->full_name = ((char*)np) + sizeof(struct device_node);
7139b6b563cSPaul Mackerras 		if (new_format) {
7149b6b563cSPaul Mackerras 			char *p = np->full_name;
7159b6b563cSPaul Mackerras 			/* rebuild full path for new format */
7169b6b563cSPaul Mackerras 			if (dad && dad->parent) {
7179b6b563cSPaul Mackerras 				strcpy(p, dad->full_name);
7189b6b563cSPaul Mackerras #ifdef DEBUG
7199b6b563cSPaul Mackerras 				if ((strlen(p) + l + 1) != allocl) {
7209b6b563cSPaul Mackerras 					DBG("%s: p: %d, l: %d, a: %d\n",
721e8222502SBenjamin Herrenschmidt 					    pathp, (int)strlen(p), l, allocl);
7229b6b563cSPaul Mackerras 				}
7239b6b563cSPaul Mackerras #endif
7249b6b563cSPaul Mackerras 				p += strlen(p);
7259b6b563cSPaul Mackerras 			}
7269b6b563cSPaul Mackerras 			*(p++) = '/';
7279b6b563cSPaul Mackerras 			memcpy(p, pathp, l);
7289b6b563cSPaul Mackerras 		} else
7299b6b563cSPaul Mackerras 			memcpy(np->full_name, pathp, l);
7309b6b563cSPaul Mackerras 		prev_pp = &np->properties;
7319b6b563cSPaul Mackerras 		**allnextpp = np;
7329b6b563cSPaul Mackerras 		*allnextpp = &np->allnext;
7339b6b563cSPaul Mackerras 		if (dad != NULL) {
7349b6b563cSPaul Mackerras 			np->parent = dad;
7359b6b563cSPaul Mackerras 			/* we temporarily use the next field as `last_child'*/
7369b6b563cSPaul Mackerras 			if (dad->next == 0)
7379b6b563cSPaul Mackerras 				dad->child = np;
7389b6b563cSPaul Mackerras 			else
7399b6b563cSPaul Mackerras 				dad->next->sibling = np;
7409b6b563cSPaul Mackerras 			dad->next = np;
7419b6b563cSPaul Mackerras 		}
7429b6b563cSPaul Mackerras 		kref_init(&np->kref);
7439b6b563cSPaul Mackerras 	}
7449b6b563cSPaul Mackerras 	while(1) {
7459b6b563cSPaul Mackerras 		u32 sz, noff;
7469b6b563cSPaul Mackerras 		char *pname;
7479b6b563cSPaul Mackerras 
7489b6b563cSPaul Mackerras 		tag = *((u32 *)(*p));
7499b6b563cSPaul Mackerras 		if (tag == OF_DT_NOP) {
7509b6b563cSPaul Mackerras 			*p += 4;
7519b6b563cSPaul Mackerras 			continue;
7529b6b563cSPaul Mackerras 		}
7539b6b563cSPaul Mackerras 		if (tag != OF_DT_PROP)
7549b6b563cSPaul Mackerras 			break;
7559b6b563cSPaul Mackerras 		*p += 4;
7569b6b563cSPaul Mackerras 		sz = *((u32 *)(*p));
7579b6b563cSPaul Mackerras 		noff = *((u32 *)((*p) + 4));
7589b6b563cSPaul Mackerras 		*p += 8;
7599b6b563cSPaul Mackerras 		if (initial_boot_params->version < 0x10)
7609b6b563cSPaul Mackerras 			*p = _ALIGN(*p, sz >= 8 ? 8 : 4);
7619b6b563cSPaul Mackerras 
7629b6b563cSPaul Mackerras 		pname = find_flat_dt_string(noff);
7639b6b563cSPaul Mackerras 		if (pname == NULL) {
7649b6b563cSPaul Mackerras 			printk("Can't find property name in list !\n");
7659b6b563cSPaul Mackerras 			break;
7669b6b563cSPaul Mackerras 		}
7679b6b563cSPaul Mackerras 		if (strcmp(pname, "name") == 0)
7689b6b563cSPaul Mackerras 			has_name = 1;
7699b6b563cSPaul Mackerras 		l = strlen(pname) + 1;
7709b6b563cSPaul Mackerras 		pp = unflatten_dt_alloc(&mem, sizeof(struct property),
7719b6b563cSPaul Mackerras 					__alignof__(struct property));
7729b6b563cSPaul Mackerras 		if (allnextpp) {
7739b6b563cSPaul Mackerras 			if (strcmp(pname, "linux,phandle") == 0) {
7749b6b563cSPaul Mackerras 				np->node = *((u32 *)*p);
7759b6b563cSPaul Mackerras 				if (np->linux_phandle == 0)
7769b6b563cSPaul Mackerras 					np->linux_phandle = np->node;
7779b6b563cSPaul Mackerras 			}
7789b6b563cSPaul Mackerras 			if (strcmp(pname, "ibm,phandle") == 0)
7799b6b563cSPaul Mackerras 				np->linux_phandle = *((u32 *)*p);
7809b6b563cSPaul Mackerras 			pp->name = pname;
7819b6b563cSPaul Mackerras 			pp->length = sz;
7829b6b563cSPaul Mackerras 			pp->value = (void *)*p;
7839b6b563cSPaul Mackerras 			*prev_pp = pp;
7849b6b563cSPaul Mackerras 			prev_pp = &pp->next;
7859b6b563cSPaul Mackerras 		}
7869b6b563cSPaul Mackerras 		*p = _ALIGN((*p) + sz, 4);
7879b6b563cSPaul Mackerras 	}
7889b6b563cSPaul Mackerras 	/* with version 0x10 we may not have the name property, recreate
7899b6b563cSPaul Mackerras 	 * it here from the unit name if absent
7909b6b563cSPaul Mackerras 	 */
7919b6b563cSPaul Mackerras 	if (!has_name) {
7929b6b563cSPaul Mackerras 		char *p = pathp, *ps = pathp, *pa = NULL;
7939b6b563cSPaul Mackerras 		int sz;
7949b6b563cSPaul Mackerras 
7959b6b563cSPaul Mackerras 		while (*p) {
7969b6b563cSPaul Mackerras 			if ((*p) == '@')
7979b6b563cSPaul Mackerras 				pa = p;
7989b6b563cSPaul Mackerras 			if ((*p) == '/')
7999b6b563cSPaul Mackerras 				ps = p + 1;
8009b6b563cSPaul Mackerras 			p++;
8019b6b563cSPaul Mackerras 		}
8029b6b563cSPaul Mackerras 		if (pa < ps)
8039b6b563cSPaul Mackerras 			pa = p;
8049b6b563cSPaul Mackerras 		sz = (pa - ps) + 1;
8059b6b563cSPaul Mackerras 		pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
8069b6b563cSPaul Mackerras 					__alignof__(struct property));
8079b6b563cSPaul Mackerras 		if (allnextpp) {
8089b6b563cSPaul Mackerras 			pp->name = "name";
8099b6b563cSPaul Mackerras 			pp->length = sz;
8109b6b563cSPaul Mackerras 			pp->value = (unsigned char *)(pp + 1);
8119b6b563cSPaul Mackerras 			*prev_pp = pp;
8129b6b563cSPaul Mackerras 			prev_pp = &pp->next;
8139b6b563cSPaul Mackerras 			memcpy(pp->value, ps, sz - 1);
8149b6b563cSPaul Mackerras 			((char *)pp->value)[sz - 1] = 0;
8159b6b563cSPaul Mackerras 			DBG("fixed up name for %s -> %s\n", pathp, pp->value);
8169b6b563cSPaul Mackerras 		}
8179b6b563cSPaul Mackerras 	}
8189b6b563cSPaul Mackerras 	if (allnextpp) {
8199b6b563cSPaul Mackerras 		*prev_pp = NULL;
8209b6b563cSPaul Mackerras 		np->name = get_property(np, "name", NULL);
8219b6b563cSPaul Mackerras 		np->type = get_property(np, "device_type", NULL);
8229b6b563cSPaul Mackerras 
8239b6b563cSPaul Mackerras 		if (!np->name)
8249b6b563cSPaul Mackerras 			np->name = "<NULL>";
8259b6b563cSPaul Mackerras 		if (!np->type)
8269b6b563cSPaul Mackerras 			np->type = "<NULL>";
8279b6b563cSPaul Mackerras 	}
8289b6b563cSPaul Mackerras 	while (tag == OF_DT_BEGIN_NODE) {
8299b6b563cSPaul Mackerras 		mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
8309b6b563cSPaul Mackerras 		tag = *((u32 *)(*p));
8319b6b563cSPaul Mackerras 	}
8329b6b563cSPaul Mackerras 	if (tag != OF_DT_END_NODE) {
8339b6b563cSPaul Mackerras 		printk("Weird tag at end of node: %x\n", tag);
8349b6b563cSPaul Mackerras 		return mem;
8359b6b563cSPaul Mackerras 	}
8369b6b563cSPaul Mackerras 	*p += 4;
8379b6b563cSPaul Mackerras 	return mem;
8389b6b563cSPaul Mackerras }
8399b6b563cSPaul Mackerras 
8402babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
8412babf5c2SMichael Ellerman {
8422babf5c2SMichael Ellerman 	if (!p)
8432babf5c2SMichael Ellerman 		return 1;
8442babf5c2SMichael Ellerman 
8452babf5c2SMichael Ellerman 	memory_limit = PAGE_ALIGN(memparse(p, &p));
8462babf5c2SMichael Ellerman 	DBG("memory limit = 0x%lx\n", memory_limit);
8472babf5c2SMichael Ellerman 
8482babf5c2SMichael Ellerman 	return 0;
8492babf5c2SMichael Ellerman }
8502babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
8512babf5c2SMichael Ellerman 
8522babf5c2SMichael Ellerman /*
8532babf5c2SMichael Ellerman  * The device tree may be allocated below our memory limit, or inside the
8542babf5c2SMichael Ellerman  * crash kernel region for kdump. If so, move it out now.
8552babf5c2SMichael Ellerman  */
8562babf5c2SMichael Ellerman static void move_device_tree(void)
8572babf5c2SMichael Ellerman {
8582babf5c2SMichael Ellerman 	unsigned long start, size;
8592babf5c2SMichael Ellerman 	void *p;
8602babf5c2SMichael Ellerman 
8612babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
8622babf5c2SMichael Ellerman 
8632babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
8642babf5c2SMichael Ellerman 	size = initial_boot_params->totalsize;
8652babf5c2SMichael Ellerman 
8662babf5c2SMichael Ellerman 	if ((memory_limit && (start + size) > memory_limit) ||
8672babf5c2SMichael Ellerman 			overlaps_crashkernel(start, size)) {
8682babf5c2SMichael Ellerman 		p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
8692babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
8702babf5c2SMichael Ellerman 		initial_boot_params = (struct boot_param_header *)p;
8712babf5c2SMichael Ellerman 		DBG("Moved device tree to 0x%p\n", p);
8722babf5c2SMichael Ellerman 	}
8732babf5c2SMichael Ellerman 
8742babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
8752babf5c2SMichael Ellerman }
8769b6b563cSPaul Mackerras 
8779b6b563cSPaul Mackerras /**
8789b6b563cSPaul Mackerras  * unflattens the device-tree passed by the firmware, creating the
8799b6b563cSPaul Mackerras  * tree of struct device_node. It also fills the "name" and "type"
8809b6b563cSPaul Mackerras  * pointers of the nodes so the normal device-tree walking functions
8819b6b563cSPaul Mackerras  * can be used (this used to be done by finish_device_tree)
8829b6b563cSPaul Mackerras  */
8839b6b563cSPaul Mackerras void __init unflatten_device_tree(void)
8849b6b563cSPaul Mackerras {
8859b6b563cSPaul Mackerras 	unsigned long start, mem, size;
8869b6b563cSPaul Mackerras 	struct device_node **allnextp = &allnodes;
8879b6b563cSPaul Mackerras 
8889b6b563cSPaul Mackerras 	DBG(" -> unflatten_device_tree()\n");
8899b6b563cSPaul Mackerras 
8909b6b563cSPaul Mackerras 	/* First pass, scan for size */
8919b6b563cSPaul Mackerras 	start = ((unsigned long)initial_boot_params) +
8929b6b563cSPaul Mackerras 		initial_boot_params->off_dt_struct;
8939b6b563cSPaul Mackerras 	size = unflatten_dt_node(0, &start, NULL, NULL, 0);
8949b6b563cSPaul Mackerras 	size = (size | 3) + 1;
8959b6b563cSPaul Mackerras 
8969b6b563cSPaul Mackerras 	DBG("  size is %lx, allocating...\n", size);
8979b6b563cSPaul Mackerras 
8989b6b563cSPaul Mackerras 	/* Allocate memory for the expanded device tree */
8999b6b563cSPaul Mackerras 	mem = lmb_alloc(size + 4, __alignof__(struct device_node));
9009b6b563cSPaul Mackerras 	mem = (unsigned long) __va(mem);
9019b6b563cSPaul Mackerras 
9029b6b563cSPaul Mackerras 	((u32 *)mem)[size / 4] = 0xdeadbeef;
9039b6b563cSPaul Mackerras 
9049b6b563cSPaul Mackerras 	DBG("  unflattening %lx...\n", mem);
9059b6b563cSPaul Mackerras 
9069b6b563cSPaul Mackerras 	/* Second pass, do actual unflattening */
9079b6b563cSPaul Mackerras 	start = ((unsigned long)initial_boot_params) +
9089b6b563cSPaul Mackerras 		initial_boot_params->off_dt_struct;
9099b6b563cSPaul Mackerras 	unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
9109b6b563cSPaul Mackerras 	if (*((u32 *)start) != OF_DT_END)
9119b6b563cSPaul Mackerras 		printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
9129b6b563cSPaul Mackerras 	if (((u32 *)mem)[size / 4] != 0xdeadbeef)
9139b6b563cSPaul Mackerras 		printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
9149b6b563cSPaul Mackerras 		       ((u32 *)mem)[size / 4] );
9159b6b563cSPaul Mackerras 	*allnextp = NULL;
9169b6b563cSPaul Mackerras 
9179b6b563cSPaul Mackerras 	/* Get pointer to OF "/chosen" node for use everywhere */
9189b6b563cSPaul Mackerras 	of_chosen = of_find_node_by_path("/chosen");
919a575b807SPaul Mackerras 	if (of_chosen == NULL)
920a575b807SPaul Mackerras 		of_chosen = of_find_node_by_path("/chosen@0");
9219b6b563cSPaul Mackerras 
9229b6b563cSPaul Mackerras 	DBG(" <- unflatten_device_tree()\n");
9239b6b563cSPaul Mackerras }
9249b6b563cSPaul Mackerras 
925d205819eSPaul Mackerras /*
926d205819eSPaul Mackerras  * ibm,pa-features is a per-cpu property that contains a string of
927d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
928d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
929d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
930d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
931d205819eSPaul Mackerras  * zero is the only currently-defined value.
932d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
933d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
934d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
935d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
936d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
937d205819eSPaul Mackerras  */
938d205819eSPaul Mackerras static struct ibm_pa_feature {
939d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
940d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
941d205819eSPaul Mackerras 	unsigned char	pabyte;		/* byte number in ibm,pa-features */
942d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
943d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
944d205819eSPaul Mackerras } ibm_pa_features[] __initdata = {
945d205819eSPaul Mackerras 	{0, PPC_FEATURE_HAS_MMU,	0, 0, 0},
946d205819eSPaul Mackerras 	{0, PPC_FEATURE_HAS_FPU,	0, 1, 0},
947d205819eSPaul Mackerras 	{CPU_FTR_SLB, 0,		0, 2, 0},
948d205819eSPaul Mackerras 	{CPU_FTR_CTRL, 0,		0, 3, 0},
949d205819eSPaul Mackerras 	{CPU_FTR_NOEXECUTE, 0,		0, 6, 0},
950d205819eSPaul Mackerras 	{CPU_FTR_NODSISRALIGN, 0,	1, 1, 1},
951d205819eSPaul Mackerras 	{CPU_FTR_CI_LARGE_PAGE, 0,	1, 2, 0},
952d205819eSPaul Mackerras };
953d205819eSPaul Mackerras 
954d205819eSPaul Mackerras static void __init check_cpu_pa_features(unsigned long node)
955d205819eSPaul Mackerras {
956d205819eSPaul Mackerras 	unsigned char *pa_ftrs;
957d205819eSPaul Mackerras 	unsigned long len, tablelen, i, bit;
958d205819eSPaul Mackerras 
959d205819eSPaul Mackerras 	pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
960d205819eSPaul Mackerras 	if (pa_ftrs == NULL)
961d205819eSPaul Mackerras 		return;
962d205819eSPaul Mackerras 
963d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
964d205819eSPaul Mackerras 	for (;;) {
965d205819eSPaul Mackerras 		if (tablelen < 3)
966d205819eSPaul Mackerras 			return;
967d205819eSPaul Mackerras 		len = 2 + pa_ftrs[0];
968d205819eSPaul Mackerras 		if (tablelen < len)
969d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
970d205819eSPaul Mackerras 		if (pa_ftrs[1] == 0)
971d205819eSPaul Mackerras 			break;
972d205819eSPaul Mackerras 		tablelen -= len;
973d205819eSPaul Mackerras 		pa_ftrs += len;
974d205819eSPaul Mackerras 	}
975d205819eSPaul Mackerras 
976d205819eSPaul Mackerras 	/* loop over bits we know about */
977d205819eSPaul Mackerras 	for (i = 0; i < ARRAY_SIZE(ibm_pa_features); ++i) {
978d205819eSPaul Mackerras 		struct ibm_pa_feature *fp = &ibm_pa_features[i];
979d205819eSPaul Mackerras 
980d205819eSPaul Mackerras 		if (fp->pabyte >= pa_ftrs[0])
981d205819eSPaul Mackerras 			continue;
982d205819eSPaul Mackerras 		bit = (pa_ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
983d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
984d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
985d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
986d205819eSPaul Mackerras 		} else {
987d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
988d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
989d205819eSPaul Mackerras 		}
990d205819eSPaul Mackerras 	}
991d205819eSPaul Mackerras }
992d205819eSPaul Mackerras 
9939b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
9944df20460SAnton Blanchard 					  const char *uname, int depth,
9954df20460SAnton Blanchard 					  void *data)
9969b6b563cSPaul Mackerras {
9974df20460SAnton Blanchard 	static int logical_cpuid = 0;
9984df20460SAnton Blanchard 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
9994d177fbfSStephen Rothwell #ifdef CONFIG_ALTIVEC
10004d177fbfSStephen Rothwell 	u32 *prop;
10014d177fbfSStephen Rothwell #endif
10024d177fbfSStephen Rothwell 	u32 *intserv;
10034df20460SAnton Blanchard 	int i, nthreads;
10044df20460SAnton Blanchard 	unsigned long len;
10054df20460SAnton Blanchard 	int found = 0;
10069b6b563cSPaul Mackerras 
10079b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
10089b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
10099b6b563cSPaul Mackerras 		return 0;
10109b6b563cSPaul Mackerras 
10114df20460SAnton Blanchard 	/* Get physical cpuid */
10124df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
10134df20460SAnton Blanchard 	if (intserv) {
10144df20460SAnton Blanchard 		nthreads = len / sizeof(int);
10159b6b563cSPaul Mackerras 	} else {
10164df20460SAnton Blanchard 		intserv = of_get_flat_dt_prop(node, "reg", NULL);
10174df20460SAnton Blanchard 		nthreads = 1;
10184df20460SAnton Blanchard 	}
10194df20460SAnton Blanchard 
10204df20460SAnton Blanchard 	/*
10214df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
10224df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
10234df20460SAnton Blanchard 	 */
10244df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
10254df20460SAnton Blanchard 		/*
10264df20460SAnton Blanchard 		 * version 2 of the kexec param format adds the phys cpuid of
10274df20460SAnton Blanchard 		 * booted proc.
10284df20460SAnton Blanchard 		 */
10294df20460SAnton Blanchard 		if (initial_boot_params && initial_boot_params->version >= 2) {
10304df20460SAnton Blanchard 			if (intserv[i] ==
10314df20460SAnton Blanchard 					initial_boot_params->boot_cpuid_phys) {
10324df20460SAnton Blanchard 				found = 1;
10334df20460SAnton Blanchard 				break;
10344df20460SAnton Blanchard 			}
10354df20460SAnton Blanchard 		} else {
10364df20460SAnton Blanchard 			/*
10374df20460SAnton Blanchard 			 * Check if it's the boot-cpu, set it's hw index now,
10384df20460SAnton Blanchard 			 * unfortunately this format did not support booting
10394df20460SAnton Blanchard 			 * off secondary threads.
10404df20460SAnton Blanchard 			 */
10413c726f8dSBenjamin Herrenschmidt 			if (of_get_flat_dt_prop(node,
10423c726f8dSBenjamin Herrenschmidt 					"linux,boot-cpu", NULL) != NULL) {
10434df20460SAnton Blanchard 				found = 1;
10444df20460SAnton Blanchard 				break;
10459b6b563cSPaul Mackerras 			}
10469b6b563cSPaul Mackerras 		}
10474df20460SAnton Blanchard 
10484df20460SAnton Blanchard #ifdef CONFIG_SMP
10494df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
10504df20460SAnton Blanchard 		logical_cpuid++;
10514df20460SAnton Blanchard #endif
10524df20460SAnton Blanchard 	}
10534df20460SAnton Blanchard 
10544df20460SAnton Blanchard 	if (found) {
10554df20460SAnton Blanchard 		DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
10564df20460SAnton Blanchard 			intserv[i]);
10574df20460SAnton Blanchard 		boot_cpuid = logical_cpuid;
10584df20460SAnton Blanchard 		set_hard_smp_processor_id(boot_cpuid, intserv[i]);
10594df20460SAnton Blanchard 	}
10609b6b563cSPaul Mackerras 
10619b6b563cSPaul Mackerras #ifdef CONFIG_ALTIVEC
10629b6b563cSPaul Mackerras 	/* Check if we have a VMX and eventually update CPU features */
1063676e2497SStephen Rothwell 	prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
10649b6b563cSPaul Mackerras 	if (prop && (*prop) > 0) {
10659b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
10669b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
10679b6b563cSPaul Mackerras 	}
10689b6b563cSPaul Mackerras 
10699b6b563cSPaul Mackerras 	/* Same goes for Apple's "altivec" property */
10703c726f8dSBenjamin Herrenschmidt 	prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
10719b6b563cSPaul Mackerras 	if (prop) {
10729b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
10739b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
10749b6b563cSPaul Mackerras 	}
10759b6b563cSPaul Mackerras #endif /* CONFIG_ALTIVEC */
10769b6b563cSPaul Mackerras 
1077d205819eSPaul Mackerras 	check_cpu_pa_features(node);
1078d205819eSPaul Mackerras 
10799b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
10804df20460SAnton Blanchard 	if (nthreads > 1)
10819b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
10824df20460SAnton Blanchard 	else
10834df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
10849b6b563cSPaul Mackerras #endif
10859b6b563cSPaul Mackerras 
10869b6b563cSPaul Mackerras 	return 0;
10879b6b563cSPaul Mackerras }
10889b6b563cSPaul Mackerras 
10899b6b563cSPaul Mackerras static int __init early_init_dt_scan_chosen(unsigned long node,
10909b6b563cSPaul Mackerras 					    const char *uname, int depth, void *data)
10919b6b563cSPaul Mackerras {
10929b6b563cSPaul Mackerras 	unsigned long *lprop;
1093329dda08SKumar Gala 	unsigned long l;
1094329dda08SKumar Gala 	char *p;
10959b6b563cSPaul Mackerras 
10969b6b563cSPaul Mackerras 	DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
10979b6b563cSPaul Mackerras 
1098a575b807SPaul Mackerras 	if (depth != 1 ||
1099a575b807SPaul Mackerras 	    (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
11009b6b563cSPaul Mackerras 		return 0;
11019b6b563cSPaul Mackerras 
11029b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
11039b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
11043c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
11059b6b563cSPaul Mackerras 		iommu_is_off = 1;
11063c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
11079b6b563cSPaul Mackerras 		iommu_force_on = 1;
11089b6b563cSPaul Mackerras #endif
11099b6b563cSPaul Mackerras 
11102babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
11113c726f8dSBenjamin Herrenschmidt  	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
11129b6b563cSPaul Mackerras  	if (lprop)
11139b6b563cSPaul Mackerras  		memory_limit = *lprop;
11149b6b563cSPaul Mackerras 
11159b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
11163c726f8dSBenjamin Herrenschmidt  	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
11179b6b563cSPaul Mackerras  	if (lprop)
11189b6b563cSPaul Mackerras  		tce_alloc_start = *lprop;
11193c726f8dSBenjamin Herrenschmidt  	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
11209b6b563cSPaul Mackerras  	if (lprop)
11219b6b563cSPaul Mackerras  		tce_alloc_end = *lprop;
11229b6b563cSPaul Mackerras #endif
11239b6b563cSPaul Mackerras 
11249b6b563cSPaul Mackerras #ifdef CONFIG_PPC_RTAS
1125943ffb58SAdrian Bunk 	/* To help early debugging via the front panel, we retrieve a minimal
11269b6b563cSPaul Mackerras 	 * set of RTAS infos now if available
11279b6b563cSPaul Mackerras 	 */
11289b6b563cSPaul Mackerras 	{
1129e8222502SBenjamin Herrenschmidt 		u64 *basep, *entryp, *sizep;
11309b6b563cSPaul Mackerras 
11313c726f8dSBenjamin Herrenschmidt 		basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
11323c726f8dSBenjamin Herrenschmidt 		entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1133e8222502SBenjamin Herrenschmidt 		sizep = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
1134e8222502SBenjamin Herrenschmidt 		if (basep && entryp && sizep) {
11359b6b563cSPaul Mackerras 			rtas.base = *basep;
11369b6b563cSPaul Mackerras 			rtas.entry = *entryp;
1137e8222502SBenjamin Herrenschmidt 			rtas.size = *sizep;
11389b6b563cSPaul Mackerras 		}
11399b6b563cSPaul Mackerras 	}
11409b6b563cSPaul Mackerras #endif /* CONFIG_PPC_RTAS */
11419b6b563cSPaul Mackerras 
1142dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC
1143dcee3036SMichael Ellerman        lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
1144dcee3036SMichael Ellerman        if (lprop)
1145dcee3036SMichael Ellerman                crashk_res.start = *lprop;
1146dcee3036SMichael Ellerman 
1147dcee3036SMichael Ellerman        lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
1148dcee3036SMichael Ellerman        if (lprop)
1149dcee3036SMichael Ellerman                crashk_res.end = crashk_res.start + *lprop - 1;
1150dcee3036SMichael Ellerman #endif
1151dcee3036SMichael Ellerman 
1152329dda08SKumar Gala 	/* Retreive command line */
1153329dda08SKumar Gala  	p = of_get_flat_dt_prop(node, "bootargs", &l);
1154329dda08SKumar Gala 	if (p != NULL && l > 0)
1155329dda08SKumar Gala 		strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
1156329dda08SKumar Gala 
1157329dda08SKumar Gala #ifdef CONFIG_CMDLINE
1158329dda08SKumar Gala 	if (l == 0 || (l == 1 && (*p) == 0))
1159329dda08SKumar Gala 		strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1160329dda08SKumar Gala #endif /* CONFIG_CMDLINE */
1161329dda08SKumar Gala 
1162329dda08SKumar Gala 	DBG("Command line is: %s\n", cmd_line);
1163329dda08SKumar Gala 
11649b6b563cSPaul Mackerras 	/* break now */
11659b6b563cSPaul Mackerras 	return 1;
11669b6b563cSPaul Mackerras }
11679b6b563cSPaul Mackerras 
11689b6b563cSPaul Mackerras static int __init early_init_dt_scan_root(unsigned long node,
11699b6b563cSPaul Mackerras 					  const char *uname, int depth, void *data)
11709b6b563cSPaul Mackerras {
11719b6b563cSPaul Mackerras 	u32 *prop;
11729b6b563cSPaul Mackerras 
11739b6b563cSPaul Mackerras 	if (depth != 0)
11749b6b563cSPaul Mackerras 		return 0;
11759b6b563cSPaul Mackerras 
11763c726f8dSBenjamin Herrenschmidt 	prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
11779b6b563cSPaul Mackerras 	dt_root_size_cells = (prop == NULL) ? 1 : *prop;
11789b6b563cSPaul Mackerras 	DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
11799b6b563cSPaul Mackerras 
11803c726f8dSBenjamin Herrenschmidt 	prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
11819b6b563cSPaul Mackerras 	dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
11829b6b563cSPaul Mackerras 	DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
11839b6b563cSPaul Mackerras 
11849b6b563cSPaul Mackerras 	/* break now */
11859b6b563cSPaul Mackerras 	return 1;
11869b6b563cSPaul Mackerras }
11879b6b563cSPaul Mackerras 
11889b6b563cSPaul Mackerras static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
11899b6b563cSPaul Mackerras {
11909b6b563cSPaul Mackerras 	cell_t *p = *cellp;
11919b6b563cSPaul Mackerras 	unsigned long r;
11929b6b563cSPaul Mackerras 
11939b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
11949b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
11959b6b563cSPaul Mackerras 		p++;
11969b6b563cSPaul Mackerras 		s--;
11979b6b563cSPaul Mackerras 	}
11989b6b563cSPaul Mackerras 	r = *p++;
11999b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
12009b6b563cSPaul Mackerras 	if (s > 1) {
12019b6b563cSPaul Mackerras 		r <<= 32;
12029b6b563cSPaul Mackerras 		r |= *(p++);
12039b6b563cSPaul Mackerras 		s--;
12049b6b563cSPaul Mackerras 	}
12059b6b563cSPaul Mackerras #endif
12069b6b563cSPaul Mackerras 
12079b6b563cSPaul Mackerras 	*cellp = p;
12089b6b563cSPaul Mackerras 	return r;
12099b6b563cSPaul Mackerras }
12109b6b563cSPaul Mackerras 
12119b6b563cSPaul Mackerras 
12129b6b563cSPaul Mackerras static int __init early_init_dt_scan_memory(unsigned long node,
12139b6b563cSPaul Mackerras 					    const char *uname, int depth, void *data)
12149b6b563cSPaul Mackerras {
12153c726f8dSBenjamin Herrenschmidt 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
12169b6b563cSPaul Mackerras 	cell_t *reg, *endp;
12179b6b563cSPaul Mackerras 	unsigned long l;
12189b6b563cSPaul Mackerras 
12199b6b563cSPaul Mackerras 	/* We are scanning "memory" nodes only */
1220a23414beSPaul Mackerras 	if (type == NULL) {
1221a23414beSPaul Mackerras 		/*
1222a23414beSPaul Mackerras 		 * The longtrail doesn't have a device_type on the
1223a23414beSPaul Mackerras 		 * /memory node, so look for the node called /memory@0.
1224a23414beSPaul Mackerras 		 */
1225a23414beSPaul Mackerras 		if (depth != 1 || strcmp(uname, "memory@0") != 0)
1226a23414beSPaul Mackerras 			return 0;
1227a23414beSPaul Mackerras 	} else if (strcmp(type, "memory") != 0)
12289b6b563cSPaul Mackerras 		return 0;
12299b6b563cSPaul Mackerras 
1230ba759485SMichael Ellerman 	reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1231ba759485SMichael Ellerman 	if (reg == NULL)
12323c726f8dSBenjamin Herrenschmidt 		reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
12339b6b563cSPaul Mackerras 	if (reg == NULL)
12349b6b563cSPaul Mackerras 		return 0;
12359b6b563cSPaul Mackerras 
12369b6b563cSPaul Mackerras 	endp = reg + (l / sizeof(cell_t));
12379b6b563cSPaul Mackerras 
1238358c86fdSMichael Ellerman 	DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
12399b6b563cSPaul Mackerras 	    uname, l, reg[0], reg[1], reg[2], reg[3]);
12409b6b563cSPaul Mackerras 
12419b6b563cSPaul Mackerras 	while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
12429b6b563cSPaul Mackerras 		unsigned long base, size;
12439b6b563cSPaul Mackerras 
12449b6b563cSPaul Mackerras 		base = dt_mem_next_cell(dt_root_addr_cells, &reg);
12459b6b563cSPaul Mackerras 		size = dt_mem_next_cell(dt_root_size_cells, &reg);
12469b6b563cSPaul Mackerras 
12479b6b563cSPaul Mackerras 		if (size == 0)
12489b6b563cSPaul Mackerras 			continue;
12499b6b563cSPaul Mackerras 		DBG(" - %lx ,  %lx\n", base, size);
12509b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
12519b6b563cSPaul Mackerras 		if (iommu_is_off) {
12529b6b563cSPaul Mackerras 			if (base >= 0x80000000ul)
12539b6b563cSPaul Mackerras 				continue;
12549b6b563cSPaul Mackerras 			if ((base + size) > 0x80000000ul)
12559b6b563cSPaul Mackerras 				size = 0x80000000ul - base;
12569b6b563cSPaul Mackerras 		}
12579b6b563cSPaul Mackerras #endif
12589b6b563cSPaul Mackerras 		lmb_add(base, size);
12599b6b563cSPaul Mackerras 	}
12609b6b563cSPaul Mackerras 	return 0;
12619b6b563cSPaul Mackerras }
12629b6b563cSPaul Mackerras 
12639b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
12649b6b563cSPaul Mackerras {
1265cbbcf340SKumar Gala 	u64 base, size;
1266cbbcf340SKumar Gala 	u64 *reserve_map;
12679b6b563cSPaul Mackerras 
1268cbbcf340SKumar Gala 	reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
12699b6b563cSPaul Mackerras 					initial_boot_params->off_mem_rsvmap);
1270cbbcf340SKumar Gala #ifdef CONFIG_PPC32
1271cbbcf340SKumar Gala 	/*
1272cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
1273cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
1274cbbcf340SKumar Gala 	 */
1275cbbcf340SKumar Gala 	if (*reserve_map > 0xffffffffull) {
1276cbbcf340SKumar Gala 		u32 base_32, size_32;
1277cbbcf340SKumar Gala 		u32 *reserve_map_32 = (u32 *)reserve_map;
1278cbbcf340SKumar Gala 
1279cbbcf340SKumar Gala 		while (1) {
1280cbbcf340SKumar Gala 			base_32 = *(reserve_map_32++);
1281cbbcf340SKumar Gala 			size_32 = *(reserve_map_32++);
1282cbbcf340SKumar Gala 			if (size_32 == 0)
1283cbbcf340SKumar Gala 				break;
1284329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
1285cbbcf340SKumar Gala 			lmb_reserve(base_32, size_32);
1286cbbcf340SKumar Gala 		}
1287cbbcf340SKumar Gala 		return;
1288cbbcf340SKumar Gala 	}
1289cbbcf340SKumar Gala #endif
12909b6b563cSPaul Mackerras 	while (1) {
12919b6b563cSPaul Mackerras 		base = *(reserve_map++);
12929b6b563cSPaul Mackerras 		size = *(reserve_map++);
12939b6b563cSPaul Mackerras 		if (size == 0)
12949b6b563cSPaul Mackerras 			break;
1295cbbcf340SKumar Gala 		DBG("reserving: %llx -> %llx\n", base, size);
12969b6b563cSPaul Mackerras 		lmb_reserve(base, size);
12979b6b563cSPaul Mackerras 	}
12989b6b563cSPaul Mackerras 
12999b6b563cSPaul Mackerras #if 0
13009b6b563cSPaul Mackerras 	DBG("memory reserved, lmbs :\n");
13019b6b563cSPaul Mackerras       	lmb_dump_all();
13029b6b563cSPaul Mackerras #endif
13039b6b563cSPaul Mackerras }
13049b6b563cSPaul Mackerras 
13059b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
13069b6b563cSPaul Mackerras {
13079b6b563cSPaul Mackerras 	DBG(" -> early_init_devtree()\n");
13089b6b563cSPaul Mackerras 
13099b6b563cSPaul Mackerras 	/* Setup flat device-tree pointer */
13109b6b563cSPaul Mackerras 	initial_boot_params = params;
13119b6b563cSPaul Mackerras 
13129b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
13139b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
13149b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
13159b6b563cSPaul Mackerras 	 */
13163c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
13179b6b563cSPaul Mackerras 
13189b6b563cSPaul Mackerras 	/* Scan memory nodes and rebuild LMBs */
13199b6b563cSPaul Mackerras 	lmb_init();
13203c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
13213c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1322846f77b0SMichael Ellerman 
1323846f77b0SMichael Ellerman 	/* Save command line for /proc/cmdline and then parse parameters */
1324846f77b0SMichael Ellerman 	strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
1325846f77b0SMichael Ellerman 	parse_early_param();
1326846f77b0SMichael Ellerman 
13279b6b563cSPaul Mackerras 	/* Reserve LMB regions used by kernel, initrd, dt, etc... */
13280cc4746cSMichael Ellerman 	lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1329*47310413SMichael Ellerman 	reserve_kdump_trampoline();
13309b6b563cSPaul Mackerras 	early_reserve_mem();
13319b6b563cSPaul Mackerras 
13322babf5c2SMichael Ellerman 	lmb_enforce_memory_limit(memory_limit);
13332babf5c2SMichael Ellerman 	lmb_analyze();
13342babf5c2SMichael Ellerman 
13352babf5c2SMichael Ellerman 	DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
13362babf5c2SMichael Ellerman 
13372babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
13382babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
13392babf5c2SMichael Ellerman 	move_device_tree();
13402babf5c2SMichael Ellerman 
13419b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
13429b6b563cSPaul Mackerras 
13433c726f8dSBenjamin Herrenschmidt 	/* Retreive CPU related informations from the flat tree
13443c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
13459b6b563cSPaul Mackerras 	 */
13463c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
13479b6b563cSPaul Mackerras 
13489b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
13499b6b563cSPaul Mackerras }
13509b6b563cSPaul Mackerras 
13519b6b563cSPaul Mackerras #undef printk
13529b6b563cSPaul Mackerras 
13539b6b563cSPaul Mackerras int
13549b6b563cSPaul Mackerras prom_n_addr_cells(struct device_node* np)
13559b6b563cSPaul Mackerras {
13569b6b563cSPaul Mackerras 	int* ip;
13579b6b563cSPaul Mackerras 	do {
13589b6b563cSPaul Mackerras 		if (np->parent)
13599b6b563cSPaul Mackerras 			np = np->parent;
13609b6b563cSPaul Mackerras 		ip = (int *) get_property(np, "#address-cells", NULL);
13619b6b563cSPaul Mackerras 		if (ip != NULL)
13629b6b563cSPaul Mackerras 			return *ip;
13639b6b563cSPaul Mackerras 	} while (np->parent);
13649b6b563cSPaul Mackerras 	/* No #address-cells property for the root node, default to 1 */
13659b6b563cSPaul Mackerras 	return 1;
13669b6b563cSPaul Mackerras }
13671dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_addr_cells);
13689b6b563cSPaul Mackerras 
13699b6b563cSPaul Mackerras int
13709b6b563cSPaul Mackerras prom_n_size_cells(struct device_node* np)
13719b6b563cSPaul Mackerras {
13729b6b563cSPaul Mackerras 	int* ip;
13739b6b563cSPaul Mackerras 	do {
13749b6b563cSPaul Mackerras 		if (np->parent)
13759b6b563cSPaul Mackerras 			np = np->parent;
13769b6b563cSPaul Mackerras 		ip = (int *) get_property(np, "#size-cells", NULL);
13779b6b563cSPaul Mackerras 		if (ip != NULL)
13789b6b563cSPaul Mackerras 			return *ip;
13799b6b563cSPaul Mackerras 	} while (np->parent);
13809b6b563cSPaul Mackerras 	/* No #size-cells property for the root node, default to 1 */
13819b6b563cSPaul Mackerras 	return 1;
13829b6b563cSPaul Mackerras }
13831dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_size_cells);
13849b6b563cSPaul Mackerras 
13859b6b563cSPaul Mackerras /**
13869b6b563cSPaul Mackerras  * Work out the sense (active-low level / active-high edge)
13879b6b563cSPaul Mackerras  * of each interrupt from the device tree.
13889b6b563cSPaul Mackerras  */
13899b6b563cSPaul Mackerras void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
13909b6b563cSPaul Mackerras {
13919b6b563cSPaul Mackerras 	struct device_node *np;
13929b6b563cSPaul Mackerras 	int i, j;
13939b6b563cSPaul Mackerras 
13949b6b563cSPaul Mackerras 	/* default to level-triggered */
13956d0124fcSPaul Mackerras 	memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
13969b6b563cSPaul Mackerras 
13979b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext) {
13989b6b563cSPaul Mackerras 		for (j = 0; j < np->n_intrs; j++) {
13999b6b563cSPaul Mackerras 			i = np->intrs[j].line;
14009b6b563cSPaul Mackerras 			if (i >= off && i < max)
14016d0124fcSPaul Mackerras 				senses[i-off] = np->intrs[j].sense;
14029b6b563cSPaul Mackerras 		}
14039b6b563cSPaul Mackerras 	}
14049b6b563cSPaul Mackerras }
14059b6b563cSPaul Mackerras 
14069b6b563cSPaul Mackerras /**
14079b6b563cSPaul Mackerras  * Construct and return a list of the device_nodes with a given name.
14089b6b563cSPaul Mackerras  */
14099b6b563cSPaul Mackerras struct device_node *find_devices(const char *name)
14109b6b563cSPaul Mackerras {
14119b6b563cSPaul Mackerras 	struct device_node *head, **prevp, *np;
14129b6b563cSPaul Mackerras 
14139b6b563cSPaul Mackerras 	prevp = &head;
14149b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext) {
14159b6b563cSPaul Mackerras 		if (np->name != 0 && strcasecmp(np->name, name) == 0) {
14169b6b563cSPaul Mackerras 			*prevp = np;
14179b6b563cSPaul Mackerras 			prevp = &np->next;
14189b6b563cSPaul Mackerras 		}
14199b6b563cSPaul Mackerras 	}
14209b6b563cSPaul Mackerras 	*prevp = NULL;
14219b6b563cSPaul Mackerras 	return head;
14229b6b563cSPaul Mackerras }
14239b6b563cSPaul Mackerras EXPORT_SYMBOL(find_devices);
14249b6b563cSPaul Mackerras 
14259b6b563cSPaul Mackerras /**
14269b6b563cSPaul Mackerras  * Construct and return a list of the device_nodes with a given type.
14279b6b563cSPaul Mackerras  */
14289b6b563cSPaul Mackerras struct device_node *find_type_devices(const char *type)
14299b6b563cSPaul Mackerras {
14309b6b563cSPaul Mackerras 	struct device_node *head, **prevp, *np;
14319b6b563cSPaul Mackerras 
14329b6b563cSPaul Mackerras 	prevp = &head;
14339b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext) {
14349b6b563cSPaul Mackerras 		if (np->type != 0 && strcasecmp(np->type, type) == 0) {
14359b6b563cSPaul Mackerras 			*prevp = np;
14369b6b563cSPaul Mackerras 			prevp = &np->next;
14379b6b563cSPaul Mackerras 		}
14389b6b563cSPaul Mackerras 	}
14399b6b563cSPaul Mackerras 	*prevp = NULL;
14409b6b563cSPaul Mackerras 	return head;
14419b6b563cSPaul Mackerras }
14429b6b563cSPaul Mackerras EXPORT_SYMBOL(find_type_devices);
14439b6b563cSPaul Mackerras 
14449b6b563cSPaul Mackerras /**
14459b6b563cSPaul Mackerras  * Returns all nodes linked together
14469b6b563cSPaul Mackerras  */
14479b6b563cSPaul Mackerras struct device_node *find_all_nodes(void)
14489b6b563cSPaul Mackerras {
14499b6b563cSPaul Mackerras 	struct device_node *head, **prevp, *np;
14509b6b563cSPaul Mackerras 
14519b6b563cSPaul Mackerras 	prevp = &head;
14529b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext) {
14539b6b563cSPaul Mackerras 		*prevp = np;
14549b6b563cSPaul Mackerras 		prevp = &np->next;
14559b6b563cSPaul Mackerras 	}
14569b6b563cSPaul Mackerras 	*prevp = NULL;
14579b6b563cSPaul Mackerras 	return head;
14589b6b563cSPaul Mackerras }
14599b6b563cSPaul Mackerras EXPORT_SYMBOL(find_all_nodes);
14609b6b563cSPaul Mackerras 
14619b6b563cSPaul Mackerras /** Checks if the given "compat" string matches one of the strings in
14629b6b563cSPaul Mackerras  * the device's "compatible" property
14639b6b563cSPaul Mackerras  */
14649b6b563cSPaul Mackerras int device_is_compatible(struct device_node *device, const char *compat)
14659b6b563cSPaul Mackerras {
14669b6b563cSPaul Mackerras 	const char* cp;
14679b6b563cSPaul Mackerras 	int cplen, l;
14689b6b563cSPaul Mackerras 
14699b6b563cSPaul Mackerras 	cp = (char *) get_property(device, "compatible", &cplen);
14709b6b563cSPaul Mackerras 	if (cp == NULL)
14719b6b563cSPaul Mackerras 		return 0;
14729b6b563cSPaul Mackerras 	while (cplen > 0) {
14739b6b563cSPaul Mackerras 		if (strncasecmp(cp, compat, strlen(compat)) == 0)
14749b6b563cSPaul Mackerras 			return 1;
14759b6b563cSPaul Mackerras 		l = strlen(cp) + 1;
14769b6b563cSPaul Mackerras 		cp += l;
14779b6b563cSPaul Mackerras 		cplen -= l;
14789b6b563cSPaul Mackerras 	}
14799b6b563cSPaul Mackerras 
14809b6b563cSPaul Mackerras 	return 0;
14819b6b563cSPaul Mackerras }
14829b6b563cSPaul Mackerras EXPORT_SYMBOL(device_is_compatible);
14839b6b563cSPaul Mackerras 
14849b6b563cSPaul Mackerras 
14859b6b563cSPaul Mackerras /**
14869b6b563cSPaul Mackerras  * Indicates whether the root node has a given value in its
14879b6b563cSPaul Mackerras  * compatible property.
14889b6b563cSPaul Mackerras  */
14899b6b563cSPaul Mackerras int machine_is_compatible(const char *compat)
14909b6b563cSPaul Mackerras {
14919b6b563cSPaul Mackerras 	struct device_node *root;
14929b6b563cSPaul Mackerras 	int rc = 0;
14939b6b563cSPaul Mackerras 
14949b6b563cSPaul Mackerras 	root = of_find_node_by_path("/");
14959b6b563cSPaul Mackerras 	if (root) {
14969b6b563cSPaul Mackerras 		rc = device_is_compatible(root, compat);
14979b6b563cSPaul Mackerras 		of_node_put(root);
14989b6b563cSPaul Mackerras 	}
14999b6b563cSPaul Mackerras 	return rc;
15009b6b563cSPaul Mackerras }
15019b6b563cSPaul Mackerras EXPORT_SYMBOL(machine_is_compatible);
15029b6b563cSPaul Mackerras 
15039b6b563cSPaul Mackerras /**
15049b6b563cSPaul Mackerras  * Construct and return a list of the device_nodes with a given type
15059b6b563cSPaul Mackerras  * and compatible property.
15069b6b563cSPaul Mackerras  */
15079b6b563cSPaul Mackerras struct device_node *find_compatible_devices(const char *type,
15089b6b563cSPaul Mackerras 					    const char *compat)
15099b6b563cSPaul Mackerras {
15109b6b563cSPaul Mackerras 	struct device_node *head, **prevp, *np;
15119b6b563cSPaul Mackerras 
15129b6b563cSPaul Mackerras 	prevp = &head;
15139b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext) {
15149b6b563cSPaul Mackerras 		if (type != NULL
15159b6b563cSPaul Mackerras 		    && !(np->type != 0 && strcasecmp(np->type, type) == 0))
15169b6b563cSPaul Mackerras 			continue;
15179b6b563cSPaul Mackerras 		if (device_is_compatible(np, compat)) {
15189b6b563cSPaul Mackerras 			*prevp = np;
15199b6b563cSPaul Mackerras 			prevp = &np->next;
15209b6b563cSPaul Mackerras 		}
15219b6b563cSPaul Mackerras 	}
15229b6b563cSPaul Mackerras 	*prevp = NULL;
15239b6b563cSPaul Mackerras 	return head;
15249b6b563cSPaul Mackerras }
15259b6b563cSPaul Mackerras EXPORT_SYMBOL(find_compatible_devices);
15269b6b563cSPaul Mackerras 
15279b6b563cSPaul Mackerras /**
15289b6b563cSPaul Mackerras  * Find the device_node with a given full_name.
15299b6b563cSPaul Mackerras  */
15309b6b563cSPaul Mackerras struct device_node *find_path_device(const char *path)
15319b6b563cSPaul Mackerras {
15329b6b563cSPaul Mackerras 	struct device_node *np;
15339b6b563cSPaul Mackerras 
15349b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext)
15359b6b563cSPaul Mackerras 		if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
15369b6b563cSPaul Mackerras 			return np;
15379b6b563cSPaul Mackerras 	return NULL;
15389b6b563cSPaul Mackerras }
15399b6b563cSPaul Mackerras EXPORT_SYMBOL(find_path_device);
15409b6b563cSPaul Mackerras 
15419b6b563cSPaul Mackerras /*******
15429b6b563cSPaul Mackerras  *
15439b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
15449b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
15459b6b563cSPaul Mackerras  * are protected by a rw_lock.
15469b6b563cSPaul Mackerras  *
15479b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
15489b6b563cSPaul Mackerras  * this isn't dealt with yet.
15499b6b563cSPaul Mackerras  *
15509b6b563cSPaul Mackerras  *******/
15519b6b563cSPaul Mackerras 
15529b6b563cSPaul Mackerras /**
15539b6b563cSPaul Mackerras  *	of_find_node_by_name - Find a node by its "name" property
15549b6b563cSPaul Mackerras  *	@from:	The node to start searching from or NULL, the node
15559b6b563cSPaul Mackerras  *		you pass will not be searched, only the next one
15569b6b563cSPaul Mackerras  *		will; typically, you pass what the previous call
15579b6b563cSPaul Mackerras  *		returned. of_node_put() will be called on it
15589b6b563cSPaul Mackerras  *	@name:	The name string to match against
15599b6b563cSPaul Mackerras  *
15609b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
15619b6b563cSPaul Mackerras  *	of_node_put() on it when done.
15629b6b563cSPaul Mackerras  */
15639b6b563cSPaul Mackerras struct device_node *of_find_node_by_name(struct device_node *from,
15649b6b563cSPaul Mackerras 	const char *name)
15659b6b563cSPaul Mackerras {
15669b6b563cSPaul Mackerras 	struct device_node *np;
15679b6b563cSPaul Mackerras 
15689b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
15699b6b563cSPaul Mackerras 	np = from ? from->allnext : allnodes;
1570090db7c8SOlaf Hering 	for (; np != NULL; np = np->allnext)
1571090db7c8SOlaf Hering 		if (np->name != NULL && strcasecmp(np->name, name) == 0
15729b6b563cSPaul Mackerras 		    && of_node_get(np))
15739b6b563cSPaul Mackerras 			break;
15749b6b563cSPaul Mackerras 	if (from)
15759b6b563cSPaul Mackerras 		of_node_put(from);
15769b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
15779b6b563cSPaul Mackerras 	return np;
15789b6b563cSPaul Mackerras }
15799b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_name);
15809b6b563cSPaul Mackerras 
15819b6b563cSPaul Mackerras /**
15829b6b563cSPaul Mackerras  *	of_find_node_by_type - Find a node by its "device_type" property
15839b6b563cSPaul Mackerras  *	@from:	The node to start searching from or NULL, the node
15849b6b563cSPaul Mackerras  *		you pass will not be searched, only the next one
15859b6b563cSPaul Mackerras  *		will; typically, you pass what the previous call
15869b6b563cSPaul Mackerras  *		returned. of_node_put() will be called on it
15879b6b563cSPaul Mackerras  *	@name:	The type string to match against
15889b6b563cSPaul Mackerras  *
15899b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
15909b6b563cSPaul Mackerras  *	of_node_put() on it when done.
15919b6b563cSPaul Mackerras  */
15929b6b563cSPaul Mackerras struct device_node *of_find_node_by_type(struct device_node *from,
15939b6b563cSPaul Mackerras 	const char *type)
15949b6b563cSPaul Mackerras {
15959b6b563cSPaul Mackerras 	struct device_node *np;
15969b6b563cSPaul Mackerras 
15979b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
15989b6b563cSPaul Mackerras 	np = from ? from->allnext : allnodes;
15999b6b563cSPaul Mackerras 	for (; np != 0; np = np->allnext)
16009b6b563cSPaul Mackerras 		if (np->type != 0 && strcasecmp(np->type, type) == 0
16019b6b563cSPaul Mackerras 		    && of_node_get(np))
16029b6b563cSPaul Mackerras 			break;
16039b6b563cSPaul Mackerras 	if (from)
16049b6b563cSPaul Mackerras 		of_node_put(from);
16059b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
16069b6b563cSPaul Mackerras 	return np;
16079b6b563cSPaul Mackerras }
16089b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_type);
16099b6b563cSPaul Mackerras 
16109b6b563cSPaul Mackerras /**
16119b6b563cSPaul Mackerras  *	of_find_compatible_node - Find a node based on type and one of the
16129b6b563cSPaul Mackerras  *                                tokens in its "compatible" property
16139b6b563cSPaul Mackerras  *	@from:		The node to start searching from or NULL, the node
16149b6b563cSPaul Mackerras  *			you pass will not be searched, only the next one
16159b6b563cSPaul Mackerras  *			will; typically, you pass what the previous call
16169b6b563cSPaul Mackerras  *			returned. of_node_put() will be called on it
16179b6b563cSPaul Mackerras  *	@type:		The type string to match "device_type" or NULL to ignore
16189b6b563cSPaul Mackerras  *	@compatible:	The string to match to one of the tokens in the device
16199b6b563cSPaul Mackerras  *			"compatible" list.
16209b6b563cSPaul Mackerras  *
16219b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
16229b6b563cSPaul Mackerras  *	of_node_put() on it when done.
16239b6b563cSPaul Mackerras  */
16249b6b563cSPaul Mackerras struct device_node *of_find_compatible_node(struct device_node *from,
16259b6b563cSPaul Mackerras 	const char *type, const char *compatible)
16269b6b563cSPaul Mackerras {
16279b6b563cSPaul Mackerras 	struct device_node *np;
16289b6b563cSPaul Mackerras 
16299b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
16309b6b563cSPaul Mackerras 	np = from ? from->allnext : allnodes;
16319b6b563cSPaul Mackerras 	for (; np != 0; np = np->allnext) {
16329b6b563cSPaul Mackerras 		if (type != NULL
16339b6b563cSPaul Mackerras 		    && !(np->type != 0 && strcasecmp(np->type, type) == 0))
16349b6b563cSPaul Mackerras 			continue;
16359b6b563cSPaul Mackerras 		if (device_is_compatible(np, compatible) && of_node_get(np))
16369b6b563cSPaul Mackerras 			break;
16379b6b563cSPaul Mackerras 	}
16389b6b563cSPaul Mackerras 	if (from)
16399b6b563cSPaul Mackerras 		of_node_put(from);
16409b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
16419b6b563cSPaul Mackerras 	return np;
16429b6b563cSPaul Mackerras }
16439b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_compatible_node);
16449b6b563cSPaul Mackerras 
16459b6b563cSPaul Mackerras /**
16469b6b563cSPaul Mackerras  *	of_find_node_by_path - Find a node matching a full OF path
16479b6b563cSPaul Mackerras  *	@path:	The full path to match
16489b6b563cSPaul Mackerras  *
16499b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
16509b6b563cSPaul Mackerras  *	of_node_put() on it when done.
16519b6b563cSPaul Mackerras  */
16529b6b563cSPaul Mackerras struct device_node *of_find_node_by_path(const char *path)
16539b6b563cSPaul Mackerras {
16549b6b563cSPaul Mackerras 	struct device_node *np = allnodes;
16559b6b563cSPaul Mackerras 
16569b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
16579b6b563cSPaul Mackerras 	for (; np != 0; np = np->allnext) {
16589b6b563cSPaul Mackerras 		if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
16599b6b563cSPaul Mackerras 		    && of_node_get(np))
16609b6b563cSPaul Mackerras 			break;
16619b6b563cSPaul Mackerras 	}
16629b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
16639b6b563cSPaul Mackerras 	return np;
16649b6b563cSPaul Mackerras }
16659b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_path);
16669b6b563cSPaul Mackerras 
16679b6b563cSPaul Mackerras /**
16689b6b563cSPaul Mackerras  *	of_find_node_by_phandle - Find a node given a phandle
16699b6b563cSPaul Mackerras  *	@handle:	phandle of the node to find
16709b6b563cSPaul Mackerras  *
16719b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
16729b6b563cSPaul Mackerras  *	of_node_put() on it when done.
16739b6b563cSPaul Mackerras  */
16749b6b563cSPaul Mackerras struct device_node *of_find_node_by_phandle(phandle handle)
16759b6b563cSPaul Mackerras {
16769b6b563cSPaul Mackerras 	struct device_node *np;
16779b6b563cSPaul Mackerras 
16789b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
16799b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext)
16809b6b563cSPaul Mackerras 		if (np->linux_phandle == handle)
16819b6b563cSPaul Mackerras 			break;
16829b6b563cSPaul Mackerras 	if (np)
16839b6b563cSPaul Mackerras 		of_node_get(np);
16849b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
16859b6b563cSPaul Mackerras 	return np;
16869b6b563cSPaul Mackerras }
16879b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_phandle);
16889b6b563cSPaul Mackerras 
16899b6b563cSPaul Mackerras /**
16909b6b563cSPaul Mackerras  *	of_find_all_nodes - Get next node in global list
16919b6b563cSPaul Mackerras  *	@prev:	Previous node or NULL to start iteration
16929b6b563cSPaul Mackerras  *		of_node_put() will be called on it
16939b6b563cSPaul Mackerras  *
16949b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
16959b6b563cSPaul Mackerras  *	of_node_put() on it when done.
16969b6b563cSPaul Mackerras  */
16979b6b563cSPaul Mackerras struct device_node *of_find_all_nodes(struct device_node *prev)
16989b6b563cSPaul Mackerras {
16999b6b563cSPaul Mackerras 	struct device_node *np;
17009b6b563cSPaul Mackerras 
17019b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
17029b6b563cSPaul Mackerras 	np = prev ? prev->allnext : allnodes;
17039b6b563cSPaul Mackerras 	for (; np != 0; np = np->allnext)
17049b6b563cSPaul Mackerras 		if (of_node_get(np))
17059b6b563cSPaul Mackerras 			break;
17069b6b563cSPaul Mackerras 	if (prev)
17079b6b563cSPaul Mackerras 		of_node_put(prev);
17089b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
17099b6b563cSPaul Mackerras 	return np;
17109b6b563cSPaul Mackerras }
17119b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_all_nodes);
17129b6b563cSPaul Mackerras 
17139b6b563cSPaul Mackerras /**
17149b6b563cSPaul Mackerras  *	of_get_parent - Get a node's parent if any
17159b6b563cSPaul Mackerras  *	@node:	Node to get parent
17169b6b563cSPaul Mackerras  *
17179b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
17189b6b563cSPaul Mackerras  *	of_node_put() on it when done.
17199b6b563cSPaul Mackerras  */
17209b6b563cSPaul Mackerras struct device_node *of_get_parent(const struct device_node *node)
17219b6b563cSPaul Mackerras {
17229b6b563cSPaul Mackerras 	struct device_node *np;
17239b6b563cSPaul Mackerras 
17249b6b563cSPaul Mackerras 	if (!node)
17259b6b563cSPaul Mackerras 		return NULL;
17269b6b563cSPaul Mackerras 
17279b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
17289b6b563cSPaul Mackerras 	np = of_node_get(node->parent);
17299b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
17309b6b563cSPaul Mackerras 	return np;
17319b6b563cSPaul Mackerras }
17329b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_parent);
17339b6b563cSPaul Mackerras 
17349b6b563cSPaul Mackerras /**
17359b6b563cSPaul Mackerras  *	of_get_next_child - Iterate a node childs
17369b6b563cSPaul Mackerras  *	@node:	parent node
17379b6b563cSPaul Mackerras  *	@prev:	previous child of the parent node, or NULL to get first
17389b6b563cSPaul Mackerras  *
17399b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
17409b6b563cSPaul Mackerras  *	of_node_put() on it when done.
17419b6b563cSPaul Mackerras  */
17429b6b563cSPaul Mackerras struct device_node *of_get_next_child(const struct device_node *node,
17439b6b563cSPaul Mackerras 	struct device_node *prev)
17449b6b563cSPaul Mackerras {
17459b6b563cSPaul Mackerras 	struct device_node *next;
17469b6b563cSPaul Mackerras 
17479b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
17489b6b563cSPaul Mackerras 	next = prev ? prev->sibling : node->child;
17499b6b563cSPaul Mackerras 	for (; next != 0; next = next->sibling)
17509b6b563cSPaul Mackerras 		if (of_node_get(next))
17519b6b563cSPaul Mackerras 			break;
17529b6b563cSPaul Mackerras 	if (prev)
17539b6b563cSPaul Mackerras 		of_node_put(prev);
17549b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
17559b6b563cSPaul Mackerras 	return next;
17569b6b563cSPaul Mackerras }
17579b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_next_child);
17589b6b563cSPaul Mackerras 
17599b6b563cSPaul Mackerras /**
17609b6b563cSPaul Mackerras  *	of_node_get - Increment refcount of a node
17619b6b563cSPaul Mackerras  *	@node:	Node to inc refcount, NULL is supported to
17629b6b563cSPaul Mackerras  *		simplify writing of callers
17639b6b563cSPaul Mackerras  *
17649b6b563cSPaul Mackerras  *	Returns node.
17659b6b563cSPaul Mackerras  */
17669b6b563cSPaul Mackerras struct device_node *of_node_get(struct device_node *node)
17679b6b563cSPaul Mackerras {
17689b6b563cSPaul Mackerras 	if (node)
17699b6b563cSPaul Mackerras 		kref_get(&node->kref);
17709b6b563cSPaul Mackerras 	return node;
17719b6b563cSPaul Mackerras }
17729b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_get);
17739b6b563cSPaul Mackerras 
17749b6b563cSPaul Mackerras static inline struct device_node * kref_to_device_node(struct kref *kref)
17759b6b563cSPaul Mackerras {
17769b6b563cSPaul Mackerras 	return container_of(kref, struct device_node, kref);
17779b6b563cSPaul Mackerras }
17789b6b563cSPaul Mackerras 
17799b6b563cSPaul Mackerras /**
17809b6b563cSPaul Mackerras  *	of_node_release - release a dynamically allocated node
17819b6b563cSPaul Mackerras  *	@kref:  kref element of the node to be released
17829b6b563cSPaul Mackerras  *
17839b6b563cSPaul Mackerras  *	In of_node_put() this function is passed to kref_put()
17849b6b563cSPaul Mackerras  *	as the destructor.
17859b6b563cSPaul Mackerras  */
17869b6b563cSPaul Mackerras static void of_node_release(struct kref *kref)
17879b6b563cSPaul Mackerras {
17889b6b563cSPaul Mackerras 	struct device_node *node = kref_to_device_node(kref);
17899b6b563cSPaul Mackerras 	struct property *prop = node->properties;
17909b6b563cSPaul Mackerras 
17919b6b563cSPaul Mackerras 	if (!OF_IS_DYNAMIC(node))
17929b6b563cSPaul Mackerras 		return;
17939b6b563cSPaul Mackerras 	while (prop) {
17949b6b563cSPaul Mackerras 		struct property *next = prop->next;
17959b6b563cSPaul Mackerras 		kfree(prop->name);
17969b6b563cSPaul Mackerras 		kfree(prop->value);
17979b6b563cSPaul Mackerras 		kfree(prop);
17989b6b563cSPaul Mackerras 		prop = next;
1799088186deSDave C Boutcher 
1800088186deSDave C Boutcher 		if (!prop) {
1801088186deSDave C Boutcher 			prop = node->deadprops;
1802088186deSDave C Boutcher 			node->deadprops = NULL;
1803088186deSDave C Boutcher 		}
18049b6b563cSPaul Mackerras 	}
18059b6b563cSPaul Mackerras 	kfree(node->intrs);
18069b6b563cSPaul Mackerras 	kfree(node->full_name);
18079b6b563cSPaul Mackerras 	kfree(node->data);
18089b6b563cSPaul Mackerras 	kfree(node);
18099b6b563cSPaul Mackerras }
18109b6b563cSPaul Mackerras 
18119b6b563cSPaul Mackerras /**
18129b6b563cSPaul Mackerras  *	of_node_put - Decrement refcount of a node
18139b6b563cSPaul Mackerras  *	@node:	Node to dec refcount, NULL is supported to
18149b6b563cSPaul Mackerras  *		simplify writing of callers
18159b6b563cSPaul Mackerras  *
18169b6b563cSPaul Mackerras  */
18179b6b563cSPaul Mackerras void of_node_put(struct device_node *node)
18189b6b563cSPaul Mackerras {
18199b6b563cSPaul Mackerras 	if (node)
18209b6b563cSPaul Mackerras 		kref_put(&node->kref, of_node_release);
18219b6b563cSPaul Mackerras }
18229b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_put);
18239b6b563cSPaul Mackerras 
18249b6b563cSPaul Mackerras /*
18259b6b563cSPaul Mackerras  * Plug a device node into the tree and global list.
18269b6b563cSPaul Mackerras  */
18279b6b563cSPaul Mackerras void of_attach_node(struct device_node *np)
18289b6b563cSPaul Mackerras {
18299b6b563cSPaul Mackerras 	write_lock(&devtree_lock);
18309b6b563cSPaul Mackerras 	np->sibling = np->parent->child;
18319b6b563cSPaul Mackerras 	np->allnext = allnodes;
18329b6b563cSPaul Mackerras 	np->parent->child = np;
18339b6b563cSPaul Mackerras 	allnodes = np;
18349b6b563cSPaul Mackerras 	write_unlock(&devtree_lock);
18359b6b563cSPaul Mackerras }
18369b6b563cSPaul Mackerras 
18379b6b563cSPaul Mackerras /*
18389b6b563cSPaul Mackerras  * "Unplug" a node from the device tree.  The caller must hold
18399b6b563cSPaul Mackerras  * a reference to the node.  The memory associated with the node
18409b6b563cSPaul Mackerras  * is not freed until its refcount goes to zero.
18419b6b563cSPaul Mackerras  */
18429b6b563cSPaul Mackerras void of_detach_node(const struct device_node *np)
18439b6b563cSPaul Mackerras {
18449b6b563cSPaul Mackerras 	struct device_node *parent;
18459b6b563cSPaul Mackerras 
18469b6b563cSPaul Mackerras 	write_lock(&devtree_lock);
18479b6b563cSPaul Mackerras 
18489b6b563cSPaul Mackerras 	parent = np->parent;
18499b6b563cSPaul Mackerras 
18509b6b563cSPaul Mackerras 	if (allnodes == np)
18519b6b563cSPaul Mackerras 		allnodes = np->allnext;
18529b6b563cSPaul Mackerras 	else {
18539b6b563cSPaul Mackerras 		struct device_node *prev;
18549b6b563cSPaul Mackerras 		for (prev = allnodes;
18559b6b563cSPaul Mackerras 		     prev->allnext != np;
18569b6b563cSPaul Mackerras 		     prev = prev->allnext)
18579b6b563cSPaul Mackerras 			;
18589b6b563cSPaul Mackerras 		prev->allnext = np->allnext;
18599b6b563cSPaul Mackerras 	}
18609b6b563cSPaul Mackerras 
18619b6b563cSPaul Mackerras 	if (parent->child == np)
18629b6b563cSPaul Mackerras 		parent->child = np->sibling;
18639b6b563cSPaul Mackerras 	else {
18649b6b563cSPaul Mackerras 		struct device_node *prevsib;
18659b6b563cSPaul Mackerras 		for (prevsib = np->parent->child;
18669b6b563cSPaul Mackerras 		     prevsib->sibling != np;
18679b6b563cSPaul Mackerras 		     prevsib = prevsib->sibling)
18689b6b563cSPaul Mackerras 			;
18699b6b563cSPaul Mackerras 		prevsib->sibling = np->sibling;
18709b6b563cSPaul Mackerras 	}
18719b6b563cSPaul Mackerras 
18729b6b563cSPaul Mackerras 	write_unlock(&devtree_lock);
18739b6b563cSPaul Mackerras }
18749b6b563cSPaul Mackerras 
18759b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
18769b6b563cSPaul Mackerras /*
18779b6b563cSPaul Mackerras  * Fix up the uninitialized fields in a new device node:
18789b6b563cSPaul Mackerras  * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
18799b6b563cSPaul Mackerras  *
18809b6b563cSPaul Mackerras  * A lot of boot-time code is duplicated here, because functions such
18819b6b563cSPaul Mackerras  * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
18829b6b563cSPaul Mackerras  * slab allocator.
18839b6b563cSPaul Mackerras  *
18849b6b563cSPaul Mackerras  * This should probably be split up into smaller chunks.
18859b6b563cSPaul Mackerras  */
18869b6b563cSPaul Mackerras 
1887cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node)
18889b6b563cSPaul Mackerras {
18899b6b563cSPaul Mackerras 	struct device_node *parent = of_get_parent(node);
18909b6b563cSPaul Mackerras 	int err = 0;
18919b6b563cSPaul Mackerras 	phandle *ibm_phandle;
18929b6b563cSPaul Mackerras 
18939b6b563cSPaul Mackerras 	node->name = get_property(node, "name", NULL);
18949b6b563cSPaul Mackerras 	node->type = get_property(node, "device_type", NULL);
18959b6b563cSPaul Mackerras 
18969b6b563cSPaul Mackerras 	if (!parent) {
18979b6b563cSPaul Mackerras 		err = -ENODEV;
18989b6b563cSPaul Mackerras 		goto out;
18999b6b563cSPaul Mackerras 	}
19009b6b563cSPaul Mackerras 
19019b6b563cSPaul Mackerras 	/* We don't support that function on PowerMac, at least
19029b6b563cSPaul Mackerras 	 * not yet
19039b6b563cSPaul Mackerras 	 */
1904e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac))
19059b6b563cSPaul Mackerras 		return -ENODEV;
19069b6b563cSPaul Mackerras 
19079b6b563cSPaul Mackerras 	/* fix up new node's linux_phandle field */
1908cc5d0189SBenjamin Herrenschmidt 	if ((ibm_phandle = (unsigned int *)get_property(node,
1909cc5d0189SBenjamin Herrenschmidt 							"ibm,phandle", NULL)))
19109b6b563cSPaul Mackerras 		node->linux_phandle = *ibm_phandle;
19119b6b563cSPaul Mackerras 
19129b6b563cSPaul Mackerras out:
19139b6b563cSPaul Mackerras 	of_node_put(parent);
19149b6b563cSPaul Mackerras 	return err;
19159b6b563cSPaul Mackerras }
19169b6b563cSPaul Mackerras 
19179b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb,
19189b6b563cSPaul Mackerras 				  unsigned long action, void *node)
19199b6b563cSPaul Mackerras {
19209b6b563cSPaul Mackerras 	int err;
19219b6b563cSPaul Mackerras 
19229b6b563cSPaul Mackerras 	switch (action) {
19239b6b563cSPaul Mackerras 	case PSERIES_RECONFIG_ADD:
1924cc5d0189SBenjamin Herrenschmidt 		err = of_finish_dynamic_node(node);
1925cc5d0189SBenjamin Herrenschmidt 		if (!err)
1926cc5d0189SBenjamin Herrenschmidt 			finish_node(node, NULL, 0);
19279b6b563cSPaul Mackerras 		if (err < 0) {
19289b6b563cSPaul Mackerras 			printk(KERN_ERR "finish_node returned %d\n", err);
19299b6b563cSPaul Mackerras 			err = NOTIFY_BAD;
19309b6b563cSPaul Mackerras 		}
19319b6b563cSPaul Mackerras 		break;
19329b6b563cSPaul Mackerras 	default:
19339b6b563cSPaul Mackerras 		err = NOTIFY_DONE;
19349b6b563cSPaul Mackerras 		break;
19359b6b563cSPaul Mackerras 	}
19369b6b563cSPaul Mackerras 	return err;
19379b6b563cSPaul Mackerras }
19389b6b563cSPaul Mackerras 
19399b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = {
19409b6b563cSPaul Mackerras 	.notifier_call = prom_reconfig_notifier,
19419b6b563cSPaul Mackerras 	.priority = 10, /* This one needs to run first */
19429b6b563cSPaul Mackerras };
19439b6b563cSPaul Mackerras 
19449b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void)
19459b6b563cSPaul Mackerras {
19469b6b563cSPaul Mackerras 	return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
19479b6b563cSPaul Mackerras }
19489b6b563cSPaul Mackerras __initcall(prom_reconfig_setup);
19499b6b563cSPaul Mackerras #endif
19509b6b563cSPaul Mackerras 
1951ecaa8b0fSDave C Boutcher struct property *of_find_property(struct device_node *np, const char *name,
19529b6b563cSPaul Mackerras 				  int *lenp)
19539b6b563cSPaul Mackerras {
19549b6b563cSPaul Mackerras 	struct property *pp;
19559b6b563cSPaul Mackerras 
1956088186deSDave C Boutcher 	read_lock(&devtree_lock);
19579b6b563cSPaul Mackerras 	for (pp = np->properties; pp != 0; pp = pp->next)
19589b6b563cSPaul Mackerras 		if (strcmp(pp->name, name) == 0) {
19599b6b563cSPaul Mackerras 			if (lenp != 0)
19609b6b563cSPaul Mackerras 				*lenp = pp->length;
1961088186deSDave C Boutcher 			break;
19629b6b563cSPaul Mackerras 		}
1963088186deSDave C Boutcher 	read_unlock(&devtree_lock);
1964088186deSDave C Boutcher 
1965ecaa8b0fSDave C Boutcher 	return pp;
1966ecaa8b0fSDave C Boutcher }
1967ecaa8b0fSDave C Boutcher 
1968ecaa8b0fSDave C Boutcher /*
1969ecaa8b0fSDave C Boutcher  * Find a property with a given name for a given node
1970ecaa8b0fSDave C Boutcher  * and return the value.
1971ecaa8b0fSDave C Boutcher  */
1972ecaa8b0fSDave C Boutcher unsigned char *get_property(struct device_node *np, const char *name,
1973ecaa8b0fSDave C Boutcher 			    int *lenp)
1974ecaa8b0fSDave C Boutcher {
1975ecaa8b0fSDave C Boutcher 	struct property *pp = of_find_property(np,name,lenp);
1976088186deSDave C Boutcher 	return pp ? pp->value : NULL;
19779b6b563cSPaul Mackerras }
19789b6b563cSPaul Mackerras EXPORT_SYMBOL(get_property);
19799b6b563cSPaul Mackerras 
19809b6b563cSPaul Mackerras /*
19819b6b563cSPaul Mackerras  * Add a property to a node
19829b6b563cSPaul Mackerras  */
1983183d0202SBenjamin Herrenschmidt int prom_add_property(struct device_node* np, struct property* prop)
19849b6b563cSPaul Mackerras {
1985183d0202SBenjamin Herrenschmidt 	struct property **next;
19869b6b563cSPaul Mackerras 
19879b6b563cSPaul Mackerras 	prop->next = NULL;
1988183d0202SBenjamin Herrenschmidt 	write_lock(&devtree_lock);
1989183d0202SBenjamin Herrenschmidt 	next = &np->properties;
1990183d0202SBenjamin Herrenschmidt 	while (*next) {
1991183d0202SBenjamin Herrenschmidt 		if (strcmp(prop->name, (*next)->name) == 0) {
1992183d0202SBenjamin Herrenschmidt 			/* duplicate ! don't insert it */
1993183d0202SBenjamin Herrenschmidt 			write_unlock(&devtree_lock);
1994183d0202SBenjamin Herrenschmidt 			return -1;
1995183d0202SBenjamin Herrenschmidt 		}
19969b6b563cSPaul Mackerras 		next = &(*next)->next;
1997183d0202SBenjamin Herrenschmidt 	}
19989b6b563cSPaul Mackerras 	*next = prop;
1999183d0202SBenjamin Herrenschmidt 	write_unlock(&devtree_lock);
2000183d0202SBenjamin Herrenschmidt 
2001799d6046SPaul Mackerras #ifdef CONFIG_PROC_DEVICETREE
2002183d0202SBenjamin Herrenschmidt 	/* try to add to proc as well if it was initialized */
2003183d0202SBenjamin Herrenschmidt 	if (np->pde)
2004183d0202SBenjamin Herrenschmidt 		proc_device_tree_add_prop(np->pde, prop);
2005799d6046SPaul Mackerras #endif /* CONFIG_PROC_DEVICETREE */
2006183d0202SBenjamin Herrenschmidt 
2007183d0202SBenjamin Herrenschmidt 	return 0;
20089b6b563cSPaul Mackerras }
20099b6b563cSPaul Mackerras 
2010088186deSDave C Boutcher /*
2011088186deSDave C Boutcher  * Remove a property from a node.  Note that we don't actually
2012088186deSDave C Boutcher  * remove it, since we have given out who-knows-how-many pointers
2013088186deSDave C Boutcher  * to the data using get-property.  Instead we just move the property
2014088186deSDave C Boutcher  * to the "dead properties" list, so it won't be found any more.
2015088186deSDave C Boutcher  */
2016088186deSDave C Boutcher int prom_remove_property(struct device_node *np, struct property *prop)
2017088186deSDave C Boutcher {
2018088186deSDave C Boutcher 	struct property **next;
2019088186deSDave C Boutcher 	int found = 0;
20209b6b563cSPaul Mackerras 
2021088186deSDave C Boutcher 	write_lock(&devtree_lock);
2022088186deSDave C Boutcher 	next = &np->properties;
2023088186deSDave C Boutcher 	while (*next) {
2024088186deSDave C Boutcher 		if (*next == prop) {
2025088186deSDave C Boutcher 			/* found the node */
2026088186deSDave C Boutcher 			*next = prop->next;
2027088186deSDave C Boutcher 			prop->next = np->deadprops;
2028088186deSDave C Boutcher 			np->deadprops = prop;
2029088186deSDave C Boutcher 			found = 1;
2030088186deSDave C Boutcher 			break;
2031088186deSDave C Boutcher 		}
2032088186deSDave C Boutcher 		next = &(*next)->next;
2033088186deSDave C Boutcher 	}
2034088186deSDave C Boutcher 	write_unlock(&devtree_lock);
2035088186deSDave C Boutcher 
2036088186deSDave C Boutcher 	if (!found)
2037088186deSDave C Boutcher 		return -ENODEV;
2038088186deSDave C Boutcher 
2039088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE
2040088186deSDave C Boutcher 	/* try to remove the proc node as well */
2041088186deSDave C Boutcher 	if (np->pde)
2042088186deSDave C Boutcher 		proc_device_tree_remove_prop(np->pde, prop);
2043088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */
2044088186deSDave C Boutcher 
2045088186deSDave C Boutcher 	return 0;
2046088186deSDave C Boutcher }
2047088186deSDave C Boutcher 
2048088186deSDave C Boutcher /*
2049088186deSDave C Boutcher  * Update a property in a node.  Note that we don't actually
2050088186deSDave C Boutcher  * remove it, since we have given out who-knows-how-many pointers
2051088186deSDave C Boutcher  * to the data using get-property.  Instead we just move the property
2052088186deSDave C Boutcher  * to the "dead properties" list, and add the new property to the
2053088186deSDave C Boutcher  * property list
2054088186deSDave C Boutcher  */
2055088186deSDave C Boutcher int prom_update_property(struct device_node *np,
2056088186deSDave C Boutcher 			 struct property *newprop,
2057088186deSDave C Boutcher 			 struct property *oldprop)
2058088186deSDave C Boutcher {
2059088186deSDave C Boutcher 	struct property **next;
2060088186deSDave C Boutcher 	int found = 0;
2061088186deSDave C Boutcher 
2062088186deSDave C Boutcher 	write_lock(&devtree_lock);
2063088186deSDave C Boutcher 	next = &np->properties;
2064088186deSDave C Boutcher 	while (*next) {
2065088186deSDave C Boutcher 		if (*next == oldprop) {
2066088186deSDave C Boutcher 			/* found the node */
2067088186deSDave C Boutcher 			newprop->next = oldprop->next;
2068088186deSDave C Boutcher 			*next = newprop;
2069088186deSDave C Boutcher 			oldprop->next = np->deadprops;
2070088186deSDave C Boutcher 			np->deadprops = oldprop;
2071088186deSDave C Boutcher 			found = 1;
2072088186deSDave C Boutcher 			break;
2073088186deSDave C Boutcher 		}
2074088186deSDave C Boutcher 		next = &(*next)->next;
2075088186deSDave C Boutcher 	}
2076088186deSDave C Boutcher 	write_unlock(&devtree_lock);
2077088186deSDave C Boutcher 
2078088186deSDave C Boutcher 	if (!found)
2079088186deSDave C Boutcher 		return -ENODEV;
2080088186deSDave C Boutcher 
2081088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE
2082088186deSDave C Boutcher 	/* try to add to proc as well if it was initialized */
2083088186deSDave C Boutcher 	if (np->pde)
2084088186deSDave C Boutcher 		proc_device_tree_update_prop(np->pde, newprop, oldprop);
2085088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */
2086088186deSDave C Boutcher 
2087088186deSDave C Boutcher 	return 0;
2088088186deSDave C Boutcher }
2089b68239eeSMichael Ellerman 
2090