smp.c 15 KB

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