smp.c 13 KB

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