smp.c 9.9 KB

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