1/* leon_smp.c: Sparc-Leon SMP support. 2 * 3 * based on sun4m_smp.c 4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu) 5 * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB 6 * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB 7 */ 8 9#include <asm/head.h> 10 11#include <linux/kernel.h> 12#include <linux/sched.h> 13#include <linux/threads.h> 14#include <linux/smp.h> 15#include <linux/smp_lock.h> 16#include <linux/interrupt.h> 17#include <linux/kernel_stat.h> 18#include <linux/init.h> 19#include <linux/spinlock.h> 20#include <linux/mm.h> 21#include <linux/swap.h> 22#include <linux/profile.h> 23#include <linux/pm.h> 24#include <linux/delay.h> 25#include <linux/gfp.h> 26 27#include <asm/cacheflush.h> 28#include <asm/tlbflush.h> 29 30#include <asm/ptrace.h> 31#include <asm/atomic.h> 32#include <asm/irq_regs.h> 33 34#include <asm/delay.h> 35#include <asm/irq.h> 36#include <asm/page.h> 37#include <asm/pgalloc.h> 38#include <asm/pgtable.h> 39#include <asm/oplib.h> 40#include <asm/cpudata.h> 41#include <asm/asi.h> 42#include <asm/leon.h> 43#include <asm/leon_amba.h> 44 45#ifdef CONFIG_SPARC_LEON 46 47#include "irq.h" 48 49extern ctxd_t *srmmu_ctx_table_phys; 50static int smp_processors_ready; 51extern volatile unsigned long cpu_callin_map[NR_CPUS]; 52extern unsigned char boot_cpu_id; 53extern cpumask_t smp_commenced_mask; 54void __init leon_configure_cache_smp(void); 55 56static inline unsigned long do_swap(volatile unsigned long *ptr, 57 unsigned long val) 58{ 59 __asm__ __volatile__("swapa [%1] %2, %0\n\t" : "=&r"(val) 60 : "r"(ptr), "i"(ASI_LEON_DCACHE_MISS) 61 : "memory"); 62 return val; 63} 64 65static void smp_setup_percpu_timer(void); 66 67void __cpuinit leon_callin(void) 68{ 69 int cpuid = hard_smpleon_processor_id(); 70 71 local_flush_cache_all(); 72 local_flush_tlb_all(); 73 leon_configure_cache_smp(); 74 75 /* Get our local ticker going. */ 76 smp_setup_percpu_timer(); 77 78 calibrate_delay(); 79 smp_store_cpu_info(cpuid); 80 81 local_flush_cache_all(); 82 local_flush_tlb_all(); 83 84 /* 85 * Unblock the master CPU _only_ when the scheduler state 86 * of all secondary CPUs will be up-to-date, so after 87 * the SMP initialization the master will be just allowed 88 * to call the scheduler code. 89 * Allow master to continue. 90 */ 91 do_swap(&cpu_callin_map[cpuid], 1); 92 93 local_flush_cache_all(); 94 local_flush_tlb_all(); 95 96 cpu_probe(); 97 98 /* Fix idle thread fields. */ 99 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set[cpuid]) 100 : "memory" /* paranoid */); 101 102 /* Attach to the address space of init_task. */ 103 atomic_inc(&init_mm.mm_count); 104 current->active_mm = &init_mm; 105 106 while (!cpu_isset(cpuid, smp_commenced_mask)) 107 mb(); 108 109 local_irq_enable(); 110 cpu_set(cpuid, cpu_online_map); 111} 112 113/* 114 * Cycle through the processors asking the PROM to start each one. 115 */ 116 117extern struct linux_prom_registers smp_penguin_ctable; 118 119void __init leon_configure_cache_smp(void) 120{ 121 unsigned long cfg = sparc_leon3_get_dcachecfg(); 122 int me = smp_processor_id(); 123 124 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) { 125 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n", 126 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg), 127 (unsigned int)cfg, (unsigned int)me); 128 sparc_leon3_disable_cache(); 129 } else { 130 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) { 131 sparc_leon3_enable_snooping(); 132 } else { 133 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n", 134 me); 135 sparc_leon3_disable_cache(); 136 } 137 } 138 139 local_flush_cache_all(); 140 local_flush_tlb_all(); 141} 142 143void leon_smp_setbroadcast(unsigned int mask) 144{ 145 int broadcast = 146 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >> 147 LEON3_IRQMPSTATUS_BROADCAST) & 1); 148 if (!broadcast) { 149 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n", 150 leon_smp_nrcpus()); 151 if (leon_smp_nrcpus() > 1) { 152 BUG(); 153 } else { 154 prom_printf("continue anyway\n"); 155 return; 156 } 157 } 158 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask); 159} 160 161unsigned int leon_smp_getbroadcast(void) 162{ 163 unsigned int mask; 164 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast)); 165 return mask; 166} 167 168int leon_smp_nrcpus(void) 169{ 170 int nrcpu = 171 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >> 172 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1; 173 return nrcpu; 174} 175 176void __init leon_boot_cpus(void) 177{ 178 int nrcpu = leon_smp_nrcpus(); 179 int me = smp_processor_id(); 180 181 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me, 182 (unsigned int)nrcpu, (unsigned int)NR_CPUS, 183 (unsigned int)&(leon3_irqctrl_regs->mpstatus)); 184 185 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me); 186 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me); 187 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, me); 188 189 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER); 190 191 leon_configure_cache_smp(); 192 smp_setup_percpu_timer(); 193 local_flush_cache_all(); 194 195} 196 197int __cpuinit leon_boot_one_cpu(int i) 198{ 199 200 struct task_struct *p; 201 int timeout; 202 203 /* Cook up an idler for this guy. */ 204 p = fork_idle(i); 205 206 current_set[i] = task_thread_info(p); 207 208 /* See trampoline.S:leon_smp_cpu_startup for details... 209 * Initialize the contexts table 210 * Since the call to prom_startcpu() trashes the structure, 211 * we need to re-initialize it for each cpu 212 */ 213 smp_penguin_ctable.which_io = 0; 214 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys; 215 smp_penguin_ctable.reg_size = 0; 216 217 /* whirrr, whirrr, whirrrrrrrrr... */ 218 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i, 219 (unsigned int)&leon3_irqctrl_regs->mpstatus); 220 local_flush_cache_all(); 221 222 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i); 223 224 /* wheee... it's going... */ 225 for (timeout = 0; timeout < 10000; timeout++) { 226 if (cpu_callin_map[i]) 227 break; 228 udelay(200); 229 } 230 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i); 231 232 if (!(cpu_callin_map[i])) { 233 printk(KERN_ERR "Processor %d is stuck.\n", i); 234 return -ENODEV; 235 } else { 236 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i); 237 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i); 238 leon_enable_irq_cpu(LEON3_IRQ_RESCHEDULE, i); 239 } 240 241 local_flush_cache_all(); 242 return 0; 243} 244 245void __init leon_smp_done(void) 246{ 247 248 int i, first; 249 int *prev; 250 251 /* setup cpu list for irq rotation */ 252 first = 0; 253 prev = &first; 254 for (i = 0; i < NR_CPUS; i++) { 255 if (cpu_online(i)) { 256 *prev = i; 257 prev = &cpu_data(i).next; 258 } 259 } 260 *prev = first; 261 local_flush_cache_all(); 262 263 /* Free unneeded trap tables */ 264 if (!cpu_isset(1, cpu_present_map)) { 265 ClearPageReserved(virt_to_page(trapbase_cpu1)); 266 init_page_count(virt_to_page(trapbase_cpu1)); 267 free_page((unsigned long)trapbase_cpu1); 268 totalram_pages++; 269 num_physpages++; 270 } 271 if (!cpu_isset(2, cpu_present_map)) { 272 ClearPageReserved(virt_to_page(trapbase_cpu2)); 273 init_page_count(virt_to_page(trapbase_cpu2)); 274 free_page((unsigned long)trapbase_cpu2); 275 totalram_pages++; 276 num_physpages++; 277 } 278 if (!cpu_isset(3, cpu_present_map)) { 279 ClearPageReserved(virt_to_page(trapbase_cpu3)); 280 init_page_count(virt_to_page(trapbase_cpu3)); 281 free_page((unsigned long)trapbase_cpu3); 282 totalram_pages++; 283 num_physpages++; 284 } 285 /* Ok, they are spinning and ready to go. */ 286 smp_processors_ready = 1; 287 288} 289 290void leon_irq_rotate(int cpu) 291{ 292} 293 294static struct smp_funcall { 295 smpfunc_t func; 296 unsigned long arg1; 297 unsigned long arg2; 298 unsigned long arg3; 299 unsigned long arg4; 300 unsigned long arg5; 301 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */ 302 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */ 303} ccall_info; 304 305static DEFINE_SPINLOCK(cross_call_lock); 306 307/* Cross calls must be serialized, at least currently. */ 308static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1, 309 unsigned long arg2, unsigned long arg3, 310 unsigned long arg4) 311{ 312 if (smp_processors_ready) { 313 register int high = NR_CPUS - 1; 314 unsigned long flags; 315 316 spin_lock_irqsave(&cross_call_lock, flags); 317 318 { 319 /* If you make changes here, make sure gcc generates proper code... */ 320 register smpfunc_t f asm("i0") = func; 321 register unsigned long a1 asm("i1") = arg1; 322 register unsigned long a2 asm("i2") = arg2; 323 register unsigned long a3 asm("i3") = arg3; 324 register unsigned long a4 asm("i4") = arg4; 325 register unsigned long a5 asm("i5") = 0; 326 327 __asm__ __volatile__("std %0, [%6]\n\t" 328 "std %2, [%6 + 8]\n\t" 329 "std %4, [%6 + 16]\n\t" : : 330 "r"(f), "r"(a1), "r"(a2), "r"(a3), 331 "r"(a4), "r"(a5), 332 "r"(&ccall_info.func)); 333 } 334 335 /* Init receive/complete mapping, plus fire the IPI's off. */ 336 { 337 register int i; 338 339 cpu_clear(smp_processor_id(), mask); 340 cpus_and(mask, cpu_online_map, mask); 341 for (i = 0; i <= high; i++) { 342 if (cpu_isset(i, mask)) { 343 ccall_info.processors_in[i] = 0; 344 ccall_info.processors_out[i] = 0; 345 set_cpu_int(i, LEON3_IRQ_CROSS_CALL); 346 347 } 348 } 349 } 350 351 { 352 register int i; 353 354 i = 0; 355 do { 356 if (!cpu_isset(i, mask)) 357 continue; 358 359 while (!ccall_info.processors_in[i]) 360 barrier(); 361 } while (++i <= high); 362 363 i = 0; 364 do { 365 if (!cpu_isset(i, mask)) 366 continue; 367 368 while (!ccall_info.processors_out[i]) 369 barrier(); 370 } while (++i <= high); 371 } 372 373 spin_unlock_irqrestore(&cross_call_lock, flags); 374 } 375} 376 377/* Running cross calls. */ 378void leon_cross_call_irq(void) 379{ 380 int i = smp_processor_id(); 381 382 ccall_info.processors_in[i] = 1; 383 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3, 384 ccall_info.arg4, ccall_info.arg5); 385 ccall_info.processors_out[i] = 1; 386} 387 388void leon_percpu_timer_interrupt(struct pt_regs *regs) 389{ 390 struct pt_regs *old_regs; 391 int cpu = smp_processor_id(); 392 393 old_regs = set_irq_regs(regs); 394 395 leon_clear_profile_irq(cpu); 396 397 profile_tick(CPU_PROFILING); 398 399 if (!--prof_counter(cpu)) { 400 int user = user_mode(regs); 401 402 irq_enter(); 403 update_process_times(user); 404 irq_exit(); 405 406 prof_counter(cpu) = prof_multiplier(cpu); 407 } 408 set_irq_regs(old_regs); 409} 410 411static void __init smp_setup_percpu_timer(void) 412{ 413 int cpu = smp_processor_id(); 414 415 prof_counter(cpu) = prof_multiplier(cpu) = 1; 416} 417 418void __init leon_blackbox_id(unsigned *addr) 419{ 420 int rd = *addr & 0x3e000000; 421 int rs1 = rd >> 11; 422 423 /* patch places where ___b_hard_smp_processor_id appears */ 424 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */ 425 addr[1] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */ 426 addr[2] = 0x01000000; /* nop */ 427} 428 429void __init leon_blackbox_current(unsigned *addr) 430{ 431 int rd = *addr & 0x3e000000; 432 int rs1 = rd >> 11; 433 434 /* patch LOAD_CURRENT macro where ___b_load_current appears */ 435 addr[0] = 0x81444000 | rd; /* rd %asr17, reg */ 436 addr[2] = 0x8130201c | rd | rs1; /* srl reg, 0x1c, reg */ 437 addr[4] = 0x81282002 | rd | rs1; /* sll reg, 0x2, reg */ 438 439} 440 441/* 442 * CPU idle callback function 443 * See .../arch/sparc/kernel/process.c 444 */ 445void pmc_leon_idle(void) 446{ 447 __asm__ volatile ("mov %g0, %asr19"); 448} 449 450void __init leon_init_smp(void) 451{ 452 /* Patch ipi15 trap table */ 453 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m); 454 455 BTFIXUPSET_BLACKBOX(hard_smp_processor_id, leon_blackbox_id); 456 BTFIXUPSET_BLACKBOX(load_current, leon_blackbox_current); 457 BTFIXUPSET_CALL(smp_cross_call, leon_cross_call, BTFIXUPCALL_NORM); 458 BTFIXUPSET_CALL(__hard_smp_processor_id, __leon_processor_id, 459 BTFIXUPCALL_NORM); 460 461#ifndef PMC_NO_IDLE 462 /* Assign power management IDLE handler */ 463 pm_idle = pmc_leon_idle; 464 printk(KERN_INFO "leon: power management initialized\n"); 465#endif 466 467} 468 469#endif /* CONFIG_SPARC_LEON */ 470