smp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/config.h"
  6. #include "linux/percpu.h"
  7. #include "asm/pgalloc.h"
  8. #include "asm/tlb.h"
  9. /* For some reason, mmu_gathers are referenced when CONFIG_SMP is off. */
  10. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  11. #ifdef CONFIG_SMP
  12. #include "linux/sched.h"
  13. #include "linux/module.h"
  14. #include "linux/threads.h"
  15. #include "linux/interrupt.h"
  16. #include "linux/err.h"
  17. #include "linux/hardirq.h"
  18. #include "asm/smp.h"
  19. #include "asm/processor.h"
  20. #include "asm/spinlock.h"
  21. #include "user_util.h"
  22. #include "kern_util.h"
  23. #include "kern.h"
  24. #include "irq_user.h"
  25. #include "os.h"
  26. /* CPU online map, set by smp_boot_cpus */
  27. cpumask_t cpu_online_map = CPU_MASK_NONE;
  28. cpumask_t cpu_possible_map = CPU_MASK_NONE;
  29. EXPORT_SYMBOL(cpu_online_map);
  30. EXPORT_SYMBOL(cpu_possible_map);
  31. /* Per CPU bogomips and other parameters
  32. * The only piece used here is the ipi pipe, which is set before SMP is
  33. * started and never changed.
  34. */
  35. struct cpuinfo_um cpu_data[NR_CPUS];
  36. /* A statistic, can be a little off */
  37. int num_reschedules_sent = 0;
  38. /* Not changed after boot */
  39. struct task_struct *idle_threads[NR_CPUS];
  40. void smp_send_reschedule(int cpu)
  41. {
  42. os_write_file(cpu_data[cpu].ipi_pipe[1], "R", 1);
  43. num_reschedules_sent++;
  44. }
  45. void smp_send_stop(void)
  46. {
  47. int i;
  48. printk(KERN_INFO "Stopping all CPUs...");
  49. for(i = 0; i < num_online_cpus(); i++){
  50. if(i == current_thread->cpu)
  51. continue;
  52. os_write_file(cpu_data[i].ipi_pipe[1], "S", 1);
  53. }
  54. printk("done\n");
  55. }
  56. static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
  57. static cpumask_t cpu_callin_map = CPU_MASK_NONE;
  58. static int idle_proc(void *cpup)
  59. {
  60. int cpu = (int) cpup, err;
  61. err = os_pipe(cpu_data[cpu].ipi_pipe, 1, 1);
  62. if(err < 0)
  63. panic("CPU#%d failed to create IPI pipe, err = %d", cpu, -err);
  64. activate_ipi(cpu_data[cpu].ipi_pipe[0],
  65. current->thread.mode.tt.extern_pid);
  66. wmb();
  67. if (cpu_test_and_set(cpu, cpu_callin_map)) {
  68. printk("huh, CPU#%d already present??\n", cpu);
  69. BUG();
  70. }
  71. while (!cpu_isset(cpu, smp_commenced_mask))
  72. cpu_relax();
  73. cpu_set(cpu, cpu_online_map);
  74. default_idle();
  75. return(0);
  76. }
  77. static struct task_struct *idle_thread(int cpu)
  78. {
  79. struct task_struct *new_task;
  80. unsigned char c;
  81. current->thread.request.u.thread.proc = idle_proc;
  82. current->thread.request.u.thread.arg = (void *) cpu;
  83. new_task = fork_idle(cpu);
  84. if(IS_ERR(new_task))
  85. panic("copy_process failed in idle_thread, error = %ld",
  86. PTR_ERR(new_task));
  87. cpu_tasks[cpu] = ((struct cpu_task)
  88. { .pid = new_task->thread.mode.tt.extern_pid,
  89. .task = new_task } );
  90. idle_threads[cpu] = new_task;
  91. CHOOSE_MODE(os_write_file(new_task->thread.mode.tt.switch_pipe[1], &c,
  92. sizeof(c)),
  93. ({ panic("skas mode doesn't support SMP"); }));
  94. return(new_task);
  95. }
  96. void smp_prepare_cpus(unsigned int maxcpus)
  97. {
  98. struct task_struct *idle;
  99. unsigned long waittime;
  100. int err, cpu, me = smp_processor_id();
  101. int i;
  102. for (i = 0; i < ncpus; ++i)
  103. cpu_set(i, cpu_possible_map);
  104. cpu_clear(me, cpu_online_map);
  105. cpu_set(me, cpu_online_map);
  106. cpu_set(me, cpu_callin_map);
  107. err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
  108. if(err < 0)
  109. panic("CPU#0 failed to create IPI pipe, errno = %d", -err);
  110. activate_ipi(cpu_data[me].ipi_pipe[0],
  111. current->thread.mode.tt.extern_pid);
  112. for(cpu = 1; cpu < ncpus; cpu++){
  113. printk("Booting processor %d...\n", cpu);
  114. idle = idle_thread(cpu);
  115. init_idle(idle, cpu);
  116. unhash_process(idle);
  117. waittime = 200000000;
  118. while (waittime-- && !cpu_isset(cpu, cpu_callin_map))
  119. cpu_relax();
  120. if (cpu_isset(cpu, cpu_callin_map))
  121. printk("done\n");
  122. else printk("failed\n");
  123. }
  124. }
  125. void smp_prepare_boot_cpu(void)
  126. {
  127. cpu_set(smp_processor_id(), cpu_online_map);
  128. }
  129. int __cpu_up(unsigned int cpu)
  130. {
  131. cpu_set(cpu, smp_commenced_mask);
  132. while (!cpu_isset(cpu, cpu_online_map))
  133. mb();
  134. return(0);
  135. }
  136. int setup_profiling_timer(unsigned int multiplier)
  137. {
  138. printk(KERN_INFO "setup_profiling_timer\n");
  139. return(0);
  140. }
  141. void smp_call_function_slave(int cpu);
  142. void IPI_handler(int cpu)
  143. {
  144. unsigned char c;
  145. int fd;
  146. fd = cpu_data[cpu].ipi_pipe[0];
  147. while (os_read_file(fd, &c, 1) == 1) {
  148. switch (c) {
  149. case 'C':
  150. smp_call_function_slave(cpu);
  151. break;
  152. case 'R':
  153. set_tsk_need_resched(current);
  154. break;
  155. case 'S':
  156. printk("CPU#%d stopping\n", cpu);
  157. while(1)
  158. pause();
  159. break;
  160. default:
  161. printk("CPU#%d received unknown IPI [%c]!\n", cpu, c);
  162. break;
  163. }
  164. }
  165. }
  166. int hard_smp_processor_id(void)
  167. {
  168. return(pid_to_processor_id(os_getpid()));
  169. }
  170. static DEFINE_SPINLOCK(call_lock);
  171. static atomic_t scf_started;
  172. static atomic_t scf_finished;
  173. static void (*func)(void *info);
  174. static void *info;
  175. void smp_call_function_slave(int cpu)
  176. {
  177. atomic_inc(&scf_started);
  178. (*func)(info);
  179. atomic_inc(&scf_finished);
  180. }
  181. int smp_call_function(void (*_func)(void *info), void *_info, int nonatomic,
  182. int wait)
  183. {
  184. int cpus = num_online_cpus() - 1;
  185. int i;
  186. if (!cpus)
  187. return 0;
  188. /* Can deadlock when called with interrupts disabled */
  189. WARN_ON(irqs_disabled());
  190. spin_lock_bh(&call_lock);
  191. atomic_set(&scf_started, 0);
  192. atomic_set(&scf_finished, 0);
  193. func = _func;
  194. info = _info;
  195. for_each_online_cpu(i)
  196. os_write_file(cpu_data[i].ipi_pipe[1], "C", 1);
  197. while (atomic_read(&scf_started) != cpus)
  198. barrier();
  199. if (wait)
  200. while (atomic_read(&scf_finished) != cpus)
  201. barrier();
  202. spin_unlock_bh(&call_lock);
  203. return 0;
  204. }
  205. #endif
  206. /*
  207. * Overrides for Emacs so that we follow Linus's tabbing style.
  208. * Emacs will notice this stuff at the end of the file and automatically
  209. * adjust the settings for this buffer only. This must remain at the end
  210. * of the file.
  211. * ---------------------------------------------------------------------------
  212. * Local variables:
  213. * c-file-style: "linux"
  214. * End:
  215. */