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