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