smp.c 11 KB

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