Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mackerras | 69a80d3 | 2005-10-10 22:03:41 +1000 | [diff] [blame] | 2 | * SMP support for pSeries machines. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #undef DEBUG |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/smp.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/cache.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/sysdev.h> |
| 28 | #include <linux/cpu.h> |
| 29 | |
| 30 | #include <asm/ptrace.h> |
| 31 | #include <asm/atomic.h> |
| 32 | #include <asm/irq.h> |
| 33 | #include <asm/page.h> |
| 34 | #include <asm/pgtable.h> |
| 35 | #include <asm/io.h> |
| 36 | #include <asm/prom.h> |
| 37 | #include <asm/smp.h> |
| 38 | #include <asm/paca.h> |
| 39 | #include <asm/time.h> |
| 40 | #include <asm/machdep.h> |
David Gibson | 007e8f5 | 2005-10-28 15:35:50 +1000 | [diff] [blame] | 41 | #include "xics.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/cputable.h> |
Stephen Rothwell | 1ababe1 | 2005-08-03 14:35:25 +1000 | [diff] [blame] | 43 | #include <asm/firmware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/system.h> |
| 45 | #include <asm/rtas.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/pSeries_reconfig.h> |
Stephen Rothwell | bbeb3f4 | 2005-09-27 13:51:59 +1000 | [diff] [blame] | 47 | #include <asm/mpic.h> |
Paul Mackerras | 271c3f3 | 2005-11-11 23:04:40 +1100 | [diff] [blame] | 48 | #include <asm/vdso_datapage.h> |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 49 | #include <asm/cputhreads.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Michael Ellerman | a121872 | 2005-11-03 15:33:31 +1100 | [diff] [blame] | 51 | #include "plpar_wrappers.h" |
Michael Ellerman | 8feaeca | 2007-02-08 18:33:55 +1100 | [diff] [blame] | 52 | #include "pseries.h" |
Michael Ellerman | a121872 | 2005-11-03 15:33:31 +1100 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * The primary thread of each non-boot processor is recorded here before |
| 57 | * smp init. |
| 58 | */ |
| 59 | static cpumask_t of_spin_map; |
| 60 | |
Olof Johansson | f39b7a5 | 2006-08-11 00:07:08 -0500 | [diff] [blame] | 61 | extern void generic_secondary_smp_init(unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /** |
| 64 | * smp_startup_cpu() - start the given cpu |
| 65 | * |
| 66 | * At boot time, there is nothing to do for primary threads which were |
| 67 | * started from Open Firmware. For anything else, call RTAS with the |
| 68 | * appropriate start location. |
| 69 | * |
| 70 | * Returns: |
| 71 | * 0 - failure |
| 72 | * 1 - success |
| 73 | */ |
| 74 | static inline int __devinit smp_startup_cpu(unsigned int lcpu) |
| 75 | { |
| 76 | int status; |
| 77 | unsigned long start_here = __pa((u32)*((unsigned long *) |
Olof Johansson | f39b7a5 | 2006-08-11 00:07:08 -0500 | [diff] [blame] | 78 | generic_secondary_smp_init)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | unsigned int pcpu; |
Anton Blanchard | 1ed2fd2 | 2005-09-12 13:17:27 +1000 | [diff] [blame] | 80 | int start_cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if (cpu_isset(lcpu, of_spin_map)) |
| 83 | /* Already started by OF and sitting in spin loop */ |
| 84 | return 1; |
| 85 | |
| 86 | pcpu = get_hard_smp_processor_id(lcpu); |
| 87 | |
| 88 | /* Fixup atomic count: it exited inside IRQ handler. */ |
Al Viro | b5e2fc1 | 2006-01-12 01:06:01 -0800 | [diff] [blame] | 89 | task_thread_info(paca[lcpu].__current)->preempt_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Anton Blanchard | 1ed2fd2 | 2005-09-12 13:17:27 +1000 | [diff] [blame] | 91 | /* |
| 92 | * If the RTAS start-cpu token does not exist then presume the |
| 93 | * cpu is already spinning. |
| 94 | */ |
| 95 | start_cpu = rtas_token("start-cpu"); |
| 96 | if (start_cpu == RTAS_UNKNOWN_SERVICE) |
| 97 | return 1; |
| 98 | |
Michael Ellerman | 496b7a5 | 2006-02-16 14:13:53 +1100 | [diff] [blame] | 99 | status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (status != 0) { |
| 101 | printk(KERN_ERR "start-cpu failed: %i\n", status); |
| 102 | return 0; |
| 103 | } |
Anton Blanchard | 1ed2fd2 | 2005-09-12 13:17:27 +1000 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return 1; |
| 106 | } |
| 107 | |
Arnd Bergmann | cebf589 | 2005-06-23 09:43:43 +1000 | [diff] [blame] | 108 | #ifdef CONFIG_XICS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | static inline void smp_xics_do_message(int cpu, int msg) |
| 110 | { |
| 111 | set_bit(msg, &xics_ipi_message[cpu].value); |
| 112 | mb(); |
| 113 | xics_cause_IPI(cpu); |
| 114 | } |
| 115 | |
| 116 | static void smp_xics_message_pass(int target, int msg) |
| 117 | { |
| 118 | unsigned int i; |
| 119 | |
| 120 | if (target < NR_CPUS) { |
| 121 | smp_xics_do_message(target, msg); |
| 122 | } else { |
| 123 | for_each_online_cpu(i) { |
| 124 | if (target == MSG_ALL_BUT_SELF |
| 125 | && i == smp_processor_id()) |
| 126 | continue; |
| 127 | smp_xics_do_message(i, msg); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static int __init smp_xics_probe(void) |
| 133 | { |
| 134 | xics_request_IPIs(); |
| 135 | |
| 136 | return cpus_weight(cpu_possible_map); |
| 137 | } |
| 138 | |
| 139 | static void __devinit smp_xics_setup_cpu(int cpu) |
| 140 | { |
| 141 | if (cpu != boot_cpuid) |
| 142 | xics_setup_cpu(); |
| 143 | |
Stephen Rothwell | 1ababe1 | 2005-08-03 14:35:25 +1000 | [diff] [blame] | 144 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | vpa_init(cpu); |
| 146 | |
| 147 | cpu_clear(cpu, of_spin_map); |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Arnd Bergmann | cebf589 | 2005-06-23 09:43:43 +1000 | [diff] [blame] | 150 | #endif /* CONFIG_XICS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | static DEFINE_SPINLOCK(timebase_lock); |
| 153 | static unsigned long timebase = 0; |
| 154 | |
| 155 | static void __devinit pSeries_give_timebase(void) |
| 156 | { |
| 157 | spin_lock(&timebase_lock); |
| 158 | rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL); |
| 159 | timebase = get_tb(); |
| 160 | spin_unlock(&timebase_lock); |
| 161 | |
| 162 | while (timebase) |
| 163 | barrier(); |
| 164 | rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL); |
| 165 | } |
| 166 | |
| 167 | static void __devinit pSeries_take_timebase(void) |
| 168 | { |
| 169 | while (!timebase) |
| 170 | barrier(); |
| 171 | spin_lock(&timebase_lock); |
| 172 | set_tb(timebase >> 32, timebase & 0xffffffff); |
| 173 | timebase = 0; |
| 174 | spin_unlock(&timebase_lock); |
| 175 | } |
| 176 | |
| 177 | static void __devinit smp_pSeries_kick_cpu(int nr) |
| 178 | { |
| 179 | BUG_ON(nr < 0 || nr >= NR_CPUS); |
| 180 | |
| 181 | if (!smp_startup_cpu(nr)) |
| 182 | return; |
| 183 | |
| 184 | /* |
| 185 | * The processor is currently spinning, waiting for the |
| 186 | * cpu_start field to become non-zero After we set cpu_start, |
| 187 | * the processor will continue on to secondary_start |
| 188 | */ |
| 189 | paca[nr].cpu_start = 1; |
| 190 | } |
| 191 | |
| 192 | static int smp_pSeries_cpu_bootable(unsigned int nr) |
| 193 | { |
| 194 | /* Special case - we inhibit secondary thread startup |
| 195 | * during boot if the user requests it. Odd-numbered |
| 196 | * cpus are assumed to be secondary threads. |
| 197 | */ |
| 198 | if (system_state < SYSTEM_RUNNING && |
Anton Blanchard | 0231c29 | 2005-06-20 21:43:15 +1000 | [diff] [blame] | 199 | cpu_has_feature(CPU_FTR_SMT) && |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 200 | !smt_enabled_at_boot && cpu_thread_in_core(nr) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return 0; |
| 202 | |
| 203 | return 1; |
| 204 | } |
Arnd Bergmann | cebf589 | 2005-06-23 09:43:43 +1000 | [diff] [blame] | 205 | #ifdef CONFIG_MPIC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | static struct smp_ops_t pSeries_mpic_smp_ops = { |
| 207 | .message_pass = smp_mpic_message_pass, |
| 208 | .probe = smp_mpic_probe, |
| 209 | .kick_cpu = smp_pSeries_kick_cpu, |
| 210 | .setup_cpu = smp_mpic_setup_cpu, |
| 211 | }; |
Arnd Bergmann | cebf589 | 2005-06-23 09:43:43 +1000 | [diff] [blame] | 212 | #endif |
| 213 | #ifdef CONFIG_XICS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | static struct smp_ops_t pSeries_xics_smp_ops = { |
| 215 | .message_pass = smp_xics_message_pass, |
| 216 | .probe = smp_xics_probe, |
| 217 | .kick_cpu = smp_pSeries_kick_cpu, |
| 218 | .setup_cpu = smp_xics_setup_cpu, |
| 219 | .cpu_bootable = smp_pSeries_cpu_bootable, |
| 220 | }; |
Arnd Bergmann | cebf589 | 2005-06-23 09:43:43 +1000 | [diff] [blame] | 221 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | /* This is called very early */ |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 224 | static void __init smp_init_pseries(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | int i; |
| 227 | |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame^] | 228 | pr_debug(" -> smp_init_pSeries()\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | /* Mark threads which are still spinning in hold loops. */ |
Anton Blanchard | 0231c29 | 2005-06-20 21:43:15 +1000 | [diff] [blame] | 231 | if (cpu_has_feature(CPU_FTR_SMT)) { |
| 232 | for_each_present_cpu(i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (i % 2 == 0) |
| 234 | /* |
| 235 | * Even-numbered logical cpus correspond to |
| 236 | * primary threads. |
| 237 | */ |
| 238 | cpu_set(i, of_spin_map); |
| 239 | } |
Anton Blanchard | 0231c29 | 2005-06-20 21:43:15 +1000 | [diff] [blame] | 240 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | of_spin_map = cpu_present_map; |
Anton Blanchard | 0231c29 | 2005-06-20 21:43:15 +1000 | [diff] [blame] | 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | cpu_clear(boot_cpuid, of_spin_map); |
| 245 | |
| 246 | /* Non-lpar has additional take/give timebase */ |
| 247 | if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) { |
| 248 | smp_ops->give_timebase = pSeries_give_timebase; |
| 249 | smp_ops->take_timebase = pSeries_take_timebase; |
| 250 | } |
| 251 | |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame^] | 252 | pr_debug(" <- smp_init_pSeries()\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 255 | #ifdef CONFIG_MPIC |
| 256 | void __init smp_init_pseries_mpic(void) |
| 257 | { |
| 258 | smp_ops = &pSeries_mpic_smp_ops; |
| 259 | |
| 260 | smp_init_pseries(); |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | void __init smp_init_pseries_xics(void) |
| 265 | { |
| 266 | smp_ops = &pSeries_xics_smp_ops; |
| 267 | |
| 268 | smp_init_pseries(); |
| 269 | } |