smp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. volatile int smp_commenced;
  36. int smp_tb_synchronized;
  37. struct cpuinfo_PPC cpu_data[NR_CPUS];
  38. struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
  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. EXPORT_SYMBOL(cpu_online_map);
  46. EXPORT_SYMBOL(cpu_possible_map);
  47. /* SMP operations for this machine */
  48. static 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, unsigned long data, int wait)
  67. {
  68. if (smp_ops){
  69. atomic_inc(&ipi_sent);
  70. smp_ops->message_pass(target,msg,data,wait);
  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, 0, 0);
  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, 0, 0);
  124. }
  125. #ifdef CONFIG_XMON
  126. void smp_send_xmon_break(int cpu)
  127. {
  128. smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
  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, 0, 0);
  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;
  257. /* Fixup boot cpu */
  258. smp_store_cpu_info(smp_processor_id());
  259. cpu_callin_map[smp_processor_id()] = 1;
  260. smp_ops = ppc_md.smp_ops;
  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. for (i = 0; i < num_cpus; ++i)
  268. cpu_set(i, cpu_possible_map);
  269. /* Backup CPU 0 state */
  270. __save_cpu_setup();
  271. if (smp_ops->space_timers)
  272. smp_ops->space_timers(num_cpus);
  273. }
  274. void __devinit smp_prepare_boot_cpu(void)
  275. {
  276. cpu_set(smp_processor_id(), cpu_online_map);
  277. cpu_set(smp_processor_id(), cpu_possible_map);
  278. }
  279. int __init setup_profiling_timer(unsigned int multiplier)
  280. {
  281. return 0;
  282. }
  283. /* Processor coming up starts here */
  284. int __devinit start_secondary(void *unused)
  285. {
  286. int cpu;
  287. atomic_inc(&init_mm.mm_count);
  288. current->active_mm = &init_mm;
  289. cpu = smp_processor_id();
  290. smp_store_cpu_info(cpu);
  291. set_dec(tb_ticks_per_jiffy);
  292. cpu_callin_map[cpu] = 1;
  293. printk("CPU %i done callin...\n", cpu);
  294. smp_ops->setup_cpu(cpu);
  295. printk("CPU %i done setup...\n", cpu);
  296. local_irq_enable();
  297. smp_ops->take_timebase();
  298. printk("CPU %i done timebase take...\n", cpu);
  299. cpu_idle();
  300. return 0;
  301. }
  302. int __cpu_up(unsigned int cpu)
  303. {
  304. struct task_struct *p;
  305. char buf[32];
  306. int c;
  307. /* create a process for the processor */
  308. /* only regs.msr is actually used, and 0 is OK for it */
  309. p = fork_idle(cpu);
  310. if (IS_ERR(p))
  311. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  312. secondary_ti = p->thread_info;
  313. p->thread_info->cpu = cpu;
  314. /*
  315. * There was a cache flush loop here to flush the cache
  316. * to memory for the first 8MB of RAM. The cache flush
  317. * has been pushed into the kick_cpu function for those
  318. * platforms that need it.
  319. */
  320. /* wake up cpu */
  321. smp_ops->kick_cpu(cpu);
  322. /*
  323. * wait to see if the cpu made a callin (is actually up).
  324. * use this value that I found through experimentation.
  325. * -- Cort
  326. */
  327. for (c = 1000; c && !cpu_callin_map[cpu]; c--)
  328. udelay(100);
  329. if (!cpu_callin_map[cpu]) {
  330. sprintf(buf, "didn't find cpu %u", cpu);
  331. if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu);
  332. printk("Processor %u is stuck.\n", cpu);
  333. return -ENOENT;
  334. }
  335. sprintf(buf, "found cpu %u", cpu);
  336. if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu);
  337. printk("Processor %d found.\n", cpu);
  338. smp_ops->give_timebase();
  339. cpu_set(cpu, cpu_online_map);
  340. return 0;
  341. }
  342. void smp_cpus_done(unsigned int max_cpus)
  343. {
  344. smp_ops->setup_cpu(0);
  345. }