smp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. #else
  362. #define smp_timer_broadcast NULL
  363. #endif
  364. #ifndef CONFIG_LOCAL_TIMERS
  365. static void broadcast_timer_set_mode(enum clock_event_mode mode,
  366. struct clock_event_device *evt)
  367. {
  368. }
  369. static void local_timer_setup(struct clock_event_device *evt)
  370. {
  371. evt->name = "dummy_timer";
  372. evt->features = CLOCK_EVT_FEAT_ONESHOT |
  373. CLOCK_EVT_FEAT_PERIODIC |
  374. CLOCK_EVT_FEAT_DUMMY;
  375. evt->rating = 400;
  376. evt->mult = 1;
  377. evt->set_mode = broadcast_timer_set_mode;
  378. clockevents_register_device(evt);
  379. }
  380. #endif
  381. void __cpuinit percpu_timer_setup(void)
  382. {
  383. unsigned int cpu = smp_processor_id();
  384. struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
  385. evt->cpumask = cpumask_of(cpu);
  386. evt->broadcast = smp_timer_broadcast;
  387. local_timer_setup(evt);
  388. }
  389. static DEFINE_SPINLOCK(stop_lock);
  390. /*
  391. * ipi_cpu_stop - handle IPI from smp_send_stop()
  392. */
  393. static void ipi_cpu_stop(unsigned int cpu)
  394. {
  395. if (system_state == SYSTEM_BOOTING ||
  396. system_state == SYSTEM_RUNNING) {
  397. spin_lock(&stop_lock);
  398. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  399. dump_stack();
  400. spin_unlock(&stop_lock);
  401. }
  402. set_cpu_online(cpu, false);
  403. local_fiq_disable();
  404. local_irq_disable();
  405. while (1)
  406. cpu_relax();
  407. }
  408. /*
  409. * Main handler for inter-processor interrupts
  410. *
  411. * For ARM, the ipimask now only identifies a single
  412. * category of IPI (Bit 1 IPIs have been replaced by a
  413. * different mechanism):
  414. *
  415. * Bit 0 - Inter-processor function call
  416. */
  417. asmlinkage void __exception do_IPI(struct pt_regs *regs)
  418. {
  419. unsigned int cpu = smp_processor_id();
  420. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  421. struct pt_regs *old_regs = set_irq_regs(regs);
  422. ipi->ipi_count++;
  423. for (;;) {
  424. unsigned long msgs;
  425. spin_lock(&ipi->lock);
  426. msgs = ipi->bits;
  427. ipi->bits = 0;
  428. spin_unlock(&ipi->lock);
  429. if (!msgs)
  430. break;
  431. do {
  432. unsigned nextmsg;
  433. nextmsg = msgs & -msgs;
  434. msgs &= ~nextmsg;
  435. nextmsg = ffz(~nextmsg);
  436. switch (nextmsg) {
  437. case IPI_TIMER:
  438. ipi_timer();
  439. break;
  440. case IPI_RESCHEDULE:
  441. /*
  442. * nothing more to do - eveything is
  443. * done on the interrupt return path
  444. */
  445. break;
  446. case IPI_CALL_FUNC:
  447. generic_smp_call_function_interrupt();
  448. break;
  449. case IPI_CALL_FUNC_SINGLE:
  450. generic_smp_call_function_single_interrupt();
  451. break;
  452. case IPI_CPU_STOP:
  453. ipi_cpu_stop(cpu);
  454. break;
  455. default:
  456. printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
  457. cpu, nextmsg);
  458. break;
  459. }
  460. } while (msgs);
  461. }
  462. set_irq_regs(old_regs);
  463. }
  464. void smp_send_reschedule(int cpu)
  465. {
  466. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  467. }
  468. void smp_send_stop(void)
  469. {
  470. cpumask_t mask = cpu_online_map;
  471. cpu_clear(smp_processor_id(), mask);
  472. send_ipi_message(&mask, IPI_CPU_STOP);
  473. }
  474. /*
  475. * not supported here
  476. */
  477. int setup_profiling_timer(unsigned int multiplier)
  478. {
  479. return -EINVAL;
  480. }
  481. static void
  482. on_each_cpu_mask(void (*func)(void *), void *info, int wait,
  483. const struct cpumask *mask)
  484. {
  485. preempt_disable();
  486. smp_call_function_many(mask, func, info, wait);
  487. if (cpumask_test_cpu(smp_processor_id(), mask))
  488. func(info);
  489. preempt_enable();
  490. }
  491. /**********************************************************************/
  492. /*
  493. * TLB operations
  494. */
  495. struct tlb_args {
  496. struct vm_area_struct *ta_vma;
  497. unsigned long ta_start;
  498. unsigned long ta_end;
  499. };
  500. static inline void ipi_flush_tlb_all(void *ignored)
  501. {
  502. local_flush_tlb_all();
  503. }
  504. static inline void ipi_flush_tlb_mm(void *arg)
  505. {
  506. struct mm_struct *mm = (struct mm_struct *)arg;
  507. local_flush_tlb_mm(mm);
  508. }
  509. static inline void ipi_flush_tlb_page(void *arg)
  510. {
  511. struct tlb_args *ta = (struct tlb_args *)arg;
  512. local_flush_tlb_page(ta->ta_vma, ta->ta_start);
  513. }
  514. static inline void ipi_flush_tlb_kernel_page(void *arg)
  515. {
  516. struct tlb_args *ta = (struct tlb_args *)arg;
  517. local_flush_tlb_kernel_page(ta->ta_start);
  518. }
  519. static inline void ipi_flush_tlb_range(void *arg)
  520. {
  521. struct tlb_args *ta = (struct tlb_args *)arg;
  522. local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
  523. }
  524. static inline void ipi_flush_tlb_kernel_range(void *arg)
  525. {
  526. struct tlb_args *ta = (struct tlb_args *)arg;
  527. local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
  528. }
  529. void flush_tlb_all(void)
  530. {
  531. if (tlb_ops_need_broadcast())
  532. on_each_cpu(ipi_flush_tlb_all, NULL, 1);
  533. else
  534. local_flush_tlb_all();
  535. }
  536. void flush_tlb_mm(struct mm_struct *mm)
  537. {
  538. if (tlb_ops_need_broadcast())
  539. on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, mm_cpumask(mm));
  540. else
  541. local_flush_tlb_mm(mm);
  542. }
  543. void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
  544. {
  545. if (tlb_ops_need_broadcast()) {
  546. struct tlb_args ta;
  547. ta.ta_vma = vma;
  548. ta.ta_start = uaddr;
  549. on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, mm_cpumask(vma->vm_mm));
  550. } else
  551. local_flush_tlb_page(vma, uaddr);
  552. }
  553. void flush_tlb_kernel_page(unsigned long kaddr)
  554. {
  555. if (tlb_ops_need_broadcast()) {
  556. struct tlb_args ta;
  557. ta.ta_start = kaddr;
  558. on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1);
  559. } else
  560. local_flush_tlb_kernel_page(kaddr);
  561. }
  562. void flush_tlb_range(struct vm_area_struct *vma,
  563. unsigned long start, unsigned long end)
  564. {
  565. if (tlb_ops_need_broadcast()) {
  566. struct tlb_args ta;
  567. ta.ta_vma = vma;
  568. ta.ta_start = start;
  569. ta.ta_end = end;
  570. on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, mm_cpumask(vma->vm_mm));
  571. } else
  572. local_flush_tlb_range(vma, start, end);
  573. }
  574. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  575. {
  576. if (tlb_ops_need_broadcast()) {
  577. struct tlb_args ta;
  578. ta.ta_start = start;
  579. ta.ta_end = end;
  580. on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1);
  581. } else
  582. local_flush_tlb_kernel_range(start, end);
  583. }