smp.c 14 KB

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