Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * SMP support for pSeries machines.
0004  *
0005  * Dave Engebretsen, Peter Bergner, and
0006  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
0007  *
0008  * Plus various changes from other IBM teams...
0009  */
0010 
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/sched.h>
0014 #include <linux/smp.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/delay.h>
0017 #include <linux/init.h>
0018 #include <linux/spinlock.h>
0019 #include <linux/cache.h>
0020 #include <linux/err.h>
0021 #include <linux/device.h>
0022 #include <linux/cpu.h>
0023 #include <linux/pgtable.h>
0024 
0025 #include <asm/ptrace.h>
0026 #include <linux/atomic.h>
0027 #include <asm/irq.h>
0028 #include <asm/page.h>
0029 #include <asm/io.h>
0030 #include <asm/smp.h>
0031 #include <asm/paca.h>
0032 #include <asm/machdep.h>
0033 #include <asm/cputable.h>
0034 #include <asm/firmware.h>
0035 #include <asm/rtas.h>
0036 #include <asm/vdso_datapage.h>
0037 #include <asm/cputhreads.h>
0038 #include <asm/xics.h>
0039 #include <asm/xive.h>
0040 #include <asm/dbell.h>
0041 #include <asm/plpar_wrappers.h>
0042 #include <asm/code-patching.h>
0043 #include <asm/svm.h>
0044 #include <asm/kvm_guest.h>
0045 
0046 #include "pseries.h"
0047 
0048 /*
0049  * The Primary thread of each non-boot processor was started from the OF client
0050  * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
0051  */
0052 static cpumask_var_t of_spin_mask;
0053 
0054 /* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
0055 int smp_query_cpu_stopped(unsigned int pcpu)
0056 {
0057     int cpu_status, status;
0058     int qcss_tok = rtas_token("query-cpu-stopped-state");
0059 
0060     if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
0061         printk_once(KERN_INFO
0062             "Firmware doesn't support query-cpu-stopped-state\n");
0063         return QCSS_HARDWARE_ERROR;
0064     }
0065 
0066     status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
0067     if (status != 0) {
0068         printk(KERN_ERR
0069                "RTAS query-cpu-stopped-state failed: %i\n", status);
0070         return status;
0071     }
0072 
0073     return cpu_status;
0074 }
0075 
0076 /**
0077  * smp_startup_cpu() - start the given cpu
0078  *
0079  * At boot time, there is nothing to do for primary threads which were
0080  * started from Open Firmware.  For anything else, call RTAS with the
0081  * appropriate start location.
0082  *
0083  * Returns:
0084  *  0   - failure
0085  *  1   - success
0086  */
0087 static inline int smp_startup_cpu(unsigned int lcpu)
0088 {
0089     int status;
0090     unsigned long start_here =
0091             __pa(ppc_function_entry(generic_secondary_smp_init));
0092     unsigned int pcpu;
0093     int start_cpu;
0094 
0095     if (cpumask_test_cpu(lcpu, of_spin_mask))
0096         /* Already started by OF and sitting in spin loop */
0097         return 1;
0098 
0099     pcpu = get_hard_smp_processor_id(lcpu);
0100 
0101     /* Check to see if the CPU out of FW already for kexec */
0102     if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
0103         cpumask_set_cpu(lcpu, of_spin_mask);
0104         return 1;
0105     }
0106 
0107     /* 
0108      * If the RTAS start-cpu token does not exist then presume the
0109      * cpu is already spinning.
0110      */
0111     start_cpu = rtas_token("start-cpu");
0112     if (start_cpu == RTAS_UNKNOWN_SERVICE)
0113         return 1;
0114 
0115     status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
0116     if (status != 0) {
0117         printk(KERN_ERR "start-cpu failed: %i\n", status);
0118         return 0;
0119     }
0120 
0121     return 1;
0122 }
0123 
0124 static void smp_setup_cpu(int cpu)
0125 {
0126     if (xive_enabled())
0127         xive_smp_setup_cpu();
0128     else if (cpu != boot_cpuid)
0129         xics_setup_cpu();
0130 
0131     if (firmware_has_feature(FW_FEATURE_SPLPAR))
0132         vpa_init(cpu);
0133 
0134     cpumask_clear_cpu(cpu, of_spin_mask);
0135 }
0136 
0137 static int smp_pSeries_kick_cpu(int nr)
0138 {
0139     if (nr < 0 || nr >= nr_cpu_ids)
0140         return -EINVAL;
0141 
0142     if (!smp_startup_cpu(nr))
0143         return -ENOENT;
0144 
0145     /*
0146      * The processor is currently spinning, waiting for the
0147      * cpu_start field to become non-zero After we set cpu_start,
0148      * the processor will continue on to secondary_start
0149      */
0150     paca_ptrs[nr]->cpu_start = 1;
0151 
0152     return 0;
0153 }
0154 
0155 static int pseries_smp_prepare_cpu(int cpu)
0156 {
0157     if (xive_enabled())
0158         return xive_smp_prepare_cpu(cpu);
0159     return 0;
0160 }
0161 
0162 /* Cause IPI as setup by the interrupt controller (xics or xive) */
0163 static void (*ic_cause_ipi)(int cpu) __ro_after_init;
0164 
0165 /* Use msgsndp doorbells target is a sibling, else use interrupt controller */
0166 static void dbell_or_ic_cause_ipi(int cpu)
0167 {
0168     if (doorbell_try_core_ipi(cpu))
0169         return;
0170 
0171     ic_cause_ipi(cpu);
0172 }
0173 
0174 static int pseries_cause_nmi_ipi(int cpu)
0175 {
0176     int hwcpu;
0177 
0178     if (cpu == NMI_IPI_ALL_OTHERS) {
0179         hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
0180     } else {
0181         if (cpu < 0) {
0182             WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
0183             return 0;
0184         }
0185 
0186         hwcpu = get_hard_smp_processor_id(cpu);
0187     }
0188 
0189     if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
0190         return 1;
0191 
0192     return 0;
0193 }
0194 
0195 static __init void pSeries_smp_probe(void)
0196 {
0197     if (xive_enabled())
0198         xive_smp_probe();
0199     else
0200         xics_smp_probe();
0201 
0202     /* No doorbell facility, must use the interrupt controller for IPIs */
0203     if (!cpu_has_feature(CPU_FTR_DBELL))
0204         return;
0205 
0206     /* Doorbells can only be used for IPIs between SMT siblings */
0207     if (!cpu_has_feature(CPU_FTR_SMT))
0208         return;
0209 
0210     check_kvm_guest();
0211 
0212     if (is_kvm_guest()) {
0213         /*
0214          * KVM emulates doorbells by disabling FSCR[MSGP] so msgsndp
0215          * faults to the hypervisor which then reads the instruction
0216          * from guest memory, which tends to be slower than using XIVE.
0217          */
0218         if (xive_enabled())
0219             return;
0220 
0221         /*
0222          * XICS hcalls aren't as fast, so we can use msgsndp (which
0223          * also helps exercise KVM emulation), however KVM can't
0224          * emulate secure guests because it can't read the instruction
0225          * out of their memory.
0226          */
0227         if (is_secure_guest())
0228             return;
0229     }
0230 
0231     /*
0232      * Under PowerVM, FSCR[MSGP] is enabled as guest vCPU siblings are
0233      * gang scheduled on the same physical core, so doorbells are always
0234      * faster than the interrupt controller, and they can be used by
0235      * secure guests.
0236      */
0237 
0238     ic_cause_ipi = smp_ops->cause_ipi;
0239     smp_ops->cause_ipi = dbell_or_ic_cause_ipi;
0240 }
0241 
0242 static struct smp_ops_t pseries_smp_ops = {
0243     .message_pass   = NULL, /* Use smp_muxed_ipi_message_pass */
0244     .cause_ipi  = NULL, /* Filled at runtime by pSeries_smp_probe() */
0245     .cause_nmi_ipi  = pseries_cause_nmi_ipi,
0246     .probe      = pSeries_smp_probe,
0247     .prepare_cpu    = pseries_smp_prepare_cpu,
0248     .kick_cpu   = smp_pSeries_kick_cpu,
0249     .setup_cpu  = smp_setup_cpu,
0250     .cpu_bootable   = smp_generic_cpu_bootable,
0251 };
0252 
0253 /* This is called very early */
0254 void __init smp_init_pseries(void)
0255 {
0256     int i;
0257 
0258     pr_debug(" -> smp_init_pSeries()\n");
0259     smp_ops = &pseries_smp_ops;
0260 
0261     alloc_bootmem_cpumask_var(&of_spin_mask);
0262 
0263     /*
0264      * Mark threads which are still spinning in hold loops
0265      *
0266      * We know prom_init will not have started them if RTAS supports
0267      * query-cpu-stopped-state.
0268      */
0269     if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
0270         if (cpu_has_feature(CPU_FTR_SMT)) {
0271             for_each_present_cpu(i) {
0272                 if (cpu_thread_in_core(i) == 0)
0273                     cpumask_set_cpu(i, of_spin_mask);
0274             }
0275         } else
0276             cpumask_copy(of_spin_mask, cpu_present_mask);
0277 
0278         cpumask_clear_cpu(boot_cpuid, of_spin_mask);
0279     }
0280 
0281     /* Non-lpar has additional take/give timebase */
0282     if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
0283         smp_ops->give_timebase = rtas_give_timebase;
0284         smp_ops->take_timebase = rtas_take_timebase;
0285     }
0286 
0287     pr_debug(" <- smp_init_pSeries()\n");
0288 }