smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * SMP support for ppc.
  3. *
  4. * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
  5. * deal of code from the sparc and intel versions.
  6. *
  7. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  8. *
  9. * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
  10. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #undef DEBUG
  18. #include <linux/config.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/smp.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/cache.h>
  28. #include <linux/err.h>
  29. #include <linux/sysdev.h>
  30. #include <linux/cpu.h>
  31. #include <linux/notifier.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/atomic.h>
  34. #include <asm/irq.h>
  35. #include <asm/page.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/prom.h>
  38. #include <asm/smp.h>
  39. #include <asm/time.h>
  40. #include <asm/xmon.h>
  41. #include <asm/machdep.h>
  42. #include <asm/cputable.h>
  43. #include <asm/system.h>
  44. #include <asm/mpic.h>
  45. #ifdef CONFIG_PPC64
  46. #include <asm/paca.h>
  47. #endif
  48. int smp_hw_index[NR_CPUS];
  49. struct thread_info *secondary_ti;
  50. #ifdef DEBUG
  51. #define DBG(fmt...) udbg_printf(fmt)
  52. #else
  53. #define DBG(fmt...)
  54. #endif
  55. cpumask_t cpu_possible_map = CPU_MASK_NONE;
  56. cpumask_t cpu_online_map = CPU_MASK_NONE;
  57. cpumask_t cpu_sibling_map[NR_CPUS] = { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
  58. EXPORT_SYMBOL(cpu_online_map);
  59. EXPORT_SYMBOL(cpu_possible_map);
  60. /* SMP operations for this machine */
  61. struct smp_ops_t *smp_ops;
  62. static volatile unsigned int cpu_callin_map[NR_CPUS];
  63. void smp_call_function_interrupt(void);
  64. int smt_enabled_at_boot = 1;
  65. #ifdef CONFIG_MPIC
  66. int __init smp_mpic_probe(void)
  67. {
  68. int nr_cpus;
  69. DBG("smp_mpic_probe()...\n");
  70. nr_cpus = cpus_weight(cpu_possible_map);
  71. DBG("nr_cpus: %d\n", nr_cpus);
  72. if (nr_cpus > 1)
  73. mpic_request_ipis();
  74. return nr_cpus;
  75. }
  76. void __devinit smp_mpic_setup_cpu(int cpu)
  77. {
  78. mpic_setup_this_cpu();
  79. }
  80. #endif /* CONFIG_MPIC */
  81. #ifdef CONFIG_PPC64
  82. void __devinit smp_generic_kick_cpu(int nr)
  83. {
  84. BUG_ON(nr < 0 || nr >= NR_CPUS);
  85. /*
  86. * The processor is currently spinning, waiting for the
  87. * cpu_start field to become non-zero After we set cpu_start,
  88. * the processor will continue on to secondary_start
  89. */
  90. paca[nr].cpu_start = 1;
  91. smp_mb();
  92. }
  93. #endif
  94. void smp_message_recv(int msg, struct pt_regs *regs)
  95. {
  96. switch(msg) {
  97. case PPC_MSG_CALL_FUNCTION:
  98. smp_call_function_interrupt();
  99. break;
  100. case PPC_MSG_RESCHEDULE:
  101. /* XXX Do we have to do this? */
  102. set_need_resched();
  103. break;
  104. #ifdef CONFIG_DEBUGGER
  105. case PPC_MSG_DEBUGGER_BREAK:
  106. debugger_ipi(regs);
  107. break;
  108. #endif
  109. default:
  110. printk("SMP %d: smp_message_recv(): unknown msg %d\n",
  111. smp_processor_id(), msg);
  112. break;
  113. }
  114. }
  115. void smp_send_reschedule(int cpu)
  116. {
  117. smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
  118. }
  119. #ifdef CONFIG_DEBUGGER
  120. void smp_send_debugger_break(int cpu)
  121. {
  122. smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
  123. }
  124. #endif
  125. static void stop_this_cpu(void *dummy)
  126. {
  127. local_irq_disable();
  128. while (1)
  129. ;
  130. }
  131. void smp_send_stop(void)
  132. {
  133. smp_call_function(stop_this_cpu, NULL, 1, 0);
  134. }
  135. /*
  136. * Structure and data for smp_call_function(). This is designed to minimise
  137. * static memory requirements. It also looks cleaner.
  138. * Stolen from the i386 version.
  139. */
  140. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
  141. static struct call_data_struct {
  142. void (*func) (void *info);
  143. void *info;
  144. atomic_t started;
  145. atomic_t finished;
  146. int wait;
  147. } *call_data;
  148. /* delay of at least 8 seconds */
  149. #define SMP_CALL_TIMEOUT 8
  150. /*
  151. * This function sends a 'generic call function' IPI to all other CPUs
  152. * in the system.
  153. *
  154. * [SUMMARY] Run a function on all other CPUs.
  155. * <func> The function to run. This must be fast and non-blocking.
  156. * <info> An arbitrary pointer to pass to the function.
  157. * <nonatomic> currently unused.
  158. * <wait> If true, wait (atomically) until function has completed on other CPUs.
  159. * [RETURNS] 0 on success, else a negative status code. Does not return until
  160. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  161. *
  162. * You must not call this function with disabled interrupts or from a
  163. * hardware interrupt handler or from a bottom half handler.
  164. */
  165. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  166. int wait)
  167. {
  168. struct call_data_struct data;
  169. int ret = -1, cpus;
  170. u64 timeout;
  171. /* Can deadlock when called with interrupts disabled */
  172. WARN_ON(irqs_disabled());
  173. data.func = func;
  174. data.info = info;
  175. atomic_set(&data.started, 0);
  176. data.wait = wait;
  177. if (wait)
  178. atomic_set(&data.finished, 0);
  179. spin_lock(&call_lock);
  180. /* Must grab online cpu count with preempt disabled, otherwise
  181. * it can change. */
  182. cpus = num_online_cpus() - 1;
  183. if (!cpus) {
  184. ret = 0;
  185. goto out;
  186. }
  187. call_data = &data;
  188. smp_wmb();
  189. /* Send a message to all other CPUs and wait for them to respond */
  190. smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_CALL_FUNCTION);
  191. timeout = get_tb() + (u64) SMP_CALL_TIMEOUT * tb_ticks_per_sec;
  192. /* Wait for response */
  193. while (atomic_read(&data.started) != cpus) {
  194. HMT_low();
  195. if (get_tb() >= timeout) {
  196. printk("smp_call_function on cpu %d: other cpus not "
  197. "responding (%d)\n", smp_processor_id(),
  198. atomic_read(&data.started));
  199. debugger(NULL);
  200. goto out;
  201. }
  202. }
  203. if (wait) {
  204. while (atomic_read(&data.finished) != cpus) {
  205. HMT_low();
  206. if (get_tb() >= timeout) {
  207. printk("smp_call_function on cpu %d: other "
  208. "cpus not finishing (%d/%d)\n",
  209. smp_processor_id(),
  210. atomic_read(&data.finished),
  211. atomic_read(&data.started));
  212. debugger(NULL);
  213. goto out;
  214. }
  215. }
  216. }
  217. ret = 0;
  218. out:
  219. call_data = NULL;
  220. HMT_medium();
  221. spin_unlock(&call_lock);
  222. return ret;
  223. }
  224. EXPORT_SYMBOL(smp_call_function);
  225. void smp_call_function_interrupt(void)
  226. {
  227. void (*func) (void *info);
  228. void *info;
  229. int wait;
  230. /* call_data will be NULL if the sender timed out while
  231. * waiting on us to receive the call.
  232. */
  233. if (!call_data)
  234. return;
  235. func = call_data->func;
  236. info = call_data->info;
  237. wait = call_data->wait;
  238. if (!wait)
  239. smp_mb__before_atomic_inc();
  240. /*
  241. * Notify initiating CPU that I've grabbed the data and am
  242. * about to execute the function
  243. */
  244. atomic_inc(&call_data->started);
  245. /*
  246. * At this point the info structure may be out of scope unless wait==1
  247. */
  248. (*func)(info);
  249. if (wait) {
  250. smp_mb__before_atomic_inc();
  251. atomic_inc(&call_data->finished);
  252. }
  253. }
  254. extern struct gettimeofday_struct do_gtod;
  255. struct thread_info *current_set[NR_CPUS];
  256. DECLARE_PER_CPU(unsigned int, pvr);
  257. static void __devinit smp_store_cpu_info(int id)
  258. {
  259. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  260. }
  261. static void __init smp_create_idle(unsigned int cpu)
  262. {
  263. struct task_struct *p;
  264. /* create a process for the processor */
  265. p = fork_idle(cpu);
  266. if (IS_ERR(p))
  267. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  268. #ifdef CONFIG_PPC64
  269. paca[cpu].__current = p;
  270. #endif
  271. current_set[cpu] = p->thread_info;
  272. p->thread_info->cpu = cpu;
  273. }
  274. void __init smp_prepare_cpus(unsigned int max_cpus)
  275. {
  276. unsigned int cpu;
  277. DBG("smp_prepare_cpus\n");
  278. /*
  279. * setup_cpu may need to be called on the boot cpu. We havent
  280. * spun any cpus up but lets be paranoid.
  281. */
  282. BUG_ON(boot_cpuid != smp_processor_id());
  283. /* Fixup boot cpu */
  284. smp_store_cpu_info(boot_cpuid);
  285. cpu_callin_map[boot_cpuid] = 1;
  286. max_cpus = smp_ops->probe();
  287. smp_space_timers(max_cpus);
  288. for_each_cpu(cpu)
  289. if (cpu != boot_cpuid)
  290. smp_create_idle(cpu);
  291. }
  292. void __devinit smp_prepare_boot_cpu(void)
  293. {
  294. BUG_ON(smp_processor_id() != boot_cpuid);
  295. cpu_set(boot_cpuid, cpu_online_map);
  296. #ifdef CONFIG_PPC64
  297. paca[boot_cpuid].__current = current;
  298. #endif
  299. current_set[boot_cpuid] = current->thread_info;
  300. }
  301. #ifdef CONFIG_HOTPLUG_CPU
  302. /* State of each CPU during hotplug phases */
  303. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  304. int generic_cpu_disable(void)
  305. {
  306. unsigned int cpu = smp_processor_id();
  307. if (cpu == boot_cpuid)
  308. return -EBUSY;
  309. systemcfg->processorCount--;
  310. cpu_clear(cpu, cpu_online_map);
  311. fixup_irqs(cpu_online_map);
  312. return 0;
  313. }
  314. int generic_cpu_enable(unsigned int cpu)
  315. {
  316. /* Do the normal bootup if we haven't
  317. * already bootstrapped. */
  318. if (system_state != SYSTEM_RUNNING)
  319. return -ENOSYS;
  320. /* get the target out of it's holding state */
  321. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  322. smp_wmb();
  323. while (!cpu_online(cpu))
  324. cpu_relax();
  325. fixup_irqs(cpu_online_map);
  326. /* counter the irq disable in fixup_irqs */
  327. local_irq_enable();
  328. return 0;
  329. }
  330. void generic_cpu_die(unsigned int cpu)
  331. {
  332. int i;
  333. for (i = 0; i < 100; i++) {
  334. smp_rmb();
  335. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  336. return;
  337. msleep(100);
  338. }
  339. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  340. }
  341. void generic_mach_cpu_die(void)
  342. {
  343. unsigned int cpu;
  344. local_irq_disable();
  345. cpu = smp_processor_id();
  346. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  347. __get_cpu_var(cpu_state) = CPU_DEAD;
  348. smp_wmb();
  349. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  350. cpu_relax();
  351. flush_tlb_pending();
  352. cpu_set(cpu, cpu_online_map);
  353. local_irq_enable();
  354. }
  355. #endif
  356. static int __devinit cpu_enable(unsigned int cpu)
  357. {
  358. if (smp_ops->cpu_enable)
  359. return smp_ops->cpu_enable(cpu);
  360. return -ENOSYS;
  361. }
  362. int __devinit __cpu_up(unsigned int cpu)
  363. {
  364. int c;
  365. secondary_ti = current_set[cpu];
  366. if (!cpu_enable(cpu))
  367. return 0;
  368. if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu))
  369. return -EINVAL;
  370. #ifdef CONFIG_PPC64
  371. paca[cpu].default_decr = tb_ticks_per_jiffy;
  372. #endif
  373. /* Make sure callin-map entry is 0 (can be leftover a CPU
  374. * hotplug
  375. */
  376. cpu_callin_map[cpu] = 0;
  377. /* The information for processor bringup must
  378. * be written out to main store before we release
  379. * the processor.
  380. */
  381. smp_mb();
  382. /* wake up cpus */
  383. DBG("smp: kicking cpu %d\n", cpu);
  384. smp_ops->kick_cpu(cpu);
  385. /*
  386. * wait to see if the cpu made a callin (is actually up).
  387. * use this value that I found through experimentation.
  388. * -- Cort
  389. */
  390. if (system_state < SYSTEM_RUNNING)
  391. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  392. udelay(100);
  393. #ifdef CONFIG_HOTPLUG_CPU
  394. else
  395. /*
  396. * CPUs can take much longer to come up in the
  397. * hotplug case. Wait five seconds.
  398. */
  399. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  400. msleep(200);
  401. }
  402. #endif
  403. if (!cpu_callin_map[cpu]) {
  404. printk("Processor %u is stuck.\n", cpu);
  405. return -ENOENT;
  406. }
  407. printk("Processor %u found.\n", cpu);
  408. if (smp_ops->give_timebase)
  409. smp_ops->give_timebase();
  410. /* Wait until cpu puts itself in the online map */
  411. while (!cpu_online(cpu))
  412. cpu_relax();
  413. return 0;
  414. }
  415. /* Activate a secondary processor. */
  416. int __devinit start_secondary(void *unused)
  417. {
  418. unsigned int cpu = smp_processor_id();
  419. atomic_inc(&init_mm.mm_count);
  420. current->active_mm = &init_mm;
  421. smp_store_cpu_info(cpu);
  422. set_dec(tb_ticks_per_jiffy);
  423. cpu_callin_map[cpu] = 1;
  424. smp_ops->setup_cpu(cpu);
  425. if (smp_ops->take_timebase)
  426. smp_ops->take_timebase();
  427. spin_lock(&call_lock);
  428. cpu_set(cpu, cpu_online_map);
  429. spin_unlock(&call_lock);
  430. local_irq_enable();
  431. cpu_idle();
  432. return 0;
  433. }
  434. int setup_profiling_timer(unsigned int multiplier)
  435. {
  436. return 0;
  437. }
  438. void __init smp_cpus_done(unsigned int max_cpus)
  439. {
  440. cpumask_t old_mask;
  441. /* We want the setup_cpu() here to be called from CPU 0, but our
  442. * init thread may have been "borrowed" by another CPU in the meantime
  443. * se we pin us down to CPU 0 for a short while
  444. */
  445. old_mask = current->cpus_allowed;
  446. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  447. smp_ops->setup_cpu(boot_cpuid);
  448. set_cpus_allowed(current, old_mask);
  449. }
  450. #ifdef CONFIG_HOTPLUG_CPU
  451. int __cpu_disable(void)
  452. {
  453. if (smp_ops->cpu_disable)
  454. return smp_ops->cpu_disable();
  455. return -ENOSYS;
  456. }
  457. void __cpu_die(unsigned int cpu)
  458. {
  459. if (smp_ops->cpu_die)
  460. smp_ops->cpu_die(cpu);
  461. }
  462. #endif