smp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/mm.h>
  24. #include <linux/sched/hotplug.h>
  25. #include <linux/atomic.h>
  26. #include <linux/clockchips.h>
  27. #include <asm/processor.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/smp.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/sections.h>
  32. #include <asm/setup.h>
  33. int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
  34. int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
  35. struct plat_smp_ops *mp_ops = NULL;
  36. /* State of each CPU */
  37. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  38. void register_smp_ops(struct plat_smp_ops *ops)
  39. {
  40. if (mp_ops)
  41. printk(KERN_WARNING "Overriding previously set SMP ops\n");
  42. mp_ops = ops;
  43. }
  44. static inline void smp_store_cpu_info(unsigned int cpu)
  45. {
  46. struct sh_cpuinfo *c = cpu_data + cpu;
  47. memcpy(c, &boot_cpu_data, sizeof(struct sh_cpuinfo));
  48. c->loops_per_jiffy = loops_per_jiffy;
  49. }
  50. void __init smp_prepare_cpus(unsigned int max_cpus)
  51. {
  52. unsigned int cpu = smp_processor_id();
  53. init_new_context(current, &init_mm);
  54. current_thread_info()->cpu = cpu;
  55. mp_ops->prepare_cpus(max_cpus);
  56. #ifndef CONFIG_HOTPLUG_CPU
  57. init_cpu_present(cpu_possible_mask);
  58. #endif
  59. }
  60. void __init smp_prepare_boot_cpu(void)
  61. {
  62. unsigned int cpu = smp_processor_id();
  63. __cpu_number_map[0] = cpu;
  64. __cpu_logical_map[0] = cpu;
  65. set_cpu_online(cpu, true);
  66. set_cpu_possible(cpu, true);
  67. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  68. }
  69. #ifdef CONFIG_HOTPLUG_CPU
  70. void native_cpu_die(unsigned int cpu)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < 10; i++) {
  74. smp_rmb();
  75. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  76. if (system_state == SYSTEM_RUNNING)
  77. pr_info("CPU %u is now offline\n", cpu);
  78. return;
  79. }
  80. msleep(100);
  81. }
  82. pr_err("CPU %u didn't die...\n", cpu);
  83. }
  84. int native_cpu_disable(unsigned int cpu)
  85. {
  86. return cpu == 0 ? -EPERM : 0;
  87. }
  88. void play_dead_common(void)
  89. {
  90. idle_task_exit();
  91. irq_ctx_exit(raw_smp_processor_id());
  92. mb();
  93. __this_cpu_write(cpu_state, CPU_DEAD);
  94. local_irq_disable();
  95. }
  96. void native_play_dead(void)
  97. {
  98. play_dead_common();
  99. }
  100. int __cpu_disable(void)
  101. {
  102. unsigned int cpu = smp_processor_id();
  103. int ret;
  104. ret = mp_ops->cpu_disable(cpu);
  105. if (ret)
  106. return ret;
  107. /*
  108. * Take this CPU offline. Once we clear this, we can't return,
  109. * and we must not schedule until we're ready to give up the cpu.
  110. */
  111. set_cpu_online(cpu, false);
  112. /*
  113. * OK - migrate IRQs away from this CPU
  114. */
  115. migrate_irqs();
  116. /*
  117. * Flush user cache and TLB mappings, and then remove this CPU
  118. * from the vm mask set of all processes.
  119. */
  120. flush_cache_all();
  121. #ifdef CONFIG_MMU
  122. local_flush_tlb_all();
  123. #endif
  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. mmgrab(mm);
  148. mmget(mm);
  149. current->active_mm = mm;
  150. #ifdef CONFIG_MMU
  151. enter_lazy_tlb(mm, current);
  152. local_flush_tlb_all();
  153. #endif
  154. per_cpu_trap_init();
  155. preempt_disable();
  156. notify_cpu_starting(cpu);
  157. local_irq_enable();
  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_AP_ONLINE_IDLE);
  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. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  226. void tick_broadcast(const struct cpumask *mask)
  227. {
  228. int cpu;
  229. for_each_cpu(cpu, mask)
  230. mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
  231. }
  232. static void ipi_timer(void)
  233. {
  234. irq_enter();
  235. tick_receive_broadcast();
  236. irq_exit();
  237. }
  238. #endif
  239. void smp_message_recv(unsigned int msg)
  240. {
  241. switch (msg) {
  242. case SMP_MSG_FUNCTION:
  243. generic_smp_call_function_interrupt();
  244. break;
  245. case SMP_MSG_RESCHEDULE:
  246. scheduler_ipi();
  247. break;
  248. case SMP_MSG_FUNCTION_SINGLE:
  249. generic_smp_call_function_single_interrupt();
  250. break;
  251. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  252. case SMP_MSG_TIMER:
  253. ipi_timer();
  254. break;
  255. #endif
  256. default:
  257. printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
  258. smp_processor_id(), __func__, msg);
  259. break;
  260. }
  261. }
  262. /* Not really SMP stuff ... */
  263. int setup_profiling_timer(unsigned int multiplier)
  264. {
  265. return 0;
  266. }
  267. #ifdef CONFIG_MMU
  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_each_online_cpu(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_each_online_cpu(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_each_online_cpu(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. }
  384. #endif