linux/arch/powerpc/platforms/pseries/smp.c
<<
>>
Prefs
   1/*
   2 * SMP support for pSeries machines.
   3 *
   4 * Dave Engebretsen, Peter Bergner, and
   5 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
   6 *
   7 * Plus various changes from other IBM teams...
   8 *
   9 *      This program is free software; you can redistribute it and/or
  10 *      modify it under the terms of the GNU General Public License
  11 *      as published by the Free Software Foundation; either version
  12 *      2 of the License, or (at your option) any later version.
  13 */
  14
  15
  16#include <linux/kernel.h>
  17#include <linux/sched.h>
  18#include <linux/smp.h>
  19#include <linux/interrupt.h>
  20#include <linux/delay.h>
  21#include <linux/init.h>
  22#include <linux/spinlock.h>
  23#include <linux/cache.h>
  24#include <linux/err.h>
  25#include <linux/device.h>
  26#include <linux/cpu.h>
  27
  28#include <asm/ptrace.h>
  29#include <linux/atomic.h>
  30#include <asm/irq.h>
  31#include <asm/page.h>
  32#include <asm/pgtable.h>
  33#include <asm/io.h>
  34#include <asm/prom.h>
  35#include <asm/smp.h>
  36#include <asm/paca.h>
  37#include <asm/machdep.h>
  38#include <asm/cputable.h>
  39#include <asm/firmware.h>
  40#include <asm/rtas.h>
  41#include <asm/vdso_datapage.h>
  42#include <asm/cputhreads.h>
  43#include <asm/xics.h>
  44#include <asm/xive.h>
  45#include <asm/dbell.h>
  46#include <asm/plpar_wrappers.h>
  47#include <asm/code-patching.h>
  48#include <asm/svm.h>
  49
  50#include "pseries.h"
  51#include "offline_states.h"
  52
  53
  54/*
  55 * The Primary thread of each non-boot processor was started from the OF client
  56 * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
  57 */
  58static cpumask_var_t of_spin_mask;
  59
  60/* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
  61int smp_query_cpu_stopped(unsigned int pcpu)
  62{
  63        int cpu_status, status;
  64        int qcss_tok = rtas_token("query-cpu-stopped-state");
  65
  66        if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
  67                printk_once(KERN_INFO
  68                        "Firmware doesn't support query-cpu-stopped-state\n");
  69                return QCSS_HARDWARE_ERROR;
  70        }
  71
  72        status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
  73        if (status != 0) {
  74                printk(KERN_ERR
  75                       "RTAS query-cpu-stopped-state failed: %i\n", status);
  76                return status;
  77        }
  78
  79        return cpu_status;
  80}
  81
  82/**
  83 * smp_startup_cpu() - start the given cpu
  84 *
  85 * At boot time, there is nothing to do for primary threads which were
  86 * started from Open Firmware.  For anything else, call RTAS with the
  87 * appropriate start location.
  88 *
  89 * Returns:
  90 *      0       - failure
  91 *      1       - success
  92 */
  93static inline int smp_startup_cpu(unsigned int lcpu)
  94{
  95        int status;
  96        unsigned long start_here =
  97                        __pa(ppc_function_entry(generic_secondary_smp_init));
  98        unsigned int pcpu;
  99        int start_cpu;
 100
 101        if (cpumask_test_cpu(lcpu, of_spin_mask))
 102                /* Already started by OF and sitting in spin loop */
 103                return 1;
 104
 105        pcpu = get_hard_smp_processor_id(lcpu);
 106
 107        /* Check to see if the CPU out of FW already for kexec */
 108        if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
 109                cpumask_set_cpu(lcpu, of_spin_mask);
 110                return 1;
 111        }
 112
 113        /* Fixup atomic count: it exited inside IRQ handler. */
 114        task_thread_info(paca_ptrs[lcpu]->__current)->preempt_count     = 0;
 115#ifdef CONFIG_HOTPLUG_CPU
 116        if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
 117                goto out;
 118#endif
 119        /* 
 120         * If the RTAS start-cpu token does not exist then presume the
 121         * cpu is already spinning.
 122         */
 123        start_cpu = rtas_token("start-cpu");
 124        if (start_cpu == RTAS_UNKNOWN_SERVICE)
 125                return 1;
 126
 127        status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
 128        if (status != 0) {
 129                printk(KERN_ERR "start-cpu failed: %i\n", status);
 130                return 0;
 131        }
 132
 133#ifdef CONFIG_HOTPLUG_CPU
 134out:
 135#endif
 136        return 1;
 137}
 138
 139static void smp_setup_cpu(int cpu)
 140{
 141        if (xive_enabled())
 142                xive_smp_setup_cpu();
 143        else if (cpu != boot_cpuid)
 144                xics_setup_cpu();
 145
 146        if (firmware_has_feature(FW_FEATURE_SPLPAR))
 147                vpa_init(cpu);
 148
 149        cpumask_clear_cpu(cpu, of_spin_mask);
 150#ifdef CONFIG_HOTPLUG_CPU
 151        set_cpu_current_state(cpu, CPU_STATE_ONLINE);
 152        set_default_offline_state(cpu);
 153#endif
 154}
 155
 156static int smp_pSeries_kick_cpu(int nr)
 157{
 158        if (nr < 0 || nr >= nr_cpu_ids)
 159                return -EINVAL;
 160
 161        if (!smp_startup_cpu(nr))
 162                return -ENOENT;
 163
 164        /*
 165         * The processor is currently spinning, waiting for the
 166         * cpu_start field to become non-zero After we set cpu_start,
 167         * the processor will continue on to secondary_start
 168         */
 169        paca_ptrs[nr]->cpu_start = 1;
 170#ifdef CONFIG_HOTPLUG_CPU
 171        set_preferred_offline_state(nr, CPU_STATE_ONLINE);
 172
 173        if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
 174                long rc;
 175                unsigned long hcpuid;
 176
 177                hcpuid = get_hard_smp_processor_id(nr);
 178                rc = plpar_hcall_norets(H_PROD, hcpuid);
 179                if (rc != H_SUCCESS)
 180                        printk(KERN_ERR "Error: Prod to wake up processor %d "
 181                                                "Ret= %ld\n", nr, rc);
 182        }
 183#endif
 184
 185        return 0;
 186}
 187
 188static int pseries_smp_prepare_cpu(int cpu)
 189{
 190        if (xive_enabled())
 191                return xive_smp_prepare_cpu(cpu);
 192        return 0;
 193}
 194
 195/* Cause IPI as setup by the interrupt controller (xics or xive) */
 196static void (*ic_cause_ipi)(int cpu) __ro_after_init;
 197
 198/* Use msgsndp doorbells target is a sibling, else use interrupt controller */
 199static void dbell_or_ic_cause_ipi(int cpu)
 200{
 201        if (doorbell_try_core_ipi(cpu))
 202                return;
 203
 204        ic_cause_ipi(cpu);
 205}
 206
 207static int pseries_cause_nmi_ipi(int cpu)
 208{
 209        int hwcpu;
 210
 211        if (cpu == NMI_IPI_ALL_OTHERS) {
 212                hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
 213        } else {
 214                if (cpu < 0) {
 215                        WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
 216                        return 0;
 217                }
 218
 219                hwcpu = get_hard_smp_processor_id(cpu);
 220        }
 221
 222        if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
 223                return 1;
 224
 225        return 0;
 226}
 227
 228static __init void pSeries_smp_probe(void)
 229{
 230        if (xive_enabled())
 231                xive_smp_probe();
 232        else
 233                xics_smp_probe();
 234
 235        /* No doorbell facility, must use the interrupt controller for IPIs */
 236        if (!cpu_has_feature(CPU_FTR_DBELL))
 237                return;
 238
 239        /* Doorbells can only be used for IPIs between SMT siblings */
 240        if (!cpu_has_feature(CPU_FTR_SMT))
 241                return;
 242
 243        if (is_kvm_guest()) {
 244                /*
 245                 * KVM emulates doorbells by disabling FSCR[MSGP] so msgsndp
 246                 * faults to the hypervisor which then reads the instruction
 247                 * from guest memory, which tends to be slower than using XIVE.
 248                 */
 249                if (xive_enabled())
 250                        return;
 251
 252                /*
 253                 * XICS hcalls aren't as fast, so we can use msgsndp (which
 254                 * also helps exercise KVM emulation), however KVM can't
 255                 * emulate secure guests because it can't read the instruction
 256                 * out of their memory.
 257                 */
 258                if (is_secure_guest())
 259                        return;
 260        }
 261
 262        /*
 263         * Under PowerVM, FSCR[MSGP] is enabled as guest vCPU siblings are
 264         * gang scheduled on the same physical core, so doorbells are always
 265         * faster than the interrupt controller, and they can be used by
 266         * secure guests.
 267         */
 268
 269        ic_cause_ipi = smp_ops->cause_ipi;
 270        smp_ops->cause_ipi = dbell_or_ic_cause_ipi;
 271}
 272
 273static struct smp_ops_t pseries_smp_ops = {
 274        .message_pass   = NULL, /* Use smp_muxed_ipi_message_pass */
 275        .cause_ipi      = NULL, /* Filled at runtime by pSeries_smp_probe() */
 276        .cause_nmi_ipi  = pseries_cause_nmi_ipi,
 277        .probe          = pSeries_smp_probe,
 278        .prepare_cpu    = pseries_smp_prepare_cpu,
 279        .kick_cpu       = smp_pSeries_kick_cpu,
 280        .setup_cpu      = smp_setup_cpu,
 281        .cpu_bootable   = smp_generic_cpu_bootable,
 282};
 283
 284/* This is called very early */
 285void __init smp_init_pseries(void)
 286{
 287        int i;
 288
 289        pr_debug(" -> smp_init_pSeries()\n");
 290        smp_ops = &pseries_smp_ops;
 291
 292        alloc_bootmem_cpumask_var(&of_spin_mask);
 293
 294        /*
 295         * Mark threads which are still spinning in hold loops
 296         *
 297         * We know prom_init will not have started them if RTAS supports
 298         * query-cpu-stopped-state.
 299         */
 300        if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
 301                if (cpu_has_feature(CPU_FTR_SMT)) {
 302                        for_each_present_cpu(i) {
 303                                if (cpu_thread_in_core(i) == 0)
 304                                        cpumask_set_cpu(i, of_spin_mask);
 305                        }
 306                } else
 307                        cpumask_copy(of_spin_mask, cpu_present_mask);
 308
 309                cpumask_clear_cpu(boot_cpuid, of_spin_mask);
 310        }
 311
 312        /* Non-lpar has additional take/give timebase */
 313        if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
 314                smp_ops->give_timebase = rtas_give_timebase;
 315                smp_ops->take_timebase = rtas_take_timebase;
 316        }
 317
 318        pr_debug(" <- smp_init_pSeries()\n");
 319}
 320