smp.c 15 KB

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