smp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 atomic_t cpus_booted = ATOMIC_INIT(0);
  36. /*
  37. * Run specified function on a particular processor.
  38. */
  39. void __smp_call_function(unsigned int cpu);
  40. static inline void __init smp_store_cpu_info(unsigned int cpu)
  41. {
  42. struct sh_cpuinfo *c = cpu_data + cpu;
  43. c->loops_per_jiffy = loops_per_jiffy;
  44. }
  45. void __init smp_prepare_cpus(unsigned int max_cpus)
  46. {
  47. unsigned int cpu = smp_processor_id();
  48. init_new_context(current, &init_mm);
  49. current_thread_info()->cpu = cpu;
  50. plat_prepare_cpus(max_cpus);
  51. #ifndef CONFIG_HOTPLUG_CPU
  52. cpu_present_map = cpu_possible_map;
  53. #endif
  54. }
  55. void __devinit smp_prepare_boot_cpu(void)
  56. {
  57. unsigned int cpu = smp_processor_id();
  58. __cpu_number_map[0] = cpu;
  59. __cpu_logical_map[0] = cpu;
  60. cpu_set(cpu, cpu_online_map);
  61. cpu_set(cpu, cpu_possible_map);
  62. }
  63. asmlinkage void __cpuinit start_secondary(void)
  64. {
  65. unsigned int cpu;
  66. struct mm_struct *mm = &init_mm;
  67. atomic_inc(&mm->mm_count);
  68. atomic_inc(&mm->mm_users);
  69. current->active_mm = mm;
  70. BUG_ON(current->mm);
  71. enter_lazy_tlb(mm, current);
  72. per_cpu_trap_init();
  73. preempt_disable();
  74. local_irq_enable();
  75. calibrate_delay();
  76. cpu = smp_processor_id();
  77. smp_store_cpu_info(cpu);
  78. cpu_set(cpu, cpu_online_map);
  79. cpu_idle();
  80. }
  81. extern struct {
  82. unsigned long sp;
  83. unsigned long bss_start;
  84. unsigned long bss_end;
  85. void *start_kernel_fn;
  86. void *cpu_init_fn;
  87. void *thread_info;
  88. } stack_start;
  89. int __cpuinit __cpu_up(unsigned int cpu)
  90. {
  91. struct task_struct *tsk;
  92. unsigned long timeout;
  93. tsk = fork_idle(cpu);
  94. if (IS_ERR(tsk)) {
  95. printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
  96. return PTR_ERR(tsk);
  97. }
  98. /* Fill in data in head.S for secondary cpus */
  99. stack_start.sp = tsk->thread.sp;
  100. stack_start.thread_info = tsk->stack;
  101. stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
  102. stack_start.start_kernel_fn = start_secondary;
  103. flush_cache_all();
  104. plat_start_cpu(cpu, (unsigned long)_stext);
  105. timeout = jiffies + HZ;
  106. while (time_before(jiffies, timeout)) {
  107. if (cpu_online(cpu))
  108. break;
  109. udelay(10);
  110. }
  111. if (cpu_online(cpu))
  112. return 0;
  113. return -ENOENT;
  114. }
  115. void __init smp_cpus_done(unsigned int max_cpus)
  116. {
  117. unsigned long bogosum = 0;
  118. int cpu;
  119. for_each_online_cpu(cpu)
  120. bogosum += cpu_data[cpu].loops_per_jiffy;
  121. printk(KERN_INFO "SMP: Total of %d processors activated "
  122. "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
  123. bogosum / (500000/HZ),
  124. (bogosum / (5000/HZ)) % 100);
  125. }
  126. void smp_send_reschedule(int cpu)
  127. {
  128. plat_send_ipi(cpu, SMP_MSG_RESCHEDULE);
  129. }
  130. static void stop_this_cpu(void *unused)
  131. {
  132. cpu_clear(smp_processor_id(), cpu_online_map);
  133. local_irq_disable();
  134. for (;;)
  135. cpu_relax();
  136. }
  137. void smp_send_stop(void)
  138. {
  139. smp_call_function(stop_this_cpu, 0, 1, 0);
  140. }
  141. struct smp_fn_call_struct smp_fn_call = {
  142. .lock = SPIN_LOCK_UNLOCKED,
  143. .finished = ATOMIC_INIT(0),
  144. };
  145. /*
  146. * The caller of this wants the passed function to run on every cpu. If wait
  147. * is set, wait until all cpus have finished the function before returning.
  148. * The lock is here to protect the call structure.
  149. * You must not call this function with disabled interrupts or from a
  150. * hardware interrupt handler or from a bottom half handler.
  151. */
  152. int smp_call_function(void (*func)(void *info), void *info, int retry, int wait)
  153. {
  154. unsigned int nr_cpus = atomic_read(&cpus_booted);
  155. int i;
  156. /* Can deadlock when called with interrupts disabled */
  157. WARN_ON(irqs_disabled());
  158. spin_lock(&smp_fn_call.lock);
  159. atomic_set(&smp_fn_call.finished, 0);
  160. smp_fn_call.fn = func;
  161. smp_fn_call.data = info;
  162. for (i = 0; i < nr_cpus; i++)
  163. if (i != smp_processor_id())
  164. plat_send_ipi(i, SMP_MSG_FUNCTION);
  165. if (wait)
  166. while (atomic_read(&smp_fn_call.finished) != (nr_cpus - 1));
  167. spin_unlock(&smp_fn_call.lock);
  168. return 0;
  169. }
  170. /* Not really SMP stuff ... */
  171. int setup_profiling_timer(unsigned int multiplier)
  172. {
  173. return 0;
  174. }
  175. static void flush_tlb_all_ipi(void *info)
  176. {
  177. local_flush_tlb_all();
  178. }
  179. void flush_tlb_all(void)
  180. {
  181. on_each_cpu(flush_tlb_all_ipi, 0, 1, 1);
  182. }
  183. static void flush_tlb_mm_ipi(void *mm)
  184. {
  185. local_flush_tlb_mm((struct mm_struct *)mm);
  186. }
  187. /*
  188. * The following tlb flush calls are invoked when old translations are
  189. * being torn down, or pte attributes are changing. For single threaded
  190. * address spaces, a new context is obtained on the current cpu, and tlb
  191. * context on other cpus are invalidated to force a new context allocation
  192. * at switch_mm time, should the mm ever be used on other cpus. For
  193. * multithreaded address spaces, intercpu interrupts have to be sent.
  194. * Another case where intercpu interrupts are required is when the target
  195. * mm might be active on another cpu (eg debuggers doing the flushes on
  196. * behalf of debugees, kswapd stealing pages from another process etc).
  197. * Kanoj 07/00.
  198. */
  199. void flush_tlb_mm(struct mm_struct *mm)
  200. {
  201. preempt_disable();
  202. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  203. smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1, 1);
  204. } else {
  205. int i;
  206. for (i = 0; i < num_online_cpus(); i++)
  207. if (smp_processor_id() != i)
  208. cpu_context(i, mm) = 0;
  209. }
  210. local_flush_tlb_mm(mm);
  211. preempt_enable();
  212. }
  213. struct flush_tlb_data {
  214. struct vm_area_struct *vma;
  215. unsigned long addr1;
  216. unsigned long addr2;
  217. };
  218. static void flush_tlb_range_ipi(void *info)
  219. {
  220. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  221. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  222. }
  223. void flush_tlb_range(struct vm_area_struct *vma,
  224. unsigned long start, unsigned long end)
  225. {
  226. struct mm_struct *mm = vma->vm_mm;
  227. preempt_disable();
  228. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  229. struct flush_tlb_data fd;
  230. fd.vma = vma;
  231. fd.addr1 = start;
  232. fd.addr2 = end;
  233. smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1, 1);
  234. } else {
  235. int i;
  236. for (i = 0; i < num_online_cpus(); i++)
  237. if (smp_processor_id() != i)
  238. cpu_context(i, mm) = 0;
  239. }
  240. local_flush_tlb_range(vma, start, end);
  241. preempt_enable();
  242. }
  243. static void flush_tlb_kernel_range_ipi(void *info)
  244. {
  245. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  246. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  247. }
  248. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  249. {
  250. struct flush_tlb_data fd;
  251. fd.addr1 = start;
  252. fd.addr2 = end;
  253. on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1, 1);
  254. }
  255. static void flush_tlb_page_ipi(void *info)
  256. {
  257. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  258. local_flush_tlb_page(fd->vma, fd->addr1);
  259. }
  260. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  261. {
  262. preempt_disable();
  263. if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
  264. (current->mm != vma->vm_mm)) {
  265. struct flush_tlb_data fd;
  266. fd.vma = vma;
  267. fd.addr1 = page;
  268. smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1, 1);
  269. } else {
  270. int i;
  271. for (i = 0; i < num_online_cpus(); i++)
  272. if (smp_processor_id() != i)
  273. cpu_context(i, vma->vm_mm) = 0;
  274. }
  275. local_flush_tlb_page(vma, page);
  276. preempt_enable();
  277. }
  278. static void flush_tlb_one_ipi(void *info)
  279. {
  280. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  281. local_flush_tlb_one(fd->addr1, fd->addr2);
  282. }
  283. void flush_tlb_one(unsigned long asid, unsigned long vaddr)
  284. {
  285. struct flush_tlb_data fd;
  286. fd.addr1 = asid;
  287. fd.addr2 = vaddr;
  288. smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1, 1);
  289. local_flush_tlb_one(asid, vaddr);
  290. }