smp.c 13 KB

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