1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Common boot and setup code for both 32-bit and 64-bit.
4  * Extracted from arch/powerpc/kernel/setup_64.c.
5  *
6  * Copyright (C) 2001 PPC64 Team, IBM Corp
7  */
8 
9 #undef DEBUG
10 
11 #include <linux/export.h>
12 #include <linux/panic_notifier.h>
13 #include <linux/string.h>
14 #include <linux/sched.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/reboot.h>
18 #include <linux/delay.h>
19 #include <linux/initrd.h>
20 #include <linux/platform_device.h>
21 #include <linux/seq_file.h>
22 #include <linux/ioport.h>
23 #include <linux/console.h>
24 #include <linux/screen_info.h>
25 #include <linux/root_dev.h>
26 #include <linux/cpu.h>
27 #include <linux/unistd.h>
28 #include <linux/serial.h>
29 #include <linux/serial_8250.h>
30 #include <linux/percpu.h>
31 #include <linux/memblock.h>
32 #include <linux/of_irq.h>
33 #include <linux/of_fdt.h>
34 #include <linux/of_platform.h>
35 #include <linux/hugetlb.h>
36 #include <linux/pgtable.h>
37 #include <asm/io.h>
38 #include <asm/paca.h>
39 #include <asm/processor.h>
40 #include <asm/vdso_datapage.h>
41 #include <asm/smp.h>
42 #include <asm/elf.h>
43 #include <asm/machdep.h>
44 #include <asm/time.h>
45 #include <asm/cputable.h>
46 #include <asm/sections.h>
47 #include <asm/firmware.h>
48 #include <asm/btext.h>
49 #include <asm/nvram.h>
50 #include <asm/setup.h>
51 #include <asm/rtas.h>
52 #include <asm/iommu.h>
53 #include <asm/serial.h>
54 #include <asm/cache.h>
55 #include <asm/page.h>
56 #include <asm/mmu.h>
57 #include <asm/xmon.h>
58 #include <asm/cputhreads.h>
59 #include <mm/mmu_decl.h>
60 #include <asm/fadump.h>
61 #include <asm/udbg.h>
62 #include <asm/hugetlb.h>
63 #include <asm/livepatch.h>
64 #include <asm/mmu_context.h>
65 #include <asm/cpu_has_feature.h>
66 #include <asm/kasan.h>
67 #include <asm/mce.h>
68 
69 #include "setup.h"
70 
71 #ifdef DEBUG
72 #define DBG(fmt...) udbg_printf(fmt)
73 #else
74 #define DBG(fmt...)
75 #endif
76 
77 /* The main machine-dep calls structure
78  */
79 struct machdep_calls ppc_md;
80 EXPORT_SYMBOL(ppc_md);
81 struct machdep_calls *machine_id;
82 EXPORT_SYMBOL(machine_id);
83 
84 int boot_cpuid = -1;
85 EXPORT_SYMBOL_GPL(boot_cpuid);
86 
87 /*
88  * These are used in binfmt_elf.c to put aux entries on the stack
89  * for each elf executable being started.
90  */
91 int dcache_bsize;
92 int icache_bsize;
93 
94 /*
95  * This still seems to be needed... -- paulus
96  */
97 struct screen_info screen_info = {
98 	.orig_x = 0,
99 	.orig_y = 25,
100 	.orig_video_cols = 80,
101 	.orig_video_lines = 25,
102 	.orig_video_isVGA = 1,
103 	.orig_video_points = 16
104 };
105 #if defined(CONFIG_FB_VGA16_MODULE)
106 EXPORT_SYMBOL(screen_info);
107 #endif
108 
109 /* Variables required to store legacy IO irq routing */
110 int of_i8042_kbd_irq;
111 EXPORT_SYMBOL_GPL(of_i8042_kbd_irq);
112 int of_i8042_aux_irq;
113 EXPORT_SYMBOL_GPL(of_i8042_aux_irq);
114 
115 #ifdef __DO_IRQ_CANON
116 /* XXX should go elsewhere eventually */
117 int ppc_do_canonicalize_irqs;
118 EXPORT_SYMBOL(ppc_do_canonicalize_irqs);
119 #endif
120 
121 #ifdef CONFIG_CRASH_CORE
122 /* This keeps a track of which one is the crashing cpu. */
123 int crashing_cpu = -1;
124 #endif
125 
126 /* also used by kexec */
127 void machine_shutdown(void)
128 {
129 	/*
130 	 * if fadump is active, cleanup the fadump registration before we
131 	 * shutdown.
132 	 */
133 	fadump_cleanup();
134 
135 	if (ppc_md.machine_shutdown)
136 		ppc_md.machine_shutdown();
137 }
138 
139 static void machine_hang(void)
140 {
141 	pr_emerg("System Halted, OK to turn off power\n");
142 	local_irq_disable();
143 	while (1)
144 		;
145 }
146 
147 void machine_restart(char *cmd)
148 {
149 	machine_shutdown();
150 	if (ppc_md.restart)
151 		ppc_md.restart(cmd);
152 
153 	smp_send_stop();
154 
155 	do_kernel_restart(cmd);
156 	mdelay(1000);
157 
158 	machine_hang();
159 }
160 
161 void machine_power_off(void)
162 {
163 	machine_shutdown();
164 	do_kernel_power_off();
165 	smp_send_stop();
166 	machine_hang();
167 }
168 /* Used by the G5 thermal driver */
169 EXPORT_SYMBOL_GPL(machine_power_off);
170 
171 void (*pm_power_off)(void);
172 EXPORT_SYMBOL_GPL(pm_power_off);
173 
174 size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
175 {
176 	if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed(v))
177 		return 1;
178 	return 0;
179 }
180 EXPORT_SYMBOL(arch_get_random_seed_longs);
181 
182 void machine_halt(void)
183 {
184 	machine_shutdown();
185 	if (ppc_md.halt)
186 		ppc_md.halt();
187 
188 	smp_send_stop();
189 	machine_hang();
190 }
191 
192 #ifdef CONFIG_SMP
193 DEFINE_PER_CPU(unsigned int, cpu_pvr);
194 #endif
195 
196 static void show_cpuinfo_summary(struct seq_file *m)
197 {
198 	struct device_node *root;
199 	const char *model = NULL;
200 	unsigned long bogosum = 0;
201 	int i;
202 
203 	if (IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_PPC32)) {
204 		for_each_online_cpu(i)
205 			bogosum += loops_per_jiffy;
206 		seq_printf(m, "total bogomips\t: %lu.%02lu\n",
207 			   bogosum / (500000 / HZ), bogosum / (5000 / HZ) % 100);
208 	}
209 	seq_printf(m, "timebase\t: %lu\n", ppc_tb_freq);
210 	if (ppc_md.name)
211 		seq_printf(m, "platform\t: %s\n", ppc_md.name);
212 	root = of_find_node_by_path("/");
213 	if (root)
214 		model = of_get_property(root, "model", NULL);
215 	if (model)
216 		seq_printf(m, "model\t\t: %s\n", model);
217 	of_node_put(root);
218 
219 	if (ppc_md.show_cpuinfo != NULL)
220 		ppc_md.show_cpuinfo(m);
221 
222 	/* Display the amount of memory */
223 	if (IS_ENABLED(CONFIG_PPC32))
224 		seq_printf(m, "Memory\t\t: %d MB\n",
225 			   (unsigned int)(total_memory / (1024 * 1024)));
226 }
227 
228 static int show_cpuinfo(struct seq_file *m, void *v)
229 {
230 	unsigned long cpu_id = (unsigned long)v - 1;
231 	unsigned int pvr;
232 	unsigned long proc_freq;
233 	unsigned short maj;
234 	unsigned short min;
235 
236 #ifdef CONFIG_SMP
237 	pvr = per_cpu(cpu_pvr, cpu_id);
238 #else
239 	pvr = mfspr(SPRN_PVR);
240 #endif
241 	maj = (pvr >> 8) & 0xFF;
242 	min = pvr & 0xFF;
243 
244 	seq_printf(m, "processor\t: %lu\ncpu\t\t: ", cpu_id);
245 
246 	if (cur_cpu_spec->pvr_mask && cur_cpu_spec->cpu_name)
247 		seq_puts(m, cur_cpu_spec->cpu_name);
248 	else
249 		seq_printf(m, "unknown (%08x)", pvr);
250 
251 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
252 		seq_puts(m, ", altivec supported");
253 
254 	seq_putc(m, '\n');
255 
256 #ifdef CONFIG_TAU
257 	if (cpu_has_feature(CPU_FTR_TAU)) {
258 		if (IS_ENABLED(CONFIG_TAU_AVERAGE)) {
259 			/* more straightforward, but potentially misleading */
260 			seq_printf(m,  "temperature \t: %u C (uncalibrated)\n",
261 				   cpu_temp(cpu_id));
262 		} else {
263 			/* show the actual temp sensor range */
264 			u32 temp;
265 			temp = cpu_temp_both(cpu_id);
266 			seq_printf(m, "temperature \t: %u-%u C (uncalibrated)\n",
267 				   temp & 0xff, temp >> 16);
268 		}
269 	}
270 #endif /* CONFIG_TAU */
271 
272 	/*
273 	 * Platforms that have variable clock rates, should implement
274 	 * the method ppc_md.get_proc_freq() that reports the clock
275 	 * rate of a given cpu. The rest can use ppc_proc_freq to
276 	 * report the clock rate that is same across all cpus.
277 	 */
278 	if (ppc_md.get_proc_freq)
279 		proc_freq = ppc_md.get_proc_freq(cpu_id);
280 	else
281 		proc_freq = ppc_proc_freq;
282 
283 	if (proc_freq)
284 		seq_printf(m, "clock\t\t: %lu.%06luMHz\n",
285 			   proc_freq / 1000000, proc_freq % 1000000);
286 
287 	/* If we are a Freescale core do a simple check so
288 	 * we don't have to keep adding cases in the future */
289 	if (PVR_VER(pvr) & 0x8000) {
290 		switch (PVR_VER(pvr)) {
291 		case 0x8000:	/* 7441/7450/7451, Voyager */
292 		case 0x8001:	/* 7445/7455, Apollo 6 */
293 		case 0x8002:	/* 7447/7457, Apollo 7 */
294 		case 0x8003:	/* 7447A, Apollo 7 PM */
295 		case 0x8004:	/* 7448, Apollo 8 */
296 		case 0x800c:	/* 7410, Nitro */
297 			maj = ((pvr >> 8) & 0xF);
298 			min = PVR_MIN(pvr);
299 			break;
300 		default:	/* e500/book-e */
301 			maj = PVR_MAJ(pvr);
302 			min = PVR_MIN(pvr);
303 			break;
304 		}
305 	} else {
306 		switch (PVR_VER(pvr)) {
307 			case 0x1008:	/* 740P/750P ?? */
308 				maj = ((pvr >> 8) & 0xFF) - 1;
309 				min = pvr & 0xFF;
310 				break;
311 			case 0x004e: /* POWER9 bits 12-15 give chip type */
312 			case 0x0080: /* POWER10 bit 12 gives SMT8/4 */
313 				maj = (pvr >> 8) & 0x0F;
314 				min = pvr & 0xFF;
315 				break;
316 			default:
317 				maj = (pvr >> 8) & 0xFF;
318 				min = pvr & 0xFF;
319 				break;
320 		}
321 	}
322 
323 	seq_printf(m, "revision\t: %hd.%hd (pvr %04x %04x)\n",
324 		   maj, min, PVR_VER(pvr), PVR_REV(pvr));
325 
326 	if (IS_ENABLED(CONFIG_PPC32))
327 		seq_printf(m, "bogomips\t: %lu.%02lu\n", loops_per_jiffy / (500000 / HZ),
328 			   (loops_per_jiffy / (5000 / HZ)) % 100);
329 
330 	seq_putc(m, '\n');
331 
332 	/* If this is the last cpu, print the summary */
333 	if (cpumask_next(cpu_id, cpu_online_mask) >= nr_cpu_ids)
334 		show_cpuinfo_summary(m);
335 
336 	return 0;
337 }
338 
339 static void *c_start(struct seq_file *m, loff_t *pos)
340 {
341 	if (*pos == 0)	/* just in case, cpu 0 is not the first */
342 		*pos = cpumask_first(cpu_online_mask);
343 	else
344 		*pos = cpumask_next(*pos - 1, cpu_online_mask);
345 	if ((*pos) < nr_cpu_ids)
346 		return (void *)(unsigned long)(*pos + 1);
347 	return NULL;
348 }
349 
350 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
351 {
352 	(*pos)++;
353 	return c_start(m, pos);
354 }
355 
356 static void c_stop(struct seq_file *m, void *v)
357 {
358 }
359 
360 const struct seq_operations cpuinfo_op = {
361 	.start	= c_start,
362 	.next	= c_next,
363 	.stop	= c_stop,
364 	.show	= show_cpuinfo,
365 };
366 
367 void __init check_for_initrd(void)
368 {
369 #ifdef CONFIG_BLK_DEV_INITRD
370 	DBG(" -> check_for_initrd()  initrd_start=0x%lx  initrd_end=0x%lx\n",
371 	    initrd_start, initrd_end);
372 
373 	/* If we were passed an initrd, set the ROOT_DEV properly if the values
374 	 * look sensible. If not, clear initrd reference.
375 	 */
376 	if (is_kernel_addr(initrd_start) && is_kernel_addr(initrd_end) &&
377 	    initrd_end > initrd_start)
378 		ROOT_DEV = Root_RAM0;
379 	else
380 		initrd_start = initrd_end = 0;
381 
382 	if (initrd_start)
383 		pr_info("Found initrd at 0x%lx:0x%lx\n", initrd_start, initrd_end);
384 
385 	DBG(" <- check_for_initrd()\n");
386 #endif /* CONFIG_BLK_DEV_INITRD */
387 }
388 
389 #ifdef CONFIG_SMP
390 
391 int threads_per_core, threads_per_subcore, threads_shift __read_mostly;
392 cpumask_t threads_core_mask __read_mostly;
393 EXPORT_SYMBOL_GPL(threads_per_core);
394 EXPORT_SYMBOL_GPL(threads_per_subcore);
395 EXPORT_SYMBOL_GPL(threads_shift);
396 EXPORT_SYMBOL_GPL(threads_core_mask);
397 
398 static void __init cpu_init_thread_core_maps(int tpc)
399 {
400 	int i;
401 
402 	threads_per_core = tpc;
403 	threads_per_subcore = tpc;
404 	cpumask_clear(&threads_core_mask);
405 
406 	/* This implementation only supports power of 2 number of threads
407 	 * for simplicity and performance
408 	 */
409 	threads_shift = ilog2(tpc);
410 	BUG_ON(tpc != (1 << threads_shift));
411 
412 	for (i = 0; i < tpc; i++)
413 		cpumask_set_cpu(i, &threads_core_mask);
414 
415 	printk(KERN_INFO "CPU maps initialized for %d thread%s per core\n",
416 	       tpc, tpc > 1 ? "s" : "");
417 	printk(KERN_DEBUG " (thread shift is %d)\n", threads_shift);
418 }
419 
420 
421 u32 *cpu_to_phys_id = NULL;
422 
423 /**
424  * setup_cpu_maps - initialize the following cpu maps:
425  *                  cpu_possible_mask
426  *                  cpu_present_mask
427  *
428  * Having the possible map set up early allows us to restrict allocations
429  * of things like irqstacks to nr_cpu_ids rather than NR_CPUS.
430  *
431  * We do not initialize the online map here; cpus set their own bits in
432  * cpu_online_mask as they come up.
433  *
434  * This function is valid only for Open Firmware systems.  finish_device_tree
435  * must be called before using this.
436  *
437  * While we're here, we may as well set the "physical" cpu ids in the paca.
438  *
439  * NOTE: This must match the parsing done in early_init_dt_scan_cpus.
440  */
441 void __init smp_setup_cpu_maps(void)
442 {
443 	struct device_node *dn;
444 	int cpu = 0;
445 	int nthreads = 1;
446 
447 	DBG("smp_setup_cpu_maps()\n");
448 
449 	cpu_to_phys_id = memblock_alloc(nr_cpu_ids * sizeof(u32),
450 					__alignof__(u32));
451 	if (!cpu_to_phys_id)
452 		panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
453 		      __func__, nr_cpu_ids * sizeof(u32), __alignof__(u32));
454 
455 	for_each_node_by_type(dn, "cpu") {
456 		const __be32 *intserv;
457 		__be32 cpu_be;
458 		int j, len;
459 
460 		DBG("  * %pOF...\n", dn);
461 
462 		intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s",
463 				&len);
464 		if (intserv) {
465 			DBG("    ibm,ppc-interrupt-server#s -> %lu threads\n",
466 			    (len / sizeof(int)));
467 		} else {
468 			DBG("    no ibm,ppc-interrupt-server#s -> 1 thread\n");
469 			intserv = of_get_property(dn, "reg", &len);
470 			if (!intserv) {
471 				cpu_be = cpu_to_be32(cpu);
472 				/* XXX: what is this? uninitialized?? */
473 				intserv = &cpu_be;	/* assume logical == phys */
474 				len = 4;
475 			}
476 		}
477 
478 		nthreads = len / sizeof(int);
479 
480 		for (j = 0; j < nthreads && cpu < nr_cpu_ids; j++) {
481 			bool avail;
482 
483 			DBG("    thread %d -> cpu %d (hard id %d)\n",
484 			    j, cpu, be32_to_cpu(intserv[j]));
485 
486 			avail = of_device_is_available(dn);
487 			if (!avail)
488 				avail = !of_property_match_string(dn,
489 						"enable-method", "spin-table");
490 
491 			set_cpu_present(cpu, avail);
492 			set_cpu_possible(cpu, true);
493 			cpu_to_phys_id[cpu] = be32_to_cpu(intserv[j]);
494 			cpu++;
495 		}
496 
497 		if (cpu >= nr_cpu_ids) {
498 			of_node_put(dn);
499 			break;
500 		}
501 	}
502 
503 	/* If no SMT supported, nthreads is forced to 1 */
504 	if (!cpu_has_feature(CPU_FTR_SMT)) {
505 		DBG("  SMT disabled ! nthreads forced to 1\n");
506 		nthreads = 1;
507 	}
508 
509 #ifdef CONFIG_PPC64
510 	/*
511 	 * On pSeries LPAR, we need to know how many cpus
512 	 * could possibly be added to this partition.
513 	 */
514 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
515 	    (dn = of_find_node_by_path("/rtas"))) {
516 		int num_addr_cell, num_size_cell, maxcpus;
517 		const __be32 *ireg;
518 
519 		num_addr_cell = of_n_addr_cells(dn);
520 		num_size_cell = of_n_size_cells(dn);
521 
522 		ireg = of_get_property(dn, "ibm,lrdr-capacity", NULL);
523 
524 		if (!ireg)
525 			goto out;
526 
527 		maxcpus = be32_to_cpup(ireg + num_addr_cell + num_size_cell);
528 
529 		/* Double maxcpus for processors which have SMT capability */
530 		if (cpu_has_feature(CPU_FTR_SMT))
531 			maxcpus *= nthreads;
532 
533 		if (maxcpus > nr_cpu_ids) {
534 			printk(KERN_WARNING
535 			       "Partition configured for %d cpus, "
536 			       "operating system maximum is %u.\n",
537 			       maxcpus, nr_cpu_ids);
538 			maxcpus = nr_cpu_ids;
539 		} else
540 			printk(KERN_INFO "Partition configured for %d cpus.\n",
541 			       maxcpus);
542 
543 		for (cpu = 0; cpu < maxcpus; cpu++)
544 			set_cpu_possible(cpu, true);
545 	out:
546 		of_node_put(dn);
547 	}
548 	vdso_data->processorCount = num_present_cpus();
549 #endif /* CONFIG_PPC64 */
550 
551         /* Initialize CPU <=> thread mapping/
552 	 *
553 	 * WARNING: We assume that the number of threads is the same for
554 	 * every CPU in the system. If that is not the case, then some code
555 	 * here will have to be reworked
556 	 */
557 	cpu_init_thread_core_maps(nthreads);
558 
559 	/* Now that possible cpus are set, set nr_cpu_ids for later use */
560 	setup_nr_cpu_ids();
561 
562 	free_unused_pacas();
563 }
564 #endif /* CONFIG_SMP */
565 
566 #ifdef CONFIG_PCSPKR_PLATFORM
567 static __init int add_pcspkr(void)
568 {
569 	struct device_node *np;
570 	struct platform_device *pd;
571 	int ret;
572 
573 	np = of_find_compatible_node(NULL, NULL, "pnpPNP,100");
574 	of_node_put(np);
575 	if (!np)
576 		return -ENODEV;
577 
578 	pd = platform_device_alloc("pcspkr", -1);
579 	if (!pd)
580 		return -ENOMEM;
581 
582 	ret = platform_device_add(pd);
583 	if (ret)
584 		platform_device_put(pd);
585 
586 	return ret;
587 }
588 device_initcall(add_pcspkr);
589 #endif	/* CONFIG_PCSPKR_PLATFORM */
590 
591 static __init void probe_machine(void)
592 {
593 	extern struct machdep_calls __machine_desc_start;
594 	extern struct machdep_calls __machine_desc_end;
595 	unsigned int i;
596 
597 	/*
598 	 * Iterate all ppc_md structures until we find the proper
599 	 * one for the current machine type
600 	 */
601 	DBG("Probing machine type ...\n");
602 
603 	/*
604 	 * Check ppc_md is empty, if not we have a bug, ie, we setup an
605 	 * entry before probe_machine() which will be overwritten
606 	 */
607 	for (i = 0; i < (sizeof(ppc_md) / sizeof(void *)); i++) {
608 		if (((void **)&ppc_md)[i]) {
609 			printk(KERN_ERR "Entry %d in ppc_md non empty before"
610 			       " machine probe !\n", i);
611 		}
612 	}
613 
614 	for (machine_id = &__machine_desc_start;
615 	     machine_id < &__machine_desc_end;
616 	     machine_id++) {
617 		DBG("  %s ...", machine_id->name);
618 		memcpy(&ppc_md, machine_id, sizeof(struct machdep_calls));
619 		if (ppc_md.probe()) {
620 			DBG(" match !\n");
621 			break;
622 		}
623 		DBG("\n");
624 	}
625 	/* What can we do if we didn't find ? */
626 	if (machine_id >= &__machine_desc_end) {
627 		pr_err("No suitable machine description found !\n");
628 		for (;;);
629 	}
630 
631 	printk(KERN_INFO "Using %s machine description\n", ppc_md.name);
632 }
633 
634 /* Match a class of boards, not a specific device configuration. */
635 int check_legacy_ioport(unsigned long base_port)
636 {
637 	struct device_node *parent, *np = NULL;
638 	int ret = -ENODEV;
639 
640 	switch(base_port) {
641 	case I8042_DATA_REG:
642 		if (!(np = of_find_compatible_node(NULL, NULL, "pnpPNP,303")))
643 			np = of_find_compatible_node(NULL, NULL, "pnpPNP,f03");
644 		if (np) {
645 			parent = of_get_parent(np);
646 
647 			of_i8042_kbd_irq = irq_of_parse_and_map(parent, 0);
648 			if (!of_i8042_kbd_irq)
649 				of_i8042_kbd_irq = 1;
650 
651 			of_i8042_aux_irq = irq_of_parse_and_map(parent, 1);
652 			if (!of_i8042_aux_irq)
653 				of_i8042_aux_irq = 12;
654 
655 			of_node_put(np);
656 			np = parent;
657 			break;
658 		}
659 		np = of_find_node_by_type(NULL, "8042");
660 		/* Pegasos has no device_type on its 8042 node, look for the
661 		 * name instead */
662 		if (!np)
663 			np = of_find_node_by_name(NULL, "8042");
664 		if (np) {
665 			of_i8042_kbd_irq = 1;
666 			of_i8042_aux_irq = 12;
667 		}
668 		break;
669 	case FDC_BASE: /* FDC1 */
670 		np = of_find_node_by_type(NULL, "fdc");
671 		break;
672 	default:
673 		/* ipmi is supposed to fail here */
674 		break;
675 	}
676 	if (!np)
677 		return ret;
678 	parent = of_get_parent(np);
679 	if (parent) {
680 		if (of_node_is_type(parent, "isa"))
681 			ret = 0;
682 		of_node_put(parent);
683 	}
684 	of_node_put(np);
685 	return ret;
686 }
687 EXPORT_SYMBOL(check_legacy_ioport);
688 
689 /*
690  * Panic notifiers setup
691  *
692  * We have 3 notifiers for powerpc, each one from a different "nature":
693  *
694  * - ppc_panic_fadump_handler() is a hypervisor notifier, which hard-disables
695  *   IRQs and deal with the Firmware-Assisted dump, when it is configured;
696  *   should run early in the panic path.
697  *
698  * - dump_kernel_offset() is an informative notifier, just showing the KASLR
699  *   offset if we have RANDOMIZE_BASE set.
700  *
701  * - ppc_panic_platform_handler() is a low-level handler that's registered
702  *   only if the platform wishes to perform final actions in the panic path,
703  *   hence it should run late and might not even return. Currently, only
704  *   pseries and ps3 platforms register callbacks.
705  */
706 static int ppc_panic_fadump_handler(struct notifier_block *this,
707 				    unsigned long event, void *ptr)
708 {
709 	/*
710 	 * panic does a local_irq_disable, but we really
711 	 * want interrupts to be hard disabled.
712 	 */
713 	hard_irq_disable();
714 
715 	/*
716 	 * If firmware-assisted dump has been registered then trigger
717 	 * its callback and let the firmware handles everything else.
718 	 */
719 	crash_fadump(NULL, ptr);
720 
721 	return NOTIFY_DONE;
722 }
723 
724 static int dump_kernel_offset(struct notifier_block *self, unsigned long v,
725 			      void *p)
726 {
727 	pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
728 		 kaslr_offset(), KERNELBASE);
729 
730 	return NOTIFY_DONE;
731 }
732 
733 static int ppc_panic_platform_handler(struct notifier_block *this,
734 				      unsigned long event, void *ptr)
735 {
736 	/*
737 	 * This handler is only registered if we have a panic callback
738 	 * on ppc_md, hence NULL check is not needed.
739 	 * Also, it may not return, so it runs really late on panic path.
740 	 */
741 	ppc_md.panic(ptr);
742 
743 	return NOTIFY_DONE;
744 }
745 
746 static struct notifier_block ppc_fadump_block = {
747 	.notifier_call = ppc_panic_fadump_handler,
748 	.priority = INT_MAX, /* run early, to notify the firmware ASAP */
749 };
750 
751 static struct notifier_block kernel_offset_notifier = {
752 	.notifier_call = dump_kernel_offset,
753 };
754 
755 static struct notifier_block ppc_panic_block = {
756 	.notifier_call = ppc_panic_platform_handler,
757 	.priority = INT_MIN, /* may not return; must be done last */
758 };
759 
760 void __init setup_panic(void)
761 {
762 	/* Hard-disables IRQs + deal with FW-assisted dump (fadump) */
763 	atomic_notifier_chain_register(&panic_notifier_list,
764 				       &ppc_fadump_block);
765 
766 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_offset() > 0)
767 		atomic_notifier_chain_register(&panic_notifier_list,
768 					       &kernel_offset_notifier);
769 
770 	/* Low-level platform-specific routines that should run on panic */
771 	if (ppc_md.panic)
772 		atomic_notifier_chain_register(&panic_notifier_list,
773 					       &ppc_panic_block);
774 }
775 
776 #ifdef CONFIG_CHECK_CACHE_COHERENCY
777 /*
778  * For platforms that have configurable cache-coherency.  This function
779  * checks that the cache coherency setting of the kernel matches the setting
780  * left by the firmware, as indicated in the device tree.  Since a mismatch
781  * will eventually result in DMA failures, we print * and error and call
782  * BUG() in that case.
783  */
784 
785 #define KERNEL_COHERENCY	(!IS_ENABLED(CONFIG_NOT_COHERENT_CACHE))
786 
787 static int __init check_cache_coherency(void)
788 {
789 	struct device_node *np;
790 	const void *prop;
791 	bool devtree_coherency;
792 
793 	np = of_find_node_by_path("/");
794 	prop = of_get_property(np, "coherency-off", NULL);
795 	of_node_put(np);
796 
797 	devtree_coherency = prop ? false : true;
798 
799 	if (devtree_coherency != KERNEL_COHERENCY) {
800 		printk(KERN_ERR
801 			"kernel coherency:%s != device tree_coherency:%s\n",
802 			KERNEL_COHERENCY ? "on" : "off",
803 			devtree_coherency ? "on" : "off");
804 		BUG();
805 	}
806 
807 	return 0;
808 }
809 
810 late_initcall(check_cache_coherency);
811 #endif /* CONFIG_CHECK_CACHE_COHERENCY */
812 
813 void ppc_printk_progress(char *s, unsigned short hex)
814 {
815 	pr_info("%s\n", s);
816 }
817 
818 static __init void print_system_info(void)
819 {
820 	pr_info("-----------------------------------------------------\n");
821 	pr_info("phys_mem_size     = 0x%llx\n",
822 		(unsigned long long)memblock_phys_mem_size());
823 
824 	pr_info("dcache_bsize      = 0x%x\n", dcache_bsize);
825 	pr_info("icache_bsize      = 0x%x\n", icache_bsize);
826 
827 	pr_info("cpu_features      = 0x%016lx\n", cur_cpu_spec->cpu_features);
828 	pr_info("  possible        = 0x%016lx\n",
829 		(unsigned long)CPU_FTRS_POSSIBLE);
830 	pr_info("  always          = 0x%016lx\n",
831 		(unsigned long)CPU_FTRS_ALWAYS);
832 	pr_info("cpu_user_features = 0x%08x 0x%08x\n",
833 		cur_cpu_spec->cpu_user_features,
834 		cur_cpu_spec->cpu_user_features2);
835 	pr_info("mmu_features      = 0x%08x\n", cur_cpu_spec->mmu_features);
836 #ifdef CONFIG_PPC64
837 	pr_info("firmware_features = 0x%016lx\n", powerpc_firmware_features);
838 #ifdef CONFIG_PPC_BOOK3S
839 	pr_info("vmalloc start     = 0x%lx\n", KERN_VIRT_START);
840 	pr_info("IO start          = 0x%lx\n", KERN_IO_START);
841 	pr_info("vmemmap start     = 0x%lx\n", (unsigned long)vmemmap);
842 #endif
843 #endif
844 
845 	if (!early_radix_enabled())
846 		print_system_hash_info();
847 
848 	if (PHYSICAL_START > 0)
849 		pr_info("physical_start    = 0x%llx\n",
850 		       (unsigned long long)PHYSICAL_START);
851 	pr_info("-----------------------------------------------------\n");
852 }
853 
854 #ifdef CONFIG_SMP
855 static void __init smp_setup_pacas(void)
856 {
857 	int cpu;
858 
859 	for_each_possible_cpu(cpu) {
860 		if (cpu == smp_processor_id())
861 			continue;
862 		allocate_paca(cpu);
863 		set_hard_smp_processor_id(cpu, cpu_to_phys_id[cpu]);
864 	}
865 
866 	memblock_free(cpu_to_phys_id, nr_cpu_ids * sizeof(u32));
867 	cpu_to_phys_id = NULL;
868 }
869 #endif
870 
871 /*
872  * Called into from start_kernel this initializes memblock, which is used
873  * to manage page allocation until mem_init is called.
874  */
875 void __init setup_arch(char **cmdline_p)
876 {
877 	kasan_init();
878 
879 	*cmdline_p = boot_command_line;
880 
881 	/* Set a half-reasonable default so udelay does something sensible */
882 	loops_per_jiffy = 500000000 / HZ;
883 
884 	/* Unflatten the device-tree passed by prom_init or kexec */
885 	unflatten_device_tree();
886 
887 	/*
888 	 * Initialize cache line/block info from device-tree (on ppc64) or
889 	 * just cputable (on ppc32).
890 	 */
891 	initialize_cache_info();
892 
893 	/* Initialize RTAS if available. */
894 	rtas_initialize();
895 
896 	/* Check if we have an initrd provided via the device-tree. */
897 	check_for_initrd();
898 
899 	/* Probe the machine type, establish ppc_md. */
900 	probe_machine();
901 
902 	/* Setup panic notifier if requested by the platform. */
903 	setup_panic();
904 
905 	/*
906 	 * Configure ppc_md.power_save (ppc32 only, 64-bit machines do
907 	 * it from their respective probe() function.
908 	 */
909 	setup_power_save();
910 
911 	/* Discover standard serial ports. */
912 	find_legacy_serial_ports();
913 
914 	/* Register early console with the printk subsystem. */
915 	register_early_udbg_console();
916 
917 	/* Setup the various CPU maps based on the device-tree. */
918 	smp_setup_cpu_maps();
919 
920 	/* Initialize xmon. */
921 	xmon_setup();
922 
923 	/* Check the SMT related command line arguments (ppc64). */
924 	check_smt_enabled();
925 
926 	/* Parse memory topology */
927 	mem_topology_setup();
928 
929 	/*
930 	 * Release secondary cpus out of their spinloops at 0x60 now that
931 	 * we can map physical -> logical CPU ids.
932 	 *
933 	 * Freescale Book3e parts spin in a loop provided by firmware,
934 	 * so smp_release_cpus() does nothing for them.
935 	 */
936 #ifdef CONFIG_SMP
937 	smp_setup_pacas();
938 
939 	/* On BookE, setup per-core TLB data structures. */
940 	setup_tlb_core_data();
941 #endif
942 
943 	/* Print various info about the machine that has been gathered so far. */
944 	print_system_info();
945 
946 	klp_init_thread_info(&init_task);
947 
948 	setup_initial_init_mm(_stext, _etext, _edata, _end);
949 
950 	mm_iommu_init(&init_mm);
951 	irqstack_early_init();
952 	exc_lvl_early_init();
953 	emergency_stack_init();
954 
955 	mce_init();
956 	smp_release_cpus();
957 
958 	initmem_init();
959 
960 	/*
961 	 * Reserve large chunks of memory for use by CMA for KVM and hugetlb. These must
962 	 * be called after initmem_init(), so that pageblock_order is initialised.
963 	 */
964 	kvm_cma_reserve();
965 	gigantic_hugetlb_cma_reserve();
966 
967 	early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
968 
969 	if (ppc_md.setup_arch)
970 		ppc_md.setup_arch();
971 
972 	setup_barrier_nospec();
973 	setup_spectre_v2();
974 
975 	paging_init();
976 
977 	/* Initialize the MMU context management stuff. */
978 	mmu_context_init();
979 
980 	/* Interrupt code needs to be 64K-aligned. */
981 	if (IS_ENABLED(CONFIG_PPC64) && (unsigned long)_stext & 0xffff)
982 		panic("Kernelbase not 64K-aligned (0x%lx)!\n",
983 		      (unsigned long)_stext);
984 }
985