smp.c 12 KB

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