smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * linux/arch/arm/kernel/smp.c
  3. *
  4. * Copyright (C) 2002 ARM Limited, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/cache.h>
  17. #include <linux/profile.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/err.h>
  21. #include <linux/cpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/irq.h>
  25. #include <asm/atomic.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/cpu.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/processor.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/ptrace.h>
  34. /*
  35. * bitmask of present and online CPUs.
  36. * The present bitmask indicates that the CPU is physically present.
  37. * The online bitmask indicates that the CPU is up and running.
  38. */
  39. cpumask_t cpu_possible_map;
  40. EXPORT_SYMBOL(cpu_possible_map);
  41. cpumask_t cpu_online_map;
  42. EXPORT_SYMBOL(cpu_online_map);
  43. /*
  44. * as from 2.5, kernels no longer have an init_tasks structure
  45. * so we need some other way of telling a new secondary core
  46. * where to place its SVC stack
  47. */
  48. struct secondary_data secondary_data;
  49. /*
  50. * structures for inter-processor calls
  51. * - A collection of single bit ipi messages.
  52. */
  53. struct ipi_data {
  54. spinlock_t lock;
  55. unsigned long ipi_count;
  56. unsigned long bits;
  57. };
  58. static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
  59. .lock = SPIN_LOCK_UNLOCKED,
  60. };
  61. enum ipi_msg_type {
  62. IPI_TIMER,
  63. IPI_RESCHEDULE,
  64. IPI_CALL_FUNC,
  65. IPI_CALL_FUNC_SINGLE,
  66. IPI_CPU_STOP,
  67. };
  68. int __cpuinit __cpu_up(unsigned int cpu)
  69. {
  70. struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
  71. struct task_struct *idle = ci->idle;
  72. pgd_t *pgd;
  73. pmd_t *pmd;
  74. int ret;
  75. /*
  76. * Spawn a new process manually, if not already done.
  77. * Grab a pointer to its task struct so we can mess with it
  78. */
  79. if (!idle) {
  80. idle = fork_idle(cpu);
  81. if (IS_ERR(idle)) {
  82. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  83. return PTR_ERR(idle);
  84. }
  85. ci->idle = idle;
  86. }
  87. /*
  88. * Allocate initial page tables to allow the new CPU to
  89. * enable the MMU safely. This essentially means a set
  90. * of our "standard" page tables, with the addition of
  91. * a 1:1 mapping for the physical address of the kernel.
  92. */
  93. pgd = pgd_alloc(&init_mm);
  94. pmd = pmd_offset(pgd, PHYS_OFFSET);
  95. *pmd = __pmd((PHYS_OFFSET & PGDIR_MASK) |
  96. PMD_TYPE_SECT | PMD_SECT_AP_WRITE);
  97. /*
  98. * We need to tell the secondary core where to find
  99. * its stack and the page tables.
  100. */
  101. secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
  102. secondary_data.pgdir = virt_to_phys(pgd);
  103. wmb();
  104. /*
  105. * Now bring the CPU into our world.
  106. */
  107. ret = boot_secondary(cpu, idle);
  108. if (ret == 0) {
  109. unsigned long timeout;
  110. /*
  111. * CPU was successfully started, wait for it
  112. * to come online or time out.
  113. */
  114. timeout = jiffies + HZ;
  115. while (time_before(jiffies, timeout)) {
  116. if (cpu_online(cpu))
  117. break;
  118. udelay(10);
  119. barrier();
  120. }
  121. if (!cpu_online(cpu))
  122. ret = -EIO;
  123. }
  124. secondary_data.stack = NULL;
  125. secondary_data.pgdir = 0;
  126. *pmd_offset(pgd, PHYS_OFFSET) = __pmd(0);
  127. pgd_free(&init_mm, pgd);
  128. if (ret) {
  129. printk(KERN_CRIT "CPU%u: processor failed to boot\n", cpu);
  130. /*
  131. * FIXME: We need to clean up the new idle thread. --rmk
  132. */
  133. }
  134. return ret;
  135. }
  136. #ifdef CONFIG_HOTPLUG_CPU
  137. /*
  138. * __cpu_disable runs on the processor to be shutdown.
  139. */
  140. int __cpuexit __cpu_disable(void)
  141. {
  142. unsigned int cpu = smp_processor_id();
  143. struct task_struct *p;
  144. int ret;
  145. ret = mach_cpu_disable(cpu);
  146. if (ret)
  147. return ret;
  148. /*
  149. * Take this CPU offline. Once we clear this, we can't return,
  150. * and we must not schedule until we're ready to give up the cpu.
  151. */
  152. cpu_clear(cpu, cpu_online_map);
  153. /*
  154. * OK - migrate IRQs away from this CPU
  155. */
  156. migrate_irqs();
  157. /*
  158. * Stop the local timer for this CPU.
  159. */
  160. local_timer_stop(cpu);
  161. /*
  162. * Flush user cache and TLB mappings, and then remove this CPU
  163. * from the vm mask set of all processes.
  164. */
  165. flush_cache_all();
  166. local_flush_tlb_all();
  167. read_lock(&tasklist_lock);
  168. for_each_process(p) {
  169. if (p->mm)
  170. cpu_clear(cpu, p->mm->cpu_vm_mask);
  171. }
  172. read_unlock(&tasklist_lock);
  173. return 0;
  174. }
  175. /*
  176. * called on the thread which is asking for a CPU to be shutdown -
  177. * waits until shutdown has completed, or it is timed out.
  178. */
  179. void __cpuexit __cpu_die(unsigned int cpu)
  180. {
  181. if (!platform_cpu_kill(cpu))
  182. printk("CPU%u: unable to kill\n", cpu);
  183. }
  184. /*
  185. * Called from the idle thread for the CPU which has been shutdown.
  186. *
  187. * Note that we disable IRQs here, but do not re-enable them
  188. * before returning to the caller. This is also the behaviour
  189. * of the other hotplug-cpu capable cores, so presumably coming
  190. * out of idle fixes this.
  191. */
  192. void __cpuexit cpu_die(void)
  193. {
  194. unsigned int cpu = smp_processor_id();
  195. local_irq_disable();
  196. idle_task_exit();
  197. /*
  198. * actual CPU shutdown procedure is at least platform (if not
  199. * CPU) specific
  200. */
  201. platform_cpu_die(cpu);
  202. /*
  203. * Do not return to the idle loop - jump back to the secondary
  204. * cpu initialisation. There's some initialisation which needs
  205. * to be repeated to undo the effects of taking the CPU offline.
  206. */
  207. __asm__("mov sp, %0\n"
  208. " b secondary_start_kernel"
  209. :
  210. : "r" (task_stack_page(current) + THREAD_SIZE - 8));
  211. }
  212. #endif /* CONFIG_HOTPLUG_CPU */
  213. /*
  214. * This is the secondary CPU boot entry. We're using this CPUs
  215. * idle thread stack, but a set of temporary page tables.
  216. */
  217. asmlinkage void __cpuinit secondary_start_kernel(void)
  218. {
  219. struct mm_struct *mm = &init_mm;
  220. unsigned int cpu = smp_processor_id();
  221. printk("CPU%u: Booted secondary processor\n", cpu);
  222. /*
  223. * All kernel threads share the same mm context; grab a
  224. * reference and switch to it.
  225. */
  226. atomic_inc(&mm->mm_users);
  227. atomic_inc(&mm->mm_count);
  228. current->active_mm = mm;
  229. cpu_set(cpu, mm->cpu_vm_mask);
  230. cpu_switch_mm(mm->pgd, mm);
  231. enter_lazy_tlb(mm, current);
  232. local_flush_tlb_all();
  233. cpu_init();
  234. preempt_disable();
  235. /*
  236. * Give the platform a chance to do its own initialisation.
  237. */
  238. platform_secondary_init(cpu);
  239. /*
  240. * Enable local interrupts.
  241. */
  242. local_irq_enable();
  243. local_fiq_enable();
  244. /*
  245. * Setup local timer for this CPU.
  246. */
  247. local_timer_setup(cpu);
  248. calibrate_delay();
  249. smp_store_cpu_info(cpu);
  250. /*
  251. * OK, now it's safe to let the boot CPU continue
  252. */
  253. cpu_set(cpu, cpu_online_map);
  254. /*
  255. * OK, it's off to the idle thread for us
  256. */
  257. cpu_idle();
  258. }
  259. /*
  260. * Called by both boot and secondaries to move global data into
  261. * per-processor storage.
  262. */
  263. void __cpuinit smp_store_cpu_info(unsigned int cpuid)
  264. {
  265. struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
  266. cpu_info->loops_per_jiffy = loops_per_jiffy;
  267. }
  268. void __init smp_cpus_done(unsigned int max_cpus)
  269. {
  270. int cpu;
  271. unsigned long bogosum = 0;
  272. for_each_online_cpu(cpu)
  273. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  274. printk(KERN_INFO "SMP: Total of %d processors activated "
  275. "(%lu.%02lu BogoMIPS).\n",
  276. num_online_cpus(),
  277. bogosum / (500000/HZ),
  278. (bogosum / (5000/HZ)) % 100);
  279. }
  280. void __init smp_prepare_boot_cpu(void)
  281. {
  282. unsigned int cpu = smp_processor_id();
  283. per_cpu(cpu_data, cpu).idle = current;
  284. }
  285. static void send_ipi_message(cpumask_t callmap, enum ipi_msg_type msg)
  286. {
  287. unsigned long flags;
  288. unsigned int cpu;
  289. local_irq_save(flags);
  290. for_each_cpu_mask(cpu, callmap) {
  291. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  292. spin_lock(&ipi->lock);
  293. ipi->bits |= 1 << msg;
  294. spin_unlock(&ipi->lock);
  295. }
  296. /*
  297. * Call the platform specific cross-CPU call function.
  298. */
  299. smp_cross_call(callmap);
  300. local_irq_restore(flags);
  301. }
  302. void arch_send_call_function_ipi(cpumask_t mask)
  303. {
  304. send_ipi_message(mask, IPI_CALL_FUNC);
  305. }
  306. void arch_send_call_function_single_ipi(int cpu)
  307. {
  308. send_ipi_message(cpumask_of_cpu(cpu), IPI_CALL_FUNC_SINGLE);
  309. }
  310. void show_ipi_list(struct seq_file *p)
  311. {
  312. unsigned int cpu;
  313. seq_puts(p, "IPI:");
  314. for_each_present_cpu(cpu)
  315. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  316. seq_putc(p, '\n');
  317. }
  318. void show_local_irqs(struct seq_file *p)
  319. {
  320. unsigned int cpu;
  321. seq_printf(p, "LOC: ");
  322. for_each_present_cpu(cpu)
  323. seq_printf(p, "%10u ", irq_stat[cpu].local_timer_irqs);
  324. seq_putc(p, '\n');
  325. }
  326. static void ipi_timer(void)
  327. {
  328. irq_enter();
  329. local_timer_interrupt();
  330. irq_exit();
  331. }
  332. #ifdef CONFIG_LOCAL_TIMERS
  333. asmlinkage void __exception do_local_timer(struct pt_regs *regs)
  334. {
  335. struct pt_regs *old_regs = set_irq_regs(regs);
  336. int cpu = smp_processor_id();
  337. if (local_timer_ack()) {
  338. irq_stat[cpu].local_timer_irqs++;
  339. ipi_timer();
  340. }
  341. set_irq_regs(old_regs);
  342. }
  343. #endif
  344. static DEFINE_SPINLOCK(stop_lock);
  345. /*
  346. * ipi_cpu_stop - handle IPI from smp_send_stop()
  347. */
  348. static void ipi_cpu_stop(unsigned int cpu)
  349. {
  350. spin_lock(&stop_lock);
  351. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  352. dump_stack();
  353. spin_unlock(&stop_lock);
  354. cpu_clear(cpu, cpu_online_map);
  355. local_fiq_disable();
  356. local_irq_disable();
  357. while (1)
  358. cpu_relax();
  359. }
  360. /*
  361. * Main handler for inter-processor interrupts
  362. *
  363. * For ARM, the ipimask now only identifies a single
  364. * category of IPI (Bit 1 IPIs have been replaced by a
  365. * different mechanism):
  366. *
  367. * Bit 0 - Inter-processor function call
  368. */
  369. asmlinkage void __exception do_IPI(struct pt_regs *regs)
  370. {
  371. unsigned int cpu = smp_processor_id();
  372. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  373. struct pt_regs *old_regs = set_irq_regs(regs);
  374. ipi->ipi_count++;
  375. for (;;) {
  376. unsigned long msgs;
  377. spin_lock(&ipi->lock);
  378. msgs = ipi->bits;
  379. ipi->bits = 0;
  380. spin_unlock(&ipi->lock);
  381. if (!msgs)
  382. break;
  383. do {
  384. unsigned nextmsg;
  385. nextmsg = msgs & -msgs;
  386. msgs &= ~nextmsg;
  387. nextmsg = ffz(~nextmsg);
  388. switch (nextmsg) {
  389. case IPI_TIMER:
  390. ipi_timer();
  391. break;
  392. case IPI_RESCHEDULE:
  393. /*
  394. * nothing more to do - eveything is
  395. * done on the interrupt return path
  396. */
  397. break;
  398. case IPI_CALL_FUNC:
  399. generic_smp_call_function_interrupt();
  400. break;
  401. case IPI_CALL_FUNC_SINGLE:
  402. generic_smp_call_function_single_interrupt();
  403. break;
  404. case IPI_CPU_STOP:
  405. ipi_cpu_stop(cpu);
  406. break;
  407. default:
  408. printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
  409. cpu, nextmsg);
  410. break;
  411. }
  412. } while (msgs);
  413. }
  414. set_irq_regs(old_regs);
  415. }
  416. void smp_send_reschedule(int cpu)
  417. {
  418. send_ipi_message(cpumask_of_cpu(cpu), IPI_RESCHEDULE);
  419. }
  420. void smp_send_timer(void)
  421. {
  422. cpumask_t mask = cpu_online_map;
  423. cpu_clear(smp_processor_id(), mask);
  424. send_ipi_message(mask, IPI_TIMER);
  425. }
  426. void smp_timer_broadcast(cpumask_t mask)
  427. {
  428. send_ipi_message(mask, IPI_TIMER);
  429. }
  430. void smp_send_stop(void)
  431. {
  432. cpumask_t mask = cpu_online_map;
  433. cpu_clear(smp_processor_id(), mask);
  434. send_ipi_message(mask, IPI_CPU_STOP);
  435. }
  436. /*
  437. * not supported here
  438. */
  439. int setup_profiling_timer(unsigned int multiplier)
  440. {
  441. return -EINVAL;
  442. }
  443. static int
  444. on_each_cpu_mask(void (*func)(void *), void *info, int wait, cpumask_t mask)
  445. {
  446. int ret = 0;
  447. preempt_disable();
  448. ret = smp_call_function_mask(mask, func, info, wait);
  449. if (cpu_isset(smp_processor_id(), mask))
  450. func(info);
  451. preempt_enable();
  452. return ret;
  453. }
  454. /**********************************************************************/
  455. /*
  456. * TLB operations
  457. */
  458. struct tlb_args {
  459. struct vm_area_struct *ta_vma;
  460. unsigned long ta_start;
  461. unsigned long ta_end;
  462. };
  463. static inline void ipi_flush_tlb_all(void *ignored)
  464. {
  465. local_flush_tlb_all();
  466. }
  467. static inline void ipi_flush_tlb_mm(void *arg)
  468. {
  469. struct mm_struct *mm = (struct mm_struct *)arg;
  470. local_flush_tlb_mm(mm);
  471. }
  472. static inline void ipi_flush_tlb_page(void *arg)
  473. {
  474. struct tlb_args *ta = (struct tlb_args *)arg;
  475. local_flush_tlb_page(ta->ta_vma, ta->ta_start);
  476. }
  477. static inline void ipi_flush_tlb_kernel_page(void *arg)
  478. {
  479. struct tlb_args *ta = (struct tlb_args *)arg;
  480. local_flush_tlb_kernel_page(ta->ta_start);
  481. }
  482. static inline void ipi_flush_tlb_range(void *arg)
  483. {
  484. struct tlb_args *ta = (struct tlb_args *)arg;
  485. local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
  486. }
  487. static inline void ipi_flush_tlb_kernel_range(void *arg)
  488. {
  489. struct tlb_args *ta = (struct tlb_args *)arg;
  490. local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
  491. }
  492. void flush_tlb_all(void)
  493. {
  494. on_each_cpu(ipi_flush_tlb_all, NULL, 1);
  495. }
  496. void flush_tlb_mm(struct mm_struct *mm)
  497. {
  498. cpumask_t mask = mm->cpu_vm_mask;
  499. on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, mask);
  500. }
  501. void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
  502. {
  503. cpumask_t mask = vma->vm_mm->cpu_vm_mask;
  504. struct tlb_args ta;
  505. ta.ta_vma = vma;
  506. ta.ta_start = uaddr;
  507. on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, mask);
  508. }
  509. void flush_tlb_kernel_page(unsigned long kaddr)
  510. {
  511. struct tlb_args ta;
  512. ta.ta_start = kaddr;
  513. on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1);
  514. }
  515. void flush_tlb_range(struct vm_area_struct *vma,
  516. unsigned long start, unsigned long end)
  517. {
  518. cpumask_t mask = vma->vm_mm->cpu_vm_mask;
  519. struct tlb_args ta;
  520. ta.ta_vma = vma;
  521. ta.ta_start = start;
  522. ta.ta_end = end;
  523. on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, mask);
  524. }
  525. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  526. {
  527. struct tlb_args ta;
  528. ta.ta_start = start;
  529. ta.ta_end = end;
  530. on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1);
  531. }