smp.c 13 KB

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