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