smp.c 18 KB

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