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 <asm/arch/hwregs/asm/mmu_defs_asm.h>
  12. #include <asm/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_save_flags(flags);
  52. local_irq_disable();
  53. for (mmu = 1; mmu <= 2; mmu++) {
  54. SUPP_BANK_SEL(mmu); /* Select the MMU */
  55. for (i = 0; i < NUM_TLB_ENTRIES; i++) {
  56. /* Store invalid entry */
  57. mmu_tlb_sel = REG_FIELD(mmu, rw_mm_tlb_sel, idx, i);
  58. mmu_tlb_hi = (REG_FIELD(mmu, rw_mm_tlb_hi, pid, INVALID_PAGEID)
  59. | REG_FIELD(mmu, rw_mm_tlb_hi, vpn, i & 0xf));
  60. SUPP_REG_WR(RW_MM_TLB_SEL, mmu_tlb_sel);
  61. SUPP_REG_WR(RW_MM_TLB_HI, mmu_tlb_hi);
  62. SUPP_REG_WR(RW_MM_TLB_LO, 0);
  63. }
  64. }
  65. local_irq_restore(flags);
  66. }
  67. /* Flush an entire user address space. */
  68. void
  69. __flush_tlb_mm(struct mm_struct *mm)
  70. {
  71. int i;
  72. int mmu;
  73. unsigned long flags;
  74. unsigned long page_id;
  75. unsigned long tlb_hi;
  76. unsigned long mmu_tlb_hi;
  77. page_id = mm->context.page_id;
  78. if (page_id == NO_CONTEXT)
  79. return;
  80. /* Mark the TLB entries that match the page_id as invalid. */
  81. local_save_flags(flags);
  82. local_irq_disable();
  83. for (mmu = 1; mmu <= 2; mmu++) {
  84. SUPP_BANK_SEL(mmu);
  85. for (i = 0; i < NUM_TLB_ENTRIES; i++) {
  86. UPDATE_TLB_SEL_IDX(i);
  87. /* Get the page_id */
  88. SUPP_REG_RD(RW_MM_TLB_HI, tlb_hi);
  89. /* Check if the page_id match. */
  90. if ((tlb_hi & 0xff) == page_id) {
  91. mmu_tlb_hi = (REG_FIELD(mmu, rw_mm_tlb_hi, pid,
  92. INVALID_PAGEID)
  93. | REG_FIELD(mmu, rw_mm_tlb_hi, vpn,
  94. i & 0xf));
  95. UPDATE_TLB_HILO(mmu_tlb_hi, 0);
  96. }
  97. }
  98. }
  99. local_irq_restore(flags);
  100. }
  101. /* Invalidate a single page. */
  102. void
  103. __flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
  104. {
  105. int i;
  106. int mmu;
  107. unsigned long page_id;
  108. unsigned long flags;
  109. unsigned long tlb_hi;
  110. unsigned long mmu_tlb_hi;
  111. page_id = vma->vm_mm->context.page_id;
  112. if (page_id == NO_CONTEXT)
  113. return;
  114. addr &= PAGE_MASK;
  115. /*
  116. * Invalidate those TLB entries that match both the mm context and the
  117. * requested virtual address.
  118. */
  119. local_save_flags(flags);
  120. local_irq_disable();
  121. for (mmu = 1; mmu <= 2; mmu++) {
  122. SUPP_BANK_SEL(mmu);
  123. for (i = 0; i < NUM_TLB_ENTRIES; i++) {
  124. UPDATE_TLB_SEL_IDX(i);
  125. SUPP_REG_RD(RW_MM_TLB_HI, tlb_hi);
  126. /* Check if page_id and address matches */
  127. if (((tlb_hi & 0xff) == page_id) &&
  128. ((tlb_hi & PAGE_MASK) == addr)) {
  129. mmu_tlb_hi = REG_FIELD(mmu, rw_mm_tlb_hi, pid,
  130. INVALID_PAGEID) | addr;
  131. UPDATE_TLB_HILO(mmu_tlb_hi, 0);
  132. }
  133. }
  134. }
  135. local_irq_restore(flags);
  136. }
  137. /*
  138. * Initialize the context related info for a new mm_struct
  139. * instance.
  140. */
  141. int
  142. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  143. {
  144. mm->context.page_id = NO_CONTEXT;
  145. return 0;
  146. }
  147. /* Called in schedule() just before actually doing the switch_to. */
  148. void
  149. switch_mm(struct mm_struct *prev, struct mm_struct *next,
  150. struct task_struct *tsk)
  151. {
  152. int cpu = smp_processor_id();
  153. /* Make sure there is a MMU context. */
  154. spin_lock(&next->page_table_lock);
  155. get_mmu_context(next);
  156. cpu_set(cpu, next->cpu_vm_mask);
  157. spin_unlock(&next->page_table_lock);
  158. /*
  159. * Remember the pgd for the fault handlers. Keep a seperate copy of it
  160. * because current and active_mm might be invalid at points where
  161. * there's still a need to derefer the pgd.
  162. */
  163. per_cpu(current_pgd, cpu) = next->pgd;
  164. /* Switch context in the MMU. */
  165. if (tsk && tsk->thread_info)
  166. {
  167. SPEC_REG_WR(SPEC_REG_PID, next->context.page_id | tsk->thread_info->tls);
  168. }
  169. else
  170. {
  171. SPEC_REG_WR(SPEC_REG_PID, next->context.page_id);
  172. }
  173. }