smp.c 15 KB

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