smp.c 15 KB

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