smp.c 14 KB

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