Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "linux/percpu.h" |
| 7 | #include "asm/pgalloc.h" |
| 8 | #include "asm/tlb.h" |
| 9 | |
| 10 | /* For some reason, mmu_gathers are referenced when CONFIG_SMP is off. */ |
| 11 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 12 | |
| 13 | #ifdef CONFIG_SMP |
| 14 | |
| 15 | #include "linux/sched.h" |
| 16 | #include "linux/module.h" |
| 17 | #include "linux/threads.h" |
| 18 | #include "linux/interrupt.h" |
| 19 | #include "linux/err.h" |
| 20 | #include "linux/hardirq.h" |
| 21 | #include "asm/smp.h" |
| 22 | #include "asm/processor.h" |
| 23 | #include "asm/spinlock.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "kern_util.h" |
| 25 | #include "kern.h" |
| 26 | #include "irq_user.h" |
| 27 | #include "os.h" |
| 28 | |
| 29 | /* CPU online map, set by smp_boot_cpus */ |
| 30 | cpumask_t cpu_online_map = CPU_MASK_NONE; |
| 31 | cpumask_t cpu_possible_map = CPU_MASK_NONE; |
| 32 | |
| 33 | EXPORT_SYMBOL(cpu_online_map); |
| 34 | EXPORT_SYMBOL(cpu_possible_map); |
| 35 | |
| 36 | /* Per CPU bogomips and other parameters |
| 37 | * The only piece used here is the ipi pipe, which is set before SMP is |
| 38 | * started and never changed. |
| 39 | */ |
| 40 | struct cpuinfo_um cpu_data[NR_CPUS]; |
| 41 | |
| 42 | /* A statistic, can be a little off */ |
| 43 | int num_reschedules_sent = 0; |
| 44 | |
| 45 | /* Not changed after boot */ |
| 46 | struct task_struct *idle_threads[NR_CPUS]; |
| 47 | |
| 48 | void smp_send_reschedule(int cpu) |
| 49 | { |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 50 | os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | num_reschedules_sent++; |
| 52 | } |
| 53 | |
| 54 | void smp_send_stop(void) |
| 55 | { |
| 56 | int i; |
| 57 | |
| 58 | printk(KERN_INFO "Stopping all CPUs..."); |
| 59 | for(i = 0; i < num_online_cpus(); i++){ |
| 60 | if(i == current_thread->cpu) |
| 61 | continue; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 62 | os_write_file(cpu_data[i].ipi_pipe[1], "S", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | printk("done\n"); |
| 65 | } |
| 66 | |
| 67 | static cpumask_t smp_commenced_mask = CPU_MASK_NONE; |
| 68 | static cpumask_t cpu_callin_map = CPU_MASK_NONE; |
| 69 | |
| 70 | static int idle_proc(void *cpup) |
| 71 | { |
| 72 | int cpu = (int) cpup, err; |
| 73 | |
| 74 | err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1); |
| 75 | if(err < 0) |
| 76 | panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err); |
| 77 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 78 | os_set_fd_async(cpu_data[cpu].ipi_pipe[0], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | current->thread.mode.tt.extern_pid); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | wmb(); |
| 82 | if (cpu_test_and_set(cpu, cpu_callin_map)) { |
| 83 | printk("huh, CPU#%d already present??\n", cpu); |
| 84 | BUG(); |
| 85 | } |
| 86 | |
| 87 | while (!cpu_isset(cpu, smp_commenced_mask)) |
| 88 | cpu_relax(); |
| 89 | |
| 90 | cpu_set(cpu, cpu_online_map); |
| 91 | default_idle(); |
Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 92 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static struct task_struct *idle_thread(int cpu) |
| 96 | { |
| 97 | struct task_struct *new_task; |
| 98 | unsigned char c; |
| 99 | |
Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 100 | current->thread.request.u.thread.proc = idle_proc; |
| 101 | current->thread.request.u.thread.arg = (void *) cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | new_task = fork_idle(cpu); |
| 103 | if(IS_ERR(new_task)) |
| 104 | panic("copy_process failed in idle_thread, error = %ld", |
| 105 | PTR_ERR(new_task)); |
| 106 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 107 | cpu_tasks[cpu] = ((struct cpu_task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { .pid = new_task->thread.mode.tt.extern_pid, |
| 109 | .task = new_task } ); |
| 110 | idle_threads[cpu] = new_task; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 111 | CHOOSE_MODE(os_write_file(new_task->thread.mode.tt.switch_pipe[1], &c, |
| 112 | sizeof(c)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | ({ panic("skas mode doesn't support SMP"); })); |
Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 114 | return new_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void smp_prepare_cpus(unsigned int maxcpus) |
| 118 | { |
| 119 | struct task_struct *idle; |
| 120 | unsigned long waittime; |
| 121 | int err, cpu, me = smp_processor_id(); |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < ncpus; ++i) |
| 125 | cpu_set(i, cpu_possible_map); |
| 126 | |
| 127 | cpu_clear(me, cpu_online_map); |
| 128 | cpu_set(me, cpu_online_map); |
| 129 | cpu_set(me, cpu_callin_map); |
| 130 | |
| 131 | err = os_pipe(cpu_data[me].ipi_pipe, 1, 1); |
| 132 | if(err < 0) |
| 133 | panic("CPU#0 failed to create IPI pipe, errno = %d", -err); |
| 134 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 135 | os_set_fd_async(cpu_data[me].ipi_pipe[0], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | current->thread.mode.tt.extern_pid); |
| 137 | |
| 138 | for(cpu = 1; cpu < ncpus; cpu++){ |
| 139 | printk("Booting processor %d...\n", cpu); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | idle = idle_thread(cpu); |
| 142 | |
| 143 | init_idle(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | waittime = 200000000; |
| 146 | while (waittime-- && !cpu_isset(cpu, cpu_callin_map)) |
| 147 | cpu_relax(); |
| 148 | |
| 149 | if (cpu_isset(cpu, cpu_callin_map)) |
| 150 | printk("done\n"); |
| 151 | else printk("failed\n"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void smp_prepare_boot_cpu(void) |
| 156 | { |
| 157 | cpu_set(smp_processor_id(), cpu_online_map); |
| 158 | } |
| 159 | |
| 160 | int __cpu_up(unsigned int cpu) |
| 161 | { |
| 162 | cpu_set(cpu, smp_commenced_mask); |
| 163 | while (!cpu_isset(cpu, cpu_online_map)) |
| 164 | mb(); |
Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 165 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | int setup_profiling_timer(unsigned int multiplier) |
| 169 | { |
| 170 | printk(KERN_INFO "setup_profiling_timer\n"); |
Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 171 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void smp_call_function_slave(int cpu); |
| 175 | |
| 176 | void IPI_handler(int cpu) |
| 177 | { |
| 178 | unsigned char c; |
| 179 | int fd; |
| 180 | |
| 181 | fd = cpu_data[cpu].ipi_pipe[0]; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 182 | while (os_read_file(fd, &c, 1) == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | switch (c) { |
| 184 | case 'C': |
| 185 | smp_call_function_slave(cpu); |
| 186 | break; |
| 187 | |
| 188 | case 'R': |
| 189 | set_tsk_need_resched(current); |
| 190 | break; |
| 191 | |
| 192 | case 'S': |
| 193 | printk("CPU#%d stopping\n", cpu); |
| 194 | while(1) |
| 195 | pause(); |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | printk("CPU#%d received unknown IPI [%c]!\n", cpu, c); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | int hard_smp_processor_id(void) |
| 206 | { |
Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 207 | return pid_to_processor_id(os_getpid()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static DEFINE_SPINLOCK(call_lock); |
| 211 | static atomic_t scf_started; |
| 212 | static atomic_t scf_finished; |
| 213 | static void (*func)(void *info); |
| 214 | static void *info; |
| 215 | |
| 216 | void smp_call_function_slave(int cpu) |
| 217 | { |
| 218 | atomic_inc(&scf_started); |
| 219 | (*func)(info); |
| 220 | atomic_inc(&scf_finished); |
| 221 | } |
| 222 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 223 | int smp_call_function(void (*_func)(void *info), void *_info, int nonatomic, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | int wait) |
| 225 | { |
| 226 | int cpus = num_online_cpus() - 1; |
| 227 | int i; |
| 228 | |
| 229 | if (!cpus) |
| 230 | return 0; |
| 231 | |
| 232 | /* Can deadlock when called with interrupts disabled */ |
| 233 | WARN_ON(irqs_disabled()); |
| 234 | |
| 235 | spin_lock_bh(&call_lock); |
| 236 | atomic_set(&scf_started, 0); |
| 237 | atomic_set(&scf_finished, 0); |
| 238 | func = _func; |
| 239 | info = _info; |
| 240 | |
| 241 | for_each_online_cpu(i) |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 242 | os_write_file(cpu_data[i].ipi_pipe[1], "C", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | while (atomic_read(&scf_started) != cpus) |
| 245 | barrier(); |
| 246 | |
| 247 | if (wait) |
| 248 | while (atomic_read(&scf_finished) != cpus) |
| 249 | barrier(); |
| 250 | |
| 251 | spin_unlock_bh(&call_lock); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | #endif |