Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SMP support for PowerNV machines. |
| 3 | * |
| 4 | * Copyright 2011 IBM Corp. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/cpu.h> |
| 21 | |
| 22 | #include <asm/irq.h> |
| 23 | #include <asm/smp.h> |
| 24 | #include <asm/paca.h> |
| 25 | #include <asm/machdep.h> |
| 26 | #include <asm/cputable.h> |
| 27 | #include <asm/firmware.h> |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 28 | #include <asm/rtas.h> |
| 29 | #include <asm/vdso_datapage.h> |
| 30 | #include <asm/cputhreads.h> |
| 31 | #include <asm/xics.h> |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 32 | #include <asm/opal.h> |
Preeti U Murthy | f203891 | 2014-04-11 16:01:48 +0530 | [diff] [blame] | 33 | #include <asm/runlatch.h> |
Anton Blanchard | 2751b62 | 2014-03-11 11:54:06 +1100 | [diff] [blame] | 34 | #include <asm/code-patching.h> |
Michael Neuling | d4e58e5 | 2014-06-11 15:59:28 +1000 | [diff] [blame^] | 35 | #include <asm/dbell.h> |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 36 | |
| 37 | #include "powernv.h" |
| 38 | |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 39 | #ifdef DEBUG |
| 40 | #include <asm/udbg.h> |
| 41 | #define DBG(fmt...) udbg_printf(fmt) |
| 42 | #else |
| 43 | #define DBG(fmt...) |
| 44 | #endif |
| 45 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 46 | static void pnv_smp_setup_cpu(int cpu) |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 47 | { |
| 48 | if (cpu != boot_cpuid) |
| 49 | xics_setup_cpu(); |
Michael Neuling | d4e58e5 | 2014-06-11 15:59:28 +1000 | [diff] [blame^] | 50 | |
| 51 | #ifdef CONFIG_PPC_DOORBELL |
| 52 | if (cpu_has_feature(CPU_FTR_DBELL)) |
| 53 | doorbell_setup_this_cpu(); |
| 54 | #endif |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 57 | int pnv_smp_kick_cpu(int nr) |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 58 | { |
| 59 | unsigned int pcpu = get_hard_smp_processor_id(nr); |
Anton Blanchard | 2751b62 | 2014-03-11 11:54:06 +1100 | [diff] [blame] | 60 | unsigned long start_here = |
| 61 | __pa(ppc_function_entry(generic_secondary_smp_init)); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 62 | long rc; |
| 63 | |
| 64 | BUG_ON(nr < 0 || nr >= NR_CPUS); |
| 65 | |
Benjamin Herrenschmidt | b2b4858 | 2013-05-14 15:12:31 +1000 | [diff] [blame] | 66 | /* |
| 67 | * If we already started or OPALv2 is not supported, we just |
| 68 | * kick the CPU via the PACA |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 69 | */ |
Benjamin Herrenschmidt | b2b4858 | 2013-05-14 15:12:31 +1000 | [diff] [blame] | 70 | if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPALv2)) |
| 71 | goto kick; |
| 72 | |
| 73 | /* |
| 74 | * At this point, the CPU can either be spinning on the way in |
| 75 | * from kexec or be inside OPAL waiting to be started for the |
| 76 | * first time. OPAL v3 allows us to query OPAL to know if it |
| 77 | * has the CPUs, so we do that |
| 78 | */ |
| 79 | if (firmware_has_feature(FW_FEATURE_OPALv3)) { |
| 80 | uint8_t status; |
| 81 | |
| 82 | rc = opal_query_cpu_status(pcpu, &status); |
Benjamin Herrenschmidt | 4ea9008 | 2013-05-03 17:21:00 +0000 | [diff] [blame] | 83 | if (rc != OPAL_SUCCESS) { |
Benjamin Herrenschmidt | b2b4858 | 2013-05-14 15:12:31 +1000 | [diff] [blame] | 84 | pr_warn("OPAL Error %ld querying CPU %d state\n", |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 85 | rc, nr); |
Benjamin Herrenschmidt | 4ea9008 | 2013-05-03 17:21:00 +0000 | [diff] [blame] | 86 | return -ENODEV; |
| 87 | } |
Benjamin Herrenschmidt | b2b4858 | 2013-05-14 15:12:31 +1000 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Already started, just kick it, probably coming from |
| 91 | * kexec and spinning |
| 92 | */ |
| 93 | if (status == OPAL_THREAD_STARTED) |
| 94 | goto kick; |
| 95 | |
| 96 | /* |
| 97 | * Available/inactive, let's kick it |
| 98 | */ |
| 99 | if (status == OPAL_THREAD_INACTIVE) { |
| 100 | pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", |
| 101 | nr, pcpu); |
| 102 | rc = opal_start_cpu(pcpu, start_here); |
| 103 | if (rc != OPAL_SUCCESS) { |
| 104 | pr_warn("OPAL Error %ld starting CPU %d\n", |
| 105 | rc, nr); |
| 106 | return -ENODEV; |
| 107 | } |
| 108 | } else { |
| 109 | /* |
| 110 | * An unavailable CPU (or any other unknown status) |
| 111 | * shouldn't be started. It should also |
| 112 | * not be in the possible map but currently it can |
| 113 | * happen |
| 114 | */ |
| 115 | pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable" |
| 116 | " (status %d)...\n", nr, pcpu, status); |
| 117 | return -ENODEV; |
| 118 | } |
| 119 | } else { |
| 120 | /* |
| 121 | * On OPAL v2, we just kick it and hope for the best, |
| 122 | * we must not test the error from opal_start_cpu() or |
| 123 | * we would fail to get CPUs from kexec. |
| 124 | */ |
| 125 | opal_start_cpu(pcpu, start_here); |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 126 | } |
Benjamin Herrenschmidt | b2b4858 | 2013-05-14 15:12:31 +1000 | [diff] [blame] | 127 | kick: |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 128 | return smp_generic_kick_cpu(nr); |
| 129 | } |
| 130 | |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 131 | #ifdef CONFIG_HOTPLUG_CPU |
| 132 | |
| 133 | static int pnv_smp_cpu_disable(void) |
| 134 | { |
| 135 | int cpu = smp_processor_id(); |
| 136 | |
| 137 | /* This is identical to pSeries... might consolidate by |
| 138 | * moving migrate_irqs_away to a ppc_md with default to |
| 139 | * the generic fixup_irqs. --BenH. |
| 140 | */ |
| 141 | set_cpu_online(cpu, false); |
| 142 | vdso_data->processorCount--; |
| 143 | if (cpu == boot_cpuid) |
| 144 | boot_cpuid = cpumask_any(cpu_online_mask); |
| 145 | xics_migrate_irqs_away(); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void pnv_smp_cpu_kill_self(void) |
| 150 | { |
| 151 | unsigned int cpu; |
| 152 | |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 153 | /* Standard hot unplug procedure */ |
| 154 | local_irq_disable(); |
| 155 | idle_task_exit(); |
| 156 | current->active_mm = NULL; /* for sanity */ |
| 157 | cpu = smp_processor_id(); |
| 158 | DBG("CPU%d offline\n", cpu); |
| 159 | generic_set_cpu_dead(cpu); |
| 160 | smp_wmb(); |
| 161 | |
| 162 | /* We don't want to take decrementer interrupts while we are offline, |
| 163 | * so clear LPCR:PECE1. We keep PECE2 enabled. |
| 164 | */ |
| 165 | mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1); |
| 166 | while (!generic_check_cpu_restart(cpu)) { |
Preeti U Murthy | f203891 | 2014-04-11 16:01:48 +0530 | [diff] [blame] | 167 | ppc64_runlatch_off(); |
Michael Ellerman | 8d6f7c5 | 2014-05-23 18:15:26 +1000 | [diff] [blame] | 168 | power7_nap(1); |
Preeti U Murthy | f203891 | 2014-04-11 16:01:48 +0530 | [diff] [blame] | 169 | ppc64_runlatch_on(); |
Michael Ellerman | e218602 | 2014-05-23 18:15:30 +1000 | [diff] [blame] | 170 | |
| 171 | /* Reenable IRQs briefly to clear the IPI that woke us */ |
| 172 | local_irq_enable(); |
| 173 | local_irq_disable(); |
| 174 | mb(); |
| 175 | |
| 176 | if (cpu_core_split_required()) |
| 177 | continue; |
| 178 | |
| 179 | if (!generic_check_cpu_restart(cpu)) |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 180 | DBG("CPU%d Unexpected exit while offline !\n", cpu); |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 181 | } |
| 182 | mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1); |
| 183 | DBG("CPU%d coming online...\n", cpu); |
| 184 | } |
| 185 | |
| 186 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 187 | |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 188 | static struct smp_ops_t pnv_smp_ops = { |
| 189 | .message_pass = smp_muxed_ipi_message_pass, |
| 190 | .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */ |
| 191 | .probe = xics_smp_probe, |
Benjamin Herrenschmidt | 14a43e6 | 2011-09-19 17:44:57 +0000 | [diff] [blame] | 192 | .kick_cpu = pnv_smp_kick_cpu, |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 193 | .setup_cpu = pnv_smp_setup_cpu, |
Andy Fleming | 39fd402 | 2013-08-05 14:58:35 -0500 | [diff] [blame] | 194 | .cpu_bootable = smp_generic_cpu_bootable, |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_HOTPLUG_CPU |
| 196 | .cpu_disable = pnv_smp_cpu_disable, |
| 197 | .cpu_die = generic_cpu_die, |
| 198 | #endif /* CONFIG_HOTPLUG_CPU */ |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | /* This is called very early during platform setup_arch */ |
| 202 | void __init pnv_smp_init(void) |
| 203 | { |
| 204 | smp_ops = &pnv_smp_ops; |
| 205 | |
| 206 | /* XXX We don't yet have a proper entry point from HAL, for |
| 207 | * now we rely on kexec-style entry from BML |
| 208 | */ |
| 209 | |
| 210 | #ifdef CONFIG_PPC_RTAS |
| 211 | /* Non-lpar has additional take/give timebase */ |
| 212 | if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) { |
| 213 | smp_ops->give_timebase = rtas_give_timebase; |
| 214 | smp_ops->take_timebase = rtas_take_timebase; |
| 215 | } |
| 216 | #endif /* CONFIG_PPC_RTAS */ |
Benjamin Herrenschmidt | 344eb01 | 2011-09-19 17:44:54 +0000 | [diff] [blame] | 217 | |
| 218 | #ifdef CONFIG_HOTPLUG_CPU |
| 219 | ppc_md.cpu_die = pnv_smp_cpu_kill_self; |
| 220 | #endif |
Benjamin Herrenschmidt | 55190f8 | 2011-09-19 17:44:52 +0000 | [diff] [blame] | 221 | } |