smp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/irq.h>
  13. #include <linux/delay.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/mc146818rtc.h>
  18. #include <linux/cache.h>
  19. #include <linux/interrupt.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. return APIC_DM_FIXED | shortcut | vector | APIC_DEST_LOGICAL;
  111. }
  112. static inline int __prepare_ICR2 (unsigned int mask)
  113. {
  114. return SET_APIC_DEST_FIELD(mask);
  115. }
  116. void __send_IPI_shortcut(unsigned int shortcut, int vector)
  117. {
  118. /*
  119. * Subtle. In the case of the 'never do double writes' workaround
  120. * we have to lock out interrupts to be safe. As we don't care
  121. * of the value read we use an atomic rmw access to avoid costly
  122. * cli/sti. Otherwise we use an even cheaper single atomic write
  123. * to the APIC.
  124. */
  125. unsigned int cfg;
  126. /*
  127. * Wait for idle.
  128. */
  129. apic_wait_icr_idle();
  130. /*
  131. * No need to touch the target chip field
  132. */
  133. cfg = __prepare_ICR(shortcut, vector);
  134. /*
  135. * Send the IPI. The write to APIC_ICR fires this off.
  136. */
  137. apic_write_around(APIC_ICR, cfg);
  138. }
  139. void fastcall send_IPI_self(int vector)
  140. {
  141. __send_IPI_shortcut(APIC_DEST_SELF, vector);
  142. }
  143. /*
  144. * This is only used on smaller machines.
  145. */
  146. void send_IPI_mask_bitmask(cpumask_t cpumask, int vector)
  147. {
  148. unsigned long mask = cpus_addr(cpumask)[0];
  149. unsigned long cfg;
  150. unsigned long flags;
  151. local_irq_save(flags);
  152. /*
  153. * Wait for idle.
  154. */
  155. apic_wait_icr_idle();
  156. /*
  157. * prepare target chip field
  158. */
  159. cfg = __prepare_ICR2(mask);
  160. apic_write_around(APIC_ICR2, cfg);
  161. /*
  162. * program the ICR
  163. */
  164. cfg = __prepare_ICR(0, vector);
  165. /*
  166. * Send the IPI. The write to APIC_ICR fires this off.
  167. */
  168. apic_write_around(APIC_ICR, cfg);
  169. local_irq_restore(flags);
  170. }
  171. void send_IPI_mask_sequence(cpumask_t mask, int vector)
  172. {
  173. unsigned long cfg, flags;
  174. unsigned int query_cpu;
  175. /*
  176. * Hack. The clustered APIC addressing mode doesn't allow us to send
  177. * to an arbitrary mask, so I do a unicasts to each CPU instead. This
  178. * should be modified to do 1 message per cluster ID - mbligh
  179. */
  180. local_irq_save(flags);
  181. for (query_cpu = 0; query_cpu < NR_CPUS; ++query_cpu) {
  182. if (cpu_isset(query_cpu, mask)) {
  183. /*
  184. * Wait for idle.
  185. */
  186. apic_wait_icr_idle();
  187. /*
  188. * prepare target chip field
  189. */
  190. cfg = __prepare_ICR2(cpu_to_logical_apicid(query_cpu));
  191. apic_write_around(APIC_ICR2, cfg);
  192. /*
  193. * program the ICR
  194. */
  195. cfg = __prepare_ICR(0, vector);
  196. /*
  197. * Send the IPI. The write to APIC_ICR fires this off.
  198. */
  199. apic_write_around(APIC_ICR, cfg);
  200. }
  201. }
  202. local_irq_restore(flags);
  203. }
  204. #include <mach_ipi.h> /* must come after the send_IPI functions above for inlining */
  205. /*
  206. * Smarter SMP flushing macros.
  207. * c/o Linus Torvalds.
  208. *
  209. * These mean you can really definitely utterly forget about
  210. * writing to user space from interrupts. (Its not allowed anyway).
  211. *
  212. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  213. */
  214. static cpumask_t flush_cpumask;
  215. static struct mm_struct * flush_mm;
  216. static unsigned long flush_va;
  217. static DEFINE_SPINLOCK(tlbstate_lock);
  218. #define FLUSH_ALL 0xffffffff
  219. /*
  220. * We cannot call mmdrop() because we are in interrupt context,
  221. * instead update mm->cpu_vm_mask.
  222. *
  223. * We need to reload %cr3 since the page tables may be going
  224. * away from under us..
  225. */
  226. static inline void leave_mm (unsigned long cpu)
  227. {
  228. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK)
  229. BUG();
  230. cpu_clear(cpu, per_cpu(cpu_tlbstate, cpu).active_mm->cpu_vm_mask);
  231. load_cr3(swapper_pg_dir);
  232. }
  233. /*
  234. *
  235. * The flush IPI assumes that a thread switch happens in this order:
  236. * [cpu0: the cpu that switches]
  237. * 1) switch_mm() either 1a) or 1b)
  238. * 1a) thread switch to a different mm
  239. * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
  240. * Stop ipi delivery for the old mm. This is not synchronized with
  241. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  242. * for the wrong mm, and in the worst case we perform a superflous
  243. * tlb flush.
  244. * 1a2) set cpu_tlbstate to TLBSTATE_OK
  245. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  246. * was in lazy tlb mode.
  247. * 1a3) update cpu_tlbstate[].active_mm
  248. * Now cpu0 accepts tlb flushes for the new mm.
  249. * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
  250. * Now the other cpus will send tlb flush ipis.
  251. * 1a4) change cr3.
  252. * 1b) thread switch without mm change
  253. * cpu_tlbstate[].active_mm is correct, cpu0 already handles
  254. * flush ipis.
  255. * 1b1) set cpu_tlbstate to TLBSTATE_OK
  256. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  257. * Atomically set the bit [other cpus will start sending flush ipis],
  258. * and test the bit.
  259. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  260. * 2) switch %%esp, ie current
  261. *
  262. * The interrupt must handle 2 special cases:
  263. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  264. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  265. * runs in kernel space, the cpu could load tlb entries for user space
  266. * pages.
  267. *
  268. * The good news is that cpu_tlbstate is local to each cpu, no
  269. * write/read ordering problems.
  270. */
  271. /*
  272. * TLB flush IPI:
  273. *
  274. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  275. * 2) Leave the mm if we are in the lazy tlb mode.
  276. */
  277. fastcall void smp_invalidate_interrupt(struct pt_regs *regs)
  278. {
  279. unsigned long cpu;
  280. cpu = get_cpu();
  281. if (!cpu_isset(cpu, flush_cpumask))
  282. goto out;
  283. /*
  284. * This was a BUG() but until someone can quote me the
  285. * line from the intel manual that guarantees an IPI to
  286. * multiple CPUs is retried _only_ on the erroring CPUs
  287. * its staying as a return
  288. *
  289. * BUG();
  290. */
  291. if (flush_mm == per_cpu(cpu_tlbstate, cpu).active_mm) {
  292. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK) {
  293. if (flush_va == FLUSH_ALL)
  294. local_flush_tlb();
  295. else
  296. __flush_tlb_one(flush_va);
  297. } else
  298. leave_mm(cpu);
  299. }
  300. ack_APIC_irq();
  301. smp_mb__before_clear_bit();
  302. cpu_clear(cpu, flush_cpumask);
  303. smp_mb__after_clear_bit();
  304. out:
  305. put_cpu_no_resched();
  306. }
  307. static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  308. unsigned long va)
  309. {
  310. cpumask_t tmp;
  311. /*
  312. * A couple of (to be removed) sanity checks:
  313. *
  314. * - we do not send IPIs to not-yet booted CPUs.
  315. * - current CPU must not be in mask
  316. * - mask must exist :)
  317. */
  318. BUG_ON(cpus_empty(cpumask));
  319. cpus_and(tmp, cpumask, cpu_online_map);
  320. BUG_ON(!cpus_equal(cpumask, tmp));
  321. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  322. BUG_ON(!mm);
  323. /*
  324. * i'm not happy about this global shared spinlock in the
  325. * MM hot path, but we'll see how contended it is.
  326. * Temporarily this turns IRQs off, so that lockups are
  327. * detected by the NMI watchdog.
  328. */
  329. spin_lock(&tlbstate_lock);
  330. flush_mm = mm;
  331. flush_va = va;
  332. #if NR_CPUS <= BITS_PER_LONG
  333. atomic_set_mask(cpumask, &flush_cpumask);
  334. #else
  335. {
  336. int k;
  337. unsigned long *flush_mask = (unsigned long *)&flush_cpumask;
  338. unsigned long *cpu_mask = (unsigned long *)&cpumask;
  339. for (k = 0; k < BITS_TO_LONGS(NR_CPUS); ++k)
  340. atomic_set_mask(cpu_mask[k], &flush_mask[k]);
  341. }
  342. #endif
  343. /*
  344. * We have to send the IPI only to
  345. * CPUs affected.
  346. */
  347. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR);
  348. while (!cpus_empty(flush_cpumask))
  349. /* nothing. lockup detection does not belong here */
  350. mb();
  351. flush_mm = NULL;
  352. flush_va = 0;
  353. spin_unlock(&tlbstate_lock);
  354. }
  355. void flush_tlb_current_task(void)
  356. {
  357. struct mm_struct *mm = current->mm;
  358. cpumask_t cpu_mask;
  359. preempt_disable();
  360. cpu_mask = mm->cpu_vm_mask;
  361. cpu_clear(smp_processor_id(), cpu_mask);
  362. local_flush_tlb();
  363. if (!cpus_empty(cpu_mask))
  364. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  365. preempt_enable();
  366. }
  367. void flush_tlb_mm (struct mm_struct * mm)
  368. {
  369. cpumask_t cpu_mask;
  370. preempt_disable();
  371. cpu_mask = mm->cpu_vm_mask;
  372. cpu_clear(smp_processor_id(), cpu_mask);
  373. if (current->active_mm == mm) {
  374. if (current->mm)
  375. local_flush_tlb();
  376. else
  377. leave_mm(smp_processor_id());
  378. }
  379. if (!cpus_empty(cpu_mask))
  380. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  381. preempt_enable();
  382. }
  383. void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
  384. {
  385. struct mm_struct *mm = vma->vm_mm;
  386. cpumask_t cpu_mask;
  387. preempt_disable();
  388. cpu_mask = mm->cpu_vm_mask;
  389. cpu_clear(smp_processor_id(), cpu_mask);
  390. if (current->active_mm == mm) {
  391. if(current->mm)
  392. __flush_tlb_one(va);
  393. else
  394. leave_mm(smp_processor_id());
  395. }
  396. if (!cpus_empty(cpu_mask))
  397. flush_tlb_others(cpu_mask, mm, va);
  398. preempt_enable();
  399. }
  400. EXPORT_SYMBOL(flush_tlb_page);
  401. static void do_flush_tlb_all(void* info)
  402. {
  403. unsigned long cpu = smp_processor_id();
  404. __flush_tlb_all();
  405. if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_LAZY)
  406. leave_mm(cpu);
  407. }
  408. void flush_tlb_all(void)
  409. {
  410. on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
  411. }
  412. /*
  413. * this function sends a 'reschedule' IPI to another CPU.
  414. * it goes straight through and wastes no time serializing
  415. * anything. Worst case is that we lose a reschedule ...
  416. */
  417. void smp_send_reschedule(int cpu)
  418. {
  419. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  420. }
  421. /*
  422. * Structure and data for smp_call_function(). This is designed to minimise
  423. * static memory requirements. It also looks cleaner.
  424. */
  425. static DEFINE_SPINLOCK(call_lock);
  426. struct call_data_struct {
  427. void (*func) (void *info);
  428. void *info;
  429. atomic_t started;
  430. atomic_t finished;
  431. int wait;
  432. };
  433. static struct call_data_struct * call_data;
  434. /*
  435. * this function sends a 'generic call function' IPI to all other CPUs
  436. * in the system.
  437. */
  438. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  439. int wait)
  440. /*
  441. * [SUMMARY] Run a function on all other CPUs.
  442. * <func> The function to run. This must be fast and non-blocking.
  443. * <info> An arbitrary pointer to pass to the function.
  444. * <nonatomic> currently unused.
  445. * <wait> If true, wait (atomically) until function has completed on other CPUs.
  446. * [RETURNS] 0 on success, else a negative status code. Does not return until
  447. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  448. *
  449. * You must not call this function with disabled interrupts or from a
  450. * hardware interrupt handler or from a bottom half handler.
  451. */
  452. {
  453. struct call_data_struct data;
  454. int cpus = num_online_cpus()-1;
  455. if (!cpus)
  456. return 0;
  457. /* Can deadlock when called with interrupts disabled */
  458. WARN_ON(irqs_disabled());
  459. data.func = func;
  460. data.info = info;
  461. atomic_set(&data.started, 0);
  462. data.wait = wait;
  463. if (wait)
  464. atomic_set(&data.finished, 0);
  465. spin_lock(&call_lock);
  466. call_data = &data;
  467. mb();
  468. /* Send a message to all other CPUs and wait for them to respond */
  469. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  470. /* Wait for response */
  471. while (atomic_read(&data.started) != cpus)
  472. cpu_relax();
  473. if (wait)
  474. while (atomic_read(&data.finished) != cpus)
  475. cpu_relax();
  476. spin_unlock(&call_lock);
  477. return 0;
  478. }
  479. EXPORT_SYMBOL(smp_call_function);
  480. static void stop_this_cpu (void * dummy)
  481. {
  482. /*
  483. * Remove this CPU:
  484. */
  485. cpu_clear(smp_processor_id(), cpu_online_map);
  486. local_irq_disable();
  487. disable_local_APIC();
  488. if (cpu_data[smp_processor_id()].hlt_works_ok)
  489. for(;;) __asm__("hlt");
  490. for (;;);
  491. }
  492. /*
  493. * this function calls the 'stop' function on all other CPUs in the system.
  494. */
  495. void smp_send_stop(void)
  496. {
  497. smp_call_function(stop_this_cpu, NULL, 1, 0);
  498. local_irq_disable();
  499. disable_local_APIC();
  500. local_irq_enable();
  501. }
  502. /*
  503. * Reschedule call back. Nothing to do,
  504. * all the work is done automatically when
  505. * we return from the interrupt.
  506. */
  507. fastcall void smp_reschedule_interrupt(struct pt_regs *regs)
  508. {
  509. ack_APIC_irq();
  510. }
  511. fastcall void smp_call_function_interrupt(struct pt_regs *regs)
  512. {
  513. void (*func) (void *info) = call_data->func;
  514. void *info = call_data->info;
  515. int wait = call_data->wait;
  516. ack_APIC_irq();
  517. /*
  518. * Notify initiating CPU that I've grabbed the data and am
  519. * about to execute the function
  520. */
  521. mb();
  522. atomic_inc(&call_data->started);
  523. /*
  524. * At this point the info structure may be out of scope unless wait==1
  525. */
  526. irq_enter();
  527. (*func)(info);
  528. irq_exit();
  529. if (wait) {
  530. mb();
  531. atomic_inc(&call_data->finished);
  532. }
  533. }