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 (davem@davemloft.net)
  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. #include <asm/setup.h>
  26. #include <asm/tlbex.h>
  27. #undef DEBUG_TLB
  28. /* CP0 hazard avoidance. */
  29. #define BARRIER \
  30. __asm__ __volatile__( \
  31. ".set push\n\t" \
  32. ".set noreorder\n\t" \
  33. "nop\n\t" \
  34. ".set pop\n\t")
  35. /* TLB operations. */
  36. static void local_flush_tlb_from(int entry)
  37. {
  38. unsigned long old_ctx;
  39. old_ctx = read_c0_entryhi() & cpu_asid_mask(&current_cpu_data);
  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(8);
  57. local_irq_restore(flags);
  58. }
  59. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  60. unsigned long end)
  61. {
  62. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  63. struct mm_struct *mm = vma->vm_mm;
  64. int cpu = smp_processor_id();
  65. if (cpu_context(cpu, mm) != 0) {
  66. unsigned long size, flags;
  67. #ifdef DEBUG_TLB
  68. printk("[tlbrange<%lu,0x%08lx,0x%08lx>]",
  69. cpu_context(cpu, mm) & asid_mask, start, end);
  70. #endif
  71. local_irq_save(flags);
  72. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  73. if (size <= current_cpu_data.tlbsize) {
  74. int oldpid = read_c0_entryhi() & asid_mask;
  75. int newpid = cpu_context(cpu, mm) & asid_mask;
  76. start &= PAGE_MASK;
  77. end += PAGE_SIZE - 1;
  78. end &= PAGE_MASK;
  79. while (start < end) {
  80. int idx;
  81. write_c0_entryhi(start | newpid);
  82. start += PAGE_SIZE; /* BARRIER */
  83. tlb_probe();
  84. idx = read_c0_index();
  85. write_c0_entrylo0(0);
  86. write_c0_entryhi(KSEG0);
  87. if (idx < 0) /* BARRIER */
  88. continue;
  89. tlb_write_indexed();
  90. }
  91. write_c0_entryhi(oldpid);
  92. } else {
  93. drop_mmu_context(mm);
  94. }
  95. local_irq_restore(flags);
  96. }
  97. }
  98. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  99. {
  100. unsigned long size, flags;
  101. #ifdef DEBUG_TLB
  102. printk("[tlbrange<%lu,0x%08lx,0x%08lx>]", start, end);
  103. #endif
  104. local_irq_save(flags);
  105. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  106. if (size <= current_cpu_data.tlbsize) {
  107. int pid = read_c0_entryhi();
  108. start &= PAGE_MASK;
  109. end += PAGE_SIZE - 1;
  110. end &= PAGE_MASK;
  111. while (start < end) {
  112. int idx;
  113. write_c0_entryhi(start);
  114. start += PAGE_SIZE; /* BARRIER */
  115. tlb_probe();
  116. idx = read_c0_index();
  117. write_c0_entrylo0(0);
  118. write_c0_entryhi(KSEG0);
  119. if (idx < 0) /* BARRIER */
  120. continue;
  121. tlb_write_indexed();
  122. }
  123. write_c0_entryhi(pid);
  124. } else {
  125. local_flush_tlb_all();
  126. }
  127. local_irq_restore(flags);
  128. }
  129. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  130. {
  131. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  132. int cpu = smp_processor_id();
  133. if (cpu_context(cpu, vma->vm_mm) != 0) {
  134. unsigned long flags;
  135. int oldpid, newpid, idx;
  136. #ifdef DEBUG_TLB
  137. printk("[tlbpage<%lu,0x%08lx>]", cpu_context(cpu, vma->vm_mm), page);
  138. #endif
  139. newpid = cpu_context(cpu, vma->vm_mm) & asid_mask;
  140. page &= PAGE_MASK;
  141. local_irq_save(flags);
  142. oldpid = read_c0_entryhi() & asid_mask;
  143. write_c0_entryhi(page | newpid);
  144. BARRIER;
  145. tlb_probe();
  146. idx = read_c0_index();
  147. write_c0_entrylo0(0);
  148. write_c0_entryhi(KSEG0);
  149. if (idx < 0) /* BARRIER */
  150. goto finish;
  151. tlb_write_indexed();
  152. finish:
  153. write_c0_entryhi(oldpid);
  154. local_irq_restore(flags);
  155. }
  156. }
  157. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  158. {
  159. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  160. unsigned long flags;
  161. int idx, pid;
  162. /*
  163. * Handle debugger faulting in for debuggee.
  164. */
  165. if (current->active_mm != vma->vm_mm)
  166. return;
  167. pid = read_c0_entryhi() & asid_mask;
  168. #ifdef DEBUG_TLB
  169. if ((pid != (cpu_context(cpu, vma->vm_mm) & asid_mask)) || (cpu_context(cpu, vma->vm_mm) == 0)) {
  170. printk("update_mmu_cache: Wheee, bogus tlbpid mmpid=%lu tlbpid=%d\n",
  171. (cpu_context(cpu, vma->vm_mm)), pid);
  172. }
  173. #endif
  174. local_irq_save(flags);
  175. address &= PAGE_MASK;
  176. write_c0_entryhi(address | pid);
  177. BARRIER;
  178. tlb_probe();
  179. idx = read_c0_index();
  180. write_c0_entrylo0(pte_val(pte));
  181. write_c0_entryhi(address | pid);
  182. if (idx < 0) { /* BARRIER */
  183. tlb_write_random();
  184. } else {
  185. tlb_write_indexed();
  186. }
  187. write_c0_entryhi(pid);
  188. local_irq_restore(flags);
  189. }
  190. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  191. unsigned long entryhi, unsigned long pagemask)
  192. {
  193. unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
  194. unsigned long flags;
  195. unsigned long old_ctx;
  196. static unsigned long wired = 0;
  197. if (wired < 8) {
  198. #ifdef DEBUG_TLB
  199. printk("[tlbwired<entry lo0 %8x, hi %8x\n>]\n",
  200. entrylo0, entryhi);
  201. #endif
  202. local_irq_save(flags);
  203. old_ctx = read_c0_entryhi() & asid_mask;
  204. write_c0_entrylo0(entrylo0);
  205. write_c0_entryhi(entryhi);
  206. write_c0_index(wired);
  207. wired++; /* BARRIER */
  208. tlb_write_indexed();
  209. write_c0_entryhi(old_ctx);
  210. local_flush_tlb_all();
  211. local_irq_restore(flags);
  212. }
  213. }
  214. void tlb_init(void)
  215. {
  216. local_flush_tlb_from(0);
  217. build_tlb_refill_handler();
  218. }