smp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * arch/sh/kernel/smp.c
  3. *
  4. * SMP support for the SuperH processors.
  5. *
  6. * Copyright (C) 2002 - 2007 Paul Mundt
  7. * Copyright (C) 2006 - 2007 Akio Idehara
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/cache.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/interrupt.h>
  22. #include <asm/atomic.h>
  23. #include <asm/processor.h>
  24. #include <asm/system.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/smp.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/sections.h>
  29. int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
  30. int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
  31. cpumask_t cpu_possible_map;
  32. EXPORT_SYMBOL(cpu_possible_map);
  33. cpumask_t cpu_online_map;
  34. EXPORT_SYMBOL(cpu_online_map);
  35. static inline void __init smp_store_cpu_info(unsigned int cpu)
  36. {
  37. struct sh_cpuinfo *c = cpu_data + cpu;
  38. c->loops_per_jiffy = loops_per_jiffy;
  39. }
  40. void __init smp_prepare_cpus(unsigned int max_cpus)
  41. {
  42. unsigned int cpu = smp_processor_id();
  43. init_new_context(current, &init_mm);
  44. current_thread_info()->cpu = cpu;
  45. plat_prepare_cpus(max_cpus);
  46. #ifndef CONFIG_HOTPLUG_CPU
  47. cpu_present_map = cpu_possible_map;
  48. #endif
  49. }
  50. void __devinit smp_prepare_boot_cpu(void)
  51. {
  52. unsigned int cpu = smp_processor_id();
  53. __cpu_number_map[0] = cpu;
  54. __cpu_logical_map[0] = cpu;
  55. cpu_set(cpu, cpu_online_map);
  56. cpu_set(cpu, cpu_possible_map);
  57. }
  58. asmlinkage void __cpuinit start_secondary(void)
  59. {
  60. unsigned int cpu;
  61. struct mm_struct *mm = &init_mm;
  62. atomic_inc(&mm->mm_count);
  63. atomic_inc(&mm->mm_users);
  64. current->active_mm = mm;
  65. BUG_ON(current->mm);
  66. enter_lazy_tlb(mm, current);
  67. per_cpu_trap_init();
  68. preempt_disable();
  69. local_irq_enable();
  70. calibrate_delay();
  71. cpu = smp_processor_id();
  72. smp_store_cpu_info(cpu);
  73. cpu_set(cpu, cpu_online_map);
  74. cpu_idle();
  75. }
  76. extern struct {
  77. unsigned long sp;
  78. unsigned long bss_start;
  79. unsigned long bss_end;
  80. void *start_kernel_fn;
  81. void *cpu_init_fn;
  82. void *thread_info;
  83. } stack_start;
  84. int __cpuinit __cpu_up(unsigned int cpu)
  85. {
  86. struct task_struct *tsk;
  87. unsigned long timeout;
  88. tsk = fork_idle(cpu);
  89. if (IS_ERR(tsk)) {
  90. printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
  91. return PTR_ERR(tsk);
  92. }
  93. /* Fill in data in head.S for secondary cpus */
  94. stack_start.sp = tsk->thread.sp;
  95. stack_start.thread_info = tsk->stack;
  96. stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
  97. stack_start.start_kernel_fn = start_secondary;
  98. flush_cache_all();
  99. plat_start_cpu(cpu, (unsigned long)_stext);
  100. timeout = jiffies + HZ;
  101. while (time_before(jiffies, timeout)) {
  102. if (cpu_online(cpu))
  103. break;
  104. udelay(10);
  105. }
  106. if (cpu_online(cpu))
  107. return 0;
  108. return -ENOENT;
  109. }
  110. void __init smp_cpus_done(unsigned int max_cpus)
  111. {
  112. unsigned long bogosum = 0;
  113. int cpu;
  114. for_each_online_cpu(cpu)
  115. bogosum += cpu_data[cpu].loops_per_jiffy;
  116. printk(KERN_INFO "SMP: Total of %d processors activated "
  117. "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
  118. bogosum / (500000/HZ),
  119. (bogosum / (5000/HZ)) % 100);
  120. }
  121. void smp_send_reschedule(int cpu)
  122. {
  123. plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
  124. }
  125. static void stop_this_cpu(void *unused)
  126. {
  127. cpu_clear(smp_processor_id(), cpu_online_map);
  128. local_irq_disable();
  129. for (;;)
  130. cpu_relax();
  131. }
  132. void smp_send_stop(void)
  133. {
  134. smp_call_function(stop_this_cpu, 0, 0);
  135. }
  136. void arch_send_call_function_ipi(cpumask_t mask)
  137. {
  138. int cpu;
  139. for_each_cpu_mask(cpu, mask)
  140. plat_send_ipi(cpu, SMP_MSG_FUNCTION);
  141. }
  142. void arch_send_call_function_single_ipi(int cpu)
  143. {
  144. plat_send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
  145. }
  146. /* Not really SMP stuff ... */
  147. int setup_profiling_timer(unsigned int multiplier)
  148. {
  149. return 0;
  150. }
  151. static void flush_tlb_all_ipi(void *info)
  152. {
  153. local_flush_tlb_all();
  154. }
  155. void flush_tlb_all(void)
  156. {
  157. on_each_cpu(flush_tlb_all_ipi, 0, 1);
  158. }
  159. static void flush_tlb_mm_ipi(void *mm)
  160. {
  161. local_flush_tlb_mm((struct mm_struct *)mm);
  162. }
  163. /*
  164. * The following tlb flush calls are invoked when old translations are
  165. * being torn down, or pte attributes are changing. For single threaded
  166. * address spaces, a new context is obtained on the current cpu, and tlb
  167. * context on other cpus are invalidated to force a new context allocation
  168. * at switch_mm time, should the mm ever be used on other cpus. For
  169. * multithreaded address spaces, intercpu interrupts have to be sent.
  170. * Another case where intercpu interrupts are required is when the target
  171. * mm might be active on another cpu (eg debuggers doing the flushes on
  172. * behalf of debugees, kswapd stealing pages from another process etc).
  173. * Kanoj 07/00.
  174. */
  175. void flush_tlb_mm(struct mm_struct *mm)
  176. {
  177. preempt_disable();
  178. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  179. smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
  180. } else {
  181. int i;
  182. for (i = 0; i < num_online_cpus(); i++)
  183. if (smp_processor_id() != i)
  184. cpu_context(i, mm) = 0;
  185. }
  186. local_flush_tlb_mm(mm);
  187. preempt_enable();
  188. }
  189. struct flush_tlb_data {
  190. struct vm_area_struct *vma;
  191. unsigned long addr1;
  192. unsigned long addr2;
  193. };
  194. static void flush_tlb_range_ipi(void *info)
  195. {
  196. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  197. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  198. }
  199. void flush_tlb_range(struct vm_area_struct *vma,
  200. unsigned long start, unsigned long end)
  201. {
  202. struct mm_struct *mm = vma->vm_mm;
  203. preempt_disable();
  204. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  205. struct flush_tlb_data fd;
  206. fd.vma = vma;
  207. fd.addr1 = start;
  208. fd.addr2 = end;
  209. smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
  210. } else {
  211. int i;
  212. for (i = 0; i < num_online_cpus(); i++)
  213. if (smp_processor_id() != i)
  214. cpu_context(i, mm) = 0;
  215. }
  216. local_flush_tlb_range(vma, start, end);
  217. preempt_enable();
  218. }
  219. static void flush_tlb_kernel_range_ipi(void *info)
  220. {
  221. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  222. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  223. }
  224. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  225. {
  226. struct flush_tlb_data fd;
  227. fd.addr1 = start;
  228. fd.addr2 = end;
  229. on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
  230. }
  231. static void flush_tlb_page_ipi(void *info)
  232. {
  233. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  234. local_flush_tlb_page(fd->vma, fd->addr1);
  235. }
  236. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  237. {
  238. preempt_disable();
  239. if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
  240. (current->mm != vma->vm_mm)) {
  241. struct flush_tlb_data fd;
  242. fd.vma = vma;
  243. fd.addr1 = page;
  244. smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
  245. } else {
  246. int i;
  247. for (i = 0; i < num_online_cpus(); i++)
  248. if (smp_processor_id() != i)
  249. cpu_context(i, vma->vm_mm) = 0;
  250. }
  251. local_flush_tlb_page(vma, page);
  252. preempt_enable();
  253. }
  254. static void flush_tlb_one_ipi(void *info)
  255. {
  256. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  257. local_flush_tlb_one(fd->addr1, fd->addr2);
  258. }
  259. void flush_tlb_one(unsigned long asid, unsigned long vaddr)
  260. {
  261. struct flush_tlb_data fd;
  262. fd.addr1 = asid;
  263. fd.addr2 = vaddr;
  264. smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
  265. local_flush_tlb_one(asid, vaddr);
  266. }