smp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * Intel SMP support routines.
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  5. * (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * This code is released under the GNU General Public License version 2 or
  8. * later.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/mm.h>
  12. #include <linux/delay.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/mc146818rtc.h>
  17. #include <linux/cache.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/cpu.h>
  20. #include <linux/module.h>
  21. #include <asm/mtrr.h>
  22. #include <asm/tlbflush.h>
  23. #include <mach_apic.h>
  24. /*
  25. * Some notes on x86 processor bugs affecting SMP operation:
  26. *
  27. * Pentium, Pentium Pro, II, III (and all CPUs) have bugs.
  28. * The Linux implications for SMP are handled as follows:
  29. *
  30. * Pentium III / [Xeon]
  31. * None of the E1AP-E3AP errata are visible to the user.
  32. *
  33. * E1AP. see PII A1AP
  34. * E2AP. see PII A2AP
  35. * E3AP. see PII A3AP
  36. *
  37. * Pentium II / [Xeon]
  38. * None of the A1AP-A3AP errata are visible to the user.
  39. *
  40. * A1AP. see PPro 1AP
  41. * A2AP. see PPro 2AP
  42. * A3AP. see PPro 7AP
  43. *
  44. * Pentium Pro
  45. * None of 1AP-9AP errata are visible to the normal user,
  46. * except occasional delivery of 'spurious interrupt' as trap #15.
  47. * This is very rare and a non-problem.
  48. *
  49. * 1AP. Linux maps APIC as non-cacheable
  50. * 2AP. worked around in hardware
  51. * 3AP. fixed in C0 and above steppings microcode update.
  52. * Linux does not use excessive STARTUP_IPIs.
  53. * 4AP. worked around in hardware
  54. * 5AP. symmetric IO mode (normal Linux operation) not affected.
  55. * 'noapic' mode has vector 0xf filled out properly.
  56. * 6AP. 'noapic' mode might be affected - fixed in later steppings
  57. * 7AP. We do not assume writes to the LVT deassering IRQs
  58. * 8AP. We do not enable low power mode (deep sleep) during MP bootup
  59. * 9AP. We do not use mixed mode
  60. *
  61. * Pentium
  62. * There is a marginal case where REP MOVS on 100MHz SMP
  63. * machines with B stepping processors can fail. XXX should provide
  64. * an L1cache=Writethrough or L1cache=off option.
  65. *
  66. * B stepping CPUs may hang. There are hardware work arounds
  67. * for this. We warn about it in case your board doesn't have the work
  68. * arounds. Basically thats so I can tell anyone with a B stepping
  69. * CPU and SMP problems "tough".
  70. *
  71. * Specific items [From Pentium Processor Specification Update]
  72. *
  73. * 1AP. Linux doesn't use remote read
  74. * 2AP. Linux doesn't trust APIC errors
  75. * 3AP. We work around this
  76. * 4AP. Linux never generated 3 interrupts of the same priority
  77. * to cause a lost local interrupt.
  78. * 5AP. Remote read is never used
  79. * 6AP. not affected - worked around in hardware
  80. * 7AP. not affected - worked around in hardware
  81. * 8AP. worked around in hardware - we get explicit CS errors if not
  82. * 9AP. only 'noapic' mode affected. Might generate spurious
  83. * interrupts, we log only the first one and count the
  84. * rest silently.
  85. * 10AP. not affected - worked around in hardware
  86. * 11AP. Linux reads the APIC between writes to avoid this, as per
  87. * the documentation. Make sure you preserve this as it affects
  88. * the C stepping chips too.
  89. * 12AP. not affected - worked around in hardware
  90. * 13AP. not affected - worked around in hardware
  91. * 14AP. we always deassert INIT during bootup
  92. * 15AP. not affected - worked around in hardware
  93. * 16AP. not affected - worked around in hardware
  94. * 17AP. not affected - worked around in hardware
  95. * 18AP. not affected - worked around in hardware
  96. * 19AP. not affected - worked around in BIOS
  97. *
  98. * If this sounds worrying believe me these bugs are either ___RARE___,
  99. * or are signal timing bugs worked around in hardware and there's
  100. * about nothing of note with C stepping upwards.
  101. */
  102. DEFINE_PER_CPU(struct tlb_state, cpu_tlbstate) ____cacheline_aligned = { &init_mm, 0, };
  103. /*
  104. * the following functions deal with sending IPIs between CPUs.
  105. *
  106. * We use 'broadcast', CPU->CPU IPIs and self-IPIs too.
  107. */
  108. static inline int __prepare_ICR (unsigned int shortcut, int vector)
  109. {
  110. unsigned int icr = shortcut | APIC_DEST_LOGICAL;
  111. switch (vector) {
  112. default:
  113. icr |= APIC_DM_FIXED | vector;
  114. break;
  115. case NMI_VECTOR:
  116. icr |= APIC_DM_NMI;
  117. break;
  118. }
  119. return icr;
  120. }
  121. static inline int __prepare_ICR2 (unsigned int mask)
  122. {
  123. return SET_APIC_DEST_FIELD(mask);
  124. }
  125. void __send_IPI_shortcut(unsigned int shortcut, int vector)
  126. {
  127. /*
  128. * Subtle. In the case of the 'never do double writes' workaround
  129. * we have to lock out interrupts to be safe. As we don't care
  130. * of the value read we use an atomic rmw access to avoid costly
  131. * cli/sti. Otherwise we use an even cheaper single atomic write
  132. * to the APIC.
  133. */
  134. unsigned int cfg;
  135. /*
  136. * Wait for idle.
  137. */
  138. apic_wait_icr_idle();
  139. /*
  140. * No need to touch the target chip field
  141. */
  142. cfg = __prepare_ICR(shortcut, vector);
  143. /*
  144. * Send the IPI. The write to APIC_ICR fires this off.
  145. */
  146. apic_write_around(APIC_ICR, cfg);
  147. }
  148. void fastcall send_IPI_self(int vector)
  149. {
  150. __send_IPI_shortcut(APIC_DEST_SELF, vector);
  151. }
  152. /*
  153. * This is only used on smaller machines.
  154. */
  155. void send_IPI_mask_bitmask(cpumask_t cpumask, int vector)
  156. {
  157. unsigned long mask = cpus_addr(cpumask)[0];
  158. unsigned long cfg;
  159. unsigned long flags;
  160. local_irq_save(flags);
  161. WARN_ON(mask & ~cpus_addr(cpu_online_map)[0]);
  162. /*
  163. * Wait for idle.
  164. */
  165. apic_wait_icr_idle();
  166. /*
  167. * prepare target chip field
  168. */
  169. cfg = __prepare_ICR2(mask);
  170. apic_write_around(APIC_ICR2, cfg);
  171. /*
  172. * program the ICR
  173. */
  174. cfg = __prepare_ICR(0, vector);
  175. /*
  176. * Send the IPI. The write to APIC_ICR fires this off.
  177. */
  178. apic_write_around(APIC_ICR, cfg);
  179. local_irq_restore(flags);
  180. }
  181. void send_IPI_mask_sequence(cpumask_t mask, int vector)
  182. {
  183. unsigned long cfg, flags;
  184. unsigned int query_cpu;
  185. /*
  186. * Hack. The clustered APIC addressing mode doesn't allow us to send
  187. * to an arbitrary mask, so I do a unicasts to each CPU instead. This
  188. * should be modified to do 1 message per cluster ID - mbligh
  189. */
  190. local_irq_save(flags);
  191. for (query_cpu = 0; query_cpu < NR_CPUS; ++query_cpu) {
  192. if (cpu_isset(query_cpu, mask)) {
  193. /*
  194. * Wait for idle.
  195. */
  196. apic_wait_icr_idle();
  197. /*
  198. * prepare target chip field
  199. */
  200. cfg = __prepare_ICR2(cpu_to_logical_apicid(query_cpu));
  201. apic_write_around(APIC_ICR2, cfg);
  202. /*
  203. * program the ICR
  204. */
  205. cfg = __prepare_ICR(0, vector);
  206. /*
  207. * Send the IPI. The write to APIC_ICR fires this off.
  208. */
  209. apic_write_around(APIC_ICR, cfg);
  210. }
  211. }
  212. local_irq_restore(flags);
  213. }
  214. #include <mach_ipi.h> /* must come after the send_IPI functions above for inlining */
  215. /*
  216. * Smarter SMP flushing macros.
  217. * c/o Linus Torvalds.
  218. *
  219. * These mean you can really definitely utterly forget about
  220. * writing to user space from interrupts. (Its not allowed anyway).
  221. *
  222. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  223. */
  224. static cpumask_t flush_cpumask;
  225. static struct mm_struct * flush_mm;
  226. static unsigned long flush_va;
  227. static DEFINE_SPINLOCK(tlbstate_lock);
  228. #define FLUSH_ALL 0xffffffff
  229. /*
  230. * We cannot call mmdrop() because we are in interrupt context,
  231. * instead update mm->cpu_vm_mask.
  232. *
  233. * We need to reload %cr3 since the page tables may be going
  234. * away from under us..
  235. */
  236. static inline void leave_mm (unsigned long cpu)
  237. {
  238. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK)
  239. BUG();
  240. cpu_clear(cpu, per_cpu(cpu_tlbstate, cpu).active_mm->cpu_vm_mask);
  241. load_cr3(swapper_pg_dir);
  242. }
  243. /*
  244. *
  245. * The flush IPI assumes that a thread switch happens in this order:
  246. * [cpu0: the cpu that switches]
  247. * 1) switch_mm() either 1a) or 1b)
  248. * 1a) thread switch to a different mm
  249. * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
  250. * Stop ipi delivery for the old mm. This is not synchronized with
  251. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  252. * for the wrong mm, and in the worst case we perform a superflous
  253. * tlb flush.
  254. * 1a2) set cpu_tlbstate to TLBSTATE_OK
  255. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  256. * was in lazy tlb mode.
  257. * 1a3) update cpu_tlbstate[].active_mm
  258. * Now cpu0 accepts tlb flushes for the new mm.
  259. * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
  260. * Now the other cpus will send tlb flush ipis.
  261. * 1a4) change cr3.
  262. * 1b) thread switch without mm change
  263. * cpu_tlbstate[].active_mm is correct, cpu0 already handles
  264. * flush ipis.
  265. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  266. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  267. * Atomically set the bit [other cpus will start sending flush ipis],
  268. * and test the bit.
  269. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  270. * 2) switch %%esp, ie current
  271. *
  272. * The interrupt must handle 2 special cases:
  273. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  274. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  275. * runs in kernel space, the cpu could load tlb entries for user space
  276. * pages.
  277. *
  278. * The good news is that cpu_tlbstate is local to each cpu, no
  279. * write/read ordering problems.
  280. */
  281. /*
  282. * TLB flush IPI:
  283. *
  284. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  285. * 2) Leave the mm if we are in the lazy tlb mode.
  286. */
  287. fastcall void smp_invalidate_interrupt(struct pt_regs *regs)
  288. {
  289. struct pt_regs *old_regs = set_irq_regs(regs);
  290. unsigned long cpu;
  291. cpu = get_cpu();
  292. if (!cpu_isset(cpu, flush_cpumask))
  293. goto out;
  294. /*
  295. * This was a BUG() but until someone can quote me the
  296. * line from the intel manual that guarantees an IPI to
  297. * multiple CPUs is retried _only_ on the erroring CPUs
  298. * its staying as a return
  299. *
  300. * BUG();
  301. */
  302. if (flush_mm == per_cpu(cpu_tlbstate, cpu).active_mm) {
  303. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK) {
  304. if (flush_va == FLUSH_ALL)
  305. local_flush_tlb();
  306. else
  307. __flush_tlb_one(flush_va);
  308. } else
  309. leave_mm(cpu);
  310. }
  311. ack_APIC_irq();
  312. smp_mb__before_clear_bit();
  313. cpu_clear(cpu, flush_cpumask);
  314. smp_mb__after_clear_bit();
  315. out:
  316. put_cpu_no_resched();
  317. set_irq_regs(old_regs);
  318. }
  319. static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  320. unsigned long va)
  321. {
  322. /*
  323. * A couple of (to be removed) sanity checks:
  324. *
  325. * - current CPU must not be in mask
  326. * - mask must exist :)
  327. */
  328. BUG_ON(cpus_empty(cpumask));
  329. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  330. BUG_ON(!mm);
  331. /* If a CPU which we ran on has gone down, OK. */
  332. cpus_and(cpumask, cpumask, cpu_online_map);
  333. if (cpus_empty(cpumask))
  334. return;
  335. /*
  336. * i'm not happy about this global shared spinlock in the
  337. * MM hot path, but we'll see how contended it is.
  338. * Temporarily this turns IRQs off, so that lockups are
  339. * detected by the NMI watchdog.
  340. */
  341. spin_lock(&tlbstate_lock);
  342. flush_mm = mm;
  343. flush_va = va;
  344. #if NR_CPUS <= BITS_PER_LONG
  345. atomic_set_mask(cpumask, &flush_cpumask);
  346. #else
  347. {
  348. int k;
  349. unsigned long *flush_mask = (unsigned long *)&flush_cpumask;
  350. unsigned long *cpu_mask = (unsigned long *)&cpumask;
  351. for (k = 0; k < BITS_TO_LONGS(NR_CPUS); ++k)
  352. atomic_set_mask(cpu_mask[k], &flush_mask[k]);
  353. }
  354. #endif
  355. /*
  356. * We have to send the IPI only to
  357. * CPUs affected.
  358. */
  359. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR);
  360. while (!cpus_empty(flush_cpumask))
  361. /* nothing. lockup detection does not belong here */
  362. mb();
  363. flush_mm = NULL;
  364. flush_va = 0;
  365. spin_unlock(&tlbstate_lock);
  366. }
  367. void flush_tlb_current_task(void)
  368. {
  369. struct mm_struct *mm = current->mm;
  370. cpumask_t cpu_mask;
  371. preempt_disable();
  372. cpu_mask = mm->cpu_vm_mask;
  373. cpu_clear(smp_processor_id(), cpu_mask);
  374. local_flush_tlb();
  375. if (!cpus_empty(cpu_mask))
  376. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  377. preempt_enable();
  378. }
  379. void flush_tlb_mm (struct mm_struct * mm)
  380. {
  381. cpumask_t cpu_mask;
  382. preempt_disable();
  383. cpu_mask = mm->cpu_vm_mask;
  384. cpu_clear(smp_processor_id(), cpu_mask);
  385. if (current->active_mm == mm) {
  386. if (current->mm)
  387. local_flush_tlb();
  388. else
  389. leave_mm(smp_processor_id());
  390. }
  391. if (!cpus_empty(cpu_mask))
  392. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  393. preempt_enable();
  394. }
  395. void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
  396. {
  397. struct mm_struct *mm = vma->vm_mm;
  398. cpumask_t cpu_mask;
  399. preempt_disable();
  400. cpu_mask = mm->cpu_vm_mask;
  401. cpu_clear(smp_processor_id(), cpu_mask);
  402. if (current->active_mm == mm) {
  403. if(current->mm)
  404. __flush_tlb_one(va);
  405. else
  406. leave_mm(smp_processor_id());
  407. }
  408. if (!cpus_empty(cpu_mask))
  409. flush_tlb_others(cpu_mask, mm, va);
  410. preempt_enable();
  411. }
  412. EXPORT_SYMBOL(flush_tlb_page);
  413. static void do_flush_tlb_all(void* info)
  414. {
  415. unsigned long cpu = smp_processor_id();
  416. __flush_tlb_all();
  417. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_LAZY)
  418. leave_mm(cpu);
  419. }
  420. void flush_tlb_all(void)
  421. {
  422. on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
  423. }
  424. /*
  425. * this function sends a 'reschedule' IPI to another CPU.
  426. * it goes straight through and wastes no time serializing
  427. * anything. Worst case is that we lose a reschedule ...
  428. */
  429. void smp_send_reschedule(int cpu)
  430. {
  431. WARN_ON(cpu_is_offline(cpu));
  432. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  433. }
  434. /*
  435. * Structure and data for smp_call_function(). This is designed to minimise
  436. * static memory requirements. It also looks cleaner.
  437. */
  438. static DEFINE_SPINLOCK(call_lock);
  439. struct call_data_struct {
  440. void (*func) (void *info);
  441. void *info;
  442. atomic_t started;
  443. atomic_t finished;
  444. int wait;
  445. };
  446. void lock_ipi_call_lock(void)
  447. {
  448. spin_lock_irq(&call_lock);
  449. }
  450. void unlock_ipi_call_lock(void)
  451. {
  452. spin_unlock_irq(&call_lock);
  453. }
  454. static struct call_data_struct *call_data;
  455. /**
  456. * smp_call_function(): Run a function on all other CPUs.
  457. * @func: The function to run. This must be fast and non-blocking.
  458. * @info: An arbitrary pointer to pass to the function.
  459. * @nonatomic: currently unused.
  460. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  461. *
  462. * Returns 0 on success, else a negative status code. Does not return until
  463. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  464. *
  465. * You must not call this function with disabled interrupts or from a
  466. * hardware interrupt handler or from a bottom half handler.
  467. */
  468. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  469. int wait)
  470. {
  471. struct call_data_struct data;
  472. int cpus;
  473. /* Holding any lock stops cpus from going down. */
  474. spin_lock(&call_lock);
  475. cpus = num_online_cpus() - 1;
  476. if (!cpus) {
  477. spin_unlock(&call_lock);
  478. return 0;
  479. }
  480. /* Can deadlock when called with interrupts disabled */
  481. WARN_ON(irqs_disabled());
  482. data.func = func;
  483. data.info = info;
  484. atomic_set(&data.started, 0);
  485. data.wait = wait;
  486. if (wait)
  487. atomic_set(&data.finished, 0);
  488. call_data = &data;
  489. mb();
  490. /* Send a message to all other CPUs and wait for them to respond */
  491. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  492. /* Wait for response */
  493. while (atomic_read(&data.started) != cpus)
  494. cpu_relax();
  495. if (wait)
  496. while (atomic_read(&data.finished) != cpus)
  497. cpu_relax();
  498. spin_unlock(&call_lock);
  499. return 0;
  500. }
  501. EXPORT_SYMBOL(smp_call_function);
  502. static void stop_this_cpu (void * dummy)
  503. {
  504. /*
  505. * Remove this CPU:
  506. */
  507. cpu_clear(smp_processor_id(), cpu_online_map);
  508. local_irq_disable();
  509. disable_local_APIC();
  510. if (cpu_data[smp_processor_id()].hlt_works_ok)
  511. for(;;) halt();
  512. for (;;);
  513. }
  514. /*
  515. * this function calls the 'stop' function on all other CPUs in the system.
  516. */
  517. void smp_send_stop(void)
  518. {
  519. smp_call_function(stop_this_cpu, NULL, 1, 0);
  520. local_irq_disable();
  521. disable_local_APIC();
  522. local_irq_enable();
  523. }
  524. /*
  525. * Reschedule call back. Nothing to do,
  526. * all the work is done automatically when
  527. * we return from the interrupt.
  528. */
  529. fastcall void smp_reschedule_interrupt(struct pt_regs *regs)
  530. {
  531. struct pt_regs *old_regs = set_irq_regs(regs);
  532. ack_APIC_irq();
  533. set_irq_regs(old_regs);
  534. }
  535. fastcall void smp_call_function_interrupt(struct pt_regs *regs)
  536. {
  537. struct pt_regs *old_regs = set_irq_regs(regs);
  538. void (*func) (void *info) = call_data->func;
  539. void *info = call_data->info;
  540. int wait = call_data->wait;
  541. ack_APIC_irq();
  542. /*
  543. * Notify initiating CPU that I've grabbed the data and am
  544. * about to execute the function
  545. */
  546. mb();
  547. atomic_inc(&call_data->started);
  548. /*
  549. * At this point the info structure may be out of scope unless wait==1
  550. */
  551. irq_enter();
  552. (*func)(info);
  553. irq_exit();
  554. if (wait) {
  555. mb();
  556. atomic_inc(&call_data->finished);
  557. }
  558. set_irq_regs(old_regs);
  559. }
  560. /*
  561. * this function sends a 'generic call function' IPI to one other CPU
  562. * in the system.
  563. *
  564. * cpu is a standard Linux logical CPU number.
  565. */
  566. static void
  567. __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  568. int nonatomic, int wait)
  569. {
  570. struct call_data_struct data;
  571. int cpus = 1;
  572. data.func = func;
  573. data.info = info;
  574. atomic_set(&data.started, 0);
  575. data.wait = wait;
  576. if (wait)
  577. atomic_set(&data.finished, 0);
  578. call_data = &data;
  579. wmb();
  580. /* Send a message to all other CPUs and wait for them to respond */
  581. send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
  582. /* Wait for response */
  583. while (atomic_read(&data.started) != cpus)
  584. cpu_relax();
  585. if (!wait)
  586. return;
  587. while (atomic_read(&data.finished) != cpus)
  588. cpu_relax();
  589. }
  590. /*
  591. * smp_call_function_single - Run a function on another CPU
  592. * @func: The function to run. This must be fast and non-blocking.
  593. * @info: An arbitrary pointer to pass to the function.
  594. * @nonatomic: Currently unused.
  595. * @wait: If true, wait until function has completed on other CPUs.
  596. *
  597. * Retrurns 0 on success, else a negative status code.
  598. *
  599. * Does not return until the remote CPU is nearly ready to execute <func>
  600. * or is or has executed.
  601. */
  602. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  603. int nonatomic, int wait)
  604. {
  605. /* prevent preemption and reschedule on another processor */
  606. int me = get_cpu();
  607. if (cpu == me) {
  608. WARN_ON(1);
  609. put_cpu();
  610. return -EBUSY;
  611. }
  612. spin_lock_bh(&call_lock);
  613. __smp_call_function_single(cpu, func, info, nonatomic, wait);
  614. spin_unlock_bh(&call_lock);
  615. put_cpu();
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(smp_call_function_single);
  619. static int convert_apicid_to_cpu(int apic_id)
  620. {
  621. int i;
  622. for (i = 0; i < NR_CPUS; i++) {
  623. if (x86_cpu_to_apicid[i] == apic_id)
  624. return i;
  625. }
  626. return -1;
  627. }
  628. int safe_smp_processor_id(void)
  629. {
  630. int apicid, cpuid;
  631. if (!boot_cpu_has(X86_FEATURE_APIC))
  632. return 0;
  633. apicid = hard_smp_processor_id();
  634. if (apicid == BAD_APICID)
  635. return 0;
  636. cpuid = convert_apicid_to_cpu(apicid);
  637. return cpuid >= 0 ? cpuid : 0;
  638. }