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