smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. ** SMP Support
  3. **
  4. ** Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  5. ** Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
  6. ** Copyright (C) 2001,2004 Grant Grundler <grundler@parisc-linux.org>
  7. **
  8. ** Lots of stuff stolen from arch/alpha/kernel/smp.c
  9. ** ...and then parisc stole from arch/ia64/kernel/smp.c. Thanks David! :^)
  10. **
  11. ** Thanks to John Curry and Ullas Ponnadi. I learned a lot from their work.
  12. ** -grant (1/12/2001)
  13. **
  14. ** This program is free software; you can redistribute it and/or modify
  15. ** it under the terms of the GNU General Public License as published by
  16. ** the Free Software Foundation; either version 2 of the License, or
  17. ** (at your option) any later version.
  18. */
  19. #include <linux/types.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/smp.h>
  27. #include <linux/kernel_stat.h>
  28. #include <linux/mm.h>
  29. #include <linux/err.h>
  30. #include <linux/delay.h>
  31. #include <linux/bitops.h>
  32. #include <linux/ftrace.h>
  33. #include <linux/atomic.h>
  34. #include <asm/current.h>
  35. #include <asm/delay.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h> /* for CPU_IRQ_REGION and friends */
  39. #include <asm/mmu_context.h>
  40. #include <asm/page.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/pgalloc.h>
  43. #include <asm/processor.h>
  44. #include <asm/ptrace.h>
  45. #include <asm/unistd.h>
  46. #include <asm/cacheflush.h>
  47. #undef DEBUG_SMP
  48. #ifdef DEBUG_SMP
  49. static int smp_debug_lvl = 0;
  50. #define smp_debug(lvl, printargs...) \
  51. if (lvl >= smp_debug_lvl) \
  52. printk(printargs);
  53. #else
  54. #define smp_debug(lvl, ...) do { } while(0)
  55. #endif /* DEBUG_SMP */
  56. volatile struct task_struct *smp_init_current_idle_task;
  57. /* track which CPU is booting */
  58. static volatile int cpu_now_booting __cpuinitdata;
  59. static int parisc_max_cpus __cpuinitdata = 1;
  60. static DEFINE_PER_CPU(spinlock_t, ipi_lock);
  61. enum ipi_message_type {
  62. IPI_NOP=0,
  63. IPI_RESCHEDULE=1,
  64. IPI_CALL_FUNC,
  65. IPI_CALL_FUNC_SINGLE,
  66. IPI_CPU_START,
  67. IPI_CPU_STOP,
  68. IPI_CPU_TEST
  69. };
  70. /********** SMP inter processor interrupt and communication routines */
  71. #undef PER_CPU_IRQ_REGION
  72. #ifdef PER_CPU_IRQ_REGION
  73. /* XXX REVISIT Ignore for now.
  74. ** *May* need this "hook" to register IPI handler
  75. ** once we have perCPU ExtIntr switch tables.
  76. */
  77. static void
  78. ipi_init(int cpuid)
  79. {
  80. #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
  81. if(cpu_online(cpuid) )
  82. {
  83. switch_to_idle_task(current);
  84. }
  85. return;
  86. }
  87. #endif
  88. /*
  89. ** Yoink this CPU from the runnable list...
  90. **
  91. */
  92. static void
  93. halt_processor(void)
  94. {
  95. /* REVISIT : redirect I/O Interrupts to another CPU? */
  96. /* REVISIT : does PM *know* this CPU isn't available? */
  97. set_cpu_online(smp_processor_id(), false);
  98. local_irq_disable();
  99. for (;;)
  100. ;
  101. }
  102. irqreturn_t __irq_entry
  103. ipi_interrupt(int irq, void *dev_id)
  104. {
  105. int this_cpu = smp_processor_id();
  106. struct cpuinfo_parisc *p = &per_cpu(cpu_data, this_cpu);
  107. unsigned long ops;
  108. unsigned long flags;
  109. /* Count this now; we may make a call that never returns. */
  110. p->ipi_count++;
  111. mb(); /* Order interrupt and bit testing. */
  112. for (;;) {
  113. spinlock_t *lock = &per_cpu(ipi_lock, this_cpu);
  114. spin_lock_irqsave(lock, flags);
  115. ops = p->pending_ipi;
  116. p->pending_ipi = 0;
  117. spin_unlock_irqrestore(lock, flags);
  118. mb(); /* Order bit clearing and data access. */
  119. if (!ops)
  120. break;
  121. while (ops) {
  122. unsigned long which = ffz(~ops);
  123. ops &= ~(1 << which);
  124. switch (which) {
  125. case IPI_NOP:
  126. smp_debug(100, KERN_DEBUG "CPU%d IPI_NOP\n", this_cpu);
  127. break;
  128. case IPI_RESCHEDULE:
  129. smp_debug(100, KERN_DEBUG "CPU%d IPI_RESCHEDULE\n", this_cpu);
  130. scheduler_ipi();
  131. break;
  132. case IPI_CALL_FUNC:
  133. smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC\n", this_cpu);
  134. generic_smp_call_function_interrupt();
  135. break;
  136. case IPI_CALL_FUNC_SINGLE:
  137. smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC_SINGLE\n", this_cpu);
  138. generic_smp_call_function_single_interrupt();
  139. break;
  140. case IPI_CPU_START:
  141. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_START\n", this_cpu);
  142. break;
  143. case IPI_CPU_STOP:
  144. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_STOP\n", this_cpu);
  145. halt_processor();
  146. break;
  147. case IPI_CPU_TEST:
  148. smp_debug(100, KERN_DEBUG "CPU%d is alive!\n", this_cpu);
  149. break;
  150. default:
  151. printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
  152. this_cpu, which);
  153. return IRQ_NONE;
  154. } /* Switch */
  155. /* let in any pending interrupts */
  156. local_irq_enable();
  157. local_irq_disable();
  158. } /* while (ops) */
  159. }
  160. return IRQ_HANDLED;
  161. }
  162. static inline void
  163. ipi_send(int cpu, enum ipi_message_type op)
  164. {
  165. struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpu);
  166. spinlock_t *lock = &per_cpu(ipi_lock, cpu);
  167. unsigned long flags;
  168. spin_lock_irqsave(lock, flags);
  169. p->pending_ipi |= 1 << op;
  170. gsc_writel(IPI_IRQ - CPU_IRQ_BASE, p->hpa);
  171. spin_unlock_irqrestore(lock, flags);
  172. }
  173. static void
  174. send_IPI_mask(const struct cpumask *mask, enum ipi_message_type op)
  175. {
  176. int cpu;
  177. for_each_cpu(cpu, mask)
  178. ipi_send(cpu, op);
  179. }
  180. static inline void
  181. send_IPI_single(int dest_cpu, enum ipi_message_type op)
  182. {
  183. BUG_ON(dest_cpu == NO_PROC_ID);
  184. ipi_send(dest_cpu, op);
  185. }
  186. static inline void
  187. send_IPI_allbutself(enum ipi_message_type op)
  188. {
  189. int i;
  190. for_each_online_cpu(i) {
  191. if (i != smp_processor_id())
  192. send_IPI_single(i, op);
  193. }
  194. }
  195. inline void
  196. smp_send_stop(void) { send_IPI_allbutself(IPI_CPU_STOP); }
  197. static inline void
  198. smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
  199. void
  200. smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
  201. void
  202. smp_send_all_nop(void)
  203. {
  204. send_IPI_allbutself(IPI_NOP);
  205. }
  206. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  207. {
  208. send_IPI_mask(mask, IPI_CALL_FUNC);
  209. }
  210. void arch_send_call_function_single_ipi(int cpu)
  211. {
  212. send_IPI_single(cpu, IPI_CALL_FUNC_SINGLE);
  213. }
  214. /*
  215. * Flush all other CPU's tlb and then mine. Do this with on_each_cpu()
  216. * as we want to ensure all TLB's flushed before proceeding.
  217. */
  218. void
  219. smp_flush_tlb_all(void)
  220. {
  221. on_each_cpu(flush_tlb_all_local, NULL, 1);
  222. }
  223. /*
  224. * Called by secondaries to update state and initialize CPU registers.
  225. */
  226. static void __init
  227. smp_cpu_init(int cpunum)
  228. {
  229. extern int init_per_cpu(int); /* arch/parisc/kernel/processor.c */
  230. extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
  231. extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
  232. /* Set modes and Enable floating point coprocessor */
  233. (void) init_per_cpu(cpunum);
  234. disable_sr_hashing();
  235. mb();
  236. /* Well, support 2.4 linux scheme as well. */
  237. if (cpu_online(cpunum)) {
  238. extern void machine_halt(void); /* arch/parisc.../process.c */
  239. printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
  240. machine_halt();
  241. }
  242. set_cpu_online(cpunum, true);
  243. /* Initialise the idle task for this CPU */
  244. atomic_inc(&init_mm.mm_count);
  245. current->active_mm = &init_mm;
  246. BUG_ON(current->mm);
  247. enter_lazy_tlb(&init_mm, current);
  248. init_IRQ(); /* make sure no IRQs are enabled or pending */
  249. start_cpu_itimer();
  250. }
  251. /*
  252. * Slaves start using C here. Indirectly called from smp_slave_stext.
  253. * Do what start_kernel() and main() do for boot strap processor (aka monarch)
  254. */
  255. void __init smp_callin(void)
  256. {
  257. int slave_id = cpu_now_booting;
  258. smp_cpu_init(slave_id);
  259. preempt_disable();
  260. flush_cache_all_local(); /* start with known state */
  261. flush_tlb_all_local(NULL);
  262. local_irq_enable(); /* Interrupts have been off until now */
  263. cpu_idle(); /* Wait for timer to schedule some work */
  264. /* NOTREACHED */
  265. panic("smp_callin() AAAAaaaaahhhh....\n");
  266. }
  267. /*
  268. * Bring one cpu online.
  269. */
  270. int __cpuinit smp_boot_one_cpu(int cpuid)
  271. {
  272. const struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpuid);
  273. struct task_struct *idle;
  274. long timeout;
  275. /*
  276. * Create an idle task for this CPU. Note the address wed* give
  277. * to kernel_thread is irrelevant -- it's going to start
  278. * where OS_BOOT_RENDEVZ vector in SAL says to start. But
  279. * this gets all the other task-y sort of data structures set
  280. * up like we wish. We need to pull the just created idle task
  281. * off the run queue and stuff it into the init_tasks[] array.
  282. * Sheesh . . .
  283. */
  284. idle = fork_idle(cpuid);
  285. if (IS_ERR(idle))
  286. panic("SMP: fork failed for CPU:%d", cpuid);
  287. task_thread_info(idle)->cpu = cpuid;
  288. /* Let _start know what logical CPU we're booting
  289. ** (offset into init_tasks[],cpu_data[])
  290. */
  291. cpu_now_booting = cpuid;
  292. /*
  293. ** boot strap code needs to know the task address since
  294. ** it also contains the process stack.
  295. */
  296. smp_init_current_idle_task = idle ;
  297. mb();
  298. printk(KERN_INFO "Releasing cpu %d now, hpa=%lx\n", cpuid, p->hpa);
  299. /*
  300. ** This gets PDC to release the CPU from a very tight loop.
  301. **
  302. ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
  303. ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
  304. ** is executed after receiving the rendezvous signal (an interrupt to
  305. ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
  306. ** contents of memory are valid."
  307. */
  308. gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, p->hpa);
  309. mb();
  310. /*
  311. * OK, wait a bit for that CPU to finish staggering about.
  312. * Slave will set a bit when it reaches smp_cpu_init().
  313. * Once the "monarch CPU" sees the bit change, it can move on.
  314. */
  315. for (timeout = 0; timeout < 10000; timeout++) {
  316. if(cpu_online(cpuid)) {
  317. /* Which implies Slave has started up */
  318. cpu_now_booting = 0;
  319. smp_init_current_idle_task = NULL;
  320. goto alive ;
  321. }
  322. udelay(100);
  323. barrier();
  324. }
  325. put_task_struct(idle);
  326. idle = NULL;
  327. printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
  328. return -1;
  329. alive:
  330. /* Remember the Slave data */
  331. smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
  332. cpuid, timeout * 100);
  333. return 0;
  334. }
  335. void __init smp_prepare_boot_cpu(void)
  336. {
  337. int bootstrap_processor = per_cpu(cpu_data, 0).cpuid;
  338. /* Setup BSP mappings */
  339. printk(KERN_INFO "SMP: bootstrap CPU ID is %d\n", bootstrap_processor);
  340. set_cpu_online(bootstrap_processor, true);
  341. set_cpu_present(bootstrap_processor, true);
  342. }
  343. /*
  344. ** inventory.c:do_inventory() hasn't yet been run and thus we
  345. ** don't 'discover' the additional CPUs until later.
  346. */
  347. void __init smp_prepare_cpus(unsigned int max_cpus)
  348. {
  349. int cpu;
  350. for_each_possible_cpu(cpu)
  351. spin_lock_init(&per_cpu(ipi_lock, cpu));
  352. init_cpu_present(cpumask_of(0));
  353. parisc_max_cpus = max_cpus;
  354. if (!max_cpus)
  355. printk(KERN_INFO "SMP mode deactivated.\n");
  356. }
  357. void smp_cpus_done(unsigned int cpu_max)
  358. {
  359. return;
  360. }
  361. int __cpuinit __cpu_up(unsigned int cpu)
  362. {
  363. if (cpu != 0 && cpu < parisc_max_cpus)
  364. smp_boot_one_cpu(cpu);
  365. return cpu_online(cpu) ? 0 : -ENOSYS;
  366. }
  367. #ifdef CONFIG_PROC_FS
  368. int __init
  369. setup_profiling_timer(unsigned int multiplier)
  370. {
  371. return -EINVAL;
  372. }
  373. #endif