smp.c 9.6 KB

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