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