smp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/cache.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/atomic.h>
  22. #include <asm/irq.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/io.h>
  26. #include <asm/prom.h>
  27. #include <asm/smp.h>
  28. #include <asm/residual.h>
  29. #include <asm/time.h>
  30. #include <asm/thread_info.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/xmon.h>
  33. #include <asm/machdep.h>
  34. volatile int smp_commenced;
  35. int smp_tb_synchronized;
  36. struct cpuinfo_PPC cpu_data[NR_CPUS];
  37. atomic_t ipi_recv;
  38. atomic_t ipi_sent;
  39. cpumask_t cpu_online_map;
  40. cpumask_t cpu_possible_map;
  41. int smp_hw_index[NR_CPUS];
  42. struct thread_info *secondary_ti;
  43. static struct task_struct *idle_tasks[NR_CPUS];
  44. EXPORT_SYMBOL(cpu_online_map);
  45. EXPORT_SYMBOL(cpu_possible_map);
  46. /* SMP operations for this machine */
  47. struct smp_ops_t *smp_ops;
  48. /* all cpu mappings are 1-1 -- Cort */
  49. volatile unsigned long cpu_callin_map[NR_CPUS];
  50. int start_secondary(void *);
  51. void smp_call_function_interrupt(void);
  52. static int __smp_call_function(void (*func) (void *info), void *info,
  53. int wait, int target);
  54. /* Low level assembly function used to backup CPU 0 state */
  55. extern void __save_cpu_setup(void);
  56. /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
  57. *
  58. * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
  59. * in /proc/interrupts will be wrong!!! --Troy */
  60. #define PPC_MSG_CALL_FUNCTION 0
  61. #define PPC_MSG_RESCHEDULE 1
  62. #define PPC_MSG_INVALIDATE_TLB 2
  63. #define PPC_MSG_XMON_BREAK 3
  64. static inline void
  65. smp_message_pass(int target, int msg)
  66. {
  67. if (smp_ops) {
  68. atomic_inc(&ipi_sent);
  69. smp_ops->message_pass(target, msg);
  70. }
  71. }
  72. /*
  73. * Common functions
  74. */
  75. void smp_message_recv(int msg)
  76. {
  77. atomic_inc(&ipi_recv);
  78. switch( msg ) {
  79. case PPC_MSG_CALL_FUNCTION:
  80. smp_call_function_interrupt();
  81. break;
  82. case PPC_MSG_RESCHEDULE:
  83. set_need_resched();
  84. break;
  85. case PPC_MSG_INVALIDATE_TLB:
  86. _tlbia();
  87. break;
  88. #ifdef CONFIG_XMON
  89. case PPC_MSG_XMON_BREAK:
  90. xmon(get_irq_regs());
  91. break;
  92. #endif /* CONFIG_XMON */
  93. default:
  94. printk("SMP %d: smp_message_recv(): unknown msg %d\n",
  95. smp_processor_id(), msg);
  96. break;
  97. }
  98. }
  99. /*
  100. * 750's don't broadcast tlb invalidates so
  101. * we have to emulate that behavior.
  102. * -- Cort
  103. */
  104. void smp_send_tlb_invalidate(int cpu)
  105. {
  106. if ( PVR_VER(mfspr(SPRN_PVR)) == 8 )
  107. smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB);
  108. }
  109. void smp_send_reschedule(int cpu)
  110. {
  111. /*
  112. * This is only used if `cpu' is running an idle task,
  113. * so it will reschedule itself anyway...
  114. *
  115. * This isn't the case anymore since the other CPU could be
  116. * sleeping and won't reschedule until the next interrupt (such
  117. * as the timer).
  118. * -- Cort
  119. */
  120. /* This is only used if `cpu' is running an idle task,
  121. so it will reschedule itself anyway... */
  122. smp_message_pass(cpu, PPC_MSG_RESCHEDULE);
  123. }
  124. #ifdef CONFIG_XMON
  125. void smp_send_xmon_break(int cpu)
  126. {
  127. smp_message_pass(cpu, PPC_MSG_XMON_BREAK);
  128. }
  129. #endif /* CONFIG_XMON */
  130. static void stop_this_cpu(void *dummy)
  131. {
  132. local_irq_disable();
  133. while (1)
  134. ;
  135. }
  136. void smp_send_stop(void)
  137. {
  138. smp_call_function(stop_this_cpu, NULL, 1, 0);
  139. }
  140. /*
  141. * Structure and data for smp_call_function(). This is designed to minimise
  142. * static memory requirements. It also looks cleaner.
  143. * Stolen from the i386 version.
  144. */
  145. static DEFINE_SPINLOCK(call_lock);
  146. static struct call_data_struct {
  147. void (*func) (void *info);
  148. void *info;
  149. atomic_t started;
  150. atomic_t finished;
  151. int wait;
  152. } *call_data;
  153. /*
  154. * this function sends a 'generic call function' IPI to all other CPUs
  155. * in the system.
  156. */
  157. int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
  158. int wait)
  159. /*
  160. * [SUMMARY] Run a function on all other CPUs.
  161. * <func> The function to run. This must be fast and non-blocking.
  162. * <info> An arbitrary pointer to pass to the function.
  163. * <nonatomic> currently unused.
  164. * <wait> If true, wait (atomically) until function has completed on other CPUs.
  165. * [RETURNS] 0 on success, else a negative status code. Does not return until
  166. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  167. *
  168. * You must not call this function with disabled interrupts or from a
  169. * hardware interrupt handler or from a bottom half handler.
  170. */
  171. {
  172. /* FIXME: get cpu lock with hotplug cpus, or change this to
  173. bitmask. --RR */
  174. if (num_online_cpus() <= 1)
  175. return 0;
  176. /* Can deadlock when called with interrupts disabled */
  177. WARN_ON(irqs_disabled());
  178. return __smp_call_function(func, info, wait, MSG_ALL_BUT_SELF);
  179. }
  180. static int __smp_call_function(void (*func) (void *info), void *info,
  181. int wait, int target)
  182. {
  183. struct call_data_struct data;
  184. int ret = -1;
  185. int timeout;
  186. int ncpus = 1;
  187. if (target == MSG_ALL_BUT_SELF)
  188. ncpus = num_online_cpus() - 1;
  189. else if (target == MSG_ALL)
  190. ncpus = num_online_cpus();
  191. data.func = func;
  192. data.info = info;
  193. atomic_set(&data.started, 0);
  194. data.wait = wait;
  195. if (wait)
  196. atomic_set(&data.finished, 0);
  197. spin_lock(&call_lock);
  198. call_data = &data;
  199. /* Send a message to all other CPUs and wait for them to respond */
  200. smp_message_pass(target, PPC_MSG_CALL_FUNCTION);
  201. /* Wait for response */
  202. timeout = 1000000;
  203. while (atomic_read(&data.started) != ncpus) {
  204. if (--timeout == 0) {
  205. printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
  206. smp_processor_id(), atomic_read(&data.started));
  207. goto out;
  208. }
  209. barrier();
  210. udelay(1);
  211. }
  212. if (wait) {
  213. timeout = 1000000;
  214. while (atomic_read(&data.finished) != ncpus) {
  215. if (--timeout == 0) {
  216. printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
  217. smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
  218. goto out;
  219. }
  220. barrier();
  221. udelay(1);
  222. }
  223. }
  224. ret = 0;
  225. out:
  226. spin_unlock(&call_lock);
  227. return ret;
  228. }
  229. void smp_call_function_interrupt(void)
  230. {
  231. void (*func) (void *info) = call_data->func;
  232. void *info = call_data->info;
  233. int wait = call_data->wait;
  234. /*
  235. * Notify initiating CPU that I've grabbed the data and am
  236. * about to execute the function
  237. */
  238. atomic_inc(&call_data->started);
  239. /*
  240. * At this point the info structure may be out of scope unless wait==1
  241. */
  242. (*func)(info);
  243. if (wait)
  244. atomic_inc(&call_data->finished);
  245. }
  246. static void __devinit smp_store_cpu_info(int id)
  247. {
  248. struct cpuinfo_PPC *c = &cpu_data[id];
  249. /* assume bogomips are same for everything */
  250. c->loops_per_jiffy = loops_per_jiffy;
  251. c->pvr = mfspr(SPRN_PVR);
  252. }
  253. void __init smp_prepare_cpus(unsigned int max_cpus)
  254. {
  255. int num_cpus, i, cpu;
  256. struct task_struct *p;
  257. /* Fixup boot cpu */
  258. smp_store_cpu_info(smp_processor_id());
  259. cpu_callin_map[smp_processor_id()] = 1;
  260. if (smp_ops == NULL) {
  261. printk("SMP not supported on this machine.\n");
  262. return;
  263. }
  264. /* Probe platform for CPUs: always linear. */
  265. num_cpus = smp_ops->probe();
  266. if (num_cpus < 2)
  267. smp_tb_synchronized = 1;
  268. for (i = 0; i < num_cpus; ++i)
  269. cpu_set(i, cpu_possible_map);
  270. /* Backup CPU 0 state */
  271. __save_cpu_setup();
  272. for_each_possible_cpu(cpu) {
  273. if (cpu == smp_processor_id())
  274. continue;
  275. /* create a process for the processor */
  276. p = fork_idle(cpu);
  277. if (IS_ERR(p))
  278. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  279. task_thread_info(p)->cpu = cpu;
  280. idle_tasks[cpu] = p;
  281. }
  282. }
  283. void __devinit smp_prepare_boot_cpu(void)
  284. {
  285. cpu_set(smp_processor_id(), cpu_online_map);
  286. cpu_set(smp_processor_id(), cpu_possible_map);
  287. }
  288. int __init setup_profiling_timer(unsigned int multiplier)
  289. {
  290. return 0;
  291. }
  292. /* Processor coming up starts here */
  293. int __devinit start_secondary(void *unused)
  294. {
  295. int cpu;
  296. atomic_inc(&init_mm.mm_count);
  297. current->active_mm = &init_mm;
  298. cpu = smp_processor_id();
  299. smp_store_cpu_info(cpu);
  300. set_dec(tb_ticks_per_jiffy);
  301. preempt_disable();
  302. cpu_callin_map[cpu] = 1;
  303. printk("CPU %d done callin...\n", cpu);
  304. smp_ops->setup_cpu(cpu);
  305. printk("CPU %d done setup...\n", cpu);
  306. smp_ops->take_timebase();
  307. printk("CPU %d done timebase take...\n", cpu);
  308. spin_lock(&call_lock);
  309. cpu_set(cpu, cpu_online_map);
  310. spin_unlock(&call_lock);
  311. local_irq_enable();
  312. cpu_idle();
  313. return 0;
  314. }
  315. int __cpu_up(unsigned int cpu)
  316. {
  317. char buf[32];
  318. int c;
  319. secondary_ti = task_thread_info(idle_tasks[cpu]);
  320. mb();
  321. /*
  322. * There was a cache flush loop here to flush the cache
  323. * to memory for the first 8MB of RAM. The cache flush
  324. * has been pushed into the kick_cpu function for those
  325. * platforms that need it.
  326. */
  327. /* wake up cpu */
  328. smp_ops->kick_cpu(cpu);
  329. /*
  330. * wait to see if the cpu made a callin (is actually up).
  331. * use this value that I found through experimentation.
  332. * -- Cort
  333. */
  334. for (c = 1000; c && !cpu_callin_map[cpu]; c--)
  335. udelay(100);
  336. if (!cpu_callin_map[cpu]) {
  337. sprintf(buf, "didn't find cpu %u", cpu);
  338. if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu);
  339. printk("Processor %u is stuck.\n", cpu);
  340. return -ENOENT;
  341. }
  342. sprintf(buf, "found cpu %u", cpu);
  343. if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu);
  344. printk("Processor %d found.\n", cpu);
  345. smp_ops->give_timebase();
  346. /* Wait until cpu puts itself in the online map */
  347. while (!cpu_online(cpu))
  348. cpu_relax();
  349. return 0;
  350. }
  351. void smp_cpus_done(unsigned int max_cpus)
  352. {
  353. smp_ops->setup_cpu(0);
  354. }