smp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_CALL_FUNC_SINGLE,
  67. IPI_CPU_START,
  68. IPI_CPU_STOP,
  69. IPI_CPU_TEST
  70. };
  71. /********** SMP inter processor interrupt and communication routines */
  72. #undef PER_CPU_IRQ_REGION
  73. #ifdef PER_CPU_IRQ_REGION
  74. /* XXX REVISIT Ignore for now.
  75. ** *May* need this "hook" to register IPI handler
  76. ** once we have perCPU ExtIntr switch tables.
  77. */
  78. static void
  79. ipi_init(int cpuid)
  80. {
  81. #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
  82. if(cpu_online(cpuid) )
  83. {
  84. switch_to_idle_task(current);
  85. }
  86. return;
  87. }
  88. #endif
  89. /*
  90. ** Yoink this CPU from the runnable list...
  91. **
  92. */
  93. static void
  94. halt_processor(void)
  95. {
  96. /* REVISIT : redirect I/O Interrupts to another CPU? */
  97. /* REVISIT : does PM *know* this CPU isn't available? */
  98. set_cpu_online(smp_processor_id(), false);
  99. local_irq_disable();
  100. for (;;)
  101. ;
  102. }
  103. irqreturn_t __irq_entry
  104. ipi_interrupt(int irq, void *dev_id)
  105. {
  106. int this_cpu = smp_processor_id();
  107. struct cpuinfo_parisc *p = &per_cpu(cpu_data, this_cpu);
  108. unsigned long ops;
  109. unsigned long flags;
  110. /* Count this now; we may make a call that never returns. */
  111. inc_irq_stat(irq_call_count);
  112. mb(); /* Order interrupt and bit testing. */
  113. for (;;) {
  114. spinlock_t *lock = &per_cpu(ipi_lock, this_cpu);
  115. spin_lock_irqsave(lock, flags);
  116. ops = p->pending_ipi;
  117. p->pending_ipi = 0;
  118. spin_unlock_irqrestore(lock, flags);
  119. mb(); /* Order bit clearing and data access. */
  120. if (!ops)
  121. break;
  122. while (ops) {
  123. unsigned long which = ffz(~ops);
  124. ops &= ~(1 << which);
  125. switch (which) {
  126. case IPI_NOP:
  127. smp_debug(100, KERN_DEBUG "CPU%d IPI_NOP\n", this_cpu);
  128. break;
  129. case IPI_RESCHEDULE:
  130. smp_debug(100, KERN_DEBUG "CPU%d IPI_RESCHEDULE\n", this_cpu);
  131. inc_irq_stat(irq_resched_count);
  132. scheduler_ipi();
  133. break;
  134. case IPI_CALL_FUNC:
  135. smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC\n", this_cpu);
  136. generic_smp_call_function_interrupt();
  137. break;
  138. case IPI_CALL_FUNC_SINGLE:
  139. smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC_SINGLE\n", this_cpu);
  140. generic_smp_call_function_single_interrupt();
  141. break;
  142. case IPI_CPU_START:
  143. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_START\n", this_cpu);
  144. break;
  145. case IPI_CPU_STOP:
  146. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_STOP\n", this_cpu);
  147. halt_processor();
  148. break;
  149. case IPI_CPU_TEST:
  150. smp_debug(100, KERN_DEBUG "CPU%d is alive!\n", this_cpu);
  151. break;
  152. default:
  153. printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
  154. this_cpu, which);
  155. return IRQ_NONE;
  156. } /* Switch */
  157. /* let in any pending interrupts */
  158. local_irq_enable();
  159. local_irq_disable();
  160. } /* while (ops) */
  161. }
  162. return IRQ_HANDLED;
  163. }
  164. static inline void
  165. ipi_send(int cpu, enum ipi_message_type op)
  166. {
  167. struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpu);
  168. spinlock_t *lock = &per_cpu(ipi_lock, cpu);
  169. unsigned long flags;
  170. spin_lock_irqsave(lock, flags);
  171. p->pending_ipi |= 1 << op;
  172. gsc_writel(IPI_IRQ - CPU_IRQ_BASE, p->hpa);
  173. spin_unlock_irqrestore(lock, flags);
  174. }
  175. static void
  176. send_IPI_mask(const struct cpumask *mask, enum ipi_message_type op)
  177. {
  178. int cpu;
  179. for_each_cpu(cpu, mask)
  180. ipi_send(cpu, op);
  181. }
  182. static inline void
  183. send_IPI_single(int dest_cpu, enum ipi_message_type op)
  184. {
  185. BUG_ON(dest_cpu == NO_PROC_ID);
  186. ipi_send(dest_cpu, op);
  187. }
  188. static inline void
  189. send_IPI_allbutself(enum ipi_message_type op)
  190. {
  191. int i;
  192. for_each_online_cpu(i) {
  193. if (i != smp_processor_id())
  194. send_IPI_single(i, op);
  195. }
  196. }
  197. inline void
  198. smp_send_stop(void) { send_IPI_allbutself(IPI_CPU_STOP); }
  199. static inline void
  200. smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
  201. void
  202. smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
  203. void
  204. smp_send_all_nop(void)
  205. {
  206. send_IPI_allbutself(IPI_NOP);
  207. }
  208. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  209. {
  210. send_IPI_mask(mask, IPI_CALL_FUNC);
  211. }
  212. void arch_send_call_function_single_ipi(int cpu)
  213. {
  214. send_IPI_single(cpu, IPI_CALL_FUNC_SINGLE);
  215. }
  216. /*
  217. * Called by secondaries to update state and initialize CPU registers.
  218. */
  219. static void __init
  220. smp_cpu_init(int cpunum)
  221. {
  222. extern int init_per_cpu(int); /* arch/parisc/kernel/processor.c */
  223. extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
  224. extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
  225. /* Set modes and Enable floating point coprocessor */
  226. (void) init_per_cpu(cpunum);
  227. disable_sr_hashing();
  228. mb();
  229. /* Well, support 2.4 linux scheme as well. */
  230. if (cpu_online(cpunum)) {
  231. extern void machine_halt(void); /* arch/parisc.../process.c */
  232. printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
  233. machine_halt();
  234. }
  235. notify_cpu_starting(cpunum);
  236. set_cpu_online(cpunum, true);
  237. /* Initialise the idle task for this CPU */
  238. atomic_inc(&init_mm.mm_count);
  239. current->active_mm = &init_mm;
  240. BUG_ON(current->mm);
  241. enter_lazy_tlb(&init_mm, current);
  242. init_IRQ(); /* make sure no IRQs are enabled or pending */
  243. start_cpu_itimer();
  244. }
  245. /*
  246. * Slaves start using C here. Indirectly called from smp_slave_stext.
  247. * Do what start_kernel() and main() do for boot strap processor (aka monarch)
  248. */
  249. void __init smp_callin(void)
  250. {
  251. int slave_id = cpu_now_booting;
  252. smp_cpu_init(slave_id);
  253. preempt_disable();
  254. flush_cache_all_local(); /* start with known state */
  255. flush_tlb_all_local(NULL);
  256. local_irq_enable(); /* Interrupts have been off until now */
  257. cpu_startup_entry(CPUHP_ONLINE);
  258. /* NOTREACHED */
  259. panic("smp_callin() AAAAaaaaahhhh....\n");
  260. }
  261. /*
  262. * Bring one cpu online.
  263. */
  264. int smp_boot_one_cpu(int cpuid, struct task_struct *idle)
  265. {
  266. const struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpuid);
  267. long timeout;
  268. task_thread_info(idle)->cpu = cpuid;
  269. /* Let _start know what logical CPU we're booting
  270. ** (offset into init_tasks[],cpu_data[])
  271. */
  272. cpu_now_booting = cpuid;
  273. /*
  274. ** boot strap code needs to know the task address since
  275. ** it also contains the process stack.
  276. */
  277. smp_init_current_idle_task = idle ;
  278. mb();
  279. printk(KERN_INFO "Releasing cpu %d now, hpa=%lx\n", cpuid, p->hpa);
  280. /*
  281. ** This gets PDC to release the CPU from a very tight loop.
  282. **
  283. ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
  284. ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
  285. ** is executed after receiving the rendezvous signal (an interrupt to
  286. ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
  287. ** contents of memory are valid."
  288. */
  289. gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, p->hpa);
  290. mb();
  291. /*
  292. * OK, wait a bit for that CPU to finish staggering about.
  293. * Slave will set a bit when it reaches smp_cpu_init().
  294. * Once the "monarch CPU" sees the bit change, it can move on.
  295. */
  296. for (timeout = 0; timeout < 10000; timeout++) {
  297. if(cpu_online(cpuid)) {
  298. /* Which implies Slave has started up */
  299. cpu_now_booting = 0;
  300. smp_init_current_idle_task = NULL;
  301. goto alive ;
  302. }
  303. udelay(100);
  304. barrier();
  305. }
  306. printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
  307. return -1;
  308. alive:
  309. /* Remember the Slave data */
  310. smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
  311. cpuid, timeout * 100);
  312. return 0;
  313. }
  314. void __init smp_prepare_boot_cpu(void)
  315. {
  316. int bootstrap_processor = per_cpu(cpu_data, 0).cpuid;
  317. /* Setup BSP mappings */
  318. printk(KERN_INFO "SMP: bootstrap CPU ID is %d\n", bootstrap_processor);
  319. set_cpu_online(bootstrap_processor, true);
  320. set_cpu_present(bootstrap_processor, true);
  321. }
  322. /*
  323. ** inventory.c:do_inventory() hasn't yet been run and thus we
  324. ** don't 'discover' the additional CPUs until later.
  325. */
  326. void __init smp_prepare_cpus(unsigned int max_cpus)
  327. {
  328. int cpu;
  329. for_each_possible_cpu(cpu)
  330. spin_lock_init(&per_cpu(ipi_lock, cpu));
  331. init_cpu_present(cpumask_of(0));
  332. parisc_max_cpus = max_cpus;
  333. if (!max_cpus)
  334. printk(KERN_INFO "SMP mode deactivated.\n");
  335. }
  336. void smp_cpus_done(unsigned int cpu_max)
  337. {
  338. return;
  339. }
  340. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  341. {
  342. if (cpu != 0 && cpu < parisc_max_cpus)
  343. smp_boot_one_cpu(cpu, tidle);
  344. return cpu_online(cpu) ? 0 : -ENOSYS;
  345. }
  346. #ifdef CONFIG_PROC_FS
  347. int __init
  348. setup_profiling_timer(unsigned int multiplier)
  349. {
  350. return -EINVAL;
  351. }
  352. #endif