GNU Linux-libre 4.14.251-gnu1
[releases.git] / arch / powerpc / kernel / setup_64.c
1 /*
2  * 
3  * Common boot and setup code.
4  *
5  * Copyright (C) 2001 PPC64 Team, IBM Corp
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12
13 #define DEBUG
14
15 #include <linux/export.h>
16 #include <linux/string.h>
17 #include <linux/sched.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/reboot.h>
21 #include <linux/delay.h>
22 #include <linux/initrd.h>
23 #include <linux/seq_file.h>
24 #include <linux/ioport.h>
25 #include <linux/console.h>
26 #include <linux/utsname.h>
27 #include <linux/tty.h>
28 #include <linux/root_dev.h>
29 #include <linux/notifier.h>
30 #include <linux/cpu.h>
31 #include <linux/unistd.h>
32 #include <linux/serial.h>
33 #include <linux/serial_8250.h>
34 #include <linux/bootmem.h>
35 #include <linux/pci.h>
36 #include <linux/lockdep.h>
37 #include <linux/memblock.h>
38 #include <linux/memory.h>
39 #include <linux/nmi.h>
40
41 #include <asm/debugfs.h>
42 #include <asm/io.h>
43 #include <asm/kdump.h>
44 #include <asm/prom.h>
45 #include <asm/processor.h>
46 #include <asm/pgtable.h>
47 #include <asm/smp.h>
48 #include <asm/elf.h>
49 #include <asm/machdep.h>
50 #include <asm/paca.h>
51 #include <asm/time.h>
52 #include <asm/cputable.h>
53 #include <asm/dt_cpu_ftrs.h>
54 #include <asm/sections.h>
55 #include <asm/btext.h>
56 #include <asm/nvram.h>
57 #include <asm/setup.h>
58 #include <asm/rtas.h>
59 #include <asm/iommu.h>
60 #include <asm/serial.h>
61 #include <asm/cache.h>
62 #include <asm/page.h>
63 #include <asm/mmu.h>
64 #include <asm/firmware.h>
65 #include <asm/xmon.h>
66 #include <asm/udbg.h>
67 #include <asm/kexec.h>
68 #include <asm/code-patching.h>
69 #include <asm/livepatch.h>
70 #include <asm/opal.h>
71 #include <asm/cputhreads.h>
72
73 #ifdef DEBUG
74 #define DBG(fmt...) udbg_printf(fmt)
75 #else
76 #define DBG(fmt...)
77 #endif
78
79 int spinning_secondaries;
80 u64 ppc64_pft_size;
81
82 struct ppc64_caches ppc64_caches = {
83         .l1d = {
84                 .block_size = 0x40,
85                 .log_block_size = 6,
86         },
87         .l1i = {
88                 .block_size = 0x40,
89                 .log_block_size = 6
90         },
91 };
92 EXPORT_SYMBOL_GPL(ppc64_caches);
93
94 #if defined(CONFIG_PPC_BOOK3E) && defined(CONFIG_SMP)
95 void __init setup_tlb_core_data(void)
96 {
97         int cpu;
98
99         BUILD_BUG_ON(offsetof(struct tlb_core_data, lock) != 0);
100
101         for_each_possible_cpu(cpu) {
102                 int first = cpu_first_thread_sibling(cpu);
103
104                 /*
105                  * If we boot via kdump on a non-primary thread,
106                  * make sure we point at the thread that actually
107                  * set up this TLB.
108                  */
109                 if (cpu_first_thread_sibling(boot_cpuid) == first)
110                         first = boot_cpuid;
111
112                 paca[cpu].tcd_ptr = &paca[first].tcd;
113
114                 /*
115                  * If we have threads, we need either tlbsrx.
116                  * or e6500 tablewalk mode, or else TLB handlers
117                  * will be racy and could produce duplicate entries.
118                  * Should we panic instead?
119                  */
120                 WARN_ONCE(smt_enabled_at_boot >= 2 &&
121                           !mmu_has_feature(MMU_FTR_USE_TLBRSRV) &&
122                           book3e_htw_mode != PPC_HTW_E6500,
123                           "%s: unsupported MMU configuration\n", __func__);
124         }
125 }
126 #endif
127
128 #ifdef CONFIG_SMP
129
130 static char *smt_enabled_cmdline;
131
132 /* Look for ibm,smt-enabled OF option */
133 void __init check_smt_enabled(void)
134 {
135         struct device_node *dn;
136         const char *smt_option;
137
138         /* Default to enabling all threads */
139         smt_enabled_at_boot = threads_per_core;
140
141         /* Allow the command line to overrule the OF option */
142         if (smt_enabled_cmdline) {
143                 if (!strcmp(smt_enabled_cmdline, "on"))
144                         smt_enabled_at_boot = threads_per_core;
145                 else if (!strcmp(smt_enabled_cmdline, "off"))
146                         smt_enabled_at_boot = 0;
147                 else {
148                         int smt;
149                         int rc;
150
151                         rc = kstrtoint(smt_enabled_cmdline, 10, &smt);
152                         if (!rc)
153                                 smt_enabled_at_boot =
154                                         min(threads_per_core, smt);
155                 }
156         } else {
157                 dn = of_find_node_by_path("/options");
158                 if (dn) {
159                         smt_option = of_get_property(dn, "ibm,smt-enabled",
160                                                      NULL);
161
162                         if (smt_option) {
163                                 if (!strcmp(smt_option, "on"))
164                                         smt_enabled_at_boot = threads_per_core;
165                                 else if (!strcmp(smt_option, "off"))
166                                         smt_enabled_at_boot = 0;
167                         }
168
169                         of_node_put(dn);
170                 }
171         }
172 }
173
174 /* Look for smt-enabled= cmdline option */
175 static int __init early_smt_enabled(char *p)
176 {
177         smt_enabled_cmdline = p;
178         return 0;
179 }
180 early_param("smt-enabled", early_smt_enabled);
181
182 #endif /* CONFIG_SMP */
183
184 /** Fix up paca fields required for the boot cpu */
185 static void __init fixup_boot_paca(void)
186 {
187         /* The boot cpu is started */
188         get_paca()->cpu_start = 1;
189         /* Allow percpu accesses to work until we setup percpu data */
190         get_paca()->data_offset = 0;
191 }
192
193 static void __init configure_exceptions(void)
194 {
195         /*
196          * Setup the trampolines from the lowmem exception vectors
197          * to the kdump kernel when not using a relocatable kernel.
198          */
199         setup_kdump_trampoline();
200
201         /* Under a PAPR hypervisor, we need hypercalls */
202         if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
203                 /* Enable AIL if possible */
204                 pseries_enable_reloc_on_exc();
205
206                 /*
207                  * Tell the hypervisor that we want our exceptions to
208                  * be taken in little endian mode.
209                  *
210                  * We don't call this for big endian as our calling convention
211                  * makes us always enter in BE, and the call may fail under
212                  * some circumstances with kdump.
213                  */
214 #ifdef __LITTLE_ENDIAN__
215                 pseries_little_endian_exceptions();
216 #endif
217         } else {
218                 /* Set endian mode using OPAL */
219                 if (firmware_has_feature(FW_FEATURE_OPAL))
220                         opal_configure_cores();
221
222                 /* AIL on native is done in cpu_ready_for_interrupts() */
223         }
224 }
225
226 static void cpu_ready_for_interrupts(void)
227 {
228         /*
229          * Enable AIL if supported, and we are in hypervisor mode. This
230          * is called once for every processor.
231          *
232          * If we are not in hypervisor mode the job is done once for
233          * the whole partition in configure_exceptions().
234          */
235         if (cpu_has_feature(CPU_FTR_HVMODE) &&
236             cpu_has_feature(CPU_FTR_ARCH_207S)) {
237                 unsigned long lpcr = mfspr(SPRN_LPCR);
238                 mtspr(SPRN_LPCR, lpcr | LPCR_AIL_3);
239         }
240
241         /*
242          * Fixup HFSCR:TM based on CPU features. The bit is set by our
243          * early asm init because at that point we haven't updated our
244          * CPU features from firmware and device-tree. Here we have,
245          * so let's do it.
246          */
247         if (cpu_has_feature(CPU_FTR_HVMODE) && !cpu_has_feature(CPU_FTR_TM_COMP))
248                 mtspr(SPRN_HFSCR, mfspr(SPRN_HFSCR) & ~HFSCR_TM);
249
250         /* Set IR and DR in PACA MSR */
251         get_paca()->kernel_msr = MSR_KERNEL;
252 }
253
254 /*
255  * Early initialization entry point. This is called by head.S
256  * with MMU translation disabled. We rely on the "feature" of
257  * the CPU that ignores the top 2 bits of the address in real
258  * mode so we can access kernel globals normally provided we
259  * only toy with things in the RMO region. From here, we do
260  * some early parsing of the device-tree to setup out MEMBLOCK
261  * data structures, and allocate & initialize the hash table
262  * and segment tables so we can start running with translation
263  * enabled.
264  *
265  * It is this function which will call the probe() callback of
266  * the various platform types and copy the matching one to the
267  * global ppc_md structure. Your platform can eventually do
268  * some very early initializations from the probe() routine, but
269  * this is not recommended, be very careful as, for example, the
270  * device-tree is not accessible via normal means at this point.
271  */
272
273 void __init early_setup(unsigned long dt_ptr)
274 {
275         static __initdata struct paca_struct boot_paca;
276
277         /* -------- printk is _NOT_ safe to use here ! ------- */
278
279         /* Try new device tree based feature discovery ... */
280         if (!dt_cpu_ftrs_init(__va(dt_ptr)))
281                 /* Otherwise use the old style CPU table */
282                 identify_cpu(0, mfspr(SPRN_PVR));
283
284         /* Assume we're on cpu 0 for now. Don't write to the paca yet! */
285         initialise_paca(&boot_paca, 0);
286         setup_paca(&boot_paca);
287         fixup_boot_paca();
288
289         /* -------- printk is now safe to use ------- */
290
291         /* Enable early debugging if any specified (see udbg.h) */
292         udbg_early_init();
293
294         DBG(" -> early_setup(), dt_ptr: 0x%lx\n", dt_ptr);
295
296         /*
297          * Do early initialization using the flattened device
298          * tree, such as retrieving the physical memory map or
299          * calculating/retrieving the hash table size.
300          */
301         early_init_devtree(__va(dt_ptr));
302
303         /* Now we know the logical id of our boot cpu, setup the paca. */
304         setup_paca(&paca[boot_cpuid]);
305         fixup_boot_paca();
306
307         /*
308          * Configure exception handlers. This include setting up trampolines
309          * if needed, setting exception endian mode, etc...
310          */
311         configure_exceptions();
312
313         /* Apply all the dynamic patching */
314         apply_feature_fixups();
315         setup_feature_keys();
316
317         /* Initialize the hash table or TLB handling */
318         early_init_mmu();
319
320         /*
321          * At this point, we can let interrupts switch to virtual mode
322          * (the MMU has been setup), so adjust the MSR in the PACA to
323          * have IR and DR set and enable AIL if it exists
324          */
325         cpu_ready_for_interrupts();
326
327         DBG(" <- early_setup()\n");
328
329 #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
330         /*
331          * This needs to be done *last* (after the above DBG() even)
332          *
333          * Right after we return from this function, we turn on the MMU
334          * which means the real-mode access trick that btext does will
335          * no longer work, it needs to switch to using a real MMU
336          * mapping. This call will ensure that it does
337          */
338         btext_map();
339 #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
340 }
341
342 #ifdef CONFIG_SMP
343 void early_setup_secondary(void)
344 {
345         /* Mark interrupts disabled in PACA */
346         get_paca()->soft_enabled = 0;
347
348         /* Initialize the hash table or TLB handling */
349         early_init_mmu_secondary();
350
351         /*
352          * At this point, we can let interrupts switch to virtual mode
353          * (the MMU has been setup), so adjust the MSR in the PACA to
354          * have IR and DR set.
355          */
356         cpu_ready_for_interrupts();
357 }
358
359 #endif /* CONFIG_SMP */
360
361 #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
362 static bool use_spinloop(void)
363 {
364         if (!IS_ENABLED(CONFIG_PPC_BOOK3E))
365                 return true;
366
367         /*
368          * When book3e boots from kexec, the ePAPR spin table does
369          * not get used.
370          */
371         return of_property_read_bool(of_chosen, "linux,booted-from-kexec");
372 }
373
374 void smp_release_cpus(void)
375 {
376         unsigned long *ptr;
377         int i;
378
379         if (!use_spinloop())
380                 return;
381
382         DBG(" -> smp_release_cpus()\n");
383
384         /* All secondary cpus are spinning on a common spinloop, release them
385          * all now so they can start to spin on their individual paca
386          * spinloops. For non SMP kernels, the secondary cpus never get out
387          * of the common spinloop.
388          */
389
390         ptr  = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
391                         - PHYSICAL_START);
392         *ptr = ppc_function_entry(generic_secondary_smp_init);
393
394         /* And wait a bit for them to catch up */
395         for (i = 0; i < 100000; i++) {
396                 mb();
397                 HMT_low();
398                 if (spinning_secondaries == 0)
399                         break;
400                 udelay(1);
401         }
402         DBG("spinning_secondaries = %d\n", spinning_secondaries);
403
404         DBG(" <- smp_release_cpus()\n");
405 }
406 #endif /* CONFIG_SMP || CONFIG_KEXEC_CORE */
407
408 /*
409  * Initialize some remaining members of the ppc64_caches and systemcfg
410  * structures
411  * (at least until we get rid of them completely). This is mostly some
412  * cache informations about the CPU that will be used by cache flush
413  * routines and/or provided to userland
414  */
415
416 static void init_cache_info(struct ppc_cache_info *info, u32 size, u32 lsize,
417                             u32 bsize, u32 sets)
418 {
419         info->size = size;
420         info->sets = sets;
421         info->line_size = lsize;
422         info->block_size = bsize;
423         info->log_block_size = __ilog2(bsize);
424         if (bsize)
425                 info->blocks_per_page = PAGE_SIZE / bsize;
426         else
427                 info->blocks_per_page = 0;
428
429         if (sets == 0)
430                 info->assoc = 0xffff;
431         else
432                 info->assoc = size / (sets * lsize);
433 }
434
435 static bool __init parse_cache_info(struct device_node *np,
436                                     bool icache,
437                                     struct ppc_cache_info *info)
438 {
439         static const char *ipropnames[] __initdata = {
440                 "i-cache-size",
441                 "i-cache-sets",
442                 "i-cache-block-size",
443                 "i-cache-line-size",
444         };
445         static const char *dpropnames[] __initdata = {
446                 "d-cache-size",
447                 "d-cache-sets",
448                 "d-cache-block-size",
449                 "d-cache-line-size",
450         };
451         const char **propnames = icache ? ipropnames : dpropnames;
452         const __be32 *sizep, *lsizep, *bsizep, *setsp;
453         u32 size, lsize, bsize, sets;
454         bool success = true;
455
456         size = 0;
457         sets = -1u;
458         lsize = bsize = cur_cpu_spec->dcache_bsize;
459         sizep = of_get_property(np, propnames[0], NULL);
460         if (sizep != NULL)
461                 size = be32_to_cpu(*sizep);
462         setsp = of_get_property(np, propnames[1], NULL);
463         if (setsp != NULL)
464                 sets = be32_to_cpu(*setsp);
465         bsizep = of_get_property(np, propnames[2], NULL);
466         lsizep = of_get_property(np, propnames[3], NULL);
467         if (bsizep == NULL)
468                 bsizep = lsizep;
469         if (lsizep == NULL)
470                 lsizep = bsizep;
471         if (lsizep != NULL)
472                 lsize = be32_to_cpu(*lsizep);
473         if (bsizep != NULL)
474                 bsize = be32_to_cpu(*bsizep);
475         if (sizep == NULL || bsizep == NULL || lsizep == NULL)
476                 success = false;
477
478         /*
479          * OF is weird .. it represents fully associative caches
480          * as "1 way" which doesn't make much sense and doesn't
481          * leave room for direct mapped. We'll assume that 0
482          * in OF means direct mapped for that reason.
483          */
484         if (sets == 1)
485                 sets = 0;
486         else if (sets == 0)
487                 sets = 1;
488
489         init_cache_info(info, size, lsize, bsize, sets);
490
491         return success;
492 }
493
494 void __init initialize_cache_info(void)
495 {
496         struct device_node *cpu = NULL, *l2, *l3 = NULL;
497         u32 pvr;
498
499         DBG(" -> initialize_cache_info()\n");
500
501         /*
502          * All shipping POWER8 machines have a firmware bug that
503          * puts incorrect information in the device-tree. This will
504          * be (hopefully) fixed for future chips but for now hard
505          * code the values if we are running on one of these
506          */
507         pvr = PVR_VER(mfspr(SPRN_PVR));
508         if (pvr == PVR_POWER8 || pvr == PVR_POWER8E ||
509             pvr == PVR_POWER8NVL) {
510                                                 /* size    lsize   blk  sets */
511                 init_cache_info(&ppc64_caches.l1i, 0x8000,   128,  128, 32);
512                 init_cache_info(&ppc64_caches.l1d, 0x10000,  128,  128, 64);
513                 init_cache_info(&ppc64_caches.l2,  0x80000,  128,  0,   512);
514                 init_cache_info(&ppc64_caches.l3,  0x800000, 128,  0,   8192);
515         } else
516                 cpu = of_find_node_by_type(NULL, "cpu");
517
518         /*
519          * We're assuming *all* of the CPUs have the same
520          * d-cache and i-cache sizes... -Peter
521          */
522         if (cpu) {
523                 if (!parse_cache_info(cpu, false, &ppc64_caches.l1d))
524                         DBG("Argh, can't find dcache properties !\n");
525
526                 if (!parse_cache_info(cpu, true, &ppc64_caches.l1i))
527                         DBG("Argh, can't find icache properties !\n");
528
529                 /*
530                  * Try to find the L2 and L3 if any. Assume they are
531                  * unified and use the D-side properties.
532                  */
533                 l2 = of_find_next_cache_node(cpu);
534                 of_node_put(cpu);
535                 if (l2) {
536                         parse_cache_info(l2, false, &ppc64_caches.l2);
537                         l3 = of_find_next_cache_node(l2);
538                         of_node_put(l2);
539                 }
540                 if (l3) {
541                         parse_cache_info(l3, false, &ppc64_caches.l3);
542                         of_node_put(l3);
543                 }
544         }
545
546         /* For use by binfmt_elf */
547         dcache_bsize = ppc64_caches.l1d.block_size;
548         icache_bsize = ppc64_caches.l1i.block_size;
549
550         cur_cpu_spec->dcache_bsize = dcache_bsize;
551         cur_cpu_spec->icache_bsize = icache_bsize;
552
553         DBG(" <- initialize_cache_info()\n");
554 }
555
556 /* This returns the limit below which memory accesses to the linear
557  * mapping are guarnateed not to cause a TLB or SLB miss. This is
558  * used to allocate interrupt or emergency stacks for which our
559  * exception entry path doesn't deal with being interrupted.
560  */
561 static __init u64 safe_stack_limit(void)
562 {
563 #ifdef CONFIG_PPC_BOOK3E
564         /* Freescale BookE bolts the entire linear mapping */
565         if (mmu_has_feature(MMU_FTR_TYPE_FSL_E))
566                 return linear_map_top;
567         /* Other BookE, we assume the first GB is bolted */
568         return 1ul << 30;
569 #else
570         if (early_radix_enabled())
571                 return ULONG_MAX;
572
573         /* BookS, the first segment is bolted */
574         if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
575                 return 1UL << SID_SHIFT_1T;
576         return 1UL << SID_SHIFT;
577 #endif
578 }
579
580 void __init irqstack_early_init(void)
581 {
582         u64 limit = safe_stack_limit();
583         unsigned int i;
584
585         /*
586          * Interrupt stacks must be in the first segment since we
587          * cannot afford to take SLB misses on them. They are not
588          * accessed in realmode.
589          */
590         for_each_possible_cpu(i) {
591                 softirq_ctx[i] = (struct thread_info *)
592                         __va(memblock_alloc_base(THREAD_SIZE,
593                                             THREAD_SIZE, limit));
594                 hardirq_ctx[i] = (struct thread_info *)
595                         __va(memblock_alloc_base(THREAD_SIZE,
596                                             THREAD_SIZE, limit));
597         }
598 }
599
600 #ifdef CONFIG_PPC_BOOK3E
601 void __init exc_lvl_early_init(void)
602 {
603         unsigned int i;
604         unsigned long sp;
605
606         for_each_possible_cpu(i) {
607                 sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
608                 critirq_ctx[i] = (struct thread_info *)__va(sp);
609                 paca[i].crit_kstack = __va(sp + THREAD_SIZE);
610
611                 sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
612                 dbgirq_ctx[i] = (struct thread_info *)__va(sp);
613                 paca[i].dbg_kstack = __va(sp + THREAD_SIZE);
614
615                 sp = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
616                 mcheckirq_ctx[i] = (struct thread_info *)__va(sp);
617                 paca[i].mc_kstack = __va(sp + THREAD_SIZE);
618         }
619
620         if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
621                 patch_exception(0x040, exc_debug_debug_book3e);
622 }
623 #endif
624
625 /*
626  * Emergency stacks are used for a range of things, from asynchronous
627  * NMIs (system reset, machine check) to synchronous, process context.
628  * We set preempt_count to zero, even though that isn't necessarily correct. To
629  * get the right value we'd need to copy it from the previous thread_info, but
630  * doing that might fault causing more problems.
631  * TODO: what to do with accounting?
632  */
633 static void emerg_stack_init_thread_info(struct thread_info *ti, int cpu)
634 {
635         ti->task = NULL;
636         ti->cpu = cpu;
637         ti->preempt_count = 0;
638         ti->local_flags = 0;
639         ti->flags = 0;
640         klp_init_thread_info(ti);
641 }
642
643 /*
644  * Stack space used when we detect a bad kernel stack pointer, and
645  * early in SMP boots before relocation is enabled. Exclusive emergency
646  * stack for machine checks.
647  */
648 void __init emergency_stack_init(void)
649 {
650         u64 limit;
651         unsigned int i;
652
653         /*
654          * Emergency stacks must be under 256MB, we cannot afford to take
655          * SLB misses on them. The ABI also requires them to be 128-byte
656          * aligned.
657          *
658          * Since we use these as temporary stacks during secondary CPU
659          * bringup, machine check, system reset, and HMI, we need to get
660          * at them in real mode. This means they must also be within the RMO
661          * region.
662          *
663          * The IRQ stacks allocated elsewhere in this file are zeroed and
664          * initialized in kernel/irq.c. These are initialized here in order
665          * to have emergency stacks available as early as possible.
666          */
667         limit = min(safe_stack_limit(), ppc64_rma_size);
668
669         for_each_possible_cpu(i) {
670                 struct thread_info *ti;
671                 ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
672                 memset(ti, 0, THREAD_SIZE);
673                 emerg_stack_init_thread_info(ti, i);
674                 paca[i].emergency_sp = (void *)ti + THREAD_SIZE;
675
676 #ifdef CONFIG_PPC_BOOK3S_64
677                 /* emergency stack for NMI exception handling. */
678                 ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
679                 memset(ti, 0, THREAD_SIZE);
680                 emerg_stack_init_thread_info(ti, i);
681                 paca[i].nmi_emergency_sp = (void *)ti + THREAD_SIZE;
682
683                 /* emergency stack for machine check exception handling. */
684                 ti = __va(memblock_alloc_base(THREAD_SIZE, THREAD_SIZE, limit));
685                 memset(ti, 0, THREAD_SIZE);
686                 emerg_stack_init_thread_info(ti, i);
687                 paca[i].mc_emergency_sp = (void *)ti + THREAD_SIZE;
688 #endif
689         }
690 }
691
692 #ifdef CONFIG_SMP
693 #define PCPU_DYN_SIZE           ()
694
695 static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
696 {
697         return __alloc_bootmem_node(NODE_DATA(early_cpu_to_node(cpu)), size, align,
698                                     __pa(MAX_DMA_ADDRESS));
699 }
700
701 static void __init pcpu_fc_free(void *ptr, size_t size)
702 {
703         free_bootmem(__pa(ptr), size);
704 }
705
706 static int pcpu_cpu_distance(unsigned int from, unsigned int to)
707 {
708         if (early_cpu_to_node(from) == early_cpu_to_node(to))
709                 return LOCAL_DISTANCE;
710         else
711                 return REMOTE_DISTANCE;
712 }
713
714 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
715 EXPORT_SYMBOL(__per_cpu_offset);
716
717 void __init setup_per_cpu_areas(void)
718 {
719         const size_t dyn_size = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
720         size_t atom_size;
721         unsigned long delta;
722         unsigned int cpu;
723         int rc;
724
725         /*
726          * Linear mapping is one of 4K, 1M and 16M.  For 4K, no need
727          * to group units.  For larger mappings, use 1M atom which
728          * should be large enough to contain a number of units.
729          */
730         if (mmu_linear_psize == MMU_PAGE_4K)
731                 atom_size = PAGE_SIZE;
732         else
733                 atom_size = 1 << 20;
734
735         rc = pcpu_embed_first_chunk(0, dyn_size, atom_size, pcpu_cpu_distance,
736                                     pcpu_fc_alloc, pcpu_fc_free);
737         if (rc < 0)
738                 panic("cannot initialize percpu area (err=%d)", rc);
739
740         delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
741         for_each_possible_cpu(cpu) {
742                 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
743                 paca[cpu].data_offset = __per_cpu_offset[cpu];
744         }
745 }
746 #endif
747
748 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
749 unsigned long memory_block_size_bytes(void)
750 {
751         if (ppc_md.memory_block_size)
752                 return ppc_md.memory_block_size();
753
754         return MIN_MEMORY_BLOCK_SIZE;
755 }
756 #endif
757
758 #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
759 struct ppc_pci_io ppc_pci_io;
760 EXPORT_SYMBOL(ppc_pci_io);
761 #endif
762
763 #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
764 u64 hw_nmi_get_sample_period(int watchdog_thresh)
765 {
766         return ppc_proc_freq * watchdog_thresh;
767 }
768 #endif
769
770 /*
771  * The perf based hardlockup detector breaks PMU event based branches, so
772  * disable it by default. Book3S has a soft-nmi hardlockup detector based
773  * on the decrementer interrupt, so it does not suffer from this problem.
774  *
775  * It is likely to get false positives in VM guests, so disable it there
776  * by default too.
777  */
778 static int __init disable_hardlockup_detector(void)
779 {
780 #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
781         hardlockup_detector_disable();
782 #else
783         if (firmware_has_feature(FW_FEATURE_LPAR))
784                 hardlockup_detector_disable();
785 #endif
786
787         return 0;
788 }
789 early_initcall(disable_hardlockup_detector);
790
791 #ifdef CONFIG_PPC_BOOK3S_64
792 static enum l1d_flush_type enabled_flush_types;
793 static void *l1d_flush_fallback_area;
794 static bool no_rfi_flush;
795 static bool no_entry_flush;
796 static bool no_uaccess_flush;
797 bool rfi_flush;
798 bool entry_flush;
799 bool uaccess_flush;
800 DEFINE_STATIC_KEY_FALSE(uaccess_flush_key);
801 EXPORT_SYMBOL(uaccess_flush_key);
802
803 static int __init handle_no_rfi_flush(char *p)
804 {
805         pr_info("rfi-flush: disabled on command line.");
806         no_rfi_flush = true;
807         return 0;
808 }
809 early_param("no_rfi_flush", handle_no_rfi_flush);
810
811 static int __init handle_no_entry_flush(char *p)
812 {
813         pr_info("entry-flush: disabled on command line.");
814         no_entry_flush = true;
815         return 0;
816 }
817 early_param("no_entry_flush", handle_no_entry_flush);
818
819 static int __init handle_no_uaccess_flush(char *p)
820 {
821         pr_info("uaccess-flush: disabled on command line.");
822         no_uaccess_flush = true;
823         return 0;
824 }
825 early_param("no_uaccess_flush", handle_no_uaccess_flush);
826
827 /*
828  * The RFI flush is not KPTI, but because users will see doco that says to use
829  * nopti we hijack that option here to also disable the RFI flush.
830  */
831 static int __init handle_no_pti(char *p)
832 {
833         pr_info("rfi-flush: disabling due to 'nopti' on command line.\n");
834         handle_no_rfi_flush(NULL);
835         return 0;
836 }
837 early_param("nopti", handle_no_pti);
838
839 static void do_nothing(void *unused)
840 {
841         /*
842          * We don't need to do the flush explicitly, just enter+exit kernel is
843          * sufficient, the RFI exit handlers will do the right thing.
844          */
845 }
846
847 void rfi_flush_enable(bool enable)
848 {
849         if (enable) {
850                 do_rfi_flush_fixups(enabled_flush_types);
851                 on_each_cpu(do_nothing, NULL, 1);
852         } else
853                 do_rfi_flush_fixups(L1D_FLUSH_NONE);
854
855         rfi_flush = enable;
856 }
857
858 void entry_flush_enable(bool enable)
859 {
860         if (enable) {
861                 do_entry_flush_fixups(enabled_flush_types);
862                 on_each_cpu(do_nothing, NULL, 1);
863         } else {
864                 do_entry_flush_fixups(L1D_FLUSH_NONE);
865         }
866
867         entry_flush = enable;
868 }
869
870 void uaccess_flush_enable(bool enable)
871 {
872         if (enable) {
873                 do_uaccess_flush_fixups(enabled_flush_types);
874                 static_branch_enable(&uaccess_flush_key);
875                 on_each_cpu(do_nothing, NULL, 1);
876         } else {
877                 static_branch_disable(&uaccess_flush_key);
878                 do_uaccess_flush_fixups(L1D_FLUSH_NONE);
879         }
880
881         uaccess_flush = enable;
882 }
883
884 static void __ref init_fallback_flush(void)
885 {
886         u64 l1d_size, limit;
887         int cpu;
888
889         /* Only allocate the fallback flush area once (at boot time). */
890         if (l1d_flush_fallback_area)
891                 return;
892
893         l1d_size = ppc64_caches.l1d.size;
894         limit = min(safe_stack_limit(), ppc64_rma_size);
895
896         /*
897          * Align to L1d size, and size it at 2x L1d size, to catch possible
898          * hardware prefetch runoff. We don't have a recipe for load patterns to
899          * reliably avoid the prefetcher.
900          */
901         l1d_flush_fallback_area = __va(memblock_alloc_base(l1d_size * 2, l1d_size, limit));
902         memset(l1d_flush_fallback_area, 0, l1d_size * 2);
903
904         for_each_possible_cpu(cpu) {
905                 paca[cpu].rfi_flush_fallback_area = l1d_flush_fallback_area;
906                 paca[cpu].l1d_flush_size = l1d_size;
907         }
908 }
909
910 void setup_rfi_flush(enum l1d_flush_type types, bool enable)
911 {
912         if (types & L1D_FLUSH_FALLBACK) {
913                 pr_info("rfi-flush: fallback displacement flush available\n");
914                 init_fallback_flush();
915         }
916
917         if (types & L1D_FLUSH_ORI)
918                 pr_info("rfi-flush: ori type flush available\n");
919
920         if (types & L1D_FLUSH_MTTRIG)
921                 pr_info("rfi-flush: mttrig type flush available\n");
922
923         enabled_flush_types = types;
924
925         if (!cpu_mitigations_off() && !no_rfi_flush)
926                 rfi_flush_enable(enable);
927 }
928
929 void setup_entry_flush(bool enable)
930 {
931         if (cpu_mitigations_off())
932                 return;
933
934         if (!no_entry_flush)
935                 entry_flush_enable(enable);
936 }
937
938 void setup_uaccess_flush(bool enable)
939 {
940         if (cpu_mitigations_off())
941                 return;
942
943         if (!no_uaccess_flush)
944                 uaccess_flush_enable(enable);
945 }
946
947 #ifdef CONFIG_DEBUG_FS
948 static int rfi_flush_set(void *data, u64 val)
949 {
950         bool enable;
951
952         if (val == 1)
953                 enable = true;
954         else if (val == 0)
955                 enable = false;
956         else
957                 return -EINVAL;
958
959         /* Only do anything if we're changing state */
960         if (enable != rfi_flush)
961                 rfi_flush_enable(enable);
962
963         return 0;
964 }
965
966 static int rfi_flush_get(void *data, u64 *val)
967 {
968         *val = rfi_flush ? 1 : 0;
969         return 0;
970 }
971
972 DEFINE_SIMPLE_ATTRIBUTE(fops_rfi_flush, rfi_flush_get, rfi_flush_set, "%llu\n");
973
974 static int entry_flush_set(void *data, u64 val)
975 {
976         bool enable;
977
978         if (val == 1)
979                 enable = true;
980         else if (val == 0)
981                 enable = false;
982         else
983                 return -EINVAL;
984
985         /* Only do anything if we're changing state */
986         if (enable != entry_flush)
987                 entry_flush_enable(enable);
988
989         return 0;
990 }
991
992 static int entry_flush_get(void *data, u64 *val)
993 {
994         *val = entry_flush ? 1 : 0;
995         return 0;
996 }
997
998 DEFINE_SIMPLE_ATTRIBUTE(fops_entry_flush, entry_flush_get, entry_flush_set, "%llu\n");
999
1000 static int uaccess_flush_set(void *data, u64 val)
1001 {
1002         bool enable;
1003
1004         if (val == 1)
1005                 enable = true;
1006         else if (val == 0)
1007                 enable = false;
1008         else
1009                 return -EINVAL;
1010
1011         /* Only do anything if we're changing state */
1012         if (enable != uaccess_flush)
1013                 uaccess_flush_enable(enable);
1014
1015         return 0;
1016 }
1017
1018 static int uaccess_flush_get(void *data, u64 *val)
1019 {
1020         *val = uaccess_flush ? 1 : 0;
1021         return 0;
1022 }
1023
1024 DEFINE_SIMPLE_ATTRIBUTE(fops_uaccess_flush, uaccess_flush_get, uaccess_flush_set, "%llu\n");
1025
1026 static __init int rfi_flush_debugfs_init(void)
1027 {
1028         debugfs_create_file("rfi_flush", 0600, powerpc_debugfs_root, NULL, &fops_rfi_flush);
1029         debugfs_create_file("entry_flush", 0600, powerpc_debugfs_root, NULL, &fops_entry_flush);
1030         debugfs_create_file("uaccess_flush", 0600, powerpc_debugfs_root, NULL, &fops_uaccess_flush);
1031         return 0;
1032 }
1033 device_initcall(rfi_flush_debugfs_init);
1034 #endif
1035 #endif /* CONFIG_PPC_BOOK3S_64 */