xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision 3eb906c6b6c123513718e7742a96a4189f900382)
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/kernel.h>
209b6b563cSPaul Mackerras #include <linux/string.h>
219b6b563cSPaul Mackerras #include <linux/init.h>
229b6b563cSPaul Mackerras #include <linux/threads.h>
239b6b563cSPaul Mackerras #include <linux/spinlock.h>
249b6b563cSPaul Mackerras #include <linux/types.h>
259b6b563cSPaul Mackerras #include <linux/pci.h>
269b6b563cSPaul Mackerras #include <linux/stringify.h>
279b6b563cSPaul Mackerras #include <linux/delay.h>
289b6b563cSPaul Mackerras #include <linux/initrd.h>
299b6b563cSPaul Mackerras #include <linux/bitops.h>
304b16f8e2SPaul Gortmaker #include <linux/export.h>
31dcee3036SMichael Ellerman #include <linux/kexec.h>
327a4571aeSMichael Ellerman #include <linux/debugfs.h>
330ebfff14SBenjamin Herrenschmidt #include <linux/irq.h>
3495f72d1eSYinghai Lu #include <linux/memblock.h>
351cf3d8b3SNathan Fontenot #include <linux/of.h>
369b6b563cSPaul Mackerras 
379b6b563cSPaul Mackerras #include <asm/prom.h>
389b6b563cSPaul Mackerras #include <asm/rtas.h>
399b6b563cSPaul Mackerras #include <asm/page.h>
409b6b563cSPaul Mackerras #include <asm/processor.h>
419b6b563cSPaul Mackerras #include <asm/irq.h>
429b6b563cSPaul Mackerras #include <asm/io.h>
430cc4746cSMichael Ellerman #include <asm/kdump.h>
449b6b563cSPaul Mackerras #include <asm/smp.h>
459b6b563cSPaul Mackerras #include <asm/mmu.h>
461426d5a3SMichael Ellerman #include <asm/paca.h>
479b6b563cSPaul Mackerras #include <asm/pgtable.h>
489b6b563cSPaul Mackerras #include <asm/pci.h>
499b6b563cSPaul Mackerras #include <asm/iommu.h>
509b6b563cSPaul Mackerras #include <asm/btext.h>
519b6b563cSPaul Mackerras #include <asm/sections.h>
529b6b563cSPaul Mackerras #include <asm/machdep.h>
5340ef8cbcSPaul Mackerras #include <asm/pci-bridge.h>
542babf5c2SMichael Ellerman #include <asm/kexec.h>
5514a43e69SBenjamin Herrenschmidt #include <asm/opal.h>
56eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
570dc3289cSTony Breeds #include <asm/debug.h>
5814a43e69SBenjamin Herrenschmidt 
5937dd2badSKumar Gala #include <mm/mmu_decl.h>
609b6b563cSPaul Mackerras 
619b6b563cSPaul Mackerras #ifdef DEBUG
629b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt)
639b6b563cSPaul Mackerras #else
649b6b563cSPaul Mackerras #define DBG(fmt...)
659b6b563cSPaul Mackerras #endif
669b6b563cSPaul Mackerras 
679b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6828897731SOlof Johansson int __initdata iommu_is_off;
699b6b563cSPaul Mackerras int __initdata iommu_force_on;
70cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
71cd3db0c4SBenjamin Herrenschmidt u64 ppc64_rma_size;
729b6b563cSPaul Mackerras #endif
7303bf469aSBenjamin Herrenschmidt static phys_addr_t first_memblock_size;
747ac87abbSMatt Evans static int __initdata boot_cpu_count;
759b6b563cSPaul Mackerras 
762babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
772babf5c2SMichael Ellerman {
782babf5c2SMichael Ellerman 	if (!p)
792babf5c2SMichael Ellerman 		return 1;
802babf5c2SMichael Ellerman 
812babf5c2SMichael Ellerman 	memory_limit = PAGE_ALIGN(memparse(p, &p));
82a84fcd46SSuzuki Poulose 	DBG("memory limit = 0x%llx\n", memory_limit);
832babf5c2SMichael Ellerman 
842babf5c2SMichael Ellerman 	return 0;
852babf5c2SMichael Ellerman }
862babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
872babf5c2SMichael Ellerman 
88307cfe71SBenjamin Herrenschmidt /*
89307cfe71SBenjamin Herrenschmidt  * overlaps_initrd - check for overlap with page aligned extension of
90307cfe71SBenjamin Herrenschmidt  * initrd.
91307cfe71SBenjamin Herrenschmidt  */
92307cfe71SBenjamin Herrenschmidt static inline int overlaps_initrd(unsigned long start, unsigned long size)
93307cfe71SBenjamin Herrenschmidt {
94307cfe71SBenjamin Herrenschmidt #ifdef CONFIG_BLK_DEV_INITRD
95307cfe71SBenjamin Herrenschmidt 	if (!initrd_start)
96307cfe71SBenjamin Herrenschmidt 		return 0;
97307cfe71SBenjamin Herrenschmidt 
98307cfe71SBenjamin Herrenschmidt 	return	(start + size) > _ALIGN_DOWN(initrd_start, PAGE_SIZE) &&
99307cfe71SBenjamin Herrenschmidt 			start <= _ALIGN_UP(initrd_end, PAGE_SIZE);
100307cfe71SBenjamin Herrenschmidt #else
101307cfe71SBenjamin Herrenschmidt 	return 0;
102307cfe71SBenjamin Herrenschmidt #endif
103307cfe71SBenjamin Herrenschmidt }
104307cfe71SBenjamin Herrenschmidt 
1053c607ce2SLinas Vepstas /**
1063c607ce2SLinas Vepstas  * move_device_tree - move tree to an unused area, if needed.
1073c607ce2SLinas Vepstas  *
1083c607ce2SLinas Vepstas  * The device tree may be allocated beyond our memory limit, or inside the
109307cfe71SBenjamin Herrenschmidt  * crash kernel region for kdump, or within the page aligned range of initrd.
110307cfe71SBenjamin Herrenschmidt  * If so, move it out of the way.
1112babf5c2SMichael Ellerman  */
11218f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
1132babf5c2SMichael Ellerman {
1142babf5c2SMichael Ellerman 	unsigned long start, size;
1152babf5c2SMichael Ellerman 	void *p;
1162babf5c2SMichael Ellerman 
1172babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
1182babf5c2SMichael Ellerman 
1192babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
120087f79c4SJeremy Kerr 	size = be32_to_cpu(initial_boot_params->totalsize);
1212babf5c2SMichael Ellerman 
1226dd22700SScott Wood 	if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
123307cfe71SBenjamin Herrenschmidt 			overlaps_crashkernel(start, size) ||
124307cfe71SBenjamin Herrenschmidt 			overlaps_initrd(start, size)) {
125e63075a3SBenjamin Herrenschmidt 		p = __va(memblock_alloc(size, PAGE_SIZE));
1262babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
1272babf5c2SMichael Ellerman 		initial_boot_params = (struct boot_param_header *)p;
1282babf5c2SMichael Ellerman 		DBG("Moved device tree to 0x%p\n", p);
1292babf5c2SMichael Ellerman 	}
1302babf5c2SMichael Ellerman 
1312babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
1322babf5c2SMichael Ellerman }
1339b6b563cSPaul Mackerras 
134d205819eSPaul Mackerras /*
135d205819eSPaul Mackerras  * ibm,pa-features is a per-cpu property that contains a string of
136d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
137d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
138d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
139d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
140d205819eSPaul Mackerras  * zero is the only currently-defined value.
141d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
142d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
143d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
144d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
145d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
146d205819eSPaul Mackerras  */
147d205819eSPaul Mackerras static struct ibm_pa_feature {
148d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
14944ae3ab3SMatt Evans 	unsigned long	mmu_features;	/* MMU_FTR_xxx bit */
150d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
151d205819eSPaul Mackerras 	unsigned char	pabyte;		/* byte number in ibm,pa-features */
152d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
153d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
154d205819eSPaul Mackerras } ibm_pa_features[] __initdata = {
15544ae3ab3SMatt Evans 	{0, 0, PPC_FEATURE_HAS_MMU,	0, 0, 0},
15644ae3ab3SMatt Evans 	{0, 0, PPC_FEATURE_HAS_FPU,	0, 1, 0},
15744ae3ab3SMatt Evans 	{0, MMU_FTR_SLB, 0,		0, 2, 0},
15844ae3ab3SMatt Evans 	{CPU_FTR_CTRL, 0, 0,		0, 3, 0},
15944ae3ab3SMatt Evans 	{CPU_FTR_NOEXECUTE, 0, 0,	0, 6, 0},
16044ae3ab3SMatt Evans 	{CPU_FTR_NODSISRALIGN, 0, 0,	1, 1, 1},
16144ae3ab3SMatt Evans 	{0, MMU_FTR_CI_LARGE_PAGE, 0,	1, 2, 0},
162339d76c5SPaul Mackerras 	{CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
163d205819eSPaul Mackerras };
164d205819eSPaul Mackerras 
165974a76f5SPaul Mackerras static void __init scan_features(unsigned long node, unsigned char *ftrs,
166974a76f5SPaul Mackerras 				 unsigned long tablelen,
167974a76f5SPaul Mackerras 				 struct ibm_pa_feature *fp,
168974a76f5SPaul Mackerras 				 unsigned long ft_size)
169d205819eSPaul Mackerras {
170974a76f5SPaul Mackerras 	unsigned long i, len, bit;
171d205819eSPaul Mackerras 
172d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
173d205819eSPaul Mackerras 	for (;;) {
174d205819eSPaul Mackerras 		if (tablelen < 3)
175d205819eSPaul Mackerras 			return;
176974a76f5SPaul Mackerras 		len = 2 + ftrs[0];
177d205819eSPaul Mackerras 		if (tablelen < len)
178d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
179974a76f5SPaul Mackerras 		if (ftrs[1] == 0)
180d205819eSPaul Mackerras 			break;
181d205819eSPaul Mackerras 		tablelen -= len;
182974a76f5SPaul Mackerras 		ftrs += len;
183d205819eSPaul Mackerras 	}
184d205819eSPaul Mackerras 
185d205819eSPaul Mackerras 	/* loop over bits we know about */
186974a76f5SPaul Mackerras 	for (i = 0; i < ft_size; ++i, ++fp) {
187974a76f5SPaul Mackerras 		if (fp->pabyte >= ftrs[0])
188d205819eSPaul Mackerras 			continue;
189974a76f5SPaul Mackerras 		bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
190d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
191d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
192d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
19344ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features |= fp->mmu_features;
194d205819eSPaul Mackerras 		} else {
195d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
196d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
19744ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features &= ~fp->mmu_features;
198d205819eSPaul Mackerras 		}
199d205819eSPaul Mackerras 	}
200d205819eSPaul Mackerras }
201d205819eSPaul Mackerras 
202974a76f5SPaul Mackerras static void __init check_cpu_pa_features(unsigned long node)
203974a76f5SPaul Mackerras {
204974a76f5SPaul Mackerras 	unsigned char *pa_ftrs;
205974a76f5SPaul Mackerras 	unsigned long tablelen;
206974a76f5SPaul Mackerras 
207974a76f5SPaul Mackerras 	pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
208974a76f5SPaul Mackerras 	if (pa_ftrs == NULL)
209974a76f5SPaul Mackerras 		return;
210974a76f5SPaul Mackerras 
211974a76f5SPaul Mackerras 	scan_features(node, pa_ftrs, tablelen,
212974a76f5SPaul Mackerras 		      ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
213974a76f5SPaul Mackerras }
214974a76f5SPaul Mackerras 
21594491685SBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU_64
216584f8b71SMichael Neuling static void __init check_cpu_slb_size(unsigned long node)
217584f8b71SMichael Neuling {
218dc0e643aSIan Munsie 	__be32 *slb_size_ptr;
219584f8b71SMichael Neuling 
220b60c31d8SMichael Neuling 	slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
221b60c31d8SMichael Neuling 	if (slb_size_ptr != NULL) {
222dc0e643aSIan Munsie 		mmu_slb_size = be32_to_cpup(slb_size_ptr);
223b60c31d8SMichael Neuling 		return;
224b60c31d8SMichael Neuling 	}
225584f8b71SMichael Neuling 	slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
226584f8b71SMichael Neuling 	if (slb_size_ptr != NULL) {
227dc0e643aSIan Munsie 		mmu_slb_size = be32_to_cpup(slb_size_ptr);
228584f8b71SMichael Neuling 	}
229584f8b71SMichael Neuling }
230584f8b71SMichael Neuling #else
231584f8b71SMichael Neuling #define check_cpu_slb_size(node) do { } while(0)
232584f8b71SMichael Neuling #endif
233584f8b71SMichael Neuling 
234974a76f5SPaul Mackerras static struct feature_property {
235974a76f5SPaul Mackerras 	const char *name;
236974a76f5SPaul Mackerras 	u32 min_value;
237974a76f5SPaul Mackerras 	unsigned long cpu_feature;
238974a76f5SPaul Mackerras 	unsigned long cpu_user_ftr;
239974a76f5SPaul Mackerras } feature_properties[] __initdata = {
240974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC
241974a76f5SPaul Mackerras 	{"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
242974a76f5SPaul Mackerras 	{"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
243974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */
244b962ce9dSMichael Neuling #ifdef CONFIG_VSX
245b962ce9dSMichael Neuling 	/* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
246b962ce9dSMichael Neuling 	{"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
247b962ce9dSMichael Neuling #endif /* CONFIG_VSX */
248974a76f5SPaul Mackerras #ifdef CONFIG_PPC64
249974a76f5SPaul Mackerras 	{"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
250974a76f5SPaul Mackerras 	{"ibm,purr", 1, CPU_FTR_PURR, 0},
251974a76f5SPaul Mackerras 	{"ibm,spurr", 1, CPU_FTR_SPURR, 0},
252974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */
253974a76f5SPaul Mackerras };
254974a76f5SPaul Mackerras 
25514b3d926SValentine Barshak #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
25614b3d926SValentine Barshak static inline void identical_pvr_fixup(unsigned long node)
25714b3d926SValentine Barshak {
25814b3d926SValentine Barshak 	unsigned int pvr;
25914b3d926SValentine Barshak 	char *model = of_get_flat_dt_prop(node, "model", NULL);
26014b3d926SValentine Barshak 
26114b3d926SValentine Barshak 	/*
26214b3d926SValentine Barshak 	 * Since 440GR(x)/440EP(x) processors have the same pvr,
26314b3d926SValentine Barshak 	 * we check the node path and set bit 28 in the cur_cpu_spec
26414b3d926SValentine Barshak 	 * pvr for EP(x) processor version. This bit is always 0 in
26514b3d926SValentine Barshak 	 * the "real" pvr. Then we call identify_cpu again with
26614b3d926SValentine Barshak 	 * the new logical pvr to enable FPU support.
26714b3d926SValentine Barshak 	 */
26814b3d926SValentine Barshak 	if (model && strstr(model, "440EP")) {
26914b3d926SValentine Barshak 		pvr = cur_cpu_spec->pvr_value | 0x8;
27014b3d926SValentine Barshak 		identify_cpu(0, pvr);
27114b3d926SValentine Barshak 		DBG("Using logical pvr %x for %s\n", pvr, model);
27214b3d926SValentine Barshak 	}
27314b3d926SValentine Barshak }
27414b3d926SValentine Barshak #else
27514b3d926SValentine Barshak #define identical_pvr_fixup(node) do { } while(0)
27614b3d926SValentine Barshak #endif
27714b3d926SValentine Barshak 
278974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node)
279974a76f5SPaul Mackerras {
280974a76f5SPaul Mackerras 	unsigned long i;
281974a76f5SPaul Mackerras 	struct feature_property *fp = feature_properties;
282dc0e643aSIan Munsie 	const __be32 *prop;
283974a76f5SPaul Mackerras 
284974a76f5SPaul Mackerras 	for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
285974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, fp->name, NULL);
286dc0e643aSIan Munsie 		if (prop && be32_to_cpup(prop) >= fp->min_value) {
287974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_feature;
288974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
289974a76f5SPaul Mackerras 		}
290974a76f5SPaul Mackerras 	}
291974a76f5SPaul Mackerras }
292974a76f5SPaul Mackerras 
2939b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
2944df20460SAnton Blanchard 					  const char *uname, int depth,
2954df20460SAnton Blanchard 					  void *data)
2969b6b563cSPaul Mackerras {
2974df20460SAnton Blanchard 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
298dc0e643aSIan Munsie 	const __be32 *prop;
299dc0e643aSIan Munsie 	const __be32 *intserv;
3004df20460SAnton Blanchard 	int i, nthreads;
3014df20460SAnton Blanchard 	unsigned long len;
3029d07bc84SBenjamin Herrenschmidt 	int found = -1;
303208b3a4cSBenjamin Herrenschmidt 	int found_thread = 0;
3049b6b563cSPaul Mackerras 
3059b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
3069b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
3079b6b563cSPaul Mackerras 		return 0;
3089b6b563cSPaul Mackerras 
3094df20460SAnton Blanchard 	/* Get physical cpuid */
3104df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
3114df20460SAnton Blanchard 	if (intserv) {
3124df20460SAnton Blanchard 		nthreads = len / sizeof(int);
3139b6b563cSPaul Mackerras 	} else {
3144df20460SAnton Blanchard 		intserv = of_get_flat_dt_prop(node, "reg", NULL);
3154df20460SAnton Blanchard 		nthreads = 1;
3164df20460SAnton Blanchard 	}
3174df20460SAnton Blanchard 
3184df20460SAnton Blanchard 	/*
3194df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
3204df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
3214df20460SAnton Blanchard 	 */
3224df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
3234df20460SAnton Blanchard 		/*
3244df20460SAnton Blanchard 		 * version 2 of the kexec param format adds the phys cpuid of
3254df20460SAnton Blanchard 		 * booted proc.
3264df20460SAnton Blanchard 		 */
327dc0e643aSIan Munsie 		if (be32_to_cpu(initial_boot_params->version) >= 2) {
328dc0e643aSIan Munsie 			if (be32_to_cpu(intserv[i]) ==
329dc0e643aSIan Munsie 			    be32_to_cpu(initial_boot_params->boot_cpuid_phys)) {
3309d07bc84SBenjamin Herrenschmidt 				found = boot_cpu_count;
331208b3a4cSBenjamin Herrenschmidt 				found_thread = i;
332208b3a4cSBenjamin Herrenschmidt 			}
3334df20460SAnton Blanchard 		} else {
3344df20460SAnton Blanchard 			/*
3354df20460SAnton Blanchard 			 * Check if it's the boot-cpu, set it's hw index now,
3364df20460SAnton Blanchard 			 * unfortunately this format did not support booting
3374df20460SAnton Blanchard 			 * off secondary threads.
3384df20460SAnton Blanchard 			 */
3393c726f8dSBenjamin Herrenschmidt 			if (of_get_flat_dt_prop(node,
3409d07bc84SBenjamin Herrenschmidt 					"linux,boot-cpu", NULL) != NULL)
3419d07bc84SBenjamin Herrenschmidt 				found = boot_cpu_count;
3429b6b563cSPaul Mackerras 		}
3434df20460SAnton Blanchard #ifdef CONFIG_SMP
3444df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
3459d07bc84SBenjamin Herrenschmidt 		boot_cpu_count++;
3464df20460SAnton Blanchard #endif
3474df20460SAnton Blanchard 	}
3484df20460SAnton Blanchard 
3499d07bc84SBenjamin Herrenschmidt 	if (found >= 0) {
3509d07bc84SBenjamin Herrenschmidt 		DBG("boot cpu: logical %d physical %d\n", found,
351dc0e643aSIan Munsie 			be32_to_cpu(intserv[found_thread]));
3529d07bc84SBenjamin Herrenschmidt 		boot_cpuid = found;
353dc0e643aSIan Munsie 		set_hard_smp_processor_id(found,
354dc0e643aSIan Munsie 			be32_to_cpu(intserv[found_thread]));
355974a76f5SPaul Mackerras 
356974a76f5SPaul Mackerras 		/*
357974a76f5SPaul Mackerras 		 * PAPR defines "logical" PVR values for cpus that
358974a76f5SPaul Mackerras 		 * meet various levels of the architecture:
359974a76f5SPaul Mackerras 		 * 0x0f000001	Architecture version 2.04
360974a76f5SPaul Mackerras 		 * 0x0f000002	Architecture version 2.05
361974a76f5SPaul Mackerras 		 * If the cpu-version property in the cpu node contains
362974a76f5SPaul Mackerras 		 * such a value, we call identify_cpu again with the
363974a76f5SPaul Mackerras 		 * logical PVR value in order to use the cpu feature
364974a76f5SPaul Mackerras 		 * bits appropriate for the architecture level.
365974a76f5SPaul Mackerras 		 *
366974a76f5SPaul Mackerras 		 * A POWER6 partition in "POWER6 architected" mode
367974a76f5SPaul Mackerras 		 * uses the 0x0f000002 PVR value; in POWER5+ mode
368974a76f5SPaul Mackerras 		 * it uses 0x0f000001.
369974a76f5SPaul Mackerras 		 */
370974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
371dc0e643aSIan Munsie 		if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000)
372dc0e643aSIan Munsie 			identify_cpu(0, be32_to_cpup(prop));
37314b3d926SValentine Barshak 
37414b3d926SValentine Barshak 		identical_pvr_fixup(node);
3754df20460SAnton Blanchard 	}
3769b6b563cSPaul Mackerras 
377974a76f5SPaul Mackerras 	check_cpu_feature_properties(node);
378d205819eSPaul Mackerras 	check_cpu_pa_features(node);
379584f8b71SMichael Neuling 	check_cpu_slb_size(node);
380d205819eSPaul Mackerras 
3819b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
3824df20460SAnton Blanchard 	if (nthreads > 1)
3839b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
3844df20460SAnton Blanchard 	else
3854df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
3869b6b563cSPaul Mackerras #endif
3879b6b563cSPaul Mackerras 
3889b6b563cSPaul Mackerras 	return 0;
3899b6b563cSPaul Mackerras }
3909b6b563cSPaul Mackerras 
39132c97689SGrant Likely int __init early_init_dt_scan_chosen_ppc(unsigned long node, const char *uname,
39232c97689SGrant Likely 					 int depth, void *data)
3939b6b563cSPaul Mackerras {
394dc0e643aSIan Munsie 	unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
3959b6b563cSPaul Mackerras 
39632c97689SGrant Likely 	/* Use common scan routine to determine if this is the chosen node */
39732c97689SGrant Likely 	if (early_init_dt_scan_chosen(node, uname, depth, data) == 0)
39832c97689SGrant Likely 		return 0;
39932c97689SGrant Likely 
4009b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4019b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
4023c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
4039b6b563cSPaul Mackerras 		iommu_is_off = 1;
4043c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
4059b6b563cSPaul Mackerras 		iommu_force_on = 1;
4069b6b563cSPaul Mackerras #endif
4079b6b563cSPaul Mackerras 
4082babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
4093c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
4109b6b563cSPaul Mackerras 	if (lprop)
4119b6b563cSPaul Mackerras 		memory_limit = *lprop;
4129b6b563cSPaul Mackerras 
4139b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4143c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
4159b6b563cSPaul Mackerras 	if (lprop)
4169b6b563cSPaul Mackerras 		tce_alloc_start = *lprop;
4173c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
4189b6b563cSPaul Mackerras 	if (lprop)
4199b6b563cSPaul Mackerras 		tce_alloc_end = *lprop;
4209b6b563cSPaul Mackerras #endif
4219b6b563cSPaul Mackerras 
422dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC
42363277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
424dcee3036SMichael Ellerman 	if (lprop)
425dcee3036SMichael Ellerman 		crashk_res.start = *lprop;
426dcee3036SMichael Ellerman 
42763277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
428dcee3036SMichael Ellerman 	if (lprop)
429dcee3036SMichael Ellerman 		crashk_res.end = crashk_res.start + *lprop - 1;
430dcee3036SMichael Ellerman #endif
43132c97689SGrant Likely 
43232c97689SGrant Likely 	/* break now */
43332c97689SGrant Likely 	return 1;
4349b6b563cSPaul Mackerras }
4359b6b563cSPaul Mackerras 
4360204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
4370204568aSPaul Mackerras /*
4380204568aSPaul Mackerras  * Interpret the ibm,dynamic-memory property in the
4390204568aSPaul Mackerras  * /ibm,dynamic-reconfiguration-memory node.
4400204568aSPaul Mackerras  * This contains a list of memory blocks along with NUMA affinity
4410204568aSPaul Mackerras  * information.
4420204568aSPaul Mackerras  */
4430204568aSPaul Mackerras static int __init early_init_dt_scan_drconf_memory(unsigned long node)
4440204568aSPaul Mackerras {
4450f0b56c3SGrant Likely 	__be32 *dm, *ls, *usm;
446abe76885SBecky Bruce 	unsigned long l, n, flags;
44795f72d1eSYinghai Lu 	u64 base, size, memblock_size;
448cf00085dSChandru 	unsigned int is_kexec_kdump = 0, rngs;
4490204568aSPaul Mackerras 
4503fdfd990SBenjamin Herrenschmidt 	ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
4510f0b56c3SGrant Likely 	if (ls == NULL || l < dt_root_size_cells * sizeof(__be32))
4520204568aSPaul Mackerras 		return 0;
45395f72d1eSYinghai Lu 	memblock_size = dt_mem_next_cell(dt_root_size_cells, &ls);
4540204568aSPaul Mackerras 
45563277161SStephen Rothwell 	dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
4560f0b56c3SGrant Likely 	if (dm == NULL || l < sizeof(__be32))
4570204568aSPaul Mackerras 		return 0;
4580204568aSPaul Mackerras 
459e91ae5bdSAlistair Popple 	n = of_read_number(dm++, 1);	/* number of entries */
4600f0b56c3SGrant Likely 	if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(__be32))
4610204568aSPaul Mackerras 		return 0;
4620204568aSPaul Mackerras 
463cf00085dSChandru 	/* check if this is a kexec/kdump kernel. */
46463277161SStephen Rothwell 	usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
465cf00085dSChandru 						 &l);
466cf00085dSChandru 	if (usm != NULL)
467cf00085dSChandru 		is_kexec_kdump = 1;
468cf00085dSChandru 
4690204568aSPaul Mackerras 	for (; n != 0; --n) {
4700204568aSPaul Mackerras 		base = dt_mem_next_cell(dt_root_addr_cells, &dm);
471e91ae5bdSAlistair Popple 		flags = of_read_number(&dm[3], 1);
4720204568aSPaul Mackerras 		/* skip DRC index, pad, assoc. list index, flags */
4730204568aSPaul Mackerras 		dm += 4;
4740204568aSPaul Mackerras 		/* skip this block if the reserved bit is set in flags (0x80)
4750204568aSPaul Mackerras 		   or if the block is not assigned to this partition (0x8) */
4760204568aSPaul Mackerras 		if ((flags & 0x80) || !(flags & 0x8))
4770204568aSPaul Mackerras 			continue;
47895f72d1eSYinghai Lu 		size = memblock_size;
479cf00085dSChandru 		rngs = 1;
480cf00085dSChandru 		if (is_kexec_kdump) {
481cf00085dSChandru 			/*
48295f72d1eSYinghai Lu 			 * For each memblock in ibm,dynamic-memory, a corresponding
483cf00085dSChandru 			 * entry in linux,drconf-usable-memory property contains
484cf00085dSChandru 			 * a counter 'p' followed by 'p' (base, size) duple.
485cf00085dSChandru 			 * Now read the counter from
486cf00085dSChandru 			 * linux,drconf-usable-memory property
487cf00085dSChandru 			 */
488cf00085dSChandru 			rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
489cf00085dSChandru 			if (!rngs) /* there are no (base, size) duple */
490cf00085dSChandru 				continue;
491cf00085dSChandru 		}
492cf00085dSChandru 		do {
493cf00085dSChandru 			if (is_kexec_kdump) {
494cf00085dSChandru 				base = dt_mem_next_cell(dt_root_addr_cells,
495cf00085dSChandru 							 &usm);
496cf00085dSChandru 				size = dt_mem_next_cell(dt_root_size_cells,
497cf00085dSChandru 							 &usm);
498cf00085dSChandru 			}
4990204568aSPaul Mackerras 			if (iommu_is_off) {
5000204568aSPaul Mackerras 				if (base >= 0x80000000ul)
5010204568aSPaul Mackerras 					continue;
5020204568aSPaul Mackerras 				if ((base + size) > 0x80000000ul)
5030204568aSPaul Mackerras 					size = 0x80000000ul - base;
5040204568aSPaul Mackerras 			}
50595f72d1eSYinghai Lu 			memblock_add(base, size);
506cf00085dSChandru 		} while (--rngs);
5070204568aSPaul Mackerras 	}
50895f72d1eSYinghai Lu 	memblock_dump_all();
5090204568aSPaul Mackerras 	return 0;
5100204568aSPaul Mackerras }
5110204568aSPaul Mackerras #else
5120204568aSPaul Mackerras #define early_init_dt_scan_drconf_memory(node)	0
5130204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
5149b6b563cSPaul Mackerras 
51551975db0SGrant Likely static int __init early_init_dt_scan_memory_ppc(unsigned long node,
51651975db0SGrant Likely 						const char *uname,
51751975db0SGrant Likely 						int depth, void *data)
5189b6b563cSPaul Mackerras {
5190204568aSPaul Mackerras 	if (depth == 1 &&
5200204568aSPaul Mackerras 	    strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
5210204568aSPaul Mackerras 		return early_init_dt_scan_drconf_memory(node);
5220204568aSPaul Mackerras 
52351975db0SGrant Likely 	return early_init_dt_scan_memory(node, uname, depth, data);
52451975db0SGrant Likely }
5259b6b563cSPaul Mackerras 
52651975db0SGrant Likely void __init early_init_dt_add_memory_arch(u64 base, u64 size)
52751975db0SGrant Likely {
528cd3db0c4SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
5299b6b563cSPaul Mackerras 	if (iommu_is_off) {
5309b6b563cSPaul Mackerras 		if (base >= 0x80000000ul)
53151975db0SGrant Likely 			return;
5329b6b563cSPaul Mackerras 		if ((base + size) > 0x80000000ul)
5339b6b563cSPaul Mackerras 			size = 0x80000000ul - base;
5349b6b563cSPaul Mackerras 	}
5359b6b563cSPaul Mackerras #endif
53603bf469aSBenjamin Herrenschmidt 	/* Keep track of the beginning of memory -and- the size of
53703bf469aSBenjamin Herrenschmidt 	 * the very first block in the device-tree as it represents
53803bf469aSBenjamin Herrenschmidt 	 * the RMA on ppc64 server
53903bf469aSBenjamin Herrenschmidt 	 */
54003bf469aSBenjamin Herrenschmidt 	if (base < memstart_addr) {
54103bf469aSBenjamin Herrenschmidt 		memstart_addr = base;
54203bf469aSBenjamin Herrenschmidt 		first_memblock_size = size;
54303bf469aSBenjamin Herrenschmidt 	}
544cd3db0c4SBenjamin Herrenschmidt 
545cd3db0c4SBenjamin Herrenschmidt 	/* Add the chunk to the MEMBLOCK list */
546cd3db0c4SBenjamin Herrenschmidt 	memblock_add(base, size);
5479b6b563cSPaul Mackerras }
54837dd2badSKumar Gala 
549c039e3a8SBenjamin Herrenschmidt static void __init early_reserve_mem_dt(void)
5500962e800SJeremy Kerr {
5510962e800SJeremy Kerr 	unsigned long i, len, dt_root;
5520962e800SJeremy Kerr 	const __be32 *prop;
5530962e800SJeremy Kerr 
5540962e800SJeremy Kerr 	dt_root = of_get_flat_dt_root();
5550962e800SJeremy Kerr 
5560962e800SJeremy Kerr 	prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
5570962e800SJeremy Kerr 
5580962e800SJeremy Kerr 	if (!prop)
559c039e3a8SBenjamin Herrenschmidt 		return;
560c039e3a8SBenjamin Herrenschmidt 
561c039e3a8SBenjamin Herrenschmidt 	DBG("Found new-style reserved-ranges\n");
5620962e800SJeremy Kerr 
5630962e800SJeremy Kerr 	/* Each reserved range is an (address,size) pair, 2 cells each,
5640962e800SJeremy Kerr 	 * totalling 4 cells per range. */
5650962e800SJeremy Kerr 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
5660962e800SJeremy Kerr 		u64 base, size;
5670962e800SJeremy Kerr 
5680962e800SJeremy Kerr 		base = of_read_number(prop + (i * 4) + 0, 2);
5690962e800SJeremy Kerr 		size = of_read_number(prop + (i * 4) + 2, 2);
5700962e800SJeremy Kerr 
571c039e3a8SBenjamin Herrenschmidt 		if (size) {
572c039e3a8SBenjamin Herrenschmidt 			DBG("reserving: %llx -> %llx\n", base, size);
5730962e800SJeremy Kerr 			memblock_reserve(base, size);
5740962e800SJeremy Kerr 		}
575c039e3a8SBenjamin Herrenschmidt 	}
5760962e800SJeremy Kerr }
5770962e800SJeremy Kerr 
5789b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
5799b6b563cSPaul Mackerras {
580cbbcf340SKumar Gala 	u64 base, size;
581dc0e643aSIan Munsie 	__be64 *reserve_map;
5828a300887SJon Loeliger 	unsigned long self_base;
5838a300887SJon Loeliger 	unsigned long self_size;
5849b6b563cSPaul Mackerras 
585dc0e643aSIan Munsie 	reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
586dc0e643aSIan Munsie 			be32_to_cpu(initial_boot_params->off_mem_rsvmap));
5874d1f3f25SJimi Xenidis 
5884d1f3f25SJimi Xenidis 	/* before we do anything, lets reserve the dt blob */
5898a300887SJon Loeliger 	self_base = __pa((unsigned long)initial_boot_params);
590dc0e643aSIan Munsie 	self_size = be32_to_cpu(initial_boot_params->totalsize);
59195f72d1eSYinghai Lu 	memblock_reserve(self_base, self_size);
5924d1f3f25SJimi Xenidis 
593c039e3a8SBenjamin Herrenschmidt 	/* Look for the new "reserved-regions" property in the DT */
594c039e3a8SBenjamin Herrenschmidt 	early_reserve_mem_dt();
5950962e800SJeremy Kerr 
59630437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
597c039e3a8SBenjamin Herrenschmidt 	/* Then reserve the initrd, if any */
598c039e3a8SBenjamin Herrenschmidt 	if (initrd_start && (initrd_end > initrd_start)) {
599307cfe71SBenjamin Herrenschmidt 		memblock_reserve(_ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
600307cfe71SBenjamin Herrenschmidt 			_ALIGN_UP(initrd_end, PAGE_SIZE) -
601307cfe71SBenjamin Herrenschmidt 			_ALIGN_DOWN(initrd_start, PAGE_SIZE));
602c039e3a8SBenjamin Herrenschmidt 	}
60330437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
60430437b3eSDavid Gibson 
605cbbcf340SKumar Gala #ifdef CONFIG_PPC32
606cbbcf340SKumar Gala 	/*
607cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
608cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
609cbbcf340SKumar Gala 	 */
610dc0e643aSIan Munsie 	if (be64_to_cpup(reserve_map) > 0xffffffffull) {
611cbbcf340SKumar Gala 		u32 base_32, size_32;
612dc0e643aSIan Munsie 		__be32 *reserve_map_32 = (__be32 *)reserve_map;
613cbbcf340SKumar Gala 
614c039e3a8SBenjamin Herrenschmidt 		DBG("Found old 32-bit reserve map\n");
615c039e3a8SBenjamin Herrenschmidt 
616cbbcf340SKumar Gala 		while (1) {
617dc0e643aSIan Munsie 			base_32 = be32_to_cpup(reserve_map_32++);
618dc0e643aSIan Munsie 			size_32 = be32_to_cpup(reserve_map_32++);
619cbbcf340SKumar Gala 			if (size_32 == 0)
620cbbcf340SKumar Gala 				break;
6218a300887SJon Loeliger 			/* skip if the reservation is for the blob */
6228a300887SJon Loeliger 			if (base_32 == self_base && size_32 == self_size)
6238a300887SJon Loeliger 				continue;
624329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
62595f72d1eSYinghai Lu 			memblock_reserve(base_32, size_32);
626cbbcf340SKumar Gala 		}
627cbbcf340SKumar Gala 		return;
628cbbcf340SKumar Gala 	}
629cbbcf340SKumar Gala #endif
630c039e3a8SBenjamin Herrenschmidt 	DBG("Processing reserve map\n");
631c039e3a8SBenjamin Herrenschmidt 
632c039e3a8SBenjamin Herrenschmidt 	/* Handle the reserve map in the fdt blob if it exists */
6339b6b563cSPaul Mackerras 	while (1) {
634dc0e643aSIan Munsie 		base = be64_to_cpup(reserve_map++);
635dc0e643aSIan Munsie 		size = be64_to_cpup(reserve_map++);
6369b6b563cSPaul Mackerras 		if (size == 0)
6379b6b563cSPaul Mackerras 			break;
638cbbcf340SKumar Gala 		DBG("reserving: %llx -> %llx\n", base, size);
63995f72d1eSYinghai Lu 		memblock_reserve(base, size);
6409b6b563cSPaul Mackerras 	}
6419b6b563cSPaul Mackerras }
6429b6b563cSPaul Mackerras 
6439b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
6449b6b563cSPaul Mackerras {
64549a84965SBecky Bruce 	phys_addr_t limit;
6466ca4f749SHollis Blanchard 
64744348105SGeoff Levand 	DBG(" -> early_init_devtree(%p)\n", params);
6489b6b563cSPaul Mackerras 
6499b6b563cSPaul Mackerras 	/* Setup flat device-tree pointer */
6509b6b563cSPaul Mackerras 	initial_boot_params = params;
6519b6b563cSPaul Mackerras 
652458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
653458148c0SMichael Ellerman 	/* Some machines might need RTAS info for debugging, grab it now. */
654458148c0SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
655458148c0SMichael Ellerman #endif
656458148c0SMichael Ellerman 
65714a43e69SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
65814a43e69SBenjamin Herrenschmidt 	/* Some machines might need OPAL info for debugging, grab it now. */
65914a43e69SBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_opal, NULL);
66014a43e69SBenjamin Herrenschmidt #endif
66114a43e69SBenjamin Herrenschmidt 
662eb39c880SMahesh Salgaonkar #ifdef CONFIG_FA_DUMP
663eb39c880SMahesh Salgaonkar 	/* scan tree to see if dump is active during last boot */
664eb39c880SMahesh Salgaonkar 	of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
665eb39c880SMahesh Salgaonkar #endif
666eb39c880SMahesh Salgaonkar 
667817c21adSBenjamin Herrenschmidt 	/* Pre-initialize the cmd_line with the content of boot_commmand_line,
668817c21adSBenjamin Herrenschmidt 	 * which will be empty except when the content of the variable has
669817c21adSBenjamin Herrenschmidt 	 * been overriden by a bootloading mechanism. This happens typically
670817c21adSBenjamin Herrenschmidt 	 * with HAL takeover
671817c21adSBenjamin Herrenschmidt 	 */
672817c21adSBenjamin Herrenschmidt 	strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);
673817c21adSBenjamin Herrenschmidt 
6749b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
6759b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
6769b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
6779b6b563cSPaul Mackerras 	 */
67885f60ae4SGrant Likely 	of_scan_flat_dt(early_init_dt_scan_chosen_ppc, cmd_line);
6799b6b563cSPaul Mackerras 
68095f72d1eSYinghai Lu 	/* Scan memory nodes and rebuild MEMBLOCKs */
6813c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
68251975db0SGrant Likely 	of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
683846f77b0SMichael Ellerman 
684846f77b0SMichael Ellerman 	/* Save command line for /proc/cmdline and then parse parameters */
685b8757b21SAlon Bar-Lev 	strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
686846f77b0SMichael Ellerman 	parse_early_param();
687846f77b0SMichael Ellerman 
688ba14f649SKumar Gala 	/* make sure we've parsed cmdline for mem= before this */
689ba14f649SKumar Gala 	if (memory_limit)
690a84fcd46SSuzuki Poulose 		first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
691ba14f649SKumar Gala 	setup_initial_memory_limit(memstart_addr, first_memblock_size);
69295f72d1eSYinghai Lu 	/* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
69395f72d1eSYinghai Lu 	memblock_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
694549e8152SPaul Mackerras 	/* If relocatable, reserve first 32k for interrupt vectors etc. */
695549e8152SPaul Mackerras 	if (PHYSICAL_START > MEMORY_START)
69695f72d1eSYinghai Lu 		memblock_reserve(MEMORY_START, 0x8000);
69747310413SMichael Ellerman 	reserve_kdump_trampoline();
698eb39c880SMahesh Salgaonkar #ifdef CONFIG_FA_DUMP
699eb39c880SMahesh Salgaonkar 	/*
700eb39c880SMahesh Salgaonkar 	 * If we fail to reserve memory for firmware-assisted dump then
701eb39c880SMahesh Salgaonkar 	 * fallback to kexec based kdump.
702eb39c880SMahesh Salgaonkar 	 */
703eb39c880SMahesh Salgaonkar 	if (fadump_reserve_mem() == 0)
704eb39c880SMahesh Salgaonkar #endif
70535dd5432SMichael Ellerman 		reserve_crashkernel();
7069b6b563cSPaul Mackerras 	early_reserve_mem();
7079b6b563cSPaul Mackerras 
7086fbef13cSTejun Heo 	/*
7096fbef13cSTejun Heo 	 * Ensure that total memory size is page-aligned, because otherwise
7106fbef13cSTejun Heo 	 * mark_bootmem() gets upset.
7116fbef13cSTejun Heo 	 */
7126fbef13cSTejun Heo 	limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
71395f72d1eSYinghai Lu 	memblock_enforce_memory_limit(limit);
7146ca4f749SHollis Blanchard 
7151aadc056STejun Heo 	memblock_allow_resize();
71695f72d1eSYinghai Lu 	memblock_dump_all();
7172babf5c2SMichael Ellerman 
71895f72d1eSYinghai Lu 	DBG("Phys. mem: %llx\n", memblock_phys_mem_size());
7192babf5c2SMichael Ellerman 
7202babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
7212babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
7222babf5c2SMichael Ellerman 	move_device_tree();
7232babf5c2SMichael Ellerman 
7241426d5a3SMichael Ellerman 	allocate_pacas();
7251426d5a3SMichael Ellerman 
7269b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
7279b6b563cSPaul Mackerras 
72825985edcSLucas De Marchi 	/* Retrieve CPU related informations from the flat tree
7293c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
7309b6b563cSPaul Mackerras 	 */
7313c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
7329b6b563cSPaul Mackerras 
7337ac87abbSMatt Evans #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
7347ac87abbSMatt Evans 	/* We'll later wait for secondaries to check in; there are
7357ac87abbSMatt Evans 	 * NCPUS-1 non-boot CPUs  :-)
7367ac87abbSMatt Evans 	 */
7377ac87abbSMatt Evans 	spinning_secondaries = boot_cpu_count - 1;
7387ac87abbSMatt Evans #endif
7397ac87abbSMatt Evans 
7409b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
7419b6b563cSPaul Mackerras }
7429b6b563cSPaul Mackerras 
7439b6b563cSPaul Mackerras /*******
7449b6b563cSPaul Mackerras  *
7459b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
7469b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
7479b6b563cSPaul Mackerras  * are protected by a rw_lock.
7489b6b563cSPaul Mackerras  *
7499b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
7509b6b563cSPaul Mackerras  * this isn't dealt with yet.
7519b6b563cSPaul Mackerras  *
7529b6b563cSPaul Mackerras  *******/
7539b6b563cSPaul Mackerras 
7549b6b563cSPaul Mackerras /**
755b37193b7SBenjamin Herrenschmidt  * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
756b37193b7SBenjamin Herrenschmidt  * @np: device node of the device
757b37193b7SBenjamin Herrenschmidt  *
758b37193b7SBenjamin Herrenschmidt  * This looks for a property "ibm,chip-id" in the node or any
759b37193b7SBenjamin Herrenschmidt  * of its parents and returns its content, or -1 if it cannot
760b37193b7SBenjamin Herrenschmidt  * be found.
761b37193b7SBenjamin Herrenschmidt  */
762b37193b7SBenjamin Herrenschmidt int of_get_ibm_chip_id(struct device_node *np)
763b37193b7SBenjamin Herrenschmidt {
764b37193b7SBenjamin Herrenschmidt 	of_node_get(np);
765b37193b7SBenjamin Herrenschmidt 	while(np) {
766b37193b7SBenjamin Herrenschmidt 		struct device_node *old = np;
767b37193b7SBenjamin Herrenschmidt 		const __be32 *prop;
768b37193b7SBenjamin Herrenschmidt 
769b37193b7SBenjamin Herrenschmidt 		prop = of_get_property(np, "ibm,chip-id", NULL);
770b37193b7SBenjamin Herrenschmidt 		if (prop) {
771b37193b7SBenjamin Herrenschmidt 			of_node_put(np);
772b37193b7SBenjamin Herrenschmidt 			return be32_to_cpup(prop);
773b37193b7SBenjamin Herrenschmidt 		}
774b37193b7SBenjamin Herrenschmidt 		np = of_get_parent(np);
775b37193b7SBenjamin Herrenschmidt 		of_node_put(old);
776b37193b7SBenjamin Herrenschmidt 	}
777b37193b7SBenjamin Herrenschmidt 	return -1;
778b37193b7SBenjamin Herrenschmidt }
779b37193b7SBenjamin Herrenschmidt 
780*3eb906c6SMichael Ellerman /**
781*3eb906c6SMichael Ellerman  * cpu_to_chip_id - Return the cpus chip-id
782*3eb906c6SMichael Ellerman  * @cpu: The logical cpu number.
783*3eb906c6SMichael Ellerman  *
784*3eb906c6SMichael Ellerman  * Return the value of the ibm,chip-id property corresponding to the given
785*3eb906c6SMichael Ellerman  * logical cpu number. If the chip-id can not be found, returns -1.
786*3eb906c6SMichael Ellerman  */
787*3eb906c6SMichael Ellerman int cpu_to_chip_id(int cpu)
788*3eb906c6SMichael Ellerman {
789*3eb906c6SMichael Ellerman 	struct device_node *np;
790*3eb906c6SMichael Ellerman 
791*3eb906c6SMichael Ellerman 	np = of_get_cpu_node(cpu, NULL);
792*3eb906c6SMichael Ellerman 	if (!np)
793*3eb906c6SMichael Ellerman 		return -1;
794*3eb906c6SMichael Ellerman 
795*3eb906c6SMichael Ellerman 	of_node_put(np);
796*3eb906c6SMichael Ellerman 	return of_get_ibm_chip_id(np);
797*3eb906c6SMichael Ellerman }
798*3eb906c6SMichael Ellerman EXPORT_SYMBOL(cpu_to_chip_id);
799*3eb906c6SMichael Ellerman 
8009b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
8019b6b563cSPaul Mackerras /*
8029b6b563cSPaul Mackerras  * Fix up the uninitialized fields in a new device node:
8030ebfff14SBenjamin Herrenschmidt  * name, type and pci-specific fields
8049b6b563cSPaul Mackerras  */
8059b6b563cSPaul Mackerras 
806cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node)
8079b6b563cSPaul Mackerras {
8089b6b563cSPaul Mackerras 	struct device_node *parent = of_get_parent(node);
8099b6b563cSPaul Mackerras 	int err = 0;
810a7f67bdfSJeremy Kerr 	const phandle *ibm_phandle;
8119b6b563cSPaul Mackerras 
8120e56efc7SStephen Rothwell 	node->name = of_get_property(node, "name", NULL);
8130e56efc7SStephen Rothwell 	node->type = of_get_property(node, "device_type", NULL);
8149b6b563cSPaul Mackerras 
815847f5976SBenjamin Herrenschmidt 	if (!node->name)
816847f5976SBenjamin Herrenschmidt 		node->name = "<NULL>";
817847f5976SBenjamin Herrenschmidt 	if (!node->type)
818847f5976SBenjamin Herrenschmidt 		node->type = "<NULL>";
819847f5976SBenjamin Herrenschmidt 
8209b6b563cSPaul Mackerras 	if (!parent) {
8219b6b563cSPaul Mackerras 		err = -ENODEV;
8229b6b563cSPaul Mackerras 		goto out;
8239b6b563cSPaul Mackerras 	}
8249b6b563cSPaul Mackerras 
8259b6b563cSPaul Mackerras 	/* We don't support that function on PowerMac, at least
8269b6b563cSPaul Mackerras 	 * not yet
8279b6b563cSPaul Mackerras 	 */
828e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac))
8299b6b563cSPaul Mackerras 		return -ENODEV;
8309b6b563cSPaul Mackerras 
8316016a363SGrant Likely 	/* fix up new node's phandle field */
8320e56efc7SStephen Rothwell 	if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
8336016a363SGrant Likely 		node->phandle = *ibm_phandle;
8349b6b563cSPaul Mackerras 
8359b6b563cSPaul Mackerras out:
8369b6b563cSPaul Mackerras 	of_node_put(parent);
8379b6b563cSPaul Mackerras 	return err;
8389b6b563cSPaul Mackerras }
8399b6b563cSPaul Mackerras 
8409b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb,
8419b6b563cSPaul Mackerras 				  unsigned long action, void *node)
8429b6b563cSPaul Mackerras {
8439b6b563cSPaul Mackerras 	int err;
8449b6b563cSPaul Mackerras 
8459b6b563cSPaul Mackerras 	switch (action) {
8461cf3d8b3SNathan Fontenot 	case OF_RECONFIG_ATTACH_NODE:
847cc5d0189SBenjamin Herrenschmidt 		err = of_finish_dynamic_node(node);
848de2780a3SAkinobu Mita 		if (err < 0)
8499b6b563cSPaul Mackerras 			printk(KERN_ERR "finish_node returned %d\n", err);
8509b6b563cSPaul Mackerras 		break;
8519b6b563cSPaul Mackerras 	default:
852de2780a3SAkinobu Mita 		err = 0;
8539b6b563cSPaul Mackerras 		break;
8549b6b563cSPaul Mackerras 	}
855de2780a3SAkinobu Mita 	return notifier_from_errno(err);
8569b6b563cSPaul Mackerras }
8579b6b563cSPaul Mackerras 
8589b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = {
8599b6b563cSPaul Mackerras 	.notifier_call = prom_reconfig_notifier,
8609b6b563cSPaul Mackerras 	.priority = 10, /* This one needs to run first */
8619b6b563cSPaul Mackerras };
8629b6b563cSPaul Mackerras 
8639b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void)
8649b6b563cSPaul Mackerras {
8651cf3d8b3SNathan Fontenot 	return of_reconfig_notifier_register(&prom_reconfig_nb);
8669b6b563cSPaul Mackerras }
8679b6b563cSPaul Mackerras __initcall(prom_reconfig_setup);
8689b6b563cSPaul Mackerras #endif
8699b6b563cSPaul Mackerras 
870819d5965SSudeep KarkadaNagesha bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
871819d5965SSudeep KarkadaNagesha {
872819d5965SSudeep KarkadaNagesha 	return (int)phys_id == get_hard_smp_processor_id(cpu);
873819d5965SSudeep KarkadaNagesha }
874819d5965SSudeep KarkadaNagesha 
87594a3807cSMichael Ellerman #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
8767a4571aeSMichael Ellerman static struct debugfs_blob_wrapper flat_dt_blob;
8777a4571aeSMichael Ellerman 
8787a4571aeSMichael Ellerman static int __init export_flat_device_tree(void)
8797a4571aeSMichael Ellerman {
8807a4571aeSMichael Ellerman 	struct dentry *d;
8817a4571aeSMichael Ellerman 
8827a4571aeSMichael Ellerman 	flat_dt_blob.data = initial_boot_params;
883dc0e643aSIan Munsie 	flat_dt_blob.size = be32_to_cpu(initial_boot_params->totalsize);
8847a4571aeSMichael Ellerman 
8857a4571aeSMichael Ellerman 	d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
88694a3807cSMichael Ellerman 				powerpc_debugfs_root, &flat_dt_blob);
8877a4571aeSMichael Ellerman 	if (!d)
8887a4571aeSMichael Ellerman 		return 1;
8897a4571aeSMichael Ellerman 
8907a4571aeSMichael Ellerman 	return 0;
8917a4571aeSMichael Ellerman }
8927a4571aeSMichael Ellerman __initcall(export_flat_device_tree);
8937a4571aeSMichael Ellerman #endif
894