smp.c 13 KB

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