smp.c 9.5 KB

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