smp.c 13 KB

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