tlb-r3k.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * r2300.c: R2000 and R3000 specific mmu/cache code.
  3. *
  4. * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
  5. *
  6. * with a lot of changes to make this thing work for R3000s
  7. * Tx39XX R4k style caches added. HK
  8. * Copyright (C) 1998, 1999, 2000 Harald Koerfgen
  9. * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
  10. * Copyright (C) 2002 Ralf Baechle
  11. * Copyright (C) 2002 Maciej W. Rozycki
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/mm.h>
  17. #include <asm/page.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/tlbmisc.h>
  21. #include <asm/isadep.h>
  22. #include <asm/io.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/cpu.h>
  25. #undef DEBUG_TLB
  26. extern void build_tlb_refill_handler(void);
  27. /* CP0 hazard avoidance. */
  28. #define BARRIER \
  29. __asm__ __volatile__( \
  30. ".set push\n\t" \
  31. ".set noreorder\n\t" \
  32. "nop\n\t" \
  33. ".set pop\n\t")
  34. static int r3k_have_wired_reg; /* Should be in cpu_data? */
  35. /* TLB operations. */
  36. static void local_flush_tlb_from(int entry)
  37. {
  38. unsigned long old_ctx;
  39. old_ctx = read_c0_entryhi() & ASID_MASK;
  40. write_c0_entrylo0(0);
  41. while (entry < current_cpu_data.tlbsize) {
  42. write_c0_index(entry << 8);
  43. write_c0_entryhi((entry | 0x80000) << 12);
  44. entry++; /* BARRIER */
  45. tlb_write_indexed();
  46. }
  47. write_c0_entryhi(old_ctx);
  48. }
  49. void local_flush_tlb_all(void)
  50. {
  51. unsigned long flags;
  52. #ifdef DEBUG_TLB
  53. printk("[tlball]");
  54. #endif
  55. local_irq_save(flags);
  56. local_flush_tlb_from(r3k_have_wired_reg ? read_c0_wired() : 8);
  57. local_irq_restore(flags);
  58. }
  59. void local_flush_tlb_mm(struct mm_struct *mm)
  60. {
  61. int cpu = smp_processor_id();
  62. if (cpu_context(cpu, mm) != 0) {
  63. #ifdef DEBUG_TLB
  64. printk("[tlbmm<%lu>]", (unsigned long)cpu_context(cpu, mm));
  65. #endif
  66. drop_mmu_context(mm, cpu);
  67. }
  68. }
  69. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  70. unsigned long end)
  71. {
  72. struct mm_struct *mm = vma->vm_mm;
  73. int cpu = smp_processor_id();
  74. if (cpu_context(cpu, mm) != 0) {
  75. unsigned long size, flags;
  76. #ifdef DEBUG_TLB
  77. printk("[tlbrange<%lu,0x%08lx,0x%08lx>]",
  78. cpu_context(cpu, mm) & ASID_MASK, start, end);
  79. #endif
  80. local_irq_save(flags);
  81. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  82. if (size <= current_cpu_data.tlbsize) {
  83. int oldpid = read_c0_entryhi() & ASID_MASK;
  84. int newpid = cpu_context(cpu, mm) & ASID_MASK;
  85. start &= PAGE_MASK;
  86. end += PAGE_SIZE - 1;
  87. end &= PAGE_MASK;
  88. while (start < end) {
  89. int idx;
  90. write_c0_entryhi(start | newpid);
  91. start += PAGE_SIZE; /* BARRIER */
  92. tlb_probe();
  93. idx = read_c0_index();
  94. write_c0_entrylo0(0);
  95. write_c0_entryhi(KSEG0);
  96. if (idx < 0) /* BARRIER */
  97. continue;
  98. tlb_write_indexed();
  99. }
  100. write_c0_entryhi(oldpid);
  101. } else {
  102. drop_mmu_context(mm, cpu);
  103. }
  104. local_irq_restore(flags);
  105. }
  106. }
  107. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  108. {
  109. unsigned long size, flags;
  110. #ifdef DEBUG_TLB
  111. printk("[tlbrange<%lu,0x%08lx,0x%08lx>]", start, end);
  112. #endif
  113. local_irq_save(flags);
  114. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  115. if (size <= current_cpu_data.tlbsize) {
  116. int pid = read_c0_entryhi();
  117. start &= PAGE_MASK;
  118. end += PAGE_SIZE - 1;
  119. end &= PAGE_MASK;
  120. while (start < end) {
  121. int idx;
  122. write_c0_entryhi(start);
  123. start += PAGE_SIZE; /* BARRIER */
  124. tlb_probe();
  125. idx = read_c0_index();
  126. write_c0_entrylo0(0);
  127. write_c0_entryhi(KSEG0);
  128. if (idx < 0) /* BARRIER */
  129. continue;
  130. tlb_write_indexed();
  131. }
  132. write_c0_entryhi(pid);
  133. } else {
  134. local_flush_tlb_all();
  135. }
  136. local_irq_restore(flags);
  137. }
  138. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  139. {
  140. int cpu = smp_processor_id();
  141. if (cpu_context(cpu, vma->vm_mm) != 0) {
  142. unsigned long flags;
  143. int oldpid, newpid, idx;
  144. #ifdef DEBUG_TLB
  145. printk("[tlbpage<%lu,0x%08lx>]", cpu_context(cpu, vma->vm_mm), page);
  146. #endif
  147. newpid = cpu_context(cpu, vma->vm_mm) & ASID_MASK;
  148. page &= PAGE_MASK;
  149. local_irq_save(flags);
  150. oldpid = read_c0_entryhi() & ASID_MASK;
  151. write_c0_entryhi(page | newpid);
  152. BARRIER;
  153. tlb_probe();
  154. idx = read_c0_index();
  155. write_c0_entrylo0(0);
  156. write_c0_entryhi(KSEG0);
  157. if (idx < 0) /* BARRIER */
  158. goto finish;
  159. tlb_write_indexed();
  160. finish:
  161. write_c0_entryhi(oldpid);
  162. local_irq_restore(flags);
  163. }
  164. }
  165. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  166. {
  167. unsigned long flags;
  168. int idx, pid;
  169. /*
  170. * Handle debugger faulting in for debugee.
  171. */
  172. if (current->active_mm != vma->vm_mm)
  173. return;
  174. pid = read_c0_entryhi() & ASID_MASK;
  175. #ifdef DEBUG_TLB
  176. if ((pid != (cpu_context(cpu, vma->vm_mm) & ASID_MASK)) || (cpu_context(cpu, vma->vm_mm) == 0)) {
  177. printk("update_mmu_cache: Wheee, bogus tlbpid mmpid=%lu tlbpid=%d\n",
  178. (cpu_context(cpu, vma->vm_mm)), pid);
  179. }
  180. #endif
  181. local_irq_save(flags);
  182. address &= PAGE_MASK;
  183. write_c0_entryhi(address | pid);
  184. BARRIER;
  185. tlb_probe();
  186. idx = read_c0_index();
  187. write_c0_entrylo0(pte_val(pte));
  188. write_c0_entryhi(address | pid);
  189. if (idx < 0) { /* BARRIER */
  190. tlb_write_random();
  191. } else {
  192. tlb_write_indexed();
  193. }
  194. write_c0_entryhi(pid);
  195. local_irq_restore(flags);
  196. }
  197. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  198. unsigned long entryhi, unsigned long pagemask)
  199. {
  200. unsigned long flags;
  201. unsigned long old_ctx;
  202. static unsigned long wired = 0;
  203. if (r3k_have_wired_reg) { /* TX39XX */
  204. unsigned long old_pagemask;
  205. unsigned long w;
  206. #ifdef DEBUG_TLB
  207. printk("[tlbwired<entry lo0 %8x, hi %8x\n, pagemask %8x>]\n",
  208. entrylo0, entryhi, pagemask);
  209. #endif
  210. local_irq_save(flags);
  211. /* Save old context and create impossible VPN2 value */
  212. old_ctx = read_c0_entryhi() & ASID_MASK;
  213. old_pagemask = read_c0_pagemask();
  214. w = read_c0_wired();
  215. write_c0_wired(w + 1);
  216. write_c0_index(w << 8);
  217. write_c0_pagemask(pagemask);
  218. write_c0_entryhi(entryhi);
  219. write_c0_entrylo0(entrylo0);
  220. BARRIER;
  221. tlb_write_indexed();
  222. write_c0_entryhi(old_ctx);
  223. write_c0_pagemask(old_pagemask);
  224. local_flush_tlb_all();
  225. local_irq_restore(flags);
  226. } else if (wired < 8) {
  227. #ifdef DEBUG_TLB
  228. printk("[tlbwired<entry lo0 %8x, hi %8x\n>]\n",
  229. entrylo0, entryhi);
  230. #endif
  231. local_irq_save(flags);
  232. old_ctx = read_c0_entryhi() & ASID_MASK;
  233. write_c0_entrylo0(entrylo0);
  234. write_c0_entryhi(entryhi);
  235. write_c0_index(wired);
  236. wired++; /* BARRIER */
  237. tlb_write_indexed();
  238. write_c0_entryhi(old_ctx);
  239. local_flush_tlb_all();
  240. local_irq_restore(flags);
  241. }
  242. }
  243. void tlb_init(void)
  244. {
  245. switch (current_cpu_type()) {
  246. case CPU_TX3922:
  247. case CPU_TX3927:
  248. r3k_have_wired_reg = 1;
  249. write_c0_wired(0); /* Set to 8 on reset... */
  250. break;
  251. }
  252. local_flush_tlb_from(0);
  253. build_tlb_refill_handler();
  254. }