Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/kernel/smp.c |
| 3 | * |
| 4 | * SMP support for the SuperH processors. |
| 5 | * |
Paul Mundt | 173a44d | 2008-08-06 18:02:48 +0900 | [diff] [blame^] | 6 | * Copyright (C) 2002 - 2008 Paul Mundt |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 7 | * Copyright (C) 2006 - 2007 Akio Idehara |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
Evgeniy Polyakov | 66c5227 | 2007-05-31 13:46:21 +0900 | [diff] [blame] | 13 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/cache.h> |
| 15 | #include <linux/cpumask.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 19 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/atomic.h> |
| 23 | #include <asm/processor.h> |
| 24 | #include <asm/system.h> |
| 25 | #include <asm/mmu_context.h> |
| 26 | #include <asm/smp.h> |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 27 | #include <asm/cacheflush.h> |
| 28 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 30 | int __cpu_number_map[NR_CPUS]; /* Map physical to logical */ |
| 31 | int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | cpumask_t cpu_possible_map; |
David S. Miller | c8923c6 | 2005-10-13 14:41:23 -0700 | [diff] [blame] | 34 | EXPORT_SYMBOL(cpu_possible_map); |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | cpumask_t cpu_online_map; |
Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 37 | EXPORT_SYMBOL(cpu_online_map); |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static inline void __init smp_store_cpu_info(unsigned int cpu) |
| 40 | { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 41 | struct sh_cpuinfo *c = cpu_data + cpu; |
| 42 | |
| 43 | c->loops_per_jiffy = loops_per_jiffy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void __init smp_prepare_cpus(unsigned int max_cpus) |
| 47 | { |
| 48 | unsigned int cpu = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 50 | init_new_context(current, &init_mm); |
| 51 | current_thread_info()->cpu = cpu; |
| 52 | plat_prepare_cpus(max_cpus); |
| 53 | |
| 54 | #ifndef CONFIG_HOTPLUG_CPU |
| 55 | cpu_present_map = cpu_possible_map; |
| 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void __devinit smp_prepare_boot_cpu(void) |
| 60 | { |
| 61 | unsigned int cpu = smp_processor_id(); |
| 62 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 63 | __cpu_number_map[0] = cpu; |
| 64 | __cpu_logical_map[0] = cpu; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | cpu_set(cpu, cpu_online_map); |
| 67 | cpu_set(cpu, cpu_possible_map); |
| 68 | } |
| 69 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 70 | asmlinkage void __cpuinit start_secondary(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 72 | unsigned int cpu; |
| 73 | struct mm_struct *mm = &init_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 75 | atomic_inc(&mm->mm_count); |
| 76 | atomic_inc(&mm->mm_users); |
| 77 | current->active_mm = mm; |
| 78 | BUG_ON(current->mm); |
| 79 | enter_lazy_tlb(mm, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 81 | per_cpu_trap_init(); |
| 82 | |
| 83 | preempt_disable(); |
| 84 | |
| 85 | local_irq_enable(); |
| 86 | |
| 87 | calibrate_delay(); |
| 88 | |
| 89 | cpu = smp_processor_id(); |
| 90 | smp_store_cpu_info(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | cpu_set(cpu, cpu_online_map); |
| 93 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 94 | cpu_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 97 | extern struct { |
| 98 | unsigned long sp; |
| 99 | unsigned long bss_start; |
| 100 | unsigned long bss_end; |
| 101 | void *start_kernel_fn; |
| 102 | void *cpu_init_fn; |
| 103 | void *thread_info; |
| 104 | } stack_start; |
| 105 | |
| 106 | int __cpuinit __cpu_up(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 108 | struct task_struct *tsk; |
| 109 | unsigned long timeout; |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 110 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 111 | tsk = fork_idle(cpu); |
| 112 | if (IS_ERR(tsk)) { |
| 113 | printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu); |
| 114 | return PTR_ERR(tsk); |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 117 | /* Fill in data in head.S for secondary cpus */ |
| 118 | stack_start.sp = tsk->thread.sp; |
| 119 | stack_start.thread_info = tsk->stack; |
| 120 | stack_start.bss_start = 0; /* don't clear bss for secondary cpus */ |
| 121 | stack_start.start_kernel_fn = start_secondary; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 123 | flush_cache_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 125 | plat_start_cpu(cpu, (unsigned long)_stext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 127 | timeout = jiffies + HZ; |
| 128 | while (time_before(jiffies, timeout)) { |
| 129 | if (cpu_online(cpu)) |
| 130 | break; |
| 131 | |
| 132 | udelay(10); |
| 133 | } |
| 134 | |
| 135 | if (cpu_online(cpu)) |
| 136 | return 0; |
| 137 | |
| 138 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void __init smp_cpus_done(unsigned int max_cpus) |
| 142 | { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 143 | unsigned long bogosum = 0; |
| 144 | int cpu; |
| 145 | |
| 146 | for_each_online_cpu(cpu) |
| 147 | bogosum += cpu_data[cpu].loops_per_jiffy; |
| 148 | |
| 149 | printk(KERN_INFO "SMP: Total of %d processors activated " |
| 150 | "(%lu.%02lu BogoMIPS).\n", num_online_cpus(), |
| 151 | bogosum / (500000/HZ), |
| 152 | (bogosum / (5000/HZ)) % 100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void smp_send_reschedule(int cpu) |
| 156 | { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 157 | plat_send_ipi(cpu, SMP_MSG_RESCHEDULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void stop_this_cpu(void *unused) |
| 161 | { |
| 162 | cpu_clear(smp_processor_id(), cpu_online_map); |
| 163 | local_irq_disable(); |
| 164 | |
| 165 | for (;;) |
| 166 | cpu_relax(); |
| 167 | } |
| 168 | |
| 169 | void smp_send_stop(void) |
| 170 | { |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 171 | smp_call_function(stop_this_cpu, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jens Axboe | 490f5de | 2008-06-10 20:52:59 +0200 | [diff] [blame] | 174 | void arch_send_call_function_ipi(cpumask_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Jens Axboe | 490f5de | 2008-06-10 20:52:59 +0200 | [diff] [blame] | 176 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Jens Axboe | 490f5de | 2008-06-10 20:52:59 +0200 | [diff] [blame] | 178 | for_each_cpu_mask(cpu, mask) |
| 179 | plat_send_ipi(cpu, SMP_MSG_FUNCTION); |
| 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Jens Axboe | 490f5de | 2008-06-10 20:52:59 +0200 | [diff] [blame] | 182 | void arch_send_call_function_single_ipi(int cpu) |
| 183 | { |
| 184 | plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Paul Mundt | 173a44d | 2008-08-06 18:02:48 +0900 | [diff] [blame^] | 187 | void smp_message_recv(unsigned int msg) |
| 188 | { |
| 189 | switch (msg) { |
| 190 | case SMP_MSG_FUNCTION: |
| 191 | generic_smp_call_function_interrupt(); |
| 192 | break; |
| 193 | case SMP_MSG_RESCHEDULE: |
| 194 | break; |
| 195 | case SMP_MSG_FUNCTION_SINGLE: |
| 196 | generic_smp_call_function_single_interrupt(); |
| 197 | break; |
| 198 | default: |
| 199 | printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n", |
| 200 | smp_processor_id(), __func__, msg); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* Not really SMP stuff ... */ |
| 206 | int setup_profiling_timer(unsigned int multiplier) |
| 207 | { |
| 208 | return 0; |
| 209 | } |
| 210 | |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 211 | static void flush_tlb_all_ipi(void *info) |
| 212 | { |
| 213 | local_flush_tlb_all(); |
| 214 | } |
| 215 | |
| 216 | void flush_tlb_all(void) |
| 217 | { |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 218 | on_each_cpu(flush_tlb_all_ipi, 0, 1); |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void flush_tlb_mm_ipi(void *mm) |
| 222 | { |
| 223 | local_flush_tlb_mm((struct mm_struct *)mm); |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * The following tlb flush calls are invoked when old translations are |
| 228 | * being torn down, or pte attributes are changing. For single threaded |
| 229 | * address spaces, a new context is obtained on the current cpu, and tlb |
| 230 | * context on other cpus are invalidated to force a new context allocation |
| 231 | * at switch_mm time, should the mm ever be used on other cpus. For |
| 232 | * multithreaded address spaces, intercpu interrupts have to be sent. |
| 233 | * Another case where intercpu interrupts are required is when the target |
| 234 | * mm might be active on another cpu (eg debuggers doing the flushes on |
| 235 | * behalf of debugees, kswapd stealing pages from another process etc). |
| 236 | * Kanoj 07/00. |
| 237 | */ |
| 238 | |
| 239 | void flush_tlb_mm(struct mm_struct *mm) |
| 240 | { |
| 241 | preempt_disable(); |
| 242 | |
| 243 | if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) { |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 244 | smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1); |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 245 | } else { |
| 246 | int i; |
| 247 | for (i = 0; i < num_online_cpus(); i++) |
| 248 | if (smp_processor_id() != i) |
| 249 | cpu_context(i, mm) = 0; |
| 250 | } |
| 251 | local_flush_tlb_mm(mm); |
| 252 | |
| 253 | preempt_enable(); |
| 254 | } |
| 255 | |
| 256 | struct flush_tlb_data { |
| 257 | struct vm_area_struct *vma; |
| 258 | unsigned long addr1; |
| 259 | unsigned long addr2; |
| 260 | }; |
| 261 | |
| 262 | static void flush_tlb_range_ipi(void *info) |
| 263 | { |
| 264 | struct flush_tlb_data *fd = (struct flush_tlb_data *)info; |
| 265 | |
| 266 | local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2); |
| 267 | } |
| 268 | |
| 269 | void flush_tlb_range(struct vm_area_struct *vma, |
| 270 | unsigned long start, unsigned long end) |
| 271 | { |
| 272 | struct mm_struct *mm = vma->vm_mm; |
| 273 | |
| 274 | preempt_disable(); |
| 275 | if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) { |
| 276 | struct flush_tlb_data fd; |
| 277 | |
| 278 | fd.vma = vma; |
| 279 | fd.addr1 = start; |
| 280 | fd.addr2 = end; |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 281 | smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1); |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 282 | } else { |
| 283 | int i; |
| 284 | for (i = 0; i < num_online_cpus(); i++) |
| 285 | if (smp_processor_id() != i) |
| 286 | cpu_context(i, mm) = 0; |
| 287 | } |
| 288 | local_flush_tlb_range(vma, start, end); |
| 289 | preempt_enable(); |
| 290 | } |
| 291 | |
| 292 | static void flush_tlb_kernel_range_ipi(void *info) |
| 293 | { |
| 294 | struct flush_tlb_data *fd = (struct flush_tlb_data *)info; |
| 295 | |
| 296 | local_flush_tlb_kernel_range(fd->addr1, fd->addr2); |
| 297 | } |
| 298 | |
| 299 | void flush_tlb_kernel_range(unsigned long start, unsigned long end) |
| 300 | { |
| 301 | struct flush_tlb_data fd; |
| 302 | |
| 303 | fd.addr1 = start; |
| 304 | fd.addr2 = end; |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 305 | on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1); |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void flush_tlb_page_ipi(void *info) |
| 309 | { |
| 310 | struct flush_tlb_data *fd = (struct flush_tlb_data *)info; |
| 311 | |
| 312 | local_flush_tlb_page(fd->vma, fd->addr1); |
| 313 | } |
| 314 | |
| 315 | void flush_tlb_page(struct vm_area_struct *vma, unsigned long page) |
| 316 | { |
| 317 | preempt_disable(); |
| 318 | if ((atomic_read(&vma->vm_mm->mm_users) != 1) || |
| 319 | (current->mm != vma->vm_mm)) { |
| 320 | struct flush_tlb_data fd; |
| 321 | |
| 322 | fd.vma = vma; |
| 323 | fd.addr1 = page; |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 324 | smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1); |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 325 | } else { |
| 326 | int i; |
| 327 | for (i = 0; i < num_online_cpus(); i++) |
| 328 | if (smp_processor_id() != i) |
| 329 | cpu_context(i, vma->vm_mm) = 0; |
| 330 | } |
| 331 | local_flush_tlb_page(vma, page); |
| 332 | preempt_enable(); |
| 333 | } |
| 334 | |
| 335 | static void flush_tlb_one_ipi(void *info) |
| 336 | { |
| 337 | struct flush_tlb_data *fd = (struct flush_tlb_data *)info; |
| 338 | local_flush_tlb_one(fd->addr1, fd->addr2); |
| 339 | } |
| 340 | |
| 341 | void flush_tlb_one(unsigned long asid, unsigned long vaddr) |
| 342 | { |
| 343 | struct flush_tlb_data fd; |
| 344 | |
| 345 | fd.addr1 = asid; |
| 346 | fd.addr2 = vaddr; |
| 347 | |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 348 | smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1); |
Paul Mundt | 9964fa8 | 2007-09-21 18:09:55 +0900 | [diff] [blame] | 349 | local_flush_tlb_one(asid, vaddr); |
| 350 | } |