smp.c 9.4 KB

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