smp.c 7.9 KB

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