smp.c 8.3 KB

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