smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. * (c) 2002,2003 Andi Kleen, SuSE Labs.
  7. *
  8. * This code is released under the GNU General Public License version 2 or
  9. * later.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/irq.h>
  14. #include <linux/delay.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mc146818rtc.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/mtrr.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mach_apic.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/proto.h>
  27. #include <asm/apicdef.h>
  28. #define __cpuinit __init
  29. /*
  30. * Smarter SMP flushing macros.
  31. * c/o Linus Torvalds.
  32. *
  33. * These mean you can really definitely utterly forget about
  34. * writing to user space from interrupts. (Its not allowed anyway).
  35. *
  36. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  37. *
  38. * More scalable flush, from Andi Kleen
  39. *
  40. * To avoid global state use 8 different call vectors.
  41. * Each CPU uses a specific vector to trigger flushes on other
  42. * CPUs. Depending on the received vector the target CPUs look into
  43. * the right per cpu variable for the flush data.
  44. *
  45. * With more than 8 CPUs they are hashed to the 8 available
  46. * vectors. The limited global vector space forces us to this right now.
  47. * In future when interrupts are split into per CPU domains this could be
  48. * fixed, at the cost of triggering multiple IPIs in some cases.
  49. */
  50. union smp_flush_state {
  51. struct {
  52. cpumask_t flush_cpumask;
  53. struct mm_struct *flush_mm;
  54. unsigned long flush_va;
  55. #define FLUSH_ALL -1ULL
  56. spinlock_t tlbstate_lock;
  57. };
  58. char pad[SMP_CACHE_BYTES];
  59. } ____cacheline_aligned;
  60. /* State is put into the per CPU data section, but padded
  61. to a full cache line because other CPUs can access it and we don't
  62. want false sharing in the per cpu data segment. */
  63. static DEFINE_PER_CPU(union smp_flush_state, flush_state);
  64. /*
  65. * We cannot call mmdrop() because we are in interrupt context,
  66. * instead update mm->cpu_vm_mask.
  67. */
  68. static inline void leave_mm(int cpu)
  69. {
  70. if (read_pda(mmu_state) == TLBSTATE_OK)
  71. BUG();
  72. clear_bit(cpu, &read_pda(active_mm)->cpu_vm_mask);
  73. load_cr3(swapper_pg_dir);
  74. }
  75. /*
  76. *
  77. * The flush IPI assumes that a thread switch happens in this order:
  78. * [cpu0: the cpu that switches]
  79. * 1) switch_mm() either 1a) or 1b)
  80. * 1a) thread switch to a different mm
  81. * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
  82. * Stop ipi delivery for the old mm. This is not synchronized with
  83. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  84. * for the wrong mm, and in the worst case we perform a superfluous
  85. * tlb flush.
  86. * 1a2) set cpu mmu_state to TLBSTATE_OK
  87. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  88. * was in lazy tlb mode.
  89. * 1a3) update cpu active_mm
  90. * Now cpu0 accepts tlb flushes for the new mm.
  91. * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
  92. * Now the other cpus will send tlb flush ipis.
  93. * 1a4) change cr3.
  94. * 1b) thread switch without mm change
  95. * cpu active_mm is correct, cpu0 already handles
  96. * flush ipis.
  97. * 1b1) set cpu mmu_state to TLBSTATE_OK
  98. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  99. * Atomically set the bit [other cpus will start sending flush ipis],
  100. * and test the bit.
  101. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  102. * 2) switch %%esp, ie current
  103. *
  104. * The interrupt must handle 2 special cases:
  105. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  106. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  107. * runs in kernel space, the cpu could load tlb entries for user space
  108. * pages.
  109. *
  110. * The good news is that cpu mmu_state is local to each cpu, no
  111. * write/read ordering problems.
  112. */
  113. /*
  114. * TLB flush IPI:
  115. *
  116. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  117. * 2) Leave the mm if we are in the lazy tlb mode.
  118. *
  119. * Interrupts are disabled.
  120. */
  121. asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
  122. {
  123. int cpu;
  124. int sender;
  125. union smp_flush_state *f;
  126. cpu = smp_processor_id();
  127. /*
  128. * orig_rax contains the interrupt vector - 256.
  129. * Use that to determine where the sender put the data.
  130. */
  131. sender = regs->orig_rax + 256 - INVALIDATE_TLB_VECTOR_START;
  132. f = &per_cpu(flush_state, sender);
  133. if (!cpu_isset(cpu, f->flush_cpumask))
  134. goto out;
  135. /*
  136. * This was a BUG() but until someone can quote me the
  137. * line from the intel manual that guarantees an IPI to
  138. * multiple CPUs is retried _only_ on the erroring CPUs
  139. * its staying as a return
  140. *
  141. * BUG();
  142. */
  143. if (f->flush_mm == read_pda(active_mm)) {
  144. if (read_pda(mmu_state) == TLBSTATE_OK) {
  145. if (f->flush_va == FLUSH_ALL)
  146. local_flush_tlb();
  147. else
  148. __flush_tlb_one(f->flush_va);
  149. } else
  150. leave_mm(cpu);
  151. }
  152. out:
  153. ack_APIC_irq();
  154. cpu_clear(cpu, f->flush_cpumask);
  155. }
  156. static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  157. unsigned long va)
  158. {
  159. int sender;
  160. union smp_flush_state *f;
  161. /* Caller has disabled preemption */
  162. sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
  163. f = &per_cpu(flush_state, sender);
  164. /* Could avoid this lock when
  165. num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
  166. probably not worth checking this for a cache-hot lock. */
  167. spin_lock(&f->tlbstate_lock);
  168. f->flush_mm = mm;
  169. f->flush_va = va;
  170. cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
  171. /*
  172. * We have to send the IPI only to
  173. * CPUs affected.
  174. */
  175. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
  176. while (!cpus_empty(f->flush_cpumask))
  177. cpu_relax();
  178. f->flush_mm = NULL;
  179. f->flush_va = 0;
  180. spin_unlock(&f->tlbstate_lock);
  181. }
  182. int __cpuinit init_smp_flush(void)
  183. {
  184. int i;
  185. for_each_cpu_mask(i, cpu_possible_map) {
  186. spin_lock_init(&per_cpu(flush_state.tlbstate_lock, i));
  187. }
  188. return 0;
  189. }
  190. core_initcall(init_smp_flush);
  191. void flush_tlb_current_task(void)
  192. {
  193. struct mm_struct *mm = current->mm;
  194. cpumask_t cpu_mask;
  195. preempt_disable();
  196. cpu_mask = mm->cpu_vm_mask;
  197. cpu_clear(smp_processor_id(), cpu_mask);
  198. local_flush_tlb();
  199. if (!cpus_empty(cpu_mask))
  200. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  201. preempt_enable();
  202. }
  203. void flush_tlb_mm (struct mm_struct * mm)
  204. {
  205. cpumask_t cpu_mask;
  206. preempt_disable();
  207. cpu_mask = mm->cpu_vm_mask;
  208. cpu_clear(smp_processor_id(), cpu_mask);
  209. if (current->active_mm == mm) {
  210. if (current->mm)
  211. local_flush_tlb();
  212. else
  213. leave_mm(smp_processor_id());
  214. }
  215. if (!cpus_empty(cpu_mask))
  216. flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
  217. preempt_enable();
  218. }
  219. void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
  220. {
  221. struct mm_struct *mm = vma->vm_mm;
  222. cpumask_t cpu_mask;
  223. preempt_disable();
  224. cpu_mask = mm->cpu_vm_mask;
  225. cpu_clear(smp_processor_id(), cpu_mask);
  226. if (current->active_mm == mm) {
  227. if(current->mm)
  228. __flush_tlb_one(va);
  229. else
  230. leave_mm(smp_processor_id());
  231. }
  232. if (!cpus_empty(cpu_mask))
  233. flush_tlb_others(cpu_mask, mm, va);
  234. preempt_enable();
  235. }
  236. static void do_flush_tlb_all(void* info)
  237. {
  238. unsigned long cpu = smp_processor_id();
  239. __flush_tlb_all();
  240. if (read_pda(mmu_state) == TLBSTATE_LAZY)
  241. leave_mm(cpu);
  242. }
  243. void flush_tlb_all(void)
  244. {
  245. on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
  246. }
  247. void smp_kdb_stop(void)
  248. {
  249. send_IPI_allbutself(KDB_VECTOR);
  250. }
  251. /*
  252. * this function sends a 'reschedule' IPI to another CPU.
  253. * it goes straight through and wastes no time serializing
  254. * anything. Worst case is that we lose a reschedule ...
  255. */
  256. void smp_send_reschedule(int cpu)
  257. {
  258. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  259. }
  260. /*
  261. * Structure and data for smp_call_function(). This is designed to minimise
  262. * static memory requirements. It also looks cleaner.
  263. */
  264. static DEFINE_SPINLOCK(call_lock);
  265. struct call_data_struct {
  266. void (*func) (void *info);
  267. void *info;
  268. atomic_t started;
  269. atomic_t finished;
  270. int wait;
  271. };
  272. static struct call_data_struct * call_data;
  273. void lock_ipi_call_lock(void)
  274. {
  275. spin_lock_irq(&call_lock);
  276. }
  277. void unlock_ipi_call_lock(void)
  278. {
  279. spin_unlock_irq(&call_lock);
  280. }
  281. /*
  282. * this function sends a 'generic call function' IPI to one other CPU
  283. * in the system.
  284. *
  285. * cpu is a standard Linux logical CPU number.
  286. */
  287. static void
  288. __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  289. int nonatomic, int wait)
  290. {
  291. struct call_data_struct data;
  292. int cpus = 1;
  293. data.func = func;
  294. data.info = info;
  295. atomic_set(&data.started, 0);
  296. data.wait = wait;
  297. if (wait)
  298. atomic_set(&data.finished, 0);
  299. call_data = &data;
  300. wmb();
  301. /* Send a message to all other CPUs and wait for them to respond */
  302. send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
  303. /* Wait for response */
  304. while (atomic_read(&data.started) != cpus)
  305. cpu_relax();
  306. if (!wait)
  307. return;
  308. while (atomic_read(&data.finished) != cpus)
  309. cpu_relax();
  310. }
  311. /*
  312. * smp_call_function_single - Run a function on another CPU
  313. * @func: The function to run. This must be fast and non-blocking.
  314. * @info: An arbitrary pointer to pass to the function.
  315. * @nonatomic: Currently unused.
  316. * @wait: If true, wait until function has completed on other CPUs.
  317. *
  318. * Retrurns 0 on success, else a negative status code.
  319. *
  320. * Does not return until the remote CPU is nearly ready to execute <func>
  321. * or is or has executed.
  322. */
  323. int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
  324. int nonatomic, int wait)
  325. {
  326. /* prevent preemption and reschedule on another processor */
  327. int me = get_cpu();
  328. if (cpu == me) {
  329. WARN_ON(1);
  330. put_cpu();
  331. return -EBUSY;
  332. }
  333. spin_lock_bh(&call_lock);
  334. __smp_call_function_single(cpu, func, info, nonatomic, wait);
  335. spin_unlock_bh(&call_lock);
  336. put_cpu();
  337. return 0;
  338. }
  339. /*
  340. * this function sends a 'generic call function' IPI to all other CPUs
  341. * in the system.
  342. */
  343. static void __smp_call_function (void (*func) (void *info), void *info,
  344. int nonatomic, int wait)
  345. {
  346. struct call_data_struct data;
  347. int cpus = num_online_cpus()-1;
  348. if (!cpus)
  349. return;
  350. data.func = func;
  351. data.info = info;
  352. atomic_set(&data.started, 0);
  353. data.wait = wait;
  354. if (wait)
  355. atomic_set(&data.finished, 0);
  356. call_data = &data;
  357. wmb();
  358. /* Send a message to all other CPUs and wait for them to respond */
  359. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  360. /* Wait for response */
  361. while (atomic_read(&data.started) != cpus)
  362. cpu_relax();
  363. if (!wait)
  364. return;
  365. while (atomic_read(&data.finished) != cpus)
  366. cpu_relax();
  367. }
  368. /*
  369. * smp_call_function - run a function on all other CPUs.
  370. * @func: The function to run. This must be fast and non-blocking.
  371. * @info: An arbitrary pointer to pass to the function.
  372. * @nonatomic: currently unused.
  373. * @wait: If true, wait (atomically) until function has completed on other
  374. * CPUs.
  375. *
  376. * Returns 0 on success, else a negative status code. Does not return until
  377. * remote CPUs are nearly ready to execute func or are or have executed.
  378. *
  379. * You must not call this function with disabled interrupts or from a
  380. * hardware interrupt handler or from a bottom half handler.
  381. * Actually there are a few legal cases, like panic.
  382. */
  383. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  384. int wait)
  385. {
  386. spin_lock(&call_lock);
  387. __smp_call_function(func,info,nonatomic,wait);
  388. spin_unlock(&call_lock);
  389. return 0;
  390. }
  391. void smp_stop_cpu(void)
  392. {
  393. /*
  394. * Remove this CPU:
  395. */
  396. cpu_clear(smp_processor_id(), cpu_online_map);
  397. local_irq_disable();
  398. disable_local_APIC();
  399. local_irq_enable();
  400. }
  401. static void smp_really_stop_cpu(void *dummy)
  402. {
  403. smp_stop_cpu();
  404. for (;;)
  405. asm("hlt");
  406. }
  407. void smp_send_stop(void)
  408. {
  409. int nolock = 0;
  410. if (reboot_force)
  411. return;
  412. /* Don't deadlock on the call lock in panic */
  413. if (!spin_trylock(&call_lock)) {
  414. /* ignore locking because we have paniced anyways */
  415. nolock = 1;
  416. }
  417. __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
  418. if (!nolock)
  419. spin_unlock(&call_lock);
  420. local_irq_disable();
  421. disable_local_APIC();
  422. local_irq_enable();
  423. }
  424. /*
  425. * Reschedule call back. Nothing to do,
  426. * all the work is done automatically when
  427. * we return from the interrupt.
  428. */
  429. asmlinkage void smp_reschedule_interrupt(void)
  430. {
  431. ack_APIC_irq();
  432. }
  433. asmlinkage void smp_call_function_interrupt(void)
  434. {
  435. void (*func) (void *info) = call_data->func;
  436. void *info = call_data->info;
  437. int wait = call_data->wait;
  438. ack_APIC_irq();
  439. /*
  440. * Notify initiating CPU that I've grabbed the data and am
  441. * about to execute the function
  442. */
  443. mb();
  444. atomic_inc(&call_data->started);
  445. /*
  446. * At this point the info structure may be out of scope unless wait==1
  447. */
  448. irq_enter();
  449. (*func)(info);
  450. irq_exit();
  451. if (wait) {
  452. mb();
  453. atomic_inc(&call_data->finished);
  454. }
  455. }
  456. int safe_smp_processor_id(void)
  457. {
  458. int apicid, i;
  459. if (disable_apic)
  460. return 0;
  461. apicid = hard_smp_processor_id();
  462. if (x86_cpu_to_apicid[apicid] == apicid)
  463. return apicid;
  464. for (i = 0; i < NR_CPUS; ++i) {
  465. if (x86_cpu_to_apicid[i] == apicid)
  466. return i;
  467. }
  468. /* No entries in x86_cpu_to_apicid? Either no MPS|ACPI,
  469. * or called too early. Either way, we must be CPU 0. */
  470. if (x86_cpu_to_apicid[0] == BAD_APICID)
  471. return 0;
  472. return 0; /* Should not happen */
  473. }