smp.c 13 KB

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