smp.c 13 KB

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