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