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/interrupt.h> 16 #include <linux/kernel_stat.h> 17 #include <linux/of.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 #include <linux/cpu.h> 27 #include <linux/clockchips.h> 28 29 #include <asm/cacheflush.h> 30 #include <asm/tlbflush.h> 31 32 #include <asm/ptrace.h> 33 #include <linux/atomic.h> 34 #include <asm/irq_regs.h> 35 #include <asm/traps.h> 36 37 #include <asm/delay.h> 38 #include <asm/irq.h> 39 #include <asm/page.h> 40 #include <asm/pgalloc.h> 41 #include <asm/pgtable.h> 42 #include <asm/oplib.h> 43 #include <asm/cpudata.h> 44 #include <asm/asi.h> 45 #include <asm/leon.h> 46 #include <asm/leon_amba.h> 47 #include <asm/timer.h> 48 49 #include "kernel.h" 50 51 #ifdef CONFIG_SPARC_LEON 52 53 #include "irq.h" 54 55 extern ctxd_t *srmmu_ctx_table_phys; 56 static int smp_processors_ready; 57 extern volatile unsigned long cpu_callin_map[NR_CPUS]; 58 extern cpumask_t smp_commenced_mask; 59 void __init leon_configure_cache_smp(void); 60 static void leon_ipi_init(void); 61 62 /* IRQ number of LEON IPIs */ 63 int leon_ipi_irq = LEON3_IRQ_IPI_DEFAULT; 64 65 static inline unsigned long do_swap(volatile unsigned long *ptr, 66 unsigned long val) 67 { 68 __asm__ __volatile__("swapa [%2] %3, %0\n\t" : "=&r"(val) 69 : "0"(val), "r"(ptr), "i"(ASI_LEON_DCACHE_MISS) 70 : "memory"); 71 return val; 72 } 73 74 void __cpuinit leon_callin(void) 75 { 76 int cpuid = hard_smp_processor_id(); 77 78 local_ops->cache_all(); 79 local_ops->tlb_all(); 80 leon_configure_cache_smp(); 81 82 notify_cpu_starting(cpuid); 83 84 /* Get our local ticker going. */ 85 register_percpu_ce(cpuid); 86 87 calibrate_delay(); 88 smp_store_cpu_info(cpuid); 89 90 local_ops->cache_all(); 91 local_ops->tlb_all(); 92 93 /* 94 * Unblock the master CPU _only_ when the scheduler state 95 * of all secondary CPUs will be up-to-date, so after 96 * the SMP initialization the master will be just allowed 97 * to call the scheduler code. 98 * Allow master to continue. 99 */ 100 do_swap(&cpu_callin_map[cpuid], 1); 101 102 local_ops->cache_all(); 103 local_ops->tlb_all(); 104 105 /* Fix idle thread fields. */ 106 __asm__ __volatile__("ld [%0], %%g6\n\t" : : "r"(¤t_set[cpuid]) 107 : "memory" /* paranoid */); 108 109 /* Attach to the address space of init_task. */ 110 atomic_inc(&init_mm.mm_count); 111 current->active_mm = &init_mm; 112 113 while (!cpumask_test_cpu(cpuid, &smp_commenced_mask)) 114 mb(); 115 116 local_irq_enable(); 117 set_cpu_online(cpuid, true); 118 } 119 120 /* 121 * Cycle through the processors asking the PROM to start each one. 122 */ 123 124 extern struct linux_prom_registers smp_penguin_ctable; 125 126 void __init leon_configure_cache_smp(void) 127 { 128 unsigned long cfg = sparc_leon3_get_dcachecfg(); 129 int me = smp_processor_id(); 130 131 if (ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg) > 4) { 132 printk(KERN_INFO "Note: SMP with snooping only works on 4k cache, found %dk(0x%x) on cpu %d, disabling caches\n", 133 (unsigned int)ASI_LEON3_SYSCTRL_CFG_SSIZE(cfg), 134 (unsigned int)cfg, (unsigned int)me); 135 sparc_leon3_disable_cache(); 136 } else { 137 if (cfg & ASI_LEON3_SYSCTRL_CFG_SNOOPING) { 138 sparc_leon3_enable_snooping(); 139 } else { 140 printk(KERN_INFO "Note: You have to enable snooping in the vhdl model cpu %d, disabling caches\n", 141 me); 142 sparc_leon3_disable_cache(); 143 } 144 } 145 146 local_ops->cache_all(); 147 local_ops->tlb_all(); 148 } 149 150 void leon_smp_setbroadcast(unsigned int mask) 151 { 152 int broadcast = 153 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >> 154 LEON3_IRQMPSTATUS_BROADCAST) & 1); 155 if (!broadcast) { 156 prom_printf("######## !!!! The irqmp-ctrl must have broadcast enabled, smp wont work !!!!! ####### nr cpus: %d\n", 157 leon_smp_nrcpus()); 158 if (leon_smp_nrcpus() > 1) { 159 BUG(); 160 } else { 161 prom_printf("continue anyway\n"); 162 return; 163 } 164 } 165 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpbroadcast), mask); 166 } 167 168 unsigned int leon_smp_getbroadcast(void) 169 { 170 unsigned int mask; 171 mask = LEON_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpbroadcast)); 172 return mask; 173 } 174 175 int leon_smp_nrcpus(void) 176 { 177 int nrcpu = 178 ((LEON3_BYPASS_LOAD_PA(&(leon3_irqctrl_regs->mpstatus)) >> 179 LEON3_IRQMPSTATUS_CPUNR) & 0xf) + 1; 180 return nrcpu; 181 } 182 183 void __init leon_boot_cpus(void) 184 { 185 int nrcpu = leon_smp_nrcpus(); 186 int me = smp_processor_id(); 187 188 /* Setup IPI */ 189 leon_ipi_init(); 190 191 printk(KERN_INFO "%d:(%d:%d) cpus mpirq at 0x%x\n", (unsigned int)me, 192 (unsigned int)nrcpu, (unsigned int)NR_CPUS, 193 (unsigned int)&(leon3_irqctrl_regs->mpstatus)); 194 195 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, me); 196 leon_enable_irq_cpu(LEON3_IRQ_TICKER, me); 197 leon_enable_irq_cpu(leon_ipi_irq, me); 198 199 leon_smp_setbroadcast(1 << LEON3_IRQ_TICKER); 200 201 leon_configure_cache_smp(); 202 local_ops->cache_all(); 203 204 } 205 206 int __cpuinit leon_boot_one_cpu(int i, struct task_struct *idle) 207 { 208 int timeout; 209 210 current_set[i] = task_thread_info(idle); 211 212 /* See trampoline.S:leon_smp_cpu_startup for details... 213 * Initialize the contexts table 214 * Since the call to prom_startcpu() trashes the structure, 215 * we need to re-initialize it for each cpu 216 */ 217 smp_penguin_ctable.which_io = 0; 218 smp_penguin_ctable.phys_addr = (unsigned int)srmmu_ctx_table_phys; 219 smp_penguin_ctable.reg_size = 0; 220 221 /* whirrr, whirrr, whirrrrrrrrr... */ 222 printk(KERN_INFO "Starting CPU %d : (irqmp: 0x%x)\n", (unsigned int)i, 223 (unsigned int)&leon3_irqctrl_regs->mpstatus); 224 local_ops->cache_all(); 225 226 /* Make sure all IRQs are of from the start for this new CPU */ 227 LEON_BYPASS_STORE_PA(&leon3_irqctrl_regs->mask[i], 0); 228 229 /* Wake one CPU */ 230 LEON_BYPASS_STORE_PA(&(leon3_irqctrl_regs->mpstatus), 1 << i); 231 232 /* wheee... it's going... */ 233 for (timeout = 0; timeout < 10000; timeout++) { 234 if (cpu_callin_map[i]) 235 break; 236 udelay(200); 237 } 238 printk(KERN_INFO "Started CPU %d\n", (unsigned int)i); 239 240 if (!(cpu_callin_map[i])) { 241 printk(KERN_ERR "Processor %d is stuck.\n", i); 242 return -ENODEV; 243 } else { 244 leon_enable_irq_cpu(LEON3_IRQ_CROSS_CALL, i); 245 leon_enable_irq_cpu(LEON3_IRQ_TICKER, i); 246 leon_enable_irq_cpu(leon_ipi_irq, i); 247 } 248 249 local_ops->cache_all(); 250 return 0; 251 } 252 253 void __init leon_smp_done(void) 254 { 255 256 int i, first; 257 int *prev; 258 259 /* setup cpu list for irq rotation */ 260 first = 0; 261 prev = &first; 262 for (i = 0; i < NR_CPUS; i++) { 263 if (cpu_online(i)) { 264 *prev = i; 265 prev = &cpu_data(i).next; 266 } 267 } 268 *prev = first; 269 local_ops->cache_all(); 270 271 /* Free unneeded trap tables */ 272 if (!cpu_present(1)) { 273 ClearPageReserved(virt_to_page(&trapbase_cpu1)); 274 init_page_count(virt_to_page(&trapbase_cpu1)); 275 free_page((unsigned long)&trapbase_cpu1); 276 totalram_pages++; 277 num_physpages++; 278 } 279 if (!cpu_present(2)) { 280 ClearPageReserved(virt_to_page(&trapbase_cpu2)); 281 init_page_count(virt_to_page(&trapbase_cpu2)); 282 free_page((unsigned long)&trapbase_cpu2); 283 totalram_pages++; 284 num_physpages++; 285 } 286 if (!cpu_present(3)) { 287 ClearPageReserved(virt_to_page(&trapbase_cpu3)); 288 init_page_count(virt_to_page(&trapbase_cpu3)); 289 free_page((unsigned long)&trapbase_cpu3); 290 totalram_pages++; 291 num_physpages++; 292 } 293 /* Ok, they are spinning and ready to go. */ 294 smp_processors_ready = 1; 295 296 } 297 298 void leon_irq_rotate(int cpu) 299 { 300 } 301 302 struct leon_ipi_work { 303 int single; 304 int msk; 305 int resched; 306 }; 307 308 static DEFINE_PER_CPU_SHARED_ALIGNED(struct leon_ipi_work, leon_ipi_work); 309 310 /* Initialize IPIs on the LEON, in order to save IRQ resources only one IRQ 311 * is used for all three types of IPIs. 312 */ 313 static void __init leon_ipi_init(void) 314 { 315 int cpu, len; 316 struct leon_ipi_work *work; 317 struct property *pp; 318 struct device_node *rootnp; 319 struct tt_entry *trap_table; 320 unsigned long flags; 321 322 /* Find IPI IRQ or stick with default value */ 323 rootnp = of_find_node_by_path("/ambapp0"); 324 if (rootnp) { 325 pp = of_find_property(rootnp, "ipi_num", &len); 326 if (pp && (*(int *)pp->value)) 327 leon_ipi_irq = *(int *)pp->value; 328 } 329 printk(KERN_INFO "leon: SMP IPIs at IRQ %d\n", leon_ipi_irq); 330 331 /* Adjust so that we jump directly to smpleon_ipi */ 332 local_irq_save(flags); 333 trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_ipi_irq - 1)]; 334 trap_table->inst_three += smpleon_ipi - real_irq_entry; 335 local_ops->cache_all(); 336 local_irq_restore(flags); 337 338 for_each_possible_cpu(cpu) { 339 work = &per_cpu(leon_ipi_work, cpu); 340 work->single = work->msk = work->resched = 0; 341 } 342 } 343 344 static void leon_send_ipi(int cpu, int level) 345 { 346 unsigned long mask; 347 mask = leon_get_irqmask(level); 348 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask); 349 } 350 351 static void leon_ipi_single(int cpu) 352 { 353 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu); 354 355 /* Mark work */ 356 work->single = 1; 357 358 /* Generate IRQ on the CPU */ 359 leon_send_ipi(cpu, leon_ipi_irq); 360 } 361 362 static void leon_ipi_mask_one(int cpu) 363 { 364 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu); 365 366 /* Mark work */ 367 work->msk = 1; 368 369 /* Generate IRQ on the CPU */ 370 leon_send_ipi(cpu, leon_ipi_irq); 371 } 372 373 static void leon_ipi_resched(int cpu) 374 { 375 struct leon_ipi_work *work = &per_cpu(leon_ipi_work, cpu); 376 377 /* Mark work */ 378 work->resched = 1; 379 380 /* Generate IRQ on the CPU (any IRQ will cause resched) */ 381 leon_send_ipi(cpu, leon_ipi_irq); 382 } 383 384 void leonsmp_ipi_interrupt(void) 385 { 386 struct leon_ipi_work *work = &__get_cpu_var(leon_ipi_work); 387 388 if (work->single) { 389 work->single = 0; 390 smp_call_function_single_interrupt(); 391 } 392 if (work->msk) { 393 work->msk = 0; 394 smp_call_function_interrupt(); 395 } 396 if (work->resched) { 397 work->resched = 0; 398 smp_resched_interrupt(); 399 } 400 } 401 402 static struct smp_funcall { 403 smpfunc_t func; 404 unsigned long arg1; 405 unsigned long arg2; 406 unsigned long arg3; 407 unsigned long arg4; 408 unsigned long arg5; 409 unsigned long processors_in[NR_CPUS]; /* Set when ipi entered. */ 410 unsigned long processors_out[NR_CPUS]; /* Set when ipi exited. */ 411 } ccall_info; 412 413 static DEFINE_SPINLOCK(cross_call_lock); 414 415 /* Cross calls must be serialized, at least currently. */ 416 static void leon_cross_call(smpfunc_t func, cpumask_t mask, unsigned long arg1, 417 unsigned long arg2, unsigned long arg3, 418 unsigned long arg4) 419 { 420 if (smp_processors_ready) { 421 register int high = NR_CPUS - 1; 422 unsigned long flags; 423 424 spin_lock_irqsave(&cross_call_lock, flags); 425 426 { 427 /* If you make changes here, make sure gcc generates proper code... */ 428 register smpfunc_t f asm("i0") = func; 429 register unsigned long a1 asm("i1") = arg1; 430 register unsigned long a2 asm("i2") = arg2; 431 register unsigned long a3 asm("i3") = arg3; 432 register unsigned long a4 asm("i4") = arg4; 433 register unsigned long a5 asm("i5") = 0; 434 435 __asm__ __volatile__("std %0, [%6]\n\t" 436 "std %2, [%6 + 8]\n\t" 437 "std %4, [%6 + 16]\n\t" : : 438 "r"(f), "r"(a1), "r"(a2), "r"(a3), 439 "r"(a4), "r"(a5), 440 "r"(&ccall_info.func)); 441 } 442 443 /* Init receive/complete mapping, plus fire the IPI's off. */ 444 { 445 register int i; 446 447 cpumask_clear_cpu(smp_processor_id(), &mask); 448 cpumask_and(&mask, cpu_online_mask, &mask); 449 for (i = 0; i <= high; i++) { 450 if (cpumask_test_cpu(i, &mask)) { 451 ccall_info.processors_in[i] = 0; 452 ccall_info.processors_out[i] = 0; 453 leon_send_ipi(i, LEON3_IRQ_CROSS_CALL); 454 455 } 456 } 457 } 458 459 { 460 register int i; 461 462 i = 0; 463 do { 464 if (!cpumask_test_cpu(i, &mask)) 465 continue; 466 467 while (!ccall_info.processors_in[i]) 468 barrier(); 469 } while (++i <= high); 470 471 i = 0; 472 do { 473 if (!cpumask_test_cpu(i, &mask)) 474 continue; 475 476 while (!ccall_info.processors_out[i]) 477 barrier(); 478 } while (++i <= high); 479 } 480 481 spin_unlock_irqrestore(&cross_call_lock, flags); 482 } 483 } 484 485 /* Running cross calls. */ 486 void leon_cross_call_irq(void) 487 { 488 int i = smp_processor_id(); 489 490 ccall_info.processors_in[i] = 1; 491 ccall_info.func(ccall_info.arg1, ccall_info.arg2, ccall_info.arg3, 492 ccall_info.arg4, ccall_info.arg5); 493 ccall_info.processors_out[i] = 1; 494 } 495 496 static const struct sparc32_ipi_ops leon_ipi_ops = { 497 .cross_call = leon_cross_call, 498 .resched = leon_ipi_resched, 499 .single = leon_ipi_single, 500 .mask_one = leon_ipi_mask_one, 501 }; 502 503 void __init leon_init_smp(void) 504 { 505 /* Patch ipi15 trap table */ 506 t_nmi[1] = t_nmi[1] + (linux_trap_ipi15_leon - linux_trap_ipi15_sun4m); 507 508 sparc32_ipi_ops = &leon_ipi_ops; 509 } 510 511 #endif /* CONFIG_SPARC_LEON */ 512