tlb-r3k.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * r2300.c: R2000 and R3000 specific mmu/cache code.
  4. *
  5. * Copyright (C) 1996 David S. Miller ([email protected])
  6. *
  7. * with a lot of changes to make this thing work for R3000s
  8. * Tx39XX R4k style caches added. HK
  9. * Copyright (C) 1998, 1999, 2000 Harald Koerfgen
  10. * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
  11. * Copyright (C) 2002 Ralf Baechle
  12. * Copyright (C) 2002 Maciej W. Rozycki
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/smp.h>
  17. #include <linux/mm.h>
  18. #include <asm/page.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. /* TLB operations. */
  35. static void local_flush_tlb_from(int entry)
  36. {
  37. unsigned long old_ctx;
  38. old_ctx = read_c0_entryhi() & cpu_asid_mask(&current_cpu_data);
  39. write_c0_entrylo0(0);
  40. while (entry < current_cpu_data.tlbsize) {
  41. write_c0_index(entry << 8);
  42. write_c0_entryhi((entry | 0x80000) << 12);
  43. entry++; /* BARRIER */
  44. tlb_write_indexed();
  45. }
  46. write_c0_entryhi(old_ctx);
  47. }
  48. void local_flush_tlb_all(void)
  49. {
  50. unsigned long flags;
  51. #ifdef DEBUG_TLB
  52. printk("[tlball]");
  53. #endif
  54. local_irq_save(flags);
  55. local_flush_tlb_from(8);
  56. local_irq_restore(flags);
  57. }
  58. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  59. unsigned long end)
  60. {
  61. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  62. struct mm_struct *mm = vma->vm_mm;
  63. int cpu = smp_processor_id();
  64. if (cpu_context(cpu, mm) != 0) {
  65. unsigned long size, flags;
  66. #ifdef DEBUG_TLB
  67. printk("[tlbrange<%lu,0x%08lx,0x%08lx>]",
  68. cpu_context(cpu, mm) & asid_mask, start, end);
  69. #endif
  70. local_irq_save(flags);
  71. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  72. if (size <= current_cpu_data.tlbsize) {
  73. int oldpid = read_c0_entryhi() & asid_mask;
  74. int newpid = cpu_context(cpu, mm) & asid_mask;
  75. start &= PAGE_MASK;
  76. end += PAGE_SIZE - 1;
  77. end &= PAGE_MASK;
  78. while (start < end) {
  79. int idx;
  80. write_c0_entryhi(start | newpid);
  81. start += PAGE_SIZE; /* BARRIER */
  82. tlb_probe();
  83. idx = read_c0_index();
  84. write_c0_entrylo0(0);
  85. write_c0_entryhi(KSEG0);
  86. if (idx < 0) /* BARRIER */
  87. continue;
  88. tlb_write_indexed();
  89. }
  90. write_c0_entryhi(oldpid);
  91. } else {
  92. drop_mmu_context(mm);
  93. }
  94. local_irq_restore(flags);
  95. }
  96. }
  97. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  98. {
  99. unsigned long size, flags;
  100. #ifdef DEBUG_TLB
  101. printk("[tlbrange<%lu,0x%08lx,0x%08lx>]", start, end);
  102. #endif
  103. local_irq_save(flags);
  104. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  105. if (size <= current_cpu_data.tlbsize) {
  106. int pid = read_c0_entryhi();
  107. start &= PAGE_MASK;
  108. end += PAGE_SIZE - 1;
  109. end &= PAGE_MASK;
  110. while (start < end) {
  111. int idx;
  112. write_c0_entryhi(start);
  113. start += PAGE_SIZE; /* BARRIER */
  114. tlb_probe();
  115. idx = read_c0_index();
  116. write_c0_entrylo0(0);
  117. write_c0_entryhi(KSEG0);
  118. if (idx < 0) /* BARRIER */
  119. continue;
  120. tlb_write_indexed();
  121. }
  122. write_c0_entryhi(pid);
  123. } else {
  124. local_flush_tlb_all();
  125. }
  126. local_irq_restore(flags);
  127. }
  128. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  129. {
  130. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  131. int cpu = smp_processor_id();
  132. if (cpu_context(cpu, vma->vm_mm) != 0) {
  133. unsigned long flags;
  134. int oldpid, newpid, idx;
  135. #ifdef DEBUG_TLB
  136. printk("[tlbpage<%lu,0x%08lx>]", cpu_context(cpu, vma->vm_mm), page);
  137. #endif
  138. newpid = cpu_context(cpu, vma->vm_mm) & asid_mask;
  139. page &= PAGE_MASK;
  140. local_irq_save(flags);
  141. oldpid = read_c0_entryhi() & asid_mask;
  142. write_c0_entryhi(page | newpid);
  143. BARRIER;
  144. tlb_probe();
  145. idx = read_c0_index();
  146. write_c0_entrylo0(0);
  147. write_c0_entryhi(KSEG0);
  148. if (idx < 0) /* BARRIER */
  149. goto finish;
  150. tlb_write_indexed();
  151. finish:
  152. write_c0_entryhi(oldpid);
  153. local_irq_restore(flags);
  154. }
  155. }
  156. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  157. {
  158. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  159. unsigned long flags;
  160. int idx, pid;
  161. /*
  162. * Handle debugger faulting in for debugee.
  163. */
  164. if (current->active_mm != vma->vm_mm)
  165. return;
  166. pid = read_c0_entryhi() & asid_mask;
  167. #ifdef DEBUG_TLB
  168. if ((pid != (cpu_context(cpu, vma->vm_mm) & asid_mask)) || (cpu_context(cpu, vma->vm_mm) == 0)) {
  169. printk("update_mmu_cache: Wheee, bogus tlbpid mmpid=%lu tlbpid=%d\n",
  170. (cpu_context(cpu, vma->vm_mm)), pid);
  171. }
  172. #endif
  173. local_irq_save(flags);
  174. address &= PAGE_MASK;
  175. write_c0_entryhi(address | pid);
  176. BARRIER;
  177. tlb_probe();
  178. idx = read_c0_index();
  179. write_c0_entrylo0(pte_val(pte));
  180. write_c0_entryhi(address | pid);
  181. if (idx < 0) { /* BARRIER */
  182. tlb_write_random();
  183. } else {
  184. tlb_write_indexed();
  185. }
  186. write_c0_entryhi(pid);
  187. local_irq_restore(flags);
  188. }
  189. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  190. unsigned long entryhi, unsigned long pagemask)
  191. {
  192. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  193. unsigned long flags;
  194. unsigned long old_ctx;
  195. static unsigned long wired = 0;
  196. if (wired < 8) {
  197. #ifdef DEBUG_TLB
  198. printk("[tlbwired<entry lo0 %8x, hi %8x\n>]\n",
  199. entrylo0, entryhi);
  200. #endif
  201. local_irq_save(flags);
  202. old_ctx = read_c0_entryhi() & asid_mask;
  203. write_c0_entrylo0(entrylo0);
  204. write_c0_entryhi(entryhi);
  205. write_c0_index(wired);
  206. wired++; /* BARRIER */
  207. tlb_write_indexed();
  208. write_c0_entryhi(old_ctx);
  209. local_flush_tlb_all();
  210. local_irq_restore(flags);
  211. }
  212. }
  213. void tlb_init(void)
  214. {
  215. local_flush_tlb_from(0);
  216. build_tlb_refill_handler();
  217. }