smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 alot 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/delay.h>
  31. #include <linux/bitops.h>
  32. #include <asm/system.h>
  33. #include <asm/atomic.h>
  34. #include <asm/current.h>
  35. #include <asm/delay.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h> /* for CPU_IRQ_REGION and friends */
  39. #include <asm/mmu_context.h>
  40. #include <asm/page.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/pgalloc.h>
  43. #include <asm/processor.h>
  44. #include <asm/ptrace.h>
  45. #include <asm/unistd.h>
  46. #include <asm/cacheflush.h>
  47. #undef DEBUG_SMP
  48. #ifdef DEBUG_SMP
  49. static int smp_debug_lvl = 0;
  50. #define smp_debug(lvl, printargs...) \
  51. if (lvl >= smp_debug_lvl) \
  52. printk(printargs);
  53. #else
  54. #define smp_debug(lvl, ...)
  55. #endif /* DEBUG_SMP */
  56. DEFINE_SPINLOCK(smp_lock);
  57. volatile struct task_struct *smp_init_current_idle_task;
  58. static volatile int cpu_now_booting __read_mostly = 0; /* track which CPU is booting */
  59. static int parisc_max_cpus __read_mostly = 1;
  60. /* online cpus are ones that we've managed to bring up completely
  61. * possible cpus are all valid cpu
  62. * present cpus are all detected cpu
  63. *
  64. * On startup we bring up the "possible" cpus. Since we discover
  65. * CPUs later, we add them as hotplug, so the possible cpu mask is
  66. * empty in the beginning.
  67. */
  68. cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE; /* Bitmap of online CPUs */
  69. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; /* Bitmap of Present CPUs */
  70. EXPORT_SYMBOL(cpu_online_map);
  71. EXPORT_SYMBOL(cpu_possible_map);
  72. DEFINE_PER_CPU(spinlock_t, ipi_lock) = SPIN_LOCK_UNLOCKED;
  73. struct smp_call_struct {
  74. void (*func) (void *info);
  75. void *info;
  76. long wait;
  77. atomic_t unstarted_count;
  78. atomic_t unfinished_count;
  79. };
  80. static volatile struct smp_call_struct *smp_call_function_data;
  81. enum ipi_message_type {
  82. IPI_NOP=0,
  83. IPI_RESCHEDULE=1,
  84. IPI_CALL_FUNC,
  85. IPI_CPU_START,
  86. IPI_CPU_STOP,
  87. IPI_CPU_TEST
  88. };
  89. /********** SMP inter processor interrupt and communication routines */
  90. #undef PER_CPU_IRQ_REGION
  91. #ifdef PER_CPU_IRQ_REGION
  92. /* XXX REVISIT Ignore for now.
  93. ** *May* need this "hook" to register IPI handler
  94. ** once we have perCPU ExtIntr switch tables.
  95. */
  96. static void
  97. ipi_init(int cpuid)
  98. {
  99. #error verify IRQ_OFFSET(IPI_IRQ) is ipi_interrupt() in new IRQ region
  100. if(cpu_online(cpuid) )
  101. {
  102. switch_to_idle_task(current);
  103. }
  104. return;
  105. }
  106. #endif
  107. /*
  108. ** Yoink this CPU from the runnable list...
  109. **
  110. */
  111. static void
  112. halt_processor(void)
  113. {
  114. /* REVISIT : redirect I/O Interrupts to another CPU? */
  115. /* REVISIT : does PM *know* this CPU isn't available? */
  116. cpu_clear(smp_processor_id(), cpu_online_map);
  117. local_irq_disable();
  118. for (;;)
  119. ;
  120. }
  121. irqreturn_t
  122. ipi_interrupt(int irq, void *dev_id)
  123. {
  124. int this_cpu = smp_processor_id();
  125. struct cpuinfo_parisc *p = &cpu_data[this_cpu];
  126. unsigned long ops;
  127. unsigned long flags;
  128. /* Count this now; we may make a call that never returns. */
  129. p->ipi_count++;
  130. mb(); /* Order interrupt and bit testing. */
  131. for (;;) {
  132. spinlock_t *lock = &per_cpu(ipi_lock, this_cpu);
  133. spin_lock_irqsave(lock, flags);
  134. ops = p->pending_ipi;
  135. p->pending_ipi = 0;
  136. spin_unlock_irqrestore(lock, flags);
  137. mb(); /* Order bit clearing and data access. */
  138. if (!ops)
  139. break;
  140. while (ops) {
  141. unsigned long which = ffz(~ops);
  142. ops &= ~(1 << which);
  143. switch (which) {
  144. case IPI_NOP:
  145. smp_debug(100, KERN_DEBUG "CPU%d IPI_NOP\n", this_cpu);
  146. break;
  147. case IPI_RESCHEDULE:
  148. smp_debug(100, KERN_DEBUG "CPU%d IPI_RESCHEDULE\n", this_cpu);
  149. /*
  150. * Reschedule callback. Everything to be
  151. * done is done by the interrupt return path.
  152. */
  153. break;
  154. case IPI_CALL_FUNC:
  155. smp_debug(100, KERN_DEBUG "CPU%d IPI_CALL_FUNC\n", this_cpu);
  156. {
  157. volatile struct smp_call_struct *data;
  158. void (*func)(void *info);
  159. void *info;
  160. int wait;
  161. data = smp_call_function_data;
  162. func = data->func;
  163. info = data->info;
  164. wait = data->wait;
  165. mb();
  166. atomic_dec ((atomic_t *)&data->unstarted_count);
  167. /* At this point, *data can't
  168. * be relied upon.
  169. */
  170. (*func)(info);
  171. /* Notify the sending CPU that the
  172. * task is done.
  173. */
  174. mb();
  175. if (wait)
  176. atomic_dec ((atomic_t *)&data->unfinished_count);
  177. }
  178. break;
  179. case IPI_CPU_START:
  180. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_START\n", this_cpu);
  181. break;
  182. case IPI_CPU_STOP:
  183. smp_debug(100, KERN_DEBUG "CPU%d IPI_CPU_STOP\n", this_cpu);
  184. halt_processor();
  185. break;
  186. case IPI_CPU_TEST:
  187. smp_debug(100, KERN_DEBUG "CPU%d is alive!\n", this_cpu);
  188. break;
  189. default:
  190. printk(KERN_CRIT "Unknown IPI num on CPU%d: %lu\n",
  191. this_cpu, which);
  192. return IRQ_NONE;
  193. } /* Switch */
  194. /* let in any pending interrupts */
  195. local_irq_enable();
  196. local_irq_disable();
  197. } /* while (ops) */
  198. }
  199. return IRQ_HANDLED;
  200. }
  201. static inline void
  202. ipi_send(int cpu, enum ipi_message_type op)
  203. {
  204. struct cpuinfo_parisc *p = &cpu_data[cpu];
  205. spinlock_t *lock = &per_cpu(ipi_lock, cpu);
  206. unsigned long flags;
  207. spin_lock_irqsave(lock, flags);
  208. p->pending_ipi |= 1 << op;
  209. gsc_writel(IPI_IRQ - CPU_IRQ_BASE, cpu_data[cpu].hpa);
  210. spin_unlock_irqrestore(lock, flags);
  211. }
  212. static inline void
  213. send_IPI_single(int dest_cpu, enum ipi_message_type op)
  214. {
  215. if (dest_cpu == NO_PROC_ID) {
  216. BUG();
  217. return;
  218. }
  219. ipi_send(dest_cpu, op);
  220. }
  221. static inline void
  222. send_IPI_allbutself(enum ipi_message_type op)
  223. {
  224. int i;
  225. for_each_online_cpu(i) {
  226. if (i != smp_processor_id())
  227. send_IPI_single(i, op);
  228. }
  229. }
  230. inline void
  231. smp_send_stop(void) { send_IPI_allbutself(IPI_CPU_STOP); }
  232. static inline void
  233. smp_send_start(void) { send_IPI_allbutself(IPI_CPU_START); }
  234. void
  235. smp_send_reschedule(int cpu) { send_IPI_single(cpu, IPI_RESCHEDULE); }
  236. void
  237. smp_send_all_nop(void)
  238. {
  239. send_IPI_allbutself(IPI_NOP);
  240. }
  241. /**
  242. * Run a function on all other CPUs.
  243. * <func> The function to run. This must be fast and non-blocking.
  244. * <info> An arbitrary pointer to pass to the function.
  245. * <retry> If true, keep retrying until ready.
  246. * <wait> If true, wait until function has completed on other CPUs.
  247. * [RETURNS] 0 on success, else a negative status code.
  248. *
  249. * Does not return until remote CPUs are nearly ready to execute <func>
  250. * or have executed.
  251. */
  252. int
  253. smp_call_function (void (*func) (void *info), void *info, int retry, int wait)
  254. {
  255. struct smp_call_struct data;
  256. unsigned long timeout;
  257. static DEFINE_SPINLOCK(lock);
  258. int retries = 0;
  259. if (num_online_cpus() < 2)
  260. return 0;
  261. /* Can deadlock when called with interrupts disabled */
  262. WARN_ON(irqs_disabled());
  263. /* can also deadlock if IPIs are disabled */
  264. WARN_ON((get_eiem() & (1UL<<(CPU_IRQ_MAX - IPI_IRQ))) == 0);
  265. data.func = func;
  266. data.info = info;
  267. data.wait = wait;
  268. atomic_set(&data.unstarted_count, num_online_cpus() - 1);
  269. atomic_set(&data.unfinished_count, num_online_cpus() - 1);
  270. if (retry) {
  271. spin_lock (&lock);
  272. while (smp_call_function_data != 0)
  273. barrier();
  274. }
  275. else {
  276. spin_lock (&lock);
  277. if (smp_call_function_data) {
  278. spin_unlock (&lock);
  279. return -EBUSY;
  280. }
  281. }
  282. smp_call_function_data = &data;
  283. spin_unlock (&lock);
  284. /* Send a message to all other CPUs and wait for them to respond */
  285. send_IPI_allbutself(IPI_CALL_FUNC);
  286. retry:
  287. /* Wait for response */
  288. timeout = jiffies + HZ;
  289. while ( (atomic_read (&data.unstarted_count) > 0) &&
  290. time_before (jiffies, timeout) )
  291. barrier ();
  292. if (atomic_read (&data.unstarted_count) > 0) {
  293. printk(KERN_CRIT "SMP CALL FUNCTION TIMED OUT! (cpu=%d), try %d\n",
  294. smp_processor_id(), ++retries);
  295. goto retry;
  296. }
  297. /* We either got one or timed out. Release the lock */
  298. mb();
  299. smp_call_function_data = NULL;
  300. while (wait && atomic_read (&data.unfinished_count) > 0)
  301. barrier ();
  302. return 0;
  303. }
  304. EXPORT_SYMBOL(smp_call_function);
  305. /*
  306. * Flush all other CPU's tlb and then mine. Do this with on_each_cpu()
  307. * as we want to ensure all TLB's flushed before proceeding.
  308. */
  309. void
  310. smp_flush_tlb_all(void)
  311. {
  312. on_each_cpu(flush_tlb_all_local, NULL, 1, 1);
  313. }
  314. /*
  315. * Called by secondaries to update state and initialize CPU registers.
  316. */
  317. static void __init
  318. smp_cpu_init(int cpunum)
  319. {
  320. extern int init_per_cpu(int); /* arch/parisc/kernel/processor.c */
  321. extern void init_IRQ(void); /* arch/parisc/kernel/irq.c */
  322. extern void start_cpu_itimer(void); /* arch/parisc/kernel/time.c */
  323. /* Set modes and Enable floating point coprocessor */
  324. (void) init_per_cpu(cpunum);
  325. disable_sr_hashing();
  326. mb();
  327. /* Well, support 2.4 linux scheme as well. */
  328. if (cpu_test_and_set(cpunum, cpu_online_map))
  329. {
  330. extern void machine_halt(void); /* arch/parisc.../process.c */
  331. printk(KERN_CRIT "CPU#%d already initialized!\n", cpunum);
  332. machine_halt();
  333. }
  334. /* Initialise the idle task for this CPU */
  335. atomic_inc(&init_mm.mm_count);
  336. current->active_mm = &init_mm;
  337. if(current->mm)
  338. BUG();
  339. enter_lazy_tlb(&init_mm, current);
  340. init_IRQ(); /* make sure no IRQ's are enabled or pending */
  341. start_cpu_itimer();
  342. }
  343. /*
  344. * Slaves start using C here. Indirectly called from smp_slave_stext.
  345. * Do what start_kernel() and main() do for boot strap processor (aka monarch)
  346. */
  347. void __init smp_callin(void)
  348. {
  349. int slave_id = cpu_now_booting;
  350. #if 0
  351. void *istack;
  352. #endif
  353. smp_cpu_init(slave_id);
  354. preempt_disable();
  355. #if 0 /* NOT WORKING YET - see entry.S */
  356. istack = (void *)__get_free_pages(GFP_KERNEL,ISTACK_ORDER);
  357. if (istack == NULL) {
  358. printk(KERN_CRIT "Failed to allocate interrupt stack for cpu %d\n",slave_id);
  359. BUG();
  360. }
  361. mtctl(istack,31);
  362. #endif
  363. flush_cache_all_local(); /* start with known state */
  364. flush_tlb_all_local(NULL);
  365. local_irq_enable(); /* Interrupts have been off until now */
  366. cpu_idle(); /* Wait for timer to schedule some work */
  367. /* NOTREACHED */
  368. panic("smp_callin() AAAAaaaaahhhh....\n");
  369. }
  370. /*
  371. * Bring one cpu online.
  372. */
  373. int __init smp_boot_one_cpu(int cpuid)
  374. {
  375. struct task_struct *idle;
  376. long timeout;
  377. /*
  378. * Create an idle task for this CPU. Note the address wed* give
  379. * to kernel_thread is irrelevant -- it's going to start
  380. * where OS_BOOT_RENDEVZ vector in SAL says to start. But
  381. * this gets all the other task-y sort of data structures set
  382. * up like we wish. We need to pull the just created idle task
  383. * off the run queue and stuff it into the init_tasks[] array.
  384. * Sheesh . . .
  385. */
  386. idle = fork_idle(cpuid);
  387. if (IS_ERR(idle))
  388. panic("SMP: fork failed for CPU:%d", cpuid);
  389. task_thread_info(idle)->cpu = cpuid;
  390. /* Let _start know what logical CPU we're booting
  391. ** (offset into init_tasks[],cpu_data[])
  392. */
  393. cpu_now_booting = cpuid;
  394. /*
  395. ** boot strap code needs to know the task address since
  396. ** it also contains the process stack.
  397. */
  398. smp_init_current_idle_task = idle ;
  399. mb();
  400. printk("Releasing cpu %d now, hpa=%lx\n", cpuid, cpu_data[cpuid].hpa);
  401. /*
  402. ** This gets PDC to release the CPU from a very tight loop.
  403. **
  404. ** From the PA-RISC 2.0 Firmware Architecture Reference Specification:
  405. ** "The MEM_RENDEZ vector specifies the location of OS_RENDEZ which
  406. ** is executed after receiving the rendezvous signal (an interrupt to
  407. ** EIR{0}). MEM_RENDEZ is valid only when it is nonzero and the
  408. ** contents of memory are valid."
  409. */
  410. gsc_writel(TIMER_IRQ - CPU_IRQ_BASE, cpu_data[cpuid].hpa);
  411. mb();
  412. /*
  413. * OK, wait a bit for that CPU to finish staggering about.
  414. * Slave will set a bit when it reaches smp_cpu_init().
  415. * Once the "monarch CPU" sees the bit change, it can move on.
  416. */
  417. for (timeout = 0; timeout < 10000; timeout++) {
  418. if(cpu_online(cpuid)) {
  419. /* Which implies Slave has started up */
  420. cpu_now_booting = 0;
  421. smp_init_current_idle_task = NULL;
  422. goto alive ;
  423. }
  424. udelay(100);
  425. barrier();
  426. }
  427. put_task_struct(idle);
  428. idle = NULL;
  429. printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
  430. return -1;
  431. alive:
  432. /* Remember the Slave data */
  433. smp_debug(100, KERN_DEBUG "SMP: CPU:%d came alive after %ld _us\n",
  434. cpuid, timeout * 100);
  435. return 0;
  436. }
  437. void __devinit smp_prepare_boot_cpu(void)
  438. {
  439. int bootstrap_processor=cpu_data[0].cpuid; /* CPU ID of BSP */
  440. /* Setup BSP mappings */
  441. printk("SMP: bootstrap CPU ID is %d\n",bootstrap_processor);
  442. cpu_set(bootstrap_processor, cpu_online_map);
  443. cpu_set(bootstrap_processor, cpu_present_map);
  444. }
  445. /*
  446. ** inventory.c:do_inventory() hasn't yet been run and thus we
  447. ** don't 'discover' the additional CPU's until later.
  448. */
  449. void __init smp_prepare_cpus(unsigned int max_cpus)
  450. {
  451. cpus_clear(cpu_present_map);
  452. cpu_set(0, cpu_present_map);
  453. parisc_max_cpus = max_cpus;
  454. if (!max_cpus)
  455. printk(KERN_INFO "SMP mode deactivated.\n");
  456. }
  457. void smp_cpus_done(unsigned int cpu_max)
  458. {
  459. return;
  460. }
  461. int __cpuinit __cpu_up(unsigned int cpu)
  462. {
  463. if (cpu != 0 && cpu < parisc_max_cpus)
  464. smp_boot_one_cpu(cpu);
  465. return cpu_online(cpu) ? 0 : -ENOSYS;
  466. }
  467. #ifdef CONFIG_PROC_FS
  468. int __init
  469. setup_profiling_timer(unsigned int multiplier)
  470. {
  471. return -EINVAL;
  472. }
  473. #endif