smp.c 11 KB

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