smp.c 14 KB

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