smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 task_struct *idle;
  75. pgd_t *pgd;
  76. pmd_t *pmd;
  77. int ret;
  78. /*
  79. * Spawn a new process manually. Grab a pointer to
  80. * its task struct so we can mess with it
  81. */
  82. idle = fork_idle(cpu);
  83. if (IS_ERR(idle)) {
  84. printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
  85. return PTR_ERR(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, 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 = (void *)idle->thread_info + 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 = 0;
  125. secondary_data.pgdir = 0;
  126. *pmd_offset(pgd, PHYS_OFFSET) = __pmd(0);
  127. pgd_free(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. /*
  137. * This is the secondary CPU boot entry. We're using this CPUs
  138. * idle thread stack, but a set of temporary page tables.
  139. */
  140. asmlinkage void __cpuinit secondary_start_kernel(void)
  141. {
  142. struct mm_struct *mm = &init_mm;
  143. unsigned int cpu = smp_processor_id();
  144. printk("CPU%u: Booted secondary processor\n", cpu);
  145. /*
  146. * All kernel threads share the same mm context; grab a
  147. * reference and switch to it.
  148. */
  149. atomic_inc(&mm->mm_users);
  150. atomic_inc(&mm->mm_count);
  151. current->active_mm = mm;
  152. cpu_set(cpu, mm->cpu_vm_mask);
  153. cpu_switch_mm(mm->pgd, mm);
  154. enter_lazy_tlb(mm, current);
  155. local_flush_tlb_all();
  156. cpu_init();
  157. /*
  158. * Give the platform a chance to do its own initialisation.
  159. */
  160. platform_secondary_init(cpu);
  161. /*
  162. * Enable local interrupts.
  163. */
  164. local_irq_enable();
  165. local_fiq_enable();
  166. calibrate_delay();
  167. smp_store_cpu_info(cpu);
  168. /*
  169. * OK, now it's safe to let the boot CPU continue
  170. */
  171. cpu_set(cpu, cpu_online_map);
  172. /*
  173. * OK, it's off to the idle thread for us
  174. */
  175. cpu_idle();
  176. }
  177. /*
  178. * Called by both boot and secondaries to move global data into
  179. * per-processor storage.
  180. */
  181. void __cpuinit smp_store_cpu_info(unsigned int cpuid)
  182. {
  183. struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
  184. cpu_info->loops_per_jiffy = loops_per_jiffy;
  185. }
  186. void __init smp_cpus_done(unsigned int max_cpus)
  187. {
  188. int cpu;
  189. unsigned long bogosum = 0;
  190. for_each_online_cpu(cpu)
  191. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  192. printk(KERN_INFO "SMP: Total of %d processors activated "
  193. "(%lu.%02lu BogoMIPS).\n",
  194. num_online_cpus(),
  195. bogosum / (500000/HZ),
  196. (bogosum / (5000/HZ)) % 100);
  197. }
  198. void __init smp_prepare_boot_cpu(void)
  199. {
  200. unsigned int cpu = smp_processor_id();
  201. cpu_set(cpu, cpu_possible_map);
  202. cpu_set(cpu, cpu_present_map);
  203. cpu_set(cpu, cpu_online_map);
  204. }
  205. static void send_ipi_message(cpumask_t callmap, enum ipi_msg_type msg)
  206. {
  207. unsigned long flags;
  208. unsigned int cpu;
  209. local_irq_save(flags);
  210. for_each_cpu_mask(cpu, callmap) {
  211. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  212. spin_lock(&ipi->lock);
  213. ipi->bits |= 1 << msg;
  214. spin_unlock(&ipi->lock);
  215. }
  216. /*
  217. * Call the platform specific cross-CPU call function.
  218. */
  219. smp_cross_call(callmap);
  220. local_irq_restore(flags);
  221. }
  222. /*
  223. * You must not call this function with disabled interrupts, from a
  224. * hardware interrupt handler, nor from a bottom half handler.
  225. */
  226. int smp_call_function_on_cpu(void (*func)(void *info), void *info, int retry,
  227. int wait, cpumask_t callmap)
  228. {
  229. struct smp_call_struct data;
  230. unsigned long timeout;
  231. int ret = 0;
  232. data.func = func;
  233. data.info = info;
  234. data.wait = wait;
  235. cpu_clear(smp_processor_id(), callmap);
  236. if (cpus_empty(callmap))
  237. goto out;
  238. data.pending = callmap;
  239. if (wait)
  240. data.unfinished = callmap;
  241. /*
  242. * try to get the mutex on smp_call_function_data
  243. */
  244. spin_lock(&smp_call_function_lock);
  245. smp_call_function_data = &data;
  246. send_ipi_message(callmap, IPI_CALL_FUNC);
  247. timeout = jiffies + HZ;
  248. while (!cpus_empty(data.pending) && time_before(jiffies, timeout))
  249. barrier();
  250. /*
  251. * did we time out?
  252. */
  253. if (!cpus_empty(data.pending)) {
  254. /*
  255. * this may be causing our panic - report it
  256. */
  257. printk(KERN_CRIT
  258. "CPU%u: smp_call_function timeout for %p(%p)\n"
  259. " callmap %lx pending %lx, %swait\n",
  260. smp_processor_id(), func, info, callmap, data.pending,
  261. wait ? "" : "no ");
  262. /*
  263. * TRACE
  264. */
  265. timeout = jiffies + (5 * HZ);
  266. while (!cpus_empty(data.pending) && time_before(jiffies, timeout))
  267. barrier();
  268. if (cpus_empty(data.pending))
  269. printk(KERN_CRIT " RESOLVED\n");
  270. else
  271. printk(KERN_CRIT " STILL STUCK\n");
  272. }
  273. /*
  274. * whatever happened, we're done with the data, so release it
  275. */
  276. smp_call_function_data = NULL;
  277. spin_unlock(&smp_call_function_lock);
  278. if (!cpus_empty(data.pending)) {
  279. ret = -ETIMEDOUT;
  280. goto out;
  281. }
  282. if (wait)
  283. while (!cpus_empty(data.unfinished))
  284. barrier();
  285. out:
  286. return 0;
  287. }
  288. int smp_call_function(void (*func)(void *info), void *info, int retry,
  289. int wait)
  290. {
  291. return smp_call_function_on_cpu(func, info, retry, wait,
  292. cpu_online_map);
  293. }
  294. void show_ipi_list(struct seq_file *p)
  295. {
  296. unsigned int cpu;
  297. seq_puts(p, "IPI:");
  298. for_each_present_cpu(cpu)
  299. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  300. seq_putc(p, '\n');
  301. }
  302. static void ipi_timer(struct pt_regs *regs)
  303. {
  304. int user = user_mode(regs);
  305. irq_enter();
  306. profile_tick(CPU_PROFILING, regs);
  307. update_process_times(user);
  308. irq_exit();
  309. }
  310. /*
  311. * ipi_call_function - handle IPI from smp_call_function()
  312. *
  313. * Note that we copy data out of the cross-call structure and then
  314. * let the caller know that we're here and have done with their data
  315. */
  316. static void ipi_call_function(unsigned int cpu)
  317. {
  318. struct smp_call_struct *data = smp_call_function_data;
  319. void (*func)(void *info) = data->func;
  320. void *info = data->info;
  321. int wait = data->wait;
  322. cpu_clear(cpu, data->pending);
  323. func(info);
  324. if (wait)
  325. cpu_clear(cpu, data->unfinished);
  326. }
  327. static DEFINE_SPINLOCK(stop_lock);
  328. /*
  329. * ipi_cpu_stop - handle IPI from smp_send_stop()
  330. */
  331. static void ipi_cpu_stop(unsigned int cpu)
  332. {
  333. spin_lock(&stop_lock);
  334. printk(KERN_CRIT "CPU%u: stopping\n", cpu);
  335. dump_stack();
  336. spin_unlock(&stop_lock);
  337. cpu_clear(cpu, cpu_online_map);
  338. local_fiq_disable();
  339. local_irq_disable();
  340. while (1)
  341. cpu_relax();
  342. }
  343. /*
  344. * Main handler for inter-processor interrupts
  345. *
  346. * For ARM, the ipimask now only identifies a single
  347. * category of IPI (Bit 1 IPIs have been replaced by a
  348. * different mechanism):
  349. *
  350. * Bit 0 - Inter-processor function call
  351. */
  352. void do_IPI(struct pt_regs *regs)
  353. {
  354. unsigned int cpu = smp_processor_id();
  355. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  356. ipi->ipi_count++;
  357. for (;;) {
  358. unsigned long msgs;
  359. spin_lock(&ipi->lock);
  360. msgs = ipi->bits;
  361. ipi->bits = 0;
  362. spin_unlock(&ipi->lock);
  363. if (!msgs)
  364. break;
  365. do {
  366. unsigned nextmsg;
  367. nextmsg = msgs & -msgs;
  368. msgs &= ~nextmsg;
  369. nextmsg = ffz(~nextmsg);
  370. switch (nextmsg) {
  371. case IPI_TIMER:
  372. ipi_timer(regs);
  373. break;
  374. case IPI_RESCHEDULE:
  375. /*
  376. * nothing more to do - eveything is
  377. * done on the interrupt return path
  378. */
  379. break;
  380. case IPI_CALL_FUNC:
  381. ipi_call_function(cpu);
  382. break;
  383. case IPI_CPU_STOP:
  384. ipi_cpu_stop(cpu);
  385. break;
  386. default:
  387. printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
  388. cpu, nextmsg);
  389. break;
  390. }
  391. } while (msgs);
  392. }
  393. }
  394. void smp_send_reschedule(int cpu)
  395. {
  396. send_ipi_message(cpumask_of_cpu(cpu), IPI_RESCHEDULE);
  397. }
  398. void smp_send_timer(void)
  399. {
  400. cpumask_t mask = cpu_online_map;
  401. cpu_clear(smp_processor_id(), mask);
  402. send_ipi_message(mask, IPI_TIMER);
  403. }
  404. void smp_send_stop(void)
  405. {
  406. cpumask_t mask = cpu_online_map;
  407. cpu_clear(smp_processor_id(), mask);
  408. send_ipi_message(mask, IPI_CPU_STOP);
  409. }
  410. /*
  411. * not supported here
  412. */
  413. int __init setup_profiling_timer(unsigned int multiplier)
  414. {
  415. return -EINVAL;
  416. }
  417. static int
  418. on_each_cpu_mask(void (*func)(void *), void *info, int retry, int wait,
  419. cpumask_t mask)
  420. {
  421. int ret = 0;
  422. preempt_disable();
  423. ret = smp_call_function_on_cpu(func, info, retry, wait, mask);
  424. if (cpu_isset(smp_processor_id(), mask))
  425. func(info);
  426. preempt_enable();
  427. return ret;
  428. }
  429. /**********************************************************************/
  430. /*
  431. * TLB operations
  432. */
  433. struct tlb_args {
  434. struct vm_area_struct *ta_vma;
  435. unsigned long ta_start;
  436. unsigned long ta_end;
  437. };
  438. static inline void ipi_flush_tlb_all(void *ignored)
  439. {
  440. local_flush_tlb_all();
  441. }
  442. static inline void ipi_flush_tlb_mm(void *arg)
  443. {
  444. struct mm_struct *mm = (struct mm_struct *)arg;
  445. local_flush_tlb_mm(mm);
  446. }
  447. static inline void ipi_flush_tlb_page(void *arg)
  448. {
  449. struct tlb_args *ta = (struct tlb_args *)arg;
  450. local_flush_tlb_page(ta->ta_vma, ta->ta_start);
  451. }
  452. static inline void ipi_flush_tlb_kernel_page(void *arg)
  453. {
  454. struct tlb_args *ta = (struct tlb_args *)arg;
  455. local_flush_tlb_kernel_page(ta->ta_start);
  456. }
  457. static inline void ipi_flush_tlb_range(void *arg)
  458. {
  459. struct tlb_args *ta = (struct tlb_args *)arg;
  460. local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
  461. }
  462. static inline void ipi_flush_tlb_kernel_range(void *arg)
  463. {
  464. struct tlb_args *ta = (struct tlb_args *)arg;
  465. local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
  466. }
  467. void flush_tlb_all(void)
  468. {
  469. on_each_cpu(ipi_flush_tlb_all, NULL, 1, 1);
  470. }
  471. void flush_tlb_mm(struct mm_struct *mm)
  472. {
  473. cpumask_t mask = mm->cpu_vm_mask;
  474. on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, 1, mask);
  475. }
  476. void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
  477. {
  478. cpumask_t mask = vma->vm_mm->cpu_vm_mask;
  479. struct tlb_args ta;
  480. ta.ta_vma = vma;
  481. ta.ta_start = uaddr;
  482. on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, 1, mask);
  483. }
  484. void flush_tlb_kernel_page(unsigned long kaddr)
  485. {
  486. struct tlb_args ta;
  487. ta.ta_start = kaddr;
  488. on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1, 1);
  489. }
  490. void flush_tlb_range(struct vm_area_struct *vma,
  491. unsigned long start, unsigned long end)
  492. {
  493. cpumask_t mask = vma->vm_mm->cpu_vm_mask;
  494. struct tlb_args ta;
  495. ta.ta_vma = vma;
  496. ta.ta_start = start;
  497. ta.ta_end = end;
  498. on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, 1, mask);
  499. }
  500. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  501. {
  502. struct tlb_args ta;
  503. ta.ta_start = start;
  504. ta.ta_end = end;
  505. on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1, 1);
  506. }