smp.c 9.5 KB

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