smp.c 13 KB

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