tlb.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * TLB support routines.
  3. *
  4. * Copyright (C) 1998-2001, 2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. *
  7. * 08/02/00 A. Mallick <asit.k.mallick@intel.com>
  8. * Modified RID allocation for SMP
  9. * Goutham Rao <goutham.rao@intel.com>
  10. * IPI based ptc implementation and A-step IPI implementation.
  11. * Rohit Seth <rohit.seth@intel.com>
  12. * Ken Chen <kenneth.w.chen@intel.com>
  13. * Christophe de Dinechin <ddd@hp.com>: Avoid ptc.e on memory allocation
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/smp.h>
  20. #include <linux/mm.h>
  21. #include <linux/bootmem.h>
  22. #include <asm/delay.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/pal.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/dma.h>
  28. static struct {
  29. unsigned long mask; /* mask of supported purge page-sizes */
  30. unsigned long max_bits; /* log2 of largest supported purge page-size */
  31. } purge;
  32. struct ia64_ctx ia64_ctx = {
  33. .lock = __SPIN_LOCK_UNLOCKED(ia64_ctx.lock),
  34. .next = 1,
  35. .max_ctx = ~0U
  36. };
  37. DEFINE_PER_CPU(u8, ia64_need_tlb_flush);
  38. /*
  39. * Initializes the ia64_ctx.bitmap array based on max_ctx+1.
  40. * Called after cpu_init() has setup ia64_ctx.max_ctx based on
  41. * maximum RID that is supported by boot CPU.
  42. */
  43. void __init
  44. mmu_context_init (void)
  45. {
  46. ia64_ctx.bitmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3);
  47. ia64_ctx.flushmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3);
  48. }
  49. /*
  50. * Acquire the ia64_ctx.lock before calling this function!
  51. */
  52. void
  53. wrap_mmu_context (struct mm_struct *mm)
  54. {
  55. int i, cpu;
  56. unsigned long flush_bit;
  57. for (i=0; i <= ia64_ctx.max_ctx / BITS_PER_LONG; i++) {
  58. flush_bit = xchg(&ia64_ctx.flushmap[i], 0);
  59. ia64_ctx.bitmap[i] ^= flush_bit;
  60. }
  61. /* use offset at 300 to skip daemons */
  62. ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
  63. ia64_ctx.max_ctx, 300);
  64. ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
  65. ia64_ctx.max_ctx, ia64_ctx.next);
  66. /*
  67. * can't call flush_tlb_all() here because of race condition
  68. * with O(1) scheduler [EF]
  69. */
  70. cpu = get_cpu(); /* prevent preemption/migration */
  71. for_each_online_cpu(i)
  72. if (i != cpu)
  73. per_cpu(ia64_need_tlb_flush, i) = 1;
  74. put_cpu();
  75. local_flush_tlb_all();
  76. }
  77. void
  78. ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
  79. unsigned long end, unsigned long nbits)
  80. {
  81. static DEFINE_SPINLOCK(ptcg_lock);
  82. struct mm_struct *active_mm = current->active_mm;
  83. if (mm != active_mm) {
  84. /* Restore region IDs for mm */
  85. if (mm && active_mm) {
  86. activate_context(mm);
  87. } else {
  88. flush_tlb_all();
  89. return;
  90. }
  91. }
  92. /* HW requires global serialization of ptc.ga. */
  93. spin_lock(&ptcg_lock);
  94. {
  95. do {
  96. /*
  97. * Flush ALAT entries also.
  98. */
  99. ia64_ptcga(start, (nbits<<2));
  100. ia64_srlz_i();
  101. start += (1UL << nbits);
  102. } while (start < end);
  103. }
  104. spin_unlock(&ptcg_lock);
  105. if (mm != active_mm) {
  106. activate_context(active_mm);
  107. }
  108. }
  109. void
  110. local_flush_tlb_all (void)
  111. {
  112. unsigned long i, j, flags, count0, count1, stride0, stride1, addr;
  113. addr = local_cpu_data->ptce_base;
  114. count0 = local_cpu_data->ptce_count[0];
  115. count1 = local_cpu_data->ptce_count[1];
  116. stride0 = local_cpu_data->ptce_stride[0];
  117. stride1 = local_cpu_data->ptce_stride[1];
  118. local_irq_save(flags);
  119. for (i = 0; i < count0; ++i) {
  120. for (j = 0; j < count1; ++j) {
  121. ia64_ptce(addr);
  122. addr += stride1;
  123. }
  124. addr += stride0;
  125. }
  126. local_irq_restore(flags);
  127. ia64_srlz_i(); /* srlz.i implies srlz.d */
  128. }
  129. void
  130. flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
  131. unsigned long end)
  132. {
  133. struct mm_struct *mm = vma->vm_mm;
  134. unsigned long size = end - start;
  135. unsigned long nbits;
  136. #ifndef CONFIG_SMP
  137. if (mm != current->active_mm) {
  138. mm->context = 0;
  139. return;
  140. }
  141. #endif
  142. nbits = ia64_fls(size + 0xfff);
  143. while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
  144. (nbits < purge.max_bits))
  145. ++nbits;
  146. if (nbits > purge.max_bits)
  147. nbits = purge.max_bits;
  148. start &= ~((1UL << nbits) - 1);
  149. preempt_disable();
  150. #ifdef CONFIG_SMP
  151. if (mm != current->active_mm || cpus_weight(mm->cpu_vm_mask) != 1) {
  152. platform_global_tlb_purge(mm, start, end, nbits);
  153. preempt_enable();
  154. return;
  155. }
  156. #endif
  157. do {
  158. ia64_ptcl(start, (nbits<<2));
  159. start += (1UL << nbits);
  160. } while (start < end);
  161. preempt_enable();
  162. ia64_srlz_i(); /* srlz.i implies srlz.d */
  163. }
  164. EXPORT_SYMBOL(flush_tlb_range);
  165. void __devinit
  166. ia64_tlb_init (void)
  167. {
  168. ia64_ptce_info_t uninitialized_var(ptce_info); /* GCC be quiet */
  169. unsigned long tr_pgbits;
  170. long status;
  171. if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) {
  172. printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; "
  173. "defaulting to architected purge page-sizes.\n", status);
  174. purge.mask = 0x115557000UL;
  175. }
  176. purge.max_bits = ia64_fls(purge.mask);
  177. ia64_get_ptce(&ptce_info);
  178. local_cpu_data->ptce_base = ptce_info.base;
  179. local_cpu_data->ptce_count[0] = ptce_info.count[0];
  180. local_cpu_data->ptce_count[1] = ptce_info.count[1];
  181. local_cpu_data->ptce_stride[0] = ptce_info.stride[0];
  182. local_cpu_data->ptce_stride[1] = ptce_info.stride[1];
  183. local_flush_tlb_all(); /* nuke left overs from bootstrapping... */
  184. }