tlb.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Low level TLB handling.
  3. *
  4. * Copyright (C) 2000-2003, Axis Communications AB.
  5. *
  6. * Authors: Bjorn Wesen <bjornw@axis.com>
  7. * Tobias Anderberg <tobiasa@axis.com>, CRISv32 port.
  8. */
  9. #include <asm/tlb.h>
  10. #include <asm/mmu_context.h>
  11. #include <arch/hwregs/asm/mmu_defs_asm.h>
  12. #include <arch/hwregs/supp_reg.h>
  13. #define UPDATE_TLB_SEL_IDX(val) \
  14. do { \
  15. unsigned long tlb_sel; \
  16. \
  17. tlb_sel = REG_FIELD(mmu, rw_mm_tlb_sel, idx, val); \
  18. SUPP_REG_WR(RW_MM_TLB_SEL, tlb_sel); \
  19. } while(0)
  20. #define UPDATE_TLB_HILO(tlb_hi, tlb_lo) \
  21. do { \
  22. SUPP_REG_WR(RW_MM_TLB_HI, tlb_hi); \
  23. SUPP_REG_WR(RW_MM_TLB_LO, tlb_lo); \
  24. } while(0)
  25. /*
  26. * The TLB can host up to 256 different mm contexts at the same time. The running
  27. * context is found in the PID register. Each TLB entry contains a page_id that
  28. * has to match the PID register to give a hit. page_id_map keeps track of which
  29. * mm's is assigned to which page_id's, making sure it's known when to
  30. * invalidate TLB entries.
  31. *
  32. * The last page_id is never running, it is used as an invalid page_id so that
  33. * it's possible to make TLB entries that will nerver match.
  34. *
  35. * Note; the flushes needs to be atomic otherwise an interrupt hander that uses
  36. * vmalloc'ed memory might cause a TLB load in the middle of a flush.
  37. */
  38. /* Flush all TLB entries. */
  39. void
  40. __flush_tlb_all(void)
  41. {
  42. int i;
  43. int mmu;
  44. unsigned long flags;
  45. unsigned long mmu_tlb_hi;
  46. unsigned long mmu_tlb_sel;
  47. /*
  48. * Mask with 0xf so similar TLB entries aren't written in the same 4-way
  49. * entry group.
  50. */
  51. local_irq_save(flags);
  52. for (mmu = 1; mmu <= 2; mmu++) {
  53. SUPP_BANK_SEL(mmu); /* Select the MMU */
  54. for (i = 0; i < NUM_TLB_ENTRIES; i++) {
  55. /* Store invalid entry */
  56. mmu_tlb_sel = REG_FIELD(mmu, rw_mm_tlb_sel, idx, i);
  57. mmu_tlb_hi = (REG_FIELD(mmu, rw_mm_tlb_hi, pid, INVALID_PAGEID)
  58. | REG_FIELD(mmu, rw_mm_tlb_hi, vpn, i & 0xf));
  59. SUPP_REG_WR(RW_MM_TLB_SEL, mmu_tlb_sel);
  60. SUPP_REG_WR(RW_MM_TLB_HI, mmu_tlb_hi);
  61. SUPP_REG_WR(RW_MM_TLB_LO, 0);
  62. }
  63. }
  64. local_irq_restore(flags);
  65. }
  66. /* Flush an entire user address space. */
  67. void
  68. __flush_tlb_mm(struct mm_struct *mm)
  69. {
  70. int i;
  71. int mmu;
  72. unsigned long flags;
  73. unsigned long page_id;
  74. unsigned long tlb_hi;
  75. unsigned long mmu_tlb_hi;
  76. page_id = mm->context.page_id;
  77. if (page_id == NO_CONTEXT)
  78. return;
  79. /* Mark the TLB entries that match the page_id as invalid. */
  80. local_irq_save(flags);
  81. for (mmu = 1; mmu <= 2; mmu++) {
  82. SUPP_BANK_SEL(mmu);
  83. for (i = 0; i < NUM_TLB_ENTRIES; i++) {
  84. UPDATE_TLB_SEL_IDX(i);
  85. /* Get the page_id */
  86. SUPP_REG_RD(RW_MM_TLB_HI, tlb_hi);
  87. /* Check if the page_id match. */
  88. if ((tlb_hi & 0xff) == page_id) {
  89. mmu_tlb_hi = (REG_FIELD(mmu, rw_mm_tlb_hi, pid,
  90. INVALID_PAGEID)
  91. | REG_FIELD(mmu, rw_mm_tlb_hi, vpn,
  92. i & 0xf));
  93. UPDATE_TLB_HILO(mmu_tlb_hi, 0);
  94. }
  95. }
  96. }
  97. local_irq_restore(flags);
  98. }
  99. /* Invalidate a single page. */
  100. void
  101. __flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
  102. {
  103. int i;
  104. int mmu;
  105. unsigned long page_id;
  106. unsigned long flags;
  107. unsigned long tlb_hi;
  108. unsigned long mmu_tlb_hi;
  109. page_id = vma->vm_mm->context.page_id;
  110. if (page_id == NO_CONTEXT)
  111. return;
  112. addr &= PAGE_MASK;
  113. /*
  114. * Invalidate those TLB entries that match both the mm context and the
  115. * requested virtual address.
  116. */
  117. local_irq_save(flags);
  118. for (mmu = 1; mmu <= 2; mmu++) {
  119. SUPP_BANK_SEL(mmu);
  120. for (i = 0; i < NUM_TLB_ENTRIES; i++) {
  121. UPDATE_TLB_SEL_IDX(i);
  122. SUPP_REG_RD(RW_MM_TLB_HI, tlb_hi);
  123. /* Check if page_id and address matches */
  124. if (((tlb_hi & 0xff) == page_id) &&
  125. ((tlb_hi & PAGE_MASK) == addr)) {
  126. mmu_tlb_hi = REG_FIELD(mmu, rw_mm_tlb_hi, pid,
  127. INVALID_PAGEID) | addr;
  128. UPDATE_TLB_HILO(mmu_tlb_hi, 0);
  129. }
  130. }
  131. }
  132. local_irq_restore(flags);
  133. }
  134. /*
  135. * Initialize the context related info for a new mm_struct
  136. * instance.
  137. */
  138. int
  139. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  140. {
  141. mm->context.page_id = NO_CONTEXT;
  142. return 0;
  143. }
  144. static DEFINE_SPINLOCK(mmu_context_lock);
  145. /* Called in schedule() just before actually doing the switch_to. */
  146. void
  147. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  148. struct task_struct *tsk)
  149. {
  150. if (prev != next) {
  151. int cpu = smp_processor_id();
  152. /* Make sure there is a MMU context. */
  153. spin_lock(&mmu_context_lock);
  154. get_mmu_context(next);
  155. cpumask_set_cpu(cpu, mm_cpumask(next));
  156. spin_unlock(&mmu_context_lock);
  157. /*
  158. * Remember the pgd for the fault handlers. Keep a separate
  159. * copy of it because current and active_mm might be invalid
  160. * at points where * there's still a need to derefer the pgd.
  161. */
  162. per_cpu(current_pgd, cpu) = next->pgd;
  163. /* Switch context in the MMU. */
  164. if (tsk && task_thread_info(tsk)) {
  165. SPEC_REG_WR(SPEC_REG_PID, next->context.page_id |
  166. task_thread_info(tsk)->tls);
  167. } else {
  168. SPEC_REG_WR(SPEC_REG_PID, next->context.page_id);
  169. }
  170. }
  171. }