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