smp.c 13 KB

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