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