smp.c 15 KB

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