smp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/cpu.h>
  34. #include <linux/atomic.h>
  35. #include <asm/current.h>
  36. #include <asm/delay.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h> /* for CPU_IRQ_REGION and friends */
  40. #include <asm/mmu_context.h>
  41. #include <asm/page.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/processor.h>
  45. #include <asm/ptrace.h>
  46. #include <asm/unistd.h>
  47. #include <asm/cacheflush.h>
  48. #undef DEBUG_SMP
  49. #ifdef DEBUG_SMP
  50. static int smp_debug_lvl = 0;
  51. #define smp_debug(lvl, printargs...) \
  52. if (lvl >= smp_debug_lvl) \
  53. printk(printargs);
  54. #else
  55. #define smp_debug(lvl, ...) do { } while(0)
  56. #endif /* DEBUG_SMP */
  57. volatile struct task_struct *smp_init_current_idle_task;
  58. /* track which CPU is booting */
  59. static volatile int cpu_now_booting;
  60. static int parisc_max_cpus = 1;
  61. static DEFINE_PER_CPU(spinlock_t, ipi_lock);
  62. enum ipi_message_type {
  63. IPI_NOP=0,
  64. IPI_RESCHEDULE=1,
  65. IPI_CALL_FUNC,
  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. inc_irq_stat(irq_call_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. inc_irq_stat(irq_resched_count);
  131. scheduler_ipi();
  132. break;
  133. case IPI_CALL_FUNC:
  134. smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC\n", this_cpu);
  135. generic_smp_call_function_interrupt();
  136. break;
  137. case IPI_CPU_START:
  138. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_START\n", this_cpu);
  139. break;
  140. case IPI_CPU_STOP:
  141. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_STOP\n", this_cpu);
  142. halt_processor();
  143. break;
  144. case IPI_CPU_TEST:
  145. smp_debug(100, KERN_DEBUG "CPU%d is alive!\n", this_cpu);
  146. break;
  147. default:
  148. printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
  149. this_cpu, which);
  150. return IRQ_NONE;
  151. } /* Switch */
  152. /* let in any pending interrupts */
  153. local_irq_enable();
  154. local_irq_disable();
  155. } /* while (ops) */
  156. }
  157. return IRQ_HANDLED;
  158. }
  159. static inline void
  160. ipi_send(int cpu, enum ipi_message_type op)
  161. {
  162. struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpu);
  163. spinlock_t *lock = &per_cpu(ipi_lock, cpu);
  164. unsigned long flags;
  165. spin_lock_irqsave(lock, flags);
  166. p->pending_ipi |= 1 << op;
  167. gsc_writel(IPI_IRQ - CPU_IRQ_BASE, p->hpa);
  168. spin_unlock_irqrestore(lock, flags);
  169. }
  170. static void
  171. send_IPI_mask(const struct cpumask *mask, enum ipi_message_type op)
  172. {
  173. int cpu;
  174. for_each_cpu(cpu, mask)
  175. ipi_send(cpu, op);
  176. }
  177. static inline void
  178. send_IPI_single(int dest_cpu, enum ipi_message_type op)
  179. {
  180. BUG_ON(dest_cpu == NO_PROC_ID);
  181. ipi_send(dest_cpu, op);
  182. }
  183. static inline void
  184. send_IPI_allbutself(enum ipi_message_type op)
  185. {
  186. int i;
  187. for_each_online_cpu(i) {
  188. if (i != smp_processor_id())
  189. send_IPI_single(i, op);
  190. }
  191. }
  192. inline void
  193. smp_send_stop(void) { send_IPI_allbutself(IPI_CPU_STOP); }
  194. static inline void
  195. smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
  196. void
  197. smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
  198. void
  199. smp_send_all_nop(void)
  200. {
  201. send_IPI_allbutself(IPI_NOP);
  202. }
  203. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  204. {
  205. send_IPI_mask(mask, IPI_CALL_FUNC);
  206. }
  207. void arch_send_call_function_single_ipi(int cpu)
  208. {
  209. send_IPI_single(cpu, IPI_CALL_FUNC);
  210. }
  211. /*
  212. * Called by secondaries to update state and initialize CPU registers.
  213. */
  214. static void __init
  215. smp_cpu_init(int cpunum)
  216. {
  217. extern int init_per_cpu(int); /* arch/parisc/kernel/processor.c */
  218. extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
  219. extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
  220. /* Set modes and Enable floating point coprocessor */
  221. (void) init_per_cpu(cpunum);
  222. disable_sr_hashing();
  223. mb();
  224. /* Well, support 2.4 linux scheme as well. */
  225. if (cpu_online(cpunum)) {
  226. extern void machine_halt(void); /* arch/parisc.../process.c */
  227. printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
  228. machine_halt();
  229. }
  230. notify_cpu_starting(cpunum);
  231. set_cpu_online(cpunum, true);
  232. /* Initialise the idle task for this CPU */
  233. atomic_inc(&init_mm.mm_count);
  234. current->active_mm = &init_mm;
  235. BUG_ON(current->mm);
  236. enter_lazy_tlb(&init_mm, current);
  237. init_IRQ(); /* make sure no IRQs are enabled or pending */
  238. start_cpu_itimer();
  239. }
  240. /*
  241. * Slaves start using C here. Indirectly called from smp_slave_stext.
  242. * Do what start_kernel() and main() do for boot strap processor (aka monarch)
  243. */
  244. void __init smp_callin(void)
  245. {
  246. int slave_id = cpu_now_booting;
  247. smp_cpu_init(slave_id);
  248. preempt_disable();
  249. flush_cache_all_local(); /* start with known state */
  250. flush_tlb_all_local(NULL);
  251. local_irq_enable(); /* Interrupts have been off until now */
  252. cpu_startup_entry(CPUHP_ONLINE);
  253. /* NOTREACHED */
  254. panic("smp_callin() AAAAaaaaahhhh....\n");
  255. }
  256. /*
  257. * Bring one cpu online.
  258. */
  259. int smp_boot_one_cpu(int cpuid, struct task_struct *idle)
  260. {
  261. const struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpuid);
  262. long timeout;
  263. task_thread_info(idle)->cpu = cpuid;
  264. /* Let _start know what logical CPU we're booting
  265. ** (offset into init_tasks[],cpu_data[])
  266. */
  267. cpu_now_booting = cpuid;
  268. /*
  269. ** boot strap code needs to know the task address since
  270. ** it also contains the process stack.
  271. */
  272. smp_init_current_idle_task = idle ;
  273. mb();
  274. printk(KERN_INFO "Releasing cpu %d now, hpa=%lx\n", cpuid, p->hpa);
  275. /*
  276. ** This gets PDC to release the CPU from a very tight loop.
  277. **
  278. ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
  279. ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
  280. ** is executed after receiving the rendezvous signal (an interrupt to
  281. ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
  282. ** contents of memory are valid."
  283. */
  284. gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, p->hpa);
  285. mb();
  286. /*
  287. * OK, wait a bit for that CPU to finish staggering about.
  288. * Slave will set a bit when it reaches smp_cpu_init().
  289. * Once the "monarch CPU" sees the bit change, it can move on.
  290. */
  291. for (timeout = 0; timeout < 10000; timeout++) {
  292. if(cpu_online(cpuid)) {
  293. /* Which implies Slave has started up */
  294. cpu_now_booting = 0;
  295. smp_init_current_idle_task = NULL;
  296. goto alive ;
  297. }
  298. udelay(100);
  299. barrier();
  300. }
  301. printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
  302. return -1;
  303. alive:
  304. /* Remember the Slave data */
  305. smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
  306. cpuid, timeout * 100);
  307. return 0;
  308. }
  309. void __init smp_prepare_boot_cpu(void)
  310. {
  311. int bootstrap_processor = per_cpu(cpu_data, 0).cpuid;
  312. /* Setup BSP mappings */
  313. printk(KERN_INFO "SMP: bootstrap CPU ID is %d\n", bootstrap_processor);
  314. set_cpu_online(bootstrap_processor, true);
  315. set_cpu_present(bootstrap_processor, true);
  316. }
  317. /*
  318. ** inventory.c:do_inventory() hasn't yet been run and thus we
  319. ** don't 'discover' the additional CPUs until later.
  320. */
  321. void __init smp_prepare_cpus(unsigned int max_cpus)
  322. {
  323. int cpu;
  324. for_each_possible_cpu(cpu)
  325. spin_lock_init(&per_cpu(ipi_lock, cpu));
  326. init_cpu_present(cpumask_of(0));
  327. parisc_max_cpus = max_cpus;
  328. if (!max_cpus)
  329. printk(KERN_INFO "SMP mode deactivated.\n");
  330. }
  331. void smp_cpus_done(unsigned int cpu_max)
  332. {
  333. return;
  334. }
  335. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  336. {
  337. if (cpu != 0 && cpu < parisc_max_cpus)
  338. smp_boot_one_cpu(cpu, tidle);
  339. return cpu_online(cpu) ? 0 : -ENOSYS;
  340. }
  341. #ifdef CONFIG_PROC_FS
  342. int __init
  343. setup_profiling_timer(unsigned int multiplier)
  344. {
  345. return -EINVAL;
  346. }
  347. #endif