smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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_MPIC
  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_MPIC */
  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 struct gettimeofday_struct do_gtod;
  289. struct thread_info *current_set[NR_CPUS];
  290. DECLARE_PER_CPU(unsigned int, pvr);
  291. static void __devinit smp_store_cpu_info(int id)
  292. {
  293. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  294. }
  295. static void __init smp_create_idle(unsigned int cpu)
  296. {
  297. struct task_struct *p;
  298. /* create a process for the processor */
  299. p = fork_idle(cpu);
  300. if (IS_ERR(p))
  301. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  302. paca[cpu].__current = p;
  303. current_set[cpu] = p->thread_info;
  304. }
  305. void __init smp_prepare_cpus(unsigned int max_cpus)
  306. {
  307. unsigned int cpu;
  308. DBG("smp_prepare_cpus\n");
  309. /*
  310. * setup_cpu may need to be called on the boot cpu. We havent
  311. * spun any cpus up but lets be paranoid.
  312. */
  313. BUG_ON(boot_cpuid != smp_processor_id());
  314. /* Fixup boot cpu */
  315. smp_store_cpu_info(boot_cpuid);
  316. cpu_callin_map[boot_cpuid] = 1;
  317. #ifndef CONFIG_PPC_ISERIES
  318. paca[boot_cpuid].next_jiffy_update_tb = tb_last_stamp = get_tb();
  319. /*
  320. * Should update do_gtod.stamp_xsec.
  321. * For now we leave it which means the time can be some
  322. * number of msecs off until someone does a settimeofday()
  323. */
  324. do_gtod.varp->tb_orig_stamp = tb_last_stamp;
  325. systemcfg->tb_orig_stamp = tb_last_stamp;
  326. #endif
  327. max_cpus = smp_ops->probe();
  328. smp_space_timers(max_cpus);
  329. for_each_cpu(cpu)
  330. if (cpu != boot_cpuid)
  331. smp_create_idle(cpu);
  332. }
  333. void __devinit smp_prepare_boot_cpu(void)
  334. {
  335. BUG_ON(smp_processor_id() != boot_cpuid);
  336. cpu_set(boot_cpuid, cpu_online_map);
  337. paca[boot_cpuid].__current = current;
  338. current_set[boot_cpuid] = current->thread_info;
  339. }
  340. #ifdef CONFIG_HOTPLUG_CPU
  341. /* State of each CPU during hotplug phases */
  342. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  343. int generic_cpu_disable(void)
  344. {
  345. unsigned int cpu = smp_processor_id();
  346. if (cpu == boot_cpuid)
  347. return -EBUSY;
  348. systemcfg->processorCount--;
  349. cpu_clear(cpu, cpu_online_map);
  350. fixup_irqs(cpu_online_map);
  351. return 0;
  352. }
  353. int generic_cpu_enable(unsigned int cpu)
  354. {
  355. /* Do the normal bootup if we haven't
  356. * already bootstrapped. */
  357. if (system_state != SYSTEM_RUNNING)
  358. return -ENOSYS;
  359. /* get the target out of it's holding state */
  360. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  361. smp_wmb();
  362. while (!cpu_online(cpu))
  363. cpu_relax();
  364. fixup_irqs(cpu_online_map);
  365. /* counter the irq disable in fixup_irqs */
  366. local_irq_enable();
  367. return 0;
  368. }
  369. void generic_cpu_die(unsigned int cpu)
  370. {
  371. int i;
  372. for (i = 0; i < 100; i++) {
  373. smp_rmb();
  374. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  375. return;
  376. msleep(100);
  377. }
  378. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  379. }
  380. void generic_mach_cpu_die(void)
  381. {
  382. unsigned int cpu;
  383. local_irq_disable();
  384. cpu = smp_processor_id();
  385. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  386. __get_cpu_var(cpu_state) = CPU_DEAD;
  387. smp_wmb();
  388. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  389. cpu_relax();
  390. flush_tlb_pending();
  391. cpu_set(cpu, cpu_online_map);
  392. local_irq_enable();
  393. }
  394. #endif
  395. static int __devinit cpu_enable(unsigned int cpu)
  396. {
  397. if (smp_ops->cpu_enable)
  398. return smp_ops->cpu_enable(cpu);
  399. return -ENOSYS;
  400. }
  401. int __devinit __cpu_up(unsigned int cpu)
  402. {
  403. int c;
  404. if (!cpu_enable(cpu))
  405. return 0;
  406. if (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu))
  407. return -EINVAL;
  408. paca[cpu].default_decr = tb_ticks_per_jiffy;
  409. if (!cpu_has_feature(CPU_FTR_SLB)) {
  410. void *tmp;
  411. /* maximum of 48 CPUs on machines with a segment table */
  412. if (cpu >= 48)
  413. BUG();
  414. tmp = &stab_array[PAGE_SIZE * cpu];
  415. memset(tmp, 0, PAGE_SIZE);
  416. paca[cpu].stab_addr = (unsigned long)tmp;
  417. paca[cpu].stab_real = virt_to_abs(tmp);
  418. }
  419. /* Make sure callin-map entry is 0 (can be leftover a CPU
  420. * hotplug
  421. */
  422. cpu_callin_map[cpu] = 0;
  423. /* The information for processor bringup must
  424. * be written out to main store before we release
  425. * the processor.
  426. */
  427. smp_mb();
  428. /* wake up cpus */
  429. DBG("smp: kicking cpu %d\n", cpu);
  430. smp_ops->kick_cpu(cpu);
  431. /*
  432. * wait to see if the cpu made a callin (is actually up).
  433. * use this value that I found through experimentation.
  434. * -- Cort
  435. */
  436. if (system_state < SYSTEM_RUNNING)
  437. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  438. udelay(100);
  439. #ifdef CONFIG_HOTPLUG_CPU
  440. else
  441. /*
  442. * CPUs can take much longer to come up in the
  443. * hotplug case. Wait five seconds.
  444. */
  445. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  446. msleep(200);
  447. }
  448. #endif
  449. if (!cpu_callin_map[cpu]) {
  450. printk("Processor %u is stuck.\n", cpu);
  451. return -ENOENT;
  452. }
  453. printk("Processor %u found.\n", cpu);
  454. if (smp_ops->give_timebase)
  455. smp_ops->give_timebase();
  456. /* Wait until cpu puts itself in the online map */
  457. while (!cpu_online(cpu))
  458. cpu_relax();
  459. return 0;
  460. }
  461. /* Activate a secondary processor. */
  462. int __devinit start_secondary(void *unused)
  463. {
  464. unsigned int cpu = smp_processor_id();
  465. atomic_inc(&init_mm.mm_count);
  466. current->active_mm = &init_mm;
  467. smp_store_cpu_info(cpu);
  468. set_dec(paca[cpu].default_decr);
  469. cpu_callin_map[cpu] = 1;
  470. smp_ops->setup_cpu(cpu);
  471. if (smp_ops->take_timebase)
  472. smp_ops->take_timebase();
  473. spin_lock(&call_lock);
  474. cpu_set(cpu, cpu_online_map);
  475. spin_unlock(&call_lock);
  476. local_irq_enable();
  477. cpu_idle();
  478. return 0;
  479. }
  480. int setup_profiling_timer(unsigned int multiplier)
  481. {
  482. return 0;
  483. }
  484. void __init smp_cpus_done(unsigned int max_cpus)
  485. {
  486. cpumask_t old_mask;
  487. /* We want the setup_cpu() here to be called from CPU 0, but our
  488. * init thread may have been "borrowed" by another CPU in the meantime
  489. * se we pin us down to CPU 0 for a short while
  490. */
  491. old_mask = current->cpus_allowed;
  492. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  493. smp_ops->setup_cpu(boot_cpuid);
  494. set_cpus_allowed(current, old_mask);
  495. }
  496. #ifdef CONFIG_HOTPLUG_CPU
  497. int __cpu_disable(void)
  498. {
  499. if (smp_ops->cpu_disable)
  500. return smp_ops->cpu_disable();
  501. return -ENOSYS;
  502. }
  503. void __cpu_die(unsigned int cpu)
  504. {
  505. if (smp_ops->cpu_die)
  506. smp_ops->cpu_die(cpu);
  507. }
  508. #endif