smp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. outer_clean_range(__pa(pmd), __pa(pmd + 1));
  95. /*
  96. * We need to tell the secondary core where to find
  97. * its stack and the page tables.
  98. */
  99. secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
  100. secondary_data.pgdir = virt_to_phys(pgd);
  101. __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data));
  102. outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1));
  103. /*
  104. * Now bring the CPU into our world.
  105. */
  106. ret = boot_secondary(cpu, idle);
  107. if (ret == 0) {
  108. unsigned long timeout;
  109. /*
  110. * CPU was successfully started, wait for it
  111. * to come online or time out.
  112. */
  113. timeout = jiffies + HZ;
  114. while (time_before(jiffies, timeout)) {
  115. if (cpu_online(cpu))
  116. break;
  117. udelay(10);
  118. barrier();
  119. }
  120. if (!cpu_online(cpu))
  121. ret = -EIO;
  122. }
  123. secondary_data.stack = NULL;
  124. secondary_data.pgdir = 0;
  125. *pmd = __pmd(0);
  126. clean_pmd_entry(pmd);
  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 __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. set_cpu_online(cpu, false);
  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();
  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. cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
  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 __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 __ref 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. cpumask_set_cpu(cpu, mm_cpumask(mm));
  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. notify_cpu_starting(cpu);
  243. local_irq_enable();
  244. local_fiq_enable();
  245. /*
  246. * Setup the percpu timer for this CPU.
  247. */
  248. percpu_timer_setup();
  249. calibrate_delay();
  250. smp_store_cpu_info(cpu);
  251. /*
  252. * OK, now it's safe to let the boot CPU continue
  253. */
  254. set_cpu_online(cpu, true);
  255. /*
  256. * OK, it's off to the idle thread for us
  257. */
  258. cpu_idle();
  259. }
  260. /*
  261. * Called by both boot and secondaries to move global data into
  262. * per-processor storage.
  263. */
  264. void __cpuinit smp_store_cpu_info(unsigned int cpuid)
  265. {
  266. struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
  267. cpu_info->loops_per_jiffy = loops_per_jiffy;
  268. }
  269. void __init smp_cpus_done(unsigned int max_cpus)
  270. {
  271. int cpu;
  272. unsigned long bogosum = 0;
  273. for_each_online_cpu(cpu)
  274. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  275. printk(KERN_INFO "SMP: Total of %d processors activated "
  276. "(%lu.%02lu BogoMIPS).\n",
  277. num_online_cpus(),
  278. bogosum / (500000/HZ),
  279. (bogosum / (5000/HZ)) % 100);
  280. }
  281. void __init smp_prepare_boot_cpu(void)
  282. {
  283. unsigned int cpu = smp_processor_id();
  284. per_cpu(cpu_data, cpu).idle = current;
  285. }
  286. static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
  287. {
  288. unsigned long flags;
  289. unsigned int cpu;
  290. local_irq_save(flags);
  291. for_each_cpu(cpu, mask) {
  292. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  293. spin_lock(&ipi->lock);
  294. ipi->bits |= 1 << msg;
  295. spin_unlock(&ipi->lock);
  296. }
  297. /*
  298. * Call the platform specific cross-CPU call function.
  299. */
  300. smp_cross_call(mask);
  301. local_irq_restore(flags);
  302. }
  303. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  304. {
  305. send_ipi_message(mask, IPI_CALL_FUNC);
  306. }
  307. void arch_send_call_function_single_ipi(int cpu)
  308. {
  309. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
  310. }
  311. void show_ipi_list(struct seq_file *p)
  312. {
  313. unsigned int cpu;
  314. seq_puts(p, "IPI:");
  315. for_each_present_cpu(cpu)
  316. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  317. seq_putc(p, '\n');
  318. }
  319. void show_local_irqs(struct seq_file *p)
  320. {
  321. unsigned int cpu;
  322. seq_printf(p, "LOC: ");
  323. for_each_present_cpu(cpu)
  324. seq_printf(p, "%10u ", irq_stat[cpu].local_timer_irqs);
  325. seq_putc(p, '\n');
  326. }
  327. /*
  328. * Timer (local or broadcast) support
  329. */
  330. static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
  331. static void ipi_timer(void)
  332. {
  333. struct clock_event_device *evt = &__get_cpu_var(percpu_clockevent);
  334. irq_enter();
  335. evt->event_handler(evt);
  336. irq_exit();
  337. }
  338. #ifdef CONFIG_LOCAL_TIMERS
  339. asmlinkage void __exception do_local_timer(struct pt_regs *regs)
  340. {
  341. struct pt_regs *old_regs = set_irq_regs(regs);
  342. int cpu = smp_processor_id();
  343. if (local_timer_ack()) {
  344. irq_stat[cpu].local_timer_irqs++;
  345. ipi_timer();
  346. }
  347. set_irq_regs(old_regs);
  348. }
  349. #endif
  350. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  351. static void smp_timer_broadcast(const struct cpumask *mask)
  352. {
  353. send_ipi_message(mask, IPI_TIMER);
  354. }
  355. static void broadcast_timer_set_mode(enum clock_event_mode mode,
  356. struct clock_event_device *evt)
  357. {
  358. }
  359. static void local_timer_setup(struct clock_event_device *evt)
  360. {
  361. evt->name = "dummy_timer";
  362. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  363. CLOCK_EVT_FEAT_PERIODIC |
  364. CLOCK_EVT_FEAT_DUMMY;
  365. evt->rating = 400;
  366. evt->mult = 1;
  367. evt->set_mode = broadcast_timer_set_mode;
  368. evt->broadcast = smp_timer_broadcast;
  369. clockevents_register_device(evt);
  370. }
  371. #endif
  372. void __cpuinit percpu_timer_setup(void)
  373. {
  374. unsigned int cpu = smp_processor_id();
  375. struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
  376. evt->cpumask = cpumask_of(cpu);
  377. local_timer_setup(evt);
  378. }
  379. static DEFINE_SPINLOCK(stop_lock);
  380. /*
  381. * ipi_cpu_stop - handle IPI from smp_send_stop()
  382. */
  383. static void ipi_cpu_stop(unsigned int cpu)
  384. {
  385. spin_lock(&stop_lock);
  386. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  387. dump_stack();
  388. spin_unlock(&stop_lock);
  389. set_cpu_online(cpu, false);
  390. local_fiq_disable();
  391. local_irq_disable();
  392. while (1)
  393. cpu_relax();
  394. }
  395. /*
  396. * Main handler for inter-processor interrupts
  397. *
  398. * For ARM, the ipimask now only identifies a single
  399. * category of IPI (Bit 1 IPIs have been replaced by a
  400. * different mechanism):
  401. *
  402. * Bit 0 - Inter-processor function call
  403. */
  404. asmlinkage void __exception do_IPI(struct pt_regs *regs)
  405. {
  406. unsigned int cpu = smp_processor_id();
  407. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  408. struct pt_regs *old_regs = set_irq_regs(regs);
  409. ipi->ipi_count++;
  410. for (;;) {
  411. unsigned long msgs;
  412. spin_lock(&ipi->lock);
  413. msgs = ipi->bits;
  414. ipi->bits = 0;
  415. spin_unlock(&ipi->lock);
  416. if (!msgs)
  417. break;
  418. do {
  419. unsigned nextmsg;
  420. nextmsg = msgs & -msgs;
  421. msgs &= ~nextmsg;
  422. nextmsg = ffz(~nextmsg);
  423. switch (nextmsg) {
  424. case IPI_TIMER:
  425. ipi_timer();
  426. break;
  427. case IPI_RESCHEDULE:
  428. /*
  429. * nothing more to do - eveything is
  430. * done on the interrupt return path
  431. */
  432. break;
  433. case IPI_CALL_FUNC:
  434. generic_smp_call_function_interrupt();
  435. break;
  436. case IPI_CALL_FUNC_SINGLE:
  437. generic_smp_call_function_single_interrupt();
  438. break;
  439. case IPI_CPU_STOP:
  440. ipi_cpu_stop(cpu);
  441. break;
  442. default:
  443. printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
  444. cpu, nextmsg);
  445. break;
  446. }
  447. } while (msgs);
  448. }
  449. set_irq_regs(old_regs);
  450. }
  451. void smp_send_reschedule(int cpu)
  452. {
  453. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  454. }
  455. void smp_send_stop(void)
  456. {
  457. cpumask_t mask = cpu_online_map;
  458. cpu_clear(smp_processor_id(), mask);
  459. send_ipi_message(&mask, IPI_CPU_STOP);
  460. }
  461. /*
  462. * not supported here
  463. */
  464. int setup_profiling_timer(unsigned int multiplier)
  465. {
  466. return -EINVAL;
  467. }
  468. static void
  469. on_each_cpu_mask(void (*func)(void *), void *info, int wait,
  470. const struct cpumask *mask)
  471. {
  472. preempt_disable();
  473. smp_call_function_many(mask, func, info, wait);
  474. if (cpumask_test_cpu(smp_processor_id(), mask))
  475. func(info);
  476. preempt_enable();
  477. }
  478. /**********************************************************************/
  479. /*
  480. * TLB operations
  481. */
  482. struct tlb_args {
  483. struct vm_area_struct *ta_vma;
  484. unsigned long ta_start;
  485. unsigned long ta_end;
  486. };
  487. static inline void ipi_flush_tlb_all(void *ignored)
  488. {
  489. local_flush_tlb_all();
  490. }
  491. static inline void ipi_flush_tlb_mm(void *arg)
  492. {
  493. struct mm_struct *mm = (struct mm_struct *)arg;
  494. local_flush_tlb_mm(mm);
  495. }
  496. static inline void ipi_flush_tlb_page(void *arg)
  497. {
  498. struct tlb_args *ta = (struct tlb_args *)arg;
  499. local_flush_tlb_page(ta->ta_vma, ta->ta_start);
  500. }
  501. static inline void ipi_flush_tlb_kernel_page(void *arg)
  502. {
  503. struct tlb_args *ta = (struct tlb_args *)arg;
  504. local_flush_tlb_kernel_page(ta->ta_start);
  505. }
  506. static inline void ipi_flush_tlb_range(void *arg)
  507. {
  508. struct tlb_args *ta = (struct tlb_args *)arg;
  509. local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
  510. }
  511. static inline void ipi_flush_tlb_kernel_range(void *arg)
  512. {
  513. struct tlb_args *ta = (struct tlb_args *)arg;
  514. local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
  515. }
  516. void flush_tlb_all(void)
  517. {
  518. if (tlb_ops_need_broadcast())
  519. on_each_cpu(ipi_flush_tlb_all, NULL, 1);
  520. else
  521. local_flush_tlb_all();
  522. }
  523. void flush_tlb_mm(struct mm_struct *mm)
  524. {
  525. if (tlb_ops_need_broadcast())
  526. on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, mm_cpumask(mm));
  527. else
  528. local_flush_tlb_mm(mm);
  529. }
  530. void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
  531. {
  532. if (tlb_ops_need_broadcast()) {
  533. struct tlb_args ta;
  534. ta.ta_vma = vma;
  535. ta.ta_start = uaddr;
  536. on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, mm_cpumask(vma->vm_mm));
  537. } else
  538. local_flush_tlb_page(vma, uaddr);
  539. }
  540. void flush_tlb_kernel_page(unsigned long kaddr)
  541. {
  542. if (tlb_ops_need_broadcast()) {
  543. struct tlb_args ta;
  544. ta.ta_start = kaddr;
  545. on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1);
  546. } else
  547. local_flush_tlb_kernel_page(kaddr);
  548. }
  549. void flush_tlb_range(struct vm_area_struct *vma,
  550. unsigned long start, unsigned long end)
  551. {
  552. if (tlb_ops_need_broadcast()) {
  553. struct tlb_args ta;
  554. ta.ta_vma = vma;
  555. ta.ta_start = start;
  556. ta.ta_end = end;
  557. on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, mm_cpumask(vma->vm_mm));
  558. } else
  559. local_flush_tlb_range(vma, start, end);
  560. }
  561. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  562. {
  563. if (tlb_ops_need_broadcast()) {
  564. struct tlb_args ta;
  565. ta.ta_start = start;
  566. ta.ta_end = end;
  567. on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1);
  568. } else
  569. local_flush_tlb_kernel_range(start, end);
  570. }