smp.c 14 KB

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