smp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <linux/sched.h>
  24. #include <linux/atomic.h>
  25. #include <asm/processor.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/smp.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/sections.h>
  30. #include <asm/setup.h>
  31. int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
  32. int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
  33. struct plat_smp_ops *mp_ops = NULL;
  34. /* State of each CPU */
  35. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  36. void register_smp_ops(struct plat_smp_ops *ops)
  37. {
  38. if (mp_ops)
  39. printk(KERN_WARNING "Overriding previously set SMP ops\n");
  40. mp_ops = ops;
  41. }
  42. static inline void smp_store_cpu_info(unsigned int cpu)
  43. {
  44. struct sh_cpuinfo *c = cpu_data + cpu;
  45. memcpy(c, &boot_cpu_data, sizeof(struct sh_cpuinfo));
  46. c->loops_per_jiffy = loops_per_jiffy;
  47. }
  48. void __init smp_prepare_cpus(unsigned int max_cpus)
  49. {
  50. unsigned int cpu = smp_processor_id();
  51. init_new_context(current, &init_mm);
  52. current_thread_info()->cpu = cpu;
  53. mp_ops->prepare_cpus(max_cpus);
  54. #ifndef CONFIG_HOTPLUG_CPU
  55. init_cpu_present(cpu_possible_mask);
  56. #endif
  57. }
  58. void __init smp_prepare_boot_cpu(void)
  59. {
  60. unsigned int cpu = smp_processor_id();
  61. __cpu_number_map[0] = cpu;
  62. __cpu_logical_map[0] = cpu;
  63. set_cpu_online(cpu, true);
  64. set_cpu_possible(cpu, true);
  65. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  66. }
  67. #ifdef CONFIG_HOTPLUG_CPU
  68. void native_cpu_die(unsigned int cpu)
  69. {
  70. unsigned int i;
  71. for (i = 0; i < 10; i++) {
  72. smp_rmb();
  73. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  74. if (system_state == SYSTEM_RUNNING)
  75. pr_info("CPU %u is now offline\n", cpu);
  76. return;
  77. }
  78. msleep(100);
  79. }
  80. pr_err("CPU %u didn't die...\n", cpu);
  81. }
  82. int native_cpu_disable(unsigned int cpu)
  83. {
  84. return cpu == 0 ? -EPERM : 0;
  85. }
  86. void play_dead_common(void)
  87. {
  88. idle_task_exit();
  89. irq_ctx_exit(raw_smp_processor_id());
  90. mb();
  91. __this_cpu_write(cpu_state, CPU_DEAD);
  92. local_irq_disable();
  93. }
  94. void native_play_dead(void)
  95. {
  96. play_dead_common();
  97. }
  98. int __cpu_disable(void)
  99. {
  100. unsigned int cpu = smp_processor_id();
  101. int ret;
  102. ret = mp_ops->cpu_disable(cpu);
  103. if (ret)
  104. return ret;
  105. /*
  106. * Take this CPU offline. Once we clear this, we can't return,
  107. * and we must not schedule until we're ready to give up the cpu.
  108. */
  109. set_cpu_online(cpu, false);
  110. /*
  111. * OK - migrate IRQs away from this CPU
  112. */
  113. migrate_irqs();
  114. /*
  115. * Stop the local timer for this CPU.
  116. */
  117. local_timer_stop(cpu);
  118. /*
  119. * Flush user cache and TLB mappings, and then remove this CPU
  120. * from the vm mask set of all processes.
  121. */
  122. flush_cache_all();
  123. local_flush_tlb_all();
  124. clear_tasks_mm_cpumask(cpu);
  125. return 0;
  126. }
  127. #else /* ... !CONFIG_HOTPLUG_CPU */
  128. int native_cpu_disable(unsigned int cpu)
  129. {
  130. return -ENOSYS;
  131. }
  132. void native_cpu_die(unsigned int cpu)
  133. {
  134. /* We said "no" in __cpu_disable */
  135. BUG();
  136. }
  137. void native_play_dead(void)
  138. {
  139. BUG();
  140. }
  141. #endif
  142. asmlinkage void start_secondary(void)
  143. {
  144. unsigned int cpu = smp_processor_id();
  145. struct mm_struct *mm = &init_mm;
  146. enable_mmu();
  147. atomic_inc(&mm->mm_count);
  148. atomic_inc(&mm->mm_users);
  149. current->active_mm = mm;
  150. enter_lazy_tlb(mm, current);
  151. local_flush_tlb_all();
  152. per_cpu_trap_init();
  153. preempt_disable();
  154. notify_cpu_starting(cpu);
  155. local_irq_enable();
  156. /* Enable local timers */
  157. local_timer_setup(cpu);
  158. calibrate_delay();
  159. smp_store_cpu_info(cpu);
  160. set_cpu_online(cpu, true);
  161. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  162. cpu_startup_entry(CPUHP_ONLINE);
  163. }
  164. extern struct {
  165. unsigned long sp;
  166. unsigned long bss_start;
  167. unsigned long bss_end;
  168. void *start_kernel_fn;
  169. void *cpu_init_fn;
  170. void *thread_info;
  171. } stack_start;
  172. int __cpu_up(unsigned int cpu, struct task_struct *tsk)
  173. {
  174. unsigned long timeout;
  175. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  176. /* Fill in data in head.S for secondary cpus */
  177. stack_start.sp = tsk->thread.sp;
  178. stack_start.thread_info = tsk->stack;
  179. stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
  180. stack_start.start_kernel_fn = start_secondary;
  181. flush_icache_range((unsigned long)&stack_start,
  182. (unsigned long)&stack_start + sizeof(stack_start));
  183. wmb();
  184. mp_ops->start_cpu(cpu, (unsigned long)_stext);
  185. timeout = jiffies + HZ;
  186. while (time_before(jiffies, timeout)) {
  187. if (cpu_online(cpu))
  188. break;
  189. udelay(10);
  190. barrier();
  191. }
  192. if (cpu_online(cpu))
  193. return 0;
  194. return -ENOENT;
  195. }
  196. void __init smp_cpus_done(unsigned int max_cpus)
  197. {
  198. unsigned long bogosum = 0;
  199. int cpu;
  200. for_each_online_cpu(cpu)
  201. bogosum += cpu_data[cpu].loops_per_jiffy;
  202. printk(KERN_INFO "SMP: Total of %d processors activated "
  203. "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
  204. bogosum / (500000/HZ),
  205. (bogosum / (5000/HZ)) % 100);
  206. }
  207. void smp_send_reschedule(int cpu)
  208. {
  209. mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
  210. }
  211. void smp_send_stop(void)
  212. {
  213. smp_call_function(stop_this_cpu, 0, 0);
  214. }
  215. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  216. {
  217. int cpu;
  218. for_each_cpu(cpu, mask)
  219. mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
  220. }
  221. void arch_send_call_function_single_ipi(int cpu)
  222. {
  223. mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
  224. }
  225. void smp_timer_broadcast(const struct cpumask *mask)
  226. {
  227. int cpu;
  228. for_each_cpu(cpu, mask)
  229. mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
  230. }
  231. static void ipi_timer(void)
  232. {
  233. irq_enter();
  234. local_timer_interrupt();
  235. irq_exit();
  236. }
  237. void smp_message_recv(unsigned int msg)
  238. {
  239. switch (msg) {
  240. case SMP_MSG_FUNCTION:
  241. generic_smp_call_function_interrupt();
  242. break;
  243. case SMP_MSG_RESCHEDULE:
  244. scheduler_ipi();
  245. break;
  246. case SMP_MSG_FUNCTION_SINGLE:
  247. generic_smp_call_function_single_interrupt();
  248. break;
  249. case SMP_MSG_TIMER:
  250. ipi_timer();
  251. break;
  252. default:
  253. printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
  254. smp_processor_id(), __func__, msg);
  255. break;
  256. }
  257. }
  258. /* Not really SMP stuff ... */
  259. int setup_profiling_timer(unsigned int multiplier)
  260. {
  261. return 0;
  262. }
  263. static void flush_tlb_all_ipi(void *info)
  264. {
  265. local_flush_tlb_all();
  266. }
  267. void flush_tlb_all(void)
  268. {
  269. on_each_cpu(flush_tlb_all_ipi, 0, 1);
  270. }
  271. static void flush_tlb_mm_ipi(void *mm)
  272. {
  273. local_flush_tlb_mm((struct mm_struct *)mm);
  274. }
  275. /*
  276. * The following tlb flush calls are invoked when old translations are
  277. * being torn down, or pte attributes are changing. For single threaded
  278. * address spaces, a new context is obtained on the current cpu, and tlb
  279. * context on other cpus are invalidated to force a new context allocation
  280. * at switch_mm time, should the mm ever be used on other cpus. For
  281. * multithreaded address spaces, intercpu interrupts have to be sent.
  282. * Another case where intercpu interrupts are required is when the target
  283. * mm might be active on another cpu (eg debuggers doing the flushes on
  284. * behalf of debugees, kswapd stealing pages from another process etc).
  285. * Kanoj 07/00.
  286. */
  287. void flush_tlb_mm(struct mm_struct *mm)
  288. {
  289. preempt_disable();
  290. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  291. smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
  292. } else {
  293. int i;
  294. for_each_online_cpu(i)
  295. if (smp_processor_id() != i)
  296. cpu_context(i, mm) = 0;
  297. }
  298. local_flush_tlb_mm(mm);
  299. preempt_enable();
  300. }
  301. struct flush_tlb_data {
  302. struct vm_area_struct *vma;
  303. unsigned long addr1;
  304. unsigned long addr2;
  305. };
  306. static void flush_tlb_range_ipi(void *info)
  307. {
  308. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  309. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  310. }
  311. void flush_tlb_range(struct vm_area_struct *vma,
  312. unsigned long start, unsigned long end)
  313. {
  314. struct mm_struct *mm = vma->vm_mm;
  315. preempt_disable();
  316. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  317. struct flush_tlb_data fd;
  318. fd.vma = vma;
  319. fd.addr1 = start;
  320. fd.addr2 = end;
  321. smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
  322. } else {
  323. int i;
  324. for_each_online_cpu(i)
  325. if (smp_processor_id() != i)
  326. cpu_context(i, mm) = 0;
  327. }
  328. local_flush_tlb_range(vma, start, end);
  329. preempt_enable();
  330. }
  331. static void flush_tlb_kernel_range_ipi(void *info)
  332. {
  333. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  334. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  335. }
  336. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  337. {
  338. struct flush_tlb_data fd;
  339. fd.addr1 = start;
  340. fd.addr2 = end;
  341. on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
  342. }
  343. static void flush_tlb_page_ipi(void *info)
  344. {
  345. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  346. local_flush_tlb_page(fd->vma, fd->addr1);
  347. }
  348. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  349. {
  350. preempt_disable();
  351. if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
  352. (current->mm != vma->vm_mm)) {
  353. struct flush_tlb_data fd;
  354. fd.vma = vma;
  355. fd.addr1 = page;
  356. smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
  357. } else {
  358. int i;
  359. for_each_online_cpu(i)
  360. if (smp_processor_id() != i)
  361. cpu_context(i, vma->vm_mm) = 0;
  362. }
  363. local_flush_tlb_page(vma, page);
  364. preempt_enable();
  365. }
  366. static void flush_tlb_one_ipi(void *info)
  367. {
  368. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  369. local_flush_tlb_one(fd->addr1, fd->addr2);
  370. }
  371. void flush_tlb_one(unsigned long asid, unsigned long vaddr)
  372. {
  373. struct flush_tlb_data fd;
  374. fd.addr1 = asid;
  375. fd.addr2 = vaddr;
  376. smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
  377. local_flush_tlb_one(asid, vaddr);
  378. }