1 /* smp.c: Sparc SMP support. 2 * 3 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu) 4 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) 5 * Copyright (C) 2004 Keith M Wesolowski (wesolows@foobazco.org) 6 */ 7 8 #include <asm/head.h> 9 10 #include <linux/kernel.h> 11 #include <linux/sched.h> 12 #include <linux/threads.h> 13 #include <linux/smp.h> 14 #include <linux/interrupt.h> 15 #include <linux/kernel_stat.h> 16 #include <linux/init.h> 17 #include <linux/spinlock.h> 18 #include <linux/mm.h> 19 #include <linux/fs.h> 20 #include <linux/seq_file.h> 21 #include <linux/cache.h> 22 #include <linux/delay.h> 23 24 #include <asm/ptrace.h> 25 #include <asm/atomic.h> 26 27 #include <asm/irq.h> 28 #include <asm/page.h> 29 #include <asm/pgalloc.h> 30 #include <asm/pgtable.h> 31 #include <asm/oplib.h> 32 #include <asm/cacheflush.h> 33 #include <asm/tlbflush.h> 34 #include <asm/cpudata.h> 35 #include <asm/leon.h> 36 37 #include "irq.h" 38 39 volatile unsigned long cpu_callin_map[NR_CPUS] __cpuinitdata = {0,}; 40 unsigned char boot_cpu_id = 0; 41 unsigned char boot_cpu_id4 = 0; /* boot_cpu_id << 2 */ 42 43 cpumask_t smp_commenced_mask = CPU_MASK_NONE; 44 45 /* The only guaranteed locking primitive available on all Sparc 46 * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically 47 * places the current byte at the effective address into dest_reg and 48 * places 0xff there afterwards. Pretty lame locking primitive 49 * compared to the Alpha and the Intel no? Most Sparcs have 'swap' 50 * instruction which is much better... 51 */ 52 53 void __cpuinit smp_store_cpu_info(int id) 54 { 55 int cpu_node; 56 int mid; 57 58 cpu_data(id).udelay_val = loops_per_jiffy; 59 60 cpu_find_by_mid(id, &cpu_node); 61 cpu_data(id).clock_tick = prom_getintdefault(cpu_node, 62 "clock-frequency", 0); 63 cpu_data(id).prom_node = cpu_node; 64 mid = cpu_get_hwmid(cpu_node); 65 66 if (mid < 0) { 67 printk(KERN_NOTICE "No MID found for CPU%d at node 0x%08d", id, cpu_node); 68 mid = 0; 69 } 70 cpu_data(id).mid = mid; 71 } 72 73 void __init smp_cpus_done(unsigned int max_cpus) 74 { 75 extern void smp4m_smp_done(void); 76 extern void smp4d_smp_done(void); 77 unsigned long bogosum = 0; 78 int cpu, num = 0; 79 80 for_each_online_cpu(cpu) { 81 num++; 82 bogosum += cpu_data(cpu).udelay_val; 83 } 84 85 printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n", 86 num, bogosum/(500000/HZ), 87 (bogosum/(5000/HZ))%100); 88 89 switch(sparc_cpu_model) { 90 case sun4: 91 printk("SUN4\n"); 92 BUG(); 93 break; 94 case sun4c: 95 printk("SUN4C\n"); 96 BUG(); 97 break; 98 case sun4m: 99 smp4m_smp_done(); 100 break; 101 case sun4d: 102 smp4d_smp_done(); 103 break; 104 case sparc_leon: 105 leon_smp_done(); 106 break; 107 case sun4e: 108 printk("SUN4E\n"); 109 BUG(); 110 break; 111 case sun4u: 112 printk("SUN4U\n"); 113 BUG(); 114 break; 115 default: 116 printk("UNKNOWN!\n"); 117 BUG(); 118 break; 119 }; 120 } 121 122 void cpu_panic(void) 123 { 124 printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id()); 125 panic("SMP bolixed\n"); 126 } 127 128 struct linux_prom_registers smp_penguin_ctable __cpuinitdata = { 0 }; 129 130 void smp_send_reschedule(int cpu) 131 { 132 /* 133 * XXX missing reschedule IPI, see scheduler_ipi() 134 */ 135 } 136 137 void smp_send_stop(void) 138 { 139 } 140 141 void smp_flush_cache_all(void) 142 { 143 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all)); 144 local_flush_cache_all(); 145 } 146 147 void smp_flush_tlb_all(void) 148 { 149 xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all)); 150 local_flush_tlb_all(); 151 } 152 153 void smp_flush_cache_mm(struct mm_struct *mm) 154 { 155 if(mm->context != NO_CONTEXT) { 156 cpumask_t cpu_mask = *mm_cpumask(mm); 157 cpu_clear(smp_processor_id(), cpu_mask); 158 if (!cpus_empty(cpu_mask)) 159 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm); 160 local_flush_cache_mm(mm); 161 } 162 } 163 164 void smp_flush_tlb_mm(struct mm_struct *mm) 165 { 166 if(mm->context != NO_CONTEXT) { 167 cpumask_t cpu_mask = *mm_cpumask(mm); 168 cpu_clear(smp_processor_id(), cpu_mask); 169 if (!cpus_empty(cpu_mask)) { 170 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm); 171 if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm) 172 cpumask_copy(mm_cpumask(mm), 173 cpumask_of(smp_processor_id())); 174 } 175 local_flush_tlb_mm(mm); 176 } 177 } 178 179 void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start, 180 unsigned long end) 181 { 182 struct mm_struct *mm = vma->vm_mm; 183 184 if (mm->context != NO_CONTEXT) { 185 cpumask_t cpu_mask = *mm_cpumask(mm); 186 cpu_clear(smp_processor_id(), cpu_mask); 187 if (!cpus_empty(cpu_mask)) 188 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end); 189 local_flush_cache_range(vma, start, end); 190 } 191 } 192 193 void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, 194 unsigned long end) 195 { 196 struct mm_struct *mm = vma->vm_mm; 197 198 if (mm->context != NO_CONTEXT) { 199 cpumask_t cpu_mask = *mm_cpumask(mm); 200 cpu_clear(smp_processor_id(), cpu_mask); 201 if (!cpus_empty(cpu_mask)) 202 xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end); 203 local_flush_tlb_range(vma, start, end); 204 } 205 } 206 207 void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page) 208 { 209 struct mm_struct *mm = vma->vm_mm; 210 211 if(mm->context != NO_CONTEXT) { 212 cpumask_t cpu_mask = *mm_cpumask(mm); 213 cpu_clear(smp_processor_id(), cpu_mask); 214 if (!cpus_empty(cpu_mask)) 215 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page); 216 local_flush_cache_page(vma, page); 217 } 218 } 219 220 void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page) 221 { 222 struct mm_struct *mm = vma->vm_mm; 223 224 if(mm->context != NO_CONTEXT) { 225 cpumask_t cpu_mask = *mm_cpumask(mm); 226 cpu_clear(smp_processor_id(), cpu_mask); 227 if (!cpus_empty(cpu_mask)) 228 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page); 229 local_flush_tlb_page(vma, page); 230 } 231 } 232 233 void smp_reschedule_irq(void) 234 { 235 set_need_resched(); 236 } 237 238 void smp_flush_page_to_ram(unsigned long page) 239 { 240 /* Current theory is that those who call this are the one's 241 * who have just dirtied their cache with the pages contents 242 * in kernel space, therefore we only run this on local cpu. 243 * 244 * XXX This experiment failed, research further... -DaveM 245 */ 246 #if 1 247 xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page); 248 #endif 249 local_flush_page_to_ram(page); 250 } 251 252 void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr) 253 { 254 cpumask_t cpu_mask = *mm_cpumask(mm); 255 cpu_clear(smp_processor_id(), cpu_mask); 256 if (!cpus_empty(cpu_mask)) 257 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr); 258 local_flush_sig_insns(mm, insn_addr); 259 } 260 261 extern unsigned int lvl14_resolution; 262 263 /* /proc/profile writes can call this, don't __init it please. */ 264 static DEFINE_SPINLOCK(prof_setup_lock); 265 266 int setup_profiling_timer(unsigned int multiplier) 267 { 268 int i; 269 unsigned long flags; 270 271 /* Prevent level14 ticker IRQ flooding. */ 272 if((!multiplier) || (lvl14_resolution / multiplier) < 500) 273 return -EINVAL; 274 275 spin_lock_irqsave(&prof_setup_lock, flags); 276 for_each_possible_cpu(i) { 277 load_profile_irq(i, lvl14_resolution / multiplier); 278 prof_multiplier(i) = multiplier; 279 } 280 spin_unlock_irqrestore(&prof_setup_lock, flags); 281 282 return 0; 283 } 284 285 void __init smp_prepare_cpus(unsigned int max_cpus) 286 { 287 extern void __init smp4m_boot_cpus(void); 288 extern void __init smp4d_boot_cpus(void); 289 int i, cpuid, extra; 290 291 printk("Entering SMP Mode...\n"); 292 293 extra = 0; 294 for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) { 295 if (cpuid >= NR_CPUS) 296 extra++; 297 } 298 /* i = number of cpus */ 299 if (extra && max_cpus > i - extra) 300 printk("Warning: NR_CPUS is too low to start all cpus\n"); 301 302 smp_store_cpu_info(boot_cpu_id); 303 304 switch(sparc_cpu_model) { 305 case sun4: 306 printk("SUN4\n"); 307 BUG(); 308 break; 309 case sun4c: 310 printk("SUN4C\n"); 311 BUG(); 312 break; 313 case sun4m: 314 smp4m_boot_cpus(); 315 break; 316 case sun4d: 317 smp4d_boot_cpus(); 318 break; 319 case sparc_leon: 320 leon_boot_cpus(); 321 break; 322 case sun4e: 323 printk("SUN4E\n"); 324 BUG(); 325 break; 326 case sun4u: 327 printk("SUN4U\n"); 328 BUG(); 329 break; 330 default: 331 printk("UNKNOWN!\n"); 332 BUG(); 333 break; 334 }; 335 } 336 337 /* Set this up early so that things like the scheduler can init 338 * properly. We use the same cpu mask for both the present and 339 * possible cpu map. 340 */ 341 void __init smp_setup_cpu_possible_map(void) 342 { 343 int instance, mid; 344 345 instance = 0; 346 while (!cpu_find_by_instance(instance, NULL, &mid)) { 347 if (mid < NR_CPUS) { 348 set_cpu_possible(mid, true); 349 set_cpu_present(mid, true); 350 } 351 instance++; 352 } 353 } 354 355 void __init smp_prepare_boot_cpu(void) 356 { 357 int cpuid = hard_smp_processor_id(); 358 359 if (cpuid >= NR_CPUS) { 360 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n"); 361 prom_halt(); 362 } 363 if (cpuid != 0) 364 printk("boot cpu id != 0, this could work but is untested\n"); 365 366 current_thread_info()->cpu = cpuid; 367 set_cpu_online(cpuid, true); 368 set_cpu_possible(cpuid, true); 369 } 370 371 int __cpuinit __cpu_up(unsigned int cpu) 372 { 373 extern int __cpuinit smp4m_boot_one_cpu(int); 374 extern int __cpuinit smp4d_boot_one_cpu(int); 375 int ret=0; 376 377 switch(sparc_cpu_model) { 378 case sun4: 379 printk("SUN4\n"); 380 BUG(); 381 break; 382 case sun4c: 383 printk("SUN4C\n"); 384 BUG(); 385 break; 386 case sun4m: 387 ret = smp4m_boot_one_cpu(cpu); 388 break; 389 case sun4d: 390 ret = smp4d_boot_one_cpu(cpu); 391 break; 392 case sparc_leon: 393 ret = leon_boot_one_cpu(cpu); 394 break; 395 case sun4e: 396 printk("SUN4E\n"); 397 BUG(); 398 break; 399 case sun4u: 400 printk("SUN4U\n"); 401 BUG(); 402 break; 403 default: 404 printk("UNKNOWN!\n"); 405 BUG(); 406 break; 407 }; 408 409 if (!ret) { 410 cpu_set(cpu, smp_commenced_mask); 411 while (!cpu_online(cpu)) 412 mb(); 413 } 414 return ret; 415 } 416 417 void smp_bogo(struct seq_file *m) 418 { 419 int i; 420 421 for_each_online_cpu(i) { 422 seq_printf(m, 423 "Cpu%dBogo\t: %lu.%02lu\n", 424 i, 425 cpu_data(i).udelay_val/(500000/HZ), 426 (cpu_data(i).udelay_val/(5000/HZ))%100); 427 } 428 } 429 430 void smp_info(struct seq_file *m) 431 { 432 int i; 433 434 seq_printf(m, "State:\n"); 435 for_each_online_cpu(i) 436 seq_printf(m, "CPU%d\t\t: online\n", i); 437 } 438