smp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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)
  178. {
  179. struct task_struct *tsk;
  180. unsigned long timeout;
  181. tsk = cpu_data[cpu].idle;
  182. if (!tsk) {
  183. tsk = fork_idle(cpu);
  184. if (IS_ERR(tsk)) {
  185. pr_err("Failed forking idle task for cpu %d\n", cpu);
  186. return PTR_ERR(tsk);
  187. }
  188. cpu_data[cpu].idle = tsk;
  189. }
  190. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  191. /* Fill in data in head.S for secondary cpus */
  192. stack_start.sp = tsk->thread.sp;
  193. stack_start.thread_info = tsk->stack;
  194. stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
  195. stack_start.start_kernel_fn = start_secondary;
  196. flush_icache_range((unsigned long)&stack_start,
  197. (unsigned long)&stack_start + sizeof(stack_start));
  198. wmb();
  199. mp_ops->start_cpu(cpu, (unsigned long)_stext);
  200. timeout = jiffies + HZ;
  201. while (time_before(jiffies, timeout)) {
  202. if (cpu_online(cpu))
  203. break;
  204. udelay(10);
  205. barrier();
  206. }
  207. if (cpu_online(cpu))
  208. return 0;
  209. return -ENOENT;
  210. }
  211. void __init smp_cpus_done(unsigned int max_cpus)
  212. {
  213. unsigned long bogosum = 0;
  214. int cpu;
  215. for_each_online_cpu(cpu)
  216. bogosum += cpu_data[cpu].loops_per_jiffy;
  217. printk(KERN_INFO "SMP: Total of %d processors activated "
  218. "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
  219. bogosum / (500000/HZ),
  220. (bogosum / (5000/HZ)) % 100);
  221. }
  222. void smp_send_reschedule(int cpu)
  223. {
  224. mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
  225. }
  226. void smp_send_stop(void)
  227. {
  228. smp_call_function(stop_this_cpu, 0, 0);
  229. }
  230. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  231. {
  232. int cpu;
  233. for_each_cpu(cpu, mask)
  234. mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
  235. }
  236. void arch_send_call_function_single_ipi(int cpu)
  237. {
  238. mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
  239. }
  240. void smp_timer_broadcast(const struct cpumask *mask)
  241. {
  242. int cpu;
  243. for_each_cpu(cpu, mask)
  244. mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
  245. }
  246. static void ipi_timer(void)
  247. {
  248. irq_enter();
  249. local_timer_interrupt();
  250. irq_exit();
  251. }
  252. void smp_message_recv(unsigned int msg)
  253. {
  254. switch (msg) {
  255. case SMP_MSG_FUNCTION:
  256. generic_smp_call_function_interrupt();
  257. break;
  258. case SMP_MSG_RESCHEDULE:
  259. scheduler_ipi();
  260. break;
  261. case SMP_MSG_FUNCTION_SINGLE:
  262. generic_smp_call_function_single_interrupt();
  263. break;
  264. case SMP_MSG_TIMER:
  265. ipi_timer();
  266. break;
  267. default:
  268. printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
  269. smp_processor_id(), __func__, msg);
  270. break;
  271. }
  272. }
  273. /* Not really SMP stuff ... */
  274. int setup_profiling_timer(unsigned int multiplier)
  275. {
  276. return 0;
  277. }
  278. static void flush_tlb_all_ipi(void *info)
  279. {
  280. local_flush_tlb_all();
  281. }
  282. void flush_tlb_all(void)
  283. {
  284. on_each_cpu(flush_tlb_all_ipi, 0, 1);
  285. }
  286. static void flush_tlb_mm_ipi(void *mm)
  287. {
  288. local_flush_tlb_mm((struct mm_struct *)mm);
  289. }
  290. /*
  291. * The following tlb flush calls are invoked when old translations are
  292. * being torn down, or pte attributes are changing. For single threaded
  293. * address spaces, a new context is obtained on the current cpu, and tlb
  294. * context on other cpus are invalidated to force a new context allocation
  295. * at switch_mm time, should the mm ever be used on other cpus. For
  296. * multithreaded address spaces, intercpu interrupts have to be sent.
  297. * Another case where intercpu interrupts are required is when the target
  298. * mm might be active on another cpu (eg debuggers doing the flushes on
  299. * behalf of debugees, kswapd stealing pages from another process etc).
  300. * Kanoj 07/00.
  301. */
  302. void flush_tlb_mm(struct mm_struct *mm)
  303. {
  304. preempt_disable();
  305. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  306. smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
  307. } else {
  308. int i;
  309. for (i = 0; i < num_online_cpus(); i++)
  310. if (smp_processor_id() != i)
  311. cpu_context(i, mm) = 0;
  312. }
  313. local_flush_tlb_mm(mm);
  314. preempt_enable();
  315. }
  316. struct flush_tlb_data {
  317. struct vm_area_struct *vma;
  318. unsigned long addr1;
  319. unsigned long addr2;
  320. };
  321. static void flush_tlb_range_ipi(void *info)
  322. {
  323. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  324. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  325. }
  326. void flush_tlb_range(struct vm_area_struct *vma,
  327. unsigned long start, unsigned long end)
  328. {
  329. struct mm_struct *mm = vma->vm_mm;
  330. preempt_disable();
  331. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  332. struct flush_tlb_data fd;
  333. fd.vma = vma;
  334. fd.addr1 = start;
  335. fd.addr2 = end;
  336. smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
  337. } else {
  338. int i;
  339. for (i = 0; i < num_online_cpus(); i++)
  340. if (smp_processor_id() != i)
  341. cpu_context(i, mm) = 0;
  342. }
  343. local_flush_tlb_range(vma, start, end);
  344. preempt_enable();
  345. }
  346. static void flush_tlb_kernel_range_ipi(void *info)
  347. {
  348. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  349. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  350. }
  351. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  352. {
  353. struct flush_tlb_data fd;
  354. fd.addr1 = start;
  355. fd.addr2 = end;
  356. on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
  357. }
  358. static void flush_tlb_page_ipi(void *info)
  359. {
  360. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  361. local_flush_tlb_page(fd->vma, fd->addr1);
  362. }
  363. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  364. {
  365. preempt_disable();
  366. if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
  367. (current->mm != vma->vm_mm)) {
  368. struct flush_tlb_data fd;
  369. fd.vma = vma;
  370. fd.addr1 = page;
  371. smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
  372. } else {
  373. int i;
  374. for (i = 0; i < num_online_cpus(); i++)
  375. if (smp_processor_id() != i)
  376. cpu_context(i, vma->vm_mm) = 0;
  377. }
  378. local_flush_tlb_page(vma, page);
  379. preempt_enable();
  380. }
  381. static void flush_tlb_one_ipi(void *info)
  382. {
  383. struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
  384. local_flush_tlb_one(fd->addr1, fd->addr2);
  385. }
  386. void flush_tlb_one(unsigned long asid, unsigned long vaddr)
  387. {
  388. struct flush_tlb_data fd;
  389. fd.addr1 = asid;
  390. fd.addr2 = vaddr;
  391. smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
  392. local_flush_tlb_one(asid, vaddr);
  393. }