smp.c 13 KB

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