smp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. static inline void
  192. smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
  193. void
  194. smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
  195. void
  196. smp_send_all_nop(void)
  197. {
  198. send_IPI_allbutself(IPI_NOP);
  199. }
  200. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  201. {
  202. send_IPI_mask(mask, IPI_CALL_FUNC);
  203. }
  204. void arch_send_call_function_single_ipi(int cpu)
  205. {
  206. send_IPI_single(cpu, IPI_CALL_FUNC);
  207. }
  208. /*
  209. * Called by secondaries to update state and initialize CPU registers.
  210. */
  211. static void __init
  212. smp_cpu_init(int cpunum)
  213. {
  214. extern int init_per_cpu(int); /* arch/parisc/kernel/processor.c */
  215. extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
  216. extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
  217. /* Set modes and Enable floating point coprocessor */
  218. (void) init_per_cpu(cpunum);
  219. disable_sr_hashing();
  220. mb();
  221. /* Well, support 2.4 linux scheme as well. */
  222. if (cpu_online(cpunum)) {
  223. extern void machine_halt(void); /* arch/parisc.../process.c */
  224. printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
  225. machine_halt();
  226. }
  227. notify_cpu_starting(cpunum);
  228. set_cpu_online(cpunum, true);
  229. /* Initialise the idle task for this CPU */
  230. atomic_inc(&init_mm.mm_count);
  231. current->active_mm = &init_mm;
  232. BUG_ON(current->mm);
  233. enter_lazy_tlb(&init_mm, current);
  234. init_IRQ(); /* make sure no IRQs are enabled or pending */
  235. start_cpu_itimer();
  236. }
  237. /*
  238. * Slaves start using C here. Indirectly called from smp_slave_stext.
  239. * Do what start_kernel() and main() do for boot strap processor (aka monarch)
  240. */
  241. void __init smp_callin(void)
  242. {
  243. int slave_id = cpu_now_booting;
  244. smp_cpu_init(slave_id);
  245. preempt_disable();
  246. flush_cache_all_local(); /* start with known state */
  247. flush_tlb_all_local(NULL);
  248. local_irq_enable(); /* Interrupts have been off until now */
  249. cpu_startup_entry(CPUHP_ONLINE);
  250. /* NOTREACHED */
  251. panic("smp_callin() AAAAaaaaahhhh....\n");
  252. }
  253. /*
  254. * Bring one cpu online.
  255. */
  256. int smp_boot_one_cpu(int cpuid, struct task_struct *idle)
  257. {
  258. const struct cpuinfo_parisc *p = &per_cpu(cpu_data, cpuid);
  259. long timeout;
  260. task_thread_info(idle)->cpu = cpuid;
  261. /* Let _start know what logical CPU we're booting
  262. ** (offset into init_tasks[],cpu_data[])
  263. */
  264. cpu_now_booting = cpuid;
  265. /*
  266. ** boot strap code needs to know the task address since
  267. ** it also contains the process stack.
  268. */
  269. smp_init_current_idle_task = idle ;
  270. mb();
  271. printk(KERN_INFO "Releasing cpu %d now, hpa=%lx\n", cpuid, p->hpa);
  272. /*
  273. ** This gets PDC to release the CPU from a very tight loop.
  274. **
  275. ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
  276. ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
  277. ** is executed after receiving the rendezvous signal (an interrupt to
  278. ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
  279. ** contents of memory are valid."
  280. */
  281. gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, p->hpa);
  282. mb();
  283. /*
  284. * OK, wait a bit for that CPU to finish staggering about.
  285. * Slave will set a bit when it reaches smp_cpu_init().
  286. * Once the "monarch CPU" sees the bit change, it can move on.
  287. */
  288. for (timeout = 0; timeout < 10000; timeout++) {
  289. if(cpu_online(cpuid)) {
  290. /* Which implies Slave has started up */
  291. cpu_now_booting = 0;
  292. smp_init_current_idle_task = NULL;
  293. goto alive ;
  294. }
  295. udelay(100);
  296. barrier();
  297. }
  298. printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
  299. return -1;
  300. alive:
  301. /* Remember the Slave data */
  302. smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
  303. cpuid, timeout * 100);
  304. return 0;
  305. }
  306. void __init smp_prepare_boot_cpu(void)
  307. {
  308. int bootstrap_processor = per_cpu(cpu_data, 0).cpuid;
  309. /* Setup BSP mappings */
  310. printk(KERN_INFO "SMP: bootstrap CPU ID is %d\n", bootstrap_processor);
  311. set_cpu_online(bootstrap_processor, true);
  312. set_cpu_present(bootstrap_processor, true);
  313. }
  314. /*
  315. ** inventory.c:do_inventory() hasn't yet been run and thus we
  316. ** don't 'discover' the additional CPUs until later.
  317. */
  318. void __init smp_prepare_cpus(unsigned int max_cpus)
  319. {
  320. int cpu;
  321. for_each_possible_cpu(cpu)
  322. spin_lock_init(&per_cpu(ipi_lock, cpu));
  323. init_cpu_present(cpumask_of(0));
  324. parisc_max_cpus = max_cpus;
  325. if (!max_cpus)
  326. printk(KERN_INFO "SMP mode deactivated.\n");
  327. }
  328. void smp_cpus_done(unsigned int cpu_max)
  329. {
  330. return;
  331. }
  332. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  333. {
  334. if (cpu != 0 && cpu < parisc_max_cpus)
  335. smp_boot_one_cpu(cpu, tidle);
  336. return cpu_online(cpu) ? 0 : -ENOSYS;
  337. }
  338. #ifdef CONFIG_PROC_FS
  339. int __init
  340. setup_profiling_timer(unsigned int multiplier)
  341. {
  342. return -EINVAL;
  343. }
  344. #endif