smp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 __cpuinit 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 __cpuinit 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. __get_cpu_var(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. struct task_struct *p;
  102. int ret;
  103. ret = mp_ops->cpu_disable(cpu);
  104. if (ret)
  105. return ret;
  106. /*
  107. * Take this CPU offline. Once we clear this, we can't return,
  108. * and we must not schedule until we're ready to give up the cpu.
  109. */
  110. set_cpu_online(cpu, false);
  111. /*
  112. * OK - migrate IRQs away from this CPU
  113. */
  114. migrate_irqs();
  115. /*
  116. * Stop the local timer for this CPU.
  117. */
  118. local_timer_stop(cpu);
  119. /*
  120. * Flush user cache and TLB mappings, and then remove this CPU
  121. * from the vm mask set of all processes.
  122. */
  123. flush_cache_all();
  124. local_flush_tlb_all();
  125. read_lock(&tasklist_lock);
  126. for_each_process(p)
  127. if (p->mm)
  128. cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
  129. read_unlock(&tasklist_lock);
  130. return 0;
  131. }
  132. #else /* ... !CONFIG_HOTPLUG_CPU */
  133. int native_cpu_disable(unsigned int cpu)
  134. {
  135. return -ENOSYS;
  136. }
  137. void native_cpu_die(unsigned int cpu)
  138. {
  139. /* We said "no" in __cpu_disable */
  140. BUG();
  141. }
  142. void native_play_dead(void)
  143. {
  144. BUG();
  145. }
  146. #endif
  147. asmlinkage void __cpuinit start_secondary(void)
  148. {
  149. unsigned int cpu = smp_processor_id();
  150. struct mm_struct *mm = &init_mm;
  151. enable_mmu();
  152. atomic_inc(&mm->mm_count);
  153. atomic_inc(&mm->mm_users);
  154. current->active_mm = mm;
  155. enter_lazy_tlb(mm, current);
  156. local_flush_tlb_all();
  157. per_cpu_trap_init();
  158. preempt_disable();
  159. notify_cpu_starting(cpu);
  160. local_irq_enable();
  161. /* Enable local timers */
  162. local_timer_setup(cpu);
  163. calibrate_delay();
  164. smp_store_cpu_info(cpu);
  165. set_cpu_online(cpu, true);
  166. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  167. cpu_idle();
  168. }
  169. extern struct {
  170. unsigned long sp;
  171. unsigned long bss_start;
  172. unsigned long bss_end;
  173. void *start_kernel_fn;
  174. void *cpu_init_fn;
  175. void *thread_info;
  176. } stack_start;
  177. int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tsk)
  178. {
  179. unsigned long timeout;
  180. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  181. /* Fill in data in head.S for secondary cpus */
  182. stack_start.sp = tsk->thread.sp;
  183. stack_start.thread_info = tsk->stack;
  184. stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
  185. stack_start.start_kernel_fn = start_secondary;
  186. flush_icache_range((unsigned long)&stack_start,
  187. (unsigned long)&stack_start + sizeof(stack_start));
  188. wmb();
  189. mp_ops->start_cpu(cpu, (unsigned long)_stext);
  190. timeout = jiffies + HZ;
  191. while (time_before(jiffies, timeout)) {
  192. if (cpu_online(cpu))
  193. break;
  194. udelay(10);
  195. barrier();
  196. }
  197. if (cpu_online(cpu))
  198. return 0;
  199. return -ENOENT;
  200. }
  201. void __init smp_cpus_done(unsigned int max_cpus)
  202. {
  203. unsigned long bogosum = 0;
  204. int cpu;
  205. for_each_online_cpu(cpu)
  206. bogosum += cpu_data[cpu].loops_per_jiffy;
  207. printk(KERN_INFO "SMP: Total of %d processors activated "
  208. "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
  209. bogosum / (500000/HZ),
  210. (bogosum / (5000/HZ)) % 100);
  211. }
  212. void smp_send_reschedule(int cpu)
  213. {
  214. mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
  215. }
  216. void smp_send_stop(void)
  217. {
  218. smp_call_function(stop_this_cpu, 0, 0);
  219. }
  220. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  221. {
  222. int cpu;
  223. for_each_cpu(cpu, mask)
  224. mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
  225. }
  226. void arch_send_call_function_single_ipi(int cpu)
  227. {
  228. mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
  229. }
  230. void smp_timer_broadcast(const struct cpumask *mask)
  231. {
  232. int cpu;
  233. for_each_cpu(cpu, mask)
  234. mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
  235. }
  236. static void ipi_timer(void)
  237. {
  238. irq_enter();
  239. local_timer_interrupt();
  240. irq_exit();
  241. }
  242. void smp_message_recv(unsigned int msg)
  243. {
  244. switch (msg) {
  245. case SMP_MSG_FUNCTION:
  246. generic_smp_call_function_interrupt();
  247. break;
  248. case SMP_MSG_RESCHEDULE:
  249. scheduler_ipi();
  250. break;
  251. case SMP_MSG_FUNCTION_SINGLE:
  252. generic_smp_call_function_single_interrupt();
  253. break;
  254. case SMP_MSG_TIMER:
  255. ipi_timer();
  256. break;
  257. default:
  258. printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
  259. smp_processor_id(), __func__, msg);
  260. break;
  261. }
  262. }
  263. /* Not really SMP stuff ... */
  264. int setup_profiling_timer(unsigned int multiplier)
  265. {
  266. return 0;
  267. }
  268. static void flush_tlb_all_ipi(void *info)
  269. {
  270. local_flush_tlb_all();
  271. }
  272. void flush_tlb_all(void)
  273. {
  274. on_each_cpu(flush_tlb_all_ipi, 0, 1);
  275. }
  276. static void flush_tlb_mm_ipi(void *mm)
  277. {
  278. local_flush_tlb_mm((struct mm_struct *)mm);
  279. }
  280. /*
  281. * The following tlb flush calls are invoked when old translations are
  282. * being torn down, or pte attributes are changing. For single threaded
  283. * address spaces, a new context is obtained on the current cpu, and tlb
  284. * context on other cpus are invalidated to force a new context allocation
  285. * at switch_mm time, should the mm ever be used on other cpus. For
  286. * multithreaded address spaces, intercpu interrupts have to be sent.
  287. * Another case where intercpu interrupts are required is when the target
  288. * mm might be active on another cpu (eg debuggers doing the flushes on
  289. * behalf of debugees, kswapd stealing pages from another process etc).
  290. * Kanoj 07/00.
  291. */
  292. void flush_tlb_mm(struct mm_struct *mm)
  293. {
  294. preempt_disable();
  295. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  296. smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
  297. } else {
  298. int i;
  299. for (i = 0; i < num_online_cpus(); i++)
  300. if (smp_processor_id() != i)
  301. cpu_context(i, mm) = 0;
  302. }
  303. local_flush_tlb_mm(mm);
  304. preempt_enable();
  305. }
  306. struct flush_tlb_data {
  307. struct vm_area_struct *vma;
  308. unsigned long addr1;
  309. unsigned long addr2;
  310. };
  311. static void flush_tlb_range_ipi(void *info)
  312. {
  313. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  314. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  315. }
  316. void flush_tlb_range(struct vm_area_struct *vma,
  317. unsigned long start, unsigned long end)
  318. {
  319. struct mm_struct *mm = vma->vm_mm;
  320. preempt_disable();
  321. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  322. struct flush_tlb_data fd;
  323. fd.vma = vma;
  324. fd.addr1 = start;
  325. fd.addr2 = end;
  326. smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
  327. } else {
  328. int i;
  329. for (i = 0; i < num_online_cpus(); i++)
  330. if (smp_processor_id() != i)
  331. cpu_context(i, mm) = 0;
  332. }
  333. local_flush_tlb_range(vma, start, end);
  334. preempt_enable();
  335. }
  336. static void flush_tlb_kernel_range_ipi(void *info)
  337. {
  338. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  339. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  340. }
  341. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  342. {
  343. struct flush_tlb_data fd;
  344. fd.addr1 = start;
  345. fd.addr2 = end;
  346. on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
  347. }
  348. static void flush_tlb_page_ipi(void *info)
  349. {
  350. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  351. local_flush_tlb_page(fd->vma, fd->addr1);
  352. }
  353. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  354. {
  355. preempt_disable();
  356. if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
  357. (current->mm != vma->vm_mm)) {
  358. struct flush_tlb_data fd;
  359. fd.vma = vma;
  360. fd.addr1 = page;
  361. smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
  362. } else {
  363. int i;
  364. for (i = 0; i < num_online_cpus(); i++)
  365. if (smp_processor_id() != i)
  366. cpu_context(i, vma->vm_mm) = 0;
  367. }
  368. local_flush_tlb_page(vma, page);
  369. preempt_enable();
  370. }
  371. static void flush_tlb_one_ipi(void *info)
  372. {
  373. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  374. local_flush_tlb_one(fd->addr1, fd->addr2);
  375. }
  376. void flush_tlb_one(unsigned long asid, unsigned long vaddr)
  377. {
  378. struct flush_tlb_data fd;
  379. fd.addr1 = asid;
  380. fd.addr2 = vaddr;
  381. smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
  382. local_flush_tlb_one(asid, vaddr);
  383. }