smp.c 15 KB

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