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